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