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