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