cf7d14ea36e1d3424122ee5eacbdbbd462fc9ebc
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29
30 bfd_boolean
31 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
32 {
33 flagword flags;
34 asection *s;
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 int ptralign;
39
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 return TRUE;
44
45 switch (bed->s->arch_size)
46 {
47 case 32:
48 ptralign = 2;
49 break;
50
51 case 64:
52 ptralign = 3;
53 break;
54
55 default:
56 bfd_set_error (bfd_error_bad_value);
57 return FALSE;
58 }
59
60 flags = (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 /* If we don't have a definition in a regular file, then we can't
2460 resolve locally. The sym is either undefined or dynamic. */
2461 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2462 return FALSE;
2463
2464 /* Forced local symbols resolve locally. */
2465 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2466 return TRUE;
2467
2468 /* As do non-dynamic symbols. */
2469 if (h->dynindx == -1)
2470 return TRUE;
2471
2472 /* At this point, we know the symbol is defined and dynamic. In an
2473 executable it must resolve locally, likewise when building symbolic
2474 shared libraries. */
2475 if (info->executable || info->symbolic)
2476 return TRUE;
2477
2478 /* Now deal with defined dynamic symbols in shared libraries. Ones
2479 with default visibility might not resolve locally. */
2480 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2481 return FALSE;
2482
2483 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2484 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2485 return TRUE;
2486
2487 /* Function pointer equality tests may require that STV_PROTECTED
2488 symbols be treated as dynamic symbols, even when we know that the
2489 dynamic linker will resolve them locally. */
2490 return local_protected;
2491 }
2492
2493 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2494 aligned. Returns the first TLS output section. */
2495
2496 struct bfd_section *
2497 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2498 {
2499 struct bfd_section *sec, *tls;
2500 unsigned int align = 0;
2501
2502 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2503 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2504 break;
2505 tls = sec;
2506
2507 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2508 if (sec->alignment_power > align)
2509 align = sec->alignment_power;
2510
2511 elf_hash_table (info)->tls_sec = tls;
2512
2513 /* Ensure the alignment of the first section is the largest alignment,
2514 so that the tls segment starts aligned. */
2515 if (tls != NULL)
2516 tls->alignment_power = align;
2517
2518 return tls;
2519 }
2520
2521 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2522 static bfd_boolean
2523 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2524 Elf_Internal_Sym *sym)
2525 {
2526 /* Local symbols do not count, but target specific ones might. */
2527 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2528 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2529 return FALSE;
2530
2531 /* Function symbols do not count. */
2532 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2533 return FALSE;
2534
2535 /* If the section is undefined, then so is the symbol. */
2536 if (sym->st_shndx == SHN_UNDEF)
2537 return FALSE;
2538
2539 /* If the symbol is defined in the common section, then
2540 it is a common definition and so does not count. */
2541 if (sym->st_shndx == SHN_COMMON)
2542 return FALSE;
2543
2544 /* If the symbol is in a target specific section then we
2545 must rely upon the backend to tell us what it is. */
2546 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2547 /* FIXME - this function is not coded yet:
2548
2549 return _bfd_is_global_symbol_definition (abfd, sym);
2550
2551 Instead for now assume that the definition is not global,
2552 Even if this is wrong, at least the linker will behave
2553 in the same way that it used to do. */
2554 return FALSE;
2555
2556 return TRUE;
2557 }
2558
2559 /* Search the symbol table of the archive element of the archive ABFD
2560 whose archive map contains a mention of SYMDEF, and determine if
2561 the symbol is defined in this element. */
2562 static bfd_boolean
2563 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2564 {
2565 Elf_Internal_Shdr * hdr;
2566 bfd_size_type symcount;
2567 bfd_size_type extsymcount;
2568 bfd_size_type extsymoff;
2569 Elf_Internal_Sym *isymbuf;
2570 Elf_Internal_Sym *isym;
2571 Elf_Internal_Sym *isymend;
2572 bfd_boolean result;
2573
2574 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2575 if (abfd == NULL)
2576 return FALSE;
2577
2578 if (! bfd_check_format (abfd, bfd_object))
2579 return FALSE;
2580
2581 /* If we have already included the element containing this symbol in the
2582 link then we do not need to include it again. Just claim that any symbol
2583 it contains is not a definition, so that our caller will not decide to
2584 (re)include this element. */
2585 if (abfd->archive_pass)
2586 return FALSE;
2587
2588 /* Select the appropriate symbol table. */
2589 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2590 hdr = &elf_tdata (abfd)->symtab_hdr;
2591 else
2592 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2593
2594 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2595
2596 /* The sh_info field of the symtab header tells us where the
2597 external symbols start. We don't care about the local symbols. */
2598 if (elf_bad_symtab (abfd))
2599 {
2600 extsymcount = symcount;
2601 extsymoff = 0;
2602 }
2603 else
2604 {
2605 extsymcount = symcount - hdr->sh_info;
2606 extsymoff = hdr->sh_info;
2607 }
2608
2609 if (extsymcount == 0)
2610 return FALSE;
2611
2612 /* Read in the symbol table. */
2613 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2614 NULL, NULL, NULL);
2615 if (isymbuf == NULL)
2616 return FALSE;
2617
2618 /* Scan the symbol table looking for SYMDEF. */
2619 result = FALSE;
2620 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2621 {
2622 const char *name;
2623
2624 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2625 isym->st_name);
2626 if (name == NULL)
2627 break;
2628
2629 if (strcmp (name, symdef->name) == 0)
2630 {
2631 result = is_global_data_symbol_definition (abfd, isym);
2632 break;
2633 }
2634 }
2635
2636 free (isymbuf);
2637
2638 return result;
2639 }
2640 \f
2641 /* Add an entry to the .dynamic table. */
2642
2643 bfd_boolean
2644 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2645 bfd_vma tag,
2646 bfd_vma val)
2647 {
2648 struct elf_link_hash_table *hash_table;
2649 const struct elf_backend_data *bed;
2650 asection *s;
2651 bfd_size_type newsize;
2652 bfd_byte *newcontents;
2653 Elf_Internal_Dyn dyn;
2654
2655 hash_table = elf_hash_table (info);
2656 if (! is_elf_hash_table (hash_table))
2657 return FALSE;
2658
2659 bed = get_elf_backend_data (hash_table->dynobj);
2660 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2661 BFD_ASSERT (s != NULL);
2662
2663 newsize = s->size + bed->s->sizeof_dyn;
2664 newcontents = bfd_realloc (s->contents, newsize);
2665 if (newcontents == NULL)
2666 return FALSE;
2667
2668 dyn.d_tag = tag;
2669 dyn.d_un.d_val = val;
2670 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2671
2672 s->size = newsize;
2673 s->contents = newcontents;
2674
2675 return TRUE;
2676 }
2677
2678 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2679 otherwise just check whether one already exists. Returns -1 on error,
2680 1 if a DT_NEEDED tag already exists, and 0 on success. */
2681
2682 static int
2683 elf_add_dt_needed_tag (struct bfd_link_info *info,
2684 const char *soname,
2685 bfd_boolean do_it)
2686 {
2687 struct elf_link_hash_table *hash_table;
2688 bfd_size_type oldsize;
2689 bfd_size_type strindex;
2690
2691 hash_table = elf_hash_table (info);
2692 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2693 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2694 if (strindex == (bfd_size_type) -1)
2695 return -1;
2696
2697 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2698 {
2699 asection *sdyn;
2700 const struct elf_backend_data *bed;
2701 bfd_byte *extdyn;
2702
2703 bed = get_elf_backend_data (hash_table->dynobj);
2704 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2705 BFD_ASSERT (sdyn != NULL);
2706
2707 for (extdyn = sdyn->contents;
2708 extdyn < sdyn->contents + sdyn->size;
2709 extdyn += bed->s->sizeof_dyn)
2710 {
2711 Elf_Internal_Dyn dyn;
2712
2713 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2714 if (dyn.d_tag == DT_NEEDED
2715 && dyn.d_un.d_val == strindex)
2716 {
2717 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2718 return 1;
2719 }
2720 }
2721 }
2722
2723 if (do_it)
2724 {
2725 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2726 return -1;
2727 }
2728 else
2729 /* We were just checking for existence of the tag. */
2730 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2731
2732 return 0;
2733 }
2734
2735 /* Sort symbol by value and section. */
2736
2737 static int
2738 elf_sort_symbol (const void *arg1, const void *arg2)
2739 {
2740 const struct elf_link_hash_entry *h1;
2741 const struct elf_link_hash_entry *h2;
2742
2743 h1 = *(const struct elf_link_hash_entry **) arg1;
2744 h2 = *(const struct elf_link_hash_entry **) arg2;
2745
2746 /* Coded this way to avoid bugs in various versions of gcc. */
2747 if (h1->root.u.def.value < h2->root.u.def.value)
2748 return -1;
2749 if (h1->root.u.def.value > h2->root.u.def.value)
2750 return 1;
2751 if (h1->root.u.def.section < h2->root.u.def.section)
2752 return -1;
2753 if (h1->root.u.def.section > h2->root.u.def.section)
2754 return 1;
2755
2756 return 0;
2757 }
2758
2759 /* This function is used to adjust offsets into .dynstr for
2760 dynamic symbols. This is called via elf_link_hash_traverse. */
2761
2762 static bfd_boolean
2763 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2764 {
2765 struct elf_strtab_hash *dynstr = data;
2766
2767 if (h->root.type == bfd_link_hash_warning)
2768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2769
2770 if (h->dynindx != -1)
2771 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2772 return TRUE;
2773 }
2774
2775 /* Assign string offsets in .dynstr, update all structures referencing
2776 them. */
2777
2778 static bfd_boolean
2779 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2780 {
2781 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2782 struct elf_link_local_dynamic_entry *entry;
2783 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2784 bfd *dynobj = hash_table->dynobj;
2785 asection *sdyn;
2786 bfd_size_type size;
2787 const struct elf_backend_data *bed;
2788 bfd_byte *extdyn;
2789
2790 _bfd_elf_strtab_finalize (dynstr);
2791 size = _bfd_elf_strtab_size (dynstr);
2792
2793 bed = get_elf_backend_data (dynobj);
2794 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2795 BFD_ASSERT (sdyn != NULL);
2796
2797 /* Update all .dynamic entries referencing .dynstr strings. */
2798 for (extdyn = sdyn->contents;
2799 extdyn < sdyn->contents + sdyn->size;
2800 extdyn += bed->s->sizeof_dyn)
2801 {
2802 Elf_Internal_Dyn dyn;
2803
2804 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2805 switch (dyn.d_tag)
2806 {
2807 case DT_STRSZ:
2808 dyn.d_un.d_val = size;
2809 break;
2810 case DT_NEEDED:
2811 case DT_SONAME:
2812 case DT_RPATH:
2813 case DT_RUNPATH:
2814 case DT_FILTER:
2815 case DT_AUXILIARY:
2816 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2817 break;
2818 default:
2819 continue;
2820 }
2821 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2822 }
2823
2824 /* Now update local dynamic symbols. */
2825 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2826 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2827 entry->isym.st_name);
2828
2829 /* And the rest of dynamic symbols. */
2830 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2831
2832 /* Adjust version definitions. */
2833 if (elf_tdata (output_bfd)->cverdefs)
2834 {
2835 asection *s;
2836 bfd_byte *p;
2837 bfd_size_type i;
2838 Elf_Internal_Verdef def;
2839 Elf_Internal_Verdaux defaux;
2840
2841 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2842 p = s->contents;
2843 do
2844 {
2845 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2846 &def);
2847 p += sizeof (Elf_External_Verdef);
2848 for (i = 0; i < def.vd_cnt; ++i)
2849 {
2850 _bfd_elf_swap_verdaux_in (output_bfd,
2851 (Elf_External_Verdaux *) p, &defaux);
2852 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2853 defaux.vda_name);
2854 _bfd_elf_swap_verdaux_out (output_bfd,
2855 &defaux, (Elf_External_Verdaux *) p);
2856 p += sizeof (Elf_External_Verdaux);
2857 }
2858 }
2859 while (def.vd_next);
2860 }
2861
2862 /* Adjust version references. */
2863 if (elf_tdata (output_bfd)->verref)
2864 {
2865 asection *s;
2866 bfd_byte *p;
2867 bfd_size_type i;
2868 Elf_Internal_Verneed need;
2869 Elf_Internal_Vernaux needaux;
2870
2871 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2872 p = s->contents;
2873 do
2874 {
2875 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2876 &need);
2877 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2878 _bfd_elf_swap_verneed_out (output_bfd, &need,
2879 (Elf_External_Verneed *) p);
2880 p += sizeof (Elf_External_Verneed);
2881 for (i = 0; i < need.vn_cnt; ++i)
2882 {
2883 _bfd_elf_swap_vernaux_in (output_bfd,
2884 (Elf_External_Vernaux *) p, &needaux);
2885 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2886 needaux.vna_name);
2887 _bfd_elf_swap_vernaux_out (output_bfd,
2888 &needaux,
2889 (Elf_External_Vernaux *) p);
2890 p += sizeof (Elf_External_Vernaux);
2891 }
2892 }
2893 while (need.vn_next);
2894 }
2895
2896 return TRUE;
2897 }
2898 \f
2899 /* Add symbols from an ELF object file to the linker hash table. */
2900
2901 static bfd_boolean
2902 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2903 {
2904 bfd_boolean (*add_symbol_hook)
2905 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2906 const char **, flagword *, asection **, bfd_vma *);
2907 bfd_boolean (*check_relocs)
2908 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2909 bfd_boolean (*check_directives)
2910 (bfd *, struct bfd_link_info *);
2911 bfd_boolean collect;
2912 Elf_Internal_Shdr *hdr;
2913 bfd_size_type symcount;
2914 bfd_size_type extsymcount;
2915 bfd_size_type extsymoff;
2916 struct elf_link_hash_entry **sym_hash;
2917 bfd_boolean dynamic;
2918 Elf_External_Versym *extversym = NULL;
2919 Elf_External_Versym *ever;
2920 struct elf_link_hash_entry *weaks;
2921 struct elf_link_hash_entry **nondeflt_vers = NULL;
2922 bfd_size_type nondeflt_vers_cnt = 0;
2923 Elf_Internal_Sym *isymbuf = NULL;
2924 Elf_Internal_Sym *isym;
2925 Elf_Internal_Sym *isymend;
2926 const struct elf_backend_data *bed;
2927 bfd_boolean add_needed;
2928 struct elf_link_hash_table * hash_table;
2929 bfd_size_type amt;
2930
2931 hash_table = elf_hash_table (info);
2932
2933 bed = get_elf_backend_data (abfd);
2934 add_symbol_hook = bed->elf_add_symbol_hook;
2935 collect = bed->collect;
2936
2937 if ((abfd->flags & DYNAMIC) == 0)
2938 dynamic = FALSE;
2939 else
2940 {
2941 dynamic = TRUE;
2942
2943 /* You can't use -r against a dynamic object. Also, there's no
2944 hope of using a dynamic object which does not exactly match
2945 the format of the output file. */
2946 if (info->relocatable
2947 || !is_elf_hash_table (hash_table)
2948 || hash_table->root.creator != abfd->xvec)
2949 {
2950 bfd_set_error (bfd_error_invalid_operation);
2951 goto error_return;
2952 }
2953 }
2954
2955 /* As a GNU extension, any input sections which are named
2956 .gnu.warning.SYMBOL are treated as warning symbols for the given
2957 symbol. This differs from .gnu.warning sections, which generate
2958 warnings when they are included in an output file. */
2959 if (info->executable)
2960 {
2961 asection *s;
2962
2963 for (s = abfd->sections; s != NULL; s = s->next)
2964 {
2965 const char *name;
2966
2967 name = bfd_get_section_name (abfd, s);
2968 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2969 {
2970 char *msg;
2971 bfd_size_type sz;
2972 bfd_size_type prefix_len;
2973 const char * gnu_warning_prefix = _("warning: ");
2974
2975 name += sizeof ".gnu.warning." - 1;
2976
2977 /* If this is a shared object, then look up the symbol
2978 in the hash table. If it is there, and it is already
2979 been defined, then we will not be using the entry
2980 from this shared object, so we don't need to warn.
2981 FIXME: If we see the definition in a regular object
2982 later on, we will warn, but we shouldn't. The only
2983 fix is to keep track of what warnings we are supposed
2984 to emit, and then handle them all at the end of the
2985 link. */
2986 if (dynamic)
2987 {
2988 struct elf_link_hash_entry *h;
2989
2990 h = elf_link_hash_lookup (hash_table, name,
2991 FALSE, FALSE, TRUE);
2992
2993 /* FIXME: What about bfd_link_hash_common? */
2994 if (h != NULL
2995 && (h->root.type == bfd_link_hash_defined
2996 || h->root.type == bfd_link_hash_defweak))
2997 {
2998 /* We don't want to issue this warning. Clobber
2999 the section size so that the warning does not
3000 get copied into the output file. */
3001 s->size = 0;
3002 continue;
3003 }
3004 }
3005
3006 sz = s->size;
3007 prefix_len = strlen (gnu_warning_prefix);
3008 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3009 if (msg == NULL)
3010 goto error_return;
3011
3012 strcpy (msg, gnu_warning_prefix);
3013 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3014 goto error_return;
3015
3016 msg[prefix_len + sz] = '\0';
3017
3018 if (! (_bfd_generic_link_add_one_symbol
3019 (info, abfd, name, BSF_WARNING, s, 0, msg,
3020 FALSE, collect, NULL)))
3021 goto error_return;
3022
3023 if (! info->relocatable)
3024 {
3025 /* Clobber the section size so that the warning does
3026 not get copied into the output file. */
3027 s->size = 0;
3028 }
3029 }
3030 }
3031 }
3032
3033 add_needed = TRUE;
3034 if (! dynamic)
3035 {
3036 /* If we are creating a shared library, create all the dynamic
3037 sections immediately. We need to attach them to something,
3038 so we attach them to this BFD, provided it is the right
3039 format. FIXME: If there are no input BFD's of the same
3040 format as the output, we can't make a shared library. */
3041 if (info->shared
3042 && is_elf_hash_table (hash_table)
3043 && hash_table->root.creator == abfd->xvec
3044 && ! hash_table->dynamic_sections_created)
3045 {
3046 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3047 goto error_return;
3048 }
3049 }
3050 else if (!is_elf_hash_table (hash_table))
3051 goto error_return;
3052 else
3053 {
3054 asection *s;
3055 const char *soname = NULL;
3056 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3057 int ret;
3058
3059 /* ld --just-symbols and dynamic objects don't mix very well.
3060 Test for --just-symbols by looking at info set up by
3061 _bfd_elf_link_just_syms. */
3062 if ((s = abfd->sections) != NULL
3063 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3064 goto error_return;
3065
3066 /* If this dynamic lib was specified on the command line with
3067 --as-needed in effect, then we don't want to add a DT_NEEDED
3068 tag unless the lib is actually used. Similary for libs brought
3069 in by another lib's DT_NEEDED. */
3070 add_needed = elf_dyn_lib_class (abfd) == DYN_NORMAL;
3071
3072 s = bfd_get_section_by_name (abfd, ".dynamic");
3073 if (s != NULL)
3074 {
3075 bfd_byte *dynbuf;
3076 bfd_byte *extdyn;
3077 int elfsec;
3078 unsigned long shlink;
3079
3080 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3081 goto error_free_dyn;
3082
3083 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3084 if (elfsec == -1)
3085 goto error_free_dyn;
3086 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3087
3088 for (extdyn = dynbuf;
3089 extdyn < dynbuf + s->size;
3090 extdyn += bed->s->sizeof_dyn)
3091 {
3092 Elf_Internal_Dyn dyn;
3093
3094 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3095 if (dyn.d_tag == DT_SONAME)
3096 {
3097 unsigned int tagv = dyn.d_un.d_val;
3098 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3099 if (soname == NULL)
3100 goto error_free_dyn;
3101 }
3102 if (dyn.d_tag == DT_NEEDED)
3103 {
3104 struct bfd_link_needed_list *n, **pn;
3105 char *fnm, *anm;
3106 unsigned int tagv = dyn.d_un.d_val;
3107
3108 amt = sizeof (struct bfd_link_needed_list);
3109 n = bfd_alloc (abfd, amt);
3110 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3111 if (n == NULL || fnm == NULL)
3112 goto error_free_dyn;
3113 amt = strlen (fnm) + 1;
3114 anm = bfd_alloc (abfd, amt);
3115 if (anm == NULL)
3116 goto error_free_dyn;
3117 memcpy (anm, fnm, amt);
3118 n->name = anm;
3119 n->by = abfd;
3120 n->next = NULL;
3121 for (pn = & hash_table->needed;
3122 *pn != NULL;
3123 pn = &(*pn)->next)
3124 ;
3125 *pn = n;
3126 }
3127 if (dyn.d_tag == DT_RUNPATH)
3128 {
3129 struct bfd_link_needed_list *n, **pn;
3130 char *fnm, *anm;
3131 unsigned int tagv = dyn.d_un.d_val;
3132
3133 amt = sizeof (struct bfd_link_needed_list);
3134 n = bfd_alloc (abfd, amt);
3135 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3136 if (n == NULL || fnm == NULL)
3137 goto error_free_dyn;
3138 amt = strlen (fnm) + 1;
3139 anm = bfd_alloc (abfd, amt);
3140 if (anm == NULL)
3141 goto error_free_dyn;
3142 memcpy (anm, fnm, amt);
3143 n->name = anm;
3144 n->by = abfd;
3145 n->next = NULL;
3146 for (pn = & runpath;
3147 *pn != NULL;
3148 pn = &(*pn)->next)
3149 ;
3150 *pn = n;
3151 }
3152 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3153 if (!runpath && dyn.d_tag == DT_RPATH)
3154 {
3155 struct bfd_link_needed_list *n, **pn;
3156 char *fnm, *anm;
3157 unsigned int tagv = dyn.d_un.d_val;
3158
3159 amt = sizeof (struct bfd_link_needed_list);
3160 n = bfd_alloc (abfd, amt);
3161 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3162 if (n == NULL || fnm == NULL)
3163 goto error_free_dyn;
3164 amt = strlen (fnm) + 1;
3165 anm = bfd_alloc (abfd, amt);
3166 if (anm == NULL)
3167 {
3168 error_free_dyn:
3169 free (dynbuf);
3170 goto error_return;
3171 }
3172 memcpy (anm, fnm, amt);
3173 n->name = anm;
3174 n->by = abfd;
3175 n->next = NULL;
3176 for (pn = & rpath;
3177 *pn != NULL;
3178 pn = &(*pn)->next)
3179 ;
3180 *pn = n;
3181 }
3182 }
3183
3184 free (dynbuf);
3185 }
3186
3187 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3188 frees all more recently bfd_alloc'd blocks as well. */
3189 if (runpath)
3190 rpath = runpath;
3191
3192 if (rpath)
3193 {
3194 struct bfd_link_needed_list **pn;
3195 for (pn = & hash_table->runpath;
3196 *pn != NULL;
3197 pn = &(*pn)->next)
3198 ;
3199 *pn = rpath;
3200 }
3201
3202 /* We do not want to include any of the sections in a dynamic
3203 object in the output file. We hack by simply clobbering the
3204 list of sections in the BFD. This could be handled more
3205 cleanly by, say, a new section flag; the existing
3206 SEC_NEVER_LOAD flag is not the one we want, because that one
3207 still implies that the section takes up space in the output
3208 file. */
3209 bfd_section_list_clear (abfd);
3210
3211 /* If this is the first dynamic object found in the link, create
3212 the special sections required for dynamic linking. */
3213 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3214 goto error_return;
3215
3216 /* Find the name to use in a DT_NEEDED entry that refers to this
3217 object. If the object has a DT_SONAME entry, we use it.
3218 Otherwise, if the generic linker stuck something in
3219 elf_dt_name, we use that. Otherwise, we just use the file
3220 name. */
3221 if (soname == NULL || *soname == '\0')
3222 {
3223 soname = elf_dt_name (abfd);
3224 if (soname == NULL || *soname == '\0')
3225 soname = bfd_get_filename (abfd);
3226 }
3227
3228 /* Save the SONAME because sometimes the linker emulation code
3229 will need to know it. */
3230 elf_dt_name (abfd) = soname;
3231
3232 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3233 if (ret < 0)
3234 goto error_return;
3235
3236 /* If we have already included this dynamic object in the
3237 link, just ignore it. There is no reason to include a
3238 particular dynamic object more than once. */
3239 if (ret > 0)
3240 return TRUE;
3241 }
3242
3243 /* If this is a dynamic object, we always link against the .dynsym
3244 symbol table, not the .symtab symbol table. The dynamic linker
3245 will only see the .dynsym symbol table, so there is no reason to
3246 look at .symtab for a dynamic object. */
3247
3248 if (! dynamic || elf_dynsymtab (abfd) == 0)
3249 hdr = &elf_tdata (abfd)->symtab_hdr;
3250 else
3251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3252
3253 symcount = hdr->sh_size / bed->s->sizeof_sym;
3254
3255 /* The sh_info field of the symtab header tells us where the
3256 external symbols start. We don't care about the local symbols at
3257 this point. */
3258 if (elf_bad_symtab (abfd))
3259 {
3260 extsymcount = symcount;
3261 extsymoff = 0;
3262 }
3263 else
3264 {
3265 extsymcount = symcount - hdr->sh_info;
3266 extsymoff = hdr->sh_info;
3267 }
3268
3269 sym_hash = NULL;
3270 if (extsymcount != 0)
3271 {
3272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3273 NULL, NULL, NULL);
3274 if (isymbuf == NULL)
3275 goto error_return;
3276
3277 /* We store a pointer to the hash table entry for each external
3278 symbol. */
3279 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3280 sym_hash = bfd_alloc (abfd, amt);
3281 if (sym_hash == NULL)
3282 goto error_free_sym;
3283 elf_sym_hashes (abfd) = sym_hash;
3284 }
3285
3286 if (dynamic)
3287 {
3288 /* Read in any version definitions. */
3289 if (! _bfd_elf_slurp_version_tables (abfd))
3290 goto error_free_sym;
3291
3292 /* Read in the symbol versions, but don't bother to convert them
3293 to internal format. */
3294 if (elf_dynversym (abfd) != 0)
3295 {
3296 Elf_Internal_Shdr *versymhdr;
3297
3298 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3299 extversym = bfd_malloc (versymhdr->sh_size);
3300 if (extversym == NULL)
3301 goto error_free_sym;
3302 amt = versymhdr->sh_size;
3303 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3304 || bfd_bread (extversym, amt, abfd) != amt)
3305 goto error_free_vers;
3306 }
3307 }
3308
3309 weaks = NULL;
3310
3311 ever = extversym != NULL ? extversym + extsymoff : NULL;
3312 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3313 isym < isymend;
3314 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3315 {
3316 int bind;
3317 bfd_vma value;
3318 asection *sec;
3319 flagword flags;
3320 const char *name;
3321 struct elf_link_hash_entry *h;
3322 bfd_boolean definition;
3323 bfd_boolean size_change_ok;
3324 bfd_boolean type_change_ok;
3325 bfd_boolean new_weakdef;
3326 bfd_boolean override;
3327 unsigned int old_alignment;
3328 bfd *old_bfd;
3329
3330 override = FALSE;
3331
3332 flags = BSF_NO_FLAGS;
3333 sec = NULL;
3334 value = isym->st_value;
3335 *sym_hash = NULL;
3336
3337 bind = ELF_ST_BIND (isym->st_info);
3338 if (bind == STB_LOCAL)
3339 {
3340 /* This should be impossible, since ELF requires that all
3341 global symbols follow all local symbols, and that sh_info
3342 point to the first global symbol. Unfortunately, Irix 5
3343 screws this up. */
3344 continue;
3345 }
3346 else if (bind == STB_GLOBAL)
3347 {
3348 if (isym->st_shndx != SHN_UNDEF
3349 && isym->st_shndx != SHN_COMMON)
3350 flags = BSF_GLOBAL;
3351 }
3352 else if (bind == STB_WEAK)
3353 flags = BSF_WEAK;
3354 else
3355 {
3356 /* Leave it up to the processor backend. */
3357 }
3358
3359 if (isym->st_shndx == SHN_UNDEF)
3360 sec = bfd_und_section_ptr;
3361 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3362 {
3363 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3364 if (sec == NULL)
3365 sec = bfd_abs_section_ptr;
3366 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3367 value -= sec->vma;
3368 }
3369 else if (isym->st_shndx == SHN_ABS)
3370 sec = bfd_abs_section_ptr;
3371 else if (isym->st_shndx == SHN_COMMON)
3372 {
3373 sec = bfd_com_section_ptr;
3374 /* What ELF calls the size we call the value. What ELF
3375 calls the value we call the alignment. */
3376 value = isym->st_size;
3377 }
3378 else
3379 {
3380 /* Leave it up to the processor backend. */
3381 }
3382
3383 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3384 isym->st_name);
3385 if (name == NULL)
3386 goto error_free_vers;
3387
3388 if (isym->st_shndx == SHN_COMMON
3389 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3390 {
3391 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3392
3393 if (tcomm == NULL)
3394 {
3395 tcomm = bfd_make_section (abfd, ".tcommon");
3396 if (tcomm == NULL
3397 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3398 | SEC_IS_COMMON
3399 | SEC_LINKER_CREATED
3400 | SEC_THREAD_LOCAL)))
3401 goto error_free_vers;
3402 }
3403 sec = tcomm;
3404 }
3405 else if (add_symbol_hook)
3406 {
3407 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3408 &value))
3409 goto error_free_vers;
3410
3411 /* The hook function sets the name to NULL if this symbol
3412 should be skipped for some reason. */
3413 if (name == NULL)
3414 continue;
3415 }
3416
3417 /* Sanity check that all possibilities were handled. */
3418 if (sec == NULL)
3419 {
3420 bfd_set_error (bfd_error_bad_value);
3421 goto error_free_vers;
3422 }
3423
3424 if (bfd_is_und_section (sec)
3425 || bfd_is_com_section (sec))
3426 definition = FALSE;
3427 else
3428 definition = TRUE;
3429
3430 size_change_ok = FALSE;
3431 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3432 old_alignment = 0;
3433 old_bfd = NULL;
3434
3435 if (is_elf_hash_table (hash_table))
3436 {
3437 Elf_Internal_Versym iver;
3438 unsigned int vernum = 0;
3439 bfd_boolean skip;
3440
3441 if (ever != NULL)
3442 {
3443 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3444 vernum = iver.vs_vers & VERSYM_VERSION;
3445
3446 /* If this is a hidden symbol, or if it is not version
3447 1, we append the version name to the symbol name.
3448 However, we do not modify a non-hidden absolute
3449 symbol, because it might be the version symbol
3450 itself. FIXME: What if it isn't? */
3451 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3452 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3453 {
3454 const char *verstr;
3455 size_t namelen, verlen, newlen;
3456 char *newname, *p;
3457
3458 if (isym->st_shndx != SHN_UNDEF)
3459 {
3460 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3461 {
3462 (*_bfd_error_handler)
3463 (_("%s: %s: invalid version %u (max %d)"),
3464 bfd_archive_filename (abfd), name, vernum,
3465 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3466 bfd_set_error (bfd_error_bad_value);
3467 goto error_free_vers;
3468 }
3469 else if (vernum > 1)
3470 verstr =
3471 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3472 else
3473 verstr = "";
3474 }
3475 else
3476 {
3477 /* We cannot simply test for the number of
3478 entries in the VERNEED section since the
3479 numbers for the needed versions do not start
3480 at 0. */
3481 Elf_Internal_Verneed *t;
3482
3483 verstr = NULL;
3484 for (t = elf_tdata (abfd)->verref;
3485 t != NULL;
3486 t = t->vn_nextref)
3487 {
3488 Elf_Internal_Vernaux *a;
3489
3490 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3491 {
3492 if (a->vna_other == vernum)
3493 {
3494 verstr = a->vna_nodename;
3495 break;
3496 }
3497 }
3498 if (a != NULL)
3499 break;
3500 }
3501 if (verstr == NULL)
3502 {
3503 (*_bfd_error_handler)
3504 (_("%s: %s: invalid needed version %d"),
3505 bfd_archive_filename (abfd), name, vernum);
3506 bfd_set_error (bfd_error_bad_value);
3507 goto error_free_vers;
3508 }
3509 }
3510
3511 namelen = strlen (name);
3512 verlen = strlen (verstr);
3513 newlen = namelen + verlen + 2;
3514 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3515 && isym->st_shndx != SHN_UNDEF)
3516 ++newlen;
3517
3518 newname = bfd_alloc (abfd, newlen);
3519 if (newname == NULL)
3520 goto error_free_vers;
3521 memcpy (newname, name, namelen);
3522 p = newname + namelen;
3523 *p++ = ELF_VER_CHR;
3524 /* If this is a defined non-hidden version symbol,
3525 we add another @ to the name. This indicates the
3526 default version of the symbol. */
3527 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3528 && isym->st_shndx != SHN_UNDEF)
3529 *p++ = ELF_VER_CHR;
3530 memcpy (p, verstr, verlen + 1);
3531
3532 name = newname;
3533 }
3534 }
3535
3536 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3537 sym_hash, &skip, &override,
3538 &type_change_ok, &size_change_ok))
3539 goto error_free_vers;
3540
3541 if (skip)
3542 continue;
3543
3544 if (override)
3545 definition = FALSE;
3546
3547 h = *sym_hash;
3548 while (h->root.type == bfd_link_hash_indirect
3549 || h->root.type == bfd_link_hash_warning)
3550 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3551
3552 /* Remember the old alignment if this is a common symbol, so
3553 that we don't reduce the alignment later on. We can't
3554 check later, because _bfd_generic_link_add_one_symbol
3555 will set a default for the alignment which we want to
3556 override. We also remember the old bfd where the existing
3557 definition comes from. */
3558 switch (h->root.type)
3559 {
3560 default:
3561 break;
3562
3563 case bfd_link_hash_defined:
3564 case bfd_link_hash_defweak:
3565 old_bfd = h->root.u.def.section->owner;
3566 break;
3567
3568 case bfd_link_hash_common:
3569 old_bfd = h->root.u.c.p->section->owner;
3570 old_alignment = h->root.u.c.p->alignment_power;
3571 break;
3572 }
3573
3574 if (elf_tdata (abfd)->verdef != NULL
3575 && ! override
3576 && vernum > 1
3577 && definition)
3578 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3579 }
3580
3581 if (! (_bfd_generic_link_add_one_symbol
3582 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3583 (struct bfd_link_hash_entry **) sym_hash)))
3584 goto error_free_vers;
3585
3586 h = *sym_hash;
3587 while (h->root.type == bfd_link_hash_indirect
3588 || h->root.type == bfd_link_hash_warning)
3589 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3590 *sym_hash = h;
3591
3592 new_weakdef = FALSE;
3593 if (dynamic
3594 && definition
3595 && (flags & BSF_WEAK) != 0
3596 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3597 && is_elf_hash_table (hash_table)
3598 && h->weakdef == NULL)
3599 {
3600 /* Keep a list of all weak defined non function symbols from
3601 a dynamic object, using the weakdef field. Later in this
3602 function we will set the weakdef field to the correct
3603 value. We only put non-function symbols from dynamic
3604 objects on this list, because that happens to be the only
3605 time we need to know the normal symbol corresponding to a
3606 weak symbol, and the information is time consuming to
3607 figure out. If the weakdef field is not already NULL,
3608 then this symbol was already defined by some previous
3609 dynamic object, and we will be using that previous
3610 definition anyhow. */
3611
3612 h->weakdef = weaks;
3613 weaks = h;
3614 new_weakdef = TRUE;
3615 }
3616
3617 /* Set the alignment of a common symbol. */
3618 if (isym->st_shndx == SHN_COMMON
3619 && h->root.type == bfd_link_hash_common)
3620 {
3621 unsigned int align;
3622
3623 align = bfd_log2 (isym->st_value);
3624 if (align > old_alignment
3625 /* Permit an alignment power of zero if an alignment of one
3626 is specified and no other alignments have been specified. */
3627 || (isym->st_value == 1 && old_alignment == 0))
3628 h->root.u.c.p->alignment_power = align;
3629 else
3630 h->root.u.c.p->alignment_power = old_alignment;
3631 }
3632
3633 if (is_elf_hash_table (hash_table))
3634 {
3635 int old_flags;
3636 bfd_boolean dynsym;
3637 int new_flag;
3638
3639 /* Check the alignment when a common symbol is involved. This
3640 can change when a common symbol is overridden by a normal
3641 definition or a common symbol is ignored due to the old
3642 normal definition. We need to make sure the maximum
3643 alignment is maintained. */
3644 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3645 && h->root.type != bfd_link_hash_common)
3646 {
3647 unsigned int common_align;
3648 unsigned int normal_align;
3649 unsigned int symbol_align;
3650 bfd *normal_bfd;
3651 bfd *common_bfd;
3652
3653 symbol_align = ffs (h->root.u.def.value) - 1;
3654 if (h->root.u.def.section->owner != NULL
3655 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3656 {
3657 normal_align = h->root.u.def.section->alignment_power;
3658 if (normal_align > symbol_align)
3659 normal_align = symbol_align;
3660 }
3661 else
3662 normal_align = symbol_align;
3663
3664 if (old_alignment)
3665 {
3666 common_align = old_alignment;
3667 common_bfd = old_bfd;
3668 normal_bfd = abfd;
3669 }
3670 else
3671 {
3672 common_align = bfd_log2 (isym->st_value);
3673 common_bfd = abfd;
3674 normal_bfd = old_bfd;
3675 }
3676
3677 if (normal_align < common_align)
3678 (*_bfd_error_handler)
3679 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
3680 1 << normal_align,
3681 name,
3682 bfd_archive_filename (normal_bfd),
3683 1 << common_align,
3684 bfd_archive_filename (common_bfd));
3685 }
3686
3687 /* Remember the symbol size and type. */
3688 if (isym->st_size != 0
3689 && (definition || h->size == 0))
3690 {
3691 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3692 (*_bfd_error_handler)
3693 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
3694 name, (unsigned long) h->size,
3695 bfd_archive_filename (old_bfd),
3696 (unsigned long) isym->st_size,
3697 bfd_archive_filename (abfd));
3698
3699 h->size = isym->st_size;
3700 }
3701
3702 /* If this is a common symbol, then we always want H->SIZE
3703 to be the size of the common symbol. The code just above
3704 won't fix the size if a common symbol becomes larger. We
3705 don't warn about a size change here, because that is
3706 covered by --warn-common. */
3707 if (h->root.type == bfd_link_hash_common)
3708 h->size = h->root.u.c.size;
3709
3710 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3711 && (definition || h->type == STT_NOTYPE))
3712 {
3713 if (h->type != STT_NOTYPE
3714 && h->type != ELF_ST_TYPE (isym->st_info)
3715 && ! type_change_ok)
3716 (*_bfd_error_handler)
3717 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
3718 name, h->type, ELF_ST_TYPE (isym->st_info),
3719 bfd_archive_filename (abfd));
3720
3721 h->type = ELF_ST_TYPE (isym->st_info);
3722 }
3723
3724 /* If st_other has a processor-specific meaning, specific
3725 code might be needed here. We never merge the visibility
3726 attribute with the one from a dynamic object. */
3727 if (bed->elf_backend_merge_symbol_attribute)
3728 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3729 dynamic);
3730
3731 if (isym->st_other != 0 && !dynamic)
3732 {
3733 unsigned char hvis, symvis, other, nvis;
3734
3735 /* Take the balance of OTHER from the definition. */
3736 other = (definition ? isym->st_other : h->other);
3737 other &= ~ ELF_ST_VISIBILITY (-1);
3738
3739 /* Combine visibilities, using the most constraining one. */
3740 hvis = ELF_ST_VISIBILITY (h->other);
3741 symvis = ELF_ST_VISIBILITY (isym->st_other);
3742 if (! hvis)
3743 nvis = symvis;
3744 else if (! symvis)
3745 nvis = hvis;
3746 else
3747 nvis = hvis < symvis ? hvis : symvis;
3748
3749 h->other = other | nvis;
3750 }
3751
3752 /* Set a flag in the hash table entry indicating the type of
3753 reference or definition we just found. Keep a count of
3754 the number of dynamic symbols we find. A dynamic symbol
3755 is one which is referenced or defined by both a regular
3756 object and a shared object. */
3757 old_flags = h->elf_link_hash_flags;
3758 dynsym = FALSE;
3759 if (! dynamic)
3760 {
3761 if (! definition)
3762 {
3763 new_flag = ELF_LINK_HASH_REF_REGULAR;
3764 if (bind != STB_WEAK)
3765 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3766 }
3767 else
3768 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3769 if (! info->executable
3770 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3771 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3772 dynsym = TRUE;
3773 }
3774 else
3775 {
3776 if (! definition)
3777 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3778 else
3779 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3780 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3781 | ELF_LINK_HASH_REF_REGULAR)) != 0
3782 || (h->weakdef != NULL
3783 && ! new_weakdef
3784 && h->weakdef->dynindx != -1))
3785 dynsym = TRUE;
3786 }
3787
3788 h->elf_link_hash_flags |= new_flag;
3789
3790 /* Check to see if we need to add an indirect symbol for
3791 the default name. */
3792 if (definition || h->root.type == bfd_link_hash_common)
3793 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3794 &sec, &value, &dynsym,
3795 override))
3796 goto error_free_vers;
3797
3798 if (definition && !dynamic)
3799 {
3800 char *p = strchr (name, ELF_VER_CHR);
3801 if (p != NULL && p[1] != ELF_VER_CHR)
3802 {
3803 /* Queue non-default versions so that .symver x, x@FOO
3804 aliases can be checked. */
3805 if (! nondeflt_vers)
3806 {
3807 amt = (isymend - isym + 1)
3808 * sizeof (struct elf_link_hash_entry *);
3809 nondeflt_vers = bfd_malloc (amt);
3810 }
3811 nondeflt_vers [nondeflt_vers_cnt++] = h;
3812 }
3813 }
3814
3815 if (dynsym && h->dynindx == -1)
3816 {
3817 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3818 goto error_free_vers;
3819 if (h->weakdef != NULL
3820 && ! new_weakdef
3821 && h->weakdef->dynindx == -1)
3822 {
3823 if (! bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3824 goto error_free_vers;
3825 }
3826 }
3827 else if (dynsym && h->dynindx != -1)
3828 /* If the symbol already has a dynamic index, but
3829 visibility says it should not be visible, turn it into
3830 a local symbol. */
3831 switch (ELF_ST_VISIBILITY (h->other))
3832 {
3833 case STV_INTERNAL:
3834 case STV_HIDDEN:
3835 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3836 dynsym = FALSE;
3837 break;
3838 }
3839
3840 if (!add_needed
3841 && definition
3842 && dynsym
3843 && (h->elf_link_hash_flags
3844 & ELF_LINK_HASH_REF_REGULAR) != 0)
3845 {
3846 int ret;
3847 const char *soname = elf_dt_name (abfd);
3848
3849 /* A symbol from a library loaded via DT_NEEDED of some
3850 other library is referenced by a regular object.
3851 Add a DT_NEEDED entry for it. */
3852 add_needed = TRUE;
3853 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3854 if (ret < 0)
3855 goto error_free_vers;
3856
3857 BFD_ASSERT (ret == 0);
3858 }
3859 }
3860 }
3861
3862 /* Now that all the symbols from this input file are created, handle
3863 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3864 if (nondeflt_vers != NULL)
3865 {
3866 bfd_size_type cnt, symidx;
3867
3868 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3869 {
3870 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3871 char *shortname, *p;
3872
3873 p = strchr (h->root.root.string, ELF_VER_CHR);
3874 if (p == NULL
3875 || (h->root.type != bfd_link_hash_defined
3876 && h->root.type != bfd_link_hash_defweak))
3877 continue;
3878
3879 amt = p - h->root.root.string;
3880 shortname = bfd_malloc (amt + 1);
3881 memcpy (shortname, h->root.root.string, amt);
3882 shortname[amt] = '\0';
3883
3884 hi = (struct elf_link_hash_entry *)
3885 bfd_link_hash_lookup (&hash_table->root, shortname,
3886 FALSE, FALSE, FALSE);
3887 if (hi != NULL
3888 && hi->root.type == h->root.type
3889 && hi->root.u.def.value == h->root.u.def.value
3890 && hi->root.u.def.section == h->root.u.def.section)
3891 {
3892 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3893 hi->root.type = bfd_link_hash_indirect;
3894 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3895 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3896 sym_hash = elf_sym_hashes (abfd);
3897 if (sym_hash)
3898 for (symidx = 0; symidx < extsymcount; ++symidx)
3899 if (sym_hash[symidx] == hi)
3900 {
3901 sym_hash[symidx] = h;
3902 break;
3903 }
3904 }
3905 free (shortname);
3906 }
3907 free (nondeflt_vers);
3908 nondeflt_vers = NULL;
3909 }
3910
3911 if (extversym != NULL)
3912 {
3913 free (extversym);
3914 extversym = NULL;
3915 }
3916
3917 if (isymbuf != NULL)
3918 free (isymbuf);
3919 isymbuf = NULL;
3920
3921 /* Now set the weakdefs field correctly for all the weak defined
3922 symbols we found. The only way to do this is to search all the
3923 symbols. Since we only need the information for non functions in
3924 dynamic objects, that's the only time we actually put anything on
3925 the list WEAKS. We need this information so that if a regular
3926 object refers to a symbol defined weakly in a dynamic object, the
3927 real symbol in the dynamic object is also put in the dynamic
3928 symbols; we also must arrange for both symbols to point to the
3929 same memory location. We could handle the general case of symbol
3930 aliasing, but a general symbol alias can only be generated in
3931 assembler code, handling it correctly would be very time
3932 consuming, and other ELF linkers don't handle general aliasing
3933 either. */
3934 if (weaks != NULL)
3935 {
3936 struct elf_link_hash_entry **hpp;
3937 struct elf_link_hash_entry **hppend;
3938 struct elf_link_hash_entry **sorted_sym_hash;
3939 struct elf_link_hash_entry *h;
3940 size_t sym_count;
3941
3942 /* Since we have to search the whole symbol list for each weak
3943 defined symbol, search time for N weak defined symbols will be
3944 O(N^2). Binary search will cut it down to O(NlogN). */
3945 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3946 sorted_sym_hash = bfd_malloc (amt);
3947 if (sorted_sym_hash == NULL)
3948 goto error_return;
3949 sym_hash = sorted_sym_hash;
3950 hpp = elf_sym_hashes (abfd);
3951 hppend = hpp + extsymcount;
3952 sym_count = 0;
3953 for (; hpp < hppend; hpp++)
3954 {
3955 h = *hpp;
3956 if (h != NULL
3957 && h->root.type == bfd_link_hash_defined
3958 && h->type != STT_FUNC)
3959 {
3960 *sym_hash = h;
3961 sym_hash++;
3962 sym_count++;
3963 }
3964 }
3965
3966 qsort (sorted_sym_hash, sym_count,
3967 sizeof (struct elf_link_hash_entry *),
3968 elf_sort_symbol);
3969
3970 while (weaks != NULL)
3971 {
3972 struct elf_link_hash_entry *hlook;
3973 asection *slook;
3974 bfd_vma vlook;
3975 long ilook;
3976 size_t i, j, idx;
3977
3978 hlook = weaks;
3979 weaks = hlook->weakdef;
3980 hlook->weakdef = NULL;
3981
3982 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3983 || hlook->root.type == bfd_link_hash_defweak
3984 || hlook->root.type == bfd_link_hash_common
3985 || hlook->root.type == bfd_link_hash_indirect);
3986 slook = hlook->root.u.def.section;
3987 vlook = hlook->root.u.def.value;
3988
3989 ilook = -1;
3990 i = 0;
3991 j = sym_count;
3992 while (i < j)
3993 {
3994 bfd_signed_vma vdiff;
3995 idx = (i + j) / 2;
3996 h = sorted_sym_hash [idx];
3997 vdiff = vlook - h->root.u.def.value;
3998 if (vdiff < 0)
3999 j = idx;
4000 else if (vdiff > 0)
4001 i = idx + 1;
4002 else
4003 {
4004 long sdiff = slook->id - h->root.u.def.section->id;
4005 if (sdiff < 0)
4006 j = idx;
4007 else if (sdiff > 0)
4008 i = idx + 1;
4009 else
4010 {
4011 ilook = idx;
4012 break;
4013 }
4014 }
4015 }
4016
4017 /* We didn't find a value/section match. */
4018 if (ilook == -1)
4019 continue;
4020
4021 for (i = ilook; i < sym_count; i++)
4022 {
4023 h = sorted_sym_hash [i];
4024
4025 /* Stop if value or section doesn't match. */
4026 if (h->root.u.def.value != vlook
4027 || h->root.u.def.section != slook)
4028 break;
4029 else if (h != hlook)
4030 {
4031 hlook->weakdef = h;
4032
4033 /* If the weak definition is in the list of dynamic
4034 symbols, make sure the real definition is put
4035 there as well. */
4036 if (hlook->dynindx != -1 && h->dynindx == -1)
4037 {
4038 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4039 goto error_return;
4040 }
4041
4042 /* If the real definition is in the list of dynamic
4043 symbols, make sure the weak definition is put
4044 there as well. If we don't do this, then the
4045 dynamic loader might not merge the entries for the
4046 real definition and the weak definition. */
4047 if (h->dynindx != -1 && hlook->dynindx == -1)
4048 {
4049 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4050 goto error_return;
4051 }
4052 break;
4053 }
4054 }
4055 }
4056
4057 free (sorted_sym_hash);
4058 }
4059
4060 check_directives = get_elf_backend_data (abfd)->check_directives;
4061 if (check_directives)
4062 check_directives (abfd, info);
4063
4064 /* If this object is the same format as the output object, and it is
4065 not a shared library, then let the backend look through the
4066 relocs.
4067
4068 This is required to build global offset table entries and to
4069 arrange for dynamic relocs. It is not required for the
4070 particular common case of linking non PIC code, even when linking
4071 against shared libraries, but unfortunately there is no way of
4072 knowing whether an object file has been compiled PIC or not.
4073 Looking through the relocs is not particularly time consuming.
4074 The problem is that we must either (1) keep the relocs in memory,
4075 which causes the linker to require additional runtime memory or
4076 (2) read the relocs twice from the input file, which wastes time.
4077 This would be a good case for using mmap.
4078
4079 I have no idea how to handle linking PIC code into a file of a
4080 different format. It probably can't be done. */
4081 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4082 if (! dynamic
4083 && is_elf_hash_table (hash_table)
4084 && hash_table->root.creator == abfd->xvec
4085 && check_relocs != NULL)
4086 {
4087 asection *o;
4088
4089 for (o = abfd->sections; o != NULL; o = o->next)
4090 {
4091 Elf_Internal_Rela *internal_relocs;
4092 bfd_boolean ok;
4093
4094 if ((o->flags & SEC_RELOC) == 0
4095 || o->reloc_count == 0
4096 || ((info->strip == strip_all || info->strip == strip_debugger)
4097 && (o->flags & SEC_DEBUGGING) != 0)
4098 || bfd_is_abs_section (o->output_section))
4099 continue;
4100
4101 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4102 info->keep_memory);
4103 if (internal_relocs == NULL)
4104 goto error_return;
4105
4106 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4107
4108 if (elf_section_data (o)->relocs != internal_relocs)
4109 free (internal_relocs);
4110
4111 if (! ok)
4112 goto error_return;
4113 }
4114 }
4115
4116 /* If this is a non-traditional link, try to optimize the handling
4117 of the .stab/.stabstr sections. */
4118 if (! dynamic
4119 && ! info->traditional_format
4120 && is_elf_hash_table (hash_table)
4121 && (info->strip != strip_all && info->strip != strip_debugger))
4122 {
4123 asection *stabstr;
4124
4125 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4126 if (stabstr != NULL)
4127 {
4128 bfd_size_type string_offset = 0;
4129 asection *stab;
4130
4131 for (stab = abfd->sections; stab; stab = stab->next)
4132 if (strncmp (".stab", stab->name, 5) == 0
4133 && (!stab->name[5] ||
4134 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4135 && (stab->flags & SEC_MERGE) == 0
4136 && !bfd_is_abs_section (stab->output_section))
4137 {
4138 struct bfd_elf_section_data *secdata;
4139
4140 secdata = elf_section_data (stab);
4141 if (! _bfd_link_section_stabs (abfd,
4142 &hash_table->stab_info,
4143 stab, stabstr,
4144 &secdata->sec_info,
4145 &string_offset))
4146 goto error_return;
4147 if (secdata->sec_info)
4148 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4149 }
4150 }
4151 }
4152
4153 if (is_elf_hash_table (hash_table))
4154 {
4155 /* Add this bfd to the loaded list. */
4156 struct elf_link_loaded_list *n;
4157
4158 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4159 if (n == NULL)
4160 goto error_return;
4161 n->abfd = abfd;
4162 n->next = hash_table->loaded;
4163 hash_table->loaded = n;
4164 }
4165
4166 return TRUE;
4167
4168 error_free_vers:
4169 if (nondeflt_vers != NULL)
4170 free (nondeflt_vers);
4171 if (extversym != NULL)
4172 free (extversym);
4173 error_free_sym:
4174 if (isymbuf != NULL)
4175 free (isymbuf);
4176 error_return:
4177 return FALSE;
4178 }
4179
4180 /* Add symbols from an ELF archive file to the linker hash table. We
4181 don't use _bfd_generic_link_add_archive_symbols because of a
4182 problem which arises on UnixWare. The UnixWare libc.so is an
4183 archive which includes an entry libc.so.1 which defines a bunch of
4184 symbols. The libc.so archive also includes a number of other
4185 object files, which also define symbols, some of which are the same
4186 as those defined in libc.so.1. Correct linking requires that we
4187 consider each object file in turn, and include it if it defines any
4188 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4189 this; it looks through the list of undefined symbols, and includes
4190 any object file which defines them. When this algorithm is used on
4191 UnixWare, it winds up pulling in libc.so.1 early and defining a
4192 bunch of symbols. This means that some of the other objects in the
4193 archive are not included in the link, which is incorrect since they
4194 precede libc.so.1 in the archive.
4195
4196 Fortunately, ELF archive handling is simpler than that done by
4197 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4198 oddities. In ELF, if we find a symbol in the archive map, and the
4199 symbol is currently undefined, we know that we must pull in that
4200 object file.
4201
4202 Unfortunately, we do have to make multiple passes over the symbol
4203 table until nothing further is resolved. */
4204
4205 static bfd_boolean
4206 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4207 {
4208 symindex c;
4209 bfd_boolean *defined = NULL;
4210 bfd_boolean *included = NULL;
4211 carsym *symdefs;
4212 bfd_boolean loop;
4213 bfd_size_type amt;
4214
4215 if (! bfd_has_map (abfd))
4216 {
4217 /* An empty archive is a special case. */
4218 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4219 return TRUE;
4220 bfd_set_error (bfd_error_no_armap);
4221 return FALSE;
4222 }
4223
4224 /* Keep track of all symbols we know to be already defined, and all
4225 files we know to be already included. This is to speed up the
4226 second and subsequent passes. */
4227 c = bfd_ardata (abfd)->symdef_count;
4228 if (c == 0)
4229 return TRUE;
4230 amt = c;
4231 amt *= sizeof (bfd_boolean);
4232 defined = bfd_zmalloc (amt);
4233 included = bfd_zmalloc (amt);
4234 if (defined == NULL || included == NULL)
4235 goto error_return;
4236
4237 symdefs = bfd_ardata (abfd)->symdefs;
4238
4239 do
4240 {
4241 file_ptr last;
4242 symindex i;
4243 carsym *symdef;
4244 carsym *symdefend;
4245
4246 loop = FALSE;
4247 last = -1;
4248
4249 symdef = symdefs;
4250 symdefend = symdef + c;
4251 for (i = 0; symdef < symdefend; symdef++, i++)
4252 {
4253 struct elf_link_hash_entry *h;
4254 bfd *element;
4255 struct bfd_link_hash_entry *undefs_tail;
4256 symindex mark;
4257
4258 if (defined[i] || included[i])
4259 continue;
4260 if (symdef->file_offset == last)
4261 {
4262 included[i] = TRUE;
4263 continue;
4264 }
4265
4266 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
4267 FALSE, FALSE, FALSE);
4268
4269 if (h == NULL)
4270 {
4271 char *p, *copy;
4272 size_t len, first;
4273
4274 /* If this is a default version (the name contains @@),
4275 look up the symbol again with only one `@' as well
4276 as without the version. The effect is that references
4277 to the symbol with and without the version will be
4278 matched by the default symbol in the archive. */
4279
4280 p = strchr (symdef->name, ELF_VER_CHR);
4281 if (p == NULL || p[1] != ELF_VER_CHR)
4282 continue;
4283
4284 /* First check with only one `@'. */
4285 len = strlen (symdef->name);
4286 copy = bfd_alloc (abfd, len);
4287 if (copy == NULL)
4288 goto error_return;
4289 first = p - symdef->name + 1;
4290 memcpy (copy, symdef->name, first);
4291 memcpy (copy + first, symdef->name + first + 1, len - first);
4292
4293 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4294 FALSE, FALSE, FALSE);
4295
4296 if (h == NULL)
4297 {
4298 /* We also need to check references to the symbol
4299 without the version. */
4300
4301 copy[first - 1] = '\0';
4302 h = elf_link_hash_lookup (elf_hash_table (info),
4303 copy, FALSE, FALSE, FALSE);
4304 }
4305
4306 bfd_release (abfd, copy);
4307 }
4308
4309 if (h == NULL)
4310 continue;
4311
4312 if (h->root.type == bfd_link_hash_common)
4313 {
4314 /* We currently have a common symbol. The archive map contains
4315 a reference to this symbol, so we may want to include it. We
4316 only want to include it however, if this archive element
4317 contains a definition of the symbol, not just another common
4318 declaration of it.
4319
4320 Unfortunately some archivers (including GNU ar) will put
4321 declarations of common symbols into their archive maps, as
4322 well as real definitions, so we cannot just go by the archive
4323 map alone. Instead we must read in the element's symbol
4324 table and check that to see what kind of symbol definition
4325 this is. */
4326 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4327 continue;
4328 }
4329 else if (h->root.type != bfd_link_hash_undefined)
4330 {
4331 if (h->root.type != bfd_link_hash_undefweak)
4332 defined[i] = TRUE;
4333 continue;
4334 }
4335
4336 /* We need to include this archive member. */
4337 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4338 if (element == NULL)
4339 goto error_return;
4340
4341 if (! bfd_check_format (element, bfd_object))
4342 goto error_return;
4343
4344 /* Doublecheck that we have not included this object
4345 already--it should be impossible, but there may be
4346 something wrong with the archive. */
4347 if (element->archive_pass != 0)
4348 {
4349 bfd_set_error (bfd_error_bad_value);
4350 goto error_return;
4351 }
4352 element->archive_pass = 1;
4353
4354 undefs_tail = info->hash->undefs_tail;
4355
4356 if (! (*info->callbacks->add_archive_element) (info, element,
4357 symdef->name))
4358 goto error_return;
4359 if (! bfd_link_add_symbols (element, info))
4360 goto error_return;
4361
4362 /* If there are any new undefined symbols, we need to make
4363 another pass through the archive in order to see whether
4364 they can be defined. FIXME: This isn't perfect, because
4365 common symbols wind up on undefs_tail and because an
4366 undefined symbol which is defined later on in this pass
4367 does not require another pass. This isn't a bug, but it
4368 does make the code less efficient than it could be. */
4369 if (undefs_tail != info->hash->undefs_tail)
4370 loop = TRUE;
4371
4372 /* Look backward to mark all symbols from this object file
4373 which we have already seen in this pass. */
4374 mark = i;
4375 do
4376 {
4377 included[mark] = TRUE;
4378 if (mark == 0)
4379 break;
4380 --mark;
4381 }
4382 while (symdefs[mark].file_offset == symdef->file_offset);
4383
4384 /* We mark subsequent symbols from this object file as we go
4385 on through the loop. */
4386 last = symdef->file_offset;
4387 }
4388 }
4389 while (loop);
4390
4391 free (defined);
4392 free (included);
4393
4394 return TRUE;
4395
4396 error_return:
4397 if (defined != NULL)
4398 free (defined);
4399 if (included != NULL)
4400 free (included);
4401 return FALSE;
4402 }
4403
4404 /* Given an ELF BFD, add symbols to the global hash table as
4405 appropriate. */
4406
4407 bfd_boolean
4408 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4409 {
4410 switch (bfd_get_format (abfd))
4411 {
4412 case bfd_object:
4413 return elf_link_add_object_symbols (abfd, info);
4414 case bfd_archive:
4415 return elf_link_add_archive_symbols (abfd, info);
4416 default:
4417 bfd_set_error (bfd_error_wrong_format);
4418 return FALSE;
4419 }
4420 }
4421 \f
4422 /* This function will be called though elf_link_hash_traverse to store
4423 all hash value of the exported symbols in an array. */
4424
4425 static bfd_boolean
4426 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4427 {
4428 unsigned long **valuep = data;
4429 const char *name;
4430 char *p;
4431 unsigned long ha;
4432 char *alc = NULL;
4433
4434 if (h->root.type == bfd_link_hash_warning)
4435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4436
4437 /* Ignore indirect symbols. These are added by the versioning code. */
4438 if (h->dynindx == -1)
4439 return TRUE;
4440
4441 name = h->root.root.string;
4442 p = strchr (name, ELF_VER_CHR);
4443 if (p != NULL)
4444 {
4445 alc = bfd_malloc (p - name + 1);
4446 memcpy (alc, name, p - name);
4447 alc[p - name] = '\0';
4448 name = alc;
4449 }
4450
4451 /* Compute the hash value. */
4452 ha = bfd_elf_hash (name);
4453
4454 /* Store the found hash value in the array given as the argument. */
4455 *(*valuep)++ = ha;
4456
4457 /* And store it in the struct so that we can put it in the hash table
4458 later. */
4459 h->elf_hash_value = ha;
4460
4461 if (alc != NULL)
4462 free (alc);
4463
4464 return TRUE;
4465 }
4466
4467 /* Array used to determine the number of hash table buckets to use
4468 based on the number of symbols there are. If there are fewer than
4469 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4470 fewer than 37 we use 17 buckets, and so forth. We never use more
4471 than 32771 buckets. */
4472
4473 static const size_t elf_buckets[] =
4474 {
4475 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4476 16411, 32771, 0
4477 };
4478
4479 /* Compute bucket count for hashing table. We do not use a static set
4480 of possible tables sizes anymore. Instead we determine for all
4481 possible reasonable sizes of the table the outcome (i.e., the
4482 number of collisions etc) and choose the best solution. The
4483 weighting functions are not too simple to allow the table to grow
4484 without bounds. Instead one of the weighting factors is the size.
4485 Therefore the result is always a good payoff between few collisions
4486 (= short chain lengths) and table size. */
4487 static size_t
4488 compute_bucket_count (struct bfd_link_info *info)
4489 {
4490 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4491 size_t best_size = 0;
4492 unsigned long int *hashcodes;
4493 unsigned long int *hashcodesp;
4494 unsigned long int i;
4495 bfd_size_type amt;
4496
4497 /* Compute the hash values for all exported symbols. At the same
4498 time store the values in an array so that we could use them for
4499 optimizations. */
4500 amt = dynsymcount;
4501 amt *= sizeof (unsigned long int);
4502 hashcodes = bfd_malloc (amt);
4503 if (hashcodes == NULL)
4504 return 0;
4505 hashcodesp = hashcodes;
4506
4507 /* Put all hash values in HASHCODES. */
4508 elf_link_hash_traverse (elf_hash_table (info),
4509 elf_collect_hash_codes, &hashcodesp);
4510
4511 /* We have a problem here. The following code to optimize the table
4512 size requires an integer type with more the 32 bits. If
4513 BFD_HOST_U_64_BIT is set we know about such a type. */
4514 #ifdef BFD_HOST_U_64_BIT
4515 if (info->optimize)
4516 {
4517 unsigned long int nsyms = hashcodesp - hashcodes;
4518 size_t minsize;
4519 size_t maxsize;
4520 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4521 unsigned long int *counts ;
4522 bfd *dynobj = elf_hash_table (info)->dynobj;
4523 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4524
4525 /* Possible optimization parameters: if we have NSYMS symbols we say
4526 that the hashing table must at least have NSYMS/4 and at most
4527 2*NSYMS buckets. */
4528 minsize = nsyms / 4;
4529 if (minsize == 0)
4530 minsize = 1;
4531 best_size = maxsize = nsyms * 2;
4532
4533 /* Create array where we count the collisions in. We must use bfd_malloc
4534 since the size could be large. */
4535 amt = maxsize;
4536 amt *= sizeof (unsigned long int);
4537 counts = bfd_malloc (amt);
4538 if (counts == NULL)
4539 {
4540 free (hashcodes);
4541 return 0;
4542 }
4543
4544 /* Compute the "optimal" size for the hash table. The criteria is a
4545 minimal chain length. The minor criteria is (of course) the size
4546 of the table. */
4547 for (i = minsize; i < maxsize; ++i)
4548 {
4549 /* Walk through the array of hashcodes and count the collisions. */
4550 BFD_HOST_U_64_BIT max;
4551 unsigned long int j;
4552 unsigned long int fact;
4553
4554 memset (counts, '\0', i * sizeof (unsigned long int));
4555
4556 /* Determine how often each hash bucket is used. */
4557 for (j = 0; j < nsyms; ++j)
4558 ++counts[hashcodes[j] % i];
4559
4560 /* For the weight function we need some information about the
4561 pagesize on the target. This is information need not be 100%
4562 accurate. Since this information is not available (so far) we
4563 define it here to a reasonable default value. If it is crucial
4564 to have a better value some day simply define this value. */
4565 # ifndef BFD_TARGET_PAGESIZE
4566 # define BFD_TARGET_PAGESIZE (4096)
4567 # endif
4568
4569 /* We in any case need 2 + NSYMS entries for the size values and
4570 the chains. */
4571 max = (2 + nsyms) * (bed->s->arch_size / 8);
4572
4573 # if 1
4574 /* Variant 1: optimize for short chains. We add the squares
4575 of all the chain lengths (which favors many small chain
4576 over a few long chains). */
4577 for (j = 0; j < i; ++j)
4578 max += counts[j] * counts[j];
4579
4580 /* This adds penalties for the overall size of the table. */
4581 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4582 max *= fact * fact;
4583 # else
4584 /* Variant 2: Optimize a lot more for small table. Here we
4585 also add squares of the size but we also add penalties for
4586 empty slots (the +1 term). */
4587 for (j = 0; j < i; ++j)
4588 max += (1 + counts[j]) * (1 + counts[j]);
4589
4590 /* The overall size of the table is considered, but not as
4591 strong as in variant 1, where it is squared. */
4592 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4593 max *= fact;
4594 # endif
4595
4596 /* Compare with current best results. */
4597 if (max < best_chlen)
4598 {
4599 best_chlen = max;
4600 best_size = i;
4601 }
4602 }
4603
4604 free (counts);
4605 }
4606 else
4607 #endif /* defined (BFD_HOST_U_64_BIT) */
4608 {
4609 /* This is the fallback solution if no 64bit type is available or if we
4610 are not supposed to spend much time on optimizations. We select the
4611 bucket count using a fixed set of numbers. */
4612 for (i = 0; elf_buckets[i] != 0; i++)
4613 {
4614 best_size = elf_buckets[i];
4615 if (dynsymcount < elf_buckets[i + 1])
4616 break;
4617 }
4618 }
4619
4620 /* Free the arrays we needed. */
4621 free (hashcodes);
4622
4623 return best_size;
4624 }
4625
4626 /* Set up the sizes and contents of the ELF dynamic sections. This is
4627 called by the ELF linker emulation before_allocation routine. We
4628 must set the sizes of the sections before the linker sets the
4629 addresses of the various sections. */
4630
4631 bfd_boolean
4632 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4633 const char *soname,
4634 const char *rpath,
4635 const char *filter_shlib,
4636 const char * const *auxiliary_filters,
4637 struct bfd_link_info *info,
4638 asection **sinterpptr,
4639 struct bfd_elf_version_tree *verdefs)
4640 {
4641 bfd_size_type soname_indx;
4642 bfd *dynobj;
4643 const struct elf_backend_data *bed;
4644 struct elf_assign_sym_version_info asvinfo;
4645
4646 *sinterpptr = NULL;
4647
4648 soname_indx = (bfd_size_type) -1;
4649
4650 if (!is_elf_hash_table (info->hash))
4651 return TRUE;
4652
4653 elf_tdata (output_bfd)->relro = info->relro;
4654 if (info->execstack)
4655 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4656 else if (info->noexecstack)
4657 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4658 else
4659 {
4660 bfd *inputobj;
4661 asection *notesec = NULL;
4662 int exec = 0;
4663
4664 for (inputobj = info->input_bfds;
4665 inputobj;
4666 inputobj = inputobj->link_next)
4667 {
4668 asection *s;
4669
4670 if (inputobj->flags & DYNAMIC)
4671 continue;
4672 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4673 if (s)
4674 {
4675 if (s->flags & SEC_CODE)
4676 exec = PF_X;
4677 notesec = s;
4678 }
4679 else
4680 exec = PF_X;
4681 }
4682 if (notesec)
4683 {
4684 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4685 if (exec && info->relocatable
4686 && notesec->output_section != bfd_abs_section_ptr)
4687 notesec->output_section->flags |= SEC_CODE;
4688 }
4689 }
4690
4691 /* Any syms created from now on start with -1 in
4692 got.refcount/offset and plt.refcount/offset. */
4693 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4694
4695 /* The backend may have to create some sections regardless of whether
4696 we're dynamic or not. */
4697 bed = get_elf_backend_data (output_bfd);
4698 if (bed->elf_backend_always_size_sections
4699 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4700 return FALSE;
4701
4702 dynobj = elf_hash_table (info)->dynobj;
4703
4704 /* If there were no dynamic objects in the link, there is nothing to
4705 do here. */
4706 if (dynobj == NULL)
4707 return TRUE;
4708
4709 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4710 return FALSE;
4711
4712 if (elf_hash_table (info)->dynamic_sections_created)
4713 {
4714 struct elf_info_failed eif;
4715 struct elf_link_hash_entry *h;
4716 asection *dynstr;
4717 struct bfd_elf_version_tree *t;
4718 struct bfd_elf_version_expr *d;
4719 bfd_boolean all_defined;
4720
4721 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4722 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4723
4724 if (soname != NULL)
4725 {
4726 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4727 soname, TRUE);
4728 if (soname_indx == (bfd_size_type) -1
4729 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4730 return FALSE;
4731 }
4732
4733 if (info->symbolic)
4734 {
4735 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4736 return FALSE;
4737 info->flags |= DF_SYMBOLIC;
4738 }
4739
4740 if (rpath != NULL)
4741 {
4742 bfd_size_type indx;
4743
4744 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4745 TRUE);
4746 if (indx == (bfd_size_type) -1
4747 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4748 return FALSE;
4749
4750 if (info->new_dtags)
4751 {
4752 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4753 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4754 return FALSE;
4755 }
4756 }
4757
4758 if (filter_shlib != NULL)
4759 {
4760 bfd_size_type indx;
4761
4762 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4763 filter_shlib, TRUE);
4764 if (indx == (bfd_size_type) -1
4765 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4766 return FALSE;
4767 }
4768
4769 if (auxiliary_filters != NULL)
4770 {
4771 const char * const *p;
4772
4773 for (p = auxiliary_filters; *p != NULL; p++)
4774 {
4775 bfd_size_type indx;
4776
4777 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4778 *p, TRUE);
4779 if (indx == (bfd_size_type) -1
4780 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4781 return FALSE;
4782 }
4783 }
4784
4785 eif.info = info;
4786 eif.verdefs = verdefs;
4787 eif.failed = FALSE;
4788
4789 /* If we are supposed to export all symbols into the dynamic symbol
4790 table (this is not the normal case), then do so. */
4791 if (info->export_dynamic)
4792 {
4793 elf_link_hash_traverse (elf_hash_table (info),
4794 _bfd_elf_export_symbol,
4795 &eif);
4796 if (eif.failed)
4797 return FALSE;
4798 }
4799
4800 /* Make all global versions with definition. */
4801 for (t = verdefs; t != NULL; t = t->next)
4802 for (d = t->globals.list; d != NULL; d = d->next)
4803 if (!d->symver && d->symbol)
4804 {
4805 const char *verstr, *name;
4806 size_t namelen, verlen, newlen;
4807 char *newname, *p;
4808 struct elf_link_hash_entry *newh;
4809
4810 name = d->symbol;
4811 namelen = strlen (name);
4812 verstr = t->name;
4813 verlen = strlen (verstr);
4814 newlen = namelen + verlen + 3;
4815
4816 newname = bfd_malloc (newlen);
4817 if (newname == NULL)
4818 return FALSE;
4819 memcpy (newname, name, namelen);
4820
4821 /* Check the hidden versioned definition. */
4822 p = newname + namelen;
4823 *p++ = ELF_VER_CHR;
4824 memcpy (p, verstr, verlen + 1);
4825 newh = elf_link_hash_lookup (elf_hash_table (info),
4826 newname, FALSE, FALSE,
4827 FALSE);
4828 if (newh == NULL
4829 || (newh->root.type != bfd_link_hash_defined
4830 && newh->root.type != bfd_link_hash_defweak))
4831 {
4832 /* Check the default versioned definition. */
4833 *p++ = ELF_VER_CHR;
4834 memcpy (p, verstr, verlen + 1);
4835 newh = elf_link_hash_lookup (elf_hash_table (info),
4836 newname, FALSE, FALSE,
4837 FALSE);
4838 }
4839 free (newname);
4840
4841 /* Mark this version if there is a definition and it is
4842 not defined in a shared object. */
4843 if (newh != NULL
4844 && ((newh->elf_link_hash_flags
4845 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4846 && (newh->root.type == bfd_link_hash_defined
4847 || newh->root.type == bfd_link_hash_defweak))
4848 d->symver = 1;
4849 }
4850
4851 /* Attach all the symbols to their version information. */
4852 asvinfo.output_bfd = output_bfd;
4853 asvinfo.info = info;
4854 asvinfo.verdefs = verdefs;
4855 asvinfo.failed = FALSE;
4856
4857 elf_link_hash_traverse (elf_hash_table (info),
4858 _bfd_elf_link_assign_sym_version,
4859 &asvinfo);
4860 if (asvinfo.failed)
4861 return FALSE;
4862
4863 if (!info->allow_undefined_version)
4864 {
4865 /* Check if all global versions have a definition. */
4866 all_defined = TRUE;
4867 for (t = verdefs; t != NULL; t = t->next)
4868 for (d = t->globals.list; d != NULL; d = d->next)
4869 if (!d->symver && !d->script)
4870 {
4871 (*_bfd_error_handler)
4872 (_("%s: undefined version: %s"),
4873 d->pattern, t->name);
4874 all_defined = FALSE;
4875 }
4876
4877 if (!all_defined)
4878 {
4879 bfd_set_error (bfd_error_bad_value);
4880 return FALSE;
4881 }
4882 }
4883
4884 /* Find all symbols which were defined in a dynamic object and make
4885 the backend pick a reasonable value for them. */
4886 elf_link_hash_traverse (elf_hash_table (info),
4887 _bfd_elf_adjust_dynamic_symbol,
4888 &eif);
4889 if (eif.failed)
4890 return FALSE;
4891
4892 /* Add some entries to the .dynamic section. We fill in some of the
4893 values later, in elf_bfd_final_link, but we must add the entries
4894 now so that we know the final size of the .dynamic section. */
4895
4896 /* If there are initialization and/or finalization functions to
4897 call then add the corresponding DT_INIT/DT_FINI entries. */
4898 h = (info->init_function
4899 ? elf_link_hash_lookup (elf_hash_table (info),
4900 info->init_function, FALSE,
4901 FALSE, FALSE)
4902 : NULL);
4903 if (h != NULL
4904 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4905 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4906 {
4907 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4908 return FALSE;
4909 }
4910 h = (info->fini_function
4911 ? elf_link_hash_lookup (elf_hash_table (info),
4912 info->fini_function, FALSE,
4913 FALSE, FALSE)
4914 : NULL);
4915 if (h != NULL
4916 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4917 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4918 {
4919 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4920 return FALSE;
4921 }
4922
4923 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4924 {
4925 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4926 if (! info->executable)
4927 {
4928 bfd *sub;
4929 asection *o;
4930
4931 for (sub = info->input_bfds; sub != NULL;
4932 sub = sub->link_next)
4933 for (o = sub->sections; o != NULL; o = o->next)
4934 if (elf_section_data (o)->this_hdr.sh_type
4935 == SHT_PREINIT_ARRAY)
4936 {
4937 (*_bfd_error_handler)
4938 (_("%s: .preinit_array section is not allowed in DSO"),
4939 bfd_archive_filename (sub));
4940 break;
4941 }
4942
4943 bfd_set_error (bfd_error_nonrepresentable_section);
4944 return FALSE;
4945 }
4946
4947 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4948 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4949 return FALSE;
4950 }
4951 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4952 {
4953 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4954 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4955 return FALSE;
4956 }
4957 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4958 {
4959 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4960 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4961 return FALSE;
4962 }
4963
4964 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4965 /* If .dynstr is excluded from the link, we don't want any of
4966 these tags. Strictly, we should be checking each section
4967 individually; This quick check covers for the case where
4968 someone does a /DISCARD/ : { *(*) }. */
4969 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4970 {
4971 bfd_size_type strsize;
4972
4973 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4974 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4975 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4976 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4977 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4978 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
4979 bed->s->sizeof_sym))
4980 return FALSE;
4981 }
4982 }
4983
4984 /* The backend must work out the sizes of all the other dynamic
4985 sections. */
4986 if (bed->elf_backend_size_dynamic_sections
4987 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
4988 return FALSE;
4989
4990 if (elf_hash_table (info)->dynamic_sections_created)
4991 {
4992 bfd_size_type dynsymcount;
4993 asection *s;
4994 size_t bucketcount = 0;
4995 size_t hash_entry_size;
4996 unsigned int dtagcount;
4997
4998 /* Set up the version definition section. */
4999 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5000 BFD_ASSERT (s != NULL);
5001
5002 /* We may have created additional version definitions if we are
5003 just linking a regular application. */
5004 verdefs = asvinfo.verdefs;
5005
5006 /* Skip anonymous version tag. */
5007 if (verdefs != NULL && verdefs->vernum == 0)
5008 verdefs = verdefs->next;
5009
5010 if (verdefs == NULL)
5011 _bfd_strip_section_from_output (info, s);
5012 else
5013 {
5014 unsigned int cdefs;
5015 bfd_size_type size;
5016 struct bfd_elf_version_tree *t;
5017 bfd_byte *p;
5018 Elf_Internal_Verdef def;
5019 Elf_Internal_Verdaux defaux;
5020
5021 cdefs = 0;
5022 size = 0;
5023
5024 /* Make space for the base version. */
5025 size += sizeof (Elf_External_Verdef);
5026 size += sizeof (Elf_External_Verdaux);
5027 ++cdefs;
5028
5029 for (t = verdefs; t != NULL; t = t->next)
5030 {
5031 struct bfd_elf_version_deps *n;
5032
5033 size += sizeof (Elf_External_Verdef);
5034 size += sizeof (Elf_External_Verdaux);
5035 ++cdefs;
5036
5037 for (n = t->deps; n != NULL; n = n->next)
5038 size += sizeof (Elf_External_Verdaux);
5039 }
5040
5041 s->size = size;
5042 s->contents = bfd_alloc (output_bfd, s->size);
5043 if (s->contents == NULL && s->size != 0)
5044 return FALSE;
5045
5046 /* Fill in the version definition section. */
5047
5048 p = s->contents;
5049
5050 def.vd_version = VER_DEF_CURRENT;
5051 def.vd_flags = VER_FLG_BASE;
5052 def.vd_ndx = 1;
5053 def.vd_cnt = 1;
5054 def.vd_aux = sizeof (Elf_External_Verdef);
5055 def.vd_next = (sizeof (Elf_External_Verdef)
5056 + sizeof (Elf_External_Verdaux));
5057
5058 if (soname_indx != (bfd_size_type) -1)
5059 {
5060 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5061 soname_indx);
5062 def.vd_hash = bfd_elf_hash (soname);
5063 defaux.vda_name = soname_indx;
5064 }
5065 else
5066 {
5067 const char *name;
5068 bfd_size_type indx;
5069
5070 name = basename (output_bfd->filename);
5071 def.vd_hash = bfd_elf_hash (name);
5072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5073 name, FALSE);
5074 if (indx == (bfd_size_type) -1)
5075 return FALSE;
5076 defaux.vda_name = indx;
5077 }
5078 defaux.vda_next = 0;
5079
5080 _bfd_elf_swap_verdef_out (output_bfd, &def,
5081 (Elf_External_Verdef *) p);
5082 p += sizeof (Elf_External_Verdef);
5083 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5084 (Elf_External_Verdaux *) p);
5085 p += sizeof (Elf_External_Verdaux);
5086
5087 for (t = verdefs; t != NULL; t = t->next)
5088 {
5089 unsigned int cdeps;
5090 struct bfd_elf_version_deps *n;
5091 struct elf_link_hash_entry *h;
5092 struct bfd_link_hash_entry *bh;
5093
5094 cdeps = 0;
5095 for (n = t->deps; n != NULL; n = n->next)
5096 ++cdeps;
5097
5098 /* Add a symbol representing this version. */
5099 bh = NULL;
5100 if (! (_bfd_generic_link_add_one_symbol
5101 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5102 0, NULL, FALSE,
5103 get_elf_backend_data (dynobj)->collect, &bh)))
5104 return FALSE;
5105 h = (struct elf_link_hash_entry *) bh;
5106 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5107 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5108 h->type = STT_OBJECT;
5109 h->verinfo.vertree = t;
5110
5111 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5112 return FALSE;
5113
5114 def.vd_version = VER_DEF_CURRENT;
5115 def.vd_flags = 0;
5116 if (t->globals.list == NULL
5117 && t->locals.list == NULL
5118 && ! t->used)
5119 def.vd_flags |= VER_FLG_WEAK;
5120 def.vd_ndx = t->vernum + 1;
5121 def.vd_cnt = cdeps + 1;
5122 def.vd_hash = bfd_elf_hash (t->name);
5123 def.vd_aux = sizeof (Elf_External_Verdef);
5124 def.vd_next = 0;
5125 if (t->next != NULL)
5126 def.vd_next = (sizeof (Elf_External_Verdef)
5127 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5128
5129 _bfd_elf_swap_verdef_out (output_bfd, &def,
5130 (Elf_External_Verdef *) p);
5131 p += sizeof (Elf_External_Verdef);
5132
5133 defaux.vda_name = h->dynstr_index;
5134 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5135 h->dynstr_index);
5136 defaux.vda_next = 0;
5137 if (t->deps != NULL)
5138 defaux.vda_next = sizeof (Elf_External_Verdaux);
5139 t->name_indx = defaux.vda_name;
5140
5141 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5142 (Elf_External_Verdaux *) p);
5143 p += sizeof (Elf_External_Verdaux);
5144
5145 for (n = t->deps; n != NULL; n = n->next)
5146 {
5147 if (n->version_needed == NULL)
5148 {
5149 /* This can happen if there was an error in the
5150 version script. */
5151 defaux.vda_name = 0;
5152 }
5153 else
5154 {
5155 defaux.vda_name = n->version_needed->name_indx;
5156 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5157 defaux.vda_name);
5158 }
5159 if (n->next == NULL)
5160 defaux.vda_next = 0;
5161 else
5162 defaux.vda_next = sizeof (Elf_External_Verdaux);
5163
5164 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5165 (Elf_External_Verdaux *) p);
5166 p += sizeof (Elf_External_Verdaux);
5167 }
5168 }
5169
5170 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5171 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5172 return FALSE;
5173
5174 elf_tdata (output_bfd)->cverdefs = cdefs;
5175 }
5176
5177 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5178 {
5179 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5180 return FALSE;
5181 }
5182 else if (info->flags & DF_BIND_NOW)
5183 {
5184 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5185 return FALSE;
5186 }
5187
5188 if (info->flags_1)
5189 {
5190 if (info->executable)
5191 info->flags_1 &= ~ (DF_1_INITFIRST
5192 | DF_1_NODELETE
5193 | DF_1_NOOPEN);
5194 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5195 return FALSE;
5196 }
5197
5198 /* Work out the size of the version reference section. */
5199
5200 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5201 BFD_ASSERT (s != NULL);
5202 {
5203 struct elf_find_verdep_info sinfo;
5204
5205 sinfo.output_bfd = output_bfd;
5206 sinfo.info = info;
5207 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5208 if (sinfo.vers == 0)
5209 sinfo.vers = 1;
5210 sinfo.failed = FALSE;
5211
5212 elf_link_hash_traverse (elf_hash_table (info),
5213 _bfd_elf_link_find_version_dependencies,
5214 &sinfo);
5215
5216 if (elf_tdata (output_bfd)->verref == NULL)
5217 _bfd_strip_section_from_output (info, s);
5218 else
5219 {
5220 Elf_Internal_Verneed *t;
5221 unsigned int size;
5222 unsigned int crefs;
5223 bfd_byte *p;
5224
5225 /* Build the version definition section. */
5226 size = 0;
5227 crefs = 0;
5228 for (t = elf_tdata (output_bfd)->verref;
5229 t != NULL;
5230 t = t->vn_nextref)
5231 {
5232 Elf_Internal_Vernaux *a;
5233
5234 size += sizeof (Elf_External_Verneed);
5235 ++crefs;
5236 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5237 size += sizeof (Elf_External_Vernaux);
5238 }
5239
5240 s->size = size;
5241 s->contents = bfd_alloc (output_bfd, s->size);
5242 if (s->contents == NULL)
5243 return FALSE;
5244
5245 p = s->contents;
5246 for (t = elf_tdata (output_bfd)->verref;
5247 t != NULL;
5248 t = t->vn_nextref)
5249 {
5250 unsigned int caux;
5251 Elf_Internal_Vernaux *a;
5252 bfd_size_type indx;
5253
5254 caux = 0;
5255 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5256 ++caux;
5257
5258 t->vn_version = VER_NEED_CURRENT;
5259 t->vn_cnt = caux;
5260 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5261 elf_dt_name (t->vn_bfd) != NULL
5262 ? elf_dt_name (t->vn_bfd)
5263 : basename (t->vn_bfd->filename),
5264 FALSE);
5265 if (indx == (bfd_size_type) -1)
5266 return FALSE;
5267 t->vn_file = indx;
5268 t->vn_aux = sizeof (Elf_External_Verneed);
5269 if (t->vn_nextref == NULL)
5270 t->vn_next = 0;
5271 else
5272 t->vn_next = (sizeof (Elf_External_Verneed)
5273 + caux * sizeof (Elf_External_Vernaux));
5274
5275 _bfd_elf_swap_verneed_out (output_bfd, t,
5276 (Elf_External_Verneed *) p);
5277 p += sizeof (Elf_External_Verneed);
5278
5279 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5280 {
5281 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5282 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5283 a->vna_nodename, FALSE);
5284 if (indx == (bfd_size_type) -1)
5285 return FALSE;
5286 a->vna_name = indx;
5287 if (a->vna_nextptr == NULL)
5288 a->vna_next = 0;
5289 else
5290 a->vna_next = sizeof (Elf_External_Vernaux);
5291
5292 _bfd_elf_swap_vernaux_out (output_bfd, a,
5293 (Elf_External_Vernaux *) p);
5294 p += sizeof (Elf_External_Vernaux);
5295 }
5296 }
5297
5298 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5299 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5300 return FALSE;
5301
5302 elf_tdata (output_bfd)->cverrefs = crefs;
5303 }
5304 }
5305
5306 /* Assign dynsym indicies. In a shared library we generate a
5307 section symbol for each output section, which come first.
5308 Next come all of the back-end allocated local dynamic syms,
5309 followed by the rest of the global symbols. */
5310
5311 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5312
5313 /* Work out the size of the symbol version section. */
5314 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5315 BFD_ASSERT (s != NULL);
5316 if (dynsymcount == 0
5317 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5318 {
5319 _bfd_strip_section_from_output (info, s);
5320 /* The DYNSYMCOUNT might have changed if we were going to
5321 output a dynamic symbol table entry for S. */
5322 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5323 }
5324 else
5325 {
5326 s->size = dynsymcount * sizeof (Elf_External_Versym);
5327 s->contents = bfd_zalloc (output_bfd, s->size);
5328 if (s->contents == NULL)
5329 return FALSE;
5330
5331 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5332 return FALSE;
5333 }
5334
5335 /* Set the size of the .dynsym and .hash sections. We counted
5336 the number of dynamic symbols in elf_link_add_object_symbols.
5337 We will build the contents of .dynsym and .hash when we build
5338 the final symbol table, because until then we do not know the
5339 correct value to give the symbols. We built the .dynstr
5340 section as we went along in elf_link_add_object_symbols. */
5341 s = bfd_get_section_by_name (dynobj, ".dynsym");
5342 BFD_ASSERT (s != NULL);
5343 s->size = dynsymcount * bed->s->sizeof_sym;
5344 s->contents = bfd_alloc (output_bfd, s->size);
5345 if (s->contents == NULL && s->size != 0)
5346 return FALSE;
5347
5348 if (dynsymcount != 0)
5349 {
5350 Elf_Internal_Sym isym;
5351
5352 /* The first entry in .dynsym is a dummy symbol. */
5353 isym.st_value = 0;
5354 isym.st_size = 0;
5355 isym.st_name = 0;
5356 isym.st_info = 0;
5357 isym.st_other = 0;
5358 isym.st_shndx = 0;
5359 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5360 }
5361
5362 /* Compute the size of the hashing table. As a side effect this
5363 computes the hash values for all the names we export. */
5364 bucketcount = compute_bucket_count (info);
5365
5366 s = bfd_get_section_by_name (dynobj, ".hash");
5367 BFD_ASSERT (s != NULL);
5368 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5369 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5370 s->contents = bfd_zalloc (output_bfd, s->size);
5371 if (s->contents == NULL)
5372 return FALSE;
5373
5374 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5375 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5376 s->contents + hash_entry_size);
5377
5378 elf_hash_table (info)->bucketcount = bucketcount;
5379
5380 s = bfd_get_section_by_name (dynobj, ".dynstr");
5381 BFD_ASSERT (s != NULL);
5382
5383 elf_finalize_dynstr (output_bfd, info);
5384
5385 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5386
5387 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5388 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5389 return FALSE;
5390 }
5391
5392 return TRUE;
5393 }
5394
5395 /* Final phase of ELF linker. */
5396
5397 /* A structure we use to avoid passing large numbers of arguments. */
5398
5399 struct elf_final_link_info
5400 {
5401 /* General link information. */
5402 struct bfd_link_info *info;
5403 /* Output BFD. */
5404 bfd *output_bfd;
5405 /* Symbol string table. */
5406 struct bfd_strtab_hash *symstrtab;
5407 /* .dynsym section. */
5408 asection *dynsym_sec;
5409 /* .hash section. */
5410 asection *hash_sec;
5411 /* symbol version section (.gnu.version). */
5412 asection *symver_sec;
5413 /* Buffer large enough to hold contents of any section. */
5414 bfd_byte *contents;
5415 /* Buffer large enough to hold external relocs of any section. */
5416 void *external_relocs;
5417 /* Buffer large enough to hold internal relocs of any section. */
5418 Elf_Internal_Rela *internal_relocs;
5419 /* Buffer large enough to hold external local symbols of any input
5420 BFD. */
5421 bfd_byte *external_syms;
5422 /* And a buffer for symbol section indices. */
5423 Elf_External_Sym_Shndx *locsym_shndx;
5424 /* Buffer large enough to hold internal local symbols of any input
5425 BFD. */
5426 Elf_Internal_Sym *internal_syms;
5427 /* Array large enough to hold a symbol index for each local symbol
5428 of any input BFD. */
5429 long *indices;
5430 /* Array large enough to hold a section pointer for each local
5431 symbol of any input BFD. */
5432 asection **sections;
5433 /* Buffer to hold swapped out symbols. */
5434 bfd_byte *symbuf;
5435 /* And one for symbol section indices. */
5436 Elf_External_Sym_Shndx *symshndxbuf;
5437 /* Number of swapped out symbols in buffer. */
5438 size_t symbuf_count;
5439 /* Number of symbols which fit in symbuf. */
5440 size_t symbuf_size;
5441 /* And same for symshndxbuf. */
5442 size_t shndxbuf_size;
5443 };
5444
5445 /* This struct is used to pass information to elf_link_output_extsym. */
5446
5447 struct elf_outext_info
5448 {
5449 bfd_boolean failed;
5450 bfd_boolean localsyms;
5451 struct elf_final_link_info *finfo;
5452 };
5453
5454 /* When performing a relocatable link, the input relocations are
5455 preserved. But, if they reference global symbols, the indices
5456 referenced must be updated. Update all the relocations in
5457 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5458
5459 static void
5460 elf_link_adjust_relocs (bfd *abfd,
5461 Elf_Internal_Shdr *rel_hdr,
5462 unsigned int count,
5463 struct elf_link_hash_entry **rel_hash)
5464 {
5465 unsigned int i;
5466 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5467 bfd_byte *erela;
5468 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5469 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5470 bfd_vma r_type_mask;
5471 int r_sym_shift;
5472
5473 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5474 {
5475 swap_in = bed->s->swap_reloc_in;
5476 swap_out = bed->s->swap_reloc_out;
5477 }
5478 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5479 {
5480 swap_in = bed->s->swap_reloca_in;
5481 swap_out = bed->s->swap_reloca_out;
5482 }
5483 else
5484 abort ();
5485
5486 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5487 abort ();
5488
5489 if (bed->s->arch_size == 32)
5490 {
5491 r_type_mask = 0xff;
5492 r_sym_shift = 8;
5493 }
5494 else
5495 {
5496 r_type_mask = 0xffffffff;
5497 r_sym_shift = 32;
5498 }
5499
5500 erela = rel_hdr->contents;
5501 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5502 {
5503 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5504 unsigned int j;
5505
5506 if (*rel_hash == NULL)
5507 continue;
5508
5509 BFD_ASSERT ((*rel_hash)->indx >= 0);
5510
5511 (*swap_in) (abfd, erela, irela);
5512 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5513 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5514 | (irela[j].r_info & r_type_mask));
5515 (*swap_out) (abfd, irela, erela);
5516 }
5517 }
5518
5519 struct elf_link_sort_rela
5520 {
5521 union {
5522 bfd_vma offset;
5523 bfd_vma sym_mask;
5524 } u;
5525 enum elf_reloc_type_class type;
5526 /* We use this as an array of size int_rels_per_ext_rel. */
5527 Elf_Internal_Rela rela[1];
5528 };
5529
5530 static int
5531 elf_link_sort_cmp1 (const void *A, const void *B)
5532 {
5533 const struct elf_link_sort_rela *a = A;
5534 const struct elf_link_sort_rela *b = B;
5535 int relativea, relativeb;
5536
5537 relativea = a->type == reloc_class_relative;
5538 relativeb = b->type == reloc_class_relative;
5539
5540 if (relativea < relativeb)
5541 return 1;
5542 if (relativea > relativeb)
5543 return -1;
5544 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5545 return -1;
5546 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5547 return 1;
5548 if (a->rela->r_offset < b->rela->r_offset)
5549 return -1;
5550 if (a->rela->r_offset > b->rela->r_offset)
5551 return 1;
5552 return 0;
5553 }
5554
5555 static int
5556 elf_link_sort_cmp2 (const void *A, const void *B)
5557 {
5558 const struct elf_link_sort_rela *a = A;
5559 const struct elf_link_sort_rela *b = B;
5560 int copya, copyb;
5561
5562 if (a->u.offset < b->u.offset)
5563 return -1;
5564 if (a->u.offset > b->u.offset)
5565 return 1;
5566 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5567 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5568 if (copya < copyb)
5569 return -1;
5570 if (copya > copyb)
5571 return 1;
5572 if (a->rela->r_offset < b->rela->r_offset)
5573 return -1;
5574 if (a->rela->r_offset > b->rela->r_offset)
5575 return 1;
5576 return 0;
5577 }
5578
5579 static size_t
5580 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5581 {
5582 asection *reldyn;
5583 bfd_size_type count, size;
5584 size_t i, ret, sort_elt, ext_size;
5585 bfd_byte *sort, *s_non_relative, *p;
5586 struct elf_link_sort_rela *sq;
5587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5588 int i2e = bed->s->int_rels_per_ext_rel;
5589 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5590 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5591 struct bfd_link_order *lo;
5592 bfd_vma r_sym_mask;
5593
5594 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5595 if (reldyn == NULL || reldyn->size == 0)
5596 {
5597 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5598 if (reldyn == NULL || reldyn->size == 0)
5599 return 0;
5600 ext_size = bed->s->sizeof_rel;
5601 swap_in = bed->s->swap_reloc_in;
5602 swap_out = bed->s->swap_reloc_out;
5603 }
5604 else
5605 {
5606 ext_size = bed->s->sizeof_rela;
5607 swap_in = bed->s->swap_reloca_in;
5608 swap_out = bed->s->swap_reloca_out;
5609 }
5610 count = reldyn->size / ext_size;
5611
5612 size = 0;
5613 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5614 if (lo->type == bfd_indirect_link_order)
5615 {
5616 asection *o = lo->u.indirect.section;
5617 size += o->size;
5618 }
5619
5620 if (size != reldyn->size)
5621 return 0;
5622
5623 sort_elt = (sizeof (struct elf_link_sort_rela)
5624 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5625 sort = bfd_zmalloc (sort_elt * count);
5626 if (sort == NULL)
5627 {
5628 (*info->callbacks->warning)
5629 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5630 return 0;
5631 }
5632
5633 if (bed->s->arch_size == 32)
5634 r_sym_mask = ~(bfd_vma) 0xff;
5635 else
5636 r_sym_mask = ~(bfd_vma) 0xffffffff;
5637
5638 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5639 if (lo->type == bfd_indirect_link_order)
5640 {
5641 bfd_byte *erel, *erelend;
5642 asection *o = lo->u.indirect.section;
5643
5644 erel = o->contents;
5645 erelend = o->contents + o->size;
5646 p = sort + o->output_offset / ext_size * sort_elt;
5647 while (erel < erelend)
5648 {
5649 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5650 (*swap_in) (abfd, erel, s->rela);
5651 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5652 s->u.sym_mask = r_sym_mask;
5653 p += sort_elt;
5654 erel += ext_size;
5655 }
5656 }
5657
5658 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5659
5660 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5661 {
5662 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5663 if (s->type != reloc_class_relative)
5664 break;
5665 }
5666 ret = i;
5667 s_non_relative = p;
5668
5669 sq = (struct elf_link_sort_rela *) s_non_relative;
5670 for (; i < count; i++, p += sort_elt)
5671 {
5672 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5673 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5674 sq = sp;
5675 sp->u.offset = sq->rela->r_offset;
5676 }
5677
5678 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5679
5680 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5681 if (lo->type == bfd_indirect_link_order)
5682 {
5683 bfd_byte *erel, *erelend;
5684 asection *o = lo->u.indirect.section;
5685
5686 erel = o->contents;
5687 erelend = o->contents + o->size;
5688 p = sort + o->output_offset / ext_size * sort_elt;
5689 while (erel < erelend)
5690 {
5691 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5692 (*swap_out) (abfd, s->rela, erel);
5693 p += sort_elt;
5694 erel += ext_size;
5695 }
5696 }
5697
5698 free (sort);
5699 *psec = reldyn;
5700 return ret;
5701 }
5702
5703 /* Flush the output symbols to the file. */
5704
5705 static bfd_boolean
5706 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5707 const struct elf_backend_data *bed)
5708 {
5709 if (finfo->symbuf_count > 0)
5710 {
5711 Elf_Internal_Shdr *hdr;
5712 file_ptr pos;
5713 bfd_size_type amt;
5714
5715 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5716 pos = hdr->sh_offset + hdr->sh_size;
5717 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5718 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5719 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5720 return FALSE;
5721
5722 hdr->sh_size += amt;
5723 finfo->symbuf_count = 0;
5724 }
5725
5726 return TRUE;
5727 }
5728
5729 /* Add a symbol to the output symbol table. */
5730
5731 static bfd_boolean
5732 elf_link_output_sym (struct elf_final_link_info *finfo,
5733 const char *name,
5734 Elf_Internal_Sym *elfsym,
5735 asection *input_sec,
5736 struct elf_link_hash_entry *h)
5737 {
5738 bfd_byte *dest;
5739 Elf_External_Sym_Shndx *destshndx;
5740 bfd_boolean (*output_symbol_hook)
5741 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5742 struct elf_link_hash_entry *);
5743 const struct elf_backend_data *bed;
5744
5745 bed = get_elf_backend_data (finfo->output_bfd);
5746 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5747 if (output_symbol_hook != NULL)
5748 {
5749 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5750 return FALSE;
5751 }
5752
5753 if (name == NULL || *name == '\0')
5754 elfsym->st_name = 0;
5755 else if (input_sec->flags & SEC_EXCLUDE)
5756 elfsym->st_name = 0;
5757 else
5758 {
5759 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5760 name, TRUE, FALSE);
5761 if (elfsym->st_name == (unsigned long) -1)
5762 return FALSE;
5763 }
5764
5765 if (finfo->symbuf_count >= finfo->symbuf_size)
5766 {
5767 if (! elf_link_flush_output_syms (finfo, bed))
5768 return FALSE;
5769 }
5770
5771 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5772 destshndx = finfo->symshndxbuf;
5773 if (destshndx != NULL)
5774 {
5775 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5776 {
5777 bfd_size_type amt;
5778
5779 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5780 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5781 if (destshndx == NULL)
5782 return FALSE;
5783 memset ((char *) destshndx + amt, 0, amt);
5784 finfo->shndxbuf_size *= 2;
5785 }
5786 destshndx += bfd_get_symcount (finfo->output_bfd);
5787 }
5788
5789 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5790 finfo->symbuf_count += 1;
5791 bfd_get_symcount (finfo->output_bfd) += 1;
5792
5793 return TRUE;
5794 }
5795
5796 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5797 allowing an unsatisfied unversioned symbol in the DSO to match a
5798 versioned symbol that would normally require an explicit version.
5799 We also handle the case that a DSO references a hidden symbol
5800 which may be satisfied by a versioned symbol in another DSO. */
5801
5802 static bfd_boolean
5803 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5804 const struct elf_backend_data *bed,
5805 struct elf_link_hash_entry *h)
5806 {
5807 bfd *abfd;
5808 struct elf_link_loaded_list *loaded;
5809
5810 if (!is_elf_hash_table (info->hash))
5811 return FALSE;
5812
5813 switch (h->root.type)
5814 {
5815 default:
5816 abfd = NULL;
5817 break;
5818
5819 case bfd_link_hash_undefined:
5820 case bfd_link_hash_undefweak:
5821 abfd = h->root.u.undef.abfd;
5822 if ((abfd->flags & DYNAMIC) == 0
5823 || elf_dyn_lib_class (abfd) != DYN_DT_NEEDED)
5824 return FALSE;
5825 break;
5826
5827 case bfd_link_hash_defined:
5828 case bfd_link_hash_defweak:
5829 abfd = h->root.u.def.section->owner;
5830 break;
5831
5832 case bfd_link_hash_common:
5833 abfd = h->root.u.c.p->section->owner;
5834 break;
5835 }
5836 BFD_ASSERT (abfd != NULL);
5837
5838 for (loaded = elf_hash_table (info)->loaded;
5839 loaded != NULL;
5840 loaded = loaded->next)
5841 {
5842 bfd *input;
5843 Elf_Internal_Shdr *hdr;
5844 bfd_size_type symcount;
5845 bfd_size_type extsymcount;
5846 bfd_size_type extsymoff;
5847 Elf_Internal_Shdr *versymhdr;
5848 Elf_Internal_Sym *isym;
5849 Elf_Internal_Sym *isymend;
5850 Elf_Internal_Sym *isymbuf;
5851 Elf_External_Versym *ever;
5852 Elf_External_Versym *extversym;
5853
5854 input = loaded->abfd;
5855
5856 /* We check each DSO for a possible hidden versioned definition. */
5857 if (input == abfd
5858 || (input->flags & DYNAMIC) == 0
5859 || elf_dynversym (input) == 0)
5860 continue;
5861
5862 hdr = &elf_tdata (input)->dynsymtab_hdr;
5863
5864 symcount = hdr->sh_size / bed->s->sizeof_sym;
5865 if (elf_bad_symtab (input))
5866 {
5867 extsymcount = symcount;
5868 extsymoff = 0;
5869 }
5870 else
5871 {
5872 extsymcount = symcount - hdr->sh_info;
5873 extsymoff = hdr->sh_info;
5874 }
5875
5876 if (extsymcount == 0)
5877 continue;
5878
5879 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
5880 NULL, NULL, NULL);
5881 if (isymbuf == NULL)
5882 return FALSE;
5883
5884 /* Read in any version definitions. */
5885 versymhdr = &elf_tdata (input)->dynversym_hdr;
5886 extversym = bfd_malloc (versymhdr->sh_size);
5887 if (extversym == NULL)
5888 goto error_ret;
5889
5890 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
5891 || (bfd_bread (extversym, versymhdr->sh_size, input)
5892 != versymhdr->sh_size))
5893 {
5894 free (extversym);
5895 error_ret:
5896 free (isymbuf);
5897 return FALSE;
5898 }
5899
5900 ever = extversym + extsymoff;
5901 isymend = isymbuf + extsymcount;
5902 for (isym = isymbuf; isym < isymend; isym++, ever++)
5903 {
5904 const char *name;
5905 Elf_Internal_Versym iver;
5906 unsigned short version_index;
5907
5908 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
5909 || isym->st_shndx == SHN_UNDEF)
5910 continue;
5911
5912 name = bfd_elf_string_from_elf_section (input,
5913 hdr->sh_link,
5914 isym->st_name);
5915 if (strcmp (name, h->root.root.string) != 0)
5916 continue;
5917
5918 _bfd_elf_swap_versym_in (input, ever, &iver);
5919
5920 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
5921 {
5922 /* If we have a non-hidden versioned sym, then it should
5923 have provided a definition for the undefined sym. */
5924 abort ();
5925 }
5926
5927 version_index = iver.vs_vers & VERSYM_VERSION;
5928 if (version_index == 1 || version_index == 2)
5929 {
5930 /* This is the base or first version. We can use it. */
5931 free (extversym);
5932 free (isymbuf);
5933 return TRUE;
5934 }
5935 }
5936
5937 free (extversym);
5938 free (isymbuf);
5939 }
5940
5941 return FALSE;
5942 }
5943
5944 /* Add an external symbol to the symbol table. This is called from
5945 the hash table traversal routine. When generating a shared object,
5946 we go through the symbol table twice. The first time we output
5947 anything that might have been forced to local scope in a version
5948 script. The second time we output the symbols that are still
5949 global symbols. */
5950
5951 static bfd_boolean
5952 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
5953 {
5954 struct elf_outext_info *eoinfo = data;
5955 struct elf_final_link_info *finfo = eoinfo->finfo;
5956 bfd_boolean strip;
5957 Elf_Internal_Sym sym;
5958 asection *input_sec;
5959 const struct elf_backend_data *bed;
5960
5961 if (h->root.type == bfd_link_hash_warning)
5962 {
5963 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5964 if (h->root.type == bfd_link_hash_new)
5965 return TRUE;
5966 }
5967
5968 /* Decide whether to output this symbol in this pass. */
5969 if (eoinfo->localsyms)
5970 {
5971 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5972 return TRUE;
5973 }
5974 else
5975 {
5976 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5977 return TRUE;
5978 }
5979
5980 bed = get_elf_backend_data (finfo->output_bfd);
5981
5982 /* If we have an undefined symbol reference here then it must have
5983 come from a shared library that is being linked in. (Undefined
5984 references in regular files have already been handled). If we
5985 are reporting errors for this situation then do so now. */
5986 if (h->root.type == bfd_link_hash_undefined
5987 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
5988 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
5989 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
5990 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
5991 {
5992 if (! ((*finfo->info->callbacks->undefined_symbol)
5993 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
5994 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
5995 {
5996 eoinfo->failed = TRUE;
5997 return FALSE;
5998 }
5999 }
6000
6001 /* We should also warn if a forced local symbol is referenced from
6002 shared libraries. */
6003 if (! finfo->info->relocatable
6004 && (! finfo->info->shared)
6005 && (h->elf_link_hash_flags
6006 & (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_DYNAMIC_DEF | ELF_LINK_DYNAMIC_WEAK))
6007 == (ELF_LINK_FORCED_LOCAL | ELF_LINK_HASH_REF_DYNAMIC)
6008 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6009 {
6010 (*_bfd_error_handler)
6011 (_("%s: %s symbol `%s' in %s is referenced by DSO"),
6012 bfd_get_filename (finfo->output_bfd),
6013 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6014 ? "internal"
6015 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6016 ? "hidden" : "local",
6017 h->root.root.string,
6018 bfd_archive_filename (h->root.u.def.section->owner));
6019 eoinfo->failed = TRUE;
6020 return FALSE;
6021 }
6022
6023 /* We don't want to output symbols that have never been mentioned by
6024 a regular file, or that we have been told to strip. However, if
6025 h->indx is set to -2, the symbol is used by a reloc and we must
6026 output it. */
6027 if (h->indx == -2)
6028 strip = FALSE;
6029 else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6030 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6031 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6032 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6033 strip = TRUE;
6034 else if (finfo->info->strip == strip_all)
6035 strip = TRUE;
6036 else if (finfo->info->strip == strip_some
6037 && bfd_hash_lookup (finfo->info->keep_hash,
6038 h->root.root.string, FALSE, FALSE) == NULL)
6039 strip = TRUE;
6040 else if (finfo->info->strip_discarded
6041 && (h->root.type == bfd_link_hash_defined
6042 || h->root.type == bfd_link_hash_defweak)
6043 && elf_discarded_section (h->root.u.def.section))
6044 strip = TRUE;
6045 else
6046 strip = FALSE;
6047
6048 /* If we're stripping it, and it's not a dynamic symbol, there's
6049 nothing else to do unless it is a forced local symbol. */
6050 if (strip
6051 && h->dynindx == -1
6052 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6053 return TRUE;
6054
6055 sym.st_value = 0;
6056 sym.st_size = h->size;
6057 sym.st_other = h->other;
6058 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6059 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6060 else if (h->root.type == bfd_link_hash_undefweak
6061 || h->root.type == bfd_link_hash_defweak)
6062 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6063 else
6064 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6065
6066 switch (h->root.type)
6067 {
6068 default:
6069 case bfd_link_hash_new:
6070 case bfd_link_hash_warning:
6071 abort ();
6072 return FALSE;
6073
6074 case bfd_link_hash_undefined:
6075 case bfd_link_hash_undefweak:
6076 input_sec = bfd_und_section_ptr;
6077 sym.st_shndx = SHN_UNDEF;
6078 break;
6079
6080 case bfd_link_hash_defined:
6081 case bfd_link_hash_defweak:
6082 {
6083 input_sec = h->root.u.def.section;
6084 if (input_sec->output_section != NULL)
6085 {
6086 sym.st_shndx =
6087 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6088 input_sec->output_section);
6089 if (sym.st_shndx == SHN_BAD)
6090 {
6091 char *sec_name = bfd_get_section_ident (input_sec);
6092 (*_bfd_error_handler)
6093 (_("%s: could not find output section %s for input section %s"),
6094 bfd_get_filename (finfo->output_bfd),
6095 input_sec->output_section->name,
6096 sec_name ? sec_name : input_sec->name);
6097 if (sec_name)
6098 free (sec_name);
6099 eoinfo->failed = TRUE;
6100 return FALSE;
6101 }
6102
6103 /* ELF symbols in relocatable files are section relative,
6104 but in nonrelocatable files they are virtual
6105 addresses. */
6106 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6107 if (! finfo->info->relocatable)
6108 {
6109 sym.st_value += input_sec->output_section->vma;
6110 if (h->type == STT_TLS)
6111 {
6112 /* STT_TLS symbols are relative to PT_TLS segment
6113 base. */
6114 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6115 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6116 }
6117 }
6118 }
6119 else
6120 {
6121 BFD_ASSERT (input_sec->owner == NULL
6122 || (input_sec->owner->flags & DYNAMIC) != 0);
6123 sym.st_shndx = SHN_UNDEF;
6124 input_sec = bfd_und_section_ptr;
6125 }
6126 }
6127 break;
6128
6129 case bfd_link_hash_common:
6130 input_sec = h->root.u.c.p->section;
6131 sym.st_shndx = SHN_COMMON;
6132 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6133 break;
6134
6135 case bfd_link_hash_indirect:
6136 /* These symbols are created by symbol versioning. They point
6137 to the decorated version of the name. For example, if the
6138 symbol foo@@GNU_1.2 is the default, which should be used when
6139 foo is used with no version, then we add an indirect symbol
6140 foo which points to foo@@GNU_1.2. We ignore these symbols,
6141 since the indirected symbol is already in the hash table. */
6142 return TRUE;
6143 }
6144
6145 /* Give the processor backend a chance to tweak the symbol value,
6146 and also to finish up anything that needs to be done for this
6147 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6148 forced local syms when non-shared is due to a historical quirk. */
6149 if ((h->dynindx != -1
6150 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
6151 && ((finfo->info->shared
6152 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6153 || h->root.type != bfd_link_hash_undefweak))
6154 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
6155 && elf_hash_table (finfo->info)->dynamic_sections_created)
6156 {
6157 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6158 (finfo->output_bfd, finfo->info, h, &sym)))
6159 {
6160 eoinfo->failed = TRUE;
6161 return FALSE;
6162 }
6163 }
6164
6165 /* If we are marking the symbol as undefined, and there are no
6166 non-weak references to this symbol from a regular object, then
6167 mark the symbol as weak undefined; if there are non-weak
6168 references, mark the symbol as strong. We can't do this earlier,
6169 because it might not be marked as undefined until the
6170 finish_dynamic_symbol routine gets through with it. */
6171 if (sym.st_shndx == SHN_UNDEF
6172 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
6173 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6174 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6175 {
6176 int bindtype;
6177
6178 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) != 0)
6179 bindtype = STB_GLOBAL;
6180 else
6181 bindtype = STB_WEAK;
6182 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6183 }
6184
6185 /* If a non-weak symbol with non-default visibility is not defined
6186 locally, it is a fatal error. */
6187 if (! finfo->info->relocatable
6188 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6189 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6190 && h->root.type == bfd_link_hash_undefined
6191 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6192 {
6193 (*_bfd_error_handler)
6194 (_("%s: %s symbol `%s' isn't defined"),
6195 bfd_get_filename (finfo->output_bfd),
6196 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6197 ? "protected"
6198 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6199 ? "internal" : "hidden",
6200 h->root.root.string);
6201 eoinfo->failed = TRUE;
6202 return FALSE;
6203 }
6204
6205 /* If this symbol should be put in the .dynsym section, then put it
6206 there now. We already know the symbol index. We also fill in
6207 the entry in the .hash section. */
6208 if (h->dynindx != -1
6209 && elf_hash_table (finfo->info)->dynamic_sections_created)
6210 {
6211 size_t bucketcount;
6212 size_t bucket;
6213 size_t hash_entry_size;
6214 bfd_byte *bucketpos;
6215 bfd_vma chain;
6216 bfd_byte *esym;
6217
6218 sym.st_name = h->dynstr_index;
6219 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6220 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6221
6222 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6223 bucket = h->elf_hash_value % bucketcount;
6224 hash_entry_size
6225 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6226 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6227 + (bucket + 2) * hash_entry_size);
6228 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6229 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6230 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6231 ((bfd_byte *) finfo->hash_sec->contents
6232 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6233
6234 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6235 {
6236 Elf_Internal_Versym iversym;
6237 Elf_External_Versym *eversym;
6238
6239 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6240 {
6241 if (h->verinfo.verdef == NULL)
6242 iversym.vs_vers = 0;
6243 else
6244 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6245 }
6246 else
6247 {
6248 if (h->verinfo.vertree == NULL)
6249 iversym.vs_vers = 1;
6250 else
6251 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6252 }
6253
6254 if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
6255 iversym.vs_vers |= VERSYM_HIDDEN;
6256
6257 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6258 eversym += h->dynindx;
6259 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6260 }
6261 }
6262
6263 /* If we're stripping it, then it was just a dynamic symbol, and
6264 there's nothing else to do. */
6265 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6266 return TRUE;
6267
6268 h->indx = bfd_get_symcount (finfo->output_bfd);
6269
6270 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6271 {
6272 eoinfo->failed = TRUE;
6273 return FALSE;
6274 }
6275
6276 return TRUE;
6277 }
6278
6279 /* Return TRUE if special handling is done for relocs in SEC against
6280 symbols defined in discarded sections. */
6281
6282 static bfd_boolean
6283 elf_section_ignore_discarded_relocs (asection *sec)
6284 {
6285 const struct elf_backend_data *bed;
6286
6287 switch (sec->sec_info_type)
6288 {
6289 case ELF_INFO_TYPE_STABS:
6290 case ELF_INFO_TYPE_EH_FRAME:
6291 return TRUE;
6292 default:
6293 break;
6294 }
6295
6296 bed = get_elf_backend_data (sec->owner);
6297 if (bed->elf_backend_ignore_discarded_relocs != NULL
6298 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6299 return TRUE;
6300
6301 return FALSE;
6302 }
6303
6304 /* Return TRUE if we should complain about a reloc in SEC against a
6305 symbol defined in a discarded section. */
6306
6307 static bfd_boolean
6308 elf_section_complain_discarded (asection *sec)
6309 {
6310 if (strncmp (".stab", sec->name, 5) == 0
6311 && (!sec->name[5] ||
6312 (sec->name[5] == '.' && ISDIGIT (sec->name[6]))))
6313 return FALSE;
6314
6315 if (strcmp (".eh_frame", sec->name) == 0)
6316 return FALSE;
6317
6318 if (strcmp (".gcc_except_table", sec->name) == 0)
6319 return FALSE;
6320
6321 return TRUE;
6322 }
6323
6324 /* Link an input file into the linker output file. This function
6325 handles all the sections and relocations of the input file at once.
6326 This is so that we only have to read the local symbols once, and
6327 don't have to keep them in memory. */
6328
6329 static bfd_boolean
6330 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6331 {
6332 bfd_boolean (*relocate_section)
6333 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6334 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6335 bfd *output_bfd;
6336 Elf_Internal_Shdr *symtab_hdr;
6337 size_t locsymcount;
6338 size_t extsymoff;
6339 Elf_Internal_Sym *isymbuf;
6340 Elf_Internal_Sym *isym;
6341 Elf_Internal_Sym *isymend;
6342 long *pindex;
6343 asection **ppsection;
6344 asection *o;
6345 const struct elf_backend_data *bed;
6346 bfd_boolean emit_relocs;
6347 struct elf_link_hash_entry **sym_hashes;
6348
6349 output_bfd = finfo->output_bfd;
6350 bed = get_elf_backend_data (output_bfd);
6351 relocate_section = bed->elf_backend_relocate_section;
6352
6353 /* If this is a dynamic object, we don't want to do anything here:
6354 we don't want the local symbols, and we don't want the section
6355 contents. */
6356 if ((input_bfd->flags & DYNAMIC) != 0)
6357 return TRUE;
6358
6359 emit_relocs = (finfo->info->relocatable
6360 || finfo->info->emitrelocations
6361 || bed->elf_backend_emit_relocs);
6362
6363 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6364 if (elf_bad_symtab (input_bfd))
6365 {
6366 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6367 extsymoff = 0;
6368 }
6369 else
6370 {
6371 locsymcount = symtab_hdr->sh_info;
6372 extsymoff = symtab_hdr->sh_info;
6373 }
6374
6375 /* Read the local symbols. */
6376 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6377 if (isymbuf == NULL && locsymcount != 0)
6378 {
6379 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6380 finfo->internal_syms,
6381 finfo->external_syms,
6382 finfo->locsym_shndx);
6383 if (isymbuf == NULL)
6384 return FALSE;
6385 }
6386
6387 /* Find local symbol sections and adjust values of symbols in
6388 SEC_MERGE sections. Write out those local symbols we know are
6389 going into the output file. */
6390 isymend = isymbuf + locsymcount;
6391 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6392 isym < isymend;
6393 isym++, pindex++, ppsection++)
6394 {
6395 asection *isec;
6396 const char *name;
6397 Elf_Internal_Sym osym;
6398
6399 *pindex = -1;
6400
6401 if (elf_bad_symtab (input_bfd))
6402 {
6403 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6404 {
6405 *ppsection = NULL;
6406 continue;
6407 }
6408 }
6409
6410 if (isym->st_shndx == SHN_UNDEF)
6411 isec = bfd_und_section_ptr;
6412 else if (isym->st_shndx < SHN_LORESERVE
6413 || isym->st_shndx > SHN_HIRESERVE)
6414 {
6415 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6416 if (isec
6417 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6418 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6419 isym->st_value =
6420 _bfd_merged_section_offset (output_bfd, &isec,
6421 elf_section_data (isec)->sec_info,
6422 isym->st_value);
6423 }
6424 else if (isym->st_shndx == SHN_ABS)
6425 isec = bfd_abs_section_ptr;
6426 else if (isym->st_shndx == SHN_COMMON)
6427 isec = bfd_com_section_ptr;
6428 else
6429 {
6430 /* Who knows? */
6431 isec = NULL;
6432 }
6433
6434 *ppsection = isec;
6435
6436 /* Don't output the first, undefined, symbol. */
6437 if (ppsection == finfo->sections)
6438 continue;
6439
6440 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6441 {
6442 /* We never output section symbols. Instead, we use the
6443 section symbol of the corresponding section in the output
6444 file. */
6445 continue;
6446 }
6447
6448 /* If we are stripping all symbols, we don't want to output this
6449 one. */
6450 if (finfo->info->strip == strip_all)
6451 continue;
6452
6453 /* If we are discarding all local symbols, we don't want to
6454 output this one. If we are generating a relocatable output
6455 file, then some of the local symbols may be required by
6456 relocs; we output them below as we discover that they are
6457 needed. */
6458 if (finfo->info->discard == discard_all)
6459 continue;
6460
6461 /* If this symbol is defined in a section which we are
6462 discarding, we don't need to keep it, but note that
6463 linker_mark is only reliable for sections that have contents.
6464 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6465 as well as linker_mark. */
6466 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6467 && isec != NULL
6468 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6469 || (! finfo->info->relocatable
6470 && (isec->flags & SEC_EXCLUDE) != 0)))
6471 continue;
6472
6473 /* Get the name of the symbol. */
6474 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6475 isym->st_name);
6476 if (name == NULL)
6477 return FALSE;
6478
6479 /* See if we are discarding symbols with this name. */
6480 if ((finfo->info->strip == strip_some
6481 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6482 == NULL))
6483 || (((finfo->info->discard == discard_sec_merge
6484 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6485 || finfo->info->discard == discard_l)
6486 && bfd_is_local_label_name (input_bfd, name)))
6487 continue;
6488
6489 /* If we get here, we are going to output this symbol. */
6490
6491 osym = *isym;
6492
6493 /* Adjust the section index for the output file. */
6494 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6495 isec->output_section);
6496 if (osym.st_shndx == SHN_BAD)
6497 return FALSE;
6498
6499 *pindex = bfd_get_symcount (output_bfd);
6500
6501 /* ELF symbols in relocatable files are section relative, but
6502 in executable files they are virtual addresses. Note that
6503 this code assumes that all ELF sections have an associated
6504 BFD section with a reasonable value for output_offset; below
6505 we assume that they also have a reasonable value for
6506 output_section. Any special sections must be set up to meet
6507 these requirements. */
6508 osym.st_value += isec->output_offset;
6509 if (! finfo->info->relocatable)
6510 {
6511 osym.st_value += isec->output_section->vma;
6512 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6513 {
6514 /* STT_TLS symbols are relative to PT_TLS segment base. */
6515 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6516 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6517 }
6518 }
6519
6520 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6521 return FALSE;
6522 }
6523
6524 /* Relocate the contents of each section. */
6525 sym_hashes = elf_sym_hashes (input_bfd);
6526 for (o = input_bfd->sections; o != NULL; o = o->next)
6527 {
6528 bfd_byte *contents;
6529
6530 if (! o->linker_mark)
6531 {
6532 /* This section was omitted from the link. */
6533 continue;
6534 }
6535
6536 if ((o->flags & SEC_HAS_CONTENTS) == 0
6537 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6538 continue;
6539
6540 if ((o->flags & SEC_LINKER_CREATED) != 0)
6541 {
6542 /* Section was created by _bfd_elf_link_create_dynamic_sections
6543 or somesuch. */
6544 continue;
6545 }
6546
6547 /* Get the contents of the section. They have been cached by a
6548 relaxation routine. Note that o is a section in an input
6549 file, so the contents field will not have been set by any of
6550 the routines which work on output files. */
6551 if (elf_section_data (o)->this_hdr.contents != NULL)
6552 contents = elf_section_data (o)->this_hdr.contents;
6553 else
6554 {
6555 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6556
6557 contents = finfo->contents;
6558 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6559 return FALSE;
6560 }
6561
6562 if ((o->flags & SEC_RELOC) != 0)
6563 {
6564 Elf_Internal_Rela *internal_relocs;
6565 bfd_vma r_type_mask;
6566 int r_sym_shift;
6567
6568 /* Get the swapped relocs. */
6569 internal_relocs
6570 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6571 finfo->internal_relocs, FALSE);
6572 if (internal_relocs == NULL
6573 && o->reloc_count > 0)
6574 return FALSE;
6575
6576 if (bed->s->arch_size == 32)
6577 {
6578 r_type_mask = 0xff;
6579 r_sym_shift = 8;
6580 }
6581 else
6582 {
6583 r_type_mask = 0xffffffff;
6584 r_sym_shift = 32;
6585 }
6586
6587 /* Run through the relocs looking for any against symbols
6588 from discarded sections and section symbols from
6589 removed link-once sections. Complain about relocs
6590 against discarded sections. Zero relocs against removed
6591 link-once sections. Preserve debug information as much
6592 as we can. */
6593 if (!elf_section_ignore_discarded_relocs (o))
6594 {
6595 Elf_Internal_Rela *rel, *relend;
6596 bfd_boolean complain = elf_section_complain_discarded (o);
6597
6598 rel = internal_relocs;
6599 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6600 for ( ; rel < relend; rel++)
6601 {
6602 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6603 asection **ps, *sec;
6604 struct elf_link_hash_entry *h = NULL;
6605 const char *sym_name;
6606
6607 if (r_symndx >= locsymcount
6608 || (elf_bad_symtab (input_bfd)
6609 && finfo->sections[r_symndx] == NULL))
6610 {
6611 h = sym_hashes[r_symndx - extsymoff];
6612 while (h->root.type == bfd_link_hash_indirect
6613 || h->root.type == bfd_link_hash_warning)
6614 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6615
6616 if (h->root.type != bfd_link_hash_defined
6617 && h->root.type != bfd_link_hash_defweak)
6618 continue;
6619
6620 ps = &h->root.u.def.section;
6621 sym_name = h->root.root.string;
6622 }
6623 else
6624 {
6625 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6626 ps = &finfo->sections[r_symndx];
6627 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6628 }
6629
6630 /* Complain if the definition comes from a
6631 discarded section. */
6632 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6633 {
6634 if ((o->flags & SEC_DEBUGGING) != 0)
6635 {
6636 BFD_ASSERT (r_symndx != 0);
6637
6638 /* Try to preserve debug information.
6639 FIXME: This is quite broken. Modifying
6640 the symbol here means we will be changing
6641 all uses of the symbol, not just those in
6642 debug sections. The only thing that makes
6643 this half reasonable is that debug sections
6644 tend to come after other sections. Of
6645 course, that doesn't help with globals.
6646 ??? All link-once sections of the same name
6647 ought to define the same set of symbols, so
6648 it would seem that globals ought to always
6649 be defined in the kept section. */
6650 if (sec->kept_section != NULL
6651 && sec->size == sec->kept_section->size)
6652 {
6653 *ps = sec->kept_section;
6654 continue;
6655 }
6656 }
6657 else if (complain)
6658 {
6659 char *r_sec
6660 = bfd_get_section_ident (o);
6661 char *d_sec
6662 = bfd_get_section_ident (sec);
6663 finfo->info->callbacks->error_handler
6664 (LD_DEFINITION_IN_DISCARDED_SECTION,
6665 _("`%T' referenced in section `%s' of %B: "
6666 "defined in discarded section `%s' of %B\n"),
6667 sym_name, sym_name,
6668 r_sec ? r_sec : o->name, input_bfd,
6669 d_sec ? d_sec : sec->name, sec->owner);
6670 if (r_sec)
6671 free (r_sec);
6672 if (d_sec)
6673 free (d_sec);
6674 }
6675
6676 /* Remove the symbol reference from the reloc, but
6677 don't kill the reloc completely. This is so that
6678 a zero value will be written into the section,
6679 which may have non-zero contents put there by the
6680 assembler. Zero in things like an eh_frame fde
6681 pc_begin allows stack unwinders to recognize the
6682 fde as bogus. */
6683 rel->r_info &= r_type_mask;
6684 rel->r_addend = 0;
6685 }
6686 }
6687 }
6688
6689 /* Relocate the section by invoking a back end routine.
6690
6691 The back end routine is responsible for adjusting the
6692 section contents as necessary, and (if using Rela relocs
6693 and generating a relocatable output file) adjusting the
6694 reloc addend as necessary.
6695
6696 The back end routine does not have to worry about setting
6697 the reloc address or the reloc symbol index.
6698
6699 The back end routine is given a pointer to the swapped in
6700 internal symbols, and can access the hash table entries
6701 for the external symbols via elf_sym_hashes (input_bfd).
6702
6703 When generating relocatable output, the back end routine
6704 must handle STB_LOCAL/STT_SECTION symbols specially. The
6705 output symbol is going to be a section symbol
6706 corresponding to the output section, which will require
6707 the addend to be adjusted. */
6708
6709 if (! (*relocate_section) (output_bfd, finfo->info,
6710 input_bfd, o, contents,
6711 internal_relocs,
6712 isymbuf,
6713 finfo->sections))
6714 return FALSE;
6715
6716 if (emit_relocs)
6717 {
6718 Elf_Internal_Rela *irela;
6719 Elf_Internal_Rela *irelaend;
6720 bfd_vma last_offset;
6721 struct elf_link_hash_entry **rel_hash;
6722 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6723 unsigned int next_erel;
6724 bfd_boolean (*reloc_emitter)
6725 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6726 bfd_boolean rela_normal;
6727
6728 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6729 rela_normal = (bed->rela_normal
6730 && (input_rel_hdr->sh_entsize
6731 == bed->s->sizeof_rela));
6732
6733 /* Adjust the reloc addresses and symbol indices. */
6734
6735 irela = internal_relocs;
6736 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6737 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6738 + elf_section_data (o->output_section)->rel_count
6739 + elf_section_data (o->output_section)->rel_count2);
6740 last_offset = o->output_offset;
6741 if (!finfo->info->relocatable)
6742 last_offset += o->output_section->vma;
6743 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6744 {
6745 unsigned long r_symndx;
6746 asection *sec;
6747 Elf_Internal_Sym sym;
6748
6749 if (next_erel == bed->s->int_rels_per_ext_rel)
6750 {
6751 rel_hash++;
6752 next_erel = 0;
6753 }
6754
6755 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6756 finfo->info, o,
6757 irela->r_offset);
6758 if (irela->r_offset >= (bfd_vma) -2)
6759 {
6760 /* This is a reloc for a deleted entry or somesuch.
6761 Turn it into an R_*_NONE reloc, at the same
6762 offset as the last reloc. elf_eh_frame.c and
6763 elf_bfd_discard_info rely on reloc offsets
6764 being ordered. */
6765 irela->r_offset = last_offset;
6766 irela->r_info = 0;
6767 irela->r_addend = 0;
6768 continue;
6769 }
6770
6771 irela->r_offset += o->output_offset;
6772
6773 /* Relocs in an executable have to be virtual addresses. */
6774 if (!finfo->info->relocatable)
6775 irela->r_offset += o->output_section->vma;
6776
6777 last_offset = irela->r_offset;
6778
6779 r_symndx = irela->r_info >> r_sym_shift;
6780 if (r_symndx == STN_UNDEF)
6781 continue;
6782
6783 if (r_symndx >= locsymcount
6784 || (elf_bad_symtab (input_bfd)
6785 && finfo->sections[r_symndx] == NULL))
6786 {
6787 struct elf_link_hash_entry *rh;
6788 unsigned long indx;
6789
6790 /* This is a reloc against a global symbol. We
6791 have not yet output all the local symbols, so
6792 we do not know the symbol index of any global
6793 symbol. We set the rel_hash entry for this
6794 reloc to point to the global hash table entry
6795 for this symbol. The symbol index is then
6796 set at the end of elf_bfd_final_link. */
6797 indx = r_symndx - extsymoff;
6798 rh = elf_sym_hashes (input_bfd)[indx];
6799 while (rh->root.type == bfd_link_hash_indirect
6800 || rh->root.type == bfd_link_hash_warning)
6801 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6802
6803 /* Setting the index to -2 tells
6804 elf_link_output_extsym that this symbol is
6805 used by a reloc. */
6806 BFD_ASSERT (rh->indx < 0);
6807 rh->indx = -2;
6808
6809 *rel_hash = rh;
6810
6811 continue;
6812 }
6813
6814 /* This is a reloc against a local symbol. */
6815
6816 *rel_hash = NULL;
6817 sym = isymbuf[r_symndx];
6818 sec = finfo->sections[r_symndx];
6819 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
6820 {
6821 /* I suppose the backend ought to fill in the
6822 section of any STT_SECTION symbol against a
6823 processor specific section. */
6824 r_symndx = 0;
6825 if (bfd_is_abs_section (sec))
6826 ;
6827 else if (sec == NULL || sec->owner == NULL)
6828 {
6829 bfd_set_error (bfd_error_bad_value);
6830 return FALSE;
6831 }
6832 else
6833 {
6834 asection *osec = sec->output_section;
6835
6836 /* If we have discarded a section, the output
6837 section will be the absolute section. In
6838 case of discarded link-once and discarded
6839 SEC_MERGE sections, use the kept section. */
6840 if (bfd_is_abs_section (osec)
6841 && sec->kept_section != NULL
6842 && sec->kept_section->output_section != NULL)
6843 {
6844 osec = sec->kept_section->output_section;
6845 irela->r_addend -= osec->vma;
6846 }
6847
6848 if (!bfd_is_abs_section (osec))
6849 {
6850 r_symndx = osec->target_index;
6851 BFD_ASSERT (r_symndx != 0);
6852 }
6853 }
6854
6855 /* Adjust the addend according to where the
6856 section winds up in the output section. */
6857 if (rela_normal)
6858 irela->r_addend += sec->output_offset;
6859 }
6860 else
6861 {
6862 if (finfo->indices[r_symndx] == -1)
6863 {
6864 unsigned long shlink;
6865 const char *name;
6866 asection *osec;
6867
6868 if (finfo->info->strip == strip_all)
6869 {
6870 /* You can't do ld -r -s. */
6871 bfd_set_error (bfd_error_invalid_operation);
6872 return FALSE;
6873 }
6874
6875 /* This symbol was skipped earlier, but
6876 since it is needed by a reloc, we
6877 must output it now. */
6878 shlink = symtab_hdr->sh_link;
6879 name = (bfd_elf_string_from_elf_section
6880 (input_bfd, shlink, sym.st_name));
6881 if (name == NULL)
6882 return FALSE;
6883
6884 osec = sec->output_section;
6885 sym.st_shndx =
6886 _bfd_elf_section_from_bfd_section (output_bfd,
6887 osec);
6888 if (sym.st_shndx == SHN_BAD)
6889 return FALSE;
6890
6891 sym.st_value += sec->output_offset;
6892 if (! finfo->info->relocatable)
6893 {
6894 sym.st_value += osec->vma;
6895 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
6896 {
6897 /* STT_TLS symbols are relative to PT_TLS
6898 segment base. */
6899 BFD_ASSERT (elf_hash_table (finfo->info)
6900 ->tls_sec != NULL);
6901 sym.st_value -= (elf_hash_table (finfo->info)
6902 ->tls_sec->vma);
6903 }
6904 }
6905
6906 finfo->indices[r_symndx]
6907 = bfd_get_symcount (output_bfd);
6908
6909 if (! elf_link_output_sym (finfo, name, &sym, sec,
6910 NULL))
6911 return FALSE;
6912 }
6913
6914 r_symndx = finfo->indices[r_symndx];
6915 }
6916
6917 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
6918 | (irela->r_info & r_type_mask));
6919 }
6920
6921 /* Swap out the relocs. */
6922 if (bed->elf_backend_emit_relocs
6923 && !(finfo->info->relocatable
6924 || finfo->info->emitrelocations))
6925 reloc_emitter = bed->elf_backend_emit_relocs;
6926 else
6927 reloc_emitter = _bfd_elf_link_output_relocs;
6928
6929 if (input_rel_hdr->sh_size != 0
6930 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
6931 internal_relocs))
6932 return FALSE;
6933
6934 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
6935 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
6936 {
6937 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
6938 * bed->s->int_rels_per_ext_rel);
6939 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
6940 internal_relocs))
6941 return FALSE;
6942 }
6943 }
6944 }
6945
6946 /* Write out the modified section contents. */
6947 if (bed->elf_backend_write_section
6948 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
6949 {
6950 /* Section written out. */
6951 }
6952 else switch (o->sec_info_type)
6953 {
6954 case ELF_INFO_TYPE_STABS:
6955 if (! (_bfd_write_section_stabs
6956 (output_bfd,
6957 &elf_hash_table (finfo->info)->stab_info,
6958 o, &elf_section_data (o)->sec_info, contents)))
6959 return FALSE;
6960 break;
6961 case ELF_INFO_TYPE_MERGE:
6962 if (! _bfd_write_merged_section (output_bfd, o,
6963 elf_section_data (o)->sec_info))
6964 return FALSE;
6965 break;
6966 case ELF_INFO_TYPE_EH_FRAME:
6967 {
6968 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
6969 o, contents))
6970 return FALSE;
6971 }
6972 break;
6973 default:
6974 {
6975 if (! (o->flags & SEC_EXCLUDE)
6976 && ! bfd_set_section_contents (output_bfd, o->output_section,
6977 contents,
6978 (file_ptr) o->output_offset,
6979 o->size))
6980 return FALSE;
6981 }
6982 break;
6983 }
6984 }
6985
6986 return TRUE;
6987 }
6988
6989 /* Generate a reloc when linking an ELF file. This is a reloc
6990 requested by the linker, and does come from any input file. This
6991 is used to build constructor and destructor tables when linking
6992 with -Ur. */
6993
6994 static bfd_boolean
6995 elf_reloc_link_order (bfd *output_bfd,
6996 struct bfd_link_info *info,
6997 asection *output_section,
6998 struct bfd_link_order *link_order)
6999 {
7000 reloc_howto_type *howto;
7001 long indx;
7002 bfd_vma offset;
7003 bfd_vma addend;
7004 struct elf_link_hash_entry **rel_hash_ptr;
7005 Elf_Internal_Shdr *rel_hdr;
7006 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7007 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7008 bfd_byte *erel;
7009 unsigned int i;
7010
7011 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7012 if (howto == NULL)
7013 {
7014 bfd_set_error (bfd_error_bad_value);
7015 return FALSE;
7016 }
7017
7018 addend = link_order->u.reloc.p->addend;
7019
7020 /* Figure out the symbol index. */
7021 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7022 + elf_section_data (output_section)->rel_count
7023 + elf_section_data (output_section)->rel_count2);
7024 if (link_order->type == bfd_section_reloc_link_order)
7025 {
7026 indx = link_order->u.reloc.p->u.section->target_index;
7027 BFD_ASSERT (indx != 0);
7028 *rel_hash_ptr = NULL;
7029 }
7030 else
7031 {
7032 struct elf_link_hash_entry *h;
7033
7034 /* Treat a reloc against a defined symbol as though it were
7035 actually against the section. */
7036 h = ((struct elf_link_hash_entry *)
7037 bfd_wrapped_link_hash_lookup (output_bfd, info,
7038 link_order->u.reloc.p->u.name,
7039 FALSE, FALSE, TRUE));
7040 if (h != NULL
7041 && (h->root.type == bfd_link_hash_defined
7042 || h->root.type == bfd_link_hash_defweak))
7043 {
7044 asection *section;
7045
7046 section = h->root.u.def.section;
7047 indx = section->output_section->target_index;
7048 *rel_hash_ptr = NULL;
7049 /* It seems that we ought to add the symbol value to the
7050 addend here, but in practice it has already been added
7051 because it was passed to constructor_callback. */
7052 addend += section->output_section->vma + section->output_offset;
7053 }
7054 else if (h != NULL)
7055 {
7056 /* Setting the index to -2 tells elf_link_output_extsym that
7057 this symbol is used by a reloc. */
7058 h->indx = -2;
7059 *rel_hash_ptr = h;
7060 indx = 0;
7061 }
7062 else
7063 {
7064 if (! ((*info->callbacks->unattached_reloc)
7065 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7066 return FALSE;
7067 indx = 0;
7068 }
7069 }
7070
7071 /* If this is an inplace reloc, we must write the addend into the
7072 object file. */
7073 if (howto->partial_inplace && addend != 0)
7074 {
7075 bfd_size_type size;
7076 bfd_reloc_status_type rstat;
7077 bfd_byte *buf;
7078 bfd_boolean ok;
7079 const char *sym_name;
7080
7081 size = bfd_get_reloc_size (howto);
7082 buf = bfd_zmalloc (size);
7083 if (buf == NULL)
7084 return FALSE;
7085 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7086 switch (rstat)
7087 {
7088 case bfd_reloc_ok:
7089 break;
7090
7091 default:
7092 case bfd_reloc_outofrange:
7093 abort ();
7094
7095 case bfd_reloc_overflow:
7096 if (link_order->type == bfd_section_reloc_link_order)
7097 sym_name = bfd_section_name (output_bfd,
7098 link_order->u.reloc.p->u.section);
7099 else
7100 sym_name = link_order->u.reloc.p->u.name;
7101 if (! ((*info->callbacks->reloc_overflow)
7102 (info, sym_name, howto->name, addend, NULL, NULL, 0)))
7103 {
7104 free (buf);
7105 return FALSE;
7106 }
7107 break;
7108 }
7109 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7110 link_order->offset, size);
7111 free (buf);
7112 if (! ok)
7113 return FALSE;
7114 }
7115
7116 /* The address of a reloc is relative to the section in a
7117 relocatable file, and is a virtual address in an executable
7118 file. */
7119 offset = link_order->offset;
7120 if (! info->relocatable)
7121 offset += output_section->vma;
7122
7123 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7124 {
7125 irel[i].r_offset = offset;
7126 irel[i].r_info = 0;
7127 irel[i].r_addend = 0;
7128 }
7129 if (bed->s->arch_size == 32)
7130 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7131 else
7132 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7133
7134 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7135 erel = rel_hdr->contents;
7136 if (rel_hdr->sh_type == SHT_REL)
7137 {
7138 erel += (elf_section_data (output_section)->rel_count
7139 * bed->s->sizeof_rel);
7140 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7141 }
7142 else
7143 {
7144 irel[0].r_addend = addend;
7145 erel += (elf_section_data (output_section)->rel_count
7146 * bed->s->sizeof_rela);
7147 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7148 }
7149
7150 ++elf_section_data (output_section)->rel_count;
7151
7152 return TRUE;
7153 }
7154
7155 /* Do the final step of an ELF link. */
7156
7157 bfd_boolean
7158 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7159 {
7160 bfd_boolean dynamic;
7161 bfd_boolean emit_relocs;
7162 bfd *dynobj;
7163 struct elf_final_link_info finfo;
7164 register asection *o;
7165 register struct bfd_link_order *p;
7166 register bfd *sub;
7167 bfd_size_type max_contents_size;
7168 bfd_size_type max_external_reloc_size;
7169 bfd_size_type max_internal_reloc_count;
7170 bfd_size_type max_sym_count;
7171 bfd_size_type max_sym_shndx_count;
7172 file_ptr off;
7173 Elf_Internal_Sym elfsym;
7174 unsigned int i;
7175 Elf_Internal_Shdr *symtab_hdr;
7176 Elf_Internal_Shdr *symtab_shndx_hdr;
7177 Elf_Internal_Shdr *symstrtab_hdr;
7178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7179 struct elf_outext_info eoinfo;
7180 bfd_boolean merged;
7181 size_t relativecount = 0;
7182 asection *reldyn = 0;
7183 bfd_size_type amt;
7184
7185 if (! is_elf_hash_table (info->hash))
7186 return FALSE;
7187
7188 if (info->shared)
7189 abfd->flags |= DYNAMIC;
7190
7191 dynamic = elf_hash_table (info)->dynamic_sections_created;
7192 dynobj = elf_hash_table (info)->dynobj;
7193
7194 emit_relocs = (info->relocatable
7195 || info->emitrelocations
7196 || bed->elf_backend_emit_relocs);
7197
7198 finfo.info = info;
7199 finfo.output_bfd = abfd;
7200 finfo.symstrtab = _bfd_elf_stringtab_init ();
7201 if (finfo.symstrtab == NULL)
7202 return FALSE;
7203
7204 if (! dynamic)
7205 {
7206 finfo.dynsym_sec = NULL;
7207 finfo.hash_sec = NULL;
7208 finfo.symver_sec = NULL;
7209 }
7210 else
7211 {
7212 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7213 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7214 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7215 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7216 /* Note that it is OK if symver_sec is NULL. */
7217 }
7218
7219 finfo.contents = NULL;
7220 finfo.external_relocs = NULL;
7221 finfo.internal_relocs = NULL;
7222 finfo.external_syms = NULL;
7223 finfo.locsym_shndx = NULL;
7224 finfo.internal_syms = NULL;
7225 finfo.indices = NULL;
7226 finfo.sections = NULL;
7227 finfo.symbuf = NULL;
7228 finfo.symshndxbuf = NULL;
7229 finfo.symbuf_count = 0;
7230 finfo.shndxbuf_size = 0;
7231
7232 /* Count up the number of relocations we will output for each output
7233 section, so that we know the sizes of the reloc sections. We
7234 also figure out some maximum sizes. */
7235 max_contents_size = 0;
7236 max_external_reloc_size = 0;
7237 max_internal_reloc_count = 0;
7238 max_sym_count = 0;
7239 max_sym_shndx_count = 0;
7240 merged = FALSE;
7241 for (o = abfd->sections; o != NULL; o = o->next)
7242 {
7243 struct bfd_elf_section_data *esdo = elf_section_data (o);
7244 o->reloc_count = 0;
7245
7246 for (p = o->link_order_head; p != NULL; p = p->next)
7247 {
7248 unsigned int reloc_count = 0;
7249 struct bfd_elf_section_data *esdi = NULL;
7250 unsigned int *rel_count1;
7251
7252 if (p->type == bfd_section_reloc_link_order
7253 || p->type == bfd_symbol_reloc_link_order)
7254 reloc_count = 1;
7255 else if (p->type == bfd_indirect_link_order)
7256 {
7257 asection *sec;
7258
7259 sec = p->u.indirect.section;
7260 esdi = elf_section_data (sec);
7261
7262 /* Mark all sections which are to be included in the
7263 link. This will normally be every section. We need
7264 to do this so that we can identify any sections which
7265 the linker has decided to not include. */
7266 sec->linker_mark = TRUE;
7267
7268 if (sec->flags & SEC_MERGE)
7269 merged = TRUE;
7270
7271 if (info->relocatable || info->emitrelocations)
7272 reloc_count = sec->reloc_count;
7273 else if (bed->elf_backend_count_relocs)
7274 {
7275 Elf_Internal_Rela * relocs;
7276
7277 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7278 info->keep_memory);
7279
7280 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7281
7282 if (elf_section_data (o)->relocs != relocs)
7283 free (relocs);
7284 }
7285
7286 if (sec->rawsize > max_contents_size)
7287 max_contents_size = sec->rawsize;
7288 if (sec->size > max_contents_size)
7289 max_contents_size = sec->size;
7290
7291 /* We are interested in just local symbols, not all
7292 symbols. */
7293 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7294 && (sec->owner->flags & DYNAMIC) == 0)
7295 {
7296 size_t sym_count;
7297
7298 if (elf_bad_symtab (sec->owner))
7299 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7300 / bed->s->sizeof_sym);
7301 else
7302 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7303
7304 if (sym_count > max_sym_count)
7305 max_sym_count = sym_count;
7306
7307 if (sym_count > max_sym_shndx_count
7308 && elf_symtab_shndx (sec->owner) != 0)
7309 max_sym_shndx_count = sym_count;
7310
7311 if ((sec->flags & SEC_RELOC) != 0)
7312 {
7313 size_t ext_size;
7314
7315 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7316 if (ext_size > max_external_reloc_size)
7317 max_external_reloc_size = ext_size;
7318 if (sec->reloc_count > max_internal_reloc_count)
7319 max_internal_reloc_count = sec->reloc_count;
7320 }
7321 }
7322 }
7323
7324 if (reloc_count == 0)
7325 continue;
7326
7327 o->reloc_count += reloc_count;
7328
7329 /* MIPS may have a mix of REL and RELA relocs on sections.
7330 To support this curious ABI we keep reloc counts in
7331 elf_section_data too. We must be careful to add the
7332 relocations from the input section to the right output
7333 count. FIXME: Get rid of one count. We have
7334 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7335 rel_count1 = &esdo->rel_count;
7336 if (esdi != NULL)
7337 {
7338 bfd_boolean same_size;
7339 bfd_size_type entsize1;
7340
7341 entsize1 = esdi->rel_hdr.sh_entsize;
7342 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7343 || entsize1 == bed->s->sizeof_rela);
7344 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7345
7346 if (!same_size)
7347 rel_count1 = &esdo->rel_count2;
7348
7349 if (esdi->rel_hdr2 != NULL)
7350 {
7351 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7352 unsigned int alt_count;
7353 unsigned int *rel_count2;
7354
7355 BFD_ASSERT (entsize2 != entsize1
7356 && (entsize2 == bed->s->sizeof_rel
7357 || entsize2 == bed->s->sizeof_rela));
7358
7359 rel_count2 = &esdo->rel_count2;
7360 if (!same_size)
7361 rel_count2 = &esdo->rel_count;
7362
7363 /* The following is probably too simplistic if the
7364 backend counts output relocs unusually. */
7365 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7366 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7367 *rel_count2 += alt_count;
7368 reloc_count -= alt_count;
7369 }
7370 }
7371 *rel_count1 += reloc_count;
7372 }
7373
7374 if (o->reloc_count > 0)
7375 o->flags |= SEC_RELOC;
7376 else
7377 {
7378 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7379 set it (this is probably a bug) and if it is set
7380 assign_section_numbers will create a reloc section. */
7381 o->flags &=~ SEC_RELOC;
7382 }
7383
7384 /* If the SEC_ALLOC flag is not set, force the section VMA to
7385 zero. This is done in elf_fake_sections as well, but forcing
7386 the VMA to 0 here will ensure that relocs against these
7387 sections are handled correctly. */
7388 if ((o->flags & SEC_ALLOC) == 0
7389 && ! o->user_set_vma)
7390 o->vma = 0;
7391 }
7392
7393 if (! info->relocatable && merged)
7394 elf_link_hash_traverse (elf_hash_table (info),
7395 _bfd_elf_link_sec_merge_syms, abfd);
7396
7397 /* Figure out the file positions for everything but the symbol table
7398 and the relocs. We set symcount to force assign_section_numbers
7399 to create a symbol table. */
7400 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7401 BFD_ASSERT (! abfd->output_has_begun);
7402 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7403 goto error_return;
7404
7405 /* That created the reloc sections. Set their sizes, and assign
7406 them file positions, and allocate some buffers. */
7407 for (o = abfd->sections; o != NULL; o = o->next)
7408 {
7409 if ((o->flags & SEC_RELOC) != 0)
7410 {
7411 if (!(_bfd_elf_link_size_reloc_section
7412 (abfd, &elf_section_data (o)->rel_hdr, o)))
7413 goto error_return;
7414
7415 if (elf_section_data (o)->rel_hdr2
7416 && !(_bfd_elf_link_size_reloc_section
7417 (abfd, elf_section_data (o)->rel_hdr2, o)))
7418 goto error_return;
7419 }
7420
7421 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7422 to count upwards while actually outputting the relocations. */
7423 elf_section_data (o)->rel_count = 0;
7424 elf_section_data (o)->rel_count2 = 0;
7425 }
7426
7427 _bfd_elf_assign_file_positions_for_relocs (abfd);
7428
7429 /* We have now assigned file positions for all the sections except
7430 .symtab and .strtab. We start the .symtab section at the current
7431 file position, and write directly to it. We build the .strtab
7432 section in memory. */
7433 bfd_get_symcount (abfd) = 0;
7434 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7435 /* sh_name is set in prep_headers. */
7436 symtab_hdr->sh_type = SHT_SYMTAB;
7437 /* sh_flags, sh_addr and sh_size all start off zero. */
7438 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7439 /* sh_link is set in assign_section_numbers. */
7440 /* sh_info is set below. */
7441 /* sh_offset is set just below. */
7442 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7443
7444 off = elf_tdata (abfd)->next_file_pos;
7445 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7446
7447 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7448 incorrect. We do not yet know the size of the .symtab section.
7449 We correct next_file_pos below, after we do know the size. */
7450
7451 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7452 continuously seeking to the right position in the file. */
7453 if (! info->keep_memory || max_sym_count < 20)
7454 finfo.symbuf_size = 20;
7455 else
7456 finfo.symbuf_size = max_sym_count;
7457 amt = finfo.symbuf_size;
7458 amt *= bed->s->sizeof_sym;
7459 finfo.symbuf = bfd_malloc (amt);
7460 if (finfo.symbuf == NULL)
7461 goto error_return;
7462 if (elf_numsections (abfd) > SHN_LORESERVE)
7463 {
7464 /* Wild guess at number of output symbols. realloc'd as needed. */
7465 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7466 finfo.shndxbuf_size = amt;
7467 amt *= sizeof (Elf_External_Sym_Shndx);
7468 finfo.symshndxbuf = bfd_zmalloc (amt);
7469 if (finfo.symshndxbuf == NULL)
7470 goto error_return;
7471 }
7472
7473 /* Start writing out the symbol table. The first symbol is always a
7474 dummy symbol. */
7475 if (info->strip != strip_all
7476 || emit_relocs)
7477 {
7478 elfsym.st_value = 0;
7479 elfsym.st_size = 0;
7480 elfsym.st_info = 0;
7481 elfsym.st_other = 0;
7482 elfsym.st_shndx = SHN_UNDEF;
7483 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7484 NULL))
7485 goto error_return;
7486 }
7487
7488 #if 0
7489 /* Some standard ELF linkers do this, but we don't because it causes
7490 bootstrap comparison failures. */
7491 /* Output a file symbol for the output file as the second symbol.
7492 We output this even if we are discarding local symbols, although
7493 I'm not sure if this is correct. */
7494 elfsym.st_value = 0;
7495 elfsym.st_size = 0;
7496 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7497 elfsym.st_other = 0;
7498 elfsym.st_shndx = SHN_ABS;
7499 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7500 &elfsym, bfd_abs_section_ptr, NULL))
7501 goto error_return;
7502 #endif
7503
7504 /* Output a symbol for each section. We output these even if we are
7505 discarding local symbols, since they are used for relocs. These
7506 symbols have no names. We store the index of each one in the
7507 index field of the section, so that we can find it again when
7508 outputting relocs. */
7509 if (info->strip != strip_all
7510 || emit_relocs)
7511 {
7512 elfsym.st_size = 0;
7513 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7514 elfsym.st_other = 0;
7515 for (i = 1; i < elf_numsections (abfd); i++)
7516 {
7517 o = bfd_section_from_elf_index (abfd, i);
7518 if (o != NULL)
7519 o->target_index = bfd_get_symcount (abfd);
7520 elfsym.st_shndx = i;
7521 if (info->relocatable || o == NULL)
7522 elfsym.st_value = 0;
7523 else
7524 elfsym.st_value = o->vma;
7525 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7526 goto error_return;
7527 if (i == SHN_LORESERVE - 1)
7528 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7529 }
7530 }
7531
7532 /* Allocate some memory to hold information read in from the input
7533 files. */
7534 if (max_contents_size != 0)
7535 {
7536 finfo.contents = bfd_malloc (max_contents_size);
7537 if (finfo.contents == NULL)
7538 goto error_return;
7539 }
7540
7541 if (max_external_reloc_size != 0)
7542 {
7543 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7544 if (finfo.external_relocs == NULL)
7545 goto error_return;
7546 }
7547
7548 if (max_internal_reloc_count != 0)
7549 {
7550 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7551 amt *= sizeof (Elf_Internal_Rela);
7552 finfo.internal_relocs = bfd_malloc (amt);
7553 if (finfo.internal_relocs == NULL)
7554 goto error_return;
7555 }
7556
7557 if (max_sym_count != 0)
7558 {
7559 amt = max_sym_count * bed->s->sizeof_sym;
7560 finfo.external_syms = bfd_malloc (amt);
7561 if (finfo.external_syms == NULL)
7562 goto error_return;
7563
7564 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7565 finfo.internal_syms = bfd_malloc (amt);
7566 if (finfo.internal_syms == NULL)
7567 goto error_return;
7568
7569 amt = max_sym_count * sizeof (long);
7570 finfo.indices = bfd_malloc (amt);
7571 if (finfo.indices == NULL)
7572 goto error_return;
7573
7574 amt = max_sym_count * sizeof (asection *);
7575 finfo.sections = bfd_malloc (amt);
7576 if (finfo.sections == NULL)
7577 goto error_return;
7578 }
7579
7580 if (max_sym_shndx_count != 0)
7581 {
7582 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7583 finfo.locsym_shndx = bfd_malloc (amt);
7584 if (finfo.locsym_shndx == NULL)
7585 goto error_return;
7586 }
7587
7588 if (elf_hash_table (info)->tls_sec)
7589 {
7590 bfd_vma base, end = 0;
7591 asection *sec;
7592
7593 for (sec = elf_hash_table (info)->tls_sec;
7594 sec && (sec->flags & SEC_THREAD_LOCAL);
7595 sec = sec->next)
7596 {
7597 bfd_vma size = sec->size;
7598
7599 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7600 {
7601 struct bfd_link_order *o;
7602
7603 for (o = sec->link_order_head; o != NULL; o = o->next)
7604 if (size < o->offset + o->size)
7605 size = o->offset + o->size;
7606 }
7607 end = sec->vma + size;
7608 }
7609 base = elf_hash_table (info)->tls_sec->vma;
7610 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7611 elf_hash_table (info)->tls_size = end - base;
7612 }
7613
7614 /* Since ELF permits relocations to be against local symbols, we
7615 must have the local symbols available when we do the relocations.
7616 Since we would rather only read the local symbols once, and we
7617 would rather not keep them in memory, we handle all the
7618 relocations for a single input file at the same time.
7619
7620 Unfortunately, there is no way to know the total number of local
7621 symbols until we have seen all of them, and the local symbol
7622 indices precede the global symbol indices. This means that when
7623 we are generating relocatable output, and we see a reloc against
7624 a global symbol, we can not know the symbol index until we have
7625 finished examining all the local symbols to see which ones we are
7626 going to output. To deal with this, we keep the relocations in
7627 memory, and don't output them until the end of the link. This is
7628 an unfortunate waste of memory, but I don't see a good way around
7629 it. Fortunately, it only happens when performing a relocatable
7630 link, which is not the common case. FIXME: If keep_memory is set
7631 we could write the relocs out and then read them again; I don't
7632 know how bad the memory loss will be. */
7633
7634 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7635 sub->output_has_begun = FALSE;
7636 for (o = abfd->sections; o != NULL; o = o->next)
7637 {
7638 for (p = o->link_order_head; p != NULL; p = p->next)
7639 {
7640 if (p->type == bfd_indirect_link_order
7641 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7642 == bfd_target_elf_flavour)
7643 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7644 {
7645 if (! sub->output_has_begun)
7646 {
7647 if (! elf_link_input_bfd (&finfo, sub))
7648 goto error_return;
7649 sub->output_has_begun = TRUE;
7650 }
7651 }
7652 else if (p->type == bfd_section_reloc_link_order
7653 || p->type == bfd_symbol_reloc_link_order)
7654 {
7655 if (! elf_reloc_link_order (abfd, info, o, p))
7656 goto error_return;
7657 }
7658 else
7659 {
7660 if (! _bfd_default_link_order (abfd, info, o, p))
7661 goto error_return;
7662 }
7663 }
7664 }
7665
7666 /* Output any global symbols that got converted to local in a
7667 version script or due to symbol visibility. We do this in a
7668 separate step since ELF requires all local symbols to appear
7669 prior to any global symbols. FIXME: We should only do this if
7670 some global symbols were, in fact, converted to become local.
7671 FIXME: Will this work correctly with the Irix 5 linker? */
7672 eoinfo.failed = FALSE;
7673 eoinfo.finfo = &finfo;
7674 eoinfo.localsyms = TRUE;
7675 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7676 &eoinfo);
7677 if (eoinfo.failed)
7678 return FALSE;
7679
7680 /* That wrote out all the local symbols. Finish up the symbol table
7681 with the global symbols. Even if we want to strip everything we
7682 can, we still need to deal with those global symbols that got
7683 converted to local in a version script. */
7684
7685 /* The sh_info field records the index of the first non local symbol. */
7686 symtab_hdr->sh_info = bfd_get_symcount (abfd);
7687
7688 if (dynamic
7689 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
7690 {
7691 Elf_Internal_Sym sym;
7692 bfd_byte *dynsym = finfo.dynsym_sec->contents;
7693 long last_local = 0;
7694
7695 /* Write out the section symbols for the output sections. */
7696 if (info->shared)
7697 {
7698 asection *s;
7699
7700 sym.st_size = 0;
7701 sym.st_name = 0;
7702 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7703 sym.st_other = 0;
7704
7705 for (s = abfd->sections; s != NULL; s = s->next)
7706 {
7707 int indx;
7708 bfd_byte *dest;
7709 long dynindx;
7710
7711 dynindx = elf_section_data (s)->dynindx;
7712 if (dynindx <= 0)
7713 continue;
7714 indx = elf_section_data (s)->this_idx;
7715 BFD_ASSERT (indx > 0);
7716 sym.st_shndx = indx;
7717 sym.st_value = s->vma;
7718 dest = dynsym + dynindx * bed->s->sizeof_sym;
7719 if (last_local < dynindx)
7720 last_local = dynindx;
7721 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7722 }
7723 }
7724
7725 /* Write out the local dynsyms. */
7726 if (elf_hash_table (info)->dynlocal)
7727 {
7728 struct elf_link_local_dynamic_entry *e;
7729 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
7730 {
7731 asection *s;
7732 bfd_byte *dest;
7733
7734 sym.st_size = e->isym.st_size;
7735 sym.st_other = e->isym.st_other;
7736
7737 /* Copy the internal symbol as is.
7738 Note that we saved a word of storage and overwrote
7739 the original st_name with the dynstr_index. */
7740 sym = e->isym;
7741
7742 if (e->isym.st_shndx != SHN_UNDEF
7743 && (e->isym.st_shndx < SHN_LORESERVE
7744 || e->isym.st_shndx > SHN_HIRESERVE))
7745 {
7746 s = bfd_section_from_elf_index (e->input_bfd,
7747 e->isym.st_shndx);
7748
7749 sym.st_shndx =
7750 elf_section_data (s->output_section)->this_idx;
7751 sym.st_value = (s->output_section->vma
7752 + s->output_offset
7753 + e->isym.st_value);
7754 }
7755
7756 if (last_local < e->dynindx)
7757 last_local = e->dynindx;
7758
7759 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
7760 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
7761 }
7762 }
7763
7764 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
7765 last_local + 1;
7766 }
7767
7768 /* We get the global symbols from the hash table. */
7769 eoinfo.failed = FALSE;
7770 eoinfo.localsyms = FALSE;
7771 eoinfo.finfo = &finfo;
7772 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
7773 &eoinfo);
7774 if (eoinfo.failed)
7775 return FALSE;
7776
7777 /* If backend needs to output some symbols not present in the hash
7778 table, do it now. */
7779 if (bed->elf_backend_output_arch_syms)
7780 {
7781 typedef bfd_boolean (*out_sym_func)
7782 (void *, const char *, Elf_Internal_Sym *, asection *,
7783 struct elf_link_hash_entry *);
7784
7785 if (! ((*bed->elf_backend_output_arch_syms)
7786 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
7787 return FALSE;
7788 }
7789
7790 /* Flush all symbols to the file. */
7791 if (! elf_link_flush_output_syms (&finfo, bed))
7792 return FALSE;
7793
7794 /* Now we know the size of the symtab section. */
7795 off += symtab_hdr->sh_size;
7796
7797 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7798 if (symtab_shndx_hdr->sh_name != 0)
7799 {
7800 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7801 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7802 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7803 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
7804 symtab_shndx_hdr->sh_size = amt;
7805
7806 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
7807 off, TRUE);
7808
7809 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
7810 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
7811 return FALSE;
7812 }
7813
7814
7815 /* Finish up and write out the symbol string table (.strtab)
7816 section. */
7817 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7818 /* sh_name was set in prep_headers. */
7819 symstrtab_hdr->sh_type = SHT_STRTAB;
7820 symstrtab_hdr->sh_flags = 0;
7821 symstrtab_hdr->sh_addr = 0;
7822 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
7823 symstrtab_hdr->sh_entsize = 0;
7824 symstrtab_hdr->sh_link = 0;
7825 symstrtab_hdr->sh_info = 0;
7826 /* sh_offset is set just below. */
7827 symstrtab_hdr->sh_addralign = 1;
7828
7829 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
7830 elf_tdata (abfd)->next_file_pos = off;
7831
7832 if (bfd_get_symcount (abfd) > 0)
7833 {
7834 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
7835 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
7836 return FALSE;
7837 }
7838
7839 /* Adjust the relocs to have the correct symbol indices. */
7840 for (o = abfd->sections; o != NULL; o = o->next)
7841 {
7842 if ((o->flags & SEC_RELOC) == 0)
7843 continue;
7844
7845 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
7846 elf_section_data (o)->rel_count,
7847 elf_section_data (o)->rel_hashes);
7848 if (elf_section_data (o)->rel_hdr2 != NULL)
7849 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
7850 elf_section_data (o)->rel_count2,
7851 (elf_section_data (o)->rel_hashes
7852 + elf_section_data (o)->rel_count));
7853
7854 /* Set the reloc_count field to 0 to prevent write_relocs from
7855 trying to swap the relocs out itself. */
7856 o->reloc_count = 0;
7857 }
7858
7859 if (dynamic && info->combreloc && dynobj != NULL)
7860 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
7861
7862 /* If we are linking against a dynamic object, or generating a
7863 shared library, finish up the dynamic linking information. */
7864 if (dynamic)
7865 {
7866 bfd_byte *dyncon, *dynconend;
7867
7868 /* Fix up .dynamic entries. */
7869 o = bfd_get_section_by_name (dynobj, ".dynamic");
7870 BFD_ASSERT (o != NULL);
7871
7872 dyncon = o->contents;
7873 dynconend = o->contents + o->size;
7874 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
7875 {
7876 Elf_Internal_Dyn dyn;
7877 const char *name;
7878 unsigned int type;
7879
7880 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
7881
7882 switch (dyn.d_tag)
7883 {
7884 default:
7885 continue;
7886 case DT_NULL:
7887 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
7888 {
7889 switch (elf_section_data (reldyn)->this_hdr.sh_type)
7890 {
7891 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
7892 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
7893 default: continue;
7894 }
7895 dyn.d_un.d_val = relativecount;
7896 relativecount = 0;
7897 break;
7898 }
7899 continue;
7900
7901 case DT_INIT:
7902 name = info->init_function;
7903 goto get_sym;
7904 case DT_FINI:
7905 name = info->fini_function;
7906 get_sym:
7907 {
7908 struct elf_link_hash_entry *h;
7909
7910 h = elf_link_hash_lookup (elf_hash_table (info), name,
7911 FALSE, FALSE, TRUE);
7912 if (h != NULL
7913 && (h->root.type == bfd_link_hash_defined
7914 || h->root.type == bfd_link_hash_defweak))
7915 {
7916 dyn.d_un.d_val = h->root.u.def.value;
7917 o = h->root.u.def.section;
7918 if (o->output_section != NULL)
7919 dyn.d_un.d_val += (o->output_section->vma
7920 + o->output_offset);
7921 else
7922 {
7923 /* The symbol is imported from another shared
7924 library and does not apply to this one. */
7925 dyn.d_un.d_val = 0;
7926 }
7927 break;
7928 }
7929 }
7930 continue;
7931
7932 case DT_PREINIT_ARRAYSZ:
7933 name = ".preinit_array";
7934 goto get_size;
7935 case DT_INIT_ARRAYSZ:
7936 name = ".init_array";
7937 goto get_size;
7938 case DT_FINI_ARRAYSZ:
7939 name = ".fini_array";
7940 get_size:
7941 o = bfd_get_section_by_name (abfd, name);
7942 if (o == NULL)
7943 {
7944 (*_bfd_error_handler)
7945 (_("%s: could not find output section %s"),
7946 bfd_get_filename (abfd), name);
7947 goto error_return;
7948 }
7949 if (o->size == 0)
7950 (*_bfd_error_handler)
7951 (_("warning: %s section has zero size"), name);
7952 dyn.d_un.d_val = o->size;
7953 break;
7954
7955 case DT_PREINIT_ARRAY:
7956 name = ".preinit_array";
7957 goto get_vma;
7958 case DT_INIT_ARRAY:
7959 name = ".init_array";
7960 goto get_vma;
7961 case DT_FINI_ARRAY:
7962 name = ".fini_array";
7963 goto get_vma;
7964
7965 case DT_HASH:
7966 name = ".hash";
7967 goto get_vma;
7968 case DT_STRTAB:
7969 name = ".dynstr";
7970 goto get_vma;
7971 case DT_SYMTAB:
7972 name = ".dynsym";
7973 goto get_vma;
7974 case DT_VERDEF:
7975 name = ".gnu.version_d";
7976 goto get_vma;
7977 case DT_VERNEED:
7978 name = ".gnu.version_r";
7979 goto get_vma;
7980 case DT_VERSYM:
7981 name = ".gnu.version";
7982 get_vma:
7983 o = bfd_get_section_by_name (abfd, name);
7984 if (o == NULL)
7985 {
7986 (*_bfd_error_handler)
7987 (_("%s: could not find output section %s"),
7988 bfd_get_filename (abfd), name);
7989 goto error_return;
7990 }
7991 dyn.d_un.d_ptr = o->vma;
7992 break;
7993
7994 case DT_REL:
7995 case DT_RELA:
7996 case DT_RELSZ:
7997 case DT_RELASZ:
7998 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
7999 type = SHT_REL;
8000 else
8001 type = SHT_RELA;
8002 dyn.d_un.d_val = 0;
8003 for (i = 1; i < elf_numsections (abfd); i++)
8004 {
8005 Elf_Internal_Shdr *hdr;
8006
8007 hdr = elf_elfsections (abfd)[i];
8008 if (hdr->sh_type == type
8009 && (hdr->sh_flags & SHF_ALLOC) != 0)
8010 {
8011 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8012 dyn.d_un.d_val += hdr->sh_size;
8013 else
8014 {
8015 if (dyn.d_un.d_val == 0
8016 || hdr->sh_addr < dyn.d_un.d_val)
8017 dyn.d_un.d_val = hdr->sh_addr;
8018 }
8019 }
8020 }
8021 break;
8022 }
8023 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8024 }
8025 }
8026
8027 /* If we have created any dynamic sections, then output them. */
8028 if (dynobj != NULL)
8029 {
8030 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8031 goto error_return;
8032
8033 for (o = dynobj->sections; o != NULL; o = o->next)
8034 {
8035 if ((o->flags & SEC_HAS_CONTENTS) == 0
8036 || o->size == 0
8037 || o->output_section == bfd_abs_section_ptr)
8038 continue;
8039 if ((o->flags & SEC_LINKER_CREATED) == 0)
8040 {
8041 /* At this point, we are only interested in sections
8042 created by _bfd_elf_link_create_dynamic_sections. */
8043 continue;
8044 }
8045 if (elf_hash_table (info)->stab_info.stabstr == o)
8046 continue;
8047 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8048 continue;
8049 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8050 != SHT_STRTAB)
8051 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8052 {
8053 if (! bfd_set_section_contents (abfd, o->output_section,
8054 o->contents,
8055 (file_ptr) o->output_offset,
8056 o->size))
8057 goto error_return;
8058 }
8059 else
8060 {
8061 /* The contents of the .dynstr section are actually in a
8062 stringtab. */
8063 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8064 if (bfd_seek (abfd, off, SEEK_SET) != 0
8065 || ! _bfd_elf_strtab_emit (abfd,
8066 elf_hash_table (info)->dynstr))
8067 goto error_return;
8068 }
8069 }
8070 }
8071
8072 if (info->relocatable)
8073 {
8074 bfd_boolean failed = FALSE;
8075
8076 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8077 if (failed)
8078 goto error_return;
8079 }
8080
8081 /* If we have optimized stabs strings, output them. */
8082 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8083 {
8084 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8085 goto error_return;
8086 }
8087
8088 if (info->eh_frame_hdr)
8089 {
8090 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8091 goto error_return;
8092 }
8093
8094 if (finfo.symstrtab != NULL)
8095 _bfd_stringtab_free (finfo.symstrtab);
8096 if (finfo.contents != NULL)
8097 free (finfo.contents);
8098 if (finfo.external_relocs != NULL)
8099 free (finfo.external_relocs);
8100 if (finfo.internal_relocs != NULL)
8101 free (finfo.internal_relocs);
8102 if (finfo.external_syms != NULL)
8103 free (finfo.external_syms);
8104 if (finfo.locsym_shndx != NULL)
8105 free (finfo.locsym_shndx);
8106 if (finfo.internal_syms != NULL)
8107 free (finfo.internal_syms);
8108 if (finfo.indices != NULL)
8109 free (finfo.indices);
8110 if (finfo.sections != NULL)
8111 free (finfo.sections);
8112 if (finfo.symbuf != NULL)
8113 free (finfo.symbuf);
8114 if (finfo.symshndxbuf != NULL)
8115 free (finfo.symshndxbuf);
8116 for (o = abfd->sections; o != NULL; o = o->next)
8117 {
8118 if ((o->flags & SEC_RELOC) != 0
8119 && elf_section_data (o)->rel_hashes != NULL)
8120 free (elf_section_data (o)->rel_hashes);
8121 }
8122
8123 elf_tdata (abfd)->linker = TRUE;
8124
8125 return TRUE;
8126
8127 error_return:
8128 if (finfo.symstrtab != NULL)
8129 _bfd_stringtab_free (finfo.symstrtab);
8130 if (finfo.contents != NULL)
8131 free (finfo.contents);
8132 if (finfo.external_relocs != NULL)
8133 free (finfo.external_relocs);
8134 if (finfo.internal_relocs != NULL)
8135 free (finfo.internal_relocs);
8136 if (finfo.external_syms != NULL)
8137 free (finfo.external_syms);
8138 if (finfo.locsym_shndx != NULL)
8139 free (finfo.locsym_shndx);
8140 if (finfo.internal_syms != NULL)
8141 free (finfo.internal_syms);
8142 if (finfo.indices != NULL)
8143 free (finfo.indices);
8144 if (finfo.sections != NULL)
8145 free (finfo.sections);
8146 if (finfo.symbuf != NULL)
8147 free (finfo.symbuf);
8148 if (finfo.symshndxbuf != NULL)
8149 free (finfo.symshndxbuf);
8150 for (o = abfd->sections; o != NULL; o = o->next)
8151 {
8152 if ((o->flags & SEC_RELOC) != 0
8153 && elf_section_data (o)->rel_hashes != NULL)
8154 free (elf_section_data (o)->rel_hashes);
8155 }
8156
8157 return FALSE;
8158 }
8159 \f
8160 /* Garbage collect unused sections. */
8161
8162 /* The mark phase of garbage collection. For a given section, mark
8163 it and any sections in this section's group, and all the sections
8164 which define symbols to which it refers. */
8165
8166 typedef asection * (*gc_mark_hook_fn)
8167 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8168 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8169
8170 static bfd_boolean
8171 elf_gc_mark (struct bfd_link_info *info,
8172 asection *sec,
8173 gc_mark_hook_fn gc_mark_hook)
8174 {
8175 bfd_boolean ret;
8176 asection *group_sec;
8177
8178 sec->gc_mark = 1;
8179
8180 /* Mark all the sections in the group. */
8181 group_sec = elf_section_data (sec)->next_in_group;
8182 if (group_sec && !group_sec->gc_mark)
8183 if (!elf_gc_mark (info, group_sec, gc_mark_hook))
8184 return FALSE;
8185
8186 /* Look through the section relocs. */
8187 ret = TRUE;
8188 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8189 {
8190 Elf_Internal_Rela *relstart, *rel, *relend;
8191 Elf_Internal_Shdr *symtab_hdr;
8192 struct elf_link_hash_entry **sym_hashes;
8193 size_t nlocsyms;
8194 size_t extsymoff;
8195 bfd *input_bfd = sec->owner;
8196 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8197 Elf_Internal_Sym *isym = NULL;
8198 int r_sym_shift;
8199
8200 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8201 sym_hashes = elf_sym_hashes (input_bfd);
8202
8203 /* Read the local symbols. */
8204 if (elf_bad_symtab (input_bfd))
8205 {
8206 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8207 extsymoff = 0;
8208 }
8209 else
8210 extsymoff = nlocsyms = symtab_hdr->sh_info;
8211
8212 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8213 if (isym == NULL && nlocsyms != 0)
8214 {
8215 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8216 NULL, NULL, NULL);
8217 if (isym == NULL)
8218 return FALSE;
8219 }
8220
8221 /* Read the relocations. */
8222 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8223 info->keep_memory);
8224 if (relstart == NULL)
8225 {
8226 ret = FALSE;
8227 goto out1;
8228 }
8229 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8230
8231 if (bed->s->arch_size == 32)
8232 r_sym_shift = 8;
8233 else
8234 r_sym_shift = 32;
8235
8236 for (rel = relstart; rel < relend; rel++)
8237 {
8238 unsigned long r_symndx;
8239 asection *rsec;
8240 struct elf_link_hash_entry *h;
8241
8242 r_symndx = rel->r_info >> r_sym_shift;
8243 if (r_symndx == 0)
8244 continue;
8245
8246 if (r_symndx >= nlocsyms
8247 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8248 {
8249 h = sym_hashes[r_symndx - extsymoff];
8250 while (h->root.type == bfd_link_hash_indirect
8251 || h->root.type == bfd_link_hash_warning)
8252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8253 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8254 }
8255 else
8256 {
8257 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8258 }
8259
8260 if (rsec && !rsec->gc_mark)
8261 {
8262 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8263 rsec->gc_mark = 1;
8264 else if (!elf_gc_mark (info, rsec, gc_mark_hook))
8265 {
8266 ret = FALSE;
8267 goto out2;
8268 }
8269 }
8270 }
8271
8272 out2:
8273 if (elf_section_data (sec)->relocs != relstart)
8274 free (relstart);
8275 out1:
8276 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8277 {
8278 if (! info->keep_memory)
8279 free (isym);
8280 else
8281 symtab_hdr->contents = (unsigned char *) isym;
8282 }
8283 }
8284
8285 return ret;
8286 }
8287
8288 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8289
8290 static bfd_boolean
8291 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8292 {
8293 int *idx = idxptr;
8294
8295 if (h->root.type == bfd_link_hash_warning)
8296 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8297
8298 if (h->dynindx != -1
8299 && ((h->root.type != bfd_link_hash_defined
8300 && h->root.type != bfd_link_hash_defweak)
8301 || h->root.u.def.section->gc_mark))
8302 h->dynindx = (*idx)++;
8303
8304 return TRUE;
8305 }
8306
8307 /* The sweep phase of garbage collection. Remove all garbage sections. */
8308
8309 typedef bfd_boolean (*gc_sweep_hook_fn)
8310 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8311
8312 static bfd_boolean
8313 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8314 {
8315 bfd *sub;
8316
8317 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8318 {
8319 asection *o;
8320
8321 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8322 continue;
8323
8324 for (o = sub->sections; o != NULL; o = o->next)
8325 {
8326 /* Keep special sections. Keep .debug sections. */
8327 if ((o->flags & SEC_LINKER_CREATED)
8328 || (o->flags & SEC_DEBUGGING))
8329 o->gc_mark = 1;
8330
8331 if (o->gc_mark)
8332 continue;
8333
8334 /* Skip sweeping sections already excluded. */
8335 if (o->flags & SEC_EXCLUDE)
8336 continue;
8337
8338 /* Since this is early in the link process, it is simple
8339 to remove a section from the output. */
8340 o->flags |= SEC_EXCLUDE;
8341
8342 /* But we also have to update some of the relocation
8343 info we collected before. */
8344 if (gc_sweep_hook
8345 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8346 {
8347 Elf_Internal_Rela *internal_relocs;
8348 bfd_boolean r;
8349
8350 internal_relocs
8351 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8352 info->keep_memory);
8353 if (internal_relocs == NULL)
8354 return FALSE;
8355
8356 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8357
8358 if (elf_section_data (o)->relocs != internal_relocs)
8359 free (internal_relocs);
8360
8361 if (!r)
8362 return FALSE;
8363 }
8364 }
8365 }
8366
8367 /* Remove the symbols that were in the swept sections from the dynamic
8368 symbol table. GCFIXME: Anyone know how to get them out of the
8369 static symbol table as well? */
8370 {
8371 int i = 0;
8372
8373 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8374
8375 elf_hash_table (info)->dynsymcount = i;
8376 }
8377
8378 return TRUE;
8379 }
8380
8381 /* Propagate collected vtable information. This is called through
8382 elf_link_hash_traverse. */
8383
8384 static bfd_boolean
8385 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8386 {
8387 if (h->root.type == bfd_link_hash_warning)
8388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8389
8390 /* Those that are not vtables. */
8391 if (h->vtable_parent == NULL)
8392 return TRUE;
8393
8394 /* Those vtables that do not have parents, we cannot merge. */
8395 if (h->vtable_parent == (struct elf_link_hash_entry *) -1)
8396 return TRUE;
8397
8398 /* If we've already been done, exit. */
8399 if (h->vtable_entries_used && h->vtable_entries_used[-1])
8400 return TRUE;
8401
8402 /* Make sure the parent's table is up to date. */
8403 elf_gc_propagate_vtable_entries_used (h->vtable_parent, okp);
8404
8405 if (h->vtable_entries_used == NULL)
8406 {
8407 /* None of this table's entries were referenced. Re-use the
8408 parent's table. */
8409 h->vtable_entries_used = h->vtable_parent->vtable_entries_used;
8410 h->vtable_entries_size = h->vtable_parent->vtable_entries_size;
8411 }
8412 else
8413 {
8414 size_t n;
8415 bfd_boolean *cu, *pu;
8416
8417 /* Or the parent's entries into ours. */
8418 cu = h->vtable_entries_used;
8419 cu[-1] = TRUE;
8420 pu = h->vtable_parent->vtable_entries_used;
8421 if (pu != NULL)
8422 {
8423 const struct elf_backend_data *bed;
8424 unsigned int log_file_align;
8425
8426 bed = get_elf_backend_data (h->root.u.def.section->owner);
8427 log_file_align = bed->s->log_file_align;
8428 n = h->vtable_parent->vtable_entries_size >> log_file_align;
8429 while (n--)
8430 {
8431 if (*pu)
8432 *cu = TRUE;
8433 pu++;
8434 cu++;
8435 }
8436 }
8437 }
8438
8439 return TRUE;
8440 }
8441
8442 static bfd_boolean
8443 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8444 {
8445 asection *sec;
8446 bfd_vma hstart, hend;
8447 Elf_Internal_Rela *relstart, *relend, *rel;
8448 const struct elf_backend_data *bed;
8449 unsigned int log_file_align;
8450
8451 if (h->root.type == bfd_link_hash_warning)
8452 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8453
8454 /* Take care of both those symbols that do not describe vtables as
8455 well as those that are not loaded. */
8456 if (h->vtable_parent == NULL)
8457 return TRUE;
8458
8459 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8460 || h->root.type == bfd_link_hash_defweak);
8461
8462 sec = h->root.u.def.section;
8463 hstart = h->root.u.def.value;
8464 hend = hstart + h->size;
8465
8466 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8467 if (!relstart)
8468 return *(bfd_boolean *) okp = FALSE;
8469 bed = get_elf_backend_data (sec->owner);
8470 log_file_align = bed->s->log_file_align;
8471
8472 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8473
8474 for (rel = relstart; rel < relend; ++rel)
8475 if (rel->r_offset >= hstart && rel->r_offset < hend)
8476 {
8477 /* If the entry is in use, do nothing. */
8478 if (h->vtable_entries_used
8479 && (rel->r_offset - hstart) < h->vtable_entries_size)
8480 {
8481 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8482 if (h->vtable_entries_used[entry])
8483 continue;
8484 }
8485 /* Otherwise, kill it. */
8486 rel->r_offset = rel->r_info = rel->r_addend = 0;
8487 }
8488
8489 return TRUE;
8490 }
8491
8492 /* Mark sections containing dynamically referenced symbols. This is called
8493 through elf_link_hash_traverse. */
8494
8495 static bfd_boolean
8496 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8497 void *okp ATTRIBUTE_UNUSED)
8498 {
8499 if (h->root.type == bfd_link_hash_warning)
8500 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8501
8502 if ((h->root.type == bfd_link_hash_defined
8503 || h->root.type == bfd_link_hash_defweak)
8504 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC))
8505 h->root.u.def.section->flags |= SEC_KEEP;
8506
8507 return TRUE;
8508 }
8509
8510 /* Do mark and sweep of unused sections. */
8511
8512 bfd_boolean
8513 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8514 {
8515 bfd_boolean ok = TRUE;
8516 bfd *sub;
8517 asection * (*gc_mark_hook)
8518 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8519 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8520
8521 if (!get_elf_backend_data (abfd)->can_gc_sections
8522 || info->relocatable
8523 || info->emitrelocations
8524 || info->shared
8525 || !is_elf_hash_table (info->hash))
8526 {
8527 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8528 return TRUE;
8529 }
8530
8531 /* Apply transitive closure to the vtable entry usage info. */
8532 elf_link_hash_traverse (elf_hash_table (info),
8533 elf_gc_propagate_vtable_entries_used,
8534 &ok);
8535 if (!ok)
8536 return FALSE;
8537
8538 /* Kill the vtable relocations that were not used. */
8539 elf_link_hash_traverse (elf_hash_table (info),
8540 elf_gc_smash_unused_vtentry_relocs,
8541 &ok);
8542 if (!ok)
8543 return FALSE;
8544
8545 /* Mark dynamically referenced symbols. */
8546 if (elf_hash_table (info)->dynamic_sections_created)
8547 elf_link_hash_traverse (elf_hash_table (info),
8548 elf_gc_mark_dynamic_ref_symbol,
8549 &ok);
8550 if (!ok)
8551 return FALSE;
8552
8553 /* Grovel through relocs to find out who stays ... */
8554 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8555 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8556 {
8557 asection *o;
8558
8559 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8560 continue;
8561
8562 for (o = sub->sections; o != NULL; o = o->next)
8563 {
8564 if (o->flags & SEC_KEEP)
8565 {
8566 /* _bfd_elf_discard_section_eh_frame knows how to discard
8567 orphaned FDEs so don't mark sections referenced by the
8568 EH frame section. */
8569 if (strcmp (o->name, ".eh_frame") == 0)
8570 o->gc_mark = 1;
8571 else if (!elf_gc_mark (info, o, gc_mark_hook))
8572 return FALSE;
8573 }
8574 }
8575 }
8576
8577 /* ... and mark SEC_EXCLUDE for those that go. */
8578 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8579 return FALSE;
8580
8581 return TRUE;
8582 }
8583 \f
8584 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8585
8586 bfd_boolean
8587 bfd_elf_gc_record_vtinherit (bfd *abfd,
8588 asection *sec,
8589 struct elf_link_hash_entry *h,
8590 bfd_vma offset)
8591 {
8592 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8593 struct elf_link_hash_entry **search, *child;
8594 bfd_size_type extsymcount;
8595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8596 char *sec_name;
8597
8598 /* The sh_info field of the symtab header tells us where the
8599 external symbols start. We don't care about the local symbols at
8600 this point. */
8601 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8602 if (!elf_bad_symtab (abfd))
8603 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8604
8605 sym_hashes = elf_sym_hashes (abfd);
8606 sym_hashes_end = sym_hashes + extsymcount;
8607
8608 /* Hunt down the child symbol, which is in this section at the same
8609 offset as the relocation. */
8610 for (search = sym_hashes; search != sym_hashes_end; ++search)
8611 {
8612 if ((child = *search) != NULL
8613 && (child->root.type == bfd_link_hash_defined
8614 || child->root.type == bfd_link_hash_defweak)
8615 && child->root.u.def.section == sec
8616 && child->root.u.def.value == offset)
8617 goto win;
8618 }
8619
8620 sec_name = bfd_get_section_ident (sec);
8621 (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
8622 bfd_archive_filename (abfd),
8623 sec_name ? sec_name : sec->name,
8624 (unsigned long) offset);
8625 bfd_set_error (bfd_error_invalid_operation);
8626 return FALSE;
8627
8628 win:
8629 if (!h)
8630 {
8631 /* This *should* only be the absolute section. It could potentially
8632 be that someone has defined a non-global vtable though, which
8633 would be bad. It isn't worth paging in the local symbols to be
8634 sure though; that case should simply be handled by the assembler. */
8635
8636 child->vtable_parent = (struct elf_link_hash_entry *) -1;
8637 }
8638 else
8639 child->vtable_parent = h;
8640
8641 return TRUE;
8642 }
8643
8644 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8645
8646 bfd_boolean
8647 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8648 asection *sec ATTRIBUTE_UNUSED,
8649 struct elf_link_hash_entry *h,
8650 bfd_vma addend)
8651 {
8652 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8653 unsigned int log_file_align = bed->s->log_file_align;
8654
8655 if (addend >= h->vtable_entries_size)
8656 {
8657 size_t size, bytes, file_align;
8658 bfd_boolean *ptr = h->vtable_entries_used;
8659
8660 /* While the symbol is undefined, we have to be prepared to handle
8661 a zero size. */
8662 file_align = 1 << log_file_align;
8663 if (h->root.type == bfd_link_hash_undefined)
8664 size = addend + file_align;
8665 else
8666 {
8667 size = h->size;
8668 if (addend >= size)
8669 {
8670 /* Oops! We've got a reference past the defined end of
8671 the table. This is probably a bug -- shall we warn? */
8672 size = addend + file_align;
8673 }
8674 }
8675 size = (size + file_align - 1) & -file_align;
8676
8677 /* Allocate one extra entry for use as a "done" flag for the
8678 consolidation pass. */
8679 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
8680
8681 if (ptr)
8682 {
8683 ptr = bfd_realloc (ptr - 1, bytes);
8684
8685 if (ptr != NULL)
8686 {
8687 size_t oldbytes;
8688
8689 oldbytes = (((h->vtable_entries_size >> log_file_align) + 1)
8690 * sizeof (bfd_boolean));
8691 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
8692 }
8693 }
8694 else
8695 ptr = bfd_zmalloc (bytes);
8696
8697 if (ptr == NULL)
8698 return FALSE;
8699
8700 /* And arrange for that done flag to be at index -1. */
8701 h->vtable_entries_used = ptr + 1;
8702 h->vtable_entries_size = size;
8703 }
8704
8705 h->vtable_entries_used[addend >> log_file_align] = TRUE;
8706
8707 return TRUE;
8708 }
8709
8710 struct alloc_got_off_arg {
8711 bfd_vma gotoff;
8712 unsigned int got_elt_size;
8713 };
8714
8715 /* We need a special top-level link routine to convert got reference counts
8716 to real got offsets. */
8717
8718 static bfd_boolean
8719 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
8720 {
8721 struct alloc_got_off_arg *gofarg = arg;
8722
8723 if (h->root.type == bfd_link_hash_warning)
8724 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8725
8726 if (h->got.refcount > 0)
8727 {
8728 h->got.offset = gofarg->gotoff;
8729 gofarg->gotoff += gofarg->got_elt_size;
8730 }
8731 else
8732 h->got.offset = (bfd_vma) -1;
8733
8734 return TRUE;
8735 }
8736
8737 /* And an accompanying bit to work out final got entry offsets once
8738 we're done. Should be called from final_link. */
8739
8740 bfd_boolean
8741 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
8742 struct bfd_link_info *info)
8743 {
8744 bfd *i;
8745 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8746 bfd_vma gotoff;
8747 unsigned int got_elt_size = bed->s->arch_size / 8;
8748 struct alloc_got_off_arg gofarg;
8749
8750 if (! is_elf_hash_table (info->hash))
8751 return FALSE;
8752
8753 /* The GOT offset is relative to the .got section, but the GOT header is
8754 put into the .got.plt section, if the backend uses it. */
8755 if (bed->want_got_plt)
8756 gotoff = 0;
8757 else
8758 gotoff = bed->got_header_size;
8759
8760 /* Do the local .got entries first. */
8761 for (i = info->input_bfds; i; i = i->link_next)
8762 {
8763 bfd_signed_vma *local_got;
8764 bfd_size_type j, locsymcount;
8765 Elf_Internal_Shdr *symtab_hdr;
8766
8767 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
8768 continue;
8769
8770 local_got = elf_local_got_refcounts (i);
8771 if (!local_got)
8772 continue;
8773
8774 symtab_hdr = &elf_tdata (i)->symtab_hdr;
8775 if (elf_bad_symtab (i))
8776 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8777 else
8778 locsymcount = symtab_hdr->sh_info;
8779
8780 for (j = 0; j < locsymcount; ++j)
8781 {
8782 if (local_got[j] > 0)
8783 {
8784 local_got[j] = gotoff;
8785 gotoff += got_elt_size;
8786 }
8787 else
8788 local_got[j] = (bfd_vma) -1;
8789 }
8790 }
8791
8792 /* Then the global .got entries. .plt refcounts are handled by
8793 adjust_dynamic_symbol */
8794 gofarg.gotoff = gotoff;
8795 gofarg.got_elt_size = got_elt_size;
8796 elf_link_hash_traverse (elf_hash_table (info),
8797 elf_gc_allocate_got_offsets,
8798 &gofarg);
8799 return TRUE;
8800 }
8801
8802 /* Many folk need no more in the way of final link than this, once
8803 got entry reference counting is enabled. */
8804
8805 bfd_boolean
8806 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
8807 {
8808 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
8809 return FALSE;
8810
8811 /* Invoke the regular ELF backend linker to do all the work. */
8812 return bfd_elf_final_link (abfd, info);
8813 }
8814
8815 bfd_boolean
8816 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
8817 {
8818 struct elf_reloc_cookie *rcookie = cookie;
8819
8820 if (rcookie->bad_symtab)
8821 rcookie->rel = rcookie->rels;
8822
8823 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
8824 {
8825 unsigned long r_symndx;
8826
8827 if (! rcookie->bad_symtab)
8828 if (rcookie->rel->r_offset > offset)
8829 return FALSE;
8830 if (rcookie->rel->r_offset != offset)
8831 continue;
8832
8833 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
8834 if (r_symndx == SHN_UNDEF)
8835 return TRUE;
8836
8837 if (r_symndx >= rcookie->locsymcount
8838 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
8839 {
8840 struct elf_link_hash_entry *h;
8841
8842 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
8843
8844 while (h->root.type == bfd_link_hash_indirect
8845 || h->root.type == bfd_link_hash_warning)
8846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8847
8848 if ((h->root.type == bfd_link_hash_defined
8849 || h->root.type == bfd_link_hash_defweak)
8850 && elf_discarded_section (h->root.u.def.section))
8851 return TRUE;
8852 else
8853 return FALSE;
8854 }
8855 else
8856 {
8857 /* It's not a relocation against a global symbol,
8858 but it could be a relocation against a local
8859 symbol for a discarded section. */
8860 asection *isec;
8861 Elf_Internal_Sym *isym;
8862
8863 /* Need to: get the symbol; get the section. */
8864 isym = &rcookie->locsyms[r_symndx];
8865 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
8866 {
8867 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
8868 if (isec != NULL && elf_discarded_section (isec))
8869 return TRUE;
8870 }
8871 }
8872 return FALSE;
8873 }
8874 return FALSE;
8875 }
8876
8877 /* Discard unneeded references to discarded sections.
8878 Returns TRUE if any section's size was changed. */
8879 /* This function assumes that the relocations are in sorted order,
8880 which is true for all known assemblers. */
8881
8882 bfd_boolean
8883 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
8884 {
8885 struct elf_reloc_cookie cookie;
8886 asection *stab, *eh;
8887 Elf_Internal_Shdr *symtab_hdr;
8888 const struct elf_backend_data *bed;
8889 bfd *abfd;
8890 unsigned int count;
8891 bfd_boolean ret = FALSE;
8892
8893 if (info->traditional_format
8894 || !is_elf_hash_table (info->hash))
8895 return FALSE;
8896
8897 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
8898 {
8899 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
8900 continue;
8901
8902 bed = get_elf_backend_data (abfd);
8903
8904 if ((abfd->flags & DYNAMIC) != 0)
8905 continue;
8906
8907 eh = bfd_get_section_by_name (abfd, ".eh_frame");
8908 if (info->relocatable
8909 || (eh != NULL
8910 && (eh->size == 0
8911 || bfd_is_abs_section (eh->output_section))))
8912 eh = NULL;
8913
8914 stab = bfd_get_section_by_name (abfd, ".stab");
8915 if (stab != NULL
8916 && (stab->size == 0
8917 || bfd_is_abs_section (stab->output_section)
8918 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
8919 stab = NULL;
8920
8921 if (stab == NULL
8922 && eh == NULL
8923 && bed->elf_backend_discard_info == NULL)
8924 continue;
8925
8926 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8927 cookie.abfd = abfd;
8928 cookie.sym_hashes = elf_sym_hashes (abfd);
8929 cookie.bad_symtab = elf_bad_symtab (abfd);
8930 if (cookie.bad_symtab)
8931 {
8932 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
8933 cookie.extsymoff = 0;
8934 }
8935 else
8936 {
8937 cookie.locsymcount = symtab_hdr->sh_info;
8938 cookie.extsymoff = symtab_hdr->sh_info;
8939 }
8940
8941 if (bed->s->arch_size == 32)
8942 cookie.r_sym_shift = 8;
8943 else
8944 cookie.r_sym_shift = 32;
8945
8946 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
8947 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
8948 {
8949 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8950 cookie.locsymcount, 0,
8951 NULL, NULL, NULL);
8952 if (cookie.locsyms == NULL)
8953 return FALSE;
8954 }
8955
8956 if (stab != NULL)
8957 {
8958 cookie.rels = NULL;
8959 count = stab->reloc_count;
8960 if (count != 0)
8961 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
8962 info->keep_memory);
8963 if (cookie.rels != NULL)
8964 {
8965 cookie.rel = cookie.rels;
8966 cookie.relend = cookie.rels;
8967 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8968 if (_bfd_discard_section_stabs (abfd, stab,
8969 elf_section_data (stab)->sec_info,
8970 bfd_elf_reloc_symbol_deleted_p,
8971 &cookie))
8972 ret = TRUE;
8973 if (elf_section_data (stab)->relocs != cookie.rels)
8974 free (cookie.rels);
8975 }
8976 }
8977
8978 if (eh != NULL)
8979 {
8980 cookie.rels = NULL;
8981 count = eh->reloc_count;
8982 if (count != 0)
8983 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
8984 info->keep_memory);
8985 cookie.rel = cookie.rels;
8986 cookie.relend = cookie.rels;
8987 if (cookie.rels != NULL)
8988 cookie.relend += count * bed->s->int_rels_per_ext_rel;
8989
8990 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
8991 bfd_elf_reloc_symbol_deleted_p,
8992 &cookie))
8993 ret = TRUE;
8994
8995 if (cookie.rels != NULL
8996 && elf_section_data (eh)->relocs != cookie.rels)
8997 free (cookie.rels);
8998 }
8999
9000 if (bed->elf_backend_discard_info != NULL
9001 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9002 ret = TRUE;
9003
9004 if (cookie.locsyms != NULL
9005 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9006 {
9007 if (! info->keep_memory)
9008 free (cookie.locsyms);
9009 else
9010 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9011 }
9012 }
9013
9014 if (info->eh_frame_hdr
9015 && !info->relocatable
9016 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9017 ret = TRUE;
9018
9019 return ret;
9020 }
This page took 0.30774 seconds and 4 git commands to generate.