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