Fix thinko in recent update to bfd_section_from_shdr.
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
b3adc24a 2 Copyright (C) 1995-2020 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
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
07d6d2b8 79 return h->root.u.def.section;
2f0c68f2
CM
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
ad32986f 121 bh = &h->root;
d98685ac 122 }
ad32986f
NC
123 else
124 bh = NULL;
d98685ac 125
cf18fda4 126 bed = get_elf_backend_data (abfd);
d98685ac 127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 128 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
ad32986f 132 BFD_ASSERT (h != NULL);
d98685ac 133 h->def_regular = 1;
e28df02b 134 h->non_elf = 0;
12b2843a 135 h->root.linker_def = 1;
d98685ac 136 h->type = STT_OBJECT;
00b7642b
AM
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 139
ccabcbe5 140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
141 return h;
142}
143
b34976b6 144bfd_boolean
268b6b39 145_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
146{
147 flagword flags;
aad5d350 148 asection *s;
252b5132 149 struct elf_link_hash_entry *h;
9c5bfbb7 150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 151 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
152
153 /* This function may be called more than once. */
ce558b89 154 if (htab->sgot != NULL)
b34976b6 155 return TRUE;
252b5132 156
e5a52504 157 flags = bed->dynamic_sec_flags;
252b5132 158
14b2f831
AM
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
6de2ae4a 164 if (s == NULL
fd361982 165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
6de2ae4a
L
166 return FALSE;
167 htab->srelgot = s;
252b5132 168
14b2f831 169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 170 if (s == NULL
fd361982 171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
64e77c6d
L
172 return FALSE;
173 htab->sgot = s;
174
252b5132
RH
175 if (bed->want_got_plt)
176 {
14b2f831 177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 178 if (s == NULL
fd361982 179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 180 return FALSE;
6de2ae4a 181 htab->sgotplt = s;
252b5132
RH
182 }
183
64e77c6d
L
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
2517a57f
AM
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
6de2ae4a
L
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
2517a57f 195 elf_hash_table (info)->hgot = h;
d98685ac
AM
196 if (h == NULL)
197 return FALSE;
2517a57f 198 }
252b5132 199
b34976b6 200 return TRUE;
252b5132
RH
201}
202\f
7e9f0867
AM
203/* Create a strtab to hold the dynamic symbol names. */
204static bfd_boolean
205_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206{
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
6cd255ca
L
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
57963c05 219 asection *s;
6cd255ca 220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 221 if ((ibfd->flags
57963c05
AM
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
7e9f0867
AM
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242}
243
45d6a902
AM
244/* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
252b5132 250
b34976b6 251bfd_boolean
268b6b39 252_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 253{
45d6a902 254 flagword flags;
91d6fa6a 255 asection *s;
9c5bfbb7 256 const struct elf_backend_data *bed;
9637f6ef 257 struct elf_link_hash_entry *h;
252b5132 258
0eddce27 259 if (! is_elf_hash_table (info->hash))
45d6a902
AM
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
7e9f0867
AM
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
45d6a902 267
7e9f0867 268 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
45d6a902
AM
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
9b8b325a 275 if (bfd_link_executable (info) && !info->nointerp)
252b5132 276 {
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
3496cb2a 279 if (s == NULL)
45d6a902
AM
280 return FALSE;
281 }
bb0deeff 282
45d6a902
AM
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
14b2f831
AM
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
45d6a902 287 if (s == NULL
fd361982 288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
289 return FALSE;
290
14b2f831
AM
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
45d6a902 293 if (s == NULL
fd361982 294 || !bfd_set_section_alignment (s, 1))
45d6a902
AM
295 return FALSE;
296
14b2f831
AM
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
45d6a902 299 if (s == NULL
fd361982 300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
301 return FALSE;
302
14b2f831
AM
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
45d6a902 305 if (s == NULL
fd361982 306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902 307 return FALSE;
cae1fbbb 308 elf_hash_table (info)->dynsym = s;
45d6a902 309
14b2f831
AM
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
3496cb2a 312 if (s == NULL)
45d6a902
AM
313 return FALSE;
314
14b2f831 315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 316 if (s == NULL
fd361982 317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
9637f6ef
L
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
45d6a902
AM
329 return FALSE;
330
fdc90cb4
JJ
331 if (info->emit_hash)
332 {
14b2f831
AM
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
fdc90cb4 335 if (s == NULL
fd361982 336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
f16a9783 341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 342 {
14b2f831
AM
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
fdc90cb4 345 if (s == NULL
fd361982 346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
45d6a902
AM
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
894891db
NC
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367}
368
369/* Create dynamic sections when linking against a dynamic object. */
370
371bfd_boolean
268b6b39 372_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
373{
374 flagword flags, pltflags;
7325306f 375 struct elf_link_hash_entry *h;
45d6a902 376 asection *s;
9c5bfbb7 377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 378 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 379
252b5132
RH
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
e5a52504 382 flags = bed->dynamic_sec_flags;
252b5132
RH
383
384 pltflags = flags;
252b5132 385 if (bed->plt_not_loaded)
6df4d94c
MM
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
5d1634d7 389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
14b2f831 395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 396 if (s == NULL
fd361982 397 || !bfd_set_section_alignment (s, bed->plt_alignment))
b34976b6 398 return FALSE;
6de2ae4a 399 htab->splt = s;
252b5132 400
d98685ac
AM
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
7325306f
RS
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
252b5132 411
14b2f831
AM
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
252b5132 416 if (s == NULL
fd361982 417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 418 return FALSE;
6de2ae4a 419 htab->srelplt = s;
252b5132
RH
420
421 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 422 return FALSE;
252b5132 423
3018b441
RH
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
14b2f831 432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 433 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 434 if (s == NULL)
b34976b6 435 return FALSE;
9d19e4fd 436 htab->sdynbss = s;
252b5132 437
5474d94f
AM
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
5474d94f 443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 444 flags);
5474d94f
AM
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
3018b441 450 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
9d19e4fd 461 if (bfd_link_executable (info))
3018b441 462 {
14b2f831
AM
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
3018b441 467 if (s == NULL
fd361982 468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 469 return FALSE;
9d19e4fd 470 htab->srelbss = s;
5474d94f
AM
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
fd361982 479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
5474d94f
AM
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
3018b441 483 }
252b5132
RH
484 }
485
b34976b6 486 return TRUE;
252b5132
RH
487}
488\f
252b5132
RH
489/* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
b34976b6 497bfd_boolean
c152c796
AM
498bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
252b5132
RH
500{
501 if (h->dynindx == -1)
502 {
2b0f7ef9 503 struct elf_strtab_hash *dynstr;
68b6ddd0 504 char *p;
252b5132 505 const char *name;
ef53be89 506 size_t indx;
252b5132 507
7a13edea
NC
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
513 {
514 case STV_INTERNAL:
515 case STV_HIDDEN:
9d6eee78
L
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
38048eb9 518 {
f5385ebf 519 h->forced_local = 1;
67687978
PB
520 if (!elf_hash_table (info)->is_relocatable_executable)
521 return TRUE;
7a13edea 522 }
0444bdd4 523
7a13edea
NC
524 default:
525 break;
526 }
527
252b5132
RH
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
530
531 dynstr = elf_hash_table (info)->dynstr;
532 if (dynstr == NULL)
533 {
534 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 536 if (dynstr == NULL)
b34976b6 537 return FALSE;
252b5132
RH
538 }
539
540 /* We don't put any version information in the dynamic string
aad5d350 541 table. */
252b5132
RH
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
544 if (p != NULL)
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
550 *p = 0;
551
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553
554 if (p != NULL)
555 *p = ELF_VER_CHR;
252b5132 556
ef53be89 557 if (indx == (size_t) -1)
b34976b6 558 return FALSE;
252b5132
RH
559 h->dynstr_index = indx;
560 }
561
b34976b6 562 return TRUE;
252b5132 563}
45d6a902 564\f
55255dae
L
565/* Mark a symbol dynamic. */
566
28caa186 567static void
55255dae 568bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
55255dae 571{
40b36307 572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 573
40b36307 574 /* It may be called more than once on the same H. */
0e1862bb 575 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
576 return;
577
40b36307
L
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
b8871f35 580 || h->type == STT_COMMON
40b36307 581 || (sym != NULL
b8871f35
L
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 584 || (d != NULL
73ec947d 585 && h->non_elf
40b36307 586 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
587 {
588 h->dynamic = 1;
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 non-IR reference. */
591 h->root.non_ir_ref_dynamic = 1;
592 }
55255dae
L
593}
594
45d6a902
AM
595/* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598bfd_boolean
fe21a8fc
L
599bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
268b6b39 601 const char *name,
fe21a8fc
L
602 bfd_boolean provide,
603 bfd_boolean hidden)
45d6a902 604{
00cbee0a 605 struct elf_link_hash_entry *h, *hv;
4ea42fb7 606 struct elf_link_hash_table *htab;
00cbee0a 607 const struct elf_backend_data *bed;
45d6a902 608
0eddce27 609 if (!is_elf_hash_table (info->hash))
45d6a902
AM
610 return TRUE;
611
4ea42fb7
AM
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 614 if (h == NULL)
4ea42fb7 615 return provide;
45d6a902 616
8e2a4f11
AM
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
0f550b3d
L
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
73ec947d
AM
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
00cbee0a 641 switch (h->root.type)
77cfaee6 642 {
00cbee0a
L
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
4ea42fb7 652 h->root.type = bfd_link_hash_new;
77cfaee6
AM
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
655 break;
656 case bfd_link_hash_new:
00cbee0a
L
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 660 the versioned symbol point to this one. */
00cbee0a
L
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
8e2a4f11
AM
673 default:
674 BFD_FAIL ();
c2596ca5 675 return FALSE;
55255dae 676 }
45d6a902
AM
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
f5385ebf
AM
683 && h->def_dynamic
684 && !h->def_regular)
45d6a902
AM
685 h->root.type = bfd_link_hash_undefined;
686
48e30f52
L
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
690 more. */
691 if (h->def_dynamic && !h->def_regular)
b531344c
MR
692 h->verinfo.verdef = NULL;
693
694 /* Make sure this symbol is not garbage collected. */
695 h->mark = 1;
45d6a902 696
f5385ebf 697 h->def_regular = 1;
45d6a902 698
eb8476a6 699 if (hidden)
fe21a8fc 700 {
91d6fa6a 701 bed = get_elf_backend_data (output_bfd);
b8297068
AM
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705 }
706
6fa3860b
PB
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 and executables. */
0e1862bb 709 if (!bfd_link_relocatable (info)
6fa3860b
PB
710 && h->dynindx != -1
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713 h->forced_local = 1;
714
f5385ebf
AM
715 if ((h->def_dynamic
716 || h->ref_dynamic
6b3b0ab8
L
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 719 && !h->forced_local
45d6a902
AM
720 && h->dynindx == -1)
721 {
c152c796 722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
723 return FALSE;
724
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
60d67dc8 728 if (h->is_weakalias)
45d6a902 729 {
60d67dc8
AM
730 struct elf_link_hash_entry *def = weakdef (h);
731
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
734 return FALSE;
735 }
736 }
737
738 return TRUE;
739}
42751cf3 740
8c58d23b
AM
741/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
744
745int
c152c796
AM
746bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 bfd *input_bfd,
748 long input_indx)
8c58d23b 749{
986f0783 750 size_t amt;
8c58d23b
AM
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
ef53be89 754 size_t dynstr_index;
8c58d23b
AM
755 char *name;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
758
0eddce27 759 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
760 return 0;
761
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 return 1;
766
767 amt = sizeof (*entry);
a50b1753 768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
769 if (entry == NULL)
770 return 0;
771
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 774 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
775 {
776 bfd_release (input_bfd, entry);
777 return 0;
778 }
779
780 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 781 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
782 {
783 asection *s;
784
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
787 {
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
791 return 2;
792 }
793 }
794
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
798
799 dynstr = elf_hash_table (info)->dynstr;
800 if (dynstr == NULL)
801 {
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 if (dynstr == NULL)
805 return 0;
806 }
807
b34976b6 808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 809 if (dynstr_index == (size_t) -1)
8c58d23b
AM
810 return 0;
811 entry->isym.st_name = dynstr_index;
812
813 eht = elf_hash_table (info);
814
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
819 eht->dynsymcount++;
820
821 /* Whatever binding the symbol had before, it's now local. */
822 entry->isym.st_info
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824
825 /* The dynindx will be set at the end of size_dynamic_sections. */
826
827 return 1;
828}
829
30b30c21 830/* Return the dynindex of a local dynamic symbol. */
42751cf3 831
30b30c21 832long
268b6b39
AM
833_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 bfd *input_bfd,
835 long input_indx)
30b30c21
RH
836{
837 struct elf_link_local_dynamic_entry *e;
838
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 return e->dynindx;
842 return -1;
843}
844
845/* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
848
b34976b6 849static bfd_boolean
268b6b39
AM
850elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
42751cf3 852{
a50b1753 853 size_t *count = (size_t *) data;
30b30c21 854
6fa3860b
PB
855 if (h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862}
863
864
865/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
867
868static bfd_boolean
869elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 void *data)
871{
a50b1753 872 size_t *count = (size_t *) data;
6fa3860b 873
6fa3860b
PB
874 if (!h->forced_local)
875 return TRUE;
876
42751cf3 877 if (h->dynindx != -1)
30b30c21
RH
878 h->dynindx = ++(*count);
879
b34976b6 880 return TRUE;
42751cf3 881}
30b30c21 882
aee6f5b4
AO
883/* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
885bfd_boolean
d00dd7dc
AM
886_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
888 asection *p)
aee6f5b4 889{
74541ad4 890 struct elf_link_hash_table *htab;
ca55926c 891 asection *ip;
74541ad4 892
aee6f5b4
AO
893 switch (elf_section_data (p)->this_hdr.sh_type)
894 {
895 case SHT_PROGBITS:
896 case SHT_NOBITS:
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
899 case SHT_NULL:
74541ad4 900 htab = elf_hash_table (info);
74541ad4
AM
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
903
ca55926c 904 return (htab->dynobj != NULL
3d4d4302 905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 906 && ip->output_section == p);
aee6f5b4
AO
907
908 /* There shouldn't be section relative relocations
909 against any other section. */
910 default:
911 return TRUE;
912 }
913}
914
d00dd7dc
AM
915bfd_boolean
916_bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
920{
921 return TRUE;
922}
923
062e2358 924/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
30b30c21 932
554220db
AM
933static unsigned long
934_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
30b30c21
RH
937{
938 unsigned long dynsymcount = 0;
63f452a8 939 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 940
0e1862bb
L
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 943 {
aee6f5b4 944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
945 asection *p;
946 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 947 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 948 && (p->flags & SEC_ALLOC) != 0
7f923b7f 949 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
74541ad4 957 elf_section_data (p)->dynindx = 0;
30b30c21 958 }
63f452a8
AM
959 if (do_sec)
960 *section_sym_count = dynsymcount;
30b30c21 961
6fa3860b
PB
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
30b30c21
RH
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
90ac2420 972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
d5486c43
L
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
30b30c21 983
ccabcbe5
AM
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
30b30c21 986}
252b5132 987
54ac0771
L
988/* Merge st_other field. */
989
990static void
991elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 992 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 993 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
994{
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
cd3416da 998 code might be needed here. */
54ac0771
L
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
cd3416da 1003 if (!dynamic)
54ac0771 1004 {
cd3416da
AM
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1007
cd3416da
AM
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1012 }
b8417128
AM
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1016 h->protected_def = 1;
54ac0771
L
1017}
1018
4f3fedcf
AM
1019/* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
45d6a902 1031
8a56bd02 1032static bfd_boolean
268b6b39
AM
1033_bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
4f3fedcf
AM
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
37a9e49a 1041 bfd_boolean *pold_weak,
af44c138 1042 unsigned int *pold_alignment,
268b6b39
AM
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
6e33951e
L
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
252b5132 1048{
7479dfd4 1049 asection *sec, *oldsec;
45d6a902 1050 struct elf_link_hash_entry *h;
90c984fc 1051 struct elf_link_hash_entry *hi;
45d6a902
AM
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1057 const struct elf_backend_data *bed;
6e33951e 1058 char *new_version;
93f4de39 1059 bfd_boolean default_sym = *matched;
45d6a902
AM
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
252b5132 1075
88ba32a0
L
1076 bed = get_elf_backend_data (abfd);
1077
6e33951e 1078 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1079 if (h->versioned != unversioned)
6e33951e 1080 {
422f1182
L
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
6e33951e 1098 }
422f1182
L
1099 else
1100 new_version = NULL;
6e33951e 1101
90c984fc
L
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
45d6a902
AM
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
6e33951e
L
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
ae7683d2 1115 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1116 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1117 true if the new symbol is only visible to the symbol with
6e33951e 1118 the same symbol version. */
422f1182
L
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
422f1182
L
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
6e33951e
L
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
934bce08
AM
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
40b36307 1185 /* We have to check it for every instance since the first few may be
ee659f1f 1186 references and not all compilers emit symbol type for undefined
40b36307
L
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
ee659f1f
AM
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
6e33951e
L
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
ee659f1f
AM
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
45d6a902
AM
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
252b5132 1226 {
f5385ebf 1227 h->non_elf = 0;
45d6a902
AM
1228 return TRUE;
1229 }
252b5132 1230
45d6a902
AM
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
895fa45f 1238 && (newweak || oldweak)
45d6a902 1239 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1240 || !h->def_regular))
45d6a902
AM
1241 return TRUE;
1242
707bba77 1243 olddyn = FALSE;
45d6a902
AM
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1246 else if (oldsec != NULL)
45d6a902 1247 {
707bba77 1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1249 indices used by MIPS ELF. */
707bba77 1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1251 }
252b5132 1252
1a3b5c34
AM
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
45d6a902
AM
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
707bba77 1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1267
707bba77
AM
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
202ac193 1270 && h->root.type != bfd_link_hash_common);
45d6a902 1271
0a36a439
L
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
c5d37467 1281 if (!(newfunc && oldfunc)
5b677558
AM
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
c5d37467
AM
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1287 {
c5d37467
AM
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
580a2b6e
L
1325 }
1326
4c34aff8
AM
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
3b36f7e6 1342 ntbfd = abfd;
7479dfd4
L
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
4eca0228 1360 _bfd_error_handler
695344c0 1361 /* xgettext:c-format */
871b3ab2
AM
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1365 else if (!tdef && !ntdef)
4eca0228 1366 _bfd_error_handler
695344c0 1367 /* xgettext:c-format */
871b3ab2
AM
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
c08bb8dd 1370 h->root.root.string, tbfd, ntbfd);
7479dfd4 1371 else if (tdef)
4eca0228 1372 _bfd_error_handler
695344c0 1373 /* xgettext:c-format */
871b3ab2
AM
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
c08bb8dd 1376 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1377 else
4eca0228 1378 _bfd_error_handler
695344c0 1379 /* xgettext:c-format */
871b3ab2
AM
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1382 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
45d6a902
AM
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
9c7a29a3 1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
f5385ebf 1396 h->ref_dynamic = 1;
90c984fc 1397 hi->ref_dynamic = 1;
45d6a902
AM
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1403 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
9c7a29a3 1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1409 && h->def_dynamic)
45d6a902
AM
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
6c9b78e6 1414 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
6c9b78e6 1422 hi->root.type = h->root.type;
d2dee3b2 1423 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1425
6c9b78e6 1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1428 {
aed81c4e
MR
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
d2dee3b2
L
1434 }
1435 else
aed81c4e
MR
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
aed81c4e
MR
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
6c9b78e6 1443 h = hi;
d2dee3b2
L
1444 }
1445 else
6c9b78e6 1446 h = hi;
d2dee3b2 1447 }
1de1a317 1448
f5eda473
AM
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1457 {
1de1a317 1458 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
f5eda473 1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1468 {
f5eda473
AM
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
45d6a902 1474 }
f5eda473
AM
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
45d6a902
AM
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
14a793b2 1483
15b43f48
AM
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
165f707a
AM
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
15b43f48
AM
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
165f707a 1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1501 newweak = FALSE;
15b43f48 1502 if (olddef && newdyn)
0f8a2703
AM
1503 oldweak = FALSE;
1504
d334575b 1505 /* Allow changes between different types of function symbol. */
0a36a439 1506 if (newfunc && oldfunc)
fcb93ecf
PB
1507 *type_change_ok = TRUE;
1508
79349b09
AM
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
252b5132 1512
79349b09
AM
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
45d6a902 1525
45d6a902
AM
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
79349b09 1551 && !newweak
45d6a902
AM
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
0a36a439 1555 && !newfunc)
45d6a902
AM
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
f5385ebf 1563 && h->def_dynamic
45d6a902
AM
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
0a36a439 1567 && !oldfunc)
45d6a902
AM
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
a4d8e49b
L
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
5d13b3b3
AM
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
a4d8e49b 1580
a83ef4d1
L
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
93f4de39 1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
45d6a902
AM
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1a72702b
AM
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
252b5132 1614
45d6a902 1615 *size_change_ok = TRUE;
252b5132
RH
1616 }
1617
45d6a902
AM
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
8170f769 1630 object to override a weak symbol in a shared object. */
45d6a902
AM
1631
1632 if (newdyn
1633 && newdef
77cfaee6 1634 && (olddef
45d6a902 1635 || (h->root.type == bfd_link_hash_common
8170f769 1636 && (newweak || newfunc))))
45d6a902
AM
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
252b5132 1641
45d6a902
AM
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
252b5132 1644
45d6a902
AM
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
252b5132 1650
45d6a902
AM
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
45d6a902
AM
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
a4d8e49b 1668 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1669 *size_change_ok = TRUE;
1670 }
1671
c5e2cead 1672 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1673 if (newdef && olddef && newweak)
54ac0771 1674 {
35ed3f94 1675 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1678 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
54ac0771
L
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
b8417128 1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
c5e2cead 1697
45d6a902
AM
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
0f8a2703 1706 symbol is a function or is weak. */
45d6a902
AM
1707
1708 flip = NULL;
77cfaee6 1709 if (!newdyn
45d6a902
AM
1710 && (newdef
1711 || (bfd_is_com_section (sec)
0a36a439 1712 && (oldweak || oldfunc)))
45d6a902
AM
1713 && olddyn
1714 && olddef
f5385ebf 1715 && h->def_dynamic)
45d6a902
AM
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
0a36a439
L
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
45d6a902 1743
6c9b78e6
AM
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
45d6a902
AM
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1a72702b
AM
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
45d6a902 1768
4cc11e76 1769 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
af44c138
L
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
6c9b78e6
AM
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
45d6a902
AM
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
45d6a902 1800 flip->root.type = h->root.type;
00cbee0a 1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1805 if (h->def_dynamic)
45d6a902 1806 {
f5385ebf
AM
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
45d6a902
AM
1809 }
1810 }
1811
45d6a902
AM
1812 return TRUE;
1813}
1814
1815/* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
4f3fedcf 1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1818 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1819
28caa186 1820static bfd_boolean
268b6b39
AM
1821_bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
4f3fedcf
AM
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
e3c9d234 1829 bfd_boolean *dynsym)
45d6a902
AM
1830{
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
9c5bfbb7 1837 const struct elf_backend_data *bed;
45d6a902
AM
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
e3c9d234 1840 bfd_boolean override;
45d6a902
AM
1841 char *p;
1842 size_t len, shortlen;
ffd65175 1843 asection *tmp_sec;
6e33951e 1844 bfd_boolean matched;
45d6a902 1845
422f1182
L
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
45d6a902
AM
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
422f1182
L
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
4373f8af
L
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
45d6a902 1879
45d6a902
AM
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
a50b1753 1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
6e33951e 1897 matched = TRUE;
ffd65175
AM
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1900 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1901 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
5fa370e4 1907 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
45d6a902
AM
1932 if (! override)
1933 {
c6e8a9a8 1934 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1935 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1936 {
1937 bh = &hi->root;
fbcc8baf 1938 if (bh->type == bfd_link_hash_defined
6cc71b82 1939 && bh->u.def.section->owner != NULL
fbcc8baf
L
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 {
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1944 it. */
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1947 }
c6e8a9a8
L
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
45d6a902
AM
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1982 if (h->def_dynamic)
45d6a902 1983 {
f5385ebf
AM
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
45d6a902 1988 {
c152c796 1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
fab4a87f
L
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
45d6a902
AM
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
45d6a902 2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2013
68c88cd4
AM
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
45d6a902
AM
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
0e1862bb 2026 if (! bfd_link_executable (info)
90c984fc 2027 || hi->def_dynamic
f5385ebf 2028 || hi->ref_dynamic)
45d6a902
AM
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
f5385ebf 2033 if (hi->ref_regular)
45d6a902
AM
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
dc1e8a47 2042 nondefault:
45d6a902 2043 len = strlen (name);
a50b1753 2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
ffd65175
AM
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2055 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2056 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
4cc11e76 2066 overridden by a versioned definition. */
45d6a902
AM
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2069 _bfd_error_handler
695344c0 2070 /* xgettext:c-format */
871b3ab2 2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2072 abfd, shortname);
45d6a902
AM
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
fcfa13d2 2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
0e1862bb 2099 if (! bfd_link_executable (info)
f5385ebf 2100 || hi->ref_dynamic)
45d6a902
AM
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
f5385ebf 2105 if (hi->ref_regular)
45d6a902
AM
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113}
2114\f
2115/* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2117
28caa186 2118static bfd_boolean
268b6b39 2119_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2120{
a50b1753 2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2122
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2125 return TRUE;
2126
7686d77d
AM
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2129 return TRUE;
45d6a902
AM
2130
2131 if (h->dynindx == -1
fd91d419
L
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
45d6a902 2135 {
fd91d419 2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2137 {
fd91d419
L
2138 eif->failed = TRUE;
2139 return FALSE;
45d6a902
AM
2140 }
2141 }
2142
2143 return TRUE;
2144}
2145\f
2146/* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2150
28caa186 2151static bfd_boolean
268b6b39
AM
2152_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 void *data)
45d6a902 2154{
a50b1753 2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
986f0783 2158 size_t amt;
45d6a902 2159
45d6a902
AM
2160 /* We only care about symbols defined in shared objects with version
2161 information. */
f5385ebf
AM
2162 if (!h->def_dynamic
2163 || h->def_regular
45d6a902 2164 || h->dynindx == -1
7b20f099
AM
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2168 return TRUE;
2169
2170 /* See if we already know about this version. */
28caa186
AM
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 t != NULL;
2173 t = t->vn_nextref)
45d6a902
AM
2174 {
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 continue;
2177
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 return TRUE;
2181
2182 break;
2183 }
2184
2185 /* This is a new version. Add it to tree we are building. */
2186
2187 if (t == NULL)
2188 {
2189 amt = sizeof *t;
a50b1753 2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2191 if (t == NULL)
2192 {
2193 rinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2200 }
2201
2202 amt = sizeof *a;
a50b1753 2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2204 if (a == NULL)
2205 {
2206 rinfo->failed = TRUE;
2207 return FALSE;
2208 }
45d6a902
AM
2209
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2213 fixed. */
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2218
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220 ++rinfo->vers;
2221
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223
2224 t->vn_auxptr = a;
2225
2226 return TRUE;
2227}
2228
099bb8fb
L
2229/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2231
2232static bfd_boolean
2233_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2237 bfd_boolean *hide)
2238{
2239 struct bfd_elf_version_tree *t;
2240
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2243 {
2244 if (strcmp (t->name, version_p) == 0)
2245 {
2246 size_t len;
2247 char *alc;
2248 struct bfd_elf_version_expr *d;
2249
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2252 if (alc == NULL)
2253 return FALSE;
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2258
2259 h->verinfo.vertree = t;
2260 t->used = TRUE;
2261 d = NULL;
2262
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2265
2266 /* See if there is anything to force this symbol to
2267 local scope. */
2268 if (d == NULL && t->locals.list != NULL)
2269 {
2270 d = (*t->match) (&t->locals, NULL, alc);
2271 if (d != NULL
2272 && h->dynindx != -1
2273 && ! info->export_dynamic)
2274 *hide = TRUE;
2275 }
2276
2277 free (alc);
2278 break;
2279 }
2280 }
2281
2282 *t_p = t;
2283
2284 return TRUE;
2285}
2286
2287/* Return TRUE if the symbol H is hidden by version script. */
2288
2289bfd_boolean
2290_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2292{
2293 const char *p;
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2297
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300 return TRUE;
2301
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2304 {
2305 struct bfd_elf_version_tree *t;
2306
2307 ++p;
2308 if (*p == ELF_VER_CHR)
2309 ++p;
2310
2311 if (*p != '\0'
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 && hide)
2314 {
2315 if (hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 return TRUE;
2318 }
2319 }
2320
2321 /* If we don't have a version for this symbol, see if we can find
2322 something. */
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324 {
2325 h->verinfo.vertree
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2329 {
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 return TRUE;
2332 }
2333 }
2334
2335 return FALSE;
2336}
2337
45d6a902
AM
2338/* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2342
28caa186 2343static bfd_boolean
268b6b39 2344_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2345{
28caa186 2346 struct elf_info_failed *sinfo;
45d6a902 2347 struct bfd_link_info *info;
9c5bfbb7 2348 const struct elf_backend_data *bed;
45d6a902
AM
2349 struct elf_info_failed eif;
2350 char *p;
099bb8fb 2351 bfd_boolean hide;
45d6a902 2352
a50b1753 2353 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2354 info = sinfo->info;
2355
45d6a902
AM
2356 /* Fix the symbol flags. */
2357 eif.failed = FALSE;
2358 eif.info = info;
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360 {
2361 if (eif.failed)
2362 sinfo->failed = TRUE;
2363 return FALSE;
2364 }
2365
0a640d71
L
2366 bed = get_elf_backend_data (info->output_bfd);
2367
45d6a902
AM
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
5fa370e4 2370 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2371 {
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 return TRUE;
2378 }
45d6a902 2379
099bb8fb 2380 hide = FALSE;
45d6a902
AM
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2383 {
2384 struct bfd_elf_version_tree *t;
45d6a902 2385
45d6a902
AM
2386 ++p;
2387 if (*p == ELF_VER_CHR)
6e33951e 2388 ++p;
45d6a902
AM
2389
2390 /* If there is no version string, we can just return out. */
2391 if (*p == '\0')
6e33951e 2392 return TRUE;
45d6a902 2393
099bb8fb 2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2395 {
099bb8fb
L
2396 sinfo->failed = TRUE;
2397 return FALSE;
45d6a902
AM
2398 }
2399
099bb8fb
L
2400 if (hide)
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402
45d6a902
AM
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
0e1862bb 2405 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2406 {
2407 struct bfd_elf_version_tree **pp;
2408 int version_index;
2409
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2413 return TRUE;
2414
ef53be89
AM
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 sizeof *t);
45d6a902
AM
2417 if (t == NULL)
2418 {
2419 sinfo->failed = TRUE;
2420 return FALSE;
2421 }
2422
45d6a902 2423 t->name = p;
45d6a902
AM
2424 t->name_indx = (unsigned int) -1;
2425 t->used = TRUE;
2426
2427 version_index = 1;
2428 /* Don't count anonymous version tag. */
fd91d419
L
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
45d6a902 2431 version_index = 0;
fd91d419
L
2432 for (pp = &sinfo->info->version_info;
2433 *pp != NULL;
2434 pp = &(*pp)->next)
45d6a902
AM
2435 ++version_index;
2436 t->vernum = version_index;
2437
2438 *pp = t;
2439
2440 h->verinfo.vertree = t;
2441 }
2442 else if (t == NULL)
2443 {
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
4eca0228 2446 _bfd_error_handler
695344c0 2447 /* xgettext:c-format */
871b3ab2 2448 (_("%pB: version node not found for symbol %s"),
28caa186 2449 info->output_bfd, h->root.root.string);
45d6a902
AM
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2452 return FALSE;
2453 }
45d6a902
AM
2454 }
2455
2456 /* If we don't have a version for this symbol, see if we can find
2457 something. */
099bb8fb
L
2458 if (!hide
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
45d6a902 2461 {
fd91d419
L
2462 h->verinfo.vertree
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
1e8fa21e
AM
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2467 }
2468
2469 return TRUE;
2470}
2471\f
45d6a902
AM
2472/* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2478
2479 Returns FALSE if something goes wrong. */
2480
2481static bfd_boolean
268b6b39 2482elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2483 asection *sec,
268b6b39
AM
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
45d6a902 2487{
9c5bfbb7 2488 const struct elf_backend_data *bed;
268b6b39 2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
243ef1e0
L
2493 Elf_Internal_Shdr *symtab_hdr;
2494 size_t nsyms;
45d6a902 2495
45d6a902
AM
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498 return FALSE;
2499
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502 return FALSE;
2503
243ef1e0 2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2506
45d6a902
AM
2507 bed = get_elf_backend_data (abfd);
2508
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2514 else
2515 {
2516 bfd_set_error (bfd_error_wrong_format);
2517 return FALSE;
2518 }
2519
a50b1753 2520 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2524 irela = internal_relocs;
f55b1e32 2525 while (erela <= erelaend)
45d6a902 2526 {
243ef1e0
L
2527 bfd_vma r_symndx;
2528
45d6a902 2529 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2532 r_symndx >>= 24;
ce98a316
NC
2533 if (nsyms > 0)
2534 {
2535 if ((size_t) r_symndx >= nsyms)
2536 {
4eca0228 2537 _bfd_error_handler
695344c0 2538 /* xgettext:c-format */
2dcf00ce
AM
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 }
cf35638d 2547 else if (r_symndx != STN_UNDEF)
243ef1e0 2548 {
4eca0228 2549 _bfd_error_handler
695344c0 2550 /* xgettext:c-format */
2dcf00ce
AM
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2553 " when the object file has no symbol table"),
2dcf00ce
AM
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2556 bfd_set_error (bfd_error_bad_value);
2557 return FALSE;
2558 }
45d6a902
AM
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2561 }
2562
2563 return TRUE;
2564}
2565
2566/* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2574 RELA_HDR relocations. */
45d6a902
AM
2575
2576Elf_Internal_Rela *
268b6b39
AM
2577_bfd_elf_link_read_relocs (bfd *abfd,
2578 asection *o,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
45d6a902 2582{
268b6b39 2583 void *alloc1 = NULL;
45d6a902 2584 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2588
d4730f92
BS
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
45d6a902
AM
2591
2592 if (o->reloc_count == 0)
2593 return NULL;
2594
45d6a902
AM
2595 if (internal_relocs == NULL)
2596 {
2597 bfd_size_type size;
2598
056bafd4 2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2600 if (keep_memory)
a50b1753 2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2602 else
a50b1753 2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2604 if (internal_relocs == NULL)
2605 goto error_return;
2606 }
2607
2608 if (external_relocs == NULL)
2609 {
d4730f92
BS
2610 bfd_size_type size = 0;
2611
2612 if (esdo->rel.hdr)
2613 size += esdo->rel.hdr->sh_size;
2614 if (esdo->rela.hdr)
2615 size += esdo->rela.hdr->sh_size;
45d6a902 2616
268b6b39 2617 alloc1 = bfd_malloc (size);
45d6a902
AM
2618 if (alloc1 == NULL)
2619 goto error_return;
2620 external_relocs = alloc1;
2621 }
2622
d4730f92
BS
2623 internal_rela_relocs = internal_relocs;
2624 if (esdo->rel.hdr)
2625 {
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 external_relocs,
2628 internal_relocs))
2629 goto error_return;
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2634 }
2635
2636 if (esdo->rela.hdr
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 external_relocs,
2639 internal_rela_relocs)))
45d6a902
AM
2640 goto error_return;
2641
2642 /* Cache the results for next time, if we can. */
2643 if (keep_memory)
d4730f92 2644 esdo->relocs = internal_relocs;
45d6a902
AM
2645
2646 if (alloc1 != NULL)
2647 free (alloc1);
2648
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2651
2652 return internal_relocs;
2653
2654 error_return:
2655 if (alloc1 != NULL)
2656 free (alloc1);
2657 if (alloc2 != NULL)
4dd07732
AM
2658 {
2659 if (keep_memory)
2660 bfd_release (abfd, alloc2);
2661 else
2662 free (alloc2);
2663 }
45d6a902
AM
2664 return NULL;
2665}
2666
2667/* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2669
28caa186 2670static bfd_boolean
9eaff861
AO
2671_bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2673{
9eaff861 2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2675
2676 /* That allows us to calculate the size of the section. */
9eaff861 2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2678
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
a50b1753 2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685 return FALSE;
2686
d4730f92 2687 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2688 {
2689 struct elf_link_hash_entry **p;
2690
ca4be51c
AM
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2693 if (p == NULL)
2694 return FALSE;
2695
d4730f92 2696 reldata->hashes = p;
45d6a902
AM
2697 }
2698
2699 return TRUE;
2700}
2701
2702/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2704 OUTPUT_BFD. */
2705
2706bfd_boolean
268b6b39
AM
2707_bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2712 ATTRIBUTE_UNUSED)
45d6a902
AM
2713{
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2716 bfd_byte *erel;
d4730f92 2717 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2718 asection *output_section;
9c5bfbb7 2719 const struct elf_backend_data *bed;
268b6b39 2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2721 struct bfd_elf_section_data *esdo;
45d6a902
AM
2722
2723 output_section = input_section->output_section;
45d6a902 2724
d4730f92
BS
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2728 {
d4730f92
BS
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
45d6a902 2731 }
d4730f92
BS
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2734 {
d4730f92
BS
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2737 }
2738 else
2739 {
4eca0228 2740 _bfd_error_handler
695344c0 2741 /* xgettext:c-format */
871b3ab2 2742 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2743 output_bfd, input_section->owner, input_section);
297d8443 2744 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2745 return FALSE;
2746 }
2747
d4730f92
BS
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2754 {
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2758 }
2759
2760 /* Bump the counter, so that we know where to add the next set of
2761 relocations. */
d4730f92 2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2763
2764 return TRUE;
2765}
2766\f
508c3946
L
2767/* Make weak undefined symbols in PIE dynamic. */
2768
2769bfd_boolean
2770_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2772{
0e1862bb 2773 if (bfd_link_pie (info)
508c3946
L
2774 && h->dynindx == -1
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2777
2778 return TRUE;
2779}
2780
45d6a902
AM
2781/* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2786
28caa186 2787static bfd_boolean
268b6b39
AM
2788_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
45d6a902 2790{
33774f08 2791 const struct elf_backend_data *bed;
508c3946 2792
45d6a902
AM
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
f5385ebf 2797 if (h->non_elf)
45d6a902
AM
2798 {
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2804 {
2805 h->ref_regular = 1;
2806 h->ref_regular_nonweak = 1;
2807 }
45d6a902
AM
2808 else
2809 {
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
f5385ebf
AM
2813 {
2814 h->ref_regular = 1;
2815 h->ref_regular_nonweak = 1;
2816 }
45d6a902 2817 else
f5385ebf 2818 h->def_regular = 1;
45d6a902
AM
2819 }
2820
2821 if (h->dynindx == -1
f5385ebf
AM
2822 && (h->def_dynamic
2823 || h->ref_dynamic))
45d6a902 2824 {
c152c796 2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832 else
2833 {
f5385ebf 2834 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2842 && !h->def_regular
45d6a902
AM
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2847 && !h->def_dynamic)))
2848 h->def_regular = 1;
45d6a902
AM
2849 }
2850
508c3946 2851 /* Backend specific symbol fixup. */
33774f08
AM
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855 return FALSE;
508c3946 2856
45d6a902
AM
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
f5385ebf 2860 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2863 && !h->def_regular
2864 && h->ref_regular
2865 && !h->def_dynamic
96f29d96 2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2867 h->def_regular = 1;
45d6a902 2868
af0bfb9c
AM
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
4deb8f71
L
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
af0bfb9c
AM
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2881 exported. */
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2885 && !h->dynamic
2886 && !h->ref_dynamic
2887 && h->def_regular)
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889
45d6a902
AM
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2895 will force it local. */
4deb8f71
L
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 && h->def_regular)
45d6a902 2902 {
45d6a902
AM
2903 bfd_boolean force_local;
2904
45d6a902
AM
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908 }
2909
45d6a902
AM
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
60d67dc8 2913 if (h->is_weakalias)
45d6a902 2914 {
60d67dc8
AM
2915 struct elf_link_hash_entry *def = weakdef (h);
2916
45d6a902
AM
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
5b9d7a9a
AM
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2929 {
2930 h = def;
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2933 }
45d6a902 2934 else
a26587ba 2935 {
4e6b54a6
AM
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2940 BFD_ASSERT (def->def_dynamic);
60d67dc8 2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2942 }
45d6a902
AM
2943 }
2944
2945 return TRUE;
2946}
2947
2948/* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2950 recursively. */
2951
28caa186 2952static bfd_boolean
268b6b39 2953_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2954{
a50b1753 2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2956 struct elf_link_hash_table *htab;
9c5bfbb7 2957 const struct elf_backend_data *bed;
45d6a902 2958
0eddce27 2959 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2960 return FALSE;
2961
45d6a902
AM
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2964 return TRUE;
2965
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 return FALSE;
2969
559192d8
AM
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2972
954b63d4
AM
2973 if (h->root.type == bfd_link_hash_undefweak)
2974 {
2975 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2977 else if (eif->info->dynamic_undefined_weak > 0
2978 && h->ref_regular
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2982 {
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2984 {
2985 eif->failed = TRUE;
2986 return FALSE;
2987 }
2988 }
2989 }
2990
45d6a902
AM
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
f5385ebf 2998 if (!h->needs_plt
91e21fb7 2999 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3000 && (h->def_regular
3001 || !h->def_dynamic
3002 || (!h->ref_regular
60d67dc8 3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3004 {
a6aa5195 3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3006 return TRUE;
3007 }
3008
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
f5385ebf 3011 if (h->dynamic_adjusted)
45d6a902
AM
3012 return TRUE;
3013
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
f5385ebf 3018 h->dynamic_adjusted = 1;
45d6a902
AM
3019
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3024
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3032 library model.
3033
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3038 int _timezone = 5;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3047
60d67dc8 3048 if (h->is_weakalias)
45d6a902 3049 {
60d67dc8
AM
3050 struct elf_link_hash_entry *def = weakdef (h);
3051
ec24dc88 3052 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
45d6a902 3055
ec24dc88 3056 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3059 return FALSE;
3060 }
3061
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3067 if (h->size == 0
3068 && h->type == STT_NOTYPE
f5385ebf 3069 && !h->needs_plt)
4eca0228 3070 _bfd_error_handler
45d6a902
AM
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3073
45d6a902
AM
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3075 {
3076 eif->failed = TRUE;
3077 return FALSE;
3078 }
3079
3080 return TRUE;
3081}
3082
027297b7
L
3083/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3084 DYNBSS. */
3085
3086bfd_boolean
6cabe1ea
AM
3087_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
027297b7
L
3089 asection *dynbss)
3090{
91ac5911 3091 unsigned int power_of_two;
027297b7
L
3092 bfd_vma mask;
3093 asection *sec = h->root.u.def.section;
3094
de194d85 3095 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
fd361982 3100 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3103 {
3104 mask >>= 1;
3105 --power_of_two;
3106 }
027297b7 3107
fd361982 3108 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3109 {
3110 /* Adjust the section alignment if needed. */
fd361982 3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3112 return FALSE;
3113 }
3114
91ac5911 3115 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
f7483970
L
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
889c2a67
L
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3130 info->callbacks->einfo
c1c8c1ef 3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3132 h->root.root.string);
6cabe1ea 3133
027297b7
L
3134 return TRUE;
3135}
3136
45d6a902
AM
3137/* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
28caa186 3140static bfd_boolean
268b6b39 3141_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3142{
3143 asection *sec;
3144
45d6a902
AM
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3149 {
a50b1753 3150 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
753731ee 3156 h->root.u.def.value);
45d6a902
AM
3157 }
3158
3159 return TRUE;
3160}
986a241f
RH
3161
3162/* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166bfd_boolean
268b6b39
AM
3167_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
89a2ee5a 3169 bfd_boolean not_local_protected)
986a241f
RH
3170{
3171 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
986a241f
RH
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
f5385ebf 3185 if (h->forced_local)
986a241f
RH
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
0e1862bb
L
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
986a241f
RH
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
fcb93ecf
PB
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
986a241f
RH
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
89a2ee5a 3209 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
986a241f
RH
3214 break;
3215 }
3216
aa37626c 3217 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3219 return TRUE;
3220
986a241f
RH
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224}
f6c52c13
AM
3225
3226/* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3229 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
89a2ee5a
AM
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3238
3239bfd_boolean
268b6b39
AM
3240_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
f6c52c13 3243{
fcb93ecf
PB
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
f6c52c13
AM
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
d95edcac
L
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
0fad2956
MR
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
7e2294f9
AO
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
f6c52c13 3264 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
f6c52c13
AM
3267 return FALSE;
3268
0fad2956 3269 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
0e1862bb 3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
fcb93ecf
PB
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
f7483970
L
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
889c2a67
L
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
1c16dfa5
L
3296 return TRUE;
3297
f6c52c13 3298 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3303 return local_protected;
3304}
e1918d23
AM
3305
3306/* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309struct bfd_section *
3310_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311{
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
fdde2fb6
SH
3326 /* Ensure the alignment of the first section (usually .tdata) is the largest
3327 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332}
0ad989f9
L
3333
3334/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335static bfd_boolean
3336is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338{
a4d8e49b
L
3339 const struct elf_backend_data *bed;
3340
0ad989f9
L
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
fcb93ecf 3346 bed = get_elf_backend_data (abfd);
0ad989f9 3347 /* Function symbols do not count. */
fcb93ecf 3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
a4d8e49b 3357 if (bed->common_definition (sym))
0ad989f9
L
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373}
3374
3375/* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378static bfd_boolean
3379elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380{
3381 Elf_Internal_Shdr * hdr;
ef53be89
AM
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
0ad989f9
L
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
7dc3990e
L
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3401#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404#endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462}
3463\f
5a580b3a
AM
3464/* Add an entry to the .dynamic table. */
3465
3466bfd_boolean
3467_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470{
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
7f923b7f
AM
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
5a580b3a 3485 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3487 BFD_ASSERT (s != NULL);
3488
eea6121a 3489 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
eea6121a 3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3497
eea6121a 3498 s->size = newsize;
5a580b3a
AM
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502}
3503
6f6fd151
L
3504/* Strip zero-sized dynamic sections. */
3505
3506bfd_boolean
3507_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3508{
3509 struct elf_link_hash_table *hash_table;
3510 const struct elf_backend_data *bed;
3511 asection *s, *sdynamic, **pp;
3512 asection *rela_dyn, *rel_dyn;
3513 Elf_Internal_Dyn dyn;
3514 bfd_byte *extdyn, *next;
3515 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3516 bfd_boolean strip_zero_sized;
3517 bfd_boolean strip_zero_sized_plt;
3518
3519 if (bfd_link_relocatable (info))
3520 return TRUE;
3521
3522 hash_table = elf_hash_table (info);
3523 if (!is_elf_hash_table (hash_table))
3524 return FALSE;
3525
3526 if (!hash_table->dynobj)
3527 return TRUE;
3528
3529 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3530 if (!sdynamic)
3531 return TRUE;
3532
3533 bed = get_elf_backend_data (hash_table->dynobj);
3534 swap_dyn_in = bed->s->swap_dyn_in;
3535
3536 strip_zero_sized = FALSE;
3537 strip_zero_sized_plt = FALSE;
3538
3539 /* Strip zero-sized dynamic sections. */
3540 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3541 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3542 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3543 if (s->size == 0
3544 && (s == rela_dyn
3545 || s == rel_dyn
3546 || s == hash_table->srelplt->output_section
3547 || s == hash_table->splt->output_section))
3548 {
3549 *pp = s->next;
3550 info->output_bfd->section_count--;
3551 strip_zero_sized = TRUE;
3552 if (s == rela_dyn)
3553 s = rela_dyn;
3554 if (s == rel_dyn)
3555 s = rel_dyn;
3556 else if (s == hash_table->splt->output_section)
3557 {
3558 s = hash_table->splt;
3559 strip_zero_sized_plt = TRUE;
3560 }
3561 else
3562 s = hash_table->srelplt;
3563 s->flags |= SEC_EXCLUDE;
3564 s->output_section = bfd_abs_section_ptr;
3565 }
3566 else
3567 pp = &s->next;
3568
3569 if (strip_zero_sized_plt)
3570 for (extdyn = sdynamic->contents;
3571 extdyn < sdynamic->contents + sdynamic->size;
3572 extdyn = next)
3573 {
3574 next = extdyn + bed->s->sizeof_dyn;
3575 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3576 switch (dyn.d_tag)
3577 {
3578 default:
3579 break;
3580 case DT_JMPREL:
3581 case DT_PLTRELSZ:
3582 case DT_PLTREL:
3583 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3584 the procedure linkage table (the .plt section) has been
3585 removed. */
3586 memmove (extdyn, next,
3587 sdynamic->size - (next - sdynamic->contents));
3588 next = extdyn;
3589 }
3590 }
3591
3592 if (strip_zero_sized)
3593 {
3594 /* Regenerate program headers. */
3595 elf_seg_map (info->output_bfd) = NULL;
3596 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3597 }
3598
3599 return TRUE;
3600}
3601
e310298c 3602/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3603 1 if a DT_NEEDED tag already exists, and 0 on success. */
3604
e310298c
AM
3605int
3606bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3607{
3608 struct elf_link_hash_table *hash_table;
ef53be89 3609 size_t strindex;
e310298c 3610 const char *soname;
5a580b3a 3611
7e9f0867
AM
3612 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3613 return -1;
3614
5a580b3a 3615 hash_table = elf_hash_table (info);
e310298c 3616 soname = elf_dt_name (abfd);
5a580b3a 3617 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3618 if (strindex == (size_t) -1)
5a580b3a
AM
3619 return -1;
3620
02be4619 3621 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3622 {
3623 asection *sdyn;
3624 const struct elf_backend_data *bed;
3625 bfd_byte *extdyn;
3626
3627 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3628 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3629 if (sdyn != NULL)
3630 for (extdyn = sdyn->contents;
3631 extdyn < sdyn->contents + sdyn->size;
3632 extdyn += bed->s->sizeof_dyn)
3633 {
3634 Elf_Internal_Dyn dyn;
5a580b3a 3635
7e9f0867
AM
3636 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3637 if (dyn.d_tag == DT_NEEDED
3638 && dyn.d_un.d_val == strindex)
3639 {
3640 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3641 return 1;
3642 }
3643 }
5a580b3a
AM
3644 }
3645
e310298c
AM
3646 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3647 return -1;
7e9f0867 3648
e310298c
AM
3649 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3650 return -1;
5a580b3a
AM
3651
3652 return 0;
3653}
3654
7b15fa7a
AM
3655/* Return true if SONAME is on the needed list between NEEDED and STOP
3656 (or the end of list if STOP is NULL), and needed by a library that
3657 will be loaded. */
3658
010e5ae2 3659static bfd_boolean
7b15fa7a
AM
3660on_needed_list (const char *soname,
3661 struct bfd_link_needed_list *needed,
3662 struct bfd_link_needed_list *stop)
010e5ae2 3663{
7b15fa7a
AM
3664 struct bfd_link_needed_list *look;
3665 for (look = needed; look != stop; look = look->next)
3666 if (strcmp (soname, look->name) == 0
3667 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3668 /* If needed by a library that itself is not directly
3669 needed, recursively check whether that library is
3670 indirectly needed. Since we add DT_NEEDED entries to
3671 the end of the list, library dependencies appear after
3672 the library. Therefore search prior to the current
3673 LOOK, preventing possible infinite recursion. */
3674 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3675 return TRUE;
3676
3677 return FALSE;
3678}
3679
3a3f4bf7 3680/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3681static int
3682elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3683{
3684 const struct elf_link_hash_entry *h1;
3685 const struct elf_link_hash_entry *h2;
10b7e05b 3686 bfd_signed_vma vdiff;
3a3f4bf7
AM
3687 int sdiff;
3688 const char *n1;
3689 const char *n2;
5a580b3a
AM
3690
3691 h1 = *(const struct elf_link_hash_entry **) arg1;
3692 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3693 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3694 if (vdiff != 0)
3695 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3696
3697 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3698 if (sdiff != 0)
3699 return sdiff;
3700
3701 /* Sort so that sized symbols are selected over zero size symbols. */
3702 vdiff = h1->size - h2->size;
3703 if (vdiff != 0)
3704 return vdiff > 0 ? 1 : -1;
3705
3706 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3707 if (h1->type != h2->type)
3708 return h1->type - h2->type;
3709
3710 /* If symbols are properly sized and typed, and multiple strong
3711 aliases are not defined in a shared library by the user we
3712 shouldn't get here. Unfortunately linker script symbols like
3713 __bss_start sometimes match a user symbol defined at the start of
3714 .bss without proper size and type. We'd like to preference the
3715 user symbol over reserved system symbols. Sort on leading
3716 underscores. */
3717 n1 = h1->root.root.string;
3718 n2 = h2->root.root.string;
3719 while (*n1 == *n2)
10b7e05b 3720 {
3a3f4bf7
AM
3721 if (*n1 == 0)
3722 break;
3723 ++n1;
3724 ++n2;
10b7e05b 3725 }
3a3f4bf7
AM
3726 if (*n1 == '_')
3727 return -1;
3728 if (*n2 == '_')
3729 return 1;
3730
3731 /* Final sort on name selects user symbols like '_u' over reserved
3732 system symbols like '_Z' and also will avoid qsort instability. */
3733 return *n1 - *n2;
5a580b3a 3734}
4ad4eba5 3735
5a580b3a
AM
3736/* This function is used to adjust offsets into .dynstr for
3737 dynamic symbols. This is called via elf_link_hash_traverse. */
3738
3739static bfd_boolean
3740elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3741{
a50b1753 3742 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3743
5a580b3a
AM
3744 if (h->dynindx != -1)
3745 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3746 return TRUE;
3747}
3748
3749/* Assign string offsets in .dynstr, update all structures referencing
3750 them. */
3751
4ad4eba5
AM
3752static bfd_boolean
3753elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3754{
3755 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3756 struct elf_link_local_dynamic_entry *entry;
3757 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3758 bfd *dynobj = hash_table->dynobj;
3759 asection *sdyn;
3760 bfd_size_type size;
3761 const struct elf_backend_data *bed;
3762 bfd_byte *extdyn;
3763
3764 _bfd_elf_strtab_finalize (dynstr);
3765 size = _bfd_elf_strtab_size (dynstr);
3766
3767 bed = get_elf_backend_data (dynobj);
3d4d4302 3768 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3769 BFD_ASSERT (sdyn != NULL);
3770
3771 /* Update all .dynamic entries referencing .dynstr strings. */
3772 for (extdyn = sdyn->contents;
eea6121a 3773 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3774 extdyn += bed->s->sizeof_dyn)
3775 {
3776 Elf_Internal_Dyn dyn;
3777
3778 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3779 switch (dyn.d_tag)
3780 {
3781 case DT_STRSZ:
3782 dyn.d_un.d_val = size;
3783 break;
3784 case DT_NEEDED:
3785 case DT_SONAME:
3786 case DT_RPATH:
3787 case DT_RUNPATH:
3788 case DT_FILTER:
3789 case DT_AUXILIARY:
7ee314fa
AM
3790 case DT_AUDIT:
3791 case DT_DEPAUDIT:
5a580b3a
AM
3792 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3793 break;
3794 default:
3795 continue;
3796 }
3797 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3798 }
3799
3800 /* Now update local dynamic symbols. */
3801 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3802 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3803 entry->isym.st_name);
3804
3805 /* And the rest of dynamic symbols. */
3806 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3807
3808 /* Adjust version definitions. */
3809 if (elf_tdata (output_bfd)->cverdefs)
3810 {
3811 asection *s;
3812 bfd_byte *p;
ef53be89 3813 size_t i;
5a580b3a
AM
3814 Elf_Internal_Verdef def;
3815 Elf_Internal_Verdaux defaux;
3816
3d4d4302 3817 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3818 p = s->contents;
3819 do
3820 {
3821 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3822 &def);
3823 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3824 if (def.vd_aux != sizeof (Elf_External_Verdef))
3825 continue;
5a580b3a
AM
3826 for (i = 0; i < def.vd_cnt; ++i)
3827 {
3828 _bfd_elf_swap_verdaux_in (output_bfd,
3829 (Elf_External_Verdaux *) p, &defaux);
3830 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3831 defaux.vda_name);
3832 _bfd_elf_swap_verdaux_out (output_bfd,
3833 &defaux, (Elf_External_Verdaux *) p);
3834 p += sizeof (Elf_External_Verdaux);
3835 }
3836 }
3837 while (def.vd_next);
3838 }
3839
3840 /* Adjust version references. */
3841 if (elf_tdata (output_bfd)->verref)
3842 {
3843 asection *s;
3844 bfd_byte *p;
ef53be89 3845 size_t i;
5a580b3a
AM
3846 Elf_Internal_Verneed need;
3847 Elf_Internal_Vernaux needaux;
3848
3d4d4302 3849 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3850 p = s->contents;
3851 do
3852 {
3853 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3854 &need);
3855 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3856 _bfd_elf_swap_verneed_out (output_bfd, &need,
3857 (Elf_External_Verneed *) p);
3858 p += sizeof (Elf_External_Verneed);
3859 for (i = 0; i < need.vn_cnt; ++i)
3860 {
3861 _bfd_elf_swap_vernaux_in (output_bfd,
3862 (Elf_External_Vernaux *) p, &needaux);
3863 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3864 needaux.vna_name);
3865 _bfd_elf_swap_vernaux_out (output_bfd,
3866 &needaux,
3867 (Elf_External_Vernaux *) p);
3868 p += sizeof (Elf_External_Vernaux);
3869 }
3870 }
3871 while (need.vn_next);
3872 }
3873
3874 return TRUE;
3875}
3876\f
13285a1b
AM
3877/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3878 The default is to only match when the INPUT and OUTPUT are exactly
3879 the same target. */
3880
3881bfd_boolean
3882_bfd_elf_default_relocs_compatible (const bfd_target *input,
3883 const bfd_target *output)
3884{
3885 return input == output;
3886}
3887
3888/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3889 This version is used when different targets for the same architecture
3890 are virtually identical. */
3891
3892bfd_boolean
3893_bfd_elf_relocs_compatible (const bfd_target *input,
3894 const bfd_target *output)
3895{
3896 const struct elf_backend_data *obed, *ibed;
3897
3898 if (input == output)
3899 return TRUE;
3900
3901 ibed = xvec_get_elf_backend_data (input);
3902 obed = xvec_get_elf_backend_data (output);
3903
3904 if (ibed->arch != obed->arch)
3905 return FALSE;
3906
3907 /* If both backends are using this function, deem them compatible. */
3908 return ibed->relocs_compatible == obed->relocs_compatible;
3909}
3910
e5034e59
AM
3911/* Make a special call to the linker "notice" function to tell it that
3912 we are about to handle an as-needed lib, or have finished
1b786873 3913 processing the lib. */
e5034e59
AM
3914
3915bfd_boolean
3916_bfd_elf_notice_as_needed (bfd *ibfd,
3917 struct bfd_link_info *info,
3918 enum notice_asneeded_action act)
3919{
46135103 3920 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3921}
3922
d9689752
L
3923/* Check relocations an ELF object file. */
3924
3925bfd_boolean
3926_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3927{
3928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3929 struct elf_link_hash_table *htab = elf_hash_table (info);
3930
3931 /* If this object is the same format as the output object, and it is
3932 not a shared library, then let the backend look through the
3933 relocs.
3934
3935 This is required to build global offset table entries and to
3936 arrange for dynamic relocs. It is not required for the
3937 particular common case of linking non PIC code, even when linking
3938 against shared libraries, but unfortunately there is no way of
3939 knowing whether an object file has been compiled PIC or not.
3940 Looking through the relocs is not particularly time consuming.
3941 The problem is that we must either (1) keep the relocs in memory,
3942 which causes the linker to require additional runtime memory or
3943 (2) read the relocs twice from the input file, which wastes time.
3944 This would be a good case for using mmap.
3945
3946 I have no idea how to handle linking PIC code into a file of a
3947 different format. It probably can't be done. */
3948 if ((abfd->flags & DYNAMIC) == 0
3949 && is_elf_hash_table (htab)
3950 && bed->check_relocs != NULL
3951 && elf_object_id (abfd) == elf_hash_table_id (htab)
3952 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3953 {
3954 asection *o;
3955
3956 for (o = abfd->sections; o != NULL; o = o->next)
3957 {
3958 Elf_Internal_Rela *internal_relocs;
3959 bfd_boolean ok;
3960
5ce03cea 3961 /* Don't check relocations in excluded sections. */
d9689752 3962 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3963 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3964 || o->reloc_count == 0
3965 || ((info->strip == strip_all || info->strip == strip_debugger)
3966 && (o->flags & SEC_DEBUGGING) != 0)
3967 || bfd_is_abs_section (o->output_section))
3968 continue;
3969
3970 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3971 info->keep_memory);
3972 if (internal_relocs == NULL)
3973 return FALSE;
3974
3975 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3976
3977 if (elf_section_data (o)->relocs != internal_relocs)
3978 free (internal_relocs);
3979
3980 if (! ok)
3981 return FALSE;
3982 }
3983 }
3984
3985 return TRUE;
3986}
3987
4ad4eba5
AM
3988/* Add symbols from an ELF object file to the linker hash table. */
3989
3990static bfd_boolean
3991elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3992{
a0c402a5 3993 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3994 Elf_Internal_Shdr *hdr;
ef53be89
AM
3995 size_t symcount;
3996 size_t extsymcount;
3997 size_t extsymoff;
4ad4eba5
AM
3998 struct elf_link_hash_entry **sym_hash;
3999 bfd_boolean dynamic;
4000 Elf_External_Versym *extversym = NULL;
be22c732 4001 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4002 Elf_External_Versym *ever;
4003 struct elf_link_hash_entry *weaks;
4004 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4005 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4006 Elf_Internal_Sym *isymbuf = NULL;
4007 Elf_Internal_Sym *isym;
4008 Elf_Internal_Sym *isymend;
4009 const struct elf_backend_data *bed;
4010 bfd_boolean add_needed;
66eb6687 4011 struct elf_link_hash_table *htab;
66eb6687 4012 void *alloc_mark = NULL;
4f87808c
AM
4013 struct bfd_hash_entry **old_table = NULL;
4014 unsigned int old_size = 0;
4015 unsigned int old_count = 0;
66eb6687 4016 void *old_tab = NULL;
66eb6687
AM
4017 void *old_ent;
4018 struct bfd_link_hash_entry *old_undefs = NULL;
4019 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4020 void *old_strtab = NULL;
66eb6687 4021 size_t tabsize = 0;
db6a5d5f 4022 asection *s;
29a9f53e 4023 bfd_boolean just_syms;
4ad4eba5 4024
66eb6687 4025 htab = elf_hash_table (info);
4ad4eba5 4026 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4027
4028 if ((abfd->flags & DYNAMIC) == 0)
4029 dynamic = FALSE;
4030 else
4031 {
4032 dynamic = TRUE;
4033
4034 /* You can't use -r against a dynamic object. Also, there's no
4035 hope of using a dynamic object which does not exactly match
4036 the format of the output file. */
0e1862bb 4037 if (bfd_link_relocatable (info)
66eb6687 4038 || !is_elf_hash_table (htab)
f13a99db 4039 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4040 {
0e1862bb 4041 if (bfd_link_relocatable (info))
9a0789ec
NC
4042 bfd_set_error (bfd_error_invalid_operation);
4043 else
4044 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4045 goto error_return;
4046 }
4047 }
4048
a0c402a5
L
4049 ehdr = elf_elfheader (abfd);
4050 if (info->warn_alternate_em
4051 && bed->elf_machine_code != ehdr->e_machine
4052 && ((bed->elf_machine_alt1 != 0
4053 && ehdr->e_machine == bed->elf_machine_alt1)
4054 || (bed->elf_machine_alt2 != 0
4055 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4056 _bfd_error_handler
695344c0 4057 /* xgettext:c-format */
9793eb77 4058 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4059 ehdr->e_machine, abfd, bed->elf_machine_code);
4060
4ad4eba5
AM
4061 /* As a GNU extension, any input sections which are named
4062 .gnu.warning.SYMBOL are treated as warning symbols for the given
4063 symbol. This differs from .gnu.warning sections, which generate
4064 warnings when they are included in an output file. */
dd98f8d2 4065 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4066 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4067 {
db6a5d5f 4068 const char *name;
4ad4eba5 4069
fd361982 4070 name = bfd_section_name (s);
db6a5d5f 4071 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4072 {
db6a5d5f
AM
4073 char *msg;
4074 bfd_size_type sz;
4075
4076 name += sizeof ".gnu.warning." - 1;
4077
4078 /* If this is a shared object, then look up the symbol
4079 in the hash table. If it is there, and it is already
4080 been defined, then we will not be using the entry
4081 from this shared object, so we don't need to warn.
4082 FIXME: If we see the definition in a regular object
4083 later on, we will warn, but we shouldn't. The only
4084 fix is to keep track of what warnings we are supposed
4085 to emit, and then handle them all at the end of the
4086 link. */
4087 if (dynamic)
4ad4eba5 4088 {
db6a5d5f
AM
4089 struct elf_link_hash_entry *h;
4090
4091 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4092
4093 /* FIXME: What about bfd_link_hash_common? */
4094 if (h != NULL
4095 && (h->root.type == bfd_link_hash_defined
4096 || h->root.type == bfd_link_hash_defweak))
4097 continue;
4098 }
4ad4eba5 4099
db6a5d5f
AM
4100 sz = s->size;
4101 msg = (char *) bfd_alloc (abfd, sz + 1);
4102 if (msg == NULL)
4103 goto error_return;
4ad4eba5 4104
db6a5d5f
AM
4105 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4106 goto error_return;
4ad4eba5 4107
db6a5d5f 4108 msg[sz] = '\0';
4ad4eba5 4109
db6a5d5f
AM
4110 if (! (_bfd_generic_link_add_one_symbol
4111 (info, abfd, name, BSF_WARNING, s, 0, msg,
4112 FALSE, bed->collect, NULL)))
4113 goto error_return;
4ad4eba5 4114
0e1862bb 4115 if (bfd_link_executable (info))
db6a5d5f
AM
4116 {
4117 /* Clobber the section size so that the warning does
4118 not get copied into the output file. */
4119 s->size = 0;
11d2f718 4120
db6a5d5f
AM
4121 /* Also set SEC_EXCLUDE, so that symbols defined in
4122 the warning section don't get copied to the output. */
4123 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4124 }
4125 }
4126 }
4127
29a9f53e
L
4128 just_syms = ((s = abfd->sections) != NULL
4129 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4130
4ad4eba5
AM
4131 add_needed = TRUE;
4132 if (! dynamic)
4133 {
4134 /* If we are creating a shared library, create all the dynamic
4135 sections immediately. We need to attach them to something,
4136 so we attach them to this BFD, provided it is the right
bf89386a
L
4137 format and is not from ld --just-symbols. Always create the
4138 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4139 are no input BFD's of the same format as the output, we can't
4140 make a shared library. */
4141 if (!just_syms
bf89386a 4142 && (bfd_link_pic (info)
9c1d7a08 4143 || (!bfd_link_relocatable (info)
3c5fce9b 4144 && info->nointerp
9c1d7a08 4145 && (info->export_dynamic || info->dynamic)))
66eb6687 4146 && is_elf_hash_table (htab)
f13a99db 4147 && info->output_bfd->xvec == abfd->xvec
66eb6687 4148 && !htab->dynamic_sections_created)
4ad4eba5
AM
4149 {
4150 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4151 goto error_return;
4152 }
4153 }
66eb6687 4154 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4155 goto error_return;
4156 else
4157 {
4ad4eba5 4158 const char *soname = NULL;
7ee314fa 4159 char *audit = NULL;
4ad4eba5 4160 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4161 const Elf_Internal_Phdr *phdr;
e310298c 4162 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4163
4164 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4165 ld shouldn't allow it. */
29a9f53e 4166 if (just_syms)
92fd189d 4167 abort ();
4ad4eba5
AM
4168
4169 /* If this dynamic lib was specified on the command line with
4170 --as-needed in effect, then we don't want to add a DT_NEEDED
4171 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4172 in by another lib's DT_NEEDED. When --no-add-needed is used
4173 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4174 any dynamic library in DT_NEEDED tags in the dynamic lib at
4175 all. */
4176 add_needed = (elf_dyn_lib_class (abfd)
4177 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4178 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4179
4180 s = bfd_get_section_by_name (abfd, ".dynamic");
4181 if (s != NULL)
4182 {
4183 bfd_byte *dynbuf;
4184 bfd_byte *extdyn;
cb33740c 4185 unsigned int elfsec;
4ad4eba5
AM
4186 unsigned long shlink;
4187
eea6121a 4188 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4189 {
dc1e8a47 4190 error_free_dyn:
f8703194
L
4191 free (dynbuf);
4192 goto error_return;
4193 }
4ad4eba5
AM
4194
4195 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4196 if (elfsec == SHN_BAD)
4ad4eba5
AM
4197 goto error_free_dyn;
4198 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4199
4200 for (extdyn = dynbuf;
9bff840e 4201 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4202 extdyn += bed->s->sizeof_dyn)
4203 {
4204 Elf_Internal_Dyn dyn;
4205
4206 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4207 if (dyn.d_tag == DT_SONAME)
4208 {
4209 unsigned int tagv = dyn.d_un.d_val;
4210 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4211 if (soname == NULL)
4212 goto error_free_dyn;
4213 }
4214 if (dyn.d_tag == DT_NEEDED)
4215 {
4216 struct bfd_link_needed_list *n, **pn;
4217 char *fnm, *anm;
4218 unsigned int tagv = dyn.d_un.d_val;
986f0783 4219 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4220
a50b1753 4221 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4222 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4223 if (n == NULL || fnm == NULL)
4224 goto error_free_dyn;
4225 amt = strlen (fnm) + 1;
a50b1753 4226 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4227 if (anm == NULL)
4228 goto error_free_dyn;
4229 memcpy (anm, fnm, amt);
4230 n->name = anm;
4231 n->by = abfd;
4232 n->next = NULL;
66eb6687 4233 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4234 ;
4235 *pn = n;
4236 }
4237 if (dyn.d_tag == DT_RUNPATH)
4238 {
4239 struct bfd_link_needed_list *n, **pn;
4240 char *fnm, *anm;
4241 unsigned int tagv = dyn.d_un.d_val;
986f0783 4242 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4243
a50b1753 4244 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4245 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4246 if (n == NULL || fnm == NULL)
4247 goto error_free_dyn;
4248 amt = strlen (fnm) + 1;
a50b1753 4249 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4250 if (anm == NULL)
4251 goto error_free_dyn;
4252 memcpy (anm, fnm, amt);
4253 n->name = anm;
4254 n->by = abfd;
4255 n->next = NULL;
4256 for (pn = & runpath;
4257 *pn != NULL;
4258 pn = &(*pn)->next)
4259 ;
4260 *pn = n;
4261 }
4262 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4263 if (!runpath && dyn.d_tag == DT_RPATH)
4264 {
4265 struct bfd_link_needed_list *n, **pn;
4266 char *fnm, *anm;
4267 unsigned int tagv = dyn.d_un.d_val;
986f0783 4268 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4269
a50b1753 4270 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4271 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4272 if (n == NULL || fnm == NULL)
4273 goto error_free_dyn;
4274 amt = strlen (fnm) + 1;
a50b1753 4275 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4276 if (anm == NULL)
f8703194 4277 goto error_free_dyn;
4ad4eba5
AM
4278 memcpy (anm, fnm, amt);
4279 n->name = anm;
4280 n->by = abfd;
4281 n->next = NULL;
4282 for (pn = & rpath;
4283 *pn != NULL;
4284 pn = &(*pn)->next)
4285 ;
4286 *pn = n;
4287 }
7ee314fa
AM
4288 if (dyn.d_tag == DT_AUDIT)
4289 {
4290 unsigned int tagv = dyn.d_un.d_val;
4291 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4292 }
4ad4eba5
AM
4293 }
4294
4295 free (dynbuf);
4296 }
4297
4298 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4299 frees all more recently bfd_alloc'd blocks as well. */
4300 if (runpath)
4301 rpath = runpath;
4302
4303 if (rpath)
4304 {
4305 struct bfd_link_needed_list **pn;
66eb6687 4306 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4307 ;
4308 *pn = rpath;
4309 }
4310
9acc85a6
AM
4311 /* If we have a PT_GNU_RELRO program header, mark as read-only
4312 all sections contained fully therein. This makes relro
4313 shared library sections appear as they will at run-time. */
4314 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4315 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4316 if (phdr->p_type == PT_GNU_RELRO)
4317 {
4318 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4319 {
4320 unsigned int opb = bfd_octets_per_byte (abfd, s);
4321
4322 if ((s->flags & SEC_ALLOC) != 0
4323 && s->vma * opb >= phdr->p_vaddr
4324 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4325 s->flags |= SEC_READONLY;
4326 }
9acc85a6
AM
4327 break;
4328 }
4329
4ad4eba5
AM
4330 /* We do not want to include any of the sections in a dynamic
4331 object in the output file. We hack by simply clobbering the
4332 list of sections in the BFD. This could be handled more
4333 cleanly by, say, a new section flag; the existing
4334 SEC_NEVER_LOAD flag is not the one we want, because that one
4335 still implies that the section takes up space in the output
4336 file. */
4337 bfd_section_list_clear (abfd);
4338
4ad4eba5
AM
4339 /* Find the name to use in a DT_NEEDED entry that refers to this
4340 object. If the object has a DT_SONAME entry, we use it.
4341 Otherwise, if the generic linker stuck something in
4342 elf_dt_name, we use that. Otherwise, we just use the file
4343 name. */
4344 if (soname == NULL || *soname == '\0')
4345 {
4346 soname = elf_dt_name (abfd);
4347 if (soname == NULL || *soname == '\0')
4348 soname = bfd_get_filename (abfd);
4349 }
4350
4351 /* Save the SONAME because sometimes the linker emulation code
4352 will need to know it. */
4353 elf_dt_name (abfd) = soname;
4354
4ad4eba5
AM
4355 /* If we have already included this dynamic object in the
4356 link, just ignore it. There is no reason to include a
4357 particular dynamic object more than once. */
e310298c
AM
4358 for (loaded_lib = htab->dyn_loaded;
4359 loaded_lib != NULL;
4360 loaded_lib = loaded_lib->next)
4361 {
4362 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4363 return TRUE;
4364 }
4365
4366 /* Create dynamic sections for backends that require that be done
4367 before setup_gnu_properties. */
4368 if (add_needed
4369 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4370 return FALSE;
7ee314fa
AM
4371
4372 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4373 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4374 }
4375
4376 /* If this is a dynamic object, we always link against the .dynsym
4377 symbol table, not the .symtab symbol table. The dynamic linker
4378 will only see the .dynsym symbol table, so there is no reason to
4379 look at .symtab for a dynamic object. */
4380
4381 if (! dynamic || elf_dynsymtab (abfd) == 0)
4382 hdr = &elf_tdata (abfd)->symtab_hdr;
4383 else
4384 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4385
4386 symcount = hdr->sh_size / bed->s->sizeof_sym;
4387
4388 /* The sh_info field of the symtab header tells us where the
4389 external symbols start. We don't care about the local symbols at
4390 this point. */
4391 if (elf_bad_symtab (abfd))
4392 {
4393 extsymcount = symcount;
4394 extsymoff = 0;
4395 }
4396 else
4397 {
4398 extsymcount = symcount - hdr->sh_info;
4399 extsymoff = hdr->sh_info;
4400 }
4401
f45794cb 4402 sym_hash = elf_sym_hashes (abfd);
012b2306 4403 if (extsymcount != 0)
4ad4eba5
AM
4404 {
4405 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4406 NULL, NULL, NULL);
4407 if (isymbuf == NULL)
4408 goto error_return;
4409
4ad4eba5 4410 if (sym_hash == NULL)
012b2306
AM
4411 {
4412 /* We store a pointer to the hash table entry for each
4413 external symbol. */
986f0783 4414 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4415 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4416 if (sym_hash == NULL)
4417 goto error_free_sym;
4418 elf_sym_hashes (abfd) = sym_hash;
4419 }
4ad4eba5
AM
4420 }
4421
4422 if (dynamic)
4423 {
4424 /* Read in any version definitions. */
fc0e6df6
PB
4425 if (!_bfd_elf_slurp_version_tables (abfd,
4426 info->default_imported_symver))
4ad4eba5
AM
4427 goto error_free_sym;
4428
4429 /* Read in the symbol versions, but don't bother to convert them
4430 to internal format. */
4431 if (elf_dynversym (abfd) != 0)
4432 {
986f0783
AM
4433 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4434 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4435
2bb3687b
AM
4436 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4437 goto error_free_sym;
4438 extversym = (Elf_External_Versym *)
4439 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4440 if (extversym == NULL)
4441 goto error_free_sym;
986f0783 4442 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4443 }
4444 }
4445
66eb6687
AM
4446 /* If we are loading an as-needed shared lib, save the symbol table
4447 state before we start adding symbols. If the lib turns out
4448 to be unneeded, restore the state. */
4449 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4450 {
4451 unsigned int i;
4452 size_t entsize;
4453
4454 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4455 {
4456 struct bfd_hash_entry *p;
2de92251 4457 struct elf_link_hash_entry *h;
66eb6687
AM
4458
4459 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4460 {
4461 h = (struct elf_link_hash_entry *) p;
4462 entsize += htab->root.table.entsize;
4463 if (h->root.type == bfd_link_hash_warning)
4464 entsize += htab->root.table.entsize;
4465 }
66eb6687
AM
4466 }
4467
4468 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4469 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4470 if (old_tab == NULL)
4471 goto error_free_vers;
4472
4473 /* Remember the current objalloc pointer, so that all mem for
4474 symbols added can later be reclaimed. */
4475 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4476 if (alloc_mark == NULL)
4477 goto error_free_vers;
4478
5061a885
AM
4479 /* Make a special call to the linker "notice" function to
4480 tell it that we are about to handle an as-needed lib. */
e5034e59 4481 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4482 goto error_free_vers;
5061a885 4483
f45794cb
AM
4484 /* Clone the symbol table. Remember some pointers into the
4485 symbol table, and dynamic symbol count. */
4486 old_ent = (char *) old_tab + tabsize;
66eb6687 4487 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4488 old_undefs = htab->root.undefs;
4489 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4490 old_table = htab->root.table.table;
4491 old_size = htab->root.table.size;
4492 old_count = htab->root.table.count;
e310298c
AM
4493 old_strtab = NULL;
4494 if (htab->dynstr != NULL)
4495 {
4496 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4497 if (old_strtab == NULL)
4498 goto error_free_vers;
4499 }
66eb6687
AM
4500
4501 for (i = 0; i < htab->root.table.size; i++)
4502 {
4503 struct bfd_hash_entry *p;
2de92251 4504 struct elf_link_hash_entry *h;
66eb6687
AM
4505
4506 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4507 {
4508 memcpy (old_ent, p, htab->root.table.entsize);
4509 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4510 h = (struct elf_link_hash_entry *) p;
4511 if (h->root.type == bfd_link_hash_warning)
4512 {
4513 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4514 old_ent = (char *) old_ent + htab->root.table.entsize;
4515 }
66eb6687
AM
4516 }
4517 }
4518 }
4ad4eba5 4519
66eb6687 4520 weaks = NULL;
be22c732
NC
4521 if (extversym == NULL)
4522 ever = NULL;
4523 else if (extversym + extsymoff < extversym_end)
4524 ever = extversym + extsymoff;
4525 else
4526 {
4527 /* xgettext:c-format */
4528 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4529 abfd, (long) extsymoff,
4530 (long) (extversym_end - extversym) / sizeof (* extversym));
4531 bfd_set_error (bfd_error_bad_value);
4532 goto error_free_vers;
4533 }
4534
b4c555cf
ML
4535 if (!bfd_link_relocatable (info)
4536 && abfd->lto_slim_object)
cc5277b1
ML
4537 {
4538 _bfd_error_handler
4539 (_("%pB: plugin needed to handle lto object"), abfd);
4540 }
4541
4ad4eba5
AM
4542 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4543 isym < isymend;
4544 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4545 {
4546 int bind;
4547 bfd_vma value;
af44c138 4548 asection *sec, *new_sec;
4ad4eba5
AM
4549 flagword flags;
4550 const char *name;
4551 struct elf_link_hash_entry *h;
90c984fc 4552 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4553 bfd_boolean definition;
4554 bfd_boolean size_change_ok;
4555 bfd_boolean type_change_ok;
37a9e49a
L
4556 bfd_boolean new_weak;
4557 bfd_boolean old_weak;
4ad4eba5 4558 bfd_boolean override;
a4d8e49b 4559 bfd_boolean common;
97196564 4560 bfd_boolean discarded;
4ad4eba5 4561 unsigned int old_alignment;
4538d1c7 4562 unsigned int shindex;
4ad4eba5 4563 bfd *old_bfd;
6e33951e 4564 bfd_boolean matched;
4ad4eba5
AM
4565
4566 override = FALSE;
4567
4568 flags = BSF_NO_FLAGS;
4569 sec = NULL;
4570 value = isym->st_value;
a4d8e49b 4571 common = bed->common_definition (isym);
2980ccad
L
4572 if (common && info->inhibit_common_definition)
4573 {
4574 /* Treat common symbol as undefined for --no-define-common. */
4575 isym->st_shndx = SHN_UNDEF;
4576 common = FALSE;
4577 }
97196564 4578 discarded = FALSE;
4ad4eba5
AM
4579
4580 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4581 switch (bind)
4ad4eba5 4582 {
3e7a7d11 4583 case STB_LOCAL:
4ad4eba5
AM
4584 /* This should be impossible, since ELF requires that all
4585 global symbols follow all local symbols, and that sh_info
4586 point to the first global symbol. Unfortunately, Irix 5
4587 screws this up. */
fe3fef62
AM
4588 if (elf_bad_symtab (abfd))
4589 continue;
4590
4591 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4592 then we'll likely segfault on a NULL symbol hash if the
4593 symbol is ever referenced in relocations. */
4594 shindex = elf_elfheader (abfd)->e_shstrndx;
4595 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4596 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4597 " (>= sh_info of %lu)"),
4598 abfd, name, (long) (isym - isymbuf + extsymoff),
4599 (long) extsymoff);
4600
4601 /* Dynamic object relocations are not processed by ld, so
4602 ld won't run into the problem mentioned above. */
4603 if (dynamic)
4604 continue;
fe3fef62
AM
4605 bfd_set_error (bfd_error_bad_value);
4606 goto error_free_vers;
3e7a7d11
NC
4607
4608 case STB_GLOBAL:
a4d8e49b 4609 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4610 flags = BSF_GLOBAL;
3e7a7d11
NC
4611 break;
4612
4613 case STB_WEAK:
4614 flags = BSF_WEAK;
4615 break;
4616
4617 case STB_GNU_UNIQUE:
4618 flags = BSF_GNU_UNIQUE;
4619 break;
4620
4621 default:
4ad4eba5 4622 /* Leave it up to the processor backend. */
3e7a7d11 4623 break;
4ad4eba5
AM
4624 }
4625
4626 if (isym->st_shndx == SHN_UNDEF)
4627 sec = bfd_und_section_ptr;
cb33740c
AM
4628 else if (isym->st_shndx == SHN_ABS)
4629 sec = bfd_abs_section_ptr;
4630 else if (isym->st_shndx == SHN_COMMON)
4631 {
4632 sec = bfd_com_section_ptr;
4633 /* What ELF calls the size we call the value. What ELF
4634 calls the value we call the alignment. */
4635 value = isym->st_size;
4636 }
4637 else
4ad4eba5
AM
4638 {
4639 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4640 if (sec == NULL)
4641 sec = bfd_abs_section_ptr;
dbaa2011 4642 else if (discarded_section (sec))
529fcb95 4643 {
e5d08002
L
4644 /* Symbols from discarded section are undefined. We keep
4645 its visibility. */
529fcb95 4646 sec = bfd_und_section_ptr;
97196564 4647 discarded = TRUE;
529fcb95
PB
4648 isym->st_shndx = SHN_UNDEF;
4649 }
4ad4eba5
AM
4650 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4651 value -= sec->vma;
4652 }
4ad4eba5
AM
4653
4654 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4655 isym->st_name);
4656 if (name == NULL)
4657 goto error_free_vers;
4658
4659 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4660 && (abfd->flags & BFD_PLUGIN) != 0)
4661 {
4662 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4663
4664 if (xc == NULL)
4665 {
4666 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4667 | SEC_EXCLUDE);
4668 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4669 if (xc == NULL)
4670 goto error_free_vers;
4671 }
4672 sec = xc;
4673 }
4674 else if (isym->st_shndx == SHN_COMMON
4675 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4676 && !bfd_link_relocatable (info))
4ad4eba5
AM
4677 {
4678 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4679
4680 if (tcomm == NULL)
4681 {
02d00247
AM
4682 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4683 | SEC_LINKER_CREATED);
4684 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4685 if (tcomm == NULL)
4ad4eba5
AM
4686 goto error_free_vers;
4687 }
4688 sec = tcomm;
4689 }
66eb6687 4690 else if (bed->elf_add_symbol_hook)
4ad4eba5 4691 {
66eb6687
AM
4692 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4693 &sec, &value))
4ad4eba5
AM
4694 goto error_free_vers;
4695
4696 /* The hook function sets the name to NULL if this symbol
4697 should be skipped for some reason. */
4698 if (name == NULL)
4699 continue;
4700 }
4701
4702 /* Sanity check that all possibilities were handled. */
4703 if (sec == NULL)
4538d1c7 4704 abort ();
4ad4eba5 4705
191c0c42
AM
4706 /* Silently discard TLS symbols from --just-syms. There's
4707 no way to combine a static TLS block with a new TLS block
4708 for this executable. */
4709 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4710 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4711 continue;
4712
4ad4eba5
AM
4713 if (bfd_is_und_section (sec)
4714 || bfd_is_com_section (sec))
4715 definition = FALSE;
4716 else
4717 definition = TRUE;
4718
4719 size_change_ok = FALSE;
66eb6687 4720 type_change_ok = bed->type_change_ok;
37a9e49a 4721 old_weak = FALSE;
6e33951e 4722 matched = FALSE;
4ad4eba5
AM
4723 old_alignment = 0;
4724 old_bfd = NULL;
af44c138 4725 new_sec = sec;
4ad4eba5 4726
66eb6687 4727 if (is_elf_hash_table (htab))
4ad4eba5
AM
4728 {
4729 Elf_Internal_Versym iver;
4730 unsigned int vernum = 0;
4731 bfd_boolean skip;
4732
fc0e6df6 4733 if (ever == NULL)
4ad4eba5 4734 {
fc0e6df6
PB
4735 if (info->default_imported_symver)
4736 /* Use the default symbol version created earlier. */
4737 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4738 else
4739 iver.vs_vers = 0;
4740 }
be22c732
NC
4741 else if (ever >= extversym_end)
4742 {
4743 /* xgettext:c-format */
4744 _bfd_error_handler (_("%pB: not enough version information"),
4745 abfd);
4746 bfd_set_error (bfd_error_bad_value);
4747 goto error_free_vers;
4748 }
fc0e6df6
PB
4749 else
4750 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4751
4752 vernum = iver.vs_vers & VERSYM_VERSION;
4753
4754 /* If this is a hidden symbol, or if it is not version
4755 1, we append the version name to the symbol name.
cc86ff91
EB
4756 However, we do not modify a non-hidden absolute symbol
4757 if it is not a function, because it might be the version
4758 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4759 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4760 || (vernum > 1
4761 && (!bfd_is_abs_section (sec)
4762 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4763 {
4764 const char *verstr;
4765 size_t namelen, verlen, newlen;
4766 char *newname, *p;
4767
4768 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4769 {
fc0e6df6
PB
4770 if (vernum > elf_tdata (abfd)->cverdefs)
4771 verstr = NULL;
4772 else if (vernum > 1)
4773 verstr =
4774 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4775 else
4776 verstr = "";
4ad4eba5 4777
fc0e6df6 4778 if (verstr == NULL)
4ad4eba5 4779 {
4eca0228 4780 _bfd_error_handler
695344c0 4781 /* xgettext:c-format */
871b3ab2 4782 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4783 abfd, name, vernum,
4784 elf_tdata (abfd)->cverdefs);
4785 bfd_set_error (bfd_error_bad_value);
4786 goto error_free_vers;
4ad4eba5 4787 }
fc0e6df6
PB
4788 }
4789 else
4790 {
4791 /* We cannot simply test for the number of
4792 entries in the VERNEED section since the
4793 numbers for the needed versions do not start
4794 at 0. */
4795 Elf_Internal_Verneed *t;
4796
4797 verstr = NULL;
4798 for (t = elf_tdata (abfd)->verref;
4799 t != NULL;
4800 t = t->vn_nextref)
4ad4eba5 4801 {
fc0e6df6 4802 Elf_Internal_Vernaux *a;
4ad4eba5 4803
fc0e6df6
PB
4804 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4805 {
4806 if (a->vna_other == vernum)
4ad4eba5 4807 {
fc0e6df6
PB
4808 verstr = a->vna_nodename;
4809 break;
4ad4eba5 4810 }
4ad4eba5 4811 }
fc0e6df6
PB
4812 if (a != NULL)
4813 break;
4814 }
4815 if (verstr == NULL)
4816 {
4eca0228 4817 _bfd_error_handler
695344c0 4818 /* xgettext:c-format */
871b3ab2 4819 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4820 abfd, name, vernum);
4821 bfd_set_error (bfd_error_bad_value);
4822 goto error_free_vers;
4ad4eba5 4823 }
4ad4eba5 4824 }
fc0e6df6
PB
4825
4826 namelen = strlen (name);
4827 verlen = strlen (verstr);
4828 newlen = namelen + verlen + 2;
4829 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4830 && isym->st_shndx != SHN_UNDEF)
4831 ++newlen;
4832
a50b1753 4833 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4834 if (newname == NULL)
4835 goto error_free_vers;
4836 memcpy (newname, name, namelen);
4837 p = newname + namelen;
4838 *p++ = ELF_VER_CHR;
4839 /* If this is a defined non-hidden version symbol,
4840 we add another @ to the name. This indicates the
4841 default version of the symbol. */
4842 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4843 && isym->st_shndx != SHN_UNDEF)
4844 *p++ = ELF_VER_CHR;
4845 memcpy (p, verstr, verlen + 1);
4846
4847 name = newname;
4ad4eba5
AM
4848 }
4849
cd3416da
AM
4850 /* If this symbol has default visibility and the user has
4851 requested we not re-export it, then mark it as hidden. */
a0d49154 4852 if (!bfd_is_und_section (sec)
cd3416da 4853 && !dynamic
ce875075 4854 && abfd->no_export
cd3416da
AM
4855 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4856 isym->st_other = (STV_HIDDEN
4857 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4858
4f3fedcf
AM
4859 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4860 sym_hash, &old_bfd, &old_weak,
4861 &old_alignment, &skip, &override,
6e33951e
L
4862 &type_change_ok, &size_change_ok,
4863 &matched))
4ad4eba5
AM
4864 goto error_free_vers;
4865
4866 if (skip)
4867 continue;
4868
6e33951e
L
4869 /* Override a definition only if the new symbol matches the
4870 existing one. */
4871 if (override && matched)
4ad4eba5
AM
4872 definition = FALSE;
4873
4874 h = *sym_hash;
4875 while (h->root.type == bfd_link_hash_indirect
4876 || h->root.type == bfd_link_hash_warning)
4877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4878
4ad4eba5 4879 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4880 && vernum > 1
4881 && definition)
4882 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4883 }
4884
4885 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4886 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4887 (struct bfd_link_hash_entry **) sym_hash)))
4888 goto error_free_vers;
4889
4890 h = *sym_hash;
90c984fc
L
4891 /* We need to make sure that indirect symbol dynamic flags are
4892 updated. */
4893 hi = h;
4ad4eba5
AM
4894 while (h->root.type == bfd_link_hash_indirect
4895 || h->root.type == bfd_link_hash_warning)
4896 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4897
97196564
L
4898 /* Setting the index to -3 tells elf_link_output_extsym that
4899 this symbol is defined in a discarded section. */
4900 if (discarded)
4901 h->indx = -3;
4902
4ad4eba5
AM
4903 *sym_hash = h;
4904
37a9e49a 4905 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4906 if (dynamic
4907 && definition
37a9e49a 4908 && new_weak
fcb93ecf 4909 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4910 && is_elf_hash_table (htab)
60d67dc8 4911 && h->u.alias == NULL)
4ad4eba5
AM
4912 {
4913 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4914 a dynamic object, using the alias field. Later in this
4915 function we will set the alias field to the correct
4ad4eba5
AM
4916 value. We only put non-function symbols from dynamic
4917 objects on this list, because that happens to be the only
4918 time we need to know the normal symbol corresponding to a
4919 weak symbol, and the information is time consuming to
60d67dc8 4920 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4921 then this symbol was already defined by some previous
4922 dynamic object, and we will be using that previous
4923 definition anyhow. */
4924
60d67dc8 4925 h->u.alias = weaks;
4ad4eba5 4926 weaks = h;
4ad4eba5
AM
4927 }
4928
4929 /* Set the alignment of a common symbol. */
a4d8e49b 4930 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4931 && h->root.type == bfd_link_hash_common)
4932 {
4933 unsigned int align;
4934
a4d8e49b 4935 if (common)
af44c138
L
4936 align = bfd_log2 (isym->st_value);
4937 else
4938 {
4939 /* The new symbol is a common symbol in a shared object.
4940 We need to get the alignment from the section. */
4941 align = new_sec->alignment_power;
4942 }
595213d4 4943 if (align > old_alignment)
4ad4eba5
AM
4944 h->root.u.c.p->alignment_power = align;
4945 else
4946 h->root.u.c.p->alignment_power = old_alignment;
4947 }
4948
66eb6687 4949 if (is_elf_hash_table (htab))
4ad4eba5 4950 {
4f3fedcf
AM
4951 /* Set a flag in the hash table entry indicating the type of
4952 reference or definition we just found. A dynamic symbol
4953 is one which is referenced or defined by both a regular
4954 object and a shared object. */
4955 bfd_boolean dynsym = FALSE;
4956
4957 /* Plugin symbols aren't normal. Don't set def_regular or
4958 ref_regular for them, or make them dynamic. */
4959 if ((abfd->flags & BFD_PLUGIN) != 0)
4960 ;
4961 else if (! dynamic)
4962 {
4963 if (! definition)
4964 {
4965 h->ref_regular = 1;
4966 if (bind != STB_WEAK)
4967 h->ref_regular_nonweak = 1;
4968 }
4969 else
4970 {
4971 h->def_regular = 1;
4972 if (h->def_dynamic)
4973 {
4974 h->def_dynamic = 0;
4975 h->ref_dynamic = 1;
4976 }
4977 }
4978
4979 /* If the indirect symbol has been forced local, don't
4980 make the real symbol dynamic. */
4981 if ((h == hi || !hi->forced_local)
0e1862bb 4982 && (bfd_link_dll (info)
4f3fedcf
AM
4983 || h->def_dynamic
4984 || h->ref_dynamic))
4985 dynsym = TRUE;
4986 }
4987 else
4988 {
4989 if (! definition)
4990 {
4991 h->ref_dynamic = 1;
4992 hi->ref_dynamic = 1;
4993 }
4994 else
4995 {
4996 h->def_dynamic = 1;
4997 hi->def_dynamic = 1;
4998 }
4999
5000 /* If the indirect symbol has been forced local, don't
5001 make the real symbol dynamic. */
5002 if ((h == hi || !hi->forced_local)
5003 && (h->def_regular
5004 || h->ref_regular
60d67dc8
AM
5005 || (h->is_weakalias
5006 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
5007 dynsym = TRUE;
5008 }
5009
5010 /* Check to see if we need to add an indirect symbol for
5011 the default name. */
5012 if (definition
5013 || (!override && h->root.type == bfd_link_hash_common))
5014 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5015 sec, value, &old_bfd, &dynsym))
5016 goto error_free_vers;
4ad4eba5
AM
5017
5018 /* Check the alignment when a common symbol is involved. This
5019 can change when a common symbol is overridden by a normal
5020 definition or a common symbol is ignored due to the old
5021 normal definition. We need to make sure the maximum
5022 alignment is maintained. */
a4d8e49b 5023 if ((old_alignment || common)
4ad4eba5
AM
5024 && h->root.type != bfd_link_hash_common)
5025 {
5026 unsigned int common_align;
5027 unsigned int normal_align;
5028 unsigned int symbol_align;
5029 bfd *normal_bfd;
5030 bfd *common_bfd;
5031
3a81e825
AM
5032 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5033 || h->root.type == bfd_link_hash_defweak);
5034
4ad4eba5
AM
5035 symbol_align = ffs (h->root.u.def.value) - 1;
5036 if (h->root.u.def.section->owner != NULL
0616a280
AM
5037 && (h->root.u.def.section->owner->flags
5038 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5039 {
5040 normal_align = h->root.u.def.section->alignment_power;
5041 if (normal_align > symbol_align)
5042 normal_align = symbol_align;
5043 }
5044 else
5045 normal_align = symbol_align;
5046
5047 if (old_alignment)
5048 {
5049 common_align = old_alignment;
5050 common_bfd = old_bfd;
5051 normal_bfd = abfd;
5052 }
5053 else
5054 {
5055 common_align = bfd_log2 (isym->st_value);
5056 common_bfd = abfd;
5057 normal_bfd = old_bfd;
5058 }
5059
5060 if (normal_align < common_align)
d07676f8
NC
5061 {
5062 /* PR binutils/2735 */
5063 if (normal_bfd == NULL)
4eca0228 5064 _bfd_error_handler
695344c0 5065 /* xgettext:c-format */
9793eb77 5066 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5067 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5068 1 << common_align, name, common_bfd,
5069 1 << normal_align, h->root.u.def.section);
d07676f8 5070 else
4eca0228 5071 _bfd_error_handler
695344c0 5072 /* xgettext:c-format */
9793eb77 5073 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5074 " is smaller than %u in %pB"),
c08bb8dd
AM
5075 1 << normal_align, name, normal_bfd,
5076 1 << common_align, common_bfd);
d07676f8 5077 }
4ad4eba5
AM
5078 }
5079
83ad0046 5080 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5081 if (isym->st_size != 0
5082 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5083 && (definition || h->size == 0))
5084 {
83ad0046
L
5085 if (h->size != 0
5086 && h->size != isym->st_size
5087 && ! size_change_ok)
4eca0228 5088 _bfd_error_handler
695344c0 5089 /* xgettext:c-format */
9793eb77 5090 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5091 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5092 name, (uint64_t) h->size, old_bfd,
5093 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5094
5095 h->size = isym->st_size;
5096 }
5097
5098 /* If this is a common symbol, then we always want H->SIZE
5099 to be the size of the common symbol. The code just above
5100 won't fix the size if a common symbol becomes larger. We
5101 don't warn about a size change here, because that is
4f3fedcf 5102 covered by --warn-common. Allow changes between different
fcb93ecf 5103 function types. */
4ad4eba5
AM
5104 if (h->root.type == bfd_link_hash_common)
5105 h->size = h->root.u.c.size;
5106
5107 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5108 && ((definition && !new_weak)
5109 || (old_weak && h->root.type == bfd_link_hash_common)
5110 || h->type == STT_NOTYPE))
4ad4eba5 5111 {
2955ec4c
L
5112 unsigned int type = ELF_ST_TYPE (isym->st_info);
5113
5114 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5115 symbol. */
5116 if (type == STT_GNU_IFUNC
5117 && (abfd->flags & DYNAMIC) != 0)
5118 type = STT_FUNC;
4ad4eba5 5119
2955ec4c
L
5120 if (h->type != type)
5121 {
5122 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5123 /* xgettext:c-format */
4eca0228 5124 _bfd_error_handler
9793eb77 5125 (_("warning: type of symbol `%s' changed"
871b3ab2 5126 " from %d to %d in %pB"),
c08bb8dd 5127 name, h->type, type, abfd);
2955ec4c
L
5128
5129 h->type = type;
5130 }
4ad4eba5
AM
5131 }
5132
54ac0771 5133 /* Merge st_other field. */
b8417128 5134 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5135
c3df8c14 5136 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5137 if (definition
5138 && (sec->flags & SEC_DEBUGGING)
5139 && !bfd_link_relocatable (info))
c3df8c14
AM
5140 dynsym = FALSE;
5141
4f3fedcf
AM
5142 /* Nor should we make plugin symbols dynamic. */
5143 if ((abfd->flags & BFD_PLUGIN) != 0)
5144 dynsym = FALSE;
5145
35fc36a8 5146 if (definition)
35399224
L
5147 {
5148 h->target_internal = isym->st_target_internal;
5149 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5150 }
35fc36a8 5151
4ad4eba5
AM
5152 if (definition && !dynamic)
5153 {
5154 char *p = strchr (name, ELF_VER_CHR);
5155 if (p != NULL && p[1] != ELF_VER_CHR)
5156 {
5157 /* Queue non-default versions so that .symver x, x@FOO
5158 aliases can be checked. */
66eb6687 5159 if (!nondeflt_vers)
4ad4eba5 5160 {
986f0783
AM
5161 size_t amt = ((isymend - isym + 1)
5162 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5163 nondeflt_vers
5164 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5165 if (!nondeflt_vers)
5166 goto error_free_vers;
4ad4eba5 5167 }
66eb6687 5168 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5169 }
5170 }
5171
5172 if (dynsym && h->dynindx == -1)
5173 {
c152c796 5174 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5175 goto error_free_vers;
60d67dc8
AM
5176 if (h->is_weakalias
5177 && weakdef (h)->dynindx == -1)
4ad4eba5 5178 {
60d67dc8 5179 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5180 goto error_free_vers;
5181 }
5182 }
1f599d0e 5183 else if (h->dynindx != -1)
4ad4eba5
AM
5184 /* If the symbol already has a dynamic index, but
5185 visibility says it should not be visible, turn it into
5186 a local symbol. */
5187 switch (ELF_ST_VISIBILITY (h->other))
5188 {
5189 case STV_INTERNAL:
5190 case STV_HIDDEN:
5191 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5192 dynsym = FALSE;
5193 break;
5194 }
5195
aef28989
L
5196 /* Don't add DT_NEEDED for references from the dummy bfd nor
5197 for unmatched symbol. */
4ad4eba5 5198 if (!add_needed
aef28989 5199 && matched
4ad4eba5 5200 && definition
010e5ae2 5201 && ((dynsym
ffa9430d 5202 && h->ref_regular_nonweak
4f3fedcf
AM
5203 && (old_bfd == NULL
5204 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5205 || (h->ref_dynamic_nonweak
010e5ae2 5206 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5207 && !on_needed_list (elf_dt_name (abfd),
5208 htab->needed, NULL))))
4ad4eba5 5209 {
4ad4eba5
AM
5210 const char *soname = elf_dt_name (abfd);
5211
16e4ecc0
AM
5212 info->callbacks->minfo ("%!", soname, old_bfd,
5213 h->root.root.string);
5214
4ad4eba5
AM
5215 /* A symbol from a library loaded via DT_NEEDED of some
5216 other library is referenced by a regular object.
e56f61be 5217 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5218 --no-add-needed is used and the reference was not
5219 a weak one. */
4f3fedcf 5220 if (old_bfd != NULL
b918acf9 5221 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5222 {
4eca0228 5223 _bfd_error_handler
695344c0 5224 /* xgettext:c-format */
871b3ab2 5225 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5226 old_bfd, name);
ff5ac77b 5227 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5228 goto error_free_vers;
5229 }
5230
a50b1753 5231 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5232 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5233
e310298c
AM
5234 /* Create dynamic sections for backends that require
5235 that be done before setup_gnu_properties. */
5236 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5237 return FALSE;
4ad4eba5 5238 add_needed = TRUE;
4ad4eba5
AM
5239 }
5240 }
5241 }
5242
a83ef4d1
L
5243 if (info->lto_plugin_active
5244 && !bfd_link_relocatable (info)
5245 && (abfd->flags & BFD_PLUGIN) == 0
5246 && !just_syms
5247 && extsymcount)
5248 {
5249 int r_sym_shift;
5250
5251 if (bed->s->arch_size == 32)
5252 r_sym_shift = 8;
5253 else
5254 r_sym_shift = 32;
5255
5256 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5257 referenced in regular objects so that linker plugin will get
5258 the correct symbol resolution. */
5259
5260 sym_hash = elf_sym_hashes (abfd);
5261 for (s = abfd->sections; s != NULL; s = s->next)
5262 {
5263 Elf_Internal_Rela *internal_relocs;
5264 Elf_Internal_Rela *rel, *relend;
5265
5266 /* Don't check relocations in excluded sections. */
5267 if ((s->flags & SEC_RELOC) == 0
5268 || s->reloc_count == 0
5269 || (s->flags & SEC_EXCLUDE) != 0
5270 || ((info->strip == strip_all
5271 || info->strip == strip_debugger)
5272 && (s->flags & SEC_DEBUGGING) != 0))
5273 continue;
5274
5275 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5276 NULL,
5277 info->keep_memory);
5278 if (internal_relocs == NULL)
5279 goto error_free_vers;
5280
5281 rel = internal_relocs;
5282 relend = rel + s->reloc_count;
5283 for ( ; rel < relend; rel++)
5284 {
5285 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5286 struct elf_link_hash_entry *h;
5287
5288 /* Skip local symbols. */
5289 if (r_symndx < extsymoff)
5290 continue;
5291
5292 h = sym_hash[r_symndx - extsymoff];
5293 if (h != NULL)
5294 h->root.non_ir_ref_regular = 1;
5295 }
5296
5297 if (elf_section_data (s)->relocs != internal_relocs)
5298 free (internal_relocs);
5299 }
5300 }
5301
66eb6687
AM
5302 if (extversym != NULL)
5303 {
5304 free (extversym);
5305 extversym = NULL;
5306 }
5307
5308 if (isymbuf != NULL)
5309 {
5310 free (isymbuf);
5311 isymbuf = NULL;
5312 }
5313
5314 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5315 {
5316 unsigned int i;
5317
5318 /* Restore the symbol table. */
f45794cb
AM
5319 old_ent = (char *) old_tab + tabsize;
5320 memset (elf_sym_hashes (abfd), 0,
5321 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5322 htab->root.table.table = old_table;
5323 htab->root.table.size = old_size;
5324 htab->root.table.count = old_count;
66eb6687 5325 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5326 htab->root.undefs = old_undefs;
5327 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5328 if (htab->dynstr != NULL)
5329 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5330 free (old_strtab);
5331 old_strtab = NULL;
66eb6687
AM
5332 for (i = 0; i < htab->root.table.size; i++)
5333 {
5334 struct bfd_hash_entry *p;
5335 struct elf_link_hash_entry *h;
3e0882af
L
5336 bfd_size_type size;
5337 unsigned int alignment_power;
4070765b 5338 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5339
5340 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5341 {
5342 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5343 if (h->root.type == bfd_link_hash_warning)
5344 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5345
3e0882af
L
5346 /* Preserve the maximum alignment and size for common
5347 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5348 since it can still be loaded at run time by another
3e0882af
L
5349 dynamic lib. */
5350 if (h->root.type == bfd_link_hash_common)
5351 {
5352 size = h->root.u.c.size;
5353 alignment_power = h->root.u.c.p->alignment_power;
5354 }
5355 else
5356 {
5357 size = 0;
5358 alignment_power = 0;
5359 }
4070765b 5360 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5361 will be exported when the dynamic lib becomes needed
5362 in the second pass. */
4070765b 5363 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5364 memcpy (p, old_ent, htab->root.table.entsize);
5365 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5366 h = (struct elf_link_hash_entry *) p;
5367 if (h->root.type == bfd_link_hash_warning)
5368 {
5369 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5370 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5371 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5372 }
a4542f1b 5373 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5374 {
5375 if (size > h->root.u.c.size)
5376 h->root.u.c.size = size;
5377 if (alignment_power > h->root.u.c.p->alignment_power)
5378 h->root.u.c.p->alignment_power = alignment_power;
5379 }
4070765b 5380 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5381 }
5382 }
5383
5061a885
AM
5384 /* Make a special call to the linker "notice" function to
5385 tell it that symbols added for crefs may need to be removed. */
e5034e59 5386 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5387 goto error_free_vers;
5061a885 5388
66eb6687
AM
5389 free (old_tab);
5390 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5391 alloc_mark);
5392 if (nondeflt_vers != NULL)
5393 free (nondeflt_vers);
5394 return TRUE;
5395 }
2de92251 5396
66eb6687
AM
5397 if (old_tab != NULL)
5398 {
e5034e59 5399 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5400 goto error_free_vers;
66eb6687
AM
5401 free (old_tab);
5402 old_tab = NULL;
5403 }
5404
c6e8a9a8
L
5405 /* Now that all the symbols from this input file are created, if
5406 not performing a relocatable link, handle .symver foo, foo@BAR
5407 such that any relocs against foo become foo@BAR. */
0e1862bb 5408 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5409 {
ef53be89 5410 size_t cnt, symidx;
4ad4eba5
AM
5411
5412 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5413 {
5414 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5415 char *shortname, *p;
986f0783 5416 size_t amt;
4ad4eba5
AM
5417
5418 p = strchr (h->root.root.string, ELF_VER_CHR);
5419 if (p == NULL
5420 || (h->root.type != bfd_link_hash_defined
5421 && h->root.type != bfd_link_hash_defweak))
5422 continue;
5423
5424 amt = p - h->root.root.string;
a50b1753 5425 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5426 if (!shortname)
5427 goto error_free_vers;
4ad4eba5
AM
5428 memcpy (shortname, h->root.root.string, amt);
5429 shortname[amt] = '\0';
5430
5431 hi = (struct elf_link_hash_entry *)
66eb6687 5432 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5433 FALSE, FALSE, FALSE);
5434 if (hi != NULL
5435 && hi->root.type == h->root.type
5436 && hi->root.u.def.value == h->root.u.def.value
5437 && hi->root.u.def.section == h->root.u.def.section)
5438 {
5439 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5440 hi->root.type = bfd_link_hash_indirect;
5441 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5442 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5443 sym_hash = elf_sym_hashes (abfd);
5444 if (sym_hash)
5445 for (symidx = 0; symidx < extsymcount; ++symidx)
5446 if (sym_hash[symidx] == hi)
5447 {
5448 sym_hash[symidx] = h;
5449 break;
5450 }
5451 }
5452 free (shortname);
5453 }
5454 free (nondeflt_vers);
5455 nondeflt_vers = NULL;
5456 }
5457
60d67dc8 5458 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5459 symbols we found. The only way to do this is to search all the
5460 symbols. Since we only need the information for non functions in
5461 dynamic objects, that's the only time we actually put anything on
5462 the list WEAKS. We need this information so that if a regular
5463 object refers to a symbol defined weakly in a dynamic object, the
5464 real symbol in the dynamic object is also put in the dynamic
5465 symbols; we also must arrange for both symbols to point to the
5466 same memory location. We could handle the general case of symbol
5467 aliasing, but a general symbol alias can only be generated in
5468 assembler code, handling it correctly would be very time
5469 consuming, and other ELF linkers don't handle general aliasing
5470 either. */
5471 if (weaks != NULL)
5472 {
5473 struct elf_link_hash_entry **hpp;
5474 struct elf_link_hash_entry **hppend;
5475 struct elf_link_hash_entry **sorted_sym_hash;
5476 struct elf_link_hash_entry *h;
986f0783 5477 size_t sym_count, amt;
4ad4eba5
AM
5478
5479 /* Since we have to search the whole symbol list for each weak
5480 defined symbol, search time for N weak defined symbols will be
5481 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5482 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5483 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5484 if (sorted_sym_hash == NULL)
5485 goto error_return;
5486 sym_hash = sorted_sym_hash;
5487 hpp = elf_sym_hashes (abfd);
5488 hppend = hpp + extsymcount;
5489 sym_count = 0;
5490 for (; hpp < hppend; hpp++)
5491 {
5492 h = *hpp;
5493 if (h != NULL
5494 && h->root.type == bfd_link_hash_defined
fcb93ecf 5495 && !bed->is_function_type (h->type))
4ad4eba5
AM
5496 {
5497 *sym_hash = h;
5498 sym_hash++;
5499 sym_count++;
5500 }
5501 }
5502
3a3f4bf7 5503 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5504 elf_sort_symbol);
5505
5506 while (weaks != NULL)
5507 {
5508 struct elf_link_hash_entry *hlook;
5509 asection *slook;
5510 bfd_vma vlook;
ed54588d 5511 size_t i, j, idx = 0;
4ad4eba5
AM
5512
5513 hlook = weaks;
60d67dc8
AM
5514 weaks = hlook->u.alias;
5515 hlook->u.alias = NULL;
4ad4eba5 5516
e3e53eed
AM
5517 if (hlook->root.type != bfd_link_hash_defined
5518 && hlook->root.type != bfd_link_hash_defweak)
5519 continue;
5520
4ad4eba5
AM
5521 slook = hlook->root.u.def.section;
5522 vlook = hlook->root.u.def.value;
5523
4ad4eba5
AM
5524 i = 0;
5525 j = sym_count;
14160578 5526 while (i != j)
4ad4eba5
AM
5527 {
5528 bfd_signed_vma vdiff;
5529 idx = (i + j) / 2;
14160578 5530 h = sorted_sym_hash[idx];
4ad4eba5
AM
5531 vdiff = vlook - h->root.u.def.value;
5532 if (vdiff < 0)
5533 j = idx;
5534 else if (vdiff > 0)
5535 i = idx + 1;
5536 else
5537 {
d3435ae8 5538 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5539 if (sdiff < 0)
5540 j = idx;
5541 else if (sdiff > 0)
5542 i = idx + 1;
5543 else
14160578 5544 break;
4ad4eba5
AM
5545 }
5546 }
5547
5548 /* We didn't find a value/section match. */
14160578 5549 if (i == j)
4ad4eba5
AM
5550 continue;
5551
14160578
AM
5552 /* With multiple aliases, or when the weak symbol is already
5553 strongly defined, we have multiple matching symbols and
5554 the binary search above may land on any of them. Step
5555 one past the matching symbol(s). */
5556 while (++idx != j)
5557 {
5558 h = sorted_sym_hash[idx];
5559 if (h->root.u.def.section != slook
5560 || h->root.u.def.value != vlook)
5561 break;
5562 }
5563
5564 /* Now look back over the aliases. Since we sorted by size
5565 as well as value and section, we'll choose the one with
5566 the largest size. */
5567 while (idx-- != i)
4ad4eba5 5568 {
14160578 5569 h = sorted_sym_hash[idx];
4ad4eba5
AM
5570
5571 /* Stop if value or section doesn't match. */
14160578
AM
5572 if (h->root.u.def.section != slook
5573 || h->root.u.def.value != vlook)
4ad4eba5
AM
5574 break;
5575 else if (h != hlook)
5576 {
60d67dc8
AM
5577 struct elf_link_hash_entry *t;
5578
5579 hlook->u.alias = h;
5580 hlook->is_weakalias = 1;
5581 t = h;
5582 if (t->u.alias != NULL)
5583 while (t->u.alias != h)
5584 t = t->u.alias;
5585 t->u.alias = hlook;
4ad4eba5
AM
5586
5587 /* If the weak definition is in the list of dynamic
5588 symbols, make sure the real definition is put
5589 there as well. */
5590 if (hlook->dynindx != -1 && h->dynindx == -1)
5591 {
c152c796 5592 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5593 {
5594 err_free_sym_hash:
5595 free (sorted_sym_hash);
5596 goto error_return;
5597 }
4ad4eba5
AM
5598 }
5599
5600 /* If the real definition is in the list of dynamic
5601 symbols, make sure the weak definition is put
5602 there as well. If we don't do this, then the
5603 dynamic loader might not merge the entries for the
5604 real definition and the weak definition. */
5605 if (h->dynindx != -1 && hlook->dynindx == -1)
5606 {
c152c796 5607 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5608 goto err_free_sym_hash;
4ad4eba5
AM
5609 }
5610 break;
5611 }
5612 }
5613 }
5614
5615 free (sorted_sym_hash);
5616 }
5617
33177bb1
AM
5618 if (bed->check_directives
5619 && !(*bed->check_directives) (abfd, info))
5620 return FALSE;
85fbca6a 5621
4ad4eba5
AM
5622 /* If this is a non-traditional link, try to optimize the handling
5623 of the .stab/.stabstr sections. */
5624 if (! dynamic
5625 && ! info->traditional_format
66eb6687 5626 && is_elf_hash_table (htab)
4ad4eba5
AM
5627 && (info->strip != strip_all && info->strip != strip_debugger))
5628 {
5629 asection *stabstr;
5630
5631 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5632 if (stabstr != NULL)
5633 {
5634 bfd_size_type string_offset = 0;
5635 asection *stab;
5636
5637 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5638 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5639 && (!stab->name[5] ||
5640 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5641 && (stab->flags & SEC_MERGE) == 0
5642 && !bfd_is_abs_section (stab->output_section))
5643 {
5644 struct bfd_elf_section_data *secdata;
5645
5646 secdata = elf_section_data (stab);
66eb6687
AM
5647 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5648 stabstr, &secdata->sec_info,
4ad4eba5
AM
5649 &string_offset))
5650 goto error_return;
5651 if (secdata->sec_info)
dbaa2011 5652 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5653 }
5654 }
5655 }
5656
e310298c 5657 if (dynamic && add_needed)
4ad4eba5
AM
5658 {
5659 /* Add this bfd to the loaded list. */
5660 struct elf_link_loaded_list *n;
5661
ca4be51c 5662 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5663 if (n == NULL)
5664 goto error_return;
5665 n->abfd = abfd;
e310298c
AM
5666 n->next = htab->dyn_loaded;
5667 htab->dyn_loaded = n;
4ad4eba5 5668 }
e310298c
AM
5669 if (dynamic && !add_needed
5670 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5671 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5672
5673 return TRUE;
5674
5675 error_free_vers:
66eb6687
AM
5676 if (old_tab != NULL)
5677 free (old_tab);
5b677558
AM
5678 if (old_strtab != NULL)
5679 free (old_strtab);
4ad4eba5
AM
5680 if (nondeflt_vers != NULL)
5681 free (nondeflt_vers);
5682 if (extversym != NULL)
5683 free (extversym);
5684 error_free_sym:
5685 if (isymbuf != NULL)
5686 free (isymbuf);
5687 error_return:
5688 return FALSE;
5689}
5690
8387904d
AM
5691/* Return the linker hash table entry of a symbol that might be
5692 satisfied by an archive symbol. Return -1 on error. */
5693
5694struct elf_link_hash_entry *
5695_bfd_elf_archive_symbol_lookup (bfd *abfd,
5696 struct bfd_link_info *info,
5697 const char *name)
5698{
5699 struct elf_link_hash_entry *h;
5700 char *p, *copy;
5701 size_t len, first;
5702
2a41f396 5703 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5704 if (h != NULL)
5705 return h;
5706
5707 /* If this is a default version (the name contains @@), look up the
5708 symbol again with only one `@' as well as without the version.
5709 The effect is that references to the symbol with and without the
5710 version will be matched by the default symbol in the archive. */
5711
5712 p = strchr (name, ELF_VER_CHR);
5713 if (p == NULL || p[1] != ELF_VER_CHR)
5714 return h;
5715
5716 /* First check with only one `@'. */
5717 len = strlen (name);
a50b1753 5718 copy = (char *) bfd_alloc (abfd, len);
8387904d 5719 if (copy == NULL)
e99955cd 5720 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5721
5722 first = p - name + 1;
5723 memcpy (copy, name, first);
5724 memcpy (copy + first, name + first + 1, len - first);
5725
2a41f396 5726 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5727 if (h == NULL)
5728 {
5729 /* We also need to check references to the symbol without the
5730 version. */
5731 copy[first - 1] = '\0';
5732 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5733 FALSE, FALSE, TRUE);
8387904d
AM
5734 }
5735
5736 bfd_release (abfd, copy);
5737 return h;
5738}
5739
0ad989f9 5740/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5741 don't use _bfd_generic_link_add_archive_symbols because we need to
5742 handle versioned symbols.
0ad989f9
L
5743
5744 Fortunately, ELF archive handling is simpler than that done by
5745 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5746 oddities. In ELF, if we find a symbol in the archive map, and the
5747 symbol is currently undefined, we know that we must pull in that
5748 object file.
5749
5750 Unfortunately, we do have to make multiple passes over the symbol
5751 table until nothing further is resolved. */
5752
4ad4eba5
AM
5753static bfd_boolean
5754elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5755{
5756 symindex c;
13e570f8 5757 unsigned char *included = NULL;
0ad989f9
L
5758 carsym *symdefs;
5759 bfd_boolean loop;
986f0783 5760 size_t amt;
8387904d
AM
5761 const struct elf_backend_data *bed;
5762 struct elf_link_hash_entry * (*archive_symbol_lookup)
5763 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5764
5765 if (! bfd_has_map (abfd))
5766 {
5767 /* An empty archive is a special case. */
5768 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5769 return TRUE;
5770 bfd_set_error (bfd_error_no_armap);
5771 return FALSE;
5772 }
5773
5774 /* Keep track of all symbols we know to be already defined, and all
5775 files we know to be already included. This is to speed up the
5776 second and subsequent passes. */
5777 c = bfd_ardata (abfd)->symdef_count;
5778 if (c == 0)
5779 return TRUE;
986f0783 5780 amt = c * sizeof (*included);
13e570f8
AM
5781 included = (unsigned char *) bfd_zmalloc (amt);
5782 if (included == NULL)
5783 return FALSE;
0ad989f9
L
5784
5785 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5786 bed = get_elf_backend_data (abfd);
5787 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5788
5789 do
5790 {
5791 file_ptr last;
5792 symindex i;
5793 carsym *symdef;
5794 carsym *symdefend;
5795
5796 loop = FALSE;
5797 last = -1;
5798
5799 symdef = symdefs;
5800 symdefend = symdef + c;
5801 for (i = 0; symdef < symdefend; symdef++, i++)
5802 {
5803 struct elf_link_hash_entry *h;
5804 bfd *element;
5805 struct bfd_link_hash_entry *undefs_tail;
5806 symindex mark;
5807
13e570f8 5808 if (included[i])
0ad989f9
L
5809 continue;
5810 if (symdef->file_offset == last)
5811 {
5812 included[i] = TRUE;
5813 continue;
5814 }
5815
8387904d 5816 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5817 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5818 goto error_return;
0ad989f9
L
5819
5820 if (h == NULL)
5821 continue;
5822
5823 if (h->root.type == bfd_link_hash_common)
5824 {
5825 /* We currently have a common symbol. The archive map contains
5826 a reference to this symbol, so we may want to include it. We
5827 only want to include it however, if this archive element
5828 contains a definition of the symbol, not just another common
5829 declaration of it.
5830
5831 Unfortunately some archivers (including GNU ar) will put
5832 declarations of common symbols into their archive maps, as
5833 well as real definitions, so we cannot just go by the archive
5834 map alone. Instead we must read in the element's symbol
5835 table and check that to see what kind of symbol definition
5836 this is. */
5837 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5838 continue;
5839 }
5840 else if (h->root.type != bfd_link_hash_undefined)
5841 {
5842 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5843 /* Symbol must be defined. Don't check it again. */
5844 included[i] = TRUE;
0ad989f9
L
5845 continue;
5846 }
5847
5848 /* We need to include this archive member. */
5849 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5850 if (element == NULL)
5851 goto error_return;
5852
5853 if (! bfd_check_format (element, bfd_object))
5854 goto error_return;
5855
0ad989f9
L
5856 undefs_tail = info->hash->undefs_tail;
5857
0e144ba7
AM
5858 if (!(*info->callbacks
5859 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5860 continue;
0e144ba7 5861 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5862 goto error_return;
5863
5864 /* If there are any new undefined symbols, we need to make
5865 another pass through the archive in order to see whether
5866 they can be defined. FIXME: This isn't perfect, because
5867 common symbols wind up on undefs_tail and because an
5868 undefined symbol which is defined later on in this pass
5869 does not require another pass. This isn't a bug, but it
5870 does make the code less efficient than it could be. */
5871 if (undefs_tail != info->hash->undefs_tail)
5872 loop = TRUE;
5873
5874 /* Look backward to mark all symbols from this object file
5875 which we have already seen in this pass. */
5876 mark = i;
5877 do
5878 {
5879 included[mark] = TRUE;
5880 if (mark == 0)
5881 break;
5882 --mark;
5883 }
5884 while (symdefs[mark].file_offset == symdef->file_offset);
5885
5886 /* We mark subsequent symbols from this object file as we go
5887 on through the loop. */
5888 last = symdef->file_offset;
5889 }
5890 }
5891 while (loop);
5892
0ad989f9
L
5893 free (included);
5894
5895 return TRUE;
5896
5897 error_return:
0ad989f9
L
5898 if (included != NULL)
5899 free (included);
5900 return FALSE;
5901}
4ad4eba5
AM
5902
5903/* Given an ELF BFD, add symbols to the global hash table as
5904 appropriate. */
5905
5906bfd_boolean
5907bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5908{
5909 switch (bfd_get_format (abfd))
5910 {
5911 case bfd_object:
5912 return elf_link_add_object_symbols (abfd, info);
5913 case bfd_archive:
5914 return elf_link_add_archive_symbols (abfd, info);
5915 default:
5916 bfd_set_error (bfd_error_wrong_format);
5917 return FALSE;
5918 }
5919}
5a580b3a 5920\f
14b1c01e
AM
5921struct hash_codes_info
5922{
5923 unsigned long *hashcodes;
5924 bfd_boolean error;
5925};
a0c8462f 5926
5a580b3a
AM
5927/* This function will be called though elf_link_hash_traverse to store
5928 all hash value of the exported symbols in an array. */
5929
5930static bfd_boolean
5931elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5932{
a50b1753 5933 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5934 const char *name;
5a580b3a
AM
5935 unsigned long ha;
5936 char *alc = NULL;
5937
5a580b3a
AM
5938 /* Ignore indirect symbols. These are added by the versioning code. */
5939 if (h->dynindx == -1)
5940 return TRUE;
5941
5942 name = h->root.root.string;
422f1182 5943 if (h->versioned >= versioned)
5a580b3a 5944 {
422f1182
L
5945 char *p = strchr (name, ELF_VER_CHR);
5946 if (p != NULL)
14b1c01e 5947 {
422f1182
L
5948 alc = (char *) bfd_malloc (p - name + 1);
5949 if (alc == NULL)
5950 {
5951 inf->error = TRUE;
5952 return FALSE;
5953 }
5954 memcpy (alc, name, p - name);
5955 alc[p - name] = '\0';
5956 name = alc;
14b1c01e 5957 }
5a580b3a
AM
5958 }
5959
5960 /* Compute the hash value. */
5961 ha = bfd_elf_hash (name);
5962
5963 /* Store the found hash value in the array given as the argument. */
14b1c01e 5964 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5965
5966 /* And store it in the struct so that we can put it in the hash table
5967 later. */
f6e332e6 5968 h->u.elf_hash_value = ha;
5a580b3a
AM
5969
5970 if (alc != NULL)
5971 free (alc);
5972
5973 return TRUE;
5974}
5975
fdc90cb4
JJ
5976struct collect_gnu_hash_codes
5977{
5978 bfd *output_bfd;
5979 const struct elf_backend_data *bed;
5980 unsigned long int nsyms;
5981 unsigned long int maskbits;
5982 unsigned long int *hashcodes;
5983 unsigned long int *hashval;
5984 unsigned long int *indx;
5985 unsigned long int *counts;
5986 bfd_vma *bitmask;
5987 bfd_byte *contents;
f16a9783 5988 bfd_size_type xlat;
fdc90cb4
JJ
5989 long int min_dynindx;
5990 unsigned long int bucketcount;
5991 unsigned long int symindx;
5992 long int local_indx;
5993 long int shift1, shift2;
5994 unsigned long int mask;
14b1c01e 5995 bfd_boolean error;
fdc90cb4
JJ
5996};
5997
5998/* This function will be called though elf_link_hash_traverse to store
5999 all hash value of the exported symbols in an array. */
6000
6001static bfd_boolean
6002elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6003{
a50b1753 6004 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6005 const char *name;
fdc90cb4
JJ
6006 unsigned long ha;
6007 char *alc = NULL;
6008
fdc90cb4
JJ
6009 /* Ignore indirect symbols. These are added by the versioning code. */
6010 if (h->dynindx == -1)
6011 return TRUE;
6012
6013 /* Ignore also local symbols and undefined symbols. */
6014 if (! (*s->bed->elf_hash_symbol) (h))
6015 return TRUE;
6016
6017 name = h->root.root.string;
422f1182 6018 if (h->versioned >= versioned)
fdc90cb4 6019 {
422f1182
L
6020 char *p = strchr (name, ELF_VER_CHR);
6021 if (p != NULL)
14b1c01e 6022 {
422f1182
L
6023 alc = (char *) bfd_malloc (p - name + 1);
6024 if (alc == NULL)
6025 {
6026 s->error = TRUE;
6027 return FALSE;
6028 }
6029 memcpy (alc, name, p - name);
6030 alc[p - name] = '\0';
6031 name = alc;
14b1c01e 6032 }
fdc90cb4
JJ
6033 }
6034
6035 /* Compute the hash value. */
6036 ha = bfd_elf_gnu_hash (name);
6037
6038 /* Store the found hash value in the array for compute_bucket_count,
6039 and also for .dynsym reordering purposes. */
6040 s->hashcodes[s->nsyms] = ha;
6041 s->hashval[h->dynindx] = ha;
6042 ++s->nsyms;
6043 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6044 s->min_dynindx = h->dynindx;
6045
6046 if (alc != NULL)
6047 free (alc);
6048
6049 return TRUE;
6050}
6051
6052/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6053 final dynamic symbol renumbering in case of .gnu.hash.
6054 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6055 to the translation table. */
fdc90cb4
JJ
6056
6057static bfd_boolean
f16a9783 6058elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6059{
a50b1753 6060 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6061 unsigned long int bucket;
6062 unsigned long int val;
6063
fdc90cb4
JJ
6064 /* Ignore indirect symbols. */
6065 if (h->dynindx == -1)
6066 return TRUE;
6067
6068 /* Ignore also local symbols and undefined symbols. */
6069 if (! (*s->bed->elf_hash_symbol) (h))
6070 {
6071 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6072 {
6073 if (s->bed->record_xhash_symbol != NULL)
6074 {
6075 (*s->bed->record_xhash_symbol) (h, 0);
6076 s->local_indx++;
6077 }
6078 else
6079 h->dynindx = s->local_indx++;
6080 }
fdc90cb4
JJ
6081 return TRUE;
6082 }
6083
6084 bucket = s->hashval[h->dynindx] % s->bucketcount;
6085 val = (s->hashval[h->dynindx] >> s->shift1)
6086 & ((s->maskbits >> s->shift1) - 1);
6087 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6088 s->bitmask[val]
6089 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6090 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6091 if (s->counts[bucket] == 1)
6092 /* Last element terminates the chain. */
6093 val |= 1;
6094 bfd_put_32 (s->output_bfd, val,
6095 s->contents + (s->indx[bucket] - s->symindx) * 4);
6096 --s->counts[bucket];
f16a9783
MS
6097 if (s->bed->record_xhash_symbol != NULL)
6098 {
6099 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6100
6101 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6102 }
6103 else
6104 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6105 return TRUE;
6106}
6107
6108/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6109
6110bfd_boolean
6111_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6112{
6113 return !(h->forced_local
6114 || h->root.type == bfd_link_hash_undefined
6115 || h->root.type == bfd_link_hash_undefweak
6116 || ((h->root.type == bfd_link_hash_defined
6117 || h->root.type == bfd_link_hash_defweak)
6118 && h->root.u.def.section->output_section == NULL));
6119}
6120
5a580b3a
AM
6121/* Array used to determine the number of hash table buckets to use
6122 based on the number of symbols there are. If there are fewer than
6123 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6124 fewer than 37 we use 17 buckets, and so forth. We never use more
6125 than 32771 buckets. */
6126
6127static const size_t elf_buckets[] =
6128{
6129 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6130 16411, 32771, 0
6131};
6132
6133/* Compute bucket count for hashing table. We do not use a static set
6134 of possible tables sizes anymore. Instead we determine for all
6135 possible reasonable sizes of the table the outcome (i.e., the
6136 number of collisions etc) and choose the best solution. The
6137 weighting functions are not too simple to allow the table to grow
6138 without bounds. Instead one of the weighting factors is the size.
6139 Therefore the result is always a good payoff between few collisions
6140 (= short chain lengths) and table size. */
6141static size_t
b20dd2ce 6142compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6143 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6144 unsigned long int nsyms,
6145 int gnu_hash)
5a580b3a 6146{
5a580b3a 6147 size_t best_size = 0;
5a580b3a 6148 unsigned long int i;
5a580b3a 6149
5a580b3a
AM
6150 /* We have a problem here. The following code to optimize the table
6151 size requires an integer type with more the 32 bits. If
6152 BFD_HOST_U_64_BIT is set we know about such a type. */
6153#ifdef BFD_HOST_U_64_BIT
6154 if (info->optimize)
6155 {
5a580b3a
AM
6156 size_t minsize;
6157 size_t maxsize;
6158 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6159 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6160 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6161 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6162 unsigned long int *counts;
d40f3da9 6163 bfd_size_type amt;
0883b6e0 6164 unsigned int no_improvement_count = 0;
5a580b3a
AM
6165
6166 /* Possible optimization parameters: if we have NSYMS symbols we say
6167 that the hashing table must at least have NSYMS/4 and at most
6168 2*NSYMS buckets. */
6169 minsize = nsyms / 4;
6170 if (minsize == 0)
6171 minsize = 1;
6172 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6173 if (gnu_hash)
6174 {
6175 if (minsize < 2)
6176 minsize = 2;
6177 if ((best_size & 31) == 0)
6178 ++best_size;
6179 }
5a580b3a
AM
6180
6181 /* Create array where we count the collisions in. We must use bfd_malloc
6182 since the size could be large. */
6183 amt = maxsize;
6184 amt *= sizeof (unsigned long int);
a50b1753 6185 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6186 if (counts == NULL)
fdc90cb4 6187 return 0;
5a580b3a
AM
6188
6189 /* Compute the "optimal" size for the hash table. The criteria is a
6190 minimal chain length. The minor criteria is (of course) the size
6191 of the table. */
6192 for (i = minsize; i < maxsize; ++i)
6193 {
6194 /* Walk through the array of hashcodes and count the collisions. */
6195 BFD_HOST_U_64_BIT max;
6196 unsigned long int j;
6197 unsigned long int fact;
6198
fdc90cb4
JJ
6199 if (gnu_hash && (i & 31) == 0)
6200 continue;
6201
5a580b3a
AM
6202 memset (counts, '\0', i * sizeof (unsigned long int));
6203
6204 /* Determine how often each hash bucket is used. */
6205 for (j = 0; j < nsyms; ++j)
6206 ++counts[hashcodes[j] % i];
6207
6208 /* For the weight function we need some information about the
6209 pagesize on the target. This is information need not be 100%
6210 accurate. Since this information is not available (so far) we
6211 define it here to a reasonable default value. If it is crucial
6212 to have a better value some day simply define this value. */
6213# ifndef BFD_TARGET_PAGESIZE
6214# define BFD_TARGET_PAGESIZE (4096)
6215# endif
6216
fdc90cb4
JJ
6217 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6218 and the chains. */
6219 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6220
6221# if 1
6222 /* Variant 1: optimize for short chains. We add the squares
6223 of all the chain lengths (which favors many small chain
6224 over a few long chains). */
6225 for (j = 0; j < i; ++j)
6226 max += counts[j] * counts[j];
6227
6228 /* This adds penalties for the overall size of the table. */
fdc90cb4 6229 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6230 max *= fact * fact;
6231# else
6232 /* Variant 2: Optimize a lot more for small table. Here we
6233 also add squares of the size but we also add penalties for
6234 empty slots (the +1 term). */
6235 for (j = 0; j < i; ++j)
6236 max += (1 + counts[j]) * (1 + counts[j]);
6237
6238 /* The overall size of the table is considered, but not as
6239 strong as in variant 1, where it is squared. */
fdc90cb4 6240 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6241 max *= fact;
6242# endif
6243
6244 /* Compare with current best results. */
6245 if (max < best_chlen)
6246 {
6247 best_chlen = max;
6248 best_size = i;
ca4be51c 6249 no_improvement_count = 0;
5a580b3a 6250 }
0883b6e0
NC
6251 /* PR 11843: Avoid futile long searches for the best bucket size
6252 when there are a large number of symbols. */
6253 else if (++no_improvement_count == 100)
6254 break;
5a580b3a
AM
6255 }
6256
6257 free (counts);
6258 }
6259 else
6260#endif /* defined (BFD_HOST_U_64_BIT) */
6261 {
6262 /* This is the fallback solution if no 64bit type is available or if we
6263 are not supposed to spend much time on optimizations. We select the
6264 bucket count using a fixed set of numbers. */
6265 for (i = 0; elf_buckets[i] != 0; i++)
6266 {
6267 best_size = elf_buckets[i];
fdc90cb4 6268 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6269 break;
6270 }
fdc90cb4
JJ
6271 if (gnu_hash && best_size < 2)
6272 best_size = 2;
5a580b3a
AM
6273 }
6274
5a580b3a
AM
6275 return best_size;
6276}
6277
d0bf826b
AM
6278/* Size any SHT_GROUP section for ld -r. */
6279
6280bfd_boolean
6281_bfd_elf_size_group_sections (struct bfd_link_info *info)
6282{
6283 bfd *ibfd;
57963c05 6284 asection *s;
d0bf826b 6285
c72f2fb2 6286 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6287 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6288 && (s = ibfd->sections) != NULL
6289 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6290 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6291 return FALSE;
6292 return TRUE;
6293}
6294
04c3a755
NS
6295/* Set a default stack segment size. The value in INFO wins. If it
6296 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6297 undefined it is initialized. */
6298
6299bfd_boolean
6300bfd_elf_stack_segment_size (bfd *output_bfd,
6301 struct bfd_link_info *info,
6302 const char *legacy_symbol,
6303 bfd_vma default_size)
6304{
6305 struct elf_link_hash_entry *h = NULL;
6306
6307 /* Look for legacy symbol. */
6308 if (legacy_symbol)
6309 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6310 FALSE, FALSE, FALSE);
6311 if (h && (h->root.type == bfd_link_hash_defined
6312 || h->root.type == bfd_link_hash_defweak)
6313 && h->def_regular
6314 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6315 {
6316 /* The symbol has no type if specified on the command line. */
6317 h->type = STT_OBJECT;
6318 if (info->stacksize)
695344c0 6319 /* xgettext:c-format */
871b3ab2 6320 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6321 output_bfd, legacy_symbol);
04c3a755 6322 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6323 /* xgettext:c-format */
871b3ab2 6324 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6325 output_bfd, legacy_symbol);
04c3a755
NS
6326 else
6327 info->stacksize = h->root.u.def.value;
6328 }
6329
6330 if (!info->stacksize)
6331 /* If the user didn't set a size, or explicitly inhibit the
6332 size, set it now. */
6333 info->stacksize = default_size;
6334
6335 /* Provide the legacy symbol, if it is referenced. */
6336 if (h && (h->root.type == bfd_link_hash_undefined
6337 || h->root.type == bfd_link_hash_undefweak))
6338 {
6339 struct bfd_link_hash_entry *bh = NULL;
6340
6341 if (!(_bfd_generic_link_add_one_symbol
6342 (info, output_bfd, legacy_symbol,
6343 BSF_GLOBAL, bfd_abs_section_ptr,
6344 info->stacksize >= 0 ? info->stacksize : 0,
6345 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6346 return FALSE;
6347
6348 h = (struct elf_link_hash_entry *) bh;
6349 h->def_regular = 1;
6350 h->type = STT_OBJECT;
6351 }
6352
6353 return TRUE;
6354}
6355
b531344c
MR
6356/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6357
6358struct elf_gc_sweep_symbol_info
6359{
6360 struct bfd_link_info *info;
6361 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6362 bfd_boolean);
6363};
6364
6365static bfd_boolean
6366elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6367{
6368 if (!h->mark
6369 && (((h->root.type == bfd_link_hash_defined
6370 || h->root.type == bfd_link_hash_defweak)
6371 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6372 && h->root.u.def.section->gc_mark))
6373 || h->root.type == bfd_link_hash_undefined
6374 || h->root.type == bfd_link_hash_undefweak))
6375 {
6376 struct elf_gc_sweep_symbol_info *inf;
6377
6378 inf = (struct elf_gc_sweep_symbol_info *) data;
6379 (*inf->hide_symbol) (inf->info, h, TRUE);
6380 h->def_regular = 0;
6381 h->ref_regular = 0;
6382 h->ref_regular_nonweak = 0;
6383 }
6384
6385 return TRUE;
6386}
6387
5a580b3a
AM
6388/* Set up the sizes and contents of the ELF dynamic sections. This is
6389 called by the ELF linker emulation before_allocation routine. We
6390 must set the sizes of the sections before the linker sets the
6391 addresses of the various sections. */
6392
6393bfd_boolean
6394bfd_elf_size_dynamic_sections (bfd *output_bfd,
6395 const char *soname,
6396 const char *rpath,
6397 const char *filter_shlib,
7ee314fa
AM
6398 const char *audit,
6399 const char *depaudit,
5a580b3a
AM
6400 const char * const *auxiliary_filters,
6401 struct bfd_link_info *info,
fd91d419 6402 asection **sinterpptr)
5a580b3a 6403{
5a580b3a
AM
6404 bfd *dynobj;
6405 const struct elf_backend_data *bed;
5a580b3a
AM
6406
6407 *sinterpptr = NULL;
6408
5a580b3a
AM
6409 if (!is_elf_hash_table (info->hash))
6410 return TRUE;
6411
5a580b3a
AM
6412 dynobj = elf_hash_table (info)->dynobj;
6413
9a2a56cc 6414 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6415 {
902e9fc7
MR
6416 struct bfd_elf_version_tree *verdefs;
6417 struct elf_info_failed asvinfo;
5a580b3a
AM
6418 struct bfd_elf_version_tree *t;
6419 struct bfd_elf_version_expr *d;
902e9fc7 6420 asection *s;
e6699019 6421 size_t soname_indx;
7ee314fa 6422
5a580b3a
AM
6423 /* If we are supposed to export all symbols into the dynamic symbol
6424 table (this is not the normal case), then do so. */
55255dae 6425 if (info->export_dynamic
0e1862bb 6426 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6427 {
3d13f3e9
AM
6428 struct elf_info_failed eif;
6429
6430 eif.info = info;
6431 eif.failed = FALSE;
5a580b3a
AM
6432 elf_link_hash_traverse (elf_hash_table (info),
6433 _bfd_elf_export_symbol,
6434 &eif);
6435 if (eif.failed)
6436 return FALSE;
6437 }
6438
e6699019
L
6439 if (soname != NULL)
6440 {
6441 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6442 soname, TRUE);
6443 if (soname_indx == (size_t) -1
6444 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6445 return FALSE;
6446 }
6447 else
6448 soname_indx = (size_t) -1;
6449
5a580b3a 6450 /* Make all global versions with definition. */
fd91d419 6451 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6452 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6453 if (!d->symver && d->literal)
5a580b3a
AM
6454 {
6455 const char *verstr, *name;
6456 size_t namelen, verlen, newlen;
93252b1c 6457 char *newname, *p, leading_char;
5a580b3a
AM
6458 struct elf_link_hash_entry *newh;
6459
93252b1c 6460 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6461 name = d->pattern;
93252b1c 6462 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6463 verstr = t->name;
6464 verlen = strlen (verstr);
6465 newlen = namelen + verlen + 3;
6466
a50b1753 6467 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6468 if (newname == NULL)
6469 return FALSE;
93252b1c
MF
6470 newname[0] = leading_char;
6471 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6472
6473 /* Check the hidden versioned definition. */
6474 p = newname + namelen;
6475 *p++ = ELF_VER_CHR;
6476 memcpy (p, verstr, verlen + 1);
6477 newh = elf_link_hash_lookup (elf_hash_table (info),
6478 newname, FALSE, FALSE,
6479 FALSE);
6480 if (newh == NULL
6481 || (newh->root.type != bfd_link_hash_defined
6482 && newh->root.type != bfd_link_hash_defweak))
6483 {
6484 /* Check the default versioned definition. */
6485 *p++ = ELF_VER_CHR;
6486 memcpy (p, verstr, verlen + 1);
6487 newh = elf_link_hash_lookup (elf_hash_table (info),
6488 newname, FALSE, FALSE,
6489 FALSE);
6490 }
6491 free (newname);
6492
6493 /* Mark this version if there is a definition and it is
6494 not defined in a shared object. */
6495 if (newh != NULL
f5385ebf 6496 && !newh->def_dynamic
5a580b3a
AM
6497 && (newh->root.type == bfd_link_hash_defined
6498 || newh->root.type == bfd_link_hash_defweak))
6499 d->symver = 1;
6500 }
6501
6502 /* Attach all the symbols to their version information. */
5a580b3a 6503 asvinfo.info = info;
5a580b3a
AM
6504 asvinfo.failed = FALSE;
6505
6506 elf_link_hash_traverse (elf_hash_table (info),
6507 _bfd_elf_link_assign_sym_version,
6508 &asvinfo);
6509 if (asvinfo.failed)
6510 return FALSE;
6511
6512 if (!info->allow_undefined_version)
6513 {
6514 /* Check if all global versions have a definition. */
3d13f3e9 6515 bfd_boolean all_defined = TRUE;
fd91d419 6516 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6517 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6518 if (d->literal && !d->symver && !d->script)
5a580b3a 6519 {
4eca0228 6520 _bfd_error_handler
5a580b3a
AM
6521 (_("%s: undefined version: %s"),
6522 d->pattern, t->name);
6523 all_defined = FALSE;
6524 }
6525
6526 if (!all_defined)
6527 {
6528 bfd_set_error (bfd_error_bad_value);
6529 return FALSE;
6530 }
6531 }
6532
902e9fc7
MR
6533 /* Set up the version definition section. */
6534 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6535 BFD_ASSERT (s != NULL);
5a580b3a 6536
902e9fc7
MR
6537 /* We may have created additional version definitions if we are
6538 just linking a regular application. */
6539 verdefs = info->version_info;
5a580b3a 6540
902e9fc7
MR
6541 /* Skip anonymous version tag. */
6542 if (verdefs != NULL && verdefs->vernum == 0)
6543 verdefs = verdefs->next;
5a580b3a 6544
902e9fc7
MR
6545 if (verdefs == NULL && !info->create_default_symver)
6546 s->flags |= SEC_EXCLUDE;
6547 else
5a580b3a 6548 {
902e9fc7
MR
6549 unsigned int cdefs;
6550 bfd_size_type size;
6551 bfd_byte *p;
6552 Elf_Internal_Verdef def;
6553 Elf_Internal_Verdaux defaux;
6554 struct bfd_link_hash_entry *bh;
6555 struct elf_link_hash_entry *h;
6556 const char *name;
5a580b3a 6557
902e9fc7
MR
6558 cdefs = 0;
6559 size = 0;
5a580b3a 6560
902e9fc7
MR
6561 /* Make space for the base version. */
6562 size += sizeof (Elf_External_Verdef);
6563 size += sizeof (Elf_External_Verdaux);
6564 ++cdefs;
6565
6566 /* Make space for the default version. */
6567 if (info->create_default_symver)
6568 {
6569 size += sizeof (Elf_External_Verdef);
6570 ++cdefs;
3e3b46e5
PB
6571 }
6572
5a580b3a
AM
6573 for (t = verdefs; t != NULL; t = t->next)
6574 {
6575 struct bfd_elf_version_deps *n;
6576
a6cc6b3b
RO
6577 /* Don't emit base version twice. */
6578 if (t->vernum == 0)
6579 continue;
6580
5a580b3a
AM
6581 size += sizeof (Elf_External_Verdef);
6582 size += sizeof (Elf_External_Verdaux);
6583 ++cdefs;
6584
6585 for (n = t->deps; n != NULL; n = n->next)
6586 size += sizeof (Elf_External_Verdaux);
6587 }
6588
eea6121a 6589 s->size = size;
a50b1753 6590 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6591 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6592 return FALSE;
6593
6594 /* Fill in the version definition section. */
6595
6596 p = s->contents;
6597
6598 def.vd_version = VER_DEF_CURRENT;
6599 def.vd_flags = VER_FLG_BASE;
6600 def.vd_ndx = 1;
6601 def.vd_cnt = 1;
3e3b46e5
PB
6602 if (info->create_default_symver)
6603 {
6604 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6605 def.vd_next = sizeof (Elf_External_Verdef);
6606 }
6607 else
6608 {
6609 def.vd_aux = sizeof (Elf_External_Verdef);
6610 def.vd_next = (sizeof (Elf_External_Verdef)
6611 + sizeof (Elf_External_Verdaux));
6612 }
5a580b3a 6613
ef53be89 6614 if (soname_indx != (size_t) -1)
5a580b3a
AM
6615 {
6616 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6617 soname_indx);
6618 def.vd_hash = bfd_elf_hash (soname);
6619 defaux.vda_name = soname_indx;
3e3b46e5 6620 name = soname;
5a580b3a
AM
6621 }
6622 else
6623 {
ef53be89 6624 size_t indx;
5a580b3a 6625
765cf5f6 6626 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6627 def.vd_hash = bfd_elf_hash (name);
6628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6629 name, FALSE);
ef53be89 6630 if (indx == (size_t) -1)
5a580b3a
AM
6631 return FALSE;
6632 defaux.vda_name = indx;
6633 }
6634 defaux.vda_next = 0;
6635
6636 _bfd_elf_swap_verdef_out (output_bfd, &def,
6637 (Elf_External_Verdef *) p);
6638 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6639 if (info->create_default_symver)
6640 {
6641 /* Add a symbol representing this version. */
6642 bh = NULL;
6643 if (! (_bfd_generic_link_add_one_symbol
6644 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6645 0, NULL, FALSE,
6646 get_elf_backend_data (dynobj)->collect, &bh)))
6647 return FALSE;
6648 h = (struct elf_link_hash_entry *) bh;
6649 h->non_elf = 0;
6650 h->def_regular = 1;
6651 h->type = STT_OBJECT;
6652 h->verinfo.vertree = NULL;
6653
6654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6655 return FALSE;
6656
6657 /* Create a duplicate of the base version with the same
6658 aux block, but different flags. */
6659 def.vd_flags = 0;
6660 def.vd_ndx = 2;
6661 def.vd_aux = sizeof (Elf_External_Verdef);
6662 if (verdefs)
6663 def.vd_next = (sizeof (Elf_External_Verdef)
6664 + sizeof (Elf_External_Verdaux));
6665 else
6666 def.vd_next = 0;
6667 _bfd_elf_swap_verdef_out (output_bfd, &def,
6668 (Elf_External_Verdef *) p);
6669 p += sizeof (Elf_External_Verdef);
6670 }
5a580b3a
AM
6671 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6672 (Elf_External_Verdaux *) p);
6673 p += sizeof (Elf_External_Verdaux);
6674
6675 for (t = verdefs; t != NULL; t = t->next)
6676 {
6677 unsigned int cdeps;
6678 struct bfd_elf_version_deps *n;
5a580b3a 6679
a6cc6b3b
RO
6680 /* Don't emit the base version twice. */
6681 if (t->vernum == 0)
6682 continue;
6683
5a580b3a
AM
6684 cdeps = 0;
6685 for (n = t->deps; n != NULL; n = n->next)
6686 ++cdeps;
6687
6688 /* Add a symbol representing this version. */
6689 bh = NULL;
6690 if (! (_bfd_generic_link_add_one_symbol
6691 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6692 0, NULL, FALSE,
6693 get_elf_backend_data (dynobj)->collect, &bh)))
6694 return FALSE;
6695 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6696 h->non_elf = 0;
6697 h->def_regular = 1;
5a580b3a
AM
6698 h->type = STT_OBJECT;
6699 h->verinfo.vertree = t;
6700
c152c796 6701 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6702 return FALSE;
6703
6704 def.vd_version = VER_DEF_CURRENT;
6705 def.vd_flags = 0;
6706 if (t->globals.list == NULL
6707 && t->locals.list == NULL
6708 && ! t->used)
6709 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6710 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6711 def.vd_cnt = cdeps + 1;
6712 def.vd_hash = bfd_elf_hash (t->name);
6713 def.vd_aux = sizeof (Elf_External_Verdef);
6714 def.vd_next = 0;
a6cc6b3b
RO
6715
6716 /* If a basever node is next, it *must* be the last node in
6717 the chain, otherwise Verdef construction breaks. */
6718 if (t->next != NULL && t->next->vernum == 0)
6719 BFD_ASSERT (t->next->next == NULL);
6720
6721 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6722 def.vd_next = (sizeof (Elf_External_Verdef)
6723 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6724
6725 _bfd_elf_swap_verdef_out (output_bfd, &def,
6726 (Elf_External_Verdef *) p);
6727 p += sizeof (Elf_External_Verdef);
6728
6729 defaux.vda_name = h->dynstr_index;
6730 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6731 h->dynstr_index);
6732 defaux.vda_next = 0;
6733 if (t->deps != NULL)
6734 defaux.vda_next = sizeof (Elf_External_Verdaux);
6735 t->name_indx = defaux.vda_name;
6736
6737 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6738 (Elf_External_Verdaux *) p);
6739 p += sizeof (Elf_External_Verdaux);
6740
6741 for (n = t->deps; n != NULL; n = n->next)
6742 {
6743 if (n->version_needed == NULL)
6744 {
6745 /* This can happen if there was an error in the
6746 version script. */
6747 defaux.vda_name = 0;
6748 }
6749 else
6750 {
6751 defaux.vda_name = n->version_needed->name_indx;
6752 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6753 defaux.vda_name);
6754 }
6755 if (n->next == NULL)
6756 defaux.vda_next = 0;
6757 else
6758 defaux.vda_next = sizeof (Elf_External_Verdaux);
6759
6760 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6761 (Elf_External_Verdaux *) p);
6762 p += sizeof (Elf_External_Verdaux);
6763 }
6764 }
6765
5a580b3a
AM
6766 elf_tdata (output_bfd)->cverdefs = cdefs;
6767 }
902e9fc7
MR
6768 }
6769
6770 bed = get_elf_backend_data (output_bfd);
6771
6772 if (info->gc_sections && bed->can_gc_sections)
6773 {
6774 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6775
6776 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6777 dynamic symbol table. */
902e9fc7
MR
6778 sweep_info.info = info;
6779 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6780 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6781 &sweep_info);
3d13f3e9
AM
6782 }
6783
6784 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6785 {
6786 asection *s;
6787 struct elf_find_verdep_info sinfo;
6788
6789 /* Work out the size of the version reference section. */
6790
6791 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6792 BFD_ASSERT (s != NULL);
902e9fc7 6793
3d13f3e9
AM
6794 sinfo.info = info;
6795 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6796 if (sinfo.vers == 0)
6797 sinfo.vers = 1;
6798 sinfo.failed = FALSE;
6799
6800 elf_link_hash_traverse (elf_hash_table (info),
6801 _bfd_elf_link_find_version_dependencies,
6802 &sinfo);
6803 if (sinfo.failed)
6804 return FALSE;
6805
6806 if (elf_tdata (output_bfd)->verref == NULL)
6807 s->flags |= SEC_EXCLUDE;
6808 else
6809 {
6810 Elf_Internal_Verneed *vn;
6811 unsigned int size;
6812 unsigned int crefs;
6813 bfd_byte *p;
6814
6815 /* Build the version dependency section. */
6816 size = 0;
6817 crefs = 0;
6818 for (vn = elf_tdata (output_bfd)->verref;
6819 vn != NULL;
6820 vn = vn->vn_nextref)
6821 {
6822 Elf_Internal_Vernaux *a;
6823
6824 size += sizeof (Elf_External_Verneed);
6825 ++crefs;
6826 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6827 size += sizeof (Elf_External_Vernaux);
6828 }
6829
6830 s->size = size;
6831 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6832 if (s->contents == NULL)
6833 return FALSE;
6834
6835 p = s->contents;
6836 for (vn = elf_tdata (output_bfd)->verref;
6837 vn != NULL;
6838 vn = vn->vn_nextref)
6839 {
6840 unsigned int caux;
6841 Elf_Internal_Vernaux *a;
6842 size_t indx;
6843
6844 caux = 0;
6845 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6846 ++caux;
6847
6848 vn->vn_version = VER_NEED_CURRENT;
6849 vn->vn_cnt = caux;
6850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6851 elf_dt_name (vn->vn_bfd) != NULL
6852 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6853 : lbasename (bfd_get_filename
6854 (vn->vn_bfd)),
3d13f3e9
AM
6855 FALSE);
6856 if (indx == (size_t) -1)
6857 return FALSE;
6858 vn->vn_file = indx;
6859 vn->vn_aux = sizeof (Elf_External_Verneed);
6860 if (vn->vn_nextref == NULL)
6861 vn->vn_next = 0;
6862 else
6863 vn->vn_next = (sizeof (Elf_External_Verneed)
6864 + caux * sizeof (Elf_External_Vernaux));
6865
6866 _bfd_elf_swap_verneed_out (output_bfd, vn,
6867 (Elf_External_Verneed *) p);
6868 p += sizeof (Elf_External_Verneed);
6869
6870 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6871 {
6872 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6873 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6874 a->vna_nodename, FALSE);
6875 if (indx == (size_t) -1)
6876 return FALSE;
6877 a->vna_name = indx;
6878 if (a->vna_nextptr == NULL)
6879 a->vna_next = 0;
6880 else
6881 a->vna_next = sizeof (Elf_External_Vernaux);
6882
6883 _bfd_elf_swap_vernaux_out (output_bfd, a,
6884 (Elf_External_Vernaux *) p);
6885 p += sizeof (Elf_External_Vernaux);
6886 }
6887 }
6888
6889 elf_tdata (output_bfd)->cverrefs = crefs;
6890 }
902e9fc7
MR
6891 }
6892
6893 /* Any syms created from now on start with -1 in
6894 got.refcount/offset and plt.refcount/offset. */
6895 elf_hash_table (info)->init_got_refcount
6896 = elf_hash_table (info)->init_got_offset;
6897 elf_hash_table (info)->init_plt_refcount
6898 = elf_hash_table (info)->init_plt_offset;
6899
6900 if (bfd_link_relocatable (info)
6901 && !_bfd_elf_size_group_sections (info))
6902 return FALSE;
6903
6904 /* The backend may have to create some sections regardless of whether
6905 we're dynamic or not. */
6906 if (bed->elf_backend_always_size_sections
6907 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6908 return FALSE;
6909
6910 /* Determine any GNU_STACK segment requirements, after the backend
6911 has had a chance to set a default segment size. */
6912 if (info->execstack)
6913 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6914 else if (info->noexecstack)
6915 elf_stack_flags (output_bfd) = PF_R | PF_W;
6916 else
6917 {
6918 bfd *inputobj;
6919 asection *notesec = NULL;
6920 int exec = 0;
6921
6922 for (inputobj = info->input_bfds;
6923 inputobj;
6924 inputobj = inputobj->link.next)
6925 {
6926 asection *s;
6927
6928 if (inputobj->flags
6929 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6930 continue;
57963c05
AM
6931 s = inputobj->sections;
6932 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6933 continue;
6934
902e9fc7
MR
6935 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6936 if (s)
6937 {
6938 if (s->flags & SEC_CODE)
6939 exec = PF_X;
6940 notesec = s;
6941 }
6942 else if (bed->default_execstack)
6943 exec = PF_X;
6944 }
6945 if (notesec || info->stacksize > 0)
6946 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6947 if (notesec && exec && bfd_link_relocatable (info)
6948 && notesec->output_section != bfd_abs_section_ptr)
6949 notesec->output_section->flags |= SEC_CODE;
6950 }
6951
6952 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6953 {
6954 struct elf_info_failed eif;
6955 struct elf_link_hash_entry *h;
6956 asection *dynstr;
6957 asection *s;
6958
6959 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6960 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6961
902e9fc7
MR
6962 if (info->symbolic)
6963 {
6964 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6965 return FALSE;
6966 info->flags |= DF_SYMBOLIC;
6967 }
6968
6969 if (rpath != NULL)
6970 {
6971 size_t indx;
6972 bfd_vma tag;
6973
6974 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6975 TRUE);
6976 if (indx == (size_t) -1)
6977 return FALSE;
6978
6979 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6980 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6981 return FALSE;
6982 }
6983
6984 if (filter_shlib != NULL)
6985 {
6986 size_t indx;
6987
6988 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6989 filter_shlib, TRUE);
6990 if (indx == (size_t) -1
6991 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6992 return FALSE;
6993 }
6994
6995 if (auxiliary_filters != NULL)
6996 {
6997 const char * const *p;
6998
6999 for (p = auxiliary_filters; *p != NULL; p++)
7000 {
7001 size_t indx;
7002
7003 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7004 *p, TRUE);
7005 if (indx == (size_t) -1
7006 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7007 return FALSE;
7008 }
7009 }
7010
7011 if (audit != NULL)
7012 {
7013 size_t indx;
7014
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7016 TRUE);
7017 if (indx == (size_t) -1
7018 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7019 return FALSE;
7020 }
7021
7022 if (depaudit != NULL)
7023 {
7024 size_t indx;
7025
7026 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7027 TRUE);
7028 if (indx == (size_t) -1
7029 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7030 return FALSE;
7031 }
7032
7033 eif.info = info;
7034 eif.failed = FALSE;
7035
7036 /* Find all symbols which were defined in a dynamic object and make
7037 the backend pick a reasonable value for them. */
7038 elf_link_hash_traverse (elf_hash_table (info),
7039 _bfd_elf_adjust_dynamic_symbol,
7040 &eif);
7041 if (eif.failed)
7042 return FALSE;
7043
7044 /* Add some entries to the .dynamic section. We fill in some of the
7045 values later, in bfd_elf_final_link, but we must add the entries
7046 now so that we know the final size of the .dynamic section. */
7047
7048 /* If there are initialization and/or finalization functions to
7049 call then add the corresponding DT_INIT/DT_FINI entries. */
7050 h = (info->init_function
7051 ? elf_link_hash_lookup (elf_hash_table (info),
7052 info->init_function, FALSE,
7053 FALSE, FALSE)
7054 : NULL);
7055 if (h != NULL
7056 && (h->ref_regular
7057 || h->def_regular))
7058 {
7059 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7060 return FALSE;
7061 }
7062 h = (info->fini_function
7063 ? elf_link_hash_lookup (elf_hash_table (info),
7064 info->fini_function, FALSE,
7065 FALSE, FALSE)
7066 : NULL);
7067 if (h != NULL
7068 && (h->ref_regular
7069 || h->def_regular))
7070 {
7071 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7072 return FALSE;
7073 }
7074
7075 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7076 if (s != NULL && s->linker_has_input)
7077 {
7078 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7079 if (! bfd_link_executable (info))
7080 {
7081 bfd *sub;
7082 asection *o;
7083
57963c05
AM
7084 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7085 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7086 && (o = sub->sections) != NULL
7087 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7088 for (o = sub->sections; o != NULL; o = o->next)
7089 if (elf_section_data (o)->this_hdr.sh_type
7090 == SHT_PREINIT_ARRAY)
7091 {
7092 _bfd_error_handler
871b3ab2 7093 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7094 sub);
7095 break;
7096 }
7097
7098 bfd_set_error (bfd_error_nonrepresentable_section);
7099 return FALSE;
7100 }
7101
7102 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7103 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7104 return FALSE;
7105 }
7106 s = bfd_get_section_by_name (output_bfd, ".init_array");
7107 if (s != NULL && s->linker_has_input)
7108 {
7109 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7110 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7111 return FALSE;
7112 }
7113 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7114 if (s != NULL && s->linker_has_input)
7115 {
7116 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7117 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7118 return FALSE;
7119 }
7120
7121 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7122 /* If .dynstr is excluded from the link, we don't want any of
7123 these tags. Strictly, we should be checking each section
7124 individually; This quick check covers for the case where
7125 someone does a /DISCARD/ : { *(*) }. */
7126 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7127 {
7128 bfd_size_type strsize;
7129
7130 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7131 if ((info->emit_hash
7132 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7133 || (info->emit_gnu_hash
f16a9783
MS
7134 && (bed->record_xhash_symbol == NULL
7135 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7136 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7137 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7138 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7139 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7140 bed->s->sizeof_sym))
7141 return FALSE;
7142 }
7143 }
7144
7145 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7146 return FALSE;
7147
7148 /* The backend must work out the sizes of all the other dynamic
7149 sections. */
7150 if (dynobj != NULL
7151 && bed->elf_backend_size_dynamic_sections != NULL
7152 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7153 return FALSE;
7154
7155 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7156 {
902e9fc7
MR
7157 if (elf_tdata (output_bfd)->cverdefs)
7158 {
7159 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7160
7161 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7162 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7163 return FALSE;
7164 }
7165
7166 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7167 {
7168 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7169 return FALSE;
7170 }
7171 else if (info->flags & DF_BIND_NOW)
7172 {
7173 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7174 return FALSE;
7175 }
7176
7177 if (info->flags_1)
7178 {
7179 if (bfd_link_executable (info))
7180 info->flags_1 &= ~ (DF_1_INITFIRST
7181 | DF_1_NODELETE
7182 | DF_1_NOOPEN);
7183 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7184 return FALSE;
7185 }
7186
7187 if (elf_tdata (output_bfd)->cverrefs)
7188 {
7189 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7190
7191 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7192 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7193 return FALSE;
7194 }
5a580b3a 7195
8423293d
AM
7196 if ((elf_tdata (output_bfd)->cverrefs == 0
7197 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7198 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7199 {
902e9fc7
MR
7200 asection *s;
7201
3d4d4302 7202 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7203 s->flags |= SEC_EXCLUDE;
7204 }
7205 }
7206 return TRUE;
7207}
7208
74541ad4
AM
7209/* Find the first non-excluded output section. We'll use its
7210 section symbol for some emitted relocs. */
7211void
7212_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7213{
7214 asection *s;
f26a3287 7215 asection *found = NULL;
74541ad4
AM
7216
7217 for (s = output_bfd->sections; s != NULL; s = s->next)
7218 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7219 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7220 {
f26a3287
AM
7221 found = s;
7222 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7223 break;
74541ad4 7224 }
f26a3287 7225 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7226}
7227
7228/* Find two non-excluded output sections, one for code, one for data.
7229 We'll use their section symbols for some emitted relocs. */
7230void
7231_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7232{
7233 asection *s;
f26a3287 7234 asection *found = NULL;
74541ad4 7235
266b05cf 7236 /* Data first, since setting text_index_section changes
7f923b7f 7237 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7238 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7239 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7240 && !(s->flags & SEC_READONLY)
d00dd7dc 7241 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7242 {
f26a3287
AM
7243 found = s;
7244 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7245 break;
74541ad4 7246 }
f26a3287 7247 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7248
7249 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7250 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7251 && (s->flags & SEC_READONLY)
d00dd7dc 7252 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7253 {
f26a3287 7254 found = s;
74541ad4
AM
7255 break;
7256 }
f26a3287 7257 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7258}
7259
f16a9783
MS
7260#define GNU_HASH_SECTION_NAME(bed) \
7261 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7262
8423293d
AM
7263bfd_boolean
7264bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7265{
74541ad4 7266 const struct elf_backend_data *bed;
23ec1e32 7267 unsigned long section_sym_count;
96d01d93 7268 bfd_size_type dynsymcount = 0;
74541ad4 7269
8423293d
AM
7270 if (!is_elf_hash_table (info->hash))
7271 return TRUE;
7272
74541ad4
AM
7273 bed = get_elf_backend_data (output_bfd);
7274 (*bed->elf_backend_init_index_section) (output_bfd, info);
7275
23ec1e32
MR
7276 /* Assign dynsym indices. In a shared library we generate a section
7277 symbol for each output section, which come first. Next come all
7278 of the back-end allocated local dynamic syms, followed by the rest
7279 of the global symbols.
7280
7281 This is usually not needed for static binaries, however backends
7282 can request to always do it, e.g. the MIPS backend uses dynamic
7283 symbol counts to lay out GOT, which will be produced in the
7284 presence of GOT relocations even in static binaries (holding fixed
7285 data in that case, to satisfy those relocations). */
7286
7287 if (elf_hash_table (info)->dynamic_sections_created
7288 || bed->always_renumber_dynsyms)
7289 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7290 &section_sym_count);
7291
8423293d
AM
7292 if (elf_hash_table (info)->dynamic_sections_created)
7293 {
7294 bfd *dynobj;
8423293d 7295 asection *s;
8423293d
AM
7296 unsigned int dtagcount;
7297
7298 dynobj = elf_hash_table (info)->dynobj;
7299
5a580b3a 7300 /* Work out the size of the symbol version section. */
3d4d4302 7301 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7302 BFD_ASSERT (s != NULL);
d5486c43 7303 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7304 {
eea6121a 7305 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7306 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7307 if (s->contents == NULL)
7308 return FALSE;
7309
7310 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7311 return FALSE;
7312 }
7313
7314 /* Set the size of the .dynsym and .hash sections. We counted
7315 the number of dynamic symbols in elf_link_add_object_symbols.
7316 We will build the contents of .dynsym and .hash when we build
7317 the final symbol table, because until then we do not know the
7318 correct value to give the symbols. We built the .dynstr
7319 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7320 s = elf_hash_table (info)->dynsym;
5a580b3a 7321 BFD_ASSERT (s != NULL);
eea6121a 7322 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7323
d5486c43
L
7324 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7325 if (s->contents == NULL)
7326 return FALSE;
5a580b3a 7327
d5486c43
L
7328 /* The first entry in .dynsym is a dummy symbol. Clear all the
7329 section syms, in case we don't output them all. */
7330 ++section_sym_count;
7331 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7332
fdc90cb4
JJ
7333 elf_hash_table (info)->bucketcount = 0;
7334
5a580b3a
AM
7335 /* Compute the size of the hashing table. As a side effect this
7336 computes the hash values for all the names we export. */
fdc90cb4
JJ
7337 if (info->emit_hash)
7338 {
7339 unsigned long int *hashcodes;
14b1c01e 7340 struct hash_codes_info hashinf;
fdc90cb4
JJ
7341 bfd_size_type amt;
7342 unsigned long int nsyms;
7343 size_t bucketcount;
7344 size_t hash_entry_size;
7345
7346 /* Compute the hash values for all exported symbols. At the same
7347 time store the values in an array so that we could use them for
7348 optimizations. */
7349 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7350 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7351 if (hashcodes == NULL)
7352 return FALSE;
14b1c01e
AM
7353 hashinf.hashcodes = hashcodes;
7354 hashinf.error = FALSE;
5a580b3a 7355
fdc90cb4
JJ
7356 /* Put all hash values in HASHCODES. */
7357 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7358 elf_collect_hash_codes, &hashinf);
7359 if (hashinf.error)
4dd07732
AM
7360 {
7361 free (hashcodes);
7362 return FALSE;
7363 }
5a580b3a 7364
14b1c01e 7365 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7366 bucketcount
7367 = compute_bucket_count (info, hashcodes, nsyms, 0);
7368 free (hashcodes);
7369
4b48e2f6 7370 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7371 return FALSE;
5a580b3a 7372
fdc90cb4
JJ
7373 elf_hash_table (info)->bucketcount = bucketcount;
7374
3d4d4302 7375 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7376 BFD_ASSERT (s != NULL);
7377 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7378 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7379 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7380 if (s->contents == NULL)
7381 return FALSE;
7382
7383 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7384 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7385 s->contents + hash_entry_size);
7386 }
7387
7388 if (info->emit_gnu_hash)
7389 {
7390 size_t i, cnt;
7391 unsigned char *contents;
7392 struct collect_gnu_hash_codes cinfo;
7393 bfd_size_type amt;
7394 size_t bucketcount;
7395
7396 memset (&cinfo, 0, sizeof (cinfo));
7397
7398 /* Compute the hash values for all exported symbols. At the same
7399 time store the values in an array so that we could use them for
7400 optimizations. */
7401 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7402 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7403 if (cinfo.hashcodes == NULL)
7404 return FALSE;
7405
7406 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7407 cinfo.min_dynindx = -1;
7408 cinfo.output_bfd = output_bfd;
7409 cinfo.bed = bed;
7410
7411 /* Put all hash values in HASHCODES. */
7412 elf_link_hash_traverse (elf_hash_table (info),
7413 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7414 if (cinfo.error)
4dd07732
AM
7415 {
7416 free (cinfo.hashcodes);
7417 return FALSE;
7418 }
fdc90cb4
JJ
7419
7420 bucketcount
7421 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7422
7423 if (bucketcount == 0)
7424 {
7425 free (cinfo.hashcodes);
7426 return FALSE;
7427 }
7428
f16a9783 7429 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7430 BFD_ASSERT (s != NULL);
7431
7432 if (cinfo.nsyms == 0)
7433 {
f16a9783 7434 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7435 BFD_ASSERT (cinfo.min_dynindx == -1);
7436 free (cinfo.hashcodes);
7437 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7438 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7439 if (contents == NULL)
7440 return FALSE;
7441 s->contents = contents;
7442 /* 1 empty bucket. */
7443 bfd_put_32 (output_bfd, 1, contents);
7444 /* SYMIDX above the special symbol 0. */
7445 bfd_put_32 (output_bfd, 1, contents + 4);
7446 /* Just one word for bitmask. */
7447 bfd_put_32 (output_bfd, 1, contents + 8);
7448 /* Only hash fn bloom filter. */
7449 bfd_put_32 (output_bfd, 0, contents + 12);
7450 /* No hashes are valid - empty bitmask. */
7451 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7452 /* No hashes in the only bucket. */
7453 bfd_put_32 (output_bfd, 0,
7454 contents + 16 + bed->s->arch_size / 8);
7455 }
7456 else
7457 {
9e6619e2 7458 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7459 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7460
9e6619e2
AM
7461 x = cinfo.nsyms;
7462 maskbitslog2 = 1;
7463 while ((x >>= 1) != 0)
7464 ++maskbitslog2;
fdc90cb4
JJ
7465 if (maskbitslog2 < 3)
7466 maskbitslog2 = 5;
7467 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7468 maskbitslog2 = maskbitslog2 + 3;
7469 else
7470 maskbitslog2 = maskbitslog2 + 2;
7471 if (bed->s->arch_size == 64)
7472 {
7473 if (maskbitslog2 == 5)
7474 maskbitslog2 = 6;
7475 cinfo.shift1 = 6;
7476 }
7477 else
7478 cinfo.shift1 = 5;
7479 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7480 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7481 cinfo.maskbits = 1 << maskbitslog2;
7482 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7483 amt = bucketcount * sizeof (unsigned long int) * 2;
7484 amt += maskwords * sizeof (bfd_vma);
a50b1753 7485 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7486 if (cinfo.bitmask == NULL)
7487 {
7488 free (cinfo.hashcodes);
7489 return FALSE;
7490 }
7491
a50b1753 7492 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7493 cinfo.indx = cinfo.counts + bucketcount;
7494 cinfo.symindx = dynsymcount - cinfo.nsyms;
7495 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7496
7497 /* Determine how often each hash bucket is used. */
7498 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7499 for (i = 0; i < cinfo.nsyms; ++i)
7500 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7501
7502 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7503 if (cinfo.counts[i] != 0)
7504 {
7505 cinfo.indx[i] = cnt;
7506 cnt += cinfo.counts[i];
7507 }
7508 BFD_ASSERT (cnt == dynsymcount);
7509 cinfo.bucketcount = bucketcount;
7510 cinfo.local_indx = cinfo.min_dynindx;
7511
7512 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7513 s->size += cinfo.maskbits / 8;
f16a9783
MS
7514 if (bed->record_xhash_symbol != NULL)
7515 s->size += cinfo.nsyms * 4;
a50b1753 7516 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7517 if (contents == NULL)
7518 {
7519 free (cinfo.bitmask);
7520 free (cinfo.hashcodes);
7521 return FALSE;
7522 }
7523
7524 s->contents = contents;
7525 bfd_put_32 (output_bfd, bucketcount, contents);
7526 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7527 bfd_put_32 (output_bfd, maskwords, contents + 8);
7528 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7529 contents += 16 + cinfo.maskbits / 8;
7530
7531 for (i = 0; i < bucketcount; ++i)
7532 {
7533 if (cinfo.counts[i] == 0)
7534 bfd_put_32 (output_bfd, 0, contents);
7535 else
7536 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7537 contents += 4;
7538 }
7539
7540 cinfo.contents = contents;
7541
f16a9783
MS
7542 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7543 /* Renumber dynamic symbols, if populating .gnu.hash section.
7544 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7545 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7546 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7547
7548 contents = s->contents + 16;
7549 for (i = 0; i < maskwords; ++i)
7550 {
7551 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7552 contents);
7553 contents += bed->s->arch_size / 8;
7554 }
7555
7556 free (cinfo.bitmask);
7557 free (cinfo.hashcodes);
7558 }
7559 }
5a580b3a 7560
3d4d4302 7561 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7562 BFD_ASSERT (s != NULL);
7563
4ad4eba5 7564 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7565
eea6121a 7566 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7567
7568 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7569 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7570 return FALSE;
7571 }
7572
7573 return TRUE;
7574}
4d269e42 7575\f
4d269e42
AM
7576/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7577
7578static void
7579merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7580 asection *sec)
7581{
dbaa2011
AM
7582 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7583 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7584}
7585
7586/* Finish SHF_MERGE section merging. */
7587
7588bfd_boolean
630993ec 7589_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7590{
7591 bfd *ibfd;
7592 asection *sec;
7593
7594 if (!is_elf_hash_table (info->hash))
7595 return FALSE;
7596
c72f2fb2 7597 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7598 if ((ibfd->flags & DYNAMIC) == 0
7599 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7600 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7601 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7602 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7603 if ((sec->flags & SEC_MERGE) != 0
7604 && !bfd_is_abs_section (sec->output_section))
7605 {
7606 struct bfd_elf_section_data *secdata;
7607
7608 secdata = elf_section_data (sec);
630993ec 7609 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7610 &elf_hash_table (info)->merge_info,
7611 sec, &secdata->sec_info))
7612 return FALSE;
7613 else if (secdata->sec_info)
dbaa2011 7614 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7615 }
7616
7617 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7618 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7619 merge_sections_remove_hook);
7620 return TRUE;
7621}
7622
7623/* Create an entry in an ELF linker hash table. */
7624
7625struct bfd_hash_entry *
7626_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7627 struct bfd_hash_table *table,
7628 const char *string)
7629{
7630 /* Allocate the structure if it has not already been allocated by a
7631 subclass. */
7632 if (entry == NULL)
7633 {
a50b1753 7634 entry = (struct bfd_hash_entry *)
ca4be51c 7635 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7636 if (entry == NULL)
7637 return entry;
7638 }
7639
7640 /* Call the allocation method of the superclass. */
7641 entry = _bfd_link_hash_newfunc (entry, table, string);
7642 if (entry != NULL)
7643 {
7644 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7645 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7646
7647 /* Set local fields. */
7648 ret->indx = -1;
7649 ret->dynindx = -1;
7650 ret->got = htab->init_got_refcount;
7651 ret->plt = htab->init_plt_refcount;
7652 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7653 - offsetof (struct elf_link_hash_entry, size)));
7654 /* Assume that we have been called by a non-ELF symbol reader.
7655 This flag is then reset by the code which reads an ELF input
7656 file. This ensures that a symbol created by a non-ELF symbol
7657 reader will have the flag set correctly. */
7658 ret->non_elf = 1;
7659 }
7660
7661 return entry;
7662}
7663
7664/* Copy data from an indirect symbol to its direct symbol, hiding the
7665 old indirect symbol. Also used for copying flags to a weakdef. */
7666
7667void
7668_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7669 struct elf_link_hash_entry *dir,
7670 struct elf_link_hash_entry *ind)
7671{
7672 struct elf_link_hash_table *htab;
7673
7674 /* Copy down any references that we may have already seen to the
e81830c5 7675 symbol which just became indirect. */
4d269e42 7676
422f1182 7677 if (dir->versioned != versioned_hidden)
e81830c5
AM
7678 dir->ref_dynamic |= ind->ref_dynamic;
7679 dir->ref_regular |= ind->ref_regular;
7680 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7681 dir->non_got_ref |= ind->non_got_ref;
7682 dir->needs_plt |= ind->needs_plt;
7683 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7684
7685 if (ind->root.type != bfd_link_hash_indirect)
7686 return;
7687
7688 /* Copy over the global and procedure linkage table refcount entries.
7689 These may have been already set up by a check_relocs routine. */
7690 htab = elf_hash_table (info);
7691 if (ind->got.refcount > htab->init_got_refcount.refcount)
7692 {
7693 if (dir->got.refcount < 0)
7694 dir->got.refcount = 0;
7695 dir->got.refcount += ind->got.refcount;
7696 ind->got.refcount = htab->init_got_refcount.refcount;
7697 }
7698
7699 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7700 {
7701 if (dir->plt.refcount < 0)
7702 dir->plt.refcount = 0;
7703 dir->plt.refcount += ind->plt.refcount;
7704 ind->plt.refcount = htab->init_plt_refcount.refcount;
7705 }
7706
7707 if (ind->dynindx != -1)
7708 {
7709 if (dir->dynindx != -1)
7710 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7711 dir->dynindx = ind->dynindx;
7712 dir->dynstr_index = ind->dynstr_index;
7713 ind->dynindx = -1;
7714 ind->dynstr_index = 0;
7715 }
7716}
7717
7718void
7719_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7720 struct elf_link_hash_entry *h,
7721 bfd_boolean force_local)
7722{
3aa14d16
L
7723 /* STT_GNU_IFUNC symbol must go through PLT. */
7724 if (h->type != STT_GNU_IFUNC)
7725 {
7726 h->plt = elf_hash_table (info)->init_plt_offset;
7727 h->needs_plt = 0;
7728 }
4d269e42
AM
7729 if (force_local)
7730 {
7731 h->forced_local = 1;
7732 if (h->dynindx != -1)
7733 {
4d269e42
AM
7734 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7735 h->dynstr_index);
641338d8
AM
7736 h->dynindx = -1;
7737 h->dynstr_index = 0;
4d269e42
AM
7738 }
7739 }
7740}
7741
34a87bb0
L
7742/* Hide a symbol. */
7743
7744void
7745_bfd_elf_link_hide_symbol (bfd *output_bfd,
7746 struct bfd_link_info *info,
7747 struct bfd_link_hash_entry *h)
7748{
7749 if (is_elf_hash_table (info->hash))
7750 {
7751 const struct elf_backend_data *bed
7752 = get_elf_backend_data (output_bfd);
7753 struct elf_link_hash_entry *eh
7754 = (struct elf_link_hash_entry *) h;
7755 bed->elf_backend_hide_symbol (info, eh, TRUE);
7756 eh->def_dynamic = 0;
7757 eh->ref_dynamic = 0;
7758 eh->dynamic_def = 0;
7759 }
7760}
7761
7bf52ea2
AM
7762/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7763 caller. */
4d269e42
AM
7764
7765bfd_boolean
7766_bfd_elf_link_hash_table_init
7767 (struct elf_link_hash_table *table,
7768 bfd *abfd,
7769 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7770 struct bfd_hash_table *,
7771 const char *),
4dfe6ac6
NC
7772 unsigned int entsize,
7773 enum elf_target_id target_id)
4d269e42
AM
7774{
7775 bfd_boolean ret;
7776 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7777
4d269e42
AM
7778 table->init_got_refcount.refcount = can_refcount - 1;
7779 table->init_plt_refcount.refcount = can_refcount - 1;
7780 table->init_got_offset.offset = -(bfd_vma) 1;
7781 table->init_plt_offset.offset = -(bfd_vma) 1;
7782 /* The first dynamic symbol is a dummy. */
7783 table->dynsymcount = 1;
7784
7785 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7786
4d269e42 7787 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7788 table->hash_table_id = target_id;
4d269e42
AM
7789
7790 return ret;
7791}
7792
7793/* Create an ELF linker hash table. */
7794
7795struct bfd_link_hash_table *
7796_bfd_elf_link_hash_table_create (bfd *abfd)
7797{
7798 struct elf_link_hash_table *ret;
986f0783 7799 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7800
7bf52ea2 7801 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7802 if (ret == NULL)
7803 return NULL;
7804
7805 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7806 sizeof (struct elf_link_hash_entry),
7807 GENERIC_ELF_DATA))
4d269e42
AM
7808 {
7809 free (ret);
7810 return NULL;
7811 }
d495ab0d 7812 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7813
7814 return &ret->root;
7815}
7816
9f7c3e5e
AM
7817/* Destroy an ELF linker hash table. */
7818
7819void
d495ab0d 7820_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7821{
d495ab0d
AM
7822 struct elf_link_hash_table *htab;
7823
7824 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7825 if (htab->dynstr != NULL)
7826 _bfd_elf_strtab_free (htab->dynstr);
7827 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7828 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7829}
7830
4d269e42
AM
7831/* This is a hook for the ELF emulation code in the generic linker to
7832 tell the backend linker what file name to use for the DT_NEEDED
7833 entry for a dynamic object. */
7834
7835void
7836bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7837{
7838 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7839 && bfd_get_format (abfd) == bfd_object)
7840 elf_dt_name (abfd) = name;
7841}
7842
7843int
7844bfd_elf_get_dyn_lib_class (bfd *abfd)
7845{
7846 int lib_class;
7847 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7848 && bfd_get_format (abfd) == bfd_object)
7849 lib_class = elf_dyn_lib_class (abfd);
7850 else
7851 lib_class = 0;
7852 return lib_class;
7853}
7854
7855void
7856bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7857{
7858 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7859 && bfd_get_format (abfd) == bfd_object)
7860 elf_dyn_lib_class (abfd) = lib_class;
7861}
7862
7863/* Get the list of DT_NEEDED entries for a link. This is a hook for
7864 the linker ELF emulation code. */
7865
7866struct bfd_link_needed_list *
7867bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7868 struct bfd_link_info *info)
7869{
7870 if (! is_elf_hash_table (info->hash))
7871 return NULL;
7872 return elf_hash_table (info)->needed;
7873}
7874
7875/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7876 hook for the linker ELF emulation code. */
7877
7878struct bfd_link_needed_list *
7879bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7880 struct bfd_link_info *info)
7881{
7882 if (! is_elf_hash_table (info->hash))
7883 return NULL;
7884 return elf_hash_table (info)->runpath;
7885}
7886
7887/* Get the name actually used for a dynamic object for a link. This
7888 is the SONAME entry if there is one. Otherwise, it is the string
7889 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7890
7891const char *
7892bfd_elf_get_dt_soname (bfd *abfd)
7893{
7894 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7895 && bfd_get_format (abfd) == bfd_object)
7896 return elf_dt_name (abfd);
7897 return NULL;
7898}
7899
7900/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7901 the ELF linker emulation code. */
7902
7903bfd_boolean
7904bfd_elf_get_bfd_needed_list (bfd *abfd,
7905 struct bfd_link_needed_list **pneeded)
7906{
7907 asection *s;
7908 bfd_byte *dynbuf = NULL;
cb33740c 7909 unsigned int elfsec;
4d269e42
AM
7910 unsigned long shlink;
7911 bfd_byte *extdyn, *extdynend;
7912 size_t extdynsize;
7913 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7914
7915 *pneeded = NULL;
7916
7917 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7918 || bfd_get_format (abfd) != bfd_object)
7919 return TRUE;
7920
7921 s = bfd_get_section_by_name (abfd, ".dynamic");
7922 if (s == NULL || s->size == 0)
7923 return TRUE;
7924
7925 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7926 goto error_return;
7927
7928 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7929 if (elfsec == SHN_BAD)
4d269e42
AM
7930 goto error_return;
7931
7932 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7933
4d269e42
AM
7934 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7935 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7936
7937 extdyn = dynbuf;
7938 extdynend = extdyn + s->size;
7939 for (; extdyn < extdynend; extdyn += extdynsize)
7940 {
7941 Elf_Internal_Dyn dyn;
7942
7943 (*swap_dyn_in) (abfd, extdyn, &dyn);
7944
7945 if (dyn.d_tag == DT_NULL)
7946 break;
7947
7948 if (dyn.d_tag == DT_NEEDED)
7949 {
7950 const char *string;
7951 struct bfd_link_needed_list *l;
7952 unsigned int tagv = dyn.d_un.d_val;
986f0783 7953 size_t amt;
4d269e42
AM
7954
7955 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7956 if (string == NULL)
7957 goto error_return;
7958
7959 amt = sizeof *l;
a50b1753 7960 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7961 if (l == NULL)
7962 goto error_return;
7963
7964 l->by = abfd;
7965 l->name = string;
7966 l->next = *pneeded;
7967 *pneeded = l;
7968 }
7969 }
7970
7971 free (dynbuf);
7972
7973 return TRUE;
7974
7975 error_return:
7976 if (dynbuf != NULL)
7977 free (dynbuf);
7978 return FALSE;
7979}
7980
7981struct elf_symbuf_symbol
7982{
7983 unsigned long st_name; /* Symbol name, index in string tbl */
7984 unsigned char st_info; /* Type and binding attributes */
7985 unsigned char st_other; /* Visibilty, and target specific */
7986};
7987
7988struct elf_symbuf_head
7989{
7990 struct elf_symbuf_symbol *ssym;
ef53be89 7991 size_t count;
4d269e42
AM
7992 unsigned int st_shndx;
7993};
7994
7995struct elf_symbol
7996{
7997 union
7998 {
7999 Elf_Internal_Sym *isym;
8000 struct elf_symbuf_symbol *ssym;
dcea6a95 8001 void *p;
4d269e42
AM
8002 } u;
8003 const char *name;
8004};
8005
8006/* Sort references to symbols by ascending section number. */
8007
8008static int
8009elf_sort_elf_symbol (const void *arg1, const void *arg2)
8010{
8011 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8012 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8013
dcea6a95
AM
8014 if (s1->st_shndx != s2->st_shndx)
8015 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8016 /* Final sort by the address of the sym in the symbuf ensures
8017 a stable sort. */
8018 if (s1 != s2)
8019 return s1 > s2 ? 1 : -1;
8020 return 0;
4d269e42
AM
8021}
8022
8023static int
8024elf_sym_name_compare (const void *arg1, const void *arg2)
8025{
8026 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8027 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8028 int ret = strcmp (s1->name, s2->name);
8029 if (ret != 0)
8030 return ret;
8031 if (s1->u.p != s2->u.p)
8032 return s1->u.p > s2->u.p ? 1 : -1;
8033 return 0;
4d269e42
AM
8034}
8035
8036static struct elf_symbuf_head *
ef53be89 8037elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8038{
14b1c01e 8039 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8040 struct elf_symbuf_symbol *ssym;
8041 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8042 size_t i, shndx_count, total_size, amt;
4d269e42 8043
446f7ed5
AM
8044 amt = symcount * sizeof (*indbuf);
8045 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8046 if (indbuf == NULL)
8047 return NULL;
8048
8049 for (ind = indbuf, i = 0; i < symcount; i++)
8050 if (isymbuf[i].st_shndx != SHN_UNDEF)
8051 *ind++ = &isymbuf[i];
8052 indbufend = ind;
8053
8054 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8055 elf_sort_elf_symbol);
8056
8057 shndx_count = 0;
8058 if (indbufend > indbuf)
8059 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8060 if (ind[0]->st_shndx != ind[1]->st_shndx)
8061 shndx_count++;
8062
3ae181ee
L
8063 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8064 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8065 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8066 if (ssymbuf == NULL)
8067 {
8068 free (indbuf);
8069 return NULL;
8070 }
8071
3ae181ee 8072 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8073 ssymbuf->ssym = NULL;
8074 ssymbuf->count = shndx_count;
8075 ssymbuf->st_shndx = 0;
8076 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8077 {
8078 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8079 {
8080 ssymhead++;
8081 ssymhead->ssym = ssym;
8082 ssymhead->count = 0;
8083 ssymhead->st_shndx = (*ind)->st_shndx;
8084 }
8085 ssym->st_name = (*ind)->st_name;
8086 ssym->st_info = (*ind)->st_info;
8087 ssym->st_other = (*ind)->st_other;
8088 ssymhead->count++;
8089 }
ef53be89 8090 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8091 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8092 == total_size));
4d269e42
AM
8093
8094 free (indbuf);
8095 return ssymbuf;
8096}
8097
8098/* Check if 2 sections define the same set of local and global
8099 symbols. */
8100
8f317e31 8101static bfd_boolean
4d269e42
AM
8102bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8103 struct bfd_link_info *info)
8104{
8105 bfd *bfd1, *bfd2;
8106 const struct elf_backend_data *bed1, *bed2;
8107 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8108 size_t symcount1, symcount2;
4d269e42
AM
8109 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8110 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8111 Elf_Internal_Sym *isym, *isymend;
8112 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8113 size_t count1, count2, i;
cb33740c 8114 unsigned int shndx1, shndx2;
4d269e42
AM
8115 bfd_boolean result;
8116
8117 bfd1 = sec1->owner;
8118 bfd2 = sec2->owner;
8119
4d269e42
AM
8120 /* Both sections have to be in ELF. */
8121 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8122 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8123 return FALSE;
8124
8125 if (elf_section_type (sec1) != elf_section_type (sec2))
8126 return FALSE;
8127
4d269e42
AM
8128 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8129 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8130 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8131 return FALSE;
8132
8133 bed1 = get_elf_backend_data (bfd1);
8134 bed2 = get_elf_backend_data (bfd2);
8135 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8136 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8137 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8138 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8139
8140 if (symcount1 == 0 || symcount2 == 0)
8141 return FALSE;
8142
8143 result = FALSE;
8144 isymbuf1 = NULL;
8145 isymbuf2 = NULL;
a50b1753
NC
8146 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8147 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8148
8149 if (ssymbuf1 == NULL)
8150 {
8151 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8152 NULL, NULL, NULL);
8153 if (isymbuf1 == NULL)
8154 goto done;
8155
8156 if (!info->reduce_memory_overheads)
dcea6a95
AM
8157 {
8158 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8159 elf_tdata (bfd1)->symbuf = ssymbuf1;
8160 }
4d269e42
AM
8161 }
8162
8163 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8164 {
8165 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8166 NULL, NULL, NULL);
8167 if (isymbuf2 == NULL)
8168 goto done;
8169
8170 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8171 {
8172 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8173 elf_tdata (bfd2)->symbuf = ssymbuf2;
8174 }
4d269e42
AM
8175 }
8176
8177 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8178 {
8179 /* Optimized faster version. */
ef53be89 8180 size_t lo, hi, mid;
4d269e42
AM
8181 struct elf_symbol *symp;
8182 struct elf_symbuf_symbol *ssym, *ssymend;
8183
8184 lo = 0;
8185 hi = ssymbuf1->count;
8186 ssymbuf1++;
8187 count1 = 0;
8188 while (lo < hi)
8189 {
8190 mid = (lo + hi) / 2;
cb33740c 8191 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8192 hi = mid;
cb33740c 8193 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8194 lo = mid + 1;
8195 else
8196 {
8197 count1 = ssymbuf1[mid].count;
8198 ssymbuf1 += mid;
8199 break;
8200 }
8201 }
8202
8203 lo = 0;
8204 hi = ssymbuf2->count;
8205 ssymbuf2++;
8206 count2 = 0;
8207 while (lo < hi)
8208 {
8209 mid = (lo + hi) / 2;
cb33740c 8210 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8211 hi = mid;
cb33740c 8212 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8213 lo = mid + 1;
8214 else
8215 {
8216 count2 = ssymbuf2[mid].count;
8217 ssymbuf2 += mid;
8218 break;
8219 }
8220 }
8221
8222 if (count1 == 0 || count2 == 0 || count1 != count2)
8223 goto done;
8224
ca4be51c
AM
8225 symtable1
8226 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8227 symtable2
8228 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8229 if (symtable1 == NULL || symtable2 == NULL)
8230 goto done;
8231
8232 symp = symtable1;
8233 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8234 ssym < ssymend; ssym++, symp++)
8235 {
8236 symp->u.ssym = ssym;
8237 symp->name = bfd_elf_string_from_elf_section (bfd1,
8238 hdr1->sh_link,
8239 ssym->st_name);
8240 }
8241
8242 symp = symtable2;
8243 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8244 ssym < ssymend; ssym++, symp++)
8245 {
8246 symp->u.ssym = ssym;
8247 symp->name = bfd_elf_string_from_elf_section (bfd2,
8248 hdr2->sh_link,
8249 ssym->st_name);
8250 }
8251
8252 /* Sort symbol by name. */
8253 qsort (symtable1, count1, sizeof (struct elf_symbol),
8254 elf_sym_name_compare);
8255 qsort (symtable2, count1, sizeof (struct elf_symbol),
8256 elf_sym_name_compare);
8257
8258 for (i = 0; i < count1; i++)
8259 /* Two symbols must have the same binding, type and name. */
8260 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8261 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8262 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8263 goto done;
8264
8265 result = TRUE;
8266 goto done;
8267 }
8268
a50b1753
NC
8269 symtable1 = (struct elf_symbol *)
8270 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8271 symtable2 = (struct elf_symbol *)
8272 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8273 if (symtable1 == NULL || symtable2 == NULL)
8274 goto done;
8275
8276 /* Count definitions in the section. */
8277 count1 = 0;
8278 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8279 if (isym->st_shndx == shndx1)
4d269e42
AM
8280 symtable1[count1++].u.isym = isym;
8281
8282 count2 = 0;
8283 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8284 if (isym->st_shndx == shndx2)
4d269e42
AM
8285 symtable2[count2++].u.isym = isym;
8286
8287 if (count1 == 0 || count2 == 0 || count1 != count2)
8288 goto done;
8289
8290 for (i = 0; i < count1; i++)
8291 symtable1[i].name
8292 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8293 symtable1[i].u.isym->st_name);
8294
8295 for (i = 0; i < count2; i++)
8296 symtable2[i].name
8297 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8298 symtable2[i].u.isym->st_name);
8299
8300 /* Sort symbol by name. */
8301 qsort (symtable1, count1, sizeof (struct elf_symbol),
8302 elf_sym_name_compare);
8303 qsort (symtable2, count1, sizeof (struct elf_symbol),
8304 elf_sym_name_compare);
8305
8306 for (i = 0; i < count1; i++)
8307 /* Two symbols must have the same binding, type and name. */
8308 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8309 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8310 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8311 goto done;
8312
8313 result = TRUE;
8314
dc1e8a47 8315 done:
4d269e42
AM
8316 if (symtable1)
8317 free (symtable1);
8318 if (symtable2)
8319 free (symtable2);
8320 if (isymbuf1)
8321 free (isymbuf1);
8322 if (isymbuf2)
8323 free (isymbuf2);
8324
8325 return result;
8326}
8327
8328/* Return TRUE if 2 section types are compatible. */
8329
8330bfd_boolean
8331_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8332 bfd *bbfd, const asection *bsec)
8333{
8334 if (asec == NULL
8335 || bsec == NULL
8336 || abfd->xvec->flavour != bfd_target_elf_flavour
8337 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8338 return TRUE;
8339
8340 return elf_section_type (asec) == elf_section_type (bsec);
8341}
8342\f
c152c796
AM
8343/* Final phase of ELF linker. */
8344
8345/* A structure we use to avoid passing large numbers of arguments. */
8346
8347struct elf_final_link_info
8348{
8349 /* General link information. */
8350 struct bfd_link_info *info;
8351 /* Output BFD. */
8352 bfd *output_bfd;
8353 /* Symbol string table. */
ef10c3ac 8354 struct elf_strtab_hash *symstrtab;
c152c796
AM
8355 /* .hash section. */
8356 asection *hash_sec;
8357 /* symbol version section (.gnu.version). */
8358 asection *symver_sec;
8359 /* Buffer large enough to hold contents of any section. */
8360 bfd_byte *contents;
8361 /* Buffer large enough to hold external relocs of any section. */
8362 void *external_relocs;
8363 /* Buffer large enough to hold internal relocs of any section. */
8364 Elf_Internal_Rela *internal_relocs;
8365 /* Buffer large enough to hold external local symbols of any input
8366 BFD. */
8367 bfd_byte *external_syms;
8368 /* And a buffer for symbol section indices. */
8369 Elf_External_Sym_Shndx *locsym_shndx;
8370 /* Buffer large enough to hold internal local symbols of any input
8371 BFD. */
8372 Elf_Internal_Sym *internal_syms;
8373 /* Array large enough to hold a symbol index for each local symbol
8374 of any input BFD. */
8375 long *indices;
8376 /* Array large enough to hold a section pointer for each local
8377 symbol of any input BFD. */
8378 asection **sections;
ef10c3ac 8379 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8380 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8381 /* Number of STT_FILE syms seen. */
8382 size_t filesym_count;
c152c796
AM
8383};
8384
8385/* This struct is used to pass information to elf_link_output_extsym. */
8386
8387struct elf_outext_info
8388{
8389 bfd_boolean failed;
8390 bfd_boolean localsyms;
34a79995 8391 bfd_boolean file_sym_done;
8b127cbc 8392 struct elf_final_link_info *flinfo;
c152c796
AM
8393};
8394
d9352518
DB
8395
8396/* Support for evaluating a complex relocation.
8397
8398 Complex relocations are generalized, self-describing relocations. The
8399 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8400 relocations themselves.
d9352518
DB
8401
8402 The relocations are use a reserved elf-wide relocation type code (R_RELC
8403 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8404 information (start bit, end bit, word width, etc) into the addend. This
8405 information is extracted from CGEN-generated operand tables within gas.
8406
8407 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8408 internal) representing prefix-notation expressions, including but not
8409 limited to those sorts of expressions normally encoded as addends in the
8410 addend field. The symbol mangling format is:
8411
8412 <node> := <literal>
07d6d2b8
AM
8413 | <unary-operator> ':' <node>
8414 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8415 ;
8416
8417 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8418 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8419 | '#' <hexdigits>
8420 ;
8421
8422 <binary-operator> := as in C
8423 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8424
8425static void
a0c8462f
AM
8426set_symbol_value (bfd *bfd_with_globals,
8427 Elf_Internal_Sym *isymbuf,
8428 size_t locsymcount,
8429 size_t symidx,
8430 bfd_vma val)
d9352518 8431{
8977835c
AM
8432 struct elf_link_hash_entry **sym_hashes;
8433 struct elf_link_hash_entry *h;
8434 size_t extsymoff = locsymcount;
d9352518 8435
8977835c 8436 if (symidx < locsymcount)
d9352518 8437 {
8977835c
AM
8438 Elf_Internal_Sym *sym;
8439
8440 sym = isymbuf + symidx;
8441 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8442 {
8443 /* It is a local symbol: move it to the
8444 "absolute" section and give it a value. */
8445 sym->st_shndx = SHN_ABS;
8446 sym->st_value = val;
8447 return;
8448 }
8449 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8450 extsymoff = 0;
d9352518 8451 }
8977835c
AM
8452
8453 /* It is a global symbol: set its link type
8454 to "defined" and give it a value. */
8455
8456 sym_hashes = elf_sym_hashes (bfd_with_globals);
8457 h = sym_hashes [symidx - extsymoff];
8458 while (h->root.type == bfd_link_hash_indirect
8459 || h->root.type == bfd_link_hash_warning)
8460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8461 h->root.type = bfd_link_hash_defined;
8462 h->root.u.def.value = val;
8463 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8464}
8465
a0c8462f
AM
8466static bfd_boolean
8467resolve_symbol (const char *name,
8468 bfd *input_bfd,
8b127cbc 8469 struct elf_final_link_info *flinfo,
a0c8462f
AM
8470 bfd_vma *result,
8471 Elf_Internal_Sym *isymbuf,
8472 size_t locsymcount)
d9352518 8473{
a0c8462f
AM
8474 Elf_Internal_Sym *sym;
8475 struct bfd_link_hash_entry *global_entry;
8476 const char *candidate = NULL;
8477 Elf_Internal_Shdr *symtab_hdr;
8478 size_t i;
8479
d9352518
DB
8480 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8481
8482 for (i = 0; i < locsymcount; ++ i)
8483 {
8977835c 8484 sym = isymbuf + i;
d9352518
DB
8485
8486 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8487 continue;
8488
8489 candidate = bfd_elf_string_from_elf_section (input_bfd,
8490 symtab_hdr->sh_link,
8491 sym->st_name);
8492#ifdef DEBUG
0f02bbd9
AM
8493 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8494 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8495#endif
8496 if (candidate && strcmp (candidate, name) == 0)
8497 {
8b127cbc 8498 asection *sec = flinfo->sections [i];
d9352518 8499
0f02bbd9
AM
8500 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8501 *result += sec->output_offset + sec->output_section->vma;
d9352518 8502#ifdef DEBUG
0f02bbd9
AM
8503 printf ("Found symbol with value %8.8lx\n",
8504 (unsigned long) *result);
d9352518
DB
8505#endif
8506 return TRUE;
8507 }
8508 }
8509
8510 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8511 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8512 FALSE, FALSE, TRUE);
d9352518
DB
8513 if (!global_entry)
8514 return FALSE;
a0c8462f 8515
d9352518
DB
8516 if (global_entry->type == bfd_link_hash_defined
8517 || global_entry->type == bfd_link_hash_defweak)
8518 {
a0c8462f
AM
8519 *result = (global_entry->u.def.value
8520 + global_entry->u.def.section->output_section->vma
8521 + global_entry->u.def.section->output_offset);
d9352518 8522#ifdef DEBUG
0f02bbd9
AM
8523 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8524 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8525#endif
8526 return TRUE;
a0c8462f 8527 }
d9352518 8528
d9352518
DB
8529 return FALSE;
8530}
8531
37b01f6a
DG
8532/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8533 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8534 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8535
d9352518 8536static bfd_boolean
a0c8462f
AM
8537resolve_section (const char *name,
8538 asection *sections,
37b01f6a
DG
8539 bfd_vma *result,
8540 bfd * abfd)
d9352518 8541{
a0c8462f
AM
8542 asection *curr;
8543 unsigned int len;
d9352518 8544
a0c8462f 8545 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8546 if (strcmp (curr->name, name) == 0)
8547 {
8548 *result = curr->vma;
8549 return TRUE;
8550 }
8551
8552 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8553 /* FIXME: This could be coded more efficiently... */
a0c8462f 8554 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8555 {
8556 len = strlen (curr->name);
a0c8462f 8557 if (len > strlen (name))
d9352518
DB
8558 continue;
8559
8560 if (strncmp (curr->name, name, len) == 0)
8561 {
8562 if (strncmp (".end", name + len, 4) == 0)
8563 {
61826503 8564 *result = (curr->vma
bb294208 8565 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8566 return TRUE;
8567 }
8568
8569 /* Insert more pseudo-section names here, if you like. */
8570 }
8571 }
a0c8462f 8572
d9352518
DB
8573 return FALSE;
8574}
8575
8576static void
a0c8462f 8577undefined_reference (const char *reftype, const char *name)
d9352518 8578{
695344c0 8579 /* xgettext:c-format */
a0c8462f
AM
8580 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8581 reftype, name);
d9352518
DB
8582}
8583
8584static bfd_boolean
a0c8462f
AM
8585eval_symbol (bfd_vma *result,
8586 const char **symp,
8587 bfd *input_bfd,
8b127cbc 8588 struct elf_final_link_info *flinfo,
a0c8462f
AM
8589 bfd_vma dot,
8590 Elf_Internal_Sym *isymbuf,
8591 size_t locsymcount,
8592 int signed_p)
d9352518 8593{
4b93929b
NC
8594 size_t len;
8595 size_t symlen;
a0c8462f
AM
8596 bfd_vma a;
8597 bfd_vma b;
4b93929b 8598 char symbuf[4096];
0f02bbd9 8599 const char *sym = *symp;
a0c8462f
AM
8600 const char *symend;
8601 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8602
8603 len = strlen (sym);
8604 symend = sym + len;
8605
4b93929b 8606 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8607 {
8608 bfd_set_error (bfd_error_invalid_operation);
8609 return FALSE;
8610 }
a0c8462f 8611
d9352518
DB
8612 switch (* sym)
8613 {
8614 case '.':
0f02bbd9
AM
8615 *result = dot;
8616 *symp = sym + 1;
d9352518
DB
8617 return TRUE;
8618
8619 case '#':
0f02bbd9
AM
8620 ++sym;
8621 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8622 return TRUE;
8623
8624 case 'S':
8625 symbol_is_section = TRUE;
1a0670f3 8626 /* Fall through. */
a0c8462f 8627 case 's':
0f02bbd9
AM
8628 ++sym;
8629 symlen = strtol (sym, (char **) symp, 10);
8630 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8631
4b93929b 8632 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8633 {
8634 bfd_set_error (bfd_error_invalid_operation);
8635 return FALSE;
8636 }
8637
8638 memcpy (symbuf, sym, symlen);
a0c8462f 8639 symbuf[symlen] = '\0';
0f02bbd9 8640 *symp = sym + symlen;
a0c8462f
AM
8641
8642 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8643 the symbol as a section, or vice-versa. so we're pretty liberal in our
8644 interpretation here; section means "try section first", not "must be a
8645 section", and likewise with symbol. */
8646
a0c8462f 8647 if (symbol_is_section)
d9352518 8648 {
37b01f6a 8649 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8650 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8651 isymbuf, locsymcount))
d9352518
DB
8652 {
8653 undefined_reference ("section", symbuf);
8654 return FALSE;
8655 }
a0c8462f
AM
8656 }
8657 else
d9352518 8658 {
8b127cbc 8659 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8660 isymbuf, locsymcount)
8b127cbc 8661 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8662 result, input_bfd))
d9352518
DB
8663 {
8664 undefined_reference ("symbol", symbuf);
8665 return FALSE;
8666 }
8667 }
8668
8669 return TRUE;
a0c8462f 8670
d9352518
DB
8671 /* All that remains are operators. */
8672
8673#define UNARY_OP(op) \
8674 if (strncmp (sym, #op, strlen (#op)) == 0) \
8675 { \
8676 sym += strlen (#op); \
a0c8462f
AM
8677 if (*sym == ':') \
8678 ++sym; \
0f02bbd9 8679 *symp = sym; \
8b127cbc 8680 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8681 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8682 return FALSE; \
8683 if (signed_p) \
0f02bbd9 8684 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8685 else \
8686 *result = op a; \
d9352518
DB
8687 return TRUE; \
8688 }
8689
8690#define BINARY_OP(op) \
8691 if (strncmp (sym, #op, strlen (#op)) == 0) \
8692 { \
8693 sym += strlen (#op); \
a0c8462f
AM
8694 if (*sym == ':') \
8695 ++sym; \
0f02bbd9 8696 *symp = sym; \
8b127cbc 8697 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8698 isymbuf, locsymcount, signed_p)) \
a0c8462f 8699 return FALSE; \
0f02bbd9 8700 ++*symp; \
8b127cbc 8701 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8702 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8703 return FALSE; \
8704 if (signed_p) \
0f02bbd9 8705 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8706 else \
8707 *result = a op b; \
d9352518
DB
8708 return TRUE; \
8709 }
8710
8711 default:
8712 UNARY_OP (0-);
8713 BINARY_OP (<<);
8714 BINARY_OP (>>);
8715 BINARY_OP (==);
8716 BINARY_OP (!=);
8717 BINARY_OP (<=);
8718 BINARY_OP (>=);
8719 BINARY_OP (&&);
8720 BINARY_OP (||);
8721 UNARY_OP (~);
8722 UNARY_OP (!);
8723 BINARY_OP (*);
8724 BINARY_OP (/);
8725 BINARY_OP (%);
8726 BINARY_OP (^);
8727 BINARY_OP (|);
8728 BINARY_OP (&);
8729 BINARY_OP (+);
8730 BINARY_OP (-);
8731 BINARY_OP (<);
8732 BINARY_OP (>);
8733#undef UNARY_OP
8734#undef BINARY_OP
8735 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8736 bfd_set_error (bfd_error_invalid_operation);
8737 return FALSE;
8738 }
8739}
8740
d9352518 8741static void
a0c8462f
AM
8742put_value (bfd_vma size,
8743 unsigned long chunksz,
8744 bfd *input_bfd,
8745 bfd_vma x,
8746 bfd_byte *location)
d9352518
DB
8747{
8748 location += (size - chunksz);
8749
41cd1ad1 8750 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8751 {
8752 switch (chunksz)
8753 {
d9352518
DB
8754 case 1:
8755 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8756 x >>= 8;
d9352518
DB
8757 break;
8758 case 2:
8759 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8760 x >>= 16;
d9352518
DB
8761 break;
8762 case 4:
8763 bfd_put_32 (input_bfd, x, location);
65164438
NC
8764 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8765 x >>= 16;
8766 x >>= 16;
d9352518 8767 break;
d9352518 8768#ifdef BFD64
41cd1ad1 8769 case 8:
d9352518 8770 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8771 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8772 x >>= 32;
8773 x >>= 32;
8774 break;
d9352518 8775#endif
41cd1ad1
NC
8776 default:
8777 abort ();
d9352518
DB
8778 break;
8779 }
8780 }
8781}
8782
a0c8462f
AM
8783static bfd_vma
8784get_value (bfd_vma size,
8785 unsigned long chunksz,
8786 bfd *input_bfd,
8787 bfd_byte *location)
d9352518 8788{
9b239e0e 8789 int shift;
d9352518
DB
8790 bfd_vma x = 0;
8791
9b239e0e
NC
8792 /* Sanity checks. */
8793 BFD_ASSERT (chunksz <= sizeof (x)
8794 && size >= chunksz
8795 && chunksz != 0
8796 && (size % chunksz) == 0
8797 && input_bfd != NULL
8798 && location != NULL);
8799
8800 if (chunksz == sizeof (x))
8801 {
8802 BFD_ASSERT (size == chunksz);
8803
8804 /* Make sure that we do not perform an undefined shift operation.
8805 We know that size == chunksz so there will only be one iteration
8806 of the loop below. */
8807 shift = 0;
8808 }
8809 else
8810 shift = 8 * chunksz;
8811
a0c8462f 8812 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8813 {
8814 switch (chunksz)
8815 {
d9352518 8816 case 1:
9b239e0e 8817 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8818 break;
8819 case 2:
9b239e0e 8820 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8821 break;
8822 case 4:
9b239e0e 8823 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8824 break;
d9352518 8825#ifdef BFD64
9b239e0e
NC
8826 case 8:
8827 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8828 break;
9b239e0e
NC
8829#endif
8830 default:
8831 abort ();
d9352518
DB
8832 }
8833 }
8834 return x;
8835}
8836
a0c8462f
AM
8837static void
8838decode_complex_addend (unsigned long *start, /* in bits */
8839 unsigned long *oplen, /* in bits */
8840 unsigned long *len, /* in bits */
8841 unsigned long *wordsz, /* in bytes */
8842 unsigned long *chunksz, /* in bytes */
8843 unsigned long *lsb0_p,
8844 unsigned long *signed_p,
8845 unsigned long *trunc_p,
8846 unsigned long encoded)
d9352518 8847{
07d6d2b8
AM
8848 * start = encoded & 0x3F;
8849 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8850 * oplen = (encoded >> 12) & 0x3F;
8851 * wordsz = (encoded >> 18) & 0xF;
8852 * chunksz = (encoded >> 22) & 0xF;
8853 * lsb0_p = (encoded >> 27) & 1;
8854 * signed_p = (encoded >> 28) & 1;
8855 * trunc_p = (encoded >> 29) & 1;
8856}
8857
cdfeee4f 8858bfd_reloc_status_type
0f02bbd9 8859bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8860 asection *input_section,
0f02bbd9
AM
8861 bfd_byte *contents,
8862 Elf_Internal_Rela *rel,
8863 bfd_vma relocation)
d9352518 8864{
0f02bbd9
AM
8865 bfd_vma shift, x, mask;
8866 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8867 bfd_reloc_status_type r;
bb294208 8868 bfd_size_type octets;
d9352518
DB
8869
8870 /* Perform this reloc, since it is complex.
8871 (this is not to say that it necessarily refers to a complex
8872 symbol; merely that it is a self-describing CGEN based reloc.
8873 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8874 word size, etc) encoded within it.). */
d9352518 8875
a0c8462f
AM
8876 decode_complex_addend (&start, &oplen, &len, &wordsz,
8877 &chunksz, &lsb0_p, &signed_p,
8878 &trunc_p, rel->r_addend);
d9352518
DB
8879
8880 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8881
8882 if (lsb0_p)
8883 shift = (start + 1) - len;
8884 else
8885 shift = (8 * wordsz) - (start + len);
8886
bb294208
AM
8887 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8888 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8889
8890#ifdef DEBUG
8891 printf ("Doing complex reloc: "
8892 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8893 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8894 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8895 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8896 oplen, (unsigned long) x, (unsigned long) mask,
8897 (unsigned long) relocation);
d9352518
DB
8898#endif
8899
cdfeee4f 8900 r = bfd_reloc_ok;
d9352518 8901 if (! trunc_p)
cdfeee4f
AM
8902 /* Now do an overflow check. */
8903 r = bfd_check_overflow ((signed_p
8904 ? complain_overflow_signed
8905 : complain_overflow_unsigned),
8906 len, 0, (8 * wordsz),
8907 relocation);
a0c8462f 8908
d9352518
DB
8909 /* Do the deed. */
8910 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8911
8912#ifdef DEBUG
8913 printf (" relocation: %8.8lx\n"
8914 " shifted mask: %8.8lx\n"
8915 " shifted/masked reloc: %8.8lx\n"
8916 " result: %8.8lx\n",
9ccb8af9
AM
8917 (unsigned long) relocation, (unsigned long) (mask << shift),
8918 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8919#endif
bb294208 8920 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 8921 return r;
d9352518
DB
8922}
8923
0e287786
AM
8924/* Functions to read r_offset from external (target order) reloc
8925 entry. Faster than bfd_getl32 et al, because we let the compiler
8926 know the value is aligned. */
53df40a4 8927
0e287786
AM
8928static bfd_vma
8929ext32l_r_offset (const void *p)
53df40a4
AM
8930{
8931 union aligned32
8932 {
8933 uint32_t v;
8934 unsigned char c[4];
8935 };
8936 const union aligned32 *a
0e287786 8937 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8938
8939 uint32_t aval = ( (uint32_t) a->c[0]
8940 | (uint32_t) a->c[1] << 8
8941 | (uint32_t) a->c[2] << 16
8942 | (uint32_t) a->c[3] << 24);
0e287786 8943 return aval;
53df40a4
AM
8944}
8945
0e287786
AM
8946static bfd_vma
8947ext32b_r_offset (const void *p)
53df40a4
AM
8948{
8949 union aligned32
8950 {
8951 uint32_t v;
8952 unsigned char c[4];
8953 };
8954 const union aligned32 *a
0e287786 8955 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8956
8957 uint32_t aval = ( (uint32_t) a->c[0] << 24
8958 | (uint32_t) a->c[1] << 16
8959 | (uint32_t) a->c[2] << 8
8960 | (uint32_t) a->c[3]);
0e287786 8961 return aval;
53df40a4
AM
8962}
8963
8964#ifdef BFD_HOST_64_BIT
0e287786
AM
8965static bfd_vma
8966ext64l_r_offset (const void *p)
53df40a4
AM
8967{
8968 union aligned64
8969 {
8970 uint64_t v;
8971 unsigned char c[8];
8972 };
8973 const union aligned64 *a
0e287786 8974 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8975
8976 uint64_t aval = ( (uint64_t) a->c[0]
8977 | (uint64_t) a->c[1] << 8
8978 | (uint64_t) a->c[2] << 16
8979 | (uint64_t) a->c[3] << 24
8980 | (uint64_t) a->c[4] << 32
8981 | (uint64_t) a->c[5] << 40
8982 | (uint64_t) a->c[6] << 48
8983 | (uint64_t) a->c[7] << 56);
0e287786 8984 return aval;
53df40a4
AM
8985}
8986
0e287786
AM
8987static bfd_vma
8988ext64b_r_offset (const void *p)
53df40a4
AM
8989{
8990 union aligned64
8991 {
8992 uint64_t v;
8993 unsigned char c[8];
8994 };
8995 const union aligned64 *a
0e287786 8996 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8997
8998 uint64_t aval = ( (uint64_t) a->c[0] << 56
8999 | (uint64_t) a->c[1] << 48
9000 | (uint64_t) a->c[2] << 40
9001 | (uint64_t) a->c[3] << 32
9002 | (uint64_t) a->c[4] << 24
9003 | (uint64_t) a->c[5] << 16
9004 | (uint64_t) a->c[6] << 8
9005 | (uint64_t) a->c[7]);
0e287786 9006 return aval;
53df40a4
AM
9007}
9008#endif
9009
c152c796
AM
9010/* When performing a relocatable link, the input relocations are
9011 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9012 referenced must be updated. Update all the relocations found in
9013 RELDATA. */
c152c796 9014
bca6d0e3 9015static bfd_boolean
c152c796 9016elf_link_adjust_relocs (bfd *abfd,
9eaff861 9017 asection *sec,
28dbcedc 9018 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9019 bfd_boolean sort,
9020 struct bfd_link_info *info)
c152c796
AM
9021{
9022 unsigned int i;
9023 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9024 bfd_byte *erela;
9025 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9026 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9027 bfd_vma r_type_mask;
9028 int r_sym_shift;
d4730f92
BS
9029 unsigned int count = reldata->count;
9030 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9031
d4730f92 9032 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9033 {
9034 swap_in = bed->s->swap_reloc_in;
9035 swap_out = bed->s->swap_reloc_out;
9036 }
d4730f92 9037 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9038 {
9039 swap_in = bed->s->swap_reloca_in;
9040 swap_out = bed->s->swap_reloca_out;
9041 }
9042 else
9043 abort ();
9044
9045 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9046 abort ();
9047
9048 if (bed->s->arch_size == 32)
9049 {
9050 r_type_mask = 0xff;
9051 r_sym_shift = 8;
9052 }
9053 else
9054 {
9055 r_type_mask = 0xffffffff;
9056 r_sym_shift = 32;
9057 }
9058
d4730f92
BS
9059 erela = reldata->hdr->contents;
9060 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9061 {
9062 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9063 unsigned int j;
9064
9065 if (*rel_hash == NULL)
9066 continue;
9067
10bbbc1d
NC
9068 if ((*rel_hash)->indx == -2
9069 && info->gc_sections
9070 && ! info->gc_keep_exported)
9071 {
9072 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9073 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9074 abfd, sec,
9075 (*rel_hash)->root.root.string);
9793eb77 9076 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9077 abfd, sec);
10bbbc1d
NC
9078 bfd_set_error (bfd_error_invalid_operation);
9079 return FALSE;
9080 }
c152c796
AM
9081 BFD_ASSERT ((*rel_hash)->indx >= 0);
9082
9083 (*swap_in) (abfd, erela, irela);
9084 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9085 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9086 | (irela[j].r_info & r_type_mask));
9087 (*swap_out) (abfd, irela, erela);
9088 }
53df40a4 9089
9eaff861
AO
9090 if (bed->elf_backend_update_relocs)
9091 (*bed->elf_backend_update_relocs) (sec, reldata);
9092
0e287786 9093 if (sort && count != 0)
53df40a4 9094 {
0e287786
AM
9095 bfd_vma (*ext_r_off) (const void *);
9096 bfd_vma r_off;
9097 size_t elt_size;
9098 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9099 bfd_byte *buf = NULL;
28dbcedc
AM
9100
9101 if (bed->s->arch_size == 32)
9102 {
9103 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9104 ext_r_off = ext32l_r_offset;
28dbcedc 9105 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9106 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9107 else
9108 abort ();
9109 }
53df40a4 9110 else
28dbcedc 9111 {
53df40a4 9112#ifdef BFD_HOST_64_BIT
28dbcedc 9113 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9114 ext_r_off = ext64l_r_offset;
28dbcedc 9115 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9116 ext_r_off = ext64b_r_offset;
28dbcedc 9117 else
53df40a4 9118#endif
28dbcedc
AM
9119 abort ();
9120 }
0e287786 9121
bca6d0e3
AM
9122 /* Must use a stable sort here. A modified insertion sort,
9123 since the relocs are mostly sorted already. */
0e287786
AM
9124 elt_size = reldata->hdr->sh_entsize;
9125 base = reldata->hdr->contents;
9126 end = base + count * elt_size;
bca6d0e3 9127 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9128 abort ();
9129
9130 /* Ensure the first element is lowest. This acts as a sentinel,
9131 speeding the main loop below. */
9132 r_off = (*ext_r_off) (base);
9133 for (p = loc = base; (p += elt_size) < end; )
9134 {
9135 bfd_vma r_off2 = (*ext_r_off) (p);
9136 if (r_off > r_off2)
9137 {
9138 r_off = r_off2;
9139 loc = p;
9140 }
9141 }
9142 if (loc != base)
9143 {
9144 /* Don't just swap *base and *loc as that changes the order
9145 of the original base[0] and base[1] if they happen to
9146 have the same r_offset. */
bca6d0e3
AM
9147 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9148 memcpy (onebuf, loc, elt_size);
0e287786 9149 memmove (base + elt_size, base, loc - base);
bca6d0e3 9150 memcpy (base, onebuf, elt_size);
0e287786
AM
9151 }
9152
b29b8669 9153 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9154 {
9155 /* base to p is sorted, *p is next to insert. */
9156 r_off = (*ext_r_off) (p);
9157 /* Search the sorted region for location to insert. */
9158 loc = p - elt_size;
9159 while (r_off < (*ext_r_off) (loc))
9160 loc -= elt_size;
9161 loc += elt_size;
9162 if (loc != p)
9163 {
bca6d0e3
AM
9164 /* Chances are there is a run of relocs to insert here,
9165 from one of more input files. Files are not always
9166 linked in order due to the way elf_link_input_bfd is
9167 called. See pr17666. */
9168 size_t sortlen = p - loc;
9169 bfd_vma r_off2 = (*ext_r_off) (loc);
9170 size_t runlen = elt_size;
9171 size_t buf_size = 96 * 1024;
9172 while (p + runlen < end
9173 && (sortlen <= buf_size
9174 || runlen + elt_size <= buf_size)
9175 && r_off2 > (*ext_r_off) (p + runlen))
9176 runlen += elt_size;
9177 if (buf == NULL)
9178 {
9179 buf = bfd_malloc (buf_size);
9180 if (buf == NULL)
9181 return FALSE;
9182 }
9183 if (runlen < sortlen)
9184 {
9185 memcpy (buf, p, runlen);
9186 memmove (loc + runlen, loc, sortlen);
9187 memcpy (loc, buf, runlen);
9188 }
9189 else
9190 {
9191 memcpy (buf, loc, sortlen);
9192 memmove (loc, p, runlen);
9193 memcpy (loc + runlen, buf, sortlen);
9194 }
b29b8669 9195 p += runlen - elt_size;
0e287786
AM
9196 }
9197 }
9198 /* Hashes are no longer valid. */
28dbcedc
AM
9199 free (reldata->hashes);
9200 reldata->hashes = NULL;
bca6d0e3 9201 free (buf);
53df40a4 9202 }
bca6d0e3 9203 return TRUE;
c152c796
AM
9204}
9205
9206struct elf_link_sort_rela
9207{
9208 union {
9209 bfd_vma offset;
9210 bfd_vma sym_mask;
9211 } u;
9212 enum elf_reloc_type_class type;
9213 /* We use this as an array of size int_rels_per_ext_rel. */
9214 Elf_Internal_Rela rela[1];
9215};
9216
dcea6a95
AM
9217/* qsort stability here and for cmp2 is only an issue if multiple
9218 dynamic relocations are emitted at the same address. But targets
9219 that apply a series of dynamic relocations each operating on the
9220 result of the prior relocation can't use -z combreloc as
9221 implemented anyway. Such schemes tend to be broken by sorting on
9222 symbol index. That leaves dynamic NONE relocs as the only other
9223 case where ld might emit multiple relocs at the same address, and
9224 those are only emitted due to target bugs. */
9225
c152c796
AM
9226static int
9227elf_link_sort_cmp1 (const void *A, const void *B)
9228{
a50b1753
NC
9229 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9230 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9231 int relativea, relativeb;
9232
9233 relativea = a->type == reloc_class_relative;
9234 relativeb = b->type == reloc_class_relative;
9235
9236 if (relativea < relativeb)
9237 return 1;
9238 if (relativea > relativeb)
9239 return -1;
9240 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9241 return -1;
9242 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9243 return 1;
9244 if (a->rela->r_offset < b->rela->r_offset)
9245 return -1;
9246 if (a->rela->r_offset > b->rela->r_offset)
9247 return 1;
9248 return 0;
9249}
9250
9251static int
9252elf_link_sort_cmp2 (const void *A, const void *B)
9253{
a50b1753
NC
9254 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9255 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9256
7e612e98 9257 if (a->type < b->type)
c152c796 9258 return -1;
7e612e98 9259 if (a->type > b->type)
c152c796 9260 return 1;
7e612e98 9261 if (a->u.offset < b->u.offset)
c152c796 9262 return -1;
7e612e98 9263 if (a->u.offset > b->u.offset)
c152c796
AM
9264 return 1;
9265 if (a->rela->r_offset < b->rela->r_offset)
9266 return -1;
9267 if (a->rela->r_offset > b->rela->r_offset)
9268 return 1;
9269 return 0;
9270}
9271
9272static size_t
9273elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9274{
3410fea8 9275 asection *dynamic_relocs;
fc66a176
L
9276 asection *rela_dyn;
9277 asection *rel_dyn;
c152c796
AM
9278 bfd_size_type count, size;
9279 size_t i, ret, sort_elt, ext_size;
9280 bfd_byte *sort, *s_non_relative, *p;
9281 struct elf_link_sort_rela *sq;
9282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9283 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9284 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9285 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9286 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9287 struct bfd_link_order *lo;
9288 bfd_vma r_sym_mask;
3410fea8 9289 bfd_boolean use_rela;
c152c796 9290
3410fea8
NC
9291 /* Find a dynamic reloc section. */
9292 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9293 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9294 if (rela_dyn != NULL && rela_dyn->size > 0
9295 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9296 {
3410fea8
NC
9297 bfd_boolean use_rela_initialised = FALSE;
9298
9299 /* This is just here to stop gcc from complaining.
c8e44c6d 9300 Its initialization checking code is not perfect. */
3410fea8
NC
9301 use_rela = TRUE;
9302
9303 /* Both sections are present. Examine the sizes
9304 of the indirect sections to help us choose. */
9305 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9306 if (lo->type == bfd_indirect_link_order)
9307 {
9308 asection *o = lo->u.indirect.section;
9309
9310 if ((o->size % bed->s->sizeof_rela) == 0)
9311 {
9312 if ((o->size % bed->s->sizeof_rel) == 0)
9313 /* Section size is divisible by both rel and rela sizes.
9314 It is of no help to us. */
9315 ;
9316 else
9317 {
9318 /* Section size is only divisible by rela. */
535b785f 9319 if (use_rela_initialised && !use_rela)
3410fea8 9320 {
9793eb77 9321 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9322 "they are in more than one size"),
9323 abfd);
3410fea8
NC
9324 bfd_set_error (bfd_error_invalid_operation);
9325 return 0;
9326 }
9327 else
9328 {
9329 use_rela = TRUE;
9330 use_rela_initialised = TRUE;
9331 }
9332 }
9333 }
9334 else if ((o->size % bed->s->sizeof_rel) == 0)
9335 {
9336 /* Section size is only divisible by rel. */
535b785f 9337 if (use_rela_initialised && use_rela)
3410fea8 9338 {
9793eb77 9339 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9340 "they are in more than one size"),
9341 abfd);
3410fea8
NC
9342 bfd_set_error (bfd_error_invalid_operation);
9343 return 0;
9344 }
9345 else
9346 {
9347 use_rela = FALSE;
9348 use_rela_initialised = TRUE;
9349 }
9350 }
9351 else
9352 {
c8e44c6d
AM
9353 /* The section size is not divisible by either -
9354 something is wrong. */
9793eb77 9355 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9356 "they are of an unknown size"), abfd);
3410fea8
NC
9357 bfd_set_error (bfd_error_invalid_operation);
9358 return 0;
9359 }
9360 }
9361
9362 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9363 if (lo->type == bfd_indirect_link_order)
9364 {
9365 asection *o = lo->u.indirect.section;
9366
9367 if ((o->size % bed->s->sizeof_rela) == 0)
9368 {
9369 if ((o->size % bed->s->sizeof_rel) == 0)
9370 /* Section size is divisible by both rel and rela sizes.
9371 It is of no help to us. */
9372 ;
9373 else
9374 {
9375 /* Section size is only divisible by rela. */
535b785f 9376 if (use_rela_initialised && !use_rela)
3410fea8 9377 {
9793eb77 9378 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9379 "they are in more than one size"),
9380 abfd);
3410fea8
NC
9381 bfd_set_error (bfd_error_invalid_operation);
9382 return 0;
9383 }
9384 else
9385 {
9386 use_rela = TRUE;
9387 use_rela_initialised = TRUE;
9388 }
9389 }
9390 }
9391 else if ((o->size % bed->s->sizeof_rel) == 0)
9392 {
9393 /* Section size is only divisible by rel. */
535b785f 9394 if (use_rela_initialised && use_rela)
3410fea8 9395 {
9793eb77 9396 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9397 "they are in more than one size"),
9398 abfd);
3410fea8
NC
9399 bfd_set_error (bfd_error_invalid_operation);
9400 return 0;
9401 }
9402 else
9403 {
9404 use_rela = FALSE;
9405 use_rela_initialised = TRUE;
9406 }
9407 }
9408 else
9409 {
c8e44c6d
AM
9410 /* The section size is not divisible by either -
9411 something is wrong. */
9793eb77 9412 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9413 "they are of an unknown size"), abfd);
3410fea8
NC
9414 bfd_set_error (bfd_error_invalid_operation);
9415 return 0;
9416 }
9417 }
9418
9419 if (! use_rela_initialised)
9420 /* Make a guess. */
9421 use_rela = TRUE;
c152c796 9422 }
fc66a176
L
9423 else if (rela_dyn != NULL && rela_dyn->size > 0)
9424 use_rela = TRUE;
9425 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9426 use_rela = FALSE;
c152c796 9427 else
fc66a176 9428 return 0;
3410fea8
NC
9429
9430 if (use_rela)
c152c796 9431 {
3410fea8 9432 dynamic_relocs = rela_dyn;
c152c796
AM
9433 ext_size = bed->s->sizeof_rela;
9434 swap_in = bed->s->swap_reloca_in;
9435 swap_out = bed->s->swap_reloca_out;
9436 }
3410fea8
NC
9437 else
9438 {
9439 dynamic_relocs = rel_dyn;
9440 ext_size = bed->s->sizeof_rel;
9441 swap_in = bed->s->swap_reloc_in;
9442 swap_out = bed->s->swap_reloc_out;
9443 }
c152c796
AM
9444
9445 size = 0;
3410fea8 9446 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9447 if (lo->type == bfd_indirect_link_order)
3410fea8 9448 size += lo->u.indirect.section->size;
c152c796 9449
3410fea8 9450 if (size != dynamic_relocs->size)
c152c796
AM
9451 return 0;
9452
9453 sort_elt = (sizeof (struct elf_link_sort_rela)
9454 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9455
9456 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9457 if (count == 0)
9458 return 0;
a50b1753 9459 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9460
c152c796
AM
9461 if (sort == NULL)
9462 {
9463 (*info->callbacks->warning)
9793eb77 9464 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9465 return 0;
9466 }
9467
9468 if (bed->s->arch_size == 32)
9469 r_sym_mask = ~(bfd_vma) 0xff;
9470 else
9471 r_sym_mask = ~(bfd_vma) 0xffffffff;
9472
3410fea8 9473 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9474 if (lo->type == bfd_indirect_link_order)
9475 {
9476 bfd_byte *erel, *erelend;
9477 asection *o = lo->u.indirect.section;
9478
1da212d6
AM
9479 if (o->contents == NULL && o->size != 0)
9480 {
9481 /* This is a reloc section that is being handled as a normal
9482 section. See bfd_section_from_shdr. We can't combine
9483 relocs in this case. */
9484 free (sort);
9485 return 0;
9486 }
c152c796 9487 erel = o->contents;
eea6121a 9488 erelend = o->contents + o->size;
c8e44c6d 9489 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9490
c152c796
AM
9491 while (erel < erelend)
9492 {
9493 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9494
c152c796 9495 (*swap_in) (abfd, erel, s->rela);
7e612e98 9496 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9497 s->u.sym_mask = r_sym_mask;
9498 p += sort_elt;
9499 erel += ext_size;
9500 }
9501 }
9502
9503 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9504
9505 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9506 {
9507 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9508 if (s->type != reloc_class_relative)
9509 break;
9510 }
9511 ret = i;
9512 s_non_relative = p;
9513
9514 sq = (struct elf_link_sort_rela *) s_non_relative;
9515 for (; i < count; i++, p += sort_elt)
9516 {
9517 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9518 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9519 sq = sp;
9520 sp->u.offset = sq->rela->r_offset;
9521 }
9522
9523 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9524
c8e44c6d
AM
9525 struct elf_link_hash_table *htab = elf_hash_table (info);
9526 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9527 {
9528 /* We have plt relocs in .rela.dyn. */
9529 sq = (struct elf_link_sort_rela *) sort;
9530 for (i = 0; i < count; i++)
9531 if (sq[count - i - 1].type != reloc_class_plt)
9532 break;
9533 if (i != 0 && htab->srelplt->size == i * ext_size)
9534 {
9535 struct bfd_link_order **plo;
9536 /* Put srelplt link_order last. This is so the output_offset
9537 set in the next loop is correct for DT_JMPREL. */
9538 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9539 if ((*plo)->type == bfd_indirect_link_order
9540 && (*plo)->u.indirect.section == htab->srelplt)
9541 {
9542 lo = *plo;
9543 *plo = lo->next;
9544 }
9545 else
9546 plo = &(*plo)->next;
9547 *plo = lo;
9548 lo->next = NULL;
9549 dynamic_relocs->map_tail.link_order = lo;
9550 }
9551 }
9552
9553 p = sort;
3410fea8 9554 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9555 if (lo->type == bfd_indirect_link_order)
9556 {
9557 bfd_byte *erel, *erelend;
9558 asection *o = lo->u.indirect.section;
9559
9560 erel = o->contents;
eea6121a 9561 erelend = o->contents + o->size;
c8e44c6d 9562 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9563 while (erel < erelend)
9564 {
9565 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9566 (*swap_out) (abfd, s->rela, erel);
9567 p += sort_elt;
9568 erel += ext_size;
9569 }
9570 }
9571
9572 free (sort);
3410fea8 9573 *psec = dynamic_relocs;
c152c796
AM
9574 return ret;
9575}
9576
ef10c3ac 9577/* Add a symbol to the output symbol string table. */
c152c796 9578
6e0b88f1 9579static int
ef10c3ac
L
9580elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9581 const char *name,
9582 Elf_Internal_Sym *elfsym,
9583 asection *input_sec,
9584 struct elf_link_hash_entry *h)
c152c796 9585{
6e0b88f1 9586 int (*output_symbol_hook)
c152c796
AM
9587 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9588 struct elf_link_hash_entry *);
ef10c3ac 9589 struct elf_link_hash_table *hash_table;
c152c796 9590 const struct elf_backend_data *bed;
ef10c3ac 9591 bfd_size_type strtabsize;
c152c796 9592
8539e4e8
AM
9593 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9594
8b127cbc 9595 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9596 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9597 if (output_symbol_hook != NULL)
9598 {
8b127cbc 9599 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9600 if (ret != 1)
9601 return ret;
c152c796
AM
9602 }
9603
06f44071
AM
9604 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9605 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9606 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9607 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9608
ef10c3ac
L
9609 if (name == NULL
9610 || *name == '\0'
9611 || (input_sec->flags & SEC_EXCLUDE))
9612 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9613 else
9614 {
ef10c3ac
L
9615 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9616 to get the final offset for st_name. */
9617 elfsym->st_name
9618 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9619 name, FALSE);
c152c796 9620 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9621 return 0;
c152c796
AM
9622 }
9623
ef10c3ac
L
9624 hash_table = elf_hash_table (flinfo->info);
9625 strtabsize = hash_table->strtabsize;
9626 if (strtabsize <= hash_table->strtabcount)
c152c796 9627 {
ef10c3ac
L
9628 strtabsize += strtabsize;
9629 hash_table->strtabsize = strtabsize;
9630 strtabsize *= sizeof (*hash_table->strtab);
9631 hash_table->strtab
9632 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9633 strtabsize);
9634 if (hash_table->strtab == NULL)
6e0b88f1 9635 return 0;
c152c796 9636 }
ef10c3ac
L
9637 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9638 hash_table->strtab[hash_table->strtabcount].dest_index
9639 = hash_table->strtabcount;
9640 hash_table->strtab[hash_table->strtabcount].destshndx_index
9641 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9642
ed48ec2e 9643 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9644 hash_table->strtabcount += 1;
9645
9646 return 1;
9647}
9648
9649/* Swap symbols out to the symbol table and flush the output symbols to
9650 the file. */
9651
9652static bfd_boolean
9653elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9654{
9655 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9656 size_t amt;
ef53be89 9657 size_t i;
ef10c3ac
L
9658 const struct elf_backend_data *bed;
9659 bfd_byte *symbuf;
9660 Elf_Internal_Shdr *hdr;
9661 file_ptr pos;
9662 bfd_boolean ret;
9663
9664 if (!hash_table->strtabcount)
9665 return TRUE;
9666
9667 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9668
9669 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9670
ef10c3ac
L
9671 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9672 symbuf = (bfd_byte *) bfd_malloc (amt);
9673 if (symbuf == NULL)
9674 return FALSE;
1b786873 9675
ef10c3ac 9676 if (flinfo->symshndxbuf)
c152c796 9677 {
ef53be89
AM
9678 amt = sizeof (Elf_External_Sym_Shndx);
9679 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9680 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9681 if (flinfo->symshndxbuf == NULL)
c152c796 9682 {
ef10c3ac
L
9683 free (symbuf);
9684 return FALSE;
c152c796 9685 }
c152c796
AM
9686 }
9687
ef10c3ac
L
9688 for (i = 0; i < hash_table->strtabcount; i++)
9689 {
9690 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9691 if (elfsym->sym.st_name == (unsigned long) -1)
9692 elfsym->sym.st_name = 0;
9693 else
9694 elfsym->sym.st_name
9695 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9696 elfsym->sym.st_name);
9697 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9698 ((bfd_byte *) symbuf
9699 + (elfsym->dest_index
9700 * bed->s->sizeof_sym)),
9701 (flinfo->symshndxbuf
9702 + elfsym->destshndx_index));
9703 }
9704
1ff6de03
NA
9705 /* Allow the linker to examine the strtab and symtab now they are
9706 populated. */
9707
9708 if (flinfo->info->callbacks->examine_strtab)
9709 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9710 hash_table->strtabcount,
9711 flinfo->symstrtab);
9712
ef10c3ac
L
9713 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9714 pos = hdr->sh_offset + hdr->sh_size;
9715 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9716 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9717 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9718 {
9719 hdr->sh_size += amt;
9720 ret = TRUE;
9721 }
9722 else
9723 ret = FALSE;
c152c796 9724
ef10c3ac
L
9725 free (symbuf);
9726
9727 free (hash_table->strtab);
9728 hash_table->strtab = NULL;
9729
9730 return ret;
c152c796
AM
9731}
9732
c0d5a53d
L
9733/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9734
9735static bfd_boolean
9736check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9737{
4fbb74a6
AM
9738 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9739 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9740 {
9741 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9742 beyond 64k. */
4eca0228 9743 _bfd_error_handler
695344c0 9744 /* xgettext:c-format */
9793eb77 9745 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9746 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9747 bfd_set_error (bfd_error_nonrepresentable_section);
9748 return FALSE;
9749 }
9750 return TRUE;
9751}
9752
c152c796
AM
9753/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9754 allowing an unsatisfied unversioned symbol in the DSO to match a
9755 versioned symbol that would normally require an explicit version.
9756 We also handle the case that a DSO references a hidden symbol
9757 which may be satisfied by a versioned symbol in another DSO. */
9758
9759static bfd_boolean
9760elf_link_check_versioned_symbol (struct bfd_link_info *info,
9761 const struct elf_backend_data *bed,
9762 struct elf_link_hash_entry *h)
9763{
9764 bfd *abfd;
9765 struct elf_link_loaded_list *loaded;
9766
9767 if (!is_elf_hash_table (info->hash))
9768 return FALSE;
9769
90c984fc
L
9770 /* Check indirect symbol. */
9771 while (h->root.type == bfd_link_hash_indirect)
9772 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9773
c152c796
AM
9774 switch (h->root.type)
9775 {
9776 default:
9777 abfd = NULL;
9778 break;
9779
9780 case bfd_link_hash_undefined:
9781 case bfd_link_hash_undefweak:
9782 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9783 if (abfd == NULL
9784 || (abfd->flags & DYNAMIC) == 0
e56f61be 9785 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9786 return FALSE;
9787 break;
9788
9789 case bfd_link_hash_defined:
9790 case bfd_link_hash_defweak:
9791 abfd = h->root.u.def.section->owner;
9792 break;
9793
9794 case bfd_link_hash_common:
9795 abfd = h->root.u.c.p->section->owner;
9796 break;
9797 }
9798 BFD_ASSERT (abfd != NULL);
9799
e310298c 9800 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
9801 loaded != NULL;
9802 loaded = loaded->next)
9803 {
9804 bfd *input;
9805 Elf_Internal_Shdr *hdr;
ef53be89
AM
9806 size_t symcount;
9807 size_t extsymcount;
9808 size_t extsymoff;
c152c796
AM
9809 Elf_Internal_Shdr *versymhdr;
9810 Elf_Internal_Sym *isym;
9811 Elf_Internal_Sym *isymend;
9812 Elf_Internal_Sym *isymbuf;
9813 Elf_External_Versym *ever;
9814 Elf_External_Versym *extversym;
9815
9816 input = loaded->abfd;
9817
9818 /* We check each DSO for a possible hidden versioned definition. */
9819 if (input == abfd
c152c796
AM
9820 || elf_dynversym (input) == 0)
9821 continue;
9822
9823 hdr = &elf_tdata (input)->dynsymtab_hdr;
9824
9825 symcount = hdr->sh_size / bed->s->sizeof_sym;
9826 if (elf_bad_symtab (input))
9827 {
9828 extsymcount = symcount;
9829 extsymoff = 0;
9830 }
9831 else
9832 {
9833 extsymcount = symcount - hdr->sh_info;
9834 extsymoff = hdr->sh_info;
9835 }
9836
9837 if (extsymcount == 0)
9838 continue;
9839
9840 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9841 NULL, NULL, NULL);
9842 if (isymbuf == NULL)
9843 return FALSE;
9844
9845 /* Read in any version definitions. */
9846 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 9847 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
9848 || (extversym = (Elf_External_Versym *)
9849 _bfd_malloc_and_read (input, versymhdr->sh_size,
9850 versymhdr->sh_size)) == NULL)
c152c796 9851 {
c152c796
AM
9852 free (isymbuf);
9853 return FALSE;
9854 }
9855
9856 ever = extversym + extsymoff;
9857 isymend = isymbuf + extsymcount;
9858 for (isym = isymbuf; isym < isymend; isym++, ever++)
9859 {
9860 const char *name;
9861 Elf_Internal_Versym iver;
9862 unsigned short version_index;
9863
9864 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9865 || isym->st_shndx == SHN_UNDEF)
9866 continue;
9867
9868 name = bfd_elf_string_from_elf_section (input,
9869 hdr->sh_link,
9870 isym->st_name);
9871 if (strcmp (name, h->root.root.string) != 0)
9872 continue;
9873
9874 _bfd_elf_swap_versym_in (input, ever, &iver);
9875
d023c380
L
9876 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9877 && !(h->def_regular
9878 && h->forced_local))
c152c796
AM
9879 {
9880 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9881 have provided a definition for the undefined sym unless
9882 it is defined in a non-shared object and forced local.
9883 */
c152c796
AM
9884 abort ();
9885 }
9886
9887 version_index = iver.vs_vers & VERSYM_VERSION;
9888 if (version_index == 1 || version_index == 2)
9889 {
9890 /* This is the base or first version. We can use it. */
9891 free (extversym);
9892 free (isymbuf);
9893 return TRUE;
9894 }
9895 }
9896
9897 free (extversym);
9898 free (isymbuf);
9899 }
9900
9901 return FALSE;
9902}
9903
b8871f35
L
9904/* Convert ELF common symbol TYPE. */
9905
9906static int
9907elf_link_convert_common_type (struct bfd_link_info *info, int type)
9908{
9909 /* Commom symbol can only appear in relocatable link. */
9910 if (!bfd_link_relocatable (info))
9911 abort ();
9912 switch (info->elf_stt_common)
9913 {
9914 case unchanged:
9915 break;
9916 case elf_stt_common:
9917 type = STT_COMMON;
9918 break;
9919 case no_elf_stt_common:
9920 type = STT_OBJECT;
9921 break;
9922 }
9923 return type;
9924}
9925
c152c796
AM
9926/* Add an external symbol to the symbol table. This is called from
9927 the hash table traversal routine. When generating a shared object,
9928 we go through the symbol table twice. The first time we output
9929 anything that might have been forced to local scope in a version
9930 script. The second time we output the symbols that are still
9931 global symbols. */
9932
9933static bfd_boolean
7686d77d 9934elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9935{
7686d77d 9936 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9937 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9938 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9939 bfd_boolean strip;
9940 Elf_Internal_Sym sym;
9941 asection *input_sec;
9942 const struct elf_backend_data *bed;
6e0b88f1
AM
9943 long indx;
9944 int ret;
b8871f35 9945 unsigned int type;
c152c796
AM
9946
9947 if (h->root.type == bfd_link_hash_warning)
9948 {
9949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9950 if (h->root.type == bfd_link_hash_new)
9951 return TRUE;
9952 }
9953
9954 /* Decide whether to output this symbol in this pass. */
9955 if (eoinfo->localsyms)
9956 {
4deb8f71 9957 if (!h->forced_local)
c152c796
AM
9958 return TRUE;
9959 }
9960 else
9961 {
4deb8f71 9962 if (h->forced_local)
c152c796
AM
9963 return TRUE;
9964 }
9965
8b127cbc 9966 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9967
12ac1cf5 9968 if (h->root.type == bfd_link_hash_undefined)
c152c796 9969 {
12ac1cf5
NC
9970 /* If we have an undefined symbol reference here then it must have
9971 come from a shared library that is being linked in. (Undefined
98da7939
L
9972 references in regular files have already been handled unless
9973 they are in unreferenced sections which are removed by garbage
9974 collection). */
12ac1cf5
NC
9975 bfd_boolean ignore_undef = FALSE;
9976
9977 /* Some symbols may be special in that the fact that they're
9978 undefined can be safely ignored - let backend determine that. */
9979 if (bed->elf_backend_ignore_undef_symbol)
9980 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9981
9982 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9983 if (!ignore_undef
c54f1524 9984 && h->ref_dynamic_nonweak
8b127cbc
AM
9985 && (!h->ref_regular || flinfo->info->gc_sections)
9986 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9987 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
9988 {
9989 flinfo->info->callbacks->undefined_symbol
9990 (flinfo->info, h->root.root.string,
9991 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
9992 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
9993 && !flinfo->info->warn_unresolved_syms);
9994 }
97196564
L
9995
9996 /* Strip a global symbol defined in a discarded section. */
9997 if (h->indx == -3)
9998 return TRUE;
c152c796
AM
9999 }
10000
10001 /* We should also warn if a forced local symbol is referenced from
10002 shared libraries. */
0e1862bb 10003 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10004 && h->forced_local
10005 && h->ref_dynamic
371a5866 10006 && h->def_regular
f5385ebf 10007 && !h->dynamic_def
ee659f1f 10008 && h->ref_dynamic_nonweak
8b127cbc 10009 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10010 {
17d078c5
AM
10011 bfd *def_bfd;
10012 const char *msg;
90c984fc
L
10013 struct elf_link_hash_entry *hi = h;
10014
10015 /* Check indirect symbol. */
10016 while (hi->root.type == bfd_link_hash_indirect)
10017 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10018
10019 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10020 /* xgettext:c-format */
871b3ab2 10021 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10022 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10023 /* xgettext:c-format */
871b3ab2 10024 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10025 else
695344c0 10026 /* xgettext:c-format */
871b3ab2 10027 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10028 def_bfd = flinfo->output_bfd;
90c984fc
L
10029 if (hi->root.u.def.section != bfd_abs_section_ptr)
10030 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10031 _bfd_error_handler (msg, flinfo->output_bfd,
10032 h->root.root.string, def_bfd);
17d078c5 10033 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10034 eoinfo->failed = TRUE;
10035 return FALSE;
10036 }
10037
10038 /* We don't want to output symbols that have never been mentioned by
10039 a regular file, or that we have been told to strip. However, if
10040 h->indx is set to -2, the symbol is used by a reloc and we must
10041 output it. */
d983c8c5 10042 strip = FALSE;
c152c796 10043 if (h->indx == -2)
d983c8c5 10044 ;
f5385ebf 10045 else if ((h->def_dynamic
77cfaee6
AM
10046 || h->ref_dynamic
10047 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10048 && !h->def_regular
10049 && !h->ref_regular)
c152c796 10050 strip = TRUE;
8b127cbc 10051 else if (flinfo->info->strip == strip_all)
c152c796 10052 strip = TRUE;
8b127cbc
AM
10053 else if (flinfo->info->strip == strip_some
10054 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10055 h->root.root.string, FALSE, FALSE) == NULL)
10056 strip = TRUE;
d56d55e7
AM
10057 else if ((h->root.type == bfd_link_hash_defined
10058 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10059 && ((flinfo->info->strip_discarded
dbaa2011 10060 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10061 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10062 && h->root.u.def.section->owner != NULL
d56d55e7 10063 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10064 strip = TRUE;
9e2278f5
AM
10065 else if ((h->root.type == bfd_link_hash_undefined
10066 || h->root.type == bfd_link_hash_undefweak)
10067 && h->root.u.undef.abfd != NULL
10068 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10069 strip = TRUE;
c152c796 10070
b8871f35
L
10071 type = h->type;
10072
c152c796 10073 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10074 nothing else to do. However, if it is a forced local symbol or
10075 an ifunc symbol we need to give the backend finish_dynamic_symbol
10076 function a chance to make it dynamic. */
c152c796
AM
10077 if (strip
10078 && h->dynindx == -1
b8871f35 10079 && type != STT_GNU_IFUNC
f5385ebf 10080 && !h->forced_local)
c152c796
AM
10081 return TRUE;
10082
10083 sym.st_value = 0;
10084 sym.st_size = h->size;
10085 sym.st_other = h->other;
c152c796
AM
10086 switch (h->root.type)
10087 {
10088 default:
10089 case bfd_link_hash_new:
10090 case bfd_link_hash_warning:
10091 abort ();
10092 return FALSE;
10093
10094 case bfd_link_hash_undefined:
10095 case bfd_link_hash_undefweak:
10096 input_sec = bfd_und_section_ptr;
10097 sym.st_shndx = SHN_UNDEF;
10098 break;
10099
10100 case bfd_link_hash_defined:
10101 case bfd_link_hash_defweak:
10102 {
10103 input_sec = h->root.u.def.section;
10104 if (input_sec->output_section != NULL)
10105 {
10106 sym.st_shndx =
8b127cbc 10107 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10108 input_sec->output_section);
10109 if (sym.st_shndx == SHN_BAD)
10110 {
4eca0228 10111 _bfd_error_handler
695344c0 10112 /* xgettext:c-format */
871b3ab2 10113 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10114 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10115 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10116 eoinfo->failed = TRUE;
10117 return FALSE;
10118 }
10119
10120 /* ELF symbols in relocatable files are section relative,
10121 but in nonrelocatable files they are virtual
10122 addresses. */
10123 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10124 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10125 {
10126 sym.st_value += input_sec->output_section->vma;
10127 if (h->type == STT_TLS)
10128 {
8b127cbc 10129 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10130 if (tls_sec != NULL)
10131 sym.st_value -= tls_sec->vma;
c152c796
AM
10132 }
10133 }
10134 }
10135 else
10136 {
10137 BFD_ASSERT (input_sec->owner == NULL
10138 || (input_sec->owner->flags & DYNAMIC) != 0);
10139 sym.st_shndx = SHN_UNDEF;
10140 input_sec = bfd_und_section_ptr;
10141 }
10142 }
10143 break;
10144
10145 case bfd_link_hash_common:
10146 input_sec = h->root.u.c.p->section;
a4d8e49b 10147 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10148 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10149 break;
10150
10151 case bfd_link_hash_indirect:
10152 /* These symbols are created by symbol versioning. They point
10153 to the decorated version of the name. For example, if the
10154 symbol foo@@GNU_1.2 is the default, which should be used when
10155 foo is used with no version, then we add an indirect symbol
10156 foo which points to foo@@GNU_1.2. We ignore these symbols,
10157 since the indirected symbol is already in the hash table. */
10158 return TRUE;
10159 }
10160
b8871f35
L
10161 if (type == STT_COMMON || type == STT_OBJECT)
10162 switch (h->root.type)
10163 {
10164 case bfd_link_hash_common:
10165 type = elf_link_convert_common_type (flinfo->info, type);
10166 break;
10167 case bfd_link_hash_defined:
10168 case bfd_link_hash_defweak:
10169 if (bed->common_definition (&sym))
10170 type = elf_link_convert_common_type (flinfo->info, type);
10171 else
10172 type = STT_OBJECT;
10173 break;
10174 case bfd_link_hash_undefined:
10175 case bfd_link_hash_undefweak:
10176 break;
10177 default:
10178 abort ();
10179 }
10180
4deb8f71 10181 if (h->forced_local)
b8871f35
L
10182 {
10183 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10184 /* Turn off visibility on local symbol. */
10185 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10186 }
10187 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10188 else if (h->unique_global && h->def_regular)
10189 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10190 else if (h->root.type == bfd_link_hash_undefweak
10191 || h->root.type == bfd_link_hash_defweak)
10192 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10193 else
10194 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10195 sym.st_target_internal = h->target_internal;
10196
c152c796
AM
10197 /* Give the processor backend a chance to tweak the symbol value,
10198 and also to finish up anything that needs to be done for this
10199 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10200 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10201 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10202 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10203 && h->def_regular
0e1862bb 10204 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10205 || ((h->dynindx != -1
10206 || h->forced_local)
0e1862bb 10207 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10208 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10209 || h->root.type != bfd_link_hash_undefweak))
10210 || !h->forced_local)
8b127cbc 10211 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10212 {
10213 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10214 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10215 {
10216 eoinfo->failed = TRUE;
10217 return FALSE;
10218 }
10219 }
10220
10221 /* If we are marking the symbol as undefined, and there are no
10222 non-weak references to this symbol from a regular object, then
10223 mark the symbol as weak undefined; if there are non-weak
10224 references, mark the symbol as strong. We can't do this earlier,
10225 because it might not be marked as undefined until the
10226 finish_dynamic_symbol routine gets through with it. */
10227 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10228 && h->ref_regular
c152c796
AM
10229 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10230 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10231 {
10232 int bindtype;
b8871f35 10233 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10234
10235 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10236 if (type == STT_GNU_IFUNC)
10237 type = STT_FUNC;
c152c796 10238
f5385ebf 10239 if (h->ref_regular_nonweak)
c152c796
AM
10240 bindtype = STB_GLOBAL;
10241 else
10242 bindtype = STB_WEAK;
2955ec4c 10243 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10244 }
10245
bda987c2
CD
10246 /* If this is a symbol defined in a dynamic library, don't use the
10247 symbol size from the dynamic library. Relinking an executable
10248 against a new library may introduce gratuitous changes in the
10249 executable's symbols if we keep the size. */
10250 if (sym.st_shndx == SHN_UNDEF
10251 && !h->def_regular
10252 && h->def_dynamic)
10253 sym.st_size = 0;
10254
c152c796
AM
10255 /* If a non-weak symbol with non-default visibility is not defined
10256 locally, it is a fatal error. */
0e1862bb 10257 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10258 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10259 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10260 && h->root.type == bfd_link_hash_undefined
f5385ebf 10261 && !h->def_regular)
c152c796 10262 {
17d078c5
AM
10263 const char *msg;
10264
10265 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10266 /* xgettext:c-format */
871b3ab2 10267 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10268 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10269 /* xgettext:c-format */
871b3ab2 10270 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10271 else
695344c0 10272 /* xgettext:c-format */
871b3ab2 10273 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10274 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10275 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10276 eoinfo->failed = TRUE;
10277 return FALSE;
10278 }
10279
10280 /* If this symbol should be put in the .dynsym section, then put it
10281 there now. We already know the symbol index. We also fill in
10282 the entry in the .hash section. */
1c2649ed
EB
10283 if (h->dynindx != -1
10284 && elf_hash_table (flinfo->info)->dynamic_sections_created
10285 && elf_hash_table (flinfo->info)->dynsym != NULL
10286 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10287 {
c152c796
AM
10288 bfd_byte *esym;
10289
90c984fc
L
10290 /* Since there is no version information in the dynamic string,
10291 if there is no version info in symbol version section, we will
1659f720 10292 have a run-time problem if not linking executable, referenced
4deb8f71 10293 by shared library, or not bound locally. */
1659f720 10294 if (h->verinfo.verdef == NULL
0e1862bb 10295 && (!bfd_link_executable (flinfo->info)
1659f720
L
10296 || h->ref_dynamic
10297 || !h->def_regular))
90c984fc
L
10298 {
10299 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10300
10301 if (p && p [1] != '\0')
10302 {
4eca0228 10303 _bfd_error_handler
695344c0 10304 /* xgettext:c-format */
9793eb77 10305 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10306 flinfo->output_bfd, h->root.root.string);
10307 eoinfo->failed = TRUE;
10308 return FALSE;
10309 }
10310 }
10311
c152c796 10312 sym.st_name = h->dynstr_index;
cae1fbbb
L
10313 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10314 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10315 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10316 {
10317 eoinfo->failed = TRUE;
10318 return FALSE;
10319 }
8b127cbc 10320 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10321
8b127cbc 10322 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10323 {
10324 size_t hash_entry_size;
10325 bfd_byte *bucketpos;
10326 bfd_vma chain;
41198d0c
L
10327 size_t bucketcount;
10328 size_t bucket;
10329
8b127cbc 10330 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10331 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10332
10333 hash_entry_size
8b127cbc
AM
10334 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10335 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10336 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10337 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10338 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10339 bucketpos);
10340 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10341 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10342 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10343 }
c152c796 10344
8b127cbc 10345 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10346 {
10347 Elf_Internal_Versym iversym;
10348 Elf_External_Versym *eversym;
10349
5fa370e4 10350 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10351 {
7b20f099
AM
10352 if (h->verinfo.verdef == NULL
10353 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10354 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10355 iversym.vs_vers = 0;
10356 else
10357 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10358 }
10359 else
10360 {
10361 if (h->verinfo.vertree == NULL)
10362 iversym.vs_vers = 1;
10363 else
10364 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10365 if (flinfo->info->create_default_symver)
3e3b46e5 10366 iversym.vs_vers++;
c152c796
AM
10367 }
10368
422f1182 10369 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10370 defined locally. */
422f1182 10371 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10372 iversym.vs_vers |= VERSYM_HIDDEN;
10373
8b127cbc 10374 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10375 eversym += h->dynindx;
8b127cbc 10376 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10377 }
10378 }
10379
d983c8c5
AM
10380 /* If the symbol is undefined, and we didn't output it to .dynsym,
10381 strip it from .symtab too. Obviously we can't do this for
10382 relocatable output or when needed for --emit-relocs. */
10383 else if (input_sec == bfd_und_section_ptr
10384 && h->indx != -2
66cae560
NC
10385 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10386 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10387 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10388 return TRUE;
66cae560 10389
d983c8c5
AM
10390 /* Also strip others that we couldn't earlier due to dynamic symbol
10391 processing. */
10392 if (strip)
10393 return TRUE;
10394 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10395 return TRUE;
10396
2ec55de3
AM
10397 /* Output a FILE symbol so that following locals are not associated
10398 with the wrong input file. We need one for forced local symbols
10399 if we've seen more than one FILE symbol or when we have exactly
10400 one FILE symbol but global symbols are present in a file other
10401 than the one with the FILE symbol. We also need one if linker
10402 defined symbols are present. In practice these conditions are
10403 always met, so just emit the FILE symbol unconditionally. */
10404 if (eoinfo->localsyms
10405 && !eoinfo->file_sym_done
10406 && eoinfo->flinfo->filesym_count != 0)
10407 {
10408 Elf_Internal_Sym fsym;
10409
10410 memset (&fsym, 0, sizeof (fsym));
10411 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10412 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10413 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10414 bfd_und_section_ptr, NULL))
2ec55de3
AM
10415 return FALSE;
10416
10417 eoinfo->file_sym_done = TRUE;
10418 }
10419
8b127cbc 10420 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10421 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10422 input_sec, h);
6e0b88f1 10423 if (ret == 0)
c152c796
AM
10424 {
10425 eoinfo->failed = TRUE;
10426 return FALSE;
10427 }
6e0b88f1
AM
10428 else if (ret == 1)
10429 h->indx = indx;
10430 else if (h->indx == -2)
10431 abort();
c152c796
AM
10432
10433 return TRUE;
10434}
10435
cdd3575c
AM
10436/* Return TRUE if special handling is done for relocs in SEC against
10437 symbols defined in discarded sections. */
10438
c152c796
AM
10439static bfd_boolean
10440elf_section_ignore_discarded_relocs (asection *sec)
10441{
10442 const struct elf_backend_data *bed;
10443
cdd3575c
AM
10444 switch (sec->sec_info_type)
10445 {
dbaa2011
AM
10446 case SEC_INFO_TYPE_STABS:
10447 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10448 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10449 return TRUE;
10450 default:
10451 break;
10452 }
c152c796
AM
10453
10454 bed = get_elf_backend_data (sec->owner);
10455 if (bed->elf_backend_ignore_discarded_relocs != NULL
10456 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10457 return TRUE;
10458
10459 return FALSE;
10460}
10461
9e66c942
AM
10462/* Return a mask saying how ld should treat relocations in SEC against
10463 symbols defined in discarded sections. If this function returns
10464 COMPLAIN set, ld will issue a warning message. If this function
10465 returns PRETEND set, and the discarded section was link-once and the
10466 same size as the kept link-once section, ld will pretend that the
10467 symbol was actually defined in the kept section. Otherwise ld will
10468 zero the reloc (at least that is the intent, but some cooperation by
10469 the target dependent code is needed, particularly for REL targets). */
10470
8a696751
AM
10471unsigned int
10472_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10473{
9e66c942 10474 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10475 return PRETEND;
cdd3575c
AM
10476
10477 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10478 return 0;
cdd3575c
AM
10479
10480 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10481 return 0;
cdd3575c 10482
9e66c942 10483 return COMPLAIN | PRETEND;
cdd3575c
AM
10484}
10485
3d7f7666
L
10486/* Find a match between a section and a member of a section group. */
10487
10488static asection *
c0f00686
L
10489match_group_member (asection *sec, asection *group,
10490 struct bfd_link_info *info)
3d7f7666
L
10491{
10492 asection *first = elf_next_in_group (group);
10493 asection *s = first;
10494
10495 while (s != NULL)
10496 {
c0f00686 10497 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10498 return s;
10499
83180ade 10500 s = elf_next_in_group (s);
3d7f7666
L
10501 if (s == first)
10502 break;
10503 }
10504
10505 return NULL;
10506}
10507
01b3c8ab 10508/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10509 to replace it. Return the replacement if it is OK. Otherwise return
10510 NULL. */
01b3c8ab
L
10511
10512asection *
c0f00686 10513_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10514{
10515 asection *kept;
10516
10517 kept = sec->kept_section;
10518 if (kept != NULL)
10519 {
c2370991 10520 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10521 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10522 if (kept != NULL
10523 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10524 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10525 kept = NULL;
c2370991 10526 sec->kept_section = kept;
01b3c8ab
L
10527 }
10528 return kept;
10529}
10530
c152c796
AM
10531/* Link an input file into the linker output file. This function
10532 handles all the sections and relocations of the input file at once.
10533 This is so that we only have to read the local symbols once, and
10534 don't have to keep them in memory. */
10535
10536static bfd_boolean
8b127cbc 10537elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10538{
ece5ef60 10539 int (*relocate_section)
c152c796
AM
10540 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10541 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10542 bfd *output_bfd;
10543 Elf_Internal_Shdr *symtab_hdr;
10544 size_t locsymcount;
10545 size_t extsymoff;
10546 Elf_Internal_Sym *isymbuf;
10547 Elf_Internal_Sym *isym;
10548 Elf_Internal_Sym *isymend;
10549 long *pindex;
10550 asection **ppsection;
10551 asection *o;
10552 const struct elf_backend_data *bed;
c152c796 10553 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10554 bfd_size_type address_size;
10555 bfd_vma r_type_mask;
10556 int r_sym_shift;
ffbc01cc 10557 bfd_boolean have_file_sym = FALSE;
c152c796 10558
8b127cbc 10559 output_bfd = flinfo->output_bfd;
c152c796
AM
10560 bed = get_elf_backend_data (output_bfd);
10561 relocate_section = bed->elf_backend_relocate_section;
10562
10563 /* If this is a dynamic object, we don't want to do anything here:
10564 we don't want the local symbols, and we don't want the section
10565 contents. */
10566 if ((input_bfd->flags & DYNAMIC) != 0)
10567 return TRUE;
10568
c152c796
AM
10569 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10570 if (elf_bad_symtab (input_bfd))
10571 {
10572 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10573 extsymoff = 0;
10574 }
10575 else
10576 {
10577 locsymcount = symtab_hdr->sh_info;
10578 extsymoff = symtab_hdr->sh_info;
10579 }
10580
10581 /* Read the local symbols. */
10582 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10583 if (isymbuf == NULL && locsymcount != 0)
10584 {
10585 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10586 flinfo->internal_syms,
10587 flinfo->external_syms,
10588 flinfo->locsym_shndx);
c152c796
AM
10589 if (isymbuf == NULL)
10590 return FALSE;
10591 }
10592
10593 /* Find local symbol sections and adjust values of symbols in
10594 SEC_MERGE sections. Write out those local symbols we know are
10595 going into the output file. */
10596 isymend = isymbuf + locsymcount;
8b127cbc 10597 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10598 isym < isymend;
10599 isym++, pindex++, ppsection++)
10600 {
10601 asection *isec;
10602 const char *name;
10603 Elf_Internal_Sym osym;
6e0b88f1
AM
10604 long indx;
10605 int ret;
c152c796
AM
10606
10607 *pindex = -1;
10608
10609 if (elf_bad_symtab (input_bfd))
10610 {
10611 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10612 {
10613 *ppsection = NULL;
10614 continue;
10615 }
10616 }
10617
10618 if (isym->st_shndx == SHN_UNDEF)
10619 isec = bfd_und_section_ptr;
c152c796
AM
10620 else if (isym->st_shndx == SHN_ABS)
10621 isec = bfd_abs_section_ptr;
10622 else if (isym->st_shndx == SHN_COMMON)
10623 isec = bfd_com_section_ptr;
10624 else
10625 {
cb33740c
AM
10626 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10627 if (isec == NULL)
10628 {
10629 /* Don't attempt to output symbols with st_shnx in the
10630 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10631 isec = bfd_und_section_ptr;
cb33740c 10632 }
dbaa2011 10633 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10634 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10635 isym->st_value =
10636 _bfd_merged_section_offset (output_bfd, &isec,
10637 elf_section_data (isec)->sec_info,
10638 isym->st_value);
c152c796
AM
10639 }
10640
10641 *ppsection = isec;
10642
d983c8c5
AM
10643 /* Don't output the first, undefined, symbol. In fact, don't
10644 output any undefined local symbol. */
10645 if (isec == bfd_und_section_ptr)
c152c796
AM
10646 continue;
10647
10648 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10649 {
10650 /* We never output section symbols. Instead, we use the
10651 section symbol of the corresponding section in the output
10652 file. */
10653 continue;
10654 }
10655
10656 /* If we are stripping all symbols, we don't want to output this
10657 one. */
8b127cbc 10658 if (flinfo->info->strip == strip_all)
c152c796
AM
10659 continue;
10660
10661 /* If we are discarding all local symbols, we don't want to
10662 output this one. If we are generating a relocatable output
10663 file, then some of the local symbols may be required by
10664 relocs; we output them below as we discover that they are
10665 needed. */
8b127cbc 10666 if (flinfo->info->discard == discard_all)
c152c796
AM
10667 continue;
10668
10669 /* If this symbol is defined in a section which we are
f02571c5 10670 discarding, we don't need to keep it. */
abf874aa
CL
10671 if (isym->st_shndx != SHN_UNDEF
10672 && isym->st_shndx < SHN_LORESERVE
10673 && isec->output_section == NULL
10674 && flinfo->info->non_contiguous_regions
10675 && flinfo->info->non_contiguous_regions_warnings)
10676 {
10677 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10678 "discards section `%s' from '%s'\n"),
765cf5f6 10679 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10680 continue;
10681 }
10682
f02571c5 10683 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10684 && isym->st_shndx < SHN_LORESERVE
10685 && bfd_section_removed_from_list (output_bfd,
10686 isec->output_section))
e75a280b
L
10687 continue;
10688
c152c796
AM
10689 /* Get the name of the symbol. */
10690 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10691 isym->st_name);
10692 if (name == NULL)
10693 return FALSE;
10694
10695 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10696 if ((flinfo->info->strip == strip_some
10697 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10698 == NULL))
8b127cbc 10699 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10700 && (isec->flags & SEC_MERGE)
10701 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10702 || flinfo->info->discard == discard_l)
c152c796
AM
10703 && bfd_is_local_label_name (input_bfd, name)))
10704 continue;
10705
ffbc01cc
AM
10706 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10707 {
ce875075
AM
10708 if (input_bfd->lto_output)
10709 /* -flto puts a temp file name here. This means builds
10710 are not reproducible. Discard the symbol. */
10711 continue;
ffbc01cc
AM
10712 have_file_sym = TRUE;
10713 flinfo->filesym_count += 1;
10714 }
10715 if (!have_file_sym)
10716 {
10717 /* In the absence of debug info, bfd_find_nearest_line uses
10718 FILE symbols to determine the source file for local
10719 function symbols. Provide a FILE symbol here if input
10720 files lack such, so that their symbols won't be
10721 associated with a previous input file. It's not the
10722 source file, but the best we can do. */
10723 have_file_sym = TRUE;
10724 flinfo->filesym_count += 1;
10725 memset (&osym, 0, sizeof (osym));
10726 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10727 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10728 if (!elf_link_output_symstrtab (flinfo,
10729 (input_bfd->lto_output ? NULL
765cf5f6 10730 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10731 &osym, bfd_abs_section_ptr,
10732 NULL))
ffbc01cc
AM
10733 return FALSE;
10734 }
10735
c152c796
AM
10736 osym = *isym;
10737
10738 /* Adjust the section index for the output file. */
10739 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10740 isec->output_section);
10741 if (osym.st_shndx == SHN_BAD)
10742 return FALSE;
10743
c152c796
AM
10744 /* ELF symbols in relocatable files are section relative, but
10745 in executable files they are virtual addresses. Note that
10746 this code assumes that all ELF sections have an associated
10747 BFD section with a reasonable value for output_offset; below
10748 we assume that they also have a reasonable value for
10749 output_section. Any special sections must be set up to meet
10750 these requirements. */
10751 osym.st_value += isec->output_offset;
0e1862bb 10752 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10753 {
10754 osym.st_value += isec->output_section->vma;
10755 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10756 {
10757 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10758 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10759 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10760 else
10761 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10762 STT_NOTYPE);
c152c796
AM
10763 }
10764 }
10765
6e0b88f1 10766 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10767 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10768 if (ret == 0)
c152c796 10769 return FALSE;
6e0b88f1
AM
10770 else if (ret == 1)
10771 *pindex = indx;
c152c796
AM
10772 }
10773
310fd250
L
10774 if (bed->s->arch_size == 32)
10775 {
10776 r_type_mask = 0xff;
10777 r_sym_shift = 8;
10778 address_size = 4;
10779 }
10780 else
10781 {
10782 r_type_mask = 0xffffffff;
10783 r_sym_shift = 32;
10784 address_size = 8;
10785 }
10786
c152c796
AM
10787 /* Relocate the contents of each section. */
10788 sym_hashes = elf_sym_hashes (input_bfd);
10789 for (o = input_bfd->sections; o != NULL; o = o->next)
10790 {
10791 bfd_byte *contents;
10792
10793 if (! o->linker_mark)
10794 {
10795 /* This section was omitted from the link. */
10796 continue;
10797 }
10798
7bdf4127 10799 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10800 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10801 {
10802 /* Deal with the group signature symbol. */
10803 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10804 unsigned long symndx = sec_data->this_hdr.sh_info;
10805 asection *osec = o->output_section;
10806
7bdf4127 10807 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10808 if (symndx >= locsymcount
10809 || (elf_bad_symtab (input_bfd)
8b127cbc 10810 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10811 {
10812 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10813 while (h->root.type == bfd_link_hash_indirect
10814 || h->root.type == bfd_link_hash_warning)
10815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10816 /* Arrange for symbol to be output. */
10817 h->indx = -2;
10818 elf_section_data (osec)->this_hdr.sh_info = -2;
10819 }
10820 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10821 {
10822 /* We'll use the output section target_index. */
8b127cbc 10823 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10824 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10825 }
10826 else
10827 {
8b127cbc 10828 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10829 {
10830 /* Otherwise output the local symbol now. */
10831 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10832 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10833 const char *name;
6e0b88f1
AM
10834 long indx;
10835 int ret;
bcacc0f5
AM
10836
10837 name = bfd_elf_string_from_elf_section (input_bfd,
10838 symtab_hdr->sh_link,
10839 sym.st_name);
10840 if (name == NULL)
10841 return FALSE;
10842
10843 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10844 sec);
10845 if (sym.st_shndx == SHN_BAD)
10846 return FALSE;
10847
10848 sym.st_value += o->output_offset;
10849
6e0b88f1 10850 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10851 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10852 NULL);
6e0b88f1 10853 if (ret == 0)
bcacc0f5 10854 return FALSE;
6e0b88f1 10855 else if (ret == 1)
8b127cbc 10856 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10857 else
10858 abort ();
bcacc0f5
AM
10859 }
10860 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10861 = flinfo->indices[symndx];
bcacc0f5
AM
10862 }
10863 }
10864
c152c796 10865 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10866 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10867 continue;
10868
10869 if ((o->flags & SEC_LINKER_CREATED) != 0)
10870 {
10871 /* Section was created by _bfd_elf_link_create_dynamic_sections
10872 or somesuch. */
10873 continue;
10874 }
10875
10876 /* Get the contents of the section. They have been cached by a
10877 relaxation routine. Note that o is a section in an input
10878 file, so the contents field will not have been set by any of
10879 the routines which work on output files. */
10880 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10881 {
10882 contents = elf_section_data (o)->this_hdr.contents;
10883 if (bed->caches_rawsize
10884 && o->rawsize != 0
10885 && o->rawsize < o->size)
10886 {
10887 memcpy (flinfo->contents, contents, o->rawsize);
10888 contents = flinfo->contents;
10889 }
10890 }
c152c796
AM
10891 else
10892 {
8b127cbc 10893 contents = flinfo->contents;
4a114e3e 10894 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10895 return FALSE;
10896 }
10897
10898 if ((o->flags & SEC_RELOC) != 0)
10899 {
10900 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10901 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10902 int action_discarded;
ece5ef60 10903 int ret;
c152c796
AM
10904
10905 /* Get the swapped relocs. */
10906 internal_relocs
8b127cbc
AM
10907 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10908 flinfo->internal_relocs, FALSE);
c152c796
AM
10909 if (internal_relocs == NULL
10910 && o->reloc_count > 0)
10911 return FALSE;
10912
310fd250
L
10913 /* We need to reverse-copy input .ctors/.dtors sections if
10914 they are placed in .init_array/.finit_array for output. */
10915 if (o->size > address_size
10916 && ((strncmp (o->name, ".ctors", 6) == 0
10917 && strcmp (o->output_section->name,
10918 ".init_array") == 0)
10919 || (strncmp (o->name, ".dtors", 6) == 0
10920 && strcmp (o->output_section->name,
10921 ".fini_array") == 0))
10922 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10923 {
056bafd4
MR
10924 if (o->size * bed->s->int_rels_per_ext_rel
10925 != o->reloc_count * address_size)
310fd250 10926 {
4eca0228 10927 _bfd_error_handler
695344c0 10928 /* xgettext:c-format */
871b3ab2 10929 (_("error: %pB: size of section %pA is not "
310fd250
L
10930 "multiple of address size"),
10931 input_bfd, o);
8c6716e5 10932 bfd_set_error (bfd_error_bad_value);
310fd250
L
10933 return FALSE;
10934 }
10935 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10936 }
10937
0f02bbd9 10938 action_discarded = -1;
c152c796 10939 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10940 action_discarded = (*bed->action_discarded) (o);
10941
10942 /* Run through the relocs evaluating complex reloc symbols and
10943 looking for relocs against symbols from discarded sections
10944 or section symbols from removed link-once sections.
10945 Complain about relocs against discarded sections. Zero
10946 relocs against removed link-once sections. */
10947
10948 rel = internal_relocs;
056bafd4 10949 relend = rel + o->reloc_count;
0f02bbd9 10950 for ( ; rel < relend; rel++)
c152c796 10951 {
0f02bbd9
AM
10952 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10953 unsigned int s_type;
10954 asection **ps, *sec;
10955 struct elf_link_hash_entry *h = NULL;
10956 const char *sym_name;
c152c796 10957
0f02bbd9
AM
10958 if (r_symndx == STN_UNDEF)
10959 continue;
c152c796 10960
0f02bbd9
AM
10961 if (r_symndx >= locsymcount
10962 || (elf_bad_symtab (input_bfd)
8b127cbc 10963 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10964 {
10965 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10966
0f02bbd9
AM
10967 /* Badly formatted input files can contain relocs that
10968 reference non-existant symbols. Check here so that
10969 we do not seg fault. */
10970 if (h == NULL)
c152c796 10971 {
4eca0228 10972 _bfd_error_handler
695344c0 10973 /* xgettext:c-format */
2dcf00ce 10974 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10975 "that references a non-existent global symbol"),
2dcf00ce 10976 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10977 bfd_set_error (bfd_error_bad_value);
10978 return FALSE;
10979 }
3b36f7e6 10980
0f02bbd9
AM
10981 while (h->root.type == bfd_link_hash_indirect
10982 || h->root.type == bfd_link_hash_warning)
10983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10984
0f02bbd9 10985 s_type = h->type;
cdd3575c 10986
9e2dec47 10987 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10988 mark the symbol as undefined. Note that the
10989 linker may attach linker created dynamic sections
10990 to the plugin bfd. Symbols defined in linker
10991 created sections are not plugin symbols. */
bc4e12de 10992 if ((h->root.non_ir_ref_regular
4070765b 10993 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10994 && (h->root.type == bfd_link_hash_defined
10995 || h->root.type == bfd_link_hash_defweak)
10996 && (h->root.u.def.section->flags
10997 & SEC_LINKER_CREATED) == 0
10998 && h->root.u.def.section->owner != NULL
10999 && (h->root.u.def.section->owner->flags
11000 & BFD_PLUGIN) != 0)
11001 {
11002 h->root.type = bfd_link_hash_undefined;
11003 h->root.u.undef.abfd = h->root.u.def.section->owner;
11004 }
11005
0f02bbd9
AM
11006 ps = NULL;
11007 if (h->root.type == bfd_link_hash_defined
11008 || h->root.type == bfd_link_hash_defweak)
11009 ps = &h->root.u.def.section;
11010
11011 sym_name = h->root.root.string;
11012 }
11013 else
11014 {
11015 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11016
11017 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11018 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11019 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11020 sym, *ps);
11021 }
c152c796 11022
c301e700 11023 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11024 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11025 {
11026 bfd_vma val;
11027 bfd_vma dot = (rel->r_offset
11028 + o->output_offset + o->output_section->vma);
11029#ifdef DEBUG
11030 printf ("Encountered a complex symbol!");
11031 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11032 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11033 (long) (rel - internal_relocs));
0f02bbd9
AM
11034 printf (" symbol: idx %8.8lx, name %s\n",
11035 r_symndx, sym_name);
11036 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11037 (unsigned long) rel->r_info,
11038 (unsigned long) rel->r_offset);
11039#endif
8b127cbc 11040 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11041 isymbuf, locsymcount, s_type == STT_SRELC))
11042 return FALSE;
11043
11044 /* Symbol evaluated OK. Update to absolute value. */
11045 set_symbol_value (input_bfd, isymbuf, locsymcount,
11046 r_symndx, val);
11047 continue;
11048 }
11049
11050 if (action_discarded != -1 && ps != NULL)
11051 {
cdd3575c
AM
11052 /* Complain if the definition comes from a
11053 discarded section. */
dbaa2011 11054 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11055 {
cf35638d 11056 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11057 if (action_discarded & COMPLAIN)
8b127cbc 11058 (*flinfo->info->callbacks->einfo)
695344c0 11059 /* xgettext:c-format */
871b3ab2
AM
11060 (_("%X`%s' referenced in section `%pA' of %pB: "
11061 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11062 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11063
87e5235d 11064 /* Try to do the best we can to support buggy old
e0ae6d6f 11065 versions of gcc. Pretend that the symbol is
87e5235d
AM
11066 really defined in the kept linkonce section.
11067 FIXME: This is quite broken. Modifying the
11068 symbol here means we will be changing all later
e0ae6d6f 11069 uses of the symbol, not just in this section. */
0f02bbd9 11070 if (action_discarded & PRETEND)
87e5235d 11071 {
01b3c8ab
L
11072 asection *kept;
11073
c0f00686 11074 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11075 flinfo->info);
01b3c8ab 11076 if (kept != NULL)
87e5235d
AM
11077 {
11078 *ps = kept;
11079 continue;
11080 }
11081 }
c152c796
AM
11082 }
11083 }
11084 }
11085
11086 /* Relocate the section by invoking a back end routine.
11087
11088 The back end routine is responsible for adjusting the
11089 section contents as necessary, and (if using Rela relocs
11090 and generating a relocatable output file) adjusting the
11091 reloc addend as necessary.
11092
11093 The back end routine does not have to worry about setting
11094 the reloc address or the reloc symbol index.
11095
11096 The back end routine is given a pointer to the swapped in
11097 internal symbols, and can access the hash table entries
11098 for the external symbols via elf_sym_hashes (input_bfd).
11099
11100 When generating relocatable output, the back end routine
11101 must handle STB_LOCAL/STT_SECTION symbols specially. The
11102 output symbol is going to be a section symbol
11103 corresponding to the output section, which will require
11104 the addend to be adjusted. */
11105
8b127cbc 11106 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11107 input_bfd, o, contents,
11108 internal_relocs,
11109 isymbuf,
8b127cbc 11110 flinfo->sections);
ece5ef60 11111 if (!ret)
c152c796
AM
11112 return FALSE;
11113
ece5ef60 11114 if (ret == 2
0e1862bb 11115 || bfd_link_relocatable (flinfo->info)
8b127cbc 11116 || flinfo->info->emitrelocations)
c152c796
AM
11117 {
11118 Elf_Internal_Rela *irela;
d4730f92 11119 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11120 bfd_vma last_offset;
11121 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11122 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11123 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11124 unsigned int next_erel;
c152c796 11125 bfd_boolean rela_normal;
d4730f92 11126 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11127
d4730f92
BS
11128 esdi = elf_section_data (o);
11129 esdo = elf_section_data (o->output_section);
11130 rela_normal = FALSE;
c152c796
AM
11131
11132 /* Adjust the reloc addresses and symbol indices. */
11133
11134 irela = internal_relocs;
056bafd4 11135 irelaend = irela + o->reloc_count;
d4730f92
BS
11136 rel_hash = esdo->rel.hashes + esdo->rel.count;
11137 /* We start processing the REL relocs, if any. When we reach
11138 IRELAMID in the loop, we switch to the RELA relocs. */
11139 irelamid = irela;
11140 if (esdi->rel.hdr != NULL)
11141 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11142 * bed->s->int_rels_per_ext_rel);
eac338cf 11143 rel_hash_list = rel_hash;
d4730f92 11144 rela_hash_list = NULL;
c152c796 11145 last_offset = o->output_offset;
0e1862bb 11146 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11147 last_offset += o->output_section->vma;
11148 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11149 {
11150 unsigned long r_symndx;
11151 asection *sec;
11152 Elf_Internal_Sym sym;
11153
11154 if (next_erel == bed->s->int_rels_per_ext_rel)
11155 {
11156 rel_hash++;
11157 next_erel = 0;
11158 }
11159
d4730f92
BS
11160 if (irela == irelamid)
11161 {
11162 rel_hash = esdo->rela.hashes + esdo->rela.count;
11163 rela_hash_list = rel_hash;
11164 rela_normal = bed->rela_normal;
11165 }
11166
c152c796 11167 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11168 flinfo->info, o,
c152c796
AM
11169 irela->r_offset);
11170 if (irela->r_offset >= (bfd_vma) -2)
11171 {
11172 /* This is a reloc for a deleted entry or somesuch.
11173 Turn it into an R_*_NONE reloc, at the same
11174 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11175 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11176 being ordered. */
11177 irela->r_offset = last_offset;
11178 irela->r_info = 0;
11179 irela->r_addend = 0;
11180 continue;
11181 }
11182
11183 irela->r_offset += o->output_offset;
11184
11185 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11186 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11187 irela->r_offset += o->output_section->vma;
11188
11189 last_offset = irela->r_offset;
11190
11191 r_symndx = irela->r_info >> r_sym_shift;
11192 if (r_symndx == STN_UNDEF)
11193 continue;
11194
11195 if (r_symndx >= locsymcount
11196 || (elf_bad_symtab (input_bfd)
8b127cbc 11197 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11198 {
11199 struct elf_link_hash_entry *rh;
11200 unsigned long indx;
11201
11202 /* This is a reloc against a global symbol. We
11203 have not yet output all the local symbols, so
11204 we do not know the symbol index of any global
11205 symbol. We set the rel_hash entry for this
11206 reloc to point to the global hash table entry
11207 for this symbol. The symbol index is then
ee75fd95 11208 set at the end of bfd_elf_final_link. */
c152c796
AM
11209 indx = r_symndx - extsymoff;
11210 rh = elf_sym_hashes (input_bfd)[indx];
11211 while (rh->root.type == bfd_link_hash_indirect
11212 || rh->root.type == bfd_link_hash_warning)
11213 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11214
11215 /* Setting the index to -2 tells
11216 elf_link_output_extsym that this symbol is
11217 used by a reloc. */
11218 BFD_ASSERT (rh->indx < 0);
11219 rh->indx = -2;
c152c796
AM
11220 *rel_hash = rh;
11221
11222 continue;
11223 }
11224
11225 /* This is a reloc against a local symbol. */
11226
11227 *rel_hash = NULL;
11228 sym = isymbuf[r_symndx];
8b127cbc 11229 sec = flinfo->sections[r_symndx];
c152c796
AM
11230 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11231 {
11232 /* I suppose the backend ought to fill in the
11233 section of any STT_SECTION symbol against a
6a8d1586 11234 processor specific section. */
cf35638d 11235 r_symndx = STN_UNDEF;
6a8d1586
AM
11236 if (bfd_is_abs_section (sec))
11237 ;
c152c796
AM
11238 else if (sec == NULL || sec->owner == NULL)
11239 {
11240 bfd_set_error (bfd_error_bad_value);
11241 return FALSE;
11242 }
11243 else
11244 {
6a8d1586
AM
11245 asection *osec = sec->output_section;
11246
11247 /* If we have discarded a section, the output
11248 section will be the absolute section. In
ab96bf03
AM
11249 case of discarded SEC_MERGE sections, use
11250 the kept section. relocate_section should
11251 have already handled discarded linkonce
11252 sections. */
6a8d1586
AM
11253 if (bfd_is_abs_section (osec)
11254 && sec->kept_section != NULL
11255 && sec->kept_section->output_section != NULL)
11256 {
11257 osec = sec->kept_section->output_section;
11258 irela->r_addend -= osec->vma;
11259 }
11260
11261 if (!bfd_is_abs_section (osec))
11262 {
11263 r_symndx = osec->target_index;
cf35638d 11264 if (r_symndx == STN_UNDEF)
74541ad4 11265 {
051d833a
AM
11266 irela->r_addend += osec->vma;
11267 osec = _bfd_nearby_section (output_bfd, osec,
11268 osec->vma);
11269 irela->r_addend -= osec->vma;
11270 r_symndx = osec->target_index;
74541ad4 11271 }
6a8d1586 11272 }
c152c796
AM
11273 }
11274
11275 /* Adjust the addend according to where the
11276 section winds up in the output section. */
11277 if (rela_normal)
11278 irela->r_addend += sec->output_offset;
11279 }
11280 else
11281 {
8b127cbc 11282 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11283 {
11284 unsigned long shlink;
11285 const char *name;
11286 asection *osec;
6e0b88f1 11287 long indx;
c152c796 11288
8b127cbc 11289 if (flinfo->info->strip == strip_all)
c152c796
AM
11290 {
11291 /* You can't do ld -r -s. */
11292 bfd_set_error (bfd_error_invalid_operation);
11293 return FALSE;
11294 }
11295
11296 /* This symbol was skipped earlier, but
11297 since it is needed by a reloc, we
11298 must output it now. */
11299 shlink = symtab_hdr->sh_link;
11300 name = (bfd_elf_string_from_elf_section
11301 (input_bfd, shlink, sym.st_name));
11302 if (name == NULL)
11303 return FALSE;
11304
11305 osec = sec->output_section;
11306 sym.st_shndx =
11307 _bfd_elf_section_from_bfd_section (output_bfd,
11308 osec);
11309 if (sym.st_shndx == SHN_BAD)
11310 return FALSE;
11311
11312 sym.st_value += sec->output_offset;
0e1862bb 11313 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11314 {
11315 sym.st_value += osec->vma;
11316 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11317 {
102def4d
AM
11318 struct elf_link_hash_table *htab
11319 = elf_hash_table (flinfo->info);
11320
c152c796
AM
11321 /* STT_TLS symbols are relative to PT_TLS
11322 segment base. */
102def4d
AM
11323 if (htab->tls_sec != NULL)
11324 sym.st_value -= htab->tls_sec->vma;
11325 else
11326 sym.st_info
11327 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11328 STT_NOTYPE);
c152c796
AM
11329 }
11330 }
11331
6e0b88f1 11332 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11333 ret = elf_link_output_symstrtab (flinfo, name,
11334 &sym, sec,
11335 NULL);
6e0b88f1 11336 if (ret == 0)
c152c796 11337 return FALSE;
6e0b88f1 11338 else if (ret == 1)
8b127cbc 11339 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11340 else
11341 abort ();
c152c796
AM
11342 }
11343
8b127cbc 11344 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11345 }
11346
11347 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11348 | (irela->r_info & r_type_mask));
11349 }
11350
11351 /* Swap out the relocs. */
d4730f92
BS
11352 input_rel_hdr = esdi->rel.hdr;
11353 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11354 {
d4730f92
BS
11355 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11356 input_rel_hdr,
11357 internal_relocs,
11358 rel_hash_list))
11359 return FALSE;
c152c796
AM
11360 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11361 * bed->s->int_rels_per_ext_rel);
eac338cf 11362 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11363 }
11364
11365 input_rela_hdr = esdi->rela.hdr;
11366 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11367 {
eac338cf 11368 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11369 input_rela_hdr,
eac338cf 11370 internal_relocs,
d4730f92 11371 rela_hash_list))
c152c796
AM
11372 return FALSE;
11373 }
11374 }
11375 }
11376
11377 /* Write out the modified section contents. */
11378 if (bed->elf_backend_write_section
8b127cbc 11379 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11380 contents))
c152c796
AM
11381 {
11382 /* Section written out. */
11383 }
11384 else switch (o->sec_info_type)
11385 {
dbaa2011 11386 case SEC_INFO_TYPE_STABS:
c152c796
AM
11387 if (! (_bfd_write_section_stabs
11388 (output_bfd,
8b127cbc 11389 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11390 o, &elf_section_data (o)->sec_info, contents)))
11391 return FALSE;
11392 break;
dbaa2011 11393 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11394 if (! _bfd_write_merged_section (output_bfd, o,
11395 elf_section_data (o)->sec_info))
11396 return FALSE;
11397 break;
dbaa2011 11398 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11399 {
8b127cbc 11400 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11401 o, contents))
11402 return FALSE;
11403 }
11404 break;
2f0c68f2
CM
11405 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11406 {
11407 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11408 flinfo->info,
11409 o, contents))
11410 return FALSE;
11411 }
11412 break;
c152c796
AM
11413 default:
11414 {
310fd250
L
11415 if (! (o->flags & SEC_EXCLUDE))
11416 {
11417 file_ptr offset = (file_ptr) o->output_offset;
11418 bfd_size_type todo = o->size;
37b01f6a 11419
bb294208 11420 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11421
310fd250
L
11422 if ((o->flags & SEC_ELF_REVERSE_COPY))
11423 {
11424 /* Reverse-copy input section to output. */
11425 do
11426 {
11427 todo -= address_size;
11428 if (! bfd_set_section_contents (output_bfd,
11429 o->output_section,
11430 contents + todo,
11431 offset,
11432 address_size))
11433 return FALSE;
11434 if (todo == 0)
11435 break;
11436 offset += address_size;
11437 }
11438 while (1);
11439 }
11440 else if (! bfd_set_section_contents (output_bfd,
11441 o->output_section,
11442 contents,
11443 offset, todo))
11444 return FALSE;
11445 }
c152c796
AM
11446 }
11447 break;
11448 }
11449 }
11450
11451 return TRUE;
11452}
11453
11454/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11455 requested by the linker, and does not come from any input file. This
c152c796
AM
11456 is used to build constructor and destructor tables when linking
11457 with -Ur. */
11458
11459static bfd_boolean
11460elf_reloc_link_order (bfd *output_bfd,
11461 struct bfd_link_info *info,
11462 asection *output_section,
11463 struct bfd_link_order *link_order)
11464{
11465 reloc_howto_type *howto;
11466 long indx;
11467 bfd_vma offset;
11468 bfd_vma addend;
d4730f92 11469 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11470 struct elf_link_hash_entry **rel_hash_ptr;
11471 Elf_Internal_Shdr *rel_hdr;
11472 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11473 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11474 bfd_byte *erel;
11475 unsigned int i;
d4730f92 11476 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11477
11478 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11479 if (howto == NULL)
11480 {
11481 bfd_set_error (bfd_error_bad_value);
11482 return FALSE;
11483 }
11484
11485 addend = link_order->u.reloc.p->addend;
11486
d4730f92
BS
11487 if (esdo->rel.hdr)
11488 reldata = &esdo->rel;
11489 else if (esdo->rela.hdr)
11490 reldata = &esdo->rela;
11491 else
11492 {
11493 reldata = NULL;
11494 BFD_ASSERT (0);
11495 }
11496
c152c796 11497 /* Figure out the symbol index. */
d4730f92 11498 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11499 if (link_order->type == bfd_section_reloc_link_order)
11500 {
11501 indx = link_order->u.reloc.p->u.section->target_index;
11502 BFD_ASSERT (indx != 0);
11503 *rel_hash_ptr = NULL;
11504 }
11505 else
11506 {
11507 struct elf_link_hash_entry *h;
11508
11509 /* Treat a reloc against a defined symbol as though it were
11510 actually against the section. */
11511 h = ((struct elf_link_hash_entry *)
11512 bfd_wrapped_link_hash_lookup (output_bfd, info,
11513 link_order->u.reloc.p->u.name,
11514 FALSE, FALSE, TRUE));
11515 if (h != NULL
11516 && (h->root.type == bfd_link_hash_defined
11517 || h->root.type == bfd_link_hash_defweak))
11518 {
11519 asection *section;
11520
11521 section = h->root.u.def.section;
11522 indx = section->output_section->target_index;
11523 *rel_hash_ptr = NULL;
11524 /* It seems that we ought to add the symbol value to the
11525 addend here, but in practice it has already been added
11526 because it was passed to constructor_callback. */
11527 addend += section->output_section->vma + section->output_offset;
11528 }
11529 else if (h != NULL)
11530 {
11531 /* Setting the index to -2 tells elf_link_output_extsym that
11532 this symbol is used by a reloc. */
11533 h->indx = -2;
11534 *rel_hash_ptr = h;
11535 indx = 0;
11536 }
11537 else
11538 {
1a72702b
AM
11539 (*info->callbacks->unattached_reloc)
11540 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11541 indx = 0;
11542 }
11543 }
11544
11545 /* If this is an inplace reloc, we must write the addend into the
11546 object file. */
11547 if (howto->partial_inplace && addend != 0)
11548 {
11549 bfd_size_type size;
11550 bfd_reloc_status_type rstat;
11551 bfd_byte *buf;
11552 bfd_boolean ok;
11553 const char *sym_name;
bb294208 11554 bfd_size_type octets;
c152c796 11555
a50b1753
NC
11556 size = (bfd_size_type) bfd_get_reloc_size (howto);
11557 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11558 if (buf == NULL && size != 0)
c152c796
AM
11559 return FALSE;
11560 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11561 switch (rstat)
11562 {
11563 case bfd_reloc_ok:
11564 break;
11565
11566 default:
11567 case bfd_reloc_outofrange:
11568 abort ();
11569
11570 case bfd_reloc_overflow:
11571 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11572 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11573 else
11574 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11575 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11576 howto->name, addend, NULL, NULL,
11577 (bfd_vma) 0);
c152c796
AM
11578 break;
11579 }
37b01f6a 11580
bb294208
AM
11581 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11582 output_section);
c152c796 11583 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11584 octets, size);
c152c796
AM
11585 free (buf);
11586 if (! ok)
11587 return FALSE;
11588 }
11589
11590 /* The address of a reloc is relative to the section in a
11591 relocatable file, and is a virtual address in an executable
11592 file. */
11593 offset = link_order->offset;
0e1862bb 11594 if (! bfd_link_relocatable (info))
c152c796
AM
11595 offset += output_section->vma;
11596
11597 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11598 {
11599 irel[i].r_offset = offset;
11600 irel[i].r_info = 0;
11601 irel[i].r_addend = 0;
11602 }
11603 if (bed->s->arch_size == 32)
11604 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11605 else
11606 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11607
d4730f92 11608 rel_hdr = reldata->hdr;
c152c796
AM
11609 erel = rel_hdr->contents;
11610 if (rel_hdr->sh_type == SHT_REL)
11611 {
d4730f92 11612 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11613 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11614 }
11615 else
11616 {
11617 irel[0].r_addend = addend;
d4730f92 11618 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11619 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11620 }
11621
d4730f92 11622 ++reldata->count;
c152c796
AM
11623
11624 return TRUE;
11625}
11626
0b52efa6 11627
0b52efa6
PB
11628/* Compare two sections based on the locations of the sections they are
11629 linked to. Used by elf_fixup_link_order. */
11630
11631static int
8c1c5e5d 11632compare_link_order (const void *a, const void *b)
0b52efa6 11633{
8c1c5e5d
AM
11634 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11635 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11636 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11637 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11638 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11639 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11640
0b52efa6
PB
11641 if (apos < bpos)
11642 return -1;
8c1c5e5d
AM
11643 if (apos > bpos)
11644 return 1;
11645
11646 /* The only way we should get matching LMAs is when the first of two
11647 sections has zero size. */
11648 if (asec->size < bsec->size)
11649 return -1;
11650 if (asec->size > bsec->size)
11651 return 1;
11652
11653 /* If they are both zero size then they almost certainly have the same
11654 VMA and thus are not ordered with respect to each other. Test VMA
11655 anyway, and fall back to id to make the result reproducible across
11656 qsort implementations. */
11657 apos = asec->output_section->vma + asec->output_offset;
11658 bpos = bsec->output_section->vma + bsec->output_offset;
11659 if (apos < bpos)
11660 return -1;
11661 if (apos > bpos)
11662 return 1;
11663
11664 return asec->id - bsec->id;
0b52efa6
PB
11665}
11666
11667
11668/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11669 order as their linked sections. Returns false if this could not be done
11670 because an output section includes both ordered and unordered
11671 sections. Ideally we'd do this in the linker proper. */
11672
11673static bfd_boolean
11674elf_fixup_link_order (bfd *abfd, asection *o)
11675{
8c1c5e5d
AM
11676 size_t seen_linkorder;
11677 size_t seen_other;
11678 size_t n;
0b52efa6
PB
11679 struct bfd_link_order *p;
11680 bfd *sub;
0b52efa6 11681 struct bfd_link_order **sections;
66631823
CE
11682 asection *other_sec, *linkorder_sec;
11683 bfd_vma offset; /* Octets. */
3b36f7e6 11684
d33cdfe3
L
11685 other_sec = NULL;
11686 linkorder_sec = NULL;
0b52efa6
PB
11687 seen_other = 0;
11688 seen_linkorder = 0;
8423293d 11689 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11690 {
d33cdfe3 11691 if (p->type == bfd_indirect_link_order)
0b52efa6 11692 {
66631823 11693 asection *s = p->u.indirect.section;
d33cdfe3 11694 sub = s->owner;
847d5183
AM
11695 if ((s->flags & SEC_LINKER_CREATED) == 0
11696 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11697 && elf_section_data (s) != NULL
11698 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11699 {
11700 seen_linkorder++;
11701 linkorder_sec = s;
11702 }
0b52efa6 11703 else
d33cdfe3
L
11704 {
11705 seen_other++;
11706 other_sec = s;
11707 }
0b52efa6
PB
11708 }
11709 else
11710 seen_other++;
d33cdfe3
L
11711
11712 if (seen_other && seen_linkorder)
11713 {
11714 if (other_sec && linkorder_sec)
4eca0228 11715 _bfd_error_handler
695344c0 11716 /* xgettext:c-format */
871b3ab2
AM
11717 (_("%pA has both ordered [`%pA' in %pB] "
11718 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11719 o, linkorder_sec, linkorder_sec->owner,
11720 other_sec, other_sec->owner);
d33cdfe3 11721 else
4eca0228 11722 _bfd_error_handler
871b3ab2 11723 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11724 bfd_set_error (bfd_error_bad_value);
11725 return FALSE;
11726 }
0b52efa6
PB
11727 }
11728
11729 if (!seen_linkorder)
11730 return TRUE;
11731
8c1c5e5d 11732 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11733 if (sections == NULL)
11734 return FALSE;
3b36f7e6 11735
8c1c5e5d 11736 seen_linkorder = 0;
8423293d 11737 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11738 sections[seen_linkorder++] = p;
11739
0b52efa6 11740 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11741 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11742
11743 /* Change the offsets of the sections. */
11744 offset = 0;
11745 for (n = 0; n < seen_linkorder; n++)
11746 {
8c1c5e5d 11747 bfd_vma mask;
66631823
CE
11748 asection *s = sections[n]->u.indirect.section;
11749 unsigned int opb = bfd_octets_per_byte (abfd, s);
11750
11751 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11752 offset = (offset + ~mask) & mask;
66631823 11753 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11754 offset += sections[n]->size;
11755 }
11756
4dd07732 11757 free (sections);
0b52efa6
PB
11758 return TRUE;
11759}
11760
76359541
TP
11761/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11762 Returns TRUE upon success, FALSE otherwise. */
11763
11764static bfd_boolean
11765elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11766{
11767 bfd_boolean ret = FALSE;
11768 bfd *implib_bfd;
11769 const struct elf_backend_data *bed;
11770 flagword flags;
11771 enum bfd_architecture arch;
11772 unsigned int mach;
11773 asymbol **sympp = NULL;
11774 long symsize;
11775 long symcount;
11776 long src_count;
11777 elf_symbol_type *osymbuf;
446f7ed5 11778 size_t amt;
76359541
TP
11779
11780 implib_bfd = info->out_implib_bfd;
11781 bed = get_elf_backend_data (abfd);
11782
11783 if (!bfd_set_format (implib_bfd, bfd_object))
11784 return FALSE;
11785
046734ff 11786 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11787 flags = bfd_get_file_flags (abfd);
11788 flags &= ~HAS_RELOC;
11789 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11790 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11791 return FALSE;
11792
11793 /* Copy architecture of output file to import library file. */
11794 arch = bfd_get_arch (abfd);
11795 mach = bfd_get_mach (abfd);
11796 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11797 && (abfd->target_defaulted
11798 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11799 return FALSE;
11800
11801 /* Get symbol table size. */
11802 symsize = bfd_get_symtab_upper_bound (abfd);
11803 if (symsize < 0)
11804 return FALSE;
11805
11806 /* Read in the symbol table. */
ec9bd0a2
AM
11807 sympp = (asymbol **) bfd_malloc (symsize);
11808 if (sympp == NULL)
11809 return FALSE;
11810
76359541
TP
11811 symcount = bfd_canonicalize_symtab (abfd, sympp);
11812 if (symcount < 0)
11813 goto free_sym_buf;
11814
11815 /* Allow the BFD backend to copy any private header data it
11816 understands from the output BFD to the import library BFD. */
11817 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11818 goto free_sym_buf;
11819
11820 /* Filter symbols to appear in the import library. */
11821 if (bed->elf_backend_filter_implib_symbols)
11822 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11823 symcount);
11824 else
11825 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11826 if (symcount == 0)
11827 {
5df1bc57 11828 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11829 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11830 implib_bfd);
76359541
TP
11831 goto free_sym_buf;
11832 }
11833
11834
11835 /* Make symbols absolute. */
446f7ed5
AM
11836 amt = symcount * sizeof (*osymbuf);
11837 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11838 if (osymbuf == NULL)
11839 goto free_sym_buf;
11840
76359541
TP
11841 for (src_count = 0; src_count < symcount; src_count++)
11842 {
11843 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11844 sizeof (*osymbuf));
11845 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11846 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11847 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11848 osymbuf[src_count].internal_elf_sym.st_value =
11849 osymbuf[src_count].symbol.value;
11850 sympp[src_count] = &osymbuf[src_count].symbol;
11851 }
11852
11853 bfd_set_symtab (implib_bfd, sympp, symcount);
11854
11855 /* Allow the BFD backend to copy any private data it understands
11856 from the output BFD to the import library BFD. This is done last
11857 to permit the routine to look at the filtered symbol table. */
11858 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11859 goto free_sym_buf;
11860
11861 if (!bfd_close (implib_bfd))
11862 goto free_sym_buf;
11863
11864 ret = TRUE;
11865
dc1e8a47 11866 free_sym_buf:
76359541
TP
11867 free (sympp);
11868 return ret;
11869}
11870
9f7c3e5e
AM
11871static void
11872elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11873{
11874 asection *o;
11875
11876 if (flinfo->symstrtab != NULL)
ef10c3ac 11877 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11878 if (flinfo->contents != NULL)
11879 free (flinfo->contents);
11880 if (flinfo->external_relocs != NULL)
11881 free (flinfo->external_relocs);
11882 if (flinfo->internal_relocs != NULL)
11883 free (flinfo->internal_relocs);
11884 if (flinfo->external_syms != NULL)
11885 free (flinfo->external_syms);
11886 if (flinfo->locsym_shndx != NULL)
11887 free (flinfo->locsym_shndx);
11888 if (flinfo->internal_syms != NULL)
11889 free (flinfo->internal_syms);
11890 if (flinfo->indices != NULL)
11891 free (flinfo->indices);
11892 if (flinfo->sections != NULL)
11893 free (flinfo->sections);
a0f6fd21
AM
11894 if (flinfo->symshndxbuf != NULL
11895 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11896 free (flinfo->symshndxbuf);
11897 for (o = obfd->sections; o != NULL; o = o->next)
11898 {
11899 struct bfd_elf_section_data *esdo = elf_section_data (o);
11900 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11901 free (esdo->rel.hashes);
11902 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11903 free (esdo->rela.hashes);
11904 }
11905}
0b52efa6 11906
c152c796
AM
11907/* Do the final step of an ELF link. */
11908
11909bfd_boolean
11910bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11911{
11912 bfd_boolean dynamic;
11913 bfd_boolean emit_relocs;
11914 bfd *dynobj;
8b127cbc 11915 struct elf_final_link_info flinfo;
91d6fa6a
NC
11916 asection *o;
11917 struct bfd_link_order *p;
11918 bfd *sub;
c152c796
AM
11919 bfd_size_type max_contents_size;
11920 bfd_size_type max_external_reloc_size;
11921 bfd_size_type max_internal_reloc_count;
11922 bfd_size_type max_sym_count;
11923 bfd_size_type max_sym_shndx_count;
c152c796
AM
11924 Elf_Internal_Sym elfsym;
11925 unsigned int i;
11926 Elf_Internal_Shdr *symtab_hdr;
11927 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11929 struct elf_outext_info eoinfo;
11930 bfd_boolean merged;
11931 size_t relativecount = 0;
11932 asection *reldyn = 0;
11933 bfd_size_type amt;
104d59d1
JM
11934 asection *attr_section = NULL;
11935 bfd_vma attr_size = 0;
11936 const char *std_attrs_section;
64f52338 11937 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 11938 bfd_boolean sections_removed;
c152c796 11939
64f52338 11940 if (!is_elf_hash_table (htab))
c152c796
AM
11941 return FALSE;
11942
0e1862bb 11943 if (bfd_link_pic (info))
c152c796
AM
11944 abfd->flags |= DYNAMIC;
11945
64f52338
AM
11946 dynamic = htab->dynamic_sections_created;
11947 dynobj = htab->dynobj;
c152c796 11948
0e1862bb 11949 emit_relocs = (bfd_link_relocatable (info)
a4676736 11950 || info->emitrelocations);
c152c796 11951
8b127cbc
AM
11952 flinfo.info = info;
11953 flinfo.output_bfd = abfd;
ef10c3ac 11954 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11955 if (flinfo.symstrtab == NULL)
c152c796
AM
11956 return FALSE;
11957
11958 if (! dynamic)
11959 {
8b127cbc
AM
11960 flinfo.hash_sec = NULL;
11961 flinfo.symver_sec = NULL;
c152c796
AM
11962 }
11963 else
11964 {
3d4d4302 11965 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11966 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11967 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11968 /* Note that it is OK if symver_sec is NULL. */
11969 }
11970
8b127cbc
AM
11971 flinfo.contents = NULL;
11972 flinfo.external_relocs = NULL;
11973 flinfo.internal_relocs = NULL;
11974 flinfo.external_syms = NULL;
11975 flinfo.locsym_shndx = NULL;
11976 flinfo.internal_syms = NULL;
11977 flinfo.indices = NULL;
11978 flinfo.sections = NULL;
8b127cbc 11979 flinfo.symshndxbuf = NULL;
ffbc01cc 11980 flinfo.filesym_count = 0;
c152c796 11981
104d59d1
JM
11982 /* The object attributes have been merged. Remove the input
11983 sections from the link, and set the contents of the output
1ff6de03 11984 section. */
4c6ee646 11985 sections_removed = FALSE;
104d59d1
JM
11986 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11987 for (o = abfd->sections; o != NULL; o = o->next)
11988 {
5270eddc 11989 bfd_boolean remove_section = FALSE;
b8a6ced7 11990
104d59d1
JM
11991 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11992 || strcmp (o->name, ".gnu.attributes") == 0)
11993 {
11994 for (p = o->map_head.link_order; p != NULL; p = p->next)
11995 {
11996 asection *input_section;
11997
11998 if (p->type != bfd_indirect_link_order)
11999 continue;
12000 input_section = p->u.indirect.section;
12001 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12002 elf_link_input_bfd ignores this section. */
12003 input_section->flags &= ~SEC_HAS_CONTENTS;
12004 }
a0c8462f 12005
104d59d1 12006 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12007 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12008 /* Skip this section later on. */
12009 o->map_head.link_order = NULL;
104d59d1 12010 if (attr_size)
b8a6ced7 12011 attr_section = o;
104d59d1 12012 else
5270eddc 12013 remove_section = TRUE;
104d59d1 12014 }
6e5e9d58
AM
12015 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12016 {
12017 /* Remove empty group section from linker output. */
5270eddc 12018 remove_section = TRUE;
b8a6ced7 12019 }
5270eddc 12020 if (remove_section)
b8a6ced7 12021 {
6e5e9d58
AM
12022 o->flags |= SEC_EXCLUDE;
12023 bfd_section_list_remove (abfd, o);
12024 abfd->section_count--;
4c6ee646 12025 sections_removed = TRUE;
6e5e9d58 12026 }
104d59d1 12027 }
4c6ee646
AM
12028 if (sections_removed)
12029 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12030
c152c796
AM
12031 /* Count up the number of relocations we will output for each output
12032 section, so that we know the sizes of the reloc sections. We
12033 also figure out some maximum sizes. */
12034 max_contents_size = 0;
12035 max_external_reloc_size = 0;
12036 max_internal_reloc_count = 0;
12037 max_sym_count = 0;
12038 max_sym_shndx_count = 0;
12039 merged = FALSE;
12040 for (o = abfd->sections; o != NULL; o = o->next)
12041 {
12042 struct bfd_elf_section_data *esdo = elf_section_data (o);
12043 o->reloc_count = 0;
12044
8423293d 12045 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12046 {
12047 unsigned int reloc_count = 0;
9eaff861 12048 unsigned int additional_reloc_count = 0;
c152c796 12049 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12050
12051 if (p->type == bfd_section_reloc_link_order
12052 || p->type == bfd_symbol_reloc_link_order)
12053 reloc_count = 1;
12054 else if (p->type == bfd_indirect_link_order)
12055 {
12056 asection *sec;
12057
12058 sec = p->u.indirect.section;
c152c796
AM
12059
12060 /* Mark all sections which are to be included in the
12061 link. This will normally be every section. We need
12062 to do this so that we can identify any sections which
12063 the linker has decided to not include. */
12064 sec->linker_mark = TRUE;
12065
12066 if (sec->flags & SEC_MERGE)
12067 merged = TRUE;
12068
eea6121a
AM
12069 if (sec->rawsize > max_contents_size)
12070 max_contents_size = sec->rawsize;
12071 if (sec->size > max_contents_size)
12072 max_contents_size = sec->size;
c152c796 12073
c152c796
AM
12074 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12075 && (sec->owner->flags & DYNAMIC) == 0)
12076 {
12077 size_t sym_count;
12078
a961cdd5
AM
12079 /* We are interested in just local symbols, not all
12080 symbols. */
c152c796
AM
12081 if (elf_bad_symtab (sec->owner))
12082 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12083 / bed->s->sizeof_sym);
12084 else
12085 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12086
12087 if (sym_count > max_sym_count)
12088 max_sym_count = sym_count;
12089
12090 if (sym_count > max_sym_shndx_count
6a40cf0c 12091 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12092 max_sym_shndx_count = sym_count;
12093
a961cdd5
AM
12094 if (esdo->this_hdr.sh_type == SHT_REL
12095 || esdo->this_hdr.sh_type == SHT_RELA)
12096 /* Some backends use reloc_count in relocation sections
12097 to count particular types of relocs. Of course,
12098 reloc sections themselves can't have relocations. */
12099 ;
12100 else if (emit_relocs)
12101 {
12102 reloc_count = sec->reloc_count;
12103 if (bed->elf_backend_count_additional_relocs)
12104 {
12105 int c;
12106 c = (*bed->elf_backend_count_additional_relocs) (sec);
12107 additional_reloc_count += c;
12108 }
12109 }
12110 else if (bed->elf_backend_count_relocs)
12111 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12112
12113 esdi = elf_section_data (sec);
12114
c152c796
AM
12115 if ((sec->flags & SEC_RELOC) != 0)
12116 {
d4730f92 12117 size_t ext_size = 0;
c152c796 12118
d4730f92
BS
12119 if (esdi->rel.hdr != NULL)
12120 ext_size = esdi->rel.hdr->sh_size;
12121 if (esdi->rela.hdr != NULL)
12122 ext_size += esdi->rela.hdr->sh_size;
7326c758 12123
c152c796
AM
12124 if (ext_size > max_external_reloc_size)
12125 max_external_reloc_size = ext_size;
12126 if (sec->reloc_count > max_internal_reloc_count)
12127 max_internal_reloc_count = sec->reloc_count;
12128 }
12129 }
12130 }
12131
12132 if (reloc_count == 0)
12133 continue;
12134
9eaff861 12135 reloc_count += additional_reloc_count;
c152c796
AM
12136 o->reloc_count += reloc_count;
12137
0e1862bb 12138 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12139 {
d4730f92 12140 if (esdi->rel.hdr)
9eaff861 12141 {
491d01d3 12142 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12143 esdo->rel.count += additional_reloc_count;
12144 }
d4730f92 12145 if (esdi->rela.hdr)
9eaff861 12146 {
491d01d3 12147 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12148 esdo->rela.count += additional_reloc_count;
12149 }
d4730f92
BS
12150 }
12151 else
12152 {
12153 if (o->use_rela_p)
12154 esdo->rela.count += reloc_count;
2c2b4ed4 12155 else
d4730f92 12156 esdo->rel.count += reloc_count;
c152c796 12157 }
c152c796
AM
12158 }
12159
9eaff861 12160 if (o->reloc_count > 0)
c152c796
AM
12161 o->flags |= SEC_RELOC;
12162 else
12163 {
12164 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12165 set it (this is probably a bug) and if it is set
12166 assign_section_numbers will create a reloc section. */
12167 o->flags &=~ SEC_RELOC;
12168 }
12169
12170 /* If the SEC_ALLOC flag is not set, force the section VMA to
12171 zero. This is done in elf_fake_sections as well, but forcing
12172 the VMA to 0 here will ensure that relocs against these
12173 sections are handled correctly. */
12174 if ((o->flags & SEC_ALLOC) == 0
12175 && ! o->user_set_vma)
12176 o->vma = 0;
12177 }
12178
0e1862bb 12179 if (! bfd_link_relocatable (info) && merged)
64f52338 12180 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12181
12182 /* Figure out the file positions for everything but the symbol table
12183 and the relocs. We set symcount to force assign_section_numbers
12184 to create a symbol table. */
ed48ec2e 12185 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12186 BFD_ASSERT (! abfd->output_has_begun);
12187 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12188 goto error_return;
12189
ee75fd95 12190 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12191 for (o = abfd->sections; o != NULL; o = o->next)
12192 {
d4730f92 12193 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12194 if ((o->flags & SEC_RELOC) != 0)
12195 {
d4730f92 12196 if (esdo->rel.hdr
9eaff861 12197 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12198 goto error_return;
12199
d4730f92 12200 if (esdo->rela.hdr
9eaff861 12201 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12202 goto error_return;
12203 }
12204
48db3297
AM
12205 /* _bfd_elf_compute_section_file_positions makes temporary use
12206 of target_index. Reset it. */
12207 o->target_index = 0;
12208
c152c796
AM
12209 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12210 to count upwards while actually outputting the relocations. */
d4730f92
BS
12211 esdo->rel.count = 0;
12212 esdo->rela.count = 0;
0ce398f1 12213
1ff6de03
NA
12214 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12215 && !bfd_section_is_ctf (o))
0ce398f1
L
12216 {
12217 /* Cache the section contents so that they can be compressed
12218 later. Use bfd_malloc since it will be freed by
12219 bfd_compress_section_contents. */
12220 unsigned char *contents = esdo->this_hdr.contents;
12221 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12222 abort ();
12223 contents
12224 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12225 if (contents == NULL)
12226 goto error_return;
12227 esdo->this_hdr.contents = contents;
12228 }
c152c796
AM
12229 }
12230
1ff6de03
NA
12231 /* We have now assigned file positions for all the sections except .symtab,
12232 .strtab, and non-loaded reloc and compressed debugging sections. We start
12233 the .symtab section at the current file position, and write directly to it.
12234 We build the .strtab section in memory. */
ed48ec2e 12235 abfd->symcount = 0;
c152c796
AM
12236 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12237 /* sh_name is set in prep_headers. */
12238 symtab_hdr->sh_type = SHT_SYMTAB;
12239 /* sh_flags, sh_addr and sh_size all start off zero. */
12240 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12241 /* sh_link is set in assign_section_numbers. */
12242 /* sh_info is set below. */
12243 /* sh_offset is set just below. */
72de5009 12244 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12245
ef10c3ac
L
12246 if (max_sym_count < 20)
12247 max_sym_count = 20;
64f52338 12248 htab->strtabsize = max_sym_count;
ef10c3ac 12249 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12250 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12251 if (htab->strtab == NULL)
c152c796 12252 goto error_return;
ef10c3ac
L
12253 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12254 flinfo.symshndxbuf
12255 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12256 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12257
8539e4e8 12258 if (info->strip != strip_all || emit_relocs)
c152c796 12259 {
8539e4e8
AM
12260 file_ptr off = elf_next_file_pos (abfd);
12261
12262 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12263
12264 /* Note that at this point elf_next_file_pos (abfd) is
12265 incorrect. We do not yet know the size of the .symtab section.
12266 We correct next_file_pos below, after we do know the size. */
12267
12268 /* Start writing out the symbol table. The first symbol is always a
12269 dummy symbol. */
c152c796
AM
12270 elfsym.st_value = 0;
12271 elfsym.st_size = 0;
12272 elfsym.st_info = 0;
12273 elfsym.st_other = 0;
12274 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12275 elfsym.st_target_internal = 0;
ef10c3ac
L
12276 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12277 bfd_und_section_ptr, NULL) != 1)
c152c796 12278 goto error_return;
c152c796 12279
8539e4e8
AM
12280 /* Output a symbol for each section. We output these even if we are
12281 discarding local symbols, since they are used for relocs. These
12282 symbols have no names. We store the index of each one in the
12283 index field of the section, so that we can find it again when
12284 outputting relocs. */
12285
c152c796
AM
12286 elfsym.st_size = 0;
12287 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12288 elfsym.st_other = 0;
f0b5bb34 12289 elfsym.st_value = 0;
35fc36a8 12290 elfsym.st_target_internal = 0;
c152c796
AM
12291 for (i = 1; i < elf_numsections (abfd); i++)
12292 {
12293 o = bfd_section_from_elf_index (abfd, i);
12294 if (o != NULL)
f0b5bb34
AM
12295 {
12296 o->target_index = bfd_get_symcount (abfd);
12297 elfsym.st_shndx = i;
0e1862bb 12298 if (!bfd_link_relocatable (info))
f0b5bb34 12299 elfsym.st_value = o->vma;
ef10c3ac
L
12300 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12301 NULL) != 1)
f0b5bb34
AM
12302 goto error_return;
12303 }
c152c796
AM
12304 }
12305 }
12306
12307 /* Allocate some memory to hold information read in from the input
12308 files. */
12309 if (max_contents_size != 0)
12310 {
8b127cbc
AM
12311 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12312 if (flinfo.contents == NULL)
c152c796
AM
12313 goto error_return;
12314 }
12315
12316 if (max_external_reloc_size != 0)
12317 {
8b127cbc
AM
12318 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12319 if (flinfo.external_relocs == NULL)
c152c796
AM
12320 goto error_return;
12321 }
12322
12323 if (max_internal_reloc_count != 0)
12324 {
056bafd4 12325 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12326 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12327 if (flinfo.internal_relocs == NULL)
c152c796
AM
12328 goto error_return;
12329 }
12330
12331 if (max_sym_count != 0)
12332 {
12333 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12334 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12335 if (flinfo.external_syms == NULL)
c152c796
AM
12336 goto error_return;
12337
12338 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12339 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12340 if (flinfo.internal_syms == NULL)
c152c796
AM
12341 goto error_return;
12342
12343 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12344 flinfo.indices = (long int *) bfd_malloc (amt);
12345 if (flinfo.indices == NULL)
c152c796
AM
12346 goto error_return;
12347
12348 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12349 flinfo.sections = (asection **) bfd_malloc (amt);
12350 if (flinfo.sections == NULL)
c152c796
AM
12351 goto error_return;
12352 }
12353
12354 if (max_sym_shndx_count != 0)
12355 {
12356 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12357 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12358 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12359 goto error_return;
12360 }
12361
64f52338 12362 if (htab->tls_sec)
c152c796 12363 {
66631823 12364 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12365 asection *sec;
12366
64f52338 12367 for (sec = htab->tls_sec;
c152c796
AM
12368 sec && (sec->flags & SEC_THREAD_LOCAL);
12369 sec = sec->next)
12370 {
3a800eb9 12371 bfd_size_type size = sec->size;
66631823 12372 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12373
3a800eb9
AM
12374 if (size == 0
12375 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12376 {
91d6fa6a
NC
12377 struct bfd_link_order *ord = sec->map_tail.link_order;
12378
12379 if (ord != NULL)
66631823 12380 size = ord->offset * opb + ord->size;
c152c796 12381 }
66631823 12382 end = sec->vma + size / opb;
c152c796 12383 }
64f52338 12384 base = htab->tls_sec->vma;
7dc98aea
RO
12385 /* Only align end of TLS section if static TLS doesn't have special
12386 alignment requirements. */
12387 if (bed->static_tls_alignment == 1)
64f52338
AM
12388 end = align_power (end, htab->tls_sec->alignment_power);
12389 htab->tls_size = end - base;
c152c796
AM
12390 }
12391
0b52efa6
PB
12392 /* Reorder SHF_LINK_ORDER sections. */
12393 for (o = abfd->sections; o != NULL; o = o->next)
12394 {
12395 if (!elf_fixup_link_order (abfd, o))
12396 return FALSE;
12397 }
12398
2f0c68f2
CM
12399 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12400 return FALSE;
12401
c152c796
AM
12402 /* Since ELF permits relocations to be against local symbols, we
12403 must have the local symbols available when we do the relocations.
12404 Since we would rather only read the local symbols once, and we
12405 would rather not keep them in memory, we handle all the
12406 relocations for a single input file at the same time.
12407
12408 Unfortunately, there is no way to know the total number of local
12409 symbols until we have seen all of them, and the local symbol
12410 indices precede the global symbol indices. This means that when
12411 we are generating relocatable output, and we see a reloc against
12412 a global symbol, we can not know the symbol index until we have
12413 finished examining all the local symbols to see which ones we are
12414 going to output. To deal with this, we keep the relocations in
12415 memory, and don't output them until the end of the link. This is
12416 an unfortunate waste of memory, but I don't see a good way around
12417 it. Fortunately, it only happens when performing a relocatable
12418 link, which is not the common case. FIXME: If keep_memory is set
12419 we could write the relocs out and then read them again; I don't
12420 know how bad the memory loss will be. */
12421
c72f2fb2 12422 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12423 sub->output_has_begun = FALSE;
12424 for (o = abfd->sections; o != NULL; o = o->next)
12425 {
8423293d 12426 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12427 {
12428 if (p->type == bfd_indirect_link_order
12429 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12430 == bfd_target_elf_flavour)
12431 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12432 {
12433 if (! sub->output_has_begun)
12434 {
8b127cbc 12435 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12436 goto error_return;
12437 sub->output_has_begun = TRUE;
12438 }
12439 }
12440 else if (p->type == bfd_section_reloc_link_order
12441 || p->type == bfd_symbol_reloc_link_order)
12442 {
12443 if (! elf_reloc_link_order (abfd, info, o, p))
12444 goto error_return;
12445 }
12446 else
12447 {
12448 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12449 {
12450 if (p->type == bfd_indirect_link_order
12451 && (bfd_get_flavour (sub)
12452 == bfd_target_elf_flavour)
12453 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12454 != bed->s->elfclass))
12455 {
12456 const char *iclass, *oclass;
12457
aebf9be7 12458 switch (bed->s->elfclass)
351f65ca 12459 {
aebf9be7
NC
12460 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12461 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12462 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12463 default: abort ();
351f65ca 12464 }
aebf9be7
NC
12465
12466 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12467 {
aebf9be7
NC
12468 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12469 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12470 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12471 default: abort ();
351f65ca
L
12472 }
12473
12474 bfd_set_error (bfd_error_wrong_format);
4eca0228 12475 _bfd_error_handler
695344c0 12476 /* xgettext:c-format */
871b3ab2 12477 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12478 sub, iclass, oclass);
12479 }
12480
12481 goto error_return;
12482 }
c152c796
AM
12483 }
12484 }
12485 }
12486
c0f00686
L
12487 /* Free symbol buffer if needed. */
12488 if (!info->reduce_memory_overheads)
12489 {
c72f2fb2 12490 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12491 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12492 && elf_tdata (sub)->symbuf)
c0f00686
L
12493 {
12494 free (elf_tdata (sub)->symbuf);
12495 elf_tdata (sub)->symbuf = NULL;
12496 }
12497 }
12498
c152c796
AM
12499 /* Output any global symbols that got converted to local in a
12500 version script or due to symbol visibility. We do this in a
12501 separate step since ELF requires all local symbols to appear
12502 prior to any global symbols. FIXME: We should only do this if
12503 some global symbols were, in fact, converted to become local.
12504 FIXME: Will this work correctly with the Irix 5 linker? */
12505 eoinfo.failed = FALSE;
8b127cbc 12506 eoinfo.flinfo = &flinfo;
c152c796 12507 eoinfo.localsyms = TRUE;
34a79995 12508 eoinfo.file_sym_done = FALSE;
7686d77d 12509 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12510 if (eoinfo.failed)
12511 return FALSE;
12512
4e617b1e
PB
12513 /* If backend needs to output some local symbols not present in the hash
12514 table, do it now. */
8539e4e8
AM
12515 if (bed->elf_backend_output_arch_local_syms
12516 && (info->strip != strip_all || emit_relocs))
4e617b1e 12517 {
6e0b88f1 12518 typedef int (*out_sym_func)
4e617b1e
PB
12519 (void *, const char *, Elf_Internal_Sym *, asection *,
12520 struct elf_link_hash_entry *);
12521
12522 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12523 (abfd, info, &flinfo,
12524 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12525 return FALSE;
12526 }
12527
c152c796
AM
12528 /* That wrote out all the local symbols. Finish up the symbol table
12529 with the global symbols. Even if we want to strip everything we
12530 can, we still need to deal with those global symbols that got
12531 converted to local in a version script. */
12532
12533 /* The sh_info field records the index of the first non local symbol. */
12534 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12535
12536 if (dynamic
64f52338
AM
12537 && htab->dynsym != NULL
12538 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12539 {
12540 Elf_Internal_Sym sym;
64f52338 12541 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12542
64f52338
AM
12543 o = htab->dynsym->output_section;
12544 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12545
12546 /* Write out the section symbols for the output sections. */
0e1862bb 12547 if (bfd_link_pic (info)
64f52338 12548 || htab->is_relocatable_executable)
c152c796
AM
12549 {
12550 asection *s;
12551
12552 sym.st_size = 0;
12553 sym.st_name = 0;
12554 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12555 sym.st_other = 0;
35fc36a8 12556 sym.st_target_internal = 0;
c152c796
AM
12557
12558 for (s = abfd->sections; s != NULL; s = s->next)
12559 {
12560 int indx;
12561 bfd_byte *dest;
12562 long dynindx;
12563
c152c796 12564 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12565 if (dynindx <= 0)
12566 continue;
12567 indx = elf_section_data (s)->this_idx;
c152c796
AM
12568 BFD_ASSERT (indx > 0);
12569 sym.st_shndx = indx;
c0d5a53d
L
12570 if (! check_dynsym (abfd, &sym))
12571 return FALSE;
c152c796
AM
12572 sym.st_value = s->vma;
12573 dest = dynsym + dynindx * bed->s->sizeof_sym;
12574 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12575 }
c152c796
AM
12576 }
12577
12578 /* Write out the local dynsyms. */
64f52338 12579 if (htab->dynlocal)
c152c796
AM
12580 {
12581 struct elf_link_local_dynamic_entry *e;
64f52338 12582 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12583 {
12584 asection *s;
12585 bfd_byte *dest;
12586
935bd1e0 12587 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12588 Note that we saved a word of storage and overwrote
12589 the original st_name with the dynstr_index. */
12590 sym = e->isym;
935bd1e0 12591 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12592
cb33740c
AM
12593 s = bfd_section_from_elf_index (e->input_bfd,
12594 e->isym.st_shndx);
12595 if (s != NULL)
c152c796 12596 {
c152c796
AM
12597 sym.st_shndx =
12598 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12599 if (! check_dynsym (abfd, &sym))
12600 return FALSE;
c152c796
AM
12601 sym.st_value = (s->output_section->vma
12602 + s->output_offset
12603 + e->isym.st_value);
12604 }
12605
c152c796
AM
12606 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12607 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12608 }
12609 }
c152c796
AM
12610 }
12611
12612 /* We get the global symbols from the hash table. */
12613 eoinfo.failed = FALSE;
12614 eoinfo.localsyms = FALSE;
8b127cbc 12615 eoinfo.flinfo = &flinfo;
7686d77d 12616 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12617 if (eoinfo.failed)
12618 return FALSE;
12619
12620 /* If backend needs to output some symbols not present in the hash
12621 table, do it now. */
8539e4e8
AM
12622 if (bed->elf_backend_output_arch_syms
12623 && (info->strip != strip_all || emit_relocs))
c152c796 12624 {
6e0b88f1 12625 typedef int (*out_sym_func)
c152c796
AM
12626 (void *, const char *, Elf_Internal_Sym *, asection *,
12627 struct elf_link_hash_entry *);
12628
12629 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12630 (abfd, info, &flinfo,
12631 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12632 return FALSE;
12633 }
12634
ef10c3ac
L
12635 /* Finalize the .strtab section. */
12636 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12637
12638 /* Swap out the .strtab section. */
12639 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12640 return FALSE;
12641
12642 /* Now we know the size of the symtab section. */
c152c796
AM
12643 if (bfd_get_symcount (abfd) > 0)
12644 {
ee3b52e9
L
12645 /* Finish up and write out the symbol string table (.strtab)
12646 section. */
ad32986f 12647 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12648 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12649
ad32986f 12650 if (elf_symtab_shndx_list (abfd))
8539e4e8 12651 {
ad32986f 12652 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12653
ad32986f
NC
12654 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12655 {
12656 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12657 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12658 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12659 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12660 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12661
ad32986f
NC
12662 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12663 off, TRUE);
12664
12665 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12666 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12667 return FALSE;
12668 }
8539e4e8 12669 }
ee3b52e9
L
12670
12671 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12672 /* sh_name was set in prep_headers. */
12673 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12674 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12675 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12676 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12677 symstrtab_hdr->sh_entsize = 0;
12678 symstrtab_hdr->sh_link = 0;
12679 symstrtab_hdr->sh_info = 0;
12680 /* sh_offset is set just below. */
12681 symstrtab_hdr->sh_addralign = 1;
12682
12683 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12684 off, TRUE);
12685 elf_next_file_pos (abfd) = off;
12686
c152c796 12687 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12688 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12689 return FALSE;
12690 }
12691
76359541
TP
12692 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12693 {
871b3ab2 12694 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12695 info->out_implib_bfd);
76359541
TP
12696 return FALSE;
12697 }
12698
c152c796
AM
12699 /* Adjust the relocs to have the correct symbol indices. */
12700 for (o = abfd->sections; o != NULL; o = o->next)
12701 {
d4730f92 12702 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12703 bfd_boolean sort;
10bbbc1d 12704
c152c796
AM
12705 if ((o->flags & SEC_RELOC) == 0)
12706 continue;
12707
28dbcedc 12708 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12709 if (esdo->rel.hdr != NULL
10bbbc1d 12710 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12711 return FALSE;
12712 if (esdo->rela.hdr != NULL
10bbbc1d 12713 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12714 return FALSE;
c152c796
AM
12715
12716 /* Set the reloc_count field to 0 to prevent write_relocs from
12717 trying to swap the relocs out itself. */
12718 o->reloc_count = 0;
12719 }
12720
12721 if (dynamic && info->combreloc && dynobj != NULL)
12722 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12723
12724 /* If we are linking against a dynamic object, or generating a
12725 shared library, finish up the dynamic linking information. */
12726 if (dynamic)
12727 {
12728 bfd_byte *dyncon, *dynconend;
12729
12730 /* Fix up .dynamic entries. */
3d4d4302 12731 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12732 BFD_ASSERT (o != NULL);
12733
12734 dyncon = o->contents;
eea6121a 12735 dynconend = o->contents + o->size;
c152c796
AM
12736 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12737 {
12738 Elf_Internal_Dyn dyn;
12739 const char *name;
12740 unsigned int type;
64487780
AM
12741 bfd_size_type sh_size;
12742 bfd_vma sh_addr;
c152c796
AM
12743
12744 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12745
12746 switch (dyn.d_tag)
12747 {
12748 default:
12749 continue;
12750 case DT_NULL:
12751 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12752 {
12753 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12754 {
12755 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12756 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12757 default: continue;
12758 }
12759 dyn.d_un.d_val = relativecount;
12760 relativecount = 0;
12761 break;
12762 }
12763 continue;
12764
12765 case DT_INIT:
12766 name = info->init_function;
12767 goto get_sym;
12768 case DT_FINI:
12769 name = info->fini_function;
12770 get_sym:
12771 {
12772 struct elf_link_hash_entry *h;
12773
64f52338 12774 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12775 if (h != NULL
12776 && (h->root.type == bfd_link_hash_defined
12777 || h->root.type == bfd_link_hash_defweak))
12778 {
bef26483 12779 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12780 o = h->root.u.def.section;
12781 if (o->output_section != NULL)
bef26483 12782 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12783 + o->output_offset);
12784 else
12785 {
12786 /* The symbol is imported from another shared
12787 library and does not apply to this one. */
bef26483 12788 dyn.d_un.d_ptr = 0;
c152c796
AM
12789 }
12790 break;
12791 }
12792 }
12793 continue;
12794
12795 case DT_PREINIT_ARRAYSZ:
12796 name = ".preinit_array";
4ade44b7 12797 goto get_out_size;
c152c796
AM
12798 case DT_INIT_ARRAYSZ:
12799 name = ".init_array";
4ade44b7 12800 goto get_out_size;
c152c796
AM
12801 case DT_FINI_ARRAYSZ:
12802 name = ".fini_array";
4ade44b7 12803 get_out_size:
c152c796
AM
12804 o = bfd_get_section_by_name (abfd, name);
12805 if (o == NULL)
12806 {
4eca0228 12807 _bfd_error_handler
4ade44b7 12808 (_("could not find section %s"), name);
c152c796
AM
12809 goto error_return;
12810 }
eea6121a 12811 if (o->size == 0)
4eca0228 12812 _bfd_error_handler
c152c796 12813 (_("warning: %s section has zero size"), name);
eea6121a 12814 dyn.d_un.d_val = o->size;
c152c796
AM
12815 break;
12816
12817 case DT_PREINIT_ARRAY:
12818 name = ".preinit_array";
4ade44b7 12819 goto get_out_vma;
c152c796
AM
12820 case DT_INIT_ARRAY:
12821 name = ".init_array";
4ade44b7 12822 goto get_out_vma;
c152c796
AM
12823 case DT_FINI_ARRAY:
12824 name = ".fini_array";
4ade44b7
AM
12825 get_out_vma:
12826 o = bfd_get_section_by_name (abfd, name);
12827 goto do_vma;
c152c796
AM
12828
12829 case DT_HASH:
12830 name = ".hash";
12831 goto get_vma;
fdc90cb4
JJ
12832 case DT_GNU_HASH:
12833 name = ".gnu.hash";
12834 goto get_vma;
c152c796
AM
12835 case DT_STRTAB:
12836 name = ".dynstr";
12837 goto get_vma;
12838 case DT_SYMTAB:
12839 name = ".dynsym";
12840 goto get_vma;
12841 case DT_VERDEF:
12842 name = ".gnu.version_d";
12843 goto get_vma;
12844 case DT_VERNEED:
12845 name = ".gnu.version_r";
12846 goto get_vma;
12847 case DT_VERSYM:
12848 name = ".gnu.version";
12849 get_vma:
4ade44b7
AM
12850 o = bfd_get_linker_section (dynobj, name);
12851 do_vma:
b3293efa 12852 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12853 {
4eca0228 12854 _bfd_error_handler
4ade44b7 12855 (_("could not find section %s"), name);
c152c796
AM
12856 goto error_return;
12857 }
894891db
NC
12858 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12859 {
4eca0228 12860 _bfd_error_handler
894891db
NC
12861 (_("warning: section '%s' is being made into a note"), name);
12862 bfd_set_error (bfd_error_nonrepresentable_section);
12863 goto error_return;
12864 }
4ade44b7 12865 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12866 break;
12867
12868 case DT_REL:
12869 case DT_RELA:
12870 case DT_RELSZ:
12871 case DT_RELASZ:
12872 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12873 type = SHT_REL;
12874 else
12875 type = SHT_RELA;
64487780
AM
12876 sh_size = 0;
12877 sh_addr = 0;
c152c796
AM
12878 for (i = 1; i < elf_numsections (abfd); i++)
12879 {
12880 Elf_Internal_Shdr *hdr;
12881
12882 hdr = elf_elfsections (abfd)[i];
12883 if (hdr->sh_type == type
12884 && (hdr->sh_flags & SHF_ALLOC) != 0)
12885 {
64487780
AM
12886 sh_size += hdr->sh_size;
12887 if (sh_addr == 0
12888 || sh_addr > hdr->sh_addr)
12889 sh_addr = hdr->sh_addr;
c152c796
AM
12890 }
12891 }
64487780 12892
64f52338
AM
12893 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12894 {
66631823
CE
12895 unsigned int opb = bfd_octets_per_byte (abfd, o);
12896
64f52338
AM
12897 /* Don't count procedure linkage table relocs in the
12898 overall reloc count. */
64487780
AM
12899 sh_size -= htab->srelplt->size;
12900 if (sh_size == 0)
12901 /* If the size is zero, make the address zero too.
12902 This is to avoid a glibc bug. If the backend
12903 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12904 zero, then we'll put DT_RELA at the end of
12905 DT_JMPREL. glibc will interpret the end of
12906 DT_RELA matching the end of DT_JMPREL as the
12907 case where DT_RELA includes DT_JMPREL, and for
12908 LD_BIND_NOW will decide that processing DT_RELA
12909 will process the PLT relocs too. Net result:
12910 No PLT relocs applied. */
12911 sh_addr = 0;
12912
64f52338
AM
12913 /* If .rela.plt is the first .rela section, exclude
12914 it from DT_RELA. */
64487780 12915 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 12916 + htab->srelplt->output_offset) * opb)
64487780 12917 sh_addr += htab->srelplt->size;
64f52338 12918 }
64487780
AM
12919
12920 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12921 dyn.d_un.d_val = sh_size;
12922 else
12923 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12924 break;
12925 }
12926 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12927 }
12928 }
12929
12930 /* If we have created any dynamic sections, then output them. */
12931 if (dynobj != NULL)
12932 {
12933 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12934 goto error_return;
12935
943284cc 12936 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12937 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12938 || info->error_textrel)
3d4d4302 12939 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12940 {
12941 bfd_byte *dyncon, *dynconend;
12942
943284cc
DJ
12943 dyncon = o->contents;
12944 dynconend = o->contents + o->size;
12945 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12946 {
12947 Elf_Internal_Dyn dyn;
12948
12949 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12950
12951 if (dyn.d_tag == DT_TEXTREL)
12952 {
c192a133
AM
12953 if (info->error_textrel)
12954 info->callbacks->einfo
9793eb77 12955 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12956 else
12957 info->callbacks->einfo
9793eb77 12958 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12959 break;
12960 }
12961 }
12962 }
12963
c152c796
AM
12964 for (o = dynobj->sections; o != NULL; o = o->next)
12965 {
12966 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12967 || o->size == 0
c152c796
AM
12968 || o->output_section == bfd_abs_section_ptr)
12969 continue;
12970 if ((o->flags & SEC_LINKER_CREATED) == 0)
12971 {
12972 /* At this point, we are only interested in sections
12973 created by _bfd_elf_link_create_dynamic_sections. */
12974 continue;
12975 }
64f52338 12976 if (htab->stab_info.stabstr == o)
3722b82f 12977 continue;
64f52338 12978 if (htab->eh_info.hdr_sec == o)
eea6121a 12979 continue;
3d4d4302 12980 if (strcmp (o->name, ".dynstr") != 0)
c152c796 12981 {
bb294208
AM
12982 bfd_size_type octets = ((file_ptr) o->output_offset
12983 * bfd_octets_per_byte (abfd, o));
12984 if (!bfd_set_section_contents (abfd, o->output_section,
12985 o->contents, octets, o->size))
c152c796
AM
12986 goto error_return;
12987 }
12988 else
12989 {
12990 /* The contents of the .dynstr section are actually in a
12991 stringtab. */
8539e4e8
AM
12992 file_ptr off;
12993
c152c796
AM
12994 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12995 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12996 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12997 goto error_return;
12998 }
12999 }
13000 }
13001
7bdf4127 13002 if (!info->resolve_section_groups)
c152c796
AM
13003 {
13004 bfd_boolean failed = FALSE;
13005
7bdf4127 13006 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13007 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13008 if (failed)
13009 goto error_return;
13010 }
13011
13012 /* If we have optimized stabs strings, output them. */
64f52338 13013 if (htab->stab_info.stabstr != NULL)
c152c796 13014 {
64f52338 13015 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13016 goto error_return;
13017 }
13018
9f7c3e5e
AM
13019 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13020 goto error_return;
c152c796 13021
1ff6de03
NA
13022 if (info->callbacks->emit_ctf)
13023 info->callbacks->emit_ctf ();
13024
9f7c3e5e 13025 elf_final_link_free (abfd, &flinfo);
c152c796 13026
104d59d1
JM
13027 if (attr_section)
13028 {
a50b1753 13029 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13030 if (contents == NULL)
d0f16d5e 13031 return FALSE; /* Bail out and fail. */
104d59d1
JM
13032 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13033 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13034 free (contents);
13035 }
13036
c152c796
AM
13037 return TRUE;
13038
13039 error_return:
9f7c3e5e 13040 elf_final_link_free (abfd, &flinfo);
c152c796
AM
13041 return FALSE;
13042}
13043\f
5241d853
RS
13044/* Initialize COOKIE for input bfd ABFD. */
13045
13046static bfd_boolean
13047init_reloc_cookie (struct elf_reloc_cookie *cookie,
13048 struct bfd_link_info *info, bfd *abfd)
13049{
13050 Elf_Internal_Shdr *symtab_hdr;
13051 const struct elf_backend_data *bed;
13052
13053 bed = get_elf_backend_data (abfd);
13054 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13055
13056 cookie->abfd = abfd;
13057 cookie->sym_hashes = elf_sym_hashes (abfd);
13058 cookie->bad_symtab = elf_bad_symtab (abfd);
13059 if (cookie->bad_symtab)
13060 {
13061 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13062 cookie->extsymoff = 0;
13063 }
13064 else
13065 {
13066 cookie->locsymcount = symtab_hdr->sh_info;
13067 cookie->extsymoff = symtab_hdr->sh_info;
13068 }
13069
13070 if (bed->s->arch_size == 32)
13071 cookie->r_sym_shift = 8;
13072 else
13073 cookie->r_sym_shift = 32;
13074
13075 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13076 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13077 {
13078 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13079 cookie->locsymcount, 0,
13080 NULL, NULL, NULL);
13081 if (cookie->locsyms == NULL)
13082 {
13083 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13084 return FALSE;
13085 }
13086 if (info->keep_memory)
13087 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13088 }
13089 return TRUE;
13090}
13091
13092/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13093
13094static void
13095fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13096{
13097 Elf_Internal_Shdr *symtab_hdr;
13098
13099 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13100 if (cookie->locsyms != NULL
13101 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13102 free (cookie->locsyms);
13103}
13104
13105/* Initialize the relocation information in COOKIE for input section SEC
13106 of input bfd ABFD. */
13107
13108static bfd_boolean
13109init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13110 struct bfd_link_info *info, bfd *abfd,
13111 asection *sec)
13112{
5241d853
RS
13113 if (sec->reloc_count == 0)
13114 {
13115 cookie->rels = NULL;
13116 cookie->relend = NULL;
13117 }
13118 else
13119 {
5241d853
RS
13120 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13121 info->keep_memory);
13122 if (cookie->rels == NULL)
13123 return FALSE;
13124 cookie->rel = cookie->rels;
056bafd4 13125 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13126 }
13127 cookie->rel = cookie->rels;
13128 return TRUE;
13129}
13130
13131/* Free the memory allocated by init_reloc_cookie_rels,
13132 if appropriate. */
13133
13134static void
13135fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13136 asection *sec)
13137{
13138 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13139 free (cookie->rels);
13140}
13141
13142/* Initialize the whole of COOKIE for input section SEC. */
13143
13144static bfd_boolean
13145init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13146 struct bfd_link_info *info,
13147 asection *sec)
13148{
13149 if (!init_reloc_cookie (cookie, info, sec->owner))
13150 goto error1;
13151 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13152 goto error2;
13153 return TRUE;
13154
13155 error2:
13156 fini_reloc_cookie (cookie, sec->owner);
13157 error1:
13158 return FALSE;
13159}
13160
13161/* Free the memory allocated by init_reloc_cookie_for_section,
13162 if appropriate. */
13163
13164static void
13165fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13166 asection *sec)
13167{
13168 fini_reloc_cookie_rels (cookie, sec);
13169 fini_reloc_cookie (cookie, sec->owner);
13170}
13171\f
c152c796
AM
13172/* Garbage collect unused sections. */
13173
07adf181
AM
13174/* Default gc_mark_hook. */
13175
13176asection *
13177_bfd_elf_gc_mark_hook (asection *sec,
13178 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13179 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13180 struct elf_link_hash_entry *h,
13181 Elf_Internal_Sym *sym)
13182{
13183 if (h != NULL)
13184 {
13185 switch (h->root.type)
13186 {
13187 case bfd_link_hash_defined:
13188 case bfd_link_hash_defweak:
13189 return h->root.u.def.section;
13190
13191 case bfd_link_hash_common:
13192 return h->root.u.c.p->section;
13193
13194 default:
13195 break;
13196 }
13197 }
13198 else
13199 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13200
13201 return NULL;
13202}
13203
9e223787 13204/* Return the debug definition section. */
b7c871ed
L
13205
13206static asection *
13207elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13208 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13209 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13210 struct elf_link_hash_entry *h,
9e223787 13211 Elf_Internal_Sym *sym)
b7c871ed 13212{
9e223787
L
13213 if (h != NULL)
13214 {
13215 /* Return the global debug definition section. */
13216 if ((h->root.type == bfd_link_hash_defined
13217 || h->root.type == bfd_link_hash_defweak)
13218 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13219 return h->root.u.def.section;
13220 }
13221 else
13222 {
13223 /* Return the local debug definition section. */
13224 asection *isec = bfd_section_from_elf_index (sec->owner,
13225 sym->st_shndx);
13226 if ((isec->flags & SEC_DEBUGGING) != 0)
13227 return isec;
13228 }
b7c871ed
L
13229
13230 return NULL;
13231}
13232
5241d853
RS
13233/* COOKIE->rel describes a relocation against section SEC, which is
13234 a section we've decided to keep. Return the section that contains
13235 the relocation symbol, or NULL if no section contains it. */
13236
13237asection *
13238_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13239 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13240 struct elf_reloc_cookie *cookie,
13241 bfd_boolean *start_stop)
5241d853
RS
13242{
13243 unsigned long r_symndx;
3024a17a 13244 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13245
13246 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13247 if (r_symndx == STN_UNDEF)
5241d853
RS
13248 return NULL;
13249
13250 if (r_symndx >= cookie->locsymcount
13251 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13252 {
13253 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13254 if (h == NULL)
13255 {
871b3ab2 13256 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13257 sec->owner);
13258 return NULL;
13259 }
5241d853
RS
13260 while (h->root.type == bfd_link_hash_indirect
13261 || h->root.type == bfd_link_hash_warning)
13262 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13263 h->mark = 1;
3024a17a
AM
13264 /* Keep all aliases of the symbol too. If an object symbol
13265 needs to be copied into .dynbss then all of its aliases
13266 should be present as dynamic symbols, not just the one used
13267 on the copy relocation. */
13268 hw = h;
13269 while (hw->is_weakalias)
13270 {
13271 hw = hw->u.alias;
13272 hw->mark = 1;
13273 }
1cce69b9 13274
a6a4679f 13275 if (start_stop != NULL)
1cce69b9 13276 {
7dba9362
AM
13277 /* To work around a glibc bug, mark XXX input sections
13278 when there is a reference to __start_XXX or __stop_XXX
13279 symbols. */
cbd0eecf 13280 if (h->start_stop)
1cce69b9 13281 {
cbd0eecf 13282 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13283 *start_stop = !s->gc_mark;
13284 return s;
1cce69b9
AM
13285 }
13286 }
13287
5241d853
RS
13288 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13289 }
13290
13291 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13292 &cookie->locsyms[r_symndx]);
13293}
13294
13295/* COOKIE->rel describes a relocation against section SEC, which is
13296 a section we've decided to keep. Mark the section that contains
9d0a14d3 13297 the relocation symbol. */
5241d853
RS
13298
13299bfd_boolean
13300_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13301 asection *sec,
13302 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13303 struct elf_reloc_cookie *cookie)
5241d853
RS
13304{
13305 asection *rsec;
1cce69b9 13306 bfd_boolean start_stop = FALSE;
5241d853 13307
1cce69b9
AM
13308 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13309 while (rsec != NULL)
5241d853 13310 {
1cce69b9
AM
13311 if (!rsec->gc_mark)
13312 {
13313 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13314 || (rsec->owner->flags & DYNAMIC) != 0)
13315 rsec->gc_mark = 1;
13316 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13317 return FALSE;
13318 }
13319 if (!start_stop)
13320 break;
199af150 13321 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13322 }
13323 return TRUE;
13324}
13325
07adf181
AM
13326/* The mark phase of garbage collection. For a given section, mark
13327 it and any sections in this section's group, and all the sections
13328 which define symbols to which it refers. */
13329
ccfa59ea
AM
13330bfd_boolean
13331_bfd_elf_gc_mark (struct bfd_link_info *info,
13332 asection *sec,
6a5bb875 13333 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13334{
13335 bfd_boolean ret;
9d0a14d3 13336 asection *group_sec, *eh_frame;
c152c796
AM
13337
13338 sec->gc_mark = 1;
13339
13340 /* Mark all the sections in the group. */
13341 group_sec = elf_section_data (sec)->next_in_group;
13342 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13343 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13344 return FALSE;
13345
13346 /* Look through the section relocs. */
13347 ret = TRUE;
9d0a14d3
RS
13348 eh_frame = elf_eh_frame_section (sec->owner);
13349 if ((sec->flags & SEC_RELOC) != 0
13350 && sec->reloc_count > 0
13351 && sec != eh_frame)
c152c796 13352 {
5241d853 13353 struct elf_reloc_cookie cookie;
c152c796 13354
5241d853
RS
13355 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13356 ret = FALSE;
c152c796 13357 else
c152c796 13358 {
5241d853 13359 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13360 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13361 {
13362 ret = FALSE;
13363 break;
13364 }
13365 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13366 }
13367 }
9d0a14d3
RS
13368
13369 if (ret && eh_frame && elf_fde_list (sec))
13370 {
13371 struct elf_reloc_cookie cookie;
13372
13373 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13374 ret = FALSE;
13375 else
13376 {
13377 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13378 gc_mark_hook, &cookie))
13379 ret = FALSE;
13380 fini_reloc_cookie_for_section (&cookie, eh_frame);
13381 }
13382 }
13383
2f0c68f2
CM
13384 eh_frame = elf_section_eh_frame_entry (sec);
13385 if (ret && eh_frame && !eh_frame->gc_mark)
13386 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13387 ret = FALSE;
13388
c152c796
AM
13389 return ret;
13390}
13391
3c758495
TG
13392/* Scan and mark sections in a special or debug section group. */
13393
13394static void
13395_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13396{
13397 /* Point to first section of section group. */
13398 asection *ssec;
13399 /* Used to iterate the section group. */
13400 asection *msec;
13401
13402 bfd_boolean is_special_grp = TRUE;
13403 bfd_boolean is_debug_grp = TRUE;
13404
13405 /* First scan to see if group contains any section other than debug
13406 and special section. */
13407 ssec = msec = elf_next_in_group (grp);
13408 do
13409 {
13410 if ((msec->flags & SEC_DEBUGGING) == 0)
13411 is_debug_grp = FALSE;
13412
13413 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13414 is_special_grp = FALSE;
13415
13416 msec = elf_next_in_group (msec);
13417 }
13418 while (msec != ssec);
13419
13420 /* If this is a pure debug section group or pure special section group,
13421 keep all sections in this group. */
13422 if (is_debug_grp || is_special_grp)
13423 {
13424 do
13425 {
13426 msec->gc_mark = 1;
13427 msec = elf_next_in_group (msec);
13428 }
13429 while (msec != ssec);
13430 }
13431}
13432
7f6ab9f8
AM
13433/* Keep debug and special sections. */
13434
13435bfd_boolean
13436_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13437 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13438{
13439 bfd *ibfd;
13440
c72f2fb2 13441 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13442 {
13443 asection *isec;
13444 bfd_boolean some_kept;
b40bf0a2 13445 bfd_boolean debug_frag_seen;
b7c871ed 13446 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13447
13448 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13449 continue;
57963c05
AM
13450 isec = ibfd->sections;
13451 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13452 continue;
7f6ab9f8 13453
b40bf0a2
NC
13454 /* Ensure all linker created sections are kept,
13455 see if any other section is already marked,
13456 and note if we have any fragmented debug sections. */
b7c871ed 13457 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13458 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13459 {
13460 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13461 isec->gc_mark = 1;
eb026f09
AM
13462 else if (isec->gc_mark
13463 && (isec->flags & SEC_ALLOC) != 0
13464 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13465 some_kept = TRUE;
b7d07216
L
13466 else
13467 {
13468 /* Since all sections, except for backend specific ones,
13469 have been garbage collected, call mark_hook on this
13470 section if any of its linked-to sections is marked. */
13471 asection *linked_to_sec = elf_linked_to_section (isec);
13472 for (; linked_to_sec != NULL;
13473 linked_to_sec = elf_linked_to_section (linked_to_sec))
13474 if (linked_to_sec->gc_mark)
13475 {
13476 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13477 return FALSE;
13478 break;
13479 }
13480 }
b40bf0a2 13481
535b785f 13482 if (!debug_frag_seen
b40bf0a2
NC
13483 && (isec->flags & SEC_DEBUGGING)
13484 && CONST_STRNEQ (isec->name, ".debug_line."))
13485 debug_frag_seen = TRUE;
5242a0a0
L
13486 else if (strcmp (bfd_section_name (isec),
13487 "__patchable_function_entries") == 0
13488 && elf_linked_to_section (isec) == NULL)
13489 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13490 "need linked-to section "
13491 "for --gc-sections\n"),
13492 isec->owner, isec);
7f6ab9f8
AM
13493 }
13494
eb026f09
AM
13495 /* If no non-note alloc section in this file will be kept, then
13496 we can toss out the debug and special sections. */
7f6ab9f8
AM
13497 if (!some_kept)
13498 continue;
13499
13500 /* Keep debug and special sections like .comment when they are
3c758495 13501 not part of a group. Also keep section groups that contain
b7d07216
L
13502 just debug sections or special sections. NB: Sections with
13503 linked-to section has been handled above. */
7f6ab9f8 13504 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13505 {
13506 if ((isec->flags & SEC_GROUP) != 0)
13507 _bfd_elf_gc_mark_debug_special_section_group (isec);
13508 else if (((isec->flags & SEC_DEBUGGING) != 0
13509 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13510 && elf_next_in_group (isec) == NULL
13511 && elf_linked_to_section (isec) == NULL)
3c758495 13512 isec->gc_mark = 1;
b7c871ed
L
13513 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13514 has_kept_debug_info = TRUE;
3c758495 13515 }
b40bf0a2 13516
b40bf0a2
NC
13517 /* Look for CODE sections which are going to be discarded,
13518 and find and discard any fragmented debug sections which
13519 are associated with that code section. */
b7c871ed
L
13520 if (debug_frag_seen)
13521 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13522 if ((isec->flags & SEC_CODE) != 0
13523 && isec->gc_mark == 0)
13524 {
13525 unsigned int ilen;
13526 asection *dsec;
b40bf0a2 13527
b7c871ed 13528 ilen = strlen (isec->name);
b40bf0a2 13529
b7c871ed 13530 /* Association is determined by the name of the debug
07d6d2b8 13531 section containing the name of the code section as
b7c871ed
L
13532 a suffix. For example .debug_line.text.foo is a
13533 debug section associated with .text.foo. */
13534 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13535 {
13536 unsigned int dlen;
b40bf0a2 13537
b7c871ed
L
13538 if (dsec->gc_mark == 0
13539 || (dsec->flags & SEC_DEBUGGING) == 0)
13540 continue;
b40bf0a2 13541
b7c871ed 13542 dlen = strlen (dsec->name);
b40bf0a2 13543
b7c871ed
L
13544 if (dlen > ilen
13545 && strncmp (dsec->name + (dlen - ilen),
13546 isec->name, ilen) == 0)
b40bf0a2 13547 dsec->gc_mark = 0;
b7c871ed 13548 }
b40bf0a2 13549 }
b7c871ed
L
13550
13551 /* Mark debug sections referenced by kept debug sections. */
13552 if (has_kept_debug_info)
13553 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13554 if (isec->gc_mark
13555 && (isec->flags & SEC_DEBUGGING) != 0)
13556 if (!_bfd_elf_gc_mark (info, isec,
13557 elf_gc_mark_debug_section))
13558 return FALSE;
7f6ab9f8
AM
13559 }
13560 return TRUE;
13561}
13562
c152c796 13563static bfd_boolean
ccabcbe5 13564elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13565{
13566 bfd *sub;
ccabcbe5 13567 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13568
c72f2fb2 13569 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13570 {
13571 asection *o;
13572
b19a8f85 13573 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13574 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13575 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13576 continue;
57963c05
AM
13577 o = sub->sections;
13578 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13579 continue;
c152c796
AM
13580
13581 for (o = sub->sections; o != NULL; o = o->next)
13582 {
a33dafc3
L
13583 /* When any section in a section group is kept, we keep all
13584 sections in the section group. If the first member of
13585 the section group is excluded, we will also exclude the
13586 group section. */
13587 if (o->flags & SEC_GROUP)
13588 {
13589 asection *first = elf_next_in_group (o);
13590 o->gc_mark = first->gc_mark;
13591 }
c152c796 13592
1e7eae0d 13593 if (o->gc_mark)
c152c796
AM
13594 continue;
13595
13596 /* Skip sweeping sections already excluded. */
13597 if (o->flags & SEC_EXCLUDE)
13598 continue;
13599
13600 /* Since this is early in the link process, it is simple
13601 to remove a section from the output. */
13602 o->flags |= SEC_EXCLUDE;
13603
c55fe096 13604 if (info->print_gc_sections && o->size != 0)
695344c0 13605 /* xgettext:c-format */
9793eb77 13606 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13607 o, sub);
c152c796
AM
13608 }
13609 }
13610
c152c796
AM
13611 return TRUE;
13612}
13613
13614/* Propagate collected vtable information. This is called through
13615 elf_link_hash_traverse. */
13616
13617static bfd_boolean
13618elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13619{
c152c796 13620 /* Those that are not vtables. */
cbd0eecf
L
13621 if (h->start_stop
13622 || h->u2.vtable == NULL
13623 || h->u2.vtable->parent == NULL)
c152c796
AM
13624 return TRUE;
13625
13626 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13627 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13628 return TRUE;
13629
13630 /* If we've already been done, exit. */
cbd0eecf 13631 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13632 return TRUE;
13633
13634 /* Make sure the parent's table is up to date. */
cbd0eecf 13635 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13636
cbd0eecf 13637 if (h->u2.vtable->used == NULL)
c152c796
AM
13638 {
13639 /* None of this table's entries were referenced. Re-use the
13640 parent's table. */
cbd0eecf
L
13641 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13642 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13643 }
13644 else
13645 {
13646 size_t n;
13647 bfd_boolean *cu, *pu;
13648
13649 /* Or the parent's entries into ours. */
cbd0eecf 13650 cu = h->u2.vtable->used;
c152c796 13651 cu[-1] = TRUE;
cbd0eecf 13652 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13653 if (pu != NULL)
13654 {
13655 const struct elf_backend_data *bed;
13656 unsigned int log_file_align;
13657
13658 bed = get_elf_backend_data (h->root.u.def.section->owner);
13659 log_file_align = bed->s->log_file_align;
cbd0eecf 13660 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13661 while (n--)
13662 {
13663 if (*pu)
13664 *cu = TRUE;
13665 pu++;
13666 cu++;
13667 }
13668 }
13669 }
13670
13671 return TRUE;
13672}
13673
13674static bfd_boolean
13675elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13676{
13677 asection *sec;
13678 bfd_vma hstart, hend;
13679 Elf_Internal_Rela *relstart, *relend, *rel;
13680 const struct elf_backend_data *bed;
13681 unsigned int log_file_align;
13682
c152c796
AM
13683 /* Take care of both those symbols that do not describe vtables as
13684 well as those that are not loaded. */
cbd0eecf
L
13685 if (h->start_stop
13686 || h->u2.vtable == NULL
13687 || h->u2.vtable->parent == NULL)
c152c796
AM
13688 return TRUE;
13689
13690 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13691 || h->root.type == bfd_link_hash_defweak);
13692
13693 sec = h->root.u.def.section;
13694 hstart = h->root.u.def.value;
13695 hend = hstart + h->size;
13696
13697 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13698 if (!relstart)
13699 return *(bfd_boolean *) okp = FALSE;
13700 bed = get_elf_backend_data (sec->owner);
13701 log_file_align = bed->s->log_file_align;
13702
056bafd4 13703 relend = relstart + sec->reloc_count;
c152c796
AM
13704
13705 for (rel = relstart; rel < relend; ++rel)
13706 if (rel->r_offset >= hstart && rel->r_offset < hend)
13707 {
13708 /* If the entry is in use, do nothing. */
cbd0eecf
L
13709 if (h->u2.vtable->used
13710 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13711 {
13712 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13713 if (h->u2.vtable->used[entry])
c152c796
AM
13714 continue;
13715 }
13716 /* Otherwise, kill it. */
13717 rel->r_offset = rel->r_info = rel->r_addend = 0;
13718 }
13719
13720 return TRUE;
13721}
13722
87538722
AM
13723/* Mark sections containing dynamically referenced symbols. When
13724 building shared libraries, we must assume that any visible symbol is
13725 referenced. */
715df9b8 13726
64d03ab5
AM
13727bfd_boolean
13728bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13729{
87538722 13730 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13731 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13732
715df9b8
EB
13733 if ((h->root.type == bfd_link_hash_defined
13734 || h->root.type == bfd_link_hash_defweak)
d664fd41 13735 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13736 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13737 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13738 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13739 && (!bfd_link_executable (info)
22185505 13740 || info->gc_keep_exported
b407645f
AM
13741 || info->export_dynamic
13742 || (h->dynamic
13743 && d != NULL
13744 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13745 && (h->versioned >= versioned
54e8959c
L
13746 || !bfd_hide_sym_by_version (info->version_info,
13747 h->root.root.string)))))
715df9b8
EB
13748 h->root.u.def.section->flags |= SEC_KEEP;
13749
13750 return TRUE;
13751}
3b36f7e6 13752
74f0fb50
AM
13753/* Keep all sections containing symbols undefined on the command-line,
13754 and the section containing the entry symbol. */
13755
13756void
13757_bfd_elf_gc_keep (struct bfd_link_info *info)
13758{
13759 struct bfd_sym_chain *sym;
13760
13761 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13762 {
13763 struct elf_link_hash_entry *h;
13764
13765 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13766 FALSE, FALSE, FALSE);
13767
13768 if (h != NULL
13769 && (h->root.type == bfd_link_hash_defined
13770 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13771 && !bfd_is_abs_section (h->root.u.def.section)
13772 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13773 h->root.u.def.section->flags |= SEC_KEEP;
13774 }
13775}
13776
2f0c68f2
CM
13777bfd_boolean
13778bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13779 struct bfd_link_info *info)
13780{
13781 bfd *ibfd = info->input_bfds;
13782
13783 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13784 {
13785 asection *sec;
13786 struct elf_reloc_cookie cookie;
13787
13788 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13789 continue;
57963c05
AM
13790 sec = ibfd->sections;
13791 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13792 continue;
2f0c68f2
CM
13793
13794 if (!init_reloc_cookie (&cookie, info, ibfd))
13795 return FALSE;
13796
13797 for (sec = ibfd->sections; sec; sec = sec->next)
13798 {
fd361982 13799 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13800 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13801 {
13802 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13803 fini_reloc_cookie_rels (&cookie, sec);
13804 }
13805 }
13806 }
13807 return TRUE;
13808}
13809
c152c796
AM
13810/* Do mark and sweep of unused sections. */
13811
13812bfd_boolean
13813bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13814{
13815 bfd_boolean ok = TRUE;
13816 bfd *sub;
6a5bb875 13817 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13818 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13819 struct elf_link_hash_table *htab;
c152c796 13820
64d03ab5 13821 if (!bed->can_gc_sections
715df9b8 13822 || !is_elf_hash_table (info->hash))
c152c796 13823 {
9793eb77 13824 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13825 return TRUE;
13826 }
13827
74f0fb50 13828 bed->gc_keep (info);
da44f4e5 13829 htab = elf_hash_table (info);
74f0fb50 13830
9d0a14d3
RS
13831 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13832 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13833 for (sub = info->input_bfds;
13834 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13835 sub = sub->link.next)
9d0a14d3
RS
13836 {
13837 asection *sec;
13838 struct elf_reloc_cookie cookie;
13839
57963c05
AM
13840 sec = sub->sections;
13841 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13842 continue;
9d0a14d3 13843 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13844 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13845 {
13846 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13847 if (elf_section_data (sec)->sec_info
13848 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13849 elf_eh_frame_section (sub) = sec;
13850 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13851 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13852 }
13853 }
9d0a14d3 13854
c152c796 13855 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13856 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13857 if (!ok)
13858 return FALSE;
13859
13860 /* Kill the vtable relocations that were not used. */
da44f4e5 13861 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13862 if (!ok)
13863 return FALSE;
13864
715df9b8 13865 /* Mark dynamically referenced symbols. */
22185505 13866 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13867 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13868
715df9b8 13869 /* Grovel through relocs to find out who stays ... */
64d03ab5 13870 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13871 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13872 {
13873 asection *o;
13874
b19a8f85 13875 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13876 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13877 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13878 continue;
13879
57963c05
AM
13880 o = sub->sections;
13881 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13882 continue;
13883
7f6ab9f8
AM
13884 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13885 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13886 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13887 well as FINI_ARRAY sections for ld -r. */
c152c796 13888 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13889 if (!o->gc_mark
13890 && (o->flags & SEC_EXCLUDE) == 0
24007750 13891 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13892 || (bfd_link_relocatable (info)
13893 && ((elf_section_data (o)->this_hdr.sh_type
13894 == SHT_PREINIT_ARRAY)
13895 || (elf_section_data (o)->this_hdr.sh_type
13896 == SHT_INIT_ARRAY)
13897 || (elf_section_data (o)->this_hdr.sh_type
13898 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13899 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13900 && elf_next_in_group (o) == NULL )))
13901 {
13902 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13903 return FALSE;
13904 }
c152c796
AM
13905 }
13906
6a5bb875 13907 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13908 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13909
c152c796 13910 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13911 return elf_gc_sweep (abfd, info);
c152c796
AM
13912}
13913\f
13914/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13915
13916bfd_boolean
13917bfd_elf_gc_record_vtinherit (bfd *abfd,
13918 asection *sec,
13919 struct elf_link_hash_entry *h,
13920 bfd_vma offset)
13921{
13922 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13923 struct elf_link_hash_entry **search, *child;
ef53be89 13924 size_t extsymcount;
c152c796
AM
13925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13926
13927 /* The sh_info field of the symtab header tells us where the
13928 external symbols start. We don't care about the local symbols at
13929 this point. */
13930 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13931 if (!elf_bad_symtab (abfd))
13932 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13933
13934 sym_hashes = elf_sym_hashes (abfd);
13935 sym_hashes_end = sym_hashes + extsymcount;
13936
13937 /* Hunt down the child symbol, which is in this section at the same
13938 offset as the relocation. */
13939 for (search = sym_hashes; search != sym_hashes_end; ++search)
13940 {
13941 if ((child = *search) != NULL
13942 && (child->root.type == bfd_link_hash_defined
13943 || child->root.type == bfd_link_hash_defweak)
13944 && child->root.u.def.section == sec
13945 && child->root.u.def.value == offset)
13946 goto win;
13947 }
13948
695344c0 13949 /* xgettext:c-format */
9793eb77 13950 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13951 abfd, sec, (uint64_t) offset);
c152c796
AM
13952 bfd_set_error (bfd_error_invalid_operation);
13953 return FALSE;
13954
13955 win:
cbd0eecf 13956 if (!child->u2.vtable)
f6e332e6 13957 {
cbd0eecf
L
13958 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13959 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13960 if (!child->u2.vtable)
f6e332e6
AM
13961 return FALSE;
13962 }
c152c796
AM
13963 if (!h)
13964 {
13965 /* This *should* only be the absolute section. It could potentially
13966 be that someone has defined a non-global vtable though, which
13967 would be bad. It isn't worth paging in the local symbols to be
13968 sure though; that case should simply be handled by the assembler. */
13969
cbd0eecf 13970 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13971 }
13972 else
cbd0eecf 13973 child->u2.vtable->parent = h;
c152c796
AM
13974
13975 return TRUE;
13976}
13977
13978/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13979
13980bfd_boolean
a0ea3a14 13981bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13982 struct elf_link_hash_entry *h,
13983 bfd_vma addend)
13984{
13985 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13986 unsigned int log_file_align = bed->s->log_file_align;
13987
a0ea3a14
L
13988 if (!h)
13989 {
13990 /* xgettext:c-format */
13991 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13992 abfd, sec);
13993 bfd_set_error (bfd_error_bad_value);
13994 return FALSE;
13995 }
13996
cbd0eecf 13997 if (!h->u2.vtable)
f6e332e6 13998 {
cbd0eecf
L
13999 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14000 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14001 if (!h->u2.vtable)
f6e332e6
AM
14002 return FALSE;
14003 }
14004
cbd0eecf 14005 if (addend >= h->u2.vtable->size)
c152c796
AM
14006 {
14007 size_t size, bytes, file_align;
cbd0eecf 14008 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14009
14010 /* While the symbol is undefined, we have to be prepared to handle
14011 a zero size. */
14012 file_align = 1 << log_file_align;
14013 if (h->root.type == bfd_link_hash_undefined)
14014 size = addend + file_align;
14015 else
14016 {
14017 size = h->size;
14018 if (addend >= size)
14019 {
14020 /* Oops! We've got a reference past the defined end of
14021 the table. This is probably a bug -- shall we warn? */
14022 size = addend + file_align;
14023 }
14024 }
14025 size = (size + file_align - 1) & -file_align;
14026
14027 /* Allocate one extra entry for use as a "done" flag for the
14028 consolidation pass. */
14029 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14030
14031 if (ptr)
14032 {
a50b1753 14033 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14034
14035 if (ptr != NULL)
14036 {
14037 size_t oldbytes;
14038
cbd0eecf 14039 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14040 * sizeof (bfd_boolean));
14041 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14042 }
14043 }
14044 else
a50b1753 14045 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14046
14047 if (ptr == NULL)
14048 return FALSE;
14049
14050 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14051 h->u2.vtable->used = ptr + 1;
14052 h->u2.vtable->size = size;
c152c796
AM
14053 }
14054
cbd0eecf 14055 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14056
14057 return TRUE;
14058}
14059
ae17ab41
CM
14060/* Map an ELF section header flag to its corresponding string. */
14061typedef struct
14062{
14063 char *flag_name;
14064 flagword flag_value;
14065} elf_flags_to_name_table;
14066
14067static elf_flags_to_name_table elf_flags_to_names [] =
14068{
14069 { "SHF_WRITE", SHF_WRITE },
14070 { "SHF_ALLOC", SHF_ALLOC },
14071 { "SHF_EXECINSTR", SHF_EXECINSTR },
14072 { "SHF_MERGE", SHF_MERGE },
14073 { "SHF_STRINGS", SHF_STRINGS },
14074 { "SHF_INFO_LINK", SHF_INFO_LINK},
14075 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14076 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14077 { "SHF_GROUP", SHF_GROUP },
14078 { "SHF_TLS", SHF_TLS },
14079 { "SHF_MASKOS", SHF_MASKOS },
14080 { "SHF_EXCLUDE", SHF_EXCLUDE },
14081};
14082
b9c361e0
JL
14083/* Returns TRUE if the section is to be included, otherwise FALSE. */
14084bfd_boolean
ae17ab41 14085bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14086 struct flag_info *flaginfo,
b9c361e0 14087 asection *section)
ae17ab41 14088{
8b127cbc 14089 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14090
8b127cbc 14091 if (!flaginfo->flags_initialized)
ae17ab41 14092 {
8b127cbc
AM
14093 bfd *obfd = info->output_bfd;
14094 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14095 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14096 int with_hex = 0;
14097 int without_hex = 0;
14098
8b127cbc 14099 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14100 {
b9c361e0 14101 unsigned i;
8b127cbc 14102 flagword (*lookup) (char *);
ae17ab41 14103
8b127cbc
AM
14104 lookup = bed->elf_backend_lookup_section_flags_hook;
14105 if (lookup != NULL)
ae17ab41 14106 {
8b127cbc 14107 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14108
14109 if (hexval != 0)
14110 {
14111 if (tf->with == with_flags)
14112 with_hex |= hexval;
14113 else if (tf->with == without_flags)
14114 without_hex |= hexval;
14115 tf->valid = TRUE;
14116 continue;
14117 }
ae17ab41 14118 }
8b127cbc 14119 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14120 {
8b127cbc 14121 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14122 {
14123 if (tf->with == with_flags)
14124 with_hex |= elf_flags_to_names[i].flag_value;
14125 else if (tf->with == without_flags)
14126 without_hex |= elf_flags_to_names[i].flag_value;
14127 tf->valid = TRUE;
14128 break;
14129 }
14130 }
8b127cbc 14131 if (!tf->valid)
b9c361e0 14132 {
68ffbac6 14133 info->callbacks->einfo
9793eb77 14134 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14135 return FALSE;
ae17ab41
CM
14136 }
14137 }
8b127cbc
AM
14138 flaginfo->flags_initialized = TRUE;
14139 flaginfo->only_with_flags |= with_hex;
14140 flaginfo->not_with_flags |= without_hex;
ae17ab41 14141 }
ae17ab41 14142
8b127cbc 14143 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14144 return FALSE;
14145
8b127cbc 14146 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14147 return FALSE;
14148
14149 return TRUE;
ae17ab41
CM
14150}
14151
c152c796
AM
14152struct alloc_got_off_arg {
14153 bfd_vma gotoff;
10455f89 14154 struct bfd_link_info *info;
c152c796
AM
14155};
14156
14157/* We need a special top-level link routine to convert got reference counts
14158 to real got offsets. */
14159
14160static bfd_boolean
14161elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14162{
a50b1753 14163 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14164 bfd *obfd = gofarg->info->output_bfd;
14165 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14166
c152c796
AM
14167 if (h->got.refcount > 0)
14168 {
14169 h->got.offset = gofarg->gotoff;
10455f89 14170 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14171 }
14172 else
14173 h->got.offset = (bfd_vma) -1;
14174
14175 return TRUE;
14176}
14177
14178/* And an accompanying bit to work out final got entry offsets once
14179 we're done. Should be called from final_link. */
14180
14181bfd_boolean
14182bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14183 struct bfd_link_info *info)
14184{
14185 bfd *i;
14186 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14187 bfd_vma gotoff;
c152c796
AM
14188 struct alloc_got_off_arg gofarg;
14189
10455f89
HPN
14190 BFD_ASSERT (abfd == info->output_bfd);
14191
c152c796
AM
14192 if (! is_elf_hash_table (info->hash))
14193 return FALSE;
14194
14195 /* The GOT offset is relative to the .got section, but the GOT header is
14196 put into the .got.plt section, if the backend uses it. */
14197 if (bed->want_got_plt)
14198 gotoff = 0;
14199 else
14200 gotoff = bed->got_header_size;
14201
14202 /* Do the local .got entries first. */
c72f2fb2 14203 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14204 {
14205 bfd_signed_vma *local_got;
ef53be89 14206 size_t j, locsymcount;
c152c796
AM
14207 Elf_Internal_Shdr *symtab_hdr;
14208
14209 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14210 continue;
14211
14212 local_got = elf_local_got_refcounts (i);
14213 if (!local_got)
14214 continue;
14215
14216 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14217 if (elf_bad_symtab (i))
14218 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14219 else
14220 locsymcount = symtab_hdr->sh_info;
14221
14222 for (j = 0; j < locsymcount; ++j)
14223 {
14224 if (local_got[j] > 0)
14225 {
14226 local_got[j] = gotoff;
10455f89 14227 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14228 }
14229 else
14230 local_got[j] = (bfd_vma) -1;
14231 }
14232 }
14233
14234 /* Then the global .got entries. .plt refcounts are handled by
14235 adjust_dynamic_symbol */
14236 gofarg.gotoff = gotoff;
10455f89 14237 gofarg.info = info;
c152c796
AM
14238 elf_link_hash_traverse (elf_hash_table (info),
14239 elf_gc_allocate_got_offsets,
14240 &gofarg);
14241 return TRUE;
14242}
14243
14244/* Many folk need no more in the way of final link than this, once
14245 got entry reference counting is enabled. */
14246
14247bfd_boolean
14248bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14249{
14250 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14251 return FALSE;
14252
14253 /* Invoke the regular ELF backend linker to do all the work. */
14254 return bfd_elf_final_link (abfd, info);
14255}
14256
14257bfd_boolean
14258bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14259{
a50b1753 14260 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14261
14262 if (rcookie->bad_symtab)
14263 rcookie->rel = rcookie->rels;
14264
14265 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14266 {
14267 unsigned long r_symndx;
14268
14269 if (! rcookie->bad_symtab)
14270 if (rcookie->rel->r_offset > offset)
14271 return FALSE;
14272 if (rcookie->rel->r_offset != offset)
14273 continue;
14274
14275 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14276 if (r_symndx == STN_UNDEF)
c152c796
AM
14277 return TRUE;
14278
14279 if (r_symndx >= rcookie->locsymcount
14280 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14281 {
14282 struct elf_link_hash_entry *h;
14283
14284 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14285
14286 while (h->root.type == bfd_link_hash_indirect
14287 || h->root.type == bfd_link_hash_warning)
14288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14289
14290 if ((h->root.type == bfd_link_hash_defined
14291 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14292 && (h->root.u.def.section->owner != rcookie->abfd
14293 || h->root.u.def.section->kept_section != NULL
14294 || discarded_section (h->root.u.def.section)))
c152c796 14295 return TRUE;
c152c796
AM
14296 }
14297 else
14298 {
14299 /* It's not a relocation against a global symbol,
14300 but it could be a relocation against a local
14301 symbol for a discarded section. */
14302 asection *isec;
14303 Elf_Internal_Sym *isym;
14304
14305 /* Need to: get the symbol; get the section. */
14306 isym = &rcookie->locsyms[r_symndx];
cb33740c 14307 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14308 if (isec != NULL
14309 && (isec->kept_section != NULL
14310 || discarded_section (isec)))
cb33740c 14311 return TRUE;
c152c796
AM
14312 }
14313 return FALSE;
14314 }
14315 return FALSE;
14316}
14317
14318/* Discard unneeded references to discarded sections.
75938853
AM
14319 Returns -1 on error, 1 if any section's size was changed, 0 if
14320 nothing changed. This function assumes that the relocations are in
14321 sorted order, which is true for all known assemblers. */
c152c796 14322
75938853 14323int
c152c796
AM
14324bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14325{
14326 struct elf_reloc_cookie cookie;
18cd5bce 14327 asection *o;
c152c796 14328 bfd *abfd;
75938853 14329 int changed = 0;
c152c796
AM
14330
14331 if (info->traditional_format
14332 || !is_elf_hash_table (info->hash))
75938853 14333 return 0;
c152c796 14334
18cd5bce
AM
14335 o = bfd_get_section_by_name (output_bfd, ".stab");
14336 if (o != NULL)
c152c796 14337 {
18cd5bce 14338 asection *i;
c152c796 14339
18cd5bce 14340 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14341 {
18cd5bce
AM
14342 if (i->size == 0
14343 || i->reloc_count == 0
14344 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14345 continue;
c152c796 14346
18cd5bce
AM
14347 abfd = i->owner;
14348 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14349 continue;
c152c796 14350
18cd5bce 14351 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14352 return -1;
c152c796 14353
18cd5bce
AM
14354 if (_bfd_discard_section_stabs (abfd, i,
14355 elf_section_data (i)->sec_info,
5241d853
RS
14356 bfd_elf_reloc_symbol_deleted_p,
14357 &cookie))
75938853 14358 changed = 1;
18cd5bce
AM
14359
14360 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14361 }
18cd5bce
AM
14362 }
14363
2f0c68f2
CM
14364 o = NULL;
14365 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14366 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14367 if (o != NULL)
14368 {
14369 asection *i;
d7153c4a 14370 int eh_changed = 0;
66631823 14371 unsigned int eh_alignment; /* Octets. */
c152c796 14372
18cd5bce 14373 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14374 {
18cd5bce
AM
14375 if (i->size == 0)
14376 continue;
14377
14378 abfd = i->owner;
14379 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14380 continue;
14381
14382 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14383 return -1;
18cd5bce
AM
14384
14385 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14386 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14387 bfd_elf_reloc_symbol_deleted_p,
14388 &cookie))
d7153c4a
AM
14389 {
14390 eh_changed = 1;
14391 if (i->size != i->rawsize)
14392 changed = 1;
14393 }
18cd5bce
AM
14394
14395 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14396 }
9866ffe2 14397
66631823
CE
14398 eh_alignment = ((1 << o->alignment_power)
14399 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14400 /* Skip over zero terminator, and prevent empty sections from
14401 adding alignment padding at the end. */
14402 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14403 if (i->size == 0)
14404 i->flags |= SEC_EXCLUDE;
14405 else if (i->size > 4)
14406 break;
14407 /* The last non-empty eh_frame section doesn't need padding. */
14408 if (i != NULL)
14409 i = i->map_tail.s;
14410 /* Any prior sections must pad the last FDE out to the output
14411 section alignment. Otherwise we might have zero padding
14412 between sections, which would be seen as a terminator. */
14413 for (; i != NULL; i = i->map_tail.s)
14414 if (i->size == 4)
14415 /* All but the last zero terminator should have been removed. */
14416 BFD_FAIL ();
14417 else
14418 {
14419 bfd_size_type size
14420 = (i->size + eh_alignment - 1) & -eh_alignment;
14421 if (i->size != size)
af471f82 14422 {
9866ffe2
AM
14423 i->size = size;
14424 changed = 1;
14425 eh_changed = 1;
af471f82 14426 }
9866ffe2 14427 }
d7153c4a
AM
14428 if (eh_changed)
14429 elf_link_hash_traverse (elf_hash_table (info),
14430 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14431 }
c152c796 14432
18cd5bce
AM
14433 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14434 {
14435 const struct elf_backend_data *bed;
57963c05 14436 asection *s;
c152c796 14437
18cd5bce
AM
14438 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14439 continue;
57963c05
AM
14440 s = abfd->sections;
14441 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14442 continue;
18cd5bce
AM
14443
14444 bed = get_elf_backend_data (abfd);
14445
14446 if (bed->elf_backend_discard_info != NULL)
14447 {
14448 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14449 return -1;
18cd5bce
AM
14450
14451 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14452 changed = 1;
18cd5bce
AM
14453
14454 fini_reloc_cookie (&cookie, abfd);
14455 }
c152c796
AM
14456 }
14457
2f0c68f2
CM
14458 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14459 _bfd_elf_end_eh_frame_parsing (info);
14460
14461 if (info->eh_frame_hdr_type
0e1862bb 14462 && !bfd_link_relocatable (info)
c152c796 14463 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14464 changed = 1;
c152c796 14465
75938853 14466 return changed;
c152c796 14467}
082b7297 14468
43e1669b 14469bfd_boolean
0c511000 14470_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14471 asection *sec,
c0f00686 14472 struct bfd_link_info *info)
082b7297
L
14473{
14474 flagword flags;
c77ec726 14475 const char *name, *key;
082b7297
L
14476 struct bfd_section_already_linked *l;
14477 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14478
c77ec726
AM
14479 if (sec->output_section == bfd_abs_section_ptr)
14480 return FALSE;
0c511000 14481
c77ec726 14482 flags = sec->flags;
0c511000 14483
c77ec726
AM
14484 /* Return if it isn't a linkonce section. A comdat group section
14485 also has SEC_LINK_ONCE set. */
14486 if ((flags & SEC_LINK_ONCE) == 0)
14487 return FALSE;
0c511000 14488
c77ec726
AM
14489 /* Don't put group member sections on our list of already linked
14490 sections. They are handled as a group via their group section. */
14491 if (elf_sec_group (sec) != NULL)
14492 return FALSE;
0c511000 14493
c77ec726
AM
14494 /* For a SHT_GROUP section, use the group signature as the key. */
14495 name = sec->name;
14496 if ((flags & SEC_GROUP) != 0
14497 && elf_next_in_group (sec) != NULL
14498 && elf_group_name (elf_next_in_group (sec)) != NULL)
14499 key = elf_group_name (elf_next_in_group (sec));
14500 else
14501 {
14502 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14503 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14504 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14505 key++;
0c511000 14506 else
c77ec726
AM
14507 /* Must be a user linkonce section that doesn't follow gcc's
14508 naming convention. In this case we won't be matching
14509 single member groups. */
14510 key = name;
0c511000 14511 }
6d2cd210 14512
c77ec726 14513 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14514
14515 for (l = already_linked_list->entry; l != NULL; l = l->next)
14516 {
c2370991 14517 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14518 sections with a signature of <key> (<key> is some string),
14519 and linkonce sections named .gnu.linkonce.<type>.<key>.
14520 Match like sections. LTO plugin sections are an exception.
14521 They are always named .gnu.linkonce.t.<key> and match either
14522 type of section. */
14523 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14524 && ((flags & SEC_GROUP) != 0
14525 || strcmp (name, l->sec->name) == 0))
14526 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14527 {
14528 /* The section has already been linked. See if we should
6d2cd210 14529 issue a warning. */
c77ec726
AM
14530 if (!_bfd_handle_already_linked (sec, l, info))
14531 return FALSE;
082b7297 14532
c77ec726 14533 if (flags & SEC_GROUP)
3d7f7666 14534 {
c77ec726
AM
14535 asection *first = elf_next_in_group (sec);
14536 asection *s = first;
3d7f7666 14537
c77ec726 14538 while (s != NULL)
3d7f7666 14539 {
c77ec726
AM
14540 s->output_section = bfd_abs_section_ptr;
14541 /* Record which group discards it. */
14542 s->kept_section = l->sec;
14543 s = elf_next_in_group (s);
14544 /* These lists are circular. */
14545 if (s == first)
14546 break;
3d7f7666
L
14547 }
14548 }
082b7297 14549
43e1669b 14550 return TRUE;
082b7297
L
14551 }
14552 }
14553
c77ec726
AM
14554 /* A single member comdat group section may be discarded by a
14555 linkonce section and vice versa. */
14556 if ((flags & SEC_GROUP) != 0)
3d7f7666 14557 {
c77ec726 14558 asection *first = elf_next_in_group (sec);
c2370991 14559
c77ec726
AM
14560 if (first != NULL && elf_next_in_group (first) == first)
14561 /* Check this single member group against linkonce sections. */
14562 for (l = already_linked_list->entry; l != NULL; l = l->next)
14563 if ((l->sec->flags & SEC_GROUP) == 0
14564 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14565 {
14566 first->output_section = bfd_abs_section_ptr;
14567 first->kept_section = l->sec;
14568 sec->output_section = bfd_abs_section_ptr;
14569 break;
14570 }
14571 }
14572 else
14573 /* Check this linkonce section against single member groups. */
14574 for (l = already_linked_list->entry; l != NULL; l = l->next)
14575 if (l->sec->flags & SEC_GROUP)
6d2cd210 14576 {
c77ec726 14577 asection *first = elf_next_in_group (l->sec);
6d2cd210 14578
c77ec726
AM
14579 if (first != NULL
14580 && elf_next_in_group (first) == first
14581 && bfd_elf_match_symbols_in_sections (first, sec, info))
14582 {
14583 sec->output_section = bfd_abs_section_ptr;
14584 sec->kept_section = first;
14585 break;
14586 }
6d2cd210 14587 }
0c511000 14588
c77ec726
AM
14589 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14590 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14591 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14592 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14593 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14594 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14595 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14596 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14597 The reverse order cannot happen as there is never a bfd with only the
14598 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14599 matter as here were are looking only for cross-bfd sections. */
14600
14601 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14602 for (l = already_linked_list->entry; l != NULL; l = l->next)
14603 if ((l->sec->flags & SEC_GROUP) == 0
14604 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14605 {
14606 if (abfd != l->sec->owner)
14607 sec->output_section = bfd_abs_section_ptr;
14608 break;
14609 }
80c29487 14610
082b7297 14611 /* This is the first section with this name. Record it. */
c77ec726 14612 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14613 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14614 return sec->output_section == bfd_abs_section_ptr;
082b7297 14615}
81e1b023 14616
a4d8e49b
L
14617bfd_boolean
14618_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14619{
14620 return sym->st_shndx == SHN_COMMON;
14621}
14622
14623unsigned int
14624_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14625{
14626 return SHN_COMMON;
14627}
14628
14629asection *
14630_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14631{
14632 return bfd_com_section_ptr;
14633}
10455f89
HPN
14634
14635bfd_vma
14636_bfd_elf_default_got_elt_size (bfd *abfd,
14637 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14638 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14639 bfd *ibfd ATTRIBUTE_UNUSED,
14640 unsigned long symndx ATTRIBUTE_UNUSED)
14641{
14642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14643 return bed->s->arch_size / 8;
14644}
83bac4b0
NC
14645
14646/* Routines to support the creation of dynamic relocs. */
14647
83bac4b0
NC
14648/* Returns the name of the dynamic reloc section associated with SEC. */
14649
14650static const char *
14651get_dynamic_reloc_section_name (bfd * abfd,
14652 asection * sec,
14653 bfd_boolean is_rela)
14654{
ddcf1fcf 14655 char *name;
fd361982 14656 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14657 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14658
ddcf1fcf 14659 if (old_name == NULL)
83bac4b0
NC
14660 return NULL;
14661
ddcf1fcf 14662 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14663 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14664
14665 return name;
14666}
14667
14668/* Returns the dynamic reloc section associated with SEC.
14669 If necessary compute the name of the dynamic reloc section based
14670 on SEC's name (looked up in ABFD's string table) and the setting
14671 of IS_RELA. */
14672
14673asection *
14674_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14675 asection * sec,
14676 bfd_boolean is_rela)
14677{
14678 asection * reloc_sec = elf_section_data (sec)->sreloc;
14679
14680 if (reloc_sec == NULL)
14681 {
14682 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14683
14684 if (name != NULL)
14685 {
3d4d4302 14686 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14687
14688 if (reloc_sec != NULL)
14689 elf_section_data (sec)->sreloc = reloc_sec;
14690 }
14691 }
14692
14693 return reloc_sec;
14694}
14695
14696/* Returns the dynamic reloc section associated with SEC. If the
14697 section does not exist it is created and attached to the DYNOBJ
14698 bfd and stored in the SRELOC field of SEC's elf_section_data
14699 structure.
f8076f98 14700
83bac4b0
NC
14701 ALIGNMENT is the alignment for the newly created section and
14702 IS_RELA defines whether the name should be .rela.<SEC's name>
14703 or .rel.<SEC's name>. The section name is looked up in the
14704 string table associated with ABFD. */
14705
14706asection *
ca4be51c
AM
14707_bfd_elf_make_dynamic_reloc_section (asection *sec,
14708 bfd *dynobj,
14709 unsigned int alignment,
14710 bfd *abfd,
14711 bfd_boolean is_rela)
83bac4b0
NC
14712{
14713 asection * reloc_sec = elf_section_data (sec)->sreloc;
14714
14715 if (reloc_sec == NULL)
14716 {
14717 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14718
14719 if (name == NULL)
14720 return NULL;
14721
3d4d4302 14722 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14723
14724 if (reloc_sec == NULL)
14725 {
3d4d4302
AM
14726 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14727 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14728 if ((sec->flags & SEC_ALLOC) != 0)
14729 flags |= SEC_ALLOC | SEC_LOAD;
14730
3d4d4302 14731 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14732 if (reloc_sec != NULL)
14733 {
8877b5e5
AM
14734 /* _bfd_elf_get_sec_type_attr chooses a section type by
14735 name. Override as it may be wrong, eg. for a user
14736 section named "auto" we'll get ".relauto" which is
14737 seen to be a .rela section. */
14738 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14739 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14740 reloc_sec = NULL;
14741 }
14742 }
14743
14744 elf_section_data (sec)->sreloc = reloc_sec;
14745 }
14746
14747 return reloc_sec;
14748}
1338dd10 14749
bffebb6b
AM
14750/* Copy the ELF symbol type and other attributes for a linker script
14751 assignment from HSRC to HDEST. Generally this should be treated as
14752 if we found a strong non-dynamic definition for HDEST (except that
14753 ld ignores multiple definition errors). */
1338dd10 14754void
bffebb6b
AM
14755_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14756 struct bfd_link_hash_entry *hdest,
14757 struct bfd_link_hash_entry *hsrc)
1338dd10 14758{
bffebb6b
AM
14759 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14760 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14761 Elf_Internal_Sym isym;
1338dd10
PB
14762
14763 ehdest->type = ehsrc->type;
35fc36a8 14764 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14765
14766 isym.st_other = ehsrc->other;
b8417128 14767 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14768}
351f65ca
L
14769
14770/* Append a RELA relocation REL to section S in BFD. */
14771
14772void
14773elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14774{
14775 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14776 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14777 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14778 bed->s->swap_reloca_out (abfd, rel, loc);
14779}
14780
14781/* Append a REL relocation REL to section S in BFD. */
14782
14783void
14784elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14785{
14786 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14787 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14788 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14789 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14790}
7dba9362
AM
14791
14792/* Define __start, __stop, .startof. or .sizeof. symbol. */
14793
14794struct bfd_link_hash_entry *
14795bfd_elf_define_start_stop (struct bfd_link_info *info,
14796 const char *symbol, asection *sec)
14797{
487b6440 14798 struct elf_link_hash_entry *h;
7dba9362 14799
487b6440
AM
14800 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14801 FALSE, FALSE, TRUE);
14802 if (h != NULL
14803 && (h->root.type == bfd_link_hash_undefined
14804 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14805 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14806 {
bf3077a6 14807 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14808 h->root.type = bfd_link_hash_defined;
14809 h->root.u.def.section = sec;
14810 h->root.u.def.value = 0;
14811 h->def_regular = 1;
14812 h->def_dynamic = 0;
14813 h->start_stop = 1;
14814 h->u2.start_stop_section = sec;
14815 if (symbol[0] == '.')
14816 {
14817 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14818 const struct elf_backend_data *bed;
14819 bed = get_elf_backend_data (info->output_bfd);
14820 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14821 }
36b8fda5
AM
14822 else
14823 {
14824 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14825 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14826 if (was_dynamic)
36b8fda5
AM
14827 bfd_elf_link_record_dynamic_symbol (info, h);
14828 }
487b6440 14829 return &h->root;
7dba9362 14830 }
487b6440 14831 return NULL;
7dba9362 14832}
This page took 3.880275 seconds and 4 git commands to generate.