PR26506 UBSAN: elf32-xtensa.c:3203 null pointer memcpy
[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
a896df97
AM
508 if (h->root.type == bfd_link_hash_defined
509 || h->root.type == bfd_link_hash_defweak)
510 {
511 /* An IR symbol should not be made dynamic. */
512 if (h->root.u.def.section != NULL
513 && h->root.u.def.section->owner != NULL
514 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
515 return TRUE;
516 }
517
7a13edea
NC
518 /* XXX: The ABI draft says the linker must turn hidden and
519 internal symbols into STB_LOCAL symbols when producing the
520 DSO. However, if ld.so honors st_other in the dynamic table,
521 this would not be necessary. */
522 switch (ELF_ST_VISIBILITY (h->other))
523 {
524 case STV_INTERNAL:
525 case STV_HIDDEN:
9d6eee78
L
526 if (h->root.type != bfd_link_hash_undefined
527 && h->root.type != bfd_link_hash_undefweak)
38048eb9 528 {
f5385ebf 529 h->forced_local = 1;
67687978
PB
530 if (!elf_hash_table (info)->is_relocatable_executable)
531 return TRUE;
7a13edea 532 }
0444bdd4 533
7a13edea
NC
534 default:
535 break;
536 }
537
252b5132
RH
538 h->dynindx = elf_hash_table (info)->dynsymcount;
539 ++elf_hash_table (info)->dynsymcount;
540
541 dynstr = elf_hash_table (info)->dynstr;
542 if (dynstr == NULL)
543 {
544 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 545 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 546 if (dynstr == NULL)
b34976b6 547 return FALSE;
252b5132
RH
548 }
549
550 /* We don't put any version information in the dynamic string
aad5d350 551 table. */
252b5132
RH
552 name = h->root.root.string;
553 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
554 if (p != NULL)
555 /* We know that the p points into writable memory. In fact,
556 there are only a few symbols that have read-only names, being
557 those like _GLOBAL_OFFSET_TABLE_ that are created specially
558 by the backends. Most symbols will have names pointing into
559 an ELF string table read from a file, or to objalloc memory. */
560 *p = 0;
561
562 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
563
564 if (p != NULL)
565 *p = ELF_VER_CHR;
252b5132 566
ef53be89 567 if (indx == (size_t) -1)
b34976b6 568 return FALSE;
252b5132
RH
569 h->dynstr_index = indx;
570 }
571
b34976b6 572 return TRUE;
252b5132 573}
45d6a902 574\f
55255dae
L
575/* Mark a symbol dynamic. */
576
28caa186 577static void
55255dae 578bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
579 struct elf_link_hash_entry *h,
580 Elf_Internal_Sym *sym)
55255dae 581{
40b36307 582 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 583
40b36307 584 /* It may be called more than once on the same H. */
0e1862bb 585 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
586 return;
587
40b36307
L
588 if ((info->dynamic_data
589 && (h->type == STT_OBJECT
b8871f35 590 || h->type == STT_COMMON
40b36307 591 || (sym != NULL
b8871f35
L
592 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
593 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 594 || (d != NULL
73ec947d 595 && h->non_elf
40b36307 596 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
597 {
598 h->dynamic = 1;
599 /* NB: If a symbol is made dynamic by --dynamic-list, it has
600 non-IR reference. */
601 h->root.non_ir_ref_dynamic = 1;
602 }
55255dae
L
603}
604
45d6a902
AM
605/* Record an assignment to a symbol made by a linker script. We need
606 this in case some dynamic object refers to this symbol. */
607
608bfd_boolean
fe21a8fc
L
609bfd_elf_record_link_assignment (bfd *output_bfd,
610 struct bfd_link_info *info,
268b6b39 611 const char *name,
fe21a8fc
L
612 bfd_boolean provide,
613 bfd_boolean hidden)
45d6a902 614{
00cbee0a 615 struct elf_link_hash_entry *h, *hv;
4ea42fb7 616 struct elf_link_hash_table *htab;
00cbee0a 617 const struct elf_backend_data *bed;
45d6a902 618
0eddce27 619 if (!is_elf_hash_table (info->hash))
45d6a902
AM
620 return TRUE;
621
4ea42fb7
AM
622 htab = elf_hash_table (info);
623 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 624 if (h == NULL)
4ea42fb7 625 return provide;
45d6a902 626
8e2a4f11
AM
627 if (h->root.type == bfd_link_hash_warning)
628 h = (struct elf_link_hash_entry *) h->root.u.i.link;
629
0f550b3d
L
630 if (h->versioned == unknown)
631 {
632 /* Set versioned if symbol version is unknown. */
633 char *version = strrchr (name, ELF_VER_CHR);
634 if (version)
635 {
636 if (version > name && version[-1] != ELF_VER_CHR)
637 h->versioned = versioned_hidden;
638 else
639 h->versioned = versioned;
640 }
641 }
642
73ec947d
AM
643 /* Symbols defined in a linker script but not referenced anywhere
644 else will have non_elf set. */
645 if (h->non_elf)
646 {
647 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
648 h->non_elf = 0;
649 }
650
00cbee0a 651 switch (h->root.type)
77cfaee6 652 {
00cbee0a
L
653 case bfd_link_hash_defined:
654 case bfd_link_hash_defweak:
655 case bfd_link_hash_common:
656 break;
657 case bfd_link_hash_undefweak:
658 case bfd_link_hash_undefined:
659 /* Since we're defining the symbol, don't let it seem to have not
660 been defined. record_dynamic_symbol and size_dynamic_sections
661 may depend on this. */
4ea42fb7 662 h->root.type = bfd_link_hash_new;
77cfaee6
AM
663 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
664 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
665 break;
666 case bfd_link_hash_new:
00cbee0a
L
667 break;
668 case bfd_link_hash_indirect:
669 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 670 the versioned symbol point to this one. */
00cbee0a
L
671 bed = get_elf_backend_data (output_bfd);
672 hv = h;
673 while (hv->root.type == bfd_link_hash_indirect
674 || hv->root.type == bfd_link_hash_warning)
675 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
676 /* We don't need to update h->root.u since linker will set them
677 later. */
678 h->root.type = bfd_link_hash_undefined;
679 hv->root.type = bfd_link_hash_indirect;
680 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
681 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
682 break;
8e2a4f11
AM
683 default:
684 BFD_FAIL ();
c2596ca5 685 return FALSE;
55255dae 686 }
45d6a902
AM
687
688 /* If this symbol is being provided by the linker script, and it is
689 currently defined by a dynamic object, but not by a regular
690 object, then mark it as undefined so that the generic linker will
691 force the correct value. */
692 if (provide
f5385ebf
AM
693 && h->def_dynamic
694 && !h->def_regular)
45d6a902
AM
695 h->root.type = bfd_link_hash_undefined;
696
48e30f52
L
697 /* If this symbol is currently defined by a dynamic object, but not
698 by a regular object, then clear out any version information because
699 the symbol will not be associated with the dynamic object any
700 more. */
701 if (h->def_dynamic && !h->def_regular)
b531344c
MR
702 h->verinfo.verdef = NULL;
703
704 /* Make sure this symbol is not garbage collected. */
705 h->mark = 1;
45d6a902 706
f5385ebf 707 h->def_regular = 1;
45d6a902 708
eb8476a6 709 if (hidden)
fe21a8fc 710 {
91d6fa6a 711 bed = get_elf_backend_data (output_bfd);
b8297068
AM
712 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
713 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
714 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
715 }
716
6fa3860b
PB
717 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
718 and executables. */
0e1862bb 719 if (!bfd_link_relocatable (info)
6fa3860b
PB
720 && h->dynindx != -1
721 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
722 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
723 h->forced_local = 1;
724
f5385ebf
AM
725 if ((h->def_dynamic
726 || h->ref_dynamic
6b3b0ab8
L
727 || bfd_link_dll (info)
728 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 729 && !h->forced_local
45d6a902
AM
730 && h->dynindx == -1)
731 {
c152c796 732 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
733 return FALSE;
734
735 /* If this is a weak defined symbol, and we know a corresponding
736 real symbol from the same dynamic object, make sure the real
737 symbol is also made into a dynamic symbol. */
60d67dc8 738 if (h->is_weakalias)
45d6a902 739 {
60d67dc8
AM
740 struct elf_link_hash_entry *def = weakdef (h);
741
742 if (def->dynindx == -1
743 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
744 return FALSE;
745 }
746 }
747
748 return TRUE;
749}
42751cf3 750
8c58d23b
AM
751/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
752 success, and 2 on a failure caused by attempting to record a symbol
753 in a discarded section, eg. a discarded link-once section symbol. */
754
755int
c152c796
AM
756bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
757 bfd *input_bfd,
758 long input_indx)
8c58d23b 759{
986f0783 760 size_t amt;
8c58d23b
AM
761 struct elf_link_local_dynamic_entry *entry;
762 struct elf_link_hash_table *eht;
763 struct elf_strtab_hash *dynstr;
ef53be89 764 size_t dynstr_index;
8c58d23b
AM
765 char *name;
766 Elf_External_Sym_Shndx eshndx;
767 char esym[sizeof (Elf64_External_Sym)];
768
0eddce27 769 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
770 return 0;
771
772 /* See if the entry exists already. */
773 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
774 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
775 return 1;
776
777 amt = sizeof (*entry);
a50b1753 778 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
779 if (entry == NULL)
780 return 0;
781
782 /* Go find the symbol, so that we can find it's name. */
783 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 784 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
785 {
786 bfd_release (input_bfd, entry);
787 return 0;
788 }
789
790 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 791 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
792 {
793 asection *s;
794
795 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
796 if (s == NULL || bfd_is_abs_section (s->output_section))
797 {
798 /* We can still bfd_release here as nothing has done another
799 bfd_alloc. We can't do this later in this function. */
800 bfd_release (input_bfd, entry);
801 return 2;
802 }
803 }
804
805 name = (bfd_elf_string_from_elf_section
806 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
807 entry->isym.st_name));
808
809 dynstr = elf_hash_table (info)->dynstr;
810 if (dynstr == NULL)
811 {
812 /* Create a strtab to hold the dynamic symbol names. */
813 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
814 if (dynstr == NULL)
815 return 0;
816 }
817
b34976b6 818 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 819 if (dynstr_index == (size_t) -1)
8c58d23b
AM
820 return 0;
821 entry->isym.st_name = dynstr_index;
822
823 eht = elf_hash_table (info);
824
825 entry->next = eht->dynlocal;
826 eht->dynlocal = entry;
827 entry->input_bfd = input_bfd;
828 entry->input_indx = input_indx;
829 eht->dynsymcount++;
830
831 /* Whatever binding the symbol had before, it's now local. */
832 entry->isym.st_info
833 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
834
835 /* The dynindx will be set at the end of size_dynamic_sections. */
836
837 return 1;
838}
839
30b30c21 840/* Return the dynindex of a local dynamic symbol. */
42751cf3 841
30b30c21 842long
268b6b39
AM
843_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
844 bfd *input_bfd,
845 long input_indx)
30b30c21
RH
846{
847 struct elf_link_local_dynamic_entry *e;
848
849 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
850 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
851 return e->dynindx;
852 return -1;
853}
854
855/* This function is used to renumber the dynamic symbols, if some of
856 them are removed because they are marked as local. This is called
857 via elf_link_hash_traverse. */
858
b34976b6 859static bfd_boolean
268b6b39
AM
860elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
861 void *data)
42751cf3 862{
a50b1753 863 size_t *count = (size_t *) data;
30b30c21 864
6fa3860b
PB
865 if (h->forced_local)
866 return TRUE;
867
868 if (h->dynindx != -1)
869 h->dynindx = ++(*count);
870
871 return TRUE;
872}
873
874
875/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
876 STB_LOCAL binding. */
877
878static bfd_boolean
879elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
880 void *data)
881{
a50b1753 882 size_t *count = (size_t *) data;
6fa3860b 883
6fa3860b
PB
884 if (!h->forced_local)
885 return TRUE;
886
42751cf3 887 if (h->dynindx != -1)
30b30c21
RH
888 h->dynindx = ++(*count);
889
b34976b6 890 return TRUE;
42751cf3 891}
30b30c21 892
aee6f5b4
AO
893/* Return true if the dynamic symbol for a given section should be
894 omitted when creating a shared library. */
895bfd_boolean
d00dd7dc
AM
896_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
897 struct bfd_link_info *info,
898 asection *p)
aee6f5b4 899{
74541ad4 900 struct elf_link_hash_table *htab;
ca55926c 901 asection *ip;
74541ad4 902
aee6f5b4
AO
903 switch (elf_section_data (p)->this_hdr.sh_type)
904 {
905 case SHT_PROGBITS:
906 case SHT_NOBITS:
907 /* If sh_type is yet undecided, assume it could be
908 SHT_PROGBITS/SHT_NOBITS. */
909 case SHT_NULL:
74541ad4 910 htab = elf_hash_table (info);
74541ad4
AM
911 if (htab->text_index_section != NULL)
912 return p != htab->text_index_section && p != htab->data_index_section;
913
ca55926c 914 return (htab->dynobj != NULL
3d4d4302 915 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 916 && ip->output_section == p);
aee6f5b4
AO
917
918 /* There shouldn't be section relative relocations
919 against any other section. */
920 default:
921 return TRUE;
922 }
923}
924
d00dd7dc
AM
925bfd_boolean
926_bfd_elf_omit_section_dynsym_all
927 (bfd *output_bfd ATTRIBUTE_UNUSED,
928 struct bfd_link_info *info ATTRIBUTE_UNUSED,
929 asection *p ATTRIBUTE_UNUSED)
930{
931 return TRUE;
932}
933
062e2358 934/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
935 symbol for each output section, which come first. Next come symbols
936 which have been forced to local binding. Then all of the back-end
937 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
938 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
939 (This prevents the early call before elf_backend_init_index_section
940 and strip_excluded_output_sections setting dynindx for sections
941 that are stripped.) */
30b30c21 942
554220db
AM
943static unsigned long
944_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
945 struct bfd_link_info *info,
946 unsigned long *section_sym_count)
30b30c21
RH
947{
948 unsigned long dynsymcount = 0;
63f452a8 949 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 950
0e1862bb
L
951 if (bfd_link_pic (info)
952 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 953 {
aee6f5b4 954 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
955 asection *p;
956 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 957 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 958 && (p->flags & SEC_ALLOC) != 0
7f923b7f 959 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 960 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
961 {
962 ++dynsymcount;
963 if (do_sec)
964 elf_section_data (p)->dynindx = dynsymcount;
965 }
966 else if (do_sec)
74541ad4 967 elf_section_data (p)->dynindx = 0;
30b30c21 968 }
63f452a8
AM
969 if (do_sec)
970 *section_sym_count = dynsymcount;
30b30c21 971
6fa3860b
PB
972 elf_link_hash_traverse (elf_hash_table (info),
973 elf_link_renumber_local_hash_table_dynsyms,
974 &dynsymcount);
975
30b30c21
RH
976 if (elf_hash_table (info)->dynlocal)
977 {
978 struct elf_link_local_dynamic_entry *p;
979 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
980 p->dynindx = ++dynsymcount;
981 }
90ac2420 982 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
983
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_hash_table_dynsyms,
986 &dynsymcount);
987
d5486c43
L
988 /* There is an unused NULL entry at the head of the table which we
989 must account for in our count even if the table is empty since it
990 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
991 .dynamic section. */
992 dynsymcount++;
30b30c21 993
ccabcbe5
AM
994 elf_hash_table (info)->dynsymcount = dynsymcount;
995 return dynsymcount;
30b30c21 996}
252b5132 997
54ac0771
L
998/* Merge st_other field. */
999
1000static void
1001elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 1002 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 1003 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1004{
1005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1006
1007 /* If st_other has a processor-specific meaning, specific
cd3416da 1008 code might be needed here. */
54ac0771
L
1009 if (bed->elf_backend_merge_symbol_attribute)
1010 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1011 dynamic);
1012
cd3416da 1013 if (!dynamic)
54ac0771 1014 {
cd3416da
AM
1015 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1016 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1017
cd3416da
AM
1018 /* Keep the most constraining visibility. Leave the remainder
1019 of the st_other field to elf_backend_merge_symbol_attribute. */
1020 if (symvis - 1 < hvis - 1)
1021 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1022 }
b8417128
AM
1023 else if (definition
1024 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1025 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1026 h->protected_def = 1;
54ac0771
L
1027}
1028
4f3fedcf
AM
1029/* This function is called when we want to merge a new symbol with an
1030 existing symbol. It handles the various cases which arise when we
1031 find a definition in a dynamic object, or when there is already a
1032 definition in a dynamic object. The new symbol is described by
1033 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1034 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1035 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1036 of an old common symbol. We set OVERRIDE if the old symbol is
1037 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1038 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1039 to change. By OK to change, we mean that we shouldn't warn if the
1040 type or size does change. */
45d6a902 1041
8a56bd02 1042static bfd_boolean
268b6b39
AM
1043_bfd_elf_merge_symbol (bfd *abfd,
1044 struct bfd_link_info *info,
1045 const char *name,
1046 Elf_Internal_Sym *sym,
1047 asection **psec,
1048 bfd_vma *pvalue,
4f3fedcf
AM
1049 struct elf_link_hash_entry **sym_hash,
1050 bfd **poldbfd,
37a9e49a 1051 bfd_boolean *pold_weak,
af44c138 1052 unsigned int *pold_alignment,
268b6b39
AM
1053 bfd_boolean *skip,
1054 bfd_boolean *override,
1055 bfd_boolean *type_change_ok,
6e33951e
L
1056 bfd_boolean *size_change_ok,
1057 bfd_boolean *matched)
252b5132 1058{
7479dfd4 1059 asection *sec, *oldsec;
45d6a902 1060 struct elf_link_hash_entry *h;
90c984fc 1061 struct elf_link_hash_entry *hi;
45d6a902
AM
1062 struct elf_link_hash_entry *flip;
1063 int bind;
1064 bfd *oldbfd;
1065 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1066 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1067 const struct elf_backend_data *bed;
6e33951e 1068 char *new_version;
93f4de39 1069 bfd_boolean default_sym = *matched;
45d6a902
AM
1070
1071 *skip = FALSE;
1072 *override = FALSE;
1073
1074 sec = *psec;
1075 bind = ELF_ST_BIND (sym->st_info);
1076
1077 if (! bfd_is_und_section (sec))
1078 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1079 else
1080 h = ((struct elf_link_hash_entry *)
1081 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1082 if (h == NULL)
1083 return FALSE;
1084 *sym_hash = h;
252b5132 1085
88ba32a0
L
1086 bed = get_elf_backend_data (abfd);
1087
6e33951e 1088 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1089 if (h->versioned != unversioned)
6e33951e 1090 {
422f1182
L
1091 /* Symbol version is unknown or versioned. */
1092 new_version = strrchr (name, ELF_VER_CHR);
1093 if (new_version)
1094 {
1095 if (h->versioned == unknown)
1096 {
1097 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1098 h->versioned = versioned_hidden;
1099 else
1100 h->versioned = versioned;
1101 }
1102 new_version += 1;
1103 if (new_version[0] == '\0')
1104 new_version = NULL;
1105 }
1106 else
1107 h->versioned = unversioned;
6e33951e 1108 }
422f1182
L
1109 else
1110 new_version = NULL;
6e33951e 1111
90c984fc
L
1112 /* For merging, we only care about real symbols. But we need to make
1113 sure that indirect symbol dynamic flags are updated. */
1114 hi = h;
45d6a902
AM
1115 while (h->root.type == bfd_link_hash_indirect
1116 || h->root.type == bfd_link_hash_warning)
1117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1118
6e33951e
L
1119 if (!*matched)
1120 {
1121 if (hi == h || h->root.type == bfd_link_hash_new)
1122 *matched = TRUE;
1123 else
1124 {
ae7683d2 1125 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1126 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1127 true if the new symbol is only visible to the symbol with
6e33951e 1128 the same symbol version. */
422f1182
L
1129 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1130 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1131 if (!old_hidden && !new_hidden)
1132 /* The new symbol matches the existing symbol if both
1133 aren't hidden. */
1134 *matched = TRUE;
1135 else
1136 {
1137 /* OLD_VERSION is the symbol version of the existing
1138 symbol. */
422f1182
L
1139 char *old_version;
1140
1141 if (h->versioned >= versioned)
1142 old_version = strrchr (h->root.root.string,
1143 ELF_VER_CHR) + 1;
1144 else
1145 old_version = NULL;
6e33951e
L
1146
1147 /* The new symbol matches the existing symbol if they
1148 have the same symbol version. */
1149 *matched = (old_version == new_version
1150 || (old_version != NULL
1151 && new_version != NULL
1152 && strcmp (old_version, new_version) == 0));
1153 }
1154 }
1155 }
1156
934bce08
AM
1157 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1158 existing symbol. */
1159
1160 oldbfd = NULL;
1161 oldsec = NULL;
1162 switch (h->root.type)
1163 {
1164 default:
1165 break;
1166
1167 case bfd_link_hash_undefined:
1168 case bfd_link_hash_undefweak:
1169 oldbfd = h->root.u.undef.abfd;
1170 break;
1171
1172 case bfd_link_hash_defined:
1173 case bfd_link_hash_defweak:
1174 oldbfd = h->root.u.def.section->owner;
1175 oldsec = h->root.u.def.section;
1176 break;
1177
1178 case bfd_link_hash_common:
1179 oldbfd = h->root.u.c.p->section->owner;
1180 oldsec = h->root.u.c.p->section;
1181 if (pold_alignment)
1182 *pold_alignment = h->root.u.c.p->alignment_power;
1183 break;
1184 }
1185 if (poldbfd && *poldbfd == NULL)
1186 *poldbfd = oldbfd;
1187
1188 /* Differentiate strong and weak symbols. */
1189 newweak = bind == STB_WEAK;
1190 oldweak = (h->root.type == bfd_link_hash_defweak
1191 || h->root.type == bfd_link_hash_undefweak);
1192 if (pold_weak)
1193 *pold_weak = oldweak;
1194
40b36307 1195 /* We have to check it for every instance since the first few may be
ee659f1f 1196 references and not all compilers emit symbol type for undefined
40b36307
L
1197 symbols. */
1198 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1199
ee659f1f
AM
1200 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1201 respectively, is from a dynamic object. */
1202
1203 newdyn = (abfd->flags & DYNAMIC) != 0;
1204
1205 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1206 syms and defined syms in dynamic libraries respectively.
1207 ref_dynamic on the other hand can be set for a symbol defined in
1208 a dynamic library, and def_dynamic may not be set; When the
1209 definition in a dynamic lib is overridden by a definition in the
1210 executable use of the symbol in the dynamic lib becomes a
1211 reference to the executable symbol. */
1212 if (newdyn)
1213 {
1214 if (bfd_is_und_section (sec))
1215 {
1216 if (bind != STB_WEAK)
1217 {
1218 h->ref_dynamic_nonweak = 1;
1219 hi->ref_dynamic_nonweak = 1;
1220 }
1221 }
1222 else
1223 {
6e33951e
L
1224 /* Update the existing symbol only if they match. */
1225 if (*matched)
1226 h->dynamic_def = 1;
ee659f1f
AM
1227 hi->dynamic_def = 1;
1228 }
1229 }
1230
45d6a902
AM
1231 /* If we just created the symbol, mark it as being an ELF symbol.
1232 Other than that, there is nothing to do--there is no merge issue
1233 with a newly defined symbol--so we just return. */
1234
1235 if (h->root.type == bfd_link_hash_new)
252b5132 1236 {
f5385ebf 1237 h->non_elf = 0;
45d6a902
AM
1238 return TRUE;
1239 }
252b5132 1240
45d6a902
AM
1241 /* In cases involving weak versioned symbols, we may wind up trying
1242 to merge a symbol with itself. Catch that here, to avoid the
1243 confusion that results if we try to override a symbol with
1244 itself. The additional tests catch cases like
1245 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1246 dynamic object, which we do want to handle here. */
1247 if (abfd == oldbfd
895fa45f 1248 && (newweak || oldweak)
45d6a902 1249 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1250 || !h->def_regular))
45d6a902
AM
1251 return TRUE;
1252
707bba77 1253 olddyn = FALSE;
45d6a902
AM
1254 if (oldbfd != NULL)
1255 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1256 else if (oldsec != NULL)
45d6a902 1257 {
707bba77 1258 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1259 indices used by MIPS ELF. */
707bba77 1260 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1261 }
252b5132 1262
1a3b5c34
AM
1263 /* Handle a case where plugin_notice won't be called and thus won't
1264 set the non_ir_ref flags on the first pass over symbols. */
1265 if (oldbfd != NULL
1266 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1267 && newdyn != olddyn)
1268 {
1269 h->root.non_ir_ref_dynamic = TRUE;
1270 hi->root.non_ir_ref_dynamic = TRUE;
1271 }
1272
45d6a902
AM
1273 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1274 respectively, appear to be a definition rather than reference. */
1275
707bba77 1276 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1277
707bba77
AM
1278 olddef = (h->root.type != bfd_link_hash_undefined
1279 && h->root.type != bfd_link_hash_undefweak
202ac193 1280 && h->root.type != bfd_link_hash_common);
45d6a902 1281
0a36a439
L
1282 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1283 respectively, appear to be a function. */
1284
1285 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1287
1288 oldfunc = (h->type != STT_NOTYPE
1289 && bed->is_function_type (h->type));
1290
c5d37467 1291 if (!(newfunc && oldfunc)
5b677558
AM
1292 && ELF_ST_TYPE (sym->st_info) != h->type
1293 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1294 && h->type != STT_NOTYPE
c5d37467
AM
1295 && (newdef || bfd_is_com_section (sec))
1296 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1297 {
c5d37467
AM
1298 /* If creating a default indirect symbol ("foo" or "foo@") from
1299 a dynamic versioned definition ("foo@@") skip doing so if
1300 there is an existing regular definition with a different
1301 type. We don't want, for example, a "time" variable in the
1302 executable overriding a "time" function in a shared library. */
1303 if (newdyn
1304 && !olddyn)
1305 {
1306 *skip = TRUE;
1307 return TRUE;
1308 }
1309
1310 /* When adding a symbol from a regular object file after we have
1311 created indirect symbols, undo the indirection and any
1312 dynamic state. */
1313 if (hi != h
1314 && !newdyn
1315 && olddyn)
1316 {
1317 h = hi;
1318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1319 h->forced_local = 0;
1320 h->ref_dynamic = 0;
1321 h->def_dynamic = 0;
1322 h->dynamic_def = 0;
1323 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1324 {
1325 h->root.type = bfd_link_hash_undefined;
1326 h->root.u.undef.abfd = abfd;
1327 }
1328 else
1329 {
1330 h->root.type = bfd_link_hash_new;
1331 h->root.u.undef.abfd = NULL;
1332 }
1333 return TRUE;
1334 }
580a2b6e
L
1335 }
1336
4c34aff8
AM
1337 /* Check TLS symbols. We don't check undefined symbols introduced
1338 by "ld -u" which have no type (and oldbfd NULL), and we don't
1339 check symbols from plugins because they also have no type. */
1340 if (oldbfd != NULL
1341 && (oldbfd->flags & BFD_PLUGIN) == 0
1342 && (abfd->flags & BFD_PLUGIN) == 0
1343 && ELF_ST_TYPE (sym->st_info) != h->type
1344 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1345 {
1346 bfd *ntbfd, *tbfd;
1347 bfd_boolean ntdef, tdef;
1348 asection *ntsec, *tsec;
1349
1350 if (h->type == STT_TLS)
1351 {
3b36f7e6 1352 ntbfd = abfd;
7479dfd4
L
1353 ntsec = sec;
1354 ntdef = newdef;
1355 tbfd = oldbfd;
1356 tsec = oldsec;
1357 tdef = olddef;
1358 }
1359 else
1360 {
1361 ntbfd = oldbfd;
1362 ntsec = oldsec;
1363 ntdef = olddef;
1364 tbfd = abfd;
1365 tsec = sec;
1366 tdef = newdef;
1367 }
1368
1369 if (tdef && ntdef)
4eca0228 1370 _bfd_error_handler
695344c0 1371 /* xgettext:c-format */
871b3ab2
AM
1372 (_("%s: TLS definition in %pB section %pA "
1373 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1374 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1375 else if (!tdef && !ntdef)
4eca0228 1376 _bfd_error_handler
695344c0 1377 /* xgettext:c-format */
871b3ab2
AM
1378 (_("%s: TLS reference in %pB "
1379 "mismatches non-TLS reference in %pB"),
c08bb8dd 1380 h->root.root.string, tbfd, ntbfd);
7479dfd4 1381 else if (tdef)
4eca0228 1382 _bfd_error_handler
695344c0 1383 /* xgettext:c-format */
871b3ab2
AM
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS reference in %pB"),
c08bb8dd 1386 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1387 else
4eca0228 1388 _bfd_error_handler
695344c0 1389 /* xgettext:c-format */
871b3ab2
AM
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1392 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1393
1394 bfd_set_error (bfd_error_bad_value);
1395 return FALSE;
1396 }
1397
45d6a902
AM
1398 /* If the old symbol has non-default visibility, we ignore the new
1399 definition from a dynamic object. */
1400 if (newdyn
9c7a29a3 1401 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1402 && !bfd_is_und_section (sec))
1403 {
1404 *skip = TRUE;
1405 /* Make sure this symbol is dynamic. */
f5385ebf 1406 h->ref_dynamic = 1;
90c984fc 1407 hi->ref_dynamic = 1;
45d6a902
AM
1408 /* A protected symbol has external availability. Make sure it is
1409 recorded as dynamic.
1410
1411 FIXME: Should we check type and size for protected symbol? */
1412 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1413 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1414 else
1415 return TRUE;
1416 }
1417 else if (!newdyn
9c7a29a3 1418 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1419 && h->def_dynamic)
45d6a902
AM
1420 {
1421 /* If the new symbol with non-default visibility comes from a
1422 relocatable file and the old definition comes from a dynamic
1423 object, we remove the old definition. */
6c9b78e6 1424 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1425 {
1426 /* Handle the case where the old dynamic definition is
1427 default versioned. We need to copy the symbol info from
1428 the symbol with default version to the normal one if it
1429 was referenced before. */
1430 if (h->ref_regular)
1431 {
6c9b78e6 1432 hi->root.type = h->root.type;
d2dee3b2 1433 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1434 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1435
6c9b78e6 1436 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1437 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1438 {
aed81c4e
MR
1439 /* If the new symbol is hidden or internal, completely undo
1440 any dynamic link state. */
1441 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1442 h->forced_local = 0;
1443 h->ref_dynamic = 0;
d2dee3b2
L
1444 }
1445 else
aed81c4e
MR
1446 h->ref_dynamic = 1;
1447
1448 h->def_dynamic = 0;
aed81c4e
MR
1449 /* FIXME: Should we check type and size for protected symbol? */
1450 h->size = 0;
1451 h->type = 0;
1452
6c9b78e6 1453 h = hi;
d2dee3b2
L
1454 }
1455 else
6c9b78e6 1456 h = hi;
d2dee3b2 1457 }
1de1a317 1458
f5eda473
AM
1459 /* If the old symbol was undefined before, then it will still be
1460 on the undefs list. If the new symbol is undefined or
1461 common, we can't make it bfd_link_hash_new here, because new
1462 undefined or common symbols will be added to the undefs list
1463 by _bfd_generic_link_add_one_symbol. Symbols may not be
1464 added twice to the undefs list. Also, if the new symbol is
1465 undefweak then we don't want to lose the strong undef. */
1466 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1467 {
1de1a317 1468 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1469 h->root.u.undef.abfd = abfd;
1470 }
1471 else
1472 {
1473 h->root.type = bfd_link_hash_new;
1474 h->root.u.undef.abfd = NULL;
1475 }
1476
f5eda473 1477 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1478 {
f5eda473
AM
1479 /* If the new symbol is hidden or internal, completely undo
1480 any dynamic link state. */
1481 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1482 h->forced_local = 0;
1483 h->ref_dynamic = 0;
45d6a902 1484 }
f5eda473
AM
1485 else
1486 h->ref_dynamic = 1;
1487 h->def_dynamic = 0;
45d6a902
AM
1488 /* FIXME: Should we check type and size for protected symbol? */
1489 h->size = 0;
1490 h->type = 0;
1491 return TRUE;
1492 }
14a793b2 1493
15b43f48
AM
1494 /* If a new weak symbol definition comes from a regular file and the
1495 old symbol comes from a dynamic library, we treat the new one as
1496 strong. Similarly, an old weak symbol definition from a regular
1497 file is treated as strong when the new symbol comes from a dynamic
1498 library. Further, an old weak symbol from a dynamic library is
1499 treated as strong if the new symbol is from a dynamic library.
1500 This reflects the way glibc's ld.so works.
1501
165f707a
AM
1502 Also allow a weak symbol to override a linker script symbol
1503 defined by an early pass over the script. This is done so the
1504 linker knows the symbol is defined in an object file, for the
1505 DEFINED script function.
1506
15b43f48
AM
1507 Do this before setting *type_change_ok or *size_change_ok so that
1508 we warn properly when dynamic library symbols are overridden. */
1509
165f707a 1510 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1511 newweak = FALSE;
15b43f48 1512 if (olddef && newdyn)
0f8a2703
AM
1513 oldweak = FALSE;
1514
d334575b 1515 /* Allow changes between different types of function symbol. */
0a36a439 1516 if (newfunc && oldfunc)
fcb93ecf
PB
1517 *type_change_ok = TRUE;
1518
79349b09
AM
1519 /* It's OK to change the type if either the existing symbol or the
1520 new symbol is weak. A type change is also OK if the old symbol
1521 is undefined and the new symbol is defined. */
252b5132 1522
79349b09
AM
1523 if (oldweak
1524 || newweak
1525 || (newdef
1526 && h->root.type == bfd_link_hash_undefined))
1527 *type_change_ok = TRUE;
1528
1529 /* It's OK to change the size if either the existing symbol or the
1530 new symbol is weak, or if the old symbol is undefined. */
1531
1532 if (*type_change_ok
1533 || h->root.type == bfd_link_hash_undefined)
1534 *size_change_ok = TRUE;
45d6a902 1535
45d6a902
AM
1536 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1537 symbol, respectively, appears to be a common symbol in a dynamic
1538 object. If a symbol appears in an uninitialized section, and is
1539 not weak, and is not a function, then it may be a common symbol
1540 which was resolved when the dynamic object was created. We want
1541 to treat such symbols specially, because they raise special
1542 considerations when setting the symbol size: if the symbol
1543 appears as a common symbol in a regular object, and the size in
1544 the regular object is larger, we must make sure that we use the
1545 larger size. This problematic case can always be avoided in C,
1546 but it must be handled correctly when using Fortran shared
1547 libraries.
1548
1549 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1550 likewise for OLDDYNCOMMON and OLDDEF.
1551
1552 Note that this test is just a heuristic, and that it is quite
1553 possible to have an uninitialized symbol in a shared object which
1554 is really a definition, rather than a common symbol. This could
1555 lead to some minor confusion when the symbol really is a common
1556 symbol in some regular object. However, I think it will be
1557 harmless. */
1558
1559 if (newdyn
1560 && newdef
79349b09 1561 && !newweak
45d6a902
AM
1562 && (sec->flags & SEC_ALLOC) != 0
1563 && (sec->flags & SEC_LOAD) == 0
1564 && sym->st_size > 0
0a36a439 1565 && !newfunc)
45d6a902
AM
1566 newdyncommon = TRUE;
1567 else
1568 newdyncommon = FALSE;
1569
1570 if (olddyn
1571 && olddef
1572 && h->root.type == bfd_link_hash_defined
f5385ebf 1573 && h->def_dynamic
45d6a902
AM
1574 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1575 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1576 && h->size > 0
0a36a439 1577 && !oldfunc)
45d6a902
AM
1578 olddyncommon = TRUE;
1579 else
1580 olddyncommon = FALSE;
1581
a4d8e49b
L
1582 /* We now know everything about the old and new symbols. We ask the
1583 backend to check if we can merge them. */
5d13b3b3
AM
1584 if (bed->merge_symbol != NULL)
1585 {
1586 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1587 return FALSE;
1588 sec = *psec;
1589 }
a4d8e49b 1590
a83ef4d1
L
1591 /* There are multiple definitions of a normal symbol. Skip the
1592 default symbol as well as definition from an IR object. */
93f4de39 1593 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1594 && !default_sym && h->def_regular
1595 && !(oldbfd != NULL
1596 && (oldbfd->flags & BFD_PLUGIN) != 0
1597 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1598 {
1599 /* Handle a multiple definition. */
1600 (*info->callbacks->multiple_definition) (info, &h->root,
1601 abfd, sec, *pvalue);
1602 *skip = TRUE;
1603 return TRUE;
1604 }
1605
45d6a902
AM
1606 /* If both the old and the new symbols look like common symbols in a
1607 dynamic object, set the size of the symbol to the larger of the
1608 two. */
1609
1610 if (olddyncommon
1611 && newdyncommon
1612 && sym->st_size != h->size)
1613 {
1614 /* Since we think we have two common symbols, issue a multiple
1615 common warning if desired. Note that we only warn if the
1616 size is different. If the size is the same, we simply let
1617 the old symbol override the new one as normally happens with
1618 symbols defined in dynamic objects. */
1619
1a72702b
AM
1620 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1621 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1622 if (sym->st_size > h->size)
1623 h->size = sym->st_size;
252b5132 1624
45d6a902 1625 *size_change_ok = TRUE;
252b5132
RH
1626 }
1627
45d6a902
AM
1628 /* If we are looking at a dynamic object, and we have found a
1629 definition, we need to see if the symbol was already defined by
1630 some other object. If so, we want to use the existing
1631 definition, and we do not want to report a multiple symbol
1632 definition error; we do this by clobbering *PSEC to be
1633 bfd_und_section_ptr.
1634
1635 We treat a common symbol as a definition if the symbol in the
1636 shared library is a function, since common symbols always
1637 represent variables; this can cause confusion in principle, but
1638 any such confusion would seem to indicate an erroneous program or
1639 shared library. We also permit a common symbol in a regular
8170f769 1640 object to override a weak symbol in a shared object. */
45d6a902
AM
1641
1642 if (newdyn
1643 && newdef
77cfaee6 1644 && (olddef
45d6a902 1645 || (h->root.type == bfd_link_hash_common
8170f769 1646 && (newweak || newfunc))))
45d6a902
AM
1647 {
1648 *override = TRUE;
1649 newdef = FALSE;
1650 newdyncommon = FALSE;
252b5132 1651
45d6a902
AM
1652 *psec = sec = bfd_und_section_ptr;
1653 *size_change_ok = TRUE;
252b5132 1654
45d6a902
AM
1655 /* If we get here when the old symbol is a common symbol, then
1656 we are explicitly letting it override a weak symbol or
1657 function in a dynamic object, and we don't want to warn about
1658 a type change. If the old symbol is a defined symbol, a type
1659 change warning may still be appropriate. */
252b5132 1660
45d6a902
AM
1661 if (h->root.type == bfd_link_hash_common)
1662 *type_change_ok = TRUE;
1663 }
1664
1665 /* Handle the special case of an old common symbol merging with a
1666 new symbol which looks like a common symbol in a shared object.
1667 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1668 common symbol, and let _bfd_generic_link_add_one_symbol do the
1669 right thing. */
45d6a902
AM
1670
1671 if (newdyncommon
1672 && h->root.type == bfd_link_hash_common)
1673 {
1674 *override = TRUE;
1675 newdef = FALSE;
1676 newdyncommon = FALSE;
1677 *pvalue = sym->st_size;
a4d8e49b 1678 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1679 *size_change_ok = TRUE;
1680 }
1681
c5e2cead 1682 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1683 if (newdef && olddef && newweak)
54ac0771 1684 {
35ed3f94 1685 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1686 if (!(oldbfd != NULL
1687 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1688 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1689 {
1690 newdef = FALSE;
1691 *skip = TRUE;
1692 }
54ac0771
L
1693
1694 /* Merge st_other. If the symbol already has a dynamic index,
1695 but visibility says it should not be visible, turn it into a
1696 local symbol. */
b8417128 1697 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1698 if (h->dynindx != -1)
1699 switch (ELF_ST_VISIBILITY (h->other))
1700 {
1701 case STV_INTERNAL:
1702 case STV_HIDDEN:
1703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1704 break;
1705 }
1706 }
c5e2cead 1707
45d6a902
AM
1708 /* If the old symbol is from a dynamic object, and the new symbol is
1709 a definition which is not from a dynamic object, then the new
1710 symbol overrides the old symbol. Symbols from regular files
1711 always take precedence over symbols from dynamic objects, even if
1712 they are defined after the dynamic object in the link.
1713
1714 As above, we again permit a common symbol in a regular object to
1715 override a definition in a shared object if the shared object
0f8a2703 1716 symbol is a function or is weak. */
45d6a902
AM
1717
1718 flip = NULL;
77cfaee6 1719 if (!newdyn
45d6a902
AM
1720 && (newdef
1721 || (bfd_is_com_section (sec)
0a36a439 1722 && (oldweak || oldfunc)))
45d6a902
AM
1723 && olddyn
1724 && olddef
f5385ebf 1725 && h->def_dynamic)
45d6a902
AM
1726 {
1727 /* Change the hash table entry to undefined, and let
1728 _bfd_generic_link_add_one_symbol do the right thing with the
1729 new definition. */
1730
1731 h->root.type = bfd_link_hash_undefined;
1732 h->root.u.undef.abfd = h->root.u.def.section->owner;
1733 *size_change_ok = TRUE;
1734
1735 olddef = FALSE;
1736 olddyncommon = FALSE;
1737
1738 /* We again permit a type change when a common symbol may be
1739 overriding a function. */
1740
1741 if (bfd_is_com_section (sec))
0a36a439
L
1742 {
1743 if (oldfunc)
1744 {
1745 /* If a common symbol overrides a function, make sure
1746 that it isn't defined dynamically nor has type
1747 function. */
1748 h->def_dynamic = 0;
1749 h->type = STT_NOTYPE;
1750 }
1751 *type_change_ok = TRUE;
1752 }
45d6a902 1753
6c9b78e6
AM
1754 if (hi->root.type == bfd_link_hash_indirect)
1755 flip = hi;
45d6a902
AM
1756 else
1757 /* This union may have been set to be non-NULL when this symbol
1758 was seen in a dynamic object. We must force the union to be
1759 NULL, so that it is correct for a regular symbol. */
1760 h->verinfo.vertree = NULL;
1761 }
1762
1763 /* Handle the special case of a new common symbol merging with an
1764 old symbol that looks like it might be a common symbol defined in
1765 a shared object. Note that we have already handled the case in
1766 which a new common symbol should simply override the definition
1767 in the shared library. */
1768
1769 if (! newdyn
1770 && bfd_is_com_section (sec)
1771 && olddyncommon)
1772 {
1773 /* It would be best if we could set the hash table entry to a
1774 common symbol, but we don't know what to use for the section
1775 or the alignment. */
1a72702b
AM
1776 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1777 bfd_link_hash_common, sym->st_size);
45d6a902 1778
4cc11e76 1779 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1780 larger, pretend that the new symbol has its size. */
1781
1782 if (h->size > *pvalue)
1783 *pvalue = h->size;
1784
af44c138
L
1785 /* We need to remember the alignment required by the symbol
1786 in the dynamic object. */
1787 BFD_ASSERT (pold_alignment);
1788 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1789
1790 olddef = FALSE;
1791 olddyncommon = FALSE;
1792
1793 h->root.type = bfd_link_hash_undefined;
1794 h->root.u.undef.abfd = h->root.u.def.section->owner;
1795
1796 *size_change_ok = TRUE;
1797 *type_change_ok = TRUE;
1798
6c9b78e6
AM
1799 if (hi->root.type == bfd_link_hash_indirect)
1800 flip = hi;
45d6a902
AM
1801 else
1802 h->verinfo.vertree = NULL;
1803 }
1804
1805 if (flip != NULL)
1806 {
1807 /* Handle the case where we had a versioned symbol in a dynamic
1808 library and now find a definition in a normal object. In this
1809 case, we make the versioned symbol point to the normal one. */
45d6a902 1810 flip->root.type = h->root.type;
00cbee0a 1811 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1812 h->root.type = bfd_link_hash_indirect;
1813 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1814 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1815 if (h->def_dynamic)
45d6a902 1816 {
f5385ebf
AM
1817 h->def_dynamic = 0;
1818 flip->ref_dynamic = 1;
45d6a902
AM
1819 }
1820 }
1821
45d6a902
AM
1822 return TRUE;
1823}
1824
1825/* This function is called to create an indirect symbol from the
1826 default for the symbol with the default version if needed. The
4f3fedcf 1827 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1828 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1829
28caa186 1830static bfd_boolean
268b6b39
AM
1831_bfd_elf_add_default_symbol (bfd *abfd,
1832 struct bfd_link_info *info,
1833 struct elf_link_hash_entry *h,
1834 const char *name,
1835 Elf_Internal_Sym *sym,
4f3fedcf
AM
1836 asection *sec,
1837 bfd_vma value,
1838 bfd **poldbfd,
e3c9d234 1839 bfd_boolean *dynsym)
45d6a902
AM
1840{
1841 bfd_boolean type_change_ok;
1842 bfd_boolean size_change_ok;
1843 bfd_boolean skip;
1844 char *shortname;
1845 struct elf_link_hash_entry *hi;
1846 struct bfd_link_hash_entry *bh;
9c5bfbb7 1847 const struct elf_backend_data *bed;
45d6a902
AM
1848 bfd_boolean collect;
1849 bfd_boolean dynamic;
e3c9d234 1850 bfd_boolean override;
45d6a902
AM
1851 char *p;
1852 size_t len, shortlen;
ffd65175 1853 asection *tmp_sec;
6e33951e 1854 bfd_boolean matched;
45d6a902 1855
422f1182
L
1856 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1857 return TRUE;
1858
45d6a902
AM
1859 /* If this symbol has a version, and it is the default version, we
1860 create an indirect symbol from the default name to the fully
1861 decorated name. This will cause external references which do not
1862 specify a version to be bound to this version of the symbol. */
1863 p = strchr (name, ELF_VER_CHR);
422f1182
L
1864 if (h->versioned == unknown)
1865 {
1866 if (p == NULL)
1867 {
1868 h->versioned = unversioned;
1869 return TRUE;
1870 }
1871 else
1872 {
1873 if (p[1] != ELF_VER_CHR)
1874 {
1875 h->versioned = versioned_hidden;
1876 return TRUE;
1877 }
1878 else
1879 h->versioned = versioned;
1880 }
1881 }
4373f8af
L
1882 else
1883 {
1884 /* PR ld/19073: We may see an unversioned definition after the
1885 default version. */
1886 if (p == NULL)
1887 return TRUE;
1888 }
45d6a902 1889
45d6a902
AM
1890 bed = get_elf_backend_data (abfd);
1891 collect = bed->collect;
1892 dynamic = (abfd->flags & DYNAMIC) != 0;
1893
1894 shortlen = p - name;
a50b1753 1895 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1896 if (shortname == NULL)
1897 return FALSE;
1898 memcpy (shortname, name, shortlen);
1899 shortname[shortlen] = '\0';
1900
1901 /* We are going to create a new symbol. Merge it with any existing
1902 symbol with this name. For the purposes of the merge, act as
1903 though we were defining the symbol we just defined, although we
1904 actually going to define an indirect symbol. */
1905 type_change_ok = FALSE;
1906 size_change_ok = FALSE;
6e33951e 1907 matched = TRUE;
ffd65175
AM
1908 tmp_sec = sec;
1909 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1910 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1911 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1912 return FALSE;
1913
1914 if (skip)
1915 goto nondefault;
1916
5fa370e4 1917 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1918 {
1919 /* If the undecorated symbol will have a version added by a
1920 script different to H, then don't indirect to/from the
1921 undecorated symbol. This isn't ideal because we may not yet
1922 have seen symbol versions, if given by a script on the
1923 command line rather than via --version-script. */
1924 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1925 {
1926 bfd_boolean hide;
1927
1928 hi->verinfo.vertree
1929 = bfd_find_version_for_sym (info->version_info,
1930 hi->root.root.string, &hide);
1931 if (hi->verinfo.vertree != NULL && hide)
1932 {
1933 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1934 goto nondefault;
1935 }
1936 }
1937 if (hi->verinfo.vertree != NULL
1938 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1939 goto nondefault;
1940 }
1941
45d6a902
AM
1942 if (! override)
1943 {
c6e8a9a8 1944 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1945 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1946 {
1947 bh = &hi->root;
fbcc8baf 1948 if (bh->type == bfd_link_hash_defined
6cc71b82 1949 && bh->u.def.section->owner != NULL
fbcc8baf
L
1950 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1951 {
1952 /* Mark the previous definition from IR object as
1953 undefined so that the generic linker will override
1954 it. */
1955 bh->type = bfd_link_hash_undefined;
1956 bh->u.undef.abfd = bh->u.def.section->owner;
1957 }
c6e8a9a8
L
1958 if (! (_bfd_generic_link_add_one_symbol
1959 (info, abfd, shortname, BSF_INDIRECT,
1960 bfd_ind_section_ptr,
1961 0, name, FALSE, collect, &bh)))
1962 return FALSE;
1963 hi = (struct elf_link_hash_entry *) bh;
1964 }
45d6a902
AM
1965 }
1966 else
1967 {
1968 /* In this case the symbol named SHORTNAME is overriding the
1969 indirect symbol we want to add. We were planning on making
1970 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1971 is the name without a version. NAME is the fully versioned
1972 name, and it is the default version.
1973
1974 Overriding means that we already saw a definition for the
1975 symbol SHORTNAME in a regular object, and it is overriding
1976 the symbol defined in the dynamic object.
1977
1978 When this happens, we actually want to change NAME, the
1979 symbol we just added, to refer to SHORTNAME. This will cause
1980 references to NAME in the shared object to become references
1981 to SHORTNAME in the regular object. This is what we expect
1982 when we override a function in a shared object: that the
1983 references in the shared object will be mapped to the
1984 definition in the regular object. */
1985
1986 while (hi->root.type == bfd_link_hash_indirect
1987 || hi->root.type == bfd_link_hash_warning)
1988 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1989
1990 h->root.type = bfd_link_hash_indirect;
1991 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1992 if (h->def_dynamic)
45d6a902 1993 {
f5385ebf
AM
1994 h->def_dynamic = 0;
1995 hi->ref_dynamic = 1;
1996 if (hi->ref_regular
1997 || hi->def_regular)
45d6a902 1998 {
c152c796 1999 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2000 return FALSE;
2001 }
2002 }
2003
2004 /* Now set HI to H, so that the following code will set the
2005 other fields correctly. */
2006 hi = h;
2007 }
2008
fab4a87f
L
2009 /* Check if HI is a warning symbol. */
2010 if (hi->root.type == bfd_link_hash_warning)
2011 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012
45d6a902
AM
2013 /* If there is a duplicate definition somewhere, then HI may not
2014 point to an indirect symbol. We will have reported an error to
2015 the user in that case. */
2016
2017 if (hi->root.type == bfd_link_hash_indirect)
2018 {
2019 struct elf_link_hash_entry *ht;
2020
45d6a902 2021 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2022 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2023
68c88cd4
AM
2024 /* A reference to the SHORTNAME symbol from a dynamic library
2025 will be satisfied by the versioned symbol at runtime. In
2026 effect, we have a reference to the versioned symbol. */
2027 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2028 hi->dynamic_def |= ht->dynamic_def;
2029
45d6a902
AM
2030 /* See if the new flags lead us to realize that the symbol must
2031 be dynamic. */
2032 if (! *dynsym)
2033 {
2034 if (! dynamic)
2035 {
0e1862bb 2036 if (! bfd_link_executable (info)
90c984fc 2037 || hi->def_dynamic
f5385ebf 2038 || hi->ref_dynamic)
45d6a902
AM
2039 *dynsym = TRUE;
2040 }
2041 else
2042 {
f5385ebf 2043 if (hi->ref_regular)
45d6a902
AM
2044 *dynsym = TRUE;
2045 }
2046 }
2047 }
2048
2049 /* We also need to define an indirection from the nondefault version
2050 of the symbol. */
2051
dc1e8a47 2052 nondefault:
45d6a902 2053 len = strlen (name);
a50b1753 2054 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2055 if (shortname == NULL)
2056 return FALSE;
2057 memcpy (shortname, name, shortlen);
2058 memcpy (shortname + shortlen, p + 1, len - shortlen);
2059
2060 /* Once again, merge with any existing symbol. */
2061 type_change_ok = FALSE;
2062 size_change_ok = FALSE;
ffd65175
AM
2063 tmp_sec = sec;
2064 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2065 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2066 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2067 return FALSE;
2068
2069 if (skip)
2070 return TRUE;
2071
2072 if (override)
2073 {
2074 /* Here SHORTNAME is a versioned name, so we don't expect to see
2075 the type of override we do in the case above unless it is
4cc11e76 2076 overridden by a versioned definition. */
45d6a902
AM
2077 if (hi->root.type != bfd_link_hash_defined
2078 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2079 _bfd_error_handler
695344c0 2080 /* xgettext:c-format */
871b3ab2 2081 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2082 abfd, shortname);
45d6a902
AM
2083 }
2084 else
2085 {
2086 bh = &hi->root;
2087 if (! (_bfd_generic_link_add_one_symbol
2088 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2089 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2090 return FALSE;
2091 hi = (struct elf_link_hash_entry *) bh;
2092
2093 /* If there is a duplicate definition somewhere, then HI may not
2094 point to an indirect symbol. We will have reported an error
2095 to the user in that case. */
2096
2097 if (hi->root.type == bfd_link_hash_indirect)
2098 {
fcfa13d2 2099 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2100 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2101 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2102
2103 /* See if the new flags lead us to realize that the symbol
2104 must be dynamic. */
2105 if (! *dynsym)
2106 {
2107 if (! dynamic)
2108 {
0e1862bb 2109 if (! bfd_link_executable (info)
f5385ebf 2110 || hi->ref_dynamic)
45d6a902
AM
2111 *dynsym = TRUE;
2112 }
2113 else
2114 {
f5385ebf 2115 if (hi->ref_regular)
45d6a902
AM
2116 *dynsym = TRUE;
2117 }
2118 }
2119 }
2120 }
2121
2122 return TRUE;
2123}
2124\f
2125/* This routine is used to export all defined symbols into the dynamic
2126 symbol table. It is called via elf_link_hash_traverse. */
2127
28caa186 2128static bfd_boolean
268b6b39 2129_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2130{
a50b1753 2131 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2132
2133 /* Ignore indirect symbols. These are added by the versioning code. */
2134 if (h->root.type == bfd_link_hash_indirect)
2135 return TRUE;
2136
7686d77d
AM
2137 /* Ignore this if we won't export it. */
2138 if (!eif->info->export_dynamic && !h->dynamic)
2139 return TRUE;
45d6a902
AM
2140
2141 if (h->dynindx == -1
fd91d419
L
2142 && (h->def_regular || h->ref_regular)
2143 && ! bfd_hide_sym_by_version (eif->info->version_info,
2144 h->root.root.string))
45d6a902 2145 {
fd91d419 2146 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2147 {
fd91d419
L
2148 eif->failed = TRUE;
2149 return FALSE;
45d6a902
AM
2150 }
2151 }
2152
2153 return TRUE;
2154}
2155\f
2156/* Look through the symbols which are defined in other shared
2157 libraries and referenced here. Update the list of version
2158 dependencies. This will be put into the .gnu.version_r section.
2159 This function is called via elf_link_hash_traverse. */
2160
28caa186 2161static bfd_boolean
268b6b39
AM
2162_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2163 void *data)
45d6a902 2164{
a50b1753 2165 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2166 Elf_Internal_Verneed *t;
2167 Elf_Internal_Vernaux *a;
986f0783 2168 size_t amt;
45d6a902 2169
45d6a902
AM
2170 /* We only care about symbols defined in shared objects with version
2171 information. */
f5385ebf
AM
2172 if (!h->def_dynamic
2173 || h->def_regular
45d6a902 2174 || h->dynindx == -1
7b20f099
AM
2175 || h->verinfo.verdef == NULL
2176 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2177 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2178 return TRUE;
2179
2180 /* See if we already know about this version. */
28caa186
AM
2181 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2182 t != NULL;
2183 t = t->vn_nextref)
45d6a902
AM
2184 {
2185 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2186 continue;
2187
2188 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2189 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2190 return TRUE;
2191
2192 break;
2193 }
2194
2195 /* This is a new version. Add it to tree we are building. */
2196
2197 if (t == NULL)
2198 {
2199 amt = sizeof *t;
a50b1753 2200 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2201 if (t == NULL)
2202 {
2203 rinfo->failed = TRUE;
2204 return FALSE;
2205 }
2206
2207 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2208 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2209 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2210 }
2211
2212 amt = sizeof *a;
a50b1753 2213 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2214 if (a == NULL)
2215 {
2216 rinfo->failed = TRUE;
2217 return FALSE;
2218 }
45d6a902
AM
2219
2220 /* Note that we are copying a string pointer here, and testing it
2221 above. If bfd_elf_string_from_elf_section is ever changed to
2222 discard the string data when low in memory, this will have to be
2223 fixed. */
2224 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2225
2226 a->vna_flags = h->verinfo.verdef->vd_flags;
2227 a->vna_nextptr = t->vn_auxptr;
2228
2229 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2230 ++rinfo->vers;
2231
2232 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2233
2234 t->vn_auxptr = a;
2235
2236 return TRUE;
2237}
2238
099bb8fb
L
2239/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2240 hidden. Set *T_P to NULL if there is no match. */
2241
2242static bfd_boolean
2243_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2244 struct elf_link_hash_entry *h,
2245 const char *version_p,
2246 struct bfd_elf_version_tree **t_p,
2247 bfd_boolean *hide)
2248{
2249 struct bfd_elf_version_tree *t;
2250
2251 /* Look for the version. If we find it, it is no longer weak. */
2252 for (t = info->version_info; t != NULL; t = t->next)
2253 {
2254 if (strcmp (t->name, version_p) == 0)
2255 {
2256 size_t len;
2257 char *alc;
2258 struct bfd_elf_version_expr *d;
2259
2260 len = version_p - h->root.root.string;
2261 alc = (char *) bfd_malloc (len);
2262 if (alc == NULL)
2263 return FALSE;
2264 memcpy (alc, h->root.root.string, len - 1);
2265 alc[len - 1] = '\0';
2266 if (alc[len - 2] == ELF_VER_CHR)
2267 alc[len - 2] = '\0';
2268
2269 h->verinfo.vertree = t;
2270 t->used = TRUE;
2271 d = NULL;
2272
2273 if (t->globals.list != NULL)
2274 d = (*t->match) (&t->globals, NULL, alc);
2275
2276 /* See if there is anything to force this symbol to
2277 local scope. */
2278 if (d == NULL && t->locals.list != NULL)
2279 {
2280 d = (*t->match) (&t->locals, NULL, alc);
2281 if (d != NULL
2282 && h->dynindx != -1
2283 && ! info->export_dynamic)
2284 *hide = TRUE;
2285 }
2286
2287 free (alc);
2288 break;
2289 }
2290 }
2291
2292 *t_p = t;
2293
2294 return TRUE;
2295}
2296
2297/* Return TRUE if the symbol H is hidden by version script. */
2298
2299bfd_boolean
2300_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2301 struct elf_link_hash_entry *h)
2302{
2303 const char *p;
2304 bfd_boolean hide = FALSE;
2305 const struct elf_backend_data *bed
2306 = get_elf_backend_data (info->output_bfd);
2307
2308 /* Version script only hides symbols defined in regular objects. */
2309 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2310 return TRUE;
2311
2312 p = strchr (h->root.root.string, ELF_VER_CHR);
2313 if (p != NULL && h->verinfo.vertree == NULL)
2314 {
2315 struct bfd_elf_version_tree *t;
2316
2317 ++p;
2318 if (*p == ELF_VER_CHR)
2319 ++p;
2320
2321 if (*p != '\0'
2322 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2323 && hide)
2324 {
2325 if (hide)
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2327 return TRUE;
2328 }
2329 }
2330
2331 /* If we don't have a version for this symbol, see if we can find
2332 something. */
2333 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2334 {
2335 h->verinfo.vertree
2336 = bfd_find_version_for_sym (info->version_info,
2337 h->root.root.string, &hide);
2338 if (h->verinfo.vertree != NULL && hide)
2339 {
2340 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2341 return TRUE;
2342 }
2343 }
2344
2345 return FALSE;
2346}
2347
45d6a902
AM
2348/* Figure out appropriate versions for all the symbols. We may not
2349 have the version number script until we have read all of the input
2350 files, so until that point we don't know which symbols should be
2351 local. This function is called via elf_link_hash_traverse. */
2352
28caa186 2353static bfd_boolean
268b6b39 2354_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2355{
28caa186 2356 struct elf_info_failed *sinfo;
45d6a902 2357 struct bfd_link_info *info;
9c5bfbb7 2358 const struct elf_backend_data *bed;
45d6a902
AM
2359 struct elf_info_failed eif;
2360 char *p;
099bb8fb 2361 bfd_boolean hide;
45d6a902 2362
a50b1753 2363 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2364 info = sinfo->info;
2365
45d6a902
AM
2366 /* Fix the symbol flags. */
2367 eif.failed = FALSE;
2368 eif.info = info;
2369 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2370 {
2371 if (eif.failed)
2372 sinfo->failed = TRUE;
2373 return FALSE;
2374 }
2375
0a640d71
L
2376 bed = get_elf_backend_data (info->output_bfd);
2377
45d6a902
AM
2378 /* We only need version numbers for symbols defined in regular
2379 objects. */
5fa370e4 2380 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2381 {
2382 /* Hide symbols defined in discarded input sections. */
2383 if ((h->root.type == bfd_link_hash_defined
2384 || h->root.type == bfd_link_hash_defweak)
2385 && discarded_section (h->root.u.def.section))
2386 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2387 return TRUE;
2388 }
45d6a902 2389
099bb8fb 2390 hide = FALSE;
45d6a902
AM
2391 p = strchr (h->root.root.string, ELF_VER_CHR);
2392 if (p != NULL && h->verinfo.vertree == NULL)
2393 {
2394 struct bfd_elf_version_tree *t;
45d6a902 2395
45d6a902
AM
2396 ++p;
2397 if (*p == ELF_VER_CHR)
6e33951e 2398 ++p;
45d6a902
AM
2399
2400 /* If there is no version string, we can just return out. */
2401 if (*p == '\0')
6e33951e 2402 return TRUE;
45d6a902 2403
099bb8fb 2404 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2405 {
099bb8fb
L
2406 sinfo->failed = TRUE;
2407 return FALSE;
45d6a902
AM
2408 }
2409
099bb8fb
L
2410 if (hide)
2411 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2412
45d6a902
AM
2413 /* If we are building an application, we need to create a
2414 version node for this version. */
0e1862bb 2415 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2416 {
2417 struct bfd_elf_version_tree **pp;
2418 int version_index;
2419
2420 /* If we aren't going to export this symbol, we don't need
2421 to worry about it. */
2422 if (h->dynindx == -1)
2423 return TRUE;
2424
ef53be89
AM
2425 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2426 sizeof *t);
45d6a902
AM
2427 if (t == NULL)
2428 {
2429 sinfo->failed = TRUE;
2430 return FALSE;
2431 }
2432
45d6a902 2433 t->name = p;
45d6a902
AM
2434 t->name_indx = (unsigned int) -1;
2435 t->used = TRUE;
2436
2437 version_index = 1;
2438 /* Don't count anonymous version tag. */
fd91d419
L
2439 if (sinfo->info->version_info != NULL
2440 && sinfo->info->version_info->vernum == 0)
45d6a902 2441 version_index = 0;
fd91d419
L
2442 for (pp = &sinfo->info->version_info;
2443 *pp != NULL;
2444 pp = &(*pp)->next)
45d6a902
AM
2445 ++version_index;
2446 t->vernum = version_index;
2447
2448 *pp = t;
2449
2450 h->verinfo.vertree = t;
2451 }
2452 else if (t == NULL)
2453 {
2454 /* We could not find the version for a symbol when
2455 generating a shared archive. Return an error. */
4eca0228 2456 _bfd_error_handler
695344c0 2457 /* xgettext:c-format */
871b3ab2 2458 (_("%pB: version node not found for symbol %s"),
28caa186 2459 info->output_bfd, h->root.root.string);
45d6a902
AM
2460 bfd_set_error (bfd_error_bad_value);
2461 sinfo->failed = TRUE;
2462 return FALSE;
2463 }
45d6a902
AM
2464 }
2465
2466 /* If we don't have a version for this symbol, see if we can find
2467 something. */
099bb8fb
L
2468 if (!hide
2469 && h->verinfo.vertree == NULL
2470 && sinfo->info->version_info != NULL)
45d6a902 2471 {
fd91d419
L
2472 h->verinfo.vertree
2473 = bfd_find_version_for_sym (sinfo->info->version_info,
2474 h->root.root.string, &hide);
1e8fa21e
AM
2475 if (h->verinfo.vertree != NULL && hide)
2476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2477 }
2478
2479 return TRUE;
2480}
2481\f
45d6a902
AM
2482/* Read and swap the relocs from the section indicated by SHDR. This
2483 may be either a REL or a RELA section. The relocations are
2484 translated into RELA relocations and stored in INTERNAL_RELOCS,
2485 which should have already been allocated to contain enough space.
2486 The EXTERNAL_RELOCS are a buffer where the external form of the
2487 relocations should be stored.
2488
2489 Returns FALSE if something goes wrong. */
2490
2491static bfd_boolean
268b6b39 2492elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2493 asection *sec,
268b6b39
AM
2494 Elf_Internal_Shdr *shdr,
2495 void *external_relocs,
2496 Elf_Internal_Rela *internal_relocs)
45d6a902 2497{
9c5bfbb7 2498 const struct elf_backend_data *bed;
268b6b39 2499 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2500 const bfd_byte *erela;
2501 const bfd_byte *erelaend;
2502 Elf_Internal_Rela *irela;
243ef1e0
L
2503 Elf_Internal_Shdr *symtab_hdr;
2504 size_t nsyms;
45d6a902 2505
45d6a902
AM
2506 /* Position ourselves at the start of the section. */
2507 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2508 return FALSE;
2509
2510 /* Read the relocations. */
2511 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2512 return FALSE;
2513
243ef1e0 2514 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2515 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2516
45d6a902
AM
2517 bed = get_elf_backend_data (abfd);
2518
2519 /* Convert the external relocations to the internal format. */
2520 if (shdr->sh_entsize == bed->s->sizeof_rel)
2521 swap_in = bed->s->swap_reloc_in;
2522 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2523 swap_in = bed->s->swap_reloca_in;
2524 else
2525 {
2526 bfd_set_error (bfd_error_wrong_format);
2527 return FALSE;
2528 }
2529
a50b1753 2530 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2531 /* Setting erelaend like this and comparing with <= handles case of
2532 a fuzzed object with sh_size not a multiple of sh_entsize. */
2533 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2534 irela = internal_relocs;
f55b1e32 2535 while (erela <= erelaend)
45d6a902 2536 {
243ef1e0
L
2537 bfd_vma r_symndx;
2538
45d6a902 2539 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2540 r_symndx = ELF32_R_SYM (irela->r_info);
2541 if (bed->s->arch_size == 64)
2542 r_symndx >>= 24;
ce98a316
NC
2543 if (nsyms > 0)
2544 {
2545 if ((size_t) r_symndx >= nsyms)
2546 {
4eca0228 2547 _bfd_error_handler
695344c0 2548 /* xgettext:c-format */
2dcf00ce
AM
2549 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2550 " for offset %#" PRIx64 " in section `%pA'"),
2551 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2552 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2553 bfd_set_error (bfd_error_bad_value);
2554 return FALSE;
2555 }
2556 }
cf35638d 2557 else if (r_symndx != STN_UNDEF)
243ef1e0 2558 {
4eca0228 2559 _bfd_error_handler
695344c0 2560 /* xgettext:c-format */
2dcf00ce
AM
2561 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2562 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2563 " when the object file has no symbol table"),
2dcf00ce
AM
2564 abfd, (uint64_t) r_symndx,
2565 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2566 bfd_set_error (bfd_error_bad_value);
2567 return FALSE;
2568 }
45d6a902
AM
2569 irela += bed->s->int_rels_per_ext_rel;
2570 erela += shdr->sh_entsize;
2571 }
2572
2573 return TRUE;
2574}
2575
2576/* Read and swap the relocs for a section O. They may have been
2577 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2578 not NULL, they are used as buffers to read into. They are known to
2579 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2580 the return value is allocated using either malloc or bfd_alloc,
2581 according to the KEEP_MEMORY argument. If O has two relocation
2582 sections (both REL and RELA relocations), then the REL_HDR
2583 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2584 RELA_HDR relocations. */
45d6a902
AM
2585
2586Elf_Internal_Rela *
268b6b39
AM
2587_bfd_elf_link_read_relocs (bfd *abfd,
2588 asection *o,
2589 void *external_relocs,
2590 Elf_Internal_Rela *internal_relocs,
2591 bfd_boolean keep_memory)
45d6a902 2592{
268b6b39 2593 void *alloc1 = NULL;
45d6a902 2594 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2596 struct bfd_elf_section_data *esdo = elf_section_data (o);
2597 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2598
d4730f92
BS
2599 if (esdo->relocs != NULL)
2600 return esdo->relocs;
45d6a902
AM
2601
2602 if (o->reloc_count == 0)
2603 return NULL;
2604
45d6a902
AM
2605 if (internal_relocs == NULL)
2606 {
2607 bfd_size_type size;
2608
056bafd4 2609 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2610 if (keep_memory)
a50b1753 2611 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2612 else
a50b1753 2613 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2614 if (internal_relocs == NULL)
2615 goto error_return;
2616 }
2617
2618 if (external_relocs == NULL)
2619 {
d4730f92
BS
2620 bfd_size_type size = 0;
2621
2622 if (esdo->rel.hdr)
2623 size += esdo->rel.hdr->sh_size;
2624 if (esdo->rela.hdr)
2625 size += esdo->rela.hdr->sh_size;
45d6a902 2626
268b6b39 2627 alloc1 = bfd_malloc (size);
45d6a902
AM
2628 if (alloc1 == NULL)
2629 goto error_return;
2630 external_relocs = alloc1;
2631 }
2632
d4730f92
BS
2633 internal_rela_relocs = internal_relocs;
2634 if (esdo->rel.hdr)
2635 {
2636 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2637 external_relocs,
2638 internal_relocs))
2639 goto error_return;
2640 external_relocs = (((bfd_byte *) external_relocs)
2641 + esdo->rel.hdr->sh_size);
2642 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2643 * bed->s->int_rels_per_ext_rel);
2644 }
2645
2646 if (esdo->rela.hdr
2647 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2648 external_relocs,
2649 internal_rela_relocs)))
45d6a902
AM
2650 goto error_return;
2651
2652 /* Cache the results for next time, if we can. */
2653 if (keep_memory)
d4730f92 2654 esdo->relocs = internal_relocs;
45d6a902 2655
c9594989 2656 free (alloc1);
45d6a902
AM
2657
2658 /* Don't free alloc2, since if it was allocated we are passing it
2659 back (under the name of internal_relocs). */
2660
2661 return internal_relocs;
2662
2663 error_return:
c9594989 2664 free (alloc1);
45d6a902 2665 if (alloc2 != NULL)
4dd07732
AM
2666 {
2667 if (keep_memory)
2668 bfd_release (abfd, alloc2);
2669 else
2670 free (alloc2);
2671 }
45d6a902
AM
2672 return NULL;
2673}
2674
2675/* Compute the size of, and allocate space for, REL_HDR which is the
2676 section header for a section containing relocations for O. */
2677
28caa186 2678static bfd_boolean
9eaff861
AO
2679_bfd_elf_link_size_reloc_section (bfd *abfd,
2680 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2681{
9eaff861 2682 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2683
2684 /* That allows us to calculate the size of the section. */
9eaff861 2685 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2686
2687 /* The contents field must last into write_object_contents, so we
2688 allocate it with bfd_alloc rather than malloc. Also since we
2689 cannot be sure that the contents will actually be filled in,
2690 we zero the allocated space. */
a50b1753 2691 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2692 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2693 return FALSE;
2694
d4730f92 2695 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2696 {
2697 struct elf_link_hash_entry **p;
2698
ca4be51c
AM
2699 p = ((struct elf_link_hash_entry **)
2700 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2701 if (p == NULL)
2702 return FALSE;
2703
d4730f92 2704 reldata->hashes = p;
45d6a902
AM
2705 }
2706
2707 return TRUE;
2708}
2709
2710/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2711 originated from the section given by INPUT_REL_HDR) to the
2712 OUTPUT_BFD. */
2713
2714bfd_boolean
268b6b39
AM
2715_bfd_elf_link_output_relocs (bfd *output_bfd,
2716 asection *input_section,
2717 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2718 Elf_Internal_Rela *internal_relocs,
2719 struct elf_link_hash_entry **rel_hash
2720 ATTRIBUTE_UNUSED)
45d6a902
AM
2721{
2722 Elf_Internal_Rela *irela;
2723 Elf_Internal_Rela *irelaend;
2724 bfd_byte *erel;
d4730f92 2725 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2726 asection *output_section;
9c5bfbb7 2727 const struct elf_backend_data *bed;
268b6b39 2728 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2729 struct bfd_elf_section_data *esdo;
45d6a902
AM
2730
2731 output_section = input_section->output_section;
45d6a902 2732
d4730f92
BS
2733 bed = get_elf_backend_data (output_bfd);
2734 esdo = elf_section_data (output_section);
2735 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2736 {
d4730f92
BS
2737 output_reldata = &esdo->rel;
2738 swap_out = bed->s->swap_reloc_out;
45d6a902 2739 }
d4730f92
BS
2740 else if (esdo->rela.hdr
2741 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2742 {
d4730f92
BS
2743 output_reldata = &esdo->rela;
2744 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2745 }
2746 else
2747 {
4eca0228 2748 _bfd_error_handler
695344c0 2749 /* xgettext:c-format */
871b3ab2 2750 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2751 output_bfd, input_section->owner, input_section);
297d8443 2752 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2753 return FALSE;
2754 }
2755
d4730f92
BS
2756 erel = output_reldata->hdr->contents;
2757 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2758 irela = internal_relocs;
2759 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2760 * bed->s->int_rels_per_ext_rel);
2761 while (irela < irelaend)
2762 {
2763 (*swap_out) (output_bfd, irela, erel);
2764 irela += bed->s->int_rels_per_ext_rel;
2765 erel += input_rel_hdr->sh_entsize;
2766 }
2767
2768 /* Bump the counter, so that we know where to add the next set of
2769 relocations. */
d4730f92 2770 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2771
2772 return TRUE;
2773}
2774\f
508c3946
L
2775/* Make weak undefined symbols in PIE dynamic. */
2776
2777bfd_boolean
2778_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2779 struct elf_link_hash_entry *h)
2780{
0e1862bb 2781 if (bfd_link_pie (info)
508c3946
L
2782 && h->dynindx == -1
2783 && h->root.type == bfd_link_hash_undefweak)
2784 return bfd_elf_link_record_dynamic_symbol (info, h);
2785
2786 return TRUE;
2787}
2788
45d6a902
AM
2789/* Fix up the flags for a symbol. This handles various cases which
2790 can only be fixed after all the input files are seen. This is
2791 currently called by both adjust_dynamic_symbol and
2792 assign_sym_version, which is unnecessary but perhaps more robust in
2793 the face of future changes. */
2794
28caa186 2795static bfd_boolean
268b6b39
AM
2796_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2797 struct elf_info_failed *eif)
45d6a902 2798{
33774f08 2799 const struct elf_backend_data *bed;
508c3946 2800
45d6a902
AM
2801 /* If this symbol was mentioned in a non-ELF file, try to set
2802 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2803 permit a non-ELF file to correctly refer to a symbol defined in
2804 an ELF dynamic object. */
f5385ebf 2805 if (h->non_elf)
45d6a902
AM
2806 {
2807 while (h->root.type == bfd_link_hash_indirect)
2808 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2809
2810 if (h->root.type != bfd_link_hash_defined
2811 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2812 {
2813 h->ref_regular = 1;
2814 h->ref_regular_nonweak = 1;
2815 }
45d6a902
AM
2816 else
2817 {
2818 if (h->root.u.def.section->owner != NULL
2819 && (bfd_get_flavour (h->root.u.def.section->owner)
2820 == bfd_target_elf_flavour))
f5385ebf
AM
2821 {
2822 h->ref_regular = 1;
2823 h->ref_regular_nonweak = 1;
2824 }
45d6a902 2825 else
f5385ebf 2826 h->def_regular = 1;
45d6a902
AM
2827 }
2828
2829 if (h->dynindx == -1
f5385ebf
AM
2830 && (h->def_dynamic
2831 || h->ref_dynamic))
45d6a902 2832 {
c152c796 2833 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2834 {
2835 eif->failed = TRUE;
2836 return FALSE;
2837 }
2838 }
2839 }
2840 else
2841 {
f5385ebf 2842 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2843 was first seen in a non-ELF file. Fortunately, if the symbol
2844 was first seen in an ELF file, we're probably OK unless the
2845 symbol was defined in a non-ELF file. Catch that case here.
2846 FIXME: We're still in trouble if the symbol was first seen in
2847 a dynamic object, and then later in a non-ELF regular object. */
2848 if ((h->root.type == bfd_link_hash_defined
2849 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2850 && !h->def_regular
45d6a902
AM
2851 && (h->root.u.def.section->owner != NULL
2852 ? (bfd_get_flavour (h->root.u.def.section->owner)
2853 != bfd_target_elf_flavour)
2854 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2855 && !h->def_dynamic)))
2856 h->def_regular = 1;
45d6a902
AM
2857 }
2858
508c3946 2859 /* Backend specific symbol fixup. */
33774f08
AM
2860 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2861 if (bed->elf_backend_fixup_symbol
2862 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2863 return FALSE;
508c3946 2864
45d6a902
AM
2865 /* If this is a final link, and the symbol was defined as a common
2866 symbol in a regular object file, and there was no definition in
2867 any dynamic object, then the linker will have allocated space for
f5385ebf 2868 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2869 flag will not have been set. */
2870 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2871 && !h->def_regular
2872 && h->ref_regular
2873 && !h->def_dynamic
96f29d96 2874 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2875 h->def_regular = 1;
45d6a902 2876
af0bfb9c
AM
2877 /* Symbols defined in discarded sections shouldn't be dynamic. */
2878 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2879 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2880
4deb8f71
L
2881 /* If a weak undefined symbol has non-default visibility, we also
2882 hide it from the dynamic linker. */
af0bfb9c
AM
2883 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2884 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2885 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2886
2887 /* A hidden versioned symbol in executable should be forced local if
2888 it is is locally defined, not referenced by shared library and not
2889 exported. */
2890 else if (bfd_link_executable (eif->info)
2891 && h->versioned == versioned_hidden
2892 && !eif->info->export_dynamic
2893 && !h->dynamic
2894 && !h->ref_dynamic
2895 && h->def_regular)
2896 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2897
45d6a902
AM
2898 /* If -Bsymbolic was used (which means to bind references to global
2899 symbols to the definition within the shared object), and this
2900 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2901 need a PLT entry. Likewise, if the symbol has non-default
2902 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2903 will force it local. */
4deb8f71
L
2904 else if (h->needs_plt
2905 && bfd_link_pic (eif->info)
2906 && is_elf_hash_table (eif->info->hash)
2907 && (SYMBOLIC_BIND (eif->info, h)
2908 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2909 && h->def_regular)
45d6a902 2910 {
45d6a902
AM
2911 bfd_boolean force_local;
2912
45d6a902
AM
2913 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2914 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2915 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2916 }
2917
45d6a902
AM
2918 /* If this is a weak defined symbol in a dynamic object, and we know
2919 the real definition in the dynamic object, copy interesting flags
2920 over to the real definition. */
60d67dc8 2921 if (h->is_weakalias)
45d6a902 2922 {
60d67dc8
AM
2923 struct elf_link_hash_entry *def = weakdef (h);
2924
45d6a902
AM
2925 /* If the real definition is defined by a regular object file,
2926 don't do anything special. See the longer description in
5b9d7a9a
AM
2927 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2928 bfd_link_hash_defined as it was when put on the alias list
2929 then it must have originally been a versioned symbol (for
2930 which a non-versioned indirect symbol is created) and later
2931 a definition for the non-versioned symbol is found. In that
2932 case the indirection is flipped with the versioned symbol
2933 becoming an indirect pointing at the non-versioned symbol.
2934 Thus, not an alias any more. */
2935 if (def->def_regular
2936 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2937 {
2938 h = def;
2939 while ((h = h->u.alias) != def)
2940 h->is_weakalias = 0;
2941 }
45d6a902 2942 else
a26587ba 2943 {
4e6b54a6
AM
2944 while (h->root.type == bfd_link_hash_indirect)
2945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2946 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2947 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2948 BFD_ASSERT (def->def_dynamic);
60d67dc8 2949 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2950 }
45d6a902
AM
2951 }
2952
2953 return TRUE;
2954}
2955
2956/* Make the backend pick a good value for a dynamic symbol. This is
2957 called via elf_link_hash_traverse, and also calls itself
2958 recursively. */
2959
28caa186 2960static bfd_boolean
268b6b39 2961_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2962{
a50b1753 2963 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2964 struct elf_link_hash_table *htab;
9c5bfbb7 2965 const struct elf_backend_data *bed;
45d6a902 2966
0eddce27 2967 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2968 return FALSE;
2969
45d6a902
AM
2970 /* Ignore indirect symbols. These are added by the versioning code. */
2971 if (h->root.type == bfd_link_hash_indirect)
2972 return TRUE;
2973
2974 /* Fix the symbol flags. */
2975 if (! _bfd_elf_fix_symbol_flags (h, eif))
2976 return FALSE;
2977
559192d8
AM
2978 htab = elf_hash_table (eif->info);
2979 bed = get_elf_backend_data (htab->dynobj);
2980
954b63d4
AM
2981 if (h->root.type == bfd_link_hash_undefweak)
2982 {
2983 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2984 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2985 else if (eif->info->dynamic_undefined_weak > 0
2986 && h->ref_regular
2987 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2988 && !bfd_hide_sym_by_version (eif->info->version_info,
2989 h->root.root.string))
2990 {
2991 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2992 {
2993 eif->failed = TRUE;
2994 return FALSE;
2995 }
2996 }
2997 }
2998
45d6a902
AM
2999 /* If this symbol does not require a PLT entry, and it is not
3000 defined by a dynamic object, or is not referenced by a regular
3001 object, ignore it. We do have to handle a weak defined symbol,
3002 even if no regular object refers to it, if we decided to add it
3003 to the dynamic symbol table. FIXME: Do we normally need to worry
3004 about symbols which are defined by one dynamic object and
3005 referenced by another one? */
f5385ebf 3006 if (!h->needs_plt
91e21fb7 3007 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3008 && (h->def_regular
3009 || !h->def_dynamic
3010 || (!h->ref_regular
60d67dc8 3011 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3012 {
a6aa5195 3013 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3014 return TRUE;
3015 }
3016
3017 /* If we've already adjusted this symbol, don't do it again. This
3018 can happen via a recursive call. */
f5385ebf 3019 if (h->dynamic_adjusted)
45d6a902
AM
3020 return TRUE;
3021
3022 /* Don't look at this symbol again. Note that we must set this
3023 after checking the above conditions, because we may look at a
3024 symbol once, decide not to do anything, and then get called
3025 recursively later after REF_REGULAR is set below. */
f5385ebf 3026 h->dynamic_adjusted = 1;
45d6a902
AM
3027
3028 /* If this is a weak definition, and we know a real definition, and
3029 the real symbol is not itself defined by a regular object file,
3030 then get a good value for the real definition. We handle the
3031 real symbol first, for the convenience of the backend routine.
3032
3033 Note that there is a confusing case here. If the real definition
3034 is defined by a regular object file, we don't get the real symbol
3035 from the dynamic object, but we do get the weak symbol. If the
3036 processor backend uses a COPY reloc, then if some routine in the
3037 dynamic object changes the real symbol, we will not see that
3038 change in the corresponding weak symbol. This is the way other
3039 ELF linkers work as well, and seems to be a result of the shared
3040 library model.
3041
3042 I will clarify this issue. Most SVR4 shared libraries define the
3043 variable _timezone and define timezone as a weak synonym. The
3044 tzset call changes _timezone. If you write
3045 extern int timezone;
3046 int _timezone = 5;
3047 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3048 you might expect that, since timezone is a synonym for _timezone,
3049 the same number will print both times. However, if the processor
3050 backend uses a COPY reloc, then actually timezone will be copied
3051 into your process image, and, since you define _timezone
3052 yourself, _timezone will not. Thus timezone and _timezone will
3053 wind up at different memory locations. The tzset call will set
3054 _timezone, leaving timezone unchanged. */
3055
60d67dc8 3056 if (h->is_weakalias)
45d6a902 3057 {
60d67dc8
AM
3058 struct elf_link_hash_entry *def = weakdef (h);
3059
ec24dc88 3060 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3061 the alias by a regular object file via the weak symbol H. */
3062 def->ref_regular = 1;
45d6a902 3063
ec24dc88 3064 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3065 the strong alias before H by recursively calling ourselves. */
3066 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3067 return FALSE;
3068 }
3069
3070 /* If a symbol has no type and no size and does not require a PLT
3071 entry, then we are probably about to do the wrong thing here: we
3072 are probably going to create a COPY reloc for an empty object.
3073 This case can arise when a shared object is built with assembly
3074 code, and the assembly code fails to set the symbol type. */
3075 if (h->size == 0
3076 && h->type == STT_NOTYPE
f5385ebf 3077 && !h->needs_plt)
4eca0228 3078 _bfd_error_handler
45d6a902
AM
3079 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3080 h->root.root.string);
3081
45d6a902
AM
3082 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3083 {
3084 eif->failed = TRUE;
3085 return FALSE;
3086 }
3087
3088 return TRUE;
3089}
3090
027297b7
L
3091/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3092 DYNBSS. */
3093
3094bfd_boolean
6cabe1ea
AM
3095_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3096 struct elf_link_hash_entry *h,
027297b7
L
3097 asection *dynbss)
3098{
91ac5911 3099 unsigned int power_of_two;
027297b7
L
3100 bfd_vma mask;
3101 asection *sec = h->root.u.def.section;
3102
de194d85 3103 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3104 requirement of symbols defined in the section. Since we don't
3105 know the symbol alignment requirement, we start with the
3106 maximum alignment and check low bits of the symbol address
3107 for the minimum alignment. */
fd361982 3108 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3109 mask = ((bfd_vma) 1 << power_of_two) - 1;
3110 while ((h->root.u.def.value & mask) != 0)
3111 {
3112 mask >>= 1;
3113 --power_of_two;
3114 }
027297b7 3115
fd361982 3116 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3117 {
3118 /* Adjust the section alignment if needed. */
fd361982 3119 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3120 return FALSE;
3121 }
3122
91ac5911 3123 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3124 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3125
3126 /* Define the symbol as being at this point in DYNBSS. */
3127 h->root.u.def.section = dynbss;
3128 h->root.u.def.value = dynbss->size;
3129
3130 /* Increment the size of DYNBSS to make room for the symbol. */
3131 dynbss->size += h->size;
3132
f7483970
L
3133 /* No error if extern_protected_data is true. */
3134 if (h->protected_def
889c2a67
L
3135 && (!info->extern_protected_data
3136 || (info->extern_protected_data < 0
3137 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3138 info->callbacks->einfo
c1c8c1ef 3139 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3140 h->root.root.string);
6cabe1ea 3141
027297b7
L
3142 return TRUE;
3143}
3144
45d6a902
AM
3145/* Adjust all external symbols pointing into SEC_MERGE sections
3146 to reflect the object merging within the sections. */
3147
28caa186 3148static bfd_boolean
268b6b39 3149_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3150{
3151 asection *sec;
3152
45d6a902
AM
3153 if ((h->root.type == bfd_link_hash_defined
3154 || h->root.type == bfd_link_hash_defweak)
3155 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3156 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3157 {
a50b1753 3158 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3159
3160 h->root.u.def.value =
3161 _bfd_merged_section_offset (output_bfd,
3162 &h->root.u.def.section,
3163 elf_section_data (sec)->sec_info,
753731ee 3164 h->root.u.def.value);
45d6a902
AM
3165 }
3166
3167 return TRUE;
3168}
986a241f
RH
3169
3170/* Returns false if the symbol referred to by H should be considered
3171 to resolve local to the current module, and true if it should be
3172 considered to bind dynamically. */
3173
3174bfd_boolean
268b6b39
AM
3175_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3176 struct bfd_link_info *info,
89a2ee5a 3177 bfd_boolean not_local_protected)
986a241f
RH
3178{
3179 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3180 const struct elf_backend_data *bed;
3181 struct elf_link_hash_table *hash_table;
986a241f
RH
3182
3183 if (h == NULL)
3184 return FALSE;
3185
3186 while (h->root.type == bfd_link_hash_indirect
3187 || h->root.type == bfd_link_hash_warning)
3188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3189
3190 /* If it was forced local, then clearly it's not dynamic. */
3191 if (h->dynindx == -1)
3192 return FALSE;
f5385ebf 3193 if (h->forced_local)
986a241f
RH
3194 return FALSE;
3195
3196 /* Identify the cases where name binding rules say that a
3197 visible symbol resolves locally. */
0e1862bb
L
3198 binding_stays_local_p = (bfd_link_executable (info)
3199 || SYMBOLIC_BIND (info, h));
986a241f
RH
3200
3201 switch (ELF_ST_VISIBILITY (h->other))
3202 {
3203 case STV_INTERNAL:
3204 case STV_HIDDEN:
3205 return FALSE;
3206
3207 case STV_PROTECTED:
fcb93ecf
PB
3208 hash_table = elf_hash_table (info);
3209 if (!is_elf_hash_table (hash_table))
3210 return FALSE;
3211
3212 bed = get_elf_backend_data (hash_table->dynobj);
3213
986a241f
RH
3214 /* Proper resolution for function pointer equality may require
3215 that these symbols perhaps be resolved dynamically, even though
3216 we should be resolving them to the current module. */
89a2ee5a 3217 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3218 binding_stays_local_p = TRUE;
3219 break;
3220
3221 default:
986a241f
RH
3222 break;
3223 }
3224
aa37626c 3225 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3226 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3227 return TRUE;
3228
986a241f
RH
3229 /* Otherwise, the symbol is dynamic if binding rules don't tell
3230 us that it remains local. */
3231 return !binding_stays_local_p;
3232}
f6c52c13
AM
3233
3234/* Return true if the symbol referred to by H should be considered
3235 to resolve local to the current module, and false otherwise. Differs
3236 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3237 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3238 for the place where dynindx == -1 is tested. If that test is true,
3239 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3240 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3241 defined symbols.
89a2ee5a
AM
3242 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3243 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3244 treatment of undefined weak symbols. For those that do not make
3245 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3246
3247bfd_boolean
268b6b39
AM
3248_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3249 struct bfd_link_info *info,
3250 bfd_boolean local_protected)
f6c52c13 3251{
fcb93ecf
PB
3252 const struct elf_backend_data *bed;
3253 struct elf_link_hash_table *hash_table;
3254
f6c52c13
AM
3255 /* If it's a local sym, of course we resolve locally. */
3256 if (h == NULL)
3257 return TRUE;
3258
d95edcac
L
3259 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3260 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3261 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3262 return TRUE;
3263
0fad2956
MR
3264 /* Forced local symbols resolve locally. */
3265 if (h->forced_local)
3266 return TRUE;
3267
7e2294f9
AO
3268 /* Common symbols that become definitions don't get the DEF_REGULAR
3269 flag set, so test it first, and don't bail out. */
3270 if (ELF_COMMON_DEF_P (h))
3271 /* Do nothing. */;
f6c52c13 3272 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3273 resolve locally. The sym is either undefined or dynamic. */
3274 else if (!h->def_regular)
f6c52c13
AM
3275 return FALSE;
3276
0fad2956 3277 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3278 if (h->dynindx == -1)
3279 return TRUE;
3280
3281 /* At this point, we know the symbol is defined and dynamic. In an
3282 executable it must resolve locally, likewise when building symbolic
3283 shared libraries. */
0e1862bb 3284 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3285 return TRUE;
3286
3287 /* Now deal with defined dynamic symbols in shared libraries. Ones
3288 with default visibility might not resolve locally. */
3289 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3290 return FALSE;
3291
fcb93ecf
PB
3292 hash_table = elf_hash_table (info);
3293 if (!is_elf_hash_table (hash_table))
3294 return TRUE;
3295
3296 bed = get_elf_backend_data (hash_table->dynobj);
3297
f7483970
L
3298 /* If extern_protected_data is false, STV_PROTECTED non-function
3299 symbols are local. */
889c2a67
L
3300 if ((!info->extern_protected_data
3301 || (info->extern_protected_data < 0
3302 && !bed->extern_protected_data))
3303 && !bed->is_function_type (h->type))
1c16dfa5
L
3304 return TRUE;
3305
f6c52c13 3306 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3307 symbols be treated as dynamic symbols. If the address of a
3308 function not defined in an executable is set to that function's
3309 plt entry in the executable, then the address of the function in
3310 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3311 return local_protected;
3312}
e1918d23
AM
3313
3314/* Caches some TLS segment info, and ensures that the TLS segment vma is
3315 aligned. Returns the first TLS output section. */
3316
3317struct bfd_section *
3318_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3319{
3320 struct bfd_section *sec, *tls;
3321 unsigned int align = 0;
3322
3323 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3324 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3325 break;
3326 tls = sec;
3327
3328 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3329 if (sec->alignment_power > align)
3330 align = sec->alignment_power;
3331
3332 elf_hash_table (info)->tls_sec = tls;
3333
fdde2fb6
SH
3334 /* Ensure the alignment of the first section (usually .tdata) is the largest
3335 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3336 if (tls != NULL)
3337 tls->alignment_power = align;
3338
3339 return tls;
3340}
0ad989f9
L
3341
3342/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3343static bfd_boolean
3344is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3345 Elf_Internal_Sym *sym)
3346{
a4d8e49b
L
3347 const struct elf_backend_data *bed;
3348
0ad989f9
L
3349 /* Local symbols do not count, but target specific ones might. */
3350 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3351 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3352 return FALSE;
3353
fcb93ecf 3354 bed = get_elf_backend_data (abfd);
0ad989f9 3355 /* Function symbols do not count. */
fcb93ecf 3356 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3357 return FALSE;
3358
3359 /* If the section is undefined, then so is the symbol. */
3360 if (sym->st_shndx == SHN_UNDEF)
3361 return FALSE;
3362
3363 /* If the symbol is defined in the common section, then
3364 it is a common definition and so does not count. */
a4d8e49b 3365 if (bed->common_definition (sym))
0ad989f9
L
3366 return FALSE;
3367
3368 /* If the symbol is in a target specific section then we
3369 must rely upon the backend to tell us what it is. */
3370 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3371 /* FIXME - this function is not coded yet:
3372
3373 return _bfd_is_global_symbol_definition (abfd, sym);
3374
3375 Instead for now assume that the definition is not global,
3376 Even if this is wrong, at least the linker will behave
3377 in the same way that it used to do. */
3378 return FALSE;
3379
3380 return TRUE;
3381}
3382
3383/* Search the symbol table of the archive element of the archive ABFD
3384 whose archive map contains a mention of SYMDEF, and determine if
3385 the symbol is defined in this element. */
3386static bfd_boolean
3387elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3388{
3389 Elf_Internal_Shdr * hdr;
ef53be89
AM
3390 size_t symcount;
3391 size_t extsymcount;
3392 size_t extsymoff;
0ad989f9
L
3393 Elf_Internal_Sym *isymbuf;
3394 Elf_Internal_Sym *isym;
3395 Elf_Internal_Sym *isymend;
3396 bfd_boolean result;
3397
3398 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3399 if (abfd == NULL)
3400 return FALSE;
3401
3402 if (! bfd_check_format (abfd, bfd_object))
3403 return FALSE;
3404
7dc3990e
L
3405 /* Select the appropriate symbol table. If we don't know if the
3406 object file is an IR object, give linker LTO plugin a chance to
3407 get the correct symbol table. */
3408 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3409#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3410 || (abfd->plugin_format == bfd_plugin_unknown
3411 && bfd_link_plugin_object_p (abfd))
3412#endif
3413 )
3414 {
3415 /* Use the IR symbol table if the object has been claimed by
3416 plugin. */
3417 abfd = abfd->plugin_dummy_bfd;
3418 hdr = &elf_tdata (abfd)->symtab_hdr;
3419 }
3420 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3421 hdr = &elf_tdata (abfd)->symtab_hdr;
3422 else
3423 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3424
3425 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3426
3427 /* The sh_info field of the symtab header tells us where the
3428 external symbols start. We don't care about the local symbols. */
3429 if (elf_bad_symtab (abfd))
3430 {
3431 extsymcount = symcount;
3432 extsymoff = 0;
3433 }
3434 else
3435 {
3436 extsymcount = symcount - hdr->sh_info;
3437 extsymoff = hdr->sh_info;
3438 }
3439
3440 if (extsymcount == 0)
3441 return FALSE;
3442
3443 /* Read in the symbol table. */
3444 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3445 NULL, NULL, NULL);
3446 if (isymbuf == NULL)
3447 return FALSE;
3448
3449 /* Scan the symbol table looking for SYMDEF. */
3450 result = FALSE;
3451 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3452 {
3453 const char *name;
3454
3455 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3456 isym->st_name);
3457 if (name == NULL)
3458 break;
3459
3460 if (strcmp (name, symdef->name) == 0)
3461 {
3462 result = is_global_data_symbol_definition (abfd, isym);
3463 break;
3464 }
3465 }
3466
3467 free (isymbuf);
3468
3469 return result;
3470}
3471\f
5a580b3a
AM
3472/* Add an entry to the .dynamic table. */
3473
3474bfd_boolean
3475_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3476 bfd_vma tag,
3477 bfd_vma val)
3478{
3479 struct elf_link_hash_table *hash_table;
3480 const struct elf_backend_data *bed;
3481 asection *s;
3482 bfd_size_type newsize;
3483 bfd_byte *newcontents;
3484 Elf_Internal_Dyn dyn;
3485
3486 hash_table = elf_hash_table (info);
3487 if (! is_elf_hash_table (hash_table))
3488 return FALSE;
3489
7f923b7f
AM
3490 if (tag == DT_RELA || tag == DT_REL)
3491 hash_table->dynamic_relocs = TRUE;
3492
5a580b3a 3493 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3494 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3495 BFD_ASSERT (s != NULL);
3496
eea6121a 3497 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3498 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3499 if (newcontents == NULL)
3500 return FALSE;
3501
3502 dyn.d_tag = tag;
3503 dyn.d_un.d_val = val;
eea6121a 3504 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3505
eea6121a 3506 s->size = newsize;
5a580b3a
AM
3507 s->contents = newcontents;
3508
3509 return TRUE;
3510}
3511
6f6fd151
L
3512/* Strip zero-sized dynamic sections. */
3513
3514bfd_boolean
3515_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3516{
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s, *sdynamic, **pp;
3520 asection *rela_dyn, *rel_dyn;
3521 Elf_Internal_Dyn dyn;
3522 bfd_byte *extdyn, *next;
3523 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3524 bfd_boolean strip_zero_sized;
3525 bfd_boolean strip_zero_sized_plt;
3526
3527 if (bfd_link_relocatable (info))
3528 return TRUE;
3529
3530 hash_table = elf_hash_table (info);
3531 if (!is_elf_hash_table (hash_table))
3532 return FALSE;
3533
3534 if (!hash_table->dynobj)
3535 return TRUE;
3536
3537 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3538 if (!sdynamic)
3539 return TRUE;
3540
3541 bed = get_elf_backend_data (hash_table->dynobj);
3542 swap_dyn_in = bed->s->swap_dyn_in;
3543
3544 strip_zero_sized = FALSE;
3545 strip_zero_sized_plt = FALSE;
3546
3547 /* Strip zero-sized dynamic sections. */
3548 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3549 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3550 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3551 if (s->size == 0
3552 && (s == rela_dyn
3553 || s == rel_dyn
3554 || s == hash_table->srelplt->output_section
3555 || s == hash_table->splt->output_section))
3556 {
3557 *pp = s->next;
3558 info->output_bfd->section_count--;
3559 strip_zero_sized = TRUE;
3560 if (s == rela_dyn)
3561 s = rela_dyn;
3562 if (s == rel_dyn)
3563 s = rel_dyn;
3564 else if (s == hash_table->splt->output_section)
3565 {
3566 s = hash_table->splt;
3567 strip_zero_sized_plt = TRUE;
3568 }
3569 else
3570 s = hash_table->srelplt;
3571 s->flags |= SEC_EXCLUDE;
3572 s->output_section = bfd_abs_section_ptr;
3573 }
3574 else
3575 pp = &s->next;
3576
3577 if (strip_zero_sized_plt)
3578 for (extdyn = sdynamic->contents;
3579 extdyn < sdynamic->contents + sdynamic->size;
3580 extdyn = next)
3581 {
3582 next = extdyn + bed->s->sizeof_dyn;
3583 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3584 switch (dyn.d_tag)
3585 {
3586 default:
3587 break;
3588 case DT_JMPREL:
3589 case DT_PLTRELSZ:
3590 case DT_PLTREL:
3591 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3592 the procedure linkage table (the .plt section) has been
3593 removed. */
3594 memmove (extdyn, next,
3595 sdynamic->size - (next - sdynamic->contents));
3596 next = extdyn;
3597 }
3598 }
3599
3600 if (strip_zero_sized)
3601 {
3602 /* Regenerate program headers. */
3603 elf_seg_map (info->output_bfd) = NULL;
3604 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3605 }
3606
3607 return TRUE;
3608}
3609
e310298c 3610/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3611 1 if a DT_NEEDED tag already exists, and 0 on success. */
3612
e310298c
AM
3613int
3614bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3615{
3616 struct elf_link_hash_table *hash_table;
ef53be89 3617 size_t strindex;
e310298c 3618 const char *soname;
5a580b3a 3619
7e9f0867
AM
3620 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3621 return -1;
3622
5a580b3a 3623 hash_table = elf_hash_table (info);
e310298c 3624 soname = elf_dt_name (abfd);
5a580b3a 3625 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3626 if (strindex == (size_t) -1)
5a580b3a
AM
3627 return -1;
3628
02be4619 3629 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3630 {
3631 asection *sdyn;
3632 const struct elf_backend_data *bed;
3633 bfd_byte *extdyn;
3634
3635 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3636 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3637 if (sdyn != NULL)
3638 for (extdyn = sdyn->contents;
3639 extdyn < sdyn->contents + sdyn->size;
3640 extdyn += bed->s->sizeof_dyn)
3641 {
3642 Elf_Internal_Dyn dyn;
5a580b3a 3643
7e9f0867
AM
3644 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3645 if (dyn.d_tag == DT_NEEDED
3646 && dyn.d_un.d_val == strindex)
3647 {
3648 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3649 return 1;
3650 }
3651 }
5a580b3a
AM
3652 }
3653
e310298c
AM
3654 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3655 return -1;
7e9f0867 3656
e310298c
AM
3657 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3658 return -1;
5a580b3a
AM
3659
3660 return 0;
3661}
3662
7b15fa7a
AM
3663/* Return true if SONAME is on the needed list between NEEDED and STOP
3664 (or the end of list if STOP is NULL), and needed by a library that
3665 will be loaded. */
3666
010e5ae2 3667static bfd_boolean
7b15fa7a
AM
3668on_needed_list (const char *soname,
3669 struct bfd_link_needed_list *needed,
3670 struct bfd_link_needed_list *stop)
010e5ae2 3671{
7b15fa7a
AM
3672 struct bfd_link_needed_list *look;
3673 for (look = needed; look != stop; look = look->next)
3674 if (strcmp (soname, look->name) == 0
3675 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3676 /* If needed by a library that itself is not directly
3677 needed, recursively check whether that library is
3678 indirectly needed. Since we add DT_NEEDED entries to
3679 the end of the list, library dependencies appear after
3680 the library. Therefore search prior to the current
3681 LOOK, preventing possible infinite recursion. */
3682 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3683 return TRUE;
3684
3685 return FALSE;
3686}
3687
3a3f4bf7 3688/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3689static int
3690elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3691{
3692 const struct elf_link_hash_entry *h1;
3693 const struct elf_link_hash_entry *h2;
10b7e05b 3694 bfd_signed_vma vdiff;
3a3f4bf7
AM
3695 int sdiff;
3696 const char *n1;
3697 const char *n2;
5a580b3a
AM
3698
3699 h1 = *(const struct elf_link_hash_entry **) arg1;
3700 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3701 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3702 if (vdiff != 0)
3703 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3704
3705 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3706 if (sdiff != 0)
3707 return sdiff;
3708
3709 /* Sort so that sized symbols are selected over zero size symbols. */
3710 vdiff = h1->size - h2->size;
3711 if (vdiff != 0)
3712 return vdiff > 0 ? 1 : -1;
3713
3714 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3715 if (h1->type != h2->type)
3716 return h1->type - h2->type;
3717
3718 /* If symbols are properly sized and typed, and multiple strong
3719 aliases are not defined in a shared library by the user we
3720 shouldn't get here. Unfortunately linker script symbols like
3721 __bss_start sometimes match a user symbol defined at the start of
3722 .bss without proper size and type. We'd like to preference the
3723 user symbol over reserved system symbols. Sort on leading
3724 underscores. */
3725 n1 = h1->root.root.string;
3726 n2 = h2->root.root.string;
3727 while (*n1 == *n2)
10b7e05b 3728 {
3a3f4bf7
AM
3729 if (*n1 == 0)
3730 break;
3731 ++n1;
3732 ++n2;
10b7e05b 3733 }
3a3f4bf7
AM
3734 if (*n1 == '_')
3735 return -1;
3736 if (*n2 == '_')
3737 return 1;
3738
3739 /* Final sort on name selects user symbols like '_u' over reserved
3740 system symbols like '_Z' and also will avoid qsort instability. */
3741 return *n1 - *n2;
5a580b3a 3742}
4ad4eba5 3743
5a580b3a
AM
3744/* This function is used to adjust offsets into .dynstr for
3745 dynamic symbols. This is called via elf_link_hash_traverse. */
3746
3747static bfd_boolean
3748elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3749{
a50b1753 3750 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3751
5a580b3a
AM
3752 if (h->dynindx != -1)
3753 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3754 return TRUE;
3755}
3756
3757/* Assign string offsets in .dynstr, update all structures referencing
3758 them. */
3759
4ad4eba5
AM
3760static bfd_boolean
3761elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3762{
3763 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3764 struct elf_link_local_dynamic_entry *entry;
3765 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3766 bfd *dynobj = hash_table->dynobj;
3767 asection *sdyn;
3768 bfd_size_type size;
3769 const struct elf_backend_data *bed;
3770 bfd_byte *extdyn;
3771
3772 _bfd_elf_strtab_finalize (dynstr);
3773 size = _bfd_elf_strtab_size (dynstr);
3774
3775 bed = get_elf_backend_data (dynobj);
3d4d4302 3776 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3777 BFD_ASSERT (sdyn != NULL);
3778
3779 /* Update all .dynamic entries referencing .dynstr strings. */
3780 for (extdyn = sdyn->contents;
eea6121a 3781 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3782 extdyn += bed->s->sizeof_dyn)
3783 {
3784 Elf_Internal_Dyn dyn;
3785
3786 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3787 switch (dyn.d_tag)
3788 {
3789 case DT_STRSZ:
3790 dyn.d_un.d_val = size;
3791 break;
3792 case DT_NEEDED:
3793 case DT_SONAME:
3794 case DT_RPATH:
3795 case DT_RUNPATH:
3796 case DT_FILTER:
3797 case DT_AUXILIARY:
7ee314fa
AM
3798 case DT_AUDIT:
3799 case DT_DEPAUDIT:
5a580b3a
AM
3800 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3801 break;
3802 default:
3803 continue;
3804 }
3805 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3806 }
3807
3808 /* Now update local dynamic symbols. */
3809 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3810 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3811 entry->isym.st_name);
3812
3813 /* And the rest of dynamic symbols. */
3814 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3815
3816 /* Adjust version definitions. */
3817 if (elf_tdata (output_bfd)->cverdefs)
3818 {
3819 asection *s;
3820 bfd_byte *p;
ef53be89 3821 size_t i;
5a580b3a
AM
3822 Elf_Internal_Verdef def;
3823 Elf_Internal_Verdaux defaux;
3824
3d4d4302 3825 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3826 p = s->contents;
3827 do
3828 {
3829 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3830 &def);
3831 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3832 if (def.vd_aux != sizeof (Elf_External_Verdef))
3833 continue;
5a580b3a
AM
3834 for (i = 0; i < def.vd_cnt; ++i)
3835 {
3836 _bfd_elf_swap_verdaux_in (output_bfd,
3837 (Elf_External_Verdaux *) p, &defaux);
3838 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3839 defaux.vda_name);
3840 _bfd_elf_swap_verdaux_out (output_bfd,
3841 &defaux, (Elf_External_Verdaux *) p);
3842 p += sizeof (Elf_External_Verdaux);
3843 }
3844 }
3845 while (def.vd_next);
3846 }
3847
3848 /* Adjust version references. */
3849 if (elf_tdata (output_bfd)->verref)
3850 {
3851 asection *s;
3852 bfd_byte *p;
ef53be89 3853 size_t i;
5a580b3a
AM
3854 Elf_Internal_Verneed need;
3855 Elf_Internal_Vernaux needaux;
3856
3d4d4302 3857 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3858 p = s->contents;
3859 do
3860 {
3861 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3862 &need);
3863 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3864 _bfd_elf_swap_verneed_out (output_bfd, &need,
3865 (Elf_External_Verneed *) p);
3866 p += sizeof (Elf_External_Verneed);
3867 for (i = 0; i < need.vn_cnt; ++i)
3868 {
3869 _bfd_elf_swap_vernaux_in (output_bfd,
3870 (Elf_External_Vernaux *) p, &needaux);
3871 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3872 needaux.vna_name);
3873 _bfd_elf_swap_vernaux_out (output_bfd,
3874 &needaux,
3875 (Elf_External_Vernaux *) p);
3876 p += sizeof (Elf_External_Vernaux);
3877 }
3878 }
3879 while (need.vn_next);
3880 }
3881
3882 return TRUE;
3883}
3884\f
13285a1b
AM
3885/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3886 The default is to only match when the INPUT and OUTPUT are exactly
3887 the same target. */
3888
3889bfd_boolean
3890_bfd_elf_default_relocs_compatible (const bfd_target *input,
3891 const bfd_target *output)
3892{
3893 return input == output;
3894}
3895
3896/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3897 This version is used when different targets for the same architecture
3898 are virtually identical. */
3899
3900bfd_boolean
3901_bfd_elf_relocs_compatible (const bfd_target *input,
3902 const bfd_target *output)
3903{
3904 const struct elf_backend_data *obed, *ibed;
3905
3906 if (input == output)
3907 return TRUE;
3908
3909 ibed = xvec_get_elf_backend_data (input);
3910 obed = xvec_get_elf_backend_data (output);
3911
3912 if (ibed->arch != obed->arch)
3913 return FALSE;
3914
3915 /* If both backends are using this function, deem them compatible. */
3916 return ibed->relocs_compatible == obed->relocs_compatible;
3917}
3918
e5034e59
AM
3919/* Make a special call to the linker "notice" function to tell it that
3920 we are about to handle an as-needed lib, or have finished
1b786873 3921 processing the lib. */
e5034e59
AM
3922
3923bfd_boolean
3924_bfd_elf_notice_as_needed (bfd *ibfd,
3925 struct bfd_link_info *info,
3926 enum notice_asneeded_action act)
3927{
46135103 3928 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3929}
3930
d9689752
L
3931/* Check relocations an ELF object file. */
3932
3933bfd_boolean
3934_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3935{
3936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3937 struct elf_link_hash_table *htab = elf_hash_table (info);
3938
3939 /* If this object is the same format as the output object, and it is
3940 not a shared library, then let the backend look through the
3941 relocs.
3942
3943 This is required to build global offset table entries and to
3944 arrange for dynamic relocs. It is not required for the
3945 particular common case of linking non PIC code, even when linking
3946 against shared libraries, but unfortunately there is no way of
3947 knowing whether an object file has been compiled PIC or not.
3948 Looking through the relocs is not particularly time consuming.
3949 The problem is that we must either (1) keep the relocs in memory,
3950 which causes the linker to require additional runtime memory or
3951 (2) read the relocs twice from the input file, which wastes time.
3952 This would be a good case for using mmap.
3953
3954 I have no idea how to handle linking PIC code into a file of a
3955 different format. It probably can't be done. */
3956 if ((abfd->flags & DYNAMIC) == 0
3957 && is_elf_hash_table (htab)
3958 && bed->check_relocs != NULL
3959 && elf_object_id (abfd) == elf_hash_table_id (htab)
3960 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3961 {
3962 asection *o;
3963
3964 for (o = abfd->sections; o != NULL; o = o->next)
3965 {
3966 Elf_Internal_Rela *internal_relocs;
3967 bfd_boolean ok;
3968
c4b126b8
L
3969 /* Don't check relocations in excluded sections. Don't do
3970 anything special with non-loaded, non-alloced sections.
3971 In particular, any relocs in such sections should not
3972 affect GOT and PLT reference counting (ie. we don't
3973 allow them to create GOT or PLT entries), there's no
3974 possibility or desire to optimize TLS relocs, and
3975 there's not much point in propagating relocs to shared
3976 libs that the dynamic linker won't relocate. */
3977 if ((o->flags & SEC_ALLOC) == 0
3978 || (o->flags & SEC_RELOC) == 0
5ce03cea 3979 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3980 || o->reloc_count == 0
3981 || ((info->strip == strip_all || info->strip == strip_debugger)
3982 && (o->flags & SEC_DEBUGGING) != 0)
3983 || bfd_is_abs_section (o->output_section))
3984 continue;
3985
3986 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3987 info->keep_memory);
3988 if (internal_relocs == NULL)
3989 return FALSE;
3990
3991 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3992
3993 if (elf_section_data (o)->relocs != internal_relocs)
3994 free (internal_relocs);
3995
3996 if (! ok)
3997 return FALSE;
3998 }
3999 }
4000
4001 return TRUE;
4002}
4003
4ad4eba5
AM
4004/* Add symbols from an ELF object file to the linker hash table. */
4005
4006static bfd_boolean
4007elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4008{
a0c402a5 4009 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4010 Elf_Internal_Shdr *hdr;
ef53be89
AM
4011 size_t symcount;
4012 size_t extsymcount;
4013 size_t extsymoff;
4ad4eba5
AM
4014 struct elf_link_hash_entry **sym_hash;
4015 bfd_boolean dynamic;
4016 Elf_External_Versym *extversym = NULL;
be22c732 4017 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4018 Elf_External_Versym *ever;
4019 struct elf_link_hash_entry *weaks;
4020 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4021 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4022 Elf_Internal_Sym *isymbuf = NULL;
4023 Elf_Internal_Sym *isym;
4024 Elf_Internal_Sym *isymend;
4025 const struct elf_backend_data *bed;
4026 bfd_boolean add_needed;
66eb6687 4027 struct elf_link_hash_table *htab;
66eb6687 4028 void *alloc_mark = NULL;
4f87808c
AM
4029 struct bfd_hash_entry **old_table = NULL;
4030 unsigned int old_size = 0;
4031 unsigned int old_count = 0;
66eb6687 4032 void *old_tab = NULL;
66eb6687
AM
4033 void *old_ent;
4034 struct bfd_link_hash_entry *old_undefs = NULL;
4035 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4036 void *old_strtab = NULL;
66eb6687 4037 size_t tabsize = 0;
db6a5d5f 4038 asection *s;
29a9f53e 4039 bfd_boolean just_syms;
4ad4eba5 4040
66eb6687 4041 htab = elf_hash_table (info);
4ad4eba5 4042 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4043
4044 if ((abfd->flags & DYNAMIC) == 0)
4045 dynamic = FALSE;
4046 else
4047 {
4048 dynamic = TRUE;
4049
4050 /* You can't use -r against a dynamic object. Also, there's no
4051 hope of using a dynamic object which does not exactly match
4052 the format of the output file. */
0e1862bb 4053 if (bfd_link_relocatable (info)
66eb6687 4054 || !is_elf_hash_table (htab)
f13a99db 4055 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4056 {
0e1862bb 4057 if (bfd_link_relocatable (info))
9a0789ec
NC
4058 bfd_set_error (bfd_error_invalid_operation);
4059 else
4060 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4061 goto error_return;
4062 }
4063 }
4064
a0c402a5
L
4065 ehdr = elf_elfheader (abfd);
4066 if (info->warn_alternate_em
4067 && bed->elf_machine_code != ehdr->e_machine
4068 && ((bed->elf_machine_alt1 != 0
4069 && ehdr->e_machine == bed->elf_machine_alt1)
4070 || (bed->elf_machine_alt2 != 0
4071 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4072 _bfd_error_handler
695344c0 4073 /* xgettext:c-format */
9793eb77 4074 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4075 ehdr->e_machine, abfd, bed->elf_machine_code);
4076
4ad4eba5
AM
4077 /* As a GNU extension, any input sections which are named
4078 .gnu.warning.SYMBOL are treated as warning symbols for the given
4079 symbol. This differs from .gnu.warning sections, which generate
4080 warnings when they are included in an output file. */
dd98f8d2 4081 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4082 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4083 {
db6a5d5f 4084 const char *name;
4ad4eba5 4085
fd361982 4086 name = bfd_section_name (s);
db6a5d5f 4087 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4088 {
db6a5d5f
AM
4089 char *msg;
4090 bfd_size_type sz;
4091
4092 name += sizeof ".gnu.warning." - 1;
4093
4094 /* If this is a shared object, then look up the symbol
4095 in the hash table. If it is there, and it is already
4096 been defined, then we will not be using the entry
4097 from this shared object, so we don't need to warn.
4098 FIXME: If we see the definition in a regular object
4099 later on, we will warn, but we shouldn't. The only
4100 fix is to keep track of what warnings we are supposed
4101 to emit, and then handle them all at the end of the
4102 link. */
4103 if (dynamic)
4ad4eba5 4104 {
db6a5d5f
AM
4105 struct elf_link_hash_entry *h;
4106
4107 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4108
4109 /* FIXME: What about bfd_link_hash_common? */
4110 if (h != NULL
4111 && (h->root.type == bfd_link_hash_defined
4112 || h->root.type == bfd_link_hash_defweak))
4113 continue;
4114 }
4ad4eba5 4115
db6a5d5f
AM
4116 sz = s->size;
4117 msg = (char *) bfd_alloc (abfd, sz + 1);
4118 if (msg == NULL)
4119 goto error_return;
4ad4eba5 4120
db6a5d5f
AM
4121 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4122 goto error_return;
4ad4eba5 4123
db6a5d5f 4124 msg[sz] = '\0';
4ad4eba5 4125
db6a5d5f
AM
4126 if (! (_bfd_generic_link_add_one_symbol
4127 (info, abfd, name, BSF_WARNING, s, 0, msg,
4128 FALSE, bed->collect, NULL)))
4129 goto error_return;
4ad4eba5 4130
0e1862bb 4131 if (bfd_link_executable (info))
db6a5d5f
AM
4132 {
4133 /* Clobber the section size so that the warning does
4134 not get copied into the output file. */
4135 s->size = 0;
11d2f718 4136
db6a5d5f
AM
4137 /* Also set SEC_EXCLUDE, so that symbols defined in
4138 the warning section don't get copied to the output. */
4139 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4140 }
4141 }
4142 }
4143
29a9f53e
L
4144 just_syms = ((s = abfd->sections) != NULL
4145 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4146
4ad4eba5
AM
4147 add_needed = TRUE;
4148 if (! dynamic)
4149 {
4150 /* If we are creating a shared library, create all the dynamic
4151 sections immediately. We need to attach them to something,
4152 so we attach them to this BFD, provided it is the right
bf89386a
L
4153 format and is not from ld --just-symbols. Always create the
4154 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4155 are no input BFD's of the same format as the output, we can't
4156 make a shared library. */
4157 if (!just_syms
bf89386a 4158 && (bfd_link_pic (info)
9c1d7a08 4159 || (!bfd_link_relocatable (info)
3c5fce9b 4160 && info->nointerp
9c1d7a08 4161 && (info->export_dynamic || info->dynamic)))
66eb6687 4162 && is_elf_hash_table (htab)
f13a99db 4163 && info->output_bfd->xvec == abfd->xvec
66eb6687 4164 && !htab->dynamic_sections_created)
4ad4eba5
AM
4165 {
4166 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4167 goto error_return;
4168 }
4169 }
66eb6687 4170 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4171 goto error_return;
4172 else
4173 {
4ad4eba5 4174 const char *soname = NULL;
7ee314fa 4175 char *audit = NULL;
4ad4eba5 4176 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4177 const Elf_Internal_Phdr *phdr;
e310298c 4178 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4179
4180 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4181 ld shouldn't allow it. */
29a9f53e 4182 if (just_syms)
92fd189d 4183 abort ();
4ad4eba5
AM
4184
4185 /* If this dynamic lib was specified on the command line with
4186 --as-needed in effect, then we don't want to add a DT_NEEDED
4187 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4188 in by another lib's DT_NEEDED. When --no-add-needed is used
4189 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4190 any dynamic library in DT_NEEDED tags in the dynamic lib at
4191 all. */
4192 add_needed = (elf_dyn_lib_class (abfd)
4193 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4194 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4195
4196 s = bfd_get_section_by_name (abfd, ".dynamic");
4197 if (s != NULL)
4198 {
4199 bfd_byte *dynbuf;
4200 bfd_byte *extdyn;
cb33740c 4201 unsigned int elfsec;
4ad4eba5
AM
4202 unsigned long shlink;
4203
eea6121a 4204 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4205 {
dc1e8a47 4206 error_free_dyn:
f8703194
L
4207 free (dynbuf);
4208 goto error_return;
4209 }
4ad4eba5
AM
4210
4211 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4212 if (elfsec == SHN_BAD)
4ad4eba5
AM
4213 goto error_free_dyn;
4214 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4215
4216 for (extdyn = dynbuf;
9bff840e 4217 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4218 extdyn += bed->s->sizeof_dyn)
4219 {
4220 Elf_Internal_Dyn dyn;
4221
4222 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4223 if (dyn.d_tag == DT_SONAME)
4224 {
4225 unsigned int tagv = dyn.d_un.d_val;
4226 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4227 if (soname == NULL)
4228 goto error_free_dyn;
4229 }
4230 if (dyn.d_tag == DT_NEEDED)
4231 {
4232 struct bfd_link_needed_list *n, **pn;
4233 char *fnm, *anm;
4234 unsigned int tagv = dyn.d_un.d_val;
986f0783 4235 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4236
a50b1753 4237 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4238 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4239 if (n == NULL || fnm == NULL)
4240 goto error_free_dyn;
4241 amt = strlen (fnm) + 1;
a50b1753 4242 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4243 if (anm == NULL)
4244 goto error_free_dyn;
4245 memcpy (anm, fnm, amt);
4246 n->name = anm;
4247 n->by = abfd;
4248 n->next = NULL;
66eb6687 4249 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4250 ;
4251 *pn = n;
4252 }
4253 if (dyn.d_tag == DT_RUNPATH)
4254 {
4255 struct bfd_link_needed_list *n, **pn;
4256 char *fnm, *anm;
4257 unsigned int tagv = dyn.d_un.d_val;
986f0783 4258 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4259
a50b1753 4260 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4261 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4262 if (n == NULL || fnm == NULL)
4263 goto error_free_dyn;
4264 amt = strlen (fnm) + 1;
a50b1753 4265 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4266 if (anm == NULL)
4267 goto error_free_dyn;
4268 memcpy (anm, fnm, amt);
4269 n->name = anm;
4270 n->by = abfd;
4271 n->next = NULL;
4272 for (pn = & runpath;
4273 *pn != NULL;
4274 pn = &(*pn)->next)
4275 ;
4276 *pn = n;
4277 }
4278 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4279 if (!runpath && dyn.d_tag == DT_RPATH)
4280 {
4281 struct bfd_link_needed_list *n, **pn;
4282 char *fnm, *anm;
4283 unsigned int tagv = dyn.d_un.d_val;
986f0783 4284 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4285
a50b1753 4286 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4287 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4288 if (n == NULL || fnm == NULL)
4289 goto error_free_dyn;
4290 amt = strlen (fnm) + 1;
a50b1753 4291 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4292 if (anm == NULL)
f8703194 4293 goto error_free_dyn;
4ad4eba5
AM
4294 memcpy (anm, fnm, amt);
4295 n->name = anm;
4296 n->by = abfd;
4297 n->next = NULL;
4298 for (pn = & rpath;
4299 *pn != NULL;
4300 pn = &(*pn)->next)
4301 ;
4302 *pn = n;
4303 }
7ee314fa
AM
4304 if (dyn.d_tag == DT_AUDIT)
4305 {
4306 unsigned int tagv = dyn.d_un.d_val;
4307 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4308 }
4ad4eba5
AM
4309 }
4310
4311 free (dynbuf);
4312 }
4313
4314 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4315 frees all more recently bfd_alloc'd blocks as well. */
4316 if (runpath)
4317 rpath = runpath;
4318
4319 if (rpath)
4320 {
4321 struct bfd_link_needed_list **pn;
66eb6687 4322 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4323 ;
4324 *pn = rpath;
4325 }
4326
9acc85a6
AM
4327 /* If we have a PT_GNU_RELRO program header, mark as read-only
4328 all sections contained fully therein. This makes relro
4329 shared library sections appear as they will at run-time. */
4330 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4331 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4332 if (phdr->p_type == PT_GNU_RELRO)
4333 {
4334 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4335 {
4336 unsigned int opb = bfd_octets_per_byte (abfd, s);
4337
4338 if ((s->flags & SEC_ALLOC) != 0
4339 && s->vma * opb >= phdr->p_vaddr
4340 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4341 s->flags |= SEC_READONLY;
4342 }
9acc85a6
AM
4343 break;
4344 }
4345
4ad4eba5
AM
4346 /* We do not want to include any of the sections in a dynamic
4347 object in the output file. We hack by simply clobbering the
4348 list of sections in the BFD. This could be handled more
4349 cleanly by, say, a new section flag; the existing
4350 SEC_NEVER_LOAD flag is not the one we want, because that one
4351 still implies that the section takes up space in the output
4352 file. */
4353 bfd_section_list_clear (abfd);
4354
4ad4eba5
AM
4355 /* Find the name to use in a DT_NEEDED entry that refers to this
4356 object. If the object has a DT_SONAME entry, we use it.
4357 Otherwise, if the generic linker stuck something in
4358 elf_dt_name, we use that. Otherwise, we just use the file
4359 name. */
4360 if (soname == NULL || *soname == '\0')
4361 {
4362 soname = elf_dt_name (abfd);
4363 if (soname == NULL || *soname == '\0')
4364 soname = bfd_get_filename (abfd);
4365 }
4366
4367 /* Save the SONAME because sometimes the linker emulation code
4368 will need to know it. */
4369 elf_dt_name (abfd) = soname;
4370
4ad4eba5
AM
4371 /* If we have already included this dynamic object in the
4372 link, just ignore it. There is no reason to include a
4373 particular dynamic object more than once. */
e310298c
AM
4374 for (loaded_lib = htab->dyn_loaded;
4375 loaded_lib != NULL;
4376 loaded_lib = loaded_lib->next)
4377 {
4378 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4379 return TRUE;
4380 }
4381
4382 /* Create dynamic sections for backends that require that be done
4383 before setup_gnu_properties. */
4384 if (add_needed
4385 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4386 return FALSE;
7ee314fa
AM
4387
4388 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4389 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4390 }
4391
4392 /* If this is a dynamic object, we always link against the .dynsym
4393 symbol table, not the .symtab symbol table. The dynamic linker
4394 will only see the .dynsym symbol table, so there is no reason to
4395 look at .symtab for a dynamic object. */
4396
4397 if (! dynamic || elf_dynsymtab (abfd) == 0)
4398 hdr = &elf_tdata (abfd)->symtab_hdr;
4399 else
4400 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4401
4402 symcount = hdr->sh_size / bed->s->sizeof_sym;
4403
4404 /* The sh_info field of the symtab header tells us where the
4405 external symbols start. We don't care about the local symbols at
4406 this point. */
4407 if (elf_bad_symtab (abfd))
4408 {
4409 extsymcount = symcount;
4410 extsymoff = 0;
4411 }
4412 else
4413 {
4414 extsymcount = symcount - hdr->sh_info;
4415 extsymoff = hdr->sh_info;
4416 }
4417
f45794cb 4418 sym_hash = elf_sym_hashes (abfd);
012b2306 4419 if (extsymcount != 0)
4ad4eba5
AM
4420 {
4421 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4422 NULL, NULL, NULL);
4423 if (isymbuf == NULL)
4424 goto error_return;
4425
4ad4eba5 4426 if (sym_hash == NULL)
012b2306
AM
4427 {
4428 /* We store a pointer to the hash table entry for each
4429 external symbol. */
986f0783 4430 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4431 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4432 if (sym_hash == NULL)
4433 goto error_free_sym;
4434 elf_sym_hashes (abfd) = sym_hash;
4435 }
4ad4eba5
AM
4436 }
4437
4438 if (dynamic)
4439 {
4440 /* Read in any version definitions. */
fc0e6df6
PB
4441 if (!_bfd_elf_slurp_version_tables (abfd,
4442 info->default_imported_symver))
4ad4eba5
AM
4443 goto error_free_sym;
4444
4445 /* Read in the symbol versions, but don't bother to convert them
4446 to internal format. */
4447 if (elf_dynversym (abfd) != 0)
4448 {
986f0783
AM
4449 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4450 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4451
2bb3687b
AM
4452 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4453 goto error_free_sym;
4454 extversym = (Elf_External_Versym *)
4455 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4456 if (extversym == NULL)
4457 goto error_free_sym;
986f0783 4458 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4459 }
4460 }
4461
66eb6687
AM
4462 /* If we are loading an as-needed shared lib, save the symbol table
4463 state before we start adding symbols. If the lib turns out
4464 to be unneeded, restore the state. */
4465 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4466 {
4467 unsigned int i;
4468 size_t entsize;
4469
4470 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4471 {
4472 struct bfd_hash_entry *p;
2de92251 4473 struct elf_link_hash_entry *h;
66eb6687
AM
4474
4475 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4476 {
4477 h = (struct elf_link_hash_entry *) p;
4478 entsize += htab->root.table.entsize;
4479 if (h->root.type == bfd_link_hash_warning)
4480 entsize += htab->root.table.entsize;
4481 }
66eb6687
AM
4482 }
4483
4484 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4485 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4486 if (old_tab == NULL)
4487 goto error_free_vers;
4488
4489 /* Remember the current objalloc pointer, so that all mem for
4490 symbols added can later be reclaimed. */
4491 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4492 if (alloc_mark == NULL)
4493 goto error_free_vers;
4494
5061a885
AM
4495 /* Make a special call to the linker "notice" function to
4496 tell it that we are about to handle an as-needed lib. */
e5034e59 4497 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4498 goto error_free_vers;
5061a885 4499
f45794cb
AM
4500 /* Clone the symbol table. Remember some pointers into the
4501 symbol table, and dynamic symbol count. */
4502 old_ent = (char *) old_tab + tabsize;
66eb6687 4503 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4504 old_undefs = htab->root.undefs;
4505 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4506 old_table = htab->root.table.table;
4507 old_size = htab->root.table.size;
4508 old_count = htab->root.table.count;
e310298c
AM
4509 old_strtab = NULL;
4510 if (htab->dynstr != NULL)
4511 {
4512 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4513 if (old_strtab == NULL)
4514 goto error_free_vers;
4515 }
66eb6687
AM
4516
4517 for (i = 0; i < htab->root.table.size; i++)
4518 {
4519 struct bfd_hash_entry *p;
2de92251 4520 struct elf_link_hash_entry *h;
66eb6687
AM
4521
4522 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4523 {
4524 memcpy (old_ent, p, htab->root.table.entsize);
4525 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4526 h = (struct elf_link_hash_entry *) p;
4527 if (h->root.type == bfd_link_hash_warning)
4528 {
4529 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4530 old_ent = (char *) old_ent + htab->root.table.entsize;
4531 }
66eb6687
AM
4532 }
4533 }
4534 }
4ad4eba5 4535
66eb6687 4536 weaks = NULL;
be22c732
NC
4537 if (extversym == NULL)
4538 ever = NULL;
4539 else if (extversym + extsymoff < extversym_end)
4540 ever = extversym + extsymoff;
4541 else
4542 {
4543 /* xgettext:c-format */
4544 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4545 abfd, (long) extsymoff,
4546 (long) (extversym_end - extversym) / sizeof (* extversym));
4547 bfd_set_error (bfd_error_bad_value);
4548 goto error_free_vers;
4549 }
4550
b4c555cf
ML
4551 if (!bfd_link_relocatable (info)
4552 && abfd->lto_slim_object)
cc5277b1
ML
4553 {
4554 _bfd_error_handler
4555 (_("%pB: plugin needed to handle lto object"), abfd);
4556 }
4557
4ad4eba5
AM
4558 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4559 isym < isymend;
4560 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4561 {
4562 int bind;
4563 bfd_vma value;
af44c138 4564 asection *sec, *new_sec;
4ad4eba5
AM
4565 flagword flags;
4566 const char *name;
4567 struct elf_link_hash_entry *h;
90c984fc 4568 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4569 bfd_boolean definition;
4570 bfd_boolean size_change_ok;
4571 bfd_boolean type_change_ok;
37a9e49a
L
4572 bfd_boolean new_weak;
4573 bfd_boolean old_weak;
4ad4eba5 4574 bfd_boolean override;
a4d8e49b 4575 bfd_boolean common;
97196564 4576 bfd_boolean discarded;
4ad4eba5 4577 unsigned int old_alignment;
4538d1c7 4578 unsigned int shindex;
4ad4eba5 4579 bfd *old_bfd;
6e33951e 4580 bfd_boolean matched;
4ad4eba5
AM
4581
4582 override = FALSE;
4583
4584 flags = BSF_NO_FLAGS;
4585 sec = NULL;
4586 value = isym->st_value;
a4d8e49b 4587 common = bed->common_definition (isym);
2980ccad
L
4588 if (common && info->inhibit_common_definition)
4589 {
4590 /* Treat common symbol as undefined for --no-define-common. */
4591 isym->st_shndx = SHN_UNDEF;
4592 common = FALSE;
4593 }
97196564 4594 discarded = FALSE;
4ad4eba5
AM
4595
4596 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4597 switch (bind)
4ad4eba5 4598 {
3e7a7d11 4599 case STB_LOCAL:
4ad4eba5
AM
4600 /* This should be impossible, since ELF requires that all
4601 global symbols follow all local symbols, and that sh_info
4602 point to the first global symbol. Unfortunately, Irix 5
4603 screws this up. */
fe3fef62
AM
4604 if (elf_bad_symtab (abfd))
4605 continue;
4606
4607 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4608 then we'll likely segfault on a NULL symbol hash if the
4609 symbol is ever referenced in relocations. */
4610 shindex = elf_elfheader (abfd)->e_shstrndx;
4611 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4612 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4613 " (>= sh_info of %lu)"),
4614 abfd, name, (long) (isym - isymbuf + extsymoff),
4615 (long) extsymoff);
4616
4617 /* Dynamic object relocations are not processed by ld, so
4618 ld won't run into the problem mentioned above. */
4619 if (dynamic)
4620 continue;
fe3fef62
AM
4621 bfd_set_error (bfd_error_bad_value);
4622 goto error_free_vers;
3e7a7d11
NC
4623
4624 case STB_GLOBAL:
a4d8e49b 4625 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4626 flags = BSF_GLOBAL;
3e7a7d11
NC
4627 break;
4628
4629 case STB_WEAK:
4630 flags = BSF_WEAK;
4631 break;
4632
4633 case STB_GNU_UNIQUE:
4634 flags = BSF_GNU_UNIQUE;
4635 break;
4636
4637 default:
4ad4eba5 4638 /* Leave it up to the processor backend. */
3e7a7d11 4639 break;
4ad4eba5
AM
4640 }
4641
4642 if (isym->st_shndx == SHN_UNDEF)
4643 sec = bfd_und_section_ptr;
cb33740c
AM
4644 else if (isym->st_shndx == SHN_ABS)
4645 sec = bfd_abs_section_ptr;
4646 else if (isym->st_shndx == SHN_COMMON)
4647 {
4648 sec = bfd_com_section_ptr;
4649 /* What ELF calls the size we call the value. What ELF
4650 calls the value we call the alignment. */
4651 value = isym->st_size;
4652 }
4653 else
4ad4eba5
AM
4654 {
4655 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4656 if (sec == NULL)
4657 sec = bfd_abs_section_ptr;
dbaa2011 4658 else if (discarded_section (sec))
529fcb95 4659 {
e5d08002
L
4660 /* Symbols from discarded section are undefined. We keep
4661 its visibility. */
529fcb95 4662 sec = bfd_und_section_ptr;
97196564 4663 discarded = TRUE;
529fcb95
PB
4664 isym->st_shndx = SHN_UNDEF;
4665 }
4ad4eba5
AM
4666 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4667 value -= sec->vma;
4668 }
4ad4eba5
AM
4669
4670 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4671 isym->st_name);
4672 if (name == NULL)
4673 goto error_free_vers;
4674
4675 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4676 && (abfd->flags & BFD_PLUGIN) != 0)
4677 {
4678 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4679
4680 if (xc == NULL)
4681 {
4682 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4683 | SEC_EXCLUDE);
4684 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4685 if (xc == NULL)
4686 goto error_free_vers;
4687 }
4688 sec = xc;
4689 }
4690 else if (isym->st_shndx == SHN_COMMON
4691 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4692 && !bfd_link_relocatable (info))
4ad4eba5
AM
4693 {
4694 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4695
4696 if (tcomm == NULL)
4697 {
02d00247
AM
4698 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4699 | SEC_LINKER_CREATED);
4700 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4701 if (tcomm == NULL)
4ad4eba5
AM
4702 goto error_free_vers;
4703 }
4704 sec = tcomm;
4705 }
66eb6687 4706 else if (bed->elf_add_symbol_hook)
4ad4eba5 4707 {
66eb6687
AM
4708 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4709 &sec, &value))
4ad4eba5
AM
4710 goto error_free_vers;
4711
4712 /* The hook function sets the name to NULL if this symbol
4713 should be skipped for some reason. */
4714 if (name == NULL)
4715 continue;
4716 }
4717
4718 /* Sanity check that all possibilities were handled. */
4719 if (sec == NULL)
4538d1c7 4720 abort ();
4ad4eba5 4721
191c0c42
AM
4722 /* Silently discard TLS symbols from --just-syms. There's
4723 no way to combine a static TLS block with a new TLS block
4724 for this executable. */
4725 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4726 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4727 continue;
4728
4ad4eba5
AM
4729 if (bfd_is_und_section (sec)
4730 || bfd_is_com_section (sec))
4731 definition = FALSE;
4732 else
4733 definition = TRUE;
4734
4735 size_change_ok = FALSE;
66eb6687 4736 type_change_ok = bed->type_change_ok;
37a9e49a 4737 old_weak = FALSE;
6e33951e 4738 matched = FALSE;
4ad4eba5
AM
4739 old_alignment = 0;
4740 old_bfd = NULL;
af44c138 4741 new_sec = sec;
4ad4eba5 4742
66eb6687 4743 if (is_elf_hash_table (htab))
4ad4eba5
AM
4744 {
4745 Elf_Internal_Versym iver;
4746 unsigned int vernum = 0;
4747 bfd_boolean skip;
4748
fc0e6df6 4749 if (ever == NULL)
4ad4eba5 4750 {
fc0e6df6
PB
4751 if (info->default_imported_symver)
4752 /* Use the default symbol version created earlier. */
4753 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4754 else
4755 iver.vs_vers = 0;
4756 }
be22c732
NC
4757 else if (ever >= extversym_end)
4758 {
4759 /* xgettext:c-format */
4760 _bfd_error_handler (_("%pB: not enough version information"),
4761 abfd);
4762 bfd_set_error (bfd_error_bad_value);
4763 goto error_free_vers;
4764 }
fc0e6df6
PB
4765 else
4766 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4767
4768 vernum = iver.vs_vers & VERSYM_VERSION;
4769
4770 /* If this is a hidden symbol, or if it is not version
4771 1, we append the version name to the symbol name.
cc86ff91
EB
4772 However, we do not modify a non-hidden absolute symbol
4773 if it is not a function, because it might be the version
4774 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4775 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4776 || (vernum > 1
4777 && (!bfd_is_abs_section (sec)
4778 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4779 {
4780 const char *verstr;
4781 size_t namelen, verlen, newlen;
4782 char *newname, *p;
4783
4784 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4785 {
fc0e6df6
PB
4786 if (vernum > elf_tdata (abfd)->cverdefs)
4787 verstr = NULL;
4788 else if (vernum > 1)
4789 verstr =
4790 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4791 else
4792 verstr = "";
4ad4eba5 4793
fc0e6df6 4794 if (verstr == NULL)
4ad4eba5 4795 {
4eca0228 4796 _bfd_error_handler
695344c0 4797 /* xgettext:c-format */
871b3ab2 4798 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4799 abfd, name, vernum,
4800 elf_tdata (abfd)->cverdefs);
4801 bfd_set_error (bfd_error_bad_value);
4802 goto error_free_vers;
4ad4eba5 4803 }
fc0e6df6
PB
4804 }
4805 else
4806 {
4807 /* We cannot simply test for the number of
4808 entries in the VERNEED section since the
4809 numbers for the needed versions do not start
4810 at 0. */
4811 Elf_Internal_Verneed *t;
4812
4813 verstr = NULL;
4814 for (t = elf_tdata (abfd)->verref;
4815 t != NULL;
4816 t = t->vn_nextref)
4ad4eba5 4817 {
fc0e6df6 4818 Elf_Internal_Vernaux *a;
4ad4eba5 4819
fc0e6df6
PB
4820 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4821 {
4822 if (a->vna_other == vernum)
4ad4eba5 4823 {
fc0e6df6
PB
4824 verstr = a->vna_nodename;
4825 break;
4ad4eba5 4826 }
4ad4eba5 4827 }
fc0e6df6
PB
4828 if (a != NULL)
4829 break;
4830 }
4831 if (verstr == NULL)
4832 {
4eca0228 4833 _bfd_error_handler
695344c0 4834 /* xgettext:c-format */
871b3ab2 4835 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4836 abfd, name, vernum);
4837 bfd_set_error (bfd_error_bad_value);
4838 goto error_free_vers;
4ad4eba5 4839 }
4ad4eba5 4840 }
fc0e6df6
PB
4841
4842 namelen = strlen (name);
4843 verlen = strlen (verstr);
4844 newlen = namelen + verlen + 2;
4845 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4846 && isym->st_shndx != SHN_UNDEF)
4847 ++newlen;
4848
a50b1753 4849 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4850 if (newname == NULL)
4851 goto error_free_vers;
4852 memcpy (newname, name, namelen);
4853 p = newname + namelen;
4854 *p++ = ELF_VER_CHR;
4855 /* If this is a defined non-hidden version symbol,
4856 we add another @ to the name. This indicates the
4857 default version of the symbol. */
4858 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4859 && isym->st_shndx != SHN_UNDEF)
4860 *p++ = ELF_VER_CHR;
4861 memcpy (p, verstr, verlen + 1);
4862
4863 name = newname;
4ad4eba5
AM
4864 }
4865
cd3416da
AM
4866 /* If this symbol has default visibility and the user has
4867 requested we not re-export it, then mark it as hidden. */
a0d49154 4868 if (!bfd_is_und_section (sec)
cd3416da 4869 && !dynamic
ce875075 4870 && abfd->no_export
cd3416da
AM
4871 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4872 isym->st_other = (STV_HIDDEN
4873 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4874
4f3fedcf
AM
4875 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4876 sym_hash, &old_bfd, &old_weak,
4877 &old_alignment, &skip, &override,
6e33951e
L
4878 &type_change_ok, &size_change_ok,
4879 &matched))
4ad4eba5
AM
4880 goto error_free_vers;
4881
4882 if (skip)
4883 continue;
4884
6e33951e
L
4885 /* Override a definition only if the new symbol matches the
4886 existing one. */
4887 if (override && matched)
4ad4eba5
AM
4888 definition = FALSE;
4889
4890 h = *sym_hash;
4891 while (h->root.type == bfd_link_hash_indirect
4892 || h->root.type == bfd_link_hash_warning)
4893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4894
4ad4eba5 4895 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4896 && vernum > 1
4897 && definition)
4898 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4899 }
4900
4901 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4902 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4903 (struct bfd_link_hash_entry **) sym_hash)))
4904 goto error_free_vers;
4905
4906 h = *sym_hash;
90c984fc
L
4907 /* We need to make sure that indirect symbol dynamic flags are
4908 updated. */
4909 hi = h;
4ad4eba5
AM
4910 while (h->root.type == bfd_link_hash_indirect
4911 || h->root.type == bfd_link_hash_warning)
4912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4913
97196564
L
4914 /* Setting the index to -3 tells elf_link_output_extsym that
4915 this symbol is defined in a discarded section. */
4916 if (discarded)
4917 h->indx = -3;
4918
4ad4eba5
AM
4919 *sym_hash = h;
4920
37a9e49a 4921 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4922 if (dynamic
4923 && definition
37a9e49a 4924 && new_weak
fcb93ecf 4925 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4926 && is_elf_hash_table (htab)
60d67dc8 4927 && h->u.alias == NULL)
4ad4eba5
AM
4928 {
4929 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4930 a dynamic object, using the alias field. Later in this
4931 function we will set the alias field to the correct
4ad4eba5
AM
4932 value. We only put non-function symbols from dynamic
4933 objects on this list, because that happens to be the only
4934 time we need to know the normal symbol corresponding to a
4935 weak symbol, and the information is time consuming to
60d67dc8 4936 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4937 then this symbol was already defined by some previous
4938 dynamic object, and we will be using that previous
4939 definition anyhow. */
4940
60d67dc8 4941 h->u.alias = weaks;
4ad4eba5 4942 weaks = h;
4ad4eba5
AM
4943 }
4944
4945 /* Set the alignment of a common symbol. */
a4d8e49b 4946 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4947 && h->root.type == bfd_link_hash_common)
4948 {
4949 unsigned int align;
4950
a4d8e49b 4951 if (common)
af44c138
L
4952 align = bfd_log2 (isym->st_value);
4953 else
4954 {
4955 /* The new symbol is a common symbol in a shared object.
4956 We need to get the alignment from the section. */
4957 align = new_sec->alignment_power;
4958 }
595213d4 4959 if (align > old_alignment)
4ad4eba5
AM
4960 h->root.u.c.p->alignment_power = align;
4961 else
4962 h->root.u.c.p->alignment_power = old_alignment;
4963 }
4964
66eb6687 4965 if (is_elf_hash_table (htab))
4ad4eba5 4966 {
4f3fedcf
AM
4967 /* Set a flag in the hash table entry indicating the type of
4968 reference or definition we just found. A dynamic symbol
4969 is one which is referenced or defined by both a regular
4970 object and a shared object. */
4971 bfd_boolean dynsym = FALSE;
4972
4973 /* Plugin symbols aren't normal. Don't set def_regular or
4974 ref_regular for them, or make them dynamic. */
4975 if ((abfd->flags & BFD_PLUGIN) != 0)
4976 ;
4977 else if (! dynamic)
4978 {
4979 if (! definition)
4980 {
4981 h->ref_regular = 1;
4982 if (bind != STB_WEAK)
4983 h->ref_regular_nonweak = 1;
4984 }
4985 else
4986 {
4987 h->def_regular = 1;
4988 if (h->def_dynamic)
4989 {
4990 h->def_dynamic = 0;
4991 h->ref_dynamic = 1;
4992 }
4993 }
4994
4995 /* If the indirect symbol has been forced local, don't
4996 make the real symbol dynamic. */
4997 if ((h == hi || !hi->forced_local)
0e1862bb 4998 && (bfd_link_dll (info)
4f3fedcf
AM
4999 || h->def_dynamic
5000 || h->ref_dynamic))
5001 dynsym = TRUE;
5002 }
5003 else
5004 {
5005 if (! definition)
5006 {
5007 h->ref_dynamic = 1;
5008 hi->ref_dynamic = 1;
5009 }
5010 else
5011 {
5012 h->def_dynamic = 1;
5013 hi->def_dynamic = 1;
5014 }
5015
5016 /* If the indirect symbol has been forced local, don't
5017 make the real symbol dynamic. */
5018 if ((h == hi || !hi->forced_local)
5019 && (h->def_regular
5020 || h->ref_regular
60d67dc8
AM
5021 || (h->is_weakalias
5022 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
5023 dynsym = TRUE;
5024 }
5025
5026 /* Check to see if we need to add an indirect symbol for
5027 the default name. */
5028 if (definition
5029 || (!override && h->root.type == bfd_link_hash_common))
5030 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5031 sec, value, &old_bfd, &dynsym))
5032 goto error_free_vers;
4ad4eba5
AM
5033
5034 /* Check the alignment when a common symbol is involved. This
5035 can change when a common symbol is overridden by a normal
5036 definition or a common symbol is ignored due to the old
5037 normal definition. We need to make sure the maximum
5038 alignment is maintained. */
a4d8e49b 5039 if ((old_alignment || common)
4ad4eba5
AM
5040 && h->root.type != bfd_link_hash_common)
5041 {
5042 unsigned int common_align;
5043 unsigned int normal_align;
5044 unsigned int symbol_align;
5045 bfd *normal_bfd;
5046 bfd *common_bfd;
5047
3a81e825
AM
5048 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5049 || h->root.type == bfd_link_hash_defweak);
5050
4ad4eba5
AM
5051 symbol_align = ffs (h->root.u.def.value) - 1;
5052 if (h->root.u.def.section->owner != NULL
0616a280
AM
5053 && (h->root.u.def.section->owner->flags
5054 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5055 {
5056 normal_align = h->root.u.def.section->alignment_power;
5057 if (normal_align > symbol_align)
5058 normal_align = symbol_align;
5059 }
5060 else
5061 normal_align = symbol_align;
5062
5063 if (old_alignment)
5064 {
5065 common_align = old_alignment;
5066 common_bfd = old_bfd;
5067 normal_bfd = abfd;
5068 }
5069 else
5070 {
5071 common_align = bfd_log2 (isym->st_value);
5072 common_bfd = abfd;
5073 normal_bfd = old_bfd;
5074 }
5075
5076 if (normal_align < common_align)
d07676f8
NC
5077 {
5078 /* PR binutils/2735 */
5079 if (normal_bfd == NULL)
4eca0228 5080 _bfd_error_handler
695344c0 5081 /* xgettext:c-format */
9793eb77 5082 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5083 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5084 1 << common_align, name, common_bfd,
5085 1 << normal_align, h->root.u.def.section);
d07676f8 5086 else
4eca0228 5087 _bfd_error_handler
695344c0 5088 /* xgettext:c-format */
9793eb77 5089 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5090 " is smaller than %u in %pB"),
c08bb8dd
AM
5091 1 << normal_align, name, normal_bfd,
5092 1 << common_align, common_bfd);
d07676f8 5093 }
4ad4eba5
AM
5094 }
5095
83ad0046 5096 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5097 if (isym->st_size != 0
5098 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5099 && (definition || h->size == 0))
5100 {
83ad0046
L
5101 if (h->size != 0
5102 && h->size != isym->st_size
5103 && ! size_change_ok)
4eca0228 5104 _bfd_error_handler
695344c0 5105 /* xgettext:c-format */
9793eb77 5106 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5107 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5108 name, (uint64_t) h->size, old_bfd,
5109 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5110
5111 h->size = isym->st_size;
5112 }
5113
5114 /* If this is a common symbol, then we always want H->SIZE
5115 to be the size of the common symbol. The code just above
5116 won't fix the size if a common symbol becomes larger. We
5117 don't warn about a size change here, because that is
4f3fedcf 5118 covered by --warn-common. Allow changes between different
fcb93ecf 5119 function types. */
4ad4eba5
AM
5120 if (h->root.type == bfd_link_hash_common)
5121 h->size = h->root.u.c.size;
5122
5123 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5124 && ((definition && !new_weak)
5125 || (old_weak && h->root.type == bfd_link_hash_common)
5126 || h->type == STT_NOTYPE))
4ad4eba5 5127 {
2955ec4c
L
5128 unsigned int type = ELF_ST_TYPE (isym->st_info);
5129
5130 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5131 symbol. */
5132 if (type == STT_GNU_IFUNC
5133 && (abfd->flags & DYNAMIC) != 0)
5134 type = STT_FUNC;
4ad4eba5 5135
2955ec4c
L
5136 if (h->type != type)
5137 {
5138 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5139 /* xgettext:c-format */
4eca0228 5140 _bfd_error_handler
9793eb77 5141 (_("warning: type of symbol `%s' changed"
871b3ab2 5142 " from %d to %d in %pB"),
c08bb8dd 5143 name, h->type, type, abfd);
2955ec4c
L
5144
5145 h->type = type;
5146 }
4ad4eba5
AM
5147 }
5148
54ac0771 5149 /* Merge st_other field. */
b8417128 5150 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5151
c3df8c14 5152 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5153 if (definition
5154 && (sec->flags & SEC_DEBUGGING)
5155 && !bfd_link_relocatable (info))
c3df8c14
AM
5156 dynsym = FALSE;
5157
4f3fedcf
AM
5158 /* Nor should we make plugin symbols dynamic. */
5159 if ((abfd->flags & BFD_PLUGIN) != 0)
5160 dynsym = FALSE;
5161
35fc36a8 5162 if (definition)
35399224
L
5163 {
5164 h->target_internal = isym->st_target_internal;
5165 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5166 }
35fc36a8 5167
4ad4eba5
AM
5168 if (definition && !dynamic)
5169 {
5170 char *p = strchr (name, ELF_VER_CHR);
5171 if (p != NULL && p[1] != ELF_VER_CHR)
5172 {
5173 /* Queue non-default versions so that .symver x, x@FOO
5174 aliases can be checked. */
66eb6687 5175 if (!nondeflt_vers)
4ad4eba5 5176 {
986f0783
AM
5177 size_t amt = ((isymend - isym + 1)
5178 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5179 nondeflt_vers
5180 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5181 if (!nondeflt_vers)
5182 goto error_free_vers;
4ad4eba5 5183 }
66eb6687 5184 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5185 }
5186 }
5187
5188 if (dynsym && h->dynindx == -1)
5189 {
c152c796 5190 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5191 goto error_free_vers;
60d67dc8
AM
5192 if (h->is_weakalias
5193 && weakdef (h)->dynindx == -1)
4ad4eba5 5194 {
60d67dc8 5195 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5196 goto error_free_vers;
5197 }
5198 }
1f599d0e 5199 else if (h->dynindx != -1)
4ad4eba5
AM
5200 /* If the symbol already has a dynamic index, but
5201 visibility says it should not be visible, turn it into
5202 a local symbol. */
5203 switch (ELF_ST_VISIBILITY (h->other))
5204 {
5205 case STV_INTERNAL:
5206 case STV_HIDDEN:
5207 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5208 dynsym = FALSE;
5209 break;
5210 }
5211
5212 if (!add_needed
aef28989 5213 && matched
4ad4eba5 5214 && definition
010e5ae2 5215 && ((dynsym
a896df97 5216 && h->ref_regular_nonweak)
ffa9430d 5217 || (h->ref_dynamic_nonweak
010e5ae2 5218 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5219 && !on_needed_list (elf_dt_name (abfd),
5220 htab->needed, NULL))))
4ad4eba5 5221 {
4ad4eba5
AM
5222 const char *soname = elf_dt_name (abfd);
5223
16e4ecc0
AM
5224 info->callbacks->minfo ("%!", soname, old_bfd,
5225 h->root.root.string);
5226
4ad4eba5
AM
5227 /* A symbol from a library loaded via DT_NEEDED of some
5228 other library is referenced by a regular object.
e56f61be 5229 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5230 --no-add-needed is used and the reference was not
5231 a weak one. */
4f3fedcf 5232 if (old_bfd != NULL
b918acf9 5233 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5234 {
4eca0228 5235 _bfd_error_handler
695344c0 5236 /* xgettext:c-format */
871b3ab2 5237 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5238 old_bfd, name);
ff5ac77b 5239 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5240 goto error_free_vers;
5241 }
5242
a50b1753 5243 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5244 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5245
e310298c
AM
5246 /* Create dynamic sections for backends that require
5247 that be done before setup_gnu_properties. */
5248 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5249 return FALSE;
4ad4eba5 5250 add_needed = TRUE;
4ad4eba5
AM
5251 }
5252 }
5253 }
5254
a83ef4d1
L
5255 if (info->lto_plugin_active
5256 && !bfd_link_relocatable (info)
5257 && (abfd->flags & BFD_PLUGIN) == 0
5258 && !just_syms
5259 && extsymcount)
5260 {
5261 int r_sym_shift;
5262
5263 if (bed->s->arch_size == 32)
5264 r_sym_shift = 8;
5265 else
5266 r_sym_shift = 32;
5267
5268 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5269 referenced in regular objects so that linker plugin will get
5270 the correct symbol resolution. */
5271
5272 sym_hash = elf_sym_hashes (abfd);
5273 for (s = abfd->sections; s != NULL; s = s->next)
5274 {
5275 Elf_Internal_Rela *internal_relocs;
5276 Elf_Internal_Rela *rel, *relend;
5277
5278 /* Don't check relocations in excluded sections. */
5279 if ((s->flags & SEC_RELOC) == 0
5280 || s->reloc_count == 0
5281 || (s->flags & SEC_EXCLUDE) != 0
5282 || ((info->strip == strip_all
5283 || info->strip == strip_debugger)
5284 && (s->flags & SEC_DEBUGGING) != 0))
5285 continue;
5286
5287 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5288 NULL,
5289 info->keep_memory);
5290 if (internal_relocs == NULL)
5291 goto error_free_vers;
5292
5293 rel = internal_relocs;
5294 relend = rel + s->reloc_count;
5295 for ( ; rel < relend; rel++)
5296 {
5297 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5298 struct elf_link_hash_entry *h;
5299
5300 /* Skip local symbols. */
5301 if (r_symndx < extsymoff)
5302 continue;
5303
5304 h = sym_hash[r_symndx - extsymoff];
5305 if (h != NULL)
5306 h->root.non_ir_ref_regular = 1;
5307 }
5308
5309 if (elf_section_data (s)->relocs != internal_relocs)
5310 free (internal_relocs);
5311 }
5312 }
5313
c9594989
AM
5314 free (extversym);
5315 extversym = NULL;
5316 free (isymbuf);
5317 isymbuf = NULL;
66eb6687
AM
5318
5319 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5320 {
5321 unsigned int i;
5322
5323 /* Restore the symbol table. */
f45794cb
AM
5324 old_ent = (char *) old_tab + tabsize;
5325 memset (elf_sym_hashes (abfd), 0,
5326 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5327 htab->root.table.table = old_table;
5328 htab->root.table.size = old_size;
5329 htab->root.table.count = old_count;
66eb6687 5330 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5331 htab->root.undefs = old_undefs;
5332 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5333 if (htab->dynstr != NULL)
5334 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5335 free (old_strtab);
5336 old_strtab = NULL;
66eb6687
AM
5337 for (i = 0; i < htab->root.table.size; i++)
5338 {
5339 struct bfd_hash_entry *p;
5340 struct elf_link_hash_entry *h;
3e0882af
L
5341 bfd_size_type size;
5342 unsigned int alignment_power;
4070765b 5343 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5344
5345 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5346 {
5347 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5348 if (h->root.type == bfd_link_hash_warning)
5349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5350
3e0882af
L
5351 /* Preserve the maximum alignment and size for common
5352 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5353 since it can still be loaded at run time by another
3e0882af
L
5354 dynamic lib. */
5355 if (h->root.type == bfd_link_hash_common)
5356 {
5357 size = h->root.u.c.size;
5358 alignment_power = h->root.u.c.p->alignment_power;
5359 }
5360 else
5361 {
5362 size = 0;
5363 alignment_power = 0;
5364 }
4070765b 5365 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5366 will be exported when the dynamic lib becomes needed
5367 in the second pass. */
4070765b 5368 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5369 memcpy (p, old_ent, htab->root.table.entsize);
5370 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5371 h = (struct elf_link_hash_entry *) p;
5372 if (h->root.type == bfd_link_hash_warning)
5373 {
5374 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5375 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5377 }
a4542f1b 5378 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5379 {
5380 if (size > h->root.u.c.size)
5381 h->root.u.c.size = size;
5382 if (alignment_power > h->root.u.c.p->alignment_power)
5383 h->root.u.c.p->alignment_power = alignment_power;
5384 }
4070765b 5385 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5386 }
5387 }
5388
5061a885
AM
5389 /* Make a special call to the linker "notice" function to
5390 tell it that symbols added for crefs may need to be removed. */
e5034e59 5391 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5392 goto error_free_vers;
5061a885 5393
66eb6687
AM
5394 free (old_tab);
5395 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5396 alloc_mark);
c9594989 5397 free (nondeflt_vers);
66eb6687
AM
5398 return TRUE;
5399 }
2de92251 5400
66eb6687
AM
5401 if (old_tab != NULL)
5402 {
e5034e59 5403 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5404 goto error_free_vers;
66eb6687
AM
5405 free (old_tab);
5406 old_tab = NULL;
5407 }
5408
c6e8a9a8
L
5409 /* Now that all the symbols from this input file are created, if
5410 not performing a relocatable link, handle .symver foo, foo@BAR
5411 such that any relocs against foo become foo@BAR. */
0e1862bb 5412 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5413 {
ef53be89 5414 size_t cnt, symidx;
4ad4eba5
AM
5415
5416 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5417 {
5418 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5419 char *shortname, *p;
986f0783 5420 size_t amt;
4ad4eba5
AM
5421
5422 p = strchr (h->root.root.string, ELF_VER_CHR);
5423 if (p == NULL
5424 || (h->root.type != bfd_link_hash_defined
5425 && h->root.type != bfd_link_hash_defweak))
5426 continue;
5427
5428 amt = p - h->root.root.string;
a50b1753 5429 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5430 if (!shortname)
5431 goto error_free_vers;
4ad4eba5
AM
5432 memcpy (shortname, h->root.root.string, amt);
5433 shortname[amt] = '\0';
5434
5435 hi = (struct elf_link_hash_entry *)
66eb6687 5436 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5437 FALSE, FALSE, FALSE);
5438 if (hi != NULL
5439 && hi->root.type == h->root.type
5440 && hi->root.u.def.value == h->root.u.def.value
5441 && hi->root.u.def.section == h->root.u.def.section)
5442 {
5443 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5444 hi->root.type = bfd_link_hash_indirect;
5445 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5446 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5447 sym_hash = elf_sym_hashes (abfd);
5448 if (sym_hash)
5449 for (symidx = 0; symidx < extsymcount; ++symidx)
5450 if (sym_hash[symidx] == hi)
5451 {
5452 sym_hash[symidx] = h;
5453 break;
5454 }
5455 }
5456 free (shortname);
5457 }
5458 free (nondeflt_vers);
5459 nondeflt_vers = NULL;
5460 }
5461
60d67dc8 5462 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5463 symbols we found. The only way to do this is to search all the
5464 symbols. Since we only need the information for non functions in
5465 dynamic objects, that's the only time we actually put anything on
5466 the list WEAKS. We need this information so that if a regular
5467 object refers to a symbol defined weakly in a dynamic object, the
5468 real symbol in the dynamic object is also put in the dynamic
5469 symbols; we also must arrange for both symbols to point to the
5470 same memory location. We could handle the general case of symbol
5471 aliasing, but a general symbol alias can only be generated in
5472 assembler code, handling it correctly would be very time
5473 consuming, and other ELF linkers don't handle general aliasing
5474 either. */
5475 if (weaks != NULL)
5476 {
5477 struct elf_link_hash_entry **hpp;
5478 struct elf_link_hash_entry **hppend;
5479 struct elf_link_hash_entry **sorted_sym_hash;
5480 struct elf_link_hash_entry *h;
986f0783 5481 size_t sym_count, amt;
4ad4eba5
AM
5482
5483 /* Since we have to search the whole symbol list for each weak
5484 defined symbol, search time for N weak defined symbols will be
5485 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5486 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5487 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5488 if (sorted_sym_hash == NULL)
5489 goto error_return;
5490 sym_hash = sorted_sym_hash;
5491 hpp = elf_sym_hashes (abfd);
5492 hppend = hpp + extsymcount;
5493 sym_count = 0;
5494 for (; hpp < hppend; hpp++)
5495 {
5496 h = *hpp;
5497 if (h != NULL
5498 && h->root.type == bfd_link_hash_defined
fcb93ecf 5499 && !bed->is_function_type (h->type))
4ad4eba5
AM
5500 {
5501 *sym_hash = h;
5502 sym_hash++;
5503 sym_count++;
5504 }
5505 }
5506
3a3f4bf7 5507 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5508 elf_sort_symbol);
5509
5510 while (weaks != NULL)
5511 {
5512 struct elf_link_hash_entry *hlook;
5513 asection *slook;
5514 bfd_vma vlook;
ed54588d 5515 size_t i, j, idx = 0;
4ad4eba5
AM
5516
5517 hlook = weaks;
60d67dc8
AM
5518 weaks = hlook->u.alias;
5519 hlook->u.alias = NULL;
4ad4eba5 5520
e3e53eed
AM
5521 if (hlook->root.type != bfd_link_hash_defined
5522 && hlook->root.type != bfd_link_hash_defweak)
5523 continue;
5524
4ad4eba5
AM
5525 slook = hlook->root.u.def.section;
5526 vlook = hlook->root.u.def.value;
5527
4ad4eba5
AM
5528 i = 0;
5529 j = sym_count;
14160578 5530 while (i != j)
4ad4eba5
AM
5531 {
5532 bfd_signed_vma vdiff;
5533 idx = (i + j) / 2;
14160578 5534 h = sorted_sym_hash[idx];
4ad4eba5
AM
5535 vdiff = vlook - h->root.u.def.value;
5536 if (vdiff < 0)
5537 j = idx;
5538 else if (vdiff > 0)
5539 i = idx + 1;
5540 else
5541 {
d3435ae8 5542 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5543 if (sdiff < 0)
5544 j = idx;
5545 else if (sdiff > 0)
5546 i = idx + 1;
5547 else
14160578 5548 break;
4ad4eba5
AM
5549 }
5550 }
5551
5552 /* We didn't find a value/section match. */
14160578 5553 if (i == j)
4ad4eba5
AM
5554 continue;
5555
14160578
AM
5556 /* With multiple aliases, or when the weak symbol is already
5557 strongly defined, we have multiple matching symbols and
5558 the binary search above may land on any of them. Step
5559 one past the matching symbol(s). */
5560 while (++idx != j)
5561 {
5562 h = sorted_sym_hash[idx];
5563 if (h->root.u.def.section != slook
5564 || h->root.u.def.value != vlook)
5565 break;
5566 }
5567
5568 /* Now look back over the aliases. Since we sorted by size
5569 as well as value and section, we'll choose the one with
5570 the largest size. */
5571 while (idx-- != i)
4ad4eba5 5572 {
14160578 5573 h = sorted_sym_hash[idx];
4ad4eba5
AM
5574
5575 /* Stop if value or section doesn't match. */
14160578
AM
5576 if (h->root.u.def.section != slook
5577 || h->root.u.def.value != vlook)
4ad4eba5
AM
5578 break;
5579 else if (h != hlook)
5580 {
60d67dc8
AM
5581 struct elf_link_hash_entry *t;
5582
5583 hlook->u.alias = h;
5584 hlook->is_weakalias = 1;
5585 t = h;
5586 if (t->u.alias != NULL)
5587 while (t->u.alias != h)
5588 t = t->u.alias;
5589 t->u.alias = hlook;
4ad4eba5
AM
5590
5591 /* If the weak definition is in the list of dynamic
5592 symbols, make sure the real definition is put
5593 there as well. */
5594 if (hlook->dynindx != -1 && h->dynindx == -1)
5595 {
c152c796 5596 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5597 {
5598 err_free_sym_hash:
5599 free (sorted_sym_hash);
5600 goto error_return;
5601 }
4ad4eba5
AM
5602 }
5603
5604 /* If the real definition is in the list of dynamic
5605 symbols, make sure the weak definition is put
5606 there as well. If we don't do this, then the
5607 dynamic loader might not merge the entries for the
5608 real definition and the weak definition. */
5609 if (h->dynindx != -1 && hlook->dynindx == -1)
5610 {
c152c796 5611 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5612 goto err_free_sym_hash;
4ad4eba5
AM
5613 }
5614 break;
5615 }
5616 }
5617 }
5618
5619 free (sorted_sym_hash);
5620 }
5621
33177bb1
AM
5622 if (bed->check_directives
5623 && !(*bed->check_directives) (abfd, info))
5624 return FALSE;
85fbca6a 5625
4ad4eba5
AM
5626 /* If this is a non-traditional link, try to optimize the handling
5627 of the .stab/.stabstr sections. */
5628 if (! dynamic
5629 && ! info->traditional_format
66eb6687 5630 && is_elf_hash_table (htab)
4ad4eba5
AM
5631 && (info->strip != strip_all && info->strip != strip_debugger))
5632 {
5633 asection *stabstr;
5634
5635 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5636 if (stabstr != NULL)
5637 {
5638 bfd_size_type string_offset = 0;
5639 asection *stab;
5640
5641 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5642 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5643 && (!stab->name[5] ||
5644 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5645 && (stab->flags & SEC_MERGE) == 0
5646 && !bfd_is_abs_section (stab->output_section))
5647 {
5648 struct bfd_elf_section_data *secdata;
5649
5650 secdata = elf_section_data (stab);
66eb6687
AM
5651 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5652 stabstr, &secdata->sec_info,
4ad4eba5
AM
5653 &string_offset))
5654 goto error_return;
5655 if (secdata->sec_info)
dbaa2011 5656 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5657 }
5658 }
5659 }
5660
e310298c 5661 if (dynamic && add_needed)
4ad4eba5
AM
5662 {
5663 /* Add this bfd to the loaded list. */
5664 struct elf_link_loaded_list *n;
5665
ca4be51c 5666 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5667 if (n == NULL)
5668 goto error_return;
5669 n->abfd = abfd;
e310298c
AM
5670 n->next = htab->dyn_loaded;
5671 htab->dyn_loaded = n;
4ad4eba5 5672 }
e310298c
AM
5673 if (dynamic && !add_needed
5674 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5675 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5676
5677 return TRUE;
5678
5679 error_free_vers:
c9594989
AM
5680 free (old_tab);
5681 free (old_strtab);
5682 free (nondeflt_vers);
5683 free (extversym);
4ad4eba5 5684 error_free_sym:
c9594989 5685 free (isymbuf);
4ad4eba5
AM
5686 error_return:
5687 return FALSE;
5688}
5689
8387904d
AM
5690/* Return the linker hash table entry of a symbol that might be
5691 satisfied by an archive symbol. Return -1 on error. */
5692
5693struct elf_link_hash_entry *
5694_bfd_elf_archive_symbol_lookup (bfd *abfd,
5695 struct bfd_link_info *info,
5696 const char *name)
5697{
5698 struct elf_link_hash_entry *h;
5699 char *p, *copy;
5700 size_t len, first;
5701
2a41f396 5702 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5703 if (h != NULL)
5704 return h;
5705
5706 /* If this is a default version (the name contains @@), look up the
5707 symbol again with only one `@' as well as without the version.
5708 The effect is that references to the symbol with and without the
5709 version will be matched by the default symbol in the archive. */
5710
5711 p = strchr (name, ELF_VER_CHR);
5712 if (p == NULL || p[1] != ELF_VER_CHR)
5713 return h;
5714
5715 /* First check with only one `@'. */
5716 len = strlen (name);
a50b1753 5717 copy = (char *) bfd_alloc (abfd, len);
8387904d 5718 if (copy == NULL)
e99955cd 5719 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5720
5721 first = p - name + 1;
5722 memcpy (copy, name, first);
5723 memcpy (copy + first, name + first + 1, len - first);
5724
2a41f396 5725 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5726 if (h == NULL)
5727 {
5728 /* We also need to check references to the symbol without the
5729 version. */
5730 copy[first - 1] = '\0';
5731 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5732 FALSE, FALSE, TRUE);
8387904d
AM
5733 }
5734
5735 bfd_release (abfd, copy);
5736 return h;
5737}
5738
0ad989f9 5739/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5740 don't use _bfd_generic_link_add_archive_symbols because we need to
5741 handle versioned symbols.
0ad989f9
L
5742
5743 Fortunately, ELF archive handling is simpler than that done by
5744 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5745 oddities. In ELF, if we find a symbol in the archive map, and the
5746 symbol is currently undefined, we know that we must pull in that
5747 object file.
5748
5749 Unfortunately, we do have to make multiple passes over the symbol
5750 table until nothing further is resolved. */
5751
4ad4eba5
AM
5752static bfd_boolean
5753elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5754{
5755 symindex c;
13e570f8 5756 unsigned char *included = NULL;
0ad989f9
L
5757 carsym *symdefs;
5758 bfd_boolean loop;
986f0783 5759 size_t amt;
8387904d
AM
5760 const struct elf_backend_data *bed;
5761 struct elf_link_hash_entry * (*archive_symbol_lookup)
5762 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5763
5764 if (! bfd_has_map (abfd))
5765 {
5766 /* An empty archive is a special case. */
5767 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5768 return TRUE;
5769 bfd_set_error (bfd_error_no_armap);
5770 return FALSE;
5771 }
5772
5773 /* Keep track of all symbols we know to be already defined, and all
5774 files we know to be already included. This is to speed up the
5775 second and subsequent passes. */
5776 c = bfd_ardata (abfd)->symdef_count;
5777 if (c == 0)
5778 return TRUE;
986f0783 5779 amt = c * sizeof (*included);
13e570f8
AM
5780 included = (unsigned char *) bfd_zmalloc (amt);
5781 if (included == NULL)
5782 return FALSE;
0ad989f9
L
5783
5784 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5785 bed = get_elf_backend_data (abfd);
5786 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5787
5788 do
5789 {
5790 file_ptr last;
5791 symindex i;
5792 carsym *symdef;
5793 carsym *symdefend;
5794
5795 loop = FALSE;
5796 last = -1;
5797
5798 symdef = symdefs;
5799 symdefend = symdef + c;
5800 for (i = 0; symdef < symdefend; symdef++, i++)
5801 {
5802 struct elf_link_hash_entry *h;
5803 bfd *element;
5804 struct bfd_link_hash_entry *undefs_tail;
5805 symindex mark;
5806
13e570f8 5807 if (included[i])
0ad989f9
L
5808 continue;
5809 if (symdef->file_offset == last)
5810 {
5811 included[i] = TRUE;
5812 continue;
5813 }
5814
8387904d 5815 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5816 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5817 goto error_return;
0ad989f9
L
5818
5819 if (h == NULL)
5820 continue;
5821
75cfe082
AM
5822 if (h->root.type == bfd_link_hash_undefined)
5823 {
5824 /* If the archive element has already been loaded then one
5825 of the symbols defined by that element might have been
5826 made undefined due to being in a discarded section. */
5827 if (h->indx == -3)
5828 continue;
5829 }
5830 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5831 {
5832 /* We currently have a common symbol. The archive map contains
5833 a reference to this symbol, so we may want to include it. We
5834 only want to include it however, if this archive element
5835 contains a definition of the symbol, not just another common
5836 declaration of it.
5837
5838 Unfortunately some archivers (including GNU ar) will put
5839 declarations of common symbols into their archive maps, as
5840 well as real definitions, so we cannot just go by the archive
5841 map alone. Instead we must read in the element's symbol
5842 table and check that to see what kind of symbol definition
5843 this is. */
5844 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5845 continue;
5846 }
75cfe082 5847 else
0ad989f9
L
5848 {
5849 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5850 /* Symbol must be defined. Don't check it again. */
5851 included[i] = TRUE;
0ad989f9
L
5852 continue;
5853 }
5854
5855 /* We need to include this archive member. */
5856 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5857 if (element == NULL)
5858 goto error_return;
5859
5860 if (! bfd_check_format (element, bfd_object))
5861 goto error_return;
5862
0ad989f9
L
5863 undefs_tail = info->hash->undefs_tail;
5864
0e144ba7
AM
5865 if (!(*info->callbacks
5866 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5867 continue;
0e144ba7 5868 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5869 goto error_return;
5870
5871 /* If there are any new undefined symbols, we need to make
5872 another pass through the archive in order to see whether
5873 they can be defined. FIXME: This isn't perfect, because
5874 common symbols wind up on undefs_tail and because an
5875 undefined symbol which is defined later on in this pass
5876 does not require another pass. This isn't a bug, but it
5877 does make the code less efficient than it could be. */
5878 if (undefs_tail != info->hash->undefs_tail)
5879 loop = TRUE;
5880
5881 /* Look backward to mark all symbols from this object file
5882 which we have already seen in this pass. */
5883 mark = i;
5884 do
5885 {
5886 included[mark] = TRUE;
5887 if (mark == 0)
5888 break;
5889 --mark;
5890 }
5891 while (symdefs[mark].file_offset == symdef->file_offset);
5892
5893 /* We mark subsequent symbols from this object file as we go
5894 on through the loop. */
5895 last = symdef->file_offset;
5896 }
5897 }
5898 while (loop);
5899
0ad989f9 5900 free (included);
0ad989f9
L
5901 return TRUE;
5902
5903 error_return:
c9594989 5904 free (included);
0ad989f9
L
5905 return FALSE;
5906}
4ad4eba5
AM
5907
5908/* Given an ELF BFD, add symbols to the global hash table as
5909 appropriate. */
5910
5911bfd_boolean
5912bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5913{
5914 switch (bfd_get_format (abfd))
5915 {
5916 case bfd_object:
5917 return elf_link_add_object_symbols (abfd, info);
5918 case bfd_archive:
5919 return elf_link_add_archive_symbols (abfd, info);
5920 default:
5921 bfd_set_error (bfd_error_wrong_format);
5922 return FALSE;
5923 }
5924}
5a580b3a 5925\f
14b1c01e
AM
5926struct hash_codes_info
5927{
5928 unsigned long *hashcodes;
5929 bfd_boolean error;
5930};
a0c8462f 5931
5a580b3a
AM
5932/* This function will be called though elf_link_hash_traverse to store
5933 all hash value of the exported symbols in an array. */
5934
5935static bfd_boolean
5936elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5937{
a50b1753 5938 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5939 const char *name;
5a580b3a
AM
5940 unsigned long ha;
5941 char *alc = NULL;
5942
5a580b3a
AM
5943 /* Ignore indirect symbols. These are added by the versioning code. */
5944 if (h->dynindx == -1)
5945 return TRUE;
5946
5947 name = h->root.root.string;
422f1182 5948 if (h->versioned >= versioned)
5a580b3a 5949 {
422f1182
L
5950 char *p = strchr (name, ELF_VER_CHR);
5951 if (p != NULL)
14b1c01e 5952 {
422f1182
L
5953 alc = (char *) bfd_malloc (p - name + 1);
5954 if (alc == NULL)
5955 {
5956 inf->error = TRUE;
5957 return FALSE;
5958 }
5959 memcpy (alc, name, p - name);
5960 alc[p - name] = '\0';
5961 name = alc;
14b1c01e 5962 }
5a580b3a
AM
5963 }
5964
5965 /* Compute the hash value. */
5966 ha = bfd_elf_hash (name);
5967
5968 /* Store the found hash value in the array given as the argument. */
14b1c01e 5969 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5970
5971 /* And store it in the struct so that we can put it in the hash table
5972 later. */
f6e332e6 5973 h->u.elf_hash_value = ha;
5a580b3a 5974
c9594989 5975 free (alc);
5a580b3a
AM
5976 return TRUE;
5977}
5978
fdc90cb4
JJ
5979struct collect_gnu_hash_codes
5980{
5981 bfd *output_bfd;
5982 const struct elf_backend_data *bed;
5983 unsigned long int nsyms;
5984 unsigned long int maskbits;
5985 unsigned long int *hashcodes;
5986 unsigned long int *hashval;
5987 unsigned long int *indx;
5988 unsigned long int *counts;
5989 bfd_vma *bitmask;
5990 bfd_byte *contents;
f16a9783 5991 bfd_size_type xlat;
fdc90cb4
JJ
5992 long int min_dynindx;
5993 unsigned long int bucketcount;
5994 unsigned long int symindx;
5995 long int local_indx;
5996 long int shift1, shift2;
5997 unsigned long int mask;
14b1c01e 5998 bfd_boolean error;
fdc90cb4
JJ
5999};
6000
6001/* This function will be called though elf_link_hash_traverse to store
6002 all hash value of the exported symbols in an array. */
6003
6004static bfd_boolean
6005elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6006{
a50b1753 6007 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6008 const char *name;
fdc90cb4
JJ
6009 unsigned long ha;
6010 char *alc = NULL;
6011
fdc90cb4
JJ
6012 /* Ignore indirect symbols. These are added by the versioning code. */
6013 if (h->dynindx == -1)
6014 return TRUE;
6015
6016 /* Ignore also local symbols and undefined symbols. */
6017 if (! (*s->bed->elf_hash_symbol) (h))
6018 return TRUE;
6019
6020 name = h->root.root.string;
422f1182 6021 if (h->versioned >= versioned)
fdc90cb4 6022 {
422f1182
L
6023 char *p = strchr (name, ELF_VER_CHR);
6024 if (p != NULL)
14b1c01e 6025 {
422f1182
L
6026 alc = (char *) bfd_malloc (p - name + 1);
6027 if (alc == NULL)
6028 {
6029 s->error = TRUE;
6030 return FALSE;
6031 }
6032 memcpy (alc, name, p - name);
6033 alc[p - name] = '\0';
6034 name = alc;
14b1c01e 6035 }
fdc90cb4
JJ
6036 }
6037
6038 /* Compute the hash value. */
6039 ha = bfd_elf_gnu_hash (name);
6040
6041 /* Store the found hash value in the array for compute_bucket_count,
6042 and also for .dynsym reordering purposes. */
6043 s->hashcodes[s->nsyms] = ha;
6044 s->hashval[h->dynindx] = ha;
6045 ++s->nsyms;
6046 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6047 s->min_dynindx = h->dynindx;
6048
c9594989 6049 free (alc);
fdc90cb4
JJ
6050 return TRUE;
6051}
6052
6053/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6054 final dynamic symbol renumbering in case of .gnu.hash.
6055 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6056 to the translation table. */
fdc90cb4
JJ
6057
6058static bfd_boolean
f16a9783 6059elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6060{
a50b1753 6061 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6062 unsigned long int bucket;
6063 unsigned long int val;
6064
fdc90cb4
JJ
6065 /* Ignore indirect symbols. */
6066 if (h->dynindx == -1)
6067 return TRUE;
6068
6069 /* Ignore also local symbols and undefined symbols. */
6070 if (! (*s->bed->elf_hash_symbol) (h))
6071 {
6072 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6073 {
6074 if (s->bed->record_xhash_symbol != NULL)
6075 {
6076 (*s->bed->record_xhash_symbol) (h, 0);
6077 s->local_indx++;
6078 }
6079 else
6080 h->dynindx = s->local_indx++;
6081 }
fdc90cb4
JJ
6082 return TRUE;
6083 }
6084
6085 bucket = s->hashval[h->dynindx] % s->bucketcount;
6086 val = (s->hashval[h->dynindx] >> s->shift1)
6087 & ((s->maskbits >> s->shift1) - 1);
6088 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6089 s->bitmask[val]
6090 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6091 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6092 if (s->counts[bucket] == 1)
6093 /* Last element terminates the chain. */
6094 val |= 1;
6095 bfd_put_32 (s->output_bfd, val,
6096 s->contents + (s->indx[bucket] - s->symindx) * 4);
6097 --s->counts[bucket];
f16a9783
MS
6098 if (s->bed->record_xhash_symbol != NULL)
6099 {
6100 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6101
6102 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6103 }
6104 else
6105 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6106 return TRUE;
6107}
6108
6109/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6110
6111bfd_boolean
6112_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6113{
6114 return !(h->forced_local
6115 || h->root.type == bfd_link_hash_undefined
6116 || h->root.type == bfd_link_hash_undefweak
6117 || ((h->root.type == bfd_link_hash_defined
6118 || h->root.type == bfd_link_hash_defweak)
6119 && h->root.u.def.section->output_section == NULL));
6120}
6121
5a580b3a
AM
6122/* Array used to determine the number of hash table buckets to use
6123 based on the number of symbols there are. If there are fewer than
6124 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6125 fewer than 37 we use 17 buckets, and so forth. We never use more
6126 than 32771 buckets. */
6127
6128static const size_t elf_buckets[] =
6129{
6130 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6131 16411, 32771, 0
6132};
6133
6134/* Compute bucket count for hashing table. We do not use a static set
6135 of possible tables sizes anymore. Instead we determine for all
6136 possible reasonable sizes of the table the outcome (i.e., the
6137 number of collisions etc) and choose the best solution. The
6138 weighting functions are not too simple to allow the table to grow
6139 without bounds. Instead one of the weighting factors is the size.
6140 Therefore the result is always a good payoff between few collisions
6141 (= short chain lengths) and table size. */
6142static size_t
b20dd2ce 6143compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6144 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6145 unsigned long int nsyms,
6146 int gnu_hash)
5a580b3a 6147{
5a580b3a 6148 size_t best_size = 0;
5a580b3a 6149 unsigned long int i;
5a580b3a 6150
5a580b3a
AM
6151 /* We have a problem here. The following code to optimize the table
6152 size requires an integer type with more the 32 bits. If
6153 BFD_HOST_U_64_BIT is set we know about such a type. */
6154#ifdef BFD_HOST_U_64_BIT
6155 if (info->optimize)
6156 {
5a580b3a
AM
6157 size_t minsize;
6158 size_t maxsize;
6159 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6160 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6161 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6162 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6163 unsigned long int *counts;
d40f3da9 6164 bfd_size_type amt;
0883b6e0 6165 unsigned int no_improvement_count = 0;
5a580b3a
AM
6166
6167 /* Possible optimization parameters: if we have NSYMS symbols we say
6168 that the hashing table must at least have NSYMS/4 and at most
6169 2*NSYMS buckets. */
6170 minsize = nsyms / 4;
6171 if (minsize == 0)
6172 minsize = 1;
6173 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6174 if (gnu_hash)
6175 {
6176 if (minsize < 2)
6177 minsize = 2;
6178 if ((best_size & 31) == 0)
6179 ++best_size;
6180 }
5a580b3a
AM
6181
6182 /* Create array where we count the collisions in. We must use bfd_malloc
6183 since the size could be large. */
6184 amt = maxsize;
6185 amt *= sizeof (unsigned long int);
a50b1753 6186 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6187 if (counts == NULL)
fdc90cb4 6188 return 0;
5a580b3a
AM
6189
6190 /* Compute the "optimal" size for the hash table. The criteria is a
6191 minimal chain length. The minor criteria is (of course) the size
6192 of the table. */
6193 for (i = minsize; i < maxsize; ++i)
6194 {
6195 /* Walk through the array of hashcodes and count the collisions. */
6196 BFD_HOST_U_64_BIT max;
6197 unsigned long int j;
6198 unsigned long int fact;
6199
fdc90cb4
JJ
6200 if (gnu_hash && (i & 31) == 0)
6201 continue;
6202
5a580b3a
AM
6203 memset (counts, '\0', i * sizeof (unsigned long int));
6204
6205 /* Determine how often each hash bucket is used. */
6206 for (j = 0; j < nsyms; ++j)
6207 ++counts[hashcodes[j] % i];
6208
6209 /* For the weight function we need some information about the
6210 pagesize on the target. This is information need not be 100%
6211 accurate. Since this information is not available (so far) we
6212 define it here to a reasonable default value. If it is crucial
6213 to have a better value some day simply define this value. */
6214# ifndef BFD_TARGET_PAGESIZE
6215# define BFD_TARGET_PAGESIZE (4096)
6216# endif
6217
fdc90cb4
JJ
6218 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6219 and the chains. */
6220 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6221
6222# if 1
6223 /* Variant 1: optimize for short chains. We add the squares
6224 of all the chain lengths (which favors many small chain
6225 over a few long chains). */
6226 for (j = 0; j < i; ++j)
6227 max += counts[j] * counts[j];
6228
6229 /* This adds penalties for the overall size of the table. */
fdc90cb4 6230 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6231 max *= fact * fact;
6232# else
6233 /* Variant 2: Optimize a lot more for small table. Here we
6234 also add squares of the size but we also add penalties for
6235 empty slots (the +1 term). */
6236 for (j = 0; j < i; ++j)
6237 max += (1 + counts[j]) * (1 + counts[j]);
6238
6239 /* The overall size of the table is considered, but not as
6240 strong as in variant 1, where it is squared. */
fdc90cb4 6241 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6242 max *= fact;
6243# endif
6244
6245 /* Compare with current best results. */
6246 if (max < best_chlen)
6247 {
6248 best_chlen = max;
6249 best_size = i;
ca4be51c 6250 no_improvement_count = 0;
5a580b3a 6251 }
0883b6e0
NC
6252 /* PR 11843: Avoid futile long searches for the best bucket size
6253 when there are a large number of symbols. */
6254 else if (++no_improvement_count == 100)
6255 break;
5a580b3a
AM
6256 }
6257
6258 free (counts);
6259 }
6260 else
6261#endif /* defined (BFD_HOST_U_64_BIT) */
6262 {
6263 /* This is the fallback solution if no 64bit type is available or if we
6264 are not supposed to spend much time on optimizations. We select the
6265 bucket count using a fixed set of numbers. */
6266 for (i = 0; elf_buckets[i] != 0; i++)
6267 {
6268 best_size = elf_buckets[i];
fdc90cb4 6269 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6270 break;
6271 }
fdc90cb4
JJ
6272 if (gnu_hash && best_size < 2)
6273 best_size = 2;
5a580b3a
AM
6274 }
6275
5a580b3a
AM
6276 return best_size;
6277}
6278
d0bf826b
AM
6279/* Size any SHT_GROUP section for ld -r. */
6280
6281bfd_boolean
6282_bfd_elf_size_group_sections (struct bfd_link_info *info)
6283{
6284 bfd *ibfd;
57963c05 6285 asection *s;
d0bf826b 6286
c72f2fb2 6287 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6288 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6289 && (s = ibfd->sections) != NULL
6290 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6291 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6292 return FALSE;
6293 return TRUE;
6294}
6295
04c3a755
NS
6296/* Set a default stack segment size. The value in INFO wins. If it
6297 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6298 undefined it is initialized. */
6299
6300bfd_boolean
6301bfd_elf_stack_segment_size (bfd *output_bfd,
6302 struct bfd_link_info *info,
6303 const char *legacy_symbol,
6304 bfd_vma default_size)
6305{
6306 struct elf_link_hash_entry *h = NULL;
6307
6308 /* Look for legacy symbol. */
6309 if (legacy_symbol)
6310 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6311 FALSE, FALSE, FALSE);
6312 if (h && (h->root.type == bfd_link_hash_defined
6313 || h->root.type == bfd_link_hash_defweak)
6314 && h->def_regular
6315 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6316 {
6317 /* The symbol has no type if specified on the command line. */
6318 h->type = STT_OBJECT;
6319 if (info->stacksize)
695344c0 6320 /* xgettext:c-format */
871b3ab2 6321 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6322 output_bfd, legacy_symbol);
04c3a755 6323 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6324 /* xgettext:c-format */
871b3ab2 6325 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6326 output_bfd, legacy_symbol);
04c3a755
NS
6327 else
6328 info->stacksize = h->root.u.def.value;
6329 }
6330
6331 if (!info->stacksize)
6332 /* If the user didn't set a size, or explicitly inhibit the
6333 size, set it now. */
6334 info->stacksize = default_size;
6335
6336 /* Provide the legacy symbol, if it is referenced. */
6337 if (h && (h->root.type == bfd_link_hash_undefined
6338 || h->root.type == bfd_link_hash_undefweak))
6339 {
6340 struct bfd_link_hash_entry *bh = NULL;
6341
6342 if (!(_bfd_generic_link_add_one_symbol
6343 (info, output_bfd, legacy_symbol,
6344 BSF_GLOBAL, bfd_abs_section_ptr,
6345 info->stacksize >= 0 ? info->stacksize : 0,
6346 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6347 return FALSE;
6348
6349 h = (struct elf_link_hash_entry *) bh;
6350 h->def_regular = 1;
6351 h->type = STT_OBJECT;
6352 }
6353
6354 return TRUE;
6355}
6356
b531344c
MR
6357/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6358
6359struct elf_gc_sweep_symbol_info
6360{
6361 struct bfd_link_info *info;
6362 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6363 bfd_boolean);
6364};
6365
6366static bfd_boolean
6367elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6368{
6369 if (!h->mark
6370 && (((h->root.type == bfd_link_hash_defined
6371 || h->root.type == bfd_link_hash_defweak)
6372 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6373 && h->root.u.def.section->gc_mark))
6374 || h->root.type == bfd_link_hash_undefined
6375 || h->root.type == bfd_link_hash_undefweak))
6376 {
6377 struct elf_gc_sweep_symbol_info *inf;
6378
6379 inf = (struct elf_gc_sweep_symbol_info *) data;
6380 (*inf->hide_symbol) (inf->info, h, TRUE);
6381 h->def_regular = 0;
6382 h->ref_regular = 0;
6383 h->ref_regular_nonweak = 0;
6384 }
6385
6386 return TRUE;
6387}
6388
5a580b3a
AM
6389/* Set up the sizes and contents of the ELF dynamic sections. This is
6390 called by the ELF linker emulation before_allocation routine. We
6391 must set the sizes of the sections before the linker sets the
6392 addresses of the various sections. */
6393
6394bfd_boolean
6395bfd_elf_size_dynamic_sections (bfd *output_bfd,
6396 const char *soname,
6397 const char *rpath,
6398 const char *filter_shlib,
7ee314fa
AM
6399 const char *audit,
6400 const char *depaudit,
5a580b3a
AM
6401 const char * const *auxiliary_filters,
6402 struct bfd_link_info *info,
fd91d419 6403 asection **sinterpptr)
5a580b3a 6404{
5a580b3a
AM
6405 bfd *dynobj;
6406 const struct elf_backend_data *bed;
5a580b3a
AM
6407
6408 *sinterpptr = NULL;
6409
5a580b3a
AM
6410 if (!is_elf_hash_table (info->hash))
6411 return TRUE;
6412
5a580b3a
AM
6413 dynobj = elf_hash_table (info)->dynobj;
6414
9a2a56cc 6415 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6416 {
902e9fc7
MR
6417 struct bfd_elf_version_tree *verdefs;
6418 struct elf_info_failed asvinfo;
5a580b3a
AM
6419 struct bfd_elf_version_tree *t;
6420 struct bfd_elf_version_expr *d;
902e9fc7 6421 asection *s;
e6699019 6422 size_t soname_indx;
7ee314fa 6423
5a580b3a
AM
6424 /* If we are supposed to export all symbols into the dynamic symbol
6425 table (this is not the normal case), then do so. */
55255dae 6426 if (info->export_dynamic
0e1862bb 6427 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6428 {
3d13f3e9
AM
6429 struct elf_info_failed eif;
6430
6431 eif.info = info;
6432 eif.failed = FALSE;
5a580b3a
AM
6433 elf_link_hash_traverse (elf_hash_table (info),
6434 _bfd_elf_export_symbol,
6435 &eif);
6436 if (eif.failed)
6437 return FALSE;
6438 }
6439
e6699019
L
6440 if (soname != NULL)
6441 {
6442 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6443 soname, TRUE);
6444 if (soname_indx == (size_t) -1
6445 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6446 return FALSE;
6447 }
6448 else
6449 soname_indx = (size_t) -1;
6450
5a580b3a 6451 /* Make all global versions with definition. */
fd91d419 6452 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6453 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6454 if (!d->symver && d->literal)
5a580b3a
AM
6455 {
6456 const char *verstr, *name;
6457 size_t namelen, verlen, newlen;
93252b1c 6458 char *newname, *p, leading_char;
5a580b3a
AM
6459 struct elf_link_hash_entry *newh;
6460
93252b1c 6461 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6462 name = d->pattern;
93252b1c 6463 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6464 verstr = t->name;
6465 verlen = strlen (verstr);
6466 newlen = namelen + verlen + 3;
6467
a50b1753 6468 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6469 if (newname == NULL)
6470 return FALSE;
93252b1c
MF
6471 newname[0] = leading_char;
6472 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6473
6474 /* Check the hidden versioned definition. */
6475 p = newname + namelen;
6476 *p++ = ELF_VER_CHR;
6477 memcpy (p, verstr, verlen + 1);
6478 newh = elf_link_hash_lookup (elf_hash_table (info),
6479 newname, FALSE, FALSE,
6480 FALSE);
6481 if (newh == NULL
6482 || (newh->root.type != bfd_link_hash_defined
6483 && newh->root.type != bfd_link_hash_defweak))
6484 {
6485 /* Check the default versioned definition. */
6486 *p++ = ELF_VER_CHR;
6487 memcpy (p, verstr, verlen + 1);
6488 newh = elf_link_hash_lookup (elf_hash_table (info),
6489 newname, FALSE, FALSE,
6490 FALSE);
6491 }
6492 free (newname);
6493
6494 /* Mark this version if there is a definition and it is
6495 not defined in a shared object. */
6496 if (newh != NULL
f5385ebf 6497 && !newh->def_dynamic
5a580b3a
AM
6498 && (newh->root.type == bfd_link_hash_defined
6499 || newh->root.type == bfd_link_hash_defweak))
6500 d->symver = 1;
6501 }
6502
6503 /* Attach all the symbols to their version information. */
5a580b3a 6504 asvinfo.info = info;
5a580b3a
AM
6505 asvinfo.failed = FALSE;
6506
6507 elf_link_hash_traverse (elf_hash_table (info),
6508 _bfd_elf_link_assign_sym_version,
6509 &asvinfo);
6510 if (asvinfo.failed)
6511 return FALSE;
6512
6513 if (!info->allow_undefined_version)
6514 {
6515 /* Check if all global versions have a definition. */
3d13f3e9 6516 bfd_boolean all_defined = TRUE;
fd91d419 6517 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6518 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6519 if (d->literal && !d->symver && !d->script)
5a580b3a 6520 {
4eca0228 6521 _bfd_error_handler
5a580b3a
AM
6522 (_("%s: undefined version: %s"),
6523 d->pattern, t->name);
6524 all_defined = FALSE;
6525 }
6526
6527 if (!all_defined)
6528 {
6529 bfd_set_error (bfd_error_bad_value);
6530 return FALSE;
6531 }
6532 }
6533
902e9fc7
MR
6534 /* Set up the version definition section. */
6535 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6536 BFD_ASSERT (s != NULL);
5a580b3a 6537
902e9fc7
MR
6538 /* We may have created additional version definitions if we are
6539 just linking a regular application. */
6540 verdefs = info->version_info;
5a580b3a 6541
902e9fc7
MR
6542 /* Skip anonymous version tag. */
6543 if (verdefs != NULL && verdefs->vernum == 0)
6544 verdefs = verdefs->next;
5a580b3a 6545
902e9fc7
MR
6546 if (verdefs == NULL && !info->create_default_symver)
6547 s->flags |= SEC_EXCLUDE;
6548 else
5a580b3a 6549 {
902e9fc7
MR
6550 unsigned int cdefs;
6551 bfd_size_type size;
6552 bfd_byte *p;
6553 Elf_Internal_Verdef def;
6554 Elf_Internal_Verdaux defaux;
6555 struct bfd_link_hash_entry *bh;
6556 struct elf_link_hash_entry *h;
6557 const char *name;
5a580b3a 6558
902e9fc7
MR
6559 cdefs = 0;
6560 size = 0;
5a580b3a 6561
902e9fc7
MR
6562 /* Make space for the base version. */
6563 size += sizeof (Elf_External_Verdef);
6564 size += sizeof (Elf_External_Verdaux);
6565 ++cdefs;
6566
6567 /* Make space for the default version. */
6568 if (info->create_default_symver)
6569 {
6570 size += sizeof (Elf_External_Verdef);
6571 ++cdefs;
3e3b46e5
PB
6572 }
6573
5a580b3a
AM
6574 for (t = verdefs; t != NULL; t = t->next)
6575 {
6576 struct bfd_elf_version_deps *n;
6577
a6cc6b3b
RO
6578 /* Don't emit base version twice. */
6579 if (t->vernum == 0)
6580 continue;
6581
5a580b3a
AM
6582 size += sizeof (Elf_External_Verdef);
6583 size += sizeof (Elf_External_Verdaux);
6584 ++cdefs;
6585
6586 for (n = t->deps; n != NULL; n = n->next)
6587 size += sizeof (Elf_External_Verdaux);
6588 }
6589
eea6121a 6590 s->size = size;
a50b1753 6591 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6592 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6593 return FALSE;
6594
6595 /* Fill in the version definition section. */
6596
6597 p = s->contents;
6598
6599 def.vd_version = VER_DEF_CURRENT;
6600 def.vd_flags = VER_FLG_BASE;
6601 def.vd_ndx = 1;
6602 def.vd_cnt = 1;
3e3b46e5
PB
6603 if (info->create_default_symver)
6604 {
6605 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6606 def.vd_next = sizeof (Elf_External_Verdef);
6607 }
6608 else
6609 {
6610 def.vd_aux = sizeof (Elf_External_Verdef);
6611 def.vd_next = (sizeof (Elf_External_Verdef)
6612 + sizeof (Elf_External_Verdaux));
6613 }
5a580b3a 6614
ef53be89 6615 if (soname_indx != (size_t) -1)
5a580b3a
AM
6616 {
6617 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6618 soname_indx);
6619 def.vd_hash = bfd_elf_hash (soname);
6620 defaux.vda_name = soname_indx;
3e3b46e5 6621 name = soname;
5a580b3a
AM
6622 }
6623 else
6624 {
ef53be89 6625 size_t indx;
5a580b3a 6626
765cf5f6 6627 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6628 def.vd_hash = bfd_elf_hash (name);
6629 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6630 name, FALSE);
ef53be89 6631 if (indx == (size_t) -1)
5a580b3a
AM
6632 return FALSE;
6633 defaux.vda_name = indx;
6634 }
6635 defaux.vda_next = 0;
6636
6637 _bfd_elf_swap_verdef_out (output_bfd, &def,
6638 (Elf_External_Verdef *) p);
6639 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6640 if (info->create_default_symver)
6641 {
6642 /* Add a symbol representing this version. */
6643 bh = NULL;
6644 if (! (_bfd_generic_link_add_one_symbol
6645 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6646 0, NULL, FALSE,
6647 get_elf_backend_data (dynobj)->collect, &bh)))
6648 return FALSE;
6649 h = (struct elf_link_hash_entry *) bh;
6650 h->non_elf = 0;
6651 h->def_regular = 1;
6652 h->type = STT_OBJECT;
6653 h->verinfo.vertree = NULL;
6654
6655 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6656 return FALSE;
6657
6658 /* Create a duplicate of the base version with the same
6659 aux block, but different flags. */
6660 def.vd_flags = 0;
6661 def.vd_ndx = 2;
6662 def.vd_aux = sizeof (Elf_External_Verdef);
6663 if (verdefs)
6664 def.vd_next = (sizeof (Elf_External_Verdef)
6665 + sizeof (Elf_External_Verdaux));
6666 else
6667 def.vd_next = 0;
6668 _bfd_elf_swap_verdef_out (output_bfd, &def,
6669 (Elf_External_Verdef *) p);
6670 p += sizeof (Elf_External_Verdef);
6671 }
5a580b3a
AM
6672 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6673 (Elf_External_Verdaux *) p);
6674 p += sizeof (Elf_External_Verdaux);
6675
6676 for (t = verdefs; t != NULL; t = t->next)
6677 {
6678 unsigned int cdeps;
6679 struct bfd_elf_version_deps *n;
5a580b3a 6680
a6cc6b3b
RO
6681 /* Don't emit the base version twice. */
6682 if (t->vernum == 0)
6683 continue;
6684
5a580b3a
AM
6685 cdeps = 0;
6686 for (n = t->deps; n != NULL; n = n->next)
6687 ++cdeps;
6688
6689 /* Add a symbol representing this version. */
6690 bh = NULL;
6691 if (! (_bfd_generic_link_add_one_symbol
6692 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6693 0, NULL, FALSE,
6694 get_elf_backend_data (dynobj)->collect, &bh)))
6695 return FALSE;
6696 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6697 h->non_elf = 0;
6698 h->def_regular = 1;
5a580b3a
AM
6699 h->type = STT_OBJECT;
6700 h->verinfo.vertree = t;
6701
c152c796 6702 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6703 return FALSE;
6704
6705 def.vd_version = VER_DEF_CURRENT;
6706 def.vd_flags = 0;
6707 if (t->globals.list == NULL
6708 && t->locals.list == NULL
6709 && ! t->used)
6710 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6711 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6712 def.vd_cnt = cdeps + 1;
6713 def.vd_hash = bfd_elf_hash (t->name);
6714 def.vd_aux = sizeof (Elf_External_Verdef);
6715 def.vd_next = 0;
a6cc6b3b
RO
6716
6717 /* If a basever node is next, it *must* be the last node in
6718 the chain, otherwise Verdef construction breaks. */
6719 if (t->next != NULL && t->next->vernum == 0)
6720 BFD_ASSERT (t->next->next == NULL);
6721
6722 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6723 def.vd_next = (sizeof (Elf_External_Verdef)
6724 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6725
6726 _bfd_elf_swap_verdef_out (output_bfd, &def,
6727 (Elf_External_Verdef *) p);
6728 p += sizeof (Elf_External_Verdef);
6729
6730 defaux.vda_name = h->dynstr_index;
6731 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6732 h->dynstr_index);
6733 defaux.vda_next = 0;
6734 if (t->deps != NULL)
6735 defaux.vda_next = sizeof (Elf_External_Verdaux);
6736 t->name_indx = defaux.vda_name;
6737
6738 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6739 (Elf_External_Verdaux *) p);
6740 p += sizeof (Elf_External_Verdaux);
6741
6742 for (n = t->deps; n != NULL; n = n->next)
6743 {
6744 if (n->version_needed == NULL)
6745 {
6746 /* This can happen if there was an error in the
6747 version script. */
6748 defaux.vda_name = 0;
6749 }
6750 else
6751 {
6752 defaux.vda_name = n->version_needed->name_indx;
6753 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6754 defaux.vda_name);
6755 }
6756 if (n->next == NULL)
6757 defaux.vda_next = 0;
6758 else
6759 defaux.vda_next = sizeof (Elf_External_Verdaux);
6760
6761 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6762 (Elf_External_Verdaux *) p);
6763 p += sizeof (Elf_External_Verdaux);
6764 }
6765 }
6766
5a580b3a
AM
6767 elf_tdata (output_bfd)->cverdefs = cdefs;
6768 }
902e9fc7
MR
6769 }
6770
6771 bed = get_elf_backend_data (output_bfd);
6772
6773 if (info->gc_sections && bed->can_gc_sections)
6774 {
6775 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6776
6777 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6778 dynamic symbol table. */
902e9fc7
MR
6779 sweep_info.info = info;
6780 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6781 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6782 &sweep_info);
3d13f3e9
AM
6783 }
6784
6785 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6786 {
6787 asection *s;
6788 struct elf_find_verdep_info sinfo;
6789
6790 /* Work out the size of the version reference section. */
6791
6792 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6793 BFD_ASSERT (s != NULL);
902e9fc7 6794
3d13f3e9
AM
6795 sinfo.info = info;
6796 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6797 if (sinfo.vers == 0)
6798 sinfo.vers = 1;
6799 sinfo.failed = FALSE;
6800
6801 elf_link_hash_traverse (elf_hash_table (info),
6802 _bfd_elf_link_find_version_dependencies,
6803 &sinfo);
6804 if (sinfo.failed)
6805 return FALSE;
6806
6807 if (elf_tdata (output_bfd)->verref == NULL)
6808 s->flags |= SEC_EXCLUDE;
6809 else
6810 {
6811 Elf_Internal_Verneed *vn;
6812 unsigned int size;
6813 unsigned int crefs;
6814 bfd_byte *p;
6815
6816 /* Build the version dependency section. */
6817 size = 0;
6818 crefs = 0;
6819 for (vn = elf_tdata (output_bfd)->verref;
6820 vn != NULL;
6821 vn = vn->vn_nextref)
6822 {
6823 Elf_Internal_Vernaux *a;
6824
6825 size += sizeof (Elf_External_Verneed);
6826 ++crefs;
6827 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6828 size += sizeof (Elf_External_Vernaux);
6829 }
6830
6831 s->size = size;
6832 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6833 if (s->contents == NULL)
6834 return FALSE;
6835
6836 p = s->contents;
6837 for (vn = elf_tdata (output_bfd)->verref;
6838 vn != NULL;
6839 vn = vn->vn_nextref)
6840 {
6841 unsigned int caux;
6842 Elf_Internal_Vernaux *a;
6843 size_t indx;
6844
6845 caux = 0;
6846 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6847 ++caux;
6848
6849 vn->vn_version = VER_NEED_CURRENT;
6850 vn->vn_cnt = caux;
6851 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6852 elf_dt_name (vn->vn_bfd) != NULL
6853 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6854 : lbasename (bfd_get_filename
6855 (vn->vn_bfd)),
3d13f3e9
AM
6856 FALSE);
6857 if (indx == (size_t) -1)
6858 return FALSE;
6859 vn->vn_file = indx;
6860 vn->vn_aux = sizeof (Elf_External_Verneed);
6861 if (vn->vn_nextref == NULL)
6862 vn->vn_next = 0;
6863 else
6864 vn->vn_next = (sizeof (Elf_External_Verneed)
6865 + caux * sizeof (Elf_External_Vernaux));
6866
6867 _bfd_elf_swap_verneed_out (output_bfd, vn,
6868 (Elf_External_Verneed *) p);
6869 p += sizeof (Elf_External_Verneed);
6870
6871 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6872 {
6873 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6874 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6875 a->vna_nodename, FALSE);
6876 if (indx == (size_t) -1)
6877 return FALSE;
6878 a->vna_name = indx;
6879 if (a->vna_nextptr == NULL)
6880 a->vna_next = 0;
6881 else
6882 a->vna_next = sizeof (Elf_External_Vernaux);
6883
6884 _bfd_elf_swap_vernaux_out (output_bfd, a,
6885 (Elf_External_Vernaux *) p);
6886 p += sizeof (Elf_External_Vernaux);
6887 }
6888 }
6889
6890 elf_tdata (output_bfd)->cverrefs = crefs;
6891 }
902e9fc7
MR
6892 }
6893
6894 /* Any syms created from now on start with -1 in
6895 got.refcount/offset and plt.refcount/offset. */
6896 elf_hash_table (info)->init_got_refcount
6897 = elf_hash_table (info)->init_got_offset;
6898 elf_hash_table (info)->init_plt_refcount
6899 = elf_hash_table (info)->init_plt_offset;
6900
6901 if (bfd_link_relocatable (info)
6902 && !_bfd_elf_size_group_sections (info))
6903 return FALSE;
6904
6905 /* The backend may have to create some sections regardless of whether
6906 we're dynamic or not. */
6907 if (bed->elf_backend_always_size_sections
6908 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6909 return FALSE;
6910
6911 /* Determine any GNU_STACK segment requirements, after the backend
6912 has had a chance to set a default segment size. */
6913 if (info->execstack)
6914 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6915 else if (info->noexecstack)
6916 elf_stack_flags (output_bfd) = PF_R | PF_W;
6917 else
6918 {
6919 bfd *inputobj;
6920 asection *notesec = NULL;
6921 int exec = 0;
6922
6923 for (inputobj = info->input_bfds;
6924 inputobj;
6925 inputobj = inputobj->link.next)
6926 {
6927 asection *s;
6928
6929 if (inputobj->flags
6930 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6931 continue;
57963c05
AM
6932 s = inputobj->sections;
6933 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6934 continue;
6935
902e9fc7
MR
6936 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6937 if (s)
6938 {
6939 if (s->flags & SEC_CODE)
6940 exec = PF_X;
6941 notesec = s;
6942 }
6943 else if (bed->default_execstack)
6944 exec = PF_X;
6945 }
6946 if (notesec || info->stacksize > 0)
6947 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6948 if (notesec && exec && bfd_link_relocatable (info)
6949 && notesec->output_section != bfd_abs_section_ptr)
6950 notesec->output_section->flags |= SEC_CODE;
6951 }
6952
6953 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6954 {
6955 struct elf_info_failed eif;
6956 struct elf_link_hash_entry *h;
6957 asection *dynstr;
6958 asection *s;
6959
6960 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6961 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6962
902e9fc7
MR
6963 if (info->symbolic)
6964 {
6965 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6966 return FALSE;
6967 info->flags |= DF_SYMBOLIC;
6968 }
6969
6970 if (rpath != NULL)
6971 {
6972 size_t indx;
6973 bfd_vma tag;
6974
6975 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6976 TRUE);
6977 if (indx == (size_t) -1)
6978 return FALSE;
6979
6980 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6981 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6982 return FALSE;
6983 }
6984
6985 if (filter_shlib != NULL)
6986 {
6987 size_t indx;
6988
6989 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6990 filter_shlib, TRUE);
6991 if (indx == (size_t) -1
6992 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6993 return FALSE;
6994 }
6995
6996 if (auxiliary_filters != NULL)
6997 {
6998 const char * const *p;
6999
7000 for (p = auxiliary_filters; *p != NULL; p++)
7001 {
7002 size_t indx;
7003
7004 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7005 *p, TRUE);
7006 if (indx == (size_t) -1
7007 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7008 return FALSE;
7009 }
7010 }
7011
7012 if (audit != NULL)
7013 {
7014 size_t indx;
7015
7016 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7017 TRUE);
7018 if (indx == (size_t) -1
7019 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7020 return FALSE;
7021 }
7022
7023 if (depaudit != NULL)
7024 {
7025 size_t indx;
7026
7027 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7028 TRUE);
7029 if (indx == (size_t) -1
7030 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7031 return FALSE;
7032 }
7033
7034 eif.info = info;
7035 eif.failed = FALSE;
7036
7037 /* Find all symbols which were defined in a dynamic object and make
7038 the backend pick a reasonable value for them. */
7039 elf_link_hash_traverse (elf_hash_table (info),
7040 _bfd_elf_adjust_dynamic_symbol,
7041 &eif);
7042 if (eif.failed)
7043 return FALSE;
7044
7045 /* Add some entries to the .dynamic section. We fill in some of the
7046 values later, in bfd_elf_final_link, but we must add the entries
7047 now so that we know the final size of the .dynamic section. */
7048
7049 /* If there are initialization and/or finalization functions to
7050 call then add the corresponding DT_INIT/DT_FINI entries. */
7051 h = (info->init_function
7052 ? elf_link_hash_lookup (elf_hash_table (info),
7053 info->init_function, FALSE,
7054 FALSE, FALSE)
7055 : NULL);
7056 if (h != NULL
7057 && (h->ref_regular
7058 || h->def_regular))
7059 {
7060 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7061 return FALSE;
7062 }
7063 h = (info->fini_function
7064 ? elf_link_hash_lookup (elf_hash_table (info),
7065 info->fini_function, FALSE,
7066 FALSE, FALSE)
7067 : NULL);
7068 if (h != NULL
7069 && (h->ref_regular
7070 || h->def_regular))
7071 {
7072 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7073 return FALSE;
7074 }
7075
7076 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7077 if (s != NULL && s->linker_has_input)
7078 {
7079 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7080 if (! bfd_link_executable (info))
7081 {
7082 bfd *sub;
7083 asection *o;
7084
57963c05
AM
7085 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7086 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7087 && (o = sub->sections) != NULL
7088 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7089 for (o = sub->sections; o != NULL; o = o->next)
7090 if (elf_section_data (o)->this_hdr.sh_type
7091 == SHT_PREINIT_ARRAY)
7092 {
7093 _bfd_error_handler
871b3ab2 7094 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7095 sub);
7096 break;
7097 }
7098
7099 bfd_set_error (bfd_error_nonrepresentable_section);
7100 return FALSE;
7101 }
7102
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7104 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7105 return FALSE;
7106 }
7107 s = bfd_get_section_by_name (output_bfd, ".init_array");
7108 if (s != NULL && s->linker_has_input)
7109 {
7110 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7111 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7112 return FALSE;
7113 }
7114 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7115 if (s != NULL && s->linker_has_input)
7116 {
7117 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7118 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7119 return FALSE;
7120 }
7121
7122 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7123 /* If .dynstr is excluded from the link, we don't want any of
7124 these tags. Strictly, we should be checking each section
7125 individually; This quick check covers for the case where
7126 someone does a /DISCARD/ : { *(*) }. */
7127 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7128 {
7129 bfd_size_type strsize;
7130
7131 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7132 if ((info->emit_hash
7133 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7134 || (info->emit_gnu_hash
f16a9783
MS
7135 && (bed->record_xhash_symbol == NULL
7136 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7137 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7138 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7139 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7140 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7141 bed->s->sizeof_sym))
7142 return FALSE;
7143 }
7144 }
7145
7146 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7147 return FALSE;
7148
7149 /* The backend must work out the sizes of all the other dynamic
7150 sections. */
7151 if (dynobj != NULL
7152 && bed->elf_backend_size_dynamic_sections != NULL
7153 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7154 return FALSE;
7155
7156 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7157 {
902e9fc7
MR
7158 if (elf_tdata (output_bfd)->cverdefs)
7159 {
7160 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7161
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7163 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7164 return FALSE;
7165 }
7166
7167 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7168 {
7169 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7170 return FALSE;
7171 }
7172 else if (info->flags & DF_BIND_NOW)
7173 {
7174 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7175 return FALSE;
7176 }
7177
7178 if (info->flags_1)
7179 {
7180 if (bfd_link_executable (info))
7181 info->flags_1 &= ~ (DF_1_INITFIRST
7182 | DF_1_NODELETE
7183 | DF_1_NOOPEN);
7184 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7185 return FALSE;
7186 }
7187
7188 if (elf_tdata (output_bfd)->cverrefs)
7189 {
7190 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7191
7192 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7193 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7194 return FALSE;
7195 }
5a580b3a 7196
8423293d
AM
7197 if ((elf_tdata (output_bfd)->cverrefs == 0
7198 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7199 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7200 {
902e9fc7
MR
7201 asection *s;
7202
3d4d4302 7203 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7204 s->flags |= SEC_EXCLUDE;
7205 }
7206 }
7207 return TRUE;
7208}
7209
74541ad4
AM
7210/* Find the first non-excluded output section. We'll use its
7211 section symbol for some emitted relocs. */
7212void
7213_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7214{
7215 asection *s;
f26a3287 7216 asection *found = NULL;
74541ad4
AM
7217
7218 for (s = output_bfd->sections; s != NULL; s = s->next)
7219 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7220 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7221 {
f26a3287
AM
7222 found = s;
7223 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7224 break;
74541ad4 7225 }
f26a3287 7226 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7227}
7228
7229/* Find two non-excluded output sections, one for code, one for data.
7230 We'll use their section symbols for some emitted relocs. */
7231void
7232_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7233{
7234 asection *s;
f26a3287 7235 asection *found = NULL;
74541ad4 7236
266b05cf 7237 /* Data first, since setting text_index_section changes
7f923b7f 7238 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7239 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7240 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7241 && !(s->flags & SEC_READONLY)
d00dd7dc 7242 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7243 {
f26a3287
AM
7244 found = s;
7245 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7246 break;
74541ad4 7247 }
f26a3287 7248 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7249
7250 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7251 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7252 && (s->flags & SEC_READONLY)
d00dd7dc 7253 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7254 {
f26a3287 7255 found = s;
74541ad4
AM
7256 break;
7257 }
f26a3287 7258 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7259}
7260
f16a9783
MS
7261#define GNU_HASH_SECTION_NAME(bed) \
7262 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7263
8423293d
AM
7264bfd_boolean
7265bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7266{
74541ad4 7267 const struct elf_backend_data *bed;
23ec1e32 7268 unsigned long section_sym_count;
96d01d93 7269 bfd_size_type dynsymcount = 0;
74541ad4 7270
8423293d
AM
7271 if (!is_elf_hash_table (info->hash))
7272 return TRUE;
7273
74541ad4
AM
7274 bed = get_elf_backend_data (output_bfd);
7275 (*bed->elf_backend_init_index_section) (output_bfd, info);
7276
23ec1e32
MR
7277 /* Assign dynsym indices. In a shared library we generate a section
7278 symbol for each output section, which come first. Next come all
7279 of the back-end allocated local dynamic syms, followed by the rest
7280 of the global symbols.
7281
7282 This is usually not needed for static binaries, however backends
7283 can request to always do it, e.g. the MIPS backend uses dynamic
7284 symbol counts to lay out GOT, which will be produced in the
7285 presence of GOT relocations even in static binaries (holding fixed
7286 data in that case, to satisfy those relocations). */
7287
7288 if (elf_hash_table (info)->dynamic_sections_created
7289 || bed->always_renumber_dynsyms)
7290 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7291 &section_sym_count);
7292
8423293d
AM
7293 if (elf_hash_table (info)->dynamic_sections_created)
7294 {
7295 bfd *dynobj;
8423293d 7296 asection *s;
8423293d
AM
7297 unsigned int dtagcount;
7298
7299 dynobj = elf_hash_table (info)->dynobj;
7300
5a580b3a 7301 /* Work out the size of the symbol version section. */
3d4d4302 7302 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7303 BFD_ASSERT (s != NULL);
d5486c43 7304 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7305 {
eea6121a 7306 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7307 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7308 if (s->contents == NULL)
7309 return FALSE;
7310
7311 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7312 return FALSE;
7313 }
7314
7315 /* Set the size of the .dynsym and .hash sections. We counted
7316 the number of dynamic symbols in elf_link_add_object_symbols.
7317 We will build the contents of .dynsym and .hash when we build
7318 the final symbol table, because until then we do not know the
7319 correct value to give the symbols. We built the .dynstr
7320 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7321 s = elf_hash_table (info)->dynsym;
5a580b3a 7322 BFD_ASSERT (s != NULL);
eea6121a 7323 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7324
d5486c43
L
7325 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7326 if (s->contents == NULL)
7327 return FALSE;
5a580b3a 7328
d5486c43
L
7329 /* The first entry in .dynsym is a dummy symbol. Clear all the
7330 section syms, in case we don't output them all. */
7331 ++section_sym_count;
7332 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7333
fdc90cb4
JJ
7334 elf_hash_table (info)->bucketcount = 0;
7335
5a580b3a
AM
7336 /* Compute the size of the hashing table. As a side effect this
7337 computes the hash values for all the names we export. */
fdc90cb4
JJ
7338 if (info->emit_hash)
7339 {
7340 unsigned long int *hashcodes;
14b1c01e 7341 struct hash_codes_info hashinf;
fdc90cb4
JJ
7342 bfd_size_type amt;
7343 unsigned long int nsyms;
7344 size_t bucketcount;
7345 size_t hash_entry_size;
7346
7347 /* Compute the hash values for all exported symbols. At the same
7348 time store the values in an array so that we could use them for
7349 optimizations. */
7350 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7351 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7352 if (hashcodes == NULL)
7353 return FALSE;
14b1c01e
AM
7354 hashinf.hashcodes = hashcodes;
7355 hashinf.error = FALSE;
5a580b3a 7356
fdc90cb4
JJ
7357 /* Put all hash values in HASHCODES. */
7358 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7359 elf_collect_hash_codes, &hashinf);
7360 if (hashinf.error)
4dd07732
AM
7361 {
7362 free (hashcodes);
7363 return FALSE;
7364 }
5a580b3a 7365
14b1c01e 7366 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7367 bucketcount
7368 = compute_bucket_count (info, hashcodes, nsyms, 0);
7369 free (hashcodes);
7370
4b48e2f6 7371 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7372 return FALSE;
5a580b3a 7373
fdc90cb4
JJ
7374 elf_hash_table (info)->bucketcount = bucketcount;
7375
3d4d4302 7376 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7377 BFD_ASSERT (s != NULL);
7378 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7379 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7380 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7381 if (s->contents == NULL)
7382 return FALSE;
7383
7384 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7385 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7386 s->contents + hash_entry_size);
7387 }
7388
7389 if (info->emit_gnu_hash)
7390 {
7391 size_t i, cnt;
7392 unsigned char *contents;
7393 struct collect_gnu_hash_codes cinfo;
7394 bfd_size_type amt;
7395 size_t bucketcount;
7396
7397 memset (&cinfo, 0, sizeof (cinfo));
7398
7399 /* Compute the hash values for all exported symbols. At the same
7400 time store the values in an array so that we could use them for
7401 optimizations. */
7402 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7403 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7404 if (cinfo.hashcodes == NULL)
7405 return FALSE;
7406
7407 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7408 cinfo.min_dynindx = -1;
7409 cinfo.output_bfd = output_bfd;
7410 cinfo.bed = bed;
7411
7412 /* Put all hash values in HASHCODES. */
7413 elf_link_hash_traverse (elf_hash_table (info),
7414 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7415 if (cinfo.error)
4dd07732
AM
7416 {
7417 free (cinfo.hashcodes);
7418 return FALSE;
7419 }
fdc90cb4
JJ
7420
7421 bucketcount
7422 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7423
7424 if (bucketcount == 0)
7425 {
7426 free (cinfo.hashcodes);
7427 return FALSE;
7428 }
7429
f16a9783 7430 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7431 BFD_ASSERT (s != NULL);
7432
7433 if (cinfo.nsyms == 0)
7434 {
f16a9783 7435 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7436 BFD_ASSERT (cinfo.min_dynindx == -1);
7437 free (cinfo.hashcodes);
7438 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7439 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7440 if (contents == NULL)
7441 return FALSE;
7442 s->contents = contents;
7443 /* 1 empty bucket. */
7444 bfd_put_32 (output_bfd, 1, contents);
7445 /* SYMIDX above the special symbol 0. */
7446 bfd_put_32 (output_bfd, 1, contents + 4);
7447 /* Just one word for bitmask. */
7448 bfd_put_32 (output_bfd, 1, contents + 8);
7449 /* Only hash fn bloom filter. */
7450 bfd_put_32 (output_bfd, 0, contents + 12);
7451 /* No hashes are valid - empty bitmask. */
7452 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7453 /* No hashes in the only bucket. */
7454 bfd_put_32 (output_bfd, 0,
7455 contents + 16 + bed->s->arch_size / 8);
7456 }
7457 else
7458 {
9e6619e2 7459 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7460 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7461
9e6619e2
AM
7462 x = cinfo.nsyms;
7463 maskbitslog2 = 1;
7464 while ((x >>= 1) != 0)
7465 ++maskbitslog2;
fdc90cb4
JJ
7466 if (maskbitslog2 < 3)
7467 maskbitslog2 = 5;
7468 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7469 maskbitslog2 = maskbitslog2 + 3;
7470 else
7471 maskbitslog2 = maskbitslog2 + 2;
7472 if (bed->s->arch_size == 64)
7473 {
7474 if (maskbitslog2 == 5)
7475 maskbitslog2 = 6;
7476 cinfo.shift1 = 6;
7477 }
7478 else
7479 cinfo.shift1 = 5;
7480 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7481 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7482 cinfo.maskbits = 1 << maskbitslog2;
7483 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7484 amt = bucketcount * sizeof (unsigned long int) * 2;
7485 amt += maskwords * sizeof (bfd_vma);
a50b1753 7486 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7487 if (cinfo.bitmask == NULL)
7488 {
7489 free (cinfo.hashcodes);
7490 return FALSE;
7491 }
7492
a50b1753 7493 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7494 cinfo.indx = cinfo.counts + bucketcount;
7495 cinfo.symindx = dynsymcount - cinfo.nsyms;
7496 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7497
7498 /* Determine how often each hash bucket is used. */
7499 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7500 for (i = 0; i < cinfo.nsyms; ++i)
7501 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7502
7503 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7504 if (cinfo.counts[i] != 0)
7505 {
7506 cinfo.indx[i] = cnt;
7507 cnt += cinfo.counts[i];
7508 }
7509 BFD_ASSERT (cnt == dynsymcount);
7510 cinfo.bucketcount = bucketcount;
7511 cinfo.local_indx = cinfo.min_dynindx;
7512
7513 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7514 s->size += cinfo.maskbits / 8;
f16a9783
MS
7515 if (bed->record_xhash_symbol != NULL)
7516 s->size += cinfo.nsyms * 4;
a50b1753 7517 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7518 if (contents == NULL)
7519 {
7520 free (cinfo.bitmask);
7521 free (cinfo.hashcodes);
7522 return FALSE;
7523 }
7524
7525 s->contents = contents;
7526 bfd_put_32 (output_bfd, bucketcount, contents);
7527 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7528 bfd_put_32 (output_bfd, maskwords, contents + 8);
7529 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7530 contents += 16 + cinfo.maskbits / 8;
7531
7532 for (i = 0; i < bucketcount; ++i)
7533 {
7534 if (cinfo.counts[i] == 0)
7535 bfd_put_32 (output_bfd, 0, contents);
7536 else
7537 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7538 contents += 4;
7539 }
7540
7541 cinfo.contents = contents;
7542
f16a9783
MS
7543 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7544 /* Renumber dynamic symbols, if populating .gnu.hash section.
7545 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7546 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7547 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7548
7549 contents = s->contents + 16;
7550 for (i = 0; i < maskwords; ++i)
7551 {
7552 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7553 contents);
7554 contents += bed->s->arch_size / 8;
7555 }
7556
7557 free (cinfo.bitmask);
7558 free (cinfo.hashcodes);
7559 }
7560 }
5a580b3a 7561
3d4d4302 7562 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7563 BFD_ASSERT (s != NULL);
7564
4ad4eba5 7565 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7566
eea6121a 7567 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7568
7569 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7570 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7571 return FALSE;
7572 }
7573
7574 return TRUE;
7575}
4d269e42 7576\f
4d269e42
AM
7577/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7578
7579static void
7580merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7581 asection *sec)
7582{
dbaa2011
AM
7583 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7584 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7585}
7586
7587/* Finish SHF_MERGE section merging. */
7588
7589bfd_boolean
630993ec 7590_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7591{
7592 bfd *ibfd;
7593 asection *sec;
7594
7595 if (!is_elf_hash_table (info->hash))
7596 return FALSE;
7597
c72f2fb2 7598 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7599 if ((ibfd->flags & DYNAMIC) == 0
7600 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7601 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7602 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7603 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7604 if ((sec->flags & SEC_MERGE) != 0
7605 && !bfd_is_abs_section (sec->output_section))
7606 {
7607 struct bfd_elf_section_data *secdata;
7608
7609 secdata = elf_section_data (sec);
630993ec 7610 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7611 &elf_hash_table (info)->merge_info,
7612 sec, &secdata->sec_info))
7613 return FALSE;
7614 else if (secdata->sec_info)
dbaa2011 7615 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7616 }
7617
7618 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7619 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7620 merge_sections_remove_hook);
7621 return TRUE;
7622}
7623
7624/* Create an entry in an ELF linker hash table. */
7625
7626struct bfd_hash_entry *
7627_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7628 struct bfd_hash_table *table,
7629 const char *string)
7630{
7631 /* Allocate the structure if it has not already been allocated by a
7632 subclass. */
7633 if (entry == NULL)
7634 {
a50b1753 7635 entry = (struct bfd_hash_entry *)
ca4be51c 7636 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7637 if (entry == NULL)
7638 return entry;
7639 }
7640
7641 /* Call the allocation method of the superclass. */
7642 entry = _bfd_link_hash_newfunc (entry, table, string);
7643 if (entry != NULL)
7644 {
7645 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7646 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7647
7648 /* Set local fields. */
7649 ret->indx = -1;
7650 ret->dynindx = -1;
7651 ret->got = htab->init_got_refcount;
7652 ret->plt = htab->init_plt_refcount;
7653 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7654 - offsetof (struct elf_link_hash_entry, size)));
7655 /* Assume that we have been called by a non-ELF symbol reader.
7656 This flag is then reset by the code which reads an ELF input
7657 file. This ensures that a symbol created by a non-ELF symbol
7658 reader will have the flag set correctly. */
7659 ret->non_elf = 1;
7660 }
7661
7662 return entry;
7663}
7664
7665/* Copy data from an indirect symbol to its direct symbol, hiding the
7666 old indirect symbol. Also used for copying flags to a weakdef. */
7667
7668void
7669_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7670 struct elf_link_hash_entry *dir,
7671 struct elf_link_hash_entry *ind)
7672{
7673 struct elf_link_hash_table *htab;
7674
ad172eaa
L
7675 if (ind->dyn_relocs != NULL)
7676 {
7677 if (dir->dyn_relocs != NULL)
7678 {
7679 struct elf_dyn_relocs **pp;
7680 struct elf_dyn_relocs *p;
7681
7682 /* Add reloc counts against the indirect sym to the direct sym
7683 list. Merge any entries against the same section. */
7684 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7685 {
7686 struct elf_dyn_relocs *q;
7687
7688 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7689 if (q->sec == p->sec)
7690 {
7691 q->pc_count += p->pc_count;
7692 q->count += p->count;
7693 *pp = p->next;
7694 break;
7695 }
7696 if (q == NULL)
7697 pp = &p->next;
7698 }
7699 *pp = dir->dyn_relocs;
7700 }
7701
7702 dir->dyn_relocs = ind->dyn_relocs;
7703 ind->dyn_relocs = NULL;
7704 }
7705
4d269e42 7706 /* Copy down any references that we may have already seen to the
e81830c5 7707 symbol which just became indirect. */
4d269e42 7708
422f1182 7709 if (dir->versioned != versioned_hidden)
e81830c5
AM
7710 dir->ref_dynamic |= ind->ref_dynamic;
7711 dir->ref_regular |= ind->ref_regular;
7712 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7713 dir->non_got_ref |= ind->non_got_ref;
7714 dir->needs_plt |= ind->needs_plt;
7715 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7716
7717 if (ind->root.type != bfd_link_hash_indirect)
7718 return;
7719
7720 /* Copy over the global and procedure linkage table refcount entries.
7721 These may have been already set up by a check_relocs routine. */
7722 htab = elf_hash_table (info);
7723 if (ind->got.refcount > htab->init_got_refcount.refcount)
7724 {
7725 if (dir->got.refcount < 0)
7726 dir->got.refcount = 0;
7727 dir->got.refcount += ind->got.refcount;
7728 ind->got.refcount = htab->init_got_refcount.refcount;
7729 }
7730
7731 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7732 {
7733 if (dir->plt.refcount < 0)
7734 dir->plt.refcount = 0;
7735 dir->plt.refcount += ind->plt.refcount;
7736 ind->plt.refcount = htab->init_plt_refcount.refcount;
7737 }
7738
7739 if (ind->dynindx != -1)
7740 {
7741 if (dir->dynindx != -1)
7742 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7743 dir->dynindx = ind->dynindx;
7744 dir->dynstr_index = ind->dynstr_index;
7745 ind->dynindx = -1;
7746 ind->dynstr_index = 0;
7747 }
7748}
7749
7750void
7751_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7752 struct elf_link_hash_entry *h,
7753 bfd_boolean force_local)
7754{
3aa14d16
L
7755 /* STT_GNU_IFUNC symbol must go through PLT. */
7756 if (h->type != STT_GNU_IFUNC)
7757 {
7758 h->plt = elf_hash_table (info)->init_plt_offset;
7759 h->needs_plt = 0;
7760 }
4d269e42
AM
7761 if (force_local)
7762 {
7763 h->forced_local = 1;
7764 if (h->dynindx != -1)
7765 {
4d269e42
AM
7766 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7767 h->dynstr_index);
641338d8
AM
7768 h->dynindx = -1;
7769 h->dynstr_index = 0;
4d269e42
AM
7770 }
7771 }
7772}
7773
34a87bb0
L
7774/* Hide a symbol. */
7775
7776void
7777_bfd_elf_link_hide_symbol (bfd *output_bfd,
7778 struct bfd_link_info *info,
7779 struct bfd_link_hash_entry *h)
7780{
7781 if (is_elf_hash_table (info->hash))
7782 {
7783 const struct elf_backend_data *bed
7784 = get_elf_backend_data (output_bfd);
7785 struct elf_link_hash_entry *eh
7786 = (struct elf_link_hash_entry *) h;
7787 bed->elf_backend_hide_symbol (info, eh, TRUE);
7788 eh->def_dynamic = 0;
7789 eh->ref_dynamic = 0;
7790 eh->dynamic_def = 0;
7791 }
7792}
7793
7bf52ea2
AM
7794/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7795 caller. */
4d269e42
AM
7796
7797bfd_boolean
7798_bfd_elf_link_hash_table_init
7799 (struct elf_link_hash_table *table,
7800 bfd *abfd,
7801 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7802 struct bfd_hash_table *,
7803 const char *),
4dfe6ac6
NC
7804 unsigned int entsize,
7805 enum elf_target_id target_id)
4d269e42
AM
7806{
7807 bfd_boolean ret;
7808 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7809
4d269e42
AM
7810 table->init_got_refcount.refcount = can_refcount - 1;
7811 table->init_plt_refcount.refcount = can_refcount - 1;
7812 table->init_got_offset.offset = -(bfd_vma) 1;
7813 table->init_plt_offset.offset = -(bfd_vma) 1;
7814 /* The first dynamic symbol is a dummy. */
7815 table->dynsymcount = 1;
7816
7817 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7818
4d269e42 7819 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7820 table->hash_table_id = target_id;
90c14f0c 7821 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7822
7823 return ret;
7824}
7825
7826/* Create an ELF linker hash table. */
7827
7828struct bfd_link_hash_table *
7829_bfd_elf_link_hash_table_create (bfd *abfd)
7830{
7831 struct elf_link_hash_table *ret;
986f0783 7832 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7833
7bf52ea2 7834 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7835 if (ret == NULL)
7836 return NULL;
7837
7838 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7839 sizeof (struct elf_link_hash_entry),
7840 GENERIC_ELF_DATA))
4d269e42
AM
7841 {
7842 free (ret);
7843 return NULL;
7844 }
d495ab0d 7845 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7846
7847 return &ret->root;
7848}
7849
9f7c3e5e
AM
7850/* Destroy an ELF linker hash table. */
7851
7852void
d495ab0d 7853_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7854{
d495ab0d
AM
7855 struct elf_link_hash_table *htab;
7856
7857 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7858 if (htab->dynstr != NULL)
7859 _bfd_elf_strtab_free (htab->dynstr);
7860 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7861 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7862}
7863
4d269e42
AM
7864/* This is a hook for the ELF emulation code in the generic linker to
7865 tell the backend linker what file name to use for the DT_NEEDED
7866 entry for a dynamic object. */
7867
7868void
7869bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7870{
7871 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7872 && bfd_get_format (abfd) == bfd_object)
7873 elf_dt_name (abfd) = name;
7874}
7875
7876int
7877bfd_elf_get_dyn_lib_class (bfd *abfd)
7878{
7879 int lib_class;
7880 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7881 && bfd_get_format (abfd) == bfd_object)
7882 lib_class = elf_dyn_lib_class (abfd);
7883 else
7884 lib_class = 0;
7885 return lib_class;
7886}
7887
7888void
7889bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7890{
7891 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7892 && bfd_get_format (abfd) == bfd_object)
7893 elf_dyn_lib_class (abfd) = lib_class;
7894}
7895
7896/* Get the list of DT_NEEDED entries for a link. This is a hook for
7897 the linker ELF emulation code. */
7898
7899struct bfd_link_needed_list *
7900bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7901 struct bfd_link_info *info)
7902{
7903 if (! is_elf_hash_table (info->hash))
7904 return NULL;
7905 return elf_hash_table (info)->needed;
7906}
7907
7908/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7909 hook for the linker ELF emulation code. */
7910
7911struct bfd_link_needed_list *
7912bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7913 struct bfd_link_info *info)
7914{
7915 if (! is_elf_hash_table (info->hash))
7916 return NULL;
7917 return elf_hash_table (info)->runpath;
7918}
7919
7920/* Get the name actually used for a dynamic object for a link. This
7921 is the SONAME entry if there is one. Otherwise, it is the string
7922 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7923
7924const char *
7925bfd_elf_get_dt_soname (bfd *abfd)
7926{
7927 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7928 && bfd_get_format (abfd) == bfd_object)
7929 return elf_dt_name (abfd);
7930 return NULL;
7931}
7932
7933/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7934 the ELF linker emulation code. */
7935
7936bfd_boolean
7937bfd_elf_get_bfd_needed_list (bfd *abfd,
7938 struct bfd_link_needed_list **pneeded)
7939{
7940 asection *s;
7941 bfd_byte *dynbuf = NULL;
cb33740c 7942 unsigned int elfsec;
4d269e42
AM
7943 unsigned long shlink;
7944 bfd_byte *extdyn, *extdynend;
7945 size_t extdynsize;
7946 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7947
7948 *pneeded = NULL;
7949
7950 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7951 || bfd_get_format (abfd) != bfd_object)
7952 return TRUE;
7953
7954 s = bfd_get_section_by_name (abfd, ".dynamic");
7955 if (s == NULL || s->size == 0)
7956 return TRUE;
7957
7958 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7959 goto error_return;
7960
7961 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7962 if (elfsec == SHN_BAD)
4d269e42
AM
7963 goto error_return;
7964
7965 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7966
4d269e42
AM
7967 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7968 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7969
7970 extdyn = dynbuf;
7971 extdynend = extdyn + s->size;
7972 for (; extdyn < extdynend; extdyn += extdynsize)
7973 {
7974 Elf_Internal_Dyn dyn;
7975
7976 (*swap_dyn_in) (abfd, extdyn, &dyn);
7977
7978 if (dyn.d_tag == DT_NULL)
7979 break;
7980
7981 if (dyn.d_tag == DT_NEEDED)
7982 {
7983 const char *string;
7984 struct bfd_link_needed_list *l;
7985 unsigned int tagv = dyn.d_un.d_val;
986f0783 7986 size_t amt;
4d269e42
AM
7987
7988 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7989 if (string == NULL)
7990 goto error_return;
7991
7992 amt = sizeof *l;
a50b1753 7993 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7994 if (l == NULL)
7995 goto error_return;
7996
7997 l->by = abfd;
7998 l->name = string;
7999 l->next = *pneeded;
8000 *pneeded = l;
8001 }
8002 }
8003
8004 free (dynbuf);
8005
8006 return TRUE;
8007
8008 error_return:
c9594989 8009 free (dynbuf);
4d269e42
AM
8010 return FALSE;
8011}
8012
8013struct elf_symbuf_symbol
8014{
8015 unsigned long st_name; /* Symbol name, index in string tbl */
8016 unsigned char st_info; /* Type and binding attributes */
8017 unsigned char st_other; /* Visibilty, and target specific */
8018};
8019
8020struct elf_symbuf_head
8021{
8022 struct elf_symbuf_symbol *ssym;
ef53be89 8023 size_t count;
4d269e42
AM
8024 unsigned int st_shndx;
8025};
8026
8027struct elf_symbol
8028{
8029 union
8030 {
8031 Elf_Internal_Sym *isym;
8032 struct elf_symbuf_symbol *ssym;
dcea6a95 8033 void *p;
4d269e42
AM
8034 } u;
8035 const char *name;
8036};
8037
8038/* Sort references to symbols by ascending section number. */
8039
8040static int
8041elf_sort_elf_symbol (const void *arg1, const void *arg2)
8042{
8043 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8044 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8045
dcea6a95
AM
8046 if (s1->st_shndx != s2->st_shndx)
8047 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8048 /* Final sort by the address of the sym in the symbuf ensures
8049 a stable sort. */
8050 if (s1 != s2)
8051 return s1 > s2 ? 1 : -1;
8052 return 0;
4d269e42
AM
8053}
8054
8055static int
8056elf_sym_name_compare (const void *arg1, const void *arg2)
8057{
8058 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8059 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8060 int ret = strcmp (s1->name, s2->name);
8061 if (ret != 0)
8062 return ret;
8063 if (s1->u.p != s2->u.p)
8064 return s1->u.p > s2->u.p ? 1 : -1;
8065 return 0;
4d269e42
AM
8066}
8067
8068static struct elf_symbuf_head *
ef53be89 8069elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8070{
14b1c01e 8071 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8072 struct elf_symbuf_symbol *ssym;
8073 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8074 size_t i, shndx_count, total_size, amt;
4d269e42 8075
446f7ed5
AM
8076 amt = symcount * sizeof (*indbuf);
8077 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8078 if (indbuf == NULL)
8079 return NULL;
8080
8081 for (ind = indbuf, i = 0; i < symcount; i++)
8082 if (isymbuf[i].st_shndx != SHN_UNDEF)
8083 *ind++ = &isymbuf[i];
8084 indbufend = ind;
8085
8086 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8087 elf_sort_elf_symbol);
8088
8089 shndx_count = 0;
8090 if (indbufend > indbuf)
8091 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8092 if (ind[0]->st_shndx != ind[1]->st_shndx)
8093 shndx_count++;
8094
3ae181ee
L
8095 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8096 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8097 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8098 if (ssymbuf == NULL)
8099 {
8100 free (indbuf);
8101 return NULL;
8102 }
8103
3ae181ee 8104 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8105 ssymbuf->ssym = NULL;
8106 ssymbuf->count = shndx_count;
8107 ssymbuf->st_shndx = 0;
8108 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8109 {
8110 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8111 {
8112 ssymhead++;
8113 ssymhead->ssym = ssym;
8114 ssymhead->count = 0;
8115 ssymhead->st_shndx = (*ind)->st_shndx;
8116 }
8117 ssym->st_name = (*ind)->st_name;
8118 ssym->st_info = (*ind)->st_info;
8119 ssym->st_other = (*ind)->st_other;
8120 ssymhead->count++;
8121 }
ef53be89 8122 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8123 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8124 == total_size));
4d269e42
AM
8125
8126 free (indbuf);
8127 return ssymbuf;
8128}
8129
8130/* Check if 2 sections define the same set of local and global
8131 symbols. */
8132
8f317e31 8133static bfd_boolean
4d269e42
AM
8134bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8135 struct bfd_link_info *info)
8136{
8137 bfd *bfd1, *bfd2;
8138 const struct elf_backend_data *bed1, *bed2;
8139 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8140 size_t symcount1, symcount2;
4d269e42
AM
8141 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8142 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8143 Elf_Internal_Sym *isym, *isymend;
8144 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8145 size_t count1, count2, i;
cb33740c 8146 unsigned int shndx1, shndx2;
4d269e42
AM
8147 bfd_boolean result;
8148
8149 bfd1 = sec1->owner;
8150 bfd2 = sec2->owner;
8151
4d269e42
AM
8152 /* Both sections have to be in ELF. */
8153 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8154 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8155 return FALSE;
8156
8157 if (elf_section_type (sec1) != elf_section_type (sec2))
8158 return FALSE;
8159
4d269e42
AM
8160 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8161 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8162 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8163 return FALSE;
8164
8165 bed1 = get_elf_backend_data (bfd1);
8166 bed2 = get_elf_backend_data (bfd2);
8167 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8168 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8169 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8170 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8171
8172 if (symcount1 == 0 || symcount2 == 0)
8173 return FALSE;
8174
8175 result = FALSE;
8176 isymbuf1 = NULL;
8177 isymbuf2 = NULL;
a50b1753
NC
8178 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8179 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8180
8181 if (ssymbuf1 == NULL)
8182 {
8183 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8184 NULL, NULL, NULL);
8185 if (isymbuf1 == NULL)
8186 goto done;
8187
67411cbf 8188 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8189 {
8190 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8191 elf_tdata (bfd1)->symbuf = ssymbuf1;
8192 }
4d269e42
AM
8193 }
8194
8195 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8196 {
8197 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8198 NULL, NULL, NULL);
8199 if (isymbuf2 == NULL)
8200 goto done;
8201
67411cbf 8202 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8203 {
8204 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8205 elf_tdata (bfd2)->symbuf = ssymbuf2;
8206 }
4d269e42
AM
8207 }
8208
8209 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8210 {
8211 /* Optimized faster version. */
ef53be89 8212 size_t lo, hi, mid;
4d269e42
AM
8213 struct elf_symbol *symp;
8214 struct elf_symbuf_symbol *ssym, *ssymend;
8215
8216 lo = 0;
8217 hi = ssymbuf1->count;
8218 ssymbuf1++;
8219 count1 = 0;
8220 while (lo < hi)
8221 {
8222 mid = (lo + hi) / 2;
cb33740c 8223 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8224 hi = mid;
cb33740c 8225 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8226 lo = mid + 1;
8227 else
8228 {
8229 count1 = ssymbuf1[mid].count;
8230 ssymbuf1 += mid;
8231 break;
8232 }
8233 }
8234
8235 lo = 0;
8236 hi = ssymbuf2->count;
8237 ssymbuf2++;
8238 count2 = 0;
8239 while (lo < hi)
8240 {
8241 mid = (lo + hi) / 2;
cb33740c 8242 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8243 hi = mid;
cb33740c 8244 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8245 lo = mid + 1;
8246 else
8247 {
8248 count2 = ssymbuf2[mid].count;
8249 ssymbuf2 += mid;
8250 break;
8251 }
8252 }
8253
8254 if (count1 == 0 || count2 == 0 || count1 != count2)
8255 goto done;
8256
ca4be51c
AM
8257 symtable1
8258 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8259 symtable2
8260 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8261 if (symtable1 == NULL || symtable2 == NULL)
8262 goto done;
8263
8264 symp = symtable1;
8265 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8266 ssym < ssymend; ssym++, symp++)
8267 {
8268 symp->u.ssym = ssym;
8269 symp->name = bfd_elf_string_from_elf_section (bfd1,
8270 hdr1->sh_link,
8271 ssym->st_name);
8272 }
8273
8274 symp = symtable2;
8275 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8276 ssym < ssymend; ssym++, symp++)
8277 {
8278 symp->u.ssym = ssym;
8279 symp->name = bfd_elf_string_from_elf_section (bfd2,
8280 hdr2->sh_link,
8281 ssym->st_name);
8282 }
8283
8284 /* Sort symbol by name. */
8285 qsort (symtable1, count1, sizeof (struct elf_symbol),
8286 elf_sym_name_compare);
8287 qsort (symtable2, count1, sizeof (struct elf_symbol),
8288 elf_sym_name_compare);
8289
8290 for (i = 0; i < count1; i++)
8291 /* Two symbols must have the same binding, type and name. */
8292 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8293 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8294 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8295 goto done;
8296
8297 result = TRUE;
8298 goto done;
8299 }
8300
a50b1753
NC
8301 symtable1 = (struct elf_symbol *)
8302 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8303 symtable2 = (struct elf_symbol *)
8304 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8305 if (symtable1 == NULL || symtable2 == NULL)
8306 goto done;
8307
8308 /* Count definitions in the section. */
8309 count1 = 0;
8310 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8311 if (isym->st_shndx == shndx1)
4d269e42
AM
8312 symtable1[count1++].u.isym = isym;
8313
8314 count2 = 0;
8315 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8316 if (isym->st_shndx == shndx2)
4d269e42
AM
8317 symtable2[count2++].u.isym = isym;
8318
8319 if (count1 == 0 || count2 == 0 || count1 != count2)
8320 goto done;
8321
8322 for (i = 0; i < count1; i++)
8323 symtable1[i].name
8324 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8325 symtable1[i].u.isym->st_name);
8326
8327 for (i = 0; i < count2; i++)
8328 symtable2[i].name
8329 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8330 symtable2[i].u.isym->st_name);
8331
8332 /* Sort symbol by name. */
8333 qsort (symtable1, count1, sizeof (struct elf_symbol),
8334 elf_sym_name_compare);
8335 qsort (symtable2, count1, sizeof (struct elf_symbol),
8336 elf_sym_name_compare);
8337
8338 for (i = 0; i < count1; i++)
8339 /* Two symbols must have the same binding, type and name. */
8340 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8341 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8342 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8343 goto done;
8344
8345 result = TRUE;
8346
dc1e8a47 8347 done:
c9594989
AM
8348 free (symtable1);
8349 free (symtable2);
8350 free (isymbuf1);
8351 free (isymbuf2);
4d269e42
AM
8352
8353 return result;
8354}
8355
8356/* Return TRUE if 2 section types are compatible. */
8357
8358bfd_boolean
8359_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8360 bfd *bbfd, const asection *bsec)
8361{
8362 if (asec == NULL
8363 || bsec == NULL
8364 || abfd->xvec->flavour != bfd_target_elf_flavour
8365 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8366 return TRUE;
8367
8368 return elf_section_type (asec) == elf_section_type (bsec);
8369}
8370\f
c152c796
AM
8371/* Final phase of ELF linker. */
8372
8373/* A structure we use to avoid passing large numbers of arguments. */
8374
8375struct elf_final_link_info
8376{
8377 /* General link information. */
8378 struct bfd_link_info *info;
8379 /* Output BFD. */
8380 bfd *output_bfd;
8381 /* Symbol string table. */
ef10c3ac 8382 struct elf_strtab_hash *symstrtab;
c152c796
AM
8383 /* .hash section. */
8384 asection *hash_sec;
8385 /* symbol version section (.gnu.version). */
8386 asection *symver_sec;
8387 /* Buffer large enough to hold contents of any section. */
8388 bfd_byte *contents;
8389 /* Buffer large enough to hold external relocs of any section. */
8390 void *external_relocs;
8391 /* Buffer large enough to hold internal relocs of any section. */
8392 Elf_Internal_Rela *internal_relocs;
8393 /* Buffer large enough to hold external local symbols of any input
8394 BFD. */
8395 bfd_byte *external_syms;
8396 /* And a buffer for symbol section indices. */
8397 Elf_External_Sym_Shndx *locsym_shndx;
8398 /* Buffer large enough to hold internal local symbols of any input
8399 BFD. */
8400 Elf_Internal_Sym *internal_syms;
8401 /* Array large enough to hold a symbol index for each local symbol
8402 of any input BFD. */
8403 long *indices;
8404 /* Array large enough to hold a section pointer for each local
8405 symbol of any input BFD. */
8406 asection **sections;
ef10c3ac 8407 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8408 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8409 /* Number of STT_FILE syms seen. */
8410 size_t filesym_count;
c152c796
AM
8411};
8412
8413/* This struct is used to pass information to elf_link_output_extsym. */
8414
8415struct elf_outext_info
8416{
8417 bfd_boolean failed;
8418 bfd_boolean localsyms;
34a79995 8419 bfd_boolean file_sym_done;
8b127cbc 8420 struct elf_final_link_info *flinfo;
c152c796
AM
8421};
8422
d9352518
DB
8423
8424/* Support for evaluating a complex relocation.
8425
8426 Complex relocations are generalized, self-describing relocations. The
8427 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8428 relocations themselves.
d9352518
DB
8429
8430 The relocations are use a reserved elf-wide relocation type code (R_RELC
8431 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8432 information (start bit, end bit, word width, etc) into the addend. This
8433 information is extracted from CGEN-generated operand tables within gas.
8434
8435 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8436 internal) representing prefix-notation expressions, including but not
8437 limited to those sorts of expressions normally encoded as addends in the
8438 addend field. The symbol mangling format is:
8439
8440 <node> := <literal>
07d6d2b8
AM
8441 | <unary-operator> ':' <node>
8442 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8443 ;
8444
8445 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8446 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8447 | '#' <hexdigits>
8448 ;
8449
8450 <binary-operator> := as in C
8451 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8452
8453static void
a0c8462f
AM
8454set_symbol_value (bfd *bfd_with_globals,
8455 Elf_Internal_Sym *isymbuf,
8456 size_t locsymcount,
8457 size_t symidx,
8458 bfd_vma val)
d9352518 8459{
8977835c
AM
8460 struct elf_link_hash_entry **sym_hashes;
8461 struct elf_link_hash_entry *h;
8462 size_t extsymoff = locsymcount;
d9352518 8463
8977835c 8464 if (symidx < locsymcount)
d9352518 8465 {
8977835c
AM
8466 Elf_Internal_Sym *sym;
8467
8468 sym = isymbuf + symidx;
8469 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8470 {
8471 /* It is a local symbol: move it to the
8472 "absolute" section and give it a value. */
8473 sym->st_shndx = SHN_ABS;
8474 sym->st_value = val;
8475 return;
8476 }
8477 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8478 extsymoff = 0;
d9352518 8479 }
8977835c
AM
8480
8481 /* It is a global symbol: set its link type
8482 to "defined" and give it a value. */
8483
8484 sym_hashes = elf_sym_hashes (bfd_with_globals);
8485 h = sym_hashes [symidx - extsymoff];
8486 while (h->root.type == bfd_link_hash_indirect
8487 || h->root.type == bfd_link_hash_warning)
8488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8489 h->root.type = bfd_link_hash_defined;
8490 h->root.u.def.value = val;
8491 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8492}
8493
a0c8462f
AM
8494static bfd_boolean
8495resolve_symbol (const char *name,
8496 bfd *input_bfd,
8b127cbc 8497 struct elf_final_link_info *flinfo,
a0c8462f
AM
8498 bfd_vma *result,
8499 Elf_Internal_Sym *isymbuf,
8500 size_t locsymcount)
d9352518 8501{
a0c8462f
AM
8502 Elf_Internal_Sym *sym;
8503 struct bfd_link_hash_entry *global_entry;
8504 const char *candidate = NULL;
8505 Elf_Internal_Shdr *symtab_hdr;
8506 size_t i;
8507
d9352518
DB
8508 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8509
8510 for (i = 0; i < locsymcount; ++ i)
8511 {
8977835c 8512 sym = isymbuf + i;
d9352518
DB
8513
8514 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8515 continue;
8516
8517 candidate = bfd_elf_string_from_elf_section (input_bfd,
8518 symtab_hdr->sh_link,
8519 sym->st_name);
8520#ifdef DEBUG
0f02bbd9
AM
8521 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8522 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8523#endif
8524 if (candidate && strcmp (candidate, name) == 0)
8525 {
8b127cbc 8526 asection *sec = flinfo->sections [i];
d9352518 8527
0f02bbd9
AM
8528 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8529 *result += sec->output_offset + sec->output_section->vma;
d9352518 8530#ifdef DEBUG
0f02bbd9
AM
8531 printf ("Found symbol with value %8.8lx\n",
8532 (unsigned long) *result);
d9352518
DB
8533#endif
8534 return TRUE;
8535 }
8536 }
8537
8538 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8539 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8540 FALSE, FALSE, TRUE);
d9352518
DB
8541 if (!global_entry)
8542 return FALSE;
a0c8462f 8543
d9352518
DB
8544 if (global_entry->type == bfd_link_hash_defined
8545 || global_entry->type == bfd_link_hash_defweak)
8546 {
a0c8462f
AM
8547 *result = (global_entry->u.def.value
8548 + global_entry->u.def.section->output_section->vma
8549 + global_entry->u.def.section->output_offset);
d9352518 8550#ifdef DEBUG
0f02bbd9
AM
8551 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8552 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8553#endif
8554 return TRUE;
a0c8462f 8555 }
d9352518 8556
d9352518
DB
8557 return FALSE;
8558}
8559
37b01f6a
DG
8560/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8561 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8562 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8563
d9352518 8564static bfd_boolean
a0c8462f
AM
8565resolve_section (const char *name,
8566 asection *sections,
37b01f6a
DG
8567 bfd_vma *result,
8568 bfd * abfd)
d9352518 8569{
a0c8462f
AM
8570 asection *curr;
8571 unsigned int len;
d9352518 8572
a0c8462f 8573 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8574 if (strcmp (curr->name, name) == 0)
8575 {
8576 *result = curr->vma;
8577 return TRUE;
8578 }
8579
8580 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8581 /* FIXME: This could be coded more efficiently... */
a0c8462f 8582 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8583 {
8584 len = strlen (curr->name);
a0c8462f 8585 if (len > strlen (name))
d9352518
DB
8586 continue;
8587
8588 if (strncmp (curr->name, name, len) == 0)
8589 {
8590 if (strncmp (".end", name + len, 4) == 0)
8591 {
61826503 8592 *result = (curr->vma
bb294208 8593 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8594 return TRUE;
8595 }
8596
8597 /* Insert more pseudo-section names here, if you like. */
8598 }
8599 }
a0c8462f 8600
d9352518
DB
8601 return FALSE;
8602}
8603
8604static void
a0c8462f 8605undefined_reference (const char *reftype, const char *name)
d9352518 8606{
695344c0 8607 /* xgettext:c-format */
a0c8462f
AM
8608 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8609 reftype, name);
d9352518
DB
8610}
8611
8612static bfd_boolean
a0c8462f
AM
8613eval_symbol (bfd_vma *result,
8614 const char **symp,
8615 bfd *input_bfd,
8b127cbc 8616 struct elf_final_link_info *flinfo,
a0c8462f
AM
8617 bfd_vma dot,
8618 Elf_Internal_Sym *isymbuf,
8619 size_t locsymcount,
8620 int signed_p)
d9352518 8621{
4b93929b
NC
8622 size_t len;
8623 size_t symlen;
a0c8462f
AM
8624 bfd_vma a;
8625 bfd_vma b;
4b93929b 8626 char symbuf[4096];
0f02bbd9 8627 const char *sym = *symp;
a0c8462f
AM
8628 const char *symend;
8629 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8630
8631 len = strlen (sym);
8632 symend = sym + len;
8633
4b93929b 8634 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8635 {
8636 bfd_set_error (bfd_error_invalid_operation);
8637 return FALSE;
8638 }
a0c8462f 8639
d9352518
DB
8640 switch (* sym)
8641 {
8642 case '.':
0f02bbd9
AM
8643 *result = dot;
8644 *symp = sym + 1;
d9352518
DB
8645 return TRUE;
8646
8647 case '#':
0f02bbd9
AM
8648 ++sym;
8649 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8650 return TRUE;
8651
8652 case 'S':
8653 symbol_is_section = TRUE;
1a0670f3 8654 /* Fall through. */
a0c8462f 8655 case 's':
0f02bbd9
AM
8656 ++sym;
8657 symlen = strtol (sym, (char **) symp, 10);
8658 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8659
4b93929b 8660 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8661 {
8662 bfd_set_error (bfd_error_invalid_operation);
8663 return FALSE;
8664 }
8665
8666 memcpy (symbuf, sym, symlen);
a0c8462f 8667 symbuf[symlen] = '\0';
0f02bbd9 8668 *symp = sym + symlen;
a0c8462f
AM
8669
8670 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8671 the symbol as a section, or vice-versa. so we're pretty liberal in our
8672 interpretation here; section means "try section first", not "must be a
8673 section", and likewise with symbol. */
8674
a0c8462f 8675 if (symbol_is_section)
d9352518 8676 {
37b01f6a 8677 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8678 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8679 isymbuf, locsymcount))
d9352518
DB
8680 {
8681 undefined_reference ("section", symbuf);
8682 return FALSE;
8683 }
a0c8462f
AM
8684 }
8685 else
d9352518 8686 {
8b127cbc 8687 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8688 isymbuf, locsymcount)
8b127cbc 8689 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8690 result, input_bfd))
d9352518
DB
8691 {
8692 undefined_reference ("symbol", symbuf);
8693 return FALSE;
8694 }
8695 }
8696
8697 return TRUE;
a0c8462f 8698
d9352518
DB
8699 /* All that remains are operators. */
8700
8701#define UNARY_OP(op) \
8702 if (strncmp (sym, #op, strlen (#op)) == 0) \
8703 { \
8704 sym += strlen (#op); \
a0c8462f
AM
8705 if (*sym == ':') \
8706 ++sym; \
0f02bbd9 8707 *symp = sym; \
8b127cbc 8708 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8709 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8710 return FALSE; \
8711 if (signed_p) \
0f02bbd9 8712 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8713 else \
8714 *result = op a; \
d9352518
DB
8715 return TRUE; \
8716 }
8717
8718#define BINARY_OP(op) \
8719 if (strncmp (sym, #op, strlen (#op)) == 0) \
8720 { \
8721 sym += strlen (#op); \
a0c8462f
AM
8722 if (*sym == ':') \
8723 ++sym; \
0f02bbd9 8724 *symp = sym; \
8b127cbc 8725 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8726 isymbuf, locsymcount, signed_p)) \
a0c8462f 8727 return FALSE; \
0f02bbd9 8728 ++*symp; \
8b127cbc 8729 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8730 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8731 return FALSE; \
8732 if (signed_p) \
0f02bbd9 8733 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8734 else \
8735 *result = a op b; \
d9352518
DB
8736 return TRUE; \
8737 }
8738
8739 default:
8740 UNARY_OP (0-);
8741 BINARY_OP (<<);
8742 BINARY_OP (>>);
8743 BINARY_OP (==);
8744 BINARY_OP (!=);
8745 BINARY_OP (<=);
8746 BINARY_OP (>=);
8747 BINARY_OP (&&);
8748 BINARY_OP (||);
8749 UNARY_OP (~);
8750 UNARY_OP (!);
8751 BINARY_OP (*);
8752 BINARY_OP (/);
8753 BINARY_OP (%);
8754 BINARY_OP (^);
8755 BINARY_OP (|);
8756 BINARY_OP (&);
8757 BINARY_OP (+);
8758 BINARY_OP (-);
8759 BINARY_OP (<);
8760 BINARY_OP (>);
8761#undef UNARY_OP
8762#undef BINARY_OP
8763 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8764 bfd_set_error (bfd_error_invalid_operation);
8765 return FALSE;
8766 }
8767}
8768
d9352518 8769static void
a0c8462f
AM
8770put_value (bfd_vma size,
8771 unsigned long chunksz,
8772 bfd *input_bfd,
8773 bfd_vma x,
8774 bfd_byte *location)
d9352518
DB
8775{
8776 location += (size - chunksz);
8777
41cd1ad1 8778 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8779 {
8780 switch (chunksz)
8781 {
d9352518
DB
8782 case 1:
8783 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8784 x >>= 8;
d9352518
DB
8785 break;
8786 case 2:
8787 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8788 x >>= 16;
d9352518
DB
8789 break;
8790 case 4:
8791 bfd_put_32 (input_bfd, x, location);
65164438
NC
8792 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8793 x >>= 16;
8794 x >>= 16;
d9352518 8795 break;
d9352518 8796#ifdef BFD64
41cd1ad1 8797 case 8:
d9352518 8798 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8799 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8800 x >>= 32;
8801 x >>= 32;
8802 break;
d9352518 8803#endif
41cd1ad1
NC
8804 default:
8805 abort ();
d9352518
DB
8806 break;
8807 }
8808 }
8809}
8810
a0c8462f
AM
8811static bfd_vma
8812get_value (bfd_vma size,
8813 unsigned long chunksz,
8814 bfd *input_bfd,
8815 bfd_byte *location)
d9352518 8816{
9b239e0e 8817 int shift;
d9352518
DB
8818 bfd_vma x = 0;
8819
9b239e0e
NC
8820 /* Sanity checks. */
8821 BFD_ASSERT (chunksz <= sizeof (x)
8822 && size >= chunksz
8823 && chunksz != 0
8824 && (size % chunksz) == 0
8825 && input_bfd != NULL
8826 && location != NULL);
8827
8828 if (chunksz == sizeof (x))
8829 {
8830 BFD_ASSERT (size == chunksz);
8831
8832 /* Make sure that we do not perform an undefined shift operation.
8833 We know that size == chunksz so there will only be one iteration
8834 of the loop below. */
8835 shift = 0;
8836 }
8837 else
8838 shift = 8 * chunksz;
8839
a0c8462f 8840 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8841 {
8842 switch (chunksz)
8843 {
d9352518 8844 case 1:
9b239e0e 8845 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8846 break;
8847 case 2:
9b239e0e 8848 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8849 break;
8850 case 4:
9b239e0e 8851 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8852 break;
d9352518 8853#ifdef BFD64
9b239e0e
NC
8854 case 8:
8855 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8856 break;
9b239e0e
NC
8857#endif
8858 default:
8859 abort ();
d9352518
DB
8860 }
8861 }
8862 return x;
8863}
8864
a0c8462f
AM
8865static void
8866decode_complex_addend (unsigned long *start, /* in bits */
8867 unsigned long *oplen, /* in bits */
8868 unsigned long *len, /* in bits */
8869 unsigned long *wordsz, /* in bytes */
8870 unsigned long *chunksz, /* in bytes */
8871 unsigned long *lsb0_p,
8872 unsigned long *signed_p,
8873 unsigned long *trunc_p,
8874 unsigned long encoded)
d9352518 8875{
07d6d2b8
AM
8876 * start = encoded & 0x3F;
8877 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8878 * oplen = (encoded >> 12) & 0x3F;
8879 * wordsz = (encoded >> 18) & 0xF;
8880 * chunksz = (encoded >> 22) & 0xF;
8881 * lsb0_p = (encoded >> 27) & 1;
8882 * signed_p = (encoded >> 28) & 1;
8883 * trunc_p = (encoded >> 29) & 1;
8884}
8885
cdfeee4f 8886bfd_reloc_status_type
0f02bbd9 8887bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8888 asection *input_section,
0f02bbd9
AM
8889 bfd_byte *contents,
8890 Elf_Internal_Rela *rel,
8891 bfd_vma relocation)
d9352518 8892{
0f02bbd9
AM
8893 bfd_vma shift, x, mask;
8894 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8895 bfd_reloc_status_type r;
bb294208 8896 bfd_size_type octets;
d9352518
DB
8897
8898 /* Perform this reloc, since it is complex.
8899 (this is not to say that it necessarily refers to a complex
8900 symbol; merely that it is a self-describing CGEN based reloc.
8901 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8902 word size, etc) encoded within it.). */
d9352518 8903
a0c8462f
AM
8904 decode_complex_addend (&start, &oplen, &len, &wordsz,
8905 &chunksz, &lsb0_p, &signed_p,
8906 &trunc_p, rel->r_addend);
d9352518
DB
8907
8908 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8909
8910 if (lsb0_p)
8911 shift = (start + 1) - len;
8912 else
8913 shift = (8 * wordsz) - (start + len);
8914
bb294208
AM
8915 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8916 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8917
8918#ifdef DEBUG
8919 printf ("Doing complex reloc: "
8920 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8921 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8922 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8923 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8924 oplen, (unsigned long) x, (unsigned long) mask,
8925 (unsigned long) relocation);
d9352518
DB
8926#endif
8927
cdfeee4f 8928 r = bfd_reloc_ok;
d9352518 8929 if (! trunc_p)
cdfeee4f
AM
8930 /* Now do an overflow check. */
8931 r = bfd_check_overflow ((signed_p
8932 ? complain_overflow_signed
8933 : complain_overflow_unsigned),
8934 len, 0, (8 * wordsz),
8935 relocation);
a0c8462f 8936
d9352518
DB
8937 /* Do the deed. */
8938 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8939
8940#ifdef DEBUG
8941 printf (" relocation: %8.8lx\n"
8942 " shifted mask: %8.8lx\n"
8943 " shifted/masked reloc: %8.8lx\n"
8944 " result: %8.8lx\n",
9ccb8af9
AM
8945 (unsigned long) relocation, (unsigned long) (mask << shift),
8946 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8947#endif
bb294208 8948 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 8949 return r;
d9352518
DB
8950}
8951
0e287786
AM
8952/* Functions to read r_offset from external (target order) reloc
8953 entry. Faster than bfd_getl32 et al, because we let the compiler
8954 know the value is aligned. */
53df40a4 8955
0e287786
AM
8956static bfd_vma
8957ext32l_r_offset (const void *p)
53df40a4
AM
8958{
8959 union aligned32
8960 {
8961 uint32_t v;
8962 unsigned char c[4];
8963 };
8964 const union aligned32 *a
0e287786 8965 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8966
8967 uint32_t aval = ( (uint32_t) a->c[0]
8968 | (uint32_t) a->c[1] << 8
8969 | (uint32_t) a->c[2] << 16
8970 | (uint32_t) a->c[3] << 24);
0e287786 8971 return aval;
53df40a4
AM
8972}
8973
0e287786
AM
8974static bfd_vma
8975ext32b_r_offset (const void *p)
53df40a4
AM
8976{
8977 union aligned32
8978 {
8979 uint32_t v;
8980 unsigned char c[4];
8981 };
8982 const union aligned32 *a
0e287786 8983 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8984
8985 uint32_t aval = ( (uint32_t) a->c[0] << 24
8986 | (uint32_t) a->c[1] << 16
8987 | (uint32_t) a->c[2] << 8
8988 | (uint32_t) a->c[3]);
0e287786 8989 return aval;
53df40a4
AM
8990}
8991
8992#ifdef BFD_HOST_64_BIT
0e287786
AM
8993static bfd_vma
8994ext64l_r_offset (const void *p)
53df40a4
AM
8995{
8996 union aligned64
8997 {
8998 uint64_t v;
8999 unsigned char c[8];
9000 };
9001 const union aligned64 *a
0e287786 9002 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9003
9004 uint64_t aval = ( (uint64_t) a->c[0]
9005 | (uint64_t) a->c[1] << 8
9006 | (uint64_t) a->c[2] << 16
9007 | (uint64_t) a->c[3] << 24
9008 | (uint64_t) a->c[4] << 32
9009 | (uint64_t) a->c[5] << 40
9010 | (uint64_t) a->c[6] << 48
9011 | (uint64_t) a->c[7] << 56);
0e287786 9012 return aval;
53df40a4
AM
9013}
9014
0e287786
AM
9015static bfd_vma
9016ext64b_r_offset (const void *p)
53df40a4
AM
9017{
9018 union aligned64
9019 {
9020 uint64_t v;
9021 unsigned char c[8];
9022 };
9023 const union aligned64 *a
0e287786 9024 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9025
9026 uint64_t aval = ( (uint64_t) a->c[0] << 56
9027 | (uint64_t) a->c[1] << 48
9028 | (uint64_t) a->c[2] << 40
9029 | (uint64_t) a->c[3] << 32
9030 | (uint64_t) a->c[4] << 24
9031 | (uint64_t) a->c[5] << 16
9032 | (uint64_t) a->c[6] << 8
9033 | (uint64_t) a->c[7]);
0e287786 9034 return aval;
53df40a4
AM
9035}
9036#endif
9037
c152c796
AM
9038/* When performing a relocatable link, the input relocations are
9039 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9040 referenced must be updated. Update all the relocations found in
9041 RELDATA. */
c152c796 9042
bca6d0e3 9043static bfd_boolean
c152c796 9044elf_link_adjust_relocs (bfd *abfd,
9eaff861 9045 asection *sec,
28dbcedc 9046 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9047 bfd_boolean sort,
9048 struct bfd_link_info *info)
c152c796
AM
9049{
9050 unsigned int i;
9051 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9052 bfd_byte *erela;
9053 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9054 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9055 bfd_vma r_type_mask;
9056 int r_sym_shift;
d4730f92
BS
9057 unsigned int count = reldata->count;
9058 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9059
d4730f92 9060 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9061 {
9062 swap_in = bed->s->swap_reloc_in;
9063 swap_out = bed->s->swap_reloc_out;
9064 }
d4730f92 9065 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9066 {
9067 swap_in = bed->s->swap_reloca_in;
9068 swap_out = bed->s->swap_reloca_out;
9069 }
9070 else
9071 abort ();
9072
9073 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9074 abort ();
9075
9076 if (bed->s->arch_size == 32)
9077 {
9078 r_type_mask = 0xff;
9079 r_sym_shift = 8;
9080 }
9081 else
9082 {
9083 r_type_mask = 0xffffffff;
9084 r_sym_shift = 32;
9085 }
9086
d4730f92
BS
9087 erela = reldata->hdr->contents;
9088 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9089 {
9090 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9091 unsigned int j;
9092
9093 if (*rel_hash == NULL)
9094 continue;
9095
10bbbc1d
NC
9096 if ((*rel_hash)->indx == -2
9097 && info->gc_sections
9098 && ! info->gc_keep_exported)
9099 {
9100 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9101 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9102 abfd, sec,
9103 (*rel_hash)->root.root.string);
9793eb77 9104 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9105 abfd, sec);
10bbbc1d
NC
9106 bfd_set_error (bfd_error_invalid_operation);
9107 return FALSE;
9108 }
c152c796
AM
9109 BFD_ASSERT ((*rel_hash)->indx >= 0);
9110
9111 (*swap_in) (abfd, erela, irela);
9112 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9113 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9114 | (irela[j].r_info & r_type_mask));
9115 (*swap_out) (abfd, irela, erela);
9116 }
53df40a4 9117
9eaff861
AO
9118 if (bed->elf_backend_update_relocs)
9119 (*bed->elf_backend_update_relocs) (sec, reldata);
9120
0e287786 9121 if (sort && count != 0)
53df40a4 9122 {
0e287786
AM
9123 bfd_vma (*ext_r_off) (const void *);
9124 bfd_vma r_off;
9125 size_t elt_size;
9126 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9127 bfd_byte *buf = NULL;
28dbcedc
AM
9128
9129 if (bed->s->arch_size == 32)
9130 {
9131 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9132 ext_r_off = ext32l_r_offset;
28dbcedc 9133 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9134 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9135 else
9136 abort ();
9137 }
53df40a4 9138 else
28dbcedc 9139 {
53df40a4 9140#ifdef BFD_HOST_64_BIT
28dbcedc 9141 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9142 ext_r_off = ext64l_r_offset;
28dbcedc 9143 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9144 ext_r_off = ext64b_r_offset;
28dbcedc 9145 else
53df40a4 9146#endif
28dbcedc
AM
9147 abort ();
9148 }
0e287786 9149
bca6d0e3
AM
9150 /* Must use a stable sort here. A modified insertion sort,
9151 since the relocs are mostly sorted already. */
0e287786
AM
9152 elt_size = reldata->hdr->sh_entsize;
9153 base = reldata->hdr->contents;
9154 end = base + count * elt_size;
bca6d0e3 9155 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9156 abort ();
9157
9158 /* Ensure the first element is lowest. This acts as a sentinel,
9159 speeding the main loop below. */
9160 r_off = (*ext_r_off) (base);
9161 for (p = loc = base; (p += elt_size) < end; )
9162 {
9163 bfd_vma r_off2 = (*ext_r_off) (p);
9164 if (r_off > r_off2)
9165 {
9166 r_off = r_off2;
9167 loc = p;
9168 }
9169 }
9170 if (loc != base)
9171 {
9172 /* Don't just swap *base and *loc as that changes the order
9173 of the original base[0] and base[1] if they happen to
9174 have the same r_offset. */
bca6d0e3
AM
9175 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9176 memcpy (onebuf, loc, elt_size);
0e287786 9177 memmove (base + elt_size, base, loc - base);
bca6d0e3 9178 memcpy (base, onebuf, elt_size);
0e287786
AM
9179 }
9180
b29b8669 9181 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9182 {
9183 /* base to p is sorted, *p is next to insert. */
9184 r_off = (*ext_r_off) (p);
9185 /* Search the sorted region for location to insert. */
9186 loc = p - elt_size;
9187 while (r_off < (*ext_r_off) (loc))
9188 loc -= elt_size;
9189 loc += elt_size;
9190 if (loc != p)
9191 {
bca6d0e3
AM
9192 /* Chances are there is a run of relocs to insert here,
9193 from one of more input files. Files are not always
9194 linked in order due to the way elf_link_input_bfd is
9195 called. See pr17666. */
9196 size_t sortlen = p - loc;
9197 bfd_vma r_off2 = (*ext_r_off) (loc);
9198 size_t runlen = elt_size;
9199 size_t buf_size = 96 * 1024;
9200 while (p + runlen < end
9201 && (sortlen <= buf_size
9202 || runlen + elt_size <= buf_size)
9203 && r_off2 > (*ext_r_off) (p + runlen))
9204 runlen += elt_size;
9205 if (buf == NULL)
9206 {
9207 buf = bfd_malloc (buf_size);
9208 if (buf == NULL)
9209 return FALSE;
9210 }
9211 if (runlen < sortlen)
9212 {
9213 memcpy (buf, p, runlen);
9214 memmove (loc + runlen, loc, sortlen);
9215 memcpy (loc, buf, runlen);
9216 }
9217 else
9218 {
9219 memcpy (buf, loc, sortlen);
9220 memmove (loc, p, runlen);
9221 memcpy (loc + runlen, buf, sortlen);
9222 }
b29b8669 9223 p += runlen - elt_size;
0e287786
AM
9224 }
9225 }
9226 /* Hashes are no longer valid. */
28dbcedc
AM
9227 free (reldata->hashes);
9228 reldata->hashes = NULL;
bca6d0e3 9229 free (buf);
53df40a4 9230 }
bca6d0e3 9231 return TRUE;
c152c796
AM
9232}
9233
9234struct elf_link_sort_rela
9235{
9236 union {
9237 bfd_vma offset;
9238 bfd_vma sym_mask;
9239 } u;
9240 enum elf_reloc_type_class type;
9241 /* We use this as an array of size int_rels_per_ext_rel. */
9242 Elf_Internal_Rela rela[1];
9243};
9244
dcea6a95
AM
9245/* qsort stability here and for cmp2 is only an issue if multiple
9246 dynamic relocations are emitted at the same address. But targets
9247 that apply a series of dynamic relocations each operating on the
9248 result of the prior relocation can't use -z combreloc as
9249 implemented anyway. Such schemes tend to be broken by sorting on
9250 symbol index. That leaves dynamic NONE relocs as the only other
9251 case where ld might emit multiple relocs at the same address, and
9252 those are only emitted due to target bugs. */
9253
c152c796
AM
9254static int
9255elf_link_sort_cmp1 (const void *A, const void *B)
9256{
a50b1753
NC
9257 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9258 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9259 int relativea, relativeb;
9260
9261 relativea = a->type == reloc_class_relative;
9262 relativeb = b->type == reloc_class_relative;
9263
9264 if (relativea < relativeb)
9265 return 1;
9266 if (relativea > relativeb)
9267 return -1;
9268 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9269 return -1;
9270 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9271 return 1;
9272 if (a->rela->r_offset < b->rela->r_offset)
9273 return -1;
9274 if (a->rela->r_offset > b->rela->r_offset)
9275 return 1;
9276 return 0;
9277}
9278
9279static int
9280elf_link_sort_cmp2 (const void *A, const void *B)
9281{
a50b1753
NC
9282 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9283 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9284
7e612e98 9285 if (a->type < b->type)
c152c796 9286 return -1;
7e612e98 9287 if (a->type > b->type)
c152c796 9288 return 1;
7e612e98 9289 if (a->u.offset < b->u.offset)
c152c796 9290 return -1;
7e612e98 9291 if (a->u.offset > b->u.offset)
c152c796
AM
9292 return 1;
9293 if (a->rela->r_offset < b->rela->r_offset)
9294 return -1;
9295 if (a->rela->r_offset > b->rela->r_offset)
9296 return 1;
9297 return 0;
9298}
9299
9300static size_t
9301elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9302{
3410fea8 9303 asection *dynamic_relocs;
fc66a176
L
9304 asection *rela_dyn;
9305 asection *rel_dyn;
c152c796
AM
9306 bfd_size_type count, size;
9307 size_t i, ret, sort_elt, ext_size;
9308 bfd_byte *sort, *s_non_relative, *p;
9309 struct elf_link_sort_rela *sq;
9310 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9311 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9312 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9313 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9314 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9315 struct bfd_link_order *lo;
9316 bfd_vma r_sym_mask;
3410fea8 9317 bfd_boolean use_rela;
c152c796 9318
3410fea8
NC
9319 /* Find a dynamic reloc section. */
9320 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9321 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9322 if (rela_dyn != NULL && rela_dyn->size > 0
9323 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9324 {
3410fea8
NC
9325 bfd_boolean use_rela_initialised = FALSE;
9326
9327 /* This is just here to stop gcc from complaining.
c8e44c6d 9328 Its initialization checking code is not perfect. */
3410fea8
NC
9329 use_rela = TRUE;
9330
9331 /* Both sections are present. Examine the sizes
9332 of the indirect sections to help us choose. */
9333 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9334 if (lo->type == bfd_indirect_link_order)
9335 {
9336 asection *o = lo->u.indirect.section;
9337
9338 if ((o->size % bed->s->sizeof_rela) == 0)
9339 {
9340 if ((o->size % bed->s->sizeof_rel) == 0)
9341 /* Section size is divisible by both rel and rela sizes.
9342 It is of no help to us. */
9343 ;
9344 else
9345 {
9346 /* Section size is only divisible by rela. */
535b785f 9347 if (use_rela_initialised && !use_rela)
3410fea8 9348 {
9793eb77 9349 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9350 "they are in more than one size"),
9351 abfd);
3410fea8
NC
9352 bfd_set_error (bfd_error_invalid_operation);
9353 return 0;
9354 }
9355 else
9356 {
9357 use_rela = TRUE;
9358 use_rela_initialised = TRUE;
9359 }
9360 }
9361 }
9362 else if ((o->size % bed->s->sizeof_rel) == 0)
9363 {
9364 /* Section size is only divisible by rel. */
535b785f 9365 if (use_rela_initialised && use_rela)
3410fea8 9366 {
9793eb77 9367 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9368 "they are in more than one size"),
9369 abfd);
3410fea8
NC
9370 bfd_set_error (bfd_error_invalid_operation);
9371 return 0;
9372 }
9373 else
9374 {
9375 use_rela = FALSE;
9376 use_rela_initialised = TRUE;
9377 }
9378 }
9379 else
9380 {
c8e44c6d
AM
9381 /* The section size is not divisible by either -
9382 something is wrong. */
9793eb77 9383 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9384 "they are of an unknown size"), abfd);
3410fea8
NC
9385 bfd_set_error (bfd_error_invalid_operation);
9386 return 0;
9387 }
9388 }
9389
9390 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9391 if (lo->type == bfd_indirect_link_order)
9392 {
9393 asection *o = lo->u.indirect.section;
9394
9395 if ((o->size % bed->s->sizeof_rela) == 0)
9396 {
9397 if ((o->size % bed->s->sizeof_rel) == 0)
9398 /* Section size is divisible by both rel and rela sizes.
9399 It is of no help to us. */
9400 ;
9401 else
9402 {
9403 /* Section size is only divisible by rela. */
535b785f 9404 if (use_rela_initialised && !use_rela)
3410fea8 9405 {
9793eb77 9406 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9407 "they are in more than one size"),
9408 abfd);
3410fea8
NC
9409 bfd_set_error (bfd_error_invalid_operation);
9410 return 0;
9411 }
9412 else
9413 {
9414 use_rela = TRUE;
9415 use_rela_initialised = TRUE;
9416 }
9417 }
9418 }
9419 else if ((o->size % bed->s->sizeof_rel) == 0)
9420 {
9421 /* Section size is only divisible by rel. */
535b785f 9422 if (use_rela_initialised && use_rela)
3410fea8 9423 {
9793eb77 9424 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9425 "they are in more than one size"),
9426 abfd);
3410fea8
NC
9427 bfd_set_error (bfd_error_invalid_operation);
9428 return 0;
9429 }
9430 else
9431 {
9432 use_rela = FALSE;
9433 use_rela_initialised = TRUE;
9434 }
9435 }
9436 else
9437 {
c8e44c6d
AM
9438 /* The section size is not divisible by either -
9439 something is wrong. */
9793eb77 9440 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9441 "they are of an unknown size"), abfd);
3410fea8
NC
9442 bfd_set_error (bfd_error_invalid_operation);
9443 return 0;
9444 }
9445 }
9446
9447 if (! use_rela_initialised)
9448 /* Make a guess. */
9449 use_rela = TRUE;
c152c796 9450 }
fc66a176
L
9451 else if (rela_dyn != NULL && rela_dyn->size > 0)
9452 use_rela = TRUE;
9453 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9454 use_rela = FALSE;
c152c796 9455 else
fc66a176 9456 return 0;
3410fea8
NC
9457
9458 if (use_rela)
c152c796 9459 {
3410fea8 9460 dynamic_relocs = rela_dyn;
c152c796
AM
9461 ext_size = bed->s->sizeof_rela;
9462 swap_in = bed->s->swap_reloca_in;
9463 swap_out = bed->s->swap_reloca_out;
9464 }
3410fea8
NC
9465 else
9466 {
9467 dynamic_relocs = rel_dyn;
9468 ext_size = bed->s->sizeof_rel;
9469 swap_in = bed->s->swap_reloc_in;
9470 swap_out = bed->s->swap_reloc_out;
9471 }
c152c796
AM
9472
9473 size = 0;
3410fea8 9474 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9475 if (lo->type == bfd_indirect_link_order)
3410fea8 9476 size += lo->u.indirect.section->size;
c152c796 9477
3410fea8 9478 if (size != dynamic_relocs->size)
c152c796
AM
9479 return 0;
9480
9481 sort_elt = (sizeof (struct elf_link_sort_rela)
9482 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9483
9484 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9485 if (count == 0)
9486 return 0;
a50b1753 9487 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9488
c152c796
AM
9489 if (sort == NULL)
9490 {
9491 (*info->callbacks->warning)
9793eb77 9492 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9493 return 0;
9494 }
9495
9496 if (bed->s->arch_size == 32)
9497 r_sym_mask = ~(bfd_vma) 0xff;
9498 else
9499 r_sym_mask = ~(bfd_vma) 0xffffffff;
9500
3410fea8 9501 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9502 if (lo->type == bfd_indirect_link_order)
9503 {
9504 bfd_byte *erel, *erelend;
9505 asection *o = lo->u.indirect.section;
9506
1da212d6
AM
9507 if (o->contents == NULL && o->size != 0)
9508 {
9509 /* This is a reloc section that is being handled as a normal
9510 section. See bfd_section_from_shdr. We can't combine
9511 relocs in this case. */
9512 free (sort);
9513 return 0;
9514 }
c152c796 9515 erel = o->contents;
eea6121a 9516 erelend = o->contents + o->size;
c8e44c6d 9517 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9518
c152c796
AM
9519 while (erel < erelend)
9520 {
9521 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9522
c152c796 9523 (*swap_in) (abfd, erel, s->rela);
7e612e98 9524 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9525 s->u.sym_mask = r_sym_mask;
9526 p += sort_elt;
9527 erel += ext_size;
9528 }
9529 }
9530
9531 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9532
9533 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9534 {
9535 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9536 if (s->type != reloc_class_relative)
9537 break;
9538 }
9539 ret = i;
9540 s_non_relative = p;
9541
9542 sq = (struct elf_link_sort_rela *) s_non_relative;
9543 for (; i < count; i++, p += sort_elt)
9544 {
9545 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9546 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9547 sq = sp;
9548 sp->u.offset = sq->rela->r_offset;
9549 }
9550
9551 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9552
c8e44c6d
AM
9553 struct elf_link_hash_table *htab = elf_hash_table (info);
9554 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9555 {
9556 /* We have plt relocs in .rela.dyn. */
9557 sq = (struct elf_link_sort_rela *) sort;
9558 for (i = 0; i < count; i++)
9559 if (sq[count - i - 1].type != reloc_class_plt)
9560 break;
9561 if (i != 0 && htab->srelplt->size == i * ext_size)
9562 {
9563 struct bfd_link_order **plo;
9564 /* Put srelplt link_order last. This is so the output_offset
9565 set in the next loop is correct for DT_JMPREL. */
9566 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9567 if ((*plo)->type == bfd_indirect_link_order
9568 && (*plo)->u.indirect.section == htab->srelplt)
9569 {
9570 lo = *plo;
9571 *plo = lo->next;
9572 }
9573 else
9574 plo = &(*plo)->next;
9575 *plo = lo;
9576 lo->next = NULL;
9577 dynamic_relocs->map_tail.link_order = lo;
9578 }
9579 }
9580
9581 p = sort;
3410fea8 9582 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9583 if (lo->type == bfd_indirect_link_order)
9584 {
9585 bfd_byte *erel, *erelend;
9586 asection *o = lo->u.indirect.section;
9587
9588 erel = o->contents;
eea6121a 9589 erelend = o->contents + o->size;
c8e44c6d 9590 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9591 while (erel < erelend)
9592 {
9593 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9594 (*swap_out) (abfd, s->rela, erel);
9595 p += sort_elt;
9596 erel += ext_size;
9597 }
9598 }
9599
9600 free (sort);
3410fea8 9601 *psec = dynamic_relocs;
c152c796
AM
9602 return ret;
9603}
9604
ef10c3ac 9605/* Add a symbol to the output symbol string table. */
c152c796 9606
6e0b88f1 9607static int
ef10c3ac
L
9608elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9609 const char *name,
9610 Elf_Internal_Sym *elfsym,
9611 asection *input_sec,
9612 struct elf_link_hash_entry *h)
c152c796 9613{
6e0b88f1 9614 int (*output_symbol_hook)
c152c796
AM
9615 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9616 struct elf_link_hash_entry *);
ef10c3ac 9617 struct elf_link_hash_table *hash_table;
c152c796 9618 const struct elf_backend_data *bed;
ef10c3ac 9619 bfd_size_type strtabsize;
c152c796 9620
8539e4e8
AM
9621 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9622
8b127cbc 9623 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9624 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9625 if (output_symbol_hook != NULL)
9626 {
8b127cbc 9627 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9628 if (ret != 1)
9629 return ret;
c152c796
AM
9630 }
9631
06f44071
AM
9632 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9633 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9634 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9635 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9636
ef10c3ac
L
9637 if (name == NULL
9638 || *name == '\0'
9639 || (input_sec->flags & SEC_EXCLUDE))
9640 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9641 else
9642 {
ef10c3ac
L
9643 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9644 to get the final offset for st_name. */
3f2e9699
L
9645 char *versioned_name = (char *) name;
9646 if (h != NULL && h->versioned == versioned && h->def_dynamic)
9647 {
9648 /* Keep only one '@' for versioned symbols defined in shared
9649 objects. */
9650 char *version = strrchr (name, ELF_VER_CHR);
9651 char *base_end = strchr (name, ELF_VER_CHR);
9652 if (version != base_end)
9653 {
9654 size_t base_len;
9655 size_t len = strlen (name);
9656 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9657 if (versioned_name == NULL)
9658 return 0;
9659 base_len = base_end - name;
9660 memcpy (versioned_name, name, base_len);
9661 memcpy (versioned_name + base_len, version,
9662 len - base_len);
9663 }
9664 }
ef10c3ac
L
9665 elfsym->st_name
9666 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9667 versioned_name, FALSE);
c152c796 9668 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9669 return 0;
c152c796
AM
9670 }
9671
ef10c3ac
L
9672 hash_table = elf_hash_table (flinfo->info);
9673 strtabsize = hash_table->strtabsize;
9674 if (strtabsize <= hash_table->strtabcount)
c152c796 9675 {
ef10c3ac
L
9676 strtabsize += strtabsize;
9677 hash_table->strtabsize = strtabsize;
9678 strtabsize *= sizeof (*hash_table->strtab);
9679 hash_table->strtab
9680 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9681 strtabsize);
9682 if (hash_table->strtab == NULL)
6e0b88f1 9683 return 0;
c152c796 9684 }
ef10c3ac
L
9685 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9686 hash_table->strtab[hash_table->strtabcount].dest_index
9687 = hash_table->strtabcount;
9688 hash_table->strtab[hash_table->strtabcount].destshndx_index
9689 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9690
ed48ec2e 9691 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9692 hash_table->strtabcount += 1;
9693
9694 return 1;
9695}
9696
9697/* Swap symbols out to the symbol table and flush the output symbols to
9698 the file. */
9699
9700static bfd_boolean
9701elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9702{
9703 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9704 size_t amt;
ef53be89 9705 size_t i;
ef10c3ac
L
9706 const struct elf_backend_data *bed;
9707 bfd_byte *symbuf;
9708 Elf_Internal_Shdr *hdr;
9709 file_ptr pos;
9710 bfd_boolean ret;
9711
9712 if (!hash_table->strtabcount)
9713 return TRUE;
9714
9715 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9716
9717 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9718
ef10c3ac
L
9719 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9720 symbuf = (bfd_byte *) bfd_malloc (amt);
9721 if (symbuf == NULL)
9722 return FALSE;
1b786873 9723
ef10c3ac 9724 if (flinfo->symshndxbuf)
c152c796 9725 {
ef53be89
AM
9726 amt = sizeof (Elf_External_Sym_Shndx);
9727 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9728 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9729 if (flinfo->symshndxbuf == NULL)
c152c796 9730 {
ef10c3ac
L
9731 free (symbuf);
9732 return FALSE;
c152c796 9733 }
c152c796
AM
9734 }
9735
ef10c3ac
L
9736 for (i = 0; i < hash_table->strtabcount; i++)
9737 {
9738 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9739 if (elfsym->sym.st_name == (unsigned long) -1)
9740 elfsym->sym.st_name = 0;
9741 else
9742 elfsym->sym.st_name
9743 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9744 elfsym->sym.st_name);
9745 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9746 ((bfd_byte *) symbuf
9747 + (elfsym->dest_index
9748 * bed->s->sizeof_sym)),
9749 (flinfo->symshndxbuf
9750 + elfsym->destshndx_index));
9751 }
9752
1ff6de03
NA
9753 /* Allow the linker to examine the strtab and symtab now they are
9754 populated. */
9755
9756 if (flinfo->info->callbacks->examine_strtab)
9757 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9758 hash_table->strtabcount,
9759 flinfo->symstrtab);
9760
ef10c3ac
L
9761 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9762 pos = hdr->sh_offset + hdr->sh_size;
9763 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9764 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9765 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9766 {
9767 hdr->sh_size += amt;
9768 ret = TRUE;
9769 }
9770 else
9771 ret = FALSE;
c152c796 9772
ef10c3ac
L
9773 free (symbuf);
9774
9775 free (hash_table->strtab);
9776 hash_table->strtab = NULL;
9777
9778 return ret;
c152c796
AM
9779}
9780
c0d5a53d
L
9781/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9782
9783static bfd_boolean
9784check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9785{
4fbb74a6
AM
9786 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9787 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9788 {
9789 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9790 beyond 64k. */
4eca0228 9791 _bfd_error_handler
695344c0 9792 /* xgettext:c-format */
9793eb77 9793 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9794 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9795 bfd_set_error (bfd_error_nonrepresentable_section);
9796 return FALSE;
9797 }
9798 return TRUE;
9799}
9800
c152c796
AM
9801/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9802 allowing an unsatisfied unversioned symbol in the DSO to match a
9803 versioned symbol that would normally require an explicit version.
9804 We also handle the case that a DSO references a hidden symbol
9805 which may be satisfied by a versioned symbol in another DSO. */
9806
9807static bfd_boolean
9808elf_link_check_versioned_symbol (struct bfd_link_info *info,
9809 const struct elf_backend_data *bed,
9810 struct elf_link_hash_entry *h)
9811{
9812 bfd *abfd;
9813 struct elf_link_loaded_list *loaded;
9814
9815 if (!is_elf_hash_table (info->hash))
9816 return FALSE;
9817
90c984fc
L
9818 /* Check indirect symbol. */
9819 while (h->root.type == bfd_link_hash_indirect)
9820 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9821
c152c796
AM
9822 switch (h->root.type)
9823 {
9824 default:
9825 abfd = NULL;
9826 break;
9827
9828 case bfd_link_hash_undefined:
9829 case bfd_link_hash_undefweak:
9830 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9831 if (abfd == NULL
9832 || (abfd->flags & DYNAMIC) == 0
e56f61be 9833 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9834 return FALSE;
9835 break;
9836
9837 case bfd_link_hash_defined:
9838 case bfd_link_hash_defweak:
9839 abfd = h->root.u.def.section->owner;
9840 break;
9841
9842 case bfd_link_hash_common:
9843 abfd = h->root.u.c.p->section->owner;
9844 break;
9845 }
9846 BFD_ASSERT (abfd != NULL);
9847
e310298c 9848 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
9849 loaded != NULL;
9850 loaded = loaded->next)
9851 {
9852 bfd *input;
9853 Elf_Internal_Shdr *hdr;
ef53be89
AM
9854 size_t symcount;
9855 size_t extsymcount;
9856 size_t extsymoff;
c152c796
AM
9857 Elf_Internal_Shdr *versymhdr;
9858 Elf_Internal_Sym *isym;
9859 Elf_Internal_Sym *isymend;
9860 Elf_Internal_Sym *isymbuf;
9861 Elf_External_Versym *ever;
9862 Elf_External_Versym *extversym;
9863
9864 input = loaded->abfd;
9865
9866 /* We check each DSO for a possible hidden versioned definition. */
9867 if (input == abfd
c152c796
AM
9868 || elf_dynversym (input) == 0)
9869 continue;
9870
9871 hdr = &elf_tdata (input)->dynsymtab_hdr;
9872
9873 symcount = hdr->sh_size / bed->s->sizeof_sym;
9874 if (elf_bad_symtab (input))
9875 {
9876 extsymcount = symcount;
9877 extsymoff = 0;
9878 }
9879 else
9880 {
9881 extsymcount = symcount - hdr->sh_info;
9882 extsymoff = hdr->sh_info;
9883 }
9884
9885 if (extsymcount == 0)
9886 continue;
9887
9888 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9889 NULL, NULL, NULL);
9890 if (isymbuf == NULL)
9891 return FALSE;
9892
9893 /* Read in any version definitions. */
9894 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 9895 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
9896 || (extversym = (Elf_External_Versym *)
9897 _bfd_malloc_and_read (input, versymhdr->sh_size,
9898 versymhdr->sh_size)) == NULL)
c152c796 9899 {
c152c796
AM
9900 free (isymbuf);
9901 return FALSE;
9902 }
9903
9904 ever = extversym + extsymoff;
9905 isymend = isymbuf + extsymcount;
9906 for (isym = isymbuf; isym < isymend; isym++, ever++)
9907 {
9908 const char *name;
9909 Elf_Internal_Versym iver;
9910 unsigned short version_index;
9911
9912 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9913 || isym->st_shndx == SHN_UNDEF)
9914 continue;
9915
9916 name = bfd_elf_string_from_elf_section (input,
9917 hdr->sh_link,
9918 isym->st_name);
9919 if (strcmp (name, h->root.root.string) != 0)
9920 continue;
9921
9922 _bfd_elf_swap_versym_in (input, ever, &iver);
9923
d023c380
L
9924 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9925 && !(h->def_regular
9926 && h->forced_local))
c152c796
AM
9927 {
9928 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9929 have provided a definition for the undefined sym unless
9930 it is defined in a non-shared object and forced local.
9931 */
c152c796
AM
9932 abort ();
9933 }
9934
9935 version_index = iver.vs_vers & VERSYM_VERSION;
9936 if (version_index == 1 || version_index == 2)
9937 {
9938 /* This is the base or first version. We can use it. */
9939 free (extversym);
9940 free (isymbuf);
9941 return TRUE;
9942 }
9943 }
9944
9945 free (extversym);
9946 free (isymbuf);
9947 }
9948
9949 return FALSE;
9950}
9951
b8871f35
L
9952/* Convert ELF common symbol TYPE. */
9953
9954static int
9955elf_link_convert_common_type (struct bfd_link_info *info, int type)
9956{
9957 /* Commom symbol can only appear in relocatable link. */
9958 if (!bfd_link_relocatable (info))
9959 abort ();
9960 switch (info->elf_stt_common)
9961 {
9962 case unchanged:
9963 break;
9964 case elf_stt_common:
9965 type = STT_COMMON;
9966 break;
9967 case no_elf_stt_common:
9968 type = STT_OBJECT;
9969 break;
9970 }
9971 return type;
9972}
9973
c152c796
AM
9974/* Add an external symbol to the symbol table. This is called from
9975 the hash table traversal routine. When generating a shared object,
9976 we go through the symbol table twice. The first time we output
9977 anything that might have been forced to local scope in a version
9978 script. The second time we output the symbols that are still
9979 global symbols. */
9980
9981static bfd_boolean
7686d77d 9982elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9983{
7686d77d 9984 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9985 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9986 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9987 bfd_boolean strip;
9988 Elf_Internal_Sym sym;
9989 asection *input_sec;
9990 const struct elf_backend_data *bed;
6e0b88f1
AM
9991 long indx;
9992 int ret;
b8871f35 9993 unsigned int type;
c152c796
AM
9994
9995 if (h->root.type == bfd_link_hash_warning)
9996 {
9997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9998 if (h->root.type == bfd_link_hash_new)
9999 return TRUE;
10000 }
10001
10002 /* Decide whether to output this symbol in this pass. */
10003 if (eoinfo->localsyms)
10004 {
4deb8f71 10005 if (!h->forced_local)
c152c796
AM
10006 return TRUE;
10007 }
10008 else
10009 {
4deb8f71 10010 if (h->forced_local)
c152c796
AM
10011 return TRUE;
10012 }
10013
8b127cbc 10014 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10015
12ac1cf5 10016 if (h->root.type == bfd_link_hash_undefined)
c152c796 10017 {
12ac1cf5
NC
10018 /* If we have an undefined symbol reference here then it must have
10019 come from a shared library that is being linked in. (Undefined
98da7939
L
10020 references in regular files have already been handled unless
10021 they are in unreferenced sections which are removed by garbage
10022 collection). */
12ac1cf5
NC
10023 bfd_boolean ignore_undef = FALSE;
10024
10025 /* Some symbols may be special in that the fact that they're
10026 undefined can be safely ignored - let backend determine that. */
10027 if (bed->elf_backend_ignore_undef_symbol)
10028 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10029
10030 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10031 if (!ignore_undef
c54f1524 10032 && h->ref_dynamic_nonweak
8b127cbc
AM
10033 && (!h->ref_regular || flinfo->info->gc_sections)
10034 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10035 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10036 {
10037 flinfo->info->callbacks->undefined_symbol
10038 (flinfo->info, h->root.root.string,
10039 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10040 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10041 && !flinfo->info->warn_unresolved_syms);
10042 }
97196564
L
10043
10044 /* Strip a global symbol defined in a discarded section. */
10045 if (h->indx == -3)
10046 return TRUE;
c152c796
AM
10047 }
10048
10049 /* We should also warn if a forced local symbol is referenced from
10050 shared libraries. */
0e1862bb 10051 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10052 && h->forced_local
10053 && h->ref_dynamic
371a5866 10054 && h->def_regular
f5385ebf 10055 && !h->dynamic_def
ee659f1f 10056 && h->ref_dynamic_nonweak
8b127cbc 10057 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10058 {
17d078c5
AM
10059 bfd *def_bfd;
10060 const char *msg;
90c984fc
L
10061 struct elf_link_hash_entry *hi = h;
10062
10063 /* Check indirect symbol. */
10064 while (hi->root.type == bfd_link_hash_indirect)
10065 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10066
10067 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10068 /* xgettext:c-format */
871b3ab2 10069 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10070 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10071 /* xgettext:c-format */
871b3ab2 10072 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10073 else
695344c0 10074 /* xgettext:c-format */
871b3ab2 10075 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10076 def_bfd = flinfo->output_bfd;
90c984fc
L
10077 if (hi->root.u.def.section != bfd_abs_section_ptr)
10078 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10079 _bfd_error_handler (msg, flinfo->output_bfd,
10080 h->root.root.string, def_bfd);
17d078c5 10081 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10082 eoinfo->failed = TRUE;
10083 return FALSE;
10084 }
10085
10086 /* We don't want to output symbols that have never been mentioned by
10087 a regular file, or that we have been told to strip. However, if
10088 h->indx is set to -2, the symbol is used by a reloc and we must
10089 output it. */
d983c8c5 10090 strip = FALSE;
c152c796 10091 if (h->indx == -2)
d983c8c5 10092 ;
f5385ebf 10093 else if ((h->def_dynamic
77cfaee6
AM
10094 || h->ref_dynamic
10095 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10096 && !h->def_regular
10097 && !h->ref_regular)
c152c796 10098 strip = TRUE;
8b127cbc 10099 else if (flinfo->info->strip == strip_all)
c152c796 10100 strip = TRUE;
8b127cbc
AM
10101 else if (flinfo->info->strip == strip_some
10102 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10103 h->root.root.string, FALSE, FALSE) == NULL)
10104 strip = TRUE;
d56d55e7
AM
10105 else if ((h->root.type == bfd_link_hash_defined
10106 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10107 && ((flinfo->info->strip_discarded
dbaa2011 10108 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10109 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10110 && h->root.u.def.section->owner != NULL
d56d55e7 10111 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10112 strip = TRUE;
9e2278f5
AM
10113 else if ((h->root.type == bfd_link_hash_undefined
10114 || h->root.type == bfd_link_hash_undefweak)
10115 && h->root.u.undef.abfd != NULL
10116 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10117 strip = TRUE;
c152c796 10118
b8871f35
L
10119 type = h->type;
10120
c152c796 10121 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10122 nothing else to do. However, if it is a forced local symbol or
10123 an ifunc symbol we need to give the backend finish_dynamic_symbol
10124 function a chance to make it dynamic. */
c152c796
AM
10125 if (strip
10126 && h->dynindx == -1
b8871f35 10127 && type != STT_GNU_IFUNC
f5385ebf 10128 && !h->forced_local)
c152c796
AM
10129 return TRUE;
10130
10131 sym.st_value = 0;
10132 sym.st_size = h->size;
10133 sym.st_other = h->other;
c152c796
AM
10134 switch (h->root.type)
10135 {
10136 default:
10137 case bfd_link_hash_new:
10138 case bfd_link_hash_warning:
10139 abort ();
10140 return FALSE;
10141
10142 case bfd_link_hash_undefined:
10143 case bfd_link_hash_undefweak:
10144 input_sec = bfd_und_section_ptr;
10145 sym.st_shndx = SHN_UNDEF;
10146 break;
10147
10148 case bfd_link_hash_defined:
10149 case bfd_link_hash_defweak:
10150 {
10151 input_sec = h->root.u.def.section;
10152 if (input_sec->output_section != NULL)
10153 {
10154 sym.st_shndx =
8b127cbc 10155 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10156 input_sec->output_section);
10157 if (sym.st_shndx == SHN_BAD)
10158 {
4eca0228 10159 _bfd_error_handler
695344c0 10160 /* xgettext:c-format */
871b3ab2 10161 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10162 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10163 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10164 eoinfo->failed = TRUE;
10165 return FALSE;
10166 }
10167
10168 /* ELF symbols in relocatable files are section relative,
10169 but in nonrelocatable files they are virtual
10170 addresses. */
10171 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10172 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10173 {
10174 sym.st_value += input_sec->output_section->vma;
10175 if (h->type == STT_TLS)
10176 {
8b127cbc 10177 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10178 if (tls_sec != NULL)
10179 sym.st_value -= tls_sec->vma;
c152c796
AM
10180 }
10181 }
10182 }
10183 else
10184 {
10185 BFD_ASSERT (input_sec->owner == NULL
10186 || (input_sec->owner->flags & DYNAMIC) != 0);
10187 sym.st_shndx = SHN_UNDEF;
10188 input_sec = bfd_und_section_ptr;
10189 }
10190 }
10191 break;
10192
10193 case bfd_link_hash_common:
10194 input_sec = h->root.u.c.p->section;
a4d8e49b 10195 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10196 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10197 break;
10198
10199 case bfd_link_hash_indirect:
10200 /* These symbols are created by symbol versioning. They point
10201 to the decorated version of the name. For example, if the
10202 symbol foo@@GNU_1.2 is the default, which should be used when
10203 foo is used with no version, then we add an indirect symbol
10204 foo which points to foo@@GNU_1.2. We ignore these symbols,
10205 since the indirected symbol is already in the hash table. */
10206 return TRUE;
10207 }
10208
b8871f35
L
10209 if (type == STT_COMMON || type == STT_OBJECT)
10210 switch (h->root.type)
10211 {
10212 case bfd_link_hash_common:
10213 type = elf_link_convert_common_type (flinfo->info, type);
10214 break;
10215 case bfd_link_hash_defined:
10216 case bfd_link_hash_defweak:
10217 if (bed->common_definition (&sym))
10218 type = elf_link_convert_common_type (flinfo->info, type);
10219 else
10220 type = STT_OBJECT;
10221 break;
10222 case bfd_link_hash_undefined:
10223 case bfd_link_hash_undefweak:
10224 break;
10225 default:
10226 abort ();
10227 }
10228
4deb8f71 10229 if (h->forced_local)
b8871f35
L
10230 {
10231 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10232 /* Turn off visibility on local symbol. */
10233 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10234 }
10235 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10236 else if (h->unique_global && h->def_regular)
10237 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10238 else if (h->root.type == bfd_link_hash_undefweak
10239 || h->root.type == bfd_link_hash_defweak)
10240 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10241 else
10242 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10243 sym.st_target_internal = h->target_internal;
10244
c152c796
AM
10245 /* Give the processor backend a chance to tweak the symbol value,
10246 and also to finish up anything that needs to be done for this
10247 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10248 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10249 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10250 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10251 && h->def_regular
0e1862bb 10252 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10253 || ((h->dynindx != -1
10254 || h->forced_local)
0e1862bb 10255 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10256 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10257 || h->root.type != bfd_link_hash_undefweak))
10258 || !h->forced_local)
8b127cbc 10259 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10260 {
10261 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10262 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10263 {
10264 eoinfo->failed = TRUE;
10265 return FALSE;
10266 }
10267 }
10268
10269 /* If we are marking the symbol as undefined, and there are no
10270 non-weak references to this symbol from a regular object, then
10271 mark the symbol as weak undefined; if there are non-weak
10272 references, mark the symbol as strong. We can't do this earlier,
10273 because it might not be marked as undefined until the
10274 finish_dynamic_symbol routine gets through with it. */
10275 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10276 && h->ref_regular
c152c796
AM
10277 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10278 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10279 {
10280 int bindtype;
b8871f35 10281 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10282
10283 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10284 if (type == STT_GNU_IFUNC)
10285 type = STT_FUNC;
c152c796 10286
f5385ebf 10287 if (h->ref_regular_nonweak)
c152c796
AM
10288 bindtype = STB_GLOBAL;
10289 else
10290 bindtype = STB_WEAK;
2955ec4c 10291 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10292 }
10293
bda987c2
CD
10294 /* If this is a symbol defined in a dynamic library, don't use the
10295 symbol size from the dynamic library. Relinking an executable
10296 against a new library may introduce gratuitous changes in the
10297 executable's symbols if we keep the size. */
10298 if (sym.st_shndx == SHN_UNDEF
10299 && !h->def_regular
10300 && h->def_dynamic)
10301 sym.st_size = 0;
10302
c152c796
AM
10303 /* If a non-weak symbol with non-default visibility is not defined
10304 locally, it is a fatal error. */
0e1862bb 10305 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10306 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10307 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10308 && h->root.type == bfd_link_hash_undefined
f5385ebf 10309 && !h->def_regular)
c152c796 10310 {
17d078c5
AM
10311 const char *msg;
10312
10313 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10314 /* xgettext:c-format */
871b3ab2 10315 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10316 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10317 /* xgettext:c-format */
871b3ab2 10318 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10319 else
695344c0 10320 /* xgettext:c-format */
871b3ab2 10321 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10322 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10323 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10324 eoinfo->failed = TRUE;
10325 return FALSE;
10326 }
10327
10328 /* If this symbol should be put in the .dynsym section, then put it
10329 there now. We already know the symbol index. We also fill in
10330 the entry in the .hash section. */
1c2649ed
EB
10331 if (h->dynindx != -1
10332 && elf_hash_table (flinfo->info)->dynamic_sections_created
10333 && elf_hash_table (flinfo->info)->dynsym != NULL
10334 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10335 {
c152c796
AM
10336 bfd_byte *esym;
10337
90c984fc
L
10338 /* Since there is no version information in the dynamic string,
10339 if there is no version info in symbol version section, we will
1659f720 10340 have a run-time problem if not linking executable, referenced
4deb8f71 10341 by shared library, or not bound locally. */
1659f720 10342 if (h->verinfo.verdef == NULL
0e1862bb 10343 && (!bfd_link_executable (flinfo->info)
1659f720
L
10344 || h->ref_dynamic
10345 || !h->def_regular))
90c984fc
L
10346 {
10347 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10348
10349 if (p && p [1] != '\0')
10350 {
4eca0228 10351 _bfd_error_handler
695344c0 10352 /* xgettext:c-format */
9793eb77 10353 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10354 flinfo->output_bfd, h->root.root.string);
10355 eoinfo->failed = TRUE;
10356 return FALSE;
10357 }
10358 }
10359
c152c796 10360 sym.st_name = h->dynstr_index;
cae1fbbb
L
10361 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10362 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10363 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10364 {
10365 eoinfo->failed = TRUE;
10366 return FALSE;
10367 }
8b127cbc 10368 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10369
8b127cbc 10370 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10371 {
10372 size_t hash_entry_size;
10373 bfd_byte *bucketpos;
10374 bfd_vma chain;
41198d0c
L
10375 size_t bucketcount;
10376 size_t bucket;
10377
8b127cbc 10378 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10379 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10380
10381 hash_entry_size
8b127cbc
AM
10382 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10383 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10384 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10385 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10386 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10387 bucketpos);
10388 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10389 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10390 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10391 }
c152c796 10392
8b127cbc 10393 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10394 {
10395 Elf_Internal_Versym iversym;
10396 Elf_External_Versym *eversym;
10397
5fa370e4 10398 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10399 {
7b20f099
AM
10400 if (h->verinfo.verdef == NULL
10401 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10402 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10403 iversym.vs_vers = 0;
10404 else
10405 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10406 }
10407 else
10408 {
10409 if (h->verinfo.vertree == NULL)
10410 iversym.vs_vers = 1;
10411 else
10412 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10413 if (flinfo->info->create_default_symver)
3e3b46e5 10414 iversym.vs_vers++;
c152c796
AM
10415 }
10416
422f1182 10417 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10418 defined locally. */
422f1182 10419 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10420 iversym.vs_vers |= VERSYM_HIDDEN;
10421
8b127cbc 10422 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10423 eversym += h->dynindx;
8b127cbc 10424 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10425 }
10426 }
10427
d983c8c5
AM
10428 /* If the symbol is undefined, and we didn't output it to .dynsym,
10429 strip it from .symtab too. Obviously we can't do this for
10430 relocatable output or when needed for --emit-relocs. */
10431 else if (input_sec == bfd_und_section_ptr
10432 && h->indx != -2
66cae560
NC
10433 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10434 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10435 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10436 return TRUE;
66cae560 10437
d983c8c5
AM
10438 /* Also strip others that we couldn't earlier due to dynamic symbol
10439 processing. */
10440 if (strip)
10441 return TRUE;
10442 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10443 return TRUE;
10444
2ec55de3
AM
10445 /* Output a FILE symbol so that following locals are not associated
10446 with the wrong input file. We need one for forced local symbols
10447 if we've seen more than one FILE symbol or when we have exactly
10448 one FILE symbol but global symbols are present in a file other
10449 than the one with the FILE symbol. We also need one if linker
10450 defined symbols are present. In practice these conditions are
10451 always met, so just emit the FILE symbol unconditionally. */
10452 if (eoinfo->localsyms
10453 && !eoinfo->file_sym_done
10454 && eoinfo->flinfo->filesym_count != 0)
10455 {
10456 Elf_Internal_Sym fsym;
10457
10458 memset (&fsym, 0, sizeof (fsym));
10459 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10460 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10461 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10462 bfd_und_section_ptr, NULL))
2ec55de3
AM
10463 return FALSE;
10464
10465 eoinfo->file_sym_done = TRUE;
10466 }
10467
8b127cbc 10468 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10469 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10470 input_sec, h);
6e0b88f1 10471 if (ret == 0)
c152c796
AM
10472 {
10473 eoinfo->failed = TRUE;
10474 return FALSE;
10475 }
6e0b88f1
AM
10476 else if (ret == 1)
10477 h->indx = indx;
10478 else if (h->indx == -2)
10479 abort();
c152c796
AM
10480
10481 return TRUE;
10482}
10483
cdd3575c
AM
10484/* Return TRUE if special handling is done for relocs in SEC against
10485 symbols defined in discarded sections. */
10486
c152c796
AM
10487static bfd_boolean
10488elf_section_ignore_discarded_relocs (asection *sec)
10489{
10490 const struct elf_backend_data *bed;
10491
cdd3575c
AM
10492 switch (sec->sec_info_type)
10493 {
dbaa2011
AM
10494 case SEC_INFO_TYPE_STABS:
10495 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10496 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10497 return TRUE;
10498 default:
10499 break;
10500 }
c152c796
AM
10501
10502 bed = get_elf_backend_data (sec->owner);
10503 if (bed->elf_backend_ignore_discarded_relocs != NULL
10504 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10505 return TRUE;
10506
10507 return FALSE;
10508}
10509
9e66c942
AM
10510/* Return a mask saying how ld should treat relocations in SEC against
10511 symbols defined in discarded sections. If this function returns
10512 COMPLAIN set, ld will issue a warning message. If this function
10513 returns PRETEND set, and the discarded section was link-once and the
10514 same size as the kept link-once section, ld will pretend that the
10515 symbol was actually defined in the kept section. Otherwise ld will
10516 zero the reloc (at least that is the intent, but some cooperation by
10517 the target dependent code is needed, particularly for REL targets). */
10518
8a696751
AM
10519unsigned int
10520_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10521{
9e66c942 10522 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10523 return PRETEND;
cdd3575c
AM
10524
10525 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10526 return 0;
cdd3575c
AM
10527
10528 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10529 return 0;
cdd3575c 10530
9e66c942 10531 return COMPLAIN | PRETEND;
cdd3575c
AM
10532}
10533
3d7f7666
L
10534/* Find a match between a section and a member of a section group. */
10535
10536static asection *
c0f00686
L
10537match_group_member (asection *sec, asection *group,
10538 struct bfd_link_info *info)
3d7f7666
L
10539{
10540 asection *first = elf_next_in_group (group);
10541 asection *s = first;
10542
10543 while (s != NULL)
10544 {
c0f00686 10545 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10546 return s;
10547
83180ade 10548 s = elf_next_in_group (s);
3d7f7666
L
10549 if (s == first)
10550 break;
10551 }
10552
10553 return NULL;
10554}
10555
01b3c8ab 10556/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10557 to replace it. Return the replacement if it is OK. Otherwise return
10558 NULL. */
01b3c8ab
L
10559
10560asection *
c0f00686 10561_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10562{
10563 asection *kept;
10564
10565 kept = sec->kept_section;
10566 if (kept != NULL)
10567 {
c2370991 10568 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10569 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10570 if (kept != NULL
10571 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10572 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10573 kept = NULL;
c2370991 10574 sec->kept_section = kept;
01b3c8ab
L
10575 }
10576 return kept;
10577}
10578
c152c796
AM
10579/* Link an input file into the linker output file. This function
10580 handles all the sections and relocations of the input file at once.
10581 This is so that we only have to read the local symbols once, and
10582 don't have to keep them in memory. */
10583
10584static bfd_boolean
8b127cbc 10585elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10586{
ece5ef60 10587 int (*relocate_section)
c152c796
AM
10588 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10589 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10590 bfd *output_bfd;
10591 Elf_Internal_Shdr *symtab_hdr;
10592 size_t locsymcount;
10593 size_t extsymoff;
10594 Elf_Internal_Sym *isymbuf;
10595 Elf_Internal_Sym *isym;
10596 Elf_Internal_Sym *isymend;
10597 long *pindex;
10598 asection **ppsection;
10599 asection *o;
10600 const struct elf_backend_data *bed;
c152c796 10601 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10602 bfd_size_type address_size;
10603 bfd_vma r_type_mask;
10604 int r_sym_shift;
ffbc01cc 10605 bfd_boolean have_file_sym = FALSE;
c152c796 10606
8b127cbc 10607 output_bfd = flinfo->output_bfd;
c152c796
AM
10608 bed = get_elf_backend_data (output_bfd);
10609 relocate_section = bed->elf_backend_relocate_section;
10610
10611 /* If this is a dynamic object, we don't want to do anything here:
10612 we don't want the local symbols, and we don't want the section
10613 contents. */
10614 if ((input_bfd->flags & DYNAMIC) != 0)
10615 return TRUE;
10616
c152c796
AM
10617 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10618 if (elf_bad_symtab (input_bfd))
10619 {
10620 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10621 extsymoff = 0;
10622 }
10623 else
10624 {
10625 locsymcount = symtab_hdr->sh_info;
10626 extsymoff = symtab_hdr->sh_info;
10627 }
10628
10629 /* Read the local symbols. */
10630 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10631 if (isymbuf == NULL && locsymcount != 0)
10632 {
10633 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10634 flinfo->internal_syms,
10635 flinfo->external_syms,
10636 flinfo->locsym_shndx);
c152c796
AM
10637 if (isymbuf == NULL)
10638 return FALSE;
10639 }
10640
10641 /* Find local symbol sections and adjust values of symbols in
10642 SEC_MERGE sections. Write out those local symbols we know are
10643 going into the output file. */
10644 isymend = isymbuf + locsymcount;
8b127cbc 10645 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10646 isym < isymend;
10647 isym++, pindex++, ppsection++)
10648 {
10649 asection *isec;
10650 const char *name;
10651 Elf_Internal_Sym osym;
6e0b88f1
AM
10652 long indx;
10653 int ret;
c152c796
AM
10654
10655 *pindex = -1;
10656
10657 if (elf_bad_symtab (input_bfd))
10658 {
10659 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10660 {
10661 *ppsection = NULL;
10662 continue;
10663 }
10664 }
10665
10666 if (isym->st_shndx == SHN_UNDEF)
10667 isec = bfd_und_section_ptr;
c152c796
AM
10668 else if (isym->st_shndx == SHN_ABS)
10669 isec = bfd_abs_section_ptr;
10670 else if (isym->st_shndx == SHN_COMMON)
10671 isec = bfd_com_section_ptr;
10672 else
10673 {
cb33740c
AM
10674 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10675 if (isec == NULL)
10676 {
10677 /* Don't attempt to output symbols with st_shnx in the
10678 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10679 isec = bfd_und_section_ptr;
cb33740c 10680 }
dbaa2011 10681 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10682 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10683 isym->st_value =
10684 _bfd_merged_section_offset (output_bfd, &isec,
10685 elf_section_data (isec)->sec_info,
10686 isym->st_value);
c152c796
AM
10687 }
10688
10689 *ppsection = isec;
10690
d983c8c5
AM
10691 /* Don't output the first, undefined, symbol. In fact, don't
10692 output any undefined local symbol. */
10693 if (isec == bfd_und_section_ptr)
c152c796
AM
10694 continue;
10695
10696 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10697 {
10698 /* We never output section symbols. Instead, we use the
10699 section symbol of the corresponding section in the output
10700 file. */
10701 continue;
10702 }
10703
10704 /* If we are stripping all symbols, we don't want to output this
10705 one. */
8b127cbc 10706 if (flinfo->info->strip == strip_all)
c152c796
AM
10707 continue;
10708
10709 /* If we are discarding all local symbols, we don't want to
10710 output this one. If we are generating a relocatable output
10711 file, then some of the local symbols may be required by
10712 relocs; we output them below as we discover that they are
10713 needed. */
8b127cbc 10714 if (flinfo->info->discard == discard_all)
c152c796
AM
10715 continue;
10716
10717 /* If this symbol is defined in a section which we are
f02571c5 10718 discarding, we don't need to keep it. */
abf874aa
CL
10719 if (isym->st_shndx != SHN_UNDEF
10720 && isym->st_shndx < SHN_LORESERVE
10721 && isec->output_section == NULL
10722 && flinfo->info->non_contiguous_regions
10723 && flinfo->info->non_contiguous_regions_warnings)
10724 {
10725 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10726 "discards section `%s' from '%s'\n"),
765cf5f6 10727 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10728 continue;
10729 }
10730
f02571c5 10731 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10732 && isym->st_shndx < SHN_LORESERVE
10733 && bfd_section_removed_from_list (output_bfd,
10734 isec->output_section))
e75a280b
L
10735 continue;
10736
c152c796
AM
10737 /* Get the name of the symbol. */
10738 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10739 isym->st_name);
10740 if (name == NULL)
10741 return FALSE;
10742
10743 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10744 if ((flinfo->info->strip == strip_some
10745 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10746 == NULL))
8b127cbc 10747 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10748 && (isec->flags & SEC_MERGE)
10749 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10750 || flinfo->info->discard == discard_l)
c152c796
AM
10751 && bfd_is_local_label_name (input_bfd, name)))
10752 continue;
10753
ffbc01cc
AM
10754 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10755 {
ce875075
AM
10756 if (input_bfd->lto_output)
10757 /* -flto puts a temp file name here. This means builds
10758 are not reproducible. Discard the symbol. */
10759 continue;
ffbc01cc
AM
10760 have_file_sym = TRUE;
10761 flinfo->filesym_count += 1;
10762 }
10763 if (!have_file_sym)
10764 {
10765 /* In the absence of debug info, bfd_find_nearest_line uses
10766 FILE symbols to determine the source file for local
10767 function symbols. Provide a FILE symbol here if input
10768 files lack such, so that their symbols won't be
10769 associated with a previous input file. It's not the
10770 source file, but the best we can do. */
10771 have_file_sym = TRUE;
10772 flinfo->filesym_count += 1;
10773 memset (&osym, 0, sizeof (osym));
10774 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10775 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10776 if (!elf_link_output_symstrtab (flinfo,
10777 (input_bfd->lto_output ? NULL
765cf5f6 10778 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10779 &osym, bfd_abs_section_ptr,
10780 NULL))
ffbc01cc
AM
10781 return FALSE;
10782 }
10783
c152c796
AM
10784 osym = *isym;
10785
10786 /* Adjust the section index for the output file. */
10787 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10788 isec->output_section);
10789 if (osym.st_shndx == SHN_BAD)
10790 return FALSE;
10791
c152c796
AM
10792 /* ELF symbols in relocatable files are section relative, but
10793 in executable files they are virtual addresses. Note that
10794 this code assumes that all ELF sections have an associated
10795 BFD section with a reasonable value for output_offset; below
10796 we assume that they also have a reasonable value for
10797 output_section. Any special sections must be set up to meet
10798 these requirements. */
10799 osym.st_value += isec->output_offset;
0e1862bb 10800 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10801 {
10802 osym.st_value += isec->output_section->vma;
10803 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10804 {
10805 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10806 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10807 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10808 else
10809 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10810 STT_NOTYPE);
c152c796
AM
10811 }
10812 }
10813
6e0b88f1 10814 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10815 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10816 if (ret == 0)
c152c796 10817 return FALSE;
6e0b88f1
AM
10818 else if (ret == 1)
10819 *pindex = indx;
c152c796
AM
10820 }
10821
310fd250
L
10822 if (bed->s->arch_size == 32)
10823 {
10824 r_type_mask = 0xff;
10825 r_sym_shift = 8;
10826 address_size = 4;
10827 }
10828 else
10829 {
10830 r_type_mask = 0xffffffff;
10831 r_sym_shift = 32;
10832 address_size = 8;
10833 }
10834
c152c796
AM
10835 /* Relocate the contents of each section. */
10836 sym_hashes = elf_sym_hashes (input_bfd);
10837 for (o = input_bfd->sections; o != NULL; o = o->next)
10838 {
10839 bfd_byte *contents;
10840
10841 if (! o->linker_mark)
10842 {
10843 /* This section was omitted from the link. */
10844 continue;
10845 }
10846
7bdf4127 10847 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10848 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10849 {
10850 /* Deal with the group signature symbol. */
10851 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10852 unsigned long symndx = sec_data->this_hdr.sh_info;
10853 asection *osec = o->output_section;
10854
7bdf4127 10855 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10856 if (symndx >= locsymcount
10857 || (elf_bad_symtab (input_bfd)
8b127cbc 10858 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10859 {
10860 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10861 while (h->root.type == bfd_link_hash_indirect
10862 || h->root.type == bfd_link_hash_warning)
10863 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10864 /* Arrange for symbol to be output. */
10865 h->indx = -2;
10866 elf_section_data (osec)->this_hdr.sh_info = -2;
10867 }
10868 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10869 {
10870 /* We'll use the output section target_index. */
8b127cbc 10871 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10872 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10873 }
10874 else
10875 {
8b127cbc 10876 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10877 {
10878 /* Otherwise output the local symbol now. */
10879 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10880 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10881 const char *name;
6e0b88f1
AM
10882 long indx;
10883 int ret;
bcacc0f5
AM
10884
10885 name = bfd_elf_string_from_elf_section (input_bfd,
10886 symtab_hdr->sh_link,
10887 sym.st_name);
10888 if (name == NULL)
10889 return FALSE;
10890
10891 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10892 sec);
10893 if (sym.st_shndx == SHN_BAD)
10894 return FALSE;
10895
10896 sym.st_value += o->output_offset;
10897
6e0b88f1 10898 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10899 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10900 NULL);
6e0b88f1 10901 if (ret == 0)
bcacc0f5 10902 return FALSE;
6e0b88f1 10903 else if (ret == 1)
8b127cbc 10904 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10905 else
10906 abort ();
bcacc0f5
AM
10907 }
10908 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10909 = flinfo->indices[symndx];
bcacc0f5
AM
10910 }
10911 }
10912
c152c796 10913 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10914 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10915 continue;
10916
10917 if ((o->flags & SEC_LINKER_CREATED) != 0)
10918 {
10919 /* Section was created by _bfd_elf_link_create_dynamic_sections
10920 or somesuch. */
10921 continue;
10922 }
10923
10924 /* Get the contents of the section. They have been cached by a
10925 relaxation routine. Note that o is a section in an input
10926 file, so the contents field will not have been set by any of
10927 the routines which work on output files. */
10928 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10929 {
10930 contents = elf_section_data (o)->this_hdr.contents;
10931 if (bed->caches_rawsize
10932 && o->rawsize != 0
10933 && o->rawsize < o->size)
10934 {
10935 memcpy (flinfo->contents, contents, o->rawsize);
10936 contents = flinfo->contents;
10937 }
10938 }
c152c796
AM
10939 else
10940 {
8b127cbc 10941 contents = flinfo->contents;
4a114e3e 10942 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10943 return FALSE;
10944 }
10945
10946 if ((o->flags & SEC_RELOC) != 0)
10947 {
10948 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10949 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10950 int action_discarded;
ece5ef60 10951 int ret;
c152c796
AM
10952
10953 /* Get the swapped relocs. */
10954 internal_relocs
8b127cbc
AM
10955 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10956 flinfo->internal_relocs, FALSE);
c152c796
AM
10957 if (internal_relocs == NULL
10958 && o->reloc_count > 0)
10959 return FALSE;
10960
310fd250
L
10961 /* We need to reverse-copy input .ctors/.dtors sections if
10962 they are placed in .init_array/.finit_array for output. */
10963 if (o->size > address_size
10964 && ((strncmp (o->name, ".ctors", 6) == 0
10965 && strcmp (o->output_section->name,
10966 ".init_array") == 0)
10967 || (strncmp (o->name, ".dtors", 6) == 0
10968 && strcmp (o->output_section->name,
10969 ".fini_array") == 0))
10970 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10971 {
056bafd4
MR
10972 if (o->size * bed->s->int_rels_per_ext_rel
10973 != o->reloc_count * address_size)
310fd250 10974 {
4eca0228 10975 _bfd_error_handler
695344c0 10976 /* xgettext:c-format */
871b3ab2 10977 (_("error: %pB: size of section %pA is not "
310fd250
L
10978 "multiple of address size"),
10979 input_bfd, o);
8c6716e5 10980 bfd_set_error (bfd_error_bad_value);
310fd250
L
10981 return FALSE;
10982 }
10983 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10984 }
10985
0f02bbd9 10986 action_discarded = -1;
c152c796 10987 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10988 action_discarded = (*bed->action_discarded) (o);
10989
10990 /* Run through the relocs evaluating complex reloc symbols and
10991 looking for relocs against symbols from discarded sections
10992 or section symbols from removed link-once sections.
10993 Complain about relocs against discarded sections. Zero
10994 relocs against removed link-once sections. */
10995
10996 rel = internal_relocs;
056bafd4 10997 relend = rel + o->reloc_count;
0f02bbd9 10998 for ( ; rel < relend; rel++)
c152c796 10999 {
0f02bbd9
AM
11000 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11001 unsigned int s_type;
11002 asection **ps, *sec;
11003 struct elf_link_hash_entry *h = NULL;
11004 const char *sym_name;
c152c796 11005
0f02bbd9
AM
11006 if (r_symndx == STN_UNDEF)
11007 continue;
c152c796 11008
0f02bbd9
AM
11009 if (r_symndx >= locsymcount
11010 || (elf_bad_symtab (input_bfd)
8b127cbc 11011 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11012 {
11013 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11014
0f02bbd9
AM
11015 /* Badly formatted input files can contain relocs that
11016 reference non-existant symbols. Check here so that
11017 we do not seg fault. */
11018 if (h == NULL)
c152c796 11019 {
4eca0228 11020 _bfd_error_handler
695344c0 11021 /* xgettext:c-format */
2dcf00ce 11022 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11023 "that references a non-existent global symbol"),
2dcf00ce 11024 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11025 bfd_set_error (bfd_error_bad_value);
11026 return FALSE;
11027 }
3b36f7e6 11028
0f02bbd9
AM
11029 while (h->root.type == bfd_link_hash_indirect
11030 || h->root.type == bfd_link_hash_warning)
11031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11032
0f02bbd9 11033 s_type = h->type;
cdd3575c 11034
9e2dec47 11035 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11036 mark the symbol as undefined. Note that the
11037 linker may attach linker created dynamic sections
11038 to the plugin bfd. Symbols defined in linker
11039 created sections are not plugin symbols. */
bc4e12de 11040 if ((h->root.non_ir_ref_regular
4070765b 11041 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11042 && (h->root.type == bfd_link_hash_defined
11043 || h->root.type == bfd_link_hash_defweak)
11044 && (h->root.u.def.section->flags
11045 & SEC_LINKER_CREATED) == 0
11046 && h->root.u.def.section->owner != NULL
11047 && (h->root.u.def.section->owner->flags
11048 & BFD_PLUGIN) != 0)
11049 {
11050 h->root.type = bfd_link_hash_undefined;
11051 h->root.u.undef.abfd = h->root.u.def.section->owner;
11052 }
11053
0f02bbd9
AM
11054 ps = NULL;
11055 if (h->root.type == bfd_link_hash_defined
11056 || h->root.type == bfd_link_hash_defweak)
11057 ps = &h->root.u.def.section;
11058
11059 sym_name = h->root.root.string;
11060 }
11061 else
11062 {
11063 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11064
11065 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11066 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11067 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11068 sym, *ps);
11069 }
c152c796 11070
c301e700 11071 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11072 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11073 {
11074 bfd_vma val;
11075 bfd_vma dot = (rel->r_offset
11076 + o->output_offset + o->output_section->vma);
11077#ifdef DEBUG
11078 printf ("Encountered a complex symbol!");
11079 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11080 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11081 (long) (rel - internal_relocs));
0f02bbd9
AM
11082 printf (" symbol: idx %8.8lx, name %s\n",
11083 r_symndx, sym_name);
11084 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11085 (unsigned long) rel->r_info,
11086 (unsigned long) rel->r_offset);
11087#endif
8b127cbc 11088 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11089 isymbuf, locsymcount, s_type == STT_SRELC))
11090 return FALSE;
11091
11092 /* Symbol evaluated OK. Update to absolute value. */
11093 set_symbol_value (input_bfd, isymbuf, locsymcount,
11094 r_symndx, val);
11095 continue;
11096 }
11097
11098 if (action_discarded != -1 && ps != NULL)
11099 {
cdd3575c
AM
11100 /* Complain if the definition comes from a
11101 discarded section. */
dbaa2011 11102 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11103 {
cf35638d 11104 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11105 if (action_discarded & COMPLAIN)
8b127cbc 11106 (*flinfo->info->callbacks->einfo)
695344c0 11107 /* xgettext:c-format */
871b3ab2
AM
11108 (_("%X`%s' referenced in section `%pA' of %pB: "
11109 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11110 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11111
87e5235d 11112 /* Try to do the best we can to support buggy old
e0ae6d6f 11113 versions of gcc. Pretend that the symbol is
87e5235d
AM
11114 really defined in the kept linkonce section.
11115 FIXME: This is quite broken. Modifying the
11116 symbol here means we will be changing all later
e0ae6d6f 11117 uses of the symbol, not just in this section. */
0f02bbd9 11118 if (action_discarded & PRETEND)
87e5235d 11119 {
01b3c8ab
L
11120 asection *kept;
11121
c0f00686 11122 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11123 flinfo->info);
01b3c8ab 11124 if (kept != NULL)
87e5235d
AM
11125 {
11126 *ps = kept;
11127 continue;
11128 }
11129 }
c152c796
AM
11130 }
11131 }
11132 }
11133
11134 /* Relocate the section by invoking a back end routine.
11135
11136 The back end routine is responsible for adjusting the
11137 section contents as necessary, and (if using Rela relocs
11138 and generating a relocatable output file) adjusting the
11139 reloc addend as necessary.
11140
11141 The back end routine does not have to worry about setting
11142 the reloc address or the reloc symbol index.
11143
11144 The back end routine is given a pointer to the swapped in
11145 internal symbols, and can access the hash table entries
11146 for the external symbols via elf_sym_hashes (input_bfd).
11147
11148 When generating relocatable output, the back end routine
11149 must handle STB_LOCAL/STT_SECTION symbols specially. The
11150 output symbol is going to be a section symbol
11151 corresponding to the output section, which will require
11152 the addend to be adjusted. */
11153
8b127cbc 11154 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11155 input_bfd, o, contents,
11156 internal_relocs,
11157 isymbuf,
8b127cbc 11158 flinfo->sections);
ece5ef60 11159 if (!ret)
c152c796
AM
11160 return FALSE;
11161
ece5ef60 11162 if (ret == 2
0e1862bb 11163 || bfd_link_relocatable (flinfo->info)
8b127cbc 11164 || flinfo->info->emitrelocations)
c152c796
AM
11165 {
11166 Elf_Internal_Rela *irela;
d4730f92 11167 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11168 bfd_vma last_offset;
11169 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11170 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11171 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11172 unsigned int next_erel;
c152c796 11173 bfd_boolean rela_normal;
d4730f92 11174 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11175
d4730f92
BS
11176 esdi = elf_section_data (o);
11177 esdo = elf_section_data (o->output_section);
11178 rela_normal = FALSE;
c152c796
AM
11179
11180 /* Adjust the reloc addresses and symbol indices. */
11181
11182 irela = internal_relocs;
056bafd4 11183 irelaend = irela + o->reloc_count;
d4730f92
BS
11184 rel_hash = esdo->rel.hashes + esdo->rel.count;
11185 /* We start processing the REL relocs, if any. When we reach
11186 IRELAMID in the loop, we switch to the RELA relocs. */
11187 irelamid = irela;
11188 if (esdi->rel.hdr != NULL)
11189 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11190 * bed->s->int_rels_per_ext_rel);
eac338cf 11191 rel_hash_list = rel_hash;
d4730f92 11192 rela_hash_list = NULL;
c152c796 11193 last_offset = o->output_offset;
0e1862bb 11194 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11195 last_offset += o->output_section->vma;
11196 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11197 {
11198 unsigned long r_symndx;
11199 asection *sec;
11200 Elf_Internal_Sym sym;
11201
11202 if (next_erel == bed->s->int_rels_per_ext_rel)
11203 {
11204 rel_hash++;
11205 next_erel = 0;
11206 }
11207
d4730f92
BS
11208 if (irela == irelamid)
11209 {
11210 rel_hash = esdo->rela.hashes + esdo->rela.count;
11211 rela_hash_list = rel_hash;
11212 rela_normal = bed->rela_normal;
11213 }
11214
c152c796 11215 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11216 flinfo->info, o,
c152c796
AM
11217 irela->r_offset);
11218 if (irela->r_offset >= (bfd_vma) -2)
11219 {
11220 /* This is a reloc for a deleted entry or somesuch.
11221 Turn it into an R_*_NONE reloc, at the same
11222 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11223 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11224 being ordered. */
11225 irela->r_offset = last_offset;
11226 irela->r_info = 0;
11227 irela->r_addend = 0;
11228 continue;
11229 }
11230
11231 irela->r_offset += o->output_offset;
11232
11233 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11234 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11235 irela->r_offset += o->output_section->vma;
11236
11237 last_offset = irela->r_offset;
11238
11239 r_symndx = irela->r_info >> r_sym_shift;
11240 if (r_symndx == STN_UNDEF)
11241 continue;
11242
11243 if (r_symndx >= locsymcount
11244 || (elf_bad_symtab (input_bfd)
8b127cbc 11245 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11246 {
11247 struct elf_link_hash_entry *rh;
11248 unsigned long indx;
11249
11250 /* This is a reloc against a global symbol. We
11251 have not yet output all the local symbols, so
11252 we do not know the symbol index of any global
11253 symbol. We set the rel_hash entry for this
11254 reloc to point to the global hash table entry
11255 for this symbol. The symbol index is then
ee75fd95 11256 set at the end of bfd_elf_final_link. */
c152c796
AM
11257 indx = r_symndx - extsymoff;
11258 rh = elf_sym_hashes (input_bfd)[indx];
11259 while (rh->root.type == bfd_link_hash_indirect
11260 || rh->root.type == bfd_link_hash_warning)
11261 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11262
11263 /* Setting the index to -2 tells
11264 elf_link_output_extsym that this symbol is
11265 used by a reloc. */
11266 BFD_ASSERT (rh->indx < 0);
11267 rh->indx = -2;
c152c796
AM
11268 *rel_hash = rh;
11269
11270 continue;
11271 }
11272
11273 /* This is a reloc against a local symbol. */
11274
11275 *rel_hash = NULL;
11276 sym = isymbuf[r_symndx];
8b127cbc 11277 sec = flinfo->sections[r_symndx];
c152c796
AM
11278 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11279 {
11280 /* I suppose the backend ought to fill in the
11281 section of any STT_SECTION symbol against a
6a8d1586 11282 processor specific section. */
cf35638d 11283 r_symndx = STN_UNDEF;
6a8d1586
AM
11284 if (bfd_is_abs_section (sec))
11285 ;
c152c796
AM
11286 else if (sec == NULL || sec->owner == NULL)
11287 {
11288 bfd_set_error (bfd_error_bad_value);
11289 return FALSE;
11290 }
11291 else
11292 {
6a8d1586
AM
11293 asection *osec = sec->output_section;
11294
11295 /* If we have discarded a section, the output
11296 section will be the absolute section. In
ab96bf03
AM
11297 case of discarded SEC_MERGE sections, use
11298 the kept section. relocate_section should
11299 have already handled discarded linkonce
11300 sections. */
6a8d1586
AM
11301 if (bfd_is_abs_section (osec)
11302 && sec->kept_section != NULL
11303 && sec->kept_section->output_section != NULL)
11304 {
11305 osec = sec->kept_section->output_section;
11306 irela->r_addend -= osec->vma;
11307 }
11308
11309 if (!bfd_is_abs_section (osec))
11310 {
11311 r_symndx = osec->target_index;
cf35638d 11312 if (r_symndx == STN_UNDEF)
74541ad4 11313 {
051d833a
AM
11314 irela->r_addend += osec->vma;
11315 osec = _bfd_nearby_section (output_bfd, osec,
11316 osec->vma);
11317 irela->r_addend -= osec->vma;
11318 r_symndx = osec->target_index;
74541ad4 11319 }
6a8d1586 11320 }
c152c796
AM
11321 }
11322
11323 /* Adjust the addend according to where the
11324 section winds up in the output section. */
11325 if (rela_normal)
11326 irela->r_addend += sec->output_offset;
11327 }
11328 else
11329 {
8b127cbc 11330 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11331 {
11332 unsigned long shlink;
11333 const char *name;
11334 asection *osec;
6e0b88f1 11335 long indx;
c152c796 11336
8b127cbc 11337 if (flinfo->info->strip == strip_all)
c152c796
AM
11338 {
11339 /* You can't do ld -r -s. */
11340 bfd_set_error (bfd_error_invalid_operation);
11341 return FALSE;
11342 }
11343
11344 /* This symbol was skipped earlier, but
11345 since it is needed by a reloc, we
11346 must output it now. */
11347 shlink = symtab_hdr->sh_link;
11348 name = (bfd_elf_string_from_elf_section
11349 (input_bfd, shlink, sym.st_name));
11350 if (name == NULL)
11351 return FALSE;
11352
11353 osec = sec->output_section;
11354 sym.st_shndx =
11355 _bfd_elf_section_from_bfd_section (output_bfd,
11356 osec);
11357 if (sym.st_shndx == SHN_BAD)
11358 return FALSE;
11359
11360 sym.st_value += sec->output_offset;
0e1862bb 11361 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11362 {
11363 sym.st_value += osec->vma;
11364 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11365 {
102def4d
AM
11366 struct elf_link_hash_table *htab
11367 = elf_hash_table (flinfo->info);
11368
c152c796
AM
11369 /* STT_TLS symbols are relative to PT_TLS
11370 segment base. */
102def4d
AM
11371 if (htab->tls_sec != NULL)
11372 sym.st_value -= htab->tls_sec->vma;
11373 else
11374 sym.st_info
11375 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11376 STT_NOTYPE);
c152c796
AM
11377 }
11378 }
11379
6e0b88f1 11380 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11381 ret = elf_link_output_symstrtab (flinfo, name,
11382 &sym, sec,
11383 NULL);
6e0b88f1 11384 if (ret == 0)
c152c796 11385 return FALSE;
6e0b88f1 11386 else if (ret == 1)
8b127cbc 11387 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11388 else
11389 abort ();
c152c796
AM
11390 }
11391
8b127cbc 11392 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11393 }
11394
11395 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11396 | (irela->r_info & r_type_mask));
11397 }
11398
11399 /* Swap out the relocs. */
d4730f92
BS
11400 input_rel_hdr = esdi->rel.hdr;
11401 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11402 {
d4730f92
BS
11403 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11404 input_rel_hdr,
11405 internal_relocs,
11406 rel_hash_list))
11407 return FALSE;
c152c796
AM
11408 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11409 * bed->s->int_rels_per_ext_rel);
eac338cf 11410 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11411 }
11412
11413 input_rela_hdr = esdi->rela.hdr;
11414 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11415 {
eac338cf 11416 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11417 input_rela_hdr,
eac338cf 11418 internal_relocs,
d4730f92 11419 rela_hash_list))
c152c796
AM
11420 return FALSE;
11421 }
11422 }
11423 }
11424
11425 /* Write out the modified section contents. */
11426 if (bed->elf_backend_write_section
8b127cbc 11427 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11428 contents))
c152c796
AM
11429 {
11430 /* Section written out. */
11431 }
11432 else switch (o->sec_info_type)
11433 {
dbaa2011 11434 case SEC_INFO_TYPE_STABS:
c152c796
AM
11435 if (! (_bfd_write_section_stabs
11436 (output_bfd,
8b127cbc 11437 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11438 o, &elf_section_data (o)->sec_info, contents)))
11439 return FALSE;
11440 break;
dbaa2011 11441 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11442 if (! _bfd_write_merged_section (output_bfd, o,
11443 elf_section_data (o)->sec_info))
11444 return FALSE;
11445 break;
dbaa2011 11446 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11447 {
8b127cbc 11448 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11449 o, contents))
11450 return FALSE;
11451 }
11452 break;
2f0c68f2
CM
11453 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11454 {
11455 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11456 flinfo->info,
11457 o, contents))
11458 return FALSE;
11459 }
11460 break;
c152c796
AM
11461 default:
11462 {
310fd250
L
11463 if (! (o->flags & SEC_EXCLUDE))
11464 {
11465 file_ptr offset = (file_ptr) o->output_offset;
11466 bfd_size_type todo = o->size;
37b01f6a 11467
bb294208 11468 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11469
310fd250
L
11470 if ((o->flags & SEC_ELF_REVERSE_COPY))
11471 {
11472 /* Reverse-copy input section to output. */
11473 do
11474 {
11475 todo -= address_size;
11476 if (! bfd_set_section_contents (output_bfd,
11477 o->output_section,
11478 contents + todo,
11479 offset,
11480 address_size))
11481 return FALSE;
11482 if (todo == 0)
11483 break;
11484 offset += address_size;
11485 }
11486 while (1);
11487 }
11488 else if (! bfd_set_section_contents (output_bfd,
11489 o->output_section,
11490 contents,
11491 offset, todo))
11492 return FALSE;
11493 }
c152c796
AM
11494 }
11495 break;
11496 }
11497 }
11498
11499 return TRUE;
11500}
11501
11502/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11503 requested by the linker, and does not come from any input file. This
c152c796
AM
11504 is used to build constructor and destructor tables when linking
11505 with -Ur. */
11506
11507static bfd_boolean
11508elf_reloc_link_order (bfd *output_bfd,
11509 struct bfd_link_info *info,
11510 asection *output_section,
11511 struct bfd_link_order *link_order)
11512{
11513 reloc_howto_type *howto;
11514 long indx;
11515 bfd_vma offset;
11516 bfd_vma addend;
d4730f92 11517 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11518 struct elf_link_hash_entry **rel_hash_ptr;
11519 Elf_Internal_Shdr *rel_hdr;
11520 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11521 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11522 bfd_byte *erel;
11523 unsigned int i;
d4730f92 11524 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11525
11526 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11527 if (howto == NULL)
11528 {
11529 bfd_set_error (bfd_error_bad_value);
11530 return FALSE;
11531 }
11532
11533 addend = link_order->u.reloc.p->addend;
11534
d4730f92
BS
11535 if (esdo->rel.hdr)
11536 reldata = &esdo->rel;
11537 else if (esdo->rela.hdr)
11538 reldata = &esdo->rela;
11539 else
11540 {
11541 reldata = NULL;
11542 BFD_ASSERT (0);
11543 }
11544
c152c796 11545 /* Figure out the symbol index. */
d4730f92 11546 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11547 if (link_order->type == bfd_section_reloc_link_order)
11548 {
11549 indx = link_order->u.reloc.p->u.section->target_index;
11550 BFD_ASSERT (indx != 0);
11551 *rel_hash_ptr = NULL;
11552 }
11553 else
11554 {
11555 struct elf_link_hash_entry *h;
11556
11557 /* Treat a reloc against a defined symbol as though it were
11558 actually against the section. */
11559 h = ((struct elf_link_hash_entry *)
11560 bfd_wrapped_link_hash_lookup (output_bfd, info,
11561 link_order->u.reloc.p->u.name,
11562 FALSE, FALSE, TRUE));
11563 if (h != NULL
11564 && (h->root.type == bfd_link_hash_defined
11565 || h->root.type == bfd_link_hash_defweak))
11566 {
11567 asection *section;
11568
11569 section = h->root.u.def.section;
11570 indx = section->output_section->target_index;
11571 *rel_hash_ptr = NULL;
11572 /* It seems that we ought to add the symbol value to the
11573 addend here, but in practice it has already been added
11574 because it was passed to constructor_callback. */
11575 addend += section->output_section->vma + section->output_offset;
11576 }
11577 else if (h != NULL)
11578 {
11579 /* Setting the index to -2 tells elf_link_output_extsym that
11580 this symbol is used by a reloc. */
11581 h->indx = -2;
11582 *rel_hash_ptr = h;
11583 indx = 0;
11584 }
11585 else
11586 {
1a72702b
AM
11587 (*info->callbacks->unattached_reloc)
11588 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11589 indx = 0;
11590 }
11591 }
11592
11593 /* If this is an inplace reloc, we must write the addend into the
11594 object file. */
11595 if (howto->partial_inplace && addend != 0)
11596 {
11597 bfd_size_type size;
11598 bfd_reloc_status_type rstat;
11599 bfd_byte *buf;
11600 bfd_boolean ok;
11601 const char *sym_name;
bb294208 11602 bfd_size_type octets;
c152c796 11603
a50b1753
NC
11604 size = (bfd_size_type) bfd_get_reloc_size (howto);
11605 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11606 if (buf == NULL && size != 0)
c152c796
AM
11607 return FALSE;
11608 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11609 switch (rstat)
11610 {
11611 case bfd_reloc_ok:
11612 break;
11613
11614 default:
11615 case bfd_reloc_outofrange:
11616 abort ();
11617
11618 case bfd_reloc_overflow:
11619 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11620 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11621 else
11622 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11623 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11624 howto->name, addend, NULL, NULL,
11625 (bfd_vma) 0);
c152c796
AM
11626 break;
11627 }
37b01f6a 11628
bb294208
AM
11629 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11630 output_section);
c152c796 11631 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11632 octets, size);
c152c796
AM
11633 free (buf);
11634 if (! ok)
11635 return FALSE;
11636 }
11637
11638 /* The address of a reloc is relative to the section in a
11639 relocatable file, and is a virtual address in an executable
11640 file. */
11641 offset = link_order->offset;
0e1862bb 11642 if (! bfd_link_relocatable (info))
c152c796
AM
11643 offset += output_section->vma;
11644
11645 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11646 {
11647 irel[i].r_offset = offset;
11648 irel[i].r_info = 0;
11649 irel[i].r_addend = 0;
11650 }
11651 if (bed->s->arch_size == 32)
11652 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11653 else
11654 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11655
d4730f92 11656 rel_hdr = reldata->hdr;
c152c796
AM
11657 erel = rel_hdr->contents;
11658 if (rel_hdr->sh_type == SHT_REL)
11659 {
d4730f92 11660 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11661 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11662 }
11663 else
11664 {
11665 irel[0].r_addend = addend;
d4730f92 11666 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11667 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11668 }
11669
d4730f92 11670 ++reldata->count;
c152c796
AM
11671
11672 return TRUE;
11673}
11674
0b52efa6 11675
0b52efa6
PB
11676/* Compare two sections based on the locations of the sections they are
11677 linked to. Used by elf_fixup_link_order. */
11678
11679static int
8c1c5e5d 11680compare_link_order (const void *a, const void *b)
0b52efa6 11681{
8c1c5e5d
AM
11682 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11683 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11684 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11685 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11686 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11687 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11688
0b52efa6
PB
11689 if (apos < bpos)
11690 return -1;
8c1c5e5d
AM
11691 if (apos > bpos)
11692 return 1;
11693
11694 /* The only way we should get matching LMAs is when the first of two
11695 sections has zero size. */
11696 if (asec->size < bsec->size)
11697 return -1;
11698 if (asec->size > bsec->size)
11699 return 1;
11700
11701 /* If they are both zero size then they almost certainly have the same
11702 VMA and thus are not ordered with respect to each other. Test VMA
11703 anyway, and fall back to id to make the result reproducible across
11704 qsort implementations. */
11705 apos = asec->output_section->vma + asec->output_offset;
11706 bpos = bsec->output_section->vma + bsec->output_offset;
11707 if (apos < bpos)
11708 return -1;
11709 if (apos > bpos)
11710 return 1;
11711
11712 return asec->id - bsec->id;
0b52efa6
PB
11713}
11714
11715
11716/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11717 order as their linked sections. Returns false if this could not be done
11718 because an output section includes both ordered and unordered
11719 sections. Ideally we'd do this in the linker proper. */
11720
11721static bfd_boolean
11722elf_fixup_link_order (bfd *abfd, asection *o)
11723{
8c1c5e5d
AM
11724 size_t seen_linkorder;
11725 size_t seen_other;
11726 size_t n;
0b52efa6
PB
11727 struct bfd_link_order *p;
11728 bfd *sub;
0b52efa6 11729 struct bfd_link_order **sections;
66631823
CE
11730 asection *other_sec, *linkorder_sec;
11731 bfd_vma offset; /* Octets. */
3b36f7e6 11732
d33cdfe3
L
11733 other_sec = NULL;
11734 linkorder_sec = NULL;
0b52efa6
PB
11735 seen_other = 0;
11736 seen_linkorder = 0;
8423293d 11737 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11738 {
d33cdfe3 11739 if (p->type == bfd_indirect_link_order)
0b52efa6 11740 {
66631823 11741 asection *s = p->u.indirect.section;
d33cdfe3 11742 sub = s->owner;
847d5183
AM
11743 if ((s->flags & SEC_LINKER_CREATED) == 0
11744 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11745 && elf_section_data (s) != NULL
11746 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11747 {
11748 seen_linkorder++;
11749 linkorder_sec = s;
11750 }
0b52efa6 11751 else
d33cdfe3
L
11752 {
11753 seen_other++;
11754 other_sec = s;
11755 }
0b52efa6
PB
11756 }
11757 else
11758 seen_other++;
d33cdfe3
L
11759
11760 if (seen_other && seen_linkorder)
11761 {
11762 if (other_sec && linkorder_sec)
4eca0228 11763 _bfd_error_handler
695344c0 11764 /* xgettext:c-format */
871b3ab2
AM
11765 (_("%pA has both ordered [`%pA' in %pB] "
11766 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11767 o, linkorder_sec, linkorder_sec->owner,
11768 other_sec, other_sec->owner);
d33cdfe3 11769 else
4eca0228 11770 _bfd_error_handler
871b3ab2 11771 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11772 bfd_set_error (bfd_error_bad_value);
11773 return FALSE;
11774 }
0b52efa6
PB
11775 }
11776
11777 if (!seen_linkorder)
11778 return TRUE;
11779
8c1c5e5d 11780 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11781 if (sections == NULL)
11782 return FALSE;
3b36f7e6 11783
8c1c5e5d 11784 seen_linkorder = 0;
8423293d 11785 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11786 sections[seen_linkorder++] = p;
11787
0b52efa6 11788 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11789 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11790
11791 /* Change the offsets of the sections. */
11792 offset = 0;
11793 for (n = 0; n < seen_linkorder; n++)
11794 {
8c1c5e5d 11795 bfd_vma mask;
66631823
CE
11796 asection *s = sections[n]->u.indirect.section;
11797 unsigned int opb = bfd_octets_per_byte (abfd, s);
11798
11799 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11800 offset = (offset + ~mask) & mask;
66631823 11801 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11802 offset += sections[n]->size;
11803 }
11804
4dd07732 11805 free (sections);
0b52efa6
PB
11806 return TRUE;
11807}
11808
76359541
TP
11809/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11810 Returns TRUE upon success, FALSE otherwise. */
11811
11812static bfd_boolean
11813elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11814{
11815 bfd_boolean ret = FALSE;
11816 bfd *implib_bfd;
11817 const struct elf_backend_data *bed;
11818 flagword flags;
11819 enum bfd_architecture arch;
11820 unsigned int mach;
11821 asymbol **sympp = NULL;
11822 long symsize;
11823 long symcount;
11824 long src_count;
11825 elf_symbol_type *osymbuf;
446f7ed5 11826 size_t amt;
76359541
TP
11827
11828 implib_bfd = info->out_implib_bfd;
11829 bed = get_elf_backend_data (abfd);
11830
11831 if (!bfd_set_format (implib_bfd, bfd_object))
11832 return FALSE;
11833
046734ff 11834 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11835 flags = bfd_get_file_flags (abfd);
11836 flags &= ~HAS_RELOC;
11837 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11838 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11839 return FALSE;
11840
11841 /* Copy architecture of output file to import library file. */
11842 arch = bfd_get_arch (abfd);
11843 mach = bfd_get_mach (abfd);
11844 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11845 && (abfd->target_defaulted
11846 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11847 return FALSE;
11848
11849 /* Get symbol table size. */
11850 symsize = bfd_get_symtab_upper_bound (abfd);
11851 if (symsize < 0)
11852 return FALSE;
11853
11854 /* Read in the symbol table. */
ec9bd0a2
AM
11855 sympp = (asymbol **) bfd_malloc (symsize);
11856 if (sympp == NULL)
11857 return FALSE;
11858
76359541
TP
11859 symcount = bfd_canonicalize_symtab (abfd, sympp);
11860 if (symcount < 0)
11861 goto free_sym_buf;
11862
11863 /* Allow the BFD backend to copy any private header data it
11864 understands from the output BFD to the import library BFD. */
11865 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11866 goto free_sym_buf;
11867
11868 /* Filter symbols to appear in the import library. */
11869 if (bed->elf_backend_filter_implib_symbols)
11870 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11871 symcount);
11872 else
11873 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11874 if (symcount == 0)
11875 {
5df1bc57 11876 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11877 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11878 implib_bfd);
76359541
TP
11879 goto free_sym_buf;
11880 }
11881
11882
11883 /* Make symbols absolute. */
446f7ed5
AM
11884 amt = symcount * sizeof (*osymbuf);
11885 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11886 if (osymbuf == NULL)
11887 goto free_sym_buf;
11888
76359541
TP
11889 for (src_count = 0; src_count < symcount; src_count++)
11890 {
11891 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11892 sizeof (*osymbuf));
11893 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11894 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11895 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11896 osymbuf[src_count].internal_elf_sym.st_value =
11897 osymbuf[src_count].symbol.value;
11898 sympp[src_count] = &osymbuf[src_count].symbol;
11899 }
11900
11901 bfd_set_symtab (implib_bfd, sympp, symcount);
11902
11903 /* Allow the BFD backend to copy any private data it understands
11904 from the output BFD to the import library BFD. This is done last
11905 to permit the routine to look at the filtered symbol table. */
11906 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11907 goto free_sym_buf;
11908
11909 if (!bfd_close (implib_bfd))
11910 goto free_sym_buf;
11911
11912 ret = TRUE;
11913
dc1e8a47 11914 free_sym_buf:
76359541
TP
11915 free (sympp);
11916 return ret;
11917}
11918
9f7c3e5e
AM
11919static void
11920elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11921{
11922 asection *o;
11923
11924 if (flinfo->symstrtab != NULL)
ef10c3ac 11925 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
11926 free (flinfo->contents);
11927 free (flinfo->external_relocs);
11928 free (flinfo->internal_relocs);
11929 free (flinfo->external_syms);
11930 free (flinfo->locsym_shndx);
11931 free (flinfo->internal_syms);
11932 free (flinfo->indices);
11933 free (flinfo->sections);
11934 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11935 free (flinfo->symshndxbuf);
11936 for (o = obfd->sections; o != NULL; o = o->next)
11937 {
11938 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
11939 free (esdo->rel.hashes);
11940 free (esdo->rela.hashes);
9f7c3e5e
AM
11941 }
11942}
0b52efa6 11943
c152c796
AM
11944/* Do the final step of an ELF link. */
11945
11946bfd_boolean
11947bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11948{
11949 bfd_boolean dynamic;
11950 bfd_boolean emit_relocs;
11951 bfd *dynobj;
8b127cbc 11952 struct elf_final_link_info flinfo;
91d6fa6a
NC
11953 asection *o;
11954 struct bfd_link_order *p;
11955 bfd *sub;
c152c796
AM
11956 bfd_size_type max_contents_size;
11957 bfd_size_type max_external_reloc_size;
11958 bfd_size_type max_internal_reloc_count;
11959 bfd_size_type max_sym_count;
11960 bfd_size_type max_sym_shndx_count;
c152c796
AM
11961 Elf_Internal_Sym elfsym;
11962 unsigned int i;
11963 Elf_Internal_Shdr *symtab_hdr;
11964 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11966 struct elf_outext_info eoinfo;
11967 bfd_boolean merged;
11968 size_t relativecount = 0;
11969 asection *reldyn = 0;
11970 bfd_size_type amt;
104d59d1
JM
11971 asection *attr_section = NULL;
11972 bfd_vma attr_size = 0;
11973 const char *std_attrs_section;
64f52338 11974 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 11975 bfd_boolean sections_removed;
c152c796 11976
64f52338 11977 if (!is_elf_hash_table (htab))
c152c796
AM
11978 return FALSE;
11979
0e1862bb 11980 if (bfd_link_pic (info))
c152c796
AM
11981 abfd->flags |= DYNAMIC;
11982
64f52338
AM
11983 dynamic = htab->dynamic_sections_created;
11984 dynobj = htab->dynobj;
c152c796 11985
0e1862bb 11986 emit_relocs = (bfd_link_relocatable (info)
a4676736 11987 || info->emitrelocations);
c152c796 11988
8b127cbc
AM
11989 flinfo.info = info;
11990 flinfo.output_bfd = abfd;
ef10c3ac 11991 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11992 if (flinfo.symstrtab == NULL)
c152c796
AM
11993 return FALSE;
11994
11995 if (! dynamic)
11996 {
8b127cbc
AM
11997 flinfo.hash_sec = NULL;
11998 flinfo.symver_sec = NULL;
c152c796
AM
11999 }
12000 else
12001 {
3d4d4302 12002 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12003 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12004 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12005 /* Note that it is OK if symver_sec is NULL. */
12006 }
12007
8b127cbc
AM
12008 flinfo.contents = NULL;
12009 flinfo.external_relocs = NULL;
12010 flinfo.internal_relocs = NULL;
12011 flinfo.external_syms = NULL;
12012 flinfo.locsym_shndx = NULL;
12013 flinfo.internal_syms = NULL;
12014 flinfo.indices = NULL;
12015 flinfo.sections = NULL;
8b127cbc 12016 flinfo.symshndxbuf = NULL;
ffbc01cc 12017 flinfo.filesym_count = 0;
c152c796 12018
104d59d1
JM
12019 /* The object attributes have been merged. Remove the input
12020 sections from the link, and set the contents of the output
1ff6de03 12021 section. */
4c6ee646 12022 sections_removed = FALSE;
104d59d1
JM
12023 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12024 for (o = abfd->sections; o != NULL; o = o->next)
12025 {
5270eddc 12026 bfd_boolean remove_section = FALSE;
b8a6ced7 12027
104d59d1
JM
12028 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12029 || strcmp (o->name, ".gnu.attributes") == 0)
12030 {
12031 for (p = o->map_head.link_order; p != NULL; p = p->next)
12032 {
12033 asection *input_section;
12034
12035 if (p->type != bfd_indirect_link_order)
12036 continue;
12037 input_section = p->u.indirect.section;
12038 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12039 elf_link_input_bfd ignores this section. */
12040 input_section->flags &= ~SEC_HAS_CONTENTS;
12041 }
a0c8462f 12042
104d59d1 12043 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12044 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12045 /* Skip this section later on. */
12046 o->map_head.link_order = NULL;
104d59d1 12047 if (attr_size)
b8a6ced7 12048 attr_section = o;
104d59d1 12049 else
5270eddc 12050 remove_section = TRUE;
104d59d1 12051 }
6e5e9d58
AM
12052 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12053 {
12054 /* Remove empty group section from linker output. */
5270eddc 12055 remove_section = TRUE;
b8a6ced7 12056 }
5270eddc 12057 if (remove_section)
b8a6ced7 12058 {
6e5e9d58
AM
12059 o->flags |= SEC_EXCLUDE;
12060 bfd_section_list_remove (abfd, o);
12061 abfd->section_count--;
4c6ee646 12062 sections_removed = TRUE;
6e5e9d58 12063 }
104d59d1 12064 }
4c6ee646
AM
12065 if (sections_removed)
12066 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12067
c152c796
AM
12068 /* Count up the number of relocations we will output for each output
12069 section, so that we know the sizes of the reloc sections. We
12070 also figure out some maximum sizes. */
12071 max_contents_size = 0;
12072 max_external_reloc_size = 0;
12073 max_internal_reloc_count = 0;
12074 max_sym_count = 0;
12075 max_sym_shndx_count = 0;
12076 merged = FALSE;
12077 for (o = abfd->sections; o != NULL; o = o->next)
12078 {
12079 struct bfd_elf_section_data *esdo = elf_section_data (o);
12080 o->reloc_count = 0;
12081
8423293d 12082 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12083 {
12084 unsigned int reloc_count = 0;
9eaff861 12085 unsigned int additional_reloc_count = 0;
c152c796 12086 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12087
12088 if (p->type == bfd_section_reloc_link_order
12089 || p->type == bfd_symbol_reloc_link_order)
12090 reloc_count = 1;
12091 else if (p->type == bfd_indirect_link_order)
12092 {
12093 asection *sec;
12094
12095 sec = p->u.indirect.section;
c152c796
AM
12096
12097 /* Mark all sections which are to be included in the
12098 link. This will normally be every section. We need
12099 to do this so that we can identify any sections which
12100 the linker has decided to not include. */
12101 sec->linker_mark = TRUE;
12102
12103 if (sec->flags & SEC_MERGE)
12104 merged = TRUE;
12105
eea6121a
AM
12106 if (sec->rawsize > max_contents_size)
12107 max_contents_size = sec->rawsize;
12108 if (sec->size > max_contents_size)
12109 max_contents_size = sec->size;
c152c796 12110
c152c796
AM
12111 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12112 && (sec->owner->flags & DYNAMIC) == 0)
12113 {
12114 size_t sym_count;
12115
a961cdd5
AM
12116 /* We are interested in just local symbols, not all
12117 symbols. */
c152c796
AM
12118 if (elf_bad_symtab (sec->owner))
12119 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12120 / bed->s->sizeof_sym);
12121 else
12122 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12123
12124 if (sym_count > max_sym_count)
12125 max_sym_count = sym_count;
12126
12127 if (sym_count > max_sym_shndx_count
6a40cf0c 12128 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12129 max_sym_shndx_count = sym_count;
12130
a961cdd5
AM
12131 if (esdo->this_hdr.sh_type == SHT_REL
12132 || esdo->this_hdr.sh_type == SHT_RELA)
12133 /* Some backends use reloc_count in relocation sections
12134 to count particular types of relocs. Of course,
12135 reloc sections themselves can't have relocations. */
12136 ;
12137 else if (emit_relocs)
12138 {
12139 reloc_count = sec->reloc_count;
12140 if (bed->elf_backend_count_additional_relocs)
12141 {
12142 int c;
12143 c = (*bed->elf_backend_count_additional_relocs) (sec);
12144 additional_reloc_count += c;
12145 }
12146 }
12147 else if (bed->elf_backend_count_relocs)
12148 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12149
12150 esdi = elf_section_data (sec);
12151
c152c796
AM
12152 if ((sec->flags & SEC_RELOC) != 0)
12153 {
d4730f92 12154 size_t ext_size = 0;
c152c796 12155
d4730f92
BS
12156 if (esdi->rel.hdr != NULL)
12157 ext_size = esdi->rel.hdr->sh_size;
12158 if (esdi->rela.hdr != NULL)
12159 ext_size += esdi->rela.hdr->sh_size;
7326c758 12160
c152c796
AM
12161 if (ext_size > max_external_reloc_size)
12162 max_external_reloc_size = ext_size;
12163 if (sec->reloc_count > max_internal_reloc_count)
12164 max_internal_reloc_count = sec->reloc_count;
12165 }
12166 }
12167 }
12168
12169 if (reloc_count == 0)
12170 continue;
12171
9eaff861 12172 reloc_count += additional_reloc_count;
c152c796
AM
12173 o->reloc_count += reloc_count;
12174
0e1862bb 12175 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12176 {
d4730f92 12177 if (esdi->rel.hdr)
9eaff861 12178 {
491d01d3 12179 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12180 esdo->rel.count += additional_reloc_count;
12181 }
d4730f92 12182 if (esdi->rela.hdr)
9eaff861 12183 {
491d01d3 12184 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12185 esdo->rela.count += additional_reloc_count;
12186 }
d4730f92
BS
12187 }
12188 else
12189 {
12190 if (o->use_rela_p)
12191 esdo->rela.count += reloc_count;
2c2b4ed4 12192 else
d4730f92 12193 esdo->rel.count += reloc_count;
c152c796 12194 }
c152c796
AM
12195 }
12196
9eaff861 12197 if (o->reloc_count > 0)
c152c796
AM
12198 o->flags |= SEC_RELOC;
12199 else
12200 {
12201 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12202 set it (this is probably a bug) and if it is set
12203 assign_section_numbers will create a reloc section. */
12204 o->flags &=~ SEC_RELOC;
12205 }
12206
12207 /* If the SEC_ALLOC flag is not set, force the section VMA to
12208 zero. This is done in elf_fake_sections as well, but forcing
12209 the VMA to 0 here will ensure that relocs against these
12210 sections are handled correctly. */
12211 if ((o->flags & SEC_ALLOC) == 0
12212 && ! o->user_set_vma)
12213 o->vma = 0;
12214 }
12215
0e1862bb 12216 if (! bfd_link_relocatable (info) && merged)
64f52338 12217 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12218
12219 /* Figure out the file positions for everything but the symbol table
12220 and the relocs. We set symcount to force assign_section_numbers
12221 to create a symbol table. */
ed48ec2e 12222 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12223 BFD_ASSERT (! abfd->output_has_begun);
12224 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12225 goto error_return;
12226
ee75fd95 12227 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12228 for (o = abfd->sections; o != NULL; o = o->next)
12229 {
d4730f92 12230 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12231 if ((o->flags & SEC_RELOC) != 0)
12232 {
d4730f92 12233 if (esdo->rel.hdr
9eaff861 12234 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12235 goto error_return;
12236
d4730f92 12237 if (esdo->rela.hdr
9eaff861 12238 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12239 goto error_return;
12240 }
12241
48db3297
AM
12242 /* _bfd_elf_compute_section_file_positions makes temporary use
12243 of target_index. Reset it. */
12244 o->target_index = 0;
12245
c152c796
AM
12246 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12247 to count upwards while actually outputting the relocations. */
d4730f92
BS
12248 esdo->rel.count = 0;
12249 esdo->rela.count = 0;
0ce398f1 12250
1ff6de03
NA
12251 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12252 && !bfd_section_is_ctf (o))
0ce398f1
L
12253 {
12254 /* Cache the section contents so that they can be compressed
12255 later. Use bfd_malloc since it will be freed by
12256 bfd_compress_section_contents. */
12257 unsigned char *contents = esdo->this_hdr.contents;
12258 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12259 abort ();
12260 contents
12261 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12262 if (contents == NULL)
12263 goto error_return;
12264 esdo->this_hdr.contents = contents;
12265 }
c152c796
AM
12266 }
12267
1ff6de03
NA
12268 /* We have now assigned file positions for all the sections except .symtab,
12269 .strtab, and non-loaded reloc and compressed debugging sections. We start
12270 the .symtab section at the current file position, and write directly to it.
12271 We build the .strtab section in memory. */
ed48ec2e 12272 abfd->symcount = 0;
c152c796
AM
12273 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12274 /* sh_name is set in prep_headers. */
12275 symtab_hdr->sh_type = SHT_SYMTAB;
12276 /* sh_flags, sh_addr and sh_size all start off zero. */
12277 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12278 /* sh_link is set in assign_section_numbers. */
12279 /* sh_info is set below. */
12280 /* sh_offset is set just below. */
72de5009 12281 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12282
ef10c3ac
L
12283 if (max_sym_count < 20)
12284 max_sym_count = 20;
64f52338 12285 htab->strtabsize = max_sym_count;
ef10c3ac 12286 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12287 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12288 if (htab->strtab == NULL)
c152c796 12289 goto error_return;
ef10c3ac
L
12290 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12291 flinfo.symshndxbuf
12292 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12293 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12294
8539e4e8 12295 if (info->strip != strip_all || emit_relocs)
c152c796 12296 {
c77cb2a0
MR
12297 bfd_boolean name_local_sections;
12298 const char *name;
12299
8539e4e8
AM
12300 file_ptr off = elf_next_file_pos (abfd);
12301
12302 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12303
12304 /* Note that at this point elf_next_file_pos (abfd) is
12305 incorrect. We do not yet know the size of the .symtab section.
12306 We correct next_file_pos below, after we do know the size. */
12307
12308 /* Start writing out the symbol table. The first symbol is always a
12309 dummy symbol. */
c152c796
AM
12310 elfsym.st_value = 0;
12311 elfsym.st_size = 0;
12312 elfsym.st_info = 0;
12313 elfsym.st_other = 0;
12314 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12315 elfsym.st_target_internal = 0;
ef10c3ac
L
12316 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12317 bfd_und_section_ptr, NULL) != 1)
c152c796 12318 goto error_return;
c152c796 12319
8539e4e8
AM
12320 /* Output a symbol for each section. We output these even if we are
12321 discarding local symbols, since they are used for relocs. These
c77cb2a0
MR
12322 symbols usually have no names. We store the index of each one in
12323 the index field of the section, so that we can find it again when
8539e4e8
AM
12324 outputting relocs. */
12325
c77cb2a0
MR
12326 name_local_sections
12327 = (bed->elf_backend_name_local_section_symbols
12328 && bed->elf_backend_name_local_section_symbols (abfd));
12329
12330 name = NULL;
c152c796
AM
12331 elfsym.st_size = 0;
12332 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12333 elfsym.st_other = 0;
f0b5bb34 12334 elfsym.st_value = 0;
35fc36a8 12335 elfsym.st_target_internal = 0;
c152c796
AM
12336 for (i = 1; i < elf_numsections (abfd); i++)
12337 {
12338 o = bfd_section_from_elf_index (abfd, i);
12339 if (o != NULL)
f0b5bb34
AM
12340 {
12341 o->target_index = bfd_get_symcount (abfd);
12342 elfsym.st_shndx = i;
0e1862bb 12343 if (!bfd_link_relocatable (info))
f0b5bb34 12344 elfsym.st_value = o->vma;
c77cb2a0
MR
12345 if (name_local_sections)
12346 name = o->name;
12347 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
ef10c3ac 12348 NULL) != 1)
f0b5bb34
AM
12349 goto error_return;
12350 }
c152c796
AM
12351 }
12352 }
12353
3f1b17bb
MR
12354 /* On some targets like Irix 5 the symbol split between local and global
12355 ones recorded in the sh_info field needs to be done between section
12356 and all other symbols. */
12357 if (bed->elf_backend_elfsym_local_is_section
12358 && bed->elf_backend_elfsym_local_is_section (abfd))
12359 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12360
c152c796
AM
12361 /* Allocate some memory to hold information read in from the input
12362 files. */
12363 if (max_contents_size != 0)
12364 {
8b127cbc
AM
12365 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12366 if (flinfo.contents == NULL)
c152c796
AM
12367 goto error_return;
12368 }
12369
12370 if (max_external_reloc_size != 0)
12371 {
8b127cbc
AM
12372 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12373 if (flinfo.external_relocs == NULL)
c152c796
AM
12374 goto error_return;
12375 }
12376
12377 if (max_internal_reloc_count != 0)
12378 {
056bafd4 12379 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12380 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12381 if (flinfo.internal_relocs == NULL)
c152c796
AM
12382 goto error_return;
12383 }
12384
12385 if (max_sym_count != 0)
12386 {
12387 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12388 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12389 if (flinfo.external_syms == NULL)
c152c796
AM
12390 goto error_return;
12391
12392 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12393 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12394 if (flinfo.internal_syms == NULL)
c152c796
AM
12395 goto error_return;
12396
12397 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12398 flinfo.indices = (long int *) bfd_malloc (amt);
12399 if (flinfo.indices == NULL)
c152c796
AM
12400 goto error_return;
12401
12402 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12403 flinfo.sections = (asection **) bfd_malloc (amt);
12404 if (flinfo.sections == NULL)
c152c796
AM
12405 goto error_return;
12406 }
12407
12408 if (max_sym_shndx_count != 0)
12409 {
12410 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12411 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12412 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12413 goto error_return;
12414 }
12415
64f52338 12416 if (htab->tls_sec)
c152c796 12417 {
66631823 12418 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12419 asection *sec;
12420
64f52338 12421 for (sec = htab->tls_sec;
c152c796
AM
12422 sec && (sec->flags & SEC_THREAD_LOCAL);
12423 sec = sec->next)
12424 {
3a800eb9 12425 bfd_size_type size = sec->size;
66631823 12426 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12427
3a800eb9
AM
12428 if (size == 0
12429 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12430 {
91d6fa6a
NC
12431 struct bfd_link_order *ord = sec->map_tail.link_order;
12432
12433 if (ord != NULL)
66631823 12434 size = ord->offset * opb + ord->size;
c152c796 12435 }
66631823 12436 end = sec->vma + size / opb;
c152c796 12437 }
64f52338 12438 base = htab->tls_sec->vma;
7dc98aea
RO
12439 /* Only align end of TLS section if static TLS doesn't have special
12440 alignment requirements. */
12441 if (bed->static_tls_alignment == 1)
64f52338
AM
12442 end = align_power (end, htab->tls_sec->alignment_power);
12443 htab->tls_size = end - base;
c152c796
AM
12444 }
12445
0b52efa6
PB
12446 /* Reorder SHF_LINK_ORDER sections. */
12447 for (o = abfd->sections; o != NULL; o = o->next)
12448 {
12449 if (!elf_fixup_link_order (abfd, o))
12450 return FALSE;
12451 }
12452
2f0c68f2
CM
12453 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12454 return FALSE;
12455
c152c796
AM
12456 /* Since ELF permits relocations to be against local symbols, we
12457 must have the local symbols available when we do the relocations.
12458 Since we would rather only read the local symbols once, and we
12459 would rather not keep them in memory, we handle all the
12460 relocations for a single input file at the same time.
12461
12462 Unfortunately, there is no way to know the total number of local
12463 symbols until we have seen all of them, and the local symbol
12464 indices precede the global symbol indices. This means that when
12465 we are generating relocatable output, and we see a reloc against
12466 a global symbol, we can not know the symbol index until we have
12467 finished examining all the local symbols to see which ones we are
12468 going to output. To deal with this, we keep the relocations in
12469 memory, and don't output them until the end of the link. This is
12470 an unfortunate waste of memory, but I don't see a good way around
12471 it. Fortunately, it only happens when performing a relocatable
12472 link, which is not the common case. FIXME: If keep_memory is set
12473 we could write the relocs out and then read them again; I don't
12474 know how bad the memory loss will be. */
12475
c72f2fb2 12476 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12477 sub->output_has_begun = FALSE;
12478 for (o = abfd->sections; o != NULL; o = o->next)
12479 {
8423293d 12480 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12481 {
12482 if (p->type == bfd_indirect_link_order
12483 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12484 == bfd_target_elf_flavour)
12485 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12486 {
12487 if (! sub->output_has_begun)
12488 {
8b127cbc 12489 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12490 goto error_return;
12491 sub->output_has_begun = TRUE;
12492 }
12493 }
12494 else if (p->type == bfd_section_reloc_link_order
12495 || p->type == bfd_symbol_reloc_link_order)
12496 {
12497 if (! elf_reloc_link_order (abfd, info, o, p))
12498 goto error_return;
12499 }
12500 else
12501 {
12502 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12503 {
12504 if (p->type == bfd_indirect_link_order
12505 && (bfd_get_flavour (sub)
12506 == bfd_target_elf_flavour)
12507 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12508 != bed->s->elfclass))
12509 {
12510 const char *iclass, *oclass;
12511
aebf9be7 12512 switch (bed->s->elfclass)
351f65ca 12513 {
aebf9be7
NC
12514 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12515 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12516 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12517 default: abort ();
351f65ca 12518 }
aebf9be7
NC
12519
12520 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12521 {
aebf9be7
NC
12522 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12523 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12524 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12525 default: abort ();
351f65ca
L
12526 }
12527
12528 bfd_set_error (bfd_error_wrong_format);
4eca0228 12529 _bfd_error_handler
695344c0 12530 /* xgettext:c-format */
871b3ab2 12531 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12532 sub, iclass, oclass);
12533 }
12534
12535 goto error_return;
12536 }
c152c796
AM
12537 }
12538 }
12539 }
12540
c0f00686
L
12541 /* Free symbol buffer if needed. */
12542 if (!info->reduce_memory_overheads)
12543 {
c72f2fb2 12544 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12545 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12546 {
12547 free (elf_tdata (sub)->symbuf);
12548 elf_tdata (sub)->symbuf = NULL;
12549 }
12550 }
12551
c152c796
AM
12552 /* Output any global symbols that got converted to local in a
12553 version script or due to symbol visibility. We do this in a
12554 separate step since ELF requires all local symbols to appear
12555 prior to any global symbols. FIXME: We should only do this if
12556 some global symbols were, in fact, converted to become local.
12557 FIXME: Will this work correctly with the Irix 5 linker? */
12558 eoinfo.failed = FALSE;
8b127cbc 12559 eoinfo.flinfo = &flinfo;
c152c796 12560 eoinfo.localsyms = TRUE;
34a79995 12561 eoinfo.file_sym_done = FALSE;
7686d77d 12562 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12563 if (eoinfo.failed)
12564 return FALSE;
12565
4e617b1e
PB
12566 /* If backend needs to output some local symbols not present in the hash
12567 table, do it now. */
8539e4e8
AM
12568 if (bed->elf_backend_output_arch_local_syms
12569 && (info->strip != strip_all || emit_relocs))
4e617b1e 12570 {
6e0b88f1 12571 typedef int (*out_sym_func)
4e617b1e
PB
12572 (void *, const char *, Elf_Internal_Sym *, asection *,
12573 struct elf_link_hash_entry *);
12574
12575 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12576 (abfd, info, &flinfo,
12577 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12578 return FALSE;
12579 }
12580
c152c796
AM
12581 /* That wrote out all the local symbols. Finish up the symbol table
12582 with the global symbols. Even if we want to strip everything we
12583 can, we still need to deal with those global symbols that got
12584 converted to local in a version script. */
12585
12586 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12587 if (!symtab_hdr->sh_info)
12588 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12589
12590 if (dynamic
64f52338
AM
12591 && htab->dynsym != NULL
12592 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12593 {
12594 Elf_Internal_Sym sym;
64f52338 12595 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12596
64f52338
AM
12597 o = htab->dynsym->output_section;
12598 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12599
12600 /* Write out the section symbols for the output sections. */
0e1862bb 12601 if (bfd_link_pic (info)
64f52338 12602 || htab->is_relocatable_executable)
c152c796
AM
12603 {
12604 asection *s;
12605
12606 sym.st_size = 0;
12607 sym.st_name = 0;
12608 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12609 sym.st_other = 0;
35fc36a8 12610 sym.st_target_internal = 0;
c152c796
AM
12611
12612 for (s = abfd->sections; s != NULL; s = s->next)
12613 {
12614 int indx;
12615 bfd_byte *dest;
12616 long dynindx;
12617
c152c796 12618 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12619 if (dynindx <= 0)
12620 continue;
12621 indx = elf_section_data (s)->this_idx;
c152c796
AM
12622 BFD_ASSERT (indx > 0);
12623 sym.st_shndx = indx;
c0d5a53d
L
12624 if (! check_dynsym (abfd, &sym))
12625 return FALSE;
c152c796
AM
12626 sym.st_value = s->vma;
12627 dest = dynsym + dynindx * bed->s->sizeof_sym;
12628 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12629 }
c152c796
AM
12630 }
12631
12632 /* Write out the local dynsyms. */
64f52338 12633 if (htab->dynlocal)
c152c796
AM
12634 {
12635 struct elf_link_local_dynamic_entry *e;
64f52338 12636 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12637 {
12638 asection *s;
12639 bfd_byte *dest;
12640
935bd1e0 12641 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12642 Note that we saved a word of storage and overwrote
12643 the original st_name with the dynstr_index. */
12644 sym = e->isym;
935bd1e0 12645 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12646 sym.st_shndx = SHN_UNDEF;
c152c796 12647
cb33740c
AM
12648 s = bfd_section_from_elf_index (e->input_bfd,
12649 e->isym.st_shndx);
4d68fd75
AM
12650 if (s != NULL
12651 && s->output_section != NULL
12652 && elf_section_data (s->output_section) != NULL)
c152c796 12653 {
c152c796
AM
12654 sym.st_shndx =
12655 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12656 if (! check_dynsym (abfd, &sym))
12657 return FALSE;
c152c796
AM
12658 sym.st_value = (s->output_section->vma
12659 + s->output_offset
12660 + e->isym.st_value);
12661 }
12662
c152c796
AM
12663 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12664 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12665 }
12666 }
c152c796
AM
12667 }
12668
12669 /* We get the global symbols from the hash table. */
12670 eoinfo.failed = FALSE;
12671 eoinfo.localsyms = FALSE;
8b127cbc 12672 eoinfo.flinfo = &flinfo;
7686d77d 12673 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12674 if (eoinfo.failed)
12675 return FALSE;
12676
12677 /* If backend needs to output some symbols not present in the hash
12678 table, do it now. */
8539e4e8
AM
12679 if (bed->elf_backend_output_arch_syms
12680 && (info->strip != strip_all || emit_relocs))
c152c796 12681 {
6e0b88f1 12682 typedef int (*out_sym_func)
c152c796
AM
12683 (void *, const char *, Elf_Internal_Sym *, asection *,
12684 struct elf_link_hash_entry *);
12685
12686 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12687 (abfd, info, &flinfo,
12688 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12689 return FALSE;
12690 }
12691
ef10c3ac
L
12692 /* Finalize the .strtab section. */
12693 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12694
12695 /* Swap out the .strtab section. */
12696 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12697 return FALSE;
12698
12699 /* Now we know the size of the symtab section. */
c152c796
AM
12700 if (bfd_get_symcount (abfd) > 0)
12701 {
ee3b52e9
L
12702 /* Finish up and write out the symbol string table (.strtab)
12703 section. */
ad32986f 12704 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12705 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12706
ad32986f 12707 if (elf_symtab_shndx_list (abfd))
8539e4e8 12708 {
ad32986f 12709 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12710
ad32986f
NC
12711 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12712 {
12713 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12714 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12715 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12716 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12717 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12718
ad32986f
NC
12719 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12720 off, TRUE);
12721
12722 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12723 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12724 return FALSE;
12725 }
8539e4e8 12726 }
ee3b52e9
L
12727
12728 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12729 /* sh_name was set in prep_headers. */
12730 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12731 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12732 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12733 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12734 symstrtab_hdr->sh_entsize = 0;
12735 symstrtab_hdr->sh_link = 0;
12736 symstrtab_hdr->sh_info = 0;
12737 /* sh_offset is set just below. */
12738 symstrtab_hdr->sh_addralign = 1;
12739
12740 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12741 off, TRUE);
12742 elf_next_file_pos (abfd) = off;
12743
c152c796 12744 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12745 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12746 return FALSE;
12747 }
12748
76359541
TP
12749 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12750 {
871b3ab2 12751 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12752 info->out_implib_bfd);
76359541
TP
12753 return FALSE;
12754 }
12755
c152c796
AM
12756 /* Adjust the relocs to have the correct symbol indices. */
12757 for (o = abfd->sections; o != NULL; o = o->next)
12758 {
d4730f92 12759 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12760 bfd_boolean sort;
10bbbc1d 12761
c152c796
AM
12762 if ((o->flags & SEC_RELOC) == 0)
12763 continue;
12764
28dbcedc 12765 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12766 if (esdo->rel.hdr != NULL
10bbbc1d 12767 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12768 return FALSE;
12769 if (esdo->rela.hdr != NULL
10bbbc1d 12770 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12771 return FALSE;
c152c796
AM
12772
12773 /* Set the reloc_count field to 0 to prevent write_relocs from
12774 trying to swap the relocs out itself. */
12775 o->reloc_count = 0;
12776 }
12777
12778 if (dynamic && info->combreloc && dynobj != NULL)
12779 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12780
12781 /* If we are linking against a dynamic object, or generating a
12782 shared library, finish up the dynamic linking information. */
12783 if (dynamic)
12784 {
12785 bfd_byte *dyncon, *dynconend;
12786
12787 /* Fix up .dynamic entries. */
3d4d4302 12788 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12789 BFD_ASSERT (o != NULL);
12790
12791 dyncon = o->contents;
eea6121a 12792 dynconend = o->contents + o->size;
c152c796
AM
12793 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12794 {
12795 Elf_Internal_Dyn dyn;
12796 const char *name;
12797 unsigned int type;
64487780
AM
12798 bfd_size_type sh_size;
12799 bfd_vma sh_addr;
c152c796
AM
12800
12801 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12802
12803 switch (dyn.d_tag)
12804 {
12805 default:
12806 continue;
12807 case DT_NULL:
12808 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12809 {
12810 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12811 {
12812 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12813 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12814 default: continue;
12815 }
12816 dyn.d_un.d_val = relativecount;
12817 relativecount = 0;
12818 break;
12819 }
12820 continue;
12821
12822 case DT_INIT:
12823 name = info->init_function;
12824 goto get_sym;
12825 case DT_FINI:
12826 name = info->fini_function;
12827 get_sym:
12828 {
12829 struct elf_link_hash_entry *h;
12830
64f52338 12831 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12832 if (h != NULL
12833 && (h->root.type == bfd_link_hash_defined
12834 || h->root.type == bfd_link_hash_defweak))
12835 {
bef26483 12836 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12837 o = h->root.u.def.section;
12838 if (o->output_section != NULL)
bef26483 12839 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12840 + o->output_offset);
12841 else
12842 {
12843 /* The symbol is imported from another shared
12844 library and does not apply to this one. */
bef26483 12845 dyn.d_un.d_ptr = 0;
c152c796
AM
12846 }
12847 break;
12848 }
12849 }
12850 continue;
12851
12852 case DT_PREINIT_ARRAYSZ:
12853 name = ".preinit_array";
4ade44b7 12854 goto get_out_size;
c152c796
AM
12855 case DT_INIT_ARRAYSZ:
12856 name = ".init_array";
4ade44b7 12857 goto get_out_size;
c152c796
AM
12858 case DT_FINI_ARRAYSZ:
12859 name = ".fini_array";
4ade44b7 12860 get_out_size:
c152c796
AM
12861 o = bfd_get_section_by_name (abfd, name);
12862 if (o == NULL)
12863 {
4eca0228 12864 _bfd_error_handler
4ade44b7 12865 (_("could not find section %s"), name);
c152c796
AM
12866 goto error_return;
12867 }
eea6121a 12868 if (o->size == 0)
4eca0228 12869 _bfd_error_handler
c152c796 12870 (_("warning: %s section has zero size"), name);
eea6121a 12871 dyn.d_un.d_val = o->size;
c152c796
AM
12872 break;
12873
12874 case DT_PREINIT_ARRAY:
12875 name = ".preinit_array";
4ade44b7 12876 goto get_out_vma;
c152c796
AM
12877 case DT_INIT_ARRAY:
12878 name = ".init_array";
4ade44b7 12879 goto get_out_vma;
c152c796
AM
12880 case DT_FINI_ARRAY:
12881 name = ".fini_array";
4ade44b7
AM
12882 get_out_vma:
12883 o = bfd_get_section_by_name (abfd, name);
12884 goto do_vma;
c152c796
AM
12885
12886 case DT_HASH:
12887 name = ".hash";
12888 goto get_vma;
fdc90cb4
JJ
12889 case DT_GNU_HASH:
12890 name = ".gnu.hash";
12891 goto get_vma;
c152c796
AM
12892 case DT_STRTAB:
12893 name = ".dynstr";
12894 goto get_vma;
12895 case DT_SYMTAB:
12896 name = ".dynsym";
12897 goto get_vma;
12898 case DT_VERDEF:
12899 name = ".gnu.version_d";
12900 goto get_vma;
12901 case DT_VERNEED:
12902 name = ".gnu.version_r";
12903 goto get_vma;
12904 case DT_VERSYM:
12905 name = ".gnu.version";
12906 get_vma:
4ade44b7
AM
12907 o = bfd_get_linker_section (dynobj, name);
12908 do_vma:
b3293efa 12909 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12910 {
4eca0228 12911 _bfd_error_handler
4ade44b7 12912 (_("could not find section %s"), name);
c152c796
AM
12913 goto error_return;
12914 }
894891db
NC
12915 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12916 {
4eca0228 12917 _bfd_error_handler
894891db
NC
12918 (_("warning: section '%s' is being made into a note"), name);
12919 bfd_set_error (bfd_error_nonrepresentable_section);
12920 goto error_return;
12921 }
4ade44b7 12922 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12923 break;
12924
12925 case DT_REL:
12926 case DT_RELA:
12927 case DT_RELSZ:
12928 case DT_RELASZ:
12929 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12930 type = SHT_REL;
12931 else
12932 type = SHT_RELA;
64487780
AM
12933 sh_size = 0;
12934 sh_addr = 0;
c152c796
AM
12935 for (i = 1; i < elf_numsections (abfd); i++)
12936 {
12937 Elf_Internal_Shdr *hdr;
12938
12939 hdr = elf_elfsections (abfd)[i];
12940 if (hdr->sh_type == type
12941 && (hdr->sh_flags & SHF_ALLOC) != 0)
12942 {
64487780
AM
12943 sh_size += hdr->sh_size;
12944 if (sh_addr == 0
12945 || sh_addr > hdr->sh_addr)
12946 sh_addr = hdr->sh_addr;
c152c796
AM
12947 }
12948 }
64487780 12949
64f52338
AM
12950 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12951 {
66631823
CE
12952 unsigned int opb = bfd_octets_per_byte (abfd, o);
12953
64f52338
AM
12954 /* Don't count procedure linkage table relocs in the
12955 overall reloc count. */
64487780
AM
12956 sh_size -= htab->srelplt->size;
12957 if (sh_size == 0)
12958 /* If the size is zero, make the address zero too.
12959 This is to avoid a glibc bug. If the backend
12960 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12961 zero, then we'll put DT_RELA at the end of
12962 DT_JMPREL. glibc will interpret the end of
12963 DT_RELA matching the end of DT_JMPREL as the
12964 case where DT_RELA includes DT_JMPREL, and for
12965 LD_BIND_NOW will decide that processing DT_RELA
12966 will process the PLT relocs too. Net result:
12967 No PLT relocs applied. */
12968 sh_addr = 0;
12969
64f52338
AM
12970 /* If .rela.plt is the first .rela section, exclude
12971 it from DT_RELA. */
64487780 12972 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 12973 + htab->srelplt->output_offset) * opb)
64487780 12974 sh_addr += htab->srelplt->size;
64f52338 12975 }
64487780
AM
12976
12977 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12978 dyn.d_un.d_val = sh_size;
12979 else
12980 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12981 break;
12982 }
12983 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12984 }
12985 }
12986
12987 /* If we have created any dynamic sections, then output them. */
12988 if (dynobj != NULL)
12989 {
12990 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12991 goto error_return;
12992
943284cc 12993 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 12994 if (bfd_link_textrel_check (info)
3d4d4302 12995 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12996 {
12997 bfd_byte *dyncon, *dynconend;
12998
943284cc
DJ
12999 dyncon = o->contents;
13000 dynconend = o->contents + o->size;
13001 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13002 {
13003 Elf_Internal_Dyn dyn;
13004
13005 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13006
13007 if (dyn.d_tag == DT_TEXTREL)
13008 {
a6dbf402 13009 if (info->textrel_check == textrel_check_error)
c192a133 13010 info->callbacks->einfo
9793eb77 13011 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13012 else if (bfd_link_dll (info))
13013 info->callbacks->einfo
13014 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13015 else
13016 info->callbacks->einfo
a6dbf402 13017 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13018 break;
13019 }
13020 }
13021 }
13022
c152c796
AM
13023 for (o = dynobj->sections; o != NULL; o = o->next)
13024 {
13025 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13026 || o->size == 0
c152c796
AM
13027 || o->output_section == bfd_abs_section_ptr)
13028 continue;
13029 if ((o->flags & SEC_LINKER_CREATED) == 0)
13030 {
13031 /* At this point, we are only interested in sections
13032 created by _bfd_elf_link_create_dynamic_sections. */
13033 continue;
13034 }
64f52338 13035 if (htab->stab_info.stabstr == o)
3722b82f 13036 continue;
64f52338 13037 if (htab->eh_info.hdr_sec == o)
eea6121a 13038 continue;
3d4d4302 13039 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13040 {
bb294208
AM
13041 bfd_size_type octets = ((file_ptr) o->output_offset
13042 * bfd_octets_per_byte (abfd, o));
13043 if (!bfd_set_section_contents (abfd, o->output_section,
13044 o->contents, octets, o->size))
c152c796
AM
13045 goto error_return;
13046 }
13047 else
13048 {
13049 /* The contents of the .dynstr section are actually in a
13050 stringtab. */
8539e4e8
AM
13051 file_ptr off;
13052
c152c796
AM
13053 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13054 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13055 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13056 goto error_return;
13057 }
13058 }
13059 }
13060
7bdf4127 13061 if (!info->resolve_section_groups)
c152c796
AM
13062 {
13063 bfd_boolean failed = FALSE;
13064
7bdf4127 13065 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13066 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13067 if (failed)
13068 goto error_return;
13069 }
13070
13071 /* If we have optimized stabs strings, output them. */
64f52338 13072 if (htab->stab_info.stabstr != NULL)
c152c796 13073 {
64f52338 13074 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13075 goto error_return;
13076 }
13077
9f7c3e5e
AM
13078 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13079 goto error_return;
c152c796 13080
1ff6de03
NA
13081 if (info->callbacks->emit_ctf)
13082 info->callbacks->emit_ctf ();
13083
9f7c3e5e 13084 elf_final_link_free (abfd, &flinfo);
c152c796 13085
104d59d1
JM
13086 if (attr_section)
13087 {
a50b1753 13088 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13089 if (contents == NULL)
d0f16d5e 13090 return FALSE; /* Bail out and fail. */
104d59d1
JM
13091 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13092 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13093 free (contents);
13094 }
13095
c152c796
AM
13096 return TRUE;
13097
13098 error_return:
9f7c3e5e 13099 elf_final_link_free (abfd, &flinfo);
c152c796
AM
13100 return FALSE;
13101}
13102\f
5241d853
RS
13103/* Initialize COOKIE for input bfd ABFD. */
13104
13105static bfd_boolean
13106init_reloc_cookie (struct elf_reloc_cookie *cookie,
13107 struct bfd_link_info *info, bfd *abfd)
13108{
13109 Elf_Internal_Shdr *symtab_hdr;
13110 const struct elf_backend_data *bed;
13111
13112 bed = get_elf_backend_data (abfd);
13113 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13114
13115 cookie->abfd = abfd;
13116 cookie->sym_hashes = elf_sym_hashes (abfd);
13117 cookie->bad_symtab = elf_bad_symtab (abfd);
13118 if (cookie->bad_symtab)
13119 {
13120 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13121 cookie->extsymoff = 0;
13122 }
13123 else
13124 {
13125 cookie->locsymcount = symtab_hdr->sh_info;
13126 cookie->extsymoff = symtab_hdr->sh_info;
13127 }
13128
13129 if (bed->s->arch_size == 32)
13130 cookie->r_sym_shift = 8;
13131 else
13132 cookie->r_sym_shift = 32;
13133
13134 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13135 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13136 {
13137 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13138 cookie->locsymcount, 0,
13139 NULL, NULL, NULL);
13140 if (cookie->locsyms == NULL)
13141 {
13142 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13143 return FALSE;
13144 }
13145 if (info->keep_memory)
13146 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13147 }
13148 return TRUE;
13149}
13150
13151/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13152
13153static void
13154fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13155{
13156 Elf_Internal_Shdr *symtab_hdr;
13157
13158 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13159 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13160 free (cookie->locsyms);
13161}
13162
13163/* Initialize the relocation information in COOKIE for input section SEC
13164 of input bfd ABFD. */
13165
13166static bfd_boolean
13167init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13168 struct bfd_link_info *info, bfd *abfd,
13169 asection *sec)
13170{
5241d853
RS
13171 if (sec->reloc_count == 0)
13172 {
13173 cookie->rels = NULL;
13174 cookie->relend = NULL;
13175 }
13176 else
13177 {
5241d853
RS
13178 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13179 info->keep_memory);
13180 if (cookie->rels == NULL)
13181 return FALSE;
13182 cookie->rel = cookie->rels;
056bafd4 13183 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13184 }
13185 cookie->rel = cookie->rels;
13186 return TRUE;
13187}
13188
13189/* Free the memory allocated by init_reloc_cookie_rels,
13190 if appropriate. */
13191
13192static void
13193fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13194 asection *sec)
13195{
c9594989 13196 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13197 free (cookie->rels);
13198}
13199
13200/* Initialize the whole of COOKIE for input section SEC. */
13201
13202static bfd_boolean
13203init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13204 struct bfd_link_info *info,
13205 asection *sec)
13206{
13207 if (!init_reloc_cookie (cookie, info, sec->owner))
13208 goto error1;
13209 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13210 goto error2;
13211 return TRUE;
13212
13213 error2:
13214 fini_reloc_cookie (cookie, sec->owner);
13215 error1:
13216 return FALSE;
13217}
13218
13219/* Free the memory allocated by init_reloc_cookie_for_section,
13220 if appropriate. */
13221
13222static void
13223fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13224 asection *sec)
13225{
13226 fini_reloc_cookie_rels (cookie, sec);
13227 fini_reloc_cookie (cookie, sec->owner);
13228}
13229\f
c152c796
AM
13230/* Garbage collect unused sections. */
13231
07adf181
AM
13232/* Default gc_mark_hook. */
13233
13234asection *
13235_bfd_elf_gc_mark_hook (asection *sec,
13236 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13237 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13238 struct elf_link_hash_entry *h,
13239 Elf_Internal_Sym *sym)
13240{
13241 if (h != NULL)
13242 {
13243 switch (h->root.type)
13244 {
13245 case bfd_link_hash_defined:
13246 case bfd_link_hash_defweak:
13247 return h->root.u.def.section;
13248
13249 case bfd_link_hash_common:
13250 return h->root.u.c.p->section;
13251
13252 default:
13253 break;
13254 }
13255 }
13256 else
13257 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13258
13259 return NULL;
13260}
13261
9e223787 13262/* Return the debug definition section. */
b7c871ed
L
13263
13264static asection *
13265elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13266 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13267 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13268 struct elf_link_hash_entry *h,
9e223787 13269 Elf_Internal_Sym *sym)
b7c871ed 13270{
9e223787
L
13271 if (h != NULL)
13272 {
13273 /* Return the global debug definition section. */
13274 if ((h->root.type == bfd_link_hash_defined
13275 || h->root.type == bfd_link_hash_defweak)
13276 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13277 return h->root.u.def.section;
13278 }
13279 else
13280 {
13281 /* Return the local debug definition section. */
13282 asection *isec = bfd_section_from_elf_index (sec->owner,
13283 sym->st_shndx);
13284 if ((isec->flags & SEC_DEBUGGING) != 0)
13285 return isec;
13286 }
b7c871ed
L
13287
13288 return NULL;
13289}
13290
5241d853
RS
13291/* COOKIE->rel describes a relocation against section SEC, which is
13292 a section we've decided to keep. Return the section that contains
13293 the relocation symbol, or NULL if no section contains it. */
13294
13295asection *
13296_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13297 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13298 struct elf_reloc_cookie *cookie,
13299 bfd_boolean *start_stop)
5241d853
RS
13300{
13301 unsigned long r_symndx;
3024a17a 13302 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13303
13304 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13305 if (r_symndx == STN_UNDEF)
5241d853
RS
13306 return NULL;
13307
13308 if (r_symndx >= cookie->locsymcount
13309 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13310 {
13311 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13312 if (h == NULL)
13313 {
871b3ab2 13314 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13315 sec->owner);
13316 return NULL;
13317 }
5241d853
RS
13318 while (h->root.type == bfd_link_hash_indirect
13319 || h->root.type == bfd_link_hash_warning)
13320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13321 h->mark = 1;
3024a17a
AM
13322 /* Keep all aliases of the symbol too. If an object symbol
13323 needs to be copied into .dynbss then all of its aliases
13324 should be present as dynamic symbols, not just the one used
13325 on the copy relocation. */
13326 hw = h;
13327 while (hw->is_weakalias)
13328 {
13329 hw = hw->u.alias;
13330 hw->mark = 1;
13331 }
1cce69b9 13332
a6a4679f 13333 if (start_stop != NULL)
1cce69b9 13334 {
7dba9362
AM
13335 /* To work around a glibc bug, mark XXX input sections
13336 when there is a reference to __start_XXX or __stop_XXX
13337 symbols. */
cbd0eecf 13338 if (h->start_stop)
1cce69b9 13339 {
cbd0eecf 13340 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13341 *start_stop = !s->gc_mark;
13342 return s;
1cce69b9
AM
13343 }
13344 }
13345
5241d853
RS
13346 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13347 }
13348
13349 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13350 &cookie->locsyms[r_symndx]);
13351}
13352
13353/* COOKIE->rel describes a relocation against section SEC, which is
13354 a section we've decided to keep. Mark the section that contains
9d0a14d3 13355 the relocation symbol. */
5241d853
RS
13356
13357bfd_boolean
13358_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13359 asection *sec,
13360 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13361 struct elf_reloc_cookie *cookie)
5241d853
RS
13362{
13363 asection *rsec;
1cce69b9 13364 bfd_boolean start_stop = FALSE;
5241d853 13365
1cce69b9
AM
13366 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13367 while (rsec != NULL)
5241d853 13368 {
1cce69b9
AM
13369 if (!rsec->gc_mark)
13370 {
13371 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13372 || (rsec->owner->flags & DYNAMIC) != 0)
13373 rsec->gc_mark = 1;
13374 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13375 return FALSE;
13376 }
13377 if (!start_stop)
13378 break;
199af150 13379 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13380 }
13381 return TRUE;
13382}
13383
07adf181
AM
13384/* The mark phase of garbage collection. For a given section, mark
13385 it and any sections in this section's group, and all the sections
13386 which define symbols to which it refers. */
13387
ccfa59ea
AM
13388bfd_boolean
13389_bfd_elf_gc_mark (struct bfd_link_info *info,
13390 asection *sec,
6a5bb875 13391 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13392{
13393 bfd_boolean ret;
9d0a14d3 13394 asection *group_sec, *eh_frame;
c152c796
AM
13395
13396 sec->gc_mark = 1;
13397
13398 /* Mark all the sections in the group. */
13399 group_sec = elf_section_data (sec)->next_in_group;
13400 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13401 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13402 return FALSE;
13403
13404 /* Look through the section relocs. */
13405 ret = TRUE;
9d0a14d3
RS
13406 eh_frame = elf_eh_frame_section (sec->owner);
13407 if ((sec->flags & SEC_RELOC) != 0
13408 && sec->reloc_count > 0
13409 && sec != eh_frame)
c152c796 13410 {
5241d853 13411 struct elf_reloc_cookie cookie;
c152c796 13412
5241d853
RS
13413 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13414 ret = FALSE;
c152c796 13415 else
c152c796 13416 {
5241d853 13417 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13418 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13419 {
13420 ret = FALSE;
13421 break;
13422 }
13423 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13424 }
13425 }
9d0a14d3
RS
13426
13427 if (ret && eh_frame && elf_fde_list (sec))
13428 {
13429 struct elf_reloc_cookie cookie;
13430
13431 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13432 ret = FALSE;
13433 else
13434 {
13435 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13436 gc_mark_hook, &cookie))
13437 ret = FALSE;
13438 fini_reloc_cookie_for_section (&cookie, eh_frame);
13439 }
13440 }
13441
2f0c68f2
CM
13442 eh_frame = elf_section_eh_frame_entry (sec);
13443 if (ret && eh_frame && !eh_frame->gc_mark)
13444 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13445 ret = FALSE;
13446
c152c796
AM
13447 return ret;
13448}
13449
3c758495
TG
13450/* Scan and mark sections in a special or debug section group. */
13451
13452static void
13453_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13454{
13455 /* Point to first section of section group. */
13456 asection *ssec;
13457 /* Used to iterate the section group. */
13458 asection *msec;
13459
13460 bfd_boolean is_special_grp = TRUE;
13461 bfd_boolean is_debug_grp = TRUE;
13462
13463 /* First scan to see if group contains any section other than debug
13464 and special section. */
13465 ssec = msec = elf_next_in_group (grp);
13466 do
13467 {
13468 if ((msec->flags & SEC_DEBUGGING) == 0)
13469 is_debug_grp = FALSE;
13470
13471 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13472 is_special_grp = FALSE;
13473
13474 msec = elf_next_in_group (msec);
13475 }
13476 while (msec != ssec);
13477
13478 /* If this is a pure debug section group or pure special section group,
13479 keep all sections in this group. */
13480 if (is_debug_grp || is_special_grp)
13481 {
13482 do
13483 {
13484 msec->gc_mark = 1;
13485 msec = elf_next_in_group (msec);
13486 }
13487 while (msec != ssec);
13488 }
13489}
13490
7f6ab9f8
AM
13491/* Keep debug and special sections. */
13492
13493bfd_boolean
13494_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13495 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13496{
13497 bfd *ibfd;
13498
c72f2fb2 13499 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13500 {
13501 asection *isec;
13502 bfd_boolean some_kept;
b40bf0a2 13503 bfd_boolean debug_frag_seen;
b7c871ed 13504 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13505
13506 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13507 continue;
57963c05
AM
13508 isec = ibfd->sections;
13509 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13510 continue;
7f6ab9f8 13511
b40bf0a2
NC
13512 /* Ensure all linker created sections are kept,
13513 see if any other section is already marked,
13514 and note if we have any fragmented debug sections. */
b7c871ed 13515 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13516 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13517 {
13518 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13519 isec->gc_mark = 1;
eb026f09
AM
13520 else if (isec->gc_mark
13521 && (isec->flags & SEC_ALLOC) != 0
13522 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13523 some_kept = TRUE;
b7d07216
L
13524 else
13525 {
13526 /* Since all sections, except for backend specific ones,
13527 have been garbage collected, call mark_hook on this
13528 section if any of its linked-to sections is marked. */
13529 asection *linked_to_sec = elf_linked_to_section (isec);
13530 for (; linked_to_sec != NULL;
13531 linked_to_sec = elf_linked_to_section (linked_to_sec))
13532 if (linked_to_sec->gc_mark)
13533 {
13534 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13535 return FALSE;
13536 break;
13537 }
13538 }
b40bf0a2 13539
535b785f 13540 if (!debug_frag_seen
b40bf0a2
NC
13541 && (isec->flags & SEC_DEBUGGING)
13542 && CONST_STRNEQ (isec->name, ".debug_line."))
13543 debug_frag_seen = TRUE;
5242a0a0
L
13544 else if (strcmp (bfd_section_name (isec),
13545 "__patchable_function_entries") == 0
13546 && elf_linked_to_section (isec) == NULL)
13547 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13548 "need linked-to section "
13549 "for --gc-sections\n"),
13550 isec->owner, isec);
7f6ab9f8
AM
13551 }
13552
eb026f09
AM
13553 /* If no non-note alloc section in this file will be kept, then
13554 we can toss out the debug and special sections. */
7f6ab9f8
AM
13555 if (!some_kept)
13556 continue;
13557
13558 /* Keep debug and special sections like .comment when they are
3c758495 13559 not part of a group. Also keep section groups that contain
b7d07216
L
13560 just debug sections or special sections. NB: Sections with
13561 linked-to section has been handled above. */
7f6ab9f8 13562 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13563 {
13564 if ((isec->flags & SEC_GROUP) != 0)
13565 _bfd_elf_gc_mark_debug_special_section_group (isec);
13566 else if (((isec->flags & SEC_DEBUGGING) != 0
13567 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13568 && elf_next_in_group (isec) == NULL
13569 && elf_linked_to_section (isec) == NULL)
3c758495 13570 isec->gc_mark = 1;
b7c871ed
L
13571 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13572 has_kept_debug_info = TRUE;
3c758495 13573 }
b40bf0a2 13574
b40bf0a2
NC
13575 /* Look for CODE sections which are going to be discarded,
13576 and find and discard any fragmented debug sections which
13577 are associated with that code section. */
b7c871ed
L
13578 if (debug_frag_seen)
13579 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13580 if ((isec->flags & SEC_CODE) != 0
13581 && isec->gc_mark == 0)
13582 {
13583 unsigned int ilen;
13584 asection *dsec;
b40bf0a2 13585
b7c871ed 13586 ilen = strlen (isec->name);
b40bf0a2 13587
b7c871ed 13588 /* Association is determined by the name of the debug
07d6d2b8 13589 section containing the name of the code section as
b7c871ed
L
13590 a suffix. For example .debug_line.text.foo is a
13591 debug section associated with .text.foo. */
13592 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13593 {
13594 unsigned int dlen;
b40bf0a2 13595
b7c871ed
L
13596 if (dsec->gc_mark == 0
13597 || (dsec->flags & SEC_DEBUGGING) == 0)
13598 continue;
b40bf0a2 13599
b7c871ed 13600 dlen = strlen (dsec->name);
b40bf0a2 13601
b7c871ed
L
13602 if (dlen > ilen
13603 && strncmp (dsec->name + (dlen - ilen),
13604 isec->name, ilen) == 0)
b40bf0a2 13605 dsec->gc_mark = 0;
b7c871ed 13606 }
b40bf0a2 13607 }
b7c871ed
L
13608
13609 /* Mark debug sections referenced by kept debug sections. */
13610 if (has_kept_debug_info)
13611 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13612 if (isec->gc_mark
13613 && (isec->flags & SEC_DEBUGGING) != 0)
13614 if (!_bfd_elf_gc_mark (info, isec,
13615 elf_gc_mark_debug_section))
13616 return FALSE;
7f6ab9f8
AM
13617 }
13618 return TRUE;
13619}
13620
c152c796 13621static bfd_boolean
ccabcbe5 13622elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13623{
13624 bfd *sub;
ccabcbe5 13625 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13626
c72f2fb2 13627 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13628 {
13629 asection *o;
13630
b19a8f85 13631 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13632 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13633 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13634 continue;
57963c05
AM
13635 o = sub->sections;
13636 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13637 continue;
c152c796
AM
13638
13639 for (o = sub->sections; o != NULL; o = o->next)
13640 {
a33dafc3
L
13641 /* When any section in a section group is kept, we keep all
13642 sections in the section group. If the first member of
13643 the section group is excluded, we will also exclude the
13644 group section. */
13645 if (o->flags & SEC_GROUP)
13646 {
13647 asection *first = elf_next_in_group (o);
13648 o->gc_mark = first->gc_mark;
13649 }
c152c796 13650
1e7eae0d 13651 if (o->gc_mark)
c152c796
AM
13652 continue;
13653
13654 /* Skip sweeping sections already excluded. */
13655 if (o->flags & SEC_EXCLUDE)
13656 continue;
13657
13658 /* Since this is early in the link process, it is simple
13659 to remove a section from the output. */
13660 o->flags |= SEC_EXCLUDE;
13661
c55fe096 13662 if (info->print_gc_sections && o->size != 0)
695344c0 13663 /* xgettext:c-format */
9793eb77 13664 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13665 o, sub);
c152c796
AM
13666 }
13667 }
13668
c152c796
AM
13669 return TRUE;
13670}
13671
13672/* Propagate collected vtable information. This is called through
13673 elf_link_hash_traverse. */
13674
13675static bfd_boolean
13676elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13677{
c152c796 13678 /* Those that are not vtables. */
cbd0eecf
L
13679 if (h->start_stop
13680 || h->u2.vtable == NULL
13681 || h->u2.vtable->parent == NULL)
c152c796
AM
13682 return TRUE;
13683
13684 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13685 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13686 return TRUE;
13687
13688 /* If we've already been done, exit. */
cbd0eecf 13689 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13690 return TRUE;
13691
13692 /* Make sure the parent's table is up to date. */
cbd0eecf 13693 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13694
cbd0eecf 13695 if (h->u2.vtable->used == NULL)
c152c796
AM
13696 {
13697 /* None of this table's entries were referenced. Re-use the
13698 parent's table. */
cbd0eecf
L
13699 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13700 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13701 }
13702 else
13703 {
13704 size_t n;
13705 bfd_boolean *cu, *pu;
13706
13707 /* Or the parent's entries into ours. */
cbd0eecf 13708 cu = h->u2.vtable->used;
c152c796 13709 cu[-1] = TRUE;
cbd0eecf 13710 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13711 if (pu != NULL)
13712 {
13713 const struct elf_backend_data *bed;
13714 unsigned int log_file_align;
13715
13716 bed = get_elf_backend_data (h->root.u.def.section->owner);
13717 log_file_align = bed->s->log_file_align;
cbd0eecf 13718 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13719 while (n--)
13720 {
13721 if (*pu)
13722 *cu = TRUE;
13723 pu++;
13724 cu++;
13725 }
13726 }
13727 }
13728
13729 return TRUE;
13730}
13731
13732static bfd_boolean
13733elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13734{
13735 asection *sec;
13736 bfd_vma hstart, hend;
13737 Elf_Internal_Rela *relstart, *relend, *rel;
13738 const struct elf_backend_data *bed;
13739 unsigned int log_file_align;
13740
c152c796
AM
13741 /* Take care of both those symbols that do not describe vtables as
13742 well as those that are not loaded. */
cbd0eecf
L
13743 if (h->start_stop
13744 || h->u2.vtable == NULL
13745 || h->u2.vtable->parent == NULL)
c152c796
AM
13746 return TRUE;
13747
13748 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13749 || h->root.type == bfd_link_hash_defweak);
13750
13751 sec = h->root.u.def.section;
13752 hstart = h->root.u.def.value;
13753 hend = hstart + h->size;
13754
13755 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13756 if (!relstart)
13757 return *(bfd_boolean *) okp = FALSE;
13758 bed = get_elf_backend_data (sec->owner);
13759 log_file_align = bed->s->log_file_align;
13760
056bafd4 13761 relend = relstart + sec->reloc_count;
c152c796
AM
13762
13763 for (rel = relstart; rel < relend; ++rel)
13764 if (rel->r_offset >= hstart && rel->r_offset < hend)
13765 {
13766 /* If the entry is in use, do nothing. */
cbd0eecf
L
13767 if (h->u2.vtable->used
13768 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13769 {
13770 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13771 if (h->u2.vtable->used[entry])
c152c796
AM
13772 continue;
13773 }
13774 /* Otherwise, kill it. */
13775 rel->r_offset = rel->r_info = rel->r_addend = 0;
13776 }
13777
13778 return TRUE;
13779}
13780
87538722
AM
13781/* Mark sections containing dynamically referenced symbols. When
13782 building shared libraries, we must assume that any visible symbol is
13783 referenced. */
715df9b8 13784
64d03ab5
AM
13785bfd_boolean
13786bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13787{
87538722 13788 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13789 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13790
715df9b8
EB
13791 if ((h->root.type == bfd_link_hash_defined
13792 || h->root.type == bfd_link_hash_defweak)
d664fd41 13793 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13794 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13795 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13796 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13797 && (!bfd_link_executable (info)
22185505 13798 || info->gc_keep_exported
b407645f
AM
13799 || info->export_dynamic
13800 || (h->dynamic
13801 && d != NULL
13802 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13803 && (h->versioned >= versioned
54e8959c
L
13804 || !bfd_hide_sym_by_version (info->version_info,
13805 h->root.root.string)))))
715df9b8
EB
13806 h->root.u.def.section->flags |= SEC_KEEP;
13807
13808 return TRUE;
13809}
3b36f7e6 13810
74f0fb50
AM
13811/* Keep all sections containing symbols undefined on the command-line,
13812 and the section containing the entry symbol. */
13813
13814void
13815_bfd_elf_gc_keep (struct bfd_link_info *info)
13816{
13817 struct bfd_sym_chain *sym;
13818
13819 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13820 {
13821 struct elf_link_hash_entry *h;
13822
13823 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13824 FALSE, FALSE, FALSE);
13825
13826 if (h != NULL
13827 && (h->root.type == bfd_link_hash_defined
13828 || h->root.type == bfd_link_hash_defweak)
2f5541f3 13829 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
13830 h->root.u.def.section->flags |= SEC_KEEP;
13831 }
13832}
13833
2f0c68f2
CM
13834bfd_boolean
13835bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13836 struct bfd_link_info *info)
13837{
13838 bfd *ibfd = info->input_bfds;
13839
13840 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13841 {
13842 asection *sec;
13843 struct elf_reloc_cookie cookie;
13844
13845 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13846 continue;
57963c05
AM
13847 sec = ibfd->sections;
13848 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13849 continue;
2f0c68f2
CM
13850
13851 if (!init_reloc_cookie (&cookie, info, ibfd))
13852 return FALSE;
13853
13854 for (sec = ibfd->sections; sec; sec = sec->next)
13855 {
fd361982 13856 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13857 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13858 {
13859 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13860 fini_reloc_cookie_rels (&cookie, sec);
13861 }
13862 }
13863 }
13864 return TRUE;
13865}
13866
c152c796
AM
13867/* Do mark and sweep of unused sections. */
13868
13869bfd_boolean
13870bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13871{
13872 bfd_boolean ok = TRUE;
13873 bfd *sub;
6a5bb875 13874 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13875 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13876 struct elf_link_hash_table *htab;
c152c796 13877
64d03ab5 13878 if (!bed->can_gc_sections
715df9b8 13879 || !is_elf_hash_table (info->hash))
c152c796 13880 {
9793eb77 13881 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13882 return TRUE;
13883 }
13884
74f0fb50 13885 bed->gc_keep (info);
da44f4e5 13886 htab = elf_hash_table (info);
74f0fb50 13887
9d0a14d3
RS
13888 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13889 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13890 for (sub = info->input_bfds;
13891 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13892 sub = sub->link.next)
9d0a14d3
RS
13893 {
13894 asection *sec;
13895 struct elf_reloc_cookie cookie;
13896
57963c05
AM
13897 sec = sub->sections;
13898 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13899 continue;
9d0a14d3 13900 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13901 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13902 {
13903 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13904 if (elf_section_data (sec)->sec_info
13905 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13906 elf_eh_frame_section (sub) = sec;
13907 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13908 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13909 }
13910 }
9d0a14d3 13911
c152c796 13912 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13913 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13914 if (!ok)
13915 return FALSE;
13916
13917 /* Kill the vtable relocations that were not used. */
da44f4e5 13918 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13919 if (!ok)
13920 return FALSE;
13921
715df9b8 13922 /* Mark dynamically referenced symbols. */
22185505 13923 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13924 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13925
715df9b8 13926 /* Grovel through relocs to find out who stays ... */
64d03ab5 13927 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13928 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13929 {
13930 asection *o;
13931
b19a8f85 13932 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13933 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13934 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13935 continue;
13936
57963c05
AM
13937 o = sub->sections;
13938 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13939 continue;
13940
7f6ab9f8
AM
13941 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13942 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13943 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13944 well as FINI_ARRAY sections for ld -r. */
c152c796 13945 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13946 if (!o->gc_mark
13947 && (o->flags & SEC_EXCLUDE) == 0
24007750 13948 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13949 || (bfd_link_relocatable (info)
13950 && ((elf_section_data (o)->this_hdr.sh_type
13951 == SHT_PREINIT_ARRAY)
13952 || (elf_section_data (o)->this_hdr.sh_type
13953 == SHT_INIT_ARRAY)
13954 || (elf_section_data (o)->this_hdr.sh_type
13955 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13956 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13957 && elf_next_in_group (o) == NULL )))
13958 {
13959 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13960 return FALSE;
13961 }
c152c796
AM
13962 }
13963
6a5bb875 13964 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13965 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13966
c152c796 13967 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13968 return elf_gc_sweep (abfd, info);
c152c796
AM
13969}
13970\f
13971/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13972
13973bfd_boolean
13974bfd_elf_gc_record_vtinherit (bfd *abfd,
13975 asection *sec,
13976 struct elf_link_hash_entry *h,
13977 bfd_vma offset)
13978{
13979 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13980 struct elf_link_hash_entry **search, *child;
ef53be89 13981 size_t extsymcount;
c152c796
AM
13982 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13983
13984 /* The sh_info field of the symtab header tells us where the
13985 external symbols start. We don't care about the local symbols at
13986 this point. */
13987 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13988 if (!elf_bad_symtab (abfd))
13989 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13990
13991 sym_hashes = elf_sym_hashes (abfd);
13992 sym_hashes_end = sym_hashes + extsymcount;
13993
13994 /* Hunt down the child symbol, which is in this section at the same
13995 offset as the relocation. */
13996 for (search = sym_hashes; search != sym_hashes_end; ++search)
13997 {
13998 if ((child = *search) != NULL
13999 && (child->root.type == bfd_link_hash_defined
14000 || child->root.type == bfd_link_hash_defweak)
14001 && child->root.u.def.section == sec
14002 && child->root.u.def.value == offset)
14003 goto win;
14004 }
14005
695344c0 14006 /* xgettext:c-format */
9793eb77 14007 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14008 abfd, sec, (uint64_t) offset);
c152c796
AM
14009 bfd_set_error (bfd_error_invalid_operation);
14010 return FALSE;
14011
14012 win:
cbd0eecf 14013 if (!child->u2.vtable)
f6e332e6 14014 {
cbd0eecf
L
14015 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14016 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14017 if (!child->u2.vtable)
f6e332e6
AM
14018 return FALSE;
14019 }
c152c796
AM
14020 if (!h)
14021 {
14022 /* This *should* only be the absolute section. It could potentially
14023 be that someone has defined a non-global vtable though, which
14024 would be bad. It isn't worth paging in the local symbols to be
14025 sure though; that case should simply be handled by the assembler. */
14026
cbd0eecf 14027 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14028 }
14029 else
cbd0eecf 14030 child->u2.vtable->parent = h;
c152c796
AM
14031
14032 return TRUE;
14033}
14034
14035/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14036
14037bfd_boolean
a0ea3a14 14038bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14039 struct elf_link_hash_entry *h,
14040 bfd_vma addend)
14041{
14042 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14043 unsigned int log_file_align = bed->s->log_file_align;
14044
a0ea3a14
L
14045 if (!h)
14046 {
14047 /* xgettext:c-format */
14048 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14049 abfd, sec);
14050 bfd_set_error (bfd_error_bad_value);
14051 return FALSE;
14052 }
14053
cbd0eecf 14054 if (!h->u2.vtable)
f6e332e6 14055 {
cbd0eecf
L
14056 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14057 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14058 if (!h->u2.vtable)
f6e332e6
AM
14059 return FALSE;
14060 }
14061
cbd0eecf 14062 if (addend >= h->u2.vtable->size)
c152c796
AM
14063 {
14064 size_t size, bytes, file_align;
cbd0eecf 14065 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14066
14067 /* While the symbol is undefined, we have to be prepared to handle
14068 a zero size. */
14069 file_align = 1 << log_file_align;
14070 if (h->root.type == bfd_link_hash_undefined)
14071 size = addend + file_align;
14072 else
14073 {
14074 size = h->size;
14075 if (addend >= size)
14076 {
14077 /* Oops! We've got a reference past the defined end of
14078 the table. This is probably a bug -- shall we warn? */
14079 size = addend + file_align;
14080 }
14081 }
14082 size = (size + file_align - 1) & -file_align;
14083
14084 /* Allocate one extra entry for use as a "done" flag for the
14085 consolidation pass. */
14086 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14087
14088 if (ptr)
14089 {
a50b1753 14090 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14091
14092 if (ptr != NULL)
14093 {
14094 size_t oldbytes;
14095
cbd0eecf 14096 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14097 * sizeof (bfd_boolean));
14098 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14099 }
14100 }
14101 else
a50b1753 14102 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14103
14104 if (ptr == NULL)
14105 return FALSE;
14106
14107 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14108 h->u2.vtable->used = ptr + 1;
14109 h->u2.vtable->size = size;
c152c796
AM
14110 }
14111
cbd0eecf 14112 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14113
14114 return TRUE;
14115}
14116
ae17ab41
CM
14117/* Map an ELF section header flag to its corresponding string. */
14118typedef struct
14119{
14120 char *flag_name;
14121 flagword flag_value;
14122} elf_flags_to_name_table;
14123
14124static elf_flags_to_name_table elf_flags_to_names [] =
14125{
14126 { "SHF_WRITE", SHF_WRITE },
14127 { "SHF_ALLOC", SHF_ALLOC },
14128 { "SHF_EXECINSTR", SHF_EXECINSTR },
14129 { "SHF_MERGE", SHF_MERGE },
14130 { "SHF_STRINGS", SHF_STRINGS },
14131 { "SHF_INFO_LINK", SHF_INFO_LINK},
14132 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14133 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14134 { "SHF_GROUP", SHF_GROUP },
14135 { "SHF_TLS", SHF_TLS },
14136 { "SHF_MASKOS", SHF_MASKOS },
14137 { "SHF_EXCLUDE", SHF_EXCLUDE },
14138};
14139
b9c361e0
JL
14140/* Returns TRUE if the section is to be included, otherwise FALSE. */
14141bfd_boolean
ae17ab41 14142bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14143 struct flag_info *flaginfo,
b9c361e0 14144 asection *section)
ae17ab41 14145{
8b127cbc 14146 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14147
8b127cbc 14148 if (!flaginfo->flags_initialized)
ae17ab41 14149 {
8b127cbc
AM
14150 bfd *obfd = info->output_bfd;
14151 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14152 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14153 int with_hex = 0;
14154 int without_hex = 0;
14155
8b127cbc 14156 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14157 {
b9c361e0 14158 unsigned i;
8b127cbc 14159 flagword (*lookup) (char *);
ae17ab41 14160
8b127cbc
AM
14161 lookup = bed->elf_backend_lookup_section_flags_hook;
14162 if (lookup != NULL)
ae17ab41 14163 {
8b127cbc 14164 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14165
14166 if (hexval != 0)
14167 {
14168 if (tf->with == with_flags)
14169 with_hex |= hexval;
14170 else if (tf->with == without_flags)
14171 without_hex |= hexval;
14172 tf->valid = TRUE;
14173 continue;
14174 }
ae17ab41 14175 }
8b127cbc 14176 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14177 {
8b127cbc 14178 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14179 {
14180 if (tf->with == with_flags)
14181 with_hex |= elf_flags_to_names[i].flag_value;
14182 else if (tf->with == without_flags)
14183 without_hex |= elf_flags_to_names[i].flag_value;
14184 tf->valid = TRUE;
14185 break;
14186 }
14187 }
8b127cbc 14188 if (!tf->valid)
b9c361e0 14189 {
68ffbac6 14190 info->callbacks->einfo
9793eb77 14191 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14192 return FALSE;
ae17ab41
CM
14193 }
14194 }
8b127cbc
AM
14195 flaginfo->flags_initialized = TRUE;
14196 flaginfo->only_with_flags |= with_hex;
14197 flaginfo->not_with_flags |= without_hex;
ae17ab41 14198 }
ae17ab41 14199
8b127cbc 14200 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14201 return FALSE;
14202
8b127cbc 14203 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14204 return FALSE;
14205
14206 return TRUE;
ae17ab41
CM
14207}
14208
c152c796
AM
14209struct alloc_got_off_arg {
14210 bfd_vma gotoff;
10455f89 14211 struct bfd_link_info *info;
c152c796
AM
14212};
14213
14214/* We need a special top-level link routine to convert got reference counts
14215 to real got offsets. */
14216
14217static bfd_boolean
14218elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14219{
a50b1753 14220 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14221 bfd *obfd = gofarg->info->output_bfd;
14222 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14223
c152c796
AM
14224 if (h->got.refcount > 0)
14225 {
14226 h->got.offset = gofarg->gotoff;
10455f89 14227 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14228 }
14229 else
14230 h->got.offset = (bfd_vma) -1;
14231
14232 return TRUE;
14233}
14234
14235/* And an accompanying bit to work out final got entry offsets once
14236 we're done. Should be called from final_link. */
14237
14238bfd_boolean
14239bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14240 struct bfd_link_info *info)
14241{
14242 bfd *i;
14243 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14244 bfd_vma gotoff;
c152c796
AM
14245 struct alloc_got_off_arg gofarg;
14246
10455f89
HPN
14247 BFD_ASSERT (abfd == info->output_bfd);
14248
c152c796
AM
14249 if (! is_elf_hash_table (info->hash))
14250 return FALSE;
14251
14252 /* The GOT offset is relative to the .got section, but the GOT header is
14253 put into the .got.plt section, if the backend uses it. */
14254 if (bed->want_got_plt)
14255 gotoff = 0;
14256 else
14257 gotoff = bed->got_header_size;
14258
14259 /* Do the local .got entries first. */
c72f2fb2 14260 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14261 {
14262 bfd_signed_vma *local_got;
ef53be89 14263 size_t j, locsymcount;
c152c796
AM
14264 Elf_Internal_Shdr *symtab_hdr;
14265
14266 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14267 continue;
14268
14269 local_got = elf_local_got_refcounts (i);
14270 if (!local_got)
14271 continue;
14272
14273 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14274 if (elf_bad_symtab (i))
14275 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14276 else
14277 locsymcount = symtab_hdr->sh_info;
14278
14279 for (j = 0; j < locsymcount; ++j)
14280 {
14281 if (local_got[j] > 0)
14282 {
14283 local_got[j] = gotoff;
10455f89 14284 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14285 }
14286 else
14287 local_got[j] = (bfd_vma) -1;
14288 }
14289 }
14290
14291 /* Then the global .got entries. .plt refcounts are handled by
14292 adjust_dynamic_symbol */
14293 gofarg.gotoff = gotoff;
10455f89 14294 gofarg.info = info;
c152c796
AM
14295 elf_link_hash_traverse (elf_hash_table (info),
14296 elf_gc_allocate_got_offsets,
14297 &gofarg);
14298 return TRUE;
14299}
14300
14301/* Many folk need no more in the way of final link than this, once
14302 got entry reference counting is enabled. */
14303
14304bfd_boolean
14305bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14306{
14307 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14308 return FALSE;
14309
14310 /* Invoke the regular ELF backend linker to do all the work. */
14311 return bfd_elf_final_link (abfd, info);
14312}
14313
14314bfd_boolean
14315bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14316{
a50b1753 14317 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14318
14319 if (rcookie->bad_symtab)
14320 rcookie->rel = rcookie->rels;
14321
14322 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14323 {
14324 unsigned long r_symndx;
14325
14326 if (! rcookie->bad_symtab)
14327 if (rcookie->rel->r_offset > offset)
14328 return FALSE;
14329 if (rcookie->rel->r_offset != offset)
14330 continue;
14331
14332 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14333 if (r_symndx == STN_UNDEF)
c152c796
AM
14334 return TRUE;
14335
14336 if (r_symndx >= rcookie->locsymcount
14337 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14338 {
14339 struct elf_link_hash_entry *h;
14340
14341 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14342
14343 while (h->root.type == bfd_link_hash_indirect
14344 || h->root.type == bfd_link_hash_warning)
14345 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14346
14347 if ((h->root.type == bfd_link_hash_defined
14348 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14349 && (h->root.u.def.section->owner != rcookie->abfd
14350 || h->root.u.def.section->kept_section != NULL
14351 || discarded_section (h->root.u.def.section)))
c152c796 14352 return TRUE;
c152c796
AM
14353 }
14354 else
14355 {
14356 /* It's not a relocation against a global symbol,
14357 but it could be a relocation against a local
14358 symbol for a discarded section. */
14359 asection *isec;
14360 Elf_Internal_Sym *isym;
14361
14362 /* Need to: get the symbol; get the section. */
14363 isym = &rcookie->locsyms[r_symndx];
cb33740c 14364 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14365 if (isec != NULL
14366 && (isec->kept_section != NULL
14367 || discarded_section (isec)))
cb33740c 14368 return TRUE;
c152c796
AM
14369 }
14370 return FALSE;
14371 }
14372 return FALSE;
14373}
14374
14375/* Discard unneeded references to discarded sections.
75938853
AM
14376 Returns -1 on error, 1 if any section's size was changed, 0 if
14377 nothing changed. This function assumes that the relocations are in
14378 sorted order, which is true for all known assemblers. */
c152c796 14379
75938853 14380int
c152c796
AM
14381bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14382{
14383 struct elf_reloc_cookie cookie;
18cd5bce 14384 asection *o;
c152c796 14385 bfd *abfd;
75938853 14386 int changed = 0;
c152c796
AM
14387
14388 if (info->traditional_format
14389 || !is_elf_hash_table (info->hash))
75938853 14390 return 0;
c152c796 14391
18cd5bce
AM
14392 o = bfd_get_section_by_name (output_bfd, ".stab");
14393 if (o != NULL)
c152c796 14394 {
18cd5bce 14395 asection *i;
c152c796 14396
18cd5bce 14397 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14398 {
18cd5bce
AM
14399 if (i->size == 0
14400 || i->reloc_count == 0
14401 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14402 continue;
c152c796 14403
18cd5bce
AM
14404 abfd = i->owner;
14405 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14406 continue;
c152c796 14407
18cd5bce 14408 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14409 return -1;
c152c796 14410
18cd5bce
AM
14411 if (_bfd_discard_section_stabs (abfd, i,
14412 elf_section_data (i)->sec_info,
5241d853
RS
14413 bfd_elf_reloc_symbol_deleted_p,
14414 &cookie))
75938853 14415 changed = 1;
18cd5bce
AM
14416
14417 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14418 }
18cd5bce
AM
14419 }
14420
2f0c68f2
CM
14421 o = NULL;
14422 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14423 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14424 if (o != NULL)
14425 {
14426 asection *i;
d7153c4a 14427 int eh_changed = 0;
66631823 14428 unsigned int eh_alignment; /* Octets. */
c152c796 14429
18cd5bce 14430 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14431 {
18cd5bce
AM
14432 if (i->size == 0)
14433 continue;
14434
14435 abfd = i->owner;
14436 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14437 continue;
14438
14439 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14440 return -1;
18cd5bce
AM
14441
14442 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14443 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14444 bfd_elf_reloc_symbol_deleted_p,
14445 &cookie))
d7153c4a
AM
14446 {
14447 eh_changed = 1;
14448 if (i->size != i->rawsize)
14449 changed = 1;
14450 }
18cd5bce
AM
14451
14452 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14453 }
9866ffe2 14454
66631823
CE
14455 eh_alignment = ((1 << o->alignment_power)
14456 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14457 /* Skip over zero terminator, and prevent empty sections from
14458 adding alignment padding at the end. */
14459 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14460 if (i->size == 0)
14461 i->flags |= SEC_EXCLUDE;
14462 else if (i->size > 4)
14463 break;
14464 /* The last non-empty eh_frame section doesn't need padding. */
14465 if (i != NULL)
14466 i = i->map_tail.s;
14467 /* Any prior sections must pad the last FDE out to the output
14468 section alignment. Otherwise we might have zero padding
14469 between sections, which would be seen as a terminator. */
14470 for (; i != NULL; i = i->map_tail.s)
14471 if (i->size == 4)
14472 /* All but the last zero terminator should have been removed. */
14473 BFD_FAIL ();
14474 else
14475 {
14476 bfd_size_type size
14477 = (i->size + eh_alignment - 1) & -eh_alignment;
14478 if (i->size != size)
af471f82 14479 {
9866ffe2
AM
14480 i->size = size;
14481 changed = 1;
14482 eh_changed = 1;
af471f82 14483 }
9866ffe2 14484 }
d7153c4a
AM
14485 if (eh_changed)
14486 elf_link_hash_traverse (elf_hash_table (info),
14487 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14488 }
c152c796 14489
18cd5bce
AM
14490 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14491 {
14492 const struct elf_backend_data *bed;
57963c05 14493 asection *s;
c152c796 14494
18cd5bce
AM
14495 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14496 continue;
57963c05
AM
14497 s = abfd->sections;
14498 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14499 continue;
18cd5bce
AM
14500
14501 bed = get_elf_backend_data (abfd);
14502
14503 if (bed->elf_backend_discard_info != NULL)
14504 {
14505 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14506 return -1;
18cd5bce
AM
14507
14508 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14509 changed = 1;
18cd5bce
AM
14510
14511 fini_reloc_cookie (&cookie, abfd);
14512 }
c152c796
AM
14513 }
14514
2f0c68f2
CM
14515 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14516 _bfd_elf_end_eh_frame_parsing (info);
14517
14518 if (info->eh_frame_hdr_type
0e1862bb 14519 && !bfd_link_relocatable (info)
c152c796 14520 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14521 changed = 1;
c152c796 14522
75938853 14523 return changed;
c152c796 14524}
082b7297 14525
43e1669b 14526bfd_boolean
0c511000 14527_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14528 asection *sec,
c0f00686 14529 struct bfd_link_info *info)
082b7297
L
14530{
14531 flagword flags;
c77ec726 14532 const char *name, *key;
082b7297
L
14533 struct bfd_section_already_linked *l;
14534 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14535
c77ec726
AM
14536 if (sec->output_section == bfd_abs_section_ptr)
14537 return FALSE;
0c511000 14538
c77ec726 14539 flags = sec->flags;
0c511000 14540
c77ec726
AM
14541 /* Return if it isn't a linkonce section. A comdat group section
14542 also has SEC_LINK_ONCE set. */
14543 if ((flags & SEC_LINK_ONCE) == 0)
14544 return FALSE;
0c511000 14545
c77ec726
AM
14546 /* Don't put group member sections on our list of already linked
14547 sections. They are handled as a group via their group section. */
14548 if (elf_sec_group (sec) != NULL)
14549 return FALSE;
0c511000 14550
c77ec726
AM
14551 /* For a SHT_GROUP section, use the group signature as the key. */
14552 name = sec->name;
14553 if ((flags & SEC_GROUP) != 0
14554 && elf_next_in_group (sec) != NULL
14555 && elf_group_name (elf_next_in_group (sec)) != NULL)
14556 key = elf_group_name (elf_next_in_group (sec));
14557 else
14558 {
14559 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14560 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14561 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14562 key++;
0c511000 14563 else
c77ec726
AM
14564 /* Must be a user linkonce section that doesn't follow gcc's
14565 naming convention. In this case we won't be matching
14566 single member groups. */
14567 key = name;
0c511000 14568 }
6d2cd210 14569
c77ec726 14570 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14571
14572 for (l = already_linked_list->entry; l != NULL; l = l->next)
14573 {
c2370991 14574 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14575 sections with a signature of <key> (<key> is some string),
14576 and linkonce sections named .gnu.linkonce.<type>.<key>.
14577 Match like sections. LTO plugin sections are an exception.
14578 They are always named .gnu.linkonce.t.<key> and match either
14579 type of section. */
14580 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14581 && ((flags & SEC_GROUP) != 0
14582 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14583 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14584 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14585 {
14586 /* The section has already been linked. See if we should
6d2cd210 14587 issue a warning. */
c77ec726
AM
14588 if (!_bfd_handle_already_linked (sec, l, info))
14589 return FALSE;
082b7297 14590
c77ec726 14591 if (flags & SEC_GROUP)
3d7f7666 14592 {
c77ec726
AM
14593 asection *first = elf_next_in_group (sec);
14594 asection *s = first;
3d7f7666 14595
c77ec726 14596 while (s != NULL)
3d7f7666 14597 {
c77ec726
AM
14598 s->output_section = bfd_abs_section_ptr;
14599 /* Record which group discards it. */
14600 s->kept_section = l->sec;
14601 s = elf_next_in_group (s);
14602 /* These lists are circular. */
14603 if (s == first)
14604 break;
3d7f7666
L
14605 }
14606 }
082b7297 14607
43e1669b 14608 return TRUE;
082b7297
L
14609 }
14610 }
14611
c77ec726
AM
14612 /* A single member comdat group section may be discarded by a
14613 linkonce section and vice versa. */
14614 if ((flags & SEC_GROUP) != 0)
3d7f7666 14615 {
c77ec726 14616 asection *first = elf_next_in_group (sec);
c2370991 14617
c77ec726
AM
14618 if (first != NULL && elf_next_in_group (first) == first)
14619 /* Check this single member group against linkonce sections. */
14620 for (l = already_linked_list->entry; l != NULL; l = l->next)
14621 if ((l->sec->flags & SEC_GROUP) == 0
14622 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14623 {
14624 first->output_section = bfd_abs_section_ptr;
14625 first->kept_section = l->sec;
14626 sec->output_section = bfd_abs_section_ptr;
14627 break;
14628 }
14629 }
14630 else
14631 /* Check this linkonce section against single member groups. */
14632 for (l = already_linked_list->entry; l != NULL; l = l->next)
14633 if (l->sec->flags & SEC_GROUP)
6d2cd210 14634 {
c77ec726 14635 asection *first = elf_next_in_group (l->sec);
6d2cd210 14636
c77ec726
AM
14637 if (first != NULL
14638 && elf_next_in_group (first) == first
14639 && bfd_elf_match_symbols_in_sections (first, sec, info))
14640 {
14641 sec->output_section = bfd_abs_section_ptr;
14642 sec->kept_section = first;
14643 break;
14644 }
6d2cd210 14645 }
0c511000 14646
c77ec726
AM
14647 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14648 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14649 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14650 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14651 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14652 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14653 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14654 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14655 The reverse order cannot happen as there is never a bfd with only the
14656 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14657 matter as here were are looking only for cross-bfd sections. */
14658
14659 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14660 for (l = already_linked_list->entry; l != NULL; l = l->next)
14661 if ((l->sec->flags & SEC_GROUP) == 0
14662 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14663 {
14664 if (abfd != l->sec->owner)
14665 sec->output_section = bfd_abs_section_ptr;
14666 break;
14667 }
80c29487 14668
082b7297 14669 /* This is the first section with this name. Record it. */
c77ec726 14670 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14671 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14672 return sec->output_section == bfd_abs_section_ptr;
082b7297 14673}
81e1b023 14674
a4d8e49b
L
14675bfd_boolean
14676_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14677{
14678 return sym->st_shndx == SHN_COMMON;
14679}
14680
14681unsigned int
14682_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14683{
14684 return SHN_COMMON;
14685}
14686
14687asection *
14688_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14689{
14690 return bfd_com_section_ptr;
14691}
10455f89
HPN
14692
14693bfd_vma
14694_bfd_elf_default_got_elt_size (bfd *abfd,
14695 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14696 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14697 bfd *ibfd ATTRIBUTE_UNUSED,
14698 unsigned long symndx ATTRIBUTE_UNUSED)
14699{
14700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14701 return bed->s->arch_size / 8;
14702}
83bac4b0
NC
14703
14704/* Routines to support the creation of dynamic relocs. */
14705
83bac4b0
NC
14706/* Returns the name of the dynamic reloc section associated with SEC. */
14707
14708static const char *
14709get_dynamic_reloc_section_name (bfd * abfd,
14710 asection * sec,
14711 bfd_boolean is_rela)
14712{
ddcf1fcf 14713 char *name;
fd361982 14714 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14715 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14716
ddcf1fcf 14717 if (old_name == NULL)
83bac4b0
NC
14718 return NULL;
14719
ddcf1fcf 14720 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14721 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14722
14723 return name;
14724}
14725
14726/* Returns the dynamic reloc section associated with SEC.
14727 If necessary compute the name of the dynamic reloc section based
14728 on SEC's name (looked up in ABFD's string table) and the setting
14729 of IS_RELA. */
14730
14731asection *
14732_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14733 asection * sec,
14734 bfd_boolean is_rela)
14735{
14736 asection * reloc_sec = elf_section_data (sec)->sreloc;
14737
14738 if (reloc_sec == NULL)
14739 {
14740 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14741
14742 if (name != NULL)
14743 {
3d4d4302 14744 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14745
14746 if (reloc_sec != NULL)
14747 elf_section_data (sec)->sreloc = reloc_sec;
14748 }
14749 }
14750
14751 return reloc_sec;
14752}
14753
14754/* Returns the dynamic reloc section associated with SEC. If the
14755 section does not exist it is created and attached to the DYNOBJ
14756 bfd and stored in the SRELOC field of SEC's elf_section_data
14757 structure.
f8076f98 14758
83bac4b0
NC
14759 ALIGNMENT is the alignment for the newly created section and
14760 IS_RELA defines whether the name should be .rela.<SEC's name>
14761 or .rel.<SEC's name>. The section name is looked up in the
14762 string table associated with ABFD. */
14763
14764asection *
ca4be51c
AM
14765_bfd_elf_make_dynamic_reloc_section (asection *sec,
14766 bfd *dynobj,
14767 unsigned int alignment,
14768 bfd *abfd,
14769 bfd_boolean is_rela)
83bac4b0
NC
14770{
14771 asection * reloc_sec = elf_section_data (sec)->sreloc;
14772
14773 if (reloc_sec == NULL)
14774 {
14775 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14776
14777 if (name == NULL)
14778 return NULL;
14779
3d4d4302 14780 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14781
14782 if (reloc_sec == NULL)
14783 {
3d4d4302
AM
14784 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14785 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14786 if ((sec->flags & SEC_ALLOC) != 0)
14787 flags |= SEC_ALLOC | SEC_LOAD;
14788
3d4d4302 14789 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14790 if (reloc_sec != NULL)
14791 {
8877b5e5
AM
14792 /* _bfd_elf_get_sec_type_attr chooses a section type by
14793 name. Override as it may be wrong, eg. for a user
14794 section named "auto" we'll get ".relauto" which is
14795 seen to be a .rela section. */
14796 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14797 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14798 reloc_sec = NULL;
14799 }
14800 }
14801
14802 elf_section_data (sec)->sreloc = reloc_sec;
14803 }
14804
14805 return reloc_sec;
14806}
1338dd10 14807
bffebb6b
AM
14808/* Copy the ELF symbol type and other attributes for a linker script
14809 assignment from HSRC to HDEST. Generally this should be treated as
14810 if we found a strong non-dynamic definition for HDEST (except that
14811 ld ignores multiple definition errors). */
1338dd10 14812void
bffebb6b
AM
14813_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14814 struct bfd_link_hash_entry *hdest,
14815 struct bfd_link_hash_entry *hsrc)
1338dd10 14816{
bffebb6b
AM
14817 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14818 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14819 Elf_Internal_Sym isym;
1338dd10
PB
14820
14821 ehdest->type = ehsrc->type;
35fc36a8 14822 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14823
14824 isym.st_other = ehsrc->other;
b8417128 14825 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14826}
351f65ca
L
14827
14828/* Append a RELA relocation REL to section S in BFD. */
14829
14830void
14831elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14832{
14833 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14834 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14835 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14836 bed->s->swap_reloca_out (abfd, rel, loc);
14837}
14838
14839/* Append a REL relocation REL to section S in BFD. */
14840
14841void
14842elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14843{
14844 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14845 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14846 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14847 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14848}
7dba9362
AM
14849
14850/* Define __start, __stop, .startof. or .sizeof. symbol. */
14851
14852struct bfd_link_hash_entry *
14853bfd_elf_define_start_stop (struct bfd_link_info *info,
14854 const char *symbol, asection *sec)
14855{
487b6440 14856 struct elf_link_hash_entry *h;
7dba9362 14857
487b6440
AM
14858 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14859 FALSE, FALSE, TRUE);
e1b5d517 14860 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
14861 if (h != NULL
14862 && (h->root.type == bfd_link_hash_undefined
14863 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
14864 || ((h->ref_regular || h->def_dynamic)
14865 && !h->def_regular
14866 && h->root.type != bfd_link_hash_common)))
7dba9362 14867 {
bf3077a6 14868 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 14869 h->verinfo.verdef = NULL;
487b6440
AM
14870 h->root.type = bfd_link_hash_defined;
14871 h->root.u.def.section = sec;
14872 h->root.u.def.value = 0;
14873 h->def_regular = 1;
14874 h->def_dynamic = 0;
14875 h->start_stop = 1;
14876 h->u2.start_stop_section = sec;
14877 if (symbol[0] == '.')
14878 {
14879 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14880 const struct elf_backend_data *bed;
14881 bed = get_elf_backend_data (info->output_bfd);
14882 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14883 }
36b8fda5
AM
14884 else
14885 {
14886 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
14887 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
14888 | info->start_stop_visibility);
bf3077a6 14889 if (was_dynamic)
36b8fda5
AM
14890 bfd_elf_link_record_dynamic_symbol (info, h);
14891 }
487b6440 14892 return &h->root;
7dba9362 14893 }
487b6440 14894 return NULL;
7dba9362 14895}
5dbc8b37
L
14896
14897/* Find dynamic relocs for H that apply to read-only sections. */
14898
14899asection *
14900_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
14901{
14902 struct elf_dyn_relocs *p;
14903
14904 for (p = h->dyn_relocs; p != NULL; p = p->next)
14905 {
14906 asection *s = p->sec->output_section;
14907
14908 if (s != NULL && (s->flags & SEC_READONLY) != 0)
14909 return p->sec;
14910 }
14911 return NULL;
14912}
d49e5065
L
14913
14914/* Set DF_TEXTREL if we find any dynamic relocs that apply to
14915 read-only sections. */
14916
14917bfd_boolean
14918_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
14919{
14920 asection *sec;
14921
14922 if (h->root.type == bfd_link_hash_indirect)
14923 return TRUE;
14924
14925 sec = _bfd_elf_readonly_dynrelocs (h);
14926 if (sec != NULL)
14927 {
14928 struct bfd_link_info *info = (struct bfd_link_info *) inf;
14929
14930 info->flags |= DF_TEXTREL;
14931 /* xgettext:c-format */
14932 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
14933 "in read-only section `%pA'\n"),
14934 sec->owner, h->root.root.string, sec);
14935
14936 if (bfd_link_textrel_check (info))
14937 /* xgettext:c-format */
14938 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
14939 "in read-only section `%pA'\n"),
14940 sec->owner, h->root.root.string, sec);
14941
14942 /* Not an error, just cut short the traversal. */
14943 return FALSE;
14944 }
14945 return TRUE;
14946}
3084d7a2
L
14947
14948/* Add dynamic tags. */
14949
14950bfd_boolean
14951_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
14952 bfd_boolean need_dynamic_reloc)
14953{
14954 struct elf_link_hash_table *htab = elf_hash_table (info);
14955
14956 if (htab->dynamic_sections_created)
14957 {
14958 /* Add some entries to the .dynamic section. We fill in the
14959 values later, in finish_dynamic_sections, but we must add
14960 the entries now so that we get the correct size for the
14961 .dynamic section. The DT_DEBUG entry is filled in by the
14962 dynamic linker and used by the debugger. */
14963#define add_dynamic_entry(TAG, VAL) \
14964 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
14965
14966 const struct elf_backend_data *bed
14967 = get_elf_backend_data (output_bfd);
14968
14969 if (bfd_link_executable (info))
14970 {
14971 if (!add_dynamic_entry (DT_DEBUG, 0))
14972 return FALSE;
14973 }
14974
14975 if (htab->dt_pltgot_required || htab->splt->size != 0)
14976 {
14977 /* DT_PLTGOT is used by prelink even if there is no PLT
14978 relocation. */
14979 if (!add_dynamic_entry (DT_PLTGOT, 0))
14980 return FALSE;
14981 }
14982
14983 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
14984 {
14985 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
14986 || !add_dynamic_entry (DT_PLTREL,
14987 (bed->rela_plts_and_copies_p
14988 ? DT_RELA : DT_REL))
14989 || !add_dynamic_entry (DT_JMPREL, 0))
14990 return FALSE;
14991 }
14992
14993 if (htab->tlsdesc_plt
14994 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
14995 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
14996 return FALSE;
14997
14998 if (need_dynamic_reloc)
14999 {
15000 if (bed->rela_plts_and_copies_p)
15001 {
15002 if (!add_dynamic_entry (DT_RELA, 0)
15003 || !add_dynamic_entry (DT_RELASZ, 0)
15004 || !add_dynamic_entry (DT_RELAENT,
15005 bed->s->sizeof_rela))
15006 return FALSE;
15007 }
15008 else
15009 {
15010 if (!add_dynamic_entry (DT_REL, 0)
15011 || !add_dynamic_entry (DT_RELSZ, 0)
15012 || !add_dynamic_entry (DT_RELENT,
15013 bed->s->sizeof_rel))
15014 return FALSE;
15015 }
15016
15017 /* If any dynamic relocs apply to a read-only section,
15018 then we need a DT_TEXTREL entry. */
15019 if ((info->flags & DF_TEXTREL) == 0)
15020 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15021 info);
15022
15023 if ((info->flags & DF_TEXTREL) != 0)
15024 {
15025 if (htab->ifunc_resolvers)
15026 info->callbacks->einfo
15027 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15028 "may result in a segfault at runtime; recompile with %s\n"),
15029 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15030
15031 if (!add_dynamic_entry (DT_TEXTREL, 0))
15032 return FALSE;
15033 }
15034 }
15035 }
15036#undef add_dynamic_entry
15037
15038 return TRUE;
15039}
This page took 2.928467 seconds and 4 git commands to generate.