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