Remove SH-5 remnants
[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
e310298c 3504/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3505 1 if a DT_NEEDED tag already exists, and 0 on success. */
3506
e310298c
AM
3507int
3508bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3509{
3510 struct elf_link_hash_table *hash_table;
ef53be89 3511 size_t strindex;
e310298c 3512 const char *soname;
5a580b3a 3513
7e9f0867
AM
3514 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3515 return -1;
3516
5a580b3a 3517 hash_table = elf_hash_table (info);
e310298c 3518 soname = elf_dt_name (abfd);
5a580b3a 3519 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3520 if (strindex == (size_t) -1)
5a580b3a
AM
3521 return -1;
3522
02be4619 3523 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3524 {
3525 asection *sdyn;
3526 const struct elf_backend_data *bed;
3527 bfd_byte *extdyn;
3528
3529 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3530 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3531 if (sdyn != NULL)
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3535 {
3536 Elf_Internal_Dyn dyn;
5a580b3a 3537
7e9f0867
AM
3538 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3539 if (dyn.d_tag == DT_NEEDED
3540 && dyn.d_un.d_val == strindex)
3541 {
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3543 return 1;
3544 }
3545 }
5a580b3a
AM
3546 }
3547
e310298c
AM
3548 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3549 return -1;
7e9f0867 3550
e310298c
AM
3551 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3552 return -1;
5a580b3a
AM
3553
3554 return 0;
3555}
3556
7b15fa7a
AM
3557/* Return true if SONAME is on the needed list between NEEDED and STOP
3558 (or the end of list if STOP is NULL), and needed by a library that
3559 will be loaded. */
3560
010e5ae2 3561static bfd_boolean
7b15fa7a
AM
3562on_needed_list (const char *soname,
3563 struct bfd_link_needed_list *needed,
3564 struct bfd_link_needed_list *stop)
010e5ae2 3565{
7b15fa7a
AM
3566 struct bfd_link_needed_list *look;
3567 for (look = needed; look != stop; look = look->next)
3568 if (strcmp (soname, look->name) == 0
3569 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3570 /* If needed by a library that itself is not directly
3571 needed, recursively check whether that library is
3572 indirectly needed. Since we add DT_NEEDED entries to
3573 the end of the list, library dependencies appear after
3574 the library. Therefore search prior to the current
3575 LOOK, preventing possible infinite recursion. */
3576 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3577 return TRUE;
3578
3579 return FALSE;
3580}
3581
3a3f4bf7 3582/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3583static int
3584elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3585{
3586 const struct elf_link_hash_entry *h1;
3587 const struct elf_link_hash_entry *h2;
10b7e05b 3588 bfd_signed_vma vdiff;
3a3f4bf7
AM
3589 int sdiff;
3590 const char *n1;
3591 const char *n2;
5a580b3a
AM
3592
3593 h1 = *(const struct elf_link_hash_entry **) arg1;
3594 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3595 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3596 if (vdiff != 0)
3597 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3598
3599 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3600 if (sdiff != 0)
3601 return sdiff;
3602
3603 /* Sort so that sized symbols are selected over zero size symbols. */
3604 vdiff = h1->size - h2->size;
3605 if (vdiff != 0)
3606 return vdiff > 0 ? 1 : -1;
3607
3608 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3609 if (h1->type != h2->type)
3610 return h1->type - h2->type;
3611
3612 /* If symbols are properly sized and typed, and multiple strong
3613 aliases are not defined in a shared library by the user we
3614 shouldn't get here. Unfortunately linker script symbols like
3615 __bss_start sometimes match a user symbol defined at the start of
3616 .bss without proper size and type. We'd like to preference the
3617 user symbol over reserved system symbols. Sort on leading
3618 underscores. */
3619 n1 = h1->root.root.string;
3620 n2 = h2->root.root.string;
3621 while (*n1 == *n2)
10b7e05b 3622 {
3a3f4bf7
AM
3623 if (*n1 == 0)
3624 break;
3625 ++n1;
3626 ++n2;
10b7e05b 3627 }
3a3f4bf7
AM
3628 if (*n1 == '_')
3629 return -1;
3630 if (*n2 == '_')
3631 return 1;
3632
3633 /* Final sort on name selects user symbols like '_u' over reserved
3634 system symbols like '_Z' and also will avoid qsort instability. */
3635 return *n1 - *n2;
5a580b3a 3636}
4ad4eba5 3637
5a580b3a
AM
3638/* This function is used to adjust offsets into .dynstr for
3639 dynamic symbols. This is called via elf_link_hash_traverse. */
3640
3641static bfd_boolean
3642elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3643{
a50b1753 3644 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3645
5a580b3a
AM
3646 if (h->dynindx != -1)
3647 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3648 return TRUE;
3649}
3650
3651/* Assign string offsets in .dynstr, update all structures referencing
3652 them. */
3653
4ad4eba5
AM
3654static bfd_boolean
3655elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3656{
3657 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3658 struct elf_link_local_dynamic_entry *entry;
3659 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3660 bfd *dynobj = hash_table->dynobj;
3661 asection *sdyn;
3662 bfd_size_type size;
3663 const struct elf_backend_data *bed;
3664 bfd_byte *extdyn;
3665
3666 _bfd_elf_strtab_finalize (dynstr);
3667 size = _bfd_elf_strtab_size (dynstr);
3668
3669 bed = get_elf_backend_data (dynobj);
3d4d4302 3670 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3671 BFD_ASSERT (sdyn != NULL);
3672
3673 /* Update all .dynamic entries referencing .dynstr strings. */
3674 for (extdyn = sdyn->contents;
eea6121a 3675 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3676 extdyn += bed->s->sizeof_dyn)
3677 {
3678 Elf_Internal_Dyn dyn;
3679
3680 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3681 switch (dyn.d_tag)
3682 {
3683 case DT_STRSZ:
3684 dyn.d_un.d_val = size;
3685 break;
3686 case DT_NEEDED:
3687 case DT_SONAME:
3688 case DT_RPATH:
3689 case DT_RUNPATH:
3690 case DT_FILTER:
3691 case DT_AUXILIARY:
7ee314fa
AM
3692 case DT_AUDIT:
3693 case DT_DEPAUDIT:
5a580b3a
AM
3694 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3695 break;
3696 default:
3697 continue;
3698 }
3699 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3700 }
3701
3702 /* Now update local dynamic symbols. */
3703 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3704 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3705 entry->isym.st_name);
3706
3707 /* And the rest of dynamic symbols. */
3708 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3709
3710 /* Adjust version definitions. */
3711 if (elf_tdata (output_bfd)->cverdefs)
3712 {
3713 asection *s;
3714 bfd_byte *p;
ef53be89 3715 size_t i;
5a580b3a
AM
3716 Elf_Internal_Verdef def;
3717 Elf_Internal_Verdaux defaux;
3718
3d4d4302 3719 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3720 p = s->contents;
3721 do
3722 {
3723 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3724 &def);
3725 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3726 if (def.vd_aux != sizeof (Elf_External_Verdef))
3727 continue;
5a580b3a
AM
3728 for (i = 0; i < def.vd_cnt; ++i)
3729 {
3730 _bfd_elf_swap_verdaux_in (output_bfd,
3731 (Elf_External_Verdaux *) p, &defaux);
3732 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3733 defaux.vda_name);
3734 _bfd_elf_swap_verdaux_out (output_bfd,
3735 &defaux, (Elf_External_Verdaux *) p);
3736 p += sizeof (Elf_External_Verdaux);
3737 }
3738 }
3739 while (def.vd_next);
3740 }
3741
3742 /* Adjust version references. */
3743 if (elf_tdata (output_bfd)->verref)
3744 {
3745 asection *s;
3746 bfd_byte *p;
ef53be89 3747 size_t i;
5a580b3a
AM
3748 Elf_Internal_Verneed need;
3749 Elf_Internal_Vernaux needaux;
3750
3d4d4302 3751 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3752 p = s->contents;
3753 do
3754 {
3755 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3756 &need);
3757 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3758 _bfd_elf_swap_verneed_out (output_bfd, &need,
3759 (Elf_External_Verneed *) p);
3760 p += sizeof (Elf_External_Verneed);
3761 for (i = 0; i < need.vn_cnt; ++i)
3762 {
3763 _bfd_elf_swap_vernaux_in (output_bfd,
3764 (Elf_External_Vernaux *) p, &needaux);
3765 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3766 needaux.vna_name);
3767 _bfd_elf_swap_vernaux_out (output_bfd,
3768 &needaux,
3769 (Elf_External_Vernaux *) p);
3770 p += sizeof (Elf_External_Vernaux);
3771 }
3772 }
3773 while (need.vn_next);
3774 }
3775
3776 return TRUE;
3777}
3778\f
13285a1b
AM
3779/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3780 The default is to only match when the INPUT and OUTPUT are exactly
3781 the same target. */
3782
3783bfd_boolean
3784_bfd_elf_default_relocs_compatible (const bfd_target *input,
3785 const bfd_target *output)
3786{
3787 return input == output;
3788}
3789
3790/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3791 This version is used when different targets for the same architecture
3792 are virtually identical. */
3793
3794bfd_boolean
3795_bfd_elf_relocs_compatible (const bfd_target *input,
3796 const bfd_target *output)
3797{
3798 const struct elf_backend_data *obed, *ibed;
3799
3800 if (input == output)
3801 return TRUE;
3802
3803 ibed = xvec_get_elf_backend_data (input);
3804 obed = xvec_get_elf_backend_data (output);
3805
3806 if (ibed->arch != obed->arch)
3807 return FALSE;
3808
3809 /* If both backends are using this function, deem them compatible. */
3810 return ibed->relocs_compatible == obed->relocs_compatible;
3811}
3812
e5034e59
AM
3813/* Make a special call to the linker "notice" function to tell it that
3814 we are about to handle an as-needed lib, or have finished
1b786873 3815 processing the lib. */
e5034e59
AM
3816
3817bfd_boolean
3818_bfd_elf_notice_as_needed (bfd *ibfd,
3819 struct bfd_link_info *info,
3820 enum notice_asneeded_action act)
3821{
46135103 3822 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3823}
3824
d9689752
L
3825/* Check relocations an ELF object file. */
3826
3827bfd_boolean
3828_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3829{
3830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3831 struct elf_link_hash_table *htab = elf_hash_table (info);
3832
3833 /* If this object is the same format as the output object, and it is
3834 not a shared library, then let the backend look through the
3835 relocs.
3836
3837 This is required to build global offset table entries and to
3838 arrange for dynamic relocs. It is not required for the
3839 particular common case of linking non PIC code, even when linking
3840 against shared libraries, but unfortunately there is no way of
3841 knowing whether an object file has been compiled PIC or not.
3842 Looking through the relocs is not particularly time consuming.
3843 The problem is that we must either (1) keep the relocs in memory,
3844 which causes the linker to require additional runtime memory or
3845 (2) read the relocs twice from the input file, which wastes time.
3846 This would be a good case for using mmap.
3847
3848 I have no idea how to handle linking PIC code into a file of a
3849 different format. It probably can't be done. */
3850 if ((abfd->flags & DYNAMIC) == 0
3851 && is_elf_hash_table (htab)
3852 && bed->check_relocs != NULL
3853 && elf_object_id (abfd) == elf_hash_table_id (htab)
3854 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3855 {
3856 asection *o;
3857
3858 for (o = abfd->sections; o != NULL; o = o->next)
3859 {
3860 Elf_Internal_Rela *internal_relocs;
3861 bfd_boolean ok;
3862
5ce03cea 3863 /* Don't check relocations in excluded sections. */
d9689752 3864 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3865 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3866 || o->reloc_count == 0
3867 || ((info->strip == strip_all || info->strip == strip_debugger)
3868 && (o->flags & SEC_DEBUGGING) != 0)
3869 || bfd_is_abs_section (o->output_section))
3870 continue;
3871
3872 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3873 info->keep_memory);
3874 if (internal_relocs == NULL)
3875 return FALSE;
3876
3877 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3878
3879 if (elf_section_data (o)->relocs != internal_relocs)
3880 free (internal_relocs);
3881
3882 if (! ok)
3883 return FALSE;
3884 }
3885 }
3886
3887 return TRUE;
3888}
3889
4ad4eba5
AM
3890/* Add symbols from an ELF object file to the linker hash table. */
3891
3892static bfd_boolean
3893elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3894{
a0c402a5 3895 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3896 Elf_Internal_Shdr *hdr;
ef53be89
AM
3897 size_t symcount;
3898 size_t extsymcount;
3899 size_t extsymoff;
4ad4eba5
AM
3900 struct elf_link_hash_entry **sym_hash;
3901 bfd_boolean dynamic;
3902 Elf_External_Versym *extversym = NULL;
be22c732 3903 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3904 Elf_External_Versym *ever;
3905 struct elf_link_hash_entry *weaks;
3906 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3907 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3908 Elf_Internal_Sym *isymbuf = NULL;
3909 Elf_Internal_Sym *isym;
3910 Elf_Internal_Sym *isymend;
3911 const struct elf_backend_data *bed;
3912 bfd_boolean add_needed;
66eb6687 3913 struct elf_link_hash_table *htab;
66eb6687 3914 void *alloc_mark = NULL;
4f87808c
AM
3915 struct bfd_hash_entry **old_table = NULL;
3916 unsigned int old_size = 0;
3917 unsigned int old_count = 0;
66eb6687 3918 void *old_tab = NULL;
66eb6687
AM
3919 void *old_ent;
3920 struct bfd_link_hash_entry *old_undefs = NULL;
3921 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3922 void *old_strtab = NULL;
66eb6687 3923 size_t tabsize = 0;
db6a5d5f 3924 asection *s;
29a9f53e 3925 bfd_boolean just_syms;
4ad4eba5 3926
66eb6687 3927 htab = elf_hash_table (info);
4ad4eba5 3928 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3929
3930 if ((abfd->flags & DYNAMIC) == 0)
3931 dynamic = FALSE;
3932 else
3933 {
3934 dynamic = TRUE;
3935
3936 /* You can't use -r against a dynamic object. Also, there's no
3937 hope of using a dynamic object which does not exactly match
3938 the format of the output file. */
0e1862bb 3939 if (bfd_link_relocatable (info)
66eb6687 3940 || !is_elf_hash_table (htab)
f13a99db 3941 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3942 {
0e1862bb 3943 if (bfd_link_relocatable (info))
9a0789ec
NC
3944 bfd_set_error (bfd_error_invalid_operation);
3945 else
3946 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3947 goto error_return;
3948 }
3949 }
3950
a0c402a5
L
3951 ehdr = elf_elfheader (abfd);
3952 if (info->warn_alternate_em
3953 && bed->elf_machine_code != ehdr->e_machine
3954 && ((bed->elf_machine_alt1 != 0
3955 && ehdr->e_machine == bed->elf_machine_alt1)
3956 || (bed->elf_machine_alt2 != 0
3957 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3958 _bfd_error_handler
695344c0 3959 /* xgettext:c-format */
9793eb77 3960 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3961 ehdr->e_machine, abfd, bed->elf_machine_code);
3962
4ad4eba5
AM
3963 /* As a GNU extension, any input sections which are named
3964 .gnu.warning.SYMBOL are treated as warning symbols for the given
3965 symbol. This differs from .gnu.warning sections, which generate
3966 warnings when they are included in an output file. */
dd98f8d2 3967 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3968 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3969 {
db6a5d5f 3970 const char *name;
4ad4eba5 3971
fd361982 3972 name = bfd_section_name (s);
db6a5d5f 3973 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3974 {
db6a5d5f
AM
3975 char *msg;
3976 bfd_size_type sz;
3977
3978 name += sizeof ".gnu.warning." - 1;
3979
3980 /* If this is a shared object, then look up the symbol
3981 in the hash table. If it is there, and it is already
3982 been defined, then we will not be using the entry
3983 from this shared object, so we don't need to warn.
3984 FIXME: If we see the definition in a regular object
3985 later on, we will warn, but we shouldn't. The only
3986 fix is to keep track of what warnings we are supposed
3987 to emit, and then handle them all at the end of the
3988 link. */
3989 if (dynamic)
4ad4eba5 3990 {
db6a5d5f
AM
3991 struct elf_link_hash_entry *h;
3992
3993 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3994
3995 /* FIXME: What about bfd_link_hash_common? */
3996 if (h != NULL
3997 && (h->root.type == bfd_link_hash_defined
3998 || h->root.type == bfd_link_hash_defweak))
3999 continue;
4000 }
4ad4eba5 4001
db6a5d5f
AM
4002 sz = s->size;
4003 msg = (char *) bfd_alloc (abfd, sz + 1);
4004 if (msg == NULL)
4005 goto error_return;
4ad4eba5 4006
db6a5d5f
AM
4007 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4008 goto error_return;
4ad4eba5 4009
db6a5d5f 4010 msg[sz] = '\0';
4ad4eba5 4011
db6a5d5f
AM
4012 if (! (_bfd_generic_link_add_one_symbol
4013 (info, abfd, name, BSF_WARNING, s, 0, msg,
4014 FALSE, bed->collect, NULL)))
4015 goto error_return;
4ad4eba5 4016
0e1862bb 4017 if (bfd_link_executable (info))
db6a5d5f
AM
4018 {
4019 /* Clobber the section size so that the warning does
4020 not get copied into the output file. */
4021 s->size = 0;
11d2f718 4022
db6a5d5f
AM
4023 /* Also set SEC_EXCLUDE, so that symbols defined in
4024 the warning section don't get copied to the output. */
4025 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4026 }
4027 }
4028 }
4029
29a9f53e
L
4030 just_syms = ((s = abfd->sections) != NULL
4031 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4032
4ad4eba5
AM
4033 add_needed = TRUE;
4034 if (! dynamic)
4035 {
4036 /* If we are creating a shared library, create all the dynamic
4037 sections immediately. We need to attach them to something,
4038 so we attach them to this BFD, provided it is the right
bf89386a
L
4039 format and is not from ld --just-symbols. Always create the
4040 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4041 are no input BFD's of the same format as the output, we can't
4042 make a shared library. */
4043 if (!just_syms
bf89386a 4044 && (bfd_link_pic (info)
9c1d7a08 4045 || (!bfd_link_relocatable (info)
3c5fce9b 4046 && info->nointerp
9c1d7a08 4047 && (info->export_dynamic || info->dynamic)))
66eb6687 4048 && is_elf_hash_table (htab)
f13a99db 4049 && info->output_bfd->xvec == abfd->xvec
66eb6687 4050 && !htab->dynamic_sections_created)
4ad4eba5
AM
4051 {
4052 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4053 goto error_return;
4054 }
4055 }
66eb6687 4056 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4057 goto error_return;
4058 else
4059 {
4ad4eba5 4060 const char *soname = NULL;
7ee314fa 4061 char *audit = NULL;
4ad4eba5 4062 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4063 const Elf_Internal_Phdr *phdr;
e310298c 4064 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4065
4066 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4067 ld shouldn't allow it. */
29a9f53e 4068 if (just_syms)
92fd189d 4069 abort ();
4ad4eba5
AM
4070
4071 /* If this dynamic lib was specified on the command line with
4072 --as-needed in effect, then we don't want to add a DT_NEEDED
4073 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4074 in by another lib's DT_NEEDED. When --no-add-needed is used
4075 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4076 any dynamic library in DT_NEEDED tags in the dynamic lib at
4077 all. */
4078 add_needed = (elf_dyn_lib_class (abfd)
4079 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4080 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4081
4082 s = bfd_get_section_by_name (abfd, ".dynamic");
4083 if (s != NULL)
4084 {
4085 bfd_byte *dynbuf;
4086 bfd_byte *extdyn;
cb33740c 4087 unsigned int elfsec;
4ad4eba5
AM
4088 unsigned long shlink;
4089
eea6121a 4090 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4091 {
dc1e8a47 4092 error_free_dyn:
f8703194
L
4093 free (dynbuf);
4094 goto error_return;
4095 }
4ad4eba5
AM
4096
4097 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4098 if (elfsec == SHN_BAD)
4ad4eba5
AM
4099 goto error_free_dyn;
4100 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4101
4102 for (extdyn = dynbuf;
9bff840e 4103 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4104 extdyn += bed->s->sizeof_dyn)
4105 {
4106 Elf_Internal_Dyn dyn;
4107
4108 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4109 if (dyn.d_tag == DT_SONAME)
4110 {
4111 unsigned int tagv = dyn.d_un.d_val;
4112 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4113 if (soname == NULL)
4114 goto error_free_dyn;
4115 }
4116 if (dyn.d_tag == DT_NEEDED)
4117 {
4118 struct bfd_link_needed_list *n, **pn;
4119 char *fnm, *anm;
4120 unsigned int tagv = dyn.d_un.d_val;
986f0783 4121 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4122
a50b1753 4123 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4124 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4125 if (n == NULL || fnm == NULL)
4126 goto error_free_dyn;
4127 amt = strlen (fnm) + 1;
a50b1753 4128 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4129 if (anm == NULL)
4130 goto error_free_dyn;
4131 memcpy (anm, fnm, amt);
4132 n->name = anm;
4133 n->by = abfd;
4134 n->next = NULL;
66eb6687 4135 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4136 ;
4137 *pn = n;
4138 }
4139 if (dyn.d_tag == DT_RUNPATH)
4140 {
4141 struct bfd_link_needed_list *n, **pn;
4142 char *fnm, *anm;
4143 unsigned int tagv = dyn.d_un.d_val;
986f0783 4144 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4145
a50b1753 4146 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4147 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4148 if (n == NULL || fnm == NULL)
4149 goto error_free_dyn;
4150 amt = strlen (fnm) + 1;
a50b1753 4151 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4152 if (anm == NULL)
4153 goto error_free_dyn;
4154 memcpy (anm, fnm, amt);
4155 n->name = anm;
4156 n->by = abfd;
4157 n->next = NULL;
4158 for (pn = & runpath;
4159 *pn != NULL;
4160 pn = &(*pn)->next)
4161 ;
4162 *pn = n;
4163 }
4164 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4165 if (!runpath && dyn.d_tag == DT_RPATH)
4166 {
4167 struct bfd_link_needed_list *n, **pn;
4168 char *fnm, *anm;
4169 unsigned int tagv = dyn.d_un.d_val;
986f0783 4170 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4171
a50b1753 4172 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4173 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4174 if (n == NULL || fnm == NULL)
4175 goto error_free_dyn;
4176 amt = strlen (fnm) + 1;
a50b1753 4177 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4178 if (anm == NULL)
f8703194 4179 goto error_free_dyn;
4ad4eba5
AM
4180 memcpy (anm, fnm, amt);
4181 n->name = anm;
4182 n->by = abfd;
4183 n->next = NULL;
4184 for (pn = & rpath;
4185 *pn != NULL;
4186 pn = &(*pn)->next)
4187 ;
4188 *pn = n;
4189 }
7ee314fa
AM
4190 if (dyn.d_tag == DT_AUDIT)
4191 {
4192 unsigned int tagv = dyn.d_un.d_val;
4193 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4194 }
4ad4eba5
AM
4195 }
4196
4197 free (dynbuf);
4198 }
4199
4200 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4201 frees all more recently bfd_alloc'd blocks as well. */
4202 if (runpath)
4203 rpath = runpath;
4204
4205 if (rpath)
4206 {
4207 struct bfd_link_needed_list **pn;
66eb6687 4208 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4209 ;
4210 *pn = rpath;
4211 }
4212
9acc85a6
AM
4213 /* If we have a PT_GNU_RELRO program header, mark as read-only
4214 all sections contained fully therein. This makes relro
4215 shared library sections appear as they will at run-time. */
4216 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4217 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4218 if (phdr->p_type == PT_GNU_RELRO)
4219 {
4220 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4221 {
4222 unsigned int opb = bfd_octets_per_byte (abfd, s);
4223
4224 if ((s->flags & SEC_ALLOC) != 0
4225 && s->vma * opb >= phdr->p_vaddr
4226 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4227 s->flags |= SEC_READONLY;
4228 }
9acc85a6
AM
4229 break;
4230 }
4231
4ad4eba5
AM
4232 /* We do not want to include any of the sections in a dynamic
4233 object in the output file. We hack by simply clobbering the
4234 list of sections in the BFD. This could be handled more
4235 cleanly by, say, a new section flag; the existing
4236 SEC_NEVER_LOAD flag is not the one we want, because that one
4237 still implies that the section takes up space in the output
4238 file. */
4239 bfd_section_list_clear (abfd);
4240
4ad4eba5
AM
4241 /* Find the name to use in a DT_NEEDED entry that refers to this
4242 object. If the object has a DT_SONAME entry, we use it.
4243 Otherwise, if the generic linker stuck something in
4244 elf_dt_name, we use that. Otherwise, we just use the file
4245 name. */
4246 if (soname == NULL || *soname == '\0')
4247 {
4248 soname = elf_dt_name (abfd);
4249 if (soname == NULL || *soname == '\0')
4250 soname = bfd_get_filename (abfd);
4251 }
4252
4253 /* Save the SONAME because sometimes the linker emulation code
4254 will need to know it. */
4255 elf_dt_name (abfd) = soname;
4256
4ad4eba5
AM
4257 /* If we have already included this dynamic object in the
4258 link, just ignore it. There is no reason to include a
4259 particular dynamic object more than once. */
e310298c
AM
4260 for (loaded_lib = htab->dyn_loaded;
4261 loaded_lib != NULL;
4262 loaded_lib = loaded_lib->next)
4263 {
4264 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4265 return TRUE;
4266 }
4267
4268 /* Create dynamic sections for backends that require that be done
4269 before setup_gnu_properties. */
4270 if (add_needed
4271 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4272 return FALSE;
7ee314fa
AM
4273
4274 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4275 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4276 }
4277
4278 /* If this is a dynamic object, we always link against the .dynsym
4279 symbol table, not the .symtab symbol table. The dynamic linker
4280 will only see the .dynsym symbol table, so there is no reason to
4281 look at .symtab for a dynamic object. */
4282
4283 if (! dynamic || elf_dynsymtab (abfd) == 0)
4284 hdr = &elf_tdata (abfd)->symtab_hdr;
4285 else
4286 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4287
4288 symcount = hdr->sh_size / bed->s->sizeof_sym;
4289
4290 /* The sh_info field of the symtab header tells us where the
4291 external symbols start. We don't care about the local symbols at
4292 this point. */
4293 if (elf_bad_symtab (abfd))
4294 {
4295 extsymcount = symcount;
4296 extsymoff = 0;
4297 }
4298 else
4299 {
4300 extsymcount = symcount - hdr->sh_info;
4301 extsymoff = hdr->sh_info;
4302 }
4303
f45794cb 4304 sym_hash = elf_sym_hashes (abfd);
012b2306 4305 if (extsymcount != 0)
4ad4eba5
AM
4306 {
4307 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4308 NULL, NULL, NULL);
4309 if (isymbuf == NULL)
4310 goto error_return;
4311
4ad4eba5 4312 if (sym_hash == NULL)
012b2306
AM
4313 {
4314 /* We store a pointer to the hash table entry for each
4315 external symbol. */
986f0783 4316 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4317 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4318 if (sym_hash == NULL)
4319 goto error_free_sym;
4320 elf_sym_hashes (abfd) = sym_hash;
4321 }
4ad4eba5
AM
4322 }
4323
4324 if (dynamic)
4325 {
4326 /* Read in any version definitions. */
fc0e6df6
PB
4327 if (!_bfd_elf_slurp_version_tables (abfd,
4328 info->default_imported_symver))
4ad4eba5
AM
4329 goto error_free_sym;
4330
4331 /* Read in the symbol versions, but don't bother to convert them
4332 to internal format. */
4333 if (elf_dynversym (abfd) != 0)
4334 {
986f0783
AM
4335 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4336 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4337
2bb3687b
AM
4338 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4339 goto error_free_sym;
4340 extversym = (Elf_External_Versym *)
4341 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4342 if (extversym == NULL)
4343 goto error_free_sym;
986f0783 4344 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4345 }
4346 }
4347
66eb6687
AM
4348 /* If we are loading an as-needed shared lib, save the symbol table
4349 state before we start adding symbols. If the lib turns out
4350 to be unneeded, restore the state. */
4351 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4352 {
4353 unsigned int i;
4354 size_t entsize;
4355
4356 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4357 {
4358 struct bfd_hash_entry *p;
2de92251 4359 struct elf_link_hash_entry *h;
66eb6687
AM
4360
4361 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4362 {
4363 h = (struct elf_link_hash_entry *) p;
4364 entsize += htab->root.table.entsize;
4365 if (h->root.type == bfd_link_hash_warning)
4366 entsize += htab->root.table.entsize;
4367 }
66eb6687
AM
4368 }
4369
4370 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4371 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4372 if (old_tab == NULL)
4373 goto error_free_vers;
4374
4375 /* Remember the current objalloc pointer, so that all mem for
4376 symbols added can later be reclaimed. */
4377 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4378 if (alloc_mark == NULL)
4379 goto error_free_vers;
4380
5061a885
AM
4381 /* Make a special call to the linker "notice" function to
4382 tell it that we are about to handle an as-needed lib. */
e5034e59 4383 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4384 goto error_free_vers;
5061a885 4385
f45794cb
AM
4386 /* Clone the symbol table. Remember some pointers into the
4387 symbol table, and dynamic symbol count. */
4388 old_ent = (char *) old_tab + tabsize;
66eb6687 4389 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4390 old_undefs = htab->root.undefs;
4391 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4392 old_table = htab->root.table.table;
4393 old_size = htab->root.table.size;
4394 old_count = htab->root.table.count;
e310298c
AM
4395 old_strtab = NULL;
4396 if (htab->dynstr != NULL)
4397 {
4398 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4399 if (old_strtab == NULL)
4400 goto error_free_vers;
4401 }
66eb6687
AM
4402
4403 for (i = 0; i < htab->root.table.size; i++)
4404 {
4405 struct bfd_hash_entry *p;
2de92251 4406 struct elf_link_hash_entry *h;
66eb6687
AM
4407
4408 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4409 {
4410 memcpy (old_ent, p, htab->root.table.entsize);
4411 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4412 h = (struct elf_link_hash_entry *) p;
4413 if (h->root.type == bfd_link_hash_warning)
4414 {
4415 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4416 old_ent = (char *) old_ent + htab->root.table.entsize;
4417 }
66eb6687
AM
4418 }
4419 }
4420 }
4ad4eba5 4421
66eb6687 4422 weaks = NULL;
be22c732
NC
4423 if (extversym == NULL)
4424 ever = NULL;
4425 else if (extversym + extsymoff < extversym_end)
4426 ever = extversym + extsymoff;
4427 else
4428 {
4429 /* xgettext:c-format */
4430 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4431 abfd, (long) extsymoff,
4432 (long) (extversym_end - extversym) / sizeof (* extversym));
4433 bfd_set_error (bfd_error_bad_value);
4434 goto error_free_vers;
4435 }
4436
b4c555cf
ML
4437 if (!bfd_link_relocatable (info)
4438 && abfd->lto_slim_object)
cc5277b1
ML
4439 {
4440 _bfd_error_handler
4441 (_("%pB: plugin needed to handle lto object"), abfd);
4442 }
4443
4ad4eba5
AM
4444 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4445 isym < isymend;
4446 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4447 {
4448 int bind;
4449 bfd_vma value;
af44c138 4450 asection *sec, *new_sec;
4ad4eba5
AM
4451 flagword flags;
4452 const char *name;
4453 struct elf_link_hash_entry *h;
90c984fc 4454 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4455 bfd_boolean definition;
4456 bfd_boolean size_change_ok;
4457 bfd_boolean type_change_ok;
37a9e49a
L
4458 bfd_boolean new_weak;
4459 bfd_boolean old_weak;
4ad4eba5 4460 bfd_boolean override;
a4d8e49b 4461 bfd_boolean common;
97196564 4462 bfd_boolean discarded;
4ad4eba5 4463 unsigned int old_alignment;
4538d1c7 4464 unsigned int shindex;
4ad4eba5 4465 bfd *old_bfd;
6e33951e 4466 bfd_boolean matched;
4ad4eba5
AM
4467
4468 override = FALSE;
4469
4470 flags = BSF_NO_FLAGS;
4471 sec = NULL;
4472 value = isym->st_value;
a4d8e49b 4473 common = bed->common_definition (isym);
2980ccad
L
4474 if (common && info->inhibit_common_definition)
4475 {
4476 /* Treat common symbol as undefined for --no-define-common. */
4477 isym->st_shndx = SHN_UNDEF;
4478 common = FALSE;
4479 }
97196564 4480 discarded = FALSE;
4ad4eba5
AM
4481
4482 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4483 switch (bind)
4ad4eba5 4484 {
3e7a7d11 4485 case STB_LOCAL:
4ad4eba5
AM
4486 /* This should be impossible, since ELF requires that all
4487 global symbols follow all local symbols, and that sh_info
4488 point to the first global symbol. Unfortunately, Irix 5
4489 screws this up. */
fe3fef62
AM
4490 if (elf_bad_symtab (abfd))
4491 continue;
4492
4493 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4494 then we'll likely segfault on a NULL symbol hash if the
4495 symbol is ever referenced in relocations. */
4496 shindex = elf_elfheader (abfd)->e_shstrndx;
4497 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4498 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4499 " (>= sh_info of %lu)"),
4500 abfd, name, (long) (isym - isymbuf + extsymoff),
4501 (long) extsymoff);
4502
4503 /* Dynamic object relocations are not processed by ld, so
4504 ld won't run into the problem mentioned above. */
4505 if (dynamic)
4506 continue;
fe3fef62
AM
4507 bfd_set_error (bfd_error_bad_value);
4508 goto error_free_vers;
3e7a7d11
NC
4509
4510 case STB_GLOBAL:
a4d8e49b 4511 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4512 flags = BSF_GLOBAL;
3e7a7d11
NC
4513 break;
4514
4515 case STB_WEAK:
4516 flags = BSF_WEAK;
4517 break;
4518
4519 case STB_GNU_UNIQUE:
4520 flags = BSF_GNU_UNIQUE;
4521 break;
4522
4523 default:
4ad4eba5 4524 /* Leave it up to the processor backend. */
3e7a7d11 4525 break;
4ad4eba5
AM
4526 }
4527
4528 if (isym->st_shndx == SHN_UNDEF)
4529 sec = bfd_und_section_ptr;
cb33740c
AM
4530 else if (isym->st_shndx == SHN_ABS)
4531 sec = bfd_abs_section_ptr;
4532 else if (isym->st_shndx == SHN_COMMON)
4533 {
4534 sec = bfd_com_section_ptr;
4535 /* What ELF calls the size we call the value. What ELF
4536 calls the value we call the alignment. */
4537 value = isym->st_size;
4538 }
4539 else
4ad4eba5
AM
4540 {
4541 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4542 if (sec == NULL)
4543 sec = bfd_abs_section_ptr;
dbaa2011 4544 else if (discarded_section (sec))
529fcb95 4545 {
e5d08002
L
4546 /* Symbols from discarded section are undefined. We keep
4547 its visibility. */
529fcb95 4548 sec = bfd_und_section_ptr;
97196564 4549 discarded = TRUE;
529fcb95
PB
4550 isym->st_shndx = SHN_UNDEF;
4551 }
4ad4eba5
AM
4552 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4553 value -= sec->vma;
4554 }
4ad4eba5
AM
4555
4556 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4557 isym->st_name);
4558 if (name == NULL)
4559 goto error_free_vers;
4560
4561 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4562 && (abfd->flags & BFD_PLUGIN) != 0)
4563 {
4564 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4565
4566 if (xc == NULL)
4567 {
4568 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4569 | SEC_EXCLUDE);
4570 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4571 if (xc == NULL)
4572 goto error_free_vers;
4573 }
4574 sec = xc;
4575 }
4576 else if (isym->st_shndx == SHN_COMMON
4577 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4578 && !bfd_link_relocatable (info))
4ad4eba5
AM
4579 {
4580 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4581
4582 if (tcomm == NULL)
4583 {
02d00247
AM
4584 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4585 | SEC_LINKER_CREATED);
4586 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4587 if (tcomm == NULL)
4ad4eba5
AM
4588 goto error_free_vers;
4589 }
4590 sec = tcomm;
4591 }
66eb6687 4592 else if (bed->elf_add_symbol_hook)
4ad4eba5 4593 {
66eb6687
AM
4594 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4595 &sec, &value))
4ad4eba5
AM
4596 goto error_free_vers;
4597
4598 /* The hook function sets the name to NULL if this symbol
4599 should be skipped for some reason. */
4600 if (name == NULL)
4601 continue;
4602 }
4603
4604 /* Sanity check that all possibilities were handled. */
4605 if (sec == NULL)
4538d1c7 4606 abort ();
4ad4eba5 4607
191c0c42
AM
4608 /* Silently discard TLS symbols from --just-syms. There's
4609 no way to combine a static TLS block with a new TLS block
4610 for this executable. */
4611 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4612 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4613 continue;
4614
4ad4eba5
AM
4615 if (bfd_is_und_section (sec)
4616 || bfd_is_com_section (sec))
4617 definition = FALSE;
4618 else
4619 definition = TRUE;
4620
4621 size_change_ok = FALSE;
66eb6687 4622 type_change_ok = bed->type_change_ok;
37a9e49a 4623 old_weak = FALSE;
6e33951e 4624 matched = FALSE;
4ad4eba5
AM
4625 old_alignment = 0;
4626 old_bfd = NULL;
af44c138 4627 new_sec = sec;
4ad4eba5 4628
66eb6687 4629 if (is_elf_hash_table (htab))
4ad4eba5
AM
4630 {
4631 Elf_Internal_Versym iver;
4632 unsigned int vernum = 0;
4633 bfd_boolean skip;
4634
fc0e6df6 4635 if (ever == NULL)
4ad4eba5 4636 {
fc0e6df6
PB
4637 if (info->default_imported_symver)
4638 /* Use the default symbol version created earlier. */
4639 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4640 else
4641 iver.vs_vers = 0;
4642 }
be22c732
NC
4643 else if (ever >= extversym_end)
4644 {
4645 /* xgettext:c-format */
4646 _bfd_error_handler (_("%pB: not enough version information"),
4647 abfd);
4648 bfd_set_error (bfd_error_bad_value);
4649 goto error_free_vers;
4650 }
fc0e6df6
PB
4651 else
4652 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4653
4654 vernum = iver.vs_vers & VERSYM_VERSION;
4655
4656 /* If this is a hidden symbol, or if it is not version
4657 1, we append the version name to the symbol name.
cc86ff91
EB
4658 However, we do not modify a non-hidden absolute symbol
4659 if it is not a function, because it might be the version
4660 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4661 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4662 || (vernum > 1
4663 && (!bfd_is_abs_section (sec)
4664 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4665 {
4666 const char *verstr;
4667 size_t namelen, verlen, newlen;
4668 char *newname, *p;
4669
4670 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4671 {
fc0e6df6
PB
4672 if (vernum > elf_tdata (abfd)->cverdefs)
4673 verstr = NULL;
4674 else if (vernum > 1)
4675 verstr =
4676 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4677 else
4678 verstr = "";
4ad4eba5 4679
fc0e6df6 4680 if (verstr == NULL)
4ad4eba5 4681 {
4eca0228 4682 _bfd_error_handler
695344c0 4683 /* xgettext:c-format */
871b3ab2 4684 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4685 abfd, name, vernum,
4686 elf_tdata (abfd)->cverdefs);
4687 bfd_set_error (bfd_error_bad_value);
4688 goto error_free_vers;
4ad4eba5 4689 }
fc0e6df6
PB
4690 }
4691 else
4692 {
4693 /* We cannot simply test for the number of
4694 entries in the VERNEED section since the
4695 numbers for the needed versions do not start
4696 at 0. */
4697 Elf_Internal_Verneed *t;
4698
4699 verstr = NULL;
4700 for (t = elf_tdata (abfd)->verref;
4701 t != NULL;
4702 t = t->vn_nextref)
4ad4eba5 4703 {
fc0e6df6 4704 Elf_Internal_Vernaux *a;
4ad4eba5 4705
fc0e6df6
PB
4706 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4707 {
4708 if (a->vna_other == vernum)
4ad4eba5 4709 {
fc0e6df6
PB
4710 verstr = a->vna_nodename;
4711 break;
4ad4eba5 4712 }
4ad4eba5 4713 }
fc0e6df6
PB
4714 if (a != NULL)
4715 break;
4716 }
4717 if (verstr == NULL)
4718 {
4eca0228 4719 _bfd_error_handler
695344c0 4720 /* xgettext:c-format */
871b3ab2 4721 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4722 abfd, name, vernum);
4723 bfd_set_error (bfd_error_bad_value);
4724 goto error_free_vers;
4ad4eba5 4725 }
4ad4eba5 4726 }
fc0e6df6
PB
4727
4728 namelen = strlen (name);
4729 verlen = strlen (verstr);
4730 newlen = namelen + verlen + 2;
4731 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4732 && isym->st_shndx != SHN_UNDEF)
4733 ++newlen;
4734
a50b1753 4735 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4736 if (newname == NULL)
4737 goto error_free_vers;
4738 memcpy (newname, name, namelen);
4739 p = newname + namelen;
4740 *p++ = ELF_VER_CHR;
4741 /* If this is a defined non-hidden version symbol,
4742 we add another @ to the name. This indicates the
4743 default version of the symbol. */
4744 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4745 && isym->st_shndx != SHN_UNDEF)
4746 *p++ = ELF_VER_CHR;
4747 memcpy (p, verstr, verlen + 1);
4748
4749 name = newname;
4ad4eba5
AM
4750 }
4751
cd3416da
AM
4752 /* If this symbol has default visibility and the user has
4753 requested we not re-export it, then mark it as hidden. */
a0d49154 4754 if (!bfd_is_und_section (sec)
cd3416da 4755 && !dynamic
ce875075 4756 && abfd->no_export
cd3416da
AM
4757 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4758 isym->st_other = (STV_HIDDEN
4759 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4760
4f3fedcf
AM
4761 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4762 sym_hash, &old_bfd, &old_weak,
4763 &old_alignment, &skip, &override,
6e33951e
L
4764 &type_change_ok, &size_change_ok,
4765 &matched))
4ad4eba5
AM
4766 goto error_free_vers;
4767
4768 if (skip)
4769 continue;
4770
6e33951e
L
4771 /* Override a definition only if the new symbol matches the
4772 existing one. */
4773 if (override && matched)
4ad4eba5
AM
4774 definition = FALSE;
4775
4776 h = *sym_hash;
4777 while (h->root.type == bfd_link_hash_indirect
4778 || h->root.type == bfd_link_hash_warning)
4779 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4780
4ad4eba5 4781 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4782 && vernum > 1
4783 && definition)
4784 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4785 }
4786
4787 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4788 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4789 (struct bfd_link_hash_entry **) sym_hash)))
4790 goto error_free_vers;
4791
4792 h = *sym_hash;
90c984fc
L
4793 /* We need to make sure that indirect symbol dynamic flags are
4794 updated. */
4795 hi = h;
4ad4eba5
AM
4796 while (h->root.type == bfd_link_hash_indirect
4797 || h->root.type == bfd_link_hash_warning)
4798 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4799
97196564
L
4800 /* Setting the index to -3 tells elf_link_output_extsym that
4801 this symbol is defined in a discarded section. */
4802 if (discarded)
4803 h->indx = -3;
4804
4ad4eba5
AM
4805 *sym_hash = h;
4806
37a9e49a 4807 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4808 if (dynamic
4809 && definition
37a9e49a 4810 && new_weak
fcb93ecf 4811 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4812 && is_elf_hash_table (htab)
60d67dc8 4813 && h->u.alias == NULL)
4ad4eba5
AM
4814 {
4815 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4816 a dynamic object, using the alias field. Later in this
4817 function we will set the alias field to the correct
4ad4eba5
AM
4818 value. We only put non-function symbols from dynamic
4819 objects on this list, because that happens to be the only
4820 time we need to know the normal symbol corresponding to a
4821 weak symbol, and the information is time consuming to
60d67dc8 4822 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4823 then this symbol was already defined by some previous
4824 dynamic object, and we will be using that previous
4825 definition anyhow. */
4826
60d67dc8 4827 h->u.alias = weaks;
4ad4eba5 4828 weaks = h;
4ad4eba5
AM
4829 }
4830
4831 /* Set the alignment of a common symbol. */
a4d8e49b 4832 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4833 && h->root.type == bfd_link_hash_common)
4834 {
4835 unsigned int align;
4836
a4d8e49b 4837 if (common)
af44c138
L
4838 align = bfd_log2 (isym->st_value);
4839 else
4840 {
4841 /* The new symbol is a common symbol in a shared object.
4842 We need to get the alignment from the section. */
4843 align = new_sec->alignment_power;
4844 }
595213d4 4845 if (align > old_alignment)
4ad4eba5
AM
4846 h->root.u.c.p->alignment_power = align;
4847 else
4848 h->root.u.c.p->alignment_power = old_alignment;
4849 }
4850
66eb6687 4851 if (is_elf_hash_table (htab))
4ad4eba5 4852 {
4f3fedcf
AM
4853 /* Set a flag in the hash table entry indicating the type of
4854 reference or definition we just found. A dynamic symbol
4855 is one which is referenced or defined by both a regular
4856 object and a shared object. */
4857 bfd_boolean dynsym = FALSE;
4858
4859 /* Plugin symbols aren't normal. Don't set def_regular or
4860 ref_regular for them, or make them dynamic. */
4861 if ((abfd->flags & BFD_PLUGIN) != 0)
4862 ;
4863 else if (! dynamic)
4864 {
4865 if (! definition)
4866 {
4867 h->ref_regular = 1;
4868 if (bind != STB_WEAK)
4869 h->ref_regular_nonweak = 1;
4870 }
4871 else
4872 {
4873 h->def_regular = 1;
4874 if (h->def_dynamic)
4875 {
4876 h->def_dynamic = 0;
4877 h->ref_dynamic = 1;
4878 }
4879 }
4880
4881 /* If the indirect symbol has been forced local, don't
4882 make the real symbol dynamic. */
4883 if ((h == hi || !hi->forced_local)
0e1862bb 4884 && (bfd_link_dll (info)
4f3fedcf
AM
4885 || h->def_dynamic
4886 || h->ref_dynamic))
4887 dynsym = TRUE;
4888 }
4889 else
4890 {
4891 if (! definition)
4892 {
4893 h->ref_dynamic = 1;
4894 hi->ref_dynamic = 1;
4895 }
4896 else
4897 {
4898 h->def_dynamic = 1;
4899 hi->def_dynamic = 1;
4900 }
4901
4902 /* If the indirect symbol has been forced local, don't
4903 make the real symbol dynamic. */
4904 if ((h == hi || !hi->forced_local)
4905 && (h->def_regular
4906 || h->ref_regular
60d67dc8
AM
4907 || (h->is_weakalias
4908 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4909 dynsym = TRUE;
4910 }
4911
4912 /* Check to see if we need to add an indirect symbol for
4913 the default name. */
4914 if (definition
4915 || (!override && h->root.type == bfd_link_hash_common))
4916 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4917 sec, value, &old_bfd, &dynsym))
4918 goto error_free_vers;
4ad4eba5
AM
4919
4920 /* Check the alignment when a common symbol is involved. This
4921 can change when a common symbol is overridden by a normal
4922 definition or a common symbol is ignored due to the old
4923 normal definition. We need to make sure the maximum
4924 alignment is maintained. */
a4d8e49b 4925 if ((old_alignment || common)
4ad4eba5
AM
4926 && h->root.type != bfd_link_hash_common)
4927 {
4928 unsigned int common_align;
4929 unsigned int normal_align;
4930 unsigned int symbol_align;
4931 bfd *normal_bfd;
4932 bfd *common_bfd;
4933
3a81e825
AM
4934 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4935 || h->root.type == bfd_link_hash_defweak);
4936
4ad4eba5
AM
4937 symbol_align = ffs (h->root.u.def.value) - 1;
4938 if (h->root.u.def.section->owner != NULL
0616a280
AM
4939 && (h->root.u.def.section->owner->flags
4940 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4941 {
4942 normal_align = h->root.u.def.section->alignment_power;
4943 if (normal_align > symbol_align)
4944 normal_align = symbol_align;
4945 }
4946 else
4947 normal_align = symbol_align;
4948
4949 if (old_alignment)
4950 {
4951 common_align = old_alignment;
4952 common_bfd = old_bfd;
4953 normal_bfd = abfd;
4954 }
4955 else
4956 {
4957 common_align = bfd_log2 (isym->st_value);
4958 common_bfd = abfd;
4959 normal_bfd = old_bfd;
4960 }
4961
4962 if (normal_align < common_align)
d07676f8
NC
4963 {
4964 /* PR binutils/2735 */
4965 if (normal_bfd == NULL)
4eca0228 4966 _bfd_error_handler
695344c0 4967 /* xgettext:c-format */
9793eb77 4968 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4969 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4970 1 << common_align, name, common_bfd,
4971 1 << normal_align, h->root.u.def.section);
d07676f8 4972 else
4eca0228 4973 _bfd_error_handler
695344c0 4974 /* xgettext:c-format */
9793eb77 4975 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4976 " is smaller than %u in %pB"),
c08bb8dd
AM
4977 1 << normal_align, name, normal_bfd,
4978 1 << common_align, common_bfd);
d07676f8 4979 }
4ad4eba5
AM
4980 }
4981
83ad0046 4982 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4983 if (isym->st_size != 0
4984 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4985 && (definition || h->size == 0))
4986 {
83ad0046
L
4987 if (h->size != 0
4988 && h->size != isym->st_size
4989 && ! size_change_ok)
4eca0228 4990 _bfd_error_handler
695344c0 4991 /* xgettext:c-format */
9793eb77 4992 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4993 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4994 name, (uint64_t) h->size, old_bfd,
4995 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4996
4997 h->size = isym->st_size;
4998 }
4999
5000 /* If this is a common symbol, then we always want H->SIZE
5001 to be the size of the common symbol. The code just above
5002 won't fix the size if a common symbol becomes larger. We
5003 don't warn about a size change here, because that is
4f3fedcf 5004 covered by --warn-common. Allow changes between different
fcb93ecf 5005 function types. */
4ad4eba5
AM
5006 if (h->root.type == bfd_link_hash_common)
5007 h->size = h->root.u.c.size;
5008
5009 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5010 && ((definition && !new_weak)
5011 || (old_weak && h->root.type == bfd_link_hash_common)
5012 || h->type == STT_NOTYPE))
4ad4eba5 5013 {
2955ec4c
L
5014 unsigned int type = ELF_ST_TYPE (isym->st_info);
5015
5016 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5017 symbol. */
5018 if (type == STT_GNU_IFUNC
5019 && (abfd->flags & DYNAMIC) != 0)
5020 type = STT_FUNC;
4ad4eba5 5021
2955ec4c
L
5022 if (h->type != type)
5023 {
5024 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5025 /* xgettext:c-format */
4eca0228 5026 _bfd_error_handler
9793eb77 5027 (_("warning: type of symbol `%s' changed"
871b3ab2 5028 " from %d to %d in %pB"),
c08bb8dd 5029 name, h->type, type, abfd);
2955ec4c
L
5030
5031 h->type = type;
5032 }
4ad4eba5
AM
5033 }
5034
54ac0771 5035 /* Merge st_other field. */
b8417128 5036 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5037
c3df8c14 5038 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5039 if (definition
5040 && (sec->flags & SEC_DEBUGGING)
5041 && !bfd_link_relocatable (info))
c3df8c14
AM
5042 dynsym = FALSE;
5043
4f3fedcf
AM
5044 /* Nor should we make plugin symbols dynamic. */
5045 if ((abfd->flags & BFD_PLUGIN) != 0)
5046 dynsym = FALSE;
5047
35fc36a8 5048 if (definition)
35399224
L
5049 {
5050 h->target_internal = isym->st_target_internal;
5051 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5052 }
35fc36a8 5053
4ad4eba5
AM
5054 if (definition && !dynamic)
5055 {
5056 char *p = strchr (name, ELF_VER_CHR);
5057 if (p != NULL && p[1] != ELF_VER_CHR)
5058 {
5059 /* Queue non-default versions so that .symver x, x@FOO
5060 aliases can be checked. */
66eb6687 5061 if (!nondeflt_vers)
4ad4eba5 5062 {
986f0783
AM
5063 size_t amt = ((isymend - isym + 1)
5064 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5065 nondeflt_vers
5066 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5067 if (!nondeflt_vers)
5068 goto error_free_vers;
4ad4eba5 5069 }
66eb6687 5070 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5071 }
5072 }
5073
5074 if (dynsym && h->dynindx == -1)
5075 {
c152c796 5076 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5077 goto error_free_vers;
60d67dc8
AM
5078 if (h->is_weakalias
5079 && weakdef (h)->dynindx == -1)
4ad4eba5 5080 {
60d67dc8 5081 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5082 goto error_free_vers;
5083 }
5084 }
1f599d0e 5085 else if (h->dynindx != -1)
4ad4eba5
AM
5086 /* If the symbol already has a dynamic index, but
5087 visibility says it should not be visible, turn it into
5088 a local symbol. */
5089 switch (ELF_ST_VISIBILITY (h->other))
5090 {
5091 case STV_INTERNAL:
5092 case STV_HIDDEN:
5093 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5094 dynsym = FALSE;
5095 break;
5096 }
5097
aef28989
L
5098 /* Don't add DT_NEEDED for references from the dummy bfd nor
5099 for unmatched symbol. */
4ad4eba5 5100 if (!add_needed
aef28989 5101 && matched
4ad4eba5 5102 && definition
010e5ae2 5103 && ((dynsym
ffa9430d 5104 && h->ref_regular_nonweak
4f3fedcf
AM
5105 && (old_bfd == NULL
5106 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5107 || (h->ref_dynamic_nonweak
010e5ae2 5108 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5109 && !on_needed_list (elf_dt_name (abfd),
5110 htab->needed, NULL))))
4ad4eba5 5111 {
4ad4eba5
AM
5112 const char *soname = elf_dt_name (abfd);
5113
16e4ecc0
AM
5114 info->callbacks->minfo ("%!", soname, old_bfd,
5115 h->root.root.string);
5116
4ad4eba5
AM
5117 /* A symbol from a library loaded via DT_NEEDED of some
5118 other library is referenced by a regular object.
e56f61be 5119 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5120 --no-add-needed is used and the reference was not
5121 a weak one. */
4f3fedcf 5122 if (old_bfd != NULL
b918acf9 5123 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5124 {
4eca0228 5125 _bfd_error_handler
695344c0 5126 /* xgettext:c-format */
871b3ab2 5127 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5128 old_bfd, name);
ff5ac77b 5129 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5130 goto error_free_vers;
5131 }
5132
a50b1753 5133 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5134 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5135
e310298c
AM
5136 /* Create dynamic sections for backends that require
5137 that be done before setup_gnu_properties. */
5138 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5139 return FALSE;
4ad4eba5 5140 add_needed = TRUE;
4ad4eba5
AM
5141 }
5142 }
5143 }
5144
a83ef4d1
L
5145 if (info->lto_plugin_active
5146 && !bfd_link_relocatable (info)
5147 && (abfd->flags & BFD_PLUGIN) == 0
5148 && !just_syms
5149 && extsymcount)
5150 {
5151 int r_sym_shift;
5152
5153 if (bed->s->arch_size == 32)
5154 r_sym_shift = 8;
5155 else
5156 r_sym_shift = 32;
5157
5158 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5159 referenced in regular objects so that linker plugin will get
5160 the correct symbol resolution. */
5161
5162 sym_hash = elf_sym_hashes (abfd);
5163 for (s = abfd->sections; s != NULL; s = s->next)
5164 {
5165 Elf_Internal_Rela *internal_relocs;
5166 Elf_Internal_Rela *rel, *relend;
5167
5168 /* Don't check relocations in excluded sections. */
5169 if ((s->flags & SEC_RELOC) == 0
5170 || s->reloc_count == 0
5171 || (s->flags & SEC_EXCLUDE) != 0
5172 || ((info->strip == strip_all
5173 || info->strip == strip_debugger)
5174 && (s->flags & SEC_DEBUGGING) != 0))
5175 continue;
5176
5177 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5178 NULL,
5179 info->keep_memory);
5180 if (internal_relocs == NULL)
5181 goto error_free_vers;
5182
5183 rel = internal_relocs;
5184 relend = rel + s->reloc_count;
5185 for ( ; rel < relend; rel++)
5186 {
5187 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5188 struct elf_link_hash_entry *h;
5189
5190 /* Skip local symbols. */
5191 if (r_symndx < extsymoff)
5192 continue;
5193
5194 h = sym_hash[r_symndx - extsymoff];
5195 if (h != NULL)
5196 h->root.non_ir_ref_regular = 1;
5197 }
5198
5199 if (elf_section_data (s)->relocs != internal_relocs)
5200 free (internal_relocs);
5201 }
5202 }
5203
66eb6687
AM
5204 if (extversym != NULL)
5205 {
5206 free (extversym);
5207 extversym = NULL;
5208 }
5209
5210 if (isymbuf != NULL)
5211 {
5212 free (isymbuf);
5213 isymbuf = NULL;
5214 }
5215
5216 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5217 {
5218 unsigned int i;
5219
5220 /* Restore the symbol table. */
f45794cb
AM
5221 old_ent = (char *) old_tab + tabsize;
5222 memset (elf_sym_hashes (abfd), 0,
5223 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5224 htab->root.table.table = old_table;
5225 htab->root.table.size = old_size;
5226 htab->root.table.count = old_count;
66eb6687 5227 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5228 htab->root.undefs = old_undefs;
5229 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5230 if (htab->dynstr != NULL)
5231 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5232 free (old_strtab);
5233 old_strtab = NULL;
66eb6687
AM
5234 for (i = 0; i < htab->root.table.size; i++)
5235 {
5236 struct bfd_hash_entry *p;
5237 struct elf_link_hash_entry *h;
3e0882af
L
5238 bfd_size_type size;
5239 unsigned int alignment_power;
4070765b 5240 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5241
5242 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5243 {
5244 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5245 if (h->root.type == bfd_link_hash_warning)
5246 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5247
3e0882af
L
5248 /* Preserve the maximum alignment and size for common
5249 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5250 since it can still be loaded at run time by another
3e0882af
L
5251 dynamic lib. */
5252 if (h->root.type == bfd_link_hash_common)
5253 {
5254 size = h->root.u.c.size;
5255 alignment_power = h->root.u.c.p->alignment_power;
5256 }
5257 else
5258 {
5259 size = 0;
5260 alignment_power = 0;
5261 }
4070765b 5262 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5263 will be exported when the dynamic lib becomes needed
5264 in the second pass. */
4070765b 5265 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5266 memcpy (p, old_ent, htab->root.table.entsize);
5267 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5268 h = (struct elf_link_hash_entry *) p;
5269 if (h->root.type == bfd_link_hash_warning)
5270 {
5271 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5272 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5273 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5274 }
a4542f1b 5275 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5276 {
5277 if (size > h->root.u.c.size)
5278 h->root.u.c.size = size;
5279 if (alignment_power > h->root.u.c.p->alignment_power)
5280 h->root.u.c.p->alignment_power = alignment_power;
5281 }
4070765b 5282 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5283 }
5284 }
5285
5061a885
AM
5286 /* Make a special call to the linker "notice" function to
5287 tell it that symbols added for crefs may need to be removed. */
e5034e59 5288 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5289 goto error_free_vers;
5061a885 5290
66eb6687
AM
5291 free (old_tab);
5292 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5293 alloc_mark);
5294 if (nondeflt_vers != NULL)
5295 free (nondeflt_vers);
5296 return TRUE;
5297 }
2de92251 5298
66eb6687
AM
5299 if (old_tab != NULL)
5300 {
e5034e59 5301 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5302 goto error_free_vers;
66eb6687
AM
5303 free (old_tab);
5304 old_tab = NULL;
5305 }
5306
c6e8a9a8
L
5307 /* Now that all the symbols from this input file are created, if
5308 not performing a relocatable link, handle .symver foo, foo@BAR
5309 such that any relocs against foo become foo@BAR. */
0e1862bb 5310 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5311 {
ef53be89 5312 size_t cnt, symidx;
4ad4eba5
AM
5313
5314 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5315 {
5316 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5317 char *shortname, *p;
986f0783 5318 size_t amt;
4ad4eba5
AM
5319
5320 p = strchr (h->root.root.string, ELF_VER_CHR);
5321 if (p == NULL
5322 || (h->root.type != bfd_link_hash_defined
5323 && h->root.type != bfd_link_hash_defweak))
5324 continue;
5325
5326 amt = p - h->root.root.string;
a50b1753 5327 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5328 if (!shortname)
5329 goto error_free_vers;
4ad4eba5
AM
5330 memcpy (shortname, h->root.root.string, amt);
5331 shortname[amt] = '\0';
5332
5333 hi = (struct elf_link_hash_entry *)
66eb6687 5334 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5335 FALSE, FALSE, FALSE);
5336 if (hi != NULL
5337 && hi->root.type == h->root.type
5338 && hi->root.u.def.value == h->root.u.def.value
5339 && hi->root.u.def.section == h->root.u.def.section)
5340 {
5341 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5342 hi->root.type = bfd_link_hash_indirect;
5343 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5344 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5345 sym_hash = elf_sym_hashes (abfd);
5346 if (sym_hash)
5347 for (symidx = 0; symidx < extsymcount; ++symidx)
5348 if (sym_hash[symidx] == hi)
5349 {
5350 sym_hash[symidx] = h;
5351 break;
5352 }
5353 }
5354 free (shortname);
5355 }
5356 free (nondeflt_vers);
5357 nondeflt_vers = NULL;
5358 }
5359
60d67dc8 5360 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5361 symbols we found. The only way to do this is to search all the
5362 symbols. Since we only need the information for non functions in
5363 dynamic objects, that's the only time we actually put anything on
5364 the list WEAKS. We need this information so that if a regular
5365 object refers to a symbol defined weakly in a dynamic object, the
5366 real symbol in the dynamic object is also put in the dynamic
5367 symbols; we also must arrange for both symbols to point to the
5368 same memory location. We could handle the general case of symbol
5369 aliasing, but a general symbol alias can only be generated in
5370 assembler code, handling it correctly would be very time
5371 consuming, and other ELF linkers don't handle general aliasing
5372 either. */
5373 if (weaks != NULL)
5374 {
5375 struct elf_link_hash_entry **hpp;
5376 struct elf_link_hash_entry **hppend;
5377 struct elf_link_hash_entry **sorted_sym_hash;
5378 struct elf_link_hash_entry *h;
986f0783 5379 size_t sym_count, amt;
4ad4eba5
AM
5380
5381 /* Since we have to search the whole symbol list for each weak
5382 defined symbol, search time for N weak defined symbols will be
5383 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5384 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5385 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5386 if (sorted_sym_hash == NULL)
5387 goto error_return;
5388 sym_hash = sorted_sym_hash;
5389 hpp = elf_sym_hashes (abfd);
5390 hppend = hpp + extsymcount;
5391 sym_count = 0;
5392 for (; hpp < hppend; hpp++)
5393 {
5394 h = *hpp;
5395 if (h != NULL
5396 && h->root.type == bfd_link_hash_defined
fcb93ecf 5397 && !bed->is_function_type (h->type))
4ad4eba5
AM
5398 {
5399 *sym_hash = h;
5400 sym_hash++;
5401 sym_count++;
5402 }
5403 }
5404
3a3f4bf7 5405 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5406 elf_sort_symbol);
5407
5408 while (weaks != NULL)
5409 {
5410 struct elf_link_hash_entry *hlook;
5411 asection *slook;
5412 bfd_vma vlook;
ed54588d 5413 size_t i, j, idx = 0;
4ad4eba5
AM
5414
5415 hlook = weaks;
60d67dc8
AM
5416 weaks = hlook->u.alias;
5417 hlook->u.alias = NULL;
4ad4eba5 5418
e3e53eed
AM
5419 if (hlook->root.type != bfd_link_hash_defined
5420 && hlook->root.type != bfd_link_hash_defweak)
5421 continue;
5422
4ad4eba5
AM
5423 slook = hlook->root.u.def.section;
5424 vlook = hlook->root.u.def.value;
5425
4ad4eba5
AM
5426 i = 0;
5427 j = sym_count;
14160578 5428 while (i != j)
4ad4eba5
AM
5429 {
5430 bfd_signed_vma vdiff;
5431 idx = (i + j) / 2;
14160578 5432 h = sorted_sym_hash[idx];
4ad4eba5
AM
5433 vdiff = vlook - h->root.u.def.value;
5434 if (vdiff < 0)
5435 j = idx;
5436 else if (vdiff > 0)
5437 i = idx + 1;
5438 else
5439 {
d3435ae8 5440 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5441 if (sdiff < 0)
5442 j = idx;
5443 else if (sdiff > 0)
5444 i = idx + 1;
5445 else
14160578 5446 break;
4ad4eba5
AM
5447 }
5448 }
5449
5450 /* We didn't find a value/section match. */
14160578 5451 if (i == j)
4ad4eba5
AM
5452 continue;
5453
14160578
AM
5454 /* With multiple aliases, or when the weak symbol is already
5455 strongly defined, we have multiple matching symbols and
5456 the binary search above may land on any of them. Step
5457 one past the matching symbol(s). */
5458 while (++idx != j)
5459 {
5460 h = sorted_sym_hash[idx];
5461 if (h->root.u.def.section != slook
5462 || h->root.u.def.value != vlook)
5463 break;
5464 }
5465
5466 /* Now look back over the aliases. Since we sorted by size
5467 as well as value and section, we'll choose the one with
5468 the largest size. */
5469 while (idx-- != i)
4ad4eba5 5470 {
14160578 5471 h = sorted_sym_hash[idx];
4ad4eba5
AM
5472
5473 /* Stop if value or section doesn't match. */
14160578
AM
5474 if (h->root.u.def.section != slook
5475 || h->root.u.def.value != vlook)
4ad4eba5
AM
5476 break;
5477 else if (h != hlook)
5478 {
60d67dc8
AM
5479 struct elf_link_hash_entry *t;
5480
5481 hlook->u.alias = h;
5482 hlook->is_weakalias = 1;
5483 t = h;
5484 if (t->u.alias != NULL)
5485 while (t->u.alias != h)
5486 t = t->u.alias;
5487 t->u.alias = hlook;
4ad4eba5
AM
5488
5489 /* If the weak definition is in the list of dynamic
5490 symbols, make sure the real definition is put
5491 there as well. */
5492 if (hlook->dynindx != -1 && h->dynindx == -1)
5493 {
c152c796 5494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5495 {
5496 err_free_sym_hash:
5497 free (sorted_sym_hash);
5498 goto error_return;
5499 }
4ad4eba5
AM
5500 }
5501
5502 /* If the real definition is in the list of dynamic
5503 symbols, make sure the weak definition is put
5504 there as well. If we don't do this, then the
5505 dynamic loader might not merge the entries for the
5506 real definition and the weak definition. */
5507 if (h->dynindx != -1 && hlook->dynindx == -1)
5508 {
c152c796 5509 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5510 goto err_free_sym_hash;
4ad4eba5
AM
5511 }
5512 break;
5513 }
5514 }
5515 }
5516
5517 free (sorted_sym_hash);
5518 }
5519
33177bb1
AM
5520 if (bed->check_directives
5521 && !(*bed->check_directives) (abfd, info))
5522 return FALSE;
85fbca6a 5523
4ad4eba5
AM
5524 /* If this is a non-traditional link, try to optimize the handling
5525 of the .stab/.stabstr sections. */
5526 if (! dynamic
5527 && ! info->traditional_format
66eb6687 5528 && is_elf_hash_table (htab)
4ad4eba5
AM
5529 && (info->strip != strip_all && info->strip != strip_debugger))
5530 {
5531 asection *stabstr;
5532
5533 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5534 if (stabstr != NULL)
5535 {
5536 bfd_size_type string_offset = 0;
5537 asection *stab;
5538
5539 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5540 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5541 && (!stab->name[5] ||
5542 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5543 && (stab->flags & SEC_MERGE) == 0
5544 && !bfd_is_abs_section (stab->output_section))
5545 {
5546 struct bfd_elf_section_data *secdata;
5547
5548 secdata = elf_section_data (stab);
66eb6687
AM
5549 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5550 stabstr, &secdata->sec_info,
4ad4eba5
AM
5551 &string_offset))
5552 goto error_return;
5553 if (secdata->sec_info)
dbaa2011 5554 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5555 }
5556 }
5557 }
5558
e310298c 5559 if (dynamic && add_needed)
4ad4eba5
AM
5560 {
5561 /* Add this bfd to the loaded list. */
5562 struct elf_link_loaded_list *n;
5563
ca4be51c 5564 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5565 if (n == NULL)
5566 goto error_return;
5567 n->abfd = abfd;
e310298c
AM
5568 n->next = htab->dyn_loaded;
5569 htab->dyn_loaded = n;
4ad4eba5 5570 }
e310298c
AM
5571 if (dynamic && !add_needed
5572 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5573 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5574
5575 return TRUE;
5576
5577 error_free_vers:
66eb6687
AM
5578 if (old_tab != NULL)
5579 free (old_tab);
5b677558
AM
5580 if (old_strtab != NULL)
5581 free (old_strtab);
4ad4eba5
AM
5582 if (nondeflt_vers != NULL)
5583 free (nondeflt_vers);
5584 if (extversym != NULL)
5585 free (extversym);
5586 error_free_sym:
5587 if (isymbuf != NULL)
5588 free (isymbuf);
5589 error_return:
5590 return FALSE;
5591}
5592
8387904d
AM
5593/* Return the linker hash table entry of a symbol that might be
5594 satisfied by an archive symbol. Return -1 on error. */
5595
5596struct elf_link_hash_entry *
5597_bfd_elf_archive_symbol_lookup (bfd *abfd,
5598 struct bfd_link_info *info,
5599 const char *name)
5600{
5601 struct elf_link_hash_entry *h;
5602 char *p, *copy;
5603 size_t len, first;
5604
2a41f396 5605 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5606 if (h != NULL)
5607 return h;
5608
5609 /* If this is a default version (the name contains @@), look up the
5610 symbol again with only one `@' as well as without the version.
5611 The effect is that references to the symbol with and without the
5612 version will be matched by the default symbol in the archive. */
5613
5614 p = strchr (name, ELF_VER_CHR);
5615 if (p == NULL || p[1] != ELF_VER_CHR)
5616 return h;
5617
5618 /* First check with only one `@'. */
5619 len = strlen (name);
a50b1753 5620 copy = (char *) bfd_alloc (abfd, len);
8387904d 5621 if (copy == NULL)
e99955cd 5622 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5623
5624 first = p - name + 1;
5625 memcpy (copy, name, first);
5626 memcpy (copy + first, name + first + 1, len - first);
5627
2a41f396 5628 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5629 if (h == NULL)
5630 {
5631 /* We also need to check references to the symbol without the
5632 version. */
5633 copy[first - 1] = '\0';
5634 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5635 FALSE, FALSE, TRUE);
8387904d
AM
5636 }
5637
5638 bfd_release (abfd, copy);
5639 return h;
5640}
5641
0ad989f9 5642/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5643 don't use _bfd_generic_link_add_archive_symbols because we need to
5644 handle versioned symbols.
0ad989f9
L
5645
5646 Fortunately, ELF archive handling is simpler than that done by
5647 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5648 oddities. In ELF, if we find a symbol in the archive map, and the
5649 symbol is currently undefined, we know that we must pull in that
5650 object file.
5651
5652 Unfortunately, we do have to make multiple passes over the symbol
5653 table until nothing further is resolved. */
5654
4ad4eba5
AM
5655static bfd_boolean
5656elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5657{
5658 symindex c;
13e570f8 5659 unsigned char *included = NULL;
0ad989f9
L
5660 carsym *symdefs;
5661 bfd_boolean loop;
986f0783 5662 size_t amt;
8387904d
AM
5663 const struct elf_backend_data *bed;
5664 struct elf_link_hash_entry * (*archive_symbol_lookup)
5665 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5666
5667 if (! bfd_has_map (abfd))
5668 {
5669 /* An empty archive is a special case. */
5670 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5671 return TRUE;
5672 bfd_set_error (bfd_error_no_armap);
5673 return FALSE;
5674 }
5675
5676 /* Keep track of all symbols we know to be already defined, and all
5677 files we know to be already included. This is to speed up the
5678 second and subsequent passes. */
5679 c = bfd_ardata (abfd)->symdef_count;
5680 if (c == 0)
5681 return TRUE;
986f0783 5682 amt = c * sizeof (*included);
13e570f8
AM
5683 included = (unsigned char *) bfd_zmalloc (amt);
5684 if (included == NULL)
5685 return FALSE;
0ad989f9
L
5686
5687 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5688 bed = get_elf_backend_data (abfd);
5689 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5690
5691 do
5692 {
5693 file_ptr last;
5694 symindex i;
5695 carsym *symdef;
5696 carsym *symdefend;
5697
5698 loop = FALSE;
5699 last = -1;
5700
5701 symdef = symdefs;
5702 symdefend = symdef + c;
5703 for (i = 0; symdef < symdefend; symdef++, i++)
5704 {
5705 struct elf_link_hash_entry *h;
5706 bfd *element;
5707 struct bfd_link_hash_entry *undefs_tail;
5708 symindex mark;
5709
13e570f8 5710 if (included[i])
0ad989f9
L
5711 continue;
5712 if (symdef->file_offset == last)
5713 {
5714 included[i] = TRUE;
5715 continue;
5716 }
5717
8387904d 5718 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5719 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5720 goto error_return;
0ad989f9
L
5721
5722 if (h == NULL)
5723 continue;
5724
5725 if (h->root.type == bfd_link_hash_common)
5726 {
5727 /* We currently have a common symbol. The archive map contains
5728 a reference to this symbol, so we may want to include it. We
5729 only want to include it however, if this archive element
5730 contains a definition of the symbol, not just another common
5731 declaration of it.
5732
5733 Unfortunately some archivers (including GNU ar) will put
5734 declarations of common symbols into their archive maps, as
5735 well as real definitions, so we cannot just go by the archive
5736 map alone. Instead we must read in the element's symbol
5737 table and check that to see what kind of symbol definition
5738 this is. */
5739 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5740 continue;
5741 }
5742 else if (h->root.type != bfd_link_hash_undefined)
5743 {
5744 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5745 /* Symbol must be defined. Don't check it again. */
5746 included[i] = TRUE;
0ad989f9
L
5747 continue;
5748 }
5749
5750 /* We need to include this archive member. */
5751 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5752 if (element == NULL)
5753 goto error_return;
5754
5755 if (! bfd_check_format (element, bfd_object))
5756 goto error_return;
5757
0ad989f9
L
5758 undefs_tail = info->hash->undefs_tail;
5759
0e144ba7
AM
5760 if (!(*info->callbacks
5761 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5762 continue;
0e144ba7 5763 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5764 goto error_return;
5765
5766 /* If there are any new undefined symbols, we need to make
5767 another pass through the archive in order to see whether
5768 they can be defined. FIXME: This isn't perfect, because
5769 common symbols wind up on undefs_tail and because an
5770 undefined symbol which is defined later on in this pass
5771 does not require another pass. This isn't a bug, but it
5772 does make the code less efficient than it could be. */
5773 if (undefs_tail != info->hash->undefs_tail)
5774 loop = TRUE;
5775
5776 /* Look backward to mark all symbols from this object file
5777 which we have already seen in this pass. */
5778 mark = i;
5779 do
5780 {
5781 included[mark] = TRUE;
5782 if (mark == 0)
5783 break;
5784 --mark;
5785 }
5786 while (symdefs[mark].file_offset == symdef->file_offset);
5787
5788 /* We mark subsequent symbols from this object file as we go
5789 on through the loop. */
5790 last = symdef->file_offset;
5791 }
5792 }
5793 while (loop);
5794
0ad989f9
L
5795 free (included);
5796
5797 return TRUE;
5798
5799 error_return:
0ad989f9
L
5800 if (included != NULL)
5801 free (included);
5802 return FALSE;
5803}
4ad4eba5
AM
5804
5805/* Given an ELF BFD, add symbols to the global hash table as
5806 appropriate. */
5807
5808bfd_boolean
5809bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5810{
5811 switch (bfd_get_format (abfd))
5812 {
5813 case bfd_object:
5814 return elf_link_add_object_symbols (abfd, info);
5815 case bfd_archive:
5816 return elf_link_add_archive_symbols (abfd, info);
5817 default:
5818 bfd_set_error (bfd_error_wrong_format);
5819 return FALSE;
5820 }
5821}
5a580b3a 5822\f
14b1c01e
AM
5823struct hash_codes_info
5824{
5825 unsigned long *hashcodes;
5826 bfd_boolean error;
5827};
a0c8462f 5828
5a580b3a
AM
5829/* This function will be called though elf_link_hash_traverse to store
5830 all hash value of the exported symbols in an array. */
5831
5832static bfd_boolean
5833elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5834{
a50b1753 5835 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5836 const char *name;
5a580b3a
AM
5837 unsigned long ha;
5838 char *alc = NULL;
5839
5a580b3a
AM
5840 /* Ignore indirect symbols. These are added by the versioning code. */
5841 if (h->dynindx == -1)
5842 return TRUE;
5843
5844 name = h->root.root.string;
422f1182 5845 if (h->versioned >= versioned)
5a580b3a 5846 {
422f1182
L
5847 char *p = strchr (name, ELF_VER_CHR);
5848 if (p != NULL)
14b1c01e 5849 {
422f1182
L
5850 alc = (char *) bfd_malloc (p - name + 1);
5851 if (alc == NULL)
5852 {
5853 inf->error = TRUE;
5854 return FALSE;
5855 }
5856 memcpy (alc, name, p - name);
5857 alc[p - name] = '\0';
5858 name = alc;
14b1c01e 5859 }
5a580b3a
AM
5860 }
5861
5862 /* Compute the hash value. */
5863 ha = bfd_elf_hash (name);
5864
5865 /* Store the found hash value in the array given as the argument. */
14b1c01e 5866 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5867
5868 /* And store it in the struct so that we can put it in the hash table
5869 later. */
f6e332e6 5870 h->u.elf_hash_value = ha;
5a580b3a
AM
5871
5872 if (alc != NULL)
5873 free (alc);
5874
5875 return TRUE;
5876}
5877
fdc90cb4
JJ
5878struct collect_gnu_hash_codes
5879{
5880 bfd *output_bfd;
5881 const struct elf_backend_data *bed;
5882 unsigned long int nsyms;
5883 unsigned long int maskbits;
5884 unsigned long int *hashcodes;
5885 unsigned long int *hashval;
5886 unsigned long int *indx;
5887 unsigned long int *counts;
5888 bfd_vma *bitmask;
5889 bfd_byte *contents;
f16a9783 5890 bfd_size_type xlat;
fdc90cb4
JJ
5891 long int min_dynindx;
5892 unsigned long int bucketcount;
5893 unsigned long int symindx;
5894 long int local_indx;
5895 long int shift1, shift2;
5896 unsigned long int mask;
14b1c01e 5897 bfd_boolean error;
fdc90cb4
JJ
5898};
5899
5900/* This function will be called though elf_link_hash_traverse to store
5901 all hash value of the exported symbols in an array. */
5902
5903static bfd_boolean
5904elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5905{
a50b1753 5906 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5907 const char *name;
fdc90cb4
JJ
5908 unsigned long ha;
5909 char *alc = NULL;
5910
fdc90cb4
JJ
5911 /* Ignore indirect symbols. These are added by the versioning code. */
5912 if (h->dynindx == -1)
5913 return TRUE;
5914
5915 /* Ignore also local symbols and undefined symbols. */
5916 if (! (*s->bed->elf_hash_symbol) (h))
5917 return TRUE;
5918
5919 name = h->root.root.string;
422f1182 5920 if (h->versioned >= versioned)
fdc90cb4 5921 {
422f1182
L
5922 char *p = strchr (name, ELF_VER_CHR);
5923 if (p != NULL)
14b1c01e 5924 {
422f1182
L
5925 alc = (char *) bfd_malloc (p - name + 1);
5926 if (alc == NULL)
5927 {
5928 s->error = TRUE;
5929 return FALSE;
5930 }
5931 memcpy (alc, name, p - name);
5932 alc[p - name] = '\0';
5933 name = alc;
14b1c01e 5934 }
fdc90cb4
JJ
5935 }
5936
5937 /* Compute the hash value. */
5938 ha = bfd_elf_gnu_hash (name);
5939
5940 /* Store the found hash value in the array for compute_bucket_count,
5941 and also for .dynsym reordering purposes. */
5942 s->hashcodes[s->nsyms] = ha;
5943 s->hashval[h->dynindx] = ha;
5944 ++s->nsyms;
5945 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5946 s->min_dynindx = h->dynindx;
5947
5948 if (alc != NULL)
5949 free (alc);
5950
5951 return TRUE;
5952}
5953
5954/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
5955 final dynamic symbol renumbering in case of .gnu.hash.
5956 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5957 to the translation table. */
fdc90cb4
JJ
5958
5959static bfd_boolean
f16a9783 5960elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 5961{
a50b1753 5962 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5963 unsigned long int bucket;
5964 unsigned long int val;
5965
fdc90cb4
JJ
5966 /* Ignore indirect symbols. */
5967 if (h->dynindx == -1)
5968 return TRUE;
5969
5970 /* Ignore also local symbols and undefined symbols. */
5971 if (! (*s->bed->elf_hash_symbol) (h))
5972 {
5973 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
5974 {
5975 if (s->bed->record_xhash_symbol != NULL)
5976 {
5977 (*s->bed->record_xhash_symbol) (h, 0);
5978 s->local_indx++;
5979 }
5980 else
5981 h->dynindx = s->local_indx++;
5982 }
fdc90cb4
JJ
5983 return TRUE;
5984 }
5985
5986 bucket = s->hashval[h->dynindx] % s->bucketcount;
5987 val = (s->hashval[h->dynindx] >> s->shift1)
5988 & ((s->maskbits >> s->shift1) - 1);
5989 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5990 s->bitmask[val]
5991 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5992 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5993 if (s->counts[bucket] == 1)
5994 /* Last element terminates the chain. */
5995 val |= 1;
5996 bfd_put_32 (s->output_bfd, val,
5997 s->contents + (s->indx[bucket] - s->symindx) * 4);
5998 --s->counts[bucket];
f16a9783
MS
5999 if (s->bed->record_xhash_symbol != NULL)
6000 {
6001 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6002
6003 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6004 }
6005 else
6006 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6007 return TRUE;
6008}
6009
6010/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6011
6012bfd_boolean
6013_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6014{
6015 return !(h->forced_local
6016 || h->root.type == bfd_link_hash_undefined
6017 || h->root.type == bfd_link_hash_undefweak
6018 || ((h->root.type == bfd_link_hash_defined
6019 || h->root.type == bfd_link_hash_defweak)
6020 && h->root.u.def.section->output_section == NULL));
6021}
6022
5a580b3a
AM
6023/* Array used to determine the number of hash table buckets to use
6024 based on the number of symbols there are. If there are fewer than
6025 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6026 fewer than 37 we use 17 buckets, and so forth. We never use more
6027 than 32771 buckets. */
6028
6029static const size_t elf_buckets[] =
6030{
6031 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6032 16411, 32771, 0
6033};
6034
6035/* Compute bucket count for hashing table. We do not use a static set
6036 of possible tables sizes anymore. Instead we determine for all
6037 possible reasonable sizes of the table the outcome (i.e., the
6038 number of collisions etc) and choose the best solution. The
6039 weighting functions are not too simple to allow the table to grow
6040 without bounds. Instead one of the weighting factors is the size.
6041 Therefore the result is always a good payoff between few collisions
6042 (= short chain lengths) and table size. */
6043static size_t
b20dd2ce 6044compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6045 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6046 unsigned long int nsyms,
6047 int gnu_hash)
5a580b3a 6048{
5a580b3a 6049 size_t best_size = 0;
5a580b3a 6050 unsigned long int i;
5a580b3a 6051
5a580b3a
AM
6052 /* We have a problem here. The following code to optimize the table
6053 size requires an integer type with more the 32 bits. If
6054 BFD_HOST_U_64_BIT is set we know about such a type. */
6055#ifdef BFD_HOST_U_64_BIT
6056 if (info->optimize)
6057 {
5a580b3a
AM
6058 size_t minsize;
6059 size_t maxsize;
6060 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6061 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6062 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6063 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6064 unsigned long int *counts;
d40f3da9 6065 bfd_size_type amt;
0883b6e0 6066 unsigned int no_improvement_count = 0;
5a580b3a
AM
6067
6068 /* Possible optimization parameters: if we have NSYMS symbols we say
6069 that the hashing table must at least have NSYMS/4 and at most
6070 2*NSYMS buckets. */
6071 minsize = nsyms / 4;
6072 if (minsize == 0)
6073 minsize = 1;
6074 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6075 if (gnu_hash)
6076 {
6077 if (minsize < 2)
6078 minsize = 2;
6079 if ((best_size & 31) == 0)
6080 ++best_size;
6081 }
5a580b3a
AM
6082
6083 /* Create array where we count the collisions in. We must use bfd_malloc
6084 since the size could be large. */
6085 amt = maxsize;
6086 amt *= sizeof (unsigned long int);
a50b1753 6087 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6088 if (counts == NULL)
fdc90cb4 6089 return 0;
5a580b3a
AM
6090
6091 /* Compute the "optimal" size for the hash table. The criteria is a
6092 minimal chain length. The minor criteria is (of course) the size
6093 of the table. */
6094 for (i = minsize; i < maxsize; ++i)
6095 {
6096 /* Walk through the array of hashcodes and count the collisions. */
6097 BFD_HOST_U_64_BIT max;
6098 unsigned long int j;
6099 unsigned long int fact;
6100
fdc90cb4
JJ
6101 if (gnu_hash && (i & 31) == 0)
6102 continue;
6103
5a580b3a
AM
6104 memset (counts, '\0', i * sizeof (unsigned long int));
6105
6106 /* Determine how often each hash bucket is used. */
6107 for (j = 0; j < nsyms; ++j)
6108 ++counts[hashcodes[j] % i];
6109
6110 /* For the weight function we need some information about the
6111 pagesize on the target. This is information need not be 100%
6112 accurate. Since this information is not available (so far) we
6113 define it here to a reasonable default value. If it is crucial
6114 to have a better value some day simply define this value. */
6115# ifndef BFD_TARGET_PAGESIZE
6116# define BFD_TARGET_PAGESIZE (4096)
6117# endif
6118
fdc90cb4
JJ
6119 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6120 and the chains. */
6121 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6122
6123# if 1
6124 /* Variant 1: optimize for short chains. We add the squares
6125 of all the chain lengths (which favors many small chain
6126 over a few long chains). */
6127 for (j = 0; j < i; ++j)
6128 max += counts[j] * counts[j];
6129
6130 /* This adds penalties for the overall size of the table. */
fdc90cb4 6131 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6132 max *= fact * fact;
6133# else
6134 /* Variant 2: Optimize a lot more for small table. Here we
6135 also add squares of the size but we also add penalties for
6136 empty slots (the +1 term). */
6137 for (j = 0; j < i; ++j)
6138 max += (1 + counts[j]) * (1 + counts[j]);
6139
6140 /* The overall size of the table is considered, but not as
6141 strong as in variant 1, where it is squared. */
fdc90cb4 6142 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6143 max *= fact;
6144# endif
6145
6146 /* Compare with current best results. */
6147 if (max < best_chlen)
6148 {
6149 best_chlen = max;
6150 best_size = i;
ca4be51c 6151 no_improvement_count = 0;
5a580b3a 6152 }
0883b6e0
NC
6153 /* PR 11843: Avoid futile long searches for the best bucket size
6154 when there are a large number of symbols. */
6155 else if (++no_improvement_count == 100)
6156 break;
5a580b3a
AM
6157 }
6158
6159 free (counts);
6160 }
6161 else
6162#endif /* defined (BFD_HOST_U_64_BIT) */
6163 {
6164 /* This is the fallback solution if no 64bit type is available or if we
6165 are not supposed to spend much time on optimizations. We select the
6166 bucket count using a fixed set of numbers. */
6167 for (i = 0; elf_buckets[i] != 0; i++)
6168 {
6169 best_size = elf_buckets[i];
fdc90cb4 6170 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6171 break;
6172 }
fdc90cb4
JJ
6173 if (gnu_hash && best_size < 2)
6174 best_size = 2;
5a580b3a
AM
6175 }
6176
5a580b3a
AM
6177 return best_size;
6178}
6179
d0bf826b
AM
6180/* Size any SHT_GROUP section for ld -r. */
6181
6182bfd_boolean
6183_bfd_elf_size_group_sections (struct bfd_link_info *info)
6184{
6185 bfd *ibfd;
57963c05 6186 asection *s;
d0bf826b 6187
c72f2fb2 6188 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6189 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6190 && (s = ibfd->sections) != NULL
6191 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6192 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6193 return FALSE;
6194 return TRUE;
6195}
6196
04c3a755
NS
6197/* Set a default stack segment size. The value in INFO wins. If it
6198 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6199 undefined it is initialized. */
6200
6201bfd_boolean
6202bfd_elf_stack_segment_size (bfd *output_bfd,
6203 struct bfd_link_info *info,
6204 const char *legacy_symbol,
6205 bfd_vma default_size)
6206{
6207 struct elf_link_hash_entry *h = NULL;
6208
6209 /* Look for legacy symbol. */
6210 if (legacy_symbol)
6211 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6212 FALSE, FALSE, FALSE);
6213 if (h && (h->root.type == bfd_link_hash_defined
6214 || h->root.type == bfd_link_hash_defweak)
6215 && h->def_regular
6216 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6217 {
6218 /* The symbol has no type if specified on the command line. */
6219 h->type = STT_OBJECT;
6220 if (info->stacksize)
695344c0 6221 /* xgettext:c-format */
871b3ab2 6222 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6223 output_bfd, legacy_symbol);
04c3a755 6224 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6225 /* xgettext:c-format */
871b3ab2 6226 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6227 output_bfd, legacy_symbol);
04c3a755
NS
6228 else
6229 info->stacksize = h->root.u.def.value;
6230 }
6231
6232 if (!info->stacksize)
6233 /* If the user didn't set a size, or explicitly inhibit the
6234 size, set it now. */
6235 info->stacksize = default_size;
6236
6237 /* Provide the legacy symbol, if it is referenced. */
6238 if (h && (h->root.type == bfd_link_hash_undefined
6239 || h->root.type == bfd_link_hash_undefweak))
6240 {
6241 struct bfd_link_hash_entry *bh = NULL;
6242
6243 if (!(_bfd_generic_link_add_one_symbol
6244 (info, output_bfd, legacy_symbol,
6245 BSF_GLOBAL, bfd_abs_section_ptr,
6246 info->stacksize >= 0 ? info->stacksize : 0,
6247 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6248 return FALSE;
6249
6250 h = (struct elf_link_hash_entry *) bh;
6251 h->def_regular = 1;
6252 h->type = STT_OBJECT;
6253 }
6254
6255 return TRUE;
6256}
6257
b531344c
MR
6258/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6259
6260struct elf_gc_sweep_symbol_info
6261{
6262 struct bfd_link_info *info;
6263 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6264 bfd_boolean);
6265};
6266
6267static bfd_boolean
6268elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6269{
6270 if (!h->mark
6271 && (((h->root.type == bfd_link_hash_defined
6272 || h->root.type == bfd_link_hash_defweak)
6273 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6274 && h->root.u.def.section->gc_mark))
6275 || h->root.type == bfd_link_hash_undefined
6276 || h->root.type == bfd_link_hash_undefweak))
6277 {
6278 struct elf_gc_sweep_symbol_info *inf;
6279
6280 inf = (struct elf_gc_sweep_symbol_info *) data;
6281 (*inf->hide_symbol) (inf->info, h, TRUE);
6282 h->def_regular = 0;
6283 h->ref_regular = 0;
6284 h->ref_regular_nonweak = 0;
6285 }
6286
6287 return TRUE;
6288}
6289
5a580b3a
AM
6290/* Set up the sizes and contents of the ELF dynamic sections. This is
6291 called by the ELF linker emulation before_allocation routine. We
6292 must set the sizes of the sections before the linker sets the
6293 addresses of the various sections. */
6294
6295bfd_boolean
6296bfd_elf_size_dynamic_sections (bfd *output_bfd,
6297 const char *soname,
6298 const char *rpath,
6299 const char *filter_shlib,
7ee314fa
AM
6300 const char *audit,
6301 const char *depaudit,
5a580b3a
AM
6302 const char * const *auxiliary_filters,
6303 struct bfd_link_info *info,
fd91d419 6304 asection **sinterpptr)
5a580b3a 6305{
5a580b3a
AM
6306 bfd *dynobj;
6307 const struct elf_backend_data *bed;
5a580b3a
AM
6308
6309 *sinterpptr = NULL;
6310
5a580b3a
AM
6311 if (!is_elf_hash_table (info->hash))
6312 return TRUE;
6313
5a580b3a
AM
6314 dynobj = elf_hash_table (info)->dynobj;
6315
9a2a56cc 6316 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6317 {
902e9fc7
MR
6318 struct bfd_elf_version_tree *verdefs;
6319 struct elf_info_failed asvinfo;
5a580b3a
AM
6320 struct bfd_elf_version_tree *t;
6321 struct bfd_elf_version_expr *d;
902e9fc7 6322 asection *s;
e6699019 6323 size_t soname_indx;
7ee314fa 6324
5a580b3a
AM
6325 /* If we are supposed to export all symbols into the dynamic symbol
6326 table (this is not the normal case), then do so. */
55255dae 6327 if (info->export_dynamic
0e1862bb 6328 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6329 {
3d13f3e9
AM
6330 struct elf_info_failed eif;
6331
6332 eif.info = info;
6333 eif.failed = FALSE;
5a580b3a
AM
6334 elf_link_hash_traverse (elf_hash_table (info),
6335 _bfd_elf_export_symbol,
6336 &eif);
6337 if (eif.failed)
6338 return FALSE;
6339 }
6340
e6699019
L
6341 if (soname != NULL)
6342 {
6343 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6344 soname, TRUE);
6345 if (soname_indx == (size_t) -1
6346 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6347 return FALSE;
6348 }
6349 else
6350 soname_indx = (size_t) -1;
6351
5a580b3a 6352 /* Make all global versions with definition. */
fd91d419 6353 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6354 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6355 if (!d->symver && d->literal)
5a580b3a
AM
6356 {
6357 const char *verstr, *name;
6358 size_t namelen, verlen, newlen;
93252b1c 6359 char *newname, *p, leading_char;
5a580b3a
AM
6360 struct elf_link_hash_entry *newh;
6361
93252b1c 6362 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6363 name = d->pattern;
93252b1c 6364 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6365 verstr = t->name;
6366 verlen = strlen (verstr);
6367 newlen = namelen + verlen + 3;
6368
a50b1753 6369 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6370 if (newname == NULL)
6371 return FALSE;
93252b1c
MF
6372 newname[0] = leading_char;
6373 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6374
6375 /* Check the hidden versioned definition. */
6376 p = newname + namelen;
6377 *p++ = ELF_VER_CHR;
6378 memcpy (p, verstr, verlen + 1);
6379 newh = elf_link_hash_lookup (elf_hash_table (info),
6380 newname, FALSE, FALSE,
6381 FALSE);
6382 if (newh == NULL
6383 || (newh->root.type != bfd_link_hash_defined
6384 && newh->root.type != bfd_link_hash_defweak))
6385 {
6386 /* Check the default versioned definition. */
6387 *p++ = ELF_VER_CHR;
6388 memcpy (p, verstr, verlen + 1);
6389 newh = elf_link_hash_lookup (elf_hash_table (info),
6390 newname, FALSE, FALSE,
6391 FALSE);
6392 }
6393 free (newname);
6394
6395 /* Mark this version if there is a definition and it is
6396 not defined in a shared object. */
6397 if (newh != NULL
f5385ebf 6398 && !newh->def_dynamic
5a580b3a
AM
6399 && (newh->root.type == bfd_link_hash_defined
6400 || newh->root.type == bfd_link_hash_defweak))
6401 d->symver = 1;
6402 }
6403
6404 /* Attach all the symbols to their version information. */
5a580b3a 6405 asvinfo.info = info;
5a580b3a
AM
6406 asvinfo.failed = FALSE;
6407
6408 elf_link_hash_traverse (elf_hash_table (info),
6409 _bfd_elf_link_assign_sym_version,
6410 &asvinfo);
6411 if (asvinfo.failed)
6412 return FALSE;
6413
6414 if (!info->allow_undefined_version)
6415 {
6416 /* Check if all global versions have a definition. */
3d13f3e9 6417 bfd_boolean all_defined = TRUE;
fd91d419 6418 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6419 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6420 if (d->literal && !d->symver && !d->script)
5a580b3a 6421 {
4eca0228 6422 _bfd_error_handler
5a580b3a
AM
6423 (_("%s: undefined version: %s"),
6424 d->pattern, t->name);
6425 all_defined = FALSE;
6426 }
6427
6428 if (!all_defined)
6429 {
6430 bfd_set_error (bfd_error_bad_value);
6431 return FALSE;
6432 }
6433 }
6434
902e9fc7
MR
6435 /* Set up the version definition section. */
6436 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6437 BFD_ASSERT (s != NULL);
5a580b3a 6438
902e9fc7
MR
6439 /* We may have created additional version definitions if we are
6440 just linking a regular application. */
6441 verdefs = info->version_info;
5a580b3a 6442
902e9fc7
MR
6443 /* Skip anonymous version tag. */
6444 if (verdefs != NULL && verdefs->vernum == 0)
6445 verdefs = verdefs->next;
5a580b3a 6446
902e9fc7
MR
6447 if (verdefs == NULL && !info->create_default_symver)
6448 s->flags |= SEC_EXCLUDE;
6449 else
5a580b3a 6450 {
902e9fc7
MR
6451 unsigned int cdefs;
6452 bfd_size_type size;
6453 bfd_byte *p;
6454 Elf_Internal_Verdef def;
6455 Elf_Internal_Verdaux defaux;
6456 struct bfd_link_hash_entry *bh;
6457 struct elf_link_hash_entry *h;
6458 const char *name;
5a580b3a 6459
902e9fc7
MR
6460 cdefs = 0;
6461 size = 0;
5a580b3a 6462
902e9fc7
MR
6463 /* Make space for the base version. */
6464 size += sizeof (Elf_External_Verdef);
6465 size += sizeof (Elf_External_Verdaux);
6466 ++cdefs;
6467
6468 /* Make space for the default version. */
6469 if (info->create_default_symver)
6470 {
6471 size += sizeof (Elf_External_Verdef);
6472 ++cdefs;
3e3b46e5
PB
6473 }
6474
5a580b3a
AM
6475 for (t = verdefs; t != NULL; t = t->next)
6476 {
6477 struct bfd_elf_version_deps *n;
6478
a6cc6b3b
RO
6479 /* Don't emit base version twice. */
6480 if (t->vernum == 0)
6481 continue;
6482
5a580b3a
AM
6483 size += sizeof (Elf_External_Verdef);
6484 size += sizeof (Elf_External_Verdaux);
6485 ++cdefs;
6486
6487 for (n = t->deps; n != NULL; n = n->next)
6488 size += sizeof (Elf_External_Verdaux);
6489 }
6490
eea6121a 6491 s->size = size;
a50b1753 6492 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6493 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6494 return FALSE;
6495
6496 /* Fill in the version definition section. */
6497
6498 p = s->contents;
6499
6500 def.vd_version = VER_DEF_CURRENT;
6501 def.vd_flags = VER_FLG_BASE;
6502 def.vd_ndx = 1;
6503 def.vd_cnt = 1;
3e3b46e5
PB
6504 if (info->create_default_symver)
6505 {
6506 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6507 def.vd_next = sizeof (Elf_External_Verdef);
6508 }
6509 else
6510 {
6511 def.vd_aux = sizeof (Elf_External_Verdef);
6512 def.vd_next = (sizeof (Elf_External_Verdef)
6513 + sizeof (Elf_External_Verdaux));
6514 }
5a580b3a 6515
ef53be89 6516 if (soname_indx != (size_t) -1)
5a580b3a
AM
6517 {
6518 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6519 soname_indx);
6520 def.vd_hash = bfd_elf_hash (soname);
6521 defaux.vda_name = soname_indx;
3e3b46e5 6522 name = soname;
5a580b3a
AM
6523 }
6524 else
6525 {
ef53be89 6526 size_t indx;
5a580b3a 6527
06084812 6528 name = lbasename (output_bfd->filename);
5a580b3a
AM
6529 def.vd_hash = bfd_elf_hash (name);
6530 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6531 name, FALSE);
ef53be89 6532 if (indx == (size_t) -1)
5a580b3a
AM
6533 return FALSE;
6534 defaux.vda_name = indx;
6535 }
6536 defaux.vda_next = 0;
6537
6538 _bfd_elf_swap_verdef_out (output_bfd, &def,
6539 (Elf_External_Verdef *) p);
6540 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6541 if (info->create_default_symver)
6542 {
6543 /* Add a symbol representing this version. */
6544 bh = NULL;
6545 if (! (_bfd_generic_link_add_one_symbol
6546 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6547 0, NULL, FALSE,
6548 get_elf_backend_data (dynobj)->collect, &bh)))
6549 return FALSE;
6550 h = (struct elf_link_hash_entry *) bh;
6551 h->non_elf = 0;
6552 h->def_regular = 1;
6553 h->type = STT_OBJECT;
6554 h->verinfo.vertree = NULL;
6555
6556 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6557 return FALSE;
6558
6559 /* Create a duplicate of the base version with the same
6560 aux block, but different flags. */
6561 def.vd_flags = 0;
6562 def.vd_ndx = 2;
6563 def.vd_aux = sizeof (Elf_External_Verdef);
6564 if (verdefs)
6565 def.vd_next = (sizeof (Elf_External_Verdef)
6566 + sizeof (Elf_External_Verdaux));
6567 else
6568 def.vd_next = 0;
6569 _bfd_elf_swap_verdef_out (output_bfd, &def,
6570 (Elf_External_Verdef *) p);
6571 p += sizeof (Elf_External_Verdef);
6572 }
5a580b3a
AM
6573 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6574 (Elf_External_Verdaux *) p);
6575 p += sizeof (Elf_External_Verdaux);
6576
6577 for (t = verdefs; t != NULL; t = t->next)
6578 {
6579 unsigned int cdeps;
6580 struct bfd_elf_version_deps *n;
5a580b3a 6581
a6cc6b3b
RO
6582 /* Don't emit the base version twice. */
6583 if (t->vernum == 0)
6584 continue;
6585
5a580b3a
AM
6586 cdeps = 0;
6587 for (n = t->deps; n != NULL; n = n->next)
6588 ++cdeps;
6589
6590 /* Add a symbol representing this version. */
6591 bh = NULL;
6592 if (! (_bfd_generic_link_add_one_symbol
6593 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6594 0, NULL, FALSE,
6595 get_elf_backend_data (dynobj)->collect, &bh)))
6596 return FALSE;
6597 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6598 h->non_elf = 0;
6599 h->def_regular = 1;
5a580b3a
AM
6600 h->type = STT_OBJECT;
6601 h->verinfo.vertree = t;
6602
c152c796 6603 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6604 return FALSE;
6605
6606 def.vd_version = VER_DEF_CURRENT;
6607 def.vd_flags = 0;
6608 if (t->globals.list == NULL
6609 && t->locals.list == NULL
6610 && ! t->used)
6611 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6612 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6613 def.vd_cnt = cdeps + 1;
6614 def.vd_hash = bfd_elf_hash (t->name);
6615 def.vd_aux = sizeof (Elf_External_Verdef);
6616 def.vd_next = 0;
a6cc6b3b
RO
6617
6618 /* If a basever node is next, it *must* be the last node in
6619 the chain, otherwise Verdef construction breaks. */
6620 if (t->next != NULL && t->next->vernum == 0)
6621 BFD_ASSERT (t->next->next == NULL);
6622
6623 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6624 def.vd_next = (sizeof (Elf_External_Verdef)
6625 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6626
6627 _bfd_elf_swap_verdef_out (output_bfd, &def,
6628 (Elf_External_Verdef *) p);
6629 p += sizeof (Elf_External_Verdef);
6630
6631 defaux.vda_name = h->dynstr_index;
6632 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6633 h->dynstr_index);
6634 defaux.vda_next = 0;
6635 if (t->deps != NULL)
6636 defaux.vda_next = sizeof (Elf_External_Verdaux);
6637 t->name_indx = defaux.vda_name;
6638
6639 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6640 (Elf_External_Verdaux *) p);
6641 p += sizeof (Elf_External_Verdaux);
6642
6643 for (n = t->deps; n != NULL; n = n->next)
6644 {
6645 if (n->version_needed == NULL)
6646 {
6647 /* This can happen if there was an error in the
6648 version script. */
6649 defaux.vda_name = 0;
6650 }
6651 else
6652 {
6653 defaux.vda_name = n->version_needed->name_indx;
6654 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6655 defaux.vda_name);
6656 }
6657 if (n->next == NULL)
6658 defaux.vda_next = 0;
6659 else
6660 defaux.vda_next = sizeof (Elf_External_Verdaux);
6661
6662 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6663 (Elf_External_Verdaux *) p);
6664 p += sizeof (Elf_External_Verdaux);
6665 }
6666 }
6667
5a580b3a
AM
6668 elf_tdata (output_bfd)->cverdefs = cdefs;
6669 }
902e9fc7
MR
6670 }
6671
6672 bed = get_elf_backend_data (output_bfd);
6673
6674 if (info->gc_sections && bed->can_gc_sections)
6675 {
6676 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6677
6678 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6679 dynamic symbol table. */
902e9fc7
MR
6680 sweep_info.info = info;
6681 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6682 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6683 &sweep_info);
3d13f3e9
AM
6684 }
6685
6686 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6687 {
6688 asection *s;
6689 struct elf_find_verdep_info sinfo;
6690
6691 /* Work out the size of the version reference section. */
6692
6693 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6694 BFD_ASSERT (s != NULL);
902e9fc7 6695
3d13f3e9
AM
6696 sinfo.info = info;
6697 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6698 if (sinfo.vers == 0)
6699 sinfo.vers = 1;
6700 sinfo.failed = FALSE;
6701
6702 elf_link_hash_traverse (elf_hash_table (info),
6703 _bfd_elf_link_find_version_dependencies,
6704 &sinfo);
6705 if (sinfo.failed)
6706 return FALSE;
6707
6708 if (elf_tdata (output_bfd)->verref == NULL)
6709 s->flags |= SEC_EXCLUDE;
6710 else
6711 {
6712 Elf_Internal_Verneed *vn;
6713 unsigned int size;
6714 unsigned int crefs;
6715 bfd_byte *p;
6716
6717 /* Build the version dependency section. */
6718 size = 0;
6719 crefs = 0;
6720 for (vn = elf_tdata (output_bfd)->verref;
6721 vn != NULL;
6722 vn = vn->vn_nextref)
6723 {
6724 Elf_Internal_Vernaux *a;
6725
6726 size += sizeof (Elf_External_Verneed);
6727 ++crefs;
6728 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6729 size += sizeof (Elf_External_Vernaux);
6730 }
6731
6732 s->size = size;
6733 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6734 if (s->contents == NULL)
6735 return FALSE;
6736
6737 p = s->contents;
6738 for (vn = elf_tdata (output_bfd)->verref;
6739 vn != NULL;
6740 vn = vn->vn_nextref)
6741 {
6742 unsigned int caux;
6743 Elf_Internal_Vernaux *a;
6744 size_t indx;
6745
6746 caux = 0;
6747 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6748 ++caux;
6749
6750 vn->vn_version = VER_NEED_CURRENT;
6751 vn->vn_cnt = caux;
6752 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6753 elf_dt_name (vn->vn_bfd) != NULL
6754 ? elf_dt_name (vn->vn_bfd)
6755 : lbasename (vn->vn_bfd->filename),
6756 FALSE);
6757 if (indx == (size_t) -1)
6758 return FALSE;
6759 vn->vn_file = indx;
6760 vn->vn_aux = sizeof (Elf_External_Verneed);
6761 if (vn->vn_nextref == NULL)
6762 vn->vn_next = 0;
6763 else
6764 vn->vn_next = (sizeof (Elf_External_Verneed)
6765 + caux * sizeof (Elf_External_Vernaux));
6766
6767 _bfd_elf_swap_verneed_out (output_bfd, vn,
6768 (Elf_External_Verneed *) p);
6769 p += sizeof (Elf_External_Verneed);
6770
6771 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6772 {
6773 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6774 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6775 a->vna_nodename, FALSE);
6776 if (indx == (size_t) -1)
6777 return FALSE;
6778 a->vna_name = indx;
6779 if (a->vna_nextptr == NULL)
6780 a->vna_next = 0;
6781 else
6782 a->vna_next = sizeof (Elf_External_Vernaux);
6783
6784 _bfd_elf_swap_vernaux_out (output_bfd, a,
6785 (Elf_External_Vernaux *) p);
6786 p += sizeof (Elf_External_Vernaux);
6787 }
6788 }
6789
6790 elf_tdata (output_bfd)->cverrefs = crefs;
6791 }
902e9fc7
MR
6792 }
6793
6794 /* Any syms created from now on start with -1 in
6795 got.refcount/offset and plt.refcount/offset. */
6796 elf_hash_table (info)->init_got_refcount
6797 = elf_hash_table (info)->init_got_offset;
6798 elf_hash_table (info)->init_plt_refcount
6799 = elf_hash_table (info)->init_plt_offset;
6800
6801 if (bfd_link_relocatable (info)
6802 && !_bfd_elf_size_group_sections (info))
6803 return FALSE;
6804
6805 /* The backend may have to create some sections regardless of whether
6806 we're dynamic or not. */
6807 if (bed->elf_backend_always_size_sections
6808 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6809 return FALSE;
6810
6811 /* Determine any GNU_STACK segment requirements, after the backend
6812 has had a chance to set a default segment size. */
6813 if (info->execstack)
6814 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6815 else if (info->noexecstack)
6816 elf_stack_flags (output_bfd) = PF_R | PF_W;
6817 else
6818 {
6819 bfd *inputobj;
6820 asection *notesec = NULL;
6821 int exec = 0;
6822
6823 for (inputobj = info->input_bfds;
6824 inputobj;
6825 inputobj = inputobj->link.next)
6826 {
6827 asection *s;
6828
6829 if (inputobj->flags
6830 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6831 continue;
57963c05
AM
6832 s = inputobj->sections;
6833 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6834 continue;
6835
902e9fc7
MR
6836 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6837 if (s)
6838 {
6839 if (s->flags & SEC_CODE)
6840 exec = PF_X;
6841 notesec = s;
6842 }
6843 else if (bed->default_execstack)
6844 exec = PF_X;
6845 }
6846 if (notesec || info->stacksize > 0)
6847 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6848 if (notesec && exec && bfd_link_relocatable (info)
6849 && notesec->output_section != bfd_abs_section_ptr)
6850 notesec->output_section->flags |= SEC_CODE;
6851 }
6852
6853 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6854 {
6855 struct elf_info_failed eif;
6856 struct elf_link_hash_entry *h;
6857 asection *dynstr;
6858 asection *s;
6859
6860 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6861 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6862
902e9fc7
MR
6863 if (info->symbolic)
6864 {
6865 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6866 return FALSE;
6867 info->flags |= DF_SYMBOLIC;
6868 }
6869
6870 if (rpath != NULL)
6871 {
6872 size_t indx;
6873 bfd_vma tag;
6874
6875 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6876 TRUE);
6877 if (indx == (size_t) -1)
6878 return FALSE;
6879
6880 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6881 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6882 return FALSE;
6883 }
6884
6885 if (filter_shlib != NULL)
6886 {
6887 size_t indx;
6888
6889 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6890 filter_shlib, TRUE);
6891 if (indx == (size_t) -1
6892 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6893 return FALSE;
6894 }
6895
6896 if (auxiliary_filters != NULL)
6897 {
6898 const char * const *p;
6899
6900 for (p = auxiliary_filters; *p != NULL; p++)
6901 {
6902 size_t indx;
6903
6904 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6905 *p, TRUE);
6906 if (indx == (size_t) -1
6907 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6908 return FALSE;
6909 }
6910 }
6911
6912 if (audit != NULL)
6913 {
6914 size_t indx;
6915
6916 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6917 TRUE);
6918 if (indx == (size_t) -1
6919 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6920 return FALSE;
6921 }
6922
6923 if (depaudit != NULL)
6924 {
6925 size_t indx;
6926
6927 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6928 TRUE);
6929 if (indx == (size_t) -1
6930 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6931 return FALSE;
6932 }
6933
6934 eif.info = info;
6935 eif.failed = FALSE;
6936
6937 /* Find all symbols which were defined in a dynamic object and make
6938 the backend pick a reasonable value for them. */
6939 elf_link_hash_traverse (elf_hash_table (info),
6940 _bfd_elf_adjust_dynamic_symbol,
6941 &eif);
6942 if (eif.failed)
6943 return FALSE;
6944
6945 /* Add some entries to the .dynamic section. We fill in some of the
6946 values later, in bfd_elf_final_link, but we must add the entries
6947 now so that we know the final size of the .dynamic section. */
6948
6949 /* If there are initialization and/or finalization functions to
6950 call then add the corresponding DT_INIT/DT_FINI entries. */
6951 h = (info->init_function
6952 ? elf_link_hash_lookup (elf_hash_table (info),
6953 info->init_function, FALSE,
6954 FALSE, FALSE)
6955 : NULL);
6956 if (h != NULL
6957 && (h->ref_regular
6958 || h->def_regular))
6959 {
6960 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6961 return FALSE;
6962 }
6963 h = (info->fini_function
6964 ? elf_link_hash_lookup (elf_hash_table (info),
6965 info->fini_function, FALSE,
6966 FALSE, FALSE)
6967 : NULL);
6968 if (h != NULL
6969 && (h->ref_regular
6970 || h->def_regular))
6971 {
6972 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6973 return FALSE;
6974 }
6975
6976 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6977 if (s != NULL && s->linker_has_input)
6978 {
6979 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6980 if (! bfd_link_executable (info))
6981 {
6982 bfd *sub;
6983 asection *o;
6984
57963c05
AM
6985 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6986 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6987 && (o = sub->sections) != NULL
6988 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6989 for (o = sub->sections; o != NULL; o = o->next)
6990 if (elf_section_data (o)->this_hdr.sh_type
6991 == SHT_PREINIT_ARRAY)
6992 {
6993 _bfd_error_handler
871b3ab2 6994 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6995 sub);
6996 break;
6997 }
6998
6999 bfd_set_error (bfd_error_nonrepresentable_section);
7000 return FALSE;
7001 }
7002
7003 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7004 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7005 return FALSE;
7006 }
7007 s = bfd_get_section_by_name (output_bfd, ".init_array");
7008 if (s != NULL && s->linker_has_input)
7009 {
7010 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7011 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7012 return FALSE;
7013 }
7014 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7015 if (s != NULL && s->linker_has_input)
7016 {
7017 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7018 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7019 return FALSE;
7020 }
7021
7022 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7023 /* If .dynstr is excluded from the link, we don't want any of
7024 these tags. Strictly, we should be checking each section
7025 individually; This quick check covers for the case where
7026 someone does a /DISCARD/ : { *(*) }. */
7027 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7028 {
7029 bfd_size_type strsize;
7030
7031 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7032 if ((info->emit_hash
7033 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7034 || (info->emit_gnu_hash
f16a9783
MS
7035 && (bed->record_xhash_symbol == NULL
7036 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7037 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7038 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7039 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7040 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7041 bed->s->sizeof_sym))
7042 return FALSE;
7043 }
7044 }
7045
7046 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7047 return FALSE;
7048
7049 /* The backend must work out the sizes of all the other dynamic
7050 sections. */
7051 if (dynobj != NULL
7052 && bed->elf_backend_size_dynamic_sections != NULL
7053 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7054 return FALSE;
7055
7056 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7057 {
902e9fc7
MR
7058 if (elf_tdata (output_bfd)->cverdefs)
7059 {
7060 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7061
7062 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7063 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7064 return FALSE;
7065 }
7066
7067 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7068 {
7069 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7070 return FALSE;
7071 }
7072 else if (info->flags & DF_BIND_NOW)
7073 {
7074 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7075 return FALSE;
7076 }
7077
7078 if (info->flags_1)
7079 {
7080 if (bfd_link_executable (info))
7081 info->flags_1 &= ~ (DF_1_INITFIRST
7082 | DF_1_NODELETE
7083 | DF_1_NOOPEN);
7084 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7085 return FALSE;
7086 }
7087
7088 if (elf_tdata (output_bfd)->cverrefs)
7089 {
7090 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7091
7092 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7093 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7094 return FALSE;
7095 }
5a580b3a 7096
8423293d
AM
7097 if ((elf_tdata (output_bfd)->cverrefs == 0
7098 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7099 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7100 {
902e9fc7
MR
7101 asection *s;
7102
3d4d4302 7103 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7104 s->flags |= SEC_EXCLUDE;
7105 }
7106 }
7107 return TRUE;
7108}
7109
74541ad4
AM
7110/* Find the first non-excluded output section. We'll use its
7111 section symbol for some emitted relocs. */
7112void
7113_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7114{
7115 asection *s;
f26a3287 7116 asection *found = NULL;
74541ad4
AM
7117
7118 for (s = output_bfd->sections; s != NULL; s = s->next)
7119 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7120 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7121 {
f26a3287
AM
7122 found = s;
7123 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7124 break;
74541ad4 7125 }
f26a3287 7126 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7127}
7128
7129/* Find two non-excluded output sections, one for code, one for data.
7130 We'll use their section symbols for some emitted relocs. */
7131void
7132_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7133{
7134 asection *s;
f26a3287 7135 asection *found = NULL;
74541ad4 7136
266b05cf 7137 /* Data first, since setting text_index_section changes
7f923b7f 7138 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7139 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7140 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7141 && !(s->flags & SEC_READONLY)
d00dd7dc 7142 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7143 {
f26a3287
AM
7144 found = s;
7145 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7146 break;
74541ad4 7147 }
f26a3287 7148 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7149
7150 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7151 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7152 && (s->flags & SEC_READONLY)
d00dd7dc 7153 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7154 {
f26a3287 7155 found = s;
74541ad4
AM
7156 break;
7157 }
f26a3287 7158 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7159}
7160
f16a9783
MS
7161#define GNU_HASH_SECTION_NAME(bed) \
7162 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7163
8423293d
AM
7164bfd_boolean
7165bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7166{
74541ad4 7167 const struct elf_backend_data *bed;
23ec1e32 7168 unsigned long section_sym_count;
96d01d93 7169 bfd_size_type dynsymcount = 0;
74541ad4 7170
8423293d
AM
7171 if (!is_elf_hash_table (info->hash))
7172 return TRUE;
7173
74541ad4
AM
7174 bed = get_elf_backend_data (output_bfd);
7175 (*bed->elf_backend_init_index_section) (output_bfd, info);
7176
23ec1e32
MR
7177 /* Assign dynsym indices. In a shared library we generate a section
7178 symbol for each output section, which come first. Next come all
7179 of the back-end allocated local dynamic syms, followed by the rest
7180 of the global symbols.
7181
7182 This is usually not needed for static binaries, however backends
7183 can request to always do it, e.g. the MIPS backend uses dynamic
7184 symbol counts to lay out GOT, which will be produced in the
7185 presence of GOT relocations even in static binaries (holding fixed
7186 data in that case, to satisfy those relocations). */
7187
7188 if (elf_hash_table (info)->dynamic_sections_created
7189 || bed->always_renumber_dynsyms)
7190 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7191 &section_sym_count);
7192
8423293d
AM
7193 if (elf_hash_table (info)->dynamic_sections_created)
7194 {
7195 bfd *dynobj;
8423293d 7196 asection *s;
8423293d
AM
7197 unsigned int dtagcount;
7198
7199 dynobj = elf_hash_table (info)->dynobj;
7200
5a580b3a 7201 /* Work out the size of the symbol version section. */
3d4d4302 7202 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7203 BFD_ASSERT (s != NULL);
d5486c43 7204 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7205 {
eea6121a 7206 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7207 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7208 if (s->contents == NULL)
7209 return FALSE;
7210
7211 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7212 return FALSE;
7213 }
7214
7215 /* Set the size of the .dynsym and .hash sections. We counted
7216 the number of dynamic symbols in elf_link_add_object_symbols.
7217 We will build the contents of .dynsym and .hash when we build
7218 the final symbol table, because until then we do not know the
7219 correct value to give the symbols. We built the .dynstr
7220 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7221 s = elf_hash_table (info)->dynsym;
5a580b3a 7222 BFD_ASSERT (s != NULL);
eea6121a 7223 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7224
d5486c43
L
7225 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7226 if (s->contents == NULL)
7227 return FALSE;
5a580b3a 7228
d5486c43
L
7229 /* The first entry in .dynsym is a dummy symbol. Clear all the
7230 section syms, in case we don't output them all. */
7231 ++section_sym_count;
7232 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7233
fdc90cb4
JJ
7234 elf_hash_table (info)->bucketcount = 0;
7235
5a580b3a
AM
7236 /* Compute the size of the hashing table. As a side effect this
7237 computes the hash values for all the names we export. */
fdc90cb4
JJ
7238 if (info->emit_hash)
7239 {
7240 unsigned long int *hashcodes;
14b1c01e 7241 struct hash_codes_info hashinf;
fdc90cb4
JJ
7242 bfd_size_type amt;
7243 unsigned long int nsyms;
7244 size_t bucketcount;
7245 size_t hash_entry_size;
7246
7247 /* Compute the hash values for all exported symbols. At the same
7248 time store the values in an array so that we could use them for
7249 optimizations. */
7250 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7251 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7252 if (hashcodes == NULL)
7253 return FALSE;
14b1c01e
AM
7254 hashinf.hashcodes = hashcodes;
7255 hashinf.error = FALSE;
5a580b3a 7256
fdc90cb4
JJ
7257 /* Put all hash values in HASHCODES. */
7258 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7259 elf_collect_hash_codes, &hashinf);
7260 if (hashinf.error)
4dd07732
AM
7261 {
7262 free (hashcodes);
7263 return FALSE;
7264 }
5a580b3a 7265
14b1c01e 7266 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7267 bucketcount
7268 = compute_bucket_count (info, hashcodes, nsyms, 0);
7269 free (hashcodes);
7270
4b48e2f6 7271 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7272 return FALSE;
5a580b3a 7273
fdc90cb4
JJ
7274 elf_hash_table (info)->bucketcount = bucketcount;
7275
3d4d4302 7276 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7277 BFD_ASSERT (s != NULL);
7278 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7279 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7280 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7281 if (s->contents == NULL)
7282 return FALSE;
7283
7284 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7285 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7286 s->contents + hash_entry_size);
7287 }
7288
7289 if (info->emit_gnu_hash)
7290 {
7291 size_t i, cnt;
7292 unsigned char *contents;
7293 struct collect_gnu_hash_codes cinfo;
7294 bfd_size_type amt;
7295 size_t bucketcount;
7296
7297 memset (&cinfo, 0, sizeof (cinfo));
7298
7299 /* Compute the hash values for all exported symbols. At the same
7300 time store the values in an array so that we could use them for
7301 optimizations. */
7302 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7303 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7304 if (cinfo.hashcodes == NULL)
7305 return FALSE;
7306
7307 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7308 cinfo.min_dynindx = -1;
7309 cinfo.output_bfd = output_bfd;
7310 cinfo.bed = bed;
7311
7312 /* Put all hash values in HASHCODES. */
7313 elf_link_hash_traverse (elf_hash_table (info),
7314 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7315 if (cinfo.error)
4dd07732
AM
7316 {
7317 free (cinfo.hashcodes);
7318 return FALSE;
7319 }
fdc90cb4
JJ
7320
7321 bucketcount
7322 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7323
7324 if (bucketcount == 0)
7325 {
7326 free (cinfo.hashcodes);
7327 return FALSE;
7328 }
7329
f16a9783 7330 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7331 BFD_ASSERT (s != NULL);
7332
7333 if (cinfo.nsyms == 0)
7334 {
f16a9783 7335 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7336 BFD_ASSERT (cinfo.min_dynindx == -1);
7337 free (cinfo.hashcodes);
7338 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7339 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7340 if (contents == NULL)
7341 return FALSE;
7342 s->contents = contents;
7343 /* 1 empty bucket. */
7344 bfd_put_32 (output_bfd, 1, contents);
7345 /* SYMIDX above the special symbol 0. */
7346 bfd_put_32 (output_bfd, 1, contents + 4);
7347 /* Just one word for bitmask. */
7348 bfd_put_32 (output_bfd, 1, contents + 8);
7349 /* Only hash fn bloom filter. */
7350 bfd_put_32 (output_bfd, 0, contents + 12);
7351 /* No hashes are valid - empty bitmask. */
7352 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7353 /* No hashes in the only bucket. */
7354 bfd_put_32 (output_bfd, 0,
7355 contents + 16 + bed->s->arch_size / 8);
7356 }
7357 else
7358 {
9e6619e2 7359 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7360 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7361
9e6619e2
AM
7362 x = cinfo.nsyms;
7363 maskbitslog2 = 1;
7364 while ((x >>= 1) != 0)
7365 ++maskbitslog2;
fdc90cb4
JJ
7366 if (maskbitslog2 < 3)
7367 maskbitslog2 = 5;
7368 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7369 maskbitslog2 = maskbitslog2 + 3;
7370 else
7371 maskbitslog2 = maskbitslog2 + 2;
7372 if (bed->s->arch_size == 64)
7373 {
7374 if (maskbitslog2 == 5)
7375 maskbitslog2 = 6;
7376 cinfo.shift1 = 6;
7377 }
7378 else
7379 cinfo.shift1 = 5;
7380 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7381 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7382 cinfo.maskbits = 1 << maskbitslog2;
7383 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7384 amt = bucketcount * sizeof (unsigned long int) * 2;
7385 amt += maskwords * sizeof (bfd_vma);
a50b1753 7386 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7387 if (cinfo.bitmask == NULL)
7388 {
7389 free (cinfo.hashcodes);
7390 return FALSE;
7391 }
7392
a50b1753 7393 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7394 cinfo.indx = cinfo.counts + bucketcount;
7395 cinfo.symindx = dynsymcount - cinfo.nsyms;
7396 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7397
7398 /* Determine how often each hash bucket is used. */
7399 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7400 for (i = 0; i < cinfo.nsyms; ++i)
7401 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7402
7403 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7404 if (cinfo.counts[i] != 0)
7405 {
7406 cinfo.indx[i] = cnt;
7407 cnt += cinfo.counts[i];
7408 }
7409 BFD_ASSERT (cnt == dynsymcount);
7410 cinfo.bucketcount = bucketcount;
7411 cinfo.local_indx = cinfo.min_dynindx;
7412
7413 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7414 s->size += cinfo.maskbits / 8;
f16a9783
MS
7415 if (bed->record_xhash_symbol != NULL)
7416 s->size += cinfo.nsyms * 4;
a50b1753 7417 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7418 if (contents == NULL)
7419 {
7420 free (cinfo.bitmask);
7421 free (cinfo.hashcodes);
7422 return FALSE;
7423 }
7424
7425 s->contents = contents;
7426 bfd_put_32 (output_bfd, bucketcount, contents);
7427 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7428 bfd_put_32 (output_bfd, maskwords, contents + 8);
7429 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7430 contents += 16 + cinfo.maskbits / 8;
7431
7432 for (i = 0; i < bucketcount; ++i)
7433 {
7434 if (cinfo.counts[i] == 0)
7435 bfd_put_32 (output_bfd, 0, contents);
7436 else
7437 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7438 contents += 4;
7439 }
7440
7441 cinfo.contents = contents;
7442
f16a9783
MS
7443 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7444 /* Renumber dynamic symbols, if populating .gnu.hash section.
7445 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7446 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7447 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7448
7449 contents = s->contents + 16;
7450 for (i = 0; i < maskwords; ++i)
7451 {
7452 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7453 contents);
7454 contents += bed->s->arch_size / 8;
7455 }
7456
7457 free (cinfo.bitmask);
7458 free (cinfo.hashcodes);
7459 }
7460 }
5a580b3a 7461
3d4d4302 7462 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7463 BFD_ASSERT (s != NULL);
7464
4ad4eba5 7465 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7466
eea6121a 7467 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7468
7469 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7470 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7471 return FALSE;
7472 }
7473
7474 return TRUE;
7475}
4d269e42 7476\f
4d269e42
AM
7477/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7478
7479static void
7480merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7481 asection *sec)
7482{
dbaa2011
AM
7483 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7484 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7485}
7486
7487/* Finish SHF_MERGE section merging. */
7488
7489bfd_boolean
630993ec 7490_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7491{
7492 bfd *ibfd;
7493 asection *sec;
7494
7495 if (!is_elf_hash_table (info->hash))
7496 return FALSE;
7497
c72f2fb2 7498 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7499 if ((ibfd->flags & DYNAMIC) == 0
7500 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7501 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7502 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7503 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7504 if ((sec->flags & SEC_MERGE) != 0
7505 && !bfd_is_abs_section (sec->output_section))
7506 {
7507 struct bfd_elf_section_data *secdata;
7508
7509 secdata = elf_section_data (sec);
630993ec 7510 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7511 &elf_hash_table (info)->merge_info,
7512 sec, &secdata->sec_info))
7513 return FALSE;
7514 else if (secdata->sec_info)
dbaa2011 7515 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7516 }
7517
7518 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7519 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7520 merge_sections_remove_hook);
7521 return TRUE;
7522}
7523
7524/* Create an entry in an ELF linker hash table. */
7525
7526struct bfd_hash_entry *
7527_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7528 struct bfd_hash_table *table,
7529 const char *string)
7530{
7531 /* Allocate the structure if it has not already been allocated by a
7532 subclass. */
7533 if (entry == NULL)
7534 {
a50b1753 7535 entry = (struct bfd_hash_entry *)
ca4be51c 7536 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7537 if (entry == NULL)
7538 return entry;
7539 }
7540
7541 /* Call the allocation method of the superclass. */
7542 entry = _bfd_link_hash_newfunc (entry, table, string);
7543 if (entry != NULL)
7544 {
7545 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7546 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7547
7548 /* Set local fields. */
7549 ret->indx = -1;
7550 ret->dynindx = -1;
7551 ret->got = htab->init_got_refcount;
7552 ret->plt = htab->init_plt_refcount;
7553 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7554 - offsetof (struct elf_link_hash_entry, size)));
7555 /* Assume that we have been called by a non-ELF symbol reader.
7556 This flag is then reset by the code which reads an ELF input
7557 file. This ensures that a symbol created by a non-ELF symbol
7558 reader will have the flag set correctly. */
7559 ret->non_elf = 1;
7560 }
7561
7562 return entry;
7563}
7564
7565/* Copy data from an indirect symbol to its direct symbol, hiding the
7566 old indirect symbol. Also used for copying flags to a weakdef. */
7567
7568void
7569_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7570 struct elf_link_hash_entry *dir,
7571 struct elf_link_hash_entry *ind)
7572{
7573 struct elf_link_hash_table *htab;
7574
7575 /* Copy down any references that we may have already seen to the
e81830c5 7576 symbol which just became indirect. */
4d269e42 7577
422f1182 7578 if (dir->versioned != versioned_hidden)
e81830c5
AM
7579 dir->ref_dynamic |= ind->ref_dynamic;
7580 dir->ref_regular |= ind->ref_regular;
7581 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7582 dir->non_got_ref |= ind->non_got_ref;
7583 dir->needs_plt |= ind->needs_plt;
7584 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7585
7586 if (ind->root.type != bfd_link_hash_indirect)
7587 return;
7588
7589 /* Copy over the global and procedure linkage table refcount entries.
7590 These may have been already set up by a check_relocs routine. */
7591 htab = elf_hash_table (info);
7592 if (ind->got.refcount > htab->init_got_refcount.refcount)
7593 {
7594 if (dir->got.refcount < 0)
7595 dir->got.refcount = 0;
7596 dir->got.refcount += ind->got.refcount;
7597 ind->got.refcount = htab->init_got_refcount.refcount;
7598 }
7599
7600 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7601 {
7602 if (dir->plt.refcount < 0)
7603 dir->plt.refcount = 0;
7604 dir->plt.refcount += ind->plt.refcount;
7605 ind->plt.refcount = htab->init_plt_refcount.refcount;
7606 }
7607
7608 if (ind->dynindx != -1)
7609 {
7610 if (dir->dynindx != -1)
7611 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7612 dir->dynindx = ind->dynindx;
7613 dir->dynstr_index = ind->dynstr_index;
7614 ind->dynindx = -1;
7615 ind->dynstr_index = 0;
7616 }
7617}
7618
7619void
7620_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7621 struct elf_link_hash_entry *h,
7622 bfd_boolean force_local)
7623{
3aa14d16
L
7624 /* STT_GNU_IFUNC symbol must go through PLT. */
7625 if (h->type != STT_GNU_IFUNC)
7626 {
7627 h->plt = elf_hash_table (info)->init_plt_offset;
7628 h->needs_plt = 0;
7629 }
4d269e42
AM
7630 if (force_local)
7631 {
7632 h->forced_local = 1;
7633 if (h->dynindx != -1)
7634 {
4d269e42
AM
7635 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7636 h->dynstr_index);
641338d8
AM
7637 h->dynindx = -1;
7638 h->dynstr_index = 0;
4d269e42
AM
7639 }
7640 }
7641}
7642
34a87bb0
L
7643/* Hide a symbol. */
7644
7645void
7646_bfd_elf_link_hide_symbol (bfd *output_bfd,
7647 struct bfd_link_info *info,
7648 struct bfd_link_hash_entry *h)
7649{
7650 if (is_elf_hash_table (info->hash))
7651 {
7652 const struct elf_backend_data *bed
7653 = get_elf_backend_data (output_bfd);
7654 struct elf_link_hash_entry *eh
7655 = (struct elf_link_hash_entry *) h;
7656 bed->elf_backend_hide_symbol (info, eh, TRUE);
7657 eh->def_dynamic = 0;
7658 eh->ref_dynamic = 0;
7659 eh->dynamic_def = 0;
7660 }
7661}
7662
7bf52ea2
AM
7663/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7664 caller. */
4d269e42
AM
7665
7666bfd_boolean
7667_bfd_elf_link_hash_table_init
7668 (struct elf_link_hash_table *table,
7669 bfd *abfd,
7670 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7671 struct bfd_hash_table *,
7672 const char *),
4dfe6ac6
NC
7673 unsigned int entsize,
7674 enum elf_target_id target_id)
4d269e42
AM
7675{
7676 bfd_boolean ret;
7677 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7678
4d269e42
AM
7679 table->init_got_refcount.refcount = can_refcount - 1;
7680 table->init_plt_refcount.refcount = can_refcount - 1;
7681 table->init_got_offset.offset = -(bfd_vma) 1;
7682 table->init_plt_offset.offset = -(bfd_vma) 1;
7683 /* The first dynamic symbol is a dummy. */
7684 table->dynsymcount = 1;
7685
7686 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7687
4d269e42 7688 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7689 table->hash_table_id = target_id;
4d269e42
AM
7690
7691 return ret;
7692}
7693
7694/* Create an ELF linker hash table. */
7695
7696struct bfd_link_hash_table *
7697_bfd_elf_link_hash_table_create (bfd *abfd)
7698{
7699 struct elf_link_hash_table *ret;
986f0783 7700 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7701
7bf52ea2 7702 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7703 if (ret == NULL)
7704 return NULL;
7705
7706 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7707 sizeof (struct elf_link_hash_entry),
7708 GENERIC_ELF_DATA))
4d269e42
AM
7709 {
7710 free (ret);
7711 return NULL;
7712 }
d495ab0d 7713 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7714
7715 return &ret->root;
7716}
7717
9f7c3e5e
AM
7718/* Destroy an ELF linker hash table. */
7719
7720void
d495ab0d 7721_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7722{
d495ab0d
AM
7723 struct elf_link_hash_table *htab;
7724
7725 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7726 if (htab->dynstr != NULL)
7727 _bfd_elf_strtab_free (htab->dynstr);
7728 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7729 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7730}
7731
4d269e42
AM
7732/* This is a hook for the ELF emulation code in the generic linker to
7733 tell the backend linker what file name to use for the DT_NEEDED
7734 entry for a dynamic object. */
7735
7736void
7737bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7738{
7739 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7740 && bfd_get_format (abfd) == bfd_object)
7741 elf_dt_name (abfd) = name;
7742}
7743
7744int
7745bfd_elf_get_dyn_lib_class (bfd *abfd)
7746{
7747 int lib_class;
7748 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7749 && bfd_get_format (abfd) == bfd_object)
7750 lib_class = elf_dyn_lib_class (abfd);
7751 else
7752 lib_class = 0;
7753 return lib_class;
7754}
7755
7756void
7757bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7758{
7759 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7760 && bfd_get_format (abfd) == bfd_object)
7761 elf_dyn_lib_class (abfd) = lib_class;
7762}
7763
7764/* Get the list of DT_NEEDED entries for a link. This is a hook for
7765 the linker ELF emulation code. */
7766
7767struct bfd_link_needed_list *
7768bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7769 struct bfd_link_info *info)
7770{
7771 if (! is_elf_hash_table (info->hash))
7772 return NULL;
7773 return elf_hash_table (info)->needed;
7774}
7775
7776/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7777 hook for the linker ELF emulation code. */
7778
7779struct bfd_link_needed_list *
7780bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7781 struct bfd_link_info *info)
7782{
7783 if (! is_elf_hash_table (info->hash))
7784 return NULL;
7785 return elf_hash_table (info)->runpath;
7786}
7787
7788/* Get the name actually used for a dynamic object for a link. This
7789 is the SONAME entry if there is one. Otherwise, it is the string
7790 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7791
7792const char *
7793bfd_elf_get_dt_soname (bfd *abfd)
7794{
7795 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7796 && bfd_get_format (abfd) == bfd_object)
7797 return elf_dt_name (abfd);
7798 return NULL;
7799}
7800
7801/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7802 the ELF linker emulation code. */
7803
7804bfd_boolean
7805bfd_elf_get_bfd_needed_list (bfd *abfd,
7806 struct bfd_link_needed_list **pneeded)
7807{
7808 asection *s;
7809 bfd_byte *dynbuf = NULL;
cb33740c 7810 unsigned int elfsec;
4d269e42
AM
7811 unsigned long shlink;
7812 bfd_byte *extdyn, *extdynend;
7813 size_t extdynsize;
7814 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7815
7816 *pneeded = NULL;
7817
7818 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7819 || bfd_get_format (abfd) != bfd_object)
7820 return TRUE;
7821
7822 s = bfd_get_section_by_name (abfd, ".dynamic");
7823 if (s == NULL || s->size == 0)
7824 return TRUE;
7825
7826 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7827 goto error_return;
7828
7829 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7830 if (elfsec == SHN_BAD)
4d269e42
AM
7831 goto error_return;
7832
7833 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7834
4d269e42
AM
7835 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7836 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7837
7838 extdyn = dynbuf;
7839 extdynend = extdyn + s->size;
7840 for (; extdyn < extdynend; extdyn += extdynsize)
7841 {
7842 Elf_Internal_Dyn dyn;
7843
7844 (*swap_dyn_in) (abfd, extdyn, &dyn);
7845
7846 if (dyn.d_tag == DT_NULL)
7847 break;
7848
7849 if (dyn.d_tag == DT_NEEDED)
7850 {
7851 const char *string;
7852 struct bfd_link_needed_list *l;
7853 unsigned int tagv = dyn.d_un.d_val;
986f0783 7854 size_t amt;
4d269e42
AM
7855
7856 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7857 if (string == NULL)
7858 goto error_return;
7859
7860 amt = sizeof *l;
a50b1753 7861 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7862 if (l == NULL)
7863 goto error_return;
7864
7865 l->by = abfd;
7866 l->name = string;
7867 l->next = *pneeded;
7868 *pneeded = l;
7869 }
7870 }
7871
7872 free (dynbuf);
7873
7874 return TRUE;
7875
7876 error_return:
7877 if (dynbuf != NULL)
7878 free (dynbuf);
7879 return FALSE;
7880}
7881
7882struct elf_symbuf_symbol
7883{
7884 unsigned long st_name; /* Symbol name, index in string tbl */
7885 unsigned char st_info; /* Type and binding attributes */
7886 unsigned char st_other; /* Visibilty, and target specific */
7887};
7888
7889struct elf_symbuf_head
7890{
7891 struct elf_symbuf_symbol *ssym;
ef53be89 7892 size_t count;
4d269e42
AM
7893 unsigned int st_shndx;
7894};
7895
7896struct elf_symbol
7897{
7898 union
7899 {
7900 Elf_Internal_Sym *isym;
7901 struct elf_symbuf_symbol *ssym;
dcea6a95 7902 void *p;
4d269e42
AM
7903 } u;
7904 const char *name;
7905};
7906
7907/* Sort references to symbols by ascending section number. */
7908
7909static int
7910elf_sort_elf_symbol (const void *arg1, const void *arg2)
7911{
7912 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7913 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7914
dcea6a95
AM
7915 if (s1->st_shndx != s2->st_shndx)
7916 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7917 /* Final sort by the address of the sym in the symbuf ensures
7918 a stable sort. */
7919 if (s1 != s2)
7920 return s1 > s2 ? 1 : -1;
7921 return 0;
4d269e42
AM
7922}
7923
7924static int
7925elf_sym_name_compare (const void *arg1, const void *arg2)
7926{
7927 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7928 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
7929 int ret = strcmp (s1->name, s2->name);
7930 if (ret != 0)
7931 return ret;
7932 if (s1->u.p != s2->u.p)
7933 return s1->u.p > s2->u.p ? 1 : -1;
7934 return 0;
4d269e42
AM
7935}
7936
7937static struct elf_symbuf_head *
ef53be89 7938elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7939{
14b1c01e 7940 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7941 struct elf_symbuf_symbol *ssym;
7942 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 7943 size_t i, shndx_count, total_size, amt;
4d269e42 7944
446f7ed5
AM
7945 amt = symcount * sizeof (*indbuf);
7946 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
7947 if (indbuf == NULL)
7948 return NULL;
7949
7950 for (ind = indbuf, i = 0; i < symcount; i++)
7951 if (isymbuf[i].st_shndx != SHN_UNDEF)
7952 *ind++ = &isymbuf[i];
7953 indbufend = ind;
7954
7955 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7956 elf_sort_elf_symbol);
7957
7958 shndx_count = 0;
7959 if (indbufend > indbuf)
7960 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7961 if (ind[0]->st_shndx != ind[1]->st_shndx)
7962 shndx_count++;
7963
3ae181ee
L
7964 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7965 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7966 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7967 if (ssymbuf == NULL)
7968 {
7969 free (indbuf);
7970 return NULL;
7971 }
7972
3ae181ee 7973 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7974 ssymbuf->ssym = NULL;
7975 ssymbuf->count = shndx_count;
7976 ssymbuf->st_shndx = 0;
7977 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7978 {
7979 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7980 {
7981 ssymhead++;
7982 ssymhead->ssym = ssym;
7983 ssymhead->count = 0;
7984 ssymhead->st_shndx = (*ind)->st_shndx;
7985 }
7986 ssym->st_name = (*ind)->st_name;
7987 ssym->st_info = (*ind)->st_info;
7988 ssym->st_other = (*ind)->st_other;
7989 ssymhead->count++;
7990 }
ef53be89 7991 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7992 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7993 == total_size));
4d269e42
AM
7994
7995 free (indbuf);
7996 return ssymbuf;
7997}
7998
7999/* Check if 2 sections define the same set of local and global
8000 symbols. */
8001
8f317e31 8002static bfd_boolean
4d269e42
AM
8003bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8004 struct bfd_link_info *info)
8005{
8006 bfd *bfd1, *bfd2;
8007 const struct elf_backend_data *bed1, *bed2;
8008 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8009 size_t symcount1, symcount2;
4d269e42
AM
8010 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8011 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8012 Elf_Internal_Sym *isym, *isymend;
8013 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8014 size_t count1, count2, i;
cb33740c 8015 unsigned int shndx1, shndx2;
4d269e42
AM
8016 bfd_boolean result;
8017
8018 bfd1 = sec1->owner;
8019 bfd2 = sec2->owner;
8020
4d269e42
AM
8021 /* Both sections have to be in ELF. */
8022 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8023 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8024 return FALSE;
8025
8026 if (elf_section_type (sec1) != elf_section_type (sec2))
8027 return FALSE;
8028
4d269e42
AM
8029 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8030 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8031 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8032 return FALSE;
8033
8034 bed1 = get_elf_backend_data (bfd1);
8035 bed2 = get_elf_backend_data (bfd2);
8036 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8037 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8038 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8039 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8040
8041 if (symcount1 == 0 || symcount2 == 0)
8042 return FALSE;
8043
8044 result = FALSE;
8045 isymbuf1 = NULL;
8046 isymbuf2 = NULL;
a50b1753
NC
8047 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8048 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8049
8050 if (ssymbuf1 == NULL)
8051 {
8052 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8053 NULL, NULL, NULL);
8054 if (isymbuf1 == NULL)
8055 goto done;
8056
8057 if (!info->reduce_memory_overheads)
dcea6a95
AM
8058 {
8059 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8060 elf_tdata (bfd1)->symbuf = ssymbuf1;
8061 }
4d269e42
AM
8062 }
8063
8064 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8065 {
8066 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8067 NULL, NULL, NULL);
8068 if (isymbuf2 == NULL)
8069 goto done;
8070
8071 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8072 {
8073 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8074 elf_tdata (bfd2)->symbuf = ssymbuf2;
8075 }
4d269e42
AM
8076 }
8077
8078 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8079 {
8080 /* Optimized faster version. */
ef53be89 8081 size_t lo, hi, mid;
4d269e42
AM
8082 struct elf_symbol *symp;
8083 struct elf_symbuf_symbol *ssym, *ssymend;
8084
8085 lo = 0;
8086 hi = ssymbuf1->count;
8087 ssymbuf1++;
8088 count1 = 0;
8089 while (lo < hi)
8090 {
8091 mid = (lo + hi) / 2;
cb33740c 8092 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8093 hi = mid;
cb33740c 8094 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8095 lo = mid + 1;
8096 else
8097 {
8098 count1 = ssymbuf1[mid].count;
8099 ssymbuf1 += mid;
8100 break;
8101 }
8102 }
8103
8104 lo = 0;
8105 hi = ssymbuf2->count;
8106 ssymbuf2++;
8107 count2 = 0;
8108 while (lo < hi)
8109 {
8110 mid = (lo + hi) / 2;
cb33740c 8111 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8112 hi = mid;
cb33740c 8113 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8114 lo = mid + 1;
8115 else
8116 {
8117 count2 = ssymbuf2[mid].count;
8118 ssymbuf2 += mid;
8119 break;
8120 }
8121 }
8122
8123 if (count1 == 0 || count2 == 0 || count1 != count2)
8124 goto done;
8125
ca4be51c
AM
8126 symtable1
8127 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8128 symtable2
8129 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8130 if (symtable1 == NULL || symtable2 == NULL)
8131 goto done;
8132
8133 symp = symtable1;
8134 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8135 ssym < ssymend; ssym++, symp++)
8136 {
8137 symp->u.ssym = ssym;
8138 symp->name = bfd_elf_string_from_elf_section (bfd1,
8139 hdr1->sh_link,
8140 ssym->st_name);
8141 }
8142
8143 symp = symtable2;
8144 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8145 ssym < ssymend; ssym++, symp++)
8146 {
8147 symp->u.ssym = ssym;
8148 symp->name = bfd_elf_string_from_elf_section (bfd2,
8149 hdr2->sh_link,
8150 ssym->st_name);
8151 }
8152
8153 /* Sort symbol by name. */
8154 qsort (symtable1, count1, sizeof (struct elf_symbol),
8155 elf_sym_name_compare);
8156 qsort (symtable2, count1, sizeof (struct elf_symbol),
8157 elf_sym_name_compare);
8158
8159 for (i = 0; i < count1; i++)
8160 /* Two symbols must have the same binding, type and name. */
8161 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8162 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8163 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8164 goto done;
8165
8166 result = TRUE;
8167 goto done;
8168 }
8169
a50b1753
NC
8170 symtable1 = (struct elf_symbol *)
8171 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8172 symtable2 = (struct elf_symbol *)
8173 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8174 if (symtable1 == NULL || symtable2 == NULL)
8175 goto done;
8176
8177 /* Count definitions in the section. */
8178 count1 = 0;
8179 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8180 if (isym->st_shndx == shndx1)
4d269e42
AM
8181 symtable1[count1++].u.isym = isym;
8182
8183 count2 = 0;
8184 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8185 if (isym->st_shndx == shndx2)
4d269e42
AM
8186 symtable2[count2++].u.isym = isym;
8187
8188 if (count1 == 0 || count2 == 0 || count1 != count2)
8189 goto done;
8190
8191 for (i = 0; i < count1; i++)
8192 symtable1[i].name
8193 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8194 symtable1[i].u.isym->st_name);
8195
8196 for (i = 0; i < count2; i++)
8197 symtable2[i].name
8198 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8199 symtable2[i].u.isym->st_name);
8200
8201 /* Sort symbol by name. */
8202 qsort (symtable1, count1, sizeof (struct elf_symbol),
8203 elf_sym_name_compare);
8204 qsort (symtable2, count1, sizeof (struct elf_symbol),
8205 elf_sym_name_compare);
8206
8207 for (i = 0; i < count1; i++)
8208 /* Two symbols must have the same binding, type and name. */
8209 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8210 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8211 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8212 goto done;
8213
8214 result = TRUE;
8215
dc1e8a47 8216 done:
4d269e42
AM
8217 if (symtable1)
8218 free (symtable1);
8219 if (symtable2)
8220 free (symtable2);
8221 if (isymbuf1)
8222 free (isymbuf1);
8223 if (isymbuf2)
8224 free (isymbuf2);
8225
8226 return result;
8227}
8228
8229/* Return TRUE if 2 section types are compatible. */
8230
8231bfd_boolean
8232_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8233 bfd *bbfd, const asection *bsec)
8234{
8235 if (asec == NULL
8236 || bsec == NULL
8237 || abfd->xvec->flavour != bfd_target_elf_flavour
8238 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8239 return TRUE;
8240
8241 return elf_section_type (asec) == elf_section_type (bsec);
8242}
8243\f
c152c796
AM
8244/* Final phase of ELF linker. */
8245
8246/* A structure we use to avoid passing large numbers of arguments. */
8247
8248struct elf_final_link_info
8249{
8250 /* General link information. */
8251 struct bfd_link_info *info;
8252 /* Output BFD. */
8253 bfd *output_bfd;
8254 /* Symbol string table. */
ef10c3ac 8255 struct elf_strtab_hash *symstrtab;
c152c796
AM
8256 /* .hash section. */
8257 asection *hash_sec;
8258 /* symbol version section (.gnu.version). */
8259 asection *symver_sec;
8260 /* Buffer large enough to hold contents of any section. */
8261 bfd_byte *contents;
8262 /* Buffer large enough to hold external relocs of any section. */
8263 void *external_relocs;
8264 /* Buffer large enough to hold internal relocs of any section. */
8265 Elf_Internal_Rela *internal_relocs;
8266 /* Buffer large enough to hold external local symbols of any input
8267 BFD. */
8268 bfd_byte *external_syms;
8269 /* And a buffer for symbol section indices. */
8270 Elf_External_Sym_Shndx *locsym_shndx;
8271 /* Buffer large enough to hold internal local symbols of any input
8272 BFD. */
8273 Elf_Internal_Sym *internal_syms;
8274 /* Array large enough to hold a symbol index for each local symbol
8275 of any input BFD. */
8276 long *indices;
8277 /* Array large enough to hold a section pointer for each local
8278 symbol of any input BFD. */
8279 asection **sections;
ef10c3ac 8280 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8281 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8282 /* Number of STT_FILE syms seen. */
8283 size_t filesym_count;
c152c796
AM
8284};
8285
8286/* This struct is used to pass information to elf_link_output_extsym. */
8287
8288struct elf_outext_info
8289{
8290 bfd_boolean failed;
8291 bfd_boolean localsyms;
34a79995 8292 bfd_boolean file_sym_done;
8b127cbc 8293 struct elf_final_link_info *flinfo;
c152c796
AM
8294};
8295
d9352518
DB
8296
8297/* Support for evaluating a complex relocation.
8298
8299 Complex relocations are generalized, self-describing relocations. The
8300 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8301 relocations themselves.
d9352518
DB
8302
8303 The relocations are use a reserved elf-wide relocation type code (R_RELC
8304 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8305 information (start bit, end bit, word width, etc) into the addend. This
8306 information is extracted from CGEN-generated operand tables within gas.
8307
8308 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8309 internal) representing prefix-notation expressions, including but not
8310 limited to those sorts of expressions normally encoded as addends in the
8311 addend field. The symbol mangling format is:
8312
8313 <node> := <literal>
07d6d2b8
AM
8314 | <unary-operator> ':' <node>
8315 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8316 ;
8317
8318 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8319 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8320 | '#' <hexdigits>
8321 ;
8322
8323 <binary-operator> := as in C
8324 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8325
8326static void
a0c8462f
AM
8327set_symbol_value (bfd *bfd_with_globals,
8328 Elf_Internal_Sym *isymbuf,
8329 size_t locsymcount,
8330 size_t symidx,
8331 bfd_vma val)
d9352518 8332{
8977835c
AM
8333 struct elf_link_hash_entry **sym_hashes;
8334 struct elf_link_hash_entry *h;
8335 size_t extsymoff = locsymcount;
d9352518 8336
8977835c 8337 if (symidx < locsymcount)
d9352518 8338 {
8977835c
AM
8339 Elf_Internal_Sym *sym;
8340
8341 sym = isymbuf + symidx;
8342 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8343 {
8344 /* It is a local symbol: move it to the
8345 "absolute" section and give it a value. */
8346 sym->st_shndx = SHN_ABS;
8347 sym->st_value = val;
8348 return;
8349 }
8350 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8351 extsymoff = 0;
d9352518 8352 }
8977835c
AM
8353
8354 /* It is a global symbol: set its link type
8355 to "defined" and give it a value. */
8356
8357 sym_hashes = elf_sym_hashes (bfd_with_globals);
8358 h = sym_hashes [symidx - extsymoff];
8359 while (h->root.type == bfd_link_hash_indirect
8360 || h->root.type == bfd_link_hash_warning)
8361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8362 h->root.type = bfd_link_hash_defined;
8363 h->root.u.def.value = val;
8364 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8365}
8366
a0c8462f
AM
8367static bfd_boolean
8368resolve_symbol (const char *name,
8369 bfd *input_bfd,
8b127cbc 8370 struct elf_final_link_info *flinfo,
a0c8462f
AM
8371 bfd_vma *result,
8372 Elf_Internal_Sym *isymbuf,
8373 size_t locsymcount)
d9352518 8374{
a0c8462f
AM
8375 Elf_Internal_Sym *sym;
8376 struct bfd_link_hash_entry *global_entry;
8377 const char *candidate = NULL;
8378 Elf_Internal_Shdr *symtab_hdr;
8379 size_t i;
8380
d9352518
DB
8381 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8382
8383 for (i = 0; i < locsymcount; ++ i)
8384 {
8977835c 8385 sym = isymbuf + i;
d9352518
DB
8386
8387 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8388 continue;
8389
8390 candidate = bfd_elf_string_from_elf_section (input_bfd,
8391 symtab_hdr->sh_link,
8392 sym->st_name);
8393#ifdef DEBUG
0f02bbd9
AM
8394 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8395 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8396#endif
8397 if (candidate && strcmp (candidate, name) == 0)
8398 {
8b127cbc 8399 asection *sec = flinfo->sections [i];
d9352518 8400
0f02bbd9
AM
8401 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8402 *result += sec->output_offset + sec->output_section->vma;
d9352518 8403#ifdef DEBUG
0f02bbd9
AM
8404 printf ("Found symbol with value %8.8lx\n",
8405 (unsigned long) *result);
d9352518
DB
8406#endif
8407 return TRUE;
8408 }
8409 }
8410
8411 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8412 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8413 FALSE, FALSE, TRUE);
d9352518
DB
8414 if (!global_entry)
8415 return FALSE;
a0c8462f 8416
d9352518
DB
8417 if (global_entry->type == bfd_link_hash_defined
8418 || global_entry->type == bfd_link_hash_defweak)
8419 {
a0c8462f
AM
8420 *result = (global_entry->u.def.value
8421 + global_entry->u.def.section->output_section->vma
8422 + global_entry->u.def.section->output_offset);
d9352518 8423#ifdef DEBUG
0f02bbd9
AM
8424 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8425 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8426#endif
8427 return TRUE;
a0c8462f 8428 }
d9352518 8429
d9352518
DB
8430 return FALSE;
8431}
8432
37b01f6a
DG
8433/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8434 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8435 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8436
d9352518 8437static bfd_boolean
a0c8462f
AM
8438resolve_section (const char *name,
8439 asection *sections,
37b01f6a
DG
8440 bfd_vma *result,
8441 bfd * abfd)
d9352518 8442{
a0c8462f
AM
8443 asection *curr;
8444 unsigned int len;
d9352518 8445
a0c8462f 8446 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8447 if (strcmp (curr->name, name) == 0)
8448 {
8449 *result = curr->vma;
8450 return TRUE;
8451 }
8452
8453 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8454 /* FIXME: This could be coded more efficiently... */
a0c8462f 8455 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8456 {
8457 len = strlen (curr->name);
a0c8462f 8458 if (len > strlen (name))
d9352518
DB
8459 continue;
8460
8461 if (strncmp (curr->name, name, len) == 0)
8462 {
8463 if (strncmp (".end", name + len, 4) == 0)
8464 {
61826503 8465 *result = (curr->vma
bb294208 8466 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8467 return TRUE;
8468 }
8469
8470 /* Insert more pseudo-section names here, if you like. */
8471 }
8472 }
a0c8462f 8473
d9352518
DB
8474 return FALSE;
8475}
8476
8477static void
a0c8462f 8478undefined_reference (const char *reftype, const char *name)
d9352518 8479{
695344c0 8480 /* xgettext:c-format */
a0c8462f
AM
8481 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8482 reftype, name);
d9352518
DB
8483}
8484
8485static bfd_boolean
a0c8462f
AM
8486eval_symbol (bfd_vma *result,
8487 const char **symp,
8488 bfd *input_bfd,
8b127cbc 8489 struct elf_final_link_info *flinfo,
a0c8462f
AM
8490 bfd_vma dot,
8491 Elf_Internal_Sym *isymbuf,
8492 size_t locsymcount,
8493 int signed_p)
d9352518 8494{
4b93929b
NC
8495 size_t len;
8496 size_t symlen;
a0c8462f
AM
8497 bfd_vma a;
8498 bfd_vma b;
4b93929b 8499 char symbuf[4096];
0f02bbd9 8500 const char *sym = *symp;
a0c8462f
AM
8501 const char *symend;
8502 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8503
8504 len = strlen (sym);
8505 symend = sym + len;
8506
4b93929b 8507 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8508 {
8509 bfd_set_error (bfd_error_invalid_operation);
8510 return FALSE;
8511 }
a0c8462f 8512
d9352518
DB
8513 switch (* sym)
8514 {
8515 case '.':
0f02bbd9
AM
8516 *result = dot;
8517 *symp = sym + 1;
d9352518
DB
8518 return TRUE;
8519
8520 case '#':
0f02bbd9
AM
8521 ++sym;
8522 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8523 return TRUE;
8524
8525 case 'S':
8526 symbol_is_section = TRUE;
1a0670f3 8527 /* Fall through. */
a0c8462f 8528 case 's':
0f02bbd9
AM
8529 ++sym;
8530 symlen = strtol (sym, (char **) symp, 10);
8531 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8532
4b93929b 8533 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8534 {
8535 bfd_set_error (bfd_error_invalid_operation);
8536 return FALSE;
8537 }
8538
8539 memcpy (symbuf, sym, symlen);
a0c8462f 8540 symbuf[symlen] = '\0';
0f02bbd9 8541 *symp = sym + symlen;
a0c8462f
AM
8542
8543 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8544 the symbol as a section, or vice-versa. so we're pretty liberal in our
8545 interpretation here; section means "try section first", not "must be a
8546 section", and likewise with symbol. */
8547
a0c8462f 8548 if (symbol_is_section)
d9352518 8549 {
37b01f6a 8550 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8551 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8552 isymbuf, locsymcount))
d9352518
DB
8553 {
8554 undefined_reference ("section", symbuf);
8555 return FALSE;
8556 }
a0c8462f
AM
8557 }
8558 else
d9352518 8559 {
8b127cbc 8560 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8561 isymbuf, locsymcount)
8b127cbc 8562 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8563 result, input_bfd))
d9352518
DB
8564 {
8565 undefined_reference ("symbol", symbuf);
8566 return FALSE;
8567 }
8568 }
8569
8570 return TRUE;
a0c8462f 8571
d9352518
DB
8572 /* All that remains are operators. */
8573
8574#define UNARY_OP(op) \
8575 if (strncmp (sym, #op, strlen (#op)) == 0) \
8576 { \
8577 sym += strlen (#op); \
a0c8462f
AM
8578 if (*sym == ':') \
8579 ++sym; \
0f02bbd9 8580 *symp = sym; \
8b127cbc 8581 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8582 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8583 return FALSE; \
8584 if (signed_p) \
0f02bbd9 8585 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8586 else \
8587 *result = op a; \
d9352518
DB
8588 return TRUE; \
8589 }
8590
8591#define BINARY_OP(op) \
8592 if (strncmp (sym, #op, strlen (#op)) == 0) \
8593 { \
8594 sym += strlen (#op); \
a0c8462f
AM
8595 if (*sym == ':') \
8596 ++sym; \
0f02bbd9 8597 *symp = sym; \
8b127cbc 8598 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8599 isymbuf, locsymcount, signed_p)) \
a0c8462f 8600 return FALSE; \
0f02bbd9 8601 ++*symp; \
8b127cbc 8602 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8603 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8604 return FALSE; \
8605 if (signed_p) \
0f02bbd9 8606 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8607 else \
8608 *result = a op b; \
d9352518
DB
8609 return TRUE; \
8610 }
8611
8612 default:
8613 UNARY_OP (0-);
8614 BINARY_OP (<<);
8615 BINARY_OP (>>);
8616 BINARY_OP (==);
8617 BINARY_OP (!=);
8618 BINARY_OP (<=);
8619 BINARY_OP (>=);
8620 BINARY_OP (&&);
8621 BINARY_OP (||);
8622 UNARY_OP (~);
8623 UNARY_OP (!);
8624 BINARY_OP (*);
8625 BINARY_OP (/);
8626 BINARY_OP (%);
8627 BINARY_OP (^);
8628 BINARY_OP (|);
8629 BINARY_OP (&);
8630 BINARY_OP (+);
8631 BINARY_OP (-);
8632 BINARY_OP (<);
8633 BINARY_OP (>);
8634#undef UNARY_OP
8635#undef BINARY_OP
8636 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8637 bfd_set_error (bfd_error_invalid_operation);
8638 return FALSE;
8639 }
8640}
8641
d9352518 8642static void
a0c8462f
AM
8643put_value (bfd_vma size,
8644 unsigned long chunksz,
8645 bfd *input_bfd,
8646 bfd_vma x,
8647 bfd_byte *location)
d9352518
DB
8648{
8649 location += (size - chunksz);
8650
41cd1ad1 8651 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8652 {
8653 switch (chunksz)
8654 {
d9352518
DB
8655 case 1:
8656 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8657 x >>= 8;
d9352518
DB
8658 break;
8659 case 2:
8660 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8661 x >>= 16;
d9352518
DB
8662 break;
8663 case 4:
8664 bfd_put_32 (input_bfd, x, location);
65164438
NC
8665 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8666 x >>= 16;
8667 x >>= 16;
d9352518 8668 break;
d9352518 8669#ifdef BFD64
41cd1ad1 8670 case 8:
d9352518 8671 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8672 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8673 x >>= 32;
8674 x >>= 32;
8675 break;
d9352518 8676#endif
41cd1ad1
NC
8677 default:
8678 abort ();
d9352518
DB
8679 break;
8680 }
8681 }
8682}
8683
a0c8462f
AM
8684static bfd_vma
8685get_value (bfd_vma size,
8686 unsigned long chunksz,
8687 bfd *input_bfd,
8688 bfd_byte *location)
d9352518 8689{
9b239e0e 8690 int shift;
d9352518
DB
8691 bfd_vma x = 0;
8692
9b239e0e
NC
8693 /* Sanity checks. */
8694 BFD_ASSERT (chunksz <= sizeof (x)
8695 && size >= chunksz
8696 && chunksz != 0
8697 && (size % chunksz) == 0
8698 && input_bfd != NULL
8699 && location != NULL);
8700
8701 if (chunksz == sizeof (x))
8702 {
8703 BFD_ASSERT (size == chunksz);
8704
8705 /* Make sure that we do not perform an undefined shift operation.
8706 We know that size == chunksz so there will only be one iteration
8707 of the loop below. */
8708 shift = 0;
8709 }
8710 else
8711 shift = 8 * chunksz;
8712
a0c8462f 8713 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8714 {
8715 switch (chunksz)
8716 {
d9352518 8717 case 1:
9b239e0e 8718 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8719 break;
8720 case 2:
9b239e0e 8721 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8722 break;
8723 case 4:
9b239e0e 8724 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8725 break;
d9352518 8726#ifdef BFD64
9b239e0e
NC
8727 case 8:
8728 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8729 break;
9b239e0e
NC
8730#endif
8731 default:
8732 abort ();
d9352518
DB
8733 }
8734 }
8735 return x;
8736}
8737
a0c8462f
AM
8738static void
8739decode_complex_addend (unsigned long *start, /* in bits */
8740 unsigned long *oplen, /* in bits */
8741 unsigned long *len, /* in bits */
8742 unsigned long *wordsz, /* in bytes */
8743 unsigned long *chunksz, /* in bytes */
8744 unsigned long *lsb0_p,
8745 unsigned long *signed_p,
8746 unsigned long *trunc_p,
8747 unsigned long encoded)
d9352518 8748{
07d6d2b8
AM
8749 * start = encoded & 0x3F;
8750 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8751 * oplen = (encoded >> 12) & 0x3F;
8752 * wordsz = (encoded >> 18) & 0xF;
8753 * chunksz = (encoded >> 22) & 0xF;
8754 * lsb0_p = (encoded >> 27) & 1;
8755 * signed_p = (encoded >> 28) & 1;
8756 * trunc_p = (encoded >> 29) & 1;
8757}
8758
cdfeee4f 8759bfd_reloc_status_type
0f02bbd9 8760bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8761 asection *input_section,
0f02bbd9
AM
8762 bfd_byte *contents,
8763 Elf_Internal_Rela *rel,
8764 bfd_vma relocation)
d9352518 8765{
0f02bbd9
AM
8766 bfd_vma shift, x, mask;
8767 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8768 bfd_reloc_status_type r;
bb294208 8769 bfd_size_type octets;
d9352518
DB
8770
8771 /* Perform this reloc, since it is complex.
8772 (this is not to say that it necessarily refers to a complex
8773 symbol; merely that it is a self-describing CGEN based reloc.
8774 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8775 word size, etc) encoded within it.). */
d9352518 8776
a0c8462f
AM
8777 decode_complex_addend (&start, &oplen, &len, &wordsz,
8778 &chunksz, &lsb0_p, &signed_p,
8779 &trunc_p, rel->r_addend);
d9352518
DB
8780
8781 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8782
8783 if (lsb0_p)
8784 shift = (start + 1) - len;
8785 else
8786 shift = (8 * wordsz) - (start + len);
8787
bb294208
AM
8788 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8789 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8790
8791#ifdef DEBUG
8792 printf ("Doing complex reloc: "
8793 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8794 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8795 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8796 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8797 oplen, (unsigned long) x, (unsigned long) mask,
8798 (unsigned long) relocation);
d9352518
DB
8799#endif
8800
cdfeee4f 8801 r = bfd_reloc_ok;
d9352518 8802 if (! trunc_p)
cdfeee4f
AM
8803 /* Now do an overflow check. */
8804 r = bfd_check_overflow ((signed_p
8805 ? complain_overflow_signed
8806 : complain_overflow_unsigned),
8807 len, 0, (8 * wordsz),
8808 relocation);
a0c8462f 8809
d9352518
DB
8810 /* Do the deed. */
8811 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8812
8813#ifdef DEBUG
8814 printf (" relocation: %8.8lx\n"
8815 " shifted mask: %8.8lx\n"
8816 " shifted/masked reloc: %8.8lx\n"
8817 " result: %8.8lx\n",
9ccb8af9
AM
8818 (unsigned long) relocation, (unsigned long) (mask << shift),
8819 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8820#endif
bb294208 8821 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 8822 return r;
d9352518
DB
8823}
8824
0e287786
AM
8825/* Functions to read r_offset from external (target order) reloc
8826 entry. Faster than bfd_getl32 et al, because we let the compiler
8827 know the value is aligned. */
53df40a4 8828
0e287786
AM
8829static bfd_vma
8830ext32l_r_offset (const void *p)
53df40a4
AM
8831{
8832 union aligned32
8833 {
8834 uint32_t v;
8835 unsigned char c[4];
8836 };
8837 const union aligned32 *a
0e287786 8838 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8839
8840 uint32_t aval = ( (uint32_t) a->c[0]
8841 | (uint32_t) a->c[1] << 8
8842 | (uint32_t) a->c[2] << 16
8843 | (uint32_t) a->c[3] << 24);
0e287786 8844 return aval;
53df40a4
AM
8845}
8846
0e287786
AM
8847static bfd_vma
8848ext32b_r_offset (const void *p)
53df40a4
AM
8849{
8850 union aligned32
8851 {
8852 uint32_t v;
8853 unsigned char c[4];
8854 };
8855 const union aligned32 *a
0e287786 8856 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8857
8858 uint32_t aval = ( (uint32_t) a->c[0] << 24
8859 | (uint32_t) a->c[1] << 16
8860 | (uint32_t) a->c[2] << 8
8861 | (uint32_t) a->c[3]);
0e287786 8862 return aval;
53df40a4
AM
8863}
8864
8865#ifdef BFD_HOST_64_BIT
0e287786
AM
8866static bfd_vma
8867ext64l_r_offset (const void *p)
53df40a4
AM
8868{
8869 union aligned64
8870 {
8871 uint64_t v;
8872 unsigned char c[8];
8873 };
8874 const union aligned64 *a
0e287786 8875 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8876
8877 uint64_t aval = ( (uint64_t) a->c[0]
8878 | (uint64_t) a->c[1] << 8
8879 | (uint64_t) a->c[2] << 16
8880 | (uint64_t) a->c[3] << 24
8881 | (uint64_t) a->c[4] << 32
8882 | (uint64_t) a->c[5] << 40
8883 | (uint64_t) a->c[6] << 48
8884 | (uint64_t) a->c[7] << 56);
0e287786 8885 return aval;
53df40a4
AM
8886}
8887
0e287786
AM
8888static bfd_vma
8889ext64b_r_offset (const void *p)
53df40a4
AM
8890{
8891 union aligned64
8892 {
8893 uint64_t v;
8894 unsigned char c[8];
8895 };
8896 const union aligned64 *a
0e287786 8897 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8898
8899 uint64_t aval = ( (uint64_t) a->c[0] << 56
8900 | (uint64_t) a->c[1] << 48
8901 | (uint64_t) a->c[2] << 40
8902 | (uint64_t) a->c[3] << 32
8903 | (uint64_t) a->c[4] << 24
8904 | (uint64_t) a->c[5] << 16
8905 | (uint64_t) a->c[6] << 8
8906 | (uint64_t) a->c[7]);
0e287786 8907 return aval;
53df40a4
AM
8908}
8909#endif
8910
c152c796
AM
8911/* When performing a relocatable link, the input relocations are
8912 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8913 referenced must be updated. Update all the relocations found in
8914 RELDATA. */
c152c796 8915
bca6d0e3 8916static bfd_boolean
c152c796 8917elf_link_adjust_relocs (bfd *abfd,
9eaff861 8918 asection *sec,
28dbcedc 8919 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8920 bfd_boolean sort,
8921 struct bfd_link_info *info)
c152c796
AM
8922{
8923 unsigned int i;
8924 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8925 bfd_byte *erela;
8926 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8927 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8928 bfd_vma r_type_mask;
8929 int r_sym_shift;
d4730f92
BS
8930 unsigned int count = reldata->count;
8931 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8932
d4730f92 8933 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8934 {
8935 swap_in = bed->s->swap_reloc_in;
8936 swap_out = bed->s->swap_reloc_out;
8937 }
d4730f92 8938 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8939 {
8940 swap_in = bed->s->swap_reloca_in;
8941 swap_out = bed->s->swap_reloca_out;
8942 }
8943 else
8944 abort ();
8945
8946 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8947 abort ();
8948
8949 if (bed->s->arch_size == 32)
8950 {
8951 r_type_mask = 0xff;
8952 r_sym_shift = 8;
8953 }
8954 else
8955 {
8956 r_type_mask = 0xffffffff;
8957 r_sym_shift = 32;
8958 }
8959
d4730f92
BS
8960 erela = reldata->hdr->contents;
8961 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8962 {
8963 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8964 unsigned int j;
8965
8966 if (*rel_hash == NULL)
8967 continue;
8968
10bbbc1d
NC
8969 if ((*rel_hash)->indx == -2
8970 && info->gc_sections
8971 && ! info->gc_keep_exported)
8972 {
8973 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8974 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8975 abfd, sec,
8976 (*rel_hash)->root.root.string);
9793eb77 8977 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8978 abfd, sec);
10bbbc1d
NC
8979 bfd_set_error (bfd_error_invalid_operation);
8980 return FALSE;
8981 }
c152c796
AM
8982 BFD_ASSERT ((*rel_hash)->indx >= 0);
8983
8984 (*swap_in) (abfd, erela, irela);
8985 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8986 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8987 | (irela[j].r_info & r_type_mask));
8988 (*swap_out) (abfd, irela, erela);
8989 }
53df40a4 8990
9eaff861
AO
8991 if (bed->elf_backend_update_relocs)
8992 (*bed->elf_backend_update_relocs) (sec, reldata);
8993
0e287786 8994 if (sort && count != 0)
53df40a4 8995 {
0e287786
AM
8996 bfd_vma (*ext_r_off) (const void *);
8997 bfd_vma r_off;
8998 size_t elt_size;
8999 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9000 bfd_byte *buf = NULL;
28dbcedc
AM
9001
9002 if (bed->s->arch_size == 32)
9003 {
9004 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9005 ext_r_off = ext32l_r_offset;
28dbcedc 9006 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9007 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9008 else
9009 abort ();
9010 }
53df40a4 9011 else
28dbcedc 9012 {
53df40a4 9013#ifdef BFD_HOST_64_BIT
28dbcedc 9014 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9015 ext_r_off = ext64l_r_offset;
28dbcedc 9016 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9017 ext_r_off = ext64b_r_offset;
28dbcedc 9018 else
53df40a4 9019#endif
28dbcedc
AM
9020 abort ();
9021 }
0e287786 9022
bca6d0e3
AM
9023 /* Must use a stable sort here. A modified insertion sort,
9024 since the relocs are mostly sorted already. */
0e287786
AM
9025 elt_size = reldata->hdr->sh_entsize;
9026 base = reldata->hdr->contents;
9027 end = base + count * elt_size;
bca6d0e3 9028 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9029 abort ();
9030
9031 /* Ensure the first element is lowest. This acts as a sentinel,
9032 speeding the main loop below. */
9033 r_off = (*ext_r_off) (base);
9034 for (p = loc = base; (p += elt_size) < end; )
9035 {
9036 bfd_vma r_off2 = (*ext_r_off) (p);
9037 if (r_off > r_off2)
9038 {
9039 r_off = r_off2;
9040 loc = p;
9041 }
9042 }
9043 if (loc != base)
9044 {
9045 /* Don't just swap *base and *loc as that changes the order
9046 of the original base[0] and base[1] if they happen to
9047 have the same r_offset. */
bca6d0e3
AM
9048 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9049 memcpy (onebuf, loc, elt_size);
0e287786 9050 memmove (base + elt_size, base, loc - base);
bca6d0e3 9051 memcpy (base, onebuf, elt_size);
0e287786
AM
9052 }
9053
b29b8669 9054 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9055 {
9056 /* base to p is sorted, *p is next to insert. */
9057 r_off = (*ext_r_off) (p);
9058 /* Search the sorted region for location to insert. */
9059 loc = p - elt_size;
9060 while (r_off < (*ext_r_off) (loc))
9061 loc -= elt_size;
9062 loc += elt_size;
9063 if (loc != p)
9064 {
bca6d0e3
AM
9065 /* Chances are there is a run of relocs to insert here,
9066 from one of more input files. Files are not always
9067 linked in order due to the way elf_link_input_bfd is
9068 called. See pr17666. */
9069 size_t sortlen = p - loc;
9070 bfd_vma r_off2 = (*ext_r_off) (loc);
9071 size_t runlen = elt_size;
9072 size_t buf_size = 96 * 1024;
9073 while (p + runlen < end
9074 && (sortlen <= buf_size
9075 || runlen + elt_size <= buf_size)
9076 && r_off2 > (*ext_r_off) (p + runlen))
9077 runlen += elt_size;
9078 if (buf == NULL)
9079 {
9080 buf = bfd_malloc (buf_size);
9081 if (buf == NULL)
9082 return FALSE;
9083 }
9084 if (runlen < sortlen)
9085 {
9086 memcpy (buf, p, runlen);
9087 memmove (loc + runlen, loc, sortlen);
9088 memcpy (loc, buf, runlen);
9089 }
9090 else
9091 {
9092 memcpy (buf, loc, sortlen);
9093 memmove (loc, p, runlen);
9094 memcpy (loc + runlen, buf, sortlen);
9095 }
b29b8669 9096 p += runlen - elt_size;
0e287786
AM
9097 }
9098 }
9099 /* Hashes are no longer valid. */
28dbcedc
AM
9100 free (reldata->hashes);
9101 reldata->hashes = NULL;
bca6d0e3 9102 free (buf);
53df40a4 9103 }
bca6d0e3 9104 return TRUE;
c152c796
AM
9105}
9106
9107struct elf_link_sort_rela
9108{
9109 union {
9110 bfd_vma offset;
9111 bfd_vma sym_mask;
9112 } u;
9113 enum elf_reloc_type_class type;
9114 /* We use this as an array of size int_rels_per_ext_rel. */
9115 Elf_Internal_Rela rela[1];
9116};
9117
dcea6a95
AM
9118/* qsort stability here and for cmp2 is only an issue if multiple
9119 dynamic relocations are emitted at the same address. But targets
9120 that apply a series of dynamic relocations each operating on the
9121 result of the prior relocation can't use -z combreloc as
9122 implemented anyway. Such schemes tend to be broken by sorting on
9123 symbol index. That leaves dynamic NONE relocs as the only other
9124 case where ld might emit multiple relocs at the same address, and
9125 those are only emitted due to target bugs. */
9126
c152c796
AM
9127static int
9128elf_link_sort_cmp1 (const void *A, const void *B)
9129{
a50b1753
NC
9130 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9131 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9132 int relativea, relativeb;
9133
9134 relativea = a->type == reloc_class_relative;
9135 relativeb = b->type == reloc_class_relative;
9136
9137 if (relativea < relativeb)
9138 return 1;
9139 if (relativea > relativeb)
9140 return -1;
9141 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9142 return -1;
9143 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9144 return 1;
9145 if (a->rela->r_offset < b->rela->r_offset)
9146 return -1;
9147 if (a->rela->r_offset > b->rela->r_offset)
9148 return 1;
9149 return 0;
9150}
9151
9152static int
9153elf_link_sort_cmp2 (const void *A, const void *B)
9154{
a50b1753
NC
9155 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9156 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9157
7e612e98 9158 if (a->type < b->type)
c152c796 9159 return -1;
7e612e98 9160 if (a->type > b->type)
c152c796 9161 return 1;
7e612e98 9162 if (a->u.offset < b->u.offset)
c152c796 9163 return -1;
7e612e98 9164 if (a->u.offset > b->u.offset)
c152c796
AM
9165 return 1;
9166 if (a->rela->r_offset < b->rela->r_offset)
9167 return -1;
9168 if (a->rela->r_offset > b->rela->r_offset)
9169 return 1;
9170 return 0;
9171}
9172
9173static size_t
9174elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9175{
3410fea8 9176 asection *dynamic_relocs;
fc66a176
L
9177 asection *rela_dyn;
9178 asection *rel_dyn;
c152c796
AM
9179 bfd_size_type count, size;
9180 size_t i, ret, sort_elt, ext_size;
9181 bfd_byte *sort, *s_non_relative, *p;
9182 struct elf_link_sort_rela *sq;
9183 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9184 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9185 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9186 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9187 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9188 struct bfd_link_order *lo;
9189 bfd_vma r_sym_mask;
3410fea8 9190 bfd_boolean use_rela;
c152c796 9191
3410fea8
NC
9192 /* Find a dynamic reloc section. */
9193 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9194 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9195 if (rela_dyn != NULL && rela_dyn->size > 0
9196 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9197 {
3410fea8
NC
9198 bfd_boolean use_rela_initialised = FALSE;
9199
9200 /* This is just here to stop gcc from complaining.
c8e44c6d 9201 Its initialization checking code is not perfect. */
3410fea8
NC
9202 use_rela = TRUE;
9203
9204 /* Both sections are present. Examine the sizes
9205 of the indirect sections to help us choose. */
9206 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9207 if (lo->type == bfd_indirect_link_order)
9208 {
9209 asection *o = lo->u.indirect.section;
9210
9211 if ((o->size % bed->s->sizeof_rela) == 0)
9212 {
9213 if ((o->size % bed->s->sizeof_rel) == 0)
9214 /* Section size is divisible by both rel and rela sizes.
9215 It is of no help to us. */
9216 ;
9217 else
9218 {
9219 /* Section size is only divisible by rela. */
535b785f 9220 if (use_rela_initialised && !use_rela)
3410fea8 9221 {
9793eb77 9222 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9223 "they are in more than one size"),
9224 abfd);
3410fea8
NC
9225 bfd_set_error (bfd_error_invalid_operation);
9226 return 0;
9227 }
9228 else
9229 {
9230 use_rela = TRUE;
9231 use_rela_initialised = TRUE;
9232 }
9233 }
9234 }
9235 else if ((o->size % bed->s->sizeof_rel) == 0)
9236 {
9237 /* Section size is only divisible by rel. */
535b785f 9238 if (use_rela_initialised && use_rela)
3410fea8 9239 {
9793eb77 9240 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9241 "they are in more than one size"),
9242 abfd);
3410fea8
NC
9243 bfd_set_error (bfd_error_invalid_operation);
9244 return 0;
9245 }
9246 else
9247 {
9248 use_rela = FALSE;
9249 use_rela_initialised = TRUE;
9250 }
9251 }
9252 else
9253 {
c8e44c6d
AM
9254 /* The section size is not divisible by either -
9255 something is wrong. */
9793eb77 9256 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9257 "they are of an unknown size"), abfd);
3410fea8
NC
9258 bfd_set_error (bfd_error_invalid_operation);
9259 return 0;
9260 }
9261 }
9262
9263 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9264 if (lo->type == bfd_indirect_link_order)
9265 {
9266 asection *o = lo->u.indirect.section;
9267
9268 if ((o->size % bed->s->sizeof_rela) == 0)
9269 {
9270 if ((o->size % bed->s->sizeof_rel) == 0)
9271 /* Section size is divisible by both rel and rela sizes.
9272 It is of no help to us. */
9273 ;
9274 else
9275 {
9276 /* Section size is only divisible by rela. */
535b785f 9277 if (use_rela_initialised && !use_rela)
3410fea8 9278 {
9793eb77 9279 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9280 "they are in more than one size"),
9281 abfd);
3410fea8
NC
9282 bfd_set_error (bfd_error_invalid_operation);
9283 return 0;
9284 }
9285 else
9286 {
9287 use_rela = TRUE;
9288 use_rela_initialised = TRUE;
9289 }
9290 }
9291 }
9292 else if ((o->size % bed->s->sizeof_rel) == 0)
9293 {
9294 /* Section size is only divisible by rel. */
535b785f 9295 if (use_rela_initialised && use_rela)
3410fea8 9296 {
9793eb77 9297 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9298 "they are in more than one size"),
9299 abfd);
3410fea8
NC
9300 bfd_set_error (bfd_error_invalid_operation);
9301 return 0;
9302 }
9303 else
9304 {
9305 use_rela = FALSE;
9306 use_rela_initialised = TRUE;
9307 }
9308 }
9309 else
9310 {
c8e44c6d
AM
9311 /* The section size is not divisible by either -
9312 something is wrong. */
9793eb77 9313 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9314 "they are of an unknown size"), abfd);
3410fea8
NC
9315 bfd_set_error (bfd_error_invalid_operation);
9316 return 0;
9317 }
9318 }
9319
9320 if (! use_rela_initialised)
9321 /* Make a guess. */
9322 use_rela = TRUE;
c152c796 9323 }
fc66a176
L
9324 else if (rela_dyn != NULL && rela_dyn->size > 0)
9325 use_rela = TRUE;
9326 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9327 use_rela = FALSE;
c152c796 9328 else
fc66a176 9329 return 0;
3410fea8
NC
9330
9331 if (use_rela)
c152c796 9332 {
3410fea8 9333 dynamic_relocs = rela_dyn;
c152c796
AM
9334 ext_size = bed->s->sizeof_rela;
9335 swap_in = bed->s->swap_reloca_in;
9336 swap_out = bed->s->swap_reloca_out;
9337 }
3410fea8
NC
9338 else
9339 {
9340 dynamic_relocs = rel_dyn;
9341 ext_size = bed->s->sizeof_rel;
9342 swap_in = bed->s->swap_reloc_in;
9343 swap_out = bed->s->swap_reloc_out;
9344 }
c152c796
AM
9345
9346 size = 0;
3410fea8 9347 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9348 if (lo->type == bfd_indirect_link_order)
3410fea8 9349 size += lo->u.indirect.section->size;
c152c796 9350
3410fea8 9351 if (size != dynamic_relocs->size)
c152c796
AM
9352 return 0;
9353
9354 sort_elt = (sizeof (struct elf_link_sort_rela)
9355 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9356
9357 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9358 if (count == 0)
9359 return 0;
a50b1753 9360 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9361
c152c796
AM
9362 if (sort == NULL)
9363 {
9364 (*info->callbacks->warning)
9793eb77 9365 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9366 return 0;
9367 }
9368
9369 if (bed->s->arch_size == 32)
9370 r_sym_mask = ~(bfd_vma) 0xff;
9371 else
9372 r_sym_mask = ~(bfd_vma) 0xffffffff;
9373
3410fea8 9374 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9375 if (lo->type == bfd_indirect_link_order)
9376 {
9377 bfd_byte *erel, *erelend;
9378 asection *o = lo->u.indirect.section;
9379
1da212d6
AM
9380 if (o->contents == NULL && o->size != 0)
9381 {
9382 /* This is a reloc section that is being handled as a normal
9383 section. See bfd_section_from_shdr. We can't combine
9384 relocs in this case. */
9385 free (sort);
9386 return 0;
9387 }
c152c796 9388 erel = o->contents;
eea6121a 9389 erelend = o->contents + o->size;
c8e44c6d 9390 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9391
c152c796
AM
9392 while (erel < erelend)
9393 {
9394 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9395
c152c796 9396 (*swap_in) (abfd, erel, s->rela);
7e612e98 9397 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9398 s->u.sym_mask = r_sym_mask;
9399 p += sort_elt;
9400 erel += ext_size;
9401 }
9402 }
9403
9404 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9405
9406 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9407 {
9408 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9409 if (s->type != reloc_class_relative)
9410 break;
9411 }
9412 ret = i;
9413 s_non_relative = p;
9414
9415 sq = (struct elf_link_sort_rela *) s_non_relative;
9416 for (; i < count; i++, p += sort_elt)
9417 {
9418 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9419 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9420 sq = sp;
9421 sp->u.offset = sq->rela->r_offset;
9422 }
9423
9424 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9425
c8e44c6d
AM
9426 struct elf_link_hash_table *htab = elf_hash_table (info);
9427 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9428 {
9429 /* We have plt relocs in .rela.dyn. */
9430 sq = (struct elf_link_sort_rela *) sort;
9431 for (i = 0; i < count; i++)
9432 if (sq[count - i - 1].type != reloc_class_plt)
9433 break;
9434 if (i != 0 && htab->srelplt->size == i * ext_size)
9435 {
9436 struct bfd_link_order **plo;
9437 /* Put srelplt link_order last. This is so the output_offset
9438 set in the next loop is correct for DT_JMPREL. */
9439 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9440 if ((*plo)->type == bfd_indirect_link_order
9441 && (*plo)->u.indirect.section == htab->srelplt)
9442 {
9443 lo = *plo;
9444 *plo = lo->next;
9445 }
9446 else
9447 plo = &(*plo)->next;
9448 *plo = lo;
9449 lo->next = NULL;
9450 dynamic_relocs->map_tail.link_order = lo;
9451 }
9452 }
9453
9454 p = sort;
3410fea8 9455 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9456 if (lo->type == bfd_indirect_link_order)
9457 {
9458 bfd_byte *erel, *erelend;
9459 asection *o = lo->u.indirect.section;
9460
9461 erel = o->contents;
eea6121a 9462 erelend = o->contents + o->size;
c8e44c6d 9463 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9464 while (erel < erelend)
9465 {
9466 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9467 (*swap_out) (abfd, s->rela, erel);
9468 p += sort_elt;
9469 erel += ext_size;
9470 }
9471 }
9472
9473 free (sort);
3410fea8 9474 *psec = dynamic_relocs;
c152c796
AM
9475 return ret;
9476}
9477
ef10c3ac 9478/* Add a symbol to the output symbol string table. */
c152c796 9479
6e0b88f1 9480static int
ef10c3ac
L
9481elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9482 const char *name,
9483 Elf_Internal_Sym *elfsym,
9484 asection *input_sec,
9485 struct elf_link_hash_entry *h)
c152c796 9486{
6e0b88f1 9487 int (*output_symbol_hook)
c152c796
AM
9488 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9489 struct elf_link_hash_entry *);
ef10c3ac 9490 struct elf_link_hash_table *hash_table;
c152c796 9491 const struct elf_backend_data *bed;
ef10c3ac 9492 bfd_size_type strtabsize;
c152c796 9493
8539e4e8
AM
9494 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9495
8b127cbc 9496 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9497 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9498 if (output_symbol_hook != NULL)
9499 {
8b127cbc 9500 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9501 if (ret != 1)
9502 return ret;
c152c796
AM
9503 }
9504
06f44071
AM
9505 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9506 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9507 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9508 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9509
ef10c3ac
L
9510 if (name == NULL
9511 || *name == '\0'
9512 || (input_sec->flags & SEC_EXCLUDE))
9513 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9514 else
9515 {
ef10c3ac
L
9516 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9517 to get the final offset for st_name. */
9518 elfsym->st_name
9519 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9520 name, FALSE);
c152c796 9521 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9522 return 0;
c152c796
AM
9523 }
9524
ef10c3ac
L
9525 hash_table = elf_hash_table (flinfo->info);
9526 strtabsize = hash_table->strtabsize;
9527 if (strtabsize <= hash_table->strtabcount)
c152c796 9528 {
ef10c3ac
L
9529 strtabsize += strtabsize;
9530 hash_table->strtabsize = strtabsize;
9531 strtabsize *= sizeof (*hash_table->strtab);
9532 hash_table->strtab
9533 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9534 strtabsize);
9535 if (hash_table->strtab == NULL)
6e0b88f1 9536 return 0;
c152c796 9537 }
ef10c3ac
L
9538 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9539 hash_table->strtab[hash_table->strtabcount].dest_index
9540 = hash_table->strtabcount;
9541 hash_table->strtab[hash_table->strtabcount].destshndx_index
9542 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9543
ed48ec2e 9544 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9545 hash_table->strtabcount += 1;
9546
9547 return 1;
9548}
9549
9550/* Swap symbols out to the symbol table and flush the output symbols to
9551 the file. */
9552
9553static bfd_boolean
9554elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9555{
9556 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9557 size_t amt;
ef53be89 9558 size_t i;
ef10c3ac
L
9559 const struct elf_backend_data *bed;
9560 bfd_byte *symbuf;
9561 Elf_Internal_Shdr *hdr;
9562 file_ptr pos;
9563 bfd_boolean ret;
9564
9565 if (!hash_table->strtabcount)
9566 return TRUE;
9567
9568 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9569
9570 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9571
ef10c3ac
L
9572 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9573 symbuf = (bfd_byte *) bfd_malloc (amt);
9574 if (symbuf == NULL)
9575 return FALSE;
1b786873 9576
ef10c3ac 9577 if (flinfo->symshndxbuf)
c152c796 9578 {
ef53be89
AM
9579 amt = sizeof (Elf_External_Sym_Shndx);
9580 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9581 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9582 if (flinfo->symshndxbuf == NULL)
c152c796 9583 {
ef10c3ac
L
9584 free (symbuf);
9585 return FALSE;
c152c796 9586 }
c152c796
AM
9587 }
9588
ef10c3ac
L
9589 for (i = 0; i < hash_table->strtabcount; i++)
9590 {
9591 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9592 if (elfsym->sym.st_name == (unsigned long) -1)
9593 elfsym->sym.st_name = 0;
9594 else
9595 elfsym->sym.st_name
9596 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9597 elfsym->sym.st_name);
9598 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9599 ((bfd_byte *) symbuf
9600 + (elfsym->dest_index
9601 * bed->s->sizeof_sym)),
9602 (flinfo->symshndxbuf
9603 + elfsym->destshndx_index));
9604 }
9605
1ff6de03
NA
9606 /* Allow the linker to examine the strtab and symtab now they are
9607 populated. */
9608
9609 if (flinfo->info->callbacks->examine_strtab)
9610 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9611 hash_table->strtabcount,
9612 flinfo->symstrtab);
9613
ef10c3ac
L
9614 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9615 pos = hdr->sh_offset + hdr->sh_size;
9616 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9617 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9618 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9619 {
9620 hdr->sh_size += amt;
9621 ret = TRUE;
9622 }
9623 else
9624 ret = FALSE;
c152c796 9625
ef10c3ac
L
9626 free (symbuf);
9627
9628 free (hash_table->strtab);
9629 hash_table->strtab = NULL;
9630
9631 return ret;
c152c796
AM
9632}
9633
c0d5a53d
L
9634/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9635
9636static bfd_boolean
9637check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9638{
4fbb74a6
AM
9639 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9640 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9641 {
9642 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9643 beyond 64k. */
4eca0228 9644 _bfd_error_handler
695344c0 9645 /* xgettext:c-format */
9793eb77 9646 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9647 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9648 bfd_set_error (bfd_error_nonrepresentable_section);
9649 return FALSE;
9650 }
9651 return TRUE;
9652}
9653
c152c796
AM
9654/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9655 allowing an unsatisfied unversioned symbol in the DSO to match a
9656 versioned symbol that would normally require an explicit version.
9657 We also handle the case that a DSO references a hidden symbol
9658 which may be satisfied by a versioned symbol in another DSO. */
9659
9660static bfd_boolean
9661elf_link_check_versioned_symbol (struct bfd_link_info *info,
9662 const struct elf_backend_data *bed,
9663 struct elf_link_hash_entry *h)
9664{
9665 bfd *abfd;
9666 struct elf_link_loaded_list *loaded;
9667
9668 if (!is_elf_hash_table (info->hash))
9669 return FALSE;
9670
90c984fc
L
9671 /* Check indirect symbol. */
9672 while (h->root.type == bfd_link_hash_indirect)
9673 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9674
c152c796
AM
9675 switch (h->root.type)
9676 {
9677 default:
9678 abfd = NULL;
9679 break;
9680
9681 case bfd_link_hash_undefined:
9682 case bfd_link_hash_undefweak:
9683 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9684 if (abfd == NULL
9685 || (abfd->flags & DYNAMIC) == 0
e56f61be 9686 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9687 return FALSE;
9688 break;
9689
9690 case bfd_link_hash_defined:
9691 case bfd_link_hash_defweak:
9692 abfd = h->root.u.def.section->owner;
9693 break;
9694
9695 case bfd_link_hash_common:
9696 abfd = h->root.u.c.p->section->owner;
9697 break;
9698 }
9699 BFD_ASSERT (abfd != NULL);
9700
e310298c 9701 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
9702 loaded != NULL;
9703 loaded = loaded->next)
9704 {
9705 bfd *input;
9706 Elf_Internal_Shdr *hdr;
ef53be89
AM
9707 size_t symcount;
9708 size_t extsymcount;
9709 size_t extsymoff;
c152c796
AM
9710 Elf_Internal_Shdr *versymhdr;
9711 Elf_Internal_Sym *isym;
9712 Elf_Internal_Sym *isymend;
9713 Elf_Internal_Sym *isymbuf;
9714 Elf_External_Versym *ever;
9715 Elf_External_Versym *extversym;
9716
9717 input = loaded->abfd;
9718
9719 /* We check each DSO for a possible hidden versioned definition. */
9720 if (input == abfd
c152c796
AM
9721 || elf_dynversym (input) == 0)
9722 continue;
9723
9724 hdr = &elf_tdata (input)->dynsymtab_hdr;
9725
9726 symcount = hdr->sh_size / bed->s->sizeof_sym;
9727 if (elf_bad_symtab (input))
9728 {
9729 extsymcount = symcount;
9730 extsymoff = 0;
9731 }
9732 else
9733 {
9734 extsymcount = symcount - hdr->sh_info;
9735 extsymoff = hdr->sh_info;
9736 }
9737
9738 if (extsymcount == 0)
9739 continue;
9740
9741 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9742 NULL, NULL, NULL);
9743 if (isymbuf == NULL)
9744 return FALSE;
9745
9746 /* Read in any version definitions. */
9747 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 9748 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
9749 || (extversym = (Elf_External_Versym *)
9750 _bfd_malloc_and_read (input, versymhdr->sh_size,
9751 versymhdr->sh_size)) == NULL)
c152c796 9752 {
c152c796
AM
9753 free (isymbuf);
9754 return FALSE;
9755 }
9756
9757 ever = extversym + extsymoff;
9758 isymend = isymbuf + extsymcount;
9759 for (isym = isymbuf; isym < isymend; isym++, ever++)
9760 {
9761 const char *name;
9762 Elf_Internal_Versym iver;
9763 unsigned short version_index;
9764
9765 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9766 || isym->st_shndx == SHN_UNDEF)
9767 continue;
9768
9769 name = bfd_elf_string_from_elf_section (input,
9770 hdr->sh_link,
9771 isym->st_name);
9772 if (strcmp (name, h->root.root.string) != 0)
9773 continue;
9774
9775 _bfd_elf_swap_versym_in (input, ever, &iver);
9776
d023c380
L
9777 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9778 && !(h->def_regular
9779 && h->forced_local))
c152c796
AM
9780 {
9781 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9782 have provided a definition for the undefined sym unless
9783 it is defined in a non-shared object and forced local.
9784 */
c152c796
AM
9785 abort ();
9786 }
9787
9788 version_index = iver.vs_vers & VERSYM_VERSION;
9789 if (version_index == 1 || version_index == 2)
9790 {
9791 /* This is the base or first version. We can use it. */
9792 free (extversym);
9793 free (isymbuf);
9794 return TRUE;
9795 }
9796 }
9797
9798 free (extversym);
9799 free (isymbuf);
9800 }
9801
9802 return FALSE;
9803}
9804
b8871f35
L
9805/* Convert ELF common symbol TYPE. */
9806
9807static int
9808elf_link_convert_common_type (struct bfd_link_info *info, int type)
9809{
9810 /* Commom symbol can only appear in relocatable link. */
9811 if (!bfd_link_relocatable (info))
9812 abort ();
9813 switch (info->elf_stt_common)
9814 {
9815 case unchanged:
9816 break;
9817 case elf_stt_common:
9818 type = STT_COMMON;
9819 break;
9820 case no_elf_stt_common:
9821 type = STT_OBJECT;
9822 break;
9823 }
9824 return type;
9825}
9826
c152c796
AM
9827/* Add an external symbol to the symbol table. This is called from
9828 the hash table traversal routine. When generating a shared object,
9829 we go through the symbol table twice. The first time we output
9830 anything that might have been forced to local scope in a version
9831 script. The second time we output the symbols that are still
9832 global symbols. */
9833
9834static bfd_boolean
7686d77d 9835elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9836{
7686d77d 9837 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9838 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9839 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9840 bfd_boolean strip;
9841 Elf_Internal_Sym sym;
9842 asection *input_sec;
9843 const struct elf_backend_data *bed;
6e0b88f1
AM
9844 long indx;
9845 int ret;
b8871f35 9846 unsigned int type;
c152c796
AM
9847
9848 if (h->root.type == bfd_link_hash_warning)
9849 {
9850 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9851 if (h->root.type == bfd_link_hash_new)
9852 return TRUE;
9853 }
9854
9855 /* Decide whether to output this symbol in this pass. */
9856 if (eoinfo->localsyms)
9857 {
4deb8f71 9858 if (!h->forced_local)
c152c796
AM
9859 return TRUE;
9860 }
9861 else
9862 {
4deb8f71 9863 if (h->forced_local)
c152c796
AM
9864 return TRUE;
9865 }
9866
8b127cbc 9867 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9868
12ac1cf5 9869 if (h->root.type == bfd_link_hash_undefined)
c152c796 9870 {
12ac1cf5
NC
9871 /* If we have an undefined symbol reference here then it must have
9872 come from a shared library that is being linked in. (Undefined
98da7939
L
9873 references in regular files have already been handled unless
9874 they are in unreferenced sections which are removed by garbage
9875 collection). */
12ac1cf5
NC
9876 bfd_boolean ignore_undef = FALSE;
9877
9878 /* Some symbols may be special in that the fact that they're
9879 undefined can be safely ignored - let backend determine that. */
9880 if (bed->elf_backend_ignore_undef_symbol)
9881 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9882
9883 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9884 if (!ignore_undef
c54f1524 9885 && h->ref_dynamic_nonweak
8b127cbc
AM
9886 && (!h->ref_regular || flinfo->info->gc_sections)
9887 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9888 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
9889 {
9890 flinfo->info->callbacks->undefined_symbol
9891 (flinfo->info, h->root.root.string,
9892 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
9893 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
9894 && !flinfo->info->warn_unresolved_syms);
9895 }
97196564
L
9896
9897 /* Strip a global symbol defined in a discarded section. */
9898 if (h->indx == -3)
9899 return TRUE;
c152c796
AM
9900 }
9901
9902 /* We should also warn if a forced local symbol is referenced from
9903 shared libraries. */
0e1862bb 9904 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9905 && h->forced_local
9906 && h->ref_dynamic
371a5866 9907 && h->def_regular
f5385ebf 9908 && !h->dynamic_def
ee659f1f 9909 && h->ref_dynamic_nonweak
8b127cbc 9910 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9911 {
17d078c5
AM
9912 bfd *def_bfd;
9913 const char *msg;
90c984fc
L
9914 struct elf_link_hash_entry *hi = h;
9915
9916 /* Check indirect symbol. */
9917 while (hi->root.type == bfd_link_hash_indirect)
9918 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9919
9920 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9921 /* xgettext:c-format */
871b3ab2 9922 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9923 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9924 /* xgettext:c-format */
871b3ab2 9925 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9926 else
695344c0 9927 /* xgettext:c-format */
871b3ab2 9928 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9929 def_bfd = flinfo->output_bfd;
90c984fc
L
9930 if (hi->root.u.def.section != bfd_abs_section_ptr)
9931 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9932 _bfd_error_handler (msg, flinfo->output_bfd,
9933 h->root.root.string, def_bfd);
17d078c5 9934 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9935 eoinfo->failed = TRUE;
9936 return FALSE;
9937 }
9938
9939 /* We don't want to output symbols that have never been mentioned by
9940 a regular file, or that we have been told to strip. However, if
9941 h->indx is set to -2, the symbol is used by a reloc and we must
9942 output it. */
d983c8c5 9943 strip = FALSE;
c152c796 9944 if (h->indx == -2)
d983c8c5 9945 ;
f5385ebf 9946 else if ((h->def_dynamic
77cfaee6
AM
9947 || h->ref_dynamic
9948 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9949 && !h->def_regular
9950 && !h->ref_regular)
c152c796 9951 strip = TRUE;
8b127cbc 9952 else if (flinfo->info->strip == strip_all)
c152c796 9953 strip = TRUE;
8b127cbc
AM
9954 else if (flinfo->info->strip == strip_some
9955 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9956 h->root.root.string, FALSE, FALSE) == NULL)
9957 strip = TRUE;
d56d55e7
AM
9958 else if ((h->root.type == bfd_link_hash_defined
9959 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9960 && ((flinfo->info->strip_discarded
dbaa2011 9961 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9962 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9963 && h->root.u.def.section->owner != NULL
d56d55e7 9964 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9965 strip = TRUE;
9e2278f5
AM
9966 else if ((h->root.type == bfd_link_hash_undefined
9967 || h->root.type == bfd_link_hash_undefweak)
9968 && h->root.u.undef.abfd != NULL
9969 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9970 strip = TRUE;
c152c796 9971
b8871f35
L
9972 type = h->type;
9973
c152c796 9974 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9975 nothing else to do. However, if it is a forced local symbol or
9976 an ifunc symbol we need to give the backend finish_dynamic_symbol
9977 function a chance to make it dynamic. */
c152c796
AM
9978 if (strip
9979 && h->dynindx == -1
b8871f35 9980 && type != STT_GNU_IFUNC
f5385ebf 9981 && !h->forced_local)
c152c796
AM
9982 return TRUE;
9983
9984 sym.st_value = 0;
9985 sym.st_size = h->size;
9986 sym.st_other = h->other;
c152c796
AM
9987 switch (h->root.type)
9988 {
9989 default:
9990 case bfd_link_hash_new:
9991 case bfd_link_hash_warning:
9992 abort ();
9993 return FALSE;
9994
9995 case bfd_link_hash_undefined:
9996 case bfd_link_hash_undefweak:
9997 input_sec = bfd_und_section_ptr;
9998 sym.st_shndx = SHN_UNDEF;
9999 break;
10000
10001 case bfd_link_hash_defined:
10002 case bfd_link_hash_defweak:
10003 {
10004 input_sec = h->root.u.def.section;
10005 if (input_sec->output_section != NULL)
10006 {
10007 sym.st_shndx =
8b127cbc 10008 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10009 input_sec->output_section);
10010 if (sym.st_shndx == SHN_BAD)
10011 {
4eca0228 10012 _bfd_error_handler
695344c0 10013 /* xgettext:c-format */
871b3ab2 10014 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10015 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10016 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10017 eoinfo->failed = TRUE;
10018 return FALSE;
10019 }
10020
10021 /* ELF symbols in relocatable files are section relative,
10022 but in nonrelocatable files they are virtual
10023 addresses. */
10024 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10025 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10026 {
10027 sym.st_value += input_sec->output_section->vma;
10028 if (h->type == STT_TLS)
10029 {
8b127cbc 10030 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10031 if (tls_sec != NULL)
10032 sym.st_value -= tls_sec->vma;
c152c796
AM
10033 }
10034 }
10035 }
10036 else
10037 {
10038 BFD_ASSERT (input_sec->owner == NULL
10039 || (input_sec->owner->flags & DYNAMIC) != 0);
10040 sym.st_shndx = SHN_UNDEF;
10041 input_sec = bfd_und_section_ptr;
10042 }
10043 }
10044 break;
10045
10046 case bfd_link_hash_common:
10047 input_sec = h->root.u.c.p->section;
a4d8e49b 10048 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10049 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10050 break;
10051
10052 case bfd_link_hash_indirect:
10053 /* These symbols are created by symbol versioning. They point
10054 to the decorated version of the name. For example, if the
10055 symbol foo@@GNU_1.2 is the default, which should be used when
10056 foo is used with no version, then we add an indirect symbol
10057 foo which points to foo@@GNU_1.2. We ignore these symbols,
10058 since the indirected symbol is already in the hash table. */
10059 return TRUE;
10060 }
10061
b8871f35
L
10062 if (type == STT_COMMON || type == STT_OBJECT)
10063 switch (h->root.type)
10064 {
10065 case bfd_link_hash_common:
10066 type = elf_link_convert_common_type (flinfo->info, type);
10067 break;
10068 case bfd_link_hash_defined:
10069 case bfd_link_hash_defweak:
10070 if (bed->common_definition (&sym))
10071 type = elf_link_convert_common_type (flinfo->info, type);
10072 else
10073 type = STT_OBJECT;
10074 break;
10075 case bfd_link_hash_undefined:
10076 case bfd_link_hash_undefweak:
10077 break;
10078 default:
10079 abort ();
10080 }
10081
4deb8f71 10082 if (h->forced_local)
b8871f35
L
10083 {
10084 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10085 /* Turn off visibility on local symbol. */
10086 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10087 }
10088 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10089 else if (h->unique_global && h->def_regular)
10090 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10091 else if (h->root.type == bfd_link_hash_undefweak
10092 || h->root.type == bfd_link_hash_defweak)
10093 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10094 else
10095 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10096 sym.st_target_internal = h->target_internal;
10097
c152c796
AM
10098 /* Give the processor backend a chance to tweak the symbol value,
10099 and also to finish up anything that needs to be done for this
10100 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10101 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10102 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10103 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10104 && h->def_regular
0e1862bb 10105 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10106 || ((h->dynindx != -1
10107 || h->forced_local)
0e1862bb 10108 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10109 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10110 || h->root.type != bfd_link_hash_undefweak))
10111 || !h->forced_local)
8b127cbc 10112 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10113 {
10114 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10115 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10116 {
10117 eoinfo->failed = TRUE;
10118 return FALSE;
10119 }
10120 }
10121
10122 /* If we are marking the symbol as undefined, and there are no
10123 non-weak references to this symbol from a regular object, then
10124 mark the symbol as weak undefined; if there are non-weak
10125 references, mark the symbol as strong. We can't do this earlier,
10126 because it might not be marked as undefined until the
10127 finish_dynamic_symbol routine gets through with it. */
10128 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10129 && h->ref_regular
c152c796
AM
10130 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10131 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10132 {
10133 int bindtype;
b8871f35 10134 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10135
10136 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10137 if (type == STT_GNU_IFUNC)
10138 type = STT_FUNC;
c152c796 10139
f5385ebf 10140 if (h->ref_regular_nonweak)
c152c796
AM
10141 bindtype = STB_GLOBAL;
10142 else
10143 bindtype = STB_WEAK;
2955ec4c 10144 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10145 }
10146
bda987c2
CD
10147 /* If this is a symbol defined in a dynamic library, don't use the
10148 symbol size from the dynamic library. Relinking an executable
10149 against a new library may introduce gratuitous changes in the
10150 executable's symbols if we keep the size. */
10151 if (sym.st_shndx == SHN_UNDEF
10152 && !h->def_regular
10153 && h->def_dynamic)
10154 sym.st_size = 0;
10155
c152c796
AM
10156 /* If a non-weak symbol with non-default visibility is not defined
10157 locally, it is a fatal error. */
0e1862bb 10158 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10159 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10160 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10161 && h->root.type == bfd_link_hash_undefined
f5385ebf 10162 && !h->def_regular)
c152c796 10163 {
17d078c5
AM
10164 const char *msg;
10165
10166 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10167 /* xgettext:c-format */
871b3ab2 10168 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10169 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10170 /* xgettext:c-format */
871b3ab2 10171 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10172 else
695344c0 10173 /* xgettext:c-format */
871b3ab2 10174 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10175 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10176 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10177 eoinfo->failed = TRUE;
10178 return FALSE;
10179 }
10180
10181 /* If this symbol should be put in the .dynsym section, then put it
10182 there now. We already know the symbol index. We also fill in
10183 the entry in the .hash section. */
1c2649ed
EB
10184 if (h->dynindx != -1
10185 && elf_hash_table (flinfo->info)->dynamic_sections_created
10186 && elf_hash_table (flinfo->info)->dynsym != NULL
10187 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10188 {
c152c796
AM
10189 bfd_byte *esym;
10190
90c984fc
L
10191 /* Since there is no version information in the dynamic string,
10192 if there is no version info in symbol version section, we will
1659f720 10193 have a run-time problem if not linking executable, referenced
4deb8f71 10194 by shared library, or not bound locally. */
1659f720 10195 if (h->verinfo.verdef == NULL
0e1862bb 10196 && (!bfd_link_executable (flinfo->info)
1659f720
L
10197 || h->ref_dynamic
10198 || !h->def_regular))
90c984fc
L
10199 {
10200 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10201
10202 if (p && p [1] != '\0')
10203 {
4eca0228 10204 _bfd_error_handler
695344c0 10205 /* xgettext:c-format */
9793eb77 10206 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10207 flinfo->output_bfd, h->root.root.string);
10208 eoinfo->failed = TRUE;
10209 return FALSE;
10210 }
10211 }
10212
c152c796 10213 sym.st_name = h->dynstr_index;
cae1fbbb
L
10214 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10215 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10216 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10217 {
10218 eoinfo->failed = TRUE;
10219 return FALSE;
10220 }
8b127cbc 10221 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10222
8b127cbc 10223 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10224 {
10225 size_t hash_entry_size;
10226 bfd_byte *bucketpos;
10227 bfd_vma chain;
41198d0c
L
10228 size_t bucketcount;
10229 size_t bucket;
10230
8b127cbc 10231 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10232 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10233
10234 hash_entry_size
8b127cbc
AM
10235 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10236 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10237 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10238 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10239 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10240 bucketpos);
10241 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10242 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10243 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10244 }
c152c796 10245
8b127cbc 10246 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10247 {
10248 Elf_Internal_Versym iversym;
10249 Elf_External_Versym *eversym;
10250
5fa370e4 10251 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10252 {
7b20f099
AM
10253 if (h->verinfo.verdef == NULL
10254 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10255 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10256 iversym.vs_vers = 0;
10257 else
10258 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10259 }
10260 else
10261 {
10262 if (h->verinfo.vertree == NULL)
10263 iversym.vs_vers = 1;
10264 else
10265 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10266 if (flinfo->info->create_default_symver)
3e3b46e5 10267 iversym.vs_vers++;
c152c796
AM
10268 }
10269
422f1182 10270 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10271 defined locally. */
422f1182 10272 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10273 iversym.vs_vers |= VERSYM_HIDDEN;
10274
8b127cbc 10275 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10276 eversym += h->dynindx;
8b127cbc 10277 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10278 }
10279 }
10280
d983c8c5
AM
10281 /* If the symbol is undefined, and we didn't output it to .dynsym,
10282 strip it from .symtab too. Obviously we can't do this for
10283 relocatable output or when needed for --emit-relocs. */
10284 else if (input_sec == bfd_und_section_ptr
10285 && h->indx != -2
66cae560
NC
10286 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10287 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10288 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10289 return TRUE;
66cae560 10290
d983c8c5
AM
10291 /* Also strip others that we couldn't earlier due to dynamic symbol
10292 processing. */
10293 if (strip)
10294 return TRUE;
10295 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10296 return TRUE;
10297
2ec55de3
AM
10298 /* Output a FILE symbol so that following locals are not associated
10299 with the wrong input file. We need one for forced local symbols
10300 if we've seen more than one FILE symbol or when we have exactly
10301 one FILE symbol but global symbols are present in a file other
10302 than the one with the FILE symbol. We also need one if linker
10303 defined symbols are present. In practice these conditions are
10304 always met, so just emit the FILE symbol unconditionally. */
10305 if (eoinfo->localsyms
10306 && !eoinfo->file_sym_done
10307 && eoinfo->flinfo->filesym_count != 0)
10308 {
10309 Elf_Internal_Sym fsym;
10310
10311 memset (&fsym, 0, sizeof (fsym));
10312 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10313 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10314 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10315 bfd_und_section_ptr, NULL))
2ec55de3
AM
10316 return FALSE;
10317
10318 eoinfo->file_sym_done = TRUE;
10319 }
10320
8b127cbc 10321 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10322 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10323 input_sec, h);
6e0b88f1 10324 if (ret == 0)
c152c796
AM
10325 {
10326 eoinfo->failed = TRUE;
10327 return FALSE;
10328 }
6e0b88f1
AM
10329 else if (ret == 1)
10330 h->indx = indx;
10331 else if (h->indx == -2)
10332 abort();
c152c796
AM
10333
10334 return TRUE;
10335}
10336
cdd3575c
AM
10337/* Return TRUE if special handling is done for relocs in SEC against
10338 symbols defined in discarded sections. */
10339
c152c796
AM
10340static bfd_boolean
10341elf_section_ignore_discarded_relocs (asection *sec)
10342{
10343 const struct elf_backend_data *bed;
10344
cdd3575c
AM
10345 switch (sec->sec_info_type)
10346 {
dbaa2011
AM
10347 case SEC_INFO_TYPE_STABS:
10348 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10349 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10350 return TRUE;
10351 default:
10352 break;
10353 }
c152c796
AM
10354
10355 bed = get_elf_backend_data (sec->owner);
10356 if (bed->elf_backend_ignore_discarded_relocs != NULL
10357 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10358 return TRUE;
10359
10360 return FALSE;
10361}
10362
9e66c942
AM
10363/* Return a mask saying how ld should treat relocations in SEC against
10364 symbols defined in discarded sections. If this function returns
10365 COMPLAIN set, ld will issue a warning message. If this function
10366 returns PRETEND set, and the discarded section was link-once and the
10367 same size as the kept link-once section, ld will pretend that the
10368 symbol was actually defined in the kept section. Otherwise ld will
10369 zero the reloc (at least that is the intent, but some cooperation by
10370 the target dependent code is needed, particularly for REL targets). */
10371
8a696751
AM
10372unsigned int
10373_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10374{
9e66c942 10375 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10376 return PRETEND;
cdd3575c
AM
10377
10378 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10379 return 0;
cdd3575c
AM
10380
10381 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10382 return 0;
cdd3575c 10383
9e66c942 10384 return COMPLAIN | PRETEND;
cdd3575c
AM
10385}
10386
3d7f7666
L
10387/* Find a match between a section and a member of a section group. */
10388
10389static asection *
c0f00686
L
10390match_group_member (asection *sec, asection *group,
10391 struct bfd_link_info *info)
3d7f7666
L
10392{
10393 asection *first = elf_next_in_group (group);
10394 asection *s = first;
10395
10396 while (s != NULL)
10397 {
c0f00686 10398 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10399 return s;
10400
83180ade 10401 s = elf_next_in_group (s);
3d7f7666
L
10402 if (s == first)
10403 break;
10404 }
10405
10406 return NULL;
10407}
10408
01b3c8ab 10409/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10410 to replace it. Return the replacement if it is OK. Otherwise return
10411 NULL. */
01b3c8ab
L
10412
10413asection *
c0f00686 10414_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10415{
10416 asection *kept;
10417
10418 kept = sec->kept_section;
10419 if (kept != NULL)
10420 {
c2370991 10421 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10422 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10423 if (kept != NULL
10424 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10425 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10426 kept = NULL;
c2370991 10427 sec->kept_section = kept;
01b3c8ab
L
10428 }
10429 return kept;
10430}
10431
c152c796
AM
10432/* Link an input file into the linker output file. This function
10433 handles all the sections and relocations of the input file at once.
10434 This is so that we only have to read the local symbols once, and
10435 don't have to keep them in memory. */
10436
10437static bfd_boolean
8b127cbc 10438elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10439{
ece5ef60 10440 int (*relocate_section)
c152c796
AM
10441 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10442 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10443 bfd *output_bfd;
10444 Elf_Internal_Shdr *symtab_hdr;
10445 size_t locsymcount;
10446 size_t extsymoff;
10447 Elf_Internal_Sym *isymbuf;
10448 Elf_Internal_Sym *isym;
10449 Elf_Internal_Sym *isymend;
10450 long *pindex;
10451 asection **ppsection;
10452 asection *o;
10453 const struct elf_backend_data *bed;
c152c796 10454 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10455 bfd_size_type address_size;
10456 bfd_vma r_type_mask;
10457 int r_sym_shift;
ffbc01cc 10458 bfd_boolean have_file_sym = FALSE;
c152c796 10459
8b127cbc 10460 output_bfd = flinfo->output_bfd;
c152c796
AM
10461 bed = get_elf_backend_data (output_bfd);
10462 relocate_section = bed->elf_backend_relocate_section;
10463
10464 /* If this is a dynamic object, we don't want to do anything here:
10465 we don't want the local symbols, and we don't want the section
10466 contents. */
10467 if ((input_bfd->flags & DYNAMIC) != 0)
10468 return TRUE;
10469
c152c796
AM
10470 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10471 if (elf_bad_symtab (input_bfd))
10472 {
10473 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10474 extsymoff = 0;
10475 }
10476 else
10477 {
10478 locsymcount = symtab_hdr->sh_info;
10479 extsymoff = symtab_hdr->sh_info;
10480 }
10481
10482 /* Read the local symbols. */
10483 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10484 if (isymbuf == NULL && locsymcount != 0)
10485 {
10486 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10487 flinfo->internal_syms,
10488 flinfo->external_syms,
10489 flinfo->locsym_shndx);
c152c796
AM
10490 if (isymbuf == NULL)
10491 return FALSE;
10492 }
10493
10494 /* Find local symbol sections and adjust values of symbols in
10495 SEC_MERGE sections. Write out those local symbols we know are
10496 going into the output file. */
10497 isymend = isymbuf + locsymcount;
8b127cbc 10498 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10499 isym < isymend;
10500 isym++, pindex++, ppsection++)
10501 {
10502 asection *isec;
10503 const char *name;
10504 Elf_Internal_Sym osym;
6e0b88f1
AM
10505 long indx;
10506 int ret;
c152c796
AM
10507
10508 *pindex = -1;
10509
10510 if (elf_bad_symtab (input_bfd))
10511 {
10512 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10513 {
10514 *ppsection = NULL;
10515 continue;
10516 }
10517 }
10518
10519 if (isym->st_shndx == SHN_UNDEF)
10520 isec = bfd_und_section_ptr;
c152c796
AM
10521 else if (isym->st_shndx == SHN_ABS)
10522 isec = bfd_abs_section_ptr;
10523 else if (isym->st_shndx == SHN_COMMON)
10524 isec = bfd_com_section_ptr;
10525 else
10526 {
cb33740c
AM
10527 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10528 if (isec == NULL)
10529 {
10530 /* Don't attempt to output symbols with st_shnx in the
10531 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10532 isec = bfd_und_section_ptr;
cb33740c 10533 }
dbaa2011 10534 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10535 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10536 isym->st_value =
10537 _bfd_merged_section_offset (output_bfd, &isec,
10538 elf_section_data (isec)->sec_info,
10539 isym->st_value);
c152c796
AM
10540 }
10541
10542 *ppsection = isec;
10543
d983c8c5
AM
10544 /* Don't output the first, undefined, symbol. In fact, don't
10545 output any undefined local symbol. */
10546 if (isec == bfd_und_section_ptr)
c152c796
AM
10547 continue;
10548
10549 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10550 {
10551 /* We never output section symbols. Instead, we use the
10552 section symbol of the corresponding section in the output
10553 file. */
10554 continue;
10555 }
10556
10557 /* If we are stripping all symbols, we don't want to output this
10558 one. */
8b127cbc 10559 if (flinfo->info->strip == strip_all)
c152c796
AM
10560 continue;
10561
10562 /* If we are discarding all local symbols, we don't want to
10563 output this one. If we are generating a relocatable output
10564 file, then some of the local symbols may be required by
10565 relocs; we output them below as we discover that they are
10566 needed. */
8b127cbc 10567 if (flinfo->info->discard == discard_all)
c152c796
AM
10568 continue;
10569
10570 /* If this symbol is defined in a section which we are
f02571c5 10571 discarding, we don't need to keep it. */
abf874aa
CL
10572 if (isym->st_shndx != SHN_UNDEF
10573 && isym->st_shndx < SHN_LORESERVE
10574 && isec->output_section == NULL
10575 && flinfo->info->non_contiguous_regions
10576 && flinfo->info->non_contiguous_regions_warnings)
10577 {
10578 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10579 "discards section `%s' from '%s'\n"),
10580 isec->name, isec->owner->filename);
10581 continue;
10582 }
10583
f02571c5 10584 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10585 && isym->st_shndx < SHN_LORESERVE
10586 && bfd_section_removed_from_list (output_bfd,
10587 isec->output_section))
e75a280b
L
10588 continue;
10589
c152c796
AM
10590 /* Get the name of the symbol. */
10591 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10592 isym->st_name);
10593 if (name == NULL)
10594 return FALSE;
10595
10596 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10597 if ((flinfo->info->strip == strip_some
10598 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10599 == NULL))
8b127cbc 10600 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10601 && (isec->flags & SEC_MERGE)
10602 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10603 || flinfo->info->discard == discard_l)
c152c796
AM
10604 && bfd_is_local_label_name (input_bfd, name)))
10605 continue;
10606
ffbc01cc
AM
10607 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10608 {
ce875075
AM
10609 if (input_bfd->lto_output)
10610 /* -flto puts a temp file name here. This means builds
10611 are not reproducible. Discard the symbol. */
10612 continue;
ffbc01cc
AM
10613 have_file_sym = TRUE;
10614 flinfo->filesym_count += 1;
10615 }
10616 if (!have_file_sym)
10617 {
10618 /* In the absence of debug info, bfd_find_nearest_line uses
10619 FILE symbols to determine the source file for local
10620 function symbols. Provide a FILE symbol here if input
10621 files lack such, so that their symbols won't be
10622 associated with a previous input file. It's not the
10623 source file, but the best we can do. */
10624 have_file_sym = TRUE;
10625 flinfo->filesym_count += 1;
10626 memset (&osym, 0, sizeof (osym));
10627 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10628 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10629 if (!elf_link_output_symstrtab (flinfo,
10630 (input_bfd->lto_output ? NULL
10631 : input_bfd->filename),
10632 &osym, bfd_abs_section_ptr,
10633 NULL))
ffbc01cc
AM
10634 return FALSE;
10635 }
10636
c152c796
AM
10637 osym = *isym;
10638
10639 /* Adjust the section index for the output file. */
10640 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10641 isec->output_section);
10642 if (osym.st_shndx == SHN_BAD)
10643 return FALSE;
10644
c152c796
AM
10645 /* ELF symbols in relocatable files are section relative, but
10646 in executable files they are virtual addresses. Note that
10647 this code assumes that all ELF sections have an associated
10648 BFD section with a reasonable value for output_offset; below
10649 we assume that they also have a reasonable value for
10650 output_section. Any special sections must be set up to meet
10651 these requirements. */
10652 osym.st_value += isec->output_offset;
0e1862bb 10653 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10654 {
10655 osym.st_value += isec->output_section->vma;
10656 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10657 {
10658 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10659 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10660 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10661 else
10662 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10663 STT_NOTYPE);
c152c796
AM
10664 }
10665 }
10666
6e0b88f1 10667 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10668 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10669 if (ret == 0)
c152c796 10670 return FALSE;
6e0b88f1
AM
10671 else if (ret == 1)
10672 *pindex = indx;
c152c796
AM
10673 }
10674
310fd250
L
10675 if (bed->s->arch_size == 32)
10676 {
10677 r_type_mask = 0xff;
10678 r_sym_shift = 8;
10679 address_size = 4;
10680 }
10681 else
10682 {
10683 r_type_mask = 0xffffffff;
10684 r_sym_shift = 32;
10685 address_size = 8;
10686 }
10687
c152c796
AM
10688 /* Relocate the contents of each section. */
10689 sym_hashes = elf_sym_hashes (input_bfd);
10690 for (o = input_bfd->sections; o != NULL; o = o->next)
10691 {
10692 bfd_byte *contents;
10693
10694 if (! o->linker_mark)
10695 {
10696 /* This section was omitted from the link. */
10697 continue;
10698 }
10699
7bdf4127 10700 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10701 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10702 {
10703 /* Deal with the group signature symbol. */
10704 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10705 unsigned long symndx = sec_data->this_hdr.sh_info;
10706 asection *osec = o->output_section;
10707
7bdf4127 10708 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10709 if (symndx >= locsymcount
10710 || (elf_bad_symtab (input_bfd)
8b127cbc 10711 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10712 {
10713 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10714 while (h->root.type == bfd_link_hash_indirect
10715 || h->root.type == bfd_link_hash_warning)
10716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10717 /* Arrange for symbol to be output. */
10718 h->indx = -2;
10719 elf_section_data (osec)->this_hdr.sh_info = -2;
10720 }
10721 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10722 {
10723 /* We'll use the output section target_index. */
8b127cbc 10724 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10725 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10726 }
10727 else
10728 {
8b127cbc 10729 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10730 {
10731 /* Otherwise output the local symbol now. */
10732 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10733 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10734 const char *name;
6e0b88f1
AM
10735 long indx;
10736 int ret;
bcacc0f5
AM
10737
10738 name = bfd_elf_string_from_elf_section (input_bfd,
10739 symtab_hdr->sh_link,
10740 sym.st_name);
10741 if (name == NULL)
10742 return FALSE;
10743
10744 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10745 sec);
10746 if (sym.st_shndx == SHN_BAD)
10747 return FALSE;
10748
10749 sym.st_value += o->output_offset;
10750
6e0b88f1 10751 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10752 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10753 NULL);
6e0b88f1 10754 if (ret == 0)
bcacc0f5 10755 return FALSE;
6e0b88f1 10756 else if (ret == 1)
8b127cbc 10757 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10758 else
10759 abort ();
bcacc0f5
AM
10760 }
10761 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10762 = flinfo->indices[symndx];
bcacc0f5
AM
10763 }
10764 }
10765
c152c796 10766 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10767 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10768 continue;
10769
10770 if ((o->flags & SEC_LINKER_CREATED) != 0)
10771 {
10772 /* Section was created by _bfd_elf_link_create_dynamic_sections
10773 or somesuch. */
10774 continue;
10775 }
10776
10777 /* Get the contents of the section. They have been cached by a
10778 relaxation routine. Note that o is a section in an input
10779 file, so the contents field will not have been set by any of
10780 the routines which work on output files. */
10781 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10782 {
10783 contents = elf_section_data (o)->this_hdr.contents;
10784 if (bed->caches_rawsize
10785 && o->rawsize != 0
10786 && o->rawsize < o->size)
10787 {
10788 memcpy (flinfo->contents, contents, o->rawsize);
10789 contents = flinfo->contents;
10790 }
10791 }
c152c796
AM
10792 else
10793 {
8b127cbc 10794 contents = flinfo->contents;
4a114e3e 10795 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10796 return FALSE;
10797 }
10798
10799 if ((o->flags & SEC_RELOC) != 0)
10800 {
10801 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10802 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10803 int action_discarded;
ece5ef60 10804 int ret;
c152c796
AM
10805
10806 /* Get the swapped relocs. */
10807 internal_relocs
8b127cbc
AM
10808 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10809 flinfo->internal_relocs, FALSE);
c152c796
AM
10810 if (internal_relocs == NULL
10811 && o->reloc_count > 0)
10812 return FALSE;
10813
310fd250
L
10814 /* We need to reverse-copy input .ctors/.dtors sections if
10815 they are placed in .init_array/.finit_array for output. */
10816 if (o->size > address_size
10817 && ((strncmp (o->name, ".ctors", 6) == 0
10818 && strcmp (o->output_section->name,
10819 ".init_array") == 0)
10820 || (strncmp (o->name, ".dtors", 6) == 0
10821 && strcmp (o->output_section->name,
10822 ".fini_array") == 0))
10823 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10824 {
056bafd4
MR
10825 if (o->size * bed->s->int_rels_per_ext_rel
10826 != o->reloc_count * address_size)
310fd250 10827 {
4eca0228 10828 _bfd_error_handler
695344c0 10829 /* xgettext:c-format */
871b3ab2 10830 (_("error: %pB: size of section %pA is not "
310fd250
L
10831 "multiple of address size"),
10832 input_bfd, o);
8c6716e5 10833 bfd_set_error (bfd_error_bad_value);
310fd250
L
10834 return FALSE;
10835 }
10836 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10837 }
10838
0f02bbd9 10839 action_discarded = -1;
c152c796 10840 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10841 action_discarded = (*bed->action_discarded) (o);
10842
10843 /* Run through the relocs evaluating complex reloc symbols and
10844 looking for relocs against symbols from discarded sections
10845 or section symbols from removed link-once sections.
10846 Complain about relocs against discarded sections. Zero
10847 relocs against removed link-once sections. */
10848
10849 rel = internal_relocs;
056bafd4 10850 relend = rel + o->reloc_count;
0f02bbd9 10851 for ( ; rel < relend; rel++)
c152c796 10852 {
0f02bbd9
AM
10853 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10854 unsigned int s_type;
10855 asection **ps, *sec;
10856 struct elf_link_hash_entry *h = NULL;
10857 const char *sym_name;
c152c796 10858
0f02bbd9
AM
10859 if (r_symndx == STN_UNDEF)
10860 continue;
c152c796 10861
0f02bbd9
AM
10862 if (r_symndx >= locsymcount
10863 || (elf_bad_symtab (input_bfd)
8b127cbc 10864 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10865 {
10866 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10867
0f02bbd9
AM
10868 /* Badly formatted input files can contain relocs that
10869 reference non-existant symbols. Check here so that
10870 we do not seg fault. */
10871 if (h == NULL)
c152c796 10872 {
4eca0228 10873 _bfd_error_handler
695344c0 10874 /* xgettext:c-format */
2dcf00ce 10875 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10876 "that references a non-existent global symbol"),
2dcf00ce 10877 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10878 bfd_set_error (bfd_error_bad_value);
10879 return FALSE;
10880 }
3b36f7e6 10881
0f02bbd9
AM
10882 while (h->root.type == bfd_link_hash_indirect
10883 || h->root.type == bfd_link_hash_warning)
10884 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10885
0f02bbd9 10886 s_type = h->type;
cdd3575c 10887
9e2dec47 10888 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10889 mark the symbol as undefined. Note that the
10890 linker may attach linker created dynamic sections
10891 to the plugin bfd. Symbols defined in linker
10892 created sections are not plugin symbols. */
bc4e12de 10893 if ((h->root.non_ir_ref_regular
4070765b 10894 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10895 && (h->root.type == bfd_link_hash_defined
10896 || h->root.type == bfd_link_hash_defweak)
10897 && (h->root.u.def.section->flags
10898 & SEC_LINKER_CREATED) == 0
10899 && h->root.u.def.section->owner != NULL
10900 && (h->root.u.def.section->owner->flags
10901 & BFD_PLUGIN) != 0)
10902 {
10903 h->root.type = bfd_link_hash_undefined;
10904 h->root.u.undef.abfd = h->root.u.def.section->owner;
10905 }
10906
0f02bbd9
AM
10907 ps = NULL;
10908 if (h->root.type == bfd_link_hash_defined
10909 || h->root.type == bfd_link_hash_defweak)
10910 ps = &h->root.u.def.section;
10911
10912 sym_name = h->root.root.string;
10913 }
10914 else
10915 {
10916 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10917
10918 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10919 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10920 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10921 sym, *ps);
10922 }
c152c796 10923
c301e700 10924 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10925 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10926 {
10927 bfd_vma val;
10928 bfd_vma dot = (rel->r_offset
10929 + o->output_offset + o->output_section->vma);
10930#ifdef DEBUG
10931 printf ("Encountered a complex symbol!");
10932 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10933 input_bfd->filename, o->name,
10934 (long) (rel - internal_relocs));
0f02bbd9
AM
10935 printf (" symbol: idx %8.8lx, name %s\n",
10936 r_symndx, sym_name);
10937 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10938 (unsigned long) rel->r_info,
10939 (unsigned long) rel->r_offset);
10940#endif
8b127cbc 10941 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10942 isymbuf, locsymcount, s_type == STT_SRELC))
10943 return FALSE;
10944
10945 /* Symbol evaluated OK. Update to absolute value. */
10946 set_symbol_value (input_bfd, isymbuf, locsymcount,
10947 r_symndx, val);
10948 continue;
10949 }
10950
10951 if (action_discarded != -1 && ps != NULL)
10952 {
cdd3575c
AM
10953 /* Complain if the definition comes from a
10954 discarded section. */
dbaa2011 10955 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10956 {
cf35638d 10957 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10958 if (action_discarded & COMPLAIN)
8b127cbc 10959 (*flinfo->info->callbacks->einfo)
695344c0 10960 /* xgettext:c-format */
871b3ab2
AM
10961 (_("%X`%s' referenced in section `%pA' of %pB: "
10962 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10963 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10964
87e5235d 10965 /* Try to do the best we can to support buggy old
e0ae6d6f 10966 versions of gcc. Pretend that the symbol is
87e5235d
AM
10967 really defined in the kept linkonce section.
10968 FIXME: This is quite broken. Modifying the
10969 symbol here means we will be changing all later
e0ae6d6f 10970 uses of the symbol, not just in this section. */
0f02bbd9 10971 if (action_discarded & PRETEND)
87e5235d 10972 {
01b3c8ab
L
10973 asection *kept;
10974
c0f00686 10975 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10976 flinfo->info);
01b3c8ab 10977 if (kept != NULL)
87e5235d
AM
10978 {
10979 *ps = kept;
10980 continue;
10981 }
10982 }
c152c796
AM
10983 }
10984 }
10985 }
10986
10987 /* Relocate the section by invoking a back end routine.
10988
10989 The back end routine is responsible for adjusting the
10990 section contents as necessary, and (if using Rela relocs
10991 and generating a relocatable output file) adjusting the
10992 reloc addend as necessary.
10993
10994 The back end routine does not have to worry about setting
10995 the reloc address or the reloc symbol index.
10996
10997 The back end routine is given a pointer to the swapped in
10998 internal symbols, and can access the hash table entries
10999 for the external symbols via elf_sym_hashes (input_bfd).
11000
11001 When generating relocatable output, the back end routine
11002 must handle STB_LOCAL/STT_SECTION symbols specially. The
11003 output symbol is going to be a section symbol
11004 corresponding to the output section, which will require
11005 the addend to be adjusted. */
11006
8b127cbc 11007 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11008 input_bfd, o, contents,
11009 internal_relocs,
11010 isymbuf,
8b127cbc 11011 flinfo->sections);
ece5ef60 11012 if (!ret)
c152c796
AM
11013 return FALSE;
11014
ece5ef60 11015 if (ret == 2
0e1862bb 11016 || bfd_link_relocatable (flinfo->info)
8b127cbc 11017 || flinfo->info->emitrelocations)
c152c796
AM
11018 {
11019 Elf_Internal_Rela *irela;
d4730f92 11020 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11021 bfd_vma last_offset;
11022 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11023 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11024 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11025 unsigned int next_erel;
c152c796 11026 bfd_boolean rela_normal;
d4730f92 11027 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11028
d4730f92
BS
11029 esdi = elf_section_data (o);
11030 esdo = elf_section_data (o->output_section);
11031 rela_normal = FALSE;
c152c796
AM
11032
11033 /* Adjust the reloc addresses and symbol indices. */
11034
11035 irela = internal_relocs;
056bafd4 11036 irelaend = irela + o->reloc_count;
d4730f92
BS
11037 rel_hash = esdo->rel.hashes + esdo->rel.count;
11038 /* We start processing the REL relocs, if any. When we reach
11039 IRELAMID in the loop, we switch to the RELA relocs. */
11040 irelamid = irela;
11041 if (esdi->rel.hdr != NULL)
11042 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11043 * bed->s->int_rels_per_ext_rel);
eac338cf 11044 rel_hash_list = rel_hash;
d4730f92 11045 rela_hash_list = NULL;
c152c796 11046 last_offset = o->output_offset;
0e1862bb 11047 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11048 last_offset += o->output_section->vma;
11049 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11050 {
11051 unsigned long r_symndx;
11052 asection *sec;
11053 Elf_Internal_Sym sym;
11054
11055 if (next_erel == bed->s->int_rels_per_ext_rel)
11056 {
11057 rel_hash++;
11058 next_erel = 0;
11059 }
11060
d4730f92
BS
11061 if (irela == irelamid)
11062 {
11063 rel_hash = esdo->rela.hashes + esdo->rela.count;
11064 rela_hash_list = rel_hash;
11065 rela_normal = bed->rela_normal;
11066 }
11067
c152c796 11068 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11069 flinfo->info, o,
c152c796
AM
11070 irela->r_offset);
11071 if (irela->r_offset >= (bfd_vma) -2)
11072 {
11073 /* This is a reloc for a deleted entry or somesuch.
11074 Turn it into an R_*_NONE reloc, at the same
11075 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11076 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11077 being ordered. */
11078 irela->r_offset = last_offset;
11079 irela->r_info = 0;
11080 irela->r_addend = 0;
11081 continue;
11082 }
11083
11084 irela->r_offset += o->output_offset;
11085
11086 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11087 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11088 irela->r_offset += o->output_section->vma;
11089
11090 last_offset = irela->r_offset;
11091
11092 r_symndx = irela->r_info >> r_sym_shift;
11093 if (r_symndx == STN_UNDEF)
11094 continue;
11095
11096 if (r_symndx >= locsymcount
11097 || (elf_bad_symtab (input_bfd)
8b127cbc 11098 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11099 {
11100 struct elf_link_hash_entry *rh;
11101 unsigned long indx;
11102
11103 /* This is a reloc against a global symbol. We
11104 have not yet output all the local symbols, so
11105 we do not know the symbol index of any global
11106 symbol. We set the rel_hash entry for this
11107 reloc to point to the global hash table entry
11108 for this symbol. The symbol index is then
ee75fd95 11109 set at the end of bfd_elf_final_link. */
c152c796
AM
11110 indx = r_symndx - extsymoff;
11111 rh = elf_sym_hashes (input_bfd)[indx];
11112 while (rh->root.type == bfd_link_hash_indirect
11113 || rh->root.type == bfd_link_hash_warning)
11114 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11115
11116 /* Setting the index to -2 tells
11117 elf_link_output_extsym that this symbol is
11118 used by a reloc. */
11119 BFD_ASSERT (rh->indx < 0);
11120 rh->indx = -2;
c152c796
AM
11121 *rel_hash = rh;
11122
11123 continue;
11124 }
11125
11126 /* This is a reloc against a local symbol. */
11127
11128 *rel_hash = NULL;
11129 sym = isymbuf[r_symndx];
8b127cbc 11130 sec = flinfo->sections[r_symndx];
c152c796
AM
11131 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11132 {
11133 /* I suppose the backend ought to fill in the
11134 section of any STT_SECTION symbol against a
6a8d1586 11135 processor specific section. */
cf35638d 11136 r_symndx = STN_UNDEF;
6a8d1586
AM
11137 if (bfd_is_abs_section (sec))
11138 ;
c152c796
AM
11139 else if (sec == NULL || sec->owner == NULL)
11140 {
11141 bfd_set_error (bfd_error_bad_value);
11142 return FALSE;
11143 }
11144 else
11145 {
6a8d1586
AM
11146 asection *osec = sec->output_section;
11147
11148 /* If we have discarded a section, the output
11149 section will be the absolute section. In
ab96bf03
AM
11150 case of discarded SEC_MERGE sections, use
11151 the kept section. relocate_section should
11152 have already handled discarded linkonce
11153 sections. */
6a8d1586
AM
11154 if (bfd_is_abs_section (osec)
11155 && sec->kept_section != NULL
11156 && sec->kept_section->output_section != NULL)
11157 {
11158 osec = sec->kept_section->output_section;
11159 irela->r_addend -= osec->vma;
11160 }
11161
11162 if (!bfd_is_abs_section (osec))
11163 {
11164 r_symndx = osec->target_index;
cf35638d 11165 if (r_symndx == STN_UNDEF)
74541ad4 11166 {
051d833a
AM
11167 irela->r_addend += osec->vma;
11168 osec = _bfd_nearby_section (output_bfd, osec,
11169 osec->vma);
11170 irela->r_addend -= osec->vma;
11171 r_symndx = osec->target_index;
74541ad4 11172 }
6a8d1586 11173 }
c152c796
AM
11174 }
11175
11176 /* Adjust the addend according to where the
11177 section winds up in the output section. */
11178 if (rela_normal)
11179 irela->r_addend += sec->output_offset;
11180 }
11181 else
11182 {
8b127cbc 11183 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11184 {
11185 unsigned long shlink;
11186 const char *name;
11187 asection *osec;
6e0b88f1 11188 long indx;
c152c796 11189
8b127cbc 11190 if (flinfo->info->strip == strip_all)
c152c796
AM
11191 {
11192 /* You can't do ld -r -s. */
11193 bfd_set_error (bfd_error_invalid_operation);
11194 return FALSE;
11195 }
11196
11197 /* This symbol was skipped earlier, but
11198 since it is needed by a reloc, we
11199 must output it now. */
11200 shlink = symtab_hdr->sh_link;
11201 name = (bfd_elf_string_from_elf_section
11202 (input_bfd, shlink, sym.st_name));
11203 if (name == NULL)
11204 return FALSE;
11205
11206 osec = sec->output_section;
11207 sym.st_shndx =
11208 _bfd_elf_section_from_bfd_section (output_bfd,
11209 osec);
11210 if (sym.st_shndx == SHN_BAD)
11211 return FALSE;
11212
11213 sym.st_value += sec->output_offset;
0e1862bb 11214 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11215 {
11216 sym.st_value += osec->vma;
11217 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11218 {
102def4d
AM
11219 struct elf_link_hash_table *htab
11220 = elf_hash_table (flinfo->info);
11221
c152c796
AM
11222 /* STT_TLS symbols are relative to PT_TLS
11223 segment base. */
102def4d
AM
11224 if (htab->tls_sec != NULL)
11225 sym.st_value -= htab->tls_sec->vma;
11226 else
11227 sym.st_info
11228 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11229 STT_NOTYPE);
c152c796
AM
11230 }
11231 }
11232
6e0b88f1 11233 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11234 ret = elf_link_output_symstrtab (flinfo, name,
11235 &sym, sec,
11236 NULL);
6e0b88f1 11237 if (ret == 0)
c152c796 11238 return FALSE;
6e0b88f1 11239 else if (ret == 1)
8b127cbc 11240 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11241 else
11242 abort ();
c152c796
AM
11243 }
11244
8b127cbc 11245 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11246 }
11247
11248 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11249 | (irela->r_info & r_type_mask));
11250 }
11251
11252 /* Swap out the relocs. */
d4730f92
BS
11253 input_rel_hdr = esdi->rel.hdr;
11254 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11255 {
d4730f92
BS
11256 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11257 input_rel_hdr,
11258 internal_relocs,
11259 rel_hash_list))
11260 return FALSE;
c152c796
AM
11261 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11262 * bed->s->int_rels_per_ext_rel);
eac338cf 11263 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11264 }
11265
11266 input_rela_hdr = esdi->rela.hdr;
11267 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11268 {
eac338cf 11269 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11270 input_rela_hdr,
eac338cf 11271 internal_relocs,
d4730f92 11272 rela_hash_list))
c152c796
AM
11273 return FALSE;
11274 }
11275 }
11276 }
11277
11278 /* Write out the modified section contents. */
11279 if (bed->elf_backend_write_section
8b127cbc 11280 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11281 contents))
c152c796
AM
11282 {
11283 /* Section written out. */
11284 }
11285 else switch (o->sec_info_type)
11286 {
dbaa2011 11287 case SEC_INFO_TYPE_STABS:
c152c796
AM
11288 if (! (_bfd_write_section_stabs
11289 (output_bfd,
8b127cbc 11290 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11291 o, &elf_section_data (o)->sec_info, contents)))
11292 return FALSE;
11293 break;
dbaa2011 11294 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11295 if (! _bfd_write_merged_section (output_bfd, o,
11296 elf_section_data (o)->sec_info))
11297 return FALSE;
11298 break;
dbaa2011 11299 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11300 {
8b127cbc 11301 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11302 o, contents))
11303 return FALSE;
11304 }
11305 break;
2f0c68f2
CM
11306 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11307 {
11308 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11309 flinfo->info,
11310 o, contents))
11311 return FALSE;
11312 }
11313 break;
c152c796
AM
11314 default:
11315 {
310fd250
L
11316 if (! (o->flags & SEC_EXCLUDE))
11317 {
11318 file_ptr offset = (file_ptr) o->output_offset;
11319 bfd_size_type todo = o->size;
37b01f6a 11320
bb294208 11321 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11322
310fd250
L
11323 if ((o->flags & SEC_ELF_REVERSE_COPY))
11324 {
11325 /* Reverse-copy input section to output. */
11326 do
11327 {
11328 todo -= address_size;
11329 if (! bfd_set_section_contents (output_bfd,
11330 o->output_section,
11331 contents + todo,
11332 offset,
11333 address_size))
11334 return FALSE;
11335 if (todo == 0)
11336 break;
11337 offset += address_size;
11338 }
11339 while (1);
11340 }
11341 else if (! bfd_set_section_contents (output_bfd,
11342 o->output_section,
11343 contents,
11344 offset, todo))
11345 return FALSE;
11346 }
c152c796
AM
11347 }
11348 break;
11349 }
11350 }
11351
11352 return TRUE;
11353}
11354
11355/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11356 requested by the linker, and does not come from any input file. This
c152c796
AM
11357 is used to build constructor and destructor tables when linking
11358 with -Ur. */
11359
11360static bfd_boolean
11361elf_reloc_link_order (bfd *output_bfd,
11362 struct bfd_link_info *info,
11363 asection *output_section,
11364 struct bfd_link_order *link_order)
11365{
11366 reloc_howto_type *howto;
11367 long indx;
11368 bfd_vma offset;
11369 bfd_vma addend;
d4730f92 11370 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11371 struct elf_link_hash_entry **rel_hash_ptr;
11372 Elf_Internal_Shdr *rel_hdr;
11373 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11374 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11375 bfd_byte *erel;
11376 unsigned int i;
d4730f92 11377 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11378
11379 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11380 if (howto == NULL)
11381 {
11382 bfd_set_error (bfd_error_bad_value);
11383 return FALSE;
11384 }
11385
11386 addend = link_order->u.reloc.p->addend;
11387
d4730f92
BS
11388 if (esdo->rel.hdr)
11389 reldata = &esdo->rel;
11390 else if (esdo->rela.hdr)
11391 reldata = &esdo->rela;
11392 else
11393 {
11394 reldata = NULL;
11395 BFD_ASSERT (0);
11396 }
11397
c152c796 11398 /* Figure out the symbol index. */
d4730f92 11399 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11400 if (link_order->type == bfd_section_reloc_link_order)
11401 {
11402 indx = link_order->u.reloc.p->u.section->target_index;
11403 BFD_ASSERT (indx != 0);
11404 *rel_hash_ptr = NULL;
11405 }
11406 else
11407 {
11408 struct elf_link_hash_entry *h;
11409
11410 /* Treat a reloc against a defined symbol as though it were
11411 actually against the section. */
11412 h = ((struct elf_link_hash_entry *)
11413 bfd_wrapped_link_hash_lookup (output_bfd, info,
11414 link_order->u.reloc.p->u.name,
11415 FALSE, FALSE, TRUE));
11416 if (h != NULL
11417 && (h->root.type == bfd_link_hash_defined
11418 || h->root.type == bfd_link_hash_defweak))
11419 {
11420 asection *section;
11421
11422 section = h->root.u.def.section;
11423 indx = section->output_section->target_index;
11424 *rel_hash_ptr = NULL;
11425 /* It seems that we ought to add the symbol value to the
11426 addend here, but in practice it has already been added
11427 because it was passed to constructor_callback. */
11428 addend += section->output_section->vma + section->output_offset;
11429 }
11430 else if (h != NULL)
11431 {
11432 /* Setting the index to -2 tells elf_link_output_extsym that
11433 this symbol is used by a reloc. */
11434 h->indx = -2;
11435 *rel_hash_ptr = h;
11436 indx = 0;
11437 }
11438 else
11439 {
1a72702b
AM
11440 (*info->callbacks->unattached_reloc)
11441 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11442 indx = 0;
11443 }
11444 }
11445
11446 /* If this is an inplace reloc, we must write the addend into the
11447 object file. */
11448 if (howto->partial_inplace && addend != 0)
11449 {
11450 bfd_size_type size;
11451 bfd_reloc_status_type rstat;
11452 bfd_byte *buf;
11453 bfd_boolean ok;
11454 const char *sym_name;
bb294208 11455 bfd_size_type octets;
c152c796 11456
a50b1753
NC
11457 size = (bfd_size_type) bfd_get_reloc_size (howto);
11458 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11459 if (buf == NULL && size != 0)
c152c796
AM
11460 return FALSE;
11461 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11462 switch (rstat)
11463 {
11464 case bfd_reloc_ok:
11465 break;
11466
11467 default:
11468 case bfd_reloc_outofrange:
11469 abort ();
11470
11471 case bfd_reloc_overflow:
11472 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11473 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11474 else
11475 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11476 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11477 howto->name, addend, NULL, NULL,
11478 (bfd_vma) 0);
c152c796
AM
11479 break;
11480 }
37b01f6a 11481
bb294208
AM
11482 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11483 output_section);
c152c796 11484 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11485 octets, size);
c152c796
AM
11486 free (buf);
11487 if (! ok)
11488 return FALSE;
11489 }
11490
11491 /* The address of a reloc is relative to the section in a
11492 relocatable file, and is a virtual address in an executable
11493 file. */
11494 offset = link_order->offset;
0e1862bb 11495 if (! bfd_link_relocatable (info))
c152c796
AM
11496 offset += output_section->vma;
11497
11498 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11499 {
11500 irel[i].r_offset = offset;
11501 irel[i].r_info = 0;
11502 irel[i].r_addend = 0;
11503 }
11504 if (bed->s->arch_size == 32)
11505 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11506 else
11507 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11508
d4730f92 11509 rel_hdr = reldata->hdr;
c152c796
AM
11510 erel = rel_hdr->contents;
11511 if (rel_hdr->sh_type == SHT_REL)
11512 {
d4730f92 11513 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11514 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11515 }
11516 else
11517 {
11518 irel[0].r_addend = addend;
d4730f92 11519 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11520 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11521 }
11522
d4730f92 11523 ++reldata->count;
c152c796
AM
11524
11525 return TRUE;
11526}
11527
0b52efa6 11528
0b52efa6
PB
11529/* Compare two sections based on the locations of the sections they are
11530 linked to. Used by elf_fixup_link_order. */
11531
11532static int
8c1c5e5d 11533compare_link_order (const void *a, const void *b)
0b52efa6 11534{
8c1c5e5d
AM
11535 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11536 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11537 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11538 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11539 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11540 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11541
0b52efa6
PB
11542 if (apos < bpos)
11543 return -1;
8c1c5e5d
AM
11544 if (apos > bpos)
11545 return 1;
11546
11547 /* The only way we should get matching LMAs is when the first of two
11548 sections has zero size. */
11549 if (asec->size < bsec->size)
11550 return -1;
11551 if (asec->size > bsec->size)
11552 return 1;
11553
11554 /* If they are both zero size then they almost certainly have the same
11555 VMA and thus are not ordered with respect to each other. Test VMA
11556 anyway, and fall back to id to make the result reproducible across
11557 qsort implementations. */
11558 apos = asec->output_section->vma + asec->output_offset;
11559 bpos = bsec->output_section->vma + bsec->output_offset;
11560 if (apos < bpos)
11561 return -1;
11562 if (apos > bpos)
11563 return 1;
11564
11565 return asec->id - bsec->id;
0b52efa6
PB
11566}
11567
11568
11569/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11570 order as their linked sections. Returns false if this could not be done
11571 because an output section includes both ordered and unordered
11572 sections. Ideally we'd do this in the linker proper. */
11573
11574static bfd_boolean
11575elf_fixup_link_order (bfd *abfd, asection *o)
11576{
8c1c5e5d
AM
11577 size_t seen_linkorder;
11578 size_t seen_other;
11579 size_t n;
0b52efa6
PB
11580 struct bfd_link_order *p;
11581 bfd *sub;
0b52efa6 11582 struct bfd_link_order **sections;
66631823
CE
11583 asection *other_sec, *linkorder_sec;
11584 bfd_vma offset; /* Octets. */
3b36f7e6 11585
d33cdfe3
L
11586 other_sec = NULL;
11587 linkorder_sec = NULL;
0b52efa6
PB
11588 seen_other = 0;
11589 seen_linkorder = 0;
8423293d 11590 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11591 {
d33cdfe3 11592 if (p->type == bfd_indirect_link_order)
0b52efa6 11593 {
66631823 11594 asection *s = p->u.indirect.section;
d33cdfe3 11595 sub = s->owner;
847d5183
AM
11596 if ((s->flags & SEC_LINKER_CREATED) == 0
11597 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11598 && elf_section_data (s) != NULL
11599 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11600 {
11601 seen_linkorder++;
11602 linkorder_sec = s;
11603 }
0b52efa6 11604 else
d33cdfe3
L
11605 {
11606 seen_other++;
11607 other_sec = s;
11608 }
0b52efa6
PB
11609 }
11610 else
11611 seen_other++;
d33cdfe3
L
11612
11613 if (seen_other && seen_linkorder)
11614 {
11615 if (other_sec && linkorder_sec)
4eca0228 11616 _bfd_error_handler
695344c0 11617 /* xgettext:c-format */
871b3ab2
AM
11618 (_("%pA has both ordered [`%pA' in %pB] "
11619 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11620 o, linkorder_sec, linkorder_sec->owner,
11621 other_sec, other_sec->owner);
d33cdfe3 11622 else
4eca0228 11623 _bfd_error_handler
871b3ab2 11624 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11625 bfd_set_error (bfd_error_bad_value);
11626 return FALSE;
11627 }
0b52efa6
PB
11628 }
11629
11630 if (!seen_linkorder)
11631 return TRUE;
11632
8c1c5e5d 11633 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11634 if (sections == NULL)
11635 return FALSE;
3b36f7e6 11636
8c1c5e5d 11637 seen_linkorder = 0;
8423293d 11638 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11639 sections[seen_linkorder++] = p;
11640
0b52efa6 11641 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11642 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11643
11644 /* Change the offsets of the sections. */
11645 offset = 0;
11646 for (n = 0; n < seen_linkorder; n++)
11647 {
8c1c5e5d 11648 bfd_vma mask;
66631823
CE
11649 asection *s = sections[n]->u.indirect.section;
11650 unsigned int opb = bfd_octets_per_byte (abfd, s);
11651
11652 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11653 offset = (offset + ~mask) & mask;
66631823 11654 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11655 offset += sections[n]->size;
11656 }
11657
4dd07732 11658 free (sections);
0b52efa6
PB
11659 return TRUE;
11660}
11661
76359541
TP
11662/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11663 Returns TRUE upon success, FALSE otherwise. */
11664
11665static bfd_boolean
11666elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11667{
11668 bfd_boolean ret = FALSE;
11669 bfd *implib_bfd;
11670 const struct elf_backend_data *bed;
11671 flagword flags;
11672 enum bfd_architecture arch;
11673 unsigned int mach;
11674 asymbol **sympp = NULL;
11675 long symsize;
11676 long symcount;
11677 long src_count;
11678 elf_symbol_type *osymbuf;
446f7ed5 11679 size_t amt;
76359541
TP
11680
11681 implib_bfd = info->out_implib_bfd;
11682 bed = get_elf_backend_data (abfd);
11683
11684 if (!bfd_set_format (implib_bfd, bfd_object))
11685 return FALSE;
11686
046734ff 11687 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11688 flags = bfd_get_file_flags (abfd);
11689 flags &= ~HAS_RELOC;
11690 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11691 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11692 return FALSE;
11693
11694 /* Copy architecture of output file to import library file. */
11695 arch = bfd_get_arch (abfd);
11696 mach = bfd_get_mach (abfd);
11697 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11698 && (abfd->target_defaulted
11699 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11700 return FALSE;
11701
11702 /* Get symbol table size. */
11703 symsize = bfd_get_symtab_upper_bound (abfd);
11704 if (symsize < 0)
11705 return FALSE;
11706
11707 /* Read in the symbol table. */
ec9bd0a2
AM
11708 sympp = (asymbol **) bfd_malloc (symsize);
11709 if (sympp == NULL)
11710 return FALSE;
11711
76359541
TP
11712 symcount = bfd_canonicalize_symtab (abfd, sympp);
11713 if (symcount < 0)
11714 goto free_sym_buf;
11715
11716 /* Allow the BFD backend to copy any private header data it
11717 understands from the output BFD to the import library BFD. */
11718 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11719 goto free_sym_buf;
11720
11721 /* Filter symbols to appear in the import library. */
11722 if (bed->elf_backend_filter_implib_symbols)
11723 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11724 symcount);
11725 else
11726 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11727 if (symcount == 0)
11728 {
5df1bc57 11729 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11730 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11731 implib_bfd);
76359541
TP
11732 goto free_sym_buf;
11733 }
11734
11735
11736 /* Make symbols absolute. */
446f7ed5
AM
11737 amt = symcount * sizeof (*osymbuf);
11738 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11739 if (osymbuf == NULL)
11740 goto free_sym_buf;
11741
76359541
TP
11742 for (src_count = 0; src_count < symcount; src_count++)
11743 {
11744 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11745 sizeof (*osymbuf));
11746 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11747 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11748 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11749 osymbuf[src_count].internal_elf_sym.st_value =
11750 osymbuf[src_count].symbol.value;
11751 sympp[src_count] = &osymbuf[src_count].symbol;
11752 }
11753
11754 bfd_set_symtab (implib_bfd, sympp, symcount);
11755
11756 /* Allow the BFD backend to copy any private data it understands
11757 from the output BFD to the import library BFD. This is done last
11758 to permit the routine to look at the filtered symbol table. */
11759 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11760 goto free_sym_buf;
11761
11762 if (!bfd_close (implib_bfd))
11763 goto free_sym_buf;
11764
11765 ret = TRUE;
11766
dc1e8a47 11767 free_sym_buf:
76359541
TP
11768 free (sympp);
11769 return ret;
11770}
11771
9f7c3e5e
AM
11772static void
11773elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11774{
11775 asection *o;
11776
11777 if (flinfo->symstrtab != NULL)
ef10c3ac 11778 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11779 if (flinfo->contents != NULL)
11780 free (flinfo->contents);
11781 if (flinfo->external_relocs != NULL)
11782 free (flinfo->external_relocs);
11783 if (flinfo->internal_relocs != NULL)
11784 free (flinfo->internal_relocs);
11785 if (flinfo->external_syms != NULL)
11786 free (flinfo->external_syms);
11787 if (flinfo->locsym_shndx != NULL)
11788 free (flinfo->locsym_shndx);
11789 if (flinfo->internal_syms != NULL)
11790 free (flinfo->internal_syms);
11791 if (flinfo->indices != NULL)
11792 free (flinfo->indices);
11793 if (flinfo->sections != NULL)
11794 free (flinfo->sections);
a0f6fd21
AM
11795 if (flinfo->symshndxbuf != NULL
11796 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11797 free (flinfo->symshndxbuf);
11798 for (o = obfd->sections; o != NULL; o = o->next)
11799 {
11800 struct bfd_elf_section_data *esdo = elf_section_data (o);
11801 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11802 free (esdo->rel.hashes);
11803 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11804 free (esdo->rela.hashes);
11805 }
11806}
0b52efa6 11807
c152c796
AM
11808/* Do the final step of an ELF link. */
11809
11810bfd_boolean
11811bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11812{
11813 bfd_boolean dynamic;
11814 bfd_boolean emit_relocs;
11815 bfd *dynobj;
8b127cbc 11816 struct elf_final_link_info flinfo;
91d6fa6a
NC
11817 asection *o;
11818 struct bfd_link_order *p;
11819 bfd *sub;
c152c796
AM
11820 bfd_size_type max_contents_size;
11821 bfd_size_type max_external_reloc_size;
11822 bfd_size_type max_internal_reloc_count;
11823 bfd_size_type max_sym_count;
11824 bfd_size_type max_sym_shndx_count;
c152c796
AM
11825 Elf_Internal_Sym elfsym;
11826 unsigned int i;
11827 Elf_Internal_Shdr *symtab_hdr;
11828 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11830 struct elf_outext_info eoinfo;
11831 bfd_boolean merged;
11832 size_t relativecount = 0;
11833 asection *reldyn = 0;
11834 bfd_size_type amt;
104d59d1
JM
11835 asection *attr_section = NULL;
11836 bfd_vma attr_size = 0;
11837 const char *std_attrs_section;
64f52338 11838 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 11839 bfd_boolean sections_removed;
c152c796 11840
64f52338 11841 if (!is_elf_hash_table (htab))
c152c796
AM
11842 return FALSE;
11843
0e1862bb 11844 if (bfd_link_pic (info))
c152c796
AM
11845 abfd->flags |= DYNAMIC;
11846
64f52338
AM
11847 dynamic = htab->dynamic_sections_created;
11848 dynobj = htab->dynobj;
c152c796 11849
0e1862bb 11850 emit_relocs = (bfd_link_relocatable (info)
a4676736 11851 || info->emitrelocations);
c152c796 11852
8b127cbc
AM
11853 flinfo.info = info;
11854 flinfo.output_bfd = abfd;
ef10c3ac 11855 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11856 if (flinfo.symstrtab == NULL)
c152c796
AM
11857 return FALSE;
11858
11859 if (! dynamic)
11860 {
8b127cbc
AM
11861 flinfo.hash_sec = NULL;
11862 flinfo.symver_sec = NULL;
c152c796
AM
11863 }
11864 else
11865 {
3d4d4302 11866 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11867 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11868 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11869 /* Note that it is OK if symver_sec is NULL. */
11870 }
11871
8b127cbc
AM
11872 flinfo.contents = NULL;
11873 flinfo.external_relocs = NULL;
11874 flinfo.internal_relocs = NULL;
11875 flinfo.external_syms = NULL;
11876 flinfo.locsym_shndx = NULL;
11877 flinfo.internal_syms = NULL;
11878 flinfo.indices = NULL;
11879 flinfo.sections = NULL;
8b127cbc 11880 flinfo.symshndxbuf = NULL;
ffbc01cc 11881 flinfo.filesym_count = 0;
c152c796 11882
104d59d1
JM
11883 /* The object attributes have been merged. Remove the input
11884 sections from the link, and set the contents of the output
1ff6de03 11885 section. */
4c6ee646 11886 sections_removed = FALSE;
104d59d1
JM
11887 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11888 for (o = abfd->sections; o != NULL; o = o->next)
11889 {
5270eddc 11890 bfd_boolean remove_section = FALSE;
b8a6ced7 11891
104d59d1
JM
11892 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11893 || strcmp (o->name, ".gnu.attributes") == 0)
11894 {
11895 for (p = o->map_head.link_order; p != NULL; p = p->next)
11896 {
11897 asection *input_section;
11898
11899 if (p->type != bfd_indirect_link_order)
11900 continue;
11901 input_section = p->u.indirect.section;
11902 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11903 elf_link_input_bfd ignores this section. */
11904 input_section->flags &= ~SEC_HAS_CONTENTS;
11905 }
a0c8462f 11906
104d59d1 11907 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 11908 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
11909 /* Skip this section later on. */
11910 o->map_head.link_order = NULL;
104d59d1 11911 if (attr_size)
b8a6ced7 11912 attr_section = o;
104d59d1 11913 else
5270eddc 11914 remove_section = TRUE;
104d59d1 11915 }
6e5e9d58
AM
11916 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11917 {
11918 /* Remove empty group section from linker output. */
5270eddc 11919 remove_section = TRUE;
b8a6ced7 11920 }
5270eddc 11921 if (remove_section)
b8a6ced7 11922 {
6e5e9d58
AM
11923 o->flags |= SEC_EXCLUDE;
11924 bfd_section_list_remove (abfd, o);
11925 abfd->section_count--;
4c6ee646 11926 sections_removed = TRUE;
6e5e9d58 11927 }
104d59d1 11928 }
4c6ee646
AM
11929 if (sections_removed)
11930 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 11931
c152c796
AM
11932 /* Count up the number of relocations we will output for each output
11933 section, so that we know the sizes of the reloc sections. We
11934 also figure out some maximum sizes. */
11935 max_contents_size = 0;
11936 max_external_reloc_size = 0;
11937 max_internal_reloc_count = 0;
11938 max_sym_count = 0;
11939 max_sym_shndx_count = 0;
11940 merged = FALSE;
11941 for (o = abfd->sections; o != NULL; o = o->next)
11942 {
11943 struct bfd_elf_section_data *esdo = elf_section_data (o);
11944 o->reloc_count = 0;
11945
8423293d 11946 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11947 {
11948 unsigned int reloc_count = 0;
9eaff861 11949 unsigned int additional_reloc_count = 0;
c152c796 11950 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11951
11952 if (p->type == bfd_section_reloc_link_order
11953 || p->type == bfd_symbol_reloc_link_order)
11954 reloc_count = 1;
11955 else if (p->type == bfd_indirect_link_order)
11956 {
11957 asection *sec;
11958
11959 sec = p->u.indirect.section;
c152c796
AM
11960
11961 /* Mark all sections which are to be included in the
11962 link. This will normally be every section. We need
11963 to do this so that we can identify any sections which
11964 the linker has decided to not include. */
11965 sec->linker_mark = TRUE;
11966
11967 if (sec->flags & SEC_MERGE)
11968 merged = TRUE;
11969
eea6121a
AM
11970 if (sec->rawsize > max_contents_size)
11971 max_contents_size = sec->rawsize;
11972 if (sec->size > max_contents_size)
11973 max_contents_size = sec->size;
c152c796 11974
c152c796
AM
11975 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11976 && (sec->owner->flags & DYNAMIC) == 0)
11977 {
11978 size_t sym_count;
11979
a961cdd5
AM
11980 /* We are interested in just local symbols, not all
11981 symbols. */
c152c796
AM
11982 if (elf_bad_symtab (sec->owner))
11983 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11984 / bed->s->sizeof_sym);
11985 else
11986 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11987
11988 if (sym_count > max_sym_count)
11989 max_sym_count = sym_count;
11990
11991 if (sym_count > max_sym_shndx_count
6a40cf0c 11992 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11993 max_sym_shndx_count = sym_count;
11994
a961cdd5
AM
11995 if (esdo->this_hdr.sh_type == SHT_REL
11996 || esdo->this_hdr.sh_type == SHT_RELA)
11997 /* Some backends use reloc_count in relocation sections
11998 to count particular types of relocs. Of course,
11999 reloc sections themselves can't have relocations. */
12000 ;
12001 else if (emit_relocs)
12002 {
12003 reloc_count = sec->reloc_count;
12004 if (bed->elf_backend_count_additional_relocs)
12005 {
12006 int c;
12007 c = (*bed->elf_backend_count_additional_relocs) (sec);
12008 additional_reloc_count += c;
12009 }
12010 }
12011 else if (bed->elf_backend_count_relocs)
12012 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12013
12014 esdi = elf_section_data (sec);
12015
c152c796
AM
12016 if ((sec->flags & SEC_RELOC) != 0)
12017 {
d4730f92 12018 size_t ext_size = 0;
c152c796 12019
d4730f92
BS
12020 if (esdi->rel.hdr != NULL)
12021 ext_size = esdi->rel.hdr->sh_size;
12022 if (esdi->rela.hdr != NULL)
12023 ext_size += esdi->rela.hdr->sh_size;
7326c758 12024
c152c796
AM
12025 if (ext_size > max_external_reloc_size)
12026 max_external_reloc_size = ext_size;
12027 if (sec->reloc_count > max_internal_reloc_count)
12028 max_internal_reloc_count = sec->reloc_count;
12029 }
12030 }
12031 }
12032
12033 if (reloc_count == 0)
12034 continue;
12035
9eaff861 12036 reloc_count += additional_reloc_count;
c152c796
AM
12037 o->reloc_count += reloc_count;
12038
0e1862bb 12039 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12040 {
d4730f92 12041 if (esdi->rel.hdr)
9eaff861 12042 {
491d01d3 12043 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12044 esdo->rel.count += additional_reloc_count;
12045 }
d4730f92 12046 if (esdi->rela.hdr)
9eaff861 12047 {
491d01d3 12048 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12049 esdo->rela.count += additional_reloc_count;
12050 }
d4730f92
BS
12051 }
12052 else
12053 {
12054 if (o->use_rela_p)
12055 esdo->rela.count += reloc_count;
2c2b4ed4 12056 else
d4730f92 12057 esdo->rel.count += reloc_count;
c152c796 12058 }
c152c796
AM
12059 }
12060
9eaff861 12061 if (o->reloc_count > 0)
c152c796
AM
12062 o->flags |= SEC_RELOC;
12063 else
12064 {
12065 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12066 set it (this is probably a bug) and if it is set
12067 assign_section_numbers will create a reloc section. */
12068 o->flags &=~ SEC_RELOC;
12069 }
12070
12071 /* If the SEC_ALLOC flag is not set, force the section VMA to
12072 zero. This is done in elf_fake_sections as well, but forcing
12073 the VMA to 0 here will ensure that relocs against these
12074 sections are handled correctly. */
12075 if ((o->flags & SEC_ALLOC) == 0
12076 && ! o->user_set_vma)
12077 o->vma = 0;
12078 }
12079
0e1862bb 12080 if (! bfd_link_relocatable (info) && merged)
64f52338 12081 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12082
12083 /* Figure out the file positions for everything but the symbol table
12084 and the relocs. We set symcount to force assign_section_numbers
12085 to create a symbol table. */
ed48ec2e 12086 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12087 BFD_ASSERT (! abfd->output_has_begun);
12088 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12089 goto error_return;
12090
ee75fd95 12091 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12092 for (o = abfd->sections; o != NULL; o = o->next)
12093 {
d4730f92 12094 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12095 if ((o->flags & SEC_RELOC) != 0)
12096 {
d4730f92 12097 if (esdo->rel.hdr
9eaff861 12098 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12099 goto error_return;
12100
d4730f92 12101 if (esdo->rela.hdr
9eaff861 12102 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12103 goto error_return;
12104 }
12105
48db3297
AM
12106 /* _bfd_elf_compute_section_file_positions makes temporary use
12107 of target_index. Reset it. */
12108 o->target_index = 0;
12109
c152c796
AM
12110 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12111 to count upwards while actually outputting the relocations. */
d4730f92
BS
12112 esdo->rel.count = 0;
12113 esdo->rela.count = 0;
0ce398f1 12114
1ff6de03
NA
12115 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12116 && !bfd_section_is_ctf (o))
0ce398f1
L
12117 {
12118 /* Cache the section contents so that they can be compressed
12119 later. Use bfd_malloc since it will be freed by
12120 bfd_compress_section_contents. */
12121 unsigned char *contents = esdo->this_hdr.contents;
12122 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12123 abort ();
12124 contents
12125 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12126 if (contents == NULL)
12127 goto error_return;
12128 esdo->this_hdr.contents = contents;
12129 }
c152c796
AM
12130 }
12131
1ff6de03
NA
12132 /* We have now assigned file positions for all the sections except .symtab,
12133 .strtab, and non-loaded reloc and compressed debugging sections. We start
12134 the .symtab section at the current file position, and write directly to it.
12135 We build the .strtab section in memory. */
ed48ec2e 12136 abfd->symcount = 0;
c152c796
AM
12137 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12138 /* sh_name is set in prep_headers. */
12139 symtab_hdr->sh_type = SHT_SYMTAB;
12140 /* sh_flags, sh_addr and sh_size all start off zero. */
12141 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12142 /* sh_link is set in assign_section_numbers. */
12143 /* sh_info is set below. */
12144 /* sh_offset is set just below. */
72de5009 12145 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12146
ef10c3ac
L
12147 if (max_sym_count < 20)
12148 max_sym_count = 20;
64f52338 12149 htab->strtabsize = max_sym_count;
ef10c3ac 12150 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12151 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12152 if (htab->strtab == NULL)
c152c796 12153 goto error_return;
ef10c3ac
L
12154 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12155 flinfo.symshndxbuf
12156 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12157 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12158
8539e4e8 12159 if (info->strip != strip_all || emit_relocs)
c152c796 12160 {
8539e4e8
AM
12161 file_ptr off = elf_next_file_pos (abfd);
12162
12163 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12164
12165 /* Note that at this point elf_next_file_pos (abfd) is
12166 incorrect. We do not yet know the size of the .symtab section.
12167 We correct next_file_pos below, after we do know the size. */
12168
12169 /* Start writing out the symbol table. The first symbol is always a
12170 dummy symbol. */
c152c796
AM
12171 elfsym.st_value = 0;
12172 elfsym.st_size = 0;
12173 elfsym.st_info = 0;
12174 elfsym.st_other = 0;
12175 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12176 elfsym.st_target_internal = 0;
ef10c3ac
L
12177 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12178 bfd_und_section_ptr, NULL) != 1)
c152c796 12179 goto error_return;
c152c796 12180
8539e4e8
AM
12181 /* Output a symbol for each section. We output these even if we are
12182 discarding local symbols, since they are used for relocs. These
12183 symbols have no names. We store the index of each one in the
12184 index field of the section, so that we can find it again when
12185 outputting relocs. */
12186
c152c796
AM
12187 elfsym.st_size = 0;
12188 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12189 elfsym.st_other = 0;
f0b5bb34 12190 elfsym.st_value = 0;
35fc36a8 12191 elfsym.st_target_internal = 0;
c152c796
AM
12192 for (i = 1; i < elf_numsections (abfd); i++)
12193 {
12194 o = bfd_section_from_elf_index (abfd, i);
12195 if (o != NULL)
f0b5bb34
AM
12196 {
12197 o->target_index = bfd_get_symcount (abfd);
12198 elfsym.st_shndx = i;
0e1862bb 12199 if (!bfd_link_relocatable (info))
f0b5bb34 12200 elfsym.st_value = o->vma;
ef10c3ac
L
12201 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12202 NULL) != 1)
f0b5bb34
AM
12203 goto error_return;
12204 }
c152c796
AM
12205 }
12206 }
12207
12208 /* Allocate some memory to hold information read in from the input
12209 files. */
12210 if (max_contents_size != 0)
12211 {
8b127cbc
AM
12212 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12213 if (flinfo.contents == NULL)
c152c796
AM
12214 goto error_return;
12215 }
12216
12217 if (max_external_reloc_size != 0)
12218 {
8b127cbc
AM
12219 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12220 if (flinfo.external_relocs == NULL)
c152c796
AM
12221 goto error_return;
12222 }
12223
12224 if (max_internal_reloc_count != 0)
12225 {
056bafd4 12226 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12227 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12228 if (flinfo.internal_relocs == NULL)
c152c796
AM
12229 goto error_return;
12230 }
12231
12232 if (max_sym_count != 0)
12233 {
12234 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12235 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12236 if (flinfo.external_syms == NULL)
c152c796
AM
12237 goto error_return;
12238
12239 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12240 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12241 if (flinfo.internal_syms == NULL)
c152c796
AM
12242 goto error_return;
12243
12244 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12245 flinfo.indices = (long int *) bfd_malloc (amt);
12246 if (flinfo.indices == NULL)
c152c796
AM
12247 goto error_return;
12248
12249 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12250 flinfo.sections = (asection **) bfd_malloc (amt);
12251 if (flinfo.sections == NULL)
c152c796
AM
12252 goto error_return;
12253 }
12254
12255 if (max_sym_shndx_count != 0)
12256 {
12257 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12258 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12259 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12260 goto error_return;
12261 }
12262
64f52338 12263 if (htab->tls_sec)
c152c796 12264 {
66631823 12265 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12266 asection *sec;
12267
64f52338 12268 for (sec = htab->tls_sec;
c152c796
AM
12269 sec && (sec->flags & SEC_THREAD_LOCAL);
12270 sec = sec->next)
12271 {
3a800eb9 12272 bfd_size_type size = sec->size;
66631823 12273 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12274
3a800eb9
AM
12275 if (size == 0
12276 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12277 {
91d6fa6a
NC
12278 struct bfd_link_order *ord = sec->map_tail.link_order;
12279
12280 if (ord != NULL)
66631823 12281 size = ord->offset * opb + ord->size;
c152c796 12282 }
66631823 12283 end = sec->vma + size / opb;
c152c796 12284 }
64f52338 12285 base = htab->tls_sec->vma;
7dc98aea
RO
12286 /* Only align end of TLS section if static TLS doesn't have special
12287 alignment requirements. */
12288 if (bed->static_tls_alignment == 1)
64f52338
AM
12289 end = align_power (end, htab->tls_sec->alignment_power);
12290 htab->tls_size = end - base;
c152c796
AM
12291 }
12292
0b52efa6
PB
12293 /* Reorder SHF_LINK_ORDER sections. */
12294 for (o = abfd->sections; o != NULL; o = o->next)
12295 {
12296 if (!elf_fixup_link_order (abfd, o))
12297 return FALSE;
12298 }
12299
2f0c68f2
CM
12300 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12301 return FALSE;
12302
c152c796
AM
12303 /* Since ELF permits relocations to be against local symbols, we
12304 must have the local symbols available when we do the relocations.
12305 Since we would rather only read the local symbols once, and we
12306 would rather not keep them in memory, we handle all the
12307 relocations for a single input file at the same time.
12308
12309 Unfortunately, there is no way to know the total number of local
12310 symbols until we have seen all of them, and the local symbol
12311 indices precede the global symbol indices. This means that when
12312 we are generating relocatable output, and we see a reloc against
12313 a global symbol, we can not know the symbol index until we have
12314 finished examining all the local symbols to see which ones we are
12315 going to output. To deal with this, we keep the relocations in
12316 memory, and don't output them until the end of the link. This is
12317 an unfortunate waste of memory, but I don't see a good way around
12318 it. Fortunately, it only happens when performing a relocatable
12319 link, which is not the common case. FIXME: If keep_memory is set
12320 we could write the relocs out and then read them again; I don't
12321 know how bad the memory loss will be. */
12322
c72f2fb2 12323 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12324 sub->output_has_begun = FALSE;
12325 for (o = abfd->sections; o != NULL; o = o->next)
12326 {
8423293d 12327 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12328 {
12329 if (p->type == bfd_indirect_link_order
12330 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12331 == bfd_target_elf_flavour)
12332 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12333 {
12334 if (! sub->output_has_begun)
12335 {
8b127cbc 12336 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12337 goto error_return;
12338 sub->output_has_begun = TRUE;
12339 }
12340 }
12341 else if (p->type == bfd_section_reloc_link_order
12342 || p->type == bfd_symbol_reloc_link_order)
12343 {
12344 if (! elf_reloc_link_order (abfd, info, o, p))
12345 goto error_return;
12346 }
12347 else
12348 {
12349 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12350 {
12351 if (p->type == bfd_indirect_link_order
12352 && (bfd_get_flavour (sub)
12353 == bfd_target_elf_flavour)
12354 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12355 != bed->s->elfclass))
12356 {
12357 const char *iclass, *oclass;
12358
aebf9be7 12359 switch (bed->s->elfclass)
351f65ca 12360 {
aebf9be7
NC
12361 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12362 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12363 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12364 default: abort ();
351f65ca 12365 }
aebf9be7
NC
12366
12367 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12368 {
aebf9be7
NC
12369 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12370 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12371 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12372 default: abort ();
351f65ca
L
12373 }
12374
12375 bfd_set_error (bfd_error_wrong_format);
4eca0228 12376 _bfd_error_handler
695344c0 12377 /* xgettext:c-format */
871b3ab2 12378 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12379 sub, iclass, oclass);
12380 }
12381
12382 goto error_return;
12383 }
c152c796
AM
12384 }
12385 }
12386 }
12387
c0f00686
L
12388 /* Free symbol buffer if needed. */
12389 if (!info->reduce_memory_overheads)
12390 {
c72f2fb2 12391 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12392 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12393 && elf_tdata (sub)->symbuf)
c0f00686
L
12394 {
12395 free (elf_tdata (sub)->symbuf);
12396 elf_tdata (sub)->symbuf = NULL;
12397 }
12398 }
12399
c152c796
AM
12400 /* Output any global symbols that got converted to local in a
12401 version script or due to symbol visibility. We do this in a
12402 separate step since ELF requires all local symbols to appear
12403 prior to any global symbols. FIXME: We should only do this if
12404 some global symbols were, in fact, converted to become local.
12405 FIXME: Will this work correctly with the Irix 5 linker? */
12406 eoinfo.failed = FALSE;
8b127cbc 12407 eoinfo.flinfo = &flinfo;
c152c796 12408 eoinfo.localsyms = TRUE;
34a79995 12409 eoinfo.file_sym_done = FALSE;
7686d77d 12410 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12411 if (eoinfo.failed)
12412 return FALSE;
12413
4e617b1e
PB
12414 /* If backend needs to output some local symbols not present in the hash
12415 table, do it now. */
8539e4e8
AM
12416 if (bed->elf_backend_output_arch_local_syms
12417 && (info->strip != strip_all || emit_relocs))
4e617b1e 12418 {
6e0b88f1 12419 typedef int (*out_sym_func)
4e617b1e
PB
12420 (void *, const char *, Elf_Internal_Sym *, asection *,
12421 struct elf_link_hash_entry *);
12422
12423 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12424 (abfd, info, &flinfo,
12425 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12426 return FALSE;
12427 }
12428
c152c796
AM
12429 /* That wrote out all the local symbols. Finish up the symbol table
12430 with the global symbols. Even if we want to strip everything we
12431 can, we still need to deal with those global symbols that got
12432 converted to local in a version script. */
12433
12434 /* The sh_info field records the index of the first non local symbol. */
12435 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12436
12437 if (dynamic
64f52338
AM
12438 && htab->dynsym != NULL
12439 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12440 {
12441 Elf_Internal_Sym sym;
64f52338 12442 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12443
64f52338
AM
12444 o = htab->dynsym->output_section;
12445 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12446
12447 /* Write out the section symbols for the output sections. */
0e1862bb 12448 if (bfd_link_pic (info)
64f52338 12449 || htab->is_relocatable_executable)
c152c796
AM
12450 {
12451 asection *s;
12452
12453 sym.st_size = 0;
12454 sym.st_name = 0;
12455 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12456 sym.st_other = 0;
35fc36a8 12457 sym.st_target_internal = 0;
c152c796
AM
12458
12459 for (s = abfd->sections; s != NULL; s = s->next)
12460 {
12461 int indx;
12462 bfd_byte *dest;
12463 long dynindx;
12464
c152c796 12465 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12466 if (dynindx <= 0)
12467 continue;
12468 indx = elf_section_data (s)->this_idx;
c152c796
AM
12469 BFD_ASSERT (indx > 0);
12470 sym.st_shndx = indx;
c0d5a53d
L
12471 if (! check_dynsym (abfd, &sym))
12472 return FALSE;
c152c796
AM
12473 sym.st_value = s->vma;
12474 dest = dynsym + dynindx * bed->s->sizeof_sym;
12475 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12476 }
c152c796
AM
12477 }
12478
12479 /* Write out the local dynsyms. */
64f52338 12480 if (htab->dynlocal)
c152c796
AM
12481 {
12482 struct elf_link_local_dynamic_entry *e;
64f52338 12483 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12484 {
12485 asection *s;
12486 bfd_byte *dest;
12487
935bd1e0 12488 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12489 Note that we saved a word of storage and overwrote
12490 the original st_name with the dynstr_index. */
12491 sym = e->isym;
935bd1e0 12492 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12493
cb33740c
AM
12494 s = bfd_section_from_elf_index (e->input_bfd,
12495 e->isym.st_shndx);
12496 if (s != NULL)
c152c796 12497 {
c152c796
AM
12498 sym.st_shndx =
12499 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12500 if (! check_dynsym (abfd, &sym))
12501 return FALSE;
c152c796
AM
12502 sym.st_value = (s->output_section->vma
12503 + s->output_offset
12504 + e->isym.st_value);
12505 }
12506
c152c796
AM
12507 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12508 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12509 }
12510 }
c152c796
AM
12511 }
12512
12513 /* We get the global symbols from the hash table. */
12514 eoinfo.failed = FALSE;
12515 eoinfo.localsyms = FALSE;
8b127cbc 12516 eoinfo.flinfo = &flinfo;
7686d77d 12517 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12518 if (eoinfo.failed)
12519 return FALSE;
12520
12521 /* If backend needs to output some symbols not present in the hash
12522 table, do it now. */
8539e4e8
AM
12523 if (bed->elf_backend_output_arch_syms
12524 && (info->strip != strip_all || emit_relocs))
c152c796 12525 {
6e0b88f1 12526 typedef int (*out_sym_func)
c152c796
AM
12527 (void *, const char *, Elf_Internal_Sym *, asection *,
12528 struct elf_link_hash_entry *);
12529
12530 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12531 (abfd, info, &flinfo,
12532 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12533 return FALSE;
12534 }
12535
ef10c3ac
L
12536 /* Finalize the .strtab section. */
12537 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12538
12539 /* Swap out the .strtab section. */
12540 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12541 return FALSE;
12542
12543 /* Now we know the size of the symtab section. */
c152c796
AM
12544 if (bfd_get_symcount (abfd) > 0)
12545 {
ee3b52e9
L
12546 /* Finish up and write out the symbol string table (.strtab)
12547 section. */
ad32986f 12548 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12549 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12550
ad32986f 12551 if (elf_symtab_shndx_list (abfd))
8539e4e8 12552 {
ad32986f 12553 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12554
ad32986f
NC
12555 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12556 {
12557 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12558 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12559 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12560 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12561 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12562
ad32986f
NC
12563 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12564 off, TRUE);
12565
12566 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12567 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12568 return FALSE;
12569 }
8539e4e8 12570 }
ee3b52e9
L
12571
12572 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12573 /* sh_name was set in prep_headers. */
12574 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12575 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12576 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12577 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12578 symstrtab_hdr->sh_entsize = 0;
12579 symstrtab_hdr->sh_link = 0;
12580 symstrtab_hdr->sh_info = 0;
12581 /* sh_offset is set just below. */
12582 symstrtab_hdr->sh_addralign = 1;
12583
12584 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12585 off, TRUE);
12586 elf_next_file_pos (abfd) = off;
12587
c152c796 12588 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12589 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12590 return FALSE;
12591 }
12592
76359541
TP
12593 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12594 {
871b3ab2 12595 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12596 info->out_implib_bfd);
76359541
TP
12597 return FALSE;
12598 }
12599
c152c796
AM
12600 /* Adjust the relocs to have the correct symbol indices. */
12601 for (o = abfd->sections; o != NULL; o = o->next)
12602 {
d4730f92 12603 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12604 bfd_boolean sort;
10bbbc1d 12605
c152c796
AM
12606 if ((o->flags & SEC_RELOC) == 0)
12607 continue;
12608
28dbcedc 12609 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12610 if (esdo->rel.hdr != NULL
10bbbc1d 12611 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12612 return FALSE;
12613 if (esdo->rela.hdr != NULL
10bbbc1d 12614 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12615 return FALSE;
c152c796
AM
12616
12617 /* Set the reloc_count field to 0 to prevent write_relocs from
12618 trying to swap the relocs out itself. */
12619 o->reloc_count = 0;
12620 }
12621
12622 if (dynamic && info->combreloc && dynobj != NULL)
12623 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12624
12625 /* If we are linking against a dynamic object, or generating a
12626 shared library, finish up the dynamic linking information. */
12627 if (dynamic)
12628 {
12629 bfd_byte *dyncon, *dynconend;
12630
12631 /* Fix up .dynamic entries. */
3d4d4302 12632 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12633 BFD_ASSERT (o != NULL);
12634
12635 dyncon = o->contents;
eea6121a 12636 dynconend = o->contents + o->size;
c152c796
AM
12637 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12638 {
12639 Elf_Internal_Dyn dyn;
12640 const char *name;
12641 unsigned int type;
64487780
AM
12642 bfd_size_type sh_size;
12643 bfd_vma sh_addr;
c152c796
AM
12644
12645 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12646
12647 switch (dyn.d_tag)
12648 {
12649 default:
12650 continue;
12651 case DT_NULL:
12652 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12653 {
12654 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12655 {
12656 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12657 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12658 default: continue;
12659 }
12660 dyn.d_un.d_val = relativecount;
12661 relativecount = 0;
12662 break;
12663 }
12664 continue;
12665
12666 case DT_INIT:
12667 name = info->init_function;
12668 goto get_sym;
12669 case DT_FINI:
12670 name = info->fini_function;
12671 get_sym:
12672 {
12673 struct elf_link_hash_entry *h;
12674
64f52338 12675 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12676 if (h != NULL
12677 && (h->root.type == bfd_link_hash_defined
12678 || h->root.type == bfd_link_hash_defweak))
12679 {
bef26483 12680 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12681 o = h->root.u.def.section;
12682 if (o->output_section != NULL)
bef26483 12683 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12684 + o->output_offset);
12685 else
12686 {
12687 /* The symbol is imported from another shared
12688 library and does not apply to this one. */
bef26483 12689 dyn.d_un.d_ptr = 0;
c152c796
AM
12690 }
12691 break;
12692 }
12693 }
12694 continue;
12695
12696 case DT_PREINIT_ARRAYSZ:
12697 name = ".preinit_array";
4ade44b7 12698 goto get_out_size;
c152c796
AM
12699 case DT_INIT_ARRAYSZ:
12700 name = ".init_array";
4ade44b7 12701 goto get_out_size;
c152c796
AM
12702 case DT_FINI_ARRAYSZ:
12703 name = ".fini_array";
4ade44b7 12704 get_out_size:
c152c796
AM
12705 o = bfd_get_section_by_name (abfd, name);
12706 if (o == NULL)
12707 {
4eca0228 12708 _bfd_error_handler
4ade44b7 12709 (_("could not find section %s"), name);
c152c796
AM
12710 goto error_return;
12711 }
eea6121a 12712 if (o->size == 0)
4eca0228 12713 _bfd_error_handler
c152c796 12714 (_("warning: %s section has zero size"), name);
eea6121a 12715 dyn.d_un.d_val = o->size;
c152c796
AM
12716 break;
12717
12718 case DT_PREINIT_ARRAY:
12719 name = ".preinit_array";
4ade44b7 12720 goto get_out_vma;
c152c796
AM
12721 case DT_INIT_ARRAY:
12722 name = ".init_array";
4ade44b7 12723 goto get_out_vma;
c152c796
AM
12724 case DT_FINI_ARRAY:
12725 name = ".fini_array";
4ade44b7
AM
12726 get_out_vma:
12727 o = bfd_get_section_by_name (abfd, name);
12728 goto do_vma;
c152c796
AM
12729
12730 case DT_HASH:
12731 name = ".hash";
12732 goto get_vma;
fdc90cb4
JJ
12733 case DT_GNU_HASH:
12734 name = ".gnu.hash";
12735 goto get_vma;
c152c796
AM
12736 case DT_STRTAB:
12737 name = ".dynstr";
12738 goto get_vma;
12739 case DT_SYMTAB:
12740 name = ".dynsym";
12741 goto get_vma;
12742 case DT_VERDEF:
12743 name = ".gnu.version_d";
12744 goto get_vma;
12745 case DT_VERNEED:
12746 name = ".gnu.version_r";
12747 goto get_vma;
12748 case DT_VERSYM:
12749 name = ".gnu.version";
12750 get_vma:
4ade44b7
AM
12751 o = bfd_get_linker_section (dynobj, name);
12752 do_vma:
b3293efa 12753 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12754 {
4eca0228 12755 _bfd_error_handler
4ade44b7 12756 (_("could not find section %s"), name);
c152c796
AM
12757 goto error_return;
12758 }
894891db
NC
12759 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12760 {
4eca0228 12761 _bfd_error_handler
894891db
NC
12762 (_("warning: section '%s' is being made into a note"), name);
12763 bfd_set_error (bfd_error_nonrepresentable_section);
12764 goto error_return;
12765 }
4ade44b7 12766 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12767 break;
12768
12769 case DT_REL:
12770 case DT_RELA:
12771 case DT_RELSZ:
12772 case DT_RELASZ:
12773 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12774 type = SHT_REL;
12775 else
12776 type = SHT_RELA;
64487780
AM
12777 sh_size = 0;
12778 sh_addr = 0;
c152c796
AM
12779 for (i = 1; i < elf_numsections (abfd); i++)
12780 {
12781 Elf_Internal_Shdr *hdr;
12782
12783 hdr = elf_elfsections (abfd)[i];
12784 if (hdr->sh_type == type
12785 && (hdr->sh_flags & SHF_ALLOC) != 0)
12786 {
64487780
AM
12787 sh_size += hdr->sh_size;
12788 if (sh_addr == 0
12789 || sh_addr > hdr->sh_addr)
12790 sh_addr = hdr->sh_addr;
c152c796
AM
12791 }
12792 }
64487780 12793
64f52338
AM
12794 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12795 {
66631823
CE
12796 unsigned int opb = bfd_octets_per_byte (abfd, o);
12797
64f52338
AM
12798 /* Don't count procedure linkage table relocs in the
12799 overall reloc count. */
64487780
AM
12800 sh_size -= htab->srelplt->size;
12801 if (sh_size == 0)
12802 /* If the size is zero, make the address zero too.
12803 This is to avoid a glibc bug. If the backend
12804 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12805 zero, then we'll put DT_RELA at the end of
12806 DT_JMPREL. glibc will interpret the end of
12807 DT_RELA matching the end of DT_JMPREL as the
12808 case where DT_RELA includes DT_JMPREL, and for
12809 LD_BIND_NOW will decide that processing DT_RELA
12810 will process the PLT relocs too. Net result:
12811 No PLT relocs applied. */
12812 sh_addr = 0;
12813
64f52338
AM
12814 /* If .rela.plt is the first .rela section, exclude
12815 it from DT_RELA. */
64487780 12816 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 12817 + htab->srelplt->output_offset) * opb)
64487780 12818 sh_addr += htab->srelplt->size;
64f52338 12819 }
64487780
AM
12820
12821 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12822 dyn.d_un.d_val = sh_size;
12823 else
12824 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12825 break;
12826 }
12827 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12828 }
12829 }
12830
12831 /* If we have created any dynamic sections, then output them. */
12832 if (dynobj != NULL)
12833 {
12834 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12835 goto error_return;
12836
943284cc 12837 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12838 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12839 || info->error_textrel)
3d4d4302 12840 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12841 {
12842 bfd_byte *dyncon, *dynconend;
12843
943284cc
DJ
12844 dyncon = o->contents;
12845 dynconend = o->contents + o->size;
12846 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12847 {
12848 Elf_Internal_Dyn dyn;
12849
12850 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12851
12852 if (dyn.d_tag == DT_TEXTREL)
12853 {
c192a133
AM
12854 if (info->error_textrel)
12855 info->callbacks->einfo
9793eb77 12856 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12857 else
12858 info->callbacks->einfo
9793eb77 12859 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12860 break;
12861 }
12862 }
12863 }
12864
c152c796
AM
12865 for (o = dynobj->sections; o != NULL; o = o->next)
12866 {
12867 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12868 || o->size == 0
c152c796
AM
12869 || o->output_section == bfd_abs_section_ptr)
12870 continue;
12871 if ((o->flags & SEC_LINKER_CREATED) == 0)
12872 {
12873 /* At this point, we are only interested in sections
12874 created by _bfd_elf_link_create_dynamic_sections. */
12875 continue;
12876 }
64f52338 12877 if (htab->stab_info.stabstr == o)
3722b82f 12878 continue;
64f52338 12879 if (htab->eh_info.hdr_sec == o)
eea6121a 12880 continue;
3d4d4302 12881 if (strcmp (o->name, ".dynstr") != 0)
c152c796 12882 {
bb294208
AM
12883 bfd_size_type octets = ((file_ptr) o->output_offset
12884 * bfd_octets_per_byte (abfd, o));
12885 if (!bfd_set_section_contents (abfd, o->output_section,
12886 o->contents, octets, o->size))
c152c796
AM
12887 goto error_return;
12888 }
12889 else
12890 {
12891 /* The contents of the .dynstr section are actually in a
12892 stringtab. */
8539e4e8
AM
12893 file_ptr off;
12894
c152c796
AM
12895 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12896 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12897 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12898 goto error_return;
12899 }
12900 }
12901 }
12902
7bdf4127 12903 if (!info->resolve_section_groups)
c152c796
AM
12904 {
12905 bfd_boolean failed = FALSE;
12906
7bdf4127 12907 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12908 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12909 if (failed)
12910 goto error_return;
12911 }
12912
12913 /* If we have optimized stabs strings, output them. */
64f52338 12914 if (htab->stab_info.stabstr != NULL)
c152c796 12915 {
64f52338 12916 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12917 goto error_return;
12918 }
12919
9f7c3e5e
AM
12920 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12921 goto error_return;
c152c796 12922
1ff6de03
NA
12923 if (info->callbacks->emit_ctf)
12924 info->callbacks->emit_ctf ();
12925
9f7c3e5e 12926 elf_final_link_free (abfd, &flinfo);
c152c796 12927
104d59d1
JM
12928 if (attr_section)
12929 {
a50b1753 12930 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12931 if (contents == NULL)
d0f16d5e 12932 return FALSE; /* Bail out and fail. */
104d59d1
JM
12933 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12934 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12935 free (contents);
12936 }
12937
c152c796
AM
12938 return TRUE;
12939
12940 error_return:
9f7c3e5e 12941 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12942 return FALSE;
12943}
12944\f
5241d853
RS
12945/* Initialize COOKIE for input bfd ABFD. */
12946
12947static bfd_boolean
12948init_reloc_cookie (struct elf_reloc_cookie *cookie,
12949 struct bfd_link_info *info, bfd *abfd)
12950{
12951 Elf_Internal_Shdr *symtab_hdr;
12952 const struct elf_backend_data *bed;
12953
12954 bed = get_elf_backend_data (abfd);
12955 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12956
12957 cookie->abfd = abfd;
12958 cookie->sym_hashes = elf_sym_hashes (abfd);
12959 cookie->bad_symtab = elf_bad_symtab (abfd);
12960 if (cookie->bad_symtab)
12961 {
12962 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12963 cookie->extsymoff = 0;
12964 }
12965 else
12966 {
12967 cookie->locsymcount = symtab_hdr->sh_info;
12968 cookie->extsymoff = symtab_hdr->sh_info;
12969 }
12970
12971 if (bed->s->arch_size == 32)
12972 cookie->r_sym_shift = 8;
12973 else
12974 cookie->r_sym_shift = 32;
12975
12976 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12977 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12978 {
12979 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12980 cookie->locsymcount, 0,
12981 NULL, NULL, NULL);
12982 if (cookie->locsyms == NULL)
12983 {
12984 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12985 return FALSE;
12986 }
12987 if (info->keep_memory)
12988 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12989 }
12990 return TRUE;
12991}
12992
12993/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12994
12995static void
12996fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12997{
12998 Elf_Internal_Shdr *symtab_hdr;
12999
13000 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13001 if (cookie->locsyms != NULL
13002 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
13003 free (cookie->locsyms);
13004}
13005
13006/* Initialize the relocation information in COOKIE for input section SEC
13007 of input bfd ABFD. */
13008
13009static bfd_boolean
13010init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13011 struct bfd_link_info *info, bfd *abfd,
13012 asection *sec)
13013{
5241d853
RS
13014 if (sec->reloc_count == 0)
13015 {
13016 cookie->rels = NULL;
13017 cookie->relend = NULL;
13018 }
13019 else
13020 {
5241d853
RS
13021 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13022 info->keep_memory);
13023 if (cookie->rels == NULL)
13024 return FALSE;
13025 cookie->rel = cookie->rels;
056bafd4 13026 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13027 }
13028 cookie->rel = cookie->rels;
13029 return TRUE;
13030}
13031
13032/* Free the memory allocated by init_reloc_cookie_rels,
13033 if appropriate. */
13034
13035static void
13036fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13037 asection *sec)
13038{
13039 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13040 free (cookie->rels);
13041}
13042
13043/* Initialize the whole of COOKIE for input section SEC. */
13044
13045static bfd_boolean
13046init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13047 struct bfd_link_info *info,
13048 asection *sec)
13049{
13050 if (!init_reloc_cookie (cookie, info, sec->owner))
13051 goto error1;
13052 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13053 goto error2;
13054 return TRUE;
13055
13056 error2:
13057 fini_reloc_cookie (cookie, sec->owner);
13058 error1:
13059 return FALSE;
13060}
13061
13062/* Free the memory allocated by init_reloc_cookie_for_section,
13063 if appropriate. */
13064
13065static void
13066fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13067 asection *sec)
13068{
13069 fini_reloc_cookie_rels (cookie, sec);
13070 fini_reloc_cookie (cookie, sec->owner);
13071}
13072\f
c152c796
AM
13073/* Garbage collect unused sections. */
13074
07adf181
AM
13075/* Default gc_mark_hook. */
13076
13077asection *
13078_bfd_elf_gc_mark_hook (asection *sec,
13079 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13080 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13081 struct elf_link_hash_entry *h,
13082 Elf_Internal_Sym *sym)
13083{
13084 if (h != NULL)
13085 {
13086 switch (h->root.type)
13087 {
13088 case bfd_link_hash_defined:
13089 case bfd_link_hash_defweak:
13090 return h->root.u.def.section;
13091
13092 case bfd_link_hash_common:
13093 return h->root.u.c.p->section;
13094
13095 default:
13096 break;
13097 }
13098 }
13099 else
13100 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13101
13102 return NULL;
13103}
13104
9e223787 13105/* Return the debug definition section. */
b7c871ed
L
13106
13107static asection *
13108elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13109 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13110 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13111 struct elf_link_hash_entry *h,
9e223787 13112 Elf_Internal_Sym *sym)
b7c871ed 13113{
9e223787
L
13114 if (h != NULL)
13115 {
13116 /* Return the global debug definition section. */
13117 if ((h->root.type == bfd_link_hash_defined
13118 || h->root.type == bfd_link_hash_defweak)
13119 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13120 return h->root.u.def.section;
13121 }
13122 else
13123 {
13124 /* Return the local debug definition section. */
13125 asection *isec = bfd_section_from_elf_index (sec->owner,
13126 sym->st_shndx);
13127 if ((isec->flags & SEC_DEBUGGING) != 0)
13128 return isec;
13129 }
b7c871ed
L
13130
13131 return NULL;
13132}
13133
5241d853
RS
13134/* COOKIE->rel describes a relocation against section SEC, which is
13135 a section we've decided to keep. Return the section that contains
13136 the relocation symbol, or NULL if no section contains it. */
13137
13138asection *
13139_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13140 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13141 struct elf_reloc_cookie *cookie,
13142 bfd_boolean *start_stop)
5241d853
RS
13143{
13144 unsigned long r_symndx;
3024a17a 13145 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13146
13147 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13148 if (r_symndx == STN_UNDEF)
5241d853
RS
13149 return NULL;
13150
13151 if (r_symndx >= cookie->locsymcount
13152 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13153 {
13154 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13155 if (h == NULL)
13156 {
871b3ab2 13157 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13158 sec->owner);
13159 return NULL;
13160 }
5241d853
RS
13161 while (h->root.type == bfd_link_hash_indirect
13162 || h->root.type == bfd_link_hash_warning)
13163 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13164 h->mark = 1;
3024a17a
AM
13165 /* Keep all aliases of the symbol too. If an object symbol
13166 needs to be copied into .dynbss then all of its aliases
13167 should be present as dynamic symbols, not just the one used
13168 on the copy relocation. */
13169 hw = h;
13170 while (hw->is_weakalias)
13171 {
13172 hw = hw->u.alias;
13173 hw->mark = 1;
13174 }
1cce69b9 13175
a6a4679f 13176 if (start_stop != NULL)
1cce69b9 13177 {
7dba9362
AM
13178 /* To work around a glibc bug, mark XXX input sections
13179 when there is a reference to __start_XXX or __stop_XXX
13180 symbols. */
cbd0eecf 13181 if (h->start_stop)
1cce69b9 13182 {
cbd0eecf 13183 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13184 *start_stop = !s->gc_mark;
13185 return s;
1cce69b9
AM
13186 }
13187 }
13188
5241d853
RS
13189 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13190 }
13191
13192 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13193 &cookie->locsyms[r_symndx]);
13194}
13195
13196/* COOKIE->rel describes a relocation against section SEC, which is
13197 a section we've decided to keep. Mark the section that contains
9d0a14d3 13198 the relocation symbol. */
5241d853
RS
13199
13200bfd_boolean
13201_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13202 asection *sec,
13203 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13204 struct elf_reloc_cookie *cookie)
5241d853
RS
13205{
13206 asection *rsec;
1cce69b9 13207 bfd_boolean start_stop = FALSE;
5241d853 13208
1cce69b9
AM
13209 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13210 while (rsec != NULL)
5241d853 13211 {
1cce69b9
AM
13212 if (!rsec->gc_mark)
13213 {
13214 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13215 || (rsec->owner->flags & DYNAMIC) != 0)
13216 rsec->gc_mark = 1;
13217 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13218 return FALSE;
13219 }
13220 if (!start_stop)
13221 break;
199af150 13222 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13223 }
13224 return TRUE;
13225}
13226
07adf181
AM
13227/* The mark phase of garbage collection. For a given section, mark
13228 it and any sections in this section's group, and all the sections
13229 which define symbols to which it refers. */
13230
ccfa59ea
AM
13231bfd_boolean
13232_bfd_elf_gc_mark (struct bfd_link_info *info,
13233 asection *sec,
6a5bb875 13234 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13235{
13236 bfd_boolean ret;
9d0a14d3 13237 asection *group_sec, *eh_frame;
c152c796
AM
13238
13239 sec->gc_mark = 1;
13240
13241 /* Mark all the sections in the group. */
13242 group_sec = elf_section_data (sec)->next_in_group;
13243 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13244 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13245 return FALSE;
13246
13247 /* Look through the section relocs. */
13248 ret = TRUE;
9d0a14d3
RS
13249 eh_frame = elf_eh_frame_section (sec->owner);
13250 if ((sec->flags & SEC_RELOC) != 0
13251 && sec->reloc_count > 0
13252 && sec != eh_frame)
c152c796 13253 {
5241d853 13254 struct elf_reloc_cookie cookie;
c152c796 13255
5241d853
RS
13256 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13257 ret = FALSE;
c152c796 13258 else
c152c796 13259 {
5241d853 13260 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13261 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13262 {
13263 ret = FALSE;
13264 break;
13265 }
13266 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13267 }
13268 }
9d0a14d3
RS
13269
13270 if (ret && eh_frame && elf_fde_list (sec))
13271 {
13272 struct elf_reloc_cookie cookie;
13273
13274 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13275 ret = FALSE;
13276 else
13277 {
13278 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13279 gc_mark_hook, &cookie))
13280 ret = FALSE;
13281 fini_reloc_cookie_for_section (&cookie, eh_frame);
13282 }
13283 }
13284
2f0c68f2
CM
13285 eh_frame = elf_section_eh_frame_entry (sec);
13286 if (ret && eh_frame && !eh_frame->gc_mark)
13287 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13288 ret = FALSE;
13289
c152c796
AM
13290 return ret;
13291}
13292
3c758495
TG
13293/* Scan and mark sections in a special or debug section group. */
13294
13295static void
13296_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13297{
13298 /* Point to first section of section group. */
13299 asection *ssec;
13300 /* Used to iterate the section group. */
13301 asection *msec;
13302
13303 bfd_boolean is_special_grp = TRUE;
13304 bfd_boolean is_debug_grp = TRUE;
13305
13306 /* First scan to see if group contains any section other than debug
13307 and special section. */
13308 ssec = msec = elf_next_in_group (grp);
13309 do
13310 {
13311 if ((msec->flags & SEC_DEBUGGING) == 0)
13312 is_debug_grp = FALSE;
13313
13314 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13315 is_special_grp = FALSE;
13316
13317 msec = elf_next_in_group (msec);
13318 }
13319 while (msec != ssec);
13320
13321 /* If this is a pure debug section group or pure special section group,
13322 keep all sections in this group. */
13323 if (is_debug_grp || is_special_grp)
13324 {
13325 do
13326 {
13327 msec->gc_mark = 1;
13328 msec = elf_next_in_group (msec);
13329 }
13330 while (msec != ssec);
13331 }
13332}
13333
7f6ab9f8
AM
13334/* Keep debug and special sections. */
13335
13336bfd_boolean
13337_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13338 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13339{
13340 bfd *ibfd;
13341
c72f2fb2 13342 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13343 {
13344 asection *isec;
13345 bfd_boolean some_kept;
b40bf0a2 13346 bfd_boolean debug_frag_seen;
b7c871ed 13347 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13348
13349 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13350 continue;
57963c05
AM
13351 isec = ibfd->sections;
13352 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13353 continue;
7f6ab9f8 13354
b40bf0a2
NC
13355 /* Ensure all linker created sections are kept,
13356 see if any other section is already marked,
13357 and note if we have any fragmented debug sections. */
b7c871ed 13358 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13359 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13360 {
13361 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13362 isec->gc_mark = 1;
eb026f09
AM
13363 else if (isec->gc_mark
13364 && (isec->flags & SEC_ALLOC) != 0
13365 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13366 some_kept = TRUE;
b7d07216
L
13367 else
13368 {
13369 /* Since all sections, except for backend specific ones,
13370 have been garbage collected, call mark_hook on this
13371 section if any of its linked-to sections is marked. */
13372 asection *linked_to_sec = elf_linked_to_section (isec);
13373 for (; linked_to_sec != NULL;
13374 linked_to_sec = elf_linked_to_section (linked_to_sec))
13375 if (linked_to_sec->gc_mark)
13376 {
13377 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13378 return FALSE;
13379 break;
13380 }
13381 }
b40bf0a2 13382
535b785f 13383 if (!debug_frag_seen
b40bf0a2
NC
13384 && (isec->flags & SEC_DEBUGGING)
13385 && CONST_STRNEQ (isec->name, ".debug_line."))
13386 debug_frag_seen = TRUE;
5242a0a0
L
13387 else if (strcmp (bfd_section_name (isec),
13388 "__patchable_function_entries") == 0
13389 && elf_linked_to_section (isec) == NULL)
13390 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13391 "need linked-to section "
13392 "for --gc-sections\n"),
13393 isec->owner, isec);
7f6ab9f8
AM
13394 }
13395
eb026f09
AM
13396 /* If no non-note alloc section in this file will be kept, then
13397 we can toss out the debug and special sections. */
7f6ab9f8
AM
13398 if (!some_kept)
13399 continue;
13400
13401 /* Keep debug and special sections like .comment when they are
3c758495 13402 not part of a group. Also keep section groups that contain
b7d07216
L
13403 just debug sections or special sections. NB: Sections with
13404 linked-to section has been handled above. */
7f6ab9f8 13405 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13406 {
13407 if ((isec->flags & SEC_GROUP) != 0)
13408 _bfd_elf_gc_mark_debug_special_section_group (isec);
13409 else if (((isec->flags & SEC_DEBUGGING) != 0
13410 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13411 && elf_next_in_group (isec) == NULL
13412 && elf_linked_to_section (isec) == NULL)
3c758495 13413 isec->gc_mark = 1;
b7c871ed
L
13414 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13415 has_kept_debug_info = TRUE;
3c758495 13416 }
b40bf0a2 13417
b40bf0a2
NC
13418 /* Look for CODE sections which are going to be discarded,
13419 and find and discard any fragmented debug sections which
13420 are associated with that code section. */
b7c871ed
L
13421 if (debug_frag_seen)
13422 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13423 if ((isec->flags & SEC_CODE) != 0
13424 && isec->gc_mark == 0)
13425 {
13426 unsigned int ilen;
13427 asection *dsec;
b40bf0a2 13428
b7c871ed 13429 ilen = strlen (isec->name);
b40bf0a2 13430
b7c871ed 13431 /* Association is determined by the name of the debug
07d6d2b8 13432 section containing the name of the code section as
b7c871ed
L
13433 a suffix. For example .debug_line.text.foo is a
13434 debug section associated with .text.foo. */
13435 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13436 {
13437 unsigned int dlen;
b40bf0a2 13438
b7c871ed
L
13439 if (dsec->gc_mark == 0
13440 || (dsec->flags & SEC_DEBUGGING) == 0)
13441 continue;
b40bf0a2 13442
b7c871ed 13443 dlen = strlen (dsec->name);
b40bf0a2 13444
b7c871ed
L
13445 if (dlen > ilen
13446 && strncmp (dsec->name + (dlen - ilen),
13447 isec->name, ilen) == 0)
b40bf0a2 13448 dsec->gc_mark = 0;
b7c871ed 13449 }
b40bf0a2 13450 }
b7c871ed
L
13451
13452 /* Mark debug sections referenced by kept debug sections. */
13453 if (has_kept_debug_info)
13454 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13455 if (isec->gc_mark
13456 && (isec->flags & SEC_DEBUGGING) != 0)
13457 if (!_bfd_elf_gc_mark (info, isec,
13458 elf_gc_mark_debug_section))
13459 return FALSE;
7f6ab9f8
AM
13460 }
13461 return TRUE;
13462}
13463
c152c796 13464static bfd_boolean
ccabcbe5 13465elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13466{
13467 bfd *sub;
ccabcbe5 13468 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13469
c72f2fb2 13470 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13471 {
13472 asection *o;
13473
b19a8f85 13474 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13475 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13476 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13477 continue;
57963c05
AM
13478 o = sub->sections;
13479 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13480 continue;
c152c796
AM
13481
13482 for (o = sub->sections; o != NULL; o = o->next)
13483 {
a33dafc3
L
13484 /* When any section in a section group is kept, we keep all
13485 sections in the section group. If the first member of
13486 the section group is excluded, we will also exclude the
13487 group section. */
13488 if (o->flags & SEC_GROUP)
13489 {
13490 asection *first = elf_next_in_group (o);
13491 o->gc_mark = first->gc_mark;
13492 }
c152c796 13493
1e7eae0d 13494 if (o->gc_mark)
c152c796
AM
13495 continue;
13496
13497 /* Skip sweeping sections already excluded. */
13498 if (o->flags & SEC_EXCLUDE)
13499 continue;
13500
13501 /* Since this is early in the link process, it is simple
13502 to remove a section from the output. */
13503 o->flags |= SEC_EXCLUDE;
13504
c55fe096 13505 if (info->print_gc_sections && o->size != 0)
695344c0 13506 /* xgettext:c-format */
9793eb77 13507 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13508 o, sub);
c152c796
AM
13509 }
13510 }
13511
c152c796
AM
13512 return TRUE;
13513}
13514
13515/* Propagate collected vtable information. This is called through
13516 elf_link_hash_traverse. */
13517
13518static bfd_boolean
13519elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13520{
c152c796 13521 /* Those that are not vtables. */
cbd0eecf
L
13522 if (h->start_stop
13523 || h->u2.vtable == NULL
13524 || h->u2.vtable->parent == NULL)
c152c796
AM
13525 return TRUE;
13526
13527 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13528 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13529 return TRUE;
13530
13531 /* If we've already been done, exit. */
cbd0eecf 13532 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13533 return TRUE;
13534
13535 /* Make sure the parent's table is up to date. */
cbd0eecf 13536 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13537
cbd0eecf 13538 if (h->u2.vtable->used == NULL)
c152c796
AM
13539 {
13540 /* None of this table's entries were referenced. Re-use the
13541 parent's table. */
cbd0eecf
L
13542 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13543 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13544 }
13545 else
13546 {
13547 size_t n;
13548 bfd_boolean *cu, *pu;
13549
13550 /* Or the parent's entries into ours. */
cbd0eecf 13551 cu = h->u2.vtable->used;
c152c796 13552 cu[-1] = TRUE;
cbd0eecf 13553 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13554 if (pu != NULL)
13555 {
13556 const struct elf_backend_data *bed;
13557 unsigned int log_file_align;
13558
13559 bed = get_elf_backend_data (h->root.u.def.section->owner);
13560 log_file_align = bed->s->log_file_align;
cbd0eecf 13561 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13562 while (n--)
13563 {
13564 if (*pu)
13565 *cu = TRUE;
13566 pu++;
13567 cu++;
13568 }
13569 }
13570 }
13571
13572 return TRUE;
13573}
13574
13575static bfd_boolean
13576elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13577{
13578 asection *sec;
13579 bfd_vma hstart, hend;
13580 Elf_Internal_Rela *relstart, *relend, *rel;
13581 const struct elf_backend_data *bed;
13582 unsigned int log_file_align;
13583
c152c796
AM
13584 /* Take care of both those symbols that do not describe vtables as
13585 well as those that are not loaded. */
cbd0eecf
L
13586 if (h->start_stop
13587 || h->u2.vtable == NULL
13588 || h->u2.vtable->parent == NULL)
c152c796
AM
13589 return TRUE;
13590
13591 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13592 || h->root.type == bfd_link_hash_defweak);
13593
13594 sec = h->root.u.def.section;
13595 hstart = h->root.u.def.value;
13596 hend = hstart + h->size;
13597
13598 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13599 if (!relstart)
13600 return *(bfd_boolean *) okp = FALSE;
13601 bed = get_elf_backend_data (sec->owner);
13602 log_file_align = bed->s->log_file_align;
13603
056bafd4 13604 relend = relstart + sec->reloc_count;
c152c796
AM
13605
13606 for (rel = relstart; rel < relend; ++rel)
13607 if (rel->r_offset >= hstart && rel->r_offset < hend)
13608 {
13609 /* If the entry is in use, do nothing. */
cbd0eecf
L
13610 if (h->u2.vtable->used
13611 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13612 {
13613 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13614 if (h->u2.vtable->used[entry])
c152c796
AM
13615 continue;
13616 }
13617 /* Otherwise, kill it. */
13618 rel->r_offset = rel->r_info = rel->r_addend = 0;
13619 }
13620
13621 return TRUE;
13622}
13623
87538722
AM
13624/* Mark sections containing dynamically referenced symbols. When
13625 building shared libraries, we must assume that any visible symbol is
13626 referenced. */
715df9b8 13627
64d03ab5
AM
13628bfd_boolean
13629bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13630{
87538722 13631 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13632 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13633
715df9b8
EB
13634 if ((h->root.type == bfd_link_hash_defined
13635 || h->root.type == bfd_link_hash_defweak)
d664fd41 13636 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13637 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13638 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13639 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13640 && (!bfd_link_executable (info)
22185505 13641 || info->gc_keep_exported
b407645f
AM
13642 || info->export_dynamic
13643 || (h->dynamic
13644 && d != NULL
13645 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13646 && (h->versioned >= versioned
54e8959c
L
13647 || !bfd_hide_sym_by_version (info->version_info,
13648 h->root.root.string)))))
715df9b8
EB
13649 h->root.u.def.section->flags |= SEC_KEEP;
13650
13651 return TRUE;
13652}
3b36f7e6 13653
74f0fb50
AM
13654/* Keep all sections containing symbols undefined on the command-line,
13655 and the section containing the entry symbol. */
13656
13657void
13658_bfd_elf_gc_keep (struct bfd_link_info *info)
13659{
13660 struct bfd_sym_chain *sym;
13661
13662 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13663 {
13664 struct elf_link_hash_entry *h;
13665
13666 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13667 FALSE, FALSE, FALSE);
13668
13669 if (h != NULL
13670 && (h->root.type == bfd_link_hash_defined
13671 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13672 && !bfd_is_abs_section (h->root.u.def.section)
13673 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13674 h->root.u.def.section->flags |= SEC_KEEP;
13675 }
13676}
13677
2f0c68f2
CM
13678bfd_boolean
13679bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13680 struct bfd_link_info *info)
13681{
13682 bfd *ibfd = info->input_bfds;
13683
13684 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13685 {
13686 asection *sec;
13687 struct elf_reloc_cookie cookie;
13688
13689 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13690 continue;
57963c05
AM
13691 sec = ibfd->sections;
13692 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13693 continue;
2f0c68f2
CM
13694
13695 if (!init_reloc_cookie (&cookie, info, ibfd))
13696 return FALSE;
13697
13698 for (sec = ibfd->sections; sec; sec = sec->next)
13699 {
fd361982 13700 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13701 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13702 {
13703 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13704 fini_reloc_cookie_rels (&cookie, sec);
13705 }
13706 }
13707 }
13708 return TRUE;
13709}
13710
c152c796
AM
13711/* Do mark and sweep of unused sections. */
13712
13713bfd_boolean
13714bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13715{
13716 bfd_boolean ok = TRUE;
13717 bfd *sub;
6a5bb875 13718 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13720 struct elf_link_hash_table *htab;
c152c796 13721
64d03ab5 13722 if (!bed->can_gc_sections
715df9b8 13723 || !is_elf_hash_table (info->hash))
c152c796 13724 {
9793eb77 13725 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13726 return TRUE;
13727 }
13728
74f0fb50 13729 bed->gc_keep (info);
da44f4e5 13730 htab = elf_hash_table (info);
74f0fb50 13731
9d0a14d3
RS
13732 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13733 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13734 for (sub = info->input_bfds;
13735 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13736 sub = sub->link.next)
9d0a14d3
RS
13737 {
13738 asection *sec;
13739 struct elf_reloc_cookie cookie;
13740
57963c05
AM
13741 sec = sub->sections;
13742 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13743 continue;
9d0a14d3 13744 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13745 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13746 {
13747 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13748 if (elf_section_data (sec)->sec_info
13749 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13750 elf_eh_frame_section (sub) = sec;
13751 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13752 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13753 }
13754 }
9d0a14d3 13755
c152c796 13756 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13757 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13758 if (!ok)
13759 return FALSE;
13760
13761 /* Kill the vtable relocations that were not used. */
da44f4e5 13762 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13763 if (!ok)
13764 return FALSE;
13765
715df9b8 13766 /* Mark dynamically referenced symbols. */
22185505 13767 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13768 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13769
715df9b8 13770 /* Grovel through relocs to find out who stays ... */
64d03ab5 13771 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13772 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13773 {
13774 asection *o;
13775
b19a8f85 13776 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13777 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13778 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13779 continue;
13780
57963c05
AM
13781 o = sub->sections;
13782 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13783 continue;
13784
7f6ab9f8
AM
13785 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13786 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13787 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13788 well as FINI_ARRAY sections for ld -r. */
c152c796 13789 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13790 if (!o->gc_mark
13791 && (o->flags & SEC_EXCLUDE) == 0
24007750 13792 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13793 || (bfd_link_relocatable (info)
13794 && ((elf_section_data (o)->this_hdr.sh_type
13795 == SHT_PREINIT_ARRAY)
13796 || (elf_section_data (o)->this_hdr.sh_type
13797 == SHT_INIT_ARRAY)
13798 || (elf_section_data (o)->this_hdr.sh_type
13799 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13800 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13801 && elf_next_in_group (o) == NULL )))
13802 {
13803 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13804 return FALSE;
13805 }
c152c796
AM
13806 }
13807
6a5bb875 13808 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13809 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13810
c152c796 13811 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13812 return elf_gc_sweep (abfd, info);
c152c796
AM
13813}
13814\f
13815/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13816
13817bfd_boolean
13818bfd_elf_gc_record_vtinherit (bfd *abfd,
13819 asection *sec,
13820 struct elf_link_hash_entry *h,
13821 bfd_vma offset)
13822{
13823 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13824 struct elf_link_hash_entry **search, *child;
ef53be89 13825 size_t extsymcount;
c152c796
AM
13826 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13827
13828 /* The sh_info field of the symtab header tells us where the
13829 external symbols start. We don't care about the local symbols at
13830 this point. */
13831 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13832 if (!elf_bad_symtab (abfd))
13833 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13834
13835 sym_hashes = elf_sym_hashes (abfd);
13836 sym_hashes_end = sym_hashes + extsymcount;
13837
13838 /* Hunt down the child symbol, which is in this section at the same
13839 offset as the relocation. */
13840 for (search = sym_hashes; search != sym_hashes_end; ++search)
13841 {
13842 if ((child = *search) != NULL
13843 && (child->root.type == bfd_link_hash_defined
13844 || child->root.type == bfd_link_hash_defweak)
13845 && child->root.u.def.section == sec
13846 && child->root.u.def.value == offset)
13847 goto win;
13848 }
13849
695344c0 13850 /* xgettext:c-format */
9793eb77 13851 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13852 abfd, sec, (uint64_t) offset);
c152c796
AM
13853 bfd_set_error (bfd_error_invalid_operation);
13854 return FALSE;
13855
13856 win:
cbd0eecf 13857 if (!child->u2.vtable)
f6e332e6 13858 {
cbd0eecf
L
13859 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13860 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13861 if (!child->u2.vtable)
f6e332e6
AM
13862 return FALSE;
13863 }
c152c796
AM
13864 if (!h)
13865 {
13866 /* This *should* only be the absolute section. It could potentially
13867 be that someone has defined a non-global vtable though, which
13868 would be bad. It isn't worth paging in the local symbols to be
13869 sure though; that case should simply be handled by the assembler. */
13870
cbd0eecf 13871 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13872 }
13873 else
cbd0eecf 13874 child->u2.vtable->parent = h;
c152c796
AM
13875
13876 return TRUE;
13877}
13878
13879/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13880
13881bfd_boolean
a0ea3a14 13882bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13883 struct elf_link_hash_entry *h,
13884 bfd_vma addend)
13885{
13886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13887 unsigned int log_file_align = bed->s->log_file_align;
13888
a0ea3a14
L
13889 if (!h)
13890 {
13891 /* xgettext:c-format */
13892 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13893 abfd, sec);
13894 bfd_set_error (bfd_error_bad_value);
13895 return FALSE;
13896 }
13897
cbd0eecf 13898 if (!h->u2.vtable)
f6e332e6 13899 {
cbd0eecf
L
13900 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13901 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13902 if (!h->u2.vtable)
f6e332e6
AM
13903 return FALSE;
13904 }
13905
cbd0eecf 13906 if (addend >= h->u2.vtable->size)
c152c796
AM
13907 {
13908 size_t size, bytes, file_align;
cbd0eecf 13909 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13910
13911 /* While the symbol is undefined, we have to be prepared to handle
13912 a zero size. */
13913 file_align = 1 << log_file_align;
13914 if (h->root.type == bfd_link_hash_undefined)
13915 size = addend + file_align;
13916 else
13917 {
13918 size = h->size;
13919 if (addend >= size)
13920 {
13921 /* Oops! We've got a reference past the defined end of
13922 the table. This is probably a bug -- shall we warn? */
13923 size = addend + file_align;
13924 }
13925 }
13926 size = (size + file_align - 1) & -file_align;
13927
13928 /* Allocate one extra entry for use as a "done" flag for the
13929 consolidation pass. */
13930 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13931
13932 if (ptr)
13933 {
a50b1753 13934 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13935
13936 if (ptr != NULL)
13937 {
13938 size_t oldbytes;
13939
cbd0eecf 13940 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13941 * sizeof (bfd_boolean));
13942 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13943 }
13944 }
13945 else
a50b1753 13946 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13947
13948 if (ptr == NULL)
13949 return FALSE;
13950
13951 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13952 h->u2.vtable->used = ptr + 1;
13953 h->u2.vtable->size = size;
c152c796
AM
13954 }
13955
cbd0eecf 13956 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13957
13958 return TRUE;
13959}
13960
ae17ab41
CM
13961/* Map an ELF section header flag to its corresponding string. */
13962typedef struct
13963{
13964 char *flag_name;
13965 flagword flag_value;
13966} elf_flags_to_name_table;
13967
13968static elf_flags_to_name_table elf_flags_to_names [] =
13969{
13970 { "SHF_WRITE", SHF_WRITE },
13971 { "SHF_ALLOC", SHF_ALLOC },
13972 { "SHF_EXECINSTR", SHF_EXECINSTR },
13973 { "SHF_MERGE", SHF_MERGE },
13974 { "SHF_STRINGS", SHF_STRINGS },
13975 { "SHF_INFO_LINK", SHF_INFO_LINK},
13976 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13977 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13978 { "SHF_GROUP", SHF_GROUP },
13979 { "SHF_TLS", SHF_TLS },
13980 { "SHF_MASKOS", SHF_MASKOS },
13981 { "SHF_EXCLUDE", SHF_EXCLUDE },
13982};
13983
b9c361e0
JL
13984/* Returns TRUE if the section is to be included, otherwise FALSE. */
13985bfd_boolean
ae17ab41 13986bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13987 struct flag_info *flaginfo,
b9c361e0 13988 asection *section)
ae17ab41 13989{
8b127cbc 13990 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13991
8b127cbc 13992 if (!flaginfo->flags_initialized)
ae17ab41 13993 {
8b127cbc
AM
13994 bfd *obfd = info->output_bfd;
13995 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13996 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13997 int with_hex = 0;
13998 int without_hex = 0;
13999
8b127cbc 14000 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14001 {
b9c361e0 14002 unsigned i;
8b127cbc 14003 flagword (*lookup) (char *);
ae17ab41 14004
8b127cbc
AM
14005 lookup = bed->elf_backend_lookup_section_flags_hook;
14006 if (lookup != NULL)
ae17ab41 14007 {
8b127cbc 14008 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14009
14010 if (hexval != 0)
14011 {
14012 if (tf->with == with_flags)
14013 with_hex |= hexval;
14014 else if (tf->with == without_flags)
14015 without_hex |= hexval;
14016 tf->valid = TRUE;
14017 continue;
14018 }
ae17ab41 14019 }
8b127cbc 14020 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14021 {
8b127cbc 14022 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14023 {
14024 if (tf->with == with_flags)
14025 with_hex |= elf_flags_to_names[i].flag_value;
14026 else if (tf->with == without_flags)
14027 without_hex |= elf_flags_to_names[i].flag_value;
14028 tf->valid = TRUE;
14029 break;
14030 }
14031 }
8b127cbc 14032 if (!tf->valid)
b9c361e0 14033 {
68ffbac6 14034 info->callbacks->einfo
9793eb77 14035 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14036 return FALSE;
ae17ab41
CM
14037 }
14038 }
8b127cbc
AM
14039 flaginfo->flags_initialized = TRUE;
14040 flaginfo->only_with_flags |= with_hex;
14041 flaginfo->not_with_flags |= without_hex;
ae17ab41 14042 }
ae17ab41 14043
8b127cbc 14044 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14045 return FALSE;
14046
8b127cbc 14047 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14048 return FALSE;
14049
14050 return TRUE;
ae17ab41
CM
14051}
14052
c152c796
AM
14053struct alloc_got_off_arg {
14054 bfd_vma gotoff;
10455f89 14055 struct bfd_link_info *info;
c152c796
AM
14056};
14057
14058/* We need a special top-level link routine to convert got reference counts
14059 to real got offsets. */
14060
14061static bfd_boolean
14062elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14063{
a50b1753 14064 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14065 bfd *obfd = gofarg->info->output_bfd;
14066 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14067
c152c796
AM
14068 if (h->got.refcount > 0)
14069 {
14070 h->got.offset = gofarg->gotoff;
10455f89 14071 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14072 }
14073 else
14074 h->got.offset = (bfd_vma) -1;
14075
14076 return TRUE;
14077}
14078
14079/* And an accompanying bit to work out final got entry offsets once
14080 we're done. Should be called from final_link. */
14081
14082bfd_boolean
14083bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14084 struct bfd_link_info *info)
14085{
14086 bfd *i;
14087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14088 bfd_vma gotoff;
c152c796
AM
14089 struct alloc_got_off_arg gofarg;
14090
10455f89
HPN
14091 BFD_ASSERT (abfd == info->output_bfd);
14092
c152c796
AM
14093 if (! is_elf_hash_table (info->hash))
14094 return FALSE;
14095
14096 /* The GOT offset is relative to the .got section, but the GOT header is
14097 put into the .got.plt section, if the backend uses it. */
14098 if (bed->want_got_plt)
14099 gotoff = 0;
14100 else
14101 gotoff = bed->got_header_size;
14102
14103 /* Do the local .got entries first. */
c72f2fb2 14104 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14105 {
14106 bfd_signed_vma *local_got;
ef53be89 14107 size_t j, locsymcount;
c152c796
AM
14108 Elf_Internal_Shdr *symtab_hdr;
14109
14110 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14111 continue;
14112
14113 local_got = elf_local_got_refcounts (i);
14114 if (!local_got)
14115 continue;
14116
14117 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14118 if (elf_bad_symtab (i))
14119 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14120 else
14121 locsymcount = symtab_hdr->sh_info;
14122
14123 for (j = 0; j < locsymcount; ++j)
14124 {
14125 if (local_got[j] > 0)
14126 {
14127 local_got[j] = gotoff;
10455f89 14128 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14129 }
14130 else
14131 local_got[j] = (bfd_vma) -1;
14132 }
14133 }
14134
14135 /* Then the global .got entries. .plt refcounts are handled by
14136 adjust_dynamic_symbol */
14137 gofarg.gotoff = gotoff;
10455f89 14138 gofarg.info = info;
c152c796
AM
14139 elf_link_hash_traverse (elf_hash_table (info),
14140 elf_gc_allocate_got_offsets,
14141 &gofarg);
14142 return TRUE;
14143}
14144
14145/* Many folk need no more in the way of final link than this, once
14146 got entry reference counting is enabled. */
14147
14148bfd_boolean
14149bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14150{
14151 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14152 return FALSE;
14153
14154 /* Invoke the regular ELF backend linker to do all the work. */
14155 return bfd_elf_final_link (abfd, info);
14156}
14157
14158bfd_boolean
14159bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14160{
a50b1753 14161 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14162
14163 if (rcookie->bad_symtab)
14164 rcookie->rel = rcookie->rels;
14165
14166 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14167 {
14168 unsigned long r_symndx;
14169
14170 if (! rcookie->bad_symtab)
14171 if (rcookie->rel->r_offset > offset)
14172 return FALSE;
14173 if (rcookie->rel->r_offset != offset)
14174 continue;
14175
14176 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14177 if (r_symndx == STN_UNDEF)
c152c796
AM
14178 return TRUE;
14179
14180 if (r_symndx >= rcookie->locsymcount
14181 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14182 {
14183 struct elf_link_hash_entry *h;
14184
14185 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14186
14187 while (h->root.type == bfd_link_hash_indirect
14188 || h->root.type == bfd_link_hash_warning)
14189 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14190
14191 if ((h->root.type == bfd_link_hash_defined
14192 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14193 && (h->root.u.def.section->owner != rcookie->abfd
14194 || h->root.u.def.section->kept_section != NULL
14195 || discarded_section (h->root.u.def.section)))
c152c796 14196 return TRUE;
c152c796
AM
14197 }
14198 else
14199 {
14200 /* It's not a relocation against a global symbol,
14201 but it could be a relocation against a local
14202 symbol for a discarded section. */
14203 asection *isec;
14204 Elf_Internal_Sym *isym;
14205
14206 /* Need to: get the symbol; get the section. */
14207 isym = &rcookie->locsyms[r_symndx];
cb33740c 14208 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14209 if (isec != NULL
14210 && (isec->kept_section != NULL
14211 || discarded_section (isec)))
cb33740c 14212 return TRUE;
c152c796
AM
14213 }
14214 return FALSE;
14215 }
14216 return FALSE;
14217}
14218
14219/* Discard unneeded references to discarded sections.
75938853
AM
14220 Returns -1 on error, 1 if any section's size was changed, 0 if
14221 nothing changed. This function assumes that the relocations are in
14222 sorted order, which is true for all known assemblers. */
c152c796 14223
75938853 14224int
c152c796
AM
14225bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14226{
14227 struct elf_reloc_cookie cookie;
18cd5bce 14228 asection *o;
c152c796 14229 bfd *abfd;
75938853 14230 int changed = 0;
c152c796
AM
14231
14232 if (info->traditional_format
14233 || !is_elf_hash_table (info->hash))
75938853 14234 return 0;
c152c796 14235
18cd5bce
AM
14236 o = bfd_get_section_by_name (output_bfd, ".stab");
14237 if (o != NULL)
c152c796 14238 {
18cd5bce 14239 asection *i;
c152c796 14240
18cd5bce 14241 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14242 {
18cd5bce
AM
14243 if (i->size == 0
14244 || i->reloc_count == 0
14245 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14246 continue;
c152c796 14247
18cd5bce
AM
14248 abfd = i->owner;
14249 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14250 continue;
c152c796 14251
18cd5bce 14252 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14253 return -1;
c152c796 14254
18cd5bce
AM
14255 if (_bfd_discard_section_stabs (abfd, i,
14256 elf_section_data (i)->sec_info,
5241d853
RS
14257 bfd_elf_reloc_symbol_deleted_p,
14258 &cookie))
75938853 14259 changed = 1;
18cd5bce
AM
14260
14261 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14262 }
18cd5bce
AM
14263 }
14264
2f0c68f2
CM
14265 o = NULL;
14266 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14267 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14268 if (o != NULL)
14269 {
14270 asection *i;
d7153c4a 14271 int eh_changed = 0;
66631823 14272 unsigned int eh_alignment; /* Octets. */
c152c796 14273
18cd5bce 14274 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14275 {
18cd5bce
AM
14276 if (i->size == 0)
14277 continue;
14278
14279 abfd = i->owner;
14280 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14281 continue;
14282
14283 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14284 return -1;
18cd5bce
AM
14285
14286 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14287 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14288 bfd_elf_reloc_symbol_deleted_p,
14289 &cookie))
d7153c4a
AM
14290 {
14291 eh_changed = 1;
14292 if (i->size != i->rawsize)
14293 changed = 1;
14294 }
18cd5bce
AM
14295
14296 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14297 }
9866ffe2 14298
66631823
CE
14299 eh_alignment = ((1 << o->alignment_power)
14300 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14301 /* Skip over zero terminator, and prevent empty sections from
14302 adding alignment padding at the end. */
14303 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14304 if (i->size == 0)
14305 i->flags |= SEC_EXCLUDE;
14306 else if (i->size > 4)
14307 break;
14308 /* The last non-empty eh_frame section doesn't need padding. */
14309 if (i != NULL)
14310 i = i->map_tail.s;
14311 /* Any prior sections must pad the last FDE out to the output
14312 section alignment. Otherwise we might have zero padding
14313 between sections, which would be seen as a terminator. */
14314 for (; i != NULL; i = i->map_tail.s)
14315 if (i->size == 4)
14316 /* All but the last zero terminator should have been removed. */
14317 BFD_FAIL ();
14318 else
14319 {
14320 bfd_size_type size
14321 = (i->size + eh_alignment - 1) & -eh_alignment;
14322 if (i->size != size)
af471f82 14323 {
9866ffe2
AM
14324 i->size = size;
14325 changed = 1;
14326 eh_changed = 1;
af471f82 14327 }
9866ffe2 14328 }
d7153c4a
AM
14329 if (eh_changed)
14330 elf_link_hash_traverse (elf_hash_table (info),
14331 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14332 }
c152c796 14333
18cd5bce
AM
14334 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14335 {
14336 const struct elf_backend_data *bed;
57963c05 14337 asection *s;
c152c796 14338
18cd5bce
AM
14339 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14340 continue;
57963c05
AM
14341 s = abfd->sections;
14342 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14343 continue;
18cd5bce
AM
14344
14345 bed = get_elf_backend_data (abfd);
14346
14347 if (bed->elf_backend_discard_info != NULL)
14348 {
14349 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14350 return -1;
18cd5bce
AM
14351
14352 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14353 changed = 1;
18cd5bce
AM
14354
14355 fini_reloc_cookie (&cookie, abfd);
14356 }
c152c796
AM
14357 }
14358
2f0c68f2
CM
14359 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14360 _bfd_elf_end_eh_frame_parsing (info);
14361
14362 if (info->eh_frame_hdr_type
0e1862bb 14363 && !bfd_link_relocatable (info)
c152c796 14364 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14365 changed = 1;
c152c796 14366
75938853 14367 return changed;
c152c796 14368}
082b7297 14369
43e1669b 14370bfd_boolean
0c511000 14371_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14372 asection *sec,
c0f00686 14373 struct bfd_link_info *info)
082b7297
L
14374{
14375 flagword flags;
c77ec726 14376 const char *name, *key;
082b7297
L
14377 struct bfd_section_already_linked *l;
14378 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14379
c77ec726
AM
14380 if (sec->output_section == bfd_abs_section_ptr)
14381 return FALSE;
0c511000 14382
c77ec726 14383 flags = sec->flags;
0c511000 14384
c77ec726
AM
14385 /* Return if it isn't a linkonce section. A comdat group section
14386 also has SEC_LINK_ONCE set. */
14387 if ((flags & SEC_LINK_ONCE) == 0)
14388 return FALSE;
0c511000 14389
c77ec726
AM
14390 /* Don't put group member sections on our list of already linked
14391 sections. They are handled as a group via their group section. */
14392 if (elf_sec_group (sec) != NULL)
14393 return FALSE;
0c511000 14394
c77ec726
AM
14395 /* For a SHT_GROUP section, use the group signature as the key. */
14396 name = sec->name;
14397 if ((flags & SEC_GROUP) != 0
14398 && elf_next_in_group (sec) != NULL
14399 && elf_group_name (elf_next_in_group (sec)) != NULL)
14400 key = elf_group_name (elf_next_in_group (sec));
14401 else
14402 {
14403 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14404 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14405 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14406 key++;
0c511000 14407 else
c77ec726
AM
14408 /* Must be a user linkonce section that doesn't follow gcc's
14409 naming convention. In this case we won't be matching
14410 single member groups. */
14411 key = name;
0c511000 14412 }
6d2cd210 14413
c77ec726 14414 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14415
14416 for (l = already_linked_list->entry; l != NULL; l = l->next)
14417 {
c2370991 14418 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14419 sections with a signature of <key> (<key> is some string),
14420 and linkonce sections named .gnu.linkonce.<type>.<key>.
14421 Match like sections. LTO plugin sections are an exception.
14422 They are always named .gnu.linkonce.t.<key> and match either
14423 type of section. */
14424 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14425 && ((flags & SEC_GROUP) != 0
14426 || strcmp (name, l->sec->name) == 0))
14427 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14428 {
14429 /* The section has already been linked. See if we should
6d2cd210 14430 issue a warning. */
c77ec726
AM
14431 if (!_bfd_handle_already_linked (sec, l, info))
14432 return FALSE;
082b7297 14433
c77ec726 14434 if (flags & SEC_GROUP)
3d7f7666 14435 {
c77ec726
AM
14436 asection *first = elf_next_in_group (sec);
14437 asection *s = first;
3d7f7666 14438
c77ec726 14439 while (s != NULL)
3d7f7666 14440 {
c77ec726
AM
14441 s->output_section = bfd_abs_section_ptr;
14442 /* Record which group discards it. */
14443 s->kept_section = l->sec;
14444 s = elf_next_in_group (s);
14445 /* These lists are circular. */
14446 if (s == first)
14447 break;
3d7f7666
L
14448 }
14449 }
082b7297 14450
43e1669b 14451 return TRUE;
082b7297
L
14452 }
14453 }
14454
c77ec726
AM
14455 /* A single member comdat group section may be discarded by a
14456 linkonce section and vice versa. */
14457 if ((flags & SEC_GROUP) != 0)
3d7f7666 14458 {
c77ec726 14459 asection *first = elf_next_in_group (sec);
c2370991 14460
c77ec726
AM
14461 if (first != NULL && elf_next_in_group (first) == first)
14462 /* Check this single member group against linkonce sections. */
14463 for (l = already_linked_list->entry; l != NULL; l = l->next)
14464 if ((l->sec->flags & SEC_GROUP) == 0
14465 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14466 {
14467 first->output_section = bfd_abs_section_ptr;
14468 first->kept_section = l->sec;
14469 sec->output_section = bfd_abs_section_ptr;
14470 break;
14471 }
14472 }
14473 else
14474 /* Check this linkonce section against single member groups. */
14475 for (l = already_linked_list->entry; l != NULL; l = l->next)
14476 if (l->sec->flags & SEC_GROUP)
6d2cd210 14477 {
c77ec726 14478 asection *first = elf_next_in_group (l->sec);
6d2cd210 14479
c77ec726
AM
14480 if (first != NULL
14481 && elf_next_in_group (first) == first
14482 && bfd_elf_match_symbols_in_sections (first, sec, info))
14483 {
14484 sec->output_section = bfd_abs_section_ptr;
14485 sec->kept_section = first;
14486 break;
14487 }
6d2cd210 14488 }
0c511000 14489
c77ec726
AM
14490 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14491 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14492 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14493 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14494 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14495 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14496 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14497 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14498 The reverse order cannot happen as there is never a bfd with only the
14499 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14500 matter as here were are looking only for cross-bfd sections. */
14501
14502 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14503 for (l = already_linked_list->entry; l != NULL; l = l->next)
14504 if ((l->sec->flags & SEC_GROUP) == 0
14505 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14506 {
14507 if (abfd != l->sec->owner)
14508 sec->output_section = bfd_abs_section_ptr;
14509 break;
14510 }
80c29487 14511
082b7297 14512 /* This is the first section with this name. Record it. */
c77ec726 14513 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14514 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14515 return sec->output_section == bfd_abs_section_ptr;
082b7297 14516}
81e1b023 14517
a4d8e49b
L
14518bfd_boolean
14519_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14520{
14521 return sym->st_shndx == SHN_COMMON;
14522}
14523
14524unsigned int
14525_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14526{
14527 return SHN_COMMON;
14528}
14529
14530asection *
14531_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14532{
14533 return bfd_com_section_ptr;
14534}
10455f89
HPN
14535
14536bfd_vma
14537_bfd_elf_default_got_elt_size (bfd *abfd,
14538 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14539 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14540 bfd *ibfd ATTRIBUTE_UNUSED,
14541 unsigned long symndx ATTRIBUTE_UNUSED)
14542{
14543 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14544 return bed->s->arch_size / 8;
14545}
83bac4b0
NC
14546
14547/* Routines to support the creation of dynamic relocs. */
14548
83bac4b0
NC
14549/* Returns the name of the dynamic reloc section associated with SEC. */
14550
14551static const char *
14552get_dynamic_reloc_section_name (bfd * abfd,
14553 asection * sec,
14554 bfd_boolean is_rela)
14555{
ddcf1fcf 14556 char *name;
fd361982 14557 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14558 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14559
ddcf1fcf 14560 if (old_name == NULL)
83bac4b0
NC
14561 return NULL;
14562
ddcf1fcf 14563 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14564 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14565
14566 return name;
14567}
14568
14569/* Returns the dynamic reloc section associated with SEC.
14570 If necessary compute the name of the dynamic reloc section based
14571 on SEC's name (looked up in ABFD's string table) and the setting
14572 of IS_RELA. */
14573
14574asection *
14575_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14576 asection * sec,
14577 bfd_boolean is_rela)
14578{
14579 asection * reloc_sec = elf_section_data (sec)->sreloc;
14580
14581 if (reloc_sec == NULL)
14582 {
14583 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14584
14585 if (name != NULL)
14586 {
3d4d4302 14587 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14588
14589 if (reloc_sec != NULL)
14590 elf_section_data (sec)->sreloc = reloc_sec;
14591 }
14592 }
14593
14594 return reloc_sec;
14595}
14596
14597/* Returns the dynamic reloc section associated with SEC. If the
14598 section does not exist it is created and attached to the DYNOBJ
14599 bfd and stored in the SRELOC field of SEC's elf_section_data
14600 structure.
f8076f98 14601
83bac4b0
NC
14602 ALIGNMENT is the alignment for the newly created section and
14603 IS_RELA defines whether the name should be .rela.<SEC's name>
14604 or .rel.<SEC's name>. The section name is looked up in the
14605 string table associated with ABFD. */
14606
14607asection *
ca4be51c
AM
14608_bfd_elf_make_dynamic_reloc_section (asection *sec,
14609 bfd *dynobj,
14610 unsigned int alignment,
14611 bfd *abfd,
14612 bfd_boolean is_rela)
83bac4b0
NC
14613{
14614 asection * reloc_sec = elf_section_data (sec)->sreloc;
14615
14616 if (reloc_sec == NULL)
14617 {
14618 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14619
14620 if (name == NULL)
14621 return NULL;
14622
3d4d4302 14623 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14624
14625 if (reloc_sec == NULL)
14626 {
3d4d4302
AM
14627 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14628 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14629 if ((sec->flags & SEC_ALLOC) != 0)
14630 flags |= SEC_ALLOC | SEC_LOAD;
14631
3d4d4302 14632 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14633 if (reloc_sec != NULL)
14634 {
8877b5e5
AM
14635 /* _bfd_elf_get_sec_type_attr chooses a section type by
14636 name. Override as it may be wrong, eg. for a user
14637 section named "auto" we'll get ".relauto" which is
14638 seen to be a .rela section. */
14639 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14640 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14641 reloc_sec = NULL;
14642 }
14643 }
14644
14645 elf_section_data (sec)->sreloc = reloc_sec;
14646 }
14647
14648 return reloc_sec;
14649}
1338dd10 14650
bffebb6b
AM
14651/* Copy the ELF symbol type and other attributes for a linker script
14652 assignment from HSRC to HDEST. Generally this should be treated as
14653 if we found a strong non-dynamic definition for HDEST (except that
14654 ld ignores multiple definition errors). */
1338dd10 14655void
bffebb6b
AM
14656_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14657 struct bfd_link_hash_entry *hdest,
14658 struct bfd_link_hash_entry *hsrc)
1338dd10 14659{
bffebb6b
AM
14660 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14661 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14662 Elf_Internal_Sym isym;
1338dd10
PB
14663
14664 ehdest->type = ehsrc->type;
35fc36a8 14665 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14666
14667 isym.st_other = ehsrc->other;
b8417128 14668 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14669}
351f65ca
L
14670
14671/* Append a RELA relocation REL to section S in BFD. */
14672
14673void
14674elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14675{
14676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14677 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14678 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14679 bed->s->swap_reloca_out (abfd, rel, loc);
14680}
14681
14682/* Append a REL relocation REL to section S in BFD. */
14683
14684void
14685elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14686{
14687 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14688 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14689 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14690 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14691}
7dba9362
AM
14692
14693/* Define __start, __stop, .startof. or .sizeof. symbol. */
14694
14695struct bfd_link_hash_entry *
14696bfd_elf_define_start_stop (struct bfd_link_info *info,
14697 const char *symbol, asection *sec)
14698{
487b6440 14699 struct elf_link_hash_entry *h;
7dba9362 14700
487b6440
AM
14701 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14702 FALSE, FALSE, TRUE);
14703 if (h != NULL
14704 && (h->root.type == bfd_link_hash_undefined
14705 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14706 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14707 {
bf3077a6 14708 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14709 h->root.type = bfd_link_hash_defined;
14710 h->root.u.def.section = sec;
14711 h->root.u.def.value = 0;
14712 h->def_regular = 1;
14713 h->def_dynamic = 0;
14714 h->start_stop = 1;
14715 h->u2.start_stop_section = sec;
14716 if (symbol[0] == '.')
14717 {
14718 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14719 const struct elf_backend_data *bed;
14720 bed = get_elf_backend_data (info->output_bfd);
14721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14722 }
36b8fda5
AM
14723 else
14724 {
14725 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14726 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14727 if (was_dynamic)
36b8fda5
AM
14728 bfd_elf_link_record_dynamic_symbol (info, h);
14729 }
487b6440 14730 return &h->root;
7dba9362 14731 }
487b6440 14732 return NULL;
7dba9362 14733}
This page took 3.413802 seconds and 4 git commands to generate.