Generate aclocal-m4-deps.mk more deterministically and portably.
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
219d1afa 2 Copyright (C) 1995-2018 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"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
07d6d2b8 80 return h->root.u.def.section;
2f0c68f2
CM
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
57963c05 221 asection *s;
6cd255ca 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 223 if ((ibfd->flags
57963c05
AM
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
48e30f52
L
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
b531344c
MR
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
45d6a902 698
f5385ebf 699 h->def_regular = 1;
45d6a902 700
eb8476a6 701 if (hidden)
fe21a8fc 702 {
91d6fa6a 703 bed = get_elf_backend_data (output_bfd);
b8297068
AM
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
6fa3860b
PB
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
0e1862bb 711 if (!bfd_link_relocatable (info)
6fa3860b
PB
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
f5385ebf
AM
717 if ((h->def_dynamic
718 || h->ref_dynamic
6b3b0ab8
L
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 721 && !h->forced_local
45d6a902
AM
722 && h->dynindx == -1)
723 {
c152c796 724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
60d67dc8 730 if (h->is_weakalias)
45d6a902 731 {
60d67dc8
AM
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741}
42751cf3 742
8c58d23b
AM
743/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747int
c152c796
AM
748bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
8c58d23b
AM
751{
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
ef53be89 756 size_t dynstr_index;
8c58d23b
AM
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
0eddce27 761 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
a50b1753 770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 776 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 783 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
b34976b6 810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 811 if (dynstr_index == (size_t) -1)
8c58d23b
AM
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830}
831
30b30c21 832/* Return the dynindex of a local dynamic symbol. */
42751cf3 833
30b30c21 834long
268b6b39
AM
835_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
30b30c21
RH
838{
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845}
846
847/* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
b34976b6 851static bfd_boolean
268b6b39
AM
852elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
42751cf3 854{
a50b1753 855 size_t *count = (size_t *) data;
30b30c21 856
6fa3860b
PB
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864}
865
866
867/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870static bfd_boolean
871elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873{
a50b1753 874 size_t *count = (size_t *) data;
6fa3860b 875
6fa3860b
PB
876 if (!h->forced_local)
877 return TRUE;
878
42751cf3 879 if (h->dynindx != -1)
30b30c21
RH
880 h->dynindx = ++(*count);
881
b34976b6 882 return TRUE;
42751cf3 883}
30b30c21 884
aee6f5b4
AO
885/* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887bfd_boolean
d00dd7dc
AM
888_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
aee6f5b4 891{
74541ad4 892 struct elf_link_hash_table *htab;
ca55926c 893 asection *ip;
74541ad4 894
aee6f5b4
AO
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
74541ad4
AM
902 htab = elf_hash_table (info);
903 if (p == htab->tls_sec)
904 return FALSE;
905
906 if (htab->text_index_section != NULL)
907 return p != htab->text_index_section && p != htab->data_index_section;
908
ca55926c 909 return (htab->dynobj != NULL
3d4d4302 910 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 911 && ip->output_section == p);
aee6f5b4
AO
912
913 /* There shouldn't be section relative relocations
914 against any other section. */
915 default:
916 return TRUE;
917 }
918}
919
d00dd7dc
AM
920bfd_boolean
921_bfd_elf_omit_section_dynsym_all
922 (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
924 asection *p ATTRIBUTE_UNUSED)
925{
926 return TRUE;
927}
928
062e2358 929/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
930 symbol for each output section, which come first. Next come symbols
931 which have been forced to local binding. Then all of the back-end
932 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
933 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
934 (This prevents the early call before elf_backend_init_index_section
935 and strip_excluded_output_sections setting dynindx for sections
936 that are stripped.) */
30b30c21 937
554220db
AM
938static unsigned long
939_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
940 struct bfd_link_info *info,
941 unsigned long *section_sym_count)
30b30c21
RH
942{
943 unsigned long dynsymcount = 0;
63f452a8 944 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 945
0e1862bb
L
946 if (bfd_link_pic (info)
947 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 948 {
aee6f5b4 949 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
950 asection *p;
951 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 952 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 953 && (p->flags & SEC_ALLOC) != 0
7f923b7f 954 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 955 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
956 {
957 ++dynsymcount;
958 if (do_sec)
959 elf_section_data (p)->dynindx = dynsymcount;
960 }
961 else if (do_sec)
74541ad4 962 elf_section_data (p)->dynindx = 0;
30b30c21 963 }
63f452a8
AM
964 if (do_sec)
965 *section_sym_count = dynsymcount;
30b30c21 966
6fa3860b
PB
967 elf_link_hash_traverse (elf_hash_table (info),
968 elf_link_renumber_local_hash_table_dynsyms,
969 &dynsymcount);
970
30b30c21
RH
971 if (elf_hash_table (info)->dynlocal)
972 {
973 struct elf_link_local_dynamic_entry *p;
974 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
975 p->dynindx = ++dynsymcount;
976 }
90ac2420 977 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_hash_table_dynsyms,
981 &dynsymcount);
982
d5486c43
L
983 /* There is an unused NULL entry at the head of the table which we
984 must account for in our count even if the table is empty since it
985 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
986 .dynamic section. */
987 dynsymcount++;
30b30c21 988
ccabcbe5
AM
989 elf_hash_table (info)->dynsymcount = dynsymcount;
990 return dynsymcount;
30b30c21 991}
252b5132 992
54ac0771
L
993/* Merge st_other field. */
994
995static void
996elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 997 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 998 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
999{
1000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1001
1002 /* If st_other has a processor-specific meaning, specific
cd3416da 1003 code might be needed here. */
54ac0771
L
1004 if (bed->elf_backend_merge_symbol_attribute)
1005 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1006 dynamic);
1007
cd3416da 1008 if (!dynamic)
54ac0771 1009 {
cd3416da
AM
1010 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1011 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1012
cd3416da
AM
1013 /* Keep the most constraining visibility. Leave the remainder
1014 of the st_other field to elf_backend_merge_symbol_attribute. */
1015 if (symvis - 1 < hvis - 1)
1016 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1017 }
b8417128
AM
1018 else if (definition
1019 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1020 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1021 h->protected_def = 1;
54ac0771
L
1022}
1023
4f3fedcf
AM
1024/* This function is called when we want to merge a new symbol with an
1025 existing symbol. It handles the various cases which arise when we
1026 find a definition in a dynamic object, or when there is already a
1027 definition in a dynamic object. The new symbol is described by
1028 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1029 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1030 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1031 of an old common symbol. We set OVERRIDE if the old symbol is
1032 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1033 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1034 to change. By OK to change, we mean that we shouldn't warn if the
1035 type or size does change. */
45d6a902 1036
8a56bd02 1037static bfd_boolean
268b6b39
AM
1038_bfd_elf_merge_symbol (bfd *abfd,
1039 struct bfd_link_info *info,
1040 const char *name,
1041 Elf_Internal_Sym *sym,
1042 asection **psec,
1043 bfd_vma *pvalue,
4f3fedcf
AM
1044 struct elf_link_hash_entry **sym_hash,
1045 bfd **poldbfd,
37a9e49a 1046 bfd_boolean *pold_weak,
af44c138 1047 unsigned int *pold_alignment,
268b6b39
AM
1048 bfd_boolean *skip,
1049 bfd_boolean *override,
1050 bfd_boolean *type_change_ok,
6e33951e
L
1051 bfd_boolean *size_change_ok,
1052 bfd_boolean *matched)
252b5132 1053{
7479dfd4 1054 asection *sec, *oldsec;
45d6a902 1055 struct elf_link_hash_entry *h;
90c984fc 1056 struct elf_link_hash_entry *hi;
45d6a902
AM
1057 struct elf_link_hash_entry *flip;
1058 int bind;
1059 bfd *oldbfd;
1060 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1061 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1062 const struct elf_backend_data *bed;
6e33951e 1063 char *new_version;
93f4de39 1064 bfd_boolean default_sym = *matched;
45d6a902
AM
1065
1066 *skip = FALSE;
1067 *override = FALSE;
1068
1069 sec = *psec;
1070 bind = ELF_ST_BIND (sym->st_info);
1071
1072 if (! bfd_is_und_section (sec))
1073 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1074 else
1075 h = ((struct elf_link_hash_entry *)
1076 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1077 if (h == NULL)
1078 return FALSE;
1079 *sym_hash = h;
252b5132 1080
88ba32a0
L
1081 bed = get_elf_backend_data (abfd);
1082
6e33951e 1083 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1084 if (h->versioned != unversioned)
6e33951e 1085 {
422f1182
L
1086 /* Symbol version is unknown or versioned. */
1087 new_version = strrchr (name, ELF_VER_CHR);
1088 if (new_version)
1089 {
1090 if (h->versioned == unknown)
1091 {
1092 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1093 h->versioned = versioned_hidden;
1094 else
1095 h->versioned = versioned;
1096 }
1097 new_version += 1;
1098 if (new_version[0] == '\0')
1099 new_version = NULL;
1100 }
1101 else
1102 h->versioned = unversioned;
6e33951e 1103 }
422f1182
L
1104 else
1105 new_version = NULL;
6e33951e 1106
90c984fc
L
1107 /* For merging, we only care about real symbols. But we need to make
1108 sure that indirect symbol dynamic flags are updated. */
1109 hi = h;
45d6a902
AM
1110 while (h->root.type == bfd_link_hash_indirect
1111 || h->root.type == bfd_link_hash_warning)
1112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1113
6e33951e
L
1114 if (!*matched)
1115 {
1116 if (hi == h || h->root.type == bfd_link_hash_new)
1117 *matched = TRUE;
1118 else
1119 {
ae7683d2 1120 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1121 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1122 true if the new symbol is only visible to the symbol with
6e33951e 1123 the same symbol version. */
422f1182
L
1124 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1125 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1126 if (!old_hidden && !new_hidden)
1127 /* The new symbol matches the existing symbol if both
1128 aren't hidden. */
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_VERSION is the symbol version of the existing
1133 symbol. */
422f1182
L
1134 char *old_version;
1135
1136 if (h->versioned >= versioned)
1137 old_version = strrchr (h->root.root.string,
1138 ELF_VER_CHR) + 1;
1139 else
1140 old_version = NULL;
6e33951e
L
1141
1142 /* The new symbol matches the existing symbol if they
1143 have the same symbol version. */
1144 *matched = (old_version == new_version
1145 || (old_version != NULL
1146 && new_version != NULL
1147 && strcmp (old_version, new_version) == 0));
1148 }
1149 }
1150 }
1151
934bce08
AM
1152 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1153 existing symbol. */
1154
1155 oldbfd = NULL;
1156 oldsec = NULL;
1157 switch (h->root.type)
1158 {
1159 default:
1160 break;
1161
1162 case bfd_link_hash_undefined:
1163 case bfd_link_hash_undefweak:
1164 oldbfd = h->root.u.undef.abfd;
1165 break;
1166
1167 case bfd_link_hash_defined:
1168 case bfd_link_hash_defweak:
1169 oldbfd = h->root.u.def.section->owner;
1170 oldsec = h->root.u.def.section;
1171 break;
1172
1173 case bfd_link_hash_common:
1174 oldbfd = h->root.u.c.p->section->owner;
1175 oldsec = h->root.u.c.p->section;
1176 if (pold_alignment)
1177 *pold_alignment = h->root.u.c.p->alignment_power;
1178 break;
1179 }
1180 if (poldbfd && *poldbfd == NULL)
1181 *poldbfd = oldbfd;
1182
1183 /* Differentiate strong and weak symbols. */
1184 newweak = bind == STB_WEAK;
1185 oldweak = (h->root.type == bfd_link_hash_defweak
1186 || h->root.type == bfd_link_hash_undefweak);
1187 if (pold_weak)
1188 *pold_weak = oldweak;
1189
40b36307 1190 /* We have to check it for every instance since the first few may be
ee659f1f 1191 references and not all compilers emit symbol type for undefined
40b36307
L
1192 symbols. */
1193 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1194
ee659f1f
AM
1195 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1196 respectively, is from a dynamic object. */
1197
1198 newdyn = (abfd->flags & DYNAMIC) != 0;
1199
1200 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1201 syms and defined syms in dynamic libraries respectively.
1202 ref_dynamic on the other hand can be set for a symbol defined in
1203 a dynamic library, and def_dynamic may not be set; When the
1204 definition in a dynamic lib is overridden by a definition in the
1205 executable use of the symbol in the dynamic lib becomes a
1206 reference to the executable symbol. */
1207 if (newdyn)
1208 {
1209 if (bfd_is_und_section (sec))
1210 {
1211 if (bind != STB_WEAK)
1212 {
1213 h->ref_dynamic_nonweak = 1;
1214 hi->ref_dynamic_nonweak = 1;
1215 }
1216 }
1217 else
1218 {
6e33951e
L
1219 /* Update the existing symbol only if they match. */
1220 if (*matched)
1221 h->dynamic_def = 1;
ee659f1f
AM
1222 hi->dynamic_def = 1;
1223 }
1224 }
1225
45d6a902
AM
1226 /* If we just created the symbol, mark it as being an ELF symbol.
1227 Other than that, there is nothing to do--there is no merge issue
1228 with a newly defined symbol--so we just return. */
1229
1230 if (h->root.type == bfd_link_hash_new)
252b5132 1231 {
f5385ebf 1232 h->non_elf = 0;
45d6a902
AM
1233 return TRUE;
1234 }
252b5132 1235
45d6a902
AM
1236 /* In cases involving weak versioned symbols, we may wind up trying
1237 to merge a symbol with itself. Catch that here, to avoid the
1238 confusion that results if we try to override a symbol with
1239 itself. The additional tests catch cases like
1240 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1241 dynamic object, which we do want to handle here. */
1242 if (abfd == oldbfd
895fa45f 1243 && (newweak || oldweak)
45d6a902 1244 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1245 || !h->def_regular))
45d6a902
AM
1246 return TRUE;
1247
707bba77 1248 olddyn = FALSE;
45d6a902
AM
1249 if (oldbfd != NULL)
1250 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1251 else if (oldsec != NULL)
45d6a902 1252 {
707bba77 1253 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1254 indices used by MIPS ELF. */
707bba77 1255 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1256 }
252b5132 1257
1a3b5c34
AM
1258 /* Handle a case where plugin_notice won't be called and thus won't
1259 set the non_ir_ref flags on the first pass over symbols. */
1260 if (oldbfd != NULL
1261 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1262 && newdyn != olddyn)
1263 {
1264 h->root.non_ir_ref_dynamic = TRUE;
1265 hi->root.non_ir_ref_dynamic = TRUE;
1266 }
1267
45d6a902
AM
1268 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1269 respectively, appear to be a definition rather than reference. */
1270
707bba77 1271 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1272
707bba77
AM
1273 olddef = (h->root.type != bfd_link_hash_undefined
1274 && h->root.type != bfd_link_hash_undefweak
202ac193 1275 && h->root.type != bfd_link_hash_common);
45d6a902 1276
0a36a439
L
1277 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1278 respectively, appear to be a function. */
1279
1280 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1281 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1282
1283 oldfunc = (h->type != STT_NOTYPE
1284 && bed->is_function_type (h->type));
1285
c5d37467 1286 if (!(newfunc && oldfunc)
5b677558
AM
1287 && ELF_ST_TYPE (sym->st_info) != h->type
1288 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1289 && h->type != STT_NOTYPE
c5d37467
AM
1290 && (newdef || bfd_is_com_section (sec))
1291 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1292 {
c5d37467
AM
1293 /* If creating a default indirect symbol ("foo" or "foo@") from
1294 a dynamic versioned definition ("foo@@") skip doing so if
1295 there is an existing regular definition with a different
1296 type. We don't want, for example, a "time" variable in the
1297 executable overriding a "time" function in a shared library. */
1298 if (newdyn
1299 && !olddyn)
1300 {
1301 *skip = TRUE;
1302 return TRUE;
1303 }
1304
1305 /* When adding a symbol from a regular object file after we have
1306 created indirect symbols, undo the indirection and any
1307 dynamic state. */
1308 if (hi != h
1309 && !newdyn
1310 && olddyn)
1311 {
1312 h = hi;
1313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1314 h->forced_local = 0;
1315 h->ref_dynamic = 0;
1316 h->def_dynamic = 0;
1317 h->dynamic_def = 0;
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1319 {
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328 return TRUE;
1329 }
580a2b6e
L
1330 }
1331
4c34aff8
AM
1332 /* Check TLS symbols. We don't check undefined symbols introduced
1333 by "ld -u" which have no type (and oldbfd NULL), and we don't
1334 check symbols from plugins because they also have no type. */
1335 if (oldbfd != NULL
1336 && (oldbfd->flags & BFD_PLUGIN) == 0
1337 && (abfd->flags & BFD_PLUGIN) == 0
1338 && ELF_ST_TYPE (sym->st_info) != h->type
1339 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1340 {
1341 bfd *ntbfd, *tbfd;
1342 bfd_boolean ntdef, tdef;
1343 asection *ntsec, *tsec;
1344
1345 if (h->type == STT_TLS)
1346 {
3b36f7e6 1347 ntbfd = abfd;
7479dfd4
L
1348 ntsec = sec;
1349 ntdef = newdef;
1350 tbfd = oldbfd;
1351 tsec = oldsec;
1352 tdef = olddef;
1353 }
1354 else
1355 {
1356 ntbfd = oldbfd;
1357 ntsec = oldsec;
1358 ntdef = olddef;
1359 tbfd = abfd;
1360 tsec = sec;
1361 tdef = newdef;
1362 }
1363
1364 if (tdef && ntdef)
4eca0228 1365 _bfd_error_handler
695344c0 1366 /* xgettext:c-format */
871b3ab2
AM
1367 (_("%s: TLS definition in %pB section %pA "
1368 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1369 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1370 else if (!tdef && !ntdef)
4eca0228 1371 _bfd_error_handler
695344c0 1372 /* xgettext:c-format */
871b3ab2
AM
1373 (_("%s: TLS reference in %pB "
1374 "mismatches non-TLS reference in %pB"),
c08bb8dd 1375 h->root.root.string, tbfd, ntbfd);
7479dfd4 1376 else if (tdef)
4eca0228 1377 _bfd_error_handler
695344c0 1378 /* xgettext:c-format */
871b3ab2
AM
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS reference in %pB"),
c08bb8dd 1381 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1382 else
4eca0228 1383 _bfd_error_handler
695344c0 1384 /* xgettext:c-format */
871b3ab2
AM
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1387 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1388
1389 bfd_set_error (bfd_error_bad_value);
1390 return FALSE;
1391 }
1392
45d6a902
AM
1393 /* If the old symbol has non-default visibility, we ignore the new
1394 definition from a dynamic object. */
1395 if (newdyn
9c7a29a3 1396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1397 && !bfd_is_und_section (sec))
1398 {
1399 *skip = TRUE;
1400 /* Make sure this symbol is dynamic. */
f5385ebf 1401 h->ref_dynamic = 1;
90c984fc 1402 hi->ref_dynamic = 1;
45d6a902
AM
1403 /* A protected symbol has external availability. Make sure it is
1404 recorded as dynamic.
1405
1406 FIXME: Should we check type and size for protected symbol? */
1407 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1408 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1409 else
1410 return TRUE;
1411 }
1412 else if (!newdyn
9c7a29a3 1413 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1414 && h->def_dynamic)
45d6a902
AM
1415 {
1416 /* If the new symbol with non-default visibility comes from a
1417 relocatable file and the old definition comes from a dynamic
1418 object, we remove the old definition. */
6c9b78e6 1419 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1420 {
1421 /* Handle the case where the old dynamic definition is
1422 default versioned. We need to copy the symbol info from
1423 the symbol with default version to the normal one if it
1424 was referenced before. */
1425 if (h->ref_regular)
1426 {
6c9b78e6 1427 hi->root.type = h->root.type;
d2dee3b2 1428 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1429 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1430
6c9b78e6 1431 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1432 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1433 {
aed81c4e
MR
1434 /* If the new symbol is hidden or internal, completely undo
1435 any dynamic link state. */
1436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1437 h->forced_local = 0;
1438 h->ref_dynamic = 0;
d2dee3b2
L
1439 }
1440 else
aed81c4e
MR
1441 h->ref_dynamic = 1;
1442
1443 h->def_dynamic = 0;
aed81c4e
MR
1444 /* FIXME: Should we check type and size for protected symbol? */
1445 h->size = 0;
1446 h->type = 0;
1447
6c9b78e6 1448 h = hi;
d2dee3b2
L
1449 }
1450 else
6c9b78e6 1451 h = hi;
d2dee3b2 1452 }
1de1a317 1453
f5eda473
AM
1454 /* If the old symbol was undefined before, then it will still be
1455 on the undefs list. If the new symbol is undefined or
1456 common, we can't make it bfd_link_hash_new here, because new
1457 undefined or common symbols will be added to the undefs list
1458 by _bfd_generic_link_add_one_symbol. Symbols may not be
1459 added twice to the undefs list. Also, if the new symbol is
1460 undefweak then we don't want to lose the strong undef. */
1461 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1462 {
1de1a317 1463 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1464 h->root.u.undef.abfd = abfd;
1465 }
1466 else
1467 {
1468 h->root.type = bfd_link_hash_new;
1469 h->root.u.undef.abfd = NULL;
1470 }
1471
f5eda473 1472 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1473 {
f5eda473
AM
1474 /* If the new symbol is hidden or internal, completely undo
1475 any dynamic link state. */
1476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1477 h->forced_local = 0;
1478 h->ref_dynamic = 0;
45d6a902 1479 }
f5eda473
AM
1480 else
1481 h->ref_dynamic = 1;
1482 h->def_dynamic = 0;
45d6a902
AM
1483 /* FIXME: Should we check type and size for protected symbol? */
1484 h->size = 0;
1485 h->type = 0;
1486 return TRUE;
1487 }
14a793b2 1488
15b43f48
AM
1489 /* If a new weak symbol definition comes from a regular file and the
1490 old symbol comes from a dynamic library, we treat the new one as
1491 strong. Similarly, an old weak symbol definition from a regular
1492 file is treated as strong when the new symbol comes from a dynamic
1493 library. Further, an old weak symbol from a dynamic library is
1494 treated as strong if the new symbol is from a dynamic library.
1495 This reflects the way glibc's ld.so works.
1496
165f707a
AM
1497 Also allow a weak symbol to override a linker script symbol
1498 defined by an early pass over the script. This is done so the
1499 linker knows the symbol is defined in an object file, for the
1500 DEFINED script function.
1501
15b43f48
AM
1502 Do this before setting *type_change_ok or *size_change_ok so that
1503 we warn properly when dynamic library symbols are overridden. */
1504
165f707a 1505 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1506 newweak = FALSE;
15b43f48 1507 if (olddef && newdyn)
0f8a2703
AM
1508 oldweak = FALSE;
1509
d334575b 1510 /* Allow changes between different types of function symbol. */
0a36a439 1511 if (newfunc && oldfunc)
fcb93ecf
PB
1512 *type_change_ok = TRUE;
1513
79349b09
AM
1514 /* It's OK to change the type if either the existing symbol or the
1515 new symbol is weak. A type change is also OK if the old symbol
1516 is undefined and the new symbol is defined. */
252b5132 1517
79349b09
AM
1518 if (oldweak
1519 || newweak
1520 || (newdef
1521 && h->root.type == bfd_link_hash_undefined))
1522 *type_change_ok = TRUE;
1523
1524 /* It's OK to change the size if either the existing symbol or the
1525 new symbol is weak, or if the old symbol is undefined. */
1526
1527 if (*type_change_ok
1528 || h->root.type == bfd_link_hash_undefined)
1529 *size_change_ok = TRUE;
45d6a902 1530
45d6a902
AM
1531 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1532 symbol, respectively, appears to be a common symbol in a dynamic
1533 object. If a symbol appears in an uninitialized section, and is
1534 not weak, and is not a function, then it may be a common symbol
1535 which was resolved when the dynamic object was created. We want
1536 to treat such symbols specially, because they raise special
1537 considerations when setting the symbol size: if the symbol
1538 appears as a common symbol in a regular object, and the size in
1539 the regular object is larger, we must make sure that we use the
1540 larger size. This problematic case can always be avoided in C,
1541 but it must be handled correctly when using Fortran shared
1542 libraries.
1543
1544 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1545 likewise for OLDDYNCOMMON and OLDDEF.
1546
1547 Note that this test is just a heuristic, and that it is quite
1548 possible to have an uninitialized symbol in a shared object which
1549 is really a definition, rather than a common symbol. This could
1550 lead to some minor confusion when the symbol really is a common
1551 symbol in some regular object. However, I think it will be
1552 harmless. */
1553
1554 if (newdyn
1555 && newdef
79349b09 1556 && !newweak
45d6a902
AM
1557 && (sec->flags & SEC_ALLOC) != 0
1558 && (sec->flags & SEC_LOAD) == 0
1559 && sym->st_size > 0
0a36a439 1560 && !newfunc)
45d6a902
AM
1561 newdyncommon = TRUE;
1562 else
1563 newdyncommon = FALSE;
1564
1565 if (olddyn
1566 && olddef
1567 && h->root.type == bfd_link_hash_defined
f5385ebf 1568 && h->def_dynamic
45d6a902
AM
1569 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1570 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1571 && h->size > 0
0a36a439 1572 && !oldfunc)
45d6a902
AM
1573 olddyncommon = TRUE;
1574 else
1575 olddyncommon = FALSE;
1576
a4d8e49b
L
1577 /* We now know everything about the old and new symbols. We ask the
1578 backend to check if we can merge them. */
5d13b3b3
AM
1579 if (bed->merge_symbol != NULL)
1580 {
1581 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1582 return FALSE;
1583 sec = *psec;
1584 }
a4d8e49b 1585
a83ef4d1
L
1586 /* There are multiple definitions of a normal symbol. Skip the
1587 default symbol as well as definition from an IR object. */
93f4de39 1588 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1589 && !default_sym && h->def_regular
1590 && !(oldbfd != NULL
1591 && (oldbfd->flags & BFD_PLUGIN) != 0
1592 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1593 {
1594 /* Handle a multiple definition. */
1595 (*info->callbacks->multiple_definition) (info, &h->root,
1596 abfd, sec, *pvalue);
1597 *skip = TRUE;
1598 return TRUE;
1599 }
1600
45d6a902
AM
1601 /* If both the old and the new symbols look like common symbols in a
1602 dynamic object, set the size of the symbol to the larger of the
1603 two. */
1604
1605 if (olddyncommon
1606 && newdyncommon
1607 && sym->st_size != h->size)
1608 {
1609 /* Since we think we have two common symbols, issue a multiple
1610 common warning if desired. Note that we only warn if the
1611 size is different. If the size is the same, we simply let
1612 the old symbol override the new one as normally happens with
1613 symbols defined in dynamic objects. */
1614
1a72702b
AM
1615 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1616 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1617 if (sym->st_size > h->size)
1618 h->size = sym->st_size;
252b5132 1619
45d6a902 1620 *size_change_ok = TRUE;
252b5132
RH
1621 }
1622
45d6a902
AM
1623 /* If we are looking at a dynamic object, and we have found a
1624 definition, we need to see if the symbol was already defined by
1625 some other object. If so, we want to use the existing
1626 definition, and we do not want to report a multiple symbol
1627 definition error; we do this by clobbering *PSEC to be
1628 bfd_und_section_ptr.
1629
1630 We treat a common symbol as a definition if the symbol in the
1631 shared library is a function, since common symbols always
1632 represent variables; this can cause confusion in principle, but
1633 any such confusion would seem to indicate an erroneous program or
1634 shared library. We also permit a common symbol in a regular
8170f769 1635 object to override a weak symbol in a shared object. */
45d6a902
AM
1636
1637 if (newdyn
1638 && newdef
77cfaee6 1639 && (olddef
45d6a902 1640 || (h->root.type == bfd_link_hash_common
8170f769 1641 && (newweak || newfunc))))
45d6a902
AM
1642 {
1643 *override = TRUE;
1644 newdef = FALSE;
1645 newdyncommon = FALSE;
252b5132 1646
45d6a902
AM
1647 *psec = sec = bfd_und_section_ptr;
1648 *size_change_ok = TRUE;
252b5132 1649
45d6a902
AM
1650 /* If we get here when the old symbol is a common symbol, then
1651 we are explicitly letting it override a weak symbol or
1652 function in a dynamic object, and we don't want to warn about
1653 a type change. If the old symbol is a defined symbol, a type
1654 change warning may still be appropriate. */
252b5132 1655
45d6a902
AM
1656 if (h->root.type == bfd_link_hash_common)
1657 *type_change_ok = TRUE;
1658 }
1659
1660 /* Handle the special case of an old common symbol merging with a
1661 new symbol which looks like a common symbol in a shared object.
1662 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1663 common symbol, and let _bfd_generic_link_add_one_symbol do the
1664 right thing. */
45d6a902
AM
1665
1666 if (newdyncommon
1667 && h->root.type == bfd_link_hash_common)
1668 {
1669 *override = TRUE;
1670 newdef = FALSE;
1671 newdyncommon = FALSE;
1672 *pvalue = sym->st_size;
a4d8e49b 1673 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1674 *size_change_ok = TRUE;
1675 }
1676
c5e2cead 1677 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1678 if (newdef && olddef && newweak)
54ac0771 1679 {
35ed3f94 1680 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1681 if (!(oldbfd != NULL
1682 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1683 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1684 {
1685 newdef = FALSE;
1686 *skip = TRUE;
1687 }
54ac0771
L
1688
1689 /* Merge st_other. If the symbol already has a dynamic index,
1690 but visibility says it should not be visible, turn it into a
1691 local symbol. */
b8417128 1692 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1693 if (h->dynindx != -1)
1694 switch (ELF_ST_VISIBILITY (h->other))
1695 {
1696 case STV_INTERNAL:
1697 case STV_HIDDEN:
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1699 break;
1700 }
1701 }
c5e2cead 1702
45d6a902
AM
1703 /* If the old symbol is from a dynamic object, and the new symbol is
1704 a definition which is not from a dynamic object, then the new
1705 symbol overrides the old symbol. Symbols from regular files
1706 always take precedence over symbols from dynamic objects, even if
1707 they are defined after the dynamic object in the link.
1708
1709 As above, we again permit a common symbol in a regular object to
1710 override a definition in a shared object if the shared object
0f8a2703 1711 symbol is a function or is weak. */
45d6a902
AM
1712
1713 flip = NULL;
77cfaee6 1714 if (!newdyn
45d6a902
AM
1715 && (newdef
1716 || (bfd_is_com_section (sec)
0a36a439 1717 && (oldweak || oldfunc)))
45d6a902
AM
1718 && olddyn
1719 && olddef
f5385ebf 1720 && h->def_dynamic)
45d6a902
AM
1721 {
1722 /* Change the hash table entry to undefined, and let
1723 _bfd_generic_link_add_one_symbol do the right thing with the
1724 new definition. */
1725
1726 h->root.type = bfd_link_hash_undefined;
1727 h->root.u.undef.abfd = h->root.u.def.section->owner;
1728 *size_change_ok = TRUE;
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 /* We again permit a type change when a common symbol may be
1734 overriding a function. */
1735
1736 if (bfd_is_com_section (sec))
0a36a439
L
1737 {
1738 if (oldfunc)
1739 {
1740 /* If a common symbol overrides a function, make sure
1741 that it isn't defined dynamically nor has type
1742 function. */
1743 h->def_dynamic = 0;
1744 h->type = STT_NOTYPE;
1745 }
1746 *type_change_ok = TRUE;
1747 }
45d6a902 1748
6c9b78e6
AM
1749 if (hi->root.type == bfd_link_hash_indirect)
1750 flip = hi;
45d6a902
AM
1751 else
1752 /* This union may have been set to be non-NULL when this symbol
1753 was seen in a dynamic object. We must force the union to be
1754 NULL, so that it is correct for a regular symbol. */
1755 h->verinfo.vertree = NULL;
1756 }
1757
1758 /* Handle the special case of a new common symbol merging with an
1759 old symbol that looks like it might be a common symbol defined in
1760 a shared object. Note that we have already handled the case in
1761 which a new common symbol should simply override the definition
1762 in the shared library. */
1763
1764 if (! newdyn
1765 && bfd_is_com_section (sec)
1766 && olddyncommon)
1767 {
1768 /* It would be best if we could set the hash table entry to a
1769 common symbol, but we don't know what to use for the section
1770 or the alignment. */
1a72702b
AM
1771 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1772 bfd_link_hash_common, sym->st_size);
45d6a902 1773
4cc11e76 1774 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1775 larger, pretend that the new symbol has its size. */
1776
1777 if (h->size > *pvalue)
1778 *pvalue = h->size;
1779
af44c138
L
1780 /* We need to remember the alignment required by the symbol
1781 in the dynamic object. */
1782 BFD_ASSERT (pold_alignment);
1783 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1784
1785 olddef = FALSE;
1786 olddyncommon = FALSE;
1787
1788 h->root.type = bfd_link_hash_undefined;
1789 h->root.u.undef.abfd = h->root.u.def.section->owner;
1790
1791 *size_change_ok = TRUE;
1792 *type_change_ok = TRUE;
1793
6c9b78e6
AM
1794 if (hi->root.type == bfd_link_hash_indirect)
1795 flip = hi;
45d6a902
AM
1796 else
1797 h->verinfo.vertree = NULL;
1798 }
1799
1800 if (flip != NULL)
1801 {
1802 /* Handle the case where we had a versioned symbol in a dynamic
1803 library and now find a definition in a normal object. In this
1804 case, we make the versioned symbol point to the normal one. */
45d6a902 1805 flip->root.type = h->root.type;
00cbee0a 1806 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1807 h->root.type = bfd_link_hash_indirect;
1808 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1809 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1810 if (h->def_dynamic)
45d6a902 1811 {
f5385ebf
AM
1812 h->def_dynamic = 0;
1813 flip->ref_dynamic = 1;
45d6a902
AM
1814 }
1815 }
1816
45d6a902
AM
1817 return TRUE;
1818}
1819
1820/* This function is called to create an indirect symbol from the
1821 default for the symbol with the default version if needed. The
4f3fedcf 1822 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1823 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1824
28caa186 1825static bfd_boolean
268b6b39
AM
1826_bfd_elf_add_default_symbol (bfd *abfd,
1827 struct bfd_link_info *info,
1828 struct elf_link_hash_entry *h,
1829 const char *name,
1830 Elf_Internal_Sym *sym,
4f3fedcf
AM
1831 asection *sec,
1832 bfd_vma value,
1833 bfd **poldbfd,
e3c9d234 1834 bfd_boolean *dynsym)
45d6a902
AM
1835{
1836 bfd_boolean type_change_ok;
1837 bfd_boolean size_change_ok;
1838 bfd_boolean skip;
1839 char *shortname;
1840 struct elf_link_hash_entry *hi;
1841 struct bfd_link_hash_entry *bh;
9c5bfbb7 1842 const struct elf_backend_data *bed;
45d6a902
AM
1843 bfd_boolean collect;
1844 bfd_boolean dynamic;
e3c9d234 1845 bfd_boolean override;
45d6a902
AM
1846 char *p;
1847 size_t len, shortlen;
ffd65175 1848 asection *tmp_sec;
6e33951e 1849 bfd_boolean matched;
45d6a902 1850
422f1182
L
1851 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1852 return TRUE;
1853
45d6a902
AM
1854 /* If this symbol has a version, and it is the default version, we
1855 create an indirect symbol from the default name to the fully
1856 decorated name. This will cause external references which do not
1857 specify a version to be bound to this version of the symbol. */
1858 p = strchr (name, ELF_VER_CHR);
422f1182
L
1859 if (h->versioned == unknown)
1860 {
1861 if (p == NULL)
1862 {
1863 h->versioned = unversioned;
1864 return TRUE;
1865 }
1866 else
1867 {
1868 if (p[1] != ELF_VER_CHR)
1869 {
1870 h->versioned = versioned_hidden;
1871 return TRUE;
1872 }
1873 else
1874 h->versioned = versioned;
1875 }
1876 }
4373f8af
L
1877 else
1878 {
1879 /* PR ld/19073: We may see an unversioned definition after the
1880 default version. */
1881 if (p == NULL)
1882 return TRUE;
1883 }
45d6a902 1884
45d6a902
AM
1885 bed = get_elf_backend_data (abfd);
1886 collect = bed->collect;
1887 dynamic = (abfd->flags & DYNAMIC) != 0;
1888
1889 shortlen = p - name;
a50b1753 1890 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1891 if (shortname == NULL)
1892 return FALSE;
1893 memcpy (shortname, name, shortlen);
1894 shortname[shortlen] = '\0';
1895
1896 /* We are going to create a new symbol. Merge it with any existing
1897 symbol with this name. For the purposes of the merge, act as
1898 though we were defining the symbol we just defined, although we
1899 actually going to define an indirect symbol. */
1900 type_change_ok = FALSE;
1901 size_change_ok = FALSE;
6e33951e 1902 matched = TRUE;
ffd65175
AM
1903 tmp_sec = sec;
1904 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1905 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1906 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1907 return FALSE;
1908
1909 if (skip)
1910 goto nondefault;
1911
5b677558
AM
1912 if (hi->def_regular)
1913 {
1914 /* If the undecorated symbol will have a version added by a
1915 script different to H, then don't indirect to/from the
1916 undecorated symbol. This isn't ideal because we may not yet
1917 have seen symbol versions, if given by a script on the
1918 command line rather than via --version-script. */
1919 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1920 {
1921 bfd_boolean hide;
1922
1923 hi->verinfo.vertree
1924 = bfd_find_version_for_sym (info->version_info,
1925 hi->root.root.string, &hide);
1926 if (hi->verinfo.vertree != NULL && hide)
1927 {
1928 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1929 goto nondefault;
1930 }
1931 }
1932 if (hi->verinfo.vertree != NULL
1933 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 goto nondefault;
1935 }
1936
45d6a902
AM
1937 if (! override)
1938 {
c6e8a9a8 1939 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1940 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1941 {
1942 bh = &hi->root;
1943 if (! (_bfd_generic_link_add_one_symbol
1944 (info, abfd, shortname, BSF_INDIRECT,
1945 bfd_ind_section_ptr,
1946 0, name, FALSE, collect, &bh)))
1947 return FALSE;
1948 hi = (struct elf_link_hash_entry *) bh;
1949 }
45d6a902
AM
1950 }
1951 else
1952 {
1953 /* In this case the symbol named SHORTNAME is overriding the
1954 indirect symbol we want to add. We were planning on making
1955 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1956 is the name without a version. NAME is the fully versioned
1957 name, and it is the default version.
1958
1959 Overriding means that we already saw a definition for the
1960 symbol SHORTNAME in a regular object, and it is overriding
1961 the symbol defined in the dynamic object.
1962
1963 When this happens, we actually want to change NAME, the
1964 symbol we just added, to refer to SHORTNAME. This will cause
1965 references to NAME in the shared object to become references
1966 to SHORTNAME in the regular object. This is what we expect
1967 when we override a function in a shared object: that the
1968 references in the shared object will be mapped to the
1969 definition in the regular object. */
1970
1971 while (hi->root.type == bfd_link_hash_indirect
1972 || hi->root.type == bfd_link_hash_warning)
1973 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1974
1975 h->root.type = bfd_link_hash_indirect;
1976 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1977 if (h->def_dynamic)
45d6a902 1978 {
f5385ebf
AM
1979 h->def_dynamic = 0;
1980 hi->ref_dynamic = 1;
1981 if (hi->ref_regular
1982 || hi->def_regular)
45d6a902 1983 {
c152c796 1984 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1985 return FALSE;
1986 }
1987 }
1988
1989 /* Now set HI to H, so that the following code will set the
1990 other fields correctly. */
1991 hi = h;
1992 }
1993
fab4a87f
L
1994 /* Check if HI is a warning symbol. */
1995 if (hi->root.type == bfd_link_hash_warning)
1996 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1997
45d6a902
AM
1998 /* If there is a duplicate definition somewhere, then HI may not
1999 point to an indirect symbol. We will have reported an error to
2000 the user in that case. */
2001
2002 if (hi->root.type == bfd_link_hash_indirect)
2003 {
2004 struct elf_link_hash_entry *ht;
2005
45d6a902 2006 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2007 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2008
68c88cd4
AM
2009 /* A reference to the SHORTNAME symbol from a dynamic library
2010 will be satisfied by the versioned symbol at runtime. In
2011 effect, we have a reference to the versioned symbol. */
2012 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2013 hi->dynamic_def |= ht->dynamic_def;
2014
45d6a902
AM
2015 /* See if the new flags lead us to realize that the symbol must
2016 be dynamic. */
2017 if (! *dynsym)
2018 {
2019 if (! dynamic)
2020 {
0e1862bb 2021 if (! bfd_link_executable (info)
90c984fc 2022 || hi->def_dynamic
f5385ebf 2023 || hi->ref_dynamic)
45d6a902
AM
2024 *dynsym = TRUE;
2025 }
2026 else
2027 {
f5385ebf 2028 if (hi->ref_regular)
45d6a902
AM
2029 *dynsym = TRUE;
2030 }
2031 }
2032 }
2033
2034 /* We also need to define an indirection from the nondefault version
2035 of the symbol. */
2036
2037nondefault:
2038 len = strlen (name);
a50b1753 2039 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2040 if (shortname == NULL)
2041 return FALSE;
2042 memcpy (shortname, name, shortlen);
2043 memcpy (shortname + shortlen, p + 1, len - shortlen);
2044
2045 /* Once again, merge with any existing symbol. */
2046 type_change_ok = FALSE;
2047 size_change_ok = FALSE;
ffd65175
AM
2048 tmp_sec = sec;
2049 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2050 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2051 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2052 return FALSE;
2053
2054 if (skip)
2055 return TRUE;
2056
2057 if (override)
2058 {
2059 /* Here SHORTNAME is a versioned name, so we don't expect to see
2060 the type of override we do in the case above unless it is
4cc11e76 2061 overridden by a versioned definition. */
45d6a902
AM
2062 if (hi->root.type != bfd_link_hash_defined
2063 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2064 _bfd_error_handler
695344c0 2065 /* xgettext:c-format */
871b3ab2 2066 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2067 abfd, shortname);
45d6a902
AM
2068 }
2069 else
2070 {
2071 bh = &hi->root;
2072 if (! (_bfd_generic_link_add_one_symbol
2073 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2074 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2075 return FALSE;
2076 hi = (struct elf_link_hash_entry *) bh;
2077
2078 /* If there is a duplicate definition somewhere, then HI may not
2079 point to an indirect symbol. We will have reported an error
2080 to the user in that case. */
2081
2082 if (hi->root.type == bfd_link_hash_indirect)
2083 {
fcfa13d2 2084 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2085 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2086 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2087
2088 /* See if the new flags lead us to realize that the symbol
2089 must be dynamic. */
2090 if (! *dynsym)
2091 {
2092 if (! dynamic)
2093 {
0e1862bb 2094 if (! bfd_link_executable (info)
f5385ebf 2095 || hi->ref_dynamic)
45d6a902
AM
2096 *dynsym = TRUE;
2097 }
2098 else
2099 {
f5385ebf 2100 if (hi->ref_regular)
45d6a902
AM
2101 *dynsym = TRUE;
2102 }
2103 }
2104 }
2105 }
2106
2107 return TRUE;
2108}
2109\f
2110/* This routine is used to export all defined symbols into the dynamic
2111 symbol table. It is called via elf_link_hash_traverse. */
2112
28caa186 2113static bfd_boolean
268b6b39 2114_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2115{
a50b1753 2116 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2117
2118 /* Ignore indirect symbols. These are added by the versioning code. */
2119 if (h->root.type == bfd_link_hash_indirect)
2120 return TRUE;
2121
7686d77d
AM
2122 /* Ignore this if we won't export it. */
2123 if (!eif->info->export_dynamic && !h->dynamic)
2124 return TRUE;
45d6a902
AM
2125
2126 if (h->dynindx == -1
fd91d419
L
2127 && (h->def_regular || h->ref_regular)
2128 && ! bfd_hide_sym_by_version (eif->info->version_info,
2129 h->root.root.string))
45d6a902 2130 {
fd91d419 2131 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2132 {
fd91d419
L
2133 eif->failed = TRUE;
2134 return FALSE;
45d6a902
AM
2135 }
2136 }
2137
2138 return TRUE;
2139}
2140\f
2141/* Look through the symbols which are defined in other shared
2142 libraries and referenced here. Update the list of version
2143 dependencies. This will be put into the .gnu.version_r section.
2144 This function is called via elf_link_hash_traverse. */
2145
28caa186 2146static bfd_boolean
268b6b39
AM
2147_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2148 void *data)
45d6a902 2149{
a50b1753 2150 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2151 Elf_Internal_Verneed *t;
2152 Elf_Internal_Vernaux *a;
2153 bfd_size_type amt;
2154
45d6a902
AM
2155 /* We only care about symbols defined in shared objects with version
2156 information. */
f5385ebf
AM
2157 if (!h->def_dynamic
2158 || h->def_regular
45d6a902 2159 || h->dynindx == -1
7b20f099
AM
2160 || h->verinfo.verdef == NULL
2161 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2162 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2163 return TRUE;
2164
2165 /* See if we already know about this version. */
28caa186
AM
2166 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2167 t != NULL;
2168 t = t->vn_nextref)
45d6a902
AM
2169 {
2170 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2171 continue;
2172
2173 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2174 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2175 return TRUE;
2176
2177 break;
2178 }
2179
2180 /* This is a new version. Add it to tree we are building. */
2181
2182 if (t == NULL)
2183 {
2184 amt = sizeof *t;
a50b1753 2185 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2186 if (t == NULL)
2187 {
2188 rinfo->failed = TRUE;
2189 return FALSE;
2190 }
2191
2192 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2193 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2194 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2195 }
2196
2197 amt = sizeof *a;
a50b1753 2198 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2199 if (a == NULL)
2200 {
2201 rinfo->failed = TRUE;
2202 return FALSE;
2203 }
45d6a902
AM
2204
2205 /* Note that we are copying a string pointer here, and testing it
2206 above. If bfd_elf_string_from_elf_section is ever changed to
2207 discard the string data when low in memory, this will have to be
2208 fixed. */
2209 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2210
2211 a->vna_flags = h->verinfo.verdef->vd_flags;
2212 a->vna_nextptr = t->vn_auxptr;
2213
2214 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2215 ++rinfo->vers;
2216
2217 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2218
2219 t->vn_auxptr = a;
2220
2221 return TRUE;
2222}
2223
099bb8fb
L
2224/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2225 hidden. Set *T_P to NULL if there is no match. */
2226
2227static bfd_boolean
2228_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2229 struct elf_link_hash_entry *h,
2230 const char *version_p,
2231 struct bfd_elf_version_tree **t_p,
2232 bfd_boolean *hide)
2233{
2234 struct bfd_elf_version_tree *t;
2235
2236 /* Look for the version. If we find it, it is no longer weak. */
2237 for (t = info->version_info; t != NULL; t = t->next)
2238 {
2239 if (strcmp (t->name, version_p) == 0)
2240 {
2241 size_t len;
2242 char *alc;
2243 struct bfd_elf_version_expr *d;
2244
2245 len = version_p - h->root.root.string;
2246 alc = (char *) bfd_malloc (len);
2247 if (alc == NULL)
2248 return FALSE;
2249 memcpy (alc, h->root.root.string, len - 1);
2250 alc[len - 1] = '\0';
2251 if (alc[len - 2] == ELF_VER_CHR)
2252 alc[len - 2] = '\0';
2253
2254 h->verinfo.vertree = t;
2255 t->used = TRUE;
2256 d = NULL;
2257
2258 if (t->globals.list != NULL)
2259 d = (*t->match) (&t->globals, NULL, alc);
2260
2261 /* See if there is anything to force this symbol to
2262 local scope. */
2263 if (d == NULL && t->locals.list != NULL)
2264 {
2265 d = (*t->match) (&t->locals, NULL, alc);
2266 if (d != NULL
2267 && h->dynindx != -1
2268 && ! info->export_dynamic)
2269 *hide = TRUE;
2270 }
2271
2272 free (alc);
2273 break;
2274 }
2275 }
2276
2277 *t_p = t;
2278
2279 return TRUE;
2280}
2281
2282/* Return TRUE if the symbol H is hidden by version script. */
2283
2284bfd_boolean
2285_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2286 struct elf_link_hash_entry *h)
2287{
2288 const char *p;
2289 bfd_boolean hide = FALSE;
2290 const struct elf_backend_data *bed
2291 = get_elf_backend_data (info->output_bfd);
2292
2293 /* Version script only hides symbols defined in regular objects. */
2294 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2295 return TRUE;
2296
2297 p = strchr (h->root.root.string, ELF_VER_CHR);
2298 if (p != NULL && h->verinfo.vertree == NULL)
2299 {
2300 struct bfd_elf_version_tree *t;
2301
2302 ++p;
2303 if (*p == ELF_VER_CHR)
2304 ++p;
2305
2306 if (*p != '\0'
2307 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2308 && hide)
2309 {
2310 if (hide)
2311 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2312 return TRUE;
2313 }
2314 }
2315
2316 /* If we don't have a version for this symbol, see if we can find
2317 something. */
2318 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2319 {
2320 h->verinfo.vertree
2321 = bfd_find_version_for_sym (info->version_info,
2322 h->root.root.string, &hide);
2323 if (h->verinfo.vertree != NULL && hide)
2324 {
2325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2326 return TRUE;
2327 }
2328 }
2329
2330 return FALSE;
2331}
2332
45d6a902
AM
2333/* Figure out appropriate versions for all the symbols. We may not
2334 have the version number script until we have read all of the input
2335 files, so until that point we don't know which symbols should be
2336 local. This function is called via elf_link_hash_traverse. */
2337
28caa186 2338static bfd_boolean
268b6b39 2339_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2340{
28caa186 2341 struct elf_info_failed *sinfo;
45d6a902 2342 struct bfd_link_info *info;
9c5bfbb7 2343 const struct elf_backend_data *bed;
45d6a902
AM
2344 struct elf_info_failed eif;
2345 char *p;
099bb8fb 2346 bfd_boolean hide;
45d6a902 2347
a50b1753 2348 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2349 info = sinfo->info;
2350
45d6a902
AM
2351 /* Fix the symbol flags. */
2352 eif.failed = FALSE;
2353 eif.info = info;
2354 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2355 {
2356 if (eif.failed)
2357 sinfo->failed = TRUE;
2358 return FALSE;
2359 }
2360
2361 /* We only need version numbers for symbols defined in regular
2362 objects. */
f5385ebf 2363 if (!h->def_regular)
45d6a902
AM
2364 return TRUE;
2365
099bb8fb 2366 hide = FALSE;
28caa186 2367 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2368 p = strchr (h->root.root.string, ELF_VER_CHR);
2369 if (p != NULL && h->verinfo.vertree == NULL)
2370 {
2371 struct bfd_elf_version_tree *t;
45d6a902 2372
45d6a902
AM
2373 ++p;
2374 if (*p == ELF_VER_CHR)
6e33951e 2375 ++p;
45d6a902
AM
2376
2377 /* If there is no version string, we can just return out. */
2378 if (*p == '\0')
6e33951e 2379 return TRUE;
45d6a902 2380
099bb8fb 2381 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2382 {
099bb8fb
L
2383 sinfo->failed = TRUE;
2384 return FALSE;
45d6a902
AM
2385 }
2386
099bb8fb
L
2387 if (hide)
2388 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2389
45d6a902
AM
2390 /* If we are building an application, we need to create a
2391 version node for this version. */
0e1862bb 2392 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2393 {
2394 struct bfd_elf_version_tree **pp;
2395 int version_index;
2396
2397 /* If we aren't going to export this symbol, we don't need
2398 to worry about it. */
2399 if (h->dynindx == -1)
2400 return TRUE;
2401
ef53be89
AM
2402 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2403 sizeof *t);
45d6a902
AM
2404 if (t == NULL)
2405 {
2406 sinfo->failed = TRUE;
2407 return FALSE;
2408 }
2409
45d6a902 2410 t->name = p;
45d6a902
AM
2411 t->name_indx = (unsigned int) -1;
2412 t->used = TRUE;
2413
2414 version_index = 1;
2415 /* Don't count anonymous version tag. */
fd91d419
L
2416 if (sinfo->info->version_info != NULL
2417 && sinfo->info->version_info->vernum == 0)
45d6a902 2418 version_index = 0;
fd91d419
L
2419 for (pp = &sinfo->info->version_info;
2420 *pp != NULL;
2421 pp = &(*pp)->next)
45d6a902
AM
2422 ++version_index;
2423 t->vernum = version_index;
2424
2425 *pp = t;
2426
2427 h->verinfo.vertree = t;
2428 }
2429 else if (t == NULL)
2430 {
2431 /* We could not find the version for a symbol when
2432 generating a shared archive. Return an error. */
4eca0228 2433 _bfd_error_handler
695344c0 2434 /* xgettext:c-format */
871b3ab2 2435 (_("%pB: version node not found for symbol %s"),
28caa186 2436 info->output_bfd, h->root.root.string);
45d6a902
AM
2437 bfd_set_error (bfd_error_bad_value);
2438 sinfo->failed = TRUE;
2439 return FALSE;
2440 }
45d6a902
AM
2441 }
2442
2443 /* If we don't have a version for this symbol, see if we can find
2444 something. */
099bb8fb
L
2445 if (!hide
2446 && h->verinfo.vertree == NULL
2447 && sinfo->info->version_info != NULL)
45d6a902 2448 {
fd91d419
L
2449 h->verinfo.vertree
2450 = bfd_find_version_for_sym (sinfo->info->version_info,
2451 h->root.root.string, &hide);
1e8fa21e
AM
2452 if (h->verinfo.vertree != NULL && hide)
2453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2454 }
2455
2456 return TRUE;
2457}
2458\f
45d6a902
AM
2459/* Read and swap the relocs from the section indicated by SHDR. This
2460 may be either a REL or a RELA section. The relocations are
2461 translated into RELA relocations and stored in INTERNAL_RELOCS,
2462 which should have already been allocated to contain enough space.
2463 The EXTERNAL_RELOCS are a buffer where the external form of the
2464 relocations should be stored.
2465
2466 Returns FALSE if something goes wrong. */
2467
2468static bfd_boolean
268b6b39 2469elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2470 asection *sec,
268b6b39
AM
2471 Elf_Internal_Shdr *shdr,
2472 void *external_relocs,
2473 Elf_Internal_Rela *internal_relocs)
45d6a902 2474{
9c5bfbb7 2475 const struct elf_backend_data *bed;
268b6b39 2476 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2477 const bfd_byte *erela;
2478 const bfd_byte *erelaend;
2479 Elf_Internal_Rela *irela;
243ef1e0
L
2480 Elf_Internal_Shdr *symtab_hdr;
2481 size_t nsyms;
45d6a902 2482
45d6a902
AM
2483 /* Position ourselves at the start of the section. */
2484 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2485 return FALSE;
2486
2487 /* Read the relocations. */
2488 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2489 return FALSE;
2490
243ef1e0 2491 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2492 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2493
45d6a902
AM
2494 bed = get_elf_backend_data (abfd);
2495
2496 /* Convert the external relocations to the internal format. */
2497 if (shdr->sh_entsize == bed->s->sizeof_rel)
2498 swap_in = bed->s->swap_reloc_in;
2499 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2500 swap_in = bed->s->swap_reloca_in;
2501 else
2502 {
2503 bfd_set_error (bfd_error_wrong_format);
2504 return FALSE;
2505 }
2506
a50b1753 2507 erela = (const bfd_byte *) external_relocs;
51992aec 2508 erelaend = erela + shdr->sh_size;
45d6a902
AM
2509 irela = internal_relocs;
2510 while (erela < erelaend)
2511 {
243ef1e0
L
2512 bfd_vma r_symndx;
2513
45d6a902 2514 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2515 r_symndx = ELF32_R_SYM (irela->r_info);
2516 if (bed->s->arch_size == 64)
2517 r_symndx >>= 24;
ce98a316
NC
2518 if (nsyms > 0)
2519 {
2520 if ((size_t) r_symndx >= nsyms)
2521 {
4eca0228 2522 _bfd_error_handler
695344c0 2523 /* xgettext:c-format */
2dcf00ce
AM
2524 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2525 " for offset %#" PRIx64 " in section `%pA'"),
2526 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2527 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2528 bfd_set_error (bfd_error_bad_value);
2529 return FALSE;
2530 }
2531 }
cf35638d 2532 else if (r_symndx != STN_UNDEF)
243ef1e0 2533 {
4eca0228 2534 _bfd_error_handler
695344c0 2535 /* xgettext:c-format */
2dcf00ce
AM
2536 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2537 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2538 " when the object file has no symbol table"),
2dcf00ce
AM
2539 abfd, (uint64_t) r_symndx,
2540 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2541 bfd_set_error (bfd_error_bad_value);
2542 return FALSE;
2543 }
45d6a902
AM
2544 irela += bed->s->int_rels_per_ext_rel;
2545 erela += shdr->sh_entsize;
2546 }
2547
2548 return TRUE;
2549}
2550
2551/* Read and swap the relocs for a section O. They may have been
2552 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2553 not NULL, they are used as buffers to read into. They are known to
2554 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2555 the return value is allocated using either malloc or bfd_alloc,
2556 according to the KEEP_MEMORY argument. If O has two relocation
2557 sections (both REL and RELA relocations), then the REL_HDR
2558 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2559 RELA_HDR relocations. */
45d6a902
AM
2560
2561Elf_Internal_Rela *
268b6b39
AM
2562_bfd_elf_link_read_relocs (bfd *abfd,
2563 asection *o,
2564 void *external_relocs,
2565 Elf_Internal_Rela *internal_relocs,
2566 bfd_boolean keep_memory)
45d6a902 2567{
268b6b39 2568 void *alloc1 = NULL;
45d6a902 2569 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2570 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2571 struct bfd_elf_section_data *esdo = elf_section_data (o);
2572 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2573
d4730f92
BS
2574 if (esdo->relocs != NULL)
2575 return esdo->relocs;
45d6a902
AM
2576
2577 if (o->reloc_count == 0)
2578 return NULL;
2579
45d6a902
AM
2580 if (internal_relocs == NULL)
2581 {
2582 bfd_size_type size;
2583
056bafd4 2584 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2585 if (keep_memory)
a50b1753 2586 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2587 else
a50b1753 2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2589 if (internal_relocs == NULL)
2590 goto error_return;
2591 }
2592
2593 if (external_relocs == NULL)
2594 {
d4730f92
BS
2595 bfd_size_type size = 0;
2596
2597 if (esdo->rel.hdr)
2598 size += esdo->rel.hdr->sh_size;
2599 if (esdo->rela.hdr)
2600 size += esdo->rela.hdr->sh_size;
45d6a902 2601
268b6b39 2602 alloc1 = bfd_malloc (size);
45d6a902
AM
2603 if (alloc1 == NULL)
2604 goto error_return;
2605 external_relocs = alloc1;
2606 }
2607
d4730f92
BS
2608 internal_rela_relocs = internal_relocs;
2609 if (esdo->rel.hdr)
2610 {
2611 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2612 external_relocs,
2613 internal_relocs))
2614 goto error_return;
2615 external_relocs = (((bfd_byte *) external_relocs)
2616 + esdo->rel.hdr->sh_size);
2617 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2618 * bed->s->int_rels_per_ext_rel);
2619 }
2620
2621 if (esdo->rela.hdr
2622 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2623 external_relocs,
2624 internal_rela_relocs)))
45d6a902
AM
2625 goto error_return;
2626
2627 /* Cache the results for next time, if we can. */
2628 if (keep_memory)
d4730f92 2629 esdo->relocs = internal_relocs;
45d6a902
AM
2630
2631 if (alloc1 != NULL)
2632 free (alloc1);
2633
2634 /* Don't free alloc2, since if it was allocated we are passing it
2635 back (under the name of internal_relocs). */
2636
2637 return internal_relocs;
2638
2639 error_return:
2640 if (alloc1 != NULL)
2641 free (alloc1);
2642 if (alloc2 != NULL)
4dd07732
AM
2643 {
2644 if (keep_memory)
2645 bfd_release (abfd, alloc2);
2646 else
2647 free (alloc2);
2648 }
45d6a902
AM
2649 return NULL;
2650}
2651
2652/* Compute the size of, and allocate space for, REL_HDR which is the
2653 section header for a section containing relocations for O. */
2654
28caa186 2655static bfd_boolean
9eaff861
AO
2656_bfd_elf_link_size_reloc_section (bfd *abfd,
2657 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2658{
9eaff861 2659 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2660
2661 /* That allows us to calculate the size of the section. */
9eaff861 2662 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2663
2664 /* The contents field must last into write_object_contents, so we
2665 allocate it with bfd_alloc rather than malloc. Also since we
2666 cannot be sure that the contents will actually be filled in,
2667 we zero the allocated space. */
a50b1753 2668 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2669 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2670 return FALSE;
2671
d4730f92 2672 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2673 {
2674 struct elf_link_hash_entry **p;
2675
ca4be51c
AM
2676 p = ((struct elf_link_hash_entry **)
2677 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2678 if (p == NULL)
2679 return FALSE;
2680
d4730f92 2681 reldata->hashes = p;
45d6a902
AM
2682 }
2683
2684 return TRUE;
2685}
2686
2687/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2688 originated from the section given by INPUT_REL_HDR) to the
2689 OUTPUT_BFD. */
2690
2691bfd_boolean
268b6b39
AM
2692_bfd_elf_link_output_relocs (bfd *output_bfd,
2693 asection *input_section,
2694 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2695 Elf_Internal_Rela *internal_relocs,
2696 struct elf_link_hash_entry **rel_hash
2697 ATTRIBUTE_UNUSED)
45d6a902
AM
2698{
2699 Elf_Internal_Rela *irela;
2700 Elf_Internal_Rela *irelaend;
2701 bfd_byte *erel;
d4730f92 2702 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2703 asection *output_section;
9c5bfbb7 2704 const struct elf_backend_data *bed;
268b6b39 2705 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2706 struct bfd_elf_section_data *esdo;
45d6a902
AM
2707
2708 output_section = input_section->output_section;
45d6a902 2709
d4730f92
BS
2710 bed = get_elf_backend_data (output_bfd);
2711 esdo = elf_section_data (output_section);
2712 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2713 {
d4730f92
BS
2714 output_reldata = &esdo->rel;
2715 swap_out = bed->s->swap_reloc_out;
45d6a902 2716 }
d4730f92
BS
2717 else if (esdo->rela.hdr
2718 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2719 {
d4730f92
BS
2720 output_reldata = &esdo->rela;
2721 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2722 }
2723 else
2724 {
4eca0228 2725 _bfd_error_handler
695344c0 2726 /* xgettext:c-format */
871b3ab2 2727 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2728 output_bfd, input_section->owner, input_section);
297d8443 2729 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2730 return FALSE;
2731 }
2732
d4730f92
BS
2733 erel = output_reldata->hdr->contents;
2734 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2735 irela = internal_relocs;
2736 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2737 * bed->s->int_rels_per_ext_rel);
2738 while (irela < irelaend)
2739 {
2740 (*swap_out) (output_bfd, irela, erel);
2741 irela += bed->s->int_rels_per_ext_rel;
2742 erel += input_rel_hdr->sh_entsize;
2743 }
2744
2745 /* Bump the counter, so that we know where to add the next set of
2746 relocations. */
d4730f92 2747 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2748
2749 return TRUE;
2750}
2751\f
508c3946
L
2752/* Make weak undefined symbols in PIE dynamic. */
2753
2754bfd_boolean
2755_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2756 struct elf_link_hash_entry *h)
2757{
0e1862bb 2758 if (bfd_link_pie (info)
508c3946
L
2759 && h->dynindx == -1
2760 && h->root.type == bfd_link_hash_undefweak)
2761 return bfd_elf_link_record_dynamic_symbol (info, h);
2762
2763 return TRUE;
2764}
2765
45d6a902
AM
2766/* Fix up the flags for a symbol. This handles various cases which
2767 can only be fixed after all the input files are seen. This is
2768 currently called by both adjust_dynamic_symbol and
2769 assign_sym_version, which is unnecessary but perhaps more robust in
2770 the face of future changes. */
2771
28caa186 2772static bfd_boolean
268b6b39
AM
2773_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2774 struct elf_info_failed *eif)
45d6a902 2775{
33774f08 2776 const struct elf_backend_data *bed;
508c3946 2777
45d6a902
AM
2778 /* If this symbol was mentioned in a non-ELF file, try to set
2779 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2780 permit a non-ELF file to correctly refer to a symbol defined in
2781 an ELF dynamic object. */
f5385ebf 2782 if (h->non_elf)
45d6a902
AM
2783 {
2784 while (h->root.type == bfd_link_hash_indirect)
2785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2786
2787 if (h->root.type != bfd_link_hash_defined
2788 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2789 {
2790 h->ref_regular = 1;
2791 h->ref_regular_nonweak = 1;
2792 }
45d6a902
AM
2793 else
2794 {
2795 if (h->root.u.def.section->owner != NULL
2796 && (bfd_get_flavour (h->root.u.def.section->owner)
2797 == bfd_target_elf_flavour))
f5385ebf
AM
2798 {
2799 h->ref_regular = 1;
2800 h->ref_regular_nonweak = 1;
2801 }
45d6a902 2802 else
f5385ebf 2803 h->def_regular = 1;
45d6a902
AM
2804 }
2805
2806 if (h->dynindx == -1
f5385ebf
AM
2807 && (h->def_dynamic
2808 || h->ref_dynamic))
45d6a902 2809 {
c152c796 2810 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2811 {
2812 eif->failed = TRUE;
2813 return FALSE;
2814 }
2815 }
2816 }
2817 else
2818 {
f5385ebf 2819 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2820 was first seen in a non-ELF file. Fortunately, if the symbol
2821 was first seen in an ELF file, we're probably OK unless the
2822 symbol was defined in a non-ELF file. Catch that case here.
2823 FIXME: We're still in trouble if the symbol was first seen in
2824 a dynamic object, and then later in a non-ELF regular object. */
2825 if ((h->root.type == bfd_link_hash_defined
2826 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2827 && !h->def_regular
45d6a902
AM
2828 && (h->root.u.def.section->owner != NULL
2829 ? (bfd_get_flavour (h->root.u.def.section->owner)
2830 != bfd_target_elf_flavour)
2831 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2832 && !h->def_dynamic)))
2833 h->def_regular = 1;
45d6a902
AM
2834 }
2835
508c3946 2836 /* Backend specific symbol fixup. */
33774f08
AM
2837 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2838 if (bed->elf_backend_fixup_symbol
2839 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2840 return FALSE;
508c3946 2841
45d6a902
AM
2842 /* If this is a final link, and the symbol was defined as a common
2843 symbol in a regular object file, and there was no definition in
2844 any dynamic object, then the linker will have allocated space for
f5385ebf 2845 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2846 flag will not have been set. */
2847 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2848 && !h->def_regular
2849 && h->ref_regular
2850 && !h->def_dynamic
96f29d96 2851 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2852 h->def_regular = 1;
45d6a902 2853
af0bfb9c
AM
2854 /* Symbols defined in discarded sections shouldn't be dynamic. */
2855 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2856 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2857
4deb8f71
L
2858 /* If a weak undefined symbol has non-default visibility, we also
2859 hide it from the dynamic linker. */
af0bfb9c
AM
2860 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2861 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2862 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2863
2864 /* A hidden versioned symbol in executable should be forced local if
2865 it is is locally defined, not referenced by shared library and not
2866 exported. */
2867 else if (bfd_link_executable (eif->info)
2868 && h->versioned == versioned_hidden
2869 && !eif->info->export_dynamic
2870 && !h->dynamic
2871 && !h->ref_dynamic
2872 && h->def_regular)
2873 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2874
45d6a902
AM
2875 /* If -Bsymbolic was used (which means to bind references to global
2876 symbols to the definition within the shared object), and this
2877 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2878 need a PLT entry. Likewise, if the symbol has non-default
2879 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2880 will force it local. */
4deb8f71
L
2881 else if (h->needs_plt
2882 && bfd_link_pic (eif->info)
2883 && is_elf_hash_table (eif->info->hash)
2884 && (SYMBOLIC_BIND (eif->info, h)
2885 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2886 && h->def_regular)
45d6a902 2887 {
45d6a902
AM
2888 bfd_boolean force_local;
2889
45d6a902
AM
2890 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2891 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2892 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2893 }
2894
45d6a902
AM
2895 /* If this is a weak defined symbol in a dynamic object, and we know
2896 the real definition in the dynamic object, copy interesting flags
2897 over to the real definition. */
60d67dc8 2898 if (h->is_weakalias)
45d6a902 2899 {
60d67dc8
AM
2900 struct elf_link_hash_entry *def = weakdef (h);
2901
45d6a902
AM
2902 /* If the real definition is defined by a regular object file,
2903 don't do anything special. See the longer description in
2904 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2905 if (def->def_regular)
2906 {
2907 h = def;
2908 while ((h = h->u.alias) != def)
2909 h->is_weakalias = 0;
2910 }
45d6a902 2911 else
a26587ba 2912 {
4e6b54a6
AM
2913 while (h->root.type == bfd_link_hash_indirect)
2914 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2915 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2916 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2917 BFD_ASSERT (def->def_dynamic);
2918 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2919 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2920 }
45d6a902
AM
2921 }
2922
2923 return TRUE;
2924}
2925
2926/* Make the backend pick a good value for a dynamic symbol. This is
2927 called via elf_link_hash_traverse, and also calls itself
2928 recursively. */
2929
28caa186 2930static bfd_boolean
268b6b39 2931_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2932{
a50b1753 2933 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2934 struct elf_link_hash_table *htab;
9c5bfbb7 2935 const struct elf_backend_data *bed;
45d6a902 2936
0eddce27 2937 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2938 return FALSE;
2939
45d6a902
AM
2940 /* Ignore indirect symbols. These are added by the versioning code. */
2941 if (h->root.type == bfd_link_hash_indirect)
2942 return TRUE;
2943
2944 /* Fix the symbol flags. */
2945 if (! _bfd_elf_fix_symbol_flags (h, eif))
2946 return FALSE;
2947
559192d8
AM
2948 htab = elf_hash_table (eif->info);
2949 bed = get_elf_backend_data (htab->dynobj);
2950
954b63d4
AM
2951 if (h->root.type == bfd_link_hash_undefweak)
2952 {
2953 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2954 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2955 else if (eif->info->dynamic_undefined_weak > 0
2956 && h->ref_regular
2957 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2958 && !bfd_hide_sym_by_version (eif->info->version_info,
2959 h->root.root.string))
2960 {
2961 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2962 {
2963 eif->failed = TRUE;
2964 return FALSE;
2965 }
2966 }
2967 }
2968
45d6a902
AM
2969 /* If this symbol does not require a PLT entry, and it is not
2970 defined by a dynamic object, or is not referenced by a regular
2971 object, ignore it. We do have to handle a weak defined symbol,
2972 even if no regular object refers to it, if we decided to add it
2973 to the dynamic symbol table. FIXME: Do we normally need to worry
2974 about symbols which are defined by one dynamic object and
2975 referenced by another one? */
f5385ebf 2976 if (!h->needs_plt
91e21fb7 2977 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2978 && (h->def_regular
2979 || !h->def_dynamic
2980 || (!h->ref_regular
60d67dc8 2981 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2982 {
a6aa5195 2983 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2984 return TRUE;
2985 }
2986
2987 /* If we've already adjusted this symbol, don't do it again. This
2988 can happen via a recursive call. */
f5385ebf 2989 if (h->dynamic_adjusted)
45d6a902
AM
2990 return TRUE;
2991
2992 /* Don't look at this symbol again. Note that we must set this
2993 after checking the above conditions, because we may look at a
2994 symbol once, decide not to do anything, and then get called
2995 recursively later after REF_REGULAR is set below. */
f5385ebf 2996 h->dynamic_adjusted = 1;
45d6a902
AM
2997
2998 /* If this is a weak definition, and we know a real definition, and
2999 the real symbol is not itself defined by a regular object file,
3000 then get a good value for the real definition. We handle the
3001 real symbol first, for the convenience of the backend routine.
3002
3003 Note that there is a confusing case here. If the real definition
3004 is defined by a regular object file, we don't get the real symbol
3005 from the dynamic object, but we do get the weak symbol. If the
3006 processor backend uses a COPY reloc, then if some routine in the
3007 dynamic object changes the real symbol, we will not see that
3008 change in the corresponding weak symbol. This is the way other
3009 ELF linkers work as well, and seems to be a result of the shared
3010 library model.
3011
3012 I will clarify this issue. Most SVR4 shared libraries define the
3013 variable _timezone and define timezone as a weak synonym. The
3014 tzset call changes _timezone. If you write
3015 extern int timezone;
3016 int _timezone = 5;
3017 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3018 you might expect that, since timezone is a synonym for _timezone,
3019 the same number will print both times. However, if the processor
3020 backend uses a COPY reloc, then actually timezone will be copied
3021 into your process image, and, since you define _timezone
3022 yourself, _timezone will not. Thus timezone and _timezone will
3023 wind up at different memory locations. The tzset call will set
3024 _timezone, leaving timezone unchanged. */
3025
60d67dc8 3026 if (h->is_weakalias)
45d6a902 3027 {
60d67dc8
AM
3028 struct elf_link_hash_entry *def = weakdef (h);
3029
ec24dc88 3030 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3031 the alias by a regular object file via the weak symbol H. */
3032 def->ref_regular = 1;
45d6a902 3033
ec24dc88 3034 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3035 the strong alias before H by recursively calling ourselves. */
3036 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3037 return FALSE;
3038 }
3039
3040 /* If a symbol has no type and no size and does not require a PLT
3041 entry, then we are probably about to do the wrong thing here: we
3042 are probably going to create a COPY reloc for an empty object.
3043 This case can arise when a shared object is built with assembly
3044 code, and the assembly code fails to set the symbol type. */
3045 if (h->size == 0
3046 && h->type == STT_NOTYPE
f5385ebf 3047 && !h->needs_plt)
4eca0228 3048 _bfd_error_handler
45d6a902
AM
3049 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3050 h->root.root.string);
3051
45d6a902
AM
3052 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3053 {
3054 eif->failed = TRUE;
3055 return FALSE;
3056 }
3057
3058 return TRUE;
3059}
3060
027297b7
L
3061/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3062 DYNBSS. */
3063
3064bfd_boolean
6cabe1ea
AM
3065_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3066 struct elf_link_hash_entry *h,
027297b7
L
3067 asection *dynbss)
3068{
91ac5911 3069 unsigned int power_of_two;
027297b7
L
3070 bfd_vma mask;
3071 asection *sec = h->root.u.def.section;
3072
de194d85 3073 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3074 requirement of symbols defined in the section. Since we don't
3075 know the symbol alignment requirement, we start with the
3076 maximum alignment and check low bits of the symbol address
3077 for the minimum alignment. */
3078 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3079 mask = ((bfd_vma) 1 << power_of_two) - 1;
3080 while ((h->root.u.def.value & mask) != 0)
3081 {
3082 mask >>= 1;
3083 --power_of_two;
3084 }
027297b7 3085
91ac5911
L
3086 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3087 dynbss))
027297b7
L
3088 {
3089 /* Adjust the section alignment if needed. */
3090 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3091 power_of_two))
027297b7
L
3092 return FALSE;
3093 }
3094
91ac5911 3095 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3096 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3097
3098 /* Define the symbol as being at this point in DYNBSS. */
3099 h->root.u.def.section = dynbss;
3100 h->root.u.def.value = dynbss->size;
3101
3102 /* Increment the size of DYNBSS to make room for the symbol. */
3103 dynbss->size += h->size;
3104
f7483970
L
3105 /* No error if extern_protected_data is true. */
3106 if (h->protected_def
889c2a67
L
3107 && (!info->extern_protected_data
3108 || (info->extern_protected_data < 0
3109 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3110 info->callbacks->einfo
c1c8c1ef 3111 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3112 h->root.root.string);
6cabe1ea 3113
027297b7
L
3114 return TRUE;
3115}
3116
45d6a902
AM
3117/* Adjust all external symbols pointing into SEC_MERGE sections
3118 to reflect the object merging within the sections. */
3119
28caa186 3120static bfd_boolean
268b6b39 3121_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3122{
3123 asection *sec;
3124
45d6a902
AM
3125 if ((h->root.type == bfd_link_hash_defined
3126 || h->root.type == bfd_link_hash_defweak)
3127 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3128 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3129 {
a50b1753 3130 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3131
3132 h->root.u.def.value =
3133 _bfd_merged_section_offset (output_bfd,
3134 &h->root.u.def.section,
3135 elf_section_data (sec)->sec_info,
753731ee 3136 h->root.u.def.value);
45d6a902
AM
3137 }
3138
3139 return TRUE;
3140}
986a241f
RH
3141
3142/* Returns false if the symbol referred to by H should be considered
3143 to resolve local to the current module, and true if it should be
3144 considered to bind dynamically. */
3145
3146bfd_boolean
268b6b39
AM
3147_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3148 struct bfd_link_info *info,
89a2ee5a 3149 bfd_boolean not_local_protected)
986a241f
RH
3150{
3151 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3152 const struct elf_backend_data *bed;
3153 struct elf_link_hash_table *hash_table;
986a241f
RH
3154
3155 if (h == NULL)
3156 return FALSE;
3157
3158 while (h->root.type == bfd_link_hash_indirect
3159 || h->root.type == bfd_link_hash_warning)
3160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3161
3162 /* If it was forced local, then clearly it's not dynamic. */
3163 if (h->dynindx == -1)
3164 return FALSE;
f5385ebf 3165 if (h->forced_local)
986a241f
RH
3166 return FALSE;
3167
3168 /* Identify the cases where name binding rules say that a
3169 visible symbol resolves locally. */
0e1862bb
L
3170 binding_stays_local_p = (bfd_link_executable (info)
3171 || SYMBOLIC_BIND (info, h));
986a241f
RH
3172
3173 switch (ELF_ST_VISIBILITY (h->other))
3174 {
3175 case STV_INTERNAL:
3176 case STV_HIDDEN:
3177 return FALSE;
3178
3179 case STV_PROTECTED:
fcb93ecf
PB
3180 hash_table = elf_hash_table (info);
3181 if (!is_elf_hash_table (hash_table))
3182 return FALSE;
3183
3184 bed = get_elf_backend_data (hash_table->dynobj);
3185
986a241f
RH
3186 /* Proper resolution for function pointer equality may require
3187 that these symbols perhaps be resolved dynamically, even though
3188 we should be resolving them to the current module. */
89a2ee5a 3189 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3190 binding_stays_local_p = TRUE;
3191 break;
3192
3193 default:
986a241f
RH
3194 break;
3195 }
3196
aa37626c 3197 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3198 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3199 return TRUE;
3200
986a241f
RH
3201 /* Otherwise, the symbol is dynamic if binding rules don't tell
3202 us that it remains local. */
3203 return !binding_stays_local_p;
3204}
f6c52c13
AM
3205
3206/* Return true if the symbol referred to by H should be considered
3207 to resolve local to the current module, and false otherwise. Differs
3208 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3209 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3210 for the place where dynindx == -1 is tested. If that test is true,
3211 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3212 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3213 defined symbols.
89a2ee5a
AM
3214 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3215 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3216 treatment of undefined weak symbols. For those that do not make
3217 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3218
3219bfd_boolean
268b6b39
AM
3220_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3221 struct bfd_link_info *info,
3222 bfd_boolean local_protected)
f6c52c13 3223{
fcb93ecf
PB
3224 const struct elf_backend_data *bed;
3225 struct elf_link_hash_table *hash_table;
3226
f6c52c13
AM
3227 /* If it's a local sym, of course we resolve locally. */
3228 if (h == NULL)
3229 return TRUE;
3230
d95edcac
L
3231 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3232 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3233 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3234 return TRUE;
3235
0fad2956
MR
3236 /* Forced local symbols resolve locally. */
3237 if (h->forced_local)
3238 return TRUE;
3239
7e2294f9
AO
3240 /* Common symbols that become definitions don't get the DEF_REGULAR
3241 flag set, so test it first, and don't bail out. */
3242 if (ELF_COMMON_DEF_P (h))
3243 /* Do nothing. */;
f6c52c13 3244 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3245 resolve locally. The sym is either undefined or dynamic. */
3246 else if (!h->def_regular)
f6c52c13
AM
3247 return FALSE;
3248
0fad2956 3249 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3250 if (h->dynindx == -1)
3251 return TRUE;
3252
3253 /* At this point, we know the symbol is defined and dynamic. In an
3254 executable it must resolve locally, likewise when building symbolic
3255 shared libraries. */
0e1862bb 3256 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3257 return TRUE;
3258
3259 /* Now deal with defined dynamic symbols in shared libraries. Ones
3260 with default visibility might not resolve locally. */
3261 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3262 return FALSE;
3263
fcb93ecf
PB
3264 hash_table = elf_hash_table (info);
3265 if (!is_elf_hash_table (hash_table))
3266 return TRUE;
3267
3268 bed = get_elf_backend_data (hash_table->dynobj);
3269
f7483970
L
3270 /* If extern_protected_data is false, STV_PROTECTED non-function
3271 symbols are local. */
889c2a67
L
3272 if ((!info->extern_protected_data
3273 || (info->extern_protected_data < 0
3274 && !bed->extern_protected_data))
3275 && !bed->is_function_type (h->type))
1c16dfa5
L
3276 return TRUE;
3277
f6c52c13 3278 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3279 symbols be treated as dynamic symbols. If the address of a
3280 function not defined in an executable is set to that function's
3281 plt entry in the executable, then the address of the function in
3282 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3283 return local_protected;
3284}
e1918d23
AM
3285
3286/* Caches some TLS segment info, and ensures that the TLS segment vma is
3287 aligned. Returns the first TLS output section. */
3288
3289struct bfd_section *
3290_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3291{
3292 struct bfd_section *sec, *tls;
3293 unsigned int align = 0;
3294
3295 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3296 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3297 break;
3298 tls = sec;
3299
3300 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3301 if (sec->alignment_power > align)
3302 align = sec->alignment_power;
3303
3304 elf_hash_table (info)->tls_sec = tls;
3305
3306 /* Ensure the alignment of the first section is the largest alignment,
3307 so that the tls segment starts aligned. */
3308 if (tls != NULL)
3309 tls->alignment_power = align;
3310
3311 return tls;
3312}
0ad989f9
L
3313
3314/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3315static bfd_boolean
3316is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3317 Elf_Internal_Sym *sym)
3318{
a4d8e49b
L
3319 const struct elf_backend_data *bed;
3320
0ad989f9
L
3321 /* Local symbols do not count, but target specific ones might. */
3322 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3323 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3324 return FALSE;
3325
fcb93ecf 3326 bed = get_elf_backend_data (abfd);
0ad989f9 3327 /* Function symbols do not count. */
fcb93ecf 3328 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3329 return FALSE;
3330
3331 /* If the section is undefined, then so is the symbol. */
3332 if (sym->st_shndx == SHN_UNDEF)
3333 return FALSE;
3334
3335 /* If the symbol is defined in the common section, then
3336 it is a common definition and so does not count. */
a4d8e49b 3337 if (bed->common_definition (sym))
0ad989f9
L
3338 return FALSE;
3339
3340 /* If the symbol is in a target specific section then we
3341 must rely upon the backend to tell us what it is. */
3342 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3343 /* FIXME - this function is not coded yet:
3344
3345 return _bfd_is_global_symbol_definition (abfd, sym);
3346
3347 Instead for now assume that the definition is not global,
3348 Even if this is wrong, at least the linker will behave
3349 in the same way that it used to do. */
3350 return FALSE;
3351
3352 return TRUE;
3353}
3354
3355/* Search the symbol table of the archive element of the archive ABFD
3356 whose archive map contains a mention of SYMDEF, and determine if
3357 the symbol is defined in this element. */
3358static bfd_boolean
3359elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3360{
3361 Elf_Internal_Shdr * hdr;
ef53be89
AM
3362 size_t symcount;
3363 size_t extsymcount;
3364 size_t extsymoff;
0ad989f9
L
3365 Elf_Internal_Sym *isymbuf;
3366 Elf_Internal_Sym *isym;
3367 Elf_Internal_Sym *isymend;
3368 bfd_boolean result;
3369
3370 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3371 if (abfd == NULL)
3372 return FALSE;
3373
3374 if (! bfd_check_format (abfd, bfd_object))
3375 return FALSE;
3376
7dc3990e
L
3377 /* Select the appropriate symbol table. If we don't know if the
3378 object file is an IR object, give linker LTO plugin a chance to
3379 get the correct symbol table. */
3380 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3381#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3382 || (abfd->plugin_format == bfd_plugin_unknown
3383 && bfd_link_plugin_object_p (abfd))
3384#endif
3385 )
3386 {
3387 /* Use the IR symbol table if the object has been claimed by
3388 plugin. */
3389 abfd = abfd->plugin_dummy_bfd;
3390 hdr = &elf_tdata (abfd)->symtab_hdr;
3391 }
3392 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3393 hdr = &elf_tdata (abfd)->symtab_hdr;
3394 else
3395 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3396
3397 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3398
3399 /* The sh_info field of the symtab header tells us where the
3400 external symbols start. We don't care about the local symbols. */
3401 if (elf_bad_symtab (abfd))
3402 {
3403 extsymcount = symcount;
3404 extsymoff = 0;
3405 }
3406 else
3407 {
3408 extsymcount = symcount - hdr->sh_info;
3409 extsymoff = hdr->sh_info;
3410 }
3411
3412 if (extsymcount == 0)
3413 return FALSE;
3414
3415 /* Read in the symbol table. */
3416 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3417 NULL, NULL, NULL);
3418 if (isymbuf == NULL)
3419 return FALSE;
3420
3421 /* Scan the symbol table looking for SYMDEF. */
3422 result = FALSE;
3423 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3424 {
3425 const char *name;
3426
3427 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3428 isym->st_name);
3429 if (name == NULL)
3430 break;
3431
3432 if (strcmp (name, symdef->name) == 0)
3433 {
3434 result = is_global_data_symbol_definition (abfd, isym);
3435 break;
3436 }
3437 }
3438
3439 free (isymbuf);
3440
3441 return result;
3442}
3443\f
5a580b3a
AM
3444/* Add an entry to the .dynamic table. */
3445
3446bfd_boolean
3447_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3448 bfd_vma tag,
3449 bfd_vma val)
3450{
3451 struct elf_link_hash_table *hash_table;
3452 const struct elf_backend_data *bed;
3453 asection *s;
3454 bfd_size_type newsize;
3455 bfd_byte *newcontents;
3456 Elf_Internal_Dyn dyn;
3457
3458 hash_table = elf_hash_table (info);
3459 if (! is_elf_hash_table (hash_table))
3460 return FALSE;
3461
7f923b7f
AM
3462 if (tag == DT_RELA || tag == DT_REL)
3463 hash_table->dynamic_relocs = TRUE;
3464
5a580b3a 3465 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3466 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3467 BFD_ASSERT (s != NULL);
3468
eea6121a 3469 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3470 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3471 if (newcontents == NULL)
3472 return FALSE;
3473
3474 dyn.d_tag = tag;
3475 dyn.d_un.d_val = val;
eea6121a 3476 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3477
eea6121a 3478 s->size = newsize;
5a580b3a
AM
3479 s->contents = newcontents;
3480
3481 return TRUE;
3482}
3483
3484/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3485 otherwise just check whether one already exists. Returns -1 on error,
3486 1 if a DT_NEEDED tag already exists, and 0 on success. */
3487
4ad4eba5 3488static int
7e9f0867
AM
3489elf_add_dt_needed_tag (bfd *abfd,
3490 struct bfd_link_info *info,
4ad4eba5
AM
3491 const char *soname,
3492 bfd_boolean do_it)
5a580b3a
AM
3493{
3494 struct elf_link_hash_table *hash_table;
ef53be89 3495 size_t strindex;
5a580b3a 3496
7e9f0867
AM
3497 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3498 return -1;
3499
5a580b3a 3500 hash_table = elf_hash_table (info);
5a580b3a 3501 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3502 if (strindex == (size_t) -1)
5a580b3a
AM
3503 return -1;
3504
02be4619 3505 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3506 {
3507 asection *sdyn;
3508 const struct elf_backend_data *bed;
3509 bfd_byte *extdyn;
3510
3511 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3512 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3513 if (sdyn != NULL)
3514 for (extdyn = sdyn->contents;
3515 extdyn < sdyn->contents + sdyn->size;
3516 extdyn += bed->s->sizeof_dyn)
3517 {
3518 Elf_Internal_Dyn dyn;
5a580b3a 3519
7e9f0867
AM
3520 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3521 if (dyn.d_tag == DT_NEEDED
3522 && dyn.d_un.d_val == strindex)
3523 {
3524 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3525 return 1;
3526 }
3527 }
5a580b3a
AM
3528 }
3529
3530 if (do_it)
3531 {
7e9f0867
AM
3532 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3533 return -1;
3534
5a580b3a
AM
3535 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3536 return -1;
3537 }
3538 else
3539 /* We were just checking for existence of the tag. */
3540 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3541
3542 return 0;
3543}
3544
7b15fa7a
AM
3545/* Return true if SONAME is on the needed list between NEEDED and STOP
3546 (or the end of list if STOP is NULL), and needed by a library that
3547 will be loaded. */
3548
010e5ae2 3549static bfd_boolean
7b15fa7a
AM
3550on_needed_list (const char *soname,
3551 struct bfd_link_needed_list *needed,
3552 struct bfd_link_needed_list *stop)
010e5ae2 3553{
7b15fa7a
AM
3554 struct bfd_link_needed_list *look;
3555 for (look = needed; look != stop; look = look->next)
3556 if (strcmp (soname, look->name) == 0
3557 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3558 /* If needed by a library that itself is not directly
3559 needed, recursively check whether that library is
3560 indirectly needed. Since we add DT_NEEDED entries to
3561 the end of the list, library dependencies appear after
3562 the library. Therefore search prior to the current
3563 LOOK, preventing possible infinite recursion. */
3564 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3565 return TRUE;
3566
3567 return FALSE;
3568}
3569
14160578 3570/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3571static int
3572elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3573{
3574 const struct elf_link_hash_entry *h1;
3575 const struct elf_link_hash_entry *h2;
10b7e05b 3576 bfd_signed_vma vdiff;
5a580b3a
AM
3577
3578 h1 = *(const struct elf_link_hash_entry **) arg1;
3579 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3580 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3581 if (vdiff != 0)
3582 return vdiff > 0 ? 1 : -1;
3583 else
3584 {
d3435ae8 3585 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3586 if (sdiff != 0)
3587 return sdiff > 0 ? 1 : -1;
3588 }
14160578
AM
3589 vdiff = h1->size - h2->size;
3590 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3591}
4ad4eba5 3592
5a580b3a
AM
3593/* This function is used to adjust offsets into .dynstr for
3594 dynamic symbols. This is called via elf_link_hash_traverse. */
3595
3596static bfd_boolean
3597elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3598{
a50b1753 3599 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3600
5a580b3a
AM
3601 if (h->dynindx != -1)
3602 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3603 return TRUE;
3604}
3605
3606/* Assign string offsets in .dynstr, update all structures referencing
3607 them. */
3608
4ad4eba5
AM
3609static bfd_boolean
3610elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3611{
3612 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3613 struct elf_link_local_dynamic_entry *entry;
3614 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3615 bfd *dynobj = hash_table->dynobj;
3616 asection *sdyn;
3617 bfd_size_type size;
3618 const struct elf_backend_data *bed;
3619 bfd_byte *extdyn;
3620
3621 _bfd_elf_strtab_finalize (dynstr);
3622 size = _bfd_elf_strtab_size (dynstr);
3623
3624 bed = get_elf_backend_data (dynobj);
3d4d4302 3625 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3626 BFD_ASSERT (sdyn != NULL);
3627
3628 /* Update all .dynamic entries referencing .dynstr strings. */
3629 for (extdyn = sdyn->contents;
eea6121a 3630 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3631 extdyn += bed->s->sizeof_dyn)
3632 {
3633 Elf_Internal_Dyn dyn;
3634
3635 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3636 switch (dyn.d_tag)
3637 {
3638 case DT_STRSZ:
3639 dyn.d_un.d_val = size;
3640 break;
3641 case DT_NEEDED:
3642 case DT_SONAME:
3643 case DT_RPATH:
3644 case DT_RUNPATH:
3645 case DT_FILTER:
3646 case DT_AUXILIARY:
7ee314fa
AM
3647 case DT_AUDIT:
3648 case DT_DEPAUDIT:
5a580b3a
AM
3649 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3650 break;
3651 default:
3652 continue;
3653 }
3654 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3655 }
3656
3657 /* Now update local dynamic symbols. */
3658 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3659 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3660 entry->isym.st_name);
3661
3662 /* And the rest of dynamic symbols. */
3663 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3664
3665 /* Adjust version definitions. */
3666 if (elf_tdata (output_bfd)->cverdefs)
3667 {
3668 asection *s;
3669 bfd_byte *p;
ef53be89 3670 size_t i;
5a580b3a
AM
3671 Elf_Internal_Verdef def;
3672 Elf_Internal_Verdaux defaux;
3673
3d4d4302 3674 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3675 p = s->contents;
3676 do
3677 {
3678 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3679 &def);
3680 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3681 if (def.vd_aux != sizeof (Elf_External_Verdef))
3682 continue;
5a580b3a
AM
3683 for (i = 0; i < def.vd_cnt; ++i)
3684 {
3685 _bfd_elf_swap_verdaux_in (output_bfd,
3686 (Elf_External_Verdaux *) p, &defaux);
3687 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3688 defaux.vda_name);
3689 _bfd_elf_swap_verdaux_out (output_bfd,
3690 &defaux, (Elf_External_Verdaux *) p);
3691 p += sizeof (Elf_External_Verdaux);
3692 }
3693 }
3694 while (def.vd_next);
3695 }
3696
3697 /* Adjust version references. */
3698 if (elf_tdata (output_bfd)->verref)
3699 {
3700 asection *s;
3701 bfd_byte *p;
ef53be89 3702 size_t i;
5a580b3a
AM
3703 Elf_Internal_Verneed need;
3704 Elf_Internal_Vernaux needaux;
3705
3d4d4302 3706 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3707 p = s->contents;
3708 do
3709 {
3710 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3711 &need);
3712 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3713 _bfd_elf_swap_verneed_out (output_bfd, &need,
3714 (Elf_External_Verneed *) p);
3715 p += sizeof (Elf_External_Verneed);
3716 for (i = 0; i < need.vn_cnt; ++i)
3717 {
3718 _bfd_elf_swap_vernaux_in (output_bfd,
3719 (Elf_External_Vernaux *) p, &needaux);
3720 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3721 needaux.vna_name);
3722 _bfd_elf_swap_vernaux_out (output_bfd,
3723 &needaux,
3724 (Elf_External_Vernaux *) p);
3725 p += sizeof (Elf_External_Vernaux);
3726 }
3727 }
3728 while (need.vn_next);
3729 }
3730
3731 return TRUE;
3732}
3733\f
13285a1b
AM
3734/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3735 The default is to only match when the INPUT and OUTPUT are exactly
3736 the same target. */
3737
3738bfd_boolean
3739_bfd_elf_default_relocs_compatible (const bfd_target *input,
3740 const bfd_target *output)
3741{
3742 return input == output;
3743}
3744
3745/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3746 This version is used when different targets for the same architecture
3747 are virtually identical. */
3748
3749bfd_boolean
3750_bfd_elf_relocs_compatible (const bfd_target *input,
3751 const bfd_target *output)
3752{
3753 const struct elf_backend_data *obed, *ibed;
3754
3755 if (input == output)
3756 return TRUE;
3757
3758 ibed = xvec_get_elf_backend_data (input);
3759 obed = xvec_get_elf_backend_data (output);
3760
3761 if (ibed->arch != obed->arch)
3762 return FALSE;
3763
3764 /* If both backends are using this function, deem them compatible. */
3765 return ibed->relocs_compatible == obed->relocs_compatible;
3766}
3767
e5034e59
AM
3768/* Make a special call to the linker "notice" function to tell it that
3769 we are about to handle an as-needed lib, or have finished
1b786873 3770 processing the lib. */
e5034e59
AM
3771
3772bfd_boolean
3773_bfd_elf_notice_as_needed (bfd *ibfd,
3774 struct bfd_link_info *info,
3775 enum notice_asneeded_action act)
3776{
46135103 3777 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3778}
3779
d9689752
L
3780/* Check relocations an ELF object file. */
3781
3782bfd_boolean
3783_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3784{
3785 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3786 struct elf_link_hash_table *htab = elf_hash_table (info);
3787
3788 /* If this object is the same format as the output object, and it is
3789 not a shared library, then let the backend look through the
3790 relocs.
3791
3792 This is required to build global offset table entries and to
3793 arrange for dynamic relocs. It is not required for the
3794 particular common case of linking non PIC code, even when linking
3795 against shared libraries, but unfortunately there is no way of
3796 knowing whether an object file has been compiled PIC or not.
3797 Looking through the relocs is not particularly time consuming.
3798 The problem is that we must either (1) keep the relocs in memory,
3799 which causes the linker to require additional runtime memory or
3800 (2) read the relocs twice from the input file, which wastes time.
3801 This would be a good case for using mmap.
3802
3803 I have no idea how to handle linking PIC code into a file of a
3804 different format. It probably can't be done. */
3805 if ((abfd->flags & DYNAMIC) == 0
3806 && is_elf_hash_table (htab)
3807 && bed->check_relocs != NULL
3808 && elf_object_id (abfd) == elf_hash_table_id (htab)
3809 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3810 {
3811 asection *o;
3812
3813 for (o = abfd->sections; o != NULL; o = o->next)
3814 {
3815 Elf_Internal_Rela *internal_relocs;
3816 bfd_boolean ok;
3817
5ce03cea 3818 /* Don't check relocations in excluded sections. */
d9689752 3819 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3820 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3821 || o->reloc_count == 0
3822 || ((info->strip == strip_all || info->strip == strip_debugger)
3823 && (o->flags & SEC_DEBUGGING) != 0)
3824 || bfd_is_abs_section (o->output_section))
3825 continue;
3826
3827 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3828 info->keep_memory);
3829 if (internal_relocs == NULL)
3830 return FALSE;
3831
3832 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3833
3834 if (elf_section_data (o)->relocs != internal_relocs)
3835 free (internal_relocs);
3836
3837 if (! ok)
3838 return FALSE;
3839 }
3840 }
3841
3842 return TRUE;
3843}
3844
4ad4eba5
AM
3845/* Add symbols from an ELF object file to the linker hash table. */
3846
3847static bfd_boolean
3848elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3849{
a0c402a5 3850 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3851 Elf_Internal_Shdr *hdr;
ef53be89
AM
3852 size_t symcount;
3853 size_t extsymcount;
3854 size_t extsymoff;
4ad4eba5
AM
3855 struct elf_link_hash_entry **sym_hash;
3856 bfd_boolean dynamic;
3857 Elf_External_Versym *extversym = NULL;
3858 Elf_External_Versym *ever;
3859 struct elf_link_hash_entry *weaks;
3860 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3861 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3862 Elf_Internal_Sym *isymbuf = NULL;
3863 Elf_Internal_Sym *isym;
3864 Elf_Internal_Sym *isymend;
3865 const struct elf_backend_data *bed;
3866 bfd_boolean add_needed;
66eb6687 3867 struct elf_link_hash_table *htab;
4ad4eba5 3868 bfd_size_type amt;
66eb6687 3869 void *alloc_mark = NULL;
4f87808c
AM
3870 struct bfd_hash_entry **old_table = NULL;
3871 unsigned int old_size = 0;
3872 unsigned int old_count = 0;
66eb6687 3873 void *old_tab = NULL;
66eb6687
AM
3874 void *old_ent;
3875 struct bfd_link_hash_entry *old_undefs = NULL;
3876 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3877 void *old_strtab = NULL;
66eb6687 3878 size_t tabsize = 0;
db6a5d5f 3879 asection *s;
29a9f53e 3880 bfd_boolean just_syms;
4ad4eba5 3881
66eb6687 3882 htab = elf_hash_table (info);
4ad4eba5 3883 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3884
3885 if ((abfd->flags & DYNAMIC) == 0)
3886 dynamic = FALSE;
3887 else
3888 {
3889 dynamic = TRUE;
3890
3891 /* You can't use -r against a dynamic object. Also, there's no
3892 hope of using a dynamic object which does not exactly match
3893 the format of the output file. */
0e1862bb 3894 if (bfd_link_relocatable (info)
66eb6687 3895 || !is_elf_hash_table (htab)
f13a99db 3896 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3897 {
0e1862bb 3898 if (bfd_link_relocatable (info))
9a0789ec
NC
3899 bfd_set_error (bfd_error_invalid_operation);
3900 else
3901 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3902 goto error_return;
3903 }
3904 }
3905
a0c402a5
L
3906 ehdr = elf_elfheader (abfd);
3907 if (info->warn_alternate_em
3908 && bed->elf_machine_code != ehdr->e_machine
3909 && ((bed->elf_machine_alt1 != 0
3910 && ehdr->e_machine == bed->elf_machine_alt1)
3911 || (bed->elf_machine_alt2 != 0
3912 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3913 _bfd_error_handler
695344c0 3914 /* xgettext:c-format */
9793eb77 3915 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3916 ehdr->e_machine, abfd, bed->elf_machine_code);
3917
4ad4eba5
AM
3918 /* As a GNU extension, any input sections which are named
3919 .gnu.warning.SYMBOL are treated as warning symbols for the given
3920 symbol. This differs from .gnu.warning sections, which generate
3921 warnings when they are included in an output file. */
dd98f8d2 3922 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3923 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3924 {
db6a5d5f 3925 const char *name;
4ad4eba5 3926
db6a5d5f
AM
3927 name = bfd_get_section_name (abfd, s);
3928 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3929 {
db6a5d5f
AM
3930 char *msg;
3931 bfd_size_type sz;
3932
3933 name += sizeof ".gnu.warning." - 1;
3934
3935 /* If this is a shared object, then look up the symbol
3936 in the hash table. If it is there, and it is already
3937 been defined, then we will not be using the entry
3938 from this shared object, so we don't need to warn.
3939 FIXME: If we see the definition in a regular object
3940 later on, we will warn, but we shouldn't. The only
3941 fix is to keep track of what warnings we are supposed
3942 to emit, and then handle them all at the end of the
3943 link. */
3944 if (dynamic)
4ad4eba5 3945 {
db6a5d5f
AM
3946 struct elf_link_hash_entry *h;
3947
3948 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3949
3950 /* FIXME: What about bfd_link_hash_common? */
3951 if (h != NULL
3952 && (h->root.type == bfd_link_hash_defined
3953 || h->root.type == bfd_link_hash_defweak))
3954 continue;
3955 }
4ad4eba5 3956
db6a5d5f
AM
3957 sz = s->size;
3958 msg = (char *) bfd_alloc (abfd, sz + 1);
3959 if (msg == NULL)
3960 goto error_return;
4ad4eba5 3961
db6a5d5f
AM
3962 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3963 goto error_return;
4ad4eba5 3964
db6a5d5f 3965 msg[sz] = '\0';
4ad4eba5 3966
db6a5d5f
AM
3967 if (! (_bfd_generic_link_add_one_symbol
3968 (info, abfd, name, BSF_WARNING, s, 0, msg,
3969 FALSE, bed->collect, NULL)))
3970 goto error_return;
4ad4eba5 3971
0e1862bb 3972 if (bfd_link_executable (info))
db6a5d5f
AM
3973 {
3974 /* Clobber the section size so that the warning does
3975 not get copied into the output file. */
3976 s->size = 0;
11d2f718 3977
db6a5d5f
AM
3978 /* Also set SEC_EXCLUDE, so that symbols defined in
3979 the warning section don't get copied to the output. */
3980 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3981 }
3982 }
3983 }
3984
29a9f53e
L
3985 just_syms = ((s = abfd->sections) != NULL
3986 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3987
4ad4eba5
AM
3988 add_needed = TRUE;
3989 if (! dynamic)
3990 {
3991 /* If we are creating a shared library, create all the dynamic
3992 sections immediately. We need to attach them to something,
3993 so we attach them to this BFD, provided it is the right
bf89386a
L
3994 format and is not from ld --just-symbols. Always create the
3995 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3996 are no input BFD's of the same format as the output, we can't
3997 make a shared library. */
3998 if (!just_syms
bf89386a 3999 && (bfd_link_pic (info)
9c1d7a08 4000 || (!bfd_link_relocatable (info)
3c5fce9b 4001 && info->nointerp
9c1d7a08 4002 && (info->export_dynamic || info->dynamic)))
66eb6687 4003 && is_elf_hash_table (htab)
f13a99db 4004 && info->output_bfd->xvec == abfd->xvec
66eb6687 4005 && !htab->dynamic_sections_created)
4ad4eba5
AM
4006 {
4007 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4008 goto error_return;
4009 }
4010 }
66eb6687 4011 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4012 goto error_return;
4013 else
4014 {
4ad4eba5 4015 const char *soname = NULL;
7ee314fa 4016 char *audit = NULL;
4ad4eba5 4017 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4018 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4019 int ret;
4020
4021 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4022 ld shouldn't allow it. */
29a9f53e 4023 if (just_syms)
92fd189d 4024 abort ();
4ad4eba5
AM
4025
4026 /* If this dynamic lib was specified on the command line with
4027 --as-needed in effect, then we don't want to add a DT_NEEDED
4028 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4029 in by another lib's DT_NEEDED. When --no-add-needed is used
4030 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4031 any dynamic library in DT_NEEDED tags in the dynamic lib at
4032 all. */
4033 add_needed = (elf_dyn_lib_class (abfd)
4034 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4035 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4036
4037 s = bfd_get_section_by_name (abfd, ".dynamic");
4038 if (s != NULL)
4039 {
4040 bfd_byte *dynbuf;
4041 bfd_byte *extdyn;
cb33740c 4042 unsigned int elfsec;
4ad4eba5
AM
4043 unsigned long shlink;
4044
eea6121a 4045 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4046 {
4047error_free_dyn:
4048 free (dynbuf);
4049 goto error_return;
4050 }
4ad4eba5
AM
4051
4052 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4053 if (elfsec == SHN_BAD)
4ad4eba5
AM
4054 goto error_free_dyn;
4055 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4056
4057 for (extdyn = dynbuf;
eea6121a 4058 extdyn < dynbuf + s->size;
4ad4eba5
AM
4059 extdyn += bed->s->sizeof_dyn)
4060 {
4061 Elf_Internal_Dyn dyn;
4062
4063 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4064 if (dyn.d_tag == DT_SONAME)
4065 {
4066 unsigned int tagv = dyn.d_un.d_val;
4067 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4068 if (soname == NULL)
4069 goto error_free_dyn;
4070 }
4071 if (dyn.d_tag == DT_NEEDED)
4072 {
4073 struct bfd_link_needed_list *n, **pn;
4074 char *fnm, *anm;
4075 unsigned int tagv = dyn.d_un.d_val;
4076
4077 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4078 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4079 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4080 if (n == NULL || fnm == NULL)
4081 goto error_free_dyn;
4082 amt = strlen (fnm) + 1;
a50b1753 4083 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4084 if (anm == NULL)
4085 goto error_free_dyn;
4086 memcpy (anm, fnm, amt);
4087 n->name = anm;
4088 n->by = abfd;
4089 n->next = NULL;
66eb6687 4090 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4091 ;
4092 *pn = n;
4093 }
4094 if (dyn.d_tag == DT_RUNPATH)
4095 {
4096 struct bfd_link_needed_list *n, **pn;
4097 char *fnm, *anm;
4098 unsigned int tagv = dyn.d_un.d_val;
4099
4100 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4101 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4102 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4103 if (n == NULL || fnm == NULL)
4104 goto error_free_dyn;
4105 amt = strlen (fnm) + 1;
a50b1753 4106 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4107 if (anm == NULL)
4108 goto error_free_dyn;
4109 memcpy (anm, fnm, amt);
4110 n->name = anm;
4111 n->by = abfd;
4112 n->next = NULL;
4113 for (pn = & runpath;
4114 *pn != NULL;
4115 pn = &(*pn)->next)
4116 ;
4117 *pn = n;
4118 }
4119 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4120 if (!runpath && dyn.d_tag == DT_RPATH)
4121 {
4122 struct bfd_link_needed_list *n, **pn;
4123 char *fnm, *anm;
4124 unsigned int tagv = dyn.d_un.d_val;
4125
4126 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4127 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4128 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4129 if (n == NULL || fnm == NULL)
4130 goto error_free_dyn;
4131 amt = strlen (fnm) + 1;
a50b1753 4132 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4133 if (anm == NULL)
f8703194 4134 goto error_free_dyn;
4ad4eba5
AM
4135 memcpy (anm, fnm, amt);
4136 n->name = anm;
4137 n->by = abfd;
4138 n->next = NULL;
4139 for (pn = & rpath;
4140 *pn != NULL;
4141 pn = &(*pn)->next)
4142 ;
4143 *pn = n;
4144 }
7ee314fa
AM
4145 if (dyn.d_tag == DT_AUDIT)
4146 {
4147 unsigned int tagv = dyn.d_un.d_val;
4148 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4149 }
4ad4eba5
AM
4150 }
4151
4152 free (dynbuf);
4153 }
4154
4155 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4156 frees all more recently bfd_alloc'd blocks as well. */
4157 if (runpath)
4158 rpath = runpath;
4159
4160 if (rpath)
4161 {
4162 struct bfd_link_needed_list **pn;
66eb6687 4163 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4164 ;
4165 *pn = rpath;
4166 }
4167
9acc85a6
AM
4168 /* If we have a PT_GNU_RELRO program header, mark as read-only
4169 all sections contained fully therein. This makes relro
4170 shared library sections appear as they will at run-time. */
4171 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4172 while (--phdr >= elf_tdata (abfd)->phdr)
4173 if (phdr->p_type == PT_GNU_RELRO)
4174 {
4175 for (s = abfd->sections; s != NULL; s = s->next)
4176 if ((s->flags & SEC_ALLOC) != 0
4177 && s->vma >= phdr->p_vaddr
4178 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4179 s->flags |= SEC_READONLY;
4180 break;
4181 }
4182
4ad4eba5
AM
4183 /* We do not want to include any of the sections in a dynamic
4184 object in the output file. We hack by simply clobbering the
4185 list of sections in the BFD. This could be handled more
4186 cleanly by, say, a new section flag; the existing
4187 SEC_NEVER_LOAD flag is not the one we want, because that one
4188 still implies that the section takes up space in the output
4189 file. */
4190 bfd_section_list_clear (abfd);
4191
4ad4eba5
AM
4192 /* Find the name to use in a DT_NEEDED entry that refers to this
4193 object. If the object has a DT_SONAME entry, we use it.
4194 Otherwise, if the generic linker stuck something in
4195 elf_dt_name, we use that. Otherwise, we just use the file
4196 name. */
4197 if (soname == NULL || *soname == '\0')
4198 {
4199 soname = elf_dt_name (abfd);
4200 if (soname == NULL || *soname == '\0')
4201 soname = bfd_get_filename (abfd);
4202 }
4203
4204 /* Save the SONAME because sometimes the linker emulation code
4205 will need to know it. */
4206 elf_dt_name (abfd) = soname;
4207
7e9f0867 4208 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4209 if (ret < 0)
4210 goto error_return;
4211
4212 /* If we have already included this dynamic object in the
4213 link, just ignore it. There is no reason to include a
4214 particular dynamic object more than once. */
4215 if (ret > 0)
4216 return TRUE;
7ee314fa
AM
4217
4218 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4219 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4220 }
4221
4222 /* If this is a dynamic object, we always link against the .dynsym
4223 symbol table, not the .symtab symbol table. The dynamic linker
4224 will only see the .dynsym symbol table, so there is no reason to
4225 look at .symtab for a dynamic object. */
4226
4227 if (! dynamic || elf_dynsymtab (abfd) == 0)
4228 hdr = &elf_tdata (abfd)->symtab_hdr;
4229 else
4230 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4231
4232 symcount = hdr->sh_size / bed->s->sizeof_sym;
4233
4234 /* The sh_info field of the symtab header tells us where the
4235 external symbols start. We don't care about the local symbols at
4236 this point. */
4237 if (elf_bad_symtab (abfd))
4238 {
4239 extsymcount = symcount;
4240 extsymoff = 0;
4241 }
4242 else
4243 {
4244 extsymcount = symcount - hdr->sh_info;
4245 extsymoff = hdr->sh_info;
4246 }
4247
f45794cb 4248 sym_hash = elf_sym_hashes (abfd);
012b2306 4249 if (extsymcount != 0)
4ad4eba5
AM
4250 {
4251 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4252 NULL, NULL, NULL);
4253 if (isymbuf == NULL)
4254 goto error_return;
4255
4ad4eba5 4256 if (sym_hash == NULL)
012b2306
AM
4257 {
4258 /* We store a pointer to the hash table entry for each
4259 external symbol. */
ef53be89
AM
4260 amt = extsymcount;
4261 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4262 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4263 if (sym_hash == NULL)
4264 goto error_free_sym;
4265 elf_sym_hashes (abfd) = sym_hash;
4266 }
4ad4eba5
AM
4267 }
4268
4269 if (dynamic)
4270 {
4271 /* Read in any version definitions. */
fc0e6df6
PB
4272 if (!_bfd_elf_slurp_version_tables (abfd,
4273 info->default_imported_symver))
4ad4eba5
AM
4274 goto error_free_sym;
4275
4276 /* Read in the symbol versions, but don't bother to convert them
4277 to internal format. */
4278 if (elf_dynversym (abfd) != 0)
4279 {
4280 Elf_Internal_Shdr *versymhdr;
4281
4282 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4283 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4284 if (extversym == NULL)
4285 goto error_free_sym;
4286 amt = versymhdr->sh_size;
4287 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4288 || bfd_bread (extversym, amt, abfd) != amt)
4289 goto error_free_vers;
4290 }
4291 }
4292
66eb6687
AM
4293 /* If we are loading an as-needed shared lib, save the symbol table
4294 state before we start adding symbols. If the lib turns out
4295 to be unneeded, restore the state. */
4296 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4297 {
4298 unsigned int i;
4299 size_t entsize;
4300
4301 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4302 {
4303 struct bfd_hash_entry *p;
2de92251 4304 struct elf_link_hash_entry *h;
66eb6687
AM
4305
4306 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4307 {
4308 h = (struct elf_link_hash_entry *) p;
4309 entsize += htab->root.table.entsize;
4310 if (h->root.type == bfd_link_hash_warning)
4311 entsize += htab->root.table.entsize;
4312 }
66eb6687
AM
4313 }
4314
4315 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4316 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4317 if (old_tab == NULL)
4318 goto error_free_vers;
4319
4320 /* Remember the current objalloc pointer, so that all mem for
4321 symbols added can later be reclaimed. */
4322 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4323 if (alloc_mark == NULL)
4324 goto error_free_vers;
4325
5061a885
AM
4326 /* Make a special call to the linker "notice" function to
4327 tell it that we are about to handle an as-needed lib. */
e5034e59 4328 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4329 goto error_free_vers;
5061a885 4330
f45794cb
AM
4331 /* Clone the symbol table. Remember some pointers into the
4332 symbol table, and dynamic symbol count. */
4333 old_ent = (char *) old_tab + tabsize;
66eb6687 4334 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4335 old_undefs = htab->root.undefs;
4336 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4337 old_table = htab->root.table.table;
4338 old_size = htab->root.table.size;
4339 old_count = htab->root.table.count;
5b677558
AM
4340 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4341 if (old_strtab == NULL)
4342 goto error_free_vers;
66eb6687
AM
4343
4344 for (i = 0; i < htab->root.table.size; i++)
4345 {
4346 struct bfd_hash_entry *p;
2de92251 4347 struct elf_link_hash_entry *h;
66eb6687
AM
4348
4349 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4350 {
4351 memcpy (old_ent, p, htab->root.table.entsize);
4352 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4353 h = (struct elf_link_hash_entry *) p;
4354 if (h->root.type == bfd_link_hash_warning)
4355 {
4356 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4357 old_ent = (char *) old_ent + htab->root.table.entsize;
4358 }
66eb6687
AM
4359 }
4360 }
4361 }
4ad4eba5 4362
66eb6687 4363 weaks = NULL;
4ad4eba5
AM
4364 ever = extversym != NULL ? extversym + extsymoff : NULL;
4365 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4366 isym < isymend;
4367 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4368 {
4369 int bind;
4370 bfd_vma value;
af44c138 4371 asection *sec, *new_sec;
4ad4eba5
AM
4372 flagword flags;
4373 const char *name;
4374 struct elf_link_hash_entry *h;
90c984fc 4375 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4376 bfd_boolean definition;
4377 bfd_boolean size_change_ok;
4378 bfd_boolean type_change_ok;
37a9e49a
L
4379 bfd_boolean new_weak;
4380 bfd_boolean old_weak;
4ad4eba5 4381 bfd_boolean override;
a4d8e49b 4382 bfd_boolean common;
97196564 4383 bfd_boolean discarded;
4ad4eba5
AM
4384 unsigned int old_alignment;
4385 bfd *old_bfd;
6e33951e 4386 bfd_boolean matched;
4ad4eba5
AM
4387
4388 override = FALSE;
4389
4390 flags = BSF_NO_FLAGS;
4391 sec = NULL;
4392 value = isym->st_value;
a4d8e49b 4393 common = bed->common_definition (isym);
2980ccad
L
4394 if (common && info->inhibit_common_definition)
4395 {
4396 /* Treat common symbol as undefined for --no-define-common. */
4397 isym->st_shndx = SHN_UNDEF;
4398 common = FALSE;
4399 }
97196564 4400 discarded = FALSE;
4ad4eba5
AM
4401
4402 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4403 switch (bind)
4ad4eba5 4404 {
3e7a7d11 4405 case STB_LOCAL:
4ad4eba5
AM
4406 /* This should be impossible, since ELF requires that all
4407 global symbols follow all local symbols, and that sh_info
4408 point to the first global symbol. Unfortunately, Irix 5
4409 screws this up. */
4410 continue;
3e7a7d11
NC
4411
4412 case STB_GLOBAL:
a4d8e49b 4413 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4414 flags = BSF_GLOBAL;
3e7a7d11
NC
4415 break;
4416
4417 case STB_WEAK:
4418 flags = BSF_WEAK;
4419 break;
4420
4421 case STB_GNU_UNIQUE:
4422 flags = BSF_GNU_UNIQUE;
4423 break;
4424
4425 default:
4ad4eba5 4426 /* Leave it up to the processor backend. */
3e7a7d11 4427 break;
4ad4eba5
AM
4428 }
4429
4430 if (isym->st_shndx == SHN_UNDEF)
4431 sec = bfd_und_section_ptr;
cb33740c
AM
4432 else if (isym->st_shndx == SHN_ABS)
4433 sec = bfd_abs_section_ptr;
4434 else if (isym->st_shndx == SHN_COMMON)
4435 {
4436 sec = bfd_com_section_ptr;
4437 /* What ELF calls the size we call the value. What ELF
4438 calls the value we call the alignment. */
4439 value = isym->st_size;
4440 }
4441 else
4ad4eba5
AM
4442 {
4443 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4444 if (sec == NULL)
4445 sec = bfd_abs_section_ptr;
dbaa2011 4446 else if (discarded_section (sec))
529fcb95 4447 {
e5d08002
L
4448 /* Symbols from discarded section are undefined. We keep
4449 its visibility. */
529fcb95 4450 sec = bfd_und_section_ptr;
97196564 4451 discarded = TRUE;
529fcb95
PB
4452 isym->st_shndx = SHN_UNDEF;
4453 }
4ad4eba5
AM
4454 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4455 value -= sec->vma;
4456 }
4ad4eba5
AM
4457
4458 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4459 isym->st_name);
4460 if (name == NULL)
4461 goto error_free_vers;
4462
4463 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4464 && (abfd->flags & BFD_PLUGIN) != 0)
4465 {
4466 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4467
4468 if (xc == NULL)
4469 {
4470 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4471 | SEC_EXCLUDE);
4472 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4473 if (xc == NULL)
4474 goto error_free_vers;
4475 }
4476 sec = xc;
4477 }
4478 else if (isym->st_shndx == SHN_COMMON
4479 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4480 && !bfd_link_relocatable (info))
4ad4eba5
AM
4481 {
4482 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4483
4484 if (tcomm == NULL)
4485 {
02d00247
AM
4486 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4487 | SEC_LINKER_CREATED);
4488 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4489 if (tcomm == NULL)
4ad4eba5
AM
4490 goto error_free_vers;
4491 }
4492 sec = tcomm;
4493 }
66eb6687 4494 else if (bed->elf_add_symbol_hook)
4ad4eba5 4495 {
66eb6687
AM
4496 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4497 &sec, &value))
4ad4eba5
AM
4498 goto error_free_vers;
4499
4500 /* The hook function sets the name to NULL if this symbol
4501 should be skipped for some reason. */
4502 if (name == NULL)
4503 continue;
4504 }
4505
4506 /* Sanity check that all possibilities were handled. */
4507 if (sec == NULL)
4508 {
4509 bfd_set_error (bfd_error_bad_value);
4510 goto error_free_vers;
4511 }
4512
191c0c42
AM
4513 /* Silently discard TLS symbols from --just-syms. There's
4514 no way to combine a static TLS block with a new TLS block
4515 for this executable. */
4516 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4517 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4518 continue;
4519
4ad4eba5
AM
4520 if (bfd_is_und_section (sec)
4521 || bfd_is_com_section (sec))
4522 definition = FALSE;
4523 else
4524 definition = TRUE;
4525
4526 size_change_ok = FALSE;
66eb6687 4527 type_change_ok = bed->type_change_ok;
37a9e49a 4528 old_weak = FALSE;
6e33951e 4529 matched = FALSE;
4ad4eba5
AM
4530 old_alignment = 0;
4531 old_bfd = NULL;
af44c138 4532 new_sec = sec;
4ad4eba5 4533
66eb6687 4534 if (is_elf_hash_table (htab))
4ad4eba5
AM
4535 {
4536 Elf_Internal_Versym iver;
4537 unsigned int vernum = 0;
4538 bfd_boolean skip;
4539
fc0e6df6 4540 if (ever == NULL)
4ad4eba5 4541 {
fc0e6df6
PB
4542 if (info->default_imported_symver)
4543 /* Use the default symbol version created earlier. */
4544 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4545 else
4546 iver.vs_vers = 0;
4547 }
4548 else
4549 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4550
4551 vernum = iver.vs_vers & VERSYM_VERSION;
4552
4553 /* If this is a hidden symbol, or if it is not version
4554 1, we append the version name to the symbol name.
cc86ff91
EB
4555 However, we do not modify a non-hidden absolute symbol
4556 if it is not a function, because it might be the version
4557 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4558 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4559 || (vernum > 1
4560 && (!bfd_is_abs_section (sec)
4561 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4562 {
4563 const char *verstr;
4564 size_t namelen, verlen, newlen;
4565 char *newname, *p;
4566
4567 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4568 {
fc0e6df6
PB
4569 if (vernum > elf_tdata (abfd)->cverdefs)
4570 verstr = NULL;
4571 else if (vernum > 1)
4572 verstr =
4573 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4574 else
4575 verstr = "";
4ad4eba5 4576
fc0e6df6 4577 if (verstr == NULL)
4ad4eba5 4578 {
4eca0228 4579 _bfd_error_handler
695344c0 4580 /* xgettext:c-format */
871b3ab2 4581 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4582 abfd, name, vernum,
4583 elf_tdata (abfd)->cverdefs);
4584 bfd_set_error (bfd_error_bad_value);
4585 goto error_free_vers;
4ad4eba5 4586 }
fc0e6df6
PB
4587 }
4588 else
4589 {
4590 /* We cannot simply test for the number of
4591 entries in the VERNEED section since the
4592 numbers for the needed versions do not start
4593 at 0. */
4594 Elf_Internal_Verneed *t;
4595
4596 verstr = NULL;
4597 for (t = elf_tdata (abfd)->verref;
4598 t != NULL;
4599 t = t->vn_nextref)
4ad4eba5 4600 {
fc0e6df6 4601 Elf_Internal_Vernaux *a;
4ad4eba5 4602
fc0e6df6
PB
4603 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4604 {
4605 if (a->vna_other == vernum)
4ad4eba5 4606 {
fc0e6df6
PB
4607 verstr = a->vna_nodename;
4608 break;
4ad4eba5 4609 }
4ad4eba5 4610 }
fc0e6df6
PB
4611 if (a != NULL)
4612 break;
4613 }
4614 if (verstr == NULL)
4615 {
4eca0228 4616 _bfd_error_handler
695344c0 4617 /* xgettext:c-format */
871b3ab2 4618 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4619 abfd, name, vernum);
4620 bfd_set_error (bfd_error_bad_value);
4621 goto error_free_vers;
4ad4eba5 4622 }
4ad4eba5 4623 }
fc0e6df6
PB
4624
4625 namelen = strlen (name);
4626 verlen = strlen (verstr);
4627 newlen = namelen + verlen + 2;
4628 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4629 && isym->st_shndx != SHN_UNDEF)
4630 ++newlen;
4631
a50b1753 4632 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4633 if (newname == NULL)
4634 goto error_free_vers;
4635 memcpy (newname, name, namelen);
4636 p = newname + namelen;
4637 *p++ = ELF_VER_CHR;
4638 /* If this is a defined non-hidden version symbol,
4639 we add another @ to the name. This indicates the
4640 default version of the symbol. */
4641 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4642 && isym->st_shndx != SHN_UNDEF)
4643 *p++ = ELF_VER_CHR;
4644 memcpy (p, verstr, verlen + 1);
4645
4646 name = newname;
4ad4eba5
AM
4647 }
4648
cd3416da
AM
4649 /* If this symbol has default visibility and the user has
4650 requested we not re-export it, then mark it as hidden. */
a0d49154 4651 if (!bfd_is_und_section (sec)
cd3416da 4652 && !dynamic
ce875075 4653 && abfd->no_export
cd3416da
AM
4654 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4655 isym->st_other = (STV_HIDDEN
4656 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4657
4f3fedcf
AM
4658 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4659 sym_hash, &old_bfd, &old_weak,
4660 &old_alignment, &skip, &override,
6e33951e
L
4661 &type_change_ok, &size_change_ok,
4662 &matched))
4ad4eba5
AM
4663 goto error_free_vers;
4664
4665 if (skip)
4666 continue;
4667
6e33951e
L
4668 /* Override a definition only if the new symbol matches the
4669 existing one. */
4670 if (override && matched)
4ad4eba5
AM
4671 definition = FALSE;
4672
4673 h = *sym_hash;
4674 while (h->root.type == bfd_link_hash_indirect
4675 || h->root.type == bfd_link_hash_warning)
4676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4677
4ad4eba5 4678 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4679 && vernum > 1
4680 && definition)
4681 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4682 }
4683
4684 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4685 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4686 (struct bfd_link_hash_entry **) sym_hash)))
4687 goto error_free_vers;
4688
ac98f9e2
L
4689 if ((abfd->flags & DYNAMIC) == 0
4690 && (bfd_get_flavour (info->output_bfd)
4691 == bfd_target_elf_flavour))
4692 {
4693 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4694 elf_tdata (info->output_bfd)->has_gnu_symbols
4695 |= elf_gnu_symbol_ifunc;
4696 if ((flags & BSF_GNU_UNIQUE))
4697 elf_tdata (info->output_bfd)->has_gnu_symbols
4698 |= elf_gnu_symbol_unique;
4699 }
a43942db 4700
4ad4eba5 4701 h = *sym_hash;
90c984fc
L
4702 /* We need to make sure that indirect symbol dynamic flags are
4703 updated. */
4704 hi = h;
4ad4eba5
AM
4705 while (h->root.type == bfd_link_hash_indirect
4706 || h->root.type == bfd_link_hash_warning)
4707 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4708
97196564
L
4709 /* Setting the index to -3 tells elf_link_output_extsym that
4710 this symbol is defined in a discarded section. */
4711 if (discarded)
4712 h->indx = -3;
4713
4ad4eba5
AM
4714 *sym_hash = h;
4715
37a9e49a 4716 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4717 if (dynamic
4718 && definition
37a9e49a 4719 && new_weak
fcb93ecf 4720 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4721 && is_elf_hash_table (htab)
60d67dc8 4722 && h->u.alias == NULL)
4ad4eba5
AM
4723 {
4724 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4725 a dynamic object, using the alias field. Later in this
4726 function we will set the alias field to the correct
4ad4eba5
AM
4727 value. We only put non-function symbols from dynamic
4728 objects on this list, because that happens to be the only
4729 time we need to know the normal symbol corresponding to a
4730 weak symbol, and the information is time consuming to
60d67dc8 4731 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4732 then this symbol was already defined by some previous
4733 dynamic object, and we will be using that previous
4734 definition anyhow. */
4735
60d67dc8 4736 h->u.alias = weaks;
4ad4eba5 4737 weaks = h;
4ad4eba5
AM
4738 }
4739
4740 /* Set the alignment of a common symbol. */
a4d8e49b 4741 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4742 && h->root.type == bfd_link_hash_common)
4743 {
4744 unsigned int align;
4745
a4d8e49b 4746 if (common)
af44c138
L
4747 align = bfd_log2 (isym->st_value);
4748 else
4749 {
4750 /* The new symbol is a common symbol in a shared object.
4751 We need to get the alignment from the section. */
4752 align = new_sec->alignment_power;
4753 }
595213d4 4754 if (align > old_alignment)
4ad4eba5
AM
4755 h->root.u.c.p->alignment_power = align;
4756 else
4757 h->root.u.c.p->alignment_power = old_alignment;
4758 }
4759
66eb6687 4760 if (is_elf_hash_table (htab))
4ad4eba5 4761 {
4f3fedcf
AM
4762 /* Set a flag in the hash table entry indicating the type of
4763 reference or definition we just found. A dynamic symbol
4764 is one which is referenced or defined by both a regular
4765 object and a shared object. */
4766 bfd_boolean dynsym = FALSE;
4767
4768 /* Plugin symbols aren't normal. Don't set def_regular or
4769 ref_regular for them, or make them dynamic. */
4770 if ((abfd->flags & BFD_PLUGIN) != 0)
4771 ;
4772 else if (! dynamic)
4773 {
4774 if (! definition)
4775 {
4776 h->ref_regular = 1;
4777 if (bind != STB_WEAK)
4778 h->ref_regular_nonweak = 1;
4779 }
4780 else
4781 {
4782 h->def_regular = 1;
4783 if (h->def_dynamic)
4784 {
4785 h->def_dynamic = 0;
4786 h->ref_dynamic = 1;
4787 }
4788 }
4789
4790 /* If the indirect symbol has been forced local, don't
4791 make the real symbol dynamic. */
4792 if ((h == hi || !hi->forced_local)
0e1862bb 4793 && (bfd_link_dll (info)
4f3fedcf
AM
4794 || h->def_dynamic
4795 || h->ref_dynamic))
4796 dynsym = TRUE;
4797 }
4798 else
4799 {
4800 if (! definition)
4801 {
4802 h->ref_dynamic = 1;
4803 hi->ref_dynamic = 1;
4804 }
4805 else
4806 {
4807 h->def_dynamic = 1;
4808 hi->def_dynamic = 1;
4809 }
4810
4811 /* If the indirect symbol has been forced local, don't
4812 make the real symbol dynamic. */
4813 if ((h == hi || !hi->forced_local)
4814 && (h->def_regular
4815 || h->ref_regular
60d67dc8
AM
4816 || (h->is_weakalias
4817 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4818 dynsym = TRUE;
4819 }
4820
4821 /* Check to see if we need to add an indirect symbol for
4822 the default name. */
4823 if (definition
4824 || (!override && h->root.type == bfd_link_hash_common))
4825 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4826 sec, value, &old_bfd, &dynsym))
4827 goto error_free_vers;
4ad4eba5
AM
4828
4829 /* Check the alignment when a common symbol is involved. This
4830 can change when a common symbol is overridden by a normal
4831 definition or a common symbol is ignored due to the old
4832 normal definition. We need to make sure the maximum
4833 alignment is maintained. */
a4d8e49b 4834 if ((old_alignment || common)
4ad4eba5
AM
4835 && h->root.type != bfd_link_hash_common)
4836 {
4837 unsigned int common_align;
4838 unsigned int normal_align;
4839 unsigned int symbol_align;
4840 bfd *normal_bfd;
4841 bfd *common_bfd;
4842
3a81e825
AM
4843 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4844 || h->root.type == bfd_link_hash_defweak);
4845
4ad4eba5
AM
4846 symbol_align = ffs (h->root.u.def.value) - 1;
4847 if (h->root.u.def.section->owner != NULL
0616a280
AM
4848 && (h->root.u.def.section->owner->flags
4849 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4850 {
4851 normal_align = h->root.u.def.section->alignment_power;
4852 if (normal_align > symbol_align)
4853 normal_align = symbol_align;
4854 }
4855 else
4856 normal_align = symbol_align;
4857
4858 if (old_alignment)
4859 {
4860 common_align = old_alignment;
4861 common_bfd = old_bfd;
4862 normal_bfd = abfd;
4863 }
4864 else
4865 {
4866 common_align = bfd_log2 (isym->st_value);
4867 common_bfd = abfd;
4868 normal_bfd = old_bfd;
4869 }
4870
4871 if (normal_align < common_align)
d07676f8
NC
4872 {
4873 /* PR binutils/2735 */
4874 if (normal_bfd == NULL)
4eca0228 4875 _bfd_error_handler
695344c0 4876 /* xgettext:c-format */
9793eb77 4877 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4878 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4879 1 << common_align, name, common_bfd,
4880 1 << normal_align, h->root.u.def.section);
d07676f8 4881 else
4eca0228 4882 _bfd_error_handler
695344c0 4883 /* xgettext:c-format */
9793eb77 4884 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4885 " is smaller than %u in %pB"),
c08bb8dd
AM
4886 1 << normal_align, name, normal_bfd,
4887 1 << common_align, common_bfd);
d07676f8 4888 }
4ad4eba5
AM
4889 }
4890
83ad0046 4891 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4892 if (isym->st_size != 0
4893 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4894 && (definition || h->size == 0))
4895 {
83ad0046
L
4896 if (h->size != 0
4897 && h->size != isym->st_size
4898 && ! size_change_ok)
4eca0228 4899 _bfd_error_handler
695344c0 4900 /* xgettext:c-format */
9793eb77 4901 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4902 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4903 name, (uint64_t) h->size, old_bfd,
4904 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4905
4906 h->size = isym->st_size;
4907 }
4908
4909 /* If this is a common symbol, then we always want H->SIZE
4910 to be the size of the common symbol. The code just above
4911 won't fix the size if a common symbol becomes larger. We
4912 don't warn about a size change here, because that is
4f3fedcf 4913 covered by --warn-common. Allow changes between different
fcb93ecf 4914 function types. */
4ad4eba5
AM
4915 if (h->root.type == bfd_link_hash_common)
4916 h->size = h->root.u.c.size;
4917
4918 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4919 && ((definition && !new_weak)
4920 || (old_weak && h->root.type == bfd_link_hash_common)
4921 || h->type == STT_NOTYPE))
4ad4eba5 4922 {
2955ec4c
L
4923 unsigned int type = ELF_ST_TYPE (isym->st_info);
4924
4925 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4926 symbol. */
4927 if (type == STT_GNU_IFUNC
4928 && (abfd->flags & DYNAMIC) != 0)
4929 type = STT_FUNC;
4ad4eba5 4930
2955ec4c
L
4931 if (h->type != type)
4932 {
4933 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4934 /* xgettext:c-format */
4eca0228 4935 _bfd_error_handler
9793eb77 4936 (_("warning: type of symbol `%s' changed"
871b3ab2 4937 " from %d to %d in %pB"),
c08bb8dd 4938 name, h->type, type, abfd);
2955ec4c
L
4939
4940 h->type = type;
4941 }
4ad4eba5
AM
4942 }
4943
54ac0771 4944 /* Merge st_other field. */
b8417128 4945 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4946
c3df8c14 4947 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4948 if (definition
4949 && (sec->flags & SEC_DEBUGGING)
4950 && !bfd_link_relocatable (info))
c3df8c14
AM
4951 dynsym = FALSE;
4952
4f3fedcf
AM
4953 /* Nor should we make plugin symbols dynamic. */
4954 if ((abfd->flags & BFD_PLUGIN) != 0)
4955 dynsym = FALSE;
4956
35fc36a8 4957 if (definition)
35399224
L
4958 {
4959 h->target_internal = isym->st_target_internal;
4960 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4961 }
35fc36a8 4962
4ad4eba5
AM
4963 if (definition && !dynamic)
4964 {
4965 char *p = strchr (name, ELF_VER_CHR);
4966 if (p != NULL && p[1] != ELF_VER_CHR)
4967 {
4968 /* Queue non-default versions so that .symver x, x@FOO
4969 aliases can be checked. */
66eb6687 4970 if (!nondeflt_vers)
4ad4eba5 4971 {
66eb6687
AM
4972 amt = ((isymend - isym + 1)
4973 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4974 nondeflt_vers
4975 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4976 if (!nondeflt_vers)
4977 goto error_free_vers;
4ad4eba5 4978 }
66eb6687 4979 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4980 }
4981 }
4982
4983 if (dynsym && h->dynindx == -1)
4984 {
c152c796 4985 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4986 goto error_free_vers;
60d67dc8
AM
4987 if (h->is_weakalias
4988 && weakdef (h)->dynindx == -1)
4ad4eba5 4989 {
60d67dc8 4990 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
4991 goto error_free_vers;
4992 }
4993 }
1f599d0e 4994 else if (h->dynindx != -1)
4ad4eba5
AM
4995 /* If the symbol already has a dynamic index, but
4996 visibility says it should not be visible, turn it into
4997 a local symbol. */
4998 switch (ELF_ST_VISIBILITY (h->other))
4999 {
5000 case STV_INTERNAL:
5001 case STV_HIDDEN:
5002 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5003 dynsym = FALSE;
5004 break;
5005 }
5006
aef28989
L
5007 /* Don't add DT_NEEDED for references from the dummy bfd nor
5008 for unmatched symbol. */
4ad4eba5 5009 if (!add_needed
aef28989 5010 && matched
4ad4eba5 5011 && definition
010e5ae2 5012 && ((dynsym
ffa9430d 5013 && h->ref_regular_nonweak
4f3fedcf
AM
5014 && (old_bfd == NULL
5015 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5016 || (h->ref_dynamic_nonweak
010e5ae2 5017 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5018 && !on_needed_list (elf_dt_name (abfd),
5019 htab->needed, NULL))))
4ad4eba5
AM
5020 {
5021 int ret;
5022 const char *soname = elf_dt_name (abfd);
5023
16e4ecc0
AM
5024 info->callbacks->minfo ("%!", soname, old_bfd,
5025 h->root.root.string);
5026
4ad4eba5
AM
5027 /* A symbol from a library loaded via DT_NEEDED of some
5028 other library is referenced by a regular object.
e56f61be 5029 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5030 --no-add-needed is used and the reference was not
5031 a weak one. */
4f3fedcf 5032 if (old_bfd != NULL
b918acf9 5033 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5034 {
4eca0228 5035 _bfd_error_handler
695344c0 5036 /* xgettext:c-format */
871b3ab2 5037 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5038 old_bfd, name);
ff5ac77b 5039 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5040 goto error_free_vers;
5041 }
5042
a50b1753 5043 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5044 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5045
4ad4eba5 5046 add_needed = TRUE;
7e9f0867 5047 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5048 if (ret < 0)
5049 goto error_free_vers;
5050
5051 BFD_ASSERT (ret == 0);
5052 }
5053 }
5054 }
5055
a83ef4d1
L
5056 if (info->lto_plugin_active
5057 && !bfd_link_relocatable (info)
5058 && (abfd->flags & BFD_PLUGIN) == 0
5059 && !just_syms
5060 && extsymcount)
5061 {
5062 int r_sym_shift;
5063
5064 if (bed->s->arch_size == 32)
5065 r_sym_shift = 8;
5066 else
5067 r_sym_shift = 32;
5068
5069 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5070 referenced in regular objects so that linker plugin will get
5071 the correct symbol resolution. */
5072
5073 sym_hash = elf_sym_hashes (abfd);
5074 for (s = abfd->sections; s != NULL; s = s->next)
5075 {
5076 Elf_Internal_Rela *internal_relocs;
5077 Elf_Internal_Rela *rel, *relend;
5078
5079 /* Don't check relocations in excluded sections. */
5080 if ((s->flags & SEC_RELOC) == 0
5081 || s->reloc_count == 0
5082 || (s->flags & SEC_EXCLUDE) != 0
5083 || ((info->strip == strip_all
5084 || info->strip == strip_debugger)
5085 && (s->flags & SEC_DEBUGGING) != 0))
5086 continue;
5087
5088 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5089 NULL,
5090 info->keep_memory);
5091 if (internal_relocs == NULL)
5092 goto error_free_vers;
5093
5094 rel = internal_relocs;
5095 relend = rel + s->reloc_count;
5096 for ( ; rel < relend; rel++)
5097 {
5098 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5099 struct elf_link_hash_entry *h;
5100
5101 /* Skip local symbols. */
5102 if (r_symndx < extsymoff)
5103 continue;
5104
5105 h = sym_hash[r_symndx - extsymoff];
5106 if (h != NULL)
5107 h->root.non_ir_ref_regular = 1;
5108 }
5109
5110 if (elf_section_data (s)->relocs != internal_relocs)
5111 free (internal_relocs);
5112 }
5113 }
5114
66eb6687
AM
5115 if (extversym != NULL)
5116 {
5117 free (extversym);
5118 extversym = NULL;
5119 }
5120
5121 if (isymbuf != NULL)
5122 {
5123 free (isymbuf);
5124 isymbuf = NULL;
5125 }
5126
5127 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5128 {
5129 unsigned int i;
5130
5131 /* Restore the symbol table. */
f45794cb
AM
5132 old_ent = (char *) old_tab + tabsize;
5133 memset (elf_sym_hashes (abfd), 0,
5134 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5135 htab->root.table.table = old_table;
5136 htab->root.table.size = old_size;
5137 htab->root.table.count = old_count;
66eb6687 5138 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5139 htab->root.undefs = old_undefs;
5140 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5141 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5142 free (old_strtab);
5143 old_strtab = NULL;
66eb6687
AM
5144 for (i = 0; i < htab->root.table.size; i++)
5145 {
5146 struct bfd_hash_entry *p;
5147 struct elf_link_hash_entry *h;
3e0882af
L
5148 bfd_size_type size;
5149 unsigned int alignment_power;
4070765b 5150 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5151
5152 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5153 {
5154 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5155 if (h->root.type == bfd_link_hash_warning)
5156 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5157
3e0882af
L
5158 /* Preserve the maximum alignment and size for common
5159 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5160 since it can still be loaded at run time by another
3e0882af
L
5161 dynamic lib. */
5162 if (h->root.type == bfd_link_hash_common)
5163 {
5164 size = h->root.u.c.size;
5165 alignment_power = h->root.u.c.p->alignment_power;
5166 }
5167 else
5168 {
5169 size = 0;
5170 alignment_power = 0;
5171 }
4070765b 5172 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5173 will be exported when the dynamic lib becomes needed
5174 in the second pass. */
4070765b 5175 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5176 memcpy (p, old_ent, htab->root.table.entsize);
5177 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5178 h = (struct elf_link_hash_entry *) p;
5179 if (h->root.type == bfd_link_hash_warning)
5180 {
5181 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5182 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5184 }
a4542f1b 5185 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5186 {
5187 if (size > h->root.u.c.size)
5188 h->root.u.c.size = size;
5189 if (alignment_power > h->root.u.c.p->alignment_power)
5190 h->root.u.c.p->alignment_power = alignment_power;
5191 }
4070765b 5192 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5193 }
5194 }
5195
5061a885
AM
5196 /* Make a special call to the linker "notice" function to
5197 tell it that symbols added for crefs may need to be removed. */
e5034e59 5198 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5199 goto error_free_vers;
5061a885 5200
66eb6687
AM
5201 free (old_tab);
5202 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5203 alloc_mark);
5204 if (nondeflt_vers != NULL)
5205 free (nondeflt_vers);
5206 return TRUE;
5207 }
2de92251 5208
66eb6687
AM
5209 if (old_tab != NULL)
5210 {
e5034e59 5211 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5212 goto error_free_vers;
66eb6687
AM
5213 free (old_tab);
5214 old_tab = NULL;
5215 }
5216
c6e8a9a8
L
5217 /* Now that all the symbols from this input file are created, if
5218 not performing a relocatable link, handle .symver foo, foo@BAR
5219 such that any relocs against foo become foo@BAR. */
0e1862bb 5220 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5221 {
ef53be89 5222 size_t cnt, symidx;
4ad4eba5
AM
5223
5224 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5225 {
5226 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5227 char *shortname, *p;
5228
5229 p = strchr (h->root.root.string, ELF_VER_CHR);
5230 if (p == NULL
5231 || (h->root.type != bfd_link_hash_defined
5232 && h->root.type != bfd_link_hash_defweak))
5233 continue;
5234
5235 amt = p - h->root.root.string;
a50b1753 5236 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5237 if (!shortname)
5238 goto error_free_vers;
4ad4eba5
AM
5239 memcpy (shortname, h->root.root.string, amt);
5240 shortname[amt] = '\0';
5241
5242 hi = (struct elf_link_hash_entry *)
66eb6687 5243 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5244 FALSE, FALSE, FALSE);
5245 if (hi != NULL
5246 && hi->root.type == h->root.type
5247 && hi->root.u.def.value == h->root.u.def.value
5248 && hi->root.u.def.section == h->root.u.def.section)
5249 {
5250 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5251 hi->root.type = bfd_link_hash_indirect;
5252 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5253 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5254 sym_hash = elf_sym_hashes (abfd);
5255 if (sym_hash)
5256 for (symidx = 0; symidx < extsymcount; ++symidx)
5257 if (sym_hash[symidx] == hi)
5258 {
5259 sym_hash[symidx] = h;
5260 break;
5261 }
5262 }
5263 free (shortname);
5264 }
5265 free (nondeflt_vers);
5266 nondeflt_vers = NULL;
5267 }
5268
60d67dc8 5269 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5270 symbols we found. The only way to do this is to search all the
5271 symbols. Since we only need the information for non functions in
5272 dynamic objects, that's the only time we actually put anything on
5273 the list WEAKS. We need this information so that if a regular
5274 object refers to a symbol defined weakly in a dynamic object, the
5275 real symbol in the dynamic object is also put in the dynamic
5276 symbols; we also must arrange for both symbols to point to the
5277 same memory location. We could handle the general case of symbol
5278 aliasing, but a general symbol alias can only be generated in
5279 assembler code, handling it correctly would be very time
5280 consuming, and other ELF linkers don't handle general aliasing
5281 either. */
5282 if (weaks != NULL)
5283 {
5284 struct elf_link_hash_entry **hpp;
5285 struct elf_link_hash_entry **hppend;
5286 struct elf_link_hash_entry **sorted_sym_hash;
5287 struct elf_link_hash_entry *h;
5288 size_t sym_count;
5289
5290 /* Since we have to search the whole symbol list for each weak
5291 defined symbol, search time for N weak defined symbols will be
5292 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5293 amt = extsymcount;
5294 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5295 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5296 if (sorted_sym_hash == NULL)
5297 goto error_return;
5298 sym_hash = sorted_sym_hash;
5299 hpp = elf_sym_hashes (abfd);
5300 hppend = hpp + extsymcount;
5301 sym_count = 0;
5302 for (; hpp < hppend; hpp++)
5303 {
5304 h = *hpp;
5305 if (h != NULL
5306 && h->root.type == bfd_link_hash_defined
fcb93ecf 5307 && !bed->is_function_type (h->type))
4ad4eba5
AM
5308 {
5309 *sym_hash = h;
5310 sym_hash++;
5311 sym_count++;
5312 }
5313 }
5314
5315 qsort (sorted_sym_hash, sym_count,
5316 sizeof (struct elf_link_hash_entry *),
5317 elf_sort_symbol);
5318
5319 while (weaks != NULL)
5320 {
5321 struct elf_link_hash_entry *hlook;
5322 asection *slook;
5323 bfd_vma vlook;
ed54588d 5324 size_t i, j, idx = 0;
4ad4eba5
AM
5325
5326 hlook = weaks;
60d67dc8
AM
5327 weaks = hlook->u.alias;
5328 hlook->u.alias = NULL;
4ad4eba5 5329
e3e53eed
AM
5330 if (hlook->root.type != bfd_link_hash_defined
5331 && hlook->root.type != bfd_link_hash_defweak)
5332 continue;
5333
4ad4eba5
AM
5334 slook = hlook->root.u.def.section;
5335 vlook = hlook->root.u.def.value;
5336
4ad4eba5
AM
5337 i = 0;
5338 j = sym_count;
14160578 5339 while (i != j)
4ad4eba5
AM
5340 {
5341 bfd_signed_vma vdiff;
5342 idx = (i + j) / 2;
14160578 5343 h = sorted_sym_hash[idx];
4ad4eba5
AM
5344 vdiff = vlook - h->root.u.def.value;
5345 if (vdiff < 0)
5346 j = idx;
5347 else if (vdiff > 0)
5348 i = idx + 1;
5349 else
5350 {
d3435ae8 5351 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5352 if (sdiff < 0)
5353 j = idx;
5354 else if (sdiff > 0)
5355 i = idx + 1;
5356 else
14160578 5357 break;
4ad4eba5
AM
5358 }
5359 }
5360
5361 /* We didn't find a value/section match. */
14160578 5362 if (i == j)
4ad4eba5
AM
5363 continue;
5364
14160578
AM
5365 /* With multiple aliases, or when the weak symbol is already
5366 strongly defined, we have multiple matching symbols and
5367 the binary search above may land on any of them. Step
5368 one past the matching symbol(s). */
5369 while (++idx != j)
5370 {
5371 h = sorted_sym_hash[idx];
5372 if (h->root.u.def.section != slook
5373 || h->root.u.def.value != vlook)
5374 break;
5375 }
5376
5377 /* Now look back over the aliases. Since we sorted by size
5378 as well as value and section, we'll choose the one with
5379 the largest size. */
5380 while (idx-- != i)
4ad4eba5 5381 {
14160578 5382 h = sorted_sym_hash[idx];
4ad4eba5
AM
5383
5384 /* Stop if value or section doesn't match. */
14160578
AM
5385 if (h->root.u.def.section != slook
5386 || h->root.u.def.value != vlook)
4ad4eba5
AM
5387 break;
5388 else if (h != hlook)
5389 {
60d67dc8
AM
5390 struct elf_link_hash_entry *t;
5391
5392 hlook->u.alias = h;
5393 hlook->is_weakalias = 1;
5394 t = h;
5395 if (t->u.alias != NULL)
5396 while (t->u.alias != h)
5397 t = t->u.alias;
5398 t->u.alias = hlook;
4ad4eba5
AM
5399
5400 /* If the weak definition is in the list of dynamic
5401 symbols, make sure the real definition is put
5402 there as well. */
5403 if (hlook->dynindx != -1 && h->dynindx == -1)
5404 {
c152c796 5405 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5406 {
5407 err_free_sym_hash:
5408 free (sorted_sym_hash);
5409 goto error_return;
5410 }
4ad4eba5
AM
5411 }
5412
5413 /* If the real definition is in the list of dynamic
5414 symbols, make sure the weak definition is put
5415 there as well. If we don't do this, then the
5416 dynamic loader might not merge the entries for the
5417 real definition and the weak definition. */
5418 if (h->dynindx != -1 && hlook->dynindx == -1)
5419 {
c152c796 5420 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5421 goto err_free_sym_hash;
4ad4eba5
AM
5422 }
5423 break;
5424 }
5425 }
5426 }
5427
5428 free (sorted_sym_hash);
5429 }
5430
33177bb1
AM
5431 if (bed->check_directives
5432 && !(*bed->check_directives) (abfd, info))
5433 return FALSE;
85fbca6a 5434
4ad4eba5
AM
5435 /* If this is a non-traditional link, try to optimize the handling
5436 of the .stab/.stabstr sections. */
5437 if (! dynamic
5438 && ! info->traditional_format
66eb6687 5439 && is_elf_hash_table (htab)
4ad4eba5
AM
5440 && (info->strip != strip_all && info->strip != strip_debugger))
5441 {
5442 asection *stabstr;
5443
5444 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5445 if (stabstr != NULL)
5446 {
5447 bfd_size_type string_offset = 0;
5448 asection *stab;
5449
5450 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5451 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5452 && (!stab->name[5] ||
5453 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5454 && (stab->flags & SEC_MERGE) == 0
5455 && !bfd_is_abs_section (stab->output_section))
5456 {
5457 struct bfd_elf_section_data *secdata;
5458
5459 secdata = elf_section_data (stab);
66eb6687
AM
5460 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5461 stabstr, &secdata->sec_info,
4ad4eba5
AM
5462 &string_offset))
5463 goto error_return;
5464 if (secdata->sec_info)
dbaa2011 5465 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5466 }
5467 }
5468 }
5469
66eb6687 5470 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5471 {
5472 /* Add this bfd to the loaded list. */
5473 struct elf_link_loaded_list *n;
5474
ca4be51c 5475 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5476 if (n == NULL)
5477 goto error_return;
5478 n->abfd = abfd;
66eb6687
AM
5479 n->next = htab->loaded;
5480 htab->loaded = n;
4ad4eba5
AM
5481 }
5482
5483 return TRUE;
5484
5485 error_free_vers:
66eb6687
AM
5486 if (old_tab != NULL)
5487 free (old_tab);
5b677558
AM
5488 if (old_strtab != NULL)
5489 free (old_strtab);
4ad4eba5
AM
5490 if (nondeflt_vers != NULL)
5491 free (nondeflt_vers);
5492 if (extversym != NULL)
5493 free (extversym);
5494 error_free_sym:
5495 if (isymbuf != NULL)
5496 free (isymbuf);
5497 error_return:
5498 return FALSE;
5499}
5500
8387904d
AM
5501/* Return the linker hash table entry of a symbol that might be
5502 satisfied by an archive symbol. Return -1 on error. */
5503
5504struct elf_link_hash_entry *
5505_bfd_elf_archive_symbol_lookup (bfd *abfd,
5506 struct bfd_link_info *info,
5507 const char *name)
5508{
5509 struct elf_link_hash_entry *h;
5510 char *p, *copy;
5511 size_t len, first;
5512
2a41f396 5513 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5514 if (h != NULL)
5515 return h;
5516
5517 /* If this is a default version (the name contains @@), look up the
5518 symbol again with only one `@' as well as without the version.
5519 The effect is that references to the symbol with and without the
5520 version will be matched by the default symbol in the archive. */
5521
5522 p = strchr (name, ELF_VER_CHR);
5523 if (p == NULL || p[1] != ELF_VER_CHR)
5524 return h;
5525
5526 /* First check with only one `@'. */
5527 len = strlen (name);
a50b1753 5528 copy = (char *) bfd_alloc (abfd, len);
8387904d 5529 if (copy == NULL)
e99955cd 5530 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5531
5532 first = p - name + 1;
5533 memcpy (copy, name, first);
5534 memcpy (copy + first, name + first + 1, len - first);
5535
2a41f396 5536 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5537 if (h == NULL)
5538 {
5539 /* We also need to check references to the symbol without the
5540 version. */
5541 copy[first - 1] = '\0';
5542 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5543 FALSE, FALSE, TRUE);
8387904d
AM
5544 }
5545
5546 bfd_release (abfd, copy);
5547 return h;
5548}
5549
0ad989f9 5550/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5551 don't use _bfd_generic_link_add_archive_symbols because we need to
5552 handle versioned symbols.
0ad989f9
L
5553
5554 Fortunately, ELF archive handling is simpler than that done by
5555 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5556 oddities. In ELF, if we find a symbol in the archive map, and the
5557 symbol is currently undefined, we know that we must pull in that
5558 object file.
5559
5560 Unfortunately, we do have to make multiple passes over the symbol
5561 table until nothing further is resolved. */
5562
4ad4eba5
AM
5563static bfd_boolean
5564elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5565{
5566 symindex c;
13e570f8 5567 unsigned char *included = NULL;
0ad989f9
L
5568 carsym *symdefs;
5569 bfd_boolean loop;
5570 bfd_size_type amt;
8387904d
AM
5571 const struct elf_backend_data *bed;
5572 struct elf_link_hash_entry * (*archive_symbol_lookup)
5573 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5574
5575 if (! bfd_has_map (abfd))
5576 {
5577 /* An empty archive is a special case. */
5578 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5579 return TRUE;
5580 bfd_set_error (bfd_error_no_armap);
5581 return FALSE;
5582 }
5583
5584 /* Keep track of all symbols we know to be already defined, and all
5585 files we know to be already included. This is to speed up the
5586 second and subsequent passes. */
5587 c = bfd_ardata (abfd)->symdef_count;
5588 if (c == 0)
5589 return TRUE;
5590 amt = c;
13e570f8
AM
5591 amt *= sizeof (*included);
5592 included = (unsigned char *) bfd_zmalloc (amt);
5593 if (included == NULL)
5594 return FALSE;
0ad989f9
L
5595
5596 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5597 bed = get_elf_backend_data (abfd);
5598 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5599
5600 do
5601 {
5602 file_ptr last;
5603 symindex i;
5604 carsym *symdef;
5605 carsym *symdefend;
5606
5607 loop = FALSE;
5608 last = -1;
5609
5610 symdef = symdefs;
5611 symdefend = symdef + c;
5612 for (i = 0; symdef < symdefend; symdef++, i++)
5613 {
5614 struct elf_link_hash_entry *h;
5615 bfd *element;
5616 struct bfd_link_hash_entry *undefs_tail;
5617 symindex mark;
5618
13e570f8 5619 if (included[i])
0ad989f9
L
5620 continue;
5621 if (symdef->file_offset == last)
5622 {
5623 included[i] = TRUE;
5624 continue;
5625 }
5626
8387904d 5627 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5628 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5629 goto error_return;
0ad989f9
L
5630
5631 if (h == NULL)
5632 continue;
5633
5634 if (h->root.type == bfd_link_hash_common)
5635 {
5636 /* We currently have a common symbol. The archive map contains
5637 a reference to this symbol, so we may want to include it. We
5638 only want to include it however, if this archive element
5639 contains a definition of the symbol, not just another common
5640 declaration of it.
5641
5642 Unfortunately some archivers (including GNU ar) will put
5643 declarations of common symbols into their archive maps, as
5644 well as real definitions, so we cannot just go by the archive
5645 map alone. Instead we must read in the element's symbol
5646 table and check that to see what kind of symbol definition
5647 this is. */
5648 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5649 continue;
5650 }
5651 else if (h->root.type != bfd_link_hash_undefined)
5652 {
5653 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5654 /* Symbol must be defined. Don't check it again. */
5655 included[i] = TRUE;
0ad989f9
L
5656 continue;
5657 }
5658
5659 /* We need to include this archive member. */
5660 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5661 if (element == NULL)
5662 goto error_return;
5663
5664 if (! bfd_check_format (element, bfd_object))
5665 goto error_return;
5666
0ad989f9
L
5667 undefs_tail = info->hash->undefs_tail;
5668
0e144ba7
AM
5669 if (!(*info->callbacks
5670 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5671 continue;
0e144ba7 5672 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5673 goto error_return;
5674
5675 /* If there are any new undefined symbols, we need to make
5676 another pass through the archive in order to see whether
5677 they can be defined. FIXME: This isn't perfect, because
5678 common symbols wind up on undefs_tail and because an
5679 undefined symbol which is defined later on in this pass
5680 does not require another pass. This isn't a bug, but it
5681 does make the code less efficient than it could be. */
5682 if (undefs_tail != info->hash->undefs_tail)
5683 loop = TRUE;
5684
5685 /* Look backward to mark all symbols from this object file
5686 which we have already seen in this pass. */
5687 mark = i;
5688 do
5689 {
5690 included[mark] = TRUE;
5691 if (mark == 0)
5692 break;
5693 --mark;
5694 }
5695 while (symdefs[mark].file_offset == symdef->file_offset);
5696
5697 /* We mark subsequent symbols from this object file as we go
5698 on through the loop. */
5699 last = symdef->file_offset;
5700 }
5701 }
5702 while (loop);
5703
0ad989f9
L
5704 free (included);
5705
5706 return TRUE;
5707
5708 error_return:
0ad989f9
L
5709 if (included != NULL)
5710 free (included);
5711 return FALSE;
5712}
4ad4eba5
AM
5713
5714/* Given an ELF BFD, add symbols to the global hash table as
5715 appropriate. */
5716
5717bfd_boolean
5718bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5719{
5720 switch (bfd_get_format (abfd))
5721 {
5722 case bfd_object:
5723 return elf_link_add_object_symbols (abfd, info);
5724 case bfd_archive:
5725 return elf_link_add_archive_symbols (abfd, info);
5726 default:
5727 bfd_set_error (bfd_error_wrong_format);
5728 return FALSE;
5729 }
5730}
5a580b3a 5731\f
14b1c01e
AM
5732struct hash_codes_info
5733{
5734 unsigned long *hashcodes;
5735 bfd_boolean error;
5736};
a0c8462f 5737
5a580b3a
AM
5738/* This function will be called though elf_link_hash_traverse to store
5739 all hash value of the exported symbols in an array. */
5740
5741static bfd_boolean
5742elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5743{
a50b1753 5744 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5745 const char *name;
5a580b3a
AM
5746 unsigned long ha;
5747 char *alc = NULL;
5748
5a580b3a
AM
5749 /* Ignore indirect symbols. These are added by the versioning code. */
5750 if (h->dynindx == -1)
5751 return TRUE;
5752
5753 name = h->root.root.string;
422f1182 5754 if (h->versioned >= versioned)
5a580b3a 5755 {
422f1182
L
5756 char *p = strchr (name, ELF_VER_CHR);
5757 if (p != NULL)
14b1c01e 5758 {
422f1182
L
5759 alc = (char *) bfd_malloc (p - name + 1);
5760 if (alc == NULL)
5761 {
5762 inf->error = TRUE;
5763 return FALSE;
5764 }
5765 memcpy (alc, name, p - name);
5766 alc[p - name] = '\0';
5767 name = alc;
14b1c01e 5768 }
5a580b3a
AM
5769 }
5770
5771 /* Compute the hash value. */
5772 ha = bfd_elf_hash (name);
5773
5774 /* Store the found hash value in the array given as the argument. */
14b1c01e 5775 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5776
5777 /* And store it in the struct so that we can put it in the hash table
5778 later. */
f6e332e6 5779 h->u.elf_hash_value = ha;
5a580b3a
AM
5780
5781 if (alc != NULL)
5782 free (alc);
5783
5784 return TRUE;
5785}
5786
fdc90cb4
JJ
5787struct collect_gnu_hash_codes
5788{
5789 bfd *output_bfd;
5790 const struct elf_backend_data *bed;
5791 unsigned long int nsyms;
5792 unsigned long int maskbits;
5793 unsigned long int *hashcodes;
5794 unsigned long int *hashval;
5795 unsigned long int *indx;
5796 unsigned long int *counts;
5797 bfd_vma *bitmask;
5798 bfd_byte *contents;
5799 long int min_dynindx;
5800 unsigned long int bucketcount;
5801 unsigned long int symindx;
5802 long int local_indx;
5803 long int shift1, shift2;
5804 unsigned long int mask;
14b1c01e 5805 bfd_boolean error;
fdc90cb4
JJ
5806};
5807
5808/* This function will be called though elf_link_hash_traverse to store
5809 all hash value of the exported symbols in an array. */
5810
5811static bfd_boolean
5812elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5813{
a50b1753 5814 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5815 const char *name;
fdc90cb4
JJ
5816 unsigned long ha;
5817 char *alc = NULL;
5818
fdc90cb4
JJ
5819 /* Ignore indirect symbols. These are added by the versioning code. */
5820 if (h->dynindx == -1)
5821 return TRUE;
5822
5823 /* Ignore also local symbols and undefined symbols. */
5824 if (! (*s->bed->elf_hash_symbol) (h))
5825 return TRUE;
5826
5827 name = h->root.root.string;
422f1182 5828 if (h->versioned >= versioned)
fdc90cb4 5829 {
422f1182
L
5830 char *p = strchr (name, ELF_VER_CHR);
5831 if (p != NULL)
14b1c01e 5832 {
422f1182
L
5833 alc = (char *) bfd_malloc (p - name + 1);
5834 if (alc == NULL)
5835 {
5836 s->error = TRUE;
5837 return FALSE;
5838 }
5839 memcpy (alc, name, p - name);
5840 alc[p - name] = '\0';
5841 name = alc;
14b1c01e 5842 }
fdc90cb4
JJ
5843 }
5844
5845 /* Compute the hash value. */
5846 ha = bfd_elf_gnu_hash (name);
5847
5848 /* Store the found hash value in the array for compute_bucket_count,
5849 and also for .dynsym reordering purposes. */
5850 s->hashcodes[s->nsyms] = ha;
5851 s->hashval[h->dynindx] = ha;
5852 ++s->nsyms;
5853 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5854 s->min_dynindx = h->dynindx;
5855
5856 if (alc != NULL)
5857 free (alc);
5858
5859 return TRUE;
5860}
5861
5862/* This function will be called though elf_link_hash_traverse to do
5863 final dynaminc symbol renumbering. */
5864
5865static bfd_boolean
5866elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5867{
a50b1753 5868 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5869 unsigned long int bucket;
5870 unsigned long int val;
5871
fdc90cb4
JJ
5872 /* Ignore indirect symbols. */
5873 if (h->dynindx == -1)
5874 return TRUE;
5875
5876 /* Ignore also local symbols and undefined symbols. */
5877 if (! (*s->bed->elf_hash_symbol) (h))
5878 {
5879 if (h->dynindx >= s->min_dynindx)
5880 h->dynindx = s->local_indx++;
5881 return TRUE;
5882 }
5883
5884 bucket = s->hashval[h->dynindx] % s->bucketcount;
5885 val = (s->hashval[h->dynindx] >> s->shift1)
5886 & ((s->maskbits >> s->shift1) - 1);
5887 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5888 s->bitmask[val]
5889 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5890 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5891 if (s->counts[bucket] == 1)
5892 /* Last element terminates the chain. */
5893 val |= 1;
5894 bfd_put_32 (s->output_bfd, val,
5895 s->contents + (s->indx[bucket] - s->symindx) * 4);
5896 --s->counts[bucket];
5897 h->dynindx = s->indx[bucket]++;
5898 return TRUE;
5899}
5900
5901/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5902
5903bfd_boolean
5904_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5905{
5906 return !(h->forced_local
5907 || h->root.type == bfd_link_hash_undefined
5908 || h->root.type == bfd_link_hash_undefweak
5909 || ((h->root.type == bfd_link_hash_defined
5910 || h->root.type == bfd_link_hash_defweak)
5911 && h->root.u.def.section->output_section == NULL));
5912}
5913
5a580b3a
AM
5914/* Array used to determine the number of hash table buckets to use
5915 based on the number of symbols there are. If there are fewer than
5916 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5917 fewer than 37 we use 17 buckets, and so forth. We never use more
5918 than 32771 buckets. */
5919
5920static const size_t elf_buckets[] =
5921{
5922 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5923 16411, 32771, 0
5924};
5925
5926/* Compute bucket count for hashing table. We do not use a static set
5927 of possible tables sizes anymore. Instead we determine for all
5928 possible reasonable sizes of the table the outcome (i.e., the
5929 number of collisions etc) and choose the best solution. The
5930 weighting functions are not too simple to allow the table to grow
5931 without bounds. Instead one of the weighting factors is the size.
5932 Therefore the result is always a good payoff between few collisions
5933 (= short chain lengths) and table size. */
5934static size_t
b20dd2ce 5935compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5936 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5937 unsigned long int nsyms,
5938 int gnu_hash)
5a580b3a 5939{
5a580b3a 5940 size_t best_size = 0;
5a580b3a 5941 unsigned long int i;
5a580b3a 5942
5a580b3a
AM
5943 /* We have a problem here. The following code to optimize the table
5944 size requires an integer type with more the 32 bits. If
5945 BFD_HOST_U_64_BIT is set we know about such a type. */
5946#ifdef BFD_HOST_U_64_BIT
5947 if (info->optimize)
5948 {
5a580b3a
AM
5949 size_t minsize;
5950 size_t maxsize;
5951 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5952 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5953 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5954 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5955 unsigned long int *counts;
d40f3da9 5956 bfd_size_type amt;
0883b6e0 5957 unsigned int no_improvement_count = 0;
5a580b3a
AM
5958
5959 /* Possible optimization parameters: if we have NSYMS symbols we say
5960 that the hashing table must at least have NSYMS/4 and at most
5961 2*NSYMS buckets. */
5962 minsize = nsyms / 4;
5963 if (minsize == 0)
5964 minsize = 1;
5965 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5966 if (gnu_hash)
5967 {
5968 if (minsize < 2)
5969 minsize = 2;
5970 if ((best_size & 31) == 0)
5971 ++best_size;
5972 }
5a580b3a
AM
5973
5974 /* Create array where we count the collisions in. We must use bfd_malloc
5975 since the size could be large. */
5976 amt = maxsize;
5977 amt *= sizeof (unsigned long int);
a50b1753 5978 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5979 if (counts == NULL)
fdc90cb4 5980 return 0;
5a580b3a
AM
5981
5982 /* Compute the "optimal" size for the hash table. The criteria is a
5983 minimal chain length. The minor criteria is (of course) the size
5984 of the table. */
5985 for (i = minsize; i < maxsize; ++i)
5986 {
5987 /* Walk through the array of hashcodes and count the collisions. */
5988 BFD_HOST_U_64_BIT max;
5989 unsigned long int j;
5990 unsigned long int fact;
5991
fdc90cb4
JJ
5992 if (gnu_hash && (i & 31) == 0)
5993 continue;
5994
5a580b3a
AM
5995 memset (counts, '\0', i * sizeof (unsigned long int));
5996
5997 /* Determine how often each hash bucket is used. */
5998 for (j = 0; j < nsyms; ++j)
5999 ++counts[hashcodes[j] % i];
6000
6001 /* For the weight function we need some information about the
6002 pagesize on the target. This is information need not be 100%
6003 accurate. Since this information is not available (so far) we
6004 define it here to a reasonable default value. If it is crucial
6005 to have a better value some day simply define this value. */
6006# ifndef BFD_TARGET_PAGESIZE
6007# define BFD_TARGET_PAGESIZE (4096)
6008# endif
6009
fdc90cb4
JJ
6010 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6011 and the chains. */
6012 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6013
6014# if 1
6015 /* Variant 1: optimize for short chains. We add the squares
6016 of all the chain lengths (which favors many small chain
6017 over a few long chains). */
6018 for (j = 0; j < i; ++j)
6019 max += counts[j] * counts[j];
6020
6021 /* This adds penalties for the overall size of the table. */
fdc90cb4 6022 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6023 max *= fact * fact;
6024# else
6025 /* Variant 2: Optimize a lot more for small table. Here we
6026 also add squares of the size but we also add penalties for
6027 empty slots (the +1 term). */
6028 for (j = 0; j < i; ++j)
6029 max += (1 + counts[j]) * (1 + counts[j]);
6030
6031 /* The overall size of the table is considered, but not as
6032 strong as in variant 1, where it is squared. */
fdc90cb4 6033 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6034 max *= fact;
6035# endif
6036
6037 /* Compare with current best results. */
6038 if (max < best_chlen)
6039 {
6040 best_chlen = max;
6041 best_size = i;
ca4be51c 6042 no_improvement_count = 0;
5a580b3a 6043 }
0883b6e0
NC
6044 /* PR 11843: Avoid futile long searches for the best bucket size
6045 when there are a large number of symbols. */
6046 else if (++no_improvement_count == 100)
6047 break;
5a580b3a
AM
6048 }
6049
6050 free (counts);
6051 }
6052 else
6053#endif /* defined (BFD_HOST_U_64_BIT) */
6054 {
6055 /* This is the fallback solution if no 64bit type is available or if we
6056 are not supposed to spend much time on optimizations. We select the
6057 bucket count using a fixed set of numbers. */
6058 for (i = 0; elf_buckets[i] != 0; i++)
6059 {
6060 best_size = elf_buckets[i];
fdc90cb4 6061 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6062 break;
6063 }
fdc90cb4
JJ
6064 if (gnu_hash && best_size < 2)
6065 best_size = 2;
5a580b3a
AM
6066 }
6067
5a580b3a
AM
6068 return best_size;
6069}
6070
d0bf826b
AM
6071/* Size any SHT_GROUP section for ld -r. */
6072
6073bfd_boolean
6074_bfd_elf_size_group_sections (struct bfd_link_info *info)
6075{
6076 bfd *ibfd;
57963c05 6077 asection *s;
d0bf826b 6078
c72f2fb2 6079 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6080 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6081 && (s = ibfd->sections) != NULL
6082 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6083 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6084 return FALSE;
6085 return TRUE;
6086}
6087
04c3a755
NS
6088/* Set a default stack segment size. The value in INFO wins. If it
6089 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6090 undefined it is initialized. */
6091
6092bfd_boolean
6093bfd_elf_stack_segment_size (bfd *output_bfd,
6094 struct bfd_link_info *info,
6095 const char *legacy_symbol,
6096 bfd_vma default_size)
6097{
6098 struct elf_link_hash_entry *h = NULL;
6099
6100 /* Look for legacy symbol. */
6101 if (legacy_symbol)
6102 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6103 FALSE, FALSE, FALSE);
6104 if (h && (h->root.type == bfd_link_hash_defined
6105 || h->root.type == bfd_link_hash_defweak)
6106 && h->def_regular
6107 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6108 {
6109 /* The symbol has no type if specified on the command line. */
6110 h->type = STT_OBJECT;
6111 if (info->stacksize)
695344c0 6112 /* xgettext:c-format */
871b3ab2 6113 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6114 output_bfd, legacy_symbol);
04c3a755 6115 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6116 /* xgettext:c-format */
871b3ab2 6117 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6118 output_bfd, legacy_symbol);
04c3a755
NS
6119 else
6120 info->stacksize = h->root.u.def.value;
6121 }
6122
6123 if (!info->stacksize)
6124 /* If the user didn't set a size, or explicitly inhibit the
6125 size, set it now. */
6126 info->stacksize = default_size;
6127
6128 /* Provide the legacy symbol, if it is referenced. */
6129 if (h && (h->root.type == bfd_link_hash_undefined
6130 || h->root.type == bfd_link_hash_undefweak))
6131 {
6132 struct bfd_link_hash_entry *bh = NULL;
6133
6134 if (!(_bfd_generic_link_add_one_symbol
6135 (info, output_bfd, legacy_symbol,
6136 BSF_GLOBAL, bfd_abs_section_ptr,
6137 info->stacksize >= 0 ? info->stacksize : 0,
6138 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6139 return FALSE;
6140
6141 h = (struct elf_link_hash_entry *) bh;
6142 h->def_regular = 1;
6143 h->type = STT_OBJECT;
6144 }
6145
6146 return TRUE;
6147}
6148
b531344c
MR
6149/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6150
6151struct elf_gc_sweep_symbol_info
6152{
6153 struct bfd_link_info *info;
6154 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6155 bfd_boolean);
6156};
6157
6158static bfd_boolean
6159elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6160{
6161 if (!h->mark
6162 && (((h->root.type == bfd_link_hash_defined
6163 || h->root.type == bfd_link_hash_defweak)
6164 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6165 && h->root.u.def.section->gc_mark))
6166 || h->root.type == bfd_link_hash_undefined
6167 || h->root.type == bfd_link_hash_undefweak))
6168 {
6169 struct elf_gc_sweep_symbol_info *inf;
6170
6171 inf = (struct elf_gc_sweep_symbol_info *) data;
6172 (*inf->hide_symbol) (inf->info, h, TRUE);
6173 h->def_regular = 0;
6174 h->ref_regular = 0;
6175 h->ref_regular_nonweak = 0;
6176 }
6177
6178 return TRUE;
6179}
6180
5a580b3a
AM
6181/* Set up the sizes and contents of the ELF dynamic sections. This is
6182 called by the ELF linker emulation before_allocation routine. We
6183 must set the sizes of the sections before the linker sets the
6184 addresses of the various sections. */
6185
6186bfd_boolean
6187bfd_elf_size_dynamic_sections (bfd *output_bfd,
6188 const char *soname,
6189 const char *rpath,
6190 const char *filter_shlib,
7ee314fa
AM
6191 const char *audit,
6192 const char *depaudit,
5a580b3a
AM
6193 const char * const *auxiliary_filters,
6194 struct bfd_link_info *info,
fd91d419 6195 asection **sinterpptr)
5a580b3a 6196{
5a580b3a
AM
6197 bfd *dynobj;
6198 const struct elf_backend_data *bed;
5a580b3a
AM
6199
6200 *sinterpptr = NULL;
6201
5a580b3a
AM
6202 if (!is_elf_hash_table (info->hash))
6203 return TRUE;
6204
5a580b3a
AM
6205 dynobj = elf_hash_table (info)->dynobj;
6206
9a2a56cc 6207 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6208 {
902e9fc7
MR
6209 struct bfd_elf_version_tree *verdefs;
6210 struct elf_info_failed asvinfo;
5a580b3a
AM
6211 struct bfd_elf_version_tree *t;
6212 struct bfd_elf_version_expr *d;
902e9fc7 6213 asection *s;
e6699019 6214 size_t soname_indx;
7ee314fa 6215
5a580b3a
AM
6216 /* If we are supposed to export all symbols into the dynamic symbol
6217 table (this is not the normal case), then do so. */
55255dae 6218 if (info->export_dynamic
0e1862bb 6219 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6220 {
3d13f3e9
AM
6221 struct elf_info_failed eif;
6222
6223 eif.info = info;
6224 eif.failed = FALSE;
5a580b3a
AM
6225 elf_link_hash_traverse (elf_hash_table (info),
6226 _bfd_elf_export_symbol,
6227 &eif);
6228 if (eif.failed)
6229 return FALSE;
6230 }
6231
e6699019
L
6232 if (soname != NULL)
6233 {
6234 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6235 soname, TRUE);
6236 if (soname_indx == (size_t) -1
6237 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6238 return FALSE;
6239 }
6240 else
6241 soname_indx = (size_t) -1;
6242
5a580b3a 6243 /* Make all global versions with definition. */
fd91d419 6244 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6245 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6246 if (!d->symver && d->literal)
5a580b3a
AM
6247 {
6248 const char *verstr, *name;
6249 size_t namelen, verlen, newlen;
93252b1c 6250 char *newname, *p, leading_char;
5a580b3a
AM
6251 struct elf_link_hash_entry *newh;
6252
93252b1c 6253 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6254 name = d->pattern;
93252b1c 6255 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6256 verstr = t->name;
6257 verlen = strlen (verstr);
6258 newlen = namelen + verlen + 3;
6259
a50b1753 6260 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6261 if (newname == NULL)
6262 return FALSE;
93252b1c
MF
6263 newname[0] = leading_char;
6264 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6265
6266 /* Check the hidden versioned definition. */
6267 p = newname + namelen;
6268 *p++ = ELF_VER_CHR;
6269 memcpy (p, verstr, verlen + 1);
6270 newh = elf_link_hash_lookup (elf_hash_table (info),
6271 newname, FALSE, FALSE,
6272 FALSE);
6273 if (newh == NULL
6274 || (newh->root.type != bfd_link_hash_defined
6275 && newh->root.type != bfd_link_hash_defweak))
6276 {
6277 /* Check the default versioned definition. */
6278 *p++ = ELF_VER_CHR;
6279 memcpy (p, verstr, verlen + 1);
6280 newh = elf_link_hash_lookup (elf_hash_table (info),
6281 newname, FALSE, FALSE,
6282 FALSE);
6283 }
6284 free (newname);
6285
6286 /* Mark this version if there is a definition and it is
6287 not defined in a shared object. */
6288 if (newh != NULL
f5385ebf 6289 && !newh->def_dynamic
5a580b3a
AM
6290 && (newh->root.type == bfd_link_hash_defined
6291 || newh->root.type == bfd_link_hash_defweak))
6292 d->symver = 1;
6293 }
6294
6295 /* Attach all the symbols to their version information. */
5a580b3a 6296 asvinfo.info = info;
5a580b3a
AM
6297 asvinfo.failed = FALSE;
6298
6299 elf_link_hash_traverse (elf_hash_table (info),
6300 _bfd_elf_link_assign_sym_version,
6301 &asvinfo);
6302 if (asvinfo.failed)
6303 return FALSE;
6304
6305 if (!info->allow_undefined_version)
6306 {
6307 /* Check if all global versions have a definition. */
3d13f3e9 6308 bfd_boolean all_defined = TRUE;
fd91d419 6309 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6310 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6311 if (d->literal && !d->symver && !d->script)
5a580b3a 6312 {
4eca0228 6313 _bfd_error_handler
5a580b3a
AM
6314 (_("%s: undefined version: %s"),
6315 d->pattern, t->name);
6316 all_defined = FALSE;
6317 }
6318
6319 if (!all_defined)
6320 {
6321 bfd_set_error (bfd_error_bad_value);
6322 return FALSE;
6323 }
6324 }
6325
902e9fc7
MR
6326 /* Set up the version definition section. */
6327 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6328 BFD_ASSERT (s != NULL);
5a580b3a 6329
902e9fc7
MR
6330 /* We may have created additional version definitions if we are
6331 just linking a regular application. */
6332 verdefs = info->version_info;
5a580b3a 6333
902e9fc7
MR
6334 /* Skip anonymous version tag. */
6335 if (verdefs != NULL && verdefs->vernum == 0)
6336 verdefs = verdefs->next;
5a580b3a 6337
902e9fc7
MR
6338 if (verdefs == NULL && !info->create_default_symver)
6339 s->flags |= SEC_EXCLUDE;
6340 else
5a580b3a 6341 {
902e9fc7
MR
6342 unsigned int cdefs;
6343 bfd_size_type size;
6344 bfd_byte *p;
6345 Elf_Internal_Verdef def;
6346 Elf_Internal_Verdaux defaux;
6347 struct bfd_link_hash_entry *bh;
6348 struct elf_link_hash_entry *h;
6349 const char *name;
5a580b3a 6350
902e9fc7
MR
6351 cdefs = 0;
6352 size = 0;
5a580b3a 6353
902e9fc7
MR
6354 /* Make space for the base version. */
6355 size += sizeof (Elf_External_Verdef);
6356 size += sizeof (Elf_External_Verdaux);
6357 ++cdefs;
6358
6359 /* Make space for the default version. */
6360 if (info->create_default_symver)
6361 {
6362 size += sizeof (Elf_External_Verdef);
6363 ++cdefs;
3e3b46e5
PB
6364 }
6365
5a580b3a
AM
6366 for (t = verdefs; t != NULL; t = t->next)
6367 {
6368 struct bfd_elf_version_deps *n;
6369
a6cc6b3b
RO
6370 /* Don't emit base version twice. */
6371 if (t->vernum == 0)
6372 continue;
6373
5a580b3a
AM
6374 size += sizeof (Elf_External_Verdef);
6375 size += sizeof (Elf_External_Verdaux);
6376 ++cdefs;
6377
6378 for (n = t->deps; n != NULL; n = n->next)
6379 size += sizeof (Elf_External_Verdaux);
6380 }
6381
eea6121a 6382 s->size = size;
a50b1753 6383 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6384 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6385 return FALSE;
6386
6387 /* Fill in the version definition section. */
6388
6389 p = s->contents;
6390
6391 def.vd_version = VER_DEF_CURRENT;
6392 def.vd_flags = VER_FLG_BASE;
6393 def.vd_ndx = 1;
6394 def.vd_cnt = 1;
3e3b46e5
PB
6395 if (info->create_default_symver)
6396 {
6397 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6398 def.vd_next = sizeof (Elf_External_Verdef);
6399 }
6400 else
6401 {
6402 def.vd_aux = sizeof (Elf_External_Verdef);
6403 def.vd_next = (sizeof (Elf_External_Verdef)
6404 + sizeof (Elf_External_Verdaux));
6405 }
5a580b3a 6406
ef53be89 6407 if (soname_indx != (size_t) -1)
5a580b3a
AM
6408 {
6409 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6410 soname_indx);
6411 def.vd_hash = bfd_elf_hash (soname);
6412 defaux.vda_name = soname_indx;
3e3b46e5 6413 name = soname;
5a580b3a
AM
6414 }
6415 else
6416 {
ef53be89 6417 size_t indx;
5a580b3a 6418
06084812 6419 name = lbasename (output_bfd->filename);
5a580b3a
AM
6420 def.vd_hash = bfd_elf_hash (name);
6421 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6422 name, FALSE);
ef53be89 6423 if (indx == (size_t) -1)
5a580b3a
AM
6424 return FALSE;
6425 defaux.vda_name = indx;
6426 }
6427 defaux.vda_next = 0;
6428
6429 _bfd_elf_swap_verdef_out (output_bfd, &def,
6430 (Elf_External_Verdef *) p);
6431 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6432 if (info->create_default_symver)
6433 {
6434 /* Add a symbol representing this version. */
6435 bh = NULL;
6436 if (! (_bfd_generic_link_add_one_symbol
6437 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6438 0, NULL, FALSE,
6439 get_elf_backend_data (dynobj)->collect, &bh)))
6440 return FALSE;
6441 h = (struct elf_link_hash_entry *) bh;
6442 h->non_elf = 0;
6443 h->def_regular = 1;
6444 h->type = STT_OBJECT;
6445 h->verinfo.vertree = NULL;
6446
6447 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6448 return FALSE;
6449
6450 /* Create a duplicate of the base version with the same
6451 aux block, but different flags. */
6452 def.vd_flags = 0;
6453 def.vd_ndx = 2;
6454 def.vd_aux = sizeof (Elf_External_Verdef);
6455 if (verdefs)
6456 def.vd_next = (sizeof (Elf_External_Verdef)
6457 + sizeof (Elf_External_Verdaux));
6458 else
6459 def.vd_next = 0;
6460 _bfd_elf_swap_verdef_out (output_bfd, &def,
6461 (Elf_External_Verdef *) p);
6462 p += sizeof (Elf_External_Verdef);
6463 }
5a580b3a
AM
6464 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6465 (Elf_External_Verdaux *) p);
6466 p += sizeof (Elf_External_Verdaux);
6467
6468 for (t = verdefs; t != NULL; t = t->next)
6469 {
6470 unsigned int cdeps;
6471 struct bfd_elf_version_deps *n;
5a580b3a 6472
a6cc6b3b
RO
6473 /* Don't emit the base version twice. */
6474 if (t->vernum == 0)
6475 continue;
6476
5a580b3a
AM
6477 cdeps = 0;
6478 for (n = t->deps; n != NULL; n = n->next)
6479 ++cdeps;
6480
6481 /* Add a symbol representing this version. */
6482 bh = NULL;
6483 if (! (_bfd_generic_link_add_one_symbol
6484 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6485 0, NULL, FALSE,
6486 get_elf_backend_data (dynobj)->collect, &bh)))
6487 return FALSE;
6488 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6489 h->non_elf = 0;
6490 h->def_regular = 1;
5a580b3a
AM
6491 h->type = STT_OBJECT;
6492 h->verinfo.vertree = t;
6493
c152c796 6494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6495 return FALSE;
6496
6497 def.vd_version = VER_DEF_CURRENT;
6498 def.vd_flags = 0;
6499 if (t->globals.list == NULL
6500 && t->locals.list == NULL
6501 && ! t->used)
6502 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6503 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6504 def.vd_cnt = cdeps + 1;
6505 def.vd_hash = bfd_elf_hash (t->name);
6506 def.vd_aux = sizeof (Elf_External_Verdef);
6507 def.vd_next = 0;
a6cc6b3b
RO
6508
6509 /* If a basever node is next, it *must* be the last node in
6510 the chain, otherwise Verdef construction breaks. */
6511 if (t->next != NULL && t->next->vernum == 0)
6512 BFD_ASSERT (t->next->next == NULL);
6513
6514 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6515 def.vd_next = (sizeof (Elf_External_Verdef)
6516 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6517
6518 _bfd_elf_swap_verdef_out (output_bfd, &def,
6519 (Elf_External_Verdef *) p);
6520 p += sizeof (Elf_External_Verdef);
6521
6522 defaux.vda_name = h->dynstr_index;
6523 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6524 h->dynstr_index);
6525 defaux.vda_next = 0;
6526 if (t->deps != NULL)
6527 defaux.vda_next = sizeof (Elf_External_Verdaux);
6528 t->name_indx = defaux.vda_name;
6529
6530 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6531 (Elf_External_Verdaux *) p);
6532 p += sizeof (Elf_External_Verdaux);
6533
6534 for (n = t->deps; n != NULL; n = n->next)
6535 {
6536 if (n->version_needed == NULL)
6537 {
6538 /* This can happen if there was an error in the
6539 version script. */
6540 defaux.vda_name = 0;
6541 }
6542 else
6543 {
6544 defaux.vda_name = n->version_needed->name_indx;
6545 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6546 defaux.vda_name);
6547 }
6548 if (n->next == NULL)
6549 defaux.vda_next = 0;
6550 else
6551 defaux.vda_next = sizeof (Elf_External_Verdaux);
6552
6553 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6554 (Elf_External_Verdaux *) p);
6555 p += sizeof (Elf_External_Verdaux);
6556 }
6557 }
6558
5a580b3a
AM
6559 elf_tdata (output_bfd)->cverdefs = cdefs;
6560 }
902e9fc7
MR
6561 }
6562
6563 bed = get_elf_backend_data (output_bfd);
6564
6565 if (info->gc_sections && bed->can_gc_sections)
6566 {
6567 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6568
6569 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6570 dynamic symbol table. */
902e9fc7
MR
6571 sweep_info.info = info;
6572 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6573 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6574 &sweep_info);
3d13f3e9
AM
6575 }
6576
6577 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6578 {
6579 asection *s;
6580 struct elf_find_verdep_info sinfo;
6581
6582 /* Work out the size of the version reference section. */
6583
6584 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6585 BFD_ASSERT (s != NULL);
902e9fc7 6586
3d13f3e9
AM
6587 sinfo.info = info;
6588 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6589 if (sinfo.vers == 0)
6590 sinfo.vers = 1;
6591 sinfo.failed = FALSE;
6592
6593 elf_link_hash_traverse (elf_hash_table (info),
6594 _bfd_elf_link_find_version_dependencies,
6595 &sinfo);
6596 if (sinfo.failed)
6597 return FALSE;
6598
6599 if (elf_tdata (output_bfd)->verref == NULL)
6600 s->flags |= SEC_EXCLUDE;
6601 else
6602 {
6603 Elf_Internal_Verneed *vn;
6604 unsigned int size;
6605 unsigned int crefs;
6606 bfd_byte *p;
6607
6608 /* Build the version dependency section. */
6609 size = 0;
6610 crefs = 0;
6611 for (vn = elf_tdata (output_bfd)->verref;
6612 vn != NULL;
6613 vn = vn->vn_nextref)
6614 {
6615 Elf_Internal_Vernaux *a;
6616
6617 size += sizeof (Elf_External_Verneed);
6618 ++crefs;
6619 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6620 size += sizeof (Elf_External_Vernaux);
6621 }
6622
6623 s->size = size;
6624 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6625 if (s->contents == NULL)
6626 return FALSE;
6627
6628 p = s->contents;
6629 for (vn = elf_tdata (output_bfd)->verref;
6630 vn != NULL;
6631 vn = vn->vn_nextref)
6632 {
6633 unsigned int caux;
6634 Elf_Internal_Vernaux *a;
6635 size_t indx;
6636
6637 caux = 0;
6638 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6639 ++caux;
6640
6641 vn->vn_version = VER_NEED_CURRENT;
6642 vn->vn_cnt = caux;
6643 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6644 elf_dt_name (vn->vn_bfd) != NULL
6645 ? elf_dt_name (vn->vn_bfd)
6646 : lbasename (vn->vn_bfd->filename),
6647 FALSE);
6648 if (indx == (size_t) -1)
6649 return FALSE;
6650 vn->vn_file = indx;
6651 vn->vn_aux = sizeof (Elf_External_Verneed);
6652 if (vn->vn_nextref == NULL)
6653 vn->vn_next = 0;
6654 else
6655 vn->vn_next = (sizeof (Elf_External_Verneed)
6656 + caux * sizeof (Elf_External_Vernaux));
6657
6658 _bfd_elf_swap_verneed_out (output_bfd, vn,
6659 (Elf_External_Verneed *) p);
6660 p += sizeof (Elf_External_Verneed);
6661
6662 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6663 {
6664 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6665 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6666 a->vna_nodename, FALSE);
6667 if (indx == (size_t) -1)
6668 return FALSE;
6669 a->vna_name = indx;
6670 if (a->vna_nextptr == NULL)
6671 a->vna_next = 0;
6672 else
6673 a->vna_next = sizeof (Elf_External_Vernaux);
6674
6675 _bfd_elf_swap_vernaux_out (output_bfd, a,
6676 (Elf_External_Vernaux *) p);
6677 p += sizeof (Elf_External_Vernaux);
6678 }
6679 }
6680
6681 elf_tdata (output_bfd)->cverrefs = crefs;
6682 }
902e9fc7
MR
6683 }
6684
6685 /* Any syms created from now on start with -1 in
6686 got.refcount/offset and plt.refcount/offset. */
6687 elf_hash_table (info)->init_got_refcount
6688 = elf_hash_table (info)->init_got_offset;
6689 elf_hash_table (info)->init_plt_refcount
6690 = elf_hash_table (info)->init_plt_offset;
6691
6692 if (bfd_link_relocatable (info)
6693 && !_bfd_elf_size_group_sections (info))
6694 return FALSE;
6695
6696 /* The backend may have to create some sections regardless of whether
6697 we're dynamic or not. */
6698 if (bed->elf_backend_always_size_sections
6699 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6700 return FALSE;
6701
6702 /* Determine any GNU_STACK segment requirements, after the backend
6703 has had a chance to set a default segment size. */
6704 if (info->execstack)
6705 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6706 else if (info->noexecstack)
6707 elf_stack_flags (output_bfd) = PF_R | PF_W;
6708 else
6709 {
6710 bfd *inputobj;
6711 asection *notesec = NULL;
6712 int exec = 0;
6713
6714 for (inputobj = info->input_bfds;
6715 inputobj;
6716 inputobj = inputobj->link.next)
6717 {
6718 asection *s;
6719
6720 if (inputobj->flags
6721 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6722 continue;
57963c05
AM
6723 s = inputobj->sections;
6724 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6725 continue;
6726
902e9fc7
MR
6727 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6728 if (s)
6729 {
6730 if (s->flags & SEC_CODE)
6731 exec = PF_X;
6732 notesec = s;
6733 }
6734 else if (bed->default_execstack)
6735 exec = PF_X;
6736 }
6737 if (notesec || info->stacksize > 0)
6738 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6739 if (notesec && exec && bfd_link_relocatable (info)
6740 && notesec->output_section != bfd_abs_section_ptr)
6741 notesec->output_section->flags |= SEC_CODE;
6742 }
6743
6744 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6745 {
6746 struct elf_info_failed eif;
6747 struct elf_link_hash_entry *h;
6748 asection *dynstr;
6749 asection *s;
6750
6751 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6752 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6753
902e9fc7
MR
6754 if (info->symbolic)
6755 {
6756 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6757 return FALSE;
6758 info->flags |= DF_SYMBOLIC;
6759 }
6760
6761 if (rpath != NULL)
6762 {
6763 size_t indx;
6764 bfd_vma tag;
6765
6766 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6767 TRUE);
6768 if (indx == (size_t) -1)
6769 return FALSE;
6770
6771 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6772 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6773 return FALSE;
6774 }
6775
6776 if (filter_shlib != NULL)
6777 {
6778 size_t indx;
6779
6780 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6781 filter_shlib, TRUE);
6782 if (indx == (size_t) -1
6783 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6784 return FALSE;
6785 }
6786
6787 if (auxiliary_filters != NULL)
6788 {
6789 const char * const *p;
6790
6791 for (p = auxiliary_filters; *p != NULL; p++)
6792 {
6793 size_t indx;
6794
6795 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6796 *p, TRUE);
6797 if (indx == (size_t) -1
6798 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6799 return FALSE;
6800 }
6801 }
6802
6803 if (audit != NULL)
6804 {
6805 size_t indx;
6806
6807 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6808 TRUE);
6809 if (indx == (size_t) -1
6810 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6811 return FALSE;
6812 }
6813
6814 if (depaudit != NULL)
6815 {
6816 size_t indx;
6817
6818 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6819 TRUE);
6820 if (indx == (size_t) -1
6821 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6822 return FALSE;
6823 }
6824
6825 eif.info = info;
6826 eif.failed = FALSE;
6827
6828 /* Find all symbols which were defined in a dynamic object and make
6829 the backend pick a reasonable value for them. */
6830 elf_link_hash_traverse (elf_hash_table (info),
6831 _bfd_elf_adjust_dynamic_symbol,
6832 &eif);
6833 if (eif.failed)
6834 return FALSE;
6835
6836 /* Add some entries to the .dynamic section. We fill in some of the
6837 values later, in bfd_elf_final_link, but we must add the entries
6838 now so that we know the final size of the .dynamic section. */
6839
6840 /* If there are initialization and/or finalization functions to
6841 call then add the corresponding DT_INIT/DT_FINI entries. */
6842 h = (info->init_function
6843 ? elf_link_hash_lookup (elf_hash_table (info),
6844 info->init_function, FALSE,
6845 FALSE, FALSE)
6846 : NULL);
6847 if (h != NULL
6848 && (h->ref_regular
6849 || h->def_regular))
6850 {
6851 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6852 return FALSE;
6853 }
6854 h = (info->fini_function
6855 ? elf_link_hash_lookup (elf_hash_table (info),
6856 info->fini_function, FALSE,
6857 FALSE, FALSE)
6858 : NULL);
6859 if (h != NULL
6860 && (h->ref_regular
6861 || h->def_regular))
6862 {
6863 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6864 return FALSE;
6865 }
6866
6867 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6868 if (s != NULL && s->linker_has_input)
6869 {
6870 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6871 if (! bfd_link_executable (info))
6872 {
6873 bfd *sub;
6874 asection *o;
6875
57963c05
AM
6876 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6877 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6878 && (o = sub->sections) != NULL
6879 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6880 for (o = sub->sections; o != NULL; o = o->next)
6881 if (elf_section_data (o)->this_hdr.sh_type
6882 == SHT_PREINIT_ARRAY)
6883 {
6884 _bfd_error_handler
871b3ab2 6885 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6886 sub);
6887 break;
6888 }
6889
6890 bfd_set_error (bfd_error_nonrepresentable_section);
6891 return FALSE;
6892 }
6893
6894 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6895 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6896 return FALSE;
6897 }
6898 s = bfd_get_section_by_name (output_bfd, ".init_array");
6899 if (s != NULL && s->linker_has_input)
6900 {
6901 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6902 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6903 return FALSE;
6904 }
6905 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6906 if (s != NULL && s->linker_has_input)
6907 {
6908 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6909 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6910 return FALSE;
6911 }
6912
6913 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6914 /* If .dynstr is excluded from the link, we don't want any of
6915 these tags. Strictly, we should be checking each section
6916 individually; This quick check covers for the case where
6917 someone does a /DISCARD/ : { *(*) }. */
6918 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6919 {
6920 bfd_size_type strsize;
6921
6922 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6923 if ((info->emit_hash
6924 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6925 || (info->emit_gnu_hash
6926 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6927 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6928 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6929 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6930 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6931 bed->s->sizeof_sym))
6932 return FALSE;
6933 }
6934 }
6935
6936 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6937 return FALSE;
6938
6939 /* The backend must work out the sizes of all the other dynamic
6940 sections. */
6941 if (dynobj != NULL
6942 && bed->elf_backend_size_dynamic_sections != NULL
6943 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6944 return FALSE;
6945
6946 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6947 {
902e9fc7
MR
6948 if (elf_tdata (output_bfd)->cverdefs)
6949 {
6950 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6951
6952 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6953 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6954 return FALSE;
6955 }
6956
6957 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6958 {
6959 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6960 return FALSE;
6961 }
6962 else if (info->flags & DF_BIND_NOW)
6963 {
6964 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6965 return FALSE;
6966 }
6967
6968 if (info->flags_1)
6969 {
6970 if (bfd_link_executable (info))
6971 info->flags_1 &= ~ (DF_1_INITFIRST
6972 | DF_1_NODELETE
6973 | DF_1_NOOPEN);
6974 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6975 return FALSE;
6976 }
6977
6978 if (elf_tdata (output_bfd)->cverrefs)
6979 {
6980 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6981
6982 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6983 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6984 return FALSE;
6985 }
5a580b3a 6986
8423293d
AM
6987 if ((elf_tdata (output_bfd)->cverrefs == 0
6988 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 6989 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 6990 {
902e9fc7
MR
6991 asection *s;
6992
3d4d4302 6993 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6994 s->flags |= SEC_EXCLUDE;
6995 }
6996 }
6997 return TRUE;
6998}
6999
74541ad4
AM
7000/* Find the first non-excluded output section. We'll use its
7001 section symbol for some emitted relocs. */
7002void
7003_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7004{
7005 asection *s;
7006
7007 for (s = output_bfd->sections; s != NULL; s = s->next)
7008 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7009 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7010 {
7011 elf_hash_table (info)->text_index_section = s;
7012 break;
7013 }
7014}
7015
7016/* Find two non-excluded output sections, one for code, one for data.
7017 We'll use their section symbols for some emitted relocs. */
7018void
7019_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7020{
7021 asection *s;
7022
266b05cf 7023 /* Data first, since setting text_index_section changes
7f923b7f 7024 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7025 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7026 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7027 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7028 {
266b05cf 7029 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7030 break;
7031 }
7032
7033 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7034 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7035 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7036 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7037 {
266b05cf 7038 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7039 break;
7040 }
7041
7042 if (elf_hash_table (info)->text_index_section == NULL)
7043 elf_hash_table (info)->text_index_section
7044 = elf_hash_table (info)->data_index_section;
7045}
7046
8423293d
AM
7047bfd_boolean
7048bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7049{
74541ad4 7050 const struct elf_backend_data *bed;
23ec1e32 7051 unsigned long section_sym_count;
96d01d93 7052 bfd_size_type dynsymcount = 0;
74541ad4 7053
8423293d
AM
7054 if (!is_elf_hash_table (info->hash))
7055 return TRUE;
7056
74541ad4
AM
7057 bed = get_elf_backend_data (output_bfd);
7058 (*bed->elf_backend_init_index_section) (output_bfd, info);
7059
23ec1e32
MR
7060 /* Assign dynsym indices. In a shared library we generate a section
7061 symbol for each output section, which come first. Next come all
7062 of the back-end allocated local dynamic syms, followed by the rest
7063 of the global symbols.
7064
7065 This is usually not needed for static binaries, however backends
7066 can request to always do it, e.g. the MIPS backend uses dynamic
7067 symbol counts to lay out GOT, which will be produced in the
7068 presence of GOT relocations even in static binaries (holding fixed
7069 data in that case, to satisfy those relocations). */
7070
7071 if (elf_hash_table (info)->dynamic_sections_created
7072 || bed->always_renumber_dynsyms)
7073 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7074 &section_sym_count);
7075
8423293d
AM
7076 if (elf_hash_table (info)->dynamic_sections_created)
7077 {
7078 bfd *dynobj;
8423293d 7079 asection *s;
8423293d
AM
7080 unsigned int dtagcount;
7081
7082 dynobj = elf_hash_table (info)->dynobj;
7083
5a580b3a 7084 /* Work out the size of the symbol version section. */
3d4d4302 7085 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7086 BFD_ASSERT (s != NULL);
d5486c43 7087 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7088 {
eea6121a 7089 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7090 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7091 if (s->contents == NULL)
7092 return FALSE;
7093
7094 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7095 return FALSE;
7096 }
7097
7098 /* Set the size of the .dynsym and .hash sections. We counted
7099 the number of dynamic symbols in elf_link_add_object_symbols.
7100 We will build the contents of .dynsym and .hash when we build
7101 the final symbol table, because until then we do not know the
7102 correct value to give the symbols. We built the .dynstr
7103 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7104 s = elf_hash_table (info)->dynsym;
5a580b3a 7105 BFD_ASSERT (s != NULL);
eea6121a 7106 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7107
d5486c43
L
7108 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7109 if (s->contents == NULL)
7110 return FALSE;
5a580b3a 7111
d5486c43
L
7112 /* The first entry in .dynsym is a dummy symbol. Clear all the
7113 section syms, in case we don't output them all. */
7114 ++section_sym_count;
7115 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7116
fdc90cb4
JJ
7117 elf_hash_table (info)->bucketcount = 0;
7118
5a580b3a
AM
7119 /* Compute the size of the hashing table. As a side effect this
7120 computes the hash values for all the names we export. */
fdc90cb4
JJ
7121 if (info->emit_hash)
7122 {
7123 unsigned long int *hashcodes;
14b1c01e 7124 struct hash_codes_info hashinf;
fdc90cb4
JJ
7125 bfd_size_type amt;
7126 unsigned long int nsyms;
7127 size_t bucketcount;
7128 size_t hash_entry_size;
7129
7130 /* Compute the hash values for all exported symbols. At the same
7131 time store the values in an array so that we could use them for
7132 optimizations. */
7133 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7134 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7135 if (hashcodes == NULL)
7136 return FALSE;
14b1c01e
AM
7137 hashinf.hashcodes = hashcodes;
7138 hashinf.error = FALSE;
5a580b3a 7139
fdc90cb4
JJ
7140 /* Put all hash values in HASHCODES. */
7141 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7142 elf_collect_hash_codes, &hashinf);
7143 if (hashinf.error)
4dd07732
AM
7144 {
7145 free (hashcodes);
7146 return FALSE;
7147 }
5a580b3a 7148
14b1c01e 7149 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7150 bucketcount
7151 = compute_bucket_count (info, hashcodes, nsyms, 0);
7152 free (hashcodes);
7153
4b48e2f6 7154 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7155 return FALSE;
5a580b3a 7156
fdc90cb4
JJ
7157 elf_hash_table (info)->bucketcount = bucketcount;
7158
3d4d4302 7159 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7160 BFD_ASSERT (s != NULL);
7161 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7162 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7163 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7164 if (s->contents == NULL)
7165 return FALSE;
7166
7167 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7168 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7169 s->contents + hash_entry_size);
7170 }
7171
7172 if (info->emit_gnu_hash)
7173 {
7174 size_t i, cnt;
7175 unsigned char *contents;
7176 struct collect_gnu_hash_codes cinfo;
7177 bfd_size_type amt;
7178 size_t bucketcount;
7179
7180 memset (&cinfo, 0, sizeof (cinfo));
7181
7182 /* Compute the hash values for all exported symbols. At the same
7183 time store the values in an array so that we could use them for
7184 optimizations. */
7185 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7186 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7187 if (cinfo.hashcodes == NULL)
7188 return FALSE;
7189
7190 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7191 cinfo.min_dynindx = -1;
7192 cinfo.output_bfd = output_bfd;
7193 cinfo.bed = bed;
7194
7195 /* Put all hash values in HASHCODES. */
7196 elf_link_hash_traverse (elf_hash_table (info),
7197 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7198 if (cinfo.error)
4dd07732
AM
7199 {
7200 free (cinfo.hashcodes);
7201 return FALSE;
7202 }
fdc90cb4
JJ
7203
7204 bucketcount
7205 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7206
7207 if (bucketcount == 0)
7208 {
7209 free (cinfo.hashcodes);
7210 return FALSE;
7211 }
7212
3d4d4302 7213 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7214 BFD_ASSERT (s != NULL);
7215
7216 if (cinfo.nsyms == 0)
7217 {
7218 /* Empty .gnu.hash section is special. */
7219 BFD_ASSERT (cinfo.min_dynindx == -1);
7220 free (cinfo.hashcodes);
7221 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7222 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7223 if (contents == NULL)
7224 return FALSE;
7225 s->contents = contents;
7226 /* 1 empty bucket. */
7227 bfd_put_32 (output_bfd, 1, contents);
7228 /* SYMIDX above the special symbol 0. */
7229 bfd_put_32 (output_bfd, 1, contents + 4);
7230 /* Just one word for bitmask. */
7231 bfd_put_32 (output_bfd, 1, contents + 8);
7232 /* Only hash fn bloom filter. */
7233 bfd_put_32 (output_bfd, 0, contents + 12);
7234 /* No hashes are valid - empty bitmask. */
7235 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7236 /* No hashes in the only bucket. */
7237 bfd_put_32 (output_bfd, 0,
7238 contents + 16 + bed->s->arch_size / 8);
7239 }
7240 else
7241 {
9e6619e2 7242 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7243 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7244
9e6619e2
AM
7245 x = cinfo.nsyms;
7246 maskbitslog2 = 1;
7247 while ((x >>= 1) != 0)
7248 ++maskbitslog2;
fdc90cb4
JJ
7249 if (maskbitslog2 < 3)
7250 maskbitslog2 = 5;
7251 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7252 maskbitslog2 = maskbitslog2 + 3;
7253 else
7254 maskbitslog2 = maskbitslog2 + 2;
7255 if (bed->s->arch_size == 64)
7256 {
7257 if (maskbitslog2 == 5)
7258 maskbitslog2 = 6;
7259 cinfo.shift1 = 6;
7260 }
7261 else
7262 cinfo.shift1 = 5;
7263 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7264 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7265 cinfo.maskbits = 1 << maskbitslog2;
7266 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7267 amt = bucketcount * sizeof (unsigned long int) * 2;
7268 amt += maskwords * sizeof (bfd_vma);
a50b1753 7269 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7270 if (cinfo.bitmask == NULL)
7271 {
7272 free (cinfo.hashcodes);
7273 return FALSE;
7274 }
7275
a50b1753 7276 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7277 cinfo.indx = cinfo.counts + bucketcount;
7278 cinfo.symindx = dynsymcount - cinfo.nsyms;
7279 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7280
7281 /* Determine how often each hash bucket is used. */
7282 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7283 for (i = 0; i < cinfo.nsyms; ++i)
7284 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7285
7286 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7287 if (cinfo.counts[i] != 0)
7288 {
7289 cinfo.indx[i] = cnt;
7290 cnt += cinfo.counts[i];
7291 }
7292 BFD_ASSERT (cnt == dynsymcount);
7293 cinfo.bucketcount = bucketcount;
7294 cinfo.local_indx = cinfo.min_dynindx;
7295
7296 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7297 s->size += cinfo.maskbits / 8;
a50b1753 7298 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7299 if (contents == NULL)
7300 {
7301 free (cinfo.bitmask);
7302 free (cinfo.hashcodes);
7303 return FALSE;
7304 }
7305
7306 s->contents = contents;
7307 bfd_put_32 (output_bfd, bucketcount, contents);
7308 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7309 bfd_put_32 (output_bfd, maskwords, contents + 8);
7310 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7311 contents += 16 + cinfo.maskbits / 8;
7312
7313 for (i = 0; i < bucketcount; ++i)
7314 {
7315 if (cinfo.counts[i] == 0)
7316 bfd_put_32 (output_bfd, 0, contents);
7317 else
7318 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7319 contents += 4;
7320 }
7321
7322 cinfo.contents = contents;
7323
7324 /* Renumber dynamic symbols, populate .gnu.hash section. */
7325 elf_link_hash_traverse (elf_hash_table (info),
7326 elf_renumber_gnu_hash_syms, &cinfo);
7327
7328 contents = s->contents + 16;
7329 for (i = 0; i < maskwords; ++i)
7330 {
7331 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7332 contents);
7333 contents += bed->s->arch_size / 8;
7334 }
7335
7336 free (cinfo.bitmask);
7337 free (cinfo.hashcodes);
7338 }
7339 }
5a580b3a 7340
3d4d4302 7341 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7342 BFD_ASSERT (s != NULL);
7343
4ad4eba5 7344 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7345
eea6121a 7346 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7347
7348 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7349 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7350 return FALSE;
7351 }
7352
7353 return TRUE;
7354}
4d269e42 7355\f
4d269e42
AM
7356/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7357
7358static void
7359merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7360 asection *sec)
7361{
dbaa2011
AM
7362 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7363 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7364}
7365
7366/* Finish SHF_MERGE section merging. */
7367
7368bfd_boolean
630993ec 7369_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7370{
7371 bfd *ibfd;
7372 asection *sec;
7373
7374 if (!is_elf_hash_table (info->hash))
7375 return FALSE;
7376
c72f2fb2 7377 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7378 if ((ibfd->flags & DYNAMIC) == 0
7379 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7380 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7381 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7382 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7383 if ((sec->flags & SEC_MERGE) != 0
7384 && !bfd_is_abs_section (sec->output_section))
7385 {
7386 struct bfd_elf_section_data *secdata;
7387
7388 secdata = elf_section_data (sec);
630993ec 7389 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7390 &elf_hash_table (info)->merge_info,
7391 sec, &secdata->sec_info))
7392 return FALSE;
7393 else if (secdata->sec_info)
dbaa2011 7394 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7395 }
7396
7397 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7398 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7399 merge_sections_remove_hook);
7400 return TRUE;
7401}
7402
7403/* Create an entry in an ELF linker hash table. */
7404
7405struct bfd_hash_entry *
7406_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7407 struct bfd_hash_table *table,
7408 const char *string)
7409{
7410 /* Allocate the structure if it has not already been allocated by a
7411 subclass. */
7412 if (entry == NULL)
7413 {
a50b1753 7414 entry = (struct bfd_hash_entry *)
ca4be51c 7415 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7416 if (entry == NULL)
7417 return entry;
7418 }
7419
7420 /* Call the allocation method of the superclass. */
7421 entry = _bfd_link_hash_newfunc (entry, table, string);
7422 if (entry != NULL)
7423 {
7424 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7425 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7426
7427 /* Set local fields. */
7428 ret->indx = -1;
7429 ret->dynindx = -1;
7430 ret->got = htab->init_got_refcount;
7431 ret->plt = htab->init_plt_refcount;
7432 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7433 - offsetof (struct elf_link_hash_entry, size)));
7434 /* Assume that we have been called by a non-ELF symbol reader.
7435 This flag is then reset by the code which reads an ELF input
7436 file. This ensures that a symbol created by a non-ELF symbol
7437 reader will have the flag set correctly. */
7438 ret->non_elf = 1;
7439 }
7440
7441 return entry;
7442}
7443
7444/* Copy data from an indirect symbol to its direct symbol, hiding the
7445 old indirect symbol. Also used for copying flags to a weakdef. */
7446
7447void
7448_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7449 struct elf_link_hash_entry *dir,
7450 struct elf_link_hash_entry *ind)
7451{
7452 struct elf_link_hash_table *htab;
7453
7454 /* Copy down any references that we may have already seen to the
e81830c5 7455 symbol which just became indirect. */
4d269e42 7456
422f1182 7457 if (dir->versioned != versioned_hidden)
e81830c5
AM
7458 dir->ref_dynamic |= ind->ref_dynamic;
7459 dir->ref_regular |= ind->ref_regular;
7460 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7461 dir->non_got_ref |= ind->non_got_ref;
7462 dir->needs_plt |= ind->needs_plt;
7463 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7464
7465 if (ind->root.type != bfd_link_hash_indirect)
7466 return;
7467
7468 /* Copy over the global and procedure linkage table refcount entries.
7469 These may have been already set up by a check_relocs routine. */
7470 htab = elf_hash_table (info);
7471 if (ind->got.refcount > htab->init_got_refcount.refcount)
7472 {
7473 if (dir->got.refcount < 0)
7474 dir->got.refcount = 0;
7475 dir->got.refcount += ind->got.refcount;
7476 ind->got.refcount = htab->init_got_refcount.refcount;
7477 }
7478
7479 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7480 {
7481 if (dir->plt.refcount < 0)
7482 dir->plt.refcount = 0;
7483 dir->plt.refcount += ind->plt.refcount;
7484 ind->plt.refcount = htab->init_plt_refcount.refcount;
7485 }
7486
7487 if (ind->dynindx != -1)
7488 {
7489 if (dir->dynindx != -1)
7490 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7491 dir->dynindx = ind->dynindx;
7492 dir->dynstr_index = ind->dynstr_index;
7493 ind->dynindx = -1;
7494 ind->dynstr_index = 0;
7495 }
7496}
7497
7498void
7499_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7500 struct elf_link_hash_entry *h,
7501 bfd_boolean force_local)
7502{
3aa14d16
L
7503 /* STT_GNU_IFUNC symbol must go through PLT. */
7504 if (h->type != STT_GNU_IFUNC)
7505 {
7506 h->plt = elf_hash_table (info)->init_plt_offset;
7507 h->needs_plt = 0;
7508 }
4d269e42
AM
7509 if (force_local)
7510 {
7511 h->forced_local = 1;
7512 if (h->dynindx != -1)
7513 {
4d269e42
AM
7514 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7515 h->dynstr_index);
641338d8
AM
7516 h->dynindx = -1;
7517 h->dynstr_index = 0;
4d269e42
AM
7518 }
7519 }
7520}
7521
34a87bb0
L
7522/* Hide a symbol. */
7523
7524void
7525_bfd_elf_link_hide_symbol (bfd *output_bfd,
7526 struct bfd_link_info *info,
7527 struct bfd_link_hash_entry *h)
7528{
7529 if (is_elf_hash_table (info->hash))
7530 {
7531 const struct elf_backend_data *bed
7532 = get_elf_backend_data (output_bfd);
7533 struct elf_link_hash_entry *eh
7534 = (struct elf_link_hash_entry *) h;
7535 bed->elf_backend_hide_symbol (info, eh, TRUE);
7536 eh->def_dynamic = 0;
7537 eh->ref_dynamic = 0;
7538 eh->dynamic_def = 0;
7539 }
7540}
7541
7bf52ea2
AM
7542/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7543 caller. */
4d269e42
AM
7544
7545bfd_boolean
7546_bfd_elf_link_hash_table_init
7547 (struct elf_link_hash_table *table,
7548 bfd *abfd,
7549 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7550 struct bfd_hash_table *,
7551 const char *),
4dfe6ac6
NC
7552 unsigned int entsize,
7553 enum elf_target_id target_id)
4d269e42
AM
7554{
7555 bfd_boolean ret;
7556 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7557
4d269e42
AM
7558 table->init_got_refcount.refcount = can_refcount - 1;
7559 table->init_plt_refcount.refcount = can_refcount - 1;
7560 table->init_got_offset.offset = -(bfd_vma) 1;
7561 table->init_plt_offset.offset = -(bfd_vma) 1;
7562 /* The first dynamic symbol is a dummy. */
7563 table->dynsymcount = 1;
7564
7565 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7566
4d269e42 7567 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7568 table->hash_table_id = target_id;
4d269e42
AM
7569
7570 return ret;
7571}
7572
7573/* Create an ELF linker hash table. */
7574
7575struct bfd_link_hash_table *
7576_bfd_elf_link_hash_table_create (bfd *abfd)
7577{
7578 struct elf_link_hash_table *ret;
7579 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7580
7bf52ea2 7581 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7582 if (ret == NULL)
7583 return NULL;
7584
7585 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7586 sizeof (struct elf_link_hash_entry),
7587 GENERIC_ELF_DATA))
4d269e42
AM
7588 {
7589 free (ret);
7590 return NULL;
7591 }
d495ab0d 7592 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7593
7594 return &ret->root;
7595}
7596
9f7c3e5e
AM
7597/* Destroy an ELF linker hash table. */
7598
7599void
d495ab0d 7600_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7601{
d495ab0d
AM
7602 struct elf_link_hash_table *htab;
7603
7604 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7605 if (htab->dynstr != NULL)
7606 _bfd_elf_strtab_free (htab->dynstr);
7607 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7608 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7609}
7610
4d269e42
AM
7611/* This is a hook for the ELF emulation code in the generic linker to
7612 tell the backend linker what file name to use for the DT_NEEDED
7613 entry for a dynamic object. */
7614
7615void
7616bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7617{
7618 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7619 && bfd_get_format (abfd) == bfd_object)
7620 elf_dt_name (abfd) = name;
7621}
7622
7623int
7624bfd_elf_get_dyn_lib_class (bfd *abfd)
7625{
7626 int lib_class;
7627 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7628 && bfd_get_format (abfd) == bfd_object)
7629 lib_class = elf_dyn_lib_class (abfd);
7630 else
7631 lib_class = 0;
7632 return lib_class;
7633}
7634
7635void
7636bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7637{
7638 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7639 && bfd_get_format (abfd) == bfd_object)
7640 elf_dyn_lib_class (abfd) = lib_class;
7641}
7642
7643/* Get the list of DT_NEEDED entries for a link. This is a hook for
7644 the linker ELF emulation code. */
7645
7646struct bfd_link_needed_list *
7647bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7648 struct bfd_link_info *info)
7649{
7650 if (! is_elf_hash_table (info->hash))
7651 return NULL;
7652 return elf_hash_table (info)->needed;
7653}
7654
7655/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7656 hook for the linker ELF emulation code. */
7657
7658struct bfd_link_needed_list *
7659bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7660 struct bfd_link_info *info)
7661{
7662 if (! is_elf_hash_table (info->hash))
7663 return NULL;
7664 return elf_hash_table (info)->runpath;
7665}
7666
7667/* Get the name actually used for a dynamic object for a link. This
7668 is the SONAME entry if there is one. Otherwise, it is the string
7669 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7670
7671const char *
7672bfd_elf_get_dt_soname (bfd *abfd)
7673{
7674 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7675 && bfd_get_format (abfd) == bfd_object)
7676 return elf_dt_name (abfd);
7677 return NULL;
7678}
7679
7680/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7681 the ELF linker emulation code. */
7682
7683bfd_boolean
7684bfd_elf_get_bfd_needed_list (bfd *abfd,
7685 struct bfd_link_needed_list **pneeded)
7686{
7687 asection *s;
7688 bfd_byte *dynbuf = NULL;
cb33740c 7689 unsigned int elfsec;
4d269e42
AM
7690 unsigned long shlink;
7691 bfd_byte *extdyn, *extdynend;
7692 size_t extdynsize;
7693 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7694
7695 *pneeded = NULL;
7696
7697 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7698 || bfd_get_format (abfd) != bfd_object)
7699 return TRUE;
7700
7701 s = bfd_get_section_by_name (abfd, ".dynamic");
7702 if (s == NULL || s->size == 0)
7703 return TRUE;
7704
7705 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7706 goto error_return;
7707
7708 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7709 if (elfsec == SHN_BAD)
4d269e42
AM
7710 goto error_return;
7711
7712 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7713
4d269e42
AM
7714 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7715 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7716
7717 extdyn = dynbuf;
7718 extdynend = extdyn + s->size;
7719 for (; extdyn < extdynend; extdyn += extdynsize)
7720 {
7721 Elf_Internal_Dyn dyn;
7722
7723 (*swap_dyn_in) (abfd, extdyn, &dyn);
7724
7725 if (dyn.d_tag == DT_NULL)
7726 break;
7727
7728 if (dyn.d_tag == DT_NEEDED)
7729 {
7730 const char *string;
7731 struct bfd_link_needed_list *l;
7732 unsigned int tagv = dyn.d_un.d_val;
7733 bfd_size_type amt;
7734
7735 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7736 if (string == NULL)
7737 goto error_return;
7738
7739 amt = sizeof *l;
a50b1753 7740 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7741 if (l == NULL)
7742 goto error_return;
7743
7744 l->by = abfd;
7745 l->name = string;
7746 l->next = *pneeded;
7747 *pneeded = l;
7748 }
7749 }
7750
7751 free (dynbuf);
7752
7753 return TRUE;
7754
7755 error_return:
7756 if (dynbuf != NULL)
7757 free (dynbuf);
7758 return FALSE;
7759}
7760
7761struct elf_symbuf_symbol
7762{
7763 unsigned long st_name; /* Symbol name, index in string tbl */
7764 unsigned char st_info; /* Type and binding attributes */
7765 unsigned char st_other; /* Visibilty, and target specific */
7766};
7767
7768struct elf_symbuf_head
7769{
7770 struct elf_symbuf_symbol *ssym;
ef53be89 7771 size_t count;
4d269e42
AM
7772 unsigned int st_shndx;
7773};
7774
7775struct elf_symbol
7776{
7777 union
7778 {
7779 Elf_Internal_Sym *isym;
7780 struct elf_symbuf_symbol *ssym;
7781 } u;
7782 const char *name;
7783};
7784
7785/* Sort references to symbols by ascending section number. */
7786
7787static int
7788elf_sort_elf_symbol (const void *arg1, const void *arg2)
7789{
7790 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7791 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7792
7793 return s1->st_shndx - s2->st_shndx;
7794}
7795
7796static int
7797elf_sym_name_compare (const void *arg1, const void *arg2)
7798{
7799 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7800 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7801 return strcmp (s1->name, s2->name);
7802}
7803
7804static struct elf_symbuf_head *
ef53be89 7805elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7806{
14b1c01e 7807 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7808 struct elf_symbuf_symbol *ssym;
7809 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7810 size_t i, shndx_count, total_size;
4d269e42 7811
a50b1753 7812 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7813 if (indbuf == NULL)
7814 return NULL;
7815
7816 for (ind = indbuf, i = 0; i < symcount; i++)
7817 if (isymbuf[i].st_shndx != SHN_UNDEF)
7818 *ind++ = &isymbuf[i];
7819 indbufend = ind;
7820
7821 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7822 elf_sort_elf_symbol);
7823
7824 shndx_count = 0;
7825 if (indbufend > indbuf)
7826 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7827 if (ind[0]->st_shndx != ind[1]->st_shndx)
7828 shndx_count++;
7829
3ae181ee
L
7830 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7831 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7832 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7833 if (ssymbuf == NULL)
7834 {
7835 free (indbuf);
7836 return NULL;
7837 }
7838
3ae181ee 7839 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7840 ssymbuf->ssym = NULL;
7841 ssymbuf->count = shndx_count;
7842 ssymbuf->st_shndx = 0;
7843 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7844 {
7845 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7846 {
7847 ssymhead++;
7848 ssymhead->ssym = ssym;
7849 ssymhead->count = 0;
7850 ssymhead->st_shndx = (*ind)->st_shndx;
7851 }
7852 ssym->st_name = (*ind)->st_name;
7853 ssym->st_info = (*ind)->st_info;
7854 ssym->st_other = (*ind)->st_other;
7855 ssymhead->count++;
7856 }
ef53be89 7857 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7858 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7859 == total_size));
4d269e42
AM
7860
7861 free (indbuf);
7862 return ssymbuf;
7863}
7864
7865/* Check if 2 sections define the same set of local and global
7866 symbols. */
7867
8f317e31 7868static bfd_boolean
4d269e42
AM
7869bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7870 struct bfd_link_info *info)
7871{
7872 bfd *bfd1, *bfd2;
7873 const struct elf_backend_data *bed1, *bed2;
7874 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7875 size_t symcount1, symcount2;
4d269e42
AM
7876 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7877 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7878 Elf_Internal_Sym *isym, *isymend;
7879 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7880 size_t count1, count2, i;
cb33740c 7881 unsigned int shndx1, shndx2;
4d269e42
AM
7882 bfd_boolean result;
7883
7884 bfd1 = sec1->owner;
7885 bfd2 = sec2->owner;
7886
4d269e42
AM
7887 /* Both sections have to be in ELF. */
7888 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7889 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7890 return FALSE;
7891
7892 if (elf_section_type (sec1) != elf_section_type (sec2))
7893 return FALSE;
7894
4d269e42
AM
7895 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7896 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7897 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7898 return FALSE;
7899
7900 bed1 = get_elf_backend_data (bfd1);
7901 bed2 = get_elf_backend_data (bfd2);
7902 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7903 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7904 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7905 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7906
7907 if (symcount1 == 0 || symcount2 == 0)
7908 return FALSE;
7909
7910 result = FALSE;
7911 isymbuf1 = NULL;
7912 isymbuf2 = NULL;
a50b1753
NC
7913 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7914 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7915
7916 if (ssymbuf1 == NULL)
7917 {
7918 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7919 NULL, NULL, NULL);
7920 if (isymbuf1 == NULL)
7921 goto done;
7922
7923 if (!info->reduce_memory_overheads)
7924 elf_tdata (bfd1)->symbuf = ssymbuf1
7925 = elf_create_symbuf (symcount1, isymbuf1);
7926 }
7927
7928 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7929 {
7930 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7931 NULL, NULL, NULL);
7932 if (isymbuf2 == NULL)
7933 goto done;
7934
7935 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7936 elf_tdata (bfd2)->symbuf = ssymbuf2
7937 = elf_create_symbuf (symcount2, isymbuf2);
7938 }
7939
7940 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7941 {
7942 /* Optimized faster version. */
ef53be89 7943 size_t lo, hi, mid;
4d269e42
AM
7944 struct elf_symbol *symp;
7945 struct elf_symbuf_symbol *ssym, *ssymend;
7946
7947 lo = 0;
7948 hi = ssymbuf1->count;
7949 ssymbuf1++;
7950 count1 = 0;
7951 while (lo < hi)
7952 {
7953 mid = (lo + hi) / 2;
cb33740c 7954 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7955 hi = mid;
cb33740c 7956 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7957 lo = mid + 1;
7958 else
7959 {
7960 count1 = ssymbuf1[mid].count;
7961 ssymbuf1 += mid;
7962 break;
7963 }
7964 }
7965
7966 lo = 0;
7967 hi = ssymbuf2->count;
7968 ssymbuf2++;
7969 count2 = 0;
7970 while (lo < hi)
7971 {
7972 mid = (lo + hi) / 2;
cb33740c 7973 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7974 hi = mid;
cb33740c 7975 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7976 lo = mid + 1;
7977 else
7978 {
7979 count2 = ssymbuf2[mid].count;
7980 ssymbuf2 += mid;
7981 break;
7982 }
7983 }
7984
7985 if (count1 == 0 || count2 == 0 || count1 != count2)
7986 goto done;
7987
ca4be51c
AM
7988 symtable1
7989 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7990 symtable2
7991 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7992 if (symtable1 == NULL || symtable2 == NULL)
7993 goto done;
7994
7995 symp = symtable1;
7996 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7997 ssym < ssymend; ssym++, symp++)
7998 {
7999 symp->u.ssym = ssym;
8000 symp->name = bfd_elf_string_from_elf_section (bfd1,
8001 hdr1->sh_link,
8002 ssym->st_name);
8003 }
8004
8005 symp = symtable2;
8006 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8007 ssym < ssymend; ssym++, symp++)
8008 {
8009 symp->u.ssym = ssym;
8010 symp->name = bfd_elf_string_from_elf_section (bfd2,
8011 hdr2->sh_link,
8012 ssym->st_name);
8013 }
8014
8015 /* Sort symbol by name. */
8016 qsort (symtable1, count1, sizeof (struct elf_symbol),
8017 elf_sym_name_compare);
8018 qsort (symtable2, count1, sizeof (struct elf_symbol),
8019 elf_sym_name_compare);
8020
8021 for (i = 0; i < count1; i++)
8022 /* Two symbols must have the same binding, type and name. */
8023 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8024 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8025 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8026 goto done;
8027
8028 result = TRUE;
8029 goto done;
8030 }
8031
a50b1753
NC
8032 symtable1 = (struct elf_symbol *)
8033 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8034 symtable2 = (struct elf_symbol *)
8035 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8036 if (symtable1 == NULL || symtable2 == NULL)
8037 goto done;
8038
8039 /* Count definitions in the section. */
8040 count1 = 0;
8041 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8042 if (isym->st_shndx == shndx1)
4d269e42
AM
8043 symtable1[count1++].u.isym = isym;
8044
8045 count2 = 0;
8046 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8047 if (isym->st_shndx == shndx2)
4d269e42
AM
8048 symtable2[count2++].u.isym = isym;
8049
8050 if (count1 == 0 || count2 == 0 || count1 != count2)
8051 goto done;
8052
8053 for (i = 0; i < count1; i++)
8054 symtable1[i].name
8055 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8056 symtable1[i].u.isym->st_name);
8057
8058 for (i = 0; i < count2; i++)
8059 symtable2[i].name
8060 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8061 symtable2[i].u.isym->st_name);
8062
8063 /* Sort symbol by name. */
8064 qsort (symtable1, count1, sizeof (struct elf_symbol),
8065 elf_sym_name_compare);
8066 qsort (symtable2, count1, sizeof (struct elf_symbol),
8067 elf_sym_name_compare);
8068
8069 for (i = 0; i < count1; i++)
8070 /* Two symbols must have the same binding, type and name. */
8071 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8072 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8073 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8074 goto done;
8075
8076 result = TRUE;
8077
8078done:
8079 if (symtable1)
8080 free (symtable1);
8081 if (symtable2)
8082 free (symtable2);
8083 if (isymbuf1)
8084 free (isymbuf1);
8085 if (isymbuf2)
8086 free (isymbuf2);
8087
8088 return result;
8089}
8090
8091/* Return TRUE if 2 section types are compatible. */
8092
8093bfd_boolean
8094_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8095 bfd *bbfd, const asection *bsec)
8096{
8097 if (asec == NULL
8098 || bsec == NULL
8099 || abfd->xvec->flavour != bfd_target_elf_flavour
8100 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8101 return TRUE;
8102
8103 return elf_section_type (asec) == elf_section_type (bsec);
8104}
8105\f
c152c796
AM
8106/* Final phase of ELF linker. */
8107
8108/* A structure we use to avoid passing large numbers of arguments. */
8109
8110struct elf_final_link_info
8111{
8112 /* General link information. */
8113 struct bfd_link_info *info;
8114 /* Output BFD. */
8115 bfd *output_bfd;
8116 /* Symbol string table. */
ef10c3ac 8117 struct elf_strtab_hash *symstrtab;
c152c796
AM
8118 /* .hash section. */
8119 asection *hash_sec;
8120 /* symbol version section (.gnu.version). */
8121 asection *symver_sec;
8122 /* Buffer large enough to hold contents of any section. */
8123 bfd_byte *contents;
8124 /* Buffer large enough to hold external relocs of any section. */
8125 void *external_relocs;
8126 /* Buffer large enough to hold internal relocs of any section. */
8127 Elf_Internal_Rela *internal_relocs;
8128 /* Buffer large enough to hold external local symbols of any input
8129 BFD. */
8130 bfd_byte *external_syms;
8131 /* And a buffer for symbol section indices. */
8132 Elf_External_Sym_Shndx *locsym_shndx;
8133 /* Buffer large enough to hold internal local symbols of any input
8134 BFD. */
8135 Elf_Internal_Sym *internal_syms;
8136 /* Array large enough to hold a symbol index for each local symbol
8137 of any input BFD. */
8138 long *indices;
8139 /* Array large enough to hold a section pointer for each local
8140 symbol of any input BFD. */
8141 asection **sections;
ef10c3ac 8142 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8143 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8144 /* Number of STT_FILE syms seen. */
8145 size_t filesym_count;
c152c796
AM
8146};
8147
8148/* This struct is used to pass information to elf_link_output_extsym. */
8149
8150struct elf_outext_info
8151{
8152 bfd_boolean failed;
8153 bfd_boolean localsyms;
34a79995 8154 bfd_boolean file_sym_done;
8b127cbc 8155 struct elf_final_link_info *flinfo;
c152c796
AM
8156};
8157
d9352518
DB
8158
8159/* Support for evaluating a complex relocation.
8160
8161 Complex relocations are generalized, self-describing relocations. The
8162 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8163 relocations themselves.
d9352518
DB
8164
8165 The relocations are use a reserved elf-wide relocation type code (R_RELC
8166 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8167 information (start bit, end bit, word width, etc) into the addend. This
8168 information is extracted from CGEN-generated operand tables within gas.
8169
8170 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8171 internal) representing prefix-notation expressions, including but not
8172 limited to those sorts of expressions normally encoded as addends in the
8173 addend field. The symbol mangling format is:
8174
8175 <node> := <literal>
07d6d2b8
AM
8176 | <unary-operator> ':' <node>
8177 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8178 ;
8179
8180 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8181 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8182 | '#' <hexdigits>
8183 ;
8184
8185 <binary-operator> := as in C
8186 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8187
8188static void
a0c8462f
AM
8189set_symbol_value (bfd *bfd_with_globals,
8190 Elf_Internal_Sym *isymbuf,
8191 size_t locsymcount,
8192 size_t symidx,
8193 bfd_vma val)
d9352518 8194{
8977835c
AM
8195 struct elf_link_hash_entry **sym_hashes;
8196 struct elf_link_hash_entry *h;
8197 size_t extsymoff = locsymcount;
d9352518 8198
8977835c 8199 if (symidx < locsymcount)
d9352518 8200 {
8977835c
AM
8201 Elf_Internal_Sym *sym;
8202
8203 sym = isymbuf + symidx;
8204 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8205 {
8206 /* It is a local symbol: move it to the
8207 "absolute" section and give it a value. */
8208 sym->st_shndx = SHN_ABS;
8209 sym->st_value = val;
8210 return;
8211 }
8212 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8213 extsymoff = 0;
d9352518 8214 }
8977835c
AM
8215
8216 /* It is a global symbol: set its link type
8217 to "defined" and give it a value. */
8218
8219 sym_hashes = elf_sym_hashes (bfd_with_globals);
8220 h = sym_hashes [symidx - extsymoff];
8221 while (h->root.type == bfd_link_hash_indirect
8222 || h->root.type == bfd_link_hash_warning)
8223 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8224 h->root.type = bfd_link_hash_defined;
8225 h->root.u.def.value = val;
8226 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8227}
8228
a0c8462f
AM
8229static bfd_boolean
8230resolve_symbol (const char *name,
8231 bfd *input_bfd,
8b127cbc 8232 struct elf_final_link_info *flinfo,
a0c8462f
AM
8233 bfd_vma *result,
8234 Elf_Internal_Sym *isymbuf,
8235 size_t locsymcount)
d9352518 8236{
a0c8462f
AM
8237 Elf_Internal_Sym *sym;
8238 struct bfd_link_hash_entry *global_entry;
8239 const char *candidate = NULL;
8240 Elf_Internal_Shdr *symtab_hdr;
8241 size_t i;
8242
d9352518
DB
8243 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8244
8245 for (i = 0; i < locsymcount; ++ i)
8246 {
8977835c 8247 sym = isymbuf + i;
d9352518
DB
8248
8249 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8250 continue;
8251
8252 candidate = bfd_elf_string_from_elf_section (input_bfd,
8253 symtab_hdr->sh_link,
8254 sym->st_name);
8255#ifdef DEBUG
0f02bbd9
AM
8256 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8257 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8258#endif
8259 if (candidate && strcmp (candidate, name) == 0)
8260 {
8b127cbc 8261 asection *sec = flinfo->sections [i];
d9352518 8262
0f02bbd9
AM
8263 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8264 *result += sec->output_offset + sec->output_section->vma;
d9352518 8265#ifdef DEBUG
0f02bbd9
AM
8266 printf ("Found symbol with value %8.8lx\n",
8267 (unsigned long) *result);
d9352518
DB
8268#endif
8269 return TRUE;
8270 }
8271 }
8272
8273 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8274 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8275 FALSE, FALSE, TRUE);
d9352518
DB
8276 if (!global_entry)
8277 return FALSE;
a0c8462f 8278
d9352518
DB
8279 if (global_entry->type == bfd_link_hash_defined
8280 || global_entry->type == bfd_link_hash_defweak)
8281 {
a0c8462f
AM
8282 *result = (global_entry->u.def.value
8283 + global_entry->u.def.section->output_section->vma
8284 + global_entry->u.def.section->output_offset);
d9352518 8285#ifdef DEBUG
0f02bbd9
AM
8286 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8287 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8288#endif
8289 return TRUE;
a0c8462f 8290 }
d9352518 8291
d9352518
DB
8292 return FALSE;
8293}
8294
37b01f6a
DG
8295/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8296 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8297 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8298
d9352518 8299static bfd_boolean
a0c8462f
AM
8300resolve_section (const char *name,
8301 asection *sections,
37b01f6a
DG
8302 bfd_vma *result,
8303 bfd * abfd)
d9352518 8304{
a0c8462f
AM
8305 asection *curr;
8306 unsigned int len;
d9352518 8307
a0c8462f 8308 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8309 if (strcmp (curr->name, name) == 0)
8310 {
8311 *result = curr->vma;
8312 return TRUE;
8313 }
8314
8315 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8316 /* FIXME: This could be coded more efficiently... */
a0c8462f 8317 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8318 {
8319 len = strlen (curr->name);
a0c8462f 8320 if (len > strlen (name))
d9352518
DB
8321 continue;
8322
8323 if (strncmp (curr->name, name, len) == 0)
8324 {
8325 if (strncmp (".end", name + len, 4) == 0)
8326 {
37b01f6a 8327 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8328 return TRUE;
8329 }
8330
8331 /* Insert more pseudo-section names here, if you like. */
8332 }
8333 }
a0c8462f 8334
d9352518
DB
8335 return FALSE;
8336}
8337
8338static void
a0c8462f 8339undefined_reference (const char *reftype, const char *name)
d9352518 8340{
695344c0 8341 /* xgettext:c-format */
a0c8462f
AM
8342 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8343 reftype, name);
d9352518
DB
8344}
8345
8346static bfd_boolean
a0c8462f
AM
8347eval_symbol (bfd_vma *result,
8348 const char **symp,
8349 bfd *input_bfd,
8b127cbc 8350 struct elf_final_link_info *flinfo,
a0c8462f
AM
8351 bfd_vma dot,
8352 Elf_Internal_Sym *isymbuf,
8353 size_t locsymcount,
8354 int signed_p)
d9352518 8355{
4b93929b
NC
8356 size_t len;
8357 size_t symlen;
a0c8462f
AM
8358 bfd_vma a;
8359 bfd_vma b;
4b93929b 8360 char symbuf[4096];
0f02bbd9 8361 const char *sym = *symp;
a0c8462f
AM
8362 const char *symend;
8363 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8364
8365 len = strlen (sym);
8366 symend = sym + len;
8367
4b93929b 8368 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8369 {
8370 bfd_set_error (bfd_error_invalid_operation);
8371 return FALSE;
8372 }
a0c8462f 8373
d9352518
DB
8374 switch (* sym)
8375 {
8376 case '.':
0f02bbd9
AM
8377 *result = dot;
8378 *symp = sym + 1;
d9352518
DB
8379 return TRUE;
8380
8381 case '#':
0f02bbd9
AM
8382 ++sym;
8383 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8384 return TRUE;
8385
8386 case 'S':
8387 symbol_is_section = TRUE;
1a0670f3 8388 /* Fall through. */
a0c8462f 8389 case 's':
0f02bbd9
AM
8390 ++sym;
8391 symlen = strtol (sym, (char **) symp, 10);
8392 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8393
4b93929b 8394 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8395 {
8396 bfd_set_error (bfd_error_invalid_operation);
8397 return FALSE;
8398 }
8399
8400 memcpy (symbuf, sym, symlen);
a0c8462f 8401 symbuf[symlen] = '\0';
0f02bbd9 8402 *symp = sym + symlen;
a0c8462f
AM
8403
8404 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8405 the symbol as a section, or vice-versa. so we're pretty liberal in our
8406 interpretation here; section means "try section first", not "must be a
8407 section", and likewise with symbol. */
8408
a0c8462f 8409 if (symbol_is_section)
d9352518 8410 {
37b01f6a 8411 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8412 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8413 isymbuf, locsymcount))
d9352518
DB
8414 {
8415 undefined_reference ("section", symbuf);
8416 return FALSE;
8417 }
a0c8462f
AM
8418 }
8419 else
d9352518 8420 {
8b127cbc 8421 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8422 isymbuf, locsymcount)
8b127cbc 8423 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8424 result, input_bfd))
d9352518
DB
8425 {
8426 undefined_reference ("symbol", symbuf);
8427 return FALSE;
8428 }
8429 }
8430
8431 return TRUE;
a0c8462f 8432
d9352518
DB
8433 /* All that remains are operators. */
8434
8435#define UNARY_OP(op) \
8436 if (strncmp (sym, #op, strlen (#op)) == 0) \
8437 { \
8438 sym += strlen (#op); \
a0c8462f
AM
8439 if (*sym == ':') \
8440 ++sym; \
0f02bbd9 8441 *symp = sym; \
8b127cbc 8442 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8443 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8444 return FALSE; \
8445 if (signed_p) \
0f02bbd9 8446 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8447 else \
8448 *result = op a; \
d9352518
DB
8449 return TRUE; \
8450 }
8451
8452#define BINARY_OP(op) \
8453 if (strncmp (sym, #op, strlen (#op)) == 0) \
8454 { \
8455 sym += strlen (#op); \
a0c8462f
AM
8456 if (*sym == ':') \
8457 ++sym; \
0f02bbd9 8458 *symp = sym; \
8b127cbc 8459 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8460 isymbuf, locsymcount, signed_p)) \
a0c8462f 8461 return FALSE; \
0f02bbd9 8462 ++*symp; \
8b127cbc 8463 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8464 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8465 return FALSE; \
8466 if (signed_p) \
0f02bbd9 8467 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8468 else \
8469 *result = a op b; \
d9352518
DB
8470 return TRUE; \
8471 }
8472
8473 default:
8474 UNARY_OP (0-);
8475 BINARY_OP (<<);
8476 BINARY_OP (>>);
8477 BINARY_OP (==);
8478 BINARY_OP (!=);
8479 BINARY_OP (<=);
8480 BINARY_OP (>=);
8481 BINARY_OP (&&);
8482 BINARY_OP (||);
8483 UNARY_OP (~);
8484 UNARY_OP (!);
8485 BINARY_OP (*);
8486 BINARY_OP (/);
8487 BINARY_OP (%);
8488 BINARY_OP (^);
8489 BINARY_OP (|);
8490 BINARY_OP (&);
8491 BINARY_OP (+);
8492 BINARY_OP (-);
8493 BINARY_OP (<);
8494 BINARY_OP (>);
8495#undef UNARY_OP
8496#undef BINARY_OP
8497 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8498 bfd_set_error (bfd_error_invalid_operation);
8499 return FALSE;
8500 }
8501}
8502
d9352518 8503static void
a0c8462f
AM
8504put_value (bfd_vma size,
8505 unsigned long chunksz,
8506 bfd *input_bfd,
8507 bfd_vma x,
8508 bfd_byte *location)
d9352518
DB
8509{
8510 location += (size - chunksz);
8511
41cd1ad1 8512 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8513 {
8514 switch (chunksz)
8515 {
d9352518
DB
8516 case 1:
8517 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8518 x >>= 8;
d9352518
DB
8519 break;
8520 case 2:
8521 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8522 x >>= 16;
d9352518
DB
8523 break;
8524 case 4:
8525 bfd_put_32 (input_bfd, x, location);
65164438
NC
8526 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8527 x >>= 16;
8528 x >>= 16;
d9352518 8529 break;
d9352518 8530#ifdef BFD64
41cd1ad1 8531 case 8:
d9352518 8532 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8533 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8534 x >>= 32;
8535 x >>= 32;
8536 break;
d9352518 8537#endif
41cd1ad1
NC
8538 default:
8539 abort ();
d9352518
DB
8540 break;
8541 }
8542 }
8543}
8544
a0c8462f
AM
8545static bfd_vma
8546get_value (bfd_vma size,
8547 unsigned long chunksz,
8548 bfd *input_bfd,
8549 bfd_byte *location)
d9352518 8550{
9b239e0e 8551 int shift;
d9352518
DB
8552 bfd_vma x = 0;
8553
9b239e0e
NC
8554 /* Sanity checks. */
8555 BFD_ASSERT (chunksz <= sizeof (x)
8556 && size >= chunksz
8557 && chunksz != 0
8558 && (size % chunksz) == 0
8559 && input_bfd != NULL
8560 && location != NULL);
8561
8562 if (chunksz == sizeof (x))
8563 {
8564 BFD_ASSERT (size == chunksz);
8565
8566 /* Make sure that we do not perform an undefined shift operation.
8567 We know that size == chunksz so there will only be one iteration
8568 of the loop below. */
8569 shift = 0;
8570 }
8571 else
8572 shift = 8 * chunksz;
8573
a0c8462f 8574 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8575 {
8576 switch (chunksz)
8577 {
d9352518 8578 case 1:
9b239e0e 8579 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8580 break;
8581 case 2:
9b239e0e 8582 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8583 break;
8584 case 4:
9b239e0e 8585 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8586 break;
d9352518 8587#ifdef BFD64
9b239e0e
NC
8588 case 8:
8589 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8590 break;
9b239e0e
NC
8591#endif
8592 default:
8593 abort ();
d9352518
DB
8594 }
8595 }
8596 return x;
8597}
8598
a0c8462f
AM
8599static void
8600decode_complex_addend (unsigned long *start, /* in bits */
8601 unsigned long *oplen, /* in bits */
8602 unsigned long *len, /* in bits */
8603 unsigned long *wordsz, /* in bytes */
8604 unsigned long *chunksz, /* in bytes */
8605 unsigned long *lsb0_p,
8606 unsigned long *signed_p,
8607 unsigned long *trunc_p,
8608 unsigned long encoded)
d9352518 8609{
07d6d2b8
AM
8610 * start = encoded & 0x3F;
8611 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8612 * oplen = (encoded >> 12) & 0x3F;
8613 * wordsz = (encoded >> 18) & 0xF;
8614 * chunksz = (encoded >> 22) & 0xF;
8615 * lsb0_p = (encoded >> 27) & 1;
8616 * signed_p = (encoded >> 28) & 1;
8617 * trunc_p = (encoded >> 29) & 1;
8618}
8619
cdfeee4f 8620bfd_reloc_status_type
0f02bbd9 8621bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8622 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8623 bfd_byte *contents,
8624 Elf_Internal_Rela *rel,
8625 bfd_vma relocation)
d9352518 8626{
0f02bbd9
AM
8627 bfd_vma shift, x, mask;
8628 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8629 bfd_reloc_status_type r;
d9352518
DB
8630
8631 /* Perform this reloc, since it is complex.
8632 (this is not to say that it necessarily refers to a complex
8633 symbol; merely that it is a self-describing CGEN based reloc.
8634 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8635 word size, etc) encoded within it.). */
d9352518 8636
a0c8462f
AM
8637 decode_complex_addend (&start, &oplen, &len, &wordsz,
8638 &chunksz, &lsb0_p, &signed_p,
8639 &trunc_p, rel->r_addend);
d9352518
DB
8640
8641 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8642
8643 if (lsb0_p)
8644 shift = (start + 1) - len;
8645 else
8646 shift = (8 * wordsz) - (start + len);
8647
37b01f6a
DG
8648 x = get_value (wordsz, chunksz, input_bfd,
8649 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8650
8651#ifdef DEBUG
8652 printf ("Doing complex reloc: "
8653 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8654 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8655 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8656 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8657 oplen, (unsigned long) x, (unsigned long) mask,
8658 (unsigned long) relocation);
d9352518
DB
8659#endif
8660
cdfeee4f 8661 r = bfd_reloc_ok;
d9352518 8662 if (! trunc_p)
cdfeee4f
AM
8663 /* Now do an overflow check. */
8664 r = bfd_check_overflow ((signed_p
8665 ? complain_overflow_signed
8666 : complain_overflow_unsigned),
8667 len, 0, (8 * wordsz),
8668 relocation);
a0c8462f 8669
d9352518
DB
8670 /* Do the deed. */
8671 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8672
8673#ifdef DEBUG
8674 printf (" relocation: %8.8lx\n"
8675 " shifted mask: %8.8lx\n"
8676 " shifted/masked reloc: %8.8lx\n"
8677 " result: %8.8lx\n",
9ccb8af9
AM
8678 (unsigned long) relocation, (unsigned long) (mask << shift),
8679 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8680#endif
37b01f6a
DG
8681 put_value (wordsz, chunksz, input_bfd, x,
8682 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8683 return r;
d9352518
DB
8684}
8685
0e287786
AM
8686/* Functions to read r_offset from external (target order) reloc
8687 entry. Faster than bfd_getl32 et al, because we let the compiler
8688 know the value is aligned. */
53df40a4 8689
0e287786
AM
8690static bfd_vma
8691ext32l_r_offset (const void *p)
53df40a4
AM
8692{
8693 union aligned32
8694 {
8695 uint32_t v;
8696 unsigned char c[4];
8697 };
8698 const union aligned32 *a
0e287786 8699 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8700
8701 uint32_t aval = ( (uint32_t) a->c[0]
8702 | (uint32_t) a->c[1] << 8
8703 | (uint32_t) a->c[2] << 16
8704 | (uint32_t) a->c[3] << 24);
0e287786 8705 return aval;
53df40a4
AM
8706}
8707
0e287786
AM
8708static bfd_vma
8709ext32b_r_offset (const void *p)
53df40a4
AM
8710{
8711 union aligned32
8712 {
8713 uint32_t v;
8714 unsigned char c[4];
8715 };
8716 const union aligned32 *a
0e287786 8717 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8718
8719 uint32_t aval = ( (uint32_t) a->c[0] << 24
8720 | (uint32_t) a->c[1] << 16
8721 | (uint32_t) a->c[2] << 8
8722 | (uint32_t) a->c[3]);
0e287786 8723 return aval;
53df40a4
AM
8724}
8725
8726#ifdef BFD_HOST_64_BIT
0e287786
AM
8727static bfd_vma
8728ext64l_r_offset (const void *p)
53df40a4
AM
8729{
8730 union aligned64
8731 {
8732 uint64_t v;
8733 unsigned char c[8];
8734 };
8735 const union aligned64 *a
0e287786 8736 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8737
8738 uint64_t aval = ( (uint64_t) a->c[0]
8739 | (uint64_t) a->c[1] << 8
8740 | (uint64_t) a->c[2] << 16
8741 | (uint64_t) a->c[3] << 24
8742 | (uint64_t) a->c[4] << 32
8743 | (uint64_t) a->c[5] << 40
8744 | (uint64_t) a->c[6] << 48
8745 | (uint64_t) a->c[7] << 56);
0e287786 8746 return aval;
53df40a4
AM
8747}
8748
0e287786
AM
8749static bfd_vma
8750ext64b_r_offset (const void *p)
53df40a4
AM
8751{
8752 union aligned64
8753 {
8754 uint64_t v;
8755 unsigned char c[8];
8756 };
8757 const union aligned64 *a
0e287786 8758 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8759
8760 uint64_t aval = ( (uint64_t) a->c[0] << 56
8761 | (uint64_t) a->c[1] << 48
8762 | (uint64_t) a->c[2] << 40
8763 | (uint64_t) a->c[3] << 32
8764 | (uint64_t) a->c[4] << 24
8765 | (uint64_t) a->c[5] << 16
8766 | (uint64_t) a->c[6] << 8
8767 | (uint64_t) a->c[7]);
0e287786 8768 return aval;
53df40a4
AM
8769}
8770#endif
8771
c152c796
AM
8772/* When performing a relocatable link, the input relocations are
8773 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8774 referenced must be updated. Update all the relocations found in
8775 RELDATA. */
c152c796 8776
bca6d0e3 8777static bfd_boolean
c152c796 8778elf_link_adjust_relocs (bfd *abfd,
9eaff861 8779 asection *sec,
28dbcedc 8780 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8781 bfd_boolean sort,
8782 struct bfd_link_info *info)
c152c796
AM
8783{
8784 unsigned int i;
8785 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8786 bfd_byte *erela;
8787 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8788 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8789 bfd_vma r_type_mask;
8790 int r_sym_shift;
d4730f92
BS
8791 unsigned int count = reldata->count;
8792 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8793
d4730f92 8794 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8795 {
8796 swap_in = bed->s->swap_reloc_in;
8797 swap_out = bed->s->swap_reloc_out;
8798 }
d4730f92 8799 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8800 {
8801 swap_in = bed->s->swap_reloca_in;
8802 swap_out = bed->s->swap_reloca_out;
8803 }
8804 else
8805 abort ();
8806
8807 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8808 abort ();
8809
8810 if (bed->s->arch_size == 32)
8811 {
8812 r_type_mask = 0xff;
8813 r_sym_shift = 8;
8814 }
8815 else
8816 {
8817 r_type_mask = 0xffffffff;
8818 r_sym_shift = 32;
8819 }
8820
d4730f92
BS
8821 erela = reldata->hdr->contents;
8822 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8823 {
8824 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8825 unsigned int j;
8826
8827 if (*rel_hash == NULL)
8828 continue;
8829
10bbbc1d
NC
8830 if ((*rel_hash)->indx == -2
8831 && info->gc_sections
8832 && ! info->gc_keep_exported)
8833 {
8834 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8835 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8836 abfd, sec,
8837 (*rel_hash)->root.root.string);
9793eb77 8838 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8839 abfd, sec);
10bbbc1d
NC
8840 bfd_set_error (bfd_error_invalid_operation);
8841 return FALSE;
8842 }
c152c796
AM
8843 BFD_ASSERT ((*rel_hash)->indx >= 0);
8844
8845 (*swap_in) (abfd, erela, irela);
8846 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8847 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8848 | (irela[j].r_info & r_type_mask));
8849 (*swap_out) (abfd, irela, erela);
8850 }
53df40a4 8851
9eaff861
AO
8852 if (bed->elf_backend_update_relocs)
8853 (*bed->elf_backend_update_relocs) (sec, reldata);
8854
0e287786 8855 if (sort && count != 0)
53df40a4 8856 {
0e287786
AM
8857 bfd_vma (*ext_r_off) (const void *);
8858 bfd_vma r_off;
8859 size_t elt_size;
8860 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8861 bfd_byte *buf = NULL;
28dbcedc
AM
8862
8863 if (bed->s->arch_size == 32)
8864 {
8865 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8866 ext_r_off = ext32l_r_offset;
28dbcedc 8867 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8868 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8869 else
8870 abort ();
8871 }
53df40a4 8872 else
28dbcedc 8873 {
53df40a4 8874#ifdef BFD_HOST_64_BIT
28dbcedc 8875 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8876 ext_r_off = ext64l_r_offset;
28dbcedc 8877 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8878 ext_r_off = ext64b_r_offset;
28dbcedc 8879 else
53df40a4 8880#endif
28dbcedc
AM
8881 abort ();
8882 }
0e287786 8883
bca6d0e3
AM
8884 /* Must use a stable sort here. A modified insertion sort,
8885 since the relocs are mostly sorted already. */
0e287786
AM
8886 elt_size = reldata->hdr->sh_entsize;
8887 base = reldata->hdr->contents;
8888 end = base + count * elt_size;
bca6d0e3 8889 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8890 abort ();
8891
8892 /* Ensure the first element is lowest. This acts as a sentinel,
8893 speeding the main loop below. */
8894 r_off = (*ext_r_off) (base);
8895 for (p = loc = base; (p += elt_size) < end; )
8896 {
8897 bfd_vma r_off2 = (*ext_r_off) (p);
8898 if (r_off > r_off2)
8899 {
8900 r_off = r_off2;
8901 loc = p;
8902 }
8903 }
8904 if (loc != base)
8905 {
8906 /* Don't just swap *base and *loc as that changes the order
8907 of the original base[0] and base[1] if they happen to
8908 have the same r_offset. */
bca6d0e3
AM
8909 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8910 memcpy (onebuf, loc, elt_size);
0e287786 8911 memmove (base + elt_size, base, loc - base);
bca6d0e3 8912 memcpy (base, onebuf, elt_size);
0e287786
AM
8913 }
8914
b29b8669 8915 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8916 {
8917 /* base to p is sorted, *p is next to insert. */
8918 r_off = (*ext_r_off) (p);
8919 /* Search the sorted region for location to insert. */
8920 loc = p - elt_size;
8921 while (r_off < (*ext_r_off) (loc))
8922 loc -= elt_size;
8923 loc += elt_size;
8924 if (loc != p)
8925 {
bca6d0e3
AM
8926 /* Chances are there is a run of relocs to insert here,
8927 from one of more input files. Files are not always
8928 linked in order due to the way elf_link_input_bfd is
8929 called. See pr17666. */
8930 size_t sortlen = p - loc;
8931 bfd_vma r_off2 = (*ext_r_off) (loc);
8932 size_t runlen = elt_size;
8933 size_t buf_size = 96 * 1024;
8934 while (p + runlen < end
8935 && (sortlen <= buf_size
8936 || runlen + elt_size <= buf_size)
8937 && r_off2 > (*ext_r_off) (p + runlen))
8938 runlen += elt_size;
8939 if (buf == NULL)
8940 {
8941 buf = bfd_malloc (buf_size);
8942 if (buf == NULL)
8943 return FALSE;
8944 }
8945 if (runlen < sortlen)
8946 {
8947 memcpy (buf, p, runlen);
8948 memmove (loc + runlen, loc, sortlen);
8949 memcpy (loc, buf, runlen);
8950 }
8951 else
8952 {
8953 memcpy (buf, loc, sortlen);
8954 memmove (loc, p, runlen);
8955 memcpy (loc + runlen, buf, sortlen);
8956 }
b29b8669 8957 p += runlen - elt_size;
0e287786
AM
8958 }
8959 }
8960 /* Hashes are no longer valid. */
28dbcedc
AM
8961 free (reldata->hashes);
8962 reldata->hashes = NULL;
bca6d0e3 8963 free (buf);
53df40a4 8964 }
bca6d0e3 8965 return TRUE;
c152c796
AM
8966}
8967
8968struct elf_link_sort_rela
8969{
8970 union {
8971 bfd_vma offset;
8972 bfd_vma sym_mask;
8973 } u;
8974 enum elf_reloc_type_class type;
8975 /* We use this as an array of size int_rels_per_ext_rel. */
8976 Elf_Internal_Rela rela[1];
8977};
8978
8979static int
8980elf_link_sort_cmp1 (const void *A, const void *B)
8981{
a50b1753
NC
8982 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8983 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8984 int relativea, relativeb;
8985
8986 relativea = a->type == reloc_class_relative;
8987 relativeb = b->type == reloc_class_relative;
8988
8989 if (relativea < relativeb)
8990 return 1;
8991 if (relativea > relativeb)
8992 return -1;
8993 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8994 return -1;
8995 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8996 return 1;
8997 if (a->rela->r_offset < b->rela->r_offset)
8998 return -1;
8999 if (a->rela->r_offset > b->rela->r_offset)
9000 return 1;
9001 return 0;
9002}
9003
9004static int
9005elf_link_sort_cmp2 (const void *A, const void *B)
9006{
a50b1753
NC
9007 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9008 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9009
7e612e98 9010 if (a->type < b->type)
c152c796 9011 return -1;
7e612e98 9012 if (a->type > b->type)
c152c796 9013 return 1;
7e612e98 9014 if (a->u.offset < b->u.offset)
c152c796 9015 return -1;
7e612e98 9016 if (a->u.offset > b->u.offset)
c152c796
AM
9017 return 1;
9018 if (a->rela->r_offset < b->rela->r_offset)
9019 return -1;
9020 if (a->rela->r_offset > b->rela->r_offset)
9021 return 1;
9022 return 0;
9023}
9024
9025static size_t
9026elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9027{
3410fea8 9028 asection *dynamic_relocs;
fc66a176
L
9029 asection *rela_dyn;
9030 asection *rel_dyn;
c152c796
AM
9031 bfd_size_type count, size;
9032 size_t i, ret, sort_elt, ext_size;
9033 bfd_byte *sort, *s_non_relative, *p;
9034 struct elf_link_sort_rela *sq;
9035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9036 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9037 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9038 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9039 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9040 struct bfd_link_order *lo;
9041 bfd_vma r_sym_mask;
3410fea8 9042 bfd_boolean use_rela;
c152c796 9043
3410fea8
NC
9044 /* Find a dynamic reloc section. */
9045 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9046 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9047 if (rela_dyn != NULL && rela_dyn->size > 0
9048 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9049 {
3410fea8
NC
9050 bfd_boolean use_rela_initialised = FALSE;
9051
9052 /* This is just here to stop gcc from complaining.
c8e44c6d 9053 Its initialization checking code is not perfect. */
3410fea8
NC
9054 use_rela = TRUE;
9055
9056 /* Both sections are present. Examine the sizes
9057 of the indirect sections to help us choose. */
9058 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9059 if (lo->type == bfd_indirect_link_order)
9060 {
9061 asection *o = lo->u.indirect.section;
9062
9063 if ((o->size % bed->s->sizeof_rela) == 0)
9064 {
9065 if ((o->size % bed->s->sizeof_rel) == 0)
9066 /* Section size is divisible by both rel and rela sizes.
9067 It is of no help to us. */
9068 ;
9069 else
9070 {
9071 /* Section size is only divisible by rela. */
535b785f 9072 if (use_rela_initialised && !use_rela)
3410fea8 9073 {
9793eb77 9074 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9075 "they are in more than one size"),
9076 abfd);
3410fea8
NC
9077 bfd_set_error (bfd_error_invalid_operation);
9078 return 0;
9079 }
9080 else
9081 {
9082 use_rela = TRUE;
9083 use_rela_initialised = TRUE;
9084 }
9085 }
9086 }
9087 else if ((o->size % bed->s->sizeof_rel) == 0)
9088 {
9089 /* Section size is only divisible by rel. */
535b785f 9090 if (use_rela_initialised && use_rela)
3410fea8 9091 {
9793eb77 9092 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9093 "they are in more than one size"),
9094 abfd);
3410fea8
NC
9095 bfd_set_error (bfd_error_invalid_operation);
9096 return 0;
9097 }
9098 else
9099 {
9100 use_rela = FALSE;
9101 use_rela_initialised = TRUE;
9102 }
9103 }
9104 else
9105 {
c8e44c6d
AM
9106 /* The section size is not divisible by either -
9107 something is wrong. */
9793eb77 9108 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9109 "they are of an unknown size"), abfd);
3410fea8
NC
9110 bfd_set_error (bfd_error_invalid_operation);
9111 return 0;
9112 }
9113 }
9114
9115 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9116 if (lo->type == bfd_indirect_link_order)
9117 {
9118 asection *o = lo->u.indirect.section;
9119
9120 if ((o->size % bed->s->sizeof_rela) == 0)
9121 {
9122 if ((o->size % bed->s->sizeof_rel) == 0)
9123 /* Section size is divisible by both rel and rela sizes.
9124 It is of no help to us. */
9125 ;
9126 else
9127 {
9128 /* Section size is only divisible by rela. */
535b785f 9129 if (use_rela_initialised && !use_rela)
3410fea8 9130 {
9793eb77 9131 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9132 "they are in more than one size"),
9133 abfd);
3410fea8
NC
9134 bfd_set_error (bfd_error_invalid_operation);
9135 return 0;
9136 }
9137 else
9138 {
9139 use_rela = TRUE;
9140 use_rela_initialised = TRUE;
9141 }
9142 }
9143 }
9144 else if ((o->size % bed->s->sizeof_rel) == 0)
9145 {
9146 /* Section size is only divisible by rel. */
535b785f 9147 if (use_rela_initialised && use_rela)
3410fea8 9148 {
9793eb77 9149 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9150 "they are in more than one size"),
9151 abfd);
3410fea8
NC
9152 bfd_set_error (bfd_error_invalid_operation);
9153 return 0;
9154 }
9155 else
9156 {
9157 use_rela = FALSE;
9158 use_rela_initialised = TRUE;
9159 }
9160 }
9161 else
9162 {
c8e44c6d
AM
9163 /* The section size is not divisible by either -
9164 something is wrong. */
9793eb77 9165 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9166 "they are of an unknown size"), abfd);
3410fea8
NC
9167 bfd_set_error (bfd_error_invalid_operation);
9168 return 0;
9169 }
9170 }
9171
9172 if (! use_rela_initialised)
9173 /* Make a guess. */
9174 use_rela = TRUE;
c152c796 9175 }
fc66a176
L
9176 else if (rela_dyn != NULL && rela_dyn->size > 0)
9177 use_rela = TRUE;
9178 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9179 use_rela = FALSE;
c152c796 9180 else
fc66a176 9181 return 0;
3410fea8
NC
9182
9183 if (use_rela)
c152c796 9184 {
3410fea8 9185 dynamic_relocs = rela_dyn;
c152c796
AM
9186 ext_size = bed->s->sizeof_rela;
9187 swap_in = bed->s->swap_reloca_in;
9188 swap_out = bed->s->swap_reloca_out;
9189 }
3410fea8
NC
9190 else
9191 {
9192 dynamic_relocs = rel_dyn;
9193 ext_size = bed->s->sizeof_rel;
9194 swap_in = bed->s->swap_reloc_in;
9195 swap_out = bed->s->swap_reloc_out;
9196 }
c152c796
AM
9197
9198 size = 0;
3410fea8 9199 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9200 if (lo->type == bfd_indirect_link_order)
3410fea8 9201 size += lo->u.indirect.section->size;
c152c796 9202
3410fea8 9203 if (size != dynamic_relocs->size)
c152c796
AM
9204 return 0;
9205
9206 sort_elt = (sizeof (struct elf_link_sort_rela)
9207 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9208
9209 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9210 if (count == 0)
9211 return 0;
a50b1753 9212 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9213
c152c796
AM
9214 if (sort == NULL)
9215 {
9216 (*info->callbacks->warning)
9793eb77 9217 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9218 return 0;
9219 }
9220
9221 if (bed->s->arch_size == 32)
9222 r_sym_mask = ~(bfd_vma) 0xff;
9223 else
9224 r_sym_mask = ~(bfd_vma) 0xffffffff;
9225
3410fea8 9226 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9227 if (lo->type == bfd_indirect_link_order)
9228 {
9229 bfd_byte *erel, *erelend;
9230 asection *o = lo->u.indirect.section;
9231
1da212d6
AM
9232 if (o->contents == NULL && o->size != 0)
9233 {
9234 /* This is a reloc section that is being handled as a normal
9235 section. See bfd_section_from_shdr. We can't combine
9236 relocs in this case. */
9237 free (sort);
9238 return 0;
9239 }
c152c796 9240 erel = o->contents;
eea6121a 9241 erelend = o->contents + o->size;
c8e44c6d 9242 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9243
c152c796
AM
9244 while (erel < erelend)
9245 {
9246 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9247
c152c796 9248 (*swap_in) (abfd, erel, s->rela);
7e612e98 9249 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9250 s->u.sym_mask = r_sym_mask;
9251 p += sort_elt;
9252 erel += ext_size;
9253 }
9254 }
9255
9256 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9257
9258 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9259 {
9260 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9261 if (s->type != reloc_class_relative)
9262 break;
9263 }
9264 ret = i;
9265 s_non_relative = p;
9266
9267 sq = (struct elf_link_sort_rela *) s_non_relative;
9268 for (; i < count; i++, p += sort_elt)
9269 {
9270 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9271 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9272 sq = sp;
9273 sp->u.offset = sq->rela->r_offset;
9274 }
9275
9276 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9277
c8e44c6d
AM
9278 struct elf_link_hash_table *htab = elf_hash_table (info);
9279 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9280 {
9281 /* We have plt relocs in .rela.dyn. */
9282 sq = (struct elf_link_sort_rela *) sort;
9283 for (i = 0; i < count; i++)
9284 if (sq[count - i - 1].type != reloc_class_plt)
9285 break;
9286 if (i != 0 && htab->srelplt->size == i * ext_size)
9287 {
9288 struct bfd_link_order **plo;
9289 /* Put srelplt link_order last. This is so the output_offset
9290 set in the next loop is correct for DT_JMPREL. */
9291 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9292 if ((*plo)->type == bfd_indirect_link_order
9293 && (*plo)->u.indirect.section == htab->srelplt)
9294 {
9295 lo = *plo;
9296 *plo = lo->next;
9297 }
9298 else
9299 plo = &(*plo)->next;
9300 *plo = lo;
9301 lo->next = NULL;
9302 dynamic_relocs->map_tail.link_order = lo;
9303 }
9304 }
9305
9306 p = sort;
3410fea8 9307 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9308 if (lo->type == bfd_indirect_link_order)
9309 {
9310 bfd_byte *erel, *erelend;
9311 asection *o = lo->u.indirect.section;
9312
9313 erel = o->contents;
eea6121a 9314 erelend = o->contents + o->size;
c8e44c6d 9315 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9316 while (erel < erelend)
9317 {
9318 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9319 (*swap_out) (abfd, s->rela, erel);
9320 p += sort_elt;
9321 erel += ext_size;
9322 }
9323 }
9324
9325 free (sort);
3410fea8 9326 *psec = dynamic_relocs;
c152c796
AM
9327 return ret;
9328}
9329
ef10c3ac 9330/* Add a symbol to the output symbol string table. */
c152c796 9331
6e0b88f1 9332static int
ef10c3ac
L
9333elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9334 const char *name,
9335 Elf_Internal_Sym *elfsym,
9336 asection *input_sec,
9337 struct elf_link_hash_entry *h)
c152c796 9338{
6e0b88f1 9339 int (*output_symbol_hook)
c152c796
AM
9340 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9341 struct elf_link_hash_entry *);
ef10c3ac 9342 struct elf_link_hash_table *hash_table;
c152c796 9343 const struct elf_backend_data *bed;
ef10c3ac 9344 bfd_size_type strtabsize;
c152c796 9345
8539e4e8
AM
9346 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9347
8b127cbc 9348 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9349 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9350 if (output_symbol_hook != NULL)
9351 {
8b127cbc 9352 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9353 if (ret != 1)
9354 return ret;
c152c796
AM
9355 }
9356
ef10c3ac
L
9357 if (name == NULL
9358 || *name == '\0'
9359 || (input_sec->flags & SEC_EXCLUDE))
9360 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9361 else
9362 {
ef10c3ac
L
9363 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9364 to get the final offset for st_name. */
9365 elfsym->st_name
9366 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9367 name, FALSE);
c152c796 9368 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9369 return 0;
c152c796
AM
9370 }
9371
ef10c3ac
L
9372 hash_table = elf_hash_table (flinfo->info);
9373 strtabsize = hash_table->strtabsize;
9374 if (strtabsize <= hash_table->strtabcount)
c152c796 9375 {
ef10c3ac
L
9376 strtabsize += strtabsize;
9377 hash_table->strtabsize = strtabsize;
9378 strtabsize *= sizeof (*hash_table->strtab);
9379 hash_table->strtab
9380 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9381 strtabsize);
9382 if (hash_table->strtab == NULL)
6e0b88f1 9383 return 0;
c152c796 9384 }
ef10c3ac
L
9385 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9386 hash_table->strtab[hash_table->strtabcount].dest_index
9387 = hash_table->strtabcount;
9388 hash_table->strtab[hash_table->strtabcount].destshndx_index
9389 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9390
9391 bfd_get_symcount (flinfo->output_bfd) += 1;
9392 hash_table->strtabcount += 1;
9393
9394 return 1;
9395}
9396
9397/* Swap symbols out to the symbol table and flush the output symbols to
9398 the file. */
9399
9400static bfd_boolean
9401elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9402{
9403 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9404 bfd_size_type amt;
9405 size_t i;
ef10c3ac
L
9406 const struct elf_backend_data *bed;
9407 bfd_byte *symbuf;
9408 Elf_Internal_Shdr *hdr;
9409 file_ptr pos;
9410 bfd_boolean ret;
9411
9412 if (!hash_table->strtabcount)
9413 return TRUE;
9414
9415 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9416
9417 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9418
ef10c3ac
L
9419 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9420 symbuf = (bfd_byte *) bfd_malloc (amt);
9421 if (symbuf == NULL)
9422 return FALSE;
1b786873 9423
ef10c3ac 9424 if (flinfo->symshndxbuf)
c152c796 9425 {
ef53be89
AM
9426 amt = sizeof (Elf_External_Sym_Shndx);
9427 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9428 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9429 if (flinfo->symshndxbuf == NULL)
c152c796 9430 {
ef10c3ac
L
9431 free (symbuf);
9432 return FALSE;
c152c796 9433 }
c152c796
AM
9434 }
9435
ef10c3ac
L
9436 for (i = 0; i < hash_table->strtabcount; i++)
9437 {
9438 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9439 if (elfsym->sym.st_name == (unsigned long) -1)
9440 elfsym->sym.st_name = 0;
9441 else
9442 elfsym->sym.st_name
9443 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9444 elfsym->sym.st_name);
9445 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9446 ((bfd_byte *) symbuf
9447 + (elfsym->dest_index
9448 * bed->s->sizeof_sym)),
9449 (flinfo->symshndxbuf
9450 + elfsym->destshndx_index));
9451 }
9452
9453 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9454 pos = hdr->sh_offset + hdr->sh_size;
9455 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9456 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9457 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9458 {
9459 hdr->sh_size += amt;
9460 ret = TRUE;
9461 }
9462 else
9463 ret = FALSE;
c152c796 9464
ef10c3ac
L
9465 free (symbuf);
9466
9467 free (hash_table->strtab);
9468 hash_table->strtab = NULL;
9469
9470 return ret;
c152c796
AM
9471}
9472
c0d5a53d
L
9473/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9474
9475static bfd_boolean
9476check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9477{
4fbb74a6
AM
9478 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9479 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9480 {
9481 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9482 beyond 64k. */
4eca0228 9483 _bfd_error_handler
695344c0 9484 /* xgettext:c-format */
9793eb77 9485 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9486 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9487 bfd_set_error (bfd_error_nonrepresentable_section);
9488 return FALSE;
9489 }
9490 return TRUE;
9491}
9492
c152c796
AM
9493/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9494 allowing an unsatisfied unversioned symbol in the DSO to match a
9495 versioned symbol that would normally require an explicit version.
9496 We also handle the case that a DSO references a hidden symbol
9497 which may be satisfied by a versioned symbol in another DSO. */
9498
9499static bfd_boolean
9500elf_link_check_versioned_symbol (struct bfd_link_info *info,
9501 const struct elf_backend_data *bed,
9502 struct elf_link_hash_entry *h)
9503{
9504 bfd *abfd;
9505 struct elf_link_loaded_list *loaded;
9506
9507 if (!is_elf_hash_table (info->hash))
9508 return FALSE;
9509
90c984fc
L
9510 /* Check indirect symbol. */
9511 while (h->root.type == bfd_link_hash_indirect)
9512 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9513
c152c796
AM
9514 switch (h->root.type)
9515 {
9516 default:
9517 abfd = NULL;
9518 break;
9519
9520 case bfd_link_hash_undefined:
9521 case bfd_link_hash_undefweak:
9522 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9523 if (abfd == NULL
9524 || (abfd->flags & DYNAMIC) == 0
e56f61be 9525 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9526 return FALSE;
9527 break;
9528
9529 case bfd_link_hash_defined:
9530 case bfd_link_hash_defweak:
9531 abfd = h->root.u.def.section->owner;
9532 break;
9533
9534 case bfd_link_hash_common:
9535 abfd = h->root.u.c.p->section->owner;
9536 break;
9537 }
9538 BFD_ASSERT (abfd != NULL);
9539
9540 for (loaded = elf_hash_table (info)->loaded;
9541 loaded != NULL;
9542 loaded = loaded->next)
9543 {
9544 bfd *input;
9545 Elf_Internal_Shdr *hdr;
ef53be89
AM
9546 size_t symcount;
9547 size_t extsymcount;
9548 size_t extsymoff;
c152c796
AM
9549 Elf_Internal_Shdr *versymhdr;
9550 Elf_Internal_Sym *isym;
9551 Elf_Internal_Sym *isymend;
9552 Elf_Internal_Sym *isymbuf;
9553 Elf_External_Versym *ever;
9554 Elf_External_Versym *extversym;
9555
9556 input = loaded->abfd;
9557
9558 /* We check each DSO for a possible hidden versioned definition. */
9559 if (input == abfd
9560 || (input->flags & DYNAMIC) == 0
9561 || elf_dynversym (input) == 0)
9562 continue;
9563
9564 hdr = &elf_tdata (input)->dynsymtab_hdr;
9565
9566 symcount = hdr->sh_size / bed->s->sizeof_sym;
9567 if (elf_bad_symtab (input))
9568 {
9569 extsymcount = symcount;
9570 extsymoff = 0;
9571 }
9572 else
9573 {
9574 extsymcount = symcount - hdr->sh_info;
9575 extsymoff = hdr->sh_info;
9576 }
9577
9578 if (extsymcount == 0)
9579 continue;
9580
9581 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9582 NULL, NULL, NULL);
9583 if (isymbuf == NULL)
9584 return FALSE;
9585
9586 /* Read in any version definitions. */
9587 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9588 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9589 if (extversym == NULL)
9590 goto error_ret;
9591
9592 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9593 || (bfd_bread (extversym, versymhdr->sh_size, input)
9594 != versymhdr->sh_size))
9595 {
9596 free (extversym);
9597 error_ret:
9598 free (isymbuf);
9599 return FALSE;
9600 }
9601
9602 ever = extversym + extsymoff;
9603 isymend = isymbuf + extsymcount;
9604 for (isym = isymbuf; isym < isymend; isym++, ever++)
9605 {
9606 const char *name;
9607 Elf_Internal_Versym iver;
9608 unsigned short version_index;
9609
9610 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9611 || isym->st_shndx == SHN_UNDEF)
9612 continue;
9613
9614 name = bfd_elf_string_from_elf_section (input,
9615 hdr->sh_link,
9616 isym->st_name);
9617 if (strcmp (name, h->root.root.string) != 0)
9618 continue;
9619
9620 _bfd_elf_swap_versym_in (input, ever, &iver);
9621
d023c380
L
9622 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9623 && !(h->def_regular
9624 && h->forced_local))
c152c796
AM
9625 {
9626 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9627 have provided a definition for the undefined sym unless
9628 it is defined in a non-shared object and forced local.
9629 */
c152c796
AM
9630 abort ();
9631 }
9632
9633 version_index = iver.vs_vers & VERSYM_VERSION;
9634 if (version_index == 1 || version_index == 2)
9635 {
9636 /* This is the base or first version. We can use it. */
9637 free (extversym);
9638 free (isymbuf);
9639 return TRUE;
9640 }
9641 }
9642
9643 free (extversym);
9644 free (isymbuf);
9645 }
9646
9647 return FALSE;
9648}
9649
b8871f35
L
9650/* Convert ELF common symbol TYPE. */
9651
9652static int
9653elf_link_convert_common_type (struct bfd_link_info *info, int type)
9654{
9655 /* Commom symbol can only appear in relocatable link. */
9656 if (!bfd_link_relocatable (info))
9657 abort ();
9658 switch (info->elf_stt_common)
9659 {
9660 case unchanged:
9661 break;
9662 case elf_stt_common:
9663 type = STT_COMMON;
9664 break;
9665 case no_elf_stt_common:
9666 type = STT_OBJECT;
9667 break;
9668 }
9669 return type;
9670}
9671
c152c796
AM
9672/* Add an external symbol to the symbol table. This is called from
9673 the hash table traversal routine. When generating a shared object,
9674 we go through the symbol table twice. The first time we output
9675 anything that might have been forced to local scope in a version
9676 script. The second time we output the symbols that are still
9677 global symbols. */
9678
9679static bfd_boolean
7686d77d 9680elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9681{
7686d77d 9682 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9683 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9684 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9685 bfd_boolean strip;
9686 Elf_Internal_Sym sym;
9687 asection *input_sec;
9688 const struct elf_backend_data *bed;
6e0b88f1
AM
9689 long indx;
9690 int ret;
b8871f35 9691 unsigned int type;
c152c796
AM
9692
9693 if (h->root.type == bfd_link_hash_warning)
9694 {
9695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9696 if (h->root.type == bfd_link_hash_new)
9697 return TRUE;
9698 }
9699
9700 /* Decide whether to output this symbol in this pass. */
9701 if (eoinfo->localsyms)
9702 {
4deb8f71 9703 if (!h->forced_local)
c152c796
AM
9704 return TRUE;
9705 }
9706 else
9707 {
4deb8f71 9708 if (h->forced_local)
c152c796
AM
9709 return TRUE;
9710 }
9711
8b127cbc 9712 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9713
12ac1cf5 9714 if (h->root.type == bfd_link_hash_undefined)
c152c796 9715 {
12ac1cf5
NC
9716 /* If we have an undefined symbol reference here then it must have
9717 come from a shared library that is being linked in. (Undefined
98da7939
L
9718 references in regular files have already been handled unless
9719 they are in unreferenced sections which are removed by garbage
9720 collection). */
12ac1cf5
NC
9721 bfd_boolean ignore_undef = FALSE;
9722
9723 /* Some symbols may be special in that the fact that they're
9724 undefined can be safely ignored - let backend determine that. */
9725 if (bed->elf_backend_ignore_undef_symbol)
9726 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9727
9728 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9729 if (!ignore_undef
12ac1cf5 9730 && h->ref_dynamic
8b127cbc
AM
9731 && (!h->ref_regular || flinfo->info->gc_sections)
9732 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9733 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9734 (*flinfo->info->callbacks->undefined_symbol)
9735 (flinfo->info, h->root.root.string,
9736 h->ref_regular ? NULL : h->root.u.undef.abfd,
9737 NULL, 0,
9738 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9739
9740 /* Strip a global symbol defined in a discarded section. */
9741 if (h->indx == -3)
9742 return TRUE;
c152c796
AM
9743 }
9744
9745 /* We should also warn if a forced local symbol is referenced from
9746 shared libraries. */
0e1862bb 9747 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9748 && h->forced_local
9749 && h->ref_dynamic
371a5866 9750 && h->def_regular
f5385ebf 9751 && !h->dynamic_def
ee659f1f 9752 && h->ref_dynamic_nonweak
8b127cbc 9753 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9754 {
17d078c5
AM
9755 bfd *def_bfd;
9756 const char *msg;
90c984fc
L
9757 struct elf_link_hash_entry *hi = h;
9758
9759 /* Check indirect symbol. */
9760 while (hi->root.type == bfd_link_hash_indirect)
9761 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9762
9763 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9764 /* xgettext:c-format */
871b3ab2 9765 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9766 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9767 /* xgettext:c-format */
871b3ab2 9768 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9769 else
695344c0 9770 /* xgettext:c-format */
871b3ab2 9771 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9772 def_bfd = flinfo->output_bfd;
90c984fc
L
9773 if (hi->root.u.def.section != bfd_abs_section_ptr)
9774 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9775 _bfd_error_handler (msg, flinfo->output_bfd,
9776 h->root.root.string, def_bfd);
17d078c5 9777 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9778 eoinfo->failed = TRUE;
9779 return FALSE;
9780 }
9781
9782 /* We don't want to output symbols that have never been mentioned by
9783 a regular file, or that we have been told to strip. However, if
9784 h->indx is set to -2, the symbol is used by a reloc and we must
9785 output it. */
d983c8c5 9786 strip = FALSE;
c152c796 9787 if (h->indx == -2)
d983c8c5 9788 ;
f5385ebf 9789 else if ((h->def_dynamic
77cfaee6
AM
9790 || h->ref_dynamic
9791 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9792 && !h->def_regular
9793 && !h->ref_regular)
c152c796 9794 strip = TRUE;
8b127cbc 9795 else if (flinfo->info->strip == strip_all)
c152c796 9796 strip = TRUE;
8b127cbc
AM
9797 else if (flinfo->info->strip == strip_some
9798 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9799 h->root.root.string, FALSE, FALSE) == NULL)
9800 strip = TRUE;
d56d55e7
AM
9801 else if ((h->root.type == bfd_link_hash_defined
9802 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9803 && ((flinfo->info->strip_discarded
dbaa2011 9804 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9805 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9806 && h->root.u.def.section->owner != NULL
d56d55e7 9807 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9808 strip = TRUE;
9e2278f5
AM
9809 else if ((h->root.type == bfd_link_hash_undefined
9810 || h->root.type == bfd_link_hash_undefweak)
9811 && h->root.u.undef.abfd != NULL
9812 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9813 strip = TRUE;
c152c796 9814
b8871f35
L
9815 type = h->type;
9816
c152c796 9817 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9818 nothing else to do. However, if it is a forced local symbol or
9819 an ifunc symbol we need to give the backend finish_dynamic_symbol
9820 function a chance to make it dynamic. */
c152c796
AM
9821 if (strip
9822 && h->dynindx == -1
b8871f35 9823 && type != STT_GNU_IFUNC
f5385ebf 9824 && !h->forced_local)
c152c796
AM
9825 return TRUE;
9826
9827 sym.st_value = 0;
9828 sym.st_size = h->size;
9829 sym.st_other = h->other;
c152c796
AM
9830 switch (h->root.type)
9831 {
9832 default:
9833 case bfd_link_hash_new:
9834 case bfd_link_hash_warning:
9835 abort ();
9836 return FALSE;
9837
9838 case bfd_link_hash_undefined:
9839 case bfd_link_hash_undefweak:
9840 input_sec = bfd_und_section_ptr;
9841 sym.st_shndx = SHN_UNDEF;
9842 break;
9843
9844 case bfd_link_hash_defined:
9845 case bfd_link_hash_defweak:
9846 {
9847 input_sec = h->root.u.def.section;
9848 if (input_sec->output_section != NULL)
9849 {
9850 sym.st_shndx =
8b127cbc 9851 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9852 input_sec->output_section);
9853 if (sym.st_shndx == SHN_BAD)
9854 {
4eca0228 9855 _bfd_error_handler
695344c0 9856 /* xgettext:c-format */
871b3ab2 9857 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9858 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9859 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9860 eoinfo->failed = TRUE;
9861 return FALSE;
9862 }
9863
9864 /* ELF symbols in relocatable files are section relative,
9865 but in nonrelocatable files they are virtual
9866 addresses. */
9867 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9868 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9869 {
9870 sym.st_value += input_sec->output_section->vma;
9871 if (h->type == STT_TLS)
9872 {
8b127cbc 9873 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9874 if (tls_sec != NULL)
9875 sym.st_value -= tls_sec->vma;
c152c796
AM
9876 }
9877 }
9878 }
9879 else
9880 {
9881 BFD_ASSERT (input_sec->owner == NULL
9882 || (input_sec->owner->flags & DYNAMIC) != 0);
9883 sym.st_shndx = SHN_UNDEF;
9884 input_sec = bfd_und_section_ptr;
9885 }
9886 }
9887 break;
9888
9889 case bfd_link_hash_common:
9890 input_sec = h->root.u.c.p->section;
a4d8e49b 9891 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9892 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9893 break;
9894
9895 case bfd_link_hash_indirect:
9896 /* These symbols are created by symbol versioning. They point
9897 to the decorated version of the name. For example, if the
9898 symbol foo@@GNU_1.2 is the default, which should be used when
9899 foo is used with no version, then we add an indirect symbol
9900 foo which points to foo@@GNU_1.2. We ignore these symbols,
9901 since the indirected symbol is already in the hash table. */
9902 return TRUE;
9903 }
9904
b8871f35
L
9905 if (type == STT_COMMON || type == STT_OBJECT)
9906 switch (h->root.type)
9907 {
9908 case bfd_link_hash_common:
9909 type = elf_link_convert_common_type (flinfo->info, type);
9910 break;
9911 case bfd_link_hash_defined:
9912 case bfd_link_hash_defweak:
9913 if (bed->common_definition (&sym))
9914 type = elf_link_convert_common_type (flinfo->info, type);
9915 else
9916 type = STT_OBJECT;
9917 break;
9918 case bfd_link_hash_undefined:
9919 case bfd_link_hash_undefweak:
9920 break;
9921 default:
9922 abort ();
9923 }
9924
4deb8f71 9925 if (h->forced_local)
b8871f35
L
9926 {
9927 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9928 /* Turn off visibility on local symbol. */
9929 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9930 }
9931 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9932 else if (h->unique_global && h->def_regular)
9933 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9934 else if (h->root.type == bfd_link_hash_undefweak
9935 || h->root.type == bfd_link_hash_defweak)
9936 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9937 else
9938 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9939 sym.st_target_internal = h->target_internal;
9940
c152c796
AM
9941 /* Give the processor backend a chance to tweak the symbol value,
9942 and also to finish up anything that needs to be done for this
9943 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9944 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9945 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9946 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9947 && h->def_regular
0e1862bb 9948 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9949 || ((h->dynindx != -1
9950 || h->forced_local)
0e1862bb 9951 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9952 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9953 || h->root.type != bfd_link_hash_undefweak))
9954 || !h->forced_local)
8b127cbc 9955 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9956 {
9957 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9958 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9959 {
9960 eoinfo->failed = TRUE;
9961 return FALSE;
9962 }
9963 }
9964
9965 /* If we are marking the symbol as undefined, and there are no
9966 non-weak references to this symbol from a regular object, then
9967 mark the symbol as weak undefined; if there are non-weak
9968 references, mark the symbol as strong. We can't do this earlier,
9969 because it might not be marked as undefined until the
9970 finish_dynamic_symbol routine gets through with it. */
9971 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9972 && h->ref_regular
c152c796
AM
9973 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9974 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9975 {
9976 int bindtype;
b8871f35 9977 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9978
9979 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9980 if (type == STT_GNU_IFUNC)
9981 type = STT_FUNC;
c152c796 9982
f5385ebf 9983 if (h->ref_regular_nonweak)
c152c796
AM
9984 bindtype = STB_GLOBAL;
9985 else
9986 bindtype = STB_WEAK;
2955ec4c 9987 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9988 }
9989
bda987c2
CD
9990 /* If this is a symbol defined in a dynamic library, don't use the
9991 symbol size from the dynamic library. Relinking an executable
9992 against a new library may introduce gratuitous changes in the
9993 executable's symbols if we keep the size. */
9994 if (sym.st_shndx == SHN_UNDEF
9995 && !h->def_regular
9996 && h->def_dynamic)
9997 sym.st_size = 0;
9998
c152c796
AM
9999 /* If a non-weak symbol with non-default visibility is not defined
10000 locally, it is a fatal error. */
0e1862bb 10001 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10002 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10003 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10004 && h->root.type == bfd_link_hash_undefined
f5385ebf 10005 && !h->def_regular)
c152c796 10006 {
17d078c5
AM
10007 const char *msg;
10008
10009 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10010 /* xgettext:c-format */
871b3ab2 10011 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10012 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10013 /* xgettext:c-format */
871b3ab2 10014 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10015 else
695344c0 10016 /* xgettext:c-format */
871b3ab2 10017 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10018 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10019 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10020 eoinfo->failed = TRUE;
10021 return FALSE;
10022 }
10023
10024 /* If this symbol should be put in the .dynsym section, then put it
10025 there now. We already know the symbol index. We also fill in
10026 the entry in the .hash section. */
cae1fbbb 10027 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 10028 && h->dynindx != -1
8b127cbc 10029 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 10030 {
c152c796
AM
10031 bfd_byte *esym;
10032
90c984fc
L
10033 /* Since there is no version information in the dynamic string,
10034 if there is no version info in symbol version section, we will
1659f720 10035 have a run-time problem if not linking executable, referenced
4deb8f71 10036 by shared library, or not bound locally. */
1659f720 10037 if (h->verinfo.verdef == NULL
0e1862bb 10038 && (!bfd_link_executable (flinfo->info)
1659f720
L
10039 || h->ref_dynamic
10040 || !h->def_regular))
90c984fc
L
10041 {
10042 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10043
10044 if (p && p [1] != '\0')
10045 {
4eca0228 10046 _bfd_error_handler
695344c0 10047 /* xgettext:c-format */
9793eb77 10048 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10049 flinfo->output_bfd, h->root.root.string);
10050 eoinfo->failed = TRUE;
10051 return FALSE;
10052 }
10053 }
10054
c152c796 10055 sym.st_name = h->dynstr_index;
cae1fbbb
L
10056 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10057 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10058 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10059 {
10060 eoinfo->failed = TRUE;
10061 return FALSE;
10062 }
8b127cbc 10063 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10064
8b127cbc 10065 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10066 {
10067 size_t hash_entry_size;
10068 bfd_byte *bucketpos;
10069 bfd_vma chain;
41198d0c
L
10070 size_t bucketcount;
10071 size_t bucket;
10072
8b127cbc 10073 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10074 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10075
10076 hash_entry_size
8b127cbc
AM
10077 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10078 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10079 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10080 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10081 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10082 bucketpos);
10083 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10084 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10085 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10086 }
c152c796 10087
8b127cbc 10088 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10089 {
10090 Elf_Internal_Versym iversym;
10091 Elf_External_Versym *eversym;
10092
f5385ebf 10093 if (!h->def_regular)
c152c796 10094 {
7b20f099
AM
10095 if (h->verinfo.verdef == NULL
10096 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10097 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10098 iversym.vs_vers = 0;
10099 else
10100 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10101 }
10102 else
10103 {
10104 if (h->verinfo.vertree == NULL)
10105 iversym.vs_vers = 1;
10106 else
10107 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10108 if (flinfo->info->create_default_symver)
3e3b46e5 10109 iversym.vs_vers++;
c152c796
AM
10110 }
10111
422f1182 10112 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10113 defined locally. */
422f1182 10114 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10115 iversym.vs_vers |= VERSYM_HIDDEN;
10116
8b127cbc 10117 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10118 eversym += h->dynindx;
8b127cbc 10119 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10120 }
10121 }
10122
d983c8c5
AM
10123 /* If the symbol is undefined, and we didn't output it to .dynsym,
10124 strip it from .symtab too. Obviously we can't do this for
10125 relocatable output or when needed for --emit-relocs. */
10126 else if (input_sec == bfd_und_section_ptr
10127 && h->indx != -2
66cae560
NC
10128 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10129 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10130 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10131 return TRUE;
66cae560 10132
d983c8c5
AM
10133 /* Also strip others that we couldn't earlier due to dynamic symbol
10134 processing. */
10135 if (strip)
10136 return TRUE;
10137 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10138 return TRUE;
10139
2ec55de3
AM
10140 /* Output a FILE symbol so that following locals are not associated
10141 with the wrong input file. We need one for forced local symbols
10142 if we've seen more than one FILE symbol or when we have exactly
10143 one FILE symbol but global symbols are present in a file other
10144 than the one with the FILE symbol. We also need one if linker
10145 defined symbols are present. In practice these conditions are
10146 always met, so just emit the FILE symbol unconditionally. */
10147 if (eoinfo->localsyms
10148 && !eoinfo->file_sym_done
10149 && eoinfo->flinfo->filesym_count != 0)
10150 {
10151 Elf_Internal_Sym fsym;
10152
10153 memset (&fsym, 0, sizeof (fsym));
10154 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10155 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10156 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10157 bfd_und_section_ptr, NULL))
2ec55de3
AM
10158 return FALSE;
10159
10160 eoinfo->file_sym_done = TRUE;
10161 }
10162
8b127cbc 10163 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10164 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10165 input_sec, h);
6e0b88f1 10166 if (ret == 0)
c152c796
AM
10167 {
10168 eoinfo->failed = TRUE;
10169 return FALSE;
10170 }
6e0b88f1
AM
10171 else if (ret == 1)
10172 h->indx = indx;
10173 else if (h->indx == -2)
10174 abort();
c152c796
AM
10175
10176 return TRUE;
10177}
10178
cdd3575c
AM
10179/* Return TRUE if special handling is done for relocs in SEC against
10180 symbols defined in discarded sections. */
10181
c152c796
AM
10182static bfd_boolean
10183elf_section_ignore_discarded_relocs (asection *sec)
10184{
10185 const struct elf_backend_data *bed;
10186
cdd3575c
AM
10187 switch (sec->sec_info_type)
10188 {
dbaa2011
AM
10189 case SEC_INFO_TYPE_STABS:
10190 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10191 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10192 return TRUE;
10193 default:
10194 break;
10195 }
c152c796
AM
10196
10197 bed = get_elf_backend_data (sec->owner);
10198 if (bed->elf_backend_ignore_discarded_relocs != NULL
10199 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10200 return TRUE;
10201
10202 return FALSE;
10203}
10204
9e66c942
AM
10205/* Return a mask saying how ld should treat relocations in SEC against
10206 symbols defined in discarded sections. If this function returns
10207 COMPLAIN set, ld will issue a warning message. If this function
10208 returns PRETEND set, and the discarded section was link-once and the
10209 same size as the kept link-once section, ld will pretend that the
10210 symbol was actually defined in the kept section. Otherwise ld will
10211 zero the reloc (at least that is the intent, but some cooperation by
10212 the target dependent code is needed, particularly for REL targets). */
10213
8a696751
AM
10214unsigned int
10215_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10216{
9e66c942 10217 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10218 return PRETEND;
cdd3575c
AM
10219
10220 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10221 return 0;
cdd3575c
AM
10222
10223 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10224 return 0;
cdd3575c 10225
9e66c942 10226 return COMPLAIN | PRETEND;
cdd3575c
AM
10227}
10228
3d7f7666
L
10229/* Find a match between a section and a member of a section group. */
10230
10231static asection *
c0f00686
L
10232match_group_member (asection *sec, asection *group,
10233 struct bfd_link_info *info)
3d7f7666
L
10234{
10235 asection *first = elf_next_in_group (group);
10236 asection *s = first;
10237
10238 while (s != NULL)
10239 {
c0f00686 10240 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10241 return s;
10242
83180ade 10243 s = elf_next_in_group (s);
3d7f7666
L
10244 if (s == first)
10245 break;
10246 }
10247
10248 return NULL;
10249}
10250
01b3c8ab 10251/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10252 to replace it. Return the replacement if it is OK. Otherwise return
10253 NULL. */
01b3c8ab
L
10254
10255asection *
c0f00686 10256_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10257{
10258 asection *kept;
10259
10260 kept = sec->kept_section;
10261 if (kept != NULL)
10262 {
c2370991 10263 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10264 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10265 if (kept != NULL
10266 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10267 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10268 kept = NULL;
c2370991 10269 sec->kept_section = kept;
01b3c8ab
L
10270 }
10271 return kept;
10272}
10273
c152c796
AM
10274/* Link an input file into the linker output file. This function
10275 handles all the sections and relocations of the input file at once.
10276 This is so that we only have to read the local symbols once, and
10277 don't have to keep them in memory. */
10278
10279static bfd_boolean
8b127cbc 10280elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10281{
ece5ef60 10282 int (*relocate_section)
c152c796
AM
10283 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10284 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10285 bfd *output_bfd;
10286 Elf_Internal_Shdr *symtab_hdr;
10287 size_t locsymcount;
10288 size_t extsymoff;
10289 Elf_Internal_Sym *isymbuf;
10290 Elf_Internal_Sym *isym;
10291 Elf_Internal_Sym *isymend;
10292 long *pindex;
10293 asection **ppsection;
10294 asection *o;
10295 const struct elf_backend_data *bed;
c152c796 10296 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10297 bfd_size_type address_size;
10298 bfd_vma r_type_mask;
10299 int r_sym_shift;
ffbc01cc 10300 bfd_boolean have_file_sym = FALSE;
c152c796 10301
8b127cbc 10302 output_bfd = flinfo->output_bfd;
c152c796
AM
10303 bed = get_elf_backend_data (output_bfd);
10304 relocate_section = bed->elf_backend_relocate_section;
10305
10306 /* If this is a dynamic object, we don't want to do anything here:
10307 we don't want the local symbols, and we don't want the section
10308 contents. */
10309 if ((input_bfd->flags & DYNAMIC) != 0)
10310 return TRUE;
10311
c152c796
AM
10312 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10313 if (elf_bad_symtab (input_bfd))
10314 {
10315 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10316 extsymoff = 0;
10317 }
10318 else
10319 {
10320 locsymcount = symtab_hdr->sh_info;
10321 extsymoff = symtab_hdr->sh_info;
10322 }
10323
10324 /* Read the local symbols. */
10325 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10326 if (isymbuf == NULL && locsymcount != 0)
10327 {
10328 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10329 flinfo->internal_syms,
10330 flinfo->external_syms,
10331 flinfo->locsym_shndx);
c152c796
AM
10332 if (isymbuf == NULL)
10333 return FALSE;
10334 }
10335
10336 /* Find local symbol sections and adjust values of symbols in
10337 SEC_MERGE sections. Write out those local symbols we know are
10338 going into the output file. */
10339 isymend = isymbuf + locsymcount;
8b127cbc 10340 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10341 isym < isymend;
10342 isym++, pindex++, ppsection++)
10343 {
10344 asection *isec;
10345 const char *name;
10346 Elf_Internal_Sym osym;
6e0b88f1
AM
10347 long indx;
10348 int ret;
c152c796
AM
10349
10350 *pindex = -1;
10351
10352 if (elf_bad_symtab (input_bfd))
10353 {
10354 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10355 {
10356 *ppsection = NULL;
10357 continue;
10358 }
10359 }
10360
10361 if (isym->st_shndx == SHN_UNDEF)
10362 isec = bfd_und_section_ptr;
c152c796
AM
10363 else if (isym->st_shndx == SHN_ABS)
10364 isec = bfd_abs_section_ptr;
10365 else if (isym->st_shndx == SHN_COMMON)
10366 isec = bfd_com_section_ptr;
10367 else
10368 {
cb33740c
AM
10369 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10370 if (isec == NULL)
10371 {
10372 /* Don't attempt to output symbols with st_shnx in the
10373 reserved range other than SHN_ABS and SHN_COMMON. */
10374 *ppsection = NULL;
10375 continue;
10376 }
dbaa2011 10377 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10378 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10379 isym->st_value =
10380 _bfd_merged_section_offset (output_bfd, &isec,
10381 elf_section_data (isec)->sec_info,
10382 isym->st_value);
c152c796
AM
10383 }
10384
10385 *ppsection = isec;
10386
d983c8c5
AM
10387 /* Don't output the first, undefined, symbol. In fact, don't
10388 output any undefined local symbol. */
10389 if (isec == bfd_und_section_ptr)
c152c796
AM
10390 continue;
10391
10392 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10393 {
10394 /* We never output section symbols. Instead, we use the
10395 section symbol of the corresponding section in the output
10396 file. */
10397 continue;
10398 }
10399
10400 /* If we are stripping all symbols, we don't want to output this
10401 one. */
8b127cbc 10402 if (flinfo->info->strip == strip_all)
c152c796
AM
10403 continue;
10404
10405 /* If we are discarding all local symbols, we don't want to
10406 output this one. If we are generating a relocatable output
10407 file, then some of the local symbols may be required by
10408 relocs; we output them below as we discover that they are
10409 needed. */
8b127cbc 10410 if (flinfo->info->discard == discard_all)
c152c796
AM
10411 continue;
10412
10413 /* If this symbol is defined in a section which we are
f02571c5
AM
10414 discarding, we don't need to keep it. */
10415 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10416 && isym->st_shndx < SHN_LORESERVE
10417 && bfd_section_removed_from_list (output_bfd,
10418 isec->output_section))
e75a280b
L
10419 continue;
10420
c152c796
AM
10421 /* Get the name of the symbol. */
10422 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10423 isym->st_name);
10424 if (name == NULL)
10425 return FALSE;
10426
10427 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10428 if ((flinfo->info->strip == strip_some
10429 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10430 == NULL))
8b127cbc 10431 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10432 && (isec->flags & SEC_MERGE)
10433 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10434 || flinfo->info->discard == discard_l)
c152c796
AM
10435 && bfd_is_local_label_name (input_bfd, name)))
10436 continue;
10437
ffbc01cc
AM
10438 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10439 {
ce875075
AM
10440 if (input_bfd->lto_output)
10441 /* -flto puts a temp file name here. This means builds
10442 are not reproducible. Discard the symbol. */
10443 continue;
ffbc01cc
AM
10444 have_file_sym = TRUE;
10445 flinfo->filesym_count += 1;
10446 }
10447 if (!have_file_sym)
10448 {
10449 /* In the absence of debug info, bfd_find_nearest_line uses
10450 FILE symbols to determine the source file for local
10451 function symbols. Provide a FILE symbol here if input
10452 files lack such, so that their symbols won't be
10453 associated with a previous input file. It's not the
10454 source file, but the best we can do. */
10455 have_file_sym = TRUE;
10456 flinfo->filesym_count += 1;
10457 memset (&osym, 0, sizeof (osym));
10458 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10459 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10460 if (!elf_link_output_symstrtab (flinfo,
10461 (input_bfd->lto_output ? NULL
10462 : input_bfd->filename),
10463 &osym, bfd_abs_section_ptr,
10464 NULL))
ffbc01cc
AM
10465 return FALSE;
10466 }
10467
c152c796
AM
10468 osym = *isym;
10469
10470 /* Adjust the section index for the output file. */
10471 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10472 isec->output_section);
10473 if (osym.st_shndx == SHN_BAD)
10474 return FALSE;
10475
c152c796
AM
10476 /* ELF symbols in relocatable files are section relative, but
10477 in executable files they are virtual addresses. Note that
10478 this code assumes that all ELF sections have an associated
10479 BFD section with a reasonable value for output_offset; below
10480 we assume that they also have a reasonable value for
10481 output_section. Any special sections must be set up to meet
10482 these requirements. */
10483 osym.st_value += isec->output_offset;
0e1862bb 10484 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10485 {
10486 osym.st_value += isec->output_section->vma;
10487 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10488 {
10489 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10490 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10491 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10492 }
10493 }
10494
6e0b88f1 10495 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10496 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10497 if (ret == 0)
c152c796 10498 return FALSE;
6e0b88f1
AM
10499 else if (ret == 1)
10500 *pindex = indx;
c152c796
AM
10501 }
10502
310fd250
L
10503 if (bed->s->arch_size == 32)
10504 {
10505 r_type_mask = 0xff;
10506 r_sym_shift = 8;
10507 address_size = 4;
10508 }
10509 else
10510 {
10511 r_type_mask = 0xffffffff;
10512 r_sym_shift = 32;
10513 address_size = 8;
10514 }
10515
c152c796
AM
10516 /* Relocate the contents of each section. */
10517 sym_hashes = elf_sym_hashes (input_bfd);
10518 for (o = input_bfd->sections; o != NULL; o = o->next)
10519 {
10520 bfd_byte *contents;
10521
10522 if (! o->linker_mark)
10523 {
10524 /* This section was omitted from the link. */
10525 continue;
10526 }
10527
7bdf4127 10528 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10529 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10530 {
10531 /* Deal with the group signature symbol. */
10532 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10533 unsigned long symndx = sec_data->this_hdr.sh_info;
10534 asection *osec = o->output_section;
10535
7bdf4127 10536 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10537 if (symndx >= locsymcount
10538 || (elf_bad_symtab (input_bfd)
8b127cbc 10539 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10540 {
10541 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10542 while (h->root.type == bfd_link_hash_indirect
10543 || h->root.type == bfd_link_hash_warning)
10544 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10545 /* Arrange for symbol to be output. */
10546 h->indx = -2;
10547 elf_section_data (osec)->this_hdr.sh_info = -2;
10548 }
10549 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10550 {
10551 /* We'll use the output section target_index. */
8b127cbc 10552 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10553 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10554 }
10555 else
10556 {
8b127cbc 10557 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10558 {
10559 /* Otherwise output the local symbol now. */
10560 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10561 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10562 const char *name;
6e0b88f1
AM
10563 long indx;
10564 int ret;
bcacc0f5
AM
10565
10566 name = bfd_elf_string_from_elf_section (input_bfd,
10567 symtab_hdr->sh_link,
10568 sym.st_name);
10569 if (name == NULL)
10570 return FALSE;
10571
10572 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10573 sec);
10574 if (sym.st_shndx == SHN_BAD)
10575 return FALSE;
10576
10577 sym.st_value += o->output_offset;
10578
6e0b88f1 10579 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10580 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10581 NULL);
6e0b88f1 10582 if (ret == 0)
bcacc0f5 10583 return FALSE;
6e0b88f1 10584 else if (ret == 1)
8b127cbc 10585 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10586 else
10587 abort ();
bcacc0f5
AM
10588 }
10589 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10590 = flinfo->indices[symndx];
bcacc0f5
AM
10591 }
10592 }
10593
c152c796 10594 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10595 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10596 continue;
10597
10598 if ((o->flags & SEC_LINKER_CREATED) != 0)
10599 {
10600 /* Section was created by _bfd_elf_link_create_dynamic_sections
10601 or somesuch. */
10602 continue;
10603 }
10604
10605 /* Get the contents of the section. They have been cached by a
10606 relaxation routine. Note that o is a section in an input
10607 file, so the contents field will not have been set by any of
10608 the routines which work on output files. */
10609 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10610 {
10611 contents = elf_section_data (o)->this_hdr.contents;
10612 if (bed->caches_rawsize
10613 && o->rawsize != 0
10614 && o->rawsize < o->size)
10615 {
10616 memcpy (flinfo->contents, contents, o->rawsize);
10617 contents = flinfo->contents;
10618 }
10619 }
c152c796
AM
10620 else
10621 {
8b127cbc 10622 contents = flinfo->contents;
4a114e3e 10623 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10624 return FALSE;
10625 }
10626
10627 if ((o->flags & SEC_RELOC) != 0)
10628 {
10629 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10630 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10631 int action_discarded;
ece5ef60 10632 int ret;
c152c796
AM
10633
10634 /* Get the swapped relocs. */
10635 internal_relocs
8b127cbc
AM
10636 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10637 flinfo->internal_relocs, FALSE);
c152c796
AM
10638 if (internal_relocs == NULL
10639 && o->reloc_count > 0)
10640 return FALSE;
10641
310fd250
L
10642 /* We need to reverse-copy input .ctors/.dtors sections if
10643 they are placed in .init_array/.finit_array for output. */
10644 if (o->size > address_size
10645 && ((strncmp (o->name, ".ctors", 6) == 0
10646 && strcmp (o->output_section->name,
10647 ".init_array") == 0)
10648 || (strncmp (o->name, ".dtors", 6) == 0
10649 && strcmp (o->output_section->name,
10650 ".fini_array") == 0))
10651 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10652 {
056bafd4
MR
10653 if (o->size * bed->s->int_rels_per_ext_rel
10654 != o->reloc_count * address_size)
310fd250 10655 {
4eca0228 10656 _bfd_error_handler
695344c0 10657 /* xgettext:c-format */
871b3ab2 10658 (_("error: %pB: size of section %pA is not "
310fd250
L
10659 "multiple of address size"),
10660 input_bfd, o);
8c6716e5 10661 bfd_set_error (bfd_error_bad_value);
310fd250
L
10662 return FALSE;
10663 }
10664 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10665 }
10666
0f02bbd9 10667 action_discarded = -1;
c152c796 10668 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10669 action_discarded = (*bed->action_discarded) (o);
10670
10671 /* Run through the relocs evaluating complex reloc symbols and
10672 looking for relocs against symbols from discarded sections
10673 or section symbols from removed link-once sections.
10674 Complain about relocs against discarded sections. Zero
10675 relocs against removed link-once sections. */
10676
10677 rel = internal_relocs;
056bafd4 10678 relend = rel + o->reloc_count;
0f02bbd9 10679 for ( ; rel < relend; rel++)
c152c796 10680 {
0f02bbd9
AM
10681 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10682 unsigned int s_type;
10683 asection **ps, *sec;
10684 struct elf_link_hash_entry *h = NULL;
10685 const char *sym_name;
c152c796 10686
0f02bbd9
AM
10687 if (r_symndx == STN_UNDEF)
10688 continue;
c152c796 10689
0f02bbd9
AM
10690 if (r_symndx >= locsymcount
10691 || (elf_bad_symtab (input_bfd)
8b127cbc 10692 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10693 {
10694 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10695
0f02bbd9
AM
10696 /* Badly formatted input files can contain relocs that
10697 reference non-existant symbols. Check here so that
10698 we do not seg fault. */
10699 if (h == NULL)
c152c796 10700 {
4eca0228 10701 _bfd_error_handler
695344c0 10702 /* xgettext:c-format */
2dcf00ce 10703 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10704 "that references a non-existent global symbol"),
2dcf00ce 10705 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10706 bfd_set_error (bfd_error_bad_value);
10707 return FALSE;
10708 }
3b36f7e6 10709
0f02bbd9
AM
10710 while (h->root.type == bfd_link_hash_indirect
10711 || h->root.type == bfd_link_hash_warning)
10712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10713
0f02bbd9 10714 s_type = h->type;
cdd3575c 10715
9e2dec47 10716 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10717 mark the symbol as undefined. Note that the
10718 linker may attach linker created dynamic sections
10719 to the plugin bfd. Symbols defined in linker
10720 created sections are not plugin symbols. */
bc4e12de 10721 if ((h->root.non_ir_ref_regular
4070765b 10722 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10723 && (h->root.type == bfd_link_hash_defined
10724 || h->root.type == bfd_link_hash_defweak)
10725 && (h->root.u.def.section->flags
10726 & SEC_LINKER_CREATED) == 0
10727 && h->root.u.def.section->owner != NULL
10728 && (h->root.u.def.section->owner->flags
10729 & BFD_PLUGIN) != 0)
10730 {
10731 h->root.type = bfd_link_hash_undefined;
10732 h->root.u.undef.abfd = h->root.u.def.section->owner;
10733 }
10734
0f02bbd9
AM
10735 ps = NULL;
10736 if (h->root.type == bfd_link_hash_defined
10737 || h->root.type == bfd_link_hash_defweak)
10738 ps = &h->root.u.def.section;
10739
10740 sym_name = h->root.root.string;
10741 }
10742 else
10743 {
10744 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10745
10746 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10747 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10748 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10749 sym, *ps);
10750 }
c152c796 10751
c301e700 10752 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10753 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10754 {
10755 bfd_vma val;
10756 bfd_vma dot = (rel->r_offset
10757 + o->output_offset + o->output_section->vma);
10758#ifdef DEBUG
10759 printf ("Encountered a complex symbol!");
10760 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10761 input_bfd->filename, o->name,
10762 (long) (rel - internal_relocs));
0f02bbd9
AM
10763 printf (" symbol: idx %8.8lx, name %s\n",
10764 r_symndx, sym_name);
10765 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10766 (unsigned long) rel->r_info,
10767 (unsigned long) rel->r_offset);
10768#endif
8b127cbc 10769 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10770 isymbuf, locsymcount, s_type == STT_SRELC))
10771 return FALSE;
10772
10773 /* Symbol evaluated OK. Update to absolute value. */
10774 set_symbol_value (input_bfd, isymbuf, locsymcount,
10775 r_symndx, val);
10776 continue;
10777 }
10778
10779 if (action_discarded != -1 && ps != NULL)
10780 {
cdd3575c
AM
10781 /* Complain if the definition comes from a
10782 discarded section. */
dbaa2011 10783 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10784 {
cf35638d 10785 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10786 if (action_discarded & COMPLAIN)
8b127cbc 10787 (*flinfo->info->callbacks->einfo)
695344c0 10788 /* xgettext:c-format */
871b3ab2
AM
10789 (_("%X`%s' referenced in section `%pA' of %pB: "
10790 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10791 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10792
87e5235d 10793 /* Try to do the best we can to support buggy old
e0ae6d6f 10794 versions of gcc. Pretend that the symbol is
87e5235d
AM
10795 really defined in the kept linkonce section.
10796 FIXME: This is quite broken. Modifying the
10797 symbol here means we will be changing all later
e0ae6d6f 10798 uses of the symbol, not just in this section. */
0f02bbd9 10799 if (action_discarded & PRETEND)
87e5235d 10800 {
01b3c8ab
L
10801 asection *kept;
10802
c0f00686 10803 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10804 flinfo->info);
01b3c8ab 10805 if (kept != NULL)
87e5235d
AM
10806 {
10807 *ps = kept;
10808 continue;
10809 }
10810 }
c152c796
AM
10811 }
10812 }
10813 }
10814
10815 /* Relocate the section by invoking a back end routine.
10816
10817 The back end routine is responsible for adjusting the
10818 section contents as necessary, and (if using Rela relocs
10819 and generating a relocatable output file) adjusting the
10820 reloc addend as necessary.
10821
10822 The back end routine does not have to worry about setting
10823 the reloc address or the reloc symbol index.
10824
10825 The back end routine is given a pointer to the swapped in
10826 internal symbols, and can access the hash table entries
10827 for the external symbols via elf_sym_hashes (input_bfd).
10828
10829 When generating relocatable output, the back end routine
10830 must handle STB_LOCAL/STT_SECTION symbols specially. The
10831 output symbol is going to be a section symbol
10832 corresponding to the output section, which will require
10833 the addend to be adjusted. */
10834
8b127cbc 10835 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10836 input_bfd, o, contents,
10837 internal_relocs,
10838 isymbuf,
8b127cbc 10839 flinfo->sections);
ece5ef60 10840 if (!ret)
c152c796
AM
10841 return FALSE;
10842
ece5ef60 10843 if (ret == 2
0e1862bb 10844 || bfd_link_relocatable (flinfo->info)
8b127cbc 10845 || flinfo->info->emitrelocations)
c152c796
AM
10846 {
10847 Elf_Internal_Rela *irela;
d4730f92 10848 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10849 bfd_vma last_offset;
10850 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10851 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10852 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10853 unsigned int next_erel;
c152c796 10854 bfd_boolean rela_normal;
d4730f92 10855 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10856
d4730f92
BS
10857 esdi = elf_section_data (o);
10858 esdo = elf_section_data (o->output_section);
10859 rela_normal = FALSE;
c152c796
AM
10860
10861 /* Adjust the reloc addresses and symbol indices. */
10862
10863 irela = internal_relocs;
056bafd4 10864 irelaend = irela + o->reloc_count;
d4730f92
BS
10865 rel_hash = esdo->rel.hashes + esdo->rel.count;
10866 /* We start processing the REL relocs, if any. When we reach
10867 IRELAMID in the loop, we switch to the RELA relocs. */
10868 irelamid = irela;
10869 if (esdi->rel.hdr != NULL)
10870 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10871 * bed->s->int_rels_per_ext_rel);
eac338cf 10872 rel_hash_list = rel_hash;
d4730f92 10873 rela_hash_list = NULL;
c152c796 10874 last_offset = o->output_offset;
0e1862bb 10875 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10876 last_offset += o->output_section->vma;
10877 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10878 {
10879 unsigned long r_symndx;
10880 asection *sec;
10881 Elf_Internal_Sym sym;
10882
10883 if (next_erel == bed->s->int_rels_per_ext_rel)
10884 {
10885 rel_hash++;
10886 next_erel = 0;
10887 }
10888
d4730f92
BS
10889 if (irela == irelamid)
10890 {
10891 rel_hash = esdo->rela.hashes + esdo->rela.count;
10892 rela_hash_list = rel_hash;
10893 rela_normal = bed->rela_normal;
10894 }
10895
c152c796 10896 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10897 flinfo->info, o,
c152c796
AM
10898 irela->r_offset);
10899 if (irela->r_offset >= (bfd_vma) -2)
10900 {
10901 /* This is a reloc for a deleted entry or somesuch.
10902 Turn it into an R_*_NONE reloc, at the same
10903 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10904 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10905 being ordered. */
10906 irela->r_offset = last_offset;
10907 irela->r_info = 0;
10908 irela->r_addend = 0;
10909 continue;
10910 }
10911
10912 irela->r_offset += o->output_offset;
10913
10914 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10915 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10916 irela->r_offset += o->output_section->vma;
10917
10918 last_offset = irela->r_offset;
10919
10920 r_symndx = irela->r_info >> r_sym_shift;
10921 if (r_symndx == STN_UNDEF)
10922 continue;
10923
10924 if (r_symndx >= locsymcount
10925 || (elf_bad_symtab (input_bfd)
8b127cbc 10926 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10927 {
10928 struct elf_link_hash_entry *rh;
10929 unsigned long indx;
10930
10931 /* This is a reloc against a global symbol. We
10932 have not yet output all the local symbols, so
10933 we do not know the symbol index of any global
10934 symbol. We set the rel_hash entry for this
10935 reloc to point to the global hash table entry
10936 for this symbol. The symbol index is then
ee75fd95 10937 set at the end of bfd_elf_final_link. */
c152c796
AM
10938 indx = r_symndx - extsymoff;
10939 rh = elf_sym_hashes (input_bfd)[indx];
10940 while (rh->root.type == bfd_link_hash_indirect
10941 || rh->root.type == bfd_link_hash_warning)
10942 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10943
10944 /* Setting the index to -2 tells
10945 elf_link_output_extsym that this symbol is
10946 used by a reloc. */
10947 BFD_ASSERT (rh->indx < 0);
10948 rh->indx = -2;
c152c796
AM
10949 *rel_hash = rh;
10950
10951 continue;
10952 }
10953
10954 /* This is a reloc against a local symbol. */
10955
10956 *rel_hash = NULL;
10957 sym = isymbuf[r_symndx];
8b127cbc 10958 sec = flinfo->sections[r_symndx];
c152c796
AM
10959 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10960 {
10961 /* I suppose the backend ought to fill in the
10962 section of any STT_SECTION symbol against a
6a8d1586 10963 processor specific section. */
cf35638d 10964 r_symndx = STN_UNDEF;
6a8d1586
AM
10965 if (bfd_is_abs_section (sec))
10966 ;
c152c796
AM
10967 else if (sec == NULL || sec->owner == NULL)
10968 {
10969 bfd_set_error (bfd_error_bad_value);
10970 return FALSE;
10971 }
10972 else
10973 {
6a8d1586
AM
10974 asection *osec = sec->output_section;
10975
10976 /* If we have discarded a section, the output
10977 section will be the absolute section. In
ab96bf03
AM
10978 case of discarded SEC_MERGE sections, use
10979 the kept section. relocate_section should
10980 have already handled discarded linkonce
10981 sections. */
6a8d1586
AM
10982 if (bfd_is_abs_section (osec)
10983 && sec->kept_section != NULL
10984 && sec->kept_section->output_section != NULL)
10985 {
10986 osec = sec->kept_section->output_section;
10987 irela->r_addend -= osec->vma;
10988 }
10989
10990 if (!bfd_is_abs_section (osec))
10991 {
10992 r_symndx = osec->target_index;
cf35638d 10993 if (r_symndx == STN_UNDEF)
74541ad4 10994 {
051d833a
AM
10995 irela->r_addend += osec->vma;
10996 osec = _bfd_nearby_section (output_bfd, osec,
10997 osec->vma);
10998 irela->r_addend -= osec->vma;
10999 r_symndx = osec->target_index;
74541ad4 11000 }
6a8d1586 11001 }
c152c796
AM
11002 }
11003
11004 /* Adjust the addend according to where the
11005 section winds up in the output section. */
11006 if (rela_normal)
11007 irela->r_addend += sec->output_offset;
11008 }
11009 else
11010 {
8b127cbc 11011 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11012 {
11013 unsigned long shlink;
11014 const char *name;
11015 asection *osec;
6e0b88f1 11016 long indx;
c152c796 11017
8b127cbc 11018 if (flinfo->info->strip == strip_all)
c152c796
AM
11019 {
11020 /* You can't do ld -r -s. */
11021 bfd_set_error (bfd_error_invalid_operation);
11022 return FALSE;
11023 }
11024
11025 /* This symbol was skipped earlier, but
11026 since it is needed by a reloc, we
11027 must output it now. */
11028 shlink = symtab_hdr->sh_link;
11029 name = (bfd_elf_string_from_elf_section
11030 (input_bfd, shlink, sym.st_name));
11031 if (name == NULL)
11032 return FALSE;
11033
11034 osec = sec->output_section;
11035 sym.st_shndx =
11036 _bfd_elf_section_from_bfd_section (output_bfd,
11037 osec);
11038 if (sym.st_shndx == SHN_BAD)
11039 return FALSE;
11040
11041 sym.st_value += sec->output_offset;
0e1862bb 11042 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11043 {
11044 sym.st_value += osec->vma;
11045 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11046 {
11047 /* STT_TLS symbols are relative to PT_TLS
11048 segment base. */
8b127cbc 11049 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 11050 ->tls_sec != NULL);
8b127cbc 11051 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
11052 ->tls_sec->vma);
11053 }
11054 }
11055
6e0b88f1 11056 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11057 ret = elf_link_output_symstrtab (flinfo, name,
11058 &sym, sec,
11059 NULL);
6e0b88f1 11060 if (ret == 0)
c152c796 11061 return FALSE;
6e0b88f1 11062 else if (ret == 1)
8b127cbc 11063 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11064 else
11065 abort ();
c152c796
AM
11066 }
11067
8b127cbc 11068 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11069 }
11070
11071 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11072 | (irela->r_info & r_type_mask));
11073 }
11074
11075 /* Swap out the relocs. */
d4730f92
BS
11076 input_rel_hdr = esdi->rel.hdr;
11077 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11078 {
d4730f92
BS
11079 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11080 input_rel_hdr,
11081 internal_relocs,
11082 rel_hash_list))
11083 return FALSE;
c152c796
AM
11084 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11085 * bed->s->int_rels_per_ext_rel);
eac338cf 11086 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11087 }
11088
11089 input_rela_hdr = esdi->rela.hdr;
11090 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11091 {
eac338cf 11092 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11093 input_rela_hdr,
eac338cf 11094 internal_relocs,
d4730f92 11095 rela_hash_list))
c152c796
AM
11096 return FALSE;
11097 }
11098 }
11099 }
11100
11101 /* Write out the modified section contents. */
11102 if (bed->elf_backend_write_section
8b127cbc 11103 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11104 contents))
c152c796
AM
11105 {
11106 /* Section written out. */
11107 }
11108 else switch (o->sec_info_type)
11109 {
dbaa2011 11110 case SEC_INFO_TYPE_STABS:
c152c796
AM
11111 if (! (_bfd_write_section_stabs
11112 (output_bfd,
8b127cbc 11113 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11114 o, &elf_section_data (o)->sec_info, contents)))
11115 return FALSE;
11116 break;
dbaa2011 11117 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11118 if (! _bfd_write_merged_section (output_bfd, o,
11119 elf_section_data (o)->sec_info))
11120 return FALSE;
11121 break;
dbaa2011 11122 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11123 {
8b127cbc 11124 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11125 o, contents))
11126 return FALSE;
11127 }
11128 break;
2f0c68f2
CM
11129 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11130 {
11131 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11132 flinfo->info,
11133 o, contents))
11134 return FALSE;
11135 }
11136 break;
c152c796
AM
11137 default:
11138 {
310fd250
L
11139 if (! (o->flags & SEC_EXCLUDE))
11140 {
11141 file_ptr offset = (file_ptr) o->output_offset;
11142 bfd_size_type todo = o->size;
37b01f6a
DG
11143
11144 offset *= bfd_octets_per_byte (output_bfd);
11145
310fd250
L
11146 if ((o->flags & SEC_ELF_REVERSE_COPY))
11147 {
11148 /* Reverse-copy input section to output. */
11149 do
11150 {
11151 todo -= address_size;
11152 if (! bfd_set_section_contents (output_bfd,
11153 o->output_section,
11154 contents + todo,
11155 offset,
11156 address_size))
11157 return FALSE;
11158 if (todo == 0)
11159 break;
11160 offset += address_size;
11161 }
11162 while (1);
11163 }
11164 else if (! bfd_set_section_contents (output_bfd,
11165 o->output_section,
11166 contents,
11167 offset, todo))
11168 return FALSE;
11169 }
c152c796
AM
11170 }
11171 break;
11172 }
11173 }
11174
11175 return TRUE;
11176}
11177
11178/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11179 requested by the linker, and does not come from any input file. This
c152c796
AM
11180 is used to build constructor and destructor tables when linking
11181 with -Ur. */
11182
11183static bfd_boolean
11184elf_reloc_link_order (bfd *output_bfd,
11185 struct bfd_link_info *info,
11186 asection *output_section,
11187 struct bfd_link_order *link_order)
11188{
11189 reloc_howto_type *howto;
11190 long indx;
11191 bfd_vma offset;
11192 bfd_vma addend;
d4730f92 11193 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11194 struct elf_link_hash_entry **rel_hash_ptr;
11195 Elf_Internal_Shdr *rel_hdr;
11196 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11197 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11198 bfd_byte *erel;
11199 unsigned int i;
d4730f92 11200 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11201
11202 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11203 if (howto == NULL)
11204 {
11205 bfd_set_error (bfd_error_bad_value);
11206 return FALSE;
11207 }
11208
11209 addend = link_order->u.reloc.p->addend;
11210
d4730f92
BS
11211 if (esdo->rel.hdr)
11212 reldata = &esdo->rel;
11213 else if (esdo->rela.hdr)
11214 reldata = &esdo->rela;
11215 else
11216 {
11217 reldata = NULL;
11218 BFD_ASSERT (0);
11219 }
11220
c152c796 11221 /* Figure out the symbol index. */
d4730f92 11222 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11223 if (link_order->type == bfd_section_reloc_link_order)
11224 {
11225 indx = link_order->u.reloc.p->u.section->target_index;
11226 BFD_ASSERT (indx != 0);
11227 *rel_hash_ptr = NULL;
11228 }
11229 else
11230 {
11231 struct elf_link_hash_entry *h;
11232
11233 /* Treat a reloc against a defined symbol as though it were
11234 actually against the section. */
11235 h = ((struct elf_link_hash_entry *)
11236 bfd_wrapped_link_hash_lookup (output_bfd, info,
11237 link_order->u.reloc.p->u.name,
11238 FALSE, FALSE, TRUE));
11239 if (h != NULL
11240 && (h->root.type == bfd_link_hash_defined
11241 || h->root.type == bfd_link_hash_defweak))
11242 {
11243 asection *section;
11244
11245 section = h->root.u.def.section;
11246 indx = section->output_section->target_index;
11247 *rel_hash_ptr = NULL;
11248 /* It seems that we ought to add the symbol value to the
11249 addend here, but in practice it has already been added
11250 because it was passed to constructor_callback. */
11251 addend += section->output_section->vma + section->output_offset;
11252 }
11253 else if (h != NULL)
11254 {
11255 /* Setting the index to -2 tells elf_link_output_extsym that
11256 this symbol is used by a reloc. */
11257 h->indx = -2;
11258 *rel_hash_ptr = h;
11259 indx = 0;
11260 }
11261 else
11262 {
1a72702b
AM
11263 (*info->callbacks->unattached_reloc)
11264 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11265 indx = 0;
11266 }
11267 }
11268
11269 /* If this is an inplace reloc, we must write the addend into the
11270 object file. */
11271 if (howto->partial_inplace && addend != 0)
11272 {
11273 bfd_size_type size;
11274 bfd_reloc_status_type rstat;
11275 bfd_byte *buf;
11276 bfd_boolean ok;
11277 const char *sym_name;
11278
a50b1753
NC
11279 size = (bfd_size_type) bfd_get_reloc_size (howto);
11280 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11281 if (buf == NULL && size != 0)
c152c796
AM
11282 return FALSE;
11283 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11284 switch (rstat)
11285 {
11286 case bfd_reloc_ok:
11287 break;
11288
11289 default:
11290 case bfd_reloc_outofrange:
11291 abort ();
11292
11293 case bfd_reloc_overflow:
11294 if (link_order->type == bfd_section_reloc_link_order)
11295 sym_name = bfd_section_name (output_bfd,
11296 link_order->u.reloc.p->u.section);
11297 else
11298 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11299 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11300 howto->name, addend, NULL, NULL,
11301 (bfd_vma) 0);
c152c796
AM
11302 break;
11303 }
37b01f6a 11304
c152c796 11305 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11306 link_order->offset
11307 * bfd_octets_per_byte (output_bfd),
11308 size);
c152c796
AM
11309 free (buf);
11310 if (! ok)
11311 return FALSE;
11312 }
11313
11314 /* The address of a reloc is relative to the section in a
11315 relocatable file, and is a virtual address in an executable
11316 file. */
11317 offset = link_order->offset;
0e1862bb 11318 if (! bfd_link_relocatable (info))
c152c796
AM
11319 offset += output_section->vma;
11320
11321 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11322 {
11323 irel[i].r_offset = offset;
11324 irel[i].r_info = 0;
11325 irel[i].r_addend = 0;
11326 }
11327 if (bed->s->arch_size == 32)
11328 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11329 else
11330 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11331
d4730f92 11332 rel_hdr = reldata->hdr;
c152c796
AM
11333 erel = rel_hdr->contents;
11334 if (rel_hdr->sh_type == SHT_REL)
11335 {
d4730f92 11336 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11337 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11338 }
11339 else
11340 {
11341 irel[0].r_addend = addend;
d4730f92 11342 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11343 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11344 }
11345
d4730f92 11346 ++reldata->count;
c152c796
AM
11347
11348 return TRUE;
11349}
11350
0b52efa6
PB
11351
11352/* Get the output vma of the section pointed to by the sh_link field. */
11353
11354static bfd_vma
11355elf_get_linked_section_vma (struct bfd_link_order *p)
11356{
11357 Elf_Internal_Shdr **elf_shdrp;
11358 asection *s;
11359 int elfsec;
11360
11361 s = p->u.indirect.section;
11362 elf_shdrp = elf_elfsections (s->owner);
11363 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11364 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11365 /* PR 290:
11366 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11367 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11368 sh_info fields. Hence we could get the situation
11369 where elfsec is 0. */
11370 if (elfsec == 0)
11371 {
11372 const struct elf_backend_data *bed
11373 = get_elf_backend_data (s->owner);
11374 if (bed->link_order_error_handler)
d003868e 11375 bed->link_order_error_handler
695344c0 11376 /* xgettext:c-format */
871b3ab2 11377 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11378 return 0;
11379 }
11380 else
11381 {
11382 s = elf_shdrp[elfsec]->bfd_section;
11383 return s->output_section->vma + s->output_offset;
11384 }
0b52efa6
PB
11385}
11386
11387
11388/* Compare two sections based on the locations of the sections they are
11389 linked to. Used by elf_fixup_link_order. */
11390
11391static int
11392compare_link_order (const void * a, const void * b)
11393{
11394 bfd_vma apos;
11395 bfd_vma bpos;
11396
11397 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11398 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11399 if (apos < bpos)
11400 return -1;
11401 return apos > bpos;
11402}
11403
11404
11405/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11406 order as their linked sections. Returns false if this could not be done
11407 because an output section includes both ordered and unordered
11408 sections. Ideally we'd do this in the linker proper. */
11409
11410static bfd_boolean
11411elf_fixup_link_order (bfd *abfd, asection *o)
11412{
11413 int seen_linkorder;
11414 int seen_other;
11415 int n;
11416 struct bfd_link_order *p;
11417 bfd *sub;
11418 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11419 unsigned elfsec;
0b52efa6 11420 struct bfd_link_order **sections;
d33cdfe3 11421 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11422 bfd_vma offset;
3b36f7e6 11423
d33cdfe3
L
11424 other_sec = NULL;
11425 linkorder_sec = NULL;
0b52efa6
PB
11426 seen_other = 0;
11427 seen_linkorder = 0;
8423293d 11428 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11429 {
d33cdfe3 11430 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11431 {
11432 s = p->u.indirect.section;
d33cdfe3
L
11433 sub = s->owner;
11434 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11435 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11436 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11437 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11438 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11439 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11440 {
11441 seen_linkorder++;
11442 linkorder_sec = s;
11443 }
0b52efa6 11444 else
d33cdfe3
L
11445 {
11446 seen_other++;
11447 other_sec = s;
11448 }
0b52efa6
PB
11449 }
11450 else
11451 seen_other++;
d33cdfe3
L
11452
11453 if (seen_other && seen_linkorder)
11454 {
11455 if (other_sec && linkorder_sec)
4eca0228 11456 _bfd_error_handler
695344c0 11457 /* xgettext:c-format */
871b3ab2
AM
11458 (_("%pA has both ordered [`%pA' in %pB] "
11459 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11460 o, linkorder_sec, linkorder_sec->owner,
11461 other_sec, other_sec->owner);
d33cdfe3 11462 else
4eca0228 11463 _bfd_error_handler
871b3ab2 11464 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11465 bfd_set_error (bfd_error_bad_value);
11466 return FALSE;
11467 }
0b52efa6
PB
11468 }
11469
11470 if (!seen_linkorder)
11471 return TRUE;
11472
0b52efa6 11473 sections = (struct bfd_link_order **)
14b1c01e
AM
11474 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11475 if (sections == NULL)
11476 return FALSE;
0b52efa6 11477 seen_linkorder = 0;
3b36f7e6 11478
8423293d 11479 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11480 {
11481 sections[seen_linkorder++] = p;
11482 }
11483 /* Sort the input sections in the order of their linked section. */
11484 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11485 compare_link_order);
11486
11487 /* Change the offsets of the sections. */
11488 offset = 0;
11489 for (n = 0; n < seen_linkorder; n++)
11490 {
11491 s = sections[n]->u.indirect.section;
461686a3 11492 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11493 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11494 sections[n]->offset = offset;
11495 offset += sections[n]->size;
11496 }
11497
4dd07732 11498 free (sections);
0b52efa6
PB
11499 return TRUE;
11500}
11501
76359541
TP
11502/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11503 Returns TRUE upon success, FALSE otherwise. */
11504
11505static bfd_boolean
11506elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11507{
11508 bfd_boolean ret = FALSE;
11509 bfd *implib_bfd;
11510 const struct elf_backend_data *bed;
11511 flagword flags;
11512 enum bfd_architecture arch;
11513 unsigned int mach;
11514 asymbol **sympp = NULL;
11515 long symsize;
11516 long symcount;
11517 long src_count;
11518 elf_symbol_type *osymbuf;
11519
11520 implib_bfd = info->out_implib_bfd;
11521 bed = get_elf_backend_data (abfd);
11522
11523 if (!bfd_set_format (implib_bfd, bfd_object))
11524 return FALSE;
11525
046734ff 11526 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11527 flags = bfd_get_file_flags (abfd);
11528 flags &= ~HAS_RELOC;
11529 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11530 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11531 return FALSE;
11532
11533 /* Copy architecture of output file to import library file. */
11534 arch = bfd_get_arch (abfd);
11535 mach = bfd_get_mach (abfd);
11536 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11537 && (abfd->target_defaulted
11538 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11539 return FALSE;
11540
11541 /* Get symbol table size. */
11542 symsize = bfd_get_symtab_upper_bound (abfd);
11543 if (symsize < 0)
11544 return FALSE;
11545
11546 /* Read in the symbol table. */
11547 sympp = (asymbol **) xmalloc (symsize);
11548 symcount = bfd_canonicalize_symtab (abfd, sympp);
11549 if (symcount < 0)
11550 goto free_sym_buf;
11551
11552 /* Allow the BFD backend to copy any private header data it
11553 understands from the output BFD to the import library BFD. */
11554 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11555 goto free_sym_buf;
11556
11557 /* Filter symbols to appear in the import library. */
11558 if (bed->elf_backend_filter_implib_symbols)
11559 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11560 symcount);
11561 else
11562 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11563 if (symcount == 0)
11564 {
5df1bc57 11565 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11566 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11567 implib_bfd);
76359541
TP
11568 goto free_sym_buf;
11569 }
11570
11571
11572 /* Make symbols absolute. */
11573 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11574 sizeof (*osymbuf));
11575 for (src_count = 0; src_count < symcount; src_count++)
11576 {
11577 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11578 sizeof (*osymbuf));
11579 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11580 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11581 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11582 osymbuf[src_count].internal_elf_sym.st_value =
11583 osymbuf[src_count].symbol.value;
11584 sympp[src_count] = &osymbuf[src_count].symbol;
11585 }
11586
11587 bfd_set_symtab (implib_bfd, sympp, symcount);
11588
11589 /* Allow the BFD backend to copy any private data it understands
11590 from the output BFD to the import library BFD. This is done last
11591 to permit the routine to look at the filtered symbol table. */
11592 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11593 goto free_sym_buf;
11594
11595 if (!bfd_close (implib_bfd))
11596 goto free_sym_buf;
11597
11598 ret = TRUE;
11599
11600free_sym_buf:
11601 free (sympp);
11602 return ret;
11603}
11604
9f7c3e5e
AM
11605static void
11606elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11607{
11608 asection *o;
11609
11610 if (flinfo->symstrtab != NULL)
ef10c3ac 11611 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11612 if (flinfo->contents != NULL)
11613 free (flinfo->contents);
11614 if (flinfo->external_relocs != NULL)
11615 free (flinfo->external_relocs);
11616 if (flinfo->internal_relocs != NULL)
11617 free (flinfo->internal_relocs);
11618 if (flinfo->external_syms != NULL)
11619 free (flinfo->external_syms);
11620 if (flinfo->locsym_shndx != NULL)
11621 free (flinfo->locsym_shndx);
11622 if (flinfo->internal_syms != NULL)
11623 free (flinfo->internal_syms);
11624 if (flinfo->indices != NULL)
11625 free (flinfo->indices);
11626 if (flinfo->sections != NULL)
11627 free (flinfo->sections);
9f7c3e5e
AM
11628 if (flinfo->symshndxbuf != NULL)
11629 free (flinfo->symshndxbuf);
11630 for (o = obfd->sections; o != NULL; o = o->next)
11631 {
11632 struct bfd_elf_section_data *esdo = elf_section_data (o);
11633 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11634 free (esdo->rel.hashes);
11635 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11636 free (esdo->rela.hashes);
11637 }
11638}
0b52efa6 11639
c152c796
AM
11640/* Do the final step of an ELF link. */
11641
11642bfd_boolean
11643bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11644{
11645 bfd_boolean dynamic;
11646 bfd_boolean emit_relocs;
11647 bfd *dynobj;
8b127cbc 11648 struct elf_final_link_info flinfo;
91d6fa6a
NC
11649 asection *o;
11650 struct bfd_link_order *p;
11651 bfd *sub;
c152c796
AM
11652 bfd_size_type max_contents_size;
11653 bfd_size_type max_external_reloc_size;
11654 bfd_size_type max_internal_reloc_count;
11655 bfd_size_type max_sym_count;
11656 bfd_size_type max_sym_shndx_count;
c152c796
AM
11657 Elf_Internal_Sym elfsym;
11658 unsigned int i;
11659 Elf_Internal_Shdr *symtab_hdr;
11660 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11661 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11662 struct elf_outext_info eoinfo;
11663 bfd_boolean merged;
11664 size_t relativecount = 0;
11665 asection *reldyn = 0;
11666 bfd_size_type amt;
104d59d1
JM
11667 asection *attr_section = NULL;
11668 bfd_vma attr_size = 0;
11669 const char *std_attrs_section;
64f52338 11670 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11671
64f52338 11672 if (!is_elf_hash_table (htab))
c152c796
AM
11673 return FALSE;
11674
0e1862bb 11675 if (bfd_link_pic (info))
c152c796
AM
11676 abfd->flags |= DYNAMIC;
11677
64f52338
AM
11678 dynamic = htab->dynamic_sections_created;
11679 dynobj = htab->dynobj;
c152c796 11680
0e1862bb 11681 emit_relocs = (bfd_link_relocatable (info)
a4676736 11682 || info->emitrelocations);
c152c796 11683
8b127cbc
AM
11684 flinfo.info = info;
11685 flinfo.output_bfd = abfd;
ef10c3ac 11686 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11687 if (flinfo.symstrtab == NULL)
c152c796
AM
11688 return FALSE;
11689
11690 if (! dynamic)
11691 {
8b127cbc
AM
11692 flinfo.hash_sec = NULL;
11693 flinfo.symver_sec = NULL;
c152c796
AM
11694 }
11695 else
11696 {
3d4d4302 11697 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11698 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11699 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11700 /* Note that it is OK if symver_sec is NULL. */
11701 }
11702
8b127cbc
AM
11703 flinfo.contents = NULL;
11704 flinfo.external_relocs = NULL;
11705 flinfo.internal_relocs = NULL;
11706 flinfo.external_syms = NULL;
11707 flinfo.locsym_shndx = NULL;
11708 flinfo.internal_syms = NULL;
11709 flinfo.indices = NULL;
11710 flinfo.sections = NULL;
8b127cbc 11711 flinfo.symshndxbuf = NULL;
ffbc01cc 11712 flinfo.filesym_count = 0;
c152c796 11713
104d59d1
JM
11714 /* The object attributes have been merged. Remove the input
11715 sections from the link, and set the contents of the output
11716 secton. */
11717 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11718 for (o = abfd->sections; o != NULL; o = o->next)
11719 {
5270eddc 11720 bfd_boolean remove_section = FALSE;
b8a6ced7 11721
104d59d1
JM
11722 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11723 || strcmp (o->name, ".gnu.attributes") == 0)
11724 {
11725 for (p = o->map_head.link_order; p != NULL; p = p->next)
11726 {
11727 asection *input_section;
11728
11729 if (p->type != bfd_indirect_link_order)
11730 continue;
11731 input_section = p->u.indirect.section;
11732 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11733 elf_link_input_bfd ignores this section. */
11734 input_section->flags &= ~SEC_HAS_CONTENTS;
11735 }
a0c8462f 11736
104d59d1 11737 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11738 bfd_set_section_size (abfd, o, attr_size);
11739 /* Skip this section later on. */
11740 o->map_head.link_order = NULL;
104d59d1 11741 if (attr_size)
b8a6ced7 11742 attr_section = o;
104d59d1 11743 else
5270eddc 11744 remove_section = TRUE;
104d59d1 11745 }
6e5e9d58
AM
11746 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11747 {
11748 /* Remove empty group section from linker output. */
5270eddc 11749 remove_section = TRUE;
b8a6ced7 11750 }
5270eddc 11751 if (remove_section)
b8a6ced7 11752 {
6e5e9d58
AM
11753 o->flags |= SEC_EXCLUDE;
11754 bfd_section_list_remove (abfd, o);
11755 abfd->section_count--;
11756 }
104d59d1
JM
11757 }
11758
c152c796
AM
11759 /* Count up the number of relocations we will output for each output
11760 section, so that we know the sizes of the reloc sections. We
11761 also figure out some maximum sizes. */
11762 max_contents_size = 0;
11763 max_external_reloc_size = 0;
11764 max_internal_reloc_count = 0;
11765 max_sym_count = 0;
11766 max_sym_shndx_count = 0;
11767 merged = FALSE;
11768 for (o = abfd->sections; o != NULL; o = o->next)
11769 {
11770 struct bfd_elf_section_data *esdo = elf_section_data (o);
11771 o->reloc_count = 0;
11772
8423293d 11773 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11774 {
11775 unsigned int reloc_count = 0;
9eaff861 11776 unsigned int additional_reloc_count = 0;
c152c796 11777 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11778
11779 if (p->type == bfd_section_reloc_link_order
11780 || p->type == bfd_symbol_reloc_link_order)
11781 reloc_count = 1;
11782 else if (p->type == bfd_indirect_link_order)
11783 {
11784 asection *sec;
11785
11786 sec = p->u.indirect.section;
c152c796
AM
11787
11788 /* Mark all sections which are to be included in the
11789 link. This will normally be every section. We need
11790 to do this so that we can identify any sections which
11791 the linker has decided to not include. */
11792 sec->linker_mark = TRUE;
11793
11794 if (sec->flags & SEC_MERGE)
11795 merged = TRUE;
11796
eea6121a
AM
11797 if (sec->rawsize > max_contents_size)
11798 max_contents_size = sec->rawsize;
11799 if (sec->size > max_contents_size)
11800 max_contents_size = sec->size;
c152c796 11801
c152c796
AM
11802 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11803 && (sec->owner->flags & DYNAMIC) == 0)
11804 {
11805 size_t sym_count;
11806
a961cdd5
AM
11807 /* We are interested in just local symbols, not all
11808 symbols. */
c152c796
AM
11809 if (elf_bad_symtab (sec->owner))
11810 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11811 / bed->s->sizeof_sym);
11812 else
11813 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11814
11815 if (sym_count > max_sym_count)
11816 max_sym_count = sym_count;
11817
11818 if (sym_count > max_sym_shndx_count
6a40cf0c 11819 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11820 max_sym_shndx_count = sym_count;
11821
a961cdd5
AM
11822 if (esdo->this_hdr.sh_type == SHT_REL
11823 || esdo->this_hdr.sh_type == SHT_RELA)
11824 /* Some backends use reloc_count in relocation sections
11825 to count particular types of relocs. Of course,
11826 reloc sections themselves can't have relocations. */
11827 ;
11828 else if (emit_relocs)
11829 {
11830 reloc_count = sec->reloc_count;
11831 if (bed->elf_backend_count_additional_relocs)
11832 {
11833 int c;
11834 c = (*bed->elf_backend_count_additional_relocs) (sec);
11835 additional_reloc_count += c;
11836 }
11837 }
11838 else if (bed->elf_backend_count_relocs)
11839 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11840
11841 esdi = elf_section_data (sec);
11842
c152c796
AM
11843 if ((sec->flags & SEC_RELOC) != 0)
11844 {
d4730f92 11845 size_t ext_size = 0;
c152c796 11846
d4730f92
BS
11847 if (esdi->rel.hdr != NULL)
11848 ext_size = esdi->rel.hdr->sh_size;
11849 if (esdi->rela.hdr != NULL)
11850 ext_size += esdi->rela.hdr->sh_size;
7326c758 11851
c152c796
AM
11852 if (ext_size > max_external_reloc_size)
11853 max_external_reloc_size = ext_size;
11854 if (sec->reloc_count > max_internal_reloc_count)
11855 max_internal_reloc_count = sec->reloc_count;
11856 }
11857 }
11858 }
11859
11860 if (reloc_count == 0)
11861 continue;
11862
9eaff861 11863 reloc_count += additional_reloc_count;
c152c796
AM
11864 o->reloc_count += reloc_count;
11865
0e1862bb 11866 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11867 {
d4730f92 11868 if (esdi->rel.hdr)
9eaff861 11869 {
491d01d3 11870 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11871 esdo->rel.count += additional_reloc_count;
11872 }
d4730f92 11873 if (esdi->rela.hdr)
9eaff861 11874 {
491d01d3 11875 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11876 esdo->rela.count += additional_reloc_count;
11877 }
d4730f92
BS
11878 }
11879 else
11880 {
11881 if (o->use_rela_p)
11882 esdo->rela.count += reloc_count;
2c2b4ed4 11883 else
d4730f92 11884 esdo->rel.count += reloc_count;
c152c796 11885 }
c152c796
AM
11886 }
11887
9eaff861 11888 if (o->reloc_count > 0)
c152c796
AM
11889 o->flags |= SEC_RELOC;
11890 else
11891 {
11892 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11893 set it (this is probably a bug) and if it is set
11894 assign_section_numbers will create a reloc section. */
11895 o->flags &=~ SEC_RELOC;
11896 }
11897
11898 /* If the SEC_ALLOC flag is not set, force the section VMA to
11899 zero. This is done in elf_fake_sections as well, but forcing
11900 the VMA to 0 here will ensure that relocs against these
11901 sections are handled correctly. */
11902 if ((o->flags & SEC_ALLOC) == 0
11903 && ! o->user_set_vma)
11904 o->vma = 0;
11905 }
11906
0e1862bb 11907 if (! bfd_link_relocatable (info) && merged)
64f52338 11908 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11909
11910 /* Figure out the file positions for everything but the symbol table
11911 and the relocs. We set symcount to force assign_section_numbers
11912 to create a symbol table. */
8539e4e8 11913 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11914 BFD_ASSERT (! abfd->output_has_begun);
11915 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11916 goto error_return;
11917
ee75fd95 11918 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11919 for (o = abfd->sections; o != NULL; o = o->next)
11920 {
d4730f92 11921 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11922 if ((o->flags & SEC_RELOC) != 0)
11923 {
d4730f92 11924 if (esdo->rel.hdr
9eaff861 11925 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11926 goto error_return;
11927
d4730f92 11928 if (esdo->rela.hdr
9eaff861 11929 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11930 goto error_return;
11931 }
11932
11933 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11934 to count upwards while actually outputting the relocations. */
d4730f92
BS
11935 esdo->rel.count = 0;
11936 esdo->rela.count = 0;
0ce398f1
L
11937
11938 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11939 {
11940 /* Cache the section contents so that they can be compressed
11941 later. Use bfd_malloc since it will be freed by
11942 bfd_compress_section_contents. */
11943 unsigned char *contents = esdo->this_hdr.contents;
11944 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11945 abort ();
11946 contents
11947 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11948 if (contents == NULL)
11949 goto error_return;
11950 esdo->this_hdr.contents = contents;
11951 }
c152c796
AM
11952 }
11953
c152c796 11954 /* We have now assigned file positions for all the sections except
a485e98e
AM
11955 .symtab, .strtab, and non-loaded reloc sections. We start the
11956 .symtab section at the current file position, and write directly
11957 to it. We build the .strtab section in memory. */
c152c796
AM
11958 bfd_get_symcount (abfd) = 0;
11959 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11960 /* sh_name is set in prep_headers. */
11961 symtab_hdr->sh_type = SHT_SYMTAB;
11962 /* sh_flags, sh_addr and sh_size all start off zero. */
11963 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11964 /* sh_link is set in assign_section_numbers. */
11965 /* sh_info is set below. */
11966 /* sh_offset is set just below. */
72de5009 11967 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11968
ef10c3ac
L
11969 if (max_sym_count < 20)
11970 max_sym_count = 20;
64f52338 11971 htab->strtabsize = max_sym_count;
ef10c3ac 11972 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11973 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11974 if (htab->strtab == NULL)
c152c796 11975 goto error_return;
ef10c3ac
L
11976 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11977 flinfo.symshndxbuf
11978 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11979 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11980
8539e4e8 11981 if (info->strip != strip_all || emit_relocs)
c152c796 11982 {
8539e4e8
AM
11983 file_ptr off = elf_next_file_pos (abfd);
11984
11985 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11986
11987 /* Note that at this point elf_next_file_pos (abfd) is
11988 incorrect. We do not yet know the size of the .symtab section.
11989 We correct next_file_pos below, after we do know the size. */
11990
11991 /* Start writing out the symbol table. The first symbol is always a
11992 dummy symbol. */
c152c796
AM
11993 elfsym.st_value = 0;
11994 elfsym.st_size = 0;
11995 elfsym.st_info = 0;
11996 elfsym.st_other = 0;
11997 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11998 elfsym.st_target_internal = 0;
ef10c3ac
L
11999 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12000 bfd_und_section_ptr, NULL) != 1)
c152c796 12001 goto error_return;
c152c796 12002
8539e4e8
AM
12003 /* Output a symbol for each section. We output these even if we are
12004 discarding local symbols, since they are used for relocs. These
12005 symbols have no names. We store the index of each one in the
12006 index field of the section, so that we can find it again when
12007 outputting relocs. */
12008
c152c796
AM
12009 elfsym.st_size = 0;
12010 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12011 elfsym.st_other = 0;
f0b5bb34 12012 elfsym.st_value = 0;
35fc36a8 12013 elfsym.st_target_internal = 0;
c152c796
AM
12014 for (i = 1; i < elf_numsections (abfd); i++)
12015 {
12016 o = bfd_section_from_elf_index (abfd, i);
12017 if (o != NULL)
f0b5bb34
AM
12018 {
12019 o->target_index = bfd_get_symcount (abfd);
12020 elfsym.st_shndx = i;
0e1862bb 12021 if (!bfd_link_relocatable (info))
f0b5bb34 12022 elfsym.st_value = o->vma;
ef10c3ac
L
12023 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12024 NULL) != 1)
f0b5bb34
AM
12025 goto error_return;
12026 }
c152c796
AM
12027 }
12028 }
12029
12030 /* Allocate some memory to hold information read in from the input
12031 files. */
12032 if (max_contents_size != 0)
12033 {
8b127cbc
AM
12034 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12035 if (flinfo.contents == NULL)
c152c796
AM
12036 goto error_return;
12037 }
12038
12039 if (max_external_reloc_size != 0)
12040 {
8b127cbc
AM
12041 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12042 if (flinfo.external_relocs == NULL)
c152c796
AM
12043 goto error_return;
12044 }
12045
12046 if (max_internal_reloc_count != 0)
12047 {
056bafd4 12048 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12049 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12050 if (flinfo.internal_relocs == NULL)
c152c796
AM
12051 goto error_return;
12052 }
12053
12054 if (max_sym_count != 0)
12055 {
12056 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12057 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12058 if (flinfo.external_syms == NULL)
c152c796
AM
12059 goto error_return;
12060
12061 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12062 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12063 if (flinfo.internal_syms == NULL)
c152c796
AM
12064 goto error_return;
12065
12066 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12067 flinfo.indices = (long int *) bfd_malloc (amt);
12068 if (flinfo.indices == NULL)
c152c796
AM
12069 goto error_return;
12070
12071 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12072 flinfo.sections = (asection **) bfd_malloc (amt);
12073 if (flinfo.sections == NULL)
c152c796
AM
12074 goto error_return;
12075 }
12076
12077 if (max_sym_shndx_count != 0)
12078 {
12079 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12080 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12081 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12082 goto error_return;
12083 }
12084
64f52338 12085 if (htab->tls_sec)
c152c796
AM
12086 {
12087 bfd_vma base, end = 0;
12088 asection *sec;
12089
64f52338 12090 for (sec = htab->tls_sec;
c152c796
AM
12091 sec && (sec->flags & SEC_THREAD_LOCAL);
12092 sec = sec->next)
12093 {
3a800eb9 12094 bfd_size_type size = sec->size;
c152c796 12095
3a800eb9
AM
12096 if (size == 0
12097 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12098 {
91d6fa6a
NC
12099 struct bfd_link_order *ord = sec->map_tail.link_order;
12100
12101 if (ord != NULL)
12102 size = ord->offset + ord->size;
c152c796
AM
12103 }
12104 end = sec->vma + size;
12105 }
64f52338 12106 base = htab->tls_sec->vma;
7dc98aea
RO
12107 /* Only align end of TLS section if static TLS doesn't have special
12108 alignment requirements. */
12109 if (bed->static_tls_alignment == 1)
64f52338
AM
12110 end = align_power (end, htab->tls_sec->alignment_power);
12111 htab->tls_size = end - base;
c152c796
AM
12112 }
12113
0b52efa6
PB
12114 /* Reorder SHF_LINK_ORDER sections. */
12115 for (o = abfd->sections; o != NULL; o = o->next)
12116 {
12117 if (!elf_fixup_link_order (abfd, o))
12118 return FALSE;
12119 }
12120
2f0c68f2
CM
12121 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12122 return FALSE;
12123
c152c796
AM
12124 /* Since ELF permits relocations to be against local symbols, we
12125 must have the local symbols available when we do the relocations.
12126 Since we would rather only read the local symbols once, and we
12127 would rather not keep them in memory, we handle all the
12128 relocations for a single input file at the same time.
12129
12130 Unfortunately, there is no way to know the total number of local
12131 symbols until we have seen all of them, and the local symbol
12132 indices precede the global symbol indices. This means that when
12133 we are generating relocatable output, and we see a reloc against
12134 a global symbol, we can not know the symbol index until we have
12135 finished examining all the local symbols to see which ones we are
12136 going to output. To deal with this, we keep the relocations in
12137 memory, and don't output them until the end of the link. This is
12138 an unfortunate waste of memory, but I don't see a good way around
12139 it. Fortunately, it only happens when performing a relocatable
12140 link, which is not the common case. FIXME: If keep_memory is set
12141 we could write the relocs out and then read them again; I don't
12142 know how bad the memory loss will be. */
12143
c72f2fb2 12144 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12145 sub->output_has_begun = FALSE;
12146 for (o = abfd->sections; o != NULL; o = o->next)
12147 {
8423293d 12148 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12149 {
12150 if (p->type == bfd_indirect_link_order
12151 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12152 == bfd_target_elf_flavour)
12153 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12154 {
12155 if (! sub->output_has_begun)
12156 {
8b127cbc 12157 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12158 goto error_return;
12159 sub->output_has_begun = TRUE;
12160 }
12161 }
12162 else if (p->type == bfd_section_reloc_link_order
12163 || p->type == bfd_symbol_reloc_link_order)
12164 {
12165 if (! elf_reloc_link_order (abfd, info, o, p))
12166 goto error_return;
12167 }
12168 else
12169 {
12170 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12171 {
12172 if (p->type == bfd_indirect_link_order
12173 && (bfd_get_flavour (sub)
12174 == bfd_target_elf_flavour)
12175 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12176 != bed->s->elfclass))
12177 {
12178 const char *iclass, *oclass;
12179
aebf9be7 12180 switch (bed->s->elfclass)
351f65ca 12181 {
aebf9be7
NC
12182 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12183 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12184 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12185 default: abort ();
351f65ca 12186 }
aebf9be7
NC
12187
12188 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12189 {
aebf9be7
NC
12190 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12191 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12192 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12193 default: abort ();
351f65ca
L
12194 }
12195
12196 bfd_set_error (bfd_error_wrong_format);
4eca0228 12197 _bfd_error_handler
695344c0 12198 /* xgettext:c-format */
871b3ab2 12199 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12200 sub, iclass, oclass);
12201 }
12202
12203 goto error_return;
12204 }
c152c796
AM
12205 }
12206 }
12207 }
12208
c0f00686
L
12209 /* Free symbol buffer if needed. */
12210 if (!info->reduce_memory_overheads)
12211 {
c72f2fb2 12212 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12213 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12214 && elf_tdata (sub)->symbuf)
c0f00686
L
12215 {
12216 free (elf_tdata (sub)->symbuf);
12217 elf_tdata (sub)->symbuf = NULL;
12218 }
12219 }
12220
c152c796
AM
12221 /* Output any global symbols that got converted to local in a
12222 version script or due to symbol visibility. We do this in a
12223 separate step since ELF requires all local symbols to appear
12224 prior to any global symbols. FIXME: We should only do this if
12225 some global symbols were, in fact, converted to become local.
12226 FIXME: Will this work correctly with the Irix 5 linker? */
12227 eoinfo.failed = FALSE;
8b127cbc 12228 eoinfo.flinfo = &flinfo;
c152c796 12229 eoinfo.localsyms = TRUE;
34a79995 12230 eoinfo.file_sym_done = FALSE;
7686d77d 12231 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12232 if (eoinfo.failed)
12233 return FALSE;
12234
4e617b1e
PB
12235 /* If backend needs to output some local symbols not present in the hash
12236 table, do it now. */
8539e4e8
AM
12237 if (bed->elf_backend_output_arch_local_syms
12238 && (info->strip != strip_all || emit_relocs))
4e617b1e 12239 {
6e0b88f1 12240 typedef int (*out_sym_func)
4e617b1e
PB
12241 (void *, const char *, Elf_Internal_Sym *, asection *,
12242 struct elf_link_hash_entry *);
12243
12244 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12245 (abfd, info, &flinfo,
12246 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12247 return FALSE;
12248 }
12249
c152c796
AM
12250 /* That wrote out all the local symbols. Finish up the symbol table
12251 with the global symbols. Even if we want to strip everything we
12252 can, we still need to deal with those global symbols that got
12253 converted to local in a version script. */
12254
12255 /* The sh_info field records the index of the first non local symbol. */
12256 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12257
12258 if (dynamic
64f52338
AM
12259 && htab->dynsym != NULL
12260 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12261 {
12262 Elf_Internal_Sym sym;
64f52338 12263 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12264
64f52338
AM
12265 o = htab->dynsym->output_section;
12266 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12267
12268 /* Write out the section symbols for the output sections. */
0e1862bb 12269 if (bfd_link_pic (info)
64f52338 12270 || htab->is_relocatable_executable)
c152c796
AM
12271 {
12272 asection *s;
12273
12274 sym.st_size = 0;
12275 sym.st_name = 0;
12276 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12277 sym.st_other = 0;
35fc36a8 12278 sym.st_target_internal = 0;
c152c796
AM
12279
12280 for (s = abfd->sections; s != NULL; s = s->next)
12281 {
12282 int indx;
12283 bfd_byte *dest;
12284 long dynindx;
12285
c152c796 12286 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12287 if (dynindx <= 0)
12288 continue;
12289 indx = elf_section_data (s)->this_idx;
c152c796
AM
12290 BFD_ASSERT (indx > 0);
12291 sym.st_shndx = indx;
c0d5a53d
L
12292 if (! check_dynsym (abfd, &sym))
12293 return FALSE;
c152c796
AM
12294 sym.st_value = s->vma;
12295 dest = dynsym + dynindx * bed->s->sizeof_sym;
12296 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12297 }
c152c796
AM
12298 }
12299
12300 /* Write out the local dynsyms. */
64f52338 12301 if (htab->dynlocal)
c152c796
AM
12302 {
12303 struct elf_link_local_dynamic_entry *e;
64f52338 12304 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12305 {
12306 asection *s;
12307 bfd_byte *dest;
12308
935bd1e0 12309 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12310 Note that we saved a word of storage and overwrote
12311 the original st_name with the dynstr_index. */
12312 sym = e->isym;
935bd1e0 12313 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12314
cb33740c
AM
12315 s = bfd_section_from_elf_index (e->input_bfd,
12316 e->isym.st_shndx);
12317 if (s != NULL)
c152c796 12318 {
c152c796
AM
12319 sym.st_shndx =
12320 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12321 if (! check_dynsym (abfd, &sym))
12322 return FALSE;
c152c796
AM
12323 sym.st_value = (s->output_section->vma
12324 + s->output_offset
12325 + e->isym.st_value);
12326 }
12327
c152c796
AM
12328 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12329 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12330 }
12331 }
c152c796
AM
12332 }
12333
12334 /* We get the global symbols from the hash table. */
12335 eoinfo.failed = FALSE;
12336 eoinfo.localsyms = FALSE;
8b127cbc 12337 eoinfo.flinfo = &flinfo;
7686d77d 12338 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12339 if (eoinfo.failed)
12340 return FALSE;
12341
12342 /* If backend needs to output some symbols not present in the hash
12343 table, do it now. */
8539e4e8
AM
12344 if (bed->elf_backend_output_arch_syms
12345 && (info->strip != strip_all || emit_relocs))
c152c796 12346 {
6e0b88f1 12347 typedef int (*out_sym_func)
c152c796
AM
12348 (void *, const char *, Elf_Internal_Sym *, asection *,
12349 struct elf_link_hash_entry *);
12350
12351 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12352 (abfd, info, &flinfo,
12353 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12354 return FALSE;
12355 }
12356
ef10c3ac
L
12357 /* Finalize the .strtab section. */
12358 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12359
12360 /* Swap out the .strtab section. */
12361 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12362 return FALSE;
12363
12364 /* Now we know the size of the symtab section. */
c152c796
AM
12365 if (bfd_get_symcount (abfd) > 0)
12366 {
ee3b52e9
L
12367 /* Finish up and write out the symbol string table (.strtab)
12368 section. */
ad32986f 12369 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12370 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12371
ad32986f 12372 if (elf_symtab_shndx_list (abfd))
8539e4e8 12373 {
ad32986f 12374 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12375
ad32986f
NC
12376 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12377 {
12378 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12379 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12380 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12381 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12382 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12383
ad32986f
NC
12384 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12385 off, TRUE);
12386
12387 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12388 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12389 return FALSE;
12390 }
8539e4e8 12391 }
ee3b52e9
L
12392
12393 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12394 /* sh_name was set in prep_headers. */
12395 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12396 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12397 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12398 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12399 symstrtab_hdr->sh_entsize = 0;
12400 symstrtab_hdr->sh_link = 0;
12401 symstrtab_hdr->sh_info = 0;
12402 /* sh_offset is set just below. */
12403 symstrtab_hdr->sh_addralign = 1;
12404
12405 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12406 off, TRUE);
12407 elf_next_file_pos (abfd) = off;
12408
c152c796 12409 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12410 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12411 return FALSE;
12412 }
12413
76359541
TP
12414 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12415 {
871b3ab2 12416 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12417 info->out_implib_bfd);
76359541
TP
12418 return FALSE;
12419 }
12420
c152c796
AM
12421 /* Adjust the relocs to have the correct symbol indices. */
12422 for (o = abfd->sections; o != NULL; o = o->next)
12423 {
d4730f92 12424 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12425 bfd_boolean sort;
10bbbc1d 12426
c152c796
AM
12427 if ((o->flags & SEC_RELOC) == 0)
12428 continue;
12429
28dbcedc 12430 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12431 if (esdo->rel.hdr != NULL
10bbbc1d 12432 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12433 return FALSE;
12434 if (esdo->rela.hdr != NULL
10bbbc1d 12435 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12436 return FALSE;
c152c796
AM
12437
12438 /* Set the reloc_count field to 0 to prevent write_relocs from
12439 trying to swap the relocs out itself. */
12440 o->reloc_count = 0;
12441 }
12442
12443 if (dynamic && info->combreloc && dynobj != NULL)
12444 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12445
12446 /* If we are linking against a dynamic object, or generating a
12447 shared library, finish up the dynamic linking information. */
12448 if (dynamic)
12449 {
12450 bfd_byte *dyncon, *dynconend;
12451
12452 /* Fix up .dynamic entries. */
3d4d4302 12453 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12454 BFD_ASSERT (o != NULL);
12455
12456 dyncon = o->contents;
eea6121a 12457 dynconend = o->contents + o->size;
c152c796
AM
12458 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12459 {
12460 Elf_Internal_Dyn dyn;
12461 const char *name;
12462 unsigned int type;
64487780
AM
12463 bfd_size_type sh_size;
12464 bfd_vma sh_addr;
c152c796
AM
12465
12466 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12467
12468 switch (dyn.d_tag)
12469 {
12470 default:
12471 continue;
12472 case DT_NULL:
12473 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12474 {
12475 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12476 {
12477 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12478 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12479 default: continue;
12480 }
12481 dyn.d_un.d_val = relativecount;
12482 relativecount = 0;
12483 break;
12484 }
12485 continue;
12486
12487 case DT_INIT:
12488 name = info->init_function;
12489 goto get_sym;
12490 case DT_FINI:
12491 name = info->fini_function;
12492 get_sym:
12493 {
12494 struct elf_link_hash_entry *h;
12495
64f52338 12496 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12497 if (h != NULL
12498 && (h->root.type == bfd_link_hash_defined
12499 || h->root.type == bfd_link_hash_defweak))
12500 {
bef26483 12501 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12502 o = h->root.u.def.section;
12503 if (o->output_section != NULL)
bef26483 12504 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12505 + o->output_offset);
12506 else
12507 {
12508 /* The symbol is imported from another shared
12509 library and does not apply to this one. */
bef26483 12510 dyn.d_un.d_ptr = 0;
c152c796
AM
12511 }
12512 break;
12513 }
12514 }
12515 continue;
12516
12517 case DT_PREINIT_ARRAYSZ:
12518 name = ".preinit_array";
4ade44b7 12519 goto get_out_size;
c152c796
AM
12520 case DT_INIT_ARRAYSZ:
12521 name = ".init_array";
4ade44b7 12522 goto get_out_size;
c152c796
AM
12523 case DT_FINI_ARRAYSZ:
12524 name = ".fini_array";
4ade44b7 12525 get_out_size:
c152c796
AM
12526 o = bfd_get_section_by_name (abfd, name);
12527 if (o == NULL)
12528 {
4eca0228 12529 _bfd_error_handler
4ade44b7 12530 (_("could not find section %s"), name);
c152c796
AM
12531 goto error_return;
12532 }
eea6121a 12533 if (o->size == 0)
4eca0228 12534 _bfd_error_handler
c152c796 12535 (_("warning: %s section has zero size"), name);
eea6121a 12536 dyn.d_un.d_val = o->size;
c152c796
AM
12537 break;
12538
12539 case DT_PREINIT_ARRAY:
12540 name = ".preinit_array";
4ade44b7 12541 goto get_out_vma;
c152c796
AM
12542 case DT_INIT_ARRAY:
12543 name = ".init_array";
4ade44b7 12544 goto get_out_vma;
c152c796
AM
12545 case DT_FINI_ARRAY:
12546 name = ".fini_array";
4ade44b7
AM
12547 get_out_vma:
12548 o = bfd_get_section_by_name (abfd, name);
12549 goto do_vma;
c152c796
AM
12550
12551 case DT_HASH:
12552 name = ".hash";
12553 goto get_vma;
fdc90cb4
JJ
12554 case DT_GNU_HASH:
12555 name = ".gnu.hash";
12556 goto get_vma;
c152c796
AM
12557 case DT_STRTAB:
12558 name = ".dynstr";
12559 goto get_vma;
12560 case DT_SYMTAB:
12561 name = ".dynsym";
12562 goto get_vma;
12563 case DT_VERDEF:
12564 name = ".gnu.version_d";
12565 goto get_vma;
12566 case DT_VERNEED:
12567 name = ".gnu.version_r";
12568 goto get_vma;
12569 case DT_VERSYM:
12570 name = ".gnu.version";
12571 get_vma:
4ade44b7
AM
12572 o = bfd_get_linker_section (dynobj, name);
12573 do_vma:
b3293efa 12574 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12575 {
4eca0228 12576 _bfd_error_handler
4ade44b7 12577 (_("could not find section %s"), name);
c152c796
AM
12578 goto error_return;
12579 }
894891db
NC
12580 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12581 {
4eca0228 12582 _bfd_error_handler
894891db
NC
12583 (_("warning: section '%s' is being made into a note"), name);
12584 bfd_set_error (bfd_error_nonrepresentable_section);
12585 goto error_return;
12586 }
4ade44b7 12587 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12588 break;
12589
12590 case DT_REL:
12591 case DT_RELA:
12592 case DT_RELSZ:
12593 case DT_RELASZ:
12594 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12595 type = SHT_REL;
12596 else
12597 type = SHT_RELA;
64487780
AM
12598 sh_size = 0;
12599 sh_addr = 0;
c152c796
AM
12600 for (i = 1; i < elf_numsections (abfd); i++)
12601 {
12602 Elf_Internal_Shdr *hdr;
12603
12604 hdr = elf_elfsections (abfd)[i];
12605 if (hdr->sh_type == type
12606 && (hdr->sh_flags & SHF_ALLOC) != 0)
12607 {
64487780
AM
12608 sh_size += hdr->sh_size;
12609 if (sh_addr == 0
12610 || sh_addr > hdr->sh_addr)
12611 sh_addr = hdr->sh_addr;
c152c796
AM
12612 }
12613 }
64487780 12614
64f52338
AM
12615 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12616 {
12617 /* Don't count procedure linkage table relocs in the
12618 overall reloc count. */
64487780
AM
12619 sh_size -= htab->srelplt->size;
12620 if (sh_size == 0)
12621 /* If the size is zero, make the address zero too.
12622 This is to avoid a glibc bug. If the backend
12623 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12624 zero, then we'll put DT_RELA at the end of
12625 DT_JMPREL. glibc will interpret the end of
12626 DT_RELA matching the end of DT_JMPREL as the
12627 case where DT_RELA includes DT_JMPREL, and for
12628 LD_BIND_NOW will decide that processing DT_RELA
12629 will process the PLT relocs too. Net result:
12630 No PLT relocs applied. */
12631 sh_addr = 0;
12632
64f52338
AM
12633 /* If .rela.plt is the first .rela section, exclude
12634 it from DT_RELA. */
64487780
AM
12635 else if (sh_addr == (htab->srelplt->output_section->vma
12636 + htab->srelplt->output_offset))
12637 sh_addr += htab->srelplt->size;
64f52338 12638 }
64487780
AM
12639
12640 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12641 dyn.d_un.d_val = sh_size;
12642 else
12643 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12644 break;
12645 }
12646 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12647 }
12648 }
12649
12650 /* If we have created any dynamic sections, then output them. */
12651 if (dynobj != NULL)
12652 {
12653 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12654 goto error_return;
12655
943284cc 12656 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12657 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12658 || info->error_textrel)
3d4d4302 12659 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12660 {
12661 bfd_byte *dyncon, *dynconend;
12662
943284cc
DJ
12663 dyncon = o->contents;
12664 dynconend = o->contents + o->size;
12665 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12666 {
12667 Elf_Internal_Dyn dyn;
12668
12669 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12670
12671 if (dyn.d_tag == DT_TEXTREL)
12672 {
c192a133
AM
12673 if (info->error_textrel)
12674 info->callbacks->einfo
9793eb77 12675 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12676 else
12677 info->callbacks->einfo
9793eb77 12678 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12679 break;
12680 }
12681 }
12682 }
12683
c152c796
AM
12684 for (o = dynobj->sections; o != NULL; o = o->next)
12685 {
12686 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12687 || o->size == 0
c152c796
AM
12688 || o->output_section == bfd_abs_section_ptr)
12689 continue;
12690 if ((o->flags & SEC_LINKER_CREATED) == 0)
12691 {
12692 /* At this point, we are only interested in sections
12693 created by _bfd_elf_link_create_dynamic_sections. */
12694 continue;
12695 }
64f52338 12696 if (htab->stab_info.stabstr == o)
3722b82f 12697 continue;
64f52338 12698 if (htab->eh_info.hdr_sec == o)
eea6121a 12699 continue;
3d4d4302 12700 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12701 {
12702 if (! bfd_set_section_contents (abfd, o->output_section,
12703 o->contents,
37b01f6a
DG
12704 (file_ptr) o->output_offset
12705 * bfd_octets_per_byte (abfd),
eea6121a 12706 o->size))
c152c796
AM
12707 goto error_return;
12708 }
12709 else
12710 {
12711 /* The contents of the .dynstr section are actually in a
12712 stringtab. */
8539e4e8
AM
12713 file_ptr off;
12714
c152c796
AM
12715 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12716 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12717 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12718 goto error_return;
12719 }
12720 }
12721 }
12722
7bdf4127 12723 if (!info->resolve_section_groups)
c152c796
AM
12724 {
12725 bfd_boolean failed = FALSE;
12726
7bdf4127 12727 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12728 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12729 if (failed)
12730 goto error_return;
12731 }
12732
12733 /* If we have optimized stabs strings, output them. */
64f52338 12734 if (htab->stab_info.stabstr != NULL)
c152c796 12735 {
64f52338 12736 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12737 goto error_return;
12738 }
12739
9f7c3e5e
AM
12740 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12741 goto error_return;
c152c796 12742
9f7c3e5e 12743 elf_final_link_free (abfd, &flinfo);
c152c796 12744
12bd6957 12745 elf_linker (abfd) = TRUE;
c152c796 12746
104d59d1
JM
12747 if (attr_section)
12748 {
a50b1753 12749 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12750 if (contents == NULL)
d0f16d5e 12751 return FALSE; /* Bail out and fail. */
104d59d1
JM
12752 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12753 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12754 free (contents);
12755 }
12756
c152c796
AM
12757 return TRUE;
12758
12759 error_return:
9f7c3e5e 12760 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12761 return FALSE;
12762}
12763\f
5241d853
RS
12764/* Initialize COOKIE for input bfd ABFD. */
12765
12766static bfd_boolean
12767init_reloc_cookie (struct elf_reloc_cookie *cookie,
12768 struct bfd_link_info *info, bfd *abfd)
12769{
12770 Elf_Internal_Shdr *symtab_hdr;
12771 const struct elf_backend_data *bed;
12772
12773 bed = get_elf_backend_data (abfd);
12774 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12775
12776 cookie->abfd = abfd;
12777 cookie->sym_hashes = elf_sym_hashes (abfd);
12778 cookie->bad_symtab = elf_bad_symtab (abfd);
12779 if (cookie->bad_symtab)
12780 {
12781 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12782 cookie->extsymoff = 0;
12783 }
12784 else
12785 {
12786 cookie->locsymcount = symtab_hdr->sh_info;
12787 cookie->extsymoff = symtab_hdr->sh_info;
12788 }
12789
12790 if (bed->s->arch_size == 32)
12791 cookie->r_sym_shift = 8;
12792 else
12793 cookie->r_sym_shift = 32;
12794
12795 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12796 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12797 {
12798 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12799 cookie->locsymcount, 0,
12800 NULL, NULL, NULL);
12801 if (cookie->locsyms == NULL)
12802 {
12803 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12804 return FALSE;
12805 }
12806 if (info->keep_memory)
12807 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12808 }
12809 return TRUE;
12810}
12811
12812/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12813
12814static void
12815fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12816{
12817 Elf_Internal_Shdr *symtab_hdr;
12818
12819 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12820 if (cookie->locsyms != NULL
12821 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12822 free (cookie->locsyms);
12823}
12824
12825/* Initialize the relocation information in COOKIE for input section SEC
12826 of input bfd ABFD. */
12827
12828static bfd_boolean
12829init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12830 struct bfd_link_info *info, bfd *abfd,
12831 asection *sec)
12832{
5241d853
RS
12833 if (sec->reloc_count == 0)
12834 {
12835 cookie->rels = NULL;
12836 cookie->relend = NULL;
12837 }
12838 else
12839 {
5241d853
RS
12840 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12841 info->keep_memory);
12842 if (cookie->rels == NULL)
12843 return FALSE;
12844 cookie->rel = cookie->rels;
056bafd4 12845 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12846 }
12847 cookie->rel = cookie->rels;
12848 return TRUE;
12849}
12850
12851/* Free the memory allocated by init_reloc_cookie_rels,
12852 if appropriate. */
12853
12854static void
12855fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12856 asection *sec)
12857{
12858 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12859 free (cookie->rels);
12860}
12861
12862/* Initialize the whole of COOKIE for input section SEC. */
12863
12864static bfd_boolean
12865init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12866 struct bfd_link_info *info,
12867 asection *sec)
12868{
12869 if (!init_reloc_cookie (cookie, info, sec->owner))
12870 goto error1;
12871 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12872 goto error2;
12873 return TRUE;
12874
12875 error2:
12876 fini_reloc_cookie (cookie, sec->owner);
12877 error1:
12878 return FALSE;
12879}
12880
12881/* Free the memory allocated by init_reloc_cookie_for_section,
12882 if appropriate. */
12883
12884static void
12885fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12886 asection *sec)
12887{
12888 fini_reloc_cookie_rels (cookie, sec);
12889 fini_reloc_cookie (cookie, sec->owner);
12890}
12891\f
c152c796
AM
12892/* Garbage collect unused sections. */
12893
07adf181
AM
12894/* Default gc_mark_hook. */
12895
12896asection *
12897_bfd_elf_gc_mark_hook (asection *sec,
12898 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12899 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12900 struct elf_link_hash_entry *h,
12901 Elf_Internal_Sym *sym)
12902{
12903 if (h != NULL)
12904 {
12905 switch (h->root.type)
12906 {
12907 case bfd_link_hash_defined:
12908 case bfd_link_hash_defweak:
12909 return h->root.u.def.section;
12910
12911 case bfd_link_hash_common:
12912 return h->root.u.c.p->section;
12913
12914 default:
12915 break;
12916 }
12917 }
12918 else
12919 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12920
12921 return NULL;
12922}
12923
9e223787 12924/* Return the debug definition section. */
b7c871ed
L
12925
12926static asection *
12927elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12928 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12929 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12930 struct elf_link_hash_entry *h,
9e223787 12931 Elf_Internal_Sym *sym)
b7c871ed 12932{
9e223787
L
12933 if (h != NULL)
12934 {
12935 /* Return the global debug definition section. */
12936 if ((h->root.type == bfd_link_hash_defined
12937 || h->root.type == bfd_link_hash_defweak)
12938 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12939 return h->root.u.def.section;
12940 }
12941 else
12942 {
12943 /* Return the local debug definition section. */
12944 asection *isec = bfd_section_from_elf_index (sec->owner,
12945 sym->st_shndx);
12946 if ((isec->flags & SEC_DEBUGGING) != 0)
12947 return isec;
12948 }
b7c871ed
L
12949
12950 return NULL;
12951}
12952
5241d853
RS
12953/* COOKIE->rel describes a relocation against section SEC, which is
12954 a section we've decided to keep. Return the section that contains
12955 the relocation symbol, or NULL if no section contains it. */
12956
12957asection *
12958_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12959 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12960 struct elf_reloc_cookie *cookie,
12961 bfd_boolean *start_stop)
5241d853
RS
12962{
12963 unsigned long r_symndx;
12964 struct elf_link_hash_entry *h;
12965
12966 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12967 if (r_symndx == STN_UNDEF)
5241d853
RS
12968 return NULL;
12969
12970 if (r_symndx >= cookie->locsymcount
12971 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12972 {
12973 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12974 if (h == NULL)
12975 {
871b3ab2 12976 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
12977 sec->owner);
12978 return NULL;
12979 }
5241d853
RS
12980 while (h->root.type == bfd_link_hash_indirect
12981 || h->root.type == bfd_link_hash_warning)
12982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12983 h->mark = 1;
4e6b54a6
AM
12984 /* If this symbol is weak and there is a non-weak definition, we
12985 keep the non-weak definition because many backends put
12986 dynamic reloc info on the non-weak definition for code
12987 handling copy relocs. */
60d67dc8
AM
12988 if (h->is_weakalias)
12989 weakdef (h)->mark = 1;
1cce69b9 12990
a6a4679f 12991 if (start_stop != NULL)
1cce69b9 12992 {
7dba9362
AM
12993 /* To work around a glibc bug, mark XXX input sections
12994 when there is a reference to __start_XXX or __stop_XXX
12995 symbols. */
cbd0eecf 12996 if (h->start_stop)
1cce69b9 12997 {
cbd0eecf 12998 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12999 *start_stop = !s->gc_mark;
13000 return s;
1cce69b9
AM
13001 }
13002 }
13003
5241d853
RS
13004 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13005 }
13006
13007 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13008 &cookie->locsyms[r_symndx]);
13009}
13010
13011/* COOKIE->rel describes a relocation against section SEC, which is
13012 a section we've decided to keep. Mark the section that contains
9d0a14d3 13013 the relocation symbol. */
5241d853
RS
13014
13015bfd_boolean
13016_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13017 asection *sec,
13018 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13019 struct elf_reloc_cookie *cookie)
5241d853
RS
13020{
13021 asection *rsec;
1cce69b9 13022 bfd_boolean start_stop = FALSE;
5241d853 13023
1cce69b9
AM
13024 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13025 while (rsec != NULL)
5241d853 13026 {
1cce69b9
AM
13027 if (!rsec->gc_mark)
13028 {
13029 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13030 || (rsec->owner->flags & DYNAMIC) != 0)
13031 rsec->gc_mark = 1;
13032 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13033 return FALSE;
13034 }
13035 if (!start_stop)
13036 break;
199af150 13037 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13038 }
13039 return TRUE;
13040}
13041
07adf181
AM
13042/* The mark phase of garbage collection. For a given section, mark
13043 it and any sections in this section's group, and all the sections
13044 which define symbols to which it refers. */
13045
ccfa59ea
AM
13046bfd_boolean
13047_bfd_elf_gc_mark (struct bfd_link_info *info,
13048 asection *sec,
6a5bb875 13049 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13050{
13051 bfd_boolean ret;
9d0a14d3 13052 asection *group_sec, *eh_frame;
c152c796
AM
13053
13054 sec->gc_mark = 1;
13055
13056 /* Mark all the sections in the group. */
13057 group_sec = elf_section_data (sec)->next_in_group;
13058 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13059 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13060 return FALSE;
13061
13062 /* Look through the section relocs. */
13063 ret = TRUE;
9d0a14d3
RS
13064 eh_frame = elf_eh_frame_section (sec->owner);
13065 if ((sec->flags & SEC_RELOC) != 0
13066 && sec->reloc_count > 0
13067 && sec != eh_frame)
c152c796 13068 {
5241d853 13069 struct elf_reloc_cookie cookie;
c152c796 13070
5241d853
RS
13071 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13072 ret = FALSE;
c152c796 13073 else
c152c796 13074 {
5241d853 13075 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13076 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13077 {
13078 ret = FALSE;
13079 break;
13080 }
13081 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13082 }
13083 }
9d0a14d3
RS
13084
13085 if (ret && eh_frame && elf_fde_list (sec))
13086 {
13087 struct elf_reloc_cookie cookie;
13088
13089 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13090 ret = FALSE;
13091 else
13092 {
13093 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13094 gc_mark_hook, &cookie))
13095 ret = FALSE;
13096 fini_reloc_cookie_for_section (&cookie, eh_frame);
13097 }
13098 }
13099
2f0c68f2
CM
13100 eh_frame = elf_section_eh_frame_entry (sec);
13101 if (ret && eh_frame && !eh_frame->gc_mark)
13102 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13103 ret = FALSE;
13104
c152c796
AM
13105 return ret;
13106}
13107
3c758495
TG
13108/* Scan and mark sections in a special or debug section group. */
13109
13110static void
13111_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13112{
13113 /* Point to first section of section group. */
13114 asection *ssec;
13115 /* Used to iterate the section group. */
13116 asection *msec;
13117
13118 bfd_boolean is_special_grp = TRUE;
13119 bfd_boolean is_debug_grp = TRUE;
13120
13121 /* First scan to see if group contains any section other than debug
13122 and special section. */
13123 ssec = msec = elf_next_in_group (grp);
13124 do
13125 {
13126 if ((msec->flags & SEC_DEBUGGING) == 0)
13127 is_debug_grp = FALSE;
13128
13129 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13130 is_special_grp = FALSE;
13131
13132 msec = elf_next_in_group (msec);
13133 }
13134 while (msec != ssec);
13135
13136 /* If this is a pure debug section group or pure special section group,
13137 keep all sections in this group. */
13138 if (is_debug_grp || is_special_grp)
13139 {
13140 do
13141 {
13142 msec->gc_mark = 1;
13143 msec = elf_next_in_group (msec);
13144 }
13145 while (msec != ssec);
13146 }
13147}
13148
7f6ab9f8
AM
13149/* Keep debug and special sections. */
13150
13151bfd_boolean
13152_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13153 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13154{
13155 bfd *ibfd;
13156
c72f2fb2 13157 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13158 {
13159 asection *isec;
13160 bfd_boolean some_kept;
b40bf0a2 13161 bfd_boolean debug_frag_seen;
b7c871ed 13162 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13163
13164 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13165 continue;
57963c05
AM
13166 isec = ibfd->sections;
13167 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13168 continue;
7f6ab9f8 13169
b40bf0a2
NC
13170 /* Ensure all linker created sections are kept,
13171 see if any other section is already marked,
13172 and note if we have any fragmented debug sections. */
b7c871ed 13173 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13174 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13175 {
13176 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13177 isec->gc_mark = 1;
eb026f09
AM
13178 else if (isec->gc_mark
13179 && (isec->flags & SEC_ALLOC) != 0
13180 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13181 some_kept = TRUE;
b40bf0a2 13182
535b785f 13183 if (!debug_frag_seen
b40bf0a2
NC
13184 && (isec->flags & SEC_DEBUGGING)
13185 && CONST_STRNEQ (isec->name, ".debug_line."))
13186 debug_frag_seen = TRUE;
7f6ab9f8
AM
13187 }
13188
eb026f09
AM
13189 /* If no non-note alloc section in this file will be kept, then
13190 we can toss out the debug and special sections. */
7f6ab9f8
AM
13191 if (!some_kept)
13192 continue;
13193
13194 /* Keep debug and special sections like .comment when they are
3c758495
TG
13195 not part of a group. Also keep section groups that contain
13196 just debug sections or special sections. */
7f6ab9f8 13197 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13198 {
13199 if ((isec->flags & SEC_GROUP) != 0)
13200 _bfd_elf_gc_mark_debug_special_section_group (isec);
13201 else if (((isec->flags & SEC_DEBUGGING) != 0
13202 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13203 && elf_next_in_group (isec) == NULL)
13204 isec->gc_mark = 1;
b7c871ed
L
13205 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13206 has_kept_debug_info = TRUE;
3c758495 13207 }
b40bf0a2 13208
b40bf0a2
NC
13209 /* Look for CODE sections which are going to be discarded,
13210 and find and discard any fragmented debug sections which
13211 are associated with that code section. */
b7c871ed
L
13212 if (debug_frag_seen)
13213 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13214 if ((isec->flags & SEC_CODE) != 0
13215 && isec->gc_mark == 0)
13216 {
13217 unsigned int ilen;
13218 asection *dsec;
b40bf0a2 13219
b7c871ed 13220 ilen = strlen (isec->name);
b40bf0a2 13221
b7c871ed 13222 /* Association is determined by the name of the debug
07d6d2b8 13223 section containing the name of the code section as
b7c871ed
L
13224 a suffix. For example .debug_line.text.foo is a
13225 debug section associated with .text.foo. */
13226 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13227 {
13228 unsigned int dlen;
b40bf0a2 13229
b7c871ed
L
13230 if (dsec->gc_mark == 0
13231 || (dsec->flags & SEC_DEBUGGING) == 0)
13232 continue;
b40bf0a2 13233
b7c871ed 13234 dlen = strlen (dsec->name);
b40bf0a2 13235
b7c871ed
L
13236 if (dlen > ilen
13237 && strncmp (dsec->name + (dlen - ilen),
13238 isec->name, ilen) == 0)
b40bf0a2 13239 dsec->gc_mark = 0;
b7c871ed 13240 }
b40bf0a2 13241 }
b7c871ed
L
13242
13243 /* Mark debug sections referenced by kept debug sections. */
13244 if (has_kept_debug_info)
13245 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13246 if (isec->gc_mark
13247 && (isec->flags & SEC_DEBUGGING) != 0)
13248 if (!_bfd_elf_gc_mark (info, isec,
13249 elf_gc_mark_debug_section))
13250 return FALSE;
7f6ab9f8
AM
13251 }
13252 return TRUE;
13253}
13254
c152c796 13255static bfd_boolean
ccabcbe5 13256elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13257{
13258 bfd *sub;
ccabcbe5 13259 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13260
c72f2fb2 13261 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13262 {
13263 asection *o;
13264
b19a8f85 13265 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13266 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13267 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13268 continue;
57963c05
AM
13269 o = sub->sections;
13270 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13271 continue;
c152c796
AM
13272
13273 for (o = sub->sections; o != NULL; o = o->next)
13274 {
a33dafc3
L
13275 /* When any section in a section group is kept, we keep all
13276 sections in the section group. If the first member of
13277 the section group is excluded, we will also exclude the
13278 group section. */
13279 if (o->flags & SEC_GROUP)
13280 {
13281 asection *first = elf_next_in_group (o);
13282 o->gc_mark = first->gc_mark;
13283 }
c152c796 13284
1e7eae0d 13285 if (o->gc_mark)
c152c796
AM
13286 continue;
13287
13288 /* Skip sweeping sections already excluded. */
13289 if (o->flags & SEC_EXCLUDE)
13290 continue;
13291
13292 /* Since this is early in the link process, it is simple
13293 to remove a section from the output. */
13294 o->flags |= SEC_EXCLUDE;
13295
c55fe096 13296 if (info->print_gc_sections && o->size != 0)
695344c0 13297 /* xgettext:c-format */
9793eb77 13298 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13299 o, sub);
c152c796
AM
13300 }
13301 }
13302
c152c796
AM
13303 return TRUE;
13304}
13305
13306/* Propagate collected vtable information. This is called through
13307 elf_link_hash_traverse. */
13308
13309static bfd_boolean
13310elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13311{
c152c796 13312 /* Those that are not vtables. */
cbd0eecf
L
13313 if (h->start_stop
13314 || h->u2.vtable == NULL
13315 || h->u2.vtable->parent == NULL)
c152c796
AM
13316 return TRUE;
13317
13318 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13319 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13320 return TRUE;
13321
13322 /* If we've already been done, exit. */
cbd0eecf 13323 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13324 return TRUE;
13325
13326 /* Make sure the parent's table is up to date. */
cbd0eecf 13327 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13328
cbd0eecf 13329 if (h->u2.vtable->used == NULL)
c152c796
AM
13330 {
13331 /* None of this table's entries were referenced. Re-use the
13332 parent's table. */
cbd0eecf
L
13333 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13334 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13335 }
13336 else
13337 {
13338 size_t n;
13339 bfd_boolean *cu, *pu;
13340
13341 /* Or the parent's entries into ours. */
cbd0eecf 13342 cu = h->u2.vtable->used;
c152c796 13343 cu[-1] = TRUE;
cbd0eecf 13344 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13345 if (pu != NULL)
13346 {
13347 const struct elf_backend_data *bed;
13348 unsigned int log_file_align;
13349
13350 bed = get_elf_backend_data (h->root.u.def.section->owner);
13351 log_file_align = bed->s->log_file_align;
cbd0eecf 13352 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13353 while (n--)
13354 {
13355 if (*pu)
13356 *cu = TRUE;
13357 pu++;
13358 cu++;
13359 }
13360 }
13361 }
13362
13363 return TRUE;
13364}
13365
13366static bfd_boolean
13367elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13368{
13369 asection *sec;
13370 bfd_vma hstart, hend;
13371 Elf_Internal_Rela *relstart, *relend, *rel;
13372 const struct elf_backend_data *bed;
13373 unsigned int log_file_align;
13374
c152c796
AM
13375 /* Take care of both those symbols that do not describe vtables as
13376 well as those that are not loaded. */
cbd0eecf
L
13377 if (h->start_stop
13378 || h->u2.vtable == NULL
13379 || h->u2.vtable->parent == NULL)
c152c796
AM
13380 return TRUE;
13381
13382 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13383 || h->root.type == bfd_link_hash_defweak);
13384
13385 sec = h->root.u.def.section;
13386 hstart = h->root.u.def.value;
13387 hend = hstart + h->size;
13388
13389 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13390 if (!relstart)
13391 return *(bfd_boolean *) okp = FALSE;
13392 bed = get_elf_backend_data (sec->owner);
13393 log_file_align = bed->s->log_file_align;
13394
056bafd4 13395 relend = relstart + sec->reloc_count;
c152c796
AM
13396
13397 for (rel = relstart; rel < relend; ++rel)
13398 if (rel->r_offset >= hstart && rel->r_offset < hend)
13399 {
13400 /* If the entry is in use, do nothing. */
cbd0eecf
L
13401 if (h->u2.vtable->used
13402 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13403 {
13404 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13405 if (h->u2.vtable->used[entry])
c152c796
AM
13406 continue;
13407 }
13408 /* Otherwise, kill it. */
13409 rel->r_offset = rel->r_info = rel->r_addend = 0;
13410 }
13411
13412 return TRUE;
13413}
13414
87538722
AM
13415/* Mark sections containing dynamically referenced symbols. When
13416 building shared libraries, we must assume that any visible symbol is
13417 referenced. */
715df9b8 13418
64d03ab5
AM
13419bfd_boolean
13420bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13421{
87538722 13422 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13423 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13424
715df9b8
EB
13425 if ((h->root.type == bfd_link_hash_defined
13426 || h->root.type == bfd_link_hash_defweak)
d664fd41 13427 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13428 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13429 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13430 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13431 && (!bfd_link_executable (info)
22185505 13432 || info->gc_keep_exported
b407645f
AM
13433 || info->export_dynamic
13434 || (h->dynamic
13435 && d != NULL
13436 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13437 && (h->versioned >= versioned
54e8959c
L
13438 || !bfd_hide_sym_by_version (info->version_info,
13439 h->root.root.string)))))
715df9b8
EB
13440 h->root.u.def.section->flags |= SEC_KEEP;
13441
13442 return TRUE;
13443}
3b36f7e6 13444
74f0fb50
AM
13445/* Keep all sections containing symbols undefined on the command-line,
13446 and the section containing the entry symbol. */
13447
13448void
13449_bfd_elf_gc_keep (struct bfd_link_info *info)
13450{
13451 struct bfd_sym_chain *sym;
13452
13453 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13454 {
13455 struct elf_link_hash_entry *h;
13456
13457 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13458 FALSE, FALSE, FALSE);
13459
13460 if (h != NULL
13461 && (h->root.type == bfd_link_hash_defined
13462 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13463 && !bfd_is_abs_section (h->root.u.def.section)
13464 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13465 h->root.u.def.section->flags |= SEC_KEEP;
13466 }
13467}
13468
2f0c68f2
CM
13469bfd_boolean
13470bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13471 struct bfd_link_info *info)
13472{
13473 bfd *ibfd = info->input_bfds;
13474
13475 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13476 {
13477 asection *sec;
13478 struct elf_reloc_cookie cookie;
13479
13480 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13481 continue;
57963c05
AM
13482 sec = ibfd->sections;
13483 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13484 continue;
2f0c68f2
CM
13485
13486 if (!init_reloc_cookie (&cookie, info, ibfd))
13487 return FALSE;
13488
13489 for (sec = ibfd->sections; sec; sec = sec->next)
13490 {
13491 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13492 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13493 {
13494 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13495 fini_reloc_cookie_rels (&cookie, sec);
13496 }
13497 }
13498 }
13499 return TRUE;
13500}
13501
c152c796
AM
13502/* Do mark and sweep of unused sections. */
13503
13504bfd_boolean
13505bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13506{
13507 bfd_boolean ok = TRUE;
13508 bfd *sub;
6a5bb875 13509 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13510 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13511 struct elf_link_hash_table *htab;
c152c796 13512
64d03ab5 13513 if (!bed->can_gc_sections
715df9b8 13514 || !is_elf_hash_table (info->hash))
c152c796 13515 {
9793eb77 13516 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13517 return TRUE;
13518 }
13519
74f0fb50 13520 bed->gc_keep (info);
da44f4e5 13521 htab = elf_hash_table (info);
74f0fb50 13522
9d0a14d3
RS
13523 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13524 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13525 for (sub = info->input_bfds;
13526 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13527 sub = sub->link.next)
9d0a14d3
RS
13528 {
13529 asection *sec;
13530 struct elf_reloc_cookie cookie;
13531
57963c05
AM
13532 sec = sub->sections;
13533 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13534 continue;
9d0a14d3 13535 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13536 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13537 {
13538 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13539 if (elf_section_data (sec)->sec_info
13540 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13541 elf_eh_frame_section (sub) = sec;
13542 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13543 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13544 }
13545 }
9d0a14d3 13546
c152c796 13547 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13548 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13549 if (!ok)
13550 return FALSE;
13551
13552 /* Kill the vtable relocations that were not used. */
da44f4e5 13553 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13554 if (!ok)
13555 return FALSE;
13556
715df9b8 13557 /* Mark dynamically referenced symbols. */
22185505 13558 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13559 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13560
715df9b8 13561 /* Grovel through relocs to find out who stays ... */
64d03ab5 13562 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13563 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13564 {
13565 asection *o;
13566
b19a8f85 13567 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13568 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13569 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13570 continue;
13571
57963c05
AM
13572 o = sub->sections;
13573 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13574 continue;
13575
7f6ab9f8
AM
13576 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13577 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13578 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13579 well as FINI_ARRAY sections for ld -r. */
c152c796 13580 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13581 if (!o->gc_mark
13582 && (o->flags & SEC_EXCLUDE) == 0
24007750 13583 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13584 || (bfd_link_relocatable (info)
13585 && ((elf_section_data (o)->this_hdr.sh_type
13586 == SHT_PREINIT_ARRAY)
13587 || (elf_section_data (o)->this_hdr.sh_type
13588 == SHT_INIT_ARRAY)
13589 || (elf_section_data (o)->this_hdr.sh_type
13590 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13591 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13592 && elf_next_in_group (o) == NULL )))
13593 {
13594 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13595 return FALSE;
13596 }
c152c796
AM
13597 }
13598
6a5bb875 13599 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13600 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13601
c152c796 13602 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13603 return elf_gc_sweep (abfd, info);
c152c796
AM
13604}
13605\f
13606/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13607
13608bfd_boolean
13609bfd_elf_gc_record_vtinherit (bfd *abfd,
13610 asection *sec,
13611 struct elf_link_hash_entry *h,
13612 bfd_vma offset)
13613{
13614 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13615 struct elf_link_hash_entry **search, *child;
ef53be89 13616 size_t extsymcount;
c152c796
AM
13617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13618
13619 /* The sh_info field of the symtab header tells us where the
13620 external symbols start. We don't care about the local symbols at
13621 this point. */
13622 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13623 if (!elf_bad_symtab (abfd))
13624 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13625
13626 sym_hashes = elf_sym_hashes (abfd);
13627 sym_hashes_end = sym_hashes + extsymcount;
13628
13629 /* Hunt down the child symbol, which is in this section at the same
13630 offset as the relocation. */
13631 for (search = sym_hashes; search != sym_hashes_end; ++search)
13632 {
13633 if ((child = *search) != NULL
13634 && (child->root.type == bfd_link_hash_defined
13635 || child->root.type == bfd_link_hash_defweak)
13636 && child->root.u.def.section == sec
13637 && child->root.u.def.value == offset)
13638 goto win;
13639 }
13640
695344c0 13641 /* xgettext:c-format */
9793eb77 13642 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13643 abfd, sec, (uint64_t) offset);
c152c796
AM
13644 bfd_set_error (bfd_error_invalid_operation);
13645 return FALSE;
13646
13647 win:
cbd0eecf 13648 if (!child->u2.vtable)
f6e332e6 13649 {
cbd0eecf
L
13650 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13651 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13652 if (!child->u2.vtable)
f6e332e6
AM
13653 return FALSE;
13654 }
c152c796
AM
13655 if (!h)
13656 {
13657 /* This *should* only be the absolute section. It could potentially
13658 be that someone has defined a non-global vtable though, which
13659 would be bad. It isn't worth paging in the local symbols to be
13660 sure though; that case should simply be handled by the assembler. */
13661
cbd0eecf 13662 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13663 }
13664 else
cbd0eecf 13665 child->u2.vtable->parent = h;
c152c796
AM
13666
13667 return TRUE;
13668}
13669
13670/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13671
13672bfd_boolean
13673bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13674 asection *sec ATTRIBUTE_UNUSED,
13675 struct elf_link_hash_entry *h,
13676 bfd_vma addend)
13677{
13678 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13679 unsigned int log_file_align = bed->s->log_file_align;
13680
cbd0eecf 13681 if (!h->u2.vtable)
f6e332e6 13682 {
cbd0eecf
L
13683 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13684 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13685 if (!h->u2.vtable)
f6e332e6
AM
13686 return FALSE;
13687 }
13688
cbd0eecf 13689 if (addend >= h->u2.vtable->size)
c152c796
AM
13690 {
13691 size_t size, bytes, file_align;
cbd0eecf 13692 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13693
13694 /* While the symbol is undefined, we have to be prepared to handle
13695 a zero size. */
13696 file_align = 1 << log_file_align;
13697 if (h->root.type == bfd_link_hash_undefined)
13698 size = addend + file_align;
13699 else
13700 {
13701 size = h->size;
13702 if (addend >= size)
13703 {
13704 /* Oops! We've got a reference past the defined end of
13705 the table. This is probably a bug -- shall we warn? */
13706 size = addend + file_align;
13707 }
13708 }
13709 size = (size + file_align - 1) & -file_align;
13710
13711 /* Allocate one extra entry for use as a "done" flag for the
13712 consolidation pass. */
13713 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13714
13715 if (ptr)
13716 {
a50b1753 13717 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13718
13719 if (ptr != NULL)
13720 {
13721 size_t oldbytes;
13722
cbd0eecf 13723 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13724 * sizeof (bfd_boolean));
13725 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13726 }
13727 }
13728 else
a50b1753 13729 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13730
13731 if (ptr == NULL)
13732 return FALSE;
13733
13734 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13735 h->u2.vtable->used = ptr + 1;
13736 h->u2.vtable->size = size;
c152c796
AM
13737 }
13738
cbd0eecf 13739 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13740
13741 return TRUE;
13742}
13743
ae17ab41
CM
13744/* Map an ELF section header flag to its corresponding string. */
13745typedef struct
13746{
13747 char *flag_name;
13748 flagword flag_value;
13749} elf_flags_to_name_table;
13750
13751static elf_flags_to_name_table elf_flags_to_names [] =
13752{
13753 { "SHF_WRITE", SHF_WRITE },
13754 { "SHF_ALLOC", SHF_ALLOC },
13755 { "SHF_EXECINSTR", SHF_EXECINSTR },
13756 { "SHF_MERGE", SHF_MERGE },
13757 { "SHF_STRINGS", SHF_STRINGS },
13758 { "SHF_INFO_LINK", SHF_INFO_LINK},
13759 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13760 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13761 { "SHF_GROUP", SHF_GROUP },
13762 { "SHF_TLS", SHF_TLS },
13763 { "SHF_MASKOS", SHF_MASKOS },
13764 { "SHF_EXCLUDE", SHF_EXCLUDE },
13765};
13766
b9c361e0
JL
13767/* Returns TRUE if the section is to be included, otherwise FALSE. */
13768bfd_boolean
ae17ab41 13769bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13770 struct flag_info *flaginfo,
b9c361e0 13771 asection *section)
ae17ab41 13772{
8b127cbc 13773 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13774
8b127cbc 13775 if (!flaginfo->flags_initialized)
ae17ab41 13776 {
8b127cbc
AM
13777 bfd *obfd = info->output_bfd;
13778 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13779 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13780 int with_hex = 0;
13781 int without_hex = 0;
13782
8b127cbc 13783 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13784 {
b9c361e0 13785 unsigned i;
8b127cbc 13786 flagword (*lookup) (char *);
ae17ab41 13787
8b127cbc
AM
13788 lookup = bed->elf_backend_lookup_section_flags_hook;
13789 if (lookup != NULL)
ae17ab41 13790 {
8b127cbc 13791 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13792
13793 if (hexval != 0)
13794 {
13795 if (tf->with == with_flags)
13796 with_hex |= hexval;
13797 else if (tf->with == without_flags)
13798 without_hex |= hexval;
13799 tf->valid = TRUE;
13800 continue;
13801 }
ae17ab41 13802 }
8b127cbc 13803 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13804 {
8b127cbc 13805 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13806 {
13807 if (tf->with == with_flags)
13808 with_hex |= elf_flags_to_names[i].flag_value;
13809 else if (tf->with == without_flags)
13810 without_hex |= elf_flags_to_names[i].flag_value;
13811 tf->valid = TRUE;
13812 break;
13813 }
13814 }
8b127cbc 13815 if (!tf->valid)
b9c361e0 13816 {
68ffbac6 13817 info->callbacks->einfo
9793eb77 13818 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13819 return FALSE;
ae17ab41
CM
13820 }
13821 }
8b127cbc
AM
13822 flaginfo->flags_initialized = TRUE;
13823 flaginfo->only_with_flags |= with_hex;
13824 flaginfo->not_with_flags |= without_hex;
ae17ab41 13825 }
ae17ab41 13826
8b127cbc 13827 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13828 return FALSE;
13829
8b127cbc 13830 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13831 return FALSE;
13832
13833 return TRUE;
ae17ab41
CM
13834}
13835
c152c796
AM
13836struct alloc_got_off_arg {
13837 bfd_vma gotoff;
10455f89 13838 struct bfd_link_info *info;
c152c796
AM
13839};
13840
13841/* We need a special top-level link routine to convert got reference counts
13842 to real got offsets. */
13843
13844static bfd_boolean
13845elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13846{
a50b1753 13847 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13848 bfd *obfd = gofarg->info->output_bfd;
13849 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13850
c152c796
AM
13851 if (h->got.refcount > 0)
13852 {
13853 h->got.offset = gofarg->gotoff;
10455f89 13854 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13855 }
13856 else
13857 h->got.offset = (bfd_vma) -1;
13858
13859 return TRUE;
13860}
13861
13862/* And an accompanying bit to work out final got entry offsets once
13863 we're done. Should be called from final_link. */
13864
13865bfd_boolean
13866bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13867 struct bfd_link_info *info)
13868{
13869 bfd *i;
13870 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13871 bfd_vma gotoff;
c152c796
AM
13872 struct alloc_got_off_arg gofarg;
13873
10455f89
HPN
13874 BFD_ASSERT (abfd == info->output_bfd);
13875
c152c796
AM
13876 if (! is_elf_hash_table (info->hash))
13877 return FALSE;
13878
13879 /* The GOT offset is relative to the .got section, but the GOT header is
13880 put into the .got.plt section, if the backend uses it. */
13881 if (bed->want_got_plt)
13882 gotoff = 0;
13883 else
13884 gotoff = bed->got_header_size;
13885
13886 /* Do the local .got entries first. */
c72f2fb2 13887 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13888 {
13889 bfd_signed_vma *local_got;
ef53be89 13890 size_t j, locsymcount;
c152c796
AM
13891 Elf_Internal_Shdr *symtab_hdr;
13892
13893 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13894 continue;
13895
13896 local_got = elf_local_got_refcounts (i);
13897 if (!local_got)
13898 continue;
13899
13900 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13901 if (elf_bad_symtab (i))
13902 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13903 else
13904 locsymcount = symtab_hdr->sh_info;
13905
13906 for (j = 0; j < locsymcount; ++j)
13907 {
13908 if (local_got[j] > 0)
13909 {
13910 local_got[j] = gotoff;
10455f89 13911 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13912 }
13913 else
13914 local_got[j] = (bfd_vma) -1;
13915 }
13916 }
13917
13918 /* Then the global .got entries. .plt refcounts are handled by
13919 adjust_dynamic_symbol */
13920 gofarg.gotoff = gotoff;
10455f89 13921 gofarg.info = info;
c152c796
AM
13922 elf_link_hash_traverse (elf_hash_table (info),
13923 elf_gc_allocate_got_offsets,
13924 &gofarg);
13925 return TRUE;
13926}
13927
13928/* Many folk need no more in the way of final link than this, once
13929 got entry reference counting is enabled. */
13930
13931bfd_boolean
13932bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13933{
13934 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13935 return FALSE;
13936
13937 /* Invoke the regular ELF backend linker to do all the work. */
13938 return bfd_elf_final_link (abfd, info);
13939}
13940
13941bfd_boolean
13942bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13943{
a50b1753 13944 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13945
13946 if (rcookie->bad_symtab)
13947 rcookie->rel = rcookie->rels;
13948
13949 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13950 {
13951 unsigned long r_symndx;
13952
13953 if (! rcookie->bad_symtab)
13954 if (rcookie->rel->r_offset > offset)
13955 return FALSE;
13956 if (rcookie->rel->r_offset != offset)
13957 continue;
13958
13959 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13960 if (r_symndx == STN_UNDEF)
c152c796
AM
13961 return TRUE;
13962
13963 if (r_symndx >= rcookie->locsymcount
13964 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13965 {
13966 struct elf_link_hash_entry *h;
13967
13968 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13969
13970 while (h->root.type == bfd_link_hash_indirect
13971 || h->root.type == bfd_link_hash_warning)
13972 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13973
13974 if ((h->root.type == bfd_link_hash_defined
13975 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13976 && (h->root.u.def.section->owner != rcookie->abfd
13977 || h->root.u.def.section->kept_section != NULL
13978 || discarded_section (h->root.u.def.section)))
c152c796 13979 return TRUE;
c152c796
AM
13980 }
13981 else
13982 {
13983 /* It's not a relocation against a global symbol,
13984 but it could be a relocation against a local
13985 symbol for a discarded section. */
13986 asection *isec;
13987 Elf_Internal_Sym *isym;
13988
13989 /* Need to: get the symbol; get the section. */
13990 isym = &rcookie->locsyms[r_symndx];
cb33740c 13991 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13992 if (isec != NULL
13993 && (isec->kept_section != NULL
13994 || discarded_section (isec)))
cb33740c 13995 return TRUE;
c152c796
AM
13996 }
13997 return FALSE;
13998 }
13999 return FALSE;
14000}
14001
14002/* Discard unneeded references to discarded sections.
75938853
AM
14003 Returns -1 on error, 1 if any section's size was changed, 0 if
14004 nothing changed. This function assumes that the relocations are in
14005 sorted order, which is true for all known assemblers. */
c152c796 14006
75938853 14007int
c152c796
AM
14008bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14009{
14010 struct elf_reloc_cookie cookie;
18cd5bce 14011 asection *o;
c152c796 14012 bfd *abfd;
75938853 14013 int changed = 0;
c152c796
AM
14014
14015 if (info->traditional_format
14016 || !is_elf_hash_table (info->hash))
75938853 14017 return 0;
c152c796 14018
18cd5bce
AM
14019 o = bfd_get_section_by_name (output_bfd, ".stab");
14020 if (o != NULL)
c152c796 14021 {
18cd5bce 14022 asection *i;
c152c796 14023
18cd5bce 14024 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14025 {
18cd5bce
AM
14026 if (i->size == 0
14027 || i->reloc_count == 0
14028 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14029 continue;
c152c796 14030
18cd5bce
AM
14031 abfd = i->owner;
14032 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14033 continue;
c152c796 14034
18cd5bce 14035 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14036 return -1;
c152c796 14037
18cd5bce
AM
14038 if (_bfd_discard_section_stabs (abfd, i,
14039 elf_section_data (i)->sec_info,
5241d853
RS
14040 bfd_elf_reloc_symbol_deleted_p,
14041 &cookie))
75938853 14042 changed = 1;
18cd5bce
AM
14043
14044 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14045 }
18cd5bce
AM
14046 }
14047
2f0c68f2
CM
14048 o = NULL;
14049 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14050 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14051 if (o != NULL)
14052 {
14053 asection *i;
d7153c4a 14054 int eh_changed = 0;
79a94a2a 14055 unsigned int eh_alignment;
c152c796 14056
18cd5bce 14057 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14058 {
18cd5bce
AM
14059 if (i->size == 0)
14060 continue;
14061
14062 abfd = i->owner;
14063 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14064 continue;
14065
14066 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14067 return -1;
18cd5bce
AM
14068
14069 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14070 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14071 bfd_elf_reloc_symbol_deleted_p,
14072 &cookie))
d7153c4a
AM
14073 {
14074 eh_changed = 1;
14075 if (i->size != i->rawsize)
14076 changed = 1;
14077 }
18cd5bce
AM
14078
14079 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14080 }
9866ffe2 14081
79a94a2a 14082 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14083 /* Skip over zero terminator, and prevent empty sections from
14084 adding alignment padding at the end. */
14085 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14086 if (i->size == 0)
14087 i->flags |= SEC_EXCLUDE;
14088 else if (i->size > 4)
14089 break;
14090 /* The last non-empty eh_frame section doesn't need padding. */
14091 if (i != NULL)
14092 i = i->map_tail.s;
14093 /* Any prior sections must pad the last FDE out to the output
14094 section alignment. Otherwise we might have zero padding
14095 between sections, which would be seen as a terminator. */
14096 for (; i != NULL; i = i->map_tail.s)
14097 if (i->size == 4)
14098 /* All but the last zero terminator should have been removed. */
14099 BFD_FAIL ();
14100 else
14101 {
14102 bfd_size_type size
14103 = (i->size + eh_alignment - 1) & -eh_alignment;
14104 if (i->size != size)
af471f82 14105 {
9866ffe2
AM
14106 i->size = size;
14107 changed = 1;
14108 eh_changed = 1;
af471f82 14109 }
9866ffe2 14110 }
d7153c4a
AM
14111 if (eh_changed)
14112 elf_link_hash_traverse (elf_hash_table (info),
14113 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14114 }
c152c796 14115
18cd5bce
AM
14116 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14117 {
14118 const struct elf_backend_data *bed;
57963c05 14119 asection *s;
c152c796 14120
18cd5bce
AM
14121 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14122 continue;
57963c05
AM
14123 s = abfd->sections;
14124 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14125 continue;
18cd5bce
AM
14126
14127 bed = get_elf_backend_data (abfd);
14128
14129 if (bed->elf_backend_discard_info != NULL)
14130 {
14131 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14132 return -1;
18cd5bce
AM
14133
14134 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14135 changed = 1;
18cd5bce
AM
14136
14137 fini_reloc_cookie (&cookie, abfd);
14138 }
c152c796
AM
14139 }
14140
2f0c68f2
CM
14141 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14142 _bfd_elf_end_eh_frame_parsing (info);
14143
14144 if (info->eh_frame_hdr_type
0e1862bb 14145 && !bfd_link_relocatable (info)
c152c796 14146 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14147 changed = 1;
c152c796 14148
75938853 14149 return changed;
c152c796 14150}
082b7297 14151
43e1669b 14152bfd_boolean
0c511000 14153_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14154 asection *sec,
c0f00686 14155 struct bfd_link_info *info)
082b7297
L
14156{
14157 flagword flags;
c77ec726 14158 const char *name, *key;
082b7297
L
14159 struct bfd_section_already_linked *l;
14160 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14161
c77ec726
AM
14162 if (sec->output_section == bfd_abs_section_ptr)
14163 return FALSE;
0c511000 14164
c77ec726 14165 flags = sec->flags;
0c511000 14166
c77ec726
AM
14167 /* Return if it isn't a linkonce section. A comdat group section
14168 also has SEC_LINK_ONCE set. */
14169 if ((flags & SEC_LINK_ONCE) == 0)
14170 return FALSE;
0c511000 14171
c77ec726
AM
14172 /* Don't put group member sections on our list of already linked
14173 sections. They are handled as a group via their group section. */
14174 if (elf_sec_group (sec) != NULL)
14175 return FALSE;
0c511000 14176
c77ec726
AM
14177 /* For a SHT_GROUP section, use the group signature as the key. */
14178 name = sec->name;
14179 if ((flags & SEC_GROUP) != 0
14180 && elf_next_in_group (sec) != NULL
14181 && elf_group_name (elf_next_in_group (sec)) != NULL)
14182 key = elf_group_name (elf_next_in_group (sec));
14183 else
14184 {
14185 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14186 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14187 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14188 key++;
0c511000 14189 else
c77ec726
AM
14190 /* Must be a user linkonce section that doesn't follow gcc's
14191 naming convention. In this case we won't be matching
14192 single member groups. */
14193 key = name;
0c511000 14194 }
6d2cd210 14195
c77ec726 14196 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14197
14198 for (l = already_linked_list->entry; l != NULL; l = l->next)
14199 {
c2370991 14200 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14201 sections with a signature of <key> (<key> is some string),
14202 and linkonce sections named .gnu.linkonce.<type>.<key>.
14203 Match like sections. LTO plugin sections are an exception.
14204 They are always named .gnu.linkonce.t.<key> and match either
14205 type of section. */
14206 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14207 && ((flags & SEC_GROUP) != 0
14208 || strcmp (name, l->sec->name) == 0))
14209 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14210 {
14211 /* The section has already been linked. See if we should
6d2cd210 14212 issue a warning. */
c77ec726
AM
14213 if (!_bfd_handle_already_linked (sec, l, info))
14214 return FALSE;
082b7297 14215
c77ec726 14216 if (flags & SEC_GROUP)
3d7f7666 14217 {
c77ec726
AM
14218 asection *first = elf_next_in_group (sec);
14219 asection *s = first;
3d7f7666 14220
c77ec726 14221 while (s != NULL)
3d7f7666 14222 {
c77ec726
AM
14223 s->output_section = bfd_abs_section_ptr;
14224 /* Record which group discards it. */
14225 s->kept_section = l->sec;
14226 s = elf_next_in_group (s);
14227 /* These lists are circular. */
14228 if (s == first)
14229 break;
3d7f7666
L
14230 }
14231 }
082b7297 14232
43e1669b 14233 return TRUE;
082b7297
L
14234 }
14235 }
14236
c77ec726
AM
14237 /* A single member comdat group section may be discarded by a
14238 linkonce section and vice versa. */
14239 if ((flags & SEC_GROUP) != 0)
3d7f7666 14240 {
c77ec726 14241 asection *first = elf_next_in_group (sec);
c2370991 14242
c77ec726
AM
14243 if (first != NULL && elf_next_in_group (first) == first)
14244 /* Check this single member group against linkonce sections. */
14245 for (l = already_linked_list->entry; l != NULL; l = l->next)
14246 if ((l->sec->flags & SEC_GROUP) == 0
14247 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14248 {
14249 first->output_section = bfd_abs_section_ptr;
14250 first->kept_section = l->sec;
14251 sec->output_section = bfd_abs_section_ptr;
14252 break;
14253 }
14254 }
14255 else
14256 /* Check this linkonce section against single member groups. */
14257 for (l = already_linked_list->entry; l != NULL; l = l->next)
14258 if (l->sec->flags & SEC_GROUP)
6d2cd210 14259 {
c77ec726 14260 asection *first = elf_next_in_group (l->sec);
6d2cd210 14261
c77ec726
AM
14262 if (first != NULL
14263 && elf_next_in_group (first) == first
14264 && bfd_elf_match_symbols_in_sections (first, sec, info))
14265 {
14266 sec->output_section = bfd_abs_section_ptr;
14267 sec->kept_section = first;
14268 break;
14269 }
6d2cd210 14270 }
0c511000 14271
c77ec726
AM
14272 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14273 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14274 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14275 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14276 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14277 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14278 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14279 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14280 The reverse order cannot happen as there is never a bfd with only the
14281 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14282 matter as here were are looking only for cross-bfd sections. */
14283
14284 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14285 for (l = already_linked_list->entry; l != NULL; l = l->next)
14286 if ((l->sec->flags & SEC_GROUP) == 0
14287 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14288 {
14289 if (abfd != l->sec->owner)
14290 sec->output_section = bfd_abs_section_ptr;
14291 break;
14292 }
80c29487 14293
082b7297 14294 /* This is the first section with this name. Record it. */
c77ec726 14295 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14296 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14297 return sec->output_section == bfd_abs_section_ptr;
082b7297 14298}
81e1b023 14299
a4d8e49b
L
14300bfd_boolean
14301_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14302{
14303 return sym->st_shndx == SHN_COMMON;
14304}
14305
14306unsigned int
14307_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14308{
14309 return SHN_COMMON;
14310}
14311
14312asection *
14313_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14314{
14315 return bfd_com_section_ptr;
14316}
10455f89
HPN
14317
14318bfd_vma
14319_bfd_elf_default_got_elt_size (bfd *abfd,
14320 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14321 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14322 bfd *ibfd ATTRIBUTE_UNUSED,
14323 unsigned long symndx ATTRIBUTE_UNUSED)
14324{
14325 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14326 return bed->s->arch_size / 8;
14327}
83bac4b0
NC
14328
14329/* Routines to support the creation of dynamic relocs. */
14330
83bac4b0
NC
14331/* Returns the name of the dynamic reloc section associated with SEC. */
14332
14333static const char *
14334get_dynamic_reloc_section_name (bfd * abfd,
14335 asection * sec,
14336 bfd_boolean is_rela)
14337{
ddcf1fcf
BS
14338 char *name;
14339 const char *old_name = bfd_get_section_name (NULL, sec);
14340 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14341
ddcf1fcf 14342 if (old_name == NULL)
83bac4b0
NC
14343 return NULL;
14344
ddcf1fcf 14345 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14346 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14347
14348 return name;
14349}
14350
14351/* Returns the dynamic reloc section associated with SEC.
14352 If necessary compute the name of the dynamic reloc section based
14353 on SEC's name (looked up in ABFD's string table) and the setting
14354 of IS_RELA. */
14355
14356asection *
14357_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14358 asection * sec,
14359 bfd_boolean is_rela)
14360{
14361 asection * reloc_sec = elf_section_data (sec)->sreloc;
14362
14363 if (reloc_sec == NULL)
14364 {
14365 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14366
14367 if (name != NULL)
14368 {
3d4d4302 14369 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14370
14371 if (reloc_sec != NULL)
14372 elf_section_data (sec)->sreloc = reloc_sec;
14373 }
14374 }
14375
14376 return reloc_sec;
14377}
14378
14379/* Returns the dynamic reloc section associated with SEC. If the
14380 section does not exist it is created and attached to the DYNOBJ
14381 bfd and stored in the SRELOC field of SEC's elf_section_data
14382 structure.
f8076f98 14383
83bac4b0
NC
14384 ALIGNMENT is the alignment for the newly created section and
14385 IS_RELA defines whether the name should be .rela.<SEC's name>
14386 or .rel.<SEC's name>. The section name is looked up in the
14387 string table associated with ABFD. */
14388
14389asection *
ca4be51c
AM
14390_bfd_elf_make_dynamic_reloc_section (asection *sec,
14391 bfd *dynobj,
14392 unsigned int alignment,
14393 bfd *abfd,
14394 bfd_boolean is_rela)
83bac4b0
NC
14395{
14396 asection * reloc_sec = elf_section_data (sec)->sreloc;
14397
14398 if (reloc_sec == NULL)
14399 {
14400 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14401
14402 if (name == NULL)
14403 return NULL;
14404
3d4d4302 14405 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14406
14407 if (reloc_sec == NULL)
14408 {
3d4d4302
AM
14409 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14410 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14411 if ((sec->flags & SEC_ALLOC) != 0)
14412 flags |= SEC_ALLOC | SEC_LOAD;
14413
3d4d4302 14414 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14415 if (reloc_sec != NULL)
14416 {
8877b5e5
AM
14417 /* _bfd_elf_get_sec_type_attr chooses a section type by
14418 name. Override as it may be wrong, eg. for a user
14419 section named "auto" we'll get ".relauto" which is
14420 seen to be a .rela section. */
14421 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14422 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14423 reloc_sec = NULL;
14424 }
14425 }
14426
14427 elf_section_data (sec)->sreloc = reloc_sec;
14428 }
14429
14430 return reloc_sec;
14431}
1338dd10 14432
bffebb6b
AM
14433/* Copy the ELF symbol type and other attributes for a linker script
14434 assignment from HSRC to HDEST. Generally this should be treated as
14435 if we found a strong non-dynamic definition for HDEST (except that
14436 ld ignores multiple definition errors). */
1338dd10 14437void
bffebb6b
AM
14438_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14439 struct bfd_link_hash_entry *hdest,
14440 struct bfd_link_hash_entry *hsrc)
1338dd10 14441{
bffebb6b
AM
14442 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14443 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14444 Elf_Internal_Sym isym;
1338dd10
PB
14445
14446 ehdest->type = ehsrc->type;
35fc36a8 14447 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14448
14449 isym.st_other = ehsrc->other;
b8417128 14450 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14451}
351f65ca
L
14452
14453/* Append a RELA relocation REL to section S in BFD. */
14454
14455void
14456elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14457{
14458 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14459 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14460 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14461 bed->s->swap_reloca_out (abfd, rel, loc);
14462}
14463
14464/* Append a REL relocation REL to section S in BFD. */
14465
14466void
14467elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14468{
14469 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14470 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14471 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14472 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14473}
7dba9362
AM
14474
14475/* Define __start, __stop, .startof. or .sizeof. symbol. */
14476
14477struct bfd_link_hash_entry *
14478bfd_elf_define_start_stop (struct bfd_link_info *info,
14479 const char *symbol, asection *sec)
14480{
487b6440 14481 struct elf_link_hash_entry *h;
7dba9362 14482
487b6440
AM
14483 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14484 FALSE, FALSE, TRUE);
14485 if (h != NULL
14486 && (h->root.type == bfd_link_hash_undefined
14487 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14488 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14489 {
bf3077a6 14490 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14491 h->root.type = bfd_link_hash_defined;
14492 h->root.u.def.section = sec;
14493 h->root.u.def.value = 0;
14494 h->def_regular = 1;
14495 h->def_dynamic = 0;
14496 h->start_stop = 1;
14497 h->u2.start_stop_section = sec;
14498 if (symbol[0] == '.')
14499 {
14500 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14501 const struct elf_backend_data *bed;
14502 bed = get_elf_backend_data (info->output_bfd);
14503 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14504 }
36b8fda5
AM
14505 else
14506 {
14507 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14508 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14509 if (was_dynamic)
36b8fda5
AM
14510 bfd_elf_link_record_dynamic_symbol (info, h);
14511 }
487b6440 14512 return &h->root;
7dba9362 14513 }
487b6440 14514 return NULL;
7dba9362 14515}
This page took 2.978147 seconds and 4 git commands to generate.