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