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