[gdb/symtab] Enable ada .gdb_index
[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 2645
c9594989 2646 free (alloc1);
45d6a902
AM
2647
2648 /* Don't free alloc2, since if it was allocated we are passing it
2649 back (under the name of internal_relocs). */
2650
2651 return internal_relocs;
2652
2653 error_return:
c9594989 2654 free (alloc1);
45d6a902 2655 if (alloc2 != NULL)
4dd07732
AM
2656 {
2657 if (keep_memory)
2658 bfd_release (abfd, alloc2);
2659 else
2660 free (alloc2);
2661 }
45d6a902
AM
2662 return NULL;
2663}
2664
2665/* Compute the size of, and allocate space for, REL_HDR which is the
2666 section header for a section containing relocations for O. */
2667
28caa186 2668static bfd_boolean
9eaff861
AO
2669_bfd_elf_link_size_reloc_section (bfd *abfd,
2670 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2671{
9eaff861 2672 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2673
2674 /* That allows us to calculate the size of the section. */
9eaff861 2675 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2676
2677 /* The contents field must last into write_object_contents, so we
2678 allocate it with bfd_alloc rather than malloc. Also since we
2679 cannot be sure that the contents will actually be filled in,
2680 we zero the allocated space. */
a50b1753 2681 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2682 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2683 return FALSE;
2684
d4730f92 2685 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2686 {
2687 struct elf_link_hash_entry **p;
2688
ca4be51c
AM
2689 p = ((struct elf_link_hash_entry **)
2690 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2691 if (p == NULL)
2692 return FALSE;
2693
d4730f92 2694 reldata->hashes = p;
45d6a902
AM
2695 }
2696
2697 return TRUE;
2698}
2699
2700/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2701 originated from the section given by INPUT_REL_HDR) to the
2702 OUTPUT_BFD. */
2703
2704bfd_boolean
268b6b39
AM
2705_bfd_elf_link_output_relocs (bfd *output_bfd,
2706 asection *input_section,
2707 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2708 Elf_Internal_Rela *internal_relocs,
2709 struct elf_link_hash_entry **rel_hash
2710 ATTRIBUTE_UNUSED)
45d6a902
AM
2711{
2712 Elf_Internal_Rela *irela;
2713 Elf_Internal_Rela *irelaend;
2714 bfd_byte *erel;
d4730f92 2715 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2716 asection *output_section;
9c5bfbb7 2717 const struct elf_backend_data *bed;
268b6b39 2718 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2719 struct bfd_elf_section_data *esdo;
45d6a902
AM
2720
2721 output_section = input_section->output_section;
45d6a902 2722
d4730f92
BS
2723 bed = get_elf_backend_data (output_bfd);
2724 esdo = elf_section_data (output_section);
2725 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2726 {
d4730f92
BS
2727 output_reldata = &esdo->rel;
2728 swap_out = bed->s->swap_reloc_out;
45d6a902 2729 }
d4730f92
BS
2730 else if (esdo->rela.hdr
2731 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2732 {
d4730f92
BS
2733 output_reldata = &esdo->rela;
2734 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2735 }
2736 else
2737 {
4eca0228 2738 _bfd_error_handler
695344c0 2739 /* xgettext:c-format */
871b3ab2 2740 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2741 output_bfd, input_section->owner, input_section);
297d8443 2742 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2743 return FALSE;
2744 }
2745
d4730f92
BS
2746 erel = output_reldata->hdr->contents;
2747 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2748 irela = internal_relocs;
2749 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2750 * bed->s->int_rels_per_ext_rel);
2751 while (irela < irelaend)
2752 {
2753 (*swap_out) (output_bfd, irela, erel);
2754 irela += bed->s->int_rels_per_ext_rel;
2755 erel += input_rel_hdr->sh_entsize;
2756 }
2757
2758 /* Bump the counter, so that we know where to add the next set of
2759 relocations. */
d4730f92 2760 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2761
2762 return TRUE;
2763}
2764\f
508c3946
L
2765/* Make weak undefined symbols in PIE dynamic. */
2766
2767bfd_boolean
2768_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2769 struct elf_link_hash_entry *h)
2770{
0e1862bb 2771 if (bfd_link_pie (info)
508c3946
L
2772 && h->dynindx == -1
2773 && h->root.type == bfd_link_hash_undefweak)
2774 return bfd_elf_link_record_dynamic_symbol (info, h);
2775
2776 return TRUE;
2777}
2778
45d6a902
AM
2779/* Fix up the flags for a symbol. This handles various cases which
2780 can only be fixed after all the input files are seen. This is
2781 currently called by both adjust_dynamic_symbol and
2782 assign_sym_version, which is unnecessary but perhaps more robust in
2783 the face of future changes. */
2784
28caa186 2785static bfd_boolean
268b6b39
AM
2786_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2787 struct elf_info_failed *eif)
45d6a902 2788{
33774f08 2789 const struct elf_backend_data *bed;
508c3946 2790
45d6a902
AM
2791 /* If this symbol was mentioned in a non-ELF file, try to set
2792 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2793 permit a non-ELF file to correctly refer to a symbol defined in
2794 an ELF dynamic object. */
f5385ebf 2795 if (h->non_elf)
45d6a902
AM
2796 {
2797 while (h->root.type == bfd_link_hash_indirect)
2798 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2799
2800 if (h->root.type != bfd_link_hash_defined
2801 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2802 {
2803 h->ref_regular = 1;
2804 h->ref_regular_nonweak = 1;
2805 }
45d6a902
AM
2806 else
2807 {
2808 if (h->root.u.def.section->owner != NULL
2809 && (bfd_get_flavour (h->root.u.def.section->owner)
2810 == bfd_target_elf_flavour))
f5385ebf
AM
2811 {
2812 h->ref_regular = 1;
2813 h->ref_regular_nonweak = 1;
2814 }
45d6a902 2815 else
f5385ebf 2816 h->def_regular = 1;
45d6a902
AM
2817 }
2818
2819 if (h->dynindx == -1
f5385ebf
AM
2820 && (h->def_dynamic
2821 || h->ref_dynamic))
45d6a902 2822 {
c152c796 2823 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2824 {
2825 eif->failed = TRUE;
2826 return FALSE;
2827 }
2828 }
2829 }
2830 else
2831 {
f5385ebf 2832 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2833 was first seen in a non-ELF file. Fortunately, if the symbol
2834 was first seen in an ELF file, we're probably OK unless the
2835 symbol was defined in a non-ELF file. Catch that case here.
2836 FIXME: We're still in trouble if the symbol was first seen in
2837 a dynamic object, and then later in a non-ELF regular object. */
2838 if ((h->root.type == bfd_link_hash_defined
2839 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2840 && !h->def_regular
45d6a902
AM
2841 && (h->root.u.def.section->owner != NULL
2842 ? (bfd_get_flavour (h->root.u.def.section->owner)
2843 != bfd_target_elf_flavour)
2844 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2845 && !h->def_dynamic)))
2846 h->def_regular = 1;
45d6a902
AM
2847 }
2848
508c3946 2849 /* Backend specific symbol fixup. */
33774f08
AM
2850 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2851 if (bed->elf_backend_fixup_symbol
2852 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2853 return FALSE;
508c3946 2854
45d6a902
AM
2855 /* If this is a final link, and the symbol was defined as a common
2856 symbol in a regular object file, and there was no definition in
2857 any dynamic object, then the linker will have allocated space for
f5385ebf 2858 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2859 flag will not have been set. */
2860 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2861 && !h->def_regular
2862 && h->ref_regular
2863 && !h->def_dynamic
96f29d96 2864 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2865 h->def_regular = 1;
45d6a902 2866
af0bfb9c
AM
2867 /* Symbols defined in discarded sections shouldn't be dynamic. */
2868 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870
4deb8f71
L
2871 /* If a weak undefined symbol has non-default visibility, we also
2872 hide it from the dynamic linker. */
af0bfb9c
AM
2873 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2874 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2875 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2876
2877 /* A hidden versioned symbol in executable should be forced local if
2878 it is is locally defined, not referenced by shared library and not
2879 exported. */
2880 else if (bfd_link_executable (eif->info)
2881 && h->versioned == versioned_hidden
2882 && !eif->info->export_dynamic
2883 && !h->dynamic
2884 && !h->ref_dynamic
2885 && h->def_regular)
2886 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2887
45d6a902
AM
2888 /* If -Bsymbolic was used (which means to bind references to global
2889 symbols to the definition within the shared object), and this
2890 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2891 need a PLT entry. Likewise, if the symbol has non-default
2892 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2893 will force it local. */
4deb8f71
L
2894 else if (h->needs_plt
2895 && bfd_link_pic (eif->info)
2896 && is_elf_hash_table (eif->info->hash)
2897 && (SYMBOLIC_BIND (eif->info, h)
2898 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2899 && h->def_regular)
45d6a902 2900 {
45d6a902
AM
2901 bfd_boolean force_local;
2902
45d6a902
AM
2903 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2904 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2905 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2906 }
2907
45d6a902
AM
2908 /* If this is a weak defined symbol in a dynamic object, and we know
2909 the real definition in the dynamic object, copy interesting flags
2910 over to the real definition. */
60d67dc8 2911 if (h->is_weakalias)
45d6a902 2912 {
60d67dc8
AM
2913 struct elf_link_hash_entry *def = weakdef (h);
2914
45d6a902
AM
2915 /* If the real definition is defined by a regular object file,
2916 don't do anything special. See the longer description in
5b9d7a9a
AM
2917 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2918 bfd_link_hash_defined as it was when put on the alias list
2919 then it must have originally been a versioned symbol (for
2920 which a non-versioned indirect symbol is created) and later
2921 a definition for the non-versioned symbol is found. In that
2922 case the indirection is flipped with the versioned symbol
2923 becoming an indirect pointing at the non-versioned symbol.
2924 Thus, not an alias any more. */
2925 if (def->def_regular
2926 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2927 {
2928 h = def;
2929 while ((h = h->u.alias) != def)
2930 h->is_weakalias = 0;
2931 }
45d6a902 2932 else
a26587ba 2933 {
4e6b54a6
AM
2934 while (h->root.type == bfd_link_hash_indirect)
2935 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2936 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2937 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2938 BFD_ASSERT (def->def_dynamic);
60d67dc8 2939 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2940 }
45d6a902
AM
2941 }
2942
2943 return TRUE;
2944}
2945
2946/* Make the backend pick a good value for a dynamic symbol. This is
2947 called via elf_link_hash_traverse, and also calls itself
2948 recursively. */
2949
28caa186 2950static bfd_boolean
268b6b39 2951_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2952{
a50b1753 2953 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2954 struct elf_link_hash_table *htab;
9c5bfbb7 2955 const struct elf_backend_data *bed;
45d6a902 2956
0eddce27 2957 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2958 return FALSE;
2959
45d6a902
AM
2960 /* Ignore indirect symbols. These are added by the versioning code. */
2961 if (h->root.type == bfd_link_hash_indirect)
2962 return TRUE;
2963
2964 /* Fix the symbol flags. */
2965 if (! _bfd_elf_fix_symbol_flags (h, eif))
2966 return FALSE;
2967
559192d8
AM
2968 htab = elf_hash_table (eif->info);
2969 bed = get_elf_backend_data (htab->dynobj);
2970
954b63d4
AM
2971 if (h->root.type == bfd_link_hash_undefweak)
2972 {
2973 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2974 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2975 else if (eif->info->dynamic_undefined_weak > 0
2976 && h->ref_regular
2977 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2978 && !bfd_hide_sym_by_version (eif->info->version_info,
2979 h->root.root.string))
2980 {
2981 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2982 {
2983 eif->failed = TRUE;
2984 return FALSE;
2985 }
2986 }
2987 }
2988
45d6a902
AM
2989 /* If this symbol does not require a PLT entry, and it is not
2990 defined by a dynamic object, or is not referenced by a regular
2991 object, ignore it. We do have to handle a weak defined symbol,
2992 even if no regular object refers to it, if we decided to add it
2993 to the dynamic symbol table. FIXME: Do we normally need to worry
2994 about symbols which are defined by one dynamic object and
2995 referenced by another one? */
f5385ebf 2996 if (!h->needs_plt
91e21fb7 2997 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2998 && (h->def_regular
2999 || !h->def_dynamic
3000 || (!h->ref_regular
60d67dc8 3001 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3002 {
a6aa5195 3003 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3004 return TRUE;
3005 }
3006
3007 /* If we've already adjusted this symbol, don't do it again. This
3008 can happen via a recursive call. */
f5385ebf 3009 if (h->dynamic_adjusted)
45d6a902
AM
3010 return TRUE;
3011
3012 /* Don't look at this symbol again. Note that we must set this
3013 after checking the above conditions, because we may look at a
3014 symbol once, decide not to do anything, and then get called
3015 recursively later after REF_REGULAR is set below. */
f5385ebf 3016 h->dynamic_adjusted = 1;
45d6a902
AM
3017
3018 /* If this is a weak definition, and we know a real definition, and
3019 the real symbol is not itself defined by a regular object file,
3020 then get a good value for the real definition. We handle the
3021 real symbol first, for the convenience of the backend routine.
3022
3023 Note that there is a confusing case here. If the real definition
3024 is defined by a regular object file, we don't get the real symbol
3025 from the dynamic object, but we do get the weak symbol. If the
3026 processor backend uses a COPY reloc, then if some routine in the
3027 dynamic object changes the real symbol, we will not see that
3028 change in the corresponding weak symbol. This is the way other
3029 ELF linkers work as well, and seems to be a result of the shared
3030 library model.
3031
3032 I will clarify this issue. Most SVR4 shared libraries define the
3033 variable _timezone and define timezone as a weak synonym. The
3034 tzset call changes _timezone. If you write
3035 extern int timezone;
3036 int _timezone = 5;
3037 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3038 you might expect that, since timezone is a synonym for _timezone,
3039 the same number will print both times. However, if the processor
3040 backend uses a COPY reloc, then actually timezone will be copied
3041 into your process image, and, since you define _timezone
3042 yourself, _timezone will not. Thus timezone and _timezone will
3043 wind up at different memory locations. The tzset call will set
3044 _timezone, leaving timezone unchanged. */
3045
60d67dc8 3046 if (h->is_weakalias)
45d6a902 3047 {
60d67dc8
AM
3048 struct elf_link_hash_entry *def = weakdef (h);
3049
ec24dc88 3050 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3051 the alias by a regular object file via the weak symbol H. */
3052 def->ref_regular = 1;
45d6a902 3053
ec24dc88 3054 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3055 the strong alias before H by recursively calling ourselves. */
3056 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3057 return FALSE;
3058 }
3059
3060 /* If a symbol has no type and no size and does not require a PLT
3061 entry, then we are probably about to do the wrong thing here: we
3062 are probably going to create a COPY reloc for an empty object.
3063 This case can arise when a shared object is built with assembly
3064 code, and the assembly code fails to set the symbol type. */
3065 if (h->size == 0
3066 && h->type == STT_NOTYPE
f5385ebf 3067 && !h->needs_plt)
4eca0228 3068 _bfd_error_handler
45d6a902
AM
3069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3070 h->root.root.string);
3071
45d6a902
AM
3072 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3073 {
3074 eif->failed = TRUE;
3075 return FALSE;
3076 }
3077
3078 return TRUE;
3079}
3080
027297b7
L
3081/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3082 DYNBSS. */
3083
3084bfd_boolean
6cabe1ea
AM
3085_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3086 struct elf_link_hash_entry *h,
027297b7
L
3087 asection *dynbss)
3088{
91ac5911 3089 unsigned int power_of_two;
027297b7
L
3090 bfd_vma mask;
3091 asection *sec = h->root.u.def.section;
3092
de194d85 3093 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3094 requirement of symbols defined in the section. Since we don't
3095 know the symbol alignment requirement, we start with the
3096 maximum alignment and check low bits of the symbol address
3097 for the minimum alignment. */
fd361982 3098 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3099 mask = ((bfd_vma) 1 << power_of_two) - 1;
3100 while ((h->root.u.def.value & mask) != 0)
3101 {
3102 mask >>= 1;
3103 --power_of_two;
3104 }
027297b7 3105
fd361982 3106 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3107 {
3108 /* Adjust the section alignment if needed. */
fd361982 3109 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3110 return FALSE;
3111 }
3112
91ac5911 3113 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3114 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3115
3116 /* Define the symbol as being at this point in DYNBSS. */
3117 h->root.u.def.section = dynbss;
3118 h->root.u.def.value = dynbss->size;
3119
3120 /* Increment the size of DYNBSS to make room for the symbol. */
3121 dynbss->size += h->size;
3122
f7483970
L
3123 /* No error if extern_protected_data is true. */
3124 if (h->protected_def
889c2a67
L
3125 && (!info->extern_protected_data
3126 || (info->extern_protected_data < 0
3127 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3128 info->callbacks->einfo
c1c8c1ef 3129 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3130 h->root.root.string);
6cabe1ea 3131
027297b7
L
3132 return TRUE;
3133}
3134
45d6a902
AM
3135/* Adjust all external symbols pointing into SEC_MERGE sections
3136 to reflect the object merging within the sections. */
3137
28caa186 3138static bfd_boolean
268b6b39 3139_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3140{
3141 asection *sec;
3142
45d6a902
AM
3143 if ((h->root.type == bfd_link_hash_defined
3144 || h->root.type == bfd_link_hash_defweak)
3145 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3146 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3147 {
a50b1753 3148 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3149
3150 h->root.u.def.value =
3151 _bfd_merged_section_offset (output_bfd,
3152 &h->root.u.def.section,
3153 elf_section_data (sec)->sec_info,
753731ee 3154 h->root.u.def.value);
45d6a902
AM
3155 }
3156
3157 return TRUE;
3158}
986a241f
RH
3159
3160/* Returns false if the symbol referred to by H should be considered
3161 to resolve local to the current module, and true if it should be
3162 considered to bind dynamically. */
3163
3164bfd_boolean
268b6b39
AM
3165_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3166 struct bfd_link_info *info,
89a2ee5a 3167 bfd_boolean not_local_protected)
986a241f
RH
3168{
3169 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3170 const struct elf_backend_data *bed;
3171 struct elf_link_hash_table *hash_table;
986a241f
RH
3172
3173 if (h == NULL)
3174 return FALSE;
3175
3176 while (h->root.type == bfd_link_hash_indirect
3177 || h->root.type == bfd_link_hash_warning)
3178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3179
3180 /* If it was forced local, then clearly it's not dynamic. */
3181 if (h->dynindx == -1)
3182 return FALSE;
f5385ebf 3183 if (h->forced_local)
986a241f
RH
3184 return FALSE;
3185
3186 /* Identify the cases where name binding rules say that a
3187 visible symbol resolves locally. */
0e1862bb
L
3188 binding_stays_local_p = (bfd_link_executable (info)
3189 || SYMBOLIC_BIND (info, h));
986a241f
RH
3190
3191 switch (ELF_ST_VISIBILITY (h->other))
3192 {
3193 case STV_INTERNAL:
3194 case STV_HIDDEN:
3195 return FALSE;
3196
3197 case STV_PROTECTED:
fcb93ecf
PB
3198 hash_table = elf_hash_table (info);
3199 if (!is_elf_hash_table (hash_table))
3200 return FALSE;
3201
3202 bed = get_elf_backend_data (hash_table->dynobj);
3203
986a241f
RH
3204 /* Proper resolution for function pointer equality may require
3205 that these symbols perhaps be resolved dynamically, even though
3206 we should be resolving them to the current module. */
89a2ee5a 3207 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3208 binding_stays_local_p = TRUE;
3209 break;
3210
3211 default:
986a241f
RH
3212 break;
3213 }
3214
aa37626c 3215 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3216 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3217 return TRUE;
3218
986a241f
RH
3219 /* Otherwise, the symbol is dynamic if binding rules don't tell
3220 us that it remains local. */
3221 return !binding_stays_local_p;
3222}
f6c52c13
AM
3223
3224/* Return true if the symbol referred to by H should be considered
3225 to resolve local to the current module, and false otherwise. Differs
3226 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3227 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3228 for the place where dynindx == -1 is tested. If that test is true,
3229 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3230 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3231 defined symbols.
89a2ee5a
AM
3232 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3233 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3234 treatment of undefined weak symbols. For those that do not make
3235 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3236
3237bfd_boolean
268b6b39
AM
3238_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3239 struct bfd_link_info *info,
3240 bfd_boolean local_protected)
f6c52c13 3241{
fcb93ecf
PB
3242 const struct elf_backend_data *bed;
3243 struct elf_link_hash_table *hash_table;
3244
f6c52c13
AM
3245 /* If it's a local sym, of course we resolve locally. */
3246 if (h == NULL)
3247 return TRUE;
3248
d95edcac
L
3249 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3250 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3251 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3252 return TRUE;
3253
0fad2956
MR
3254 /* Forced local symbols resolve locally. */
3255 if (h->forced_local)
3256 return TRUE;
3257
7e2294f9
AO
3258 /* Common symbols that become definitions don't get the DEF_REGULAR
3259 flag set, so test it first, and don't bail out. */
3260 if (ELF_COMMON_DEF_P (h))
3261 /* Do nothing. */;
f6c52c13 3262 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3263 resolve locally. The sym is either undefined or dynamic. */
3264 else if (!h->def_regular)
f6c52c13
AM
3265 return FALSE;
3266
0fad2956 3267 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3268 if (h->dynindx == -1)
3269 return TRUE;
3270
3271 /* At this point, we know the symbol is defined and dynamic. In an
3272 executable it must resolve locally, likewise when building symbolic
3273 shared libraries. */
0e1862bb 3274 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3275 return TRUE;
3276
3277 /* Now deal with defined dynamic symbols in shared libraries. Ones
3278 with default visibility might not resolve locally. */
3279 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3280 return FALSE;
3281
fcb93ecf
PB
3282 hash_table = elf_hash_table (info);
3283 if (!is_elf_hash_table (hash_table))
3284 return TRUE;
3285
3286 bed = get_elf_backend_data (hash_table->dynobj);
3287
f7483970
L
3288 /* If extern_protected_data is false, STV_PROTECTED non-function
3289 symbols are local. */
889c2a67
L
3290 if ((!info->extern_protected_data
3291 || (info->extern_protected_data < 0
3292 && !bed->extern_protected_data))
3293 && !bed->is_function_type (h->type))
1c16dfa5
L
3294 return TRUE;
3295
f6c52c13 3296 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3297 symbols be treated as dynamic symbols. If the address of a
3298 function not defined in an executable is set to that function's
3299 plt entry in the executable, then the address of the function in
3300 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3301 return local_protected;
3302}
e1918d23
AM
3303
3304/* Caches some TLS segment info, and ensures that the TLS segment vma is
3305 aligned. Returns the first TLS output section. */
3306
3307struct bfd_section *
3308_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3309{
3310 struct bfd_section *sec, *tls;
3311 unsigned int align = 0;
3312
3313 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3314 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3315 break;
3316 tls = sec;
3317
3318 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3319 if (sec->alignment_power > align)
3320 align = sec->alignment_power;
3321
3322 elf_hash_table (info)->tls_sec = tls;
3323
fdde2fb6
SH
3324 /* Ensure the alignment of the first section (usually .tdata) is the largest
3325 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3326 if (tls != NULL)
3327 tls->alignment_power = align;
3328
3329 return tls;
3330}
0ad989f9
L
3331
3332/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3333static bfd_boolean
3334is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3335 Elf_Internal_Sym *sym)
3336{
a4d8e49b
L
3337 const struct elf_backend_data *bed;
3338
0ad989f9
L
3339 /* Local symbols do not count, but target specific ones might. */
3340 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3341 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3342 return FALSE;
3343
fcb93ecf 3344 bed = get_elf_backend_data (abfd);
0ad989f9 3345 /* Function symbols do not count. */
fcb93ecf 3346 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3347 return FALSE;
3348
3349 /* If the section is undefined, then so is the symbol. */
3350 if (sym->st_shndx == SHN_UNDEF)
3351 return FALSE;
3352
3353 /* If the symbol is defined in the common section, then
3354 it is a common definition and so does not count. */
a4d8e49b 3355 if (bed->common_definition (sym))
0ad989f9
L
3356 return FALSE;
3357
3358 /* If the symbol is in a target specific section then we
3359 must rely upon the backend to tell us what it is. */
3360 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3361 /* FIXME - this function is not coded yet:
3362
3363 return _bfd_is_global_symbol_definition (abfd, sym);
3364
3365 Instead for now assume that the definition is not global,
3366 Even if this is wrong, at least the linker will behave
3367 in the same way that it used to do. */
3368 return FALSE;
3369
3370 return TRUE;
3371}
3372
3373/* Search the symbol table of the archive element of the archive ABFD
3374 whose archive map contains a mention of SYMDEF, and determine if
3375 the symbol is defined in this element. */
3376static bfd_boolean
3377elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3378{
3379 Elf_Internal_Shdr * hdr;
ef53be89
AM
3380 size_t symcount;
3381 size_t extsymcount;
3382 size_t extsymoff;
0ad989f9
L
3383 Elf_Internal_Sym *isymbuf;
3384 Elf_Internal_Sym *isym;
3385 Elf_Internal_Sym *isymend;
3386 bfd_boolean result;
3387
3388 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3389 if (abfd == NULL)
3390 return FALSE;
3391
3392 if (! bfd_check_format (abfd, bfd_object))
3393 return FALSE;
3394
7dc3990e
L
3395 /* Select the appropriate symbol table. If we don't know if the
3396 object file is an IR object, give linker LTO plugin a chance to
3397 get the correct symbol table. */
3398 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3399#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3400 || (abfd->plugin_format == bfd_plugin_unknown
3401 && bfd_link_plugin_object_p (abfd))
3402#endif
3403 )
3404 {
3405 /* Use the IR symbol table if the object has been claimed by
3406 plugin. */
3407 abfd = abfd->plugin_dummy_bfd;
3408 hdr = &elf_tdata (abfd)->symtab_hdr;
3409 }
3410 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3411 hdr = &elf_tdata (abfd)->symtab_hdr;
3412 else
3413 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3414
3415 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3416
3417 /* The sh_info field of the symtab header tells us where the
3418 external symbols start. We don't care about the local symbols. */
3419 if (elf_bad_symtab (abfd))
3420 {
3421 extsymcount = symcount;
3422 extsymoff = 0;
3423 }
3424 else
3425 {
3426 extsymcount = symcount - hdr->sh_info;
3427 extsymoff = hdr->sh_info;
3428 }
3429
3430 if (extsymcount == 0)
3431 return FALSE;
3432
3433 /* Read in the symbol table. */
3434 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3435 NULL, NULL, NULL);
3436 if (isymbuf == NULL)
3437 return FALSE;
3438
3439 /* Scan the symbol table looking for SYMDEF. */
3440 result = FALSE;
3441 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3442 {
3443 const char *name;
3444
3445 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3446 isym->st_name);
3447 if (name == NULL)
3448 break;
3449
3450 if (strcmp (name, symdef->name) == 0)
3451 {
3452 result = is_global_data_symbol_definition (abfd, isym);
3453 break;
3454 }
3455 }
3456
3457 free (isymbuf);
3458
3459 return result;
3460}
3461\f
5a580b3a
AM
3462/* Add an entry to the .dynamic table. */
3463
3464bfd_boolean
3465_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3466 bfd_vma tag,
3467 bfd_vma val)
3468{
3469 struct elf_link_hash_table *hash_table;
3470 const struct elf_backend_data *bed;
3471 asection *s;
3472 bfd_size_type newsize;
3473 bfd_byte *newcontents;
3474 Elf_Internal_Dyn dyn;
3475
3476 hash_table = elf_hash_table (info);
3477 if (! is_elf_hash_table (hash_table))
3478 return FALSE;
3479
7f923b7f
AM
3480 if (tag == DT_RELA || tag == DT_REL)
3481 hash_table->dynamic_relocs = TRUE;
3482
5a580b3a 3483 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3484 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3485 BFD_ASSERT (s != NULL);
3486
eea6121a 3487 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3488 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3489 if (newcontents == NULL)
3490 return FALSE;
3491
3492 dyn.d_tag = tag;
3493 dyn.d_un.d_val = val;
eea6121a 3494 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3495
eea6121a 3496 s->size = newsize;
5a580b3a
AM
3497 s->contents = newcontents;
3498
3499 return TRUE;
3500}
3501
6f6fd151
L
3502/* Strip zero-sized dynamic sections. */
3503
3504bfd_boolean
3505_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3506{
3507 struct elf_link_hash_table *hash_table;
3508 const struct elf_backend_data *bed;
3509 asection *s, *sdynamic, **pp;
3510 asection *rela_dyn, *rel_dyn;
3511 Elf_Internal_Dyn dyn;
3512 bfd_byte *extdyn, *next;
3513 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3514 bfd_boolean strip_zero_sized;
3515 bfd_boolean strip_zero_sized_plt;
3516
3517 if (bfd_link_relocatable (info))
3518 return TRUE;
3519
3520 hash_table = elf_hash_table (info);
3521 if (!is_elf_hash_table (hash_table))
3522 return FALSE;
3523
3524 if (!hash_table->dynobj)
3525 return TRUE;
3526
3527 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3528 if (!sdynamic)
3529 return TRUE;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 swap_dyn_in = bed->s->swap_dyn_in;
3533
3534 strip_zero_sized = FALSE;
3535 strip_zero_sized_plt = FALSE;
3536
3537 /* Strip zero-sized dynamic sections. */
3538 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3539 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3540 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3541 if (s->size == 0
3542 && (s == rela_dyn
3543 || s == rel_dyn
3544 || s == hash_table->srelplt->output_section
3545 || s == hash_table->splt->output_section))
3546 {
3547 *pp = s->next;
3548 info->output_bfd->section_count--;
3549 strip_zero_sized = TRUE;
3550 if (s == rela_dyn)
3551 s = rela_dyn;
3552 if (s == rel_dyn)
3553 s = rel_dyn;
3554 else if (s == hash_table->splt->output_section)
3555 {
3556 s = hash_table->splt;
3557 strip_zero_sized_plt = TRUE;
3558 }
3559 else
3560 s = hash_table->srelplt;
3561 s->flags |= SEC_EXCLUDE;
3562 s->output_section = bfd_abs_section_ptr;
3563 }
3564 else
3565 pp = &s->next;
3566
3567 if (strip_zero_sized_plt)
3568 for (extdyn = sdynamic->contents;
3569 extdyn < sdynamic->contents + sdynamic->size;
3570 extdyn = next)
3571 {
3572 next = extdyn + bed->s->sizeof_dyn;
3573 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3574 switch (dyn.d_tag)
3575 {
3576 default:
3577 break;
3578 case DT_JMPREL:
3579 case DT_PLTRELSZ:
3580 case DT_PLTREL:
3581 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3582 the procedure linkage table (the .plt section) has been
3583 removed. */
3584 memmove (extdyn, next,
3585 sdynamic->size - (next - sdynamic->contents));
3586 next = extdyn;
3587 }
3588 }
3589
3590 if (strip_zero_sized)
3591 {
3592 /* Regenerate program headers. */
3593 elf_seg_map (info->output_bfd) = NULL;
3594 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3595 }
3596
3597 return TRUE;
3598}
3599
e310298c 3600/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3601 1 if a DT_NEEDED tag already exists, and 0 on success. */
3602
e310298c
AM
3603int
3604bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3605{
3606 struct elf_link_hash_table *hash_table;
ef53be89 3607 size_t strindex;
e310298c 3608 const char *soname;
5a580b3a 3609
7e9f0867
AM
3610 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3611 return -1;
3612
5a580b3a 3613 hash_table = elf_hash_table (info);
e310298c 3614 soname = elf_dt_name (abfd);
5a580b3a 3615 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3616 if (strindex == (size_t) -1)
5a580b3a
AM
3617 return -1;
3618
02be4619 3619 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3620 {
3621 asection *sdyn;
3622 const struct elf_backend_data *bed;
3623 bfd_byte *extdyn;
3624
3625 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3626 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3627 if (sdyn != NULL)
3628 for (extdyn = sdyn->contents;
3629 extdyn < sdyn->contents + sdyn->size;
3630 extdyn += bed->s->sizeof_dyn)
3631 {
3632 Elf_Internal_Dyn dyn;
5a580b3a 3633
7e9f0867
AM
3634 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3635 if (dyn.d_tag == DT_NEEDED
3636 && dyn.d_un.d_val == strindex)
3637 {
3638 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3639 return 1;
3640 }
3641 }
5a580b3a
AM
3642 }
3643
e310298c
AM
3644 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3645 return -1;
7e9f0867 3646
e310298c
AM
3647 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3648 return -1;
5a580b3a
AM
3649
3650 return 0;
3651}
3652
7b15fa7a
AM
3653/* Return true if SONAME is on the needed list between NEEDED and STOP
3654 (or the end of list if STOP is NULL), and needed by a library that
3655 will be loaded. */
3656
010e5ae2 3657static bfd_boolean
7b15fa7a
AM
3658on_needed_list (const char *soname,
3659 struct bfd_link_needed_list *needed,
3660 struct bfd_link_needed_list *stop)
010e5ae2 3661{
7b15fa7a
AM
3662 struct bfd_link_needed_list *look;
3663 for (look = needed; look != stop; look = look->next)
3664 if (strcmp (soname, look->name) == 0
3665 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3666 /* If needed by a library that itself is not directly
3667 needed, recursively check whether that library is
3668 indirectly needed. Since we add DT_NEEDED entries to
3669 the end of the list, library dependencies appear after
3670 the library. Therefore search prior to the current
3671 LOOK, preventing possible infinite recursion. */
3672 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3673 return TRUE;
3674
3675 return FALSE;
3676}
3677
3a3f4bf7 3678/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3679static int
3680elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3681{
3682 const struct elf_link_hash_entry *h1;
3683 const struct elf_link_hash_entry *h2;
10b7e05b 3684 bfd_signed_vma vdiff;
3a3f4bf7
AM
3685 int sdiff;
3686 const char *n1;
3687 const char *n2;
5a580b3a
AM
3688
3689 h1 = *(const struct elf_link_hash_entry **) arg1;
3690 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3691 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3692 if (vdiff != 0)
3693 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3694
3695 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3696 if (sdiff != 0)
3697 return sdiff;
3698
3699 /* Sort so that sized symbols are selected over zero size symbols. */
3700 vdiff = h1->size - h2->size;
3701 if (vdiff != 0)
3702 return vdiff > 0 ? 1 : -1;
3703
3704 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3705 if (h1->type != h2->type)
3706 return h1->type - h2->type;
3707
3708 /* If symbols are properly sized and typed, and multiple strong
3709 aliases are not defined in a shared library by the user we
3710 shouldn't get here. Unfortunately linker script symbols like
3711 __bss_start sometimes match a user symbol defined at the start of
3712 .bss without proper size and type. We'd like to preference the
3713 user symbol over reserved system symbols. Sort on leading
3714 underscores. */
3715 n1 = h1->root.root.string;
3716 n2 = h2->root.root.string;
3717 while (*n1 == *n2)
10b7e05b 3718 {
3a3f4bf7
AM
3719 if (*n1 == 0)
3720 break;
3721 ++n1;
3722 ++n2;
10b7e05b 3723 }
3a3f4bf7
AM
3724 if (*n1 == '_')
3725 return -1;
3726 if (*n2 == '_')
3727 return 1;
3728
3729 /* Final sort on name selects user symbols like '_u' over reserved
3730 system symbols like '_Z' and also will avoid qsort instability. */
3731 return *n1 - *n2;
5a580b3a 3732}
4ad4eba5 3733
5a580b3a
AM
3734/* This function is used to adjust offsets into .dynstr for
3735 dynamic symbols. This is called via elf_link_hash_traverse. */
3736
3737static bfd_boolean
3738elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3739{
a50b1753 3740 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3741
5a580b3a
AM
3742 if (h->dynindx != -1)
3743 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3744 return TRUE;
3745}
3746
3747/* Assign string offsets in .dynstr, update all structures referencing
3748 them. */
3749
4ad4eba5
AM
3750static bfd_boolean
3751elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3752{
3753 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3754 struct elf_link_local_dynamic_entry *entry;
3755 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3756 bfd *dynobj = hash_table->dynobj;
3757 asection *sdyn;
3758 bfd_size_type size;
3759 const struct elf_backend_data *bed;
3760 bfd_byte *extdyn;
3761
3762 _bfd_elf_strtab_finalize (dynstr);
3763 size = _bfd_elf_strtab_size (dynstr);
3764
3765 bed = get_elf_backend_data (dynobj);
3d4d4302 3766 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3767 BFD_ASSERT (sdyn != NULL);
3768
3769 /* Update all .dynamic entries referencing .dynstr strings. */
3770 for (extdyn = sdyn->contents;
eea6121a 3771 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3772 extdyn += bed->s->sizeof_dyn)
3773 {
3774 Elf_Internal_Dyn dyn;
3775
3776 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3777 switch (dyn.d_tag)
3778 {
3779 case DT_STRSZ:
3780 dyn.d_un.d_val = size;
3781 break;
3782 case DT_NEEDED:
3783 case DT_SONAME:
3784 case DT_RPATH:
3785 case DT_RUNPATH:
3786 case DT_FILTER:
3787 case DT_AUXILIARY:
7ee314fa
AM
3788 case DT_AUDIT:
3789 case DT_DEPAUDIT:
5a580b3a
AM
3790 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3791 break;
3792 default:
3793 continue;
3794 }
3795 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3796 }
3797
3798 /* Now update local dynamic symbols. */
3799 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3800 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3801 entry->isym.st_name);
3802
3803 /* And the rest of dynamic symbols. */
3804 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3805
3806 /* Adjust version definitions. */
3807 if (elf_tdata (output_bfd)->cverdefs)
3808 {
3809 asection *s;
3810 bfd_byte *p;
ef53be89 3811 size_t i;
5a580b3a
AM
3812 Elf_Internal_Verdef def;
3813 Elf_Internal_Verdaux defaux;
3814
3d4d4302 3815 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3816 p = s->contents;
3817 do
3818 {
3819 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3820 &def);
3821 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3822 if (def.vd_aux != sizeof (Elf_External_Verdef))
3823 continue;
5a580b3a
AM
3824 for (i = 0; i < def.vd_cnt; ++i)
3825 {
3826 _bfd_elf_swap_verdaux_in (output_bfd,
3827 (Elf_External_Verdaux *) p, &defaux);
3828 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3829 defaux.vda_name);
3830 _bfd_elf_swap_verdaux_out (output_bfd,
3831 &defaux, (Elf_External_Verdaux *) p);
3832 p += sizeof (Elf_External_Verdaux);
3833 }
3834 }
3835 while (def.vd_next);
3836 }
3837
3838 /* Adjust version references. */
3839 if (elf_tdata (output_bfd)->verref)
3840 {
3841 asection *s;
3842 bfd_byte *p;
ef53be89 3843 size_t i;
5a580b3a
AM
3844 Elf_Internal_Verneed need;
3845 Elf_Internal_Vernaux needaux;
3846
3d4d4302 3847 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3848 p = s->contents;
3849 do
3850 {
3851 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3852 &need);
3853 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3854 _bfd_elf_swap_verneed_out (output_bfd, &need,
3855 (Elf_External_Verneed *) p);
3856 p += sizeof (Elf_External_Verneed);
3857 for (i = 0; i < need.vn_cnt; ++i)
3858 {
3859 _bfd_elf_swap_vernaux_in (output_bfd,
3860 (Elf_External_Vernaux *) p, &needaux);
3861 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3862 needaux.vna_name);
3863 _bfd_elf_swap_vernaux_out (output_bfd,
3864 &needaux,
3865 (Elf_External_Vernaux *) p);
3866 p += sizeof (Elf_External_Vernaux);
3867 }
3868 }
3869 while (need.vn_next);
3870 }
3871
3872 return TRUE;
3873}
3874\f
13285a1b
AM
3875/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3876 The default is to only match when the INPUT and OUTPUT are exactly
3877 the same target. */
3878
3879bfd_boolean
3880_bfd_elf_default_relocs_compatible (const bfd_target *input,
3881 const bfd_target *output)
3882{
3883 return input == output;
3884}
3885
3886/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3887 This version is used when different targets for the same architecture
3888 are virtually identical. */
3889
3890bfd_boolean
3891_bfd_elf_relocs_compatible (const bfd_target *input,
3892 const bfd_target *output)
3893{
3894 const struct elf_backend_data *obed, *ibed;
3895
3896 if (input == output)
3897 return TRUE;
3898
3899 ibed = xvec_get_elf_backend_data (input);
3900 obed = xvec_get_elf_backend_data (output);
3901
3902 if (ibed->arch != obed->arch)
3903 return FALSE;
3904
3905 /* If both backends are using this function, deem them compatible. */
3906 return ibed->relocs_compatible == obed->relocs_compatible;
3907}
3908
e5034e59
AM
3909/* Make a special call to the linker "notice" function to tell it that
3910 we are about to handle an as-needed lib, or have finished
1b786873 3911 processing the lib. */
e5034e59
AM
3912
3913bfd_boolean
3914_bfd_elf_notice_as_needed (bfd *ibfd,
3915 struct bfd_link_info *info,
3916 enum notice_asneeded_action act)
3917{
46135103 3918 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3919}
3920
d9689752
L
3921/* Check relocations an ELF object file. */
3922
3923bfd_boolean
3924_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3925{
3926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3927 struct elf_link_hash_table *htab = elf_hash_table (info);
3928
3929 /* If this object is the same format as the output object, and it is
3930 not a shared library, then let the backend look through the
3931 relocs.
3932
3933 This is required to build global offset table entries and to
3934 arrange for dynamic relocs. It is not required for the
3935 particular common case of linking non PIC code, even when linking
3936 against shared libraries, but unfortunately there is no way of
3937 knowing whether an object file has been compiled PIC or not.
3938 Looking through the relocs is not particularly time consuming.
3939 The problem is that we must either (1) keep the relocs in memory,
3940 which causes the linker to require additional runtime memory or
3941 (2) read the relocs twice from the input file, which wastes time.
3942 This would be a good case for using mmap.
3943
3944 I have no idea how to handle linking PIC code into a file of a
3945 different format. It probably can't be done. */
3946 if ((abfd->flags & DYNAMIC) == 0
3947 && is_elf_hash_table (htab)
3948 && bed->check_relocs != NULL
3949 && elf_object_id (abfd) == elf_hash_table_id (htab)
3950 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3951 {
3952 asection *o;
3953
3954 for (o = abfd->sections; o != NULL; o = o->next)
3955 {
3956 Elf_Internal_Rela *internal_relocs;
3957 bfd_boolean ok;
3958
c4b126b8
L
3959 /* Don't check relocations in excluded sections. Don't do
3960 anything special with non-loaded, non-alloced sections.
3961 In particular, any relocs in such sections should not
3962 affect GOT and PLT reference counting (ie. we don't
3963 allow them to create GOT or PLT entries), there's no
3964 possibility or desire to optimize TLS relocs, and
3965 there's not much point in propagating relocs to shared
3966 libs that the dynamic linker won't relocate. */
3967 if ((o->flags & SEC_ALLOC) == 0
3968 || (o->flags & SEC_RELOC) == 0
5ce03cea 3969 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3970 || o->reloc_count == 0
3971 || ((info->strip == strip_all || info->strip == strip_debugger)
3972 && (o->flags & SEC_DEBUGGING) != 0)
3973 || bfd_is_abs_section (o->output_section))
3974 continue;
3975
3976 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3977 info->keep_memory);
3978 if (internal_relocs == NULL)
3979 return FALSE;
3980
3981 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3982
3983 if (elf_section_data (o)->relocs != internal_relocs)
3984 free (internal_relocs);
3985
3986 if (! ok)
3987 return FALSE;
3988 }
3989 }
3990
3991 return TRUE;
3992}
3993
4ad4eba5
AM
3994/* Add symbols from an ELF object file to the linker hash table. */
3995
3996static bfd_boolean
3997elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3998{
a0c402a5 3999 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4000 Elf_Internal_Shdr *hdr;
ef53be89
AM
4001 size_t symcount;
4002 size_t extsymcount;
4003 size_t extsymoff;
4ad4eba5
AM
4004 struct elf_link_hash_entry **sym_hash;
4005 bfd_boolean dynamic;
4006 Elf_External_Versym *extversym = NULL;
be22c732 4007 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4008 Elf_External_Versym *ever;
4009 struct elf_link_hash_entry *weaks;
4010 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4011 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4012 Elf_Internal_Sym *isymbuf = NULL;
4013 Elf_Internal_Sym *isym;
4014 Elf_Internal_Sym *isymend;
4015 const struct elf_backend_data *bed;
4016 bfd_boolean add_needed;
66eb6687 4017 struct elf_link_hash_table *htab;
66eb6687 4018 void *alloc_mark = NULL;
4f87808c
AM
4019 struct bfd_hash_entry **old_table = NULL;
4020 unsigned int old_size = 0;
4021 unsigned int old_count = 0;
66eb6687 4022 void *old_tab = NULL;
66eb6687
AM
4023 void *old_ent;
4024 struct bfd_link_hash_entry *old_undefs = NULL;
4025 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4026 void *old_strtab = NULL;
66eb6687 4027 size_t tabsize = 0;
db6a5d5f 4028 asection *s;
29a9f53e 4029 bfd_boolean just_syms;
4ad4eba5 4030
66eb6687 4031 htab = elf_hash_table (info);
4ad4eba5 4032 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4033
4034 if ((abfd->flags & DYNAMIC) == 0)
4035 dynamic = FALSE;
4036 else
4037 {
4038 dynamic = TRUE;
4039
4040 /* You can't use -r against a dynamic object. Also, there's no
4041 hope of using a dynamic object which does not exactly match
4042 the format of the output file. */
0e1862bb 4043 if (bfd_link_relocatable (info)
66eb6687 4044 || !is_elf_hash_table (htab)
f13a99db 4045 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4046 {
0e1862bb 4047 if (bfd_link_relocatable (info))
9a0789ec
NC
4048 bfd_set_error (bfd_error_invalid_operation);
4049 else
4050 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4051 goto error_return;
4052 }
4053 }
4054
a0c402a5
L
4055 ehdr = elf_elfheader (abfd);
4056 if (info->warn_alternate_em
4057 && bed->elf_machine_code != ehdr->e_machine
4058 && ((bed->elf_machine_alt1 != 0
4059 && ehdr->e_machine == bed->elf_machine_alt1)
4060 || (bed->elf_machine_alt2 != 0
4061 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4062 _bfd_error_handler
695344c0 4063 /* xgettext:c-format */
9793eb77 4064 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4065 ehdr->e_machine, abfd, bed->elf_machine_code);
4066
4ad4eba5
AM
4067 /* As a GNU extension, any input sections which are named
4068 .gnu.warning.SYMBOL are treated as warning symbols for the given
4069 symbol. This differs from .gnu.warning sections, which generate
4070 warnings when they are included in an output file. */
dd98f8d2 4071 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4072 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4073 {
db6a5d5f 4074 const char *name;
4ad4eba5 4075
fd361982 4076 name = bfd_section_name (s);
db6a5d5f 4077 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4078 {
db6a5d5f
AM
4079 char *msg;
4080 bfd_size_type sz;
4081
4082 name += sizeof ".gnu.warning." - 1;
4083
4084 /* If this is a shared object, then look up the symbol
4085 in the hash table. If it is there, and it is already
4086 been defined, then we will not be using the entry
4087 from this shared object, so we don't need to warn.
4088 FIXME: If we see the definition in a regular object
4089 later on, we will warn, but we shouldn't. The only
4090 fix is to keep track of what warnings we are supposed
4091 to emit, and then handle them all at the end of the
4092 link. */
4093 if (dynamic)
4ad4eba5 4094 {
db6a5d5f
AM
4095 struct elf_link_hash_entry *h;
4096
4097 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4098
4099 /* FIXME: What about bfd_link_hash_common? */
4100 if (h != NULL
4101 && (h->root.type == bfd_link_hash_defined
4102 || h->root.type == bfd_link_hash_defweak))
4103 continue;
4104 }
4ad4eba5 4105
db6a5d5f
AM
4106 sz = s->size;
4107 msg = (char *) bfd_alloc (abfd, sz + 1);
4108 if (msg == NULL)
4109 goto error_return;
4ad4eba5 4110
db6a5d5f
AM
4111 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4112 goto error_return;
4ad4eba5 4113
db6a5d5f 4114 msg[sz] = '\0';
4ad4eba5 4115
db6a5d5f
AM
4116 if (! (_bfd_generic_link_add_one_symbol
4117 (info, abfd, name, BSF_WARNING, s, 0, msg,
4118 FALSE, bed->collect, NULL)))
4119 goto error_return;
4ad4eba5 4120
0e1862bb 4121 if (bfd_link_executable (info))
db6a5d5f
AM
4122 {
4123 /* Clobber the section size so that the warning does
4124 not get copied into the output file. */
4125 s->size = 0;
11d2f718 4126
db6a5d5f
AM
4127 /* Also set SEC_EXCLUDE, so that symbols defined in
4128 the warning section don't get copied to the output. */
4129 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4130 }
4131 }
4132 }
4133
29a9f53e
L
4134 just_syms = ((s = abfd->sections) != NULL
4135 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4136
4ad4eba5
AM
4137 add_needed = TRUE;
4138 if (! dynamic)
4139 {
4140 /* If we are creating a shared library, create all the dynamic
4141 sections immediately. We need to attach them to something,
4142 so we attach them to this BFD, provided it is the right
bf89386a
L
4143 format and is not from ld --just-symbols. Always create the
4144 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4145 are no input BFD's of the same format as the output, we can't
4146 make a shared library. */
4147 if (!just_syms
bf89386a 4148 && (bfd_link_pic (info)
9c1d7a08 4149 || (!bfd_link_relocatable (info)
3c5fce9b 4150 && info->nointerp
9c1d7a08 4151 && (info->export_dynamic || info->dynamic)))
66eb6687 4152 && is_elf_hash_table (htab)
f13a99db 4153 && info->output_bfd->xvec == abfd->xvec
66eb6687 4154 && !htab->dynamic_sections_created)
4ad4eba5
AM
4155 {
4156 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4157 goto error_return;
4158 }
4159 }
66eb6687 4160 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4161 goto error_return;
4162 else
4163 {
4ad4eba5 4164 const char *soname = NULL;
7ee314fa 4165 char *audit = NULL;
4ad4eba5 4166 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4167 const Elf_Internal_Phdr *phdr;
e310298c 4168 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4169
4170 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4171 ld shouldn't allow it. */
29a9f53e 4172 if (just_syms)
92fd189d 4173 abort ();
4ad4eba5
AM
4174
4175 /* If this dynamic lib was specified on the command line with
4176 --as-needed in effect, then we don't want to add a DT_NEEDED
4177 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4178 in by another lib's DT_NEEDED. When --no-add-needed is used
4179 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4180 any dynamic library in DT_NEEDED tags in the dynamic lib at
4181 all. */
4182 add_needed = (elf_dyn_lib_class (abfd)
4183 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4184 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4185
4186 s = bfd_get_section_by_name (abfd, ".dynamic");
4187 if (s != NULL)
4188 {
4189 bfd_byte *dynbuf;
4190 bfd_byte *extdyn;
cb33740c 4191 unsigned int elfsec;
4ad4eba5
AM
4192 unsigned long shlink;
4193
eea6121a 4194 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4195 {
dc1e8a47 4196 error_free_dyn:
f8703194
L
4197 free (dynbuf);
4198 goto error_return;
4199 }
4ad4eba5
AM
4200
4201 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4202 if (elfsec == SHN_BAD)
4ad4eba5
AM
4203 goto error_free_dyn;
4204 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4205
4206 for (extdyn = dynbuf;
9bff840e 4207 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4208 extdyn += bed->s->sizeof_dyn)
4209 {
4210 Elf_Internal_Dyn dyn;
4211
4212 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4213 if (dyn.d_tag == DT_SONAME)
4214 {
4215 unsigned int tagv = dyn.d_un.d_val;
4216 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4217 if (soname == NULL)
4218 goto error_free_dyn;
4219 }
4220 if (dyn.d_tag == DT_NEEDED)
4221 {
4222 struct bfd_link_needed_list *n, **pn;
4223 char *fnm, *anm;
4224 unsigned int tagv = dyn.d_un.d_val;
986f0783 4225 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4226
a50b1753 4227 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4228 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4229 if (n == NULL || fnm == NULL)
4230 goto error_free_dyn;
4231 amt = strlen (fnm) + 1;
a50b1753 4232 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4233 if (anm == NULL)
4234 goto error_free_dyn;
4235 memcpy (anm, fnm, amt);
4236 n->name = anm;
4237 n->by = abfd;
4238 n->next = NULL;
66eb6687 4239 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4240 ;
4241 *pn = n;
4242 }
4243 if (dyn.d_tag == DT_RUNPATH)
4244 {
4245 struct bfd_link_needed_list *n, **pn;
4246 char *fnm, *anm;
4247 unsigned int tagv = dyn.d_un.d_val;
986f0783 4248 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4249
a50b1753 4250 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4251 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4252 if (n == NULL || fnm == NULL)
4253 goto error_free_dyn;
4254 amt = strlen (fnm) + 1;
a50b1753 4255 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4256 if (anm == NULL)
4257 goto error_free_dyn;
4258 memcpy (anm, fnm, amt);
4259 n->name = anm;
4260 n->by = abfd;
4261 n->next = NULL;
4262 for (pn = & runpath;
4263 *pn != NULL;
4264 pn = &(*pn)->next)
4265 ;
4266 *pn = n;
4267 }
4268 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4269 if (!runpath && dyn.d_tag == DT_RPATH)
4270 {
4271 struct bfd_link_needed_list *n, **pn;
4272 char *fnm, *anm;
4273 unsigned int tagv = dyn.d_un.d_val;
986f0783 4274 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4275
a50b1753 4276 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4277 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4278 if (n == NULL || fnm == NULL)
4279 goto error_free_dyn;
4280 amt = strlen (fnm) + 1;
a50b1753 4281 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4282 if (anm == NULL)
f8703194 4283 goto error_free_dyn;
4ad4eba5
AM
4284 memcpy (anm, fnm, amt);
4285 n->name = anm;
4286 n->by = abfd;
4287 n->next = NULL;
4288 for (pn = & rpath;
4289 *pn != NULL;
4290 pn = &(*pn)->next)
4291 ;
4292 *pn = n;
4293 }
7ee314fa
AM
4294 if (dyn.d_tag == DT_AUDIT)
4295 {
4296 unsigned int tagv = dyn.d_un.d_val;
4297 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4298 }
4ad4eba5
AM
4299 }
4300
4301 free (dynbuf);
4302 }
4303
4304 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4305 frees all more recently bfd_alloc'd blocks as well. */
4306 if (runpath)
4307 rpath = runpath;
4308
4309 if (rpath)
4310 {
4311 struct bfd_link_needed_list **pn;
66eb6687 4312 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4313 ;
4314 *pn = rpath;
4315 }
4316
9acc85a6
AM
4317 /* If we have a PT_GNU_RELRO program header, mark as read-only
4318 all sections contained fully therein. This makes relro
4319 shared library sections appear as they will at run-time. */
4320 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4321 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4322 if (phdr->p_type == PT_GNU_RELRO)
4323 {
4324 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4325 {
4326 unsigned int opb = bfd_octets_per_byte (abfd, s);
4327
4328 if ((s->flags & SEC_ALLOC) != 0
4329 && s->vma * opb >= phdr->p_vaddr
4330 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4331 s->flags |= SEC_READONLY;
4332 }
9acc85a6
AM
4333 break;
4334 }
4335
4ad4eba5
AM
4336 /* We do not want to include any of the sections in a dynamic
4337 object in the output file. We hack by simply clobbering the
4338 list of sections in the BFD. This could be handled more
4339 cleanly by, say, a new section flag; the existing
4340 SEC_NEVER_LOAD flag is not the one we want, because that one
4341 still implies that the section takes up space in the output
4342 file. */
4343 bfd_section_list_clear (abfd);
4344
4ad4eba5
AM
4345 /* Find the name to use in a DT_NEEDED entry that refers to this
4346 object. If the object has a DT_SONAME entry, we use it.
4347 Otherwise, if the generic linker stuck something in
4348 elf_dt_name, we use that. Otherwise, we just use the file
4349 name. */
4350 if (soname == NULL || *soname == '\0')
4351 {
4352 soname = elf_dt_name (abfd);
4353 if (soname == NULL || *soname == '\0')
4354 soname = bfd_get_filename (abfd);
4355 }
4356
4357 /* Save the SONAME because sometimes the linker emulation code
4358 will need to know it. */
4359 elf_dt_name (abfd) = soname;
4360
4ad4eba5
AM
4361 /* If we have already included this dynamic object in the
4362 link, just ignore it. There is no reason to include a
4363 particular dynamic object more than once. */
e310298c
AM
4364 for (loaded_lib = htab->dyn_loaded;
4365 loaded_lib != NULL;
4366 loaded_lib = loaded_lib->next)
4367 {
4368 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4369 return TRUE;
4370 }
4371
4372 /* Create dynamic sections for backends that require that be done
4373 before setup_gnu_properties. */
4374 if (add_needed
4375 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4376 return FALSE;
7ee314fa
AM
4377
4378 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4379 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4380 }
4381
4382 /* If this is a dynamic object, we always link against the .dynsym
4383 symbol table, not the .symtab symbol table. The dynamic linker
4384 will only see the .dynsym symbol table, so there is no reason to
4385 look at .symtab for a dynamic object. */
4386
4387 if (! dynamic || elf_dynsymtab (abfd) == 0)
4388 hdr = &elf_tdata (abfd)->symtab_hdr;
4389 else
4390 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4391
4392 symcount = hdr->sh_size / bed->s->sizeof_sym;
4393
4394 /* The sh_info field of the symtab header tells us where the
4395 external symbols start. We don't care about the local symbols at
4396 this point. */
4397 if (elf_bad_symtab (abfd))
4398 {
4399 extsymcount = symcount;
4400 extsymoff = 0;
4401 }
4402 else
4403 {
4404 extsymcount = symcount - hdr->sh_info;
4405 extsymoff = hdr->sh_info;
4406 }
4407
f45794cb 4408 sym_hash = elf_sym_hashes (abfd);
012b2306 4409 if (extsymcount != 0)
4ad4eba5
AM
4410 {
4411 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4412 NULL, NULL, NULL);
4413 if (isymbuf == NULL)
4414 goto error_return;
4415
4ad4eba5 4416 if (sym_hash == NULL)
012b2306
AM
4417 {
4418 /* We store a pointer to the hash table entry for each
4419 external symbol. */
986f0783 4420 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4421 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4422 if (sym_hash == NULL)
4423 goto error_free_sym;
4424 elf_sym_hashes (abfd) = sym_hash;
4425 }
4ad4eba5
AM
4426 }
4427
4428 if (dynamic)
4429 {
4430 /* Read in any version definitions. */
fc0e6df6
PB
4431 if (!_bfd_elf_slurp_version_tables (abfd,
4432 info->default_imported_symver))
4ad4eba5
AM
4433 goto error_free_sym;
4434
4435 /* Read in the symbol versions, but don't bother to convert them
4436 to internal format. */
4437 if (elf_dynversym (abfd) != 0)
4438 {
986f0783
AM
4439 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4440 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4441
2bb3687b
AM
4442 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4443 goto error_free_sym;
4444 extversym = (Elf_External_Versym *)
4445 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4446 if (extversym == NULL)
4447 goto error_free_sym;
986f0783 4448 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4449 }
4450 }
4451
66eb6687
AM
4452 /* If we are loading an as-needed shared lib, save the symbol table
4453 state before we start adding symbols. If the lib turns out
4454 to be unneeded, restore the state. */
4455 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4456 {
4457 unsigned int i;
4458 size_t entsize;
4459
4460 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4461 {
4462 struct bfd_hash_entry *p;
2de92251 4463 struct elf_link_hash_entry *h;
66eb6687
AM
4464
4465 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4466 {
4467 h = (struct elf_link_hash_entry *) p;
4468 entsize += htab->root.table.entsize;
4469 if (h->root.type == bfd_link_hash_warning)
4470 entsize += htab->root.table.entsize;
4471 }
66eb6687
AM
4472 }
4473
4474 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4475 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4476 if (old_tab == NULL)
4477 goto error_free_vers;
4478
4479 /* Remember the current objalloc pointer, so that all mem for
4480 symbols added can later be reclaimed. */
4481 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4482 if (alloc_mark == NULL)
4483 goto error_free_vers;
4484
5061a885
AM
4485 /* Make a special call to the linker "notice" function to
4486 tell it that we are about to handle an as-needed lib. */
e5034e59 4487 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4488 goto error_free_vers;
5061a885 4489
f45794cb
AM
4490 /* Clone the symbol table. Remember some pointers into the
4491 symbol table, and dynamic symbol count. */
4492 old_ent = (char *) old_tab + tabsize;
66eb6687 4493 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4494 old_undefs = htab->root.undefs;
4495 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4496 old_table = htab->root.table.table;
4497 old_size = htab->root.table.size;
4498 old_count = htab->root.table.count;
e310298c
AM
4499 old_strtab = NULL;
4500 if (htab->dynstr != NULL)
4501 {
4502 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4503 if (old_strtab == NULL)
4504 goto error_free_vers;
4505 }
66eb6687
AM
4506
4507 for (i = 0; i < htab->root.table.size; i++)
4508 {
4509 struct bfd_hash_entry *p;
2de92251 4510 struct elf_link_hash_entry *h;
66eb6687
AM
4511
4512 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4513 {
4514 memcpy (old_ent, p, htab->root.table.entsize);
4515 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4516 h = (struct elf_link_hash_entry *) p;
4517 if (h->root.type == bfd_link_hash_warning)
4518 {
4519 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4520 old_ent = (char *) old_ent + htab->root.table.entsize;
4521 }
66eb6687
AM
4522 }
4523 }
4524 }
4ad4eba5 4525
66eb6687 4526 weaks = NULL;
be22c732
NC
4527 if (extversym == NULL)
4528 ever = NULL;
4529 else if (extversym + extsymoff < extversym_end)
4530 ever = extversym + extsymoff;
4531 else
4532 {
4533 /* xgettext:c-format */
4534 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4535 abfd, (long) extsymoff,
4536 (long) (extversym_end - extversym) / sizeof (* extversym));
4537 bfd_set_error (bfd_error_bad_value);
4538 goto error_free_vers;
4539 }
4540
b4c555cf
ML
4541 if (!bfd_link_relocatable (info)
4542 && abfd->lto_slim_object)
cc5277b1
ML
4543 {
4544 _bfd_error_handler
4545 (_("%pB: plugin needed to handle lto object"), abfd);
4546 }
4547
4ad4eba5
AM
4548 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4549 isym < isymend;
4550 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4551 {
4552 int bind;
4553 bfd_vma value;
af44c138 4554 asection *sec, *new_sec;
4ad4eba5
AM
4555 flagword flags;
4556 const char *name;
4557 struct elf_link_hash_entry *h;
90c984fc 4558 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4559 bfd_boolean definition;
4560 bfd_boolean size_change_ok;
4561 bfd_boolean type_change_ok;
37a9e49a
L
4562 bfd_boolean new_weak;
4563 bfd_boolean old_weak;
4ad4eba5 4564 bfd_boolean override;
a4d8e49b 4565 bfd_boolean common;
97196564 4566 bfd_boolean discarded;
4ad4eba5 4567 unsigned int old_alignment;
4538d1c7 4568 unsigned int shindex;
4ad4eba5 4569 bfd *old_bfd;
6e33951e 4570 bfd_boolean matched;
4ad4eba5
AM
4571
4572 override = FALSE;
4573
4574 flags = BSF_NO_FLAGS;
4575 sec = NULL;
4576 value = isym->st_value;
a4d8e49b 4577 common = bed->common_definition (isym);
2980ccad
L
4578 if (common && info->inhibit_common_definition)
4579 {
4580 /* Treat common symbol as undefined for --no-define-common. */
4581 isym->st_shndx = SHN_UNDEF;
4582 common = FALSE;
4583 }
97196564 4584 discarded = FALSE;
4ad4eba5
AM
4585
4586 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4587 switch (bind)
4ad4eba5 4588 {
3e7a7d11 4589 case STB_LOCAL:
4ad4eba5
AM
4590 /* This should be impossible, since ELF requires that all
4591 global symbols follow all local symbols, and that sh_info
4592 point to the first global symbol. Unfortunately, Irix 5
4593 screws this up. */
fe3fef62
AM
4594 if (elf_bad_symtab (abfd))
4595 continue;
4596
4597 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4598 then we'll likely segfault on a NULL symbol hash if the
4599 symbol is ever referenced in relocations. */
4600 shindex = elf_elfheader (abfd)->e_shstrndx;
4601 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4602 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4603 " (>= sh_info of %lu)"),
4604 abfd, name, (long) (isym - isymbuf + extsymoff),
4605 (long) extsymoff);
4606
4607 /* Dynamic object relocations are not processed by ld, so
4608 ld won't run into the problem mentioned above. */
4609 if (dynamic)
4610 continue;
fe3fef62
AM
4611 bfd_set_error (bfd_error_bad_value);
4612 goto error_free_vers;
3e7a7d11
NC
4613
4614 case STB_GLOBAL:
a4d8e49b 4615 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4616 flags = BSF_GLOBAL;
3e7a7d11
NC
4617 break;
4618
4619 case STB_WEAK:
4620 flags = BSF_WEAK;
4621 break;
4622
4623 case STB_GNU_UNIQUE:
4624 flags = BSF_GNU_UNIQUE;
4625 break;
4626
4627 default:
4ad4eba5 4628 /* Leave it up to the processor backend. */
3e7a7d11 4629 break;
4ad4eba5
AM
4630 }
4631
4632 if (isym->st_shndx == SHN_UNDEF)
4633 sec = bfd_und_section_ptr;
cb33740c
AM
4634 else if (isym->st_shndx == SHN_ABS)
4635 sec = bfd_abs_section_ptr;
4636 else if (isym->st_shndx == SHN_COMMON)
4637 {
4638 sec = bfd_com_section_ptr;
4639 /* What ELF calls the size we call the value. What ELF
4640 calls the value we call the alignment. */
4641 value = isym->st_size;
4642 }
4643 else
4ad4eba5
AM
4644 {
4645 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4646 if (sec == NULL)
4647 sec = bfd_abs_section_ptr;
dbaa2011 4648 else if (discarded_section (sec))
529fcb95 4649 {
e5d08002
L
4650 /* Symbols from discarded section are undefined. We keep
4651 its visibility. */
529fcb95 4652 sec = bfd_und_section_ptr;
97196564 4653 discarded = TRUE;
529fcb95
PB
4654 isym->st_shndx = SHN_UNDEF;
4655 }
4ad4eba5
AM
4656 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4657 value -= sec->vma;
4658 }
4ad4eba5
AM
4659
4660 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4661 isym->st_name);
4662 if (name == NULL)
4663 goto error_free_vers;
4664
4665 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4666 && (abfd->flags & BFD_PLUGIN) != 0)
4667 {
4668 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4669
4670 if (xc == NULL)
4671 {
4672 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4673 | SEC_EXCLUDE);
4674 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4675 if (xc == NULL)
4676 goto error_free_vers;
4677 }
4678 sec = xc;
4679 }
4680 else if (isym->st_shndx == SHN_COMMON
4681 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4682 && !bfd_link_relocatable (info))
4ad4eba5
AM
4683 {
4684 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4685
4686 if (tcomm == NULL)
4687 {
02d00247
AM
4688 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4689 | SEC_LINKER_CREATED);
4690 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4691 if (tcomm == NULL)
4ad4eba5
AM
4692 goto error_free_vers;
4693 }
4694 sec = tcomm;
4695 }
66eb6687 4696 else if (bed->elf_add_symbol_hook)
4ad4eba5 4697 {
66eb6687
AM
4698 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4699 &sec, &value))
4ad4eba5
AM
4700 goto error_free_vers;
4701
4702 /* The hook function sets the name to NULL if this symbol
4703 should be skipped for some reason. */
4704 if (name == NULL)
4705 continue;
4706 }
4707
4708 /* Sanity check that all possibilities were handled. */
4709 if (sec == NULL)
4538d1c7 4710 abort ();
4ad4eba5 4711
191c0c42
AM
4712 /* Silently discard TLS symbols from --just-syms. There's
4713 no way to combine a static TLS block with a new TLS block
4714 for this executable. */
4715 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4716 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4717 continue;
4718
4ad4eba5
AM
4719 if (bfd_is_und_section (sec)
4720 || bfd_is_com_section (sec))
4721 definition = FALSE;
4722 else
4723 definition = TRUE;
4724
4725 size_change_ok = FALSE;
66eb6687 4726 type_change_ok = bed->type_change_ok;
37a9e49a 4727 old_weak = FALSE;
6e33951e 4728 matched = FALSE;
4ad4eba5
AM
4729 old_alignment = 0;
4730 old_bfd = NULL;
af44c138 4731 new_sec = sec;
4ad4eba5 4732
66eb6687 4733 if (is_elf_hash_table (htab))
4ad4eba5
AM
4734 {
4735 Elf_Internal_Versym iver;
4736 unsigned int vernum = 0;
4737 bfd_boolean skip;
4738
fc0e6df6 4739 if (ever == NULL)
4ad4eba5 4740 {
fc0e6df6
PB
4741 if (info->default_imported_symver)
4742 /* Use the default symbol version created earlier. */
4743 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4744 else
4745 iver.vs_vers = 0;
4746 }
be22c732
NC
4747 else if (ever >= extversym_end)
4748 {
4749 /* xgettext:c-format */
4750 _bfd_error_handler (_("%pB: not enough version information"),
4751 abfd);
4752 bfd_set_error (bfd_error_bad_value);
4753 goto error_free_vers;
4754 }
fc0e6df6
PB
4755 else
4756 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4757
4758 vernum = iver.vs_vers & VERSYM_VERSION;
4759
4760 /* If this is a hidden symbol, or if it is not version
4761 1, we append the version name to the symbol name.
cc86ff91
EB
4762 However, we do not modify a non-hidden absolute symbol
4763 if it is not a function, because it might be the version
4764 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4765 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4766 || (vernum > 1
4767 && (!bfd_is_abs_section (sec)
4768 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4769 {
4770 const char *verstr;
4771 size_t namelen, verlen, newlen;
4772 char *newname, *p;
4773
4774 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4775 {
fc0e6df6
PB
4776 if (vernum > elf_tdata (abfd)->cverdefs)
4777 verstr = NULL;
4778 else if (vernum > 1)
4779 verstr =
4780 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4781 else
4782 verstr = "";
4ad4eba5 4783
fc0e6df6 4784 if (verstr == NULL)
4ad4eba5 4785 {
4eca0228 4786 _bfd_error_handler
695344c0 4787 /* xgettext:c-format */
871b3ab2 4788 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4789 abfd, name, vernum,
4790 elf_tdata (abfd)->cverdefs);
4791 bfd_set_error (bfd_error_bad_value);
4792 goto error_free_vers;
4ad4eba5 4793 }
fc0e6df6
PB
4794 }
4795 else
4796 {
4797 /* We cannot simply test for the number of
4798 entries in the VERNEED section since the
4799 numbers for the needed versions do not start
4800 at 0. */
4801 Elf_Internal_Verneed *t;
4802
4803 verstr = NULL;
4804 for (t = elf_tdata (abfd)->verref;
4805 t != NULL;
4806 t = t->vn_nextref)
4ad4eba5 4807 {
fc0e6df6 4808 Elf_Internal_Vernaux *a;
4ad4eba5 4809
fc0e6df6
PB
4810 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4811 {
4812 if (a->vna_other == vernum)
4ad4eba5 4813 {
fc0e6df6
PB
4814 verstr = a->vna_nodename;
4815 break;
4ad4eba5 4816 }
4ad4eba5 4817 }
fc0e6df6
PB
4818 if (a != NULL)
4819 break;
4820 }
4821 if (verstr == NULL)
4822 {
4eca0228 4823 _bfd_error_handler
695344c0 4824 /* xgettext:c-format */
871b3ab2 4825 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4826 abfd, name, vernum);
4827 bfd_set_error (bfd_error_bad_value);
4828 goto error_free_vers;
4ad4eba5 4829 }
4ad4eba5 4830 }
fc0e6df6
PB
4831
4832 namelen = strlen (name);
4833 verlen = strlen (verstr);
4834 newlen = namelen + verlen + 2;
4835 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4836 && isym->st_shndx != SHN_UNDEF)
4837 ++newlen;
4838
a50b1753 4839 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4840 if (newname == NULL)
4841 goto error_free_vers;
4842 memcpy (newname, name, namelen);
4843 p = newname + namelen;
4844 *p++ = ELF_VER_CHR;
4845 /* If this is a defined non-hidden version symbol,
4846 we add another @ to the name. This indicates the
4847 default version of the symbol. */
4848 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4849 && isym->st_shndx != SHN_UNDEF)
4850 *p++ = ELF_VER_CHR;
4851 memcpy (p, verstr, verlen + 1);
4852
4853 name = newname;
4ad4eba5
AM
4854 }
4855
cd3416da
AM
4856 /* If this symbol has default visibility and the user has
4857 requested we not re-export it, then mark it as hidden. */
a0d49154 4858 if (!bfd_is_und_section (sec)
cd3416da 4859 && !dynamic
ce875075 4860 && abfd->no_export
cd3416da
AM
4861 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4862 isym->st_other = (STV_HIDDEN
4863 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4864
4f3fedcf
AM
4865 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4866 sym_hash, &old_bfd, &old_weak,
4867 &old_alignment, &skip, &override,
6e33951e
L
4868 &type_change_ok, &size_change_ok,
4869 &matched))
4ad4eba5
AM
4870 goto error_free_vers;
4871
4872 if (skip)
4873 continue;
4874
6e33951e
L
4875 /* Override a definition only if the new symbol matches the
4876 existing one. */
4877 if (override && matched)
4ad4eba5
AM
4878 definition = FALSE;
4879
4880 h = *sym_hash;
4881 while (h->root.type == bfd_link_hash_indirect
4882 || h->root.type == bfd_link_hash_warning)
4883 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4884
4ad4eba5 4885 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4886 && vernum > 1
4887 && definition)
4888 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4889 }
4890
4891 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4892 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4893 (struct bfd_link_hash_entry **) sym_hash)))
4894 goto error_free_vers;
4895
4896 h = *sym_hash;
90c984fc
L
4897 /* We need to make sure that indirect symbol dynamic flags are
4898 updated. */
4899 hi = h;
4ad4eba5
AM
4900 while (h->root.type == bfd_link_hash_indirect
4901 || h->root.type == bfd_link_hash_warning)
4902 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4903
97196564
L
4904 /* Setting the index to -3 tells elf_link_output_extsym that
4905 this symbol is defined in a discarded section. */
4906 if (discarded)
4907 h->indx = -3;
4908
4ad4eba5
AM
4909 *sym_hash = h;
4910
37a9e49a 4911 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4912 if (dynamic
4913 && definition
37a9e49a 4914 && new_weak
fcb93ecf 4915 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4916 && is_elf_hash_table (htab)
60d67dc8 4917 && h->u.alias == NULL)
4ad4eba5
AM
4918 {
4919 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4920 a dynamic object, using the alias field. Later in this
4921 function we will set the alias field to the correct
4ad4eba5
AM
4922 value. We only put non-function symbols from dynamic
4923 objects on this list, because that happens to be the only
4924 time we need to know the normal symbol corresponding to a
4925 weak symbol, and the information is time consuming to
60d67dc8 4926 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4927 then this symbol was already defined by some previous
4928 dynamic object, and we will be using that previous
4929 definition anyhow. */
4930
60d67dc8 4931 h->u.alias = weaks;
4ad4eba5 4932 weaks = h;
4ad4eba5
AM
4933 }
4934
4935 /* Set the alignment of a common symbol. */
a4d8e49b 4936 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4937 && h->root.type == bfd_link_hash_common)
4938 {
4939 unsigned int align;
4940
a4d8e49b 4941 if (common)
af44c138
L
4942 align = bfd_log2 (isym->st_value);
4943 else
4944 {
4945 /* The new symbol is a common symbol in a shared object.
4946 We need to get the alignment from the section. */
4947 align = new_sec->alignment_power;
4948 }
595213d4 4949 if (align > old_alignment)
4ad4eba5
AM
4950 h->root.u.c.p->alignment_power = align;
4951 else
4952 h->root.u.c.p->alignment_power = old_alignment;
4953 }
4954
66eb6687 4955 if (is_elf_hash_table (htab))
4ad4eba5 4956 {
4f3fedcf
AM
4957 /* Set a flag in the hash table entry indicating the type of
4958 reference or definition we just found. A dynamic symbol
4959 is one which is referenced or defined by both a regular
4960 object and a shared object. */
4961 bfd_boolean dynsym = FALSE;
4962
4963 /* Plugin symbols aren't normal. Don't set def_regular or
4964 ref_regular for them, or make them dynamic. */
4965 if ((abfd->flags & BFD_PLUGIN) != 0)
4966 ;
4967 else if (! dynamic)
4968 {
4969 if (! definition)
4970 {
4971 h->ref_regular = 1;
4972 if (bind != STB_WEAK)
4973 h->ref_regular_nonweak = 1;
4974 }
4975 else
4976 {
4977 h->def_regular = 1;
4978 if (h->def_dynamic)
4979 {
4980 h->def_dynamic = 0;
4981 h->ref_dynamic = 1;
4982 }
4983 }
4984
4985 /* If the indirect symbol has been forced local, don't
4986 make the real symbol dynamic. */
4987 if ((h == hi || !hi->forced_local)
0e1862bb 4988 && (bfd_link_dll (info)
4f3fedcf
AM
4989 || h->def_dynamic
4990 || h->ref_dynamic))
4991 dynsym = TRUE;
4992 }
4993 else
4994 {
4995 if (! definition)
4996 {
4997 h->ref_dynamic = 1;
4998 hi->ref_dynamic = 1;
4999 }
5000 else
5001 {
5002 h->def_dynamic = 1;
5003 hi->def_dynamic = 1;
5004 }
5005
5006 /* If the indirect symbol has been forced local, don't
5007 make the real symbol dynamic. */
5008 if ((h == hi || !hi->forced_local)
5009 && (h->def_regular
5010 || h->ref_regular
60d67dc8
AM
5011 || (h->is_weakalias
5012 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
5013 dynsym = TRUE;
5014 }
5015
5016 /* Check to see if we need to add an indirect symbol for
5017 the default name. */
5018 if (definition
5019 || (!override && h->root.type == bfd_link_hash_common))
5020 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5021 sec, value, &old_bfd, &dynsym))
5022 goto error_free_vers;
4ad4eba5
AM
5023
5024 /* Check the alignment when a common symbol is involved. This
5025 can change when a common symbol is overridden by a normal
5026 definition or a common symbol is ignored due to the old
5027 normal definition. We need to make sure the maximum
5028 alignment is maintained. */
a4d8e49b 5029 if ((old_alignment || common)
4ad4eba5
AM
5030 && h->root.type != bfd_link_hash_common)
5031 {
5032 unsigned int common_align;
5033 unsigned int normal_align;
5034 unsigned int symbol_align;
5035 bfd *normal_bfd;
5036 bfd *common_bfd;
5037
3a81e825
AM
5038 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5039 || h->root.type == bfd_link_hash_defweak);
5040
4ad4eba5
AM
5041 symbol_align = ffs (h->root.u.def.value) - 1;
5042 if (h->root.u.def.section->owner != NULL
0616a280
AM
5043 && (h->root.u.def.section->owner->flags
5044 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5045 {
5046 normal_align = h->root.u.def.section->alignment_power;
5047 if (normal_align > symbol_align)
5048 normal_align = symbol_align;
5049 }
5050 else
5051 normal_align = symbol_align;
5052
5053 if (old_alignment)
5054 {
5055 common_align = old_alignment;
5056 common_bfd = old_bfd;
5057 normal_bfd = abfd;
5058 }
5059 else
5060 {
5061 common_align = bfd_log2 (isym->st_value);
5062 common_bfd = abfd;
5063 normal_bfd = old_bfd;
5064 }
5065
5066 if (normal_align < common_align)
d07676f8
NC
5067 {
5068 /* PR binutils/2735 */
5069 if (normal_bfd == NULL)
4eca0228 5070 _bfd_error_handler
695344c0 5071 /* xgettext:c-format */
9793eb77 5072 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5073 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5074 1 << common_align, name, common_bfd,
5075 1 << normal_align, h->root.u.def.section);
d07676f8 5076 else
4eca0228 5077 _bfd_error_handler
695344c0 5078 /* xgettext:c-format */
9793eb77 5079 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5080 " is smaller than %u in %pB"),
c08bb8dd
AM
5081 1 << normal_align, name, normal_bfd,
5082 1 << common_align, common_bfd);
d07676f8 5083 }
4ad4eba5
AM
5084 }
5085
83ad0046 5086 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5087 if (isym->st_size != 0
5088 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5089 && (definition || h->size == 0))
5090 {
83ad0046
L
5091 if (h->size != 0
5092 && h->size != isym->st_size
5093 && ! size_change_ok)
4eca0228 5094 _bfd_error_handler
695344c0 5095 /* xgettext:c-format */
9793eb77 5096 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5097 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5098 name, (uint64_t) h->size, old_bfd,
5099 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5100
5101 h->size = isym->st_size;
5102 }
5103
5104 /* If this is a common symbol, then we always want H->SIZE
5105 to be the size of the common symbol. The code just above
5106 won't fix the size if a common symbol becomes larger. We
5107 don't warn about a size change here, because that is
4f3fedcf 5108 covered by --warn-common. Allow changes between different
fcb93ecf 5109 function types. */
4ad4eba5
AM
5110 if (h->root.type == bfd_link_hash_common)
5111 h->size = h->root.u.c.size;
5112
5113 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5114 && ((definition && !new_weak)
5115 || (old_weak && h->root.type == bfd_link_hash_common)
5116 || h->type == STT_NOTYPE))
4ad4eba5 5117 {
2955ec4c
L
5118 unsigned int type = ELF_ST_TYPE (isym->st_info);
5119
5120 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5121 symbol. */
5122 if (type == STT_GNU_IFUNC
5123 && (abfd->flags & DYNAMIC) != 0)
5124 type = STT_FUNC;
4ad4eba5 5125
2955ec4c
L
5126 if (h->type != type)
5127 {
5128 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5129 /* xgettext:c-format */
4eca0228 5130 _bfd_error_handler
9793eb77 5131 (_("warning: type of symbol `%s' changed"
871b3ab2 5132 " from %d to %d in %pB"),
c08bb8dd 5133 name, h->type, type, abfd);
2955ec4c
L
5134
5135 h->type = type;
5136 }
4ad4eba5
AM
5137 }
5138
54ac0771 5139 /* Merge st_other field. */
b8417128 5140 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5141
c3df8c14 5142 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5143 if (definition
5144 && (sec->flags & SEC_DEBUGGING)
5145 && !bfd_link_relocatable (info))
c3df8c14
AM
5146 dynsym = FALSE;
5147
4f3fedcf
AM
5148 /* Nor should we make plugin symbols dynamic. */
5149 if ((abfd->flags & BFD_PLUGIN) != 0)
5150 dynsym = FALSE;
5151
35fc36a8 5152 if (definition)
35399224
L
5153 {
5154 h->target_internal = isym->st_target_internal;
5155 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5156 }
35fc36a8 5157
4ad4eba5
AM
5158 if (definition && !dynamic)
5159 {
5160 char *p = strchr (name, ELF_VER_CHR);
5161 if (p != NULL && p[1] != ELF_VER_CHR)
5162 {
5163 /* Queue non-default versions so that .symver x, x@FOO
5164 aliases can be checked. */
66eb6687 5165 if (!nondeflt_vers)
4ad4eba5 5166 {
986f0783
AM
5167 size_t amt = ((isymend - isym + 1)
5168 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5169 nondeflt_vers
5170 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5171 if (!nondeflt_vers)
5172 goto error_free_vers;
4ad4eba5 5173 }
66eb6687 5174 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5175 }
5176 }
5177
5178 if (dynsym && h->dynindx == -1)
5179 {
c152c796 5180 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5181 goto error_free_vers;
60d67dc8
AM
5182 if (h->is_weakalias
5183 && weakdef (h)->dynindx == -1)
4ad4eba5 5184 {
60d67dc8 5185 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5186 goto error_free_vers;
5187 }
5188 }
1f599d0e 5189 else if (h->dynindx != -1)
4ad4eba5
AM
5190 /* If the symbol already has a dynamic index, but
5191 visibility says it should not be visible, turn it into
5192 a local symbol. */
5193 switch (ELF_ST_VISIBILITY (h->other))
5194 {
5195 case STV_INTERNAL:
5196 case STV_HIDDEN:
5197 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5198 dynsym = FALSE;
5199 break;
5200 }
5201
aef28989
L
5202 /* Don't add DT_NEEDED for references from the dummy bfd nor
5203 for unmatched symbol. */
4ad4eba5 5204 if (!add_needed
aef28989 5205 && matched
4ad4eba5 5206 && definition
010e5ae2 5207 && ((dynsym
ffa9430d 5208 && h->ref_regular_nonweak
4f3fedcf
AM
5209 && (old_bfd == NULL
5210 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5211 || (h->ref_dynamic_nonweak
010e5ae2 5212 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5213 && !on_needed_list (elf_dt_name (abfd),
5214 htab->needed, NULL))))
4ad4eba5 5215 {
4ad4eba5
AM
5216 const char *soname = elf_dt_name (abfd);
5217
16e4ecc0
AM
5218 info->callbacks->minfo ("%!", soname, old_bfd,
5219 h->root.root.string);
5220
4ad4eba5
AM
5221 /* A symbol from a library loaded via DT_NEEDED of some
5222 other library is referenced by a regular object.
e56f61be 5223 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5224 --no-add-needed is used and the reference was not
5225 a weak one. */
4f3fedcf 5226 if (old_bfd != NULL
b918acf9 5227 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5228 {
4eca0228 5229 _bfd_error_handler
695344c0 5230 /* xgettext:c-format */
871b3ab2 5231 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5232 old_bfd, name);
ff5ac77b 5233 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5234 goto error_free_vers;
5235 }
5236
a50b1753 5237 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5238 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5239
e310298c
AM
5240 /* Create dynamic sections for backends that require
5241 that be done before setup_gnu_properties. */
5242 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5243 return FALSE;
4ad4eba5 5244 add_needed = TRUE;
4ad4eba5
AM
5245 }
5246 }
5247 }
5248
a83ef4d1
L
5249 if (info->lto_plugin_active
5250 && !bfd_link_relocatable (info)
5251 && (abfd->flags & BFD_PLUGIN) == 0
5252 && !just_syms
5253 && extsymcount)
5254 {
5255 int r_sym_shift;
5256
5257 if (bed->s->arch_size == 32)
5258 r_sym_shift = 8;
5259 else
5260 r_sym_shift = 32;
5261
5262 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5263 referenced in regular objects so that linker plugin will get
5264 the correct symbol resolution. */
5265
5266 sym_hash = elf_sym_hashes (abfd);
5267 for (s = abfd->sections; s != NULL; s = s->next)
5268 {
5269 Elf_Internal_Rela *internal_relocs;
5270 Elf_Internal_Rela *rel, *relend;
5271
5272 /* Don't check relocations in excluded sections. */
5273 if ((s->flags & SEC_RELOC) == 0
5274 || s->reloc_count == 0
5275 || (s->flags & SEC_EXCLUDE) != 0
5276 || ((info->strip == strip_all
5277 || info->strip == strip_debugger)
5278 && (s->flags & SEC_DEBUGGING) != 0))
5279 continue;
5280
5281 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5282 NULL,
5283 info->keep_memory);
5284 if (internal_relocs == NULL)
5285 goto error_free_vers;
5286
5287 rel = internal_relocs;
5288 relend = rel + s->reloc_count;
5289 for ( ; rel < relend; rel++)
5290 {
5291 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5292 struct elf_link_hash_entry *h;
5293
5294 /* Skip local symbols. */
5295 if (r_symndx < extsymoff)
5296 continue;
5297
5298 h = sym_hash[r_symndx - extsymoff];
5299 if (h != NULL)
5300 h->root.non_ir_ref_regular = 1;
5301 }
5302
5303 if (elf_section_data (s)->relocs != internal_relocs)
5304 free (internal_relocs);
5305 }
5306 }
5307
c9594989
AM
5308 free (extversym);
5309 extversym = NULL;
5310 free (isymbuf);
5311 isymbuf = NULL;
66eb6687
AM
5312
5313 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5314 {
5315 unsigned int i;
5316
5317 /* Restore the symbol table. */
f45794cb
AM
5318 old_ent = (char *) old_tab + tabsize;
5319 memset (elf_sym_hashes (abfd), 0,
5320 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5321 htab->root.table.table = old_table;
5322 htab->root.table.size = old_size;
5323 htab->root.table.count = old_count;
66eb6687 5324 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5325 htab->root.undefs = old_undefs;
5326 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5327 if (htab->dynstr != NULL)
5328 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5329 free (old_strtab);
5330 old_strtab = NULL;
66eb6687
AM
5331 for (i = 0; i < htab->root.table.size; i++)
5332 {
5333 struct bfd_hash_entry *p;
5334 struct elf_link_hash_entry *h;
3e0882af
L
5335 bfd_size_type size;
5336 unsigned int alignment_power;
4070765b 5337 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5338
5339 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5340 {
5341 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5342 if (h->root.type == bfd_link_hash_warning)
5343 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5344
3e0882af
L
5345 /* Preserve the maximum alignment and size for common
5346 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5347 since it can still be loaded at run time by another
3e0882af
L
5348 dynamic lib. */
5349 if (h->root.type == bfd_link_hash_common)
5350 {
5351 size = h->root.u.c.size;
5352 alignment_power = h->root.u.c.p->alignment_power;
5353 }
5354 else
5355 {
5356 size = 0;
5357 alignment_power = 0;
5358 }
4070765b 5359 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5360 will be exported when the dynamic lib becomes needed
5361 in the second pass. */
4070765b 5362 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5363 memcpy (p, old_ent, htab->root.table.entsize);
5364 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5365 h = (struct elf_link_hash_entry *) p;
5366 if (h->root.type == bfd_link_hash_warning)
5367 {
5368 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5369 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5370 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5371 }
a4542f1b 5372 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5373 {
5374 if (size > h->root.u.c.size)
5375 h->root.u.c.size = size;
5376 if (alignment_power > h->root.u.c.p->alignment_power)
5377 h->root.u.c.p->alignment_power = alignment_power;
5378 }
4070765b 5379 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5380 }
5381 }
5382
5061a885
AM
5383 /* Make a special call to the linker "notice" function to
5384 tell it that symbols added for crefs may need to be removed. */
e5034e59 5385 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5386 goto error_free_vers;
5061a885 5387
66eb6687
AM
5388 free (old_tab);
5389 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5390 alloc_mark);
c9594989 5391 free (nondeflt_vers);
66eb6687
AM
5392 return TRUE;
5393 }
2de92251 5394
66eb6687
AM
5395 if (old_tab != NULL)
5396 {
e5034e59 5397 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5398 goto error_free_vers;
66eb6687
AM
5399 free (old_tab);
5400 old_tab = NULL;
5401 }
5402
c6e8a9a8
L
5403 /* Now that all the symbols from this input file are created, if
5404 not performing a relocatable link, handle .symver foo, foo@BAR
5405 such that any relocs against foo become foo@BAR. */
0e1862bb 5406 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5407 {
ef53be89 5408 size_t cnt, symidx;
4ad4eba5
AM
5409
5410 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5411 {
5412 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5413 char *shortname, *p;
986f0783 5414 size_t amt;
4ad4eba5
AM
5415
5416 p = strchr (h->root.root.string, ELF_VER_CHR);
5417 if (p == NULL
5418 || (h->root.type != bfd_link_hash_defined
5419 && h->root.type != bfd_link_hash_defweak))
5420 continue;
5421
5422 amt = p - h->root.root.string;
a50b1753 5423 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5424 if (!shortname)
5425 goto error_free_vers;
4ad4eba5
AM
5426 memcpy (shortname, h->root.root.string, amt);
5427 shortname[amt] = '\0';
5428
5429 hi = (struct elf_link_hash_entry *)
66eb6687 5430 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5431 FALSE, FALSE, FALSE);
5432 if (hi != NULL
5433 && hi->root.type == h->root.type
5434 && hi->root.u.def.value == h->root.u.def.value
5435 && hi->root.u.def.section == h->root.u.def.section)
5436 {
5437 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5438 hi->root.type = bfd_link_hash_indirect;
5439 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5440 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5441 sym_hash = elf_sym_hashes (abfd);
5442 if (sym_hash)
5443 for (symidx = 0; symidx < extsymcount; ++symidx)
5444 if (sym_hash[symidx] == hi)
5445 {
5446 sym_hash[symidx] = h;
5447 break;
5448 }
5449 }
5450 free (shortname);
5451 }
5452 free (nondeflt_vers);
5453 nondeflt_vers = NULL;
5454 }
5455
60d67dc8 5456 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5457 symbols we found. The only way to do this is to search all the
5458 symbols. Since we only need the information for non functions in
5459 dynamic objects, that's the only time we actually put anything on
5460 the list WEAKS. We need this information so that if a regular
5461 object refers to a symbol defined weakly in a dynamic object, the
5462 real symbol in the dynamic object is also put in the dynamic
5463 symbols; we also must arrange for both symbols to point to the
5464 same memory location. We could handle the general case of symbol
5465 aliasing, but a general symbol alias can only be generated in
5466 assembler code, handling it correctly would be very time
5467 consuming, and other ELF linkers don't handle general aliasing
5468 either. */
5469 if (weaks != NULL)
5470 {
5471 struct elf_link_hash_entry **hpp;
5472 struct elf_link_hash_entry **hppend;
5473 struct elf_link_hash_entry **sorted_sym_hash;
5474 struct elf_link_hash_entry *h;
986f0783 5475 size_t sym_count, amt;
4ad4eba5
AM
5476
5477 /* Since we have to search the whole symbol list for each weak
5478 defined symbol, search time for N weak defined symbols will be
5479 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5480 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5481 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5482 if (sorted_sym_hash == NULL)
5483 goto error_return;
5484 sym_hash = sorted_sym_hash;
5485 hpp = elf_sym_hashes (abfd);
5486 hppend = hpp + extsymcount;
5487 sym_count = 0;
5488 for (; hpp < hppend; hpp++)
5489 {
5490 h = *hpp;
5491 if (h != NULL
5492 && h->root.type == bfd_link_hash_defined
fcb93ecf 5493 && !bed->is_function_type (h->type))
4ad4eba5
AM
5494 {
5495 *sym_hash = h;
5496 sym_hash++;
5497 sym_count++;
5498 }
5499 }
5500
3a3f4bf7 5501 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5502 elf_sort_symbol);
5503
5504 while (weaks != NULL)
5505 {
5506 struct elf_link_hash_entry *hlook;
5507 asection *slook;
5508 bfd_vma vlook;
ed54588d 5509 size_t i, j, idx = 0;
4ad4eba5
AM
5510
5511 hlook = weaks;
60d67dc8
AM
5512 weaks = hlook->u.alias;
5513 hlook->u.alias = NULL;
4ad4eba5 5514
e3e53eed
AM
5515 if (hlook->root.type != bfd_link_hash_defined
5516 && hlook->root.type != bfd_link_hash_defweak)
5517 continue;
5518
4ad4eba5
AM
5519 slook = hlook->root.u.def.section;
5520 vlook = hlook->root.u.def.value;
5521
4ad4eba5
AM
5522 i = 0;
5523 j = sym_count;
14160578 5524 while (i != j)
4ad4eba5
AM
5525 {
5526 bfd_signed_vma vdiff;
5527 idx = (i + j) / 2;
14160578 5528 h = sorted_sym_hash[idx];
4ad4eba5
AM
5529 vdiff = vlook - h->root.u.def.value;
5530 if (vdiff < 0)
5531 j = idx;
5532 else if (vdiff > 0)
5533 i = idx + 1;
5534 else
5535 {
d3435ae8 5536 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5537 if (sdiff < 0)
5538 j = idx;
5539 else if (sdiff > 0)
5540 i = idx + 1;
5541 else
14160578 5542 break;
4ad4eba5
AM
5543 }
5544 }
5545
5546 /* We didn't find a value/section match. */
14160578 5547 if (i == j)
4ad4eba5
AM
5548 continue;
5549
14160578
AM
5550 /* With multiple aliases, or when the weak symbol is already
5551 strongly defined, we have multiple matching symbols and
5552 the binary search above may land on any of them. Step
5553 one past the matching symbol(s). */
5554 while (++idx != j)
5555 {
5556 h = sorted_sym_hash[idx];
5557 if (h->root.u.def.section != slook
5558 || h->root.u.def.value != vlook)
5559 break;
5560 }
5561
5562 /* Now look back over the aliases. Since we sorted by size
5563 as well as value and section, we'll choose the one with
5564 the largest size. */
5565 while (idx-- != i)
4ad4eba5 5566 {
14160578 5567 h = sorted_sym_hash[idx];
4ad4eba5
AM
5568
5569 /* Stop if value or section doesn't match. */
14160578
AM
5570 if (h->root.u.def.section != slook
5571 || h->root.u.def.value != vlook)
4ad4eba5
AM
5572 break;
5573 else if (h != hlook)
5574 {
60d67dc8
AM
5575 struct elf_link_hash_entry *t;
5576
5577 hlook->u.alias = h;
5578 hlook->is_weakalias = 1;
5579 t = h;
5580 if (t->u.alias != NULL)
5581 while (t->u.alias != h)
5582 t = t->u.alias;
5583 t->u.alias = hlook;
4ad4eba5
AM
5584
5585 /* If the weak definition is in the list of dynamic
5586 symbols, make sure the real definition is put
5587 there as well. */
5588 if (hlook->dynindx != -1 && h->dynindx == -1)
5589 {
c152c796 5590 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5591 {
5592 err_free_sym_hash:
5593 free (sorted_sym_hash);
5594 goto error_return;
5595 }
4ad4eba5
AM
5596 }
5597
5598 /* If the real definition is in the list of dynamic
5599 symbols, make sure the weak definition is put
5600 there as well. If we don't do this, then the
5601 dynamic loader might not merge the entries for the
5602 real definition and the weak definition. */
5603 if (h->dynindx != -1 && hlook->dynindx == -1)
5604 {
c152c796 5605 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5606 goto err_free_sym_hash;
4ad4eba5
AM
5607 }
5608 break;
5609 }
5610 }
5611 }
5612
5613 free (sorted_sym_hash);
5614 }
5615
33177bb1
AM
5616 if (bed->check_directives
5617 && !(*bed->check_directives) (abfd, info))
5618 return FALSE;
85fbca6a 5619
4ad4eba5
AM
5620 /* If this is a non-traditional link, try to optimize the handling
5621 of the .stab/.stabstr sections. */
5622 if (! dynamic
5623 && ! info->traditional_format
66eb6687 5624 && is_elf_hash_table (htab)
4ad4eba5
AM
5625 && (info->strip != strip_all && info->strip != strip_debugger))
5626 {
5627 asection *stabstr;
5628
5629 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5630 if (stabstr != NULL)
5631 {
5632 bfd_size_type string_offset = 0;
5633 asection *stab;
5634
5635 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5636 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5637 && (!stab->name[5] ||
5638 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5639 && (stab->flags & SEC_MERGE) == 0
5640 && !bfd_is_abs_section (stab->output_section))
5641 {
5642 struct bfd_elf_section_data *secdata;
5643
5644 secdata = elf_section_data (stab);
66eb6687
AM
5645 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5646 stabstr, &secdata->sec_info,
4ad4eba5
AM
5647 &string_offset))
5648 goto error_return;
5649 if (secdata->sec_info)
dbaa2011 5650 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5651 }
5652 }
5653 }
5654
e310298c 5655 if (dynamic && add_needed)
4ad4eba5
AM
5656 {
5657 /* Add this bfd to the loaded list. */
5658 struct elf_link_loaded_list *n;
5659
ca4be51c 5660 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5661 if (n == NULL)
5662 goto error_return;
5663 n->abfd = abfd;
e310298c
AM
5664 n->next = htab->dyn_loaded;
5665 htab->dyn_loaded = n;
4ad4eba5 5666 }
e310298c
AM
5667 if (dynamic && !add_needed
5668 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5669 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5670
5671 return TRUE;
5672
5673 error_free_vers:
c9594989
AM
5674 free (old_tab);
5675 free (old_strtab);
5676 free (nondeflt_vers);
5677 free (extversym);
4ad4eba5 5678 error_free_sym:
c9594989 5679 free (isymbuf);
4ad4eba5
AM
5680 error_return:
5681 return FALSE;
5682}
5683
8387904d
AM
5684/* Return the linker hash table entry of a symbol that might be
5685 satisfied by an archive symbol. Return -1 on error. */
5686
5687struct elf_link_hash_entry *
5688_bfd_elf_archive_symbol_lookup (bfd *abfd,
5689 struct bfd_link_info *info,
5690 const char *name)
5691{
5692 struct elf_link_hash_entry *h;
5693 char *p, *copy;
5694 size_t len, first;
5695
2a41f396 5696 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5697 if (h != NULL)
5698 return h;
5699
5700 /* If this is a default version (the name contains @@), look up the
5701 symbol again with only one `@' as well as without the version.
5702 The effect is that references to the symbol with and without the
5703 version will be matched by the default symbol in the archive. */
5704
5705 p = strchr (name, ELF_VER_CHR);
5706 if (p == NULL || p[1] != ELF_VER_CHR)
5707 return h;
5708
5709 /* First check with only one `@'. */
5710 len = strlen (name);
a50b1753 5711 copy = (char *) bfd_alloc (abfd, len);
8387904d 5712 if (copy == NULL)
e99955cd 5713 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5714
5715 first = p - name + 1;
5716 memcpy (copy, name, first);
5717 memcpy (copy + first, name + first + 1, len - first);
5718
2a41f396 5719 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5720 if (h == NULL)
5721 {
5722 /* We also need to check references to the symbol without the
5723 version. */
5724 copy[first - 1] = '\0';
5725 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5726 FALSE, FALSE, TRUE);
8387904d
AM
5727 }
5728
5729 bfd_release (abfd, copy);
5730 return h;
5731}
5732
0ad989f9 5733/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5734 don't use _bfd_generic_link_add_archive_symbols because we need to
5735 handle versioned symbols.
0ad989f9
L
5736
5737 Fortunately, ELF archive handling is simpler than that done by
5738 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5739 oddities. In ELF, if we find a symbol in the archive map, and the
5740 symbol is currently undefined, we know that we must pull in that
5741 object file.
5742
5743 Unfortunately, we do have to make multiple passes over the symbol
5744 table until nothing further is resolved. */
5745
4ad4eba5
AM
5746static bfd_boolean
5747elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5748{
5749 symindex c;
13e570f8 5750 unsigned char *included = NULL;
0ad989f9
L
5751 carsym *symdefs;
5752 bfd_boolean loop;
986f0783 5753 size_t amt;
8387904d
AM
5754 const struct elf_backend_data *bed;
5755 struct elf_link_hash_entry * (*archive_symbol_lookup)
5756 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5757
5758 if (! bfd_has_map (abfd))
5759 {
5760 /* An empty archive is a special case. */
5761 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5762 return TRUE;
5763 bfd_set_error (bfd_error_no_armap);
5764 return FALSE;
5765 }
5766
5767 /* Keep track of all symbols we know to be already defined, and all
5768 files we know to be already included. This is to speed up the
5769 second and subsequent passes. */
5770 c = bfd_ardata (abfd)->symdef_count;
5771 if (c == 0)
5772 return TRUE;
986f0783 5773 amt = c * sizeof (*included);
13e570f8
AM
5774 included = (unsigned char *) bfd_zmalloc (amt);
5775 if (included == NULL)
5776 return FALSE;
0ad989f9
L
5777
5778 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5779 bed = get_elf_backend_data (abfd);
5780 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5781
5782 do
5783 {
5784 file_ptr last;
5785 symindex i;
5786 carsym *symdef;
5787 carsym *symdefend;
5788
5789 loop = FALSE;
5790 last = -1;
5791
5792 symdef = symdefs;
5793 symdefend = symdef + c;
5794 for (i = 0; symdef < symdefend; symdef++, i++)
5795 {
5796 struct elf_link_hash_entry *h;
5797 bfd *element;
5798 struct bfd_link_hash_entry *undefs_tail;
5799 symindex mark;
5800
13e570f8 5801 if (included[i])
0ad989f9
L
5802 continue;
5803 if (symdef->file_offset == last)
5804 {
5805 included[i] = TRUE;
5806 continue;
5807 }
5808
8387904d 5809 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5810 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5811 goto error_return;
0ad989f9
L
5812
5813 if (h == NULL)
5814 continue;
5815
5816 if (h->root.type == bfd_link_hash_common)
5817 {
5818 /* We currently have a common symbol. The archive map contains
5819 a reference to this symbol, so we may want to include it. We
5820 only want to include it however, if this archive element
5821 contains a definition of the symbol, not just another common
5822 declaration of it.
5823
5824 Unfortunately some archivers (including GNU ar) will put
5825 declarations of common symbols into their archive maps, as
5826 well as real definitions, so we cannot just go by the archive
5827 map alone. Instead we must read in the element's symbol
5828 table and check that to see what kind of symbol definition
5829 this is. */
5830 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5831 continue;
5832 }
5833 else if (h->root.type != bfd_link_hash_undefined)
5834 {
5835 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5836 /* Symbol must be defined. Don't check it again. */
5837 included[i] = TRUE;
0ad989f9
L
5838 continue;
5839 }
5840
5841 /* We need to include this archive member. */
5842 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5843 if (element == NULL)
5844 goto error_return;
5845
5846 if (! bfd_check_format (element, bfd_object))
5847 goto error_return;
5848
0ad989f9
L
5849 undefs_tail = info->hash->undefs_tail;
5850
0e144ba7
AM
5851 if (!(*info->callbacks
5852 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5853 continue;
0e144ba7 5854 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5855 goto error_return;
5856
5857 /* If there are any new undefined symbols, we need to make
5858 another pass through the archive in order to see whether
5859 they can be defined. FIXME: This isn't perfect, because
5860 common symbols wind up on undefs_tail and because an
5861 undefined symbol which is defined later on in this pass
5862 does not require another pass. This isn't a bug, but it
5863 does make the code less efficient than it could be. */
5864 if (undefs_tail != info->hash->undefs_tail)
5865 loop = TRUE;
5866
5867 /* Look backward to mark all symbols from this object file
5868 which we have already seen in this pass. */
5869 mark = i;
5870 do
5871 {
5872 included[mark] = TRUE;
5873 if (mark == 0)
5874 break;
5875 --mark;
5876 }
5877 while (symdefs[mark].file_offset == symdef->file_offset);
5878
5879 /* We mark subsequent symbols from this object file as we go
5880 on through the loop. */
5881 last = symdef->file_offset;
5882 }
5883 }
5884 while (loop);
5885
0ad989f9 5886 free (included);
0ad989f9
L
5887 return TRUE;
5888
5889 error_return:
c9594989 5890 free (included);
0ad989f9
L
5891 return FALSE;
5892}
4ad4eba5
AM
5893
5894/* Given an ELF BFD, add symbols to the global hash table as
5895 appropriate. */
5896
5897bfd_boolean
5898bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5899{
5900 switch (bfd_get_format (abfd))
5901 {
5902 case bfd_object:
5903 return elf_link_add_object_symbols (abfd, info);
5904 case bfd_archive:
5905 return elf_link_add_archive_symbols (abfd, info);
5906 default:
5907 bfd_set_error (bfd_error_wrong_format);
5908 return FALSE;
5909 }
5910}
5a580b3a 5911\f
14b1c01e
AM
5912struct hash_codes_info
5913{
5914 unsigned long *hashcodes;
5915 bfd_boolean error;
5916};
a0c8462f 5917
5a580b3a
AM
5918/* This function will be called though elf_link_hash_traverse to store
5919 all hash value of the exported symbols in an array. */
5920
5921static bfd_boolean
5922elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5923{
a50b1753 5924 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5925 const char *name;
5a580b3a
AM
5926 unsigned long ha;
5927 char *alc = NULL;
5928
5a580b3a
AM
5929 /* Ignore indirect symbols. These are added by the versioning code. */
5930 if (h->dynindx == -1)
5931 return TRUE;
5932
5933 name = h->root.root.string;
422f1182 5934 if (h->versioned >= versioned)
5a580b3a 5935 {
422f1182
L
5936 char *p = strchr (name, ELF_VER_CHR);
5937 if (p != NULL)
14b1c01e 5938 {
422f1182
L
5939 alc = (char *) bfd_malloc (p - name + 1);
5940 if (alc == NULL)
5941 {
5942 inf->error = TRUE;
5943 return FALSE;
5944 }
5945 memcpy (alc, name, p - name);
5946 alc[p - name] = '\0';
5947 name = alc;
14b1c01e 5948 }
5a580b3a
AM
5949 }
5950
5951 /* Compute the hash value. */
5952 ha = bfd_elf_hash (name);
5953
5954 /* Store the found hash value in the array given as the argument. */
14b1c01e 5955 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5956
5957 /* And store it in the struct so that we can put it in the hash table
5958 later. */
f6e332e6 5959 h->u.elf_hash_value = ha;
5a580b3a 5960
c9594989 5961 free (alc);
5a580b3a
AM
5962 return TRUE;
5963}
5964
fdc90cb4
JJ
5965struct collect_gnu_hash_codes
5966{
5967 bfd *output_bfd;
5968 const struct elf_backend_data *bed;
5969 unsigned long int nsyms;
5970 unsigned long int maskbits;
5971 unsigned long int *hashcodes;
5972 unsigned long int *hashval;
5973 unsigned long int *indx;
5974 unsigned long int *counts;
5975 bfd_vma *bitmask;
5976 bfd_byte *contents;
f16a9783 5977 bfd_size_type xlat;
fdc90cb4
JJ
5978 long int min_dynindx;
5979 unsigned long int bucketcount;
5980 unsigned long int symindx;
5981 long int local_indx;
5982 long int shift1, shift2;
5983 unsigned long int mask;
14b1c01e 5984 bfd_boolean error;
fdc90cb4
JJ
5985};
5986
5987/* This function will be called though elf_link_hash_traverse to store
5988 all hash value of the exported symbols in an array. */
5989
5990static bfd_boolean
5991elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5992{
a50b1753 5993 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5994 const char *name;
fdc90cb4
JJ
5995 unsigned long ha;
5996 char *alc = NULL;
5997
fdc90cb4
JJ
5998 /* Ignore indirect symbols. These are added by the versioning code. */
5999 if (h->dynindx == -1)
6000 return TRUE;
6001
6002 /* Ignore also local symbols and undefined symbols. */
6003 if (! (*s->bed->elf_hash_symbol) (h))
6004 return TRUE;
6005
6006 name = h->root.root.string;
422f1182 6007 if (h->versioned >= versioned)
fdc90cb4 6008 {
422f1182
L
6009 char *p = strchr (name, ELF_VER_CHR);
6010 if (p != NULL)
14b1c01e 6011 {
422f1182
L
6012 alc = (char *) bfd_malloc (p - name + 1);
6013 if (alc == NULL)
6014 {
6015 s->error = TRUE;
6016 return FALSE;
6017 }
6018 memcpy (alc, name, p - name);
6019 alc[p - name] = '\0';
6020 name = alc;
14b1c01e 6021 }
fdc90cb4
JJ
6022 }
6023
6024 /* Compute the hash value. */
6025 ha = bfd_elf_gnu_hash (name);
6026
6027 /* Store the found hash value in the array for compute_bucket_count,
6028 and also for .dynsym reordering purposes. */
6029 s->hashcodes[s->nsyms] = ha;
6030 s->hashval[h->dynindx] = ha;
6031 ++s->nsyms;
6032 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6033 s->min_dynindx = h->dynindx;
6034
c9594989 6035 free (alc);
fdc90cb4
JJ
6036 return TRUE;
6037}
6038
6039/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6040 final dynamic symbol renumbering in case of .gnu.hash.
6041 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6042 to the translation table. */
fdc90cb4
JJ
6043
6044static bfd_boolean
f16a9783 6045elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6046{
a50b1753 6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6048 unsigned long int bucket;
6049 unsigned long int val;
6050
fdc90cb4
JJ
6051 /* Ignore indirect symbols. */
6052 if (h->dynindx == -1)
6053 return TRUE;
6054
6055 /* Ignore also local symbols and undefined symbols. */
6056 if (! (*s->bed->elf_hash_symbol) (h))
6057 {
6058 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6059 {
6060 if (s->bed->record_xhash_symbol != NULL)
6061 {
6062 (*s->bed->record_xhash_symbol) (h, 0);
6063 s->local_indx++;
6064 }
6065 else
6066 h->dynindx = s->local_indx++;
6067 }
fdc90cb4
JJ
6068 return TRUE;
6069 }
6070
6071 bucket = s->hashval[h->dynindx] % s->bucketcount;
6072 val = (s->hashval[h->dynindx] >> s->shift1)
6073 & ((s->maskbits >> s->shift1) - 1);
6074 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6075 s->bitmask[val]
6076 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6077 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6078 if (s->counts[bucket] == 1)
6079 /* Last element terminates the chain. */
6080 val |= 1;
6081 bfd_put_32 (s->output_bfd, val,
6082 s->contents + (s->indx[bucket] - s->symindx) * 4);
6083 --s->counts[bucket];
f16a9783
MS
6084 if (s->bed->record_xhash_symbol != NULL)
6085 {
6086 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6087
6088 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6089 }
6090 else
6091 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6092 return TRUE;
6093}
6094
6095/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6096
6097bfd_boolean
6098_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6099{
6100 return !(h->forced_local
6101 || h->root.type == bfd_link_hash_undefined
6102 || h->root.type == bfd_link_hash_undefweak
6103 || ((h->root.type == bfd_link_hash_defined
6104 || h->root.type == bfd_link_hash_defweak)
6105 && h->root.u.def.section->output_section == NULL));
6106}
6107
5a580b3a
AM
6108/* Array used to determine the number of hash table buckets to use
6109 based on the number of symbols there are. If there are fewer than
6110 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6111 fewer than 37 we use 17 buckets, and so forth. We never use more
6112 than 32771 buckets. */
6113
6114static const size_t elf_buckets[] =
6115{
6116 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6117 16411, 32771, 0
6118};
6119
6120/* Compute bucket count for hashing table. We do not use a static set
6121 of possible tables sizes anymore. Instead we determine for all
6122 possible reasonable sizes of the table the outcome (i.e., the
6123 number of collisions etc) and choose the best solution. The
6124 weighting functions are not too simple to allow the table to grow
6125 without bounds. Instead one of the weighting factors is the size.
6126 Therefore the result is always a good payoff between few collisions
6127 (= short chain lengths) and table size. */
6128static size_t
b20dd2ce 6129compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6130 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6131 unsigned long int nsyms,
6132 int gnu_hash)
5a580b3a 6133{
5a580b3a 6134 size_t best_size = 0;
5a580b3a 6135 unsigned long int i;
5a580b3a 6136
5a580b3a
AM
6137 /* We have a problem here. The following code to optimize the table
6138 size requires an integer type with more the 32 bits. If
6139 BFD_HOST_U_64_BIT is set we know about such a type. */
6140#ifdef BFD_HOST_U_64_BIT
6141 if (info->optimize)
6142 {
5a580b3a
AM
6143 size_t minsize;
6144 size_t maxsize;
6145 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6146 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6147 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6148 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6149 unsigned long int *counts;
d40f3da9 6150 bfd_size_type amt;
0883b6e0 6151 unsigned int no_improvement_count = 0;
5a580b3a
AM
6152
6153 /* Possible optimization parameters: if we have NSYMS symbols we say
6154 that the hashing table must at least have NSYMS/4 and at most
6155 2*NSYMS buckets. */
6156 minsize = nsyms / 4;
6157 if (minsize == 0)
6158 minsize = 1;
6159 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6160 if (gnu_hash)
6161 {
6162 if (minsize < 2)
6163 minsize = 2;
6164 if ((best_size & 31) == 0)
6165 ++best_size;
6166 }
5a580b3a
AM
6167
6168 /* Create array where we count the collisions in. We must use bfd_malloc
6169 since the size could be large. */
6170 amt = maxsize;
6171 amt *= sizeof (unsigned long int);
a50b1753 6172 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6173 if (counts == NULL)
fdc90cb4 6174 return 0;
5a580b3a
AM
6175
6176 /* Compute the "optimal" size for the hash table. The criteria is a
6177 minimal chain length. The minor criteria is (of course) the size
6178 of the table. */
6179 for (i = minsize; i < maxsize; ++i)
6180 {
6181 /* Walk through the array of hashcodes and count the collisions. */
6182 BFD_HOST_U_64_BIT max;
6183 unsigned long int j;
6184 unsigned long int fact;
6185
fdc90cb4
JJ
6186 if (gnu_hash && (i & 31) == 0)
6187 continue;
6188
5a580b3a
AM
6189 memset (counts, '\0', i * sizeof (unsigned long int));
6190
6191 /* Determine how often each hash bucket is used. */
6192 for (j = 0; j < nsyms; ++j)
6193 ++counts[hashcodes[j] % i];
6194
6195 /* For the weight function we need some information about the
6196 pagesize on the target. This is information need not be 100%
6197 accurate. Since this information is not available (so far) we
6198 define it here to a reasonable default value. If it is crucial
6199 to have a better value some day simply define this value. */
6200# ifndef BFD_TARGET_PAGESIZE
6201# define BFD_TARGET_PAGESIZE (4096)
6202# endif
6203
fdc90cb4
JJ
6204 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6205 and the chains. */
6206 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6207
6208# if 1
6209 /* Variant 1: optimize for short chains. We add the squares
6210 of all the chain lengths (which favors many small chain
6211 over a few long chains). */
6212 for (j = 0; j < i; ++j)
6213 max += counts[j] * counts[j];
6214
6215 /* This adds penalties for the overall size of the table. */
fdc90cb4 6216 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6217 max *= fact * fact;
6218# else
6219 /* Variant 2: Optimize a lot more for small table. Here we
6220 also add squares of the size but we also add penalties for
6221 empty slots (the +1 term). */
6222 for (j = 0; j < i; ++j)
6223 max += (1 + counts[j]) * (1 + counts[j]);
6224
6225 /* The overall size of the table is considered, but not as
6226 strong as in variant 1, where it is squared. */
fdc90cb4 6227 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6228 max *= fact;
6229# endif
6230
6231 /* Compare with current best results. */
6232 if (max < best_chlen)
6233 {
6234 best_chlen = max;
6235 best_size = i;
ca4be51c 6236 no_improvement_count = 0;
5a580b3a 6237 }
0883b6e0
NC
6238 /* PR 11843: Avoid futile long searches for the best bucket size
6239 when there are a large number of symbols. */
6240 else if (++no_improvement_count == 100)
6241 break;
5a580b3a
AM
6242 }
6243
6244 free (counts);
6245 }
6246 else
6247#endif /* defined (BFD_HOST_U_64_BIT) */
6248 {
6249 /* This is the fallback solution if no 64bit type is available or if we
6250 are not supposed to spend much time on optimizations. We select the
6251 bucket count using a fixed set of numbers. */
6252 for (i = 0; elf_buckets[i] != 0; i++)
6253 {
6254 best_size = elf_buckets[i];
fdc90cb4 6255 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6256 break;
6257 }
fdc90cb4
JJ
6258 if (gnu_hash && best_size < 2)
6259 best_size = 2;
5a580b3a
AM
6260 }
6261
5a580b3a
AM
6262 return best_size;
6263}
6264
d0bf826b
AM
6265/* Size any SHT_GROUP section for ld -r. */
6266
6267bfd_boolean
6268_bfd_elf_size_group_sections (struct bfd_link_info *info)
6269{
6270 bfd *ibfd;
57963c05 6271 asection *s;
d0bf826b 6272
c72f2fb2 6273 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6274 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6275 && (s = ibfd->sections) != NULL
6276 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6277 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6278 return FALSE;
6279 return TRUE;
6280}
6281
04c3a755
NS
6282/* Set a default stack segment size. The value in INFO wins. If it
6283 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6284 undefined it is initialized. */
6285
6286bfd_boolean
6287bfd_elf_stack_segment_size (bfd *output_bfd,
6288 struct bfd_link_info *info,
6289 const char *legacy_symbol,
6290 bfd_vma default_size)
6291{
6292 struct elf_link_hash_entry *h = NULL;
6293
6294 /* Look for legacy symbol. */
6295 if (legacy_symbol)
6296 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6297 FALSE, FALSE, FALSE);
6298 if (h && (h->root.type == bfd_link_hash_defined
6299 || h->root.type == bfd_link_hash_defweak)
6300 && h->def_regular
6301 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6302 {
6303 /* The symbol has no type if specified on the command line. */
6304 h->type = STT_OBJECT;
6305 if (info->stacksize)
695344c0 6306 /* xgettext:c-format */
871b3ab2 6307 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6308 output_bfd, legacy_symbol);
04c3a755 6309 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6310 /* xgettext:c-format */
871b3ab2 6311 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6312 output_bfd, legacy_symbol);
04c3a755
NS
6313 else
6314 info->stacksize = h->root.u.def.value;
6315 }
6316
6317 if (!info->stacksize)
6318 /* If the user didn't set a size, or explicitly inhibit the
6319 size, set it now. */
6320 info->stacksize = default_size;
6321
6322 /* Provide the legacy symbol, if it is referenced. */
6323 if (h && (h->root.type == bfd_link_hash_undefined
6324 || h->root.type == bfd_link_hash_undefweak))
6325 {
6326 struct bfd_link_hash_entry *bh = NULL;
6327
6328 if (!(_bfd_generic_link_add_one_symbol
6329 (info, output_bfd, legacy_symbol,
6330 BSF_GLOBAL, bfd_abs_section_ptr,
6331 info->stacksize >= 0 ? info->stacksize : 0,
6332 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6333 return FALSE;
6334
6335 h = (struct elf_link_hash_entry *) bh;
6336 h->def_regular = 1;
6337 h->type = STT_OBJECT;
6338 }
6339
6340 return TRUE;
6341}
6342
b531344c
MR
6343/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6344
6345struct elf_gc_sweep_symbol_info
6346{
6347 struct bfd_link_info *info;
6348 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6349 bfd_boolean);
6350};
6351
6352static bfd_boolean
6353elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6354{
6355 if (!h->mark
6356 && (((h->root.type == bfd_link_hash_defined
6357 || h->root.type == bfd_link_hash_defweak)
6358 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6359 && h->root.u.def.section->gc_mark))
6360 || h->root.type == bfd_link_hash_undefined
6361 || h->root.type == bfd_link_hash_undefweak))
6362 {
6363 struct elf_gc_sweep_symbol_info *inf;
6364
6365 inf = (struct elf_gc_sweep_symbol_info *) data;
6366 (*inf->hide_symbol) (inf->info, h, TRUE);
6367 h->def_regular = 0;
6368 h->ref_regular = 0;
6369 h->ref_regular_nonweak = 0;
6370 }
6371
6372 return TRUE;
6373}
6374
5a580b3a
AM
6375/* Set up the sizes and contents of the ELF dynamic sections. This is
6376 called by the ELF linker emulation before_allocation routine. We
6377 must set the sizes of the sections before the linker sets the
6378 addresses of the various sections. */
6379
6380bfd_boolean
6381bfd_elf_size_dynamic_sections (bfd *output_bfd,
6382 const char *soname,
6383 const char *rpath,
6384 const char *filter_shlib,
7ee314fa
AM
6385 const char *audit,
6386 const char *depaudit,
5a580b3a
AM
6387 const char * const *auxiliary_filters,
6388 struct bfd_link_info *info,
fd91d419 6389 asection **sinterpptr)
5a580b3a 6390{
5a580b3a
AM
6391 bfd *dynobj;
6392 const struct elf_backend_data *bed;
5a580b3a
AM
6393
6394 *sinterpptr = NULL;
6395
5a580b3a
AM
6396 if (!is_elf_hash_table (info->hash))
6397 return TRUE;
6398
5a580b3a
AM
6399 dynobj = elf_hash_table (info)->dynobj;
6400
9a2a56cc 6401 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6402 {
902e9fc7
MR
6403 struct bfd_elf_version_tree *verdefs;
6404 struct elf_info_failed asvinfo;
5a580b3a
AM
6405 struct bfd_elf_version_tree *t;
6406 struct bfd_elf_version_expr *d;
902e9fc7 6407 asection *s;
e6699019 6408 size_t soname_indx;
7ee314fa 6409
5a580b3a
AM
6410 /* If we are supposed to export all symbols into the dynamic symbol
6411 table (this is not the normal case), then do so. */
55255dae 6412 if (info->export_dynamic
0e1862bb 6413 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6414 {
3d13f3e9
AM
6415 struct elf_info_failed eif;
6416
6417 eif.info = info;
6418 eif.failed = FALSE;
5a580b3a
AM
6419 elf_link_hash_traverse (elf_hash_table (info),
6420 _bfd_elf_export_symbol,
6421 &eif);
6422 if (eif.failed)
6423 return FALSE;
6424 }
6425
e6699019
L
6426 if (soname != NULL)
6427 {
6428 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6429 soname, TRUE);
6430 if (soname_indx == (size_t) -1
6431 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6432 return FALSE;
6433 }
6434 else
6435 soname_indx = (size_t) -1;
6436
5a580b3a 6437 /* Make all global versions with definition. */
fd91d419 6438 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6439 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6440 if (!d->symver && d->literal)
5a580b3a
AM
6441 {
6442 const char *verstr, *name;
6443 size_t namelen, verlen, newlen;
93252b1c 6444 char *newname, *p, leading_char;
5a580b3a
AM
6445 struct elf_link_hash_entry *newh;
6446
93252b1c 6447 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6448 name = d->pattern;
93252b1c 6449 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6450 verstr = t->name;
6451 verlen = strlen (verstr);
6452 newlen = namelen + verlen + 3;
6453
a50b1753 6454 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6455 if (newname == NULL)
6456 return FALSE;
93252b1c
MF
6457 newname[0] = leading_char;
6458 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6459
6460 /* Check the hidden versioned definition. */
6461 p = newname + namelen;
6462 *p++ = ELF_VER_CHR;
6463 memcpy (p, verstr, verlen + 1);
6464 newh = elf_link_hash_lookup (elf_hash_table (info),
6465 newname, FALSE, FALSE,
6466 FALSE);
6467 if (newh == NULL
6468 || (newh->root.type != bfd_link_hash_defined
6469 && newh->root.type != bfd_link_hash_defweak))
6470 {
6471 /* Check the default versioned definition. */
6472 *p++ = ELF_VER_CHR;
6473 memcpy (p, verstr, verlen + 1);
6474 newh = elf_link_hash_lookup (elf_hash_table (info),
6475 newname, FALSE, FALSE,
6476 FALSE);
6477 }
6478 free (newname);
6479
6480 /* Mark this version if there is a definition and it is
6481 not defined in a shared object. */
6482 if (newh != NULL
f5385ebf 6483 && !newh->def_dynamic
5a580b3a
AM
6484 && (newh->root.type == bfd_link_hash_defined
6485 || newh->root.type == bfd_link_hash_defweak))
6486 d->symver = 1;
6487 }
6488
6489 /* Attach all the symbols to their version information. */
5a580b3a 6490 asvinfo.info = info;
5a580b3a
AM
6491 asvinfo.failed = FALSE;
6492
6493 elf_link_hash_traverse (elf_hash_table (info),
6494 _bfd_elf_link_assign_sym_version,
6495 &asvinfo);
6496 if (asvinfo.failed)
6497 return FALSE;
6498
6499 if (!info->allow_undefined_version)
6500 {
6501 /* Check if all global versions have a definition. */
3d13f3e9 6502 bfd_boolean all_defined = TRUE;
fd91d419 6503 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6504 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6505 if (d->literal && !d->symver && !d->script)
5a580b3a 6506 {
4eca0228 6507 _bfd_error_handler
5a580b3a
AM
6508 (_("%s: undefined version: %s"),
6509 d->pattern, t->name);
6510 all_defined = FALSE;
6511 }
6512
6513 if (!all_defined)
6514 {
6515 bfd_set_error (bfd_error_bad_value);
6516 return FALSE;
6517 }
6518 }
6519
902e9fc7
MR
6520 /* Set up the version definition section. */
6521 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6522 BFD_ASSERT (s != NULL);
5a580b3a 6523
902e9fc7
MR
6524 /* We may have created additional version definitions if we are
6525 just linking a regular application. */
6526 verdefs = info->version_info;
5a580b3a 6527
902e9fc7
MR
6528 /* Skip anonymous version tag. */
6529 if (verdefs != NULL && verdefs->vernum == 0)
6530 verdefs = verdefs->next;
5a580b3a 6531
902e9fc7
MR
6532 if (verdefs == NULL && !info->create_default_symver)
6533 s->flags |= SEC_EXCLUDE;
6534 else
5a580b3a 6535 {
902e9fc7
MR
6536 unsigned int cdefs;
6537 bfd_size_type size;
6538 bfd_byte *p;
6539 Elf_Internal_Verdef def;
6540 Elf_Internal_Verdaux defaux;
6541 struct bfd_link_hash_entry *bh;
6542 struct elf_link_hash_entry *h;
6543 const char *name;
5a580b3a 6544
902e9fc7
MR
6545 cdefs = 0;
6546 size = 0;
5a580b3a 6547
902e9fc7
MR
6548 /* Make space for the base version. */
6549 size += sizeof (Elf_External_Verdef);
6550 size += sizeof (Elf_External_Verdaux);
6551 ++cdefs;
6552
6553 /* Make space for the default version. */
6554 if (info->create_default_symver)
6555 {
6556 size += sizeof (Elf_External_Verdef);
6557 ++cdefs;
3e3b46e5
PB
6558 }
6559
5a580b3a
AM
6560 for (t = verdefs; t != NULL; t = t->next)
6561 {
6562 struct bfd_elf_version_deps *n;
6563
a6cc6b3b
RO
6564 /* Don't emit base version twice. */
6565 if (t->vernum == 0)
6566 continue;
6567
5a580b3a
AM
6568 size += sizeof (Elf_External_Verdef);
6569 size += sizeof (Elf_External_Verdaux);
6570 ++cdefs;
6571
6572 for (n = t->deps; n != NULL; n = n->next)
6573 size += sizeof (Elf_External_Verdaux);
6574 }
6575
eea6121a 6576 s->size = size;
a50b1753 6577 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6578 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6579 return FALSE;
6580
6581 /* Fill in the version definition section. */
6582
6583 p = s->contents;
6584
6585 def.vd_version = VER_DEF_CURRENT;
6586 def.vd_flags = VER_FLG_BASE;
6587 def.vd_ndx = 1;
6588 def.vd_cnt = 1;
3e3b46e5
PB
6589 if (info->create_default_symver)
6590 {
6591 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6592 def.vd_next = sizeof (Elf_External_Verdef);
6593 }
6594 else
6595 {
6596 def.vd_aux = sizeof (Elf_External_Verdef);
6597 def.vd_next = (sizeof (Elf_External_Verdef)
6598 + sizeof (Elf_External_Verdaux));
6599 }
5a580b3a 6600
ef53be89 6601 if (soname_indx != (size_t) -1)
5a580b3a
AM
6602 {
6603 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6604 soname_indx);
6605 def.vd_hash = bfd_elf_hash (soname);
6606 defaux.vda_name = soname_indx;
3e3b46e5 6607 name = soname;
5a580b3a
AM
6608 }
6609 else
6610 {
ef53be89 6611 size_t indx;
5a580b3a 6612
765cf5f6 6613 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6614 def.vd_hash = bfd_elf_hash (name);
6615 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6616 name, FALSE);
ef53be89 6617 if (indx == (size_t) -1)
5a580b3a
AM
6618 return FALSE;
6619 defaux.vda_name = indx;
6620 }
6621 defaux.vda_next = 0;
6622
6623 _bfd_elf_swap_verdef_out (output_bfd, &def,
6624 (Elf_External_Verdef *) p);
6625 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6626 if (info->create_default_symver)
6627 {
6628 /* Add a symbol representing this version. */
6629 bh = NULL;
6630 if (! (_bfd_generic_link_add_one_symbol
6631 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6632 0, NULL, FALSE,
6633 get_elf_backend_data (dynobj)->collect, &bh)))
6634 return FALSE;
6635 h = (struct elf_link_hash_entry *) bh;
6636 h->non_elf = 0;
6637 h->def_regular = 1;
6638 h->type = STT_OBJECT;
6639 h->verinfo.vertree = NULL;
6640
6641 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6642 return FALSE;
6643
6644 /* Create a duplicate of the base version with the same
6645 aux block, but different flags. */
6646 def.vd_flags = 0;
6647 def.vd_ndx = 2;
6648 def.vd_aux = sizeof (Elf_External_Verdef);
6649 if (verdefs)
6650 def.vd_next = (sizeof (Elf_External_Verdef)
6651 + sizeof (Elf_External_Verdaux));
6652 else
6653 def.vd_next = 0;
6654 _bfd_elf_swap_verdef_out (output_bfd, &def,
6655 (Elf_External_Verdef *) p);
6656 p += sizeof (Elf_External_Verdef);
6657 }
5a580b3a
AM
6658 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6659 (Elf_External_Verdaux *) p);
6660 p += sizeof (Elf_External_Verdaux);
6661
6662 for (t = verdefs; t != NULL; t = t->next)
6663 {
6664 unsigned int cdeps;
6665 struct bfd_elf_version_deps *n;
5a580b3a 6666
a6cc6b3b
RO
6667 /* Don't emit the base version twice. */
6668 if (t->vernum == 0)
6669 continue;
6670
5a580b3a
AM
6671 cdeps = 0;
6672 for (n = t->deps; n != NULL; n = n->next)
6673 ++cdeps;
6674
6675 /* Add a symbol representing this version. */
6676 bh = NULL;
6677 if (! (_bfd_generic_link_add_one_symbol
6678 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6679 0, NULL, FALSE,
6680 get_elf_backend_data (dynobj)->collect, &bh)))
6681 return FALSE;
6682 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6683 h->non_elf = 0;
6684 h->def_regular = 1;
5a580b3a
AM
6685 h->type = STT_OBJECT;
6686 h->verinfo.vertree = t;
6687
c152c796 6688 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6689 return FALSE;
6690
6691 def.vd_version = VER_DEF_CURRENT;
6692 def.vd_flags = 0;
6693 if (t->globals.list == NULL
6694 && t->locals.list == NULL
6695 && ! t->used)
6696 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6697 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6698 def.vd_cnt = cdeps + 1;
6699 def.vd_hash = bfd_elf_hash (t->name);
6700 def.vd_aux = sizeof (Elf_External_Verdef);
6701 def.vd_next = 0;
a6cc6b3b
RO
6702
6703 /* If a basever node is next, it *must* be the last node in
6704 the chain, otherwise Verdef construction breaks. */
6705 if (t->next != NULL && t->next->vernum == 0)
6706 BFD_ASSERT (t->next->next == NULL);
6707
6708 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6709 def.vd_next = (sizeof (Elf_External_Verdef)
6710 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6711
6712 _bfd_elf_swap_verdef_out (output_bfd, &def,
6713 (Elf_External_Verdef *) p);
6714 p += sizeof (Elf_External_Verdef);
6715
6716 defaux.vda_name = h->dynstr_index;
6717 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6718 h->dynstr_index);
6719 defaux.vda_next = 0;
6720 if (t->deps != NULL)
6721 defaux.vda_next = sizeof (Elf_External_Verdaux);
6722 t->name_indx = defaux.vda_name;
6723
6724 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6725 (Elf_External_Verdaux *) p);
6726 p += sizeof (Elf_External_Verdaux);
6727
6728 for (n = t->deps; n != NULL; n = n->next)
6729 {
6730 if (n->version_needed == NULL)
6731 {
6732 /* This can happen if there was an error in the
6733 version script. */
6734 defaux.vda_name = 0;
6735 }
6736 else
6737 {
6738 defaux.vda_name = n->version_needed->name_indx;
6739 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6740 defaux.vda_name);
6741 }
6742 if (n->next == NULL)
6743 defaux.vda_next = 0;
6744 else
6745 defaux.vda_next = sizeof (Elf_External_Verdaux);
6746
6747 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6748 (Elf_External_Verdaux *) p);
6749 p += sizeof (Elf_External_Verdaux);
6750 }
6751 }
6752
5a580b3a
AM
6753 elf_tdata (output_bfd)->cverdefs = cdefs;
6754 }
902e9fc7
MR
6755 }
6756
6757 bed = get_elf_backend_data (output_bfd);
6758
6759 if (info->gc_sections && bed->can_gc_sections)
6760 {
6761 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6762
6763 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6764 dynamic symbol table. */
902e9fc7
MR
6765 sweep_info.info = info;
6766 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6767 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6768 &sweep_info);
3d13f3e9
AM
6769 }
6770
6771 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6772 {
6773 asection *s;
6774 struct elf_find_verdep_info sinfo;
6775
6776 /* Work out the size of the version reference section. */
6777
6778 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6779 BFD_ASSERT (s != NULL);
902e9fc7 6780
3d13f3e9
AM
6781 sinfo.info = info;
6782 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6783 if (sinfo.vers == 0)
6784 sinfo.vers = 1;
6785 sinfo.failed = FALSE;
6786
6787 elf_link_hash_traverse (elf_hash_table (info),
6788 _bfd_elf_link_find_version_dependencies,
6789 &sinfo);
6790 if (sinfo.failed)
6791 return FALSE;
6792
6793 if (elf_tdata (output_bfd)->verref == NULL)
6794 s->flags |= SEC_EXCLUDE;
6795 else
6796 {
6797 Elf_Internal_Verneed *vn;
6798 unsigned int size;
6799 unsigned int crefs;
6800 bfd_byte *p;
6801
6802 /* Build the version dependency section. */
6803 size = 0;
6804 crefs = 0;
6805 for (vn = elf_tdata (output_bfd)->verref;
6806 vn != NULL;
6807 vn = vn->vn_nextref)
6808 {
6809 Elf_Internal_Vernaux *a;
6810
6811 size += sizeof (Elf_External_Verneed);
6812 ++crefs;
6813 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6814 size += sizeof (Elf_External_Vernaux);
6815 }
6816
6817 s->size = size;
6818 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6819 if (s->contents == NULL)
6820 return FALSE;
6821
6822 p = s->contents;
6823 for (vn = elf_tdata (output_bfd)->verref;
6824 vn != NULL;
6825 vn = vn->vn_nextref)
6826 {
6827 unsigned int caux;
6828 Elf_Internal_Vernaux *a;
6829 size_t indx;
6830
6831 caux = 0;
6832 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6833 ++caux;
6834
6835 vn->vn_version = VER_NEED_CURRENT;
6836 vn->vn_cnt = caux;
6837 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6838 elf_dt_name (vn->vn_bfd) != NULL
6839 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6840 : lbasename (bfd_get_filename
6841 (vn->vn_bfd)),
3d13f3e9
AM
6842 FALSE);
6843 if (indx == (size_t) -1)
6844 return FALSE;
6845 vn->vn_file = indx;
6846 vn->vn_aux = sizeof (Elf_External_Verneed);
6847 if (vn->vn_nextref == NULL)
6848 vn->vn_next = 0;
6849 else
6850 vn->vn_next = (sizeof (Elf_External_Verneed)
6851 + caux * sizeof (Elf_External_Vernaux));
6852
6853 _bfd_elf_swap_verneed_out (output_bfd, vn,
6854 (Elf_External_Verneed *) p);
6855 p += sizeof (Elf_External_Verneed);
6856
6857 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6858 {
6859 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6860 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6861 a->vna_nodename, FALSE);
6862 if (indx == (size_t) -1)
6863 return FALSE;
6864 a->vna_name = indx;
6865 if (a->vna_nextptr == NULL)
6866 a->vna_next = 0;
6867 else
6868 a->vna_next = sizeof (Elf_External_Vernaux);
6869
6870 _bfd_elf_swap_vernaux_out (output_bfd, a,
6871 (Elf_External_Vernaux *) p);
6872 p += sizeof (Elf_External_Vernaux);
6873 }
6874 }
6875
6876 elf_tdata (output_bfd)->cverrefs = crefs;
6877 }
902e9fc7
MR
6878 }
6879
6880 /* Any syms created from now on start with -1 in
6881 got.refcount/offset and plt.refcount/offset. */
6882 elf_hash_table (info)->init_got_refcount
6883 = elf_hash_table (info)->init_got_offset;
6884 elf_hash_table (info)->init_plt_refcount
6885 = elf_hash_table (info)->init_plt_offset;
6886
6887 if (bfd_link_relocatable (info)
6888 && !_bfd_elf_size_group_sections (info))
6889 return FALSE;
6890
6891 /* The backend may have to create some sections regardless of whether
6892 we're dynamic or not. */
6893 if (bed->elf_backend_always_size_sections
6894 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6895 return FALSE;
6896
6897 /* Determine any GNU_STACK segment requirements, after the backend
6898 has had a chance to set a default segment size. */
6899 if (info->execstack)
6900 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6901 else if (info->noexecstack)
6902 elf_stack_flags (output_bfd) = PF_R | PF_W;
6903 else
6904 {
6905 bfd *inputobj;
6906 asection *notesec = NULL;
6907 int exec = 0;
6908
6909 for (inputobj = info->input_bfds;
6910 inputobj;
6911 inputobj = inputobj->link.next)
6912 {
6913 asection *s;
6914
6915 if (inputobj->flags
6916 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6917 continue;
57963c05
AM
6918 s = inputobj->sections;
6919 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6920 continue;
6921
902e9fc7
MR
6922 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6923 if (s)
6924 {
6925 if (s->flags & SEC_CODE)
6926 exec = PF_X;
6927 notesec = s;
6928 }
6929 else if (bed->default_execstack)
6930 exec = PF_X;
6931 }
6932 if (notesec || info->stacksize > 0)
6933 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6934 if (notesec && exec && bfd_link_relocatable (info)
6935 && notesec->output_section != bfd_abs_section_ptr)
6936 notesec->output_section->flags |= SEC_CODE;
6937 }
6938
6939 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6940 {
6941 struct elf_info_failed eif;
6942 struct elf_link_hash_entry *h;
6943 asection *dynstr;
6944 asection *s;
6945
6946 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6947 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6948
902e9fc7
MR
6949 if (info->symbolic)
6950 {
6951 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6952 return FALSE;
6953 info->flags |= DF_SYMBOLIC;
6954 }
6955
6956 if (rpath != NULL)
6957 {
6958 size_t indx;
6959 bfd_vma tag;
6960
6961 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6962 TRUE);
6963 if (indx == (size_t) -1)
6964 return FALSE;
6965
6966 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6967 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6968 return FALSE;
6969 }
6970
6971 if (filter_shlib != NULL)
6972 {
6973 size_t indx;
6974
6975 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6976 filter_shlib, TRUE);
6977 if (indx == (size_t) -1
6978 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6979 return FALSE;
6980 }
6981
6982 if (auxiliary_filters != NULL)
6983 {
6984 const char * const *p;
6985
6986 for (p = auxiliary_filters; *p != NULL; p++)
6987 {
6988 size_t indx;
6989
6990 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6991 *p, TRUE);
6992 if (indx == (size_t) -1
6993 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6994 return FALSE;
6995 }
6996 }
6997
6998 if (audit != NULL)
6999 {
7000 size_t indx;
7001
7002 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7003 TRUE);
7004 if (indx == (size_t) -1
7005 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7006 return FALSE;
7007 }
7008
7009 if (depaudit != NULL)
7010 {
7011 size_t indx;
7012
7013 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7014 TRUE);
7015 if (indx == (size_t) -1
7016 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7017 return FALSE;
7018 }
7019
7020 eif.info = info;
7021 eif.failed = FALSE;
7022
7023 /* Find all symbols which were defined in a dynamic object and make
7024 the backend pick a reasonable value for them. */
7025 elf_link_hash_traverse (elf_hash_table (info),
7026 _bfd_elf_adjust_dynamic_symbol,
7027 &eif);
7028 if (eif.failed)
7029 return FALSE;
7030
7031 /* Add some entries to the .dynamic section. We fill in some of the
7032 values later, in bfd_elf_final_link, but we must add the entries
7033 now so that we know the final size of the .dynamic section. */
7034
7035 /* If there are initialization and/or finalization functions to
7036 call then add the corresponding DT_INIT/DT_FINI entries. */
7037 h = (info->init_function
7038 ? elf_link_hash_lookup (elf_hash_table (info),
7039 info->init_function, FALSE,
7040 FALSE, FALSE)
7041 : NULL);
7042 if (h != NULL
7043 && (h->ref_regular
7044 || h->def_regular))
7045 {
7046 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7047 return FALSE;
7048 }
7049 h = (info->fini_function
7050 ? elf_link_hash_lookup (elf_hash_table (info),
7051 info->fini_function, FALSE,
7052 FALSE, FALSE)
7053 : NULL);
7054 if (h != NULL
7055 && (h->ref_regular
7056 || h->def_regular))
7057 {
7058 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7059 return FALSE;
7060 }
7061
7062 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7063 if (s != NULL && s->linker_has_input)
7064 {
7065 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7066 if (! bfd_link_executable (info))
7067 {
7068 bfd *sub;
7069 asection *o;
7070
57963c05
AM
7071 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7072 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7073 && (o = sub->sections) != NULL
7074 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7075 for (o = sub->sections; o != NULL; o = o->next)
7076 if (elf_section_data (o)->this_hdr.sh_type
7077 == SHT_PREINIT_ARRAY)
7078 {
7079 _bfd_error_handler
871b3ab2 7080 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7081 sub);
7082 break;
7083 }
7084
7085 bfd_set_error (bfd_error_nonrepresentable_section);
7086 return FALSE;
7087 }
7088
7089 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7090 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7091 return FALSE;
7092 }
7093 s = bfd_get_section_by_name (output_bfd, ".init_array");
7094 if (s != NULL && s->linker_has_input)
7095 {
7096 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7097 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7098 return FALSE;
7099 }
7100 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7101 if (s != NULL && s->linker_has_input)
7102 {
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7104 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7105 return FALSE;
7106 }
7107
7108 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7109 /* If .dynstr is excluded from the link, we don't want any of
7110 these tags. Strictly, we should be checking each section
7111 individually; This quick check covers for the case where
7112 someone does a /DISCARD/ : { *(*) }. */
7113 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7114 {
7115 bfd_size_type strsize;
7116
7117 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7118 if ((info->emit_hash
7119 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7120 || (info->emit_gnu_hash
f16a9783
MS
7121 && (bed->record_xhash_symbol == NULL
7122 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7123 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7124 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7125 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7126 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7127 bed->s->sizeof_sym))
7128 return FALSE;
7129 }
7130 }
7131
7132 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7133 return FALSE;
7134
7135 /* The backend must work out the sizes of all the other dynamic
7136 sections. */
7137 if (dynobj != NULL
7138 && bed->elf_backend_size_dynamic_sections != NULL
7139 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7140 return FALSE;
7141
7142 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7143 {
902e9fc7
MR
7144 if (elf_tdata (output_bfd)->cverdefs)
7145 {
7146 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7147
7148 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7149 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7150 return FALSE;
7151 }
7152
7153 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7156 return FALSE;
7157 }
7158 else if (info->flags & DF_BIND_NOW)
7159 {
7160 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7161 return FALSE;
7162 }
7163
7164 if (info->flags_1)
7165 {
7166 if (bfd_link_executable (info))
7167 info->flags_1 &= ~ (DF_1_INITFIRST
7168 | DF_1_NODELETE
7169 | DF_1_NOOPEN);
7170 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7171 return FALSE;
7172 }
7173
7174 if (elf_tdata (output_bfd)->cverrefs)
7175 {
7176 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7177
7178 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7180 return FALSE;
7181 }
5a580b3a 7182
8423293d
AM
7183 if ((elf_tdata (output_bfd)->cverrefs == 0
7184 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7185 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7186 {
902e9fc7
MR
7187 asection *s;
7188
3d4d4302 7189 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7190 s->flags |= SEC_EXCLUDE;
7191 }
7192 }
7193 return TRUE;
7194}
7195
74541ad4
AM
7196/* Find the first non-excluded output section. We'll use its
7197 section symbol for some emitted relocs. */
7198void
7199_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7200{
7201 asection *s;
f26a3287 7202 asection *found = NULL;
74541ad4
AM
7203
7204 for (s = output_bfd->sections; s != NULL; s = s->next)
7205 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7206 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7207 {
f26a3287
AM
7208 found = s;
7209 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7210 break;
74541ad4 7211 }
f26a3287 7212 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7213}
7214
7215/* Find two non-excluded output sections, one for code, one for data.
7216 We'll use their section symbols for some emitted relocs. */
7217void
7218_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7219{
7220 asection *s;
f26a3287 7221 asection *found = NULL;
74541ad4 7222
266b05cf 7223 /* Data first, since setting text_index_section changes
7f923b7f 7224 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7225 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7226 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7227 && !(s->flags & SEC_READONLY)
d00dd7dc 7228 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7229 {
f26a3287
AM
7230 found = s;
7231 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7232 break;
74541ad4 7233 }
f26a3287 7234 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7235
7236 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7237 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7238 && (s->flags & SEC_READONLY)
d00dd7dc 7239 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7240 {
f26a3287 7241 found = s;
74541ad4
AM
7242 break;
7243 }
f26a3287 7244 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7245}
7246
f16a9783
MS
7247#define GNU_HASH_SECTION_NAME(bed) \
7248 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7249
8423293d
AM
7250bfd_boolean
7251bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7252{
74541ad4 7253 const struct elf_backend_data *bed;
23ec1e32 7254 unsigned long section_sym_count;
96d01d93 7255 bfd_size_type dynsymcount = 0;
74541ad4 7256
8423293d
AM
7257 if (!is_elf_hash_table (info->hash))
7258 return TRUE;
7259
74541ad4
AM
7260 bed = get_elf_backend_data (output_bfd);
7261 (*bed->elf_backend_init_index_section) (output_bfd, info);
7262
23ec1e32
MR
7263 /* Assign dynsym indices. In a shared library we generate a section
7264 symbol for each output section, which come first. Next come all
7265 of the back-end allocated local dynamic syms, followed by the rest
7266 of the global symbols.
7267
7268 This is usually not needed for static binaries, however backends
7269 can request to always do it, e.g. the MIPS backend uses dynamic
7270 symbol counts to lay out GOT, which will be produced in the
7271 presence of GOT relocations even in static binaries (holding fixed
7272 data in that case, to satisfy those relocations). */
7273
7274 if (elf_hash_table (info)->dynamic_sections_created
7275 || bed->always_renumber_dynsyms)
7276 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7277 &section_sym_count);
7278
8423293d
AM
7279 if (elf_hash_table (info)->dynamic_sections_created)
7280 {
7281 bfd *dynobj;
8423293d 7282 asection *s;
8423293d
AM
7283 unsigned int dtagcount;
7284
7285 dynobj = elf_hash_table (info)->dynobj;
7286
5a580b3a 7287 /* Work out the size of the symbol version section. */
3d4d4302 7288 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7289 BFD_ASSERT (s != NULL);
d5486c43 7290 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7291 {
eea6121a 7292 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7293 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7294 if (s->contents == NULL)
7295 return FALSE;
7296
7297 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7298 return FALSE;
7299 }
7300
7301 /* Set the size of the .dynsym and .hash sections. We counted
7302 the number of dynamic symbols in elf_link_add_object_symbols.
7303 We will build the contents of .dynsym and .hash when we build
7304 the final symbol table, because until then we do not know the
7305 correct value to give the symbols. We built the .dynstr
7306 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7307 s = elf_hash_table (info)->dynsym;
5a580b3a 7308 BFD_ASSERT (s != NULL);
eea6121a 7309 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7310
d5486c43
L
7311 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7312 if (s->contents == NULL)
7313 return FALSE;
5a580b3a 7314
d5486c43
L
7315 /* The first entry in .dynsym is a dummy symbol. Clear all the
7316 section syms, in case we don't output them all. */
7317 ++section_sym_count;
7318 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7319
fdc90cb4
JJ
7320 elf_hash_table (info)->bucketcount = 0;
7321
5a580b3a
AM
7322 /* Compute the size of the hashing table. As a side effect this
7323 computes the hash values for all the names we export. */
fdc90cb4
JJ
7324 if (info->emit_hash)
7325 {
7326 unsigned long int *hashcodes;
14b1c01e 7327 struct hash_codes_info hashinf;
fdc90cb4
JJ
7328 bfd_size_type amt;
7329 unsigned long int nsyms;
7330 size_t bucketcount;
7331 size_t hash_entry_size;
7332
7333 /* Compute the hash values for all exported symbols. At the same
7334 time store the values in an array so that we could use them for
7335 optimizations. */
7336 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7337 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7338 if (hashcodes == NULL)
7339 return FALSE;
14b1c01e
AM
7340 hashinf.hashcodes = hashcodes;
7341 hashinf.error = FALSE;
5a580b3a 7342
fdc90cb4
JJ
7343 /* Put all hash values in HASHCODES. */
7344 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7345 elf_collect_hash_codes, &hashinf);
7346 if (hashinf.error)
4dd07732
AM
7347 {
7348 free (hashcodes);
7349 return FALSE;
7350 }
5a580b3a 7351
14b1c01e 7352 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7353 bucketcount
7354 = compute_bucket_count (info, hashcodes, nsyms, 0);
7355 free (hashcodes);
7356
4b48e2f6 7357 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7358 return FALSE;
5a580b3a 7359
fdc90cb4
JJ
7360 elf_hash_table (info)->bucketcount = bucketcount;
7361
3d4d4302 7362 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7363 BFD_ASSERT (s != NULL);
7364 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7365 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7366 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7367 if (s->contents == NULL)
7368 return FALSE;
7369
7370 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7371 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7372 s->contents + hash_entry_size);
7373 }
7374
7375 if (info->emit_gnu_hash)
7376 {
7377 size_t i, cnt;
7378 unsigned char *contents;
7379 struct collect_gnu_hash_codes cinfo;
7380 bfd_size_type amt;
7381 size_t bucketcount;
7382
7383 memset (&cinfo, 0, sizeof (cinfo));
7384
7385 /* Compute the hash values for all exported symbols. At the same
7386 time store the values in an array so that we could use them for
7387 optimizations. */
7388 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7389 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7390 if (cinfo.hashcodes == NULL)
7391 return FALSE;
7392
7393 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7394 cinfo.min_dynindx = -1;
7395 cinfo.output_bfd = output_bfd;
7396 cinfo.bed = bed;
7397
7398 /* Put all hash values in HASHCODES. */
7399 elf_link_hash_traverse (elf_hash_table (info),
7400 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7401 if (cinfo.error)
4dd07732
AM
7402 {
7403 free (cinfo.hashcodes);
7404 return FALSE;
7405 }
fdc90cb4
JJ
7406
7407 bucketcount
7408 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7409
7410 if (bucketcount == 0)
7411 {
7412 free (cinfo.hashcodes);
7413 return FALSE;
7414 }
7415
f16a9783 7416 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7417 BFD_ASSERT (s != NULL);
7418
7419 if (cinfo.nsyms == 0)
7420 {
f16a9783 7421 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7422 BFD_ASSERT (cinfo.min_dynindx == -1);
7423 free (cinfo.hashcodes);
7424 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7425 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7426 if (contents == NULL)
7427 return FALSE;
7428 s->contents = contents;
7429 /* 1 empty bucket. */
7430 bfd_put_32 (output_bfd, 1, contents);
7431 /* SYMIDX above the special symbol 0. */
7432 bfd_put_32 (output_bfd, 1, contents + 4);
7433 /* Just one word for bitmask. */
7434 bfd_put_32 (output_bfd, 1, contents + 8);
7435 /* Only hash fn bloom filter. */
7436 bfd_put_32 (output_bfd, 0, contents + 12);
7437 /* No hashes are valid - empty bitmask. */
7438 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7439 /* No hashes in the only bucket. */
7440 bfd_put_32 (output_bfd, 0,
7441 contents + 16 + bed->s->arch_size / 8);
7442 }
7443 else
7444 {
9e6619e2 7445 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7446 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7447
9e6619e2
AM
7448 x = cinfo.nsyms;
7449 maskbitslog2 = 1;
7450 while ((x >>= 1) != 0)
7451 ++maskbitslog2;
fdc90cb4
JJ
7452 if (maskbitslog2 < 3)
7453 maskbitslog2 = 5;
7454 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7455 maskbitslog2 = maskbitslog2 + 3;
7456 else
7457 maskbitslog2 = maskbitslog2 + 2;
7458 if (bed->s->arch_size == 64)
7459 {
7460 if (maskbitslog2 == 5)
7461 maskbitslog2 = 6;
7462 cinfo.shift1 = 6;
7463 }
7464 else
7465 cinfo.shift1 = 5;
7466 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7467 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7468 cinfo.maskbits = 1 << maskbitslog2;
7469 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7470 amt = bucketcount * sizeof (unsigned long int) * 2;
7471 amt += maskwords * sizeof (bfd_vma);
a50b1753 7472 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7473 if (cinfo.bitmask == NULL)
7474 {
7475 free (cinfo.hashcodes);
7476 return FALSE;
7477 }
7478
a50b1753 7479 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7480 cinfo.indx = cinfo.counts + bucketcount;
7481 cinfo.symindx = dynsymcount - cinfo.nsyms;
7482 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7483
7484 /* Determine how often each hash bucket is used. */
7485 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7486 for (i = 0; i < cinfo.nsyms; ++i)
7487 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7488
7489 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7490 if (cinfo.counts[i] != 0)
7491 {
7492 cinfo.indx[i] = cnt;
7493 cnt += cinfo.counts[i];
7494 }
7495 BFD_ASSERT (cnt == dynsymcount);
7496 cinfo.bucketcount = bucketcount;
7497 cinfo.local_indx = cinfo.min_dynindx;
7498
7499 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7500 s->size += cinfo.maskbits / 8;
f16a9783
MS
7501 if (bed->record_xhash_symbol != NULL)
7502 s->size += cinfo.nsyms * 4;
a50b1753 7503 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7504 if (contents == NULL)
7505 {
7506 free (cinfo.bitmask);
7507 free (cinfo.hashcodes);
7508 return FALSE;
7509 }
7510
7511 s->contents = contents;
7512 bfd_put_32 (output_bfd, bucketcount, contents);
7513 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7514 bfd_put_32 (output_bfd, maskwords, contents + 8);
7515 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7516 contents += 16 + cinfo.maskbits / 8;
7517
7518 for (i = 0; i < bucketcount; ++i)
7519 {
7520 if (cinfo.counts[i] == 0)
7521 bfd_put_32 (output_bfd, 0, contents);
7522 else
7523 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7524 contents += 4;
7525 }
7526
7527 cinfo.contents = contents;
7528
f16a9783
MS
7529 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7530 /* Renumber dynamic symbols, if populating .gnu.hash section.
7531 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7532 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7533 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7534
7535 contents = s->contents + 16;
7536 for (i = 0; i < maskwords; ++i)
7537 {
7538 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7539 contents);
7540 contents += bed->s->arch_size / 8;
7541 }
7542
7543 free (cinfo.bitmask);
7544 free (cinfo.hashcodes);
7545 }
7546 }
5a580b3a 7547
3d4d4302 7548 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7549 BFD_ASSERT (s != NULL);
7550
4ad4eba5 7551 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7552
eea6121a 7553 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7554
7555 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7556 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7557 return FALSE;
7558 }
7559
7560 return TRUE;
7561}
4d269e42 7562\f
4d269e42
AM
7563/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7564
7565static void
7566merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7567 asection *sec)
7568{
dbaa2011
AM
7569 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7570 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7571}
7572
7573/* Finish SHF_MERGE section merging. */
7574
7575bfd_boolean
630993ec 7576_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7577{
7578 bfd *ibfd;
7579 asection *sec;
7580
7581 if (!is_elf_hash_table (info->hash))
7582 return FALSE;
7583
c72f2fb2 7584 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7585 if ((ibfd->flags & DYNAMIC) == 0
7586 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7587 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7588 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7589 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7590 if ((sec->flags & SEC_MERGE) != 0
7591 && !bfd_is_abs_section (sec->output_section))
7592 {
7593 struct bfd_elf_section_data *secdata;
7594
7595 secdata = elf_section_data (sec);
630993ec 7596 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7597 &elf_hash_table (info)->merge_info,
7598 sec, &secdata->sec_info))
7599 return FALSE;
7600 else if (secdata->sec_info)
dbaa2011 7601 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7602 }
7603
7604 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7605 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7606 merge_sections_remove_hook);
7607 return TRUE;
7608}
7609
7610/* Create an entry in an ELF linker hash table. */
7611
7612struct bfd_hash_entry *
7613_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7614 struct bfd_hash_table *table,
7615 const char *string)
7616{
7617 /* Allocate the structure if it has not already been allocated by a
7618 subclass. */
7619 if (entry == NULL)
7620 {
a50b1753 7621 entry = (struct bfd_hash_entry *)
ca4be51c 7622 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7623 if (entry == NULL)
7624 return entry;
7625 }
7626
7627 /* Call the allocation method of the superclass. */
7628 entry = _bfd_link_hash_newfunc (entry, table, string);
7629 if (entry != NULL)
7630 {
7631 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7632 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7633
7634 /* Set local fields. */
7635 ret->indx = -1;
7636 ret->dynindx = -1;
7637 ret->got = htab->init_got_refcount;
7638 ret->plt = htab->init_plt_refcount;
7639 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7640 - offsetof (struct elf_link_hash_entry, size)));
7641 /* Assume that we have been called by a non-ELF symbol reader.
7642 This flag is then reset by the code which reads an ELF input
7643 file. This ensures that a symbol created by a non-ELF symbol
7644 reader will have the flag set correctly. */
7645 ret->non_elf = 1;
7646 }
7647
7648 return entry;
7649}
7650
7651/* Copy data from an indirect symbol to its direct symbol, hiding the
7652 old indirect symbol. Also used for copying flags to a weakdef. */
7653
7654void
7655_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7656 struct elf_link_hash_entry *dir,
7657 struct elf_link_hash_entry *ind)
7658{
7659 struct elf_link_hash_table *htab;
7660
ad172eaa
L
7661 if (ind->dyn_relocs != NULL)
7662 {
7663 if (dir->dyn_relocs != NULL)
7664 {
7665 struct elf_dyn_relocs **pp;
7666 struct elf_dyn_relocs *p;
7667
7668 /* Add reloc counts against the indirect sym to the direct sym
7669 list. Merge any entries against the same section. */
7670 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7671 {
7672 struct elf_dyn_relocs *q;
7673
7674 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7675 if (q->sec == p->sec)
7676 {
7677 q->pc_count += p->pc_count;
7678 q->count += p->count;
7679 *pp = p->next;
7680 break;
7681 }
7682 if (q == NULL)
7683 pp = &p->next;
7684 }
7685 *pp = dir->dyn_relocs;
7686 }
7687
7688 dir->dyn_relocs = ind->dyn_relocs;
7689 ind->dyn_relocs = NULL;
7690 }
7691
4d269e42 7692 /* Copy down any references that we may have already seen to the
e81830c5 7693 symbol which just became indirect. */
4d269e42 7694
422f1182 7695 if (dir->versioned != versioned_hidden)
e81830c5
AM
7696 dir->ref_dynamic |= ind->ref_dynamic;
7697 dir->ref_regular |= ind->ref_regular;
7698 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7699 dir->non_got_ref |= ind->non_got_ref;
7700 dir->needs_plt |= ind->needs_plt;
7701 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7702
7703 if (ind->root.type != bfd_link_hash_indirect)
7704 return;
7705
7706 /* Copy over the global and procedure linkage table refcount entries.
7707 These may have been already set up by a check_relocs routine. */
7708 htab = elf_hash_table (info);
7709 if (ind->got.refcount > htab->init_got_refcount.refcount)
7710 {
7711 if (dir->got.refcount < 0)
7712 dir->got.refcount = 0;
7713 dir->got.refcount += ind->got.refcount;
7714 ind->got.refcount = htab->init_got_refcount.refcount;
7715 }
7716
7717 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7718 {
7719 if (dir->plt.refcount < 0)
7720 dir->plt.refcount = 0;
7721 dir->plt.refcount += ind->plt.refcount;
7722 ind->plt.refcount = htab->init_plt_refcount.refcount;
7723 }
7724
7725 if (ind->dynindx != -1)
7726 {
7727 if (dir->dynindx != -1)
7728 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7729 dir->dynindx = ind->dynindx;
7730 dir->dynstr_index = ind->dynstr_index;
7731 ind->dynindx = -1;
7732 ind->dynstr_index = 0;
7733 }
7734}
7735
7736void
7737_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7738 struct elf_link_hash_entry *h,
7739 bfd_boolean force_local)
7740{
3aa14d16
L
7741 /* STT_GNU_IFUNC symbol must go through PLT. */
7742 if (h->type != STT_GNU_IFUNC)
7743 {
7744 h->plt = elf_hash_table (info)->init_plt_offset;
7745 h->needs_plt = 0;
7746 }
4d269e42
AM
7747 if (force_local)
7748 {
7749 h->forced_local = 1;
7750 if (h->dynindx != -1)
7751 {
4d269e42
AM
7752 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7753 h->dynstr_index);
641338d8
AM
7754 h->dynindx = -1;
7755 h->dynstr_index = 0;
4d269e42
AM
7756 }
7757 }
7758}
7759
34a87bb0
L
7760/* Hide a symbol. */
7761
7762void
7763_bfd_elf_link_hide_symbol (bfd *output_bfd,
7764 struct bfd_link_info *info,
7765 struct bfd_link_hash_entry *h)
7766{
7767 if (is_elf_hash_table (info->hash))
7768 {
7769 const struct elf_backend_data *bed
7770 = get_elf_backend_data (output_bfd);
7771 struct elf_link_hash_entry *eh
7772 = (struct elf_link_hash_entry *) h;
7773 bed->elf_backend_hide_symbol (info, eh, TRUE);
7774 eh->def_dynamic = 0;
7775 eh->ref_dynamic = 0;
7776 eh->dynamic_def = 0;
7777 }
7778}
7779
7bf52ea2
AM
7780/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7781 caller. */
4d269e42
AM
7782
7783bfd_boolean
7784_bfd_elf_link_hash_table_init
7785 (struct elf_link_hash_table *table,
7786 bfd *abfd,
7787 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7788 struct bfd_hash_table *,
7789 const char *),
4dfe6ac6
NC
7790 unsigned int entsize,
7791 enum elf_target_id target_id)
4d269e42
AM
7792{
7793 bfd_boolean ret;
7794 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7795
4d269e42
AM
7796 table->init_got_refcount.refcount = can_refcount - 1;
7797 table->init_plt_refcount.refcount = can_refcount - 1;
7798 table->init_got_offset.offset = -(bfd_vma) 1;
7799 table->init_plt_offset.offset = -(bfd_vma) 1;
7800 /* The first dynamic symbol is a dummy. */
7801 table->dynsymcount = 1;
7802
7803 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7804
4d269e42 7805 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7806 table->hash_table_id = target_id;
90c14f0c 7807 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7808
7809 return ret;
7810}
7811
7812/* Create an ELF linker hash table. */
7813
7814struct bfd_link_hash_table *
7815_bfd_elf_link_hash_table_create (bfd *abfd)
7816{
7817 struct elf_link_hash_table *ret;
986f0783 7818 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7819
7bf52ea2 7820 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7821 if (ret == NULL)
7822 return NULL;
7823
7824 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7825 sizeof (struct elf_link_hash_entry),
7826 GENERIC_ELF_DATA))
4d269e42
AM
7827 {
7828 free (ret);
7829 return NULL;
7830 }
d495ab0d 7831 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7832
7833 return &ret->root;
7834}
7835
9f7c3e5e
AM
7836/* Destroy an ELF linker hash table. */
7837
7838void
d495ab0d 7839_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7840{
d495ab0d
AM
7841 struct elf_link_hash_table *htab;
7842
7843 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7844 if (htab->dynstr != NULL)
7845 _bfd_elf_strtab_free (htab->dynstr);
7846 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7847 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7848}
7849
4d269e42
AM
7850/* This is a hook for the ELF emulation code in the generic linker to
7851 tell the backend linker what file name to use for the DT_NEEDED
7852 entry for a dynamic object. */
7853
7854void
7855bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7856{
7857 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7858 && bfd_get_format (abfd) == bfd_object)
7859 elf_dt_name (abfd) = name;
7860}
7861
7862int
7863bfd_elf_get_dyn_lib_class (bfd *abfd)
7864{
7865 int lib_class;
7866 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7867 && bfd_get_format (abfd) == bfd_object)
7868 lib_class = elf_dyn_lib_class (abfd);
7869 else
7870 lib_class = 0;
7871 return lib_class;
7872}
7873
7874void
7875bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7876{
7877 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7878 && bfd_get_format (abfd) == bfd_object)
7879 elf_dyn_lib_class (abfd) = lib_class;
7880}
7881
7882/* Get the list of DT_NEEDED entries for a link. This is a hook for
7883 the linker ELF emulation code. */
7884
7885struct bfd_link_needed_list *
7886bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7887 struct bfd_link_info *info)
7888{
7889 if (! is_elf_hash_table (info->hash))
7890 return NULL;
7891 return elf_hash_table (info)->needed;
7892}
7893
7894/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7895 hook for the linker ELF emulation code. */
7896
7897struct bfd_link_needed_list *
7898bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7899 struct bfd_link_info *info)
7900{
7901 if (! is_elf_hash_table (info->hash))
7902 return NULL;
7903 return elf_hash_table (info)->runpath;
7904}
7905
7906/* Get the name actually used for a dynamic object for a link. This
7907 is the SONAME entry if there is one. Otherwise, it is the string
7908 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7909
7910const char *
7911bfd_elf_get_dt_soname (bfd *abfd)
7912{
7913 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7914 && bfd_get_format (abfd) == bfd_object)
7915 return elf_dt_name (abfd);
7916 return NULL;
7917}
7918
7919/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7920 the ELF linker emulation code. */
7921
7922bfd_boolean
7923bfd_elf_get_bfd_needed_list (bfd *abfd,
7924 struct bfd_link_needed_list **pneeded)
7925{
7926 asection *s;
7927 bfd_byte *dynbuf = NULL;
cb33740c 7928 unsigned int elfsec;
4d269e42
AM
7929 unsigned long shlink;
7930 bfd_byte *extdyn, *extdynend;
7931 size_t extdynsize;
7932 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7933
7934 *pneeded = NULL;
7935
7936 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7937 || bfd_get_format (abfd) != bfd_object)
7938 return TRUE;
7939
7940 s = bfd_get_section_by_name (abfd, ".dynamic");
7941 if (s == NULL || s->size == 0)
7942 return TRUE;
7943
7944 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7945 goto error_return;
7946
7947 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7948 if (elfsec == SHN_BAD)
4d269e42
AM
7949 goto error_return;
7950
7951 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7952
4d269e42
AM
7953 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7954 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7955
7956 extdyn = dynbuf;
7957 extdynend = extdyn + s->size;
7958 for (; extdyn < extdynend; extdyn += extdynsize)
7959 {
7960 Elf_Internal_Dyn dyn;
7961
7962 (*swap_dyn_in) (abfd, extdyn, &dyn);
7963
7964 if (dyn.d_tag == DT_NULL)
7965 break;
7966
7967 if (dyn.d_tag == DT_NEEDED)
7968 {
7969 const char *string;
7970 struct bfd_link_needed_list *l;
7971 unsigned int tagv = dyn.d_un.d_val;
986f0783 7972 size_t amt;
4d269e42
AM
7973
7974 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7975 if (string == NULL)
7976 goto error_return;
7977
7978 amt = sizeof *l;
a50b1753 7979 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7980 if (l == NULL)
7981 goto error_return;
7982
7983 l->by = abfd;
7984 l->name = string;
7985 l->next = *pneeded;
7986 *pneeded = l;
7987 }
7988 }
7989
7990 free (dynbuf);
7991
7992 return TRUE;
7993
7994 error_return:
c9594989 7995 free (dynbuf);
4d269e42
AM
7996 return FALSE;
7997}
7998
7999struct elf_symbuf_symbol
8000{
8001 unsigned long st_name; /* Symbol name, index in string tbl */
8002 unsigned char st_info; /* Type and binding attributes */
8003 unsigned char st_other; /* Visibilty, and target specific */
8004};
8005
8006struct elf_symbuf_head
8007{
8008 struct elf_symbuf_symbol *ssym;
ef53be89 8009 size_t count;
4d269e42
AM
8010 unsigned int st_shndx;
8011};
8012
8013struct elf_symbol
8014{
8015 union
8016 {
8017 Elf_Internal_Sym *isym;
8018 struct elf_symbuf_symbol *ssym;
dcea6a95 8019 void *p;
4d269e42
AM
8020 } u;
8021 const char *name;
8022};
8023
8024/* Sort references to symbols by ascending section number. */
8025
8026static int
8027elf_sort_elf_symbol (const void *arg1, const void *arg2)
8028{
8029 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8030 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8031
dcea6a95
AM
8032 if (s1->st_shndx != s2->st_shndx)
8033 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8034 /* Final sort by the address of the sym in the symbuf ensures
8035 a stable sort. */
8036 if (s1 != s2)
8037 return s1 > s2 ? 1 : -1;
8038 return 0;
4d269e42
AM
8039}
8040
8041static int
8042elf_sym_name_compare (const void *arg1, const void *arg2)
8043{
8044 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8045 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8046 int ret = strcmp (s1->name, s2->name);
8047 if (ret != 0)
8048 return ret;
8049 if (s1->u.p != s2->u.p)
8050 return s1->u.p > s2->u.p ? 1 : -1;
8051 return 0;
4d269e42
AM
8052}
8053
8054static struct elf_symbuf_head *
ef53be89 8055elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8056{
14b1c01e 8057 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8058 struct elf_symbuf_symbol *ssym;
8059 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8060 size_t i, shndx_count, total_size, amt;
4d269e42 8061
446f7ed5
AM
8062 amt = symcount * sizeof (*indbuf);
8063 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8064 if (indbuf == NULL)
8065 return NULL;
8066
8067 for (ind = indbuf, i = 0; i < symcount; i++)
8068 if (isymbuf[i].st_shndx != SHN_UNDEF)
8069 *ind++ = &isymbuf[i];
8070 indbufend = ind;
8071
8072 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8073 elf_sort_elf_symbol);
8074
8075 shndx_count = 0;
8076 if (indbufend > indbuf)
8077 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8078 if (ind[0]->st_shndx != ind[1]->st_shndx)
8079 shndx_count++;
8080
3ae181ee
L
8081 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8082 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8083 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8084 if (ssymbuf == NULL)
8085 {
8086 free (indbuf);
8087 return NULL;
8088 }
8089
3ae181ee 8090 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8091 ssymbuf->ssym = NULL;
8092 ssymbuf->count = shndx_count;
8093 ssymbuf->st_shndx = 0;
8094 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8095 {
8096 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8097 {
8098 ssymhead++;
8099 ssymhead->ssym = ssym;
8100 ssymhead->count = 0;
8101 ssymhead->st_shndx = (*ind)->st_shndx;
8102 }
8103 ssym->st_name = (*ind)->st_name;
8104 ssym->st_info = (*ind)->st_info;
8105 ssym->st_other = (*ind)->st_other;
8106 ssymhead->count++;
8107 }
ef53be89 8108 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8109 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8110 == total_size));
4d269e42
AM
8111
8112 free (indbuf);
8113 return ssymbuf;
8114}
8115
8116/* Check if 2 sections define the same set of local and global
8117 symbols. */
8118
8f317e31 8119static bfd_boolean
4d269e42
AM
8120bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8121 struct bfd_link_info *info)
8122{
8123 bfd *bfd1, *bfd2;
8124 const struct elf_backend_data *bed1, *bed2;
8125 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8126 size_t symcount1, symcount2;
4d269e42
AM
8127 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8128 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8129 Elf_Internal_Sym *isym, *isymend;
8130 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8131 size_t count1, count2, i;
cb33740c 8132 unsigned int shndx1, shndx2;
4d269e42
AM
8133 bfd_boolean result;
8134
8135 bfd1 = sec1->owner;
8136 bfd2 = sec2->owner;
8137
4d269e42
AM
8138 /* Both sections have to be in ELF. */
8139 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8140 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8141 return FALSE;
8142
8143 if (elf_section_type (sec1) != elf_section_type (sec2))
8144 return FALSE;
8145
4d269e42
AM
8146 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8147 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8148 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8149 return FALSE;
8150
8151 bed1 = get_elf_backend_data (bfd1);
8152 bed2 = get_elf_backend_data (bfd2);
8153 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8154 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8155 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8156 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8157
8158 if (symcount1 == 0 || symcount2 == 0)
8159 return FALSE;
8160
8161 result = FALSE;
8162 isymbuf1 = NULL;
8163 isymbuf2 = NULL;
a50b1753
NC
8164 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8165 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8166
8167 if (ssymbuf1 == NULL)
8168 {
8169 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8170 NULL, NULL, NULL);
8171 if (isymbuf1 == NULL)
8172 goto done;
8173
8174 if (!info->reduce_memory_overheads)
dcea6a95
AM
8175 {
8176 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8177 elf_tdata (bfd1)->symbuf = ssymbuf1;
8178 }
4d269e42
AM
8179 }
8180
8181 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8182 {
8183 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8184 NULL, NULL, NULL);
8185 if (isymbuf2 == NULL)
8186 goto done;
8187
8188 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8189 {
8190 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8191 elf_tdata (bfd2)->symbuf = ssymbuf2;
8192 }
4d269e42
AM
8193 }
8194
8195 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8196 {
8197 /* Optimized faster version. */
ef53be89 8198 size_t lo, hi, mid;
4d269e42
AM
8199 struct elf_symbol *symp;
8200 struct elf_symbuf_symbol *ssym, *ssymend;
8201
8202 lo = 0;
8203 hi = ssymbuf1->count;
8204 ssymbuf1++;
8205 count1 = 0;
8206 while (lo < hi)
8207 {
8208 mid = (lo + hi) / 2;
cb33740c 8209 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8210 hi = mid;
cb33740c 8211 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8212 lo = mid + 1;
8213 else
8214 {
8215 count1 = ssymbuf1[mid].count;
8216 ssymbuf1 += mid;
8217 break;
8218 }
8219 }
8220
8221 lo = 0;
8222 hi = ssymbuf2->count;
8223 ssymbuf2++;
8224 count2 = 0;
8225 while (lo < hi)
8226 {
8227 mid = (lo + hi) / 2;
cb33740c 8228 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8229 hi = mid;
cb33740c 8230 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8231 lo = mid + 1;
8232 else
8233 {
8234 count2 = ssymbuf2[mid].count;
8235 ssymbuf2 += mid;
8236 break;
8237 }
8238 }
8239
8240 if (count1 == 0 || count2 == 0 || count1 != count2)
8241 goto done;
8242
ca4be51c
AM
8243 symtable1
8244 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8245 symtable2
8246 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8247 if (symtable1 == NULL || symtable2 == NULL)
8248 goto done;
8249
8250 symp = symtable1;
8251 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8252 ssym < ssymend; ssym++, symp++)
8253 {
8254 symp->u.ssym = ssym;
8255 symp->name = bfd_elf_string_from_elf_section (bfd1,
8256 hdr1->sh_link,
8257 ssym->st_name);
8258 }
8259
8260 symp = symtable2;
8261 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8262 ssym < ssymend; ssym++, symp++)
8263 {
8264 symp->u.ssym = ssym;
8265 symp->name = bfd_elf_string_from_elf_section (bfd2,
8266 hdr2->sh_link,
8267 ssym->st_name);
8268 }
8269
8270 /* Sort symbol by name. */
8271 qsort (symtable1, count1, sizeof (struct elf_symbol),
8272 elf_sym_name_compare);
8273 qsort (symtable2, count1, sizeof (struct elf_symbol),
8274 elf_sym_name_compare);
8275
8276 for (i = 0; i < count1; i++)
8277 /* Two symbols must have the same binding, type and name. */
8278 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8279 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8280 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8281 goto done;
8282
8283 result = TRUE;
8284 goto done;
8285 }
8286
a50b1753
NC
8287 symtable1 = (struct elf_symbol *)
8288 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8289 symtable2 = (struct elf_symbol *)
8290 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8291 if (symtable1 == NULL || symtable2 == NULL)
8292 goto done;
8293
8294 /* Count definitions in the section. */
8295 count1 = 0;
8296 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8297 if (isym->st_shndx == shndx1)
4d269e42
AM
8298 symtable1[count1++].u.isym = isym;
8299
8300 count2 = 0;
8301 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8302 if (isym->st_shndx == shndx2)
4d269e42
AM
8303 symtable2[count2++].u.isym = isym;
8304
8305 if (count1 == 0 || count2 == 0 || count1 != count2)
8306 goto done;
8307
8308 for (i = 0; i < count1; i++)
8309 symtable1[i].name
8310 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8311 symtable1[i].u.isym->st_name);
8312
8313 for (i = 0; i < count2; i++)
8314 symtable2[i].name
8315 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8316 symtable2[i].u.isym->st_name);
8317
8318 /* Sort symbol by name. */
8319 qsort (symtable1, count1, sizeof (struct elf_symbol),
8320 elf_sym_name_compare);
8321 qsort (symtable2, count1, sizeof (struct elf_symbol),
8322 elf_sym_name_compare);
8323
8324 for (i = 0; i < count1; i++)
8325 /* Two symbols must have the same binding, type and name. */
8326 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8327 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8328 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8329 goto done;
8330
8331 result = TRUE;
8332
dc1e8a47 8333 done:
c9594989
AM
8334 free (symtable1);
8335 free (symtable2);
8336 free (isymbuf1);
8337 free (isymbuf2);
4d269e42
AM
8338
8339 return result;
8340}
8341
8342/* Return TRUE if 2 section types are compatible. */
8343
8344bfd_boolean
8345_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8346 bfd *bbfd, const asection *bsec)
8347{
8348 if (asec == NULL
8349 || bsec == NULL
8350 || abfd->xvec->flavour != bfd_target_elf_flavour
8351 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8352 return TRUE;
8353
8354 return elf_section_type (asec) == elf_section_type (bsec);
8355}
8356\f
c152c796
AM
8357/* Final phase of ELF linker. */
8358
8359/* A structure we use to avoid passing large numbers of arguments. */
8360
8361struct elf_final_link_info
8362{
8363 /* General link information. */
8364 struct bfd_link_info *info;
8365 /* Output BFD. */
8366 bfd *output_bfd;
8367 /* Symbol string table. */
ef10c3ac 8368 struct elf_strtab_hash *symstrtab;
c152c796
AM
8369 /* .hash section. */
8370 asection *hash_sec;
8371 /* symbol version section (.gnu.version). */
8372 asection *symver_sec;
8373 /* Buffer large enough to hold contents of any section. */
8374 bfd_byte *contents;
8375 /* Buffer large enough to hold external relocs of any section. */
8376 void *external_relocs;
8377 /* Buffer large enough to hold internal relocs of any section. */
8378 Elf_Internal_Rela *internal_relocs;
8379 /* Buffer large enough to hold external local symbols of any input
8380 BFD. */
8381 bfd_byte *external_syms;
8382 /* And a buffer for symbol section indices. */
8383 Elf_External_Sym_Shndx *locsym_shndx;
8384 /* Buffer large enough to hold internal local symbols of any input
8385 BFD. */
8386 Elf_Internal_Sym *internal_syms;
8387 /* Array large enough to hold a symbol index for each local symbol
8388 of any input BFD. */
8389 long *indices;
8390 /* Array large enough to hold a section pointer for each local
8391 symbol of any input BFD. */
8392 asection **sections;
ef10c3ac 8393 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8394 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8395 /* Number of STT_FILE syms seen. */
8396 size_t filesym_count;
c152c796
AM
8397};
8398
8399/* This struct is used to pass information to elf_link_output_extsym. */
8400
8401struct elf_outext_info
8402{
8403 bfd_boolean failed;
8404 bfd_boolean localsyms;
34a79995 8405 bfd_boolean file_sym_done;
8b127cbc 8406 struct elf_final_link_info *flinfo;
c152c796
AM
8407};
8408
d9352518
DB
8409
8410/* Support for evaluating a complex relocation.
8411
8412 Complex relocations are generalized, self-describing relocations. The
8413 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8414 relocations themselves.
d9352518
DB
8415
8416 The relocations are use a reserved elf-wide relocation type code (R_RELC
8417 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8418 information (start bit, end bit, word width, etc) into the addend. This
8419 information is extracted from CGEN-generated operand tables within gas.
8420
8421 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8422 internal) representing prefix-notation expressions, including but not
8423 limited to those sorts of expressions normally encoded as addends in the
8424 addend field. The symbol mangling format is:
8425
8426 <node> := <literal>
07d6d2b8
AM
8427 | <unary-operator> ':' <node>
8428 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8429 ;
8430
8431 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8432 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8433 | '#' <hexdigits>
8434 ;
8435
8436 <binary-operator> := as in C
8437 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8438
8439static void
a0c8462f
AM
8440set_symbol_value (bfd *bfd_with_globals,
8441 Elf_Internal_Sym *isymbuf,
8442 size_t locsymcount,
8443 size_t symidx,
8444 bfd_vma val)
d9352518 8445{
8977835c
AM
8446 struct elf_link_hash_entry **sym_hashes;
8447 struct elf_link_hash_entry *h;
8448 size_t extsymoff = locsymcount;
d9352518 8449
8977835c 8450 if (symidx < locsymcount)
d9352518 8451 {
8977835c
AM
8452 Elf_Internal_Sym *sym;
8453
8454 sym = isymbuf + symidx;
8455 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8456 {
8457 /* It is a local symbol: move it to the
8458 "absolute" section and give it a value. */
8459 sym->st_shndx = SHN_ABS;
8460 sym->st_value = val;
8461 return;
8462 }
8463 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8464 extsymoff = 0;
d9352518 8465 }
8977835c
AM
8466
8467 /* It is a global symbol: set its link type
8468 to "defined" and give it a value. */
8469
8470 sym_hashes = elf_sym_hashes (bfd_with_globals);
8471 h = sym_hashes [symidx - extsymoff];
8472 while (h->root.type == bfd_link_hash_indirect
8473 || h->root.type == bfd_link_hash_warning)
8474 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8475 h->root.type = bfd_link_hash_defined;
8476 h->root.u.def.value = val;
8477 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8478}
8479
a0c8462f
AM
8480static bfd_boolean
8481resolve_symbol (const char *name,
8482 bfd *input_bfd,
8b127cbc 8483 struct elf_final_link_info *flinfo,
a0c8462f
AM
8484 bfd_vma *result,
8485 Elf_Internal_Sym *isymbuf,
8486 size_t locsymcount)
d9352518 8487{
a0c8462f
AM
8488 Elf_Internal_Sym *sym;
8489 struct bfd_link_hash_entry *global_entry;
8490 const char *candidate = NULL;
8491 Elf_Internal_Shdr *symtab_hdr;
8492 size_t i;
8493
d9352518
DB
8494 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8495
8496 for (i = 0; i < locsymcount; ++ i)
8497 {
8977835c 8498 sym = isymbuf + i;
d9352518
DB
8499
8500 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8501 continue;
8502
8503 candidate = bfd_elf_string_from_elf_section (input_bfd,
8504 symtab_hdr->sh_link,
8505 sym->st_name);
8506#ifdef DEBUG
0f02bbd9
AM
8507 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8508 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8509#endif
8510 if (candidate && strcmp (candidate, name) == 0)
8511 {
8b127cbc 8512 asection *sec = flinfo->sections [i];
d9352518 8513
0f02bbd9
AM
8514 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8515 *result += sec->output_offset + sec->output_section->vma;
d9352518 8516#ifdef DEBUG
0f02bbd9
AM
8517 printf ("Found symbol with value %8.8lx\n",
8518 (unsigned long) *result);
d9352518
DB
8519#endif
8520 return TRUE;
8521 }
8522 }
8523
8524 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8525 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8526 FALSE, FALSE, TRUE);
d9352518
DB
8527 if (!global_entry)
8528 return FALSE;
a0c8462f 8529
d9352518
DB
8530 if (global_entry->type == bfd_link_hash_defined
8531 || global_entry->type == bfd_link_hash_defweak)
8532 {
a0c8462f
AM
8533 *result = (global_entry->u.def.value
8534 + global_entry->u.def.section->output_section->vma
8535 + global_entry->u.def.section->output_offset);
d9352518 8536#ifdef DEBUG
0f02bbd9
AM
8537 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8538 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8539#endif
8540 return TRUE;
a0c8462f 8541 }
d9352518 8542
d9352518
DB
8543 return FALSE;
8544}
8545
37b01f6a
DG
8546/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8547 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8548 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8549
d9352518 8550static bfd_boolean
a0c8462f
AM
8551resolve_section (const char *name,
8552 asection *sections,
37b01f6a
DG
8553 bfd_vma *result,
8554 bfd * abfd)
d9352518 8555{
a0c8462f
AM
8556 asection *curr;
8557 unsigned int len;
d9352518 8558
a0c8462f 8559 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8560 if (strcmp (curr->name, name) == 0)
8561 {
8562 *result = curr->vma;
8563 return TRUE;
8564 }
8565
8566 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8567 /* FIXME: This could be coded more efficiently... */
a0c8462f 8568 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8569 {
8570 len = strlen (curr->name);
a0c8462f 8571 if (len > strlen (name))
d9352518
DB
8572 continue;
8573
8574 if (strncmp (curr->name, name, len) == 0)
8575 {
8576 if (strncmp (".end", name + len, 4) == 0)
8577 {
61826503 8578 *result = (curr->vma
bb294208 8579 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8580 return TRUE;
8581 }
8582
8583 /* Insert more pseudo-section names here, if you like. */
8584 }
8585 }
a0c8462f 8586
d9352518
DB
8587 return FALSE;
8588}
8589
8590static void
a0c8462f 8591undefined_reference (const char *reftype, const char *name)
d9352518 8592{
695344c0 8593 /* xgettext:c-format */
a0c8462f
AM
8594 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8595 reftype, name);
d9352518
DB
8596}
8597
8598static bfd_boolean
a0c8462f
AM
8599eval_symbol (bfd_vma *result,
8600 const char **symp,
8601 bfd *input_bfd,
8b127cbc 8602 struct elf_final_link_info *flinfo,
a0c8462f
AM
8603 bfd_vma dot,
8604 Elf_Internal_Sym *isymbuf,
8605 size_t locsymcount,
8606 int signed_p)
d9352518 8607{
4b93929b
NC
8608 size_t len;
8609 size_t symlen;
a0c8462f
AM
8610 bfd_vma a;
8611 bfd_vma b;
4b93929b 8612 char symbuf[4096];
0f02bbd9 8613 const char *sym = *symp;
a0c8462f
AM
8614 const char *symend;
8615 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8616
8617 len = strlen (sym);
8618 symend = sym + len;
8619
4b93929b 8620 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8621 {
8622 bfd_set_error (bfd_error_invalid_operation);
8623 return FALSE;
8624 }
a0c8462f 8625
d9352518
DB
8626 switch (* sym)
8627 {
8628 case '.':
0f02bbd9
AM
8629 *result = dot;
8630 *symp = sym + 1;
d9352518
DB
8631 return TRUE;
8632
8633 case '#':
0f02bbd9
AM
8634 ++sym;
8635 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8636 return TRUE;
8637
8638 case 'S':
8639 symbol_is_section = TRUE;
1a0670f3 8640 /* Fall through. */
a0c8462f 8641 case 's':
0f02bbd9
AM
8642 ++sym;
8643 symlen = strtol (sym, (char **) symp, 10);
8644 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8645
4b93929b 8646 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8647 {
8648 bfd_set_error (bfd_error_invalid_operation);
8649 return FALSE;
8650 }
8651
8652 memcpy (symbuf, sym, symlen);
a0c8462f 8653 symbuf[symlen] = '\0';
0f02bbd9 8654 *symp = sym + symlen;
a0c8462f
AM
8655
8656 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8657 the symbol as a section, or vice-versa. so we're pretty liberal in our
8658 interpretation here; section means "try section first", not "must be a
8659 section", and likewise with symbol. */
8660
a0c8462f 8661 if (symbol_is_section)
d9352518 8662 {
37b01f6a 8663 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8664 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8665 isymbuf, locsymcount))
d9352518
DB
8666 {
8667 undefined_reference ("section", symbuf);
8668 return FALSE;
8669 }
a0c8462f
AM
8670 }
8671 else
d9352518 8672 {
8b127cbc 8673 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8674 isymbuf, locsymcount)
8b127cbc 8675 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8676 result, input_bfd))
d9352518
DB
8677 {
8678 undefined_reference ("symbol", symbuf);
8679 return FALSE;
8680 }
8681 }
8682
8683 return TRUE;
a0c8462f 8684
d9352518
DB
8685 /* All that remains are operators. */
8686
8687#define UNARY_OP(op) \
8688 if (strncmp (sym, #op, strlen (#op)) == 0) \
8689 { \
8690 sym += strlen (#op); \
a0c8462f
AM
8691 if (*sym == ':') \
8692 ++sym; \
0f02bbd9 8693 *symp = sym; \
8b127cbc 8694 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8695 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8696 return FALSE; \
8697 if (signed_p) \
0f02bbd9 8698 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8699 else \
8700 *result = op a; \
d9352518
DB
8701 return TRUE; \
8702 }
8703
8704#define BINARY_OP(op) \
8705 if (strncmp (sym, #op, strlen (#op)) == 0) \
8706 { \
8707 sym += strlen (#op); \
a0c8462f
AM
8708 if (*sym == ':') \
8709 ++sym; \
0f02bbd9 8710 *symp = sym; \
8b127cbc 8711 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8712 isymbuf, locsymcount, signed_p)) \
a0c8462f 8713 return FALSE; \
0f02bbd9 8714 ++*symp; \
8b127cbc 8715 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8716 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8717 return FALSE; \
8718 if (signed_p) \
0f02bbd9 8719 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8720 else \
8721 *result = a op b; \
d9352518
DB
8722 return TRUE; \
8723 }
8724
8725 default:
8726 UNARY_OP (0-);
8727 BINARY_OP (<<);
8728 BINARY_OP (>>);
8729 BINARY_OP (==);
8730 BINARY_OP (!=);
8731 BINARY_OP (<=);
8732 BINARY_OP (>=);
8733 BINARY_OP (&&);
8734 BINARY_OP (||);
8735 UNARY_OP (~);
8736 UNARY_OP (!);
8737 BINARY_OP (*);
8738 BINARY_OP (/);
8739 BINARY_OP (%);
8740 BINARY_OP (^);
8741 BINARY_OP (|);
8742 BINARY_OP (&);
8743 BINARY_OP (+);
8744 BINARY_OP (-);
8745 BINARY_OP (<);
8746 BINARY_OP (>);
8747#undef UNARY_OP
8748#undef BINARY_OP
8749 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8750 bfd_set_error (bfd_error_invalid_operation);
8751 return FALSE;
8752 }
8753}
8754
d9352518 8755static void
a0c8462f
AM
8756put_value (bfd_vma size,
8757 unsigned long chunksz,
8758 bfd *input_bfd,
8759 bfd_vma x,
8760 bfd_byte *location)
d9352518
DB
8761{
8762 location += (size - chunksz);
8763
41cd1ad1 8764 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8765 {
8766 switch (chunksz)
8767 {
d9352518
DB
8768 case 1:
8769 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8770 x >>= 8;
d9352518
DB
8771 break;
8772 case 2:
8773 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8774 x >>= 16;
d9352518
DB
8775 break;
8776 case 4:
8777 bfd_put_32 (input_bfd, x, location);
65164438
NC
8778 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8779 x >>= 16;
8780 x >>= 16;
d9352518 8781 break;
d9352518 8782#ifdef BFD64
41cd1ad1 8783 case 8:
d9352518 8784 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8785 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8786 x >>= 32;
8787 x >>= 32;
8788 break;
d9352518 8789#endif
41cd1ad1
NC
8790 default:
8791 abort ();
d9352518
DB
8792 break;
8793 }
8794 }
8795}
8796
a0c8462f
AM
8797static bfd_vma
8798get_value (bfd_vma size,
8799 unsigned long chunksz,
8800 bfd *input_bfd,
8801 bfd_byte *location)
d9352518 8802{
9b239e0e 8803 int shift;
d9352518
DB
8804 bfd_vma x = 0;
8805
9b239e0e
NC
8806 /* Sanity checks. */
8807 BFD_ASSERT (chunksz <= sizeof (x)
8808 && size >= chunksz
8809 && chunksz != 0
8810 && (size % chunksz) == 0
8811 && input_bfd != NULL
8812 && location != NULL);
8813
8814 if (chunksz == sizeof (x))
8815 {
8816 BFD_ASSERT (size == chunksz);
8817
8818 /* Make sure that we do not perform an undefined shift operation.
8819 We know that size == chunksz so there will only be one iteration
8820 of the loop below. */
8821 shift = 0;
8822 }
8823 else
8824 shift = 8 * chunksz;
8825
a0c8462f 8826 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8827 {
8828 switch (chunksz)
8829 {
d9352518 8830 case 1:
9b239e0e 8831 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8832 break;
8833 case 2:
9b239e0e 8834 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8835 break;
8836 case 4:
9b239e0e 8837 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8838 break;
d9352518 8839#ifdef BFD64
9b239e0e
NC
8840 case 8:
8841 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8842 break;
9b239e0e
NC
8843#endif
8844 default:
8845 abort ();
d9352518
DB
8846 }
8847 }
8848 return x;
8849}
8850
a0c8462f
AM
8851static void
8852decode_complex_addend (unsigned long *start, /* in bits */
8853 unsigned long *oplen, /* in bits */
8854 unsigned long *len, /* in bits */
8855 unsigned long *wordsz, /* in bytes */
8856 unsigned long *chunksz, /* in bytes */
8857 unsigned long *lsb0_p,
8858 unsigned long *signed_p,
8859 unsigned long *trunc_p,
8860 unsigned long encoded)
d9352518 8861{
07d6d2b8
AM
8862 * start = encoded & 0x3F;
8863 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8864 * oplen = (encoded >> 12) & 0x3F;
8865 * wordsz = (encoded >> 18) & 0xF;
8866 * chunksz = (encoded >> 22) & 0xF;
8867 * lsb0_p = (encoded >> 27) & 1;
8868 * signed_p = (encoded >> 28) & 1;
8869 * trunc_p = (encoded >> 29) & 1;
8870}
8871
cdfeee4f 8872bfd_reloc_status_type
0f02bbd9 8873bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8874 asection *input_section,
0f02bbd9
AM
8875 bfd_byte *contents,
8876 Elf_Internal_Rela *rel,
8877 bfd_vma relocation)
d9352518 8878{
0f02bbd9
AM
8879 bfd_vma shift, x, mask;
8880 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8881 bfd_reloc_status_type r;
bb294208 8882 bfd_size_type octets;
d9352518
DB
8883
8884 /* Perform this reloc, since it is complex.
8885 (this is not to say that it necessarily refers to a complex
8886 symbol; merely that it is a self-describing CGEN based reloc.
8887 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8888 word size, etc) encoded within it.). */
d9352518 8889
a0c8462f
AM
8890 decode_complex_addend (&start, &oplen, &len, &wordsz,
8891 &chunksz, &lsb0_p, &signed_p,
8892 &trunc_p, rel->r_addend);
d9352518
DB
8893
8894 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8895
8896 if (lsb0_p)
8897 shift = (start + 1) - len;
8898 else
8899 shift = (8 * wordsz) - (start + len);
8900
bb294208
AM
8901 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8902 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8903
8904#ifdef DEBUG
8905 printf ("Doing complex reloc: "
8906 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8907 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8908 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8909 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8910 oplen, (unsigned long) x, (unsigned long) mask,
8911 (unsigned long) relocation);
d9352518
DB
8912#endif
8913
cdfeee4f 8914 r = bfd_reloc_ok;
d9352518 8915 if (! trunc_p)
cdfeee4f
AM
8916 /* Now do an overflow check. */
8917 r = bfd_check_overflow ((signed_p
8918 ? complain_overflow_signed
8919 : complain_overflow_unsigned),
8920 len, 0, (8 * wordsz),
8921 relocation);
a0c8462f 8922
d9352518
DB
8923 /* Do the deed. */
8924 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8925
8926#ifdef DEBUG
8927 printf (" relocation: %8.8lx\n"
8928 " shifted mask: %8.8lx\n"
8929 " shifted/masked reloc: %8.8lx\n"
8930 " result: %8.8lx\n",
9ccb8af9
AM
8931 (unsigned long) relocation, (unsigned long) (mask << shift),
8932 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8933#endif
bb294208 8934 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 8935 return r;
d9352518
DB
8936}
8937
0e287786
AM
8938/* Functions to read r_offset from external (target order) reloc
8939 entry. Faster than bfd_getl32 et al, because we let the compiler
8940 know the value is aligned. */
53df40a4 8941
0e287786
AM
8942static bfd_vma
8943ext32l_r_offset (const void *p)
53df40a4
AM
8944{
8945 union aligned32
8946 {
8947 uint32_t v;
8948 unsigned char c[4];
8949 };
8950 const union aligned32 *a
0e287786 8951 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8952
8953 uint32_t aval = ( (uint32_t) a->c[0]
8954 | (uint32_t) a->c[1] << 8
8955 | (uint32_t) a->c[2] << 16
8956 | (uint32_t) a->c[3] << 24);
0e287786 8957 return aval;
53df40a4
AM
8958}
8959
0e287786
AM
8960static bfd_vma
8961ext32b_r_offset (const void *p)
53df40a4
AM
8962{
8963 union aligned32
8964 {
8965 uint32_t v;
8966 unsigned char c[4];
8967 };
8968 const union aligned32 *a
0e287786 8969 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8970
8971 uint32_t aval = ( (uint32_t) a->c[0] << 24
8972 | (uint32_t) a->c[1] << 16
8973 | (uint32_t) a->c[2] << 8
8974 | (uint32_t) a->c[3]);
0e287786 8975 return aval;
53df40a4
AM
8976}
8977
8978#ifdef BFD_HOST_64_BIT
0e287786
AM
8979static bfd_vma
8980ext64l_r_offset (const void *p)
53df40a4
AM
8981{
8982 union aligned64
8983 {
8984 uint64_t v;
8985 unsigned char c[8];
8986 };
8987 const union aligned64 *a
0e287786 8988 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8989
8990 uint64_t aval = ( (uint64_t) a->c[0]
8991 | (uint64_t) a->c[1] << 8
8992 | (uint64_t) a->c[2] << 16
8993 | (uint64_t) a->c[3] << 24
8994 | (uint64_t) a->c[4] << 32
8995 | (uint64_t) a->c[5] << 40
8996 | (uint64_t) a->c[6] << 48
8997 | (uint64_t) a->c[7] << 56);
0e287786 8998 return aval;
53df40a4
AM
8999}
9000
0e287786
AM
9001static bfd_vma
9002ext64b_r_offset (const void *p)
53df40a4
AM
9003{
9004 union aligned64
9005 {
9006 uint64_t v;
9007 unsigned char c[8];
9008 };
9009 const union aligned64 *a
0e287786 9010 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9011
9012 uint64_t aval = ( (uint64_t) a->c[0] << 56
9013 | (uint64_t) a->c[1] << 48
9014 | (uint64_t) a->c[2] << 40
9015 | (uint64_t) a->c[3] << 32
9016 | (uint64_t) a->c[4] << 24
9017 | (uint64_t) a->c[5] << 16
9018 | (uint64_t) a->c[6] << 8
9019 | (uint64_t) a->c[7]);
0e287786 9020 return aval;
53df40a4
AM
9021}
9022#endif
9023
c152c796
AM
9024/* When performing a relocatable link, the input relocations are
9025 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9026 referenced must be updated. Update all the relocations found in
9027 RELDATA. */
c152c796 9028
bca6d0e3 9029static bfd_boolean
c152c796 9030elf_link_adjust_relocs (bfd *abfd,
9eaff861 9031 asection *sec,
28dbcedc 9032 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9033 bfd_boolean sort,
9034 struct bfd_link_info *info)
c152c796
AM
9035{
9036 unsigned int i;
9037 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9038 bfd_byte *erela;
9039 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9040 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9041 bfd_vma r_type_mask;
9042 int r_sym_shift;
d4730f92
BS
9043 unsigned int count = reldata->count;
9044 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9045
d4730f92 9046 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9047 {
9048 swap_in = bed->s->swap_reloc_in;
9049 swap_out = bed->s->swap_reloc_out;
9050 }
d4730f92 9051 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9052 {
9053 swap_in = bed->s->swap_reloca_in;
9054 swap_out = bed->s->swap_reloca_out;
9055 }
9056 else
9057 abort ();
9058
9059 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9060 abort ();
9061
9062 if (bed->s->arch_size == 32)
9063 {
9064 r_type_mask = 0xff;
9065 r_sym_shift = 8;
9066 }
9067 else
9068 {
9069 r_type_mask = 0xffffffff;
9070 r_sym_shift = 32;
9071 }
9072
d4730f92
BS
9073 erela = reldata->hdr->contents;
9074 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9075 {
9076 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9077 unsigned int j;
9078
9079 if (*rel_hash == NULL)
9080 continue;
9081
10bbbc1d
NC
9082 if ((*rel_hash)->indx == -2
9083 && info->gc_sections
9084 && ! info->gc_keep_exported)
9085 {
9086 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9087 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9088 abfd, sec,
9089 (*rel_hash)->root.root.string);
9793eb77 9090 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9091 abfd, sec);
10bbbc1d
NC
9092 bfd_set_error (bfd_error_invalid_operation);
9093 return FALSE;
9094 }
c152c796
AM
9095 BFD_ASSERT ((*rel_hash)->indx >= 0);
9096
9097 (*swap_in) (abfd, erela, irela);
9098 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9099 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9100 | (irela[j].r_info & r_type_mask));
9101 (*swap_out) (abfd, irela, erela);
9102 }
53df40a4 9103
9eaff861
AO
9104 if (bed->elf_backend_update_relocs)
9105 (*bed->elf_backend_update_relocs) (sec, reldata);
9106
0e287786 9107 if (sort && count != 0)
53df40a4 9108 {
0e287786
AM
9109 bfd_vma (*ext_r_off) (const void *);
9110 bfd_vma r_off;
9111 size_t elt_size;
9112 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9113 bfd_byte *buf = NULL;
28dbcedc
AM
9114
9115 if (bed->s->arch_size == 32)
9116 {
9117 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9118 ext_r_off = ext32l_r_offset;
28dbcedc 9119 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9120 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9121 else
9122 abort ();
9123 }
53df40a4 9124 else
28dbcedc 9125 {
53df40a4 9126#ifdef BFD_HOST_64_BIT
28dbcedc 9127 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9128 ext_r_off = ext64l_r_offset;
28dbcedc 9129 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9130 ext_r_off = ext64b_r_offset;
28dbcedc 9131 else
53df40a4 9132#endif
28dbcedc
AM
9133 abort ();
9134 }
0e287786 9135
bca6d0e3
AM
9136 /* Must use a stable sort here. A modified insertion sort,
9137 since the relocs are mostly sorted already. */
0e287786
AM
9138 elt_size = reldata->hdr->sh_entsize;
9139 base = reldata->hdr->contents;
9140 end = base + count * elt_size;
bca6d0e3 9141 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9142 abort ();
9143
9144 /* Ensure the first element is lowest. This acts as a sentinel,
9145 speeding the main loop below. */
9146 r_off = (*ext_r_off) (base);
9147 for (p = loc = base; (p += elt_size) < end; )
9148 {
9149 bfd_vma r_off2 = (*ext_r_off) (p);
9150 if (r_off > r_off2)
9151 {
9152 r_off = r_off2;
9153 loc = p;
9154 }
9155 }
9156 if (loc != base)
9157 {
9158 /* Don't just swap *base and *loc as that changes the order
9159 of the original base[0] and base[1] if they happen to
9160 have the same r_offset. */
bca6d0e3
AM
9161 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9162 memcpy (onebuf, loc, elt_size);
0e287786 9163 memmove (base + elt_size, base, loc - base);
bca6d0e3 9164 memcpy (base, onebuf, elt_size);
0e287786
AM
9165 }
9166
b29b8669 9167 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9168 {
9169 /* base to p is sorted, *p is next to insert. */
9170 r_off = (*ext_r_off) (p);
9171 /* Search the sorted region for location to insert. */
9172 loc = p - elt_size;
9173 while (r_off < (*ext_r_off) (loc))
9174 loc -= elt_size;
9175 loc += elt_size;
9176 if (loc != p)
9177 {
bca6d0e3
AM
9178 /* Chances are there is a run of relocs to insert here,
9179 from one of more input files. Files are not always
9180 linked in order due to the way elf_link_input_bfd is
9181 called. See pr17666. */
9182 size_t sortlen = p - loc;
9183 bfd_vma r_off2 = (*ext_r_off) (loc);
9184 size_t runlen = elt_size;
9185 size_t buf_size = 96 * 1024;
9186 while (p + runlen < end
9187 && (sortlen <= buf_size
9188 || runlen + elt_size <= buf_size)
9189 && r_off2 > (*ext_r_off) (p + runlen))
9190 runlen += elt_size;
9191 if (buf == NULL)
9192 {
9193 buf = bfd_malloc (buf_size);
9194 if (buf == NULL)
9195 return FALSE;
9196 }
9197 if (runlen < sortlen)
9198 {
9199 memcpy (buf, p, runlen);
9200 memmove (loc + runlen, loc, sortlen);
9201 memcpy (loc, buf, runlen);
9202 }
9203 else
9204 {
9205 memcpy (buf, loc, sortlen);
9206 memmove (loc, p, runlen);
9207 memcpy (loc + runlen, buf, sortlen);
9208 }
b29b8669 9209 p += runlen - elt_size;
0e287786
AM
9210 }
9211 }
9212 /* Hashes are no longer valid. */
28dbcedc
AM
9213 free (reldata->hashes);
9214 reldata->hashes = NULL;
bca6d0e3 9215 free (buf);
53df40a4 9216 }
bca6d0e3 9217 return TRUE;
c152c796
AM
9218}
9219
9220struct elf_link_sort_rela
9221{
9222 union {
9223 bfd_vma offset;
9224 bfd_vma sym_mask;
9225 } u;
9226 enum elf_reloc_type_class type;
9227 /* We use this as an array of size int_rels_per_ext_rel. */
9228 Elf_Internal_Rela rela[1];
9229};
9230
dcea6a95
AM
9231/* qsort stability here and for cmp2 is only an issue if multiple
9232 dynamic relocations are emitted at the same address. But targets
9233 that apply a series of dynamic relocations each operating on the
9234 result of the prior relocation can't use -z combreloc as
9235 implemented anyway. Such schemes tend to be broken by sorting on
9236 symbol index. That leaves dynamic NONE relocs as the only other
9237 case where ld might emit multiple relocs at the same address, and
9238 those are only emitted due to target bugs. */
9239
c152c796
AM
9240static int
9241elf_link_sort_cmp1 (const void *A, const void *B)
9242{
a50b1753
NC
9243 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9244 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9245 int relativea, relativeb;
9246
9247 relativea = a->type == reloc_class_relative;
9248 relativeb = b->type == reloc_class_relative;
9249
9250 if (relativea < relativeb)
9251 return 1;
9252 if (relativea > relativeb)
9253 return -1;
9254 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9255 return -1;
9256 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9257 return 1;
9258 if (a->rela->r_offset < b->rela->r_offset)
9259 return -1;
9260 if (a->rela->r_offset > b->rela->r_offset)
9261 return 1;
9262 return 0;
9263}
9264
9265static int
9266elf_link_sort_cmp2 (const void *A, const void *B)
9267{
a50b1753
NC
9268 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9269 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9270
7e612e98 9271 if (a->type < b->type)
c152c796 9272 return -1;
7e612e98 9273 if (a->type > b->type)
c152c796 9274 return 1;
7e612e98 9275 if (a->u.offset < b->u.offset)
c152c796 9276 return -1;
7e612e98 9277 if (a->u.offset > b->u.offset)
c152c796
AM
9278 return 1;
9279 if (a->rela->r_offset < b->rela->r_offset)
9280 return -1;
9281 if (a->rela->r_offset > b->rela->r_offset)
9282 return 1;
9283 return 0;
9284}
9285
9286static size_t
9287elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9288{
3410fea8 9289 asection *dynamic_relocs;
fc66a176
L
9290 asection *rela_dyn;
9291 asection *rel_dyn;
c152c796
AM
9292 bfd_size_type count, size;
9293 size_t i, ret, sort_elt, ext_size;
9294 bfd_byte *sort, *s_non_relative, *p;
9295 struct elf_link_sort_rela *sq;
9296 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9297 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9298 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9299 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9300 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9301 struct bfd_link_order *lo;
9302 bfd_vma r_sym_mask;
3410fea8 9303 bfd_boolean use_rela;
c152c796 9304
3410fea8
NC
9305 /* Find a dynamic reloc section. */
9306 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9307 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9308 if (rela_dyn != NULL && rela_dyn->size > 0
9309 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9310 {
3410fea8
NC
9311 bfd_boolean use_rela_initialised = FALSE;
9312
9313 /* This is just here to stop gcc from complaining.
c8e44c6d 9314 Its initialization checking code is not perfect. */
3410fea8
NC
9315 use_rela = TRUE;
9316
9317 /* Both sections are present. Examine the sizes
9318 of the indirect sections to help us choose. */
9319 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9320 if (lo->type == bfd_indirect_link_order)
9321 {
9322 asection *o = lo->u.indirect.section;
9323
9324 if ((o->size % bed->s->sizeof_rela) == 0)
9325 {
9326 if ((o->size % bed->s->sizeof_rel) == 0)
9327 /* Section size is divisible by both rel and rela sizes.
9328 It is of no help to us. */
9329 ;
9330 else
9331 {
9332 /* Section size is only divisible by rela. */
535b785f 9333 if (use_rela_initialised && !use_rela)
3410fea8 9334 {
9793eb77 9335 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9336 "they are in more than one size"),
9337 abfd);
3410fea8
NC
9338 bfd_set_error (bfd_error_invalid_operation);
9339 return 0;
9340 }
9341 else
9342 {
9343 use_rela = TRUE;
9344 use_rela_initialised = TRUE;
9345 }
9346 }
9347 }
9348 else if ((o->size % bed->s->sizeof_rel) == 0)
9349 {
9350 /* Section size is only divisible by rel. */
535b785f 9351 if (use_rela_initialised && use_rela)
3410fea8 9352 {
9793eb77 9353 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9354 "they are in more than one size"),
9355 abfd);
3410fea8
NC
9356 bfd_set_error (bfd_error_invalid_operation);
9357 return 0;
9358 }
9359 else
9360 {
9361 use_rela = FALSE;
9362 use_rela_initialised = TRUE;
9363 }
9364 }
9365 else
9366 {
c8e44c6d
AM
9367 /* The section size is not divisible by either -
9368 something is wrong. */
9793eb77 9369 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9370 "they are of an unknown size"), abfd);
3410fea8
NC
9371 bfd_set_error (bfd_error_invalid_operation);
9372 return 0;
9373 }
9374 }
9375
9376 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9377 if (lo->type == bfd_indirect_link_order)
9378 {
9379 asection *o = lo->u.indirect.section;
9380
9381 if ((o->size % bed->s->sizeof_rela) == 0)
9382 {
9383 if ((o->size % bed->s->sizeof_rel) == 0)
9384 /* Section size is divisible by both rel and rela sizes.
9385 It is of no help to us. */
9386 ;
9387 else
9388 {
9389 /* Section size is only divisible by rela. */
535b785f 9390 if (use_rela_initialised && !use_rela)
3410fea8 9391 {
9793eb77 9392 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9393 "they are in more than one size"),
9394 abfd);
3410fea8
NC
9395 bfd_set_error (bfd_error_invalid_operation);
9396 return 0;
9397 }
9398 else
9399 {
9400 use_rela = TRUE;
9401 use_rela_initialised = TRUE;
9402 }
9403 }
9404 }
9405 else if ((o->size % bed->s->sizeof_rel) == 0)
9406 {
9407 /* Section size is only divisible by rel. */
535b785f 9408 if (use_rela_initialised && use_rela)
3410fea8 9409 {
9793eb77 9410 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9411 "they are in more than one size"),
9412 abfd);
3410fea8
NC
9413 bfd_set_error (bfd_error_invalid_operation);
9414 return 0;
9415 }
9416 else
9417 {
9418 use_rela = FALSE;
9419 use_rela_initialised = TRUE;
9420 }
9421 }
9422 else
9423 {
c8e44c6d
AM
9424 /* The section size is not divisible by either -
9425 something is wrong. */
9793eb77 9426 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9427 "they are of an unknown size"), abfd);
3410fea8
NC
9428 bfd_set_error (bfd_error_invalid_operation);
9429 return 0;
9430 }
9431 }
9432
9433 if (! use_rela_initialised)
9434 /* Make a guess. */
9435 use_rela = TRUE;
c152c796 9436 }
fc66a176
L
9437 else if (rela_dyn != NULL && rela_dyn->size > 0)
9438 use_rela = TRUE;
9439 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9440 use_rela = FALSE;
c152c796 9441 else
fc66a176 9442 return 0;
3410fea8
NC
9443
9444 if (use_rela)
c152c796 9445 {
3410fea8 9446 dynamic_relocs = rela_dyn;
c152c796
AM
9447 ext_size = bed->s->sizeof_rela;
9448 swap_in = bed->s->swap_reloca_in;
9449 swap_out = bed->s->swap_reloca_out;
9450 }
3410fea8
NC
9451 else
9452 {
9453 dynamic_relocs = rel_dyn;
9454 ext_size = bed->s->sizeof_rel;
9455 swap_in = bed->s->swap_reloc_in;
9456 swap_out = bed->s->swap_reloc_out;
9457 }
c152c796
AM
9458
9459 size = 0;
3410fea8 9460 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9461 if (lo->type == bfd_indirect_link_order)
3410fea8 9462 size += lo->u.indirect.section->size;
c152c796 9463
3410fea8 9464 if (size != dynamic_relocs->size)
c152c796
AM
9465 return 0;
9466
9467 sort_elt = (sizeof (struct elf_link_sort_rela)
9468 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9469
9470 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9471 if (count == 0)
9472 return 0;
a50b1753 9473 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9474
c152c796
AM
9475 if (sort == NULL)
9476 {
9477 (*info->callbacks->warning)
9793eb77 9478 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9479 return 0;
9480 }
9481
9482 if (bed->s->arch_size == 32)
9483 r_sym_mask = ~(bfd_vma) 0xff;
9484 else
9485 r_sym_mask = ~(bfd_vma) 0xffffffff;
9486
3410fea8 9487 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9488 if (lo->type == bfd_indirect_link_order)
9489 {
9490 bfd_byte *erel, *erelend;
9491 asection *o = lo->u.indirect.section;
9492
1da212d6
AM
9493 if (o->contents == NULL && o->size != 0)
9494 {
9495 /* This is a reloc section that is being handled as a normal
9496 section. See bfd_section_from_shdr. We can't combine
9497 relocs in this case. */
9498 free (sort);
9499 return 0;
9500 }
c152c796 9501 erel = o->contents;
eea6121a 9502 erelend = o->contents + o->size;
c8e44c6d 9503 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9504
c152c796
AM
9505 while (erel < erelend)
9506 {
9507 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9508
c152c796 9509 (*swap_in) (abfd, erel, s->rela);
7e612e98 9510 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9511 s->u.sym_mask = r_sym_mask;
9512 p += sort_elt;
9513 erel += ext_size;
9514 }
9515 }
9516
9517 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9518
9519 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9520 {
9521 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9522 if (s->type != reloc_class_relative)
9523 break;
9524 }
9525 ret = i;
9526 s_non_relative = p;
9527
9528 sq = (struct elf_link_sort_rela *) s_non_relative;
9529 for (; i < count; i++, p += sort_elt)
9530 {
9531 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9532 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9533 sq = sp;
9534 sp->u.offset = sq->rela->r_offset;
9535 }
9536
9537 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9538
c8e44c6d
AM
9539 struct elf_link_hash_table *htab = elf_hash_table (info);
9540 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9541 {
9542 /* We have plt relocs in .rela.dyn. */
9543 sq = (struct elf_link_sort_rela *) sort;
9544 for (i = 0; i < count; i++)
9545 if (sq[count - i - 1].type != reloc_class_plt)
9546 break;
9547 if (i != 0 && htab->srelplt->size == i * ext_size)
9548 {
9549 struct bfd_link_order **plo;
9550 /* Put srelplt link_order last. This is so the output_offset
9551 set in the next loop is correct for DT_JMPREL. */
9552 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9553 if ((*plo)->type == bfd_indirect_link_order
9554 && (*plo)->u.indirect.section == htab->srelplt)
9555 {
9556 lo = *plo;
9557 *plo = lo->next;
9558 }
9559 else
9560 plo = &(*plo)->next;
9561 *plo = lo;
9562 lo->next = NULL;
9563 dynamic_relocs->map_tail.link_order = lo;
9564 }
9565 }
9566
9567 p = sort;
3410fea8 9568 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9569 if (lo->type == bfd_indirect_link_order)
9570 {
9571 bfd_byte *erel, *erelend;
9572 asection *o = lo->u.indirect.section;
9573
9574 erel = o->contents;
eea6121a 9575 erelend = o->contents + o->size;
c8e44c6d 9576 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9577 while (erel < erelend)
9578 {
9579 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9580 (*swap_out) (abfd, s->rela, erel);
9581 p += sort_elt;
9582 erel += ext_size;
9583 }
9584 }
9585
9586 free (sort);
3410fea8 9587 *psec = dynamic_relocs;
c152c796
AM
9588 return ret;
9589}
9590
ef10c3ac 9591/* Add a symbol to the output symbol string table. */
c152c796 9592
6e0b88f1 9593static int
ef10c3ac
L
9594elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9595 const char *name,
9596 Elf_Internal_Sym *elfsym,
9597 asection *input_sec,
9598 struct elf_link_hash_entry *h)
c152c796 9599{
6e0b88f1 9600 int (*output_symbol_hook)
c152c796
AM
9601 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9602 struct elf_link_hash_entry *);
ef10c3ac 9603 struct elf_link_hash_table *hash_table;
c152c796 9604 const struct elf_backend_data *bed;
ef10c3ac 9605 bfd_size_type strtabsize;
c152c796 9606
8539e4e8
AM
9607 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9608
8b127cbc 9609 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9610 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9611 if (output_symbol_hook != NULL)
9612 {
8b127cbc 9613 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9614 if (ret != 1)
9615 return ret;
c152c796
AM
9616 }
9617
06f44071
AM
9618 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9619 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9620 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9621 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9622
ef10c3ac
L
9623 if (name == NULL
9624 || *name == '\0'
9625 || (input_sec->flags & SEC_EXCLUDE))
9626 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9627 else
9628 {
ef10c3ac
L
9629 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9630 to get the final offset for st_name. */
9631 elfsym->st_name
9632 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9633 name, FALSE);
c152c796 9634 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9635 return 0;
c152c796
AM
9636 }
9637
ef10c3ac
L
9638 hash_table = elf_hash_table (flinfo->info);
9639 strtabsize = hash_table->strtabsize;
9640 if (strtabsize <= hash_table->strtabcount)
c152c796 9641 {
ef10c3ac
L
9642 strtabsize += strtabsize;
9643 hash_table->strtabsize = strtabsize;
9644 strtabsize *= sizeof (*hash_table->strtab);
9645 hash_table->strtab
9646 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9647 strtabsize);
9648 if (hash_table->strtab == NULL)
6e0b88f1 9649 return 0;
c152c796 9650 }
ef10c3ac
L
9651 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9652 hash_table->strtab[hash_table->strtabcount].dest_index
9653 = hash_table->strtabcount;
9654 hash_table->strtab[hash_table->strtabcount].destshndx_index
9655 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9656
ed48ec2e 9657 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9658 hash_table->strtabcount += 1;
9659
9660 return 1;
9661}
9662
9663/* Swap symbols out to the symbol table and flush the output symbols to
9664 the file. */
9665
9666static bfd_boolean
9667elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9668{
9669 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9670 size_t amt;
ef53be89 9671 size_t i;
ef10c3ac
L
9672 const struct elf_backend_data *bed;
9673 bfd_byte *symbuf;
9674 Elf_Internal_Shdr *hdr;
9675 file_ptr pos;
9676 bfd_boolean ret;
9677
9678 if (!hash_table->strtabcount)
9679 return TRUE;
9680
9681 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9682
9683 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9684
ef10c3ac
L
9685 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9686 symbuf = (bfd_byte *) bfd_malloc (amt);
9687 if (symbuf == NULL)
9688 return FALSE;
1b786873 9689
ef10c3ac 9690 if (flinfo->symshndxbuf)
c152c796 9691 {
ef53be89
AM
9692 amt = sizeof (Elf_External_Sym_Shndx);
9693 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9694 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9695 if (flinfo->symshndxbuf == NULL)
c152c796 9696 {
ef10c3ac
L
9697 free (symbuf);
9698 return FALSE;
c152c796 9699 }
c152c796
AM
9700 }
9701
ef10c3ac
L
9702 for (i = 0; i < hash_table->strtabcount; i++)
9703 {
9704 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9705 if (elfsym->sym.st_name == (unsigned long) -1)
9706 elfsym->sym.st_name = 0;
9707 else
9708 elfsym->sym.st_name
9709 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9710 elfsym->sym.st_name);
9711 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9712 ((bfd_byte *) symbuf
9713 + (elfsym->dest_index
9714 * bed->s->sizeof_sym)),
9715 (flinfo->symshndxbuf
9716 + elfsym->destshndx_index));
9717 }
9718
1ff6de03
NA
9719 /* Allow the linker to examine the strtab and symtab now they are
9720 populated. */
9721
9722 if (flinfo->info->callbacks->examine_strtab)
9723 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9724 hash_table->strtabcount,
9725 flinfo->symstrtab);
9726
ef10c3ac
L
9727 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9728 pos = hdr->sh_offset + hdr->sh_size;
9729 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9730 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9731 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9732 {
9733 hdr->sh_size += amt;
9734 ret = TRUE;
9735 }
9736 else
9737 ret = FALSE;
c152c796 9738
ef10c3ac
L
9739 free (symbuf);
9740
9741 free (hash_table->strtab);
9742 hash_table->strtab = NULL;
9743
9744 return ret;
c152c796
AM
9745}
9746
c0d5a53d
L
9747/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9748
9749static bfd_boolean
9750check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9751{
4fbb74a6
AM
9752 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9753 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9754 {
9755 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9756 beyond 64k. */
4eca0228 9757 _bfd_error_handler
695344c0 9758 /* xgettext:c-format */
9793eb77 9759 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9760 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9761 bfd_set_error (bfd_error_nonrepresentable_section);
9762 return FALSE;
9763 }
9764 return TRUE;
9765}
9766
c152c796
AM
9767/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9768 allowing an unsatisfied unversioned symbol in the DSO to match a
9769 versioned symbol that would normally require an explicit version.
9770 We also handle the case that a DSO references a hidden symbol
9771 which may be satisfied by a versioned symbol in another DSO. */
9772
9773static bfd_boolean
9774elf_link_check_versioned_symbol (struct bfd_link_info *info,
9775 const struct elf_backend_data *bed,
9776 struct elf_link_hash_entry *h)
9777{
9778 bfd *abfd;
9779 struct elf_link_loaded_list *loaded;
9780
9781 if (!is_elf_hash_table (info->hash))
9782 return FALSE;
9783
90c984fc
L
9784 /* Check indirect symbol. */
9785 while (h->root.type == bfd_link_hash_indirect)
9786 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9787
c152c796
AM
9788 switch (h->root.type)
9789 {
9790 default:
9791 abfd = NULL;
9792 break;
9793
9794 case bfd_link_hash_undefined:
9795 case bfd_link_hash_undefweak:
9796 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9797 if (abfd == NULL
9798 || (abfd->flags & DYNAMIC) == 0
e56f61be 9799 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9800 return FALSE;
9801 break;
9802
9803 case bfd_link_hash_defined:
9804 case bfd_link_hash_defweak:
9805 abfd = h->root.u.def.section->owner;
9806 break;
9807
9808 case bfd_link_hash_common:
9809 abfd = h->root.u.c.p->section->owner;
9810 break;
9811 }
9812 BFD_ASSERT (abfd != NULL);
9813
e310298c 9814 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
9815 loaded != NULL;
9816 loaded = loaded->next)
9817 {
9818 bfd *input;
9819 Elf_Internal_Shdr *hdr;
ef53be89
AM
9820 size_t symcount;
9821 size_t extsymcount;
9822 size_t extsymoff;
c152c796
AM
9823 Elf_Internal_Shdr *versymhdr;
9824 Elf_Internal_Sym *isym;
9825 Elf_Internal_Sym *isymend;
9826 Elf_Internal_Sym *isymbuf;
9827 Elf_External_Versym *ever;
9828 Elf_External_Versym *extversym;
9829
9830 input = loaded->abfd;
9831
9832 /* We check each DSO for a possible hidden versioned definition. */
9833 if (input == abfd
c152c796
AM
9834 || elf_dynversym (input) == 0)
9835 continue;
9836
9837 hdr = &elf_tdata (input)->dynsymtab_hdr;
9838
9839 symcount = hdr->sh_size / bed->s->sizeof_sym;
9840 if (elf_bad_symtab (input))
9841 {
9842 extsymcount = symcount;
9843 extsymoff = 0;
9844 }
9845 else
9846 {
9847 extsymcount = symcount - hdr->sh_info;
9848 extsymoff = hdr->sh_info;
9849 }
9850
9851 if (extsymcount == 0)
9852 continue;
9853
9854 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9855 NULL, NULL, NULL);
9856 if (isymbuf == NULL)
9857 return FALSE;
9858
9859 /* Read in any version definitions. */
9860 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 9861 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
9862 || (extversym = (Elf_External_Versym *)
9863 _bfd_malloc_and_read (input, versymhdr->sh_size,
9864 versymhdr->sh_size)) == NULL)
c152c796 9865 {
c152c796
AM
9866 free (isymbuf);
9867 return FALSE;
9868 }
9869
9870 ever = extversym + extsymoff;
9871 isymend = isymbuf + extsymcount;
9872 for (isym = isymbuf; isym < isymend; isym++, ever++)
9873 {
9874 const char *name;
9875 Elf_Internal_Versym iver;
9876 unsigned short version_index;
9877
9878 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9879 || isym->st_shndx == SHN_UNDEF)
9880 continue;
9881
9882 name = bfd_elf_string_from_elf_section (input,
9883 hdr->sh_link,
9884 isym->st_name);
9885 if (strcmp (name, h->root.root.string) != 0)
9886 continue;
9887
9888 _bfd_elf_swap_versym_in (input, ever, &iver);
9889
d023c380
L
9890 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9891 && !(h->def_regular
9892 && h->forced_local))
c152c796
AM
9893 {
9894 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9895 have provided a definition for the undefined sym unless
9896 it is defined in a non-shared object and forced local.
9897 */
c152c796
AM
9898 abort ();
9899 }
9900
9901 version_index = iver.vs_vers & VERSYM_VERSION;
9902 if (version_index == 1 || version_index == 2)
9903 {
9904 /* This is the base or first version. We can use it. */
9905 free (extversym);
9906 free (isymbuf);
9907 return TRUE;
9908 }
9909 }
9910
9911 free (extversym);
9912 free (isymbuf);
9913 }
9914
9915 return FALSE;
9916}
9917
b8871f35
L
9918/* Convert ELF common symbol TYPE. */
9919
9920static int
9921elf_link_convert_common_type (struct bfd_link_info *info, int type)
9922{
9923 /* Commom symbol can only appear in relocatable link. */
9924 if (!bfd_link_relocatable (info))
9925 abort ();
9926 switch (info->elf_stt_common)
9927 {
9928 case unchanged:
9929 break;
9930 case elf_stt_common:
9931 type = STT_COMMON;
9932 break;
9933 case no_elf_stt_common:
9934 type = STT_OBJECT;
9935 break;
9936 }
9937 return type;
9938}
9939
c152c796
AM
9940/* Add an external symbol to the symbol table. This is called from
9941 the hash table traversal routine. When generating a shared object,
9942 we go through the symbol table twice. The first time we output
9943 anything that might have been forced to local scope in a version
9944 script. The second time we output the symbols that are still
9945 global symbols. */
9946
9947static bfd_boolean
7686d77d 9948elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9949{
7686d77d 9950 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9951 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9952 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9953 bfd_boolean strip;
9954 Elf_Internal_Sym sym;
9955 asection *input_sec;
9956 const struct elf_backend_data *bed;
6e0b88f1
AM
9957 long indx;
9958 int ret;
b8871f35 9959 unsigned int type;
c152c796
AM
9960
9961 if (h->root.type == bfd_link_hash_warning)
9962 {
9963 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9964 if (h->root.type == bfd_link_hash_new)
9965 return TRUE;
9966 }
9967
9968 /* Decide whether to output this symbol in this pass. */
9969 if (eoinfo->localsyms)
9970 {
4deb8f71 9971 if (!h->forced_local)
c152c796
AM
9972 return TRUE;
9973 }
9974 else
9975 {
4deb8f71 9976 if (h->forced_local)
c152c796
AM
9977 return TRUE;
9978 }
9979
8b127cbc 9980 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9981
12ac1cf5 9982 if (h->root.type == bfd_link_hash_undefined)
c152c796 9983 {
12ac1cf5
NC
9984 /* If we have an undefined symbol reference here then it must have
9985 come from a shared library that is being linked in. (Undefined
98da7939
L
9986 references in regular files have already been handled unless
9987 they are in unreferenced sections which are removed by garbage
9988 collection). */
12ac1cf5
NC
9989 bfd_boolean ignore_undef = FALSE;
9990
9991 /* Some symbols may be special in that the fact that they're
9992 undefined can be safely ignored - let backend determine that. */
9993 if (bed->elf_backend_ignore_undef_symbol)
9994 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9995
9996 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9997 if (!ignore_undef
c54f1524 9998 && h->ref_dynamic_nonweak
8b127cbc
AM
9999 && (!h->ref_regular || flinfo->info->gc_sections)
10000 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10001 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10002 {
10003 flinfo->info->callbacks->undefined_symbol
10004 (flinfo->info, h->root.root.string,
10005 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10006 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10007 && !flinfo->info->warn_unresolved_syms);
10008 }
97196564
L
10009
10010 /* Strip a global symbol defined in a discarded section. */
10011 if (h->indx == -3)
10012 return TRUE;
c152c796
AM
10013 }
10014
10015 /* We should also warn if a forced local symbol is referenced from
10016 shared libraries. */
0e1862bb 10017 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10018 && h->forced_local
10019 && h->ref_dynamic
371a5866 10020 && h->def_regular
f5385ebf 10021 && !h->dynamic_def
ee659f1f 10022 && h->ref_dynamic_nonweak
8b127cbc 10023 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10024 {
17d078c5
AM
10025 bfd *def_bfd;
10026 const char *msg;
90c984fc
L
10027 struct elf_link_hash_entry *hi = h;
10028
10029 /* Check indirect symbol. */
10030 while (hi->root.type == bfd_link_hash_indirect)
10031 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10032
10033 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10034 /* xgettext:c-format */
871b3ab2 10035 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10036 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10037 /* xgettext:c-format */
871b3ab2 10038 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10039 else
695344c0 10040 /* xgettext:c-format */
871b3ab2 10041 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10042 def_bfd = flinfo->output_bfd;
90c984fc
L
10043 if (hi->root.u.def.section != bfd_abs_section_ptr)
10044 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10045 _bfd_error_handler (msg, flinfo->output_bfd,
10046 h->root.root.string, def_bfd);
17d078c5 10047 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10048 eoinfo->failed = TRUE;
10049 return FALSE;
10050 }
10051
10052 /* We don't want to output symbols that have never been mentioned by
10053 a regular file, or that we have been told to strip. However, if
10054 h->indx is set to -2, the symbol is used by a reloc and we must
10055 output it. */
d983c8c5 10056 strip = FALSE;
c152c796 10057 if (h->indx == -2)
d983c8c5 10058 ;
f5385ebf 10059 else if ((h->def_dynamic
77cfaee6
AM
10060 || h->ref_dynamic
10061 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10062 && !h->def_regular
10063 && !h->ref_regular)
c152c796 10064 strip = TRUE;
8b127cbc 10065 else if (flinfo->info->strip == strip_all)
c152c796 10066 strip = TRUE;
8b127cbc
AM
10067 else if (flinfo->info->strip == strip_some
10068 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10069 h->root.root.string, FALSE, FALSE) == NULL)
10070 strip = TRUE;
d56d55e7
AM
10071 else if ((h->root.type == bfd_link_hash_defined
10072 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10073 && ((flinfo->info->strip_discarded
dbaa2011 10074 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10075 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10076 && h->root.u.def.section->owner != NULL
d56d55e7 10077 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10078 strip = TRUE;
9e2278f5
AM
10079 else if ((h->root.type == bfd_link_hash_undefined
10080 || h->root.type == bfd_link_hash_undefweak)
10081 && h->root.u.undef.abfd != NULL
10082 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10083 strip = TRUE;
c152c796 10084
b8871f35
L
10085 type = h->type;
10086
c152c796 10087 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10088 nothing else to do. However, if it is a forced local symbol or
10089 an ifunc symbol we need to give the backend finish_dynamic_symbol
10090 function a chance to make it dynamic. */
c152c796
AM
10091 if (strip
10092 && h->dynindx == -1
b8871f35 10093 && type != STT_GNU_IFUNC
f5385ebf 10094 && !h->forced_local)
c152c796
AM
10095 return TRUE;
10096
10097 sym.st_value = 0;
10098 sym.st_size = h->size;
10099 sym.st_other = h->other;
c152c796
AM
10100 switch (h->root.type)
10101 {
10102 default:
10103 case bfd_link_hash_new:
10104 case bfd_link_hash_warning:
10105 abort ();
10106 return FALSE;
10107
10108 case bfd_link_hash_undefined:
10109 case bfd_link_hash_undefweak:
10110 input_sec = bfd_und_section_ptr;
10111 sym.st_shndx = SHN_UNDEF;
10112 break;
10113
10114 case bfd_link_hash_defined:
10115 case bfd_link_hash_defweak:
10116 {
10117 input_sec = h->root.u.def.section;
10118 if (input_sec->output_section != NULL)
10119 {
10120 sym.st_shndx =
8b127cbc 10121 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10122 input_sec->output_section);
10123 if (sym.st_shndx == SHN_BAD)
10124 {
4eca0228 10125 _bfd_error_handler
695344c0 10126 /* xgettext:c-format */
871b3ab2 10127 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10128 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10129 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10130 eoinfo->failed = TRUE;
10131 return FALSE;
10132 }
10133
10134 /* ELF symbols in relocatable files are section relative,
10135 but in nonrelocatable files they are virtual
10136 addresses. */
10137 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10138 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10139 {
10140 sym.st_value += input_sec->output_section->vma;
10141 if (h->type == STT_TLS)
10142 {
8b127cbc 10143 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10144 if (tls_sec != NULL)
10145 sym.st_value -= tls_sec->vma;
c152c796
AM
10146 }
10147 }
10148 }
10149 else
10150 {
10151 BFD_ASSERT (input_sec->owner == NULL
10152 || (input_sec->owner->flags & DYNAMIC) != 0);
10153 sym.st_shndx = SHN_UNDEF;
10154 input_sec = bfd_und_section_ptr;
10155 }
10156 }
10157 break;
10158
10159 case bfd_link_hash_common:
10160 input_sec = h->root.u.c.p->section;
a4d8e49b 10161 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10162 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10163 break;
10164
10165 case bfd_link_hash_indirect:
10166 /* These symbols are created by symbol versioning. They point
10167 to the decorated version of the name. For example, if the
10168 symbol foo@@GNU_1.2 is the default, which should be used when
10169 foo is used with no version, then we add an indirect symbol
10170 foo which points to foo@@GNU_1.2. We ignore these symbols,
10171 since the indirected symbol is already in the hash table. */
10172 return TRUE;
10173 }
10174
b8871f35
L
10175 if (type == STT_COMMON || type == STT_OBJECT)
10176 switch (h->root.type)
10177 {
10178 case bfd_link_hash_common:
10179 type = elf_link_convert_common_type (flinfo->info, type);
10180 break;
10181 case bfd_link_hash_defined:
10182 case bfd_link_hash_defweak:
10183 if (bed->common_definition (&sym))
10184 type = elf_link_convert_common_type (flinfo->info, type);
10185 else
10186 type = STT_OBJECT;
10187 break;
10188 case bfd_link_hash_undefined:
10189 case bfd_link_hash_undefweak:
10190 break;
10191 default:
10192 abort ();
10193 }
10194
4deb8f71 10195 if (h->forced_local)
b8871f35
L
10196 {
10197 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10198 /* Turn off visibility on local symbol. */
10199 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10200 }
10201 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10202 else if (h->unique_global && h->def_regular)
10203 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10204 else if (h->root.type == bfd_link_hash_undefweak
10205 || h->root.type == bfd_link_hash_defweak)
10206 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10207 else
10208 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10209 sym.st_target_internal = h->target_internal;
10210
c152c796
AM
10211 /* Give the processor backend a chance to tweak the symbol value,
10212 and also to finish up anything that needs to be done for this
10213 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10214 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10215 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10216 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10217 && h->def_regular
0e1862bb 10218 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10219 || ((h->dynindx != -1
10220 || h->forced_local)
0e1862bb 10221 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10222 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10223 || h->root.type != bfd_link_hash_undefweak))
10224 || !h->forced_local)
8b127cbc 10225 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10226 {
10227 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10228 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10229 {
10230 eoinfo->failed = TRUE;
10231 return FALSE;
10232 }
10233 }
10234
10235 /* If we are marking the symbol as undefined, and there are no
10236 non-weak references to this symbol from a regular object, then
10237 mark the symbol as weak undefined; if there are non-weak
10238 references, mark the symbol as strong. We can't do this earlier,
10239 because it might not be marked as undefined until the
10240 finish_dynamic_symbol routine gets through with it. */
10241 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10242 && h->ref_regular
c152c796
AM
10243 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10244 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10245 {
10246 int bindtype;
b8871f35 10247 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10248
10249 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10250 if (type == STT_GNU_IFUNC)
10251 type = STT_FUNC;
c152c796 10252
f5385ebf 10253 if (h->ref_regular_nonweak)
c152c796
AM
10254 bindtype = STB_GLOBAL;
10255 else
10256 bindtype = STB_WEAK;
2955ec4c 10257 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10258 }
10259
bda987c2
CD
10260 /* If this is a symbol defined in a dynamic library, don't use the
10261 symbol size from the dynamic library. Relinking an executable
10262 against a new library may introduce gratuitous changes in the
10263 executable's symbols if we keep the size. */
10264 if (sym.st_shndx == SHN_UNDEF
10265 && !h->def_regular
10266 && h->def_dynamic)
10267 sym.st_size = 0;
10268
c152c796
AM
10269 /* If a non-weak symbol with non-default visibility is not defined
10270 locally, it is a fatal error. */
0e1862bb 10271 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10272 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10273 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10274 && h->root.type == bfd_link_hash_undefined
f5385ebf 10275 && !h->def_regular)
c152c796 10276 {
17d078c5
AM
10277 const char *msg;
10278
10279 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10280 /* xgettext:c-format */
871b3ab2 10281 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10282 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10283 /* xgettext:c-format */
871b3ab2 10284 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10285 else
695344c0 10286 /* xgettext:c-format */
871b3ab2 10287 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10288 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10289 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10290 eoinfo->failed = TRUE;
10291 return FALSE;
10292 }
10293
10294 /* If this symbol should be put in the .dynsym section, then put it
10295 there now. We already know the symbol index. We also fill in
10296 the entry in the .hash section. */
1c2649ed
EB
10297 if (h->dynindx != -1
10298 && elf_hash_table (flinfo->info)->dynamic_sections_created
10299 && elf_hash_table (flinfo->info)->dynsym != NULL
10300 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10301 {
c152c796
AM
10302 bfd_byte *esym;
10303
90c984fc
L
10304 /* Since there is no version information in the dynamic string,
10305 if there is no version info in symbol version section, we will
1659f720 10306 have a run-time problem if not linking executable, referenced
4deb8f71 10307 by shared library, or not bound locally. */
1659f720 10308 if (h->verinfo.verdef == NULL
0e1862bb 10309 && (!bfd_link_executable (flinfo->info)
1659f720
L
10310 || h->ref_dynamic
10311 || !h->def_regular))
90c984fc
L
10312 {
10313 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10314
10315 if (p && p [1] != '\0')
10316 {
4eca0228 10317 _bfd_error_handler
695344c0 10318 /* xgettext:c-format */
9793eb77 10319 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10320 flinfo->output_bfd, h->root.root.string);
10321 eoinfo->failed = TRUE;
10322 return FALSE;
10323 }
10324 }
10325
c152c796 10326 sym.st_name = h->dynstr_index;
cae1fbbb
L
10327 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10328 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10329 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10330 {
10331 eoinfo->failed = TRUE;
10332 return FALSE;
10333 }
8b127cbc 10334 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10335
8b127cbc 10336 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10337 {
10338 size_t hash_entry_size;
10339 bfd_byte *bucketpos;
10340 bfd_vma chain;
41198d0c
L
10341 size_t bucketcount;
10342 size_t bucket;
10343
8b127cbc 10344 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10345 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10346
10347 hash_entry_size
8b127cbc
AM
10348 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10349 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10350 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10351 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10352 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10353 bucketpos);
10354 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10355 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10356 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10357 }
c152c796 10358
8b127cbc 10359 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10360 {
10361 Elf_Internal_Versym iversym;
10362 Elf_External_Versym *eversym;
10363
5fa370e4 10364 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10365 {
7b20f099
AM
10366 if (h->verinfo.verdef == NULL
10367 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10368 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10369 iversym.vs_vers = 0;
10370 else
10371 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10372 }
10373 else
10374 {
10375 if (h->verinfo.vertree == NULL)
10376 iversym.vs_vers = 1;
10377 else
10378 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10379 if (flinfo->info->create_default_symver)
3e3b46e5 10380 iversym.vs_vers++;
c152c796
AM
10381 }
10382
422f1182 10383 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10384 defined locally. */
422f1182 10385 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10386 iversym.vs_vers |= VERSYM_HIDDEN;
10387
8b127cbc 10388 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10389 eversym += h->dynindx;
8b127cbc 10390 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10391 }
10392 }
10393
d983c8c5
AM
10394 /* If the symbol is undefined, and we didn't output it to .dynsym,
10395 strip it from .symtab too. Obviously we can't do this for
10396 relocatable output or when needed for --emit-relocs. */
10397 else if (input_sec == bfd_und_section_ptr
10398 && h->indx != -2
66cae560
NC
10399 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10400 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10401 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10402 return TRUE;
66cae560 10403
d983c8c5
AM
10404 /* Also strip others that we couldn't earlier due to dynamic symbol
10405 processing. */
10406 if (strip)
10407 return TRUE;
10408 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10409 return TRUE;
10410
2ec55de3
AM
10411 /* Output a FILE symbol so that following locals are not associated
10412 with the wrong input file. We need one for forced local symbols
10413 if we've seen more than one FILE symbol or when we have exactly
10414 one FILE symbol but global symbols are present in a file other
10415 than the one with the FILE symbol. We also need one if linker
10416 defined symbols are present. In practice these conditions are
10417 always met, so just emit the FILE symbol unconditionally. */
10418 if (eoinfo->localsyms
10419 && !eoinfo->file_sym_done
10420 && eoinfo->flinfo->filesym_count != 0)
10421 {
10422 Elf_Internal_Sym fsym;
10423
10424 memset (&fsym, 0, sizeof (fsym));
10425 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10426 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10427 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10428 bfd_und_section_ptr, NULL))
2ec55de3
AM
10429 return FALSE;
10430
10431 eoinfo->file_sym_done = TRUE;
10432 }
10433
8b127cbc 10434 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10435 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10436 input_sec, h);
6e0b88f1 10437 if (ret == 0)
c152c796
AM
10438 {
10439 eoinfo->failed = TRUE;
10440 return FALSE;
10441 }
6e0b88f1
AM
10442 else if (ret == 1)
10443 h->indx = indx;
10444 else if (h->indx == -2)
10445 abort();
c152c796
AM
10446
10447 return TRUE;
10448}
10449
cdd3575c
AM
10450/* Return TRUE if special handling is done for relocs in SEC against
10451 symbols defined in discarded sections. */
10452
c152c796
AM
10453static bfd_boolean
10454elf_section_ignore_discarded_relocs (asection *sec)
10455{
10456 const struct elf_backend_data *bed;
10457
cdd3575c
AM
10458 switch (sec->sec_info_type)
10459 {
dbaa2011
AM
10460 case SEC_INFO_TYPE_STABS:
10461 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10462 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10463 return TRUE;
10464 default:
10465 break;
10466 }
c152c796
AM
10467
10468 bed = get_elf_backend_data (sec->owner);
10469 if (bed->elf_backend_ignore_discarded_relocs != NULL
10470 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10471 return TRUE;
10472
10473 return FALSE;
10474}
10475
9e66c942
AM
10476/* Return a mask saying how ld should treat relocations in SEC against
10477 symbols defined in discarded sections. If this function returns
10478 COMPLAIN set, ld will issue a warning message. If this function
10479 returns PRETEND set, and the discarded section was link-once and the
10480 same size as the kept link-once section, ld will pretend that the
10481 symbol was actually defined in the kept section. Otherwise ld will
10482 zero the reloc (at least that is the intent, but some cooperation by
10483 the target dependent code is needed, particularly for REL targets). */
10484
8a696751
AM
10485unsigned int
10486_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10487{
9e66c942 10488 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10489 return PRETEND;
cdd3575c
AM
10490
10491 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10492 return 0;
cdd3575c
AM
10493
10494 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10495 return 0;
cdd3575c 10496
9e66c942 10497 return COMPLAIN | PRETEND;
cdd3575c
AM
10498}
10499
3d7f7666
L
10500/* Find a match between a section and a member of a section group. */
10501
10502static asection *
c0f00686
L
10503match_group_member (asection *sec, asection *group,
10504 struct bfd_link_info *info)
3d7f7666
L
10505{
10506 asection *first = elf_next_in_group (group);
10507 asection *s = first;
10508
10509 while (s != NULL)
10510 {
c0f00686 10511 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10512 return s;
10513
83180ade 10514 s = elf_next_in_group (s);
3d7f7666
L
10515 if (s == first)
10516 break;
10517 }
10518
10519 return NULL;
10520}
10521
01b3c8ab 10522/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10523 to replace it. Return the replacement if it is OK. Otherwise return
10524 NULL. */
01b3c8ab
L
10525
10526asection *
c0f00686 10527_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10528{
10529 asection *kept;
10530
10531 kept = sec->kept_section;
10532 if (kept != NULL)
10533 {
c2370991 10534 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10535 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10536 if (kept != NULL
10537 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10538 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10539 kept = NULL;
c2370991 10540 sec->kept_section = kept;
01b3c8ab
L
10541 }
10542 return kept;
10543}
10544
c152c796
AM
10545/* Link an input file into the linker output file. This function
10546 handles all the sections and relocations of the input file at once.
10547 This is so that we only have to read the local symbols once, and
10548 don't have to keep them in memory. */
10549
10550static bfd_boolean
8b127cbc 10551elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10552{
ece5ef60 10553 int (*relocate_section)
c152c796
AM
10554 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10555 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10556 bfd *output_bfd;
10557 Elf_Internal_Shdr *symtab_hdr;
10558 size_t locsymcount;
10559 size_t extsymoff;
10560 Elf_Internal_Sym *isymbuf;
10561 Elf_Internal_Sym *isym;
10562 Elf_Internal_Sym *isymend;
10563 long *pindex;
10564 asection **ppsection;
10565 asection *o;
10566 const struct elf_backend_data *bed;
c152c796 10567 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10568 bfd_size_type address_size;
10569 bfd_vma r_type_mask;
10570 int r_sym_shift;
ffbc01cc 10571 bfd_boolean have_file_sym = FALSE;
c152c796 10572
8b127cbc 10573 output_bfd = flinfo->output_bfd;
c152c796
AM
10574 bed = get_elf_backend_data (output_bfd);
10575 relocate_section = bed->elf_backend_relocate_section;
10576
10577 /* If this is a dynamic object, we don't want to do anything here:
10578 we don't want the local symbols, and we don't want the section
10579 contents. */
10580 if ((input_bfd->flags & DYNAMIC) != 0)
10581 return TRUE;
10582
c152c796
AM
10583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10584 if (elf_bad_symtab (input_bfd))
10585 {
10586 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10587 extsymoff = 0;
10588 }
10589 else
10590 {
10591 locsymcount = symtab_hdr->sh_info;
10592 extsymoff = symtab_hdr->sh_info;
10593 }
10594
10595 /* Read the local symbols. */
10596 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10597 if (isymbuf == NULL && locsymcount != 0)
10598 {
10599 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10600 flinfo->internal_syms,
10601 flinfo->external_syms,
10602 flinfo->locsym_shndx);
c152c796
AM
10603 if (isymbuf == NULL)
10604 return FALSE;
10605 }
10606
10607 /* Find local symbol sections and adjust values of symbols in
10608 SEC_MERGE sections. Write out those local symbols we know are
10609 going into the output file. */
10610 isymend = isymbuf + locsymcount;
8b127cbc 10611 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10612 isym < isymend;
10613 isym++, pindex++, ppsection++)
10614 {
10615 asection *isec;
10616 const char *name;
10617 Elf_Internal_Sym osym;
6e0b88f1
AM
10618 long indx;
10619 int ret;
c152c796
AM
10620
10621 *pindex = -1;
10622
10623 if (elf_bad_symtab (input_bfd))
10624 {
10625 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10626 {
10627 *ppsection = NULL;
10628 continue;
10629 }
10630 }
10631
10632 if (isym->st_shndx == SHN_UNDEF)
10633 isec = bfd_und_section_ptr;
c152c796
AM
10634 else if (isym->st_shndx == SHN_ABS)
10635 isec = bfd_abs_section_ptr;
10636 else if (isym->st_shndx == SHN_COMMON)
10637 isec = bfd_com_section_ptr;
10638 else
10639 {
cb33740c
AM
10640 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10641 if (isec == NULL)
10642 {
10643 /* Don't attempt to output symbols with st_shnx in the
10644 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10645 isec = bfd_und_section_ptr;
cb33740c 10646 }
dbaa2011 10647 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10648 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10649 isym->st_value =
10650 _bfd_merged_section_offset (output_bfd, &isec,
10651 elf_section_data (isec)->sec_info,
10652 isym->st_value);
c152c796
AM
10653 }
10654
10655 *ppsection = isec;
10656
d983c8c5
AM
10657 /* Don't output the first, undefined, symbol. In fact, don't
10658 output any undefined local symbol. */
10659 if (isec == bfd_und_section_ptr)
c152c796
AM
10660 continue;
10661
10662 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10663 {
10664 /* We never output section symbols. Instead, we use the
10665 section symbol of the corresponding section in the output
10666 file. */
10667 continue;
10668 }
10669
10670 /* If we are stripping all symbols, we don't want to output this
10671 one. */
8b127cbc 10672 if (flinfo->info->strip == strip_all)
c152c796
AM
10673 continue;
10674
10675 /* If we are discarding all local symbols, we don't want to
10676 output this one. If we are generating a relocatable output
10677 file, then some of the local symbols may be required by
10678 relocs; we output them below as we discover that they are
10679 needed. */
8b127cbc 10680 if (flinfo->info->discard == discard_all)
c152c796
AM
10681 continue;
10682
10683 /* If this symbol is defined in a section which we are
f02571c5 10684 discarding, we don't need to keep it. */
abf874aa
CL
10685 if (isym->st_shndx != SHN_UNDEF
10686 && isym->st_shndx < SHN_LORESERVE
10687 && isec->output_section == NULL
10688 && flinfo->info->non_contiguous_regions
10689 && flinfo->info->non_contiguous_regions_warnings)
10690 {
10691 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10692 "discards section `%s' from '%s'\n"),
765cf5f6 10693 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10694 continue;
10695 }
10696
f02571c5 10697 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10698 && isym->st_shndx < SHN_LORESERVE
10699 && bfd_section_removed_from_list (output_bfd,
10700 isec->output_section))
e75a280b
L
10701 continue;
10702
c152c796
AM
10703 /* Get the name of the symbol. */
10704 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10705 isym->st_name);
10706 if (name == NULL)
10707 return FALSE;
10708
10709 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10710 if ((flinfo->info->strip == strip_some
10711 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10712 == NULL))
8b127cbc 10713 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10714 && (isec->flags & SEC_MERGE)
10715 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10716 || flinfo->info->discard == discard_l)
c152c796
AM
10717 && bfd_is_local_label_name (input_bfd, name)))
10718 continue;
10719
ffbc01cc
AM
10720 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10721 {
ce875075
AM
10722 if (input_bfd->lto_output)
10723 /* -flto puts a temp file name here. This means builds
10724 are not reproducible. Discard the symbol. */
10725 continue;
ffbc01cc
AM
10726 have_file_sym = TRUE;
10727 flinfo->filesym_count += 1;
10728 }
10729 if (!have_file_sym)
10730 {
10731 /* In the absence of debug info, bfd_find_nearest_line uses
10732 FILE symbols to determine the source file for local
10733 function symbols. Provide a FILE symbol here if input
10734 files lack such, so that their symbols won't be
10735 associated with a previous input file. It's not the
10736 source file, but the best we can do. */
10737 have_file_sym = TRUE;
10738 flinfo->filesym_count += 1;
10739 memset (&osym, 0, sizeof (osym));
10740 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10741 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10742 if (!elf_link_output_symstrtab (flinfo,
10743 (input_bfd->lto_output ? NULL
765cf5f6 10744 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10745 &osym, bfd_abs_section_ptr,
10746 NULL))
ffbc01cc
AM
10747 return FALSE;
10748 }
10749
c152c796
AM
10750 osym = *isym;
10751
10752 /* Adjust the section index for the output file. */
10753 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10754 isec->output_section);
10755 if (osym.st_shndx == SHN_BAD)
10756 return FALSE;
10757
c152c796
AM
10758 /* ELF symbols in relocatable files are section relative, but
10759 in executable files they are virtual addresses. Note that
10760 this code assumes that all ELF sections have an associated
10761 BFD section with a reasonable value for output_offset; below
10762 we assume that they also have a reasonable value for
10763 output_section. Any special sections must be set up to meet
10764 these requirements. */
10765 osym.st_value += isec->output_offset;
0e1862bb 10766 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10767 {
10768 osym.st_value += isec->output_section->vma;
10769 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10770 {
10771 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10772 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10773 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10774 else
10775 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10776 STT_NOTYPE);
c152c796
AM
10777 }
10778 }
10779
6e0b88f1 10780 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10781 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10782 if (ret == 0)
c152c796 10783 return FALSE;
6e0b88f1
AM
10784 else if (ret == 1)
10785 *pindex = indx;
c152c796
AM
10786 }
10787
310fd250
L
10788 if (bed->s->arch_size == 32)
10789 {
10790 r_type_mask = 0xff;
10791 r_sym_shift = 8;
10792 address_size = 4;
10793 }
10794 else
10795 {
10796 r_type_mask = 0xffffffff;
10797 r_sym_shift = 32;
10798 address_size = 8;
10799 }
10800
c152c796
AM
10801 /* Relocate the contents of each section. */
10802 sym_hashes = elf_sym_hashes (input_bfd);
10803 for (o = input_bfd->sections; o != NULL; o = o->next)
10804 {
10805 bfd_byte *contents;
10806
10807 if (! o->linker_mark)
10808 {
10809 /* This section was omitted from the link. */
10810 continue;
10811 }
10812
7bdf4127 10813 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10814 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10815 {
10816 /* Deal with the group signature symbol. */
10817 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10818 unsigned long symndx = sec_data->this_hdr.sh_info;
10819 asection *osec = o->output_section;
10820
7bdf4127 10821 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10822 if (symndx >= locsymcount
10823 || (elf_bad_symtab (input_bfd)
8b127cbc 10824 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10825 {
10826 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10827 while (h->root.type == bfd_link_hash_indirect
10828 || h->root.type == bfd_link_hash_warning)
10829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10830 /* Arrange for symbol to be output. */
10831 h->indx = -2;
10832 elf_section_data (osec)->this_hdr.sh_info = -2;
10833 }
10834 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10835 {
10836 /* We'll use the output section target_index. */
8b127cbc 10837 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10838 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10839 }
10840 else
10841 {
8b127cbc 10842 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10843 {
10844 /* Otherwise output the local symbol now. */
10845 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10846 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10847 const char *name;
6e0b88f1
AM
10848 long indx;
10849 int ret;
bcacc0f5
AM
10850
10851 name = bfd_elf_string_from_elf_section (input_bfd,
10852 symtab_hdr->sh_link,
10853 sym.st_name);
10854 if (name == NULL)
10855 return FALSE;
10856
10857 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10858 sec);
10859 if (sym.st_shndx == SHN_BAD)
10860 return FALSE;
10861
10862 sym.st_value += o->output_offset;
10863
6e0b88f1 10864 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10865 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10866 NULL);
6e0b88f1 10867 if (ret == 0)
bcacc0f5 10868 return FALSE;
6e0b88f1 10869 else if (ret == 1)
8b127cbc 10870 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10871 else
10872 abort ();
bcacc0f5
AM
10873 }
10874 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10875 = flinfo->indices[symndx];
bcacc0f5
AM
10876 }
10877 }
10878
c152c796 10879 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10880 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10881 continue;
10882
10883 if ((o->flags & SEC_LINKER_CREATED) != 0)
10884 {
10885 /* Section was created by _bfd_elf_link_create_dynamic_sections
10886 or somesuch. */
10887 continue;
10888 }
10889
10890 /* Get the contents of the section. They have been cached by a
10891 relaxation routine. Note that o is a section in an input
10892 file, so the contents field will not have been set by any of
10893 the routines which work on output files. */
10894 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10895 {
10896 contents = elf_section_data (o)->this_hdr.contents;
10897 if (bed->caches_rawsize
10898 && o->rawsize != 0
10899 && o->rawsize < o->size)
10900 {
10901 memcpy (flinfo->contents, contents, o->rawsize);
10902 contents = flinfo->contents;
10903 }
10904 }
c152c796
AM
10905 else
10906 {
8b127cbc 10907 contents = flinfo->contents;
4a114e3e 10908 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10909 return FALSE;
10910 }
10911
10912 if ((o->flags & SEC_RELOC) != 0)
10913 {
10914 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10915 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10916 int action_discarded;
ece5ef60 10917 int ret;
c152c796
AM
10918
10919 /* Get the swapped relocs. */
10920 internal_relocs
8b127cbc
AM
10921 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10922 flinfo->internal_relocs, FALSE);
c152c796
AM
10923 if (internal_relocs == NULL
10924 && o->reloc_count > 0)
10925 return FALSE;
10926
310fd250
L
10927 /* We need to reverse-copy input .ctors/.dtors sections if
10928 they are placed in .init_array/.finit_array for output. */
10929 if (o->size > address_size
10930 && ((strncmp (o->name, ".ctors", 6) == 0
10931 && strcmp (o->output_section->name,
10932 ".init_array") == 0)
10933 || (strncmp (o->name, ".dtors", 6) == 0
10934 && strcmp (o->output_section->name,
10935 ".fini_array") == 0))
10936 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10937 {
056bafd4
MR
10938 if (o->size * bed->s->int_rels_per_ext_rel
10939 != o->reloc_count * address_size)
310fd250 10940 {
4eca0228 10941 _bfd_error_handler
695344c0 10942 /* xgettext:c-format */
871b3ab2 10943 (_("error: %pB: size of section %pA is not "
310fd250
L
10944 "multiple of address size"),
10945 input_bfd, o);
8c6716e5 10946 bfd_set_error (bfd_error_bad_value);
310fd250
L
10947 return FALSE;
10948 }
10949 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10950 }
10951
0f02bbd9 10952 action_discarded = -1;
c152c796 10953 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10954 action_discarded = (*bed->action_discarded) (o);
10955
10956 /* Run through the relocs evaluating complex reloc symbols and
10957 looking for relocs against symbols from discarded sections
10958 or section symbols from removed link-once sections.
10959 Complain about relocs against discarded sections. Zero
10960 relocs against removed link-once sections. */
10961
10962 rel = internal_relocs;
056bafd4 10963 relend = rel + o->reloc_count;
0f02bbd9 10964 for ( ; rel < relend; rel++)
c152c796 10965 {
0f02bbd9
AM
10966 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10967 unsigned int s_type;
10968 asection **ps, *sec;
10969 struct elf_link_hash_entry *h = NULL;
10970 const char *sym_name;
c152c796 10971
0f02bbd9
AM
10972 if (r_symndx == STN_UNDEF)
10973 continue;
c152c796 10974
0f02bbd9
AM
10975 if (r_symndx >= locsymcount
10976 || (elf_bad_symtab (input_bfd)
8b127cbc 10977 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10978 {
10979 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10980
0f02bbd9
AM
10981 /* Badly formatted input files can contain relocs that
10982 reference non-existant symbols. Check here so that
10983 we do not seg fault. */
10984 if (h == NULL)
c152c796 10985 {
4eca0228 10986 _bfd_error_handler
695344c0 10987 /* xgettext:c-format */
2dcf00ce 10988 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10989 "that references a non-existent global symbol"),
2dcf00ce 10990 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10991 bfd_set_error (bfd_error_bad_value);
10992 return FALSE;
10993 }
3b36f7e6 10994
0f02bbd9
AM
10995 while (h->root.type == bfd_link_hash_indirect
10996 || h->root.type == bfd_link_hash_warning)
10997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10998
0f02bbd9 10999 s_type = h->type;
cdd3575c 11000
9e2dec47 11001 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11002 mark the symbol as undefined. Note that the
11003 linker may attach linker created dynamic sections
11004 to the plugin bfd. Symbols defined in linker
11005 created sections are not plugin symbols. */
bc4e12de 11006 if ((h->root.non_ir_ref_regular
4070765b 11007 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11008 && (h->root.type == bfd_link_hash_defined
11009 || h->root.type == bfd_link_hash_defweak)
11010 && (h->root.u.def.section->flags
11011 & SEC_LINKER_CREATED) == 0
11012 && h->root.u.def.section->owner != NULL
11013 && (h->root.u.def.section->owner->flags
11014 & BFD_PLUGIN) != 0)
11015 {
11016 h->root.type = bfd_link_hash_undefined;
11017 h->root.u.undef.abfd = h->root.u.def.section->owner;
11018 }
11019
0f02bbd9
AM
11020 ps = NULL;
11021 if (h->root.type == bfd_link_hash_defined
11022 || h->root.type == bfd_link_hash_defweak)
11023 ps = &h->root.u.def.section;
11024
11025 sym_name = h->root.root.string;
11026 }
11027 else
11028 {
11029 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11030
11031 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11032 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11033 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11034 sym, *ps);
11035 }
c152c796 11036
c301e700 11037 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11038 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11039 {
11040 bfd_vma val;
11041 bfd_vma dot = (rel->r_offset
11042 + o->output_offset + o->output_section->vma);
11043#ifdef DEBUG
11044 printf ("Encountered a complex symbol!");
11045 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11046 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11047 (long) (rel - internal_relocs));
0f02bbd9
AM
11048 printf (" symbol: idx %8.8lx, name %s\n",
11049 r_symndx, sym_name);
11050 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11051 (unsigned long) rel->r_info,
11052 (unsigned long) rel->r_offset);
11053#endif
8b127cbc 11054 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11055 isymbuf, locsymcount, s_type == STT_SRELC))
11056 return FALSE;
11057
11058 /* Symbol evaluated OK. Update to absolute value. */
11059 set_symbol_value (input_bfd, isymbuf, locsymcount,
11060 r_symndx, val);
11061 continue;
11062 }
11063
11064 if (action_discarded != -1 && ps != NULL)
11065 {
cdd3575c
AM
11066 /* Complain if the definition comes from a
11067 discarded section. */
dbaa2011 11068 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11069 {
cf35638d 11070 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11071 if (action_discarded & COMPLAIN)
8b127cbc 11072 (*flinfo->info->callbacks->einfo)
695344c0 11073 /* xgettext:c-format */
871b3ab2
AM
11074 (_("%X`%s' referenced in section `%pA' of %pB: "
11075 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11076 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11077
87e5235d 11078 /* Try to do the best we can to support buggy old
e0ae6d6f 11079 versions of gcc. Pretend that the symbol is
87e5235d
AM
11080 really defined in the kept linkonce section.
11081 FIXME: This is quite broken. Modifying the
11082 symbol here means we will be changing all later
e0ae6d6f 11083 uses of the symbol, not just in this section. */
0f02bbd9 11084 if (action_discarded & PRETEND)
87e5235d 11085 {
01b3c8ab
L
11086 asection *kept;
11087
c0f00686 11088 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11089 flinfo->info);
01b3c8ab 11090 if (kept != NULL)
87e5235d
AM
11091 {
11092 *ps = kept;
11093 continue;
11094 }
11095 }
c152c796
AM
11096 }
11097 }
11098 }
11099
11100 /* Relocate the section by invoking a back end routine.
11101
11102 The back end routine is responsible for adjusting the
11103 section contents as necessary, and (if using Rela relocs
11104 and generating a relocatable output file) adjusting the
11105 reloc addend as necessary.
11106
11107 The back end routine does not have to worry about setting
11108 the reloc address or the reloc symbol index.
11109
11110 The back end routine is given a pointer to the swapped in
11111 internal symbols, and can access the hash table entries
11112 for the external symbols via elf_sym_hashes (input_bfd).
11113
11114 When generating relocatable output, the back end routine
11115 must handle STB_LOCAL/STT_SECTION symbols specially. The
11116 output symbol is going to be a section symbol
11117 corresponding to the output section, which will require
11118 the addend to be adjusted. */
11119
8b127cbc 11120 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11121 input_bfd, o, contents,
11122 internal_relocs,
11123 isymbuf,
8b127cbc 11124 flinfo->sections);
ece5ef60 11125 if (!ret)
c152c796
AM
11126 return FALSE;
11127
ece5ef60 11128 if (ret == 2
0e1862bb 11129 || bfd_link_relocatable (flinfo->info)
8b127cbc 11130 || flinfo->info->emitrelocations)
c152c796
AM
11131 {
11132 Elf_Internal_Rela *irela;
d4730f92 11133 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11134 bfd_vma last_offset;
11135 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11136 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11137 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11138 unsigned int next_erel;
c152c796 11139 bfd_boolean rela_normal;
d4730f92 11140 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11141
d4730f92
BS
11142 esdi = elf_section_data (o);
11143 esdo = elf_section_data (o->output_section);
11144 rela_normal = FALSE;
c152c796
AM
11145
11146 /* Adjust the reloc addresses and symbol indices. */
11147
11148 irela = internal_relocs;
056bafd4 11149 irelaend = irela + o->reloc_count;
d4730f92
BS
11150 rel_hash = esdo->rel.hashes + esdo->rel.count;
11151 /* We start processing the REL relocs, if any. When we reach
11152 IRELAMID in the loop, we switch to the RELA relocs. */
11153 irelamid = irela;
11154 if (esdi->rel.hdr != NULL)
11155 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11156 * bed->s->int_rels_per_ext_rel);
eac338cf 11157 rel_hash_list = rel_hash;
d4730f92 11158 rela_hash_list = NULL;
c152c796 11159 last_offset = o->output_offset;
0e1862bb 11160 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11161 last_offset += o->output_section->vma;
11162 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11163 {
11164 unsigned long r_symndx;
11165 asection *sec;
11166 Elf_Internal_Sym sym;
11167
11168 if (next_erel == bed->s->int_rels_per_ext_rel)
11169 {
11170 rel_hash++;
11171 next_erel = 0;
11172 }
11173
d4730f92
BS
11174 if (irela == irelamid)
11175 {
11176 rel_hash = esdo->rela.hashes + esdo->rela.count;
11177 rela_hash_list = rel_hash;
11178 rela_normal = bed->rela_normal;
11179 }
11180
c152c796 11181 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11182 flinfo->info, o,
c152c796
AM
11183 irela->r_offset);
11184 if (irela->r_offset >= (bfd_vma) -2)
11185 {
11186 /* This is a reloc for a deleted entry or somesuch.
11187 Turn it into an R_*_NONE reloc, at the same
11188 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11189 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11190 being ordered. */
11191 irela->r_offset = last_offset;
11192 irela->r_info = 0;
11193 irela->r_addend = 0;
11194 continue;
11195 }
11196
11197 irela->r_offset += o->output_offset;
11198
11199 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11200 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11201 irela->r_offset += o->output_section->vma;
11202
11203 last_offset = irela->r_offset;
11204
11205 r_symndx = irela->r_info >> r_sym_shift;
11206 if (r_symndx == STN_UNDEF)
11207 continue;
11208
11209 if (r_symndx >= locsymcount
11210 || (elf_bad_symtab (input_bfd)
8b127cbc 11211 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11212 {
11213 struct elf_link_hash_entry *rh;
11214 unsigned long indx;
11215
11216 /* This is a reloc against a global symbol. We
11217 have not yet output all the local symbols, so
11218 we do not know the symbol index of any global
11219 symbol. We set the rel_hash entry for this
11220 reloc to point to the global hash table entry
11221 for this symbol. The symbol index is then
ee75fd95 11222 set at the end of bfd_elf_final_link. */
c152c796
AM
11223 indx = r_symndx - extsymoff;
11224 rh = elf_sym_hashes (input_bfd)[indx];
11225 while (rh->root.type == bfd_link_hash_indirect
11226 || rh->root.type == bfd_link_hash_warning)
11227 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11228
11229 /* Setting the index to -2 tells
11230 elf_link_output_extsym that this symbol is
11231 used by a reloc. */
11232 BFD_ASSERT (rh->indx < 0);
11233 rh->indx = -2;
c152c796
AM
11234 *rel_hash = rh;
11235
11236 continue;
11237 }
11238
11239 /* This is a reloc against a local symbol. */
11240
11241 *rel_hash = NULL;
11242 sym = isymbuf[r_symndx];
8b127cbc 11243 sec = flinfo->sections[r_symndx];
c152c796
AM
11244 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11245 {
11246 /* I suppose the backend ought to fill in the
11247 section of any STT_SECTION symbol against a
6a8d1586 11248 processor specific section. */
cf35638d 11249 r_symndx = STN_UNDEF;
6a8d1586
AM
11250 if (bfd_is_abs_section (sec))
11251 ;
c152c796
AM
11252 else if (sec == NULL || sec->owner == NULL)
11253 {
11254 bfd_set_error (bfd_error_bad_value);
11255 return FALSE;
11256 }
11257 else
11258 {
6a8d1586
AM
11259 asection *osec = sec->output_section;
11260
11261 /* If we have discarded a section, the output
11262 section will be the absolute section. In
ab96bf03
AM
11263 case of discarded SEC_MERGE sections, use
11264 the kept section. relocate_section should
11265 have already handled discarded linkonce
11266 sections. */
6a8d1586
AM
11267 if (bfd_is_abs_section (osec)
11268 && sec->kept_section != NULL
11269 && sec->kept_section->output_section != NULL)
11270 {
11271 osec = sec->kept_section->output_section;
11272 irela->r_addend -= osec->vma;
11273 }
11274
11275 if (!bfd_is_abs_section (osec))
11276 {
11277 r_symndx = osec->target_index;
cf35638d 11278 if (r_symndx == STN_UNDEF)
74541ad4 11279 {
051d833a
AM
11280 irela->r_addend += osec->vma;
11281 osec = _bfd_nearby_section (output_bfd, osec,
11282 osec->vma);
11283 irela->r_addend -= osec->vma;
11284 r_symndx = osec->target_index;
74541ad4 11285 }
6a8d1586 11286 }
c152c796
AM
11287 }
11288
11289 /* Adjust the addend according to where the
11290 section winds up in the output section. */
11291 if (rela_normal)
11292 irela->r_addend += sec->output_offset;
11293 }
11294 else
11295 {
8b127cbc 11296 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11297 {
11298 unsigned long shlink;
11299 const char *name;
11300 asection *osec;
6e0b88f1 11301 long indx;
c152c796 11302
8b127cbc 11303 if (flinfo->info->strip == strip_all)
c152c796
AM
11304 {
11305 /* You can't do ld -r -s. */
11306 bfd_set_error (bfd_error_invalid_operation);
11307 return FALSE;
11308 }
11309
11310 /* This symbol was skipped earlier, but
11311 since it is needed by a reloc, we
11312 must output it now. */
11313 shlink = symtab_hdr->sh_link;
11314 name = (bfd_elf_string_from_elf_section
11315 (input_bfd, shlink, sym.st_name));
11316 if (name == NULL)
11317 return FALSE;
11318
11319 osec = sec->output_section;
11320 sym.st_shndx =
11321 _bfd_elf_section_from_bfd_section (output_bfd,
11322 osec);
11323 if (sym.st_shndx == SHN_BAD)
11324 return FALSE;
11325
11326 sym.st_value += sec->output_offset;
0e1862bb 11327 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11328 {
11329 sym.st_value += osec->vma;
11330 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11331 {
102def4d
AM
11332 struct elf_link_hash_table *htab
11333 = elf_hash_table (flinfo->info);
11334
c152c796
AM
11335 /* STT_TLS symbols are relative to PT_TLS
11336 segment base. */
102def4d
AM
11337 if (htab->tls_sec != NULL)
11338 sym.st_value -= htab->tls_sec->vma;
11339 else
11340 sym.st_info
11341 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11342 STT_NOTYPE);
c152c796
AM
11343 }
11344 }
11345
6e0b88f1 11346 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11347 ret = elf_link_output_symstrtab (flinfo, name,
11348 &sym, sec,
11349 NULL);
6e0b88f1 11350 if (ret == 0)
c152c796 11351 return FALSE;
6e0b88f1 11352 else if (ret == 1)
8b127cbc 11353 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11354 else
11355 abort ();
c152c796
AM
11356 }
11357
8b127cbc 11358 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11359 }
11360
11361 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11362 | (irela->r_info & r_type_mask));
11363 }
11364
11365 /* Swap out the relocs. */
d4730f92
BS
11366 input_rel_hdr = esdi->rel.hdr;
11367 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11368 {
d4730f92
BS
11369 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11370 input_rel_hdr,
11371 internal_relocs,
11372 rel_hash_list))
11373 return FALSE;
c152c796
AM
11374 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11375 * bed->s->int_rels_per_ext_rel);
eac338cf 11376 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11377 }
11378
11379 input_rela_hdr = esdi->rela.hdr;
11380 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11381 {
eac338cf 11382 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11383 input_rela_hdr,
eac338cf 11384 internal_relocs,
d4730f92 11385 rela_hash_list))
c152c796
AM
11386 return FALSE;
11387 }
11388 }
11389 }
11390
11391 /* Write out the modified section contents. */
11392 if (bed->elf_backend_write_section
8b127cbc 11393 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11394 contents))
c152c796
AM
11395 {
11396 /* Section written out. */
11397 }
11398 else switch (o->sec_info_type)
11399 {
dbaa2011 11400 case SEC_INFO_TYPE_STABS:
c152c796
AM
11401 if (! (_bfd_write_section_stabs
11402 (output_bfd,
8b127cbc 11403 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11404 o, &elf_section_data (o)->sec_info, contents)))
11405 return FALSE;
11406 break;
dbaa2011 11407 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11408 if (! _bfd_write_merged_section (output_bfd, o,
11409 elf_section_data (o)->sec_info))
11410 return FALSE;
11411 break;
dbaa2011 11412 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11413 {
8b127cbc 11414 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11415 o, contents))
11416 return FALSE;
11417 }
11418 break;
2f0c68f2
CM
11419 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11420 {
11421 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11422 flinfo->info,
11423 o, contents))
11424 return FALSE;
11425 }
11426 break;
c152c796
AM
11427 default:
11428 {
310fd250
L
11429 if (! (o->flags & SEC_EXCLUDE))
11430 {
11431 file_ptr offset = (file_ptr) o->output_offset;
11432 bfd_size_type todo = o->size;
37b01f6a 11433
bb294208 11434 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11435
310fd250
L
11436 if ((o->flags & SEC_ELF_REVERSE_COPY))
11437 {
11438 /* Reverse-copy input section to output. */
11439 do
11440 {
11441 todo -= address_size;
11442 if (! bfd_set_section_contents (output_bfd,
11443 o->output_section,
11444 contents + todo,
11445 offset,
11446 address_size))
11447 return FALSE;
11448 if (todo == 0)
11449 break;
11450 offset += address_size;
11451 }
11452 while (1);
11453 }
11454 else if (! bfd_set_section_contents (output_bfd,
11455 o->output_section,
11456 contents,
11457 offset, todo))
11458 return FALSE;
11459 }
c152c796
AM
11460 }
11461 break;
11462 }
11463 }
11464
11465 return TRUE;
11466}
11467
11468/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11469 requested by the linker, and does not come from any input file. This
c152c796
AM
11470 is used to build constructor and destructor tables when linking
11471 with -Ur. */
11472
11473static bfd_boolean
11474elf_reloc_link_order (bfd *output_bfd,
11475 struct bfd_link_info *info,
11476 asection *output_section,
11477 struct bfd_link_order *link_order)
11478{
11479 reloc_howto_type *howto;
11480 long indx;
11481 bfd_vma offset;
11482 bfd_vma addend;
d4730f92 11483 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11484 struct elf_link_hash_entry **rel_hash_ptr;
11485 Elf_Internal_Shdr *rel_hdr;
11486 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11487 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11488 bfd_byte *erel;
11489 unsigned int i;
d4730f92 11490 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11491
11492 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11493 if (howto == NULL)
11494 {
11495 bfd_set_error (bfd_error_bad_value);
11496 return FALSE;
11497 }
11498
11499 addend = link_order->u.reloc.p->addend;
11500
d4730f92
BS
11501 if (esdo->rel.hdr)
11502 reldata = &esdo->rel;
11503 else if (esdo->rela.hdr)
11504 reldata = &esdo->rela;
11505 else
11506 {
11507 reldata = NULL;
11508 BFD_ASSERT (0);
11509 }
11510
c152c796 11511 /* Figure out the symbol index. */
d4730f92 11512 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11513 if (link_order->type == bfd_section_reloc_link_order)
11514 {
11515 indx = link_order->u.reloc.p->u.section->target_index;
11516 BFD_ASSERT (indx != 0);
11517 *rel_hash_ptr = NULL;
11518 }
11519 else
11520 {
11521 struct elf_link_hash_entry *h;
11522
11523 /* Treat a reloc against a defined symbol as though it were
11524 actually against the section. */
11525 h = ((struct elf_link_hash_entry *)
11526 bfd_wrapped_link_hash_lookup (output_bfd, info,
11527 link_order->u.reloc.p->u.name,
11528 FALSE, FALSE, TRUE));
11529 if (h != NULL
11530 && (h->root.type == bfd_link_hash_defined
11531 || h->root.type == bfd_link_hash_defweak))
11532 {
11533 asection *section;
11534
11535 section = h->root.u.def.section;
11536 indx = section->output_section->target_index;
11537 *rel_hash_ptr = NULL;
11538 /* It seems that we ought to add the symbol value to the
11539 addend here, but in practice it has already been added
11540 because it was passed to constructor_callback. */
11541 addend += section->output_section->vma + section->output_offset;
11542 }
11543 else if (h != NULL)
11544 {
11545 /* Setting the index to -2 tells elf_link_output_extsym that
11546 this symbol is used by a reloc. */
11547 h->indx = -2;
11548 *rel_hash_ptr = h;
11549 indx = 0;
11550 }
11551 else
11552 {
1a72702b
AM
11553 (*info->callbacks->unattached_reloc)
11554 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11555 indx = 0;
11556 }
11557 }
11558
11559 /* If this is an inplace reloc, we must write the addend into the
11560 object file. */
11561 if (howto->partial_inplace && addend != 0)
11562 {
11563 bfd_size_type size;
11564 bfd_reloc_status_type rstat;
11565 bfd_byte *buf;
11566 bfd_boolean ok;
11567 const char *sym_name;
bb294208 11568 bfd_size_type octets;
c152c796 11569
a50b1753
NC
11570 size = (bfd_size_type) bfd_get_reloc_size (howto);
11571 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11572 if (buf == NULL && size != 0)
c152c796
AM
11573 return FALSE;
11574 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11575 switch (rstat)
11576 {
11577 case bfd_reloc_ok:
11578 break;
11579
11580 default:
11581 case bfd_reloc_outofrange:
11582 abort ();
11583
11584 case bfd_reloc_overflow:
11585 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11586 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11587 else
11588 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11589 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11590 howto->name, addend, NULL, NULL,
11591 (bfd_vma) 0);
c152c796
AM
11592 break;
11593 }
37b01f6a 11594
bb294208
AM
11595 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11596 output_section);
c152c796 11597 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11598 octets, size);
c152c796
AM
11599 free (buf);
11600 if (! ok)
11601 return FALSE;
11602 }
11603
11604 /* The address of a reloc is relative to the section in a
11605 relocatable file, and is a virtual address in an executable
11606 file. */
11607 offset = link_order->offset;
0e1862bb 11608 if (! bfd_link_relocatable (info))
c152c796
AM
11609 offset += output_section->vma;
11610
11611 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11612 {
11613 irel[i].r_offset = offset;
11614 irel[i].r_info = 0;
11615 irel[i].r_addend = 0;
11616 }
11617 if (bed->s->arch_size == 32)
11618 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11619 else
11620 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11621
d4730f92 11622 rel_hdr = reldata->hdr;
c152c796
AM
11623 erel = rel_hdr->contents;
11624 if (rel_hdr->sh_type == SHT_REL)
11625 {
d4730f92 11626 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11627 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11628 }
11629 else
11630 {
11631 irel[0].r_addend = addend;
d4730f92 11632 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11633 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11634 }
11635
d4730f92 11636 ++reldata->count;
c152c796
AM
11637
11638 return TRUE;
11639}
11640
0b52efa6 11641
0b52efa6
PB
11642/* Compare two sections based on the locations of the sections they are
11643 linked to. Used by elf_fixup_link_order. */
11644
11645static int
8c1c5e5d 11646compare_link_order (const void *a, const void *b)
0b52efa6 11647{
8c1c5e5d
AM
11648 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11649 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11650 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11651 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11652 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11653 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11654
0b52efa6
PB
11655 if (apos < bpos)
11656 return -1;
8c1c5e5d
AM
11657 if (apos > bpos)
11658 return 1;
11659
11660 /* The only way we should get matching LMAs is when the first of two
11661 sections has zero size. */
11662 if (asec->size < bsec->size)
11663 return -1;
11664 if (asec->size > bsec->size)
11665 return 1;
11666
11667 /* If they are both zero size then they almost certainly have the same
11668 VMA and thus are not ordered with respect to each other. Test VMA
11669 anyway, and fall back to id to make the result reproducible across
11670 qsort implementations. */
11671 apos = asec->output_section->vma + asec->output_offset;
11672 bpos = bsec->output_section->vma + bsec->output_offset;
11673 if (apos < bpos)
11674 return -1;
11675 if (apos > bpos)
11676 return 1;
11677
11678 return asec->id - bsec->id;
0b52efa6
PB
11679}
11680
11681
11682/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11683 order as their linked sections. Returns false if this could not be done
11684 because an output section includes both ordered and unordered
11685 sections. Ideally we'd do this in the linker proper. */
11686
11687static bfd_boolean
11688elf_fixup_link_order (bfd *abfd, asection *o)
11689{
8c1c5e5d
AM
11690 size_t seen_linkorder;
11691 size_t seen_other;
11692 size_t n;
0b52efa6
PB
11693 struct bfd_link_order *p;
11694 bfd *sub;
0b52efa6 11695 struct bfd_link_order **sections;
66631823
CE
11696 asection *other_sec, *linkorder_sec;
11697 bfd_vma offset; /* Octets. */
3b36f7e6 11698
d33cdfe3
L
11699 other_sec = NULL;
11700 linkorder_sec = NULL;
0b52efa6
PB
11701 seen_other = 0;
11702 seen_linkorder = 0;
8423293d 11703 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11704 {
d33cdfe3 11705 if (p->type == bfd_indirect_link_order)
0b52efa6 11706 {
66631823 11707 asection *s = p->u.indirect.section;
d33cdfe3 11708 sub = s->owner;
847d5183
AM
11709 if ((s->flags & SEC_LINKER_CREATED) == 0
11710 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11711 && elf_section_data (s) != NULL
11712 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11713 {
11714 seen_linkorder++;
11715 linkorder_sec = s;
11716 }
0b52efa6 11717 else
d33cdfe3
L
11718 {
11719 seen_other++;
11720 other_sec = s;
11721 }
0b52efa6
PB
11722 }
11723 else
11724 seen_other++;
d33cdfe3
L
11725
11726 if (seen_other && seen_linkorder)
11727 {
11728 if (other_sec && linkorder_sec)
4eca0228 11729 _bfd_error_handler
695344c0 11730 /* xgettext:c-format */
871b3ab2
AM
11731 (_("%pA has both ordered [`%pA' in %pB] "
11732 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11733 o, linkorder_sec, linkorder_sec->owner,
11734 other_sec, other_sec->owner);
d33cdfe3 11735 else
4eca0228 11736 _bfd_error_handler
871b3ab2 11737 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11738 bfd_set_error (bfd_error_bad_value);
11739 return FALSE;
11740 }
0b52efa6
PB
11741 }
11742
11743 if (!seen_linkorder)
11744 return TRUE;
11745
8c1c5e5d 11746 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11747 if (sections == NULL)
11748 return FALSE;
3b36f7e6 11749
8c1c5e5d 11750 seen_linkorder = 0;
8423293d 11751 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11752 sections[seen_linkorder++] = p;
11753
0b52efa6 11754 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11755 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11756
11757 /* Change the offsets of the sections. */
11758 offset = 0;
11759 for (n = 0; n < seen_linkorder; n++)
11760 {
8c1c5e5d 11761 bfd_vma mask;
66631823
CE
11762 asection *s = sections[n]->u.indirect.section;
11763 unsigned int opb = bfd_octets_per_byte (abfd, s);
11764
11765 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11766 offset = (offset + ~mask) & mask;
66631823 11767 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11768 offset += sections[n]->size;
11769 }
11770
4dd07732 11771 free (sections);
0b52efa6
PB
11772 return TRUE;
11773}
11774
76359541
TP
11775/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11776 Returns TRUE upon success, FALSE otherwise. */
11777
11778static bfd_boolean
11779elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11780{
11781 bfd_boolean ret = FALSE;
11782 bfd *implib_bfd;
11783 const struct elf_backend_data *bed;
11784 flagword flags;
11785 enum bfd_architecture arch;
11786 unsigned int mach;
11787 asymbol **sympp = NULL;
11788 long symsize;
11789 long symcount;
11790 long src_count;
11791 elf_symbol_type *osymbuf;
446f7ed5 11792 size_t amt;
76359541
TP
11793
11794 implib_bfd = info->out_implib_bfd;
11795 bed = get_elf_backend_data (abfd);
11796
11797 if (!bfd_set_format (implib_bfd, bfd_object))
11798 return FALSE;
11799
046734ff 11800 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11801 flags = bfd_get_file_flags (abfd);
11802 flags &= ~HAS_RELOC;
11803 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11804 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11805 return FALSE;
11806
11807 /* Copy architecture of output file to import library file. */
11808 arch = bfd_get_arch (abfd);
11809 mach = bfd_get_mach (abfd);
11810 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11811 && (abfd->target_defaulted
11812 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11813 return FALSE;
11814
11815 /* Get symbol table size. */
11816 symsize = bfd_get_symtab_upper_bound (abfd);
11817 if (symsize < 0)
11818 return FALSE;
11819
11820 /* Read in the symbol table. */
ec9bd0a2
AM
11821 sympp = (asymbol **) bfd_malloc (symsize);
11822 if (sympp == NULL)
11823 return FALSE;
11824
76359541
TP
11825 symcount = bfd_canonicalize_symtab (abfd, sympp);
11826 if (symcount < 0)
11827 goto free_sym_buf;
11828
11829 /* Allow the BFD backend to copy any private header data it
11830 understands from the output BFD to the import library BFD. */
11831 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11832 goto free_sym_buf;
11833
11834 /* Filter symbols to appear in the import library. */
11835 if (bed->elf_backend_filter_implib_symbols)
11836 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11837 symcount);
11838 else
11839 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11840 if (symcount == 0)
11841 {
5df1bc57 11842 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11843 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11844 implib_bfd);
76359541
TP
11845 goto free_sym_buf;
11846 }
11847
11848
11849 /* Make symbols absolute. */
446f7ed5
AM
11850 amt = symcount * sizeof (*osymbuf);
11851 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11852 if (osymbuf == NULL)
11853 goto free_sym_buf;
11854
76359541
TP
11855 for (src_count = 0; src_count < symcount; src_count++)
11856 {
11857 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11858 sizeof (*osymbuf));
11859 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11860 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11861 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11862 osymbuf[src_count].internal_elf_sym.st_value =
11863 osymbuf[src_count].symbol.value;
11864 sympp[src_count] = &osymbuf[src_count].symbol;
11865 }
11866
11867 bfd_set_symtab (implib_bfd, sympp, symcount);
11868
11869 /* Allow the BFD backend to copy any private data it understands
11870 from the output BFD to the import library BFD. This is done last
11871 to permit the routine to look at the filtered symbol table. */
11872 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11873 goto free_sym_buf;
11874
11875 if (!bfd_close (implib_bfd))
11876 goto free_sym_buf;
11877
11878 ret = TRUE;
11879
dc1e8a47 11880 free_sym_buf:
76359541
TP
11881 free (sympp);
11882 return ret;
11883}
11884
9f7c3e5e
AM
11885static void
11886elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11887{
11888 asection *o;
11889
11890 if (flinfo->symstrtab != NULL)
ef10c3ac 11891 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
11892 free (flinfo->contents);
11893 free (flinfo->external_relocs);
11894 free (flinfo->internal_relocs);
11895 free (flinfo->external_syms);
11896 free (flinfo->locsym_shndx);
11897 free (flinfo->internal_syms);
11898 free (flinfo->indices);
11899 free (flinfo->sections);
11900 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11901 free (flinfo->symshndxbuf);
11902 for (o = obfd->sections; o != NULL; o = o->next)
11903 {
11904 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
11905 free (esdo->rel.hashes);
11906 free (esdo->rela.hashes);
9f7c3e5e
AM
11907 }
11908}
0b52efa6 11909
c152c796
AM
11910/* Do the final step of an ELF link. */
11911
11912bfd_boolean
11913bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11914{
11915 bfd_boolean dynamic;
11916 bfd_boolean emit_relocs;
11917 bfd *dynobj;
8b127cbc 11918 struct elf_final_link_info flinfo;
91d6fa6a
NC
11919 asection *o;
11920 struct bfd_link_order *p;
11921 bfd *sub;
c152c796
AM
11922 bfd_size_type max_contents_size;
11923 bfd_size_type max_external_reloc_size;
11924 bfd_size_type max_internal_reloc_count;
11925 bfd_size_type max_sym_count;
11926 bfd_size_type max_sym_shndx_count;
c152c796
AM
11927 Elf_Internal_Sym elfsym;
11928 unsigned int i;
11929 Elf_Internal_Shdr *symtab_hdr;
11930 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11931 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11932 struct elf_outext_info eoinfo;
11933 bfd_boolean merged;
11934 size_t relativecount = 0;
11935 asection *reldyn = 0;
11936 bfd_size_type amt;
104d59d1
JM
11937 asection *attr_section = NULL;
11938 bfd_vma attr_size = 0;
11939 const char *std_attrs_section;
64f52338 11940 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 11941 bfd_boolean sections_removed;
c152c796 11942
64f52338 11943 if (!is_elf_hash_table (htab))
c152c796
AM
11944 return FALSE;
11945
0e1862bb 11946 if (bfd_link_pic (info))
c152c796
AM
11947 abfd->flags |= DYNAMIC;
11948
64f52338
AM
11949 dynamic = htab->dynamic_sections_created;
11950 dynobj = htab->dynobj;
c152c796 11951
0e1862bb 11952 emit_relocs = (bfd_link_relocatable (info)
a4676736 11953 || info->emitrelocations);
c152c796 11954
8b127cbc
AM
11955 flinfo.info = info;
11956 flinfo.output_bfd = abfd;
ef10c3ac 11957 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11958 if (flinfo.symstrtab == NULL)
c152c796
AM
11959 return FALSE;
11960
11961 if (! dynamic)
11962 {
8b127cbc
AM
11963 flinfo.hash_sec = NULL;
11964 flinfo.symver_sec = NULL;
c152c796
AM
11965 }
11966 else
11967 {
3d4d4302 11968 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11969 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11970 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11971 /* Note that it is OK if symver_sec is NULL. */
11972 }
11973
8b127cbc
AM
11974 flinfo.contents = NULL;
11975 flinfo.external_relocs = NULL;
11976 flinfo.internal_relocs = NULL;
11977 flinfo.external_syms = NULL;
11978 flinfo.locsym_shndx = NULL;
11979 flinfo.internal_syms = NULL;
11980 flinfo.indices = NULL;
11981 flinfo.sections = NULL;
8b127cbc 11982 flinfo.symshndxbuf = NULL;
ffbc01cc 11983 flinfo.filesym_count = 0;
c152c796 11984
104d59d1
JM
11985 /* The object attributes have been merged. Remove the input
11986 sections from the link, and set the contents of the output
1ff6de03 11987 section. */
4c6ee646 11988 sections_removed = FALSE;
104d59d1
JM
11989 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11990 for (o = abfd->sections; o != NULL; o = o->next)
11991 {
5270eddc 11992 bfd_boolean remove_section = FALSE;
b8a6ced7 11993
104d59d1
JM
11994 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11995 || strcmp (o->name, ".gnu.attributes") == 0)
11996 {
11997 for (p = o->map_head.link_order; p != NULL; p = p->next)
11998 {
11999 asection *input_section;
12000
12001 if (p->type != bfd_indirect_link_order)
12002 continue;
12003 input_section = p->u.indirect.section;
12004 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12005 elf_link_input_bfd ignores this section. */
12006 input_section->flags &= ~SEC_HAS_CONTENTS;
12007 }
a0c8462f 12008
104d59d1 12009 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12010 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12011 /* Skip this section later on. */
12012 o->map_head.link_order = NULL;
104d59d1 12013 if (attr_size)
b8a6ced7 12014 attr_section = o;
104d59d1 12015 else
5270eddc 12016 remove_section = TRUE;
104d59d1 12017 }
6e5e9d58
AM
12018 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12019 {
12020 /* Remove empty group section from linker output. */
5270eddc 12021 remove_section = TRUE;
b8a6ced7 12022 }
5270eddc 12023 if (remove_section)
b8a6ced7 12024 {
6e5e9d58
AM
12025 o->flags |= SEC_EXCLUDE;
12026 bfd_section_list_remove (abfd, o);
12027 abfd->section_count--;
4c6ee646 12028 sections_removed = TRUE;
6e5e9d58 12029 }
104d59d1 12030 }
4c6ee646
AM
12031 if (sections_removed)
12032 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12033
c152c796
AM
12034 /* Count up the number of relocations we will output for each output
12035 section, so that we know the sizes of the reloc sections. We
12036 also figure out some maximum sizes. */
12037 max_contents_size = 0;
12038 max_external_reloc_size = 0;
12039 max_internal_reloc_count = 0;
12040 max_sym_count = 0;
12041 max_sym_shndx_count = 0;
12042 merged = FALSE;
12043 for (o = abfd->sections; o != NULL; o = o->next)
12044 {
12045 struct bfd_elf_section_data *esdo = elf_section_data (o);
12046 o->reloc_count = 0;
12047
8423293d 12048 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12049 {
12050 unsigned int reloc_count = 0;
9eaff861 12051 unsigned int additional_reloc_count = 0;
c152c796 12052 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12053
12054 if (p->type == bfd_section_reloc_link_order
12055 || p->type == bfd_symbol_reloc_link_order)
12056 reloc_count = 1;
12057 else if (p->type == bfd_indirect_link_order)
12058 {
12059 asection *sec;
12060
12061 sec = p->u.indirect.section;
c152c796
AM
12062
12063 /* Mark all sections which are to be included in the
12064 link. This will normally be every section. We need
12065 to do this so that we can identify any sections which
12066 the linker has decided to not include. */
12067 sec->linker_mark = TRUE;
12068
12069 if (sec->flags & SEC_MERGE)
12070 merged = TRUE;
12071
eea6121a
AM
12072 if (sec->rawsize > max_contents_size)
12073 max_contents_size = sec->rawsize;
12074 if (sec->size > max_contents_size)
12075 max_contents_size = sec->size;
c152c796 12076
c152c796
AM
12077 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12078 && (sec->owner->flags & DYNAMIC) == 0)
12079 {
12080 size_t sym_count;
12081
a961cdd5
AM
12082 /* We are interested in just local symbols, not all
12083 symbols. */
c152c796
AM
12084 if (elf_bad_symtab (sec->owner))
12085 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12086 / bed->s->sizeof_sym);
12087 else
12088 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12089
12090 if (sym_count > max_sym_count)
12091 max_sym_count = sym_count;
12092
12093 if (sym_count > max_sym_shndx_count
6a40cf0c 12094 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12095 max_sym_shndx_count = sym_count;
12096
a961cdd5
AM
12097 if (esdo->this_hdr.sh_type == SHT_REL
12098 || esdo->this_hdr.sh_type == SHT_RELA)
12099 /* Some backends use reloc_count in relocation sections
12100 to count particular types of relocs. Of course,
12101 reloc sections themselves can't have relocations. */
12102 ;
12103 else if (emit_relocs)
12104 {
12105 reloc_count = sec->reloc_count;
12106 if (bed->elf_backend_count_additional_relocs)
12107 {
12108 int c;
12109 c = (*bed->elf_backend_count_additional_relocs) (sec);
12110 additional_reloc_count += c;
12111 }
12112 }
12113 else if (bed->elf_backend_count_relocs)
12114 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12115
12116 esdi = elf_section_data (sec);
12117
c152c796
AM
12118 if ((sec->flags & SEC_RELOC) != 0)
12119 {
d4730f92 12120 size_t ext_size = 0;
c152c796 12121
d4730f92
BS
12122 if (esdi->rel.hdr != NULL)
12123 ext_size = esdi->rel.hdr->sh_size;
12124 if (esdi->rela.hdr != NULL)
12125 ext_size += esdi->rela.hdr->sh_size;
7326c758 12126
c152c796
AM
12127 if (ext_size > max_external_reloc_size)
12128 max_external_reloc_size = ext_size;
12129 if (sec->reloc_count > max_internal_reloc_count)
12130 max_internal_reloc_count = sec->reloc_count;
12131 }
12132 }
12133 }
12134
12135 if (reloc_count == 0)
12136 continue;
12137
9eaff861 12138 reloc_count += additional_reloc_count;
c152c796
AM
12139 o->reloc_count += reloc_count;
12140
0e1862bb 12141 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12142 {
d4730f92 12143 if (esdi->rel.hdr)
9eaff861 12144 {
491d01d3 12145 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12146 esdo->rel.count += additional_reloc_count;
12147 }
d4730f92 12148 if (esdi->rela.hdr)
9eaff861 12149 {
491d01d3 12150 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12151 esdo->rela.count += additional_reloc_count;
12152 }
d4730f92
BS
12153 }
12154 else
12155 {
12156 if (o->use_rela_p)
12157 esdo->rela.count += reloc_count;
2c2b4ed4 12158 else
d4730f92 12159 esdo->rel.count += reloc_count;
c152c796 12160 }
c152c796
AM
12161 }
12162
9eaff861 12163 if (o->reloc_count > 0)
c152c796
AM
12164 o->flags |= SEC_RELOC;
12165 else
12166 {
12167 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12168 set it (this is probably a bug) and if it is set
12169 assign_section_numbers will create a reloc section. */
12170 o->flags &=~ SEC_RELOC;
12171 }
12172
12173 /* If the SEC_ALLOC flag is not set, force the section VMA to
12174 zero. This is done in elf_fake_sections as well, but forcing
12175 the VMA to 0 here will ensure that relocs against these
12176 sections are handled correctly. */
12177 if ((o->flags & SEC_ALLOC) == 0
12178 && ! o->user_set_vma)
12179 o->vma = 0;
12180 }
12181
0e1862bb 12182 if (! bfd_link_relocatable (info) && merged)
64f52338 12183 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12184
12185 /* Figure out the file positions for everything but the symbol table
12186 and the relocs. We set symcount to force assign_section_numbers
12187 to create a symbol table. */
ed48ec2e 12188 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12189 BFD_ASSERT (! abfd->output_has_begun);
12190 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12191 goto error_return;
12192
ee75fd95 12193 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12194 for (o = abfd->sections; o != NULL; o = o->next)
12195 {
d4730f92 12196 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12197 if ((o->flags & SEC_RELOC) != 0)
12198 {
d4730f92 12199 if (esdo->rel.hdr
9eaff861 12200 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12201 goto error_return;
12202
d4730f92 12203 if (esdo->rela.hdr
9eaff861 12204 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12205 goto error_return;
12206 }
12207
48db3297
AM
12208 /* _bfd_elf_compute_section_file_positions makes temporary use
12209 of target_index. Reset it. */
12210 o->target_index = 0;
12211
c152c796
AM
12212 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12213 to count upwards while actually outputting the relocations. */
d4730f92
BS
12214 esdo->rel.count = 0;
12215 esdo->rela.count = 0;
0ce398f1 12216
1ff6de03
NA
12217 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12218 && !bfd_section_is_ctf (o))
0ce398f1
L
12219 {
12220 /* Cache the section contents so that they can be compressed
12221 later. Use bfd_malloc since it will be freed by
12222 bfd_compress_section_contents. */
12223 unsigned char *contents = esdo->this_hdr.contents;
12224 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12225 abort ();
12226 contents
12227 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12228 if (contents == NULL)
12229 goto error_return;
12230 esdo->this_hdr.contents = contents;
12231 }
c152c796
AM
12232 }
12233
1ff6de03
NA
12234 /* We have now assigned file positions for all the sections except .symtab,
12235 .strtab, and non-loaded reloc and compressed debugging sections. We start
12236 the .symtab section at the current file position, and write directly to it.
12237 We build the .strtab section in memory. */
ed48ec2e 12238 abfd->symcount = 0;
c152c796
AM
12239 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12240 /* sh_name is set in prep_headers. */
12241 symtab_hdr->sh_type = SHT_SYMTAB;
12242 /* sh_flags, sh_addr and sh_size all start off zero. */
12243 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12244 /* sh_link is set in assign_section_numbers. */
12245 /* sh_info is set below. */
12246 /* sh_offset is set just below. */
72de5009 12247 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12248
ef10c3ac
L
12249 if (max_sym_count < 20)
12250 max_sym_count = 20;
64f52338 12251 htab->strtabsize = max_sym_count;
ef10c3ac 12252 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12253 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12254 if (htab->strtab == NULL)
c152c796 12255 goto error_return;
ef10c3ac
L
12256 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12257 flinfo.symshndxbuf
12258 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12259 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12260
8539e4e8 12261 if (info->strip != strip_all || emit_relocs)
c152c796 12262 {
8539e4e8
AM
12263 file_ptr off = elf_next_file_pos (abfd);
12264
12265 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12266
12267 /* Note that at this point elf_next_file_pos (abfd) is
12268 incorrect. We do not yet know the size of the .symtab section.
12269 We correct next_file_pos below, after we do know the size. */
12270
12271 /* Start writing out the symbol table. The first symbol is always a
12272 dummy symbol. */
c152c796
AM
12273 elfsym.st_value = 0;
12274 elfsym.st_size = 0;
12275 elfsym.st_info = 0;
12276 elfsym.st_other = 0;
12277 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12278 elfsym.st_target_internal = 0;
ef10c3ac
L
12279 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12280 bfd_und_section_ptr, NULL) != 1)
c152c796 12281 goto error_return;
c152c796 12282
8539e4e8
AM
12283 /* Output a symbol for each section. We output these even if we are
12284 discarding local symbols, since they are used for relocs. These
12285 symbols have no names. We store the index of each one in the
12286 index field of the section, so that we can find it again when
12287 outputting relocs. */
12288
c152c796
AM
12289 elfsym.st_size = 0;
12290 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12291 elfsym.st_other = 0;
f0b5bb34 12292 elfsym.st_value = 0;
35fc36a8 12293 elfsym.st_target_internal = 0;
c152c796
AM
12294 for (i = 1; i < elf_numsections (abfd); i++)
12295 {
12296 o = bfd_section_from_elf_index (abfd, i);
12297 if (o != NULL)
f0b5bb34
AM
12298 {
12299 o->target_index = bfd_get_symcount (abfd);
12300 elfsym.st_shndx = i;
0e1862bb 12301 if (!bfd_link_relocatable (info))
f0b5bb34 12302 elfsym.st_value = o->vma;
ef10c3ac
L
12303 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12304 NULL) != 1)
f0b5bb34
AM
12305 goto error_return;
12306 }
c152c796
AM
12307 }
12308 }
12309
12310 /* Allocate some memory to hold information read in from the input
12311 files. */
12312 if (max_contents_size != 0)
12313 {
8b127cbc
AM
12314 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12315 if (flinfo.contents == NULL)
c152c796
AM
12316 goto error_return;
12317 }
12318
12319 if (max_external_reloc_size != 0)
12320 {
8b127cbc
AM
12321 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12322 if (flinfo.external_relocs == NULL)
c152c796
AM
12323 goto error_return;
12324 }
12325
12326 if (max_internal_reloc_count != 0)
12327 {
056bafd4 12328 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12329 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12330 if (flinfo.internal_relocs == NULL)
c152c796
AM
12331 goto error_return;
12332 }
12333
12334 if (max_sym_count != 0)
12335 {
12336 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12337 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12338 if (flinfo.external_syms == NULL)
c152c796
AM
12339 goto error_return;
12340
12341 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12342 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12343 if (flinfo.internal_syms == NULL)
c152c796
AM
12344 goto error_return;
12345
12346 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12347 flinfo.indices = (long int *) bfd_malloc (amt);
12348 if (flinfo.indices == NULL)
c152c796
AM
12349 goto error_return;
12350
12351 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12352 flinfo.sections = (asection **) bfd_malloc (amt);
12353 if (flinfo.sections == NULL)
c152c796
AM
12354 goto error_return;
12355 }
12356
12357 if (max_sym_shndx_count != 0)
12358 {
12359 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12360 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12361 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12362 goto error_return;
12363 }
12364
64f52338 12365 if (htab->tls_sec)
c152c796 12366 {
66631823 12367 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12368 asection *sec;
12369
64f52338 12370 for (sec = htab->tls_sec;
c152c796
AM
12371 sec && (sec->flags & SEC_THREAD_LOCAL);
12372 sec = sec->next)
12373 {
3a800eb9 12374 bfd_size_type size = sec->size;
66631823 12375 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12376
3a800eb9
AM
12377 if (size == 0
12378 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12379 {
91d6fa6a
NC
12380 struct bfd_link_order *ord = sec->map_tail.link_order;
12381
12382 if (ord != NULL)
66631823 12383 size = ord->offset * opb + ord->size;
c152c796 12384 }
66631823 12385 end = sec->vma + size / opb;
c152c796 12386 }
64f52338 12387 base = htab->tls_sec->vma;
7dc98aea
RO
12388 /* Only align end of TLS section if static TLS doesn't have special
12389 alignment requirements. */
12390 if (bed->static_tls_alignment == 1)
64f52338
AM
12391 end = align_power (end, htab->tls_sec->alignment_power);
12392 htab->tls_size = end - base;
c152c796
AM
12393 }
12394
0b52efa6
PB
12395 /* Reorder SHF_LINK_ORDER sections. */
12396 for (o = abfd->sections; o != NULL; o = o->next)
12397 {
12398 if (!elf_fixup_link_order (abfd, o))
12399 return FALSE;
12400 }
12401
2f0c68f2
CM
12402 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12403 return FALSE;
12404
c152c796
AM
12405 /* Since ELF permits relocations to be against local symbols, we
12406 must have the local symbols available when we do the relocations.
12407 Since we would rather only read the local symbols once, and we
12408 would rather not keep them in memory, we handle all the
12409 relocations for a single input file at the same time.
12410
12411 Unfortunately, there is no way to know the total number of local
12412 symbols until we have seen all of them, and the local symbol
12413 indices precede the global symbol indices. This means that when
12414 we are generating relocatable output, and we see a reloc against
12415 a global symbol, we can not know the symbol index until we have
12416 finished examining all the local symbols to see which ones we are
12417 going to output. To deal with this, we keep the relocations in
12418 memory, and don't output them until the end of the link. This is
12419 an unfortunate waste of memory, but I don't see a good way around
12420 it. Fortunately, it only happens when performing a relocatable
12421 link, which is not the common case. FIXME: If keep_memory is set
12422 we could write the relocs out and then read them again; I don't
12423 know how bad the memory loss will be. */
12424
c72f2fb2 12425 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12426 sub->output_has_begun = FALSE;
12427 for (o = abfd->sections; o != NULL; o = o->next)
12428 {
8423293d 12429 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12430 {
12431 if (p->type == bfd_indirect_link_order
12432 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12433 == bfd_target_elf_flavour)
12434 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12435 {
12436 if (! sub->output_has_begun)
12437 {
8b127cbc 12438 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12439 goto error_return;
12440 sub->output_has_begun = TRUE;
12441 }
12442 }
12443 else if (p->type == bfd_section_reloc_link_order
12444 || p->type == bfd_symbol_reloc_link_order)
12445 {
12446 if (! elf_reloc_link_order (abfd, info, o, p))
12447 goto error_return;
12448 }
12449 else
12450 {
12451 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12452 {
12453 if (p->type == bfd_indirect_link_order
12454 && (bfd_get_flavour (sub)
12455 == bfd_target_elf_flavour)
12456 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12457 != bed->s->elfclass))
12458 {
12459 const char *iclass, *oclass;
12460
aebf9be7 12461 switch (bed->s->elfclass)
351f65ca 12462 {
aebf9be7
NC
12463 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12464 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12465 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12466 default: abort ();
351f65ca 12467 }
aebf9be7
NC
12468
12469 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12470 {
aebf9be7
NC
12471 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12472 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12473 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12474 default: abort ();
351f65ca
L
12475 }
12476
12477 bfd_set_error (bfd_error_wrong_format);
4eca0228 12478 _bfd_error_handler
695344c0 12479 /* xgettext:c-format */
871b3ab2 12480 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12481 sub, iclass, oclass);
12482 }
12483
12484 goto error_return;
12485 }
c152c796
AM
12486 }
12487 }
12488 }
12489
c0f00686
L
12490 /* Free symbol buffer if needed. */
12491 if (!info->reduce_memory_overheads)
12492 {
c72f2fb2 12493 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12494 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12495 {
12496 free (elf_tdata (sub)->symbuf);
12497 elf_tdata (sub)->symbuf = NULL;
12498 }
12499 }
12500
c152c796
AM
12501 /* Output any global symbols that got converted to local in a
12502 version script or due to symbol visibility. We do this in a
12503 separate step since ELF requires all local symbols to appear
12504 prior to any global symbols. FIXME: We should only do this if
12505 some global symbols were, in fact, converted to become local.
12506 FIXME: Will this work correctly with the Irix 5 linker? */
12507 eoinfo.failed = FALSE;
8b127cbc 12508 eoinfo.flinfo = &flinfo;
c152c796 12509 eoinfo.localsyms = TRUE;
34a79995 12510 eoinfo.file_sym_done = FALSE;
7686d77d 12511 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12512 if (eoinfo.failed)
12513 return FALSE;
12514
4e617b1e
PB
12515 /* If backend needs to output some local symbols not present in the hash
12516 table, do it now. */
8539e4e8
AM
12517 if (bed->elf_backend_output_arch_local_syms
12518 && (info->strip != strip_all || emit_relocs))
4e617b1e 12519 {
6e0b88f1 12520 typedef int (*out_sym_func)
4e617b1e
PB
12521 (void *, const char *, Elf_Internal_Sym *, asection *,
12522 struct elf_link_hash_entry *);
12523
12524 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12525 (abfd, info, &flinfo,
12526 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12527 return FALSE;
12528 }
12529
c152c796
AM
12530 /* That wrote out all the local symbols. Finish up the symbol table
12531 with the global symbols. Even if we want to strip everything we
12532 can, we still need to deal with those global symbols that got
12533 converted to local in a version script. */
12534
12535 /* The sh_info field records the index of the first non local symbol. */
12536 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12537
12538 if (dynamic
64f52338
AM
12539 && htab->dynsym != NULL
12540 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12541 {
12542 Elf_Internal_Sym sym;
64f52338 12543 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12544
64f52338
AM
12545 o = htab->dynsym->output_section;
12546 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12547
12548 /* Write out the section symbols for the output sections. */
0e1862bb 12549 if (bfd_link_pic (info)
64f52338 12550 || htab->is_relocatable_executable)
c152c796
AM
12551 {
12552 asection *s;
12553
12554 sym.st_size = 0;
12555 sym.st_name = 0;
12556 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12557 sym.st_other = 0;
35fc36a8 12558 sym.st_target_internal = 0;
c152c796
AM
12559
12560 for (s = abfd->sections; s != NULL; s = s->next)
12561 {
12562 int indx;
12563 bfd_byte *dest;
12564 long dynindx;
12565
c152c796 12566 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12567 if (dynindx <= 0)
12568 continue;
12569 indx = elf_section_data (s)->this_idx;
c152c796
AM
12570 BFD_ASSERT (indx > 0);
12571 sym.st_shndx = indx;
c0d5a53d
L
12572 if (! check_dynsym (abfd, &sym))
12573 return FALSE;
c152c796
AM
12574 sym.st_value = s->vma;
12575 dest = dynsym + dynindx * bed->s->sizeof_sym;
12576 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12577 }
c152c796
AM
12578 }
12579
12580 /* Write out the local dynsyms. */
64f52338 12581 if (htab->dynlocal)
c152c796
AM
12582 {
12583 struct elf_link_local_dynamic_entry *e;
64f52338 12584 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12585 {
12586 asection *s;
12587 bfd_byte *dest;
12588
935bd1e0 12589 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12590 Note that we saved a word of storage and overwrote
12591 the original st_name with the dynstr_index. */
12592 sym = e->isym;
935bd1e0 12593 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12594
cb33740c
AM
12595 s = bfd_section_from_elf_index (e->input_bfd,
12596 e->isym.st_shndx);
12597 if (s != NULL)
c152c796 12598 {
c152c796
AM
12599 sym.st_shndx =
12600 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12601 if (! check_dynsym (abfd, &sym))
12602 return FALSE;
c152c796
AM
12603 sym.st_value = (s->output_section->vma
12604 + s->output_offset
12605 + e->isym.st_value);
12606 }
12607
c152c796
AM
12608 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12609 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12610 }
12611 }
c152c796
AM
12612 }
12613
12614 /* We get the global symbols from the hash table. */
12615 eoinfo.failed = FALSE;
12616 eoinfo.localsyms = FALSE;
8b127cbc 12617 eoinfo.flinfo = &flinfo;
7686d77d 12618 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12619 if (eoinfo.failed)
12620 return FALSE;
12621
12622 /* If backend needs to output some symbols not present in the hash
12623 table, do it now. */
8539e4e8
AM
12624 if (bed->elf_backend_output_arch_syms
12625 && (info->strip != strip_all || emit_relocs))
c152c796 12626 {
6e0b88f1 12627 typedef int (*out_sym_func)
c152c796
AM
12628 (void *, const char *, Elf_Internal_Sym *, asection *,
12629 struct elf_link_hash_entry *);
12630
12631 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12632 (abfd, info, &flinfo,
12633 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12634 return FALSE;
12635 }
12636
ef10c3ac
L
12637 /* Finalize the .strtab section. */
12638 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12639
12640 /* Swap out the .strtab section. */
12641 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12642 return FALSE;
12643
12644 /* Now we know the size of the symtab section. */
c152c796
AM
12645 if (bfd_get_symcount (abfd) > 0)
12646 {
ee3b52e9
L
12647 /* Finish up and write out the symbol string table (.strtab)
12648 section. */
ad32986f 12649 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12650 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12651
ad32986f 12652 if (elf_symtab_shndx_list (abfd))
8539e4e8 12653 {
ad32986f 12654 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12655
ad32986f
NC
12656 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12657 {
12658 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12659 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12660 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12661 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12662 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12663
ad32986f
NC
12664 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12665 off, TRUE);
12666
12667 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12668 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12669 return FALSE;
12670 }
8539e4e8 12671 }
ee3b52e9
L
12672
12673 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12674 /* sh_name was set in prep_headers. */
12675 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12676 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12677 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12678 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12679 symstrtab_hdr->sh_entsize = 0;
12680 symstrtab_hdr->sh_link = 0;
12681 symstrtab_hdr->sh_info = 0;
12682 /* sh_offset is set just below. */
12683 symstrtab_hdr->sh_addralign = 1;
12684
12685 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12686 off, TRUE);
12687 elf_next_file_pos (abfd) = off;
12688
c152c796 12689 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12690 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12691 return FALSE;
12692 }
12693
76359541
TP
12694 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12695 {
871b3ab2 12696 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12697 info->out_implib_bfd);
76359541
TP
12698 return FALSE;
12699 }
12700
c152c796
AM
12701 /* Adjust the relocs to have the correct symbol indices. */
12702 for (o = abfd->sections; o != NULL; o = o->next)
12703 {
d4730f92 12704 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12705 bfd_boolean sort;
10bbbc1d 12706
c152c796
AM
12707 if ((o->flags & SEC_RELOC) == 0)
12708 continue;
12709
28dbcedc 12710 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12711 if (esdo->rel.hdr != NULL
10bbbc1d 12712 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12713 return FALSE;
12714 if (esdo->rela.hdr != NULL
10bbbc1d 12715 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12716 return FALSE;
c152c796
AM
12717
12718 /* Set the reloc_count field to 0 to prevent write_relocs from
12719 trying to swap the relocs out itself. */
12720 o->reloc_count = 0;
12721 }
12722
12723 if (dynamic && info->combreloc && dynobj != NULL)
12724 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12725
12726 /* If we are linking against a dynamic object, or generating a
12727 shared library, finish up the dynamic linking information. */
12728 if (dynamic)
12729 {
12730 bfd_byte *dyncon, *dynconend;
12731
12732 /* Fix up .dynamic entries. */
3d4d4302 12733 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12734 BFD_ASSERT (o != NULL);
12735
12736 dyncon = o->contents;
eea6121a 12737 dynconend = o->contents + o->size;
c152c796
AM
12738 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12739 {
12740 Elf_Internal_Dyn dyn;
12741 const char *name;
12742 unsigned int type;
64487780
AM
12743 bfd_size_type sh_size;
12744 bfd_vma sh_addr;
c152c796
AM
12745
12746 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12747
12748 switch (dyn.d_tag)
12749 {
12750 default:
12751 continue;
12752 case DT_NULL:
12753 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12754 {
12755 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12756 {
12757 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12758 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12759 default: continue;
12760 }
12761 dyn.d_un.d_val = relativecount;
12762 relativecount = 0;
12763 break;
12764 }
12765 continue;
12766
12767 case DT_INIT:
12768 name = info->init_function;
12769 goto get_sym;
12770 case DT_FINI:
12771 name = info->fini_function;
12772 get_sym:
12773 {
12774 struct elf_link_hash_entry *h;
12775
64f52338 12776 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12777 if (h != NULL
12778 && (h->root.type == bfd_link_hash_defined
12779 || h->root.type == bfd_link_hash_defweak))
12780 {
bef26483 12781 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12782 o = h->root.u.def.section;
12783 if (o->output_section != NULL)
bef26483 12784 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12785 + o->output_offset);
12786 else
12787 {
12788 /* The symbol is imported from another shared
12789 library and does not apply to this one. */
bef26483 12790 dyn.d_un.d_ptr = 0;
c152c796
AM
12791 }
12792 break;
12793 }
12794 }
12795 continue;
12796
12797 case DT_PREINIT_ARRAYSZ:
12798 name = ".preinit_array";
4ade44b7 12799 goto get_out_size;
c152c796
AM
12800 case DT_INIT_ARRAYSZ:
12801 name = ".init_array";
4ade44b7 12802 goto get_out_size;
c152c796
AM
12803 case DT_FINI_ARRAYSZ:
12804 name = ".fini_array";
4ade44b7 12805 get_out_size:
c152c796
AM
12806 o = bfd_get_section_by_name (abfd, name);
12807 if (o == NULL)
12808 {
4eca0228 12809 _bfd_error_handler
4ade44b7 12810 (_("could not find section %s"), name);
c152c796
AM
12811 goto error_return;
12812 }
eea6121a 12813 if (o->size == 0)
4eca0228 12814 _bfd_error_handler
c152c796 12815 (_("warning: %s section has zero size"), name);
eea6121a 12816 dyn.d_un.d_val = o->size;
c152c796
AM
12817 break;
12818
12819 case DT_PREINIT_ARRAY:
12820 name = ".preinit_array";
4ade44b7 12821 goto get_out_vma;
c152c796
AM
12822 case DT_INIT_ARRAY:
12823 name = ".init_array";
4ade44b7 12824 goto get_out_vma;
c152c796
AM
12825 case DT_FINI_ARRAY:
12826 name = ".fini_array";
4ade44b7
AM
12827 get_out_vma:
12828 o = bfd_get_section_by_name (abfd, name);
12829 goto do_vma;
c152c796
AM
12830
12831 case DT_HASH:
12832 name = ".hash";
12833 goto get_vma;
fdc90cb4
JJ
12834 case DT_GNU_HASH:
12835 name = ".gnu.hash";
12836 goto get_vma;
c152c796
AM
12837 case DT_STRTAB:
12838 name = ".dynstr";
12839 goto get_vma;
12840 case DT_SYMTAB:
12841 name = ".dynsym";
12842 goto get_vma;
12843 case DT_VERDEF:
12844 name = ".gnu.version_d";
12845 goto get_vma;
12846 case DT_VERNEED:
12847 name = ".gnu.version_r";
12848 goto get_vma;
12849 case DT_VERSYM:
12850 name = ".gnu.version";
12851 get_vma:
4ade44b7
AM
12852 o = bfd_get_linker_section (dynobj, name);
12853 do_vma:
b3293efa 12854 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12855 {
4eca0228 12856 _bfd_error_handler
4ade44b7 12857 (_("could not find section %s"), name);
c152c796
AM
12858 goto error_return;
12859 }
894891db
NC
12860 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12861 {
4eca0228 12862 _bfd_error_handler
894891db
NC
12863 (_("warning: section '%s' is being made into a note"), name);
12864 bfd_set_error (bfd_error_nonrepresentable_section);
12865 goto error_return;
12866 }
4ade44b7 12867 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12868 break;
12869
12870 case DT_REL:
12871 case DT_RELA:
12872 case DT_RELSZ:
12873 case DT_RELASZ:
12874 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12875 type = SHT_REL;
12876 else
12877 type = SHT_RELA;
64487780
AM
12878 sh_size = 0;
12879 sh_addr = 0;
c152c796
AM
12880 for (i = 1; i < elf_numsections (abfd); i++)
12881 {
12882 Elf_Internal_Shdr *hdr;
12883
12884 hdr = elf_elfsections (abfd)[i];
12885 if (hdr->sh_type == type
12886 && (hdr->sh_flags & SHF_ALLOC) != 0)
12887 {
64487780
AM
12888 sh_size += hdr->sh_size;
12889 if (sh_addr == 0
12890 || sh_addr > hdr->sh_addr)
12891 sh_addr = hdr->sh_addr;
c152c796
AM
12892 }
12893 }
64487780 12894
64f52338
AM
12895 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12896 {
66631823
CE
12897 unsigned int opb = bfd_octets_per_byte (abfd, o);
12898
64f52338
AM
12899 /* Don't count procedure linkage table relocs in the
12900 overall reloc count. */
64487780
AM
12901 sh_size -= htab->srelplt->size;
12902 if (sh_size == 0)
12903 /* If the size is zero, make the address zero too.
12904 This is to avoid a glibc bug. If the backend
12905 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12906 zero, then we'll put DT_RELA at the end of
12907 DT_JMPREL. glibc will interpret the end of
12908 DT_RELA matching the end of DT_JMPREL as the
12909 case where DT_RELA includes DT_JMPREL, and for
12910 LD_BIND_NOW will decide that processing DT_RELA
12911 will process the PLT relocs too. Net result:
12912 No PLT relocs applied. */
12913 sh_addr = 0;
12914
64f52338
AM
12915 /* If .rela.plt is the first .rela section, exclude
12916 it from DT_RELA. */
64487780 12917 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 12918 + htab->srelplt->output_offset) * opb)
64487780 12919 sh_addr += htab->srelplt->size;
64f52338 12920 }
64487780
AM
12921
12922 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12923 dyn.d_un.d_val = sh_size;
12924 else
12925 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12926 break;
12927 }
12928 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12929 }
12930 }
12931
12932 /* If we have created any dynamic sections, then output them. */
12933 if (dynobj != NULL)
12934 {
12935 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12936 goto error_return;
12937
943284cc 12938 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 12939 if (bfd_link_textrel_check (info)
3d4d4302 12940 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12941 {
12942 bfd_byte *dyncon, *dynconend;
12943
943284cc
DJ
12944 dyncon = o->contents;
12945 dynconend = o->contents + o->size;
12946 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12947 {
12948 Elf_Internal_Dyn dyn;
12949
12950 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12951
12952 if (dyn.d_tag == DT_TEXTREL)
12953 {
a6dbf402 12954 if (info->textrel_check == textrel_check_error)
c192a133 12955 info->callbacks->einfo
9793eb77 12956 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
12957 else if (bfd_link_dll (info))
12958 info->callbacks->einfo
12959 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
12960 else
12961 info->callbacks->einfo
a6dbf402 12962 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
12963 break;
12964 }
12965 }
12966 }
12967
c152c796
AM
12968 for (o = dynobj->sections; o != NULL; o = o->next)
12969 {
12970 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12971 || o->size == 0
c152c796
AM
12972 || o->output_section == bfd_abs_section_ptr)
12973 continue;
12974 if ((o->flags & SEC_LINKER_CREATED) == 0)
12975 {
12976 /* At this point, we are only interested in sections
12977 created by _bfd_elf_link_create_dynamic_sections. */
12978 continue;
12979 }
64f52338 12980 if (htab->stab_info.stabstr == o)
3722b82f 12981 continue;
64f52338 12982 if (htab->eh_info.hdr_sec == o)
eea6121a 12983 continue;
3d4d4302 12984 if (strcmp (o->name, ".dynstr") != 0)
c152c796 12985 {
bb294208
AM
12986 bfd_size_type octets = ((file_ptr) o->output_offset
12987 * bfd_octets_per_byte (abfd, o));
12988 if (!bfd_set_section_contents (abfd, o->output_section,
12989 o->contents, octets, o->size))
c152c796
AM
12990 goto error_return;
12991 }
12992 else
12993 {
12994 /* The contents of the .dynstr section are actually in a
12995 stringtab. */
8539e4e8
AM
12996 file_ptr off;
12997
c152c796
AM
12998 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12999 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13000 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13001 goto error_return;
13002 }
13003 }
13004 }
13005
7bdf4127 13006 if (!info->resolve_section_groups)
c152c796
AM
13007 {
13008 bfd_boolean failed = FALSE;
13009
7bdf4127 13010 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13011 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13012 if (failed)
13013 goto error_return;
13014 }
13015
13016 /* If we have optimized stabs strings, output them. */
64f52338 13017 if (htab->stab_info.stabstr != NULL)
c152c796 13018 {
64f52338 13019 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13020 goto error_return;
13021 }
13022
9f7c3e5e
AM
13023 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13024 goto error_return;
c152c796 13025
1ff6de03
NA
13026 if (info->callbacks->emit_ctf)
13027 info->callbacks->emit_ctf ();
13028
9f7c3e5e 13029 elf_final_link_free (abfd, &flinfo);
c152c796 13030
104d59d1
JM
13031 if (attr_section)
13032 {
a50b1753 13033 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13034 if (contents == NULL)
d0f16d5e 13035 return FALSE; /* Bail out and fail. */
104d59d1
JM
13036 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13037 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13038 free (contents);
13039 }
13040
c152c796
AM
13041 return TRUE;
13042
13043 error_return:
9f7c3e5e 13044 elf_final_link_free (abfd, &flinfo);
c152c796
AM
13045 return FALSE;
13046}
13047\f
5241d853
RS
13048/* Initialize COOKIE for input bfd ABFD. */
13049
13050static bfd_boolean
13051init_reloc_cookie (struct elf_reloc_cookie *cookie,
13052 struct bfd_link_info *info, bfd *abfd)
13053{
13054 Elf_Internal_Shdr *symtab_hdr;
13055 const struct elf_backend_data *bed;
13056
13057 bed = get_elf_backend_data (abfd);
13058 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13059
13060 cookie->abfd = abfd;
13061 cookie->sym_hashes = elf_sym_hashes (abfd);
13062 cookie->bad_symtab = elf_bad_symtab (abfd);
13063 if (cookie->bad_symtab)
13064 {
13065 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13066 cookie->extsymoff = 0;
13067 }
13068 else
13069 {
13070 cookie->locsymcount = symtab_hdr->sh_info;
13071 cookie->extsymoff = symtab_hdr->sh_info;
13072 }
13073
13074 if (bed->s->arch_size == 32)
13075 cookie->r_sym_shift = 8;
13076 else
13077 cookie->r_sym_shift = 32;
13078
13079 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13080 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13081 {
13082 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13083 cookie->locsymcount, 0,
13084 NULL, NULL, NULL);
13085 if (cookie->locsyms == NULL)
13086 {
13087 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13088 return FALSE;
13089 }
13090 if (info->keep_memory)
13091 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13092 }
13093 return TRUE;
13094}
13095
13096/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13097
13098static void
13099fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13100{
13101 Elf_Internal_Shdr *symtab_hdr;
13102
13103 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13104 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13105 free (cookie->locsyms);
13106}
13107
13108/* Initialize the relocation information in COOKIE for input section SEC
13109 of input bfd ABFD. */
13110
13111static bfd_boolean
13112init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13113 struct bfd_link_info *info, bfd *abfd,
13114 asection *sec)
13115{
5241d853
RS
13116 if (sec->reloc_count == 0)
13117 {
13118 cookie->rels = NULL;
13119 cookie->relend = NULL;
13120 }
13121 else
13122 {
5241d853
RS
13123 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13124 info->keep_memory);
13125 if (cookie->rels == NULL)
13126 return FALSE;
13127 cookie->rel = cookie->rels;
056bafd4 13128 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13129 }
13130 cookie->rel = cookie->rels;
13131 return TRUE;
13132}
13133
13134/* Free the memory allocated by init_reloc_cookie_rels,
13135 if appropriate. */
13136
13137static void
13138fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13139 asection *sec)
13140{
c9594989 13141 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13142 free (cookie->rels);
13143}
13144
13145/* Initialize the whole of COOKIE for input section SEC. */
13146
13147static bfd_boolean
13148init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13149 struct bfd_link_info *info,
13150 asection *sec)
13151{
13152 if (!init_reloc_cookie (cookie, info, sec->owner))
13153 goto error1;
13154 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13155 goto error2;
13156 return TRUE;
13157
13158 error2:
13159 fini_reloc_cookie (cookie, sec->owner);
13160 error1:
13161 return FALSE;
13162}
13163
13164/* Free the memory allocated by init_reloc_cookie_for_section,
13165 if appropriate. */
13166
13167static void
13168fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13169 asection *sec)
13170{
13171 fini_reloc_cookie_rels (cookie, sec);
13172 fini_reloc_cookie (cookie, sec->owner);
13173}
13174\f
c152c796
AM
13175/* Garbage collect unused sections. */
13176
07adf181
AM
13177/* Default gc_mark_hook. */
13178
13179asection *
13180_bfd_elf_gc_mark_hook (asection *sec,
13181 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13182 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13183 struct elf_link_hash_entry *h,
13184 Elf_Internal_Sym *sym)
13185{
13186 if (h != NULL)
13187 {
13188 switch (h->root.type)
13189 {
13190 case bfd_link_hash_defined:
13191 case bfd_link_hash_defweak:
13192 return h->root.u.def.section;
13193
13194 case bfd_link_hash_common:
13195 return h->root.u.c.p->section;
13196
13197 default:
13198 break;
13199 }
13200 }
13201 else
13202 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13203
13204 return NULL;
13205}
13206
9e223787 13207/* Return the debug definition section. */
b7c871ed
L
13208
13209static asection *
13210elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13211 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13212 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13213 struct elf_link_hash_entry *h,
9e223787 13214 Elf_Internal_Sym *sym)
b7c871ed 13215{
9e223787
L
13216 if (h != NULL)
13217 {
13218 /* Return the global debug definition section. */
13219 if ((h->root.type == bfd_link_hash_defined
13220 || h->root.type == bfd_link_hash_defweak)
13221 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13222 return h->root.u.def.section;
13223 }
13224 else
13225 {
13226 /* Return the local debug definition section. */
13227 asection *isec = bfd_section_from_elf_index (sec->owner,
13228 sym->st_shndx);
13229 if ((isec->flags & SEC_DEBUGGING) != 0)
13230 return isec;
13231 }
b7c871ed
L
13232
13233 return NULL;
13234}
13235
5241d853
RS
13236/* COOKIE->rel describes a relocation against section SEC, which is
13237 a section we've decided to keep. Return the section that contains
13238 the relocation symbol, or NULL if no section contains it. */
13239
13240asection *
13241_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13242 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13243 struct elf_reloc_cookie *cookie,
13244 bfd_boolean *start_stop)
5241d853
RS
13245{
13246 unsigned long r_symndx;
3024a17a 13247 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13248
13249 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13250 if (r_symndx == STN_UNDEF)
5241d853
RS
13251 return NULL;
13252
13253 if (r_symndx >= cookie->locsymcount
13254 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13255 {
13256 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13257 if (h == NULL)
13258 {
871b3ab2 13259 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13260 sec->owner);
13261 return NULL;
13262 }
5241d853
RS
13263 while (h->root.type == bfd_link_hash_indirect
13264 || h->root.type == bfd_link_hash_warning)
13265 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13266 h->mark = 1;
3024a17a
AM
13267 /* Keep all aliases of the symbol too. If an object symbol
13268 needs to be copied into .dynbss then all of its aliases
13269 should be present as dynamic symbols, not just the one used
13270 on the copy relocation. */
13271 hw = h;
13272 while (hw->is_weakalias)
13273 {
13274 hw = hw->u.alias;
13275 hw->mark = 1;
13276 }
1cce69b9 13277
a6a4679f 13278 if (start_stop != NULL)
1cce69b9 13279 {
7dba9362
AM
13280 /* To work around a glibc bug, mark XXX input sections
13281 when there is a reference to __start_XXX or __stop_XXX
13282 symbols. */
cbd0eecf 13283 if (h->start_stop)
1cce69b9 13284 {
cbd0eecf 13285 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13286 *start_stop = !s->gc_mark;
13287 return s;
1cce69b9
AM
13288 }
13289 }
13290
5241d853
RS
13291 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13292 }
13293
13294 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13295 &cookie->locsyms[r_symndx]);
13296}
13297
13298/* COOKIE->rel describes a relocation against section SEC, which is
13299 a section we've decided to keep. Mark the section that contains
9d0a14d3 13300 the relocation symbol. */
5241d853
RS
13301
13302bfd_boolean
13303_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13304 asection *sec,
13305 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13306 struct elf_reloc_cookie *cookie)
5241d853
RS
13307{
13308 asection *rsec;
1cce69b9 13309 bfd_boolean start_stop = FALSE;
5241d853 13310
1cce69b9
AM
13311 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13312 while (rsec != NULL)
5241d853 13313 {
1cce69b9
AM
13314 if (!rsec->gc_mark)
13315 {
13316 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13317 || (rsec->owner->flags & DYNAMIC) != 0)
13318 rsec->gc_mark = 1;
13319 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13320 return FALSE;
13321 }
13322 if (!start_stop)
13323 break;
199af150 13324 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13325 }
13326 return TRUE;
13327}
13328
07adf181
AM
13329/* The mark phase of garbage collection. For a given section, mark
13330 it and any sections in this section's group, and all the sections
13331 which define symbols to which it refers. */
13332
ccfa59ea
AM
13333bfd_boolean
13334_bfd_elf_gc_mark (struct bfd_link_info *info,
13335 asection *sec,
6a5bb875 13336 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13337{
13338 bfd_boolean ret;
9d0a14d3 13339 asection *group_sec, *eh_frame;
c152c796
AM
13340
13341 sec->gc_mark = 1;
13342
13343 /* Mark all the sections in the group. */
13344 group_sec = elf_section_data (sec)->next_in_group;
13345 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13346 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13347 return FALSE;
13348
13349 /* Look through the section relocs. */
13350 ret = TRUE;
9d0a14d3
RS
13351 eh_frame = elf_eh_frame_section (sec->owner);
13352 if ((sec->flags & SEC_RELOC) != 0
13353 && sec->reloc_count > 0
13354 && sec != eh_frame)
c152c796 13355 {
5241d853 13356 struct elf_reloc_cookie cookie;
c152c796 13357
5241d853
RS
13358 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13359 ret = FALSE;
c152c796 13360 else
c152c796 13361 {
5241d853 13362 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13363 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13364 {
13365 ret = FALSE;
13366 break;
13367 }
13368 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13369 }
13370 }
9d0a14d3
RS
13371
13372 if (ret && eh_frame && elf_fde_list (sec))
13373 {
13374 struct elf_reloc_cookie cookie;
13375
13376 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13377 ret = FALSE;
13378 else
13379 {
13380 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13381 gc_mark_hook, &cookie))
13382 ret = FALSE;
13383 fini_reloc_cookie_for_section (&cookie, eh_frame);
13384 }
13385 }
13386
2f0c68f2
CM
13387 eh_frame = elf_section_eh_frame_entry (sec);
13388 if (ret && eh_frame && !eh_frame->gc_mark)
13389 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13390 ret = FALSE;
13391
c152c796
AM
13392 return ret;
13393}
13394
3c758495
TG
13395/* Scan and mark sections in a special or debug section group. */
13396
13397static void
13398_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13399{
13400 /* Point to first section of section group. */
13401 asection *ssec;
13402 /* Used to iterate the section group. */
13403 asection *msec;
13404
13405 bfd_boolean is_special_grp = TRUE;
13406 bfd_boolean is_debug_grp = TRUE;
13407
13408 /* First scan to see if group contains any section other than debug
13409 and special section. */
13410 ssec = msec = elf_next_in_group (grp);
13411 do
13412 {
13413 if ((msec->flags & SEC_DEBUGGING) == 0)
13414 is_debug_grp = FALSE;
13415
13416 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13417 is_special_grp = FALSE;
13418
13419 msec = elf_next_in_group (msec);
13420 }
13421 while (msec != ssec);
13422
13423 /* If this is a pure debug section group or pure special section group,
13424 keep all sections in this group. */
13425 if (is_debug_grp || is_special_grp)
13426 {
13427 do
13428 {
13429 msec->gc_mark = 1;
13430 msec = elf_next_in_group (msec);
13431 }
13432 while (msec != ssec);
13433 }
13434}
13435
7f6ab9f8
AM
13436/* Keep debug and special sections. */
13437
13438bfd_boolean
13439_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13440 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13441{
13442 bfd *ibfd;
13443
c72f2fb2 13444 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13445 {
13446 asection *isec;
13447 bfd_boolean some_kept;
b40bf0a2 13448 bfd_boolean debug_frag_seen;
b7c871ed 13449 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13450
13451 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13452 continue;
57963c05
AM
13453 isec = ibfd->sections;
13454 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13455 continue;
7f6ab9f8 13456
b40bf0a2
NC
13457 /* Ensure all linker created sections are kept,
13458 see if any other section is already marked,
13459 and note if we have any fragmented debug sections. */
b7c871ed 13460 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13461 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13462 {
13463 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13464 isec->gc_mark = 1;
eb026f09
AM
13465 else if (isec->gc_mark
13466 && (isec->flags & SEC_ALLOC) != 0
13467 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13468 some_kept = TRUE;
b7d07216
L
13469 else
13470 {
13471 /* Since all sections, except for backend specific ones,
13472 have been garbage collected, call mark_hook on this
13473 section if any of its linked-to sections is marked. */
13474 asection *linked_to_sec = elf_linked_to_section (isec);
13475 for (; linked_to_sec != NULL;
13476 linked_to_sec = elf_linked_to_section (linked_to_sec))
13477 if (linked_to_sec->gc_mark)
13478 {
13479 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13480 return FALSE;
13481 break;
13482 }
13483 }
b40bf0a2 13484
535b785f 13485 if (!debug_frag_seen
b40bf0a2
NC
13486 && (isec->flags & SEC_DEBUGGING)
13487 && CONST_STRNEQ (isec->name, ".debug_line."))
13488 debug_frag_seen = TRUE;
5242a0a0
L
13489 else if (strcmp (bfd_section_name (isec),
13490 "__patchable_function_entries") == 0
13491 && elf_linked_to_section (isec) == NULL)
13492 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13493 "need linked-to section "
13494 "for --gc-sections\n"),
13495 isec->owner, isec);
7f6ab9f8
AM
13496 }
13497
eb026f09
AM
13498 /* If no non-note alloc section in this file will be kept, then
13499 we can toss out the debug and special sections. */
7f6ab9f8
AM
13500 if (!some_kept)
13501 continue;
13502
13503 /* Keep debug and special sections like .comment when they are
3c758495 13504 not part of a group. Also keep section groups that contain
b7d07216
L
13505 just debug sections or special sections. NB: Sections with
13506 linked-to section has been handled above. */
7f6ab9f8 13507 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13508 {
13509 if ((isec->flags & SEC_GROUP) != 0)
13510 _bfd_elf_gc_mark_debug_special_section_group (isec);
13511 else if (((isec->flags & SEC_DEBUGGING) != 0
13512 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13513 && elf_next_in_group (isec) == NULL
13514 && elf_linked_to_section (isec) == NULL)
3c758495 13515 isec->gc_mark = 1;
b7c871ed
L
13516 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13517 has_kept_debug_info = TRUE;
3c758495 13518 }
b40bf0a2 13519
b40bf0a2
NC
13520 /* Look for CODE sections which are going to be discarded,
13521 and find and discard any fragmented debug sections which
13522 are associated with that code section. */
b7c871ed
L
13523 if (debug_frag_seen)
13524 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13525 if ((isec->flags & SEC_CODE) != 0
13526 && isec->gc_mark == 0)
13527 {
13528 unsigned int ilen;
13529 asection *dsec;
b40bf0a2 13530
b7c871ed 13531 ilen = strlen (isec->name);
b40bf0a2 13532
b7c871ed 13533 /* Association is determined by the name of the debug
07d6d2b8 13534 section containing the name of the code section as
b7c871ed
L
13535 a suffix. For example .debug_line.text.foo is a
13536 debug section associated with .text.foo. */
13537 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13538 {
13539 unsigned int dlen;
b40bf0a2 13540
b7c871ed
L
13541 if (dsec->gc_mark == 0
13542 || (dsec->flags & SEC_DEBUGGING) == 0)
13543 continue;
b40bf0a2 13544
b7c871ed 13545 dlen = strlen (dsec->name);
b40bf0a2 13546
b7c871ed
L
13547 if (dlen > ilen
13548 && strncmp (dsec->name + (dlen - ilen),
13549 isec->name, ilen) == 0)
b40bf0a2 13550 dsec->gc_mark = 0;
b7c871ed 13551 }
b40bf0a2 13552 }
b7c871ed
L
13553
13554 /* Mark debug sections referenced by kept debug sections. */
13555 if (has_kept_debug_info)
13556 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13557 if (isec->gc_mark
13558 && (isec->flags & SEC_DEBUGGING) != 0)
13559 if (!_bfd_elf_gc_mark (info, isec,
13560 elf_gc_mark_debug_section))
13561 return FALSE;
7f6ab9f8
AM
13562 }
13563 return TRUE;
13564}
13565
c152c796 13566static bfd_boolean
ccabcbe5 13567elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13568{
13569 bfd *sub;
ccabcbe5 13570 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13571
c72f2fb2 13572 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13573 {
13574 asection *o;
13575
b19a8f85 13576 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13577 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13578 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13579 continue;
57963c05
AM
13580 o = sub->sections;
13581 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13582 continue;
c152c796
AM
13583
13584 for (o = sub->sections; o != NULL; o = o->next)
13585 {
a33dafc3
L
13586 /* When any section in a section group is kept, we keep all
13587 sections in the section group. If the first member of
13588 the section group is excluded, we will also exclude the
13589 group section. */
13590 if (o->flags & SEC_GROUP)
13591 {
13592 asection *first = elf_next_in_group (o);
13593 o->gc_mark = first->gc_mark;
13594 }
c152c796 13595
1e7eae0d 13596 if (o->gc_mark)
c152c796
AM
13597 continue;
13598
13599 /* Skip sweeping sections already excluded. */
13600 if (o->flags & SEC_EXCLUDE)
13601 continue;
13602
13603 /* Since this is early in the link process, it is simple
13604 to remove a section from the output. */
13605 o->flags |= SEC_EXCLUDE;
13606
c55fe096 13607 if (info->print_gc_sections && o->size != 0)
695344c0 13608 /* xgettext:c-format */
9793eb77 13609 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13610 o, sub);
c152c796
AM
13611 }
13612 }
13613
c152c796
AM
13614 return TRUE;
13615}
13616
13617/* Propagate collected vtable information. This is called through
13618 elf_link_hash_traverse. */
13619
13620static bfd_boolean
13621elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13622{
c152c796 13623 /* Those that are not vtables. */
cbd0eecf
L
13624 if (h->start_stop
13625 || h->u2.vtable == NULL
13626 || h->u2.vtable->parent == NULL)
c152c796
AM
13627 return TRUE;
13628
13629 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13630 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13631 return TRUE;
13632
13633 /* If we've already been done, exit. */
cbd0eecf 13634 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13635 return TRUE;
13636
13637 /* Make sure the parent's table is up to date. */
cbd0eecf 13638 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13639
cbd0eecf 13640 if (h->u2.vtable->used == NULL)
c152c796
AM
13641 {
13642 /* None of this table's entries were referenced. Re-use the
13643 parent's table. */
cbd0eecf
L
13644 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13645 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13646 }
13647 else
13648 {
13649 size_t n;
13650 bfd_boolean *cu, *pu;
13651
13652 /* Or the parent's entries into ours. */
cbd0eecf 13653 cu = h->u2.vtable->used;
c152c796 13654 cu[-1] = TRUE;
cbd0eecf 13655 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13656 if (pu != NULL)
13657 {
13658 const struct elf_backend_data *bed;
13659 unsigned int log_file_align;
13660
13661 bed = get_elf_backend_data (h->root.u.def.section->owner);
13662 log_file_align = bed->s->log_file_align;
cbd0eecf 13663 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13664 while (n--)
13665 {
13666 if (*pu)
13667 *cu = TRUE;
13668 pu++;
13669 cu++;
13670 }
13671 }
13672 }
13673
13674 return TRUE;
13675}
13676
13677static bfd_boolean
13678elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13679{
13680 asection *sec;
13681 bfd_vma hstart, hend;
13682 Elf_Internal_Rela *relstart, *relend, *rel;
13683 const struct elf_backend_data *bed;
13684 unsigned int log_file_align;
13685
c152c796
AM
13686 /* Take care of both those symbols that do not describe vtables as
13687 well as those that are not loaded. */
cbd0eecf
L
13688 if (h->start_stop
13689 || h->u2.vtable == NULL
13690 || h->u2.vtable->parent == NULL)
c152c796
AM
13691 return TRUE;
13692
13693 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13694 || h->root.type == bfd_link_hash_defweak);
13695
13696 sec = h->root.u.def.section;
13697 hstart = h->root.u.def.value;
13698 hend = hstart + h->size;
13699
13700 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13701 if (!relstart)
13702 return *(bfd_boolean *) okp = FALSE;
13703 bed = get_elf_backend_data (sec->owner);
13704 log_file_align = bed->s->log_file_align;
13705
056bafd4 13706 relend = relstart + sec->reloc_count;
c152c796
AM
13707
13708 for (rel = relstart; rel < relend; ++rel)
13709 if (rel->r_offset >= hstart && rel->r_offset < hend)
13710 {
13711 /* If the entry is in use, do nothing. */
cbd0eecf
L
13712 if (h->u2.vtable->used
13713 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13714 {
13715 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13716 if (h->u2.vtable->used[entry])
c152c796
AM
13717 continue;
13718 }
13719 /* Otherwise, kill it. */
13720 rel->r_offset = rel->r_info = rel->r_addend = 0;
13721 }
13722
13723 return TRUE;
13724}
13725
87538722
AM
13726/* Mark sections containing dynamically referenced symbols. When
13727 building shared libraries, we must assume that any visible symbol is
13728 referenced. */
715df9b8 13729
64d03ab5
AM
13730bfd_boolean
13731bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13732{
87538722 13733 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13734 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13735
715df9b8
EB
13736 if ((h->root.type == bfd_link_hash_defined
13737 || h->root.type == bfd_link_hash_defweak)
d664fd41 13738 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13739 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13740 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13741 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13742 && (!bfd_link_executable (info)
22185505 13743 || info->gc_keep_exported
b407645f
AM
13744 || info->export_dynamic
13745 || (h->dynamic
13746 && d != NULL
13747 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13748 && (h->versioned >= versioned
54e8959c
L
13749 || !bfd_hide_sym_by_version (info->version_info,
13750 h->root.root.string)))))
715df9b8
EB
13751 h->root.u.def.section->flags |= SEC_KEEP;
13752
13753 return TRUE;
13754}
3b36f7e6 13755
74f0fb50
AM
13756/* Keep all sections containing symbols undefined on the command-line,
13757 and the section containing the entry symbol. */
13758
13759void
13760_bfd_elf_gc_keep (struct bfd_link_info *info)
13761{
13762 struct bfd_sym_chain *sym;
13763
13764 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13765 {
13766 struct elf_link_hash_entry *h;
13767
13768 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13769 FALSE, FALSE, FALSE);
13770
13771 if (h != NULL
13772 && (h->root.type == bfd_link_hash_defined
13773 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13774 && !bfd_is_abs_section (h->root.u.def.section)
13775 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13776 h->root.u.def.section->flags |= SEC_KEEP;
13777 }
13778}
13779
2f0c68f2
CM
13780bfd_boolean
13781bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13782 struct bfd_link_info *info)
13783{
13784 bfd *ibfd = info->input_bfds;
13785
13786 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13787 {
13788 asection *sec;
13789 struct elf_reloc_cookie cookie;
13790
13791 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13792 continue;
57963c05
AM
13793 sec = ibfd->sections;
13794 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13795 continue;
2f0c68f2
CM
13796
13797 if (!init_reloc_cookie (&cookie, info, ibfd))
13798 return FALSE;
13799
13800 for (sec = ibfd->sections; sec; sec = sec->next)
13801 {
fd361982 13802 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13803 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13804 {
13805 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13806 fini_reloc_cookie_rels (&cookie, sec);
13807 }
13808 }
13809 }
13810 return TRUE;
13811}
13812
c152c796
AM
13813/* Do mark and sweep of unused sections. */
13814
13815bfd_boolean
13816bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13817{
13818 bfd_boolean ok = TRUE;
13819 bfd *sub;
6a5bb875 13820 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13821 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13822 struct elf_link_hash_table *htab;
c152c796 13823
64d03ab5 13824 if (!bed->can_gc_sections
715df9b8 13825 || !is_elf_hash_table (info->hash))
c152c796 13826 {
9793eb77 13827 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13828 return TRUE;
13829 }
13830
74f0fb50 13831 bed->gc_keep (info);
da44f4e5 13832 htab = elf_hash_table (info);
74f0fb50 13833
9d0a14d3
RS
13834 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13835 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13836 for (sub = info->input_bfds;
13837 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13838 sub = sub->link.next)
9d0a14d3
RS
13839 {
13840 asection *sec;
13841 struct elf_reloc_cookie cookie;
13842
57963c05
AM
13843 sec = sub->sections;
13844 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13845 continue;
9d0a14d3 13846 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13847 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13848 {
13849 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13850 if (elf_section_data (sec)->sec_info
13851 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13852 elf_eh_frame_section (sub) = sec;
13853 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13854 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13855 }
13856 }
9d0a14d3 13857
c152c796 13858 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13859 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13860 if (!ok)
13861 return FALSE;
13862
13863 /* Kill the vtable relocations that were not used. */
da44f4e5 13864 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13865 if (!ok)
13866 return FALSE;
13867
715df9b8 13868 /* Mark dynamically referenced symbols. */
22185505 13869 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13870 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13871
715df9b8 13872 /* Grovel through relocs to find out who stays ... */
64d03ab5 13873 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13874 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13875 {
13876 asection *o;
13877
b19a8f85 13878 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13879 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13880 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13881 continue;
13882
57963c05
AM
13883 o = sub->sections;
13884 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13885 continue;
13886
7f6ab9f8
AM
13887 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13888 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13889 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13890 well as FINI_ARRAY sections for ld -r. */
c152c796 13891 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13892 if (!o->gc_mark
13893 && (o->flags & SEC_EXCLUDE) == 0
24007750 13894 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13895 || (bfd_link_relocatable (info)
13896 && ((elf_section_data (o)->this_hdr.sh_type
13897 == SHT_PREINIT_ARRAY)
13898 || (elf_section_data (o)->this_hdr.sh_type
13899 == SHT_INIT_ARRAY)
13900 || (elf_section_data (o)->this_hdr.sh_type
13901 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13902 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13903 && elf_next_in_group (o) == NULL )))
13904 {
13905 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13906 return FALSE;
13907 }
c152c796
AM
13908 }
13909
6a5bb875 13910 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13911 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13912
c152c796 13913 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13914 return elf_gc_sweep (abfd, info);
c152c796
AM
13915}
13916\f
13917/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13918
13919bfd_boolean
13920bfd_elf_gc_record_vtinherit (bfd *abfd,
13921 asection *sec,
13922 struct elf_link_hash_entry *h,
13923 bfd_vma offset)
13924{
13925 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13926 struct elf_link_hash_entry **search, *child;
ef53be89 13927 size_t extsymcount;
c152c796
AM
13928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13929
13930 /* The sh_info field of the symtab header tells us where the
13931 external symbols start. We don't care about the local symbols at
13932 this point. */
13933 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13934 if (!elf_bad_symtab (abfd))
13935 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13936
13937 sym_hashes = elf_sym_hashes (abfd);
13938 sym_hashes_end = sym_hashes + extsymcount;
13939
13940 /* Hunt down the child symbol, which is in this section at the same
13941 offset as the relocation. */
13942 for (search = sym_hashes; search != sym_hashes_end; ++search)
13943 {
13944 if ((child = *search) != NULL
13945 && (child->root.type == bfd_link_hash_defined
13946 || child->root.type == bfd_link_hash_defweak)
13947 && child->root.u.def.section == sec
13948 && child->root.u.def.value == offset)
13949 goto win;
13950 }
13951
695344c0 13952 /* xgettext:c-format */
9793eb77 13953 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13954 abfd, sec, (uint64_t) offset);
c152c796
AM
13955 bfd_set_error (bfd_error_invalid_operation);
13956 return FALSE;
13957
13958 win:
cbd0eecf 13959 if (!child->u2.vtable)
f6e332e6 13960 {
cbd0eecf
L
13961 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13962 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13963 if (!child->u2.vtable)
f6e332e6
AM
13964 return FALSE;
13965 }
c152c796
AM
13966 if (!h)
13967 {
13968 /* This *should* only be the absolute section. It could potentially
13969 be that someone has defined a non-global vtable though, which
13970 would be bad. It isn't worth paging in the local symbols to be
13971 sure though; that case should simply be handled by the assembler. */
13972
cbd0eecf 13973 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13974 }
13975 else
cbd0eecf 13976 child->u2.vtable->parent = h;
c152c796
AM
13977
13978 return TRUE;
13979}
13980
13981/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13982
13983bfd_boolean
a0ea3a14 13984bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13985 struct elf_link_hash_entry *h,
13986 bfd_vma addend)
13987{
13988 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13989 unsigned int log_file_align = bed->s->log_file_align;
13990
a0ea3a14
L
13991 if (!h)
13992 {
13993 /* xgettext:c-format */
13994 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13995 abfd, sec);
13996 bfd_set_error (bfd_error_bad_value);
13997 return FALSE;
13998 }
13999
cbd0eecf 14000 if (!h->u2.vtable)
f6e332e6 14001 {
cbd0eecf
L
14002 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14003 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14004 if (!h->u2.vtable)
f6e332e6
AM
14005 return FALSE;
14006 }
14007
cbd0eecf 14008 if (addend >= h->u2.vtable->size)
c152c796
AM
14009 {
14010 size_t size, bytes, file_align;
cbd0eecf 14011 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14012
14013 /* While the symbol is undefined, we have to be prepared to handle
14014 a zero size. */
14015 file_align = 1 << log_file_align;
14016 if (h->root.type == bfd_link_hash_undefined)
14017 size = addend + file_align;
14018 else
14019 {
14020 size = h->size;
14021 if (addend >= size)
14022 {
14023 /* Oops! We've got a reference past the defined end of
14024 the table. This is probably a bug -- shall we warn? */
14025 size = addend + file_align;
14026 }
14027 }
14028 size = (size + file_align - 1) & -file_align;
14029
14030 /* Allocate one extra entry for use as a "done" flag for the
14031 consolidation pass. */
14032 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14033
14034 if (ptr)
14035 {
a50b1753 14036 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14037
14038 if (ptr != NULL)
14039 {
14040 size_t oldbytes;
14041
cbd0eecf 14042 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14043 * sizeof (bfd_boolean));
14044 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14045 }
14046 }
14047 else
a50b1753 14048 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14049
14050 if (ptr == NULL)
14051 return FALSE;
14052
14053 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14054 h->u2.vtable->used = ptr + 1;
14055 h->u2.vtable->size = size;
c152c796
AM
14056 }
14057
cbd0eecf 14058 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14059
14060 return TRUE;
14061}
14062
ae17ab41
CM
14063/* Map an ELF section header flag to its corresponding string. */
14064typedef struct
14065{
14066 char *flag_name;
14067 flagword flag_value;
14068} elf_flags_to_name_table;
14069
14070static elf_flags_to_name_table elf_flags_to_names [] =
14071{
14072 { "SHF_WRITE", SHF_WRITE },
14073 { "SHF_ALLOC", SHF_ALLOC },
14074 { "SHF_EXECINSTR", SHF_EXECINSTR },
14075 { "SHF_MERGE", SHF_MERGE },
14076 { "SHF_STRINGS", SHF_STRINGS },
14077 { "SHF_INFO_LINK", SHF_INFO_LINK},
14078 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14079 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14080 { "SHF_GROUP", SHF_GROUP },
14081 { "SHF_TLS", SHF_TLS },
14082 { "SHF_MASKOS", SHF_MASKOS },
14083 { "SHF_EXCLUDE", SHF_EXCLUDE },
14084};
14085
b9c361e0
JL
14086/* Returns TRUE if the section is to be included, otherwise FALSE. */
14087bfd_boolean
ae17ab41 14088bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14089 struct flag_info *flaginfo,
b9c361e0 14090 asection *section)
ae17ab41 14091{
8b127cbc 14092 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14093
8b127cbc 14094 if (!flaginfo->flags_initialized)
ae17ab41 14095 {
8b127cbc
AM
14096 bfd *obfd = info->output_bfd;
14097 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14098 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14099 int with_hex = 0;
14100 int without_hex = 0;
14101
8b127cbc 14102 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14103 {
b9c361e0 14104 unsigned i;
8b127cbc 14105 flagword (*lookup) (char *);
ae17ab41 14106
8b127cbc
AM
14107 lookup = bed->elf_backend_lookup_section_flags_hook;
14108 if (lookup != NULL)
ae17ab41 14109 {
8b127cbc 14110 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14111
14112 if (hexval != 0)
14113 {
14114 if (tf->with == with_flags)
14115 with_hex |= hexval;
14116 else if (tf->with == without_flags)
14117 without_hex |= hexval;
14118 tf->valid = TRUE;
14119 continue;
14120 }
ae17ab41 14121 }
8b127cbc 14122 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14123 {
8b127cbc 14124 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14125 {
14126 if (tf->with == with_flags)
14127 with_hex |= elf_flags_to_names[i].flag_value;
14128 else if (tf->with == without_flags)
14129 without_hex |= elf_flags_to_names[i].flag_value;
14130 tf->valid = TRUE;
14131 break;
14132 }
14133 }
8b127cbc 14134 if (!tf->valid)
b9c361e0 14135 {
68ffbac6 14136 info->callbacks->einfo
9793eb77 14137 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14138 return FALSE;
ae17ab41
CM
14139 }
14140 }
8b127cbc
AM
14141 flaginfo->flags_initialized = TRUE;
14142 flaginfo->only_with_flags |= with_hex;
14143 flaginfo->not_with_flags |= without_hex;
ae17ab41 14144 }
ae17ab41 14145
8b127cbc 14146 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14147 return FALSE;
14148
8b127cbc 14149 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14150 return FALSE;
14151
14152 return TRUE;
ae17ab41
CM
14153}
14154
c152c796
AM
14155struct alloc_got_off_arg {
14156 bfd_vma gotoff;
10455f89 14157 struct bfd_link_info *info;
c152c796
AM
14158};
14159
14160/* We need a special top-level link routine to convert got reference counts
14161 to real got offsets. */
14162
14163static bfd_boolean
14164elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14165{
a50b1753 14166 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14167 bfd *obfd = gofarg->info->output_bfd;
14168 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14169
c152c796
AM
14170 if (h->got.refcount > 0)
14171 {
14172 h->got.offset = gofarg->gotoff;
10455f89 14173 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14174 }
14175 else
14176 h->got.offset = (bfd_vma) -1;
14177
14178 return TRUE;
14179}
14180
14181/* And an accompanying bit to work out final got entry offsets once
14182 we're done. Should be called from final_link. */
14183
14184bfd_boolean
14185bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14186 struct bfd_link_info *info)
14187{
14188 bfd *i;
14189 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14190 bfd_vma gotoff;
c152c796
AM
14191 struct alloc_got_off_arg gofarg;
14192
10455f89
HPN
14193 BFD_ASSERT (abfd == info->output_bfd);
14194
c152c796
AM
14195 if (! is_elf_hash_table (info->hash))
14196 return FALSE;
14197
14198 /* The GOT offset is relative to the .got section, but the GOT header is
14199 put into the .got.plt section, if the backend uses it. */
14200 if (bed->want_got_plt)
14201 gotoff = 0;
14202 else
14203 gotoff = bed->got_header_size;
14204
14205 /* Do the local .got entries first. */
c72f2fb2 14206 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14207 {
14208 bfd_signed_vma *local_got;
ef53be89 14209 size_t j, locsymcount;
c152c796
AM
14210 Elf_Internal_Shdr *symtab_hdr;
14211
14212 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14213 continue;
14214
14215 local_got = elf_local_got_refcounts (i);
14216 if (!local_got)
14217 continue;
14218
14219 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14220 if (elf_bad_symtab (i))
14221 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14222 else
14223 locsymcount = symtab_hdr->sh_info;
14224
14225 for (j = 0; j < locsymcount; ++j)
14226 {
14227 if (local_got[j] > 0)
14228 {
14229 local_got[j] = gotoff;
10455f89 14230 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14231 }
14232 else
14233 local_got[j] = (bfd_vma) -1;
14234 }
14235 }
14236
14237 /* Then the global .got entries. .plt refcounts are handled by
14238 adjust_dynamic_symbol */
14239 gofarg.gotoff = gotoff;
10455f89 14240 gofarg.info = info;
c152c796
AM
14241 elf_link_hash_traverse (elf_hash_table (info),
14242 elf_gc_allocate_got_offsets,
14243 &gofarg);
14244 return TRUE;
14245}
14246
14247/* Many folk need no more in the way of final link than this, once
14248 got entry reference counting is enabled. */
14249
14250bfd_boolean
14251bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14252{
14253 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14254 return FALSE;
14255
14256 /* Invoke the regular ELF backend linker to do all the work. */
14257 return bfd_elf_final_link (abfd, info);
14258}
14259
14260bfd_boolean
14261bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14262{
a50b1753 14263 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14264
14265 if (rcookie->bad_symtab)
14266 rcookie->rel = rcookie->rels;
14267
14268 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14269 {
14270 unsigned long r_symndx;
14271
14272 if (! rcookie->bad_symtab)
14273 if (rcookie->rel->r_offset > offset)
14274 return FALSE;
14275 if (rcookie->rel->r_offset != offset)
14276 continue;
14277
14278 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14279 if (r_symndx == STN_UNDEF)
c152c796
AM
14280 return TRUE;
14281
14282 if (r_symndx >= rcookie->locsymcount
14283 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14284 {
14285 struct elf_link_hash_entry *h;
14286
14287 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14288
14289 while (h->root.type == bfd_link_hash_indirect
14290 || h->root.type == bfd_link_hash_warning)
14291 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14292
14293 if ((h->root.type == bfd_link_hash_defined
14294 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14295 && (h->root.u.def.section->owner != rcookie->abfd
14296 || h->root.u.def.section->kept_section != NULL
14297 || discarded_section (h->root.u.def.section)))
c152c796 14298 return TRUE;
c152c796
AM
14299 }
14300 else
14301 {
14302 /* It's not a relocation against a global symbol,
14303 but it could be a relocation against a local
14304 symbol for a discarded section. */
14305 asection *isec;
14306 Elf_Internal_Sym *isym;
14307
14308 /* Need to: get the symbol; get the section. */
14309 isym = &rcookie->locsyms[r_symndx];
cb33740c 14310 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14311 if (isec != NULL
14312 && (isec->kept_section != NULL
14313 || discarded_section (isec)))
cb33740c 14314 return TRUE;
c152c796
AM
14315 }
14316 return FALSE;
14317 }
14318 return FALSE;
14319}
14320
14321/* Discard unneeded references to discarded sections.
75938853
AM
14322 Returns -1 on error, 1 if any section's size was changed, 0 if
14323 nothing changed. This function assumes that the relocations are in
14324 sorted order, which is true for all known assemblers. */
c152c796 14325
75938853 14326int
c152c796
AM
14327bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14328{
14329 struct elf_reloc_cookie cookie;
18cd5bce 14330 asection *o;
c152c796 14331 bfd *abfd;
75938853 14332 int changed = 0;
c152c796
AM
14333
14334 if (info->traditional_format
14335 || !is_elf_hash_table (info->hash))
75938853 14336 return 0;
c152c796 14337
18cd5bce
AM
14338 o = bfd_get_section_by_name (output_bfd, ".stab");
14339 if (o != NULL)
c152c796 14340 {
18cd5bce 14341 asection *i;
c152c796 14342
18cd5bce 14343 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14344 {
18cd5bce
AM
14345 if (i->size == 0
14346 || i->reloc_count == 0
14347 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14348 continue;
c152c796 14349
18cd5bce
AM
14350 abfd = i->owner;
14351 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14352 continue;
c152c796 14353
18cd5bce 14354 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14355 return -1;
c152c796 14356
18cd5bce
AM
14357 if (_bfd_discard_section_stabs (abfd, i,
14358 elf_section_data (i)->sec_info,
5241d853
RS
14359 bfd_elf_reloc_symbol_deleted_p,
14360 &cookie))
75938853 14361 changed = 1;
18cd5bce
AM
14362
14363 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14364 }
18cd5bce
AM
14365 }
14366
2f0c68f2
CM
14367 o = NULL;
14368 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14369 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14370 if (o != NULL)
14371 {
14372 asection *i;
d7153c4a 14373 int eh_changed = 0;
66631823 14374 unsigned int eh_alignment; /* Octets. */
c152c796 14375
18cd5bce 14376 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14377 {
18cd5bce
AM
14378 if (i->size == 0)
14379 continue;
14380
14381 abfd = i->owner;
14382 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14383 continue;
14384
14385 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14386 return -1;
18cd5bce
AM
14387
14388 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14389 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14390 bfd_elf_reloc_symbol_deleted_p,
14391 &cookie))
d7153c4a
AM
14392 {
14393 eh_changed = 1;
14394 if (i->size != i->rawsize)
14395 changed = 1;
14396 }
18cd5bce
AM
14397
14398 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14399 }
9866ffe2 14400
66631823
CE
14401 eh_alignment = ((1 << o->alignment_power)
14402 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14403 /* Skip over zero terminator, and prevent empty sections from
14404 adding alignment padding at the end. */
14405 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14406 if (i->size == 0)
14407 i->flags |= SEC_EXCLUDE;
14408 else if (i->size > 4)
14409 break;
14410 /* The last non-empty eh_frame section doesn't need padding. */
14411 if (i != NULL)
14412 i = i->map_tail.s;
14413 /* Any prior sections must pad the last FDE out to the output
14414 section alignment. Otherwise we might have zero padding
14415 between sections, which would be seen as a terminator. */
14416 for (; i != NULL; i = i->map_tail.s)
14417 if (i->size == 4)
14418 /* All but the last zero terminator should have been removed. */
14419 BFD_FAIL ();
14420 else
14421 {
14422 bfd_size_type size
14423 = (i->size + eh_alignment - 1) & -eh_alignment;
14424 if (i->size != size)
af471f82 14425 {
9866ffe2
AM
14426 i->size = size;
14427 changed = 1;
14428 eh_changed = 1;
af471f82 14429 }
9866ffe2 14430 }
d7153c4a
AM
14431 if (eh_changed)
14432 elf_link_hash_traverse (elf_hash_table (info),
14433 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14434 }
c152c796 14435
18cd5bce
AM
14436 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14437 {
14438 const struct elf_backend_data *bed;
57963c05 14439 asection *s;
c152c796 14440
18cd5bce
AM
14441 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14442 continue;
57963c05
AM
14443 s = abfd->sections;
14444 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14445 continue;
18cd5bce
AM
14446
14447 bed = get_elf_backend_data (abfd);
14448
14449 if (bed->elf_backend_discard_info != NULL)
14450 {
14451 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14452 return -1;
18cd5bce
AM
14453
14454 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14455 changed = 1;
18cd5bce
AM
14456
14457 fini_reloc_cookie (&cookie, abfd);
14458 }
c152c796
AM
14459 }
14460
2f0c68f2
CM
14461 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14462 _bfd_elf_end_eh_frame_parsing (info);
14463
14464 if (info->eh_frame_hdr_type
0e1862bb 14465 && !bfd_link_relocatable (info)
c152c796 14466 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14467 changed = 1;
c152c796 14468
75938853 14469 return changed;
c152c796 14470}
082b7297 14471
43e1669b 14472bfd_boolean
0c511000 14473_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14474 asection *sec,
c0f00686 14475 struct bfd_link_info *info)
082b7297
L
14476{
14477 flagword flags;
c77ec726 14478 const char *name, *key;
082b7297
L
14479 struct bfd_section_already_linked *l;
14480 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14481
c77ec726
AM
14482 if (sec->output_section == bfd_abs_section_ptr)
14483 return FALSE;
0c511000 14484
c77ec726 14485 flags = sec->flags;
0c511000 14486
c77ec726
AM
14487 /* Return if it isn't a linkonce section. A comdat group section
14488 also has SEC_LINK_ONCE set. */
14489 if ((flags & SEC_LINK_ONCE) == 0)
14490 return FALSE;
0c511000 14491
c77ec726
AM
14492 /* Don't put group member sections on our list of already linked
14493 sections. They are handled as a group via their group section. */
14494 if (elf_sec_group (sec) != NULL)
14495 return FALSE;
0c511000 14496
c77ec726
AM
14497 /* For a SHT_GROUP section, use the group signature as the key. */
14498 name = sec->name;
14499 if ((flags & SEC_GROUP) != 0
14500 && elf_next_in_group (sec) != NULL
14501 && elf_group_name (elf_next_in_group (sec)) != NULL)
14502 key = elf_group_name (elf_next_in_group (sec));
14503 else
14504 {
14505 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14506 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14507 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14508 key++;
0c511000 14509 else
c77ec726
AM
14510 /* Must be a user linkonce section that doesn't follow gcc's
14511 naming convention. In this case we won't be matching
14512 single member groups. */
14513 key = name;
0c511000 14514 }
6d2cd210 14515
c77ec726 14516 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14517
14518 for (l = already_linked_list->entry; l != NULL; l = l->next)
14519 {
c2370991 14520 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14521 sections with a signature of <key> (<key> is some string),
14522 and linkonce sections named .gnu.linkonce.<type>.<key>.
14523 Match like sections. LTO plugin sections are an exception.
14524 They are always named .gnu.linkonce.t.<key> and match either
14525 type of section. */
14526 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14527 && ((flags & SEC_GROUP) != 0
14528 || strcmp (name, l->sec->name) == 0))
14529 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14530 {
14531 /* The section has already been linked. See if we should
6d2cd210 14532 issue a warning. */
c77ec726
AM
14533 if (!_bfd_handle_already_linked (sec, l, info))
14534 return FALSE;
082b7297 14535
c77ec726 14536 if (flags & SEC_GROUP)
3d7f7666 14537 {
c77ec726
AM
14538 asection *first = elf_next_in_group (sec);
14539 asection *s = first;
3d7f7666 14540
c77ec726 14541 while (s != NULL)
3d7f7666 14542 {
c77ec726
AM
14543 s->output_section = bfd_abs_section_ptr;
14544 /* Record which group discards it. */
14545 s->kept_section = l->sec;
14546 s = elf_next_in_group (s);
14547 /* These lists are circular. */
14548 if (s == first)
14549 break;
3d7f7666
L
14550 }
14551 }
082b7297 14552
43e1669b 14553 return TRUE;
082b7297
L
14554 }
14555 }
14556
c77ec726
AM
14557 /* A single member comdat group section may be discarded by a
14558 linkonce section and vice versa. */
14559 if ((flags & SEC_GROUP) != 0)
3d7f7666 14560 {
c77ec726 14561 asection *first = elf_next_in_group (sec);
c2370991 14562
c77ec726
AM
14563 if (first != NULL && elf_next_in_group (first) == first)
14564 /* Check this single member group against linkonce sections. */
14565 for (l = already_linked_list->entry; l != NULL; l = l->next)
14566 if ((l->sec->flags & SEC_GROUP) == 0
14567 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14568 {
14569 first->output_section = bfd_abs_section_ptr;
14570 first->kept_section = l->sec;
14571 sec->output_section = bfd_abs_section_ptr;
14572 break;
14573 }
14574 }
14575 else
14576 /* Check this linkonce section against single member groups. */
14577 for (l = already_linked_list->entry; l != NULL; l = l->next)
14578 if (l->sec->flags & SEC_GROUP)
6d2cd210 14579 {
c77ec726 14580 asection *first = elf_next_in_group (l->sec);
6d2cd210 14581
c77ec726
AM
14582 if (first != NULL
14583 && elf_next_in_group (first) == first
14584 && bfd_elf_match_symbols_in_sections (first, sec, info))
14585 {
14586 sec->output_section = bfd_abs_section_ptr;
14587 sec->kept_section = first;
14588 break;
14589 }
6d2cd210 14590 }
0c511000 14591
c77ec726
AM
14592 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14593 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14594 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14595 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14596 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14597 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14598 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14599 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14600 The reverse order cannot happen as there is never a bfd with only the
14601 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14602 matter as here were are looking only for cross-bfd sections. */
14603
14604 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14605 for (l = already_linked_list->entry; l != NULL; l = l->next)
14606 if ((l->sec->flags & SEC_GROUP) == 0
14607 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14608 {
14609 if (abfd != l->sec->owner)
14610 sec->output_section = bfd_abs_section_ptr;
14611 break;
14612 }
80c29487 14613
082b7297 14614 /* This is the first section with this name. Record it. */
c77ec726 14615 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14616 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14617 return sec->output_section == bfd_abs_section_ptr;
082b7297 14618}
81e1b023 14619
a4d8e49b
L
14620bfd_boolean
14621_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14622{
14623 return sym->st_shndx == SHN_COMMON;
14624}
14625
14626unsigned int
14627_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14628{
14629 return SHN_COMMON;
14630}
14631
14632asection *
14633_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14634{
14635 return bfd_com_section_ptr;
14636}
10455f89
HPN
14637
14638bfd_vma
14639_bfd_elf_default_got_elt_size (bfd *abfd,
14640 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14641 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14642 bfd *ibfd ATTRIBUTE_UNUSED,
14643 unsigned long symndx ATTRIBUTE_UNUSED)
14644{
14645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14646 return bed->s->arch_size / 8;
14647}
83bac4b0
NC
14648
14649/* Routines to support the creation of dynamic relocs. */
14650
83bac4b0
NC
14651/* Returns the name of the dynamic reloc section associated with SEC. */
14652
14653static const char *
14654get_dynamic_reloc_section_name (bfd * abfd,
14655 asection * sec,
14656 bfd_boolean is_rela)
14657{
ddcf1fcf 14658 char *name;
fd361982 14659 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14660 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14661
ddcf1fcf 14662 if (old_name == NULL)
83bac4b0
NC
14663 return NULL;
14664
ddcf1fcf 14665 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14666 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14667
14668 return name;
14669}
14670
14671/* Returns the dynamic reloc section associated with SEC.
14672 If necessary compute the name of the dynamic reloc section based
14673 on SEC's name (looked up in ABFD's string table) and the setting
14674 of IS_RELA. */
14675
14676asection *
14677_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14678 asection * sec,
14679 bfd_boolean is_rela)
14680{
14681 asection * reloc_sec = elf_section_data (sec)->sreloc;
14682
14683 if (reloc_sec == NULL)
14684 {
14685 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14686
14687 if (name != NULL)
14688 {
3d4d4302 14689 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14690
14691 if (reloc_sec != NULL)
14692 elf_section_data (sec)->sreloc = reloc_sec;
14693 }
14694 }
14695
14696 return reloc_sec;
14697}
14698
14699/* Returns the dynamic reloc section associated with SEC. If the
14700 section does not exist it is created and attached to the DYNOBJ
14701 bfd and stored in the SRELOC field of SEC's elf_section_data
14702 structure.
f8076f98 14703
83bac4b0
NC
14704 ALIGNMENT is the alignment for the newly created section and
14705 IS_RELA defines whether the name should be .rela.<SEC's name>
14706 or .rel.<SEC's name>. The section name is looked up in the
14707 string table associated with ABFD. */
14708
14709asection *
ca4be51c
AM
14710_bfd_elf_make_dynamic_reloc_section (asection *sec,
14711 bfd *dynobj,
14712 unsigned int alignment,
14713 bfd *abfd,
14714 bfd_boolean is_rela)
83bac4b0
NC
14715{
14716 asection * reloc_sec = elf_section_data (sec)->sreloc;
14717
14718 if (reloc_sec == NULL)
14719 {
14720 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14721
14722 if (name == NULL)
14723 return NULL;
14724
3d4d4302 14725 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14726
14727 if (reloc_sec == NULL)
14728 {
3d4d4302
AM
14729 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14730 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14731 if ((sec->flags & SEC_ALLOC) != 0)
14732 flags |= SEC_ALLOC | SEC_LOAD;
14733
3d4d4302 14734 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14735 if (reloc_sec != NULL)
14736 {
8877b5e5
AM
14737 /* _bfd_elf_get_sec_type_attr chooses a section type by
14738 name. Override as it may be wrong, eg. for a user
14739 section named "auto" we'll get ".relauto" which is
14740 seen to be a .rela section. */
14741 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14742 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14743 reloc_sec = NULL;
14744 }
14745 }
14746
14747 elf_section_data (sec)->sreloc = reloc_sec;
14748 }
14749
14750 return reloc_sec;
14751}
1338dd10 14752
bffebb6b
AM
14753/* Copy the ELF symbol type and other attributes for a linker script
14754 assignment from HSRC to HDEST. Generally this should be treated as
14755 if we found a strong non-dynamic definition for HDEST (except that
14756 ld ignores multiple definition errors). */
1338dd10 14757void
bffebb6b
AM
14758_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14759 struct bfd_link_hash_entry *hdest,
14760 struct bfd_link_hash_entry *hsrc)
1338dd10 14761{
bffebb6b
AM
14762 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14763 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14764 Elf_Internal_Sym isym;
1338dd10
PB
14765
14766 ehdest->type = ehsrc->type;
35fc36a8 14767 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14768
14769 isym.st_other = ehsrc->other;
b8417128 14770 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14771}
351f65ca
L
14772
14773/* Append a RELA relocation REL to section S in BFD. */
14774
14775void
14776elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14777{
14778 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14779 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14780 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14781 bed->s->swap_reloca_out (abfd, rel, loc);
14782}
14783
14784/* Append a REL relocation REL to section S in BFD. */
14785
14786void
14787elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14788{
14789 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14790 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14791 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14792 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14793}
7dba9362
AM
14794
14795/* Define __start, __stop, .startof. or .sizeof. symbol. */
14796
14797struct bfd_link_hash_entry *
14798bfd_elf_define_start_stop (struct bfd_link_info *info,
14799 const char *symbol, asection *sec)
14800{
487b6440 14801 struct elf_link_hash_entry *h;
7dba9362 14802
487b6440
AM
14803 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14804 FALSE, FALSE, TRUE);
e1b5d517 14805 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
14806 if (h != NULL
14807 && (h->root.type == bfd_link_hash_undefined
14808 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
14809 || ((h->ref_regular || h->def_dynamic)
14810 && !h->def_regular
14811 && h->root.type != bfd_link_hash_common)))
7dba9362 14812 {
bf3077a6 14813 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 14814 h->verinfo.verdef = NULL;
487b6440
AM
14815 h->root.type = bfd_link_hash_defined;
14816 h->root.u.def.section = sec;
14817 h->root.u.def.value = 0;
14818 h->def_regular = 1;
14819 h->def_dynamic = 0;
14820 h->start_stop = 1;
14821 h->u2.start_stop_section = sec;
14822 if (symbol[0] == '.')
14823 {
14824 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14825 const struct elf_backend_data *bed;
14826 bed = get_elf_backend_data (info->output_bfd);
14827 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14828 }
36b8fda5
AM
14829 else
14830 {
14831 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14832 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14833 if (was_dynamic)
36b8fda5
AM
14834 bfd_elf_link_record_dynamic_symbol (info, h);
14835 }
487b6440 14836 return &h->root;
7dba9362 14837 }
487b6440 14838 return NULL;
7dba9362 14839}
5dbc8b37
L
14840
14841/* Find dynamic relocs for H that apply to read-only sections. */
14842
14843asection *
14844_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
14845{
14846 struct elf_dyn_relocs *p;
14847
14848 for (p = h->dyn_relocs; p != NULL; p = p->next)
14849 {
14850 asection *s = p->sec->output_section;
14851
14852 if (s != NULL && (s->flags & SEC_READONLY) != 0)
14853 return p->sec;
14854 }
14855 return NULL;
14856}
d49e5065
L
14857
14858/* Set DF_TEXTREL if we find any dynamic relocs that apply to
14859 read-only sections. */
14860
14861bfd_boolean
14862_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
14863{
14864 asection *sec;
14865
14866 if (h->root.type == bfd_link_hash_indirect)
14867 return TRUE;
14868
14869 sec = _bfd_elf_readonly_dynrelocs (h);
14870 if (sec != NULL)
14871 {
14872 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14873
14874 info->flags |= DF_TEXTREL;
14875 /* xgettext:c-format */
14876 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
14877 "in read-only section `%pA'\n"),
14878 sec->owner, h->root.root.string, sec);
14879
14880 if (bfd_link_textrel_check (info))
14881 /* xgettext:c-format */
14882 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
14883 "in read-only section `%pA'\n"),
14884 sec->owner, h->root.root.string, sec);
14885
14886 /* Not an error, just cut short the traversal. */
14887 return FALSE;
14888 }
14889 return TRUE;
14890}
This page took 2.893307 seconds and 4 git commands to generate.