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