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