Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
250d07de 2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
4b69ce9b
AM
35#ifdef HAVE_LIMITS_H
36#include <limits.h>
37#endif
38#ifndef CHAR_BIT
39#define CHAR_BIT 8
40#endif
41
28caa186
AM
42/* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45struct elf_info_failed
46{
47 struct bfd_link_info *info;
28caa186
AM
48 bfd_boolean failed;
49};
50
51/* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54struct elf_find_verdep_info
55{
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62};
63
64static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
2f0c68f2
CM
67asection *
68_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71{
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
07d6d2b8 86 return h->root.u.def.section;
2f0c68f2
CM
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106}
107
d98685ac
AM
108/* Define a symbol in a dynamic linkage section. */
109
110struct elf_link_hash_entry *
111_bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115{
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
ccabcbe5 118 const struct elf_backend_data *bed;
d98685ac
AM
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
ad32986f 128 bh = &h->root;
d98685ac 129 }
ad32986f
NC
130 else
131 bh = NULL;
d98685ac 132
cf18fda4 133 bed = get_elf_backend_data (abfd);
d98685ac 134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 135 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
ad32986f 139 BFD_ASSERT (h != NULL);
d98685ac 140 h->def_regular = 1;
e28df02b 141 h->non_elf = 0;
12b2843a 142 h->root.linker_def = 1;
d98685ac 143 h->type = STT_OBJECT;
00b7642b
AM
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 146
ccabcbe5 147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
148 return h;
149}
150
b34976b6 151bfd_boolean
268b6b39 152_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
153{
154 flagword flags;
aad5d350 155 asection *s;
252b5132 156 struct elf_link_hash_entry *h;
9c5bfbb7 157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 158 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
159
160 /* This function may be called more than once. */
ce558b89 161 if (htab->sgot != NULL)
b34976b6 162 return TRUE;
252b5132 163
e5a52504 164 flags = bed->dynamic_sec_flags;
252b5132 165
14b2f831
AM
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
6de2ae4a 171 if (s == NULL
fd361982 172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
6de2ae4a
L
173 return FALSE;
174 htab->srelgot = s;
252b5132 175
14b2f831 176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 177 if (s == NULL
fd361982 178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
64e77c6d
L
179 return FALSE;
180 htab->sgot = s;
181
252b5132
RH
182 if (bed->want_got_plt)
183 {
14b2f831 184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 185 if (s == NULL
fd361982 186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 187 return FALSE;
6de2ae4a 188 htab->sgotplt = s;
252b5132
RH
189 }
190
64e77c6d
L
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
2517a57f
AM
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
6de2ae4a
L
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
2517a57f 202 elf_hash_table (info)->hgot = h;
d98685ac
AM
203 if (h == NULL)
204 return FALSE;
2517a57f 205 }
252b5132 206
b34976b6 207 return TRUE;
252b5132
RH
208}
209\f
7e9f0867
AM
210/* Create a strtab to hold the dynamic symbol names. */
211static bfd_boolean
212_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213{
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
6cd255ca
L
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
57963c05 226 asection *s;
6cd255ca 227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 228 if ((ibfd->flags
57963c05
AM
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
7e9f0867
AM
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249}
250
45d6a902
AM
251/* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
252b5132 257
b34976b6 258bfd_boolean
268b6b39 259_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 260{
45d6a902 261 flagword flags;
91d6fa6a 262 asection *s;
9c5bfbb7 263 const struct elf_backend_data *bed;
9637f6ef 264 struct elf_link_hash_entry *h;
252b5132 265
0eddce27 266 if (! is_elf_hash_table (info->hash))
45d6a902
AM
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
7e9f0867
AM
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
45d6a902 274
7e9f0867 275 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
45d6a902
AM
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
9b8b325a 282 if (bfd_link_executable (info) && !info->nointerp)
252b5132 283 {
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288 }
bb0deeff 289
45d6a902
AM
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
fd361982 295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
fd361982 301 || !bfd_set_section_alignment (s, 1))
45d6a902
AM
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
fd361982 307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
308 return FALSE;
309
14b2f831
AM
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
45d6a902 312 if (s == NULL
fd361982 313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902 314 return FALSE;
cae1fbbb 315 elf_hash_table (info)->dynsym = s;
45d6a902 316
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
3496cb2a 319 if (s == NULL)
45d6a902
AM
320 return FALSE;
321
14b2f831 322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 323 if (s == NULL
fd361982 324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
9637f6ef
L
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
45d6a902
AM
336 return FALSE;
337
fdc90cb4
JJ
338 if (info->emit_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
fdc90cb4 342 if (s == NULL
fd361982 343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
f16a9783 348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 349 {
14b2f831
AM
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
fdc90cb4 352 if (s == NULL
fd361982 353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
45d6a902
AM
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
894891db
NC
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374}
375
376/* Create dynamic sections when linking against a dynamic object. */
377
378bfd_boolean
268b6b39 379_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
380{
381 flagword flags, pltflags;
7325306f 382 struct elf_link_hash_entry *h;
45d6a902 383 asection *s;
9c5bfbb7 384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 385 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 386
252b5132
RH
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
e5a52504 389 flags = bed->dynamic_sec_flags;
252b5132
RH
390
391 pltflags = flags;
252b5132 392 if (bed->plt_not_loaded)
6df4d94c
MM
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
5d1634d7 396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
14b2f831 402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 403 if (s == NULL
fd361982 404 || !bfd_set_section_alignment (s, bed->plt_alignment))
b34976b6 405 return FALSE;
6de2ae4a 406 htab->splt = s;
252b5132 407
d98685ac
AM
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
7325306f
RS
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
252b5132 418
14b2f831
AM
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
252b5132 423 if (s == NULL
fd361982 424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 425 return FALSE;
6de2ae4a 426 htab->srelplt = s;
252b5132
RH
427
428 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 429 return FALSE;
252b5132 430
3018b441
RH
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
14b2f831 439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 440 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 441 if (s == NULL)
b34976b6 442 return FALSE;
9d19e4fd 443 htab->sdynbss = s;
252b5132 444
5474d94f
AM
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
5474d94f 450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 451 flags);
5474d94f
AM
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
3018b441 457 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
9d19e4fd 468 if (bfd_link_executable (info))
3018b441 469 {
14b2f831
AM
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
3018b441 474 if (s == NULL
fd361982 475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 476 return FALSE;
9d19e4fd 477 htab->srelbss = s;
5474d94f
AM
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
fd361982 486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
5474d94f
AM
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
3018b441 490 }
252b5132
RH
491 }
492
b34976b6 493 return TRUE;
252b5132
RH
494}
495\f
252b5132
RH
496/* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
b34976b6 504bfd_boolean
c152c796
AM
505bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
252b5132
RH
507{
508 if (h->dynindx == -1)
509 {
2b0f7ef9 510 struct elf_strtab_hash *dynstr;
68b6ddd0 511 char *p;
252b5132 512 const char *name;
ef53be89 513 size_t indx;
252b5132 514
a896df97
AM
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
7a13edea
NC
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
9d6eee78
L
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
38048eb9 535 {
f5385ebf 536 h->forced_local = 1;
c8bad65e
AM
537 if (!elf_hash_table (info)->is_relocatable_executable
538 || ((h->root.type == bfd_link_hash_defined
539 || h->root.type == bfd_link_hash_defweak)
540 && h->root.u.def.section->owner->no_export)
541 || (h->root.type == bfd_link_hash_common
542 && h->root.u.c.p->section->owner->no_export))
67687978 543 return TRUE;
7a13edea 544 }
0444bdd4 545
7a13edea
NC
546 default:
547 break;
548 }
549
252b5132
RH
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 558 if (dynstr == NULL)
b34976b6 559 return FALSE;
252b5132
RH
560 }
561
562 /* We don't put any version information in the dynamic string
aad5d350 563 table. */
252b5132
RH
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
252b5132 578
ef53be89 579 if (indx == (size_t) -1)
b34976b6 580 return FALSE;
252b5132
RH
581 h->dynstr_index = indx;
582 }
583
b34976b6 584 return TRUE;
252b5132 585}
45d6a902 586\f
55255dae
L
587/* Mark a symbol dynamic. */
588
28caa186 589static void
55255dae 590bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
55255dae 593{
40b36307 594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 595
40b36307 596 /* It may be called more than once on the same H. */
0e1862bb 597 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
598 return;
599
40b36307
L
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
b8871f35 602 || h->type == STT_COMMON
40b36307 603 || (sym != NULL
b8871f35
L
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 606 || (d != NULL
73ec947d 607 && h->non_elf
40b36307 608 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
55255dae
L
615}
616
45d6a902
AM
617/* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
620bfd_boolean
fe21a8fc
L
621bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
268b6b39 623 const char *name,
fe21a8fc
L
624 bfd_boolean provide,
625 bfd_boolean hidden)
45d6a902 626{
00cbee0a 627 struct elf_link_hash_entry *h, *hv;
4ea42fb7 628 struct elf_link_hash_table *htab;
00cbee0a 629 const struct elf_backend_data *bed;
45d6a902 630
0eddce27 631 if (!is_elf_hash_table (info->hash))
45d6a902
AM
632 return TRUE;
633
4ea42fb7
AM
634 htab = elf_hash_table (info);
635 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 636 if (h == NULL)
4ea42fb7 637 return provide;
45d6a902 638
8e2a4f11
AM
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
0f550b3d
L
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
73ec947d
AM
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
00cbee0a 663 switch (h->root.type)
77cfaee6 664 {
00cbee0a
L
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
4ea42fb7 674 h->root.type = bfd_link_hash_new;
77cfaee6
AM
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
677 break;
678 case bfd_link_hash_new:
00cbee0a
L
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 682 the versioned symbol point to this one. */
00cbee0a
L
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
8e2a4f11
AM
695 default:
696 BFD_FAIL ();
c2596ca5 697 return FALSE;
55255dae 698 }
45d6a902
AM
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
f5385ebf
AM
705 && h->def_dynamic
706 && !h->def_regular)
45d6a902
AM
707 h->root.type = bfd_link_hash_undefined;
708
48e30f52
L
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
b531344c
MR
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
45d6a902 718
f5385ebf 719 h->def_regular = 1;
45d6a902 720
eb8476a6 721 if (hidden)
fe21a8fc 722 {
91d6fa6a 723 bed = get_elf_backend_data (output_bfd);
b8297068
AM
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
726 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
727 }
728
6fa3860b
PB
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
0e1862bb 731 if (!bfd_link_relocatable (info)
6fa3860b
PB
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
f5385ebf
AM
737 if ((h->def_dynamic
738 || h->ref_dynamic
6b3b0ab8
L
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 741 && !h->forced_local
45d6a902
AM
742 && h->dynindx == -1)
743 {
c152c796 744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
745 return FALSE;
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
60d67dc8 750 if (h->is_weakalias)
45d6a902 751 {
60d67dc8
AM
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
756 return FALSE;
757 }
758 }
759
760 return TRUE;
761}
42751cf3 762
8c58d23b
AM
763/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767int
c152c796
AM
768bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
8c58d23b 771{
986f0783 772 size_t amt;
8c58d23b
AM
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
ef53be89 776 size_t dynstr_index;
8c58d23b
AM
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
0eddce27 781 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
a50b1753 790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 796 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 803 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
b34976b6 830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 831 if (dynstr_index == (size_t) -1)
8c58d23b
AM
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850}
851
30b30c21 852/* Return the dynindex of a local dynamic symbol. */
42751cf3 853
30b30c21 854long
268b6b39
AM
855_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
30b30c21
RH
858{
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865}
866
867/* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
b34976b6 871static bfd_boolean
268b6b39
AM
872elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
42751cf3 874{
a50b1753 875 size_t *count = (size_t *) data;
30b30c21 876
6fa3860b
PB
877 if (h->forced_local)
878 return TRUE;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return TRUE;
884}
885
886
887/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
890static bfd_boolean
891elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893{
a50b1753 894 size_t *count = (size_t *) data;
6fa3860b 895
6fa3860b
PB
896 if (!h->forced_local)
897 return TRUE;
898
42751cf3 899 if (h->dynindx != -1)
30b30c21
RH
900 h->dynindx = ++(*count);
901
b34976b6 902 return TRUE;
42751cf3 903}
30b30c21 904
aee6f5b4
AO
905/* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
907bfd_boolean
d00dd7dc
AM
908_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
aee6f5b4 911{
74541ad4 912 struct elf_link_hash_table *htab;
ca55926c 913 asection *ip;
74541ad4 914
aee6f5b4
AO
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
74541ad4 922 htab = elf_hash_table (info);
74541ad4
AM
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
ca55926c 926 return (htab->dynobj != NULL
3d4d4302 927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 928 && ip->output_section == p);
aee6f5b4
AO
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
933 return TRUE;
934 }
935}
936
d00dd7dc
AM
937bfd_boolean
938_bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942{
943 return TRUE;
944}
945
062e2358 946/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
30b30c21 954
554220db
AM
955static unsigned long
956_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
30b30c21
RH
959{
960 unsigned long dynsymcount = 0;
63f452a8 961 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 962
0e1862bb
L
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 965 {
aee6f5b4 966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 969 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 970 && (p->flags & SEC_ALLOC) != 0
7f923b7f 971 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
74541ad4 979 elf_section_data (p)->dynindx = 0;
30b30c21 980 }
63f452a8
AM
981 if (do_sec)
982 *section_sym_count = dynsymcount;
30b30c21 983
6fa3860b
PB
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
30b30c21
RH
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
90ac2420 994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
d5486c43
L
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
30b30c21 1005
ccabcbe5
AM
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
30b30c21 1008}
252b5132 1009
54ac0771
L
1010/* Merge st_other field. */
1011
1012static void
1013elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
5160d0f3 1014 unsigned int st_other, asection *sec,
cd3416da 1015 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1016{
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
cd3416da 1020 code might be needed here. */
54ac0771 1021 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1023 dynamic);
1024
cd3416da 1025 if (!dynamic)
54ac0771 1026 {
5160d0f3 1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1029
cd3416da
AM
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1034 }
b8417128 1035 else if (definition
5160d0f3 1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 1037 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1038 h->protected_def = 1;
54ac0771
L
1039}
1040
4f3fedcf
AM
1041/* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
45d6a902 1053
8a56bd02 1054static bfd_boolean
268b6b39
AM
1055_bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
4f3fedcf
AM
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
37a9e49a 1063 bfd_boolean *pold_weak,
af44c138 1064 unsigned int *pold_alignment,
268b6b39 1065 bfd_boolean *skip,
7ba11550 1066 bfd **override,
268b6b39 1067 bfd_boolean *type_change_ok,
6e33951e
L
1068 bfd_boolean *size_change_ok,
1069 bfd_boolean *matched)
252b5132 1070{
7479dfd4 1071 asection *sec, *oldsec;
45d6a902 1072 struct elf_link_hash_entry *h;
90c984fc 1073 struct elf_link_hash_entry *hi;
45d6a902
AM
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
1077 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1078 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1079 const struct elf_backend_data *bed;
6e33951e 1080 char *new_version;
93f4de39 1081 bfd_boolean default_sym = *matched;
45d6a902
AM
1082
1083 *skip = FALSE;
7ba11550 1084 *override = NULL;
45d6a902
AM
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
1090 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1091 else
1092 h = ((struct elf_link_hash_entry *)
1093 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1094 if (h == NULL)
1095 return FALSE;
1096 *sym_hash = h;
252b5132 1097
88ba32a0
L
1098 bed = get_elf_backend_data (abfd);
1099
6e33951e 1100 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1101 if (h->versioned != unversioned)
6e33951e 1102 {
422f1182
L
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
6e33951e 1120 }
422f1182
L
1121 else
1122 new_version = NULL;
6e33951e 1123
90c984fc
L
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
45d6a902
AM
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
6e33951e
L
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
1134 *matched = TRUE;
1135 else
1136 {
ae7683d2 1137 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1138 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1139 true if the new symbol is only visible to the symbol with
6e33951e 1140 the same symbol version. */
422f1182
L
1141 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1142 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
1146 *matched = TRUE;
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
422f1182
L
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
6e33951e
L
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
934bce08
AM
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
40b36307 1207 /* We have to check it for every instance since the first few may be
ee659f1f 1208 references and not all compilers emit symbol type for undefined
40b36307
L
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
ee659f1f
AM
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
6e33951e
L
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
ee659f1f
AM
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
45d6a902
AM
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
252b5132 1248 {
f5385ebf 1249 h->non_elf = 0;
45d6a902
AM
1250 return TRUE;
1251 }
252b5132 1252
45d6a902
AM
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
895fa45f 1260 && (newweak || oldweak)
45d6a902 1261 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1262 || !h->def_regular))
45d6a902
AM
1263 return TRUE;
1264
707bba77 1265 olddyn = FALSE;
45d6a902
AM
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1268 else if (oldsec != NULL)
45d6a902 1269 {
707bba77 1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1271 indices used by MIPS ELF. */
707bba77 1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1273 }
252b5132 1274
1a3b5c34
AM
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1277 if (oldbfd != NULL
1278 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1279 && newdyn != olddyn)
1280 {
1281 h->root.non_ir_ref_dynamic = TRUE;
1282 hi->root.non_ir_ref_dynamic = TRUE;
1283 }
1284
45d6a902
AM
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1287
707bba77 1288 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1289
707bba77
AM
1290 olddef = (h->root.type != bfd_link_hash_undefined
1291 && h->root.type != bfd_link_hash_undefweak
202ac193 1292 && h->root.type != bfd_link_hash_common);
45d6a902 1293
0a36a439
L
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1296
1297 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1298 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1299
1300 oldfunc = (h->type != STT_NOTYPE
1301 && bed->is_function_type (h->type));
1302
c5d37467 1303 if (!(newfunc && oldfunc)
5b677558
AM
1304 && ELF_ST_TYPE (sym->st_info) != h->type
1305 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1306 && h->type != STT_NOTYPE
c5d37467
AM
1307 && (newdef || bfd_is_com_section (sec))
1308 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1309 {
c5d37467
AM
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1315 if (newdyn
1316 && !olddyn)
1317 {
1318 *skip = TRUE;
1319 return TRUE;
1320 }
1321
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1324 dynamic state. */
1325 if (hi != h
1326 && !newdyn
1327 && olddyn)
1328 {
1329 h = hi;
1330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 h->def_dynamic = 0;
1334 h->dynamic_def = 0;
1335 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1336 {
1337 h->root.type = bfd_link_hash_undefined;
1338 h->root.u.undef.abfd = abfd;
1339 }
1340 else
1341 {
1342 h->root.type = bfd_link_hash_new;
1343 h->root.u.undef.abfd = NULL;
1344 }
1345 return TRUE;
1346 }
580a2b6e
L
1347 }
1348
4c34aff8
AM
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1352 if (oldbfd != NULL
1353 && (oldbfd->flags & BFD_PLUGIN) == 0
1354 && (abfd->flags & BFD_PLUGIN) == 0
1355 && ELF_ST_TYPE (sym->st_info) != h->type
1356 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1357 {
1358 bfd *ntbfd, *tbfd;
1359 bfd_boolean ntdef, tdef;
1360 asection *ntsec, *tsec;
1361
1362 if (h->type == STT_TLS)
1363 {
3b36f7e6 1364 ntbfd = abfd;
7479dfd4
L
1365 ntsec = sec;
1366 ntdef = newdef;
1367 tbfd = oldbfd;
1368 tsec = oldsec;
1369 tdef = olddef;
1370 }
1371 else
1372 {
1373 ntbfd = oldbfd;
1374 ntsec = oldsec;
1375 ntdef = olddef;
1376 tbfd = abfd;
1377 tsec = sec;
1378 tdef = newdef;
1379 }
1380
1381 if (tdef && ntdef)
4eca0228 1382 _bfd_error_handler
695344c0 1383 /* xgettext:c-format */
871b3ab2
AM
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1386 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1387 else if (!tdef && !ntdef)
4eca0228 1388 _bfd_error_handler
695344c0 1389 /* xgettext:c-format */
871b3ab2
AM
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
c08bb8dd 1392 h->root.root.string, tbfd, ntbfd);
7479dfd4 1393 else if (tdef)
4eca0228 1394 _bfd_error_handler
695344c0 1395 /* xgettext:c-format */
871b3ab2
AM
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
c08bb8dd 1398 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1399 else
4eca0228 1400 _bfd_error_handler
695344c0 1401 /* xgettext:c-format */
871b3ab2
AM
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1404 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1405
1406 bfd_set_error (bfd_error_bad_value);
1407 return FALSE;
1408 }
1409
45d6a902
AM
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1412 if (newdyn
9c7a29a3 1413 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1414 && !bfd_is_und_section (sec))
1415 {
1416 *skip = TRUE;
1417 /* Make sure this symbol is dynamic. */
f5385ebf 1418 h->ref_dynamic = 1;
90c984fc 1419 hi->ref_dynamic = 1;
45d6a902
AM
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1422
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1425 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1426 else
1427 return TRUE;
1428 }
1429 else if (!newdyn
9c7a29a3 1430 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1431 && h->def_dynamic)
45d6a902
AM
1432 {
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
6c9b78e6 1436 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1437 {
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1442 if (h->ref_regular)
1443 {
6c9b78e6 1444 hi->root.type = h->root.type;
d2dee3b2 1445 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1446 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1447
6c9b78e6 1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1450 {
aed81c4e
MR
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
d2dee3b2
L
1456 }
1457 else
aed81c4e
MR
1458 h->ref_dynamic = 1;
1459
1460 h->def_dynamic = 0;
aed81c4e
MR
1461 /* FIXME: Should we check type and size for protected symbol? */
1462 h->size = 0;
1463 h->type = 0;
1464
6c9b78e6 1465 h = hi;
d2dee3b2
L
1466 }
1467 else
6c9b78e6 1468 h = hi;
d2dee3b2 1469 }
1de1a317 1470
f5eda473
AM
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1479 {
1de1a317 1480 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1481 h->root.u.undef.abfd = abfd;
1482 }
1483 else
1484 {
1485 h->root.type = bfd_link_hash_new;
1486 h->root.u.undef.abfd = NULL;
1487 }
1488
f5eda473 1489 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1490 {
f5eda473
AM
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
1493 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1494 h->forced_local = 0;
1495 h->ref_dynamic = 0;
45d6a902 1496 }
f5eda473
AM
1497 else
1498 h->ref_dynamic = 1;
1499 h->def_dynamic = 0;
45d6a902
AM
1500 /* FIXME: Should we check type and size for protected symbol? */
1501 h->size = 0;
1502 h->type = 0;
1503 return TRUE;
1504 }
14a793b2 1505
15b43f48
AM
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1513
165f707a
AM
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1518
15b43f48
AM
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1521
165f707a 1522 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1523 newweak = FALSE;
15b43f48 1524 if (olddef && newdyn)
0f8a2703
AM
1525 oldweak = FALSE;
1526
d334575b 1527 /* Allow changes between different types of function symbol. */
0a36a439 1528 if (newfunc && oldfunc)
fcb93ecf
PB
1529 *type_change_ok = TRUE;
1530
79349b09
AM
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
252b5132 1534
79349b09
AM
1535 if (oldweak
1536 || newweak
1537 || (newdef
1538 && h->root.type == bfd_link_hash_undefined))
1539 *type_change_ok = TRUE;
1540
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1543
1544 if (*type_change_ok
1545 || h->root.type == bfd_link_hash_undefined)
1546 *size_change_ok = TRUE;
45d6a902 1547
45d6a902
AM
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1559 libraries.
1560
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1563
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1569 harmless. */
1570
1571 if (newdyn
1572 && newdef
79349b09 1573 && !newweak
45d6a902
AM
1574 && (sec->flags & SEC_ALLOC) != 0
1575 && (sec->flags & SEC_LOAD) == 0
1576 && sym->st_size > 0
0a36a439 1577 && !newfunc)
45d6a902
AM
1578 newdyncommon = TRUE;
1579 else
1580 newdyncommon = FALSE;
1581
1582 if (olddyn
1583 && olddef
1584 && h->root.type == bfd_link_hash_defined
f5385ebf 1585 && h->def_dynamic
45d6a902
AM
1586 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1587 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1588 && h->size > 0
0a36a439 1589 && !oldfunc)
45d6a902
AM
1590 olddyncommon = TRUE;
1591 else
1592 olddyncommon = FALSE;
1593
a4d8e49b
L
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
5d13b3b3
AM
1596 if (bed->merge_symbol != NULL)
1597 {
1598 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1599 return FALSE;
1600 sec = *psec;
1601 }
a4d8e49b 1602
a83ef4d1
L
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
93f4de39 1605 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1606 && !default_sym && h->def_regular
1607 && !(oldbfd != NULL
1608 && (oldbfd->flags & BFD_PLUGIN) != 0
1609 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1610 {
1611 /* Handle a multiple definition. */
1612 (*info->callbacks->multiple_definition) (info, &h->root,
1613 abfd, sec, *pvalue);
1614 *skip = TRUE;
1615 return TRUE;
1616 }
1617
45d6a902
AM
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1620 two. */
1621
1622 if (olddyncommon
1623 && newdyncommon
1624 && sym->st_size != h->size)
1625 {
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1631
1a72702b
AM
1632 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1633 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1634 if (sym->st_size > h->size)
1635 h->size = sym->st_size;
252b5132 1636
45d6a902 1637 *size_change_ok = TRUE;
252b5132
RH
1638 }
1639
45d6a902
AM
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1646
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
8170f769 1652 object to override a weak symbol in a shared object. */
45d6a902
AM
1653
1654 if (newdyn
1655 && newdef
77cfaee6 1656 && (olddef
45d6a902 1657 || (h->root.type == bfd_link_hash_common
8170f769 1658 && (newweak || newfunc))))
45d6a902 1659 {
7ba11550 1660 *override = abfd;
45d6a902
AM
1661 newdef = FALSE;
1662 newdyncommon = FALSE;
252b5132 1663
45d6a902
AM
1664 *psec = sec = bfd_und_section_ptr;
1665 *size_change_ok = TRUE;
252b5132 1666
45d6a902
AM
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
252b5132 1672
45d6a902
AM
1673 if (h->root.type == bfd_link_hash_common)
1674 *type_change_ok = TRUE;
1675 }
1676
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1681 right thing. */
45d6a902
AM
1682
1683 if (newdyncommon
1684 && h->root.type == bfd_link_hash_common)
1685 {
7ba11550 1686 *override = oldbfd;
45d6a902
AM
1687 newdef = FALSE;
1688 newdyncommon = FALSE;
1689 *pvalue = sym->st_size;
a4d8e49b 1690 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1691 *size_change_ok = TRUE;
1692 }
1693
c5e2cead 1694 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1695 if (newdef && olddef && newweak)
54ac0771 1696 {
35ed3f94 1697 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1698 if (!(oldbfd != NULL
1699 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1700 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1701 {
1702 newdef = FALSE;
1703 *skip = TRUE;
1704 }
54ac0771
L
1705
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1708 local symbol. */
5160d0f3 1709 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
54ac0771
L
1710 if (h->dynindx != -1)
1711 switch (ELF_ST_VISIBILITY (h->other))
1712 {
1713 case STV_INTERNAL:
1714 case STV_HIDDEN:
1715 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1716 break;
1717 }
1718 }
c5e2cead 1719
45d6a902
AM
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1725
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
0f8a2703 1728 symbol is a function or is weak. */
45d6a902
AM
1729
1730 flip = NULL;
77cfaee6 1731 if (!newdyn
45d6a902
AM
1732 && (newdef
1733 || (bfd_is_com_section (sec)
0a36a439 1734 && (oldweak || oldfunc)))
45d6a902
AM
1735 && olddyn
1736 && olddef
f5385ebf 1737 && h->def_dynamic)
45d6a902
AM
1738 {
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1741 new definition. */
1742
1743 h->root.type = bfd_link_hash_undefined;
1744 h->root.u.undef.abfd = h->root.u.def.section->owner;
1745 *size_change_ok = TRUE;
1746
1747 olddef = FALSE;
1748 olddyncommon = FALSE;
1749
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1752
1753 if (bfd_is_com_section (sec))
0a36a439
L
1754 {
1755 if (oldfunc)
1756 {
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1759 function. */
1760 h->def_dynamic = 0;
1761 h->type = STT_NOTYPE;
1762 }
1763 *type_change_ok = TRUE;
1764 }
45d6a902 1765
6c9b78e6
AM
1766 if (hi->root.type == bfd_link_hash_indirect)
1767 flip = hi;
45d6a902
AM
1768 else
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h->verinfo.vertree = NULL;
1773 }
1774
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1780
1781 if (! newdyn
1782 && bfd_is_com_section (sec)
1783 && olddyncommon)
1784 {
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1a72702b
AM
1788 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1789 bfd_link_hash_common, sym->st_size);
45d6a902 1790
4cc11e76 1791 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1792 larger, pretend that the new symbol has its size. */
1793
1794 if (h->size > *pvalue)
1795 *pvalue = h->size;
1796
af44c138
L
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment);
1800 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1801
1802 olddef = FALSE;
1803 olddyncommon = FALSE;
1804
1805 h->root.type = bfd_link_hash_undefined;
1806 h->root.u.undef.abfd = h->root.u.def.section->owner;
1807
1808 *size_change_ok = TRUE;
1809 *type_change_ok = TRUE;
1810
6c9b78e6
AM
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 flip = hi;
45d6a902
AM
1813 else
1814 h->verinfo.vertree = NULL;
1815 }
1816
1817 if (flip != NULL)
1818 {
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
45d6a902 1822 flip->root.type = h->root.type;
00cbee0a 1823 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1826 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1827 if (h->def_dynamic)
45d6a902 1828 {
f5385ebf
AM
1829 h->def_dynamic = 0;
1830 flip->ref_dynamic = 1;
45d6a902
AM
1831 }
1832 }
1833
45d6a902
AM
1834 return TRUE;
1835}
1836
1837/* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
4f3fedcf 1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1840 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1841
28caa186 1842static bfd_boolean
268b6b39
AM
1843_bfd_elf_add_default_symbol (bfd *abfd,
1844 struct bfd_link_info *info,
1845 struct elf_link_hash_entry *h,
1846 const char *name,
1847 Elf_Internal_Sym *sym,
4f3fedcf
AM
1848 asection *sec,
1849 bfd_vma value,
1850 bfd **poldbfd,
e3c9d234 1851 bfd_boolean *dynsym)
45d6a902
AM
1852{
1853 bfd_boolean type_change_ok;
1854 bfd_boolean size_change_ok;
1855 bfd_boolean skip;
1856 char *shortname;
1857 struct elf_link_hash_entry *hi;
1858 struct bfd_link_hash_entry *bh;
9c5bfbb7 1859 const struct elf_backend_data *bed;
45d6a902
AM
1860 bfd_boolean collect;
1861 bfd_boolean dynamic;
7ba11550 1862 bfd *override;
45d6a902
AM
1863 char *p;
1864 size_t len, shortlen;
ffd65175 1865 asection *tmp_sec;
6e33951e 1866 bfd_boolean matched;
45d6a902 1867
422f1182
L
1868 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1869 return TRUE;
1870
45d6a902
AM
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p = strchr (name, ELF_VER_CHR);
422f1182
L
1876 if (h->versioned == unknown)
1877 {
1878 if (p == NULL)
1879 {
1880 h->versioned = unversioned;
1881 return TRUE;
1882 }
1883 else
1884 {
1885 if (p[1] != ELF_VER_CHR)
1886 {
1887 h->versioned = versioned_hidden;
1888 return TRUE;
1889 }
1890 else
1891 h->versioned = versioned;
1892 }
1893 }
4373f8af
L
1894 else
1895 {
1896 /* PR ld/19073: We may see an unversioned definition after the
1897 default version. */
1898 if (p == NULL)
1899 return TRUE;
1900 }
45d6a902 1901
45d6a902
AM
1902 bed = get_elf_backend_data (abfd);
1903 collect = bed->collect;
1904 dynamic = (abfd->flags & DYNAMIC) != 0;
1905
1906 shortlen = p - name;
a50b1753 1907 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1908 if (shortname == NULL)
1909 return FALSE;
1910 memcpy (shortname, name, shortlen);
1911 shortname[shortlen] = '\0';
1912
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
1917 type_change_ok = FALSE;
1918 size_change_ok = FALSE;
6e33951e 1919 matched = TRUE;
ffd65175
AM
1920 tmp_sec = sec;
1921 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1922 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1923 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1924 return FALSE;
1925
1926 if (skip)
1927 goto nondefault;
1928
5fa370e4 1929 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1930 {
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1937 {
1938 bfd_boolean hide;
1939
1940 hi->verinfo.vertree
1941 = bfd_find_version_for_sym (info->version_info,
1942 hi->root.root.string, &hide);
1943 if (hi->verinfo.vertree != NULL && hide)
1944 {
1945 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1946 goto nondefault;
1947 }
1948 }
1949 if (hi->verinfo.vertree != NULL
1950 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1951 goto nondefault;
1952 }
1953
45d6a902
AM
1954 if (! override)
1955 {
c6e8a9a8 1956 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1957 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1958 {
1959 bh = &hi->root;
fbcc8baf 1960 if (bh->type == bfd_link_hash_defined
6cc71b82 1961 && bh->u.def.section->owner != NULL
fbcc8baf
L
1962 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1963 {
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1966 it. */
1967 bh->type = bfd_link_hash_undefined;
1968 bh->u.undef.abfd = bh->u.def.section->owner;
1969 }
c6e8a9a8
L
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info, abfd, shortname, BSF_INDIRECT,
1972 bfd_ind_section_ptr,
1973 0, name, FALSE, collect, &bh)))
1974 return FALSE;
1975 hi = (struct elf_link_hash_entry *) bh;
1976 }
45d6a902
AM
1977 }
1978 else
1979 {
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1985
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1989
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1997
1998 while (hi->root.type == bfd_link_hash_indirect
1999 || hi->root.type == bfd_link_hash_warning)
2000 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2001
2002 h->root.type = bfd_link_hash_indirect;
2003 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 2004 if (h->def_dynamic)
45d6a902 2005 {
f5385ebf
AM
2006 h->def_dynamic = 0;
2007 hi->ref_dynamic = 1;
2008 if (hi->ref_regular
2009 || hi->def_regular)
45d6a902 2010 {
c152c796 2011 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2012 return FALSE;
2013 }
2014 }
2015
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2018 hi = h;
2019 }
2020
fab4a87f
L
2021 /* Check if HI is a warning symbol. */
2022 if (hi->root.type == bfd_link_hash_warning)
2023 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2024
45d6a902
AM
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2028
2029 if (hi->root.type == bfd_link_hash_indirect)
2030 {
2031 struct elf_link_hash_entry *ht;
2032
45d6a902 2033 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2034 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2035
5160d0f3
AM
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
2038 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2039
68c88cd4
AM
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2044 hi->dynamic_def |= ht->dynamic_def;
2045
45d6a902
AM
2046 /* See if the new flags lead us to realize that the symbol must
2047 be dynamic. */
2048 if (! *dynsym)
2049 {
2050 if (! dynamic)
2051 {
0e1862bb 2052 if (! bfd_link_executable (info)
90c984fc 2053 || hi->def_dynamic
f5385ebf 2054 || hi->ref_dynamic)
45d6a902
AM
2055 *dynsym = TRUE;
2056 }
2057 else
2058 {
f5385ebf 2059 if (hi->ref_regular)
45d6a902
AM
2060 *dynsym = TRUE;
2061 }
2062 }
2063 }
2064
2065 /* We also need to define an indirection from the nondefault version
2066 of the symbol. */
2067
dc1e8a47 2068 nondefault:
45d6a902 2069 len = strlen (name);
a50b1753 2070 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2071 if (shortname == NULL)
2072 return FALSE;
2073 memcpy (shortname, name, shortlen);
2074 memcpy (shortname + shortlen, p + 1, len - shortlen);
2075
2076 /* Once again, merge with any existing symbol. */
2077 type_change_ok = FALSE;
2078 size_change_ok = FALSE;
ffd65175
AM
2079 tmp_sec = sec;
2080 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2081 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2082 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2083 return FALSE;
2084
2085 if (skip)
726d7d1e
AM
2086 {
2087 if (!dynamic
2088 && h->root.type == bfd_link_hash_defweak
2089 && hi->root.type == bfd_link_hash_defined)
2090 {
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h->root.type = bfd_link_hash_defined;
2097 h->root.u.def.section = hi->root.u.def.section;
2098 h->root.u.def.value = hi->root.u.def.value;
2099 hi->root.type = bfd_link_hash_indirect;
2100 hi->root.u.i.link = &h->root;
2101 }
2102 else
2103 return TRUE;
2104 }
2105 else if (override)
45d6a902
AM
2106 {
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
4cc11e76 2109 overridden by a versioned definition. */
45d6a902
AM
2110 if (hi->root.type != bfd_link_hash_defined
2111 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2112 _bfd_error_handler
695344c0 2113 /* xgettext:c-format */
871b3ab2 2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2115 abfd, shortname);
726d7d1e 2116 return TRUE;
45d6a902
AM
2117 }
2118 else
2119 {
2120 bh = &hi->root;
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2123 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2124 return FALSE;
2125 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2126 }
45d6a902 2127
726d7d1e
AM
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi->root.type == bfd_link_hash_indirect)
2132 {
2133 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2134 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2135 hi->dynamic_def |= h->dynamic_def;
45d6a902 2136
726d7d1e
AM
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2139 @@VER symbol. */
2140 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2141
2142 /* See if the new flags lead us to realize that the symbol
2143 must be dynamic. */
2144 if (! *dynsym)
45d6a902 2145 {
726d7d1e 2146 if (! dynamic)
45d6a902 2147 {
726d7d1e
AM
2148 if (! bfd_link_executable (info)
2149 || hi->ref_dynamic)
2150 *dynsym = TRUE;
2151 }
2152 else
2153 {
2154 if (hi->ref_regular)
2155 *dynsym = TRUE;
45d6a902
AM
2156 }
2157 }
2158 }
2159
2160 return TRUE;
2161}
2162\f
2163/* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2165
28caa186 2166static bfd_boolean
268b6b39 2167_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2168{
a50b1753 2169 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2170
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h->root.type == bfd_link_hash_indirect)
2173 return TRUE;
2174
7686d77d
AM
2175 /* Ignore this if we won't export it. */
2176 if (!eif->info->export_dynamic && !h->dynamic)
2177 return TRUE;
45d6a902
AM
2178
2179 if (h->dynindx == -1
fd91d419
L
2180 && (h->def_regular || h->ref_regular)
2181 && ! bfd_hide_sym_by_version (eif->info->version_info,
2182 h->root.root.string))
45d6a902 2183 {
fd91d419 2184 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2185 {
fd91d419
L
2186 eif->failed = TRUE;
2187 return FALSE;
45d6a902
AM
2188 }
2189 }
2190
2191 return TRUE;
2192}
2193\f
2194/* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2198
28caa186 2199static bfd_boolean
268b6b39
AM
2200_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2201 void *data)
45d6a902 2202{
a50b1753 2203 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2204 Elf_Internal_Verneed *t;
2205 Elf_Internal_Vernaux *a;
986f0783 2206 size_t amt;
45d6a902 2207
45d6a902
AM
2208 /* We only care about symbols defined in shared objects with version
2209 information. */
f5385ebf
AM
2210 if (!h->def_dynamic
2211 || h->def_regular
45d6a902 2212 || h->dynindx == -1
7b20f099
AM
2213 || h->verinfo.verdef == NULL
2214 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2215 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2216 return TRUE;
2217
2218 /* See if we already know about this version. */
28caa186
AM
2219 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2220 t != NULL;
2221 t = t->vn_nextref)
45d6a902
AM
2222 {
2223 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2224 continue;
2225
2226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2227 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2228 return TRUE;
2229
2230 break;
2231 }
2232
2233 /* This is a new version. Add it to tree we are building. */
2234
2235 if (t == NULL)
2236 {
2237 amt = sizeof *t;
a50b1753 2238 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2239 if (t == NULL)
2240 {
2241 rinfo->failed = TRUE;
2242 return FALSE;
2243 }
2244
2245 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2246 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2247 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2248 }
2249
2250 amt = sizeof *a;
a50b1753 2251 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2252 if (a == NULL)
2253 {
2254 rinfo->failed = TRUE;
2255 return FALSE;
2256 }
45d6a902
AM
2257
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2261 fixed. */
2262 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2263
2264 a->vna_flags = h->verinfo.verdef->vd_flags;
2265 a->vna_nextptr = t->vn_auxptr;
2266
2267 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2268 ++rinfo->vers;
2269
2270 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2271
2272 t->vn_auxptr = a;
2273
2274 return TRUE;
2275}
2276
099bb8fb
L
2277/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2279
2280static bfd_boolean
2281_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2282 struct elf_link_hash_entry *h,
2283 const char *version_p,
2284 struct bfd_elf_version_tree **t_p,
2285 bfd_boolean *hide)
2286{
2287 struct bfd_elf_version_tree *t;
2288
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t = info->version_info; t != NULL; t = t->next)
2291 {
2292 if (strcmp (t->name, version_p) == 0)
2293 {
2294 size_t len;
2295 char *alc;
2296 struct bfd_elf_version_expr *d;
2297
2298 len = version_p - h->root.root.string;
2299 alc = (char *) bfd_malloc (len);
2300 if (alc == NULL)
2301 return FALSE;
2302 memcpy (alc, h->root.root.string, len - 1);
2303 alc[len - 1] = '\0';
2304 if (alc[len - 2] == ELF_VER_CHR)
2305 alc[len - 2] = '\0';
2306
2307 h->verinfo.vertree = t;
2308 t->used = TRUE;
2309 d = NULL;
2310
2311 if (t->globals.list != NULL)
2312 d = (*t->match) (&t->globals, NULL, alc);
2313
2314 /* See if there is anything to force this symbol to
2315 local scope. */
2316 if (d == NULL && t->locals.list != NULL)
2317 {
2318 d = (*t->match) (&t->locals, NULL, alc);
2319 if (d != NULL
2320 && h->dynindx != -1
2321 && ! info->export_dynamic)
2322 *hide = TRUE;
2323 }
2324
2325 free (alc);
2326 break;
2327 }
2328 }
2329
2330 *t_p = t;
2331
2332 return TRUE;
2333}
2334
2335/* Return TRUE if the symbol H is hidden by version script. */
2336
2337bfd_boolean
2338_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2339 struct elf_link_hash_entry *h)
2340{
2341 const char *p;
2342 bfd_boolean hide = FALSE;
2343 const struct elf_backend_data *bed
2344 = get_elf_backend_data (info->output_bfd);
2345
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2348 return TRUE;
2349
2350 p = strchr (h->root.root.string, ELF_VER_CHR);
2351 if (p != NULL && h->verinfo.vertree == NULL)
2352 {
2353 struct bfd_elf_version_tree *t;
2354
2355 ++p;
2356 if (*p == ELF_VER_CHR)
2357 ++p;
2358
2359 if (*p != '\0'
2360 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2361 && hide)
2362 {
2363 if (hide)
2364 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2365 return TRUE;
2366 }
2367 }
2368
2369 /* If we don't have a version for this symbol, see if we can find
2370 something. */
2371 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2372 {
2373 h->verinfo.vertree
2374 = bfd_find_version_for_sym (info->version_info,
2375 h->root.root.string, &hide);
2376 if (h->verinfo.vertree != NULL && hide)
2377 {
2378 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2379 return TRUE;
2380 }
2381 }
2382
2383 return FALSE;
2384}
2385
45d6a902
AM
2386/* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2390
28caa186 2391static bfd_boolean
268b6b39 2392_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2393{
28caa186 2394 struct elf_info_failed *sinfo;
45d6a902 2395 struct bfd_link_info *info;
9c5bfbb7 2396 const struct elf_backend_data *bed;
45d6a902
AM
2397 struct elf_info_failed eif;
2398 char *p;
099bb8fb 2399 bfd_boolean hide;
45d6a902 2400
a50b1753 2401 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2402 info = sinfo->info;
2403
45d6a902
AM
2404 /* Fix the symbol flags. */
2405 eif.failed = FALSE;
2406 eif.info = info;
2407 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2408 {
2409 if (eif.failed)
2410 sinfo->failed = TRUE;
2411 return FALSE;
2412 }
2413
0a640d71
L
2414 bed = get_elf_backend_data (info->output_bfd);
2415
45d6a902
AM
2416 /* We only need version numbers for symbols defined in regular
2417 objects. */
5fa370e4 2418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2419 {
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h->root.type == bfd_link_hash_defined
2422 || h->root.type == bfd_link_hash_defweak)
2423 && discarded_section (h->root.u.def.section))
2424 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2425 return TRUE;
2426 }
45d6a902 2427
099bb8fb 2428 hide = FALSE;
45d6a902
AM
2429 p = strchr (h->root.root.string, ELF_VER_CHR);
2430 if (p != NULL && h->verinfo.vertree == NULL)
2431 {
2432 struct bfd_elf_version_tree *t;
45d6a902 2433
45d6a902
AM
2434 ++p;
2435 if (*p == ELF_VER_CHR)
6e33951e 2436 ++p;
45d6a902
AM
2437
2438 /* If there is no version string, we can just return out. */
2439 if (*p == '\0')
6e33951e 2440 return TRUE;
45d6a902 2441
099bb8fb 2442 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2443 {
099bb8fb
L
2444 sinfo->failed = TRUE;
2445 return FALSE;
45d6a902
AM
2446 }
2447
099bb8fb
L
2448 if (hide)
2449 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2450
45d6a902
AM
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
0e1862bb 2453 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2454 {
2455 struct bfd_elf_version_tree **pp;
2456 int version_index;
2457
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h->dynindx == -1)
2461 return TRUE;
2462
ef53be89
AM
2463 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2464 sizeof *t);
45d6a902
AM
2465 if (t == NULL)
2466 {
2467 sinfo->failed = TRUE;
2468 return FALSE;
2469 }
2470
45d6a902 2471 t->name = p;
45d6a902
AM
2472 t->name_indx = (unsigned int) -1;
2473 t->used = TRUE;
2474
2475 version_index = 1;
2476 /* Don't count anonymous version tag. */
fd91d419
L
2477 if (sinfo->info->version_info != NULL
2478 && sinfo->info->version_info->vernum == 0)
45d6a902 2479 version_index = 0;
fd91d419
L
2480 for (pp = &sinfo->info->version_info;
2481 *pp != NULL;
2482 pp = &(*pp)->next)
45d6a902
AM
2483 ++version_index;
2484 t->vernum = version_index;
2485
2486 *pp = t;
2487
2488 h->verinfo.vertree = t;
2489 }
2490 else if (t == NULL)
2491 {
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
4eca0228 2494 _bfd_error_handler
695344c0 2495 /* xgettext:c-format */
871b3ab2 2496 (_("%pB: version node not found for symbol %s"),
28caa186 2497 info->output_bfd, h->root.root.string);
45d6a902
AM
2498 bfd_set_error (bfd_error_bad_value);
2499 sinfo->failed = TRUE;
2500 return FALSE;
2501 }
45d6a902
AM
2502 }
2503
2504 /* If we don't have a version for this symbol, see if we can find
2505 something. */
099bb8fb
L
2506 if (!hide
2507 && h->verinfo.vertree == NULL
2508 && sinfo->info->version_info != NULL)
45d6a902 2509 {
fd91d419
L
2510 h->verinfo.vertree
2511 = bfd_find_version_for_sym (sinfo->info->version_info,
2512 h->root.root.string, &hide);
1e8fa21e
AM
2513 if (h->verinfo.vertree != NULL && hide)
2514 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2515 }
2516
2517 return TRUE;
2518}
2519\f
45d6a902
AM
2520/* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2526
2527 Returns FALSE if something goes wrong. */
2528
2529static bfd_boolean
268b6b39 2530elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2531 asection *sec,
268b6b39
AM
2532 Elf_Internal_Shdr *shdr,
2533 void *external_relocs,
2534 Elf_Internal_Rela *internal_relocs)
45d6a902 2535{
9c5bfbb7 2536 const struct elf_backend_data *bed;
268b6b39 2537 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2538 const bfd_byte *erela;
2539 const bfd_byte *erelaend;
2540 Elf_Internal_Rela *irela;
243ef1e0
L
2541 Elf_Internal_Shdr *symtab_hdr;
2542 size_t nsyms;
45d6a902 2543
45d6a902
AM
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2546 return FALSE;
2547
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2550 return FALSE;
2551
243ef1e0 2552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2553 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2554
45d6a902
AM
2555 bed = get_elf_backend_data (abfd);
2556
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr->sh_entsize == bed->s->sizeof_rel)
2559 swap_in = bed->s->swap_reloc_in;
2560 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2561 swap_in = bed->s->swap_reloca_in;
2562 else
2563 {
2564 bfd_set_error (bfd_error_wrong_format);
2565 return FALSE;
2566 }
2567
a50b1753 2568 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2572 irela = internal_relocs;
f55b1e32 2573 while (erela <= erelaend)
45d6a902 2574 {
243ef1e0
L
2575 bfd_vma r_symndx;
2576
45d6a902 2577 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2578 r_symndx = ELF32_R_SYM (irela->r_info);
2579 if (bed->s->arch_size == 64)
2580 r_symndx >>= 24;
ce98a316
NC
2581 if (nsyms > 0)
2582 {
2583 if ((size_t) r_symndx >= nsyms)
2584 {
4eca0228 2585 _bfd_error_handler
695344c0 2586 /* xgettext:c-format */
2dcf00ce
AM
2587 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2588 " for offset %#" PRIx64 " in section `%pA'"),
2589 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2590 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2591 bfd_set_error (bfd_error_bad_value);
2592 return FALSE;
2593 }
2594 }
cf35638d 2595 else if (r_symndx != STN_UNDEF)
243ef1e0 2596 {
4eca0228 2597 _bfd_error_handler
695344c0 2598 /* xgettext:c-format */
2dcf00ce
AM
2599 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2600 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2601 " when the object file has no symbol table"),
2dcf00ce
AM
2602 abfd, (uint64_t) r_symndx,
2603 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2604 bfd_set_error (bfd_error_bad_value);
2605 return FALSE;
2606 }
45d6a902
AM
2607 irela += bed->s->int_rels_per_ext_rel;
2608 erela += shdr->sh_entsize;
2609 }
2610
2611 return TRUE;
2612}
2613
2614/* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2622 RELA_HDR relocations. */
45d6a902
AM
2623
2624Elf_Internal_Rela *
268b6b39
AM
2625_bfd_elf_link_read_relocs (bfd *abfd,
2626 asection *o,
2627 void *external_relocs,
2628 Elf_Internal_Rela *internal_relocs,
2629 bfd_boolean keep_memory)
45d6a902 2630{
268b6b39 2631 void *alloc1 = NULL;
45d6a902 2632 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2634 struct bfd_elf_section_data *esdo = elf_section_data (o);
2635 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2636
d4730f92
BS
2637 if (esdo->relocs != NULL)
2638 return esdo->relocs;
45d6a902
AM
2639
2640 if (o->reloc_count == 0)
2641 return NULL;
2642
45d6a902
AM
2643 if (internal_relocs == NULL)
2644 {
2645 bfd_size_type size;
2646
056bafd4 2647 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2648 if (keep_memory)
a50b1753 2649 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2650 else
a50b1753 2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2652 if (internal_relocs == NULL)
2653 goto error_return;
2654 }
2655
2656 if (external_relocs == NULL)
2657 {
d4730f92
BS
2658 bfd_size_type size = 0;
2659
2660 if (esdo->rel.hdr)
2661 size += esdo->rel.hdr->sh_size;
2662 if (esdo->rela.hdr)
2663 size += esdo->rela.hdr->sh_size;
45d6a902 2664
268b6b39 2665 alloc1 = bfd_malloc (size);
45d6a902
AM
2666 if (alloc1 == NULL)
2667 goto error_return;
2668 external_relocs = alloc1;
2669 }
2670
d4730f92
BS
2671 internal_rela_relocs = internal_relocs;
2672 if (esdo->rel.hdr)
2673 {
2674 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2675 external_relocs,
2676 internal_relocs))
2677 goto error_return;
2678 external_relocs = (((bfd_byte *) external_relocs)
2679 + esdo->rel.hdr->sh_size);
2680 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2681 * bed->s->int_rels_per_ext_rel);
2682 }
2683
2684 if (esdo->rela.hdr
2685 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2686 external_relocs,
2687 internal_rela_relocs)))
45d6a902
AM
2688 goto error_return;
2689
2690 /* Cache the results for next time, if we can. */
2691 if (keep_memory)
d4730f92 2692 esdo->relocs = internal_relocs;
45d6a902 2693
c9594989 2694 free (alloc1);
45d6a902
AM
2695
2696 /* Don't free alloc2, since if it was allocated we are passing it
2697 back (under the name of internal_relocs). */
2698
2699 return internal_relocs;
2700
2701 error_return:
c9594989 2702 free (alloc1);
45d6a902 2703 if (alloc2 != NULL)
4dd07732
AM
2704 {
2705 if (keep_memory)
2706 bfd_release (abfd, alloc2);
2707 else
2708 free (alloc2);
2709 }
45d6a902
AM
2710 return NULL;
2711}
2712
2713/* Compute the size of, and allocate space for, REL_HDR which is the
2714 section header for a section containing relocations for O. */
2715
28caa186 2716static bfd_boolean
9eaff861
AO
2717_bfd_elf_link_size_reloc_section (bfd *abfd,
2718 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2719{
9eaff861 2720 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2721
2722 /* That allows us to calculate the size of the section. */
9eaff861 2723 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2724
2725 /* The contents field must last into write_object_contents, so we
2726 allocate it with bfd_alloc rather than malloc. Also since we
2727 cannot be sure that the contents will actually be filled in,
2728 we zero the allocated space. */
a50b1753 2729 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2730 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2731 return FALSE;
2732
d4730f92 2733 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2734 {
2735 struct elf_link_hash_entry **p;
2736
ca4be51c
AM
2737 p = ((struct elf_link_hash_entry **)
2738 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2739 if (p == NULL)
2740 return FALSE;
2741
d4730f92 2742 reldata->hashes = p;
45d6a902
AM
2743 }
2744
2745 return TRUE;
2746}
2747
2748/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2749 originated from the section given by INPUT_REL_HDR) to the
2750 OUTPUT_BFD. */
2751
2752bfd_boolean
268b6b39
AM
2753_bfd_elf_link_output_relocs (bfd *output_bfd,
2754 asection *input_section,
2755 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2756 Elf_Internal_Rela *internal_relocs,
2757 struct elf_link_hash_entry **rel_hash
2758 ATTRIBUTE_UNUSED)
45d6a902
AM
2759{
2760 Elf_Internal_Rela *irela;
2761 Elf_Internal_Rela *irelaend;
2762 bfd_byte *erel;
d4730f92 2763 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2764 asection *output_section;
9c5bfbb7 2765 const struct elf_backend_data *bed;
268b6b39 2766 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2767 struct bfd_elf_section_data *esdo;
45d6a902
AM
2768
2769 output_section = input_section->output_section;
45d6a902 2770
d4730f92
BS
2771 bed = get_elf_backend_data (output_bfd);
2772 esdo = elf_section_data (output_section);
2773 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2774 {
d4730f92
BS
2775 output_reldata = &esdo->rel;
2776 swap_out = bed->s->swap_reloc_out;
45d6a902 2777 }
d4730f92
BS
2778 else if (esdo->rela.hdr
2779 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2780 {
d4730f92
BS
2781 output_reldata = &esdo->rela;
2782 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2783 }
2784 else
2785 {
4eca0228 2786 _bfd_error_handler
695344c0 2787 /* xgettext:c-format */
871b3ab2 2788 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2789 output_bfd, input_section->owner, input_section);
297d8443 2790 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2791 return FALSE;
2792 }
2793
d4730f92
BS
2794 erel = output_reldata->hdr->contents;
2795 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2796 irela = internal_relocs;
2797 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2798 * bed->s->int_rels_per_ext_rel);
2799 while (irela < irelaend)
2800 {
2801 (*swap_out) (output_bfd, irela, erel);
2802 irela += bed->s->int_rels_per_ext_rel;
2803 erel += input_rel_hdr->sh_entsize;
2804 }
2805
2806 /* Bump the counter, so that we know where to add the next set of
2807 relocations. */
d4730f92 2808 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2809
2810 return TRUE;
2811}
2812\f
508c3946
L
2813/* Make weak undefined symbols in PIE dynamic. */
2814
2815bfd_boolean
2816_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2817 struct elf_link_hash_entry *h)
2818{
0e1862bb 2819 if (bfd_link_pie (info)
508c3946
L
2820 && h->dynindx == -1
2821 && h->root.type == bfd_link_hash_undefweak)
2822 return bfd_elf_link_record_dynamic_symbol (info, h);
2823
2824 return TRUE;
2825}
2826
45d6a902
AM
2827/* Fix up the flags for a symbol. This handles various cases which
2828 can only be fixed after all the input files are seen. This is
2829 currently called by both adjust_dynamic_symbol and
2830 assign_sym_version, which is unnecessary but perhaps more robust in
2831 the face of future changes. */
2832
28caa186 2833static bfd_boolean
268b6b39
AM
2834_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2835 struct elf_info_failed *eif)
45d6a902 2836{
33774f08 2837 const struct elf_backend_data *bed;
508c3946 2838
45d6a902
AM
2839 /* If this symbol was mentioned in a non-ELF file, try to set
2840 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2841 permit a non-ELF file to correctly refer to a symbol defined in
2842 an ELF dynamic object. */
f5385ebf 2843 if (h->non_elf)
45d6a902
AM
2844 {
2845 while (h->root.type == bfd_link_hash_indirect)
2846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2847
2848 if (h->root.type != bfd_link_hash_defined
2849 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2850 {
2851 h->ref_regular = 1;
2852 h->ref_regular_nonweak = 1;
2853 }
45d6a902
AM
2854 else
2855 {
2856 if (h->root.u.def.section->owner != NULL
2857 && (bfd_get_flavour (h->root.u.def.section->owner)
2858 == bfd_target_elf_flavour))
f5385ebf
AM
2859 {
2860 h->ref_regular = 1;
2861 h->ref_regular_nonweak = 1;
2862 }
45d6a902 2863 else
f5385ebf 2864 h->def_regular = 1;
45d6a902
AM
2865 }
2866
2867 if (h->dynindx == -1
f5385ebf
AM
2868 && (h->def_dynamic
2869 || h->ref_dynamic))
45d6a902 2870 {
c152c796 2871 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2872 {
2873 eif->failed = TRUE;
2874 return FALSE;
2875 }
2876 }
2877 }
2878 else
2879 {
f5385ebf 2880 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2881 was first seen in a non-ELF file. Fortunately, if the symbol
2882 was first seen in an ELF file, we're probably OK unless the
2883 symbol was defined in a non-ELF file. Catch that case here.
2884 FIXME: We're still in trouble if the symbol was first seen in
2885 a dynamic object, and then later in a non-ELF regular object. */
2886 if ((h->root.type == bfd_link_hash_defined
2887 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2888 && !h->def_regular
45d6a902
AM
2889 && (h->root.u.def.section->owner != NULL
2890 ? (bfd_get_flavour (h->root.u.def.section->owner)
2891 != bfd_target_elf_flavour)
2892 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2893 && !h->def_dynamic)))
2894 h->def_regular = 1;
45d6a902
AM
2895 }
2896
508c3946 2897 /* Backend specific symbol fixup. */
33774f08
AM
2898 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2899 if (bed->elf_backend_fixup_symbol
2900 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2901 return FALSE;
508c3946 2902
45d6a902
AM
2903 /* If this is a final link, and the symbol was defined as a common
2904 symbol in a regular object file, and there was no definition in
2905 any dynamic object, then the linker will have allocated space for
f5385ebf 2906 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2907 flag will not have been set. */
2908 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2909 && !h->def_regular
2910 && h->ref_regular
2911 && !h->def_dynamic
96f29d96 2912 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2913 h->def_regular = 1;
45d6a902 2914
af0bfb9c
AM
2915 /* Symbols defined in discarded sections shouldn't be dynamic. */
2916 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2917 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2918
4deb8f71
L
2919 /* If a weak undefined symbol has non-default visibility, we also
2920 hide it from the dynamic linker. */
af0bfb9c
AM
2921 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2922 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2923 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2924
2925 /* A hidden versioned symbol in executable should be forced local if
2926 it is is locally defined, not referenced by shared library and not
2927 exported. */
2928 else if (bfd_link_executable (eif->info)
2929 && h->versioned == versioned_hidden
2930 && !eif->info->export_dynamic
2931 && !h->dynamic
2932 && !h->ref_dynamic
2933 && h->def_regular)
2934 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2935
45d6a902
AM
2936 /* If -Bsymbolic was used (which means to bind references to global
2937 symbols to the definition within the shared object), and this
2938 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2939 need a PLT entry. Likewise, if the symbol has non-default
2940 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2941 will force it local. */
4deb8f71
L
2942 else if (h->needs_plt
2943 && bfd_link_pic (eif->info)
2944 && is_elf_hash_table (eif->info->hash)
2945 && (SYMBOLIC_BIND (eif->info, h)
2946 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2947 && h->def_regular)
45d6a902 2948 {
45d6a902
AM
2949 bfd_boolean force_local;
2950
45d6a902
AM
2951 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2952 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2953 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2954 }
2955
45d6a902
AM
2956 /* If this is a weak defined symbol in a dynamic object, and we know
2957 the real definition in the dynamic object, copy interesting flags
2958 over to the real definition. */
60d67dc8 2959 if (h->is_weakalias)
45d6a902 2960 {
60d67dc8
AM
2961 struct elf_link_hash_entry *def = weakdef (h);
2962
45d6a902
AM
2963 /* If the real definition is defined by a regular object file,
2964 don't do anything special. See the longer description in
5b9d7a9a
AM
2965 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2966 bfd_link_hash_defined as it was when put on the alias list
2967 then it must have originally been a versioned symbol (for
2968 which a non-versioned indirect symbol is created) and later
2969 a definition for the non-versioned symbol is found. In that
2970 case the indirection is flipped with the versioned symbol
2971 becoming an indirect pointing at the non-versioned symbol.
2972 Thus, not an alias any more. */
2973 if (def->def_regular
2974 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2975 {
2976 h = def;
2977 while ((h = h->u.alias) != def)
2978 h->is_weakalias = 0;
2979 }
45d6a902 2980 else
a26587ba 2981 {
4e6b54a6
AM
2982 while (h->root.type == bfd_link_hash_indirect)
2983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2985 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2986 BFD_ASSERT (def->def_dynamic);
60d67dc8 2987 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2988 }
45d6a902
AM
2989 }
2990
2991 return TRUE;
2992}
2993
2994/* Make the backend pick a good value for a dynamic symbol. This is
2995 called via elf_link_hash_traverse, and also calls itself
2996 recursively. */
2997
28caa186 2998static bfd_boolean
268b6b39 2999_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3000{
a50b1753 3001 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3002 struct elf_link_hash_table *htab;
9c5bfbb7 3003 const struct elf_backend_data *bed;
45d6a902 3004
0eddce27 3005 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
3006 return FALSE;
3007
45d6a902
AM
3008 /* Ignore indirect symbols. These are added by the versioning code. */
3009 if (h->root.type == bfd_link_hash_indirect)
3010 return TRUE;
3011
3012 /* Fix the symbol flags. */
3013 if (! _bfd_elf_fix_symbol_flags (h, eif))
3014 return FALSE;
3015
559192d8
AM
3016 htab = elf_hash_table (eif->info);
3017 bed = get_elf_backend_data (htab->dynobj);
3018
954b63d4
AM
3019 if (h->root.type == bfd_link_hash_undefweak)
3020 {
3021 if (eif->info->dynamic_undefined_weak == 0)
559192d8 3022 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
3023 else if (eif->info->dynamic_undefined_weak > 0
3024 && h->ref_regular
3025 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3026 && !bfd_hide_sym_by_version (eif->info->version_info,
3027 h->root.root.string))
3028 {
3029 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3030 {
3031 eif->failed = TRUE;
3032 return FALSE;
3033 }
3034 }
3035 }
3036
45d6a902
AM
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
f5385ebf 3044 if (!h->needs_plt
91e21fb7 3045 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3046 && (h->def_regular
3047 || !h->def_dynamic
3048 || (!h->ref_regular
60d67dc8 3049 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3050 {
a6aa5195 3051 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3052 return TRUE;
3053 }
3054
3055 /* If we've already adjusted this symbol, don't do it again. This
3056 can happen via a recursive call. */
f5385ebf 3057 if (h->dynamic_adjusted)
45d6a902
AM
3058 return TRUE;
3059
3060 /* Don't look at this symbol again. Note that we must set this
3061 after checking the above conditions, because we may look at a
3062 symbol once, decide not to do anything, and then get called
3063 recursively later after REF_REGULAR is set below. */
f5385ebf 3064 h->dynamic_adjusted = 1;
45d6a902
AM
3065
3066 /* If this is a weak definition, and we know a real definition, and
3067 the real symbol is not itself defined by a regular object file,
3068 then get a good value for the real definition. We handle the
3069 real symbol first, for the convenience of the backend routine.
3070
3071 Note that there is a confusing case here. If the real definition
3072 is defined by a regular object file, we don't get the real symbol
3073 from the dynamic object, but we do get the weak symbol. If the
3074 processor backend uses a COPY reloc, then if some routine in the
3075 dynamic object changes the real symbol, we will not see that
3076 change in the corresponding weak symbol. This is the way other
3077 ELF linkers work as well, and seems to be a result of the shared
3078 library model.
3079
3080 I will clarify this issue. Most SVR4 shared libraries define the
3081 variable _timezone and define timezone as a weak synonym. The
3082 tzset call changes _timezone. If you write
3083 extern int timezone;
3084 int _timezone = 5;
3085 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3086 you might expect that, since timezone is a synonym for _timezone,
3087 the same number will print both times. However, if the processor
3088 backend uses a COPY reloc, then actually timezone will be copied
3089 into your process image, and, since you define _timezone
3090 yourself, _timezone will not. Thus timezone and _timezone will
3091 wind up at different memory locations. The tzset call will set
3092 _timezone, leaving timezone unchanged. */
3093
60d67dc8 3094 if (h->is_weakalias)
45d6a902 3095 {
60d67dc8
AM
3096 struct elf_link_hash_entry *def = weakdef (h);
3097
ec24dc88 3098 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3099 the alias by a regular object file via the weak symbol H. */
3100 def->ref_regular = 1;
45d6a902 3101
ec24dc88 3102 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3103 the strong alias before H by recursively calling ourselves. */
3104 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3105 return FALSE;
3106 }
3107
3108 /* If a symbol has no type and no size and does not require a PLT
3109 entry, then we are probably about to do the wrong thing here: we
3110 are probably going to create a COPY reloc for an empty object.
3111 This case can arise when a shared object is built with assembly
3112 code, and the assembly code fails to set the symbol type. */
3113 if (h->size == 0
3114 && h->type == STT_NOTYPE
f5385ebf 3115 && !h->needs_plt)
4eca0228 3116 _bfd_error_handler
45d6a902
AM
3117 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3118 h->root.root.string);
3119
45d6a902
AM
3120 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3121 {
3122 eif->failed = TRUE;
3123 return FALSE;
3124 }
3125
3126 return TRUE;
3127}
3128
027297b7
L
3129/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3130 DYNBSS. */
3131
3132bfd_boolean
6cabe1ea
AM
3133_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3134 struct elf_link_hash_entry *h,
027297b7
L
3135 asection *dynbss)
3136{
91ac5911 3137 unsigned int power_of_two;
027297b7
L
3138 bfd_vma mask;
3139 asection *sec = h->root.u.def.section;
3140
de194d85 3141 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3142 requirement of symbols defined in the section. Since we don't
3143 know the symbol alignment requirement, we start with the
3144 maximum alignment and check low bits of the symbol address
3145 for the minimum alignment. */
fd361982 3146 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3147 mask = ((bfd_vma) 1 << power_of_two) - 1;
3148 while ((h->root.u.def.value & mask) != 0)
3149 {
3150 mask >>= 1;
3151 --power_of_two;
3152 }
027297b7 3153
fd361982 3154 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3155 {
3156 /* Adjust the section alignment if needed. */
fd361982 3157 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3158 return FALSE;
3159 }
3160
91ac5911 3161 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3162 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3163
3164 /* Define the symbol as being at this point in DYNBSS. */
3165 h->root.u.def.section = dynbss;
3166 h->root.u.def.value = dynbss->size;
3167
3168 /* Increment the size of DYNBSS to make room for the symbol. */
3169 dynbss->size += h->size;
3170
f7483970
L
3171 /* No error if extern_protected_data is true. */
3172 if (h->protected_def
889c2a67
L
3173 && (!info->extern_protected_data
3174 || (info->extern_protected_data < 0
3175 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3176 info->callbacks->einfo
c1c8c1ef 3177 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3178 h->root.root.string);
6cabe1ea 3179
027297b7
L
3180 return TRUE;
3181}
3182
45d6a902
AM
3183/* Adjust all external symbols pointing into SEC_MERGE sections
3184 to reflect the object merging within the sections. */
3185
28caa186 3186static bfd_boolean
268b6b39 3187_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3188{
3189 asection *sec;
3190
45d6a902
AM
3191 if ((h->root.type == bfd_link_hash_defined
3192 || h->root.type == bfd_link_hash_defweak)
3193 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3194 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3195 {
a50b1753 3196 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3197
3198 h->root.u.def.value =
3199 _bfd_merged_section_offset (output_bfd,
3200 &h->root.u.def.section,
3201 elf_section_data (sec)->sec_info,
753731ee 3202 h->root.u.def.value);
45d6a902
AM
3203 }
3204
3205 return TRUE;
3206}
986a241f
RH
3207
3208/* Returns false if the symbol referred to by H should be considered
3209 to resolve local to the current module, and true if it should be
3210 considered to bind dynamically. */
3211
3212bfd_boolean
268b6b39
AM
3213_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3214 struct bfd_link_info *info,
89a2ee5a 3215 bfd_boolean not_local_protected)
986a241f
RH
3216{
3217 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3218 const struct elf_backend_data *bed;
3219 struct elf_link_hash_table *hash_table;
986a241f
RH
3220
3221 if (h == NULL)
3222 return FALSE;
3223
3224 while (h->root.type == bfd_link_hash_indirect
3225 || h->root.type == bfd_link_hash_warning)
3226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3227
3228 /* If it was forced local, then clearly it's not dynamic. */
3229 if (h->dynindx == -1)
3230 return FALSE;
f5385ebf 3231 if (h->forced_local)
986a241f
RH
3232 return FALSE;
3233
3234 /* Identify the cases where name binding rules say that a
3235 visible symbol resolves locally. */
0e1862bb
L
3236 binding_stays_local_p = (bfd_link_executable (info)
3237 || SYMBOLIC_BIND (info, h));
986a241f
RH
3238
3239 switch (ELF_ST_VISIBILITY (h->other))
3240 {
3241 case STV_INTERNAL:
3242 case STV_HIDDEN:
3243 return FALSE;
3244
3245 case STV_PROTECTED:
fcb93ecf
PB
3246 hash_table = elf_hash_table (info);
3247 if (!is_elf_hash_table (hash_table))
3248 return FALSE;
3249
3250 bed = get_elf_backend_data (hash_table->dynobj);
3251
986a241f
RH
3252 /* Proper resolution for function pointer equality may require
3253 that these symbols perhaps be resolved dynamically, even though
3254 we should be resolving them to the current module. */
89a2ee5a 3255 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3256 binding_stays_local_p = TRUE;
3257 break;
3258
3259 default:
986a241f
RH
3260 break;
3261 }
3262
aa37626c 3263 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3264 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3265 return TRUE;
3266
986a241f
RH
3267 /* Otherwise, the symbol is dynamic if binding rules don't tell
3268 us that it remains local. */
3269 return !binding_stays_local_p;
3270}
f6c52c13
AM
3271
3272/* Return true if the symbol referred to by H should be considered
3273 to resolve local to the current module, and false otherwise. Differs
3274 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3275 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3276 for the place where dynindx == -1 is tested. If that test is true,
3277 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3278 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3279 defined symbols.
89a2ee5a
AM
3280 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3281 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3282 treatment of undefined weak symbols. For those that do not make
3283 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3284
3285bfd_boolean
268b6b39
AM
3286_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3287 struct bfd_link_info *info,
3288 bfd_boolean local_protected)
f6c52c13 3289{
fcb93ecf
PB
3290 const struct elf_backend_data *bed;
3291 struct elf_link_hash_table *hash_table;
3292
f6c52c13
AM
3293 /* If it's a local sym, of course we resolve locally. */
3294 if (h == NULL)
3295 return TRUE;
3296
d95edcac
L
3297 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3298 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3299 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3300 return TRUE;
3301
0fad2956
MR
3302 /* Forced local symbols resolve locally. */
3303 if (h->forced_local)
3304 return TRUE;
3305
7e2294f9
AO
3306 /* Common symbols that become definitions don't get the DEF_REGULAR
3307 flag set, so test it first, and don't bail out. */
3308 if (ELF_COMMON_DEF_P (h))
3309 /* Do nothing. */;
f6c52c13 3310 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3311 resolve locally. The sym is either undefined or dynamic. */
3312 else if (!h->def_regular)
f6c52c13
AM
3313 return FALSE;
3314
0fad2956 3315 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3316 if (h->dynindx == -1)
3317 return TRUE;
3318
3319 /* At this point, we know the symbol is defined and dynamic. In an
3320 executable it must resolve locally, likewise when building symbolic
3321 shared libraries. */
0e1862bb 3322 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3323 return TRUE;
3324
3325 /* Now deal with defined dynamic symbols in shared libraries. Ones
3326 with default visibility might not resolve locally. */
3327 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3328 return FALSE;
3329
fcb93ecf
PB
3330 hash_table = elf_hash_table (info);
3331 if (!is_elf_hash_table (hash_table))
3332 return TRUE;
3333
3334 bed = get_elf_backend_data (hash_table->dynobj);
3335
f7483970
L
3336 /* If extern_protected_data is false, STV_PROTECTED non-function
3337 symbols are local. */
889c2a67
L
3338 if ((!info->extern_protected_data
3339 || (info->extern_protected_data < 0
3340 && !bed->extern_protected_data))
3341 && !bed->is_function_type (h->type))
1c16dfa5
L
3342 return TRUE;
3343
f6c52c13 3344 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3345 symbols be treated as dynamic symbols. If the address of a
3346 function not defined in an executable is set to that function's
3347 plt entry in the executable, then the address of the function in
3348 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3349 return local_protected;
3350}
e1918d23
AM
3351
3352/* Caches some TLS segment info, and ensures that the TLS segment vma is
3353 aligned. Returns the first TLS output section. */
3354
3355struct bfd_section *
3356_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3357{
3358 struct bfd_section *sec, *tls;
3359 unsigned int align = 0;
3360
3361 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3362 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3363 break;
3364 tls = sec;
3365
3366 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3367 if (sec->alignment_power > align)
3368 align = sec->alignment_power;
3369
3370 elf_hash_table (info)->tls_sec = tls;
3371
fdde2fb6
SH
3372 /* Ensure the alignment of the first section (usually .tdata) is the largest
3373 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3374 if (tls != NULL)
3375 tls->alignment_power = align;
3376
3377 return tls;
3378}
0ad989f9
L
3379
3380/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3381static bfd_boolean
3382is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3383 Elf_Internal_Sym *sym)
3384{
a4d8e49b
L
3385 const struct elf_backend_data *bed;
3386
0ad989f9
L
3387 /* Local symbols do not count, but target specific ones might. */
3388 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3389 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3390 return FALSE;
3391
fcb93ecf 3392 bed = get_elf_backend_data (abfd);
0ad989f9 3393 /* Function symbols do not count. */
fcb93ecf 3394 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3395 return FALSE;
3396
3397 /* If the section is undefined, then so is the symbol. */
3398 if (sym->st_shndx == SHN_UNDEF)
3399 return FALSE;
3400
3401 /* If the symbol is defined in the common section, then
3402 it is a common definition and so does not count. */
a4d8e49b 3403 if (bed->common_definition (sym))
0ad989f9
L
3404 return FALSE;
3405
3406 /* If the symbol is in a target specific section then we
3407 must rely upon the backend to tell us what it is. */
3408 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3409 /* FIXME - this function is not coded yet:
3410
3411 return _bfd_is_global_symbol_definition (abfd, sym);
3412
3413 Instead for now assume that the definition is not global,
3414 Even if this is wrong, at least the linker will behave
3415 in the same way that it used to do. */
3416 return FALSE;
3417
3418 return TRUE;
3419}
3420
3421/* Search the symbol table of the archive element of the archive ABFD
3422 whose archive map contains a mention of SYMDEF, and determine if
3423 the symbol is defined in this element. */
3424static bfd_boolean
3425elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3426{
3427 Elf_Internal_Shdr * hdr;
ef53be89
AM
3428 size_t symcount;
3429 size_t extsymcount;
3430 size_t extsymoff;
0ad989f9
L
3431 Elf_Internal_Sym *isymbuf;
3432 Elf_Internal_Sym *isym;
3433 Elf_Internal_Sym *isymend;
3434 bfd_boolean result;
3435
3436 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3437 if (abfd == NULL)
3438 return FALSE;
3439
3440 if (! bfd_check_format (abfd, bfd_object))
3441 return FALSE;
3442
7dc3990e
L
3443 /* Select the appropriate symbol table. If we don't know if the
3444 object file is an IR object, give linker LTO plugin a chance to
3445 get the correct symbol table. */
3446 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3447#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3448 || (abfd->plugin_format == bfd_plugin_unknown
3449 && bfd_link_plugin_object_p (abfd))
3450#endif
3451 )
3452 {
3453 /* Use the IR symbol table if the object has been claimed by
3454 plugin. */
3455 abfd = abfd->plugin_dummy_bfd;
3456 hdr = &elf_tdata (abfd)->symtab_hdr;
3457 }
3458 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3459 hdr = &elf_tdata (abfd)->symtab_hdr;
3460 else
3461 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3462
3463 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3464
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols. */
3467 if (elf_bad_symtab (abfd))
3468 {
3469 extsymcount = symcount;
3470 extsymoff = 0;
3471 }
3472 else
3473 {
3474 extsymcount = symcount - hdr->sh_info;
3475 extsymoff = hdr->sh_info;
3476 }
3477
3478 if (extsymcount == 0)
3479 return FALSE;
3480
3481 /* Read in the symbol table. */
3482 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3483 NULL, NULL, NULL);
3484 if (isymbuf == NULL)
3485 return FALSE;
3486
3487 /* Scan the symbol table looking for SYMDEF. */
3488 result = FALSE;
3489 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3490 {
3491 const char *name;
3492
3493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3494 isym->st_name);
3495 if (name == NULL)
3496 break;
3497
3498 if (strcmp (name, symdef->name) == 0)
3499 {
3500 result = is_global_data_symbol_definition (abfd, isym);
3501 break;
3502 }
3503 }
3504
3505 free (isymbuf);
3506
3507 return result;
3508}
3509\f
5a580b3a
AM
3510/* Add an entry to the .dynamic table. */
3511
3512bfd_boolean
3513_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3514 bfd_vma tag,
3515 bfd_vma val)
3516{
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s;
3520 bfd_size_type newsize;
3521 bfd_byte *newcontents;
3522 Elf_Internal_Dyn dyn;
3523
3524 hash_table = elf_hash_table (info);
3525 if (! is_elf_hash_table (hash_table))
3526 return FALSE;
3527
7f923b7f
AM
3528 if (tag == DT_RELA || tag == DT_REL)
3529 hash_table->dynamic_relocs = TRUE;
3530
5a580b3a 3531 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3532 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3533 BFD_ASSERT (s != NULL);
3534
eea6121a 3535 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3536 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3537 if (newcontents == NULL)
3538 return FALSE;
3539
3540 dyn.d_tag = tag;
3541 dyn.d_un.d_val = val;
eea6121a 3542 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3543
eea6121a 3544 s->size = newsize;
5a580b3a
AM
3545 s->contents = newcontents;
3546
3547 return TRUE;
3548}
3549
6f6fd151
L
3550/* Strip zero-sized dynamic sections. */
3551
3552bfd_boolean
3553_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3554{
3555 struct elf_link_hash_table *hash_table;
3556 const struct elf_backend_data *bed;
3557 asection *s, *sdynamic, **pp;
3558 asection *rela_dyn, *rel_dyn;
3559 Elf_Internal_Dyn dyn;
3560 bfd_byte *extdyn, *next;
3561 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3562 bfd_boolean strip_zero_sized;
3563 bfd_boolean strip_zero_sized_plt;
3564
3565 if (bfd_link_relocatable (info))
3566 return TRUE;
3567
3568 hash_table = elf_hash_table (info);
3569 if (!is_elf_hash_table (hash_table))
3570 return FALSE;
3571
3572 if (!hash_table->dynobj)
3573 return TRUE;
3574
3575 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3576 if (!sdynamic)
3577 return TRUE;
3578
3579 bed = get_elf_backend_data (hash_table->dynobj);
3580 swap_dyn_in = bed->s->swap_dyn_in;
3581
3582 strip_zero_sized = FALSE;
3583 strip_zero_sized_plt = FALSE;
3584
3585 /* Strip zero-sized dynamic sections. */
3586 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3587 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3588 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3589 if (s->size == 0
3590 && (s == rela_dyn
3591 || s == rel_dyn
3592 || s == hash_table->srelplt->output_section
3593 || s == hash_table->splt->output_section))
3594 {
3595 *pp = s->next;
3596 info->output_bfd->section_count--;
3597 strip_zero_sized = TRUE;
3598 if (s == rela_dyn)
3599 s = rela_dyn;
3600 if (s == rel_dyn)
3601 s = rel_dyn;
3602 else if (s == hash_table->splt->output_section)
3603 {
3604 s = hash_table->splt;
3605 strip_zero_sized_plt = TRUE;
3606 }
3607 else
3608 s = hash_table->srelplt;
3609 s->flags |= SEC_EXCLUDE;
3610 s->output_section = bfd_abs_section_ptr;
3611 }
3612 else
3613 pp = &s->next;
3614
3615 if (strip_zero_sized_plt)
3616 for (extdyn = sdynamic->contents;
3617 extdyn < sdynamic->contents + sdynamic->size;
3618 extdyn = next)
3619 {
3620 next = extdyn + bed->s->sizeof_dyn;
3621 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3622 switch (dyn.d_tag)
3623 {
3624 default:
3625 break;
3626 case DT_JMPREL:
3627 case DT_PLTRELSZ:
3628 case DT_PLTREL:
3629 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3630 the procedure linkage table (the .plt section) has been
3631 removed. */
3632 memmove (extdyn, next,
3633 sdynamic->size - (next - sdynamic->contents));
3634 next = extdyn;
3635 }
3636 }
3637
3638 if (strip_zero_sized)
3639 {
3640 /* Regenerate program headers. */
3641 elf_seg_map (info->output_bfd) = NULL;
3642 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3643 }
3644
3645 return TRUE;
3646}
3647
e310298c 3648/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3649 1 if a DT_NEEDED tag already exists, and 0 on success. */
3650
e310298c
AM
3651int
3652bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3653{
3654 struct elf_link_hash_table *hash_table;
ef53be89 3655 size_t strindex;
e310298c 3656 const char *soname;
5a580b3a 3657
7e9f0867
AM
3658 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3659 return -1;
3660
5a580b3a 3661 hash_table = elf_hash_table (info);
e310298c 3662 soname = elf_dt_name (abfd);
5a580b3a 3663 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3664 if (strindex == (size_t) -1)
5a580b3a
AM
3665 return -1;
3666
02be4619 3667 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3668 {
3669 asection *sdyn;
3670 const struct elf_backend_data *bed;
3671 bfd_byte *extdyn;
3672
3673 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3674 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3675 if (sdyn != NULL)
3676 for (extdyn = sdyn->contents;
3677 extdyn < sdyn->contents + sdyn->size;
3678 extdyn += bed->s->sizeof_dyn)
3679 {
3680 Elf_Internal_Dyn dyn;
5a580b3a 3681
7e9f0867
AM
3682 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3683 if (dyn.d_tag == DT_NEEDED
3684 && dyn.d_un.d_val == strindex)
3685 {
3686 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3687 return 1;
3688 }
3689 }
5a580b3a
AM
3690 }
3691
e310298c
AM
3692 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3693 return -1;
7e9f0867 3694
e310298c
AM
3695 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3696 return -1;
5a580b3a
AM
3697
3698 return 0;
3699}
3700
7b15fa7a
AM
3701/* Return true if SONAME is on the needed list between NEEDED and STOP
3702 (or the end of list if STOP is NULL), and needed by a library that
3703 will be loaded. */
3704
010e5ae2 3705static bfd_boolean
7b15fa7a
AM
3706on_needed_list (const char *soname,
3707 struct bfd_link_needed_list *needed,
3708 struct bfd_link_needed_list *stop)
010e5ae2 3709{
7b15fa7a
AM
3710 struct bfd_link_needed_list *look;
3711 for (look = needed; look != stop; look = look->next)
3712 if (strcmp (soname, look->name) == 0
3713 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3714 /* If needed by a library that itself is not directly
3715 needed, recursively check whether that library is
3716 indirectly needed. Since we add DT_NEEDED entries to
3717 the end of the list, library dependencies appear after
3718 the library. Therefore search prior to the current
3719 LOOK, preventing possible infinite recursion. */
3720 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3721 return TRUE;
3722
3723 return FALSE;
3724}
3725
3a3f4bf7 3726/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3727static int
3728elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3729{
3730 const struct elf_link_hash_entry *h1;
3731 const struct elf_link_hash_entry *h2;
10b7e05b 3732 bfd_signed_vma vdiff;
3a3f4bf7
AM
3733 int sdiff;
3734 const char *n1;
3735 const char *n2;
5a580b3a
AM
3736
3737 h1 = *(const struct elf_link_hash_entry **) arg1;
3738 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3739 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3740 if (vdiff != 0)
3741 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3742
3743 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3744 if (sdiff != 0)
3745 return sdiff;
3746
3747 /* Sort so that sized symbols are selected over zero size symbols. */
3748 vdiff = h1->size - h2->size;
3749 if (vdiff != 0)
3750 return vdiff > 0 ? 1 : -1;
3751
3752 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3753 if (h1->type != h2->type)
3754 return h1->type - h2->type;
3755
3756 /* If symbols are properly sized and typed, and multiple strong
3757 aliases are not defined in a shared library by the user we
3758 shouldn't get here. Unfortunately linker script symbols like
3759 __bss_start sometimes match a user symbol defined at the start of
3760 .bss without proper size and type. We'd like to preference the
3761 user symbol over reserved system symbols. Sort on leading
3762 underscores. */
3763 n1 = h1->root.root.string;
3764 n2 = h2->root.root.string;
3765 while (*n1 == *n2)
10b7e05b 3766 {
3a3f4bf7
AM
3767 if (*n1 == 0)
3768 break;
3769 ++n1;
3770 ++n2;
10b7e05b 3771 }
3a3f4bf7
AM
3772 if (*n1 == '_')
3773 return -1;
3774 if (*n2 == '_')
3775 return 1;
3776
3777 /* Final sort on name selects user symbols like '_u' over reserved
3778 system symbols like '_Z' and also will avoid qsort instability. */
3779 return *n1 - *n2;
5a580b3a 3780}
4ad4eba5 3781
5a580b3a
AM
3782/* This function is used to adjust offsets into .dynstr for
3783 dynamic symbols. This is called via elf_link_hash_traverse. */
3784
3785static bfd_boolean
3786elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3787{
a50b1753 3788 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3789
5a580b3a
AM
3790 if (h->dynindx != -1)
3791 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3792 return TRUE;
3793}
3794
3795/* Assign string offsets in .dynstr, update all structures referencing
3796 them. */
3797
4ad4eba5
AM
3798static bfd_boolean
3799elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3800{
3801 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3802 struct elf_link_local_dynamic_entry *entry;
3803 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3804 bfd *dynobj = hash_table->dynobj;
3805 asection *sdyn;
3806 bfd_size_type size;
3807 const struct elf_backend_data *bed;
3808 bfd_byte *extdyn;
3809
3810 _bfd_elf_strtab_finalize (dynstr);
3811 size = _bfd_elf_strtab_size (dynstr);
3812
3d16b64e
NA
3813 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3814
3815 if (info->callbacks->examine_strtab)
3816 info->callbacks->examine_strtab (dynstr);
3817
5a580b3a 3818 bed = get_elf_backend_data (dynobj);
3d4d4302 3819 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3820 BFD_ASSERT (sdyn != NULL);
3821
3822 /* Update all .dynamic entries referencing .dynstr strings. */
3823 for (extdyn = sdyn->contents;
eea6121a 3824 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3825 extdyn += bed->s->sizeof_dyn)
3826 {
3827 Elf_Internal_Dyn dyn;
3828
3829 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3830 switch (dyn.d_tag)
3831 {
3832 case DT_STRSZ:
3833 dyn.d_un.d_val = size;
3834 break;
3835 case DT_NEEDED:
3836 case DT_SONAME:
3837 case DT_RPATH:
3838 case DT_RUNPATH:
3839 case DT_FILTER:
3840 case DT_AUXILIARY:
7ee314fa
AM
3841 case DT_AUDIT:
3842 case DT_DEPAUDIT:
5a580b3a
AM
3843 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3844 break;
3845 default:
3846 continue;
3847 }
3848 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3849 }
3850
3851 /* Now update local dynamic symbols. */
3852 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3853 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3854 entry->isym.st_name);
3855
3856 /* And the rest of dynamic symbols. */
3857 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3858
3859 /* Adjust version definitions. */
3860 if (elf_tdata (output_bfd)->cverdefs)
3861 {
3862 asection *s;
3863 bfd_byte *p;
ef53be89 3864 size_t i;
5a580b3a
AM
3865 Elf_Internal_Verdef def;
3866 Elf_Internal_Verdaux defaux;
3867
3d4d4302 3868 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3869 p = s->contents;
3870 do
3871 {
3872 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3873 &def);
3874 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3875 if (def.vd_aux != sizeof (Elf_External_Verdef))
3876 continue;
5a580b3a
AM
3877 for (i = 0; i < def.vd_cnt; ++i)
3878 {
3879 _bfd_elf_swap_verdaux_in (output_bfd,
3880 (Elf_External_Verdaux *) p, &defaux);
3881 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3882 defaux.vda_name);
3883 _bfd_elf_swap_verdaux_out (output_bfd,
3884 &defaux, (Elf_External_Verdaux *) p);
3885 p += sizeof (Elf_External_Verdaux);
3886 }
3887 }
3888 while (def.vd_next);
3889 }
3890
3891 /* Adjust version references. */
3892 if (elf_tdata (output_bfd)->verref)
3893 {
3894 asection *s;
3895 bfd_byte *p;
ef53be89 3896 size_t i;
5a580b3a
AM
3897 Elf_Internal_Verneed need;
3898 Elf_Internal_Vernaux needaux;
3899
3d4d4302 3900 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3901 p = s->contents;
3902 do
3903 {
3904 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3905 &need);
3906 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3907 _bfd_elf_swap_verneed_out (output_bfd, &need,
3908 (Elf_External_Verneed *) p);
3909 p += sizeof (Elf_External_Verneed);
3910 for (i = 0; i < need.vn_cnt; ++i)
3911 {
3912 _bfd_elf_swap_vernaux_in (output_bfd,
3913 (Elf_External_Vernaux *) p, &needaux);
3914 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3915 needaux.vna_name);
3916 _bfd_elf_swap_vernaux_out (output_bfd,
3917 &needaux,
3918 (Elf_External_Vernaux *) p);
3919 p += sizeof (Elf_External_Vernaux);
3920 }
3921 }
3922 while (need.vn_next);
3923 }
3924
3925 return TRUE;
3926}
3927\f
13285a1b
AM
3928/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3929 The default is to only match when the INPUT and OUTPUT are exactly
3930 the same target. */
3931
3932bfd_boolean
3933_bfd_elf_default_relocs_compatible (const bfd_target *input,
3934 const bfd_target *output)
3935{
3936 return input == output;
3937}
3938
3939/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3940 This version is used when different targets for the same architecture
3941 are virtually identical. */
3942
3943bfd_boolean
3944_bfd_elf_relocs_compatible (const bfd_target *input,
3945 const bfd_target *output)
3946{
3947 const struct elf_backend_data *obed, *ibed;
3948
3949 if (input == output)
3950 return TRUE;
3951
3952 ibed = xvec_get_elf_backend_data (input);
3953 obed = xvec_get_elf_backend_data (output);
3954
3955 if (ibed->arch != obed->arch)
3956 return FALSE;
3957
3958 /* If both backends are using this function, deem them compatible. */
3959 return ibed->relocs_compatible == obed->relocs_compatible;
3960}
3961
e5034e59
AM
3962/* Make a special call to the linker "notice" function to tell it that
3963 we are about to handle an as-needed lib, or have finished
1b786873 3964 processing the lib. */
e5034e59
AM
3965
3966bfd_boolean
3967_bfd_elf_notice_as_needed (bfd *ibfd,
3968 struct bfd_link_info *info,
3969 enum notice_asneeded_action act)
3970{
46135103 3971 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3972}
3973
d9689752
L
3974/* Check relocations an ELF object file. */
3975
3976bfd_boolean
3977_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3978{
3979 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3980 struct elf_link_hash_table *htab = elf_hash_table (info);
3981
3982 /* If this object is the same format as the output object, and it is
3983 not a shared library, then let the backend look through the
3984 relocs.
3985
3986 This is required to build global offset table entries and to
3987 arrange for dynamic relocs. It is not required for the
3988 particular common case of linking non PIC code, even when linking
3989 against shared libraries, but unfortunately there is no way of
3990 knowing whether an object file has been compiled PIC or not.
3991 Looking through the relocs is not particularly time consuming.
3992 The problem is that we must either (1) keep the relocs in memory,
3993 which causes the linker to require additional runtime memory or
3994 (2) read the relocs twice from the input file, which wastes time.
3995 This would be a good case for using mmap.
3996
3997 I have no idea how to handle linking PIC code into a file of a
3998 different format. It probably can't be done. */
3999 if ((abfd->flags & DYNAMIC) == 0
4000 && is_elf_hash_table (htab)
4001 && bed->check_relocs != NULL
4002 && elf_object_id (abfd) == elf_hash_table_id (htab)
4003 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4004 {
4005 asection *o;
4006
4007 for (o = abfd->sections; o != NULL; o = o->next)
4008 {
4009 Elf_Internal_Rela *internal_relocs;
4010 bfd_boolean ok;
4011
c4b126b8
L
4012 /* Don't check relocations in excluded sections. Don't do
4013 anything special with non-loaded, non-alloced sections.
4014 In particular, any relocs in such sections should not
4015 affect GOT and PLT reference counting (ie. we don't
4016 allow them to create GOT or PLT entries), there's no
4017 possibility or desire to optimize TLS relocs, and
4018 there's not much point in propagating relocs to shared
4019 libs that the dynamic linker won't relocate. */
4020 if ((o->flags & SEC_ALLOC) == 0
4021 || (o->flags & SEC_RELOC) == 0
5ce03cea 4022 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4023 || o->reloc_count == 0
4024 || ((info->strip == strip_all || info->strip == strip_debugger)
4025 && (o->flags & SEC_DEBUGGING) != 0)
4026 || bfd_is_abs_section (o->output_section))
4027 continue;
4028
4029 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4030 info->keep_memory);
4031 if (internal_relocs == NULL)
4032 return FALSE;
4033
4034 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4035
4036 if (elf_section_data (o)->relocs != internal_relocs)
4037 free (internal_relocs);
4038
4039 if (! ok)
4040 return FALSE;
4041 }
4042 }
4043
4044 return TRUE;
4045}
4046
4ad4eba5
AM
4047/* Add symbols from an ELF object file to the linker hash table. */
4048
4049static bfd_boolean
4050elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4051{
a0c402a5 4052 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4053 Elf_Internal_Shdr *hdr;
ef53be89
AM
4054 size_t symcount;
4055 size_t extsymcount;
4056 size_t extsymoff;
4ad4eba5
AM
4057 struct elf_link_hash_entry **sym_hash;
4058 bfd_boolean dynamic;
4059 Elf_External_Versym *extversym = NULL;
be22c732 4060 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4061 Elf_External_Versym *ever;
4062 struct elf_link_hash_entry *weaks;
4063 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4064 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4065 Elf_Internal_Sym *isymbuf = NULL;
4066 Elf_Internal_Sym *isym;
4067 Elf_Internal_Sym *isymend;
4068 const struct elf_backend_data *bed;
4069 bfd_boolean add_needed;
66eb6687 4070 struct elf_link_hash_table *htab;
66eb6687 4071 void *alloc_mark = NULL;
4f87808c
AM
4072 struct bfd_hash_entry **old_table = NULL;
4073 unsigned int old_size = 0;
4074 unsigned int old_count = 0;
66eb6687 4075 void *old_tab = NULL;
66eb6687
AM
4076 void *old_ent;
4077 struct bfd_link_hash_entry *old_undefs = NULL;
4078 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4079 void *old_strtab = NULL;
66eb6687 4080 size_t tabsize = 0;
db6a5d5f 4081 asection *s;
29a9f53e 4082 bfd_boolean just_syms;
4ad4eba5 4083
66eb6687 4084 htab = elf_hash_table (info);
4ad4eba5 4085 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4086
4087 if ((abfd->flags & DYNAMIC) == 0)
4088 dynamic = FALSE;
4089 else
4090 {
4091 dynamic = TRUE;
4092
4093 /* You can't use -r against a dynamic object. Also, there's no
4094 hope of using a dynamic object which does not exactly match
4095 the format of the output file. */
0e1862bb 4096 if (bfd_link_relocatable (info)
66eb6687 4097 || !is_elf_hash_table (htab)
f13a99db 4098 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4099 {
0e1862bb 4100 if (bfd_link_relocatable (info))
9a0789ec
NC
4101 bfd_set_error (bfd_error_invalid_operation);
4102 else
4103 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4104 goto error_return;
4105 }
4106 }
4107
a0c402a5
L
4108 ehdr = elf_elfheader (abfd);
4109 if (info->warn_alternate_em
4110 && bed->elf_machine_code != ehdr->e_machine
4111 && ((bed->elf_machine_alt1 != 0
4112 && ehdr->e_machine == bed->elf_machine_alt1)
4113 || (bed->elf_machine_alt2 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4115 _bfd_error_handler
695344c0 4116 /* xgettext:c-format */
9793eb77 4117 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4118 ehdr->e_machine, abfd, bed->elf_machine_code);
4119
4ad4eba5
AM
4120 /* As a GNU extension, any input sections which are named
4121 .gnu.warning.SYMBOL are treated as warning symbols for the given
4122 symbol. This differs from .gnu.warning sections, which generate
4123 warnings when they are included in an output file. */
dd98f8d2 4124 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4125 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4126 {
db6a5d5f 4127 const char *name;
4ad4eba5 4128
fd361982 4129 name = bfd_section_name (s);
db6a5d5f 4130 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4131 {
db6a5d5f
AM
4132 char *msg;
4133 bfd_size_type sz;
4134
4135 name += sizeof ".gnu.warning." - 1;
4136
4137 /* If this is a shared object, then look up the symbol
4138 in the hash table. If it is there, and it is already
4139 been defined, then we will not be using the entry
4140 from this shared object, so we don't need to warn.
4141 FIXME: If we see the definition in a regular object
4142 later on, we will warn, but we shouldn't. The only
4143 fix is to keep track of what warnings we are supposed
4144 to emit, and then handle them all at the end of the
4145 link. */
4146 if (dynamic)
4ad4eba5 4147 {
db6a5d5f
AM
4148 struct elf_link_hash_entry *h;
4149
4150 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4151
4152 /* FIXME: What about bfd_link_hash_common? */
4153 if (h != NULL
4154 && (h->root.type == bfd_link_hash_defined
4155 || h->root.type == bfd_link_hash_defweak))
4156 continue;
4157 }
4ad4eba5 4158
db6a5d5f
AM
4159 sz = s->size;
4160 msg = (char *) bfd_alloc (abfd, sz + 1);
4161 if (msg == NULL)
4162 goto error_return;
4ad4eba5 4163
db6a5d5f
AM
4164 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4165 goto error_return;
4ad4eba5 4166
db6a5d5f 4167 msg[sz] = '\0';
4ad4eba5 4168
db6a5d5f
AM
4169 if (! (_bfd_generic_link_add_one_symbol
4170 (info, abfd, name, BSF_WARNING, s, 0, msg,
4171 FALSE, bed->collect, NULL)))
4172 goto error_return;
4ad4eba5 4173
0e1862bb 4174 if (bfd_link_executable (info))
db6a5d5f
AM
4175 {
4176 /* Clobber the section size so that the warning does
4177 not get copied into the output file. */
4178 s->size = 0;
11d2f718 4179
db6a5d5f
AM
4180 /* Also set SEC_EXCLUDE, so that symbols defined in
4181 the warning section don't get copied to the output. */
4182 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4183 }
4184 }
4185 }
4186
29a9f53e
L
4187 just_syms = ((s = abfd->sections) != NULL
4188 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4189
4ad4eba5
AM
4190 add_needed = TRUE;
4191 if (! dynamic)
4192 {
4193 /* If we are creating a shared library, create all the dynamic
4194 sections immediately. We need to attach them to something,
4195 so we attach them to this BFD, provided it is the right
bf89386a
L
4196 format and is not from ld --just-symbols. Always create the
4197 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4198 are no input BFD's of the same format as the output, we can't
4199 make a shared library. */
4200 if (!just_syms
bf89386a 4201 && (bfd_link_pic (info)
9c1d7a08 4202 || (!bfd_link_relocatable (info)
3c5fce9b 4203 && info->nointerp
9c1d7a08 4204 && (info->export_dynamic || info->dynamic)))
66eb6687 4205 && is_elf_hash_table (htab)
f13a99db 4206 && info->output_bfd->xvec == abfd->xvec
66eb6687 4207 && !htab->dynamic_sections_created)
4ad4eba5
AM
4208 {
4209 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4210 goto error_return;
4211 }
4212 }
66eb6687 4213 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4214 goto error_return;
4215 else
4216 {
4ad4eba5 4217 const char *soname = NULL;
7ee314fa 4218 char *audit = NULL;
4ad4eba5 4219 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4220 const Elf_Internal_Phdr *phdr;
e310298c 4221 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4222
4223 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4224 ld shouldn't allow it. */
29a9f53e 4225 if (just_syms)
92fd189d 4226 abort ();
4ad4eba5
AM
4227
4228 /* If this dynamic lib was specified on the command line with
4229 --as-needed in effect, then we don't want to add a DT_NEEDED
4230 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4231 in by another lib's DT_NEEDED. When --no-add-needed is used
4232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4233 any dynamic library in DT_NEEDED tags in the dynamic lib at
4234 all. */
4235 add_needed = (elf_dyn_lib_class (abfd)
4236 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4237 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4238
4239 s = bfd_get_section_by_name (abfd, ".dynamic");
4240 if (s != NULL)
4241 {
4242 bfd_byte *dynbuf;
4243 bfd_byte *extdyn;
cb33740c 4244 unsigned int elfsec;
4ad4eba5
AM
4245 unsigned long shlink;
4246
eea6121a 4247 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4248 {
dc1e8a47 4249 error_free_dyn:
f8703194
L
4250 free (dynbuf);
4251 goto error_return;
4252 }
4ad4eba5
AM
4253
4254 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4255 if (elfsec == SHN_BAD)
4ad4eba5
AM
4256 goto error_free_dyn;
4257 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4258
4259 for (extdyn = dynbuf;
9bff840e 4260 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4261 extdyn += bed->s->sizeof_dyn)
4262 {
4263 Elf_Internal_Dyn dyn;
4264
4265 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4266 if (dyn.d_tag == DT_SONAME)
4267 {
4268 unsigned int tagv = dyn.d_un.d_val;
4269 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4270 if (soname == NULL)
4271 goto error_free_dyn;
4272 }
4273 if (dyn.d_tag == DT_NEEDED)
4274 {
4275 struct bfd_link_needed_list *n, **pn;
4276 char *fnm, *anm;
4277 unsigned int tagv = dyn.d_un.d_val;
986f0783 4278 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4279
a50b1753 4280 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4281 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4282 if (n == NULL || fnm == NULL)
4283 goto error_free_dyn;
4284 amt = strlen (fnm) + 1;
a50b1753 4285 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4286 if (anm == NULL)
4287 goto error_free_dyn;
4288 memcpy (anm, fnm, amt);
4289 n->name = anm;
4290 n->by = abfd;
4291 n->next = NULL;
66eb6687 4292 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4293 ;
4294 *pn = n;
4295 }
4296 if (dyn.d_tag == DT_RUNPATH)
4297 {
4298 struct bfd_link_needed_list *n, **pn;
4299 char *fnm, *anm;
4300 unsigned int tagv = dyn.d_un.d_val;
986f0783 4301 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4302
a50b1753 4303 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4304 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4305 if (n == NULL || fnm == NULL)
4306 goto error_free_dyn;
4307 amt = strlen (fnm) + 1;
a50b1753 4308 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4309 if (anm == NULL)
4310 goto error_free_dyn;
4311 memcpy (anm, fnm, amt);
4312 n->name = anm;
4313 n->by = abfd;
4314 n->next = NULL;
4315 for (pn = & runpath;
4316 *pn != NULL;
4317 pn = &(*pn)->next)
4318 ;
4319 *pn = n;
4320 }
4321 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4322 if (!runpath && dyn.d_tag == DT_RPATH)
4323 {
4324 struct bfd_link_needed_list *n, **pn;
4325 char *fnm, *anm;
4326 unsigned int tagv = dyn.d_un.d_val;
986f0783 4327 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4328
a50b1753 4329 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4330 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4331 if (n == NULL || fnm == NULL)
4332 goto error_free_dyn;
4333 amt = strlen (fnm) + 1;
a50b1753 4334 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4335 if (anm == NULL)
f8703194 4336 goto error_free_dyn;
4ad4eba5
AM
4337 memcpy (anm, fnm, amt);
4338 n->name = anm;
4339 n->by = abfd;
4340 n->next = NULL;
4341 for (pn = & rpath;
4342 *pn != NULL;
4343 pn = &(*pn)->next)
4344 ;
4345 *pn = n;
4346 }
7ee314fa
AM
4347 if (dyn.d_tag == DT_AUDIT)
4348 {
4349 unsigned int tagv = dyn.d_un.d_val;
4350 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4351 }
4ad4eba5
AM
4352 }
4353
4354 free (dynbuf);
4355 }
4356
4357 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4358 frees all more recently bfd_alloc'd blocks as well. */
4359 if (runpath)
4360 rpath = runpath;
4361
4362 if (rpath)
4363 {
4364 struct bfd_link_needed_list **pn;
66eb6687 4365 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4366 ;
4367 *pn = rpath;
4368 }
4369
9acc85a6
AM
4370 /* If we have a PT_GNU_RELRO program header, mark as read-only
4371 all sections contained fully therein. This makes relro
4372 shared library sections appear as they will at run-time. */
4373 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4374 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4375 if (phdr->p_type == PT_GNU_RELRO)
4376 {
4377 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4378 {
4379 unsigned int opb = bfd_octets_per_byte (abfd, s);
4380
4381 if ((s->flags & SEC_ALLOC) != 0
4382 && s->vma * opb >= phdr->p_vaddr
4383 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4384 s->flags |= SEC_READONLY;
4385 }
9acc85a6
AM
4386 break;
4387 }
4388
4ad4eba5
AM
4389 /* We do not want to include any of the sections in a dynamic
4390 object in the output file. We hack by simply clobbering the
4391 list of sections in the BFD. This could be handled more
4392 cleanly by, say, a new section flag; the existing
4393 SEC_NEVER_LOAD flag is not the one we want, because that one
4394 still implies that the section takes up space in the output
4395 file. */
4396 bfd_section_list_clear (abfd);
4397
4ad4eba5
AM
4398 /* Find the name to use in a DT_NEEDED entry that refers to this
4399 object. If the object has a DT_SONAME entry, we use it.
4400 Otherwise, if the generic linker stuck something in
4401 elf_dt_name, we use that. Otherwise, we just use the file
4402 name. */
4403 if (soname == NULL || *soname == '\0')
4404 {
4405 soname = elf_dt_name (abfd);
4406 if (soname == NULL || *soname == '\0')
4407 soname = bfd_get_filename (abfd);
4408 }
4409
4410 /* Save the SONAME because sometimes the linker emulation code
4411 will need to know it. */
4412 elf_dt_name (abfd) = soname;
4413
4ad4eba5
AM
4414 /* If we have already included this dynamic object in the
4415 link, just ignore it. There is no reason to include a
4416 particular dynamic object more than once. */
e310298c
AM
4417 for (loaded_lib = htab->dyn_loaded;
4418 loaded_lib != NULL;
4419 loaded_lib = loaded_lib->next)
4420 {
4421 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4422 return TRUE;
4423 }
4424
4425 /* Create dynamic sections for backends that require that be done
4426 before setup_gnu_properties. */
4427 if (add_needed
4428 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4429 return FALSE;
7ee314fa
AM
4430
4431 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4432 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4433 }
4434
4435 /* If this is a dynamic object, we always link against the .dynsym
4436 symbol table, not the .symtab symbol table. The dynamic linker
4437 will only see the .dynsym symbol table, so there is no reason to
4438 look at .symtab for a dynamic object. */
4439
4440 if (! dynamic || elf_dynsymtab (abfd) == 0)
4441 hdr = &elf_tdata (abfd)->symtab_hdr;
4442 else
4443 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4444
4445 symcount = hdr->sh_size / bed->s->sizeof_sym;
4446
4447 /* The sh_info field of the symtab header tells us where the
4448 external symbols start. We don't care about the local symbols at
4449 this point. */
4450 if (elf_bad_symtab (abfd))
4451 {
4452 extsymcount = symcount;
4453 extsymoff = 0;
4454 }
4455 else
4456 {
4457 extsymcount = symcount - hdr->sh_info;
4458 extsymoff = hdr->sh_info;
4459 }
4460
f45794cb 4461 sym_hash = elf_sym_hashes (abfd);
012b2306 4462 if (extsymcount != 0)
4ad4eba5
AM
4463 {
4464 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4465 NULL, NULL, NULL);
4466 if (isymbuf == NULL)
4467 goto error_return;
4468
4ad4eba5 4469 if (sym_hash == NULL)
012b2306
AM
4470 {
4471 /* We store a pointer to the hash table entry for each
4472 external symbol. */
986f0783 4473 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4474 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4475 if (sym_hash == NULL)
4476 goto error_free_sym;
4477 elf_sym_hashes (abfd) = sym_hash;
4478 }
4ad4eba5
AM
4479 }
4480
4481 if (dynamic)
4482 {
4483 /* Read in any version definitions. */
fc0e6df6
PB
4484 if (!_bfd_elf_slurp_version_tables (abfd,
4485 info->default_imported_symver))
4ad4eba5
AM
4486 goto error_free_sym;
4487
4488 /* Read in the symbol versions, but don't bother to convert them
4489 to internal format. */
4490 if (elf_dynversym (abfd) != 0)
4491 {
986f0783
AM
4492 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4493 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4494
2bb3687b
AM
4495 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4496 goto error_free_sym;
4497 extversym = (Elf_External_Versym *)
4498 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4499 if (extversym == NULL)
4500 goto error_free_sym;
986f0783 4501 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4502 }
4503 }
4504
66eb6687
AM
4505 /* If we are loading an as-needed shared lib, save the symbol table
4506 state before we start adding symbols. If the lib turns out
4507 to be unneeded, restore the state. */
4508 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4509 {
4510 unsigned int i;
4511 size_t entsize;
4512
4513 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4514 {
4515 struct bfd_hash_entry *p;
2de92251 4516 struct elf_link_hash_entry *h;
66eb6687
AM
4517
4518 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4519 {
4520 h = (struct elf_link_hash_entry *) p;
4521 entsize += htab->root.table.entsize;
4522 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4523 {
4524 entsize += htab->root.table.entsize;
4525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4526 }
4527 if (h->root.type == bfd_link_hash_common)
4528 entsize += sizeof (*h->root.u.c.p);
2de92251 4529 }
66eb6687
AM
4530 }
4531
4532 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4533 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4534 if (old_tab == NULL)
4535 goto error_free_vers;
4536
4537 /* Remember the current objalloc pointer, so that all mem for
4538 symbols added can later be reclaimed. */
4539 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4540 if (alloc_mark == NULL)
4541 goto error_free_vers;
4542
5061a885
AM
4543 /* Make a special call to the linker "notice" function to
4544 tell it that we are about to handle an as-needed lib. */
e5034e59 4545 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4546 goto error_free_vers;
5061a885 4547
f45794cb
AM
4548 /* Clone the symbol table. Remember some pointers into the
4549 symbol table, and dynamic symbol count. */
4550 old_ent = (char *) old_tab + tabsize;
66eb6687 4551 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4552 old_undefs = htab->root.undefs;
4553 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4554 old_table = htab->root.table.table;
4555 old_size = htab->root.table.size;
4556 old_count = htab->root.table.count;
e310298c
AM
4557 old_strtab = NULL;
4558 if (htab->dynstr != NULL)
4559 {
4560 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4561 if (old_strtab == NULL)
4562 goto error_free_vers;
4563 }
66eb6687
AM
4564
4565 for (i = 0; i < htab->root.table.size; i++)
4566 {
4567 struct bfd_hash_entry *p;
2de92251 4568 struct elf_link_hash_entry *h;
66eb6687
AM
4569
4570 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4571 {
2de92251 4572 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4573 memcpy (old_ent, h, htab->root.table.entsize);
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4575 if (h->root.type == bfd_link_hash_warning)
4576 {
7ba11550
AM
4577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4578 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4579 old_ent = (char *) old_ent + htab->root.table.entsize;
4580 }
7ba11550
AM
4581 if (h->root.type == bfd_link_hash_common)
4582 {
4583 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4584 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4585 }
66eb6687
AM
4586 }
4587 }
4588 }
4ad4eba5 4589
66eb6687 4590 weaks = NULL;
be22c732
NC
4591 if (extversym == NULL)
4592 ever = NULL;
4593 else if (extversym + extsymoff < extversym_end)
4594 ever = extversym + extsymoff;
4595 else
4596 {
4597 /* xgettext:c-format */
4598 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4599 abfd, (long) extsymoff,
4600 (long) (extversym_end - extversym) / sizeof (* extversym));
4601 bfd_set_error (bfd_error_bad_value);
4602 goto error_free_vers;
4603 }
4604
b4c555cf
ML
4605 if (!bfd_link_relocatable (info)
4606 && abfd->lto_slim_object)
cc5277b1
ML
4607 {
4608 _bfd_error_handler
4609 (_("%pB: plugin needed to handle lto object"), abfd);
4610 }
4611
4ad4eba5
AM
4612 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4613 isym < isymend;
4614 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4615 {
4616 int bind;
4617 bfd_vma value;
af44c138 4618 asection *sec, *new_sec;
4ad4eba5
AM
4619 flagword flags;
4620 const char *name;
4621 struct elf_link_hash_entry *h;
90c984fc 4622 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4623 bfd_boolean definition;
4624 bfd_boolean size_change_ok;
4625 bfd_boolean type_change_ok;
37a9e49a
L
4626 bfd_boolean new_weak;
4627 bfd_boolean old_weak;
7ba11550 4628 bfd *override;
a4d8e49b 4629 bfd_boolean common;
97196564 4630 bfd_boolean discarded;
4ad4eba5 4631 unsigned int old_alignment;
4538d1c7 4632 unsigned int shindex;
4ad4eba5 4633 bfd *old_bfd;
6e33951e 4634 bfd_boolean matched;
4ad4eba5 4635
7ba11550 4636 override = NULL;
4ad4eba5
AM
4637
4638 flags = BSF_NO_FLAGS;
4639 sec = NULL;
4640 value = isym->st_value;
a4d8e49b 4641 common = bed->common_definition (isym);
2980ccad
L
4642 if (common && info->inhibit_common_definition)
4643 {
4644 /* Treat common symbol as undefined for --no-define-common. */
4645 isym->st_shndx = SHN_UNDEF;
4646 common = FALSE;
4647 }
97196564 4648 discarded = FALSE;
4ad4eba5
AM
4649
4650 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4651 switch (bind)
4ad4eba5 4652 {
3e7a7d11 4653 case STB_LOCAL:
4ad4eba5
AM
4654 /* This should be impossible, since ELF requires that all
4655 global symbols follow all local symbols, and that sh_info
4656 point to the first global symbol. Unfortunately, Irix 5
4657 screws this up. */
fe3fef62
AM
4658 if (elf_bad_symtab (abfd))
4659 continue;
4660
4661 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4662 then we'll likely segfault on a NULL symbol hash if the
4663 symbol is ever referenced in relocations. */
4664 shindex = elf_elfheader (abfd)->e_shstrndx;
4665 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4666 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4667 " (>= sh_info of %lu)"),
4668 abfd, name, (long) (isym - isymbuf + extsymoff),
4669 (long) extsymoff);
4670
4671 /* Dynamic object relocations are not processed by ld, so
4672 ld won't run into the problem mentioned above. */
4673 if (dynamic)
4674 continue;
fe3fef62
AM
4675 bfd_set_error (bfd_error_bad_value);
4676 goto error_free_vers;
3e7a7d11
NC
4677
4678 case STB_GLOBAL:
a4d8e49b 4679 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4680 flags = BSF_GLOBAL;
3e7a7d11
NC
4681 break;
4682
4683 case STB_WEAK:
4684 flags = BSF_WEAK;
4685 break;
4686
4687 case STB_GNU_UNIQUE:
4688 flags = BSF_GNU_UNIQUE;
4689 break;
4690
4691 default:
4ad4eba5 4692 /* Leave it up to the processor backend. */
3e7a7d11 4693 break;
4ad4eba5
AM
4694 }
4695
4696 if (isym->st_shndx == SHN_UNDEF)
4697 sec = bfd_und_section_ptr;
cb33740c
AM
4698 else if (isym->st_shndx == SHN_ABS)
4699 sec = bfd_abs_section_ptr;
4700 else if (isym->st_shndx == SHN_COMMON)
4701 {
4702 sec = bfd_com_section_ptr;
4703 /* What ELF calls the size we call the value. What ELF
4704 calls the value we call the alignment. */
4705 value = isym->st_size;
4706 }
4707 else
4ad4eba5
AM
4708 {
4709 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4710 if (sec == NULL)
4711 sec = bfd_abs_section_ptr;
dbaa2011 4712 else if (discarded_section (sec))
529fcb95 4713 {
e5d08002
L
4714 /* Symbols from discarded section are undefined. We keep
4715 its visibility. */
529fcb95 4716 sec = bfd_und_section_ptr;
97196564 4717 discarded = TRUE;
529fcb95
PB
4718 isym->st_shndx = SHN_UNDEF;
4719 }
4ad4eba5
AM
4720 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4721 value -= sec->vma;
4722 }
4ad4eba5
AM
4723
4724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4725 isym->st_name);
4726 if (name == NULL)
4727 goto error_free_vers;
4728
4729 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4730 && (abfd->flags & BFD_PLUGIN) != 0)
4731 {
4732 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4733
4734 if (xc == NULL)
4735 {
4736 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4737 | SEC_EXCLUDE);
4738 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4739 if (xc == NULL)
4740 goto error_free_vers;
4741 }
4742 sec = xc;
4743 }
4744 else if (isym->st_shndx == SHN_COMMON
4745 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4746 && !bfd_link_relocatable (info))
4ad4eba5
AM
4747 {
4748 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4749
4750 if (tcomm == NULL)
4751 {
02d00247
AM
4752 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4753 | SEC_LINKER_CREATED);
4754 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4755 if (tcomm == NULL)
4ad4eba5
AM
4756 goto error_free_vers;
4757 }
4758 sec = tcomm;
4759 }
66eb6687 4760 else if (bed->elf_add_symbol_hook)
4ad4eba5 4761 {
66eb6687
AM
4762 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4763 &sec, &value))
4ad4eba5
AM
4764 goto error_free_vers;
4765
4766 /* The hook function sets the name to NULL if this symbol
4767 should be skipped for some reason. */
4768 if (name == NULL)
4769 continue;
4770 }
4771
4772 /* Sanity check that all possibilities were handled. */
4773 if (sec == NULL)
4538d1c7 4774 abort ();
4ad4eba5 4775
191c0c42
AM
4776 /* Silently discard TLS symbols from --just-syms. There's
4777 no way to combine a static TLS block with a new TLS block
4778 for this executable. */
4779 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4780 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4781 continue;
4782
4ad4eba5
AM
4783 if (bfd_is_und_section (sec)
4784 || bfd_is_com_section (sec))
4785 definition = FALSE;
4786 else
4787 definition = TRUE;
4788
4789 size_change_ok = FALSE;
66eb6687 4790 type_change_ok = bed->type_change_ok;
37a9e49a 4791 old_weak = FALSE;
6e33951e 4792 matched = FALSE;
4ad4eba5
AM
4793 old_alignment = 0;
4794 old_bfd = NULL;
af44c138 4795 new_sec = sec;
4ad4eba5 4796
66eb6687 4797 if (is_elf_hash_table (htab))
4ad4eba5
AM
4798 {
4799 Elf_Internal_Versym iver;
4800 unsigned int vernum = 0;
4801 bfd_boolean skip;
4802
fc0e6df6 4803 if (ever == NULL)
4ad4eba5 4804 {
fc0e6df6
PB
4805 if (info->default_imported_symver)
4806 /* Use the default symbol version created earlier. */
4807 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4808 else
4809 iver.vs_vers = 0;
4810 }
be22c732
NC
4811 else if (ever >= extversym_end)
4812 {
4813 /* xgettext:c-format */
4814 _bfd_error_handler (_("%pB: not enough version information"),
4815 abfd);
4816 bfd_set_error (bfd_error_bad_value);
4817 goto error_free_vers;
4818 }
fc0e6df6
PB
4819 else
4820 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4821
4822 vernum = iver.vs_vers & VERSYM_VERSION;
4823
4824 /* If this is a hidden symbol, or if it is not version
4825 1, we append the version name to the symbol name.
cc86ff91
EB
4826 However, we do not modify a non-hidden absolute symbol
4827 if it is not a function, because it might be the version
4828 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4829 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4830 || (vernum > 1
4831 && (!bfd_is_abs_section (sec)
4832 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4833 {
4834 const char *verstr;
4835 size_t namelen, verlen, newlen;
4836 char *newname, *p;
4837
4838 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4839 {
fc0e6df6
PB
4840 if (vernum > elf_tdata (abfd)->cverdefs)
4841 verstr = NULL;
4842 else if (vernum > 1)
4843 verstr =
4844 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4845 else
4846 verstr = "";
4ad4eba5 4847
fc0e6df6 4848 if (verstr == NULL)
4ad4eba5 4849 {
4eca0228 4850 _bfd_error_handler
695344c0 4851 /* xgettext:c-format */
871b3ab2 4852 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4853 abfd, name, vernum,
4854 elf_tdata (abfd)->cverdefs);
4855 bfd_set_error (bfd_error_bad_value);
4856 goto error_free_vers;
4ad4eba5 4857 }
fc0e6df6
PB
4858 }
4859 else
4860 {
4861 /* We cannot simply test for the number of
4862 entries in the VERNEED section since the
4863 numbers for the needed versions do not start
4864 at 0. */
4865 Elf_Internal_Verneed *t;
4866
4867 verstr = NULL;
4868 for (t = elf_tdata (abfd)->verref;
4869 t != NULL;
4870 t = t->vn_nextref)
4ad4eba5 4871 {
fc0e6df6 4872 Elf_Internal_Vernaux *a;
4ad4eba5 4873
fc0e6df6
PB
4874 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4875 {
4876 if (a->vna_other == vernum)
4ad4eba5 4877 {
fc0e6df6
PB
4878 verstr = a->vna_nodename;
4879 break;
4ad4eba5 4880 }
4ad4eba5 4881 }
fc0e6df6
PB
4882 if (a != NULL)
4883 break;
4884 }
4885 if (verstr == NULL)
4886 {
4eca0228 4887 _bfd_error_handler
695344c0 4888 /* xgettext:c-format */
871b3ab2 4889 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4890 abfd, name, vernum);
4891 bfd_set_error (bfd_error_bad_value);
4892 goto error_free_vers;
4ad4eba5 4893 }
4ad4eba5 4894 }
fc0e6df6
PB
4895
4896 namelen = strlen (name);
4897 verlen = strlen (verstr);
4898 newlen = namelen + verlen + 2;
4899 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4900 && isym->st_shndx != SHN_UNDEF)
4901 ++newlen;
4902
a50b1753 4903 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4904 if (newname == NULL)
4905 goto error_free_vers;
4906 memcpy (newname, name, namelen);
4907 p = newname + namelen;
4908 *p++ = ELF_VER_CHR;
4909 /* If this is a defined non-hidden version symbol,
4910 we add another @ to the name. This indicates the
4911 default version of the symbol. */
4912 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4913 && isym->st_shndx != SHN_UNDEF)
4914 *p++ = ELF_VER_CHR;
4915 memcpy (p, verstr, verlen + 1);
4916
4917 name = newname;
4ad4eba5
AM
4918 }
4919
cd3416da
AM
4920 /* If this symbol has default visibility and the user has
4921 requested we not re-export it, then mark it as hidden. */
a0d49154 4922 if (!bfd_is_und_section (sec)
cd3416da 4923 && !dynamic
ce875075 4924 && abfd->no_export
cd3416da
AM
4925 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4926 isym->st_other = (STV_HIDDEN
4927 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4928
4f3fedcf
AM
4929 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4930 sym_hash, &old_bfd, &old_weak,
4931 &old_alignment, &skip, &override,
6e33951e
L
4932 &type_change_ok, &size_change_ok,
4933 &matched))
4ad4eba5
AM
4934 goto error_free_vers;
4935
4936 if (skip)
4937 continue;
4938
6e33951e
L
4939 /* Override a definition only if the new symbol matches the
4940 existing one. */
4941 if (override && matched)
4ad4eba5
AM
4942 definition = FALSE;
4943
4944 h = *sym_hash;
4945 while (h->root.type == bfd_link_hash_indirect
4946 || h->root.type == bfd_link_hash_warning)
4947 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4948
4ad4eba5 4949 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4950 && vernum > 1
4951 && definition)
4952 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4953 }
4954
4955 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4956 (info, override ? override : abfd, name, flags, sec, value,
4957 NULL, FALSE, bed->collect,
4ad4eba5
AM
4958 (struct bfd_link_hash_entry **) sym_hash)))
4959 goto error_free_vers;
4960
4961 h = *sym_hash;
90c984fc
L
4962 /* We need to make sure that indirect symbol dynamic flags are
4963 updated. */
4964 hi = h;
4ad4eba5
AM
4965 while (h->root.type == bfd_link_hash_indirect
4966 || h->root.type == bfd_link_hash_warning)
4967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4968
97196564
L
4969 /* Setting the index to -3 tells elf_link_output_extsym that
4970 this symbol is defined in a discarded section. */
4971 if (discarded)
4972 h->indx = -3;
4973
4ad4eba5
AM
4974 *sym_hash = h;
4975
37a9e49a 4976 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4977 if (dynamic
4978 && definition
37a9e49a 4979 && new_weak
fcb93ecf 4980 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4981 && is_elf_hash_table (htab)
60d67dc8 4982 && h->u.alias == NULL)
4ad4eba5
AM
4983 {
4984 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4985 a dynamic object, using the alias field. Later in this
4986 function we will set the alias field to the correct
4ad4eba5
AM
4987 value. We only put non-function symbols from dynamic
4988 objects on this list, because that happens to be the only
4989 time we need to know the normal symbol corresponding to a
4990 weak symbol, and the information is time consuming to
60d67dc8 4991 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4992 then this symbol was already defined by some previous
4993 dynamic object, and we will be using that previous
4994 definition anyhow. */
4995
60d67dc8 4996 h->u.alias = weaks;
4ad4eba5 4997 weaks = h;
4ad4eba5
AM
4998 }
4999
5000 /* Set the alignment of a common symbol. */
a4d8e49b 5001 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5002 && h->root.type == bfd_link_hash_common)
5003 {
5004 unsigned int align;
5005
a4d8e49b 5006 if (common)
af44c138
L
5007 align = bfd_log2 (isym->st_value);
5008 else
5009 {
5010 /* The new symbol is a common symbol in a shared object.
5011 We need to get the alignment from the section. */
5012 align = new_sec->alignment_power;
5013 }
595213d4 5014 if (align > old_alignment)
4ad4eba5
AM
5015 h->root.u.c.p->alignment_power = align;
5016 else
5017 h->root.u.c.p->alignment_power = old_alignment;
5018 }
5019
66eb6687 5020 if (is_elf_hash_table (htab))
4ad4eba5 5021 {
4f3fedcf
AM
5022 /* Set a flag in the hash table entry indicating the type of
5023 reference or definition we just found. A dynamic symbol
5024 is one which is referenced or defined by both a regular
5025 object and a shared object. */
5026 bfd_boolean dynsym = FALSE;
5027
b1a92c63
AM
5028 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5029 if ((abfd->flags & BFD_PLUGIN) != 0)
5030 ;
5031 else if (!dynamic)
4f3fedcf
AM
5032 {
5033 if (! definition)
5034 {
5035 h->ref_regular = 1;
5036 if (bind != STB_WEAK)
5037 h->ref_regular_nonweak = 1;
5038 }
5039 else
5040 {
5041 h->def_regular = 1;
5042 if (h->def_dynamic)
5043 {
5044 h->def_dynamic = 0;
5045 h->ref_dynamic = 1;
5046 }
5047 }
4f3fedcf
AM
5048 }
5049 else
5050 {
5051 if (! definition)
5052 {
5053 h->ref_dynamic = 1;
5054 hi->ref_dynamic = 1;
5055 }
5056 else
5057 {
5058 h->def_dynamic = 1;
5059 hi->def_dynamic = 1;
5060 }
b1a92c63 5061 }
4f3fedcf 5062
b1a92c63
AM
5063 /* If an indirect symbol has been forced local, don't
5064 make the real symbol dynamic. */
5065 if (h != hi && hi->forced_local)
5066 ;
5067 else if (!dynamic)
5068 {
5069 if (bfd_link_dll (info)
5070 || h->def_dynamic
5071 || h->ref_dynamic)
5072 dynsym = TRUE;
5073 }
5074 else
5075 {
5076 if (h->def_regular
5077 || h->ref_regular
5078 || (h->is_weakalias
5079 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5080 dynsym = TRUE;
5081 }
5082
5083 /* Check to see if we need to add an indirect symbol for
5084 the default name. */
726d7d1e
AM
5085 if ((definition
5086 || (!override && h->root.type == bfd_link_hash_common))
5087 && !(hi != h
5088 && hi->versioned == versioned_hidden))
4f3fedcf
AM
5089 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5090 sec, value, &old_bfd, &dynsym))
5091 goto error_free_vers;
4ad4eba5
AM
5092
5093 /* Check the alignment when a common symbol is involved. This
5094 can change when a common symbol is overridden by a normal
5095 definition or a common symbol is ignored due to the old
5096 normal definition. We need to make sure the maximum
5097 alignment is maintained. */
a4d8e49b 5098 if ((old_alignment || common)
4ad4eba5
AM
5099 && h->root.type != bfd_link_hash_common)
5100 {
5101 unsigned int common_align;
5102 unsigned int normal_align;
5103 unsigned int symbol_align;
5104 bfd *normal_bfd;
5105 bfd *common_bfd;
5106
3a81e825
AM
5107 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5108 || h->root.type == bfd_link_hash_defweak);
5109
4ad4eba5
AM
5110 symbol_align = ffs (h->root.u.def.value) - 1;
5111 if (h->root.u.def.section->owner != NULL
0616a280
AM
5112 && (h->root.u.def.section->owner->flags
5113 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5114 {
5115 normal_align = h->root.u.def.section->alignment_power;
5116 if (normal_align > symbol_align)
5117 normal_align = symbol_align;
5118 }
5119 else
5120 normal_align = symbol_align;
5121
5122 if (old_alignment)
5123 {
5124 common_align = old_alignment;
5125 common_bfd = old_bfd;
5126 normal_bfd = abfd;
5127 }
5128 else
5129 {
5130 common_align = bfd_log2 (isym->st_value);
5131 common_bfd = abfd;
5132 normal_bfd = old_bfd;
5133 }
5134
5135 if (normal_align < common_align)
d07676f8
NC
5136 {
5137 /* PR binutils/2735 */
5138 if (normal_bfd == NULL)
4eca0228 5139 _bfd_error_handler
695344c0 5140 /* xgettext:c-format */
9793eb77 5141 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5142 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5143 1 << common_align, name, common_bfd,
5144 1 << normal_align, h->root.u.def.section);
d07676f8 5145 else
4eca0228 5146 _bfd_error_handler
695344c0 5147 /* xgettext:c-format */
9793eb77 5148 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5149 " is smaller than %u in %pB"),
c08bb8dd
AM
5150 1 << normal_align, name, normal_bfd,
5151 1 << common_align, common_bfd);
d07676f8 5152 }
4ad4eba5
AM
5153 }
5154
83ad0046 5155 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5156 if (isym->st_size != 0
5157 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5158 && (definition || h->size == 0))
5159 {
83ad0046
L
5160 if (h->size != 0
5161 && h->size != isym->st_size
5162 && ! size_change_ok)
4eca0228 5163 _bfd_error_handler
695344c0 5164 /* xgettext:c-format */
9793eb77 5165 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5166 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5167 name, (uint64_t) h->size, old_bfd,
5168 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5169
5170 h->size = isym->st_size;
5171 }
5172
5173 /* If this is a common symbol, then we always want H->SIZE
5174 to be the size of the common symbol. The code just above
5175 won't fix the size if a common symbol becomes larger. We
5176 don't warn about a size change here, because that is
4f3fedcf 5177 covered by --warn-common. Allow changes between different
fcb93ecf 5178 function types. */
4ad4eba5
AM
5179 if (h->root.type == bfd_link_hash_common)
5180 h->size = h->root.u.c.size;
5181
5182 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5183 && ((definition && !new_weak)
5184 || (old_weak && h->root.type == bfd_link_hash_common)
5185 || h->type == STT_NOTYPE))
4ad4eba5 5186 {
2955ec4c
L
5187 unsigned int type = ELF_ST_TYPE (isym->st_info);
5188
5189 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5190 symbol. */
5191 if (type == STT_GNU_IFUNC
5192 && (abfd->flags & DYNAMIC) != 0)
5193 type = STT_FUNC;
4ad4eba5 5194
2955ec4c
L
5195 if (h->type != type)
5196 {
5197 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5198 /* xgettext:c-format */
4eca0228 5199 _bfd_error_handler
9793eb77 5200 (_("warning: type of symbol `%s' changed"
871b3ab2 5201 " from %d to %d in %pB"),
c08bb8dd 5202 name, h->type, type, abfd);
2955ec4c
L
5203
5204 h->type = type;
5205 }
4ad4eba5
AM
5206 }
5207
54ac0771 5208 /* Merge st_other field. */
5160d0f3
AM
5209 elf_merge_st_other (abfd, h, isym->st_other, sec,
5210 definition, dynamic);
4ad4eba5 5211
c3df8c14 5212 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5213 if (definition
5214 && (sec->flags & SEC_DEBUGGING)
5215 && !bfd_link_relocatable (info))
c3df8c14
AM
5216 dynsym = FALSE;
5217
b1a92c63
AM
5218 /* Nor should we make plugin symbols dynamic. */
5219 if ((abfd->flags & BFD_PLUGIN) != 0)
5220 dynsym = FALSE;
5221
35fc36a8 5222 if (definition)
35399224
L
5223 {
5224 h->target_internal = isym->st_target_internal;
5225 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5226 }
35fc36a8 5227
4ad4eba5
AM
5228 if (definition && !dynamic)
5229 {
5230 char *p = strchr (name, ELF_VER_CHR);
5231 if (p != NULL && p[1] != ELF_VER_CHR)
5232 {
5233 /* Queue non-default versions so that .symver x, x@FOO
5234 aliases can be checked. */
66eb6687 5235 if (!nondeflt_vers)
4ad4eba5 5236 {
986f0783
AM
5237 size_t amt = ((isymend - isym + 1)
5238 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5239 nondeflt_vers
5240 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5241 if (!nondeflt_vers)
5242 goto error_free_vers;
4ad4eba5 5243 }
66eb6687 5244 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5245 }
5246 }
5247
b1a92c63 5248 if (dynsym && h->dynindx == -1)
4ad4eba5 5249 {
c152c796 5250 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5251 goto error_free_vers;
60d67dc8
AM
5252 if (h->is_weakalias
5253 && weakdef (h)->dynindx == -1)
4ad4eba5 5254 {
60d67dc8 5255 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5256 goto error_free_vers;
5257 }
5258 }
1f599d0e 5259 else if (h->dynindx != -1)
4ad4eba5
AM
5260 /* If the symbol already has a dynamic index, but
5261 visibility says it should not be visible, turn it into
5262 a local symbol. */
5263 switch (ELF_ST_VISIBILITY (h->other))
5264 {
5265 case STV_INTERNAL:
5266 case STV_HIDDEN:
5267 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5268 dynsym = FALSE;
5269 break;
5270 }
5271
5272 if (!add_needed
aef28989 5273 && matched
4ad4eba5 5274 && definition
010e5ae2 5275 && ((dynsym
a896df97 5276 && h->ref_regular_nonweak)
b1a92c63
AM
5277 || (old_bfd != NULL
5278 && (old_bfd->flags & BFD_PLUGIN) != 0
5279 && bind != STB_WEAK)
ffa9430d 5280 || (h->ref_dynamic_nonweak
010e5ae2 5281 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5282 && !on_needed_list (elf_dt_name (abfd),
5283 htab->needed, NULL))))
4ad4eba5 5284 {
4ad4eba5
AM
5285 const char *soname = elf_dt_name (abfd);
5286
16e4ecc0
AM
5287 info->callbacks->minfo ("%!", soname, old_bfd,
5288 h->root.root.string);
5289
4ad4eba5
AM
5290 /* A symbol from a library loaded via DT_NEEDED of some
5291 other library is referenced by a regular object.
e56f61be 5292 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5293 --no-add-needed is used and the reference was not
5294 a weak one. */
4f3fedcf 5295 if (old_bfd != NULL
b918acf9 5296 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5297 {
4eca0228 5298 _bfd_error_handler
695344c0 5299 /* xgettext:c-format */
871b3ab2 5300 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5301 old_bfd, name);
ff5ac77b 5302 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5303 goto error_free_vers;
5304 }
5305
a50b1753 5306 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5307 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5308
e310298c
AM
5309 /* Create dynamic sections for backends that require
5310 that be done before setup_gnu_properties. */
5311 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5312 return FALSE;
4ad4eba5 5313 add_needed = TRUE;
4ad4eba5
AM
5314 }
5315 }
5316 }
5317
a83ef4d1
L
5318 if (info->lto_plugin_active
5319 && !bfd_link_relocatable (info)
5320 && (abfd->flags & BFD_PLUGIN) == 0
5321 && !just_syms
5322 && extsymcount)
5323 {
5324 int r_sym_shift;
5325
5326 if (bed->s->arch_size == 32)
5327 r_sym_shift = 8;
5328 else
5329 r_sym_shift = 32;
5330
5331 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5332 referenced in regular objects so that linker plugin will get
5333 the correct symbol resolution. */
5334
5335 sym_hash = elf_sym_hashes (abfd);
5336 for (s = abfd->sections; s != NULL; s = s->next)
5337 {
5338 Elf_Internal_Rela *internal_relocs;
5339 Elf_Internal_Rela *rel, *relend;
5340
5341 /* Don't check relocations in excluded sections. */
5342 if ((s->flags & SEC_RELOC) == 0
5343 || s->reloc_count == 0
5344 || (s->flags & SEC_EXCLUDE) != 0
5345 || ((info->strip == strip_all
5346 || info->strip == strip_debugger)
5347 && (s->flags & SEC_DEBUGGING) != 0))
5348 continue;
5349
5350 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5351 NULL,
5352 info->keep_memory);
5353 if (internal_relocs == NULL)
5354 goto error_free_vers;
5355
5356 rel = internal_relocs;
5357 relend = rel + s->reloc_count;
5358 for ( ; rel < relend; rel++)
5359 {
5360 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5361 struct elf_link_hash_entry *h;
5362
5363 /* Skip local symbols. */
5364 if (r_symndx < extsymoff)
5365 continue;
5366
5367 h = sym_hash[r_symndx - extsymoff];
5368 if (h != NULL)
5369 h->root.non_ir_ref_regular = 1;
5370 }
5371
5372 if (elf_section_data (s)->relocs != internal_relocs)
5373 free (internal_relocs);
5374 }
5375 }
5376
c9594989
AM
5377 free (extversym);
5378 extversym = NULL;
5379 free (isymbuf);
5380 isymbuf = NULL;
66eb6687
AM
5381
5382 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5383 {
5384 unsigned int i;
5385
5386 /* Restore the symbol table. */
f45794cb
AM
5387 old_ent = (char *) old_tab + tabsize;
5388 memset (elf_sym_hashes (abfd), 0,
5389 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5390 htab->root.table.table = old_table;
5391 htab->root.table.size = old_size;
5392 htab->root.table.count = old_count;
66eb6687 5393 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5394 htab->root.undefs = old_undefs;
5395 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5396 if (htab->dynstr != NULL)
5397 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5398 free (old_strtab);
5399 old_strtab = NULL;
66eb6687
AM
5400 for (i = 0; i < htab->root.table.size; i++)
5401 {
5402 struct bfd_hash_entry *p;
5403 struct elf_link_hash_entry *h;
4070765b 5404 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5405
5406 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5407 {
4070765b 5408 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5409 will be exported when the dynamic lib becomes needed
5410 in the second pass. */
7ba11550
AM
5411 h = (struct elf_link_hash_entry *) p;
5412 if (h->root.type == bfd_link_hash_warning)
5413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5414 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5415
2de92251 5416 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5419 if (h->root.type == bfd_link_hash_warning)
5420 {
a4542f1b 5421 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5422 memcpy (h, old_ent, htab->root.table.entsize);
5423 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5424 }
a4542f1b 5425 if (h->root.type == bfd_link_hash_common)
3e0882af 5426 {
7ba11550
AM
5427 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5428 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5429 }
4070765b 5430 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5431 }
5432 }
5433
5061a885
AM
5434 /* Make a special call to the linker "notice" function to
5435 tell it that symbols added for crefs may need to be removed. */
e5034e59 5436 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5437 goto error_free_vers;
5061a885 5438
66eb6687
AM
5439 free (old_tab);
5440 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5441 alloc_mark);
c9594989 5442 free (nondeflt_vers);
66eb6687
AM
5443 return TRUE;
5444 }
2de92251 5445
66eb6687
AM
5446 if (old_tab != NULL)
5447 {
e5034e59 5448 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5449 goto error_free_vers;
66eb6687
AM
5450 free (old_tab);
5451 old_tab = NULL;
5452 }
5453
c6e8a9a8
L
5454 /* Now that all the symbols from this input file are created, if
5455 not performing a relocatable link, handle .symver foo, foo@BAR
5456 such that any relocs against foo become foo@BAR. */
0e1862bb 5457 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5458 {
ef53be89 5459 size_t cnt, symidx;
4ad4eba5
AM
5460
5461 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5462 {
5463 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5464 char *shortname, *p;
986f0783 5465 size_t amt;
4ad4eba5
AM
5466
5467 p = strchr (h->root.root.string, ELF_VER_CHR);
5468 if (p == NULL
5469 || (h->root.type != bfd_link_hash_defined
5470 && h->root.type != bfd_link_hash_defweak))
5471 continue;
5472
5473 amt = p - h->root.root.string;
a50b1753 5474 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5475 if (!shortname)
5476 goto error_free_vers;
4ad4eba5
AM
5477 memcpy (shortname, h->root.root.string, amt);
5478 shortname[amt] = '\0';
5479
5480 hi = (struct elf_link_hash_entry *)
66eb6687 5481 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5482 FALSE, FALSE, FALSE);
5483 if (hi != NULL
5484 && hi->root.type == h->root.type
5485 && hi->root.u.def.value == h->root.u.def.value
5486 && hi->root.u.def.section == h->root.u.def.section)
5487 {
5488 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5489 hi->root.type = bfd_link_hash_indirect;
5490 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5491 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5492 sym_hash = elf_sym_hashes (abfd);
5493 if (sym_hash)
5494 for (symidx = 0; symidx < extsymcount; ++symidx)
5495 if (sym_hash[symidx] == hi)
5496 {
5497 sym_hash[symidx] = h;
5498 break;
5499 }
5500 }
5501 free (shortname);
5502 }
5503 free (nondeflt_vers);
5504 nondeflt_vers = NULL;
5505 }
5506
60d67dc8 5507 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5508 symbols we found. The only way to do this is to search all the
5509 symbols. Since we only need the information for non functions in
5510 dynamic objects, that's the only time we actually put anything on
5511 the list WEAKS. We need this information so that if a regular
5512 object refers to a symbol defined weakly in a dynamic object, the
5513 real symbol in the dynamic object is also put in the dynamic
5514 symbols; we also must arrange for both symbols to point to the
5515 same memory location. We could handle the general case of symbol
5516 aliasing, but a general symbol alias can only be generated in
5517 assembler code, handling it correctly would be very time
5518 consuming, and other ELF linkers don't handle general aliasing
5519 either. */
5520 if (weaks != NULL)
5521 {
5522 struct elf_link_hash_entry **hpp;
5523 struct elf_link_hash_entry **hppend;
5524 struct elf_link_hash_entry **sorted_sym_hash;
5525 struct elf_link_hash_entry *h;
986f0783 5526 size_t sym_count, amt;
4ad4eba5
AM
5527
5528 /* Since we have to search the whole symbol list for each weak
5529 defined symbol, search time for N weak defined symbols will be
5530 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5531 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5532 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5533 if (sorted_sym_hash == NULL)
5534 goto error_return;
5535 sym_hash = sorted_sym_hash;
5536 hpp = elf_sym_hashes (abfd);
5537 hppend = hpp + extsymcount;
5538 sym_count = 0;
5539 for (; hpp < hppend; hpp++)
5540 {
5541 h = *hpp;
5542 if (h != NULL
5543 && h->root.type == bfd_link_hash_defined
fcb93ecf 5544 && !bed->is_function_type (h->type))
4ad4eba5
AM
5545 {
5546 *sym_hash = h;
5547 sym_hash++;
5548 sym_count++;
5549 }
5550 }
5551
3a3f4bf7 5552 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5553 elf_sort_symbol);
5554
5555 while (weaks != NULL)
5556 {
5557 struct elf_link_hash_entry *hlook;
5558 asection *slook;
5559 bfd_vma vlook;
ed54588d 5560 size_t i, j, idx = 0;
4ad4eba5
AM
5561
5562 hlook = weaks;
60d67dc8
AM
5563 weaks = hlook->u.alias;
5564 hlook->u.alias = NULL;
4ad4eba5 5565
e3e53eed
AM
5566 if (hlook->root.type != bfd_link_hash_defined
5567 && hlook->root.type != bfd_link_hash_defweak)
5568 continue;
5569
4ad4eba5
AM
5570 slook = hlook->root.u.def.section;
5571 vlook = hlook->root.u.def.value;
5572
4ad4eba5
AM
5573 i = 0;
5574 j = sym_count;
14160578 5575 while (i != j)
4ad4eba5
AM
5576 {
5577 bfd_signed_vma vdiff;
5578 idx = (i + j) / 2;
14160578 5579 h = sorted_sym_hash[idx];
4ad4eba5
AM
5580 vdiff = vlook - h->root.u.def.value;
5581 if (vdiff < 0)
5582 j = idx;
5583 else if (vdiff > 0)
5584 i = idx + 1;
5585 else
5586 {
d3435ae8 5587 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5588 if (sdiff < 0)
5589 j = idx;
5590 else if (sdiff > 0)
5591 i = idx + 1;
5592 else
14160578 5593 break;
4ad4eba5
AM
5594 }
5595 }
5596
5597 /* We didn't find a value/section match. */
14160578 5598 if (i == j)
4ad4eba5
AM
5599 continue;
5600
14160578
AM
5601 /* With multiple aliases, or when the weak symbol is already
5602 strongly defined, we have multiple matching symbols and
5603 the binary search above may land on any of them. Step
5604 one past the matching symbol(s). */
5605 while (++idx != j)
5606 {
5607 h = sorted_sym_hash[idx];
5608 if (h->root.u.def.section != slook
5609 || h->root.u.def.value != vlook)
5610 break;
5611 }
5612
5613 /* Now look back over the aliases. Since we sorted by size
5614 as well as value and section, we'll choose the one with
5615 the largest size. */
5616 while (idx-- != i)
4ad4eba5 5617 {
14160578 5618 h = sorted_sym_hash[idx];
4ad4eba5
AM
5619
5620 /* Stop if value or section doesn't match. */
14160578
AM
5621 if (h->root.u.def.section != slook
5622 || h->root.u.def.value != vlook)
4ad4eba5
AM
5623 break;
5624 else if (h != hlook)
5625 {
60d67dc8
AM
5626 struct elf_link_hash_entry *t;
5627
5628 hlook->u.alias = h;
5629 hlook->is_weakalias = 1;
5630 t = h;
5631 if (t->u.alias != NULL)
5632 while (t->u.alias != h)
5633 t = t->u.alias;
5634 t->u.alias = hlook;
4ad4eba5
AM
5635
5636 /* If the weak definition is in the list of dynamic
5637 symbols, make sure the real definition is put
5638 there as well. */
5639 if (hlook->dynindx != -1 && h->dynindx == -1)
5640 {
c152c796 5641 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5642 {
5643 err_free_sym_hash:
5644 free (sorted_sym_hash);
5645 goto error_return;
5646 }
4ad4eba5
AM
5647 }
5648
5649 /* If the real definition is in the list of dynamic
5650 symbols, make sure the weak definition is put
5651 there as well. If we don't do this, then the
5652 dynamic loader might not merge the entries for the
5653 real definition and the weak definition. */
5654 if (h->dynindx != -1 && hlook->dynindx == -1)
5655 {
c152c796 5656 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5657 goto err_free_sym_hash;
4ad4eba5
AM
5658 }
5659 break;
5660 }
5661 }
5662 }
5663
5664 free (sorted_sym_hash);
5665 }
5666
33177bb1
AM
5667 if (bed->check_directives
5668 && !(*bed->check_directives) (abfd, info))
5669 return FALSE;
85fbca6a 5670
4ad4eba5
AM
5671 /* If this is a non-traditional link, try to optimize the handling
5672 of the .stab/.stabstr sections. */
5673 if (! dynamic
5674 && ! info->traditional_format
66eb6687 5675 && is_elf_hash_table (htab)
4ad4eba5
AM
5676 && (info->strip != strip_all && info->strip != strip_debugger))
5677 {
5678 asection *stabstr;
5679
5680 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5681 if (stabstr != NULL)
5682 {
5683 bfd_size_type string_offset = 0;
5684 asection *stab;
5685
5686 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5687 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5688 && (!stab->name[5] ||
5689 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5690 && (stab->flags & SEC_MERGE) == 0
5691 && !bfd_is_abs_section (stab->output_section))
5692 {
5693 struct bfd_elf_section_data *secdata;
5694
5695 secdata = elf_section_data (stab);
66eb6687
AM
5696 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5697 stabstr, &secdata->sec_info,
4ad4eba5
AM
5698 &string_offset))
5699 goto error_return;
5700 if (secdata->sec_info)
dbaa2011 5701 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5702 }
5703 }
5704 }
5705
e310298c 5706 if (dynamic && add_needed)
4ad4eba5
AM
5707 {
5708 /* Add this bfd to the loaded list. */
5709 struct elf_link_loaded_list *n;
5710
ca4be51c 5711 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5712 if (n == NULL)
5713 goto error_return;
5714 n->abfd = abfd;
e310298c
AM
5715 n->next = htab->dyn_loaded;
5716 htab->dyn_loaded = n;
4ad4eba5 5717 }
e310298c
AM
5718 if (dynamic && !add_needed
5719 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5720 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5721
5722 return TRUE;
5723
5724 error_free_vers:
c9594989
AM
5725 free (old_tab);
5726 free (old_strtab);
5727 free (nondeflt_vers);
5728 free (extversym);
4ad4eba5 5729 error_free_sym:
c9594989 5730 free (isymbuf);
4ad4eba5
AM
5731 error_return:
5732 return FALSE;
5733}
5734
8387904d
AM
5735/* Return the linker hash table entry of a symbol that might be
5736 satisfied by an archive symbol. Return -1 on error. */
5737
5738struct elf_link_hash_entry *
5739_bfd_elf_archive_symbol_lookup (bfd *abfd,
5740 struct bfd_link_info *info,
5741 const char *name)
5742{
5743 struct elf_link_hash_entry *h;
5744 char *p, *copy;
5745 size_t len, first;
5746
2a41f396 5747 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5748 if (h != NULL)
5749 return h;
5750
5751 /* If this is a default version (the name contains @@), look up the
5752 symbol again with only one `@' as well as without the version.
5753 The effect is that references to the symbol with and without the
5754 version will be matched by the default symbol in the archive. */
5755
5756 p = strchr (name, ELF_VER_CHR);
5757 if (p == NULL || p[1] != ELF_VER_CHR)
5758 return h;
5759
5760 /* First check with only one `@'. */
5761 len = strlen (name);
a50b1753 5762 copy = (char *) bfd_alloc (abfd, len);
8387904d 5763 if (copy == NULL)
e99955cd 5764 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5765
5766 first = p - name + 1;
5767 memcpy (copy, name, first);
5768 memcpy (copy + first, name + first + 1, len - first);
5769
2a41f396 5770 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5771 if (h == NULL)
5772 {
5773 /* We also need to check references to the symbol without the
5774 version. */
5775 copy[first - 1] = '\0';
5776 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5777 FALSE, FALSE, TRUE);
8387904d
AM
5778 }
5779
5780 bfd_release (abfd, copy);
5781 return h;
5782}
5783
0ad989f9 5784/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5785 don't use _bfd_generic_link_add_archive_symbols because we need to
5786 handle versioned symbols.
0ad989f9
L
5787
5788 Fortunately, ELF archive handling is simpler than that done by
5789 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5790 oddities. In ELF, if we find a symbol in the archive map, and the
5791 symbol is currently undefined, we know that we must pull in that
5792 object file.
5793
5794 Unfortunately, we do have to make multiple passes over the symbol
5795 table until nothing further is resolved. */
5796
4ad4eba5
AM
5797static bfd_boolean
5798elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5799{
5800 symindex c;
13e570f8 5801 unsigned char *included = NULL;
0ad989f9
L
5802 carsym *symdefs;
5803 bfd_boolean loop;
986f0783 5804 size_t amt;
8387904d
AM
5805 const struct elf_backend_data *bed;
5806 struct elf_link_hash_entry * (*archive_symbol_lookup)
5807 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5808
5809 if (! bfd_has_map (abfd))
5810 {
5811 /* An empty archive is a special case. */
5812 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5813 return TRUE;
5814 bfd_set_error (bfd_error_no_armap);
5815 return FALSE;
5816 }
5817
5818 /* Keep track of all symbols we know to be already defined, and all
5819 files we know to be already included. This is to speed up the
5820 second and subsequent passes. */
5821 c = bfd_ardata (abfd)->symdef_count;
5822 if (c == 0)
5823 return TRUE;
986f0783 5824 amt = c * sizeof (*included);
13e570f8
AM
5825 included = (unsigned char *) bfd_zmalloc (amt);
5826 if (included == NULL)
5827 return FALSE;
0ad989f9
L
5828
5829 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5830 bed = get_elf_backend_data (abfd);
5831 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5832
5833 do
5834 {
5835 file_ptr last;
5836 symindex i;
5837 carsym *symdef;
5838 carsym *symdefend;
5839
5840 loop = FALSE;
5841 last = -1;
5842
5843 symdef = symdefs;
5844 symdefend = symdef + c;
5845 for (i = 0; symdef < symdefend; symdef++, i++)
5846 {
5847 struct elf_link_hash_entry *h;
5848 bfd *element;
5849 struct bfd_link_hash_entry *undefs_tail;
5850 symindex mark;
5851
13e570f8 5852 if (included[i])
0ad989f9
L
5853 continue;
5854 if (symdef->file_offset == last)
5855 {
5856 included[i] = TRUE;
5857 continue;
5858 }
5859
8387904d 5860 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5861 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5862 goto error_return;
0ad989f9
L
5863
5864 if (h == NULL)
5865 continue;
5866
75cfe082
AM
5867 if (h->root.type == bfd_link_hash_undefined)
5868 {
5869 /* If the archive element has already been loaded then one
5870 of the symbols defined by that element might have been
5871 made undefined due to being in a discarded section. */
5872 if (h->indx == -3)
5873 continue;
5874 }
5875 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5876 {
5877 /* We currently have a common symbol. The archive map contains
5878 a reference to this symbol, so we may want to include it. We
5879 only want to include it however, if this archive element
5880 contains a definition of the symbol, not just another common
5881 declaration of it.
5882
5883 Unfortunately some archivers (including GNU ar) will put
5884 declarations of common symbols into their archive maps, as
5885 well as real definitions, so we cannot just go by the archive
5886 map alone. Instead we must read in the element's symbol
5887 table and check that to see what kind of symbol definition
5888 this is. */
5889 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5890 continue;
5891 }
75cfe082 5892 else
0ad989f9
L
5893 {
5894 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5895 /* Symbol must be defined. Don't check it again. */
5896 included[i] = TRUE;
0ad989f9
L
5897 continue;
5898 }
5899
5900 /* We need to include this archive member. */
5901 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5902 if (element == NULL)
5903 goto error_return;
5904
5905 if (! bfd_check_format (element, bfd_object))
5906 goto error_return;
5907
0ad989f9
L
5908 undefs_tail = info->hash->undefs_tail;
5909
0e144ba7
AM
5910 if (!(*info->callbacks
5911 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5912 continue;
0e144ba7 5913 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5914 goto error_return;
5915
5916 /* If there are any new undefined symbols, we need to make
5917 another pass through the archive in order to see whether
5918 they can be defined. FIXME: This isn't perfect, because
5919 common symbols wind up on undefs_tail and because an
5920 undefined symbol which is defined later on in this pass
5921 does not require another pass. This isn't a bug, but it
5922 does make the code less efficient than it could be. */
5923 if (undefs_tail != info->hash->undefs_tail)
5924 loop = TRUE;
5925
5926 /* Look backward to mark all symbols from this object file
5927 which we have already seen in this pass. */
5928 mark = i;
5929 do
5930 {
5931 included[mark] = TRUE;
5932 if (mark == 0)
5933 break;
5934 --mark;
5935 }
5936 while (symdefs[mark].file_offset == symdef->file_offset);
5937
5938 /* We mark subsequent symbols from this object file as we go
5939 on through the loop. */
5940 last = symdef->file_offset;
5941 }
5942 }
5943 while (loop);
5944
0ad989f9 5945 free (included);
0ad989f9
L
5946 return TRUE;
5947
5948 error_return:
c9594989 5949 free (included);
0ad989f9
L
5950 return FALSE;
5951}
4ad4eba5
AM
5952
5953/* Given an ELF BFD, add symbols to the global hash table as
5954 appropriate. */
5955
5956bfd_boolean
5957bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5958{
5959 switch (bfd_get_format (abfd))
5960 {
5961 case bfd_object:
5962 return elf_link_add_object_symbols (abfd, info);
5963 case bfd_archive:
5964 return elf_link_add_archive_symbols (abfd, info);
5965 default:
5966 bfd_set_error (bfd_error_wrong_format);
5967 return FALSE;
5968 }
5969}
5a580b3a 5970\f
14b1c01e
AM
5971struct hash_codes_info
5972{
5973 unsigned long *hashcodes;
5974 bfd_boolean error;
5975};
a0c8462f 5976
5a580b3a
AM
5977/* This function will be called though elf_link_hash_traverse to store
5978 all hash value of the exported symbols in an array. */
5979
5980static bfd_boolean
5981elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5982{
a50b1753 5983 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5984 const char *name;
5a580b3a
AM
5985 unsigned long ha;
5986 char *alc = NULL;
5987
5a580b3a
AM
5988 /* Ignore indirect symbols. These are added by the versioning code. */
5989 if (h->dynindx == -1)
5990 return TRUE;
5991
5992 name = h->root.root.string;
422f1182 5993 if (h->versioned >= versioned)
5a580b3a 5994 {
422f1182
L
5995 char *p = strchr (name, ELF_VER_CHR);
5996 if (p != NULL)
14b1c01e 5997 {
422f1182
L
5998 alc = (char *) bfd_malloc (p - name + 1);
5999 if (alc == NULL)
6000 {
6001 inf->error = TRUE;
6002 return FALSE;
6003 }
6004 memcpy (alc, name, p - name);
6005 alc[p - name] = '\0';
6006 name = alc;
14b1c01e 6007 }
5a580b3a
AM
6008 }
6009
6010 /* Compute the hash value. */
6011 ha = bfd_elf_hash (name);
6012
6013 /* Store the found hash value in the array given as the argument. */
14b1c01e 6014 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6015
6016 /* And store it in the struct so that we can put it in the hash table
6017 later. */
f6e332e6 6018 h->u.elf_hash_value = ha;
5a580b3a 6019
c9594989 6020 free (alc);
5a580b3a
AM
6021 return TRUE;
6022}
6023
fdc90cb4
JJ
6024struct collect_gnu_hash_codes
6025{
6026 bfd *output_bfd;
6027 const struct elf_backend_data *bed;
6028 unsigned long int nsyms;
6029 unsigned long int maskbits;
6030 unsigned long int *hashcodes;
6031 unsigned long int *hashval;
6032 unsigned long int *indx;
6033 unsigned long int *counts;
6034 bfd_vma *bitmask;
6035 bfd_byte *contents;
f16a9783 6036 bfd_size_type xlat;
fdc90cb4
JJ
6037 long int min_dynindx;
6038 unsigned long int bucketcount;
6039 unsigned long int symindx;
6040 long int local_indx;
6041 long int shift1, shift2;
6042 unsigned long int mask;
14b1c01e 6043 bfd_boolean error;
fdc90cb4
JJ
6044};
6045
6046/* This function will be called though elf_link_hash_traverse to store
6047 all hash value of the exported symbols in an array. */
6048
6049static bfd_boolean
6050elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6051{
a50b1753 6052 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6053 const char *name;
fdc90cb4
JJ
6054 unsigned long ha;
6055 char *alc = NULL;
6056
fdc90cb4
JJ
6057 /* Ignore indirect symbols. These are added by the versioning code. */
6058 if (h->dynindx == -1)
6059 return TRUE;
6060
6061 /* Ignore also local symbols and undefined symbols. */
6062 if (! (*s->bed->elf_hash_symbol) (h))
6063 return TRUE;
6064
6065 name = h->root.root.string;
422f1182 6066 if (h->versioned >= versioned)
fdc90cb4 6067 {
422f1182
L
6068 char *p = strchr (name, ELF_VER_CHR);
6069 if (p != NULL)
14b1c01e 6070 {
422f1182
L
6071 alc = (char *) bfd_malloc (p - name + 1);
6072 if (alc == NULL)
6073 {
6074 s->error = TRUE;
6075 return FALSE;
6076 }
6077 memcpy (alc, name, p - name);
6078 alc[p - name] = '\0';
6079 name = alc;
14b1c01e 6080 }
fdc90cb4
JJ
6081 }
6082
6083 /* Compute the hash value. */
6084 ha = bfd_elf_gnu_hash (name);
6085
6086 /* Store the found hash value in the array for compute_bucket_count,
6087 and also for .dynsym reordering purposes. */
6088 s->hashcodes[s->nsyms] = ha;
6089 s->hashval[h->dynindx] = ha;
6090 ++s->nsyms;
6091 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6092 s->min_dynindx = h->dynindx;
6093
c9594989 6094 free (alc);
fdc90cb4
JJ
6095 return TRUE;
6096}
6097
6098/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6099 final dynamic symbol renumbering in case of .gnu.hash.
6100 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6101 to the translation table. */
fdc90cb4
JJ
6102
6103static bfd_boolean
f16a9783 6104elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6105{
a50b1753 6106 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6107 unsigned long int bucket;
6108 unsigned long int val;
6109
fdc90cb4
JJ
6110 /* Ignore indirect symbols. */
6111 if (h->dynindx == -1)
6112 return TRUE;
6113
6114 /* Ignore also local symbols and undefined symbols. */
6115 if (! (*s->bed->elf_hash_symbol) (h))
6116 {
6117 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6118 {
6119 if (s->bed->record_xhash_symbol != NULL)
6120 {
6121 (*s->bed->record_xhash_symbol) (h, 0);
6122 s->local_indx++;
6123 }
6124 else
6125 h->dynindx = s->local_indx++;
6126 }
fdc90cb4
JJ
6127 return TRUE;
6128 }
6129
6130 bucket = s->hashval[h->dynindx] % s->bucketcount;
6131 val = (s->hashval[h->dynindx] >> s->shift1)
6132 & ((s->maskbits >> s->shift1) - 1);
6133 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6134 s->bitmask[val]
6135 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6136 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6137 if (s->counts[bucket] == 1)
6138 /* Last element terminates the chain. */
6139 val |= 1;
6140 bfd_put_32 (s->output_bfd, val,
6141 s->contents + (s->indx[bucket] - s->symindx) * 4);
6142 --s->counts[bucket];
f16a9783
MS
6143 if (s->bed->record_xhash_symbol != NULL)
6144 {
6145 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6146
6147 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6148 }
6149 else
6150 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6151 return TRUE;
6152}
6153
6154/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6155
6156bfd_boolean
6157_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6158{
6159 return !(h->forced_local
6160 || h->root.type == bfd_link_hash_undefined
6161 || h->root.type == bfd_link_hash_undefweak
6162 || ((h->root.type == bfd_link_hash_defined
6163 || h->root.type == bfd_link_hash_defweak)
6164 && h->root.u.def.section->output_section == NULL));
6165}
6166
5a580b3a
AM
6167/* Array used to determine the number of hash table buckets to use
6168 based on the number of symbols there are. If there are fewer than
6169 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6170 fewer than 37 we use 17 buckets, and so forth. We never use more
6171 than 32771 buckets. */
6172
6173static const size_t elf_buckets[] =
6174{
6175 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6176 16411, 32771, 0
6177};
6178
6179/* Compute bucket count for hashing table. We do not use a static set
6180 of possible tables sizes anymore. Instead we determine for all
6181 possible reasonable sizes of the table the outcome (i.e., the
6182 number of collisions etc) and choose the best solution. The
6183 weighting functions are not too simple to allow the table to grow
6184 without bounds. Instead one of the weighting factors is the size.
6185 Therefore the result is always a good payoff between few collisions
6186 (= short chain lengths) and table size. */
6187static size_t
b20dd2ce 6188compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6189 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6190 unsigned long int nsyms,
6191 int gnu_hash)
5a580b3a 6192{
5a580b3a 6193 size_t best_size = 0;
5a580b3a 6194 unsigned long int i;
5a580b3a 6195
5a580b3a
AM
6196 /* We have a problem here. The following code to optimize the table
6197 size requires an integer type with more the 32 bits. If
6198 BFD_HOST_U_64_BIT is set we know about such a type. */
6199#ifdef BFD_HOST_U_64_BIT
6200 if (info->optimize)
6201 {
5a580b3a
AM
6202 size_t minsize;
6203 size_t maxsize;
6204 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6205 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6206 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6207 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6208 unsigned long int *counts;
d40f3da9 6209 bfd_size_type amt;
0883b6e0 6210 unsigned int no_improvement_count = 0;
5a580b3a
AM
6211
6212 /* Possible optimization parameters: if we have NSYMS symbols we say
6213 that the hashing table must at least have NSYMS/4 and at most
6214 2*NSYMS buckets. */
6215 minsize = nsyms / 4;
6216 if (minsize == 0)
6217 minsize = 1;
6218 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6219 if (gnu_hash)
6220 {
6221 if (minsize < 2)
6222 minsize = 2;
6223 if ((best_size & 31) == 0)
6224 ++best_size;
6225 }
5a580b3a
AM
6226
6227 /* Create array where we count the collisions in. We must use bfd_malloc
6228 since the size could be large. */
6229 amt = maxsize;
6230 amt *= sizeof (unsigned long int);
a50b1753 6231 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6232 if (counts == NULL)
fdc90cb4 6233 return 0;
5a580b3a
AM
6234
6235 /* Compute the "optimal" size for the hash table. The criteria is a
6236 minimal chain length. The minor criteria is (of course) the size
6237 of the table. */
6238 for (i = minsize; i < maxsize; ++i)
6239 {
6240 /* Walk through the array of hashcodes and count the collisions. */
6241 BFD_HOST_U_64_BIT max;
6242 unsigned long int j;
6243 unsigned long int fact;
6244
fdc90cb4
JJ
6245 if (gnu_hash && (i & 31) == 0)
6246 continue;
6247
5a580b3a
AM
6248 memset (counts, '\0', i * sizeof (unsigned long int));
6249
6250 /* Determine how often each hash bucket is used. */
6251 for (j = 0; j < nsyms; ++j)
6252 ++counts[hashcodes[j] % i];
6253
6254 /* For the weight function we need some information about the
6255 pagesize on the target. This is information need not be 100%
6256 accurate. Since this information is not available (so far) we
6257 define it here to a reasonable default value. If it is crucial
6258 to have a better value some day simply define this value. */
6259# ifndef BFD_TARGET_PAGESIZE
6260# define BFD_TARGET_PAGESIZE (4096)
6261# endif
6262
fdc90cb4
JJ
6263 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6264 and the chains. */
6265 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6266
6267# if 1
6268 /* Variant 1: optimize for short chains. We add the squares
6269 of all the chain lengths (which favors many small chain
6270 over a few long chains). */
6271 for (j = 0; j < i; ++j)
6272 max += counts[j] * counts[j];
6273
6274 /* This adds penalties for the overall size of the table. */
fdc90cb4 6275 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6276 max *= fact * fact;
6277# else
6278 /* Variant 2: Optimize a lot more for small table. Here we
6279 also add squares of the size but we also add penalties for
6280 empty slots (the +1 term). */
6281 for (j = 0; j < i; ++j)
6282 max += (1 + counts[j]) * (1 + counts[j]);
6283
6284 /* The overall size of the table is considered, but not as
6285 strong as in variant 1, where it is squared. */
fdc90cb4 6286 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6287 max *= fact;
6288# endif
6289
6290 /* Compare with current best results. */
6291 if (max < best_chlen)
6292 {
6293 best_chlen = max;
6294 best_size = i;
ca4be51c 6295 no_improvement_count = 0;
5a580b3a 6296 }
0883b6e0
NC
6297 /* PR 11843: Avoid futile long searches for the best bucket size
6298 when there are a large number of symbols. */
6299 else if (++no_improvement_count == 100)
6300 break;
5a580b3a
AM
6301 }
6302
6303 free (counts);
6304 }
6305 else
6306#endif /* defined (BFD_HOST_U_64_BIT) */
6307 {
6308 /* This is the fallback solution if no 64bit type is available or if we
6309 are not supposed to spend much time on optimizations. We select the
6310 bucket count using a fixed set of numbers. */
6311 for (i = 0; elf_buckets[i] != 0; i++)
6312 {
6313 best_size = elf_buckets[i];
fdc90cb4 6314 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6315 break;
6316 }
fdc90cb4
JJ
6317 if (gnu_hash && best_size < 2)
6318 best_size = 2;
5a580b3a
AM
6319 }
6320
5a580b3a
AM
6321 return best_size;
6322}
6323
d0bf826b
AM
6324/* Size any SHT_GROUP section for ld -r. */
6325
6326bfd_boolean
6327_bfd_elf_size_group_sections (struct bfd_link_info *info)
6328{
6329 bfd *ibfd;
57963c05 6330 asection *s;
d0bf826b 6331
c72f2fb2 6332 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6333 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6334 && (s = ibfd->sections) != NULL
6335 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6336 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6337 return FALSE;
6338 return TRUE;
6339}
6340
04c3a755
NS
6341/* Set a default stack segment size. The value in INFO wins. If it
6342 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6343 undefined it is initialized. */
6344
6345bfd_boolean
6346bfd_elf_stack_segment_size (bfd *output_bfd,
6347 struct bfd_link_info *info,
6348 const char *legacy_symbol,
6349 bfd_vma default_size)
6350{
6351 struct elf_link_hash_entry *h = NULL;
6352
6353 /* Look for legacy symbol. */
6354 if (legacy_symbol)
6355 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6356 FALSE, FALSE, FALSE);
6357 if (h && (h->root.type == bfd_link_hash_defined
6358 || h->root.type == bfd_link_hash_defweak)
6359 && h->def_regular
6360 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6361 {
6362 /* The symbol has no type if specified on the command line. */
6363 h->type = STT_OBJECT;
6364 if (info->stacksize)
695344c0 6365 /* xgettext:c-format */
871b3ab2 6366 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6367 output_bfd, legacy_symbol);
04c3a755 6368 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6369 /* xgettext:c-format */
871b3ab2 6370 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6371 output_bfd, legacy_symbol);
04c3a755
NS
6372 else
6373 info->stacksize = h->root.u.def.value;
6374 }
6375
6376 if (!info->stacksize)
6377 /* If the user didn't set a size, or explicitly inhibit the
6378 size, set it now. */
6379 info->stacksize = default_size;
6380
6381 /* Provide the legacy symbol, if it is referenced. */
6382 if (h && (h->root.type == bfd_link_hash_undefined
6383 || h->root.type == bfd_link_hash_undefweak))
6384 {
6385 struct bfd_link_hash_entry *bh = NULL;
6386
6387 if (!(_bfd_generic_link_add_one_symbol
6388 (info, output_bfd, legacy_symbol,
6389 BSF_GLOBAL, bfd_abs_section_ptr,
6390 info->stacksize >= 0 ? info->stacksize : 0,
6391 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6392 return FALSE;
6393
6394 h = (struct elf_link_hash_entry *) bh;
6395 h->def_regular = 1;
6396 h->type = STT_OBJECT;
6397 }
6398
6399 return TRUE;
6400}
6401
b531344c
MR
6402/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6403
6404struct elf_gc_sweep_symbol_info
6405{
6406 struct bfd_link_info *info;
6407 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6408 bfd_boolean);
6409};
6410
6411static bfd_boolean
6412elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6413{
6414 if (!h->mark
6415 && (((h->root.type == bfd_link_hash_defined
6416 || h->root.type == bfd_link_hash_defweak)
6417 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6418 && h->root.u.def.section->gc_mark))
6419 || h->root.type == bfd_link_hash_undefined
6420 || h->root.type == bfd_link_hash_undefweak))
6421 {
6422 struct elf_gc_sweep_symbol_info *inf;
6423
6424 inf = (struct elf_gc_sweep_symbol_info *) data;
6425 (*inf->hide_symbol) (inf->info, h, TRUE);
6426 h->def_regular = 0;
6427 h->ref_regular = 0;
6428 h->ref_regular_nonweak = 0;
6429 }
6430
6431 return TRUE;
6432}
6433
5a580b3a
AM
6434/* Set up the sizes and contents of the ELF dynamic sections. This is
6435 called by the ELF linker emulation before_allocation routine. We
6436 must set the sizes of the sections before the linker sets the
6437 addresses of the various sections. */
6438
6439bfd_boolean
6440bfd_elf_size_dynamic_sections (bfd *output_bfd,
6441 const char *soname,
6442 const char *rpath,
6443 const char *filter_shlib,
7ee314fa
AM
6444 const char *audit,
6445 const char *depaudit,
5a580b3a
AM
6446 const char * const *auxiliary_filters,
6447 struct bfd_link_info *info,
fd91d419 6448 asection **sinterpptr)
5a580b3a 6449{
5a580b3a
AM
6450 bfd *dynobj;
6451 const struct elf_backend_data *bed;
5a580b3a
AM
6452
6453 *sinterpptr = NULL;
6454
5a580b3a
AM
6455 if (!is_elf_hash_table (info->hash))
6456 return TRUE;
6457
5a580b3a
AM
6458 dynobj = elf_hash_table (info)->dynobj;
6459
9a2a56cc 6460 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6461 {
902e9fc7
MR
6462 struct bfd_elf_version_tree *verdefs;
6463 struct elf_info_failed asvinfo;
5a580b3a
AM
6464 struct bfd_elf_version_tree *t;
6465 struct bfd_elf_version_expr *d;
902e9fc7 6466 asection *s;
e6699019 6467 size_t soname_indx;
7ee314fa 6468
5a580b3a
AM
6469 /* If we are supposed to export all symbols into the dynamic symbol
6470 table (this is not the normal case), then do so. */
55255dae 6471 if (info->export_dynamic
0e1862bb 6472 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6473 {
3d13f3e9
AM
6474 struct elf_info_failed eif;
6475
6476 eif.info = info;
6477 eif.failed = FALSE;
5a580b3a
AM
6478 elf_link_hash_traverse (elf_hash_table (info),
6479 _bfd_elf_export_symbol,
6480 &eif);
6481 if (eif.failed)
6482 return FALSE;
6483 }
6484
e6699019
L
6485 if (soname != NULL)
6486 {
6487 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6488 soname, TRUE);
6489 if (soname_indx == (size_t) -1
6490 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6491 return FALSE;
6492 }
6493 else
6494 soname_indx = (size_t) -1;
6495
5a580b3a 6496 /* Make all global versions with definition. */
fd91d419 6497 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6498 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6499 if (!d->symver && d->literal)
5a580b3a
AM
6500 {
6501 const char *verstr, *name;
6502 size_t namelen, verlen, newlen;
93252b1c 6503 char *newname, *p, leading_char;
5a580b3a
AM
6504 struct elf_link_hash_entry *newh;
6505
93252b1c 6506 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6507 name = d->pattern;
93252b1c 6508 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6509 verstr = t->name;
6510 verlen = strlen (verstr);
6511 newlen = namelen + verlen + 3;
6512
a50b1753 6513 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6514 if (newname == NULL)
6515 return FALSE;
93252b1c
MF
6516 newname[0] = leading_char;
6517 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6518
6519 /* Check the hidden versioned definition. */
6520 p = newname + namelen;
6521 *p++ = ELF_VER_CHR;
6522 memcpy (p, verstr, verlen + 1);
6523 newh = elf_link_hash_lookup (elf_hash_table (info),
6524 newname, FALSE, FALSE,
6525 FALSE);
6526 if (newh == NULL
6527 || (newh->root.type != bfd_link_hash_defined
6528 && newh->root.type != bfd_link_hash_defweak))
6529 {
6530 /* Check the default versioned definition. */
6531 *p++ = ELF_VER_CHR;
6532 memcpy (p, verstr, verlen + 1);
6533 newh = elf_link_hash_lookup (elf_hash_table (info),
6534 newname, FALSE, FALSE,
6535 FALSE);
6536 }
6537 free (newname);
6538
6539 /* Mark this version if there is a definition and it is
6540 not defined in a shared object. */
6541 if (newh != NULL
f5385ebf 6542 && !newh->def_dynamic
5a580b3a
AM
6543 && (newh->root.type == bfd_link_hash_defined
6544 || newh->root.type == bfd_link_hash_defweak))
6545 d->symver = 1;
6546 }
6547
6548 /* Attach all the symbols to their version information. */
5a580b3a 6549 asvinfo.info = info;
5a580b3a
AM
6550 asvinfo.failed = FALSE;
6551
6552 elf_link_hash_traverse (elf_hash_table (info),
6553 _bfd_elf_link_assign_sym_version,
6554 &asvinfo);
6555 if (asvinfo.failed)
6556 return FALSE;
6557
6558 if (!info->allow_undefined_version)
6559 {
6560 /* Check if all global versions have a definition. */
3d13f3e9 6561 bfd_boolean all_defined = TRUE;
fd91d419 6562 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6563 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6564 if (d->literal && !d->symver && !d->script)
5a580b3a 6565 {
4eca0228 6566 _bfd_error_handler
5a580b3a
AM
6567 (_("%s: undefined version: %s"),
6568 d->pattern, t->name);
6569 all_defined = FALSE;
6570 }
6571
6572 if (!all_defined)
6573 {
6574 bfd_set_error (bfd_error_bad_value);
6575 return FALSE;
6576 }
6577 }
6578
902e9fc7
MR
6579 /* Set up the version definition section. */
6580 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6581 BFD_ASSERT (s != NULL);
5a580b3a 6582
902e9fc7
MR
6583 /* We may have created additional version definitions if we are
6584 just linking a regular application. */
6585 verdefs = info->version_info;
5a580b3a 6586
902e9fc7
MR
6587 /* Skip anonymous version tag. */
6588 if (verdefs != NULL && verdefs->vernum == 0)
6589 verdefs = verdefs->next;
5a580b3a 6590
902e9fc7
MR
6591 if (verdefs == NULL && !info->create_default_symver)
6592 s->flags |= SEC_EXCLUDE;
6593 else
5a580b3a 6594 {
902e9fc7
MR
6595 unsigned int cdefs;
6596 bfd_size_type size;
6597 bfd_byte *p;
6598 Elf_Internal_Verdef def;
6599 Elf_Internal_Verdaux defaux;
6600 struct bfd_link_hash_entry *bh;
6601 struct elf_link_hash_entry *h;
6602 const char *name;
5a580b3a 6603
902e9fc7
MR
6604 cdefs = 0;
6605 size = 0;
5a580b3a 6606
902e9fc7
MR
6607 /* Make space for the base version. */
6608 size += sizeof (Elf_External_Verdef);
6609 size += sizeof (Elf_External_Verdaux);
6610 ++cdefs;
6611
6612 /* Make space for the default version. */
6613 if (info->create_default_symver)
6614 {
6615 size += sizeof (Elf_External_Verdef);
6616 ++cdefs;
3e3b46e5
PB
6617 }
6618
5a580b3a
AM
6619 for (t = verdefs; t != NULL; t = t->next)
6620 {
6621 struct bfd_elf_version_deps *n;
6622
a6cc6b3b
RO
6623 /* Don't emit base version twice. */
6624 if (t->vernum == 0)
6625 continue;
6626
5a580b3a
AM
6627 size += sizeof (Elf_External_Verdef);
6628 size += sizeof (Elf_External_Verdaux);
6629 ++cdefs;
6630
6631 for (n = t->deps; n != NULL; n = n->next)
6632 size += sizeof (Elf_External_Verdaux);
6633 }
6634
eea6121a 6635 s->size = size;
a50b1753 6636 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6637 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6638 return FALSE;
6639
6640 /* Fill in the version definition section. */
6641
6642 p = s->contents;
6643
6644 def.vd_version = VER_DEF_CURRENT;
6645 def.vd_flags = VER_FLG_BASE;
6646 def.vd_ndx = 1;
6647 def.vd_cnt = 1;
3e3b46e5
PB
6648 if (info->create_default_symver)
6649 {
6650 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6651 def.vd_next = sizeof (Elf_External_Verdef);
6652 }
6653 else
6654 {
6655 def.vd_aux = sizeof (Elf_External_Verdef);
6656 def.vd_next = (sizeof (Elf_External_Verdef)
6657 + sizeof (Elf_External_Verdaux));
6658 }
5a580b3a 6659
ef53be89 6660 if (soname_indx != (size_t) -1)
5a580b3a
AM
6661 {
6662 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6663 soname_indx);
6664 def.vd_hash = bfd_elf_hash (soname);
6665 defaux.vda_name = soname_indx;
3e3b46e5 6666 name = soname;
5a580b3a
AM
6667 }
6668 else
6669 {
ef53be89 6670 size_t indx;
5a580b3a 6671
765cf5f6 6672 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6673 def.vd_hash = bfd_elf_hash (name);
6674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6675 name, FALSE);
ef53be89 6676 if (indx == (size_t) -1)
5a580b3a
AM
6677 return FALSE;
6678 defaux.vda_name = indx;
6679 }
6680 defaux.vda_next = 0;
6681
6682 _bfd_elf_swap_verdef_out (output_bfd, &def,
6683 (Elf_External_Verdef *) p);
6684 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6685 if (info->create_default_symver)
6686 {
6687 /* Add a symbol representing this version. */
6688 bh = NULL;
6689 if (! (_bfd_generic_link_add_one_symbol
6690 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6691 0, NULL, FALSE,
6692 get_elf_backend_data (dynobj)->collect, &bh)))
6693 return FALSE;
6694 h = (struct elf_link_hash_entry *) bh;
6695 h->non_elf = 0;
6696 h->def_regular = 1;
6697 h->type = STT_OBJECT;
6698 h->verinfo.vertree = NULL;
6699
6700 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6701 return FALSE;
6702
6703 /* Create a duplicate of the base version with the same
6704 aux block, but different flags. */
6705 def.vd_flags = 0;
6706 def.vd_ndx = 2;
6707 def.vd_aux = sizeof (Elf_External_Verdef);
6708 if (verdefs)
6709 def.vd_next = (sizeof (Elf_External_Verdef)
6710 + sizeof (Elf_External_Verdaux));
6711 else
6712 def.vd_next = 0;
6713 _bfd_elf_swap_verdef_out (output_bfd, &def,
6714 (Elf_External_Verdef *) p);
6715 p += sizeof (Elf_External_Verdef);
6716 }
5a580b3a
AM
6717 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6718 (Elf_External_Verdaux *) p);
6719 p += sizeof (Elf_External_Verdaux);
6720
6721 for (t = verdefs; t != NULL; t = t->next)
6722 {
6723 unsigned int cdeps;
6724 struct bfd_elf_version_deps *n;
5a580b3a 6725
a6cc6b3b
RO
6726 /* Don't emit the base version twice. */
6727 if (t->vernum == 0)
6728 continue;
6729
5a580b3a
AM
6730 cdeps = 0;
6731 for (n = t->deps; n != NULL; n = n->next)
6732 ++cdeps;
6733
6734 /* Add a symbol representing this version. */
6735 bh = NULL;
6736 if (! (_bfd_generic_link_add_one_symbol
6737 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6738 0, NULL, FALSE,
6739 get_elf_backend_data (dynobj)->collect, &bh)))
6740 return FALSE;
6741 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6742 h->non_elf = 0;
6743 h->def_regular = 1;
5a580b3a
AM
6744 h->type = STT_OBJECT;
6745 h->verinfo.vertree = t;
6746
c152c796 6747 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6748 return FALSE;
6749
6750 def.vd_version = VER_DEF_CURRENT;
6751 def.vd_flags = 0;
6752 if (t->globals.list == NULL
6753 && t->locals.list == NULL
6754 && ! t->used)
6755 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6756 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6757 def.vd_cnt = cdeps + 1;
6758 def.vd_hash = bfd_elf_hash (t->name);
6759 def.vd_aux = sizeof (Elf_External_Verdef);
6760 def.vd_next = 0;
a6cc6b3b
RO
6761
6762 /* If a basever node is next, it *must* be the last node in
6763 the chain, otherwise Verdef construction breaks. */
6764 if (t->next != NULL && t->next->vernum == 0)
6765 BFD_ASSERT (t->next->next == NULL);
6766
6767 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6768 def.vd_next = (sizeof (Elf_External_Verdef)
6769 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6770
6771 _bfd_elf_swap_verdef_out (output_bfd, &def,
6772 (Elf_External_Verdef *) p);
6773 p += sizeof (Elf_External_Verdef);
6774
6775 defaux.vda_name = h->dynstr_index;
6776 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6777 h->dynstr_index);
6778 defaux.vda_next = 0;
6779 if (t->deps != NULL)
6780 defaux.vda_next = sizeof (Elf_External_Verdaux);
6781 t->name_indx = defaux.vda_name;
6782
6783 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6784 (Elf_External_Verdaux *) p);
6785 p += sizeof (Elf_External_Verdaux);
6786
6787 for (n = t->deps; n != NULL; n = n->next)
6788 {
6789 if (n->version_needed == NULL)
6790 {
6791 /* This can happen if there was an error in the
6792 version script. */
6793 defaux.vda_name = 0;
6794 }
6795 else
6796 {
6797 defaux.vda_name = n->version_needed->name_indx;
6798 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6799 defaux.vda_name);
6800 }
6801 if (n->next == NULL)
6802 defaux.vda_next = 0;
6803 else
6804 defaux.vda_next = sizeof (Elf_External_Verdaux);
6805
6806 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6807 (Elf_External_Verdaux *) p);
6808 p += sizeof (Elf_External_Verdaux);
6809 }
6810 }
6811
5a580b3a
AM
6812 elf_tdata (output_bfd)->cverdefs = cdefs;
6813 }
902e9fc7
MR
6814 }
6815
6816 bed = get_elf_backend_data (output_bfd);
6817
6818 if (info->gc_sections && bed->can_gc_sections)
6819 {
6820 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6821
6822 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6823 dynamic symbol table. */
902e9fc7
MR
6824 sweep_info.info = info;
6825 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6826 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6827 &sweep_info);
3d13f3e9
AM
6828 }
6829
6830 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6831 {
6832 asection *s;
6833 struct elf_find_verdep_info sinfo;
6834
6835 /* Work out the size of the version reference section. */
6836
6837 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6838 BFD_ASSERT (s != NULL);
902e9fc7 6839
3d13f3e9
AM
6840 sinfo.info = info;
6841 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6842 if (sinfo.vers == 0)
6843 sinfo.vers = 1;
6844 sinfo.failed = FALSE;
6845
6846 elf_link_hash_traverse (elf_hash_table (info),
6847 _bfd_elf_link_find_version_dependencies,
6848 &sinfo);
6849 if (sinfo.failed)
6850 return FALSE;
6851
6852 if (elf_tdata (output_bfd)->verref == NULL)
6853 s->flags |= SEC_EXCLUDE;
6854 else
6855 {
6856 Elf_Internal_Verneed *vn;
6857 unsigned int size;
6858 unsigned int crefs;
6859 bfd_byte *p;
6860
6861 /* Build the version dependency section. */
6862 size = 0;
6863 crefs = 0;
6864 for (vn = elf_tdata (output_bfd)->verref;
6865 vn != NULL;
6866 vn = vn->vn_nextref)
6867 {
6868 Elf_Internal_Vernaux *a;
6869
6870 size += sizeof (Elf_External_Verneed);
6871 ++crefs;
6872 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6873 size += sizeof (Elf_External_Vernaux);
6874 }
6875
6876 s->size = size;
6877 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6878 if (s->contents == NULL)
6879 return FALSE;
6880
6881 p = s->contents;
6882 for (vn = elf_tdata (output_bfd)->verref;
6883 vn != NULL;
6884 vn = vn->vn_nextref)
6885 {
6886 unsigned int caux;
6887 Elf_Internal_Vernaux *a;
6888 size_t indx;
6889
6890 caux = 0;
6891 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6892 ++caux;
6893
6894 vn->vn_version = VER_NEED_CURRENT;
6895 vn->vn_cnt = caux;
6896 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6897 elf_dt_name (vn->vn_bfd) != NULL
6898 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6899 : lbasename (bfd_get_filename
6900 (vn->vn_bfd)),
3d13f3e9
AM
6901 FALSE);
6902 if (indx == (size_t) -1)
6903 return FALSE;
6904 vn->vn_file = indx;
6905 vn->vn_aux = sizeof (Elf_External_Verneed);
6906 if (vn->vn_nextref == NULL)
6907 vn->vn_next = 0;
6908 else
6909 vn->vn_next = (sizeof (Elf_External_Verneed)
6910 + caux * sizeof (Elf_External_Vernaux));
6911
6912 _bfd_elf_swap_verneed_out (output_bfd, vn,
6913 (Elf_External_Verneed *) p);
6914 p += sizeof (Elf_External_Verneed);
6915
6916 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6917 {
6918 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6919 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6920 a->vna_nodename, FALSE);
6921 if (indx == (size_t) -1)
6922 return FALSE;
6923 a->vna_name = indx;
6924 if (a->vna_nextptr == NULL)
6925 a->vna_next = 0;
6926 else
6927 a->vna_next = sizeof (Elf_External_Vernaux);
6928
6929 _bfd_elf_swap_vernaux_out (output_bfd, a,
6930 (Elf_External_Vernaux *) p);
6931 p += sizeof (Elf_External_Vernaux);
6932 }
6933 }
6934
6935 elf_tdata (output_bfd)->cverrefs = crefs;
6936 }
902e9fc7
MR
6937 }
6938
6939 /* Any syms created from now on start with -1 in
6940 got.refcount/offset and plt.refcount/offset. */
6941 elf_hash_table (info)->init_got_refcount
6942 = elf_hash_table (info)->init_got_offset;
6943 elf_hash_table (info)->init_plt_refcount
6944 = elf_hash_table (info)->init_plt_offset;
6945
6946 if (bfd_link_relocatable (info)
6947 && !_bfd_elf_size_group_sections (info))
6948 return FALSE;
6949
6950 /* The backend may have to create some sections regardless of whether
6951 we're dynamic or not. */
6952 if (bed->elf_backend_always_size_sections
6953 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6954 return FALSE;
6955
6956 /* Determine any GNU_STACK segment requirements, after the backend
6957 has had a chance to set a default segment size. */
6958 if (info->execstack)
6959 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6960 else if (info->noexecstack)
6961 elf_stack_flags (output_bfd) = PF_R | PF_W;
6962 else
6963 {
6964 bfd *inputobj;
6965 asection *notesec = NULL;
6966 int exec = 0;
6967
6968 for (inputobj = info->input_bfds;
6969 inputobj;
6970 inputobj = inputobj->link.next)
6971 {
6972 asection *s;
6973
6974 if (inputobj->flags
6975 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6976 continue;
57963c05
AM
6977 s = inputobj->sections;
6978 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6979 continue;
6980
902e9fc7
MR
6981 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6982 if (s)
6983 {
6984 if (s->flags & SEC_CODE)
6985 exec = PF_X;
6986 notesec = s;
6987 }
6988 else if (bed->default_execstack)
6989 exec = PF_X;
6990 }
6991 if (notesec || info->stacksize > 0)
6992 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6993 if (notesec && exec && bfd_link_relocatable (info)
6994 && notesec->output_section != bfd_abs_section_ptr)
6995 notesec->output_section->flags |= SEC_CODE;
6996 }
6997
6998 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6999 {
7000 struct elf_info_failed eif;
7001 struct elf_link_hash_entry *h;
7002 asection *dynstr;
7003 asection *s;
7004
7005 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7006 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7007
902e9fc7
MR
7008 if (info->symbolic)
7009 {
7010 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7011 return FALSE;
7012 info->flags |= DF_SYMBOLIC;
7013 }
7014
7015 if (rpath != NULL)
7016 {
7017 size_t indx;
7018 bfd_vma tag;
7019
7020 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7021 TRUE);
7022 if (indx == (size_t) -1)
7023 return FALSE;
7024
7025 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7026 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7027 return FALSE;
7028 }
7029
7030 if (filter_shlib != NULL)
7031 {
7032 size_t indx;
7033
7034 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7035 filter_shlib, TRUE);
7036 if (indx == (size_t) -1
7037 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7038 return FALSE;
7039 }
7040
7041 if (auxiliary_filters != NULL)
7042 {
7043 const char * const *p;
7044
7045 for (p = auxiliary_filters; *p != NULL; p++)
7046 {
7047 size_t indx;
7048
7049 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7050 *p, TRUE);
7051 if (indx == (size_t) -1
7052 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7053 return FALSE;
7054 }
7055 }
7056
7057 if (audit != NULL)
7058 {
7059 size_t indx;
7060
7061 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7062 TRUE);
7063 if (indx == (size_t) -1
7064 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7065 return FALSE;
7066 }
7067
7068 if (depaudit != NULL)
7069 {
7070 size_t indx;
7071
7072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7073 TRUE);
7074 if (indx == (size_t) -1
7075 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7076 return FALSE;
7077 }
7078
7079 eif.info = info;
7080 eif.failed = FALSE;
7081
7082 /* Find all symbols which were defined in a dynamic object and make
7083 the backend pick a reasonable value for them. */
7084 elf_link_hash_traverse (elf_hash_table (info),
7085 _bfd_elf_adjust_dynamic_symbol,
7086 &eif);
7087 if (eif.failed)
7088 return FALSE;
7089
7090 /* Add some entries to the .dynamic section. We fill in some of the
7091 values later, in bfd_elf_final_link, but we must add the entries
7092 now so that we know the final size of the .dynamic section. */
7093
7094 /* If there are initialization and/or finalization functions to
7095 call then add the corresponding DT_INIT/DT_FINI entries. */
7096 h = (info->init_function
7097 ? elf_link_hash_lookup (elf_hash_table (info),
7098 info->init_function, FALSE,
7099 FALSE, FALSE)
7100 : NULL);
7101 if (h != NULL
7102 && (h->ref_regular
7103 || h->def_regular))
7104 {
7105 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7106 return FALSE;
7107 }
7108 h = (info->fini_function
7109 ? elf_link_hash_lookup (elf_hash_table (info),
7110 info->fini_function, FALSE,
7111 FALSE, FALSE)
7112 : NULL);
7113 if (h != NULL
7114 && (h->ref_regular
7115 || h->def_regular))
7116 {
7117 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7118 return FALSE;
7119 }
7120
7121 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7122 if (s != NULL && s->linker_has_input)
7123 {
7124 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7125 if (! bfd_link_executable (info))
7126 {
7127 bfd *sub;
7128 asection *o;
7129
57963c05
AM
7130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7131 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7132 && (o = sub->sections) != NULL
7133 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7134 for (o = sub->sections; o != NULL; o = o->next)
7135 if (elf_section_data (o)->this_hdr.sh_type
7136 == SHT_PREINIT_ARRAY)
7137 {
7138 _bfd_error_handler
871b3ab2 7139 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7140 sub);
7141 break;
7142 }
7143
7144 bfd_set_error (bfd_error_nonrepresentable_section);
7145 return FALSE;
7146 }
7147
7148 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7149 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7150 return FALSE;
7151 }
7152 s = bfd_get_section_by_name (output_bfd, ".init_array");
7153 if (s != NULL && s->linker_has_input)
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7156 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7157 return FALSE;
7158 }
7159 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7160 if (s != NULL && s->linker_has_input)
7161 {
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7163 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7164 return FALSE;
7165 }
7166
7167 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7168 /* If .dynstr is excluded from the link, we don't want any of
7169 these tags. Strictly, we should be checking each section
7170 individually; This quick check covers for the case where
7171 someone does a /DISCARD/ : { *(*) }. */
7172 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7173 {
7174 bfd_size_type strsize;
7175
7176 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7177 if ((info->emit_hash
7178 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7179 || (info->emit_gnu_hash
f16a9783
MS
7180 && (bed->record_xhash_symbol == NULL
7181 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7182 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7183 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7184 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7185 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7186 bed->s->sizeof_sym)
7187 || (info->gnu_flags_1
7188 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7189 info->gnu_flags_1)))
902e9fc7
MR
7190 return FALSE;
7191 }
7192 }
7193
7194 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7195 return FALSE;
7196
7197 /* The backend must work out the sizes of all the other dynamic
7198 sections. */
7199 if (dynobj != NULL
7200 && bed->elf_backend_size_dynamic_sections != NULL
7201 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7202 return FALSE;
7203
7204 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7205 {
902e9fc7
MR
7206 if (elf_tdata (output_bfd)->cverdefs)
7207 {
7208 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7209
7210 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7211 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7212 return FALSE;
7213 }
7214
7215 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7216 {
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7218 return FALSE;
7219 }
7220 else if (info->flags & DF_BIND_NOW)
7221 {
7222 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7223 return FALSE;
7224 }
7225
7226 if (info->flags_1)
7227 {
7228 if (bfd_link_executable (info))
7229 info->flags_1 &= ~ (DF_1_INITFIRST
7230 | DF_1_NODELETE
7231 | DF_1_NOOPEN);
7232 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7233 return FALSE;
7234 }
7235
7236 if (elf_tdata (output_bfd)->cverrefs)
7237 {
7238 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7239
7240 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7241 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7242 return FALSE;
7243 }
5a580b3a 7244
8423293d
AM
7245 if ((elf_tdata (output_bfd)->cverrefs == 0
7246 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7247 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7248 {
902e9fc7
MR
7249 asection *s;
7250
3d4d4302 7251 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7252 s->flags |= SEC_EXCLUDE;
7253 }
7254 }
7255 return TRUE;
7256}
7257
74541ad4
AM
7258/* Find the first non-excluded output section. We'll use its
7259 section symbol for some emitted relocs. */
7260void
7261_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7262{
7263 asection *s;
f26a3287 7264 asection *found = NULL;
74541ad4
AM
7265
7266 for (s = output_bfd->sections; s != NULL; s = s->next)
7267 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7268 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7269 {
f26a3287
AM
7270 found = s;
7271 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7272 break;
74541ad4 7273 }
f26a3287 7274 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7275}
7276
7277/* Find two non-excluded output sections, one for code, one for data.
7278 We'll use their section symbols for some emitted relocs. */
7279void
7280_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7281{
7282 asection *s;
f26a3287 7283 asection *found = NULL;
74541ad4 7284
266b05cf 7285 /* Data first, since setting text_index_section changes
7f923b7f 7286 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7287 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7288 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7289 && !(s->flags & SEC_READONLY)
d00dd7dc 7290 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7291 {
f26a3287
AM
7292 found = s;
7293 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7294 break;
74541ad4 7295 }
f26a3287 7296 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7297
7298 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7299 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7300 && (s->flags & SEC_READONLY)
d00dd7dc 7301 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7302 {
f26a3287 7303 found = s;
74541ad4
AM
7304 break;
7305 }
f26a3287 7306 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7307}
7308
f16a9783
MS
7309#define GNU_HASH_SECTION_NAME(bed) \
7310 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7311
8423293d
AM
7312bfd_boolean
7313bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7314{
74541ad4 7315 const struct elf_backend_data *bed;
23ec1e32 7316 unsigned long section_sym_count;
96d01d93 7317 bfd_size_type dynsymcount = 0;
74541ad4 7318
8423293d
AM
7319 if (!is_elf_hash_table (info->hash))
7320 return TRUE;
7321
74541ad4
AM
7322 bed = get_elf_backend_data (output_bfd);
7323 (*bed->elf_backend_init_index_section) (output_bfd, info);
7324
23ec1e32
MR
7325 /* Assign dynsym indices. In a shared library we generate a section
7326 symbol for each output section, which come first. Next come all
7327 of the back-end allocated local dynamic syms, followed by the rest
7328 of the global symbols.
7329
7330 This is usually not needed for static binaries, however backends
7331 can request to always do it, e.g. the MIPS backend uses dynamic
7332 symbol counts to lay out GOT, which will be produced in the
7333 presence of GOT relocations even in static binaries (holding fixed
7334 data in that case, to satisfy those relocations). */
7335
7336 if (elf_hash_table (info)->dynamic_sections_created
7337 || bed->always_renumber_dynsyms)
7338 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7339 &section_sym_count);
7340
8423293d
AM
7341 if (elf_hash_table (info)->dynamic_sections_created)
7342 {
7343 bfd *dynobj;
8423293d 7344 asection *s;
8423293d
AM
7345 unsigned int dtagcount;
7346
7347 dynobj = elf_hash_table (info)->dynobj;
7348
5a580b3a 7349 /* Work out the size of the symbol version section. */
3d4d4302 7350 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7351 BFD_ASSERT (s != NULL);
d5486c43 7352 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7353 {
eea6121a 7354 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7355 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7356 if (s->contents == NULL)
7357 return FALSE;
7358
7359 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7360 return FALSE;
7361 }
7362
7363 /* Set the size of the .dynsym and .hash sections. We counted
7364 the number of dynamic symbols in elf_link_add_object_symbols.
7365 We will build the contents of .dynsym and .hash when we build
7366 the final symbol table, because until then we do not know the
7367 correct value to give the symbols. We built the .dynstr
7368 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7369 s = elf_hash_table (info)->dynsym;
5a580b3a 7370 BFD_ASSERT (s != NULL);
eea6121a 7371 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7372
d5486c43
L
7373 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7374 if (s->contents == NULL)
7375 return FALSE;
5a580b3a 7376
d5486c43
L
7377 /* The first entry in .dynsym is a dummy symbol. Clear all the
7378 section syms, in case we don't output them all. */
7379 ++section_sym_count;
7380 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7381
fdc90cb4
JJ
7382 elf_hash_table (info)->bucketcount = 0;
7383
5a580b3a
AM
7384 /* Compute the size of the hashing table. As a side effect this
7385 computes the hash values for all the names we export. */
fdc90cb4
JJ
7386 if (info->emit_hash)
7387 {
7388 unsigned long int *hashcodes;
14b1c01e 7389 struct hash_codes_info hashinf;
fdc90cb4
JJ
7390 bfd_size_type amt;
7391 unsigned long int nsyms;
7392 size_t bucketcount;
7393 size_t hash_entry_size;
7394
7395 /* Compute the hash values for all exported symbols. At the same
7396 time store the values in an array so that we could use them for
7397 optimizations. */
7398 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7399 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7400 if (hashcodes == NULL)
7401 return FALSE;
14b1c01e
AM
7402 hashinf.hashcodes = hashcodes;
7403 hashinf.error = FALSE;
5a580b3a 7404
fdc90cb4
JJ
7405 /* Put all hash values in HASHCODES. */
7406 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7407 elf_collect_hash_codes, &hashinf);
7408 if (hashinf.error)
4dd07732
AM
7409 {
7410 free (hashcodes);
7411 return FALSE;
7412 }
5a580b3a 7413
14b1c01e 7414 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7415 bucketcount
7416 = compute_bucket_count (info, hashcodes, nsyms, 0);
7417 free (hashcodes);
7418
4b48e2f6 7419 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7420 return FALSE;
5a580b3a 7421
fdc90cb4
JJ
7422 elf_hash_table (info)->bucketcount = bucketcount;
7423
3d4d4302 7424 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7425 BFD_ASSERT (s != NULL);
7426 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7427 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7428 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7429 if (s->contents == NULL)
7430 return FALSE;
7431
7432 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7433 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7434 s->contents + hash_entry_size);
7435 }
7436
7437 if (info->emit_gnu_hash)
7438 {
7439 size_t i, cnt;
7440 unsigned char *contents;
7441 struct collect_gnu_hash_codes cinfo;
7442 bfd_size_type amt;
7443 size_t bucketcount;
7444
7445 memset (&cinfo, 0, sizeof (cinfo));
7446
7447 /* Compute the hash values for all exported symbols. At the same
7448 time store the values in an array so that we could use them for
7449 optimizations. */
7450 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7451 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7452 if (cinfo.hashcodes == NULL)
7453 return FALSE;
7454
7455 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7456 cinfo.min_dynindx = -1;
7457 cinfo.output_bfd = output_bfd;
7458 cinfo.bed = bed;
7459
7460 /* Put all hash values in HASHCODES. */
7461 elf_link_hash_traverse (elf_hash_table (info),
7462 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7463 if (cinfo.error)
4dd07732
AM
7464 {
7465 free (cinfo.hashcodes);
7466 return FALSE;
7467 }
fdc90cb4
JJ
7468
7469 bucketcount
7470 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7471
7472 if (bucketcount == 0)
7473 {
7474 free (cinfo.hashcodes);
7475 return FALSE;
7476 }
7477
f16a9783 7478 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7479 BFD_ASSERT (s != NULL);
7480
7481 if (cinfo.nsyms == 0)
7482 {
f16a9783 7483 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7484 BFD_ASSERT (cinfo.min_dynindx == -1);
7485 free (cinfo.hashcodes);
7486 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7487 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7488 if (contents == NULL)
7489 return FALSE;
7490 s->contents = contents;
7491 /* 1 empty bucket. */
7492 bfd_put_32 (output_bfd, 1, contents);
7493 /* SYMIDX above the special symbol 0. */
7494 bfd_put_32 (output_bfd, 1, contents + 4);
7495 /* Just one word for bitmask. */
7496 bfd_put_32 (output_bfd, 1, contents + 8);
7497 /* Only hash fn bloom filter. */
7498 bfd_put_32 (output_bfd, 0, contents + 12);
7499 /* No hashes are valid - empty bitmask. */
7500 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7501 /* No hashes in the only bucket. */
7502 bfd_put_32 (output_bfd, 0,
7503 contents + 16 + bed->s->arch_size / 8);
7504 }
7505 else
7506 {
9e6619e2 7507 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7508 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7509
9e6619e2
AM
7510 x = cinfo.nsyms;
7511 maskbitslog2 = 1;
7512 while ((x >>= 1) != 0)
7513 ++maskbitslog2;
fdc90cb4
JJ
7514 if (maskbitslog2 < 3)
7515 maskbitslog2 = 5;
7516 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7517 maskbitslog2 = maskbitslog2 + 3;
7518 else
7519 maskbitslog2 = maskbitslog2 + 2;
7520 if (bed->s->arch_size == 64)
7521 {
7522 if (maskbitslog2 == 5)
7523 maskbitslog2 = 6;
7524 cinfo.shift1 = 6;
7525 }
7526 else
7527 cinfo.shift1 = 5;
7528 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7529 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7530 cinfo.maskbits = 1 << maskbitslog2;
7531 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7532 amt = bucketcount * sizeof (unsigned long int) * 2;
7533 amt += maskwords * sizeof (bfd_vma);
a50b1753 7534 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7535 if (cinfo.bitmask == NULL)
7536 {
7537 free (cinfo.hashcodes);
7538 return FALSE;
7539 }
7540
a50b1753 7541 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7542 cinfo.indx = cinfo.counts + bucketcount;
7543 cinfo.symindx = dynsymcount - cinfo.nsyms;
7544 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7545
7546 /* Determine how often each hash bucket is used. */
7547 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7548 for (i = 0; i < cinfo.nsyms; ++i)
7549 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7550
7551 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7552 if (cinfo.counts[i] != 0)
7553 {
7554 cinfo.indx[i] = cnt;
7555 cnt += cinfo.counts[i];
7556 }
7557 BFD_ASSERT (cnt == dynsymcount);
7558 cinfo.bucketcount = bucketcount;
7559 cinfo.local_indx = cinfo.min_dynindx;
7560
7561 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7562 s->size += cinfo.maskbits / 8;
f16a9783
MS
7563 if (bed->record_xhash_symbol != NULL)
7564 s->size += cinfo.nsyms * 4;
a50b1753 7565 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7566 if (contents == NULL)
7567 {
7568 free (cinfo.bitmask);
7569 free (cinfo.hashcodes);
7570 return FALSE;
7571 }
7572
7573 s->contents = contents;
7574 bfd_put_32 (output_bfd, bucketcount, contents);
7575 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7576 bfd_put_32 (output_bfd, maskwords, contents + 8);
7577 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7578 contents += 16 + cinfo.maskbits / 8;
7579
7580 for (i = 0; i < bucketcount; ++i)
7581 {
7582 if (cinfo.counts[i] == 0)
7583 bfd_put_32 (output_bfd, 0, contents);
7584 else
7585 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7586 contents += 4;
7587 }
7588
7589 cinfo.contents = contents;
7590
f16a9783
MS
7591 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7592 /* Renumber dynamic symbols, if populating .gnu.hash section.
7593 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7594 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7595 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7596
7597 contents = s->contents + 16;
7598 for (i = 0; i < maskwords; ++i)
7599 {
7600 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7601 contents);
7602 contents += bed->s->arch_size / 8;
7603 }
7604
7605 free (cinfo.bitmask);
7606 free (cinfo.hashcodes);
7607 }
7608 }
5a580b3a 7609
3d4d4302 7610 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7611 BFD_ASSERT (s != NULL);
7612
4ad4eba5 7613 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7614
eea6121a 7615 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7616
7617 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7618 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7619 return FALSE;
7620 }
7621
7622 return TRUE;
7623}
4d269e42 7624\f
4d269e42
AM
7625/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7626
7627static void
7628merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7629 asection *sec)
7630{
dbaa2011
AM
7631 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7632 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7633}
7634
7635/* Finish SHF_MERGE section merging. */
7636
7637bfd_boolean
630993ec 7638_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7639{
7640 bfd *ibfd;
7641 asection *sec;
7642
7643 if (!is_elf_hash_table (info->hash))
7644 return FALSE;
7645
c72f2fb2 7646 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7647 if ((ibfd->flags & DYNAMIC) == 0
7648 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7649 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7650 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7651 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7652 if ((sec->flags & SEC_MERGE) != 0
7653 && !bfd_is_abs_section (sec->output_section))
7654 {
7655 struct bfd_elf_section_data *secdata;
7656
7657 secdata = elf_section_data (sec);
630993ec 7658 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7659 &elf_hash_table (info)->merge_info,
7660 sec, &secdata->sec_info))
7661 return FALSE;
7662 else if (secdata->sec_info)
dbaa2011 7663 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7664 }
7665
7666 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7667 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7668 merge_sections_remove_hook);
7669 return TRUE;
7670}
7671
7672/* Create an entry in an ELF linker hash table. */
7673
7674struct bfd_hash_entry *
7675_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7676 struct bfd_hash_table *table,
7677 const char *string)
7678{
7679 /* Allocate the structure if it has not already been allocated by a
7680 subclass. */
7681 if (entry == NULL)
7682 {
a50b1753 7683 entry = (struct bfd_hash_entry *)
ca4be51c 7684 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7685 if (entry == NULL)
7686 return entry;
7687 }
7688
7689 /* Call the allocation method of the superclass. */
7690 entry = _bfd_link_hash_newfunc (entry, table, string);
7691 if (entry != NULL)
7692 {
7693 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7694 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7695
7696 /* Set local fields. */
7697 ret->indx = -1;
7698 ret->dynindx = -1;
7699 ret->got = htab->init_got_refcount;
7700 ret->plt = htab->init_plt_refcount;
7701 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7702 - offsetof (struct elf_link_hash_entry, size)));
7703 /* Assume that we have been called by a non-ELF symbol reader.
7704 This flag is then reset by the code which reads an ELF input
7705 file. This ensures that a symbol created by a non-ELF symbol
7706 reader will have the flag set correctly. */
7707 ret->non_elf = 1;
7708 }
7709
7710 return entry;
7711}
7712
7713/* Copy data from an indirect symbol to its direct symbol, hiding the
7714 old indirect symbol. Also used for copying flags to a weakdef. */
7715
7716void
7717_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7718 struct elf_link_hash_entry *dir,
7719 struct elf_link_hash_entry *ind)
7720{
7721 struct elf_link_hash_table *htab;
7722
ad172eaa
L
7723 if (ind->dyn_relocs != NULL)
7724 {
7725 if (dir->dyn_relocs != NULL)
7726 {
7727 struct elf_dyn_relocs **pp;
7728 struct elf_dyn_relocs *p;
7729
7730 /* Add reloc counts against the indirect sym to the direct sym
7731 list. Merge any entries against the same section. */
7732 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7733 {
7734 struct elf_dyn_relocs *q;
7735
7736 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7737 if (q->sec == p->sec)
7738 {
7739 q->pc_count += p->pc_count;
7740 q->count += p->count;
7741 *pp = p->next;
7742 break;
7743 }
7744 if (q == NULL)
7745 pp = &p->next;
7746 }
7747 *pp = dir->dyn_relocs;
7748 }
7749
7750 dir->dyn_relocs = ind->dyn_relocs;
7751 ind->dyn_relocs = NULL;
7752 }
7753
4d269e42 7754 /* Copy down any references that we may have already seen to the
e81830c5 7755 symbol which just became indirect. */
4d269e42 7756
422f1182 7757 if (dir->versioned != versioned_hidden)
e81830c5
AM
7758 dir->ref_dynamic |= ind->ref_dynamic;
7759 dir->ref_regular |= ind->ref_regular;
7760 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7761 dir->non_got_ref |= ind->non_got_ref;
7762 dir->needs_plt |= ind->needs_plt;
7763 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7764
7765 if (ind->root.type != bfd_link_hash_indirect)
7766 return;
7767
7768 /* Copy over the global and procedure linkage table refcount entries.
7769 These may have been already set up by a check_relocs routine. */
7770 htab = elf_hash_table (info);
7771 if (ind->got.refcount > htab->init_got_refcount.refcount)
7772 {
7773 if (dir->got.refcount < 0)
7774 dir->got.refcount = 0;
7775 dir->got.refcount += ind->got.refcount;
7776 ind->got.refcount = htab->init_got_refcount.refcount;
7777 }
7778
7779 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7780 {
7781 if (dir->plt.refcount < 0)
7782 dir->plt.refcount = 0;
7783 dir->plt.refcount += ind->plt.refcount;
7784 ind->plt.refcount = htab->init_plt_refcount.refcount;
7785 }
7786
7787 if (ind->dynindx != -1)
7788 {
7789 if (dir->dynindx != -1)
7790 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7791 dir->dynindx = ind->dynindx;
7792 dir->dynstr_index = ind->dynstr_index;
7793 ind->dynindx = -1;
7794 ind->dynstr_index = 0;
7795 }
7796}
7797
7798void
7799_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7800 struct elf_link_hash_entry *h,
7801 bfd_boolean force_local)
7802{
3aa14d16
L
7803 /* STT_GNU_IFUNC symbol must go through PLT. */
7804 if (h->type != STT_GNU_IFUNC)
7805 {
7806 h->plt = elf_hash_table (info)->init_plt_offset;
7807 h->needs_plt = 0;
7808 }
4d269e42
AM
7809 if (force_local)
7810 {
7811 h->forced_local = 1;
7812 if (h->dynindx != -1)
7813 {
4d269e42
AM
7814 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7815 h->dynstr_index);
641338d8
AM
7816 h->dynindx = -1;
7817 h->dynstr_index = 0;
4d269e42
AM
7818 }
7819 }
7820}
7821
34a87bb0
L
7822/* Hide a symbol. */
7823
7824void
7825_bfd_elf_link_hide_symbol (bfd *output_bfd,
7826 struct bfd_link_info *info,
7827 struct bfd_link_hash_entry *h)
7828{
7829 if (is_elf_hash_table (info->hash))
7830 {
7831 const struct elf_backend_data *bed
7832 = get_elf_backend_data (output_bfd);
7833 struct elf_link_hash_entry *eh
7834 = (struct elf_link_hash_entry *) h;
7835 bed->elf_backend_hide_symbol (info, eh, TRUE);
7836 eh->def_dynamic = 0;
7837 eh->ref_dynamic = 0;
7838 eh->dynamic_def = 0;
7839 }
7840}
7841
7bf52ea2
AM
7842/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7843 caller. */
4d269e42
AM
7844
7845bfd_boolean
7846_bfd_elf_link_hash_table_init
7847 (struct elf_link_hash_table *table,
7848 bfd *abfd,
7849 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7850 struct bfd_hash_table *,
7851 const char *),
4dfe6ac6
NC
7852 unsigned int entsize,
7853 enum elf_target_id target_id)
4d269e42
AM
7854{
7855 bfd_boolean ret;
7856 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7857
4d269e42
AM
7858 table->init_got_refcount.refcount = can_refcount - 1;
7859 table->init_plt_refcount.refcount = can_refcount - 1;
7860 table->init_got_offset.offset = -(bfd_vma) 1;
7861 table->init_plt_offset.offset = -(bfd_vma) 1;
7862 /* The first dynamic symbol is a dummy. */
7863 table->dynsymcount = 1;
7864
7865 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7866
4d269e42 7867 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7868 table->hash_table_id = target_id;
90c14f0c 7869 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7870
7871 return ret;
7872}
7873
7874/* Create an ELF linker hash table. */
7875
7876struct bfd_link_hash_table *
7877_bfd_elf_link_hash_table_create (bfd *abfd)
7878{
7879 struct elf_link_hash_table *ret;
986f0783 7880 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7881
7bf52ea2 7882 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7883 if (ret == NULL)
7884 return NULL;
7885
7886 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7887 sizeof (struct elf_link_hash_entry),
7888 GENERIC_ELF_DATA))
4d269e42
AM
7889 {
7890 free (ret);
7891 return NULL;
7892 }
d495ab0d 7893 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7894
7895 return &ret->root;
7896}
7897
9f7c3e5e
AM
7898/* Destroy an ELF linker hash table. */
7899
7900void
d495ab0d 7901_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7902{
d495ab0d
AM
7903 struct elf_link_hash_table *htab;
7904
7905 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7906 if (htab->dynstr != NULL)
7907 _bfd_elf_strtab_free (htab->dynstr);
7908 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7909 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7910}
7911
4d269e42
AM
7912/* This is a hook for the ELF emulation code in the generic linker to
7913 tell the backend linker what file name to use for the DT_NEEDED
7914 entry for a dynamic object. */
7915
7916void
7917bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7918{
7919 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7920 && bfd_get_format (abfd) == bfd_object)
7921 elf_dt_name (abfd) = name;
7922}
7923
7924int
7925bfd_elf_get_dyn_lib_class (bfd *abfd)
7926{
7927 int lib_class;
7928 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd) == bfd_object)
7930 lib_class = elf_dyn_lib_class (abfd);
7931 else
7932 lib_class = 0;
7933 return lib_class;
7934}
7935
7936void
7937bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7938{
7939 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7940 && bfd_get_format (abfd) == bfd_object)
7941 elf_dyn_lib_class (abfd) = lib_class;
7942}
7943
7944/* Get the list of DT_NEEDED entries for a link. This is a hook for
7945 the linker ELF emulation code. */
7946
7947struct bfd_link_needed_list *
7948bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7949 struct bfd_link_info *info)
7950{
7951 if (! is_elf_hash_table (info->hash))
7952 return NULL;
7953 return elf_hash_table (info)->needed;
7954}
7955
7956/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7957 hook for the linker ELF emulation code. */
7958
7959struct bfd_link_needed_list *
7960bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7961 struct bfd_link_info *info)
7962{
7963 if (! is_elf_hash_table (info->hash))
7964 return NULL;
7965 return elf_hash_table (info)->runpath;
7966}
7967
7968/* Get the name actually used for a dynamic object for a link. This
7969 is the SONAME entry if there is one. Otherwise, it is the string
7970 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7971
7972const char *
7973bfd_elf_get_dt_soname (bfd *abfd)
7974{
7975 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7976 && bfd_get_format (abfd) == bfd_object)
7977 return elf_dt_name (abfd);
7978 return NULL;
7979}
7980
7981/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7982 the ELF linker emulation code. */
7983
7984bfd_boolean
7985bfd_elf_get_bfd_needed_list (bfd *abfd,
7986 struct bfd_link_needed_list **pneeded)
7987{
7988 asection *s;
7989 bfd_byte *dynbuf = NULL;
cb33740c 7990 unsigned int elfsec;
4d269e42
AM
7991 unsigned long shlink;
7992 bfd_byte *extdyn, *extdynend;
7993 size_t extdynsize;
7994 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7995
7996 *pneeded = NULL;
7997
7998 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7999 || bfd_get_format (abfd) != bfd_object)
8000 return TRUE;
8001
8002 s = bfd_get_section_by_name (abfd, ".dynamic");
8003 if (s == NULL || s->size == 0)
8004 return TRUE;
8005
8006 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8007 goto error_return;
8008
8009 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8010 if (elfsec == SHN_BAD)
4d269e42
AM
8011 goto error_return;
8012
8013 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8014
4d269e42
AM
8015 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8016 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8017
8018 extdyn = dynbuf;
8019 extdynend = extdyn + s->size;
8020 for (; extdyn < extdynend; extdyn += extdynsize)
8021 {
8022 Elf_Internal_Dyn dyn;
8023
8024 (*swap_dyn_in) (abfd, extdyn, &dyn);
8025
8026 if (dyn.d_tag == DT_NULL)
8027 break;
8028
8029 if (dyn.d_tag == DT_NEEDED)
8030 {
8031 const char *string;
8032 struct bfd_link_needed_list *l;
8033 unsigned int tagv = dyn.d_un.d_val;
986f0783 8034 size_t amt;
4d269e42
AM
8035
8036 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8037 if (string == NULL)
8038 goto error_return;
8039
8040 amt = sizeof *l;
a50b1753 8041 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8042 if (l == NULL)
8043 goto error_return;
8044
8045 l->by = abfd;
8046 l->name = string;
8047 l->next = *pneeded;
8048 *pneeded = l;
8049 }
8050 }
8051
8052 free (dynbuf);
8053
8054 return TRUE;
8055
8056 error_return:
c9594989 8057 free (dynbuf);
4d269e42
AM
8058 return FALSE;
8059}
8060
8061struct elf_symbuf_symbol
8062{
8063 unsigned long st_name; /* Symbol name, index in string tbl */
8064 unsigned char st_info; /* Type and binding attributes */
8065 unsigned char st_other; /* Visibilty, and target specific */
8066};
8067
8068struct elf_symbuf_head
8069{
8070 struct elf_symbuf_symbol *ssym;
ef53be89 8071 size_t count;
4d269e42
AM
8072 unsigned int st_shndx;
8073};
8074
8075struct elf_symbol
8076{
8077 union
8078 {
8079 Elf_Internal_Sym *isym;
8080 struct elf_symbuf_symbol *ssym;
dcea6a95 8081 void *p;
4d269e42
AM
8082 } u;
8083 const char *name;
8084};
8085
8086/* Sort references to symbols by ascending section number. */
8087
8088static int
8089elf_sort_elf_symbol (const void *arg1, const void *arg2)
8090{
8091 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8092 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8093
dcea6a95
AM
8094 if (s1->st_shndx != s2->st_shndx)
8095 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8096 /* Final sort by the address of the sym in the symbuf ensures
8097 a stable sort. */
8098 if (s1 != s2)
8099 return s1 > s2 ? 1 : -1;
8100 return 0;
4d269e42
AM
8101}
8102
8103static int
8104elf_sym_name_compare (const void *arg1, const void *arg2)
8105{
8106 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8107 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8108 int ret = strcmp (s1->name, s2->name);
8109 if (ret != 0)
8110 return ret;
8111 if (s1->u.p != s2->u.p)
8112 return s1->u.p > s2->u.p ? 1 : -1;
8113 return 0;
4d269e42
AM
8114}
8115
8116static struct elf_symbuf_head *
ef53be89 8117elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8118{
14b1c01e 8119 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8120 struct elf_symbuf_symbol *ssym;
8121 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8122 size_t i, shndx_count, total_size, amt;
4d269e42 8123
446f7ed5
AM
8124 amt = symcount * sizeof (*indbuf);
8125 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8126 if (indbuf == NULL)
8127 return NULL;
8128
8129 for (ind = indbuf, i = 0; i < symcount; i++)
8130 if (isymbuf[i].st_shndx != SHN_UNDEF)
8131 *ind++ = &isymbuf[i];
8132 indbufend = ind;
8133
8134 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8135 elf_sort_elf_symbol);
8136
8137 shndx_count = 0;
8138 if (indbufend > indbuf)
8139 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8140 if (ind[0]->st_shndx != ind[1]->st_shndx)
8141 shndx_count++;
8142
3ae181ee
L
8143 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8144 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8145 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8146 if (ssymbuf == NULL)
8147 {
8148 free (indbuf);
8149 return NULL;
8150 }
8151
3ae181ee 8152 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8153 ssymbuf->ssym = NULL;
8154 ssymbuf->count = shndx_count;
8155 ssymbuf->st_shndx = 0;
8156 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8157 {
8158 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8159 {
8160 ssymhead++;
8161 ssymhead->ssym = ssym;
8162 ssymhead->count = 0;
8163 ssymhead->st_shndx = (*ind)->st_shndx;
8164 }
8165 ssym->st_name = (*ind)->st_name;
8166 ssym->st_info = (*ind)->st_info;
8167 ssym->st_other = (*ind)->st_other;
8168 ssymhead->count++;
8169 }
ef53be89 8170 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8171 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8172 == total_size));
4d269e42
AM
8173
8174 free (indbuf);
8175 return ssymbuf;
8176}
8177
8178/* Check if 2 sections define the same set of local and global
8179 symbols. */
8180
8f317e31 8181static bfd_boolean
4d269e42
AM
8182bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8183 struct bfd_link_info *info)
8184{
8185 bfd *bfd1, *bfd2;
8186 const struct elf_backend_data *bed1, *bed2;
8187 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8188 size_t symcount1, symcount2;
4d269e42
AM
8189 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8190 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8191 Elf_Internal_Sym *isym, *isymend;
8192 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8193 size_t count1, count2, i;
cb33740c 8194 unsigned int shndx1, shndx2;
4d269e42
AM
8195 bfd_boolean result;
8196
8197 bfd1 = sec1->owner;
8198 bfd2 = sec2->owner;
8199
4d269e42
AM
8200 /* Both sections have to be in ELF. */
8201 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8202 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8203 return FALSE;
8204
8205 if (elf_section_type (sec1) != elf_section_type (sec2))
8206 return FALSE;
8207
4d269e42
AM
8208 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8209 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8210 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8211 return FALSE;
8212
8213 bed1 = get_elf_backend_data (bfd1);
8214 bed2 = get_elf_backend_data (bfd2);
8215 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8216 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8217 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8218 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8219
8220 if (symcount1 == 0 || symcount2 == 0)
8221 return FALSE;
8222
8223 result = FALSE;
8224 isymbuf1 = NULL;
8225 isymbuf2 = NULL;
a50b1753
NC
8226 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8227 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8228
8229 if (ssymbuf1 == NULL)
8230 {
8231 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8232 NULL, NULL, NULL);
8233 if (isymbuf1 == NULL)
8234 goto done;
8235
67411cbf 8236 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8237 {
8238 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8239 elf_tdata (bfd1)->symbuf = ssymbuf1;
8240 }
4d269e42
AM
8241 }
8242
8243 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8244 {
8245 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8246 NULL, NULL, NULL);
8247 if (isymbuf2 == NULL)
8248 goto done;
8249
67411cbf 8250 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8251 {
8252 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8253 elf_tdata (bfd2)->symbuf = ssymbuf2;
8254 }
4d269e42
AM
8255 }
8256
8257 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8258 {
8259 /* Optimized faster version. */
ef53be89 8260 size_t lo, hi, mid;
4d269e42
AM
8261 struct elf_symbol *symp;
8262 struct elf_symbuf_symbol *ssym, *ssymend;
8263
8264 lo = 0;
8265 hi = ssymbuf1->count;
8266 ssymbuf1++;
8267 count1 = 0;
8268 while (lo < hi)
8269 {
8270 mid = (lo + hi) / 2;
cb33740c 8271 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8272 hi = mid;
cb33740c 8273 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8274 lo = mid + 1;
8275 else
8276 {
8277 count1 = ssymbuf1[mid].count;
8278 ssymbuf1 += mid;
8279 break;
8280 }
8281 }
8282
8283 lo = 0;
8284 hi = ssymbuf2->count;
8285 ssymbuf2++;
8286 count2 = 0;
8287 while (lo < hi)
8288 {
8289 mid = (lo + hi) / 2;
cb33740c 8290 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8291 hi = mid;
cb33740c 8292 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8293 lo = mid + 1;
8294 else
8295 {
8296 count2 = ssymbuf2[mid].count;
8297 ssymbuf2 += mid;
8298 break;
8299 }
8300 }
8301
8302 if (count1 == 0 || count2 == 0 || count1 != count2)
8303 goto done;
8304
ca4be51c
AM
8305 symtable1
8306 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8307 symtable2
8308 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8309 if (symtable1 == NULL || symtable2 == NULL)
8310 goto done;
8311
8312 symp = symtable1;
8313 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8314 ssym < ssymend; ssym++, symp++)
8315 {
8316 symp->u.ssym = ssym;
8317 symp->name = bfd_elf_string_from_elf_section (bfd1,
8318 hdr1->sh_link,
8319 ssym->st_name);
8320 }
8321
8322 symp = symtable2;
8323 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8324 ssym < ssymend; ssym++, symp++)
8325 {
8326 symp->u.ssym = ssym;
8327 symp->name = bfd_elf_string_from_elf_section (bfd2,
8328 hdr2->sh_link,
8329 ssym->st_name);
8330 }
8331
8332 /* Sort symbol by name. */
8333 qsort (symtable1, count1, sizeof (struct elf_symbol),
8334 elf_sym_name_compare);
8335 qsort (symtable2, count1, sizeof (struct elf_symbol),
8336 elf_sym_name_compare);
8337
8338 for (i = 0; i < count1; i++)
8339 /* Two symbols must have the same binding, type and name. */
8340 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8341 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8342 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8343 goto done;
8344
8345 result = TRUE;
8346 goto done;
8347 }
8348
a50b1753
NC
8349 symtable1 = (struct elf_symbol *)
8350 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8351 symtable2 = (struct elf_symbol *)
8352 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8353 if (symtable1 == NULL || symtable2 == NULL)
8354 goto done;
8355
8356 /* Count definitions in the section. */
8357 count1 = 0;
8358 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8359 if (isym->st_shndx == shndx1)
4d269e42
AM
8360 symtable1[count1++].u.isym = isym;
8361
8362 count2 = 0;
8363 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8364 if (isym->st_shndx == shndx2)
4d269e42
AM
8365 symtable2[count2++].u.isym = isym;
8366
8367 if (count1 == 0 || count2 == 0 || count1 != count2)
8368 goto done;
8369
8370 for (i = 0; i < count1; i++)
8371 symtable1[i].name
8372 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8373 symtable1[i].u.isym->st_name);
8374
8375 for (i = 0; i < count2; i++)
8376 symtable2[i].name
8377 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8378 symtable2[i].u.isym->st_name);
8379
8380 /* Sort symbol by name. */
8381 qsort (symtable1, count1, sizeof (struct elf_symbol),
8382 elf_sym_name_compare);
8383 qsort (symtable2, count1, sizeof (struct elf_symbol),
8384 elf_sym_name_compare);
8385
8386 for (i = 0; i < count1; i++)
8387 /* Two symbols must have the same binding, type and name. */
8388 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8389 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8390 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8391 goto done;
8392
8393 result = TRUE;
8394
dc1e8a47 8395 done:
c9594989
AM
8396 free (symtable1);
8397 free (symtable2);
8398 free (isymbuf1);
8399 free (isymbuf2);
4d269e42
AM
8400
8401 return result;
8402}
8403
8404/* Return TRUE if 2 section types are compatible. */
8405
8406bfd_boolean
8407_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8408 bfd *bbfd, const asection *bsec)
8409{
8410 if (asec == NULL
8411 || bsec == NULL
8412 || abfd->xvec->flavour != bfd_target_elf_flavour
8413 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8414 return TRUE;
8415
8416 return elf_section_type (asec) == elf_section_type (bsec);
8417}
8418\f
c152c796
AM
8419/* Final phase of ELF linker. */
8420
8421/* A structure we use to avoid passing large numbers of arguments. */
8422
8423struct elf_final_link_info
8424{
8425 /* General link information. */
8426 struct bfd_link_info *info;
8427 /* Output BFD. */
8428 bfd *output_bfd;
8429 /* Symbol string table. */
ef10c3ac 8430 struct elf_strtab_hash *symstrtab;
c152c796
AM
8431 /* .hash section. */
8432 asection *hash_sec;
8433 /* symbol version section (.gnu.version). */
8434 asection *symver_sec;
8435 /* Buffer large enough to hold contents of any section. */
8436 bfd_byte *contents;
8437 /* Buffer large enough to hold external relocs of any section. */
8438 void *external_relocs;
8439 /* Buffer large enough to hold internal relocs of any section. */
8440 Elf_Internal_Rela *internal_relocs;
8441 /* Buffer large enough to hold external local symbols of any input
8442 BFD. */
8443 bfd_byte *external_syms;
8444 /* And a buffer for symbol section indices. */
8445 Elf_External_Sym_Shndx *locsym_shndx;
8446 /* Buffer large enough to hold internal local symbols of any input
8447 BFD. */
8448 Elf_Internal_Sym *internal_syms;
8449 /* Array large enough to hold a symbol index for each local symbol
8450 of any input BFD. */
8451 long *indices;
8452 /* Array large enough to hold a section pointer for each local
8453 symbol of any input BFD. */
8454 asection **sections;
ef10c3ac 8455 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8456 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8457 /* Number of STT_FILE syms seen. */
8458 size_t filesym_count;
496afd17
L
8459 /* Local symbol hash table. */
8460 struct bfd_hash_table local_hash_table;
c152c796
AM
8461};
8462
496afd17
L
8463struct local_hash_entry
8464{
8465 /* Base hash table entry structure. */
8466 struct bfd_hash_entry root;
8467 /* Size of the local symbol name. */
8468 size_t size;
8469 /* Number of the duplicated local symbol names. */
8470 long count;
8471};
8472
8473/* Create an entry in the local symbol hash table. */
8474
8475static struct bfd_hash_entry *
8476local_hash_newfunc (struct bfd_hash_entry *entry,
8477 struct bfd_hash_table *table,
8478 const char *string)
8479{
8480
8481 /* Allocate the structure if it has not already been allocated by a
8482 subclass. */
8483 if (entry == NULL)
8484 {
8485 entry = bfd_hash_allocate (table,
8486 sizeof (struct local_hash_entry));
8487 if (entry == NULL)
8488 return entry;
8489 }
8490
8491 /* Call the allocation method of the superclass. */
8492 entry = bfd_hash_newfunc (entry, table, string);
8493 if (entry != NULL)
8494 {
8495 ((struct local_hash_entry *) entry)->count = 0;
8496 ((struct local_hash_entry *) entry)->size = 0;
8497 }
8498
8499 return entry;
8500}
8501
c152c796
AM
8502/* This struct is used to pass information to elf_link_output_extsym. */
8503
8504struct elf_outext_info
8505{
8506 bfd_boolean failed;
8507 bfd_boolean localsyms;
34a79995 8508 bfd_boolean file_sym_done;
8b127cbc 8509 struct elf_final_link_info *flinfo;
c152c796
AM
8510};
8511
d9352518
DB
8512
8513/* Support for evaluating a complex relocation.
8514
8515 Complex relocations are generalized, self-describing relocations. The
8516 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8517 relocations themselves.
d9352518 8518
4b69ce9b 8519 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8520 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8521 information (start bit, end bit, word width, etc) into the addend. This
8522 information is extracted from CGEN-generated operand tables within gas.
8523
4b69ce9b 8524 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8525 internal) representing prefix-notation expressions, including but not
8526 limited to those sorts of expressions normally encoded as addends in the
8527 addend field. The symbol mangling format is:
8528
8529 <node> := <literal>
07d6d2b8
AM
8530 | <unary-operator> ':' <node>
8531 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8532 ;
8533
8534 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8535 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8536 | '#' <hexdigits>
8537 ;
8538
8539 <binary-operator> := as in C
8540 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8541
8542static void
a0c8462f
AM
8543set_symbol_value (bfd *bfd_with_globals,
8544 Elf_Internal_Sym *isymbuf,
8545 size_t locsymcount,
8546 size_t symidx,
8547 bfd_vma val)
d9352518 8548{
8977835c
AM
8549 struct elf_link_hash_entry **sym_hashes;
8550 struct elf_link_hash_entry *h;
8551 size_t extsymoff = locsymcount;
d9352518 8552
8977835c 8553 if (symidx < locsymcount)
d9352518 8554 {
8977835c
AM
8555 Elf_Internal_Sym *sym;
8556
8557 sym = isymbuf + symidx;
8558 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8559 {
8560 /* It is a local symbol: move it to the
8561 "absolute" section and give it a value. */
8562 sym->st_shndx = SHN_ABS;
8563 sym->st_value = val;
8564 return;
8565 }
8566 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8567 extsymoff = 0;
d9352518 8568 }
8977835c
AM
8569
8570 /* It is a global symbol: set its link type
8571 to "defined" and give it a value. */
8572
8573 sym_hashes = elf_sym_hashes (bfd_with_globals);
8574 h = sym_hashes [symidx - extsymoff];
8575 while (h->root.type == bfd_link_hash_indirect
8576 || h->root.type == bfd_link_hash_warning)
8577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8578 h->root.type = bfd_link_hash_defined;
8579 h->root.u.def.value = val;
8580 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8581}
8582
a0c8462f
AM
8583static bfd_boolean
8584resolve_symbol (const char *name,
8585 bfd *input_bfd,
8b127cbc 8586 struct elf_final_link_info *flinfo,
a0c8462f
AM
8587 bfd_vma *result,
8588 Elf_Internal_Sym *isymbuf,
8589 size_t locsymcount)
d9352518 8590{
a0c8462f
AM
8591 Elf_Internal_Sym *sym;
8592 struct bfd_link_hash_entry *global_entry;
8593 const char *candidate = NULL;
8594 Elf_Internal_Shdr *symtab_hdr;
8595 size_t i;
8596
d9352518
DB
8597 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8598
8599 for (i = 0; i < locsymcount; ++ i)
8600 {
8977835c 8601 sym = isymbuf + i;
d9352518
DB
8602
8603 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8604 continue;
8605
8606 candidate = bfd_elf_string_from_elf_section (input_bfd,
8607 symtab_hdr->sh_link,
8608 sym->st_name);
8609#ifdef DEBUG
0f02bbd9
AM
8610 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8611 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8612#endif
8613 if (candidate && strcmp (candidate, name) == 0)
8614 {
8b127cbc 8615 asection *sec = flinfo->sections [i];
d9352518 8616
0f02bbd9
AM
8617 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8618 *result += sec->output_offset + sec->output_section->vma;
d9352518 8619#ifdef DEBUG
0f02bbd9
AM
8620 printf ("Found symbol with value %8.8lx\n",
8621 (unsigned long) *result);
d9352518
DB
8622#endif
8623 return TRUE;
8624 }
8625 }
8626
8627 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8628 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8629 FALSE, FALSE, TRUE);
d9352518
DB
8630 if (!global_entry)
8631 return FALSE;
a0c8462f 8632
d9352518
DB
8633 if (global_entry->type == bfd_link_hash_defined
8634 || global_entry->type == bfd_link_hash_defweak)
8635 {
a0c8462f
AM
8636 *result = (global_entry->u.def.value
8637 + global_entry->u.def.section->output_section->vma
8638 + global_entry->u.def.section->output_offset);
d9352518 8639#ifdef DEBUG
0f02bbd9
AM
8640 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8641 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8642#endif
8643 return TRUE;
a0c8462f 8644 }
d9352518 8645
d9352518
DB
8646 return FALSE;
8647}
8648
37b01f6a
DG
8649/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8650 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8651 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8652
d9352518 8653static bfd_boolean
a0c8462f
AM
8654resolve_section (const char *name,
8655 asection *sections,
37b01f6a
DG
8656 bfd_vma *result,
8657 bfd * abfd)
d9352518 8658{
a0c8462f
AM
8659 asection *curr;
8660 unsigned int len;
d9352518 8661
a0c8462f 8662 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8663 if (strcmp (curr->name, name) == 0)
8664 {
8665 *result = curr->vma;
8666 return TRUE;
8667 }
8668
8669 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8670 /* FIXME: This could be coded more efficiently... */
a0c8462f 8671 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8672 {
8673 len = strlen (curr->name);
a0c8462f 8674 if (len > strlen (name))
d9352518
DB
8675 continue;
8676
8677 if (strncmp (curr->name, name, len) == 0)
8678 {
8679 if (strncmp (".end", name + len, 4) == 0)
8680 {
61826503 8681 *result = (curr->vma
bb294208 8682 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8683 return TRUE;
8684 }
8685
8686 /* Insert more pseudo-section names here, if you like. */
8687 }
8688 }
a0c8462f 8689
d9352518
DB
8690 return FALSE;
8691}
8692
8693static void
a0c8462f 8694undefined_reference (const char *reftype, const char *name)
d9352518 8695{
695344c0 8696 /* xgettext:c-format */
a0c8462f
AM
8697 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8698 reftype, name);
4b69ce9b 8699 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8700}
8701
8702static bfd_boolean
a0c8462f
AM
8703eval_symbol (bfd_vma *result,
8704 const char **symp,
8705 bfd *input_bfd,
8b127cbc 8706 struct elf_final_link_info *flinfo,
a0c8462f
AM
8707 bfd_vma dot,
8708 Elf_Internal_Sym *isymbuf,
8709 size_t locsymcount,
8710 int signed_p)
d9352518 8711{
4b93929b
NC
8712 size_t len;
8713 size_t symlen;
a0c8462f
AM
8714 bfd_vma a;
8715 bfd_vma b;
4b93929b 8716 char symbuf[4096];
0f02bbd9 8717 const char *sym = *symp;
a0c8462f
AM
8718 const char *symend;
8719 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8720
8721 len = strlen (sym);
8722 symend = sym + len;
8723
4b93929b 8724 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8725 {
8726 bfd_set_error (bfd_error_invalid_operation);
8727 return FALSE;
8728 }
a0c8462f 8729
d9352518
DB
8730 switch (* sym)
8731 {
8732 case '.':
0f02bbd9
AM
8733 *result = dot;
8734 *symp = sym + 1;
d9352518
DB
8735 return TRUE;
8736
8737 case '#':
0f02bbd9
AM
8738 ++sym;
8739 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8740 return TRUE;
8741
8742 case 'S':
8743 symbol_is_section = TRUE;
1a0670f3 8744 /* Fall through. */
a0c8462f 8745 case 's':
0f02bbd9
AM
8746 ++sym;
8747 symlen = strtol (sym, (char **) symp, 10);
8748 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8749
4b93929b 8750 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8751 {
8752 bfd_set_error (bfd_error_invalid_operation);
8753 return FALSE;
8754 }
8755
8756 memcpy (symbuf, sym, symlen);
a0c8462f 8757 symbuf[symlen] = '\0';
0f02bbd9 8758 *symp = sym + symlen;
a0c8462f
AM
8759
8760 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8761 the symbol as a section, or vice-versa. so we're pretty liberal in our
8762 interpretation here; section means "try section first", not "must be a
8763 section", and likewise with symbol. */
8764
a0c8462f 8765 if (symbol_is_section)
d9352518 8766 {
37b01f6a 8767 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8768 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8769 isymbuf, locsymcount))
d9352518
DB
8770 {
8771 undefined_reference ("section", symbuf);
8772 return FALSE;
8773 }
a0c8462f
AM
8774 }
8775 else
d9352518 8776 {
8b127cbc 8777 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8778 isymbuf, locsymcount)
8b127cbc 8779 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8780 result, input_bfd))
d9352518
DB
8781 {
8782 undefined_reference ("symbol", symbuf);
8783 return FALSE;
8784 }
8785 }
8786
8787 return TRUE;
a0c8462f 8788
d9352518
DB
8789 /* All that remains are operators. */
8790
8791#define UNARY_OP(op) \
8792 if (strncmp (sym, #op, strlen (#op)) == 0) \
8793 { \
8794 sym += strlen (#op); \
a0c8462f
AM
8795 if (*sym == ':') \
8796 ++sym; \
0f02bbd9 8797 *symp = sym; \
8b127cbc 8798 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8799 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8800 return FALSE; \
8801 if (signed_p) \
0f02bbd9 8802 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8803 else \
8804 *result = op a; \
d9352518
DB
8805 return TRUE; \
8806 }
8807
4b69ce9b 8808#define BINARY_OP_HEAD(op) \
d9352518
DB
8809 if (strncmp (sym, #op, strlen (#op)) == 0) \
8810 { \
8811 sym += strlen (#op); \
a0c8462f
AM
8812 if (*sym == ':') \
8813 ++sym; \
0f02bbd9 8814 *symp = sym; \
8b127cbc 8815 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8816 isymbuf, locsymcount, signed_p)) \
a0c8462f 8817 return FALSE; \
0f02bbd9 8818 ++*symp; \
8b127cbc 8819 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8820 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8821 return FALSE;
8822#define BINARY_OP_TAIL(op) \
a0c8462f 8823 if (signed_p) \
0f02bbd9 8824 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8825 else \
8826 *result = a op b; \
d9352518
DB
8827 return TRUE; \
8828 }
4b69ce9b 8829#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8830
8831 default:
8832 UNARY_OP (0-);
4b69ce9b
AM
8833 BINARY_OP_HEAD (<<);
8834 if (b >= sizeof (a) * CHAR_BIT)
8835 {
8836 *result = 0;
8837 return TRUE;
8838 }
8839 signed_p = 0;
8840 BINARY_OP_TAIL (<<);
8841 BINARY_OP_HEAD (>>);
8842 if (b >= sizeof (a) * CHAR_BIT)
8843 {
8844 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8845 return TRUE;
8846 }
8847 BINARY_OP_TAIL (>>);
d9352518
DB
8848 BINARY_OP (==);
8849 BINARY_OP (!=);
8850 BINARY_OP (<=);
8851 BINARY_OP (>=);
8852 BINARY_OP (&&);
8853 BINARY_OP (||);
8854 UNARY_OP (~);
8855 UNARY_OP (!);
8856 BINARY_OP (*);
4b69ce9b
AM
8857 BINARY_OP_HEAD (/);
8858 if (b == 0)
8859 {
8860 _bfd_error_handler (_("division by zero"));
8861 bfd_set_error (bfd_error_bad_value);
8862 return FALSE;
8863 }
8864 BINARY_OP_TAIL (/);
8865 BINARY_OP_HEAD (%);
8866 if (b == 0)
8867 {
8868 _bfd_error_handler (_("division by zero"));
8869 bfd_set_error (bfd_error_bad_value);
8870 return FALSE;
8871 }
8872 BINARY_OP_TAIL (%);
d9352518
DB
8873 BINARY_OP (^);
8874 BINARY_OP (|);
8875 BINARY_OP (&);
8876 BINARY_OP (+);
8877 BINARY_OP (-);
8878 BINARY_OP (<);
8879 BINARY_OP (>);
8880#undef UNARY_OP
8881#undef BINARY_OP
8882 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8883 bfd_set_error (bfd_error_invalid_operation);
8884 return FALSE;
8885 }
8886}
8887
d9352518 8888static void
a0c8462f
AM
8889put_value (bfd_vma size,
8890 unsigned long chunksz,
8891 bfd *input_bfd,
8892 bfd_vma x,
8893 bfd_byte *location)
d9352518
DB
8894{
8895 location += (size - chunksz);
8896
41cd1ad1 8897 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8898 {
8899 switch (chunksz)
8900 {
d9352518
DB
8901 case 1:
8902 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8903 x >>= 8;
d9352518
DB
8904 break;
8905 case 2:
8906 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8907 x >>= 16;
d9352518
DB
8908 break;
8909 case 4:
8910 bfd_put_32 (input_bfd, x, location);
65164438
NC
8911 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8912 x >>= 16;
8913 x >>= 16;
d9352518 8914 break;
d9352518 8915#ifdef BFD64
41cd1ad1 8916 case 8:
d9352518 8917 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8918 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8919 x >>= 32;
8920 x >>= 32;
8921 break;
d9352518 8922#endif
41cd1ad1
NC
8923 default:
8924 abort ();
d9352518
DB
8925 break;
8926 }
8927 }
8928}
8929
a0c8462f
AM
8930static bfd_vma
8931get_value (bfd_vma size,
8932 unsigned long chunksz,
8933 bfd *input_bfd,
8934 bfd_byte *location)
d9352518 8935{
9b239e0e 8936 int shift;
d9352518
DB
8937 bfd_vma x = 0;
8938
9b239e0e
NC
8939 /* Sanity checks. */
8940 BFD_ASSERT (chunksz <= sizeof (x)
8941 && size >= chunksz
8942 && chunksz != 0
8943 && (size % chunksz) == 0
8944 && input_bfd != NULL
8945 && location != NULL);
8946
8947 if (chunksz == sizeof (x))
8948 {
8949 BFD_ASSERT (size == chunksz);
8950
8951 /* Make sure that we do not perform an undefined shift operation.
8952 We know that size == chunksz so there will only be one iteration
8953 of the loop below. */
8954 shift = 0;
8955 }
8956 else
8957 shift = 8 * chunksz;
8958
a0c8462f 8959 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8960 {
8961 switch (chunksz)
8962 {
d9352518 8963 case 1:
9b239e0e 8964 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8965 break;
8966 case 2:
9b239e0e 8967 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8968 break;
8969 case 4:
9b239e0e 8970 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8971 break;
d9352518 8972#ifdef BFD64
9b239e0e
NC
8973 case 8:
8974 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8975 break;
9b239e0e
NC
8976#endif
8977 default:
8978 abort ();
d9352518
DB
8979 }
8980 }
8981 return x;
8982}
8983
a0c8462f
AM
8984static void
8985decode_complex_addend (unsigned long *start, /* in bits */
8986 unsigned long *oplen, /* in bits */
8987 unsigned long *len, /* in bits */
8988 unsigned long *wordsz, /* in bytes */
8989 unsigned long *chunksz, /* in bytes */
8990 unsigned long *lsb0_p,
8991 unsigned long *signed_p,
8992 unsigned long *trunc_p,
8993 unsigned long encoded)
d9352518 8994{
07d6d2b8
AM
8995 * start = encoded & 0x3F;
8996 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8997 * oplen = (encoded >> 12) & 0x3F;
8998 * wordsz = (encoded >> 18) & 0xF;
8999 * chunksz = (encoded >> 22) & 0xF;
9000 * lsb0_p = (encoded >> 27) & 1;
9001 * signed_p = (encoded >> 28) & 1;
9002 * trunc_p = (encoded >> 29) & 1;
9003}
9004
cdfeee4f 9005bfd_reloc_status_type
0f02bbd9 9006bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9007 asection *input_section,
0f02bbd9
AM
9008 bfd_byte *contents,
9009 Elf_Internal_Rela *rel,
9010 bfd_vma relocation)
d9352518 9011{
0f02bbd9
AM
9012 bfd_vma shift, x, mask;
9013 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9014 bfd_reloc_status_type r;
bb294208 9015 bfd_size_type octets;
d9352518
DB
9016
9017 /* Perform this reloc, since it is complex.
9018 (this is not to say that it necessarily refers to a complex
9019 symbol; merely that it is a self-describing CGEN based reloc.
9020 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9021 word size, etc) encoded within it.). */
d9352518 9022
a0c8462f
AM
9023 decode_complex_addend (&start, &oplen, &len, &wordsz,
9024 &chunksz, &lsb0_p, &signed_p,
9025 &trunc_p, rel->r_addend);
d9352518
DB
9026
9027 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9028
9029 if (lsb0_p)
9030 shift = (start + 1) - len;
9031 else
9032 shift = (8 * wordsz) - (start + len);
9033
bb294208
AM
9034 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9035 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9036
9037#ifdef DEBUG
9038 printf ("Doing complex reloc: "
9039 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9040 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9041 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9042 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9043 oplen, (unsigned long) x, (unsigned long) mask,
9044 (unsigned long) relocation);
d9352518
DB
9045#endif
9046
cdfeee4f 9047 r = bfd_reloc_ok;
d9352518 9048 if (! trunc_p)
cdfeee4f
AM
9049 /* Now do an overflow check. */
9050 r = bfd_check_overflow ((signed_p
9051 ? complain_overflow_signed
9052 : complain_overflow_unsigned),
9053 len, 0, (8 * wordsz),
9054 relocation);
a0c8462f 9055
d9352518
DB
9056 /* Do the deed. */
9057 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9058
9059#ifdef DEBUG
9060 printf (" relocation: %8.8lx\n"
9061 " shifted mask: %8.8lx\n"
9062 " shifted/masked reloc: %8.8lx\n"
9063 " result: %8.8lx\n",
9ccb8af9
AM
9064 (unsigned long) relocation, (unsigned long) (mask << shift),
9065 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9066#endif
bb294208 9067 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9068 return r;
d9352518
DB
9069}
9070
0e287786
AM
9071/* Functions to read r_offset from external (target order) reloc
9072 entry. Faster than bfd_getl32 et al, because we let the compiler
9073 know the value is aligned. */
53df40a4 9074
0e287786
AM
9075static bfd_vma
9076ext32l_r_offset (const void *p)
53df40a4
AM
9077{
9078 union aligned32
9079 {
9080 uint32_t v;
9081 unsigned char c[4];
9082 };
9083 const union aligned32 *a
0e287786 9084 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9085
9086 uint32_t aval = ( (uint32_t) a->c[0]
9087 | (uint32_t) a->c[1] << 8
9088 | (uint32_t) a->c[2] << 16
9089 | (uint32_t) a->c[3] << 24);
0e287786 9090 return aval;
53df40a4
AM
9091}
9092
0e287786
AM
9093static bfd_vma
9094ext32b_r_offset (const void *p)
53df40a4
AM
9095{
9096 union aligned32
9097 {
9098 uint32_t v;
9099 unsigned char c[4];
9100 };
9101 const union aligned32 *a
0e287786 9102 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9103
9104 uint32_t aval = ( (uint32_t) a->c[0] << 24
9105 | (uint32_t) a->c[1] << 16
9106 | (uint32_t) a->c[2] << 8
9107 | (uint32_t) a->c[3]);
0e287786 9108 return aval;
53df40a4
AM
9109}
9110
9111#ifdef BFD_HOST_64_BIT
0e287786
AM
9112static bfd_vma
9113ext64l_r_offset (const void *p)
53df40a4
AM
9114{
9115 union aligned64
9116 {
9117 uint64_t v;
9118 unsigned char c[8];
9119 };
9120 const union aligned64 *a
0e287786 9121 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9122
9123 uint64_t aval = ( (uint64_t) a->c[0]
9124 | (uint64_t) a->c[1] << 8
9125 | (uint64_t) a->c[2] << 16
9126 | (uint64_t) a->c[3] << 24
9127 | (uint64_t) a->c[4] << 32
9128 | (uint64_t) a->c[5] << 40
9129 | (uint64_t) a->c[6] << 48
9130 | (uint64_t) a->c[7] << 56);
0e287786 9131 return aval;
53df40a4
AM
9132}
9133
0e287786
AM
9134static bfd_vma
9135ext64b_r_offset (const void *p)
53df40a4
AM
9136{
9137 union aligned64
9138 {
9139 uint64_t v;
9140 unsigned char c[8];
9141 };
9142 const union aligned64 *a
0e287786 9143 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9144
9145 uint64_t aval = ( (uint64_t) a->c[0] << 56
9146 | (uint64_t) a->c[1] << 48
9147 | (uint64_t) a->c[2] << 40
9148 | (uint64_t) a->c[3] << 32
9149 | (uint64_t) a->c[4] << 24
9150 | (uint64_t) a->c[5] << 16
9151 | (uint64_t) a->c[6] << 8
9152 | (uint64_t) a->c[7]);
0e287786 9153 return aval;
53df40a4
AM
9154}
9155#endif
9156
c152c796
AM
9157/* When performing a relocatable link, the input relocations are
9158 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9159 referenced must be updated. Update all the relocations found in
9160 RELDATA. */
c152c796 9161
bca6d0e3 9162static bfd_boolean
c152c796 9163elf_link_adjust_relocs (bfd *abfd,
9eaff861 9164 asection *sec,
28dbcedc 9165 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9166 bfd_boolean sort,
9167 struct bfd_link_info *info)
c152c796
AM
9168{
9169 unsigned int i;
9170 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9171 bfd_byte *erela;
9172 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9173 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9174 bfd_vma r_type_mask;
9175 int r_sym_shift;
d4730f92
BS
9176 unsigned int count = reldata->count;
9177 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9178
d4730f92 9179 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9180 {
9181 swap_in = bed->s->swap_reloc_in;
9182 swap_out = bed->s->swap_reloc_out;
9183 }
d4730f92 9184 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9185 {
9186 swap_in = bed->s->swap_reloca_in;
9187 swap_out = bed->s->swap_reloca_out;
9188 }
9189 else
9190 abort ();
9191
9192 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9193 abort ();
9194
9195 if (bed->s->arch_size == 32)
9196 {
9197 r_type_mask = 0xff;
9198 r_sym_shift = 8;
9199 }
9200 else
9201 {
9202 r_type_mask = 0xffffffff;
9203 r_sym_shift = 32;
9204 }
9205
d4730f92
BS
9206 erela = reldata->hdr->contents;
9207 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9208 {
9209 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9210 unsigned int j;
9211
9212 if (*rel_hash == NULL)
9213 continue;
9214
10bbbc1d
NC
9215 if ((*rel_hash)->indx == -2
9216 && info->gc_sections
9217 && ! info->gc_keep_exported)
9218 {
9219 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9220 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9221 abfd, sec,
9222 (*rel_hash)->root.root.string);
9793eb77 9223 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9224 abfd, sec);
10bbbc1d
NC
9225 bfd_set_error (bfd_error_invalid_operation);
9226 return FALSE;
9227 }
c152c796
AM
9228 BFD_ASSERT ((*rel_hash)->indx >= 0);
9229
9230 (*swap_in) (abfd, erela, irela);
9231 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9232 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9233 | (irela[j].r_info & r_type_mask));
9234 (*swap_out) (abfd, irela, erela);
9235 }
53df40a4 9236
9eaff861
AO
9237 if (bed->elf_backend_update_relocs)
9238 (*bed->elf_backend_update_relocs) (sec, reldata);
9239
0e287786 9240 if (sort && count != 0)
53df40a4 9241 {
0e287786
AM
9242 bfd_vma (*ext_r_off) (const void *);
9243 bfd_vma r_off;
9244 size_t elt_size;
9245 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9246 bfd_byte *buf = NULL;
28dbcedc
AM
9247
9248 if (bed->s->arch_size == 32)
9249 {
9250 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9251 ext_r_off = ext32l_r_offset;
28dbcedc 9252 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9253 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9254 else
9255 abort ();
9256 }
53df40a4 9257 else
28dbcedc 9258 {
53df40a4 9259#ifdef BFD_HOST_64_BIT
28dbcedc 9260 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9261 ext_r_off = ext64l_r_offset;
28dbcedc 9262 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9263 ext_r_off = ext64b_r_offset;
28dbcedc 9264 else
53df40a4 9265#endif
28dbcedc
AM
9266 abort ();
9267 }
0e287786 9268
bca6d0e3
AM
9269 /* Must use a stable sort here. A modified insertion sort,
9270 since the relocs are mostly sorted already. */
0e287786
AM
9271 elt_size = reldata->hdr->sh_entsize;
9272 base = reldata->hdr->contents;
9273 end = base + count * elt_size;
bca6d0e3 9274 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9275 abort ();
9276
9277 /* Ensure the first element is lowest. This acts as a sentinel,
9278 speeding the main loop below. */
9279 r_off = (*ext_r_off) (base);
9280 for (p = loc = base; (p += elt_size) < end; )
9281 {
9282 bfd_vma r_off2 = (*ext_r_off) (p);
9283 if (r_off > r_off2)
9284 {
9285 r_off = r_off2;
9286 loc = p;
9287 }
9288 }
9289 if (loc != base)
9290 {
9291 /* Don't just swap *base and *loc as that changes the order
9292 of the original base[0] and base[1] if they happen to
9293 have the same r_offset. */
bca6d0e3
AM
9294 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9295 memcpy (onebuf, loc, elt_size);
0e287786 9296 memmove (base + elt_size, base, loc - base);
bca6d0e3 9297 memcpy (base, onebuf, elt_size);
0e287786
AM
9298 }
9299
b29b8669 9300 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9301 {
9302 /* base to p is sorted, *p is next to insert. */
9303 r_off = (*ext_r_off) (p);
9304 /* Search the sorted region for location to insert. */
9305 loc = p - elt_size;
9306 while (r_off < (*ext_r_off) (loc))
9307 loc -= elt_size;
9308 loc += elt_size;
9309 if (loc != p)
9310 {
bca6d0e3
AM
9311 /* Chances are there is a run of relocs to insert here,
9312 from one of more input files. Files are not always
9313 linked in order due to the way elf_link_input_bfd is
9314 called. See pr17666. */
9315 size_t sortlen = p - loc;
9316 bfd_vma r_off2 = (*ext_r_off) (loc);
9317 size_t runlen = elt_size;
9318 size_t buf_size = 96 * 1024;
9319 while (p + runlen < end
9320 && (sortlen <= buf_size
9321 || runlen + elt_size <= buf_size)
9322 && r_off2 > (*ext_r_off) (p + runlen))
9323 runlen += elt_size;
9324 if (buf == NULL)
9325 {
9326 buf = bfd_malloc (buf_size);
9327 if (buf == NULL)
9328 return FALSE;
9329 }
9330 if (runlen < sortlen)
9331 {
9332 memcpy (buf, p, runlen);
9333 memmove (loc + runlen, loc, sortlen);
9334 memcpy (loc, buf, runlen);
9335 }
9336 else
9337 {
9338 memcpy (buf, loc, sortlen);
9339 memmove (loc, p, runlen);
9340 memcpy (loc + runlen, buf, sortlen);
9341 }
b29b8669 9342 p += runlen - elt_size;
0e287786
AM
9343 }
9344 }
9345 /* Hashes are no longer valid. */
28dbcedc
AM
9346 free (reldata->hashes);
9347 reldata->hashes = NULL;
bca6d0e3 9348 free (buf);
53df40a4 9349 }
bca6d0e3 9350 return TRUE;
c152c796
AM
9351}
9352
9353struct elf_link_sort_rela
9354{
9355 union {
9356 bfd_vma offset;
9357 bfd_vma sym_mask;
9358 } u;
9359 enum elf_reloc_type_class type;
9360 /* We use this as an array of size int_rels_per_ext_rel. */
9361 Elf_Internal_Rela rela[1];
9362};
9363
dcea6a95
AM
9364/* qsort stability here and for cmp2 is only an issue if multiple
9365 dynamic relocations are emitted at the same address. But targets
9366 that apply a series of dynamic relocations each operating on the
9367 result of the prior relocation can't use -z combreloc as
9368 implemented anyway. Such schemes tend to be broken by sorting on
9369 symbol index. That leaves dynamic NONE relocs as the only other
9370 case where ld might emit multiple relocs at the same address, and
9371 those are only emitted due to target bugs. */
9372
c152c796
AM
9373static int
9374elf_link_sort_cmp1 (const void *A, const void *B)
9375{
a50b1753
NC
9376 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9377 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9378 int relativea, relativeb;
9379
9380 relativea = a->type == reloc_class_relative;
9381 relativeb = b->type == reloc_class_relative;
9382
9383 if (relativea < relativeb)
9384 return 1;
9385 if (relativea > relativeb)
9386 return -1;
9387 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9388 return -1;
9389 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9390 return 1;
9391 if (a->rela->r_offset < b->rela->r_offset)
9392 return -1;
9393 if (a->rela->r_offset > b->rela->r_offset)
9394 return 1;
9395 return 0;
9396}
9397
9398static int
9399elf_link_sort_cmp2 (const void *A, const void *B)
9400{
a50b1753
NC
9401 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9402 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9403
7e612e98 9404 if (a->type < b->type)
c152c796 9405 return -1;
7e612e98 9406 if (a->type > b->type)
c152c796 9407 return 1;
7e612e98 9408 if (a->u.offset < b->u.offset)
c152c796 9409 return -1;
7e612e98 9410 if (a->u.offset > b->u.offset)
c152c796
AM
9411 return 1;
9412 if (a->rela->r_offset < b->rela->r_offset)
9413 return -1;
9414 if (a->rela->r_offset > b->rela->r_offset)
9415 return 1;
9416 return 0;
9417}
9418
9419static size_t
9420elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9421{
3410fea8 9422 asection *dynamic_relocs;
fc66a176
L
9423 asection *rela_dyn;
9424 asection *rel_dyn;
c152c796
AM
9425 bfd_size_type count, size;
9426 size_t i, ret, sort_elt, ext_size;
9427 bfd_byte *sort, *s_non_relative, *p;
9428 struct elf_link_sort_rela *sq;
9429 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9430 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9431 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9432 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9433 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9434 struct bfd_link_order *lo;
9435 bfd_vma r_sym_mask;
3410fea8 9436 bfd_boolean use_rela;
c152c796 9437
3410fea8
NC
9438 /* Find a dynamic reloc section. */
9439 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9440 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9441 if (rela_dyn != NULL && rela_dyn->size > 0
9442 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9443 {
3410fea8
NC
9444 bfd_boolean use_rela_initialised = FALSE;
9445
9446 /* This is just here to stop gcc from complaining.
c8e44c6d 9447 Its initialization checking code is not perfect. */
3410fea8
NC
9448 use_rela = TRUE;
9449
9450 /* Both sections are present. Examine the sizes
9451 of the indirect sections to help us choose. */
9452 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9453 if (lo->type == bfd_indirect_link_order)
9454 {
9455 asection *o = lo->u.indirect.section;
9456
9457 if ((o->size % bed->s->sizeof_rela) == 0)
9458 {
9459 if ((o->size % bed->s->sizeof_rel) == 0)
9460 /* Section size is divisible by both rel and rela sizes.
9461 It is of no help to us. */
9462 ;
9463 else
9464 {
9465 /* Section size is only divisible by rela. */
535b785f 9466 if (use_rela_initialised && !use_rela)
3410fea8 9467 {
9793eb77 9468 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9469 "they are in more than one size"),
9470 abfd);
3410fea8
NC
9471 bfd_set_error (bfd_error_invalid_operation);
9472 return 0;
9473 }
9474 else
9475 {
9476 use_rela = TRUE;
9477 use_rela_initialised = TRUE;
9478 }
9479 }
9480 }
9481 else if ((o->size % bed->s->sizeof_rel) == 0)
9482 {
9483 /* Section size is only divisible by rel. */
535b785f 9484 if (use_rela_initialised && use_rela)
3410fea8 9485 {
9793eb77 9486 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9487 "they are in more than one size"),
9488 abfd);
3410fea8
NC
9489 bfd_set_error (bfd_error_invalid_operation);
9490 return 0;
9491 }
9492 else
9493 {
9494 use_rela = FALSE;
9495 use_rela_initialised = TRUE;
9496 }
9497 }
9498 else
9499 {
c8e44c6d
AM
9500 /* The section size is not divisible by either -
9501 something is wrong. */
9793eb77 9502 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9503 "they are of an unknown size"), abfd);
3410fea8
NC
9504 bfd_set_error (bfd_error_invalid_operation);
9505 return 0;
9506 }
9507 }
9508
9509 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9510 if (lo->type == bfd_indirect_link_order)
9511 {
9512 asection *o = lo->u.indirect.section;
9513
9514 if ((o->size % bed->s->sizeof_rela) == 0)
9515 {
9516 if ((o->size % bed->s->sizeof_rel) == 0)
9517 /* Section size is divisible by both rel and rela sizes.
9518 It is of no help to us. */
9519 ;
9520 else
9521 {
9522 /* Section size is only divisible by rela. */
535b785f 9523 if (use_rela_initialised && !use_rela)
3410fea8 9524 {
9793eb77 9525 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9526 "they are in more than one size"),
9527 abfd);
3410fea8
NC
9528 bfd_set_error (bfd_error_invalid_operation);
9529 return 0;
9530 }
9531 else
9532 {
9533 use_rela = TRUE;
9534 use_rela_initialised = TRUE;
9535 }
9536 }
9537 }
9538 else if ((o->size % bed->s->sizeof_rel) == 0)
9539 {
9540 /* Section size is only divisible by rel. */
535b785f 9541 if (use_rela_initialised && use_rela)
3410fea8 9542 {
9793eb77 9543 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9544 "they are in more than one size"),
9545 abfd);
3410fea8
NC
9546 bfd_set_error (bfd_error_invalid_operation);
9547 return 0;
9548 }
9549 else
9550 {
9551 use_rela = FALSE;
9552 use_rela_initialised = TRUE;
9553 }
9554 }
9555 else
9556 {
c8e44c6d
AM
9557 /* The section size is not divisible by either -
9558 something is wrong. */
9793eb77 9559 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9560 "they are of an unknown size"), abfd);
3410fea8
NC
9561 bfd_set_error (bfd_error_invalid_operation);
9562 return 0;
9563 }
9564 }
9565
9566 if (! use_rela_initialised)
9567 /* Make a guess. */
9568 use_rela = TRUE;
c152c796 9569 }
fc66a176
L
9570 else if (rela_dyn != NULL && rela_dyn->size > 0)
9571 use_rela = TRUE;
9572 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9573 use_rela = FALSE;
c152c796 9574 else
fc66a176 9575 return 0;
3410fea8
NC
9576
9577 if (use_rela)
c152c796 9578 {
3410fea8 9579 dynamic_relocs = rela_dyn;
c152c796
AM
9580 ext_size = bed->s->sizeof_rela;
9581 swap_in = bed->s->swap_reloca_in;
9582 swap_out = bed->s->swap_reloca_out;
9583 }
3410fea8
NC
9584 else
9585 {
9586 dynamic_relocs = rel_dyn;
9587 ext_size = bed->s->sizeof_rel;
9588 swap_in = bed->s->swap_reloc_in;
9589 swap_out = bed->s->swap_reloc_out;
9590 }
c152c796
AM
9591
9592 size = 0;
3410fea8 9593 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9594 if (lo->type == bfd_indirect_link_order)
3410fea8 9595 size += lo->u.indirect.section->size;
c152c796 9596
3410fea8 9597 if (size != dynamic_relocs->size)
c152c796
AM
9598 return 0;
9599
9600 sort_elt = (sizeof (struct elf_link_sort_rela)
9601 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9602
9603 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9604 if (count == 0)
9605 return 0;
a50b1753 9606 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9607
c152c796
AM
9608 if (sort == NULL)
9609 {
9610 (*info->callbacks->warning)
9793eb77 9611 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9612 return 0;
9613 }
9614
9615 if (bed->s->arch_size == 32)
9616 r_sym_mask = ~(bfd_vma) 0xff;
9617 else
9618 r_sym_mask = ~(bfd_vma) 0xffffffff;
9619
3410fea8 9620 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9621 if (lo->type == bfd_indirect_link_order)
9622 {
9623 bfd_byte *erel, *erelend;
9624 asection *o = lo->u.indirect.section;
9625
1da212d6
AM
9626 if (o->contents == NULL && o->size != 0)
9627 {
9628 /* This is a reloc section that is being handled as a normal
9629 section. See bfd_section_from_shdr. We can't combine
9630 relocs in this case. */
9631 free (sort);
9632 return 0;
9633 }
c152c796 9634 erel = o->contents;
eea6121a 9635 erelend = o->contents + o->size;
c8e44c6d 9636 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9637
c152c796
AM
9638 while (erel < erelend)
9639 {
9640 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9641
c152c796 9642 (*swap_in) (abfd, erel, s->rela);
7e612e98 9643 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9644 s->u.sym_mask = r_sym_mask;
9645 p += sort_elt;
9646 erel += ext_size;
9647 }
9648 }
9649
9650 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9651
9652 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9653 {
9654 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9655 if (s->type != reloc_class_relative)
9656 break;
9657 }
9658 ret = i;
9659 s_non_relative = p;
9660
9661 sq = (struct elf_link_sort_rela *) s_non_relative;
9662 for (; i < count; i++, p += sort_elt)
9663 {
9664 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9665 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9666 sq = sp;
9667 sp->u.offset = sq->rela->r_offset;
9668 }
9669
9670 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9671
c8e44c6d
AM
9672 struct elf_link_hash_table *htab = elf_hash_table (info);
9673 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9674 {
9675 /* We have plt relocs in .rela.dyn. */
9676 sq = (struct elf_link_sort_rela *) sort;
9677 for (i = 0; i < count; i++)
9678 if (sq[count - i - 1].type != reloc_class_plt)
9679 break;
9680 if (i != 0 && htab->srelplt->size == i * ext_size)
9681 {
9682 struct bfd_link_order **plo;
9683 /* Put srelplt link_order last. This is so the output_offset
9684 set in the next loop is correct for DT_JMPREL. */
9685 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9686 if ((*plo)->type == bfd_indirect_link_order
9687 && (*plo)->u.indirect.section == htab->srelplt)
9688 {
9689 lo = *plo;
9690 *plo = lo->next;
9691 }
9692 else
9693 plo = &(*plo)->next;
9694 *plo = lo;
9695 lo->next = NULL;
9696 dynamic_relocs->map_tail.link_order = lo;
9697 }
9698 }
9699
9700 p = sort;
3410fea8 9701 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9702 if (lo->type == bfd_indirect_link_order)
9703 {
9704 bfd_byte *erel, *erelend;
9705 asection *o = lo->u.indirect.section;
9706
9707 erel = o->contents;
eea6121a 9708 erelend = o->contents + o->size;
c8e44c6d 9709 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9710 while (erel < erelend)
9711 {
9712 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9713 (*swap_out) (abfd, s->rela, erel);
9714 p += sort_elt;
9715 erel += ext_size;
9716 }
9717 }
9718
9719 free (sort);
3410fea8 9720 *psec = dynamic_relocs;
c152c796
AM
9721 return ret;
9722}
9723
ef10c3ac 9724/* Add a symbol to the output symbol string table. */
c152c796 9725
6e0b88f1 9726static int
ef10c3ac
L
9727elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9728 const char *name,
9729 Elf_Internal_Sym *elfsym,
9730 asection *input_sec,
9731 struct elf_link_hash_entry *h)
c152c796 9732{
6e0b88f1 9733 int (*output_symbol_hook)
c152c796
AM
9734 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9735 struct elf_link_hash_entry *);
ef10c3ac 9736 struct elf_link_hash_table *hash_table;
c152c796 9737 const struct elf_backend_data *bed;
ef10c3ac 9738 bfd_size_type strtabsize;
c152c796 9739
8539e4e8
AM
9740 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9741
8b127cbc 9742 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9743 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9744 if (output_symbol_hook != NULL)
9745 {
8b127cbc 9746 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9747 if (ret != 1)
9748 return ret;
c152c796
AM
9749 }
9750
06f44071
AM
9751 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9752 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9753 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9754 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9755
ef10c3ac
L
9756 if (name == NULL
9757 || *name == '\0'
9758 || (input_sec->flags & SEC_EXCLUDE))
9759 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9760 else
9761 {
ef10c3ac
L
9762 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9763 to get the final offset for st_name. */
3f2e9699 9764 char *versioned_name = (char *) name;
496afd17
L
9765 if (h != NULL)
9766 {
9767 if (h->versioned == versioned && h->def_dynamic)
9768 {
9769 /* Keep only one '@' for versioned symbols defined in
9770 shared objects. */
9771 char *version = strrchr (name, ELF_VER_CHR);
9772 char *base_end = strchr (name, ELF_VER_CHR);
9773 if (version != base_end)
9774 {
9775 size_t base_len;
9776 size_t len = strlen (name);
9777 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9778 if (versioned_name == NULL)
9779 return 0;
9780 base_len = base_end - name;
9781 memcpy (versioned_name, name, base_len);
9782 memcpy (versioned_name + base_len, version,
9783 len - base_len);
9784 }
9785 }
9786 }
9787 else if (flinfo->info->unique_symbol
9788 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9789 {
496afd17
L
9790 struct local_hash_entry *lh;
9791 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9792 {
496afd17
L
9793 case STT_FILE:
9794 case STT_SECTION:
9795 break;
9796 default:
9797 lh = (struct local_hash_entry *) bfd_hash_lookup
9798 (&flinfo->local_hash_table, name, TRUE, FALSE);
9799 if (lh == NULL)
3f2e9699 9800 return 0;
496afd17
L
9801 if (lh->count)
9802 {
9803 /* Append ".COUNT" to duplicated local symbols. */
9804 size_t count_len;
9805 size_t base_len = lh->size;
9806 char buf[30];
9807 sprintf (buf, "%lx", lh->count);
9808 if (!base_len)
9809 {
9810 base_len = strlen (name);
9811 lh->size = base_len;
9812 }
9813 count_len = strlen (buf);
9814 versioned_name = bfd_alloc (flinfo->output_bfd,
9815 base_len + count_len + 2);
9816 if (versioned_name == NULL)
9817 return 0;
9818 memcpy (versioned_name, name, base_len);
9819 versioned_name[base_len] = '.';
9820 memcpy (versioned_name + base_len + 1, buf,
9821 count_len + 1);
9822 }
9823 lh->count++;
9824 break;
3f2e9699
L
9825 }
9826 }
ef10c3ac
L
9827 elfsym->st_name
9828 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9829 versioned_name, FALSE);
c152c796 9830 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9831 return 0;
c152c796
AM
9832 }
9833
ef10c3ac
L
9834 hash_table = elf_hash_table (flinfo->info);
9835 strtabsize = hash_table->strtabsize;
9836 if (strtabsize <= hash_table->strtabcount)
c152c796 9837 {
ef10c3ac
L
9838 strtabsize += strtabsize;
9839 hash_table->strtabsize = strtabsize;
9840 strtabsize *= sizeof (*hash_table->strtab);
9841 hash_table->strtab
9842 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9843 strtabsize);
9844 if (hash_table->strtab == NULL)
6e0b88f1 9845 return 0;
c152c796 9846 }
ef10c3ac
L
9847 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9848 hash_table->strtab[hash_table->strtabcount].dest_index
9849 = hash_table->strtabcount;
9850 hash_table->strtab[hash_table->strtabcount].destshndx_index
9851 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9852
ed48ec2e 9853 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9854 hash_table->strtabcount += 1;
9855
9856 return 1;
9857}
9858
9859/* Swap symbols out to the symbol table and flush the output symbols to
9860 the file. */
9861
9862static bfd_boolean
9863elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9864{
9865 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9866 size_t amt;
ef53be89 9867 size_t i;
ef10c3ac
L
9868 const struct elf_backend_data *bed;
9869 bfd_byte *symbuf;
9870 Elf_Internal_Shdr *hdr;
9871 file_ptr pos;
9872 bfd_boolean ret;
9873
9874 if (!hash_table->strtabcount)
9875 return TRUE;
9876
9877 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9878
9879 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9880
ef10c3ac
L
9881 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9882 symbuf = (bfd_byte *) bfd_malloc (amt);
9883 if (symbuf == NULL)
9884 return FALSE;
1b786873 9885
ef10c3ac 9886 if (flinfo->symshndxbuf)
c152c796 9887 {
ef53be89
AM
9888 amt = sizeof (Elf_External_Sym_Shndx);
9889 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9890 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9891 if (flinfo->symshndxbuf == NULL)
c152c796 9892 {
ef10c3ac
L
9893 free (symbuf);
9894 return FALSE;
c152c796 9895 }
c152c796
AM
9896 }
9897
3d16b64e 9898 /* Now swap out the symbols. */
ef10c3ac
L
9899 for (i = 0; i < hash_table->strtabcount; i++)
9900 {
9901 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9902 if (elfsym->sym.st_name == (unsigned long) -1)
9903 elfsym->sym.st_name = 0;
9904 else
9905 elfsym->sym.st_name
9906 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9907 elfsym->sym.st_name);
3d16b64e
NA
9908
9909 /* Inform the linker of the addition of this symbol. */
9910
9911 if (flinfo->info->callbacks->ctf_new_symbol)
9912 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9913 &elfsym->sym);
9914
ef10c3ac
L
9915 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9916 ((bfd_byte *) symbuf
9917 + (elfsym->dest_index
9918 * bed->s->sizeof_sym)),
9919 (flinfo->symshndxbuf
9920 + elfsym->destshndx_index));
9921 }
9922
9923 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9924 pos = hdr->sh_offset + hdr->sh_size;
9925 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9926 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9927 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9928 {
9929 hdr->sh_size += amt;
9930 ret = TRUE;
9931 }
9932 else
9933 ret = FALSE;
c152c796 9934
ef10c3ac
L
9935 free (symbuf);
9936
9937 free (hash_table->strtab);
9938 hash_table->strtab = NULL;
9939
9940 return ret;
c152c796
AM
9941}
9942
c0d5a53d
L
9943/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9944
9945static bfd_boolean
9946check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9947{
4fbb74a6
AM
9948 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9949 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9950 {
9951 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9952 beyond 64k. */
4eca0228 9953 _bfd_error_handler
695344c0 9954 /* xgettext:c-format */
9793eb77 9955 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9956 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9957 bfd_set_error (bfd_error_nonrepresentable_section);
9958 return FALSE;
9959 }
9960 return TRUE;
9961}
9962
c152c796
AM
9963/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9964 allowing an unsatisfied unversioned symbol in the DSO to match a
9965 versioned symbol that would normally require an explicit version.
9966 We also handle the case that a DSO references a hidden symbol
9967 which may be satisfied by a versioned symbol in another DSO. */
9968
9969static bfd_boolean
9970elf_link_check_versioned_symbol (struct bfd_link_info *info,
9971 const struct elf_backend_data *bed,
9972 struct elf_link_hash_entry *h)
9973{
9974 bfd *abfd;
9975 struct elf_link_loaded_list *loaded;
9976
9977 if (!is_elf_hash_table (info->hash))
9978 return FALSE;
9979
90c984fc
L
9980 /* Check indirect symbol. */
9981 while (h->root.type == bfd_link_hash_indirect)
9982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9983
c152c796
AM
9984 switch (h->root.type)
9985 {
9986 default:
9987 abfd = NULL;
9988 break;
9989
9990 case bfd_link_hash_undefined:
9991 case bfd_link_hash_undefweak:
9992 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9993 if (abfd == NULL
9994 || (abfd->flags & DYNAMIC) == 0
e56f61be 9995 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9996 return FALSE;
9997 break;
9998
9999 case bfd_link_hash_defined:
10000 case bfd_link_hash_defweak:
10001 abfd = h->root.u.def.section->owner;
10002 break;
10003
10004 case bfd_link_hash_common:
10005 abfd = h->root.u.c.p->section->owner;
10006 break;
10007 }
10008 BFD_ASSERT (abfd != NULL);
10009
e310298c 10010 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10011 loaded != NULL;
10012 loaded = loaded->next)
10013 {
10014 bfd *input;
10015 Elf_Internal_Shdr *hdr;
ef53be89
AM
10016 size_t symcount;
10017 size_t extsymcount;
10018 size_t extsymoff;
c152c796
AM
10019 Elf_Internal_Shdr *versymhdr;
10020 Elf_Internal_Sym *isym;
10021 Elf_Internal_Sym *isymend;
10022 Elf_Internal_Sym *isymbuf;
10023 Elf_External_Versym *ever;
10024 Elf_External_Versym *extversym;
10025
10026 input = loaded->abfd;
10027
10028 /* We check each DSO for a possible hidden versioned definition. */
10029 if (input == abfd
c152c796
AM
10030 || elf_dynversym (input) == 0)
10031 continue;
10032
10033 hdr = &elf_tdata (input)->dynsymtab_hdr;
10034
10035 symcount = hdr->sh_size / bed->s->sizeof_sym;
10036 if (elf_bad_symtab (input))
10037 {
10038 extsymcount = symcount;
10039 extsymoff = 0;
10040 }
10041 else
10042 {
10043 extsymcount = symcount - hdr->sh_info;
10044 extsymoff = hdr->sh_info;
10045 }
10046
10047 if (extsymcount == 0)
10048 continue;
10049
10050 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10051 NULL, NULL, NULL);
10052 if (isymbuf == NULL)
10053 return FALSE;
10054
10055 /* Read in any version definitions. */
10056 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10057 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10058 || (extversym = (Elf_External_Versym *)
10059 _bfd_malloc_and_read (input, versymhdr->sh_size,
10060 versymhdr->sh_size)) == NULL)
c152c796 10061 {
c152c796
AM
10062 free (isymbuf);
10063 return FALSE;
10064 }
10065
10066 ever = extversym + extsymoff;
10067 isymend = isymbuf + extsymcount;
10068 for (isym = isymbuf; isym < isymend; isym++, ever++)
10069 {
10070 const char *name;
10071 Elf_Internal_Versym iver;
10072 unsigned short version_index;
10073
10074 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10075 || isym->st_shndx == SHN_UNDEF)
10076 continue;
10077
10078 name = bfd_elf_string_from_elf_section (input,
10079 hdr->sh_link,
10080 isym->st_name);
10081 if (strcmp (name, h->root.root.string) != 0)
10082 continue;
10083
10084 _bfd_elf_swap_versym_in (input, ever, &iver);
10085
d023c380
L
10086 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10087 && !(h->def_regular
10088 && h->forced_local))
c152c796
AM
10089 {
10090 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10091 have provided a definition for the undefined sym unless
10092 it is defined in a non-shared object and forced local.
10093 */
c152c796
AM
10094 abort ();
10095 }
10096
10097 version_index = iver.vs_vers & VERSYM_VERSION;
10098 if (version_index == 1 || version_index == 2)
10099 {
10100 /* This is the base or first version. We can use it. */
10101 free (extversym);
10102 free (isymbuf);
10103 return TRUE;
10104 }
10105 }
10106
10107 free (extversym);
10108 free (isymbuf);
10109 }
10110
10111 return FALSE;
10112}
10113
b8871f35
L
10114/* Convert ELF common symbol TYPE. */
10115
10116static int
10117elf_link_convert_common_type (struct bfd_link_info *info, int type)
10118{
10119 /* Commom symbol can only appear in relocatable link. */
10120 if (!bfd_link_relocatable (info))
10121 abort ();
10122 switch (info->elf_stt_common)
10123 {
10124 case unchanged:
10125 break;
10126 case elf_stt_common:
10127 type = STT_COMMON;
10128 break;
10129 case no_elf_stt_common:
10130 type = STT_OBJECT;
10131 break;
10132 }
10133 return type;
10134}
10135
c152c796
AM
10136/* Add an external symbol to the symbol table. This is called from
10137 the hash table traversal routine. When generating a shared object,
10138 we go through the symbol table twice. The first time we output
10139 anything that might have been forced to local scope in a version
10140 script. The second time we output the symbols that are still
10141 global symbols. */
10142
10143static bfd_boolean
7686d77d 10144elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10145{
7686d77d 10146 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10147 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10148 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10149 bfd_boolean strip;
10150 Elf_Internal_Sym sym;
10151 asection *input_sec;
10152 const struct elf_backend_data *bed;
6e0b88f1
AM
10153 long indx;
10154 int ret;
b8871f35 10155 unsigned int type;
c152c796
AM
10156
10157 if (h->root.type == bfd_link_hash_warning)
10158 {
10159 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10160 if (h->root.type == bfd_link_hash_new)
10161 return TRUE;
10162 }
10163
10164 /* Decide whether to output this symbol in this pass. */
10165 if (eoinfo->localsyms)
10166 {
4deb8f71 10167 if (!h->forced_local)
c152c796
AM
10168 return TRUE;
10169 }
10170 else
10171 {
4deb8f71 10172 if (h->forced_local)
c152c796
AM
10173 return TRUE;
10174 }
10175
8b127cbc 10176 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10177
12ac1cf5 10178 if (h->root.type == bfd_link_hash_undefined)
c152c796 10179 {
12ac1cf5
NC
10180 /* If we have an undefined symbol reference here then it must have
10181 come from a shared library that is being linked in. (Undefined
98da7939
L
10182 references in regular files have already been handled unless
10183 they are in unreferenced sections which are removed by garbage
10184 collection). */
12ac1cf5
NC
10185 bfd_boolean ignore_undef = FALSE;
10186
10187 /* Some symbols may be special in that the fact that they're
10188 undefined can be safely ignored - let backend determine that. */
10189 if (bed->elf_backend_ignore_undef_symbol)
10190 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10191
10192 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10193 if (!ignore_undef
c54f1524 10194 && h->ref_dynamic_nonweak
8b127cbc
AM
10195 && (!h->ref_regular || flinfo->info->gc_sections)
10196 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10197 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10198 {
10199 flinfo->info->callbacks->undefined_symbol
10200 (flinfo->info, h->root.root.string,
10201 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10202 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10203 && !flinfo->info->warn_unresolved_syms);
10204 }
97196564
L
10205
10206 /* Strip a global symbol defined in a discarded section. */
10207 if (h->indx == -3)
10208 return TRUE;
c152c796
AM
10209 }
10210
10211 /* We should also warn if a forced local symbol is referenced from
10212 shared libraries. */
0e1862bb 10213 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10214 && h->forced_local
10215 && h->ref_dynamic
371a5866 10216 && h->def_regular
f5385ebf 10217 && !h->dynamic_def
ee659f1f 10218 && h->ref_dynamic_nonweak
8b127cbc 10219 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10220 {
17d078c5
AM
10221 bfd *def_bfd;
10222 const char *msg;
90c984fc
L
10223 struct elf_link_hash_entry *hi = h;
10224
10225 /* Check indirect symbol. */
10226 while (hi->root.type == bfd_link_hash_indirect)
10227 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10228
10229 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10230 /* xgettext:c-format */
871b3ab2 10231 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10232 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10233 /* xgettext:c-format */
871b3ab2 10234 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10235 else
695344c0 10236 /* xgettext:c-format */
871b3ab2 10237 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10238 def_bfd = flinfo->output_bfd;
90c984fc
L
10239 if (hi->root.u.def.section != bfd_abs_section_ptr)
10240 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10241 _bfd_error_handler (msg, flinfo->output_bfd,
10242 h->root.root.string, def_bfd);
17d078c5 10243 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10244 eoinfo->failed = TRUE;
10245 return FALSE;
10246 }
10247
10248 /* We don't want to output symbols that have never been mentioned by
10249 a regular file, or that we have been told to strip. However, if
10250 h->indx is set to -2, the symbol is used by a reloc and we must
10251 output it. */
d983c8c5 10252 strip = FALSE;
c152c796 10253 if (h->indx == -2)
d983c8c5 10254 ;
f5385ebf 10255 else if ((h->def_dynamic
77cfaee6
AM
10256 || h->ref_dynamic
10257 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10258 && !h->def_regular
10259 && !h->ref_regular)
c152c796 10260 strip = TRUE;
8b127cbc 10261 else if (flinfo->info->strip == strip_all)
c152c796 10262 strip = TRUE;
8b127cbc
AM
10263 else if (flinfo->info->strip == strip_some
10264 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10265 h->root.root.string, FALSE, FALSE) == NULL)
10266 strip = TRUE;
d56d55e7
AM
10267 else if ((h->root.type == bfd_link_hash_defined
10268 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10269 && ((flinfo->info->strip_discarded
dbaa2011 10270 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10271 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10272 && h->root.u.def.section->owner != NULL
d56d55e7 10273 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10274 strip = TRUE;
9e2278f5
AM
10275 else if ((h->root.type == bfd_link_hash_undefined
10276 || h->root.type == bfd_link_hash_undefweak)
10277 && h->root.u.undef.abfd != NULL
10278 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10279 strip = TRUE;
c152c796 10280
b8871f35
L
10281 type = h->type;
10282
c152c796 10283 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10284 nothing else to do. However, if it is a forced local symbol or
10285 an ifunc symbol we need to give the backend finish_dynamic_symbol
10286 function a chance to make it dynamic. */
c152c796
AM
10287 if (strip
10288 && h->dynindx == -1
b8871f35 10289 && type != STT_GNU_IFUNC
f5385ebf 10290 && !h->forced_local)
c152c796
AM
10291 return TRUE;
10292
10293 sym.st_value = 0;
10294 sym.st_size = h->size;
10295 sym.st_other = h->other;
c152c796
AM
10296 switch (h->root.type)
10297 {
10298 default:
10299 case bfd_link_hash_new:
10300 case bfd_link_hash_warning:
10301 abort ();
10302 return FALSE;
10303
10304 case bfd_link_hash_undefined:
10305 case bfd_link_hash_undefweak:
10306 input_sec = bfd_und_section_ptr;
10307 sym.st_shndx = SHN_UNDEF;
10308 break;
10309
10310 case bfd_link_hash_defined:
10311 case bfd_link_hash_defweak:
10312 {
10313 input_sec = h->root.u.def.section;
10314 if (input_sec->output_section != NULL)
10315 {
10316 sym.st_shndx =
8b127cbc 10317 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10318 input_sec->output_section);
10319 if (sym.st_shndx == SHN_BAD)
10320 {
4eca0228 10321 _bfd_error_handler
695344c0 10322 /* xgettext:c-format */
871b3ab2 10323 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10324 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10325 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10326 eoinfo->failed = TRUE;
10327 return FALSE;
10328 }
10329
10330 /* ELF symbols in relocatable files are section relative,
10331 but in nonrelocatable files they are virtual
10332 addresses. */
10333 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10334 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10335 {
10336 sym.st_value += input_sec->output_section->vma;
10337 if (h->type == STT_TLS)
10338 {
8b127cbc 10339 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10340 if (tls_sec != NULL)
10341 sym.st_value -= tls_sec->vma;
c152c796
AM
10342 }
10343 }
10344 }
10345 else
10346 {
10347 BFD_ASSERT (input_sec->owner == NULL
10348 || (input_sec->owner->flags & DYNAMIC) != 0);
10349 sym.st_shndx = SHN_UNDEF;
10350 input_sec = bfd_und_section_ptr;
10351 }
10352 }
10353 break;
10354
10355 case bfd_link_hash_common:
10356 input_sec = h->root.u.c.p->section;
a4d8e49b 10357 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10358 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10359 break;
10360
10361 case bfd_link_hash_indirect:
10362 /* These symbols are created by symbol versioning. They point
10363 to the decorated version of the name. For example, if the
10364 symbol foo@@GNU_1.2 is the default, which should be used when
10365 foo is used with no version, then we add an indirect symbol
10366 foo which points to foo@@GNU_1.2. We ignore these symbols,
10367 since the indirected symbol is already in the hash table. */
10368 return TRUE;
10369 }
10370
b8871f35
L
10371 if (type == STT_COMMON || type == STT_OBJECT)
10372 switch (h->root.type)
10373 {
10374 case bfd_link_hash_common:
10375 type = elf_link_convert_common_type (flinfo->info, type);
10376 break;
10377 case bfd_link_hash_defined:
10378 case bfd_link_hash_defweak:
10379 if (bed->common_definition (&sym))
10380 type = elf_link_convert_common_type (flinfo->info, type);
10381 else
10382 type = STT_OBJECT;
10383 break;
10384 case bfd_link_hash_undefined:
10385 case bfd_link_hash_undefweak:
10386 break;
10387 default:
10388 abort ();
10389 }
10390
4deb8f71 10391 if (h->forced_local)
b8871f35
L
10392 {
10393 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10394 /* Turn off visibility on local symbol. */
10395 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10396 }
10397 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10398 else if (h->unique_global && h->def_regular)
10399 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10400 else if (h->root.type == bfd_link_hash_undefweak
10401 || h->root.type == bfd_link_hash_defweak)
10402 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10403 else
10404 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10405 sym.st_target_internal = h->target_internal;
10406
c152c796
AM
10407 /* Give the processor backend a chance to tweak the symbol value,
10408 and also to finish up anything that needs to be done for this
10409 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10410 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10411 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10412 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10413 && h->def_regular
0e1862bb 10414 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10415 || ((h->dynindx != -1
10416 || h->forced_local)
0e1862bb 10417 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10418 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10419 || h->root.type != bfd_link_hash_undefweak))
10420 || !h->forced_local)
8b127cbc 10421 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10422 {
10423 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10424 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10425 {
10426 eoinfo->failed = TRUE;
10427 return FALSE;
10428 }
10429 }
10430
10431 /* If we are marking the symbol as undefined, and there are no
10432 non-weak references to this symbol from a regular object, then
10433 mark the symbol as weak undefined; if there are non-weak
10434 references, mark the symbol as strong. We can't do this earlier,
10435 because it might not be marked as undefined until the
10436 finish_dynamic_symbol routine gets through with it. */
10437 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10438 && h->ref_regular
c152c796
AM
10439 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10440 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10441 {
10442 int bindtype;
b8871f35 10443 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10444
10445 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10446 if (type == STT_GNU_IFUNC)
10447 type = STT_FUNC;
c152c796 10448
f5385ebf 10449 if (h->ref_regular_nonweak)
c152c796
AM
10450 bindtype = STB_GLOBAL;
10451 else
10452 bindtype = STB_WEAK;
2955ec4c 10453 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10454 }
10455
bda987c2
CD
10456 /* If this is a symbol defined in a dynamic library, don't use the
10457 symbol size from the dynamic library. Relinking an executable
10458 against a new library may introduce gratuitous changes in the
10459 executable's symbols if we keep the size. */
10460 if (sym.st_shndx == SHN_UNDEF
10461 && !h->def_regular
10462 && h->def_dynamic)
10463 sym.st_size = 0;
10464
c152c796
AM
10465 /* If a non-weak symbol with non-default visibility is not defined
10466 locally, it is a fatal error. */
0e1862bb 10467 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10468 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10469 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10470 && h->root.type == bfd_link_hash_undefined
f5385ebf 10471 && !h->def_regular)
c152c796 10472 {
17d078c5
AM
10473 const char *msg;
10474
10475 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10476 /* xgettext:c-format */
871b3ab2 10477 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10478 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10479 /* xgettext:c-format */
871b3ab2 10480 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10481 else
695344c0 10482 /* xgettext:c-format */
871b3ab2 10483 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10484 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10485 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10486 eoinfo->failed = TRUE;
10487 return FALSE;
10488 }
10489
10490 /* If this symbol should be put in the .dynsym section, then put it
10491 there now. We already know the symbol index. We also fill in
10492 the entry in the .hash section. */
1c2649ed
EB
10493 if (h->dynindx != -1
10494 && elf_hash_table (flinfo->info)->dynamic_sections_created
10495 && elf_hash_table (flinfo->info)->dynsym != NULL
10496 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10497 {
c152c796
AM
10498 bfd_byte *esym;
10499
90c984fc
L
10500 /* Since there is no version information in the dynamic string,
10501 if there is no version info in symbol version section, we will
1659f720 10502 have a run-time problem if not linking executable, referenced
4deb8f71 10503 by shared library, or not bound locally. */
1659f720 10504 if (h->verinfo.verdef == NULL
0e1862bb 10505 && (!bfd_link_executable (flinfo->info)
1659f720
L
10506 || h->ref_dynamic
10507 || !h->def_regular))
90c984fc
L
10508 {
10509 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10510
10511 if (p && p [1] != '\0')
10512 {
4eca0228 10513 _bfd_error_handler
695344c0 10514 /* xgettext:c-format */
9793eb77 10515 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10516 flinfo->output_bfd, h->root.root.string);
10517 eoinfo->failed = TRUE;
10518 return FALSE;
10519 }
10520 }
10521
c152c796 10522 sym.st_name = h->dynstr_index;
cae1fbbb
L
10523 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10524 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10525 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10526 {
10527 eoinfo->failed = TRUE;
10528 return FALSE;
10529 }
3d16b64e
NA
10530
10531 /* Inform the linker of the addition of this symbol. */
10532
10533 if (flinfo->info->callbacks->ctf_new_dynsym)
10534 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10535
8b127cbc 10536 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10537
8b127cbc 10538 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10539 {
10540 size_t hash_entry_size;
10541 bfd_byte *bucketpos;
10542 bfd_vma chain;
41198d0c
L
10543 size_t bucketcount;
10544 size_t bucket;
10545
8b127cbc 10546 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10547 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10548
10549 hash_entry_size
8b127cbc
AM
10550 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10551 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10552 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10553 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10554 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10555 bucketpos);
10556 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10557 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10558 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10559 }
c152c796 10560
8b127cbc 10561 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10562 {
10563 Elf_Internal_Versym iversym;
10564 Elf_External_Versym *eversym;
10565
5fa370e4 10566 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10567 {
7b20f099
AM
10568 if (h->verinfo.verdef == NULL
10569 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10570 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10571 iversym.vs_vers = 0;
10572 else
10573 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10574 }
10575 else
10576 {
10577 if (h->verinfo.vertree == NULL)
10578 iversym.vs_vers = 1;
10579 else
10580 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10581 if (flinfo->info->create_default_symver)
3e3b46e5 10582 iversym.vs_vers++;
c152c796
AM
10583 }
10584
422f1182 10585 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10586 defined locally. */
422f1182 10587 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10588 iversym.vs_vers |= VERSYM_HIDDEN;
10589
8b127cbc 10590 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10591 eversym += h->dynindx;
8b127cbc 10592 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10593 }
10594 }
10595
d983c8c5
AM
10596 /* If the symbol is undefined, and we didn't output it to .dynsym,
10597 strip it from .symtab too. Obviously we can't do this for
10598 relocatable output or when needed for --emit-relocs. */
10599 else if (input_sec == bfd_und_section_ptr
10600 && h->indx != -2
66cae560
NC
10601 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10602 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10603 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10604 return TRUE;
66cae560 10605
d983c8c5
AM
10606 /* Also strip others that we couldn't earlier due to dynamic symbol
10607 processing. */
10608 if (strip)
10609 return TRUE;
10610 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10611 return TRUE;
10612
2ec55de3
AM
10613 /* Output a FILE symbol so that following locals are not associated
10614 with the wrong input file. We need one for forced local symbols
10615 if we've seen more than one FILE symbol or when we have exactly
10616 one FILE symbol but global symbols are present in a file other
10617 than the one with the FILE symbol. We also need one if linker
10618 defined symbols are present. In practice these conditions are
10619 always met, so just emit the FILE symbol unconditionally. */
10620 if (eoinfo->localsyms
10621 && !eoinfo->file_sym_done
10622 && eoinfo->flinfo->filesym_count != 0)
10623 {
10624 Elf_Internal_Sym fsym;
10625
10626 memset (&fsym, 0, sizeof (fsym));
10627 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10628 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10629 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10630 bfd_und_section_ptr, NULL))
2ec55de3
AM
10631 return FALSE;
10632
10633 eoinfo->file_sym_done = TRUE;
10634 }
10635
8b127cbc 10636 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10637 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10638 input_sec, h);
6e0b88f1 10639 if (ret == 0)
c152c796
AM
10640 {
10641 eoinfo->failed = TRUE;
10642 return FALSE;
10643 }
6e0b88f1
AM
10644 else if (ret == 1)
10645 h->indx = indx;
10646 else if (h->indx == -2)
10647 abort();
c152c796
AM
10648
10649 return TRUE;
10650}
10651
cdd3575c
AM
10652/* Return TRUE if special handling is done for relocs in SEC against
10653 symbols defined in discarded sections. */
10654
c152c796
AM
10655static bfd_boolean
10656elf_section_ignore_discarded_relocs (asection *sec)
10657{
10658 const struct elf_backend_data *bed;
10659
cdd3575c
AM
10660 switch (sec->sec_info_type)
10661 {
dbaa2011
AM
10662 case SEC_INFO_TYPE_STABS:
10663 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10664 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10665 return TRUE;
10666 default:
10667 break;
10668 }
c152c796
AM
10669
10670 bed = get_elf_backend_data (sec->owner);
10671 if (bed->elf_backend_ignore_discarded_relocs != NULL
10672 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10673 return TRUE;
10674
10675 return FALSE;
10676}
10677
9e66c942
AM
10678/* Return a mask saying how ld should treat relocations in SEC against
10679 symbols defined in discarded sections. If this function returns
10680 COMPLAIN set, ld will issue a warning message. If this function
10681 returns PRETEND set, and the discarded section was link-once and the
10682 same size as the kept link-once section, ld will pretend that the
10683 symbol was actually defined in the kept section. Otherwise ld will
10684 zero the reloc (at least that is the intent, but some cooperation by
10685 the target dependent code is needed, particularly for REL targets). */
10686
8a696751
AM
10687unsigned int
10688_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10689{
9e66c942 10690 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10691 return PRETEND;
cdd3575c
AM
10692
10693 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10694 return 0;
cdd3575c
AM
10695
10696 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10697 return 0;
cdd3575c 10698
9e66c942 10699 return COMPLAIN | PRETEND;
cdd3575c
AM
10700}
10701
3d7f7666
L
10702/* Find a match between a section and a member of a section group. */
10703
10704static asection *
c0f00686
L
10705match_group_member (asection *sec, asection *group,
10706 struct bfd_link_info *info)
3d7f7666
L
10707{
10708 asection *first = elf_next_in_group (group);
10709 asection *s = first;
10710
10711 while (s != NULL)
10712 {
c0f00686 10713 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10714 return s;
10715
83180ade 10716 s = elf_next_in_group (s);
3d7f7666
L
10717 if (s == first)
10718 break;
10719 }
10720
10721 return NULL;
10722}
10723
01b3c8ab 10724/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10725 to replace it. Return the replacement if it is OK. Otherwise return
10726 NULL. */
01b3c8ab
L
10727
10728asection *
c0f00686 10729_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10730{
10731 asection *kept;
10732
10733 kept = sec->kept_section;
10734 if (kept != NULL)
10735 {
c2370991 10736 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10737 kept = match_group_member (sec, kept, info);
58349d00
L
10738 if (kept != NULL)
10739 {
10740 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10741 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10742 kept = NULL;
10743 else
10744 {
10745 /* Get the real kept section. */
10746 asection *next;
10747 for (next = kept->kept_section;
10748 next != NULL;
10749 next = next->kept_section)
10750 kept = next;
10751 }
10752 }
c2370991 10753 sec->kept_section = kept;
01b3c8ab
L
10754 }
10755 return kept;
10756}
10757
c152c796
AM
10758/* Link an input file into the linker output file. This function
10759 handles all the sections and relocations of the input file at once.
10760 This is so that we only have to read the local symbols once, and
10761 don't have to keep them in memory. */
10762
10763static bfd_boolean
8b127cbc 10764elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10765{
ece5ef60 10766 int (*relocate_section)
c152c796
AM
10767 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10768 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10769 bfd *output_bfd;
10770 Elf_Internal_Shdr *symtab_hdr;
10771 size_t locsymcount;
10772 size_t extsymoff;
10773 Elf_Internal_Sym *isymbuf;
10774 Elf_Internal_Sym *isym;
10775 Elf_Internal_Sym *isymend;
10776 long *pindex;
10777 asection **ppsection;
10778 asection *o;
10779 const struct elf_backend_data *bed;
c152c796 10780 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10781 bfd_size_type address_size;
10782 bfd_vma r_type_mask;
10783 int r_sym_shift;
ffbc01cc 10784 bfd_boolean have_file_sym = FALSE;
c152c796 10785
8b127cbc 10786 output_bfd = flinfo->output_bfd;
c152c796
AM
10787 bed = get_elf_backend_data (output_bfd);
10788 relocate_section = bed->elf_backend_relocate_section;
10789
10790 /* If this is a dynamic object, we don't want to do anything here:
10791 we don't want the local symbols, and we don't want the section
10792 contents. */
10793 if ((input_bfd->flags & DYNAMIC) != 0)
10794 return TRUE;
10795
c152c796
AM
10796 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10797 if (elf_bad_symtab (input_bfd))
10798 {
10799 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10800 extsymoff = 0;
10801 }
10802 else
10803 {
10804 locsymcount = symtab_hdr->sh_info;
10805 extsymoff = symtab_hdr->sh_info;
10806 }
10807
99fabbc9
JL
10808 /* Enable GNU OSABI features in the output BFD that are used in the input
10809 BFD. */
10810 if (bed->elf_osabi == ELFOSABI_NONE
10811 || bed->elf_osabi == ELFOSABI_GNU
10812 || bed->elf_osabi == ELFOSABI_FREEBSD)
10813 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10814 |= (elf_tdata (input_bfd)->has_gnu_osabi
10815 & (bfd_link_relocatable (flinfo->info)
10816 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10817
c152c796
AM
10818 /* Read the local symbols. */
10819 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10820 if (isymbuf == NULL && locsymcount != 0)
10821 {
10822 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10823 flinfo->internal_syms,
10824 flinfo->external_syms,
10825 flinfo->locsym_shndx);
c152c796
AM
10826 if (isymbuf == NULL)
10827 return FALSE;
10828 }
10829
10830 /* Find local symbol sections and adjust values of symbols in
10831 SEC_MERGE sections. Write out those local symbols we know are
10832 going into the output file. */
10833 isymend = isymbuf + locsymcount;
8b127cbc 10834 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10835 isym < isymend;
10836 isym++, pindex++, ppsection++)
10837 {
10838 asection *isec;
10839 const char *name;
10840 Elf_Internal_Sym osym;
6e0b88f1
AM
10841 long indx;
10842 int ret;
c152c796
AM
10843
10844 *pindex = -1;
10845
10846 if (elf_bad_symtab (input_bfd))
10847 {
10848 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10849 {
10850 *ppsection = NULL;
10851 continue;
10852 }
10853 }
10854
10855 if (isym->st_shndx == SHN_UNDEF)
10856 isec = bfd_und_section_ptr;
c152c796
AM
10857 else if (isym->st_shndx == SHN_ABS)
10858 isec = bfd_abs_section_ptr;
10859 else if (isym->st_shndx == SHN_COMMON)
10860 isec = bfd_com_section_ptr;
10861 else
10862 {
cb33740c
AM
10863 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10864 if (isec == NULL)
10865 {
10866 /* Don't attempt to output symbols with st_shnx in the
10867 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10868 isec = bfd_und_section_ptr;
cb33740c 10869 }
dbaa2011 10870 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10871 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10872 isym->st_value =
10873 _bfd_merged_section_offset (output_bfd, &isec,
10874 elf_section_data (isec)->sec_info,
10875 isym->st_value);
c152c796
AM
10876 }
10877
10878 *ppsection = isec;
10879
d983c8c5
AM
10880 /* Don't output the first, undefined, symbol. In fact, don't
10881 output any undefined local symbol. */
10882 if (isec == bfd_und_section_ptr)
c152c796
AM
10883 continue;
10884
10885 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10886 {
10887 /* We never output section symbols. Instead, we use the
10888 section symbol of the corresponding section in the output
10889 file. */
10890 continue;
10891 }
10892
10893 /* If we are stripping all symbols, we don't want to output this
10894 one. */
8b127cbc 10895 if (flinfo->info->strip == strip_all)
c152c796
AM
10896 continue;
10897
10898 /* If we are discarding all local symbols, we don't want to
10899 output this one. If we are generating a relocatable output
10900 file, then some of the local symbols may be required by
10901 relocs; we output them below as we discover that they are
10902 needed. */
8b127cbc 10903 if (flinfo->info->discard == discard_all)
c152c796
AM
10904 continue;
10905
10906 /* If this symbol is defined in a section which we are
f02571c5 10907 discarding, we don't need to keep it. */
abf874aa
CL
10908 if (isym->st_shndx != SHN_UNDEF
10909 && isym->st_shndx < SHN_LORESERVE
10910 && isec->output_section == NULL
10911 && flinfo->info->non_contiguous_regions
10912 && flinfo->info->non_contiguous_regions_warnings)
10913 {
10914 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10915 "discards section `%s' from '%s'\n"),
765cf5f6 10916 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10917 continue;
10918 }
10919
f02571c5 10920 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10921 && isym->st_shndx < SHN_LORESERVE
10922 && bfd_section_removed_from_list (output_bfd,
10923 isec->output_section))
e75a280b
L
10924 continue;
10925
c152c796
AM
10926 /* Get the name of the symbol. */
10927 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10928 isym->st_name);
10929 if (name == NULL)
10930 return FALSE;
10931
10932 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10933 if ((flinfo->info->strip == strip_some
10934 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10935 == NULL))
8b127cbc 10936 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10937 && (isec->flags & SEC_MERGE)
10938 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10939 || flinfo->info->discard == discard_l)
c152c796
AM
10940 && bfd_is_local_label_name (input_bfd, name)))
10941 continue;
10942
ffbc01cc
AM
10943 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10944 {
ce875075
AM
10945 if (input_bfd->lto_output)
10946 /* -flto puts a temp file name here. This means builds
10947 are not reproducible. Discard the symbol. */
10948 continue;
ffbc01cc
AM
10949 have_file_sym = TRUE;
10950 flinfo->filesym_count += 1;
10951 }
10952 if (!have_file_sym)
10953 {
10954 /* In the absence of debug info, bfd_find_nearest_line uses
10955 FILE symbols to determine the source file for local
10956 function symbols. Provide a FILE symbol here if input
10957 files lack such, so that their symbols won't be
10958 associated with a previous input file. It's not the
10959 source file, but the best we can do. */
5b4293ba 10960 const char *filename;
ffbc01cc
AM
10961 have_file_sym = TRUE;
10962 flinfo->filesym_count += 1;
10963 memset (&osym, 0, sizeof (osym));
10964 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10965 osym.st_shndx = SHN_ABS;
5b4293ba
AM
10966 if (input_bfd->lto_output)
10967 filename = NULL;
10968 else
10969 filename = lbasename (bfd_get_filename (input_bfd));
10970 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
10971 bfd_abs_section_ptr, NULL))
ffbc01cc
AM
10972 return FALSE;
10973 }
10974
c152c796
AM
10975 osym = *isym;
10976
10977 /* Adjust the section index for the output file. */
10978 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10979 isec->output_section);
10980 if (osym.st_shndx == SHN_BAD)
10981 return FALSE;
10982
c152c796
AM
10983 /* ELF symbols in relocatable files are section relative, but
10984 in executable files they are virtual addresses. Note that
10985 this code assumes that all ELF sections have an associated
10986 BFD section with a reasonable value for output_offset; below
10987 we assume that they also have a reasonable value for
10988 output_section. Any special sections must be set up to meet
10989 these requirements. */
10990 osym.st_value += isec->output_offset;
0e1862bb 10991 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10992 {
10993 osym.st_value += isec->output_section->vma;
10994 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10995 {
10996 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10997 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10998 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10999 else
11000 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11001 STT_NOTYPE);
c152c796
AM
11002 }
11003 }
11004
6e0b88f1 11005 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11006 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11007 if (ret == 0)
c152c796 11008 return FALSE;
6e0b88f1
AM
11009 else if (ret == 1)
11010 *pindex = indx;
c152c796
AM
11011 }
11012
310fd250
L
11013 if (bed->s->arch_size == 32)
11014 {
11015 r_type_mask = 0xff;
11016 r_sym_shift = 8;
11017 address_size = 4;
11018 }
11019 else
11020 {
11021 r_type_mask = 0xffffffff;
11022 r_sym_shift = 32;
11023 address_size = 8;
11024 }
11025
c152c796
AM
11026 /* Relocate the contents of each section. */
11027 sym_hashes = elf_sym_hashes (input_bfd);
11028 for (o = input_bfd->sections; o != NULL; o = o->next)
11029 {
11030 bfd_byte *contents;
11031
11032 if (! o->linker_mark)
11033 {
11034 /* This section was omitted from the link. */
11035 continue;
11036 }
11037
7bdf4127 11038 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11039 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11040 {
11041 /* Deal with the group signature symbol. */
11042 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11043 unsigned long symndx = sec_data->this_hdr.sh_info;
11044 asection *osec = o->output_section;
11045
7bdf4127 11046 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11047 if (symndx >= locsymcount
11048 || (elf_bad_symtab (input_bfd)
8b127cbc 11049 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11050 {
11051 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11052 while (h->root.type == bfd_link_hash_indirect
11053 || h->root.type == bfd_link_hash_warning)
11054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11055 /* Arrange for symbol to be output. */
11056 h->indx = -2;
11057 elf_section_data (osec)->this_hdr.sh_info = -2;
11058 }
11059 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11060 {
11061 /* We'll use the output section target_index. */
8b127cbc 11062 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11063 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11064 }
11065 else
11066 {
8b127cbc 11067 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11068 {
11069 /* Otherwise output the local symbol now. */
11070 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11071 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11072 const char *name;
6e0b88f1
AM
11073 long indx;
11074 int ret;
bcacc0f5
AM
11075
11076 name = bfd_elf_string_from_elf_section (input_bfd,
11077 symtab_hdr->sh_link,
11078 sym.st_name);
11079 if (name == NULL)
11080 return FALSE;
11081
11082 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11083 sec);
11084 if (sym.st_shndx == SHN_BAD)
11085 return FALSE;
11086
11087 sym.st_value += o->output_offset;
11088
6e0b88f1 11089 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11090 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11091 NULL);
6e0b88f1 11092 if (ret == 0)
bcacc0f5 11093 return FALSE;
6e0b88f1 11094 else if (ret == 1)
8b127cbc 11095 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11096 else
11097 abort ();
bcacc0f5
AM
11098 }
11099 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11100 = flinfo->indices[symndx];
bcacc0f5
AM
11101 }
11102 }
11103
c152c796 11104 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11105 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11106 continue;
11107
11108 if ((o->flags & SEC_LINKER_CREATED) != 0)
11109 {
11110 /* Section was created by _bfd_elf_link_create_dynamic_sections
11111 or somesuch. */
11112 continue;
11113 }
11114
11115 /* Get the contents of the section. They have been cached by a
11116 relaxation routine. Note that o is a section in an input
11117 file, so the contents field will not have been set by any of
11118 the routines which work on output files. */
11119 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11120 {
11121 contents = elf_section_data (o)->this_hdr.contents;
11122 if (bed->caches_rawsize
11123 && o->rawsize != 0
11124 && o->rawsize < o->size)
11125 {
11126 memcpy (flinfo->contents, contents, o->rawsize);
11127 contents = flinfo->contents;
11128 }
11129 }
c152c796
AM
11130 else
11131 {
8b127cbc 11132 contents = flinfo->contents;
4a114e3e 11133 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11134 return FALSE;
11135 }
11136
11137 if ((o->flags & SEC_RELOC) != 0)
11138 {
11139 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11140 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11141 int action_discarded;
ece5ef60 11142 int ret;
c152c796
AM
11143
11144 /* Get the swapped relocs. */
11145 internal_relocs
8b127cbc
AM
11146 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11147 flinfo->internal_relocs, FALSE);
c152c796
AM
11148 if (internal_relocs == NULL
11149 && o->reloc_count > 0)
11150 return FALSE;
11151
310fd250
L
11152 /* We need to reverse-copy input .ctors/.dtors sections if
11153 they are placed in .init_array/.finit_array for output. */
11154 if (o->size > address_size
11155 && ((strncmp (o->name, ".ctors", 6) == 0
11156 && strcmp (o->output_section->name,
11157 ".init_array") == 0)
11158 || (strncmp (o->name, ".dtors", 6) == 0
11159 && strcmp (o->output_section->name,
11160 ".fini_array") == 0))
11161 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11162 {
056bafd4
MR
11163 if (o->size * bed->s->int_rels_per_ext_rel
11164 != o->reloc_count * address_size)
310fd250 11165 {
4eca0228 11166 _bfd_error_handler
695344c0 11167 /* xgettext:c-format */
871b3ab2 11168 (_("error: %pB: size of section %pA is not "
310fd250
L
11169 "multiple of address size"),
11170 input_bfd, o);
8c6716e5 11171 bfd_set_error (bfd_error_bad_value);
310fd250
L
11172 return FALSE;
11173 }
11174 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11175 }
11176
0f02bbd9 11177 action_discarded = -1;
c152c796 11178 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11179 action_discarded = (*bed->action_discarded) (o);
11180
11181 /* Run through the relocs evaluating complex reloc symbols and
11182 looking for relocs against symbols from discarded sections
11183 or section symbols from removed link-once sections.
11184 Complain about relocs against discarded sections. Zero
11185 relocs against removed link-once sections. */
11186
11187 rel = internal_relocs;
056bafd4 11188 relend = rel + o->reloc_count;
0f02bbd9 11189 for ( ; rel < relend; rel++)
c152c796 11190 {
0f02bbd9
AM
11191 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11192 unsigned int s_type;
11193 asection **ps, *sec;
11194 struct elf_link_hash_entry *h = NULL;
11195 const char *sym_name;
c152c796 11196
0f02bbd9
AM
11197 if (r_symndx == STN_UNDEF)
11198 continue;
c152c796 11199
0f02bbd9
AM
11200 if (r_symndx >= locsymcount
11201 || (elf_bad_symtab (input_bfd)
8b127cbc 11202 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11203 {
11204 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11205
0f02bbd9
AM
11206 /* Badly formatted input files can contain relocs that
11207 reference non-existant symbols. Check here so that
11208 we do not seg fault. */
11209 if (h == NULL)
c152c796 11210 {
4eca0228 11211 _bfd_error_handler
695344c0 11212 /* xgettext:c-format */
2dcf00ce 11213 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11214 "that references a non-existent global symbol"),
2dcf00ce 11215 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11216 bfd_set_error (bfd_error_bad_value);
11217 return FALSE;
11218 }
3b36f7e6 11219
0f02bbd9
AM
11220 while (h->root.type == bfd_link_hash_indirect
11221 || h->root.type == bfd_link_hash_warning)
11222 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11223
0f02bbd9 11224 s_type = h->type;
cdd3575c 11225
9e2dec47 11226 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11227 mark the symbol as undefined. Note that the
11228 linker may attach linker created dynamic sections
11229 to the plugin bfd. Symbols defined in linker
11230 created sections are not plugin symbols. */
bc4e12de 11231 if ((h->root.non_ir_ref_regular
4070765b 11232 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11233 && (h->root.type == bfd_link_hash_defined
11234 || h->root.type == bfd_link_hash_defweak)
11235 && (h->root.u.def.section->flags
11236 & SEC_LINKER_CREATED) == 0
11237 && h->root.u.def.section->owner != NULL
11238 && (h->root.u.def.section->owner->flags
11239 & BFD_PLUGIN) != 0)
11240 {
11241 h->root.type = bfd_link_hash_undefined;
11242 h->root.u.undef.abfd = h->root.u.def.section->owner;
11243 }
11244
0f02bbd9
AM
11245 ps = NULL;
11246 if (h->root.type == bfd_link_hash_defined
11247 || h->root.type == bfd_link_hash_defweak)
11248 ps = &h->root.u.def.section;
11249
11250 sym_name = h->root.root.string;
11251 }
11252 else
11253 {
11254 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11255
11256 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11257 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11258 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11259 sym, *ps);
11260 }
c152c796 11261
c301e700 11262 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11263 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11264 {
11265 bfd_vma val;
11266 bfd_vma dot = (rel->r_offset
11267 + o->output_offset + o->output_section->vma);
11268#ifdef DEBUG
11269 printf ("Encountered a complex symbol!");
11270 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11271 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11272 (long) (rel - internal_relocs));
0f02bbd9
AM
11273 printf (" symbol: idx %8.8lx, name %s\n",
11274 r_symndx, sym_name);
11275 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11276 (unsigned long) rel->r_info,
11277 (unsigned long) rel->r_offset);
11278#endif
8b127cbc 11279 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11280 isymbuf, locsymcount, s_type == STT_SRELC))
11281 return FALSE;
11282
11283 /* Symbol evaluated OK. Update to absolute value. */
11284 set_symbol_value (input_bfd, isymbuf, locsymcount,
11285 r_symndx, val);
11286 continue;
11287 }
11288
11289 if (action_discarded != -1 && ps != NULL)
11290 {
cdd3575c
AM
11291 /* Complain if the definition comes from a
11292 discarded section. */
dbaa2011 11293 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11294 {
cf35638d 11295 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11296 if (action_discarded & COMPLAIN)
8b127cbc 11297 (*flinfo->info->callbacks->einfo)
695344c0 11298 /* xgettext:c-format */
871b3ab2
AM
11299 (_("%X`%s' referenced in section `%pA' of %pB: "
11300 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11301 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11302
87e5235d 11303 /* Try to do the best we can to support buggy old
e0ae6d6f 11304 versions of gcc. Pretend that the symbol is
87e5235d
AM
11305 really defined in the kept linkonce section.
11306 FIXME: This is quite broken. Modifying the
11307 symbol here means we will be changing all later
e0ae6d6f 11308 uses of the symbol, not just in this section. */
0f02bbd9 11309 if (action_discarded & PRETEND)
87e5235d 11310 {
01b3c8ab
L
11311 asection *kept;
11312
c0f00686 11313 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11314 flinfo->info);
01b3c8ab 11315 if (kept != NULL)
87e5235d
AM
11316 {
11317 *ps = kept;
11318 continue;
11319 }
11320 }
c152c796
AM
11321 }
11322 }
11323 }
11324
11325 /* Relocate the section by invoking a back end routine.
11326
11327 The back end routine is responsible for adjusting the
11328 section contents as necessary, and (if using Rela relocs
11329 and generating a relocatable output file) adjusting the
11330 reloc addend as necessary.
11331
11332 The back end routine does not have to worry about setting
11333 the reloc address or the reloc symbol index.
11334
11335 The back end routine is given a pointer to the swapped in
11336 internal symbols, and can access the hash table entries
11337 for the external symbols via elf_sym_hashes (input_bfd).
11338
11339 When generating relocatable output, the back end routine
11340 must handle STB_LOCAL/STT_SECTION symbols specially. The
11341 output symbol is going to be a section symbol
11342 corresponding to the output section, which will require
11343 the addend to be adjusted. */
11344
8b127cbc 11345 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11346 input_bfd, o, contents,
11347 internal_relocs,
11348 isymbuf,
8b127cbc 11349 flinfo->sections);
ece5ef60 11350 if (!ret)
c152c796
AM
11351 return FALSE;
11352
ece5ef60 11353 if (ret == 2
0e1862bb 11354 || bfd_link_relocatable (flinfo->info)
8b127cbc 11355 || flinfo->info->emitrelocations)
c152c796
AM
11356 {
11357 Elf_Internal_Rela *irela;
d4730f92 11358 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11359 bfd_vma last_offset;
11360 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11361 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11362 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11363 unsigned int next_erel;
c152c796 11364 bfd_boolean rela_normal;
d4730f92 11365 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11366
d4730f92
BS
11367 esdi = elf_section_data (o);
11368 esdo = elf_section_data (o->output_section);
11369 rela_normal = FALSE;
c152c796
AM
11370
11371 /* Adjust the reloc addresses and symbol indices. */
11372
11373 irela = internal_relocs;
056bafd4 11374 irelaend = irela + o->reloc_count;
d4730f92
BS
11375 rel_hash = esdo->rel.hashes + esdo->rel.count;
11376 /* We start processing the REL relocs, if any. When we reach
11377 IRELAMID in the loop, we switch to the RELA relocs. */
11378 irelamid = irela;
11379 if (esdi->rel.hdr != NULL)
11380 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11381 * bed->s->int_rels_per_ext_rel);
eac338cf 11382 rel_hash_list = rel_hash;
d4730f92 11383 rela_hash_list = NULL;
c152c796 11384 last_offset = o->output_offset;
0e1862bb 11385 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11386 last_offset += o->output_section->vma;
11387 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11388 {
11389 unsigned long r_symndx;
11390 asection *sec;
11391 Elf_Internal_Sym sym;
11392
11393 if (next_erel == bed->s->int_rels_per_ext_rel)
11394 {
11395 rel_hash++;
11396 next_erel = 0;
11397 }
11398
d4730f92
BS
11399 if (irela == irelamid)
11400 {
11401 rel_hash = esdo->rela.hashes + esdo->rela.count;
11402 rela_hash_list = rel_hash;
11403 rela_normal = bed->rela_normal;
11404 }
11405
c152c796 11406 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11407 flinfo->info, o,
c152c796
AM
11408 irela->r_offset);
11409 if (irela->r_offset >= (bfd_vma) -2)
11410 {
11411 /* This is a reloc for a deleted entry or somesuch.
11412 Turn it into an R_*_NONE reloc, at the same
11413 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11414 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11415 being ordered. */
11416 irela->r_offset = last_offset;
11417 irela->r_info = 0;
11418 irela->r_addend = 0;
11419 continue;
11420 }
11421
11422 irela->r_offset += o->output_offset;
11423
11424 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11425 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11426 irela->r_offset += o->output_section->vma;
11427
11428 last_offset = irela->r_offset;
11429
11430 r_symndx = irela->r_info >> r_sym_shift;
11431 if (r_symndx == STN_UNDEF)
11432 continue;
11433
11434 if (r_symndx >= locsymcount
11435 || (elf_bad_symtab (input_bfd)
8b127cbc 11436 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11437 {
11438 struct elf_link_hash_entry *rh;
11439 unsigned long indx;
11440
11441 /* This is a reloc against a global symbol. We
11442 have not yet output all the local symbols, so
11443 we do not know the symbol index of any global
11444 symbol. We set the rel_hash entry for this
11445 reloc to point to the global hash table entry
11446 for this symbol. The symbol index is then
ee75fd95 11447 set at the end of bfd_elf_final_link. */
c152c796
AM
11448 indx = r_symndx - extsymoff;
11449 rh = elf_sym_hashes (input_bfd)[indx];
11450 while (rh->root.type == bfd_link_hash_indirect
11451 || rh->root.type == bfd_link_hash_warning)
11452 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11453
11454 /* Setting the index to -2 tells
11455 elf_link_output_extsym that this symbol is
11456 used by a reloc. */
11457 BFD_ASSERT (rh->indx < 0);
11458 rh->indx = -2;
c152c796
AM
11459 *rel_hash = rh;
11460
11461 continue;
11462 }
11463
11464 /* This is a reloc against a local symbol. */
11465
11466 *rel_hash = NULL;
11467 sym = isymbuf[r_symndx];
8b127cbc 11468 sec = flinfo->sections[r_symndx];
c152c796
AM
11469 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11470 {
11471 /* I suppose the backend ought to fill in the
11472 section of any STT_SECTION symbol against a
6a8d1586 11473 processor specific section. */
cf35638d 11474 r_symndx = STN_UNDEF;
6a8d1586
AM
11475 if (bfd_is_abs_section (sec))
11476 ;
c152c796
AM
11477 else if (sec == NULL || sec->owner == NULL)
11478 {
11479 bfd_set_error (bfd_error_bad_value);
11480 return FALSE;
11481 }
11482 else
11483 {
6a8d1586
AM
11484 asection *osec = sec->output_section;
11485
11486 /* If we have discarded a section, the output
11487 section will be the absolute section. In
ab96bf03
AM
11488 case of discarded SEC_MERGE sections, use
11489 the kept section. relocate_section should
11490 have already handled discarded linkonce
11491 sections. */
6a8d1586
AM
11492 if (bfd_is_abs_section (osec)
11493 && sec->kept_section != NULL
11494 && sec->kept_section->output_section != NULL)
11495 {
11496 osec = sec->kept_section->output_section;
11497 irela->r_addend -= osec->vma;
11498 }
11499
11500 if (!bfd_is_abs_section (osec))
11501 {
11502 r_symndx = osec->target_index;
cf35638d 11503 if (r_symndx == STN_UNDEF)
74541ad4 11504 {
051d833a
AM
11505 irela->r_addend += osec->vma;
11506 osec = _bfd_nearby_section (output_bfd, osec,
11507 osec->vma);
11508 irela->r_addend -= osec->vma;
11509 r_symndx = osec->target_index;
74541ad4 11510 }
6a8d1586 11511 }
c152c796
AM
11512 }
11513
11514 /* Adjust the addend according to where the
11515 section winds up in the output section. */
11516 if (rela_normal)
11517 irela->r_addend += sec->output_offset;
11518 }
11519 else
11520 {
8b127cbc 11521 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11522 {
11523 unsigned long shlink;
11524 const char *name;
11525 asection *osec;
6e0b88f1 11526 long indx;
c152c796 11527
8b127cbc 11528 if (flinfo->info->strip == strip_all)
c152c796
AM
11529 {
11530 /* You can't do ld -r -s. */
11531 bfd_set_error (bfd_error_invalid_operation);
11532 return FALSE;
11533 }
11534
11535 /* This symbol was skipped earlier, but
11536 since it is needed by a reloc, we
11537 must output it now. */
11538 shlink = symtab_hdr->sh_link;
11539 name = (bfd_elf_string_from_elf_section
11540 (input_bfd, shlink, sym.st_name));
11541 if (name == NULL)
11542 return FALSE;
11543
11544 osec = sec->output_section;
11545 sym.st_shndx =
11546 _bfd_elf_section_from_bfd_section (output_bfd,
11547 osec);
11548 if (sym.st_shndx == SHN_BAD)
11549 return FALSE;
11550
11551 sym.st_value += sec->output_offset;
0e1862bb 11552 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11553 {
11554 sym.st_value += osec->vma;
11555 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11556 {
102def4d
AM
11557 struct elf_link_hash_table *htab
11558 = elf_hash_table (flinfo->info);
11559
c152c796
AM
11560 /* STT_TLS symbols are relative to PT_TLS
11561 segment base. */
102def4d
AM
11562 if (htab->tls_sec != NULL)
11563 sym.st_value -= htab->tls_sec->vma;
11564 else
11565 sym.st_info
11566 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11567 STT_NOTYPE);
c152c796
AM
11568 }
11569 }
11570
6e0b88f1 11571 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11572 ret = elf_link_output_symstrtab (flinfo, name,
11573 &sym, sec,
11574 NULL);
6e0b88f1 11575 if (ret == 0)
c152c796 11576 return FALSE;
6e0b88f1 11577 else if (ret == 1)
8b127cbc 11578 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11579 else
11580 abort ();
c152c796
AM
11581 }
11582
8b127cbc 11583 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11584 }
11585
11586 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11587 | (irela->r_info & r_type_mask));
11588 }
11589
11590 /* Swap out the relocs. */
d4730f92
BS
11591 input_rel_hdr = esdi->rel.hdr;
11592 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11593 {
d4730f92
BS
11594 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11595 input_rel_hdr,
11596 internal_relocs,
11597 rel_hash_list))
11598 return FALSE;
c152c796
AM
11599 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11600 * bed->s->int_rels_per_ext_rel);
eac338cf 11601 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11602 }
11603
11604 input_rela_hdr = esdi->rela.hdr;
11605 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11606 {
eac338cf 11607 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11608 input_rela_hdr,
eac338cf 11609 internal_relocs,
d4730f92 11610 rela_hash_list))
c152c796
AM
11611 return FALSE;
11612 }
11613 }
11614 }
11615
11616 /* Write out the modified section contents. */
11617 if (bed->elf_backend_write_section
8b127cbc 11618 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11619 contents))
c152c796
AM
11620 {
11621 /* Section written out. */
11622 }
11623 else switch (o->sec_info_type)
11624 {
dbaa2011 11625 case SEC_INFO_TYPE_STABS:
c152c796
AM
11626 if (! (_bfd_write_section_stabs
11627 (output_bfd,
8b127cbc 11628 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11629 o, &elf_section_data (o)->sec_info, contents)))
11630 return FALSE;
11631 break;
dbaa2011 11632 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11633 if (! _bfd_write_merged_section (output_bfd, o,
11634 elf_section_data (o)->sec_info))
11635 return FALSE;
11636 break;
dbaa2011 11637 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11638 {
8b127cbc 11639 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11640 o, contents))
11641 return FALSE;
11642 }
11643 break;
2f0c68f2
CM
11644 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11645 {
11646 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11647 flinfo->info,
11648 o, contents))
11649 return FALSE;
11650 }
11651 break;
c152c796
AM
11652 default:
11653 {
310fd250
L
11654 if (! (o->flags & SEC_EXCLUDE))
11655 {
11656 file_ptr offset = (file_ptr) o->output_offset;
11657 bfd_size_type todo = o->size;
37b01f6a 11658
bb294208 11659 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11660
310fd250
L
11661 if ((o->flags & SEC_ELF_REVERSE_COPY))
11662 {
11663 /* Reverse-copy input section to output. */
11664 do
11665 {
11666 todo -= address_size;
11667 if (! bfd_set_section_contents (output_bfd,
11668 o->output_section,
11669 contents + todo,
11670 offset,
11671 address_size))
11672 return FALSE;
11673 if (todo == 0)
11674 break;
11675 offset += address_size;
11676 }
11677 while (1);
11678 }
11679 else if (! bfd_set_section_contents (output_bfd,
11680 o->output_section,
11681 contents,
11682 offset, todo))
11683 return FALSE;
11684 }
c152c796
AM
11685 }
11686 break;
11687 }
11688 }
11689
11690 return TRUE;
11691}
11692
11693/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11694 requested by the linker, and does not come from any input file. This
c152c796
AM
11695 is used to build constructor and destructor tables when linking
11696 with -Ur. */
11697
11698static bfd_boolean
11699elf_reloc_link_order (bfd *output_bfd,
11700 struct bfd_link_info *info,
11701 asection *output_section,
11702 struct bfd_link_order *link_order)
11703{
11704 reloc_howto_type *howto;
11705 long indx;
11706 bfd_vma offset;
11707 bfd_vma addend;
d4730f92 11708 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11709 struct elf_link_hash_entry **rel_hash_ptr;
11710 Elf_Internal_Shdr *rel_hdr;
11711 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11712 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11713 bfd_byte *erel;
11714 unsigned int i;
d4730f92 11715 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11716
11717 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11718 if (howto == NULL)
11719 {
11720 bfd_set_error (bfd_error_bad_value);
11721 return FALSE;
11722 }
11723
11724 addend = link_order->u.reloc.p->addend;
11725
d4730f92
BS
11726 if (esdo->rel.hdr)
11727 reldata = &esdo->rel;
11728 else if (esdo->rela.hdr)
11729 reldata = &esdo->rela;
11730 else
11731 {
11732 reldata = NULL;
11733 BFD_ASSERT (0);
11734 }
11735
c152c796 11736 /* Figure out the symbol index. */
d4730f92 11737 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11738 if (link_order->type == bfd_section_reloc_link_order)
11739 {
11740 indx = link_order->u.reloc.p->u.section->target_index;
11741 BFD_ASSERT (indx != 0);
11742 *rel_hash_ptr = NULL;
11743 }
11744 else
11745 {
11746 struct elf_link_hash_entry *h;
11747
11748 /* Treat a reloc against a defined symbol as though it were
11749 actually against the section. */
11750 h = ((struct elf_link_hash_entry *)
11751 bfd_wrapped_link_hash_lookup (output_bfd, info,
11752 link_order->u.reloc.p->u.name,
11753 FALSE, FALSE, TRUE));
11754 if (h != NULL
11755 && (h->root.type == bfd_link_hash_defined
11756 || h->root.type == bfd_link_hash_defweak))
11757 {
11758 asection *section;
11759
11760 section = h->root.u.def.section;
11761 indx = section->output_section->target_index;
11762 *rel_hash_ptr = NULL;
11763 /* It seems that we ought to add the symbol value to the
11764 addend here, but in practice it has already been added
11765 because it was passed to constructor_callback. */
11766 addend += section->output_section->vma + section->output_offset;
11767 }
11768 else if (h != NULL)
11769 {
11770 /* Setting the index to -2 tells elf_link_output_extsym that
11771 this symbol is used by a reloc. */
11772 h->indx = -2;
11773 *rel_hash_ptr = h;
11774 indx = 0;
11775 }
11776 else
11777 {
1a72702b
AM
11778 (*info->callbacks->unattached_reloc)
11779 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11780 indx = 0;
11781 }
11782 }
11783
11784 /* If this is an inplace reloc, we must write the addend into the
11785 object file. */
11786 if (howto->partial_inplace && addend != 0)
11787 {
11788 bfd_size_type size;
11789 bfd_reloc_status_type rstat;
11790 bfd_byte *buf;
11791 bfd_boolean ok;
11792 const char *sym_name;
bb294208 11793 bfd_size_type octets;
c152c796 11794
a50b1753
NC
11795 size = (bfd_size_type) bfd_get_reloc_size (howto);
11796 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11797 if (buf == NULL && size != 0)
c152c796
AM
11798 return FALSE;
11799 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11800 switch (rstat)
11801 {
11802 case bfd_reloc_ok:
11803 break;
11804
11805 default:
11806 case bfd_reloc_outofrange:
11807 abort ();
11808
11809 case bfd_reloc_overflow:
11810 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11811 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11812 else
11813 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11814 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11815 howto->name, addend, NULL, NULL,
11816 (bfd_vma) 0);
c152c796
AM
11817 break;
11818 }
37b01f6a 11819
bb294208
AM
11820 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11821 output_section);
c152c796 11822 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11823 octets, size);
c152c796
AM
11824 free (buf);
11825 if (! ok)
11826 return FALSE;
11827 }
11828
11829 /* The address of a reloc is relative to the section in a
11830 relocatable file, and is a virtual address in an executable
11831 file. */
11832 offset = link_order->offset;
0e1862bb 11833 if (! bfd_link_relocatable (info))
c152c796
AM
11834 offset += output_section->vma;
11835
11836 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11837 {
11838 irel[i].r_offset = offset;
11839 irel[i].r_info = 0;
11840 irel[i].r_addend = 0;
11841 }
11842 if (bed->s->arch_size == 32)
11843 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11844 else
11845 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11846
d4730f92 11847 rel_hdr = reldata->hdr;
c152c796
AM
11848 erel = rel_hdr->contents;
11849 if (rel_hdr->sh_type == SHT_REL)
11850 {
d4730f92 11851 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11852 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11853 }
11854 else
11855 {
11856 irel[0].r_addend = addend;
d4730f92 11857 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11858 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11859 }
11860
d4730f92 11861 ++reldata->count;
c152c796
AM
11862
11863 return TRUE;
11864}
11865
0b52efa6 11866
0b52efa6
PB
11867/* Compare two sections based on the locations of the sections they are
11868 linked to. Used by elf_fixup_link_order. */
11869
11870static int
8c1c5e5d 11871compare_link_order (const void *a, const void *b)
0b52efa6 11872{
8c1c5e5d
AM
11873 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11874 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11875 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11876 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
cd6d537c
L
11877 bfd_vma apos, bpos;
11878
11879 /* Check if any sections are unordered. */
11880 if (asec == NULL || bsec == NULL)
11881 {
11882 /* Place unordered sections before ordered sections. */
11883 if (bsec != NULL)
11884 return -1;
11885 else if (asec != NULL)
11886 return 1;
11887 return 0;
11888 }
11889
11890 apos = asec->output_section->lma + asec->output_offset;
11891 bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11892
0b52efa6
PB
11893 if (apos < bpos)
11894 return -1;
8c1c5e5d
AM
11895 if (apos > bpos)
11896 return 1;
11897
11898 /* The only way we should get matching LMAs is when the first of two
11899 sections has zero size. */
11900 if (asec->size < bsec->size)
11901 return -1;
11902 if (asec->size > bsec->size)
11903 return 1;
11904
11905 /* If they are both zero size then they almost certainly have the same
11906 VMA and thus are not ordered with respect to each other. Test VMA
11907 anyway, and fall back to id to make the result reproducible across
11908 qsort implementations. */
11909 apos = asec->output_section->vma + asec->output_offset;
11910 bpos = bsec->output_section->vma + bsec->output_offset;
11911 if (apos < bpos)
11912 return -1;
11913 if (apos > bpos)
11914 return 1;
11915
11916 return asec->id - bsec->id;
0b52efa6
PB
11917}
11918
11919
11920/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11921 order as their linked sections. Returns false if this could not be done
11922 because an output section includes both ordered and unordered
11923 sections. Ideally we'd do this in the linker proper. */
11924
11925static bfd_boolean
cd6d537c 11926elf_fixup_link_order (struct bfd_link_info *info, bfd *abfd, asection *o)
0b52efa6 11927{
8c1c5e5d
AM
11928 size_t seen_linkorder;
11929 size_t seen_other;
11930 size_t n;
0b52efa6
PB
11931 struct bfd_link_order *p;
11932 bfd *sub;
cd6d537c 11933 struct bfd_link_order **sections, **indirect_sections;
66631823
CE
11934 asection *other_sec, *linkorder_sec;
11935 bfd_vma offset; /* Octets. */
3b36f7e6 11936
d33cdfe3
L
11937 other_sec = NULL;
11938 linkorder_sec = NULL;
0b52efa6
PB
11939 seen_other = 0;
11940 seen_linkorder = 0;
8423293d 11941 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11942 {
d33cdfe3 11943 if (p->type == bfd_indirect_link_order)
0b52efa6 11944 {
66631823 11945 asection *s = p->u.indirect.section;
d33cdfe3 11946 sub = s->owner;
847d5183
AM
11947 if ((s->flags & SEC_LINKER_CREATED) == 0
11948 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11949 && elf_section_data (s) != NULL
11950 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11951 {
11952 seen_linkorder++;
11953 linkorder_sec = s;
11954 }
0b52efa6 11955 else
d33cdfe3
L
11956 {
11957 seen_other++;
11958 other_sec = s;
11959 }
0b52efa6
PB
11960 }
11961 else
11962 seen_other++;
d33cdfe3 11963
cd6d537c
L
11964 /* Allow mixed ordered and unordered input sections for
11965 non-relocatable link. */
11966 if (bfd_link_relocatable (info) && seen_other && seen_linkorder)
d33cdfe3
L
11967 {
11968 if (other_sec && linkorder_sec)
4eca0228 11969 _bfd_error_handler
695344c0 11970 /* xgettext:c-format */
871b3ab2
AM
11971 (_("%pA has both ordered [`%pA' in %pB] "
11972 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11973 o, linkorder_sec, linkorder_sec->owner,
11974 other_sec, other_sec->owner);
d33cdfe3 11975 else
4eca0228 11976 _bfd_error_handler
871b3ab2 11977 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11978 bfd_set_error (bfd_error_bad_value);
11979 return FALSE;
11980 }
0b52efa6
PB
11981 }
11982
11983 if (!seen_linkorder)
11984 return TRUE;
11985
cd6d537c
L
11986 /* Non-relocatable output can have both ordered and unordered input
11987 sections. */
11988 seen_linkorder += seen_other;
11989
8c1c5e5d 11990 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11991 if (sections == NULL)
11992 return FALSE;
3b36f7e6 11993
8c1c5e5d 11994 seen_linkorder = 0;
8423293d 11995 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11996 sections[seen_linkorder++] = p;
11997
cd6d537c
L
11998 for (indirect_sections = sections, n = 0;
11999 n < seen_linkorder;
12000 indirect_sections++, n++)
12001 {
12002 /* Find the first bfd_indirect_link_order section. */
12003 if (indirect_sections[0]->type == bfd_indirect_link_order)
12004 {
12005 /* Count the consecutive bfd_indirect_link_order sections
12006 with the same pattern. */
12007 size_t i, n_indirect;
12008 const char *pattern
12009 = indirect_sections[0]->u.indirect.section->pattern;
12010 for (i = n + 1; i < seen_linkorder; i++)
12011 if (sections[i]->type != bfd_indirect_link_order
12012 || sections[i]->u.indirect.section->pattern != pattern)
12013 break;
12014 n_indirect = i - n;
12015 /* Sort the bfd_indirect_link_order sections in the order of
12016 their linked section. */
12017 qsort (indirect_sections, n_indirect, sizeof (*sections),
12018 compare_link_order);
12019 indirect_sections += n_indirect;
12020 n += n_indirect;
12021 }
12022 }
0b52efa6 12023
cd6d537c 12024 /* Change the offsets of the bfd_indirect_link_order sections. */
0b52efa6
PB
12025 offset = 0;
12026 for (n = 0; n < seen_linkorder; n++)
cd6d537c
L
12027 if (sections[n]->type == bfd_indirect_link_order)
12028 {
12029 bfd_vma mask;
12030 asection *s = sections[n]->u.indirect.section;
12031 unsigned int opb = bfd_octets_per_byte (abfd, s);
12032
12033 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
12034 offset = (offset + ~mask) & mask;
12035 sections[n]->offset = s->output_offset = offset / opb;
12036 offset += sections[n]->size;
12037 }
12038 else
12039 offset = sections[n]->offset + sections[n]->size;
0b52efa6 12040
4dd07732 12041 free (sections);
0b52efa6
PB
12042 return TRUE;
12043}
12044
76359541
TP
12045/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12046 Returns TRUE upon success, FALSE otherwise. */
12047
12048static bfd_boolean
12049elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12050{
12051 bfd_boolean ret = FALSE;
12052 bfd *implib_bfd;
12053 const struct elf_backend_data *bed;
12054 flagword flags;
12055 enum bfd_architecture arch;
12056 unsigned int mach;
12057 asymbol **sympp = NULL;
12058 long symsize;
12059 long symcount;
12060 long src_count;
12061 elf_symbol_type *osymbuf;
446f7ed5 12062 size_t amt;
76359541
TP
12063
12064 implib_bfd = info->out_implib_bfd;
12065 bed = get_elf_backend_data (abfd);
12066
12067 if (!bfd_set_format (implib_bfd, bfd_object))
12068 return FALSE;
12069
046734ff 12070 /* Use flag from executable but make it a relocatable object. */
76359541
TP
12071 flags = bfd_get_file_flags (abfd);
12072 flags &= ~HAS_RELOC;
12073 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 12074 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
12075 return FALSE;
12076
12077 /* Copy architecture of output file to import library file. */
12078 arch = bfd_get_arch (abfd);
12079 mach = bfd_get_mach (abfd);
12080 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12081 && (abfd->target_defaulted
12082 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12083 return FALSE;
12084
12085 /* Get symbol table size. */
12086 symsize = bfd_get_symtab_upper_bound (abfd);
12087 if (symsize < 0)
12088 return FALSE;
12089
12090 /* Read in the symbol table. */
ec9bd0a2
AM
12091 sympp = (asymbol **) bfd_malloc (symsize);
12092 if (sympp == NULL)
12093 return FALSE;
12094
76359541
TP
12095 symcount = bfd_canonicalize_symtab (abfd, sympp);
12096 if (symcount < 0)
12097 goto free_sym_buf;
12098
12099 /* Allow the BFD backend to copy any private header data it
12100 understands from the output BFD to the import library BFD. */
12101 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12102 goto free_sym_buf;
12103
12104 /* Filter symbols to appear in the import library. */
12105 if (bed->elf_backend_filter_implib_symbols)
12106 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12107 symcount);
12108 else
12109 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12110 if (symcount == 0)
12111 {
5df1bc57 12112 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12113 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12114 implib_bfd);
76359541
TP
12115 goto free_sym_buf;
12116 }
12117
12118
12119 /* Make symbols absolute. */
446f7ed5
AM
12120 amt = symcount * sizeof (*osymbuf);
12121 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12122 if (osymbuf == NULL)
12123 goto free_sym_buf;
12124
76359541
TP
12125 for (src_count = 0; src_count < symcount; src_count++)
12126 {
12127 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12128 sizeof (*osymbuf));
12129 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12130 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12131 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12132 osymbuf[src_count].internal_elf_sym.st_value =
12133 osymbuf[src_count].symbol.value;
12134 sympp[src_count] = &osymbuf[src_count].symbol;
12135 }
12136
12137 bfd_set_symtab (implib_bfd, sympp, symcount);
12138
12139 /* Allow the BFD backend to copy any private data it understands
12140 from the output BFD to the import library BFD. This is done last
12141 to permit the routine to look at the filtered symbol table. */
12142 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12143 goto free_sym_buf;
12144
12145 if (!bfd_close (implib_bfd))
12146 goto free_sym_buf;
12147
12148 ret = TRUE;
12149
dc1e8a47 12150 free_sym_buf:
76359541
TP
12151 free (sympp);
12152 return ret;
12153}
12154
9f7c3e5e
AM
12155static void
12156elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12157{
12158 asection *o;
12159
12160 if (flinfo->symstrtab != NULL)
ef10c3ac 12161 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12162 free (flinfo->contents);
12163 free (flinfo->external_relocs);
12164 free (flinfo->internal_relocs);
12165 free (flinfo->external_syms);
12166 free (flinfo->locsym_shndx);
12167 free (flinfo->internal_syms);
12168 free (flinfo->indices);
12169 free (flinfo->sections);
12170 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12171 free (flinfo->symshndxbuf);
12172 for (o = obfd->sections; o != NULL; o = o->next)
12173 {
12174 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12175 free (esdo->rel.hashes);
12176 free (esdo->rela.hashes);
9f7c3e5e
AM
12177 }
12178}
0b52efa6 12179
c152c796
AM
12180/* Do the final step of an ELF link. */
12181
12182bfd_boolean
12183bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12184{
12185 bfd_boolean dynamic;
12186 bfd_boolean emit_relocs;
12187 bfd *dynobj;
8b127cbc 12188 struct elf_final_link_info flinfo;
91d6fa6a
NC
12189 asection *o;
12190 struct bfd_link_order *p;
12191 bfd *sub;
c152c796
AM
12192 bfd_size_type max_contents_size;
12193 bfd_size_type max_external_reloc_size;
12194 bfd_size_type max_internal_reloc_count;
12195 bfd_size_type max_sym_count;
12196 bfd_size_type max_sym_shndx_count;
c152c796
AM
12197 Elf_Internal_Sym elfsym;
12198 unsigned int i;
12199 Elf_Internal_Shdr *symtab_hdr;
12200 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12201 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12202 struct elf_outext_info eoinfo;
12203 bfd_boolean merged;
12204 size_t relativecount = 0;
12205 asection *reldyn = 0;
12206 bfd_size_type amt;
104d59d1
JM
12207 asection *attr_section = NULL;
12208 bfd_vma attr_size = 0;
12209 const char *std_attrs_section;
64f52338 12210 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12211 bfd_boolean sections_removed;
496afd17 12212 bfd_boolean ret;
c152c796 12213
64f52338 12214 if (!is_elf_hash_table (htab))
c152c796
AM
12215 return FALSE;
12216
0e1862bb 12217 if (bfd_link_pic (info))
c152c796
AM
12218 abfd->flags |= DYNAMIC;
12219
64f52338
AM
12220 dynamic = htab->dynamic_sections_created;
12221 dynobj = htab->dynobj;
c152c796 12222
0e1862bb 12223 emit_relocs = (bfd_link_relocatable (info)
a4676736 12224 || info->emitrelocations);
c152c796 12225
496afd17 12226 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12227 flinfo.info = info;
12228 flinfo.output_bfd = abfd;
ef10c3ac 12229 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12230 if (flinfo.symstrtab == NULL)
c152c796
AM
12231 return FALSE;
12232
12233 if (! dynamic)
12234 {
8b127cbc
AM
12235 flinfo.hash_sec = NULL;
12236 flinfo.symver_sec = NULL;
c152c796
AM
12237 }
12238 else
12239 {
3d4d4302 12240 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12241 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12242 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12243 /* Note that it is OK if symver_sec is NULL. */
12244 }
12245
496afd17
L
12246 if (info->unique_symbol
12247 && !bfd_hash_table_init (&flinfo.local_hash_table,
12248 local_hash_newfunc,
12249 sizeof (struct local_hash_entry)))
12250 return FALSE;
c152c796 12251
104d59d1
JM
12252 /* The object attributes have been merged. Remove the input
12253 sections from the link, and set the contents of the output
1ff6de03 12254 section. */
4c6ee646 12255 sections_removed = FALSE;
104d59d1
JM
12256 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12257 for (o = abfd->sections; o != NULL; o = o->next)
12258 {
5270eddc 12259 bfd_boolean remove_section = FALSE;
b8a6ced7 12260
104d59d1
JM
12261 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12262 || strcmp (o->name, ".gnu.attributes") == 0)
12263 {
12264 for (p = o->map_head.link_order; p != NULL; p = p->next)
12265 {
12266 asection *input_section;
12267
12268 if (p->type != bfd_indirect_link_order)
12269 continue;
12270 input_section = p->u.indirect.section;
12271 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12272 elf_link_input_bfd ignores this section. */
12273 input_section->flags &= ~SEC_HAS_CONTENTS;
12274 }
a0c8462f 12275
104d59d1 12276 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12277 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12278 /* Skip this section later on. */
12279 o->map_head.link_order = NULL;
104d59d1 12280 if (attr_size)
b8a6ced7 12281 attr_section = o;
104d59d1 12282 else
5270eddc 12283 remove_section = TRUE;
104d59d1 12284 }
6e5e9d58
AM
12285 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12286 {
12287 /* Remove empty group section from linker output. */
5270eddc 12288 remove_section = TRUE;
b8a6ced7 12289 }
5270eddc 12290 if (remove_section)
b8a6ced7 12291 {
6e5e9d58
AM
12292 o->flags |= SEC_EXCLUDE;
12293 bfd_section_list_remove (abfd, o);
12294 abfd->section_count--;
4c6ee646 12295 sections_removed = TRUE;
6e5e9d58 12296 }
104d59d1 12297 }
4c6ee646
AM
12298 if (sections_removed)
12299 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12300
c152c796
AM
12301 /* Count up the number of relocations we will output for each output
12302 section, so that we know the sizes of the reloc sections. We
12303 also figure out some maximum sizes. */
12304 max_contents_size = 0;
12305 max_external_reloc_size = 0;
12306 max_internal_reloc_count = 0;
12307 max_sym_count = 0;
12308 max_sym_shndx_count = 0;
12309 merged = FALSE;
12310 for (o = abfd->sections; o != NULL; o = o->next)
12311 {
12312 struct bfd_elf_section_data *esdo = elf_section_data (o);
12313 o->reloc_count = 0;
12314
8423293d 12315 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12316 {
12317 unsigned int reloc_count = 0;
9eaff861 12318 unsigned int additional_reloc_count = 0;
c152c796 12319 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12320
12321 if (p->type == bfd_section_reloc_link_order
12322 || p->type == bfd_symbol_reloc_link_order)
12323 reloc_count = 1;
12324 else if (p->type == bfd_indirect_link_order)
12325 {
12326 asection *sec;
12327
12328 sec = p->u.indirect.section;
c152c796
AM
12329
12330 /* Mark all sections which are to be included in the
12331 link. This will normally be every section. We need
12332 to do this so that we can identify any sections which
12333 the linker has decided to not include. */
12334 sec->linker_mark = TRUE;
12335
12336 if (sec->flags & SEC_MERGE)
12337 merged = TRUE;
12338
eea6121a
AM
12339 if (sec->rawsize > max_contents_size)
12340 max_contents_size = sec->rawsize;
12341 if (sec->size > max_contents_size)
12342 max_contents_size = sec->size;
c152c796 12343
c152c796
AM
12344 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12345 && (sec->owner->flags & DYNAMIC) == 0)
12346 {
12347 size_t sym_count;
12348
a961cdd5
AM
12349 /* We are interested in just local symbols, not all
12350 symbols. */
c152c796
AM
12351 if (elf_bad_symtab (sec->owner))
12352 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12353 / bed->s->sizeof_sym);
12354 else
12355 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12356
12357 if (sym_count > max_sym_count)
12358 max_sym_count = sym_count;
12359
12360 if (sym_count > max_sym_shndx_count
6a40cf0c 12361 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12362 max_sym_shndx_count = sym_count;
12363
a961cdd5
AM
12364 if (esdo->this_hdr.sh_type == SHT_REL
12365 || esdo->this_hdr.sh_type == SHT_RELA)
12366 /* Some backends use reloc_count in relocation sections
12367 to count particular types of relocs. Of course,
12368 reloc sections themselves can't have relocations. */
12369 ;
12370 else if (emit_relocs)
12371 {
12372 reloc_count = sec->reloc_count;
12373 if (bed->elf_backend_count_additional_relocs)
12374 {
12375 int c;
12376 c = (*bed->elf_backend_count_additional_relocs) (sec);
12377 additional_reloc_count += c;
12378 }
12379 }
12380 else if (bed->elf_backend_count_relocs)
12381 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12382
12383 esdi = elf_section_data (sec);
12384
c152c796
AM
12385 if ((sec->flags & SEC_RELOC) != 0)
12386 {
d4730f92 12387 size_t ext_size = 0;
c152c796 12388
d4730f92
BS
12389 if (esdi->rel.hdr != NULL)
12390 ext_size = esdi->rel.hdr->sh_size;
12391 if (esdi->rela.hdr != NULL)
12392 ext_size += esdi->rela.hdr->sh_size;
7326c758 12393
c152c796
AM
12394 if (ext_size > max_external_reloc_size)
12395 max_external_reloc_size = ext_size;
12396 if (sec->reloc_count > max_internal_reloc_count)
12397 max_internal_reloc_count = sec->reloc_count;
12398 }
12399 }
12400 }
12401
12402 if (reloc_count == 0)
12403 continue;
12404
9eaff861 12405 reloc_count += additional_reloc_count;
c152c796
AM
12406 o->reloc_count += reloc_count;
12407
0e1862bb 12408 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12409 {
d4730f92 12410 if (esdi->rel.hdr)
9eaff861 12411 {
491d01d3 12412 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12413 esdo->rel.count += additional_reloc_count;
12414 }
d4730f92 12415 if (esdi->rela.hdr)
9eaff861 12416 {
491d01d3 12417 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12418 esdo->rela.count += additional_reloc_count;
12419 }
d4730f92
BS
12420 }
12421 else
12422 {
12423 if (o->use_rela_p)
12424 esdo->rela.count += reloc_count;
2c2b4ed4 12425 else
d4730f92 12426 esdo->rel.count += reloc_count;
c152c796 12427 }
c152c796
AM
12428 }
12429
9eaff861 12430 if (o->reloc_count > 0)
c152c796
AM
12431 o->flags |= SEC_RELOC;
12432 else
12433 {
12434 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12435 set it (this is probably a bug) and if it is set
12436 assign_section_numbers will create a reloc section. */
12437 o->flags &=~ SEC_RELOC;
12438 }
12439
12440 /* If the SEC_ALLOC flag is not set, force the section VMA to
12441 zero. This is done in elf_fake_sections as well, but forcing
12442 the VMA to 0 here will ensure that relocs against these
12443 sections are handled correctly. */
12444 if ((o->flags & SEC_ALLOC) == 0
12445 && ! o->user_set_vma)
12446 o->vma = 0;
12447 }
12448
0e1862bb 12449 if (! bfd_link_relocatable (info) && merged)
64f52338 12450 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12451
12452 /* Figure out the file positions for everything but the symbol table
12453 and the relocs. We set symcount to force assign_section_numbers
12454 to create a symbol table. */
ed48ec2e 12455 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12456 BFD_ASSERT (! abfd->output_has_begun);
12457 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12458 goto error_return;
12459
ee75fd95 12460 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12461 for (o = abfd->sections; o != NULL; o = o->next)
12462 {
d4730f92 12463 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12464 if ((o->flags & SEC_RELOC) != 0)
12465 {
d4730f92 12466 if (esdo->rel.hdr
9eaff861 12467 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12468 goto error_return;
12469
d4730f92 12470 if (esdo->rela.hdr
9eaff861 12471 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12472 goto error_return;
12473 }
12474
48db3297
AM
12475 /* _bfd_elf_compute_section_file_positions makes temporary use
12476 of target_index. Reset it. */
12477 o->target_index = 0;
12478
c152c796
AM
12479 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12480 to count upwards while actually outputting the relocations. */
d4730f92
BS
12481 esdo->rel.count = 0;
12482 esdo->rela.count = 0;
0ce398f1 12483
1ff6de03
NA
12484 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12485 && !bfd_section_is_ctf (o))
0ce398f1
L
12486 {
12487 /* Cache the section contents so that they can be compressed
12488 later. Use bfd_malloc since it will be freed by
12489 bfd_compress_section_contents. */
12490 unsigned char *contents = esdo->this_hdr.contents;
12491 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12492 abort ();
12493 contents
12494 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12495 if (contents == NULL)
12496 goto error_return;
12497 esdo->this_hdr.contents = contents;
12498 }
c152c796
AM
12499 }
12500
1ff6de03
NA
12501 /* We have now assigned file positions for all the sections except .symtab,
12502 .strtab, and non-loaded reloc and compressed debugging sections. We start
12503 the .symtab section at the current file position, and write directly to it.
12504 We build the .strtab section in memory. */
ed48ec2e 12505 abfd->symcount = 0;
c152c796
AM
12506 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12507 /* sh_name is set in prep_headers. */
12508 symtab_hdr->sh_type = SHT_SYMTAB;
12509 /* sh_flags, sh_addr and sh_size all start off zero. */
12510 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12511 /* sh_link is set in assign_section_numbers. */
12512 /* sh_info is set below. */
12513 /* sh_offset is set just below. */
72de5009 12514 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12515
ef10c3ac
L
12516 if (max_sym_count < 20)
12517 max_sym_count = 20;
64f52338 12518 htab->strtabsize = max_sym_count;
ef10c3ac 12519 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12520 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12521 if (htab->strtab == NULL)
c152c796 12522 goto error_return;
ef10c3ac
L
12523 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12524 flinfo.symshndxbuf
12525 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12526 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12527
8539e4e8 12528 if (info->strip != strip_all || emit_relocs)
c152c796 12529 {
8539e4e8
AM
12530 file_ptr off = elf_next_file_pos (abfd);
12531
12532 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12533
12534 /* Note that at this point elf_next_file_pos (abfd) is
12535 incorrect. We do not yet know the size of the .symtab section.
12536 We correct next_file_pos below, after we do know the size. */
12537
12538 /* Start writing out the symbol table. The first symbol is always a
12539 dummy symbol. */
c152c796
AM
12540 elfsym.st_value = 0;
12541 elfsym.st_size = 0;
12542 elfsym.st_info = 0;
12543 elfsym.st_other = 0;
12544 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12545 elfsym.st_target_internal = 0;
ef10c3ac
L
12546 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12547 bfd_und_section_ptr, NULL) != 1)
c152c796 12548 goto error_return;
c152c796 12549
d1bcae83
L
12550 /* Output a symbol for each section if asked or they are used for
12551 relocs. These symbols usually have no names. We store the
12552 index of each one in the index field of the section, so that
12553 we can find it again when outputting relocs. */
c77cb2a0 12554
d1bcae83 12555 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 12556 {
d1bcae83
L
12557 bfd_boolean name_local_sections
12558 = (bed->elf_backend_name_local_section_symbols
12559 && bed->elf_backend_name_local_section_symbols (abfd));
12560 const char *name = NULL;
12561
12562 elfsym.st_size = 0;
12563 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12564 elfsym.st_other = 0;
12565 elfsym.st_value = 0;
12566 elfsym.st_target_internal = 0;
12567 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 12568 {
d1bcae83
L
12569 o = bfd_section_from_elf_index (abfd, i);
12570 if (o != NULL)
12571 {
12572 o->target_index = bfd_get_symcount (abfd);
12573 elfsym.st_shndx = i;
12574 if (!bfd_link_relocatable (info))
12575 elfsym.st_value = o->vma;
12576 if (name_local_sections)
12577 name = o->name;
12578 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12579 NULL) != 1)
12580 goto error_return;
12581 }
f0b5bb34 12582 }
c152c796
AM
12583 }
12584 }
12585
3f1b17bb
MR
12586 /* On some targets like Irix 5 the symbol split between local and global
12587 ones recorded in the sh_info field needs to be done between section
12588 and all other symbols. */
12589 if (bed->elf_backend_elfsym_local_is_section
12590 && bed->elf_backend_elfsym_local_is_section (abfd))
12591 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12592
c152c796
AM
12593 /* Allocate some memory to hold information read in from the input
12594 files. */
12595 if (max_contents_size != 0)
12596 {
8b127cbc
AM
12597 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12598 if (flinfo.contents == NULL)
c152c796
AM
12599 goto error_return;
12600 }
12601
12602 if (max_external_reloc_size != 0)
12603 {
8b127cbc
AM
12604 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12605 if (flinfo.external_relocs == NULL)
c152c796
AM
12606 goto error_return;
12607 }
12608
12609 if (max_internal_reloc_count != 0)
12610 {
056bafd4 12611 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12612 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12613 if (flinfo.internal_relocs == NULL)
c152c796
AM
12614 goto error_return;
12615 }
12616
12617 if (max_sym_count != 0)
12618 {
12619 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12620 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12621 if (flinfo.external_syms == NULL)
c152c796
AM
12622 goto error_return;
12623
12624 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12625 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12626 if (flinfo.internal_syms == NULL)
c152c796
AM
12627 goto error_return;
12628
12629 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12630 flinfo.indices = (long int *) bfd_malloc (amt);
12631 if (flinfo.indices == NULL)
c152c796
AM
12632 goto error_return;
12633
12634 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12635 flinfo.sections = (asection **) bfd_malloc (amt);
12636 if (flinfo.sections == NULL)
c152c796
AM
12637 goto error_return;
12638 }
12639
12640 if (max_sym_shndx_count != 0)
12641 {
12642 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12643 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12644 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12645 goto error_return;
12646 }
12647
64f52338 12648 if (htab->tls_sec)
c152c796 12649 {
66631823 12650 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12651 asection *sec;
12652
64f52338 12653 for (sec = htab->tls_sec;
c152c796
AM
12654 sec && (sec->flags & SEC_THREAD_LOCAL);
12655 sec = sec->next)
12656 {
3a800eb9 12657 bfd_size_type size = sec->size;
66631823 12658 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12659
3a800eb9
AM
12660 if (size == 0
12661 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12662 {
91d6fa6a
NC
12663 struct bfd_link_order *ord = sec->map_tail.link_order;
12664
12665 if (ord != NULL)
66631823 12666 size = ord->offset * opb + ord->size;
c152c796 12667 }
66631823 12668 end = sec->vma + size / opb;
c152c796 12669 }
64f52338 12670 base = htab->tls_sec->vma;
7dc98aea
RO
12671 /* Only align end of TLS section if static TLS doesn't have special
12672 alignment requirements. */
12673 if (bed->static_tls_alignment == 1)
64f52338
AM
12674 end = align_power (end, htab->tls_sec->alignment_power);
12675 htab->tls_size = end - base;
c152c796
AM
12676 }
12677
0b52efa6
PB
12678 /* Reorder SHF_LINK_ORDER sections. */
12679 for (o = abfd->sections; o != NULL; o = o->next)
12680 {
cd6d537c 12681 if (!elf_fixup_link_order (info, abfd, o))
0b52efa6
PB
12682 return FALSE;
12683 }
12684
2f0c68f2
CM
12685 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12686 return FALSE;
12687
c152c796
AM
12688 /* Since ELF permits relocations to be against local symbols, we
12689 must have the local symbols available when we do the relocations.
12690 Since we would rather only read the local symbols once, and we
12691 would rather not keep them in memory, we handle all the
12692 relocations for a single input file at the same time.
12693
12694 Unfortunately, there is no way to know the total number of local
12695 symbols until we have seen all of them, and the local symbol
12696 indices precede the global symbol indices. This means that when
12697 we are generating relocatable output, and we see a reloc against
12698 a global symbol, we can not know the symbol index until we have
12699 finished examining all the local symbols to see which ones we are
12700 going to output. To deal with this, we keep the relocations in
12701 memory, and don't output them until the end of the link. This is
12702 an unfortunate waste of memory, but I don't see a good way around
12703 it. Fortunately, it only happens when performing a relocatable
12704 link, which is not the common case. FIXME: If keep_memory is set
12705 we could write the relocs out and then read them again; I don't
12706 know how bad the memory loss will be. */
12707
c72f2fb2 12708 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12709 sub->output_has_begun = FALSE;
12710 for (o = abfd->sections; o != NULL; o = o->next)
12711 {
8423293d 12712 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12713 {
12714 if (p->type == bfd_indirect_link_order
12715 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12716 == bfd_target_elf_flavour)
12717 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12718 {
12719 if (! sub->output_has_begun)
12720 {
8b127cbc 12721 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12722 goto error_return;
12723 sub->output_has_begun = TRUE;
12724 }
12725 }
12726 else if (p->type == bfd_section_reloc_link_order
12727 || p->type == bfd_symbol_reloc_link_order)
12728 {
12729 if (! elf_reloc_link_order (abfd, info, o, p))
12730 goto error_return;
12731 }
12732 else
12733 {
12734 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12735 {
12736 if (p->type == bfd_indirect_link_order
12737 && (bfd_get_flavour (sub)
12738 == bfd_target_elf_flavour)
12739 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12740 != bed->s->elfclass))
12741 {
12742 const char *iclass, *oclass;
12743
aebf9be7 12744 switch (bed->s->elfclass)
351f65ca 12745 {
aebf9be7
NC
12746 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12747 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12748 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12749 default: abort ();
351f65ca 12750 }
aebf9be7
NC
12751
12752 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12753 {
aebf9be7
NC
12754 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12755 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12756 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12757 default: abort ();
351f65ca
L
12758 }
12759
12760 bfd_set_error (bfd_error_wrong_format);
4eca0228 12761 _bfd_error_handler
695344c0 12762 /* xgettext:c-format */
871b3ab2 12763 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12764 sub, iclass, oclass);
12765 }
12766
12767 goto error_return;
12768 }
c152c796
AM
12769 }
12770 }
12771 }
12772
c0f00686
L
12773 /* Free symbol buffer if needed. */
12774 if (!info->reduce_memory_overheads)
12775 {
c72f2fb2 12776 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12777 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12778 {
12779 free (elf_tdata (sub)->symbuf);
12780 elf_tdata (sub)->symbuf = NULL;
12781 }
12782 }
12783
496afd17
L
12784 ret = TRUE;
12785
c152c796
AM
12786 /* Output any global symbols that got converted to local in a
12787 version script or due to symbol visibility. We do this in a
12788 separate step since ELF requires all local symbols to appear
12789 prior to any global symbols. FIXME: We should only do this if
12790 some global symbols were, in fact, converted to become local.
12791 FIXME: Will this work correctly with the Irix 5 linker? */
12792 eoinfo.failed = FALSE;
8b127cbc 12793 eoinfo.flinfo = &flinfo;
c152c796 12794 eoinfo.localsyms = TRUE;
34a79995 12795 eoinfo.file_sym_done = FALSE;
7686d77d 12796 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12797 if (eoinfo.failed)
496afd17
L
12798 {
12799 ret = FALSE;
12800 goto return_local_hash_table;
12801 }
c152c796 12802
4e617b1e
PB
12803 /* If backend needs to output some local symbols not present in the hash
12804 table, do it now. */
8539e4e8
AM
12805 if (bed->elf_backend_output_arch_local_syms
12806 && (info->strip != strip_all || emit_relocs))
4e617b1e 12807 {
6e0b88f1 12808 typedef int (*out_sym_func)
4e617b1e
PB
12809 (void *, const char *, Elf_Internal_Sym *, asection *,
12810 struct elf_link_hash_entry *);
12811
12812 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12813 (abfd, info, &flinfo,
12814 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12815 {
12816 ret = FALSE;
12817 goto return_local_hash_table;
12818 }
4e617b1e
PB
12819 }
12820
c152c796
AM
12821 /* That wrote out all the local symbols. Finish up the symbol table
12822 with the global symbols. Even if we want to strip everything we
12823 can, we still need to deal with those global symbols that got
12824 converted to local in a version script. */
12825
12826 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12827 if (!symtab_hdr->sh_info)
12828 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12829
12830 if (dynamic
64f52338
AM
12831 && htab->dynsym != NULL
12832 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12833 {
12834 Elf_Internal_Sym sym;
64f52338 12835 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12836
64f52338
AM
12837 o = htab->dynsym->output_section;
12838 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12839
12840 /* Write out the section symbols for the output sections. */
0e1862bb 12841 if (bfd_link_pic (info)
64f52338 12842 || htab->is_relocatable_executable)
c152c796
AM
12843 {
12844 asection *s;
12845
12846 sym.st_size = 0;
12847 sym.st_name = 0;
12848 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12849 sym.st_other = 0;
35fc36a8 12850 sym.st_target_internal = 0;
c152c796
AM
12851
12852 for (s = abfd->sections; s != NULL; s = s->next)
12853 {
12854 int indx;
12855 bfd_byte *dest;
12856 long dynindx;
12857
c152c796 12858 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12859 if (dynindx <= 0)
12860 continue;
12861 indx = elf_section_data (s)->this_idx;
c152c796
AM
12862 BFD_ASSERT (indx > 0);
12863 sym.st_shndx = indx;
c0d5a53d 12864 if (! check_dynsym (abfd, &sym))
496afd17
L
12865 {
12866 ret = FALSE;
12867 goto return_local_hash_table;
12868 }
c152c796
AM
12869 sym.st_value = s->vma;
12870 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12871
12872 /* Inform the linker of the addition of this symbol. */
12873
12874 if (info->callbacks->ctf_new_dynsym)
12875 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12876
c152c796
AM
12877 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12878 }
c152c796
AM
12879 }
12880
12881 /* Write out the local dynsyms. */
64f52338 12882 if (htab->dynlocal)
c152c796
AM
12883 {
12884 struct elf_link_local_dynamic_entry *e;
64f52338 12885 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12886 {
12887 asection *s;
12888 bfd_byte *dest;
12889
935bd1e0 12890 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12891 Note that we saved a word of storage and overwrote
12892 the original st_name with the dynstr_index. */
12893 sym = e->isym;
935bd1e0 12894 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12895 sym.st_shndx = SHN_UNDEF;
c152c796 12896
cb33740c
AM
12897 s = bfd_section_from_elf_index (e->input_bfd,
12898 e->isym.st_shndx);
4d68fd75
AM
12899 if (s != NULL
12900 && s->output_section != NULL
12901 && elf_section_data (s->output_section) != NULL)
c152c796 12902 {
c152c796
AM
12903 sym.st_shndx =
12904 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12905 if (! check_dynsym (abfd, &sym))
496afd17
L
12906 {
12907 ret = FALSE;
12908 goto return_local_hash_table;
12909 }
c152c796
AM
12910 sym.st_value = (s->output_section->vma
12911 + s->output_offset
12912 + e->isym.st_value);
12913 }
12914
3d16b64e
NA
12915 /* Inform the linker of the addition of this symbol. */
12916
12917 if (info->callbacks->ctf_new_dynsym)
12918 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12919
c152c796
AM
12920 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12921 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12922 }
12923 }
c152c796
AM
12924 }
12925
12926 /* We get the global symbols from the hash table. */
12927 eoinfo.failed = FALSE;
12928 eoinfo.localsyms = FALSE;
8b127cbc 12929 eoinfo.flinfo = &flinfo;
7686d77d 12930 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12931 if (eoinfo.failed)
496afd17
L
12932 {
12933 ret = FALSE;
12934 goto return_local_hash_table;
12935 }
c152c796
AM
12936
12937 /* If backend needs to output some symbols not present in the hash
12938 table, do it now. */
8539e4e8
AM
12939 if (bed->elf_backend_output_arch_syms
12940 && (info->strip != strip_all || emit_relocs))
c152c796 12941 {
6e0b88f1 12942 typedef int (*out_sym_func)
c152c796
AM
12943 (void *, const char *, Elf_Internal_Sym *, asection *,
12944 struct elf_link_hash_entry *);
12945
12946 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12947 (abfd, info, &flinfo,
12948 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12949 {
12950 ret = FALSE;
12951 goto return_local_hash_table;
12952 }
c152c796
AM
12953 }
12954
ef10c3ac
L
12955 /* Finalize the .strtab section. */
12956 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12957
12958 /* Swap out the .strtab section. */
12959 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12960 {
12961 ret = FALSE;
12962 goto return_local_hash_table;
12963 }
c152c796
AM
12964
12965 /* Now we know the size of the symtab section. */
c152c796
AM
12966 if (bfd_get_symcount (abfd) > 0)
12967 {
ee3b52e9
L
12968 /* Finish up and write out the symbol string table (.strtab)
12969 section. */
ad32986f 12970 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12971 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12972
ad32986f 12973 if (elf_symtab_shndx_list (abfd))
8539e4e8 12974 {
ad32986f 12975 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12976
ad32986f
NC
12977 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12978 {
12979 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12980 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12981 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12982 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12983 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12984
ad32986f
NC
12985 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12986 off, TRUE);
12987
12988 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12989 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12990 {
12991 ret = FALSE;
12992 goto return_local_hash_table;
12993 }
ad32986f 12994 }
8539e4e8 12995 }
ee3b52e9
L
12996
12997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12998 /* sh_name was set in prep_headers. */
12999 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 13000 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 13001 symstrtab_hdr->sh_addr = 0;
ef10c3ac 13002 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
13003 symstrtab_hdr->sh_entsize = 0;
13004 symstrtab_hdr->sh_link = 0;
13005 symstrtab_hdr->sh_info = 0;
13006 /* sh_offset is set just below. */
13007 symstrtab_hdr->sh_addralign = 1;
13008
13009 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
13010 off, TRUE);
13011 elf_next_file_pos (abfd) = off;
13012
c152c796 13013 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 13014 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
13015 {
13016 ret = FALSE;
13017 goto return_local_hash_table;
13018 }
c152c796
AM
13019 }
13020
76359541
TP
13021 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13022 {
871b3ab2 13023 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 13024 info->out_implib_bfd);
496afd17
L
13025 ret = FALSE;
13026 goto return_local_hash_table;
76359541
TP
13027 }
13028
c152c796
AM
13029 /* Adjust the relocs to have the correct symbol indices. */
13030 for (o = abfd->sections; o != NULL; o = o->next)
13031 {
d4730f92 13032 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 13033 bfd_boolean sort;
10bbbc1d 13034
c152c796
AM
13035 if ((o->flags & SEC_RELOC) == 0)
13036 continue;
13037
28dbcedc 13038 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 13039 if (esdo->rel.hdr != NULL
10bbbc1d 13040 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
13041 {
13042 ret = FALSE;
13043 goto return_local_hash_table;
13044 }
bca6d0e3 13045 if (esdo->rela.hdr != NULL
10bbbc1d 13046 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
13047 {
13048 ret = FALSE;
13049 goto return_local_hash_table;
13050 }
c152c796
AM
13051
13052 /* Set the reloc_count field to 0 to prevent write_relocs from
13053 trying to swap the relocs out itself. */
13054 o->reloc_count = 0;
13055 }
13056
13057 if (dynamic && info->combreloc && dynobj != NULL)
13058 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13059
13060 /* If we are linking against a dynamic object, or generating a
13061 shared library, finish up the dynamic linking information. */
13062 if (dynamic)
13063 {
13064 bfd_byte *dyncon, *dynconend;
13065
13066 /* Fix up .dynamic entries. */
3d4d4302 13067 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
13068 BFD_ASSERT (o != NULL);
13069
13070 dyncon = o->contents;
eea6121a 13071 dynconend = o->contents + o->size;
c152c796
AM
13072 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13073 {
13074 Elf_Internal_Dyn dyn;
13075 const char *name;
13076 unsigned int type;
64487780
AM
13077 bfd_size_type sh_size;
13078 bfd_vma sh_addr;
c152c796
AM
13079
13080 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13081
13082 switch (dyn.d_tag)
13083 {
13084 default:
13085 continue;
13086 case DT_NULL:
13087 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13088 {
13089 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13090 {
13091 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13092 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13093 default: continue;
13094 }
13095 dyn.d_un.d_val = relativecount;
13096 relativecount = 0;
13097 break;
13098 }
13099 continue;
13100
13101 case DT_INIT:
13102 name = info->init_function;
13103 goto get_sym;
13104 case DT_FINI:
13105 name = info->fini_function;
13106 get_sym:
13107 {
13108 struct elf_link_hash_entry *h;
13109
64f52338 13110 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
13111 if (h != NULL
13112 && (h->root.type == bfd_link_hash_defined
13113 || h->root.type == bfd_link_hash_defweak))
13114 {
bef26483 13115 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
13116 o = h->root.u.def.section;
13117 if (o->output_section != NULL)
bef26483 13118 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
13119 + o->output_offset);
13120 else
13121 {
13122 /* The symbol is imported from another shared
13123 library and does not apply to this one. */
bef26483 13124 dyn.d_un.d_ptr = 0;
c152c796
AM
13125 }
13126 break;
13127 }
13128 }
13129 continue;
13130
13131 case DT_PREINIT_ARRAYSZ:
13132 name = ".preinit_array";
4ade44b7 13133 goto get_out_size;
c152c796
AM
13134 case DT_INIT_ARRAYSZ:
13135 name = ".init_array";
4ade44b7 13136 goto get_out_size;
c152c796
AM
13137 case DT_FINI_ARRAYSZ:
13138 name = ".fini_array";
4ade44b7 13139 get_out_size:
c152c796
AM
13140 o = bfd_get_section_by_name (abfd, name);
13141 if (o == NULL)
13142 {
4eca0228 13143 _bfd_error_handler
4ade44b7 13144 (_("could not find section %s"), name);
c152c796
AM
13145 goto error_return;
13146 }
eea6121a 13147 if (o->size == 0)
4eca0228 13148 _bfd_error_handler
c152c796 13149 (_("warning: %s section has zero size"), name);
eea6121a 13150 dyn.d_un.d_val = o->size;
c152c796
AM
13151 break;
13152
13153 case DT_PREINIT_ARRAY:
13154 name = ".preinit_array";
4ade44b7 13155 goto get_out_vma;
c152c796
AM
13156 case DT_INIT_ARRAY:
13157 name = ".init_array";
4ade44b7 13158 goto get_out_vma;
c152c796
AM
13159 case DT_FINI_ARRAY:
13160 name = ".fini_array";
4ade44b7
AM
13161 get_out_vma:
13162 o = bfd_get_section_by_name (abfd, name);
13163 goto do_vma;
c152c796
AM
13164
13165 case DT_HASH:
13166 name = ".hash";
13167 goto get_vma;
fdc90cb4
JJ
13168 case DT_GNU_HASH:
13169 name = ".gnu.hash";
13170 goto get_vma;
c152c796
AM
13171 case DT_STRTAB:
13172 name = ".dynstr";
13173 goto get_vma;
13174 case DT_SYMTAB:
13175 name = ".dynsym";
13176 goto get_vma;
13177 case DT_VERDEF:
13178 name = ".gnu.version_d";
13179 goto get_vma;
13180 case DT_VERNEED:
13181 name = ".gnu.version_r";
13182 goto get_vma;
13183 case DT_VERSYM:
13184 name = ".gnu.version";
13185 get_vma:
4ade44b7
AM
13186 o = bfd_get_linker_section (dynobj, name);
13187 do_vma:
b3293efa 13188 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13189 {
4eca0228 13190 _bfd_error_handler
4ade44b7 13191 (_("could not find section %s"), name);
c152c796
AM
13192 goto error_return;
13193 }
894891db
NC
13194 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13195 {
4eca0228 13196 _bfd_error_handler
894891db
NC
13197 (_("warning: section '%s' is being made into a note"), name);
13198 bfd_set_error (bfd_error_nonrepresentable_section);
13199 goto error_return;
13200 }
4ade44b7 13201 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13202 break;
13203
13204 case DT_REL:
13205 case DT_RELA:
13206 case DT_RELSZ:
13207 case DT_RELASZ:
13208 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13209 type = SHT_REL;
13210 else
13211 type = SHT_RELA;
64487780
AM
13212 sh_size = 0;
13213 sh_addr = 0;
c152c796
AM
13214 for (i = 1; i < elf_numsections (abfd); i++)
13215 {
13216 Elf_Internal_Shdr *hdr;
13217
13218 hdr = elf_elfsections (abfd)[i];
13219 if (hdr->sh_type == type
13220 && (hdr->sh_flags & SHF_ALLOC) != 0)
13221 {
64487780
AM
13222 sh_size += hdr->sh_size;
13223 if (sh_addr == 0
13224 || sh_addr > hdr->sh_addr)
13225 sh_addr = hdr->sh_addr;
c152c796
AM
13226 }
13227 }
64487780 13228
64f52338
AM
13229 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13230 {
66631823
CE
13231 unsigned int opb = bfd_octets_per_byte (abfd, o);
13232
64f52338
AM
13233 /* Don't count procedure linkage table relocs in the
13234 overall reloc count. */
64487780
AM
13235 sh_size -= htab->srelplt->size;
13236 if (sh_size == 0)
13237 /* If the size is zero, make the address zero too.
13238 This is to avoid a glibc bug. If the backend
13239 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13240 zero, then we'll put DT_RELA at the end of
13241 DT_JMPREL. glibc will interpret the end of
13242 DT_RELA matching the end of DT_JMPREL as the
13243 case where DT_RELA includes DT_JMPREL, and for
13244 LD_BIND_NOW will decide that processing DT_RELA
13245 will process the PLT relocs too. Net result:
13246 No PLT relocs applied. */
13247 sh_addr = 0;
13248
64f52338
AM
13249 /* If .rela.plt is the first .rela section, exclude
13250 it from DT_RELA. */
64487780 13251 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13252 + htab->srelplt->output_offset) * opb)
64487780 13253 sh_addr += htab->srelplt->size;
64f52338 13254 }
64487780
AM
13255
13256 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13257 dyn.d_un.d_val = sh_size;
13258 else
13259 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13260 break;
13261 }
13262 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13263 }
13264 }
13265
13266 /* If we have created any dynamic sections, then output them. */
13267 if (dynobj != NULL)
13268 {
13269 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13270 goto error_return;
13271
943284cc 13272 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13273 if (bfd_link_textrel_check (info)
3d4d4302 13274 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13275 {
13276 bfd_byte *dyncon, *dynconend;
13277
943284cc
DJ
13278 dyncon = o->contents;
13279 dynconend = o->contents + o->size;
13280 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13281 {
13282 Elf_Internal_Dyn dyn;
13283
13284 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13285
13286 if (dyn.d_tag == DT_TEXTREL)
13287 {
a6dbf402 13288 if (info->textrel_check == textrel_check_error)
c192a133 13289 info->callbacks->einfo
9793eb77 13290 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13291 else if (bfd_link_dll (info))
13292 info->callbacks->einfo
13293 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13294 else
13295 info->callbacks->einfo
a6dbf402 13296 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13297 break;
13298 }
13299 }
13300 }
13301
c152c796
AM
13302 for (o = dynobj->sections; o != NULL; o = o->next)
13303 {
13304 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13305 || o->size == 0
c152c796
AM
13306 || o->output_section == bfd_abs_section_ptr)
13307 continue;
13308 if ((o->flags & SEC_LINKER_CREATED) == 0)
13309 {
13310 /* At this point, we are only interested in sections
13311 created by _bfd_elf_link_create_dynamic_sections. */
13312 continue;
13313 }
64f52338 13314 if (htab->stab_info.stabstr == o)
3722b82f 13315 continue;
64f52338 13316 if (htab->eh_info.hdr_sec == o)
eea6121a 13317 continue;
3d4d4302 13318 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13319 {
bb294208
AM
13320 bfd_size_type octets = ((file_ptr) o->output_offset
13321 * bfd_octets_per_byte (abfd, o));
13322 if (!bfd_set_section_contents (abfd, o->output_section,
13323 o->contents, octets, o->size))
c152c796
AM
13324 goto error_return;
13325 }
13326 else
13327 {
13328 /* The contents of the .dynstr section are actually in a
13329 stringtab. */
8539e4e8
AM
13330 file_ptr off;
13331
c152c796
AM
13332 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13333 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13334 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13335 goto error_return;
13336 }
13337 }
13338 }
13339
7bdf4127 13340 if (!info->resolve_section_groups)
c152c796
AM
13341 {
13342 bfd_boolean failed = FALSE;
13343
7bdf4127 13344 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13345 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13346 if (failed)
13347 goto error_return;
13348 }
13349
13350 /* If we have optimized stabs strings, output them. */
64f52338 13351 if (htab->stab_info.stabstr != NULL)
c152c796 13352 {
64f52338 13353 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13354 goto error_return;
13355 }
13356
9f7c3e5e
AM
13357 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13358 goto error_return;
c152c796 13359
1ff6de03
NA
13360 if (info->callbacks->emit_ctf)
13361 info->callbacks->emit_ctf ();
13362
9f7c3e5e 13363 elf_final_link_free (abfd, &flinfo);
c152c796 13364
104d59d1
JM
13365 if (attr_section)
13366 {
a50b1753 13367 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13368 if (contents == NULL)
496afd17
L
13369 {
13370 /* Bail out and fail. */
13371 ret = FALSE;
13372 goto return_local_hash_table;
13373 }
104d59d1
JM
13374 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13375 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13376 free (contents);
13377 }
13378
496afd17
L
13379 return_local_hash_table:
13380 if (info->unique_symbol)
13381 bfd_hash_table_free (&flinfo.local_hash_table);
13382 return ret;
c152c796
AM
13383
13384 error_return:
9f7c3e5e 13385 elf_final_link_free (abfd, &flinfo);
496afd17
L
13386 ret = FALSE;
13387 goto return_local_hash_table;
c152c796
AM
13388}
13389\f
5241d853
RS
13390/* Initialize COOKIE for input bfd ABFD. */
13391
13392static bfd_boolean
13393init_reloc_cookie (struct elf_reloc_cookie *cookie,
13394 struct bfd_link_info *info, bfd *abfd)
13395{
13396 Elf_Internal_Shdr *symtab_hdr;
13397 const struct elf_backend_data *bed;
13398
13399 bed = get_elf_backend_data (abfd);
13400 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13401
13402 cookie->abfd = abfd;
13403 cookie->sym_hashes = elf_sym_hashes (abfd);
13404 cookie->bad_symtab = elf_bad_symtab (abfd);
13405 if (cookie->bad_symtab)
13406 {
13407 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13408 cookie->extsymoff = 0;
13409 }
13410 else
13411 {
13412 cookie->locsymcount = symtab_hdr->sh_info;
13413 cookie->extsymoff = symtab_hdr->sh_info;
13414 }
13415
13416 if (bed->s->arch_size == 32)
13417 cookie->r_sym_shift = 8;
13418 else
13419 cookie->r_sym_shift = 32;
13420
13421 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13422 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13423 {
13424 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13425 cookie->locsymcount, 0,
13426 NULL, NULL, NULL);
13427 if (cookie->locsyms == NULL)
13428 {
13429 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13430 return FALSE;
13431 }
13432 if (info->keep_memory)
13433 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13434 }
13435 return TRUE;
13436}
13437
13438/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13439
13440static void
13441fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13442{
13443 Elf_Internal_Shdr *symtab_hdr;
13444
13445 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13446 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13447 free (cookie->locsyms);
13448}
13449
13450/* Initialize the relocation information in COOKIE for input section SEC
13451 of input bfd ABFD. */
13452
13453static bfd_boolean
13454init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13455 struct bfd_link_info *info, bfd *abfd,
13456 asection *sec)
13457{
5241d853
RS
13458 if (sec->reloc_count == 0)
13459 {
13460 cookie->rels = NULL;
13461 cookie->relend = NULL;
13462 }
13463 else
13464 {
5241d853
RS
13465 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13466 info->keep_memory);
13467 if (cookie->rels == NULL)
13468 return FALSE;
13469 cookie->rel = cookie->rels;
056bafd4 13470 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13471 }
13472 cookie->rel = cookie->rels;
13473 return TRUE;
13474}
13475
13476/* Free the memory allocated by init_reloc_cookie_rels,
13477 if appropriate. */
13478
13479static void
13480fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13481 asection *sec)
13482{
c9594989 13483 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13484 free (cookie->rels);
13485}
13486
13487/* Initialize the whole of COOKIE for input section SEC. */
13488
13489static bfd_boolean
13490init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13491 struct bfd_link_info *info,
13492 asection *sec)
13493{
13494 if (!init_reloc_cookie (cookie, info, sec->owner))
13495 goto error1;
13496 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13497 goto error2;
13498 return TRUE;
13499
13500 error2:
13501 fini_reloc_cookie (cookie, sec->owner);
13502 error1:
13503 return FALSE;
13504}
13505
13506/* Free the memory allocated by init_reloc_cookie_for_section,
13507 if appropriate. */
13508
13509static void
13510fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13511 asection *sec)
13512{
13513 fini_reloc_cookie_rels (cookie, sec);
13514 fini_reloc_cookie (cookie, sec->owner);
13515}
13516\f
c152c796
AM
13517/* Garbage collect unused sections. */
13518
07adf181
AM
13519/* Default gc_mark_hook. */
13520
13521asection *
13522_bfd_elf_gc_mark_hook (asection *sec,
13523 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13524 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13525 struct elf_link_hash_entry *h,
13526 Elf_Internal_Sym *sym)
13527{
13528 if (h != NULL)
13529 {
13530 switch (h->root.type)
13531 {
13532 case bfd_link_hash_defined:
13533 case bfd_link_hash_defweak:
13534 return h->root.u.def.section;
13535
13536 case bfd_link_hash_common:
13537 return h->root.u.c.p->section;
13538
13539 default:
13540 break;
13541 }
13542 }
13543 else
13544 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13545
13546 return NULL;
13547}
13548
9e223787 13549/* Return the debug definition section. */
b7c871ed
L
13550
13551static asection *
13552elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13553 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13554 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13555 struct elf_link_hash_entry *h,
9e223787 13556 Elf_Internal_Sym *sym)
b7c871ed 13557{
9e223787
L
13558 if (h != NULL)
13559 {
13560 /* Return the global debug definition section. */
13561 if ((h->root.type == bfd_link_hash_defined
13562 || h->root.type == bfd_link_hash_defweak)
13563 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13564 return h->root.u.def.section;
13565 }
13566 else
13567 {
13568 /* Return the local debug definition section. */
13569 asection *isec = bfd_section_from_elf_index (sec->owner,
13570 sym->st_shndx);
13571 if ((isec->flags & SEC_DEBUGGING) != 0)
13572 return isec;
13573 }
b7c871ed
L
13574
13575 return NULL;
13576}
13577
5241d853
RS
13578/* COOKIE->rel describes a relocation against section SEC, which is
13579 a section we've decided to keep. Return the section that contains
13580 the relocation symbol, or NULL if no section contains it. */
13581
13582asection *
13583_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13584 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13585 struct elf_reloc_cookie *cookie,
13586 bfd_boolean *start_stop)
5241d853
RS
13587{
13588 unsigned long r_symndx;
3024a17a 13589 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13590
13591 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13592 if (r_symndx == STN_UNDEF)
5241d853
RS
13593 return NULL;
13594
13595 if (r_symndx >= cookie->locsymcount
13596 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13597 {
13598 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13599 if (h == NULL)
13600 {
871b3ab2 13601 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13602 sec->owner);
13603 return NULL;
13604 }
5241d853
RS
13605 while (h->root.type == bfd_link_hash_indirect
13606 || h->root.type == bfd_link_hash_warning)
13607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13608 h->mark = 1;
3024a17a
AM
13609 /* Keep all aliases of the symbol too. If an object symbol
13610 needs to be copied into .dynbss then all of its aliases
13611 should be present as dynamic symbols, not just the one used
13612 on the copy relocation. */
13613 hw = h;
13614 while (hw->is_weakalias)
13615 {
13616 hw = hw->u.alias;
13617 hw->mark = 1;
13618 }
1cce69b9 13619
a6a4679f 13620 if (start_stop != NULL)
1cce69b9 13621 {
7dba9362
AM
13622 /* To work around a glibc bug, mark XXX input sections
13623 when there is a reference to __start_XXX or __stop_XXX
13624 symbols. */
cbd0eecf 13625 if (h->start_stop)
1cce69b9 13626 {
cbd0eecf 13627 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13628 *start_stop = !s->gc_mark;
13629 return s;
1cce69b9
AM
13630 }
13631 }
13632
5241d853
RS
13633 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13634 }
13635
13636 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13637 &cookie->locsyms[r_symndx]);
13638}
13639
13640/* COOKIE->rel describes a relocation against section SEC, which is
13641 a section we've decided to keep. Mark the section that contains
9d0a14d3 13642 the relocation symbol. */
5241d853
RS
13643
13644bfd_boolean
13645_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13646 asection *sec,
13647 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13648 struct elf_reloc_cookie *cookie)
5241d853
RS
13649{
13650 asection *rsec;
1cce69b9 13651 bfd_boolean start_stop = FALSE;
5241d853 13652
1cce69b9
AM
13653 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13654 while (rsec != NULL)
5241d853 13655 {
1cce69b9
AM
13656 if (!rsec->gc_mark)
13657 {
13658 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13659 || (rsec->owner->flags & DYNAMIC) != 0)
13660 rsec->gc_mark = 1;
13661 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13662 return FALSE;
13663 }
13664 if (!start_stop)
13665 break;
199af150 13666 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13667 }
13668 return TRUE;
13669}
13670
07adf181
AM
13671/* The mark phase of garbage collection. For a given section, mark
13672 it and any sections in this section's group, and all the sections
13673 which define symbols to which it refers. */
13674
ccfa59ea
AM
13675bfd_boolean
13676_bfd_elf_gc_mark (struct bfd_link_info *info,
13677 asection *sec,
6a5bb875 13678 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13679{
13680 bfd_boolean ret;
9d0a14d3 13681 asection *group_sec, *eh_frame;
c152c796
AM
13682
13683 sec->gc_mark = 1;
13684
13685 /* Mark all the sections in the group. */
13686 group_sec = elf_section_data (sec)->next_in_group;
13687 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13688 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13689 return FALSE;
13690
13691 /* Look through the section relocs. */
13692 ret = TRUE;
9d0a14d3
RS
13693 eh_frame = elf_eh_frame_section (sec->owner);
13694 if ((sec->flags & SEC_RELOC) != 0
13695 && sec->reloc_count > 0
13696 && sec != eh_frame)
c152c796 13697 {
5241d853 13698 struct elf_reloc_cookie cookie;
c152c796 13699
5241d853
RS
13700 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13701 ret = FALSE;
c152c796 13702 else
c152c796 13703 {
5241d853 13704 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13705 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13706 {
13707 ret = FALSE;
13708 break;
13709 }
13710 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13711 }
13712 }
9d0a14d3
RS
13713
13714 if (ret && eh_frame && elf_fde_list (sec))
13715 {
13716 struct elf_reloc_cookie cookie;
13717
13718 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13719 ret = FALSE;
13720 else
13721 {
13722 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13723 gc_mark_hook, &cookie))
13724 ret = FALSE;
13725 fini_reloc_cookie_for_section (&cookie, eh_frame);
13726 }
13727 }
13728
2f0c68f2
CM
13729 eh_frame = elf_section_eh_frame_entry (sec);
13730 if (ret && eh_frame && !eh_frame->gc_mark)
13731 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13732 ret = FALSE;
13733
c152c796
AM
13734 return ret;
13735}
13736
3c758495
TG
13737/* Scan and mark sections in a special or debug section group. */
13738
13739static void
13740_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13741{
13742 /* Point to first section of section group. */
13743 asection *ssec;
13744 /* Used to iterate the section group. */
13745 asection *msec;
13746
13747 bfd_boolean is_special_grp = TRUE;
13748 bfd_boolean is_debug_grp = TRUE;
13749
13750 /* First scan to see if group contains any section other than debug
13751 and special section. */
13752 ssec = msec = elf_next_in_group (grp);
13753 do
13754 {
13755 if ((msec->flags & SEC_DEBUGGING) == 0)
13756 is_debug_grp = FALSE;
13757
13758 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13759 is_special_grp = FALSE;
13760
13761 msec = elf_next_in_group (msec);
13762 }
13763 while (msec != ssec);
13764
13765 /* If this is a pure debug section group or pure special section group,
13766 keep all sections in this group. */
13767 if (is_debug_grp || is_special_grp)
13768 {
13769 do
13770 {
13771 msec->gc_mark = 1;
13772 msec = elf_next_in_group (msec);
13773 }
13774 while (msec != ssec);
13775 }
13776}
13777
7f6ab9f8
AM
13778/* Keep debug and special sections. */
13779
13780bfd_boolean
13781_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13782 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13783{
13784 bfd *ibfd;
13785
c72f2fb2 13786 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13787 {
13788 asection *isec;
13789 bfd_boolean some_kept;
b40bf0a2 13790 bfd_boolean debug_frag_seen;
b7c871ed 13791 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13792
13793 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13794 continue;
57963c05
AM
13795 isec = ibfd->sections;
13796 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13797 continue;
7f6ab9f8 13798
b40bf0a2
NC
13799 /* Ensure all linker created sections are kept,
13800 see if any other section is already marked,
13801 and note if we have any fragmented debug sections. */
b7c871ed 13802 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13803 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13804 {
13805 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13806 isec->gc_mark = 1;
eb026f09
AM
13807 else if (isec->gc_mark
13808 && (isec->flags & SEC_ALLOC) != 0
13809 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13810 some_kept = TRUE;
b7d07216
L
13811 else
13812 {
13813 /* Since all sections, except for backend specific ones,
13814 have been garbage collected, call mark_hook on this
13815 section if any of its linked-to sections is marked. */
13816 asection *linked_to_sec = elf_linked_to_section (isec);
13817 for (; linked_to_sec != NULL;
13818 linked_to_sec = elf_linked_to_section (linked_to_sec))
13819 if (linked_to_sec->gc_mark)
13820 {
13821 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13822 return FALSE;
13823 break;
13824 }
13825 }
b40bf0a2 13826
535b785f 13827 if (!debug_frag_seen
b40bf0a2
NC
13828 && (isec->flags & SEC_DEBUGGING)
13829 && CONST_STRNEQ (isec->name, ".debug_line."))
13830 debug_frag_seen = TRUE;
5242a0a0
L
13831 else if (strcmp (bfd_section_name (isec),
13832 "__patchable_function_entries") == 0
13833 && elf_linked_to_section (isec) == NULL)
13834 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13835 "need linked-to section "
13836 "for --gc-sections\n"),
13837 isec->owner, isec);
7f6ab9f8
AM
13838 }
13839
eb026f09
AM
13840 /* If no non-note alloc section in this file will be kept, then
13841 we can toss out the debug and special sections. */
7f6ab9f8
AM
13842 if (!some_kept)
13843 continue;
13844
13845 /* Keep debug and special sections like .comment when they are
3c758495 13846 not part of a group. Also keep section groups that contain
b7d07216
L
13847 just debug sections or special sections. NB: Sections with
13848 linked-to section has been handled above. */
7f6ab9f8 13849 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13850 {
13851 if ((isec->flags & SEC_GROUP) != 0)
13852 _bfd_elf_gc_mark_debug_special_section_group (isec);
13853 else if (((isec->flags & SEC_DEBUGGING) != 0
13854 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13855 && elf_next_in_group (isec) == NULL
13856 && elf_linked_to_section (isec) == NULL)
3c758495 13857 isec->gc_mark = 1;
b7c871ed
L
13858 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13859 has_kept_debug_info = TRUE;
3c758495 13860 }
b40bf0a2 13861
b40bf0a2
NC
13862 /* Look for CODE sections which are going to be discarded,
13863 and find and discard any fragmented debug sections which
13864 are associated with that code section. */
b7c871ed
L
13865 if (debug_frag_seen)
13866 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13867 if ((isec->flags & SEC_CODE) != 0
13868 && isec->gc_mark == 0)
13869 {
13870 unsigned int ilen;
13871 asection *dsec;
b40bf0a2 13872
b7c871ed 13873 ilen = strlen (isec->name);
b40bf0a2 13874
b7c871ed 13875 /* Association is determined by the name of the debug
07d6d2b8 13876 section containing the name of the code section as
b7c871ed
L
13877 a suffix. For example .debug_line.text.foo is a
13878 debug section associated with .text.foo. */
13879 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13880 {
13881 unsigned int dlen;
b40bf0a2 13882
b7c871ed
L
13883 if (dsec->gc_mark == 0
13884 || (dsec->flags & SEC_DEBUGGING) == 0)
13885 continue;
b40bf0a2 13886
b7c871ed 13887 dlen = strlen (dsec->name);
b40bf0a2 13888
b7c871ed
L
13889 if (dlen > ilen
13890 && strncmp (dsec->name + (dlen - ilen),
13891 isec->name, ilen) == 0)
b40bf0a2 13892 dsec->gc_mark = 0;
b7c871ed 13893 }
b40bf0a2 13894 }
b7c871ed
L
13895
13896 /* Mark debug sections referenced by kept debug sections. */
13897 if (has_kept_debug_info)
13898 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13899 if (isec->gc_mark
13900 && (isec->flags & SEC_DEBUGGING) != 0)
13901 if (!_bfd_elf_gc_mark (info, isec,
13902 elf_gc_mark_debug_section))
13903 return FALSE;
7f6ab9f8
AM
13904 }
13905 return TRUE;
13906}
13907
c152c796 13908static bfd_boolean
ccabcbe5 13909elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13910{
13911 bfd *sub;
ccabcbe5 13912 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13913
c72f2fb2 13914 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13915 {
13916 asection *o;
13917
b19a8f85 13918 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13919 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13920 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13921 continue;
57963c05
AM
13922 o = sub->sections;
13923 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13924 continue;
c152c796
AM
13925
13926 for (o = sub->sections; o != NULL; o = o->next)
13927 {
a33dafc3
L
13928 /* When any section in a section group is kept, we keep all
13929 sections in the section group. If the first member of
13930 the section group is excluded, we will also exclude the
13931 group section. */
13932 if (o->flags & SEC_GROUP)
13933 {
13934 asection *first = elf_next_in_group (o);
13935 o->gc_mark = first->gc_mark;
13936 }
c152c796 13937
1e7eae0d 13938 if (o->gc_mark)
c152c796
AM
13939 continue;
13940
13941 /* Skip sweeping sections already excluded. */
13942 if (o->flags & SEC_EXCLUDE)
13943 continue;
13944
13945 /* Since this is early in the link process, it is simple
13946 to remove a section from the output. */
13947 o->flags |= SEC_EXCLUDE;
13948
c55fe096 13949 if (info->print_gc_sections && o->size != 0)
695344c0 13950 /* xgettext:c-format */
9793eb77 13951 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13952 o, sub);
c152c796
AM
13953 }
13954 }
13955
c152c796
AM
13956 return TRUE;
13957}
13958
13959/* Propagate collected vtable information. This is called through
13960 elf_link_hash_traverse. */
13961
13962static bfd_boolean
13963elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13964{
c152c796 13965 /* Those that are not vtables. */
cbd0eecf
L
13966 if (h->start_stop
13967 || h->u2.vtable == NULL
13968 || h->u2.vtable->parent == NULL)
c152c796
AM
13969 return TRUE;
13970
13971 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13972 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13973 return TRUE;
13974
13975 /* If we've already been done, exit. */
cbd0eecf 13976 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13977 return TRUE;
13978
13979 /* Make sure the parent's table is up to date. */
cbd0eecf 13980 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13981
cbd0eecf 13982 if (h->u2.vtable->used == NULL)
c152c796
AM
13983 {
13984 /* None of this table's entries were referenced. Re-use the
13985 parent's table. */
cbd0eecf
L
13986 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13987 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13988 }
13989 else
13990 {
13991 size_t n;
13992 bfd_boolean *cu, *pu;
13993
13994 /* Or the parent's entries into ours. */
cbd0eecf 13995 cu = h->u2.vtable->used;
c152c796 13996 cu[-1] = TRUE;
cbd0eecf 13997 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13998 if (pu != NULL)
13999 {
14000 const struct elf_backend_data *bed;
14001 unsigned int log_file_align;
14002
14003 bed = get_elf_backend_data (h->root.u.def.section->owner);
14004 log_file_align = bed->s->log_file_align;
cbd0eecf 14005 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
14006 while (n--)
14007 {
14008 if (*pu)
14009 *cu = TRUE;
14010 pu++;
14011 cu++;
14012 }
14013 }
14014 }
14015
14016 return TRUE;
14017}
14018
14019static bfd_boolean
14020elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
14021{
14022 asection *sec;
14023 bfd_vma hstart, hend;
14024 Elf_Internal_Rela *relstart, *relend, *rel;
14025 const struct elf_backend_data *bed;
14026 unsigned int log_file_align;
14027
c152c796
AM
14028 /* Take care of both those symbols that do not describe vtables as
14029 well as those that are not loaded. */
cbd0eecf
L
14030 if (h->start_stop
14031 || h->u2.vtable == NULL
14032 || h->u2.vtable->parent == NULL)
c152c796
AM
14033 return TRUE;
14034
14035 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14036 || h->root.type == bfd_link_hash_defweak);
14037
14038 sec = h->root.u.def.section;
14039 hstart = h->root.u.def.value;
14040 hend = hstart + h->size;
14041
14042 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
14043 if (!relstart)
14044 return *(bfd_boolean *) okp = FALSE;
14045 bed = get_elf_backend_data (sec->owner);
14046 log_file_align = bed->s->log_file_align;
14047
056bafd4 14048 relend = relstart + sec->reloc_count;
c152c796
AM
14049
14050 for (rel = relstart; rel < relend; ++rel)
14051 if (rel->r_offset >= hstart && rel->r_offset < hend)
14052 {
14053 /* If the entry is in use, do nothing. */
cbd0eecf
L
14054 if (h->u2.vtable->used
14055 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
14056 {
14057 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 14058 if (h->u2.vtable->used[entry])
c152c796
AM
14059 continue;
14060 }
14061 /* Otherwise, kill it. */
14062 rel->r_offset = rel->r_info = rel->r_addend = 0;
14063 }
14064
14065 return TRUE;
14066}
14067
87538722
AM
14068/* Mark sections containing dynamically referenced symbols. When
14069 building shared libraries, we must assume that any visible symbol is
14070 referenced. */
715df9b8 14071
64d03ab5
AM
14072bfd_boolean
14073bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 14074{
87538722 14075 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 14076 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 14077
715df9b8
EB
14078 if ((h->root.type == bfd_link_hash_defined
14079 || h->root.type == bfd_link_hash_defweak)
d664fd41 14080 && ((h->ref_dynamic && !h->forced_local)
c4621b33 14081 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 14082 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 14083 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 14084 && (!bfd_link_executable (info)
22185505 14085 || info->gc_keep_exported
b407645f
AM
14086 || info->export_dynamic
14087 || (h->dynamic
14088 && d != NULL
14089 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14090 && (h->versioned >= versioned
54e8959c
L
14091 || !bfd_hide_sym_by_version (info->version_info,
14092 h->root.root.string)))))
715df9b8
EB
14093 h->root.u.def.section->flags |= SEC_KEEP;
14094
14095 return TRUE;
14096}
3b36f7e6 14097
74f0fb50
AM
14098/* Keep all sections containing symbols undefined on the command-line,
14099 and the section containing the entry symbol. */
14100
14101void
14102_bfd_elf_gc_keep (struct bfd_link_info *info)
14103{
14104 struct bfd_sym_chain *sym;
14105
14106 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14107 {
14108 struct elf_link_hash_entry *h;
14109
14110 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14111 FALSE, FALSE, FALSE);
14112
14113 if (h != NULL
14114 && (h->root.type == bfd_link_hash_defined
14115 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14116 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14117 h->root.u.def.section->flags |= SEC_KEEP;
14118 }
14119}
14120
2f0c68f2
CM
14121bfd_boolean
14122bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14123 struct bfd_link_info *info)
14124{
14125 bfd *ibfd = info->input_bfds;
14126
14127 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14128 {
14129 asection *sec;
14130 struct elf_reloc_cookie cookie;
14131
14132 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14133 continue;
57963c05
AM
14134 sec = ibfd->sections;
14135 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14136 continue;
2f0c68f2
CM
14137
14138 if (!init_reloc_cookie (&cookie, info, ibfd))
14139 return FALSE;
14140
14141 for (sec = ibfd->sections; sec; sec = sec->next)
14142 {
fd361982 14143 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14144 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14145 {
14146 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14147 fini_reloc_cookie_rels (&cookie, sec);
14148 }
14149 }
14150 }
14151 return TRUE;
14152}
14153
c152c796
AM
14154/* Do mark and sweep of unused sections. */
14155
14156bfd_boolean
14157bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14158{
14159 bfd_boolean ok = TRUE;
14160 bfd *sub;
6a5bb875 14161 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14162 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14163 struct elf_link_hash_table *htab;
c152c796 14164
64d03ab5 14165 if (!bed->can_gc_sections
715df9b8 14166 || !is_elf_hash_table (info->hash))
c152c796 14167 {
9793eb77 14168 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
14169 return TRUE;
14170 }
14171
74f0fb50 14172 bed->gc_keep (info);
da44f4e5 14173 htab = elf_hash_table (info);
74f0fb50 14174
9d0a14d3
RS
14175 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14176 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14177 for (sub = info->input_bfds;
14178 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14179 sub = sub->link.next)
9d0a14d3
RS
14180 {
14181 asection *sec;
14182 struct elf_reloc_cookie cookie;
14183
57963c05
AM
14184 sec = sub->sections;
14185 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14186 continue;
9d0a14d3 14187 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14188 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14189 {
14190 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14191 if (elf_section_data (sec)->sec_info
14192 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14193 elf_eh_frame_section (sub) = sec;
14194 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14195 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14196 }
14197 }
9d0a14d3 14198
c152c796 14199 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14200 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14201 if (!ok)
14202 return FALSE;
14203
14204 /* Kill the vtable relocations that were not used. */
da44f4e5 14205 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14206 if (!ok)
14207 return FALSE;
14208
715df9b8 14209 /* Mark dynamically referenced symbols. */
22185505 14210 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14211 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14212
715df9b8 14213 /* Grovel through relocs to find out who stays ... */
64d03ab5 14214 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14215 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14216 {
14217 asection *o;
14218
b19a8f85 14219 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14220 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14221 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14222 continue;
14223
57963c05
AM
14224 o = sub->sections;
14225 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14226 continue;
14227
7f6ab9f8
AM
14228 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14229 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14230 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14231 well as FINI_ARRAY sections for ld -r. */
c152c796 14232 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14233 if (!o->gc_mark
14234 && (o->flags & SEC_EXCLUDE) == 0
24007750 14235 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14236 || (bfd_link_relocatable (info)
14237 && ((elf_section_data (o)->this_hdr.sh_type
14238 == SHT_PREINIT_ARRAY)
14239 || (elf_section_data (o)->this_hdr.sh_type
14240 == SHT_INIT_ARRAY)
14241 || (elf_section_data (o)->this_hdr.sh_type
14242 == SHT_FINI_ARRAY)))
7f6ab9f8 14243 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14244 && elf_next_in_group (o) == NULL
99fabbc9
JL
14245 && elf_linked_to_section (o) == NULL)
14246 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14247 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14248 {
14249 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14250 return FALSE;
14251 }
c152c796
AM
14252 }
14253
6a5bb875 14254 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14255 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14256
c152c796 14257 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14258 return elf_gc_sweep (abfd, info);
c152c796
AM
14259}
14260\f
14261/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14262
14263bfd_boolean
14264bfd_elf_gc_record_vtinherit (bfd *abfd,
14265 asection *sec,
14266 struct elf_link_hash_entry *h,
14267 bfd_vma offset)
14268{
14269 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14270 struct elf_link_hash_entry **search, *child;
ef53be89 14271 size_t extsymcount;
c152c796
AM
14272 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14273
14274 /* The sh_info field of the symtab header tells us where the
14275 external symbols start. We don't care about the local symbols at
14276 this point. */
14277 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14278 if (!elf_bad_symtab (abfd))
14279 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14280
14281 sym_hashes = elf_sym_hashes (abfd);
14282 sym_hashes_end = sym_hashes + extsymcount;
14283
14284 /* Hunt down the child symbol, which is in this section at the same
14285 offset as the relocation. */
14286 for (search = sym_hashes; search != sym_hashes_end; ++search)
14287 {
14288 if ((child = *search) != NULL
14289 && (child->root.type == bfd_link_hash_defined
14290 || child->root.type == bfd_link_hash_defweak)
14291 && child->root.u.def.section == sec
14292 && child->root.u.def.value == offset)
14293 goto win;
14294 }
14295
695344c0 14296 /* xgettext:c-format */
9793eb77 14297 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14298 abfd, sec, (uint64_t) offset);
c152c796
AM
14299 bfd_set_error (bfd_error_invalid_operation);
14300 return FALSE;
14301
14302 win:
cbd0eecf 14303 if (!child->u2.vtable)
f6e332e6 14304 {
cbd0eecf
L
14305 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14306 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14307 if (!child->u2.vtable)
f6e332e6
AM
14308 return FALSE;
14309 }
c152c796
AM
14310 if (!h)
14311 {
14312 /* This *should* only be the absolute section. It could potentially
14313 be that someone has defined a non-global vtable though, which
14314 would be bad. It isn't worth paging in the local symbols to be
14315 sure though; that case should simply be handled by the assembler. */
14316
cbd0eecf 14317 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14318 }
14319 else
cbd0eecf 14320 child->u2.vtable->parent = h;
c152c796
AM
14321
14322 return TRUE;
14323}
14324
14325/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14326
14327bfd_boolean
a0ea3a14 14328bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14329 struct elf_link_hash_entry *h,
14330 bfd_vma addend)
14331{
14332 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14333 unsigned int log_file_align = bed->s->log_file_align;
14334
a0ea3a14
L
14335 if (!h)
14336 {
14337 /* xgettext:c-format */
14338 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14339 abfd, sec);
14340 bfd_set_error (bfd_error_bad_value);
14341 return FALSE;
14342 }
14343
cbd0eecf 14344 if (!h->u2.vtable)
f6e332e6 14345 {
cbd0eecf
L
14346 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14347 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14348 if (!h->u2.vtable)
f6e332e6
AM
14349 return FALSE;
14350 }
14351
cbd0eecf 14352 if (addend >= h->u2.vtable->size)
c152c796
AM
14353 {
14354 size_t size, bytes, file_align;
cbd0eecf 14355 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14356
14357 /* While the symbol is undefined, we have to be prepared to handle
14358 a zero size. */
14359 file_align = 1 << log_file_align;
14360 if (h->root.type == bfd_link_hash_undefined)
14361 size = addend + file_align;
14362 else
14363 {
14364 size = h->size;
14365 if (addend >= size)
14366 {
14367 /* Oops! We've got a reference past the defined end of
14368 the table. This is probably a bug -- shall we warn? */
14369 size = addend + file_align;
14370 }
14371 }
14372 size = (size + file_align - 1) & -file_align;
14373
14374 /* Allocate one extra entry for use as a "done" flag for the
14375 consolidation pass. */
14376 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14377
14378 if (ptr)
14379 {
a50b1753 14380 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14381
14382 if (ptr != NULL)
14383 {
14384 size_t oldbytes;
14385
cbd0eecf 14386 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14387 * sizeof (bfd_boolean));
14388 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14389 }
14390 }
14391 else
a50b1753 14392 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14393
14394 if (ptr == NULL)
14395 return FALSE;
14396
14397 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14398 h->u2.vtable->used = ptr + 1;
14399 h->u2.vtable->size = size;
c152c796
AM
14400 }
14401
cbd0eecf 14402 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14403
14404 return TRUE;
14405}
14406
ae17ab41
CM
14407/* Map an ELF section header flag to its corresponding string. */
14408typedef struct
14409{
14410 char *flag_name;
14411 flagword flag_value;
14412} elf_flags_to_name_table;
14413
3f75e1d6 14414static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14415{
14416 { "SHF_WRITE", SHF_WRITE },
14417 { "SHF_ALLOC", SHF_ALLOC },
14418 { "SHF_EXECINSTR", SHF_EXECINSTR },
14419 { "SHF_MERGE", SHF_MERGE },
14420 { "SHF_STRINGS", SHF_STRINGS },
14421 { "SHF_INFO_LINK", SHF_INFO_LINK},
14422 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14423 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14424 { "SHF_GROUP", SHF_GROUP },
14425 { "SHF_TLS", SHF_TLS },
14426 { "SHF_MASKOS", SHF_MASKOS },
14427 { "SHF_EXCLUDE", SHF_EXCLUDE },
14428};
14429
b9c361e0
JL
14430/* Returns TRUE if the section is to be included, otherwise FALSE. */
14431bfd_boolean
ae17ab41 14432bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14433 struct flag_info *flaginfo,
b9c361e0 14434 asection *section)
ae17ab41 14435{
8b127cbc 14436 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14437
8b127cbc 14438 if (!flaginfo->flags_initialized)
ae17ab41 14439 {
8b127cbc
AM
14440 bfd *obfd = info->output_bfd;
14441 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14442 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14443 int with_hex = 0;
14444 int without_hex = 0;
14445
8b127cbc 14446 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14447 {
b9c361e0 14448 unsigned i;
8b127cbc 14449 flagword (*lookup) (char *);
ae17ab41 14450
8b127cbc
AM
14451 lookup = bed->elf_backend_lookup_section_flags_hook;
14452 if (lookup != NULL)
ae17ab41 14453 {
8b127cbc 14454 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14455
14456 if (hexval != 0)
14457 {
14458 if (tf->with == with_flags)
14459 with_hex |= hexval;
14460 else if (tf->with == without_flags)
14461 without_hex |= hexval;
14462 tf->valid = TRUE;
14463 continue;
14464 }
ae17ab41 14465 }
8b127cbc 14466 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14467 {
8b127cbc 14468 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14469 {
14470 if (tf->with == with_flags)
14471 with_hex |= elf_flags_to_names[i].flag_value;
14472 else if (tf->with == without_flags)
14473 without_hex |= elf_flags_to_names[i].flag_value;
14474 tf->valid = TRUE;
14475 break;
14476 }
14477 }
8b127cbc 14478 if (!tf->valid)
b9c361e0 14479 {
68ffbac6 14480 info->callbacks->einfo
9793eb77 14481 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14482 return FALSE;
ae17ab41
CM
14483 }
14484 }
8b127cbc
AM
14485 flaginfo->flags_initialized = TRUE;
14486 flaginfo->only_with_flags |= with_hex;
14487 flaginfo->not_with_flags |= without_hex;
ae17ab41 14488 }
ae17ab41 14489
8b127cbc 14490 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14491 return FALSE;
14492
8b127cbc 14493 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14494 return FALSE;
14495
14496 return TRUE;
ae17ab41
CM
14497}
14498
c152c796
AM
14499struct alloc_got_off_arg {
14500 bfd_vma gotoff;
10455f89 14501 struct bfd_link_info *info;
c152c796
AM
14502};
14503
14504/* We need a special top-level link routine to convert got reference counts
14505 to real got offsets. */
14506
14507static bfd_boolean
14508elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14509{
a50b1753 14510 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14511 bfd *obfd = gofarg->info->output_bfd;
14512 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14513
c152c796
AM
14514 if (h->got.refcount > 0)
14515 {
14516 h->got.offset = gofarg->gotoff;
10455f89 14517 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14518 }
14519 else
14520 h->got.offset = (bfd_vma) -1;
14521
14522 return TRUE;
14523}
14524
14525/* And an accompanying bit to work out final got entry offsets once
14526 we're done. Should be called from final_link. */
14527
14528bfd_boolean
14529bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14530 struct bfd_link_info *info)
14531{
14532 bfd *i;
14533 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14534 bfd_vma gotoff;
c152c796
AM
14535 struct alloc_got_off_arg gofarg;
14536
10455f89
HPN
14537 BFD_ASSERT (abfd == info->output_bfd);
14538
c152c796
AM
14539 if (! is_elf_hash_table (info->hash))
14540 return FALSE;
14541
14542 /* The GOT offset is relative to the .got section, but the GOT header is
14543 put into the .got.plt section, if the backend uses it. */
14544 if (bed->want_got_plt)
14545 gotoff = 0;
14546 else
14547 gotoff = bed->got_header_size;
14548
14549 /* Do the local .got entries first. */
c72f2fb2 14550 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14551 {
14552 bfd_signed_vma *local_got;
ef53be89 14553 size_t j, locsymcount;
c152c796
AM
14554 Elf_Internal_Shdr *symtab_hdr;
14555
14556 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14557 continue;
14558
14559 local_got = elf_local_got_refcounts (i);
14560 if (!local_got)
14561 continue;
14562
14563 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14564 if (elf_bad_symtab (i))
14565 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14566 else
14567 locsymcount = symtab_hdr->sh_info;
14568
14569 for (j = 0; j < locsymcount; ++j)
14570 {
14571 if (local_got[j] > 0)
14572 {
14573 local_got[j] = gotoff;
10455f89 14574 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14575 }
14576 else
14577 local_got[j] = (bfd_vma) -1;
14578 }
14579 }
14580
14581 /* Then the global .got entries. .plt refcounts are handled by
14582 adjust_dynamic_symbol */
14583 gofarg.gotoff = gotoff;
10455f89 14584 gofarg.info = info;
c152c796
AM
14585 elf_link_hash_traverse (elf_hash_table (info),
14586 elf_gc_allocate_got_offsets,
14587 &gofarg);
14588 return TRUE;
14589}
14590
14591/* Many folk need no more in the way of final link than this, once
14592 got entry reference counting is enabled. */
14593
14594bfd_boolean
14595bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14596{
14597 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14598 return FALSE;
14599
14600 /* Invoke the regular ELF backend linker to do all the work. */
14601 return bfd_elf_final_link (abfd, info);
14602}
14603
14604bfd_boolean
14605bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14606{
a50b1753 14607 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14608
14609 if (rcookie->bad_symtab)
14610 rcookie->rel = rcookie->rels;
14611
14612 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14613 {
14614 unsigned long r_symndx;
14615
14616 if (! rcookie->bad_symtab)
14617 if (rcookie->rel->r_offset > offset)
14618 return FALSE;
14619 if (rcookie->rel->r_offset != offset)
14620 continue;
14621
14622 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14623 if (r_symndx == STN_UNDEF)
c152c796
AM
14624 return TRUE;
14625
14626 if (r_symndx >= rcookie->locsymcount
14627 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14628 {
14629 struct elf_link_hash_entry *h;
14630
14631 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14632
14633 while (h->root.type == bfd_link_hash_indirect
14634 || h->root.type == bfd_link_hash_warning)
14635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14636
14637 if ((h->root.type == bfd_link_hash_defined
14638 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14639 && (h->root.u.def.section->owner != rcookie->abfd
14640 || h->root.u.def.section->kept_section != NULL
14641 || discarded_section (h->root.u.def.section)))
c152c796 14642 return TRUE;
c152c796
AM
14643 }
14644 else
14645 {
14646 /* It's not a relocation against a global symbol,
14647 but it could be a relocation against a local
14648 symbol for a discarded section. */
14649 asection *isec;
14650 Elf_Internal_Sym *isym;
14651
14652 /* Need to: get the symbol; get the section. */
14653 isym = &rcookie->locsyms[r_symndx];
cb33740c 14654 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14655 if (isec != NULL
14656 && (isec->kept_section != NULL
14657 || discarded_section (isec)))
cb33740c 14658 return TRUE;
c152c796
AM
14659 }
14660 return FALSE;
14661 }
14662 return FALSE;
14663}
14664
14665/* Discard unneeded references to discarded sections.
75938853
AM
14666 Returns -1 on error, 1 if any section's size was changed, 0 if
14667 nothing changed. This function assumes that the relocations are in
14668 sorted order, which is true for all known assemblers. */
c152c796 14669
75938853 14670int
c152c796
AM
14671bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14672{
14673 struct elf_reloc_cookie cookie;
18cd5bce 14674 asection *o;
c152c796 14675 bfd *abfd;
75938853 14676 int changed = 0;
c152c796
AM
14677
14678 if (info->traditional_format
14679 || !is_elf_hash_table (info->hash))
75938853 14680 return 0;
c152c796 14681
18cd5bce
AM
14682 o = bfd_get_section_by_name (output_bfd, ".stab");
14683 if (o != NULL)
c152c796 14684 {
18cd5bce 14685 asection *i;
c152c796 14686
18cd5bce 14687 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14688 {
18cd5bce
AM
14689 if (i->size == 0
14690 || i->reloc_count == 0
14691 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14692 continue;
c152c796 14693
18cd5bce
AM
14694 abfd = i->owner;
14695 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14696 continue;
c152c796 14697
18cd5bce 14698 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14699 return -1;
c152c796 14700
18cd5bce
AM
14701 if (_bfd_discard_section_stabs (abfd, i,
14702 elf_section_data (i)->sec_info,
5241d853
RS
14703 bfd_elf_reloc_symbol_deleted_p,
14704 &cookie))
75938853 14705 changed = 1;
18cd5bce
AM
14706
14707 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14708 }
18cd5bce
AM
14709 }
14710
2f0c68f2
CM
14711 o = NULL;
14712 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14713 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14714 if (o != NULL)
14715 {
14716 asection *i;
d7153c4a 14717 int eh_changed = 0;
66631823 14718 unsigned int eh_alignment; /* Octets. */
c152c796 14719
18cd5bce 14720 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14721 {
18cd5bce
AM
14722 if (i->size == 0)
14723 continue;
14724
14725 abfd = i->owner;
14726 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14727 continue;
14728
14729 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14730 return -1;
18cd5bce
AM
14731
14732 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14733 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14734 bfd_elf_reloc_symbol_deleted_p,
14735 &cookie))
d7153c4a
AM
14736 {
14737 eh_changed = 1;
14738 if (i->size != i->rawsize)
14739 changed = 1;
14740 }
18cd5bce
AM
14741
14742 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14743 }
9866ffe2 14744
66631823
CE
14745 eh_alignment = ((1 << o->alignment_power)
14746 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14747 /* Skip over zero terminator, and prevent empty sections from
14748 adding alignment padding at the end. */
14749 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14750 if (i->size == 0)
14751 i->flags |= SEC_EXCLUDE;
14752 else if (i->size > 4)
14753 break;
14754 /* The last non-empty eh_frame section doesn't need padding. */
14755 if (i != NULL)
14756 i = i->map_tail.s;
14757 /* Any prior sections must pad the last FDE out to the output
14758 section alignment. Otherwise we might have zero padding
14759 between sections, which would be seen as a terminator. */
14760 for (; i != NULL; i = i->map_tail.s)
14761 if (i->size == 4)
14762 /* All but the last zero terminator should have been removed. */
14763 BFD_FAIL ();
14764 else
14765 {
14766 bfd_size_type size
14767 = (i->size + eh_alignment - 1) & -eh_alignment;
14768 if (i->size != size)
af471f82 14769 {
9866ffe2
AM
14770 i->size = size;
14771 changed = 1;
14772 eh_changed = 1;
af471f82 14773 }
9866ffe2 14774 }
d7153c4a
AM
14775 if (eh_changed)
14776 elf_link_hash_traverse (elf_hash_table (info),
14777 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14778 }
c152c796 14779
18cd5bce
AM
14780 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14781 {
14782 const struct elf_backend_data *bed;
57963c05 14783 asection *s;
c152c796 14784
18cd5bce
AM
14785 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14786 continue;
57963c05
AM
14787 s = abfd->sections;
14788 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14789 continue;
18cd5bce
AM
14790
14791 bed = get_elf_backend_data (abfd);
14792
14793 if (bed->elf_backend_discard_info != NULL)
14794 {
14795 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14796 return -1;
18cd5bce
AM
14797
14798 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14799 changed = 1;
18cd5bce
AM
14800
14801 fini_reloc_cookie (&cookie, abfd);
14802 }
c152c796
AM
14803 }
14804
2f0c68f2
CM
14805 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14806 _bfd_elf_end_eh_frame_parsing (info);
14807
14808 if (info->eh_frame_hdr_type
0e1862bb 14809 && !bfd_link_relocatable (info)
c152c796 14810 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14811 changed = 1;
c152c796 14812
75938853 14813 return changed;
c152c796 14814}
082b7297 14815
43e1669b 14816bfd_boolean
0c511000 14817_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14818 asection *sec,
c0f00686 14819 struct bfd_link_info *info)
082b7297
L
14820{
14821 flagword flags;
c77ec726 14822 const char *name, *key;
082b7297
L
14823 struct bfd_section_already_linked *l;
14824 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14825
c77ec726
AM
14826 if (sec->output_section == bfd_abs_section_ptr)
14827 return FALSE;
0c511000 14828
c77ec726 14829 flags = sec->flags;
0c511000 14830
c77ec726
AM
14831 /* Return if it isn't a linkonce section. A comdat group section
14832 also has SEC_LINK_ONCE set. */
14833 if ((flags & SEC_LINK_ONCE) == 0)
14834 return FALSE;
0c511000 14835
c77ec726
AM
14836 /* Don't put group member sections on our list of already linked
14837 sections. They are handled as a group via their group section. */
14838 if (elf_sec_group (sec) != NULL)
14839 return FALSE;
0c511000 14840
c77ec726
AM
14841 /* For a SHT_GROUP section, use the group signature as the key. */
14842 name = sec->name;
14843 if ((flags & SEC_GROUP) != 0
14844 && elf_next_in_group (sec) != NULL
14845 && elf_group_name (elf_next_in_group (sec)) != NULL)
14846 key = elf_group_name (elf_next_in_group (sec));
14847 else
14848 {
14849 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14850 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14851 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14852 key++;
0c511000 14853 else
c77ec726
AM
14854 /* Must be a user linkonce section that doesn't follow gcc's
14855 naming convention. In this case we won't be matching
14856 single member groups. */
14857 key = name;
0c511000 14858 }
6d2cd210 14859
c77ec726 14860 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14861
14862 for (l = already_linked_list->entry; l != NULL; l = l->next)
14863 {
c2370991 14864 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14865 sections with a signature of <key> (<key> is some string),
14866 and linkonce sections named .gnu.linkonce.<type>.<key>.
14867 Match like sections. LTO plugin sections are an exception.
14868 They are always named .gnu.linkonce.t.<key> and match either
14869 type of section. */
14870 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14871 && ((flags & SEC_GROUP) != 0
14872 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14873 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14874 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14875 {
14876 /* The section has already been linked. See if we should
6d2cd210 14877 issue a warning. */
c77ec726
AM
14878 if (!_bfd_handle_already_linked (sec, l, info))
14879 return FALSE;
082b7297 14880
c77ec726 14881 if (flags & SEC_GROUP)
3d7f7666 14882 {
c77ec726
AM
14883 asection *first = elf_next_in_group (sec);
14884 asection *s = first;
3d7f7666 14885
c77ec726 14886 while (s != NULL)
3d7f7666 14887 {
c77ec726
AM
14888 s->output_section = bfd_abs_section_ptr;
14889 /* Record which group discards it. */
14890 s->kept_section = l->sec;
14891 s = elf_next_in_group (s);
14892 /* These lists are circular. */
14893 if (s == first)
14894 break;
3d7f7666
L
14895 }
14896 }
082b7297 14897
43e1669b 14898 return TRUE;
082b7297
L
14899 }
14900 }
14901
c77ec726
AM
14902 /* A single member comdat group section may be discarded by a
14903 linkonce section and vice versa. */
14904 if ((flags & SEC_GROUP) != 0)
3d7f7666 14905 {
c77ec726 14906 asection *first = elf_next_in_group (sec);
c2370991 14907
c77ec726
AM
14908 if (first != NULL && elf_next_in_group (first) == first)
14909 /* Check this single member group against linkonce sections. */
14910 for (l = already_linked_list->entry; l != NULL; l = l->next)
14911 if ((l->sec->flags & SEC_GROUP) == 0
14912 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14913 {
14914 first->output_section = bfd_abs_section_ptr;
14915 first->kept_section = l->sec;
14916 sec->output_section = bfd_abs_section_ptr;
14917 break;
14918 }
14919 }
14920 else
14921 /* Check this linkonce section against single member groups. */
14922 for (l = already_linked_list->entry; l != NULL; l = l->next)
14923 if (l->sec->flags & SEC_GROUP)
6d2cd210 14924 {
c77ec726 14925 asection *first = elf_next_in_group (l->sec);
6d2cd210 14926
c77ec726
AM
14927 if (first != NULL
14928 && elf_next_in_group (first) == first
14929 && bfd_elf_match_symbols_in_sections (first, sec, info))
14930 {
14931 sec->output_section = bfd_abs_section_ptr;
14932 sec->kept_section = first;
14933 break;
14934 }
6d2cd210 14935 }
0c511000 14936
c77ec726
AM
14937 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14938 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14939 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14940 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14941 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14942 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14943 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14944 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14945 The reverse order cannot happen as there is never a bfd with only the
14946 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14947 matter as here were are looking only for cross-bfd sections. */
14948
14949 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14950 for (l = already_linked_list->entry; l != NULL; l = l->next)
14951 if ((l->sec->flags & SEC_GROUP) == 0
14952 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14953 {
14954 if (abfd != l->sec->owner)
14955 sec->output_section = bfd_abs_section_ptr;
14956 break;
14957 }
80c29487 14958
082b7297 14959 /* This is the first section with this name. Record it. */
c77ec726 14960 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14961 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14962 return sec->output_section == bfd_abs_section_ptr;
082b7297 14963}
81e1b023 14964
a4d8e49b
L
14965bfd_boolean
14966_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14967{
14968 return sym->st_shndx == SHN_COMMON;
14969}
14970
14971unsigned int
14972_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14973{
14974 return SHN_COMMON;
14975}
14976
14977asection *
14978_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14979{
14980 return bfd_com_section_ptr;
14981}
10455f89
HPN
14982
14983bfd_vma
14984_bfd_elf_default_got_elt_size (bfd *abfd,
14985 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14986 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14987 bfd *ibfd ATTRIBUTE_UNUSED,
14988 unsigned long symndx ATTRIBUTE_UNUSED)
14989{
14990 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14991 return bed->s->arch_size / 8;
14992}
83bac4b0
NC
14993
14994/* Routines to support the creation of dynamic relocs. */
14995
83bac4b0
NC
14996/* Returns the name of the dynamic reloc section associated with SEC. */
14997
14998static const char *
14999get_dynamic_reloc_section_name (bfd * abfd,
15000 asection * sec,
15001 bfd_boolean is_rela)
15002{
ddcf1fcf 15003 char *name;
fd361982 15004 const char *old_name = bfd_section_name (sec);
ddcf1fcf 15005 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 15006
ddcf1fcf 15007 if (old_name == NULL)
83bac4b0
NC
15008 return NULL;
15009
ddcf1fcf 15010 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 15011 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
15012
15013 return name;
15014}
15015
15016/* Returns the dynamic reloc section associated with SEC.
15017 If necessary compute the name of the dynamic reloc section based
15018 on SEC's name (looked up in ABFD's string table) and the setting
15019 of IS_RELA. */
15020
15021asection *
15022_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
15023 asection * sec,
15024 bfd_boolean is_rela)
15025{
15026 asection * reloc_sec = elf_section_data (sec)->sreloc;
15027
15028 if (reloc_sec == NULL)
15029 {
15030 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15031
15032 if (name != NULL)
15033 {
3d4d4302 15034 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
15035
15036 if (reloc_sec != NULL)
15037 elf_section_data (sec)->sreloc = reloc_sec;
15038 }
15039 }
15040
15041 return reloc_sec;
15042}
15043
15044/* Returns the dynamic reloc section associated with SEC. If the
15045 section does not exist it is created and attached to the DYNOBJ
15046 bfd and stored in the SRELOC field of SEC's elf_section_data
15047 structure.
f8076f98 15048
83bac4b0
NC
15049 ALIGNMENT is the alignment for the newly created section and
15050 IS_RELA defines whether the name should be .rela.<SEC's name>
15051 or .rel.<SEC's name>. The section name is looked up in the
15052 string table associated with ABFD. */
15053
15054asection *
ca4be51c
AM
15055_bfd_elf_make_dynamic_reloc_section (asection *sec,
15056 bfd *dynobj,
15057 unsigned int alignment,
15058 bfd *abfd,
15059 bfd_boolean is_rela)
83bac4b0
NC
15060{
15061 asection * reloc_sec = elf_section_data (sec)->sreloc;
15062
15063 if (reloc_sec == NULL)
15064 {
15065 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15066
15067 if (name == NULL)
15068 return NULL;
15069
3d4d4302 15070 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
15071
15072 if (reloc_sec == NULL)
15073 {
3d4d4302
AM
15074 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15075 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
15076 if ((sec->flags & SEC_ALLOC) != 0)
15077 flags |= SEC_ALLOC | SEC_LOAD;
15078
3d4d4302 15079 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
15080 if (reloc_sec != NULL)
15081 {
8877b5e5
AM
15082 /* _bfd_elf_get_sec_type_attr chooses a section type by
15083 name. Override as it may be wrong, eg. for a user
15084 section named "auto" we'll get ".relauto" which is
15085 seen to be a .rela section. */
15086 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15087 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15088 reloc_sec = NULL;
15089 }
15090 }
15091
15092 elf_section_data (sec)->sreloc = reloc_sec;
15093 }
15094
15095 return reloc_sec;
15096}
1338dd10 15097
bffebb6b
AM
15098/* Copy the ELF symbol type and other attributes for a linker script
15099 assignment from HSRC to HDEST. Generally this should be treated as
15100 if we found a strong non-dynamic definition for HDEST (except that
15101 ld ignores multiple definition errors). */
1338dd10 15102void
bffebb6b
AM
15103_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15104 struct bfd_link_hash_entry *hdest,
15105 struct bfd_link_hash_entry *hsrc)
1338dd10 15106{
bffebb6b
AM
15107 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15108 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15109 Elf_Internal_Sym isym;
1338dd10
PB
15110
15111 ehdest->type = ehsrc->type;
35fc36a8 15112 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15113
15114 isym.st_other = ehsrc->other;
5160d0f3 15115 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
1338dd10 15116}
351f65ca
L
15117
15118/* Append a RELA relocation REL to section S in BFD. */
15119
15120void
15121elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15122{
15123 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15124 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15125 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15126 bed->s->swap_reloca_out (abfd, rel, loc);
15127}
15128
15129/* Append a REL relocation REL to section S in BFD. */
15130
15131void
15132elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15133{
15134 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15135 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15136 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15137 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15138}
7dba9362
AM
15139
15140/* Define __start, __stop, .startof. or .sizeof. symbol. */
15141
15142struct bfd_link_hash_entry *
15143bfd_elf_define_start_stop (struct bfd_link_info *info,
15144 const char *symbol, asection *sec)
15145{
487b6440 15146 struct elf_link_hash_entry *h;
7dba9362 15147
487b6440
AM
15148 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15149 FALSE, FALSE, TRUE);
e1b5d517 15150 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
15151 if (h != NULL
15152 && (h->root.type == bfd_link_hash_undefined
15153 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15154 || ((h->ref_regular || h->def_dynamic)
15155 && !h->def_regular
15156 && h->root.type != bfd_link_hash_common)))
7dba9362 15157 {
bf3077a6 15158 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15159 h->verinfo.verdef = NULL;
487b6440
AM
15160 h->root.type = bfd_link_hash_defined;
15161 h->root.u.def.section = sec;
15162 h->root.u.def.value = 0;
15163 h->def_regular = 1;
15164 h->def_dynamic = 0;
15165 h->start_stop = 1;
15166 h->u2.start_stop_section = sec;
15167 if (symbol[0] == '.')
15168 {
15169 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15170 const struct elf_backend_data *bed;
15171 bed = get_elf_backend_data (info->output_bfd);
15172 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15173 }
36b8fda5
AM
15174 else
15175 {
15176 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15177 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15178 | info->start_stop_visibility);
bf3077a6 15179 if (was_dynamic)
36b8fda5
AM
15180 bfd_elf_link_record_dynamic_symbol (info, h);
15181 }
487b6440 15182 return &h->root;
7dba9362 15183 }
487b6440 15184 return NULL;
7dba9362 15185}
5dbc8b37
L
15186
15187/* Find dynamic relocs for H that apply to read-only sections. */
15188
15189asection *
15190_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15191{
15192 struct elf_dyn_relocs *p;
15193
15194 for (p = h->dyn_relocs; p != NULL; p = p->next)
15195 {
15196 asection *s = p->sec->output_section;
15197
15198 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15199 return p->sec;
15200 }
15201 return NULL;
15202}
d49e5065
L
15203
15204/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15205 read-only sections. */
15206
15207bfd_boolean
15208_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15209{
15210 asection *sec;
15211
15212 if (h->root.type == bfd_link_hash_indirect)
15213 return TRUE;
15214
15215 sec = _bfd_elf_readonly_dynrelocs (h);
15216 if (sec != NULL)
15217 {
15218 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15219
15220 info->flags |= DF_TEXTREL;
15221 /* xgettext:c-format */
15222 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15223 "in read-only section `%pA'\n"),
15224 sec->owner, h->root.root.string, sec);
15225
15226 if (bfd_link_textrel_check (info))
15227 /* xgettext:c-format */
15228 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15229 "in read-only section `%pA'\n"),
15230 sec->owner, h->root.root.string, sec);
15231
15232 /* Not an error, just cut short the traversal. */
15233 return FALSE;
15234 }
15235 return TRUE;
15236}
3084d7a2
L
15237
15238/* Add dynamic tags. */
15239
15240bfd_boolean
15241_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15242 bfd_boolean need_dynamic_reloc)
15243{
15244 struct elf_link_hash_table *htab = elf_hash_table (info);
15245
15246 if (htab->dynamic_sections_created)
15247 {
15248 /* Add some entries to the .dynamic section. We fill in the
15249 values later, in finish_dynamic_sections, but we must add
15250 the entries now so that we get the correct size for the
15251 .dynamic section. The DT_DEBUG entry is filled in by the
15252 dynamic linker and used by the debugger. */
15253#define add_dynamic_entry(TAG, VAL) \
15254 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15255
15256 const struct elf_backend_data *bed
15257 = get_elf_backend_data (output_bfd);
15258
15259 if (bfd_link_executable (info))
15260 {
15261 if (!add_dynamic_entry (DT_DEBUG, 0))
15262 return FALSE;
15263 }
15264
15265 if (htab->dt_pltgot_required || htab->splt->size != 0)
15266 {
15267 /* DT_PLTGOT is used by prelink even if there is no PLT
15268 relocation. */
15269 if (!add_dynamic_entry (DT_PLTGOT, 0))
15270 return FALSE;
15271 }
15272
15273 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15274 {
15275 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15276 || !add_dynamic_entry (DT_PLTREL,
15277 (bed->rela_plts_and_copies_p
15278 ? DT_RELA : DT_REL))
15279 || !add_dynamic_entry (DT_JMPREL, 0))
15280 return FALSE;
15281 }
15282
15283 if (htab->tlsdesc_plt
15284 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15285 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15286 return FALSE;
15287
15288 if (need_dynamic_reloc)
15289 {
15290 if (bed->rela_plts_and_copies_p)
15291 {
15292 if (!add_dynamic_entry (DT_RELA, 0)
15293 || !add_dynamic_entry (DT_RELASZ, 0)
15294 || !add_dynamic_entry (DT_RELAENT,
15295 bed->s->sizeof_rela))
15296 return FALSE;
15297 }
15298 else
15299 {
15300 if (!add_dynamic_entry (DT_REL, 0)
15301 || !add_dynamic_entry (DT_RELSZ, 0)
15302 || !add_dynamic_entry (DT_RELENT,
15303 bed->s->sizeof_rel))
15304 return FALSE;
15305 }
15306
15307 /* If any dynamic relocs apply to a read-only section,
15308 then we need a DT_TEXTREL entry. */
15309 if ((info->flags & DF_TEXTREL) == 0)
15310 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15311 info);
15312
15313 if ((info->flags & DF_TEXTREL) != 0)
15314 {
15315 if (htab->ifunc_resolvers)
15316 info->callbacks->einfo
15317 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15318 "may result in a segfault at runtime; recompile with %s\n"),
15319 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15320
15321 if (!add_dynamic_entry (DT_TEXTREL, 0))
15322 return FALSE;
15323 }
15324 }
15325 }
15326#undef add_dynamic_entry
15327
15328 return TRUE;
15329}
This page took 2.933531 seconds and 4 git commands to generate.