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