]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Update year range in copyright notice of binutils files
[thirdparty/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;
67687978
PB
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
7a13edea 539 }
0444bdd4 540
7a13edea
NC
541 default:
542 break;
543 }
544
252b5132
RH
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
547
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
550 {
551 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 553 if (dynstr == NULL)
b34976b6 554 return FALSE;
252b5132
RH
555 }
556
557 /* We don't put any version information in the dynamic string
aad5d350 558 table. */
252b5132
RH
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
568
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
570
571 if (p != NULL)
572 *p = ELF_VER_CHR;
252b5132 573
ef53be89 574 if (indx == (size_t) -1)
b34976b6 575 return FALSE;
252b5132
RH
576 h->dynstr_index = indx;
577 }
578
b34976b6 579 return TRUE;
252b5132 580}
45d6a902 581\f
55255dae
L
582/* Mark a symbol dynamic. */
583
28caa186 584static void
55255dae 585bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
55255dae 588{
40b36307 589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 590
40b36307 591 /* It may be called more than once on the same H. */
0e1862bb 592 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
593 return;
594
40b36307
L
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
b8871f35 597 || h->type == STT_COMMON
40b36307 598 || (sym != NULL
b8871f35
L
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 601 || (d != NULL
73ec947d 602 && h->non_elf
40b36307 603 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
604 {
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
609 }
55255dae
L
610}
611
45d6a902
AM
612/* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
614
615bfd_boolean
fe21a8fc
L
616bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
268b6b39 618 const char *name,
fe21a8fc
L
619 bfd_boolean provide,
620 bfd_boolean hidden)
45d6a902 621{
00cbee0a 622 struct elf_link_hash_entry *h, *hv;
4ea42fb7 623 struct elf_link_hash_table *htab;
00cbee0a 624 const struct elf_backend_data *bed;
45d6a902 625
0eddce27 626 if (!is_elf_hash_table (info->hash))
45d6a902
AM
627 return TRUE;
628
4ea42fb7
AM
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 631 if (h == NULL)
4ea42fb7 632 return provide;
45d6a902 633
8e2a4f11
AM
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
636
0f550b3d
L
637 if (h->versioned == unknown)
638 {
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
642 {
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
647 }
648 }
649
73ec947d
AM
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
653 {
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
656 }
657
00cbee0a 658 switch (h->root.type)
77cfaee6 659 {
00cbee0a
L
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
4ea42fb7 669 h->root.type = bfd_link_hash_new;
77cfaee6
AM
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
672 break;
673 case bfd_link_hash_new:
00cbee0a
L
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 677 the versioned symbol point to this one. */
00cbee0a
L
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
8e2a4f11
AM
690 default:
691 BFD_FAIL ();
c2596ca5 692 return FALSE;
55255dae 693 }
45d6a902
AM
694
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
f5385ebf
AM
700 && h->def_dynamic
701 && !h->def_regular)
45d6a902
AM
702 h->root.type = bfd_link_hash_undefined;
703
48e30f52
L
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
b531344c
MR
709 h->verinfo.verdef = NULL;
710
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
45d6a902 713
f5385ebf 714 h->def_regular = 1;
45d6a902 715
eb8476a6 716 if (hidden)
fe21a8fc 717 {
91d6fa6a 718 bed = get_elf_backend_data (output_bfd);
b8297068
AM
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
722 }
723
6fa3860b
PB
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
0e1862bb 726 if (!bfd_link_relocatable (info)
6fa3860b
PB
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
731
f5385ebf
AM
732 if ((h->def_dynamic
733 || h->ref_dynamic
6b3b0ab8
L
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 736 && !h->forced_local
45d6a902
AM
737 && h->dynindx == -1)
738 {
c152c796 739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
740 return FALSE;
741
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
60d67dc8 745 if (h->is_weakalias)
45d6a902 746 {
60d67dc8
AM
747 struct elf_link_hash_entry *def = weakdef (h);
748
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
751 return FALSE;
752 }
753 }
754
755 return TRUE;
756}
42751cf3 757
8c58d23b
AM
758/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
761
762int
c152c796
AM
763bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
8c58d23b 766{
986f0783 767 size_t amt;
8c58d23b
AM
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
ef53be89 771 size_t dynstr_index;
8c58d23b
AM
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
775
0eddce27 776 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
777 return 0;
778
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
783
784 amt = sizeof (*entry);
a50b1753 785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
786 if (entry == NULL)
787 return 0;
788
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 791 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
792 {
793 bfd_release (input_bfd, entry);
794 return 0;
795 }
796
797 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 798 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
799 {
800 asection *s;
801
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
804 {
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
809 }
810 }
811
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
815
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
818 {
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
823 }
824
b34976b6 825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 826 if (dynstr_index == (size_t) -1)
8c58d23b
AM
827 return 0;
828 entry->isym.st_name = dynstr_index;
829
830 eht = elf_hash_table (info);
831
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
837
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
841
842 /* The dynindx will be set at the end of size_dynamic_sections. */
843
844 return 1;
845}
846
30b30c21 847/* Return the dynindex of a local dynamic symbol. */
42751cf3 848
30b30c21 849long
268b6b39
AM
850_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
30b30c21
RH
853{
854 struct elf_link_local_dynamic_entry *e;
855
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
860}
861
862/* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
865
b34976b6 866static bfd_boolean
268b6b39
AM
867elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
42751cf3 869{
a50b1753 870 size_t *count = (size_t *) data;
30b30c21 871
6fa3860b
PB
872 if (h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879}
880
881
882/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
884
885static bfd_boolean
886elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888{
a50b1753 889 size_t *count = (size_t *) data;
6fa3860b 890
6fa3860b
PB
891 if (!h->forced_local)
892 return TRUE;
893
42751cf3 894 if (h->dynindx != -1)
30b30c21
RH
895 h->dynindx = ++(*count);
896
b34976b6 897 return TRUE;
42751cf3 898}
30b30c21 899
aee6f5b4
AO
900/* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902bfd_boolean
d00dd7dc
AM
903_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
aee6f5b4 906{
74541ad4 907 struct elf_link_hash_table *htab;
ca55926c 908 asection *ip;
74541ad4 909
aee6f5b4
AO
910 switch (elf_section_data (p)->this_hdr.sh_type)
911 {
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
74541ad4 917 htab = elf_hash_table (info);
74541ad4
AM
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
920
ca55926c 921 return (htab->dynobj != NULL
3d4d4302 922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 923 && ip->output_section == p);
aee6f5b4
AO
924
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
929 }
930}
931
d00dd7dc
AM
932bfd_boolean
933_bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
937{
938 return TRUE;
939}
940
062e2358 941/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
30b30c21 949
554220db
AM
950static unsigned long
951_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
30b30c21
RH
954{
955 unsigned long dynsymcount = 0;
63f452a8 956 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 957
0e1862bb
L
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 960 {
aee6f5b4 961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 964 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 965 && (p->flags & SEC_ALLOC) != 0
7f923b7f 966 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
968 {
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
972 }
973 else if (do_sec)
74541ad4 974 elf_section_data (p)->dynindx = 0;
30b30c21 975 }
63f452a8
AM
976 if (do_sec)
977 *section_sym_count = dynsymcount;
30b30c21 978
6fa3860b
PB
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
982
30b30c21
RH
983 if (elf_hash_table (info)->dynlocal)
984 {
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
988 }
90ac2420 989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
990
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
994
d5486c43
L
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
30b30c21 1000
ccabcbe5
AM
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
30b30c21 1003}
252b5132 1004
54ac0771
L
1005/* Merge st_other field. */
1006
1007static void
1008elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
5160d0f3 1009 unsigned int st_other, asection *sec,
cd3416da 1010 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1011{
1012 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1013
1014 /* If st_other has a processor-specific meaning, specific
cd3416da 1015 code might be needed here. */
54ac0771 1016 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1017 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1018 dynamic);
1019
cd3416da 1020 if (!dynamic)
54ac0771 1021 {
5160d0f3 1022 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 1023 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1024
cd3416da
AM
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis - 1 < hvis - 1)
1028 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1029 }
b8417128 1030 else if (definition
5160d0f3 1031 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 1032 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1033 h->protected_def = 1;
54ac0771
L
1034}
1035
4f3fedcf
AM
1036/* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
45d6a902 1048
8a56bd02 1049static bfd_boolean
268b6b39
AM
1050_bfd_elf_merge_symbol (bfd *abfd,
1051 struct bfd_link_info *info,
1052 const char *name,
1053 Elf_Internal_Sym *sym,
1054 asection **psec,
1055 bfd_vma *pvalue,
4f3fedcf
AM
1056 struct elf_link_hash_entry **sym_hash,
1057 bfd **poldbfd,
37a9e49a 1058 bfd_boolean *pold_weak,
af44c138 1059 unsigned int *pold_alignment,
268b6b39 1060 bfd_boolean *skip,
7ba11550 1061 bfd **override,
268b6b39 1062 bfd_boolean *type_change_ok,
6e33951e
L
1063 bfd_boolean *size_change_ok,
1064 bfd_boolean *matched)
252b5132 1065{
7479dfd4 1066 asection *sec, *oldsec;
45d6a902 1067 struct elf_link_hash_entry *h;
90c984fc 1068 struct elf_link_hash_entry *hi;
45d6a902
AM
1069 struct elf_link_hash_entry *flip;
1070 int bind;
1071 bfd *oldbfd;
1072 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1073 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1074 const struct elf_backend_data *bed;
6e33951e 1075 char *new_version;
93f4de39 1076 bfd_boolean default_sym = *matched;
45d6a902
AM
1077
1078 *skip = FALSE;
7ba11550 1079 *override = NULL;
45d6a902
AM
1080
1081 sec = *psec;
1082 bind = ELF_ST_BIND (sym->st_info);
1083
1084 if (! bfd_is_und_section (sec))
1085 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1086 else
1087 h = ((struct elf_link_hash_entry *)
1088 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1089 if (h == NULL)
1090 return FALSE;
1091 *sym_hash = h;
252b5132 1092
88ba32a0
L
1093 bed = get_elf_backend_data (abfd);
1094
6e33951e 1095 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1096 if (h->versioned != unversioned)
6e33951e 1097 {
422f1182
L
1098 /* Symbol version is unknown or versioned. */
1099 new_version = strrchr (name, ELF_VER_CHR);
1100 if (new_version)
1101 {
1102 if (h->versioned == unknown)
1103 {
1104 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1105 h->versioned = versioned_hidden;
1106 else
1107 h->versioned = versioned;
1108 }
1109 new_version += 1;
1110 if (new_version[0] == '\0')
1111 new_version = NULL;
1112 }
1113 else
1114 h->versioned = unversioned;
6e33951e 1115 }
422f1182
L
1116 else
1117 new_version = NULL;
6e33951e 1118
90c984fc
L
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1121 hi = h;
45d6a902
AM
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1125
6e33951e
L
1126 if (!*matched)
1127 {
1128 if (hi == h || h->root.type == bfd_link_hash_new)
1129 *matched = TRUE;
1130 else
1131 {
ae7683d2 1132 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1133 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1134 true if the new symbol is only visible to the symbol with
6e33951e 1135 the same symbol version. */
422f1182
L
1136 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1137 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1138 if (!old_hidden && !new_hidden)
1139 /* The new symbol matches the existing symbol if both
1140 aren't hidden. */
1141 *matched = TRUE;
1142 else
1143 {
1144 /* OLD_VERSION is the symbol version of the existing
1145 symbol. */
422f1182
L
1146 char *old_version;
1147
1148 if (h->versioned >= versioned)
1149 old_version = strrchr (h->root.root.string,
1150 ELF_VER_CHR) + 1;
1151 else
1152 old_version = NULL;
6e33951e
L
1153
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched = (old_version == new_version
1157 || (old_version != NULL
1158 && new_version != NULL
1159 && strcmp (old_version, new_version) == 0));
1160 }
1161 }
1162 }
1163
934bce08
AM
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1165 existing symbol. */
1166
1167 oldbfd = NULL;
1168 oldsec = NULL;
1169 switch (h->root.type)
1170 {
1171 default:
1172 break;
1173
1174 case bfd_link_hash_undefined:
1175 case bfd_link_hash_undefweak:
1176 oldbfd = h->root.u.undef.abfd;
1177 break;
1178
1179 case bfd_link_hash_defined:
1180 case bfd_link_hash_defweak:
1181 oldbfd = h->root.u.def.section->owner;
1182 oldsec = h->root.u.def.section;
1183 break;
1184
1185 case bfd_link_hash_common:
1186 oldbfd = h->root.u.c.p->section->owner;
1187 oldsec = h->root.u.c.p->section;
1188 if (pold_alignment)
1189 *pold_alignment = h->root.u.c.p->alignment_power;
1190 break;
1191 }
1192 if (poldbfd && *poldbfd == NULL)
1193 *poldbfd = oldbfd;
1194
1195 /* Differentiate strong and weak symbols. */
1196 newweak = bind == STB_WEAK;
1197 oldweak = (h->root.type == bfd_link_hash_defweak
1198 || h->root.type == bfd_link_hash_undefweak);
1199 if (pold_weak)
1200 *pold_weak = oldweak;
1201
40b36307 1202 /* We have to check it for every instance since the first few may be
ee659f1f 1203 references and not all compilers emit symbol type for undefined
40b36307
L
1204 symbols. */
1205 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1206
ee659f1f
AM
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1209
1210 newdyn = (abfd->flags & DYNAMIC) != 0;
1211
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1219 if (newdyn)
1220 {
1221 if (bfd_is_und_section (sec))
1222 {
1223 if (bind != STB_WEAK)
1224 {
1225 h->ref_dynamic_nonweak = 1;
1226 hi->ref_dynamic_nonweak = 1;
1227 }
1228 }
1229 else
1230 {
6e33951e
L
1231 /* Update the existing symbol only if they match. */
1232 if (*matched)
1233 h->dynamic_def = 1;
ee659f1f
AM
1234 hi->dynamic_def = 1;
1235 }
1236 }
1237
45d6a902
AM
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1241
1242 if (h->root.type == bfd_link_hash_new)
252b5132 1243 {
f5385ebf 1244 h->non_elf = 0;
45d6a902
AM
1245 return TRUE;
1246 }
252b5132 1247
45d6a902
AM
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1254 if (abfd == oldbfd
895fa45f 1255 && (newweak || oldweak)
45d6a902 1256 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1257 || !h->def_regular))
45d6a902
AM
1258 return TRUE;
1259
707bba77 1260 olddyn = FALSE;
45d6a902
AM
1261 if (oldbfd != NULL)
1262 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1263 else if (oldsec != NULL)
45d6a902 1264 {
707bba77 1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1266 indices used by MIPS ELF. */
707bba77 1267 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1268 }
252b5132 1269
1a3b5c34
AM
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1272 if (oldbfd != NULL
1273 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1274 && newdyn != olddyn)
1275 {
1276 h->root.non_ir_ref_dynamic = TRUE;
1277 hi->root.non_ir_ref_dynamic = TRUE;
1278 }
1279
45d6a902
AM
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1282
707bba77 1283 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1284
707bba77
AM
1285 olddef = (h->root.type != bfd_link_hash_undefined
1286 && h->root.type != bfd_link_hash_undefweak
202ac193 1287 && h->root.type != bfd_link_hash_common);
45d6a902 1288
0a36a439
L
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1291
1292 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1293 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1294
1295 oldfunc = (h->type != STT_NOTYPE
1296 && bed->is_function_type (h->type));
1297
c5d37467 1298 if (!(newfunc && oldfunc)
5b677558
AM
1299 && ELF_ST_TYPE (sym->st_info) != h->type
1300 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1301 && h->type != STT_NOTYPE
c5d37467
AM
1302 && (newdef || bfd_is_com_section (sec))
1303 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1304 {
c5d37467
AM
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1310 if (newdyn
1311 && !olddyn)
1312 {
1313 *skip = TRUE;
1314 return TRUE;
1315 }
1316
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1319 dynamic state. */
1320 if (hi != h
1321 && !newdyn
1322 && olddyn)
1323 {
1324 h = hi;
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 h->def_dynamic = 0;
1329 h->dynamic_def = 0;
1330 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1331 {
1332 h->root.type = bfd_link_hash_undefined;
1333 h->root.u.undef.abfd = abfd;
1334 }
1335 else
1336 {
1337 h->root.type = bfd_link_hash_new;
1338 h->root.u.undef.abfd = NULL;
1339 }
1340 return TRUE;
1341 }
580a2b6e
L
1342 }
1343
4c34aff8
AM
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1347 if (oldbfd != NULL
1348 && (oldbfd->flags & BFD_PLUGIN) == 0
1349 && (abfd->flags & BFD_PLUGIN) == 0
1350 && ELF_ST_TYPE (sym->st_info) != h->type
1351 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1352 {
1353 bfd *ntbfd, *tbfd;
1354 bfd_boolean ntdef, tdef;
1355 asection *ntsec, *tsec;
1356
1357 if (h->type == STT_TLS)
1358 {
3b36f7e6 1359 ntbfd = abfd;
7479dfd4
L
1360 ntsec = sec;
1361 ntdef = newdef;
1362 tbfd = oldbfd;
1363 tsec = oldsec;
1364 tdef = olddef;
1365 }
1366 else
1367 {
1368 ntbfd = oldbfd;
1369 ntsec = oldsec;
1370 ntdef = olddef;
1371 tbfd = abfd;
1372 tsec = sec;
1373 tdef = newdef;
1374 }
1375
1376 if (tdef && ntdef)
4eca0228 1377 _bfd_error_handler
695344c0 1378 /* xgettext:c-format */
871b3ab2
AM
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1381 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1382 else if (!tdef && !ntdef)
4eca0228 1383 _bfd_error_handler
695344c0 1384 /* xgettext:c-format */
871b3ab2
AM
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
c08bb8dd 1387 h->root.root.string, tbfd, ntbfd);
7479dfd4 1388 else if (tdef)
4eca0228 1389 _bfd_error_handler
695344c0 1390 /* xgettext:c-format */
871b3ab2
AM
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
c08bb8dd 1393 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1394 else
4eca0228 1395 _bfd_error_handler
695344c0 1396 /* xgettext:c-format */
871b3ab2
AM
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1399 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1400
1401 bfd_set_error (bfd_error_bad_value);
1402 return FALSE;
1403 }
1404
45d6a902
AM
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1407 if (newdyn
9c7a29a3 1408 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1409 && !bfd_is_und_section (sec))
1410 {
1411 *skip = TRUE;
1412 /* Make sure this symbol is dynamic. */
f5385ebf 1413 h->ref_dynamic = 1;
90c984fc 1414 hi->ref_dynamic = 1;
45d6a902
AM
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1417
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1420 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1421 else
1422 return TRUE;
1423 }
1424 else if (!newdyn
9c7a29a3 1425 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1426 && h->def_dynamic)
45d6a902
AM
1427 {
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
6c9b78e6 1431 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1432 {
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1437 if (h->ref_regular)
1438 {
6c9b78e6 1439 hi->root.type = h->root.type;
d2dee3b2 1440 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1441 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1442
6c9b78e6 1443 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1444 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1445 {
aed81c4e
MR
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 h->forced_local = 0;
1450 h->ref_dynamic = 0;
d2dee3b2
L
1451 }
1452 else
aed81c4e
MR
1453 h->ref_dynamic = 1;
1454
1455 h->def_dynamic = 0;
aed81c4e
MR
1456 /* FIXME: Should we check type and size for protected symbol? */
1457 h->size = 0;
1458 h->type = 0;
1459
6c9b78e6 1460 h = hi;
d2dee3b2
L
1461 }
1462 else
6c9b78e6 1463 h = hi;
d2dee3b2 1464 }
1de1a317 1465
f5eda473
AM
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1474 {
1de1a317 1475 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1476 h->root.u.undef.abfd = abfd;
1477 }
1478 else
1479 {
1480 h->root.type = bfd_link_hash_new;
1481 h->root.u.undef.abfd = NULL;
1482 }
1483
f5eda473 1484 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1485 {
f5eda473
AM
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1489 h->forced_local = 0;
1490 h->ref_dynamic = 0;
45d6a902 1491 }
f5eda473
AM
1492 else
1493 h->ref_dynamic = 1;
1494 h->def_dynamic = 0;
45d6a902
AM
1495 /* FIXME: Should we check type and size for protected symbol? */
1496 h->size = 0;
1497 h->type = 0;
1498 return TRUE;
1499 }
14a793b2 1500
15b43f48
AM
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1508
165f707a
AM
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1513
15b43f48
AM
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1516
165f707a 1517 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1518 newweak = FALSE;
15b43f48 1519 if (olddef && newdyn)
0f8a2703
AM
1520 oldweak = FALSE;
1521
d334575b 1522 /* Allow changes between different types of function symbol. */
0a36a439 1523 if (newfunc && oldfunc)
fcb93ecf
PB
1524 *type_change_ok = TRUE;
1525
79349b09
AM
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
252b5132 1529
79349b09
AM
1530 if (oldweak
1531 || newweak
1532 || (newdef
1533 && h->root.type == bfd_link_hash_undefined))
1534 *type_change_ok = TRUE;
1535
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1538
1539 if (*type_change_ok
1540 || h->root.type == bfd_link_hash_undefined)
1541 *size_change_ok = TRUE;
45d6a902 1542
45d6a902
AM
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1554 libraries.
1555
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1558
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1564 harmless. */
1565
1566 if (newdyn
1567 && newdef
79349b09 1568 && !newweak
45d6a902
AM
1569 && (sec->flags & SEC_ALLOC) != 0
1570 && (sec->flags & SEC_LOAD) == 0
1571 && sym->st_size > 0
0a36a439 1572 && !newfunc)
45d6a902
AM
1573 newdyncommon = TRUE;
1574 else
1575 newdyncommon = FALSE;
1576
1577 if (olddyn
1578 && olddef
1579 && h->root.type == bfd_link_hash_defined
f5385ebf 1580 && h->def_dynamic
45d6a902
AM
1581 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1582 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1583 && h->size > 0
0a36a439 1584 && !oldfunc)
45d6a902
AM
1585 olddyncommon = TRUE;
1586 else
1587 olddyncommon = FALSE;
1588
a4d8e49b
L
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
5d13b3b3
AM
1591 if (bed->merge_symbol != NULL)
1592 {
1593 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1594 return FALSE;
1595 sec = *psec;
1596 }
a4d8e49b 1597
a83ef4d1
L
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
93f4de39 1600 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1601 && !default_sym && h->def_regular
1602 && !(oldbfd != NULL
1603 && (oldbfd->flags & BFD_PLUGIN) != 0
1604 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1605 {
1606 /* Handle a multiple definition. */
1607 (*info->callbacks->multiple_definition) (info, &h->root,
1608 abfd, sec, *pvalue);
1609 *skip = TRUE;
1610 return TRUE;
1611 }
1612
45d6a902
AM
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1615 two. */
1616
1617 if (olddyncommon
1618 && newdyncommon
1619 && sym->st_size != h->size)
1620 {
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1626
1a72702b
AM
1627 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1628 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1629 if (sym->st_size > h->size)
1630 h->size = sym->st_size;
252b5132 1631
45d6a902 1632 *size_change_ok = TRUE;
252b5132
RH
1633 }
1634
45d6a902
AM
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1641
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
8170f769 1647 object to override a weak symbol in a shared object. */
45d6a902
AM
1648
1649 if (newdyn
1650 && newdef
77cfaee6 1651 && (olddef
45d6a902 1652 || (h->root.type == bfd_link_hash_common
8170f769 1653 && (newweak || newfunc))))
45d6a902 1654 {
7ba11550 1655 *override = abfd;
45d6a902
AM
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
252b5132 1658
45d6a902
AM
1659 *psec = sec = bfd_und_section_ptr;
1660 *size_change_ok = TRUE;
252b5132 1661
45d6a902
AM
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
252b5132 1667
45d6a902
AM
1668 if (h->root.type == bfd_link_hash_common)
1669 *type_change_ok = TRUE;
1670 }
1671
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1676 right thing. */
45d6a902
AM
1677
1678 if (newdyncommon
1679 && h->root.type == bfd_link_hash_common)
1680 {
7ba11550 1681 *override = oldbfd;
45d6a902
AM
1682 newdef = FALSE;
1683 newdyncommon = FALSE;
1684 *pvalue = sym->st_size;
a4d8e49b 1685 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1686 *size_change_ok = TRUE;
1687 }
1688
c5e2cead 1689 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1690 if (newdef && olddef && newweak)
54ac0771 1691 {
35ed3f94 1692 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1693 if (!(oldbfd != NULL
1694 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1695 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1696 {
1697 newdef = FALSE;
1698 *skip = TRUE;
1699 }
54ac0771
L
1700
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1703 local symbol. */
5160d0f3 1704 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
54ac0771
L
1705 if (h->dynindx != -1)
1706 switch (ELF_ST_VISIBILITY (h->other))
1707 {
1708 case STV_INTERNAL:
1709 case STV_HIDDEN:
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1711 break;
1712 }
1713 }
c5e2cead 1714
45d6a902
AM
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1720
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
0f8a2703 1723 symbol is a function or is weak. */
45d6a902
AM
1724
1725 flip = NULL;
77cfaee6 1726 if (!newdyn
45d6a902
AM
1727 && (newdef
1728 || (bfd_is_com_section (sec)
0a36a439 1729 && (oldweak || oldfunc)))
45d6a902
AM
1730 && olddyn
1731 && olddef
f5385ebf 1732 && h->def_dynamic)
45d6a902
AM
1733 {
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1736 new definition. */
1737
1738 h->root.type = bfd_link_hash_undefined;
1739 h->root.u.undef.abfd = h->root.u.def.section->owner;
1740 *size_change_ok = TRUE;
1741
1742 olddef = FALSE;
1743 olddyncommon = FALSE;
1744
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1747
1748 if (bfd_is_com_section (sec))
0a36a439
L
1749 {
1750 if (oldfunc)
1751 {
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1754 function. */
1755 h->def_dynamic = 0;
1756 h->type = STT_NOTYPE;
1757 }
1758 *type_change_ok = TRUE;
1759 }
45d6a902 1760
6c9b78e6
AM
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
45d6a902
AM
1763 else
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h->verinfo.vertree = NULL;
1768 }
1769
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1775
1776 if (! newdyn
1777 && bfd_is_com_section (sec)
1778 && olddyncommon)
1779 {
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1a72702b
AM
1783 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1784 bfd_link_hash_common, sym->st_size);
45d6a902 1785
4cc11e76 1786 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1787 larger, pretend that the new symbol has its size. */
1788
1789 if (h->size > *pvalue)
1790 *pvalue = h->size;
1791
af44c138
L
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment);
1795 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1796
1797 olddef = FALSE;
1798 olddyncommon = FALSE;
1799
1800 h->root.type = bfd_link_hash_undefined;
1801 h->root.u.undef.abfd = h->root.u.def.section->owner;
1802
1803 *size_change_ok = TRUE;
1804 *type_change_ok = TRUE;
1805
6c9b78e6
AM
1806 if (hi->root.type == bfd_link_hash_indirect)
1807 flip = hi;
45d6a902
AM
1808 else
1809 h->verinfo.vertree = NULL;
1810 }
1811
1812 if (flip != NULL)
1813 {
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
45d6a902 1817 flip->root.type = h->root.type;
00cbee0a 1818 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1819 h->root.type = bfd_link_hash_indirect;
1820 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1821 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1822 if (h->def_dynamic)
45d6a902 1823 {
f5385ebf
AM
1824 h->def_dynamic = 0;
1825 flip->ref_dynamic = 1;
45d6a902
AM
1826 }
1827 }
1828
45d6a902
AM
1829 return TRUE;
1830}
1831
1832/* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
4f3fedcf 1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1835 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1836
28caa186 1837static bfd_boolean
268b6b39
AM
1838_bfd_elf_add_default_symbol (bfd *abfd,
1839 struct bfd_link_info *info,
1840 struct elf_link_hash_entry *h,
1841 const char *name,
1842 Elf_Internal_Sym *sym,
4f3fedcf
AM
1843 asection *sec,
1844 bfd_vma value,
1845 bfd **poldbfd,
e3c9d234 1846 bfd_boolean *dynsym)
45d6a902
AM
1847{
1848 bfd_boolean type_change_ok;
1849 bfd_boolean size_change_ok;
1850 bfd_boolean skip;
1851 char *shortname;
1852 struct elf_link_hash_entry *hi;
1853 struct bfd_link_hash_entry *bh;
9c5bfbb7 1854 const struct elf_backend_data *bed;
45d6a902
AM
1855 bfd_boolean collect;
1856 bfd_boolean dynamic;
7ba11550 1857 bfd *override;
45d6a902
AM
1858 char *p;
1859 size_t len, shortlen;
ffd65175 1860 asection *tmp_sec;
6e33951e 1861 bfd_boolean matched;
45d6a902 1862
422f1182
L
1863 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1864 return TRUE;
1865
45d6a902
AM
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p = strchr (name, ELF_VER_CHR);
422f1182
L
1871 if (h->versioned == unknown)
1872 {
1873 if (p == NULL)
1874 {
1875 h->versioned = unversioned;
1876 return TRUE;
1877 }
1878 else
1879 {
1880 if (p[1] != ELF_VER_CHR)
1881 {
1882 h->versioned = versioned_hidden;
1883 return TRUE;
1884 }
1885 else
1886 h->versioned = versioned;
1887 }
1888 }
4373f8af
L
1889 else
1890 {
1891 /* PR ld/19073: We may see an unversioned definition after the
1892 default version. */
1893 if (p == NULL)
1894 return TRUE;
1895 }
45d6a902 1896
45d6a902
AM
1897 bed = get_elf_backend_data (abfd);
1898 collect = bed->collect;
1899 dynamic = (abfd->flags & DYNAMIC) != 0;
1900
1901 shortlen = p - name;
a50b1753 1902 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1903 if (shortname == NULL)
1904 return FALSE;
1905 memcpy (shortname, name, shortlen);
1906 shortname[shortlen] = '\0';
1907
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok = FALSE;
1913 size_change_ok = FALSE;
6e33951e 1914 matched = TRUE;
ffd65175
AM
1915 tmp_sec = sec;
1916 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1917 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1918 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1919 return FALSE;
1920
1921 if (skip)
1922 goto nondefault;
1923
5fa370e4 1924 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1925 {
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1932 {
1933 bfd_boolean hide;
1934
1935 hi->verinfo.vertree
1936 = bfd_find_version_for_sym (info->version_info,
1937 hi->root.root.string, &hide);
1938 if (hi->verinfo.vertree != NULL && hide)
1939 {
1940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1941 goto nondefault;
1942 }
1943 }
1944 if (hi->verinfo.vertree != NULL
1945 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1946 goto nondefault;
1947 }
1948
45d6a902
AM
1949 if (! override)
1950 {
c6e8a9a8 1951 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1952 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1953 {
1954 bh = &hi->root;
fbcc8baf 1955 if (bh->type == bfd_link_hash_defined
6cc71b82 1956 && bh->u.def.section->owner != NULL
fbcc8baf
L
1957 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1958 {
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1961 it. */
1962 bh->type = bfd_link_hash_undefined;
1963 bh->u.undef.abfd = bh->u.def.section->owner;
1964 }
c6e8a9a8
L
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info, abfd, shortname, BSF_INDIRECT,
1967 bfd_ind_section_ptr,
1968 0, name, FALSE, collect, &bh)))
1969 return FALSE;
1970 hi = (struct elf_link_hash_entry *) bh;
1971 }
45d6a902
AM
1972 }
1973 else
1974 {
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1980
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1984
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1992
1993 while (hi->root.type == bfd_link_hash_indirect
1994 || hi->root.type == bfd_link_hash_warning)
1995 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996
1997 h->root.type = bfd_link_hash_indirect;
1998 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1999 if (h->def_dynamic)
45d6a902 2000 {
f5385ebf
AM
2001 h->def_dynamic = 0;
2002 hi->ref_dynamic = 1;
2003 if (hi->ref_regular
2004 || hi->def_regular)
45d6a902 2005 {
c152c796 2006 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2007 return FALSE;
2008 }
2009 }
2010
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2013 hi = h;
2014 }
2015
fab4a87f
L
2016 /* Check if HI is a warning symbol. */
2017 if (hi->root.type == bfd_link_hash_warning)
2018 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2019
45d6a902
AM
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2023
2024 if (hi->root.type == bfd_link_hash_indirect)
2025 {
2026 struct elf_link_hash_entry *ht;
2027
45d6a902 2028 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2029 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2030
5160d0f3
AM
2031 /* If we first saw a reference to SHORTNAME with non-default
2032 visibility, merge that visibility to the @@VER symbol. */
2033 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2034
68c88cd4
AM
2035 /* A reference to the SHORTNAME symbol from a dynamic library
2036 will be satisfied by the versioned symbol at runtime. In
2037 effect, we have a reference to the versioned symbol. */
2038 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2039 hi->dynamic_def |= ht->dynamic_def;
2040
45d6a902
AM
2041 /* See if the new flags lead us to realize that the symbol must
2042 be dynamic. */
2043 if (! *dynsym)
2044 {
2045 if (! dynamic)
2046 {
0e1862bb 2047 if (! bfd_link_executable (info)
90c984fc 2048 || hi->def_dynamic
f5385ebf 2049 || hi->ref_dynamic)
45d6a902
AM
2050 *dynsym = TRUE;
2051 }
2052 else
2053 {
f5385ebf 2054 if (hi->ref_regular)
45d6a902
AM
2055 *dynsym = TRUE;
2056 }
2057 }
2058 }
2059
2060 /* We also need to define an indirection from the nondefault version
2061 of the symbol. */
2062
dc1e8a47 2063 nondefault:
45d6a902 2064 len = strlen (name);
a50b1753 2065 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2066 if (shortname == NULL)
2067 return FALSE;
2068 memcpy (shortname, name, shortlen);
2069 memcpy (shortname + shortlen, p + 1, len - shortlen);
2070
2071 /* Once again, merge with any existing symbol. */
2072 type_change_ok = FALSE;
2073 size_change_ok = FALSE;
ffd65175
AM
2074 tmp_sec = sec;
2075 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2076 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2077 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2078 return FALSE;
2079
2080 if (skip)
726d7d1e
AM
2081 {
2082 if (!dynamic
2083 && h->root.type == bfd_link_hash_defweak
2084 && hi->root.type == bfd_link_hash_defined)
2085 {
2086 /* We are handling a weak sym@@ver and attempting to define
2087 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2088 new weak sym@ver because there is already a strong sym@ver.
2089 However, sym@ver and sym@@ver are really the same symbol.
2090 The existing strong sym@ver ought to override sym@@ver. */
2091 h->root.type = bfd_link_hash_defined;
2092 h->root.u.def.section = hi->root.u.def.section;
2093 h->root.u.def.value = hi->root.u.def.value;
2094 hi->root.type = bfd_link_hash_indirect;
2095 hi->root.u.i.link = &h->root;
2096 }
2097 else
2098 return TRUE;
2099 }
2100 else if (override)
45d6a902
AM
2101 {
2102 /* Here SHORTNAME is a versioned name, so we don't expect to see
2103 the type of override we do in the case above unless it is
4cc11e76 2104 overridden by a versioned definition. */
45d6a902
AM
2105 if (hi->root.type != bfd_link_hash_defined
2106 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2107 _bfd_error_handler
695344c0 2108 /* xgettext:c-format */
871b3ab2 2109 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2110 abfd, shortname);
726d7d1e 2111 return TRUE;
45d6a902
AM
2112 }
2113 else
2114 {
2115 bh = &hi->root;
2116 if (! (_bfd_generic_link_add_one_symbol
2117 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2118 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2119 return FALSE;
2120 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2121 }
45d6a902 2122
726d7d1e
AM
2123 /* If there is a duplicate definition somewhere, then HI may not
2124 point to an indirect symbol. We will have reported an error
2125 to the user in that case. */
2126 if (hi->root.type == bfd_link_hash_indirect)
2127 {
2128 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2129 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2130 hi->dynamic_def |= h->dynamic_def;
45d6a902 2131
726d7d1e
AM
2132 /* If we first saw a reference to @VER symbol with
2133 non-default visibility, merge that visibility to the
2134 @@VER symbol. */
2135 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2136
2137 /* See if the new flags lead us to realize that the symbol
2138 must be dynamic. */
2139 if (! *dynsym)
45d6a902 2140 {
726d7d1e 2141 if (! dynamic)
45d6a902 2142 {
726d7d1e
AM
2143 if (! bfd_link_executable (info)
2144 || hi->ref_dynamic)
2145 *dynsym = TRUE;
2146 }
2147 else
2148 {
2149 if (hi->ref_regular)
2150 *dynsym = TRUE;
45d6a902
AM
2151 }
2152 }
2153 }
2154
2155 return TRUE;
2156}
2157\f
2158/* This routine is used to export all defined symbols into the dynamic
2159 symbol table. It is called via elf_link_hash_traverse. */
2160
28caa186 2161static bfd_boolean
268b6b39 2162_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2163{
a50b1753 2164 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2165
2166 /* Ignore indirect symbols. These are added by the versioning code. */
2167 if (h->root.type == bfd_link_hash_indirect)
2168 return TRUE;
2169
7686d77d
AM
2170 /* Ignore this if we won't export it. */
2171 if (!eif->info->export_dynamic && !h->dynamic)
2172 return TRUE;
45d6a902
AM
2173
2174 if (h->dynindx == -1
fd91d419
L
2175 && (h->def_regular || h->ref_regular)
2176 && ! bfd_hide_sym_by_version (eif->info->version_info,
2177 h->root.root.string))
45d6a902 2178 {
fd91d419 2179 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2180 {
fd91d419
L
2181 eif->failed = TRUE;
2182 return FALSE;
45d6a902
AM
2183 }
2184 }
2185
2186 return TRUE;
2187}
2188\f
2189/* Look through the symbols which are defined in other shared
2190 libraries and referenced here. Update the list of version
2191 dependencies. This will be put into the .gnu.version_r section.
2192 This function is called via elf_link_hash_traverse. */
2193
28caa186 2194static bfd_boolean
268b6b39
AM
2195_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2196 void *data)
45d6a902 2197{
a50b1753 2198 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2199 Elf_Internal_Verneed *t;
2200 Elf_Internal_Vernaux *a;
986f0783 2201 size_t amt;
45d6a902 2202
45d6a902
AM
2203 /* We only care about symbols defined in shared objects with version
2204 information. */
f5385ebf
AM
2205 if (!h->def_dynamic
2206 || h->def_regular
45d6a902 2207 || h->dynindx == -1
7b20f099
AM
2208 || h->verinfo.verdef == NULL
2209 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2210 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2211 return TRUE;
2212
2213 /* See if we already know about this version. */
28caa186
AM
2214 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2215 t != NULL;
2216 t = t->vn_nextref)
45d6a902
AM
2217 {
2218 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2219 continue;
2220
2221 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2222 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2223 return TRUE;
2224
2225 break;
2226 }
2227
2228 /* This is a new version. Add it to tree we are building. */
2229
2230 if (t == NULL)
2231 {
2232 amt = sizeof *t;
a50b1753 2233 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2234 if (t == NULL)
2235 {
2236 rinfo->failed = TRUE;
2237 return FALSE;
2238 }
2239
2240 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2241 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2242 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2243 }
2244
2245 amt = sizeof *a;
a50b1753 2246 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2247 if (a == NULL)
2248 {
2249 rinfo->failed = TRUE;
2250 return FALSE;
2251 }
45d6a902
AM
2252
2253 /* Note that we are copying a string pointer here, and testing it
2254 above. If bfd_elf_string_from_elf_section is ever changed to
2255 discard the string data when low in memory, this will have to be
2256 fixed. */
2257 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2258
2259 a->vna_flags = h->verinfo.verdef->vd_flags;
2260 a->vna_nextptr = t->vn_auxptr;
2261
2262 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2263 ++rinfo->vers;
2264
2265 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2266
2267 t->vn_auxptr = a;
2268
2269 return TRUE;
2270}
2271
099bb8fb
L
2272/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2273 hidden. Set *T_P to NULL if there is no match. */
2274
2275static bfd_boolean
2276_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2277 struct elf_link_hash_entry *h,
2278 const char *version_p,
2279 struct bfd_elf_version_tree **t_p,
2280 bfd_boolean *hide)
2281{
2282 struct bfd_elf_version_tree *t;
2283
2284 /* Look for the version. If we find it, it is no longer weak. */
2285 for (t = info->version_info; t != NULL; t = t->next)
2286 {
2287 if (strcmp (t->name, version_p) == 0)
2288 {
2289 size_t len;
2290 char *alc;
2291 struct bfd_elf_version_expr *d;
2292
2293 len = version_p - h->root.root.string;
2294 alc = (char *) bfd_malloc (len);
2295 if (alc == NULL)
2296 return FALSE;
2297 memcpy (alc, h->root.root.string, len - 1);
2298 alc[len - 1] = '\0';
2299 if (alc[len - 2] == ELF_VER_CHR)
2300 alc[len - 2] = '\0';
2301
2302 h->verinfo.vertree = t;
2303 t->used = TRUE;
2304 d = NULL;
2305
2306 if (t->globals.list != NULL)
2307 d = (*t->match) (&t->globals, NULL, alc);
2308
2309 /* See if there is anything to force this symbol to
2310 local scope. */
2311 if (d == NULL && t->locals.list != NULL)
2312 {
2313 d = (*t->match) (&t->locals, NULL, alc);
2314 if (d != NULL
2315 && h->dynindx != -1
2316 && ! info->export_dynamic)
2317 *hide = TRUE;
2318 }
2319
2320 free (alc);
2321 break;
2322 }
2323 }
2324
2325 *t_p = t;
2326
2327 return TRUE;
2328}
2329
2330/* Return TRUE if the symbol H is hidden by version script. */
2331
2332bfd_boolean
2333_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2334 struct elf_link_hash_entry *h)
2335{
2336 const char *p;
2337 bfd_boolean hide = FALSE;
2338 const struct elf_backend_data *bed
2339 = get_elf_backend_data (info->output_bfd);
2340
2341 /* Version script only hides symbols defined in regular objects. */
2342 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2343 return TRUE;
2344
2345 p = strchr (h->root.root.string, ELF_VER_CHR);
2346 if (p != NULL && h->verinfo.vertree == NULL)
2347 {
2348 struct bfd_elf_version_tree *t;
2349
2350 ++p;
2351 if (*p == ELF_VER_CHR)
2352 ++p;
2353
2354 if (*p != '\0'
2355 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2356 && hide)
2357 {
2358 if (hide)
2359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2360 return TRUE;
2361 }
2362 }
2363
2364 /* If we don't have a version for this symbol, see if we can find
2365 something. */
2366 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2367 {
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2372 {
2373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2374 return TRUE;
2375 }
2376 }
2377
2378 return FALSE;
2379}
2380
45d6a902
AM
2381/* Figure out appropriate versions for all the symbols. We may not
2382 have the version number script until we have read all of the input
2383 files, so until that point we don't know which symbols should be
2384 local. This function is called via elf_link_hash_traverse. */
2385
28caa186 2386static bfd_boolean
268b6b39 2387_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2388{
28caa186 2389 struct elf_info_failed *sinfo;
45d6a902 2390 struct bfd_link_info *info;
9c5bfbb7 2391 const struct elf_backend_data *bed;
45d6a902
AM
2392 struct elf_info_failed eif;
2393 char *p;
099bb8fb 2394 bfd_boolean hide;
45d6a902 2395
a50b1753 2396 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2397 info = sinfo->info;
2398
45d6a902
AM
2399 /* Fix the symbol flags. */
2400 eif.failed = FALSE;
2401 eif.info = info;
2402 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2403 {
2404 if (eif.failed)
2405 sinfo->failed = TRUE;
2406 return FALSE;
2407 }
2408
0a640d71
L
2409 bed = get_elf_backend_data (info->output_bfd);
2410
45d6a902
AM
2411 /* We only need version numbers for symbols defined in regular
2412 objects. */
5fa370e4 2413 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2414 {
2415 /* Hide symbols defined in discarded input sections. */
2416 if ((h->root.type == bfd_link_hash_defined
2417 || h->root.type == bfd_link_hash_defweak)
2418 && discarded_section (h->root.u.def.section))
2419 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2420 return TRUE;
2421 }
45d6a902 2422
099bb8fb 2423 hide = FALSE;
45d6a902
AM
2424 p = strchr (h->root.root.string, ELF_VER_CHR);
2425 if (p != NULL && h->verinfo.vertree == NULL)
2426 {
2427 struct bfd_elf_version_tree *t;
45d6a902 2428
45d6a902
AM
2429 ++p;
2430 if (*p == ELF_VER_CHR)
6e33951e 2431 ++p;
45d6a902
AM
2432
2433 /* If there is no version string, we can just return out. */
2434 if (*p == '\0')
6e33951e 2435 return TRUE;
45d6a902 2436
099bb8fb 2437 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2438 {
099bb8fb
L
2439 sinfo->failed = TRUE;
2440 return FALSE;
45d6a902
AM
2441 }
2442
099bb8fb
L
2443 if (hide)
2444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2445
45d6a902
AM
2446 /* If we are building an application, we need to create a
2447 version node for this version. */
0e1862bb 2448 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2449 {
2450 struct bfd_elf_version_tree **pp;
2451 int version_index;
2452
2453 /* If we aren't going to export this symbol, we don't need
2454 to worry about it. */
2455 if (h->dynindx == -1)
2456 return TRUE;
2457
ef53be89
AM
2458 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2459 sizeof *t);
45d6a902
AM
2460 if (t == NULL)
2461 {
2462 sinfo->failed = TRUE;
2463 return FALSE;
2464 }
2465
45d6a902 2466 t->name = p;
45d6a902
AM
2467 t->name_indx = (unsigned int) -1;
2468 t->used = TRUE;
2469
2470 version_index = 1;
2471 /* Don't count anonymous version tag. */
fd91d419
L
2472 if (sinfo->info->version_info != NULL
2473 && sinfo->info->version_info->vernum == 0)
45d6a902 2474 version_index = 0;
fd91d419
L
2475 for (pp = &sinfo->info->version_info;
2476 *pp != NULL;
2477 pp = &(*pp)->next)
45d6a902
AM
2478 ++version_index;
2479 t->vernum = version_index;
2480
2481 *pp = t;
2482
2483 h->verinfo.vertree = t;
2484 }
2485 else if (t == NULL)
2486 {
2487 /* We could not find the version for a symbol when
2488 generating a shared archive. Return an error. */
4eca0228 2489 _bfd_error_handler
695344c0 2490 /* xgettext:c-format */
871b3ab2 2491 (_("%pB: version node not found for symbol %s"),
28caa186 2492 info->output_bfd, h->root.root.string);
45d6a902
AM
2493 bfd_set_error (bfd_error_bad_value);
2494 sinfo->failed = TRUE;
2495 return FALSE;
2496 }
45d6a902
AM
2497 }
2498
2499 /* If we don't have a version for this symbol, see if we can find
2500 something. */
099bb8fb
L
2501 if (!hide
2502 && h->verinfo.vertree == NULL
2503 && sinfo->info->version_info != NULL)
45d6a902 2504 {
fd91d419
L
2505 h->verinfo.vertree
2506 = bfd_find_version_for_sym (sinfo->info->version_info,
2507 h->root.root.string, &hide);
1e8fa21e
AM
2508 if (h->verinfo.vertree != NULL && hide)
2509 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2510 }
2511
2512 return TRUE;
2513}
2514\f
45d6a902
AM
2515/* Read and swap the relocs from the section indicated by SHDR. This
2516 may be either a REL or a RELA section. The relocations are
2517 translated into RELA relocations and stored in INTERNAL_RELOCS,
2518 which should have already been allocated to contain enough space.
2519 The EXTERNAL_RELOCS are a buffer where the external form of the
2520 relocations should be stored.
2521
2522 Returns FALSE if something goes wrong. */
2523
2524static bfd_boolean
268b6b39 2525elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2526 asection *sec,
268b6b39
AM
2527 Elf_Internal_Shdr *shdr,
2528 void *external_relocs,
2529 Elf_Internal_Rela *internal_relocs)
45d6a902 2530{
9c5bfbb7 2531 const struct elf_backend_data *bed;
268b6b39 2532 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2533 const bfd_byte *erela;
2534 const bfd_byte *erelaend;
2535 Elf_Internal_Rela *irela;
243ef1e0
L
2536 Elf_Internal_Shdr *symtab_hdr;
2537 size_t nsyms;
45d6a902 2538
45d6a902
AM
2539 /* Position ourselves at the start of the section. */
2540 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2541 return FALSE;
2542
2543 /* Read the relocations. */
2544 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2545 return FALSE;
2546
243ef1e0 2547 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2548 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2549
45d6a902
AM
2550 bed = get_elf_backend_data (abfd);
2551
2552 /* Convert the external relocations to the internal format. */
2553 if (shdr->sh_entsize == bed->s->sizeof_rel)
2554 swap_in = bed->s->swap_reloc_in;
2555 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2556 swap_in = bed->s->swap_reloca_in;
2557 else
2558 {
2559 bfd_set_error (bfd_error_wrong_format);
2560 return FALSE;
2561 }
2562
a50b1753 2563 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2564 /* Setting erelaend like this and comparing with <= handles case of
2565 a fuzzed object with sh_size not a multiple of sh_entsize. */
2566 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2567 irela = internal_relocs;
f55b1e32 2568 while (erela <= erelaend)
45d6a902 2569 {
243ef1e0
L
2570 bfd_vma r_symndx;
2571
45d6a902 2572 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2573 r_symndx = ELF32_R_SYM (irela->r_info);
2574 if (bed->s->arch_size == 64)
2575 r_symndx >>= 24;
ce98a316
NC
2576 if (nsyms > 0)
2577 {
2578 if ((size_t) r_symndx >= nsyms)
2579 {
4eca0228 2580 _bfd_error_handler
695344c0 2581 /* xgettext:c-format */
2dcf00ce
AM
2582 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2583 " for offset %#" PRIx64 " in section `%pA'"),
2584 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2585 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2586 bfd_set_error (bfd_error_bad_value);
2587 return FALSE;
2588 }
2589 }
cf35638d 2590 else if (r_symndx != STN_UNDEF)
243ef1e0 2591 {
4eca0228 2592 _bfd_error_handler
695344c0 2593 /* xgettext:c-format */
2dcf00ce
AM
2594 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2595 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2596 " when the object file has no symbol table"),
2dcf00ce
AM
2597 abfd, (uint64_t) r_symndx,
2598 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2599 bfd_set_error (bfd_error_bad_value);
2600 return FALSE;
2601 }
45d6a902
AM
2602 irela += bed->s->int_rels_per_ext_rel;
2603 erela += shdr->sh_entsize;
2604 }
2605
2606 return TRUE;
2607}
2608
2609/* Read and swap the relocs for a section O. They may have been
2610 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2611 not NULL, they are used as buffers to read into. They are known to
2612 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2613 the return value is allocated using either malloc or bfd_alloc,
2614 according to the KEEP_MEMORY argument. If O has two relocation
2615 sections (both REL and RELA relocations), then the REL_HDR
2616 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2617 RELA_HDR relocations. */
45d6a902
AM
2618
2619Elf_Internal_Rela *
268b6b39
AM
2620_bfd_elf_link_read_relocs (bfd *abfd,
2621 asection *o,
2622 void *external_relocs,
2623 Elf_Internal_Rela *internal_relocs,
2624 bfd_boolean keep_memory)
45d6a902 2625{
268b6b39 2626 void *alloc1 = NULL;
45d6a902 2627 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2629 struct bfd_elf_section_data *esdo = elf_section_data (o);
2630 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2631
d4730f92
BS
2632 if (esdo->relocs != NULL)
2633 return esdo->relocs;
45d6a902
AM
2634
2635 if (o->reloc_count == 0)
2636 return NULL;
2637
45d6a902
AM
2638 if (internal_relocs == NULL)
2639 {
2640 bfd_size_type size;
2641
056bafd4 2642 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2643 if (keep_memory)
a50b1753 2644 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2645 else
a50b1753 2646 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2647 if (internal_relocs == NULL)
2648 goto error_return;
2649 }
2650
2651 if (external_relocs == NULL)
2652 {
d4730f92
BS
2653 bfd_size_type size = 0;
2654
2655 if (esdo->rel.hdr)
2656 size += esdo->rel.hdr->sh_size;
2657 if (esdo->rela.hdr)
2658 size += esdo->rela.hdr->sh_size;
45d6a902 2659
268b6b39 2660 alloc1 = bfd_malloc (size);
45d6a902
AM
2661 if (alloc1 == NULL)
2662 goto error_return;
2663 external_relocs = alloc1;
2664 }
2665
d4730f92
BS
2666 internal_rela_relocs = internal_relocs;
2667 if (esdo->rel.hdr)
2668 {
2669 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2670 external_relocs,
2671 internal_relocs))
2672 goto error_return;
2673 external_relocs = (((bfd_byte *) external_relocs)
2674 + esdo->rel.hdr->sh_size);
2675 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2676 * bed->s->int_rels_per_ext_rel);
2677 }
2678
2679 if (esdo->rela.hdr
2680 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2681 external_relocs,
2682 internal_rela_relocs)))
45d6a902
AM
2683 goto error_return;
2684
2685 /* Cache the results for next time, if we can. */
2686 if (keep_memory)
d4730f92 2687 esdo->relocs = internal_relocs;
45d6a902 2688
c9594989 2689 free (alloc1);
45d6a902
AM
2690
2691 /* Don't free alloc2, since if it was allocated we are passing it
2692 back (under the name of internal_relocs). */
2693
2694 return internal_relocs;
2695
2696 error_return:
c9594989 2697 free (alloc1);
45d6a902 2698 if (alloc2 != NULL)
4dd07732
AM
2699 {
2700 if (keep_memory)
2701 bfd_release (abfd, alloc2);
2702 else
2703 free (alloc2);
2704 }
45d6a902
AM
2705 return NULL;
2706}
2707
2708/* Compute the size of, and allocate space for, REL_HDR which is the
2709 section header for a section containing relocations for O. */
2710
28caa186 2711static bfd_boolean
9eaff861
AO
2712_bfd_elf_link_size_reloc_section (bfd *abfd,
2713 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2714{
9eaff861 2715 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2716
2717 /* That allows us to calculate the size of the section. */
9eaff861 2718 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2719
2720 /* The contents field must last into write_object_contents, so we
2721 allocate it with bfd_alloc rather than malloc. Also since we
2722 cannot be sure that the contents will actually be filled in,
2723 we zero the allocated space. */
a50b1753 2724 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2725 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2726 return FALSE;
2727
d4730f92 2728 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2729 {
2730 struct elf_link_hash_entry **p;
2731
ca4be51c
AM
2732 p = ((struct elf_link_hash_entry **)
2733 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2734 if (p == NULL)
2735 return FALSE;
2736
d4730f92 2737 reldata->hashes = p;
45d6a902
AM
2738 }
2739
2740 return TRUE;
2741}
2742
2743/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2744 originated from the section given by INPUT_REL_HDR) to the
2745 OUTPUT_BFD. */
2746
2747bfd_boolean
268b6b39
AM
2748_bfd_elf_link_output_relocs (bfd *output_bfd,
2749 asection *input_section,
2750 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2751 Elf_Internal_Rela *internal_relocs,
2752 struct elf_link_hash_entry **rel_hash
2753 ATTRIBUTE_UNUSED)
45d6a902
AM
2754{
2755 Elf_Internal_Rela *irela;
2756 Elf_Internal_Rela *irelaend;
2757 bfd_byte *erel;
d4730f92 2758 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2759 asection *output_section;
9c5bfbb7 2760 const struct elf_backend_data *bed;
268b6b39 2761 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2762 struct bfd_elf_section_data *esdo;
45d6a902
AM
2763
2764 output_section = input_section->output_section;
45d6a902 2765
d4730f92
BS
2766 bed = get_elf_backend_data (output_bfd);
2767 esdo = elf_section_data (output_section);
2768 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2769 {
d4730f92
BS
2770 output_reldata = &esdo->rel;
2771 swap_out = bed->s->swap_reloc_out;
45d6a902 2772 }
d4730f92
BS
2773 else if (esdo->rela.hdr
2774 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2775 {
d4730f92
BS
2776 output_reldata = &esdo->rela;
2777 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2778 }
2779 else
2780 {
4eca0228 2781 _bfd_error_handler
695344c0 2782 /* xgettext:c-format */
871b3ab2 2783 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2784 output_bfd, input_section->owner, input_section);
297d8443 2785 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2786 return FALSE;
2787 }
2788
d4730f92
BS
2789 erel = output_reldata->hdr->contents;
2790 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2791 irela = internal_relocs;
2792 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2793 * bed->s->int_rels_per_ext_rel);
2794 while (irela < irelaend)
2795 {
2796 (*swap_out) (output_bfd, irela, erel);
2797 irela += bed->s->int_rels_per_ext_rel;
2798 erel += input_rel_hdr->sh_entsize;
2799 }
2800
2801 /* Bump the counter, so that we know where to add the next set of
2802 relocations. */
d4730f92 2803 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2804
2805 return TRUE;
2806}
2807\f
508c3946
L
2808/* Make weak undefined symbols in PIE dynamic. */
2809
2810bfd_boolean
2811_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2812 struct elf_link_hash_entry *h)
2813{
0e1862bb 2814 if (bfd_link_pie (info)
508c3946
L
2815 && h->dynindx == -1
2816 && h->root.type == bfd_link_hash_undefweak)
2817 return bfd_elf_link_record_dynamic_symbol (info, h);
2818
2819 return TRUE;
2820}
2821
45d6a902
AM
2822/* Fix up the flags for a symbol. This handles various cases which
2823 can only be fixed after all the input files are seen. This is
2824 currently called by both adjust_dynamic_symbol and
2825 assign_sym_version, which is unnecessary but perhaps more robust in
2826 the face of future changes. */
2827
28caa186 2828static bfd_boolean
268b6b39
AM
2829_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2830 struct elf_info_failed *eif)
45d6a902 2831{
33774f08 2832 const struct elf_backend_data *bed;
508c3946 2833
45d6a902
AM
2834 /* If this symbol was mentioned in a non-ELF file, try to set
2835 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2836 permit a non-ELF file to correctly refer to a symbol defined in
2837 an ELF dynamic object. */
f5385ebf 2838 if (h->non_elf)
45d6a902
AM
2839 {
2840 while (h->root.type == bfd_link_hash_indirect)
2841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2842
2843 if (h->root.type != bfd_link_hash_defined
2844 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2845 {
2846 h->ref_regular = 1;
2847 h->ref_regular_nonweak = 1;
2848 }
45d6a902
AM
2849 else
2850 {
2851 if (h->root.u.def.section->owner != NULL
2852 && (bfd_get_flavour (h->root.u.def.section->owner)
2853 == bfd_target_elf_flavour))
f5385ebf
AM
2854 {
2855 h->ref_regular = 1;
2856 h->ref_regular_nonweak = 1;
2857 }
45d6a902 2858 else
f5385ebf 2859 h->def_regular = 1;
45d6a902
AM
2860 }
2861
2862 if (h->dynindx == -1
f5385ebf
AM
2863 && (h->def_dynamic
2864 || h->ref_dynamic))
45d6a902 2865 {
c152c796 2866 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2867 {
2868 eif->failed = TRUE;
2869 return FALSE;
2870 }
2871 }
2872 }
2873 else
2874 {
f5385ebf 2875 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2876 was first seen in a non-ELF file. Fortunately, if the symbol
2877 was first seen in an ELF file, we're probably OK unless the
2878 symbol was defined in a non-ELF file. Catch that case here.
2879 FIXME: We're still in trouble if the symbol was first seen in
2880 a dynamic object, and then later in a non-ELF regular object. */
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2883 && !h->def_regular
45d6a902
AM
2884 && (h->root.u.def.section->owner != NULL
2885 ? (bfd_get_flavour (h->root.u.def.section->owner)
2886 != bfd_target_elf_flavour)
2887 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2888 && !h->def_dynamic)))
2889 h->def_regular = 1;
45d6a902
AM
2890 }
2891
508c3946 2892 /* Backend specific symbol fixup. */
33774f08
AM
2893 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2894 if (bed->elf_backend_fixup_symbol
2895 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2896 return FALSE;
508c3946 2897
45d6a902
AM
2898 /* If this is a final link, and the symbol was defined as a common
2899 symbol in a regular object file, and there was no definition in
2900 any dynamic object, then the linker will have allocated space for
f5385ebf 2901 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2902 flag will not have been set. */
2903 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2904 && !h->def_regular
2905 && h->ref_regular
2906 && !h->def_dynamic
96f29d96 2907 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2908 h->def_regular = 1;
45d6a902 2909
af0bfb9c
AM
2910 /* Symbols defined in discarded sections shouldn't be dynamic. */
2911 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2913
4deb8f71
L
2914 /* If a weak undefined symbol has non-default visibility, we also
2915 hide it from the dynamic linker. */
af0bfb9c
AM
2916 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2917 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2918 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2919
2920 /* A hidden versioned symbol in executable should be forced local if
2921 it is is locally defined, not referenced by shared library and not
2922 exported. */
2923 else if (bfd_link_executable (eif->info)
2924 && h->versioned == versioned_hidden
2925 && !eif->info->export_dynamic
2926 && !h->dynamic
2927 && !h->ref_dynamic
2928 && h->def_regular)
2929 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2930
45d6a902
AM
2931 /* If -Bsymbolic was used (which means to bind references to global
2932 symbols to the definition within the shared object), and this
2933 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2934 need a PLT entry. Likewise, if the symbol has non-default
2935 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2936 will force it local. */
4deb8f71
L
2937 else if (h->needs_plt
2938 && bfd_link_pic (eif->info)
2939 && is_elf_hash_table (eif->info->hash)
2940 && (SYMBOLIC_BIND (eif->info, h)
2941 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2942 && h->def_regular)
45d6a902 2943 {
45d6a902
AM
2944 bfd_boolean force_local;
2945
45d6a902
AM
2946 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2947 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2948 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2949 }
2950
45d6a902
AM
2951 /* If this is a weak defined symbol in a dynamic object, and we know
2952 the real definition in the dynamic object, copy interesting flags
2953 over to the real definition. */
60d67dc8 2954 if (h->is_weakalias)
45d6a902 2955 {
60d67dc8
AM
2956 struct elf_link_hash_entry *def = weakdef (h);
2957
45d6a902
AM
2958 /* If the real definition is defined by a regular object file,
2959 don't do anything special. See the longer description in
5b9d7a9a
AM
2960 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2961 bfd_link_hash_defined as it was when put on the alias list
2962 then it must have originally been a versioned symbol (for
2963 which a non-versioned indirect symbol is created) and later
2964 a definition for the non-versioned symbol is found. In that
2965 case the indirection is flipped with the versioned symbol
2966 becoming an indirect pointing at the non-versioned symbol.
2967 Thus, not an alias any more. */
2968 if (def->def_regular
2969 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2970 {
2971 h = def;
2972 while ((h = h->u.alias) != def)
2973 h->is_weakalias = 0;
2974 }
45d6a902 2975 else
a26587ba 2976 {
4e6b54a6
AM
2977 while (h->root.type == bfd_link_hash_indirect)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2979 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2980 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2981 BFD_ASSERT (def->def_dynamic);
60d67dc8 2982 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2983 }
45d6a902
AM
2984 }
2985
2986 return TRUE;
2987}
2988
2989/* Make the backend pick a good value for a dynamic symbol. This is
2990 called via elf_link_hash_traverse, and also calls itself
2991 recursively. */
2992
28caa186 2993static bfd_boolean
268b6b39 2994_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2995{
a50b1753 2996 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2997 struct elf_link_hash_table *htab;
9c5bfbb7 2998 const struct elf_backend_data *bed;
45d6a902 2999
0eddce27 3000 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
3001 return FALSE;
3002
45d6a902
AM
3003 /* Ignore indirect symbols. These are added by the versioning code. */
3004 if (h->root.type == bfd_link_hash_indirect)
3005 return TRUE;
3006
3007 /* Fix the symbol flags. */
3008 if (! _bfd_elf_fix_symbol_flags (h, eif))
3009 return FALSE;
3010
559192d8
AM
3011 htab = elf_hash_table (eif->info);
3012 bed = get_elf_backend_data (htab->dynobj);
3013
954b63d4
AM
3014 if (h->root.type == bfd_link_hash_undefweak)
3015 {
3016 if (eif->info->dynamic_undefined_weak == 0)
559192d8 3017 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
3018 else if (eif->info->dynamic_undefined_weak > 0
3019 && h->ref_regular
3020 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3021 && !bfd_hide_sym_by_version (eif->info->version_info,
3022 h->root.root.string))
3023 {
3024 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3025 {
3026 eif->failed = TRUE;
3027 return FALSE;
3028 }
3029 }
3030 }
3031
45d6a902
AM
3032 /* If this symbol does not require a PLT entry, and it is not
3033 defined by a dynamic object, or is not referenced by a regular
3034 object, ignore it. We do have to handle a weak defined symbol,
3035 even if no regular object refers to it, if we decided to add it
3036 to the dynamic symbol table. FIXME: Do we normally need to worry
3037 about symbols which are defined by one dynamic object and
3038 referenced by another one? */
f5385ebf 3039 if (!h->needs_plt
91e21fb7 3040 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3041 && (h->def_regular
3042 || !h->def_dynamic
3043 || (!h->ref_regular
60d67dc8 3044 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3045 {
a6aa5195 3046 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3047 return TRUE;
3048 }
3049
3050 /* If we've already adjusted this symbol, don't do it again. This
3051 can happen via a recursive call. */
f5385ebf 3052 if (h->dynamic_adjusted)
45d6a902
AM
3053 return TRUE;
3054
3055 /* Don't look at this symbol again. Note that we must set this
3056 after checking the above conditions, because we may look at a
3057 symbol once, decide not to do anything, and then get called
3058 recursively later after REF_REGULAR is set below. */
f5385ebf 3059 h->dynamic_adjusted = 1;
45d6a902
AM
3060
3061 /* If this is a weak definition, and we know a real definition, and
3062 the real symbol is not itself defined by a regular object file,
3063 then get a good value for the real definition. We handle the
3064 real symbol first, for the convenience of the backend routine.
3065
3066 Note that there is a confusing case here. If the real definition
3067 is defined by a regular object file, we don't get the real symbol
3068 from the dynamic object, but we do get the weak symbol. If the
3069 processor backend uses a COPY reloc, then if some routine in the
3070 dynamic object changes the real symbol, we will not see that
3071 change in the corresponding weak symbol. This is the way other
3072 ELF linkers work as well, and seems to be a result of the shared
3073 library model.
3074
3075 I will clarify this issue. Most SVR4 shared libraries define the
3076 variable _timezone and define timezone as a weak synonym. The
3077 tzset call changes _timezone. If you write
3078 extern int timezone;
3079 int _timezone = 5;
3080 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3081 you might expect that, since timezone is a synonym for _timezone,
3082 the same number will print both times. However, if the processor
3083 backend uses a COPY reloc, then actually timezone will be copied
3084 into your process image, and, since you define _timezone
3085 yourself, _timezone will not. Thus timezone and _timezone will
3086 wind up at different memory locations. The tzset call will set
3087 _timezone, leaving timezone unchanged. */
3088
60d67dc8 3089 if (h->is_weakalias)
45d6a902 3090 {
60d67dc8
AM
3091 struct elf_link_hash_entry *def = weakdef (h);
3092
ec24dc88 3093 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3094 the alias by a regular object file via the weak symbol H. */
3095 def->ref_regular = 1;
45d6a902 3096
ec24dc88 3097 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3098 the strong alias before H by recursively calling ourselves. */
3099 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3100 return FALSE;
3101 }
3102
3103 /* If a symbol has no type and no size and does not require a PLT
3104 entry, then we are probably about to do the wrong thing here: we
3105 are probably going to create a COPY reloc for an empty object.
3106 This case can arise when a shared object is built with assembly
3107 code, and the assembly code fails to set the symbol type. */
3108 if (h->size == 0
3109 && h->type == STT_NOTYPE
f5385ebf 3110 && !h->needs_plt)
4eca0228 3111 _bfd_error_handler
45d6a902
AM
3112 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3113 h->root.root.string);
3114
45d6a902
AM
3115 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3116 {
3117 eif->failed = TRUE;
3118 return FALSE;
3119 }
3120
3121 return TRUE;
3122}
3123
027297b7
L
3124/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3125 DYNBSS. */
3126
3127bfd_boolean
6cabe1ea
AM
3128_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3129 struct elf_link_hash_entry *h,
027297b7
L
3130 asection *dynbss)
3131{
91ac5911 3132 unsigned int power_of_two;
027297b7
L
3133 bfd_vma mask;
3134 asection *sec = h->root.u.def.section;
3135
de194d85 3136 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3137 requirement of symbols defined in the section. Since we don't
3138 know the symbol alignment requirement, we start with the
3139 maximum alignment and check low bits of the symbol address
3140 for the minimum alignment. */
fd361982 3141 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3142 mask = ((bfd_vma) 1 << power_of_two) - 1;
3143 while ((h->root.u.def.value & mask) != 0)
3144 {
3145 mask >>= 1;
3146 --power_of_two;
3147 }
027297b7 3148
fd361982 3149 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3150 {
3151 /* Adjust the section alignment if needed. */
fd361982 3152 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3153 return FALSE;
3154 }
3155
91ac5911 3156 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3157 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3158
3159 /* Define the symbol as being at this point in DYNBSS. */
3160 h->root.u.def.section = dynbss;
3161 h->root.u.def.value = dynbss->size;
3162
3163 /* Increment the size of DYNBSS to make room for the symbol. */
3164 dynbss->size += h->size;
3165
f7483970
L
3166 /* No error if extern_protected_data is true. */
3167 if (h->protected_def
889c2a67
L
3168 && (!info->extern_protected_data
3169 || (info->extern_protected_data < 0
3170 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3171 info->callbacks->einfo
c1c8c1ef 3172 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3173 h->root.root.string);
6cabe1ea 3174
027297b7
L
3175 return TRUE;
3176}
3177
45d6a902
AM
3178/* Adjust all external symbols pointing into SEC_MERGE sections
3179 to reflect the object merging within the sections. */
3180
28caa186 3181static bfd_boolean
268b6b39 3182_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3183{
3184 asection *sec;
3185
45d6a902
AM
3186 if ((h->root.type == bfd_link_hash_defined
3187 || h->root.type == bfd_link_hash_defweak)
3188 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3189 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3190 {
a50b1753 3191 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3192
3193 h->root.u.def.value =
3194 _bfd_merged_section_offset (output_bfd,
3195 &h->root.u.def.section,
3196 elf_section_data (sec)->sec_info,
753731ee 3197 h->root.u.def.value);
45d6a902
AM
3198 }
3199
3200 return TRUE;
3201}
986a241f
RH
3202
3203/* Returns false if the symbol referred to by H should be considered
3204 to resolve local to the current module, and true if it should be
3205 considered to bind dynamically. */
3206
3207bfd_boolean
268b6b39
AM
3208_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3209 struct bfd_link_info *info,
89a2ee5a 3210 bfd_boolean not_local_protected)
986a241f
RH
3211{
3212 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3213 const struct elf_backend_data *bed;
3214 struct elf_link_hash_table *hash_table;
986a241f
RH
3215
3216 if (h == NULL)
3217 return FALSE;
3218
3219 while (h->root.type == bfd_link_hash_indirect
3220 || h->root.type == bfd_link_hash_warning)
3221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3222
3223 /* If it was forced local, then clearly it's not dynamic. */
3224 if (h->dynindx == -1)
3225 return FALSE;
f5385ebf 3226 if (h->forced_local)
986a241f
RH
3227 return FALSE;
3228
3229 /* Identify the cases where name binding rules say that a
3230 visible symbol resolves locally. */
0e1862bb
L
3231 binding_stays_local_p = (bfd_link_executable (info)
3232 || SYMBOLIC_BIND (info, h));
986a241f
RH
3233
3234 switch (ELF_ST_VISIBILITY (h->other))
3235 {
3236 case STV_INTERNAL:
3237 case STV_HIDDEN:
3238 return FALSE;
3239
3240 case STV_PROTECTED:
fcb93ecf
PB
3241 hash_table = elf_hash_table (info);
3242 if (!is_elf_hash_table (hash_table))
3243 return FALSE;
3244
3245 bed = get_elf_backend_data (hash_table->dynobj);
3246
986a241f
RH
3247 /* Proper resolution for function pointer equality may require
3248 that these symbols perhaps be resolved dynamically, even though
3249 we should be resolving them to the current module. */
89a2ee5a 3250 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3251 binding_stays_local_p = TRUE;
3252 break;
3253
3254 default:
986a241f
RH
3255 break;
3256 }
3257
aa37626c 3258 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3259 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3260 return TRUE;
3261
986a241f
RH
3262 /* Otherwise, the symbol is dynamic if binding rules don't tell
3263 us that it remains local. */
3264 return !binding_stays_local_p;
3265}
f6c52c13
AM
3266
3267/* Return true if the symbol referred to by H should be considered
3268 to resolve local to the current module, and false otherwise. Differs
3269 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3270 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3271 for the place where dynindx == -1 is tested. If that test is true,
3272 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3273 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3274 defined symbols.
89a2ee5a
AM
3275 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3276 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3277 treatment of undefined weak symbols. For those that do not make
3278 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3279
3280bfd_boolean
268b6b39
AM
3281_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3282 struct bfd_link_info *info,
3283 bfd_boolean local_protected)
f6c52c13 3284{
fcb93ecf
PB
3285 const struct elf_backend_data *bed;
3286 struct elf_link_hash_table *hash_table;
3287
f6c52c13
AM
3288 /* If it's a local sym, of course we resolve locally. */
3289 if (h == NULL)
3290 return TRUE;
3291
d95edcac
L
3292 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3293 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3294 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3295 return TRUE;
3296
0fad2956
MR
3297 /* Forced local symbols resolve locally. */
3298 if (h->forced_local)
3299 return TRUE;
3300
7e2294f9
AO
3301 /* Common symbols that become definitions don't get the DEF_REGULAR
3302 flag set, so test it first, and don't bail out. */
3303 if (ELF_COMMON_DEF_P (h))
3304 /* Do nothing. */;
f6c52c13 3305 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3306 resolve locally. The sym is either undefined or dynamic. */
3307 else if (!h->def_regular)
f6c52c13
AM
3308 return FALSE;
3309
0fad2956 3310 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3311 if (h->dynindx == -1)
3312 return TRUE;
3313
3314 /* At this point, we know the symbol is defined and dynamic. In an
3315 executable it must resolve locally, likewise when building symbolic
3316 shared libraries. */
0e1862bb 3317 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3318 return TRUE;
3319
3320 /* Now deal with defined dynamic symbols in shared libraries. Ones
3321 with default visibility might not resolve locally. */
3322 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3323 return FALSE;
3324
fcb93ecf
PB
3325 hash_table = elf_hash_table (info);
3326 if (!is_elf_hash_table (hash_table))
3327 return TRUE;
3328
3329 bed = get_elf_backend_data (hash_table->dynobj);
3330
f7483970
L
3331 /* If extern_protected_data is false, STV_PROTECTED non-function
3332 symbols are local. */
889c2a67
L
3333 if ((!info->extern_protected_data
3334 || (info->extern_protected_data < 0
3335 && !bed->extern_protected_data))
3336 && !bed->is_function_type (h->type))
1c16dfa5
L
3337 return TRUE;
3338
f6c52c13 3339 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3340 symbols be treated as dynamic symbols. If the address of a
3341 function not defined in an executable is set to that function's
3342 plt entry in the executable, then the address of the function in
3343 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3344 return local_protected;
3345}
e1918d23
AM
3346
3347/* Caches some TLS segment info, and ensures that the TLS segment vma is
3348 aligned. Returns the first TLS output section. */
3349
3350struct bfd_section *
3351_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3352{
3353 struct bfd_section *sec, *tls;
3354 unsigned int align = 0;
3355
3356 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3357 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3358 break;
3359 tls = sec;
3360
3361 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3362 if (sec->alignment_power > align)
3363 align = sec->alignment_power;
3364
3365 elf_hash_table (info)->tls_sec = tls;
3366
fdde2fb6
SH
3367 /* Ensure the alignment of the first section (usually .tdata) is the largest
3368 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3369 if (tls != NULL)
3370 tls->alignment_power = align;
3371
3372 return tls;
3373}
0ad989f9
L
3374
3375/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3376static bfd_boolean
3377is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3378 Elf_Internal_Sym *sym)
3379{
a4d8e49b
L
3380 const struct elf_backend_data *bed;
3381
0ad989f9
L
3382 /* Local symbols do not count, but target specific ones might. */
3383 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3384 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3385 return FALSE;
3386
fcb93ecf 3387 bed = get_elf_backend_data (abfd);
0ad989f9 3388 /* Function symbols do not count. */
fcb93ecf 3389 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3390 return FALSE;
3391
3392 /* If the section is undefined, then so is the symbol. */
3393 if (sym->st_shndx == SHN_UNDEF)
3394 return FALSE;
3395
3396 /* If the symbol is defined in the common section, then
3397 it is a common definition and so does not count. */
a4d8e49b 3398 if (bed->common_definition (sym))
0ad989f9
L
3399 return FALSE;
3400
3401 /* If the symbol is in a target specific section then we
3402 must rely upon the backend to tell us what it is. */
3403 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3404 /* FIXME - this function is not coded yet:
3405
3406 return _bfd_is_global_symbol_definition (abfd, sym);
3407
3408 Instead for now assume that the definition is not global,
3409 Even if this is wrong, at least the linker will behave
3410 in the same way that it used to do. */
3411 return FALSE;
3412
3413 return TRUE;
3414}
3415
3416/* Search the symbol table of the archive element of the archive ABFD
3417 whose archive map contains a mention of SYMDEF, and determine if
3418 the symbol is defined in this element. */
3419static bfd_boolean
3420elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3421{
3422 Elf_Internal_Shdr * hdr;
ef53be89
AM
3423 size_t symcount;
3424 size_t extsymcount;
3425 size_t extsymoff;
0ad989f9
L
3426 Elf_Internal_Sym *isymbuf;
3427 Elf_Internal_Sym *isym;
3428 Elf_Internal_Sym *isymend;
3429 bfd_boolean result;
3430
3431 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3432 if (abfd == NULL)
3433 return FALSE;
3434
3435 if (! bfd_check_format (abfd, bfd_object))
3436 return FALSE;
3437
7dc3990e
L
3438 /* Select the appropriate symbol table. If we don't know if the
3439 object file is an IR object, give linker LTO plugin a chance to
3440 get the correct symbol table. */
3441 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3442#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3443 || (abfd->plugin_format == bfd_plugin_unknown
3444 && bfd_link_plugin_object_p (abfd))
3445#endif
3446 )
3447 {
3448 /* Use the IR symbol table if the object has been claimed by
3449 plugin. */
3450 abfd = abfd->plugin_dummy_bfd;
3451 hdr = &elf_tdata (abfd)->symtab_hdr;
3452 }
3453 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3454 hdr = &elf_tdata (abfd)->symtab_hdr;
3455 else
3456 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3457
3458 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3459
3460 /* The sh_info field of the symtab header tells us where the
3461 external symbols start. We don't care about the local symbols. */
3462 if (elf_bad_symtab (abfd))
3463 {
3464 extsymcount = symcount;
3465 extsymoff = 0;
3466 }
3467 else
3468 {
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3471 }
3472
3473 if (extsymcount == 0)
3474 return FALSE;
3475
3476 /* Read in the symbol table. */
3477 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3478 NULL, NULL, NULL);
3479 if (isymbuf == NULL)
3480 return FALSE;
3481
3482 /* Scan the symbol table looking for SYMDEF. */
3483 result = FALSE;
3484 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3485 {
3486 const char *name;
3487
3488 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3489 isym->st_name);
3490 if (name == NULL)
3491 break;
3492
3493 if (strcmp (name, symdef->name) == 0)
3494 {
3495 result = is_global_data_symbol_definition (abfd, isym);
3496 break;
3497 }
3498 }
3499
3500 free (isymbuf);
3501
3502 return result;
3503}
3504\f
5a580b3a
AM
3505/* Add an entry to the .dynamic table. */
3506
3507bfd_boolean
3508_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3509 bfd_vma tag,
3510 bfd_vma val)
3511{
3512 struct elf_link_hash_table *hash_table;
3513 const struct elf_backend_data *bed;
3514 asection *s;
3515 bfd_size_type newsize;
3516 bfd_byte *newcontents;
3517 Elf_Internal_Dyn dyn;
3518
3519 hash_table = elf_hash_table (info);
3520 if (! is_elf_hash_table (hash_table))
3521 return FALSE;
3522
7f923b7f
AM
3523 if (tag == DT_RELA || tag == DT_REL)
3524 hash_table->dynamic_relocs = TRUE;
3525
5a580b3a 3526 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3527 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3528 BFD_ASSERT (s != NULL);
3529
eea6121a 3530 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3531 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3532 if (newcontents == NULL)
3533 return FALSE;
3534
3535 dyn.d_tag = tag;
3536 dyn.d_un.d_val = val;
eea6121a 3537 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3538
eea6121a 3539 s->size = newsize;
5a580b3a
AM
3540 s->contents = newcontents;
3541
3542 return TRUE;
3543}
3544
6f6fd151
L
3545/* Strip zero-sized dynamic sections. */
3546
3547bfd_boolean
3548_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3549{
3550 struct elf_link_hash_table *hash_table;
3551 const struct elf_backend_data *bed;
3552 asection *s, *sdynamic, **pp;
3553 asection *rela_dyn, *rel_dyn;
3554 Elf_Internal_Dyn dyn;
3555 bfd_byte *extdyn, *next;
3556 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3557 bfd_boolean strip_zero_sized;
3558 bfd_boolean strip_zero_sized_plt;
3559
3560 if (bfd_link_relocatable (info))
3561 return TRUE;
3562
3563 hash_table = elf_hash_table (info);
3564 if (!is_elf_hash_table (hash_table))
3565 return FALSE;
3566
3567 if (!hash_table->dynobj)
3568 return TRUE;
3569
3570 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3571 if (!sdynamic)
3572 return TRUE;
3573
3574 bed = get_elf_backend_data (hash_table->dynobj);
3575 swap_dyn_in = bed->s->swap_dyn_in;
3576
3577 strip_zero_sized = FALSE;
3578 strip_zero_sized_plt = FALSE;
3579
3580 /* Strip zero-sized dynamic sections. */
3581 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3582 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3583 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3584 if (s->size == 0
3585 && (s == rela_dyn
3586 || s == rel_dyn
3587 || s == hash_table->srelplt->output_section
3588 || s == hash_table->splt->output_section))
3589 {
3590 *pp = s->next;
3591 info->output_bfd->section_count--;
3592 strip_zero_sized = TRUE;
3593 if (s == rela_dyn)
3594 s = rela_dyn;
3595 if (s == rel_dyn)
3596 s = rel_dyn;
3597 else if (s == hash_table->splt->output_section)
3598 {
3599 s = hash_table->splt;
3600 strip_zero_sized_plt = TRUE;
3601 }
3602 else
3603 s = hash_table->srelplt;
3604 s->flags |= SEC_EXCLUDE;
3605 s->output_section = bfd_abs_section_ptr;
3606 }
3607 else
3608 pp = &s->next;
3609
3610 if (strip_zero_sized_plt)
3611 for (extdyn = sdynamic->contents;
3612 extdyn < sdynamic->contents + sdynamic->size;
3613 extdyn = next)
3614 {
3615 next = extdyn + bed->s->sizeof_dyn;
3616 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3617 switch (dyn.d_tag)
3618 {
3619 default:
3620 break;
3621 case DT_JMPREL:
3622 case DT_PLTRELSZ:
3623 case DT_PLTREL:
3624 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3625 the procedure linkage table (the .plt section) has been
3626 removed. */
3627 memmove (extdyn, next,
3628 sdynamic->size - (next - sdynamic->contents));
3629 next = extdyn;
3630 }
3631 }
3632
3633 if (strip_zero_sized)
3634 {
3635 /* Regenerate program headers. */
3636 elf_seg_map (info->output_bfd) = NULL;
3637 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3638 }
3639
3640 return TRUE;
3641}
3642
e310298c 3643/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3644 1 if a DT_NEEDED tag already exists, and 0 on success. */
3645
e310298c
AM
3646int
3647bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3648{
3649 struct elf_link_hash_table *hash_table;
ef53be89 3650 size_t strindex;
e310298c 3651 const char *soname;
5a580b3a 3652
7e9f0867
AM
3653 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3654 return -1;
3655
5a580b3a 3656 hash_table = elf_hash_table (info);
e310298c 3657 soname = elf_dt_name (abfd);
5a580b3a 3658 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3659 if (strindex == (size_t) -1)
5a580b3a
AM
3660 return -1;
3661
02be4619 3662 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3663 {
3664 asection *sdyn;
3665 const struct elf_backend_data *bed;
3666 bfd_byte *extdyn;
3667
3668 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3669 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3670 if (sdyn != NULL)
3671 for (extdyn = sdyn->contents;
3672 extdyn < sdyn->contents + sdyn->size;
3673 extdyn += bed->s->sizeof_dyn)
3674 {
3675 Elf_Internal_Dyn dyn;
5a580b3a 3676
7e9f0867
AM
3677 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3678 if (dyn.d_tag == DT_NEEDED
3679 && dyn.d_un.d_val == strindex)
3680 {
3681 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3682 return 1;
3683 }
3684 }
5a580b3a
AM
3685 }
3686
e310298c
AM
3687 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3688 return -1;
7e9f0867 3689
e310298c
AM
3690 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3691 return -1;
5a580b3a
AM
3692
3693 return 0;
3694}
3695
7b15fa7a
AM
3696/* Return true if SONAME is on the needed list between NEEDED and STOP
3697 (or the end of list if STOP is NULL), and needed by a library that
3698 will be loaded. */
3699
010e5ae2 3700static bfd_boolean
7b15fa7a
AM
3701on_needed_list (const char *soname,
3702 struct bfd_link_needed_list *needed,
3703 struct bfd_link_needed_list *stop)
010e5ae2 3704{
7b15fa7a
AM
3705 struct bfd_link_needed_list *look;
3706 for (look = needed; look != stop; look = look->next)
3707 if (strcmp (soname, look->name) == 0
3708 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3709 /* If needed by a library that itself is not directly
3710 needed, recursively check whether that library is
3711 indirectly needed. Since we add DT_NEEDED entries to
3712 the end of the list, library dependencies appear after
3713 the library. Therefore search prior to the current
3714 LOOK, preventing possible infinite recursion. */
3715 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3716 return TRUE;
3717
3718 return FALSE;
3719}
3720
3a3f4bf7 3721/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3722static int
3723elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3724{
3725 const struct elf_link_hash_entry *h1;
3726 const struct elf_link_hash_entry *h2;
10b7e05b 3727 bfd_signed_vma vdiff;
3a3f4bf7
AM
3728 int sdiff;
3729 const char *n1;
3730 const char *n2;
5a580b3a
AM
3731
3732 h1 = *(const struct elf_link_hash_entry **) arg1;
3733 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3734 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3735 if (vdiff != 0)
3736 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3737
3738 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3739 if (sdiff != 0)
3740 return sdiff;
3741
3742 /* Sort so that sized symbols are selected over zero size symbols. */
3743 vdiff = h1->size - h2->size;
3744 if (vdiff != 0)
3745 return vdiff > 0 ? 1 : -1;
3746
3747 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3748 if (h1->type != h2->type)
3749 return h1->type - h2->type;
3750
3751 /* If symbols are properly sized and typed, and multiple strong
3752 aliases are not defined in a shared library by the user we
3753 shouldn't get here. Unfortunately linker script symbols like
3754 __bss_start sometimes match a user symbol defined at the start of
3755 .bss without proper size and type. We'd like to preference the
3756 user symbol over reserved system symbols. Sort on leading
3757 underscores. */
3758 n1 = h1->root.root.string;
3759 n2 = h2->root.root.string;
3760 while (*n1 == *n2)
10b7e05b 3761 {
3a3f4bf7
AM
3762 if (*n1 == 0)
3763 break;
3764 ++n1;
3765 ++n2;
10b7e05b 3766 }
3a3f4bf7
AM
3767 if (*n1 == '_')
3768 return -1;
3769 if (*n2 == '_')
3770 return 1;
3771
3772 /* Final sort on name selects user symbols like '_u' over reserved
3773 system symbols like '_Z' and also will avoid qsort instability. */
3774 return *n1 - *n2;
5a580b3a 3775}
4ad4eba5 3776
5a580b3a
AM
3777/* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3779
3780static bfd_boolean
3781elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3782{
a50b1753 3783 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3784
5a580b3a
AM
3785 if (h->dynindx != -1)
3786 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3787 return TRUE;
3788}
3789
3790/* Assign string offsets in .dynstr, update all structures referencing
3791 them. */
3792
4ad4eba5
AM
3793static bfd_boolean
3794elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3795{
3796 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3797 struct elf_link_local_dynamic_entry *entry;
3798 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3799 bfd *dynobj = hash_table->dynobj;
3800 asection *sdyn;
3801 bfd_size_type size;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3804
3805 _bfd_elf_strtab_finalize (dynstr);
3806 size = _bfd_elf_strtab_size (dynstr);
3807
3d16b64e
NA
3808 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3809
3810 if (info->callbacks->examine_strtab)
3811 info->callbacks->examine_strtab (dynstr);
3812
5a580b3a 3813 bed = get_elf_backend_data (dynobj);
3d4d4302 3814 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3815 BFD_ASSERT (sdyn != NULL);
3816
3817 /* Update all .dynamic entries referencing .dynstr strings. */
3818 for (extdyn = sdyn->contents;
eea6121a 3819 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3820 extdyn += bed->s->sizeof_dyn)
3821 {
3822 Elf_Internal_Dyn dyn;
3823
3824 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3825 switch (dyn.d_tag)
3826 {
3827 case DT_STRSZ:
3828 dyn.d_un.d_val = size;
3829 break;
3830 case DT_NEEDED:
3831 case DT_SONAME:
3832 case DT_RPATH:
3833 case DT_RUNPATH:
3834 case DT_FILTER:
3835 case DT_AUXILIARY:
7ee314fa
AM
3836 case DT_AUDIT:
3837 case DT_DEPAUDIT:
5a580b3a
AM
3838 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3839 break;
3840 default:
3841 continue;
3842 }
3843 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3844 }
3845
3846 /* Now update local dynamic symbols. */
3847 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3848 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3849 entry->isym.st_name);
3850
3851 /* And the rest of dynamic symbols. */
3852 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3853
3854 /* Adjust version definitions. */
3855 if (elf_tdata (output_bfd)->cverdefs)
3856 {
3857 asection *s;
3858 bfd_byte *p;
ef53be89 3859 size_t i;
5a580b3a
AM
3860 Elf_Internal_Verdef def;
3861 Elf_Internal_Verdaux defaux;
3862
3d4d4302 3863 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3864 p = s->contents;
3865 do
3866 {
3867 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3868 &def);
3869 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3870 if (def.vd_aux != sizeof (Elf_External_Verdef))
3871 continue;
5a580b3a
AM
3872 for (i = 0; i < def.vd_cnt; ++i)
3873 {
3874 _bfd_elf_swap_verdaux_in (output_bfd,
3875 (Elf_External_Verdaux *) p, &defaux);
3876 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3877 defaux.vda_name);
3878 _bfd_elf_swap_verdaux_out (output_bfd,
3879 &defaux, (Elf_External_Verdaux *) p);
3880 p += sizeof (Elf_External_Verdaux);
3881 }
3882 }
3883 while (def.vd_next);
3884 }
3885
3886 /* Adjust version references. */
3887 if (elf_tdata (output_bfd)->verref)
3888 {
3889 asection *s;
3890 bfd_byte *p;
ef53be89 3891 size_t i;
5a580b3a
AM
3892 Elf_Internal_Verneed need;
3893 Elf_Internal_Vernaux needaux;
3894
3d4d4302 3895 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3896 p = s->contents;
3897 do
3898 {
3899 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3900 &need);
3901 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3902 _bfd_elf_swap_verneed_out (output_bfd, &need,
3903 (Elf_External_Verneed *) p);
3904 p += sizeof (Elf_External_Verneed);
3905 for (i = 0; i < need.vn_cnt; ++i)
3906 {
3907 _bfd_elf_swap_vernaux_in (output_bfd,
3908 (Elf_External_Vernaux *) p, &needaux);
3909 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3910 needaux.vna_name);
3911 _bfd_elf_swap_vernaux_out (output_bfd,
3912 &needaux,
3913 (Elf_External_Vernaux *) p);
3914 p += sizeof (Elf_External_Vernaux);
3915 }
3916 }
3917 while (need.vn_next);
3918 }
3919
3920 return TRUE;
3921}
3922\f
13285a1b
AM
3923/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3924 The default is to only match when the INPUT and OUTPUT are exactly
3925 the same target. */
3926
3927bfd_boolean
3928_bfd_elf_default_relocs_compatible (const bfd_target *input,
3929 const bfd_target *output)
3930{
3931 return input == output;
3932}
3933
3934/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3935 This version is used when different targets for the same architecture
3936 are virtually identical. */
3937
3938bfd_boolean
3939_bfd_elf_relocs_compatible (const bfd_target *input,
3940 const bfd_target *output)
3941{
3942 const struct elf_backend_data *obed, *ibed;
3943
3944 if (input == output)
3945 return TRUE;
3946
3947 ibed = xvec_get_elf_backend_data (input);
3948 obed = xvec_get_elf_backend_data (output);
3949
3950 if (ibed->arch != obed->arch)
3951 return FALSE;
3952
3953 /* If both backends are using this function, deem them compatible. */
3954 return ibed->relocs_compatible == obed->relocs_compatible;
3955}
3956
e5034e59
AM
3957/* Make a special call to the linker "notice" function to tell it that
3958 we are about to handle an as-needed lib, or have finished
1b786873 3959 processing the lib. */
e5034e59
AM
3960
3961bfd_boolean
3962_bfd_elf_notice_as_needed (bfd *ibfd,
3963 struct bfd_link_info *info,
3964 enum notice_asneeded_action act)
3965{
46135103 3966 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3967}
3968
d9689752
L
3969/* Check relocations an ELF object file. */
3970
3971bfd_boolean
3972_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3973{
3974 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3975 struct elf_link_hash_table *htab = elf_hash_table (info);
3976
3977 /* If this object is the same format as the output object, and it is
3978 not a shared library, then let the backend look through the
3979 relocs.
3980
3981 This is required to build global offset table entries and to
3982 arrange for dynamic relocs. It is not required for the
3983 particular common case of linking non PIC code, even when linking
3984 against shared libraries, but unfortunately there is no way of
3985 knowing whether an object file has been compiled PIC or not.
3986 Looking through the relocs is not particularly time consuming.
3987 The problem is that we must either (1) keep the relocs in memory,
3988 which causes the linker to require additional runtime memory or
3989 (2) read the relocs twice from the input file, which wastes time.
3990 This would be a good case for using mmap.
3991
3992 I have no idea how to handle linking PIC code into a file of a
3993 different format. It probably can't be done. */
3994 if ((abfd->flags & DYNAMIC) == 0
3995 && is_elf_hash_table (htab)
3996 && bed->check_relocs != NULL
3997 && elf_object_id (abfd) == elf_hash_table_id (htab)
3998 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3999 {
4000 asection *o;
4001
4002 for (o = abfd->sections; o != NULL; o = o->next)
4003 {
4004 Elf_Internal_Rela *internal_relocs;
4005 bfd_boolean ok;
4006
c4b126b8
L
4007 /* Don't check relocations in excluded sections. Don't do
4008 anything special with non-loaded, non-alloced sections.
4009 In particular, any relocs in such sections should not
4010 affect GOT and PLT reference counting (ie. we don't
4011 allow them to create GOT or PLT entries), there's no
4012 possibility or desire to optimize TLS relocs, and
4013 there's not much point in propagating relocs to shared
4014 libs that the dynamic linker won't relocate. */
4015 if ((o->flags & SEC_ALLOC) == 0
4016 || (o->flags & SEC_RELOC) == 0
5ce03cea 4017 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4018 || o->reloc_count == 0
4019 || ((info->strip == strip_all || info->strip == strip_debugger)
4020 && (o->flags & SEC_DEBUGGING) != 0)
4021 || bfd_is_abs_section (o->output_section))
4022 continue;
4023
4024 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4025 info->keep_memory);
4026 if (internal_relocs == NULL)
4027 return FALSE;
4028
4029 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4030
4031 if (elf_section_data (o)->relocs != internal_relocs)
4032 free (internal_relocs);
4033
4034 if (! ok)
4035 return FALSE;
4036 }
4037 }
4038
4039 return TRUE;
4040}
4041
4ad4eba5
AM
4042/* Add symbols from an ELF object file to the linker hash table. */
4043
4044static bfd_boolean
4045elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4046{
a0c402a5 4047 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4048 Elf_Internal_Shdr *hdr;
ef53be89
AM
4049 size_t symcount;
4050 size_t extsymcount;
4051 size_t extsymoff;
4ad4eba5
AM
4052 struct elf_link_hash_entry **sym_hash;
4053 bfd_boolean dynamic;
4054 Elf_External_Versym *extversym = NULL;
be22c732 4055 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4056 Elf_External_Versym *ever;
4057 struct elf_link_hash_entry *weaks;
4058 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4059 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4060 Elf_Internal_Sym *isymbuf = NULL;
4061 Elf_Internal_Sym *isym;
4062 Elf_Internal_Sym *isymend;
4063 const struct elf_backend_data *bed;
4064 bfd_boolean add_needed;
66eb6687 4065 struct elf_link_hash_table *htab;
66eb6687 4066 void *alloc_mark = NULL;
4f87808c
AM
4067 struct bfd_hash_entry **old_table = NULL;
4068 unsigned int old_size = 0;
4069 unsigned int old_count = 0;
66eb6687 4070 void *old_tab = NULL;
66eb6687
AM
4071 void *old_ent;
4072 struct bfd_link_hash_entry *old_undefs = NULL;
4073 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4074 void *old_strtab = NULL;
66eb6687 4075 size_t tabsize = 0;
db6a5d5f 4076 asection *s;
29a9f53e 4077 bfd_boolean just_syms;
4ad4eba5 4078
66eb6687 4079 htab = elf_hash_table (info);
4ad4eba5 4080 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4081
4082 if ((abfd->flags & DYNAMIC) == 0)
4083 dynamic = FALSE;
4084 else
4085 {
4086 dynamic = TRUE;
4087
4088 /* You can't use -r against a dynamic object. Also, there's no
4089 hope of using a dynamic object which does not exactly match
4090 the format of the output file. */
0e1862bb 4091 if (bfd_link_relocatable (info)
66eb6687 4092 || !is_elf_hash_table (htab)
f13a99db 4093 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4094 {
0e1862bb 4095 if (bfd_link_relocatable (info))
9a0789ec
NC
4096 bfd_set_error (bfd_error_invalid_operation);
4097 else
4098 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4099 goto error_return;
4100 }
4101 }
4102
a0c402a5
L
4103 ehdr = elf_elfheader (abfd);
4104 if (info->warn_alternate_em
4105 && bed->elf_machine_code != ehdr->e_machine
4106 && ((bed->elf_machine_alt1 != 0
4107 && ehdr->e_machine == bed->elf_machine_alt1)
4108 || (bed->elf_machine_alt2 != 0
4109 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4110 _bfd_error_handler
695344c0 4111 /* xgettext:c-format */
9793eb77 4112 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4113 ehdr->e_machine, abfd, bed->elf_machine_code);
4114
4ad4eba5
AM
4115 /* As a GNU extension, any input sections which are named
4116 .gnu.warning.SYMBOL are treated as warning symbols for the given
4117 symbol. This differs from .gnu.warning sections, which generate
4118 warnings when they are included in an output file. */
dd98f8d2 4119 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4120 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4121 {
db6a5d5f 4122 const char *name;
4ad4eba5 4123
fd361982 4124 name = bfd_section_name (s);
db6a5d5f 4125 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4126 {
db6a5d5f
AM
4127 char *msg;
4128 bfd_size_type sz;
4129
4130 name += sizeof ".gnu.warning." - 1;
4131
4132 /* If this is a shared object, then look up the symbol
4133 in the hash table. If it is there, and it is already
4134 been defined, then we will not be using the entry
4135 from this shared object, so we don't need to warn.
4136 FIXME: If we see the definition in a regular object
4137 later on, we will warn, but we shouldn't. The only
4138 fix is to keep track of what warnings we are supposed
4139 to emit, and then handle them all at the end of the
4140 link. */
4141 if (dynamic)
4ad4eba5 4142 {
db6a5d5f
AM
4143 struct elf_link_hash_entry *h;
4144
4145 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4146
4147 /* FIXME: What about bfd_link_hash_common? */
4148 if (h != NULL
4149 && (h->root.type == bfd_link_hash_defined
4150 || h->root.type == bfd_link_hash_defweak))
4151 continue;
4152 }
4ad4eba5 4153
db6a5d5f
AM
4154 sz = s->size;
4155 msg = (char *) bfd_alloc (abfd, sz + 1);
4156 if (msg == NULL)
4157 goto error_return;
4ad4eba5 4158
db6a5d5f
AM
4159 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4160 goto error_return;
4ad4eba5 4161
db6a5d5f 4162 msg[sz] = '\0';
4ad4eba5 4163
db6a5d5f
AM
4164 if (! (_bfd_generic_link_add_one_symbol
4165 (info, abfd, name, BSF_WARNING, s, 0, msg,
4166 FALSE, bed->collect, NULL)))
4167 goto error_return;
4ad4eba5 4168
0e1862bb 4169 if (bfd_link_executable (info))
db6a5d5f
AM
4170 {
4171 /* Clobber the section size so that the warning does
4172 not get copied into the output file. */
4173 s->size = 0;
11d2f718 4174
db6a5d5f
AM
4175 /* Also set SEC_EXCLUDE, so that symbols defined in
4176 the warning section don't get copied to the output. */
4177 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4178 }
4179 }
4180 }
4181
29a9f53e
L
4182 just_syms = ((s = abfd->sections) != NULL
4183 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4184
4ad4eba5
AM
4185 add_needed = TRUE;
4186 if (! dynamic)
4187 {
4188 /* If we are creating a shared library, create all the dynamic
4189 sections immediately. We need to attach them to something,
4190 so we attach them to this BFD, provided it is the right
bf89386a
L
4191 format and is not from ld --just-symbols. Always create the
4192 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4193 are no input BFD's of the same format as the output, we can't
4194 make a shared library. */
4195 if (!just_syms
bf89386a 4196 && (bfd_link_pic (info)
9c1d7a08 4197 || (!bfd_link_relocatable (info)
3c5fce9b 4198 && info->nointerp
9c1d7a08 4199 && (info->export_dynamic || info->dynamic)))
66eb6687 4200 && is_elf_hash_table (htab)
f13a99db 4201 && info->output_bfd->xvec == abfd->xvec
66eb6687 4202 && !htab->dynamic_sections_created)
4ad4eba5
AM
4203 {
4204 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4205 goto error_return;
4206 }
4207 }
66eb6687 4208 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4209 goto error_return;
4210 else
4211 {
4ad4eba5 4212 const char *soname = NULL;
7ee314fa 4213 char *audit = NULL;
4ad4eba5 4214 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4215 const Elf_Internal_Phdr *phdr;
e310298c 4216 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4217
4218 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4219 ld shouldn't allow it. */
29a9f53e 4220 if (just_syms)
92fd189d 4221 abort ();
4ad4eba5
AM
4222
4223 /* If this dynamic lib was specified on the command line with
4224 --as-needed in effect, then we don't want to add a DT_NEEDED
4225 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4226 in by another lib's DT_NEEDED. When --no-add-needed is used
4227 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4228 any dynamic library in DT_NEEDED tags in the dynamic lib at
4229 all. */
4230 add_needed = (elf_dyn_lib_class (abfd)
4231 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4232 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4233
4234 s = bfd_get_section_by_name (abfd, ".dynamic");
4235 if (s != NULL)
4236 {
4237 bfd_byte *dynbuf;
4238 bfd_byte *extdyn;
cb33740c 4239 unsigned int elfsec;
4ad4eba5
AM
4240 unsigned long shlink;
4241
eea6121a 4242 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4243 {
dc1e8a47 4244 error_free_dyn:
f8703194
L
4245 free (dynbuf);
4246 goto error_return;
4247 }
4ad4eba5
AM
4248
4249 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4250 if (elfsec == SHN_BAD)
4ad4eba5
AM
4251 goto error_free_dyn;
4252 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4253
4254 for (extdyn = dynbuf;
9bff840e 4255 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4256 extdyn += bed->s->sizeof_dyn)
4257 {
4258 Elf_Internal_Dyn dyn;
4259
4260 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4261 if (dyn.d_tag == DT_SONAME)
4262 {
4263 unsigned int tagv = dyn.d_un.d_val;
4264 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4265 if (soname == NULL)
4266 goto error_free_dyn;
4267 }
4268 if (dyn.d_tag == DT_NEEDED)
4269 {
4270 struct bfd_link_needed_list *n, **pn;
4271 char *fnm, *anm;
4272 unsigned int tagv = dyn.d_un.d_val;
986f0783 4273 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4274
a50b1753 4275 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4276 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4277 if (n == NULL || fnm == NULL)
4278 goto error_free_dyn;
4279 amt = strlen (fnm) + 1;
a50b1753 4280 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4281 if (anm == NULL)
4282 goto error_free_dyn;
4283 memcpy (anm, fnm, amt);
4284 n->name = anm;
4285 n->by = abfd;
4286 n->next = NULL;
66eb6687 4287 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4288 ;
4289 *pn = n;
4290 }
4291 if (dyn.d_tag == DT_RUNPATH)
4292 {
4293 struct bfd_link_needed_list *n, **pn;
4294 char *fnm, *anm;
4295 unsigned int tagv = dyn.d_un.d_val;
986f0783 4296 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4297
a50b1753 4298 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4299 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4300 if (n == NULL || fnm == NULL)
4301 goto error_free_dyn;
4302 amt = strlen (fnm) + 1;
a50b1753 4303 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4304 if (anm == NULL)
4305 goto error_free_dyn;
4306 memcpy (anm, fnm, amt);
4307 n->name = anm;
4308 n->by = abfd;
4309 n->next = NULL;
4310 for (pn = & runpath;
4311 *pn != NULL;
4312 pn = &(*pn)->next)
4313 ;
4314 *pn = n;
4315 }
4316 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4317 if (!runpath && dyn.d_tag == DT_RPATH)
4318 {
4319 struct bfd_link_needed_list *n, **pn;
4320 char *fnm, *anm;
4321 unsigned int tagv = dyn.d_un.d_val;
986f0783 4322 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4323
a50b1753 4324 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4325 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4326 if (n == NULL || fnm == NULL)
4327 goto error_free_dyn;
4328 amt = strlen (fnm) + 1;
a50b1753 4329 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4330 if (anm == NULL)
f8703194 4331 goto error_free_dyn;
4ad4eba5
AM
4332 memcpy (anm, fnm, amt);
4333 n->name = anm;
4334 n->by = abfd;
4335 n->next = NULL;
4336 for (pn = & rpath;
4337 *pn != NULL;
4338 pn = &(*pn)->next)
4339 ;
4340 *pn = n;
4341 }
7ee314fa
AM
4342 if (dyn.d_tag == DT_AUDIT)
4343 {
4344 unsigned int tagv = dyn.d_un.d_val;
4345 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4346 }
4ad4eba5
AM
4347 }
4348
4349 free (dynbuf);
4350 }
4351
4352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4353 frees all more recently bfd_alloc'd blocks as well. */
4354 if (runpath)
4355 rpath = runpath;
4356
4357 if (rpath)
4358 {
4359 struct bfd_link_needed_list **pn;
66eb6687 4360 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4361 ;
4362 *pn = rpath;
4363 }
4364
9acc85a6
AM
4365 /* If we have a PT_GNU_RELRO program header, mark as read-only
4366 all sections contained fully therein. This makes relro
4367 shared library sections appear as they will at run-time. */
4368 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4369 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4370 if (phdr->p_type == PT_GNU_RELRO)
4371 {
4372 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4373 {
4374 unsigned int opb = bfd_octets_per_byte (abfd, s);
4375
4376 if ((s->flags & SEC_ALLOC) != 0
4377 && s->vma * opb >= phdr->p_vaddr
4378 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4379 s->flags |= SEC_READONLY;
4380 }
9acc85a6
AM
4381 break;
4382 }
4383
4ad4eba5
AM
4384 /* We do not want to include any of the sections in a dynamic
4385 object in the output file. We hack by simply clobbering the
4386 list of sections in the BFD. This could be handled more
4387 cleanly by, say, a new section flag; the existing
4388 SEC_NEVER_LOAD flag is not the one we want, because that one
4389 still implies that the section takes up space in the output
4390 file. */
4391 bfd_section_list_clear (abfd);
4392
4ad4eba5
AM
4393 /* Find the name to use in a DT_NEEDED entry that refers to this
4394 object. If the object has a DT_SONAME entry, we use it.
4395 Otherwise, if the generic linker stuck something in
4396 elf_dt_name, we use that. Otherwise, we just use the file
4397 name. */
4398 if (soname == NULL || *soname == '\0')
4399 {
4400 soname = elf_dt_name (abfd);
4401 if (soname == NULL || *soname == '\0')
4402 soname = bfd_get_filename (abfd);
4403 }
4404
4405 /* Save the SONAME because sometimes the linker emulation code
4406 will need to know it. */
4407 elf_dt_name (abfd) = soname;
4408
4ad4eba5
AM
4409 /* If we have already included this dynamic object in the
4410 link, just ignore it. There is no reason to include a
4411 particular dynamic object more than once. */
e310298c
AM
4412 for (loaded_lib = htab->dyn_loaded;
4413 loaded_lib != NULL;
4414 loaded_lib = loaded_lib->next)
4415 {
4416 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4417 return TRUE;
4418 }
4419
4420 /* Create dynamic sections for backends that require that be done
4421 before setup_gnu_properties. */
4422 if (add_needed
4423 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4424 return FALSE;
7ee314fa
AM
4425
4426 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4427 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4428 }
4429
4430 /* If this is a dynamic object, we always link against the .dynsym
4431 symbol table, not the .symtab symbol table. The dynamic linker
4432 will only see the .dynsym symbol table, so there is no reason to
4433 look at .symtab for a dynamic object. */
4434
4435 if (! dynamic || elf_dynsymtab (abfd) == 0)
4436 hdr = &elf_tdata (abfd)->symtab_hdr;
4437 else
4438 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4439
4440 symcount = hdr->sh_size / bed->s->sizeof_sym;
4441
4442 /* The sh_info field of the symtab header tells us where the
4443 external symbols start. We don't care about the local symbols at
4444 this point. */
4445 if (elf_bad_symtab (abfd))
4446 {
4447 extsymcount = symcount;
4448 extsymoff = 0;
4449 }
4450 else
4451 {
4452 extsymcount = symcount - hdr->sh_info;
4453 extsymoff = hdr->sh_info;
4454 }
4455
f45794cb 4456 sym_hash = elf_sym_hashes (abfd);
012b2306 4457 if (extsymcount != 0)
4ad4eba5
AM
4458 {
4459 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4460 NULL, NULL, NULL);
4461 if (isymbuf == NULL)
4462 goto error_return;
4463
4ad4eba5 4464 if (sym_hash == NULL)
012b2306
AM
4465 {
4466 /* We store a pointer to the hash table entry for each
4467 external symbol. */
986f0783 4468 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4469 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4470 if (sym_hash == NULL)
4471 goto error_free_sym;
4472 elf_sym_hashes (abfd) = sym_hash;
4473 }
4ad4eba5
AM
4474 }
4475
4476 if (dynamic)
4477 {
4478 /* Read in any version definitions. */
fc0e6df6
PB
4479 if (!_bfd_elf_slurp_version_tables (abfd,
4480 info->default_imported_symver))
4ad4eba5
AM
4481 goto error_free_sym;
4482
4483 /* Read in the symbol versions, but don't bother to convert them
4484 to internal format. */
4485 if (elf_dynversym (abfd) != 0)
4486 {
986f0783
AM
4487 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4488 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4489
2bb3687b
AM
4490 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4491 goto error_free_sym;
4492 extversym = (Elf_External_Versym *)
4493 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4494 if (extversym == NULL)
4495 goto error_free_sym;
986f0783 4496 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4497 }
4498 }
4499
66eb6687
AM
4500 /* If we are loading an as-needed shared lib, save the symbol table
4501 state before we start adding symbols. If the lib turns out
4502 to be unneeded, restore the state. */
4503 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4504 {
4505 unsigned int i;
4506 size_t entsize;
4507
4508 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4509 {
4510 struct bfd_hash_entry *p;
2de92251 4511 struct elf_link_hash_entry *h;
66eb6687
AM
4512
4513 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4514 {
4515 h = (struct elf_link_hash_entry *) p;
4516 entsize += htab->root.table.entsize;
4517 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4518 {
4519 entsize += htab->root.table.entsize;
4520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4521 }
4522 if (h->root.type == bfd_link_hash_common)
4523 entsize += sizeof (*h->root.u.c.p);
2de92251 4524 }
66eb6687
AM
4525 }
4526
4527 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4528 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4529 if (old_tab == NULL)
4530 goto error_free_vers;
4531
4532 /* Remember the current objalloc pointer, so that all mem for
4533 symbols added can later be reclaimed. */
4534 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4535 if (alloc_mark == NULL)
4536 goto error_free_vers;
4537
5061a885
AM
4538 /* Make a special call to the linker "notice" function to
4539 tell it that we are about to handle an as-needed lib. */
e5034e59 4540 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4541 goto error_free_vers;
5061a885 4542
f45794cb
AM
4543 /* Clone the symbol table. Remember some pointers into the
4544 symbol table, and dynamic symbol count. */
4545 old_ent = (char *) old_tab + tabsize;
66eb6687 4546 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4547 old_undefs = htab->root.undefs;
4548 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4549 old_table = htab->root.table.table;
4550 old_size = htab->root.table.size;
4551 old_count = htab->root.table.count;
e310298c
AM
4552 old_strtab = NULL;
4553 if (htab->dynstr != NULL)
4554 {
4555 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4556 if (old_strtab == NULL)
4557 goto error_free_vers;
4558 }
66eb6687
AM
4559
4560 for (i = 0; i < htab->root.table.size; i++)
4561 {
4562 struct bfd_hash_entry *p;
2de92251 4563 struct elf_link_hash_entry *h;
66eb6687
AM
4564
4565 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4566 {
2de92251 4567 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4568 memcpy (old_ent, h, htab->root.table.entsize);
4569 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4570 if (h->root.type == bfd_link_hash_warning)
4571 {
7ba11550
AM
4572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4573 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
4575 }
7ba11550
AM
4576 if (h->root.type == bfd_link_hash_common)
4577 {
4578 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4579 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4580 }
66eb6687
AM
4581 }
4582 }
4583 }
4ad4eba5 4584
66eb6687 4585 weaks = NULL;
be22c732
NC
4586 if (extversym == NULL)
4587 ever = NULL;
4588 else if (extversym + extsymoff < extversym_end)
4589 ever = extversym + extsymoff;
4590 else
4591 {
4592 /* xgettext:c-format */
4593 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4594 abfd, (long) extsymoff,
4595 (long) (extversym_end - extversym) / sizeof (* extversym));
4596 bfd_set_error (bfd_error_bad_value);
4597 goto error_free_vers;
4598 }
4599
b4c555cf
ML
4600 if (!bfd_link_relocatable (info)
4601 && abfd->lto_slim_object)
cc5277b1
ML
4602 {
4603 _bfd_error_handler
4604 (_("%pB: plugin needed to handle lto object"), abfd);
4605 }
4606
4ad4eba5
AM
4607 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4608 isym < isymend;
4609 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4610 {
4611 int bind;
4612 bfd_vma value;
af44c138 4613 asection *sec, *new_sec;
4ad4eba5
AM
4614 flagword flags;
4615 const char *name;
4616 struct elf_link_hash_entry *h;
90c984fc 4617 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4618 bfd_boolean definition;
4619 bfd_boolean size_change_ok;
4620 bfd_boolean type_change_ok;
37a9e49a
L
4621 bfd_boolean new_weak;
4622 bfd_boolean old_weak;
7ba11550 4623 bfd *override;
a4d8e49b 4624 bfd_boolean common;
97196564 4625 bfd_boolean discarded;
4ad4eba5 4626 unsigned int old_alignment;
4538d1c7 4627 unsigned int shindex;
4ad4eba5 4628 bfd *old_bfd;
6e33951e 4629 bfd_boolean matched;
4ad4eba5 4630
7ba11550 4631 override = NULL;
4ad4eba5
AM
4632
4633 flags = BSF_NO_FLAGS;
4634 sec = NULL;
4635 value = isym->st_value;
a4d8e49b 4636 common = bed->common_definition (isym);
2980ccad
L
4637 if (common && info->inhibit_common_definition)
4638 {
4639 /* Treat common symbol as undefined for --no-define-common. */
4640 isym->st_shndx = SHN_UNDEF;
4641 common = FALSE;
4642 }
97196564 4643 discarded = FALSE;
4ad4eba5
AM
4644
4645 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4646 switch (bind)
4ad4eba5 4647 {
3e7a7d11 4648 case STB_LOCAL:
4ad4eba5
AM
4649 /* This should be impossible, since ELF requires that all
4650 global symbols follow all local symbols, and that sh_info
4651 point to the first global symbol. Unfortunately, Irix 5
4652 screws this up. */
fe3fef62
AM
4653 if (elf_bad_symtab (abfd))
4654 continue;
4655
4656 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4657 then we'll likely segfault on a NULL symbol hash if the
4658 symbol is ever referenced in relocations. */
4659 shindex = elf_elfheader (abfd)->e_shstrndx;
4660 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4661 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4662 " (>= sh_info of %lu)"),
4663 abfd, name, (long) (isym - isymbuf + extsymoff),
4664 (long) extsymoff);
4665
4666 /* Dynamic object relocations are not processed by ld, so
4667 ld won't run into the problem mentioned above. */
4668 if (dynamic)
4669 continue;
fe3fef62
AM
4670 bfd_set_error (bfd_error_bad_value);
4671 goto error_free_vers;
3e7a7d11
NC
4672
4673 case STB_GLOBAL:
a4d8e49b 4674 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4675 flags = BSF_GLOBAL;
3e7a7d11
NC
4676 break;
4677
4678 case STB_WEAK:
4679 flags = BSF_WEAK;
4680 break;
4681
4682 case STB_GNU_UNIQUE:
4683 flags = BSF_GNU_UNIQUE;
4684 break;
4685
4686 default:
4ad4eba5 4687 /* Leave it up to the processor backend. */
3e7a7d11 4688 break;
4ad4eba5
AM
4689 }
4690
4691 if (isym->st_shndx == SHN_UNDEF)
4692 sec = bfd_und_section_ptr;
cb33740c
AM
4693 else if (isym->st_shndx == SHN_ABS)
4694 sec = bfd_abs_section_ptr;
4695 else if (isym->st_shndx == SHN_COMMON)
4696 {
4697 sec = bfd_com_section_ptr;
4698 /* What ELF calls the size we call the value. What ELF
4699 calls the value we call the alignment. */
4700 value = isym->st_size;
4701 }
4702 else
4ad4eba5
AM
4703 {
4704 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4705 if (sec == NULL)
4706 sec = bfd_abs_section_ptr;
dbaa2011 4707 else if (discarded_section (sec))
529fcb95 4708 {
e5d08002
L
4709 /* Symbols from discarded section are undefined. We keep
4710 its visibility. */
529fcb95 4711 sec = bfd_und_section_ptr;
97196564 4712 discarded = TRUE;
529fcb95
PB
4713 isym->st_shndx = SHN_UNDEF;
4714 }
4ad4eba5
AM
4715 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4716 value -= sec->vma;
4717 }
4ad4eba5
AM
4718
4719 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4720 isym->st_name);
4721 if (name == NULL)
4722 goto error_free_vers;
4723
4724 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4725 && (abfd->flags & BFD_PLUGIN) != 0)
4726 {
4727 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4728
4729 if (xc == NULL)
4730 {
4731 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4732 | SEC_EXCLUDE);
4733 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4734 if (xc == NULL)
4735 goto error_free_vers;
4736 }
4737 sec = xc;
4738 }
4739 else if (isym->st_shndx == SHN_COMMON
4740 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4741 && !bfd_link_relocatable (info))
4ad4eba5
AM
4742 {
4743 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4744
4745 if (tcomm == NULL)
4746 {
02d00247
AM
4747 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4748 | SEC_LINKER_CREATED);
4749 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4750 if (tcomm == NULL)
4ad4eba5
AM
4751 goto error_free_vers;
4752 }
4753 sec = tcomm;
4754 }
66eb6687 4755 else if (bed->elf_add_symbol_hook)
4ad4eba5 4756 {
66eb6687
AM
4757 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4758 &sec, &value))
4ad4eba5
AM
4759 goto error_free_vers;
4760
4761 /* The hook function sets the name to NULL if this symbol
4762 should be skipped for some reason. */
4763 if (name == NULL)
4764 continue;
4765 }
4766
4767 /* Sanity check that all possibilities were handled. */
4768 if (sec == NULL)
4538d1c7 4769 abort ();
4ad4eba5 4770
191c0c42
AM
4771 /* Silently discard TLS symbols from --just-syms. There's
4772 no way to combine a static TLS block with a new TLS block
4773 for this executable. */
4774 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4775 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4776 continue;
4777
4ad4eba5
AM
4778 if (bfd_is_und_section (sec)
4779 || bfd_is_com_section (sec))
4780 definition = FALSE;
4781 else
4782 definition = TRUE;
4783
4784 size_change_ok = FALSE;
66eb6687 4785 type_change_ok = bed->type_change_ok;
37a9e49a 4786 old_weak = FALSE;
6e33951e 4787 matched = FALSE;
4ad4eba5
AM
4788 old_alignment = 0;
4789 old_bfd = NULL;
af44c138 4790 new_sec = sec;
4ad4eba5 4791
66eb6687 4792 if (is_elf_hash_table (htab))
4ad4eba5
AM
4793 {
4794 Elf_Internal_Versym iver;
4795 unsigned int vernum = 0;
4796 bfd_boolean skip;
4797
fc0e6df6 4798 if (ever == NULL)
4ad4eba5 4799 {
fc0e6df6
PB
4800 if (info->default_imported_symver)
4801 /* Use the default symbol version created earlier. */
4802 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4803 else
4804 iver.vs_vers = 0;
4805 }
be22c732
NC
4806 else if (ever >= extversym_end)
4807 {
4808 /* xgettext:c-format */
4809 _bfd_error_handler (_("%pB: not enough version information"),
4810 abfd);
4811 bfd_set_error (bfd_error_bad_value);
4812 goto error_free_vers;
4813 }
fc0e6df6
PB
4814 else
4815 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4816
4817 vernum = iver.vs_vers & VERSYM_VERSION;
4818
4819 /* If this is a hidden symbol, or if it is not version
4820 1, we append the version name to the symbol name.
cc86ff91
EB
4821 However, we do not modify a non-hidden absolute symbol
4822 if it is not a function, because it might be the version
4823 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4824 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4825 || (vernum > 1
4826 && (!bfd_is_abs_section (sec)
4827 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4828 {
4829 const char *verstr;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4832
4833 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4834 {
fc0e6df6
PB
4835 if (vernum > elf_tdata (abfd)->cverdefs)
4836 verstr = NULL;
4837 else if (vernum > 1)
4838 verstr =
4839 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4840 else
4841 verstr = "";
4ad4eba5 4842
fc0e6df6 4843 if (verstr == NULL)
4ad4eba5 4844 {
4eca0228 4845 _bfd_error_handler
695344c0 4846 /* xgettext:c-format */
871b3ab2 4847 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4848 abfd, name, vernum,
4849 elf_tdata (abfd)->cverdefs);
4850 bfd_set_error (bfd_error_bad_value);
4851 goto error_free_vers;
4ad4eba5 4852 }
fc0e6df6
PB
4853 }
4854 else
4855 {
4856 /* We cannot simply test for the number of
4857 entries in the VERNEED section since the
4858 numbers for the needed versions do not start
4859 at 0. */
4860 Elf_Internal_Verneed *t;
4861
4862 verstr = NULL;
4863 for (t = elf_tdata (abfd)->verref;
4864 t != NULL;
4865 t = t->vn_nextref)
4ad4eba5 4866 {
fc0e6df6 4867 Elf_Internal_Vernaux *a;
4ad4eba5 4868
fc0e6df6
PB
4869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4870 {
4871 if (a->vna_other == vernum)
4ad4eba5 4872 {
fc0e6df6
PB
4873 verstr = a->vna_nodename;
4874 break;
4ad4eba5 4875 }
4ad4eba5 4876 }
fc0e6df6
PB
4877 if (a != NULL)
4878 break;
4879 }
4880 if (verstr == NULL)
4881 {
4eca0228 4882 _bfd_error_handler
695344c0 4883 /* xgettext:c-format */
871b3ab2 4884 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4885 abfd, name, vernum);
4886 bfd_set_error (bfd_error_bad_value);
4887 goto error_free_vers;
4ad4eba5 4888 }
4ad4eba5 4889 }
fc0e6df6
PB
4890
4891 namelen = strlen (name);
4892 verlen = strlen (verstr);
4893 newlen = namelen + verlen + 2;
4894 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4895 && isym->st_shndx != SHN_UNDEF)
4896 ++newlen;
4897
a50b1753 4898 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4899 if (newname == NULL)
4900 goto error_free_vers;
4901 memcpy (newname, name, namelen);
4902 p = newname + namelen;
4903 *p++ = ELF_VER_CHR;
4904 /* If this is a defined non-hidden version symbol,
4905 we add another @ to the name. This indicates the
4906 default version of the symbol. */
4907 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4908 && isym->st_shndx != SHN_UNDEF)
4909 *p++ = ELF_VER_CHR;
4910 memcpy (p, verstr, verlen + 1);
4911
4912 name = newname;
4ad4eba5
AM
4913 }
4914
cd3416da
AM
4915 /* If this symbol has default visibility and the user has
4916 requested we not re-export it, then mark it as hidden. */
a0d49154 4917 if (!bfd_is_und_section (sec)
cd3416da 4918 && !dynamic
ce875075 4919 && abfd->no_export
cd3416da
AM
4920 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4921 isym->st_other = (STV_HIDDEN
4922 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4923
4f3fedcf
AM
4924 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4925 sym_hash, &old_bfd, &old_weak,
4926 &old_alignment, &skip, &override,
6e33951e
L
4927 &type_change_ok, &size_change_ok,
4928 &matched))
4ad4eba5
AM
4929 goto error_free_vers;
4930
4931 if (skip)
4932 continue;
4933
6e33951e
L
4934 /* Override a definition only if the new symbol matches the
4935 existing one. */
4936 if (override && matched)
4ad4eba5
AM
4937 definition = FALSE;
4938
4939 h = *sym_hash;
4940 while (h->root.type == bfd_link_hash_indirect
4941 || h->root.type == bfd_link_hash_warning)
4942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4943
4ad4eba5 4944 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4945 && vernum > 1
4946 && definition)
4947 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4948 }
4949
4950 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4951 (info, override ? override : abfd, name, flags, sec, value,
4952 NULL, FALSE, bed->collect,
4ad4eba5
AM
4953 (struct bfd_link_hash_entry **) sym_hash)))
4954 goto error_free_vers;
4955
4956 h = *sym_hash;
90c984fc
L
4957 /* We need to make sure that indirect symbol dynamic flags are
4958 updated. */
4959 hi = h;
4ad4eba5
AM
4960 while (h->root.type == bfd_link_hash_indirect
4961 || h->root.type == bfd_link_hash_warning)
4962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4963
97196564
L
4964 /* Setting the index to -3 tells elf_link_output_extsym that
4965 this symbol is defined in a discarded section. */
4966 if (discarded)
4967 h->indx = -3;
4968
4ad4eba5
AM
4969 *sym_hash = h;
4970
37a9e49a 4971 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4972 if (dynamic
4973 && definition
37a9e49a 4974 && new_weak
fcb93ecf 4975 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4976 && is_elf_hash_table (htab)
60d67dc8 4977 && h->u.alias == NULL)
4ad4eba5
AM
4978 {
4979 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4980 a dynamic object, using the alias field. Later in this
4981 function we will set the alias field to the correct
4ad4eba5
AM
4982 value. We only put non-function symbols from dynamic
4983 objects on this list, because that happens to be the only
4984 time we need to know the normal symbol corresponding to a
4985 weak symbol, and the information is time consuming to
60d67dc8 4986 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4987 then this symbol was already defined by some previous
4988 dynamic object, and we will be using that previous
4989 definition anyhow. */
4990
60d67dc8 4991 h->u.alias = weaks;
4ad4eba5 4992 weaks = h;
4ad4eba5
AM
4993 }
4994
4995 /* Set the alignment of a common symbol. */
a4d8e49b 4996 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4997 && h->root.type == bfd_link_hash_common)
4998 {
4999 unsigned int align;
5000
a4d8e49b 5001 if (common)
af44c138
L
5002 align = bfd_log2 (isym->st_value);
5003 else
5004 {
5005 /* The new symbol is a common symbol in a shared object.
5006 We need to get the alignment from the section. */
5007 align = new_sec->alignment_power;
5008 }
595213d4 5009 if (align > old_alignment)
4ad4eba5
AM
5010 h->root.u.c.p->alignment_power = align;
5011 else
5012 h->root.u.c.p->alignment_power = old_alignment;
5013 }
5014
66eb6687 5015 if (is_elf_hash_table (htab))
4ad4eba5 5016 {
4f3fedcf
AM
5017 /* Set a flag in the hash table entry indicating the type of
5018 reference or definition we just found. A dynamic symbol
5019 is one which is referenced or defined by both a regular
5020 object and a shared object. */
5021 bfd_boolean dynsym = FALSE;
5022
b1a92c63
AM
5023 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5024 if ((abfd->flags & BFD_PLUGIN) != 0)
5025 ;
5026 else if (!dynamic)
4f3fedcf
AM
5027 {
5028 if (! definition)
5029 {
5030 h->ref_regular = 1;
5031 if (bind != STB_WEAK)
5032 h->ref_regular_nonweak = 1;
5033 }
5034 else
5035 {
5036 h->def_regular = 1;
5037 if (h->def_dynamic)
5038 {
5039 h->def_dynamic = 0;
5040 h->ref_dynamic = 1;
5041 }
5042 }
4f3fedcf
AM
5043 }
5044 else
5045 {
5046 if (! definition)
5047 {
5048 h->ref_dynamic = 1;
5049 hi->ref_dynamic = 1;
5050 }
5051 else
5052 {
5053 h->def_dynamic = 1;
5054 hi->def_dynamic = 1;
5055 }
b1a92c63 5056 }
4f3fedcf 5057
b1a92c63
AM
5058 /* If an indirect symbol has been forced local, don't
5059 make the real symbol dynamic. */
5060 if (h != hi && hi->forced_local)
5061 ;
5062 else if (!dynamic)
5063 {
5064 if (bfd_link_dll (info)
5065 || h->def_dynamic
5066 || h->ref_dynamic)
5067 dynsym = TRUE;
5068 }
5069 else
5070 {
5071 if (h->def_regular
5072 || h->ref_regular
5073 || (h->is_weakalias
5074 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5075 dynsym = TRUE;
5076 }
5077
5078 /* Check to see if we need to add an indirect symbol for
5079 the default name. */
726d7d1e
AM
5080 if ((definition
5081 || (!override && h->root.type == bfd_link_hash_common))
5082 && !(hi != h
5083 && hi->versioned == versioned_hidden))
4f3fedcf
AM
5084 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5085 sec, value, &old_bfd, &dynsym))
5086 goto error_free_vers;
4ad4eba5
AM
5087
5088 /* Check the alignment when a common symbol is involved. This
5089 can change when a common symbol is overridden by a normal
5090 definition or a common symbol is ignored due to the old
5091 normal definition. We need to make sure the maximum
5092 alignment is maintained. */
a4d8e49b 5093 if ((old_alignment || common)
4ad4eba5
AM
5094 && h->root.type != bfd_link_hash_common)
5095 {
5096 unsigned int common_align;
5097 unsigned int normal_align;
5098 unsigned int symbol_align;
5099 bfd *normal_bfd;
5100 bfd *common_bfd;
5101
3a81e825
AM
5102 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5103 || h->root.type == bfd_link_hash_defweak);
5104
4ad4eba5
AM
5105 symbol_align = ffs (h->root.u.def.value) - 1;
5106 if (h->root.u.def.section->owner != NULL
0616a280
AM
5107 && (h->root.u.def.section->owner->flags
5108 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5109 {
5110 normal_align = h->root.u.def.section->alignment_power;
5111 if (normal_align > symbol_align)
5112 normal_align = symbol_align;
5113 }
5114 else
5115 normal_align = symbol_align;
5116
5117 if (old_alignment)
5118 {
5119 common_align = old_alignment;
5120 common_bfd = old_bfd;
5121 normal_bfd = abfd;
5122 }
5123 else
5124 {
5125 common_align = bfd_log2 (isym->st_value);
5126 common_bfd = abfd;
5127 normal_bfd = old_bfd;
5128 }
5129
5130 if (normal_align < common_align)
d07676f8
NC
5131 {
5132 /* PR binutils/2735 */
5133 if (normal_bfd == NULL)
4eca0228 5134 _bfd_error_handler
695344c0 5135 /* xgettext:c-format */
9793eb77 5136 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5137 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5138 1 << common_align, name, common_bfd,
5139 1 << normal_align, h->root.u.def.section);
d07676f8 5140 else
4eca0228 5141 _bfd_error_handler
695344c0 5142 /* xgettext:c-format */
9793eb77 5143 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5144 " is smaller than %u in %pB"),
c08bb8dd
AM
5145 1 << normal_align, name, normal_bfd,
5146 1 << common_align, common_bfd);
d07676f8 5147 }
4ad4eba5
AM
5148 }
5149
83ad0046 5150 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5151 if (isym->st_size != 0
5152 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5153 && (definition || h->size == 0))
5154 {
83ad0046
L
5155 if (h->size != 0
5156 && h->size != isym->st_size
5157 && ! size_change_ok)
4eca0228 5158 _bfd_error_handler
695344c0 5159 /* xgettext:c-format */
9793eb77 5160 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5161 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5162 name, (uint64_t) h->size, old_bfd,
5163 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5164
5165 h->size = isym->st_size;
5166 }
5167
5168 /* If this is a common symbol, then we always want H->SIZE
5169 to be the size of the common symbol. The code just above
5170 won't fix the size if a common symbol becomes larger. We
5171 don't warn about a size change here, because that is
4f3fedcf 5172 covered by --warn-common. Allow changes between different
fcb93ecf 5173 function types. */
4ad4eba5
AM
5174 if (h->root.type == bfd_link_hash_common)
5175 h->size = h->root.u.c.size;
5176
5177 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5178 && ((definition && !new_weak)
5179 || (old_weak && h->root.type == bfd_link_hash_common)
5180 || h->type == STT_NOTYPE))
4ad4eba5 5181 {
2955ec4c
L
5182 unsigned int type = ELF_ST_TYPE (isym->st_info);
5183
5184 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5185 symbol. */
5186 if (type == STT_GNU_IFUNC
5187 && (abfd->flags & DYNAMIC) != 0)
5188 type = STT_FUNC;
4ad4eba5 5189
2955ec4c
L
5190 if (h->type != type)
5191 {
5192 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5193 /* xgettext:c-format */
4eca0228 5194 _bfd_error_handler
9793eb77 5195 (_("warning: type of symbol `%s' changed"
871b3ab2 5196 " from %d to %d in %pB"),
c08bb8dd 5197 name, h->type, type, abfd);
2955ec4c
L
5198
5199 h->type = type;
5200 }
4ad4eba5
AM
5201 }
5202
54ac0771 5203 /* Merge st_other field. */
5160d0f3
AM
5204 elf_merge_st_other (abfd, h, isym->st_other, sec,
5205 definition, dynamic);
4ad4eba5 5206
c3df8c14 5207 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5208 if (definition
5209 && (sec->flags & SEC_DEBUGGING)
5210 && !bfd_link_relocatable (info))
c3df8c14
AM
5211 dynsym = FALSE;
5212
b1a92c63
AM
5213 /* Nor should we make plugin symbols dynamic. */
5214 if ((abfd->flags & BFD_PLUGIN) != 0)
5215 dynsym = FALSE;
5216
35fc36a8 5217 if (definition)
35399224
L
5218 {
5219 h->target_internal = isym->st_target_internal;
5220 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5221 }
35fc36a8 5222
4ad4eba5
AM
5223 if (definition && !dynamic)
5224 {
5225 char *p = strchr (name, ELF_VER_CHR);
5226 if (p != NULL && p[1] != ELF_VER_CHR)
5227 {
5228 /* Queue non-default versions so that .symver x, x@FOO
5229 aliases can be checked. */
66eb6687 5230 if (!nondeflt_vers)
4ad4eba5 5231 {
986f0783
AM
5232 size_t amt = ((isymend - isym + 1)
5233 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5234 nondeflt_vers
5235 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5236 if (!nondeflt_vers)
5237 goto error_free_vers;
4ad4eba5 5238 }
66eb6687 5239 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5240 }
5241 }
5242
b1a92c63 5243 if (dynsym && h->dynindx == -1)
4ad4eba5 5244 {
c152c796 5245 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5246 goto error_free_vers;
60d67dc8
AM
5247 if (h->is_weakalias
5248 && weakdef (h)->dynindx == -1)
4ad4eba5 5249 {
60d67dc8 5250 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5251 goto error_free_vers;
5252 }
5253 }
1f599d0e 5254 else if (h->dynindx != -1)
4ad4eba5
AM
5255 /* If the symbol already has a dynamic index, but
5256 visibility says it should not be visible, turn it into
5257 a local symbol. */
5258 switch (ELF_ST_VISIBILITY (h->other))
5259 {
5260 case STV_INTERNAL:
5261 case STV_HIDDEN:
5262 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5263 dynsym = FALSE;
5264 break;
5265 }
5266
5267 if (!add_needed
aef28989 5268 && matched
4ad4eba5 5269 && definition
010e5ae2 5270 && ((dynsym
a896df97 5271 && h->ref_regular_nonweak)
b1a92c63
AM
5272 || (old_bfd != NULL
5273 && (old_bfd->flags & BFD_PLUGIN) != 0
5274 && bind != STB_WEAK)
ffa9430d 5275 || (h->ref_dynamic_nonweak
010e5ae2 5276 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5277 && !on_needed_list (elf_dt_name (abfd),
5278 htab->needed, NULL))))
4ad4eba5 5279 {
4ad4eba5
AM
5280 const char *soname = elf_dt_name (abfd);
5281
16e4ecc0
AM
5282 info->callbacks->minfo ("%!", soname, old_bfd,
5283 h->root.root.string);
5284
4ad4eba5
AM
5285 /* A symbol from a library loaded via DT_NEEDED of some
5286 other library is referenced by a regular object.
e56f61be 5287 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5288 --no-add-needed is used and the reference was not
5289 a weak one. */
4f3fedcf 5290 if (old_bfd != NULL
b918acf9 5291 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5292 {
4eca0228 5293 _bfd_error_handler
695344c0 5294 /* xgettext:c-format */
871b3ab2 5295 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5296 old_bfd, name);
ff5ac77b 5297 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5298 goto error_free_vers;
5299 }
5300
a50b1753 5301 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5302 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5303
e310298c
AM
5304 /* Create dynamic sections for backends that require
5305 that be done before setup_gnu_properties. */
5306 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5307 return FALSE;
4ad4eba5 5308 add_needed = TRUE;
4ad4eba5
AM
5309 }
5310 }
5311 }
5312
a83ef4d1
L
5313 if (info->lto_plugin_active
5314 && !bfd_link_relocatable (info)
5315 && (abfd->flags & BFD_PLUGIN) == 0
5316 && !just_syms
5317 && extsymcount)
5318 {
5319 int r_sym_shift;
5320
5321 if (bed->s->arch_size == 32)
5322 r_sym_shift = 8;
5323 else
5324 r_sym_shift = 32;
5325
5326 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5327 referenced in regular objects so that linker plugin will get
5328 the correct symbol resolution. */
5329
5330 sym_hash = elf_sym_hashes (abfd);
5331 for (s = abfd->sections; s != NULL; s = s->next)
5332 {
5333 Elf_Internal_Rela *internal_relocs;
5334 Elf_Internal_Rela *rel, *relend;
5335
5336 /* Don't check relocations in excluded sections. */
5337 if ((s->flags & SEC_RELOC) == 0
5338 || s->reloc_count == 0
5339 || (s->flags & SEC_EXCLUDE) != 0
5340 || ((info->strip == strip_all
5341 || info->strip == strip_debugger)
5342 && (s->flags & SEC_DEBUGGING) != 0))
5343 continue;
5344
5345 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5346 NULL,
5347 info->keep_memory);
5348 if (internal_relocs == NULL)
5349 goto error_free_vers;
5350
5351 rel = internal_relocs;
5352 relend = rel + s->reloc_count;
5353 for ( ; rel < relend; rel++)
5354 {
5355 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5356 struct elf_link_hash_entry *h;
5357
5358 /* Skip local symbols. */
5359 if (r_symndx < extsymoff)
5360 continue;
5361
5362 h = sym_hash[r_symndx - extsymoff];
5363 if (h != NULL)
5364 h->root.non_ir_ref_regular = 1;
5365 }
5366
5367 if (elf_section_data (s)->relocs != internal_relocs)
5368 free (internal_relocs);
5369 }
5370 }
5371
c9594989
AM
5372 free (extversym);
5373 extversym = NULL;
5374 free (isymbuf);
5375 isymbuf = NULL;
66eb6687
AM
5376
5377 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5378 {
5379 unsigned int i;
5380
5381 /* Restore the symbol table. */
f45794cb
AM
5382 old_ent = (char *) old_tab + tabsize;
5383 memset (elf_sym_hashes (abfd), 0,
5384 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5385 htab->root.table.table = old_table;
5386 htab->root.table.size = old_size;
5387 htab->root.table.count = old_count;
66eb6687 5388 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5389 htab->root.undefs = old_undefs;
5390 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5391 if (htab->dynstr != NULL)
5392 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5393 free (old_strtab);
5394 old_strtab = NULL;
66eb6687
AM
5395 for (i = 0; i < htab->root.table.size; i++)
5396 {
5397 struct bfd_hash_entry *p;
5398 struct elf_link_hash_entry *h;
4070765b 5399 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5400
5401 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5402 {
4070765b 5403 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5404 will be exported when the dynamic lib becomes needed
5405 in the second pass. */
7ba11550
AM
5406 h = (struct elf_link_hash_entry *) p;
5407 if (h->root.type == bfd_link_hash_warning)
5408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5409 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5410
2de92251 5411 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5412 memcpy (h, old_ent, htab->root.table.entsize);
5413 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5414 if (h->root.type == bfd_link_hash_warning)
5415 {
a4542f1b 5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5419 }
a4542f1b 5420 if (h->root.type == bfd_link_hash_common)
3e0882af 5421 {
7ba11550
AM
5422 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5423 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5424 }
4070765b 5425 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5426 }
5427 }
5428
5061a885
AM
5429 /* Make a special call to the linker "notice" function to
5430 tell it that symbols added for crefs may need to be removed. */
e5034e59 5431 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5432 goto error_free_vers;
5061a885 5433
66eb6687
AM
5434 free (old_tab);
5435 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5436 alloc_mark);
c9594989 5437 free (nondeflt_vers);
66eb6687
AM
5438 return TRUE;
5439 }
2de92251 5440
66eb6687
AM
5441 if (old_tab != NULL)
5442 {
e5034e59 5443 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5444 goto error_free_vers;
66eb6687
AM
5445 free (old_tab);
5446 old_tab = NULL;
5447 }
5448
c6e8a9a8
L
5449 /* Now that all the symbols from this input file are created, if
5450 not performing a relocatable link, handle .symver foo, foo@BAR
5451 such that any relocs against foo become foo@BAR. */
0e1862bb 5452 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5453 {
ef53be89 5454 size_t cnt, symidx;
4ad4eba5
AM
5455
5456 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5457 {
5458 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5459 char *shortname, *p;
986f0783 5460 size_t amt;
4ad4eba5
AM
5461
5462 p = strchr (h->root.root.string, ELF_VER_CHR);
5463 if (p == NULL
5464 || (h->root.type != bfd_link_hash_defined
5465 && h->root.type != bfd_link_hash_defweak))
5466 continue;
5467
5468 amt = p - h->root.root.string;
a50b1753 5469 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5470 if (!shortname)
5471 goto error_free_vers;
4ad4eba5
AM
5472 memcpy (shortname, h->root.root.string, amt);
5473 shortname[amt] = '\0';
5474
5475 hi = (struct elf_link_hash_entry *)
66eb6687 5476 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5477 FALSE, FALSE, FALSE);
5478 if (hi != NULL
5479 && hi->root.type == h->root.type
5480 && hi->root.u.def.value == h->root.u.def.value
5481 && hi->root.u.def.section == h->root.u.def.section)
5482 {
5483 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5484 hi->root.type = bfd_link_hash_indirect;
5485 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5486 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5487 sym_hash = elf_sym_hashes (abfd);
5488 if (sym_hash)
5489 for (symidx = 0; symidx < extsymcount; ++symidx)
5490 if (sym_hash[symidx] == hi)
5491 {
5492 sym_hash[symidx] = h;
5493 break;
5494 }
5495 }
5496 free (shortname);
5497 }
5498 free (nondeflt_vers);
5499 nondeflt_vers = NULL;
5500 }
5501
60d67dc8 5502 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5503 symbols we found. The only way to do this is to search all the
5504 symbols. Since we only need the information for non functions in
5505 dynamic objects, that's the only time we actually put anything on
5506 the list WEAKS. We need this information so that if a regular
5507 object refers to a symbol defined weakly in a dynamic object, the
5508 real symbol in the dynamic object is also put in the dynamic
5509 symbols; we also must arrange for both symbols to point to the
5510 same memory location. We could handle the general case of symbol
5511 aliasing, but a general symbol alias can only be generated in
5512 assembler code, handling it correctly would be very time
5513 consuming, and other ELF linkers don't handle general aliasing
5514 either. */
5515 if (weaks != NULL)
5516 {
5517 struct elf_link_hash_entry **hpp;
5518 struct elf_link_hash_entry **hppend;
5519 struct elf_link_hash_entry **sorted_sym_hash;
5520 struct elf_link_hash_entry *h;
986f0783 5521 size_t sym_count, amt;
4ad4eba5
AM
5522
5523 /* Since we have to search the whole symbol list for each weak
5524 defined symbol, search time for N weak defined symbols will be
5525 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5526 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5527 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5528 if (sorted_sym_hash == NULL)
5529 goto error_return;
5530 sym_hash = sorted_sym_hash;
5531 hpp = elf_sym_hashes (abfd);
5532 hppend = hpp + extsymcount;
5533 sym_count = 0;
5534 for (; hpp < hppend; hpp++)
5535 {
5536 h = *hpp;
5537 if (h != NULL
5538 && h->root.type == bfd_link_hash_defined
fcb93ecf 5539 && !bed->is_function_type (h->type))
4ad4eba5
AM
5540 {
5541 *sym_hash = h;
5542 sym_hash++;
5543 sym_count++;
5544 }
5545 }
5546
3a3f4bf7 5547 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5548 elf_sort_symbol);
5549
5550 while (weaks != NULL)
5551 {
5552 struct elf_link_hash_entry *hlook;
5553 asection *slook;
5554 bfd_vma vlook;
ed54588d 5555 size_t i, j, idx = 0;
4ad4eba5
AM
5556
5557 hlook = weaks;
60d67dc8
AM
5558 weaks = hlook->u.alias;
5559 hlook->u.alias = NULL;
4ad4eba5 5560
e3e53eed
AM
5561 if (hlook->root.type != bfd_link_hash_defined
5562 && hlook->root.type != bfd_link_hash_defweak)
5563 continue;
5564
4ad4eba5
AM
5565 slook = hlook->root.u.def.section;
5566 vlook = hlook->root.u.def.value;
5567
4ad4eba5
AM
5568 i = 0;
5569 j = sym_count;
14160578 5570 while (i != j)
4ad4eba5
AM
5571 {
5572 bfd_signed_vma vdiff;
5573 idx = (i + j) / 2;
14160578 5574 h = sorted_sym_hash[idx];
4ad4eba5
AM
5575 vdiff = vlook - h->root.u.def.value;
5576 if (vdiff < 0)
5577 j = idx;
5578 else if (vdiff > 0)
5579 i = idx + 1;
5580 else
5581 {
d3435ae8 5582 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5583 if (sdiff < 0)
5584 j = idx;
5585 else if (sdiff > 0)
5586 i = idx + 1;
5587 else
14160578 5588 break;
4ad4eba5
AM
5589 }
5590 }
5591
5592 /* We didn't find a value/section match. */
14160578 5593 if (i == j)
4ad4eba5
AM
5594 continue;
5595
14160578
AM
5596 /* With multiple aliases, or when the weak symbol is already
5597 strongly defined, we have multiple matching symbols and
5598 the binary search above may land on any of them. Step
5599 one past the matching symbol(s). */
5600 while (++idx != j)
5601 {
5602 h = sorted_sym_hash[idx];
5603 if (h->root.u.def.section != slook
5604 || h->root.u.def.value != vlook)
5605 break;
5606 }
5607
5608 /* Now look back over the aliases. Since we sorted by size
5609 as well as value and section, we'll choose the one with
5610 the largest size. */
5611 while (idx-- != i)
4ad4eba5 5612 {
14160578 5613 h = sorted_sym_hash[idx];
4ad4eba5
AM
5614
5615 /* Stop if value or section doesn't match. */
14160578
AM
5616 if (h->root.u.def.section != slook
5617 || h->root.u.def.value != vlook)
4ad4eba5
AM
5618 break;
5619 else if (h != hlook)
5620 {
60d67dc8
AM
5621 struct elf_link_hash_entry *t;
5622
5623 hlook->u.alias = h;
5624 hlook->is_weakalias = 1;
5625 t = h;
5626 if (t->u.alias != NULL)
5627 while (t->u.alias != h)
5628 t = t->u.alias;
5629 t->u.alias = hlook;
4ad4eba5
AM
5630
5631 /* If the weak definition is in the list of dynamic
5632 symbols, make sure the real definition is put
5633 there as well. */
5634 if (hlook->dynindx != -1 && h->dynindx == -1)
5635 {
c152c796 5636 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5637 {
5638 err_free_sym_hash:
5639 free (sorted_sym_hash);
5640 goto error_return;
5641 }
4ad4eba5
AM
5642 }
5643
5644 /* If the real definition is in the list of dynamic
5645 symbols, make sure the weak definition is put
5646 there as well. If we don't do this, then the
5647 dynamic loader might not merge the entries for the
5648 real definition and the weak definition. */
5649 if (h->dynindx != -1 && hlook->dynindx == -1)
5650 {
c152c796 5651 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5652 goto err_free_sym_hash;
4ad4eba5
AM
5653 }
5654 break;
5655 }
5656 }
5657 }
5658
5659 free (sorted_sym_hash);
5660 }
5661
33177bb1
AM
5662 if (bed->check_directives
5663 && !(*bed->check_directives) (abfd, info))
5664 return FALSE;
85fbca6a 5665
4ad4eba5
AM
5666 /* If this is a non-traditional link, try to optimize the handling
5667 of the .stab/.stabstr sections. */
5668 if (! dynamic
5669 && ! info->traditional_format
66eb6687 5670 && is_elf_hash_table (htab)
4ad4eba5
AM
5671 && (info->strip != strip_all && info->strip != strip_debugger))
5672 {
5673 asection *stabstr;
5674
5675 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5676 if (stabstr != NULL)
5677 {
5678 bfd_size_type string_offset = 0;
5679 asection *stab;
5680
5681 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5682 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5683 && (!stab->name[5] ||
5684 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5685 && (stab->flags & SEC_MERGE) == 0
5686 && !bfd_is_abs_section (stab->output_section))
5687 {
5688 struct bfd_elf_section_data *secdata;
5689
5690 secdata = elf_section_data (stab);
66eb6687
AM
5691 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5692 stabstr, &secdata->sec_info,
4ad4eba5
AM
5693 &string_offset))
5694 goto error_return;
5695 if (secdata->sec_info)
dbaa2011 5696 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5697 }
5698 }
5699 }
5700
e310298c 5701 if (dynamic && add_needed)
4ad4eba5
AM
5702 {
5703 /* Add this bfd to the loaded list. */
5704 struct elf_link_loaded_list *n;
5705
ca4be51c 5706 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5707 if (n == NULL)
5708 goto error_return;
5709 n->abfd = abfd;
e310298c
AM
5710 n->next = htab->dyn_loaded;
5711 htab->dyn_loaded = n;
4ad4eba5 5712 }
e310298c
AM
5713 if (dynamic && !add_needed
5714 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5715 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5716
5717 return TRUE;
5718
5719 error_free_vers:
c9594989
AM
5720 free (old_tab);
5721 free (old_strtab);
5722 free (nondeflt_vers);
5723 free (extversym);
4ad4eba5 5724 error_free_sym:
c9594989 5725 free (isymbuf);
4ad4eba5
AM
5726 error_return:
5727 return FALSE;
5728}
5729
8387904d
AM
5730/* Return the linker hash table entry of a symbol that might be
5731 satisfied by an archive symbol. Return -1 on error. */
5732
5733struct elf_link_hash_entry *
5734_bfd_elf_archive_symbol_lookup (bfd *abfd,
5735 struct bfd_link_info *info,
5736 const char *name)
5737{
5738 struct elf_link_hash_entry *h;
5739 char *p, *copy;
5740 size_t len, first;
5741
2a41f396 5742 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5743 if (h != NULL)
5744 return h;
5745
5746 /* If this is a default version (the name contains @@), look up the
5747 symbol again with only one `@' as well as without the version.
5748 The effect is that references to the symbol with and without the
5749 version will be matched by the default symbol in the archive. */
5750
5751 p = strchr (name, ELF_VER_CHR);
5752 if (p == NULL || p[1] != ELF_VER_CHR)
5753 return h;
5754
5755 /* First check with only one `@'. */
5756 len = strlen (name);
a50b1753 5757 copy = (char *) bfd_alloc (abfd, len);
8387904d 5758 if (copy == NULL)
e99955cd 5759 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5760
5761 first = p - name + 1;
5762 memcpy (copy, name, first);
5763 memcpy (copy + first, name + first + 1, len - first);
5764
2a41f396 5765 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5766 if (h == NULL)
5767 {
5768 /* We also need to check references to the symbol without the
5769 version. */
5770 copy[first - 1] = '\0';
5771 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5772 FALSE, FALSE, TRUE);
8387904d
AM
5773 }
5774
5775 bfd_release (abfd, copy);
5776 return h;
5777}
5778
0ad989f9 5779/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5780 don't use _bfd_generic_link_add_archive_symbols because we need to
5781 handle versioned symbols.
0ad989f9
L
5782
5783 Fortunately, ELF archive handling is simpler than that done by
5784 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5785 oddities. In ELF, if we find a symbol in the archive map, and the
5786 symbol is currently undefined, we know that we must pull in that
5787 object file.
5788
5789 Unfortunately, we do have to make multiple passes over the symbol
5790 table until nothing further is resolved. */
5791
4ad4eba5
AM
5792static bfd_boolean
5793elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5794{
5795 symindex c;
13e570f8 5796 unsigned char *included = NULL;
0ad989f9
L
5797 carsym *symdefs;
5798 bfd_boolean loop;
986f0783 5799 size_t amt;
8387904d
AM
5800 const struct elf_backend_data *bed;
5801 struct elf_link_hash_entry * (*archive_symbol_lookup)
5802 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5803
5804 if (! bfd_has_map (abfd))
5805 {
5806 /* An empty archive is a special case. */
5807 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5808 return TRUE;
5809 bfd_set_error (bfd_error_no_armap);
5810 return FALSE;
5811 }
5812
5813 /* Keep track of all symbols we know to be already defined, and all
5814 files we know to be already included. This is to speed up the
5815 second and subsequent passes. */
5816 c = bfd_ardata (abfd)->symdef_count;
5817 if (c == 0)
5818 return TRUE;
986f0783 5819 amt = c * sizeof (*included);
13e570f8
AM
5820 included = (unsigned char *) bfd_zmalloc (amt);
5821 if (included == NULL)
5822 return FALSE;
0ad989f9
L
5823
5824 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5825 bed = get_elf_backend_data (abfd);
5826 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5827
5828 do
5829 {
5830 file_ptr last;
5831 symindex i;
5832 carsym *symdef;
5833 carsym *symdefend;
5834
5835 loop = FALSE;
5836 last = -1;
5837
5838 symdef = symdefs;
5839 symdefend = symdef + c;
5840 for (i = 0; symdef < symdefend; symdef++, i++)
5841 {
5842 struct elf_link_hash_entry *h;
5843 bfd *element;
5844 struct bfd_link_hash_entry *undefs_tail;
5845 symindex mark;
5846
13e570f8 5847 if (included[i])
0ad989f9
L
5848 continue;
5849 if (symdef->file_offset == last)
5850 {
5851 included[i] = TRUE;
5852 continue;
5853 }
5854
8387904d 5855 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5856 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5857 goto error_return;
0ad989f9
L
5858
5859 if (h == NULL)
5860 continue;
5861
75cfe082
AM
5862 if (h->root.type == bfd_link_hash_undefined)
5863 {
5864 /* If the archive element has already been loaded then one
5865 of the symbols defined by that element might have been
5866 made undefined due to being in a discarded section. */
5867 if (h->indx == -3)
5868 continue;
5869 }
5870 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5871 {
5872 /* We currently have a common symbol. The archive map contains
5873 a reference to this symbol, so we may want to include it. We
5874 only want to include it however, if this archive element
5875 contains a definition of the symbol, not just another common
5876 declaration of it.
5877
5878 Unfortunately some archivers (including GNU ar) will put
5879 declarations of common symbols into their archive maps, as
5880 well as real definitions, so we cannot just go by the archive
5881 map alone. Instead we must read in the element's symbol
5882 table and check that to see what kind of symbol definition
5883 this is. */
5884 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5885 continue;
5886 }
75cfe082 5887 else
0ad989f9
L
5888 {
5889 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5890 /* Symbol must be defined. Don't check it again. */
5891 included[i] = TRUE;
0ad989f9
L
5892 continue;
5893 }
5894
5895 /* We need to include this archive member. */
5896 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5897 if (element == NULL)
5898 goto error_return;
5899
5900 if (! bfd_check_format (element, bfd_object))
5901 goto error_return;
5902
0ad989f9
L
5903 undefs_tail = info->hash->undefs_tail;
5904
0e144ba7
AM
5905 if (!(*info->callbacks
5906 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5907 continue;
0e144ba7 5908 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5909 goto error_return;
5910
5911 /* If there are any new undefined symbols, we need to make
5912 another pass through the archive in order to see whether
5913 they can be defined. FIXME: This isn't perfect, because
5914 common symbols wind up on undefs_tail and because an
5915 undefined symbol which is defined later on in this pass
5916 does not require another pass. This isn't a bug, but it
5917 does make the code less efficient than it could be. */
5918 if (undefs_tail != info->hash->undefs_tail)
5919 loop = TRUE;
5920
5921 /* Look backward to mark all symbols from this object file
5922 which we have already seen in this pass. */
5923 mark = i;
5924 do
5925 {
5926 included[mark] = TRUE;
5927 if (mark == 0)
5928 break;
5929 --mark;
5930 }
5931 while (symdefs[mark].file_offset == symdef->file_offset);
5932
5933 /* We mark subsequent symbols from this object file as we go
5934 on through the loop. */
5935 last = symdef->file_offset;
5936 }
5937 }
5938 while (loop);
5939
0ad989f9 5940 free (included);
0ad989f9
L
5941 return TRUE;
5942
5943 error_return:
c9594989 5944 free (included);
0ad989f9
L
5945 return FALSE;
5946}
4ad4eba5
AM
5947
5948/* Given an ELF BFD, add symbols to the global hash table as
5949 appropriate. */
5950
5951bfd_boolean
5952bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5953{
5954 switch (bfd_get_format (abfd))
5955 {
5956 case bfd_object:
5957 return elf_link_add_object_symbols (abfd, info);
5958 case bfd_archive:
5959 return elf_link_add_archive_symbols (abfd, info);
5960 default:
5961 bfd_set_error (bfd_error_wrong_format);
5962 return FALSE;
5963 }
5964}
5a580b3a 5965\f
14b1c01e
AM
5966struct hash_codes_info
5967{
5968 unsigned long *hashcodes;
5969 bfd_boolean error;
5970};
a0c8462f 5971
5a580b3a
AM
5972/* This function will be called though elf_link_hash_traverse to store
5973 all hash value of the exported symbols in an array. */
5974
5975static bfd_boolean
5976elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5977{
a50b1753 5978 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5979 const char *name;
5a580b3a
AM
5980 unsigned long ha;
5981 char *alc = NULL;
5982
5a580b3a
AM
5983 /* Ignore indirect symbols. These are added by the versioning code. */
5984 if (h->dynindx == -1)
5985 return TRUE;
5986
5987 name = h->root.root.string;
422f1182 5988 if (h->versioned >= versioned)
5a580b3a 5989 {
422f1182
L
5990 char *p = strchr (name, ELF_VER_CHR);
5991 if (p != NULL)
14b1c01e 5992 {
422f1182
L
5993 alc = (char *) bfd_malloc (p - name + 1);
5994 if (alc == NULL)
5995 {
5996 inf->error = TRUE;
5997 return FALSE;
5998 }
5999 memcpy (alc, name, p - name);
6000 alc[p - name] = '\0';
6001 name = alc;
14b1c01e 6002 }
5a580b3a
AM
6003 }
6004
6005 /* Compute the hash value. */
6006 ha = bfd_elf_hash (name);
6007
6008 /* Store the found hash value in the array given as the argument. */
14b1c01e 6009 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6010
6011 /* And store it in the struct so that we can put it in the hash table
6012 later. */
f6e332e6 6013 h->u.elf_hash_value = ha;
5a580b3a 6014
c9594989 6015 free (alc);
5a580b3a
AM
6016 return TRUE;
6017}
6018
fdc90cb4
JJ
6019struct collect_gnu_hash_codes
6020{
6021 bfd *output_bfd;
6022 const struct elf_backend_data *bed;
6023 unsigned long int nsyms;
6024 unsigned long int maskbits;
6025 unsigned long int *hashcodes;
6026 unsigned long int *hashval;
6027 unsigned long int *indx;
6028 unsigned long int *counts;
6029 bfd_vma *bitmask;
6030 bfd_byte *contents;
f16a9783 6031 bfd_size_type xlat;
fdc90cb4
JJ
6032 long int min_dynindx;
6033 unsigned long int bucketcount;
6034 unsigned long int symindx;
6035 long int local_indx;
6036 long int shift1, shift2;
6037 unsigned long int mask;
14b1c01e 6038 bfd_boolean error;
fdc90cb4
JJ
6039};
6040
6041/* This function will be called though elf_link_hash_traverse to store
6042 all hash value of the exported symbols in an array. */
6043
6044static bfd_boolean
6045elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6046{
a50b1753 6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6048 const char *name;
fdc90cb4
JJ
6049 unsigned long ha;
6050 char *alc = NULL;
6051
fdc90cb4
JJ
6052 /* Ignore indirect symbols. These are added by the versioning code. */
6053 if (h->dynindx == -1)
6054 return TRUE;
6055
6056 /* Ignore also local symbols and undefined symbols. */
6057 if (! (*s->bed->elf_hash_symbol) (h))
6058 return TRUE;
6059
6060 name = h->root.root.string;
422f1182 6061 if (h->versioned >= versioned)
fdc90cb4 6062 {
422f1182
L
6063 char *p = strchr (name, ELF_VER_CHR);
6064 if (p != NULL)
14b1c01e 6065 {
422f1182
L
6066 alc = (char *) bfd_malloc (p - name + 1);
6067 if (alc == NULL)
6068 {
6069 s->error = TRUE;
6070 return FALSE;
6071 }
6072 memcpy (alc, name, p - name);
6073 alc[p - name] = '\0';
6074 name = alc;
14b1c01e 6075 }
fdc90cb4
JJ
6076 }
6077
6078 /* Compute the hash value. */
6079 ha = bfd_elf_gnu_hash (name);
6080
6081 /* Store the found hash value in the array for compute_bucket_count,
6082 and also for .dynsym reordering purposes. */
6083 s->hashcodes[s->nsyms] = ha;
6084 s->hashval[h->dynindx] = ha;
6085 ++s->nsyms;
6086 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6087 s->min_dynindx = h->dynindx;
6088
c9594989 6089 free (alc);
fdc90cb4
JJ
6090 return TRUE;
6091}
6092
6093/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6094 final dynamic symbol renumbering in case of .gnu.hash.
6095 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6096 to the translation table. */
fdc90cb4
JJ
6097
6098static bfd_boolean
f16a9783 6099elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6100{
a50b1753 6101 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6102 unsigned long int bucket;
6103 unsigned long int val;
6104
fdc90cb4
JJ
6105 /* Ignore indirect symbols. */
6106 if (h->dynindx == -1)
6107 return TRUE;
6108
6109 /* Ignore also local symbols and undefined symbols. */
6110 if (! (*s->bed->elf_hash_symbol) (h))
6111 {
6112 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6113 {
6114 if (s->bed->record_xhash_symbol != NULL)
6115 {
6116 (*s->bed->record_xhash_symbol) (h, 0);
6117 s->local_indx++;
6118 }
6119 else
6120 h->dynindx = s->local_indx++;
6121 }
fdc90cb4
JJ
6122 return TRUE;
6123 }
6124
6125 bucket = s->hashval[h->dynindx] % s->bucketcount;
6126 val = (s->hashval[h->dynindx] >> s->shift1)
6127 & ((s->maskbits >> s->shift1) - 1);
6128 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6129 s->bitmask[val]
6130 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6131 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6132 if (s->counts[bucket] == 1)
6133 /* Last element terminates the chain. */
6134 val |= 1;
6135 bfd_put_32 (s->output_bfd, val,
6136 s->contents + (s->indx[bucket] - s->symindx) * 4);
6137 --s->counts[bucket];
f16a9783
MS
6138 if (s->bed->record_xhash_symbol != NULL)
6139 {
6140 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6141
6142 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6143 }
6144 else
6145 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6146 return TRUE;
6147}
6148
6149/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6150
6151bfd_boolean
6152_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6153{
6154 return !(h->forced_local
6155 || h->root.type == bfd_link_hash_undefined
6156 || h->root.type == bfd_link_hash_undefweak
6157 || ((h->root.type == bfd_link_hash_defined
6158 || h->root.type == bfd_link_hash_defweak)
6159 && h->root.u.def.section->output_section == NULL));
6160}
6161
5a580b3a
AM
6162/* Array used to determine the number of hash table buckets to use
6163 based on the number of symbols there are. If there are fewer than
6164 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6165 fewer than 37 we use 17 buckets, and so forth. We never use more
6166 than 32771 buckets. */
6167
6168static const size_t elf_buckets[] =
6169{
6170 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6171 16411, 32771, 0
6172};
6173
6174/* Compute bucket count for hashing table. We do not use a static set
6175 of possible tables sizes anymore. Instead we determine for all
6176 possible reasonable sizes of the table the outcome (i.e., the
6177 number of collisions etc) and choose the best solution. The
6178 weighting functions are not too simple to allow the table to grow
6179 without bounds. Instead one of the weighting factors is the size.
6180 Therefore the result is always a good payoff between few collisions
6181 (= short chain lengths) and table size. */
6182static size_t
b20dd2ce 6183compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6184 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6185 unsigned long int nsyms,
6186 int gnu_hash)
5a580b3a 6187{
5a580b3a 6188 size_t best_size = 0;
5a580b3a 6189 unsigned long int i;
5a580b3a 6190
5a580b3a
AM
6191 /* We have a problem here. The following code to optimize the table
6192 size requires an integer type with more the 32 bits. If
6193 BFD_HOST_U_64_BIT is set we know about such a type. */
6194#ifdef BFD_HOST_U_64_BIT
6195 if (info->optimize)
6196 {
5a580b3a
AM
6197 size_t minsize;
6198 size_t maxsize;
6199 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6200 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6201 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6202 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6203 unsigned long int *counts;
d40f3da9 6204 bfd_size_type amt;
0883b6e0 6205 unsigned int no_improvement_count = 0;
5a580b3a
AM
6206
6207 /* Possible optimization parameters: if we have NSYMS symbols we say
6208 that the hashing table must at least have NSYMS/4 and at most
6209 2*NSYMS buckets. */
6210 minsize = nsyms / 4;
6211 if (minsize == 0)
6212 minsize = 1;
6213 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6214 if (gnu_hash)
6215 {
6216 if (minsize < 2)
6217 minsize = 2;
6218 if ((best_size & 31) == 0)
6219 ++best_size;
6220 }
5a580b3a
AM
6221
6222 /* Create array where we count the collisions in. We must use bfd_malloc
6223 since the size could be large. */
6224 amt = maxsize;
6225 amt *= sizeof (unsigned long int);
a50b1753 6226 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6227 if (counts == NULL)
fdc90cb4 6228 return 0;
5a580b3a
AM
6229
6230 /* Compute the "optimal" size for the hash table. The criteria is a
6231 minimal chain length. The minor criteria is (of course) the size
6232 of the table. */
6233 for (i = minsize; i < maxsize; ++i)
6234 {
6235 /* Walk through the array of hashcodes and count the collisions. */
6236 BFD_HOST_U_64_BIT max;
6237 unsigned long int j;
6238 unsigned long int fact;
6239
fdc90cb4
JJ
6240 if (gnu_hash && (i & 31) == 0)
6241 continue;
6242
5a580b3a
AM
6243 memset (counts, '\0', i * sizeof (unsigned long int));
6244
6245 /* Determine how often each hash bucket is used. */
6246 for (j = 0; j < nsyms; ++j)
6247 ++counts[hashcodes[j] % i];
6248
6249 /* For the weight function we need some information about the
6250 pagesize on the target. This is information need not be 100%
6251 accurate. Since this information is not available (so far) we
6252 define it here to a reasonable default value. If it is crucial
6253 to have a better value some day simply define this value. */
6254# ifndef BFD_TARGET_PAGESIZE
6255# define BFD_TARGET_PAGESIZE (4096)
6256# endif
6257
fdc90cb4
JJ
6258 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6259 and the chains. */
6260 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6261
6262# if 1
6263 /* Variant 1: optimize for short chains. We add the squares
6264 of all the chain lengths (which favors many small chain
6265 over a few long chains). */
6266 for (j = 0; j < i; ++j)
6267 max += counts[j] * counts[j];
6268
6269 /* This adds penalties for the overall size of the table. */
fdc90cb4 6270 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6271 max *= fact * fact;
6272# else
6273 /* Variant 2: Optimize a lot more for small table. Here we
6274 also add squares of the size but we also add penalties for
6275 empty slots (the +1 term). */
6276 for (j = 0; j < i; ++j)
6277 max += (1 + counts[j]) * (1 + counts[j]);
6278
6279 /* The overall size of the table is considered, but not as
6280 strong as in variant 1, where it is squared. */
fdc90cb4 6281 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6282 max *= fact;
6283# endif
6284
6285 /* Compare with current best results. */
6286 if (max < best_chlen)
6287 {
6288 best_chlen = max;
6289 best_size = i;
ca4be51c 6290 no_improvement_count = 0;
5a580b3a 6291 }
0883b6e0
NC
6292 /* PR 11843: Avoid futile long searches for the best bucket size
6293 when there are a large number of symbols. */
6294 else if (++no_improvement_count == 100)
6295 break;
5a580b3a
AM
6296 }
6297
6298 free (counts);
6299 }
6300 else
6301#endif /* defined (BFD_HOST_U_64_BIT) */
6302 {
6303 /* This is the fallback solution if no 64bit type is available or if we
6304 are not supposed to spend much time on optimizations. We select the
6305 bucket count using a fixed set of numbers. */
6306 for (i = 0; elf_buckets[i] != 0; i++)
6307 {
6308 best_size = elf_buckets[i];
fdc90cb4 6309 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6310 break;
6311 }
fdc90cb4
JJ
6312 if (gnu_hash && best_size < 2)
6313 best_size = 2;
5a580b3a
AM
6314 }
6315
5a580b3a
AM
6316 return best_size;
6317}
6318
d0bf826b
AM
6319/* Size any SHT_GROUP section for ld -r. */
6320
6321bfd_boolean
6322_bfd_elf_size_group_sections (struct bfd_link_info *info)
6323{
6324 bfd *ibfd;
57963c05 6325 asection *s;
d0bf826b 6326
c72f2fb2 6327 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6328 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6329 && (s = ibfd->sections) != NULL
6330 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6331 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6332 return FALSE;
6333 return TRUE;
6334}
6335
04c3a755
NS
6336/* Set a default stack segment size. The value in INFO wins. If it
6337 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6338 undefined it is initialized. */
6339
6340bfd_boolean
6341bfd_elf_stack_segment_size (bfd *output_bfd,
6342 struct bfd_link_info *info,
6343 const char *legacy_symbol,
6344 bfd_vma default_size)
6345{
6346 struct elf_link_hash_entry *h = NULL;
6347
6348 /* Look for legacy symbol. */
6349 if (legacy_symbol)
6350 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6351 FALSE, FALSE, FALSE);
6352 if (h && (h->root.type == bfd_link_hash_defined
6353 || h->root.type == bfd_link_hash_defweak)
6354 && h->def_regular
6355 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6356 {
6357 /* The symbol has no type if specified on the command line. */
6358 h->type = STT_OBJECT;
6359 if (info->stacksize)
695344c0 6360 /* xgettext:c-format */
871b3ab2 6361 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6362 output_bfd, legacy_symbol);
04c3a755 6363 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6364 /* xgettext:c-format */
871b3ab2 6365 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6366 output_bfd, legacy_symbol);
04c3a755
NS
6367 else
6368 info->stacksize = h->root.u.def.value;
6369 }
6370
6371 if (!info->stacksize)
6372 /* If the user didn't set a size, or explicitly inhibit the
6373 size, set it now. */
6374 info->stacksize = default_size;
6375
6376 /* Provide the legacy symbol, if it is referenced. */
6377 if (h && (h->root.type == bfd_link_hash_undefined
6378 || h->root.type == bfd_link_hash_undefweak))
6379 {
6380 struct bfd_link_hash_entry *bh = NULL;
6381
6382 if (!(_bfd_generic_link_add_one_symbol
6383 (info, output_bfd, legacy_symbol,
6384 BSF_GLOBAL, bfd_abs_section_ptr,
6385 info->stacksize >= 0 ? info->stacksize : 0,
6386 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6387 return FALSE;
6388
6389 h = (struct elf_link_hash_entry *) bh;
6390 h->def_regular = 1;
6391 h->type = STT_OBJECT;
6392 }
6393
6394 return TRUE;
6395}
6396
b531344c
MR
6397/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6398
6399struct elf_gc_sweep_symbol_info
6400{
6401 struct bfd_link_info *info;
6402 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6403 bfd_boolean);
6404};
6405
6406static bfd_boolean
6407elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6408{
6409 if (!h->mark
6410 && (((h->root.type == bfd_link_hash_defined
6411 || h->root.type == bfd_link_hash_defweak)
6412 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6413 && h->root.u.def.section->gc_mark))
6414 || h->root.type == bfd_link_hash_undefined
6415 || h->root.type == bfd_link_hash_undefweak))
6416 {
6417 struct elf_gc_sweep_symbol_info *inf;
6418
6419 inf = (struct elf_gc_sweep_symbol_info *) data;
6420 (*inf->hide_symbol) (inf->info, h, TRUE);
6421 h->def_regular = 0;
6422 h->ref_regular = 0;
6423 h->ref_regular_nonweak = 0;
6424 }
6425
6426 return TRUE;
6427}
6428
5a580b3a
AM
6429/* Set up the sizes and contents of the ELF dynamic sections. This is
6430 called by the ELF linker emulation before_allocation routine. We
6431 must set the sizes of the sections before the linker sets the
6432 addresses of the various sections. */
6433
6434bfd_boolean
6435bfd_elf_size_dynamic_sections (bfd *output_bfd,
6436 const char *soname,
6437 const char *rpath,
6438 const char *filter_shlib,
7ee314fa
AM
6439 const char *audit,
6440 const char *depaudit,
5a580b3a
AM
6441 const char * const *auxiliary_filters,
6442 struct bfd_link_info *info,
fd91d419 6443 asection **sinterpptr)
5a580b3a 6444{
5a580b3a
AM
6445 bfd *dynobj;
6446 const struct elf_backend_data *bed;
5a580b3a
AM
6447
6448 *sinterpptr = NULL;
6449
5a580b3a
AM
6450 if (!is_elf_hash_table (info->hash))
6451 return TRUE;
6452
5a580b3a
AM
6453 dynobj = elf_hash_table (info)->dynobj;
6454
9a2a56cc 6455 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6456 {
902e9fc7
MR
6457 struct bfd_elf_version_tree *verdefs;
6458 struct elf_info_failed asvinfo;
5a580b3a
AM
6459 struct bfd_elf_version_tree *t;
6460 struct bfd_elf_version_expr *d;
902e9fc7 6461 asection *s;
e6699019 6462 size_t soname_indx;
7ee314fa 6463
5a580b3a
AM
6464 /* If we are supposed to export all symbols into the dynamic symbol
6465 table (this is not the normal case), then do so. */
55255dae 6466 if (info->export_dynamic
0e1862bb 6467 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6468 {
3d13f3e9
AM
6469 struct elf_info_failed eif;
6470
6471 eif.info = info;
6472 eif.failed = FALSE;
5a580b3a
AM
6473 elf_link_hash_traverse (elf_hash_table (info),
6474 _bfd_elf_export_symbol,
6475 &eif);
6476 if (eif.failed)
6477 return FALSE;
6478 }
6479
e6699019
L
6480 if (soname != NULL)
6481 {
6482 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6483 soname, TRUE);
6484 if (soname_indx == (size_t) -1
6485 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6486 return FALSE;
6487 }
6488 else
6489 soname_indx = (size_t) -1;
6490
5a580b3a 6491 /* Make all global versions with definition. */
fd91d419 6492 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6493 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6494 if (!d->symver && d->literal)
5a580b3a
AM
6495 {
6496 const char *verstr, *name;
6497 size_t namelen, verlen, newlen;
93252b1c 6498 char *newname, *p, leading_char;
5a580b3a
AM
6499 struct elf_link_hash_entry *newh;
6500
93252b1c 6501 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6502 name = d->pattern;
93252b1c 6503 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6504 verstr = t->name;
6505 verlen = strlen (verstr);
6506 newlen = namelen + verlen + 3;
6507
a50b1753 6508 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6509 if (newname == NULL)
6510 return FALSE;
93252b1c
MF
6511 newname[0] = leading_char;
6512 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6513
6514 /* Check the hidden versioned definition. */
6515 p = newname + namelen;
6516 *p++ = ELF_VER_CHR;
6517 memcpy (p, verstr, verlen + 1);
6518 newh = elf_link_hash_lookup (elf_hash_table (info),
6519 newname, FALSE, FALSE,
6520 FALSE);
6521 if (newh == NULL
6522 || (newh->root.type != bfd_link_hash_defined
6523 && newh->root.type != bfd_link_hash_defweak))
6524 {
6525 /* Check the default versioned definition. */
6526 *p++ = ELF_VER_CHR;
6527 memcpy (p, verstr, verlen + 1);
6528 newh = elf_link_hash_lookup (elf_hash_table (info),
6529 newname, FALSE, FALSE,
6530 FALSE);
6531 }
6532 free (newname);
6533
6534 /* Mark this version if there is a definition and it is
6535 not defined in a shared object. */
6536 if (newh != NULL
f5385ebf 6537 && !newh->def_dynamic
5a580b3a
AM
6538 && (newh->root.type == bfd_link_hash_defined
6539 || newh->root.type == bfd_link_hash_defweak))
6540 d->symver = 1;
6541 }
6542
6543 /* Attach all the symbols to their version information. */
5a580b3a 6544 asvinfo.info = info;
5a580b3a
AM
6545 asvinfo.failed = FALSE;
6546
6547 elf_link_hash_traverse (elf_hash_table (info),
6548 _bfd_elf_link_assign_sym_version,
6549 &asvinfo);
6550 if (asvinfo.failed)
6551 return FALSE;
6552
6553 if (!info->allow_undefined_version)
6554 {
6555 /* Check if all global versions have a definition. */
3d13f3e9 6556 bfd_boolean all_defined = TRUE;
fd91d419 6557 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6558 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6559 if (d->literal && !d->symver && !d->script)
5a580b3a 6560 {
4eca0228 6561 _bfd_error_handler
5a580b3a
AM
6562 (_("%s: undefined version: %s"),
6563 d->pattern, t->name);
6564 all_defined = FALSE;
6565 }
6566
6567 if (!all_defined)
6568 {
6569 bfd_set_error (bfd_error_bad_value);
6570 return FALSE;
6571 }
6572 }
6573
902e9fc7
MR
6574 /* Set up the version definition section. */
6575 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6576 BFD_ASSERT (s != NULL);
5a580b3a 6577
902e9fc7
MR
6578 /* We may have created additional version definitions if we are
6579 just linking a regular application. */
6580 verdefs = info->version_info;
5a580b3a 6581
902e9fc7
MR
6582 /* Skip anonymous version tag. */
6583 if (verdefs != NULL && verdefs->vernum == 0)
6584 verdefs = verdefs->next;
5a580b3a 6585
902e9fc7
MR
6586 if (verdefs == NULL && !info->create_default_symver)
6587 s->flags |= SEC_EXCLUDE;
6588 else
5a580b3a 6589 {
902e9fc7
MR
6590 unsigned int cdefs;
6591 bfd_size_type size;
6592 bfd_byte *p;
6593 Elf_Internal_Verdef def;
6594 Elf_Internal_Verdaux defaux;
6595 struct bfd_link_hash_entry *bh;
6596 struct elf_link_hash_entry *h;
6597 const char *name;
5a580b3a 6598
902e9fc7
MR
6599 cdefs = 0;
6600 size = 0;
5a580b3a 6601
902e9fc7
MR
6602 /* Make space for the base version. */
6603 size += sizeof (Elf_External_Verdef);
6604 size += sizeof (Elf_External_Verdaux);
6605 ++cdefs;
6606
6607 /* Make space for the default version. */
6608 if (info->create_default_symver)
6609 {
6610 size += sizeof (Elf_External_Verdef);
6611 ++cdefs;
3e3b46e5
PB
6612 }
6613
5a580b3a
AM
6614 for (t = verdefs; t != NULL; t = t->next)
6615 {
6616 struct bfd_elf_version_deps *n;
6617
a6cc6b3b
RO
6618 /* Don't emit base version twice. */
6619 if (t->vernum == 0)
6620 continue;
6621
5a580b3a
AM
6622 size += sizeof (Elf_External_Verdef);
6623 size += sizeof (Elf_External_Verdaux);
6624 ++cdefs;
6625
6626 for (n = t->deps; n != NULL; n = n->next)
6627 size += sizeof (Elf_External_Verdaux);
6628 }
6629
eea6121a 6630 s->size = size;
a50b1753 6631 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6632 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6633 return FALSE;
6634
6635 /* Fill in the version definition section. */
6636
6637 p = s->contents;
6638
6639 def.vd_version = VER_DEF_CURRENT;
6640 def.vd_flags = VER_FLG_BASE;
6641 def.vd_ndx = 1;
6642 def.vd_cnt = 1;
3e3b46e5
PB
6643 if (info->create_default_symver)
6644 {
6645 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6646 def.vd_next = sizeof (Elf_External_Verdef);
6647 }
6648 else
6649 {
6650 def.vd_aux = sizeof (Elf_External_Verdef);
6651 def.vd_next = (sizeof (Elf_External_Verdef)
6652 + sizeof (Elf_External_Verdaux));
6653 }
5a580b3a 6654
ef53be89 6655 if (soname_indx != (size_t) -1)
5a580b3a
AM
6656 {
6657 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6658 soname_indx);
6659 def.vd_hash = bfd_elf_hash (soname);
6660 defaux.vda_name = soname_indx;
3e3b46e5 6661 name = soname;
5a580b3a
AM
6662 }
6663 else
6664 {
ef53be89 6665 size_t indx;
5a580b3a 6666
765cf5f6 6667 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6668 def.vd_hash = bfd_elf_hash (name);
6669 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6670 name, FALSE);
ef53be89 6671 if (indx == (size_t) -1)
5a580b3a
AM
6672 return FALSE;
6673 defaux.vda_name = indx;
6674 }
6675 defaux.vda_next = 0;
6676
6677 _bfd_elf_swap_verdef_out (output_bfd, &def,
6678 (Elf_External_Verdef *) p);
6679 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6680 if (info->create_default_symver)
6681 {
6682 /* Add a symbol representing this version. */
6683 bh = NULL;
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6686 0, NULL, FALSE,
6687 get_elf_backend_data (dynobj)->collect, &bh)))
6688 return FALSE;
6689 h = (struct elf_link_hash_entry *) bh;
6690 h->non_elf = 0;
6691 h->def_regular = 1;
6692 h->type = STT_OBJECT;
6693 h->verinfo.vertree = NULL;
6694
6695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6696 return FALSE;
6697
6698 /* Create a duplicate of the base version with the same
6699 aux block, but different flags. */
6700 def.vd_flags = 0;
6701 def.vd_ndx = 2;
6702 def.vd_aux = sizeof (Elf_External_Verdef);
6703 if (verdefs)
6704 def.vd_next = (sizeof (Elf_External_Verdef)
6705 + sizeof (Elf_External_Verdaux));
6706 else
6707 def.vd_next = 0;
6708 _bfd_elf_swap_verdef_out (output_bfd, &def,
6709 (Elf_External_Verdef *) p);
6710 p += sizeof (Elf_External_Verdef);
6711 }
5a580b3a
AM
6712 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6713 (Elf_External_Verdaux *) p);
6714 p += sizeof (Elf_External_Verdaux);
6715
6716 for (t = verdefs; t != NULL; t = t->next)
6717 {
6718 unsigned int cdeps;
6719 struct bfd_elf_version_deps *n;
5a580b3a 6720
a6cc6b3b
RO
6721 /* Don't emit the base version twice. */
6722 if (t->vernum == 0)
6723 continue;
6724
5a580b3a
AM
6725 cdeps = 0;
6726 for (n = t->deps; n != NULL; n = n->next)
6727 ++cdeps;
6728
6729 /* Add a symbol representing this version. */
6730 bh = NULL;
6731 if (! (_bfd_generic_link_add_one_symbol
6732 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6733 0, NULL, FALSE,
6734 get_elf_backend_data (dynobj)->collect, &bh)))
6735 return FALSE;
6736 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6737 h->non_elf = 0;
6738 h->def_regular = 1;
5a580b3a
AM
6739 h->type = STT_OBJECT;
6740 h->verinfo.vertree = t;
6741
c152c796 6742 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6743 return FALSE;
6744
6745 def.vd_version = VER_DEF_CURRENT;
6746 def.vd_flags = 0;
6747 if (t->globals.list == NULL
6748 && t->locals.list == NULL
6749 && ! t->used)
6750 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6751 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6752 def.vd_cnt = cdeps + 1;
6753 def.vd_hash = bfd_elf_hash (t->name);
6754 def.vd_aux = sizeof (Elf_External_Verdef);
6755 def.vd_next = 0;
a6cc6b3b
RO
6756
6757 /* If a basever node is next, it *must* be the last node in
6758 the chain, otherwise Verdef construction breaks. */
6759 if (t->next != NULL && t->next->vernum == 0)
6760 BFD_ASSERT (t->next->next == NULL);
6761
6762 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6763 def.vd_next = (sizeof (Elf_External_Verdef)
6764 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6765
6766 _bfd_elf_swap_verdef_out (output_bfd, &def,
6767 (Elf_External_Verdef *) p);
6768 p += sizeof (Elf_External_Verdef);
6769
6770 defaux.vda_name = h->dynstr_index;
6771 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6772 h->dynstr_index);
6773 defaux.vda_next = 0;
6774 if (t->deps != NULL)
6775 defaux.vda_next = sizeof (Elf_External_Verdaux);
6776 t->name_indx = defaux.vda_name;
6777
6778 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6779 (Elf_External_Verdaux *) p);
6780 p += sizeof (Elf_External_Verdaux);
6781
6782 for (n = t->deps; n != NULL; n = n->next)
6783 {
6784 if (n->version_needed == NULL)
6785 {
6786 /* This can happen if there was an error in the
6787 version script. */
6788 defaux.vda_name = 0;
6789 }
6790 else
6791 {
6792 defaux.vda_name = n->version_needed->name_indx;
6793 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6794 defaux.vda_name);
6795 }
6796 if (n->next == NULL)
6797 defaux.vda_next = 0;
6798 else
6799 defaux.vda_next = sizeof (Elf_External_Verdaux);
6800
6801 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6802 (Elf_External_Verdaux *) p);
6803 p += sizeof (Elf_External_Verdaux);
6804 }
6805 }
6806
5a580b3a
AM
6807 elf_tdata (output_bfd)->cverdefs = cdefs;
6808 }
902e9fc7
MR
6809 }
6810
6811 bed = get_elf_backend_data (output_bfd);
6812
6813 if (info->gc_sections && bed->can_gc_sections)
6814 {
6815 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6816
6817 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6818 dynamic symbol table. */
902e9fc7
MR
6819 sweep_info.info = info;
6820 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6821 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6822 &sweep_info);
3d13f3e9
AM
6823 }
6824
6825 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6826 {
6827 asection *s;
6828 struct elf_find_verdep_info sinfo;
6829
6830 /* Work out the size of the version reference section. */
6831
6832 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6833 BFD_ASSERT (s != NULL);
902e9fc7 6834
3d13f3e9
AM
6835 sinfo.info = info;
6836 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6837 if (sinfo.vers == 0)
6838 sinfo.vers = 1;
6839 sinfo.failed = FALSE;
6840
6841 elf_link_hash_traverse (elf_hash_table (info),
6842 _bfd_elf_link_find_version_dependencies,
6843 &sinfo);
6844 if (sinfo.failed)
6845 return FALSE;
6846
6847 if (elf_tdata (output_bfd)->verref == NULL)
6848 s->flags |= SEC_EXCLUDE;
6849 else
6850 {
6851 Elf_Internal_Verneed *vn;
6852 unsigned int size;
6853 unsigned int crefs;
6854 bfd_byte *p;
6855
6856 /* Build the version dependency section. */
6857 size = 0;
6858 crefs = 0;
6859 for (vn = elf_tdata (output_bfd)->verref;
6860 vn != NULL;
6861 vn = vn->vn_nextref)
6862 {
6863 Elf_Internal_Vernaux *a;
6864
6865 size += sizeof (Elf_External_Verneed);
6866 ++crefs;
6867 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6868 size += sizeof (Elf_External_Vernaux);
6869 }
6870
6871 s->size = size;
6872 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6873 if (s->contents == NULL)
6874 return FALSE;
6875
6876 p = s->contents;
6877 for (vn = elf_tdata (output_bfd)->verref;
6878 vn != NULL;
6879 vn = vn->vn_nextref)
6880 {
6881 unsigned int caux;
6882 Elf_Internal_Vernaux *a;
6883 size_t indx;
6884
6885 caux = 0;
6886 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6887 ++caux;
6888
6889 vn->vn_version = VER_NEED_CURRENT;
6890 vn->vn_cnt = caux;
6891 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6892 elf_dt_name (vn->vn_bfd) != NULL
6893 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6894 : lbasename (bfd_get_filename
6895 (vn->vn_bfd)),
3d13f3e9
AM
6896 FALSE);
6897 if (indx == (size_t) -1)
6898 return FALSE;
6899 vn->vn_file = indx;
6900 vn->vn_aux = sizeof (Elf_External_Verneed);
6901 if (vn->vn_nextref == NULL)
6902 vn->vn_next = 0;
6903 else
6904 vn->vn_next = (sizeof (Elf_External_Verneed)
6905 + caux * sizeof (Elf_External_Vernaux));
6906
6907 _bfd_elf_swap_verneed_out (output_bfd, vn,
6908 (Elf_External_Verneed *) p);
6909 p += sizeof (Elf_External_Verneed);
6910
6911 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6912 {
6913 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6914 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6915 a->vna_nodename, FALSE);
6916 if (indx == (size_t) -1)
6917 return FALSE;
6918 a->vna_name = indx;
6919 if (a->vna_nextptr == NULL)
6920 a->vna_next = 0;
6921 else
6922 a->vna_next = sizeof (Elf_External_Vernaux);
6923
6924 _bfd_elf_swap_vernaux_out (output_bfd, a,
6925 (Elf_External_Vernaux *) p);
6926 p += sizeof (Elf_External_Vernaux);
6927 }
6928 }
6929
6930 elf_tdata (output_bfd)->cverrefs = crefs;
6931 }
902e9fc7
MR
6932 }
6933
6934 /* Any syms created from now on start with -1 in
6935 got.refcount/offset and plt.refcount/offset. */
6936 elf_hash_table (info)->init_got_refcount
6937 = elf_hash_table (info)->init_got_offset;
6938 elf_hash_table (info)->init_plt_refcount
6939 = elf_hash_table (info)->init_plt_offset;
6940
6941 if (bfd_link_relocatable (info)
6942 && !_bfd_elf_size_group_sections (info))
6943 return FALSE;
6944
6945 /* The backend may have to create some sections regardless of whether
6946 we're dynamic or not. */
6947 if (bed->elf_backend_always_size_sections
6948 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6949 return FALSE;
6950
6951 /* Determine any GNU_STACK segment requirements, after the backend
6952 has had a chance to set a default segment size. */
6953 if (info->execstack)
6954 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6955 else if (info->noexecstack)
6956 elf_stack_flags (output_bfd) = PF_R | PF_W;
6957 else
6958 {
6959 bfd *inputobj;
6960 asection *notesec = NULL;
6961 int exec = 0;
6962
6963 for (inputobj = info->input_bfds;
6964 inputobj;
6965 inputobj = inputobj->link.next)
6966 {
6967 asection *s;
6968
6969 if (inputobj->flags
6970 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6971 continue;
57963c05
AM
6972 s = inputobj->sections;
6973 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6974 continue;
6975
902e9fc7
MR
6976 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6977 if (s)
6978 {
6979 if (s->flags & SEC_CODE)
6980 exec = PF_X;
6981 notesec = s;
6982 }
6983 else if (bed->default_execstack)
6984 exec = PF_X;
6985 }
6986 if (notesec || info->stacksize > 0)
6987 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6988 if (notesec && exec && bfd_link_relocatable (info)
6989 && notesec->output_section != bfd_abs_section_ptr)
6990 notesec->output_section->flags |= SEC_CODE;
6991 }
6992
6993 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6994 {
6995 struct elf_info_failed eif;
6996 struct elf_link_hash_entry *h;
6997 asection *dynstr;
6998 asection *s;
6999
7000 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7001 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7002
902e9fc7
MR
7003 if (info->symbolic)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7006 return FALSE;
7007 info->flags |= DF_SYMBOLIC;
7008 }
7009
7010 if (rpath != NULL)
7011 {
7012 size_t indx;
7013 bfd_vma tag;
7014
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7016 TRUE);
7017 if (indx == (size_t) -1)
7018 return FALSE;
7019
7020 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7021 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7022 return FALSE;
7023 }
7024
7025 if (filter_shlib != NULL)
7026 {
7027 size_t indx;
7028
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7030 filter_shlib, TRUE);
7031 if (indx == (size_t) -1
7032 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7033 return FALSE;
7034 }
7035
7036 if (auxiliary_filters != NULL)
7037 {
7038 const char * const *p;
7039
7040 for (p = auxiliary_filters; *p != NULL; p++)
7041 {
7042 size_t indx;
7043
7044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7045 *p, TRUE);
7046 if (indx == (size_t) -1
7047 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7048 return FALSE;
7049 }
7050 }
7051
7052 if (audit != NULL)
7053 {
7054 size_t indx;
7055
7056 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7057 TRUE);
7058 if (indx == (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7060 return FALSE;
7061 }
7062
7063 if (depaudit != NULL)
7064 {
7065 size_t indx;
7066
7067 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7068 TRUE);
7069 if (indx == (size_t) -1
7070 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7071 return FALSE;
7072 }
7073
7074 eif.info = info;
7075 eif.failed = FALSE;
7076
7077 /* Find all symbols which were defined in a dynamic object and make
7078 the backend pick a reasonable value for them. */
7079 elf_link_hash_traverse (elf_hash_table (info),
7080 _bfd_elf_adjust_dynamic_symbol,
7081 &eif);
7082 if (eif.failed)
7083 return FALSE;
7084
7085 /* Add some entries to the .dynamic section. We fill in some of the
7086 values later, in bfd_elf_final_link, but we must add the entries
7087 now so that we know the final size of the .dynamic section. */
7088
7089 /* If there are initialization and/or finalization functions to
7090 call then add the corresponding DT_INIT/DT_FINI entries. */
7091 h = (info->init_function
7092 ? elf_link_hash_lookup (elf_hash_table (info),
7093 info->init_function, FALSE,
7094 FALSE, FALSE)
7095 : NULL);
7096 if (h != NULL
7097 && (h->ref_regular
7098 || h->def_regular))
7099 {
7100 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7101 return FALSE;
7102 }
7103 h = (info->fini_function
7104 ? elf_link_hash_lookup (elf_hash_table (info),
7105 info->fini_function, FALSE,
7106 FALSE, FALSE)
7107 : NULL);
7108 if (h != NULL
7109 && (h->ref_regular
7110 || h->def_regular))
7111 {
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7113 return FALSE;
7114 }
7115
7116 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7117 if (s != NULL && s->linker_has_input)
7118 {
7119 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7120 if (! bfd_link_executable (info))
7121 {
7122 bfd *sub;
7123 asection *o;
7124
57963c05
AM
7125 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7126 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7127 && (o = sub->sections) != NULL
7128 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7129 for (o = sub->sections; o != NULL; o = o->next)
7130 if (elf_section_data (o)->this_hdr.sh_type
7131 == SHT_PREINIT_ARRAY)
7132 {
7133 _bfd_error_handler
871b3ab2 7134 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7135 sub);
7136 break;
7137 }
7138
7139 bfd_set_error (bfd_error_nonrepresentable_section);
7140 return FALSE;
7141 }
7142
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7144 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7145 return FALSE;
7146 }
7147 s = bfd_get_section_by_name (output_bfd, ".init_array");
7148 if (s != NULL && s->linker_has_input)
7149 {
7150 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7151 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7152 return FALSE;
7153 }
7154 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7155 if (s != NULL && s->linker_has_input)
7156 {
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7159 return FALSE;
7160 }
7161
7162 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7163 /* If .dynstr is excluded from the link, we don't want any of
7164 these tags. Strictly, we should be checking each section
7165 individually; This quick check covers for the case where
7166 someone does a /DISCARD/ : { *(*) }. */
7167 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7168 {
7169 bfd_size_type strsize;
7170
7171 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7172 if ((info->emit_hash
7173 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7174 || (info->emit_gnu_hash
f16a9783
MS
7175 && (bed->record_xhash_symbol == NULL
7176 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7177 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7178 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7180 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7181 bed->s->sizeof_sym)
7182 || (info->gnu_flags_1
7183 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7184 info->gnu_flags_1)))
902e9fc7
MR
7185 return FALSE;
7186 }
7187 }
7188
7189 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7190 return FALSE;
7191
7192 /* The backend must work out the sizes of all the other dynamic
7193 sections. */
7194 if (dynobj != NULL
7195 && bed->elf_backend_size_dynamic_sections != NULL
7196 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7197 return FALSE;
7198
7199 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7200 {
902e9fc7
MR
7201 if (elf_tdata (output_bfd)->cverdefs)
7202 {
7203 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7204
7205 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7206 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7207 return FALSE;
7208 }
7209
7210 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7211 {
7212 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7213 return FALSE;
7214 }
7215 else if (info->flags & DF_BIND_NOW)
7216 {
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7218 return FALSE;
7219 }
7220
7221 if (info->flags_1)
7222 {
7223 if (bfd_link_executable (info))
7224 info->flags_1 &= ~ (DF_1_INITFIRST
7225 | DF_1_NODELETE
7226 | DF_1_NOOPEN);
7227 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7228 return FALSE;
7229 }
7230
7231 if (elf_tdata (output_bfd)->cverrefs)
7232 {
7233 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7234
7235 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7236 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7237 return FALSE;
7238 }
5a580b3a 7239
8423293d
AM
7240 if ((elf_tdata (output_bfd)->cverrefs == 0
7241 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7242 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7243 {
902e9fc7
MR
7244 asection *s;
7245
3d4d4302 7246 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7247 s->flags |= SEC_EXCLUDE;
7248 }
7249 }
7250 return TRUE;
7251}
7252
74541ad4
AM
7253/* Find the first non-excluded output section. We'll use its
7254 section symbol for some emitted relocs. */
7255void
7256_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7257{
7258 asection *s;
f26a3287 7259 asection *found = NULL;
74541ad4
AM
7260
7261 for (s = output_bfd->sections; s != NULL; s = s->next)
7262 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7263 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7264 {
f26a3287
AM
7265 found = s;
7266 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7267 break;
74541ad4 7268 }
f26a3287 7269 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7270}
7271
7272/* Find two non-excluded output sections, one for code, one for data.
7273 We'll use their section symbols for some emitted relocs. */
7274void
7275_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7276{
7277 asection *s;
f26a3287 7278 asection *found = NULL;
74541ad4 7279
266b05cf 7280 /* Data first, since setting text_index_section changes
7f923b7f 7281 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7282 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7283 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7284 && !(s->flags & SEC_READONLY)
d00dd7dc 7285 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7286 {
f26a3287
AM
7287 found = s;
7288 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7289 break;
74541ad4 7290 }
f26a3287 7291 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7292
7293 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7294 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7295 && (s->flags & SEC_READONLY)
d00dd7dc 7296 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7297 {
f26a3287 7298 found = s;
74541ad4
AM
7299 break;
7300 }
f26a3287 7301 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7302}
7303
f16a9783
MS
7304#define GNU_HASH_SECTION_NAME(bed) \
7305 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7306
8423293d
AM
7307bfd_boolean
7308bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7309{
74541ad4 7310 const struct elf_backend_data *bed;
23ec1e32 7311 unsigned long section_sym_count;
96d01d93 7312 bfd_size_type dynsymcount = 0;
74541ad4 7313
8423293d
AM
7314 if (!is_elf_hash_table (info->hash))
7315 return TRUE;
7316
74541ad4
AM
7317 bed = get_elf_backend_data (output_bfd);
7318 (*bed->elf_backend_init_index_section) (output_bfd, info);
7319
23ec1e32
MR
7320 /* Assign dynsym indices. In a shared library we generate a section
7321 symbol for each output section, which come first. Next come all
7322 of the back-end allocated local dynamic syms, followed by the rest
7323 of the global symbols.
7324
7325 This is usually not needed for static binaries, however backends
7326 can request to always do it, e.g. the MIPS backend uses dynamic
7327 symbol counts to lay out GOT, which will be produced in the
7328 presence of GOT relocations even in static binaries (holding fixed
7329 data in that case, to satisfy those relocations). */
7330
7331 if (elf_hash_table (info)->dynamic_sections_created
7332 || bed->always_renumber_dynsyms)
7333 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7334 &section_sym_count);
7335
8423293d
AM
7336 if (elf_hash_table (info)->dynamic_sections_created)
7337 {
7338 bfd *dynobj;
8423293d 7339 asection *s;
8423293d
AM
7340 unsigned int dtagcount;
7341
7342 dynobj = elf_hash_table (info)->dynobj;
7343
5a580b3a 7344 /* Work out the size of the symbol version section. */
3d4d4302 7345 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7346 BFD_ASSERT (s != NULL);
d5486c43 7347 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7348 {
eea6121a 7349 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7350 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7351 if (s->contents == NULL)
7352 return FALSE;
7353
7354 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7355 return FALSE;
7356 }
7357
7358 /* Set the size of the .dynsym and .hash sections. We counted
7359 the number of dynamic symbols in elf_link_add_object_symbols.
7360 We will build the contents of .dynsym and .hash when we build
7361 the final symbol table, because until then we do not know the
7362 correct value to give the symbols. We built the .dynstr
7363 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7364 s = elf_hash_table (info)->dynsym;
5a580b3a 7365 BFD_ASSERT (s != NULL);
eea6121a 7366 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7367
d5486c43
L
7368 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7369 if (s->contents == NULL)
7370 return FALSE;
5a580b3a 7371
d5486c43
L
7372 /* The first entry in .dynsym is a dummy symbol. Clear all the
7373 section syms, in case we don't output them all. */
7374 ++section_sym_count;
7375 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7376
fdc90cb4
JJ
7377 elf_hash_table (info)->bucketcount = 0;
7378
5a580b3a
AM
7379 /* Compute the size of the hashing table. As a side effect this
7380 computes the hash values for all the names we export. */
fdc90cb4
JJ
7381 if (info->emit_hash)
7382 {
7383 unsigned long int *hashcodes;
14b1c01e 7384 struct hash_codes_info hashinf;
fdc90cb4
JJ
7385 bfd_size_type amt;
7386 unsigned long int nsyms;
7387 size_t bucketcount;
7388 size_t hash_entry_size;
7389
7390 /* Compute the hash values for all exported symbols. At the same
7391 time store the values in an array so that we could use them for
7392 optimizations. */
7393 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7394 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7395 if (hashcodes == NULL)
7396 return FALSE;
14b1c01e
AM
7397 hashinf.hashcodes = hashcodes;
7398 hashinf.error = FALSE;
5a580b3a 7399
fdc90cb4
JJ
7400 /* Put all hash values in HASHCODES. */
7401 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7402 elf_collect_hash_codes, &hashinf);
7403 if (hashinf.error)
4dd07732
AM
7404 {
7405 free (hashcodes);
7406 return FALSE;
7407 }
5a580b3a 7408
14b1c01e 7409 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7410 bucketcount
7411 = compute_bucket_count (info, hashcodes, nsyms, 0);
7412 free (hashcodes);
7413
4b48e2f6 7414 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7415 return FALSE;
5a580b3a 7416
fdc90cb4
JJ
7417 elf_hash_table (info)->bucketcount = bucketcount;
7418
3d4d4302 7419 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7420 BFD_ASSERT (s != NULL);
7421 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7422 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7423 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7424 if (s->contents == NULL)
7425 return FALSE;
7426
7427 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7428 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7429 s->contents + hash_entry_size);
7430 }
7431
7432 if (info->emit_gnu_hash)
7433 {
7434 size_t i, cnt;
7435 unsigned char *contents;
7436 struct collect_gnu_hash_codes cinfo;
7437 bfd_size_type amt;
7438 size_t bucketcount;
7439
7440 memset (&cinfo, 0, sizeof (cinfo));
7441
7442 /* Compute the hash values for all exported symbols. At the same
7443 time store the values in an array so that we could use them for
7444 optimizations. */
7445 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7446 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7447 if (cinfo.hashcodes == NULL)
7448 return FALSE;
7449
7450 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7451 cinfo.min_dynindx = -1;
7452 cinfo.output_bfd = output_bfd;
7453 cinfo.bed = bed;
7454
7455 /* Put all hash values in HASHCODES. */
7456 elf_link_hash_traverse (elf_hash_table (info),
7457 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7458 if (cinfo.error)
4dd07732
AM
7459 {
7460 free (cinfo.hashcodes);
7461 return FALSE;
7462 }
fdc90cb4
JJ
7463
7464 bucketcount
7465 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7466
7467 if (bucketcount == 0)
7468 {
7469 free (cinfo.hashcodes);
7470 return FALSE;
7471 }
7472
f16a9783 7473 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7474 BFD_ASSERT (s != NULL);
7475
7476 if (cinfo.nsyms == 0)
7477 {
f16a9783 7478 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7479 BFD_ASSERT (cinfo.min_dynindx == -1);
7480 free (cinfo.hashcodes);
7481 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7482 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7483 if (contents == NULL)
7484 return FALSE;
7485 s->contents = contents;
7486 /* 1 empty bucket. */
7487 bfd_put_32 (output_bfd, 1, contents);
7488 /* SYMIDX above the special symbol 0. */
7489 bfd_put_32 (output_bfd, 1, contents + 4);
7490 /* Just one word for bitmask. */
7491 bfd_put_32 (output_bfd, 1, contents + 8);
7492 /* Only hash fn bloom filter. */
7493 bfd_put_32 (output_bfd, 0, contents + 12);
7494 /* No hashes are valid - empty bitmask. */
7495 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7496 /* No hashes in the only bucket. */
7497 bfd_put_32 (output_bfd, 0,
7498 contents + 16 + bed->s->arch_size / 8);
7499 }
7500 else
7501 {
9e6619e2 7502 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7503 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7504
9e6619e2
AM
7505 x = cinfo.nsyms;
7506 maskbitslog2 = 1;
7507 while ((x >>= 1) != 0)
7508 ++maskbitslog2;
fdc90cb4
JJ
7509 if (maskbitslog2 < 3)
7510 maskbitslog2 = 5;
7511 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7512 maskbitslog2 = maskbitslog2 + 3;
7513 else
7514 maskbitslog2 = maskbitslog2 + 2;
7515 if (bed->s->arch_size == 64)
7516 {
7517 if (maskbitslog2 == 5)
7518 maskbitslog2 = 6;
7519 cinfo.shift1 = 6;
7520 }
7521 else
7522 cinfo.shift1 = 5;
7523 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7524 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7525 cinfo.maskbits = 1 << maskbitslog2;
7526 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7527 amt = bucketcount * sizeof (unsigned long int) * 2;
7528 amt += maskwords * sizeof (bfd_vma);
a50b1753 7529 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7530 if (cinfo.bitmask == NULL)
7531 {
7532 free (cinfo.hashcodes);
7533 return FALSE;
7534 }
7535
a50b1753 7536 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7537 cinfo.indx = cinfo.counts + bucketcount;
7538 cinfo.symindx = dynsymcount - cinfo.nsyms;
7539 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7540
7541 /* Determine how often each hash bucket is used. */
7542 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7543 for (i = 0; i < cinfo.nsyms; ++i)
7544 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7545
7546 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7547 if (cinfo.counts[i] != 0)
7548 {
7549 cinfo.indx[i] = cnt;
7550 cnt += cinfo.counts[i];
7551 }
7552 BFD_ASSERT (cnt == dynsymcount);
7553 cinfo.bucketcount = bucketcount;
7554 cinfo.local_indx = cinfo.min_dynindx;
7555
7556 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7557 s->size += cinfo.maskbits / 8;
f16a9783
MS
7558 if (bed->record_xhash_symbol != NULL)
7559 s->size += cinfo.nsyms * 4;
a50b1753 7560 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7561 if (contents == NULL)
7562 {
7563 free (cinfo.bitmask);
7564 free (cinfo.hashcodes);
7565 return FALSE;
7566 }
7567
7568 s->contents = contents;
7569 bfd_put_32 (output_bfd, bucketcount, contents);
7570 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7571 bfd_put_32 (output_bfd, maskwords, contents + 8);
7572 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7573 contents += 16 + cinfo.maskbits / 8;
7574
7575 for (i = 0; i < bucketcount; ++i)
7576 {
7577 if (cinfo.counts[i] == 0)
7578 bfd_put_32 (output_bfd, 0, contents);
7579 else
7580 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7581 contents += 4;
7582 }
7583
7584 cinfo.contents = contents;
7585
f16a9783
MS
7586 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7587 /* Renumber dynamic symbols, if populating .gnu.hash section.
7588 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7589 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7590 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7591
7592 contents = s->contents + 16;
7593 for (i = 0; i < maskwords; ++i)
7594 {
7595 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7596 contents);
7597 contents += bed->s->arch_size / 8;
7598 }
7599
7600 free (cinfo.bitmask);
7601 free (cinfo.hashcodes);
7602 }
7603 }
5a580b3a 7604
3d4d4302 7605 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7606 BFD_ASSERT (s != NULL);
7607
4ad4eba5 7608 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7609
eea6121a 7610 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7611
7612 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7613 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7614 return FALSE;
7615 }
7616
7617 return TRUE;
7618}
4d269e42 7619\f
4d269e42
AM
7620/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7621
7622static void
7623merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7624 asection *sec)
7625{
dbaa2011
AM
7626 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7627 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7628}
7629
7630/* Finish SHF_MERGE section merging. */
7631
7632bfd_boolean
630993ec 7633_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7634{
7635 bfd *ibfd;
7636 asection *sec;
7637
7638 if (!is_elf_hash_table (info->hash))
7639 return FALSE;
7640
c72f2fb2 7641 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7642 if ((ibfd->flags & DYNAMIC) == 0
7643 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7644 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7645 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7646 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7647 if ((sec->flags & SEC_MERGE) != 0
7648 && !bfd_is_abs_section (sec->output_section))
7649 {
7650 struct bfd_elf_section_data *secdata;
7651
7652 secdata = elf_section_data (sec);
630993ec 7653 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7654 &elf_hash_table (info)->merge_info,
7655 sec, &secdata->sec_info))
7656 return FALSE;
7657 else if (secdata->sec_info)
dbaa2011 7658 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7659 }
7660
7661 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7662 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7663 merge_sections_remove_hook);
7664 return TRUE;
7665}
7666
7667/* Create an entry in an ELF linker hash table. */
7668
7669struct bfd_hash_entry *
7670_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7671 struct bfd_hash_table *table,
7672 const char *string)
7673{
7674 /* Allocate the structure if it has not already been allocated by a
7675 subclass. */
7676 if (entry == NULL)
7677 {
a50b1753 7678 entry = (struct bfd_hash_entry *)
ca4be51c 7679 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7680 if (entry == NULL)
7681 return entry;
7682 }
7683
7684 /* Call the allocation method of the superclass. */
7685 entry = _bfd_link_hash_newfunc (entry, table, string);
7686 if (entry != NULL)
7687 {
7688 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7689 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7690
7691 /* Set local fields. */
7692 ret->indx = -1;
7693 ret->dynindx = -1;
7694 ret->got = htab->init_got_refcount;
7695 ret->plt = htab->init_plt_refcount;
7696 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7697 - offsetof (struct elf_link_hash_entry, size)));
7698 /* Assume that we have been called by a non-ELF symbol reader.
7699 This flag is then reset by the code which reads an ELF input
7700 file. This ensures that a symbol created by a non-ELF symbol
7701 reader will have the flag set correctly. */
7702 ret->non_elf = 1;
7703 }
7704
7705 return entry;
7706}
7707
7708/* Copy data from an indirect symbol to its direct symbol, hiding the
7709 old indirect symbol. Also used for copying flags to a weakdef. */
7710
7711void
7712_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7713 struct elf_link_hash_entry *dir,
7714 struct elf_link_hash_entry *ind)
7715{
7716 struct elf_link_hash_table *htab;
7717
ad172eaa
L
7718 if (ind->dyn_relocs != NULL)
7719 {
7720 if (dir->dyn_relocs != NULL)
7721 {
7722 struct elf_dyn_relocs **pp;
7723 struct elf_dyn_relocs *p;
7724
7725 /* Add reloc counts against the indirect sym to the direct sym
7726 list. Merge any entries against the same section. */
7727 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7728 {
7729 struct elf_dyn_relocs *q;
7730
7731 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7732 if (q->sec == p->sec)
7733 {
7734 q->pc_count += p->pc_count;
7735 q->count += p->count;
7736 *pp = p->next;
7737 break;
7738 }
7739 if (q == NULL)
7740 pp = &p->next;
7741 }
7742 *pp = dir->dyn_relocs;
7743 }
7744
7745 dir->dyn_relocs = ind->dyn_relocs;
7746 ind->dyn_relocs = NULL;
7747 }
7748
4d269e42 7749 /* Copy down any references that we may have already seen to the
e81830c5 7750 symbol which just became indirect. */
4d269e42 7751
422f1182 7752 if (dir->versioned != versioned_hidden)
e81830c5
AM
7753 dir->ref_dynamic |= ind->ref_dynamic;
7754 dir->ref_regular |= ind->ref_regular;
7755 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7756 dir->non_got_ref |= ind->non_got_ref;
7757 dir->needs_plt |= ind->needs_plt;
7758 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7759
7760 if (ind->root.type != bfd_link_hash_indirect)
7761 return;
7762
7763 /* Copy over the global and procedure linkage table refcount entries.
7764 These may have been already set up by a check_relocs routine. */
7765 htab = elf_hash_table (info);
7766 if (ind->got.refcount > htab->init_got_refcount.refcount)
7767 {
7768 if (dir->got.refcount < 0)
7769 dir->got.refcount = 0;
7770 dir->got.refcount += ind->got.refcount;
7771 ind->got.refcount = htab->init_got_refcount.refcount;
7772 }
7773
7774 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7775 {
7776 if (dir->plt.refcount < 0)
7777 dir->plt.refcount = 0;
7778 dir->plt.refcount += ind->plt.refcount;
7779 ind->plt.refcount = htab->init_plt_refcount.refcount;
7780 }
7781
7782 if (ind->dynindx != -1)
7783 {
7784 if (dir->dynindx != -1)
7785 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7786 dir->dynindx = ind->dynindx;
7787 dir->dynstr_index = ind->dynstr_index;
7788 ind->dynindx = -1;
7789 ind->dynstr_index = 0;
7790 }
7791}
7792
7793void
7794_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7795 struct elf_link_hash_entry *h,
7796 bfd_boolean force_local)
7797{
3aa14d16
L
7798 /* STT_GNU_IFUNC symbol must go through PLT. */
7799 if (h->type != STT_GNU_IFUNC)
7800 {
7801 h->plt = elf_hash_table (info)->init_plt_offset;
7802 h->needs_plt = 0;
7803 }
4d269e42
AM
7804 if (force_local)
7805 {
7806 h->forced_local = 1;
7807 if (h->dynindx != -1)
7808 {
4d269e42
AM
7809 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7810 h->dynstr_index);
641338d8
AM
7811 h->dynindx = -1;
7812 h->dynstr_index = 0;
4d269e42
AM
7813 }
7814 }
7815}
7816
34a87bb0
L
7817/* Hide a symbol. */
7818
7819void
7820_bfd_elf_link_hide_symbol (bfd *output_bfd,
7821 struct bfd_link_info *info,
7822 struct bfd_link_hash_entry *h)
7823{
7824 if (is_elf_hash_table (info->hash))
7825 {
7826 const struct elf_backend_data *bed
7827 = get_elf_backend_data (output_bfd);
7828 struct elf_link_hash_entry *eh
7829 = (struct elf_link_hash_entry *) h;
7830 bed->elf_backend_hide_symbol (info, eh, TRUE);
7831 eh->def_dynamic = 0;
7832 eh->ref_dynamic = 0;
7833 eh->dynamic_def = 0;
7834 }
7835}
7836
7bf52ea2
AM
7837/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7838 caller. */
4d269e42
AM
7839
7840bfd_boolean
7841_bfd_elf_link_hash_table_init
7842 (struct elf_link_hash_table *table,
7843 bfd *abfd,
7844 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7845 struct bfd_hash_table *,
7846 const char *),
4dfe6ac6
NC
7847 unsigned int entsize,
7848 enum elf_target_id target_id)
4d269e42
AM
7849{
7850 bfd_boolean ret;
7851 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7852
4d269e42
AM
7853 table->init_got_refcount.refcount = can_refcount - 1;
7854 table->init_plt_refcount.refcount = can_refcount - 1;
7855 table->init_got_offset.offset = -(bfd_vma) 1;
7856 table->init_plt_offset.offset = -(bfd_vma) 1;
7857 /* The first dynamic symbol is a dummy. */
7858 table->dynsymcount = 1;
7859
7860 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7861
4d269e42 7862 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7863 table->hash_table_id = target_id;
90c14f0c 7864 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7865
7866 return ret;
7867}
7868
7869/* Create an ELF linker hash table. */
7870
7871struct bfd_link_hash_table *
7872_bfd_elf_link_hash_table_create (bfd *abfd)
7873{
7874 struct elf_link_hash_table *ret;
986f0783 7875 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7876
7bf52ea2 7877 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7878 if (ret == NULL)
7879 return NULL;
7880
7881 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7882 sizeof (struct elf_link_hash_entry),
7883 GENERIC_ELF_DATA))
4d269e42
AM
7884 {
7885 free (ret);
7886 return NULL;
7887 }
d495ab0d 7888 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7889
7890 return &ret->root;
7891}
7892
9f7c3e5e
AM
7893/* Destroy an ELF linker hash table. */
7894
7895void
d495ab0d 7896_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7897{
d495ab0d
AM
7898 struct elf_link_hash_table *htab;
7899
7900 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7901 if (htab->dynstr != NULL)
7902 _bfd_elf_strtab_free (htab->dynstr);
7903 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7904 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7905}
7906
4d269e42
AM
7907/* This is a hook for the ELF emulation code in the generic linker to
7908 tell the backend linker what file name to use for the DT_NEEDED
7909 entry for a dynamic object. */
7910
7911void
7912bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7913{
7914 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7915 && bfd_get_format (abfd) == bfd_object)
7916 elf_dt_name (abfd) = name;
7917}
7918
7919int
7920bfd_elf_get_dyn_lib_class (bfd *abfd)
7921{
7922 int lib_class;
7923 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7924 && bfd_get_format (abfd) == bfd_object)
7925 lib_class = elf_dyn_lib_class (abfd);
7926 else
7927 lib_class = 0;
7928 return lib_class;
7929}
7930
7931void
7932bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7933{
7934 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7935 && bfd_get_format (abfd) == bfd_object)
7936 elf_dyn_lib_class (abfd) = lib_class;
7937}
7938
7939/* Get the list of DT_NEEDED entries for a link. This is a hook for
7940 the linker ELF emulation code. */
7941
7942struct bfd_link_needed_list *
7943bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7944 struct bfd_link_info *info)
7945{
7946 if (! is_elf_hash_table (info->hash))
7947 return NULL;
7948 return elf_hash_table (info)->needed;
7949}
7950
7951/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7952 hook for the linker ELF emulation code. */
7953
7954struct bfd_link_needed_list *
7955bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7956 struct bfd_link_info *info)
7957{
7958 if (! is_elf_hash_table (info->hash))
7959 return NULL;
7960 return elf_hash_table (info)->runpath;
7961}
7962
7963/* Get the name actually used for a dynamic object for a link. This
7964 is the SONAME entry if there is one. Otherwise, it is the string
7965 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7966
7967const char *
7968bfd_elf_get_dt_soname (bfd *abfd)
7969{
7970 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7971 && bfd_get_format (abfd) == bfd_object)
7972 return elf_dt_name (abfd);
7973 return NULL;
7974}
7975
7976/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7977 the ELF linker emulation code. */
7978
7979bfd_boolean
7980bfd_elf_get_bfd_needed_list (bfd *abfd,
7981 struct bfd_link_needed_list **pneeded)
7982{
7983 asection *s;
7984 bfd_byte *dynbuf = NULL;
cb33740c 7985 unsigned int elfsec;
4d269e42
AM
7986 unsigned long shlink;
7987 bfd_byte *extdyn, *extdynend;
7988 size_t extdynsize;
7989 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7990
7991 *pneeded = NULL;
7992
7993 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7994 || bfd_get_format (abfd) != bfd_object)
7995 return TRUE;
7996
7997 s = bfd_get_section_by_name (abfd, ".dynamic");
7998 if (s == NULL || s->size == 0)
7999 return TRUE;
8000
8001 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8002 goto error_return;
8003
8004 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8005 if (elfsec == SHN_BAD)
4d269e42
AM
8006 goto error_return;
8007
8008 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8009
4d269e42
AM
8010 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8011 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8012
8013 extdyn = dynbuf;
8014 extdynend = extdyn + s->size;
8015 for (; extdyn < extdynend; extdyn += extdynsize)
8016 {
8017 Elf_Internal_Dyn dyn;
8018
8019 (*swap_dyn_in) (abfd, extdyn, &dyn);
8020
8021 if (dyn.d_tag == DT_NULL)
8022 break;
8023
8024 if (dyn.d_tag == DT_NEEDED)
8025 {
8026 const char *string;
8027 struct bfd_link_needed_list *l;
8028 unsigned int tagv = dyn.d_un.d_val;
986f0783 8029 size_t amt;
4d269e42
AM
8030
8031 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8032 if (string == NULL)
8033 goto error_return;
8034
8035 amt = sizeof *l;
a50b1753 8036 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8037 if (l == NULL)
8038 goto error_return;
8039
8040 l->by = abfd;
8041 l->name = string;
8042 l->next = *pneeded;
8043 *pneeded = l;
8044 }
8045 }
8046
8047 free (dynbuf);
8048
8049 return TRUE;
8050
8051 error_return:
c9594989 8052 free (dynbuf);
4d269e42
AM
8053 return FALSE;
8054}
8055
8056struct elf_symbuf_symbol
8057{
8058 unsigned long st_name; /* Symbol name, index in string tbl */
8059 unsigned char st_info; /* Type and binding attributes */
8060 unsigned char st_other; /* Visibilty, and target specific */
8061};
8062
8063struct elf_symbuf_head
8064{
8065 struct elf_symbuf_symbol *ssym;
ef53be89 8066 size_t count;
4d269e42
AM
8067 unsigned int st_shndx;
8068};
8069
8070struct elf_symbol
8071{
8072 union
8073 {
8074 Elf_Internal_Sym *isym;
8075 struct elf_symbuf_symbol *ssym;
dcea6a95 8076 void *p;
4d269e42
AM
8077 } u;
8078 const char *name;
8079};
8080
8081/* Sort references to symbols by ascending section number. */
8082
8083static int
8084elf_sort_elf_symbol (const void *arg1, const void *arg2)
8085{
8086 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8087 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8088
dcea6a95
AM
8089 if (s1->st_shndx != s2->st_shndx)
8090 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8091 /* Final sort by the address of the sym in the symbuf ensures
8092 a stable sort. */
8093 if (s1 != s2)
8094 return s1 > s2 ? 1 : -1;
8095 return 0;
4d269e42
AM
8096}
8097
8098static int
8099elf_sym_name_compare (const void *arg1, const void *arg2)
8100{
8101 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8102 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8103 int ret = strcmp (s1->name, s2->name);
8104 if (ret != 0)
8105 return ret;
8106 if (s1->u.p != s2->u.p)
8107 return s1->u.p > s2->u.p ? 1 : -1;
8108 return 0;
4d269e42
AM
8109}
8110
8111static struct elf_symbuf_head *
ef53be89 8112elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8113{
14b1c01e 8114 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8115 struct elf_symbuf_symbol *ssym;
8116 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8117 size_t i, shndx_count, total_size, amt;
4d269e42 8118
446f7ed5
AM
8119 amt = symcount * sizeof (*indbuf);
8120 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8121 if (indbuf == NULL)
8122 return NULL;
8123
8124 for (ind = indbuf, i = 0; i < symcount; i++)
8125 if (isymbuf[i].st_shndx != SHN_UNDEF)
8126 *ind++ = &isymbuf[i];
8127 indbufend = ind;
8128
8129 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8130 elf_sort_elf_symbol);
8131
8132 shndx_count = 0;
8133 if (indbufend > indbuf)
8134 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8135 if (ind[0]->st_shndx != ind[1]->st_shndx)
8136 shndx_count++;
8137
3ae181ee
L
8138 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8139 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8140 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8141 if (ssymbuf == NULL)
8142 {
8143 free (indbuf);
8144 return NULL;
8145 }
8146
3ae181ee 8147 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8148 ssymbuf->ssym = NULL;
8149 ssymbuf->count = shndx_count;
8150 ssymbuf->st_shndx = 0;
8151 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8152 {
8153 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8154 {
8155 ssymhead++;
8156 ssymhead->ssym = ssym;
8157 ssymhead->count = 0;
8158 ssymhead->st_shndx = (*ind)->st_shndx;
8159 }
8160 ssym->st_name = (*ind)->st_name;
8161 ssym->st_info = (*ind)->st_info;
8162 ssym->st_other = (*ind)->st_other;
8163 ssymhead->count++;
8164 }
ef53be89 8165 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8166 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8167 == total_size));
4d269e42
AM
8168
8169 free (indbuf);
8170 return ssymbuf;
8171}
8172
8173/* Check if 2 sections define the same set of local and global
8174 symbols. */
8175
8f317e31 8176static bfd_boolean
4d269e42
AM
8177bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8178 struct bfd_link_info *info)
8179{
8180 bfd *bfd1, *bfd2;
8181 const struct elf_backend_data *bed1, *bed2;
8182 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8183 size_t symcount1, symcount2;
4d269e42
AM
8184 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8185 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8186 Elf_Internal_Sym *isym, *isymend;
8187 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8188 size_t count1, count2, i;
cb33740c 8189 unsigned int shndx1, shndx2;
4d269e42
AM
8190 bfd_boolean result;
8191
8192 bfd1 = sec1->owner;
8193 bfd2 = sec2->owner;
8194
4d269e42
AM
8195 /* Both sections have to be in ELF. */
8196 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8197 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8198 return FALSE;
8199
8200 if (elf_section_type (sec1) != elf_section_type (sec2))
8201 return FALSE;
8202
4d269e42
AM
8203 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8204 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8205 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8206 return FALSE;
8207
8208 bed1 = get_elf_backend_data (bfd1);
8209 bed2 = get_elf_backend_data (bfd2);
8210 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8211 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8212 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8213 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8214
8215 if (symcount1 == 0 || symcount2 == 0)
8216 return FALSE;
8217
8218 result = FALSE;
8219 isymbuf1 = NULL;
8220 isymbuf2 = NULL;
a50b1753
NC
8221 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8222 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8223
8224 if (ssymbuf1 == NULL)
8225 {
8226 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8227 NULL, NULL, NULL);
8228 if (isymbuf1 == NULL)
8229 goto done;
8230
67411cbf 8231 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8232 {
8233 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8234 elf_tdata (bfd1)->symbuf = ssymbuf1;
8235 }
4d269e42
AM
8236 }
8237
8238 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8239 {
8240 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8241 NULL, NULL, NULL);
8242 if (isymbuf2 == NULL)
8243 goto done;
8244
67411cbf 8245 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8246 {
8247 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8248 elf_tdata (bfd2)->symbuf = ssymbuf2;
8249 }
4d269e42
AM
8250 }
8251
8252 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8253 {
8254 /* Optimized faster version. */
ef53be89 8255 size_t lo, hi, mid;
4d269e42
AM
8256 struct elf_symbol *symp;
8257 struct elf_symbuf_symbol *ssym, *ssymend;
8258
8259 lo = 0;
8260 hi = ssymbuf1->count;
8261 ssymbuf1++;
8262 count1 = 0;
8263 while (lo < hi)
8264 {
8265 mid = (lo + hi) / 2;
cb33740c 8266 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8267 hi = mid;
cb33740c 8268 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8269 lo = mid + 1;
8270 else
8271 {
8272 count1 = ssymbuf1[mid].count;
8273 ssymbuf1 += mid;
8274 break;
8275 }
8276 }
8277
8278 lo = 0;
8279 hi = ssymbuf2->count;
8280 ssymbuf2++;
8281 count2 = 0;
8282 while (lo < hi)
8283 {
8284 mid = (lo + hi) / 2;
cb33740c 8285 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8286 hi = mid;
cb33740c 8287 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8288 lo = mid + 1;
8289 else
8290 {
8291 count2 = ssymbuf2[mid].count;
8292 ssymbuf2 += mid;
8293 break;
8294 }
8295 }
8296
8297 if (count1 == 0 || count2 == 0 || count1 != count2)
8298 goto done;
8299
ca4be51c
AM
8300 symtable1
8301 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8302 symtable2
8303 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8304 if (symtable1 == NULL || symtable2 == NULL)
8305 goto done;
8306
8307 symp = symtable1;
8308 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8309 ssym < ssymend; ssym++, symp++)
8310 {
8311 symp->u.ssym = ssym;
8312 symp->name = bfd_elf_string_from_elf_section (bfd1,
8313 hdr1->sh_link,
8314 ssym->st_name);
8315 }
8316
8317 symp = symtable2;
8318 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8319 ssym < ssymend; ssym++, symp++)
8320 {
8321 symp->u.ssym = ssym;
8322 symp->name = bfd_elf_string_from_elf_section (bfd2,
8323 hdr2->sh_link,
8324 ssym->st_name);
8325 }
8326
8327 /* Sort symbol by name. */
8328 qsort (symtable1, count1, sizeof (struct elf_symbol),
8329 elf_sym_name_compare);
8330 qsort (symtable2, count1, sizeof (struct elf_symbol),
8331 elf_sym_name_compare);
8332
8333 for (i = 0; i < count1; i++)
8334 /* Two symbols must have the same binding, type and name. */
8335 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8336 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8337 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8338 goto done;
8339
8340 result = TRUE;
8341 goto done;
8342 }
8343
a50b1753
NC
8344 symtable1 = (struct elf_symbol *)
8345 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8346 symtable2 = (struct elf_symbol *)
8347 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8348 if (symtable1 == NULL || symtable2 == NULL)
8349 goto done;
8350
8351 /* Count definitions in the section. */
8352 count1 = 0;
8353 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8354 if (isym->st_shndx == shndx1)
4d269e42
AM
8355 symtable1[count1++].u.isym = isym;
8356
8357 count2 = 0;
8358 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8359 if (isym->st_shndx == shndx2)
4d269e42
AM
8360 symtable2[count2++].u.isym = isym;
8361
8362 if (count1 == 0 || count2 == 0 || count1 != count2)
8363 goto done;
8364
8365 for (i = 0; i < count1; i++)
8366 symtable1[i].name
8367 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8368 symtable1[i].u.isym->st_name);
8369
8370 for (i = 0; i < count2; i++)
8371 symtable2[i].name
8372 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8373 symtable2[i].u.isym->st_name);
8374
8375 /* Sort symbol by name. */
8376 qsort (symtable1, count1, sizeof (struct elf_symbol),
8377 elf_sym_name_compare);
8378 qsort (symtable2, count1, sizeof (struct elf_symbol),
8379 elf_sym_name_compare);
8380
8381 for (i = 0; i < count1; i++)
8382 /* Two symbols must have the same binding, type and name. */
8383 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8384 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8385 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8386 goto done;
8387
8388 result = TRUE;
8389
dc1e8a47 8390 done:
c9594989
AM
8391 free (symtable1);
8392 free (symtable2);
8393 free (isymbuf1);
8394 free (isymbuf2);
4d269e42
AM
8395
8396 return result;
8397}
8398
8399/* Return TRUE if 2 section types are compatible. */
8400
8401bfd_boolean
8402_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8403 bfd *bbfd, const asection *bsec)
8404{
8405 if (asec == NULL
8406 || bsec == NULL
8407 || abfd->xvec->flavour != bfd_target_elf_flavour
8408 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8409 return TRUE;
8410
8411 return elf_section_type (asec) == elf_section_type (bsec);
8412}
8413\f
c152c796
AM
8414/* Final phase of ELF linker. */
8415
8416/* A structure we use to avoid passing large numbers of arguments. */
8417
8418struct elf_final_link_info
8419{
8420 /* General link information. */
8421 struct bfd_link_info *info;
8422 /* Output BFD. */
8423 bfd *output_bfd;
8424 /* Symbol string table. */
ef10c3ac 8425 struct elf_strtab_hash *symstrtab;
c152c796
AM
8426 /* .hash section. */
8427 asection *hash_sec;
8428 /* symbol version section (.gnu.version). */
8429 asection *symver_sec;
8430 /* Buffer large enough to hold contents of any section. */
8431 bfd_byte *contents;
8432 /* Buffer large enough to hold external relocs of any section. */
8433 void *external_relocs;
8434 /* Buffer large enough to hold internal relocs of any section. */
8435 Elf_Internal_Rela *internal_relocs;
8436 /* Buffer large enough to hold external local symbols of any input
8437 BFD. */
8438 bfd_byte *external_syms;
8439 /* And a buffer for symbol section indices. */
8440 Elf_External_Sym_Shndx *locsym_shndx;
8441 /* Buffer large enough to hold internal local symbols of any input
8442 BFD. */
8443 Elf_Internal_Sym *internal_syms;
8444 /* Array large enough to hold a symbol index for each local symbol
8445 of any input BFD. */
8446 long *indices;
8447 /* Array large enough to hold a section pointer for each local
8448 symbol of any input BFD. */
8449 asection **sections;
ef10c3ac 8450 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8451 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8452 /* Number of STT_FILE syms seen. */
8453 size_t filesym_count;
496afd17
L
8454 /* Local symbol hash table. */
8455 struct bfd_hash_table local_hash_table;
c152c796
AM
8456};
8457
496afd17
L
8458struct local_hash_entry
8459{
8460 /* Base hash table entry structure. */
8461 struct bfd_hash_entry root;
8462 /* Size of the local symbol name. */
8463 size_t size;
8464 /* Number of the duplicated local symbol names. */
8465 long count;
8466};
8467
8468/* Create an entry in the local symbol hash table. */
8469
8470static struct bfd_hash_entry *
8471local_hash_newfunc (struct bfd_hash_entry *entry,
8472 struct bfd_hash_table *table,
8473 const char *string)
8474{
8475
8476 /* Allocate the structure if it has not already been allocated by a
8477 subclass. */
8478 if (entry == NULL)
8479 {
8480 entry = bfd_hash_allocate (table,
8481 sizeof (struct local_hash_entry));
8482 if (entry == NULL)
8483 return entry;
8484 }
8485
8486 /* Call the allocation method of the superclass. */
8487 entry = bfd_hash_newfunc (entry, table, string);
8488 if (entry != NULL)
8489 {
8490 ((struct local_hash_entry *) entry)->count = 0;
8491 ((struct local_hash_entry *) entry)->size = 0;
8492 }
8493
8494 return entry;
8495}
8496
c152c796
AM
8497/* This struct is used to pass information to elf_link_output_extsym. */
8498
8499struct elf_outext_info
8500{
8501 bfd_boolean failed;
8502 bfd_boolean localsyms;
34a79995 8503 bfd_boolean file_sym_done;
8b127cbc 8504 struct elf_final_link_info *flinfo;
c152c796
AM
8505};
8506
d9352518
DB
8507
8508/* Support for evaluating a complex relocation.
8509
8510 Complex relocations are generalized, self-describing relocations. The
8511 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8512 relocations themselves.
d9352518 8513
4b69ce9b 8514 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8515 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8516 information (start bit, end bit, word width, etc) into the addend. This
8517 information is extracted from CGEN-generated operand tables within gas.
8518
4b69ce9b 8519 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8520 internal) representing prefix-notation expressions, including but not
8521 limited to those sorts of expressions normally encoded as addends in the
8522 addend field. The symbol mangling format is:
8523
8524 <node> := <literal>
07d6d2b8
AM
8525 | <unary-operator> ':' <node>
8526 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8527 ;
8528
8529 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8530 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8531 | '#' <hexdigits>
8532 ;
8533
8534 <binary-operator> := as in C
8535 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8536
8537static void
a0c8462f
AM
8538set_symbol_value (bfd *bfd_with_globals,
8539 Elf_Internal_Sym *isymbuf,
8540 size_t locsymcount,
8541 size_t symidx,
8542 bfd_vma val)
d9352518 8543{
8977835c
AM
8544 struct elf_link_hash_entry **sym_hashes;
8545 struct elf_link_hash_entry *h;
8546 size_t extsymoff = locsymcount;
d9352518 8547
8977835c 8548 if (symidx < locsymcount)
d9352518 8549 {
8977835c
AM
8550 Elf_Internal_Sym *sym;
8551
8552 sym = isymbuf + symidx;
8553 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8554 {
8555 /* It is a local symbol: move it to the
8556 "absolute" section and give it a value. */
8557 sym->st_shndx = SHN_ABS;
8558 sym->st_value = val;
8559 return;
8560 }
8561 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8562 extsymoff = 0;
d9352518 8563 }
8977835c
AM
8564
8565 /* It is a global symbol: set its link type
8566 to "defined" and give it a value. */
8567
8568 sym_hashes = elf_sym_hashes (bfd_with_globals);
8569 h = sym_hashes [symidx - extsymoff];
8570 while (h->root.type == bfd_link_hash_indirect
8571 || h->root.type == bfd_link_hash_warning)
8572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8573 h->root.type = bfd_link_hash_defined;
8574 h->root.u.def.value = val;
8575 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8576}
8577
a0c8462f
AM
8578static bfd_boolean
8579resolve_symbol (const char *name,
8580 bfd *input_bfd,
8b127cbc 8581 struct elf_final_link_info *flinfo,
a0c8462f
AM
8582 bfd_vma *result,
8583 Elf_Internal_Sym *isymbuf,
8584 size_t locsymcount)
d9352518 8585{
a0c8462f
AM
8586 Elf_Internal_Sym *sym;
8587 struct bfd_link_hash_entry *global_entry;
8588 const char *candidate = NULL;
8589 Elf_Internal_Shdr *symtab_hdr;
8590 size_t i;
8591
d9352518
DB
8592 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8593
8594 for (i = 0; i < locsymcount; ++ i)
8595 {
8977835c 8596 sym = isymbuf + i;
d9352518
DB
8597
8598 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8599 continue;
8600
8601 candidate = bfd_elf_string_from_elf_section (input_bfd,
8602 symtab_hdr->sh_link,
8603 sym->st_name);
8604#ifdef DEBUG
0f02bbd9
AM
8605 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8606 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8607#endif
8608 if (candidate && strcmp (candidate, name) == 0)
8609 {
8b127cbc 8610 asection *sec = flinfo->sections [i];
d9352518 8611
0f02bbd9
AM
8612 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8613 *result += sec->output_offset + sec->output_section->vma;
d9352518 8614#ifdef DEBUG
0f02bbd9
AM
8615 printf ("Found symbol with value %8.8lx\n",
8616 (unsigned long) *result);
d9352518
DB
8617#endif
8618 return TRUE;
8619 }
8620 }
8621
8622 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8623 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8624 FALSE, FALSE, TRUE);
d9352518
DB
8625 if (!global_entry)
8626 return FALSE;
a0c8462f 8627
d9352518
DB
8628 if (global_entry->type == bfd_link_hash_defined
8629 || global_entry->type == bfd_link_hash_defweak)
8630 {
a0c8462f
AM
8631 *result = (global_entry->u.def.value
8632 + global_entry->u.def.section->output_section->vma
8633 + global_entry->u.def.section->output_offset);
d9352518 8634#ifdef DEBUG
0f02bbd9
AM
8635 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8636 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8637#endif
8638 return TRUE;
a0c8462f 8639 }
d9352518 8640
d9352518
DB
8641 return FALSE;
8642}
8643
37b01f6a
DG
8644/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8645 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8646 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8647
d9352518 8648static bfd_boolean
a0c8462f
AM
8649resolve_section (const char *name,
8650 asection *sections,
37b01f6a
DG
8651 bfd_vma *result,
8652 bfd * abfd)
d9352518 8653{
a0c8462f
AM
8654 asection *curr;
8655 unsigned int len;
d9352518 8656
a0c8462f 8657 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8658 if (strcmp (curr->name, name) == 0)
8659 {
8660 *result = curr->vma;
8661 return TRUE;
8662 }
8663
8664 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8665 /* FIXME: This could be coded more efficiently... */
a0c8462f 8666 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8667 {
8668 len = strlen (curr->name);
a0c8462f 8669 if (len > strlen (name))
d9352518
DB
8670 continue;
8671
8672 if (strncmp (curr->name, name, len) == 0)
8673 {
8674 if (strncmp (".end", name + len, 4) == 0)
8675 {
61826503 8676 *result = (curr->vma
bb294208 8677 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8678 return TRUE;
8679 }
8680
8681 /* Insert more pseudo-section names here, if you like. */
8682 }
8683 }
a0c8462f 8684
d9352518
DB
8685 return FALSE;
8686}
8687
8688static void
a0c8462f 8689undefined_reference (const char *reftype, const char *name)
d9352518 8690{
695344c0 8691 /* xgettext:c-format */
a0c8462f
AM
8692 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8693 reftype, name);
4b69ce9b 8694 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8695}
8696
8697static bfd_boolean
a0c8462f
AM
8698eval_symbol (bfd_vma *result,
8699 const char **symp,
8700 bfd *input_bfd,
8b127cbc 8701 struct elf_final_link_info *flinfo,
a0c8462f
AM
8702 bfd_vma dot,
8703 Elf_Internal_Sym *isymbuf,
8704 size_t locsymcount,
8705 int signed_p)
d9352518 8706{
4b93929b
NC
8707 size_t len;
8708 size_t symlen;
a0c8462f
AM
8709 bfd_vma a;
8710 bfd_vma b;
4b93929b 8711 char symbuf[4096];
0f02bbd9 8712 const char *sym = *symp;
a0c8462f
AM
8713 const char *symend;
8714 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8715
8716 len = strlen (sym);
8717 symend = sym + len;
8718
4b93929b 8719 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8720 {
8721 bfd_set_error (bfd_error_invalid_operation);
8722 return FALSE;
8723 }
a0c8462f 8724
d9352518
DB
8725 switch (* sym)
8726 {
8727 case '.':
0f02bbd9
AM
8728 *result = dot;
8729 *symp = sym + 1;
d9352518
DB
8730 return TRUE;
8731
8732 case '#':
0f02bbd9
AM
8733 ++sym;
8734 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8735 return TRUE;
8736
8737 case 'S':
8738 symbol_is_section = TRUE;
1a0670f3 8739 /* Fall through. */
a0c8462f 8740 case 's':
0f02bbd9
AM
8741 ++sym;
8742 symlen = strtol (sym, (char **) symp, 10);
8743 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8744
4b93929b 8745 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8746 {
8747 bfd_set_error (bfd_error_invalid_operation);
8748 return FALSE;
8749 }
8750
8751 memcpy (symbuf, sym, symlen);
a0c8462f 8752 symbuf[symlen] = '\0';
0f02bbd9 8753 *symp = sym + symlen;
a0c8462f
AM
8754
8755 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8756 the symbol as a section, or vice-versa. so we're pretty liberal in our
8757 interpretation here; section means "try section first", not "must be a
8758 section", and likewise with symbol. */
8759
a0c8462f 8760 if (symbol_is_section)
d9352518 8761 {
37b01f6a 8762 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8763 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8764 isymbuf, locsymcount))
d9352518
DB
8765 {
8766 undefined_reference ("section", symbuf);
8767 return FALSE;
8768 }
a0c8462f
AM
8769 }
8770 else
d9352518 8771 {
8b127cbc 8772 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8773 isymbuf, locsymcount)
8b127cbc 8774 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8775 result, input_bfd))
d9352518
DB
8776 {
8777 undefined_reference ("symbol", symbuf);
8778 return FALSE;
8779 }
8780 }
8781
8782 return TRUE;
a0c8462f 8783
d9352518
DB
8784 /* All that remains are operators. */
8785
8786#define UNARY_OP(op) \
8787 if (strncmp (sym, #op, strlen (#op)) == 0) \
8788 { \
8789 sym += strlen (#op); \
a0c8462f
AM
8790 if (*sym == ':') \
8791 ++sym; \
0f02bbd9 8792 *symp = sym; \
8b127cbc 8793 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8794 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8795 return FALSE; \
8796 if (signed_p) \
0f02bbd9 8797 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8798 else \
8799 *result = op a; \
d9352518
DB
8800 return TRUE; \
8801 }
8802
4b69ce9b 8803#define BINARY_OP_HEAD(op) \
d9352518
DB
8804 if (strncmp (sym, #op, strlen (#op)) == 0) \
8805 { \
8806 sym += strlen (#op); \
a0c8462f
AM
8807 if (*sym == ':') \
8808 ++sym; \
0f02bbd9 8809 *symp = sym; \
8b127cbc 8810 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8811 isymbuf, locsymcount, signed_p)) \
a0c8462f 8812 return FALSE; \
0f02bbd9 8813 ++*symp; \
8b127cbc 8814 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8815 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8816 return FALSE;
8817#define BINARY_OP_TAIL(op) \
a0c8462f 8818 if (signed_p) \
0f02bbd9 8819 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8820 else \
8821 *result = a op b; \
d9352518
DB
8822 return TRUE; \
8823 }
4b69ce9b 8824#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8825
8826 default:
8827 UNARY_OP (0-);
4b69ce9b
AM
8828 BINARY_OP_HEAD (<<);
8829 if (b >= sizeof (a) * CHAR_BIT)
8830 {
8831 *result = 0;
8832 return TRUE;
8833 }
8834 signed_p = 0;
8835 BINARY_OP_TAIL (<<);
8836 BINARY_OP_HEAD (>>);
8837 if (b >= sizeof (a) * CHAR_BIT)
8838 {
8839 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8840 return TRUE;
8841 }
8842 BINARY_OP_TAIL (>>);
d9352518
DB
8843 BINARY_OP (==);
8844 BINARY_OP (!=);
8845 BINARY_OP (<=);
8846 BINARY_OP (>=);
8847 BINARY_OP (&&);
8848 BINARY_OP (||);
8849 UNARY_OP (~);
8850 UNARY_OP (!);
8851 BINARY_OP (*);
4b69ce9b
AM
8852 BINARY_OP_HEAD (/);
8853 if (b == 0)
8854 {
8855 _bfd_error_handler (_("division by zero"));
8856 bfd_set_error (bfd_error_bad_value);
8857 return FALSE;
8858 }
8859 BINARY_OP_TAIL (/);
8860 BINARY_OP_HEAD (%);
8861 if (b == 0)
8862 {
8863 _bfd_error_handler (_("division by zero"));
8864 bfd_set_error (bfd_error_bad_value);
8865 return FALSE;
8866 }
8867 BINARY_OP_TAIL (%);
d9352518
DB
8868 BINARY_OP (^);
8869 BINARY_OP (|);
8870 BINARY_OP (&);
8871 BINARY_OP (+);
8872 BINARY_OP (-);
8873 BINARY_OP (<);
8874 BINARY_OP (>);
8875#undef UNARY_OP
8876#undef BINARY_OP
8877 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8878 bfd_set_error (bfd_error_invalid_operation);
8879 return FALSE;
8880 }
8881}
8882
d9352518 8883static void
a0c8462f
AM
8884put_value (bfd_vma size,
8885 unsigned long chunksz,
8886 bfd *input_bfd,
8887 bfd_vma x,
8888 bfd_byte *location)
d9352518
DB
8889{
8890 location += (size - chunksz);
8891
41cd1ad1 8892 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8893 {
8894 switch (chunksz)
8895 {
d9352518
DB
8896 case 1:
8897 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8898 x >>= 8;
d9352518
DB
8899 break;
8900 case 2:
8901 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8902 x >>= 16;
d9352518
DB
8903 break;
8904 case 4:
8905 bfd_put_32 (input_bfd, x, location);
65164438
NC
8906 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8907 x >>= 16;
8908 x >>= 16;
d9352518 8909 break;
d9352518 8910#ifdef BFD64
41cd1ad1 8911 case 8:
d9352518 8912 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8913 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8914 x >>= 32;
8915 x >>= 32;
8916 break;
d9352518 8917#endif
41cd1ad1
NC
8918 default:
8919 abort ();
d9352518
DB
8920 break;
8921 }
8922 }
8923}
8924
a0c8462f
AM
8925static bfd_vma
8926get_value (bfd_vma size,
8927 unsigned long chunksz,
8928 bfd *input_bfd,
8929 bfd_byte *location)
d9352518 8930{
9b239e0e 8931 int shift;
d9352518
DB
8932 bfd_vma x = 0;
8933
9b239e0e
NC
8934 /* Sanity checks. */
8935 BFD_ASSERT (chunksz <= sizeof (x)
8936 && size >= chunksz
8937 && chunksz != 0
8938 && (size % chunksz) == 0
8939 && input_bfd != NULL
8940 && location != NULL);
8941
8942 if (chunksz == sizeof (x))
8943 {
8944 BFD_ASSERT (size == chunksz);
8945
8946 /* Make sure that we do not perform an undefined shift operation.
8947 We know that size == chunksz so there will only be one iteration
8948 of the loop below. */
8949 shift = 0;
8950 }
8951 else
8952 shift = 8 * chunksz;
8953
a0c8462f 8954 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8955 {
8956 switch (chunksz)
8957 {
d9352518 8958 case 1:
9b239e0e 8959 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8960 break;
8961 case 2:
9b239e0e 8962 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8963 break;
8964 case 4:
9b239e0e 8965 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8966 break;
d9352518 8967#ifdef BFD64
9b239e0e
NC
8968 case 8:
8969 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8970 break;
9b239e0e
NC
8971#endif
8972 default:
8973 abort ();
d9352518
DB
8974 }
8975 }
8976 return x;
8977}
8978
a0c8462f
AM
8979static void
8980decode_complex_addend (unsigned long *start, /* in bits */
8981 unsigned long *oplen, /* in bits */
8982 unsigned long *len, /* in bits */
8983 unsigned long *wordsz, /* in bytes */
8984 unsigned long *chunksz, /* in bytes */
8985 unsigned long *lsb0_p,
8986 unsigned long *signed_p,
8987 unsigned long *trunc_p,
8988 unsigned long encoded)
d9352518 8989{
07d6d2b8
AM
8990 * start = encoded & 0x3F;
8991 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8992 * oplen = (encoded >> 12) & 0x3F;
8993 * wordsz = (encoded >> 18) & 0xF;
8994 * chunksz = (encoded >> 22) & 0xF;
8995 * lsb0_p = (encoded >> 27) & 1;
8996 * signed_p = (encoded >> 28) & 1;
8997 * trunc_p = (encoded >> 29) & 1;
8998}
8999
cdfeee4f 9000bfd_reloc_status_type
0f02bbd9 9001bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9002 asection *input_section,
0f02bbd9
AM
9003 bfd_byte *contents,
9004 Elf_Internal_Rela *rel,
9005 bfd_vma relocation)
d9352518 9006{
0f02bbd9
AM
9007 bfd_vma shift, x, mask;
9008 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9009 bfd_reloc_status_type r;
bb294208 9010 bfd_size_type octets;
d9352518
DB
9011
9012 /* Perform this reloc, since it is complex.
9013 (this is not to say that it necessarily refers to a complex
9014 symbol; merely that it is a self-describing CGEN based reloc.
9015 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9016 word size, etc) encoded within it.). */
d9352518 9017
a0c8462f
AM
9018 decode_complex_addend (&start, &oplen, &len, &wordsz,
9019 &chunksz, &lsb0_p, &signed_p,
9020 &trunc_p, rel->r_addend);
d9352518
DB
9021
9022 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9023
9024 if (lsb0_p)
9025 shift = (start + 1) - len;
9026 else
9027 shift = (8 * wordsz) - (start + len);
9028
bb294208
AM
9029 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9030 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9031
9032#ifdef DEBUG
9033 printf ("Doing complex reloc: "
9034 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9035 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9036 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9037 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9038 oplen, (unsigned long) x, (unsigned long) mask,
9039 (unsigned long) relocation);
d9352518
DB
9040#endif
9041
cdfeee4f 9042 r = bfd_reloc_ok;
d9352518 9043 if (! trunc_p)
cdfeee4f
AM
9044 /* Now do an overflow check. */
9045 r = bfd_check_overflow ((signed_p
9046 ? complain_overflow_signed
9047 : complain_overflow_unsigned),
9048 len, 0, (8 * wordsz),
9049 relocation);
a0c8462f 9050
d9352518
DB
9051 /* Do the deed. */
9052 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9053
9054#ifdef DEBUG
9055 printf (" relocation: %8.8lx\n"
9056 " shifted mask: %8.8lx\n"
9057 " shifted/masked reloc: %8.8lx\n"
9058 " result: %8.8lx\n",
9ccb8af9
AM
9059 (unsigned long) relocation, (unsigned long) (mask << shift),
9060 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9061#endif
bb294208 9062 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9063 return r;
d9352518
DB
9064}
9065
0e287786
AM
9066/* Functions to read r_offset from external (target order) reloc
9067 entry. Faster than bfd_getl32 et al, because we let the compiler
9068 know the value is aligned. */
53df40a4 9069
0e287786
AM
9070static bfd_vma
9071ext32l_r_offset (const void *p)
53df40a4
AM
9072{
9073 union aligned32
9074 {
9075 uint32_t v;
9076 unsigned char c[4];
9077 };
9078 const union aligned32 *a
0e287786 9079 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9080
9081 uint32_t aval = ( (uint32_t) a->c[0]
9082 | (uint32_t) a->c[1] << 8
9083 | (uint32_t) a->c[2] << 16
9084 | (uint32_t) a->c[3] << 24);
0e287786 9085 return aval;
53df40a4
AM
9086}
9087
0e287786
AM
9088static bfd_vma
9089ext32b_r_offset (const void *p)
53df40a4
AM
9090{
9091 union aligned32
9092 {
9093 uint32_t v;
9094 unsigned char c[4];
9095 };
9096 const union aligned32 *a
0e287786 9097 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9098
9099 uint32_t aval = ( (uint32_t) a->c[0] << 24
9100 | (uint32_t) a->c[1] << 16
9101 | (uint32_t) a->c[2] << 8
9102 | (uint32_t) a->c[3]);
0e287786 9103 return aval;
53df40a4
AM
9104}
9105
9106#ifdef BFD_HOST_64_BIT
0e287786
AM
9107static bfd_vma
9108ext64l_r_offset (const void *p)
53df40a4
AM
9109{
9110 union aligned64
9111 {
9112 uint64_t v;
9113 unsigned char c[8];
9114 };
9115 const union aligned64 *a
0e287786 9116 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9117
9118 uint64_t aval = ( (uint64_t) a->c[0]
9119 | (uint64_t) a->c[1] << 8
9120 | (uint64_t) a->c[2] << 16
9121 | (uint64_t) a->c[3] << 24
9122 | (uint64_t) a->c[4] << 32
9123 | (uint64_t) a->c[5] << 40
9124 | (uint64_t) a->c[6] << 48
9125 | (uint64_t) a->c[7] << 56);
0e287786 9126 return aval;
53df40a4
AM
9127}
9128
0e287786
AM
9129static bfd_vma
9130ext64b_r_offset (const void *p)
53df40a4
AM
9131{
9132 union aligned64
9133 {
9134 uint64_t v;
9135 unsigned char c[8];
9136 };
9137 const union aligned64 *a
0e287786 9138 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9139
9140 uint64_t aval = ( (uint64_t) a->c[0] << 56
9141 | (uint64_t) a->c[1] << 48
9142 | (uint64_t) a->c[2] << 40
9143 | (uint64_t) a->c[3] << 32
9144 | (uint64_t) a->c[4] << 24
9145 | (uint64_t) a->c[5] << 16
9146 | (uint64_t) a->c[6] << 8
9147 | (uint64_t) a->c[7]);
0e287786 9148 return aval;
53df40a4
AM
9149}
9150#endif
9151
c152c796
AM
9152/* When performing a relocatable link, the input relocations are
9153 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9154 referenced must be updated. Update all the relocations found in
9155 RELDATA. */
c152c796 9156
bca6d0e3 9157static bfd_boolean
c152c796 9158elf_link_adjust_relocs (bfd *abfd,
9eaff861 9159 asection *sec,
28dbcedc 9160 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9161 bfd_boolean sort,
9162 struct bfd_link_info *info)
c152c796
AM
9163{
9164 unsigned int i;
9165 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9166 bfd_byte *erela;
9167 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9168 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9169 bfd_vma r_type_mask;
9170 int r_sym_shift;
d4730f92
BS
9171 unsigned int count = reldata->count;
9172 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9173
d4730f92 9174 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9175 {
9176 swap_in = bed->s->swap_reloc_in;
9177 swap_out = bed->s->swap_reloc_out;
9178 }
d4730f92 9179 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9180 {
9181 swap_in = bed->s->swap_reloca_in;
9182 swap_out = bed->s->swap_reloca_out;
9183 }
9184 else
9185 abort ();
9186
9187 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9188 abort ();
9189
9190 if (bed->s->arch_size == 32)
9191 {
9192 r_type_mask = 0xff;
9193 r_sym_shift = 8;
9194 }
9195 else
9196 {
9197 r_type_mask = 0xffffffff;
9198 r_sym_shift = 32;
9199 }
9200
d4730f92
BS
9201 erela = reldata->hdr->contents;
9202 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9203 {
9204 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9205 unsigned int j;
9206
9207 if (*rel_hash == NULL)
9208 continue;
9209
10bbbc1d
NC
9210 if ((*rel_hash)->indx == -2
9211 && info->gc_sections
9212 && ! info->gc_keep_exported)
9213 {
9214 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9215 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9216 abfd, sec,
9217 (*rel_hash)->root.root.string);
9793eb77 9218 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9219 abfd, sec);
10bbbc1d
NC
9220 bfd_set_error (bfd_error_invalid_operation);
9221 return FALSE;
9222 }
c152c796
AM
9223 BFD_ASSERT ((*rel_hash)->indx >= 0);
9224
9225 (*swap_in) (abfd, erela, irela);
9226 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9227 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9228 | (irela[j].r_info & r_type_mask));
9229 (*swap_out) (abfd, irela, erela);
9230 }
53df40a4 9231
9eaff861
AO
9232 if (bed->elf_backend_update_relocs)
9233 (*bed->elf_backend_update_relocs) (sec, reldata);
9234
0e287786 9235 if (sort && count != 0)
53df40a4 9236 {
0e287786
AM
9237 bfd_vma (*ext_r_off) (const void *);
9238 bfd_vma r_off;
9239 size_t elt_size;
9240 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9241 bfd_byte *buf = NULL;
28dbcedc
AM
9242
9243 if (bed->s->arch_size == 32)
9244 {
9245 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9246 ext_r_off = ext32l_r_offset;
28dbcedc 9247 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9248 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9249 else
9250 abort ();
9251 }
53df40a4 9252 else
28dbcedc 9253 {
53df40a4 9254#ifdef BFD_HOST_64_BIT
28dbcedc 9255 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9256 ext_r_off = ext64l_r_offset;
28dbcedc 9257 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9258 ext_r_off = ext64b_r_offset;
28dbcedc 9259 else
53df40a4 9260#endif
28dbcedc
AM
9261 abort ();
9262 }
0e287786 9263
bca6d0e3
AM
9264 /* Must use a stable sort here. A modified insertion sort,
9265 since the relocs are mostly sorted already. */
0e287786
AM
9266 elt_size = reldata->hdr->sh_entsize;
9267 base = reldata->hdr->contents;
9268 end = base + count * elt_size;
bca6d0e3 9269 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9270 abort ();
9271
9272 /* Ensure the first element is lowest. This acts as a sentinel,
9273 speeding the main loop below. */
9274 r_off = (*ext_r_off) (base);
9275 for (p = loc = base; (p += elt_size) < end; )
9276 {
9277 bfd_vma r_off2 = (*ext_r_off) (p);
9278 if (r_off > r_off2)
9279 {
9280 r_off = r_off2;
9281 loc = p;
9282 }
9283 }
9284 if (loc != base)
9285 {
9286 /* Don't just swap *base and *loc as that changes the order
9287 of the original base[0] and base[1] if they happen to
9288 have the same r_offset. */
bca6d0e3
AM
9289 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9290 memcpy (onebuf, loc, elt_size);
0e287786 9291 memmove (base + elt_size, base, loc - base);
bca6d0e3 9292 memcpy (base, onebuf, elt_size);
0e287786
AM
9293 }
9294
b29b8669 9295 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9296 {
9297 /* base to p is sorted, *p is next to insert. */
9298 r_off = (*ext_r_off) (p);
9299 /* Search the sorted region for location to insert. */
9300 loc = p - elt_size;
9301 while (r_off < (*ext_r_off) (loc))
9302 loc -= elt_size;
9303 loc += elt_size;
9304 if (loc != p)
9305 {
bca6d0e3
AM
9306 /* Chances are there is a run of relocs to insert here,
9307 from one of more input files. Files are not always
9308 linked in order due to the way elf_link_input_bfd is
9309 called. See pr17666. */
9310 size_t sortlen = p - loc;
9311 bfd_vma r_off2 = (*ext_r_off) (loc);
9312 size_t runlen = elt_size;
9313 size_t buf_size = 96 * 1024;
9314 while (p + runlen < end
9315 && (sortlen <= buf_size
9316 || runlen + elt_size <= buf_size)
9317 && r_off2 > (*ext_r_off) (p + runlen))
9318 runlen += elt_size;
9319 if (buf == NULL)
9320 {
9321 buf = bfd_malloc (buf_size);
9322 if (buf == NULL)
9323 return FALSE;
9324 }
9325 if (runlen < sortlen)
9326 {
9327 memcpy (buf, p, runlen);
9328 memmove (loc + runlen, loc, sortlen);
9329 memcpy (loc, buf, runlen);
9330 }
9331 else
9332 {
9333 memcpy (buf, loc, sortlen);
9334 memmove (loc, p, runlen);
9335 memcpy (loc + runlen, buf, sortlen);
9336 }
b29b8669 9337 p += runlen - elt_size;
0e287786
AM
9338 }
9339 }
9340 /* Hashes are no longer valid. */
28dbcedc
AM
9341 free (reldata->hashes);
9342 reldata->hashes = NULL;
bca6d0e3 9343 free (buf);
53df40a4 9344 }
bca6d0e3 9345 return TRUE;
c152c796
AM
9346}
9347
9348struct elf_link_sort_rela
9349{
9350 union {
9351 bfd_vma offset;
9352 bfd_vma sym_mask;
9353 } u;
9354 enum elf_reloc_type_class type;
9355 /* We use this as an array of size int_rels_per_ext_rel. */
9356 Elf_Internal_Rela rela[1];
9357};
9358
dcea6a95
AM
9359/* qsort stability here and for cmp2 is only an issue if multiple
9360 dynamic relocations are emitted at the same address. But targets
9361 that apply a series of dynamic relocations each operating on the
9362 result of the prior relocation can't use -z combreloc as
9363 implemented anyway. Such schemes tend to be broken by sorting on
9364 symbol index. That leaves dynamic NONE relocs as the only other
9365 case where ld might emit multiple relocs at the same address, and
9366 those are only emitted due to target bugs. */
9367
c152c796
AM
9368static int
9369elf_link_sort_cmp1 (const void *A, const void *B)
9370{
a50b1753
NC
9371 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9372 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9373 int relativea, relativeb;
9374
9375 relativea = a->type == reloc_class_relative;
9376 relativeb = b->type == reloc_class_relative;
9377
9378 if (relativea < relativeb)
9379 return 1;
9380 if (relativea > relativeb)
9381 return -1;
9382 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9383 return -1;
9384 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9385 return 1;
9386 if (a->rela->r_offset < b->rela->r_offset)
9387 return -1;
9388 if (a->rela->r_offset > b->rela->r_offset)
9389 return 1;
9390 return 0;
9391}
9392
9393static int
9394elf_link_sort_cmp2 (const void *A, const void *B)
9395{
a50b1753
NC
9396 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9397 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9398
7e612e98 9399 if (a->type < b->type)
c152c796 9400 return -1;
7e612e98 9401 if (a->type > b->type)
c152c796 9402 return 1;
7e612e98 9403 if (a->u.offset < b->u.offset)
c152c796 9404 return -1;
7e612e98 9405 if (a->u.offset > b->u.offset)
c152c796
AM
9406 return 1;
9407 if (a->rela->r_offset < b->rela->r_offset)
9408 return -1;
9409 if (a->rela->r_offset > b->rela->r_offset)
9410 return 1;
9411 return 0;
9412}
9413
9414static size_t
9415elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9416{
3410fea8 9417 asection *dynamic_relocs;
fc66a176
L
9418 asection *rela_dyn;
9419 asection *rel_dyn;
c152c796
AM
9420 bfd_size_type count, size;
9421 size_t i, ret, sort_elt, ext_size;
9422 bfd_byte *sort, *s_non_relative, *p;
9423 struct elf_link_sort_rela *sq;
9424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9425 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9426 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9427 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9428 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9429 struct bfd_link_order *lo;
9430 bfd_vma r_sym_mask;
3410fea8 9431 bfd_boolean use_rela;
c152c796 9432
3410fea8
NC
9433 /* Find a dynamic reloc section. */
9434 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9435 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9436 if (rela_dyn != NULL && rela_dyn->size > 0
9437 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9438 {
3410fea8
NC
9439 bfd_boolean use_rela_initialised = FALSE;
9440
9441 /* This is just here to stop gcc from complaining.
c8e44c6d 9442 Its initialization checking code is not perfect. */
3410fea8
NC
9443 use_rela = TRUE;
9444
9445 /* Both sections are present. Examine the sizes
9446 of the indirect sections to help us choose. */
9447 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9448 if (lo->type == bfd_indirect_link_order)
9449 {
9450 asection *o = lo->u.indirect.section;
9451
9452 if ((o->size % bed->s->sizeof_rela) == 0)
9453 {
9454 if ((o->size % bed->s->sizeof_rel) == 0)
9455 /* Section size is divisible by both rel and rela sizes.
9456 It is of no help to us. */
9457 ;
9458 else
9459 {
9460 /* Section size is only divisible by rela. */
535b785f 9461 if (use_rela_initialised && !use_rela)
3410fea8 9462 {
9793eb77 9463 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9464 "they are in more than one size"),
9465 abfd);
3410fea8
NC
9466 bfd_set_error (bfd_error_invalid_operation);
9467 return 0;
9468 }
9469 else
9470 {
9471 use_rela = TRUE;
9472 use_rela_initialised = TRUE;
9473 }
9474 }
9475 }
9476 else if ((o->size % bed->s->sizeof_rel) == 0)
9477 {
9478 /* Section size is only divisible by rel. */
535b785f 9479 if (use_rela_initialised && use_rela)
3410fea8 9480 {
9793eb77 9481 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9482 "they are in more than one size"),
9483 abfd);
3410fea8
NC
9484 bfd_set_error (bfd_error_invalid_operation);
9485 return 0;
9486 }
9487 else
9488 {
9489 use_rela = FALSE;
9490 use_rela_initialised = TRUE;
9491 }
9492 }
9493 else
9494 {
c8e44c6d
AM
9495 /* The section size is not divisible by either -
9496 something is wrong. */
9793eb77 9497 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9498 "they are of an unknown size"), abfd);
3410fea8
NC
9499 bfd_set_error (bfd_error_invalid_operation);
9500 return 0;
9501 }
9502 }
9503
9504 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9505 if (lo->type == bfd_indirect_link_order)
9506 {
9507 asection *o = lo->u.indirect.section;
9508
9509 if ((o->size % bed->s->sizeof_rela) == 0)
9510 {
9511 if ((o->size % bed->s->sizeof_rel) == 0)
9512 /* Section size is divisible by both rel and rela sizes.
9513 It is of no help to us. */
9514 ;
9515 else
9516 {
9517 /* Section size is only divisible by rela. */
535b785f 9518 if (use_rela_initialised && !use_rela)
3410fea8 9519 {
9793eb77 9520 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9521 "they are in more than one size"),
9522 abfd);
3410fea8
NC
9523 bfd_set_error (bfd_error_invalid_operation);
9524 return 0;
9525 }
9526 else
9527 {
9528 use_rela = TRUE;
9529 use_rela_initialised = TRUE;
9530 }
9531 }
9532 }
9533 else if ((o->size % bed->s->sizeof_rel) == 0)
9534 {
9535 /* Section size is only divisible by rel. */
535b785f 9536 if (use_rela_initialised && use_rela)
3410fea8 9537 {
9793eb77 9538 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9539 "they are in more than one size"),
9540 abfd);
3410fea8
NC
9541 bfd_set_error (bfd_error_invalid_operation);
9542 return 0;
9543 }
9544 else
9545 {
9546 use_rela = FALSE;
9547 use_rela_initialised = TRUE;
9548 }
9549 }
9550 else
9551 {
c8e44c6d
AM
9552 /* The section size is not divisible by either -
9553 something is wrong. */
9793eb77 9554 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9555 "they are of an unknown size"), abfd);
3410fea8
NC
9556 bfd_set_error (bfd_error_invalid_operation);
9557 return 0;
9558 }
9559 }
9560
9561 if (! use_rela_initialised)
9562 /* Make a guess. */
9563 use_rela = TRUE;
c152c796 9564 }
fc66a176
L
9565 else if (rela_dyn != NULL && rela_dyn->size > 0)
9566 use_rela = TRUE;
9567 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9568 use_rela = FALSE;
c152c796 9569 else
fc66a176 9570 return 0;
3410fea8
NC
9571
9572 if (use_rela)
c152c796 9573 {
3410fea8 9574 dynamic_relocs = rela_dyn;
c152c796
AM
9575 ext_size = bed->s->sizeof_rela;
9576 swap_in = bed->s->swap_reloca_in;
9577 swap_out = bed->s->swap_reloca_out;
9578 }
3410fea8
NC
9579 else
9580 {
9581 dynamic_relocs = rel_dyn;
9582 ext_size = bed->s->sizeof_rel;
9583 swap_in = bed->s->swap_reloc_in;
9584 swap_out = bed->s->swap_reloc_out;
9585 }
c152c796
AM
9586
9587 size = 0;
3410fea8 9588 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9589 if (lo->type == bfd_indirect_link_order)
3410fea8 9590 size += lo->u.indirect.section->size;
c152c796 9591
3410fea8 9592 if (size != dynamic_relocs->size)
c152c796
AM
9593 return 0;
9594
9595 sort_elt = (sizeof (struct elf_link_sort_rela)
9596 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9597
9598 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9599 if (count == 0)
9600 return 0;
a50b1753 9601 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9602
c152c796
AM
9603 if (sort == NULL)
9604 {
9605 (*info->callbacks->warning)
9793eb77 9606 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9607 return 0;
9608 }
9609
9610 if (bed->s->arch_size == 32)
9611 r_sym_mask = ~(bfd_vma) 0xff;
9612 else
9613 r_sym_mask = ~(bfd_vma) 0xffffffff;
9614
3410fea8 9615 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9616 if (lo->type == bfd_indirect_link_order)
9617 {
9618 bfd_byte *erel, *erelend;
9619 asection *o = lo->u.indirect.section;
9620
1da212d6
AM
9621 if (o->contents == NULL && o->size != 0)
9622 {
9623 /* This is a reloc section that is being handled as a normal
9624 section. See bfd_section_from_shdr. We can't combine
9625 relocs in this case. */
9626 free (sort);
9627 return 0;
9628 }
c152c796 9629 erel = o->contents;
eea6121a 9630 erelend = o->contents + o->size;
c8e44c6d 9631 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9632
c152c796
AM
9633 while (erel < erelend)
9634 {
9635 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9636
c152c796 9637 (*swap_in) (abfd, erel, s->rela);
7e612e98 9638 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9639 s->u.sym_mask = r_sym_mask;
9640 p += sort_elt;
9641 erel += ext_size;
9642 }
9643 }
9644
9645 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9646
9647 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9648 {
9649 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9650 if (s->type != reloc_class_relative)
9651 break;
9652 }
9653 ret = i;
9654 s_non_relative = p;
9655
9656 sq = (struct elf_link_sort_rela *) s_non_relative;
9657 for (; i < count; i++, p += sort_elt)
9658 {
9659 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9660 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9661 sq = sp;
9662 sp->u.offset = sq->rela->r_offset;
9663 }
9664
9665 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9666
c8e44c6d
AM
9667 struct elf_link_hash_table *htab = elf_hash_table (info);
9668 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9669 {
9670 /* We have plt relocs in .rela.dyn. */
9671 sq = (struct elf_link_sort_rela *) sort;
9672 for (i = 0; i < count; i++)
9673 if (sq[count - i - 1].type != reloc_class_plt)
9674 break;
9675 if (i != 0 && htab->srelplt->size == i * ext_size)
9676 {
9677 struct bfd_link_order **plo;
9678 /* Put srelplt link_order last. This is so the output_offset
9679 set in the next loop is correct for DT_JMPREL. */
9680 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9681 if ((*plo)->type == bfd_indirect_link_order
9682 && (*plo)->u.indirect.section == htab->srelplt)
9683 {
9684 lo = *plo;
9685 *plo = lo->next;
9686 }
9687 else
9688 plo = &(*plo)->next;
9689 *plo = lo;
9690 lo->next = NULL;
9691 dynamic_relocs->map_tail.link_order = lo;
9692 }
9693 }
9694
9695 p = sort;
3410fea8 9696 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9697 if (lo->type == bfd_indirect_link_order)
9698 {
9699 bfd_byte *erel, *erelend;
9700 asection *o = lo->u.indirect.section;
9701
9702 erel = o->contents;
eea6121a 9703 erelend = o->contents + o->size;
c8e44c6d 9704 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9705 while (erel < erelend)
9706 {
9707 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9708 (*swap_out) (abfd, s->rela, erel);
9709 p += sort_elt;
9710 erel += ext_size;
9711 }
9712 }
9713
9714 free (sort);
3410fea8 9715 *psec = dynamic_relocs;
c152c796
AM
9716 return ret;
9717}
9718
ef10c3ac 9719/* Add a symbol to the output symbol string table. */
c152c796 9720
6e0b88f1 9721static int
ef10c3ac
L
9722elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9723 const char *name,
9724 Elf_Internal_Sym *elfsym,
9725 asection *input_sec,
9726 struct elf_link_hash_entry *h)
c152c796 9727{
6e0b88f1 9728 int (*output_symbol_hook)
c152c796
AM
9729 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9730 struct elf_link_hash_entry *);
ef10c3ac 9731 struct elf_link_hash_table *hash_table;
c152c796 9732 const struct elf_backend_data *bed;
ef10c3ac 9733 bfd_size_type strtabsize;
c152c796 9734
8539e4e8
AM
9735 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9736
8b127cbc 9737 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9738 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9739 if (output_symbol_hook != NULL)
9740 {
8b127cbc 9741 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9742 if (ret != 1)
9743 return ret;
c152c796
AM
9744 }
9745
06f44071
AM
9746 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9747 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9748 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9749 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9750
ef10c3ac
L
9751 if (name == NULL
9752 || *name == '\0'
9753 || (input_sec->flags & SEC_EXCLUDE))
9754 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9755 else
9756 {
ef10c3ac
L
9757 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9758 to get the final offset for st_name. */
3f2e9699 9759 char *versioned_name = (char *) name;
496afd17
L
9760 if (h != NULL)
9761 {
9762 if (h->versioned == versioned && h->def_dynamic)
9763 {
9764 /* Keep only one '@' for versioned symbols defined in
9765 shared objects. */
9766 char *version = strrchr (name, ELF_VER_CHR);
9767 char *base_end = strchr (name, ELF_VER_CHR);
9768 if (version != base_end)
9769 {
9770 size_t base_len;
9771 size_t len = strlen (name);
9772 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9773 if (versioned_name == NULL)
9774 return 0;
9775 base_len = base_end - name;
9776 memcpy (versioned_name, name, base_len);
9777 memcpy (versioned_name + base_len, version,
9778 len - base_len);
9779 }
9780 }
9781 }
9782 else if (flinfo->info->unique_symbol
9783 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9784 {
496afd17
L
9785 struct local_hash_entry *lh;
9786 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9787 {
496afd17
L
9788 case STT_FILE:
9789 case STT_SECTION:
9790 break;
9791 default:
9792 lh = (struct local_hash_entry *) bfd_hash_lookup
9793 (&flinfo->local_hash_table, name, TRUE, FALSE);
9794 if (lh == NULL)
3f2e9699 9795 return 0;
496afd17
L
9796 if (lh->count)
9797 {
9798 /* Append ".COUNT" to duplicated local symbols. */
9799 size_t count_len;
9800 size_t base_len = lh->size;
9801 char buf[30];
9802 sprintf (buf, "%lx", lh->count);
9803 if (!base_len)
9804 {
9805 base_len = strlen (name);
9806 lh->size = base_len;
9807 }
9808 count_len = strlen (buf);
9809 versioned_name = bfd_alloc (flinfo->output_bfd,
9810 base_len + count_len + 2);
9811 if (versioned_name == NULL)
9812 return 0;
9813 memcpy (versioned_name, name, base_len);
9814 versioned_name[base_len] = '.';
9815 memcpy (versioned_name + base_len + 1, buf,
9816 count_len + 1);
9817 }
9818 lh->count++;
9819 break;
3f2e9699
L
9820 }
9821 }
ef10c3ac
L
9822 elfsym->st_name
9823 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9824 versioned_name, FALSE);
c152c796 9825 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9826 return 0;
c152c796
AM
9827 }
9828
ef10c3ac
L
9829 hash_table = elf_hash_table (flinfo->info);
9830 strtabsize = hash_table->strtabsize;
9831 if (strtabsize <= hash_table->strtabcount)
c152c796 9832 {
ef10c3ac
L
9833 strtabsize += strtabsize;
9834 hash_table->strtabsize = strtabsize;
9835 strtabsize *= sizeof (*hash_table->strtab);
9836 hash_table->strtab
9837 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9838 strtabsize);
9839 if (hash_table->strtab == NULL)
6e0b88f1 9840 return 0;
c152c796 9841 }
ef10c3ac
L
9842 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9843 hash_table->strtab[hash_table->strtabcount].dest_index
9844 = hash_table->strtabcount;
9845 hash_table->strtab[hash_table->strtabcount].destshndx_index
9846 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9847
ed48ec2e 9848 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9849 hash_table->strtabcount += 1;
9850
9851 return 1;
9852}
9853
9854/* Swap symbols out to the symbol table and flush the output symbols to
9855 the file. */
9856
9857static bfd_boolean
9858elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9859{
9860 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9861 size_t amt;
ef53be89 9862 size_t i;
ef10c3ac
L
9863 const struct elf_backend_data *bed;
9864 bfd_byte *symbuf;
9865 Elf_Internal_Shdr *hdr;
9866 file_ptr pos;
9867 bfd_boolean ret;
9868
9869 if (!hash_table->strtabcount)
9870 return TRUE;
9871
9872 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9873
9874 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9875
ef10c3ac
L
9876 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9877 symbuf = (bfd_byte *) bfd_malloc (amt);
9878 if (symbuf == NULL)
9879 return FALSE;
1b786873 9880
ef10c3ac 9881 if (flinfo->symshndxbuf)
c152c796 9882 {
ef53be89
AM
9883 amt = sizeof (Elf_External_Sym_Shndx);
9884 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9885 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9886 if (flinfo->symshndxbuf == NULL)
c152c796 9887 {
ef10c3ac
L
9888 free (symbuf);
9889 return FALSE;
c152c796 9890 }
c152c796
AM
9891 }
9892
3d16b64e 9893 /* Now swap out the symbols. */
ef10c3ac
L
9894 for (i = 0; i < hash_table->strtabcount; i++)
9895 {
9896 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9897 if (elfsym->sym.st_name == (unsigned long) -1)
9898 elfsym->sym.st_name = 0;
9899 else
9900 elfsym->sym.st_name
9901 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9902 elfsym->sym.st_name);
3d16b64e
NA
9903
9904 /* Inform the linker of the addition of this symbol. */
9905
9906 if (flinfo->info->callbacks->ctf_new_symbol)
9907 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9908 &elfsym->sym);
9909
ef10c3ac
L
9910 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9911 ((bfd_byte *) symbuf
9912 + (elfsym->dest_index
9913 * bed->s->sizeof_sym)),
9914 (flinfo->symshndxbuf
9915 + elfsym->destshndx_index));
9916 }
9917
9918 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9919 pos = hdr->sh_offset + hdr->sh_size;
9920 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9921 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9922 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9923 {
9924 hdr->sh_size += amt;
9925 ret = TRUE;
9926 }
9927 else
9928 ret = FALSE;
c152c796 9929
ef10c3ac
L
9930 free (symbuf);
9931
9932 free (hash_table->strtab);
9933 hash_table->strtab = NULL;
9934
9935 return ret;
c152c796
AM
9936}
9937
c0d5a53d
L
9938/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9939
9940static bfd_boolean
9941check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9942{
4fbb74a6
AM
9943 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9944 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9945 {
9946 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9947 beyond 64k. */
4eca0228 9948 _bfd_error_handler
695344c0 9949 /* xgettext:c-format */
9793eb77 9950 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9951 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9952 bfd_set_error (bfd_error_nonrepresentable_section);
9953 return FALSE;
9954 }
9955 return TRUE;
9956}
9957
c152c796
AM
9958/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9959 allowing an unsatisfied unversioned symbol in the DSO to match a
9960 versioned symbol that would normally require an explicit version.
9961 We also handle the case that a DSO references a hidden symbol
9962 which may be satisfied by a versioned symbol in another DSO. */
9963
9964static bfd_boolean
9965elf_link_check_versioned_symbol (struct bfd_link_info *info,
9966 const struct elf_backend_data *bed,
9967 struct elf_link_hash_entry *h)
9968{
9969 bfd *abfd;
9970 struct elf_link_loaded_list *loaded;
9971
9972 if (!is_elf_hash_table (info->hash))
9973 return FALSE;
9974
90c984fc
L
9975 /* Check indirect symbol. */
9976 while (h->root.type == bfd_link_hash_indirect)
9977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9978
c152c796
AM
9979 switch (h->root.type)
9980 {
9981 default:
9982 abfd = NULL;
9983 break;
9984
9985 case bfd_link_hash_undefined:
9986 case bfd_link_hash_undefweak:
9987 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9988 if (abfd == NULL
9989 || (abfd->flags & DYNAMIC) == 0
e56f61be 9990 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9991 return FALSE;
9992 break;
9993
9994 case bfd_link_hash_defined:
9995 case bfd_link_hash_defweak:
9996 abfd = h->root.u.def.section->owner;
9997 break;
9998
9999 case bfd_link_hash_common:
10000 abfd = h->root.u.c.p->section->owner;
10001 break;
10002 }
10003 BFD_ASSERT (abfd != NULL);
10004
e310298c 10005 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10006 loaded != NULL;
10007 loaded = loaded->next)
10008 {
10009 bfd *input;
10010 Elf_Internal_Shdr *hdr;
ef53be89
AM
10011 size_t symcount;
10012 size_t extsymcount;
10013 size_t extsymoff;
c152c796
AM
10014 Elf_Internal_Shdr *versymhdr;
10015 Elf_Internal_Sym *isym;
10016 Elf_Internal_Sym *isymend;
10017 Elf_Internal_Sym *isymbuf;
10018 Elf_External_Versym *ever;
10019 Elf_External_Versym *extversym;
10020
10021 input = loaded->abfd;
10022
10023 /* We check each DSO for a possible hidden versioned definition. */
10024 if (input == abfd
c152c796
AM
10025 || elf_dynversym (input) == 0)
10026 continue;
10027
10028 hdr = &elf_tdata (input)->dynsymtab_hdr;
10029
10030 symcount = hdr->sh_size / bed->s->sizeof_sym;
10031 if (elf_bad_symtab (input))
10032 {
10033 extsymcount = symcount;
10034 extsymoff = 0;
10035 }
10036 else
10037 {
10038 extsymcount = symcount - hdr->sh_info;
10039 extsymoff = hdr->sh_info;
10040 }
10041
10042 if (extsymcount == 0)
10043 continue;
10044
10045 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10046 NULL, NULL, NULL);
10047 if (isymbuf == NULL)
10048 return FALSE;
10049
10050 /* Read in any version definitions. */
10051 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10052 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10053 || (extversym = (Elf_External_Versym *)
10054 _bfd_malloc_and_read (input, versymhdr->sh_size,
10055 versymhdr->sh_size)) == NULL)
c152c796 10056 {
c152c796
AM
10057 free (isymbuf);
10058 return FALSE;
10059 }
10060
10061 ever = extversym + extsymoff;
10062 isymend = isymbuf + extsymcount;
10063 for (isym = isymbuf; isym < isymend; isym++, ever++)
10064 {
10065 const char *name;
10066 Elf_Internal_Versym iver;
10067 unsigned short version_index;
10068
10069 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10070 || isym->st_shndx == SHN_UNDEF)
10071 continue;
10072
10073 name = bfd_elf_string_from_elf_section (input,
10074 hdr->sh_link,
10075 isym->st_name);
10076 if (strcmp (name, h->root.root.string) != 0)
10077 continue;
10078
10079 _bfd_elf_swap_versym_in (input, ever, &iver);
10080
d023c380
L
10081 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10082 && !(h->def_regular
10083 && h->forced_local))
c152c796
AM
10084 {
10085 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10086 have provided a definition for the undefined sym unless
10087 it is defined in a non-shared object and forced local.
10088 */
c152c796
AM
10089 abort ();
10090 }
10091
10092 version_index = iver.vs_vers & VERSYM_VERSION;
10093 if (version_index == 1 || version_index == 2)
10094 {
10095 /* This is the base or first version. We can use it. */
10096 free (extversym);
10097 free (isymbuf);
10098 return TRUE;
10099 }
10100 }
10101
10102 free (extversym);
10103 free (isymbuf);
10104 }
10105
10106 return FALSE;
10107}
10108
b8871f35
L
10109/* Convert ELF common symbol TYPE. */
10110
10111static int
10112elf_link_convert_common_type (struct bfd_link_info *info, int type)
10113{
10114 /* Commom symbol can only appear in relocatable link. */
10115 if (!bfd_link_relocatable (info))
10116 abort ();
10117 switch (info->elf_stt_common)
10118 {
10119 case unchanged:
10120 break;
10121 case elf_stt_common:
10122 type = STT_COMMON;
10123 break;
10124 case no_elf_stt_common:
10125 type = STT_OBJECT;
10126 break;
10127 }
10128 return type;
10129}
10130
c152c796
AM
10131/* Add an external symbol to the symbol table. This is called from
10132 the hash table traversal routine. When generating a shared object,
10133 we go through the symbol table twice. The first time we output
10134 anything that might have been forced to local scope in a version
10135 script. The second time we output the symbols that are still
10136 global symbols. */
10137
10138static bfd_boolean
7686d77d 10139elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10140{
7686d77d 10141 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10142 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10143 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10144 bfd_boolean strip;
10145 Elf_Internal_Sym sym;
10146 asection *input_sec;
10147 const struct elf_backend_data *bed;
6e0b88f1
AM
10148 long indx;
10149 int ret;
b8871f35 10150 unsigned int type;
c152c796
AM
10151
10152 if (h->root.type == bfd_link_hash_warning)
10153 {
10154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10155 if (h->root.type == bfd_link_hash_new)
10156 return TRUE;
10157 }
10158
10159 /* Decide whether to output this symbol in this pass. */
10160 if (eoinfo->localsyms)
10161 {
4deb8f71 10162 if (!h->forced_local)
c152c796
AM
10163 return TRUE;
10164 }
10165 else
10166 {
4deb8f71 10167 if (h->forced_local)
c152c796
AM
10168 return TRUE;
10169 }
10170
8b127cbc 10171 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10172
12ac1cf5 10173 if (h->root.type == bfd_link_hash_undefined)
c152c796 10174 {
12ac1cf5
NC
10175 /* If we have an undefined symbol reference here then it must have
10176 come from a shared library that is being linked in. (Undefined
98da7939
L
10177 references in regular files have already been handled unless
10178 they are in unreferenced sections which are removed by garbage
10179 collection). */
12ac1cf5
NC
10180 bfd_boolean ignore_undef = FALSE;
10181
10182 /* Some symbols may be special in that the fact that they're
10183 undefined can be safely ignored - let backend determine that. */
10184 if (bed->elf_backend_ignore_undef_symbol)
10185 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10186
10187 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10188 if (!ignore_undef
c54f1524 10189 && h->ref_dynamic_nonweak
8b127cbc
AM
10190 && (!h->ref_regular || flinfo->info->gc_sections)
10191 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10192 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10193 {
10194 flinfo->info->callbacks->undefined_symbol
10195 (flinfo->info, h->root.root.string,
10196 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10197 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10198 && !flinfo->info->warn_unresolved_syms);
10199 }
97196564
L
10200
10201 /* Strip a global symbol defined in a discarded section. */
10202 if (h->indx == -3)
10203 return TRUE;
c152c796
AM
10204 }
10205
10206 /* We should also warn if a forced local symbol is referenced from
10207 shared libraries. */
0e1862bb 10208 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10209 && h->forced_local
10210 && h->ref_dynamic
371a5866 10211 && h->def_regular
f5385ebf 10212 && !h->dynamic_def
ee659f1f 10213 && h->ref_dynamic_nonweak
8b127cbc 10214 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10215 {
17d078c5
AM
10216 bfd *def_bfd;
10217 const char *msg;
90c984fc
L
10218 struct elf_link_hash_entry *hi = h;
10219
10220 /* Check indirect symbol. */
10221 while (hi->root.type == bfd_link_hash_indirect)
10222 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10223
10224 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10225 /* xgettext:c-format */
871b3ab2 10226 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10227 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10228 /* xgettext:c-format */
871b3ab2 10229 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10230 else
695344c0 10231 /* xgettext:c-format */
871b3ab2 10232 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10233 def_bfd = flinfo->output_bfd;
90c984fc
L
10234 if (hi->root.u.def.section != bfd_abs_section_ptr)
10235 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10236 _bfd_error_handler (msg, flinfo->output_bfd,
10237 h->root.root.string, def_bfd);
17d078c5 10238 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10239 eoinfo->failed = TRUE;
10240 return FALSE;
10241 }
10242
10243 /* We don't want to output symbols that have never been mentioned by
10244 a regular file, or that we have been told to strip. However, if
10245 h->indx is set to -2, the symbol is used by a reloc and we must
10246 output it. */
d983c8c5 10247 strip = FALSE;
c152c796 10248 if (h->indx == -2)
d983c8c5 10249 ;
f5385ebf 10250 else if ((h->def_dynamic
77cfaee6
AM
10251 || h->ref_dynamic
10252 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10253 && !h->def_regular
10254 && !h->ref_regular)
c152c796 10255 strip = TRUE;
8b127cbc 10256 else if (flinfo->info->strip == strip_all)
c152c796 10257 strip = TRUE;
8b127cbc
AM
10258 else if (flinfo->info->strip == strip_some
10259 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10260 h->root.root.string, FALSE, FALSE) == NULL)
10261 strip = TRUE;
d56d55e7
AM
10262 else if ((h->root.type == bfd_link_hash_defined
10263 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10264 && ((flinfo->info->strip_discarded
dbaa2011 10265 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10266 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10267 && h->root.u.def.section->owner != NULL
d56d55e7 10268 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10269 strip = TRUE;
9e2278f5
AM
10270 else if ((h->root.type == bfd_link_hash_undefined
10271 || h->root.type == bfd_link_hash_undefweak)
10272 && h->root.u.undef.abfd != NULL
10273 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10274 strip = TRUE;
c152c796 10275
b8871f35
L
10276 type = h->type;
10277
c152c796 10278 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10279 nothing else to do. However, if it is a forced local symbol or
10280 an ifunc symbol we need to give the backend finish_dynamic_symbol
10281 function a chance to make it dynamic. */
c152c796
AM
10282 if (strip
10283 && h->dynindx == -1
b8871f35 10284 && type != STT_GNU_IFUNC
f5385ebf 10285 && !h->forced_local)
c152c796
AM
10286 return TRUE;
10287
10288 sym.st_value = 0;
10289 sym.st_size = h->size;
10290 sym.st_other = h->other;
c152c796
AM
10291 switch (h->root.type)
10292 {
10293 default:
10294 case bfd_link_hash_new:
10295 case bfd_link_hash_warning:
10296 abort ();
10297 return FALSE;
10298
10299 case bfd_link_hash_undefined:
10300 case bfd_link_hash_undefweak:
10301 input_sec = bfd_und_section_ptr;
10302 sym.st_shndx = SHN_UNDEF;
10303 break;
10304
10305 case bfd_link_hash_defined:
10306 case bfd_link_hash_defweak:
10307 {
10308 input_sec = h->root.u.def.section;
10309 if (input_sec->output_section != NULL)
10310 {
10311 sym.st_shndx =
8b127cbc 10312 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10313 input_sec->output_section);
10314 if (sym.st_shndx == SHN_BAD)
10315 {
4eca0228 10316 _bfd_error_handler
695344c0 10317 /* xgettext:c-format */
871b3ab2 10318 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10319 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10320 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10321 eoinfo->failed = TRUE;
10322 return FALSE;
10323 }
10324
10325 /* ELF symbols in relocatable files are section relative,
10326 but in nonrelocatable files they are virtual
10327 addresses. */
10328 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10329 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10330 {
10331 sym.st_value += input_sec->output_section->vma;
10332 if (h->type == STT_TLS)
10333 {
8b127cbc 10334 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10335 if (tls_sec != NULL)
10336 sym.st_value -= tls_sec->vma;
c152c796
AM
10337 }
10338 }
10339 }
10340 else
10341 {
10342 BFD_ASSERT (input_sec->owner == NULL
10343 || (input_sec->owner->flags & DYNAMIC) != 0);
10344 sym.st_shndx = SHN_UNDEF;
10345 input_sec = bfd_und_section_ptr;
10346 }
10347 }
10348 break;
10349
10350 case bfd_link_hash_common:
10351 input_sec = h->root.u.c.p->section;
a4d8e49b 10352 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10353 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10354 break;
10355
10356 case bfd_link_hash_indirect:
10357 /* These symbols are created by symbol versioning. They point
10358 to the decorated version of the name. For example, if the
10359 symbol foo@@GNU_1.2 is the default, which should be used when
10360 foo is used with no version, then we add an indirect symbol
10361 foo which points to foo@@GNU_1.2. We ignore these symbols,
10362 since the indirected symbol is already in the hash table. */
10363 return TRUE;
10364 }
10365
b8871f35
L
10366 if (type == STT_COMMON || type == STT_OBJECT)
10367 switch (h->root.type)
10368 {
10369 case bfd_link_hash_common:
10370 type = elf_link_convert_common_type (flinfo->info, type);
10371 break;
10372 case bfd_link_hash_defined:
10373 case bfd_link_hash_defweak:
10374 if (bed->common_definition (&sym))
10375 type = elf_link_convert_common_type (flinfo->info, type);
10376 else
10377 type = STT_OBJECT;
10378 break;
10379 case bfd_link_hash_undefined:
10380 case bfd_link_hash_undefweak:
10381 break;
10382 default:
10383 abort ();
10384 }
10385
4deb8f71 10386 if (h->forced_local)
b8871f35
L
10387 {
10388 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10389 /* Turn off visibility on local symbol. */
10390 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10391 }
10392 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10393 else if (h->unique_global && h->def_regular)
10394 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10395 else if (h->root.type == bfd_link_hash_undefweak
10396 || h->root.type == bfd_link_hash_defweak)
10397 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10398 else
10399 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10400 sym.st_target_internal = h->target_internal;
10401
c152c796
AM
10402 /* Give the processor backend a chance to tweak the symbol value,
10403 and also to finish up anything that needs to be done for this
10404 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10405 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10406 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10407 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10408 && h->def_regular
0e1862bb 10409 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10410 || ((h->dynindx != -1
10411 || h->forced_local)
0e1862bb 10412 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10413 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10414 || h->root.type != bfd_link_hash_undefweak))
10415 || !h->forced_local)
8b127cbc 10416 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10417 {
10418 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10419 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10420 {
10421 eoinfo->failed = TRUE;
10422 return FALSE;
10423 }
10424 }
10425
10426 /* If we are marking the symbol as undefined, and there are no
10427 non-weak references to this symbol from a regular object, then
10428 mark the symbol as weak undefined; if there are non-weak
10429 references, mark the symbol as strong. We can't do this earlier,
10430 because it might not be marked as undefined until the
10431 finish_dynamic_symbol routine gets through with it. */
10432 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10433 && h->ref_regular
c152c796
AM
10434 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10435 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10436 {
10437 int bindtype;
b8871f35 10438 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10439
10440 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10441 if (type == STT_GNU_IFUNC)
10442 type = STT_FUNC;
c152c796 10443
f5385ebf 10444 if (h->ref_regular_nonweak)
c152c796
AM
10445 bindtype = STB_GLOBAL;
10446 else
10447 bindtype = STB_WEAK;
2955ec4c 10448 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10449 }
10450
bda987c2
CD
10451 /* If this is a symbol defined in a dynamic library, don't use the
10452 symbol size from the dynamic library. Relinking an executable
10453 against a new library may introduce gratuitous changes in the
10454 executable's symbols if we keep the size. */
10455 if (sym.st_shndx == SHN_UNDEF
10456 && !h->def_regular
10457 && h->def_dynamic)
10458 sym.st_size = 0;
10459
c152c796
AM
10460 /* If a non-weak symbol with non-default visibility is not defined
10461 locally, it is a fatal error. */
0e1862bb 10462 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10463 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10464 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10465 && h->root.type == bfd_link_hash_undefined
f5385ebf 10466 && !h->def_regular)
c152c796 10467 {
17d078c5
AM
10468 const char *msg;
10469
10470 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10471 /* xgettext:c-format */
871b3ab2 10472 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10473 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10474 /* xgettext:c-format */
871b3ab2 10475 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10476 else
695344c0 10477 /* xgettext:c-format */
871b3ab2 10478 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10479 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10480 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10481 eoinfo->failed = TRUE;
10482 return FALSE;
10483 }
10484
10485 /* If this symbol should be put in the .dynsym section, then put it
10486 there now. We already know the symbol index. We also fill in
10487 the entry in the .hash section. */
1c2649ed
EB
10488 if (h->dynindx != -1
10489 && elf_hash_table (flinfo->info)->dynamic_sections_created
10490 && elf_hash_table (flinfo->info)->dynsym != NULL
10491 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10492 {
c152c796
AM
10493 bfd_byte *esym;
10494
90c984fc
L
10495 /* Since there is no version information in the dynamic string,
10496 if there is no version info in symbol version section, we will
1659f720 10497 have a run-time problem if not linking executable, referenced
4deb8f71 10498 by shared library, or not bound locally. */
1659f720 10499 if (h->verinfo.verdef == NULL
0e1862bb 10500 && (!bfd_link_executable (flinfo->info)
1659f720
L
10501 || h->ref_dynamic
10502 || !h->def_regular))
90c984fc
L
10503 {
10504 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10505
10506 if (p && p [1] != '\0')
10507 {
4eca0228 10508 _bfd_error_handler
695344c0 10509 /* xgettext:c-format */
9793eb77 10510 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10511 flinfo->output_bfd, h->root.root.string);
10512 eoinfo->failed = TRUE;
10513 return FALSE;
10514 }
10515 }
10516
c152c796 10517 sym.st_name = h->dynstr_index;
cae1fbbb
L
10518 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10519 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10520 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10521 {
10522 eoinfo->failed = TRUE;
10523 return FALSE;
10524 }
3d16b64e
NA
10525
10526 /* Inform the linker of the addition of this symbol. */
10527
10528 if (flinfo->info->callbacks->ctf_new_dynsym)
10529 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10530
8b127cbc 10531 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10532
8b127cbc 10533 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10534 {
10535 size_t hash_entry_size;
10536 bfd_byte *bucketpos;
10537 bfd_vma chain;
41198d0c
L
10538 size_t bucketcount;
10539 size_t bucket;
10540
8b127cbc 10541 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10542 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10543
10544 hash_entry_size
8b127cbc
AM
10545 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10546 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10547 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10548 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10549 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10550 bucketpos);
10551 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10552 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10553 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10554 }
c152c796 10555
8b127cbc 10556 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10557 {
10558 Elf_Internal_Versym iversym;
10559 Elf_External_Versym *eversym;
10560
5fa370e4 10561 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10562 {
7b20f099
AM
10563 if (h->verinfo.verdef == NULL
10564 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10565 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10566 iversym.vs_vers = 0;
10567 else
10568 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10569 }
10570 else
10571 {
10572 if (h->verinfo.vertree == NULL)
10573 iversym.vs_vers = 1;
10574 else
10575 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10576 if (flinfo->info->create_default_symver)
3e3b46e5 10577 iversym.vs_vers++;
c152c796
AM
10578 }
10579
422f1182 10580 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10581 defined locally. */
422f1182 10582 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10583 iversym.vs_vers |= VERSYM_HIDDEN;
10584
8b127cbc 10585 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10586 eversym += h->dynindx;
8b127cbc 10587 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10588 }
10589 }
10590
d983c8c5
AM
10591 /* If the symbol is undefined, and we didn't output it to .dynsym,
10592 strip it from .symtab too. Obviously we can't do this for
10593 relocatable output or when needed for --emit-relocs. */
10594 else if (input_sec == bfd_und_section_ptr
10595 && h->indx != -2
66cae560
NC
10596 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10597 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10598 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10599 return TRUE;
66cae560 10600
d983c8c5
AM
10601 /* Also strip others that we couldn't earlier due to dynamic symbol
10602 processing. */
10603 if (strip)
10604 return TRUE;
10605 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10606 return TRUE;
10607
2ec55de3
AM
10608 /* Output a FILE symbol so that following locals are not associated
10609 with the wrong input file. We need one for forced local symbols
10610 if we've seen more than one FILE symbol or when we have exactly
10611 one FILE symbol but global symbols are present in a file other
10612 than the one with the FILE symbol. We also need one if linker
10613 defined symbols are present. In practice these conditions are
10614 always met, so just emit the FILE symbol unconditionally. */
10615 if (eoinfo->localsyms
10616 && !eoinfo->file_sym_done
10617 && eoinfo->flinfo->filesym_count != 0)
10618 {
10619 Elf_Internal_Sym fsym;
10620
10621 memset (&fsym, 0, sizeof (fsym));
10622 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10623 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10624 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10625 bfd_und_section_ptr, NULL))
2ec55de3
AM
10626 return FALSE;
10627
10628 eoinfo->file_sym_done = TRUE;
10629 }
10630
8b127cbc 10631 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10632 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10633 input_sec, h);
6e0b88f1 10634 if (ret == 0)
c152c796
AM
10635 {
10636 eoinfo->failed = TRUE;
10637 return FALSE;
10638 }
6e0b88f1
AM
10639 else if (ret == 1)
10640 h->indx = indx;
10641 else if (h->indx == -2)
10642 abort();
c152c796
AM
10643
10644 return TRUE;
10645}
10646
cdd3575c
AM
10647/* Return TRUE if special handling is done for relocs in SEC against
10648 symbols defined in discarded sections. */
10649
c152c796
AM
10650static bfd_boolean
10651elf_section_ignore_discarded_relocs (asection *sec)
10652{
10653 const struct elf_backend_data *bed;
10654
cdd3575c
AM
10655 switch (sec->sec_info_type)
10656 {
dbaa2011
AM
10657 case SEC_INFO_TYPE_STABS:
10658 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10659 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10660 return TRUE;
10661 default:
10662 break;
10663 }
c152c796
AM
10664
10665 bed = get_elf_backend_data (sec->owner);
10666 if (bed->elf_backend_ignore_discarded_relocs != NULL
10667 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10668 return TRUE;
10669
10670 return FALSE;
10671}
10672
9e66c942
AM
10673/* Return a mask saying how ld should treat relocations in SEC against
10674 symbols defined in discarded sections. If this function returns
10675 COMPLAIN set, ld will issue a warning message. If this function
10676 returns PRETEND set, and the discarded section was link-once and the
10677 same size as the kept link-once section, ld will pretend that the
10678 symbol was actually defined in the kept section. Otherwise ld will
10679 zero the reloc (at least that is the intent, but some cooperation by
10680 the target dependent code is needed, particularly for REL targets). */
10681
8a696751
AM
10682unsigned int
10683_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10684{
9e66c942 10685 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10686 return PRETEND;
cdd3575c
AM
10687
10688 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10689 return 0;
cdd3575c
AM
10690
10691 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10692 return 0;
cdd3575c 10693
9e66c942 10694 return COMPLAIN | PRETEND;
cdd3575c
AM
10695}
10696
3d7f7666
L
10697/* Find a match between a section and a member of a section group. */
10698
10699static asection *
c0f00686
L
10700match_group_member (asection *sec, asection *group,
10701 struct bfd_link_info *info)
3d7f7666
L
10702{
10703 asection *first = elf_next_in_group (group);
10704 asection *s = first;
10705
10706 while (s != NULL)
10707 {
c0f00686 10708 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10709 return s;
10710
83180ade 10711 s = elf_next_in_group (s);
3d7f7666
L
10712 if (s == first)
10713 break;
10714 }
10715
10716 return NULL;
10717}
10718
01b3c8ab 10719/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10720 to replace it. Return the replacement if it is OK. Otherwise return
10721 NULL. */
01b3c8ab
L
10722
10723asection *
c0f00686 10724_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10725{
10726 asection *kept;
10727
10728 kept = sec->kept_section;
10729 if (kept != NULL)
10730 {
c2370991 10731 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10732 kept = match_group_member (sec, kept, info);
58349d00
L
10733 if (kept != NULL)
10734 {
10735 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10736 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10737 kept = NULL;
10738 else
10739 {
10740 /* Get the real kept section. */
10741 asection *next;
10742 for (next = kept->kept_section;
10743 next != NULL;
10744 next = next->kept_section)
10745 kept = next;
10746 }
10747 }
c2370991 10748 sec->kept_section = kept;
01b3c8ab
L
10749 }
10750 return kept;
10751}
10752
c152c796
AM
10753/* Link an input file into the linker output file. This function
10754 handles all the sections and relocations of the input file at once.
10755 This is so that we only have to read the local symbols once, and
10756 don't have to keep them in memory. */
10757
10758static bfd_boolean
8b127cbc 10759elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10760{
ece5ef60 10761 int (*relocate_section)
c152c796
AM
10762 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10763 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10764 bfd *output_bfd;
10765 Elf_Internal_Shdr *symtab_hdr;
10766 size_t locsymcount;
10767 size_t extsymoff;
10768 Elf_Internal_Sym *isymbuf;
10769 Elf_Internal_Sym *isym;
10770 Elf_Internal_Sym *isymend;
10771 long *pindex;
10772 asection **ppsection;
10773 asection *o;
10774 const struct elf_backend_data *bed;
c152c796 10775 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10776 bfd_size_type address_size;
10777 bfd_vma r_type_mask;
10778 int r_sym_shift;
ffbc01cc 10779 bfd_boolean have_file_sym = FALSE;
c152c796 10780
8b127cbc 10781 output_bfd = flinfo->output_bfd;
c152c796
AM
10782 bed = get_elf_backend_data (output_bfd);
10783 relocate_section = bed->elf_backend_relocate_section;
10784
10785 /* If this is a dynamic object, we don't want to do anything here:
10786 we don't want the local symbols, and we don't want the section
10787 contents. */
10788 if ((input_bfd->flags & DYNAMIC) != 0)
10789 return TRUE;
10790
c152c796
AM
10791 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10792 if (elf_bad_symtab (input_bfd))
10793 {
10794 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10795 extsymoff = 0;
10796 }
10797 else
10798 {
10799 locsymcount = symtab_hdr->sh_info;
10800 extsymoff = symtab_hdr->sh_info;
10801 }
10802
99fabbc9
JL
10803 /* Enable GNU OSABI features in the output BFD that are used in the input
10804 BFD. */
10805 if (bed->elf_osabi == ELFOSABI_NONE
10806 || bed->elf_osabi == ELFOSABI_GNU
10807 || bed->elf_osabi == ELFOSABI_FREEBSD)
10808 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10809 |= (elf_tdata (input_bfd)->has_gnu_osabi
10810 & (bfd_link_relocatable (flinfo->info)
10811 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10812
c152c796
AM
10813 /* Read the local symbols. */
10814 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10815 if (isymbuf == NULL && locsymcount != 0)
10816 {
10817 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10818 flinfo->internal_syms,
10819 flinfo->external_syms,
10820 flinfo->locsym_shndx);
c152c796
AM
10821 if (isymbuf == NULL)
10822 return FALSE;
10823 }
10824
10825 /* Find local symbol sections and adjust values of symbols in
10826 SEC_MERGE sections. Write out those local symbols we know are
10827 going into the output file. */
10828 isymend = isymbuf + locsymcount;
8b127cbc 10829 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10830 isym < isymend;
10831 isym++, pindex++, ppsection++)
10832 {
10833 asection *isec;
10834 const char *name;
10835 Elf_Internal_Sym osym;
6e0b88f1
AM
10836 long indx;
10837 int ret;
c152c796
AM
10838
10839 *pindex = -1;
10840
10841 if (elf_bad_symtab (input_bfd))
10842 {
10843 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10844 {
10845 *ppsection = NULL;
10846 continue;
10847 }
10848 }
10849
10850 if (isym->st_shndx == SHN_UNDEF)
10851 isec = bfd_und_section_ptr;
c152c796
AM
10852 else if (isym->st_shndx == SHN_ABS)
10853 isec = bfd_abs_section_ptr;
10854 else if (isym->st_shndx == SHN_COMMON)
10855 isec = bfd_com_section_ptr;
10856 else
10857 {
cb33740c
AM
10858 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10859 if (isec == NULL)
10860 {
10861 /* Don't attempt to output symbols with st_shnx in the
10862 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10863 isec = bfd_und_section_ptr;
cb33740c 10864 }
dbaa2011 10865 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10866 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10867 isym->st_value =
10868 _bfd_merged_section_offset (output_bfd, &isec,
10869 elf_section_data (isec)->sec_info,
10870 isym->st_value);
c152c796
AM
10871 }
10872
10873 *ppsection = isec;
10874
d983c8c5
AM
10875 /* Don't output the first, undefined, symbol. In fact, don't
10876 output any undefined local symbol. */
10877 if (isec == bfd_und_section_ptr)
c152c796
AM
10878 continue;
10879
10880 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10881 {
10882 /* We never output section symbols. Instead, we use the
10883 section symbol of the corresponding section in the output
10884 file. */
10885 continue;
10886 }
10887
10888 /* If we are stripping all symbols, we don't want to output this
10889 one. */
8b127cbc 10890 if (flinfo->info->strip == strip_all)
c152c796
AM
10891 continue;
10892
10893 /* If we are discarding all local symbols, we don't want to
10894 output this one. If we are generating a relocatable output
10895 file, then some of the local symbols may be required by
10896 relocs; we output them below as we discover that they are
10897 needed. */
8b127cbc 10898 if (flinfo->info->discard == discard_all)
c152c796
AM
10899 continue;
10900
10901 /* If this symbol is defined in a section which we are
f02571c5 10902 discarding, we don't need to keep it. */
abf874aa
CL
10903 if (isym->st_shndx != SHN_UNDEF
10904 && isym->st_shndx < SHN_LORESERVE
10905 && isec->output_section == NULL
10906 && flinfo->info->non_contiguous_regions
10907 && flinfo->info->non_contiguous_regions_warnings)
10908 {
10909 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10910 "discards section `%s' from '%s'\n"),
765cf5f6 10911 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10912 continue;
10913 }
10914
f02571c5 10915 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10916 && isym->st_shndx < SHN_LORESERVE
10917 && bfd_section_removed_from_list (output_bfd,
10918 isec->output_section))
e75a280b
L
10919 continue;
10920
c152c796
AM
10921 /* Get the name of the symbol. */
10922 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10923 isym->st_name);
10924 if (name == NULL)
10925 return FALSE;
10926
10927 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10928 if ((flinfo->info->strip == strip_some
10929 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10930 == NULL))
8b127cbc 10931 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10932 && (isec->flags & SEC_MERGE)
10933 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10934 || flinfo->info->discard == discard_l)
c152c796
AM
10935 && bfd_is_local_label_name (input_bfd, name)))
10936 continue;
10937
ffbc01cc
AM
10938 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10939 {
ce875075
AM
10940 if (input_bfd->lto_output)
10941 /* -flto puts a temp file name here. This means builds
10942 are not reproducible. Discard the symbol. */
10943 continue;
ffbc01cc
AM
10944 have_file_sym = TRUE;
10945 flinfo->filesym_count += 1;
10946 }
10947 if (!have_file_sym)
10948 {
10949 /* In the absence of debug info, bfd_find_nearest_line uses
10950 FILE symbols to determine the source file for local
10951 function symbols. Provide a FILE symbol here if input
10952 files lack such, so that their symbols won't be
10953 associated with a previous input file. It's not the
10954 source file, but the best we can do. */
10955 have_file_sym = TRUE;
10956 flinfo->filesym_count += 1;
10957 memset (&osym, 0, sizeof (osym));
10958 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10959 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10960 if (!elf_link_output_symstrtab (flinfo,
10961 (input_bfd->lto_output ? NULL
765cf5f6 10962 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10963 &osym, bfd_abs_section_ptr,
10964 NULL))
ffbc01cc
AM
10965 return FALSE;
10966 }
10967
c152c796
AM
10968 osym = *isym;
10969
10970 /* Adjust the section index for the output file. */
10971 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10972 isec->output_section);
10973 if (osym.st_shndx == SHN_BAD)
10974 return FALSE;
10975
c152c796
AM
10976 /* ELF symbols in relocatable files are section relative, but
10977 in executable files they are virtual addresses. Note that
10978 this code assumes that all ELF sections have an associated
10979 BFD section with a reasonable value for output_offset; below
10980 we assume that they also have a reasonable value for
10981 output_section. Any special sections must be set up to meet
10982 these requirements. */
10983 osym.st_value += isec->output_offset;
0e1862bb 10984 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10985 {
10986 osym.st_value += isec->output_section->vma;
10987 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10988 {
10989 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10990 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10991 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10992 else
10993 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10994 STT_NOTYPE);
c152c796
AM
10995 }
10996 }
10997
6e0b88f1 10998 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10999 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11000 if (ret == 0)
c152c796 11001 return FALSE;
6e0b88f1
AM
11002 else if (ret == 1)
11003 *pindex = indx;
c152c796
AM
11004 }
11005
310fd250
L
11006 if (bed->s->arch_size == 32)
11007 {
11008 r_type_mask = 0xff;
11009 r_sym_shift = 8;
11010 address_size = 4;
11011 }
11012 else
11013 {
11014 r_type_mask = 0xffffffff;
11015 r_sym_shift = 32;
11016 address_size = 8;
11017 }
11018
c152c796
AM
11019 /* Relocate the contents of each section. */
11020 sym_hashes = elf_sym_hashes (input_bfd);
11021 for (o = input_bfd->sections; o != NULL; o = o->next)
11022 {
11023 bfd_byte *contents;
11024
11025 if (! o->linker_mark)
11026 {
11027 /* This section was omitted from the link. */
11028 continue;
11029 }
11030
7bdf4127 11031 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11032 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11033 {
11034 /* Deal with the group signature symbol. */
11035 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11036 unsigned long symndx = sec_data->this_hdr.sh_info;
11037 asection *osec = o->output_section;
11038
7bdf4127 11039 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11040 if (symndx >= locsymcount
11041 || (elf_bad_symtab (input_bfd)
8b127cbc 11042 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11043 {
11044 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11045 while (h->root.type == bfd_link_hash_indirect
11046 || h->root.type == bfd_link_hash_warning)
11047 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11048 /* Arrange for symbol to be output. */
11049 h->indx = -2;
11050 elf_section_data (osec)->this_hdr.sh_info = -2;
11051 }
11052 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11053 {
11054 /* We'll use the output section target_index. */
8b127cbc 11055 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11056 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11057 }
11058 else
11059 {
8b127cbc 11060 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11061 {
11062 /* Otherwise output the local symbol now. */
11063 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11064 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11065 const char *name;
6e0b88f1
AM
11066 long indx;
11067 int ret;
bcacc0f5
AM
11068
11069 name = bfd_elf_string_from_elf_section (input_bfd,
11070 symtab_hdr->sh_link,
11071 sym.st_name);
11072 if (name == NULL)
11073 return FALSE;
11074
11075 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11076 sec);
11077 if (sym.st_shndx == SHN_BAD)
11078 return FALSE;
11079
11080 sym.st_value += o->output_offset;
11081
6e0b88f1 11082 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11083 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11084 NULL);
6e0b88f1 11085 if (ret == 0)
bcacc0f5 11086 return FALSE;
6e0b88f1 11087 else if (ret == 1)
8b127cbc 11088 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11089 else
11090 abort ();
bcacc0f5
AM
11091 }
11092 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11093 = flinfo->indices[symndx];
bcacc0f5
AM
11094 }
11095 }
11096
c152c796 11097 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11098 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11099 continue;
11100
11101 if ((o->flags & SEC_LINKER_CREATED) != 0)
11102 {
11103 /* Section was created by _bfd_elf_link_create_dynamic_sections
11104 or somesuch. */
11105 continue;
11106 }
11107
11108 /* Get the contents of the section. They have been cached by a
11109 relaxation routine. Note that o is a section in an input
11110 file, so the contents field will not have been set by any of
11111 the routines which work on output files. */
11112 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11113 {
11114 contents = elf_section_data (o)->this_hdr.contents;
11115 if (bed->caches_rawsize
11116 && o->rawsize != 0
11117 && o->rawsize < o->size)
11118 {
11119 memcpy (flinfo->contents, contents, o->rawsize);
11120 contents = flinfo->contents;
11121 }
11122 }
c152c796
AM
11123 else
11124 {
8b127cbc 11125 contents = flinfo->contents;
4a114e3e 11126 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11127 return FALSE;
11128 }
11129
11130 if ((o->flags & SEC_RELOC) != 0)
11131 {
11132 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11133 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11134 int action_discarded;
ece5ef60 11135 int ret;
c152c796
AM
11136
11137 /* Get the swapped relocs. */
11138 internal_relocs
8b127cbc
AM
11139 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11140 flinfo->internal_relocs, FALSE);
c152c796
AM
11141 if (internal_relocs == NULL
11142 && o->reloc_count > 0)
11143 return FALSE;
11144
310fd250
L
11145 /* We need to reverse-copy input .ctors/.dtors sections if
11146 they are placed in .init_array/.finit_array for output. */
11147 if (o->size > address_size
11148 && ((strncmp (o->name, ".ctors", 6) == 0
11149 && strcmp (o->output_section->name,
11150 ".init_array") == 0)
11151 || (strncmp (o->name, ".dtors", 6) == 0
11152 && strcmp (o->output_section->name,
11153 ".fini_array") == 0))
11154 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11155 {
056bafd4
MR
11156 if (o->size * bed->s->int_rels_per_ext_rel
11157 != o->reloc_count * address_size)
310fd250 11158 {
4eca0228 11159 _bfd_error_handler
695344c0 11160 /* xgettext:c-format */
871b3ab2 11161 (_("error: %pB: size of section %pA is not "
310fd250
L
11162 "multiple of address size"),
11163 input_bfd, o);
8c6716e5 11164 bfd_set_error (bfd_error_bad_value);
310fd250
L
11165 return FALSE;
11166 }
11167 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11168 }
11169
0f02bbd9 11170 action_discarded = -1;
c152c796 11171 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11172 action_discarded = (*bed->action_discarded) (o);
11173
11174 /* Run through the relocs evaluating complex reloc symbols and
11175 looking for relocs against symbols from discarded sections
11176 or section symbols from removed link-once sections.
11177 Complain about relocs against discarded sections. Zero
11178 relocs against removed link-once sections. */
11179
11180 rel = internal_relocs;
056bafd4 11181 relend = rel + o->reloc_count;
0f02bbd9 11182 for ( ; rel < relend; rel++)
c152c796 11183 {
0f02bbd9
AM
11184 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11185 unsigned int s_type;
11186 asection **ps, *sec;
11187 struct elf_link_hash_entry *h = NULL;
11188 const char *sym_name;
c152c796 11189
0f02bbd9
AM
11190 if (r_symndx == STN_UNDEF)
11191 continue;
c152c796 11192
0f02bbd9
AM
11193 if (r_symndx >= locsymcount
11194 || (elf_bad_symtab (input_bfd)
8b127cbc 11195 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11196 {
11197 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11198
0f02bbd9
AM
11199 /* Badly formatted input files can contain relocs that
11200 reference non-existant symbols. Check here so that
11201 we do not seg fault. */
11202 if (h == NULL)
c152c796 11203 {
4eca0228 11204 _bfd_error_handler
695344c0 11205 /* xgettext:c-format */
2dcf00ce 11206 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11207 "that references a non-existent global symbol"),
2dcf00ce 11208 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11209 bfd_set_error (bfd_error_bad_value);
11210 return FALSE;
11211 }
3b36f7e6 11212
0f02bbd9
AM
11213 while (h->root.type == bfd_link_hash_indirect
11214 || h->root.type == bfd_link_hash_warning)
11215 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11216
0f02bbd9 11217 s_type = h->type;
cdd3575c 11218
9e2dec47 11219 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11220 mark the symbol as undefined. Note that the
11221 linker may attach linker created dynamic sections
11222 to the plugin bfd. Symbols defined in linker
11223 created sections are not plugin symbols. */
bc4e12de 11224 if ((h->root.non_ir_ref_regular
4070765b 11225 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11226 && (h->root.type == bfd_link_hash_defined
11227 || h->root.type == bfd_link_hash_defweak)
11228 && (h->root.u.def.section->flags
11229 & SEC_LINKER_CREATED) == 0
11230 && h->root.u.def.section->owner != NULL
11231 && (h->root.u.def.section->owner->flags
11232 & BFD_PLUGIN) != 0)
11233 {
11234 h->root.type = bfd_link_hash_undefined;
11235 h->root.u.undef.abfd = h->root.u.def.section->owner;
11236 }
11237
0f02bbd9
AM
11238 ps = NULL;
11239 if (h->root.type == bfd_link_hash_defined
11240 || h->root.type == bfd_link_hash_defweak)
11241 ps = &h->root.u.def.section;
11242
11243 sym_name = h->root.root.string;
11244 }
11245 else
11246 {
11247 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11248
11249 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11250 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11251 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11252 sym, *ps);
11253 }
c152c796 11254
c301e700 11255 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11256 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11257 {
11258 bfd_vma val;
11259 bfd_vma dot = (rel->r_offset
11260 + o->output_offset + o->output_section->vma);
11261#ifdef DEBUG
11262 printf ("Encountered a complex symbol!");
11263 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11264 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11265 (long) (rel - internal_relocs));
0f02bbd9
AM
11266 printf (" symbol: idx %8.8lx, name %s\n",
11267 r_symndx, sym_name);
11268 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11269 (unsigned long) rel->r_info,
11270 (unsigned long) rel->r_offset);
11271#endif
8b127cbc 11272 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11273 isymbuf, locsymcount, s_type == STT_SRELC))
11274 return FALSE;
11275
11276 /* Symbol evaluated OK. Update to absolute value. */
11277 set_symbol_value (input_bfd, isymbuf, locsymcount,
11278 r_symndx, val);
11279 continue;
11280 }
11281
11282 if (action_discarded != -1 && ps != NULL)
11283 {
cdd3575c
AM
11284 /* Complain if the definition comes from a
11285 discarded section. */
dbaa2011 11286 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11287 {
cf35638d 11288 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11289 if (action_discarded & COMPLAIN)
8b127cbc 11290 (*flinfo->info->callbacks->einfo)
695344c0 11291 /* xgettext:c-format */
871b3ab2
AM
11292 (_("%X`%s' referenced in section `%pA' of %pB: "
11293 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11294 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11295
87e5235d 11296 /* Try to do the best we can to support buggy old
e0ae6d6f 11297 versions of gcc. Pretend that the symbol is
87e5235d
AM
11298 really defined in the kept linkonce section.
11299 FIXME: This is quite broken. Modifying the
11300 symbol here means we will be changing all later
e0ae6d6f 11301 uses of the symbol, not just in this section. */
0f02bbd9 11302 if (action_discarded & PRETEND)
87e5235d 11303 {
01b3c8ab
L
11304 asection *kept;
11305
c0f00686 11306 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11307 flinfo->info);
01b3c8ab 11308 if (kept != NULL)
87e5235d
AM
11309 {
11310 *ps = kept;
11311 continue;
11312 }
11313 }
c152c796
AM
11314 }
11315 }
11316 }
11317
11318 /* Relocate the section by invoking a back end routine.
11319
11320 The back end routine is responsible for adjusting the
11321 section contents as necessary, and (if using Rela relocs
11322 and generating a relocatable output file) adjusting the
11323 reloc addend as necessary.
11324
11325 The back end routine does not have to worry about setting
11326 the reloc address or the reloc symbol index.
11327
11328 The back end routine is given a pointer to the swapped in
11329 internal symbols, and can access the hash table entries
11330 for the external symbols via elf_sym_hashes (input_bfd).
11331
11332 When generating relocatable output, the back end routine
11333 must handle STB_LOCAL/STT_SECTION symbols specially. The
11334 output symbol is going to be a section symbol
11335 corresponding to the output section, which will require
11336 the addend to be adjusted. */
11337
8b127cbc 11338 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11339 input_bfd, o, contents,
11340 internal_relocs,
11341 isymbuf,
8b127cbc 11342 flinfo->sections);
ece5ef60 11343 if (!ret)
c152c796
AM
11344 return FALSE;
11345
ece5ef60 11346 if (ret == 2
0e1862bb 11347 || bfd_link_relocatable (flinfo->info)
8b127cbc 11348 || flinfo->info->emitrelocations)
c152c796
AM
11349 {
11350 Elf_Internal_Rela *irela;
d4730f92 11351 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11352 bfd_vma last_offset;
11353 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11354 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11355 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11356 unsigned int next_erel;
c152c796 11357 bfd_boolean rela_normal;
d4730f92 11358 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11359
d4730f92
BS
11360 esdi = elf_section_data (o);
11361 esdo = elf_section_data (o->output_section);
11362 rela_normal = FALSE;
c152c796
AM
11363
11364 /* Adjust the reloc addresses and symbol indices. */
11365
11366 irela = internal_relocs;
056bafd4 11367 irelaend = irela + o->reloc_count;
d4730f92
BS
11368 rel_hash = esdo->rel.hashes + esdo->rel.count;
11369 /* We start processing the REL relocs, if any. When we reach
11370 IRELAMID in the loop, we switch to the RELA relocs. */
11371 irelamid = irela;
11372 if (esdi->rel.hdr != NULL)
11373 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11374 * bed->s->int_rels_per_ext_rel);
eac338cf 11375 rel_hash_list = rel_hash;
d4730f92 11376 rela_hash_list = NULL;
c152c796 11377 last_offset = o->output_offset;
0e1862bb 11378 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11379 last_offset += o->output_section->vma;
11380 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11381 {
11382 unsigned long r_symndx;
11383 asection *sec;
11384 Elf_Internal_Sym sym;
11385
11386 if (next_erel == bed->s->int_rels_per_ext_rel)
11387 {
11388 rel_hash++;
11389 next_erel = 0;
11390 }
11391
d4730f92
BS
11392 if (irela == irelamid)
11393 {
11394 rel_hash = esdo->rela.hashes + esdo->rela.count;
11395 rela_hash_list = rel_hash;
11396 rela_normal = bed->rela_normal;
11397 }
11398
c152c796 11399 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11400 flinfo->info, o,
c152c796
AM
11401 irela->r_offset);
11402 if (irela->r_offset >= (bfd_vma) -2)
11403 {
11404 /* This is a reloc for a deleted entry or somesuch.
11405 Turn it into an R_*_NONE reloc, at the same
11406 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11407 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11408 being ordered. */
11409 irela->r_offset = last_offset;
11410 irela->r_info = 0;
11411 irela->r_addend = 0;
11412 continue;
11413 }
11414
11415 irela->r_offset += o->output_offset;
11416
11417 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11418 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11419 irela->r_offset += o->output_section->vma;
11420
11421 last_offset = irela->r_offset;
11422
11423 r_symndx = irela->r_info >> r_sym_shift;
11424 if (r_symndx == STN_UNDEF)
11425 continue;
11426
11427 if (r_symndx >= locsymcount
11428 || (elf_bad_symtab (input_bfd)
8b127cbc 11429 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11430 {
11431 struct elf_link_hash_entry *rh;
11432 unsigned long indx;
11433
11434 /* This is a reloc against a global symbol. We
11435 have not yet output all the local symbols, so
11436 we do not know the symbol index of any global
11437 symbol. We set the rel_hash entry for this
11438 reloc to point to the global hash table entry
11439 for this symbol. The symbol index is then
ee75fd95 11440 set at the end of bfd_elf_final_link. */
c152c796
AM
11441 indx = r_symndx - extsymoff;
11442 rh = elf_sym_hashes (input_bfd)[indx];
11443 while (rh->root.type == bfd_link_hash_indirect
11444 || rh->root.type == bfd_link_hash_warning)
11445 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11446
11447 /* Setting the index to -2 tells
11448 elf_link_output_extsym that this symbol is
11449 used by a reloc. */
11450 BFD_ASSERT (rh->indx < 0);
11451 rh->indx = -2;
c152c796
AM
11452 *rel_hash = rh;
11453
11454 continue;
11455 }
11456
11457 /* This is a reloc against a local symbol. */
11458
11459 *rel_hash = NULL;
11460 sym = isymbuf[r_symndx];
8b127cbc 11461 sec = flinfo->sections[r_symndx];
c152c796
AM
11462 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11463 {
11464 /* I suppose the backend ought to fill in the
11465 section of any STT_SECTION symbol against a
6a8d1586 11466 processor specific section. */
cf35638d 11467 r_symndx = STN_UNDEF;
6a8d1586
AM
11468 if (bfd_is_abs_section (sec))
11469 ;
c152c796
AM
11470 else if (sec == NULL || sec->owner == NULL)
11471 {
11472 bfd_set_error (bfd_error_bad_value);
11473 return FALSE;
11474 }
11475 else
11476 {
6a8d1586
AM
11477 asection *osec = sec->output_section;
11478
11479 /* If we have discarded a section, the output
11480 section will be the absolute section. In
ab96bf03
AM
11481 case of discarded SEC_MERGE sections, use
11482 the kept section. relocate_section should
11483 have already handled discarded linkonce
11484 sections. */
6a8d1586
AM
11485 if (bfd_is_abs_section (osec)
11486 && sec->kept_section != NULL
11487 && sec->kept_section->output_section != NULL)
11488 {
11489 osec = sec->kept_section->output_section;
11490 irela->r_addend -= osec->vma;
11491 }
11492
11493 if (!bfd_is_abs_section (osec))
11494 {
11495 r_symndx = osec->target_index;
cf35638d 11496 if (r_symndx == STN_UNDEF)
74541ad4 11497 {
051d833a
AM
11498 irela->r_addend += osec->vma;
11499 osec = _bfd_nearby_section (output_bfd, osec,
11500 osec->vma);
11501 irela->r_addend -= osec->vma;
11502 r_symndx = osec->target_index;
74541ad4 11503 }
6a8d1586 11504 }
c152c796
AM
11505 }
11506
11507 /* Adjust the addend according to where the
11508 section winds up in the output section. */
11509 if (rela_normal)
11510 irela->r_addend += sec->output_offset;
11511 }
11512 else
11513 {
8b127cbc 11514 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11515 {
11516 unsigned long shlink;
11517 const char *name;
11518 asection *osec;
6e0b88f1 11519 long indx;
c152c796 11520
8b127cbc 11521 if (flinfo->info->strip == strip_all)
c152c796
AM
11522 {
11523 /* You can't do ld -r -s. */
11524 bfd_set_error (bfd_error_invalid_operation);
11525 return FALSE;
11526 }
11527
11528 /* This symbol was skipped earlier, but
11529 since it is needed by a reloc, we
11530 must output it now. */
11531 shlink = symtab_hdr->sh_link;
11532 name = (bfd_elf_string_from_elf_section
11533 (input_bfd, shlink, sym.st_name));
11534 if (name == NULL)
11535 return FALSE;
11536
11537 osec = sec->output_section;
11538 sym.st_shndx =
11539 _bfd_elf_section_from_bfd_section (output_bfd,
11540 osec);
11541 if (sym.st_shndx == SHN_BAD)
11542 return FALSE;
11543
11544 sym.st_value += sec->output_offset;
0e1862bb 11545 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11546 {
11547 sym.st_value += osec->vma;
11548 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11549 {
102def4d
AM
11550 struct elf_link_hash_table *htab
11551 = elf_hash_table (flinfo->info);
11552
c152c796
AM
11553 /* STT_TLS symbols are relative to PT_TLS
11554 segment base. */
102def4d
AM
11555 if (htab->tls_sec != NULL)
11556 sym.st_value -= htab->tls_sec->vma;
11557 else
11558 sym.st_info
11559 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11560 STT_NOTYPE);
c152c796
AM
11561 }
11562 }
11563
6e0b88f1 11564 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11565 ret = elf_link_output_symstrtab (flinfo, name,
11566 &sym, sec,
11567 NULL);
6e0b88f1 11568 if (ret == 0)
c152c796 11569 return FALSE;
6e0b88f1 11570 else if (ret == 1)
8b127cbc 11571 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11572 else
11573 abort ();
c152c796
AM
11574 }
11575
8b127cbc 11576 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11577 }
11578
11579 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11580 | (irela->r_info & r_type_mask));
11581 }
11582
11583 /* Swap out the relocs. */
d4730f92
BS
11584 input_rel_hdr = esdi->rel.hdr;
11585 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11586 {
d4730f92
BS
11587 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11588 input_rel_hdr,
11589 internal_relocs,
11590 rel_hash_list))
11591 return FALSE;
c152c796
AM
11592 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11593 * bed->s->int_rels_per_ext_rel);
eac338cf 11594 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11595 }
11596
11597 input_rela_hdr = esdi->rela.hdr;
11598 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11599 {
eac338cf 11600 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11601 input_rela_hdr,
eac338cf 11602 internal_relocs,
d4730f92 11603 rela_hash_list))
c152c796
AM
11604 return FALSE;
11605 }
11606 }
11607 }
11608
11609 /* Write out the modified section contents. */
11610 if (bed->elf_backend_write_section
8b127cbc 11611 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11612 contents))
c152c796
AM
11613 {
11614 /* Section written out. */
11615 }
11616 else switch (o->sec_info_type)
11617 {
dbaa2011 11618 case SEC_INFO_TYPE_STABS:
c152c796
AM
11619 if (! (_bfd_write_section_stabs
11620 (output_bfd,
8b127cbc 11621 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11622 o, &elf_section_data (o)->sec_info, contents)))
11623 return FALSE;
11624 break;
dbaa2011 11625 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11626 if (! _bfd_write_merged_section (output_bfd, o,
11627 elf_section_data (o)->sec_info))
11628 return FALSE;
11629 break;
dbaa2011 11630 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11631 {
8b127cbc 11632 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11633 o, contents))
11634 return FALSE;
11635 }
11636 break;
2f0c68f2
CM
11637 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11638 {
11639 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11640 flinfo->info,
11641 o, contents))
11642 return FALSE;
11643 }
11644 break;
c152c796
AM
11645 default:
11646 {
310fd250
L
11647 if (! (o->flags & SEC_EXCLUDE))
11648 {
11649 file_ptr offset = (file_ptr) o->output_offset;
11650 bfd_size_type todo = o->size;
37b01f6a 11651
bb294208 11652 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11653
310fd250
L
11654 if ((o->flags & SEC_ELF_REVERSE_COPY))
11655 {
11656 /* Reverse-copy input section to output. */
11657 do
11658 {
11659 todo -= address_size;
11660 if (! bfd_set_section_contents (output_bfd,
11661 o->output_section,
11662 contents + todo,
11663 offset,
11664 address_size))
11665 return FALSE;
11666 if (todo == 0)
11667 break;
11668 offset += address_size;
11669 }
11670 while (1);
11671 }
11672 else if (! bfd_set_section_contents (output_bfd,
11673 o->output_section,
11674 contents,
11675 offset, todo))
11676 return FALSE;
11677 }
c152c796
AM
11678 }
11679 break;
11680 }
11681 }
11682
11683 return TRUE;
11684}
11685
11686/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11687 requested by the linker, and does not come from any input file. This
c152c796
AM
11688 is used to build constructor and destructor tables when linking
11689 with -Ur. */
11690
11691static bfd_boolean
11692elf_reloc_link_order (bfd *output_bfd,
11693 struct bfd_link_info *info,
11694 asection *output_section,
11695 struct bfd_link_order *link_order)
11696{
11697 reloc_howto_type *howto;
11698 long indx;
11699 bfd_vma offset;
11700 bfd_vma addend;
d4730f92 11701 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11702 struct elf_link_hash_entry **rel_hash_ptr;
11703 Elf_Internal_Shdr *rel_hdr;
11704 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11705 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11706 bfd_byte *erel;
11707 unsigned int i;
d4730f92 11708 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11709
11710 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11711 if (howto == NULL)
11712 {
11713 bfd_set_error (bfd_error_bad_value);
11714 return FALSE;
11715 }
11716
11717 addend = link_order->u.reloc.p->addend;
11718
d4730f92
BS
11719 if (esdo->rel.hdr)
11720 reldata = &esdo->rel;
11721 else if (esdo->rela.hdr)
11722 reldata = &esdo->rela;
11723 else
11724 {
11725 reldata = NULL;
11726 BFD_ASSERT (0);
11727 }
11728
c152c796 11729 /* Figure out the symbol index. */
d4730f92 11730 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11731 if (link_order->type == bfd_section_reloc_link_order)
11732 {
11733 indx = link_order->u.reloc.p->u.section->target_index;
11734 BFD_ASSERT (indx != 0);
11735 *rel_hash_ptr = NULL;
11736 }
11737 else
11738 {
11739 struct elf_link_hash_entry *h;
11740
11741 /* Treat a reloc against a defined symbol as though it were
11742 actually against the section. */
11743 h = ((struct elf_link_hash_entry *)
11744 bfd_wrapped_link_hash_lookup (output_bfd, info,
11745 link_order->u.reloc.p->u.name,
11746 FALSE, FALSE, TRUE));
11747 if (h != NULL
11748 && (h->root.type == bfd_link_hash_defined
11749 || h->root.type == bfd_link_hash_defweak))
11750 {
11751 asection *section;
11752
11753 section = h->root.u.def.section;
11754 indx = section->output_section->target_index;
11755 *rel_hash_ptr = NULL;
11756 /* It seems that we ought to add the symbol value to the
11757 addend here, but in practice it has already been added
11758 because it was passed to constructor_callback. */
11759 addend += section->output_section->vma + section->output_offset;
11760 }
11761 else if (h != NULL)
11762 {
11763 /* Setting the index to -2 tells elf_link_output_extsym that
11764 this symbol is used by a reloc. */
11765 h->indx = -2;
11766 *rel_hash_ptr = h;
11767 indx = 0;
11768 }
11769 else
11770 {
1a72702b
AM
11771 (*info->callbacks->unattached_reloc)
11772 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11773 indx = 0;
11774 }
11775 }
11776
11777 /* If this is an inplace reloc, we must write the addend into the
11778 object file. */
11779 if (howto->partial_inplace && addend != 0)
11780 {
11781 bfd_size_type size;
11782 bfd_reloc_status_type rstat;
11783 bfd_byte *buf;
11784 bfd_boolean ok;
11785 const char *sym_name;
bb294208 11786 bfd_size_type octets;
c152c796 11787
a50b1753
NC
11788 size = (bfd_size_type) bfd_get_reloc_size (howto);
11789 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11790 if (buf == NULL && size != 0)
c152c796
AM
11791 return FALSE;
11792 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11793 switch (rstat)
11794 {
11795 case bfd_reloc_ok:
11796 break;
11797
11798 default:
11799 case bfd_reloc_outofrange:
11800 abort ();
11801
11802 case bfd_reloc_overflow:
11803 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11804 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11805 else
11806 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11807 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11808 howto->name, addend, NULL, NULL,
11809 (bfd_vma) 0);
c152c796
AM
11810 break;
11811 }
37b01f6a 11812
bb294208
AM
11813 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11814 output_section);
c152c796 11815 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11816 octets, size);
c152c796
AM
11817 free (buf);
11818 if (! ok)
11819 return FALSE;
11820 }
11821
11822 /* The address of a reloc is relative to the section in a
11823 relocatable file, and is a virtual address in an executable
11824 file. */
11825 offset = link_order->offset;
0e1862bb 11826 if (! bfd_link_relocatable (info))
c152c796
AM
11827 offset += output_section->vma;
11828
11829 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11830 {
11831 irel[i].r_offset = offset;
11832 irel[i].r_info = 0;
11833 irel[i].r_addend = 0;
11834 }
11835 if (bed->s->arch_size == 32)
11836 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11837 else
11838 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11839
d4730f92 11840 rel_hdr = reldata->hdr;
c152c796
AM
11841 erel = rel_hdr->contents;
11842 if (rel_hdr->sh_type == SHT_REL)
11843 {
d4730f92 11844 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11845 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11846 }
11847 else
11848 {
11849 irel[0].r_addend = addend;
d4730f92 11850 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11851 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11852 }
11853
d4730f92 11854 ++reldata->count;
c152c796
AM
11855
11856 return TRUE;
11857}
11858
0b52efa6 11859
0b52efa6
PB
11860/* Compare two sections based on the locations of the sections they are
11861 linked to. Used by elf_fixup_link_order. */
11862
11863static int
8c1c5e5d 11864compare_link_order (const void *a, const void *b)
0b52efa6 11865{
8c1c5e5d
AM
11866 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11867 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11868 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11869 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11870 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11871 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11872
0b52efa6
PB
11873 if (apos < bpos)
11874 return -1;
8c1c5e5d
AM
11875 if (apos > bpos)
11876 return 1;
11877
11878 /* The only way we should get matching LMAs is when the first of two
11879 sections has zero size. */
11880 if (asec->size < bsec->size)
11881 return -1;
11882 if (asec->size > bsec->size)
11883 return 1;
11884
11885 /* If they are both zero size then they almost certainly have the same
11886 VMA and thus are not ordered with respect to each other. Test VMA
11887 anyway, and fall back to id to make the result reproducible across
11888 qsort implementations. */
11889 apos = asec->output_section->vma + asec->output_offset;
11890 bpos = bsec->output_section->vma + bsec->output_offset;
11891 if (apos < bpos)
11892 return -1;
11893 if (apos > bpos)
11894 return 1;
11895
11896 return asec->id - bsec->id;
0b52efa6
PB
11897}
11898
11899
11900/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11901 order as their linked sections. Returns false if this could not be done
11902 because an output section includes both ordered and unordered
11903 sections. Ideally we'd do this in the linker proper. */
11904
11905static bfd_boolean
11906elf_fixup_link_order (bfd *abfd, asection *o)
11907{
8c1c5e5d
AM
11908 size_t seen_linkorder;
11909 size_t seen_other;
11910 size_t n;
0b52efa6
PB
11911 struct bfd_link_order *p;
11912 bfd *sub;
0b52efa6 11913 struct bfd_link_order **sections;
66631823
CE
11914 asection *other_sec, *linkorder_sec;
11915 bfd_vma offset; /* Octets. */
3b36f7e6 11916
d33cdfe3
L
11917 other_sec = NULL;
11918 linkorder_sec = NULL;
0b52efa6
PB
11919 seen_other = 0;
11920 seen_linkorder = 0;
8423293d 11921 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11922 {
d33cdfe3 11923 if (p->type == bfd_indirect_link_order)
0b52efa6 11924 {
66631823 11925 asection *s = p->u.indirect.section;
d33cdfe3 11926 sub = s->owner;
847d5183
AM
11927 if ((s->flags & SEC_LINKER_CREATED) == 0
11928 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11929 && elf_section_data (s) != NULL
11930 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11931 {
11932 seen_linkorder++;
11933 linkorder_sec = s;
11934 }
0b52efa6 11935 else
d33cdfe3
L
11936 {
11937 seen_other++;
11938 other_sec = s;
11939 }
0b52efa6
PB
11940 }
11941 else
11942 seen_other++;
d33cdfe3
L
11943
11944 if (seen_other && seen_linkorder)
11945 {
11946 if (other_sec && linkorder_sec)
4eca0228 11947 _bfd_error_handler
695344c0 11948 /* xgettext:c-format */
871b3ab2
AM
11949 (_("%pA has both ordered [`%pA' in %pB] "
11950 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11951 o, linkorder_sec, linkorder_sec->owner,
11952 other_sec, other_sec->owner);
d33cdfe3 11953 else
4eca0228 11954 _bfd_error_handler
871b3ab2 11955 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11956 bfd_set_error (bfd_error_bad_value);
11957 return FALSE;
11958 }
0b52efa6
PB
11959 }
11960
11961 if (!seen_linkorder)
11962 return TRUE;
11963
8c1c5e5d 11964 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11965 if (sections == NULL)
11966 return FALSE;
3b36f7e6 11967
8c1c5e5d 11968 seen_linkorder = 0;
8423293d 11969 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11970 sections[seen_linkorder++] = p;
11971
0b52efa6 11972 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11973 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11974
11975 /* Change the offsets of the sections. */
11976 offset = 0;
11977 for (n = 0; n < seen_linkorder; n++)
11978 {
8c1c5e5d 11979 bfd_vma mask;
66631823
CE
11980 asection *s = sections[n]->u.indirect.section;
11981 unsigned int opb = bfd_octets_per_byte (abfd, s);
11982
11983 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11984 offset = (offset + ~mask) & mask;
66631823 11985 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11986 offset += sections[n]->size;
11987 }
11988
4dd07732 11989 free (sections);
0b52efa6
PB
11990 return TRUE;
11991}
11992
76359541
TP
11993/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11994 Returns TRUE upon success, FALSE otherwise. */
11995
11996static bfd_boolean
11997elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11998{
11999 bfd_boolean ret = FALSE;
12000 bfd *implib_bfd;
12001 const struct elf_backend_data *bed;
12002 flagword flags;
12003 enum bfd_architecture arch;
12004 unsigned int mach;
12005 asymbol **sympp = NULL;
12006 long symsize;
12007 long symcount;
12008 long src_count;
12009 elf_symbol_type *osymbuf;
446f7ed5 12010 size_t amt;
76359541
TP
12011
12012 implib_bfd = info->out_implib_bfd;
12013 bed = get_elf_backend_data (abfd);
12014
12015 if (!bfd_set_format (implib_bfd, bfd_object))
12016 return FALSE;
12017
046734ff 12018 /* Use flag from executable but make it a relocatable object. */
76359541
TP
12019 flags = bfd_get_file_flags (abfd);
12020 flags &= ~HAS_RELOC;
12021 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 12022 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
12023 return FALSE;
12024
12025 /* Copy architecture of output file to import library file. */
12026 arch = bfd_get_arch (abfd);
12027 mach = bfd_get_mach (abfd);
12028 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12029 && (abfd->target_defaulted
12030 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12031 return FALSE;
12032
12033 /* Get symbol table size. */
12034 symsize = bfd_get_symtab_upper_bound (abfd);
12035 if (symsize < 0)
12036 return FALSE;
12037
12038 /* Read in the symbol table. */
ec9bd0a2
AM
12039 sympp = (asymbol **) bfd_malloc (symsize);
12040 if (sympp == NULL)
12041 return FALSE;
12042
76359541
TP
12043 symcount = bfd_canonicalize_symtab (abfd, sympp);
12044 if (symcount < 0)
12045 goto free_sym_buf;
12046
12047 /* Allow the BFD backend to copy any private header data it
12048 understands from the output BFD to the import library BFD. */
12049 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12050 goto free_sym_buf;
12051
12052 /* Filter symbols to appear in the import library. */
12053 if (bed->elf_backend_filter_implib_symbols)
12054 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12055 symcount);
12056 else
12057 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12058 if (symcount == 0)
12059 {
5df1bc57 12060 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12061 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12062 implib_bfd);
76359541
TP
12063 goto free_sym_buf;
12064 }
12065
12066
12067 /* Make symbols absolute. */
446f7ed5
AM
12068 amt = symcount * sizeof (*osymbuf);
12069 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12070 if (osymbuf == NULL)
12071 goto free_sym_buf;
12072
76359541
TP
12073 for (src_count = 0; src_count < symcount; src_count++)
12074 {
12075 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12076 sizeof (*osymbuf));
12077 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12078 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12079 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12080 osymbuf[src_count].internal_elf_sym.st_value =
12081 osymbuf[src_count].symbol.value;
12082 sympp[src_count] = &osymbuf[src_count].symbol;
12083 }
12084
12085 bfd_set_symtab (implib_bfd, sympp, symcount);
12086
12087 /* Allow the BFD backend to copy any private data it understands
12088 from the output BFD to the import library BFD. This is done last
12089 to permit the routine to look at the filtered symbol table. */
12090 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12091 goto free_sym_buf;
12092
12093 if (!bfd_close (implib_bfd))
12094 goto free_sym_buf;
12095
12096 ret = TRUE;
12097
dc1e8a47 12098 free_sym_buf:
76359541
TP
12099 free (sympp);
12100 return ret;
12101}
12102
9f7c3e5e
AM
12103static void
12104elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12105{
12106 asection *o;
12107
12108 if (flinfo->symstrtab != NULL)
ef10c3ac 12109 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12110 free (flinfo->contents);
12111 free (flinfo->external_relocs);
12112 free (flinfo->internal_relocs);
12113 free (flinfo->external_syms);
12114 free (flinfo->locsym_shndx);
12115 free (flinfo->internal_syms);
12116 free (flinfo->indices);
12117 free (flinfo->sections);
12118 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12119 free (flinfo->symshndxbuf);
12120 for (o = obfd->sections; o != NULL; o = o->next)
12121 {
12122 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12123 free (esdo->rel.hashes);
12124 free (esdo->rela.hashes);
9f7c3e5e
AM
12125 }
12126}
0b52efa6 12127
c152c796
AM
12128/* Do the final step of an ELF link. */
12129
12130bfd_boolean
12131bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12132{
12133 bfd_boolean dynamic;
12134 bfd_boolean emit_relocs;
12135 bfd *dynobj;
8b127cbc 12136 struct elf_final_link_info flinfo;
91d6fa6a
NC
12137 asection *o;
12138 struct bfd_link_order *p;
12139 bfd *sub;
c152c796
AM
12140 bfd_size_type max_contents_size;
12141 bfd_size_type max_external_reloc_size;
12142 bfd_size_type max_internal_reloc_count;
12143 bfd_size_type max_sym_count;
12144 bfd_size_type max_sym_shndx_count;
c152c796
AM
12145 Elf_Internal_Sym elfsym;
12146 unsigned int i;
12147 Elf_Internal_Shdr *symtab_hdr;
12148 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12149 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12150 struct elf_outext_info eoinfo;
12151 bfd_boolean merged;
12152 size_t relativecount = 0;
12153 asection *reldyn = 0;
12154 bfd_size_type amt;
104d59d1
JM
12155 asection *attr_section = NULL;
12156 bfd_vma attr_size = 0;
12157 const char *std_attrs_section;
64f52338 12158 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12159 bfd_boolean sections_removed;
496afd17 12160 bfd_boolean ret;
c152c796 12161
64f52338 12162 if (!is_elf_hash_table (htab))
c152c796
AM
12163 return FALSE;
12164
0e1862bb 12165 if (bfd_link_pic (info))
c152c796
AM
12166 abfd->flags |= DYNAMIC;
12167
64f52338
AM
12168 dynamic = htab->dynamic_sections_created;
12169 dynobj = htab->dynobj;
c152c796 12170
0e1862bb 12171 emit_relocs = (bfd_link_relocatable (info)
a4676736 12172 || info->emitrelocations);
c152c796 12173
496afd17 12174 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12175 flinfo.info = info;
12176 flinfo.output_bfd = abfd;
ef10c3ac 12177 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12178 if (flinfo.symstrtab == NULL)
c152c796
AM
12179 return FALSE;
12180
12181 if (! dynamic)
12182 {
8b127cbc
AM
12183 flinfo.hash_sec = NULL;
12184 flinfo.symver_sec = NULL;
c152c796
AM
12185 }
12186 else
12187 {
3d4d4302 12188 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12189 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12190 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12191 /* Note that it is OK if symver_sec is NULL. */
12192 }
12193
496afd17
L
12194 if (info->unique_symbol
12195 && !bfd_hash_table_init (&flinfo.local_hash_table,
12196 local_hash_newfunc,
12197 sizeof (struct local_hash_entry)))
12198 return FALSE;
c152c796 12199
104d59d1
JM
12200 /* The object attributes have been merged. Remove the input
12201 sections from the link, and set the contents of the output
1ff6de03 12202 section. */
4c6ee646 12203 sections_removed = FALSE;
104d59d1
JM
12204 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12205 for (o = abfd->sections; o != NULL; o = o->next)
12206 {
5270eddc 12207 bfd_boolean remove_section = FALSE;
b8a6ced7 12208
104d59d1
JM
12209 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12210 || strcmp (o->name, ".gnu.attributes") == 0)
12211 {
12212 for (p = o->map_head.link_order; p != NULL; p = p->next)
12213 {
12214 asection *input_section;
12215
12216 if (p->type != bfd_indirect_link_order)
12217 continue;
12218 input_section = p->u.indirect.section;
12219 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12220 elf_link_input_bfd ignores this section. */
12221 input_section->flags &= ~SEC_HAS_CONTENTS;
12222 }
a0c8462f 12223
104d59d1 12224 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12225 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12226 /* Skip this section later on. */
12227 o->map_head.link_order = NULL;
104d59d1 12228 if (attr_size)
b8a6ced7 12229 attr_section = o;
104d59d1 12230 else
5270eddc 12231 remove_section = TRUE;
104d59d1 12232 }
6e5e9d58
AM
12233 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12234 {
12235 /* Remove empty group section from linker output. */
5270eddc 12236 remove_section = TRUE;
b8a6ced7 12237 }
5270eddc 12238 if (remove_section)
b8a6ced7 12239 {
6e5e9d58
AM
12240 o->flags |= SEC_EXCLUDE;
12241 bfd_section_list_remove (abfd, o);
12242 abfd->section_count--;
4c6ee646 12243 sections_removed = TRUE;
6e5e9d58 12244 }
104d59d1 12245 }
4c6ee646
AM
12246 if (sections_removed)
12247 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12248
c152c796
AM
12249 /* Count up the number of relocations we will output for each output
12250 section, so that we know the sizes of the reloc sections. We
12251 also figure out some maximum sizes. */
12252 max_contents_size = 0;
12253 max_external_reloc_size = 0;
12254 max_internal_reloc_count = 0;
12255 max_sym_count = 0;
12256 max_sym_shndx_count = 0;
12257 merged = FALSE;
12258 for (o = abfd->sections; o != NULL; o = o->next)
12259 {
12260 struct bfd_elf_section_data *esdo = elf_section_data (o);
12261 o->reloc_count = 0;
12262
8423293d 12263 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12264 {
12265 unsigned int reloc_count = 0;
9eaff861 12266 unsigned int additional_reloc_count = 0;
c152c796 12267 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12268
12269 if (p->type == bfd_section_reloc_link_order
12270 || p->type == bfd_symbol_reloc_link_order)
12271 reloc_count = 1;
12272 else if (p->type == bfd_indirect_link_order)
12273 {
12274 asection *sec;
12275
12276 sec = p->u.indirect.section;
c152c796
AM
12277
12278 /* Mark all sections which are to be included in the
12279 link. This will normally be every section. We need
12280 to do this so that we can identify any sections which
12281 the linker has decided to not include. */
12282 sec->linker_mark = TRUE;
12283
12284 if (sec->flags & SEC_MERGE)
12285 merged = TRUE;
12286
eea6121a
AM
12287 if (sec->rawsize > max_contents_size)
12288 max_contents_size = sec->rawsize;
12289 if (sec->size > max_contents_size)
12290 max_contents_size = sec->size;
c152c796 12291
c152c796
AM
12292 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12293 && (sec->owner->flags & DYNAMIC) == 0)
12294 {
12295 size_t sym_count;
12296
a961cdd5
AM
12297 /* We are interested in just local symbols, not all
12298 symbols. */
c152c796
AM
12299 if (elf_bad_symtab (sec->owner))
12300 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12301 / bed->s->sizeof_sym);
12302 else
12303 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12304
12305 if (sym_count > max_sym_count)
12306 max_sym_count = sym_count;
12307
12308 if (sym_count > max_sym_shndx_count
6a40cf0c 12309 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12310 max_sym_shndx_count = sym_count;
12311
a961cdd5
AM
12312 if (esdo->this_hdr.sh_type == SHT_REL
12313 || esdo->this_hdr.sh_type == SHT_RELA)
12314 /* Some backends use reloc_count in relocation sections
12315 to count particular types of relocs. Of course,
12316 reloc sections themselves can't have relocations. */
12317 ;
12318 else if (emit_relocs)
12319 {
12320 reloc_count = sec->reloc_count;
12321 if (bed->elf_backend_count_additional_relocs)
12322 {
12323 int c;
12324 c = (*bed->elf_backend_count_additional_relocs) (sec);
12325 additional_reloc_count += c;
12326 }
12327 }
12328 else if (bed->elf_backend_count_relocs)
12329 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12330
12331 esdi = elf_section_data (sec);
12332
c152c796
AM
12333 if ((sec->flags & SEC_RELOC) != 0)
12334 {
d4730f92 12335 size_t ext_size = 0;
c152c796 12336
d4730f92
BS
12337 if (esdi->rel.hdr != NULL)
12338 ext_size = esdi->rel.hdr->sh_size;
12339 if (esdi->rela.hdr != NULL)
12340 ext_size += esdi->rela.hdr->sh_size;
7326c758 12341
c152c796
AM
12342 if (ext_size > max_external_reloc_size)
12343 max_external_reloc_size = ext_size;
12344 if (sec->reloc_count > max_internal_reloc_count)
12345 max_internal_reloc_count = sec->reloc_count;
12346 }
12347 }
12348 }
12349
12350 if (reloc_count == 0)
12351 continue;
12352
9eaff861 12353 reloc_count += additional_reloc_count;
c152c796
AM
12354 o->reloc_count += reloc_count;
12355
0e1862bb 12356 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12357 {
d4730f92 12358 if (esdi->rel.hdr)
9eaff861 12359 {
491d01d3 12360 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12361 esdo->rel.count += additional_reloc_count;
12362 }
d4730f92 12363 if (esdi->rela.hdr)
9eaff861 12364 {
491d01d3 12365 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12366 esdo->rela.count += additional_reloc_count;
12367 }
d4730f92
BS
12368 }
12369 else
12370 {
12371 if (o->use_rela_p)
12372 esdo->rela.count += reloc_count;
2c2b4ed4 12373 else
d4730f92 12374 esdo->rel.count += reloc_count;
c152c796 12375 }
c152c796
AM
12376 }
12377
9eaff861 12378 if (o->reloc_count > 0)
c152c796
AM
12379 o->flags |= SEC_RELOC;
12380 else
12381 {
12382 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12383 set it (this is probably a bug) and if it is set
12384 assign_section_numbers will create a reloc section. */
12385 o->flags &=~ SEC_RELOC;
12386 }
12387
12388 /* If the SEC_ALLOC flag is not set, force the section VMA to
12389 zero. This is done in elf_fake_sections as well, but forcing
12390 the VMA to 0 here will ensure that relocs against these
12391 sections are handled correctly. */
12392 if ((o->flags & SEC_ALLOC) == 0
12393 && ! o->user_set_vma)
12394 o->vma = 0;
12395 }
12396
0e1862bb 12397 if (! bfd_link_relocatable (info) && merged)
64f52338 12398 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12399
12400 /* Figure out the file positions for everything but the symbol table
12401 and the relocs. We set symcount to force assign_section_numbers
12402 to create a symbol table. */
ed48ec2e 12403 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12404 BFD_ASSERT (! abfd->output_has_begun);
12405 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12406 goto error_return;
12407
ee75fd95 12408 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12409 for (o = abfd->sections; o != NULL; o = o->next)
12410 {
d4730f92 12411 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12412 if ((o->flags & SEC_RELOC) != 0)
12413 {
d4730f92 12414 if (esdo->rel.hdr
9eaff861 12415 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12416 goto error_return;
12417
d4730f92 12418 if (esdo->rela.hdr
9eaff861 12419 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12420 goto error_return;
12421 }
12422
48db3297
AM
12423 /* _bfd_elf_compute_section_file_positions makes temporary use
12424 of target_index. Reset it. */
12425 o->target_index = 0;
12426
c152c796
AM
12427 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12428 to count upwards while actually outputting the relocations. */
d4730f92
BS
12429 esdo->rel.count = 0;
12430 esdo->rela.count = 0;
0ce398f1 12431
1ff6de03
NA
12432 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12433 && !bfd_section_is_ctf (o))
0ce398f1
L
12434 {
12435 /* Cache the section contents so that they can be compressed
12436 later. Use bfd_malloc since it will be freed by
12437 bfd_compress_section_contents. */
12438 unsigned char *contents = esdo->this_hdr.contents;
12439 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12440 abort ();
12441 contents
12442 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12443 if (contents == NULL)
12444 goto error_return;
12445 esdo->this_hdr.contents = contents;
12446 }
c152c796
AM
12447 }
12448
1ff6de03
NA
12449 /* We have now assigned file positions for all the sections except .symtab,
12450 .strtab, and non-loaded reloc and compressed debugging sections. We start
12451 the .symtab section at the current file position, and write directly to it.
12452 We build the .strtab section in memory. */
ed48ec2e 12453 abfd->symcount = 0;
c152c796
AM
12454 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12455 /* sh_name is set in prep_headers. */
12456 symtab_hdr->sh_type = SHT_SYMTAB;
12457 /* sh_flags, sh_addr and sh_size all start off zero. */
12458 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12459 /* sh_link is set in assign_section_numbers. */
12460 /* sh_info is set below. */
12461 /* sh_offset is set just below. */
72de5009 12462 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12463
ef10c3ac
L
12464 if (max_sym_count < 20)
12465 max_sym_count = 20;
64f52338 12466 htab->strtabsize = max_sym_count;
ef10c3ac 12467 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12468 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12469 if (htab->strtab == NULL)
c152c796 12470 goto error_return;
ef10c3ac
L
12471 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12472 flinfo.symshndxbuf
12473 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12474 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12475
8539e4e8 12476 if (info->strip != strip_all || emit_relocs)
c152c796 12477 {
c77cb2a0
MR
12478 bfd_boolean name_local_sections;
12479 const char *name;
12480
8539e4e8
AM
12481 file_ptr off = elf_next_file_pos (abfd);
12482
12483 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12484
12485 /* Note that at this point elf_next_file_pos (abfd) is
12486 incorrect. We do not yet know the size of the .symtab section.
12487 We correct next_file_pos below, after we do know the size. */
12488
12489 /* Start writing out the symbol table. The first symbol is always a
12490 dummy symbol. */
c152c796
AM
12491 elfsym.st_value = 0;
12492 elfsym.st_size = 0;
12493 elfsym.st_info = 0;
12494 elfsym.st_other = 0;
12495 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12496 elfsym.st_target_internal = 0;
ef10c3ac
L
12497 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12498 bfd_und_section_ptr, NULL) != 1)
c152c796 12499 goto error_return;
c152c796 12500
8539e4e8
AM
12501 /* Output a symbol for each section. We output these even if we are
12502 discarding local symbols, since they are used for relocs. These
c77cb2a0
MR
12503 symbols usually have no names. We store the index of each one in
12504 the index field of the section, so that we can find it again when
8539e4e8
AM
12505 outputting relocs. */
12506
c77cb2a0
MR
12507 name_local_sections
12508 = (bed->elf_backend_name_local_section_symbols
12509 && bed->elf_backend_name_local_section_symbols (abfd));
12510
12511 name = NULL;
c152c796
AM
12512 elfsym.st_size = 0;
12513 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12514 elfsym.st_other = 0;
f0b5bb34 12515 elfsym.st_value = 0;
35fc36a8 12516 elfsym.st_target_internal = 0;
c152c796
AM
12517 for (i = 1; i < elf_numsections (abfd); i++)
12518 {
12519 o = bfd_section_from_elf_index (abfd, i);
12520 if (o != NULL)
f0b5bb34
AM
12521 {
12522 o->target_index = bfd_get_symcount (abfd);
12523 elfsym.st_shndx = i;
0e1862bb 12524 if (!bfd_link_relocatable (info))
f0b5bb34 12525 elfsym.st_value = o->vma;
c77cb2a0
MR
12526 if (name_local_sections)
12527 name = o->name;
12528 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
ef10c3ac 12529 NULL) != 1)
f0b5bb34
AM
12530 goto error_return;
12531 }
c152c796
AM
12532 }
12533 }
12534
3f1b17bb
MR
12535 /* On some targets like Irix 5 the symbol split between local and global
12536 ones recorded in the sh_info field needs to be done between section
12537 and all other symbols. */
12538 if (bed->elf_backend_elfsym_local_is_section
12539 && bed->elf_backend_elfsym_local_is_section (abfd))
12540 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12541
c152c796
AM
12542 /* Allocate some memory to hold information read in from the input
12543 files. */
12544 if (max_contents_size != 0)
12545 {
8b127cbc
AM
12546 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12547 if (flinfo.contents == NULL)
c152c796
AM
12548 goto error_return;
12549 }
12550
12551 if (max_external_reloc_size != 0)
12552 {
8b127cbc
AM
12553 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12554 if (flinfo.external_relocs == NULL)
c152c796
AM
12555 goto error_return;
12556 }
12557
12558 if (max_internal_reloc_count != 0)
12559 {
056bafd4 12560 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12561 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12562 if (flinfo.internal_relocs == NULL)
c152c796
AM
12563 goto error_return;
12564 }
12565
12566 if (max_sym_count != 0)
12567 {
12568 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12569 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12570 if (flinfo.external_syms == NULL)
c152c796
AM
12571 goto error_return;
12572
12573 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12574 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12575 if (flinfo.internal_syms == NULL)
c152c796
AM
12576 goto error_return;
12577
12578 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12579 flinfo.indices = (long int *) bfd_malloc (amt);
12580 if (flinfo.indices == NULL)
c152c796
AM
12581 goto error_return;
12582
12583 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12584 flinfo.sections = (asection **) bfd_malloc (amt);
12585 if (flinfo.sections == NULL)
c152c796
AM
12586 goto error_return;
12587 }
12588
12589 if (max_sym_shndx_count != 0)
12590 {
12591 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12592 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12593 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12594 goto error_return;
12595 }
12596
64f52338 12597 if (htab->tls_sec)
c152c796 12598 {
66631823 12599 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12600 asection *sec;
12601
64f52338 12602 for (sec = htab->tls_sec;
c152c796
AM
12603 sec && (sec->flags & SEC_THREAD_LOCAL);
12604 sec = sec->next)
12605 {
3a800eb9 12606 bfd_size_type size = sec->size;
66631823 12607 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12608
3a800eb9
AM
12609 if (size == 0
12610 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12611 {
91d6fa6a
NC
12612 struct bfd_link_order *ord = sec->map_tail.link_order;
12613
12614 if (ord != NULL)
66631823 12615 size = ord->offset * opb + ord->size;
c152c796 12616 }
66631823 12617 end = sec->vma + size / opb;
c152c796 12618 }
64f52338 12619 base = htab->tls_sec->vma;
7dc98aea
RO
12620 /* Only align end of TLS section if static TLS doesn't have special
12621 alignment requirements. */
12622 if (bed->static_tls_alignment == 1)
64f52338
AM
12623 end = align_power (end, htab->tls_sec->alignment_power);
12624 htab->tls_size = end - base;
c152c796
AM
12625 }
12626
0b52efa6
PB
12627 /* Reorder SHF_LINK_ORDER sections. */
12628 for (o = abfd->sections; o != NULL; o = o->next)
12629 {
12630 if (!elf_fixup_link_order (abfd, o))
12631 return FALSE;
12632 }
12633
2f0c68f2
CM
12634 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12635 return FALSE;
12636
c152c796
AM
12637 /* Since ELF permits relocations to be against local symbols, we
12638 must have the local symbols available when we do the relocations.
12639 Since we would rather only read the local symbols once, and we
12640 would rather not keep them in memory, we handle all the
12641 relocations for a single input file at the same time.
12642
12643 Unfortunately, there is no way to know the total number of local
12644 symbols until we have seen all of them, and the local symbol
12645 indices precede the global symbol indices. This means that when
12646 we are generating relocatable output, and we see a reloc against
12647 a global symbol, we can not know the symbol index until we have
12648 finished examining all the local symbols to see which ones we are
12649 going to output. To deal with this, we keep the relocations in
12650 memory, and don't output them until the end of the link. This is
12651 an unfortunate waste of memory, but I don't see a good way around
12652 it. Fortunately, it only happens when performing a relocatable
12653 link, which is not the common case. FIXME: If keep_memory is set
12654 we could write the relocs out and then read them again; I don't
12655 know how bad the memory loss will be. */
12656
c72f2fb2 12657 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12658 sub->output_has_begun = FALSE;
12659 for (o = abfd->sections; o != NULL; o = o->next)
12660 {
8423293d 12661 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12662 {
12663 if (p->type == bfd_indirect_link_order
12664 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12665 == bfd_target_elf_flavour)
12666 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12667 {
12668 if (! sub->output_has_begun)
12669 {
8b127cbc 12670 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12671 goto error_return;
12672 sub->output_has_begun = TRUE;
12673 }
12674 }
12675 else if (p->type == bfd_section_reloc_link_order
12676 || p->type == bfd_symbol_reloc_link_order)
12677 {
12678 if (! elf_reloc_link_order (abfd, info, o, p))
12679 goto error_return;
12680 }
12681 else
12682 {
12683 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12684 {
12685 if (p->type == bfd_indirect_link_order
12686 && (bfd_get_flavour (sub)
12687 == bfd_target_elf_flavour)
12688 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12689 != bed->s->elfclass))
12690 {
12691 const char *iclass, *oclass;
12692
aebf9be7 12693 switch (bed->s->elfclass)
351f65ca 12694 {
aebf9be7
NC
12695 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12696 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12697 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12698 default: abort ();
351f65ca 12699 }
aebf9be7
NC
12700
12701 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12702 {
aebf9be7
NC
12703 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12704 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12705 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12706 default: abort ();
351f65ca
L
12707 }
12708
12709 bfd_set_error (bfd_error_wrong_format);
4eca0228 12710 _bfd_error_handler
695344c0 12711 /* xgettext:c-format */
871b3ab2 12712 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12713 sub, iclass, oclass);
12714 }
12715
12716 goto error_return;
12717 }
c152c796
AM
12718 }
12719 }
12720 }
12721
c0f00686
L
12722 /* Free symbol buffer if needed. */
12723 if (!info->reduce_memory_overheads)
12724 {
c72f2fb2 12725 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12726 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12727 {
12728 free (elf_tdata (sub)->symbuf);
12729 elf_tdata (sub)->symbuf = NULL;
12730 }
12731 }
12732
496afd17
L
12733 ret = TRUE;
12734
c152c796
AM
12735 /* Output any global symbols that got converted to local in a
12736 version script or due to symbol visibility. We do this in a
12737 separate step since ELF requires all local symbols to appear
12738 prior to any global symbols. FIXME: We should only do this if
12739 some global symbols were, in fact, converted to become local.
12740 FIXME: Will this work correctly with the Irix 5 linker? */
12741 eoinfo.failed = FALSE;
8b127cbc 12742 eoinfo.flinfo = &flinfo;
c152c796 12743 eoinfo.localsyms = TRUE;
34a79995 12744 eoinfo.file_sym_done = FALSE;
7686d77d 12745 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12746 if (eoinfo.failed)
496afd17
L
12747 {
12748 ret = FALSE;
12749 goto return_local_hash_table;
12750 }
c152c796 12751
4e617b1e
PB
12752 /* If backend needs to output some local symbols not present in the hash
12753 table, do it now. */
8539e4e8
AM
12754 if (bed->elf_backend_output_arch_local_syms
12755 && (info->strip != strip_all || emit_relocs))
4e617b1e 12756 {
6e0b88f1 12757 typedef int (*out_sym_func)
4e617b1e
PB
12758 (void *, const char *, Elf_Internal_Sym *, asection *,
12759 struct elf_link_hash_entry *);
12760
12761 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12762 (abfd, info, &flinfo,
12763 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12764 {
12765 ret = FALSE;
12766 goto return_local_hash_table;
12767 }
4e617b1e
PB
12768 }
12769
c152c796
AM
12770 /* That wrote out all the local symbols. Finish up the symbol table
12771 with the global symbols. Even if we want to strip everything we
12772 can, we still need to deal with those global symbols that got
12773 converted to local in a version script. */
12774
12775 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12776 if (!symtab_hdr->sh_info)
12777 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12778
12779 if (dynamic
64f52338
AM
12780 && htab->dynsym != NULL
12781 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12782 {
12783 Elf_Internal_Sym sym;
64f52338 12784 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12785
64f52338
AM
12786 o = htab->dynsym->output_section;
12787 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12788
12789 /* Write out the section symbols for the output sections. */
0e1862bb 12790 if (bfd_link_pic (info)
64f52338 12791 || htab->is_relocatable_executable)
c152c796
AM
12792 {
12793 asection *s;
12794
12795 sym.st_size = 0;
12796 sym.st_name = 0;
12797 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12798 sym.st_other = 0;
35fc36a8 12799 sym.st_target_internal = 0;
c152c796
AM
12800
12801 for (s = abfd->sections; s != NULL; s = s->next)
12802 {
12803 int indx;
12804 bfd_byte *dest;
12805 long dynindx;
12806
c152c796 12807 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12808 if (dynindx <= 0)
12809 continue;
12810 indx = elf_section_data (s)->this_idx;
c152c796
AM
12811 BFD_ASSERT (indx > 0);
12812 sym.st_shndx = indx;
c0d5a53d 12813 if (! check_dynsym (abfd, &sym))
496afd17
L
12814 {
12815 ret = FALSE;
12816 goto return_local_hash_table;
12817 }
c152c796
AM
12818 sym.st_value = s->vma;
12819 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12820
12821 /* Inform the linker of the addition of this symbol. */
12822
12823 if (info->callbacks->ctf_new_dynsym)
12824 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12825
c152c796
AM
12826 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12827 }
c152c796
AM
12828 }
12829
12830 /* Write out the local dynsyms. */
64f52338 12831 if (htab->dynlocal)
c152c796
AM
12832 {
12833 struct elf_link_local_dynamic_entry *e;
64f52338 12834 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12835 {
12836 asection *s;
12837 bfd_byte *dest;
12838
935bd1e0 12839 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12840 Note that we saved a word of storage and overwrote
12841 the original st_name with the dynstr_index. */
12842 sym = e->isym;
935bd1e0 12843 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12844 sym.st_shndx = SHN_UNDEF;
c152c796 12845
cb33740c
AM
12846 s = bfd_section_from_elf_index (e->input_bfd,
12847 e->isym.st_shndx);
4d68fd75
AM
12848 if (s != NULL
12849 && s->output_section != NULL
12850 && elf_section_data (s->output_section) != NULL)
c152c796 12851 {
c152c796
AM
12852 sym.st_shndx =
12853 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12854 if (! check_dynsym (abfd, &sym))
496afd17
L
12855 {
12856 ret = FALSE;
12857 goto return_local_hash_table;
12858 }
c152c796
AM
12859 sym.st_value = (s->output_section->vma
12860 + s->output_offset
12861 + e->isym.st_value);
12862 }
12863
3d16b64e
NA
12864 /* Inform the linker of the addition of this symbol. */
12865
12866 if (info->callbacks->ctf_new_dynsym)
12867 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12868
c152c796
AM
12869 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12870 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12871 }
12872 }
c152c796
AM
12873 }
12874
12875 /* We get the global symbols from the hash table. */
12876 eoinfo.failed = FALSE;
12877 eoinfo.localsyms = FALSE;
8b127cbc 12878 eoinfo.flinfo = &flinfo;
7686d77d 12879 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12880 if (eoinfo.failed)
496afd17
L
12881 {
12882 ret = FALSE;
12883 goto return_local_hash_table;
12884 }
c152c796
AM
12885
12886 /* If backend needs to output some symbols not present in the hash
12887 table, do it now. */
8539e4e8
AM
12888 if (bed->elf_backend_output_arch_syms
12889 && (info->strip != strip_all || emit_relocs))
c152c796 12890 {
6e0b88f1 12891 typedef int (*out_sym_func)
c152c796
AM
12892 (void *, const char *, Elf_Internal_Sym *, asection *,
12893 struct elf_link_hash_entry *);
12894
12895 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12896 (abfd, info, &flinfo,
12897 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12898 {
12899 ret = FALSE;
12900 goto return_local_hash_table;
12901 }
c152c796
AM
12902 }
12903
ef10c3ac
L
12904 /* Finalize the .strtab section. */
12905 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12906
12907 /* Swap out the .strtab section. */
12908 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12909 {
12910 ret = FALSE;
12911 goto return_local_hash_table;
12912 }
c152c796
AM
12913
12914 /* Now we know the size of the symtab section. */
c152c796
AM
12915 if (bfd_get_symcount (abfd) > 0)
12916 {
ee3b52e9
L
12917 /* Finish up and write out the symbol string table (.strtab)
12918 section. */
ad32986f 12919 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12920 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12921
ad32986f 12922 if (elf_symtab_shndx_list (abfd))
8539e4e8 12923 {
ad32986f 12924 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12925
ad32986f
NC
12926 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12927 {
12928 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12929 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12930 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12931 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12932 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12933
ad32986f
NC
12934 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12935 off, TRUE);
12936
12937 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12938 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12939 {
12940 ret = FALSE;
12941 goto return_local_hash_table;
12942 }
ad32986f 12943 }
8539e4e8 12944 }
ee3b52e9
L
12945
12946 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12947 /* sh_name was set in prep_headers. */
12948 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12949 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12950 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12951 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12952 symstrtab_hdr->sh_entsize = 0;
12953 symstrtab_hdr->sh_link = 0;
12954 symstrtab_hdr->sh_info = 0;
12955 /* sh_offset is set just below. */
12956 symstrtab_hdr->sh_addralign = 1;
12957
12958 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12959 off, TRUE);
12960 elf_next_file_pos (abfd) = off;
12961
c152c796 12962 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12963 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
12964 {
12965 ret = FALSE;
12966 goto return_local_hash_table;
12967 }
c152c796
AM
12968 }
12969
76359541
TP
12970 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12971 {
871b3ab2 12972 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12973 info->out_implib_bfd);
496afd17
L
12974 ret = FALSE;
12975 goto return_local_hash_table;
76359541
TP
12976 }
12977
c152c796
AM
12978 /* Adjust the relocs to have the correct symbol indices. */
12979 for (o = abfd->sections; o != NULL; o = o->next)
12980 {
d4730f92 12981 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12982 bfd_boolean sort;
10bbbc1d 12983
c152c796
AM
12984 if ((o->flags & SEC_RELOC) == 0)
12985 continue;
12986
28dbcedc 12987 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12988 if (esdo->rel.hdr != NULL
10bbbc1d 12989 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
12990 {
12991 ret = FALSE;
12992 goto return_local_hash_table;
12993 }
bca6d0e3 12994 if (esdo->rela.hdr != NULL
10bbbc1d 12995 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
12996 {
12997 ret = FALSE;
12998 goto return_local_hash_table;
12999 }
c152c796
AM
13000
13001 /* Set the reloc_count field to 0 to prevent write_relocs from
13002 trying to swap the relocs out itself. */
13003 o->reloc_count = 0;
13004 }
13005
13006 if (dynamic && info->combreloc && dynobj != NULL)
13007 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13008
13009 /* If we are linking against a dynamic object, or generating a
13010 shared library, finish up the dynamic linking information. */
13011 if (dynamic)
13012 {
13013 bfd_byte *dyncon, *dynconend;
13014
13015 /* Fix up .dynamic entries. */
3d4d4302 13016 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
13017 BFD_ASSERT (o != NULL);
13018
13019 dyncon = o->contents;
eea6121a 13020 dynconend = o->contents + o->size;
c152c796
AM
13021 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13022 {
13023 Elf_Internal_Dyn dyn;
13024 const char *name;
13025 unsigned int type;
64487780
AM
13026 bfd_size_type sh_size;
13027 bfd_vma sh_addr;
c152c796
AM
13028
13029 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13030
13031 switch (dyn.d_tag)
13032 {
13033 default:
13034 continue;
13035 case DT_NULL:
13036 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13037 {
13038 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13039 {
13040 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13041 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13042 default: continue;
13043 }
13044 dyn.d_un.d_val = relativecount;
13045 relativecount = 0;
13046 break;
13047 }
13048 continue;
13049
13050 case DT_INIT:
13051 name = info->init_function;
13052 goto get_sym;
13053 case DT_FINI:
13054 name = info->fini_function;
13055 get_sym:
13056 {
13057 struct elf_link_hash_entry *h;
13058
64f52338 13059 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
13060 if (h != NULL
13061 && (h->root.type == bfd_link_hash_defined
13062 || h->root.type == bfd_link_hash_defweak))
13063 {
bef26483 13064 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
13065 o = h->root.u.def.section;
13066 if (o->output_section != NULL)
bef26483 13067 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
13068 + o->output_offset);
13069 else
13070 {
13071 /* The symbol is imported from another shared
13072 library and does not apply to this one. */
bef26483 13073 dyn.d_un.d_ptr = 0;
c152c796
AM
13074 }
13075 break;
13076 }
13077 }
13078 continue;
13079
13080 case DT_PREINIT_ARRAYSZ:
13081 name = ".preinit_array";
4ade44b7 13082 goto get_out_size;
c152c796
AM
13083 case DT_INIT_ARRAYSZ:
13084 name = ".init_array";
4ade44b7 13085 goto get_out_size;
c152c796
AM
13086 case DT_FINI_ARRAYSZ:
13087 name = ".fini_array";
4ade44b7 13088 get_out_size:
c152c796
AM
13089 o = bfd_get_section_by_name (abfd, name);
13090 if (o == NULL)
13091 {
4eca0228 13092 _bfd_error_handler
4ade44b7 13093 (_("could not find section %s"), name);
c152c796
AM
13094 goto error_return;
13095 }
eea6121a 13096 if (o->size == 0)
4eca0228 13097 _bfd_error_handler
c152c796 13098 (_("warning: %s section has zero size"), name);
eea6121a 13099 dyn.d_un.d_val = o->size;
c152c796
AM
13100 break;
13101
13102 case DT_PREINIT_ARRAY:
13103 name = ".preinit_array";
4ade44b7 13104 goto get_out_vma;
c152c796
AM
13105 case DT_INIT_ARRAY:
13106 name = ".init_array";
4ade44b7 13107 goto get_out_vma;
c152c796
AM
13108 case DT_FINI_ARRAY:
13109 name = ".fini_array";
4ade44b7
AM
13110 get_out_vma:
13111 o = bfd_get_section_by_name (abfd, name);
13112 goto do_vma;
c152c796
AM
13113
13114 case DT_HASH:
13115 name = ".hash";
13116 goto get_vma;
fdc90cb4
JJ
13117 case DT_GNU_HASH:
13118 name = ".gnu.hash";
13119 goto get_vma;
c152c796
AM
13120 case DT_STRTAB:
13121 name = ".dynstr";
13122 goto get_vma;
13123 case DT_SYMTAB:
13124 name = ".dynsym";
13125 goto get_vma;
13126 case DT_VERDEF:
13127 name = ".gnu.version_d";
13128 goto get_vma;
13129 case DT_VERNEED:
13130 name = ".gnu.version_r";
13131 goto get_vma;
13132 case DT_VERSYM:
13133 name = ".gnu.version";
13134 get_vma:
4ade44b7
AM
13135 o = bfd_get_linker_section (dynobj, name);
13136 do_vma:
b3293efa 13137 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13138 {
4eca0228 13139 _bfd_error_handler
4ade44b7 13140 (_("could not find section %s"), name);
c152c796
AM
13141 goto error_return;
13142 }
894891db
NC
13143 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13144 {
4eca0228 13145 _bfd_error_handler
894891db
NC
13146 (_("warning: section '%s' is being made into a note"), name);
13147 bfd_set_error (bfd_error_nonrepresentable_section);
13148 goto error_return;
13149 }
4ade44b7 13150 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13151 break;
13152
13153 case DT_REL:
13154 case DT_RELA:
13155 case DT_RELSZ:
13156 case DT_RELASZ:
13157 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13158 type = SHT_REL;
13159 else
13160 type = SHT_RELA;
64487780
AM
13161 sh_size = 0;
13162 sh_addr = 0;
c152c796
AM
13163 for (i = 1; i < elf_numsections (abfd); i++)
13164 {
13165 Elf_Internal_Shdr *hdr;
13166
13167 hdr = elf_elfsections (abfd)[i];
13168 if (hdr->sh_type == type
13169 && (hdr->sh_flags & SHF_ALLOC) != 0)
13170 {
64487780
AM
13171 sh_size += hdr->sh_size;
13172 if (sh_addr == 0
13173 || sh_addr > hdr->sh_addr)
13174 sh_addr = hdr->sh_addr;
c152c796
AM
13175 }
13176 }
64487780 13177
64f52338
AM
13178 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13179 {
66631823
CE
13180 unsigned int opb = bfd_octets_per_byte (abfd, o);
13181
64f52338
AM
13182 /* Don't count procedure linkage table relocs in the
13183 overall reloc count. */
64487780
AM
13184 sh_size -= htab->srelplt->size;
13185 if (sh_size == 0)
13186 /* If the size is zero, make the address zero too.
13187 This is to avoid a glibc bug. If the backend
13188 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13189 zero, then we'll put DT_RELA at the end of
13190 DT_JMPREL. glibc will interpret the end of
13191 DT_RELA matching the end of DT_JMPREL as the
13192 case where DT_RELA includes DT_JMPREL, and for
13193 LD_BIND_NOW will decide that processing DT_RELA
13194 will process the PLT relocs too. Net result:
13195 No PLT relocs applied. */
13196 sh_addr = 0;
13197
64f52338
AM
13198 /* If .rela.plt is the first .rela section, exclude
13199 it from DT_RELA. */
64487780 13200 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13201 + htab->srelplt->output_offset) * opb)
64487780 13202 sh_addr += htab->srelplt->size;
64f52338 13203 }
64487780
AM
13204
13205 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13206 dyn.d_un.d_val = sh_size;
13207 else
13208 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13209 break;
13210 }
13211 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13212 }
13213 }
13214
13215 /* If we have created any dynamic sections, then output them. */
13216 if (dynobj != NULL)
13217 {
13218 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13219 goto error_return;
13220
943284cc 13221 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13222 if (bfd_link_textrel_check (info)
3d4d4302 13223 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13224 {
13225 bfd_byte *dyncon, *dynconend;
13226
943284cc
DJ
13227 dyncon = o->contents;
13228 dynconend = o->contents + o->size;
13229 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13230 {
13231 Elf_Internal_Dyn dyn;
13232
13233 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13234
13235 if (dyn.d_tag == DT_TEXTREL)
13236 {
a6dbf402 13237 if (info->textrel_check == textrel_check_error)
c192a133 13238 info->callbacks->einfo
9793eb77 13239 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13240 else if (bfd_link_dll (info))
13241 info->callbacks->einfo
13242 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13243 else
13244 info->callbacks->einfo
a6dbf402 13245 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13246 break;
13247 }
13248 }
13249 }
13250
c152c796
AM
13251 for (o = dynobj->sections; o != NULL; o = o->next)
13252 {
13253 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13254 || o->size == 0
c152c796
AM
13255 || o->output_section == bfd_abs_section_ptr)
13256 continue;
13257 if ((o->flags & SEC_LINKER_CREATED) == 0)
13258 {
13259 /* At this point, we are only interested in sections
13260 created by _bfd_elf_link_create_dynamic_sections. */
13261 continue;
13262 }
64f52338 13263 if (htab->stab_info.stabstr == o)
3722b82f 13264 continue;
64f52338 13265 if (htab->eh_info.hdr_sec == o)
eea6121a 13266 continue;
3d4d4302 13267 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13268 {
bb294208
AM
13269 bfd_size_type octets = ((file_ptr) o->output_offset
13270 * bfd_octets_per_byte (abfd, o));
13271 if (!bfd_set_section_contents (abfd, o->output_section,
13272 o->contents, octets, o->size))
c152c796
AM
13273 goto error_return;
13274 }
13275 else
13276 {
13277 /* The contents of the .dynstr section are actually in a
13278 stringtab. */
8539e4e8
AM
13279 file_ptr off;
13280
c152c796
AM
13281 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13282 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13283 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13284 goto error_return;
13285 }
13286 }
13287 }
13288
7bdf4127 13289 if (!info->resolve_section_groups)
c152c796
AM
13290 {
13291 bfd_boolean failed = FALSE;
13292
7bdf4127 13293 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13294 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13295 if (failed)
13296 goto error_return;
13297 }
13298
13299 /* If we have optimized stabs strings, output them. */
64f52338 13300 if (htab->stab_info.stabstr != NULL)
c152c796 13301 {
64f52338 13302 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13303 goto error_return;
13304 }
13305
9f7c3e5e
AM
13306 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13307 goto error_return;
c152c796 13308
1ff6de03
NA
13309 if (info->callbacks->emit_ctf)
13310 info->callbacks->emit_ctf ();
13311
9f7c3e5e 13312 elf_final_link_free (abfd, &flinfo);
c152c796 13313
104d59d1
JM
13314 if (attr_section)
13315 {
a50b1753 13316 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13317 if (contents == NULL)
496afd17
L
13318 {
13319 /* Bail out and fail. */
13320 ret = FALSE;
13321 goto return_local_hash_table;
13322 }
104d59d1
JM
13323 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13324 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13325 free (contents);
13326 }
13327
496afd17
L
13328 return_local_hash_table:
13329 if (info->unique_symbol)
13330 bfd_hash_table_free (&flinfo.local_hash_table);
13331 return ret;
c152c796
AM
13332
13333 error_return:
9f7c3e5e 13334 elf_final_link_free (abfd, &flinfo);
496afd17
L
13335 ret = FALSE;
13336 goto return_local_hash_table;
c152c796
AM
13337}
13338\f
5241d853
RS
13339/* Initialize COOKIE for input bfd ABFD. */
13340
13341static bfd_boolean
13342init_reloc_cookie (struct elf_reloc_cookie *cookie,
13343 struct bfd_link_info *info, bfd *abfd)
13344{
13345 Elf_Internal_Shdr *symtab_hdr;
13346 const struct elf_backend_data *bed;
13347
13348 bed = get_elf_backend_data (abfd);
13349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13350
13351 cookie->abfd = abfd;
13352 cookie->sym_hashes = elf_sym_hashes (abfd);
13353 cookie->bad_symtab = elf_bad_symtab (abfd);
13354 if (cookie->bad_symtab)
13355 {
13356 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13357 cookie->extsymoff = 0;
13358 }
13359 else
13360 {
13361 cookie->locsymcount = symtab_hdr->sh_info;
13362 cookie->extsymoff = symtab_hdr->sh_info;
13363 }
13364
13365 if (bed->s->arch_size == 32)
13366 cookie->r_sym_shift = 8;
13367 else
13368 cookie->r_sym_shift = 32;
13369
13370 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13371 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13372 {
13373 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13374 cookie->locsymcount, 0,
13375 NULL, NULL, NULL);
13376 if (cookie->locsyms == NULL)
13377 {
13378 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13379 return FALSE;
13380 }
13381 if (info->keep_memory)
13382 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13383 }
13384 return TRUE;
13385}
13386
13387/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13388
13389static void
13390fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13391{
13392 Elf_Internal_Shdr *symtab_hdr;
13393
13394 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13395 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13396 free (cookie->locsyms);
13397}
13398
13399/* Initialize the relocation information in COOKIE for input section SEC
13400 of input bfd ABFD. */
13401
13402static bfd_boolean
13403init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13404 struct bfd_link_info *info, bfd *abfd,
13405 asection *sec)
13406{
5241d853
RS
13407 if (sec->reloc_count == 0)
13408 {
13409 cookie->rels = NULL;
13410 cookie->relend = NULL;
13411 }
13412 else
13413 {
5241d853
RS
13414 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13415 info->keep_memory);
13416 if (cookie->rels == NULL)
13417 return FALSE;
13418 cookie->rel = cookie->rels;
056bafd4 13419 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13420 }
13421 cookie->rel = cookie->rels;
13422 return TRUE;
13423}
13424
13425/* Free the memory allocated by init_reloc_cookie_rels,
13426 if appropriate. */
13427
13428static void
13429fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13430 asection *sec)
13431{
c9594989 13432 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13433 free (cookie->rels);
13434}
13435
13436/* Initialize the whole of COOKIE for input section SEC. */
13437
13438static bfd_boolean
13439init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13440 struct bfd_link_info *info,
13441 asection *sec)
13442{
13443 if (!init_reloc_cookie (cookie, info, sec->owner))
13444 goto error1;
13445 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13446 goto error2;
13447 return TRUE;
13448
13449 error2:
13450 fini_reloc_cookie (cookie, sec->owner);
13451 error1:
13452 return FALSE;
13453}
13454
13455/* Free the memory allocated by init_reloc_cookie_for_section,
13456 if appropriate. */
13457
13458static void
13459fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13460 asection *sec)
13461{
13462 fini_reloc_cookie_rels (cookie, sec);
13463 fini_reloc_cookie (cookie, sec->owner);
13464}
13465\f
c152c796
AM
13466/* Garbage collect unused sections. */
13467
07adf181
AM
13468/* Default gc_mark_hook. */
13469
13470asection *
13471_bfd_elf_gc_mark_hook (asection *sec,
13472 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13473 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13474 struct elf_link_hash_entry *h,
13475 Elf_Internal_Sym *sym)
13476{
13477 if (h != NULL)
13478 {
13479 switch (h->root.type)
13480 {
13481 case bfd_link_hash_defined:
13482 case bfd_link_hash_defweak:
13483 return h->root.u.def.section;
13484
13485 case bfd_link_hash_common:
13486 return h->root.u.c.p->section;
13487
13488 default:
13489 break;
13490 }
13491 }
13492 else
13493 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13494
13495 return NULL;
13496}
13497
9e223787 13498/* Return the debug definition section. */
b7c871ed
L
13499
13500static asection *
13501elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13502 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13503 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13504 struct elf_link_hash_entry *h,
9e223787 13505 Elf_Internal_Sym *sym)
b7c871ed 13506{
9e223787
L
13507 if (h != NULL)
13508 {
13509 /* Return the global debug definition section. */
13510 if ((h->root.type == bfd_link_hash_defined
13511 || h->root.type == bfd_link_hash_defweak)
13512 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13513 return h->root.u.def.section;
13514 }
13515 else
13516 {
13517 /* Return the local debug definition section. */
13518 asection *isec = bfd_section_from_elf_index (sec->owner,
13519 sym->st_shndx);
13520 if ((isec->flags & SEC_DEBUGGING) != 0)
13521 return isec;
13522 }
b7c871ed
L
13523
13524 return NULL;
13525}
13526
5241d853
RS
13527/* COOKIE->rel describes a relocation against section SEC, which is
13528 a section we've decided to keep. Return the section that contains
13529 the relocation symbol, or NULL if no section contains it. */
13530
13531asection *
13532_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13533 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13534 struct elf_reloc_cookie *cookie,
13535 bfd_boolean *start_stop)
5241d853
RS
13536{
13537 unsigned long r_symndx;
3024a17a 13538 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13539
13540 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13541 if (r_symndx == STN_UNDEF)
5241d853
RS
13542 return NULL;
13543
13544 if (r_symndx >= cookie->locsymcount
13545 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13546 {
13547 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13548 if (h == NULL)
13549 {
871b3ab2 13550 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13551 sec->owner);
13552 return NULL;
13553 }
5241d853
RS
13554 while (h->root.type == bfd_link_hash_indirect
13555 || h->root.type == bfd_link_hash_warning)
13556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13557 h->mark = 1;
3024a17a
AM
13558 /* Keep all aliases of the symbol too. If an object symbol
13559 needs to be copied into .dynbss then all of its aliases
13560 should be present as dynamic symbols, not just the one used
13561 on the copy relocation. */
13562 hw = h;
13563 while (hw->is_weakalias)
13564 {
13565 hw = hw->u.alias;
13566 hw->mark = 1;
13567 }
1cce69b9 13568
a6a4679f 13569 if (start_stop != NULL)
1cce69b9 13570 {
7dba9362
AM
13571 /* To work around a glibc bug, mark XXX input sections
13572 when there is a reference to __start_XXX or __stop_XXX
13573 symbols. */
cbd0eecf 13574 if (h->start_stop)
1cce69b9 13575 {
cbd0eecf 13576 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13577 *start_stop = !s->gc_mark;
13578 return s;
1cce69b9
AM
13579 }
13580 }
13581
5241d853
RS
13582 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13583 }
13584
13585 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13586 &cookie->locsyms[r_symndx]);
13587}
13588
13589/* COOKIE->rel describes a relocation against section SEC, which is
13590 a section we've decided to keep. Mark the section that contains
9d0a14d3 13591 the relocation symbol. */
5241d853
RS
13592
13593bfd_boolean
13594_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13595 asection *sec,
13596 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13597 struct elf_reloc_cookie *cookie)
5241d853
RS
13598{
13599 asection *rsec;
1cce69b9 13600 bfd_boolean start_stop = FALSE;
5241d853 13601
1cce69b9
AM
13602 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13603 while (rsec != NULL)
5241d853 13604 {
1cce69b9
AM
13605 if (!rsec->gc_mark)
13606 {
13607 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13608 || (rsec->owner->flags & DYNAMIC) != 0)
13609 rsec->gc_mark = 1;
13610 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13611 return FALSE;
13612 }
13613 if (!start_stop)
13614 break;
199af150 13615 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13616 }
13617 return TRUE;
13618}
13619
07adf181
AM
13620/* The mark phase of garbage collection. For a given section, mark
13621 it and any sections in this section's group, and all the sections
13622 which define symbols to which it refers. */
13623
ccfa59ea
AM
13624bfd_boolean
13625_bfd_elf_gc_mark (struct bfd_link_info *info,
13626 asection *sec,
6a5bb875 13627 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13628{
13629 bfd_boolean ret;
9d0a14d3 13630 asection *group_sec, *eh_frame;
c152c796
AM
13631
13632 sec->gc_mark = 1;
13633
13634 /* Mark all the sections in the group. */
13635 group_sec = elf_section_data (sec)->next_in_group;
13636 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13637 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13638 return FALSE;
13639
13640 /* Look through the section relocs. */
13641 ret = TRUE;
9d0a14d3
RS
13642 eh_frame = elf_eh_frame_section (sec->owner);
13643 if ((sec->flags & SEC_RELOC) != 0
13644 && sec->reloc_count > 0
13645 && sec != eh_frame)
c152c796 13646 {
5241d853 13647 struct elf_reloc_cookie cookie;
c152c796 13648
5241d853
RS
13649 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13650 ret = FALSE;
c152c796 13651 else
c152c796 13652 {
5241d853 13653 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13654 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13655 {
13656 ret = FALSE;
13657 break;
13658 }
13659 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13660 }
13661 }
9d0a14d3
RS
13662
13663 if (ret && eh_frame && elf_fde_list (sec))
13664 {
13665 struct elf_reloc_cookie cookie;
13666
13667 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13668 ret = FALSE;
13669 else
13670 {
13671 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13672 gc_mark_hook, &cookie))
13673 ret = FALSE;
13674 fini_reloc_cookie_for_section (&cookie, eh_frame);
13675 }
13676 }
13677
2f0c68f2
CM
13678 eh_frame = elf_section_eh_frame_entry (sec);
13679 if (ret && eh_frame && !eh_frame->gc_mark)
13680 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13681 ret = FALSE;
13682
c152c796
AM
13683 return ret;
13684}
13685
3c758495
TG
13686/* Scan and mark sections in a special or debug section group. */
13687
13688static void
13689_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13690{
13691 /* Point to first section of section group. */
13692 asection *ssec;
13693 /* Used to iterate the section group. */
13694 asection *msec;
13695
13696 bfd_boolean is_special_grp = TRUE;
13697 bfd_boolean is_debug_grp = TRUE;
13698
13699 /* First scan to see if group contains any section other than debug
13700 and special section. */
13701 ssec = msec = elf_next_in_group (grp);
13702 do
13703 {
13704 if ((msec->flags & SEC_DEBUGGING) == 0)
13705 is_debug_grp = FALSE;
13706
13707 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13708 is_special_grp = FALSE;
13709
13710 msec = elf_next_in_group (msec);
13711 }
13712 while (msec != ssec);
13713
13714 /* If this is a pure debug section group or pure special section group,
13715 keep all sections in this group. */
13716 if (is_debug_grp || is_special_grp)
13717 {
13718 do
13719 {
13720 msec->gc_mark = 1;
13721 msec = elf_next_in_group (msec);
13722 }
13723 while (msec != ssec);
13724 }
13725}
13726
7f6ab9f8
AM
13727/* Keep debug and special sections. */
13728
13729bfd_boolean
13730_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13731 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13732{
13733 bfd *ibfd;
13734
c72f2fb2 13735 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13736 {
13737 asection *isec;
13738 bfd_boolean some_kept;
b40bf0a2 13739 bfd_boolean debug_frag_seen;
b7c871ed 13740 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13741
13742 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13743 continue;
57963c05
AM
13744 isec = ibfd->sections;
13745 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13746 continue;
7f6ab9f8 13747
b40bf0a2
NC
13748 /* Ensure all linker created sections are kept,
13749 see if any other section is already marked,
13750 and note if we have any fragmented debug sections. */
b7c871ed 13751 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13752 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13753 {
13754 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13755 isec->gc_mark = 1;
eb026f09
AM
13756 else if (isec->gc_mark
13757 && (isec->flags & SEC_ALLOC) != 0
13758 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13759 some_kept = TRUE;
b7d07216
L
13760 else
13761 {
13762 /* Since all sections, except for backend specific ones,
13763 have been garbage collected, call mark_hook on this
13764 section if any of its linked-to sections is marked. */
13765 asection *linked_to_sec = elf_linked_to_section (isec);
13766 for (; linked_to_sec != NULL;
13767 linked_to_sec = elf_linked_to_section (linked_to_sec))
13768 if (linked_to_sec->gc_mark)
13769 {
13770 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13771 return FALSE;
13772 break;
13773 }
13774 }
b40bf0a2 13775
535b785f 13776 if (!debug_frag_seen
b40bf0a2
NC
13777 && (isec->flags & SEC_DEBUGGING)
13778 && CONST_STRNEQ (isec->name, ".debug_line."))
13779 debug_frag_seen = TRUE;
5242a0a0
L
13780 else if (strcmp (bfd_section_name (isec),
13781 "__patchable_function_entries") == 0
13782 && elf_linked_to_section (isec) == NULL)
13783 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13784 "need linked-to section "
13785 "for --gc-sections\n"),
13786 isec->owner, isec);
7f6ab9f8
AM
13787 }
13788
eb026f09
AM
13789 /* If no non-note alloc section in this file will be kept, then
13790 we can toss out the debug and special sections. */
7f6ab9f8
AM
13791 if (!some_kept)
13792 continue;
13793
13794 /* Keep debug and special sections like .comment when they are
3c758495 13795 not part of a group. Also keep section groups that contain
b7d07216
L
13796 just debug sections or special sections. NB: Sections with
13797 linked-to section has been handled above. */
7f6ab9f8 13798 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13799 {
13800 if ((isec->flags & SEC_GROUP) != 0)
13801 _bfd_elf_gc_mark_debug_special_section_group (isec);
13802 else if (((isec->flags & SEC_DEBUGGING) != 0
13803 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13804 && elf_next_in_group (isec) == NULL
13805 && elf_linked_to_section (isec) == NULL)
3c758495 13806 isec->gc_mark = 1;
b7c871ed
L
13807 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13808 has_kept_debug_info = TRUE;
3c758495 13809 }
b40bf0a2 13810
b40bf0a2
NC
13811 /* Look for CODE sections which are going to be discarded,
13812 and find and discard any fragmented debug sections which
13813 are associated with that code section. */
b7c871ed
L
13814 if (debug_frag_seen)
13815 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13816 if ((isec->flags & SEC_CODE) != 0
13817 && isec->gc_mark == 0)
13818 {
13819 unsigned int ilen;
13820 asection *dsec;
b40bf0a2 13821
b7c871ed 13822 ilen = strlen (isec->name);
b40bf0a2 13823
b7c871ed 13824 /* Association is determined by the name of the debug
07d6d2b8 13825 section containing the name of the code section as
b7c871ed
L
13826 a suffix. For example .debug_line.text.foo is a
13827 debug section associated with .text.foo. */
13828 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13829 {
13830 unsigned int dlen;
b40bf0a2 13831
b7c871ed
L
13832 if (dsec->gc_mark == 0
13833 || (dsec->flags & SEC_DEBUGGING) == 0)
13834 continue;
b40bf0a2 13835
b7c871ed 13836 dlen = strlen (dsec->name);
b40bf0a2 13837
b7c871ed
L
13838 if (dlen > ilen
13839 && strncmp (dsec->name + (dlen - ilen),
13840 isec->name, ilen) == 0)
b40bf0a2 13841 dsec->gc_mark = 0;
b7c871ed 13842 }
b40bf0a2 13843 }
b7c871ed
L
13844
13845 /* Mark debug sections referenced by kept debug sections. */
13846 if (has_kept_debug_info)
13847 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13848 if (isec->gc_mark
13849 && (isec->flags & SEC_DEBUGGING) != 0)
13850 if (!_bfd_elf_gc_mark (info, isec,
13851 elf_gc_mark_debug_section))
13852 return FALSE;
7f6ab9f8
AM
13853 }
13854 return TRUE;
13855}
13856
c152c796 13857static bfd_boolean
ccabcbe5 13858elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13859{
13860 bfd *sub;
ccabcbe5 13861 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13862
c72f2fb2 13863 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13864 {
13865 asection *o;
13866
b19a8f85 13867 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13868 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13869 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13870 continue;
57963c05
AM
13871 o = sub->sections;
13872 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13873 continue;
c152c796
AM
13874
13875 for (o = sub->sections; o != NULL; o = o->next)
13876 {
a33dafc3
L
13877 /* When any section in a section group is kept, we keep all
13878 sections in the section group. If the first member of
13879 the section group is excluded, we will also exclude the
13880 group section. */
13881 if (o->flags & SEC_GROUP)
13882 {
13883 asection *first = elf_next_in_group (o);
13884 o->gc_mark = first->gc_mark;
13885 }
c152c796 13886
1e7eae0d 13887 if (o->gc_mark)
c152c796
AM
13888 continue;
13889
13890 /* Skip sweeping sections already excluded. */
13891 if (o->flags & SEC_EXCLUDE)
13892 continue;
13893
13894 /* Since this is early in the link process, it is simple
13895 to remove a section from the output. */
13896 o->flags |= SEC_EXCLUDE;
13897
c55fe096 13898 if (info->print_gc_sections && o->size != 0)
695344c0 13899 /* xgettext:c-format */
9793eb77 13900 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13901 o, sub);
c152c796
AM
13902 }
13903 }
13904
c152c796
AM
13905 return TRUE;
13906}
13907
13908/* Propagate collected vtable information. This is called through
13909 elf_link_hash_traverse. */
13910
13911static bfd_boolean
13912elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13913{
c152c796 13914 /* Those that are not vtables. */
cbd0eecf
L
13915 if (h->start_stop
13916 || h->u2.vtable == NULL
13917 || h->u2.vtable->parent == NULL)
c152c796
AM
13918 return TRUE;
13919
13920 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13921 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13922 return TRUE;
13923
13924 /* If we've already been done, exit. */
cbd0eecf 13925 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13926 return TRUE;
13927
13928 /* Make sure the parent's table is up to date. */
cbd0eecf 13929 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13930
cbd0eecf 13931 if (h->u2.vtable->used == NULL)
c152c796
AM
13932 {
13933 /* None of this table's entries were referenced. Re-use the
13934 parent's table. */
cbd0eecf
L
13935 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13936 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13937 }
13938 else
13939 {
13940 size_t n;
13941 bfd_boolean *cu, *pu;
13942
13943 /* Or the parent's entries into ours. */
cbd0eecf 13944 cu = h->u2.vtable->used;
c152c796 13945 cu[-1] = TRUE;
cbd0eecf 13946 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13947 if (pu != NULL)
13948 {
13949 const struct elf_backend_data *bed;
13950 unsigned int log_file_align;
13951
13952 bed = get_elf_backend_data (h->root.u.def.section->owner);
13953 log_file_align = bed->s->log_file_align;
cbd0eecf 13954 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13955 while (n--)
13956 {
13957 if (*pu)
13958 *cu = TRUE;
13959 pu++;
13960 cu++;
13961 }
13962 }
13963 }
13964
13965 return TRUE;
13966}
13967
13968static bfd_boolean
13969elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13970{
13971 asection *sec;
13972 bfd_vma hstart, hend;
13973 Elf_Internal_Rela *relstart, *relend, *rel;
13974 const struct elf_backend_data *bed;
13975 unsigned int log_file_align;
13976
c152c796
AM
13977 /* Take care of both those symbols that do not describe vtables as
13978 well as those that are not loaded. */
cbd0eecf
L
13979 if (h->start_stop
13980 || h->u2.vtable == NULL
13981 || h->u2.vtable->parent == NULL)
c152c796
AM
13982 return TRUE;
13983
13984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13985 || h->root.type == bfd_link_hash_defweak);
13986
13987 sec = h->root.u.def.section;
13988 hstart = h->root.u.def.value;
13989 hend = hstart + h->size;
13990
13991 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13992 if (!relstart)
13993 return *(bfd_boolean *) okp = FALSE;
13994 bed = get_elf_backend_data (sec->owner);
13995 log_file_align = bed->s->log_file_align;
13996
056bafd4 13997 relend = relstart + sec->reloc_count;
c152c796
AM
13998
13999 for (rel = relstart; rel < relend; ++rel)
14000 if (rel->r_offset >= hstart && rel->r_offset < hend)
14001 {
14002 /* If the entry is in use, do nothing. */
cbd0eecf
L
14003 if (h->u2.vtable->used
14004 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
14005 {
14006 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 14007 if (h->u2.vtable->used[entry])
c152c796
AM
14008 continue;
14009 }
14010 /* Otherwise, kill it. */
14011 rel->r_offset = rel->r_info = rel->r_addend = 0;
14012 }
14013
14014 return TRUE;
14015}
14016
87538722
AM
14017/* Mark sections containing dynamically referenced symbols. When
14018 building shared libraries, we must assume that any visible symbol is
14019 referenced. */
715df9b8 14020
64d03ab5
AM
14021bfd_boolean
14022bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 14023{
87538722 14024 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 14025 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 14026
715df9b8
EB
14027 if ((h->root.type == bfd_link_hash_defined
14028 || h->root.type == bfd_link_hash_defweak)
d664fd41 14029 && ((h->ref_dynamic && !h->forced_local)
c4621b33 14030 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 14031 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 14032 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 14033 && (!bfd_link_executable (info)
22185505 14034 || info->gc_keep_exported
b407645f
AM
14035 || info->export_dynamic
14036 || (h->dynamic
14037 && d != NULL
14038 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14039 && (h->versioned >= versioned
54e8959c
L
14040 || !bfd_hide_sym_by_version (info->version_info,
14041 h->root.root.string)))))
715df9b8
EB
14042 h->root.u.def.section->flags |= SEC_KEEP;
14043
14044 return TRUE;
14045}
3b36f7e6 14046
74f0fb50
AM
14047/* Keep all sections containing symbols undefined on the command-line,
14048 and the section containing the entry symbol. */
14049
14050void
14051_bfd_elf_gc_keep (struct bfd_link_info *info)
14052{
14053 struct bfd_sym_chain *sym;
14054
14055 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14056 {
14057 struct elf_link_hash_entry *h;
14058
14059 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14060 FALSE, FALSE, FALSE);
14061
14062 if (h != NULL
14063 && (h->root.type == bfd_link_hash_defined
14064 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14065 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14066 h->root.u.def.section->flags |= SEC_KEEP;
14067 }
14068}
14069
2f0c68f2
CM
14070bfd_boolean
14071bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14072 struct bfd_link_info *info)
14073{
14074 bfd *ibfd = info->input_bfds;
14075
14076 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14077 {
14078 asection *sec;
14079 struct elf_reloc_cookie cookie;
14080
14081 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14082 continue;
57963c05
AM
14083 sec = ibfd->sections;
14084 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14085 continue;
2f0c68f2
CM
14086
14087 if (!init_reloc_cookie (&cookie, info, ibfd))
14088 return FALSE;
14089
14090 for (sec = ibfd->sections; sec; sec = sec->next)
14091 {
fd361982 14092 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14093 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14094 {
14095 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14096 fini_reloc_cookie_rels (&cookie, sec);
14097 }
14098 }
14099 }
14100 return TRUE;
14101}
14102
c152c796
AM
14103/* Do mark and sweep of unused sections. */
14104
14105bfd_boolean
14106bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14107{
14108 bfd_boolean ok = TRUE;
14109 bfd *sub;
6a5bb875 14110 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14111 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14112 struct elf_link_hash_table *htab;
c152c796 14113
64d03ab5 14114 if (!bed->can_gc_sections
715df9b8 14115 || !is_elf_hash_table (info->hash))
c152c796 14116 {
9793eb77 14117 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
14118 return TRUE;
14119 }
14120
74f0fb50 14121 bed->gc_keep (info);
da44f4e5 14122 htab = elf_hash_table (info);
74f0fb50 14123
9d0a14d3
RS
14124 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14125 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14126 for (sub = info->input_bfds;
14127 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14128 sub = sub->link.next)
9d0a14d3
RS
14129 {
14130 asection *sec;
14131 struct elf_reloc_cookie cookie;
14132
57963c05
AM
14133 sec = sub->sections;
14134 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14135 continue;
9d0a14d3 14136 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14137 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14138 {
14139 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14140 if (elf_section_data (sec)->sec_info
14141 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14142 elf_eh_frame_section (sub) = sec;
14143 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14144 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14145 }
14146 }
9d0a14d3 14147
c152c796 14148 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14149 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14150 if (!ok)
14151 return FALSE;
14152
14153 /* Kill the vtable relocations that were not used. */
da44f4e5 14154 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14155 if (!ok)
14156 return FALSE;
14157
715df9b8 14158 /* Mark dynamically referenced symbols. */
22185505 14159 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14160 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14161
715df9b8 14162 /* Grovel through relocs to find out who stays ... */
64d03ab5 14163 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14164 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14165 {
14166 asection *o;
14167
b19a8f85 14168 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14169 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14170 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14171 continue;
14172
57963c05
AM
14173 o = sub->sections;
14174 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14175 continue;
14176
7f6ab9f8
AM
14177 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14178 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14179 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14180 well as FINI_ARRAY sections for ld -r. */
c152c796 14181 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14182 if (!o->gc_mark
14183 && (o->flags & SEC_EXCLUDE) == 0
24007750 14184 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14185 || (bfd_link_relocatable (info)
14186 && ((elf_section_data (o)->this_hdr.sh_type
14187 == SHT_PREINIT_ARRAY)
14188 || (elf_section_data (o)->this_hdr.sh_type
14189 == SHT_INIT_ARRAY)
14190 || (elf_section_data (o)->this_hdr.sh_type
14191 == SHT_FINI_ARRAY)))
7f6ab9f8 14192 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14193 && elf_next_in_group (o) == NULL
99fabbc9
JL
14194 && elf_linked_to_section (o) == NULL)
14195 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14196 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14197 {
14198 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14199 return FALSE;
14200 }
c152c796
AM
14201 }
14202
6a5bb875 14203 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14204 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14205
c152c796 14206 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14207 return elf_gc_sweep (abfd, info);
c152c796
AM
14208}
14209\f
14210/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14211
14212bfd_boolean
14213bfd_elf_gc_record_vtinherit (bfd *abfd,
14214 asection *sec,
14215 struct elf_link_hash_entry *h,
14216 bfd_vma offset)
14217{
14218 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14219 struct elf_link_hash_entry **search, *child;
ef53be89 14220 size_t extsymcount;
c152c796
AM
14221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14222
14223 /* The sh_info field of the symtab header tells us where the
14224 external symbols start. We don't care about the local symbols at
14225 this point. */
14226 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14227 if (!elf_bad_symtab (abfd))
14228 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14229
14230 sym_hashes = elf_sym_hashes (abfd);
14231 sym_hashes_end = sym_hashes + extsymcount;
14232
14233 /* Hunt down the child symbol, which is in this section at the same
14234 offset as the relocation. */
14235 for (search = sym_hashes; search != sym_hashes_end; ++search)
14236 {
14237 if ((child = *search) != NULL
14238 && (child->root.type == bfd_link_hash_defined
14239 || child->root.type == bfd_link_hash_defweak)
14240 && child->root.u.def.section == sec
14241 && child->root.u.def.value == offset)
14242 goto win;
14243 }
14244
695344c0 14245 /* xgettext:c-format */
9793eb77 14246 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14247 abfd, sec, (uint64_t) offset);
c152c796
AM
14248 bfd_set_error (bfd_error_invalid_operation);
14249 return FALSE;
14250
14251 win:
cbd0eecf 14252 if (!child->u2.vtable)
f6e332e6 14253 {
cbd0eecf
L
14254 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14255 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14256 if (!child->u2.vtable)
f6e332e6
AM
14257 return FALSE;
14258 }
c152c796
AM
14259 if (!h)
14260 {
14261 /* This *should* only be the absolute section. It could potentially
14262 be that someone has defined a non-global vtable though, which
14263 would be bad. It isn't worth paging in the local symbols to be
14264 sure though; that case should simply be handled by the assembler. */
14265
cbd0eecf 14266 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14267 }
14268 else
cbd0eecf 14269 child->u2.vtable->parent = h;
c152c796
AM
14270
14271 return TRUE;
14272}
14273
14274/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14275
14276bfd_boolean
a0ea3a14 14277bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14278 struct elf_link_hash_entry *h,
14279 bfd_vma addend)
14280{
14281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14282 unsigned int log_file_align = bed->s->log_file_align;
14283
a0ea3a14
L
14284 if (!h)
14285 {
14286 /* xgettext:c-format */
14287 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14288 abfd, sec);
14289 bfd_set_error (bfd_error_bad_value);
14290 return FALSE;
14291 }
14292
cbd0eecf 14293 if (!h->u2.vtable)
f6e332e6 14294 {
cbd0eecf
L
14295 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14296 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14297 if (!h->u2.vtable)
f6e332e6
AM
14298 return FALSE;
14299 }
14300
cbd0eecf 14301 if (addend >= h->u2.vtable->size)
c152c796
AM
14302 {
14303 size_t size, bytes, file_align;
cbd0eecf 14304 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14305
14306 /* While the symbol is undefined, we have to be prepared to handle
14307 a zero size. */
14308 file_align = 1 << log_file_align;
14309 if (h->root.type == bfd_link_hash_undefined)
14310 size = addend + file_align;
14311 else
14312 {
14313 size = h->size;
14314 if (addend >= size)
14315 {
14316 /* Oops! We've got a reference past the defined end of
14317 the table. This is probably a bug -- shall we warn? */
14318 size = addend + file_align;
14319 }
14320 }
14321 size = (size + file_align - 1) & -file_align;
14322
14323 /* Allocate one extra entry for use as a "done" flag for the
14324 consolidation pass. */
14325 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14326
14327 if (ptr)
14328 {
a50b1753 14329 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14330
14331 if (ptr != NULL)
14332 {
14333 size_t oldbytes;
14334
cbd0eecf 14335 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14336 * sizeof (bfd_boolean));
14337 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14338 }
14339 }
14340 else
a50b1753 14341 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14342
14343 if (ptr == NULL)
14344 return FALSE;
14345
14346 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14347 h->u2.vtable->used = ptr + 1;
14348 h->u2.vtable->size = size;
c152c796
AM
14349 }
14350
cbd0eecf 14351 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14352
14353 return TRUE;
14354}
14355
ae17ab41
CM
14356/* Map an ELF section header flag to its corresponding string. */
14357typedef struct
14358{
14359 char *flag_name;
14360 flagword flag_value;
14361} elf_flags_to_name_table;
14362
3f75e1d6 14363static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14364{
14365 { "SHF_WRITE", SHF_WRITE },
14366 { "SHF_ALLOC", SHF_ALLOC },
14367 { "SHF_EXECINSTR", SHF_EXECINSTR },
14368 { "SHF_MERGE", SHF_MERGE },
14369 { "SHF_STRINGS", SHF_STRINGS },
14370 { "SHF_INFO_LINK", SHF_INFO_LINK},
14371 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14372 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14373 { "SHF_GROUP", SHF_GROUP },
14374 { "SHF_TLS", SHF_TLS },
14375 { "SHF_MASKOS", SHF_MASKOS },
14376 { "SHF_EXCLUDE", SHF_EXCLUDE },
14377};
14378
b9c361e0
JL
14379/* Returns TRUE if the section is to be included, otherwise FALSE. */
14380bfd_boolean
ae17ab41 14381bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14382 struct flag_info *flaginfo,
b9c361e0 14383 asection *section)
ae17ab41 14384{
8b127cbc 14385 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14386
8b127cbc 14387 if (!flaginfo->flags_initialized)
ae17ab41 14388 {
8b127cbc
AM
14389 bfd *obfd = info->output_bfd;
14390 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14391 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14392 int with_hex = 0;
14393 int without_hex = 0;
14394
8b127cbc 14395 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14396 {
b9c361e0 14397 unsigned i;
8b127cbc 14398 flagword (*lookup) (char *);
ae17ab41 14399
8b127cbc
AM
14400 lookup = bed->elf_backend_lookup_section_flags_hook;
14401 if (lookup != NULL)
ae17ab41 14402 {
8b127cbc 14403 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14404
14405 if (hexval != 0)
14406 {
14407 if (tf->with == with_flags)
14408 with_hex |= hexval;
14409 else if (tf->with == without_flags)
14410 without_hex |= hexval;
14411 tf->valid = TRUE;
14412 continue;
14413 }
ae17ab41 14414 }
8b127cbc 14415 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14416 {
8b127cbc 14417 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14418 {
14419 if (tf->with == with_flags)
14420 with_hex |= elf_flags_to_names[i].flag_value;
14421 else if (tf->with == without_flags)
14422 without_hex |= elf_flags_to_names[i].flag_value;
14423 tf->valid = TRUE;
14424 break;
14425 }
14426 }
8b127cbc 14427 if (!tf->valid)
b9c361e0 14428 {
68ffbac6 14429 info->callbacks->einfo
9793eb77 14430 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14431 return FALSE;
ae17ab41
CM
14432 }
14433 }
8b127cbc
AM
14434 flaginfo->flags_initialized = TRUE;
14435 flaginfo->only_with_flags |= with_hex;
14436 flaginfo->not_with_flags |= without_hex;
ae17ab41 14437 }
ae17ab41 14438
8b127cbc 14439 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14440 return FALSE;
14441
8b127cbc 14442 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14443 return FALSE;
14444
14445 return TRUE;
ae17ab41
CM
14446}
14447
c152c796
AM
14448struct alloc_got_off_arg {
14449 bfd_vma gotoff;
10455f89 14450 struct bfd_link_info *info;
c152c796
AM
14451};
14452
14453/* We need a special top-level link routine to convert got reference counts
14454 to real got offsets. */
14455
14456static bfd_boolean
14457elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14458{
a50b1753 14459 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14460 bfd *obfd = gofarg->info->output_bfd;
14461 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14462
c152c796
AM
14463 if (h->got.refcount > 0)
14464 {
14465 h->got.offset = gofarg->gotoff;
10455f89 14466 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14467 }
14468 else
14469 h->got.offset = (bfd_vma) -1;
14470
14471 return TRUE;
14472}
14473
14474/* And an accompanying bit to work out final got entry offsets once
14475 we're done. Should be called from final_link. */
14476
14477bfd_boolean
14478bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14479 struct bfd_link_info *info)
14480{
14481 bfd *i;
14482 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14483 bfd_vma gotoff;
c152c796
AM
14484 struct alloc_got_off_arg gofarg;
14485
10455f89
HPN
14486 BFD_ASSERT (abfd == info->output_bfd);
14487
c152c796
AM
14488 if (! is_elf_hash_table (info->hash))
14489 return FALSE;
14490
14491 /* The GOT offset is relative to the .got section, but the GOT header is
14492 put into the .got.plt section, if the backend uses it. */
14493 if (bed->want_got_plt)
14494 gotoff = 0;
14495 else
14496 gotoff = bed->got_header_size;
14497
14498 /* Do the local .got entries first. */
c72f2fb2 14499 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14500 {
14501 bfd_signed_vma *local_got;
ef53be89 14502 size_t j, locsymcount;
c152c796
AM
14503 Elf_Internal_Shdr *symtab_hdr;
14504
14505 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14506 continue;
14507
14508 local_got = elf_local_got_refcounts (i);
14509 if (!local_got)
14510 continue;
14511
14512 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14513 if (elf_bad_symtab (i))
14514 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14515 else
14516 locsymcount = symtab_hdr->sh_info;
14517
14518 for (j = 0; j < locsymcount; ++j)
14519 {
14520 if (local_got[j] > 0)
14521 {
14522 local_got[j] = gotoff;
10455f89 14523 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14524 }
14525 else
14526 local_got[j] = (bfd_vma) -1;
14527 }
14528 }
14529
14530 /* Then the global .got entries. .plt refcounts are handled by
14531 adjust_dynamic_symbol */
14532 gofarg.gotoff = gotoff;
10455f89 14533 gofarg.info = info;
c152c796
AM
14534 elf_link_hash_traverse (elf_hash_table (info),
14535 elf_gc_allocate_got_offsets,
14536 &gofarg);
14537 return TRUE;
14538}
14539
14540/* Many folk need no more in the way of final link than this, once
14541 got entry reference counting is enabled. */
14542
14543bfd_boolean
14544bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14545{
14546 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14547 return FALSE;
14548
14549 /* Invoke the regular ELF backend linker to do all the work. */
14550 return bfd_elf_final_link (abfd, info);
14551}
14552
14553bfd_boolean
14554bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14555{
a50b1753 14556 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14557
14558 if (rcookie->bad_symtab)
14559 rcookie->rel = rcookie->rels;
14560
14561 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14562 {
14563 unsigned long r_symndx;
14564
14565 if (! rcookie->bad_symtab)
14566 if (rcookie->rel->r_offset > offset)
14567 return FALSE;
14568 if (rcookie->rel->r_offset != offset)
14569 continue;
14570
14571 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14572 if (r_symndx == STN_UNDEF)
c152c796
AM
14573 return TRUE;
14574
14575 if (r_symndx >= rcookie->locsymcount
14576 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14577 {
14578 struct elf_link_hash_entry *h;
14579
14580 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14581
14582 while (h->root.type == bfd_link_hash_indirect
14583 || h->root.type == bfd_link_hash_warning)
14584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14585
14586 if ((h->root.type == bfd_link_hash_defined
14587 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14588 && (h->root.u.def.section->owner != rcookie->abfd
14589 || h->root.u.def.section->kept_section != NULL
14590 || discarded_section (h->root.u.def.section)))
c152c796 14591 return TRUE;
c152c796
AM
14592 }
14593 else
14594 {
14595 /* It's not a relocation against a global symbol,
14596 but it could be a relocation against a local
14597 symbol for a discarded section. */
14598 asection *isec;
14599 Elf_Internal_Sym *isym;
14600
14601 /* Need to: get the symbol; get the section. */
14602 isym = &rcookie->locsyms[r_symndx];
cb33740c 14603 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14604 if (isec != NULL
14605 && (isec->kept_section != NULL
14606 || discarded_section (isec)))
cb33740c 14607 return TRUE;
c152c796
AM
14608 }
14609 return FALSE;
14610 }
14611 return FALSE;
14612}
14613
14614/* Discard unneeded references to discarded sections.
75938853
AM
14615 Returns -1 on error, 1 if any section's size was changed, 0 if
14616 nothing changed. This function assumes that the relocations are in
14617 sorted order, which is true for all known assemblers. */
c152c796 14618
75938853 14619int
c152c796
AM
14620bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14621{
14622 struct elf_reloc_cookie cookie;
18cd5bce 14623 asection *o;
c152c796 14624 bfd *abfd;
75938853 14625 int changed = 0;
c152c796
AM
14626
14627 if (info->traditional_format
14628 || !is_elf_hash_table (info->hash))
75938853 14629 return 0;
c152c796 14630
18cd5bce
AM
14631 o = bfd_get_section_by_name (output_bfd, ".stab");
14632 if (o != NULL)
c152c796 14633 {
18cd5bce 14634 asection *i;
c152c796 14635
18cd5bce 14636 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14637 {
18cd5bce
AM
14638 if (i->size == 0
14639 || i->reloc_count == 0
14640 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14641 continue;
c152c796 14642
18cd5bce
AM
14643 abfd = i->owner;
14644 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14645 continue;
c152c796 14646
18cd5bce 14647 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14648 return -1;
c152c796 14649
18cd5bce
AM
14650 if (_bfd_discard_section_stabs (abfd, i,
14651 elf_section_data (i)->sec_info,
5241d853
RS
14652 bfd_elf_reloc_symbol_deleted_p,
14653 &cookie))
75938853 14654 changed = 1;
18cd5bce
AM
14655
14656 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14657 }
18cd5bce
AM
14658 }
14659
2f0c68f2
CM
14660 o = NULL;
14661 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14662 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14663 if (o != NULL)
14664 {
14665 asection *i;
d7153c4a 14666 int eh_changed = 0;
66631823 14667 unsigned int eh_alignment; /* Octets. */
c152c796 14668
18cd5bce 14669 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14670 {
18cd5bce
AM
14671 if (i->size == 0)
14672 continue;
14673
14674 abfd = i->owner;
14675 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14676 continue;
14677
14678 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14679 return -1;
18cd5bce
AM
14680
14681 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14682 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14683 bfd_elf_reloc_symbol_deleted_p,
14684 &cookie))
d7153c4a
AM
14685 {
14686 eh_changed = 1;
14687 if (i->size != i->rawsize)
14688 changed = 1;
14689 }
18cd5bce
AM
14690
14691 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14692 }
9866ffe2 14693
66631823
CE
14694 eh_alignment = ((1 << o->alignment_power)
14695 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14696 /* Skip over zero terminator, and prevent empty sections from
14697 adding alignment padding at the end. */
14698 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14699 if (i->size == 0)
14700 i->flags |= SEC_EXCLUDE;
14701 else if (i->size > 4)
14702 break;
14703 /* The last non-empty eh_frame section doesn't need padding. */
14704 if (i != NULL)
14705 i = i->map_tail.s;
14706 /* Any prior sections must pad the last FDE out to the output
14707 section alignment. Otherwise we might have zero padding
14708 between sections, which would be seen as a terminator. */
14709 for (; i != NULL; i = i->map_tail.s)
14710 if (i->size == 4)
14711 /* All but the last zero terminator should have been removed. */
14712 BFD_FAIL ();
14713 else
14714 {
14715 bfd_size_type size
14716 = (i->size + eh_alignment - 1) & -eh_alignment;
14717 if (i->size != size)
af471f82 14718 {
9866ffe2
AM
14719 i->size = size;
14720 changed = 1;
14721 eh_changed = 1;
af471f82 14722 }
9866ffe2 14723 }
d7153c4a
AM
14724 if (eh_changed)
14725 elf_link_hash_traverse (elf_hash_table (info),
14726 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14727 }
c152c796 14728
18cd5bce
AM
14729 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14730 {
14731 const struct elf_backend_data *bed;
57963c05 14732 asection *s;
c152c796 14733
18cd5bce
AM
14734 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14735 continue;
57963c05
AM
14736 s = abfd->sections;
14737 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14738 continue;
18cd5bce
AM
14739
14740 bed = get_elf_backend_data (abfd);
14741
14742 if (bed->elf_backend_discard_info != NULL)
14743 {
14744 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14745 return -1;
18cd5bce
AM
14746
14747 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14748 changed = 1;
18cd5bce
AM
14749
14750 fini_reloc_cookie (&cookie, abfd);
14751 }
c152c796
AM
14752 }
14753
2f0c68f2
CM
14754 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14755 _bfd_elf_end_eh_frame_parsing (info);
14756
14757 if (info->eh_frame_hdr_type
0e1862bb 14758 && !bfd_link_relocatable (info)
c152c796 14759 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14760 changed = 1;
c152c796 14761
75938853 14762 return changed;
c152c796 14763}
082b7297 14764
43e1669b 14765bfd_boolean
0c511000 14766_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14767 asection *sec,
c0f00686 14768 struct bfd_link_info *info)
082b7297
L
14769{
14770 flagword flags;
c77ec726 14771 const char *name, *key;
082b7297
L
14772 struct bfd_section_already_linked *l;
14773 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14774
c77ec726
AM
14775 if (sec->output_section == bfd_abs_section_ptr)
14776 return FALSE;
0c511000 14777
c77ec726 14778 flags = sec->flags;
0c511000 14779
c77ec726
AM
14780 /* Return if it isn't a linkonce section. A comdat group section
14781 also has SEC_LINK_ONCE set. */
14782 if ((flags & SEC_LINK_ONCE) == 0)
14783 return FALSE;
0c511000 14784
c77ec726
AM
14785 /* Don't put group member sections on our list of already linked
14786 sections. They are handled as a group via their group section. */
14787 if (elf_sec_group (sec) != NULL)
14788 return FALSE;
0c511000 14789
c77ec726
AM
14790 /* For a SHT_GROUP section, use the group signature as the key. */
14791 name = sec->name;
14792 if ((flags & SEC_GROUP) != 0
14793 && elf_next_in_group (sec) != NULL
14794 && elf_group_name (elf_next_in_group (sec)) != NULL)
14795 key = elf_group_name (elf_next_in_group (sec));
14796 else
14797 {
14798 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14799 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14800 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14801 key++;
0c511000 14802 else
c77ec726
AM
14803 /* Must be a user linkonce section that doesn't follow gcc's
14804 naming convention. In this case we won't be matching
14805 single member groups. */
14806 key = name;
0c511000 14807 }
6d2cd210 14808
c77ec726 14809 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14810
14811 for (l = already_linked_list->entry; l != NULL; l = l->next)
14812 {
c2370991 14813 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14814 sections with a signature of <key> (<key> is some string),
14815 and linkonce sections named .gnu.linkonce.<type>.<key>.
14816 Match like sections. LTO plugin sections are an exception.
14817 They are always named .gnu.linkonce.t.<key> and match either
14818 type of section. */
14819 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14820 && ((flags & SEC_GROUP) != 0
14821 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14822 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14823 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14824 {
14825 /* The section has already been linked. See if we should
6d2cd210 14826 issue a warning. */
c77ec726
AM
14827 if (!_bfd_handle_already_linked (sec, l, info))
14828 return FALSE;
082b7297 14829
c77ec726 14830 if (flags & SEC_GROUP)
3d7f7666 14831 {
c77ec726
AM
14832 asection *first = elf_next_in_group (sec);
14833 asection *s = first;
3d7f7666 14834
c77ec726 14835 while (s != NULL)
3d7f7666 14836 {
c77ec726
AM
14837 s->output_section = bfd_abs_section_ptr;
14838 /* Record which group discards it. */
14839 s->kept_section = l->sec;
14840 s = elf_next_in_group (s);
14841 /* These lists are circular. */
14842 if (s == first)
14843 break;
3d7f7666
L
14844 }
14845 }
082b7297 14846
43e1669b 14847 return TRUE;
082b7297
L
14848 }
14849 }
14850
c77ec726
AM
14851 /* A single member comdat group section may be discarded by a
14852 linkonce section and vice versa. */
14853 if ((flags & SEC_GROUP) != 0)
3d7f7666 14854 {
c77ec726 14855 asection *first = elf_next_in_group (sec);
c2370991 14856
c77ec726
AM
14857 if (first != NULL && elf_next_in_group (first) == first)
14858 /* Check this single member group against linkonce sections. */
14859 for (l = already_linked_list->entry; l != NULL; l = l->next)
14860 if ((l->sec->flags & SEC_GROUP) == 0
14861 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14862 {
14863 first->output_section = bfd_abs_section_ptr;
14864 first->kept_section = l->sec;
14865 sec->output_section = bfd_abs_section_ptr;
14866 break;
14867 }
14868 }
14869 else
14870 /* Check this linkonce section against single member groups. */
14871 for (l = already_linked_list->entry; l != NULL; l = l->next)
14872 if (l->sec->flags & SEC_GROUP)
6d2cd210 14873 {
c77ec726 14874 asection *first = elf_next_in_group (l->sec);
6d2cd210 14875
c77ec726
AM
14876 if (first != NULL
14877 && elf_next_in_group (first) == first
14878 && bfd_elf_match_symbols_in_sections (first, sec, info))
14879 {
14880 sec->output_section = bfd_abs_section_ptr;
14881 sec->kept_section = first;
14882 break;
14883 }
6d2cd210 14884 }
0c511000 14885
c77ec726
AM
14886 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14887 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14888 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14889 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14890 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14891 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14892 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14893 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14894 The reverse order cannot happen as there is never a bfd with only the
14895 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14896 matter as here were are looking only for cross-bfd sections. */
14897
14898 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14899 for (l = already_linked_list->entry; l != NULL; l = l->next)
14900 if ((l->sec->flags & SEC_GROUP) == 0
14901 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14902 {
14903 if (abfd != l->sec->owner)
14904 sec->output_section = bfd_abs_section_ptr;
14905 break;
14906 }
80c29487 14907
082b7297 14908 /* This is the first section with this name. Record it. */
c77ec726 14909 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14910 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14911 return sec->output_section == bfd_abs_section_ptr;
082b7297 14912}
81e1b023 14913
a4d8e49b
L
14914bfd_boolean
14915_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14916{
14917 return sym->st_shndx == SHN_COMMON;
14918}
14919
14920unsigned int
14921_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14922{
14923 return SHN_COMMON;
14924}
14925
14926asection *
14927_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14928{
14929 return bfd_com_section_ptr;
14930}
10455f89
HPN
14931
14932bfd_vma
14933_bfd_elf_default_got_elt_size (bfd *abfd,
14934 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14935 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14936 bfd *ibfd ATTRIBUTE_UNUSED,
14937 unsigned long symndx ATTRIBUTE_UNUSED)
14938{
14939 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14940 return bed->s->arch_size / 8;
14941}
83bac4b0
NC
14942
14943/* Routines to support the creation of dynamic relocs. */
14944
83bac4b0
NC
14945/* Returns the name of the dynamic reloc section associated with SEC. */
14946
14947static const char *
14948get_dynamic_reloc_section_name (bfd * abfd,
14949 asection * sec,
14950 bfd_boolean is_rela)
14951{
ddcf1fcf 14952 char *name;
fd361982 14953 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14954 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14955
ddcf1fcf 14956 if (old_name == NULL)
83bac4b0
NC
14957 return NULL;
14958
ddcf1fcf 14959 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14960 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14961
14962 return name;
14963}
14964
14965/* Returns the dynamic reloc section associated with SEC.
14966 If necessary compute the name of the dynamic reloc section based
14967 on SEC's name (looked up in ABFD's string table) and the setting
14968 of IS_RELA. */
14969
14970asection *
14971_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14972 asection * sec,
14973 bfd_boolean is_rela)
14974{
14975 asection * reloc_sec = elf_section_data (sec)->sreloc;
14976
14977 if (reloc_sec == NULL)
14978 {
14979 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14980
14981 if (name != NULL)
14982 {
3d4d4302 14983 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14984
14985 if (reloc_sec != NULL)
14986 elf_section_data (sec)->sreloc = reloc_sec;
14987 }
14988 }
14989
14990 return reloc_sec;
14991}
14992
14993/* Returns the dynamic reloc section associated with SEC. If the
14994 section does not exist it is created and attached to the DYNOBJ
14995 bfd and stored in the SRELOC field of SEC's elf_section_data
14996 structure.
f8076f98 14997
83bac4b0
NC
14998 ALIGNMENT is the alignment for the newly created section and
14999 IS_RELA defines whether the name should be .rela.<SEC's name>
15000 or .rel.<SEC's name>. The section name is looked up in the
15001 string table associated with ABFD. */
15002
15003asection *
ca4be51c
AM
15004_bfd_elf_make_dynamic_reloc_section (asection *sec,
15005 bfd *dynobj,
15006 unsigned int alignment,
15007 bfd *abfd,
15008 bfd_boolean is_rela)
83bac4b0
NC
15009{
15010 asection * reloc_sec = elf_section_data (sec)->sreloc;
15011
15012 if (reloc_sec == NULL)
15013 {
15014 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15015
15016 if (name == NULL)
15017 return NULL;
15018
3d4d4302 15019 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
15020
15021 if (reloc_sec == NULL)
15022 {
3d4d4302
AM
15023 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15024 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
15025 if ((sec->flags & SEC_ALLOC) != 0)
15026 flags |= SEC_ALLOC | SEC_LOAD;
15027
3d4d4302 15028 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
15029 if (reloc_sec != NULL)
15030 {
8877b5e5
AM
15031 /* _bfd_elf_get_sec_type_attr chooses a section type by
15032 name. Override as it may be wrong, eg. for a user
15033 section named "auto" we'll get ".relauto" which is
15034 seen to be a .rela section. */
15035 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15036 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15037 reloc_sec = NULL;
15038 }
15039 }
15040
15041 elf_section_data (sec)->sreloc = reloc_sec;
15042 }
15043
15044 return reloc_sec;
15045}
1338dd10 15046
bffebb6b
AM
15047/* Copy the ELF symbol type and other attributes for a linker script
15048 assignment from HSRC to HDEST. Generally this should be treated as
15049 if we found a strong non-dynamic definition for HDEST (except that
15050 ld ignores multiple definition errors). */
1338dd10 15051void
bffebb6b
AM
15052_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15053 struct bfd_link_hash_entry *hdest,
15054 struct bfd_link_hash_entry *hsrc)
1338dd10 15055{
bffebb6b
AM
15056 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15057 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15058 Elf_Internal_Sym isym;
1338dd10
PB
15059
15060 ehdest->type = ehsrc->type;
35fc36a8 15061 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15062
15063 isym.st_other = ehsrc->other;
5160d0f3 15064 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
1338dd10 15065}
351f65ca
L
15066
15067/* Append a RELA relocation REL to section S in BFD. */
15068
15069void
15070elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15071{
15072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15073 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15074 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15075 bed->s->swap_reloca_out (abfd, rel, loc);
15076}
15077
15078/* Append a REL relocation REL to section S in BFD. */
15079
15080void
15081elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15082{
15083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15084 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15085 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15086 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15087}
7dba9362
AM
15088
15089/* Define __start, __stop, .startof. or .sizeof. symbol. */
15090
15091struct bfd_link_hash_entry *
15092bfd_elf_define_start_stop (struct bfd_link_info *info,
15093 const char *symbol, asection *sec)
15094{
487b6440 15095 struct elf_link_hash_entry *h;
7dba9362 15096
487b6440
AM
15097 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15098 FALSE, FALSE, TRUE);
e1b5d517 15099 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
15100 if (h != NULL
15101 && (h->root.type == bfd_link_hash_undefined
15102 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15103 || ((h->ref_regular || h->def_dynamic)
15104 && !h->def_regular
15105 && h->root.type != bfd_link_hash_common)))
7dba9362 15106 {
bf3077a6 15107 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15108 h->verinfo.verdef = NULL;
487b6440
AM
15109 h->root.type = bfd_link_hash_defined;
15110 h->root.u.def.section = sec;
15111 h->root.u.def.value = 0;
15112 h->def_regular = 1;
15113 h->def_dynamic = 0;
15114 h->start_stop = 1;
15115 h->u2.start_stop_section = sec;
15116 if (symbol[0] == '.')
15117 {
15118 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15119 const struct elf_backend_data *bed;
15120 bed = get_elf_backend_data (info->output_bfd);
15121 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15122 }
36b8fda5
AM
15123 else
15124 {
15125 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15126 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15127 | info->start_stop_visibility);
bf3077a6 15128 if (was_dynamic)
36b8fda5
AM
15129 bfd_elf_link_record_dynamic_symbol (info, h);
15130 }
487b6440 15131 return &h->root;
7dba9362 15132 }
487b6440 15133 return NULL;
7dba9362 15134}
5dbc8b37
L
15135
15136/* Find dynamic relocs for H that apply to read-only sections. */
15137
15138asection *
15139_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15140{
15141 struct elf_dyn_relocs *p;
15142
15143 for (p = h->dyn_relocs; p != NULL; p = p->next)
15144 {
15145 asection *s = p->sec->output_section;
15146
15147 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15148 return p->sec;
15149 }
15150 return NULL;
15151}
d49e5065
L
15152
15153/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15154 read-only sections. */
15155
15156bfd_boolean
15157_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15158{
15159 asection *sec;
15160
15161 if (h->root.type == bfd_link_hash_indirect)
15162 return TRUE;
15163
15164 sec = _bfd_elf_readonly_dynrelocs (h);
15165 if (sec != NULL)
15166 {
15167 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15168
15169 info->flags |= DF_TEXTREL;
15170 /* xgettext:c-format */
15171 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15172 "in read-only section `%pA'\n"),
15173 sec->owner, h->root.root.string, sec);
15174
15175 if (bfd_link_textrel_check (info))
15176 /* xgettext:c-format */
15177 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15178 "in read-only section `%pA'\n"),
15179 sec->owner, h->root.root.string, sec);
15180
15181 /* Not an error, just cut short the traversal. */
15182 return FALSE;
15183 }
15184 return TRUE;
15185}
3084d7a2
L
15186
15187/* Add dynamic tags. */
15188
15189bfd_boolean
15190_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15191 bfd_boolean need_dynamic_reloc)
15192{
15193 struct elf_link_hash_table *htab = elf_hash_table (info);
15194
15195 if (htab->dynamic_sections_created)
15196 {
15197 /* Add some entries to the .dynamic section. We fill in the
15198 values later, in finish_dynamic_sections, but we must add
15199 the entries now so that we get the correct size for the
15200 .dynamic section. The DT_DEBUG entry is filled in by the
15201 dynamic linker and used by the debugger. */
15202#define add_dynamic_entry(TAG, VAL) \
15203 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15204
15205 const struct elf_backend_data *bed
15206 = get_elf_backend_data (output_bfd);
15207
15208 if (bfd_link_executable (info))
15209 {
15210 if (!add_dynamic_entry (DT_DEBUG, 0))
15211 return FALSE;
15212 }
15213
15214 if (htab->dt_pltgot_required || htab->splt->size != 0)
15215 {
15216 /* DT_PLTGOT is used by prelink even if there is no PLT
15217 relocation. */
15218 if (!add_dynamic_entry (DT_PLTGOT, 0))
15219 return FALSE;
15220 }
15221
15222 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15223 {
15224 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15225 || !add_dynamic_entry (DT_PLTREL,
15226 (bed->rela_plts_and_copies_p
15227 ? DT_RELA : DT_REL))
15228 || !add_dynamic_entry (DT_JMPREL, 0))
15229 return FALSE;
15230 }
15231
15232 if (htab->tlsdesc_plt
15233 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15234 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15235 return FALSE;
15236
15237 if (need_dynamic_reloc)
15238 {
15239 if (bed->rela_plts_and_copies_p)
15240 {
15241 if (!add_dynamic_entry (DT_RELA, 0)
15242 || !add_dynamic_entry (DT_RELASZ, 0)
15243 || !add_dynamic_entry (DT_RELAENT,
15244 bed->s->sizeof_rela))
15245 return FALSE;
15246 }
15247 else
15248 {
15249 if (!add_dynamic_entry (DT_REL, 0)
15250 || !add_dynamic_entry (DT_RELSZ, 0)
15251 || !add_dynamic_entry (DT_RELENT,
15252 bed->s->sizeof_rel))
15253 return FALSE;
15254 }
15255
15256 /* If any dynamic relocs apply to a read-only section,
15257 then we need a DT_TEXTREL entry. */
15258 if ((info->flags & DF_TEXTREL) == 0)
15259 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15260 info);
15261
15262 if ((info->flags & DF_TEXTREL) != 0)
15263 {
15264 if (htab->ifunc_resolvers)
15265 info->callbacks->einfo
15266 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15267 "may result in a segfault at runtime; recompile with %s\n"),
15268 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15269
15270 if (!add_dynamic_entry (DT_TEXTREL, 0))
15271 return FALSE;
15272 }
15273 }
15274 }
15275#undef add_dynamic_entry
15276
15277 return TRUE;
15278}