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