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