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