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