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