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