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