9d65b5fc9d87f884e32c53b1399a3a6e2516af75
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30
31 /* Define a symbol in a dynamic linkage section. */
32
33 struct elf_link_hash_entry *
34 _bfd_elf_define_linkage_sym (bfd *abfd,
35 struct bfd_link_info *info,
36 asection *sec,
37 const char *name)
38 {
39 struct elf_link_hash_entry *h;
40 struct bfd_link_hash_entry *bh;
41 const struct elf_backend_data *bed;
42
43 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
44 if (h != NULL)
45 {
46 /* Zap symbol defined in an as-needed lib that wasn't linked.
47 This is a symptom of a larger problem: Absolute symbols
48 defined in shared libraries can't be overridden, because we
49 lose the link to the bfd which is via the symbol section. */
50 h->root.type = bfd_link_hash_new;
51 }
52
53 bh = &h->root;
54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
55 sec, 0, NULL, FALSE,
56 get_elf_backend_data (abfd)->collect,
57 &bh))
58 return NULL;
59 h = (struct elf_link_hash_entry *) bh;
60 h->def_regular = 1;
61 h->type = STT_OBJECT;
62 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
63
64 bed = get_elf_backend_data (abfd);
65 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
66 return h;
67 }
68
69 bfd_boolean
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
71 {
72 flagword flags;
73 asection *s;
74 struct elf_link_hash_entry *h;
75 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
76 int ptralign;
77
78 /* This function may be called more than once. */
79 s = bfd_get_section_by_name (abfd, ".got");
80 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
81 return TRUE;
82
83 switch (bed->s->arch_size)
84 {
85 case 32:
86 ptralign = 2;
87 break;
88
89 case 64:
90 ptralign = 3;
91 break;
92
93 default:
94 bfd_set_error (bfd_error_bad_value);
95 return FALSE;
96 }
97
98 flags = bed->dynamic_sec_flags;
99
100 s = bfd_make_section_with_flags (abfd, ".got", flags);
101 if (s == NULL
102 || !bfd_set_section_alignment (abfd, s, ptralign))
103 return FALSE;
104
105 if (bed->want_got_plt)
106 {
107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
108 if (s == NULL
109 || !bfd_set_section_alignment (abfd, s, ptralign))
110 return FALSE;
111 }
112
113 if (bed->want_got_sym)
114 {
115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 (or .got.plt) section. We don't do this in the linker script
117 because we don't want to define the symbol if we are not creating
118 a global offset table. */
119 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
120 elf_hash_table (info)->hgot = h;
121 if (h == NULL)
122 return FALSE;
123 }
124
125 /* The first bit of the global offset table is the header. */
126 s->size += bed->got_header_size;
127
128 return TRUE;
129 }
130 \f
131 /* Create a strtab to hold the dynamic symbol names. */
132 static bfd_boolean
133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
134 {
135 struct elf_link_hash_table *hash_table;
136
137 hash_table = elf_hash_table (info);
138 if (hash_table->dynobj == NULL)
139 hash_table->dynobj = abfd;
140
141 if (hash_table->dynstr == NULL)
142 {
143 hash_table->dynstr = _bfd_elf_strtab_init ();
144 if (hash_table->dynstr == NULL)
145 return FALSE;
146 }
147 return TRUE;
148 }
149
150 /* Create some sections which will be filled in with dynamic linking
151 information. ABFD is an input file which requires dynamic sections
152 to be created. The dynamic sections take up virtual memory space
153 when the final executable is run, so we need to create them before
154 addresses are assigned to the output sections. We work out the
155 actual contents and size of these sections later. */
156
157 bfd_boolean
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
159 {
160 flagword flags;
161 register asection *s;
162 const struct elf_backend_data *bed;
163
164 if (! is_elf_hash_table (info->hash))
165 return FALSE;
166
167 if (elf_hash_table (info)->dynamic_sections_created)
168 return TRUE;
169
170 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
171 return FALSE;
172
173 abfd = elf_hash_table (info)->dynobj;
174 bed = get_elf_backend_data (abfd);
175
176 flags = bed->dynamic_sec_flags;
177
178 /* A dynamically linked executable has a .interp section, but a
179 shared library does not. */
180 if (info->executable)
181 {
182 s = bfd_make_section_with_flags (abfd, ".interp",
183 flags | SEC_READONLY);
184 if (s == NULL)
185 return FALSE;
186 }
187
188 if (! info->traditional_format)
189 {
190 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
191 flags | SEC_READONLY);
192 if (s == NULL
193 || ! bfd_set_section_alignment (abfd, s, 2))
194 return FALSE;
195 elf_hash_table (info)->eh_info.hdr_sec = s;
196 }
197
198 /* Create sections to hold version informations. These are removed
199 if they are not needed. */
200 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
201 flags | SEC_READONLY);
202 if (s == NULL
203 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
204 return FALSE;
205
206 s = bfd_make_section_with_flags (abfd, ".gnu.version",
207 flags | SEC_READONLY);
208 if (s == NULL
209 || ! bfd_set_section_alignment (abfd, s, 1))
210 return FALSE;
211
212 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
213 flags | SEC_READONLY);
214 if (s == NULL
215 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
216 return FALSE;
217
218 s = bfd_make_section_with_flags (abfd, ".dynsym",
219 flags | SEC_READONLY);
220 if (s == NULL
221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222 return FALSE;
223
224 s = bfd_make_section_with_flags (abfd, ".dynstr",
225 flags | SEC_READONLY);
226 if (s == NULL)
227 return FALSE;
228
229 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
230 if (s == NULL
231 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
232 return FALSE;
233
234 /* The special symbol _DYNAMIC is always set to the start of the
235 .dynamic section. We could set _DYNAMIC in a linker script, but we
236 only want to define it if we are, in fact, creating a .dynamic
237 section. We don't want to define it if there is no .dynamic
238 section, since on some ELF platforms the start up code examines it
239 to decide how to initialize the process. */
240 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
241 return FALSE;
242
243 if (info->emit_hash)
244 {
245 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
246 if (s == NULL
247 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
248 return FALSE;
249 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
250 }
251
252 if (info->emit_gnu_hash)
253 {
254 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
255 flags | SEC_READONLY);
256 if (s == NULL
257 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
258 return FALSE;
259 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
260 4 32-bit words followed by variable count of 64-bit words, then
261 variable count of 32-bit words. */
262 if (bed->s->arch_size == 64)
263 elf_section_data (s)->this_hdr.sh_entsize = 0;
264 else
265 elf_section_data (s)->this_hdr.sh_entsize = 4;
266 }
267
268 /* Let the backend create the rest of the sections. This lets the
269 backend set the right flags. The backend will normally create
270 the .got and .plt sections. */
271 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
272 return FALSE;
273
274 elf_hash_table (info)->dynamic_sections_created = TRUE;
275
276 return TRUE;
277 }
278
279 /* Create dynamic sections when linking against a dynamic object. */
280
281 bfd_boolean
282 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
283 {
284 flagword flags, pltflags;
285 struct elf_link_hash_entry *h;
286 asection *s;
287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
288
289 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
290 .rel[a].bss sections. */
291 flags = bed->dynamic_sec_flags;
292
293 pltflags = flags;
294 if (bed->plt_not_loaded)
295 /* We do not clear SEC_ALLOC here because we still want the OS to
296 allocate space for the section; it's just that there's nothing
297 to read in from the object file. */
298 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
299 else
300 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
301 if (bed->plt_readonly)
302 pltflags |= SEC_READONLY;
303
304 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
305 if (s == NULL
306 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
307 return FALSE;
308
309 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
310 .plt section. */
311 if (bed->want_plt_sym)
312 {
313 h = _bfd_elf_define_linkage_sym (abfd, info, s,
314 "_PROCEDURE_LINKAGE_TABLE_");
315 elf_hash_table (info)->hplt = h;
316 if (h == NULL)
317 return FALSE;
318 }
319
320 s = bfd_make_section_with_flags (abfd,
321 (bed->default_use_rela_p
322 ? ".rela.plt" : ".rel.plt"),
323 flags | SEC_READONLY);
324 if (s == NULL
325 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
326 return FALSE;
327
328 if (! _bfd_elf_create_got_section (abfd, info))
329 return FALSE;
330
331 if (bed->want_dynbss)
332 {
333 /* The .dynbss section is a place to put symbols which are defined
334 by dynamic objects, are referenced by regular objects, and are
335 not functions. We must allocate space for them in the process
336 image and use a R_*_COPY reloc to tell the dynamic linker to
337 initialize them at run time. The linker script puts the .dynbss
338 section into the .bss section of the final image. */
339 s = bfd_make_section_with_flags (abfd, ".dynbss",
340 (SEC_ALLOC
341 | SEC_LINKER_CREATED));
342 if (s == NULL)
343 return FALSE;
344
345 /* The .rel[a].bss section holds copy relocs. This section is not
346 normally needed. We need to create it here, though, so that the
347 linker will map it to an output section. We can't just create it
348 only if we need it, because we will not know whether we need it
349 until we have seen all the input files, and the first time the
350 main linker code calls BFD after examining all the input files
351 (size_dynamic_sections) the input sections have already been
352 mapped to the output sections. If the section turns out not to
353 be needed, we can discard it later. We will never need this
354 section when generating a shared object, since they do not use
355 copy relocs. */
356 if (! info->shared)
357 {
358 s = bfd_make_section_with_flags (abfd,
359 (bed->default_use_rela_p
360 ? ".rela.bss" : ".rel.bss"),
361 flags | SEC_READONLY);
362 if (s == NULL
363 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
364 return FALSE;
365 }
366 }
367
368 return TRUE;
369 }
370 \f
371 /* Record a new dynamic symbol. We record the dynamic symbols as we
372 read the input files, since we need to have a list of all of them
373 before we can determine the final sizes of the output sections.
374 Note that we may actually call this function even though we are not
375 going to output any dynamic symbols; in some cases we know that a
376 symbol should be in the dynamic symbol table, but only if there is
377 one. */
378
379 bfd_boolean
380 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
381 struct elf_link_hash_entry *h)
382 {
383 if (h->dynindx == -1)
384 {
385 struct elf_strtab_hash *dynstr;
386 char *p;
387 const char *name;
388 bfd_size_type indx;
389
390 /* XXX: The ABI draft says the linker must turn hidden and
391 internal symbols into STB_LOCAL symbols when producing the
392 DSO. However, if ld.so honors st_other in the dynamic table,
393 this would not be necessary. */
394 switch (ELF_ST_VISIBILITY (h->other))
395 {
396 case STV_INTERNAL:
397 case STV_HIDDEN:
398 if (h->root.type != bfd_link_hash_undefined
399 && h->root.type != bfd_link_hash_undefweak)
400 {
401 h->forced_local = 1;
402 if (!elf_hash_table (info)->is_relocatable_executable)
403 return TRUE;
404 }
405
406 default:
407 break;
408 }
409
410 h->dynindx = elf_hash_table (info)->dynsymcount;
411 ++elf_hash_table (info)->dynsymcount;
412
413 dynstr = elf_hash_table (info)->dynstr;
414 if (dynstr == NULL)
415 {
416 /* Create a strtab to hold the dynamic symbol names. */
417 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
418 if (dynstr == NULL)
419 return FALSE;
420 }
421
422 /* We don't put any version information in the dynamic string
423 table. */
424 name = h->root.root.string;
425 p = strchr (name, ELF_VER_CHR);
426 if (p != NULL)
427 /* We know that the p points into writable memory. In fact,
428 there are only a few symbols that have read-only names, being
429 those like _GLOBAL_OFFSET_TABLE_ that are created specially
430 by the backends. Most symbols will have names pointing into
431 an ELF string table read from a file, or to objalloc memory. */
432 *p = 0;
433
434 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
435
436 if (p != NULL)
437 *p = ELF_VER_CHR;
438
439 if (indx == (bfd_size_type) -1)
440 return FALSE;
441 h->dynstr_index = indx;
442 }
443
444 return TRUE;
445 }
446 \f
447 /* Record an assignment to a symbol made by a linker script. We need
448 this in case some dynamic object refers to this symbol. */
449
450 bfd_boolean
451 bfd_elf_record_link_assignment (bfd *output_bfd,
452 struct bfd_link_info *info,
453 const char *name,
454 bfd_boolean provide,
455 bfd_boolean hidden)
456 {
457 struct elf_link_hash_entry *h;
458 struct elf_link_hash_table *htab;
459
460 if (!is_elf_hash_table (info->hash))
461 return TRUE;
462
463 htab = elf_hash_table (info);
464 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
465 if (h == NULL)
466 return provide;
467
468 /* Since we're defining the symbol, don't let it seem to have not
469 been defined. record_dynamic_symbol and size_dynamic_sections
470 may depend on this. */
471 if (h->root.type == bfd_link_hash_undefweak
472 || h->root.type == bfd_link_hash_undefined)
473 {
474 h->root.type = bfd_link_hash_new;
475 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
476 bfd_link_repair_undef_list (&htab->root);
477 }
478
479 if (h->root.type == bfd_link_hash_new)
480 h->non_elf = 0;
481
482 /* If this symbol is being provided by the linker script, and it is
483 currently defined by a dynamic object, but not by a regular
484 object, then mark it as undefined so that the generic linker will
485 force the correct value. */
486 if (provide
487 && h->def_dynamic
488 && !h->def_regular)
489 h->root.type = bfd_link_hash_undefined;
490
491 /* If this symbol is not being provided by the linker script, and it is
492 currently defined by a dynamic object, but not by a regular object,
493 then clear out any version information because the symbol will not be
494 associated with the dynamic object any more. */
495 if (!provide
496 && h->def_dynamic
497 && !h->def_regular)
498 h->verinfo.verdef = NULL;
499
500 h->def_regular = 1;
501
502 if (provide && hidden)
503 {
504 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
505
506 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
507 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
508 }
509
510 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
511 and executables. */
512 if (!info->relocatable
513 && h->dynindx != -1
514 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
515 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
516 h->forced_local = 1;
517
518 if ((h->def_dynamic
519 || h->ref_dynamic
520 || info->shared
521 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
522 && h->dynindx == -1)
523 {
524 if (! bfd_elf_link_record_dynamic_symbol (info, h))
525 return FALSE;
526
527 /* If this is a weak defined symbol, and we know a corresponding
528 real symbol from the same dynamic object, make sure the real
529 symbol is also made into a dynamic symbol. */
530 if (h->u.weakdef != NULL
531 && h->u.weakdef->dynindx == -1)
532 {
533 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
534 return FALSE;
535 }
536 }
537
538 return TRUE;
539 }
540
541 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
542 success, and 2 on a failure caused by attempting to record a symbol
543 in a discarded section, eg. a discarded link-once section symbol. */
544
545 int
546 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
547 bfd *input_bfd,
548 long input_indx)
549 {
550 bfd_size_type amt;
551 struct elf_link_local_dynamic_entry *entry;
552 struct elf_link_hash_table *eht;
553 struct elf_strtab_hash *dynstr;
554 unsigned long dynstr_index;
555 char *name;
556 Elf_External_Sym_Shndx eshndx;
557 char esym[sizeof (Elf64_External_Sym)];
558
559 if (! is_elf_hash_table (info->hash))
560 return 0;
561
562 /* See if the entry exists already. */
563 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
564 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
565 return 1;
566
567 amt = sizeof (*entry);
568 entry = bfd_alloc (input_bfd, amt);
569 if (entry == NULL)
570 return 0;
571
572 /* Go find the symbol, so that we can find it's name. */
573 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
574 1, input_indx, &entry->isym, esym, &eshndx))
575 {
576 bfd_release (input_bfd, entry);
577 return 0;
578 }
579
580 if (entry->isym.st_shndx != SHN_UNDEF
581 && (entry->isym.st_shndx < SHN_LORESERVE
582 || entry->isym.st_shndx > SHN_HIRESERVE))
583 {
584 asection *s;
585
586 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
587 if (s == NULL || bfd_is_abs_section (s->output_section))
588 {
589 /* We can still bfd_release here as nothing has done another
590 bfd_alloc. We can't do this later in this function. */
591 bfd_release (input_bfd, entry);
592 return 2;
593 }
594 }
595
596 name = (bfd_elf_string_from_elf_section
597 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
598 entry->isym.st_name));
599
600 dynstr = elf_hash_table (info)->dynstr;
601 if (dynstr == NULL)
602 {
603 /* Create a strtab to hold the dynamic symbol names. */
604 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
605 if (dynstr == NULL)
606 return 0;
607 }
608
609 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
610 if (dynstr_index == (unsigned long) -1)
611 return 0;
612 entry->isym.st_name = dynstr_index;
613
614 eht = elf_hash_table (info);
615
616 entry->next = eht->dynlocal;
617 eht->dynlocal = entry;
618 entry->input_bfd = input_bfd;
619 entry->input_indx = input_indx;
620 eht->dynsymcount++;
621
622 /* Whatever binding the symbol had before, it's now local. */
623 entry->isym.st_info
624 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
625
626 /* The dynindx will be set at the end of size_dynamic_sections. */
627
628 return 1;
629 }
630
631 /* Return the dynindex of a local dynamic symbol. */
632
633 long
634 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
635 bfd *input_bfd,
636 long input_indx)
637 {
638 struct elf_link_local_dynamic_entry *e;
639
640 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
641 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
642 return e->dynindx;
643 return -1;
644 }
645
646 /* This function is used to renumber the dynamic symbols, if some of
647 them are removed because they are marked as local. This is called
648 via elf_link_hash_traverse. */
649
650 static bfd_boolean
651 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
652 void *data)
653 {
654 size_t *count = data;
655
656 if (h->root.type == bfd_link_hash_warning)
657 h = (struct elf_link_hash_entry *) h->root.u.i.link;
658
659 if (h->forced_local)
660 return TRUE;
661
662 if (h->dynindx != -1)
663 h->dynindx = ++(*count);
664
665 return TRUE;
666 }
667
668
669 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
670 STB_LOCAL binding. */
671
672 static bfd_boolean
673 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
674 void *data)
675 {
676 size_t *count = data;
677
678 if (h->root.type == bfd_link_hash_warning)
679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
680
681 if (!h->forced_local)
682 return TRUE;
683
684 if (h->dynindx != -1)
685 h->dynindx = ++(*count);
686
687 return TRUE;
688 }
689
690 /* Return true if the dynamic symbol for a given section should be
691 omitted when creating a shared library. */
692 bfd_boolean
693 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
694 struct bfd_link_info *info,
695 asection *p)
696 {
697 switch (elf_section_data (p)->this_hdr.sh_type)
698 {
699 case SHT_PROGBITS:
700 case SHT_NOBITS:
701 /* If sh_type is yet undecided, assume it could be
702 SHT_PROGBITS/SHT_NOBITS. */
703 case SHT_NULL:
704 if (strcmp (p->name, ".got") == 0
705 || strcmp (p->name, ".got.plt") == 0
706 || strcmp (p->name, ".plt") == 0)
707 {
708 asection *ip;
709 bfd *dynobj = elf_hash_table (info)->dynobj;
710
711 if (dynobj != NULL
712 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
713 && (ip->flags & SEC_LINKER_CREATED)
714 && ip->output_section == p)
715 return TRUE;
716 }
717 return FALSE;
718
719 /* There shouldn't be section relative relocations
720 against any other section. */
721 default:
722 return TRUE;
723 }
724 }
725
726 /* Assign dynsym indices. In a shared library we generate a section
727 symbol for each output section, which come first. Next come symbols
728 which have been forced to local binding. Then all of the back-end
729 allocated local dynamic syms, followed by the rest of the global
730 symbols. */
731
732 static unsigned long
733 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
734 struct bfd_link_info *info,
735 unsigned long *section_sym_count)
736 {
737 unsigned long dynsymcount = 0;
738
739 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
740 {
741 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
742 asection *p;
743 for (p = output_bfd->sections; p ; p = p->next)
744 if ((p->flags & SEC_EXCLUDE) == 0
745 && (p->flags & SEC_ALLOC) != 0
746 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
747 elf_section_data (p)->dynindx = ++dynsymcount;
748 }
749 *section_sym_count = dynsymcount;
750
751 elf_link_hash_traverse (elf_hash_table (info),
752 elf_link_renumber_local_hash_table_dynsyms,
753 &dynsymcount);
754
755 if (elf_hash_table (info)->dynlocal)
756 {
757 struct elf_link_local_dynamic_entry *p;
758 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
759 p->dynindx = ++dynsymcount;
760 }
761
762 elf_link_hash_traverse (elf_hash_table (info),
763 elf_link_renumber_hash_table_dynsyms,
764 &dynsymcount);
765
766 /* There is an unused NULL entry at the head of the table which
767 we must account for in our count. Unless there weren't any
768 symbols, which means we'll have no table at all. */
769 if (dynsymcount != 0)
770 ++dynsymcount;
771
772 elf_hash_table (info)->dynsymcount = dynsymcount;
773 return dynsymcount;
774 }
775
776 /* This function is called when we want to define a new symbol. It
777 handles the various cases which arise when we find a definition in
778 a dynamic object, or when there is already a definition in a
779 dynamic object. The new symbol is described by NAME, SYM, PSEC,
780 and PVALUE. We set SYM_HASH to the hash table entry. We set
781 OVERRIDE if the old symbol is overriding a new definition. We set
782 TYPE_CHANGE_OK if it is OK for the type to change. We set
783 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
784 change, we mean that we shouldn't warn if the type or size does
785 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
786 object is overridden by a regular object. */
787
788 bfd_boolean
789 _bfd_elf_merge_symbol (bfd *abfd,
790 struct bfd_link_info *info,
791 const char *name,
792 Elf_Internal_Sym *sym,
793 asection **psec,
794 bfd_vma *pvalue,
795 unsigned int *pold_alignment,
796 struct elf_link_hash_entry **sym_hash,
797 bfd_boolean *skip,
798 bfd_boolean *override,
799 bfd_boolean *type_change_ok,
800 bfd_boolean *size_change_ok)
801 {
802 asection *sec, *oldsec;
803 struct elf_link_hash_entry *h;
804 struct elf_link_hash_entry *flip;
805 int bind;
806 bfd *oldbfd;
807 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
808 bfd_boolean newweak, oldweak;
809 const struct elf_backend_data *bed;
810
811 *skip = FALSE;
812 *override = FALSE;
813
814 sec = *psec;
815 bind = ELF_ST_BIND (sym->st_info);
816
817 if (! bfd_is_und_section (sec))
818 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
819 else
820 h = ((struct elf_link_hash_entry *)
821 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
822 if (h == NULL)
823 return FALSE;
824 *sym_hash = h;
825
826 /* This code is for coping with dynamic objects, and is only useful
827 if we are doing an ELF link. */
828 if (info->hash->creator != abfd->xvec)
829 return TRUE;
830
831 /* For merging, we only care about real symbols. */
832
833 while (h->root.type == bfd_link_hash_indirect
834 || h->root.type == bfd_link_hash_warning)
835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
836
837 /* If we just created the symbol, mark it as being an ELF symbol.
838 Other than that, there is nothing to do--there is no merge issue
839 with a newly defined symbol--so we just return. */
840
841 if (h->root.type == bfd_link_hash_new)
842 {
843 h->non_elf = 0;
844 return TRUE;
845 }
846
847 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
848 existing symbol. */
849
850 switch (h->root.type)
851 {
852 default:
853 oldbfd = NULL;
854 oldsec = NULL;
855 break;
856
857 case bfd_link_hash_undefined:
858 case bfd_link_hash_undefweak:
859 oldbfd = h->root.u.undef.abfd;
860 oldsec = NULL;
861 break;
862
863 case bfd_link_hash_defined:
864 case bfd_link_hash_defweak:
865 oldbfd = h->root.u.def.section->owner;
866 oldsec = h->root.u.def.section;
867 break;
868
869 case bfd_link_hash_common:
870 oldbfd = h->root.u.c.p->section->owner;
871 oldsec = h->root.u.c.p->section;
872 break;
873 }
874
875 /* In cases involving weak versioned symbols, we may wind up trying
876 to merge a symbol with itself. Catch that here, to avoid the
877 confusion that results if we try to override a symbol with
878 itself. The additional tests catch cases like
879 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
880 dynamic object, which we do want to handle here. */
881 if (abfd == oldbfd
882 && ((abfd->flags & DYNAMIC) == 0
883 || !h->def_regular))
884 return TRUE;
885
886 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
887 respectively, is from a dynamic object. */
888
889 newdyn = (abfd->flags & DYNAMIC) != 0;
890
891 olddyn = FALSE;
892 if (oldbfd != NULL)
893 olddyn = (oldbfd->flags & DYNAMIC) != 0;
894 else if (oldsec != NULL)
895 {
896 /* This handles the special SHN_MIPS_{TEXT,DATA} section
897 indices used by MIPS ELF. */
898 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
899 }
900
901 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
902 respectively, appear to be a definition rather than reference. */
903
904 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
905
906 olddef = (h->root.type != bfd_link_hash_undefined
907 && h->root.type != bfd_link_hash_undefweak
908 && h->root.type != bfd_link_hash_common);
909
910 /* When we try to create a default indirect symbol from the dynamic
911 definition with the default version, we skip it if its type and
912 the type of existing regular definition mismatch. We only do it
913 if the existing regular definition won't be dynamic. */
914 if (pold_alignment == NULL
915 && !info->shared
916 && !info->export_dynamic
917 && !h->ref_dynamic
918 && newdyn
919 && newdef
920 && !olddyn
921 && (olddef || h->root.type == bfd_link_hash_common)
922 && ELF_ST_TYPE (sym->st_info) != h->type
923 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
924 && h->type != STT_NOTYPE)
925 {
926 *skip = TRUE;
927 return TRUE;
928 }
929
930 /* Check TLS symbol. We don't check undefined symbol introduced by
931 "ld -u". */
932 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
933 && ELF_ST_TYPE (sym->st_info) != h->type
934 && oldbfd != NULL)
935 {
936 bfd *ntbfd, *tbfd;
937 bfd_boolean ntdef, tdef;
938 asection *ntsec, *tsec;
939
940 if (h->type == STT_TLS)
941 {
942 ntbfd = abfd;
943 ntsec = sec;
944 ntdef = newdef;
945 tbfd = oldbfd;
946 tsec = oldsec;
947 tdef = olddef;
948 }
949 else
950 {
951 ntbfd = oldbfd;
952 ntsec = oldsec;
953 ntdef = olddef;
954 tbfd = abfd;
955 tsec = sec;
956 tdef = newdef;
957 }
958
959 if (tdef && ntdef)
960 (*_bfd_error_handler)
961 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
962 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
963 else if (!tdef && !ntdef)
964 (*_bfd_error_handler)
965 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
966 tbfd, ntbfd, h->root.root.string);
967 else if (tdef)
968 (*_bfd_error_handler)
969 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
970 tbfd, tsec, ntbfd, h->root.root.string);
971 else
972 (*_bfd_error_handler)
973 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
974 tbfd, ntbfd, ntsec, h->root.root.string);
975
976 bfd_set_error (bfd_error_bad_value);
977 return FALSE;
978 }
979
980 /* We need to remember if a symbol has a definition in a dynamic
981 object or is weak in all dynamic objects. Internal and hidden
982 visibility will make it unavailable to dynamic objects. */
983 if (newdyn && !h->dynamic_def)
984 {
985 if (!bfd_is_und_section (sec))
986 h->dynamic_def = 1;
987 else
988 {
989 /* Check if this symbol is weak in all dynamic objects. If it
990 is the first time we see it in a dynamic object, we mark
991 if it is weak. Otherwise, we clear it. */
992 if (!h->ref_dynamic)
993 {
994 if (bind == STB_WEAK)
995 h->dynamic_weak = 1;
996 }
997 else if (bind != STB_WEAK)
998 h->dynamic_weak = 0;
999 }
1000 }
1001
1002 /* If the old symbol has non-default visibility, we ignore the new
1003 definition from a dynamic object. */
1004 if (newdyn
1005 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1006 && !bfd_is_und_section (sec))
1007 {
1008 *skip = TRUE;
1009 /* Make sure this symbol is dynamic. */
1010 h->ref_dynamic = 1;
1011 /* A protected symbol has external availability. Make sure it is
1012 recorded as dynamic.
1013
1014 FIXME: Should we check type and size for protected symbol? */
1015 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1016 return bfd_elf_link_record_dynamic_symbol (info, h);
1017 else
1018 return TRUE;
1019 }
1020 else if (!newdyn
1021 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1022 && h->def_dynamic)
1023 {
1024 /* If the new symbol with non-default visibility comes from a
1025 relocatable file and the old definition comes from a dynamic
1026 object, we remove the old definition. */
1027 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1028 h = *sym_hash;
1029
1030 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1031 && bfd_is_und_section (sec))
1032 {
1033 /* If the new symbol is undefined and the old symbol was
1034 also undefined before, we need to make sure
1035 _bfd_generic_link_add_one_symbol doesn't mess
1036 up the linker hash table undefs list. Since the old
1037 definition came from a dynamic object, it is still on the
1038 undefs list. */
1039 h->root.type = bfd_link_hash_undefined;
1040 h->root.u.undef.abfd = abfd;
1041 }
1042 else
1043 {
1044 h->root.type = bfd_link_hash_new;
1045 h->root.u.undef.abfd = NULL;
1046 }
1047
1048 if (h->def_dynamic)
1049 {
1050 h->def_dynamic = 0;
1051 h->ref_dynamic = 1;
1052 h->dynamic_def = 1;
1053 }
1054 /* FIXME: Should we check type and size for protected symbol? */
1055 h->size = 0;
1056 h->type = 0;
1057 return TRUE;
1058 }
1059
1060 /* Differentiate strong and weak symbols. */
1061 newweak = bind == STB_WEAK;
1062 oldweak = (h->root.type == bfd_link_hash_defweak
1063 || h->root.type == bfd_link_hash_undefweak);
1064
1065 /* If a new weak symbol definition comes from a regular file and the
1066 old symbol comes from a dynamic library, we treat the new one as
1067 strong. Similarly, an old weak symbol definition from a regular
1068 file is treated as strong when the new symbol comes from a dynamic
1069 library. Further, an old weak symbol from a dynamic library is
1070 treated as strong if the new symbol is from a dynamic library.
1071 This reflects the way glibc's ld.so works.
1072
1073 Do this before setting *type_change_ok or *size_change_ok so that
1074 we warn properly when dynamic library symbols are overridden. */
1075
1076 if (newdef && !newdyn && olddyn)
1077 newweak = FALSE;
1078 if (olddef && newdyn)
1079 oldweak = FALSE;
1080
1081 /* It's OK to change the type if either the existing symbol or the
1082 new symbol is weak. A type change is also OK if the old symbol
1083 is undefined and the new symbol is defined. */
1084
1085 if (oldweak
1086 || newweak
1087 || (newdef
1088 && h->root.type == bfd_link_hash_undefined))
1089 *type_change_ok = TRUE;
1090
1091 /* It's OK to change the size if either the existing symbol or the
1092 new symbol is weak, or if the old symbol is undefined. */
1093
1094 if (*type_change_ok
1095 || h->root.type == bfd_link_hash_undefined)
1096 *size_change_ok = TRUE;
1097
1098 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1099 symbol, respectively, appears to be a common symbol in a dynamic
1100 object. If a symbol appears in an uninitialized section, and is
1101 not weak, and is not a function, then it may be a common symbol
1102 which was resolved when the dynamic object was created. We want
1103 to treat such symbols specially, because they raise special
1104 considerations when setting the symbol size: if the symbol
1105 appears as a common symbol in a regular object, and the size in
1106 the regular object is larger, we must make sure that we use the
1107 larger size. This problematic case can always be avoided in C,
1108 but it must be handled correctly when using Fortran shared
1109 libraries.
1110
1111 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1112 likewise for OLDDYNCOMMON and OLDDEF.
1113
1114 Note that this test is just a heuristic, and that it is quite
1115 possible to have an uninitialized symbol in a shared object which
1116 is really a definition, rather than a common symbol. This could
1117 lead to some minor confusion when the symbol really is a common
1118 symbol in some regular object. However, I think it will be
1119 harmless. */
1120
1121 if (newdyn
1122 && newdef
1123 && !newweak
1124 && (sec->flags & SEC_ALLOC) != 0
1125 && (sec->flags & SEC_LOAD) == 0
1126 && sym->st_size > 0
1127 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1128 newdyncommon = TRUE;
1129 else
1130 newdyncommon = FALSE;
1131
1132 if (olddyn
1133 && olddef
1134 && h->root.type == bfd_link_hash_defined
1135 && h->def_dynamic
1136 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1137 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1138 && h->size > 0
1139 && h->type != STT_FUNC)
1140 olddyncommon = TRUE;
1141 else
1142 olddyncommon = FALSE;
1143
1144 /* We now know everything about the old and new symbols. We ask the
1145 backend to check if we can merge them. */
1146 bed = get_elf_backend_data (abfd);
1147 if (bed->merge_symbol
1148 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1149 pold_alignment, skip, override,
1150 type_change_ok, size_change_ok,
1151 &newdyn, &newdef, &newdyncommon, &newweak,
1152 abfd, &sec,
1153 &olddyn, &olddef, &olddyncommon, &oldweak,
1154 oldbfd, &oldsec))
1155 return FALSE;
1156
1157 /* If both the old and the new symbols look like common symbols in a
1158 dynamic object, set the size of the symbol to the larger of the
1159 two. */
1160
1161 if (olddyncommon
1162 && newdyncommon
1163 && sym->st_size != h->size)
1164 {
1165 /* Since we think we have two common symbols, issue a multiple
1166 common warning if desired. Note that we only warn if the
1167 size is different. If the size is the same, we simply let
1168 the old symbol override the new one as normally happens with
1169 symbols defined in dynamic objects. */
1170
1171 if (! ((*info->callbacks->multiple_common)
1172 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1173 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1174 return FALSE;
1175
1176 if (sym->st_size > h->size)
1177 h->size = sym->st_size;
1178
1179 *size_change_ok = TRUE;
1180 }
1181
1182 /* If we are looking at a dynamic object, and we have found a
1183 definition, we need to see if the symbol was already defined by
1184 some other object. If so, we want to use the existing
1185 definition, and we do not want to report a multiple symbol
1186 definition error; we do this by clobbering *PSEC to be
1187 bfd_und_section_ptr.
1188
1189 We treat a common symbol as a definition if the symbol in the
1190 shared library is a function, since common symbols always
1191 represent variables; this can cause confusion in principle, but
1192 any such confusion would seem to indicate an erroneous program or
1193 shared library. We also permit a common symbol in a regular
1194 object to override a weak symbol in a shared object. */
1195
1196 if (newdyn
1197 && newdef
1198 && (olddef
1199 || (h->root.type == bfd_link_hash_common
1200 && (newweak
1201 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1202 {
1203 *override = TRUE;
1204 newdef = FALSE;
1205 newdyncommon = FALSE;
1206
1207 *psec = sec = bfd_und_section_ptr;
1208 *size_change_ok = TRUE;
1209
1210 /* If we get here when the old symbol is a common symbol, then
1211 we are explicitly letting it override a weak symbol or
1212 function in a dynamic object, and we don't want to warn about
1213 a type change. If the old symbol is a defined symbol, a type
1214 change warning may still be appropriate. */
1215
1216 if (h->root.type == bfd_link_hash_common)
1217 *type_change_ok = TRUE;
1218 }
1219
1220 /* Handle the special case of an old common symbol merging with a
1221 new symbol which looks like a common symbol in a shared object.
1222 We change *PSEC and *PVALUE to make the new symbol look like a
1223 common symbol, and let _bfd_generic_link_add_one_symbol do the
1224 right thing. */
1225
1226 if (newdyncommon
1227 && h->root.type == bfd_link_hash_common)
1228 {
1229 *override = TRUE;
1230 newdef = FALSE;
1231 newdyncommon = FALSE;
1232 *pvalue = sym->st_size;
1233 *psec = sec = bed->common_section (oldsec);
1234 *size_change_ok = TRUE;
1235 }
1236
1237 /* Skip weak definitions of symbols that are already defined. */
1238 if (newdef && olddef && newweak)
1239 *skip = TRUE;
1240
1241 /* If the old symbol is from a dynamic object, and the new symbol is
1242 a definition which is not from a dynamic object, then the new
1243 symbol overrides the old symbol. Symbols from regular files
1244 always take precedence over symbols from dynamic objects, even if
1245 they are defined after the dynamic object in the link.
1246
1247 As above, we again permit a common symbol in a regular object to
1248 override a definition in a shared object if the shared object
1249 symbol is a function or is weak. */
1250
1251 flip = NULL;
1252 if (!newdyn
1253 && (newdef
1254 || (bfd_is_com_section (sec)
1255 && (oldweak
1256 || h->type == STT_FUNC)))
1257 && olddyn
1258 && olddef
1259 && h->def_dynamic)
1260 {
1261 /* Change the hash table entry to undefined, and let
1262 _bfd_generic_link_add_one_symbol do the right thing with the
1263 new definition. */
1264
1265 h->root.type = bfd_link_hash_undefined;
1266 h->root.u.undef.abfd = h->root.u.def.section->owner;
1267 *size_change_ok = TRUE;
1268
1269 olddef = FALSE;
1270 olddyncommon = FALSE;
1271
1272 /* We again permit a type change when a common symbol may be
1273 overriding a function. */
1274
1275 if (bfd_is_com_section (sec))
1276 *type_change_ok = TRUE;
1277
1278 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1279 flip = *sym_hash;
1280 else
1281 /* This union may have been set to be non-NULL when this symbol
1282 was seen in a dynamic object. We must force the union to be
1283 NULL, so that it is correct for a regular symbol. */
1284 h->verinfo.vertree = NULL;
1285 }
1286
1287 /* Handle the special case of a new common symbol merging with an
1288 old symbol that looks like it might be a common symbol defined in
1289 a shared object. Note that we have already handled the case in
1290 which a new common symbol should simply override the definition
1291 in the shared library. */
1292
1293 if (! newdyn
1294 && bfd_is_com_section (sec)
1295 && olddyncommon)
1296 {
1297 /* It would be best if we could set the hash table entry to a
1298 common symbol, but we don't know what to use for the section
1299 or the alignment. */
1300 if (! ((*info->callbacks->multiple_common)
1301 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1302 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1303 return FALSE;
1304
1305 /* If the presumed common symbol in the dynamic object is
1306 larger, pretend that the new symbol has its size. */
1307
1308 if (h->size > *pvalue)
1309 *pvalue = h->size;
1310
1311 /* We need to remember the alignment required by the symbol
1312 in the dynamic object. */
1313 BFD_ASSERT (pold_alignment);
1314 *pold_alignment = h->root.u.def.section->alignment_power;
1315
1316 olddef = FALSE;
1317 olddyncommon = FALSE;
1318
1319 h->root.type = bfd_link_hash_undefined;
1320 h->root.u.undef.abfd = h->root.u.def.section->owner;
1321
1322 *size_change_ok = TRUE;
1323 *type_change_ok = TRUE;
1324
1325 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1326 flip = *sym_hash;
1327 else
1328 h->verinfo.vertree = NULL;
1329 }
1330
1331 if (flip != NULL)
1332 {
1333 /* Handle the case where we had a versioned symbol in a dynamic
1334 library and now find a definition in a normal object. In this
1335 case, we make the versioned symbol point to the normal one. */
1336 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1337 flip->root.type = h->root.type;
1338 h->root.type = bfd_link_hash_indirect;
1339 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1340 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1341 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1342 if (h->def_dynamic)
1343 {
1344 h->def_dynamic = 0;
1345 flip->ref_dynamic = 1;
1346 }
1347 }
1348
1349 return TRUE;
1350 }
1351
1352 /* This function is called to create an indirect symbol from the
1353 default for the symbol with the default version if needed. The
1354 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1355 set DYNSYM if the new indirect symbol is dynamic. */
1356
1357 bfd_boolean
1358 _bfd_elf_add_default_symbol (bfd *abfd,
1359 struct bfd_link_info *info,
1360 struct elf_link_hash_entry *h,
1361 const char *name,
1362 Elf_Internal_Sym *sym,
1363 asection **psec,
1364 bfd_vma *value,
1365 bfd_boolean *dynsym,
1366 bfd_boolean override)
1367 {
1368 bfd_boolean type_change_ok;
1369 bfd_boolean size_change_ok;
1370 bfd_boolean skip;
1371 char *shortname;
1372 struct elf_link_hash_entry *hi;
1373 struct bfd_link_hash_entry *bh;
1374 const struct elf_backend_data *bed;
1375 bfd_boolean collect;
1376 bfd_boolean dynamic;
1377 char *p;
1378 size_t len, shortlen;
1379 asection *sec;
1380
1381 /* If this symbol has a version, and it is the default version, we
1382 create an indirect symbol from the default name to the fully
1383 decorated name. This will cause external references which do not
1384 specify a version to be bound to this version of the symbol. */
1385 p = strchr (name, ELF_VER_CHR);
1386 if (p == NULL || p[1] != ELF_VER_CHR)
1387 return TRUE;
1388
1389 if (override)
1390 {
1391 /* We are overridden by an old definition. We need to check if we
1392 need to create the indirect symbol from the default name. */
1393 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1394 FALSE, FALSE);
1395 BFD_ASSERT (hi != NULL);
1396 if (hi == h)
1397 return TRUE;
1398 while (hi->root.type == bfd_link_hash_indirect
1399 || hi->root.type == bfd_link_hash_warning)
1400 {
1401 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1402 if (hi == h)
1403 return TRUE;
1404 }
1405 }
1406
1407 bed = get_elf_backend_data (abfd);
1408 collect = bed->collect;
1409 dynamic = (abfd->flags & DYNAMIC) != 0;
1410
1411 shortlen = p - name;
1412 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1413 if (shortname == NULL)
1414 return FALSE;
1415 memcpy (shortname, name, shortlen);
1416 shortname[shortlen] = '\0';
1417
1418 /* We are going to create a new symbol. Merge it with any existing
1419 symbol with this name. For the purposes of the merge, act as
1420 though we were defining the symbol we just defined, although we
1421 actually going to define an indirect symbol. */
1422 type_change_ok = FALSE;
1423 size_change_ok = FALSE;
1424 sec = *psec;
1425 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1426 NULL, &hi, &skip, &override,
1427 &type_change_ok, &size_change_ok))
1428 return FALSE;
1429
1430 if (skip)
1431 goto nondefault;
1432
1433 if (! override)
1434 {
1435 bh = &hi->root;
1436 if (! (_bfd_generic_link_add_one_symbol
1437 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1438 0, name, FALSE, collect, &bh)))
1439 return FALSE;
1440 hi = (struct elf_link_hash_entry *) bh;
1441 }
1442 else
1443 {
1444 /* In this case the symbol named SHORTNAME is overriding the
1445 indirect symbol we want to add. We were planning on making
1446 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1447 is the name without a version. NAME is the fully versioned
1448 name, and it is the default version.
1449
1450 Overriding means that we already saw a definition for the
1451 symbol SHORTNAME in a regular object, and it is overriding
1452 the symbol defined in the dynamic object.
1453
1454 When this happens, we actually want to change NAME, the
1455 symbol we just added, to refer to SHORTNAME. This will cause
1456 references to NAME in the shared object to become references
1457 to SHORTNAME in the regular object. This is what we expect
1458 when we override a function in a shared object: that the
1459 references in the shared object will be mapped to the
1460 definition in the regular object. */
1461
1462 while (hi->root.type == bfd_link_hash_indirect
1463 || hi->root.type == bfd_link_hash_warning)
1464 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1465
1466 h->root.type = bfd_link_hash_indirect;
1467 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1468 if (h->def_dynamic)
1469 {
1470 h->def_dynamic = 0;
1471 hi->ref_dynamic = 1;
1472 if (hi->ref_regular
1473 || hi->def_regular)
1474 {
1475 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1476 return FALSE;
1477 }
1478 }
1479
1480 /* Now set HI to H, so that the following code will set the
1481 other fields correctly. */
1482 hi = h;
1483 }
1484
1485 /* If there is a duplicate definition somewhere, then HI may not
1486 point to an indirect symbol. We will have reported an error to
1487 the user in that case. */
1488
1489 if (hi->root.type == bfd_link_hash_indirect)
1490 {
1491 struct elf_link_hash_entry *ht;
1492
1493 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1494 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1495
1496 /* See if the new flags lead us to realize that the symbol must
1497 be dynamic. */
1498 if (! *dynsym)
1499 {
1500 if (! dynamic)
1501 {
1502 if (info->shared
1503 || hi->ref_dynamic)
1504 *dynsym = TRUE;
1505 }
1506 else
1507 {
1508 if (hi->ref_regular)
1509 *dynsym = TRUE;
1510 }
1511 }
1512 }
1513
1514 /* We also need to define an indirection from the nondefault version
1515 of the symbol. */
1516
1517 nondefault:
1518 len = strlen (name);
1519 shortname = bfd_hash_allocate (&info->hash->table, len);
1520 if (shortname == NULL)
1521 return FALSE;
1522 memcpy (shortname, name, shortlen);
1523 memcpy (shortname + shortlen, p + 1, len - shortlen);
1524
1525 /* Once again, merge with any existing symbol. */
1526 type_change_ok = FALSE;
1527 size_change_ok = FALSE;
1528 sec = *psec;
1529 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1530 NULL, &hi, &skip, &override,
1531 &type_change_ok, &size_change_ok))
1532 return FALSE;
1533
1534 if (skip)
1535 return TRUE;
1536
1537 if (override)
1538 {
1539 /* Here SHORTNAME is a versioned name, so we don't expect to see
1540 the type of override we do in the case above unless it is
1541 overridden by a versioned definition. */
1542 if (hi->root.type != bfd_link_hash_defined
1543 && hi->root.type != bfd_link_hash_defweak)
1544 (*_bfd_error_handler)
1545 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1546 abfd, shortname);
1547 }
1548 else
1549 {
1550 bh = &hi->root;
1551 if (! (_bfd_generic_link_add_one_symbol
1552 (info, abfd, shortname, BSF_INDIRECT,
1553 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1554 return FALSE;
1555 hi = (struct elf_link_hash_entry *) bh;
1556
1557 /* If there is a duplicate definition somewhere, then HI may not
1558 point to an indirect symbol. We will have reported an error
1559 to the user in that case. */
1560
1561 if (hi->root.type == bfd_link_hash_indirect)
1562 {
1563 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1564
1565 /* See if the new flags lead us to realize that the symbol
1566 must be dynamic. */
1567 if (! *dynsym)
1568 {
1569 if (! dynamic)
1570 {
1571 if (info->shared
1572 || hi->ref_dynamic)
1573 *dynsym = TRUE;
1574 }
1575 else
1576 {
1577 if (hi->ref_regular)
1578 *dynsym = TRUE;
1579 }
1580 }
1581 }
1582 }
1583
1584 return TRUE;
1585 }
1586 \f
1587 /* This routine is used to export all defined symbols into the dynamic
1588 symbol table. It is called via elf_link_hash_traverse. */
1589
1590 bfd_boolean
1591 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1592 {
1593 struct elf_info_failed *eif = data;
1594
1595 /* Ignore indirect symbols. These are added by the versioning code. */
1596 if (h->root.type == bfd_link_hash_indirect)
1597 return TRUE;
1598
1599 if (h->root.type == bfd_link_hash_warning)
1600 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1601
1602 if (h->dynindx == -1
1603 && (h->def_regular
1604 || h->ref_regular))
1605 {
1606 struct bfd_elf_version_tree *t;
1607 struct bfd_elf_version_expr *d;
1608
1609 for (t = eif->verdefs; t != NULL; t = t->next)
1610 {
1611 if (t->globals.list != NULL)
1612 {
1613 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1614 if (d != NULL)
1615 goto doit;
1616 }
1617
1618 if (t->locals.list != NULL)
1619 {
1620 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1621 if (d != NULL)
1622 return TRUE;
1623 }
1624 }
1625
1626 if (!eif->verdefs)
1627 {
1628 doit:
1629 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1630 {
1631 eif->failed = TRUE;
1632 return FALSE;
1633 }
1634 }
1635 }
1636
1637 return TRUE;
1638 }
1639 \f
1640 /* Look through the symbols which are defined in other shared
1641 libraries and referenced here. Update the list of version
1642 dependencies. This will be put into the .gnu.version_r section.
1643 This function is called via elf_link_hash_traverse. */
1644
1645 bfd_boolean
1646 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1647 void *data)
1648 {
1649 struct elf_find_verdep_info *rinfo = data;
1650 Elf_Internal_Verneed *t;
1651 Elf_Internal_Vernaux *a;
1652 bfd_size_type amt;
1653
1654 if (h->root.type == bfd_link_hash_warning)
1655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1656
1657 /* We only care about symbols defined in shared objects with version
1658 information. */
1659 if (!h->def_dynamic
1660 || h->def_regular
1661 || h->dynindx == -1
1662 || h->verinfo.verdef == NULL)
1663 return TRUE;
1664
1665 /* See if we already know about this version. */
1666 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1667 {
1668 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1669 continue;
1670
1671 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1672 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1673 return TRUE;
1674
1675 break;
1676 }
1677
1678 /* This is a new version. Add it to tree we are building. */
1679
1680 if (t == NULL)
1681 {
1682 amt = sizeof *t;
1683 t = bfd_zalloc (rinfo->output_bfd, amt);
1684 if (t == NULL)
1685 {
1686 rinfo->failed = TRUE;
1687 return FALSE;
1688 }
1689
1690 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1691 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1692 elf_tdata (rinfo->output_bfd)->verref = t;
1693 }
1694
1695 amt = sizeof *a;
1696 a = bfd_zalloc (rinfo->output_bfd, amt);
1697
1698 /* Note that we are copying a string pointer here, and testing it
1699 above. If bfd_elf_string_from_elf_section is ever changed to
1700 discard the string data when low in memory, this will have to be
1701 fixed. */
1702 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1703
1704 a->vna_flags = h->verinfo.verdef->vd_flags;
1705 a->vna_nextptr = t->vn_auxptr;
1706
1707 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1708 ++rinfo->vers;
1709
1710 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1711
1712 t->vn_auxptr = a;
1713
1714 return TRUE;
1715 }
1716
1717 /* Figure out appropriate versions for all the symbols. We may not
1718 have the version number script until we have read all of the input
1719 files, so until that point we don't know which symbols should be
1720 local. This function is called via elf_link_hash_traverse. */
1721
1722 bfd_boolean
1723 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1724 {
1725 struct elf_assign_sym_version_info *sinfo;
1726 struct bfd_link_info *info;
1727 const struct elf_backend_data *bed;
1728 struct elf_info_failed eif;
1729 char *p;
1730 bfd_size_type amt;
1731
1732 sinfo = data;
1733 info = sinfo->info;
1734
1735 if (h->root.type == bfd_link_hash_warning)
1736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1737
1738 /* Fix the symbol flags. */
1739 eif.failed = FALSE;
1740 eif.info = info;
1741 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1742 {
1743 if (eif.failed)
1744 sinfo->failed = TRUE;
1745 return FALSE;
1746 }
1747
1748 /* We only need version numbers for symbols defined in regular
1749 objects. */
1750 if (!h->def_regular)
1751 return TRUE;
1752
1753 bed = get_elf_backend_data (sinfo->output_bfd);
1754 p = strchr (h->root.root.string, ELF_VER_CHR);
1755 if (p != NULL && h->verinfo.vertree == NULL)
1756 {
1757 struct bfd_elf_version_tree *t;
1758 bfd_boolean hidden;
1759
1760 hidden = TRUE;
1761
1762 /* There are two consecutive ELF_VER_CHR characters if this is
1763 not a hidden symbol. */
1764 ++p;
1765 if (*p == ELF_VER_CHR)
1766 {
1767 hidden = FALSE;
1768 ++p;
1769 }
1770
1771 /* If there is no version string, we can just return out. */
1772 if (*p == '\0')
1773 {
1774 if (hidden)
1775 h->hidden = 1;
1776 return TRUE;
1777 }
1778
1779 /* Look for the version. If we find it, it is no longer weak. */
1780 for (t = sinfo->verdefs; t != NULL; t = t->next)
1781 {
1782 if (strcmp (t->name, p) == 0)
1783 {
1784 size_t len;
1785 char *alc;
1786 struct bfd_elf_version_expr *d;
1787
1788 len = p - h->root.root.string;
1789 alc = bfd_malloc (len);
1790 if (alc == NULL)
1791 return FALSE;
1792 memcpy (alc, h->root.root.string, len - 1);
1793 alc[len - 1] = '\0';
1794 if (alc[len - 2] == ELF_VER_CHR)
1795 alc[len - 2] = '\0';
1796
1797 h->verinfo.vertree = t;
1798 t->used = TRUE;
1799 d = NULL;
1800
1801 if (t->globals.list != NULL)
1802 d = (*t->match) (&t->globals, NULL, alc);
1803
1804 /* See if there is anything to force this symbol to
1805 local scope. */
1806 if (d == NULL && t->locals.list != NULL)
1807 {
1808 d = (*t->match) (&t->locals, NULL, alc);
1809 if (d != NULL
1810 && h->dynindx != -1
1811 && ! info->export_dynamic)
1812 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1813 }
1814
1815 free (alc);
1816 break;
1817 }
1818 }
1819
1820 /* If we are building an application, we need to create a
1821 version node for this version. */
1822 if (t == NULL && info->executable)
1823 {
1824 struct bfd_elf_version_tree **pp;
1825 int version_index;
1826
1827 /* If we aren't going to export this symbol, we don't need
1828 to worry about it. */
1829 if (h->dynindx == -1)
1830 return TRUE;
1831
1832 amt = sizeof *t;
1833 t = bfd_zalloc (sinfo->output_bfd, amt);
1834 if (t == NULL)
1835 {
1836 sinfo->failed = TRUE;
1837 return FALSE;
1838 }
1839
1840 t->name = p;
1841 t->name_indx = (unsigned int) -1;
1842 t->used = TRUE;
1843
1844 version_index = 1;
1845 /* Don't count anonymous version tag. */
1846 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1847 version_index = 0;
1848 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1849 ++version_index;
1850 t->vernum = version_index;
1851
1852 *pp = t;
1853
1854 h->verinfo.vertree = t;
1855 }
1856 else if (t == NULL)
1857 {
1858 /* We could not find the version for a symbol when
1859 generating a shared archive. Return an error. */
1860 (*_bfd_error_handler)
1861 (_("%B: undefined versioned symbol name %s"),
1862 sinfo->output_bfd, h->root.root.string);
1863 bfd_set_error (bfd_error_bad_value);
1864 sinfo->failed = TRUE;
1865 return FALSE;
1866 }
1867
1868 if (hidden)
1869 h->hidden = 1;
1870 }
1871
1872 /* If we don't have a version for this symbol, see if we can find
1873 something. */
1874 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1875 {
1876 struct bfd_elf_version_tree *t;
1877 struct bfd_elf_version_tree *local_ver;
1878 struct bfd_elf_version_expr *d;
1879
1880 /* See if can find what version this symbol is in. If the
1881 symbol is supposed to be local, then don't actually register
1882 it. */
1883 local_ver = NULL;
1884 for (t = sinfo->verdefs; t != NULL; t = t->next)
1885 {
1886 if (t->globals.list != NULL)
1887 {
1888 bfd_boolean matched;
1889
1890 matched = FALSE;
1891 d = NULL;
1892 while ((d = (*t->match) (&t->globals, d,
1893 h->root.root.string)) != NULL)
1894 if (d->symver)
1895 matched = TRUE;
1896 else
1897 {
1898 /* There is a version without definition. Make
1899 the symbol the default definition for this
1900 version. */
1901 h->verinfo.vertree = t;
1902 local_ver = NULL;
1903 d->script = 1;
1904 break;
1905 }
1906 if (d != NULL)
1907 break;
1908 else if (matched)
1909 /* There is no undefined version for this symbol. Hide the
1910 default one. */
1911 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1912 }
1913
1914 if (t->locals.list != NULL)
1915 {
1916 d = NULL;
1917 while ((d = (*t->match) (&t->locals, d,
1918 h->root.root.string)) != NULL)
1919 {
1920 local_ver = t;
1921 /* If the match is "*", keep looking for a more
1922 explicit, perhaps even global, match.
1923 XXX: Shouldn't this be !d->wildcard instead? */
1924 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1925 break;
1926 }
1927
1928 if (d != NULL)
1929 break;
1930 }
1931 }
1932
1933 if (local_ver != NULL)
1934 {
1935 h->verinfo.vertree = local_ver;
1936 if (h->dynindx != -1
1937 && ! info->export_dynamic)
1938 {
1939 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1940 }
1941 }
1942 }
1943
1944 return TRUE;
1945 }
1946 \f
1947 /* Read and swap the relocs from the section indicated by SHDR. This
1948 may be either a REL or a RELA section. The relocations are
1949 translated into RELA relocations and stored in INTERNAL_RELOCS,
1950 which should have already been allocated to contain enough space.
1951 The EXTERNAL_RELOCS are a buffer where the external form of the
1952 relocations should be stored.
1953
1954 Returns FALSE if something goes wrong. */
1955
1956 static bfd_boolean
1957 elf_link_read_relocs_from_section (bfd *abfd,
1958 asection *sec,
1959 Elf_Internal_Shdr *shdr,
1960 void *external_relocs,
1961 Elf_Internal_Rela *internal_relocs)
1962 {
1963 const struct elf_backend_data *bed;
1964 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1965 const bfd_byte *erela;
1966 const bfd_byte *erelaend;
1967 Elf_Internal_Rela *irela;
1968 Elf_Internal_Shdr *symtab_hdr;
1969 size_t nsyms;
1970
1971 /* Position ourselves at the start of the section. */
1972 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1973 return FALSE;
1974
1975 /* Read the relocations. */
1976 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1977 return FALSE;
1978
1979 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1980 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1981
1982 bed = get_elf_backend_data (abfd);
1983
1984 /* Convert the external relocations to the internal format. */
1985 if (shdr->sh_entsize == bed->s->sizeof_rel)
1986 swap_in = bed->s->swap_reloc_in;
1987 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1988 swap_in = bed->s->swap_reloca_in;
1989 else
1990 {
1991 bfd_set_error (bfd_error_wrong_format);
1992 return FALSE;
1993 }
1994
1995 erela = external_relocs;
1996 erelaend = erela + shdr->sh_size;
1997 irela = internal_relocs;
1998 while (erela < erelaend)
1999 {
2000 bfd_vma r_symndx;
2001
2002 (*swap_in) (abfd, erela, irela);
2003 r_symndx = ELF32_R_SYM (irela->r_info);
2004 if (bed->s->arch_size == 64)
2005 r_symndx >>= 24;
2006 if ((size_t) r_symndx >= nsyms)
2007 {
2008 (*_bfd_error_handler)
2009 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2010 " for offset 0x%lx in section `%A'"),
2011 abfd, sec,
2012 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2013 bfd_set_error (bfd_error_bad_value);
2014 return FALSE;
2015 }
2016 irela += bed->s->int_rels_per_ext_rel;
2017 erela += shdr->sh_entsize;
2018 }
2019
2020 return TRUE;
2021 }
2022
2023 /* Read and swap the relocs for a section O. They may have been
2024 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2025 not NULL, they are used as buffers to read into. They are known to
2026 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2027 the return value is allocated using either malloc or bfd_alloc,
2028 according to the KEEP_MEMORY argument. If O has two relocation
2029 sections (both REL and RELA relocations), then the REL_HDR
2030 relocations will appear first in INTERNAL_RELOCS, followed by the
2031 REL_HDR2 relocations. */
2032
2033 Elf_Internal_Rela *
2034 _bfd_elf_link_read_relocs (bfd *abfd,
2035 asection *o,
2036 void *external_relocs,
2037 Elf_Internal_Rela *internal_relocs,
2038 bfd_boolean keep_memory)
2039 {
2040 Elf_Internal_Shdr *rel_hdr;
2041 void *alloc1 = NULL;
2042 Elf_Internal_Rela *alloc2 = NULL;
2043 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2044
2045 if (elf_section_data (o)->relocs != NULL)
2046 return elf_section_data (o)->relocs;
2047
2048 if (o->reloc_count == 0)
2049 return NULL;
2050
2051 rel_hdr = &elf_section_data (o)->rel_hdr;
2052
2053 if (internal_relocs == NULL)
2054 {
2055 bfd_size_type size;
2056
2057 size = o->reloc_count;
2058 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2059 if (keep_memory)
2060 internal_relocs = bfd_alloc (abfd, size);
2061 else
2062 internal_relocs = alloc2 = bfd_malloc (size);
2063 if (internal_relocs == NULL)
2064 goto error_return;
2065 }
2066
2067 if (external_relocs == NULL)
2068 {
2069 bfd_size_type size = rel_hdr->sh_size;
2070
2071 if (elf_section_data (o)->rel_hdr2)
2072 size += elf_section_data (o)->rel_hdr2->sh_size;
2073 alloc1 = bfd_malloc (size);
2074 if (alloc1 == NULL)
2075 goto error_return;
2076 external_relocs = alloc1;
2077 }
2078
2079 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2080 external_relocs,
2081 internal_relocs))
2082 goto error_return;
2083 if (elf_section_data (o)->rel_hdr2
2084 && (!elf_link_read_relocs_from_section
2085 (abfd, o,
2086 elf_section_data (o)->rel_hdr2,
2087 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2088 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2089 * bed->s->int_rels_per_ext_rel))))
2090 goto error_return;
2091
2092 /* Cache the results for next time, if we can. */
2093 if (keep_memory)
2094 elf_section_data (o)->relocs = internal_relocs;
2095
2096 if (alloc1 != NULL)
2097 free (alloc1);
2098
2099 /* Don't free alloc2, since if it was allocated we are passing it
2100 back (under the name of internal_relocs). */
2101
2102 return internal_relocs;
2103
2104 error_return:
2105 if (alloc1 != NULL)
2106 free (alloc1);
2107 if (alloc2 != NULL)
2108 free (alloc2);
2109 return NULL;
2110 }
2111
2112 /* Compute the size of, and allocate space for, REL_HDR which is the
2113 section header for a section containing relocations for O. */
2114
2115 bfd_boolean
2116 _bfd_elf_link_size_reloc_section (bfd *abfd,
2117 Elf_Internal_Shdr *rel_hdr,
2118 asection *o)
2119 {
2120 bfd_size_type reloc_count;
2121 bfd_size_type num_rel_hashes;
2122
2123 /* Figure out how many relocations there will be. */
2124 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2125 reloc_count = elf_section_data (o)->rel_count;
2126 else
2127 reloc_count = elf_section_data (o)->rel_count2;
2128
2129 num_rel_hashes = o->reloc_count;
2130 if (num_rel_hashes < reloc_count)
2131 num_rel_hashes = reloc_count;
2132
2133 /* That allows us to calculate the size of the section. */
2134 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2135
2136 /* The contents field must last into write_object_contents, so we
2137 allocate it with bfd_alloc rather than malloc. Also since we
2138 cannot be sure that the contents will actually be filled in,
2139 we zero the allocated space. */
2140 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2141 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2142 return FALSE;
2143
2144 /* We only allocate one set of hash entries, so we only do it the
2145 first time we are called. */
2146 if (elf_section_data (o)->rel_hashes == NULL
2147 && num_rel_hashes)
2148 {
2149 struct elf_link_hash_entry **p;
2150
2151 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2152 if (p == NULL)
2153 return FALSE;
2154
2155 elf_section_data (o)->rel_hashes = p;
2156 }
2157
2158 return TRUE;
2159 }
2160
2161 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2162 originated from the section given by INPUT_REL_HDR) to the
2163 OUTPUT_BFD. */
2164
2165 bfd_boolean
2166 _bfd_elf_link_output_relocs (bfd *output_bfd,
2167 asection *input_section,
2168 Elf_Internal_Shdr *input_rel_hdr,
2169 Elf_Internal_Rela *internal_relocs,
2170 struct elf_link_hash_entry **rel_hash
2171 ATTRIBUTE_UNUSED)
2172 {
2173 Elf_Internal_Rela *irela;
2174 Elf_Internal_Rela *irelaend;
2175 bfd_byte *erel;
2176 Elf_Internal_Shdr *output_rel_hdr;
2177 asection *output_section;
2178 unsigned int *rel_countp = NULL;
2179 const struct elf_backend_data *bed;
2180 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2181
2182 output_section = input_section->output_section;
2183 output_rel_hdr = NULL;
2184
2185 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2186 == input_rel_hdr->sh_entsize)
2187 {
2188 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2189 rel_countp = &elf_section_data (output_section)->rel_count;
2190 }
2191 else if (elf_section_data (output_section)->rel_hdr2
2192 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2193 == input_rel_hdr->sh_entsize))
2194 {
2195 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2196 rel_countp = &elf_section_data (output_section)->rel_count2;
2197 }
2198 else
2199 {
2200 (*_bfd_error_handler)
2201 (_("%B: relocation size mismatch in %B section %A"),
2202 output_bfd, input_section->owner, input_section);
2203 bfd_set_error (bfd_error_wrong_object_format);
2204 return FALSE;
2205 }
2206
2207 bed = get_elf_backend_data (output_bfd);
2208 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2209 swap_out = bed->s->swap_reloc_out;
2210 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2211 swap_out = bed->s->swap_reloca_out;
2212 else
2213 abort ();
2214
2215 erel = output_rel_hdr->contents;
2216 erel += *rel_countp * input_rel_hdr->sh_entsize;
2217 irela = internal_relocs;
2218 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2219 * bed->s->int_rels_per_ext_rel);
2220 while (irela < irelaend)
2221 {
2222 (*swap_out) (output_bfd, irela, erel);
2223 irela += bed->s->int_rels_per_ext_rel;
2224 erel += input_rel_hdr->sh_entsize;
2225 }
2226
2227 /* Bump the counter, so that we know where to add the next set of
2228 relocations. */
2229 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2230
2231 return TRUE;
2232 }
2233 \f
2234 /* Make weak undefined symbols in PIE dynamic. */
2235
2236 bfd_boolean
2237 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2238 struct elf_link_hash_entry *h)
2239 {
2240 if (info->pie
2241 && h->dynindx == -1
2242 && h->root.type == bfd_link_hash_undefweak)
2243 return bfd_elf_link_record_dynamic_symbol (info, h);
2244
2245 return TRUE;
2246 }
2247
2248 /* Fix up the flags for a symbol. This handles various cases which
2249 can only be fixed after all the input files are seen. This is
2250 currently called by both adjust_dynamic_symbol and
2251 assign_sym_version, which is unnecessary but perhaps more robust in
2252 the face of future changes. */
2253
2254 bfd_boolean
2255 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2256 struct elf_info_failed *eif)
2257 {
2258 const struct elf_backend_data *bed = NULL;
2259
2260 /* If this symbol was mentioned in a non-ELF file, try to set
2261 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2262 permit a non-ELF file to correctly refer to a symbol defined in
2263 an ELF dynamic object. */
2264 if (h->non_elf)
2265 {
2266 while (h->root.type == bfd_link_hash_indirect)
2267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2268
2269 if (h->root.type != bfd_link_hash_defined
2270 && h->root.type != bfd_link_hash_defweak)
2271 {
2272 h->ref_regular = 1;
2273 h->ref_regular_nonweak = 1;
2274 }
2275 else
2276 {
2277 if (h->root.u.def.section->owner != NULL
2278 && (bfd_get_flavour (h->root.u.def.section->owner)
2279 == bfd_target_elf_flavour))
2280 {
2281 h->ref_regular = 1;
2282 h->ref_regular_nonweak = 1;
2283 }
2284 else
2285 h->def_regular = 1;
2286 }
2287
2288 if (h->dynindx == -1
2289 && (h->def_dynamic
2290 || h->ref_dynamic))
2291 {
2292 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2293 {
2294 eif->failed = TRUE;
2295 return FALSE;
2296 }
2297 }
2298 }
2299 else
2300 {
2301 /* Unfortunately, NON_ELF is only correct if the symbol
2302 was first seen in a non-ELF file. Fortunately, if the symbol
2303 was first seen in an ELF file, we're probably OK unless the
2304 symbol was defined in a non-ELF file. Catch that case here.
2305 FIXME: We're still in trouble if the symbol was first seen in
2306 a dynamic object, and then later in a non-ELF regular object. */
2307 if ((h->root.type == bfd_link_hash_defined
2308 || h->root.type == bfd_link_hash_defweak)
2309 && !h->def_regular
2310 && (h->root.u.def.section->owner != NULL
2311 ? (bfd_get_flavour (h->root.u.def.section->owner)
2312 != bfd_target_elf_flavour)
2313 : (bfd_is_abs_section (h->root.u.def.section)
2314 && !h->def_dynamic)))
2315 h->def_regular = 1;
2316 }
2317
2318 /* Backend specific symbol fixup. */
2319 if (elf_hash_table (eif->info)->dynobj)
2320 {
2321 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2322 if (bed->elf_backend_fixup_symbol
2323 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2324 return FALSE;
2325 }
2326
2327 /* If this is a final link, and the symbol was defined as a common
2328 symbol in a regular object file, and there was no definition in
2329 any dynamic object, then the linker will have allocated space for
2330 the symbol in a common section but the DEF_REGULAR
2331 flag will not have been set. */
2332 if (h->root.type == bfd_link_hash_defined
2333 && !h->def_regular
2334 && h->ref_regular
2335 && !h->def_dynamic
2336 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2337 h->def_regular = 1;
2338
2339 /* If -Bsymbolic was used (which means to bind references to global
2340 symbols to the definition within the shared object), and this
2341 symbol was defined in a regular object, then it actually doesn't
2342 need a PLT entry. Likewise, if the symbol has non-default
2343 visibility. If the symbol has hidden or internal visibility, we
2344 will force it local. */
2345 if (h->needs_plt
2346 && eif->info->shared
2347 && is_elf_hash_table (eif->info->hash)
2348 && (eif->info->symbolic
2349 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2350 && h->def_regular)
2351 {
2352 bfd_boolean force_local;
2353
2354 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2355 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2356 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2357 }
2358
2359 /* If a weak undefined symbol has non-default visibility, we also
2360 hide it from the dynamic linker. */
2361 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2362 && h->root.type == bfd_link_hash_undefweak)
2363 {
2364 const struct elf_backend_data *bed;
2365 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2366 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2367 }
2368
2369 /* If this is a weak defined symbol in a dynamic object, and we know
2370 the real definition in the dynamic object, copy interesting flags
2371 over to the real definition. */
2372 if (h->u.weakdef != NULL)
2373 {
2374 struct elf_link_hash_entry *weakdef;
2375
2376 weakdef = h->u.weakdef;
2377 if (h->root.type == bfd_link_hash_indirect)
2378 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2379
2380 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2381 || h->root.type == bfd_link_hash_defweak);
2382 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2383 || weakdef->root.type == bfd_link_hash_defweak);
2384 BFD_ASSERT (weakdef->def_dynamic);
2385
2386 /* If the real definition is defined by a regular object file,
2387 don't do anything special. See the longer description in
2388 _bfd_elf_adjust_dynamic_symbol, below. */
2389 if (weakdef->def_regular)
2390 h->u.weakdef = NULL;
2391 else
2392 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2393 h);
2394 }
2395
2396 return TRUE;
2397 }
2398
2399 /* Make the backend pick a good value for a dynamic symbol. This is
2400 called via elf_link_hash_traverse, and also calls itself
2401 recursively. */
2402
2403 bfd_boolean
2404 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2405 {
2406 struct elf_info_failed *eif = data;
2407 bfd *dynobj;
2408 const struct elf_backend_data *bed;
2409
2410 if (! is_elf_hash_table (eif->info->hash))
2411 return FALSE;
2412
2413 if (h->root.type == bfd_link_hash_warning)
2414 {
2415 h->got = elf_hash_table (eif->info)->init_got_offset;
2416 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2417
2418 /* When warning symbols are created, they **replace** the "real"
2419 entry in the hash table, thus we never get to see the real
2420 symbol in a hash traversal. So look at it now. */
2421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2422 }
2423
2424 /* Ignore indirect symbols. These are added by the versioning code. */
2425 if (h->root.type == bfd_link_hash_indirect)
2426 return TRUE;
2427
2428 /* Fix the symbol flags. */
2429 if (! _bfd_elf_fix_symbol_flags (h, eif))
2430 return FALSE;
2431
2432 /* If this symbol does not require a PLT entry, and it is not
2433 defined by a dynamic object, or is not referenced by a regular
2434 object, ignore it. We do have to handle a weak defined symbol,
2435 even if no regular object refers to it, if we decided to add it
2436 to the dynamic symbol table. FIXME: Do we normally need to worry
2437 about symbols which are defined by one dynamic object and
2438 referenced by another one? */
2439 if (!h->needs_plt
2440 && (h->def_regular
2441 || !h->def_dynamic
2442 || (!h->ref_regular
2443 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2444 {
2445 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2446 return TRUE;
2447 }
2448
2449 /* If we've already adjusted this symbol, don't do it again. This
2450 can happen via a recursive call. */
2451 if (h->dynamic_adjusted)
2452 return TRUE;
2453
2454 /* Don't look at this symbol again. Note that we must set this
2455 after checking the above conditions, because we may look at a
2456 symbol once, decide not to do anything, and then get called
2457 recursively later after REF_REGULAR is set below. */
2458 h->dynamic_adjusted = 1;
2459
2460 /* If this is a weak definition, and we know a real definition, and
2461 the real symbol is not itself defined by a regular object file,
2462 then get a good value for the real definition. We handle the
2463 real symbol first, for the convenience of the backend routine.
2464
2465 Note that there is a confusing case here. If the real definition
2466 is defined by a regular object file, we don't get the real symbol
2467 from the dynamic object, but we do get the weak symbol. If the
2468 processor backend uses a COPY reloc, then if some routine in the
2469 dynamic object changes the real symbol, we will not see that
2470 change in the corresponding weak symbol. This is the way other
2471 ELF linkers work as well, and seems to be a result of the shared
2472 library model.
2473
2474 I will clarify this issue. Most SVR4 shared libraries define the
2475 variable _timezone and define timezone as a weak synonym. The
2476 tzset call changes _timezone. If you write
2477 extern int timezone;
2478 int _timezone = 5;
2479 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2480 you might expect that, since timezone is a synonym for _timezone,
2481 the same number will print both times. However, if the processor
2482 backend uses a COPY reloc, then actually timezone will be copied
2483 into your process image, and, since you define _timezone
2484 yourself, _timezone will not. Thus timezone and _timezone will
2485 wind up at different memory locations. The tzset call will set
2486 _timezone, leaving timezone unchanged. */
2487
2488 if (h->u.weakdef != NULL)
2489 {
2490 /* If we get to this point, we know there is an implicit
2491 reference by a regular object file via the weak symbol H.
2492 FIXME: Is this really true? What if the traversal finds
2493 H->U.WEAKDEF before it finds H? */
2494 h->u.weakdef->ref_regular = 1;
2495
2496 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2497 return FALSE;
2498 }
2499
2500 /* If a symbol has no type and no size and does not require a PLT
2501 entry, then we are probably about to do the wrong thing here: we
2502 are probably going to create a COPY reloc for an empty object.
2503 This case can arise when a shared object is built with assembly
2504 code, and the assembly code fails to set the symbol type. */
2505 if (h->size == 0
2506 && h->type == STT_NOTYPE
2507 && !h->needs_plt)
2508 (*_bfd_error_handler)
2509 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2510 h->root.root.string);
2511
2512 dynobj = elf_hash_table (eif->info)->dynobj;
2513 bed = get_elf_backend_data (dynobj);
2514 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2515 {
2516 eif->failed = TRUE;
2517 return FALSE;
2518 }
2519
2520 return TRUE;
2521 }
2522
2523 /* Adjust all external symbols pointing into SEC_MERGE sections
2524 to reflect the object merging within the sections. */
2525
2526 bfd_boolean
2527 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2528 {
2529 asection *sec;
2530
2531 if (h->root.type == bfd_link_hash_warning)
2532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2533
2534 if ((h->root.type == bfd_link_hash_defined
2535 || h->root.type == bfd_link_hash_defweak)
2536 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2537 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2538 {
2539 bfd *output_bfd = data;
2540
2541 h->root.u.def.value =
2542 _bfd_merged_section_offset (output_bfd,
2543 &h->root.u.def.section,
2544 elf_section_data (sec)->sec_info,
2545 h->root.u.def.value);
2546 }
2547
2548 return TRUE;
2549 }
2550
2551 /* Returns false if the symbol referred to by H should be considered
2552 to resolve local to the current module, and true if it should be
2553 considered to bind dynamically. */
2554
2555 bfd_boolean
2556 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2557 struct bfd_link_info *info,
2558 bfd_boolean ignore_protected)
2559 {
2560 bfd_boolean binding_stays_local_p;
2561
2562 if (h == NULL)
2563 return FALSE;
2564
2565 while (h->root.type == bfd_link_hash_indirect
2566 || h->root.type == bfd_link_hash_warning)
2567 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2568
2569 /* If it was forced local, then clearly it's not dynamic. */
2570 if (h->dynindx == -1)
2571 return FALSE;
2572 if (h->forced_local)
2573 return FALSE;
2574
2575 /* Identify the cases where name binding rules say that a
2576 visible symbol resolves locally. */
2577 binding_stays_local_p = info->executable || info->symbolic;
2578
2579 switch (ELF_ST_VISIBILITY (h->other))
2580 {
2581 case STV_INTERNAL:
2582 case STV_HIDDEN:
2583 return FALSE;
2584
2585 case STV_PROTECTED:
2586 /* Proper resolution for function pointer equality may require
2587 that these symbols perhaps be resolved dynamically, even though
2588 we should be resolving them to the current module. */
2589 if (!ignore_protected || h->type != STT_FUNC)
2590 binding_stays_local_p = TRUE;
2591 break;
2592
2593 default:
2594 break;
2595 }
2596
2597 /* If it isn't defined locally, then clearly it's dynamic. */
2598 if (!h->def_regular)
2599 return TRUE;
2600
2601 /* Otherwise, the symbol is dynamic if binding rules don't tell
2602 us that it remains local. */
2603 return !binding_stays_local_p;
2604 }
2605
2606 /* Return true if the symbol referred to by H should be considered
2607 to resolve local to the current module, and false otherwise. Differs
2608 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2609 undefined symbols and weak symbols. */
2610
2611 bfd_boolean
2612 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2613 struct bfd_link_info *info,
2614 bfd_boolean local_protected)
2615 {
2616 /* If it's a local sym, of course we resolve locally. */
2617 if (h == NULL)
2618 return TRUE;
2619
2620 /* Common symbols that become definitions don't get the DEF_REGULAR
2621 flag set, so test it first, and don't bail out. */
2622 if (ELF_COMMON_DEF_P (h))
2623 /* Do nothing. */;
2624 /* If we don't have a definition in a regular file, then we can't
2625 resolve locally. The sym is either undefined or dynamic. */
2626 else if (!h->def_regular)
2627 return FALSE;
2628
2629 /* Forced local symbols resolve locally. */
2630 if (h->forced_local)
2631 return TRUE;
2632
2633 /* As do non-dynamic symbols. */
2634 if (h->dynindx == -1)
2635 return TRUE;
2636
2637 /* At this point, we know the symbol is defined and dynamic. In an
2638 executable it must resolve locally, likewise when building symbolic
2639 shared libraries. */
2640 if (info->executable || info->symbolic)
2641 return TRUE;
2642
2643 /* Now deal with defined dynamic symbols in shared libraries. Ones
2644 with default visibility might not resolve locally. */
2645 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2646 return FALSE;
2647
2648 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2649 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2650 return TRUE;
2651
2652 /* STV_PROTECTED non-function symbols are local. */
2653 if (h->type != STT_FUNC)
2654 return TRUE;
2655
2656 /* Function pointer equality tests may require that STV_PROTECTED
2657 symbols be treated as dynamic symbols, even when we know that the
2658 dynamic linker will resolve them locally. */
2659 return local_protected;
2660 }
2661
2662 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2663 aligned. Returns the first TLS output section. */
2664
2665 struct bfd_section *
2666 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2667 {
2668 struct bfd_section *sec, *tls;
2669 unsigned int align = 0;
2670
2671 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2672 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2673 break;
2674 tls = sec;
2675
2676 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2677 if (sec->alignment_power > align)
2678 align = sec->alignment_power;
2679
2680 elf_hash_table (info)->tls_sec = tls;
2681
2682 /* Ensure the alignment of the first section is the largest alignment,
2683 so that the tls segment starts aligned. */
2684 if (tls != NULL)
2685 tls->alignment_power = align;
2686
2687 return tls;
2688 }
2689
2690 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2691 static bfd_boolean
2692 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2693 Elf_Internal_Sym *sym)
2694 {
2695 const struct elf_backend_data *bed;
2696
2697 /* Local symbols do not count, but target specific ones might. */
2698 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2699 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2700 return FALSE;
2701
2702 /* Function symbols do not count. */
2703 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2704 return FALSE;
2705
2706 /* If the section is undefined, then so is the symbol. */
2707 if (sym->st_shndx == SHN_UNDEF)
2708 return FALSE;
2709
2710 /* If the symbol is defined in the common section, then
2711 it is a common definition and so does not count. */
2712 bed = get_elf_backend_data (abfd);
2713 if (bed->common_definition (sym))
2714 return FALSE;
2715
2716 /* If the symbol is in a target specific section then we
2717 must rely upon the backend to tell us what it is. */
2718 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2719 /* FIXME - this function is not coded yet:
2720
2721 return _bfd_is_global_symbol_definition (abfd, sym);
2722
2723 Instead for now assume that the definition is not global,
2724 Even if this is wrong, at least the linker will behave
2725 in the same way that it used to do. */
2726 return FALSE;
2727
2728 return TRUE;
2729 }
2730
2731 /* Search the symbol table of the archive element of the archive ABFD
2732 whose archive map contains a mention of SYMDEF, and determine if
2733 the symbol is defined in this element. */
2734 static bfd_boolean
2735 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2736 {
2737 Elf_Internal_Shdr * hdr;
2738 bfd_size_type symcount;
2739 bfd_size_type extsymcount;
2740 bfd_size_type extsymoff;
2741 Elf_Internal_Sym *isymbuf;
2742 Elf_Internal_Sym *isym;
2743 Elf_Internal_Sym *isymend;
2744 bfd_boolean result;
2745
2746 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2747 if (abfd == NULL)
2748 return FALSE;
2749
2750 if (! bfd_check_format (abfd, bfd_object))
2751 return FALSE;
2752
2753 /* If we have already included the element containing this symbol in the
2754 link then we do not need to include it again. Just claim that any symbol
2755 it contains is not a definition, so that our caller will not decide to
2756 (re)include this element. */
2757 if (abfd->archive_pass)
2758 return FALSE;
2759
2760 /* Select the appropriate symbol table. */
2761 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2762 hdr = &elf_tdata (abfd)->symtab_hdr;
2763 else
2764 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2765
2766 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2767
2768 /* The sh_info field of the symtab header tells us where the
2769 external symbols start. We don't care about the local symbols. */
2770 if (elf_bad_symtab (abfd))
2771 {
2772 extsymcount = symcount;
2773 extsymoff = 0;
2774 }
2775 else
2776 {
2777 extsymcount = symcount - hdr->sh_info;
2778 extsymoff = hdr->sh_info;
2779 }
2780
2781 if (extsymcount == 0)
2782 return FALSE;
2783
2784 /* Read in the symbol table. */
2785 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2786 NULL, NULL, NULL);
2787 if (isymbuf == NULL)
2788 return FALSE;
2789
2790 /* Scan the symbol table looking for SYMDEF. */
2791 result = FALSE;
2792 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2793 {
2794 const char *name;
2795
2796 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2797 isym->st_name);
2798 if (name == NULL)
2799 break;
2800
2801 if (strcmp (name, symdef->name) == 0)
2802 {
2803 result = is_global_data_symbol_definition (abfd, isym);
2804 break;
2805 }
2806 }
2807
2808 free (isymbuf);
2809
2810 return result;
2811 }
2812 \f
2813 /* Add an entry to the .dynamic table. */
2814
2815 bfd_boolean
2816 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2817 bfd_vma tag,
2818 bfd_vma val)
2819 {
2820 struct elf_link_hash_table *hash_table;
2821 const struct elf_backend_data *bed;
2822 asection *s;
2823 bfd_size_type newsize;
2824 bfd_byte *newcontents;
2825 Elf_Internal_Dyn dyn;
2826
2827 hash_table = elf_hash_table (info);
2828 if (! is_elf_hash_table (hash_table))
2829 return FALSE;
2830
2831 bed = get_elf_backend_data (hash_table->dynobj);
2832 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2833 BFD_ASSERT (s != NULL);
2834
2835 newsize = s->size + bed->s->sizeof_dyn;
2836 newcontents = bfd_realloc (s->contents, newsize);
2837 if (newcontents == NULL)
2838 return FALSE;
2839
2840 dyn.d_tag = tag;
2841 dyn.d_un.d_val = val;
2842 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2843
2844 s->size = newsize;
2845 s->contents = newcontents;
2846
2847 return TRUE;
2848 }
2849
2850 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2851 otherwise just check whether one already exists. Returns -1 on error,
2852 1 if a DT_NEEDED tag already exists, and 0 on success. */
2853
2854 static int
2855 elf_add_dt_needed_tag (bfd *abfd,
2856 struct bfd_link_info *info,
2857 const char *soname,
2858 bfd_boolean do_it)
2859 {
2860 struct elf_link_hash_table *hash_table;
2861 bfd_size_type oldsize;
2862 bfd_size_type strindex;
2863
2864 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2865 return -1;
2866
2867 hash_table = elf_hash_table (info);
2868 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2869 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2870 if (strindex == (bfd_size_type) -1)
2871 return -1;
2872
2873 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2874 {
2875 asection *sdyn;
2876 const struct elf_backend_data *bed;
2877 bfd_byte *extdyn;
2878
2879 bed = get_elf_backend_data (hash_table->dynobj);
2880 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2881 if (sdyn != NULL)
2882 for (extdyn = sdyn->contents;
2883 extdyn < sdyn->contents + sdyn->size;
2884 extdyn += bed->s->sizeof_dyn)
2885 {
2886 Elf_Internal_Dyn dyn;
2887
2888 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2889 if (dyn.d_tag == DT_NEEDED
2890 && dyn.d_un.d_val == strindex)
2891 {
2892 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2893 return 1;
2894 }
2895 }
2896 }
2897
2898 if (do_it)
2899 {
2900 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2901 return -1;
2902
2903 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2904 return -1;
2905 }
2906 else
2907 /* We were just checking for existence of the tag. */
2908 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2909
2910 return 0;
2911 }
2912
2913 /* Sort symbol by value and section. */
2914 static int
2915 elf_sort_symbol (const void *arg1, const void *arg2)
2916 {
2917 const struct elf_link_hash_entry *h1;
2918 const struct elf_link_hash_entry *h2;
2919 bfd_signed_vma vdiff;
2920
2921 h1 = *(const struct elf_link_hash_entry **) arg1;
2922 h2 = *(const struct elf_link_hash_entry **) arg2;
2923 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2924 if (vdiff != 0)
2925 return vdiff > 0 ? 1 : -1;
2926 else
2927 {
2928 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2929 if (sdiff != 0)
2930 return sdiff > 0 ? 1 : -1;
2931 }
2932 return 0;
2933 }
2934
2935 /* This function is used to adjust offsets into .dynstr for
2936 dynamic symbols. This is called via elf_link_hash_traverse. */
2937
2938 static bfd_boolean
2939 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2940 {
2941 struct elf_strtab_hash *dynstr = data;
2942
2943 if (h->root.type == bfd_link_hash_warning)
2944 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2945
2946 if (h->dynindx != -1)
2947 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2948 return TRUE;
2949 }
2950
2951 /* Assign string offsets in .dynstr, update all structures referencing
2952 them. */
2953
2954 static bfd_boolean
2955 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2956 {
2957 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2958 struct elf_link_local_dynamic_entry *entry;
2959 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2960 bfd *dynobj = hash_table->dynobj;
2961 asection *sdyn;
2962 bfd_size_type size;
2963 const struct elf_backend_data *bed;
2964 bfd_byte *extdyn;
2965
2966 _bfd_elf_strtab_finalize (dynstr);
2967 size = _bfd_elf_strtab_size (dynstr);
2968
2969 bed = get_elf_backend_data (dynobj);
2970 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2971 BFD_ASSERT (sdyn != NULL);
2972
2973 /* Update all .dynamic entries referencing .dynstr strings. */
2974 for (extdyn = sdyn->contents;
2975 extdyn < sdyn->contents + sdyn->size;
2976 extdyn += bed->s->sizeof_dyn)
2977 {
2978 Elf_Internal_Dyn dyn;
2979
2980 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2981 switch (dyn.d_tag)
2982 {
2983 case DT_STRSZ:
2984 dyn.d_un.d_val = size;
2985 break;
2986 case DT_NEEDED:
2987 case DT_SONAME:
2988 case DT_RPATH:
2989 case DT_RUNPATH:
2990 case DT_FILTER:
2991 case DT_AUXILIARY:
2992 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2993 break;
2994 default:
2995 continue;
2996 }
2997 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2998 }
2999
3000 /* Now update local dynamic symbols. */
3001 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3002 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3003 entry->isym.st_name);
3004
3005 /* And the rest of dynamic symbols. */
3006 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3007
3008 /* Adjust version definitions. */
3009 if (elf_tdata (output_bfd)->cverdefs)
3010 {
3011 asection *s;
3012 bfd_byte *p;
3013 bfd_size_type i;
3014 Elf_Internal_Verdef def;
3015 Elf_Internal_Verdaux defaux;
3016
3017 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3018 p = s->contents;
3019 do
3020 {
3021 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3022 &def);
3023 p += sizeof (Elf_External_Verdef);
3024 if (def.vd_aux != sizeof (Elf_External_Verdef))
3025 continue;
3026 for (i = 0; i < def.vd_cnt; ++i)
3027 {
3028 _bfd_elf_swap_verdaux_in (output_bfd,
3029 (Elf_External_Verdaux *) p, &defaux);
3030 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3031 defaux.vda_name);
3032 _bfd_elf_swap_verdaux_out (output_bfd,
3033 &defaux, (Elf_External_Verdaux *) p);
3034 p += sizeof (Elf_External_Verdaux);
3035 }
3036 }
3037 while (def.vd_next);
3038 }
3039
3040 /* Adjust version references. */
3041 if (elf_tdata (output_bfd)->verref)
3042 {
3043 asection *s;
3044 bfd_byte *p;
3045 bfd_size_type i;
3046 Elf_Internal_Verneed need;
3047 Elf_Internal_Vernaux needaux;
3048
3049 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3050 p = s->contents;
3051 do
3052 {
3053 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3054 &need);
3055 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3056 _bfd_elf_swap_verneed_out (output_bfd, &need,
3057 (Elf_External_Verneed *) p);
3058 p += sizeof (Elf_External_Verneed);
3059 for (i = 0; i < need.vn_cnt; ++i)
3060 {
3061 _bfd_elf_swap_vernaux_in (output_bfd,
3062 (Elf_External_Vernaux *) p, &needaux);
3063 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3064 needaux.vna_name);
3065 _bfd_elf_swap_vernaux_out (output_bfd,
3066 &needaux,
3067 (Elf_External_Vernaux *) p);
3068 p += sizeof (Elf_External_Vernaux);
3069 }
3070 }
3071 while (need.vn_next);
3072 }
3073
3074 return TRUE;
3075 }
3076 \f
3077 /* Add symbols from an ELF object file to the linker hash table. */
3078
3079 static bfd_boolean
3080 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3081 {
3082 Elf_Internal_Shdr *hdr;
3083 bfd_size_type symcount;
3084 bfd_size_type extsymcount;
3085 bfd_size_type extsymoff;
3086 struct elf_link_hash_entry **sym_hash;
3087 bfd_boolean dynamic;
3088 Elf_External_Versym *extversym = NULL;
3089 Elf_External_Versym *ever;
3090 struct elf_link_hash_entry *weaks;
3091 struct elf_link_hash_entry **nondeflt_vers = NULL;
3092 bfd_size_type nondeflt_vers_cnt = 0;
3093 Elf_Internal_Sym *isymbuf = NULL;
3094 Elf_Internal_Sym *isym;
3095 Elf_Internal_Sym *isymend;
3096 const struct elf_backend_data *bed;
3097 bfd_boolean add_needed;
3098 struct elf_link_hash_table *htab;
3099 bfd_size_type amt;
3100 void *alloc_mark = NULL;
3101 struct bfd_hash_entry **old_table = NULL;
3102 unsigned int old_size = 0;
3103 unsigned int old_count = 0;
3104 void *old_tab = NULL;
3105 void *old_hash;
3106 void *old_ent;
3107 struct bfd_link_hash_entry *old_undefs = NULL;
3108 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3109 long old_dynsymcount = 0;
3110 size_t tabsize = 0;
3111 size_t hashsize = 0;
3112
3113 htab = elf_hash_table (info);
3114 bed = get_elf_backend_data (abfd);
3115
3116 if ((abfd->flags & DYNAMIC) == 0)
3117 dynamic = FALSE;
3118 else
3119 {
3120 dynamic = TRUE;
3121
3122 /* You can't use -r against a dynamic object. Also, there's no
3123 hope of using a dynamic object which does not exactly match
3124 the format of the output file. */
3125 if (info->relocatable
3126 || !is_elf_hash_table (htab)
3127 || htab->root.creator != abfd->xvec)
3128 {
3129 if (info->relocatable)
3130 bfd_set_error (bfd_error_invalid_operation);
3131 else
3132 bfd_set_error (bfd_error_wrong_format);
3133 goto error_return;
3134 }
3135 }
3136
3137 /* As a GNU extension, any input sections which are named
3138 .gnu.warning.SYMBOL are treated as warning symbols for the given
3139 symbol. This differs from .gnu.warning sections, which generate
3140 warnings when they are included in an output file. */
3141 if (info->executable)
3142 {
3143 asection *s;
3144
3145 for (s = abfd->sections; s != NULL; s = s->next)
3146 {
3147 const char *name;
3148
3149 name = bfd_get_section_name (abfd, s);
3150 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3151 {
3152 char *msg;
3153 bfd_size_type sz;
3154
3155 name += sizeof ".gnu.warning." - 1;
3156
3157 /* If this is a shared object, then look up the symbol
3158 in the hash table. If it is there, and it is already
3159 been defined, then we will not be using the entry
3160 from this shared object, so we don't need to warn.
3161 FIXME: If we see the definition in a regular object
3162 later on, we will warn, but we shouldn't. The only
3163 fix is to keep track of what warnings we are supposed
3164 to emit, and then handle them all at the end of the
3165 link. */
3166 if (dynamic)
3167 {
3168 struct elf_link_hash_entry *h;
3169
3170 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3171
3172 /* FIXME: What about bfd_link_hash_common? */
3173 if (h != NULL
3174 && (h->root.type == bfd_link_hash_defined
3175 || h->root.type == bfd_link_hash_defweak))
3176 {
3177 /* We don't want to issue this warning. Clobber
3178 the section size so that the warning does not
3179 get copied into the output file. */
3180 s->size = 0;
3181 continue;
3182 }
3183 }
3184
3185 sz = s->size;
3186 msg = bfd_alloc (abfd, sz + 1);
3187 if (msg == NULL)
3188 goto error_return;
3189
3190 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3191 goto error_return;
3192
3193 msg[sz] = '\0';
3194
3195 if (! (_bfd_generic_link_add_one_symbol
3196 (info, abfd, name, BSF_WARNING, s, 0, msg,
3197 FALSE, bed->collect, NULL)))
3198 goto error_return;
3199
3200 if (! info->relocatable)
3201 {
3202 /* Clobber the section size so that the warning does
3203 not get copied into the output file. */
3204 s->size = 0;
3205
3206 /* Also set SEC_EXCLUDE, so that symbols defined in
3207 the warning section don't get copied to the output. */
3208 s->flags |= SEC_EXCLUDE;
3209 }
3210 }
3211 }
3212 }
3213
3214 add_needed = TRUE;
3215 if (! dynamic)
3216 {
3217 /* If we are creating a shared library, create all the dynamic
3218 sections immediately. We need to attach them to something,
3219 so we attach them to this BFD, provided it is the right
3220 format. FIXME: If there are no input BFD's of the same
3221 format as the output, we can't make a shared library. */
3222 if (info->shared
3223 && is_elf_hash_table (htab)
3224 && htab->root.creator == abfd->xvec
3225 && !htab->dynamic_sections_created)
3226 {
3227 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3228 goto error_return;
3229 }
3230 }
3231 else if (!is_elf_hash_table (htab))
3232 goto error_return;
3233 else
3234 {
3235 asection *s;
3236 const char *soname = NULL;
3237 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3238 int ret;
3239
3240 /* ld --just-symbols and dynamic objects don't mix very well.
3241 ld shouldn't allow it. */
3242 if ((s = abfd->sections) != NULL
3243 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3244 abort ();
3245
3246 /* If this dynamic lib was specified on the command line with
3247 --as-needed in effect, then we don't want to add a DT_NEEDED
3248 tag unless the lib is actually used. Similary for libs brought
3249 in by another lib's DT_NEEDED. When --no-add-needed is used
3250 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3251 any dynamic library in DT_NEEDED tags in the dynamic lib at
3252 all. */
3253 add_needed = (elf_dyn_lib_class (abfd)
3254 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3255 | DYN_NO_NEEDED)) == 0;
3256
3257 s = bfd_get_section_by_name (abfd, ".dynamic");
3258 if (s != NULL)
3259 {
3260 bfd_byte *dynbuf;
3261 bfd_byte *extdyn;
3262 int elfsec;
3263 unsigned long shlink;
3264
3265 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3266 goto error_free_dyn;
3267
3268 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3269 if (elfsec == -1)
3270 goto error_free_dyn;
3271 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3272
3273 for (extdyn = dynbuf;
3274 extdyn < dynbuf + s->size;
3275 extdyn += bed->s->sizeof_dyn)
3276 {
3277 Elf_Internal_Dyn dyn;
3278
3279 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3280 if (dyn.d_tag == DT_SONAME)
3281 {
3282 unsigned int tagv = dyn.d_un.d_val;
3283 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3284 if (soname == NULL)
3285 goto error_free_dyn;
3286 }
3287 if (dyn.d_tag == DT_NEEDED)
3288 {
3289 struct bfd_link_needed_list *n, **pn;
3290 char *fnm, *anm;
3291 unsigned int tagv = dyn.d_un.d_val;
3292
3293 amt = sizeof (struct bfd_link_needed_list);
3294 n = bfd_alloc (abfd, amt);
3295 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3296 if (n == NULL || fnm == NULL)
3297 goto error_free_dyn;
3298 amt = strlen (fnm) + 1;
3299 anm = bfd_alloc (abfd, amt);
3300 if (anm == NULL)
3301 goto error_free_dyn;
3302 memcpy (anm, fnm, amt);
3303 n->name = anm;
3304 n->by = abfd;
3305 n->next = NULL;
3306 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3307 ;
3308 *pn = n;
3309 }
3310 if (dyn.d_tag == DT_RUNPATH)
3311 {
3312 struct bfd_link_needed_list *n, **pn;
3313 char *fnm, *anm;
3314 unsigned int tagv = dyn.d_un.d_val;
3315
3316 amt = sizeof (struct bfd_link_needed_list);
3317 n = bfd_alloc (abfd, amt);
3318 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3319 if (n == NULL || fnm == NULL)
3320 goto error_free_dyn;
3321 amt = strlen (fnm) + 1;
3322 anm = bfd_alloc (abfd, amt);
3323 if (anm == NULL)
3324 goto error_free_dyn;
3325 memcpy (anm, fnm, amt);
3326 n->name = anm;
3327 n->by = abfd;
3328 n->next = NULL;
3329 for (pn = & runpath;
3330 *pn != NULL;
3331 pn = &(*pn)->next)
3332 ;
3333 *pn = n;
3334 }
3335 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3336 if (!runpath && dyn.d_tag == DT_RPATH)
3337 {
3338 struct bfd_link_needed_list *n, **pn;
3339 char *fnm, *anm;
3340 unsigned int tagv = dyn.d_un.d_val;
3341
3342 amt = sizeof (struct bfd_link_needed_list);
3343 n = bfd_alloc (abfd, amt);
3344 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3345 if (n == NULL || fnm == NULL)
3346 goto error_free_dyn;
3347 amt = strlen (fnm) + 1;
3348 anm = bfd_alloc (abfd, amt);
3349 if (anm == NULL)
3350 {
3351 error_free_dyn:
3352 free (dynbuf);
3353 goto error_return;
3354 }
3355 memcpy (anm, fnm, amt);
3356 n->name = anm;
3357 n->by = abfd;
3358 n->next = NULL;
3359 for (pn = & rpath;
3360 *pn != NULL;
3361 pn = &(*pn)->next)
3362 ;
3363 *pn = n;
3364 }
3365 }
3366
3367 free (dynbuf);
3368 }
3369
3370 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3371 frees all more recently bfd_alloc'd blocks as well. */
3372 if (runpath)
3373 rpath = runpath;
3374
3375 if (rpath)
3376 {
3377 struct bfd_link_needed_list **pn;
3378 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3379 ;
3380 *pn = rpath;
3381 }
3382
3383 /* We do not want to include any of the sections in a dynamic
3384 object in the output file. We hack by simply clobbering the
3385 list of sections in the BFD. This could be handled more
3386 cleanly by, say, a new section flag; the existing
3387 SEC_NEVER_LOAD flag is not the one we want, because that one
3388 still implies that the section takes up space in the output
3389 file. */
3390 bfd_section_list_clear (abfd);
3391
3392 /* Find the name to use in a DT_NEEDED entry that refers to this
3393 object. If the object has a DT_SONAME entry, we use it.
3394 Otherwise, if the generic linker stuck something in
3395 elf_dt_name, we use that. Otherwise, we just use the file
3396 name. */
3397 if (soname == NULL || *soname == '\0')
3398 {
3399 soname = elf_dt_name (abfd);
3400 if (soname == NULL || *soname == '\0')
3401 soname = bfd_get_filename (abfd);
3402 }
3403
3404 /* Save the SONAME because sometimes the linker emulation code
3405 will need to know it. */
3406 elf_dt_name (abfd) = soname;
3407
3408 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3409 if (ret < 0)
3410 goto error_return;
3411
3412 /* If we have already included this dynamic object in the
3413 link, just ignore it. There is no reason to include a
3414 particular dynamic object more than once. */
3415 if (ret > 0)
3416 return TRUE;
3417 }
3418
3419 /* If this is a dynamic object, we always link against the .dynsym
3420 symbol table, not the .symtab symbol table. The dynamic linker
3421 will only see the .dynsym symbol table, so there is no reason to
3422 look at .symtab for a dynamic object. */
3423
3424 if (! dynamic || elf_dynsymtab (abfd) == 0)
3425 hdr = &elf_tdata (abfd)->symtab_hdr;
3426 else
3427 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3428
3429 symcount = hdr->sh_size / bed->s->sizeof_sym;
3430
3431 /* The sh_info field of the symtab header tells us where the
3432 external symbols start. We don't care about the local symbols at
3433 this point. */
3434 if (elf_bad_symtab (abfd))
3435 {
3436 extsymcount = symcount;
3437 extsymoff = 0;
3438 }
3439 else
3440 {
3441 extsymcount = symcount - hdr->sh_info;
3442 extsymoff = hdr->sh_info;
3443 }
3444
3445 sym_hash = NULL;
3446 if (extsymcount != 0)
3447 {
3448 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3449 NULL, NULL, NULL);
3450 if (isymbuf == NULL)
3451 goto error_return;
3452
3453 /* We store a pointer to the hash table entry for each external
3454 symbol. */
3455 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3456 sym_hash = bfd_alloc (abfd, amt);
3457 if (sym_hash == NULL)
3458 goto error_free_sym;
3459 elf_sym_hashes (abfd) = sym_hash;
3460 }
3461
3462 if (dynamic)
3463 {
3464 /* Read in any version definitions. */
3465 if (!_bfd_elf_slurp_version_tables (abfd,
3466 info->default_imported_symver))
3467 goto error_free_sym;
3468
3469 /* Read in the symbol versions, but don't bother to convert them
3470 to internal format. */
3471 if (elf_dynversym (abfd) != 0)
3472 {
3473 Elf_Internal_Shdr *versymhdr;
3474
3475 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3476 extversym = bfd_malloc (versymhdr->sh_size);
3477 if (extversym == NULL)
3478 goto error_free_sym;
3479 amt = versymhdr->sh_size;
3480 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3481 || bfd_bread (extversym, amt, abfd) != amt)
3482 goto error_free_vers;
3483 }
3484 }
3485
3486 /* If we are loading an as-needed shared lib, save the symbol table
3487 state before we start adding symbols. If the lib turns out
3488 to be unneeded, restore the state. */
3489 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3490 {
3491 unsigned int i;
3492 size_t entsize;
3493
3494 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3495 {
3496 struct bfd_hash_entry *p;
3497 struct elf_link_hash_entry *h;
3498
3499 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3500 {
3501 h = (struct elf_link_hash_entry *) p;
3502 entsize += htab->root.table.entsize;
3503 if (h->root.type == bfd_link_hash_warning)
3504 entsize += htab->root.table.entsize;
3505 }
3506 }
3507
3508 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3509 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3510 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3511 if (old_tab == NULL)
3512 goto error_free_vers;
3513
3514 /* Remember the current objalloc pointer, so that all mem for
3515 symbols added can later be reclaimed. */
3516 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3517 if (alloc_mark == NULL)
3518 goto error_free_vers;
3519
3520 /* Clone the symbol table and sym hashes. Remember some
3521 pointers into the symbol table, and dynamic symbol count. */
3522 old_hash = (char *) old_tab + tabsize;
3523 old_ent = (char *) old_hash + hashsize;
3524 memcpy (old_tab, htab->root.table.table, tabsize);
3525 memcpy (old_hash, sym_hash, hashsize);
3526 old_undefs = htab->root.undefs;
3527 old_undefs_tail = htab->root.undefs_tail;
3528 old_table = htab->root.table.table;
3529 old_size = htab->root.table.size;
3530 old_count = htab->root.table.count;
3531 old_dynsymcount = htab->dynsymcount;
3532
3533 for (i = 0; i < htab->root.table.size; i++)
3534 {
3535 struct bfd_hash_entry *p;
3536 struct elf_link_hash_entry *h;
3537
3538 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3539 {
3540 memcpy (old_ent, p, htab->root.table.entsize);
3541 old_ent = (char *) old_ent + htab->root.table.entsize;
3542 h = (struct elf_link_hash_entry *) p;
3543 if (h->root.type == bfd_link_hash_warning)
3544 {
3545 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3546 old_ent = (char *) old_ent + htab->root.table.entsize;
3547 }
3548 }
3549 }
3550 }
3551
3552 weaks = NULL;
3553 ever = extversym != NULL ? extversym + extsymoff : NULL;
3554 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3555 isym < isymend;
3556 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3557 {
3558 int bind;
3559 bfd_vma value;
3560 asection *sec, *new_sec;
3561 flagword flags;
3562 const char *name;
3563 struct elf_link_hash_entry *h;
3564 bfd_boolean definition;
3565 bfd_boolean size_change_ok;
3566 bfd_boolean type_change_ok;
3567 bfd_boolean new_weakdef;
3568 bfd_boolean override;
3569 bfd_boolean common;
3570 unsigned int old_alignment;
3571 bfd *old_bfd;
3572
3573 override = FALSE;
3574
3575 flags = BSF_NO_FLAGS;
3576 sec = NULL;
3577 value = isym->st_value;
3578 *sym_hash = NULL;
3579 common = bed->common_definition (isym);
3580
3581 bind = ELF_ST_BIND (isym->st_info);
3582 if (bind == STB_LOCAL)
3583 {
3584 /* This should be impossible, since ELF requires that all
3585 global symbols follow all local symbols, and that sh_info
3586 point to the first global symbol. Unfortunately, Irix 5
3587 screws this up. */
3588 continue;
3589 }
3590 else if (bind == STB_GLOBAL)
3591 {
3592 if (isym->st_shndx != SHN_UNDEF && !common)
3593 flags = BSF_GLOBAL;
3594 }
3595 else if (bind == STB_WEAK)
3596 flags = BSF_WEAK;
3597 else
3598 {
3599 /* Leave it up to the processor backend. */
3600 }
3601
3602 if (isym->st_shndx == SHN_UNDEF)
3603 sec = bfd_und_section_ptr;
3604 else if (isym->st_shndx < SHN_LORESERVE
3605 || isym->st_shndx > SHN_HIRESERVE)
3606 {
3607 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3608 if (sec == NULL)
3609 sec = bfd_abs_section_ptr;
3610 else if (sec->kept_section)
3611 {
3612 /* Symbols from discarded section are undefined, and have
3613 default visibility. */
3614 sec = bfd_und_section_ptr;
3615 isym->st_shndx = SHN_UNDEF;
3616 isym->st_other = (STV_DEFAULT
3617 | (isym->st_other & ~ ELF_ST_VISIBILITY (-1)));
3618 }
3619 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3620 value -= sec->vma;
3621 }
3622 else if (isym->st_shndx == SHN_ABS)
3623 sec = bfd_abs_section_ptr;
3624 else if (isym->st_shndx == SHN_COMMON)
3625 {
3626 sec = bfd_com_section_ptr;
3627 /* What ELF calls the size we call the value. What ELF
3628 calls the value we call the alignment. */
3629 value = isym->st_size;
3630 }
3631 else
3632 {
3633 /* Leave it up to the processor backend. */
3634 }
3635
3636 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3637 isym->st_name);
3638 if (name == NULL)
3639 goto error_free_vers;
3640
3641 if (isym->st_shndx == SHN_COMMON
3642 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3643 && !info->relocatable)
3644 {
3645 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3646
3647 if (tcomm == NULL)
3648 {
3649 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3650 (SEC_ALLOC
3651 | SEC_IS_COMMON
3652 | SEC_LINKER_CREATED
3653 | SEC_THREAD_LOCAL));
3654 if (tcomm == NULL)
3655 goto error_free_vers;
3656 }
3657 sec = tcomm;
3658 }
3659 else if (bed->elf_add_symbol_hook)
3660 {
3661 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3662 &sec, &value))
3663 goto error_free_vers;
3664
3665 /* The hook function sets the name to NULL if this symbol
3666 should be skipped for some reason. */
3667 if (name == NULL)
3668 continue;
3669 }
3670
3671 /* Sanity check that all possibilities were handled. */
3672 if (sec == NULL)
3673 {
3674 bfd_set_error (bfd_error_bad_value);
3675 goto error_free_vers;
3676 }
3677
3678 if (bfd_is_und_section (sec)
3679 || bfd_is_com_section (sec))
3680 definition = FALSE;
3681 else
3682 definition = TRUE;
3683
3684 size_change_ok = FALSE;
3685 type_change_ok = bed->type_change_ok;
3686 old_alignment = 0;
3687 old_bfd = NULL;
3688 new_sec = sec;
3689
3690 if (is_elf_hash_table (htab))
3691 {
3692 Elf_Internal_Versym iver;
3693 unsigned int vernum = 0;
3694 bfd_boolean skip;
3695
3696 if (ever == NULL)
3697 {
3698 if (info->default_imported_symver)
3699 /* Use the default symbol version created earlier. */
3700 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3701 else
3702 iver.vs_vers = 0;
3703 }
3704 else
3705 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3706
3707 vernum = iver.vs_vers & VERSYM_VERSION;
3708
3709 /* If this is a hidden symbol, or if it is not version
3710 1, we append the version name to the symbol name.
3711 However, we do not modify a non-hidden absolute symbol
3712 if it is not a function, because it might be the version
3713 symbol itself. FIXME: What if it isn't? */
3714 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3715 || (vernum > 1 && (! bfd_is_abs_section (sec)
3716 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3717 {
3718 const char *verstr;
3719 size_t namelen, verlen, newlen;
3720 char *newname, *p;
3721
3722 if (isym->st_shndx != SHN_UNDEF)
3723 {
3724 if (vernum > elf_tdata (abfd)->cverdefs)
3725 verstr = NULL;
3726 else if (vernum > 1)
3727 verstr =
3728 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3729 else
3730 verstr = "";
3731
3732 if (verstr == NULL)
3733 {
3734 (*_bfd_error_handler)
3735 (_("%B: %s: invalid version %u (max %d)"),
3736 abfd, name, vernum,
3737 elf_tdata (abfd)->cverdefs);
3738 bfd_set_error (bfd_error_bad_value);
3739 goto error_free_vers;
3740 }
3741 }
3742 else
3743 {
3744 /* We cannot simply test for the number of
3745 entries in the VERNEED section since the
3746 numbers for the needed versions do not start
3747 at 0. */
3748 Elf_Internal_Verneed *t;
3749
3750 verstr = NULL;
3751 for (t = elf_tdata (abfd)->verref;
3752 t != NULL;
3753 t = t->vn_nextref)
3754 {
3755 Elf_Internal_Vernaux *a;
3756
3757 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3758 {
3759 if (a->vna_other == vernum)
3760 {
3761 verstr = a->vna_nodename;
3762 break;
3763 }
3764 }
3765 if (a != NULL)
3766 break;
3767 }
3768 if (verstr == NULL)
3769 {
3770 (*_bfd_error_handler)
3771 (_("%B: %s: invalid needed version %d"),
3772 abfd, name, vernum);
3773 bfd_set_error (bfd_error_bad_value);
3774 goto error_free_vers;
3775 }
3776 }
3777
3778 namelen = strlen (name);
3779 verlen = strlen (verstr);
3780 newlen = namelen + verlen + 2;
3781 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3782 && isym->st_shndx != SHN_UNDEF)
3783 ++newlen;
3784
3785 newname = bfd_hash_allocate (&htab->root.table, newlen);
3786 if (newname == NULL)
3787 goto error_free_vers;
3788 memcpy (newname, name, namelen);
3789 p = newname + namelen;
3790 *p++ = ELF_VER_CHR;
3791 /* If this is a defined non-hidden version symbol,
3792 we add another @ to the name. This indicates the
3793 default version of the symbol. */
3794 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3795 && isym->st_shndx != SHN_UNDEF)
3796 *p++ = ELF_VER_CHR;
3797 memcpy (p, verstr, verlen + 1);
3798
3799 name = newname;
3800 }
3801
3802 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3803 &value, &old_alignment,
3804 sym_hash, &skip, &override,
3805 &type_change_ok, &size_change_ok))
3806 goto error_free_vers;
3807
3808 if (skip)
3809 continue;
3810
3811 if (override)
3812 definition = FALSE;
3813
3814 h = *sym_hash;
3815 while (h->root.type == bfd_link_hash_indirect
3816 || h->root.type == bfd_link_hash_warning)
3817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3818
3819 /* Remember the old alignment if this is a common symbol, so
3820 that we don't reduce the alignment later on. We can't
3821 check later, because _bfd_generic_link_add_one_symbol
3822 will set a default for the alignment which we want to
3823 override. We also remember the old bfd where the existing
3824 definition comes from. */
3825 switch (h->root.type)
3826 {
3827 default:
3828 break;
3829
3830 case bfd_link_hash_defined:
3831 case bfd_link_hash_defweak:
3832 old_bfd = h->root.u.def.section->owner;
3833 break;
3834
3835 case bfd_link_hash_common:
3836 old_bfd = h->root.u.c.p->section->owner;
3837 old_alignment = h->root.u.c.p->alignment_power;
3838 break;
3839 }
3840
3841 if (elf_tdata (abfd)->verdef != NULL
3842 && ! override
3843 && vernum > 1
3844 && definition)
3845 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3846 }
3847
3848 if (! (_bfd_generic_link_add_one_symbol
3849 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3850 (struct bfd_link_hash_entry **) sym_hash)))
3851 goto error_free_vers;
3852
3853 h = *sym_hash;
3854 while (h->root.type == bfd_link_hash_indirect
3855 || h->root.type == bfd_link_hash_warning)
3856 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3857 *sym_hash = h;
3858
3859 new_weakdef = FALSE;
3860 if (dynamic
3861 && definition
3862 && (flags & BSF_WEAK) != 0
3863 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3864 && is_elf_hash_table (htab)
3865 && h->u.weakdef == NULL)
3866 {
3867 /* Keep a list of all weak defined non function symbols from
3868 a dynamic object, using the weakdef field. Later in this
3869 function we will set the weakdef field to the correct
3870 value. We only put non-function symbols from dynamic
3871 objects on this list, because that happens to be the only
3872 time we need to know the normal symbol corresponding to a
3873 weak symbol, and the information is time consuming to
3874 figure out. If the weakdef field is not already NULL,
3875 then this symbol was already defined by some previous
3876 dynamic object, and we will be using that previous
3877 definition anyhow. */
3878
3879 h->u.weakdef = weaks;
3880 weaks = h;
3881 new_weakdef = TRUE;
3882 }
3883
3884 /* Set the alignment of a common symbol. */
3885 if ((common || bfd_is_com_section (sec))
3886 && h->root.type == bfd_link_hash_common)
3887 {
3888 unsigned int align;
3889
3890 if (common)
3891 align = bfd_log2 (isym->st_value);
3892 else
3893 {
3894 /* The new symbol is a common symbol in a shared object.
3895 We need to get the alignment from the section. */
3896 align = new_sec->alignment_power;
3897 }
3898 if (align > old_alignment
3899 /* Permit an alignment power of zero if an alignment of one
3900 is specified and no other alignments have been specified. */
3901 || (isym->st_value == 1 && old_alignment == 0))
3902 h->root.u.c.p->alignment_power = align;
3903 else
3904 h->root.u.c.p->alignment_power = old_alignment;
3905 }
3906
3907 if (is_elf_hash_table (htab))
3908 {
3909 bfd_boolean dynsym;
3910
3911 /* Check the alignment when a common symbol is involved. This
3912 can change when a common symbol is overridden by a normal
3913 definition or a common symbol is ignored due to the old
3914 normal definition. We need to make sure the maximum
3915 alignment is maintained. */
3916 if ((old_alignment || common)
3917 && h->root.type != bfd_link_hash_common)
3918 {
3919 unsigned int common_align;
3920 unsigned int normal_align;
3921 unsigned int symbol_align;
3922 bfd *normal_bfd;
3923 bfd *common_bfd;
3924
3925 symbol_align = ffs (h->root.u.def.value) - 1;
3926 if (h->root.u.def.section->owner != NULL
3927 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3928 {
3929 normal_align = h->root.u.def.section->alignment_power;
3930 if (normal_align > symbol_align)
3931 normal_align = symbol_align;
3932 }
3933 else
3934 normal_align = symbol_align;
3935
3936 if (old_alignment)
3937 {
3938 common_align = old_alignment;
3939 common_bfd = old_bfd;
3940 normal_bfd = abfd;
3941 }
3942 else
3943 {
3944 common_align = bfd_log2 (isym->st_value);
3945 common_bfd = abfd;
3946 normal_bfd = old_bfd;
3947 }
3948
3949 if (normal_align < common_align)
3950 {
3951 /* PR binutils/2735 */
3952 if (normal_bfd == NULL)
3953 (*_bfd_error_handler)
3954 (_("Warning: alignment %u of common symbol `%s' in %B"
3955 " is greater than the alignment (%u) of its section %A"),
3956 common_bfd, h->root.u.def.section,
3957 1 << common_align, name, 1 << normal_align);
3958 else
3959 (*_bfd_error_handler)
3960 (_("Warning: alignment %u of symbol `%s' in %B"
3961 " is smaller than %u in %B"),
3962 normal_bfd, common_bfd,
3963 1 << normal_align, name, 1 << common_align);
3964 }
3965 }
3966
3967 /* Remember the symbol size and type. */
3968 if (isym->st_size != 0
3969 && (definition || h->size == 0))
3970 {
3971 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3972 (*_bfd_error_handler)
3973 (_("Warning: size of symbol `%s' changed"
3974 " from %lu in %B to %lu in %B"),
3975 old_bfd, abfd,
3976 name, (unsigned long) h->size,
3977 (unsigned long) isym->st_size);
3978
3979 h->size = isym->st_size;
3980 }
3981
3982 /* If this is a common symbol, then we always want H->SIZE
3983 to be the size of the common symbol. The code just above
3984 won't fix the size if a common symbol becomes larger. We
3985 don't warn about a size change here, because that is
3986 covered by --warn-common. */
3987 if (h->root.type == bfd_link_hash_common)
3988 h->size = h->root.u.c.size;
3989
3990 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3991 && (definition || h->type == STT_NOTYPE))
3992 {
3993 if (h->type != STT_NOTYPE
3994 && h->type != ELF_ST_TYPE (isym->st_info)
3995 && ! type_change_ok)
3996 (*_bfd_error_handler)
3997 (_("Warning: type of symbol `%s' changed"
3998 " from %d to %d in %B"),
3999 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4000
4001 h->type = ELF_ST_TYPE (isym->st_info);
4002 }
4003
4004 /* If st_other has a processor-specific meaning, specific
4005 code might be needed here. We never merge the visibility
4006 attribute with the one from a dynamic object. */
4007 if (bed->elf_backend_merge_symbol_attribute)
4008 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4009 dynamic);
4010
4011 /* If this symbol has default visibility and the user has requested
4012 we not re-export it, then mark it as hidden. */
4013 if (definition && !dynamic
4014 && (abfd->no_export
4015 || (abfd->my_archive && abfd->my_archive->no_export))
4016 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4017 isym->st_other = (STV_HIDDEN
4018 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4019
4020 if (isym->st_other != 0 && !dynamic)
4021 {
4022 unsigned char hvis, symvis, other, nvis;
4023
4024 /* Take the balance of OTHER from the definition. */
4025 other = (definition ? isym->st_other : h->other);
4026 other &= ~ ELF_ST_VISIBILITY (-1);
4027
4028 /* Combine visibilities, using the most constraining one. */
4029 hvis = ELF_ST_VISIBILITY (h->other);
4030 symvis = ELF_ST_VISIBILITY (isym->st_other);
4031 if (! hvis)
4032 nvis = symvis;
4033 else if (! symvis)
4034 nvis = hvis;
4035 else
4036 nvis = hvis < symvis ? hvis : symvis;
4037
4038 h->other = other | nvis;
4039 }
4040
4041 /* Set a flag in the hash table entry indicating the type of
4042 reference or definition we just found. Keep a count of
4043 the number of dynamic symbols we find. A dynamic symbol
4044 is one which is referenced or defined by both a regular
4045 object and a shared object. */
4046 dynsym = FALSE;
4047 if (! dynamic)
4048 {
4049 if (! definition)
4050 {
4051 h->ref_regular = 1;
4052 if (bind != STB_WEAK)
4053 h->ref_regular_nonweak = 1;
4054 }
4055 else
4056 h->def_regular = 1;
4057 if (! info->executable
4058 || h->def_dynamic
4059 || h->ref_dynamic)
4060 dynsym = TRUE;
4061 }
4062 else
4063 {
4064 if (! definition)
4065 h->ref_dynamic = 1;
4066 else
4067 h->def_dynamic = 1;
4068 if (h->def_regular
4069 || h->ref_regular
4070 || (h->u.weakdef != NULL
4071 && ! new_weakdef
4072 && h->u.weakdef->dynindx != -1))
4073 dynsym = TRUE;
4074 }
4075
4076 /* Check to see if we need to add an indirect symbol for
4077 the default name. */
4078 if (definition || h->root.type == bfd_link_hash_common)
4079 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4080 &sec, &value, &dynsym,
4081 override))
4082 goto error_free_vers;
4083
4084 if (definition && !dynamic)
4085 {
4086 char *p = strchr (name, ELF_VER_CHR);
4087 if (p != NULL && p[1] != ELF_VER_CHR)
4088 {
4089 /* Queue non-default versions so that .symver x, x@FOO
4090 aliases can be checked. */
4091 if (!nondeflt_vers)
4092 {
4093 amt = ((isymend - isym + 1)
4094 * sizeof (struct elf_link_hash_entry *));
4095 nondeflt_vers = bfd_malloc (amt);
4096 }
4097 nondeflt_vers[nondeflt_vers_cnt++] = h;
4098 }
4099 }
4100
4101 if (dynsym && h->dynindx == -1)
4102 {
4103 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4104 goto error_free_vers;
4105 if (h->u.weakdef != NULL
4106 && ! new_weakdef
4107 && h->u.weakdef->dynindx == -1)
4108 {
4109 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4110 goto error_free_vers;
4111 }
4112 }
4113 else if (dynsym && h->dynindx != -1)
4114 /* If the symbol already has a dynamic index, but
4115 visibility says it should not be visible, turn it into
4116 a local symbol. */
4117 switch (ELF_ST_VISIBILITY (h->other))
4118 {
4119 case STV_INTERNAL:
4120 case STV_HIDDEN:
4121 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4122 dynsym = FALSE;
4123 break;
4124 }
4125
4126 if (!add_needed
4127 && definition
4128 && dynsym
4129 && h->ref_regular)
4130 {
4131 int ret;
4132 const char *soname = elf_dt_name (abfd);
4133
4134 /* A symbol from a library loaded via DT_NEEDED of some
4135 other library is referenced by a regular object.
4136 Add a DT_NEEDED entry for it. Issue an error if
4137 --no-add-needed is used. */
4138 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4139 {
4140 (*_bfd_error_handler)
4141 (_("%s: invalid DSO for symbol `%s' definition"),
4142 abfd, name);
4143 bfd_set_error (bfd_error_bad_value);
4144 goto error_free_vers;
4145 }
4146
4147 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4148
4149 add_needed = TRUE;
4150 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4151 if (ret < 0)
4152 goto error_free_vers;
4153
4154 BFD_ASSERT (ret == 0);
4155 }
4156 }
4157 }
4158
4159 if (extversym != NULL)
4160 {
4161 free (extversym);
4162 extversym = NULL;
4163 }
4164
4165 if (isymbuf != NULL)
4166 {
4167 free (isymbuf);
4168 isymbuf = NULL;
4169 }
4170
4171 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4172 {
4173 unsigned int i;
4174
4175 /* Restore the symbol table. */
4176 old_hash = (char *) old_tab + tabsize;
4177 old_ent = (char *) old_hash + hashsize;
4178 sym_hash = elf_sym_hashes (abfd);
4179 htab->root.table.table = old_table;
4180 htab->root.table.size = old_size;
4181 htab->root.table.count = old_count;
4182 memcpy (htab->root.table.table, old_tab, tabsize);
4183 memcpy (sym_hash, old_hash, hashsize);
4184 htab->root.undefs = old_undefs;
4185 htab->root.undefs_tail = old_undefs_tail;
4186 for (i = 0; i < htab->root.table.size; i++)
4187 {
4188 struct bfd_hash_entry *p;
4189 struct elf_link_hash_entry *h;
4190
4191 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4192 {
4193 h = (struct elf_link_hash_entry *) p;
4194 if (h->root.type == bfd_link_hash_warning)
4195 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4196 if (h->dynindx >= old_dynsymcount)
4197 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4198
4199 memcpy (p, old_ent, htab->root.table.entsize);
4200 old_ent = (char *) old_ent + htab->root.table.entsize;
4201 h = (struct elf_link_hash_entry *) p;
4202 if (h->root.type == bfd_link_hash_warning)
4203 {
4204 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4205 old_ent = (char *) old_ent + htab->root.table.entsize;
4206 }
4207 }
4208 }
4209
4210 free (old_tab);
4211 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4212 alloc_mark);
4213 if (nondeflt_vers != NULL)
4214 free (nondeflt_vers);
4215 return TRUE;
4216 }
4217
4218 if (old_tab != NULL)
4219 {
4220 free (old_tab);
4221 old_tab = NULL;
4222 }
4223
4224 /* Now that all the symbols from this input file are created, handle
4225 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4226 if (nondeflt_vers != NULL)
4227 {
4228 bfd_size_type cnt, symidx;
4229
4230 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4231 {
4232 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4233 char *shortname, *p;
4234
4235 p = strchr (h->root.root.string, ELF_VER_CHR);
4236 if (p == NULL
4237 || (h->root.type != bfd_link_hash_defined
4238 && h->root.type != bfd_link_hash_defweak))
4239 continue;
4240
4241 amt = p - h->root.root.string;
4242 shortname = bfd_malloc (amt + 1);
4243 memcpy (shortname, h->root.root.string, amt);
4244 shortname[amt] = '\0';
4245
4246 hi = (struct elf_link_hash_entry *)
4247 bfd_link_hash_lookup (&htab->root, shortname,
4248 FALSE, FALSE, FALSE);
4249 if (hi != NULL
4250 && hi->root.type == h->root.type
4251 && hi->root.u.def.value == h->root.u.def.value
4252 && hi->root.u.def.section == h->root.u.def.section)
4253 {
4254 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4255 hi->root.type = bfd_link_hash_indirect;
4256 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4257 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4258 sym_hash = elf_sym_hashes (abfd);
4259 if (sym_hash)
4260 for (symidx = 0; symidx < extsymcount; ++symidx)
4261 if (sym_hash[symidx] == hi)
4262 {
4263 sym_hash[symidx] = h;
4264 break;
4265 }
4266 }
4267 free (shortname);
4268 }
4269 free (nondeflt_vers);
4270 nondeflt_vers = NULL;
4271 }
4272
4273 /* Now set the weakdefs field correctly for all the weak defined
4274 symbols we found. The only way to do this is to search all the
4275 symbols. Since we only need the information for non functions in
4276 dynamic objects, that's the only time we actually put anything on
4277 the list WEAKS. We need this information so that if a regular
4278 object refers to a symbol defined weakly in a dynamic object, the
4279 real symbol in the dynamic object is also put in the dynamic
4280 symbols; we also must arrange for both symbols to point to the
4281 same memory location. We could handle the general case of symbol
4282 aliasing, but a general symbol alias can only be generated in
4283 assembler code, handling it correctly would be very time
4284 consuming, and other ELF linkers don't handle general aliasing
4285 either. */
4286 if (weaks != NULL)
4287 {
4288 struct elf_link_hash_entry **hpp;
4289 struct elf_link_hash_entry **hppend;
4290 struct elf_link_hash_entry **sorted_sym_hash;
4291 struct elf_link_hash_entry *h;
4292 size_t sym_count;
4293
4294 /* Since we have to search the whole symbol list for each weak
4295 defined symbol, search time for N weak defined symbols will be
4296 O(N^2). Binary search will cut it down to O(NlogN). */
4297 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4298 sorted_sym_hash = bfd_malloc (amt);
4299 if (sorted_sym_hash == NULL)
4300 goto error_return;
4301 sym_hash = sorted_sym_hash;
4302 hpp = elf_sym_hashes (abfd);
4303 hppend = hpp + extsymcount;
4304 sym_count = 0;
4305 for (; hpp < hppend; hpp++)
4306 {
4307 h = *hpp;
4308 if (h != NULL
4309 && h->root.type == bfd_link_hash_defined
4310 && h->type != STT_FUNC)
4311 {
4312 *sym_hash = h;
4313 sym_hash++;
4314 sym_count++;
4315 }
4316 }
4317
4318 qsort (sorted_sym_hash, sym_count,
4319 sizeof (struct elf_link_hash_entry *),
4320 elf_sort_symbol);
4321
4322 while (weaks != NULL)
4323 {
4324 struct elf_link_hash_entry *hlook;
4325 asection *slook;
4326 bfd_vma vlook;
4327 long ilook;
4328 size_t i, j, idx;
4329
4330 hlook = weaks;
4331 weaks = hlook->u.weakdef;
4332 hlook->u.weakdef = NULL;
4333
4334 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4335 || hlook->root.type == bfd_link_hash_defweak
4336 || hlook->root.type == bfd_link_hash_common
4337 || hlook->root.type == bfd_link_hash_indirect);
4338 slook = hlook->root.u.def.section;
4339 vlook = hlook->root.u.def.value;
4340
4341 ilook = -1;
4342 i = 0;
4343 j = sym_count;
4344 while (i < j)
4345 {
4346 bfd_signed_vma vdiff;
4347 idx = (i + j) / 2;
4348 h = sorted_sym_hash [idx];
4349 vdiff = vlook - h->root.u.def.value;
4350 if (vdiff < 0)
4351 j = idx;
4352 else if (vdiff > 0)
4353 i = idx + 1;
4354 else
4355 {
4356 long sdiff = slook->id - h->root.u.def.section->id;
4357 if (sdiff < 0)
4358 j = idx;
4359 else if (sdiff > 0)
4360 i = idx + 1;
4361 else
4362 {
4363 ilook = idx;
4364 break;
4365 }
4366 }
4367 }
4368
4369 /* We didn't find a value/section match. */
4370 if (ilook == -1)
4371 continue;
4372
4373 for (i = ilook; i < sym_count; i++)
4374 {
4375 h = sorted_sym_hash [i];
4376
4377 /* Stop if value or section doesn't match. */
4378 if (h->root.u.def.value != vlook
4379 || h->root.u.def.section != slook)
4380 break;
4381 else if (h != hlook)
4382 {
4383 hlook->u.weakdef = h;
4384
4385 /* If the weak definition is in the list of dynamic
4386 symbols, make sure the real definition is put
4387 there as well. */
4388 if (hlook->dynindx != -1 && h->dynindx == -1)
4389 {
4390 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4391 goto error_return;
4392 }
4393
4394 /* If the real definition is in the list of dynamic
4395 symbols, make sure the weak definition is put
4396 there as well. If we don't do this, then the
4397 dynamic loader might not merge the entries for the
4398 real definition and the weak definition. */
4399 if (h->dynindx != -1 && hlook->dynindx == -1)
4400 {
4401 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4402 goto error_return;
4403 }
4404 break;
4405 }
4406 }
4407 }
4408
4409 free (sorted_sym_hash);
4410 }
4411
4412 if (bed->check_directives)
4413 (*bed->check_directives) (abfd, info);
4414
4415 /* If this object is the same format as the output object, and it is
4416 not a shared library, then let the backend look through the
4417 relocs.
4418
4419 This is required to build global offset table entries and to
4420 arrange for dynamic relocs. It is not required for the
4421 particular common case of linking non PIC code, even when linking
4422 against shared libraries, but unfortunately there is no way of
4423 knowing whether an object file has been compiled PIC or not.
4424 Looking through the relocs is not particularly time consuming.
4425 The problem is that we must either (1) keep the relocs in memory,
4426 which causes the linker to require additional runtime memory or
4427 (2) read the relocs twice from the input file, which wastes time.
4428 This would be a good case for using mmap.
4429
4430 I have no idea how to handle linking PIC code into a file of a
4431 different format. It probably can't be done. */
4432 if (! dynamic
4433 && is_elf_hash_table (htab)
4434 && htab->root.creator == abfd->xvec
4435 && bed->check_relocs != NULL)
4436 {
4437 asection *o;
4438
4439 for (o = abfd->sections; o != NULL; o = o->next)
4440 {
4441 Elf_Internal_Rela *internal_relocs;
4442 bfd_boolean ok;
4443
4444 if ((o->flags & SEC_RELOC) == 0
4445 || o->reloc_count == 0
4446 || ((info->strip == strip_all || info->strip == strip_debugger)
4447 && (o->flags & SEC_DEBUGGING) != 0)
4448 || bfd_is_abs_section (o->output_section))
4449 continue;
4450
4451 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4452 info->keep_memory);
4453 if (internal_relocs == NULL)
4454 goto error_return;
4455
4456 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4457
4458 if (elf_section_data (o)->relocs != internal_relocs)
4459 free (internal_relocs);
4460
4461 if (! ok)
4462 goto error_return;
4463 }
4464 }
4465
4466 /* If this is a non-traditional link, try to optimize the handling
4467 of the .stab/.stabstr sections. */
4468 if (! dynamic
4469 && ! info->traditional_format
4470 && is_elf_hash_table (htab)
4471 && (info->strip != strip_all && info->strip != strip_debugger))
4472 {
4473 asection *stabstr;
4474
4475 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4476 if (stabstr != NULL)
4477 {
4478 bfd_size_type string_offset = 0;
4479 asection *stab;
4480
4481 for (stab = abfd->sections; stab; stab = stab->next)
4482 if (strncmp (".stab", stab->name, 5) == 0
4483 && (!stab->name[5] ||
4484 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4485 && (stab->flags & SEC_MERGE) == 0
4486 && !bfd_is_abs_section (stab->output_section))
4487 {
4488 struct bfd_elf_section_data *secdata;
4489
4490 secdata = elf_section_data (stab);
4491 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4492 stabstr, &secdata->sec_info,
4493 &string_offset))
4494 goto error_return;
4495 if (secdata->sec_info)
4496 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4497 }
4498 }
4499 }
4500
4501 if (is_elf_hash_table (htab) && add_needed)
4502 {
4503 /* Add this bfd to the loaded list. */
4504 struct elf_link_loaded_list *n;
4505
4506 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4507 if (n == NULL)
4508 goto error_return;
4509 n->abfd = abfd;
4510 n->next = htab->loaded;
4511 htab->loaded = n;
4512 }
4513
4514 return TRUE;
4515
4516 error_free_vers:
4517 if (old_tab != NULL)
4518 free (old_tab);
4519 if (nondeflt_vers != NULL)
4520 free (nondeflt_vers);
4521 if (extversym != NULL)
4522 free (extversym);
4523 error_free_sym:
4524 if (isymbuf != NULL)
4525 free (isymbuf);
4526 error_return:
4527 return FALSE;
4528 }
4529
4530 /* Return the linker hash table entry of a symbol that might be
4531 satisfied by an archive symbol. Return -1 on error. */
4532
4533 struct elf_link_hash_entry *
4534 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4535 struct bfd_link_info *info,
4536 const char *name)
4537 {
4538 struct elf_link_hash_entry *h;
4539 char *p, *copy;
4540 size_t len, first;
4541
4542 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4543 if (h != NULL)
4544 return h;
4545
4546 /* If this is a default version (the name contains @@), look up the
4547 symbol again with only one `@' as well as without the version.
4548 The effect is that references to the symbol with and without the
4549 version will be matched by the default symbol in the archive. */
4550
4551 p = strchr (name, ELF_VER_CHR);
4552 if (p == NULL || p[1] != ELF_VER_CHR)
4553 return h;
4554
4555 /* First check with only one `@'. */
4556 len = strlen (name);
4557 copy = bfd_alloc (abfd, len);
4558 if (copy == NULL)
4559 return (struct elf_link_hash_entry *) 0 - 1;
4560
4561 first = p - name + 1;
4562 memcpy (copy, name, first);
4563 memcpy (copy + first, name + first + 1, len - first);
4564
4565 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4566 if (h == NULL)
4567 {
4568 /* We also need to check references to the symbol without the
4569 version. */
4570 copy[first - 1] = '\0';
4571 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4572 FALSE, FALSE, FALSE);
4573 }
4574
4575 bfd_release (abfd, copy);
4576 return h;
4577 }
4578
4579 /* Add symbols from an ELF archive file to the linker hash table. We
4580 don't use _bfd_generic_link_add_archive_symbols because of a
4581 problem which arises on UnixWare. The UnixWare libc.so is an
4582 archive which includes an entry libc.so.1 which defines a bunch of
4583 symbols. The libc.so archive also includes a number of other
4584 object files, which also define symbols, some of which are the same
4585 as those defined in libc.so.1. Correct linking requires that we
4586 consider each object file in turn, and include it if it defines any
4587 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4588 this; it looks through the list of undefined symbols, and includes
4589 any object file which defines them. When this algorithm is used on
4590 UnixWare, it winds up pulling in libc.so.1 early and defining a
4591 bunch of symbols. This means that some of the other objects in the
4592 archive are not included in the link, which is incorrect since they
4593 precede libc.so.1 in the archive.
4594
4595 Fortunately, ELF archive handling is simpler than that done by
4596 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4597 oddities. In ELF, if we find a symbol in the archive map, and the
4598 symbol is currently undefined, we know that we must pull in that
4599 object file.
4600
4601 Unfortunately, we do have to make multiple passes over the symbol
4602 table until nothing further is resolved. */
4603
4604 static bfd_boolean
4605 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4606 {
4607 symindex c;
4608 bfd_boolean *defined = NULL;
4609 bfd_boolean *included = NULL;
4610 carsym *symdefs;
4611 bfd_boolean loop;
4612 bfd_size_type amt;
4613 const struct elf_backend_data *bed;
4614 struct elf_link_hash_entry * (*archive_symbol_lookup)
4615 (bfd *, struct bfd_link_info *, const char *);
4616
4617 if (! bfd_has_map (abfd))
4618 {
4619 /* An empty archive is a special case. */
4620 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4621 return TRUE;
4622 bfd_set_error (bfd_error_no_armap);
4623 return FALSE;
4624 }
4625
4626 /* Keep track of all symbols we know to be already defined, and all
4627 files we know to be already included. This is to speed up the
4628 second and subsequent passes. */
4629 c = bfd_ardata (abfd)->symdef_count;
4630 if (c == 0)
4631 return TRUE;
4632 amt = c;
4633 amt *= sizeof (bfd_boolean);
4634 defined = bfd_zmalloc (amt);
4635 included = bfd_zmalloc (amt);
4636 if (defined == NULL || included == NULL)
4637 goto error_return;
4638
4639 symdefs = bfd_ardata (abfd)->symdefs;
4640 bed = get_elf_backend_data (abfd);
4641 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4642
4643 do
4644 {
4645 file_ptr last;
4646 symindex i;
4647 carsym *symdef;
4648 carsym *symdefend;
4649
4650 loop = FALSE;
4651 last = -1;
4652
4653 symdef = symdefs;
4654 symdefend = symdef + c;
4655 for (i = 0; symdef < symdefend; symdef++, i++)
4656 {
4657 struct elf_link_hash_entry *h;
4658 bfd *element;
4659 struct bfd_link_hash_entry *undefs_tail;
4660 symindex mark;
4661
4662 if (defined[i] || included[i])
4663 continue;
4664 if (symdef->file_offset == last)
4665 {
4666 included[i] = TRUE;
4667 continue;
4668 }
4669
4670 h = archive_symbol_lookup (abfd, info, symdef->name);
4671 if (h == (struct elf_link_hash_entry *) 0 - 1)
4672 goto error_return;
4673
4674 if (h == NULL)
4675 continue;
4676
4677 if (h->root.type == bfd_link_hash_common)
4678 {
4679 /* We currently have a common symbol. The archive map contains
4680 a reference to this symbol, so we may want to include it. We
4681 only want to include it however, if this archive element
4682 contains a definition of the symbol, not just another common
4683 declaration of it.
4684
4685 Unfortunately some archivers (including GNU ar) will put
4686 declarations of common symbols into their archive maps, as
4687 well as real definitions, so we cannot just go by the archive
4688 map alone. Instead we must read in the element's symbol
4689 table and check that to see what kind of symbol definition
4690 this is. */
4691 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4692 continue;
4693 }
4694 else if (h->root.type != bfd_link_hash_undefined)
4695 {
4696 if (h->root.type != bfd_link_hash_undefweak)
4697 defined[i] = TRUE;
4698 continue;
4699 }
4700
4701 /* We need to include this archive member. */
4702 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4703 if (element == NULL)
4704 goto error_return;
4705
4706 if (! bfd_check_format (element, bfd_object))
4707 goto error_return;
4708
4709 /* Doublecheck that we have not included this object
4710 already--it should be impossible, but there may be
4711 something wrong with the archive. */
4712 if (element->archive_pass != 0)
4713 {
4714 bfd_set_error (bfd_error_bad_value);
4715 goto error_return;
4716 }
4717 element->archive_pass = 1;
4718
4719 undefs_tail = info->hash->undefs_tail;
4720
4721 if (! (*info->callbacks->add_archive_element) (info, element,
4722 symdef->name))
4723 goto error_return;
4724 if (! bfd_link_add_symbols (element, info))
4725 goto error_return;
4726
4727 /* If there are any new undefined symbols, we need to make
4728 another pass through the archive in order to see whether
4729 they can be defined. FIXME: This isn't perfect, because
4730 common symbols wind up on undefs_tail and because an
4731 undefined symbol which is defined later on in this pass
4732 does not require another pass. This isn't a bug, but it
4733 does make the code less efficient than it could be. */
4734 if (undefs_tail != info->hash->undefs_tail)
4735 loop = TRUE;
4736
4737 /* Look backward to mark all symbols from this object file
4738 which we have already seen in this pass. */
4739 mark = i;
4740 do
4741 {
4742 included[mark] = TRUE;
4743 if (mark == 0)
4744 break;
4745 --mark;
4746 }
4747 while (symdefs[mark].file_offset == symdef->file_offset);
4748
4749 /* We mark subsequent symbols from this object file as we go
4750 on through the loop. */
4751 last = symdef->file_offset;
4752 }
4753 }
4754 while (loop);
4755
4756 free (defined);
4757 free (included);
4758
4759 return TRUE;
4760
4761 error_return:
4762 if (defined != NULL)
4763 free (defined);
4764 if (included != NULL)
4765 free (included);
4766 return FALSE;
4767 }
4768
4769 /* Given an ELF BFD, add symbols to the global hash table as
4770 appropriate. */
4771
4772 bfd_boolean
4773 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4774 {
4775 switch (bfd_get_format (abfd))
4776 {
4777 case bfd_object:
4778 return elf_link_add_object_symbols (abfd, info);
4779 case bfd_archive:
4780 return elf_link_add_archive_symbols (abfd, info);
4781 default:
4782 bfd_set_error (bfd_error_wrong_format);
4783 return FALSE;
4784 }
4785 }
4786 \f
4787 /* This function will be called though elf_link_hash_traverse to store
4788 all hash value of the exported symbols in an array. */
4789
4790 static bfd_boolean
4791 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4792 {
4793 unsigned long **valuep = data;
4794 const char *name;
4795 char *p;
4796 unsigned long ha;
4797 char *alc = NULL;
4798
4799 if (h->root.type == bfd_link_hash_warning)
4800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4801
4802 /* Ignore indirect symbols. These are added by the versioning code. */
4803 if (h->dynindx == -1)
4804 return TRUE;
4805
4806 name = h->root.root.string;
4807 p = strchr (name, ELF_VER_CHR);
4808 if (p != NULL)
4809 {
4810 alc = bfd_malloc (p - name + 1);
4811 memcpy (alc, name, p - name);
4812 alc[p - name] = '\0';
4813 name = alc;
4814 }
4815
4816 /* Compute the hash value. */
4817 ha = bfd_elf_hash (name);
4818
4819 /* Store the found hash value in the array given as the argument. */
4820 *(*valuep)++ = ha;
4821
4822 /* And store it in the struct so that we can put it in the hash table
4823 later. */
4824 h->u.elf_hash_value = ha;
4825
4826 if (alc != NULL)
4827 free (alc);
4828
4829 return TRUE;
4830 }
4831
4832 struct collect_gnu_hash_codes
4833 {
4834 bfd *output_bfd;
4835 const struct elf_backend_data *bed;
4836 unsigned long int nsyms;
4837 unsigned long int maskbits;
4838 unsigned long int *hashcodes;
4839 unsigned long int *hashval;
4840 unsigned long int *indx;
4841 unsigned long int *counts;
4842 bfd_vma *bitmask;
4843 bfd_byte *contents;
4844 long int min_dynindx;
4845 unsigned long int bucketcount;
4846 unsigned long int symindx;
4847 long int local_indx;
4848 long int shift1, shift2;
4849 unsigned long int mask;
4850 };
4851
4852 /* This function will be called though elf_link_hash_traverse to store
4853 all hash value of the exported symbols in an array. */
4854
4855 static bfd_boolean
4856 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
4857 {
4858 struct collect_gnu_hash_codes *s = data;
4859 const char *name;
4860 char *p;
4861 unsigned long ha;
4862 char *alc = NULL;
4863
4864 if (h->root.type == bfd_link_hash_warning)
4865 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4866
4867 /* Ignore indirect symbols. These are added by the versioning code. */
4868 if (h->dynindx == -1)
4869 return TRUE;
4870
4871 /* Ignore also local symbols and undefined symbols. */
4872 if (! (*s->bed->elf_hash_symbol) (h))
4873 return TRUE;
4874
4875 name = h->root.root.string;
4876 p = strchr (name, ELF_VER_CHR);
4877 if (p != NULL)
4878 {
4879 alc = bfd_malloc (p - name + 1);
4880 memcpy (alc, name, p - name);
4881 alc[p - name] = '\0';
4882 name = alc;
4883 }
4884
4885 /* Compute the hash value. */
4886 ha = bfd_elf_gnu_hash (name);
4887
4888 /* Store the found hash value in the array for compute_bucket_count,
4889 and also for .dynsym reordering purposes. */
4890 s->hashcodes[s->nsyms] = ha;
4891 s->hashval[h->dynindx] = ha;
4892 ++s->nsyms;
4893 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
4894 s->min_dynindx = h->dynindx;
4895
4896 if (alc != NULL)
4897 free (alc);
4898
4899 return TRUE;
4900 }
4901
4902 /* This function will be called though elf_link_hash_traverse to do
4903 final dynaminc symbol renumbering. */
4904
4905 static bfd_boolean
4906 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
4907 {
4908 struct collect_gnu_hash_codes *s = data;
4909 unsigned long int bucket;
4910 unsigned long int val;
4911
4912 if (h->root.type == bfd_link_hash_warning)
4913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4914
4915 /* Ignore indirect symbols. */
4916 if (h->dynindx == -1)
4917 return TRUE;
4918
4919 /* Ignore also local symbols and undefined symbols. */
4920 if (! (*s->bed->elf_hash_symbol) (h))
4921 {
4922 if (h->dynindx >= s->min_dynindx)
4923 h->dynindx = s->local_indx++;
4924 return TRUE;
4925 }
4926
4927 bucket = s->hashval[h->dynindx] % s->bucketcount;
4928 val = (s->hashval[h->dynindx] >> s->shift1)
4929 & ((s->maskbits >> s->shift1) - 1);
4930 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
4931 s->bitmask[val]
4932 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
4933 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
4934 if (s->counts[bucket] == 1)
4935 /* Last element terminates the chain. */
4936 val |= 1;
4937 bfd_put_32 (s->output_bfd, val,
4938 s->contents + (s->indx[bucket] - s->symindx) * 4);
4939 --s->counts[bucket];
4940 h->dynindx = s->indx[bucket]++;
4941 return TRUE;
4942 }
4943
4944 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4945
4946 bfd_boolean
4947 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
4948 {
4949 return !(h->forced_local
4950 || h->root.type == bfd_link_hash_undefined
4951 || h->root.type == bfd_link_hash_undefweak
4952 || ((h->root.type == bfd_link_hash_defined
4953 || h->root.type == bfd_link_hash_defweak)
4954 && h->root.u.def.section->output_section == NULL));
4955 }
4956
4957 /* Array used to determine the number of hash table buckets to use
4958 based on the number of symbols there are. If there are fewer than
4959 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4960 fewer than 37 we use 17 buckets, and so forth. We never use more
4961 than 32771 buckets. */
4962
4963 static const size_t elf_buckets[] =
4964 {
4965 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4966 16411, 32771, 0
4967 };
4968
4969 /* Compute bucket count for hashing table. We do not use a static set
4970 of possible tables sizes anymore. Instead we determine for all
4971 possible reasonable sizes of the table the outcome (i.e., the
4972 number of collisions etc) and choose the best solution. The
4973 weighting functions are not too simple to allow the table to grow
4974 without bounds. Instead one of the weighting factors is the size.
4975 Therefore the result is always a good payoff between few collisions
4976 (= short chain lengths) and table size. */
4977 static size_t
4978 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
4979 unsigned long int nsyms, int gnu_hash)
4980 {
4981 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4982 size_t best_size = 0;
4983 unsigned long int i;
4984 bfd_size_type amt;
4985
4986 /* We have a problem here. The following code to optimize the table
4987 size requires an integer type with more the 32 bits. If
4988 BFD_HOST_U_64_BIT is set we know about such a type. */
4989 #ifdef BFD_HOST_U_64_BIT
4990 if (info->optimize)
4991 {
4992 size_t minsize;
4993 size_t maxsize;
4994 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4995 bfd *dynobj = elf_hash_table (info)->dynobj;
4996 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4997 unsigned long int *counts;
4998
4999 /* Possible optimization parameters: if we have NSYMS symbols we say
5000 that the hashing table must at least have NSYMS/4 and at most
5001 2*NSYMS buckets. */
5002 minsize = nsyms / 4;
5003 if (minsize == 0)
5004 minsize = 1;
5005 best_size = maxsize = nsyms * 2;
5006 if (gnu_hash)
5007 {
5008 if (minsize < 2)
5009 minsize = 2;
5010 if ((best_size & 31) == 0)
5011 ++best_size;
5012 }
5013
5014 /* Create array where we count the collisions in. We must use bfd_malloc
5015 since the size could be large. */
5016 amt = maxsize;
5017 amt *= sizeof (unsigned long int);
5018 counts = bfd_malloc (amt);
5019 if (counts == NULL)
5020 return 0;
5021
5022 /* Compute the "optimal" size for the hash table. The criteria is a
5023 minimal chain length. The minor criteria is (of course) the size
5024 of the table. */
5025 for (i = minsize; i < maxsize; ++i)
5026 {
5027 /* Walk through the array of hashcodes and count the collisions. */
5028 BFD_HOST_U_64_BIT max;
5029 unsigned long int j;
5030 unsigned long int fact;
5031
5032 if (gnu_hash && (i & 31) == 0)
5033 continue;
5034
5035 memset (counts, '\0', i * sizeof (unsigned long int));
5036
5037 /* Determine how often each hash bucket is used. */
5038 for (j = 0; j < nsyms; ++j)
5039 ++counts[hashcodes[j] % i];
5040
5041 /* For the weight function we need some information about the
5042 pagesize on the target. This is information need not be 100%
5043 accurate. Since this information is not available (so far) we
5044 define it here to a reasonable default value. If it is crucial
5045 to have a better value some day simply define this value. */
5046 # ifndef BFD_TARGET_PAGESIZE
5047 # define BFD_TARGET_PAGESIZE (4096)
5048 # endif
5049
5050 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5051 and the chains. */
5052 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5053
5054 # if 1
5055 /* Variant 1: optimize for short chains. We add the squares
5056 of all the chain lengths (which favors many small chain
5057 over a few long chains). */
5058 for (j = 0; j < i; ++j)
5059 max += counts[j] * counts[j];
5060
5061 /* This adds penalties for the overall size of the table. */
5062 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5063 max *= fact * fact;
5064 # else
5065 /* Variant 2: Optimize a lot more for small table. Here we
5066 also add squares of the size but we also add penalties for
5067 empty slots (the +1 term). */
5068 for (j = 0; j < i; ++j)
5069 max += (1 + counts[j]) * (1 + counts[j]);
5070
5071 /* The overall size of the table is considered, but not as
5072 strong as in variant 1, where it is squared. */
5073 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5074 max *= fact;
5075 # endif
5076
5077 /* Compare with current best results. */
5078 if (max < best_chlen)
5079 {
5080 best_chlen = max;
5081 best_size = i;
5082 }
5083 }
5084
5085 free (counts);
5086 }
5087 else
5088 #endif /* defined (BFD_HOST_U_64_BIT) */
5089 {
5090 /* This is the fallback solution if no 64bit type is available or if we
5091 are not supposed to spend much time on optimizations. We select the
5092 bucket count using a fixed set of numbers. */
5093 for (i = 0; elf_buckets[i] != 0; i++)
5094 {
5095 best_size = elf_buckets[i];
5096 if (nsyms < elf_buckets[i + 1])
5097 break;
5098 }
5099 if (gnu_hash && best_size < 2)
5100 best_size = 2;
5101 }
5102
5103 return best_size;
5104 }
5105
5106 /* Set up the sizes and contents of the ELF dynamic sections. This is
5107 called by the ELF linker emulation before_allocation routine. We
5108 must set the sizes of the sections before the linker sets the
5109 addresses of the various sections. */
5110
5111 bfd_boolean
5112 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5113 const char *soname,
5114 const char *rpath,
5115 const char *filter_shlib,
5116 const char * const *auxiliary_filters,
5117 struct bfd_link_info *info,
5118 asection **sinterpptr,
5119 struct bfd_elf_version_tree *verdefs)
5120 {
5121 bfd_size_type soname_indx;
5122 bfd *dynobj;
5123 const struct elf_backend_data *bed;
5124 struct elf_assign_sym_version_info asvinfo;
5125
5126 *sinterpptr = NULL;
5127
5128 soname_indx = (bfd_size_type) -1;
5129
5130 if (!is_elf_hash_table (info->hash))
5131 return TRUE;
5132
5133 elf_tdata (output_bfd)->relro = info->relro;
5134 if (info->execstack)
5135 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5136 else if (info->noexecstack)
5137 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5138 else
5139 {
5140 bfd *inputobj;
5141 asection *notesec = NULL;
5142 int exec = 0;
5143
5144 for (inputobj = info->input_bfds;
5145 inputobj;
5146 inputobj = inputobj->link_next)
5147 {
5148 asection *s;
5149
5150 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5151 continue;
5152 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5153 if (s)
5154 {
5155 if (s->flags & SEC_CODE)
5156 exec = PF_X;
5157 notesec = s;
5158 }
5159 else
5160 exec = PF_X;
5161 }
5162 if (notesec)
5163 {
5164 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5165 if (exec && info->relocatable
5166 && notesec->output_section != bfd_abs_section_ptr)
5167 notesec->output_section->flags |= SEC_CODE;
5168 }
5169 }
5170
5171 /* Any syms created from now on start with -1 in
5172 got.refcount/offset and plt.refcount/offset. */
5173 elf_hash_table (info)->init_got_refcount
5174 = elf_hash_table (info)->init_got_offset;
5175 elf_hash_table (info)->init_plt_refcount
5176 = elf_hash_table (info)->init_plt_offset;
5177
5178 /* The backend may have to create some sections regardless of whether
5179 we're dynamic or not. */
5180 bed = get_elf_backend_data (output_bfd);
5181 if (bed->elf_backend_always_size_sections
5182 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5183 return FALSE;
5184
5185 dynobj = elf_hash_table (info)->dynobj;
5186
5187 /* If there were no dynamic objects in the link, there is nothing to
5188 do here. */
5189 if (dynobj == NULL)
5190 return TRUE;
5191
5192 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5193 return FALSE;
5194
5195 if (elf_hash_table (info)->dynamic_sections_created)
5196 {
5197 struct elf_info_failed eif;
5198 struct elf_link_hash_entry *h;
5199 asection *dynstr;
5200 struct bfd_elf_version_tree *t;
5201 struct bfd_elf_version_expr *d;
5202 asection *s;
5203 bfd_boolean all_defined;
5204
5205 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5206 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5207
5208 if (soname != NULL)
5209 {
5210 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5211 soname, TRUE);
5212 if (soname_indx == (bfd_size_type) -1
5213 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5214 return FALSE;
5215 }
5216
5217 if (info->symbolic)
5218 {
5219 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5220 return FALSE;
5221 info->flags |= DF_SYMBOLIC;
5222 }
5223
5224 if (rpath != NULL)
5225 {
5226 bfd_size_type indx;
5227
5228 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5229 TRUE);
5230 if (indx == (bfd_size_type) -1
5231 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5232 return FALSE;
5233
5234 if (info->new_dtags)
5235 {
5236 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5237 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5238 return FALSE;
5239 }
5240 }
5241
5242 if (filter_shlib != NULL)
5243 {
5244 bfd_size_type indx;
5245
5246 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5247 filter_shlib, TRUE);
5248 if (indx == (bfd_size_type) -1
5249 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5250 return FALSE;
5251 }
5252
5253 if (auxiliary_filters != NULL)
5254 {
5255 const char * const *p;
5256
5257 for (p = auxiliary_filters; *p != NULL; p++)
5258 {
5259 bfd_size_type indx;
5260
5261 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5262 *p, TRUE);
5263 if (indx == (bfd_size_type) -1
5264 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5265 return FALSE;
5266 }
5267 }
5268
5269 eif.info = info;
5270 eif.verdefs = verdefs;
5271 eif.failed = FALSE;
5272
5273 /* If we are supposed to export all symbols into the dynamic symbol
5274 table (this is not the normal case), then do so. */
5275 if (info->export_dynamic)
5276 {
5277 elf_link_hash_traverse (elf_hash_table (info),
5278 _bfd_elf_export_symbol,
5279 &eif);
5280 if (eif.failed)
5281 return FALSE;
5282 }
5283
5284 /* Make all global versions with definition. */
5285 for (t = verdefs; t != NULL; t = t->next)
5286 for (d = t->globals.list; d != NULL; d = d->next)
5287 if (!d->symver && d->symbol)
5288 {
5289 const char *verstr, *name;
5290 size_t namelen, verlen, newlen;
5291 char *newname, *p;
5292 struct elf_link_hash_entry *newh;
5293
5294 name = d->symbol;
5295 namelen = strlen (name);
5296 verstr = t->name;
5297 verlen = strlen (verstr);
5298 newlen = namelen + verlen + 3;
5299
5300 newname = bfd_malloc (newlen);
5301 if (newname == NULL)
5302 return FALSE;
5303 memcpy (newname, name, namelen);
5304
5305 /* Check the hidden versioned definition. */
5306 p = newname + namelen;
5307 *p++ = ELF_VER_CHR;
5308 memcpy (p, verstr, verlen + 1);
5309 newh = elf_link_hash_lookup (elf_hash_table (info),
5310 newname, FALSE, FALSE,
5311 FALSE);
5312 if (newh == NULL
5313 || (newh->root.type != bfd_link_hash_defined
5314 && newh->root.type != bfd_link_hash_defweak))
5315 {
5316 /* Check the default versioned definition. */
5317 *p++ = ELF_VER_CHR;
5318 memcpy (p, verstr, verlen + 1);
5319 newh = elf_link_hash_lookup (elf_hash_table (info),
5320 newname, FALSE, FALSE,
5321 FALSE);
5322 }
5323 free (newname);
5324
5325 /* Mark this version if there is a definition and it is
5326 not defined in a shared object. */
5327 if (newh != NULL
5328 && !newh->def_dynamic
5329 && (newh->root.type == bfd_link_hash_defined
5330 || newh->root.type == bfd_link_hash_defweak))
5331 d->symver = 1;
5332 }
5333
5334 /* Attach all the symbols to their version information. */
5335 asvinfo.output_bfd = output_bfd;
5336 asvinfo.info = info;
5337 asvinfo.verdefs = verdefs;
5338 asvinfo.failed = FALSE;
5339
5340 elf_link_hash_traverse (elf_hash_table (info),
5341 _bfd_elf_link_assign_sym_version,
5342 &asvinfo);
5343 if (asvinfo.failed)
5344 return FALSE;
5345
5346 if (!info->allow_undefined_version)
5347 {
5348 /* Check if all global versions have a definition. */
5349 all_defined = TRUE;
5350 for (t = verdefs; t != NULL; t = t->next)
5351 for (d = t->globals.list; d != NULL; d = d->next)
5352 if (!d->symver && !d->script)
5353 {
5354 (*_bfd_error_handler)
5355 (_("%s: undefined version: %s"),
5356 d->pattern, t->name);
5357 all_defined = FALSE;
5358 }
5359
5360 if (!all_defined)
5361 {
5362 bfd_set_error (bfd_error_bad_value);
5363 return FALSE;
5364 }
5365 }
5366
5367 /* Find all symbols which were defined in a dynamic object and make
5368 the backend pick a reasonable value for them. */
5369 elf_link_hash_traverse (elf_hash_table (info),
5370 _bfd_elf_adjust_dynamic_symbol,
5371 &eif);
5372 if (eif.failed)
5373 return FALSE;
5374
5375 /* Add some entries to the .dynamic section. We fill in some of the
5376 values later, in bfd_elf_final_link, but we must add the entries
5377 now so that we know the final size of the .dynamic section. */
5378
5379 /* If there are initialization and/or finalization functions to
5380 call then add the corresponding DT_INIT/DT_FINI entries. */
5381 h = (info->init_function
5382 ? elf_link_hash_lookup (elf_hash_table (info),
5383 info->init_function, FALSE,
5384 FALSE, FALSE)
5385 : NULL);
5386 if (h != NULL
5387 && (h->ref_regular
5388 || h->def_regular))
5389 {
5390 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5391 return FALSE;
5392 }
5393 h = (info->fini_function
5394 ? elf_link_hash_lookup (elf_hash_table (info),
5395 info->fini_function, FALSE,
5396 FALSE, FALSE)
5397 : NULL);
5398 if (h != NULL
5399 && (h->ref_regular
5400 || h->def_regular))
5401 {
5402 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5403 return FALSE;
5404 }
5405
5406 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5407 if (s != NULL && s->linker_has_input)
5408 {
5409 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5410 if (! info->executable)
5411 {
5412 bfd *sub;
5413 asection *o;
5414
5415 for (sub = info->input_bfds; sub != NULL;
5416 sub = sub->link_next)
5417 for (o = sub->sections; o != NULL; o = o->next)
5418 if (elf_section_data (o)->this_hdr.sh_type
5419 == SHT_PREINIT_ARRAY)
5420 {
5421 (*_bfd_error_handler)
5422 (_("%B: .preinit_array section is not allowed in DSO"),
5423 sub);
5424 break;
5425 }
5426
5427 bfd_set_error (bfd_error_nonrepresentable_section);
5428 return FALSE;
5429 }
5430
5431 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5432 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5433 return FALSE;
5434 }
5435 s = bfd_get_section_by_name (output_bfd, ".init_array");
5436 if (s != NULL && s->linker_has_input)
5437 {
5438 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5439 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5440 return FALSE;
5441 }
5442 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5443 if (s != NULL && s->linker_has_input)
5444 {
5445 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5446 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5447 return FALSE;
5448 }
5449
5450 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5451 /* If .dynstr is excluded from the link, we don't want any of
5452 these tags. Strictly, we should be checking each section
5453 individually; This quick check covers for the case where
5454 someone does a /DISCARD/ : { *(*) }. */
5455 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5456 {
5457 bfd_size_type strsize;
5458
5459 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5460 if ((info->emit_hash
5461 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5462 || (info->emit_gnu_hash
5463 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5464 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5465 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5466 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5467 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5468 bed->s->sizeof_sym))
5469 return FALSE;
5470 }
5471 }
5472
5473 /* The backend must work out the sizes of all the other dynamic
5474 sections. */
5475 if (bed->elf_backend_size_dynamic_sections
5476 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5477 return FALSE;
5478
5479 if (elf_hash_table (info)->dynamic_sections_created)
5480 {
5481 unsigned long section_sym_count;
5482 asection *s;
5483
5484 /* Set up the version definition section. */
5485 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5486 BFD_ASSERT (s != NULL);
5487
5488 /* We may have created additional version definitions if we are
5489 just linking a regular application. */
5490 verdefs = asvinfo.verdefs;
5491
5492 /* Skip anonymous version tag. */
5493 if (verdefs != NULL && verdefs->vernum == 0)
5494 verdefs = verdefs->next;
5495
5496 if (verdefs == NULL && !info->create_default_symver)
5497 s->flags |= SEC_EXCLUDE;
5498 else
5499 {
5500 unsigned int cdefs;
5501 bfd_size_type size;
5502 struct bfd_elf_version_tree *t;
5503 bfd_byte *p;
5504 Elf_Internal_Verdef def;
5505 Elf_Internal_Verdaux defaux;
5506 struct bfd_link_hash_entry *bh;
5507 struct elf_link_hash_entry *h;
5508 const char *name;
5509
5510 cdefs = 0;
5511 size = 0;
5512
5513 /* Make space for the base version. */
5514 size += sizeof (Elf_External_Verdef);
5515 size += sizeof (Elf_External_Verdaux);
5516 ++cdefs;
5517
5518 /* Make space for the default version. */
5519 if (info->create_default_symver)
5520 {
5521 size += sizeof (Elf_External_Verdef);
5522 ++cdefs;
5523 }
5524
5525 for (t = verdefs; t != NULL; t = t->next)
5526 {
5527 struct bfd_elf_version_deps *n;
5528
5529 size += sizeof (Elf_External_Verdef);
5530 size += sizeof (Elf_External_Verdaux);
5531 ++cdefs;
5532
5533 for (n = t->deps; n != NULL; n = n->next)
5534 size += sizeof (Elf_External_Verdaux);
5535 }
5536
5537 s->size = size;
5538 s->contents = bfd_alloc (output_bfd, s->size);
5539 if (s->contents == NULL && s->size != 0)
5540 return FALSE;
5541
5542 /* Fill in the version definition section. */
5543
5544 p = s->contents;
5545
5546 def.vd_version = VER_DEF_CURRENT;
5547 def.vd_flags = VER_FLG_BASE;
5548 def.vd_ndx = 1;
5549 def.vd_cnt = 1;
5550 if (info->create_default_symver)
5551 {
5552 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5553 def.vd_next = sizeof (Elf_External_Verdef);
5554 }
5555 else
5556 {
5557 def.vd_aux = sizeof (Elf_External_Verdef);
5558 def.vd_next = (sizeof (Elf_External_Verdef)
5559 + sizeof (Elf_External_Verdaux));
5560 }
5561
5562 if (soname_indx != (bfd_size_type) -1)
5563 {
5564 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5565 soname_indx);
5566 def.vd_hash = bfd_elf_hash (soname);
5567 defaux.vda_name = soname_indx;
5568 name = soname;
5569 }
5570 else
5571 {
5572 bfd_size_type indx;
5573
5574 name = lbasename (output_bfd->filename);
5575 def.vd_hash = bfd_elf_hash (name);
5576 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5577 name, FALSE);
5578 if (indx == (bfd_size_type) -1)
5579 return FALSE;
5580 defaux.vda_name = indx;
5581 }
5582 defaux.vda_next = 0;
5583
5584 _bfd_elf_swap_verdef_out (output_bfd, &def,
5585 (Elf_External_Verdef *) p);
5586 p += sizeof (Elf_External_Verdef);
5587 if (info->create_default_symver)
5588 {
5589 /* Add a symbol representing this version. */
5590 bh = NULL;
5591 if (! (_bfd_generic_link_add_one_symbol
5592 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5593 0, NULL, FALSE,
5594 get_elf_backend_data (dynobj)->collect, &bh)))
5595 return FALSE;
5596 h = (struct elf_link_hash_entry *) bh;
5597 h->non_elf = 0;
5598 h->def_regular = 1;
5599 h->type = STT_OBJECT;
5600 h->verinfo.vertree = NULL;
5601
5602 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5603 return FALSE;
5604
5605 /* Create a duplicate of the base version with the same
5606 aux block, but different flags. */
5607 def.vd_flags = 0;
5608 def.vd_ndx = 2;
5609 def.vd_aux = sizeof (Elf_External_Verdef);
5610 if (verdefs)
5611 def.vd_next = (sizeof (Elf_External_Verdef)
5612 + sizeof (Elf_External_Verdaux));
5613 else
5614 def.vd_next = 0;
5615 _bfd_elf_swap_verdef_out (output_bfd, &def,
5616 (Elf_External_Verdef *) p);
5617 p += sizeof (Elf_External_Verdef);
5618 }
5619 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5620 (Elf_External_Verdaux *) p);
5621 p += sizeof (Elf_External_Verdaux);
5622
5623 for (t = verdefs; t != NULL; t = t->next)
5624 {
5625 unsigned int cdeps;
5626 struct bfd_elf_version_deps *n;
5627
5628 cdeps = 0;
5629 for (n = t->deps; n != NULL; n = n->next)
5630 ++cdeps;
5631
5632 /* Add a symbol representing this version. */
5633 bh = NULL;
5634 if (! (_bfd_generic_link_add_one_symbol
5635 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5636 0, NULL, FALSE,
5637 get_elf_backend_data (dynobj)->collect, &bh)))
5638 return FALSE;
5639 h = (struct elf_link_hash_entry *) bh;
5640 h->non_elf = 0;
5641 h->def_regular = 1;
5642 h->type = STT_OBJECT;
5643 h->verinfo.vertree = t;
5644
5645 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5646 return FALSE;
5647
5648 def.vd_version = VER_DEF_CURRENT;
5649 def.vd_flags = 0;
5650 if (t->globals.list == NULL
5651 && t->locals.list == NULL
5652 && ! t->used)
5653 def.vd_flags |= VER_FLG_WEAK;
5654 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5655 def.vd_cnt = cdeps + 1;
5656 def.vd_hash = bfd_elf_hash (t->name);
5657 def.vd_aux = sizeof (Elf_External_Verdef);
5658 def.vd_next = 0;
5659 if (t->next != NULL)
5660 def.vd_next = (sizeof (Elf_External_Verdef)
5661 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5662
5663 _bfd_elf_swap_verdef_out (output_bfd, &def,
5664 (Elf_External_Verdef *) p);
5665 p += sizeof (Elf_External_Verdef);
5666
5667 defaux.vda_name = h->dynstr_index;
5668 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5669 h->dynstr_index);
5670 defaux.vda_next = 0;
5671 if (t->deps != NULL)
5672 defaux.vda_next = sizeof (Elf_External_Verdaux);
5673 t->name_indx = defaux.vda_name;
5674
5675 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5676 (Elf_External_Verdaux *) p);
5677 p += sizeof (Elf_External_Verdaux);
5678
5679 for (n = t->deps; n != NULL; n = n->next)
5680 {
5681 if (n->version_needed == NULL)
5682 {
5683 /* This can happen if there was an error in the
5684 version script. */
5685 defaux.vda_name = 0;
5686 }
5687 else
5688 {
5689 defaux.vda_name = n->version_needed->name_indx;
5690 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5691 defaux.vda_name);
5692 }
5693 if (n->next == NULL)
5694 defaux.vda_next = 0;
5695 else
5696 defaux.vda_next = sizeof (Elf_External_Verdaux);
5697
5698 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5699 (Elf_External_Verdaux *) p);
5700 p += sizeof (Elf_External_Verdaux);
5701 }
5702 }
5703
5704 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5705 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5706 return FALSE;
5707
5708 elf_tdata (output_bfd)->cverdefs = cdefs;
5709 }
5710
5711 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5712 {
5713 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5714 return FALSE;
5715 }
5716 else if (info->flags & DF_BIND_NOW)
5717 {
5718 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5719 return FALSE;
5720 }
5721
5722 if (info->flags_1)
5723 {
5724 if (info->executable)
5725 info->flags_1 &= ~ (DF_1_INITFIRST
5726 | DF_1_NODELETE
5727 | DF_1_NOOPEN);
5728 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5729 return FALSE;
5730 }
5731
5732 /* Work out the size of the version reference section. */
5733
5734 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5735 BFD_ASSERT (s != NULL);
5736 {
5737 struct elf_find_verdep_info sinfo;
5738
5739 sinfo.output_bfd = output_bfd;
5740 sinfo.info = info;
5741 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5742 if (sinfo.vers == 0)
5743 sinfo.vers = 1;
5744 sinfo.failed = FALSE;
5745
5746 elf_link_hash_traverse (elf_hash_table (info),
5747 _bfd_elf_link_find_version_dependencies,
5748 &sinfo);
5749
5750 if (elf_tdata (output_bfd)->verref == NULL)
5751 s->flags |= SEC_EXCLUDE;
5752 else
5753 {
5754 Elf_Internal_Verneed *t;
5755 unsigned int size;
5756 unsigned int crefs;
5757 bfd_byte *p;
5758
5759 /* Build the version definition section. */
5760 size = 0;
5761 crefs = 0;
5762 for (t = elf_tdata (output_bfd)->verref;
5763 t != NULL;
5764 t = t->vn_nextref)
5765 {
5766 Elf_Internal_Vernaux *a;
5767
5768 size += sizeof (Elf_External_Verneed);
5769 ++crefs;
5770 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5771 size += sizeof (Elf_External_Vernaux);
5772 }
5773
5774 s->size = size;
5775 s->contents = bfd_alloc (output_bfd, s->size);
5776 if (s->contents == NULL)
5777 return FALSE;
5778
5779 p = s->contents;
5780 for (t = elf_tdata (output_bfd)->verref;
5781 t != NULL;
5782 t = t->vn_nextref)
5783 {
5784 unsigned int caux;
5785 Elf_Internal_Vernaux *a;
5786 bfd_size_type indx;
5787
5788 caux = 0;
5789 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5790 ++caux;
5791
5792 t->vn_version = VER_NEED_CURRENT;
5793 t->vn_cnt = caux;
5794 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5795 elf_dt_name (t->vn_bfd) != NULL
5796 ? elf_dt_name (t->vn_bfd)
5797 : lbasename (t->vn_bfd->filename),
5798 FALSE);
5799 if (indx == (bfd_size_type) -1)
5800 return FALSE;
5801 t->vn_file = indx;
5802 t->vn_aux = sizeof (Elf_External_Verneed);
5803 if (t->vn_nextref == NULL)
5804 t->vn_next = 0;
5805 else
5806 t->vn_next = (sizeof (Elf_External_Verneed)
5807 + caux * sizeof (Elf_External_Vernaux));
5808
5809 _bfd_elf_swap_verneed_out (output_bfd, t,
5810 (Elf_External_Verneed *) p);
5811 p += sizeof (Elf_External_Verneed);
5812
5813 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5814 {
5815 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5817 a->vna_nodename, FALSE);
5818 if (indx == (bfd_size_type) -1)
5819 return FALSE;
5820 a->vna_name = indx;
5821 if (a->vna_nextptr == NULL)
5822 a->vna_next = 0;
5823 else
5824 a->vna_next = sizeof (Elf_External_Vernaux);
5825
5826 _bfd_elf_swap_vernaux_out (output_bfd, a,
5827 (Elf_External_Vernaux *) p);
5828 p += sizeof (Elf_External_Vernaux);
5829 }
5830 }
5831
5832 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5833 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5834 return FALSE;
5835
5836 elf_tdata (output_bfd)->cverrefs = crefs;
5837 }
5838 }
5839
5840 if ((elf_tdata (output_bfd)->cverrefs == 0
5841 && elf_tdata (output_bfd)->cverdefs == 0)
5842 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5843 &section_sym_count) == 0)
5844 {
5845 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5846 s->flags |= SEC_EXCLUDE;
5847 }
5848 }
5849 return TRUE;
5850 }
5851
5852 bfd_boolean
5853 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5854 {
5855 if (!is_elf_hash_table (info->hash))
5856 return TRUE;
5857
5858 if (elf_hash_table (info)->dynamic_sections_created)
5859 {
5860 bfd *dynobj;
5861 const struct elf_backend_data *bed;
5862 asection *s;
5863 bfd_size_type dynsymcount;
5864 unsigned long section_sym_count;
5865 unsigned int dtagcount;
5866
5867 dynobj = elf_hash_table (info)->dynobj;
5868
5869 /* Assign dynsym indicies. In a shared library we generate a
5870 section symbol for each output section, which come first.
5871 Next come all of the back-end allocated local dynamic syms,
5872 followed by the rest of the global symbols. */
5873
5874 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5875 &section_sym_count);
5876
5877 /* Work out the size of the symbol version section. */
5878 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5879 BFD_ASSERT (s != NULL);
5880 if (dynsymcount != 0
5881 && (s->flags & SEC_EXCLUDE) == 0)
5882 {
5883 s->size = dynsymcount * sizeof (Elf_External_Versym);
5884 s->contents = bfd_zalloc (output_bfd, s->size);
5885 if (s->contents == NULL)
5886 return FALSE;
5887
5888 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5889 return FALSE;
5890 }
5891
5892 /* Set the size of the .dynsym and .hash sections. We counted
5893 the number of dynamic symbols in elf_link_add_object_symbols.
5894 We will build the contents of .dynsym and .hash when we build
5895 the final symbol table, because until then we do not know the
5896 correct value to give the symbols. We built the .dynstr
5897 section as we went along in elf_link_add_object_symbols. */
5898 s = bfd_get_section_by_name (dynobj, ".dynsym");
5899 BFD_ASSERT (s != NULL);
5900 bed = get_elf_backend_data (output_bfd);
5901 s->size = dynsymcount * bed->s->sizeof_sym;
5902
5903 if (dynsymcount != 0)
5904 {
5905 s->contents = bfd_alloc (output_bfd, s->size);
5906 if (s->contents == NULL)
5907 return FALSE;
5908
5909 /* The first entry in .dynsym is a dummy symbol.
5910 Clear all the section syms, in case we don't output them all. */
5911 ++section_sym_count;
5912 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5913 }
5914
5915 elf_hash_table (info)->bucketcount = 0;
5916
5917 /* Compute the size of the hashing table. As a side effect this
5918 computes the hash values for all the names we export. */
5919 if (info->emit_hash)
5920 {
5921 unsigned long int *hashcodes;
5922 unsigned long int *hashcodesp;
5923 bfd_size_type amt;
5924 unsigned long int nsyms;
5925 size_t bucketcount;
5926 size_t hash_entry_size;
5927
5928 /* Compute the hash values for all exported symbols. At the same
5929 time store the values in an array so that we could use them for
5930 optimizations. */
5931 amt = dynsymcount * sizeof (unsigned long int);
5932 hashcodes = bfd_malloc (amt);
5933 if (hashcodes == NULL)
5934 return FALSE;
5935 hashcodesp = hashcodes;
5936
5937 /* Put all hash values in HASHCODES. */
5938 elf_link_hash_traverse (elf_hash_table (info),
5939 elf_collect_hash_codes, &hashcodesp);
5940
5941 nsyms = hashcodesp - hashcodes;
5942 bucketcount
5943 = compute_bucket_count (info, hashcodes, nsyms, 0);
5944 free (hashcodes);
5945
5946 if (bucketcount == 0)
5947 return FALSE;
5948
5949 elf_hash_table (info)->bucketcount = bucketcount;
5950
5951 s = bfd_get_section_by_name (dynobj, ".hash");
5952 BFD_ASSERT (s != NULL);
5953 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5954 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5955 s->contents = bfd_zalloc (output_bfd, s->size);
5956 if (s->contents == NULL)
5957 return FALSE;
5958
5959 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5960 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5961 s->contents + hash_entry_size);
5962 }
5963
5964 if (info->emit_gnu_hash)
5965 {
5966 size_t i, cnt;
5967 unsigned char *contents;
5968 struct collect_gnu_hash_codes cinfo;
5969 bfd_size_type amt;
5970 size_t bucketcount;
5971
5972 memset (&cinfo, 0, sizeof (cinfo));
5973
5974 /* Compute the hash values for all exported symbols. At the same
5975 time store the values in an array so that we could use them for
5976 optimizations. */
5977 amt = dynsymcount * 2 * sizeof (unsigned long int);
5978 cinfo.hashcodes = bfd_malloc (amt);
5979 if (cinfo.hashcodes == NULL)
5980 return FALSE;
5981
5982 cinfo.hashval = cinfo.hashcodes + dynsymcount;
5983 cinfo.min_dynindx = -1;
5984 cinfo.output_bfd = output_bfd;
5985 cinfo.bed = bed;
5986
5987 /* Put all hash values in HASHCODES. */
5988 elf_link_hash_traverse (elf_hash_table (info),
5989 elf_collect_gnu_hash_codes, &cinfo);
5990
5991 bucketcount
5992 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
5993
5994 if (bucketcount == 0)
5995 {
5996 free (cinfo.hashcodes);
5997 return FALSE;
5998 }
5999
6000 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6001 BFD_ASSERT (s != NULL);
6002
6003 if (cinfo.nsyms == 0)
6004 {
6005 /* Empty .gnu.hash section is special. */
6006 BFD_ASSERT (cinfo.min_dynindx == -1);
6007 free (cinfo.hashcodes);
6008 s->size = 5 * 4 + bed->s->arch_size / 8;
6009 contents = bfd_zalloc (output_bfd, s->size);
6010 if (contents == NULL)
6011 return FALSE;
6012 s->contents = contents;
6013 /* 1 empty bucket. */
6014 bfd_put_32 (output_bfd, 1, contents);
6015 /* SYMIDX above the special symbol 0. */
6016 bfd_put_32 (output_bfd, 1, contents + 4);
6017 /* Just one word for bitmask. */
6018 bfd_put_32 (output_bfd, 1, contents + 8);
6019 /* Only hash fn bloom filter. */
6020 bfd_put_32 (output_bfd, 0, contents + 12);
6021 /* No hashes are valid - empty bitmask. */
6022 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6023 /* No hashes in the only bucket. */
6024 bfd_put_32 (output_bfd, 0,
6025 contents + 16 + bed->s->arch_size / 8);
6026 }
6027 else
6028 {
6029 BFD_ASSERT (cinfo.min_dynindx != -1);
6030 unsigned long int maskwords, maskbitslog2;
6031
6032 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6033 if (maskbitslog2 < 3)
6034 maskbitslog2 = 5;
6035 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6036 maskbitslog2 = maskbitslog2 + 3;
6037 else
6038 maskbitslog2 = maskbitslog2 + 2;
6039 if (bed->s->arch_size == 64)
6040 {
6041 if (maskbitslog2 == 5)
6042 maskbitslog2 = 6;
6043 cinfo.shift1 = 6;
6044 }
6045 else
6046 cinfo.shift1 = 5;
6047 cinfo.mask = (1 << cinfo.shift1) - 1;
6048 cinfo.shift2 = maskbitslog2 + cinfo.shift1;
6049 cinfo.maskbits = 1 << maskbitslog2;
6050 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6051 amt = bucketcount * sizeof (unsigned long int) * 2;
6052 amt += maskwords * sizeof (bfd_vma);
6053 cinfo.bitmask = bfd_malloc (amt);
6054 if (cinfo.bitmask == NULL)
6055 {
6056 free (cinfo.hashcodes);
6057 return FALSE;
6058 }
6059
6060 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6061 cinfo.indx = cinfo.counts + bucketcount;
6062 cinfo.symindx = dynsymcount - cinfo.nsyms;
6063 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6064
6065 /* Determine how often each hash bucket is used. */
6066 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6067 for (i = 0; i < cinfo.nsyms; ++i)
6068 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6069
6070 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6071 if (cinfo.counts[i] != 0)
6072 {
6073 cinfo.indx[i] = cnt;
6074 cnt += cinfo.counts[i];
6075 }
6076 BFD_ASSERT (cnt == dynsymcount);
6077 cinfo.bucketcount = bucketcount;
6078 cinfo.local_indx = cinfo.min_dynindx;
6079
6080 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6081 s->size += cinfo.maskbits / 8;
6082 contents = bfd_zalloc (output_bfd, s->size);
6083 if (contents == NULL)
6084 {
6085 free (cinfo.bitmask);
6086 free (cinfo.hashcodes);
6087 return FALSE;
6088 }
6089
6090 s->contents = contents;
6091 bfd_put_32 (output_bfd, bucketcount, contents);
6092 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6093 bfd_put_32 (output_bfd, maskwords, contents + 8);
6094 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6095 contents += 16 + cinfo.maskbits / 8;
6096
6097 for (i = 0; i < bucketcount; ++i)
6098 {
6099 if (cinfo.counts[i] == 0)
6100 bfd_put_32 (output_bfd, 0, contents);
6101 else
6102 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6103 contents += 4;
6104 }
6105
6106 cinfo.contents = contents;
6107
6108 /* Renumber dynamic symbols, populate .gnu.hash section. */
6109 elf_link_hash_traverse (elf_hash_table (info),
6110 elf_renumber_gnu_hash_syms, &cinfo);
6111
6112 contents = s->contents + 16;
6113 for (i = 0; i < maskwords; ++i)
6114 {
6115 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6116 contents);
6117 contents += bed->s->arch_size / 8;
6118 }
6119
6120 free (cinfo.bitmask);
6121 free (cinfo.hashcodes);
6122 }
6123 }
6124
6125 s = bfd_get_section_by_name (dynobj, ".dynstr");
6126 BFD_ASSERT (s != NULL);
6127
6128 elf_finalize_dynstr (output_bfd, info);
6129
6130 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6131
6132 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6133 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6134 return FALSE;
6135 }
6136
6137 return TRUE;
6138 }
6139
6140 /* Final phase of ELF linker. */
6141
6142 /* A structure we use to avoid passing large numbers of arguments. */
6143
6144 struct elf_final_link_info
6145 {
6146 /* General link information. */
6147 struct bfd_link_info *info;
6148 /* Output BFD. */
6149 bfd *output_bfd;
6150 /* Symbol string table. */
6151 struct bfd_strtab_hash *symstrtab;
6152 /* .dynsym section. */
6153 asection *dynsym_sec;
6154 /* .hash section. */
6155 asection *hash_sec;
6156 /* symbol version section (.gnu.version). */
6157 asection *symver_sec;
6158 /* Buffer large enough to hold contents of any section. */
6159 bfd_byte *contents;
6160 /* Buffer large enough to hold external relocs of any section. */
6161 void *external_relocs;
6162 /* Buffer large enough to hold internal relocs of any section. */
6163 Elf_Internal_Rela *internal_relocs;
6164 /* Buffer large enough to hold external local symbols of any input
6165 BFD. */
6166 bfd_byte *external_syms;
6167 /* And a buffer for symbol section indices. */
6168 Elf_External_Sym_Shndx *locsym_shndx;
6169 /* Buffer large enough to hold internal local symbols of any input
6170 BFD. */
6171 Elf_Internal_Sym *internal_syms;
6172 /* Array large enough to hold a symbol index for each local symbol
6173 of any input BFD. */
6174 long *indices;
6175 /* Array large enough to hold a section pointer for each local
6176 symbol of any input BFD. */
6177 asection **sections;
6178 /* Buffer to hold swapped out symbols. */
6179 bfd_byte *symbuf;
6180 /* And one for symbol section indices. */
6181 Elf_External_Sym_Shndx *symshndxbuf;
6182 /* Number of swapped out symbols in buffer. */
6183 size_t symbuf_count;
6184 /* Number of symbols which fit in symbuf. */
6185 size_t symbuf_size;
6186 /* And same for symshndxbuf. */
6187 size_t shndxbuf_size;
6188 };
6189
6190 /* This struct is used to pass information to elf_link_output_extsym. */
6191
6192 struct elf_outext_info
6193 {
6194 bfd_boolean failed;
6195 bfd_boolean localsyms;
6196 struct elf_final_link_info *finfo;
6197 };
6198
6199 /* When performing a relocatable link, the input relocations are
6200 preserved. But, if they reference global symbols, the indices
6201 referenced must be updated. Update all the relocations in
6202 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
6203
6204 static void
6205 elf_link_adjust_relocs (bfd *abfd,
6206 Elf_Internal_Shdr *rel_hdr,
6207 unsigned int count,
6208 struct elf_link_hash_entry **rel_hash)
6209 {
6210 unsigned int i;
6211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6212 bfd_byte *erela;
6213 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6214 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6215 bfd_vma r_type_mask;
6216 int r_sym_shift;
6217
6218 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
6219 {
6220 swap_in = bed->s->swap_reloc_in;
6221 swap_out = bed->s->swap_reloc_out;
6222 }
6223 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
6224 {
6225 swap_in = bed->s->swap_reloca_in;
6226 swap_out = bed->s->swap_reloca_out;
6227 }
6228 else
6229 abort ();
6230
6231 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
6232 abort ();
6233
6234 if (bed->s->arch_size == 32)
6235 {
6236 r_type_mask = 0xff;
6237 r_sym_shift = 8;
6238 }
6239 else
6240 {
6241 r_type_mask = 0xffffffff;
6242 r_sym_shift = 32;
6243 }
6244
6245 erela = rel_hdr->contents;
6246 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
6247 {
6248 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
6249 unsigned int j;
6250
6251 if (*rel_hash == NULL)
6252 continue;
6253
6254 BFD_ASSERT ((*rel_hash)->indx >= 0);
6255
6256 (*swap_in) (abfd, erela, irela);
6257 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
6258 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
6259 | (irela[j].r_info & r_type_mask));
6260 (*swap_out) (abfd, irela, erela);
6261 }
6262 }
6263
6264 struct elf_link_sort_rela
6265 {
6266 union {
6267 bfd_vma offset;
6268 bfd_vma sym_mask;
6269 } u;
6270 enum elf_reloc_type_class type;
6271 /* We use this as an array of size int_rels_per_ext_rel. */
6272 Elf_Internal_Rela rela[1];
6273 };
6274
6275 static int
6276 elf_link_sort_cmp1 (const void *A, const void *B)
6277 {
6278 const struct elf_link_sort_rela *a = A;
6279 const struct elf_link_sort_rela *b = B;
6280 int relativea, relativeb;
6281
6282 relativea = a->type == reloc_class_relative;
6283 relativeb = b->type == reloc_class_relative;
6284
6285 if (relativea < relativeb)
6286 return 1;
6287 if (relativea > relativeb)
6288 return -1;
6289 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
6290 return -1;
6291 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
6292 return 1;
6293 if (a->rela->r_offset < b->rela->r_offset)
6294 return -1;
6295 if (a->rela->r_offset > b->rela->r_offset)
6296 return 1;
6297 return 0;
6298 }
6299
6300 static int
6301 elf_link_sort_cmp2 (const void *A, const void *B)
6302 {
6303 const struct elf_link_sort_rela *a = A;
6304 const struct elf_link_sort_rela *b = B;
6305 int copya, copyb;
6306
6307 if (a->u.offset < b->u.offset)
6308 return -1;
6309 if (a->u.offset > b->u.offset)
6310 return 1;
6311 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
6312 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
6313 if (copya < copyb)
6314 return -1;
6315 if (copya > copyb)
6316 return 1;
6317 if (a->rela->r_offset < b->rela->r_offset)
6318 return -1;
6319 if (a->rela->r_offset > b->rela->r_offset)
6320 return 1;
6321 return 0;
6322 }
6323
6324 static size_t
6325 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
6326 {
6327 asection *reldyn;
6328 bfd_size_type count, size;
6329 size_t i, ret, sort_elt, ext_size;
6330 bfd_byte *sort, *s_non_relative, *p;
6331 struct elf_link_sort_rela *sq;
6332 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6333 int i2e = bed->s->int_rels_per_ext_rel;
6334 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6335 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6336 struct bfd_link_order *lo;
6337 bfd_vma r_sym_mask;
6338
6339 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
6340 if (reldyn == NULL || reldyn->size == 0)
6341 {
6342 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
6343 if (reldyn == NULL || reldyn->size == 0)
6344 return 0;
6345 ext_size = bed->s->sizeof_rel;
6346 swap_in = bed->s->swap_reloc_in;
6347 swap_out = bed->s->swap_reloc_out;
6348 }
6349 else
6350 {
6351 ext_size = bed->s->sizeof_rela;
6352 swap_in = bed->s->swap_reloca_in;
6353 swap_out = bed->s->swap_reloca_out;
6354 }
6355 count = reldyn->size / ext_size;
6356
6357 size = 0;
6358 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6359 if (lo->type == bfd_indirect_link_order)
6360 {
6361 asection *o = lo->u.indirect.section;
6362 size += o->size;
6363 }
6364
6365 if (size != reldyn->size)
6366 return 0;
6367
6368 sort_elt = (sizeof (struct elf_link_sort_rela)
6369 + (i2e - 1) * sizeof (Elf_Internal_Rela));
6370 sort = bfd_zmalloc (sort_elt * count);
6371 if (sort == NULL)
6372 {
6373 (*info->callbacks->warning)
6374 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6375 return 0;
6376 }
6377
6378 if (bed->s->arch_size == 32)
6379 r_sym_mask = ~(bfd_vma) 0xff;
6380 else
6381 r_sym_mask = ~(bfd_vma) 0xffffffff;
6382
6383 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6384 if (lo->type == bfd_indirect_link_order)
6385 {
6386 bfd_byte *erel, *erelend;
6387 asection *o = lo->u.indirect.section;
6388
6389 if (o->contents == NULL && o->size != 0)
6390 {
6391 /* This is a reloc section that is being handled as a normal
6392 section. See bfd_section_from_shdr. We can't combine
6393 relocs in this case. */
6394 free (sort);
6395 return 0;
6396 }
6397 erel = o->contents;
6398 erelend = o->contents + o->size;
6399 p = sort + o->output_offset / ext_size * sort_elt;
6400 while (erel < erelend)
6401 {
6402 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6403 (*swap_in) (abfd, erel, s->rela);
6404 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6405 s->u.sym_mask = r_sym_mask;
6406 p += sort_elt;
6407 erel += ext_size;
6408 }
6409 }
6410
6411 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6412
6413 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6414 {
6415 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6416 if (s->type != reloc_class_relative)
6417 break;
6418 }
6419 ret = i;
6420 s_non_relative = p;
6421
6422 sq = (struct elf_link_sort_rela *) s_non_relative;
6423 for (; i < count; i++, p += sort_elt)
6424 {
6425 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6426 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6427 sq = sp;
6428 sp->u.offset = sq->rela->r_offset;
6429 }
6430
6431 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6432
6433 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6434 if (lo->type == bfd_indirect_link_order)
6435 {
6436 bfd_byte *erel, *erelend;
6437 asection *o = lo->u.indirect.section;
6438
6439 erel = o->contents;
6440 erelend = o->contents + o->size;
6441 p = sort + o->output_offset / ext_size * sort_elt;
6442 while (erel < erelend)
6443 {
6444 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6445 (*swap_out) (abfd, s->rela, erel);
6446 p += sort_elt;
6447 erel += ext_size;
6448 }
6449 }
6450
6451 free (sort);
6452 *psec = reldyn;
6453 return ret;
6454 }
6455
6456 /* Flush the output symbols to the file. */
6457
6458 static bfd_boolean
6459 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6460 const struct elf_backend_data *bed)
6461 {
6462 if (finfo->symbuf_count > 0)
6463 {
6464 Elf_Internal_Shdr *hdr;
6465 file_ptr pos;
6466 bfd_size_type amt;
6467
6468 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6469 pos = hdr->sh_offset + hdr->sh_size;
6470 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6471 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6472 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6473 return FALSE;
6474
6475 hdr->sh_size += amt;
6476 finfo->symbuf_count = 0;
6477 }
6478
6479 return TRUE;
6480 }
6481
6482 /* Add a symbol to the output symbol table. */
6483
6484 static bfd_boolean
6485 elf_link_output_sym (struct elf_final_link_info *finfo,
6486 const char *name,
6487 Elf_Internal_Sym *elfsym,
6488 asection *input_sec,
6489 struct elf_link_hash_entry *h)
6490 {
6491 bfd_byte *dest;
6492 Elf_External_Sym_Shndx *destshndx;
6493 bfd_boolean (*output_symbol_hook)
6494 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6495 struct elf_link_hash_entry *);
6496 const struct elf_backend_data *bed;
6497
6498 bed = get_elf_backend_data (finfo->output_bfd);
6499 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6500 if (output_symbol_hook != NULL)
6501 {
6502 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6503 return FALSE;
6504 }
6505
6506 if (name == NULL || *name == '\0')
6507 elfsym->st_name = 0;
6508 else if (input_sec->flags & SEC_EXCLUDE)
6509 elfsym->st_name = 0;
6510 else
6511 {
6512 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6513 name, TRUE, FALSE);
6514 if (elfsym->st_name == (unsigned long) -1)
6515 return FALSE;
6516 }
6517
6518 if (finfo->symbuf_count >= finfo->symbuf_size)
6519 {
6520 if (! elf_link_flush_output_syms (finfo, bed))
6521 return FALSE;
6522 }
6523
6524 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6525 destshndx = finfo->symshndxbuf;
6526 if (destshndx != NULL)
6527 {
6528 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6529 {
6530 bfd_size_type amt;
6531
6532 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6533 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6534 if (destshndx == NULL)
6535 return FALSE;
6536 memset ((char *) destshndx + amt, 0, amt);
6537 finfo->shndxbuf_size *= 2;
6538 }
6539 destshndx += bfd_get_symcount (finfo->output_bfd);
6540 }
6541
6542 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6543 finfo->symbuf_count += 1;
6544 bfd_get_symcount (finfo->output_bfd) += 1;
6545
6546 return TRUE;
6547 }
6548
6549 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
6550
6551 static bfd_boolean
6552 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
6553 {
6554 if (sym->st_shndx > SHN_HIRESERVE)
6555 {
6556 /* The gABI doesn't support dynamic symbols in output sections
6557 beyond 64k. */
6558 (*_bfd_error_handler)
6559 (_("%B: Too many sections: %d (>= %d)"),
6560 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
6561 bfd_set_error (bfd_error_nonrepresentable_section);
6562 return FALSE;
6563 }
6564 return TRUE;
6565 }
6566
6567 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6568 allowing an unsatisfied unversioned symbol in the DSO to match a
6569 versioned symbol that would normally require an explicit version.
6570 We also handle the case that a DSO references a hidden symbol
6571 which may be satisfied by a versioned symbol in another DSO. */
6572
6573 static bfd_boolean
6574 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6575 const struct elf_backend_data *bed,
6576 struct elf_link_hash_entry *h)
6577 {
6578 bfd *abfd;
6579 struct elf_link_loaded_list *loaded;
6580
6581 if (!is_elf_hash_table (info->hash))
6582 return FALSE;
6583
6584 switch (h->root.type)
6585 {
6586 default:
6587 abfd = NULL;
6588 break;
6589
6590 case bfd_link_hash_undefined:
6591 case bfd_link_hash_undefweak:
6592 abfd = h->root.u.undef.abfd;
6593 if ((abfd->flags & DYNAMIC) == 0
6594 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6595 return FALSE;
6596 break;
6597
6598 case bfd_link_hash_defined:
6599 case bfd_link_hash_defweak:
6600 abfd = h->root.u.def.section->owner;
6601 break;
6602
6603 case bfd_link_hash_common:
6604 abfd = h->root.u.c.p->section->owner;
6605 break;
6606 }
6607 BFD_ASSERT (abfd != NULL);
6608
6609 for (loaded = elf_hash_table (info)->loaded;
6610 loaded != NULL;
6611 loaded = loaded->next)
6612 {
6613 bfd *input;
6614 Elf_Internal_Shdr *hdr;
6615 bfd_size_type symcount;
6616 bfd_size_type extsymcount;
6617 bfd_size_type extsymoff;
6618 Elf_Internal_Shdr *versymhdr;
6619 Elf_Internal_Sym *isym;
6620 Elf_Internal_Sym *isymend;
6621 Elf_Internal_Sym *isymbuf;
6622 Elf_External_Versym *ever;
6623 Elf_External_Versym *extversym;
6624
6625 input = loaded->abfd;
6626
6627 /* We check each DSO for a possible hidden versioned definition. */
6628 if (input == abfd
6629 || (input->flags & DYNAMIC) == 0
6630 || elf_dynversym (input) == 0)
6631 continue;
6632
6633 hdr = &elf_tdata (input)->dynsymtab_hdr;
6634
6635 symcount = hdr->sh_size / bed->s->sizeof_sym;
6636 if (elf_bad_symtab (input))
6637 {
6638 extsymcount = symcount;
6639 extsymoff = 0;
6640 }
6641 else
6642 {
6643 extsymcount = symcount - hdr->sh_info;
6644 extsymoff = hdr->sh_info;
6645 }
6646
6647 if (extsymcount == 0)
6648 continue;
6649
6650 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6651 NULL, NULL, NULL);
6652 if (isymbuf == NULL)
6653 return FALSE;
6654
6655 /* Read in any version definitions. */
6656 versymhdr = &elf_tdata (input)->dynversym_hdr;
6657 extversym = bfd_malloc (versymhdr->sh_size);
6658 if (extversym == NULL)
6659 goto error_ret;
6660
6661 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6662 || (bfd_bread (extversym, versymhdr->sh_size, input)
6663 != versymhdr->sh_size))
6664 {
6665 free (extversym);
6666 error_ret:
6667 free (isymbuf);
6668 return FALSE;
6669 }
6670
6671 ever = extversym + extsymoff;
6672 isymend = isymbuf + extsymcount;
6673 for (isym = isymbuf; isym < isymend; isym++, ever++)
6674 {
6675 const char *name;
6676 Elf_Internal_Versym iver;
6677 unsigned short version_index;
6678
6679 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6680 || isym->st_shndx == SHN_UNDEF)
6681 continue;
6682
6683 name = bfd_elf_string_from_elf_section (input,
6684 hdr->sh_link,
6685 isym->st_name);
6686 if (strcmp (name, h->root.root.string) != 0)
6687 continue;
6688
6689 _bfd_elf_swap_versym_in (input, ever, &iver);
6690
6691 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6692 {
6693 /* If we have a non-hidden versioned sym, then it should
6694 have provided a definition for the undefined sym. */
6695 abort ();
6696 }
6697
6698 version_index = iver.vs_vers & VERSYM_VERSION;
6699 if (version_index == 1 || version_index == 2)
6700 {
6701 /* This is the base or first version. We can use it. */
6702 free (extversym);
6703 free (isymbuf);
6704 return TRUE;
6705 }
6706 }
6707
6708 free (extversym);
6709 free (isymbuf);
6710 }
6711
6712 return FALSE;
6713 }
6714
6715 /* Add an external symbol to the symbol table. This is called from
6716 the hash table traversal routine. When generating a shared object,
6717 we go through the symbol table twice. The first time we output
6718 anything that might have been forced to local scope in a version
6719 script. The second time we output the symbols that are still
6720 global symbols. */
6721
6722 static bfd_boolean
6723 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6724 {
6725 struct elf_outext_info *eoinfo = data;
6726 struct elf_final_link_info *finfo = eoinfo->finfo;
6727 bfd_boolean strip;
6728 Elf_Internal_Sym sym;
6729 asection *input_sec;
6730 const struct elf_backend_data *bed;
6731
6732 if (h->root.type == bfd_link_hash_warning)
6733 {
6734 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6735 if (h->root.type == bfd_link_hash_new)
6736 return TRUE;
6737 }
6738
6739 /* Decide whether to output this symbol in this pass. */
6740 if (eoinfo->localsyms)
6741 {
6742 if (!h->forced_local)
6743 return TRUE;
6744 }
6745 else
6746 {
6747 if (h->forced_local)
6748 return TRUE;
6749 }
6750
6751 bed = get_elf_backend_data (finfo->output_bfd);
6752
6753 if (h->root.type == bfd_link_hash_undefined)
6754 {
6755 /* If we have an undefined symbol reference here then it must have
6756 come from a shared library that is being linked in. (Undefined
6757 references in regular files have already been handled). */
6758 bfd_boolean ignore_undef = FALSE;
6759
6760 /* Some symbols may be special in that the fact that they're
6761 undefined can be safely ignored - let backend determine that. */
6762 if (bed->elf_backend_ignore_undef_symbol)
6763 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6764
6765 /* If we are reporting errors for this situation then do so now. */
6766 if (ignore_undef == FALSE
6767 && h->ref_dynamic
6768 && ! h->ref_regular
6769 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6770 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6771 {
6772 if (! (finfo->info->callbacks->undefined_symbol
6773 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6774 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6775 {
6776 eoinfo->failed = TRUE;
6777 return FALSE;
6778 }
6779 }
6780 }
6781
6782 /* We should also warn if a forced local symbol is referenced from
6783 shared libraries. */
6784 if (! finfo->info->relocatable
6785 && (! finfo->info->shared)
6786 && h->forced_local
6787 && h->ref_dynamic
6788 && !h->dynamic_def
6789 && !h->dynamic_weak
6790 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6791 {
6792 (*_bfd_error_handler)
6793 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6794 finfo->output_bfd,
6795 h->root.u.def.section == bfd_abs_section_ptr
6796 ? finfo->output_bfd : h->root.u.def.section->owner,
6797 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6798 ? "internal"
6799 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6800 ? "hidden" : "local",
6801 h->root.root.string);
6802 eoinfo->failed = TRUE;
6803 return FALSE;
6804 }
6805
6806 /* We don't want to output symbols that have never been mentioned by
6807 a regular file, or that we have been told to strip. However, if
6808 h->indx is set to -2, the symbol is used by a reloc and we must
6809 output it. */
6810 if (h->indx == -2)
6811 strip = FALSE;
6812 else if ((h->def_dynamic
6813 || h->ref_dynamic
6814 || h->root.type == bfd_link_hash_new)
6815 && !h->def_regular
6816 && !h->ref_regular)
6817 strip = TRUE;
6818 else if (finfo->info->strip == strip_all)
6819 strip = TRUE;
6820 else if (finfo->info->strip == strip_some
6821 && bfd_hash_lookup (finfo->info->keep_hash,
6822 h->root.root.string, FALSE, FALSE) == NULL)
6823 strip = TRUE;
6824 else if (finfo->info->strip_discarded
6825 && (h->root.type == bfd_link_hash_defined
6826 || h->root.type == bfd_link_hash_defweak)
6827 && elf_discarded_section (h->root.u.def.section))
6828 strip = TRUE;
6829 else
6830 strip = FALSE;
6831
6832 /* If we're stripping it, and it's not a dynamic symbol, there's
6833 nothing else to do unless it is a forced local symbol. */
6834 if (strip
6835 && h->dynindx == -1
6836 && !h->forced_local)
6837 return TRUE;
6838
6839 sym.st_value = 0;
6840 sym.st_size = h->size;
6841 sym.st_other = h->other;
6842 if (h->forced_local)
6843 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6844 else if (h->root.type == bfd_link_hash_undefweak
6845 || h->root.type == bfd_link_hash_defweak)
6846 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6847 else
6848 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6849
6850 switch (h->root.type)
6851 {
6852 default:
6853 case bfd_link_hash_new:
6854 case bfd_link_hash_warning:
6855 abort ();
6856 return FALSE;
6857
6858 case bfd_link_hash_undefined:
6859 case bfd_link_hash_undefweak:
6860 input_sec = bfd_und_section_ptr;
6861 sym.st_shndx = SHN_UNDEF;
6862 break;
6863
6864 case bfd_link_hash_defined:
6865 case bfd_link_hash_defweak:
6866 {
6867 input_sec = h->root.u.def.section;
6868 if (input_sec->output_section != NULL)
6869 {
6870 sym.st_shndx =
6871 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6872 input_sec->output_section);
6873 if (sym.st_shndx == SHN_BAD)
6874 {
6875 (*_bfd_error_handler)
6876 (_("%B: could not find output section %A for input section %A"),
6877 finfo->output_bfd, input_sec->output_section, input_sec);
6878 eoinfo->failed = TRUE;
6879 return FALSE;
6880 }
6881
6882 /* ELF symbols in relocatable files are section relative,
6883 but in nonrelocatable files they are virtual
6884 addresses. */
6885 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6886 if (! finfo->info->relocatable)
6887 {
6888 sym.st_value += input_sec->output_section->vma;
6889 if (h->type == STT_TLS)
6890 {
6891 /* STT_TLS symbols are relative to PT_TLS segment
6892 base. */
6893 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6894 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6895 }
6896 }
6897 }
6898 else
6899 {
6900 BFD_ASSERT (input_sec->owner == NULL
6901 || (input_sec->owner->flags & DYNAMIC) != 0);
6902 sym.st_shndx = SHN_UNDEF;
6903 input_sec = bfd_und_section_ptr;
6904 }
6905 }
6906 break;
6907
6908 case bfd_link_hash_common:
6909 input_sec = h->root.u.c.p->section;
6910 sym.st_shndx = bed->common_section_index (input_sec);
6911 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6912 break;
6913
6914 case bfd_link_hash_indirect:
6915 /* These symbols are created by symbol versioning. They point
6916 to the decorated version of the name. For example, if the
6917 symbol foo@@GNU_1.2 is the default, which should be used when
6918 foo is used with no version, then we add an indirect symbol
6919 foo which points to foo@@GNU_1.2. We ignore these symbols,
6920 since the indirected symbol is already in the hash table. */
6921 return TRUE;
6922 }
6923
6924 /* Give the processor backend a chance to tweak the symbol value,
6925 and also to finish up anything that needs to be done for this
6926 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6927 forced local syms when non-shared is due to a historical quirk. */
6928 if ((h->dynindx != -1
6929 || h->forced_local)
6930 && ((finfo->info->shared
6931 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6932 || h->root.type != bfd_link_hash_undefweak))
6933 || !h->forced_local)
6934 && elf_hash_table (finfo->info)->dynamic_sections_created)
6935 {
6936 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6937 (finfo->output_bfd, finfo->info, h, &sym)))
6938 {
6939 eoinfo->failed = TRUE;
6940 return FALSE;
6941 }
6942 }
6943
6944 /* If we are marking the symbol as undefined, and there are no
6945 non-weak references to this symbol from a regular object, then
6946 mark the symbol as weak undefined; if there are non-weak
6947 references, mark the symbol as strong. We can't do this earlier,
6948 because it might not be marked as undefined until the
6949 finish_dynamic_symbol routine gets through with it. */
6950 if (sym.st_shndx == SHN_UNDEF
6951 && h->ref_regular
6952 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6953 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6954 {
6955 int bindtype;
6956
6957 if (h->ref_regular_nonweak)
6958 bindtype = STB_GLOBAL;
6959 else
6960 bindtype = STB_WEAK;
6961 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6962 }
6963
6964 /* If a non-weak symbol with non-default visibility is not defined
6965 locally, it is a fatal error. */
6966 if (! finfo->info->relocatable
6967 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6968 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6969 && h->root.type == bfd_link_hash_undefined
6970 && !h->def_regular)
6971 {
6972 (*_bfd_error_handler)
6973 (_("%B: %s symbol `%s' isn't defined"),
6974 finfo->output_bfd,
6975 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6976 ? "protected"
6977 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6978 ? "internal" : "hidden",
6979 h->root.root.string);
6980 eoinfo->failed = TRUE;
6981 return FALSE;
6982 }
6983
6984 /* If this symbol should be put in the .dynsym section, then put it
6985 there now. We already know the symbol index. We also fill in
6986 the entry in the .hash section. */
6987 if (h->dynindx != -1
6988 && elf_hash_table (finfo->info)->dynamic_sections_created)
6989 {
6990 size_t bucketcount;
6991 size_t bucket;
6992 bfd_byte *esym;
6993
6994 sym.st_name = h->dynstr_index;
6995 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6996 if (! check_dynsym (finfo->output_bfd, &sym))
6997 {
6998 eoinfo->failed = TRUE;
6999 return FALSE;
7000 }
7001 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
7002
7003 bucketcount = elf_hash_table (finfo->info)->bucketcount;
7004 bucket = h->u.elf_hash_value % bucketcount;
7005
7006 if (finfo->hash_sec != NULL)
7007 {
7008 size_t hash_entry_size;
7009 bfd_byte *bucketpos;
7010 bfd_vma chain;
7011
7012 hash_entry_size
7013 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
7014 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
7015 + (bucket + 2) * hash_entry_size);
7016 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
7017 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
7018 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
7019 ((bfd_byte *) finfo->hash_sec->contents
7020 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
7021 }
7022
7023 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
7024 {
7025 Elf_Internal_Versym iversym;
7026 Elf_External_Versym *eversym;
7027
7028 if (!h->def_regular)
7029 {
7030 if (h->verinfo.verdef == NULL)
7031 iversym.vs_vers = 0;
7032 else
7033 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
7034 }
7035 else
7036 {
7037 if (h->verinfo.vertree == NULL)
7038 iversym.vs_vers = 1;
7039 else
7040 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
7041 if (finfo->info->create_default_symver)
7042 iversym.vs_vers++;
7043 }
7044
7045 if (h->hidden)
7046 iversym.vs_vers |= VERSYM_HIDDEN;
7047
7048 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
7049 eversym += h->dynindx;
7050 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
7051 }
7052 }
7053
7054 /* If we're stripping it, then it was just a dynamic symbol, and
7055 there's nothing else to do. */
7056 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
7057 return TRUE;
7058
7059 h->indx = bfd_get_symcount (finfo->output_bfd);
7060
7061 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
7062 {
7063 eoinfo->failed = TRUE;
7064 return FALSE;
7065 }
7066
7067 return TRUE;
7068 }
7069
7070 /* Return TRUE if special handling is done for relocs in SEC against
7071 symbols defined in discarded sections. */
7072
7073 static bfd_boolean
7074 elf_section_ignore_discarded_relocs (asection *sec)
7075 {
7076 const struct elf_backend_data *bed;
7077
7078 switch (sec->sec_info_type)
7079 {
7080 case ELF_INFO_TYPE_STABS:
7081 case ELF_INFO_TYPE_EH_FRAME:
7082 return TRUE;
7083 default:
7084 break;
7085 }
7086
7087 bed = get_elf_backend_data (sec->owner);
7088 if (bed->elf_backend_ignore_discarded_relocs != NULL
7089 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
7090 return TRUE;
7091
7092 return FALSE;
7093 }
7094
7095 /* Return a mask saying how ld should treat relocations in SEC against
7096 symbols defined in discarded sections. If this function returns
7097 COMPLAIN set, ld will issue a warning message. If this function
7098 returns PRETEND set, and the discarded section was link-once and the
7099 same size as the kept link-once section, ld will pretend that the
7100 symbol was actually defined in the kept section. Otherwise ld will
7101 zero the reloc (at least that is the intent, but some cooperation by
7102 the target dependent code is needed, particularly for REL targets). */
7103
7104 unsigned int
7105 _bfd_elf_default_action_discarded (asection *sec)
7106 {
7107 if (sec->flags & SEC_DEBUGGING)
7108 return PRETEND;
7109
7110 if (strcmp (".eh_frame", sec->name) == 0)
7111 return 0;
7112
7113 if (strcmp (".gcc_except_table", sec->name) == 0)
7114 return 0;
7115
7116 return COMPLAIN | PRETEND;
7117 }
7118
7119 /* Find a match between a section and a member of a section group. */
7120
7121 static asection *
7122 match_group_member (asection *sec, asection *group)
7123 {
7124 asection *first = elf_next_in_group (group);
7125 asection *s = first;
7126
7127 while (s != NULL)
7128 {
7129 if (bfd_elf_match_symbols_in_sections (s, sec))
7130 return s;
7131
7132 s = elf_next_in_group (s);
7133 if (s == first)
7134 break;
7135 }
7136
7137 return NULL;
7138 }
7139
7140 /* Check if the kept section of a discarded section SEC can be used
7141 to replace it. Return the replacement if it is OK. Otherwise return
7142 NULL. */
7143
7144 asection *
7145 _bfd_elf_check_kept_section (asection *sec)
7146 {
7147 asection *kept;
7148
7149 kept = sec->kept_section;
7150 if (kept != NULL)
7151 {
7152 if (elf_sec_group (sec) != NULL)
7153 kept = match_group_member (sec, kept);
7154 if (kept != NULL && sec->size != kept->size)
7155 kept = NULL;
7156 }
7157 return kept;
7158 }
7159
7160 /* Link an input file into the linker output file. This function
7161 handles all the sections and relocations of the input file at once.
7162 This is so that we only have to read the local symbols once, and
7163 don't have to keep them in memory. */
7164
7165 static bfd_boolean
7166 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
7167 {
7168 bfd_boolean (*relocate_section)
7169 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
7170 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
7171 bfd *output_bfd;
7172 Elf_Internal_Shdr *symtab_hdr;
7173 size_t locsymcount;
7174 size_t extsymoff;
7175 Elf_Internal_Sym *isymbuf;
7176 Elf_Internal_Sym *isym;
7177 Elf_Internal_Sym *isymend;
7178 long *pindex;
7179 asection **ppsection;
7180 asection *o;
7181 const struct elf_backend_data *bed;
7182 bfd_boolean emit_relocs;
7183 struct elf_link_hash_entry **sym_hashes;
7184
7185 output_bfd = finfo->output_bfd;
7186 bed = get_elf_backend_data (output_bfd);
7187 relocate_section = bed->elf_backend_relocate_section;
7188
7189 /* If this is a dynamic object, we don't want to do anything here:
7190 we don't want the local symbols, and we don't want the section
7191 contents. */
7192 if ((input_bfd->flags & DYNAMIC) != 0)
7193 return TRUE;
7194
7195 emit_relocs = (finfo->info->relocatable
7196 || finfo->info->emitrelocations);
7197
7198 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7199 if (elf_bad_symtab (input_bfd))
7200 {
7201 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
7202 extsymoff = 0;
7203 }
7204 else
7205 {
7206 locsymcount = symtab_hdr->sh_info;
7207 extsymoff = symtab_hdr->sh_info;
7208 }
7209
7210 /* Read the local symbols. */
7211 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7212 if (isymbuf == NULL && locsymcount != 0)
7213 {
7214 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
7215 finfo->internal_syms,
7216 finfo->external_syms,
7217 finfo->locsym_shndx);
7218 if (isymbuf == NULL)
7219 return FALSE;
7220 }
7221
7222 /* Find local symbol sections and adjust values of symbols in
7223 SEC_MERGE sections. Write out those local symbols we know are
7224 going into the output file. */
7225 isymend = isymbuf + locsymcount;
7226 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
7227 isym < isymend;
7228 isym++, pindex++, ppsection++)
7229 {
7230 asection *isec;
7231 const char *name;
7232 Elf_Internal_Sym osym;
7233
7234 *pindex = -1;
7235
7236 if (elf_bad_symtab (input_bfd))
7237 {
7238 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
7239 {
7240 *ppsection = NULL;
7241 continue;
7242 }
7243 }
7244
7245 if (isym->st_shndx == SHN_UNDEF)
7246 isec = bfd_und_section_ptr;
7247 else if (isym->st_shndx < SHN_LORESERVE
7248 || isym->st_shndx > SHN_HIRESERVE)
7249 {
7250 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
7251 if (isec
7252 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
7253 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
7254 isym->st_value =
7255 _bfd_merged_section_offset (output_bfd, &isec,
7256 elf_section_data (isec)->sec_info,
7257 isym->st_value);
7258 }
7259 else if (isym->st_shndx == SHN_ABS)
7260 isec = bfd_abs_section_ptr;
7261 else if (isym->st_shndx == SHN_COMMON)
7262 isec = bfd_com_section_ptr;
7263 else
7264 {
7265 /* Don't attempt to output symbols with st_shnx in the
7266 reserved range other than SHN_ABS and SHN_COMMON. */
7267 *ppsection = NULL;
7268 continue;
7269 }
7270
7271 *ppsection = isec;
7272
7273 /* Don't output the first, undefined, symbol. */
7274 if (ppsection == finfo->sections)
7275 continue;
7276
7277 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
7278 {
7279 /* We never output section symbols. Instead, we use the
7280 section symbol of the corresponding section in the output
7281 file. */
7282 continue;
7283 }
7284
7285 /* If we are stripping all symbols, we don't want to output this
7286 one. */
7287 if (finfo->info->strip == strip_all)
7288 continue;
7289
7290 /* If we are discarding all local symbols, we don't want to
7291 output this one. If we are generating a relocatable output
7292 file, then some of the local symbols may be required by
7293 relocs; we output them below as we discover that they are
7294 needed. */
7295 if (finfo->info->discard == discard_all)
7296 continue;
7297
7298 /* If this symbol is defined in a section which we are
7299 discarding, we don't need to keep it. */
7300 if (isym->st_shndx != SHN_UNDEF
7301 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
7302 && (isec == NULL
7303 || bfd_section_removed_from_list (output_bfd,
7304 isec->output_section)))
7305 continue;
7306
7307 /* Get the name of the symbol. */
7308 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
7309 isym->st_name);
7310 if (name == NULL)
7311 return FALSE;
7312
7313 /* See if we are discarding symbols with this name. */
7314 if ((finfo->info->strip == strip_some
7315 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
7316 == NULL))
7317 || (((finfo->info->discard == discard_sec_merge
7318 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
7319 || finfo->info->discard == discard_l)
7320 && bfd_is_local_label_name (input_bfd, name)))
7321 continue;
7322
7323 /* If we get here, we are going to output this symbol. */
7324
7325 osym = *isym;
7326
7327 /* Adjust the section index for the output file. */
7328 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
7329 isec->output_section);
7330 if (osym.st_shndx == SHN_BAD)
7331 return FALSE;
7332
7333 *pindex = bfd_get_symcount (output_bfd);
7334
7335 /* ELF symbols in relocatable files are section relative, but
7336 in executable files they are virtual addresses. Note that
7337 this code assumes that all ELF sections have an associated
7338 BFD section with a reasonable value for output_offset; below
7339 we assume that they also have a reasonable value for
7340 output_section. Any special sections must be set up to meet
7341 these requirements. */
7342 osym.st_value += isec->output_offset;
7343 if (! finfo->info->relocatable)
7344 {
7345 osym.st_value += isec->output_section->vma;
7346 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
7347 {
7348 /* STT_TLS symbols are relative to PT_TLS segment base. */
7349 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7350 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7351 }
7352 }
7353
7354 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
7355 return FALSE;
7356 }
7357
7358 /* Relocate the contents of each section. */
7359 sym_hashes = elf_sym_hashes (input_bfd);
7360 for (o = input_bfd->sections; o != NULL; o = o->next)
7361 {
7362 bfd_byte *contents;
7363
7364 if (! o->linker_mark)
7365 {
7366 /* This section was omitted from the link. */
7367 continue;
7368 }
7369
7370 if ((o->flags & SEC_HAS_CONTENTS) == 0
7371 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
7372 continue;
7373
7374 if ((o->flags & SEC_LINKER_CREATED) != 0)
7375 {
7376 /* Section was created by _bfd_elf_link_create_dynamic_sections
7377 or somesuch. */
7378 continue;
7379 }
7380
7381 /* Get the contents of the section. They have been cached by a
7382 relaxation routine. Note that o is a section in an input
7383 file, so the contents field will not have been set by any of
7384 the routines which work on output files. */
7385 if (elf_section_data (o)->this_hdr.contents != NULL)
7386 contents = elf_section_data (o)->this_hdr.contents;
7387 else
7388 {
7389 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7390
7391 contents = finfo->contents;
7392 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7393 return FALSE;
7394 }
7395
7396 if ((o->flags & SEC_RELOC) != 0)
7397 {
7398 Elf_Internal_Rela *internal_relocs;
7399 bfd_vma r_type_mask;
7400 int r_sym_shift;
7401
7402 /* Get the swapped relocs. */
7403 internal_relocs
7404 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7405 finfo->internal_relocs, FALSE);
7406 if (internal_relocs == NULL
7407 && o->reloc_count > 0)
7408 return FALSE;
7409
7410 if (bed->s->arch_size == 32)
7411 {
7412 r_type_mask = 0xff;
7413 r_sym_shift = 8;
7414 }
7415 else
7416 {
7417 r_type_mask = 0xffffffff;
7418 r_sym_shift = 32;
7419 }
7420
7421 /* Run through the relocs looking for any against symbols
7422 from discarded sections and section symbols from
7423 removed link-once sections. Complain about relocs
7424 against discarded sections. Zero relocs against removed
7425 link-once sections. */
7426 if (!elf_section_ignore_discarded_relocs (o))
7427 {
7428 Elf_Internal_Rela *rel, *relend;
7429 unsigned int action = (*bed->action_discarded) (o);
7430
7431 rel = internal_relocs;
7432 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7433 for ( ; rel < relend; rel++)
7434 {
7435 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7436 asection **ps, *sec;
7437 struct elf_link_hash_entry *h = NULL;
7438 const char *sym_name;
7439
7440 if (r_symndx == STN_UNDEF)
7441 continue;
7442
7443 if (r_symndx >= locsymcount
7444 || (elf_bad_symtab (input_bfd)
7445 && finfo->sections[r_symndx] == NULL))
7446 {
7447 h = sym_hashes[r_symndx - extsymoff];
7448
7449 /* Badly formatted input files can contain relocs that
7450 reference non-existant symbols. Check here so that
7451 we do not seg fault. */
7452 if (h == NULL)
7453 {
7454 char buffer [32];
7455
7456 sprintf_vma (buffer, rel->r_info);
7457 (*_bfd_error_handler)
7458 (_("error: %B contains a reloc (0x%s) for section %A "
7459 "that references a non-existent global symbol"),
7460 input_bfd, o, buffer);
7461 bfd_set_error (bfd_error_bad_value);
7462 return FALSE;
7463 }
7464
7465 while (h->root.type == bfd_link_hash_indirect
7466 || h->root.type == bfd_link_hash_warning)
7467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7468
7469 if (h->root.type != bfd_link_hash_defined
7470 && h->root.type != bfd_link_hash_defweak)
7471 continue;
7472
7473 ps = &h->root.u.def.section;
7474 sym_name = h->root.root.string;
7475 }
7476 else
7477 {
7478 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7479 ps = &finfo->sections[r_symndx];
7480 sym_name = bfd_elf_sym_name (input_bfd,
7481 symtab_hdr,
7482 sym, *ps);
7483 }
7484
7485 /* Complain if the definition comes from a
7486 discarded section. */
7487 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7488 {
7489 BFD_ASSERT (r_symndx != 0);
7490 if (action & COMPLAIN)
7491 (*finfo->info->callbacks->einfo)
7492 (_("%X`%s' referenced in section `%A' of %B: "
7493 "defined in discarded section `%A' of %B\n"),
7494 sym_name, o, input_bfd, sec, sec->owner);
7495
7496 /* Try to do the best we can to support buggy old
7497 versions of gcc. Pretend that the symbol is
7498 really defined in the kept linkonce section.
7499 FIXME: This is quite broken. Modifying the
7500 symbol here means we will be changing all later
7501 uses of the symbol, not just in this section. */
7502 if (action & PRETEND)
7503 {
7504 asection *kept;
7505
7506 kept = _bfd_elf_check_kept_section (sec);
7507 if (kept != NULL)
7508 {
7509 *ps = kept;
7510 continue;
7511 }
7512 }
7513
7514 /* Remove the symbol reference from the reloc, but
7515 don't kill the reloc completely. This is so that
7516 a zero value will be written into the section,
7517 which may have non-zero contents put there by the
7518 assembler. Zero in things like an eh_frame fde
7519 pc_begin allows stack unwinders to recognize the
7520 fde as bogus. */
7521 rel->r_info &= r_type_mask;
7522 rel->r_addend = 0;
7523 }
7524 }
7525 }
7526
7527 /* Relocate the section by invoking a back end routine.
7528
7529 The back end routine is responsible for adjusting the
7530 section contents as necessary, and (if using Rela relocs
7531 and generating a relocatable output file) adjusting the
7532 reloc addend as necessary.
7533
7534 The back end routine does not have to worry about setting
7535 the reloc address or the reloc symbol index.
7536
7537 The back end routine is given a pointer to the swapped in
7538 internal symbols, and can access the hash table entries
7539 for the external symbols via elf_sym_hashes (input_bfd).
7540
7541 When generating relocatable output, the back end routine
7542 must handle STB_LOCAL/STT_SECTION symbols specially. The
7543 output symbol is going to be a section symbol
7544 corresponding to the output section, which will require
7545 the addend to be adjusted. */
7546
7547 if (! (*relocate_section) (output_bfd, finfo->info,
7548 input_bfd, o, contents,
7549 internal_relocs,
7550 isymbuf,
7551 finfo->sections))
7552 return FALSE;
7553
7554 if (emit_relocs)
7555 {
7556 Elf_Internal_Rela *irela;
7557 Elf_Internal_Rela *irelaend;
7558 bfd_vma last_offset;
7559 struct elf_link_hash_entry **rel_hash;
7560 struct elf_link_hash_entry **rel_hash_list;
7561 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7562 unsigned int next_erel;
7563 bfd_boolean rela_normal;
7564
7565 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7566 rela_normal = (bed->rela_normal
7567 && (input_rel_hdr->sh_entsize
7568 == bed->s->sizeof_rela));
7569
7570 /* Adjust the reloc addresses and symbol indices. */
7571
7572 irela = internal_relocs;
7573 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7574 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7575 + elf_section_data (o->output_section)->rel_count
7576 + elf_section_data (o->output_section)->rel_count2);
7577 rel_hash_list = rel_hash;
7578 last_offset = o->output_offset;
7579 if (!finfo->info->relocatable)
7580 last_offset += o->output_section->vma;
7581 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7582 {
7583 unsigned long r_symndx;
7584 asection *sec;
7585 Elf_Internal_Sym sym;
7586
7587 if (next_erel == bed->s->int_rels_per_ext_rel)
7588 {
7589 rel_hash++;
7590 next_erel = 0;
7591 }
7592
7593 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7594 finfo->info, o,
7595 irela->r_offset);
7596 if (irela->r_offset >= (bfd_vma) -2)
7597 {
7598 /* This is a reloc for a deleted entry or somesuch.
7599 Turn it into an R_*_NONE reloc, at the same
7600 offset as the last reloc. elf_eh_frame.c and
7601 bfd_elf_discard_info rely on reloc offsets
7602 being ordered. */
7603 irela->r_offset = last_offset;
7604 irela->r_info = 0;
7605 irela->r_addend = 0;
7606 continue;
7607 }
7608
7609 irela->r_offset += o->output_offset;
7610
7611 /* Relocs in an executable have to be virtual addresses. */
7612 if (!finfo->info->relocatable)
7613 irela->r_offset += o->output_section->vma;
7614
7615 last_offset = irela->r_offset;
7616
7617 r_symndx = irela->r_info >> r_sym_shift;
7618 if (r_symndx == STN_UNDEF)
7619 continue;
7620
7621 if (r_symndx >= locsymcount
7622 || (elf_bad_symtab (input_bfd)
7623 && finfo->sections[r_symndx] == NULL))
7624 {
7625 struct elf_link_hash_entry *rh;
7626 unsigned long indx;
7627
7628 /* This is a reloc against a global symbol. We
7629 have not yet output all the local symbols, so
7630 we do not know the symbol index of any global
7631 symbol. We set the rel_hash entry for this
7632 reloc to point to the global hash table entry
7633 for this symbol. The symbol index is then
7634 set at the end of bfd_elf_final_link. */
7635 indx = r_symndx - extsymoff;
7636 rh = elf_sym_hashes (input_bfd)[indx];
7637 while (rh->root.type == bfd_link_hash_indirect
7638 || rh->root.type == bfd_link_hash_warning)
7639 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7640
7641 /* Setting the index to -2 tells
7642 elf_link_output_extsym that this symbol is
7643 used by a reloc. */
7644 BFD_ASSERT (rh->indx < 0);
7645 rh->indx = -2;
7646
7647 *rel_hash = rh;
7648
7649 continue;
7650 }
7651
7652 /* This is a reloc against a local symbol. */
7653
7654 *rel_hash = NULL;
7655 sym = isymbuf[r_symndx];
7656 sec = finfo->sections[r_symndx];
7657 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7658 {
7659 /* I suppose the backend ought to fill in the
7660 section of any STT_SECTION symbol against a
7661 processor specific section. */
7662 r_symndx = 0;
7663 if (bfd_is_abs_section (sec))
7664 ;
7665 else if (sec == NULL || sec->owner == NULL)
7666 {
7667 bfd_set_error (bfd_error_bad_value);
7668 return FALSE;
7669 }
7670 else
7671 {
7672 asection *osec = sec->output_section;
7673
7674 /* If we have discarded a section, the output
7675 section will be the absolute section. In
7676 case of discarded link-once and discarded
7677 SEC_MERGE sections, use the kept section. */
7678 if (bfd_is_abs_section (osec)
7679 && sec->kept_section != NULL
7680 && sec->kept_section->output_section != NULL)
7681 {
7682 osec = sec->kept_section->output_section;
7683 irela->r_addend -= osec->vma;
7684 }
7685
7686 if (!bfd_is_abs_section (osec))
7687 {
7688 r_symndx = osec->target_index;
7689 BFD_ASSERT (r_symndx != 0);
7690 }
7691 }
7692
7693 /* Adjust the addend according to where the
7694 section winds up in the output section. */
7695 if (rela_normal)
7696 irela->r_addend += sec->output_offset;
7697 }
7698 else
7699 {
7700 if (finfo->indices[r_symndx] == -1)
7701 {
7702 unsigned long shlink;
7703 const char *name;
7704 asection *osec;
7705
7706 if (finfo->info->strip == strip_all)
7707 {
7708 /* You can't do ld -r -s. */
7709 bfd_set_error (bfd_error_invalid_operation);
7710 return FALSE;
7711 }
7712
7713 /* This symbol was skipped earlier, but
7714 since it is needed by a reloc, we
7715 must output it now. */
7716 shlink = symtab_hdr->sh_link;
7717 name = (bfd_elf_string_from_elf_section
7718 (input_bfd, shlink, sym.st_name));
7719 if (name == NULL)
7720 return FALSE;
7721
7722 osec = sec->output_section;
7723 sym.st_shndx =
7724 _bfd_elf_section_from_bfd_section (output_bfd,
7725 osec);
7726 if (sym.st_shndx == SHN_BAD)
7727 return FALSE;
7728
7729 sym.st_value += sec->output_offset;
7730 if (! finfo->info->relocatable)
7731 {
7732 sym.st_value += osec->vma;
7733 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7734 {
7735 /* STT_TLS symbols are relative to PT_TLS
7736 segment base. */
7737 BFD_ASSERT (elf_hash_table (finfo->info)
7738 ->tls_sec != NULL);
7739 sym.st_value -= (elf_hash_table (finfo->info)
7740 ->tls_sec->vma);
7741 }
7742 }
7743
7744 finfo->indices[r_symndx]
7745 = bfd_get_symcount (output_bfd);
7746
7747 if (! elf_link_output_sym (finfo, name, &sym, sec,
7748 NULL))
7749 return FALSE;
7750 }
7751
7752 r_symndx = finfo->indices[r_symndx];
7753 }
7754
7755 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7756 | (irela->r_info & r_type_mask));
7757 }
7758
7759 /* Swap out the relocs. */
7760 if (input_rel_hdr->sh_size != 0
7761 && !bed->elf_backend_emit_relocs (output_bfd, o,
7762 input_rel_hdr,
7763 internal_relocs,
7764 rel_hash_list))
7765 return FALSE;
7766
7767 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7768 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7769 {
7770 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7771 * bed->s->int_rels_per_ext_rel);
7772 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7773 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7774 input_rel_hdr2,
7775 internal_relocs,
7776 rel_hash_list))
7777 return FALSE;
7778 }
7779 }
7780 }
7781
7782 /* Write out the modified section contents. */
7783 if (bed->elf_backend_write_section
7784 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7785 {
7786 /* Section written out. */
7787 }
7788 else switch (o->sec_info_type)
7789 {
7790 case ELF_INFO_TYPE_STABS:
7791 if (! (_bfd_write_section_stabs
7792 (output_bfd,
7793 &elf_hash_table (finfo->info)->stab_info,
7794 o, &elf_section_data (o)->sec_info, contents)))
7795 return FALSE;
7796 break;
7797 case ELF_INFO_TYPE_MERGE:
7798 if (! _bfd_write_merged_section (output_bfd, o,
7799 elf_section_data (o)->sec_info))
7800 return FALSE;
7801 break;
7802 case ELF_INFO_TYPE_EH_FRAME:
7803 {
7804 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7805 o, contents))
7806 return FALSE;
7807 }
7808 break;
7809 default:
7810 {
7811 if (! (o->flags & SEC_EXCLUDE)
7812 && ! bfd_set_section_contents (output_bfd, o->output_section,
7813 contents,
7814 (file_ptr) o->output_offset,
7815 o->size))
7816 return FALSE;
7817 }
7818 break;
7819 }
7820 }
7821
7822 return TRUE;
7823 }
7824
7825 /* Generate a reloc when linking an ELF file. This is a reloc
7826 requested by the linker, and does not come from any input file. This
7827 is used to build constructor and destructor tables when linking
7828 with -Ur. */
7829
7830 static bfd_boolean
7831 elf_reloc_link_order (bfd *output_bfd,
7832 struct bfd_link_info *info,
7833 asection *output_section,
7834 struct bfd_link_order *link_order)
7835 {
7836 reloc_howto_type *howto;
7837 long indx;
7838 bfd_vma offset;
7839 bfd_vma addend;
7840 struct elf_link_hash_entry **rel_hash_ptr;
7841 Elf_Internal_Shdr *rel_hdr;
7842 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7843 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7844 bfd_byte *erel;
7845 unsigned int i;
7846
7847 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7848 if (howto == NULL)
7849 {
7850 bfd_set_error (bfd_error_bad_value);
7851 return FALSE;
7852 }
7853
7854 addend = link_order->u.reloc.p->addend;
7855
7856 /* Figure out the symbol index. */
7857 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7858 + elf_section_data (output_section)->rel_count
7859 + elf_section_data (output_section)->rel_count2);
7860 if (link_order->type == bfd_section_reloc_link_order)
7861 {
7862 indx = link_order->u.reloc.p->u.section->target_index;
7863 BFD_ASSERT (indx != 0);
7864 *rel_hash_ptr = NULL;
7865 }
7866 else
7867 {
7868 struct elf_link_hash_entry *h;
7869
7870 /* Treat a reloc against a defined symbol as though it were
7871 actually against the section. */
7872 h = ((struct elf_link_hash_entry *)
7873 bfd_wrapped_link_hash_lookup (output_bfd, info,
7874 link_order->u.reloc.p->u.name,
7875 FALSE, FALSE, TRUE));
7876 if (h != NULL
7877 && (h->root.type == bfd_link_hash_defined
7878 || h->root.type == bfd_link_hash_defweak))
7879 {
7880 asection *section;
7881
7882 section = h->root.u.def.section;
7883 indx = section->output_section->target_index;
7884 *rel_hash_ptr = NULL;
7885 /* It seems that we ought to add the symbol value to the
7886 addend here, but in practice it has already been added
7887 because it was passed to constructor_callback. */
7888 addend += section->output_section->vma + section->output_offset;
7889 }
7890 else if (h != NULL)
7891 {
7892 /* Setting the index to -2 tells elf_link_output_extsym that
7893 this symbol is used by a reloc. */
7894 h->indx = -2;
7895 *rel_hash_ptr = h;
7896 indx = 0;
7897 }
7898 else
7899 {
7900 if (! ((*info->callbacks->unattached_reloc)
7901 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7902 return FALSE;
7903 indx = 0;
7904 }
7905 }
7906
7907 /* If this is an inplace reloc, we must write the addend into the
7908 object file. */
7909 if (howto->partial_inplace && addend != 0)
7910 {
7911 bfd_size_type size;
7912 bfd_reloc_status_type rstat;
7913 bfd_byte *buf;
7914 bfd_boolean ok;
7915 const char *sym_name;
7916
7917 size = bfd_get_reloc_size (howto);
7918 buf = bfd_zmalloc (size);
7919 if (buf == NULL)
7920 return FALSE;
7921 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7922 switch (rstat)
7923 {
7924 case bfd_reloc_ok:
7925 break;
7926
7927 default:
7928 case bfd_reloc_outofrange:
7929 abort ();
7930
7931 case bfd_reloc_overflow:
7932 if (link_order->type == bfd_section_reloc_link_order)
7933 sym_name = bfd_section_name (output_bfd,
7934 link_order->u.reloc.p->u.section);
7935 else
7936 sym_name = link_order->u.reloc.p->u.name;
7937 if (! ((*info->callbacks->reloc_overflow)
7938 (info, NULL, sym_name, howto->name, addend, NULL,
7939 NULL, (bfd_vma) 0)))
7940 {
7941 free (buf);
7942 return FALSE;
7943 }
7944 break;
7945 }
7946 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7947 link_order->offset, size);
7948 free (buf);
7949 if (! ok)
7950 return FALSE;
7951 }
7952
7953 /* The address of a reloc is relative to the section in a
7954 relocatable file, and is a virtual address in an executable
7955 file. */
7956 offset = link_order->offset;
7957 if (! info->relocatable)
7958 offset += output_section->vma;
7959
7960 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7961 {
7962 irel[i].r_offset = offset;
7963 irel[i].r_info = 0;
7964 irel[i].r_addend = 0;
7965 }
7966 if (bed->s->arch_size == 32)
7967 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7968 else
7969 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7970
7971 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7972 erel = rel_hdr->contents;
7973 if (rel_hdr->sh_type == SHT_REL)
7974 {
7975 erel += (elf_section_data (output_section)->rel_count
7976 * bed->s->sizeof_rel);
7977 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7978 }
7979 else
7980 {
7981 irel[0].r_addend = addend;
7982 erel += (elf_section_data (output_section)->rel_count
7983 * bed->s->sizeof_rela);
7984 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7985 }
7986
7987 ++elf_section_data (output_section)->rel_count;
7988
7989 return TRUE;
7990 }
7991
7992
7993 /* Get the output vma of the section pointed to by the sh_link field. */
7994
7995 static bfd_vma
7996 elf_get_linked_section_vma (struct bfd_link_order *p)
7997 {
7998 Elf_Internal_Shdr **elf_shdrp;
7999 asection *s;
8000 int elfsec;
8001
8002 s = p->u.indirect.section;
8003 elf_shdrp = elf_elfsections (s->owner);
8004 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
8005 elfsec = elf_shdrp[elfsec]->sh_link;
8006 /* PR 290:
8007 The Intel C compiler generates SHT_IA_64_UNWIND with
8008 SHF_LINK_ORDER. But it doesn't set the sh_link or
8009 sh_info fields. Hence we could get the situation
8010 where elfsec is 0. */
8011 if (elfsec == 0)
8012 {
8013 const struct elf_backend_data *bed
8014 = get_elf_backend_data (s->owner);
8015 if (bed->link_order_error_handler)
8016 bed->link_order_error_handler
8017 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
8018 return 0;
8019 }
8020 else
8021 {
8022 s = elf_shdrp[elfsec]->bfd_section;
8023 return s->output_section->vma + s->output_offset;
8024 }
8025 }
8026
8027
8028 /* Compare two sections based on the locations of the sections they are
8029 linked to. Used by elf_fixup_link_order. */
8030
8031 static int
8032 compare_link_order (const void * a, const void * b)
8033 {
8034 bfd_vma apos;
8035 bfd_vma bpos;
8036
8037 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
8038 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
8039 if (apos < bpos)
8040 return -1;
8041 return apos > bpos;
8042 }
8043
8044
8045 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
8046 order as their linked sections. Returns false if this could not be done
8047 because an output section includes both ordered and unordered
8048 sections. Ideally we'd do this in the linker proper. */
8049
8050 static bfd_boolean
8051 elf_fixup_link_order (bfd *abfd, asection *o)
8052 {
8053 int seen_linkorder;
8054 int seen_other;
8055 int n;
8056 struct bfd_link_order *p;
8057 bfd *sub;
8058 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8059 unsigned elfsec;
8060 struct bfd_link_order **sections;
8061 asection *s, *other_sec, *linkorder_sec;
8062 bfd_vma offset;
8063
8064 other_sec = NULL;
8065 linkorder_sec = NULL;
8066 seen_other = 0;
8067 seen_linkorder = 0;
8068 for (p = o->map_head.link_order; p != NULL; p = p->next)
8069 {
8070 if (p->type == bfd_indirect_link_order)
8071 {
8072 s = p->u.indirect.section;
8073 sub = s->owner;
8074 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
8075 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
8076 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
8077 && elfsec < elf_numsections (sub)
8078 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
8079 {
8080 seen_linkorder++;
8081 linkorder_sec = s;
8082 }
8083 else
8084 {
8085 seen_other++;
8086 other_sec = s;
8087 }
8088 }
8089 else
8090 seen_other++;
8091
8092 if (seen_other && seen_linkorder)
8093 {
8094 if (other_sec && linkorder_sec)
8095 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8096 o, linkorder_sec,
8097 linkorder_sec->owner, other_sec,
8098 other_sec->owner);
8099 else
8100 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
8101 o);
8102 bfd_set_error (bfd_error_bad_value);
8103 return FALSE;
8104 }
8105 }
8106
8107 if (!seen_linkorder)
8108 return TRUE;
8109
8110 sections = (struct bfd_link_order **)
8111 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
8112 seen_linkorder = 0;
8113
8114 for (p = o->map_head.link_order; p != NULL; p = p->next)
8115 {
8116 sections[seen_linkorder++] = p;
8117 }
8118 /* Sort the input sections in the order of their linked section. */
8119 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
8120 compare_link_order);
8121
8122 /* Change the offsets of the sections. */
8123 offset = 0;
8124 for (n = 0; n < seen_linkorder; n++)
8125 {
8126 s = sections[n]->u.indirect.section;
8127 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
8128 s->output_offset = offset;
8129 sections[n]->offset = offset;
8130 offset += sections[n]->size;
8131 }
8132
8133 return TRUE;
8134 }
8135
8136
8137 /* Do the final step of an ELF link. */
8138
8139 bfd_boolean
8140 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8141 {
8142 bfd_boolean dynamic;
8143 bfd_boolean emit_relocs;
8144 bfd *dynobj;
8145 struct elf_final_link_info finfo;
8146 register asection *o;
8147 register struct bfd_link_order *p;
8148 register bfd *sub;
8149 bfd_size_type max_contents_size;
8150 bfd_size_type max_external_reloc_size;
8151 bfd_size_type max_internal_reloc_count;
8152 bfd_size_type max_sym_count;
8153 bfd_size_type max_sym_shndx_count;
8154 file_ptr off;
8155 Elf_Internal_Sym elfsym;
8156 unsigned int i;
8157 Elf_Internal_Shdr *symtab_hdr;
8158 Elf_Internal_Shdr *symtab_shndx_hdr;
8159 Elf_Internal_Shdr *symstrtab_hdr;
8160 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8161 struct elf_outext_info eoinfo;
8162 bfd_boolean merged;
8163 size_t relativecount = 0;
8164 asection *reldyn = 0;
8165 bfd_size_type amt;
8166
8167 if (! is_elf_hash_table (info->hash))
8168 return FALSE;
8169
8170 if (info->shared)
8171 abfd->flags |= DYNAMIC;
8172
8173 dynamic = elf_hash_table (info)->dynamic_sections_created;
8174 dynobj = elf_hash_table (info)->dynobj;
8175
8176 emit_relocs = (info->relocatable
8177 || info->emitrelocations);
8178
8179 finfo.info = info;
8180 finfo.output_bfd = abfd;
8181 finfo.symstrtab = _bfd_elf_stringtab_init ();
8182 if (finfo.symstrtab == NULL)
8183 return FALSE;
8184
8185 if (! dynamic)
8186 {
8187 finfo.dynsym_sec = NULL;
8188 finfo.hash_sec = NULL;
8189 finfo.symver_sec = NULL;
8190 }
8191 else
8192 {
8193 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
8194 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
8195 BFD_ASSERT (finfo.dynsym_sec != NULL);
8196 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
8197 /* Note that it is OK if symver_sec is NULL. */
8198 }
8199
8200 finfo.contents = NULL;
8201 finfo.external_relocs = NULL;
8202 finfo.internal_relocs = NULL;
8203 finfo.external_syms = NULL;
8204 finfo.locsym_shndx = NULL;
8205 finfo.internal_syms = NULL;
8206 finfo.indices = NULL;
8207 finfo.sections = NULL;
8208 finfo.symbuf = NULL;
8209 finfo.symshndxbuf = NULL;
8210 finfo.symbuf_count = 0;
8211 finfo.shndxbuf_size = 0;
8212
8213 /* Count up the number of relocations we will output for each output
8214 section, so that we know the sizes of the reloc sections. We
8215 also figure out some maximum sizes. */
8216 max_contents_size = 0;
8217 max_external_reloc_size = 0;
8218 max_internal_reloc_count = 0;
8219 max_sym_count = 0;
8220 max_sym_shndx_count = 0;
8221 merged = FALSE;
8222 for (o = abfd->sections; o != NULL; o = o->next)
8223 {
8224 struct bfd_elf_section_data *esdo = elf_section_data (o);
8225 o->reloc_count = 0;
8226
8227 for (p = o->map_head.link_order; p != NULL; p = p->next)
8228 {
8229 unsigned int reloc_count = 0;
8230 struct bfd_elf_section_data *esdi = NULL;
8231 unsigned int *rel_count1;
8232
8233 if (p->type == bfd_section_reloc_link_order
8234 || p->type == bfd_symbol_reloc_link_order)
8235 reloc_count = 1;
8236 else if (p->type == bfd_indirect_link_order)
8237 {
8238 asection *sec;
8239
8240 sec = p->u.indirect.section;
8241 esdi = elf_section_data (sec);
8242
8243 /* Mark all sections which are to be included in the
8244 link. This will normally be every section. We need
8245 to do this so that we can identify any sections which
8246 the linker has decided to not include. */
8247 sec->linker_mark = TRUE;
8248
8249 if (sec->flags & SEC_MERGE)
8250 merged = TRUE;
8251
8252 if (info->relocatable || info->emitrelocations)
8253 reloc_count = sec->reloc_count;
8254 else if (bed->elf_backend_count_relocs)
8255 {
8256 Elf_Internal_Rela * relocs;
8257
8258 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8259 info->keep_memory);
8260
8261 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
8262
8263 if (elf_section_data (o)->relocs != relocs)
8264 free (relocs);
8265 }
8266
8267 if (sec->rawsize > max_contents_size)
8268 max_contents_size = sec->rawsize;
8269 if (sec->size > max_contents_size)
8270 max_contents_size = sec->size;
8271
8272 /* We are interested in just local symbols, not all
8273 symbols. */
8274 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
8275 && (sec->owner->flags & DYNAMIC) == 0)
8276 {
8277 size_t sym_count;
8278
8279 if (elf_bad_symtab (sec->owner))
8280 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
8281 / bed->s->sizeof_sym);
8282 else
8283 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
8284
8285 if (sym_count > max_sym_count)
8286 max_sym_count = sym_count;
8287
8288 if (sym_count > max_sym_shndx_count
8289 && elf_symtab_shndx (sec->owner) != 0)
8290 max_sym_shndx_count = sym_count;
8291
8292 if ((sec->flags & SEC_RELOC) != 0)
8293 {
8294 size_t ext_size;
8295
8296 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
8297 if (ext_size > max_external_reloc_size)
8298 max_external_reloc_size = ext_size;
8299 if (sec->reloc_count > max_internal_reloc_count)
8300 max_internal_reloc_count = sec->reloc_count;
8301 }
8302 }
8303 }
8304
8305 if (reloc_count == 0)
8306 continue;
8307
8308 o->reloc_count += reloc_count;
8309
8310 /* MIPS may have a mix of REL and RELA relocs on sections.
8311 To support this curious ABI we keep reloc counts in
8312 elf_section_data too. We must be careful to add the
8313 relocations from the input section to the right output
8314 count. FIXME: Get rid of one count. We have
8315 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
8316 rel_count1 = &esdo->rel_count;
8317 if (esdi != NULL)
8318 {
8319 bfd_boolean same_size;
8320 bfd_size_type entsize1;
8321
8322 entsize1 = esdi->rel_hdr.sh_entsize;
8323 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
8324 || entsize1 == bed->s->sizeof_rela);
8325 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
8326
8327 if (!same_size)
8328 rel_count1 = &esdo->rel_count2;
8329
8330 if (esdi->rel_hdr2 != NULL)
8331 {
8332 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
8333 unsigned int alt_count;
8334 unsigned int *rel_count2;
8335
8336 BFD_ASSERT (entsize2 != entsize1
8337 && (entsize2 == bed->s->sizeof_rel
8338 || entsize2 == bed->s->sizeof_rela));
8339
8340 rel_count2 = &esdo->rel_count2;
8341 if (!same_size)
8342 rel_count2 = &esdo->rel_count;
8343
8344 /* The following is probably too simplistic if the
8345 backend counts output relocs unusually. */
8346 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
8347 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
8348 *rel_count2 += alt_count;
8349 reloc_count -= alt_count;
8350 }
8351 }
8352 *rel_count1 += reloc_count;
8353 }
8354
8355 if (o->reloc_count > 0)
8356 o->flags |= SEC_RELOC;
8357 else
8358 {
8359 /* Explicitly clear the SEC_RELOC flag. The linker tends to
8360 set it (this is probably a bug) and if it is set
8361 assign_section_numbers will create a reloc section. */
8362 o->flags &=~ SEC_RELOC;
8363 }
8364
8365 /* If the SEC_ALLOC flag is not set, force the section VMA to
8366 zero. This is done in elf_fake_sections as well, but forcing
8367 the VMA to 0 here will ensure that relocs against these
8368 sections are handled correctly. */
8369 if ((o->flags & SEC_ALLOC) == 0
8370 && ! o->user_set_vma)
8371 o->vma = 0;
8372 }
8373
8374 if (! info->relocatable && merged)
8375 elf_link_hash_traverse (elf_hash_table (info),
8376 _bfd_elf_link_sec_merge_syms, abfd);
8377
8378 /* Figure out the file positions for everything but the symbol table
8379 and the relocs. We set symcount to force assign_section_numbers
8380 to create a symbol table. */
8381 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
8382 BFD_ASSERT (! abfd->output_has_begun);
8383 if (! _bfd_elf_compute_section_file_positions (abfd, info))
8384 goto error_return;
8385
8386 /* Set sizes, and assign file positions for reloc sections. */
8387 for (o = abfd->sections; o != NULL; o = o->next)
8388 {
8389 if ((o->flags & SEC_RELOC) != 0)
8390 {
8391 if (!(_bfd_elf_link_size_reloc_section
8392 (abfd, &elf_section_data (o)->rel_hdr, o)))
8393 goto error_return;
8394
8395 if (elf_section_data (o)->rel_hdr2
8396 && !(_bfd_elf_link_size_reloc_section
8397 (abfd, elf_section_data (o)->rel_hdr2, o)))
8398 goto error_return;
8399 }
8400
8401 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8402 to count upwards while actually outputting the relocations. */
8403 elf_section_data (o)->rel_count = 0;
8404 elf_section_data (o)->rel_count2 = 0;
8405 }
8406
8407 _bfd_elf_assign_file_positions_for_relocs (abfd);
8408
8409 /* We have now assigned file positions for all the sections except
8410 .symtab and .strtab. We start the .symtab section at the current
8411 file position, and write directly to it. We build the .strtab
8412 section in memory. */
8413 bfd_get_symcount (abfd) = 0;
8414 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8415 /* sh_name is set in prep_headers. */
8416 symtab_hdr->sh_type = SHT_SYMTAB;
8417 /* sh_flags, sh_addr and sh_size all start off zero. */
8418 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8419 /* sh_link is set in assign_section_numbers. */
8420 /* sh_info is set below. */
8421 /* sh_offset is set just below. */
8422 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8423
8424 off = elf_tdata (abfd)->next_file_pos;
8425 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8426
8427 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8428 incorrect. We do not yet know the size of the .symtab section.
8429 We correct next_file_pos below, after we do know the size. */
8430
8431 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8432 continuously seeking to the right position in the file. */
8433 if (! info->keep_memory || max_sym_count < 20)
8434 finfo.symbuf_size = 20;
8435 else
8436 finfo.symbuf_size = max_sym_count;
8437 amt = finfo.symbuf_size;
8438 amt *= bed->s->sizeof_sym;
8439 finfo.symbuf = bfd_malloc (amt);
8440 if (finfo.symbuf == NULL)
8441 goto error_return;
8442 if (elf_numsections (abfd) > SHN_LORESERVE)
8443 {
8444 /* Wild guess at number of output symbols. realloc'd as needed. */
8445 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8446 finfo.shndxbuf_size = amt;
8447 amt *= sizeof (Elf_External_Sym_Shndx);
8448 finfo.symshndxbuf = bfd_zmalloc (amt);
8449 if (finfo.symshndxbuf == NULL)
8450 goto error_return;
8451 }
8452
8453 /* Start writing out the symbol table. The first symbol is always a
8454 dummy symbol. */
8455 if (info->strip != strip_all
8456 || emit_relocs)
8457 {
8458 elfsym.st_value = 0;
8459 elfsym.st_size = 0;
8460 elfsym.st_info = 0;
8461 elfsym.st_other = 0;
8462 elfsym.st_shndx = SHN_UNDEF;
8463 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8464 NULL))
8465 goto error_return;
8466 }
8467
8468 /* Output a symbol for each section. We output these even if we are
8469 discarding local symbols, since they are used for relocs. These
8470 symbols have no names. We store the index of each one in the
8471 index field of the section, so that we can find it again when
8472 outputting relocs. */
8473 if (info->strip != strip_all
8474 || emit_relocs)
8475 {
8476 elfsym.st_size = 0;
8477 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8478 elfsym.st_other = 0;
8479 elfsym.st_value = 0;
8480 for (i = 1; i < elf_numsections (abfd); i++)
8481 {
8482 o = bfd_section_from_elf_index (abfd, i);
8483 if (o != NULL)
8484 {
8485 o->target_index = bfd_get_symcount (abfd);
8486 elfsym.st_shndx = i;
8487 if (!info->relocatable)
8488 elfsym.st_value = o->vma;
8489 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8490 goto error_return;
8491 }
8492 if (i == SHN_LORESERVE - 1)
8493 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8494 }
8495 }
8496
8497 /* Allocate some memory to hold information read in from the input
8498 files. */
8499 if (max_contents_size != 0)
8500 {
8501 finfo.contents = bfd_malloc (max_contents_size);
8502 if (finfo.contents == NULL)
8503 goto error_return;
8504 }
8505
8506 if (max_external_reloc_size != 0)
8507 {
8508 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8509 if (finfo.external_relocs == NULL)
8510 goto error_return;
8511 }
8512
8513 if (max_internal_reloc_count != 0)
8514 {
8515 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8516 amt *= sizeof (Elf_Internal_Rela);
8517 finfo.internal_relocs = bfd_malloc (amt);
8518 if (finfo.internal_relocs == NULL)
8519 goto error_return;
8520 }
8521
8522 if (max_sym_count != 0)
8523 {
8524 amt = max_sym_count * bed->s->sizeof_sym;
8525 finfo.external_syms = bfd_malloc (amt);
8526 if (finfo.external_syms == NULL)
8527 goto error_return;
8528
8529 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8530 finfo.internal_syms = bfd_malloc (amt);
8531 if (finfo.internal_syms == NULL)
8532 goto error_return;
8533
8534 amt = max_sym_count * sizeof (long);
8535 finfo.indices = bfd_malloc (amt);
8536 if (finfo.indices == NULL)
8537 goto error_return;
8538
8539 amt = max_sym_count * sizeof (asection *);
8540 finfo.sections = bfd_malloc (amt);
8541 if (finfo.sections == NULL)
8542 goto error_return;
8543 }
8544
8545 if (max_sym_shndx_count != 0)
8546 {
8547 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8548 finfo.locsym_shndx = bfd_malloc (amt);
8549 if (finfo.locsym_shndx == NULL)
8550 goto error_return;
8551 }
8552
8553 if (elf_hash_table (info)->tls_sec)
8554 {
8555 bfd_vma base, end = 0;
8556 asection *sec;
8557
8558 for (sec = elf_hash_table (info)->tls_sec;
8559 sec && (sec->flags & SEC_THREAD_LOCAL);
8560 sec = sec->next)
8561 {
8562 bfd_size_type size = sec->size;
8563
8564 if (size == 0
8565 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8566 {
8567 struct bfd_link_order *o = sec->map_tail.link_order;
8568 if (o != NULL)
8569 size = o->offset + o->size;
8570 }
8571 end = sec->vma + size;
8572 }
8573 base = elf_hash_table (info)->tls_sec->vma;
8574 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8575 elf_hash_table (info)->tls_size = end - base;
8576 }
8577
8578 /* Reorder SHF_LINK_ORDER sections. */
8579 for (o = abfd->sections; o != NULL; o = o->next)
8580 {
8581 if (!elf_fixup_link_order (abfd, o))
8582 return FALSE;
8583 }
8584
8585 /* Since ELF permits relocations to be against local symbols, we
8586 must have the local symbols available when we do the relocations.
8587 Since we would rather only read the local symbols once, and we
8588 would rather not keep them in memory, we handle all the
8589 relocations for a single input file at the same time.
8590
8591 Unfortunately, there is no way to know the total number of local
8592 symbols until we have seen all of them, and the local symbol
8593 indices precede the global symbol indices. This means that when
8594 we are generating relocatable output, and we see a reloc against
8595 a global symbol, we can not know the symbol index until we have
8596 finished examining all the local symbols to see which ones we are
8597 going to output. To deal with this, we keep the relocations in
8598 memory, and don't output them until the end of the link. This is
8599 an unfortunate waste of memory, but I don't see a good way around
8600 it. Fortunately, it only happens when performing a relocatable
8601 link, which is not the common case. FIXME: If keep_memory is set
8602 we could write the relocs out and then read them again; I don't
8603 know how bad the memory loss will be. */
8604
8605 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8606 sub->output_has_begun = FALSE;
8607 for (o = abfd->sections; o != NULL; o = o->next)
8608 {
8609 for (p = o->map_head.link_order; p != NULL; p = p->next)
8610 {
8611 if (p->type == bfd_indirect_link_order
8612 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8613 == bfd_target_elf_flavour)
8614 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8615 {
8616 if (! sub->output_has_begun)
8617 {
8618 if (! elf_link_input_bfd (&finfo, sub))
8619 goto error_return;
8620 sub->output_has_begun = TRUE;
8621 }
8622 }
8623 else if (p->type == bfd_section_reloc_link_order
8624 || p->type == bfd_symbol_reloc_link_order)
8625 {
8626 if (! elf_reloc_link_order (abfd, info, o, p))
8627 goto error_return;
8628 }
8629 else
8630 {
8631 if (! _bfd_default_link_order (abfd, info, o, p))
8632 goto error_return;
8633 }
8634 }
8635 }
8636
8637 /* Output any global symbols that got converted to local in a
8638 version script or due to symbol visibility. We do this in a
8639 separate step since ELF requires all local symbols to appear
8640 prior to any global symbols. FIXME: We should only do this if
8641 some global symbols were, in fact, converted to become local.
8642 FIXME: Will this work correctly with the Irix 5 linker? */
8643 eoinfo.failed = FALSE;
8644 eoinfo.finfo = &finfo;
8645 eoinfo.localsyms = TRUE;
8646 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8647 &eoinfo);
8648 if (eoinfo.failed)
8649 return FALSE;
8650
8651 /* If backend needs to output some local symbols not present in the hash
8652 table, do it now. */
8653 if (bed->elf_backend_output_arch_local_syms)
8654 {
8655 typedef bfd_boolean (*out_sym_func)
8656 (void *, const char *, Elf_Internal_Sym *, asection *,
8657 struct elf_link_hash_entry *);
8658
8659 if (! ((*bed->elf_backend_output_arch_local_syms)
8660 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8661 return FALSE;
8662 }
8663
8664 /* That wrote out all the local symbols. Finish up the symbol table
8665 with the global symbols. Even if we want to strip everything we
8666 can, we still need to deal with those global symbols that got
8667 converted to local in a version script. */
8668
8669 /* The sh_info field records the index of the first non local symbol. */
8670 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8671
8672 if (dynamic
8673 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8674 {
8675 Elf_Internal_Sym sym;
8676 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8677 long last_local = 0;
8678
8679 /* Write out the section symbols for the output sections. */
8680 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8681 {
8682 asection *s;
8683
8684 sym.st_size = 0;
8685 sym.st_name = 0;
8686 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8687 sym.st_other = 0;
8688
8689 for (s = abfd->sections; s != NULL; s = s->next)
8690 {
8691 int indx;
8692 bfd_byte *dest;
8693 long dynindx;
8694
8695 dynindx = elf_section_data (s)->dynindx;
8696 if (dynindx <= 0)
8697 continue;
8698 indx = elf_section_data (s)->this_idx;
8699 BFD_ASSERT (indx > 0);
8700 sym.st_shndx = indx;
8701 if (! check_dynsym (abfd, &sym))
8702 return FALSE;
8703 sym.st_value = s->vma;
8704 dest = dynsym + dynindx * bed->s->sizeof_sym;
8705 if (last_local < dynindx)
8706 last_local = dynindx;
8707 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8708 }
8709 }
8710
8711 /* Write out the local dynsyms. */
8712 if (elf_hash_table (info)->dynlocal)
8713 {
8714 struct elf_link_local_dynamic_entry *e;
8715 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8716 {
8717 asection *s;
8718 bfd_byte *dest;
8719
8720 sym.st_size = e->isym.st_size;
8721 sym.st_other = e->isym.st_other;
8722
8723 /* Copy the internal symbol as is.
8724 Note that we saved a word of storage and overwrote
8725 the original st_name with the dynstr_index. */
8726 sym = e->isym;
8727
8728 if (e->isym.st_shndx != SHN_UNDEF
8729 && (e->isym.st_shndx < SHN_LORESERVE
8730 || e->isym.st_shndx > SHN_HIRESERVE))
8731 {
8732 s = bfd_section_from_elf_index (e->input_bfd,
8733 e->isym.st_shndx);
8734
8735 sym.st_shndx =
8736 elf_section_data (s->output_section)->this_idx;
8737 if (! check_dynsym (abfd, &sym))
8738 return FALSE;
8739 sym.st_value = (s->output_section->vma
8740 + s->output_offset
8741 + e->isym.st_value);
8742 }
8743
8744 if (last_local < e->dynindx)
8745 last_local = e->dynindx;
8746
8747 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8748 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8749 }
8750 }
8751
8752 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8753 last_local + 1;
8754 }
8755
8756 /* We get the global symbols from the hash table. */
8757 eoinfo.failed = FALSE;
8758 eoinfo.localsyms = FALSE;
8759 eoinfo.finfo = &finfo;
8760 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8761 &eoinfo);
8762 if (eoinfo.failed)
8763 return FALSE;
8764
8765 /* If backend needs to output some symbols not present in the hash
8766 table, do it now. */
8767 if (bed->elf_backend_output_arch_syms)
8768 {
8769 typedef bfd_boolean (*out_sym_func)
8770 (void *, const char *, Elf_Internal_Sym *, asection *,
8771 struct elf_link_hash_entry *);
8772
8773 if (! ((*bed->elf_backend_output_arch_syms)
8774 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8775 return FALSE;
8776 }
8777
8778 /* Flush all symbols to the file. */
8779 if (! elf_link_flush_output_syms (&finfo, bed))
8780 return FALSE;
8781
8782 /* Now we know the size of the symtab section. */
8783 off += symtab_hdr->sh_size;
8784
8785 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8786 if (symtab_shndx_hdr->sh_name != 0)
8787 {
8788 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8789 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8790 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8791 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8792 symtab_shndx_hdr->sh_size = amt;
8793
8794 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8795 off, TRUE);
8796
8797 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8798 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8799 return FALSE;
8800 }
8801
8802
8803 /* Finish up and write out the symbol string table (.strtab)
8804 section. */
8805 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8806 /* sh_name was set in prep_headers. */
8807 symstrtab_hdr->sh_type = SHT_STRTAB;
8808 symstrtab_hdr->sh_flags = 0;
8809 symstrtab_hdr->sh_addr = 0;
8810 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8811 symstrtab_hdr->sh_entsize = 0;
8812 symstrtab_hdr->sh_link = 0;
8813 symstrtab_hdr->sh_info = 0;
8814 /* sh_offset is set just below. */
8815 symstrtab_hdr->sh_addralign = 1;
8816
8817 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8818 elf_tdata (abfd)->next_file_pos = off;
8819
8820 if (bfd_get_symcount (abfd) > 0)
8821 {
8822 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8823 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8824 return FALSE;
8825 }
8826
8827 /* Adjust the relocs to have the correct symbol indices. */
8828 for (o = abfd->sections; o != NULL; o = o->next)
8829 {
8830 if ((o->flags & SEC_RELOC) == 0)
8831 continue;
8832
8833 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8834 elf_section_data (o)->rel_count,
8835 elf_section_data (o)->rel_hashes);
8836 if (elf_section_data (o)->rel_hdr2 != NULL)
8837 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8838 elf_section_data (o)->rel_count2,
8839 (elf_section_data (o)->rel_hashes
8840 + elf_section_data (o)->rel_count));
8841
8842 /* Set the reloc_count field to 0 to prevent write_relocs from
8843 trying to swap the relocs out itself. */
8844 o->reloc_count = 0;
8845 }
8846
8847 if (dynamic && info->combreloc && dynobj != NULL)
8848 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8849
8850 /* If we are linking against a dynamic object, or generating a
8851 shared library, finish up the dynamic linking information. */
8852 if (dynamic)
8853 {
8854 bfd_byte *dyncon, *dynconend;
8855
8856 /* Fix up .dynamic entries. */
8857 o = bfd_get_section_by_name (dynobj, ".dynamic");
8858 BFD_ASSERT (o != NULL);
8859
8860 dyncon = o->contents;
8861 dynconend = o->contents + o->size;
8862 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8863 {
8864 Elf_Internal_Dyn dyn;
8865 const char *name;
8866 unsigned int type;
8867
8868 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8869
8870 switch (dyn.d_tag)
8871 {
8872 default:
8873 continue;
8874 case DT_NULL:
8875 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8876 {
8877 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8878 {
8879 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8880 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8881 default: continue;
8882 }
8883 dyn.d_un.d_val = relativecount;
8884 relativecount = 0;
8885 break;
8886 }
8887 continue;
8888
8889 case DT_INIT:
8890 name = info->init_function;
8891 goto get_sym;
8892 case DT_FINI:
8893 name = info->fini_function;
8894 get_sym:
8895 {
8896 struct elf_link_hash_entry *h;
8897
8898 h = elf_link_hash_lookup (elf_hash_table (info), name,
8899 FALSE, FALSE, TRUE);
8900 if (h != NULL
8901 && (h->root.type == bfd_link_hash_defined
8902 || h->root.type == bfd_link_hash_defweak))
8903 {
8904 dyn.d_un.d_val = h->root.u.def.value;
8905 o = h->root.u.def.section;
8906 if (o->output_section != NULL)
8907 dyn.d_un.d_val += (o->output_section->vma
8908 + o->output_offset);
8909 else
8910 {
8911 /* The symbol is imported from another shared
8912 library and does not apply to this one. */
8913 dyn.d_un.d_val = 0;
8914 }
8915 break;
8916 }
8917 }
8918 continue;
8919
8920 case DT_PREINIT_ARRAYSZ:
8921 name = ".preinit_array";
8922 goto get_size;
8923 case DT_INIT_ARRAYSZ:
8924 name = ".init_array";
8925 goto get_size;
8926 case DT_FINI_ARRAYSZ:
8927 name = ".fini_array";
8928 get_size:
8929 o = bfd_get_section_by_name (abfd, name);
8930 if (o == NULL)
8931 {
8932 (*_bfd_error_handler)
8933 (_("%B: could not find output section %s"), abfd, name);
8934 goto error_return;
8935 }
8936 if (o->size == 0)
8937 (*_bfd_error_handler)
8938 (_("warning: %s section has zero size"), name);
8939 dyn.d_un.d_val = o->size;
8940 break;
8941
8942 case DT_PREINIT_ARRAY:
8943 name = ".preinit_array";
8944 goto get_vma;
8945 case DT_INIT_ARRAY:
8946 name = ".init_array";
8947 goto get_vma;
8948 case DT_FINI_ARRAY:
8949 name = ".fini_array";
8950 goto get_vma;
8951
8952 case DT_HASH:
8953 name = ".hash";
8954 goto get_vma;
8955 case DT_GNU_HASH:
8956 name = ".gnu.hash";
8957 goto get_vma;
8958 case DT_STRTAB:
8959 name = ".dynstr";
8960 goto get_vma;
8961 case DT_SYMTAB:
8962 name = ".dynsym";
8963 goto get_vma;
8964 case DT_VERDEF:
8965 name = ".gnu.version_d";
8966 goto get_vma;
8967 case DT_VERNEED:
8968 name = ".gnu.version_r";
8969 goto get_vma;
8970 case DT_VERSYM:
8971 name = ".gnu.version";
8972 get_vma:
8973 o = bfd_get_section_by_name (abfd, name);
8974 if (o == NULL)
8975 {
8976 (*_bfd_error_handler)
8977 (_("%B: could not find output section %s"), abfd, name);
8978 goto error_return;
8979 }
8980 dyn.d_un.d_ptr = o->vma;
8981 break;
8982
8983 case DT_REL:
8984 case DT_RELA:
8985 case DT_RELSZ:
8986 case DT_RELASZ:
8987 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8988 type = SHT_REL;
8989 else
8990 type = SHT_RELA;
8991 dyn.d_un.d_val = 0;
8992 for (i = 1; i < elf_numsections (abfd); i++)
8993 {
8994 Elf_Internal_Shdr *hdr;
8995
8996 hdr = elf_elfsections (abfd)[i];
8997 if (hdr->sh_type == type
8998 && (hdr->sh_flags & SHF_ALLOC) != 0)
8999 {
9000 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
9001 dyn.d_un.d_val += hdr->sh_size;
9002 else
9003 {
9004 if (dyn.d_un.d_val == 0
9005 || hdr->sh_addr < dyn.d_un.d_val)
9006 dyn.d_un.d_val = hdr->sh_addr;
9007 }
9008 }
9009 }
9010 break;
9011 }
9012 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
9013 }
9014 }
9015
9016 /* If we have created any dynamic sections, then output them. */
9017 if (dynobj != NULL)
9018 {
9019 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
9020 goto error_return;
9021
9022 /* Check for DT_TEXTREL (late, in case the backend removes it). */
9023 if (info->warn_shared_textrel && info->shared)
9024 {
9025 bfd_byte *dyncon, *dynconend;
9026
9027 /* Fix up .dynamic entries. */
9028 o = bfd_get_section_by_name (dynobj, ".dynamic");
9029 BFD_ASSERT (o != NULL);
9030
9031 dyncon = o->contents;
9032 dynconend = o->contents + o->size;
9033 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9034 {
9035 Elf_Internal_Dyn dyn;
9036
9037 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9038
9039 if (dyn.d_tag == DT_TEXTREL)
9040 {
9041 _bfd_error_handler
9042 (_("warning: creating a DT_TEXTREL in a shared object."));
9043 break;
9044 }
9045 }
9046 }
9047
9048 for (o = dynobj->sections; o != NULL; o = o->next)
9049 {
9050 if ((o->flags & SEC_HAS_CONTENTS) == 0
9051 || o->size == 0
9052 || o->output_section == bfd_abs_section_ptr)
9053 continue;
9054 if ((o->flags & SEC_LINKER_CREATED) == 0)
9055 {
9056 /* At this point, we are only interested in sections
9057 created by _bfd_elf_link_create_dynamic_sections. */
9058 continue;
9059 }
9060 if (elf_hash_table (info)->stab_info.stabstr == o)
9061 continue;
9062 if (elf_hash_table (info)->eh_info.hdr_sec == o)
9063 continue;
9064 if ((elf_section_data (o->output_section)->this_hdr.sh_type
9065 != SHT_STRTAB)
9066 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
9067 {
9068 if (! bfd_set_section_contents (abfd, o->output_section,
9069 o->contents,
9070 (file_ptr) o->output_offset,
9071 o->size))
9072 goto error_return;
9073 }
9074 else
9075 {
9076 /* The contents of the .dynstr section are actually in a
9077 stringtab. */
9078 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
9079 if (bfd_seek (abfd, off, SEEK_SET) != 0
9080 || ! _bfd_elf_strtab_emit (abfd,
9081 elf_hash_table (info)->dynstr))
9082 goto error_return;
9083 }
9084 }
9085 }
9086
9087 if (info->relocatable)
9088 {
9089 bfd_boolean failed = FALSE;
9090
9091 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
9092 if (failed)
9093 goto error_return;
9094 }
9095
9096 /* If we have optimized stabs strings, output them. */
9097 if (elf_hash_table (info)->stab_info.stabstr != NULL)
9098 {
9099 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
9100 goto error_return;
9101 }
9102
9103 if (info->eh_frame_hdr)
9104 {
9105 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
9106 goto error_return;
9107 }
9108
9109 if (finfo.symstrtab != NULL)
9110 _bfd_stringtab_free (finfo.symstrtab);
9111 if (finfo.contents != NULL)
9112 free (finfo.contents);
9113 if (finfo.external_relocs != NULL)
9114 free (finfo.external_relocs);
9115 if (finfo.internal_relocs != NULL)
9116 free (finfo.internal_relocs);
9117 if (finfo.external_syms != NULL)
9118 free (finfo.external_syms);
9119 if (finfo.locsym_shndx != NULL)
9120 free (finfo.locsym_shndx);
9121 if (finfo.internal_syms != NULL)
9122 free (finfo.internal_syms);
9123 if (finfo.indices != NULL)
9124 free (finfo.indices);
9125 if (finfo.sections != NULL)
9126 free (finfo.sections);
9127 if (finfo.symbuf != NULL)
9128 free (finfo.symbuf);
9129 if (finfo.symshndxbuf != NULL)
9130 free (finfo.symshndxbuf);
9131 for (o = abfd->sections; o != NULL; o = o->next)
9132 {
9133 if ((o->flags & SEC_RELOC) != 0
9134 && elf_section_data (o)->rel_hashes != NULL)
9135 free (elf_section_data (o)->rel_hashes);
9136 }
9137
9138 elf_tdata (abfd)->linker = TRUE;
9139
9140 return TRUE;
9141
9142 error_return:
9143 if (finfo.symstrtab != NULL)
9144 _bfd_stringtab_free (finfo.symstrtab);
9145 if (finfo.contents != NULL)
9146 free (finfo.contents);
9147 if (finfo.external_relocs != NULL)
9148 free (finfo.external_relocs);
9149 if (finfo.internal_relocs != NULL)
9150 free (finfo.internal_relocs);
9151 if (finfo.external_syms != NULL)
9152 free (finfo.external_syms);
9153 if (finfo.locsym_shndx != NULL)
9154 free (finfo.locsym_shndx);
9155 if (finfo.internal_syms != NULL)
9156 free (finfo.internal_syms);
9157 if (finfo.indices != NULL)
9158 free (finfo.indices);
9159 if (finfo.sections != NULL)
9160 free (finfo.sections);
9161 if (finfo.symbuf != NULL)
9162 free (finfo.symbuf);
9163 if (finfo.symshndxbuf != NULL)
9164 free (finfo.symshndxbuf);
9165 for (o = abfd->sections; o != NULL; o = o->next)
9166 {
9167 if ((o->flags & SEC_RELOC) != 0
9168 && elf_section_data (o)->rel_hashes != NULL)
9169 free (elf_section_data (o)->rel_hashes);
9170 }
9171
9172 return FALSE;
9173 }
9174 \f
9175 /* Garbage collect unused sections. */
9176
9177 /* The mark phase of garbage collection. For a given section, mark
9178 it and any sections in this section's group, and all the sections
9179 which define symbols to which it refers. */
9180
9181 typedef asection * (*gc_mark_hook_fn)
9182 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9183 struct elf_link_hash_entry *, Elf_Internal_Sym *);
9184
9185 bfd_boolean
9186 _bfd_elf_gc_mark (struct bfd_link_info *info,
9187 asection *sec,
9188 gc_mark_hook_fn gc_mark_hook)
9189 {
9190 bfd_boolean ret;
9191 bfd_boolean is_eh;
9192 asection *group_sec;
9193
9194 sec->gc_mark = 1;
9195
9196 /* Mark all the sections in the group. */
9197 group_sec = elf_section_data (sec)->next_in_group;
9198 if (group_sec && !group_sec->gc_mark)
9199 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
9200 return FALSE;
9201
9202 /* Look through the section relocs. */
9203 ret = TRUE;
9204 is_eh = strcmp (sec->name, ".eh_frame") == 0;
9205 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
9206 {
9207 Elf_Internal_Rela *relstart, *rel, *relend;
9208 Elf_Internal_Shdr *symtab_hdr;
9209 struct elf_link_hash_entry **sym_hashes;
9210 size_t nlocsyms;
9211 size_t extsymoff;
9212 bfd *input_bfd = sec->owner;
9213 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
9214 Elf_Internal_Sym *isym = NULL;
9215 int r_sym_shift;
9216
9217 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9218 sym_hashes = elf_sym_hashes (input_bfd);
9219
9220 /* Read the local symbols. */
9221 if (elf_bad_symtab (input_bfd))
9222 {
9223 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
9224 extsymoff = 0;
9225 }
9226 else
9227 extsymoff = nlocsyms = symtab_hdr->sh_info;
9228
9229 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
9230 if (isym == NULL && nlocsyms != 0)
9231 {
9232 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
9233 NULL, NULL, NULL);
9234 if (isym == NULL)
9235 return FALSE;
9236 }
9237
9238 /* Read the relocations. */
9239 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
9240 info->keep_memory);
9241 if (relstart == NULL)
9242 {
9243 ret = FALSE;
9244 goto out1;
9245 }
9246 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9247
9248 if (bed->s->arch_size == 32)
9249 r_sym_shift = 8;
9250 else
9251 r_sym_shift = 32;
9252
9253 for (rel = relstart; rel < relend; rel++)
9254 {
9255 unsigned long r_symndx;
9256 asection *rsec;
9257 struct elf_link_hash_entry *h;
9258
9259 r_symndx = rel->r_info >> r_sym_shift;
9260 if (r_symndx == 0)
9261 continue;
9262
9263 if (r_symndx >= nlocsyms
9264 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
9265 {
9266 h = sym_hashes[r_symndx - extsymoff];
9267 while (h->root.type == bfd_link_hash_indirect
9268 || h->root.type == bfd_link_hash_warning)
9269 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9270 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
9271 }
9272 else
9273 {
9274 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
9275 }
9276
9277 if (rsec && !rsec->gc_mark)
9278 {
9279 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
9280 rsec->gc_mark = 1;
9281 else if (is_eh)
9282 rsec->gc_mark_from_eh = 1;
9283 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
9284 {
9285 ret = FALSE;
9286 goto out2;
9287 }
9288 }
9289 }
9290
9291 out2:
9292 if (elf_section_data (sec)->relocs != relstart)
9293 free (relstart);
9294 out1:
9295 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
9296 {
9297 if (! info->keep_memory)
9298 free (isym);
9299 else
9300 symtab_hdr->contents = (unsigned char *) isym;
9301 }
9302 }
9303
9304 return ret;
9305 }
9306
9307 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
9308
9309 struct elf_gc_sweep_symbol_info {
9310 struct bfd_link_info *info;
9311 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
9312 bfd_boolean);
9313 };
9314
9315 static bfd_boolean
9316 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
9317 {
9318 if (h->root.type == bfd_link_hash_warning)
9319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9320
9321 if ((h->root.type == bfd_link_hash_defined
9322 || h->root.type == bfd_link_hash_defweak)
9323 && !h->root.u.def.section->gc_mark
9324 && !(h->root.u.def.section->owner->flags & DYNAMIC))
9325 {
9326 struct elf_gc_sweep_symbol_info *inf = data;
9327 (*inf->hide_symbol) (inf->info, h, TRUE);
9328 }
9329
9330 return TRUE;
9331 }
9332
9333 /* The sweep phase of garbage collection. Remove all garbage sections. */
9334
9335 typedef bfd_boolean (*gc_sweep_hook_fn)
9336 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
9337
9338 static bfd_boolean
9339 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
9340 {
9341 bfd *sub;
9342 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9343 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
9344 unsigned long section_sym_count;
9345 struct elf_gc_sweep_symbol_info sweep_info;
9346
9347 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9348 {
9349 asection *o;
9350
9351 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9352 continue;
9353
9354 for (o = sub->sections; o != NULL; o = o->next)
9355 {
9356 /* Keep debug and special sections. */
9357 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
9358 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
9359 o->gc_mark = 1;
9360
9361 if (o->gc_mark)
9362 continue;
9363
9364 /* Skip sweeping sections already excluded. */
9365 if (o->flags & SEC_EXCLUDE)
9366 continue;
9367
9368 /* Since this is early in the link process, it is simple
9369 to remove a section from the output. */
9370 o->flags |= SEC_EXCLUDE;
9371
9372 /* But we also have to update some of the relocation
9373 info we collected before. */
9374 if (gc_sweep_hook
9375 && (o->flags & SEC_RELOC) != 0
9376 && o->reloc_count > 0
9377 && !bfd_is_abs_section (o->output_section))
9378 {
9379 Elf_Internal_Rela *internal_relocs;
9380 bfd_boolean r;
9381
9382 internal_relocs
9383 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
9384 info->keep_memory);
9385 if (internal_relocs == NULL)
9386 return FALSE;
9387
9388 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
9389
9390 if (elf_section_data (o)->relocs != internal_relocs)
9391 free (internal_relocs);
9392
9393 if (!r)
9394 return FALSE;
9395 }
9396 }
9397 }
9398
9399 /* Remove the symbols that were in the swept sections from the dynamic
9400 symbol table. GCFIXME: Anyone know how to get them out of the
9401 static symbol table as well? */
9402 sweep_info.info = info;
9403 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
9404 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
9405 &sweep_info);
9406
9407 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
9408 return TRUE;
9409 }
9410
9411 /* Propagate collected vtable information. This is called through
9412 elf_link_hash_traverse. */
9413
9414 static bfd_boolean
9415 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
9416 {
9417 if (h->root.type == bfd_link_hash_warning)
9418 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9419
9420 /* Those that are not vtables. */
9421 if (h->vtable == NULL || h->vtable->parent == NULL)
9422 return TRUE;
9423
9424 /* Those vtables that do not have parents, we cannot merge. */
9425 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
9426 return TRUE;
9427
9428 /* If we've already been done, exit. */
9429 if (h->vtable->used && h->vtable->used[-1])
9430 return TRUE;
9431
9432 /* Make sure the parent's table is up to date. */
9433 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9434
9435 if (h->vtable->used == NULL)
9436 {
9437 /* None of this table's entries were referenced. Re-use the
9438 parent's table. */
9439 h->vtable->used = h->vtable->parent->vtable->used;
9440 h->vtable->size = h->vtable->parent->vtable->size;
9441 }
9442 else
9443 {
9444 size_t n;
9445 bfd_boolean *cu, *pu;
9446
9447 /* Or the parent's entries into ours. */
9448 cu = h->vtable->used;
9449 cu[-1] = TRUE;
9450 pu = h->vtable->parent->vtable->used;
9451 if (pu != NULL)
9452 {
9453 const struct elf_backend_data *bed;
9454 unsigned int log_file_align;
9455
9456 bed = get_elf_backend_data (h->root.u.def.section->owner);
9457 log_file_align = bed->s->log_file_align;
9458 n = h->vtable->parent->vtable->size >> log_file_align;
9459 while (n--)
9460 {
9461 if (*pu)
9462 *cu = TRUE;
9463 pu++;
9464 cu++;
9465 }
9466 }
9467 }
9468
9469 return TRUE;
9470 }
9471
9472 static bfd_boolean
9473 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9474 {
9475 asection *sec;
9476 bfd_vma hstart, hend;
9477 Elf_Internal_Rela *relstart, *relend, *rel;
9478 const struct elf_backend_data *bed;
9479 unsigned int log_file_align;
9480
9481 if (h->root.type == bfd_link_hash_warning)
9482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9483
9484 /* Take care of both those symbols that do not describe vtables as
9485 well as those that are not loaded. */
9486 if (h->vtable == NULL || h->vtable->parent == NULL)
9487 return TRUE;
9488
9489 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9490 || h->root.type == bfd_link_hash_defweak);
9491
9492 sec = h->root.u.def.section;
9493 hstart = h->root.u.def.value;
9494 hend = hstart + h->size;
9495
9496 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9497 if (!relstart)
9498 return *(bfd_boolean *) okp = FALSE;
9499 bed = get_elf_backend_data (sec->owner);
9500 log_file_align = bed->s->log_file_align;
9501
9502 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9503
9504 for (rel = relstart; rel < relend; ++rel)
9505 if (rel->r_offset >= hstart && rel->r_offset < hend)
9506 {
9507 /* If the entry is in use, do nothing. */
9508 if (h->vtable->used
9509 && (rel->r_offset - hstart) < h->vtable->size)
9510 {
9511 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9512 if (h->vtable->used[entry])
9513 continue;
9514 }
9515 /* Otherwise, kill it. */
9516 rel->r_offset = rel->r_info = rel->r_addend = 0;
9517 }
9518
9519 return TRUE;
9520 }
9521
9522 /* Mark sections containing dynamically referenced symbols. When
9523 building shared libraries, we must assume that any visible symbol is
9524 referenced. */
9525
9526 bfd_boolean
9527 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9528 {
9529 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9530
9531 if (h->root.type == bfd_link_hash_warning)
9532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9533
9534 if ((h->root.type == bfd_link_hash_defined
9535 || h->root.type == bfd_link_hash_defweak)
9536 && (h->ref_dynamic
9537 || (!info->executable
9538 && h->def_regular
9539 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9540 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9541 h->root.u.def.section->flags |= SEC_KEEP;
9542
9543 return TRUE;
9544 }
9545
9546 /* Do mark and sweep of unused sections. */
9547
9548 bfd_boolean
9549 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9550 {
9551 bfd_boolean ok = TRUE;
9552 bfd *sub;
9553 asection * (*gc_mark_hook)
9554 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9555 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9557
9558 if (!bed->can_gc_sections
9559 || info->relocatable
9560 || info->emitrelocations
9561 || !is_elf_hash_table (info->hash))
9562 {
9563 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9564 return TRUE;
9565 }
9566
9567 /* Apply transitive closure to the vtable entry usage info. */
9568 elf_link_hash_traverse (elf_hash_table (info),
9569 elf_gc_propagate_vtable_entries_used,
9570 &ok);
9571 if (!ok)
9572 return FALSE;
9573
9574 /* Kill the vtable relocations that were not used. */
9575 elf_link_hash_traverse (elf_hash_table (info),
9576 elf_gc_smash_unused_vtentry_relocs,
9577 &ok);
9578 if (!ok)
9579 return FALSE;
9580
9581 /* Mark dynamically referenced symbols. */
9582 if (elf_hash_table (info)->dynamic_sections_created)
9583 elf_link_hash_traverse (elf_hash_table (info),
9584 bed->gc_mark_dynamic_ref,
9585 info);
9586
9587 /* Grovel through relocs to find out who stays ... */
9588 gc_mark_hook = bed->gc_mark_hook;
9589 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9590 {
9591 asection *o;
9592
9593 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9594 continue;
9595
9596 for (o = sub->sections; o != NULL; o = o->next)
9597 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9598 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9599 return FALSE;
9600 }
9601
9602 /* ... again for sections marked from eh_frame. */
9603 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9604 {
9605 asection *o;
9606
9607 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9608 continue;
9609
9610 /* Keep .gcc_except_table.* if the associated .text.* is
9611 marked. This isn't very nice, but the proper solution,
9612 splitting .eh_frame up and using comdat doesn't pan out
9613 easily due to needing special relocs to handle the
9614 difference of two symbols in separate sections.
9615 Don't keep code sections referenced by .eh_frame. */
9616 for (o = sub->sections; o != NULL; o = o->next)
9617 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9618 {
9619 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9620 {
9621 unsigned long len;
9622 char *fn_name;
9623 asection *fn_text;
9624
9625 len = strlen (o->name + 18) + 1;
9626 fn_name = bfd_malloc (len + 6);
9627 if (fn_name == NULL)
9628 return FALSE;
9629 memcpy (fn_name, ".text.", 6);
9630 memcpy (fn_name + 6, o->name + 18, len);
9631 fn_text = bfd_get_section_by_name (sub, fn_name);
9632 free (fn_name);
9633 if (fn_text == NULL || !fn_text->gc_mark)
9634 continue;
9635 }
9636
9637 /* If not using specially named exception table section,
9638 then keep whatever we are using. */
9639 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9640 return FALSE;
9641 }
9642 }
9643
9644 /* ... and mark SEC_EXCLUDE for those that go. */
9645 return elf_gc_sweep (abfd, info);
9646 }
9647 \f
9648 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9649
9650 bfd_boolean
9651 bfd_elf_gc_record_vtinherit (bfd *abfd,
9652 asection *sec,
9653 struct elf_link_hash_entry *h,
9654 bfd_vma offset)
9655 {
9656 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9657 struct elf_link_hash_entry **search, *child;
9658 bfd_size_type extsymcount;
9659 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9660
9661 /* The sh_info field of the symtab header tells us where the
9662 external symbols start. We don't care about the local symbols at
9663 this point. */
9664 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9665 if (!elf_bad_symtab (abfd))
9666 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9667
9668 sym_hashes = elf_sym_hashes (abfd);
9669 sym_hashes_end = sym_hashes + extsymcount;
9670
9671 /* Hunt down the child symbol, which is in this section at the same
9672 offset as the relocation. */
9673 for (search = sym_hashes; search != sym_hashes_end; ++search)
9674 {
9675 if ((child = *search) != NULL
9676 && (child->root.type == bfd_link_hash_defined
9677 || child->root.type == bfd_link_hash_defweak)
9678 && child->root.u.def.section == sec
9679 && child->root.u.def.value == offset)
9680 goto win;
9681 }
9682
9683 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9684 abfd, sec, (unsigned long) offset);
9685 bfd_set_error (bfd_error_invalid_operation);
9686 return FALSE;
9687
9688 win:
9689 if (!child->vtable)
9690 {
9691 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9692 if (!child->vtable)
9693 return FALSE;
9694 }
9695 if (!h)
9696 {
9697 /* This *should* only be the absolute section. It could potentially
9698 be that someone has defined a non-global vtable though, which
9699 would be bad. It isn't worth paging in the local symbols to be
9700 sure though; that case should simply be handled by the assembler. */
9701
9702 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9703 }
9704 else
9705 child->vtable->parent = h;
9706
9707 return TRUE;
9708 }
9709
9710 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9711
9712 bfd_boolean
9713 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9714 asection *sec ATTRIBUTE_UNUSED,
9715 struct elf_link_hash_entry *h,
9716 bfd_vma addend)
9717 {
9718 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9719 unsigned int log_file_align = bed->s->log_file_align;
9720
9721 if (!h->vtable)
9722 {
9723 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9724 if (!h->vtable)
9725 return FALSE;
9726 }
9727
9728 if (addend >= h->vtable->size)
9729 {
9730 size_t size, bytes, file_align;
9731 bfd_boolean *ptr = h->vtable->used;
9732
9733 /* While the symbol is undefined, we have to be prepared to handle
9734 a zero size. */
9735 file_align = 1 << log_file_align;
9736 if (h->root.type == bfd_link_hash_undefined)
9737 size = addend + file_align;
9738 else
9739 {
9740 size = h->size;
9741 if (addend >= size)
9742 {
9743 /* Oops! We've got a reference past the defined end of
9744 the table. This is probably a bug -- shall we warn? */
9745 size = addend + file_align;
9746 }
9747 }
9748 size = (size + file_align - 1) & -file_align;
9749
9750 /* Allocate one extra entry for use as a "done" flag for the
9751 consolidation pass. */
9752 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9753
9754 if (ptr)
9755 {
9756 ptr = bfd_realloc (ptr - 1, bytes);
9757
9758 if (ptr != NULL)
9759 {
9760 size_t oldbytes;
9761
9762 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9763 * sizeof (bfd_boolean));
9764 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9765 }
9766 }
9767 else
9768 ptr = bfd_zmalloc (bytes);
9769
9770 if (ptr == NULL)
9771 return FALSE;
9772
9773 /* And arrange for that done flag to be at index -1. */
9774 h->vtable->used = ptr + 1;
9775 h->vtable->size = size;
9776 }
9777
9778 h->vtable->used[addend >> log_file_align] = TRUE;
9779
9780 return TRUE;
9781 }
9782
9783 struct alloc_got_off_arg {
9784 bfd_vma gotoff;
9785 unsigned int got_elt_size;
9786 };
9787
9788 /* We need a special top-level link routine to convert got reference counts
9789 to real got offsets. */
9790
9791 static bfd_boolean
9792 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9793 {
9794 struct alloc_got_off_arg *gofarg = arg;
9795
9796 if (h->root.type == bfd_link_hash_warning)
9797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9798
9799 if (h->got.refcount > 0)
9800 {
9801 h->got.offset = gofarg->gotoff;
9802 gofarg->gotoff += gofarg->got_elt_size;
9803 }
9804 else
9805 h->got.offset = (bfd_vma) -1;
9806
9807 return TRUE;
9808 }
9809
9810 /* And an accompanying bit to work out final got entry offsets once
9811 we're done. Should be called from final_link. */
9812
9813 bfd_boolean
9814 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9815 struct bfd_link_info *info)
9816 {
9817 bfd *i;
9818 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9819 bfd_vma gotoff;
9820 unsigned int got_elt_size = bed->s->arch_size / 8;
9821 struct alloc_got_off_arg gofarg;
9822
9823 if (! is_elf_hash_table (info->hash))
9824 return FALSE;
9825
9826 /* The GOT offset is relative to the .got section, but the GOT header is
9827 put into the .got.plt section, if the backend uses it. */
9828 if (bed->want_got_plt)
9829 gotoff = 0;
9830 else
9831 gotoff = bed->got_header_size;
9832
9833 /* Do the local .got entries first. */
9834 for (i = info->input_bfds; i; i = i->link_next)
9835 {
9836 bfd_signed_vma *local_got;
9837 bfd_size_type j, locsymcount;
9838 Elf_Internal_Shdr *symtab_hdr;
9839
9840 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9841 continue;
9842
9843 local_got = elf_local_got_refcounts (i);
9844 if (!local_got)
9845 continue;
9846
9847 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9848 if (elf_bad_symtab (i))
9849 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9850 else
9851 locsymcount = symtab_hdr->sh_info;
9852
9853 for (j = 0; j < locsymcount; ++j)
9854 {
9855 if (local_got[j] > 0)
9856 {
9857 local_got[j] = gotoff;
9858 gotoff += got_elt_size;
9859 }
9860 else
9861 local_got[j] = (bfd_vma) -1;
9862 }
9863 }
9864
9865 /* Then the global .got entries. .plt refcounts are handled by
9866 adjust_dynamic_symbol */
9867 gofarg.gotoff = gotoff;
9868 gofarg.got_elt_size = got_elt_size;
9869 elf_link_hash_traverse (elf_hash_table (info),
9870 elf_gc_allocate_got_offsets,
9871 &gofarg);
9872 return TRUE;
9873 }
9874
9875 /* Many folk need no more in the way of final link than this, once
9876 got entry reference counting is enabled. */
9877
9878 bfd_boolean
9879 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9880 {
9881 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9882 return FALSE;
9883
9884 /* Invoke the regular ELF backend linker to do all the work. */
9885 return bfd_elf_final_link (abfd, info);
9886 }
9887
9888 bfd_boolean
9889 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9890 {
9891 struct elf_reloc_cookie *rcookie = cookie;
9892
9893 if (rcookie->bad_symtab)
9894 rcookie->rel = rcookie->rels;
9895
9896 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9897 {
9898 unsigned long r_symndx;
9899
9900 if (! rcookie->bad_symtab)
9901 if (rcookie->rel->r_offset > offset)
9902 return FALSE;
9903 if (rcookie->rel->r_offset != offset)
9904 continue;
9905
9906 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9907 if (r_symndx == SHN_UNDEF)
9908 return TRUE;
9909
9910 if (r_symndx >= rcookie->locsymcount
9911 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9912 {
9913 struct elf_link_hash_entry *h;
9914
9915 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9916
9917 while (h->root.type == bfd_link_hash_indirect
9918 || h->root.type == bfd_link_hash_warning)
9919 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9920
9921 if ((h->root.type == bfd_link_hash_defined
9922 || h->root.type == bfd_link_hash_defweak)
9923 && elf_discarded_section (h->root.u.def.section))
9924 return TRUE;
9925 else
9926 return FALSE;
9927 }
9928 else
9929 {
9930 /* It's not a relocation against a global symbol,
9931 but it could be a relocation against a local
9932 symbol for a discarded section. */
9933 asection *isec;
9934 Elf_Internal_Sym *isym;
9935
9936 /* Need to: get the symbol; get the section. */
9937 isym = &rcookie->locsyms[r_symndx];
9938 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9939 {
9940 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9941 if (isec != NULL && elf_discarded_section (isec))
9942 return TRUE;
9943 }
9944 }
9945 return FALSE;
9946 }
9947 return FALSE;
9948 }
9949
9950 /* Discard unneeded references to discarded sections.
9951 Returns TRUE if any section's size was changed. */
9952 /* This function assumes that the relocations are in sorted order,
9953 which is true for all known assemblers. */
9954
9955 bfd_boolean
9956 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9957 {
9958 struct elf_reloc_cookie cookie;
9959 asection *stab, *eh;
9960 Elf_Internal_Shdr *symtab_hdr;
9961 const struct elf_backend_data *bed;
9962 bfd *abfd;
9963 unsigned int count;
9964 bfd_boolean ret = FALSE;
9965
9966 if (info->traditional_format
9967 || !is_elf_hash_table (info->hash))
9968 return FALSE;
9969
9970 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9971 {
9972 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9973 continue;
9974
9975 bed = get_elf_backend_data (abfd);
9976
9977 if ((abfd->flags & DYNAMIC) != 0)
9978 continue;
9979
9980 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9981 if (info->relocatable
9982 || (eh != NULL
9983 && (eh->size == 0
9984 || bfd_is_abs_section (eh->output_section))))
9985 eh = NULL;
9986
9987 stab = bfd_get_section_by_name (abfd, ".stab");
9988 if (stab != NULL
9989 && (stab->size == 0
9990 || bfd_is_abs_section (stab->output_section)
9991 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9992 stab = NULL;
9993
9994 if (stab == NULL
9995 && eh == NULL
9996 && bed->elf_backend_discard_info == NULL)
9997 continue;
9998
9999 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10000 cookie.abfd = abfd;
10001 cookie.sym_hashes = elf_sym_hashes (abfd);
10002 cookie.bad_symtab = elf_bad_symtab (abfd);
10003 if (cookie.bad_symtab)
10004 {
10005 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10006 cookie.extsymoff = 0;
10007 }
10008 else
10009 {
10010 cookie.locsymcount = symtab_hdr->sh_info;
10011 cookie.extsymoff = symtab_hdr->sh_info;
10012 }
10013
10014 if (bed->s->arch_size == 32)
10015 cookie.r_sym_shift = 8;
10016 else
10017 cookie.r_sym_shift = 32;
10018
10019 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10020 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
10021 {
10022 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
10023 cookie.locsymcount, 0,
10024 NULL, NULL, NULL);
10025 if (cookie.locsyms == NULL)
10026 return FALSE;
10027 }
10028
10029 if (stab != NULL)
10030 {
10031 cookie.rels = NULL;
10032 count = stab->reloc_count;
10033 if (count != 0)
10034 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
10035 info->keep_memory);
10036 if (cookie.rels != NULL)
10037 {
10038 cookie.rel = cookie.rels;
10039 cookie.relend = cookie.rels;
10040 cookie.relend += count * bed->s->int_rels_per_ext_rel;
10041 if (_bfd_discard_section_stabs (abfd, stab,
10042 elf_section_data (stab)->sec_info,
10043 bfd_elf_reloc_symbol_deleted_p,
10044 &cookie))
10045 ret = TRUE;
10046 if (elf_section_data (stab)->relocs != cookie.rels)
10047 free (cookie.rels);
10048 }
10049 }
10050
10051 if (eh != NULL)
10052 {
10053 cookie.rels = NULL;
10054 count = eh->reloc_count;
10055 if (count != 0)
10056 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
10057 info->keep_memory);
10058 cookie.rel = cookie.rels;
10059 cookie.relend = cookie.rels;
10060 if (cookie.rels != NULL)
10061 cookie.relend += count * bed->s->int_rels_per_ext_rel;
10062
10063 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
10064 bfd_elf_reloc_symbol_deleted_p,
10065 &cookie))
10066 ret = TRUE;
10067
10068 if (cookie.rels != NULL
10069 && elf_section_data (eh)->relocs != cookie.rels)
10070 free (cookie.rels);
10071 }
10072
10073 if (bed->elf_backend_discard_info != NULL
10074 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
10075 ret = TRUE;
10076
10077 if (cookie.locsyms != NULL
10078 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
10079 {
10080 if (! info->keep_memory)
10081 free (cookie.locsyms);
10082 else
10083 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
10084 }
10085 }
10086
10087 if (info->eh_frame_hdr
10088 && !info->relocatable
10089 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
10090 ret = TRUE;
10091
10092 return ret;
10093 }
10094
10095 void
10096 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
10097 {
10098 flagword flags;
10099 const char *name, *p;
10100 struct bfd_section_already_linked *l;
10101 struct bfd_section_already_linked_hash_entry *already_linked_list;
10102 asection *group;
10103
10104 /* A single member comdat group section may be discarded by a
10105 linkonce section. See below. */
10106 if (sec->output_section == bfd_abs_section_ptr)
10107 return;
10108
10109 flags = sec->flags;
10110
10111 /* Check if it belongs to a section group. */
10112 group = elf_sec_group (sec);
10113
10114 /* Return if it isn't a linkonce section nor a member of a group. A
10115 comdat group section also has SEC_LINK_ONCE set. */
10116 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
10117 return;
10118
10119 if (group)
10120 {
10121 /* If this is the member of a single member comdat group, check if
10122 the group should be discarded. */
10123 if (elf_next_in_group (sec) == sec
10124 && (group->flags & SEC_LINK_ONCE) != 0)
10125 sec = group;
10126 else
10127 return;
10128 }
10129
10130 /* FIXME: When doing a relocatable link, we may have trouble
10131 copying relocations in other sections that refer to local symbols
10132 in the section being discarded. Those relocations will have to
10133 be converted somehow; as of this writing I'm not sure that any of
10134 the backends handle that correctly.
10135
10136 It is tempting to instead not discard link once sections when
10137 doing a relocatable link (technically, they should be discarded
10138 whenever we are building constructors). However, that fails,
10139 because the linker winds up combining all the link once sections
10140 into a single large link once section, which defeats the purpose
10141 of having link once sections in the first place.
10142
10143 Also, not merging link once sections in a relocatable link
10144 causes trouble for MIPS ELF, which relies on link once semantics
10145 to handle the .reginfo section correctly. */
10146
10147 name = bfd_get_section_name (abfd, sec);
10148
10149 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
10150 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
10151 p++;
10152 else
10153 p = name;
10154
10155 already_linked_list = bfd_section_already_linked_table_lookup (p);
10156
10157 for (l = already_linked_list->entry; l != NULL; l = l->next)
10158 {
10159 /* We may have 3 different sections on the list: group section,
10160 comdat section and linkonce section. SEC may be a linkonce or
10161 group section. We match a group section with a group section,
10162 a linkonce section with a linkonce section, and ignore comdat
10163 section. */
10164 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
10165 && strcmp (name, l->sec->name) == 0
10166 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
10167 {
10168 /* The section has already been linked. See if we should
10169 issue a warning. */
10170 switch (flags & SEC_LINK_DUPLICATES)
10171 {
10172 default:
10173 abort ();
10174
10175 case SEC_LINK_DUPLICATES_DISCARD:
10176 break;
10177
10178 case SEC_LINK_DUPLICATES_ONE_ONLY:
10179 (*_bfd_error_handler)
10180 (_("%B: ignoring duplicate section `%A'"),
10181 abfd, sec);
10182 break;
10183
10184 case SEC_LINK_DUPLICATES_SAME_SIZE:
10185 if (sec->size != l->sec->size)
10186 (*_bfd_error_handler)
10187 (_("%B: duplicate section `%A' has different size"),
10188 abfd, sec);
10189 break;
10190
10191 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
10192 if (sec->size != l->sec->size)
10193 (*_bfd_error_handler)
10194 (_("%B: duplicate section `%A' has different size"),
10195 abfd, sec);
10196 else if (sec->size != 0)
10197 {
10198 bfd_byte *sec_contents, *l_sec_contents;
10199
10200 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
10201 (*_bfd_error_handler)
10202 (_("%B: warning: could not read contents of section `%A'"),
10203 abfd, sec);
10204 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
10205 &l_sec_contents))
10206 (*_bfd_error_handler)
10207 (_("%B: warning: could not read contents of section `%A'"),
10208 l->sec->owner, l->sec);
10209 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
10210 (*_bfd_error_handler)
10211 (_("%B: warning: duplicate section `%A' has different contents"),
10212 abfd, sec);
10213
10214 if (sec_contents)
10215 free (sec_contents);
10216 if (l_sec_contents)
10217 free (l_sec_contents);
10218 }
10219 break;
10220 }
10221
10222 /* Set the output_section field so that lang_add_section
10223 does not create a lang_input_section structure for this
10224 section. Since there might be a symbol in the section
10225 being discarded, we must retain a pointer to the section
10226 which we are really going to use. */
10227 sec->output_section = bfd_abs_section_ptr;
10228 sec->kept_section = l->sec;
10229
10230 if (flags & SEC_GROUP)
10231 {
10232 asection *first = elf_next_in_group (sec);
10233 asection *s = first;
10234
10235 while (s != NULL)
10236 {
10237 s->output_section = bfd_abs_section_ptr;
10238 /* Record which group discards it. */
10239 s->kept_section = l->sec;
10240 s = elf_next_in_group (s);
10241 /* These lists are circular. */
10242 if (s == first)
10243 break;
10244 }
10245 }
10246
10247 return;
10248 }
10249 }
10250
10251 if (group)
10252 {
10253 /* If this is the member of a single member comdat group and the
10254 group hasn't be discarded, we check if it matches a linkonce
10255 section. We only record the discarded comdat group. Otherwise
10256 the undiscarded group will be discarded incorrectly later since
10257 itself has been recorded. */
10258 for (l = already_linked_list->entry; l != NULL; l = l->next)
10259 if ((l->sec->flags & SEC_GROUP) == 0
10260 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
10261 && bfd_elf_match_symbols_in_sections (l->sec,
10262 elf_next_in_group (sec)))
10263 {
10264 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
10265 elf_next_in_group (sec)->kept_section = l->sec;
10266 group->output_section = bfd_abs_section_ptr;
10267 break;
10268 }
10269 if (l == NULL)
10270 return;
10271 }
10272 else
10273 /* There is no direct match. But for linkonce section, we should
10274 check if there is a match with comdat group member. We always
10275 record the linkonce section, discarded or not. */
10276 for (l = already_linked_list->entry; l != NULL; l = l->next)
10277 if (l->sec->flags & SEC_GROUP)
10278 {
10279 asection *first = elf_next_in_group (l->sec);
10280
10281 if (first != NULL
10282 && elf_next_in_group (first) == first
10283 && bfd_elf_match_symbols_in_sections (first, sec))
10284 {
10285 sec->output_section = bfd_abs_section_ptr;
10286 sec->kept_section = l->sec;
10287 break;
10288 }
10289 }
10290
10291 /* This is the first section with this name. Record it. */
10292 bfd_section_already_linked_table_insert (already_linked_list, sec);
10293 }
10294
10295 bfd_boolean
10296 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
10297 {
10298 return sym->st_shndx == SHN_COMMON;
10299 }
10300
10301 unsigned int
10302 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
10303 {
10304 return SHN_COMMON;
10305 }
10306
10307 asection *
10308 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
10309 {
10310 return bfd_com_section_ptr;
10311 }
This page took 0.25495 seconds and 4 git commands to generate.