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