S/390: Fix (some) PIE+undef weak failures
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 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)))
55255dae
L
589 h->dynamic = 1;
590}
591
45d6a902
AM
592/* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
594
595bfd_boolean
fe21a8fc
L
596bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
268b6b39 598 const char *name,
fe21a8fc
L
599 bfd_boolean provide,
600 bfd_boolean hidden)
45d6a902 601{
00cbee0a 602 struct elf_link_hash_entry *h, *hv;
4ea42fb7 603 struct elf_link_hash_table *htab;
00cbee0a 604 const struct elf_backend_data *bed;
45d6a902 605
0eddce27 606 if (!is_elf_hash_table (info->hash))
45d6a902
AM
607 return TRUE;
608
4ea42fb7
AM
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 611 if (h == NULL)
4ea42fb7 612 return provide;
45d6a902 613
8e2a4f11
AM
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
616
0f550b3d
L
617 if (h->versioned == unknown)
618 {
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
622 {
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
627 }
628 }
629
73ec947d
AM
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
633 {
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
636 }
637
00cbee0a 638 switch (h->root.type)
77cfaee6 639 {
00cbee0a
L
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
4ea42fb7 649 h->root.type = bfd_link_hash_new;
77cfaee6
AM
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
652 break;
653 case bfd_link_hash_new:
00cbee0a
L
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 657 the versioned symbol point to this one. */
00cbee0a
L
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
8e2a4f11
AM
670 default:
671 BFD_FAIL ();
c2596ca5 672 return FALSE;
55255dae 673 }
45d6a902
AM
674
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
f5385ebf
AM
680 && h->def_dynamic
681 && !h->def_regular)
45d6a902
AM
682 h->root.type = bfd_link_hash_undefined;
683
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
45d6a902 688 if (!provide
f5385ebf
AM
689 && h->def_dynamic
690 && !h->def_regular)
b531344c
MR
691 h->verinfo.verdef = NULL;
692
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
45d6a902 695
f5385ebf 696 h->def_regular = 1;
45d6a902 697
eb8476a6 698 if (hidden)
fe21a8fc 699 {
91d6fa6a 700 bed = get_elf_backend_data (output_bfd);
b8297068
AM
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
704 }
705
6fa3860b
PB
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
0e1862bb 708 if (!bfd_link_relocatable (info)
6fa3860b
PB
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
713
f5385ebf
AM
714 if ((h->def_dynamic
715 || h->ref_dynamic
6b3b0ab8
L
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
718 && h->dynindx == -1)
719 {
c152c796 720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
721 return FALSE;
722
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
60d67dc8 726 if (h->is_weakalias)
45d6a902 727 {
60d67dc8
AM
728 struct elf_link_hash_entry *def = weakdef (h);
729
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
732 return FALSE;
733 }
734 }
735
736 return TRUE;
737}
42751cf3 738
8c58d23b
AM
739/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
742
743int
c152c796
AM
744bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
745 bfd *input_bfd,
746 long input_indx)
8c58d23b
AM
747{
748 bfd_size_type amt;
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
ef53be89 752 size_t dynstr_index;
8c58d23b
AM
753 char *name;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
756
0eddce27 757 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
758 return 0;
759
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
763 return 1;
764
765 amt = sizeof (*entry);
a50b1753 766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
767 if (entry == NULL)
768 return 0;
769
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 772 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
773 {
774 bfd_release (input_bfd, entry);
775 return 0;
776 }
777
778 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 779 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
780 {
781 asection *s;
782
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
785 {
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
789 return 2;
790 }
791 }
792
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
796
797 dynstr = elf_hash_table (info)->dynstr;
798 if (dynstr == NULL)
799 {
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
802 if (dynstr == NULL)
803 return 0;
804 }
805
b34976b6 806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 807 if (dynstr_index == (size_t) -1)
8c58d23b
AM
808 return 0;
809 entry->isym.st_name = dynstr_index;
810
811 eht = elf_hash_table (info);
812
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
817 eht->dynsymcount++;
818
819 /* Whatever binding the symbol had before, it's now local. */
820 entry->isym.st_info
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
822
823 /* The dynindx will be set at the end of size_dynamic_sections. */
824
825 return 1;
826}
827
30b30c21 828/* Return the dynindex of a local dynamic symbol. */
42751cf3 829
30b30c21 830long
268b6b39
AM
831_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
832 bfd *input_bfd,
833 long input_indx)
30b30c21
RH
834{
835 struct elf_link_local_dynamic_entry *e;
836
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
839 return e->dynindx;
840 return -1;
841}
842
843/* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
846
b34976b6 847static bfd_boolean
268b6b39
AM
848elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
849 void *data)
42751cf3 850{
a50b1753 851 size_t *count = (size_t *) data;
30b30c21 852
6fa3860b
PB
853 if (h->forced_local)
854 return TRUE;
855
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
858
859 return TRUE;
860}
861
862
863/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
865
866static bfd_boolean
867elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869{
a50b1753 870 size_t *count = (size_t *) data;
6fa3860b 871
6fa3860b
PB
872 if (!h->forced_local)
873 return TRUE;
874
42751cf3 875 if (h->dynindx != -1)
30b30c21
RH
876 h->dynindx = ++(*count);
877
b34976b6 878 return TRUE;
42751cf3 879}
30b30c21 880
aee6f5b4
AO
881/* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
883bfd_boolean
884_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
886 asection *p)
887{
74541ad4 888 struct elf_link_hash_table *htab;
ca55926c 889 asection *ip;
74541ad4 890
aee6f5b4
AO
891 switch (elf_section_data (p)->this_hdr.sh_type)
892 {
893 case SHT_PROGBITS:
894 case SHT_NOBITS:
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
897 case SHT_NULL:
74541ad4
AM
898 htab = elf_hash_table (info);
899 if (p == htab->tls_sec)
900 return FALSE;
901
902 if (htab->text_index_section != NULL)
903 return p != htab->text_index_section && p != htab->data_index_section;
904
ca55926c 905 return (htab->dynobj != NULL
3d4d4302 906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 907 && ip->output_section == p);
aee6f5b4
AO
908
909 /* There shouldn't be section relative relocations
910 against any other section. */
911 default:
912 return TRUE;
913 }
914}
915
062e2358 916/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
917 symbol for each output section, which come first. Next come symbols
918 which have been forced to local binding. Then all of the back-end
919 allocated local dynamic syms, followed by the rest of the global
920 symbols. */
30b30c21 921
554220db
AM
922static unsigned long
923_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
924 struct bfd_link_info *info,
925 unsigned long *section_sym_count)
30b30c21
RH
926{
927 unsigned long dynsymcount = 0;
928
0e1862bb
L
929 if (bfd_link_pic (info)
930 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 931 {
aee6f5b4 932 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
933 asection *p;
934 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 935 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
936 && (p->flags & SEC_ALLOC) != 0
937 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
938 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
939 else
940 elf_section_data (p)->dynindx = 0;
30b30c21 941 }
554220db 942 *section_sym_count = dynsymcount;
30b30c21 943
6fa3860b
PB
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_local_hash_table_dynsyms,
946 &dynsymcount);
947
30b30c21
RH
948 if (elf_hash_table (info)->dynlocal)
949 {
950 struct elf_link_local_dynamic_entry *p;
951 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
952 p->dynindx = ++dynsymcount;
953 }
90ac2420 954 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
955
956 elf_link_hash_traverse (elf_hash_table (info),
957 elf_link_renumber_hash_table_dynsyms,
958 &dynsymcount);
959
d5486c43
L
960 /* There is an unused NULL entry at the head of the table which we
961 must account for in our count even if the table is empty since it
962 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
963 .dynamic section. */
964 dynsymcount++;
30b30c21 965
ccabcbe5
AM
966 elf_hash_table (info)->dynsymcount = dynsymcount;
967 return dynsymcount;
30b30c21 968}
252b5132 969
54ac0771
L
970/* Merge st_other field. */
971
972static void
973elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 974 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 975 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
976{
977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
978
979 /* If st_other has a processor-specific meaning, specific
cd3416da 980 code might be needed here. */
54ac0771
L
981 if (bed->elf_backend_merge_symbol_attribute)
982 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
983 dynamic);
984
cd3416da 985 if (!dynamic)
54ac0771 986 {
cd3416da
AM
987 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
988 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 989
cd3416da
AM
990 /* Keep the most constraining visibility. Leave the remainder
991 of the st_other field to elf_backend_merge_symbol_attribute. */
992 if (symvis - 1 < hvis - 1)
993 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 994 }
b8417128
AM
995 else if (definition
996 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
997 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 998 h->protected_def = 1;
54ac0771
L
999}
1000
4f3fedcf
AM
1001/* This function is called when we want to merge a new symbol with an
1002 existing symbol. It handles the various cases which arise when we
1003 find a definition in a dynamic object, or when there is already a
1004 definition in a dynamic object. The new symbol is described by
1005 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1006 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1007 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1008 of an old common symbol. We set OVERRIDE if the old symbol is
1009 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1010 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1011 to change. By OK to change, we mean that we shouldn't warn if the
1012 type or size does change. */
45d6a902 1013
8a56bd02 1014static bfd_boolean
268b6b39
AM
1015_bfd_elf_merge_symbol (bfd *abfd,
1016 struct bfd_link_info *info,
1017 const char *name,
1018 Elf_Internal_Sym *sym,
1019 asection **psec,
1020 bfd_vma *pvalue,
4f3fedcf
AM
1021 struct elf_link_hash_entry **sym_hash,
1022 bfd **poldbfd,
37a9e49a 1023 bfd_boolean *pold_weak,
af44c138 1024 unsigned int *pold_alignment,
268b6b39
AM
1025 bfd_boolean *skip,
1026 bfd_boolean *override,
1027 bfd_boolean *type_change_ok,
6e33951e
L
1028 bfd_boolean *size_change_ok,
1029 bfd_boolean *matched)
252b5132 1030{
7479dfd4 1031 asection *sec, *oldsec;
45d6a902 1032 struct elf_link_hash_entry *h;
90c984fc 1033 struct elf_link_hash_entry *hi;
45d6a902
AM
1034 struct elf_link_hash_entry *flip;
1035 int bind;
1036 bfd *oldbfd;
1037 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1038 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1039 const struct elf_backend_data *bed;
6e33951e 1040 char *new_version;
93f4de39 1041 bfd_boolean default_sym = *matched;
45d6a902
AM
1042
1043 *skip = FALSE;
1044 *override = FALSE;
1045
1046 sec = *psec;
1047 bind = ELF_ST_BIND (sym->st_info);
1048
1049 if (! bfd_is_und_section (sec))
1050 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1051 else
1052 h = ((struct elf_link_hash_entry *)
1053 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1054 if (h == NULL)
1055 return FALSE;
1056 *sym_hash = h;
252b5132 1057
88ba32a0
L
1058 bed = get_elf_backend_data (abfd);
1059
6e33951e 1060 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1061 if (h->versioned != unversioned)
6e33951e 1062 {
422f1182
L
1063 /* Symbol version is unknown or versioned. */
1064 new_version = strrchr (name, ELF_VER_CHR);
1065 if (new_version)
1066 {
1067 if (h->versioned == unknown)
1068 {
1069 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1070 h->versioned = versioned_hidden;
1071 else
1072 h->versioned = versioned;
1073 }
1074 new_version += 1;
1075 if (new_version[0] == '\0')
1076 new_version = NULL;
1077 }
1078 else
1079 h->versioned = unversioned;
6e33951e 1080 }
422f1182
L
1081 else
1082 new_version = NULL;
6e33951e 1083
90c984fc
L
1084 /* For merging, we only care about real symbols. But we need to make
1085 sure that indirect symbol dynamic flags are updated. */
1086 hi = h;
45d6a902
AM
1087 while (h->root.type == bfd_link_hash_indirect
1088 || h->root.type == bfd_link_hash_warning)
1089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1090
6e33951e
L
1091 if (!*matched)
1092 {
1093 if (hi == h || h->root.type == bfd_link_hash_new)
1094 *matched = TRUE;
1095 else
1096 {
ae7683d2 1097 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1098 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1099 true if the new symbol is only visible to the symbol with
6e33951e 1100 the same symbol version. */
422f1182
L
1101 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1102 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1103 if (!old_hidden && !new_hidden)
1104 /* The new symbol matches the existing symbol if both
1105 aren't hidden. */
1106 *matched = TRUE;
1107 else
1108 {
1109 /* OLD_VERSION is the symbol version of the existing
1110 symbol. */
422f1182
L
1111 char *old_version;
1112
1113 if (h->versioned >= versioned)
1114 old_version = strrchr (h->root.root.string,
1115 ELF_VER_CHR) + 1;
1116 else
1117 old_version = NULL;
6e33951e
L
1118
1119 /* The new symbol matches the existing symbol if they
1120 have the same symbol version. */
1121 *matched = (old_version == new_version
1122 || (old_version != NULL
1123 && new_version != NULL
1124 && strcmp (old_version, new_version) == 0));
1125 }
1126 }
1127 }
1128
934bce08
AM
1129 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1130 existing symbol. */
1131
1132 oldbfd = NULL;
1133 oldsec = NULL;
1134 switch (h->root.type)
1135 {
1136 default:
1137 break;
1138
1139 case bfd_link_hash_undefined:
1140 case bfd_link_hash_undefweak:
1141 oldbfd = h->root.u.undef.abfd;
1142 break;
1143
1144 case bfd_link_hash_defined:
1145 case bfd_link_hash_defweak:
1146 oldbfd = h->root.u.def.section->owner;
1147 oldsec = h->root.u.def.section;
1148 break;
1149
1150 case bfd_link_hash_common:
1151 oldbfd = h->root.u.c.p->section->owner;
1152 oldsec = h->root.u.c.p->section;
1153 if (pold_alignment)
1154 *pold_alignment = h->root.u.c.p->alignment_power;
1155 break;
1156 }
1157 if (poldbfd && *poldbfd == NULL)
1158 *poldbfd = oldbfd;
1159
1160 /* Differentiate strong and weak symbols. */
1161 newweak = bind == STB_WEAK;
1162 oldweak = (h->root.type == bfd_link_hash_defweak
1163 || h->root.type == bfd_link_hash_undefweak);
1164 if (pold_weak)
1165 *pold_weak = oldweak;
1166
40b36307 1167 /* We have to check it for every instance since the first few may be
ee659f1f 1168 references and not all compilers emit symbol type for undefined
40b36307
L
1169 symbols. */
1170 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1171
ee659f1f
AM
1172 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1173 respectively, is from a dynamic object. */
1174
1175 newdyn = (abfd->flags & DYNAMIC) != 0;
1176
1177 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1178 syms and defined syms in dynamic libraries respectively.
1179 ref_dynamic on the other hand can be set for a symbol defined in
1180 a dynamic library, and def_dynamic may not be set; When the
1181 definition in a dynamic lib is overridden by a definition in the
1182 executable use of the symbol in the dynamic lib becomes a
1183 reference to the executable symbol. */
1184 if (newdyn)
1185 {
1186 if (bfd_is_und_section (sec))
1187 {
1188 if (bind != STB_WEAK)
1189 {
1190 h->ref_dynamic_nonweak = 1;
1191 hi->ref_dynamic_nonweak = 1;
1192 }
1193 }
1194 else
1195 {
6e33951e
L
1196 /* Update the existing symbol only if they match. */
1197 if (*matched)
1198 h->dynamic_def = 1;
ee659f1f
AM
1199 hi->dynamic_def = 1;
1200 }
1201 }
1202
45d6a902
AM
1203 /* If we just created the symbol, mark it as being an ELF symbol.
1204 Other than that, there is nothing to do--there is no merge issue
1205 with a newly defined symbol--so we just return. */
1206
1207 if (h->root.type == bfd_link_hash_new)
252b5132 1208 {
f5385ebf 1209 h->non_elf = 0;
45d6a902
AM
1210 return TRUE;
1211 }
252b5132 1212
45d6a902
AM
1213 /* In cases involving weak versioned symbols, we may wind up trying
1214 to merge a symbol with itself. Catch that here, to avoid the
1215 confusion that results if we try to override a symbol with
1216 itself. The additional tests catch cases like
1217 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1218 dynamic object, which we do want to handle here. */
1219 if (abfd == oldbfd
895fa45f 1220 && (newweak || oldweak)
45d6a902 1221 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1222 || !h->def_regular))
45d6a902
AM
1223 return TRUE;
1224
707bba77 1225 olddyn = FALSE;
45d6a902
AM
1226 if (oldbfd != NULL)
1227 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1228 else if (oldsec != NULL)
45d6a902 1229 {
707bba77 1230 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1231 indices used by MIPS ELF. */
707bba77 1232 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1233 }
252b5132 1234
1a3b5c34
AM
1235 /* Handle a case where plugin_notice won't be called and thus won't
1236 set the non_ir_ref flags on the first pass over symbols. */
1237 if (oldbfd != NULL
1238 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1239 && newdyn != olddyn)
1240 {
1241 h->root.non_ir_ref_dynamic = TRUE;
1242 hi->root.non_ir_ref_dynamic = TRUE;
1243 }
1244
45d6a902
AM
1245 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1246 respectively, appear to be a definition rather than reference. */
1247
707bba77 1248 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1249
707bba77
AM
1250 olddef = (h->root.type != bfd_link_hash_undefined
1251 && h->root.type != bfd_link_hash_undefweak
202ac193 1252 && h->root.type != bfd_link_hash_common);
45d6a902 1253
0a36a439
L
1254 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1255 respectively, appear to be a function. */
1256
1257 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1258 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1259
1260 oldfunc = (h->type != STT_NOTYPE
1261 && bed->is_function_type (h->type));
1262
c5d37467 1263 if (!(newfunc && oldfunc)
5b677558
AM
1264 && ELF_ST_TYPE (sym->st_info) != h->type
1265 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1266 && h->type != STT_NOTYPE
c5d37467
AM
1267 && (newdef || bfd_is_com_section (sec))
1268 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1269 {
c5d37467
AM
1270 /* If creating a default indirect symbol ("foo" or "foo@") from
1271 a dynamic versioned definition ("foo@@") skip doing so if
1272 there is an existing regular definition with a different
1273 type. We don't want, for example, a "time" variable in the
1274 executable overriding a "time" function in a shared library. */
1275 if (newdyn
1276 && !olddyn)
1277 {
1278 *skip = TRUE;
1279 return TRUE;
1280 }
1281
1282 /* When adding a symbol from a regular object file after we have
1283 created indirect symbols, undo the indirection and any
1284 dynamic state. */
1285 if (hi != h
1286 && !newdyn
1287 && olddyn)
1288 {
1289 h = hi;
1290 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1291 h->forced_local = 0;
1292 h->ref_dynamic = 0;
1293 h->def_dynamic = 0;
1294 h->dynamic_def = 0;
1295 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1296 {
1297 h->root.type = bfd_link_hash_undefined;
1298 h->root.u.undef.abfd = abfd;
1299 }
1300 else
1301 {
1302 h->root.type = bfd_link_hash_new;
1303 h->root.u.undef.abfd = NULL;
1304 }
1305 return TRUE;
1306 }
580a2b6e
L
1307 }
1308
4c34aff8
AM
1309 /* Check TLS symbols. We don't check undefined symbols introduced
1310 by "ld -u" which have no type (and oldbfd NULL), and we don't
1311 check symbols from plugins because they also have no type. */
1312 if (oldbfd != NULL
1313 && (oldbfd->flags & BFD_PLUGIN) == 0
1314 && (abfd->flags & BFD_PLUGIN) == 0
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1317 {
1318 bfd *ntbfd, *tbfd;
1319 bfd_boolean ntdef, tdef;
1320 asection *ntsec, *tsec;
1321
1322 if (h->type == STT_TLS)
1323 {
3b36f7e6 1324 ntbfd = abfd;
7479dfd4
L
1325 ntsec = sec;
1326 ntdef = newdef;
1327 tbfd = oldbfd;
1328 tsec = oldsec;
1329 tdef = olddef;
1330 }
1331 else
1332 {
1333 ntbfd = oldbfd;
1334 ntsec = oldsec;
1335 ntdef = olddef;
1336 tbfd = abfd;
1337 tsec = sec;
1338 tdef = newdef;
1339 }
1340
1341 if (tdef && ntdef)
4eca0228 1342 _bfd_error_handler
695344c0 1343 /* xgettext:c-format */
191c0c42
AM
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1346 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1347 else if (!tdef && !ntdef)
4eca0228 1348 _bfd_error_handler
695344c0 1349 /* xgettext:c-format */
191c0c42
AM
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS reference in %B"),
c08bb8dd 1352 h->root.root.string, tbfd, ntbfd);
7479dfd4 1353 else if (tdef)
4eca0228 1354 _bfd_error_handler
695344c0 1355 /* xgettext:c-format */
191c0c42
AM
1356 (_("%s: TLS definition in %B section %A "
1357 "mismatches non-TLS reference in %B"),
c08bb8dd 1358 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1359 else
4eca0228 1360 _bfd_error_handler
695344c0 1361 /* xgettext:c-format */
191c0c42
AM
1362 (_("%s: TLS reference in %B "
1363 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1364 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1365
1366 bfd_set_error (bfd_error_bad_value);
1367 return FALSE;
1368 }
1369
45d6a902
AM
1370 /* If the old symbol has non-default visibility, we ignore the new
1371 definition from a dynamic object. */
1372 if (newdyn
9c7a29a3 1373 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1374 && !bfd_is_und_section (sec))
1375 {
1376 *skip = TRUE;
1377 /* Make sure this symbol is dynamic. */
f5385ebf 1378 h->ref_dynamic = 1;
90c984fc 1379 hi->ref_dynamic = 1;
45d6a902
AM
1380 /* A protected symbol has external availability. Make sure it is
1381 recorded as dynamic.
1382
1383 FIXME: Should we check type and size for protected symbol? */
1384 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1385 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1386 else
1387 return TRUE;
1388 }
1389 else if (!newdyn
9c7a29a3 1390 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1391 && h->def_dynamic)
45d6a902
AM
1392 {
1393 /* If the new symbol with non-default visibility comes from a
1394 relocatable file and the old definition comes from a dynamic
1395 object, we remove the old definition. */
6c9b78e6 1396 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1397 {
1398 /* Handle the case where the old dynamic definition is
1399 default versioned. We need to copy the symbol info from
1400 the symbol with default version to the normal one if it
1401 was referenced before. */
1402 if (h->ref_regular)
1403 {
6c9b78e6 1404 hi->root.type = h->root.type;
d2dee3b2 1405 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1406 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1407
6c9b78e6 1408 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1409 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1410 {
aed81c4e
MR
1411 /* If the new symbol is hidden or internal, completely undo
1412 any dynamic link state. */
1413 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1414 h->forced_local = 0;
1415 h->ref_dynamic = 0;
d2dee3b2
L
1416 }
1417 else
aed81c4e
MR
1418 h->ref_dynamic = 1;
1419
1420 h->def_dynamic = 0;
aed81c4e
MR
1421 /* FIXME: Should we check type and size for protected symbol? */
1422 h->size = 0;
1423 h->type = 0;
1424
6c9b78e6 1425 h = hi;
d2dee3b2
L
1426 }
1427 else
6c9b78e6 1428 h = hi;
d2dee3b2 1429 }
1de1a317 1430
f5eda473
AM
1431 /* If the old symbol was undefined before, then it will still be
1432 on the undefs list. If the new symbol is undefined or
1433 common, we can't make it bfd_link_hash_new here, because new
1434 undefined or common symbols will be added to the undefs list
1435 by _bfd_generic_link_add_one_symbol. Symbols may not be
1436 added twice to the undefs list. Also, if the new symbol is
1437 undefweak then we don't want to lose the strong undef. */
1438 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1439 {
1de1a317 1440 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1441 h->root.u.undef.abfd = abfd;
1442 }
1443 else
1444 {
1445 h->root.type = bfd_link_hash_new;
1446 h->root.u.undef.abfd = NULL;
1447 }
1448
f5eda473 1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1450 {
f5eda473
AM
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
45d6a902 1456 }
f5eda473
AM
1457 else
1458 h->ref_dynamic = 1;
1459 h->def_dynamic = 0;
45d6a902
AM
1460 /* FIXME: Should we check type and size for protected symbol? */
1461 h->size = 0;
1462 h->type = 0;
1463 return TRUE;
1464 }
14a793b2 1465
15b43f48
AM
1466 /* If a new weak symbol definition comes from a regular file and the
1467 old symbol comes from a dynamic library, we treat the new one as
1468 strong. Similarly, an old weak symbol definition from a regular
1469 file is treated as strong when the new symbol comes from a dynamic
1470 library. Further, an old weak symbol from a dynamic library is
1471 treated as strong if the new symbol is from a dynamic library.
1472 This reflects the way glibc's ld.so works.
1473
165f707a
AM
1474 Also allow a weak symbol to override a linker script symbol
1475 defined by an early pass over the script. This is done so the
1476 linker knows the symbol is defined in an object file, for the
1477 DEFINED script function.
1478
15b43f48
AM
1479 Do this before setting *type_change_ok or *size_change_ok so that
1480 we warn properly when dynamic library symbols are overridden. */
1481
165f707a 1482 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1483 newweak = FALSE;
15b43f48 1484 if (olddef && newdyn)
0f8a2703
AM
1485 oldweak = FALSE;
1486
d334575b 1487 /* Allow changes between different types of function symbol. */
0a36a439 1488 if (newfunc && oldfunc)
fcb93ecf
PB
1489 *type_change_ok = TRUE;
1490
79349b09
AM
1491 /* It's OK to change the type if either the existing symbol or the
1492 new symbol is weak. A type change is also OK if the old symbol
1493 is undefined and the new symbol is defined. */
252b5132 1494
79349b09
AM
1495 if (oldweak
1496 || newweak
1497 || (newdef
1498 && h->root.type == bfd_link_hash_undefined))
1499 *type_change_ok = TRUE;
1500
1501 /* It's OK to change the size if either the existing symbol or the
1502 new symbol is weak, or if the old symbol is undefined. */
1503
1504 if (*type_change_ok
1505 || h->root.type == bfd_link_hash_undefined)
1506 *size_change_ok = TRUE;
45d6a902 1507
45d6a902
AM
1508 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1509 symbol, respectively, appears to be a common symbol in a dynamic
1510 object. If a symbol appears in an uninitialized section, and is
1511 not weak, and is not a function, then it may be a common symbol
1512 which was resolved when the dynamic object was created. We want
1513 to treat such symbols specially, because they raise special
1514 considerations when setting the symbol size: if the symbol
1515 appears as a common symbol in a regular object, and the size in
1516 the regular object is larger, we must make sure that we use the
1517 larger size. This problematic case can always be avoided in C,
1518 but it must be handled correctly when using Fortran shared
1519 libraries.
1520
1521 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1522 likewise for OLDDYNCOMMON and OLDDEF.
1523
1524 Note that this test is just a heuristic, and that it is quite
1525 possible to have an uninitialized symbol in a shared object which
1526 is really a definition, rather than a common symbol. This could
1527 lead to some minor confusion when the symbol really is a common
1528 symbol in some regular object. However, I think it will be
1529 harmless. */
1530
1531 if (newdyn
1532 && newdef
79349b09 1533 && !newweak
45d6a902
AM
1534 && (sec->flags & SEC_ALLOC) != 0
1535 && (sec->flags & SEC_LOAD) == 0
1536 && sym->st_size > 0
0a36a439 1537 && !newfunc)
45d6a902
AM
1538 newdyncommon = TRUE;
1539 else
1540 newdyncommon = FALSE;
1541
1542 if (olddyn
1543 && olddef
1544 && h->root.type == bfd_link_hash_defined
f5385ebf 1545 && h->def_dynamic
45d6a902
AM
1546 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1547 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1548 && h->size > 0
0a36a439 1549 && !oldfunc)
45d6a902
AM
1550 olddyncommon = TRUE;
1551 else
1552 olddyncommon = FALSE;
1553
a4d8e49b
L
1554 /* We now know everything about the old and new symbols. We ask the
1555 backend to check if we can merge them. */
5d13b3b3
AM
1556 if (bed->merge_symbol != NULL)
1557 {
1558 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1559 return FALSE;
1560 sec = *psec;
1561 }
a4d8e49b 1562
a83ef4d1
L
1563 /* There are multiple definitions of a normal symbol. Skip the
1564 default symbol as well as definition from an IR object. */
93f4de39 1565 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1566 && !default_sym && h->def_regular
1567 && !(oldbfd != NULL
1568 && (oldbfd->flags & BFD_PLUGIN) != 0
1569 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1570 {
1571 /* Handle a multiple definition. */
1572 (*info->callbacks->multiple_definition) (info, &h->root,
1573 abfd, sec, *pvalue);
1574 *skip = TRUE;
1575 return TRUE;
1576 }
1577
45d6a902
AM
1578 /* If both the old and the new symbols look like common symbols in a
1579 dynamic object, set the size of the symbol to the larger of the
1580 two. */
1581
1582 if (olddyncommon
1583 && newdyncommon
1584 && sym->st_size != h->size)
1585 {
1586 /* Since we think we have two common symbols, issue a multiple
1587 common warning if desired. Note that we only warn if the
1588 size is different. If the size is the same, we simply let
1589 the old symbol override the new one as normally happens with
1590 symbols defined in dynamic objects. */
1591
1a72702b
AM
1592 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1593 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1594 if (sym->st_size > h->size)
1595 h->size = sym->st_size;
252b5132 1596
45d6a902 1597 *size_change_ok = TRUE;
252b5132
RH
1598 }
1599
45d6a902
AM
1600 /* If we are looking at a dynamic object, and we have found a
1601 definition, we need to see if the symbol was already defined by
1602 some other object. If so, we want to use the existing
1603 definition, and we do not want to report a multiple symbol
1604 definition error; we do this by clobbering *PSEC to be
1605 bfd_und_section_ptr.
1606
1607 We treat a common symbol as a definition if the symbol in the
1608 shared library is a function, since common symbols always
1609 represent variables; this can cause confusion in principle, but
1610 any such confusion would seem to indicate an erroneous program or
1611 shared library. We also permit a common symbol in a regular
8170f769 1612 object to override a weak symbol in a shared object. */
45d6a902
AM
1613
1614 if (newdyn
1615 && newdef
77cfaee6 1616 && (olddef
45d6a902 1617 || (h->root.type == bfd_link_hash_common
8170f769 1618 && (newweak || newfunc))))
45d6a902
AM
1619 {
1620 *override = TRUE;
1621 newdef = FALSE;
1622 newdyncommon = FALSE;
252b5132 1623
45d6a902
AM
1624 *psec = sec = bfd_und_section_ptr;
1625 *size_change_ok = TRUE;
252b5132 1626
45d6a902
AM
1627 /* If we get here when the old symbol is a common symbol, then
1628 we are explicitly letting it override a weak symbol or
1629 function in a dynamic object, and we don't want to warn about
1630 a type change. If the old symbol is a defined symbol, a type
1631 change warning may still be appropriate. */
252b5132 1632
45d6a902
AM
1633 if (h->root.type == bfd_link_hash_common)
1634 *type_change_ok = TRUE;
1635 }
1636
1637 /* Handle the special case of an old common symbol merging with a
1638 new symbol which looks like a common symbol in a shared object.
1639 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1640 common symbol, and let _bfd_generic_link_add_one_symbol do the
1641 right thing. */
45d6a902
AM
1642
1643 if (newdyncommon
1644 && h->root.type == bfd_link_hash_common)
1645 {
1646 *override = TRUE;
1647 newdef = FALSE;
1648 newdyncommon = FALSE;
1649 *pvalue = sym->st_size;
a4d8e49b 1650 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1651 *size_change_ok = TRUE;
1652 }
1653
c5e2cead 1654 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1655 if (newdef && olddef && newweak)
54ac0771 1656 {
35ed3f94 1657 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1658 if (!(oldbfd != NULL
1659 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1660 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1661 {
1662 newdef = FALSE;
1663 *skip = TRUE;
1664 }
54ac0771
L
1665
1666 /* Merge st_other. If the symbol already has a dynamic index,
1667 but visibility says it should not be visible, turn it into a
1668 local symbol. */
b8417128 1669 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1670 if (h->dynindx != -1)
1671 switch (ELF_ST_VISIBILITY (h->other))
1672 {
1673 case STV_INTERNAL:
1674 case STV_HIDDEN:
1675 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1676 break;
1677 }
1678 }
c5e2cead 1679
45d6a902
AM
1680 /* If the old symbol is from a dynamic object, and the new symbol is
1681 a definition which is not from a dynamic object, then the new
1682 symbol overrides the old symbol. Symbols from regular files
1683 always take precedence over symbols from dynamic objects, even if
1684 they are defined after the dynamic object in the link.
1685
1686 As above, we again permit a common symbol in a regular object to
1687 override a definition in a shared object if the shared object
0f8a2703 1688 symbol is a function or is weak. */
45d6a902
AM
1689
1690 flip = NULL;
77cfaee6 1691 if (!newdyn
45d6a902
AM
1692 && (newdef
1693 || (bfd_is_com_section (sec)
0a36a439 1694 && (oldweak || oldfunc)))
45d6a902
AM
1695 && olddyn
1696 && olddef
f5385ebf 1697 && h->def_dynamic)
45d6a902
AM
1698 {
1699 /* Change the hash table entry to undefined, and let
1700 _bfd_generic_link_add_one_symbol do the right thing with the
1701 new definition. */
1702
1703 h->root.type = bfd_link_hash_undefined;
1704 h->root.u.undef.abfd = h->root.u.def.section->owner;
1705 *size_change_ok = TRUE;
1706
1707 olddef = FALSE;
1708 olddyncommon = FALSE;
1709
1710 /* We again permit a type change when a common symbol may be
1711 overriding a function. */
1712
1713 if (bfd_is_com_section (sec))
0a36a439
L
1714 {
1715 if (oldfunc)
1716 {
1717 /* If a common symbol overrides a function, make sure
1718 that it isn't defined dynamically nor has type
1719 function. */
1720 h->def_dynamic = 0;
1721 h->type = STT_NOTYPE;
1722 }
1723 *type_change_ok = TRUE;
1724 }
45d6a902 1725
6c9b78e6
AM
1726 if (hi->root.type == bfd_link_hash_indirect)
1727 flip = hi;
45d6a902
AM
1728 else
1729 /* This union may have been set to be non-NULL when this symbol
1730 was seen in a dynamic object. We must force the union to be
1731 NULL, so that it is correct for a regular symbol. */
1732 h->verinfo.vertree = NULL;
1733 }
1734
1735 /* Handle the special case of a new common symbol merging with an
1736 old symbol that looks like it might be a common symbol defined in
1737 a shared object. Note that we have already handled the case in
1738 which a new common symbol should simply override the definition
1739 in the shared library. */
1740
1741 if (! newdyn
1742 && bfd_is_com_section (sec)
1743 && olddyncommon)
1744 {
1745 /* It would be best if we could set the hash table entry to a
1746 common symbol, but we don't know what to use for the section
1747 or the alignment. */
1a72702b
AM
1748 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1749 bfd_link_hash_common, sym->st_size);
45d6a902 1750
4cc11e76 1751 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1752 larger, pretend that the new symbol has its size. */
1753
1754 if (h->size > *pvalue)
1755 *pvalue = h->size;
1756
af44c138
L
1757 /* We need to remember the alignment required by the symbol
1758 in the dynamic object. */
1759 BFD_ASSERT (pold_alignment);
1760 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1761
1762 olddef = FALSE;
1763 olddyncommon = FALSE;
1764
1765 h->root.type = bfd_link_hash_undefined;
1766 h->root.u.undef.abfd = h->root.u.def.section->owner;
1767
1768 *size_change_ok = TRUE;
1769 *type_change_ok = TRUE;
1770
6c9b78e6
AM
1771 if (hi->root.type == bfd_link_hash_indirect)
1772 flip = hi;
45d6a902
AM
1773 else
1774 h->verinfo.vertree = NULL;
1775 }
1776
1777 if (flip != NULL)
1778 {
1779 /* Handle the case where we had a versioned symbol in a dynamic
1780 library and now find a definition in a normal object. In this
1781 case, we make the versioned symbol point to the normal one. */
45d6a902 1782 flip->root.type = h->root.type;
00cbee0a 1783 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1784 h->root.type = bfd_link_hash_indirect;
1785 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1786 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1787 if (h->def_dynamic)
45d6a902 1788 {
f5385ebf
AM
1789 h->def_dynamic = 0;
1790 flip->ref_dynamic = 1;
45d6a902
AM
1791 }
1792 }
1793
45d6a902
AM
1794 return TRUE;
1795}
1796
1797/* This function is called to create an indirect symbol from the
1798 default for the symbol with the default version if needed. The
4f3fedcf 1799 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1800 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1801
28caa186 1802static bfd_boolean
268b6b39
AM
1803_bfd_elf_add_default_symbol (bfd *abfd,
1804 struct bfd_link_info *info,
1805 struct elf_link_hash_entry *h,
1806 const char *name,
1807 Elf_Internal_Sym *sym,
4f3fedcf
AM
1808 asection *sec,
1809 bfd_vma value,
1810 bfd **poldbfd,
e3c9d234 1811 bfd_boolean *dynsym)
45d6a902
AM
1812{
1813 bfd_boolean type_change_ok;
1814 bfd_boolean size_change_ok;
1815 bfd_boolean skip;
1816 char *shortname;
1817 struct elf_link_hash_entry *hi;
1818 struct bfd_link_hash_entry *bh;
9c5bfbb7 1819 const struct elf_backend_data *bed;
45d6a902
AM
1820 bfd_boolean collect;
1821 bfd_boolean dynamic;
e3c9d234 1822 bfd_boolean override;
45d6a902
AM
1823 char *p;
1824 size_t len, shortlen;
ffd65175 1825 asection *tmp_sec;
6e33951e 1826 bfd_boolean matched;
45d6a902 1827
422f1182
L
1828 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1829 return TRUE;
1830
45d6a902
AM
1831 /* If this symbol has a version, and it is the default version, we
1832 create an indirect symbol from the default name to the fully
1833 decorated name. This will cause external references which do not
1834 specify a version to be bound to this version of the symbol. */
1835 p = strchr (name, ELF_VER_CHR);
422f1182
L
1836 if (h->versioned == unknown)
1837 {
1838 if (p == NULL)
1839 {
1840 h->versioned = unversioned;
1841 return TRUE;
1842 }
1843 else
1844 {
1845 if (p[1] != ELF_VER_CHR)
1846 {
1847 h->versioned = versioned_hidden;
1848 return TRUE;
1849 }
1850 else
1851 h->versioned = versioned;
1852 }
1853 }
4373f8af
L
1854 else
1855 {
1856 /* PR ld/19073: We may see an unversioned definition after the
1857 default version. */
1858 if (p == NULL)
1859 return TRUE;
1860 }
45d6a902 1861
45d6a902
AM
1862 bed = get_elf_backend_data (abfd);
1863 collect = bed->collect;
1864 dynamic = (abfd->flags & DYNAMIC) != 0;
1865
1866 shortlen = p - name;
a50b1753 1867 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1868 if (shortname == NULL)
1869 return FALSE;
1870 memcpy (shortname, name, shortlen);
1871 shortname[shortlen] = '\0';
1872
1873 /* We are going to create a new symbol. Merge it with any existing
1874 symbol with this name. For the purposes of the merge, act as
1875 though we were defining the symbol we just defined, although we
1876 actually going to define an indirect symbol. */
1877 type_change_ok = FALSE;
1878 size_change_ok = FALSE;
6e33951e 1879 matched = TRUE;
ffd65175
AM
1880 tmp_sec = sec;
1881 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1882 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1883 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1884 return FALSE;
1885
1886 if (skip)
1887 goto nondefault;
1888
5b677558
AM
1889 if (hi->def_regular)
1890 {
1891 /* If the undecorated symbol will have a version added by a
1892 script different to H, then don't indirect to/from the
1893 undecorated symbol. This isn't ideal because we may not yet
1894 have seen symbol versions, if given by a script on the
1895 command line rather than via --version-script. */
1896 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1897 {
1898 bfd_boolean hide;
1899
1900 hi->verinfo.vertree
1901 = bfd_find_version_for_sym (info->version_info,
1902 hi->root.root.string, &hide);
1903 if (hi->verinfo.vertree != NULL && hide)
1904 {
1905 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1906 goto nondefault;
1907 }
1908 }
1909 if (hi->verinfo.vertree != NULL
1910 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1911 goto nondefault;
1912 }
1913
45d6a902
AM
1914 if (! override)
1915 {
c6e8a9a8 1916 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1917 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1918 {
1919 bh = &hi->root;
1920 if (! (_bfd_generic_link_add_one_symbol
1921 (info, abfd, shortname, BSF_INDIRECT,
1922 bfd_ind_section_ptr,
1923 0, name, FALSE, collect, &bh)))
1924 return FALSE;
1925 hi = (struct elf_link_hash_entry *) bh;
1926 }
45d6a902
AM
1927 }
1928 else
1929 {
1930 /* In this case the symbol named SHORTNAME is overriding the
1931 indirect symbol we want to add. We were planning on making
1932 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1933 is the name without a version. NAME is the fully versioned
1934 name, and it is the default version.
1935
1936 Overriding means that we already saw a definition for the
1937 symbol SHORTNAME in a regular object, and it is overriding
1938 the symbol defined in the dynamic object.
1939
1940 When this happens, we actually want to change NAME, the
1941 symbol we just added, to refer to SHORTNAME. This will cause
1942 references to NAME in the shared object to become references
1943 to SHORTNAME in the regular object. This is what we expect
1944 when we override a function in a shared object: that the
1945 references in the shared object will be mapped to the
1946 definition in the regular object. */
1947
1948 while (hi->root.type == bfd_link_hash_indirect
1949 || hi->root.type == bfd_link_hash_warning)
1950 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1951
1952 h->root.type = bfd_link_hash_indirect;
1953 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1954 if (h->def_dynamic)
45d6a902 1955 {
f5385ebf
AM
1956 h->def_dynamic = 0;
1957 hi->ref_dynamic = 1;
1958 if (hi->ref_regular
1959 || hi->def_regular)
45d6a902 1960 {
c152c796 1961 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1962 return FALSE;
1963 }
1964 }
1965
1966 /* Now set HI to H, so that the following code will set the
1967 other fields correctly. */
1968 hi = h;
1969 }
1970
fab4a87f
L
1971 /* Check if HI is a warning symbol. */
1972 if (hi->root.type == bfd_link_hash_warning)
1973 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1974
45d6a902
AM
1975 /* If there is a duplicate definition somewhere, then HI may not
1976 point to an indirect symbol. We will have reported an error to
1977 the user in that case. */
1978
1979 if (hi->root.type == bfd_link_hash_indirect)
1980 {
1981 struct elf_link_hash_entry *ht;
1982
45d6a902 1983 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1984 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1985
68c88cd4
AM
1986 /* A reference to the SHORTNAME symbol from a dynamic library
1987 will be satisfied by the versioned symbol at runtime. In
1988 effect, we have a reference to the versioned symbol. */
1989 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1990 hi->dynamic_def |= ht->dynamic_def;
1991
45d6a902
AM
1992 /* See if the new flags lead us to realize that the symbol must
1993 be dynamic. */
1994 if (! *dynsym)
1995 {
1996 if (! dynamic)
1997 {
0e1862bb 1998 if (! bfd_link_executable (info)
90c984fc 1999 || hi->def_dynamic
f5385ebf 2000 || hi->ref_dynamic)
45d6a902
AM
2001 *dynsym = TRUE;
2002 }
2003 else
2004 {
f5385ebf 2005 if (hi->ref_regular)
45d6a902
AM
2006 *dynsym = TRUE;
2007 }
2008 }
2009 }
2010
2011 /* We also need to define an indirection from the nondefault version
2012 of the symbol. */
2013
2014nondefault:
2015 len = strlen (name);
a50b1753 2016 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2017 if (shortname == NULL)
2018 return FALSE;
2019 memcpy (shortname, name, shortlen);
2020 memcpy (shortname + shortlen, p + 1, len - shortlen);
2021
2022 /* Once again, merge with any existing symbol. */
2023 type_change_ok = FALSE;
2024 size_change_ok = FALSE;
ffd65175
AM
2025 tmp_sec = sec;
2026 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2027 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2028 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2029 return FALSE;
2030
2031 if (skip)
2032 return TRUE;
2033
2034 if (override)
2035 {
2036 /* Here SHORTNAME is a versioned name, so we don't expect to see
2037 the type of override we do in the case above unless it is
4cc11e76 2038 overridden by a versioned definition. */
45d6a902
AM
2039 if (hi->root.type != bfd_link_hash_defined
2040 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2041 _bfd_error_handler
695344c0 2042 /* xgettext:c-format */
d003868e
AM
2043 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2044 abfd, shortname);
45d6a902
AM
2045 }
2046 else
2047 {
2048 bh = &hi->root;
2049 if (! (_bfd_generic_link_add_one_symbol
2050 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2051 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2052 return FALSE;
2053 hi = (struct elf_link_hash_entry *) bh;
2054
2055 /* If there is a duplicate definition somewhere, then HI may not
2056 point to an indirect symbol. We will have reported an error
2057 to the user in that case. */
2058
2059 if (hi->root.type == bfd_link_hash_indirect)
2060 {
fcfa13d2 2061 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2062 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2063 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2064
2065 /* See if the new flags lead us to realize that the symbol
2066 must be dynamic. */
2067 if (! *dynsym)
2068 {
2069 if (! dynamic)
2070 {
0e1862bb 2071 if (! bfd_link_executable (info)
f5385ebf 2072 || hi->ref_dynamic)
45d6a902
AM
2073 *dynsym = TRUE;
2074 }
2075 else
2076 {
f5385ebf 2077 if (hi->ref_regular)
45d6a902
AM
2078 *dynsym = TRUE;
2079 }
2080 }
2081 }
2082 }
2083
2084 return TRUE;
2085}
2086\f
2087/* This routine is used to export all defined symbols into the dynamic
2088 symbol table. It is called via elf_link_hash_traverse. */
2089
28caa186 2090static bfd_boolean
268b6b39 2091_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2092{
a50b1753 2093 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2094
2095 /* Ignore indirect symbols. These are added by the versioning code. */
2096 if (h->root.type == bfd_link_hash_indirect)
2097 return TRUE;
2098
7686d77d
AM
2099 /* Ignore this if we won't export it. */
2100 if (!eif->info->export_dynamic && !h->dynamic)
2101 return TRUE;
45d6a902
AM
2102
2103 if (h->dynindx == -1
fd91d419
L
2104 && (h->def_regular || h->ref_regular)
2105 && ! bfd_hide_sym_by_version (eif->info->version_info,
2106 h->root.root.string))
45d6a902 2107 {
fd91d419 2108 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2109 {
fd91d419
L
2110 eif->failed = TRUE;
2111 return FALSE;
45d6a902
AM
2112 }
2113 }
2114
2115 return TRUE;
2116}
2117\f
2118/* Look through the symbols which are defined in other shared
2119 libraries and referenced here. Update the list of version
2120 dependencies. This will be put into the .gnu.version_r section.
2121 This function is called via elf_link_hash_traverse. */
2122
28caa186 2123static bfd_boolean
268b6b39
AM
2124_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2125 void *data)
45d6a902 2126{
a50b1753 2127 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2128 Elf_Internal_Verneed *t;
2129 Elf_Internal_Vernaux *a;
2130 bfd_size_type amt;
2131
45d6a902
AM
2132 /* We only care about symbols defined in shared objects with version
2133 information. */
f5385ebf
AM
2134 if (!h->def_dynamic
2135 || h->def_regular
45d6a902 2136 || h->dynindx == -1
7b20f099
AM
2137 || h->verinfo.verdef == NULL
2138 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2139 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2140 return TRUE;
2141
2142 /* See if we already know about this version. */
28caa186
AM
2143 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2144 t != NULL;
2145 t = t->vn_nextref)
45d6a902
AM
2146 {
2147 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2148 continue;
2149
2150 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2151 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2152 return TRUE;
2153
2154 break;
2155 }
2156
2157 /* This is a new version. Add it to tree we are building. */
2158
2159 if (t == NULL)
2160 {
2161 amt = sizeof *t;
a50b1753 2162 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2163 if (t == NULL)
2164 {
2165 rinfo->failed = TRUE;
2166 return FALSE;
2167 }
2168
2169 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2170 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2171 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2172 }
2173
2174 amt = sizeof *a;
a50b1753 2175 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2176 if (a == NULL)
2177 {
2178 rinfo->failed = TRUE;
2179 return FALSE;
2180 }
45d6a902
AM
2181
2182 /* Note that we are copying a string pointer here, and testing it
2183 above. If bfd_elf_string_from_elf_section is ever changed to
2184 discard the string data when low in memory, this will have to be
2185 fixed. */
2186 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2187
2188 a->vna_flags = h->verinfo.verdef->vd_flags;
2189 a->vna_nextptr = t->vn_auxptr;
2190
2191 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2192 ++rinfo->vers;
2193
2194 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2195
2196 t->vn_auxptr = a;
2197
2198 return TRUE;
2199}
2200
2201/* Figure out appropriate versions for all the symbols. We may not
2202 have the version number script until we have read all of the input
2203 files, so until that point we don't know which symbols should be
2204 local. This function is called via elf_link_hash_traverse. */
2205
28caa186 2206static bfd_boolean
268b6b39 2207_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2208{
28caa186 2209 struct elf_info_failed *sinfo;
45d6a902 2210 struct bfd_link_info *info;
9c5bfbb7 2211 const struct elf_backend_data *bed;
45d6a902
AM
2212 struct elf_info_failed eif;
2213 char *p;
45d6a902 2214
a50b1753 2215 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2216 info = sinfo->info;
2217
45d6a902
AM
2218 /* Fix the symbol flags. */
2219 eif.failed = FALSE;
2220 eif.info = info;
2221 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2222 {
2223 if (eif.failed)
2224 sinfo->failed = TRUE;
2225 return FALSE;
2226 }
2227
2228 /* We only need version numbers for symbols defined in regular
2229 objects. */
f5385ebf 2230 if (!h->def_regular)
45d6a902
AM
2231 return TRUE;
2232
28caa186 2233 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2234 p = strchr (h->root.root.string, ELF_VER_CHR);
2235 if (p != NULL && h->verinfo.vertree == NULL)
2236 {
2237 struct bfd_elf_version_tree *t;
45d6a902 2238
45d6a902
AM
2239 ++p;
2240 if (*p == ELF_VER_CHR)
6e33951e 2241 ++p;
45d6a902
AM
2242
2243 /* If there is no version string, we can just return out. */
2244 if (*p == '\0')
6e33951e 2245 return TRUE;
45d6a902
AM
2246
2247 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2248 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2249 {
2250 if (strcmp (t->name, p) == 0)
2251 {
2252 size_t len;
2253 char *alc;
2254 struct bfd_elf_version_expr *d;
2255
2256 len = p - h->root.root.string;
a50b1753 2257 alc = (char *) bfd_malloc (len);
45d6a902 2258 if (alc == NULL)
14b1c01e
AM
2259 {
2260 sinfo->failed = TRUE;
2261 return FALSE;
2262 }
45d6a902
AM
2263 memcpy (alc, h->root.root.string, len - 1);
2264 alc[len - 1] = '\0';
2265 if (alc[len - 2] == ELF_VER_CHR)
2266 alc[len - 2] = '\0';
2267
2268 h->verinfo.vertree = t;
2269 t->used = TRUE;
2270 d = NULL;
2271
108ba305
JJ
2272 if (t->globals.list != NULL)
2273 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2274
2275 /* See if there is anything to force this symbol to
2276 local scope. */
108ba305 2277 if (d == NULL && t->locals.list != NULL)
45d6a902 2278 {
108ba305
JJ
2279 d = (*t->match) (&t->locals, NULL, alc);
2280 if (d != NULL
2281 && h->dynindx != -1
108ba305
JJ
2282 && ! info->export_dynamic)
2283 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2284 }
2285
2286 free (alc);
2287 break;
2288 }
2289 }
2290
2291 /* If we are building an application, we need to create a
2292 version node for this version. */
0e1862bb 2293 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2294 {
2295 struct bfd_elf_version_tree **pp;
2296 int version_index;
2297
2298 /* If we aren't going to export this symbol, we don't need
2299 to worry about it. */
2300 if (h->dynindx == -1)
2301 return TRUE;
2302
ef53be89
AM
2303 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2304 sizeof *t);
45d6a902
AM
2305 if (t == NULL)
2306 {
2307 sinfo->failed = TRUE;
2308 return FALSE;
2309 }
2310
45d6a902 2311 t->name = p;
45d6a902
AM
2312 t->name_indx = (unsigned int) -1;
2313 t->used = TRUE;
2314
2315 version_index = 1;
2316 /* Don't count anonymous version tag. */
fd91d419
L
2317 if (sinfo->info->version_info != NULL
2318 && sinfo->info->version_info->vernum == 0)
45d6a902 2319 version_index = 0;
fd91d419
L
2320 for (pp = &sinfo->info->version_info;
2321 *pp != NULL;
2322 pp = &(*pp)->next)
45d6a902
AM
2323 ++version_index;
2324 t->vernum = version_index;
2325
2326 *pp = t;
2327
2328 h->verinfo.vertree = t;
2329 }
2330 else if (t == NULL)
2331 {
2332 /* We could not find the version for a symbol when
2333 generating a shared archive. Return an error. */
4eca0228 2334 _bfd_error_handler
695344c0 2335 /* xgettext:c-format */
c55fe096 2336 (_("%B: version node not found for symbol %s"),
28caa186 2337 info->output_bfd, h->root.root.string);
45d6a902
AM
2338 bfd_set_error (bfd_error_bad_value);
2339 sinfo->failed = TRUE;
2340 return FALSE;
2341 }
45d6a902
AM
2342 }
2343
2344 /* If we don't have a version for this symbol, see if we can find
2345 something. */
fd91d419 2346 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2347 {
1e8fa21e 2348 bfd_boolean hide;
ae5a3597 2349
fd91d419
L
2350 h->verinfo.vertree
2351 = bfd_find_version_for_sym (sinfo->info->version_info,
2352 h->root.root.string, &hide);
1e8fa21e
AM
2353 if (h->verinfo.vertree != NULL && hide)
2354 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2355 }
2356
2357 return TRUE;
2358}
2359\f
45d6a902
AM
2360/* Read and swap the relocs from the section indicated by SHDR. This
2361 may be either a REL or a RELA section. The relocations are
2362 translated into RELA relocations and stored in INTERNAL_RELOCS,
2363 which should have already been allocated to contain enough space.
2364 The EXTERNAL_RELOCS are a buffer where the external form of the
2365 relocations should be stored.
2366
2367 Returns FALSE if something goes wrong. */
2368
2369static bfd_boolean
268b6b39 2370elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2371 asection *sec,
268b6b39
AM
2372 Elf_Internal_Shdr *shdr,
2373 void *external_relocs,
2374 Elf_Internal_Rela *internal_relocs)
45d6a902 2375{
9c5bfbb7 2376 const struct elf_backend_data *bed;
268b6b39 2377 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2378 const bfd_byte *erela;
2379 const bfd_byte *erelaend;
2380 Elf_Internal_Rela *irela;
243ef1e0
L
2381 Elf_Internal_Shdr *symtab_hdr;
2382 size_t nsyms;
45d6a902 2383
45d6a902
AM
2384 /* Position ourselves at the start of the section. */
2385 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2386 return FALSE;
2387
2388 /* Read the relocations. */
2389 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2390 return FALSE;
2391
243ef1e0 2392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2393 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2394
45d6a902
AM
2395 bed = get_elf_backend_data (abfd);
2396
2397 /* Convert the external relocations to the internal format. */
2398 if (shdr->sh_entsize == bed->s->sizeof_rel)
2399 swap_in = bed->s->swap_reloc_in;
2400 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2401 swap_in = bed->s->swap_reloca_in;
2402 else
2403 {
2404 bfd_set_error (bfd_error_wrong_format);
2405 return FALSE;
2406 }
2407
a50b1753 2408 erela = (const bfd_byte *) external_relocs;
51992aec 2409 erelaend = erela + shdr->sh_size;
45d6a902
AM
2410 irela = internal_relocs;
2411 while (erela < erelaend)
2412 {
243ef1e0
L
2413 bfd_vma r_symndx;
2414
45d6a902 2415 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2416 r_symndx = ELF32_R_SYM (irela->r_info);
2417 if (bed->s->arch_size == 64)
2418 r_symndx >>= 24;
ce98a316
NC
2419 if (nsyms > 0)
2420 {
2421 if ((size_t) r_symndx >= nsyms)
2422 {
4eca0228 2423 _bfd_error_handler
695344c0 2424 /* xgettext:c-format */
d42c267e 2425 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
76cfced5 2426 " for offset %#Lx in section `%A'"),
d42c267e 2427 abfd, r_symndx, (unsigned long) nsyms,
c08bb8dd 2428 irela->r_offset, sec);
ce98a316
NC
2429 bfd_set_error (bfd_error_bad_value);
2430 return FALSE;
2431 }
2432 }
cf35638d 2433 else if (r_symndx != STN_UNDEF)
243ef1e0 2434 {
4eca0228 2435 _bfd_error_handler
695344c0 2436 /* xgettext:c-format */
d42c267e 2437 (_("%B: non-zero symbol index (%#Lx)"
76cfced5 2438 " for offset %#Lx in section `%A'"
ce98a316 2439 " when the object file has no symbol table"),
d42c267e 2440 abfd, r_symndx,
c08bb8dd 2441 irela->r_offset, sec);
243ef1e0
L
2442 bfd_set_error (bfd_error_bad_value);
2443 return FALSE;
2444 }
45d6a902
AM
2445 irela += bed->s->int_rels_per_ext_rel;
2446 erela += shdr->sh_entsize;
2447 }
2448
2449 return TRUE;
2450}
2451
2452/* Read and swap the relocs for a section O. They may have been
2453 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2454 not NULL, they are used as buffers to read into. They are known to
2455 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2456 the return value is allocated using either malloc or bfd_alloc,
2457 according to the KEEP_MEMORY argument. If O has two relocation
2458 sections (both REL and RELA relocations), then the REL_HDR
2459 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2460 RELA_HDR relocations. */
45d6a902
AM
2461
2462Elf_Internal_Rela *
268b6b39
AM
2463_bfd_elf_link_read_relocs (bfd *abfd,
2464 asection *o,
2465 void *external_relocs,
2466 Elf_Internal_Rela *internal_relocs,
2467 bfd_boolean keep_memory)
45d6a902 2468{
268b6b39 2469 void *alloc1 = NULL;
45d6a902 2470 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2472 struct bfd_elf_section_data *esdo = elf_section_data (o);
2473 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2474
d4730f92
BS
2475 if (esdo->relocs != NULL)
2476 return esdo->relocs;
45d6a902
AM
2477
2478 if (o->reloc_count == 0)
2479 return NULL;
2480
45d6a902
AM
2481 if (internal_relocs == NULL)
2482 {
2483 bfd_size_type size;
2484
056bafd4 2485 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2486 if (keep_memory)
a50b1753 2487 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2488 else
a50b1753 2489 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2490 if (internal_relocs == NULL)
2491 goto error_return;
2492 }
2493
2494 if (external_relocs == NULL)
2495 {
d4730f92
BS
2496 bfd_size_type size = 0;
2497
2498 if (esdo->rel.hdr)
2499 size += esdo->rel.hdr->sh_size;
2500 if (esdo->rela.hdr)
2501 size += esdo->rela.hdr->sh_size;
45d6a902 2502
268b6b39 2503 alloc1 = bfd_malloc (size);
45d6a902
AM
2504 if (alloc1 == NULL)
2505 goto error_return;
2506 external_relocs = alloc1;
2507 }
2508
d4730f92
BS
2509 internal_rela_relocs = internal_relocs;
2510 if (esdo->rel.hdr)
2511 {
2512 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2513 external_relocs,
2514 internal_relocs))
2515 goto error_return;
2516 external_relocs = (((bfd_byte *) external_relocs)
2517 + esdo->rel.hdr->sh_size);
2518 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2519 * bed->s->int_rels_per_ext_rel);
2520 }
2521
2522 if (esdo->rela.hdr
2523 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2524 external_relocs,
2525 internal_rela_relocs)))
45d6a902
AM
2526 goto error_return;
2527
2528 /* Cache the results for next time, if we can. */
2529 if (keep_memory)
d4730f92 2530 esdo->relocs = internal_relocs;
45d6a902
AM
2531
2532 if (alloc1 != NULL)
2533 free (alloc1);
2534
2535 /* Don't free alloc2, since if it was allocated we are passing it
2536 back (under the name of internal_relocs). */
2537
2538 return internal_relocs;
2539
2540 error_return:
2541 if (alloc1 != NULL)
2542 free (alloc1);
2543 if (alloc2 != NULL)
4dd07732
AM
2544 {
2545 if (keep_memory)
2546 bfd_release (abfd, alloc2);
2547 else
2548 free (alloc2);
2549 }
45d6a902
AM
2550 return NULL;
2551}
2552
2553/* Compute the size of, and allocate space for, REL_HDR which is the
2554 section header for a section containing relocations for O. */
2555
28caa186 2556static bfd_boolean
9eaff861
AO
2557_bfd_elf_link_size_reloc_section (bfd *abfd,
2558 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2559{
9eaff861 2560 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2561
2562 /* That allows us to calculate the size of the section. */
9eaff861 2563 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2564
2565 /* The contents field must last into write_object_contents, so we
2566 allocate it with bfd_alloc rather than malloc. Also since we
2567 cannot be sure that the contents will actually be filled in,
2568 we zero the allocated space. */
a50b1753 2569 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2570 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2571 return FALSE;
2572
d4730f92 2573 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2574 {
2575 struct elf_link_hash_entry **p;
2576
ca4be51c
AM
2577 p = ((struct elf_link_hash_entry **)
2578 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2579 if (p == NULL)
2580 return FALSE;
2581
d4730f92 2582 reldata->hashes = p;
45d6a902
AM
2583 }
2584
2585 return TRUE;
2586}
2587
2588/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2589 originated from the section given by INPUT_REL_HDR) to the
2590 OUTPUT_BFD. */
2591
2592bfd_boolean
268b6b39
AM
2593_bfd_elf_link_output_relocs (bfd *output_bfd,
2594 asection *input_section,
2595 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2596 Elf_Internal_Rela *internal_relocs,
2597 struct elf_link_hash_entry **rel_hash
2598 ATTRIBUTE_UNUSED)
45d6a902
AM
2599{
2600 Elf_Internal_Rela *irela;
2601 Elf_Internal_Rela *irelaend;
2602 bfd_byte *erel;
d4730f92 2603 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2604 asection *output_section;
9c5bfbb7 2605 const struct elf_backend_data *bed;
268b6b39 2606 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2607 struct bfd_elf_section_data *esdo;
45d6a902
AM
2608
2609 output_section = input_section->output_section;
45d6a902 2610
d4730f92
BS
2611 bed = get_elf_backend_data (output_bfd);
2612 esdo = elf_section_data (output_section);
2613 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2614 {
d4730f92
BS
2615 output_reldata = &esdo->rel;
2616 swap_out = bed->s->swap_reloc_out;
45d6a902 2617 }
d4730f92
BS
2618 else if (esdo->rela.hdr
2619 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2620 {
d4730f92
BS
2621 output_reldata = &esdo->rela;
2622 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2623 }
2624 else
2625 {
4eca0228 2626 _bfd_error_handler
695344c0 2627 /* xgettext:c-format */
d003868e
AM
2628 (_("%B: relocation size mismatch in %B section %A"),
2629 output_bfd, input_section->owner, input_section);
297d8443 2630 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2631 return FALSE;
2632 }
2633
d4730f92
BS
2634 erel = output_reldata->hdr->contents;
2635 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2636 irela = internal_relocs;
2637 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2638 * bed->s->int_rels_per_ext_rel);
2639 while (irela < irelaend)
2640 {
2641 (*swap_out) (output_bfd, irela, erel);
2642 irela += bed->s->int_rels_per_ext_rel;
2643 erel += input_rel_hdr->sh_entsize;
2644 }
2645
2646 /* Bump the counter, so that we know where to add the next set of
2647 relocations. */
d4730f92 2648 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2649
2650 return TRUE;
2651}
2652\f
508c3946
L
2653/* Make weak undefined symbols in PIE dynamic. */
2654
2655bfd_boolean
2656_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2657 struct elf_link_hash_entry *h)
2658{
0e1862bb 2659 if (bfd_link_pie (info)
508c3946
L
2660 && h->dynindx == -1
2661 && h->root.type == bfd_link_hash_undefweak)
2662 return bfd_elf_link_record_dynamic_symbol (info, h);
2663
2664 return TRUE;
2665}
2666
45d6a902
AM
2667/* Fix up the flags for a symbol. This handles various cases which
2668 can only be fixed after all the input files are seen. This is
2669 currently called by both adjust_dynamic_symbol and
2670 assign_sym_version, which is unnecessary but perhaps more robust in
2671 the face of future changes. */
2672
28caa186 2673static bfd_boolean
268b6b39
AM
2674_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2675 struct elf_info_failed *eif)
45d6a902 2676{
33774f08 2677 const struct elf_backend_data *bed;
508c3946 2678
45d6a902
AM
2679 /* If this symbol was mentioned in a non-ELF file, try to set
2680 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2681 permit a non-ELF file to correctly refer to a symbol defined in
2682 an ELF dynamic object. */
f5385ebf 2683 if (h->non_elf)
45d6a902
AM
2684 {
2685 while (h->root.type == bfd_link_hash_indirect)
2686 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2687
2688 if (h->root.type != bfd_link_hash_defined
2689 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2690 {
2691 h->ref_regular = 1;
2692 h->ref_regular_nonweak = 1;
2693 }
45d6a902
AM
2694 else
2695 {
2696 if (h->root.u.def.section->owner != NULL
2697 && (bfd_get_flavour (h->root.u.def.section->owner)
2698 == bfd_target_elf_flavour))
f5385ebf
AM
2699 {
2700 h->ref_regular = 1;
2701 h->ref_regular_nonweak = 1;
2702 }
45d6a902 2703 else
f5385ebf 2704 h->def_regular = 1;
45d6a902
AM
2705 }
2706
2707 if (h->dynindx == -1
f5385ebf
AM
2708 && (h->def_dynamic
2709 || h->ref_dynamic))
45d6a902 2710 {
c152c796 2711 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2712 {
2713 eif->failed = TRUE;
2714 return FALSE;
2715 }
2716 }
2717 }
2718 else
2719 {
f5385ebf 2720 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2721 was first seen in a non-ELF file. Fortunately, if the symbol
2722 was first seen in an ELF file, we're probably OK unless the
2723 symbol was defined in a non-ELF file. Catch that case here.
2724 FIXME: We're still in trouble if the symbol was first seen in
2725 a dynamic object, and then later in a non-ELF regular object. */
2726 if ((h->root.type == bfd_link_hash_defined
2727 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2728 && !h->def_regular
45d6a902
AM
2729 && (h->root.u.def.section->owner != NULL
2730 ? (bfd_get_flavour (h->root.u.def.section->owner)
2731 != bfd_target_elf_flavour)
2732 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2733 && !h->def_dynamic)))
2734 h->def_regular = 1;
45d6a902
AM
2735 }
2736
508c3946 2737 /* Backend specific symbol fixup. */
33774f08
AM
2738 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2739 if (bed->elf_backend_fixup_symbol
2740 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2741 return FALSE;
508c3946 2742
45d6a902
AM
2743 /* If this is a final link, and the symbol was defined as a common
2744 symbol in a regular object file, and there was no definition in
2745 any dynamic object, then the linker will have allocated space for
f5385ebf 2746 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2747 flag will not have been set. */
2748 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2749 && !h->def_regular
2750 && h->ref_regular
2751 && !h->def_dynamic
96f29d96 2752 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2753 h->def_regular = 1;
45d6a902 2754
4deb8f71
L
2755 /* If a weak undefined symbol has non-default visibility, we also
2756 hide it from the dynamic linker. */
2757 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2758 && h->root.type == bfd_link_hash_undefweak)
2759 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2760
2761 /* A hidden versioned symbol in executable should be forced local if
2762 it is is locally defined, not referenced by shared library and not
2763 exported. */
2764 else if (bfd_link_executable (eif->info)
2765 && h->versioned == versioned_hidden
2766 && !eif->info->export_dynamic
2767 && !h->dynamic
2768 && !h->ref_dynamic
2769 && h->def_regular)
2770 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2771
45d6a902
AM
2772 /* If -Bsymbolic was used (which means to bind references to global
2773 symbols to the definition within the shared object), and this
2774 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2775 need a PLT entry. Likewise, if the symbol has non-default
2776 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2777 will force it local. */
4deb8f71
L
2778 else if (h->needs_plt
2779 && bfd_link_pic (eif->info)
2780 && is_elf_hash_table (eif->info->hash)
2781 && (SYMBOLIC_BIND (eif->info, h)
2782 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2783 && h->def_regular)
45d6a902 2784 {
45d6a902
AM
2785 bfd_boolean force_local;
2786
45d6a902
AM
2787 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2788 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2789 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2790 }
2791
45d6a902
AM
2792 /* If this is a weak defined symbol in a dynamic object, and we know
2793 the real definition in the dynamic object, copy interesting flags
2794 over to the real definition. */
60d67dc8 2795 if (h->is_weakalias)
45d6a902 2796 {
60d67dc8
AM
2797 struct elf_link_hash_entry *def = weakdef (h);
2798
45d6a902
AM
2799 /* If the real definition is defined by a regular object file,
2800 don't do anything special. See the longer description in
2801 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2802 if (def->def_regular)
2803 {
2804 h = def;
2805 while ((h = h->u.alias) != def)
2806 h->is_weakalias = 0;
2807 }
45d6a902 2808 else
a26587ba 2809 {
4e6b54a6
AM
2810 while (h->root.type == bfd_link_hash_indirect)
2811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2812 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2813 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2814 BFD_ASSERT (def->def_dynamic);
2815 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2816 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2817 }
45d6a902
AM
2818 }
2819
2820 return TRUE;
2821}
2822
2823/* Make the backend pick a good value for a dynamic symbol. This is
2824 called via elf_link_hash_traverse, and also calls itself
2825 recursively. */
2826
28caa186 2827static bfd_boolean
268b6b39 2828_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2829{
a50b1753 2830 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2831 struct elf_link_hash_table *htab;
9c5bfbb7 2832 const struct elf_backend_data *bed;
45d6a902 2833
0eddce27 2834 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2835 return FALSE;
2836
45d6a902
AM
2837 /* Ignore indirect symbols. These are added by the versioning code. */
2838 if (h->root.type == bfd_link_hash_indirect)
2839 return TRUE;
2840
2841 /* Fix the symbol flags. */
2842 if (! _bfd_elf_fix_symbol_flags (h, eif))
2843 return FALSE;
2844
559192d8
AM
2845 htab = elf_hash_table (eif->info);
2846 bed = get_elf_backend_data (htab->dynobj);
2847
954b63d4
AM
2848 if (h->root.type == bfd_link_hash_undefweak)
2849 {
2850 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2851 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2852 else if (eif->info->dynamic_undefined_weak > 0
2853 && h->ref_regular
2854 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2855 && !bfd_hide_sym_by_version (eif->info->version_info,
2856 h->root.root.string))
2857 {
2858 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2859 {
2860 eif->failed = TRUE;
2861 return FALSE;
2862 }
2863 }
2864 }
2865
45d6a902
AM
2866 /* If this symbol does not require a PLT entry, and it is not
2867 defined by a dynamic object, or is not referenced by a regular
2868 object, ignore it. We do have to handle a weak defined symbol,
2869 even if no regular object refers to it, if we decided to add it
2870 to the dynamic symbol table. FIXME: Do we normally need to worry
2871 about symbols which are defined by one dynamic object and
2872 referenced by another one? */
f5385ebf 2873 if (!h->needs_plt
91e21fb7 2874 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2875 && (h->def_regular
2876 || !h->def_dynamic
2877 || (!h->ref_regular
60d67dc8 2878 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2879 {
a6aa5195 2880 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2881 return TRUE;
2882 }
2883
2884 /* If we've already adjusted this symbol, don't do it again. This
2885 can happen via a recursive call. */
f5385ebf 2886 if (h->dynamic_adjusted)
45d6a902
AM
2887 return TRUE;
2888
2889 /* Don't look at this symbol again. Note that we must set this
2890 after checking the above conditions, because we may look at a
2891 symbol once, decide not to do anything, and then get called
2892 recursively later after REF_REGULAR is set below. */
f5385ebf 2893 h->dynamic_adjusted = 1;
45d6a902
AM
2894
2895 /* If this is a weak definition, and we know a real definition, and
2896 the real symbol is not itself defined by a regular object file,
2897 then get a good value for the real definition. We handle the
2898 real symbol first, for the convenience of the backend routine.
2899
2900 Note that there is a confusing case here. If the real definition
2901 is defined by a regular object file, we don't get the real symbol
2902 from the dynamic object, but we do get the weak symbol. If the
2903 processor backend uses a COPY reloc, then if some routine in the
2904 dynamic object changes the real symbol, we will not see that
2905 change in the corresponding weak symbol. This is the way other
2906 ELF linkers work as well, and seems to be a result of the shared
2907 library model.
2908
2909 I will clarify this issue. Most SVR4 shared libraries define the
2910 variable _timezone and define timezone as a weak synonym. The
2911 tzset call changes _timezone. If you write
2912 extern int timezone;
2913 int _timezone = 5;
2914 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2915 you might expect that, since timezone is a synonym for _timezone,
2916 the same number will print both times. However, if the processor
2917 backend uses a COPY reloc, then actually timezone will be copied
2918 into your process image, and, since you define _timezone
2919 yourself, _timezone will not. Thus timezone and _timezone will
2920 wind up at different memory locations. The tzset call will set
2921 _timezone, leaving timezone unchanged. */
2922
60d67dc8 2923 if (h->is_weakalias)
45d6a902 2924 {
60d67dc8
AM
2925 struct elf_link_hash_entry *def = weakdef (h);
2926
ec24dc88 2927 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
2928 the alias by a regular object file via the weak symbol H. */
2929 def->ref_regular = 1;
45d6a902 2930
ec24dc88 2931 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
2932 the strong alias before H by recursively calling ourselves. */
2933 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
2934 return FALSE;
2935 }
2936
2937 /* If a symbol has no type and no size and does not require a PLT
2938 entry, then we are probably about to do the wrong thing here: we
2939 are probably going to create a COPY reloc for an empty object.
2940 This case can arise when a shared object is built with assembly
2941 code, and the assembly code fails to set the symbol type. */
2942 if (h->size == 0
2943 && h->type == STT_NOTYPE
f5385ebf 2944 && !h->needs_plt)
4eca0228 2945 _bfd_error_handler
45d6a902
AM
2946 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2947 h->root.root.string);
2948
45d6a902
AM
2949 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2950 {
2951 eif->failed = TRUE;
2952 return FALSE;
2953 }
2954
2955 return TRUE;
2956}
2957
027297b7
L
2958/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2959 DYNBSS. */
2960
2961bfd_boolean
6cabe1ea
AM
2962_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2963 struct elf_link_hash_entry *h,
027297b7
L
2964 asection *dynbss)
2965{
91ac5911 2966 unsigned int power_of_two;
027297b7
L
2967 bfd_vma mask;
2968 asection *sec = h->root.u.def.section;
2969
de194d85 2970 /* The section alignment of the definition is the maximum alignment
91ac5911
L
2971 requirement of symbols defined in the section. Since we don't
2972 know the symbol alignment requirement, we start with the
2973 maximum alignment and check low bits of the symbol address
2974 for the minimum alignment. */
2975 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2976 mask = ((bfd_vma) 1 << power_of_two) - 1;
2977 while ((h->root.u.def.value & mask) != 0)
2978 {
2979 mask >>= 1;
2980 --power_of_two;
2981 }
027297b7 2982
91ac5911
L
2983 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2984 dynbss))
027297b7
L
2985 {
2986 /* Adjust the section alignment if needed. */
2987 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2988 power_of_two))
027297b7
L
2989 return FALSE;
2990 }
2991
91ac5911 2992 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2993 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2994
2995 /* Define the symbol as being at this point in DYNBSS. */
2996 h->root.u.def.section = dynbss;
2997 h->root.u.def.value = dynbss->size;
2998
2999 /* Increment the size of DYNBSS to make room for the symbol. */
3000 dynbss->size += h->size;
3001
f7483970
L
3002 /* No error if extern_protected_data is true. */
3003 if (h->protected_def
889c2a67
L
3004 && (!info->extern_protected_data
3005 || (info->extern_protected_data < 0
3006 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
3007 info->callbacks->einfo
3008 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3009 h->root.root.string);
6cabe1ea 3010
027297b7
L
3011 return TRUE;
3012}
3013
45d6a902
AM
3014/* Adjust all external symbols pointing into SEC_MERGE sections
3015 to reflect the object merging within the sections. */
3016
28caa186 3017static bfd_boolean
268b6b39 3018_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3019{
3020 asection *sec;
3021
45d6a902
AM
3022 if ((h->root.type == bfd_link_hash_defined
3023 || h->root.type == bfd_link_hash_defweak)
3024 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3025 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3026 {
a50b1753 3027 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3028
3029 h->root.u.def.value =
3030 _bfd_merged_section_offset (output_bfd,
3031 &h->root.u.def.section,
3032 elf_section_data (sec)->sec_info,
753731ee 3033 h->root.u.def.value);
45d6a902
AM
3034 }
3035
3036 return TRUE;
3037}
986a241f
RH
3038
3039/* Returns false if the symbol referred to by H should be considered
3040 to resolve local to the current module, and true if it should be
3041 considered to bind dynamically. */
3042
3043bfd_boolean
268b6b39
AM
3044_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3045 struct bfd_link_info *info,
89a2ee5a 3046 bfd_boolean not_local_protected)
986a241f
RH
3047{
3048 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3049 const struct elf_backend_data *bed;
3050 struct elf_link_hash_table *hash_table;
986a241f
RH
3051
3052 if (h == NULL)
3053 return FALSE;
3054
3055 while (h->root.type == bfd_link_hash_indirect
3056 || h->root.type == bfd_link_hash_warning)
3057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3058
3059 /* If it was forced local, then clearly it's not dynamic. */
3060 if (h->dynindx == -1)
3061 return FALSE;
f5385ebf 3062 if (h->forced_local)
986a241f
RH
3063 return FALSE;
3064
3065 /* Identify the cases where name binding rules say that a
3066 visible symbol resolves locally. */
0e1862bb
L
3067 binding_stays_local_p = (bfd_link_executable (info)
3068 || SYMBOLIC_BIND (info, h));
986a241f
RH
3069
3070 switch (ELF_ST_VISIBILITY (h->other))
3071 {
3072 case STV_INTERNAL:
3073 case STV_HIDDEN:
3074 return FALSE;
3075
3076 case STV_PROTECTED:
fcb93ecf
PB
3077 hash_table = elf_hash_table (info);
3078 if (!is_elf_hash_table (hash_table))
3079 return FALSE;
3080
3081 bed = get_elf_backend_data (hash_table->dynobj);
3082
986a241f
RH
3083 /* Proper resolution for function pointer equality may require
3084 that these symbols perhaps be resolved dynamically, even though
3085 we should be resolving them to the current module. */
89a2ee5a 3086 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3087 binding_stays_local_p = TRUE;
3088 break;
3089
3090 default:
986a241f
RH
3091 break;
3092 }
3093
aa37626c 3094 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3095 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3096 return TRUE;
3097
986a241f
RH
3098 /* Otherwise, the symbol is dynamic if binding rules don't tell
3099 us that it remains local. */
3100 return !binding_stays_local_p;
3101}
f6c52c13
AM
3102
3103/* Return true if the symbol referred to by H should be considered
3104 to resolve local to the current module, and false otherwise. Differs
3105 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3106 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3107 for the place where dynindx == -1 is tested. If that test is true,
3108 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3109 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3110 defined symbols.
89a2ee5a
AM
3111 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3112 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3113 treatment of undefined weak symbols. For those that do not make
3114 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3115
3116bfd_boolean
268b6b39
AM
3117_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3118 struct bfd_link_info *info,
3119 bfd_boolean local_protected)
f6c52c13 3120{
fcb93ecf
PB
3121 const struct elf_backend_data *bed;
3122 struct elf_link_hash_table *hash_table;
3123
f6c52c13
AM
3124 /* If it's a local sym, of course we resolve locally. */
3125 if (h == NULL)
3126 return TRUE;
3127
d95edcac
L
3128 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3129 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3130 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3131 return TRUE;
3132
0fad2956
MR
3133 /* Forced local symbols resolve locally. */
3134 if (h->forced_local)
3135 return TRUE;
3136
7e2294f9
AO
3137 /* Common symbols that become definitions don't get the DEF_REGULAR
3138 flag set, so test it first, and don't bail out. */
3139 if (ELF_COMMON_DEF_P (h))
3140 /* Do nothing. */;
f6c52c13 3141 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3142 resolve locally. The sym is either undefined or dynamic. */
3143 else if (!h->def_regular)
f6c52c13
AM
3144 return FALSE;
3145
0fad2956 3146 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3147 if (h->dynindx == -1)
3148 return TRUE;
3149
3150 /* At this point, we know the symbol is defined and dynamic. In an
3151 executable it must resolve locally, likewise when building symbolic
3152 shared libraries. */
0e1862bb 3153 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3154 return TRUE;
3155
3156 /* Now deal with defined dynamic symbols in shared libraries. Ones
3157 with default visibility might not resolve locally. */
3158 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3159 return FALSE;
3160
fcb93ecf
PB
3161 hash_table = elf_hash_table (info);
3162 if (!is_elf_hash_table (hash_table))
3163 return TRUE;
3164
3165 bed = get_elf_backend_data (hash_table->dynobj);
3166
f7483970
L
3167 /* If extern_protected_data is false, STV_PROTECTED non-function
3168 symbols are local. */
889c2a67
L
3169 if ((!info->extern_protected_data
3170 || (info->extern_protected_data < 0
3171 && !bed->extern_protected_data))
3172 && !bed->is_function_type (h->type))
1c16dfa5
L
3173 return TRUE;
3174
f6c52c13 3175 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3176 symbols be treated as dynamic symbols. If the address of a
3177 function not defined in an executable is set to that function's
3178 plt entry in the executable, then the address of the function in
3179 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3180 return local_protected;
3181}
e1918d23
AM
3182
3183/* Caches some TLS segment info, and ensures that the TLS segment vma is
3184 aligned. Returns the first TLS output section. */
3185
3186struct bfd_section *
3187_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3188{
3189 struct bfd_section *sec, *tls;
3190 unsigned int align = 0;
3191
3192 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3193 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3194 break;
3195 tls = sec;
3196
3197 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3198 if (sec->alignment_power > align)
3199 align = sec->alignment_power;
3200
3201 elf_hash_table (info)->tls_sec = tls;
3202
3203 /* Ensure the alignment of the first section is the largest alignment,
3204 so that the tls segment starts aligned. */
3205 if (tls != NULL)
3206 tls->alignment_power = align;
3207
3208 return tls;
3209}
0ad989f9
L
3210
3211/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3212static bfd_boolean
3213is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3214 Elf_Internal_Sym *sym)
3215{
a4d8e49b
L
3216 const struct elf_backend_data *bed;
3217
0ad989f9
L
3218 /* Local symbols do not count, but target specific ones might. */
3219 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3220 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3221 return FALSE;
3222
fcb93ecf 3223 bed = get_elf_backend_data (abfd);
0ad989f9 3224 /* Function symbols do not count. */
fcb93ecf 3225 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3226 return FALSE;
3227
3228 /* If the section is undefined, then so is the symbol. */
3229 if (sym->st_shndx == SHN_UNDEF)
3230 return FALSE;
3231
3232 /* If the symbol is defined in the common section, then
3233 it is a common definition and so does not count. */
a4d8e49b 3234 if (bed->common_definition (sym))
0ad989f9
L
3235 return FALSE;
3236
3237 /* If the symbol is in a target specific section then we
3238 must rely upon the backend to tell us what it is. */
3239 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3240 /* FIXME - this function is not coded yet:
3241
3242 return _bfd_is_global_symbol_definition (abfd, sym);
3243
3244 Instead for now assume that the definition is not global,
3245 Even if this is wrong, at least the linker will behave
3246 in the same way that it used to do. */
3247 return FALSE;
3248
3249 return TRUE;
3250}
3251
3252/* Search the symbol table of the archive element of the archive ABFD
3253 whose archive map contains a mention of SYMDEF, and determine if
3254 the symbol is defined in this element. */
3255static bfd_boolean
3256elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3257{
3258 Elf_Internal_Shdr * hdr;
ef53be89
AM
3259 size_t symcount;
3260 size_t extsymcount;
3261 size_t extsymoff;
0ad989f9
L
3262 Elf_Internal_Sym *isymbuf;
3263 Elf_Internal_Sym *isym;
3264 Elf_Internal_Sym *isymend;
3265 bfd_boolean result;
3266
3267 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3268 if (abfd == NULL)
3269 return FALSE;
3270
3271 if (! bfd_check_format (abfd, bfd_object))
3272 return FALSE;
3273
7dc3990e
L
3274 /* Select the appropriate symbol table. If we don't know if the
3275 object file is an IR object, give linker LTO plugin a chance to
3276 get the correct symbol table. */
3277 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3278#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3279 || (abfd->plugin_format == bfd_plugin_unknown
3280 && bfd_link_plugin_object_p (abfd))
3281#endif
3282 )
3283 {
3284 /* Use the IR symbol table if the object has been claimed by
3285 plugin. */
3286 abfd = abfd->plugin_dummy_bfd;
3287 hdr = &elf_tdata (abfd)->symtab_hdr;
3288 }
3289 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3290 hdr = &elf_tdata (abfd)->symtab_hdr;
3291 else
3292 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3293
3294 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3295
3296 /* The sh_info field of the symtab header tells us where the
3297 external symbols start. We don't care about the local symbols. */
3298 if (elf_bad_symtab (abfd))
3299 {
3300 extsymcount = symcount;
3301 extsymoff = 0;
3302 }
3303 else
3304 {
3305 extsymcount = symcount - hdr->sh_info;
3306 extsymoff = hdr->sh_info;
3307 }
3308
3309 if (extsymcount == 0)
3310 return FALSE;
3311
3312 /* Read in the symbol table. */
3313 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3314 NULL, NULL, NULL);
3315 if (isymbuf == NULL)
3316 return FALSE;
3317
3318 /* Scan the symbol table looking for SYMDEF. */
3319 result = FALSE;
3320 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3321 {
3322 const char *name;
3323
3324 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3325 isym->st_name);
3326 if (name == NULL)
3327 break;
3328
3329 if (strcmp (name, symdef->name) == 0)
3330 {
3331 result = is_global_data_symbol_definition (abfd, isym);
3332 break;
3333 }
3334 }
3335
3336 free (isymbuf);
3337
3338 return result;
3339}
3340\f
5a580b3a
AM
3341/* Add an entry to the .dynamic table. */
3342
3343bfd_boolean
3344_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3345 bfd_vma tag,
3346 bfd_vma val)
3347{
3348 struct elf_link_hash_table *hash_table;
3349 const struct elf_backend_data *bed;
3350 asection *s;
3351 bfd_size_type newsize;
3352 bfd_byte *newcontents;
3353 Elf_Internal_Dyn dyn;
3354
3355 hash_table = elf_hash_table (info);
3356 if (! is_elf_hash_table (hash_table))
3357 return FALSE;
3358
3359 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3360 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3361 BFD_ASSERT (s != NULL);
3362
eea6121a 3363 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3364 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3365 if (newcontents == NULL)
3366 return FALSE;
3367
3368 dyn.d_tag = tag;
3369 dyn.d_un.d_val = val;
eea6121a 3370 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3371
eea6121a 3372 s->size = newsize;
5a580b3a
AM
3373 s->contents = newcontents;
3374
3375 return TRUE;
3376}
3377
3378/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3379 otherwise just check whether one already exists. Returns -1 on error,
3380 1 if a DT_NEEDED tag already exists, and 0 on success. */
3381
4ad4eba5 3382static int
7e9f0867
AM
3383elf_add_dt_needed_tag (bfd *abfd,
3384 struct bfd_link_info *info,
4ad4eba5
AM
3385 const char *soname,
3386 bfd_boolean do_it)
5a580b3a
AM
3387{
3388 struct elf_link_hash_table *hash_table;
ef53be89 3389 size_t strindex;
5a580b3a 3390
7e9f0867
AM
3391 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3392 return -1;
3393
5a580b3a 3394 hash_table = elf_hash_table (info);
5a580b3a 3395 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3396 if (strindex == (size_t) -1)
5a580b3a
AM
3397 return -1;
3398
02be4619 3399 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3400 {
3401 asection *sdyn;
3402 const struct elf_backend_data *bed;
3403 bfd_byte *extdyn;
3404
3405 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3406 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3407 if (sdyn != NULL)
3408 for (extdyn = sdyn->contents;
3409 extdyn < sdyn->contents + sdyn->size;
3410 extdyn += bed->s->sizeof_dyn)
3411 {
3412 Elf_Internal_Dyn dyn;
5a580b3a 3413
7e9f0867
AM
3414 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3415 if (dyn.d_tag == DT_NEEDED
3416 && dyn.d_un.d_val == strindex)
3417 {
3418 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3419 return 1;
3420 }
3421 }
5a580b3a
AM
3422 }
3423
3424 if (do_it)
3425 {
7e9f0867
AM
3426 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3427 return -1;
3428
5a580b3a
AM
3429 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3430 return -1;
3431 }
3432 else
3433 /* We were just checking for existence of the tag. */
3434 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3435
3436 return 0;
3437}
3438
7b15fa7a
AM
3439/* Return true if SONAME is on the needed list between NEEDED and STOP
3440 (or the end of list if STOP is NULL), and needed by a library that
3441 will be loaded. */
3442
010e5ae2 3443static bfd_boolean
7b15fa7a
AM
3444on_needed_list (const char *soname,
3445 struct bfd_link_needed_list *needed,
3446 struct bfd_link_needed_list *stop)
010e5ae2 3447{
7b15fa7a
AM
3448 struct bfd_link_needed_list *look;
3449 for (look = needed; look != stop; look = look->next)
3450 if (strcmp (soname, look->name) == 0
3451 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3452 /* If needed by a library that itself is not directly
3453 needed, recursively check whether that library is
3454 indirectly needed. Since we add DT_NEEDED entries to
3455 the end of the list, library dependencies appear after
3456 the library. Therefore search prior to the current
3457 LOOK, preventing possible infinite recursion. */
3458 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3459 return TRUE;
3460
3461 return FALSE;
3462}
3463
14160578 3464/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3465static int
3466elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3467{
3468 const struct elf_link_hash_entry *h1;
3469 const struct elf_link_hash_entry *h2;
10b7e05b 3470 bfd_signed_vma vdiff;
5a580b3a
AM
3471
3472 h1 = *(const struct elf_link_hash_entry **) arg1;
3473 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3474 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3475 if (vdiff != 0)
3476 return vdiff > 0 ? 1 : -1;
3477 else
3478 {
d3435ae8 3479 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3480 if (sdiff != 0)
3481 return sdiff > 0 ? 1 : -1;
3482 }
14160578
AM
3483 vdiff = h1->size - h2->size;
3484 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3485}
4ad4eba5 3486
5a580b3a
AM
3487/* This function is used to adjust offsets into .dynstr for
3488 dynamic symbols. This is called via elf_link_hash_traverse. */
3489
3490static bfd_boolean
3491elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3492{
a50b1753 3493 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3494
5a580b3a
AM
3495 if (h->dynindx != -1)
3496 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3497 return TRUE;
3498}
3499
3500/* Assign string offsets in .dynstr, update all structures referencing
3501 them. */
3502
4ad4eba5
AM
3503static bfd_boolean
3504elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3505{
3506 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3507 struct elf_link_local_dynamic_entry *entry;
3508 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3509 bfd *dynobj = hash_table->dynobj;
3510 asection *sdyn;
3511 bfd_size_type size;
3512 const struct elf_backend_data *bed;
3513 bfd_byte *extdyn;
3514
3515 _bfd_elf_strtab_finalize (dynstr);
3516 size = _bfd_elf_strtab_size (dynstr);
3517
3518 bed = get_elf_backend_data (dynobj);
3d4d4302 3519 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3520 BFD_ASSERT (sdyn != NULL);
3521
3522 /* Update all .dynamic entries referencing .dynstr strings. */
3523 for (extdyn = sdyn->contents;
eea6121a 3524 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3525 extdyn += bed->s->sizeof_dyn)
3526 {
3527 Elf_Internal_Dyn dyn;
3528
3529 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3530 switch (dyn.d_tag)
3531 {
3532 case DT_STRSZ:
3533 dyn.d_un.d_val = size;
3534 break;
3535 case DT_NEEDED:
3536 case DT_SONAME:
3537 case DT_RPATH:
3538 case DT_RUNPATH:
3539 case DT_FILTER:
3540 case DT_AUXILIARY:
7ee314fa
AM
3541 case DT_AUDIT:
3542 case DT_DEPAUDIT:
5a580b3a
AM
3543 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3544 break;
3545 default:
3546 continue;
3547 }
3548 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3549 }
3550
3551 /* Now update local dynamic symbols. */
3552 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3553 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3554 entry->isym.st_name);
3555
3556 /* And the rest of dynamic symbols. */
3557 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3558
3559 /* Adjust version definitions. */
3560 if (elf_tdata (output_bfd)->cverdefs)
3561 {
3562 asection *s;
3563 bfd_byte *p;
ef53be89 3564 size_t i;
5a580b3a
AM
3565 Elf_Internal_Verdef def;
3566 Elf_Internal_Verdaux defaux;
3567
3d4d4302 3568 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3569 p = s->contents;
3570 do
3571 {
3572 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3573 &def);
3574 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3575 if (def.vd_aux != sizeof (Elf_External_Verdef))
3576 continue;
5a580b3a
AM
3577 for (i = 0; i < def.vd_cnt; ++i)
3578 {
3579 _bfd_elf_swap_verdaux_in (output_bfd,
3580 (Elf_External_Verdaux *) p, &defaux);
3581 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3582 defaux.vda_name);
3583 _bfd_elf_swap_verdaux_out (output_bfd,
3584 &defaux, (Elf_External_Verdaux *) p);
3585 p += sizeof (Elf_External_Verdaux);
3586 }
3587 }
3588 while (def.vd_next);
3589 }
3590
3591 /* Adjust version references. */
3592 if (elf_tdata (output_bfd)->verref)
3593 {
3594 asection *s;
3595 bfd_byte *p;
ef53be89 3596 size_t i;
5a580b3a
AM
3597 Elf_Internal_Verneed need;
3598 Elf_Internal_Vernaux needaux;
3599
3d4d4302 3600 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3601 p = s->contents;
3602 do
3603 {
3604 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3605 &need);
3606 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3607 _bfd_elf_swap_verneed_out (output_bfd, &need,
3608 (Elf_External_Verneed *) p);
3609 p += sizeof (Elf_External_Verneed);
3610 for (i = 0; i < need.vn_cnt; ++i)
3611 {
3612 _bfd_elf_swap_vernaux_in (output_bfd,
3613 (Elf_External_Vernaux *) p, &needaux);
3614 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3615 needaux.vna_name);
3616 _bfd_elf_swap_vernaux_out (output_bfd,
3617 &needaux,
3618 (Elf_External_Vernaux *) p);
3619 p += sizeof (Elf_External_Vernaux);
3620 }
3621 }
3622 while (need.vn_next);
3623 }
3624
3625 return TRUE;
3626}
3627\f
13285a1b
AM
3628/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3629 The default is to only match when the INPUT and OUTPUT are exactly
3630 the same target. */
3631
3632bfd_boolean
3633_bfd_elf_default_relocs_compatible (const bfd_target *input,
3634 const bfd_target *output)
3635{
3636 return input == output;
3637}
3638
3639/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3640 This version is used when different targets for the same architecture
3641 are virtually identical. */
3642
3643bfd_boolean
3644_bfd_elf_relocs_compatible (const bfd_target *input,
3645 const bfd_target *output)
3646{
3647 const struct elf_backend_data *obed, *ibed;
3648
3649 if (input == output)
3650 return TRUE;
3651
3652 ibed = xvec_get_elf_backend_data (input);
3653 obed = xvec_get_elf_backend_data (output);
3654
3655 if (ibed->arch != obed->arch)
3656 return FALSE;
3657
3658 /* If both backends are using this function, deem them compatible. */
3659 return ibed->relocs_compatible == obed->relocs_compatible;
3660}
3661
e5034e59
AM
3662/* Make a special call to the linker "notice" function to tell it that
3663 we are about to handle an as-needed lib, or have finished
1b786873 3664 processing the lib. */
e5034e59
AM
3665
3666bfd_boolean
3667_bfd_elf_notice_as_needed (bfd *ibfd,
3668 struct bfd_link_info *info,
3669 enum notice_asneeded_action act)
3670{
46135103 3671 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3672}
3673
d9689752
L
3674/* Check relocations an ELF object file. */
3675
3676bfd_boolean
3677_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3678{
3679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3680 struct elf_link_hash_table *htab = elf_hash_table (info);
3681
3682 /* If this object is the same format as the output object, and it is
3683 not a shared library, then let the backend look through the
3684 relocs.
3685
3686 This is required to build global offset table entries and to
3687 arrange for dynamic relocs. It is not required for the
3688 particular common case of linking non PIC code, even when linking
3689 against shared libraries, but unfortunately there is no way of
3690 knowing whether an object file has been compiled PIC or not.
3691 Looking through the relocs is not particularly time consuming.
3692 The problem is that we must either (1) keep the relocs in memory,
3693 which causes the linker to require additional runtime memory or
3694 (2) read the relocs twice from the input file, which wastes time.
3695 This would be a good case for using mmap.
3696
3697 I have no idea how to handle linking PIC code into a file of a
3698 different format. It probably can't be done. */
3699 if ((abfd->flags & DYNAMIC) == 0
3700 && is_elf_hash_table (htab)
3701 && bed->check_relocs != NULL
3702 && elf_object_id (abfd) == elf_hash_table_id (htab)
3703 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3704 {
3705 asection *o;
3706
3707 for (o = abfd->sections; o != NULL; o = o->next)
3708 {
3709 Elf_Internal_Rela *internal_relocs;
3710 bfd_boolean ok;
3711
5ce03cea 3712 /* Don't check relocations in excluded sections. */
d9689752 3713 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3714 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3715 || o->reloc_count == 0
3716 || ((info->strip == strip_all || info->strip == strip_debugger)
3717 && (o->flags & SEC_DEBUGGING) != 0)
3718 || bfd_is_abs_section (o->output_section))
3719 continue;
3720
3721 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3722 info->keep_memory);
3723 if (internal_relocs == NULL)
3724 return FALSE;
3725
3726 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3727
3728 if (elf_section_data (o)->relocs != internal_relocs)
3729 free (internal_relocs);
3730
3731 if (! ok)
3732 return FALSE;
3733 }
3734 }
3735
3736 return TRUE;
3737}
3738
4ad4eba5
AM
3739/* Add symbols from an ELF object file to the linker hash table. */
3740
3741static bfd_boolean
3742elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3743{
a0c402a5 3744 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3745 Elf_Internal_Shdr *hdr;
ef53be89
AM
3746 size_t symcount;
3747 size_t extsymcount;
3748 size_t extsymoff;
4ad4eba5
AM
3749 struct elf_link_hash_entry **sym_hash;
3750 bfd_boolean dynamic;
3751 Elf_External_Versym *extversym = NULL;
3752 Elf_External_Versym *ever;
3753 struct elf_link_hash_entry *weaks;
3754 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3755 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3756 Elf_Internal_Sym *isymbuf = NULL;
3757 Elf_Internal_Sym *isym;
3758 Elf_Internal_Sym *isymend;
3759 const struct elf_backend_data *bed;
3760 bfd_boolean add_needed;
66eb6687 3761 struct elf_link_hash_table *htab;
4ad4eba5 3762 bfd_size_type amt;
66eb6687 3763 void *alloc_mark = NULL;
4f87808c
AM
3764 struct bfd_hash_entry **old_table = NULL;
3765 unsigned int old_size = 0;
3766 unsigned int old_count = 0;
66eb6687 3767 void *old_tab = NULL;
66eb6687
AM
3768 void *old_ent;
3769 struct bfd_link_hash_entry *old_undefs = NULL;
3770 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3771 void *old_strtab = NULL;
66eb6687 3772 size_t tabsize = 0;
db6a5d5f 3773 asection *s;
29a9f53e 3774 bfd_boolean just_syms;
4ad4eba5 3775
66eb6687 3776 htab = elf_hash_table (info);
4ad4eba5 3777 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3778
3779 if ((abfd->flags & DYNAMIC) == 0)
3780 dynamic = FALSE;
3781 else
3782 {
3783 dynamic = TRUE;
3784
3785 /* You can't use -r against a dynamic object. Also, there's no
3786 hope of using a dynamic object which does not exactly match
3787 the format of the output file. */
0e1862bb 3788 if (bfd_link_relocatable (info)
66eb6687 3789 || !is_elf_hash_table (htab)
f13a99db 3790 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3791 {
0e1862bb 3792 if (bfd_link_relocatable (info))
9a0789ec
NC
3793 bfd_set_error (bfd_error_invalid_operation);
3794 else
3795 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3796 goto error_return;
3797 }
3798 }
3799
a0c402a5
L
3800 ehdr = elf_elfheader (abfd);
3801 if (info->warn_alternate_em
3802 && bed->elf_machine_code != ehdr->e_machine
3803 && ((bed->elf_machine_alt1 != 0
3804 && ehdr->e_machine == bed->elf_machine_alt1)
3805 || (bed->elf_machine_alt2 != 0
3806 && ehdr->e_machine == bed->elf_machine_alt2)))
3807 info->callbacks->einfo
695344c0 3808 /* xgettext:c-format */
a0c402a5
L
3809 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3810 ehdr->e_machine, abfd, bed->elf_machine_code);
3811
4ad4eba5
AM
3812 /* As a GNU extension, any input sections which are named
3813 .gnu.warning.SYMBOL are treated as warning symbols for the given
3814 symbol. This differs from .gnu.warning sections, which generate
3815 warnings when they are included in an output file. */
dd98f8d2 3816 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3817 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3818 {
db6a5d5f 3819 const char *name;
4ad4eba5 3820
db6a5d5f
AM
3821 name = bfd_get_section_name (abfd, s);
3822 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3823 {
db6a5d5f
AM
3824 char *msg;
3825 bfd_size_type sz;
3826
3827 name += sizeof ".gnu.warning." - 1;
3828
3829 /* If this is a shared object, then look up the symbol
3830 in the hash table. If it is there, and it is already
3831 been defined, then we will not be using the entry
3832 from this shared object, so we don't need to warn.
3833 FIXME: If we see the definition in a regular object
3834 later on, we will warn, but we shouldn't. The only
3835 fix is to keep track of what warnings we are supposed
3836 to emit, and then handle them all at the end of the
3837 link. */
3838 if (dynamic)
4ad4eba5 3839 {
db6a5d5f
AM
3840 struct elf_link_hash_entry *h;
3841
3842 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3843
3844 /* FIXME: What about bfd_link_hash_common? */
3845 if (h != NULL
3846 && (h->root.type == bfd_link_hash_defined
3847 || h->root.type == bfd_link_hash_defweak))
3848 continue;
3849 }
4ad4eba5 3850
db6a5d5f
AM
3851 sz = s->size;
3852 msg = (char *) bfd_alloc (abfd, sz + 1);
3853 if (msg == NULL)
3854 goto error_return;
4ad4eba5 3855
db6a5d5f
AM
3856 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3857 goto error_return;
4ad4eba5 3858
db6a5d5f 3859 msg[sz] = '\0';
4ad4eba5 3860
db6a5d5f
AM
3861 if (! (_bfd_generic_link_add_one_symbol
3862 (info, abfd, name, BSF_WARNING, s, 0, msg,
3863 FALSE, bed->collect, NULL)))
3864 goto error_return;
4ad4eba5 3865
0e1862bb 3866 if (bfd_link_executable (info))
db6a5d5f
AM
3867 {
3868 /* Clobber the section size so that the warning does
3869 not get copied into the output file. */
3870 s->size = 0;
11d2f718 3871
db6a5d5f
AM
3872 /* Also set SEC_EXCLUDE, so that symbols defined in
3873 the warning section don't get copied to the output. */
3874 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3875 }
3876 }
3877 }
3878
29a9f53e
L
3879 just_syms = ((s = abfd->sections) != NULL
3880 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3881
4ad4eba5
AM
3882 add_needed = TRUE;
3883 if (! dynamic)
3884 {
3885 /* If we are creating a shared library, create all the dynamic
3886 sections immediately. We need to attach them to something,
3887 so we attach them to this BFD, provided it is the right
bf89386a
L
3888 format and is not from ld --just-symbols. Always create the
3889 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3890 are no input BFD's of the same format as the output, we can't
3891 make a shared library. */
3892 if (!just_syms
bf89386a 3893 && (bfd_link_pic (info)
9c1d7a08 3894 || (!bfd_link_relocatable (info)
3c5fce9b 3895 && info->nointerp
9c1d7a08 3896 && (info->export_dynamic || info->dynamic)))
66eb6687 3897 && is_elf_hash_table (htab)
f13a99db 3898 && info->output_bfd->xvec == abfd->xvec
66eb6687 3899 && !htab->dynamic_sections_created)
4ad4eba5
AM
3900 {
3901 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3902 goto error_return;
3903 }
3904 }
66eb6687 3905 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3906 goto error_return;
3907 else
3908 {
4ad4eba5 3909 const char *soname = NULL;
7ee314fa 3910 char *audit = NULL;
4ad4eba5 3911 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3912 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3913 int ret;
3914
3915 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3916 ld shouldn't allow it. */
29a9f53e 3917 if (just_syms)
92fd189d 3918 abort ();
4ad4eba5
AM
3919
3920 /* If this dynamic lib was specified on the command line with
3921 --as-needed in effect, then we don't want to add a DT_NEEDED
3922 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3923 in by another lib's DT_NEEDED. When --no-add-needed is used
3924 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3925 any dynamic library in DT_NEEDED tags in the dynamic lib at
3926 all. */
3927 add_needed = (elf_dyn_lib_class (abfd)
3928 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3929 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3930
3931 s = bfd_get_section_by_name (abfd, ".dynamic");
3932 if (s != NULL)
3933 {
3934 bfd_byte *dynbuf;
3935 bfd_byte *extdyn;
cb33740c 3936 unsigned int elfsec;
4ad4eba5
AM
3937 unsigned long shlink;
3938
eea6121a 3939 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3940 {
3941error_free_dyn:
3942 free (dynbuf);
3943 goto error_return;
3944 }
4ad4eba5
AM
3945
3946 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3947 if (elfsec == SHN_BAD)
4ad4eba5
AM
3948 goto error_free_dyn;
3949 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3950
3951 for (extdyn = dynbuf;
eea6121a 3952 extdyn < dynbuf + s->size;
4ad4eba5
AM
3953 extdyn += bed->s->sizeof_dyn)
3954 {
3955 Elf_Internal_Dyn dyn;
3956
3957 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3958 if (dyn.d_tag == DT_SONAME)
3959 {
3960 unsigned int tagv = dyn.d_un.d_val;
3961 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3962 if (soname == NULL)
3963 goto error_free_dyn;
3964 }
3965 if (dyn.d_tag == DT_NEEDED)
3966 {
3967 struct bfd_link_needed_list *n, **pn;
3968 char *fnm, *anm;
3969 unsigned int tagv = dyn.d_un.d_val;
3970
3971 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3972 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3973 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3974 if (n == NULL || fnm == NULL)
3975 goto error_free_dyn;
3976 amt = strlen (fnm) + 1;
a50b1753 3977 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3978 if (anm == NULL)
3979 goto error_free_dyn;
3980 memcpy (anm, fnm, amt);
3981 n->name = anm;
3982 n->by = abfd;
3983 n->next = NULL;
66eb6687 3984 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3985 ;
3986 *pn = n;
3987 }
3988 if (dyn.d_tag == DT_RUNPATH)
3989 {
3990 struct bfd_link_needed_list *n, **pn;
3991 char *fnm, *anm;
3992 unsigned int tagv = dyn.d_un.d_val;
3993
3994 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3995 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3996 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3997 if (n == NULL || fnm == NULL)
3998 goto error_free_dyn;
3999 amt = strlen (fnm) + 1;
a50b1753 4000 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4001 if (anm == NULL)
4002 goto error_free_dyn;
4003 memcpy (anm, fnm, amt);
4004 n->name = anm;
4005 n->by = abfd;
4006 n->next = NULL;
4007 for (pn = & runpath;
4008 *pn != NULL;
4009 pn = &(*pn)->next)
4010 ;
4011 *pn = n;
4012 }
4013 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4014 if (!runpath && dyn.d_tag == DT_RPATH)
4015 {
4016 struct bfd_link_needed_list *n, **pn;
4017 char *fnm, *anm;
4018 unsigned int tagv = dyn.d_un.d_val;
4019
4020 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4021 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4022 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4023 if (n == NULL || fnm == NULL)
4024 goto error_free_dyn;
4025 amt = strlen (fnm) + 1;
a50b1753 4026 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4027 if (anm == NULL)
f8703194 4028 goto error_free_dyn;
4ad4eba5
AM
4029 memcpy (anm, fnm, amt);
4030 n->name = anm;
4031 n->by = abfd;
4032 n->next = NULL;
4033 for (pn = & rpath;
4034 *pn != NULL;
4035 pn = &(*pn)->next)
4036 ;
4037 *pn = n;
4038 }
7ee314fa
AM
4039 if (dyn.d_tag == DT_AUDIT)
4040 {
4041 unsigned int tagv = dyn.d_un.d_val;
4042 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4043 }
4ad4eba5
AM
4044 }
4045
4046 free (dynbuf);
4047 }
4048
4049 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4050 frees all more recently bfd_alloc'd blocks as well. */
4051 if (runpath)
4052 rpath = runpath;
4053
4054 if (rpath)
4055 {
4056 struct bfd_link_needed_list **pn;
66eb6687 4057 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4058 ;
4059 *pn = rpath;
4060 }
4061
9acc85a6
AM
4062 /* If we have a PT_GNU_RELRO program header, mark as read-only
4063 all sections contained fully therein. This makes relro
4064 shared library sections appear as they will at run-time. */
4065 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4066 while (--phdr >= elf_tdata (abfd)->phdr)
4067 if (phdr->p_type == PT_GNU_RELRO)
4068 {
4069 for (s = abfd->sections; s != NULL; s = s->next)
4070 if ((s->flags & SEC_ALLOC) != 0
4071 && s->vma >= phdr->p_vaddr
4072 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4073 s->flags |= SEC_READONLY;
4074 break;
4075 }
4076
4ad4eba5
AM
4077 /* We do not want to include any of the sections in a dynamic
4078 object in the output file. We hack by simply clobbering the
4079 list of sections in the BFD. This could be handled more
4080 cleanly by, say, a new section flag; the existing
4081 SEC_NEVER_LOAD flag is not the one we want, because that one
4082 still implies that the section takes up space in the output
4083 file. */
4084 bfd_section_list_clear (abfd);
4085
4ad4eba5
AM
4086 /* Find the name to use in a DT_NEEDED entry that refers to this
4087 object. If the object has a DT_SONAME entry, we use it.
4088 Otherwise, if the generic linker stuck something in
4089 elf_dt_name, we use that. Otherwise, we just use the file
4090 name. */
4091 if (soname == NULL || *soname == '\0')
4092 {
4093 soname = elf_dt_name (abfd);
4094 if (soname == NULL || *soname == '\0')
4095 soname = bfd_get_filename (abfd);
4096 }
4097
4098 /* Save the SONAME because sometimes the linker emulation code
4099 will need to know it. */
4100 elf_dt_name (abfd) = soname;
4101
7e9f0867 4102 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4103 if (ret < 0)
4104 goto error_return;
4105
4106 /* If we have already included this dynamic object in the
4107 link, just ignore it. There is no reason to include a
4108 particular dynamic object more than once. */
4109 if (ret > 0)
4110 return TRUE;
7ee314fa
AM
4111
4112 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4113 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4114 }
4115
4116 /* If this is a dynamic object, we always link against the .dynsym
4117 symbol table, not the .symtab symbol table. The dynamic linker
4118 will only see the .dynsym symbol table, so there is no reason to
4119 look at .symtab for a dynamic object. */
4120
4121 if (! dynamic || elf_dynsymtab (abfd) == 0)
4122 hdr = &elf_tdata (abfd)->symtab_hdr;
4123 else
4124 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4125
4126 symcount = hdr->sh_size / bed->s->sizeof_sym;
4127
4128 /* The sh_info field of the symtab header tells us where the
4129 external symbols start. We don't care about the local symbols at
4130 this point. */
4131 if (elf_bad_symtab (abfd))
4132 {
4133 extsymcount = symcount;
4134 extsymoff = 0;
4135 }
4136 else
4137 {
4138 extsymcount = symcount - hdr->sh_info;
4139 extsymoff = hdr->sh_info;
4140 }
4141
f45794cb 4142 sym_hash = elf_sym_hashes (abfd);
012b2306 4143 if (extsymcount != 0)
4ad4eba5
AM
4144 {
4145 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4146 NULL, NULL, NULL);
4147 if (isymbuf == NULL)
4148 goto error_return;
4149
4ad4eba5 4150 if (sym_hash == NULL)
012b2306
AM
4151 {
4152 /* We store a pointer to the hash table entry for each
4153 external symbol. */
ef53be89
AM
4154 amt = extsymcount;
4155 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4156 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4157 if (sym_hash == NULL)
4158 goto error_free_sym;
4159 elf_sym_hashes (abfd) = sym_hash;
4160 }
4ad4eba5
AM
4161 }
4162
4163 if (dynamic)
4164 {
4165 /* Read in any version definitions. */
fc0e6df6
PB
4166 if (!_bfd_elf_slurp_version_tables (abfd,
4167 info->default_imported_symver))
4ad4eba5
AM
4168 goto error_free_sym;
4169
4170 /* Read in the symbol versions, but don't bother to convert them
4171 to internal format. */
4172 if (elf_dynversym (abfd) != 0)
4173 {
4174 Elf_Internal_Shdr *versymhdr;
4175
4176 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4177 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4178 if (extversym == NULL)
4179 goto error_free_sym;
4180 amt = versymhdr->sh_size;
4181 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4182 || bfd_bread (extversym, amt, abfd) != amt)
4183 goto error_free_vers;
4184 }
4185 }
4186
66eb6687
AM
4187 /* If we are loading an as-needed shared lib, save the symbol table
4188 state before we start adding symbols. If the lib turns out
4189 to be unneeded, restore the state. */
4190 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4191 {
4192 unsigned int i;
4193 size_t entsize;
4194
4195 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4196 {
4197 struct bfd_hash_entry *p;
2de92251 4198 struct elf_link_hash_entry *h;
66eb6687
AM
4199
4200 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4201 {
4202 h = (struct elf_link_hash_entry *) p;
4203 entsize += htab->root.table.entsize;
4204 if (h->root.type == bfd_link_hash_warning)
4205 entsize += htab->root.table.entsize;
4206 }
66eb6687
AM
4207 }
4208
4209 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4210 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4211 if (old_tab == NULL)
4212 goto error_free_vers;
4213
4214 /* Remember the current objalloc pointer, so that all mem for
4215 symbols added can later be reclaimed. */
4216 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4217 if (alloc_mark == NULL)
4218 goto error_free_vers;
4219
5061a885
AM
4220 /* Make a special call to the linker "notice" function to
4221 tell it that we are about to handle an as-needed lib. */
e5034e59 4222 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4223 goto error_free_vers;
5061a885 4224
f45794cb
AM
4225 /* Clone the symbol table. Remember some pointers into the
4226 symbol table, and dynamic symbol count. */
4227 old_ent = (char *) old_tab + tabsize;
66eb6687 4228 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4229 old_undefs = htab->root.undefs;
4230 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4231 old_table = htab->root.table.table;
4232 old_size = htab->root.table.size;
4233 old_count = htab->root.table.count;
5b677558
AM
4234 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4235 if (old_strtab == NULL)
4236 goto error_free_vers;
66eb6687
AM
4237
4238 for (i = 0; i < htab->root.table.size; i++)
4239 {
4240 struct bfd_hash_entry *p;
2de92251 4241 struct elf_link_hash_entry *h;
66eb6687
AM
4242
4243 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4244 {
4245 memcpy (old_ent, p, htab->root.table.entsize);
4246 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4247 h = (struct elf_link_hash_entry *) p;
4248 if (h->root.type == bfd_link_hash_warning)
4249 {
4250 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4251 old_ent = (char *) old_ent + htab->root.table.entsize;
4252 }
66eb6687
AM
4253 }
4254 }
4255 }
4ad4eba5 4256
66eb6687 4257 weaks = NULL;
4ad4eba5
AM
4258 ever = extversym != NULL ? extversym + extsymoff : NULL;
4259 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4260 isym < isymend;
4261 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4262 {
4263 int bind;
4264 bfd_vma value;
af44c138 4265 asection *sec, *new_sec;
4ad4eba5
AM
4266 flagword flags;
4267 const char *name;
4268 struct elf_link_hash_entry *h;
90c984fc 4269 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4270 bfd_boolean definition;
4271 bfd_boolean size_change_ok;
4272 bfd_boolean type_change_ok;
37a9e49a
L
4273 bfd_boolean new_weak;
4274 bfd_boolean old_weak;
4ad4eba5 4275 bfd_boolean override;
a4d8e49b 4276 bfd_boolean common;
97196564 4277 bfd_boolean discarded;
4ad4eba5
AM
4278 unsigned int old_alignment;
4279 bfd *old_bfd;
6e33951e 4280 bfd_boolean matched;
4ad4eba5
AM
4281
4282 override = FALSE;
4283
4284 flags = BSF_NO_FLAGS;
4285 sec = NULL;
4286 value = isym->st_value;
a4d8e49b 4287 common = bed->common_definition (isym);
2980ccad
L
4288 if (common && info->inhibit_common_definition)
4289 {
4290 /* Treat common symbol as undefined for --no-define-common. */
4291 isym->st_shndx = SHN_UNDEF;
4292 common = FALSE;
4293 }
97196564 4294 discarded = FALSE;
4ad4eba5
AM
4295
4296 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4297 switch (bind)
4ad4eba5 4298 {
3e7a7d11 4299 case STB_LOCAL:
4ad4eba5
AM
4300 /* This should be impossible, since ELF requires that all
4301 global symbols follow all local symbols, and that sh_info
4302 point to the first global symbol. Unfortunately, Irix 5
4303 screws this up. */
4304 continue;
3e7a7d11
NC
4305
4306 case STB_GLOBAL:
a4d8e49b 4307 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4308 flags = BSF_GLOBAL;
3e7a7d11
NC
4309 break;
4310
4311 case STB_WEAK:
4312 flags = BSF_WEAK;
4313 break;
4314
4315 case STB_GNU_UNIQUE:
4316 flags = BSF_GNU_UNIQUE;
4317 break;
4318
4319 default:
4ad4eba5 4320 /* Leave it up to the processor backend. */
3e7a7d11 4321 break;
4ad4eba5
AM
4322 }
4323
4324 if (isym->st_shndx == SHN_UNDEF)
4325 sec = bfd_und_section_ptr;
cb33740c
AM
4326 else if (isym->st_shndx == SHN_ABS)
4327 sec = bfd_abs_section_ptr;
4328 else if (isym->st_shndx == SHN_COMMON)
4329 {
4330 sec = bfd_com_section_ptr;
4331 /* What ELF calls the size we call the value. What ELF
4332 calls the value we call the alignment. */
4333 value = isym->st_size;
4334 }
4335 else
4ad4eba5
AM
4336 {
4337 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4338 if (sec == NULL)
4339 sec = bfd_abs_section_ptr;
dbaa2011 4340 else if (discarded_section (sec))
529fcb95 4341 {
e5d08002
L
4342 /* Symbols from discarded section are undefined. We keep
4343 its visibility. */
529fcb95 4344 sec = bfd_und_section_ptr;
97196564 4345 discarded = TRUE;
529fcb95
PB
4346 isym->st_shndx = SHN_UNDEF;
4347 }
4ad4eba5
AM
4348 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4349 value -= sec->vma;
4350 }
4ad4eba5
AM
4351
4352 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4353 isym->st_name);
4354 if (name == NULL)
4355 goto error_free_vers;
4356
4357 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4358 && (abfd->flags & BFD_PLUGIN) != 0)
4359 {
4360 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4361
4362 if (xc == NULL)
4363 {
4364 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4365 | SEC_EXCLUDE);
4366 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4367 if (xc == NULL)
4368 goto error_free_vers;
4369 }
4370 sec = xc;
4371 }
4372 else if (isym->st_shndx == SHN_COMMON
4373 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4374 && !bfd_link_relocatable (info))
4ad4eba5
AM
4375 {
4376 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4377
4378 if (tcomm == NULL)
4379 {
02d00247
AM
4380 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4381 | SEC_LINKER_CREATED);
4382 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4383 if (tcomm == NULL)
4ad4eba5
AM
4384 goto error_free_vers;
4385 }
4386 sec = tcomm;
4387 }
66eb6687 4388 else if (bed->elf_add_symbol_hook)
4ad4eba5 4389 {
66eb6687
AM
4390 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4391 &sec, &value))
4ad4eba5
AM
4392 goto error_free_vers;
4393
4394 /* The hook function sets the name to NULL if this symbol
4395 should be skipped for some reason. */
4396 if (name == NULL)
4397 continue;
4398 }
4399
4400 /* Sanity check that all possibilities were handled. */
4401 if (sec == NULL)
4402 {
4403 bfd_set_error (bfd_error_bad_value);
4404 goto error_free_vers;
4405 }
4406
191c0c42
AM
4407 /* Silently discard TLS symbols from --just-syms. There's
4408 no way to combine a static TLS block with a new TLS block
4409 for this executable. */
4410 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4411 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4412 continue;
4413
4ad4eba5
AM
4414 if (bfd_is_und_section (sec)
4415 || bfd_is_com_section (sec))
4416 definition = FALSE;
4417 else
4418 definition = TRUE;
4419
4420 size_change_ok = FALSE;
66eb6687 4421 type_change_ok = bed->type_change_ok;
37a9e49a 4422 old_weak = FALSE;
6e33951e 4423 matched = FALSE;
4ad4eba5
AM
4424 old_alignment = 0;
4425 old_bfd = NULL;
af44c138 4426 new_sec = sec;
4ad4eba5 4427
66eb6687 4428 if (is_elf_hash_table (htab))
4ad4eba5
AM
4429 {
4430 Elf_Internal_Versym iver;
4431 unsigned int vernum = 0;
4432 bfd_boolean skip;
4433
fc0e6df6 4434 if (ever == NULL)
4ad4eba5 4435 {
fc0e6df6
PB
4436 if (info->default_imported_symver)
4437 /* Use the default symbol version created earlier. */
4438 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4439 else
4440 iver.vs_vers = 0;
4441 }
4442 else
4443 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4444
4445 vernum = iver.vs_vers & VERSYM_VERSION;
4446
4447 /* If this is a hidden symbol, or if it is not version
4448 1, we append the version name to the symbol name.
cc86ff91
EB
4449 However, we do not modify a non-hidden absolute symbol
4450 if it is not a function, because it might be the version
4451 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4452 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4453 || (vernum > 1
4454 && (!bfd_is_abs_section (sec)
4455 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4456 {
4457 const char *verstr;
4458 size_t namelen, verlen, newlen;
4459 char *newname, *p;
4460
4461 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4462 {
fc0e6df6
PB
4463 if (vernum > elf_tdata (abfd)->cverdefs)
4464 verstr = NULL;
4465 else if (vernum > 1)
4466 verstr =
4467 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4468 else
4469 verstr = "";
4ad4eba5 4470
fc0e6df6 4471 if (verstr == NULL)
4ad4eba5 4472 {
4eca0228 4473 _bfd_error_handler
695344c0 4474 /* xgettext:c-format */
fc0e6df6
PB
4475 (_("%B: %s: invalid version %u (max %d)"),
4476 abfd, name, vernum,
4477 elf_tdata (abfd)->cverdefs);
4478 bfd_set_error (bfd_error_bad_value);
4479 goto error_free_vers;
4ad4eba5 4480 }
fc0e6df6
PB
4481 }
4482 else
4483 {
4484 /* We cannot simply test for the number of
4485 entries in the VERNEED section since the
4486 numbers for the needed versions do not start
4487 at 0. */
4488 Elf_Internal_Verneed *t;
4489
4490 verstr = NULL;
4491 for (t = elf_tdata (abfd)->verref;
4492 t != NULL;
4493 t = t->vn_nextref)
4ad4eba5 4494 {
fc0e6df6 4495 Elf_Internal_Vernaux *a;
4ad4eba5 4496
fc0e6df6
PB
4497 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4498 {
4499 if (a->vna_other == vernum)
4ad4eba5 4500 {
fc0e6df6
PB
4501 verstr = a->vna_nodename;
4502 break;
4ad4eba5 4503 }
4ad4eba5 4504 }
fc0e6df6
PB
4505 if (a != NULL)
4506 break;
4507 }
4508 if (verstr == NULL)
4509 {
4eca0228 4510 _bfd_error_handler
695344c0 4511 /* xgettext:c-format */
fc0e6df6
PB
4512 (_("%B: %s: invalid needed version %d"),
4513 abfd, name, vernum);
4514 bfd_set_error (bfd_error_bad_value);
4515 goto error_free_vers;
4ad4eba5 4516 }
4ad4eba5 4517 }
fc0e6df6
PB
4518
4519 namelen = strlen (name);
4520 verlen = strlen (verstr);
4521 newlen = namelen + verlen + 2;
4522 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4523 && isym->st_shndx != SHN_UNDEF)
4524 ++newlen;
4525
a50b1753 4526 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4527 if (newname == NULL)
4528 goto error_free_vers;
4529 memcpy (newname, name, namelen);
4530 p = newname + namelen;
4531 *p++ = ELF_VER_CHR;
4532 /* If this is a defined non-hidden version symbol,
4533 we add another @ to the name. This indicates the
4534 default version of the symbol. */
4535 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4536 && isym->st_shndx != SHN_UNDEF)
4537 *p++ = ELF_VER_CHR;
4538 memcpy (p, verstr, verlen + 1);
4539
4540 name = newname;
4ad4eba5
AM
4541 }
4542
cd3416da
AM
4543 /* If this symbol has default visibility and the user has
4544 requested we not re-export it, then mark it as hidden. */
a0d49154 4545 if (!bfd_is_und_section (sec)
cd3416da 4546 && !dynamic
ce875075 4547 && abfd->no_export
cd3416da
AM
4548 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4549 isym->st_other = (STV_HIDDEN
4550 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4551
4f3fedcf
AM
4552 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4553 sym_hash, &old_bfd, &old_weak,
4554 &old_alignment, &skip, &override,
6e33951e
L
4555 &type_change_ok, &size_change_ok,
4556 &matched))
4ad4eba5
AM
4557 goto error_free_vers;
4558
4559 if (skip)
4560 continue;
4561
6e33951e
L
4562 /* Override a definition only if the new symbol matches the
4563 existing one. */
4564 if (override && matched)
4ad4eba5
AM
4565 definition = FALSE;
4566
4567 h = *sym_hash;
4568 while (h->root.type == bfd_link_hash_indirect
4569 || h->root.type == bfd_link_hash_warning)
4570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4571
4ad4eba5 4572 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4573 && vernum > 1
4574 && definition)
4575 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4576 }
4577
4578 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4579 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4580 (struct bfd_link_hash_entry **) sym_hash)))
4581 goto error_free_vers;
4582
a43942db
MR
4583 if ((flags & BSF_GNU_UNIQUE)
4584 && (abfd->flags & DYNAMIC) == 0
4585 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4586 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4587
4ad4eba5 4588 h = *sym_hash;
90c984fc
L
4589 /* We need to make sure that indirect symbol dynamic flags are
4590 updated. */
4591 hi = h;
4ad4eba5
AM
4592 while (h->root.type == bfd_link_hash_indirect
4593 || h->root.type == bfd_link_hash_warning)
4594 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4595
97196564
L
4596 /* Setting the index to -3 tells elf_link_output_extsym that
4597 this symbol is defined in a discarded section. */
4598 if (discarded)
4599 h->indx = -3;
4600
4ad4eba5
AM
4601 *sym_hash = h;
4602
37a9e49a 4603 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4604 if (dynamic
4605 && definition
37a9e49a 4606 && new_weak
fcb93ecf 4607 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4608 && is_elf_hash_table (htab)
60d67dc8 4609 && h->u.alias == NULL)
4ad4eba5
AM
4610 {
4611 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4612 a dynamic object, using the alias field. Later in this
4613 function we will set the alias field to the correct
4ad4eba5
AM
4614 value. We only put non-function symbols from dynamic
4615 objects on this list, because that happens to be the only
4616 time we need to know the normal symbol corresponding to a
4617 weak symbol, and the information is time consuming to
60d67dc8 4618 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4619 then this symbol was already defined by some previous
4620 dynamic object, and we will be using that previous
4621 definition anyhow. */
4622
60d67dc8 4623 h->u.alias = weaks;
4ad4eba5 4624 weaks = h;
4ad4eba5
AM
4625 }
4626
4627 /* Set the alignment of a common symbol. */
a4d8e49b 4628 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4629 && h->root.type == bfd_link_hash_common)
4630 {
4631 unsigned int align;
4632
a4d8e49b 4633 if (common)
af44c138
L
4634 align = bfd_log2 (isym->st_value);
4635 else
4636 {
4637 /* The new symbol is a common symbol in a shared object.
4638 We need to get the alignment from the section. */
4639 align = new_sec->alignment_power;
4640 }
595213d4 4641 if (align > old_alignment)
4ad4eba5
AM
4642 h->root.u.c.p->alignment_power = align;
4643 else
4644 h->root.u.c.p->alignment_power = old_alignment;
4645 }
4646
66eb6687 4647 if (is_elf_hash_table (htab))
4ad4eba5 4648 {
4f3fedcf
AM
4649 /* Set a flag in the hash table entry indicating the type of
4650 reference or definition we just found. A dynamic symbol
4651 is one which is referenced or defined by both a regular
4652 object and a shared object. */
4653 bfd_boolean dynsym = FALSE;
4654
4655 /* Plugin symbols aren't normal. Don't set def_regular or
4656 ref_regular for them, or make them dynamic. */
4657 if ((abfd->flags & BFD_PLUGIN) != 0)
4658 ;
4659 else if (! dynamic)
4660 {
4661 if (! definition)
4662 {
4663 h->ref_regular = 1;
4664 if (bind != STB_WEAK)
4665 h->ref_regular_nonweak = 1;
4666 }
4667 else
4668 {
4669 h->def_regular = 1;
4670 if (h->def_dynamic)
4671 {
4672 h->def_dynamic = 0;
4673 h->ref_dynamic = 1;
4674 }
4675 }
4676
4677 /* If the indirect symbol has been forced local, don't
4678 make the real symbol dynamic. */
4679 if ((h == hi || !hi->forced_local)
0e1862bb 4680 && (bfd_link_dll (info)
4f3fedcf
AM
4681 || h->def_dynamic
4682 || h->ref_dynamic))
4683 dynsym = TRUE;
4684 }
4685 else
4686 {
4687 if (! definition)
4688 {
4689 h->ref_dynamic = 1;
4690 hi->ref_dynamic = 1;
4691 }
4692 else
4693 {
4694 h->def_dynamic = 1;
4695 hi->def_dynamic = 1;
4696 }
4697
4698 /* If the indirect symbol has been forced local, don't
4699 make the real symbol dynamic. */
4700 if ((h == hi || !hi->forced_local)
4701 && (h->def_regular
4702 || h->ref_regular
60d67dc8
AM
4703 || (h->is_weakalias
4704 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4705 dynsym = TRUE;
4706 }
4707
4708 /* Check to see if we need to add an indirect symbol for
4709 the default name. */
4710 if (definition
4711 || (!override && h->root.type == bfd_link_hash_common))
4712 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4713 sec, value, &old_bfd, &dynsym))
4714 goto error_free_vers;
4ad4eba5
AM
4715
4716 /* Check the alignment when a common symbol is involved. This
4717 can change when a common symbol is overridden by a normal
4718 definition or a common symbol is ignored due to the old
4719 normal definition. We need to make sure the maximum
4720 alignment is maintained. */
a4d8e49b 4721 if ((old_alignment || common)
4ad4eba5
AM
4722 && h->root.type != bfd_link_hash_common)
4723 {
4724 unsigned int common_align;
4725 unsigned int normal_align;
4726 unsigned int symbol_align;
4727 bfd *normal_bfd;
4728 bfd *common_bfd;
4729
3a81e825
AM
4730 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4731 || h->root.type == bfd_link_hash_defweak);
4732
4ad4eba5
AM
4733 symbol_align = ffs (h->root.u.def.value) - 1;
4734 if (h->root.u.def.section->owner != NULL
0616a280
AM
4735 && (h->root.u.def.section->owner->flags
4736 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4737 {
4738 normal_align = h->root.u.def.section->alignment_power;
4739 if (normal_align > symbol_align)
4740 normal_align = symbol_align;
4741 }
4742 else
4743 normal_align = symbol_align;
4744
4745 if (old_alignment)
4746 {
4747 common_align = old_alignment;
4748 common_bfd = old_bfd;
4749 normal_bfd = abfd;
4750 }
4751 else
4752 {
4753 common_align = bfd_log2 (isym->st_value);
4754 common_bfd = abfd;
4755 normal_bfd = old_bfd;
4756 }
4757
4758 if (normal_align < common_align)
d07676f8
NC
4759 {
4760 /* PR binutils/2735 */
4761 if (normal_bfd == NULL)
4eca0228 4762 _bfd_error_handler
695344c0 4763 /* xgettext:c-format */
4f3fedcf
AM
4764 (_("Warning: alignment %u of common symbol `%s' in %B is"
4765 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4766 1 << common_align, name, common_bfd,
4767 1 << normal_align, h->root.u.def.section);
d07676f8 4768 else
4eca0228 4769 _bfd_error_handler
695344c0 4770 /* xgettext:c-format */
d07676f8
NC
4771 (_("Warning: alignment %u of symbol `%s' in %B"
4772 " is smaller than %u in %B"),
c08bb8dd
AM
4773 1 << normal_align, name, normal_bfd,
4774 1 << common_align, common_bfd);
d07676f8 4775 }
4ad4eba5
AM
4776 }
4777
83ad0046 4778 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4779 if (isym->st_size != 0
4780 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4781 && (definition || h->size == 0))
4782 {
83ad0046
L
4783 if (h->size != 0
4784 && h->size != isym->st_size
4785 && ! size_change_ok)
4eca0228 4786 _bfd_error_handler
695344c0 4787 /* xgettext:c-format */
d003868e 4788 (_("Warning: size of symbol `%s' changed"
76cfced5
AM
4789 " from %Lu in %B to %Lu in %B"),
4790 name, h->size, old_bfd, isym->st_size, abfd);
4ad4eba5
AM
4791
4792 h->size = isym->st_size;
4793 }
4794
4795 /* If this is a common symbol, then we always want H->SIZE
4796 to be the size of the common symbol. The code just above
4797 won't fix the size if a common symbol becomes larger. We
4798 don't warn about a size change here, because that is
4f3fedcf 4799 covered by --warn-common. Allow changes between different
fcb93ecf 4800 function types. */
4ad4eba5
AM
4801 if (h->root.type == bfd_link_hash_common)
4802 h->size = h->root.u.c.size;
4803
4804 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4805 && ((definition && !new_weak)
4806 || (old_weak && h->root.type == bfd_link_hash_common)
4807 || h->type == STT_NOTYPE))
4ad4eba5 4808 {
2955ec4c
L
4809 unsigned int type = ELF_ST_TYPE (isym->st_info);
4810
4811 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4812 symbol. */
4813 if (type == STT_GNU_IFUNC
4814 && (abfd->flags & DYNAMIC) != 0)
4815 type = STT_FUNC;
4ad4eba5 4816
2955ec4c
L
4817 if (h->type != type)
4818 {
4819 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4820 /* xgettext:c-format */
4eca0228 4821 _bfd_error_handler
2955ec4c
L
4822 (_("Warning: type of symbol `%s' changed"
4823 " from %d to %d in %B"),
c08bb8dd 4824 name, h->type, type, abfd);
2955ec4c
L
4825
4826 h->type = type;
4827 }
4ad4eba5
AM
4828 }
4829
54ac0771 4830 /* Merge st_other field. */
b8417128 4831 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4832
c3df8c14 4833 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4834 if (definition
4835 && (sec->flags & SEC_DEBUGGING)
4836 && !bfd_link_relocatable (info))
c3df8c14
AM
4837 dynsym = FALSE;
4838
4f3fedcf
AM
4839 /* Nor should we make plugin symbols dynamic. */
4840 if ((abfd->flags & BFD_PLUGIN) != 0)
4841 dynsym = FALSE;
4842
35fc36a8 4843 if (definition)
35399224
L
4844 {
4845 h->target_internal = isym->st_target_internal;
4846 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4847 }
35fc36a8 4848
4ad4eba5
AM
4849 if (definition && !dynamic)
4850 {
4851 char *p = strchr (name, ELF_VER_CHR);
4852 if (p != NULL && p[1] != ELF_VER_CHR)
4853 {
4854 /* Queue non-default versions so that .symver x, x@FOO
4855 aliases can be checked. */
66eb6687 4856 if (!nondeflt_vers)
4ad4eba5 4857 {
66eb6687
AM
4858 amt = ((isymend - isym + 1)
4859 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4860 nondeflt_vers
4861 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4862 if (!nondeflt_vers)
4863 goto error_free_vers;
4ad4eba5 4864 }
66eb6687 4865 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4866 }
4867 }
4868
4869 if (dynsym && h->dynindx == -1)
4870 {
c152c796 4871 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4872 goto error_free_vers;
60d67dc8
AM
4873 if (h->is_weakalias
4874 && weakdef (h)->dynindx == -1)
4ad4eba5 4875 {
60d67dc8 4876 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
4877 goto error_free_vers;
4878 }
4879 }
1f599d0e 4880 else if (h->dynindx != -1)
4ad4eba5
AM
4881 /* If the symbol already has a dynamic index, but
4882 visibility says it should not be visible, turn it into
4883 a local symbol. */
4884 switch (ELF_ST_VISIBILITY (h->other))
4885 {
4886 case STV_INTERNAL:
4887 case STV_HIDDEN:
4888 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4889 dynsym = FALSE;
4890 break;
4891 }
4892
aef28989
L
4893 /* Don't add DT_NEEDED for references from the dummy bfd nor
4894 for unmatched symbol. */
4ad4eba5 4895 if (!add_needed
aef28989 4896 && matched
4ad4eba5 4897 && definition
010e5ae2 4898 && ((dynsym
ffa9430d 4899 && h->ref_regular_nonweak
4f3fedcf
AM
4900 && (old_bfd == NULL
4901 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4902 || (h->ref_dynamic_nonweak
010e5ae2 4903 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4904 && !on_needed_list (elf_dt_name (abfd),
4905 htab->needed, NULL))))
4ad4eba5
AM
4906 {
4907 int ret;
4908 const char *soname = elf_dt_name (abfd);
4909
16e4ecc0
AM
4910 info->callbacks->minfo ("%!", soname, old_bfd,
4911 h->root.root.string);
4912
4ad4eba5
AM
4913 /* A symbol from a library loaded via DT_NEEDED of some
4914 other library is referenced by a regular object.
e56f61be 4915 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4916 --no-add-needed is used and the reference was not
4917 a weak one. */
4f3fedcf 4918 if (old_bfd != NULL
b918acf9 4919 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4920 {
4eca0228 4921 _bfd_error_handler
695344c0 4922 /* xgettext:c-format */
3cbc5de0 4923 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4924 old_bfd, name);
ff5ac77b 4925 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4926 goto error_free_vers;
4927 }
4928
a50b1753 4929 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4930 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4931
4ad4eba5 4932 add_needed = TRUE;
7e9f0867 4933 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4934 if (ret < 0)
4935 goto error_free_vers;
4936
4937 BFD_ASSERT (ret == 0);
4938 }
4939 }
4940 }
4941
a83ef4d1
L
4942 if (info->lto_plugin_active
4943 && !bfd_link_relocatable (info)
4944 && (abfd->flags & BFD_PLUGIN) == 0
4945 && !just_syms
4946 && extsymcount)
4947 {
4948 int r_sym_shift;
4949
4950 if (bed->s->arch_size == 32)
4951 r_sym_shift = 8;
4952 else
4953 r_sym_shift = 32;
4954
4955 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4956 referenced in regular objects so that linker plugin will get
4957 the correct symbol resolution. */
4958
4959 sym_hash = elf_sym_hashes (abfd);
4960 for (s = abfd->sections; s != NULL; s = s->next)
4961 {
4962 Elf_Internal_Rela *internal_relocs;
4963 Elf_Internal_Rela *rel, *relend;
4964
4965 /* Don't check relocations in excluded sections. */
4966 if ((s->flags & SEC_RELOC) == 0
4967 || s->reloc_count == 0
4968 || (s->flags & SEC_EXCLUDE) != 0
4969 || ((info->strip == strip_all
4970 || info->strip == strip_debugger)
4971 && (s->flags & SEC_DEBUGGING) != 0))
4972 continue;
4973
4974 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4975 NULL,
4976 info->keep_memory);
4977 if (internal_relocs == NULL)
4978 goto error_free_vers;
4979
4980 rel = internal_relocs;
4981 relend = rel + s->reloc_count;
4982 for ( ; rel < relend; rel++)
4983 {
4984 unsigned long r_symndx = rel->r_info >> r_sym_shift;
4985 struct elf_link_hash_entry *h;
4986
4987 /* Skip local symbols. */
4988 if (r_symndx < extsymoff)
4989 continue;
4990
4991 h = sym_hash[r_symndx - extsymoff];
4992 if (h != NULL)
4993 h->root.non_ir_ref_regular = 1;
4994 }
4995
4996 if (elf_section_data (s)->relocs != internal_relocs)
4997 free (internal_relocs);
4998 }
4999 }
5000
66eb6687
AM
5001 if (extversym != NULL)
5002 {
5003 free (extversym);
5004 extversym = NULL;
5005 }
5006
5007 if (isymbuf != NULL)
5008 {
5009 free (isymbuf);
5010 isymbuf = NULL;
5011 }
5012
5013 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5014 {
5015 unsigned int i;
5016
5017 /* Restore the symbol table. */
f45794cb
AM
5018 old_ent = (char *) old_tab + tabsize;
5019 memset (elf_sym_hashes (abfd), 0,
5020 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5021 htab->root.table.table = old_table;
5022 htab->root.table.size = old_size;
5023 htab->root.table.count = old_count;
66eb6687 5024 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5025 htab->root.undefs = old_undefs;
5026 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5027 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5028 free (old_strtab);
5029 old_strtab = NULL;
66eb6687
AM
5030 for (i = 0; i < htab->root.table.size; i++)
5031 {
5032 struct bfd_hash_entry *p;
5033 struct elf_link_hash_entry *h;
3e0882af
L
5034 bfd_size_type size;
5035 unsigned int alignment_power;
4070765b 5036 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5037
5038 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5039 {
5040 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5041 if (h->root.type == bfd_link_hash_warning)
5042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5043
3e0882af
L
5044 /* Preserve the maximum alignment and size for common
5045 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5046 since it can still be loaded at run time by another
3e0882af
L
5047 dynamic lib. */
5048 if (h->root.type == bfd_link_hash_common)
5049 {
5050 size = h->root.u.c.size;
5051 alignment_power = h->root.u.c.p->alignment_power;
5052 }
5053 else
5054 {
5055 size = 0;
5056 alignment_power = 0;
5057 }
4070765b 5058 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5059 will be exported when the dynamic lib becomes needed
5060 in the second pass. */
4070765b 5061 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5062 memcpy (p, old_ent, htab->root.table.entsize);
5063 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5064 h = (struct elf_link_hash_entry *) p;
5065 if (h->root.type == bfd_link_hash_warning)
5066 {
5067 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5068 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5069 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5070 }
a4542f1b 5071 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5072 {
5073 if (size > h->root.u.c.size)
5074 h->root.u.c.size = size;
5075 if (alignment_power > h->root.u.c.p->alignment_power)
5076 h->root.u.c.p->alignment_power = alignment_power;
5077 }
4070765b 5078 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5079 }
5080 }
5081
5061a885
AM
5082 /* Make a special call to the linker "notice" function to
5083 tell it that symbols added for crefs may need to be removed. */
e5034e59 5084 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5085 goto error_free_vers;
5061a885 5086
66eb6687
AM
5087 free (old_tab);
5088 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5089 alloc_mark);
5090 if (nondeflt_vers != NULL)
5091 free (nondeflt_vers);
5092 return TRUE;
5093 }
2de92251 5094
66eb6687
AM
5095 if (old_tab != NULL)
5096 {
e5034e59 5097 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5098 goto error_free_vers;
66eb6687
AM
5099 free (old_tab);
5100 old_tab = NULL;
5101 }
5102
c6e8a9a8
L
5103 /* Now that all the symbols from this input file are created, if
5104 not performing a relocatable link, handle .symver foo, foo@BAR
5105 such that any relocs against foo become foo@BAR. */
0e1862bb 5106 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5107 {
ef53be89 5108 size_t cnt, symidx;
4ad4eba5
AM
5109
5110 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5111 {
5112 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5113 char *shortname, *p;
5114
5115 p = strchr (h->root.root.string, ELF_VER_CHR);
5116 if (p == NULL
5117 || (h->root.type != bfd_link_hash_defined
5118 && h->root.type != bfd_link_hash_defweak))
5119 continue;
5120
5121 amt = p - h->root.root.string;
a50b1753 5122 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5123 if (!shortname)
5124 goto error_free_vers;
4ad4eba5
AM
5125 memcpy (shortname, h->root.root.string, amt);
5126 shortname[amt] = '\0';
5127
5128 hi = (struct elf_link_hash_entry *)
66eb6687 5129 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5130 FALSE, FALSE, FALSE);
5131 if (hi != NULL
5132 && hi->root.type == h->root.type
5133 && hi->root.u.def.value == h->root.u.def.value
5134 && hi->root.u.def.section == h->root.u.def.section)
5135 {
5136 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5137 hi->root.type = bfd_link_hash_indirect;
5138 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5139 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5140 sym_hash = elf_sym_hashes (abfd);
5141 if (sym_hash)
5142 for (symidx = 0; symidx < extsymcount; ++symidx)
5143 if (sym_hash[symidx] == hi)
5144 {
5145 sym_hash[symidx] = h;
5146 break;
5147 }
5148 }
5149 free (shortname);
5150 }
5151 free (nondeflt_vers);
5152 nondeflt_vers = NULL;
5153 }
5154
60d67dc8 5155 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5156 symbols we found. The only way to do this is to search all the
5157 symbols. Since we only need the information for non functions in
5158 dynamic objects, that's the only time we actually put anything on
5159 the list WEAKS. We need this information so that if a regular
5160 object refers to a symbol defined weakly in a dynamic object, the
5161 real symbol in the dynamic object is also put in the dynamic
5162 symbols; we also must arrange for both symbols to point to the
5163 same memory location. We could handle the general case of symbol
5164 aliasing, but a general symbol alias can only be generated in
5165 assembler code, handling it correctly would be very time
5166 consuming, and other ELF linkers don't handle general aliasing
5167 either. */
5168 if (weaks != NULL)
5169 {
5170 struct elf_link_hash_entry **hpp;
5171 struct elf_link_hash_entry **hppend;
5172 struct elf_link_hash_entry **sorted_sym_hash;
5173 struct elf_link_hash_entry *h;
5174 size_t sym_count;
5175
5176 /* Since we have to search the whole symbol list for each weak
5177 defined symbol, search time for N weak defined symbols will be
5178 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5179 amt = extsymcount;
5180 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5181 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5182 if (sorted_sym_hash == NULL)
5183 goto error_return;
5184 sym_hash = sorted_sym_hash;
5185 hpp = elf_sym_hashes (abfd);
5186 hppend = hpp + extsymcount;
5187 sym_count = 0;
5188 for (; hpp < hppend; hpp++)
5189 {
5190 h = *hpp;
5191 if (h != NULL
5192 && h->root.type == bfd_link_hash_defined
fcb93ecf 5193 && !bed->is_function_type (h->type))
4ad4eba5
AM
5194 {
5195 *sym_hash = h;
5196 sym_hash++;
5197 sym_count++;
5198 }
5199 }
5200
5201 qsort (sorted_sym_hash, sym_count,
5202 sizeof (struct elf_link_hash_entry *),
5203 elf_sort_symbol);
5204
5205 while (weaks != NULL)
5206 {
5207 struct elf_link_hash_entry *hlook;
5208 asection *slook;
5209 bfd_vma vlook;
ed54588d 5210 size_t i, j, idx = 0;
4ad4eba5
AM
5211
5212 hlook = weaks;
60d67dc8
AM
5213 weaks = hlook->u.alias;
5214 hlook->u.alias = NULL;
4ad4eba5 5215
e3e53eed
AM
5216 if (hlook->root.type != bfd_link_hash_defined
5217 && hlook->root.type != bfd_link_hash_defweak)
5218 continue;
5219
4ad4eba5
AM
5220 slook = hlook->root.u.def.section;
5221 vlook = hlook->root.u.def.value;
5222
4ad4eba5
AM
5223 i = 0;
5224 j = sym_count;
14160578 5225 while (i != j)
4ad4eba5
AM
5226 {
5227 bfd_signed_vma vdiff;
5228 idx = (i + j) / 2;
14160578 5229 h = sorted_sym_hash[idx];
4ad4eba5
AM
5230 vdiff = vlook - h->root.u.def.value;
5231 if (vdiff < 0)
5232 j = idx;
5233 else if (vdiff > 0)
5234 i = idx + 1;
5235 else
5236 {
d3435ae8 5237 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5238 if (sdiff < 0)
5239 j = idx;
5240 else if (sdiff > 0)
5241 i = idx + 1;
5242 else
14160578 5243 break;
4ad4eba5
AM
5244 }
5245 }
5246
5247 /* We didn't find a value/section match. */
14160578 5248 if (i == j)
4ad4eba5
AM
5249 continue;
5250
14160578
AM
5251 /* With multiple aliases, or when the weak symbol is already
5252 strongly defined, we have multiple matching symbols and
5253 the binary search above may land on any of them. Step
5254 one past the matching symbol(s). */
5255 while (++idx != j)
5256 {
5257 h = sorted_sym_hash[idx];
5258 if (h->root.u.def.section != slook
5259 || h->root.u.def.value != vlook)
5260 break;
5261 }
5262
5263 /* Now look back over the aliases. Since we sorted by size
5264 as well as value and section, we'll choose the one with
5265 the largest size. */
5266 while (idx-- != i)
4ad4eba5 5267 {
14160578 5268 h = sorted_sym_hash[idx];
4ad4eba5
AM
5269
5270 /* Stop if value or section doesn't match. */
14160578
AM
5271 if (h->root.u.def.section != slook
5272 || h->root.u.def.value != vlook)
4ad4eba5
AM
5273 break;
5274 else if (h != hlook)
5275 {
60d67dc8
AM
5276 struct elf_link_hash_entry *t;
5277
5278 hlook->u.alias = h;
5279 hlook->is_weakalias = 1;
5280 t = h;
5281 if (t->u.alias != NULL)
5282 while (t->u.alias != h)
5283 t = t->u.alias;
5284 t->u.alias = hlook;
4ad4eba5
AM
5285
5286 /* If the weak definition is in the list of dynamic
5287 symbols, make sure the real definition is put
5288 there as well. */
5289 if (hlook->dynindx != -1 && h->dynindx == -1)
5290 {
c152c796 5291 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5292 {
5293 err_free_sym_hash:
5294 free (sorted_sym_hash);
5295 goto error_return;
5296 }
4ad4eba5
AM
5297 }
5298
5299 /* If the real definition is in the list of dynamic
5300 symbols, make sure the weak definition is put
5301 there as well. If we don't do this, then the
5302 dynamic loader might not merge the entries for the
5303 real definition and the weak definition. */
5304 if (h->dynindx != -1 && hlook->dynindx == -1)
5305 {
c152c796 5306 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5307 goto err_free_sym_hash;
4ad4eba5
AM
5308 }
5309 break;
5310 }
5311 }
5312 }
5313
5314 free (sorted_sym_hash);
5315 }
5316
33177bb1
AM
5317 if (bed->check_directives
5318 && !(*bed->check_directives) (abfd, info))
5319 return FALSE;
85fbca6a 5320
4ad4eba5
AM
5321 /* If this is a non-traditional link, try to optimize the handling
5322 of the .stab/.stabstr sections. */
5323 if (! dynamic
5324 && ! info->traditional_format
66eb6687 5325 && is_elf_hash_table (htab)
4ad4eba5
AM
5326 && (info->strip != strip_all && info->strip != strip_debugger))
5327 {
5328 asection *stabstr;
5329
5330 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5331 if (stabstr != NULL)
5332 {
5333 bfd_size_type string_offset = 0;
5334 asection *stab;
5335
5336 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5337 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5338 && (!stab->name[5] ||
5339 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5340 && (stab->flags & SEC_MERGE) == 0
5341 && !bfd_is_abs_section (stab->output_section))
5342 {
5343 struct bfd_elf_section_data *secdata;
5344
5345 secdata = elf_section_data (stab);
66eb6687
AM
5346 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5347 stabstr, &secdata->sec_info,
4ad4eba5
AM
5348 &string_offset))
5349 goto error_return;
5350 if (secdata->sec_info)
dbaa2011 5351 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5352 }
5353 }
5354 }
5355
66eb6687 5356 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5357 {
5358 /* Add this bfd to the loaded list. */
5359 struct elf_link_loaded_list *n;
5360
ca4be51c 5361 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5362 if (n == NULL)
5363 goto error_return;
5364 n->abfd = abfd;
66eb6687
AM
5365 n->next = htab->loaded;
5366 htab->loaded = n;
4ad4eba5
AM
5367 }
5368
5369 return TRUE;
5370
5371 error_free_vers:
66eb6687
AM
5372 if (old_tab != NULL)
5373 free (old_tab);
5b677558
AM
5374 if (old_strtab != NULL)
5375 free (old_strtab);
4ad4eba5
AM
5376 if (nondeflt_vers != NULL)
5377 free (nondeflt_vers);
5378 if (extversym != NULL)
5379 free (extversym);
5380 error_free_sym:
5381 if (isymbuf != NULL)
5382 free (isymbuf);
5383 error_return:
5384 return FALSE;
5385}
5386
8387904d
AM
5387/* Return the linker hash table entry of a symbol that might be
5388 satisfied by an archive symbol. Return -1 on error. */
5389
5390struct elf_link_hash_entry *
5391_bfd_elf_archive_symbol_lookup (bfd *abfd,
5392 struct bfd_link_info *info,
5393 const char *name)
5394{
5395 struct elf_link_hash_entry *h;
5396 char *p, *copy;
5397 size_t len, first;
5398
2a41f396 5399 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5400 if (h != NULL)
5401 return h;
5402
5403 /* If this is a default version (the name contains @@), look up the
5404 symbol again with only one `@' as well as without the version.
5405 The effect is that references to the symbol with and without the
5406 version will be matched by the default symbol in the archive. */
5407
5408 p = strchr (name, ELF_VER_CHR);
5409 if (p == NULL || p[1] != ELF_VER_CHR)
5410 return h;
5411
5412 /* First check with only one `@'. */
5413 len = strlen (name);
a50b1753 5414 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5415 if (copy == NULL)
5416 return (struct elf_link_hash_entry *) 0 - 1;
5417
5418 first = p - name + 1;
5419 memcpy (copy, name, first);
5420 memcpy (copy + first, name + first + 1, len - first);
5421
2a41f396 5422 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5423 if (h == NULL)
5424 {
5425 /* We also need to check references to the symbol without the
5426 version. */
5427 copy[first - 1] = '\0';
5428 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5429 FALSE, FALSE, TRUE);
8387904d
AM
5430 }
5431
5432 bfd_release (abfd, copy);
5433 return h;
5434}
5435
0ad989f9 5436/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5437 don't use _bfd_generic_link_add_archive_symbols because we need to
5438 handle versioned symbols.
0ad989f9
L
5439
5440 Fortunately, ELF archive handling is simpler than that done by
5441 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5442 oddities. In ELF, if we find a symbol in the archive map, and the
5443 symbol is currently undefined, we know that we must pull in that
5444 object file.
5445
5446 Unfortunately, we do have to make multiple passes over the symbol
5447 table until nothing further is resolved. */
5448
4ad4eba5
AM
5449static bfd_boolean
5450elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5451{
5452 symindex c;
13e570f8 5453 unsigned char *included = NULL;
0ad989f9
L
5454 carsym *symdefs;
5455 bfd_boolean loop;
5456 bfd_size_type amt;
8387904d
AM
5457 const struct elf_backend_data *bed;
5458 struct elf_link_hash_entry * (*archive_symbol_lookup)
5459 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5460
5461 if (! bfd_has_map (abfd))
5462 {
5463 /* An empty archive is a special case. */
5464 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5465 return TRUE;
5466 bfd_set_error (bfd_error_no_armap);
5467 return FALSE;
5468 }
5469
5470 /* Keep track of all symbols we know to be already defined, and all
5471 files we know to be already included. This is to speed up the
5472 second and subsequent passes. */
5473 c = bfd_ardata (abfd)->symdef_count;
5474 if (c == 0)
5475 return TRUE;
5476 amt = c;
13e570f8
AM
5477 amt *= sizeof (*included);
5478 included = (unsigned char *) bfd_zmalloc (amt);
5479 if (included == NULL)
5480 return FALSE;
0ad989f9
L
5481
5482 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5483 bed = get_elf_backend_data (abfd);
5484 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5485
5486 do
5487 {
5488 file_ptr last;
5489 symindex i;
5490 carsym *symdef;
5491 carsym *symdefend;
5492
5493 loop = FALSE;
5494 last = -1;
5495
5496 symdef = symdefs;
5497 symdefend = symdef + c;
5498 for (i = 0; symdef < symdefend; symdef++, i++)
5499 {
5500 struct elf_link_hash_entry *h;
5501 bfd *element;
5502 struct bfd_link_hash_entry *undefs_tail;
5503 symindex mark;
5504
13e570f8 5505 if (included[i])
0ad989f9
L
5506 continue;
5507 if (symdef->file_offset == last)
5508 {
5509 included[i] = TRUE;
5510 continue;
5511 }
5512
8387904d
AM
5513 h = archive_symbol_lookup (abfd, info, symdef->name);
5514 if (h == (struct elf_link_hash_entry *) 0 - 1)
5515 goto error_return;
0ad989f9
L
5516
5517 if (h == NULL)
5518 continue;
5519
5520 if (h->root.type == bfd_link_hash_common)
5521 {
5522 /* We currently have a common symbol. The archive map contains
5523 a reference to this symbol, so we may want to include it. We
5524 only want to include it however, if this archive element
5525 contains a definition of the symbol, not just another common
5526 declaration of it.
5527
5528 Unfortunately some archivers (including GNU ar) will put
5529 declarations of common symbols into their archive maps, as
5530 well as real definitions, so we cannot just go by the archive
5531 map alone. Instead we must read in the element's symbol
5532 table and check that to see what kind of symbol definition
5533 this is. */
5534 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5535 continue;
5536 }
5537 else if (h->root.type != bfd_link_hash_undefined)
5538 {
5539 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5540 /* Symbol must be defined. Don't check it again. */
5541 included[i] = TRUE;
0ad989f9
L
5542 continue;
5543 }
5544
5545 /* We need to include this archive member. */
5546 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5547 if (element == NULL)
5548 goto error_return;
5549
5550 if (! bfd_check_format (element, bfd_object))
5551 goto error_return;
5552
0ad989f9
L
5553 undefs_tail = info->hash->undefs_tail;
5554
0e144ba7
AM
5555 if (!(*info->callbacks
5556 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5557 continue;
0e144ba7 5558 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5559 goto error_return;
5560
5561 /* If there are any new undefined symbols, we need to make
5562 another pass through the archive in order to see whether
5563 they can be defined. FIXME: This isn't perfect, because
5564 common symbols wind up on undefs_tail and because an
5565 undefined symbol which is defined later on in this pass
5566 does not require another pass. This isn't a bug, but it
5567 does make the code less efficient than it could be. */
5568 if (undefs_tail != info->hash->undefs_tail)
5569 loop = TRUE;
5570
5571 /* Look backward to mark all symbols from this object file
5572 which we have already seen in this pass. */
5573 mark = i;
5574 do
5575 {
5576 included[mark] = TRUE;
5577 if (mark == 0)
5578 break;
5579 --mark;
5580 }
5581 while (symdefs[mark].file_offset == symdef->file_offset);
5582
5583 /* We mark subsequent symbols from this object file as we go
5584 on through the loop. */
5585 last = symdef->file_offset;
5586 }
5587 }
5588 while (loop);
5589
0ad989f9
L
5590 free (included);
5591
5592 return TRUE;
5593
5594 error_return:
0ad989f9
L
5595 if (included != NULL)
5596 free (included);
5597 return FALSE;
5598}
4ad4eba5
AM
5599
5600/* Given an ELF BFD, add symbols to the global hash table as
5601 appropriate. */
5602
5603bfd_boolean
5604bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5605{
5606 switch (bfd_get_format (abfd))
5607 {
5608 case bfd_object:
5609 return elf_link_add_object_symbols (abfd, info);
5610 case bfd_archive:
5611 return elf_link_add_archive_symbols (abfd, info);
5612 default:
5613 bfd_set_error (bfd_error_wrong_format);
5614 return FALSE;
5615 }
5616}
5a580b3a 5617\f
14b1c01e
AM
5618struct hash_codes_info
5619{
5620 unsigned long *hashcodes;
5621 bfd_boolean error;
5622};
a0c8462f 5623
5a580b3a
AM
5624/* This function will be called though elf_link_hash_traverse to store
5625 all hash value of the exported symbols in an array. */
5626
5627static bfd_boolean
5628elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5629{
a50b1753 5630 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5631 const char *name;
5a580b3a
AM
5632 unsigned long ha;
5633 char *alc = NULL;
5634
5a580b3a
AM
5635 /* Ignore indirect symbols. These are added by the versioning code. */
5636 if (h->dynindx == -1)
5637 return TRUE;
5638
5639 name = h->root.root.string;
422f1182 5640 if (h->versioned >= versioned)
5a580b3a 5641 {
422f1182
L
5642 char *p = strchr (name, ELF_VER_CHR);
5643 if (p != NULL)
14b1c01e 5644 {
422f1182
L
5645 alc = (char *) bfd_malloc (p - name + 1);
5646 if (alc == NULL)
5647 {
5648 inf->error = TRUE;
5649 return FALSE;
5650 }
5651 memcpy (alc, name, p - name);
5652 alc[p - name] = '\0';
5653 name = alc;
14b1c01e 5654 }
5a580b3a
AM
5655 }
5656
5657 /* Compute the hash value. */
5658 ha = bfd_elf_hash (name);
5659
5660 /* Store the found hash value in the array given as the argument. */
14b1c01e 5661 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5662
5663 /* And store it in the struct so that we can put it in the hash table
5664 later. */
f6e332e6 5665 h->u.elf_hash_value = ha;
5a580b3a
AM
5666
5667 if (alc != NULL)
5668 free (alc);
5669
5670 return TRUE;
5671}
5672
fdc90cb4
JJ
5673struct collect_gnu_hash_codes
5674{
5675 bfd *output_bfd;
5676 const struct elf_backend_data *bed;
5677 unsigned long int nsyms;
5678 unsigned long int maskbits;
5679 unsigned long int *hashcodes;
5680 unsigned long int *hashval;
5681 unsigned long int *indx;
5682 unsigned long int *counts;
5683 bfd_vma *bitmask;
5684 bfd_byte *contents;
5685 long int min_dynindx;
5686 unsigned long int bucketcount;
5687 unsigned long int symindx;
5688 long int local_indx;
5689 long int shift1, shift2;
5690 unsigned long int mask;
14b1c01e 5691 bfd_boolean error;
fdc90cb4
JJ
5692};
5693
5694/* This function will be called though elf_link_hash_traverse to store
5695 all hash value of the exported symbols in an array. */
5696
5697static bfd_boolean
5698elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5699{
a50b1753 5700 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5701 const char *name;
fdc90cb4
JJ
5702 unsigned long ha;
5703 char *alc = NULL;
5704
fdc90cb4
JJ
5705 /* Ignore indirect symbols. These are added by the versioning code. */
5706 if (h->dynindx == -1)
5707 return TRUE;
5708
5709 /* Ignore also local symbols and undefined symbols. */
5710 if (! (*s->bed->elf_hash_symbol) (h))
5711 return TRUE;
5712
5713 name = h->root.root.string;
422f1182 5714 if (h->versioned >= versioned)
fdc90cb4 5715 {
422f1182
L
5716 char *p = strchr (name, ELF_VER_CHR);
5717 if (p != NULL)
14b1c01e 5718 {
422f1182
L
5719 alc = (char *) bfd_malloc (p - name + 1);
5720 if (alc == NULL)
5721 {
5722 s->error = TRUE;
5723 return FALSE;
5724 }
5725 memcpy (alc, name, p - name);
5726 alc[p - name] = '\0';
5727 name = alc;
14b1c01e 5728 }
fdc90cb4
JJ
5729 }
5730
5731 /* Compute the hash value. */
5732 ha = bfd_elf_gnu_hash (name);
5733
5734 /* Store the found hash value in the array for compute_bucket_count,
5735 and also for .dynsym reordering purposes. */
5736 s->hashcodes[s->nsyms] = ha;
5737 s->hashval[h->dynindx] = ha;
5738 ++s->nsyms;
5739 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5740 s->min_dynindx = h->dynindx;
5741
5742 if (alc != NULL)
5743 free (alc);
5744
5745 return TRUE;
5746}
5747
5748/* This function will be called though elf_link_hash_traverse to do
5749 final dynaminc symbol renumbering. */
5750
5751static bfd_boolean
5752elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5753{
a50b1753 5754 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5755 unsigned long int bucket;
5756 unsigned long int val;
5757
fdc90cb4
JJ
5758 /* Ignore indirect symbols. */
5759 if (h->dynindx == -1)
5760 return TRUE;
5761
5762 /* Ignore also local symbols and undefined symbols. */
5763 if (! (*s->bed->elf_hash_symbol) (h))
5764 {
5765 if (h->dynindx >= s->min_dynindx)
5766 h->dynindx = s->local_indx++;
5767 return TRUE;
5768 }
5769
5770 bucket = s->hashval[h->dynindx] % s->bucketcount;
5771 val = (s->hashval[h->dynindx] >> s->shift1)
5772 & ((s->maskbits >> s->shift1) - 1);
5773 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5774 s->bitmask[val]
5775 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5776 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5777 if (s->counts[bucket] == 1)
5778 /* Last element terminates the chain. */
5779 val |= 1;
5780 bfd_put_32 (s->output_bfd, val,
5781 s->contents + (s->indx[bucket] - s->symindx) * 4);
5782 --s->counts[bucket];
5783 h->dynindx = s->indx[bucket]++;
5784 return TRUE;
5785}
5786
5787/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5788
5789bfd_boolean
5790_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5791{
5792 return !(h->forced_local
5793 || h->root.type == bfd_link_hash_undefined
5794 || h->root.type == bfd_link_hash_undefweak
5795 || ((h->root.type == bfd_link_hash_defined
5796 || h->root.type == bfd_link_hash_defweak)
5797 && h->root.u.def.section->output_section == NULL));
5798}
5799
5a580b3a
AM
5800/* Array used to determine the number of hash table buckets to use
5801 based on the number of symbols there are. If there are fewer than
5802 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5803 fewer than 37 we use 17 buckets, and so forth. We never use more
5804 than 32771 buckets. */
5805
5806static const size_t elf_buckets[] =
5807{
5808 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5809 16411, 32771, 0
5810};
5811
5812/* Compute bucket count for hashing table. We do not use a static set
5813 of possible tables sizes anymore. Instead we determine for all
5814 possible reasonable sizes of the table the outcome (i.e., the
5815 number of collisions etc) and choose the best solution. The
5816 weighting functions are not too simple to allow the table to grow
5817 without bounds. Instead one of the weighting factors is the size.
5818 Therefore the result is always a good payoff between few collisions
5819 (= short chain lengths) and table size. */
5820static size_t
b20dd2ce 5821compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5822 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5823 unsigned long int nsyms,
5824 int gnu_hash)
5a580b3a 5825{
5a580b3a 5826 size_t best_size = 0;
5a580b3a 5827 unsigned long int i;
5a580b3a 5828
5a580b3a
AM
5829 /* We have a problem here. The following code to optimize the table
5830 size requires an integer type with more the 32 bits. If
5831 BFD_HOST_U_64_BIT is set we know about such a type. */
5832#ifdef BFD_HOST_U_64_BIT
5833 if (info->optimize)
5834 {
5a580b3a
AM
5835 size_t minsize;
5836 size_t maxsize;
5837 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5838 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5839 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5840 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5841 unsigned long int *counts;
d40f3da9 5842 bfd_size_type amt;
0883b6e0 5843 unsigned int no_improvement_count = 0;
5a580b3a
AM
5844
5845 /* Possible optimization parameters: if we have NSYMS symbols we say
5846 that the hashing table must at least have NSYMS/4 and at most
5847 2*NSYMS buckets. */
5848 minsize = nsyms / 4;
5849 if (minsize == 0)
5850 minsize = 1;
5851 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5852 if (gnu_hash)
5853 {
5854 if (minsize < 2)
5855 minsize = 2;
5856 if ((best_size & 31) == 0)
5857 ++best_size;
5858 }
5a580b3a
AM
5859
5860 /* Create array where we count the collisions in. We must use bfd_malloc
5861 since the size could be large. */
5862 amt = maxsize;
5863 amt *= sizeof (unsigned long int);
a50b1753 5864 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5865 if (counts == NULL)
fdc90cb4 5866 return 0;
5a580b3a
AM
5867
5868 /* Compute the "optimal" size for the hash table. The criteria is a
5869 minimal chain length. The minor criteria is (of course) the size
5870 of the table. */
5871 for (i = minsize; i < maxsize; ++i)
5872 {
5873 /* Walk through the array of hashcodes and count the collisions. */
5874 BFD_HOST_U_64_BIT max;
5875 unsigned long int j;
5876 unsigned long int fact;
5877
fdc90cb4
JJ
5878 if (gnu_hash && (i & 31) == 0)
5879 continue;
5880
5a580b3a
AM
5881 memset (counts, '\0', i * sizeof (unsigned long int));
5882
5883 /* Determine how often each hash bucket is used. */
5884 for (j = 0; j < nsyms; ++j)
5885 ++counts[hashcodes[j] % i];
5886
5887 /* For the weight function we need some information about the
5888 pagesize on the target. This is information need not be 100%
5889 accurate. Since this information is not available (so far) we
5890 define it here to a reasonable default value. If it is crucial
5891 to have a better value some day simply define this value. */
5892# ifndef BFD_TARGET_PAGESIZE
5893# define BFD_TARGET_PAGESIZE (4096)
5894# endif
5895
fdc90cb4
JJ
5896 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5897 and the chains. */
5898 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5899
5900# if 1
5901 /* Variant 1: optimize for short chains. We add the squares
5902 of all the chain lengths (which favors many small chain
5903 over a few long chains). */
5904 for (j = 0; j < i; ++j)
5905 max += counts[j] * counts[j];
5906
5907 /* This adds penalties for the overall size of the table. */
fdc90cb4 5908 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5909 max *= fact * fact;
5910# else
5911 /* Variant 2: Optimize a lot more for small table. Here we
5912 also add squares of the size but we also add penalties for
5913 empty slots (the +1 term). */
5914 for (j = 0; j < i; ++j)
5915 max += (1 + counts[j]) * (1 + counts[j]);
5916
5917 /* The overall size of the table is considered, but not as
5918 strong as in variant 1, where it is squared. */
fdc90cb4 5919 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5920 max *= fact;
5921# endif
5922
5923 /* Compare with current best results. */
5924 if (max < best_chlen)
5925 {
5926 best_chlen = max;
5927 best_size = i;
ca4be51c 5928 no_improvement_count = 0;
5a580b3a 5929 }
0883b6e0
NC
5930 /* PR 11843: Avoid futile long searches for the best bucket size
5931 when there are a large number of symbols. */
5932 else if (++no_improvement_count == 100)
5933 break;
5a580b3a
AM
5934 }
5935
5936 free (counts);
5937 }
5938 else
5939#endif /* defined (BFD_HOST_U_64_BIT) */
5940 {
5941 /* This is the fallback solution if no 64bit type is available or if we
5942 are not supposed to spend much time on optimizations. We select the
5943 bucket count using a fixed set of numbers. */
5944 for (i = 0; elf_buckets[i] != 0; i++)
5945 {
5946 best_size = elf_buckets[i];
fdc90cb4 5947 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5948 break;
5949 }
fdc90cb4
JJ
5950 if (gnu_hash && best_size < 2)
5951 best_size = 2;
5a580b3a
AM
5952 }
5953
5a580b3a
AM
5954 return best_size;
5955}
5956
d0bf826b
AM
5957/* Size any SHT_GROUP section for ld -r. */
5958
5959bfd_boolean
5960_bfd_elf_size_group_sections (struct bfd_link_info *info)
5961{
5962 bfd *ibfd;
57963c05 5963 asection *s;
d0bf826b 5964
c72f2fb2 5965 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 5966 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
5967 && (s = ibfd->sections) != NULL
5968 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
5969 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5970 return FALSE;
5971 return TRUE;
5972}
5973
04c3a755
NS
5974/* Set a default stack segment size. The value in INFO wins. If it
5975 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5976 undefined it is initialized. */
5977
5978bfd_boolean
5979bfd_elf_stack_segment_size (bfd *output_bfd,
5980 struct bfd_link_info *info,
5981 const char *legacy_symbol,
5982 bfd_vma default_size)
5983{
5984 struct elf_link_hash_entry *h = NULL;
5985
5986 /* Look for legacy symbol. */
5987 if (legacy_symbol)
5988 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5989 FALSE, FALSE, FALSE);
5990 if (h && (h->root.type == bfd_link_hash_defined
5991 || h->root.type == bfd_link_hash_defweak)
5992 && h->def_regular
5993 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5994 {
5995 /* The symbol has no type if specified on the command line. */
5996 h->type = STT_OBJECT;
5997 if (info->stacksize)
695344c0 5998 /* xgettext:c-format */
4eca0228
AM
5999 _bfd_error_handler (_("%B: stack size specified and %s set"),
6000 output_bfd, legacy_symbol);
04c3a755 6001 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6002 /* xgettext:c-format */
4eca0228
AM
6003 _bfd_error_handler (_("%B: %s not absolute"),
6004 output_bfd, legacy_symbol);
04c3a755
NS
6005 else
6006 info->stacksize = h->root.u.def.value;
6007 }
6008
6009 if (!info->stacksize)
6010 /* If the user didn't set a size, or explicitly inhibit the
6011 size, set it now. */
6012 info->stacksize = default_size;
6013
6014 /* Provide the legacy symbol, if it is referenced. */
6015 if (h && (h->root.type == bfd_link_hash_undefined
6016 || h->root.type == bfd_link_hash_undefweak))
6017 {
6018 struct bfd_link_hash_entry *bh = NULL;
6019
6020 if (!(_bfd_generic_link_add_one_symbol
6021 (info, output_bfd, legacy_symbol,
6022 BSF_GLOBAL, bfd_abs_section_ptr,
6023 info->stacksize >= 0 ? info->stacksize : 0,
6024 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6025 return FALSE;
6026
6027 h = (struct elf_link_hash_entry *) bh;
6028 h->def_regular = 1;
6029 h->type = STT_OBJECT;
6030 }
6031
6032 return TRUE;
6033}
6034
b531344c
MR
6035/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6036
6037struct elf_gc_sweep_symbol_info
6038{
6039 struct bfd_link_info *info;
6040 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6041 bfd_boolean);
6042};
6043
6044static bfd_boolean
6045elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6046{
6047 if (!h->mark
6048 && (((h->root.type == bfd_link_hash_defined
6049 || h->root.type == bfd_link_hash_defweak)
6050 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6051 && h->root.u.def.section->gc_mark))
6052 || h->root.type == bfd_link_hash_undefined
6053 || h->root.type == bfd_link_hash_undefweak))
6054 {
6055 struct elf_gc_sweep_symbol_info *inf;
6056
6057 inf = (struct elf_gc_sweep_symbol_info *) data;
6058 (*inf->hide_symbol) (inf->info, h, TRUE);
6059 h->def_regular = 0;
6060 h->ref_regular = 0;
6061 h->ref_regular_nonweak = 0;
6062 }
6063
6064 return TRUE;
6065}
6066
5a580b3a
AM
6067/* Set up the sizes and contents of the ELF dynamic sections. This is
6068 called by the ELF linker emulation before_allocation routine. We
6069 must set the sizes of the sections before the linker sets the
6070 addresses of the various sections. */
6071
6072bfd_boolean
6073bfd_elf_size_dynamic_sections (bfd *output_bfd,
6074 const char *soname,
6075 const char *rpath,
6076 const char *filter_shlib,
7ee314fa
AM
6077 const char *audit,
6078 const char *depaudit,
5a580b3a
AM
6079 const char * const *auxiliary_filters,
6080 struct bfd_link_info *info,
fd91d419 6081 asection **sinterpptr)
5a580b3a 6082{
5a580b3a
AM
6083 bfd *dynobj;
6084 const struct elf_backend_data *bed;
5a580b3a
AM
6085
6086 *sinterpptr = NULL;
6087
5a580b3a
AM
6088 if (!is_elf_hash_table (info->hash))
6089 return TRUE;
6090
5a580b3a
AM
6091 dynobj = elf_hash_table (info)->dynobj;
6092
9a2a56cc 6093 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6094 {
902e9fc7
MR
6095 struct bfd_elf_version_tree *verdefs;
6096 struct elf_info_failed asvinfo;
5a580b3a
AM
6097 struct bfd_elf_version_tree *t;
6098 struct bfd_elf_version_expr *d;
902e9fc7 6099 asection *s;
e6699019 6100 size_t soname_indx;
7ee314fa 6101
5a580b3a
AM
6102 /* If we are supposed to export all symbols into the dynamic symbol
6103 table (this is not the normal case), then do so. */
55255dae 6104 if (info->export_dynamic
0e1862bb 6105 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6106 {
3d13f3e9
AM
6107 struct elf_info_failed eif;
6108
6109 eif.info = info;
6110 eif.failed = FALSE;
5a580b3a
AM
6111 elf_link_hash_traverse (elf_hash_table (info),
6112 _bfd_elf_export_symbol,
6113 &eif);
6114 if (eif.failed)
6115 return FALSE;
6116 }
6117
e6699019
L
6118 if (soname != NULL)
6119 {
6120 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6121 soname, TRUE);
6122 if (soname_indx == (size_t) -1
6123 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6124 return FALSE;
6125 }
6126 else
6127 soname_indx = (size_t) -1;
6128
5a580b3a 6129 /* Make all global versions with definition. */
fd91d419 6130 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6131 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6132 if (!d->symver && d->literal)
5a580b3a
AM
6133 {
6134 const char *verstr, *name;
6135 size_t namelen, verlen, newlen;
93252b1c 6136 char *newname, *p, leading_char;
5a580b3a
AM
6137 struct elf_link_hash_entry *newh;
6138
93252b1c 6139 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6140 name = d->pattern;
93252b1c 6141 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6142 verstr = t->name;
6143 verlen = strlen (verstr);
6144 newlen = namelen + verlen + 3;
6145
a50b1753 6146 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6147 if (newname == NULL)
6148 return FALSE;
93252b1c
MF
6149 newname[0] = leading_char;
6150 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6151
6152 /* Check the hidden versioned definition. */
6153 p = newname + namelen;
6154 *p++ = ELF_VER_CHR;
6155 memcpy (p, verstr, verlen + 1);
6156 newh = elf_link_hash_lookup (elf_hash_table (info),
6157 newname, FALSE, FALSE,
6158 FALSE);
6159 if (newh == NULL
6160 || (newh->root.type != bfd_link_hash_defined
6161 && newh->root.type != bfd_link_hash_defweak))
6162 {
6163 /* Check the default versioned definition. */
6164 *p++ = ELF_VER_CHR;
6165 memcpy (p, verstr, verlen + 1);
6166 newh = elf_link_hash_lookup (elf_hash_table (info),
6167 newname, FALSE, FALSE,
6168 FALSE);
6169 }
6170 free (newname);
6171
6172 /* Mark this version if there is a definition and it is
6173 not defined in a shared object. */
6174 if (newh != NULL
f5385ebf 6175 && !newh->def_dynamic
5a580b3a
AM
6176 && (newh->root.type == bfd_link_hash_defined
6177 || newh->root.type == bfd_link_hash_defweak))
6178 d->symver = 1;
6179 }
6180
6181 /* Attach all the symbols to their version information. */
5a580b3a 6182 asvinfo.info = info;
5a580b3a
AM
6183 asvinfo.failed = FALSE;
6184
6185 elf_link_hash_traverse (elf_hash_table (info),
6186 _bfd_elf_link_assign_sym_version,
6187 &asvinfo);
6188 if (asvinfo.failed)
6189 return FALSE;
6190
6191 if (!info->allow_undefined_version)
6192 {
6193 /* Check if all global versions have a definition. */
3d13f3e9 6194 bfd_boolean all_defined = TRUE;
fd91d419 6195 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6196 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6197 if (d->literal && !d->symver && !d->script)
5a580b3a 6198 {
4eca0228 6199 _bfd_error_handler
5a580b3a
AM
6200 (_("%s: undefined version: %s"),
6201 d->pattern, t->name);
6202 all_defined = FALSE;
6203 }
6204
6205 if (!all_defined)
6206 {
6207 bfd_set_error (bfd_error_bad_value);
6208 return FALSE;
6209 }
6210 }
6211
902e9fc7
MR
6212 /* Set up the version definition section. */
6213 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6214 BFD_ASSERT (s != NULL);
5a580b3a 6215
902e9fc7
MR
6216 /* We may have created additional version definitions if we are
6217 just linking a regular application. */
6218 verdefs = info->version_info;
5a580b3a 6219
902e9fc7
MR
6220 /* Skip anonymous version tag. */
6221 if (verdefs != NULL && verdefs->vernum == 0)
6222 verdefs = verdefs->next;
5a580b3a 6223
902e9fc7
MR
6224 if (verdefs == NULL && !info->create_default_symver)
6225 s->flags |= SEC_EXCLUDE;
6226 else
5a580b3a 6227 {
902e9fc7
MR
6228 unsigned int cdefs;
6229 bfd_size_type size;
6230 bfd_byte *p;
6231 Elf_Internal_Verdef def;
6232 Elf_Internal_Verdaux defaux;
6233 struct bfd_link_hash_entry *bh;
6234 struct elf_link_hash_entry *h;
6235 const char *name;
5a580b3a 6236
902e9fc7
MR
6237 cdefs = 0;
6238 size = 0;
5a580b3a 6239
902e9fc7
MR
6240 /* Make space for the base version. */
6241 size += sizeof (Elf_External_Verdef);
6242 size += sizeof (Elf_External_Verdaux);
6243 ++cdefs;
6244
6245 /* Make space for the default version. */
6246 if (info->create_default_symver)
6247 {
6248 size += sizeof (Elf_External_Verdef);
6249 ++cdefs;
3e3b46e5
PB
6250 }
6251
5a580b3a
AM
6252 for (t = verdefs; t != NULL; t = t->next)
6253 {
6254 struct bfd_elf_version_deps *n;
6255
a6cc6b3b
RO
6256 /* Don't emit base version twice. */
6257 if (t->vernum == 0)
6258 continue;
6259
5a580b3a
AM
6260 size += sizeof (Elf_External_Verdef);
6261 size += sizeof (Elf_External_Verdaux);
6262 ++cdefs;
6263
6264 for (n = t->deps; n != NULL; n = n->next)
6265 size += sizeof (Elf_External_Verdaux);
6266 }
6267
eea6121a 6268 s->size = size;
a50b1753 6269 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6270 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6271 return FALSE;
6272
6273 /* Fill in the version definition section. */
6274
6275 p = s->contents;
6276
6277 def.vd_version = VER_DEF_CURRENT;
6278 def.vd_flags = VER_FLG_BASE;
6279 def.vd_ndx = 1;
6280 def.vd_cnt = 1;
3e3b46e5
PB
6281 if (info->create_default_symver)
6282 {
6283 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6284 def.vd_next = sizeof (Elf_External_Verdef);
6285 }
6286 else
6287 {
6288 def.vd_aux = sizeof (Elf_External_Verdef);
6289 def.vd_next = (sizeof (Elf_External_Verdef)
6290 + sizeof (Elf_External_Verdaux));
6291 }
5a580b3a 6292
ef53be89 6293 if (soname_indx != (size_t) -1)
5a580b3a
AM
6294 {
6295 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6296 soname_indx);
6297 def.vd_hash = bfd_elf_hash (soname);
6298 defaux.vda_name = soname_indx;
3e3b46e5 6299 name = soname;
5a580b3a
AM
6300 }
6301 else
6302 {
ef53be89 6303 size_t indx;
5a580b3a 6304
06084812 6305 name = lbasename (output_bfd->filename);
5a580b3a
AM
6306 def.vd_hash = bfd_elf_hash (name);
6307 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6308 name, FALSE);
ef53be89 6309 if (indx == (size_t) -1)
5a580b3a
AM
6310 return FALSE;
6311 defaux.vda_name = indx;
6312 }
6313 defaux.vda_next = 0;
6314
6315 _bfd_elf_swap_verdef_out (output_bfd, &def,
6316 (Elf_External_Verdef *) p);
6317 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6318 if (info->create_default_symver)
6319 {
6320 /* Add a symbol representing this version. */
6321 bh = NULL;
6322 if (! (_bfd_generic_link_add_one_symbol
6323 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6324 0, NULL, FALSE,
6325 get_elf_backend_data (dynobj)->collect, &bh)))
6326 return FALSE;
6327 h = (struct elf_link_hash_entry *) bh;
6328 h->non_elf = 0;
6329 h->def_regular = 1;
6330 h->type = STT_OBJECT;
6331 h->verinfo.vertree = NULL;
6332
6333 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6334 return FALSE;
6335
6336 /* Create a duplicate of the base version with the same
6337 aux block, but different flags. */
6338 def.vd_flags = 0;
6339 def.vd_ndx = 2;
6340 def.vd_aux = sizeof (Elf_External_Verdef);
6341 if (verdefs)
6342 def.vd_next = (sizeof (Elf_External_Verdef)
6343 + sizeof (Elf_External_Verdaux));
6344 else
6345 def.vd_next = 0;
6346 _bfd_elf_swap_verdef_out (output_bfd, &def,
6347 (Elf_External_Verdef *) p);
6348 p += sizeof (Elf_External_Verdef);
6349 }
5a580b3a
AM
6350 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6351 (Elf_External_Verdaux *) p);
6352 p += sizeof (Elf_External_Verdaux);
6353
6354 for (t = verdefs; t != NULL; t = t->next)
6355 {
6356 unsigned int cdeps;
6357 struct bfd_elf_version_deps *n;
5a580b3a 6358
a6cc6b3b
RO
6359 /* Don't emit the base version twice. */
6360 if (t->vernum == 0)
6361 continue;
6362
5a580b3a
AM
6363 cdeps = 0;
6364 for (n = t->deps; n != NULL; n = n->next)
6365 ++cdeps;
6366
6367 /* Add a symbol representing this version. */
6368 bh = NULL;
6369 if (! (_bfd_generic_link_add_one_symbol
6370 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6371 0, NULL, FALSE,
6372 get_elf_backend_data (dynobj)->collect, &bh)))
6373 return FALSE;
6374 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6375 h->non_elf = 0;
6376 h->def_regular = 1;
5a580b3a
AM
6377 h->type = STT_OBJECT;
6378 h->verinfo.vertree = t;
6379
c152c796 6380 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6381 return FALSE;
6382
6383 def.vd_version = VER_DEF_CURRENT;
6384 def.vd_flags = 0;
6385 if (t->globals.list == NULL
6386 && t->locals.list == NULL
6387 && ! t->used)
6388 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6389 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6390 def.vd_cnt = cdeps + 1;
6391 def.vd_hash = bfd_elf_hash (t->name);
6392 def.vd_aux = sizeof (Elf_External_Verdef);
6393 def.vd_next = 0;
a6cc6b3b
RO
6394
6395 /* If a basever node is next, it *must* be the last node in
6396 the chain, otherwise Verdef construction breaks. */
6397 if (t->next != NULL && t->next->vernum == 0)
6398 BFD_ASSERT (t->next->next == NULL);
6399
6400 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6401 def.vd_next = (sizeof (Elf_External_Verdef)
6402 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6403
6404 _bfd_elf_swap_verdef_out (output_bfd, &def,
6405 (Elf_External_Verdef *) p);
6406 p += sizeof (Elf_External_Verdef);
6407
6408 defaux.vda_name = h->dynstr_index;
6409 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6410 h->dynstr_index);
6411 defaux.vda_next = 0;
6412 if (t->deps != NULL)
6413 defaux.vda_next = sizeof (Elf_External_Verdaux);
6414 t->name_indx = defaux.vda_name;
6415
6416 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6417 (Elf_External_Verdaux *) p);
6418 p += sizeof (Elf_External_Verdaux);
6419
6420 for (n = t->deps; n != NULL; n = n->next)
6421 {
6422 if (n->version_needed == NULL)
6423 {
6424 /* This can happen if there was an error in the
6425 version script. */
6426 defaux.vda_name = 0;
6427 }
6428 else
6429 {
6430 defaux.vda_name = n->version_needed->name_indx;
6431 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6432 defaux.vda_name);
6433 }
6434 if (n->next == NULL)
6435 defaux.vda_next = 0;
6436 else
6437 defaux.vda_next = sizeof (Elf_External_Verdaux);
6438
6439 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6440 (Elf_External_Verdaux *) p);
6441 p += sizeof (Elf_External_Verdaux);
6442 }
6443 }
6444
5a580b3a
AM
6445 elf_tdata (output_bfd)->cverdefs = cdefs;
6446 }
902e9fc7
MR
6447 }
6448
6449 bed = get_elf_backend_data (output_bfd);
6450
6451 if (info->gc_sections && bed->can_gc_sections)
6452 {
6453 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6454
6455 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6456 dynamic symbol table. */
902e9fc7
MR
6457 sweep_info.info = info;
6458 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6459 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6460 &sweep_info);
3d13f3e9
AM
6461 }
6462
6463 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6464 {
6465 asection *s;
6466 struct elf_find_verdep_info sinfo;
6467
6468 /* Work out the size of the version reference section. */
6469
6470 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6471 BFD_ASSERT (s != NULL);
902e9fc7 6472
3d13f3e9
AM
6473 sinfo.info = info;
6474 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6475 if (sinfo.vers == 0)
6476 sinfo.vers = 1;
6477 sinfo.failed = FALSE;
6478
6479 elf_link_hash_traverse (elf_hash_table (info),
6480 _bfd_elf_link_find_version_dependencies,
6481 &sinfo);
6482 if (sinfo.failed)
6483 return FALSE;
6484
6485 if (elf_tdata (output_bfd)->verref == NULL)
6486 s->flags |= SEC_EXCLUDE;
6487 else
6488 {
6489 Elf_Internal_Verneed *vn;
6490 unsigned int size;
6491 unsigned int crefs;
6492 bfd_byte *p;
6493
6494 /* Build the version dependency section. */
6495 size = 0;
6496 crefs = 0;
6497 for (vn = elf_tdata (output_bfd)->verref;
6498 vn != NULL;
6499 vn = vn->vn_nextref)
6500 {
6501 Elf_Internal_Vernaux *a;
6502
6503 size += sizeof (Elf_External_Verneed);
6504 ++crefs;
6505 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6506 size += sizeof (Elf_External_Vernaux);
6507 }
6508
6509 s->size = size;
6510 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6511 if (s->contents == NULL)
6512 return FALSE;
6513
6514 p = s->contents;
6515 for (vn = elf_tdata (output_bfd)->verref;
6516 vn != NULL;
6517 vn = vn->vn_nextref)
6518 {
6519 unsigned int caux;
6520 Elf_Internal_Vernaux *a;
6521 size_t indx;
6522
6523 caux = 0;
6524 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6525 ++caux;
6526
6527 vn->vn_version = VER_NEED_CURRENT;
6528 vn->vn_cnt = caux;
6529 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6530 elf_dt_name (vn->vn_bfd) != NULL
6531 ? elf_dt_name (vn->vn_bfd)
6532 : lbasename (vn->vn_bfd->filename),
6533 FALSE);
6534 if (indx == (size_t) -1)
6535 return FALSE;
6536 vn->vn_file = indx;
6537 vn->vn_aux = sizeof (Elf_External_Verneed);
6538 if (vn->vn_nextref == NULL)
6539 vn->vn_next = 0;
6540 else
6541 vn->vn_next = (sizeof (Elf_External_Verneed)
6542 + caux * sizeof (Elf_External_Vernaux));
6543
6544 _bfd_elf_swap_verneed_out (output_bfd, vn,
6545 (Elf_External_Verneed *) p);
6546 p += sizeof (Elf_External_Verneed);
6547
6548 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6549 {
6550 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6551 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6552 a->vna_nodename, FALSE);
6553 if (indx == (size_t) -1)
6554 return FALSE;
6555 a->vna_name = indx;
6556 if (a->vna_nextptr == NULL)
6557 a->vna_next = 0;
6558 else
6559 a->vna_next = sizeof (Elf_External_Vernaux);
6560
6561 _bfd_elf_swap_vernaux_out (output_bfd, a,
6562 (Elf_External_Vernaux *) p);
6563 p += sizeof (Elf_External_Vernaux);
6564 }
6565 }
6566
6567 elf_tdata (output_bfd)->cverrefs = crefs;
6568 }
902e9fc7
MR
6569 }
6570
6571 /* Any syms created from now on start with -1 in
6572 got.refcount/offset and plt.refcount/offset. */
6573 elf_hash_table (info)->init_got_refcount
6574 = elf_hash_table (info)->init_got_offset;
6575 elf_hash_table (info)->init_plt_refcount
6576 = elf_hash_table (info)->init_plt_offset;
6577
6578 if (bfd_link_relocatable (info)
6579 && !_bfd_elf_size_group_sections (info))
6580 return FALSE;
6581
6582 /* The backend may have to create some sections regardless of whether
6583 we're dynamic or not. */
6584 if (bed->elf_backend_always_size_sections
6585 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6586 return FALSE;
6587
6588 /* Determine any GNU_STACK segment requirements, after the backend
6589 has had a chance to set a default segment size. */
6590 if (info->execstack)
6591 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6592 else if (info->noexecstack)
6593 elf_stack_flags (output_bfd) = PF_R | PF_W;
6594 else
6595 {
6596 bfd *inputobj;
6597 asection *notesec = NULL;
6598 int exec = 0;
6599
6600 for (inputobj = info->input_bfds;
6601 inputobj;
6602 inputobj = inputobj->link.next)
6603 {
6604 asection *s;
6605
6606 if (inputobj->flags
6607 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6608 continue;
57963c05
AM
6609 s = inputobj->sections;
6610 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6611 continue;
6612
902e9fc7
MR
6613 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6614 if (s)
6615 {
6616 if (s->flags & SEC_CODE)
6617 exec = PF_X;
6618 notesec = s;
6619 }
6620 else if (bed->default_execstack)
6621 exec = PF_X;
6622 }
6623 if (notesec || info->stacksize > 0)
6624 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6625 if (notesec && exec && bfd_link_relocatable (info)
6626 && notesec->output_section != bfd_abs_section_ptr)
6627 notesec->output_section->flags |= SEC_CODE;
6628 }
6629
6630 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6631 {
6632 struct elf_info_failed eif;
6633 struct elf_link_hash_entry *h;
6634 asection *dynstr;
6635 asection *s;
6636
6637 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6638 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6639
902e9fc7
MR
6640 if (info->symbolic)
6641 {
6642 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6643 return FALSE;
6644 info->flags |= DF_SYMBOLIC;
6645 }
6646
6647 if (rpath != NULL)
6648 {
6649 size_t indx;
6650 bfd_vma tag;
6651
6652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6653 TRUE);
6654 if (indx == (size_t) -1)
6655 return FALSE;
6656
6657 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6658 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6659 return FALSE;
6660 }
6661
6662 if (filter_shlib != NULL)
6663 {
6664 size_t indx;
6665
6666 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6667 filter_shlib, TRUE);
6668 if (indx == (size_t) -1
6669 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6670 return FALSE;
6671 }
6672
6673 if (auxiliary_filters != NULL)
6674 {
6675 const char * const *p;
6676
6677 for (p = auxiliary_filters; *p != NULL; p++)
6678 {
6679 size_t indx;
6680
6681 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6682 *p, TRUE);
6683 if (indx == (size_t) -1
6684 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6685 return FALSE;
6686 }
6687 }
6688
6689 if (audit != NULL)
6690 {
6691 size_t indx;
6692
6693 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6694 TRUE);
6695 if (indx == (size_t) -1
6696 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6697 return FALSE;
6698 }
6699
6700 if (depaudit != NULL)
6701 {
6702 size_t indx;
6703
6704 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6705 TRUE);
6706 if (indx == (size_t) -1
6707 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6708 return FALSE;
6709 }
6710
6711 eif.info = info;
6712 eif.failed = FALSE;
6713
6714 /* Find all symbols which were defined in a dynamic object and make
6715 the backend pick a reasonable value for them. */
6716 elf_link_hash_traverse (elf_hash_table (info),
6717 _bfd_elf_adjust_dynamic_symbol,
6718 &eif);
6719 if (eif.failed)
6720 return FALSE;
6721
6722 /* Add some entries to the .dynamic section. We fill in some of the
6723 values later, in bfd_elf_final_link, but we must add the entries
6724 now so that we know the final size of the .dynamic section. */
6725
6726 /* If there are initialization and/or finalization functions to
6727 call then add the corresponding DT_INIT/DT_FINI entries. */
6728 h = (info->init_function
6729 ? elf_link_hash_lookup (elf_hash_table (info),
6730 info->init_function, FALSE,
6731 FALSE, FALSE)
6732 : NULL);
6733 if (h != NULL
6734 && (h->ref_regular
6735 || h->def_regular))
6736 {
6737 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6738 return FALSE;
6739 }
6740 h = (info->fini_function
6741 ? elf_link_hash_lookup (elf_hash_table (info),
6742 info->fini_function, FALSE,
6743 FALSE, FALSE)
6744 : NULL);
6745 if (h != NULL
6746 && (h->ref_regular
6747 || h->def_regular))
6748 {
6749 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6750 return FALSE;
6751 }
6752
6753 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6754 if (s != NULL && s->linker_has_input)
6755 {
6756 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6757 if (! bfd_link_executable (info))
6758 {
6759 bfd *sub;
6760 asection *o;
6761
57963c05
AM
6762 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6763 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6764 && (o = sub->sections) != NULL
6765 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6766 for (o = sub->sections; o != NULL; o = o->next)
6767 if (elf_section_data (o)->this_hdr.sh_type
6768 == SHT_PREINIT_ARRAY)
6769 {
6770 _bfd_error_handler
6771 (_("%B: .preinit_array section is not allowed in DSO"),
6772 sub);
6773 break;
6774 }
6775
6776 bfd_set_error (bfd_error_nonrepresentable_section);
6777 return FALSE;
6778 }
6779
6780 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6781 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6782 return FALSE;
6783 }
6784 s = bfd_get_section_by_name (output_bfd, ".init_array");
6785 if (s != NULL && s->linker_has_input)
6786 {
6787 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6788 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6789 return FALSE;
6790 }
6791 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6792 if (s != NULL && s->linker_has_input)
6793 {
6794 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6795 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6796 return FALSE;
6797 }
6798
6799 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6800 /* If .dynstr is excluded from the link, we don't want any of
6801 these tags. Strictly, we should be checking each section
6802 individually; This quick check covers for the case where
6803 someone does a /DISCARD/ : { *(*) }. */
6804 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6805 {
6806 bfd_size_type strsize;
6807
6808 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6809 if ((info->emit_hash
6810 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6811 || (info->emit_gnu_hash
6812 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6813 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6814 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6815 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6816 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6817 bed->s->sizeof_sym))
6818 return FALSE;
6819 }
6820 }
6821
6822 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6823 return FALSE;
6824
6825 /* The backend must work out the sizes of all the other dynamic
6826 sections. */
6827 if (dynobj != NULL
6828 && bed->elf_backend_size_dynamic_sections != NULL
6829 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6830 return FALSE;
6831
6832 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6833 {
6834 unsigned long section_sym_count;
6835
6836 if (elf_tdata (output_bfd)->cverdefs)
6837 {
6838 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6839
6840 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6841 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6842 return FALSE;
6843 }
6844
6845 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6846 {
6847 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6848 return FALSE;
6849 }
6850 else if (info->flags & DF_BIND_NOW)
6851 {
6852 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6853 return FALSE;
6854 }
6855
6856 if (info->flags_1)
6857 {
6858 if (bfd_link_executable (info))
6859 info->flags_1 &= ~ (DF_1_INITFIRST
6860 | DF_1_NODELETE
6861 | DF_1_NOOPEN);
6862 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6863 return FALSE;
6864 }
6865
6866 if (elf_tdata (output_bfd)->cverrefs)
6867 {
6868 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6869
6870 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6871 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6872 return FALSE;
6873 }
5a580b3a 6874
8423293d
AM
6875 if ((elf_tdata (output_bfd)->cverrefs == 0
6876 && elf_tdata (output_bfd)->cverdefs == 0)
6877 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
3d13f3e9 6878 &section_sym_count) <= 1)
8423293d 6879 {
902e9fc7
MR
6880 asection *s;
6881
3d4d4302 6882 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6883 s->flags |= SEC_EXCLUDE;
6884 }
6885 }
6886 return TRUE;
6887}
6888
74541ad4
AM
6889/* Find the first non-excluded output section. We'll use its
6890 section symbol for some emitted relocs. */
6891void
6892_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6893{
6894 asection *s;
6895
6896 for (s = output_bfd->sections; s != NULL; s = s->next)
6897 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6898 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6899 {
6900 elf_hash_table (info)->text_index_section = s;
6901 break;
6902 }
6903}
6904
6905/* Find two non-excluded output sections, one for code, one for data.
6906 We'll use their section symbols for some emitted relocs. */
6907void
6908_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6909{
6910 asection *s;
6911
266b05cf
DJ
6912 /* Data first, since setting text_index_section changes
6913 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6914 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6915 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6916 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6917 {
266b05cf 6918 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6919 break;
6920 }
6921
6922 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6923 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6924 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6925 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6926 {
266b05cf 6927 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6928 break;
6929 }
6930
6931 if (elf_hash_table (info)->text_index_section == NULL)
6932 elf_hash_table (info)->text_index_section
6933 = elf_hash_table (info)->data_index_section;
6934}
6935
8423293d
AM
6936bfd_boolean
6937bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6938{
74541ad4 6939 const struct elf_backend_data *bed;
23ec1e32 6940 unsigned long section_sym_count;
96d01d93 6941 bfd_size_type dynsymcount = 0;
74541ad4 6942
8423293d
AM
6943 if (!is_elf_hash_table (info->hash))
6944 return TRUE;
6945
74541ad4
AM
6946 bed = get_elf_backend_data (output_bfd);
6947 (*bed->elf_backend_init_index_section) (output_bfd, info);
6948
23ec1e32
MR
6949 /* Assign dynsym indices. In a shared library we generate a section
6950 symbol for each output section, which come first. Next come all
6951 of the back-end allocated local dynamic syms, followed by the rest
6952 of the global symbols.
6953
6954 This is usually not needed for static binaries, however backends
6955 can request to always do it, e.g. the MIPS backend uses dynamic
6956 symbol counts to lay out GOT, which will be produced in the
6957 presence of GOT relocations even in static binaries (holding fixed
6958 data in that case, to satisfy those relocations). */
6959
6960 if (elf_hash_table (info)->dynamic_sections_created
6961 || bed->always_renumber_dynsyms)
6962 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6963 &section_sym_count);
6964
8423293d
AM
6965 if (elf_hash_table (info)->dynamic_sections_created)
6966 {
6967 bfd *dynobj;
8423293d 6968 asection *s;
8423293d
AM
6969 unsigned int dtagcount;
6970
6971 dynobj = elf_hash_table (info)->dynobj;
6972
5a580b3a 6973 /* Work out the size of the symbol version section. */
3d4d4302 6974 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6975 BFD_ASSERT (s != NULL);
d5486c43 6976 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6977 {
eea6121a 6978 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6979 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6980 if (s->contents == NULL)
6981 return FALSE;
6982
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6984 return FALSE;
6985 }
6986
6987 /* Set the size of the .dynsym and .hash sections. We counted
6988 the number of dynamic symbols in elf_link_add_object_symbols.
6989 We will build the contents of .dynsym and .hash when we build
6990 the final symbol table, because until then we do not know the
6991 correct value to give the symbols. We built the .dynstr
6992 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6993 s = elf_hash_table (info)->dynsym;
5a580b3a 6994 BFD_ASSERT (s != NULL);
eea6121a 6995 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6996
d5486c43
L
6997 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6998 if (s->contents == NULL)
6999 return FALSE;
5a580b3a 7000
d5486c43
L
7001 /* The first entry in .dynsym is a dummy symbol. Clear all the
7002 section syms, in case we don't output them all. */
7003 ++section_sym_count;
7004 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7005
fdc90cb4
JJ
7006 elf_hash_table (info)->bucketcount = 0;
7007
5a580b3a
AM
7008 /* Compute the size of the hashing table. As a side effect this
7009 computes the hash values for all the names we export. */
fdc90cb4
JJ
7010 if (info->emit_hash)
7011 {
7012 unsigned long int *hashcodes;
14b1c01e 7013 struct hash_codes_info hashinf;
fdc90cb4
JJ
7014 bfd_size_type amt;
7015 unsigned long int nsyms;
7016 size_t bucketcount;
7017 size_t hash_entry_size;
7018
7019 /* Compute the hash values for all exported symbols. At the same
7020 time store the values in an array so that we could use them for
7021 optimizations. */
7022 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7023 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7024 if (hashcodes == NULL)
7025 return FALSE;
14b1c01e
AM
7026 hashinf.hashcodes = hashcodes;
7027 hashinf.error = FALSE;
5a580b3a 7028
fdc90cb4
JJ
7029 /* Put all hash values in HASHCODES. */
7030 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7031 elf_collect_hash_codes, &hashinf);
7032 if (hashinf.error)
4dd07732
AM
7033 {
7034 free (hashcodes);
7035 return FALSE;
7036 }
5a580b3a 7037
14b1c01e 7038 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7039 bucketcount
7040 = compute_bucket_count (info, hashcodes, nsyms, 0);
7041 free (hashcodes);
7042
4b48e2f6 7043 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7044 return FALSE;
5a580b3a 7045
fdc90cb4
JJ
7046 elf_hash_table (info)->bucketcount = bucketcount;
7047
3d4d4302 7048 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7049 BFD_ASSERT (s != NULL);
7050 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7051 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7052 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7053 if (s->contents == NULL)
7054 return FALSE;
7055
7056 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7057 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7058 s->contents + hash_entry_size);
7059 }
7060
7061 if (info->emit_gnu_hash)
7062 {
7063 size_t i, cnt;
7064 unsigned char *contents;
7065 struct collect_gnu_hash_codes cinfo;
7066 bfd_size_type amt;
7067 size_t bucketcount;
7068
7069 memset (&cinfo, 0, sizeof (cinfo));
7070
7071 /* Compute the hash values for all exported symbols. At the same
7072 time store the values in an array so that we could use them for
7073 optimizations. */
7074 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7075 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7076 if (cinfo.hashcodes == NULL)
7077 return FALSE;
7078
7079 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7080 cinfo.min_dynindx = -1;
7081 cinfo.output_bfd = output_bfd;
7082 cinfo.bed = bed;
7083
7084 /* Put all hash values in HASHCODES. */
7085 elf_link_hash_traverse (elf_hash_table (info),
7086 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7087 if (cinfo.error)
4dd07732
AM
7088 {
7089 free (cinfo.hashcodes);
7090 return FALSE;
7091 }
fdc90cb4
JJ
7092
7093 bucketcount
7094 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7095
7096 if (bucketcount == 0)
7097 {
7098 free (cinfo.hashcodes);
7099 return FALSE;
7100 }
7101
3d4d4302 7102 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7103 BFD_ASSERT (s != NULL);
7104
7105 if (cinfo.nsyms == 0)
7106 {
7107 /* Empty .gnu.hash section is special. */
7108 BFD_ASSERT (cinfo.min_dynindx == -1);
7109 free (cinfo.hashcodes);
7110 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7111 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7112 if (contents == NULL)
7113 return FALSE;
7114 s->contents = contents;
7115 /* 1 empty bucket. */
7116 bfd_put_32 (output_bfd, 1, contents);
7117 /* SYMIDX above the special symbol 0. */
7118 bfd_put_32 (output_bfd, 1, contents + 4);
7119 /* Just one word for bitmask. */
7120 bfd_put_32 (output_bfd, 1, contents + 8);
7121 /* Only hash fn bloom filter. */
7122 bfd_put_32 (output_bfd, 0, contents + 12);
7123 /* No hashes are valid - empty bitmask. */
7124 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7125 /* No hashes in the only bucket. */
7126 bfd_put_32 (output_bfd, 0,
7127 contents + 16 + bed->s->arch_size / 8);
7128 }
7129 else
7130 {
9e6619e2 7131 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7132 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7133
9e6619e2
AM
7134 x = cinfo.nsyms;
7135 maskbitslog2 = 1;
7136 while ((x >>= 1) != 0)
7137 ++maskbitslog2;
fdc90cb4
JJ
7138 if (maskbitslog2 < 3)
7139 maskbitslog2 = 5;
7140 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7141 maskbitslog2 = maskbitslog2 + 3;
7142 else
7143 maskbitslog2 = maskbitslog2 + 2;
7144 if (bed->s->arch_size == 64)
7145 {
7146 if (maskbitslog2 == 5)
7147 maskbitslog2 = 6;
7148 cinfo.shift1 = 6;
7149 }
7150 else
7151 cinfo.shift1 = 5;
7152 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7153 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7154 cinfo.maskbits = 1 << maskbitslog2;
7155 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7156 amt = bucketcount * sizeof (unsigned long int) * 2;
7157 amt += maskwords * sizeof (bfd_vma);
a50b1753 7158 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7159 if (cinfo.bitmask == NULL)
7160 {
7161 free (cinfo.hashcodes);
7162 return FALSE;
7163 }
7164
a50b1753 7165 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7166 cinfo.indx = cinfo.counts + bucketcount;
7167 cinfo.symindx = dynsymcount - cinfo.nsyms;
7168 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7169
7170 /* Determine how often each hash bucket is used. */
7171 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7172 for (i = 0; i < cinfo.nsyms; ++i)
7173 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7174
7175 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7176 if (cinfo.counts[i] != 0)
7177 {
7178 cinfo.indx[i] = cnt;
7179 cnt += cinfo.counts[i];
7180 }
7181 BFD_ASSERT (cnt == dynsymcount);
7182 cinfo.bucketcount = bucketcount;
7183 cinfo.local_indx = cinfo.min_dynindx;
7184
7185 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7186 s->size += cinfo.maskbits / 8;
a50b1753 7187 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7188 if (contents == NULL)
7189 {
7190 free (cinfo.bitmask);
7191 free (cinfo.hashcodes);
7192 return FALSE;
7193 }
7194
7195 s->contents = contents;
7196 bfd_put_32 (output_bfd, bucketcount, contents);
7197 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7198 bfd_put_32 (output_bfd, maskwords, contents + 8);
7199 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7200 contents += 16 + cinfo.maskbits / 8;
7201
7202 for (i = 0; i < bucketcount; ++i)
7203 {
7204 if (cinfo.counts[i] == 0)
7205 bfd_put_32 (output_bfd, 0, contents);
7206 else
7207 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7208 contents += 4;
7209 }
7210
7211 cinfo.contents = contents;
7212
7213 /* Renumber dynamic symbols, populate .gnu.hash section. */
7214 elf_link_hash_traverse (elf_hash_table (info),
7215 elf_renumber_gnu_hash_syms, &cinfo);
7216
7217 contents = s->contents + 16;
7218 for (i = 0; i < maskwords; ++i)
7219 {
7220 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7221 contents);
7222 contents += bed->s->arch_size / 8;
7223 }
7224
7225 free (cinfo.bitmask);
7226 free (cinfo.hashcodes);
7227 }
7228 }
5a580b3a 7229
3d4d4302 7230 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7231 BFD_ASSERT (s != NULL);
7232
4ad4eba5 7233 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7234
eea6121a 7235 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7236
7237 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7238 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7239 return FALSE;
7240 }
7241
7242 return TRUE;
7243}
4d269e42 7244\f
4d269e42
AM
7245/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7246
7247static void
7248merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7249 asection *sec)
7250{
dbaa2011
AM
7251 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7252 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7253}
7254
7255/* Finish SHF_MERGE section merging. */
7256
7257bfd_boolean
630993ec 7258_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7259{
7260 bfd *ibfd;
7261 asection *sec;
7262
7263 if (!is_elf_hash_table (info->hash))
7264 return FALSE;
7265
c72f2fb2 7266 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7267 if ((ibfd->flags & DYNAMIC) == 0
7268 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7269 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7270 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7271 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7272 if ((sec->flags & SEC_MERGE) != 0
7273 && !bfd_is_abs_section (sec->output_section))
7274 {
7275 struct bfd_elf_section_data *secdata;
7276
7277 secdata = elf_section_data (sec);
630993ec 7278 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7279 &elf_hash_table (info)->merge_info,
7280 sec, &secdata->sec_info))
7281 return FALSE;
7282 else if (secdata->sec_info)
dbaa2011 7283 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7284 }
7285
7286 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7287 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7288 merge_sections_remove_hook);
7289 return TRUE;
7290}
7291
7292/* Create an entry in an ELF linker hash table. */
7293
7294struct bfd_hash_entry *
7295_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7296 struct bfd_hash_table *table,
7297 const char *string)
7298{
7299 /* Allocate the structure if it has not already been allocated by a
7300 subclass. */
7301 if (entry == NULL)
7302 {
a50b1753 7303 entry = (struct bfd_hash_entry *)
ca4be51c 7304 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7305 if (entry == NULL)
7306 return entry;
7307 }
7308
7309 /* Call the allocation method of the superclass. */
7310 entry = _bfd_link_hash_newfunc (entry, table, string);
7311 if (entry != NULL)
7312 {
7313 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7314 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7315
7316 /* Set local fields. */
7317 ret->indx = -1;
7318 ret->dynindx = -1;
7319 ret->got = htab->init_got_refcount;
7320 ret->plt = htab->init_plt_refcount;
7321 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7322 - offsetof (struct elf_link_hash_entry, size)));
7323 /* Assume that we have been called by a non-ELF symbol reader.
7324 This flag is then reset by the code which reads an ELF input
7325 file. This ensures that a symbol created by a non-ELF symbol
7326 reader will have the flag set correctly. */
7327 ret->non_elf = 1;
7328 }
7329
7330 return entry;
7331}
7332
7333/* Copy data from an indirect symbol to its direct symbol, hiding the
7334 old indirect symbol. Also used for copying flags to a weakdef. */
7335
7336void
7337_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7338 struct elf_link_hash_entry *dir,
7339 struct elf_link_hash_entry *ind)
7340{
7341 struct elf_link_hash_table *htab;
7342
7343 /* Copy down any references that we may have already seen to the
e81830c5 7344 symbol which just became indirect. */
4d269e42 7345
422f1182 7346 if (dir->versioned != versioned_hidden)
e81830c5
AM
7347 dir->ref_dynamic |= ind->ref_dynamic;
7348 dir->ref_regular |= ind->ref_regular;
7349 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7350 dir->non_got_ref |= ind->non_got_ref;
7351 dir->needs_plt |= ind->needs_plt;
7352 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7353
7354 if (ind->root.type != bfd_link_hash_indirect)
7355 return;
7356
7357 /* Copy over the global and procedure linkage table refcount entries.
7358 These may have been already set up by a check_relocs routine. */
7359 htab = elf_hash_table (info);
7360 if (ind->got.refcount > htab->init_got_refcount.refcount)
7361 {
7362 if (dir->got.refcount < 0)
7363 dir->got.refcount = 0;
7364 dir->got.refcount += ind->got.refcount;
7365 ind->got.refcount = htab->init_got_refcount.refcount;
7366 }
7367
7368 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7369 {
7370 if (dir->plt.refcount < 0)
7371 dir->plt.refcount = 0;
7372 dir->plt.refcount += ind->plt.refcount;
7373 ind->plt.refcount = htab->init_plt_refcount.refcount;
7374 }
7375
7376 if (ind->dynindx != -1)
7377 {
7378 if (dir->dynindx != -1)
7379 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7380 dir->dynindx = ind->dynindx;
7381 dir->dynstr_index = ind->dynstr_index;
7382 ind->dynindx = -1;
7383 ind->dynstr_index = 0;
7384 }
7385}
7386
7387void
7388_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7389 struct elf_link_hash_entry *h,
7390 bfd_boolean force_local)
7391{
3aa14d16
L
7392 /* STT_GNU_IFUNC symbol must go through PLT. */
7393 if (h->type != STT_GNU_IFUNC)
7394 {
7395 h->plt = elf_hash_table (info)->init_plt_offset;
7396 h->needs_plt = 0;
7397 }
4d269e42
AM
7398 if (force_local)
7399 {
7400 h->forced_local = 1;
7401 if (h->dynindx != -1)
7402 {
4d269e42
AM
7403 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7404 h->dynstr_index);
641338d8
AM
7405 h->dynindx = -1;
7406 h->dynstr_index = 0;
4d269e42
AM
7407 }
7408 }
7409}
7410
7bf52ea2
AM
7411/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7412 caller. */
4d269e42
AM
7413
7414bfd_boolean
7415_bfd_elf_link_hash_table_init
7416 (struct elf_link_hash_table *table,
7417 bfd *abfd,
7418 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7419 struct bfd_hash_table *,
7420 const char *),
4dfe6ac6
NC
7421 unsigned int entsize,
7422 enum elf_target_id target_id)
4d269e42
AM
7423{
7424 bfd_boolean ret;
7425 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7426
4d269e42
AM
7427 table->init_got_refcount.refcount = can_refcount - 1;
7428 table->init_plt_refcount.refcount = can_refcount - 1;
7429 table->init_got_offset.offset = -(bfd_vma) 1;
7430 table->init_plt_offset.offset = -(bfd_vma) 1;
7431 /* The first dynamic symbol is a dummy. */
7432 table->dynsymcount = 1;
7433
7434 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7435
4d269e42 7436 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7437 table->hash_table_id = target_id;
4d269e42
AM
7438
7439 return ret;
7440}
7441
7442/* Create an ELF linker hash table. */
7443
7444struct bfd_link_hash_table *
7445_bfd_elf_link_hash_table_create (bfd *abfd)
7446{
7447 struct elf_link_hash_table *ret;
7448 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7449
7bf52ea2 7450 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7451 if (ret == NULL)
7452 return NULL;
7453
7454 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7455 sizeof (struct elf_link_hash_entry),
7456 GENERIC_ELF_DATA))
4d269e42
AM
7457 {
7458 free (ret);
7459 return NULL;
7460 }
d495ab0d 7461 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7462
7463 return &ret->root;
7464}
7465
9f7c3e5e
AM
7466/* Destroy an ELF linker hash table. */
7467
7468void
d495ab0d 7469_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7470{
d495ab0d
AM
7471 struct elf_link_hash_table *htab;
7472
7473 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7474 if (htab->dynstr != NULL)
7475 _bfd_elf_strtab_free (htab->dynstr);
7476 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7477 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7478}
7479
4d269e42
AM
7480/* This is a hook for the ELF emulation code in the generic linker to
7481 tell the backend linker what file name to use for the DT_NEEDED
7482 entry for a dynamic object. */
7483
7484void
7485bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7486{
7487 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7488 && bfd_get_format (abfd) == bfd_object)
7489 elf_dt_name (abfd) = name;
7490}
7491
7492int
7493bfd_elf_get_dyn_lib_class (bfd *abfd)
7494{
7495 int lib_class;
7496 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7497 && bfd_get_format (abfd) == bfd_object)
7498 lib_class = elf_dyn_lib_class (abfd);
7499 else
7500 lib_class = 0;
7501 return lib_class;
7502}
7503
7504void
7505bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7506{
7507 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7508 && bfd_get_format (abfd) == bfd_object)
7509 elf_dyn_lib_class (abfd) = lib_class;
7510}
7511
7512/* Get the list of DT_NEEDED entries for a link. This is a hook for
7513 the linker ELF emulation code. */
7514
7515struct bfd_link_needed_list *
7516bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7517 struct bfd_link_info *info)
7518{
7519 if (! is_elf_hash_table (info->hash))
7520 return NULL;
7521 return elf_hash_table (info)->needed;
7522}
7523
7524/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7525 hook for the linker ELF emulation code. */
7526
7527struct bfd_link_needed_list *
7528bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7529 struct bfd_link_info *info)
7530{
7531 if (! is_elf_hash_table (info->hash))
7532 return NULL;
7533 return elf_hash_table (info)->runpath;
7534}
7535
7536/* Get the name actually used for a dynamic object for a link. This
7537 is the SONAME entry if there is one. Otherwise, it is the string
7538 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7539
7540const char *
7541bfd_elf_get_dt_soname (bfd *abfd)
7542{
7543 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7544 && bfd_get_format (abfd) == bfd_object)
7545 return elf_dt_name (abfd);
7546 return NULL;
7547}
7548
7549/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7550 the ELF linker emulation code. */
7551
7552bfd_boolean
7553bfd_elf_get_bfd_needed_list (bfd *abfd,
7554 struct bfd_link_needed_list **pneeded)
7555{
7556 asection *s;
7557 bfd_byte *dynbuf = NULL;
cb33740c 7558 unsigned int elfsec;
4d269e42
AM
7559 unsigned long shlink;
7560 bfd_byte *extdyn, *extdynend;
7561 size_t extdynsize;
7562 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7563
7564 *pneeded = NULL;
7565
7566 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7567 || bfd_get_format (abfd) != bfd_object)
7568 return TRUE;
7569
7570 s = bfd_get_section_by_name (abfd, ".dynamic");
7571 if (s == NULL || s->size == 0)
7572 return TRUE;
7573
7574 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7575 goto error_return;
7576
7577 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7578 if (elfsec == SHN_BAD)
4d269e42
AM
7579 goto error_return;
7580
7581 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7582
4d269e42
AM
7583 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7584 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7585
7586 extdyn = dynbuf;
7587 extdynend = extdyn + s->size;
7588 for (; extdyn < extdynend; extdyn += extdynsize)
7589 {
7590 Elf_Internal_Dyn dyn;
7591
7592 (*swap_dyn_in) (abfd, extdyn, &dyn);
7593
7594 if (dyn.d_tag == DT_NULL)
7595 break;
7596
7597 if (dyn.d_tag == DT_NEEDED)
7598 {
7599 const char *string;
7600 struct bfd_link_needed_list *l;
7601 unsigned int tagv = dyn.d_un.d_val;
7602 bfd_size_type amt;
7603
7604 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7605 if (string == NULL)
7606 goto error_return;
7607
7608 amt = sizeof *l;
a50b1753 7609 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7610 if (l == NULL)
7611 goto error_return;
7612
7613 l->by = abfd;
7614 l->name = string;
7615 l->next = *pneeded;
7616 *pneeded = l;
7617 }
7618 }
7619
7620 free (dynbuf);
7621
7622 return TRUE;
7623
7624 error_return:
7625 if (dynbuf != NULL)
7626 free (dynbuf);
7627 return FALSE;
7628}
7629
7630struct elf_symbuf_symbol
7631{
7632 unsigned long st_name; /* Symbol name, index in string tbl */
7633 unsigned char st_info; /* Type and binding attributes */
7634 unsigned char st_other; /* Visibilty, and target specific */
7635};
7636
7637struct elf_symbuf_head
7638{
7639 struct elf_symbuf_symbol *ssym;
ef53be89 7640 size_t count;
4d269e42
AM
7641 unsigned int st_shndx;
7642};
7643
7644struct elf_symbol
7645{
7646 union
7647 {
7648 Elf_Internal_Sym *isym;
7649 struct elf_symbuf_symbol *ssym;
7650 } u;
7651 const char *name;
7652};
7653
7654/* Sort references to symbols by ascending section number. */
7655
7656static int
7657elf_sort_elf_symbol (const void *arg1, const void *arg2)
7658{
7659 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7660 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7661
7662 return s1->st_shndx - s2->st_shndx;
7663}
7664
7665static int
7666elf_sym_name_compare (const void *arg1, const void *arg2)
7667{
7668 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7669 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7670 return strcmp (s1->name, s2->name);
7671}
7672
7673static struct elf_symbuf_head *
ef53be89 7674elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7675{
14b1c01e 7676 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7677 struct elf_symbuf_symbol *ssym;
7678 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7679 size_t i, shndx_count, total_size;
4d269e42 7680
a50b1753 7681 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7682 if (indbuf == NULL)
7683 return NULL;
7684
7685 for (ind = indbuf, i = 0; i < symcount; i++)
7686 if (isymbuf[i].st_shndx != SHN_UNDEF)
7687 *ind++ = &isymbuf[i];
7688 indbufend = ind;
7689
7690 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7691 elf_sort_elf_symbol);
7692
7693 shndx_count = 0;
7694 if (indbufend > indbuf)
7695 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7696 if (ind[0]->st_shndx != ind[1]->st_shndx)
7697 shndx_count++;
7698
3ae181ee
L
7699 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7700 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7701 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7702 if (ssymbuf == NULL)
7703 {
7704 free (indbuf);
7705 return NULL;
7706 }
7707
3ae181ee 7708 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7709 ssymbuf->ssym = NULL;
7710 ssymbuf->count = shndx_count;
7711 ssymbuf->st_shndx = 0;
7712 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7713 {
7714 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7715 {
7716 ssymhead++;
7717 ssymhead->ssym = ssym;
7718 ssymhead->count = 0;
7719 ssymhead->st_shndx = (*ind)->st_shndx;
7720 }
7721 ssym->st_name = (*ind)->st_name;
7722 ssym->st_info = (*ind)->st_info;
7723 ssym->st_other = (*ind)->st_other;
7724 ssymhead->count++;
7725 }
ef53be89 7726 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7727 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7728 == total_size));
4d269e42
AM
7729
7730 free (indbuf);
7731 return ssymbuf;
7732}
7733
7734/* Check if 2 sections define the same set of local and global
7735 symbols. */
7736
8f317e31 7737static bfd_boolean
4d269e42
AM
7738bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7739 struct bfd_link_info *info)
7740{
7741 bfd *bfd1, *bfd2;
7742 const struct elf_backend_data *bed1, *bed2;
7743 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7744 size_t symcount1, symcount2;
4d269e42
AM
7745 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7746 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7747 Elf_Internal_Sym *isym, *isymend;
7748 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7749 size_t count1, count2, i;
cb33740c 7750 unsigned int shndx1, shndx2;
4d269e42
AM
7751 bfd_boolean result;
7752
7753 bfd1 = sec1->owner;
7754 bfd2 = sec2->owner;
7755
4d269e42
AM
7756 /* Both sections have to be in ELF. */
7757 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7758 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7759 return FALSE;
7760
7761 if (elf_section_type (sec1) != elf_section_type (sec2))
7762 return FALSE;
7763
4d269e42
AM
7764 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7765 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7766 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7767 return FALSE;
7768
7769 bed1 = get_elf_backend_data (bfd1);
7770 bed2 = get_elf_backend_data (bfd2);
7771 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7772 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7773 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7774 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7775
7776 if (symcount1 == 0 || symcount2 == 0)
7777 return FALSE;
7778
7779 result = FALSE;
7780 isymbuf1 = NULL;
7781 isymbuf2 = NULL;
a50b1753
NC
7782 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7783 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7784
7785 if (ssymbuf1 == NULL)
7786 {
7787 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7788 NULL, NULL, NULL);
7789 if (isymbuf1 == NULL)
7790 goto done;
7791
7792 if (!info->reduce_memory_overheads)
7793 elf_tdata (bfd1)->symbuf = ssymbuf1
7794 = elf_create_symbuf (symcount1, isymbuf1);
7795 }
7796
7797 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7798 {
7799 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7800 NULL, NULL, NULL);
7801 if (isymbuf2 == NULL)
7802 goto done;
7803
7804 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7805 elf_tdata (bfd2)->symbuf = ssymbuf2
7806 = elf_create_symbuf (symcount2, isymbuf2);
7807 }
7808
7809 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7810 {
7811 /* Optimized faster version. */
ef53be89 7812 size_t lo, hi, mid;
4d269e42
AM
7813 struct elf_symbol *symp;
7814 struct elf_symbuf_symbol *ssym, *ssymend;
7815
7816 lo = 0;
7817 hi = ssymbuf1->count;
7818 ssymbuf1++;
7819 count1 = 0;
7820 while (lo < hi)
7821 {
7822 mid = (lo + hi) / 2;
cb33740c 7823 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7824 hi = mid;
cb33740c 7825 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7826 lo = mid + 1;
7827 else
7828 {
7829 count1 = ssymbuf1[mid].count;
7830 ssymbuf1 += mid;
7831 break;
7832 }
7833 }
7834
7835 lo = 0;
7836 hi = ssymbuf2->count;
7837 ssymbuf2++;
7838 count2 = 0;
7839 while (lo < hi)
7840 {
7841 mid = (lo + hi) / 2;
cb33740c 7842 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7843 hi = mid;
cb33740c 7844 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7845 lo = mid + 1;
7846 else
7847 {
7848 count2 = ssymbuf2[mid].count;
7849 ssymbuf2 += mid;
7850 break;
7851 }
7852 }
7853
7854 if (count1 == 0 || count2 == 0 || count1 != count2)
7855 goto done;
7856
ca4be51c
AM
7857 symtable1
7858 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7859 symtable2
7860 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7861 if (symtable1 == NULL || symtable2 == NULL)
7862 goto done;
7863
7864 symp = symtable1;
7865 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7866 ssym < ssymend; ssym++, symp++)
7867 {
7868 symp->u.ssym = ssym;
7869 symp->name = bfd_elf_string_from_elf_section (bfd1,
7870 hdr1->sh_link,
7871 ssym->st_name);
7872 }
7873
7874 symp = symtable2;
7875 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7876 ssym < ssymend; ssym++, symp++)
7877 {
7878 symp->u.ssym = ssym;
7879 symp->name = bfd_elf_string_from_elf_section (bfd2,
7880 hdr2->sh_link,
7881 ssym->st_name);
7882 }
7883
7884 /* Sort symbol by name. */
7885 qsort (symtable1, count1, sizeof (struct elf_symbol),
7886 elf_sym_name_compare);
7887 qsort (symtable2, count1, sizeof (struct elf_symbol),
7888 elf_sym_name_compare);
7889
7890 for (i = 0; i < count1; i++)
7891 /* Two symbols must have the same binding, type and name. */
7892 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7893 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7894 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7895 goto done;
7896
7897 result = TRUE;
7898 goto done;
7899 }
7900
a50b1753
NC
7901 symtable1 = (struct elf_symbol *)
7902 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7903 symtable2 = (struct elf_symbol *)
7904 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7905 if (symtable1 == NULL || symtable2 == NULL)
7906 goto done;
7907
7908 /* Count definitions in the section. */
7909 count1 = 0;
7910 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7911 if (isym->st_shndx == shndx1)
4d269e42
AM
7912 symtable1[count1++].u.isym = isym;
7913
7914 count2 = 0;
7915 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7916 if (isym->st_shndx == shndx2)
4d269e42
AM
7917 symtable2[count2++].u.isym = isym;
7918
7919 if (count1 == 0 || count2 == 0 || count1 != count2)
7920 goto done;
7921
7922 for (i = 0; i < count1; i++)
7923 symtable1[i].name
7924 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7925 symtable1[i].u.isym->st_name);
7926
7927 for (i = 0; i < count2; i++)
7928 symtable2[i].name
7929 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7930 symtable2[i].u.isym->st_name);
7931
7932 /* Sort symbol by name. */
7933 qsort (symtable1, count1, sizeof (struct elf_symbol),
7934 elf_sym_name_compare);
7935 qsort (symtable2, count1, sizeof (struct elf_symbol),
7936 elf_sym_name_compare);
7937
7938 for (i = 0; i < count1; i++)
7939 /* Two symbols must have the same binding, type and name. */
7940 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7941 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7942 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7943 goto done;
7944
7945 result = TRUE;
7946
7947done:
7948 if (symtable1)
7949 free (symtable1);
7950 if (symtable2)
7951 free (symtable2);
7952 if (isymbuf1)
7953 free (isymbuf1);
7954 if (isymbuf2)
7955 free (isymbuf2);
7956
7957 return result;
7958}
7959
7960/* Return TRUE if 2 section types are compatible. */
7961
7962bfd_boolean
7963_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7964 bfd *bbfd, const asection *bsec)
7965{
7966 if (asec == NULL
7967 || bsec == NULL
7968 || abfd->xvec->flavour != bfd_target_elf_flavour
7969 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7970 return TRUE;
7971
7972 return elf_section_type (asec) == elf_section_type (bsec);
7973}
7974\f
c152c796
AM
7975/* Final phase of ELF linker. */
7976
7977/* A structure we use to avoid passing large numbers of arguments. */
7978
7979struct elf_final_link_info
7980{
7981 /* General link information. */
7982 struct bfd_link_info *info;
7983 /* Output BFD. */
7984 bfd *output_bfd;
7985 /* Symbol string table. */
ef10c3ac 7986 struct elf_strtab_hash *symstrtab;
c152c796
AM
7987 /* .hash section. */
7988 asection *hash_sec;
7989 /* symbol version section (.gnu.version). */
7990 asection *symver_sec;
7991 /* Buffer large enough to hold contents of any section. */
7992 bfd_byte *contents;
7993 /* Buffer large enough to hold external relocs of any section. */
7994 void *external_relocs;
7995 /* Buffer large enough to hold internal relocs of any section. */
7996 Elf_Internal_Rela *internal_relocs;
7997 /* Buffer large enough to hold external local symbols of any input
7998 BFD. */
7999 bfd_byte *external_syms;
8000 /* And a buffer for symbol section indices. */
8001 Elf_External_Sym_Shndx *locsym_shndx;
8002 /* Buffer large enough to hold internal local symbols of any input
8003 BFD. */
8004 Elf_Internal_Sym *internal_syms;
8005 /* Array large enough to hold a symbol index for each local symbol
8006 of any input BFD. */
8007 long *indices;
8008 /* Array large enough to hold a section pointer for each local
8009 symbol of any input BFD. */
8010 asection **sections;
ef10c3ac 8011 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8012 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8013 /* Number of STT_FILE syms seen. */
8014 size_t filesym_count;
c152c796
AM
8015};
8016
8017/* This struct is used to pass information to elf_link_output_extsym. */
8018
8019struct elf_outext_info
8020{
8021 bfd_boolean failed;
8022 bfd_boolean localsyms;
34a79995 8023 bfd_boolean file_sym_done;
8b127cbc 8024 struct elf_final_link_info *flinfo;
c152c796
AM
8025};
8026
d9352518
DB
8027
8028/* Support for evaluating a complex relocation.
8029
8030 Complex relocations are generalized, self-describing relocations. The
8031 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8032 relocations themselves.
d9352518
DB
8033
8034 The relocations are use a reserved elf-wide relocation type code (R_RELC
8035 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8036 information (start bit, end bit, word width, etc) into the addend. This
8037 information is extracted from CGEN-generated operand tables within gas.
8038
8039 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8040 internal) representing prefix-notation expressions, including but not
8041 limited to those sorts of expressions normally encoded as addends in the
8042 addend field. The symbol mangling format is:
8043
8044 <node> := <literal>
07d6d2b8
AM
8045 | <unary-operator> ':' <node>
8046 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8047 ;
8048
8049 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8050 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8051 | '#' <hexdigits>
8052 ;
8053
8054 <binary-operator> := as in C
8055 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8056
8057static void
a0c8462f
AM
8058set_symbol_value (bfd *bfd_with_globals,
8059 Elf_Internal_Sym *isymbuf,
8060 size_t locsymcount,
8061 size_t symidx,
8062 bfd_vma val)
d9352518 8063{
8977835c
AM
8064 struct elf_link_hash_entry **sym_hashes;
8065 struct elf_link_hash_entry *h;
8066 size_t extsymoff = locsymcount;
d9352518 8067
8977835c 8068 if (symidx < locsymcount)
d9352518 8069 {
8977835c
AM
8070 Elf_Internal_Sym *sym;
8071
8072 sym = isymbuf + symidx;
8073 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8074 {
8075 /* It is a local symbol: move it to the
8076 "absolute" section and give it a value. */
8077 sym->st_shndx = SHN_ABS;
8078 sym->st_value = val;
8079 return;
8080 }
8081 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8082 extsymoff = 0;
d9352518 8083 }
8977835c
AM
8084
8085 /* It is a global symbol: set its link type
8086 to "defined" and give it a value. */
8087
8088 sym_hashes = elf_sym_hashes (bfd_with_globals);
8089 h = sym_hashes [symidx - extsymoff];
8090 while (h->root.type == bfd_link_hash_indirect
8091 || h->root.type == bfd_link_hash_warning)
8092 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8093 h->root.type = bfd_link_hash_defined;
8094 h->root.u.def.value = val;
8095 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8096}
8097
a0c8462f
AM
8098static bfd_boolean
8099resolve_symbol (const char *name,
8100 bfd *input_bfd,
8b127cbc 8101 struct elf_final_link_info *flinfo,
a0c8462f
AM
8102 bfd_vma *result,
8103 Elf_Internal_Sym *isymbuf,
8104 size_t locsymcount)
d9352518 8105{
a0c8462f
AM
8106 Elf_Internal_Sym *sym;
8107 struct bfd_link_hash_entry *global_entry;
8108 const char *candidate = NULL;
8109 Elf_Internal_Shdr *symtab_hdr;
8110 size_t i;
8111
d9352518
DB
8112 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8113
8114 for (i = 0; i < locsymcount; ++ i)
8115 {
8977835c 8116 sym = isymbuf + i;
d9352518
DB
8117
8118 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8119 continue;
8120
8121 candidate = bfd_elf_string_from_elf_section (input_bfd,
8122 symtab_hdr->sh_link,
8123 sym->st_name);
8124#ifdef DEBUG
0f02bbd9
AM
8125 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8126 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8127#endif
8128 if (candidate && strcmp (candidate, name) == 0)
8129 {
8b127cbc 8130 asection *sec = flinfo->sections [i];
d9352518 8131
0f02bbd9
AM
8132 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8133 *result += sec->output_offset + sec->output_section->vma;
d9352518 8134#ifdef DEBUG
0f02bbd9
AM
8135 printf ("Found symbol with value %8.8lx\n",
8136 (unsigned long) *result);
d9352518
DB
8137#endif
8138 return TRUE;
8139 }
8140 }
8141
8142 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8143 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8144 FALSE, FALSE, TRUE);
d9352518
DB
8145 if (!global_entry)
8146 return FALSE;
a0c8462f 8147
d9352518
DB
8148 if (global_entry->type == bfd_link_hash_defined
8149 || global_entry->type == bfd_link_hash_defweak)
8150 {
a0c8462f
AM
8151 *result = (global_entry->u.def.value
8152 + global_entry->u.def.section->output_section->vma
8153 + global_entry->u.def.section->output_offset);
d9352518 8154#ifdef DEBUG
0f02bbd9
AM
8155 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8156 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8157#endif
8158 return TRUE;
a0c8462f 8159 }
d9352518 8160
d9352518
DB
8161 return FALSE;
8162}
8163
37b01f6a
DG
8164/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8165 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8166 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8167
d9352518 8168static bfd_boolean
a0c8462f
AM
8169resolve_section (const char *name,
8170 asection *sections,
37b01f6a
DG
8171 bfd_vma *result,
8172 bfd * abfd)
d9352518 8173{
a0c8462f
AM
8174 asection *curr;
8175 unsigned int len;
d9352518 8176
a0c8462f 8177 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8178 if (strcmp (curr->name, name) == 0)
8179 {
8180 *result = curr->vma;
8181 return TRUE;
8182 }
8183
8184 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8185 /* FIXME: This could be coded more efficiently... */
a0c8462f 8186 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8187 {
8188 len = strlen (curr->name);
a0c8462f 8189 if (len > strlen (name))
d9352518
DB
8190 continue;
8191
8192 if (strncmp (curr->name, name, len) == 0)
8193 {
8194 if (strncmp (".end", name + len, 4) == 0)
8195 {
37b01f6a 8196 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8197 return TRUE;
8198 }
8199
8200 /* Insert more pseudo-section names here, if you like. */
8201 }
8202 }
a0c8462f 8203
d9352518
DB
8204 return FALSE;
8205}
8206
8207static void
a0c8462f 8208undefined_reference (const char *reftype, const char *name)
d9352518 8209{
695344c0 8210 /* xgettext:c-format */
a0c8462f
AM
8211 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8212 reftype, name);
d9352518
DB
8213}
8214
8215static bfd_boolean
a0c8462f
AM
8216eval_symbol (bfd_vma *result,
8217 const char **symp,
8218 bfd *input_bfd,
8b127cbc 8219 struct elf_final_link_info *flinfo,
a0c8462f
AM
8220 bfd_vma dot,
8221 Elf_Internal_Sym *isymbuf,
8222 size_t locsymcount,
8223 int signed_p)
d9352518 8224{
4b93929b
NC
8225 size_t len;
8226 size_t symlen;
a0c8462f
AM
8227 bfd_vma a;
8228 bfd_vma b;
4b93929b 8229 char symbuf[4096];
0f02bbd9 8230 const char *sym = *symp;
a0c8462f
AM
8231 const char *symend;
8232 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8233
8234 len = strlen (sym);
8235 symend = sym + len;
8236
4b93929b 8237 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8238 {
8239 bfd_set_error (bfd_error_invalid_operation);
8240 return FALSE;
8241 }
a0c8462f 8242
d9352518
DB
8243 switch (* sym)
8244 {
8245 case '.':
0f02bbd9
AM
8246 *result = dot;
8247 *symp = sym + 1;
d9352518
DB
8248 return TRUE;
8249
8250 case '#':
0f02bbd9
AM
8251 ++sym;
8252 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8253 return TRUE;
8254
8255 case 'S':
8256 symbol_is_section = TRUE;
1a0670f3 8257 /* Fall through. */
a0c8462f 8258 case 's':
0f02bbd9
AM
8259 ++sym;
8260 symlen = strtol (sym, (char **) symp, 10);
8261 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8262
4b93929b 8263 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8264 {
8265 bfd_set_error (bfd_error_invalid_operation);
8266 return FALSE;
8267 }
8268
8269 memcpy (symbuf, sym, symlen);
a0c8462f 8270 symbuf[symlen] = '\0';
0f02bbd9 8271 *symp = sym + symlen;
a0c8462f
AM
8272
8273 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8274 the symbol as a section, or vice-versa. so we're pretty liberal in our
8275 interpretation here; section means "try section first", not "must be a
8276 section", and likewise with symbol. */
8277
a0c8462f 8278 if (symbol_is_section)
d9352518 8279 {
37b01f6a 8280 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8281 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8282 isymbuf, locsymcount))
d9352518
DB
8283 {
8284 undefined_reference ("section", symbuf);
8285 return FALSE;
8286 }
a0c8462f
AM
8287 }
8288 else
d9352518 8289 {
8b127cbc 8290 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8291 isymbuf, locsymcount)
8b127cbc 8292 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8293 result, input_bfd))
d9352518
DB
8294 {
8295 undefined_reference ("symbol", symbuf);
8296 return FALSE;
8297 }
8298 }
8299
8300 return TRUE;
a0c8462f 8301
d9352518
DB
8302 /* All that remains are operators. */
8303
8304#define UNARY_OP(op) \
8305 if (strncmp (sym, #op, strlen (#op)) == 0) \
8306 { \
8307 sym += strlen (#op); \
a0c8462f
AM
8308 if (*sym == ':') \
8309 ++sym; \
0f02bbd9 8310 *symp = sym; \
8b127cbc 8311 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8312 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8313 return FALSE; \
8314 if (signed_p) \
0f02bbd9 8315 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8316 else \
8317 *result = op a; \
d9352518
DB
8318 return TRUE; \
8319 }
8320
8321#define BINARY_OP(op) \
8322 if (strncmp (sym, #op, strlen (#op)) == 0) \
8323 { \
8324 sym += strlen (#op); \
a0c8462f
AM
8325 if (*sym == ':') \
8326 ++sym; \
0f02bbd9 8327 *symp = sym; \
8b127cbc 8328 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8329 isymbuf, locsymcount, signed_p)) \
a0c8462f 8330 return FALSE; \
0f02bbd9 8331 ++*symp; \
8b127cbc 8332 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8333 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8334 return FALSE; \
8335 if (signed_p) \
0f02bbd9 8336 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8337 else \
8338 *result = a op b; \
d9352518
DB
8339 return TRUE; \
8340 }
8341
8342 default:
8343 UNARY_OP (0-);
8344 BINARY_OP (<<);
8345 BINARY_OP (>>);
8346 BINARY_OP (==);
8347 BINARY_OP (!=);
8348 BINARY_OP (<=);
8349 BINARY_OP (>=);
8350 BINARY_OP (&&);
8351 BINARY_OP (||);
8352 UNARY_OP (~);
8353 UNARY_OP (!);
8354 BINARY_OP (*);
8355 BINARY_OP (/);
8356 BINARY_OP (%);
8357 BINARY_OP (^);
8358 BINARY_OP (|);
8359 BINARY_OP (&);
8360 BINARY_OP (+);
8361 BINARY_OP (-);
8362 BINARY_OP (<);
8363 BINARY_OP (>);
8364#undef UNARY_OP
8365#undef BINARY_OP
8366 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8367 bfd_set_error (bfd_error_invalid_operation);
8368 return FALSE;
8369 }
8370}
8371
d9352518 8372static void
a0c8462f
AM
8373put_value (bfd_vma size,
8374 unsigned long chunksz,
8375 bfd *input_bfd,
8376 bfd_vma x,
8377 bfd_byte *location)
d9352518
DB
8378{
8379 location += (size - chunksz);
8380
41cd1ad1 8381 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8382 {
8383 switch (chunksz)
8384 {
d9352518
DB
8385 case 1:
8386 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8387 x >>= 8;
d9352518
DB
8388 break;
8389 case 2:
8390 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8391 x >>= 16;
d9352518
DB
8392 break;
8393 case 4:
8394 bfd_put_32 (input_bfd, x, location);
65164438
NC
8395 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8396 x >>= 16;
8397 x >>= 16;
d9352518 8398 break;
d9352518 8399#ifdef BFD64
41cd1ad1 8400 case 8:
d9352518 8401 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8402 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8403 x >>= 32;
8404 x >>= 32;
8405 break;
d9352518 8406#endif
41cd1ad1
NC
8407 default:
8408 abort ();
d9352518
DB
8409 break;
8410 }
8411 }
8412}
8413
a0c8462f
AM
8414static bfd_vma
8415get_value (bfd_vma size,
8416 unsigned long chunksz,
8417 bfd *input_bfd,
8418 bfd_byte *location)
d9352518 8419{
9b239e0e 8420 int shift;
d9352518
DB
8421 bfd_vma x = 0;
8422
9b239e0e
NC
8423 /* Sanity checks. */
8424 BFD_ASSERT (chunksz <= sizeof (x)
8425 && size >= chunksz
8426 && chunksz != 0
8427 && (size % chunksz) == 0
8428 && input_bfd != NULL
8429 && location != NULL);
8430
8431 if (chunksz == sizeof (x))
8432 {
8433 BFD_ASSERT (size == chunksz);
8434
8435 /* Make sure that we do not perform an undefined shift operation.
8436 We know that size == chunksz so there will only be one iteration
8437 of the loop below. */
8438 shift = 0;
8439 }
8440 else
8441 shift = 8 * chunksz;
8442
a0c8462f 8443 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8444 {
8445 switch (chunksz)
8446 {
d9352518 8447 case 1:
9b239e0e 8448 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8449 break;
8450 case 2:
9b239e0e 8451 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8452 break;
8453 case 4:
9b239e0e 8454 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8455 break;
d9352518 8456#ifdef BFD64
9b239e0e
NC
8457 case 8:
8458 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8459 break;
9b239e0e
NC
8460#endif
8461 default:
8462 abort ();
d9352518
DB
8463 }
8464 }
8465 return x;
8466}
8467
a0c8462f
AM
8468static void
8469decode_complex_addend (unsigned long *start, /* in bits */
8470 unsigned long *oplen, /* in bits */
8471 unsigned long *len, /* in bits */
8472 unsigned long *wordsz, /* in bytes */
8473 unsigned long *chunksz, /* in bytes */
8474 unsigned long *lsb0_p,
8475 unsigned long *signed_p,
8476 unsigned long *trunc_p,
8477 unsigned long encoded)
d9352518 8478{
07d6d2b8
AM
8479 * start = encoded & 0x3F;
8480 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8481 * oplen = (encoded >> 12) & 0x3F;
8482 * wordsz = (encoded >> 18) & 0xF;
8483 * chunksz = (encoded >> 22) & 0xF;
8484 * lsb0_p = (encoded >> 27) & 1;
8485 * signed_p = (encoded >> 28) & 1;
8486 * trunc_p = (encoded >> 29) & 1;
8487}
8488
cdfeee4f 8489bfd_reloc_status_type
0f02bbd9 8490bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8491 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8492 bfd_byte *contents,
8493 Elf_Internal_Rela *rel,
8494 bfd_vma relocation)
d9352518 8495{
0f02bbd9
AM
8496 bfd_vma shift, x, mask;
8497 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8498 bfd_reloc_status_type r;
d9352518
DB
8499
8500 /* Perform this reloc, since it is complex.
8501 (this is not to say that it necessarily refers to a complex
8502 symbol; merely that it is a self-describing CGEN based reloc.
8503 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8504 word size, etc) encoded within it.). */
d9352518 8505
a0c8462f
AM
8506 decode_complex_addend (&start, &oplen, &len, &wordsz,
8507 &chunksz, &lsb0_p, &signed_p,
8508 &trunc_p, rel->r_addend);
d9352518
DB
8509
8510 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8511
8512 if (lsb0_p)
8513 shift = (start + 1) - len;
8514 else
8515 shift = (8 * wordsz) - (start + len);
8516
37b01f6a
DG
8517 x = get_value (wordsz, chunksz, input_bfd,
8518 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8519
8520#ifdef DEBUG
8521 printf ("Doing complex reloc: "
8522 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8523 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8524 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8525 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8526 oplen, (unsigned long) x, (unsigned long) mask,
8527 (unsigned long) relocation);
d9352518
DB
8528#endif
8529
cdfeee4f 8530 r = bfd_reloc_ok;
d9352518 8531 if (! trunc_p)
cdfeee4f
AM
8532 /* Now do an overflow check. */
8533 r = bfd_check_overflow ((signed_p
8534 ? complain_overflow_signed
8535 : complain_overflow_unsigned),
8536 len, 0, (8 * wordsz),
8537 relocation);
a0c8462f 8538
d9352518
DB
8539 /* Do the deed. */
8540 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8541
8542#ifdef DEBUG
8543 printf (" relocation: %8.8lx\n"
8544 " shifted mask: %8.8lx\n"
8545 " shifted/masked reloc: %8.8lx\n"
8546 " result: %8.8lx\n",
9ccb8af9
AM
8547 (unsigned long) relocation, (unsigned long) (mask << shift),
8548 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8549#endif
37b01f6a
DG
8550 put_value (wordsz, chunksz, input_bfd, x,
8551 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8552 return r;
d9352518
DB
8553}
8554
0e287786
AM
8555/* Functions to read r_offset from external (target order) reloc
8556 entry. Faster than bfd_getl32 et al, because we let the compiler
8557 know the value is aligned. */
53df40a4 8558
0e287786
AM
8559static bfd_vma
8560ext32l_r_offset (const void *p)
53df40a4
AM
8561{
8562 union aligned32
8563 {
8564 uint32_t v;
8565 unsigned char c[4];
8566 };
8567 const union aligned32 *a
0e287786 8568 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8569
8570 uint32_t aval = ( (uint32_t) a->c[0]
8571 | (uint32_t) a->c[1] << 8
8572 | (uint32_t) a->c[2] << 16
8573 | (uint32_t) a->c[3] << 24);
0e287786 8574 return aval;
53df40a4
AM
8575}
8576
0e287786
AM
8577static bfd_vma
8578ext32b_r_offset (const void *p)
53df40a4
AM
8579{
8580 union aligned32
8581 {
8582 uint32_t v;
8583 unsigned char c[4];
8584 };
8585 const union aligned32 *a
0e287786 8586 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8587
8588 uint32_t aval = ( (uint32_t) a->c[0] << 24
8589 | (uint32_t) a->c[1] << 16
8590 | (uint32_t) a->c[2] << 8
8591 | (uint32_t) a->c[3]);
0e287786 8592 return aval;
53df40a4
AM
8593}
8594
8595#ifdef BFD_HOST_64_BIT
0e287786
AM
8596static bfd_vma
8597ext64l_r_offset (const void *p)
53df40a4
AM
8598{
8599 union aligned64
8600 {
8601 uint64_t v;
8602 unsigned char c[8];
8603 };
8604 const union aligned64 *a
0e287786 8605 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8606
8607 uint64_t aval = ( (uint64_t) a->c[0]
8608 | (uint64_t) a->c[1] << 8
8609 | (uint64_t) a->c[2] << 16
8610 | (uint64_t) a->c[3] << 24
8611 | (uint64_t) a->c[4] << 32
8612 | (uint64_t) a->c[5] << 40
8613 | (uint64_t) a->c[6] << 48
8614 | (uint64_t) a->c[7] << 56);
0e287786 8615 return aval;
53df40a4
AM
8616}
8617
0e287786
AM
8618static bfd_vma
8619ext64b_r_offset (const void *p)
53df40a4
AM
8620{
8621 union aligned64
8622 {
8623 uint64_t v;
8624 unsigned char c[8];
8625 };
8626 const union aligned64 *a
0e287786 8627 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8628
8629 uint64_t aval = ( (uint64_t) a->c[0] << 56
8630 | (uint64_t) a->c[1] << 48
8631 | (uint64_t) a->c[2] << 40
8632 | (uint64_t) a->c[3] << 32
8633 | (uint64_t) a->c[4] << 24
8634 | (uint64_t) a->c[5] << 16
8635 | (uint64_t) a->c[6] << 8
8636 | (uint64_t) a->c[7]);
0e287786 8637 return aval;
53df40a4
AM
8638}
8639#endif
8640
c152c796
AM
8641/* When performing a relocatable link, the input relocations are
8642 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8643 referenced must be updated. Update all the relocations found in
8644 RELDATA. */
c152c796 8645
bca6d0e3 8646static bfd_boolean
c152c796 8647elf_link_adjust_relocs (bfd *abfd,
9eaff861 8648 asection *sec,
28dbcedc 8649 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8650 bfd_boolean sort,
8651 struct bfd_link_info *info)
c152c796
AM
8652{
8653 unsigned int i;
8654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8655 bfd_byte *erela;
8656 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8657 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8658 bfd_vma r_type_mask;
8659 int r_sym_shift;
d4730f92
BS
8660 unsigned int count = reldata->count;
8661 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8662
d4730f92 8663 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8664 {
8665 swap_in = bed->s->swap_reloc_in;
8666 swap_out = bed->s->swap_reloc_out;
8667 }
d4730f92 8668 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8669 {
8670 swap_in = bed->s->swap_reloca_in;
8671 swap_out = bed->s->swap_reloca_out;
8672 }
8673 else
8674 abort ();
8675
8676 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8677 abort ();
8678
8679 if (bed->s->arch_size == 32)
8680 {
8681 r_type_mask = 0xff;
8682 r_sym_shift = 8;
8683 }
8684 else
8685 {
8686 r_type_mask = 0xffffffff;
8687 r_sym_shift = 32;
8688 }
8689
d4730f92
BS
8690 erela = reldata->hdr->contents;
8691 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8692 {
8693 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8694 unsigned int j;
8695
8696 if (*rel_hash == NULL)
8697 continue;
8698
10bbbc1d
NC
8699 if ((*rel_hash)->indx == -2
8700 && info->gc_sections
8701 && ! info->gc_keep_exported)
8702 {
8703 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8704 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8705 abfd, sec,
8706 (*rel_hash)->root.root.string);
8707 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
d42c267e 8708 abfd, sec);
10bbbc1d
NC
8709 bfd_set_error (bfd_error_invalid_operation);
8710 return FALSE;
8711 }
c152c796
AM
8712 BFD_ASSERT ((*rel_hash)->indx >= 0);
8713
8714 (*swap_in) (abfd, erela, irela);
8715 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8716 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8717 | (irela[j].r_info & r_type_mask));
8718 (*swap_out) (abfd, irela, erela);
8719 }
53df40a4 8720
9eaff861
AO
8721 if (bed->elf_backend_update_relocs)
8722 (*bed->elf_backend_update_relocs) (sec, reldata);
8723
0e287786 8724 if (sort && count != 0)
53df40a4 8725 {
0e287786
AM
8726 bfd_vma (*ext_r_off) (const void *);
8727 bfd_vma r_off;
8728 size_t elt_size;
8729 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8730 bfd_byte *buf = NULL;
28dbcedc
AM
8731
8732 if (bed->s->arch_size == 32)
8733 {
8734 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8735 ext_r_off = ext32l_r_offset;
28dbcedc 8736 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8737 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8738 else
8739 abort ();
8740 }
53df40a4 8741 else
28dbcedc 8742 {
53df40a4 8743#ifdef BFD_HOST_64_BIT
28dbcedc 8744 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8745 ext_r_off = ext64l_r_offset;
28dbcedc 8746 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8747 ext_r_off = ext64b_r_offset;
28dbcedc 8748 else
53df40a4 8749#endif
28dbcedc
AM
8750 abort ();
8751 }
0e287786 8752
bca6d0e3
AM
8753 /* Must use a stable sort here. A modified insertion sort,
8754 since the relocs are mostly sorted already. */
0e287786
AM
8755 elt_size = reldata->hdr->sh_entsize;
8756 base = reldata->hdr->contents;
8757 end = base + count * elt_size;
bca6d0e3 8758 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8759 abort ();
8760
8761 /* Ensure the first element is lowest. This acts as a sentinel,
8762 speeding the main loop below. */
8763 r_off = (*ext_r_off) (base);
8764 for (p = loc = base; (p += elt_size) < end; )
8765 {
8766 bfd_vma r_off2 = (*ext_r_off) (p);
8767 if (r_off > r_off2)
8768 {
8769 r_off = r_off2;
8770 loc = p;
8771 }
8772 }
8773 if (loc != base)
8774 {
8775 /* Don't just swap *base and *loc as that changes the order
8776 of the original base[0] and base[1] if they happen to
8777 have the same r_offset. */
bca6d0e3
AM
8778 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8779 memcpy (onebuf, loc, elt_size);
0e287786 8780 memmove (base + elt_size, base, loc - base);
bca6d0e3 8781 memcpy (base, onebuf, elt_size);
0e287786
AM
8782 }
8783
b29b8669 8784 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8785 {
8786 /* base to p is sorted, *p is next to insert. */
8787 r_off = (*ext_r_off) (p);
8788 /* Search the sorted region for location to insert. */
8789 loc = p - elt_size;
8790 while (r_off < (*ext_r_off) (loc))
8791 loc -= elt_size;
8792 loc += elt_size;
8793 if (loc != p)
8794 {
bca6d0e3
AM
8795 /* Chances are there is a run of relocs to insert here,
8796 from one of more input files. Files are not always
8797 linked in order due to the way elf_link_input_bfd is
8798 called. See pr17666. */
8799 size_t sortlen = p - loc;
8800 bfd_vma r_off2 = (*ext_r_off) (loc);
8801 size_t runlen = elt_size;
8802 size_t buf_size = 96 * 1024;
8803 while (p + runlen < end
8804 && (sortlen <= buf_size
8805 || runlen + elt_size <= buf_size)
8806 && r_off2 > (*ext_r_off) (p + runlen))
8807 runlen += elt_size;
8808 if (buf == NULL)
8809 {
8810 buf = bfd_malloc (buf_size);
8811 if (buf == NULL)
8812 return FALSE;
8813 }
8814 if (runlen < sortlen)
8815 {
8816 memcpy (buf, p, runlen);
8817 memmove (loc + runlen, loc, sortlen);
8818 memcpy (loc, buf, runlen);
8819 }
8820 else
8821 {
8822 memcpy (buf, loc, sortlen);
8823 memmove (loc, p, runlen);
8824 memcpy (loc + runlen, buf, sortlen);
8825 }
b29b8669 8826 p += runlen - elt_size;
0e287786
AM
8827 }
8828 }
8829 /* Hashes are no longer valid. */
28dbcedc
AM
8830 free (reldata->hashes);
8831 reldata->hashes = NULL;
bca6d0e3 8832 free (buf);
53df40a4 8833 }
bca6d0e3 8834 return TRUE;
c152c796
AM
8835}
8836
8837struct elf_link_sort_rela
8838{
8839 union {
8840 bfd_vma offset;
8841 bfd_vma sym_mask;
8842 } u;
8843 enum elf_reloc_type_class type;
8844 /* We use this as an array of size int_rels_per_ext_rel. */
8845 Elf_Internal_Rela rela[1];
8846};
8847
8848static int
8849elf_link_sort_cmp1 (const void *A, const void *B)
8850{
a50b1753
NC
8851 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8852 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8853 int relativea, relativeb;
8854
8855 relativea = a->type == reloc_class_relative;
8856 relativeb = b->type == reloc_class_relative;
8857
8858 if (relativea < relativeb)
8859 return 1;
8860 if (relativea > relativeb)
8861 return -1;
8862 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8863 return -1;
8864 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8865 return 1;
8866 if (a->rela->r_offset < b->rela->r_offset)
8867 return -1;
8868 if (a->rela->r_offset > b->rela->r_offset)
8869 return 1;
8870 return 0;
8871}
8872
8873static int
8874elf_link_sort_cmp2 (const void *A, const void *B)
8875{
a50b1753
NC
8876 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8877 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8878
7e612e98 8879 if (a->type < b->type)
c152c796 8880 return -1;
7e612e98 8881 if (a->type > b->type)
c152c796 8882 return 1;
7e612e98 8883 if (a->u.offset < b->u.offset)
c152c796 8884 return -1;
7e612e98 8885 if (a->u.offset > b->u.offset)
c152c796
AM
8886 return 1;
8887 if (a->rela->r_offset < b->rela->r_offset)
8888 return -1;
8889 if (a->rela->r_offset > b->rela->r_offset)
8890 return 1;
8891 return 0;
8892}
8893
8894static size_t
8895elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8896{
3410fea8 8897 asection *dynamic_relocs;
fc66a176
L
8898 asection *rela_dyn;
8899 asection *rel_dyn;
c152c796
AM
8900 bfd_size_type count, size;
8901 size_t i, ret, sort_elt, ext_size;
8902 bfd_byte *sort, *s_non_relative, *p;
8903 struct elf_link_sort_rela *sq;
8904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8905 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8906 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8907 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8908 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8909 struct bfd_link_order *lo;
8910 bfd_vma r_sym_mask;
3410fea8 8911 bfd_boolean use_rela;
c152c796 8912
3410fea8
NC
8913 /* Find a dynamic reloc section. */
8914 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8915 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8916 if (rela_dyn != NULL && rela_dyn->size > 0
8917 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8918 {
3410fea8
NC
8919 bfd_boolean use_rela_initialised = FALSE;
8920
8921 /* This is just here to stop gcc from complaining.
c8e44c6d 8922 Its initialization checking code is not perfect. */
3410fea8
NC
8923 use_rela = TRUE;
8924
8925 /* Both sections are present. Examine the sizes
8926 of the indirect sections to help us choose. */
8927 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8928 if (lo->type == bfd_indirect_link_order)
8929 {
8930 asection *o = lo->u.indirect.section;
8931
8932 if ((o->size % bed->s->sizeof_rela) == 0)
8933 {
8934 if ((o->size % bed->s->sizeof_rel) == 0)
8935 /* Section size is divisible by both rel and rela sizes.
8936 It is of no help to us. */
8937 ;
8938 else
8939 {
8940 /* Section size is only divisible by rela. */
535b785f 8941 if (use_rela_initialised && !use_rela)
3410fea8 8942 {
c8e44c6d
AM
8943 _bfd_error_handler (_("%B: Unable to sort relocs - "
8944 "they are in more than one size"),
8945 abfd);
3410fea8
NC
8946 bfd_set_error (bfd_error_invalid_operation);
8947 return 0;
8948 }
8949 else
8950 {
8951 use_rela = TRUE;
8952 use_rela_initialised = TRUE;
8953 }
8954 }
8955 }
8956 else if ((o->size % bed->s->sizeof_rel) == 0)
8957 {
8958 /* Section size is only divisible by rel. */
535b785f 8959 if (use_rela_initialised && use_rela)
3410fea8 8960 {
c8e44c6d
AM
8961 _bfd_error_handler (_("%B: Unable to sort relocs - "
8962 "they are in more than one size"),
8963 abfd);
3410fea8
NC
8964 bfd_set_error (bfd_error_invalid_operation);
8965 return 0;
8966 }
8967 else
8968 {
8969 use_rela = FALSE;
8970 use_rela_initialised = TRUE;
8971 }
8972 }
8973 else
8974 {
c8e44c6d
AM
8975 /* The section size is not divisible by either -
8976 something is wrong. */
8977 _bfd_error_handler (_("%B: Unable to sort relocs - "
8978 "they are of an unknown size"), abfd);
3410fea8
NC
8979 bfd_set_error (bfd_error_invalid_operation);
8980 return 0;
8981 }
8982 }
8983
8984 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8985 if (lo->type == bfd_indirect_link_order)
8986 {
8987 asection *o = lo->u.indirect.section;
8988
8989 if ((o->size % bed->s->sizeof_rela) == 0)
8990 {
8991 if ((o->size % bed->s->sizeof_rel) == 0)
8992 /* Section size is divisible by both rel and rela sizes.
8993 It is of no help to us. */
8994 ;
8995 else
8996 {
8997 /* Section size is only divisible by rela. */
535b785f 8998 if (use_rela_initialised && !use_rela)
3410fea8 8999 {
c8e44c6d
AM
9000 _bfd_error_handler (_("%B: Unable to sort relocs - "
9001 "they are in more than one size"),
9002 abfd);
3410fea8
NC
9003 bfd_set_error (bfd_error_invalid_operation);
9004 return 0;
9005 }
9006 else
9007 {
9008 use_rela = TRUE;
9009 use_rela_initialised = TRUE;
9010 }
9011 }
9012 }
9013 else if ((o->size % bed->s->sizeof_rel) == 0)
9014 {
9015 /* Section size is only divisible by rel. */
535b785f 9016 if (use_rela_initialised && use_rela)
3410fea8 9017 {
c8e44c6d
AM
9018 _bfd_error_handler (_("%B: Unable to sort relocs - "
9019 "they are in more than one size"),
9020 abfd);
3410fea8
NC
9021 bfd_set_error (bfd_error_invalid_operation);
9022 return 0;
9023 }
9024 else
9025 {
9026 use_rela = FALSE;
9027 use_rela_initialised = TRUE;
9028 }
9029 }
9030 else
9031 {
c8e44c6d
AM
9032 /* The section size is not divisible by either -
9033 something is wrong. */
9034 _bfd_error_handler (_("%B: Unable to sort relocs - "
9035 "they are of an unknown size"), abfd);
3410fea8
NC
9036 bfd_set_error (bfd_error_invalid_operation);
9037 return 0;
9038 }
9039 }
9040
9041 if (! use_rela_initialised)
9042 /* Make a guess. */
9043 use_rela = TRUE;
c152c796 9044 }
fc66a176
L
9045 else if (rela_dyn != NULL && rela_dyn->size > 0)
9046 use_rela = TRUE;
9047 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9048 use_rela = FALSE;
c152c796 9049 else
fc66a176 9050 return 0;
3410fea8
NC
9051
9052 if (use_rela)
c152c796 9053 {
3410fea8 9054 dynamic_relocs = rela_dyn;
c152c796
AM
9055 ext_size = bed->s->sizeof_rela;
9056 swap_in = bed->s->swap_reloca_in;
9057 swap_out = bed->s->swap_reloca_out;
9058 }
3410fea8
NC
9059 else
9060 {
9061 dynamic_relocs = rel_dyn;
9062 ext_size = bed->s->sizeof_rel;
9063 swap_in = bed->s->swap_reloc_in;
9064 swap_out = bed->s->swap_reloc_out;
9065 }
c152c796
AM
9066
9067 size = 0;
3410fea8 9068 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9069 if (lo->type == bfd_indirect_link_order)
3410fea8 9070 size += lo->u.indirect.section->size;
c152c796 9071
3410fea8 9072 if (size != dynamic_relocs->size)
c152c796
AM
9073 return 0;
9074
9075 sort_elt = (sizeof (struct elf_link_sort_rela)
9076 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9077
9078 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9079 if (count == 0)
9080 return 0;
a50b1753 9081 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9082
c152c796
AM
9083 if (sort == NULL)
9084 {
9085 (*info->callbacks->warning)
9086 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9087 return 0;
9088 }
9089
9090 if (bed->s->arch_size == 32)
9091 r_sym_mask = ~(bfd_vma) 0xff;
9092 else
9093 r_sym_mask = ~(bfd_vma) 0xffffffff;
9094
3410fea8 9095 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9096 if (lo->type == bfd_indirect_link_order)
9097 {
9098 bfd_byte *erel, *erelend;
9099 asection *o = lo->u.indirect.section;
9100
1da212d6
AM
9101 if (o->contents == NULL && o->size != 0)
9102 {
9103 /* This is a reloc section that is being handled as a normal
9104 section. See bfd_section_from_shdr. We can't combine
9105 relocs in this case. */
9106 free (sort);
9107 return 0;
9108 }
c152c796 9109 erel = o->contents;
eea6121a 9110 erelend = o->contents + o->size;
c8e44c6d 9111 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9112
c152c796
AM
9113 while (erel < erelend)
9114 {
9115 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9116
c152c796 9117 (*swap_in) (abfd, erel, s->rela);
7e612e98 9118 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9119 s->u.sym_mask = r_sym_mask;
9120 p += sort_elt;
9121 erel += ext_size;
9122 }
9123 }
9124
9125 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9126
9127 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9128 {
9129 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9130 if (s->type != reloc_class_relative)
9131 break;
9132 }
9133 ret = i;
9134 s_non_relative = p;
9135
9136 sq = (struct elf_link_sort_rela *) s_non_relative;
9137 for (; i < count; i++, p += sort_elt)
9138 {
9139 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9140 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9141 sq = sp;
9142 sp->u.offset = sq->rela->r_offset;
9143 }
9144
9145 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9146
c8e44c6d
AM
9147 struct elf_link_hash_table *htab = elf_hash_table (info);
9148 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9149 {
9150 /* We have plt relocs in .rela.dyn. */
9151 sq = (struct elf_link_sort_rela *) sort;
9152 for (i = 0; i < count; i++)
9153 if (sq[count - i - 1].type != reloc_class_plt)
9154 break;
9155 if (i != 0 && htab->srelplt->size == i * ext_size)
9156 {
9157 struct bfd_link_order **plo;
9158 /* Put srelplt link_order last. This is so the output_offset
9159 set in the next loop is correct for DT_JMPREL. */
9160 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9161 if ((*plo)->type == bfd_indirect_link_order
9162 && (*plo)->u.indirect.section == htab->srelplt)
9163 {
9164 lo = *plo;
9165 *plo = lo->next;
9166 }
9167 else
9168 plo = &(*plo)->next;
9169 *plo = lo;
9170 lo->next = NULL;
9171 dynamic_relocs->map_tail.link_order = lo;
9172 }
9173 }
9174
9175 p = sort;
3410fea8 9176 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9177 if (lo->type == bfd_indirect_link_order)
9178 {
9179 bfd_byte *erel, *erelend;
9180 asection *o = lo->u.indirect.section;
9181
9182 erel = o->contents;
eea6121a 9183 erelend = o->contents + o->size;
c8e44c6d 9184 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9185 while (erel < erelend)
9186 {
9187 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9188 (*swap_out) (abfd, s->rela, erel);
9189 p += sort_elt;
9190 erel += ext_size;
9191 }
9192 }
9193
9194 free (sort);
3410fea8 9195 *psec = dynamic_relocs;
c152c796
AM
9196 return ret;
9197}
9198
ef10c3ac 9199/* Add a symbol to the output symbol string table. */
c152c796 9200
6e0b88f1 9201static int
ef10c3ac
L
9202elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9203 const char *name,
9204 Elf_Internal_Sym *elfsym,
9205 asection *input_sec,
9206 struct elf_link_hash_entry *h)
c152c796 9207{
6e0b88f1 9208 int (*output_symbol_hook)
c152c796
AM
9209 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9210 struct elf_link_hash_entry *);
ef10c3ac 9211 struct elf_link_hash_table *hash_table;
c152c796 9212 const struct elf_backend_data *bed;
ef10c3ac 9213 bfd_size_type strtabsize;
c152c796 9214
8539e4e8
AM
9215 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9216
8b127cbc 9217 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9218 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9219 if (output_symbol_hook != NULL)
9220 {
8b127cbc 9221 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9222 if (ret != 1)
9223 return ret;
c152c796
AM
9224 }
9225
ef10c3ac
L
9226 if (name == NULL
9227 || *name == '\0'
9228 || (input_sec->flags & SEC_EXCLUDE))
9229 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9230 else
9231 {
ef10c3ac
L
9232 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9233 to get the final offset for st_name. */
9234 elfsym->st_name
9235 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9236 name, FALSE);
c152c796 9237 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9238 return 0;
c152c796
AM
9239 }
9240
ef10c3ac
L
9241 hash_table = elf_hash_table (flinfo->info);
9242 strtabsize = hash_table->strtabsize;
9243 if (strtabsize <= hash_table->strtabcount)
c152c796 9244 {
ef10c3ac
L
9245 strtabsize += strtabsize;
9246 hash_table->strtabsize = strtabsize;
9247 strtabsize *= sizeof (*hash_table->strtab);
9248 hash_table->strtab
9249 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9250 strtabsize);
9251 if (hash_table->strtab == NULL)
6e0b88f1 9252 return 0;
c152c796 9253 }
ef10c3ac
L
9254 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9255 hash_table->strtab[hash_table->strtabcount].dest_index
9256 = hash_table->strtabcount;
9257 hash_table->strtab[hash_table->strtabcount].destshndx_index
9258 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9259
9260 bfd_get_symcount (flinfo->output_bfd) += 1;
9261 hash_table->strtabcount += 1;
9262
9263 return 1;
9264}
9265
9266/* Swap symbols out to the symbol table and flush the output symbols to
9267 the file. */
9268
9269static bfd_boolean
9270elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9271{
9272 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9273 bfd_size_type amt;
9274 size_t i;
ef10c3ac
L
9275 const struct elf_backend_data *bed;
9276 bfd_byte *symbuf;
9277 Elf_Internal_Shdr *hdr;
9278 file_ptr pos;
9279 bfd_boolean ret;
9280
9281 if (!hash_table->strtabcount)
9282 return TRUE;
9283
9284 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9285
9286 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9287
ef10c3ac
L
9288 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9289 symbuf = (bfd_byte *) bfd_malloc (amt);
9290 if (symbuf == NULL)
9291 return FALSE;
1b786873 9292
ef10c3ac 9293 if (flinfo->symshndxbuf)
c152c796 9294 {
ef53be89
AM
9295 amt = sizeof (Elf_External_Sym_Shndx);
9296 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9297 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9298 if (flinfo->symshndxbuf == NULL)
c152c796 9299 {
ef10c3ac
L
9300 free (symbuf);
9301 return FALSE;
c152c796 9302 }
c152c796
AM
9303 }
9304
ef10c3ac
L
9305 for (i = 0; i < hash_table->strtabcount; i++)
9306 {
9307 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9308 if (elfsym->sym.st_name == (unsigned long) -1)
9309 elfsym->sym.st_name = 0;
9310 else
9311 elfsym->sym.st_name
9312 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9313 elfsym->sym.st_name);
9314 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9315 ((bfd_byte *) symbuf
9316 + (elfsym->dest_index
9317 * bed->s->sizeof_sym)),
9318 (flinfo->symshndxbuf
9319 + elfsym->destshndx_index));
9320 }
9321
9322 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9323 pos = hdr->sh_offset + hdr->sh_size;
9324 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9325 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9326 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9327 {
9328 hdr->sh_size += amt;
9329 ret = TRUE;
9330 }
9331 else
9332 ret = FALSE;
c152c796 9333
ef10c3ac
L
9334 free (symbuf);
9335
9336 free (hash_table->strtab);
9337 hash_table->strtab = NULL;
9338
9339 return ret;
c152c796
AM
9340}
9341
c0d5a53d
L
9342/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9343
9344static bfd_boolean
9345check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9346{
4fbb74a6
AM
9347 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9348 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9349 {
9350 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9351 beyond 64k. */
4eca0228 9352 _bfd_error_handler
695344c0 9353 /* xgettext:c-format */
c0d5a53d 9354 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9355 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9356 bfd_set_error (bfd_error_nonrepresentable_section);
9357 return FALSE;
9358 }
9359 return TRUE;
9360}
9361
c152c796
AM
9362/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9363 allowing an unsatisfied unversioned symbol in the DSO to match a
9364 versioned symbol that would normally require an explicit version.
9365 We also handle the case that a DSO references a hidden symbol
9366 which may be satisfied by a versioned symbol in another DSO. */
9367
9368static bfd_boolean
9369elf_link_check_versioned_symbol (struct bfd_link_info *info,
9370 const struct elf_backend_data *bed,
9371 struct elf_link_hash_entry *h)
9372{
9373 bfd *abfd;
9374 struct elf_link_loaded_list *loaded;
9375
9376 if (!is_elf_hash_table (info->hash))
9377 return FALSE;
9378
90c984fc
L
9379 /* Check indirect symbol. */
9380 while (h->root.type == bfd_link_hash_indirect)
9381 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9382
c152c796
AM
9383 switch (h->root.type)
9384 {
9385 default:
9386 abfd = NULL;
9387 break;
9388
9389 case bfd_link_hash_undefined:
9390 case bfd_link_hash_undefweak:
9391 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9392 if (abfd == NULL
9393 || (abfd->flags & DYNAMIC) == 0
e56f61be 9394 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9395 return FALSE;
9396 break;
9397
9398 case bfd_link_hash_defined:
9399 case bfd_link_hash_defweak:
9400 abfd = h->root.u.def.section->owner;
9401 break;
9402
9403 case bfd_link_hash_common:
9404 abfd = h->root.u.c.p->section->owner;
9405 break;
9406 }
9407 BFD_ASSERT (abfd != NULL);
9408
9409 for (loaded = elf_hash_table (info)->loaded;
9410 loaded != NULL;
9411 loaded = loaded->next)
9412 {
9413 bfd *input;
9414 Elf_Internal_Shdr *hdr;
ef53be89
AM
9415 size_t symcount;
9416 size_t extsymcount;
9417 size_t extsymoff;
c152c796
AM
9418 Elf_Internal_Shdr *versymhdr;
9419 Elf_Internal_Sym *isym;
9420 Elf_Internal_Sym *isymend;
9421 Elf_Internal_Sym *isymbuf;
9422 Elf_External_Versym *ever;
9423 Elf_External_Versym *extversym;
9424
9425 input = loaded->abfd;
9426
9427 /* We check each DSO for a possible hidden versioned definition. */
9428 if (input == abfd
9429 || (input->flags & DYNAMIC) == 0
9430 || elf_dynversym (input) == 0)
9431 continue;
9432
9433 hdr = &elf_tdata (input)->dynsymtab_hdr;
9434
9435 symcount = hdr->sh_size / bed->s->sizeof_sym;
9436 if (elf_bad_symtab (input))
9437 {
9438 extsymcount = symcount;
9439 extsymoff = 0;
9440 }
9441 else
9442 {
9443 extsymcount = symcount - hdr->sh_info;
9444 extsymoff = hdr->sh_info;
9445 }
9446
9447 if (extsymcount == 0)
9448 continue;
9449
9450 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9451 NULL, NULL, NULL);
9452 if (isymbuf == NULL)
9453 return FALSE;
9454
9455 /* Read in any version definitions. */
9456 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9457 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9458 if (extversym == NULL)
9459 goto error_ret;
9460
9461 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9462 || (bfd_bread (extversym, versymhdr->sh_size, input)
9463 != versymhdr->sh_size))
9464 {
9465 free (extversym);
9466 error_ret:
9467 free (isymbuf);
9468 return FALSE;
9469 }
9470
9471 ever = extversym + extsymoff;
9472 isymend = isymbuf + extsymcount;
9473 for (isym = isymbuf; isym < isymend; isym++, ever++)
9474 {
9475 const char *name;
9476 Elf_Internal_Versym iver;
9477 unsigned short version_index;
9478
9479 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9480 || isym->st_shndx == SHN_UNDEF)
9481 continue;
9482
9483 name = bfd_elf_string_from_elf_section (input,
9484 hdr->sh_link,
9485 isym->st_name);
9486 if (strcmp (name, h->root.root.string) != 0)
9487 continue;
9488
9489 _bfd_elf_swap_versym_in (input, ever, &iver);
9490
d023c380
L
9491 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9492 && !(h->def_regular
9493 && h->forced_local))
c152c796
AM
9494 {
9495 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9496 have provided a definition for the undefined sym unless
9497 it is defined in a non-shared object and forced local.
9498 */
c152c796
AM
9499 abort ();
9500 }
9501
9502 version_index = iver.vs_vers & VERSYM_VERSION;
9503 if (version_index == 1 || version_index == 2)
9504 {
9505 /* This is the base or first version. We can use it. */
9506 free (extversym);
9507 free (isymbuf);
9508 return TRUE;
9509 }
9510 }
9511
9512 free (extversym);
9513 free (isymbuf);
9514 }
9515
9516 return FALSE;
9517}
9518
b8871f35
L
9519/* Convert ELF common symbol TYPE. */
9520
9521static int
9522elf_link_convert_common_type (struct bfd_link_info *info, int type)
9523{
9524 /* Commom symbol can only appear in relocatable link. */
9525 if (!bfd_link_relocatable (info))
9526 abort ();
9527 switch (info->elf_stt_common)
9528 {
9529 case unchanged:
9530 break;
9531 case elf_stt_common:
9532 type = STT_COMMON;
9533 break;
9534 case no_elf_stt_common:
9535 type = STT_OBJECT;
9536 break;
9537 }
9538 return type;
9539}
9540
c152c796
AM
9541/* Add an external symbol to the symbol table. This is called from
9542 the hash table traversal routine. When generating a shared object,
9543 we go through the symbol table twice. The first time we output
9544 anything that might have been forced to local scope in a version
9545 script. The second time we output the symbols that are still
9546 global symbols. */
9547
9548static bfd_boolean
7686d77d 9549elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9550{
7686d77d 9551 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9552 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9553 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9554 bfd_boolean strip;
9555 Elf_Internal_Sym sym;
9556 asection *input_sec;
9557 const struct elf_backend_data *bed;
6e0b88f1
AM
9558 long indx;
9559 int ret;
b8871f35 9560 unsigned int type;
c152c796
AM
9561
9562 if (h->root.type == bfd_link_hash_warning)
9563 {
9564 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9565 if (h->root.type == bfd_link_hash_new)
9566 return TRUE;
9567 }
9568
9569 /* Decide whether to output this symbol in this pass. */
9570 if (eoinfo->localsyms)
9571 {
4deb8f71 9572 if (!h->forced_local)
c152c796
AM
9573 return TRUE;
9574 }
9575 else
9576 {
4deb8f71 9577 if (h->forced_local)
c152c796
AM
9578 return TRUE;
9579 }
9580
8b127cbc 9581 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9582
12ac1cf5 9583 if (h->root.type == bfd_link_hash_undefined)
c152c796 9584 {
12ac1cf5
NC
9585 /* If we have an undefined symbol reference here then it must have
9586 come from a shared library that is being linked in. (Undefined
98da7939
L
9587 references in regular files have already been handled unless
9588 they are in unreferenced sections which are removed by garbage
9589 collection). */
12ac1cf5
NC
9590 bfd_boolean ignore_undef = FALSE;
9591
9592 /* Some symbols may be special in that the fact that they're
9593 undefined can be safely ignored - let backend determine that. */
9594 if (bed->elf_backend_ignore_undef_symbol)
9595 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9596
9597 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9598 if (!ignore_undef
12ac1cf5 9599 && h->ref_dynamic
8b127cbc
AM
9600 && (!h->ref_regular || flinfo->info->gc_sections)
9601 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9602 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9603 (*flinfo->info->callbacks->undefined_symbol)
9604 (flinfo->info, h->root.root.string,
9605 h->ref_regular ? NULL : h->root.u.undef.abfd,
9606 NULL, 0,
9607 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9608
9609 /* Strip a global symbol defined in a discarded section. */
9610 if (h->indx == -3)
9611 return TRUE;
c152c796
AM
9612 }
9613
9614 /* We should also warn if a forced local symbol is referenced from
9615 shared libraries. */
0e1862bb 9616 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9617 && h->forced_local
9618 && h->ref_dynamic
371a5866 9619 && h->def_regular
f5385ebf 9620 && !h->dynamic_def
ee659f1f 9621 && h->ref_dynamic_nonweak
8b127cbc 9622 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9623 {
17d078c5
AM
9624 bfd *def_bfd;
9625 const char *msg;
90c984fc
L
9626 struct elf_link_hash_entry *hi = h;
9627
9628 /* Check indirect symbol. */
9629 while (hi->root.type == bfd_link_hash_indirect)
9630 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9631
9632 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9633 /* xgettext:c-format */
17d078c5
AM
9634 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9635 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9636 /* xgettext:c-format */
17d078c5
AM
9637 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9638 else
695344c0 9639 /* xgettext:c-format */
17d078c5 9640 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9641 def_bfd = flinfo->output_bfd;
90c984fc
L
9642 if (hi->root.u.def.section != bfd_abs_section_ptr)
9643 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9644 _bfd_error_handler (msg, flinfo->output_bfd,
9645 h->root.root.string, def_bfd);
17d078c5 9646 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9647 eoinfo->failed = TRUE;
9648 return FALSE;
9649 }
9650
9651 /* We don't want to output symbols that have never been mentioned by
9652 a regular file, or that we have been told to strip. However, if
9653 h->indx is set to -2, the symbol is used by a reloc and we must
9654 output it. */
d983c8c5 9655 strip = FALSE;
c152c796 9656 if (h->indx == -2)
d983c8c5 9657 ;
f5385ebf 9658 else if ((h->def_dynamic
77cfaee6
AM
9659 || h->ref_dynamic
9660 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9661 && !h->def_regular
9662 && !h->ref_regular)
c152c796 9663 strip = TRUE;
8b127cbc 9664 else if (flinfo->info->strip == strip_all)
c152c796 9665 strip = TRUE;
8b127cbc
AM
9666 else if (flinfo->info->strip == strip_some
9667 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9668 h->root.root.string, FALSE, FALSE) == NULL)
9669 strip = TRUE;
d56d55e7
AM
9670 else if ((h->root.type == bfd_link_hash_defined
9671 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9672 && ((flinfo->info->strip_discarded
dbaa2011 9673 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9674 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9675 && h->root.u.def.section->owner != NULL
d56d55e7 9676 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9677 strip = TRUE;
9e2278f5
AM
9678 else if ((h->root.type == bfd_link_hash_undefined
9679 || h->root.type == bfd_link_hash_undefweak)
9680 && h->root.u.undef.abfd != NULL
9681 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9682 strip = TRUE;
c152c796 9683
b8871f35
L
9684 type = h->type;
9685
c152c796 9686 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9687 nothing else to do. However, if it is a forced local symbol or
9688 an ifunc symbol we need to give the backend finish_dynamic_symbol
9689 function a chance to make it dynamic. */
c152c796
AM
9690 if (strip
9691 && h->dynindx == -1
b8871f35 9692 && type != STT_GNU_IFUNC
f5385ebf 9693 && !h->forced_local)
c152c796
AM
9694 return TRUE;
9695
9696 sym.st_value = 0;
9697 sym.st_size = h->size;
9698 sym.st_other = h->other;
c152c796
AM
9699 switch (h->root.type)
9700 {
9701 default:
9702 case bfd_link_hash_new:
9703 case bfd_link_hash_warning:
9704 abort ();
9705 return FALSE;
9706
9707 case bfd_link_hash_undefined:
9708 case bfd_link_hash_undefweak:
9709 input_sec = bfd_und_section_ptr;
9710 sym.st_shndx = SHN_UNDEF;
9711 break;
9712
9713 case bfd_link_hash_defined:
9714 case bfd_link_hash_defweak:
9715 {
9716 input_sec = h->root.u.def.section;
9717 if (input_sec->output_section != NULL)
9718 {
9719 sym.st_shndx =
8b127cbc 9720 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9721 input_sec->output_section);
9722 if (sym.st_shndx == SHN_BAD)
9723 {
4eca0228 9724 _bfd_error_handler
695344c0 9725 /* xgettext:c-format */
d003868e 9726 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9727 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9728 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9729 eoinfo->failed = TRUE;
9730 return FALSE;
9731 }
9732
9733 /* ELF symbols in relocatable files are section relative,
9734 but in nonrelocatable files they are virtual
9735 addresses. */
9736 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9737 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9738 {
9739 sym.st_value += input_sec->output_section->vma;
9740 if (h->type == STT_TLS)
9741 {
8b127cbc 9742 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9743 if (tls_sec != NULL)
9744 sym.st_value -= tls_sec->vma;
c152c796
AM
9745 }
9746 }
9747 }
9748 else
9749 {
9750 BFD_ASSERT (input_sec->owner == NULL
9751 || (input_sec->owner->flags & DYNAMIC) != 0);
9752 sym.st_shndx = SHN_UNDEF;
9753 input_sec = bfd_und_section_ptr;
9754 }
9755 }
9756 break;
9757
9758 case bfd_link_hash_common:
9759 input_sec = h->root.u.c.p->section;
a4d8e49b 9760 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9761 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9762 break;
9763
9764 case bfd_link_hash_indirect:
9765 /* These symbols are created by symbol versioning. They point
9766 to the decorated version of the name. For example, if the
9767 symbol foo@@GNU_1.2 is the default, which should be used when
9768 foo is used with no version, then we add an indirect symbol
9769 foo which points to foo@@GNU_1.2. We ignore these symbols,
9770 since the indirected symbol is already in the hash table. */
9771 return TRUE;
9772 }
9773
b8871f35
L
9774 if (type == STT_COMMON || type == STT_OBJECT)
9775 switch (h->root.type)
9776 {
9777 case bfd_link_hash_common:
9778 type = elf_link_convert_common_type (flinfo->info, type);
9779 break;
9780 case bfd_link_hash_defined:
9781 case bfd_link_hash_defweak:
9782 if (bed->common_definition (&sym))
9783 type = elf_link_convert_common_type (flinfo->info, type);
9784 else
9785 type = STT_OBJECT;
9786 break;
9787 case bfd_link_hash_undefined:
9788 case bfd_link_hash_undefweak:
9789 break;
9790 default:
9791 abort ();
9792 }
9793
4deb8f71 9794 if (h->forced_local)
b8871f35
L
9795 {
9796 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9797 /* Turn off visibility on local symbol. */
9798 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9799 }
9800 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9801 else if (h->unique_global && h->def_regular)
9802 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9803 else if (h->root.type == bfd_link_hash_undefweak
9804 || h->root.type == bfd_link_hash_defweak)
9805 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9806 else
9807 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9808 sym.st_target_internal = h->target_internal;
9809
c152c796
AM
9810 /* Give the processor backend a chance to tweak the symbol value,
9811 and also to finish up anything that needs to be done for this
9812 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9813 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9814 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9815 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9816 && h->def_regular
0e1862bb 9817 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9818 || ((h->dynindx != -1
9819 || h->forced_local)
0e1862bb 9820 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9821 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9822 || h->root.type != bfd_link_hash_undefweak))
9823 || !h->forced_local)
8b127cbc 9824 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9825 {
9826 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9827 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9828 {
9829 eoinfo->failed = TRUE;
9830 return FALSE;
9831 }
9832 }
9833
9834 /* If we are marking the symbol as undefined, and there are no
9835 non-weak references to this symbol from a regular object, then
9836 mark the symbol as weak undefined; if there are non-weak
9837 references, mark the symbol as strong. We can't do this earlier,
9838 because it might not be marked as undefined until the
9839 finish_dynamic_symbol routine gets through with it. */
9840 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9841 && h->ref_regular
c152c796
AM
9842 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9843 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9844 {
9845 int bindtype;
b8871f35 9846 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9847
9848 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9849 if (type == STT_GNU_IFUNC)
9850 type = STT_FUNC;
c152c796 9851
f5385ebf 9852 if (h->ref_regular_nonweak)
c152c796
AM
9853 bindtype = STB_GLOBAL;
9854 else
9855 bindtype = STB_WEAK;
2955ec4c 9856 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9857 }
9858
bda987c2
CD
9859 /* If this is a symbol defined in a dynamic library, don't use the
9860 symbol size from the dynamic library. Relinking an executable
9861 against a new library may introduce gratuitous changes in the
9862 executable's symbols if we keep the size. */
9863 if (sym.st_shndx == SHN_UNDEF
9864 && !h->def_regular
9865 && h->def_dynamic)
9866 sym.st_size = 0;
9867
c152c796
AM
9868 /* If a non-weak symbol with non-default visibility is not defined
9869 locally, it is a fatal error. */
0e1862bb 9870 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9871 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9872 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9873 && h->root.type == bfd_link_hash_undefined
f5385ebf 9874 && !h->def_regular)
c152c796 9875 {
17d078c5
AM
9876 const char *msg;
9877
9878 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9879 /* xgettext:c-format */
17d078c5
AM
9880 msg = _("%B: protected symbol `%s' isn't defined");
9881 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9882 /* xgettext:c-format */
17d078c5
AM
9883 msg = _("%B: internal symbol `%s' isn't defined");
9884 else
695344c0 9885 /* xgettext:c-format */
17d078c5 9886 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9887 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9888 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9889 eoinfo->failed = TRUE;
9890 return FALSE;
9891 }
9892
9893 /* If this symbol should be put in the .dynsym section, then put it
9894 there now. We already know the symbol index. We also fill in
9895 the entry in the .hash section. */
cae1fbbb 9896 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9897 && h->dynindx != -1
8b127cbc 9898 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9899 {
c152c796
AM
9900 bfd_byte *esym;
9901
90c984fc
L
9902 /* Since there is no version information in the dynamic string,
9903 if there is no version info in symbol version section, we will
1659f720 9904 have a run-time problem if not linking executable, referenced
4deb8f71 9905 by shared library, or not bound locally. */
1659f720 9906 if (h->verinfo.verdef == NULL
0e1862bb 9907 && (!bfd_link_executable (flinfo->info)
1659f720
L
9908 || h->ref_dynamic
9909 || !h->def_regular))
90c984fc
L
9910 {
9911 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9912
9913 if (p && p [1] != '\0')
9914 {
4eca0228 9915 _bfd_error_handler
695344c0 9916 /* xgettext:c-format */
90c984fc
L
9917 (_("%B: No symbol version section for versioned symbol `%s'"),
9918 flinfo->output_bfd, h->root.root.string);
9919 eoinfo->failed = TRUE;
9920 return FALSE;
9921 }
9922 }
9923
c152c796 9924 sym.st_name = h->dynstr_index;
cae1fbbb
L
9925 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9926 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9927 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9928 {
9929 eoinfo->failed = TRUE;
9930 return FALSE;
9931 }
8b127cbc 9932 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9933
8b127cbc 9934 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9935 {
9936 size_t hash_entry_size;
9937 bfd_byte *bucketpos;
9938 bfd_vma chain;
41198d0c
L
9939 size_t bucketcount;
9940 size_t bucket;
9941
8b127cbc 9942 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9943 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9944
9945 hash_entry_size
8b127cbc
AM
9946 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9947 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9948 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9949 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9950 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9951 bucketpos);
9952 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9953 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9954 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9955 }
c152c796 9956
8b127cbc 9957 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9958 {
9959 Elf_Internal_Versym iversym;
9960 Elf_External_Versym *eversym;
9961
f5385ebf 9962 if (!h->def_regular)
c152c796 9963 {
7b20f099
AM
9964 if (h->verinfo.verdef == NULL
9965 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9966 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9967 iversym.vs_vers = 0;
9968 else
9969 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9970 }
9971 else
9972 {
9973 if (h->verinfo.vertree == NULL)
9974 iversym.vs_vers = 1;
9975 else
9976 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9977 if (flinfo->info->create_default_symver)
3e3b46e5 9978 iversym.vs_vers++;
c152c796
AM
9979 }
9980
422f1182 9981 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9982 defined locally. */
422f1182 9983 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9984 iversym.vs_vers |= VERSYM_HIDDEN;
9985
8b127cbc 9986 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9987 eversym += h->dynindx;
8b127cbc 9988 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9989 }
9990 }
9991
d983c8c5
AM
9992 /* If the symbol is undefined, and we didn't output it to .dynsym,
9993 strip it from .symtab too. Obviously we can't do this for
9994 relocatable output or when needed for --emit-relocs. */
9995 else if (input_sec == bfd_und_section_ptr
9996 && h->indx != -2
66cae560
NC
9997 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9998 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 9999 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10000 return TRUE;
66cae560 10001
d983c8c5
AM
10002 /* Also strip others that we couldn't earlier due to dynamic symbol
10003 processing. */
10004 if (strip)
10005 return TRUE;
10006 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10007 return TRUE;
10008
2ec55de3
AM
10009 /* Output a FILE symbol so that following locals are not associated
10010 with the wrong input file. We need one for forced local symbols
10011 if we've seen more than one FILE symbol or when we have exactly
10012 one FILE symbol but global symbols are present in a file other
10013 than the one with the FILE symbol. We also need one if linker
10014 defined symbols are present. In practice these conditions are
10015 always met, so just emit the FILE symbol unconditionally. */
10016 if (eoinfo->localsyms
10017 && !eoinfo->file_sym_done
10018 && eoinfo->flinfo->filesym_count != 0)
10019 {
10020 Elf_Internal_Sym fsym;
10021
10022 memset (&fsym, 0, sizeof (fsym));
10023 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10024 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10025 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10026 bfd_und_section_ptr, NULL))
2ec55de3
AM
10027 return FALSE;
10028
10029 eoinfo->file_sym_done = TRUE;
10030 }
10031
8b127cbc 10032 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10033 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10034 input_sec, h);
6e0b88f1 10035 if (ret == 0)
c152c796
AM
10036 {
10037 eoinfo->failed = TRUE;
10038 return FALSE;
10039 }
6e0b88f1
AM
10040 else if (ret == 1)
10041 h->indx = indx;
10042 else if (h->indx == -2)
10043 abort();
c152c796
AM
10044
10045 return TRUE;
10046}
10047
cdd3575c
AM
10048/* Return TRUE if special handling is done for relocs in SEC against
10049 symbols defined in discarded sections. */
10050
c152c796
AM
10051static bfd_boolean
10052elf_section_ignore_discarded_relocs (asection *sec)
10053{
10054 const struct elf_backend_data *bed;
10055
cdd3575c
AM
10056 switch (sec->sec_info_type)
10057 {
dbaa2011
AM
10058 case SEC_INFO_TYPE_STABS:
10059 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10060 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10061 return TRUE;
10062 default:
10063 break;
10064 }
c152c796
AM
10065
10066 bed = get_elf_backend_data (sec->owner);
10067 if (bed->elf_backend_ignore_discarded_relocs != NULL
10068 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10069 return TRUE;
10070
10071 return FALSE;
10072}
10073
9e66c942
AM
10074/* Return a mask saying how ld should treat relocations in SEC against
10075 symbols defined in discarded sections. If this function returns
10076 COMPLAIN set, ld will issue a warning message. If this function
10077 returns PRETEND set, and the discarded section was link-once and the
10078 same size as the kept link-once section, ld will pretend that the
10079 symbol was actually defined in the kept section. Otherwise ld will
10080 zero the reloc (at least that is the intent, but some cooperation by
10081 the target dependent code is needed, particularly for REL targets). */
10082
8a696751
AM
10083unsigned int
10084_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10085{
9e66c942 10086 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10087 return PRETEND;
cdd3575c
AM
10088
10089 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10090 return 0;
cdd3575c
AM
10091
10092 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10093 return 0;
cdd3575c 10094
9e66c942 10095 return COMPLAIN | PRETEND;
cdd3575c
AM
10096}
10097
3d7f7666
L
10098/* Find a match between a section and a member of a section group. */
10099
10100static asection *
c0f00686
L
10101match_group_member (asection *sec, asection *group,
10102 struct bfd_link_info *info)
3d7f7666
L
10103{
10104 asection *first = elf_next_in_group (group);
10105 asection *s = first;
10106
10107 while (s != NULL)
10108 {
c0f00686 10109 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10110 return s;
10111
83180ade 10112 s = elf_next_in_group (s);
3d7f7666
L
10113 if (s == first)
10114 break;
10115 }
10116
10117 return NULL;
10118}
10119
01b3c8ab 10120/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10121 to replace it. Return the replacement if it is OK. Otherwise return
10122 NULL. */
01b3c8ab
L
10123
10124asection *
c0f00686 10125_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10126{
10127 asection *kept;
10128
10129 kept = sec->kept_section;
10130 if (kept != NULL)
10131 {
c2370991 10132 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10133 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10134 if (kept != NULL
10135 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10136 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10137 kept = NULL;
c2370991 10138 sec->kept_section = kept;
01b3c8ab
L
10139 }
10140 return kept;
10141}
10142
c152c796
AM
10143/* Link an input file into the linker output file. This function
10144 handles all the sections and relocations of the input file at once.
10145 This is so that we only have to read the local symbols once, and
10146 don't have to keep them in memory. */
10147
10148static bfd_boolean
8b127cbc 10149elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10150{
ece5ef60 10151 int (*relocate_section)
c152c796
AM
10152 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10153 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10154 bfd *output_bfd;
10155 Elf_Internal_Shdr *symtab_hdr;
10156 size_t locsymcount;
10157 size_t extsymoff;
10158 Elf_Internal_Sym *isymbuf;
10159 Elf_Internal_Sym *isym;
10160 Elf_Internal_Sym *isymend;
10161 long *pindex;
10162 asection **ppsection;
10163 asection *o;
10164 const struct elf_backend_data *bed;
c152c796 10165 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10166 bfd_size_type address_size;
10167 bfd_vma r_type_mask;
10168 int r_sym_shift;
ffbc01cc 10169 bfd_boolean have_file_sym = FALSE;
c152c796 10170
8b127cbc 10171 output_bfd = flinfo->output_bfd;
c152c796
AM
10172 bed = get_elf_backend_data (output_bfd);
10173 relocate_section = bed->elf_backend_relocate_section;
10174
10175 /* If this is a dynamic object, we don't want to do anything here:
10176 we don't want the local symbols, and we don't want the section
10177 contents. */
10178 if ((input_bfd->flags & DYNAMIC) != 0)
10179 return TRUE;
10180
c152c796
AM
10181 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10182 if (elf_bad_symtab (input_bfd))
10183 {
10184 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10185 extsymoff = 0;
10186 }
10187 else
10188 {
10189 locsymcount = symtab_hdr->sh_info;
10190 extsymoff = symtab_hdr->sh_info;
10191 }
10192
10193 /* Read the local symbols. */
10194 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10195 if (isymbuf == NULL && locsymcount != 0)
10196 {
10197 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10198 flinfo->internal_syms,
10199 flinfo->external_syms,
10200 flinfo->locsym_shndx);
c152c796
AM
10201 if (isymbuf == NULL)
10202 return FALSE;
10203 }
10204
10205 /* Find local symbol sections and adjust values of symbols in
10206 SEC_MERGE sections. Write out those local symbols we know are
10207 going into the output file. */
10208 isymend = isymbuf + locsymcount;
8b127cbc 10209 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10210 isym < isymend;
10211 isym++, pindex++, ppsection++)
10212 {
10213 asection *isec;
10214 const char *name;
10215 Elf_Internal_Sym osym;
6e0b88f1
AM
10216 long indx;
10217 int ret;
c152c796
AM
10218
10219 *pindex = -1;
10220
10221 if (elf_bad_symtab (input_bfd))
10222 {
10223 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10224 {
10225 *ppsection = NULL;
10226 continue;
10227 }
10228 }
10229
10230 if (isym->st_shndx == SHN_UNDEF)
10231 isec = bfd_und_section_ptr;
c152c796
AM
10232 else if (isym->st_shndx == SHN_ABS)
10233 isec = bfd_abs_section_ptr;
10234 else if (isym->st_shndx == SHN_COMMON)
10235 isec = bfd_com_section_ptr;
10236 else
10237 {
cb33740c
AM
10238 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10239 if (isec == NULL)
10240 {
10241 /* Don't attempt to output symbols with st_shnx in the
10242 reserved range other than SHN_ABS and SHN_COMMON. */
10243 *ppsection = NULL;
10244 continue;
10245 }
dbaa2011 10246 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10247 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10248 isym->st_value =
10249 _bfd_merged_section_offset (output_bfd, &isec,
10250 elf_section_data (isec)->sec_info,
10251 isym->st_value);
c152c796
AM
10252 }
10253
10254 *ppsection = isec;
10255
d983c8c5
AM
10256 /* Don't output the first, undefined, symbol. In fact, don't
10257 output any undefined local symbol. */
10258 if (isec == bfd_und_section_ptr)
c152c796
AM
10259 continue;
10260
10261 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10262 {
10263 /* We never output section symbols. Instead, we use the
10264 section symbol of the corresponding section in the output
10265 file. */
10266 continue;
10267 }
10268
10269 /* If we are stripping all symbols, we don't want to output this
10270 one. */
8b127cbc 10271 if (flinfo->info->strip == strip_all)
c152c796
AM
10272 continue;
10273
10274 /* If we are discarding all local symbols, we don't want to
10275 output this one. If we are generating a relocatable output
10276 file, then some of the local symbols may be required by
10277 relocs; we output them below as we discover that they are
10278 needed. */
8b127cbc 10279 if (flinfo->info->discard == discard_all)
c152c796
AM
10280 continue;
10281
10282 /* If this symbol is defined in a section which we are
f02571c5
AM
10283 discarding, we don't need to keep it. */
10284 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10285 && isym->st_shndx < SHN_LORESERVE
10286 && bfd_section_removed_from_list (output_bfd,
10287 isec->output_section))
e75a280b
L
10288 continue;
10289
c152c796
AM
10290 /* Get the name of the symbol. */
10291 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10292 isym->st_name);
10293 if (name == NULL)
10294 return FALSE;
10295
10296 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10297 if ((flinfo->info->strip == strip_some
10298 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10299 == NULL))
8b127cbc 10300 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10301 && (isec->flags & SEC_MERGE)
10302 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10303 || flinfo->info->discard == discard_l)
c152c796
AM
10304 && bfd_is_local_label_name (input_bfd, name)))
10305 continue;
10306
ffbc01cc
AM
10307 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10308 {
ce875075
AM
10309 if (input_bfd->lto_output)
10310 /* -flto puts a temp file name here. This means builds
10311 are not reproducible. Discard the symbol. */
10312 continue;
ffbc01cc
AM
10313 have_file_sym = TRUE;
10314 flinfo->filesym_count += 1;
10315 }
10316 if (!have_file_sym)
10317 {
10318 /* In the absence of debug info, bfd_find_nearest_line uses
10319 FILE symbols to determine the source file for local
10320 function symbols. Provide a FILE symbol here if input
10321 files lack such, so that their symbols won't be
10322 associated with a previous input file. It's not the
10323 source file, but the best we can do. */
10324 have_file_sym = TRUE;
10325 flinfo->filesym_count += 1;
10326 memset (&osym, 0, sizeof (osym));
10327 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10328 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10329 if (!elf_link_output_symstrtab (flinfo,
10330 (input_bfd->lto_output ? NULL
10331 : input_bfd->filename),
10332 &osym, bfd_abs_section_ptr,
10333 NULL))
ffbc01cc
AM
10334 return FALSE;
10335 }
10336
c152c796
AM
10337 osym = *isym;
10338
10339 /* Adjust the section index for the output file. */
10340 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10341 isec->output_section);
10342 if (osym.st_shndx == SHN_BAD)
10343 return FALSE;
10344
c152c796
AM
10345 /* ELF symbols in relocatable files are section relative, but
10346 in executable files they are virtual addresses. Note that
10347 this code assumes that all ELF sections have an associated
10348 BFD section with a reasonable value for output_offset; below
10349 we assume that they also have a reasonable value for
10350 output_section. Any special sections must be set up to meet
10351 these requirements. */
10352 osym.st_value += isec->output_offset;
0e1862bb 10353 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10354 {
10355 osym.st_value += isec->output_section->vma;
10356 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10357 {
10358 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10359 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10360 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10361 }
10362 }
10363
6e0b88f1 10364 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10365 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10366 if (ret == 0)
c152c796 10367 return FALSE;
6e0b88f1
AM
10368 else if (ret == 1)
10369 *pindex = indx;
c152c796
AM
10370 }
10371
310fd250
L
10372 if (bed->s->arch_size == 32)
10373 {
10374 r_type_mask = 0xff;
10375 r_sym_shift = 8;
10376 address_size = 4;
10377 }
10378 else
10379 {
10380 r_type_mask = 0xffffffff;
10381 r_sym_shift = 32;
10382 address_size = 8;
10383 }
10384
c152c796
AM
10385 /* Relocate the contents of each section. */
10386 sym_hashes = elf_sym_hashes (input_bfd);
10387 for (o = input_bfd->sections; o != NULL; o = o->next)
10388 {
10389 bfd_byte *contents;
10390
10391 if (! o->linker_mark)
10392 {
10393 /* This section was omitted from the link. */
10394 continue;
10395 }
10396
7bdf4127 10397 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10398 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10399 {
10400 /* Deal with the group signature symbol. */
10401 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10402 unsigned long symndx = sec_data->this_hdr.sh_info;
10403 asection *osec = o->output_section;
10404
7bdf4127 10405 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10406 if (symndx >= locsymcount
10407 || (elf_bad_symtab (input_bfd)
8b127cbc 10408 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10409 {
10410 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10411 while (h->root.type == bfd_link_hash_indirect
10412 || h->root.type == bfd_link_hash_warning)
10413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10414 /* Arrange for symbol to be output. */
10415 h->indx = -2;
10416 elf_section_data (osec)->this_hdr.sh_info = -2;
10417 }
10418 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10419 {
10420 /* We'll use the output section target_index. */
8b127cbc 10421 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10422 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10423 }
10424 else
10425 {
8b127cbc 10426 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10427 {
10428 /* Otherwise output the local symbol now. */
10429 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10430 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10431 const char *name;
6e0b88f1
AM
10432 long indx;
10433 int ret;
bcacc0f5
AM
10434
10435 name = bfd_elf_string_from_elf_section (input_bfd,
10436 symtab_hdr->sh_link,
10437 sym.st_name);
10438 if (name == NULL)
10439 return FALSE;
10440
10441 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10442 sec);
10443 if (sym.st_shndx == SHN_BAD)
10444 return FALSE;
10445
10446 sym.st_value += o->output_offset;
10447
6e0b88f1 10448 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10449 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10450 NULL);
6e0b88f1 10451 if (ret == 0)
bcacc0f5 10452 return FALSE;
6e0b88f1 10453 else if (ret == 1)
8b127cbc 10454 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10455 else
10456 abort ();
bcacc0f5
AM
10457 }
10458 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10459 = flinfo->indices[symndx];
bcacc0f5
AM
10460 }
10461 }
10462
c152c796 10463 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10464 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10465 continue;
10466
10467 if ((o->flags & SEC_LINKER_CREATED) != 0)
10468 {
10469 /* Section was created by _bfd_elf_link_create_dynamic_sections
10470 or somesuch. */
10471 continue;
10472 }
10473
10474 /* Get the contents of the section. They have been cached by a
10475 relaxation routine. Note that o is a section in an input
10476 file, so the contents field will not have been set by any of
10477 the routines which work on output files. */
10478 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10479 {
10480 contents = elf_section_data (o)->this_hdr.contents;
10481 if (bed->caches_rawsize
10482 && o->rawsize != 0
10483 && o->rawsize < o->size)
10484 {
10485 memcpy (flinfo->contents, contents, o->rawsize);
10486 contents = flinfo->contents;
10487 }
10488 }
c152c796
AM
10489 else
10490 {
8b127cbc 10491 contents = flinfo->contents;
4a114e3e 10492 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10493 return FALSE;
10494 }
10495
10496 if ((o->flags & SEC_RELOC) != 0)
10497 {
10498 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10499 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10500 int action_discarded;
ece5ef60 10501 int ret;
c152c796
AM
10502
10503 /* Get the swapped relocs. */
10504 internal_relocs
8b127cbc
AM
10505 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10506 flinfo->internal_relocs, FALSE);
c152c796
AM
10507 if (internal_relocs == NULL
10508 && o->reloc_count > 0)
10509 return FALSE;
10510
310fd250
L
10511 /* We need to reverse-copy input .ctors/.dtors sections if
10512 they are placed in .init_array/.finit_array for output. */
10513 if (o->size > address_size
10514 && ((strncmp (o->name, ".ctors", 6) == 0
10515 && strcmp (o->output_section->name,
10516 ".init_array") == 0)
10517 || (strncmp (o->name, ".dtors", 6) == 0
10518 && strcmp (o->output_section->name,
10519 ".fini_array") == 0))
10520 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10521 {
056bafd4
MR
10522 if (o->size * bed->s->int_rels_per_ext_rel
10523 != o->reloc_count * address_size)
310fd250 10524 {
4eca0228 10525 _bfd_error_handler
695344c0 10526 /* xgettext:c-format */
310fd250
L
10527 (_("error: %B: size of section %A is not "
10528 "multiple of address size"),
10529 input_bfd, o);
8c6716e5 10530 bfd_set_error (bfd_error_bad_value);
310fd250
L
10531 return FALSE;
10532 }
10533 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10534 }
10535
0f02bbd9 10536 action_discarded = -1;
c152c796 10537 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10538 action_discarded = (*bed->action_discarded) (o);
10539
10540 /* Run through the relocs evaluating complex reloc symbols and
10541 looking for relocs against symbols from discarded sections
10542 or section symbols from removed link-once sections.
10543 Complain about relocs against discarded sections. Zero
10544 relocs against removed link-once sections. */
10545
10546 rel = internal_relocs;
056bafd4 10547 relend = rel + o->reloc_count;
0f02bbd9 10548 for ( ; rel < relend; rel++)
c152c796 10549 {
0f02bbd9
AM
10550 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10551 unsigned int s_type;
10552 asection **ps, *sec;
10553 struct elf_link_hash_entry *h = NULL;
10554 const char *sym_name;
c152c796 10555
0f02bbd9
AM
10556 if (r_symndx == STN_UNDEF)
10557 continue;
c152c796 10558
0f02bbd9
AM
10559 if (r_symndx >= locsymcount
10560 || (elf_bad_symtab (input_bfd)
8b127cbc 10561 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10562 {
10563 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10564
0f02bbd9
AM
10565 /* Badly formatted input files can contain relocs that
10566 reference non-existant symbols. Check here so that
10567 we do not seg fault. */
10568 if (h == NULL)
c152c796 10569 {
4eca0228 10570 _bfd_error_handler
695344c0 10571 /* xgettext:c-format */
76cfced5 10572 (_("error: %B contains a reloc (%#Lx) for section %A "
0f02bbd9 10573 "that references a non-existent global symbol"),
76cfced5 10574 input_bfd, rel->r_info, o);
0f02bbd9
AM
10575 bfd_set_error (bfd_error_bad_value);
10576 return FALSE;
10577 }
3b36f7e6 10578
0f02bbd9
AM
10579 while (h->root.type == bfd_link_hash_indirect
10580 || h->root.type == bfd_link_hash_warning)
10581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10582
0f02bbd9 10583 s_type = h->type;
cdd3575c 10584
9e2dec47 10585 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10586 mark the symbol as undefined. Note that the
10587 linker may attach linker created dynamic sections
10588 to the plugin bfd. Symbols defined in linker
10589 created sections are not plugin symbols. */
bc4e12de 10590 if ((h->root.non_ir_ref_regular
4070765b 10591 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10592 && (h->root.type == bfd_link_hash_defined
10593 || h->root.type == bfd_link_hash_defweak)
10594 && (h->root.u.def.section->flags
10595 & SEC_LINKER_CREATED) == 0
10596 && h->root.u.def.section->owner != NULL
10597 && (h->root.u.def.section->owner->flags
10598 & BFD_PLUGIN) != 0)
10599 {
10600 h->root.type = bfd_link_hash_undefined;
10601 h->root.u.undef.abfd = h->root.u.def.section->owner;
10602 }
10603
0f02bbd9
AM
10604 ps = NULL;
10605 if (h->root.type == bfd_link_hash_defined
10606 || h->root.type == bfd_link_hash_defweak)
10607 ps = &h->root.u.def.section;
10608
10609 sym_name = h->root.root.string;
10610 }
10611 else
10612 {
10613 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10614
10615 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10616 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10617 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10618 sym, *ps);
10619 }
c152c796 10620
c301e700 10621 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10622 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10623 {
10624 bfd_vma val;
10625 bfd_vma dot = (rel->r_offset
10626 + o->output_offset + o->output_section->vma);
10627#ifdef DEBUG
10628 printf ("Encountered a complex symbol!");
10629 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10630 input_bfd->filename, o->name,
10631 (long) (rel - internal_relocs));
0f02bbd9
AM
10632 printf (" symbol: idx %8.8lx, name %s\n",
10633 r_symndx, sym_name);
10634 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10635 (unsigned long) rel->r_info,
10636 (unsigned long) rel->r_offset);
10637#endif
8b127cbc 10638 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10639 isymbuf, locsymcount, s_type == STT_SRELC))
10640 return FALSE;
10641
10642 /* Symbol evaluated OK. Update to absolute value. */
10643 set_symbol_value (input_bfd, isymbuf, locsymcount,
10644 r_symndx, val);
10645 continue;
10646 }
10647
10648 if (action_discarded != -1 && ps != NULL)
10649 {
cdd3575c
AM
10650 /* Complain if the definition comes from a
10651 discarded section. */
dbaa2011 10652 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10653 {
cf35638d 10654 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10655 if (action_discarded & COMPLAIN)
8b127cbc 10656 (*flinfo->info->callbacks->einfo)
695344c0 10657 /* xgettext:c-format */
e1fffbe6 10658 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10659 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10660 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10661
87e5235d 10662 /* Try to do the best we can to support buggy old
e0ae6d6f 10663 versions of gcc. Pretend that the symbol is
87e5235d
AM
10664 really defined in the kept linkonce section.
10665 FIXME: This is quite broken. Modifying the
10666 symbol here means we will be changing all later
e0ae6d6f 10667 uses of the symbol, not just in this section. */
0f02bbd9 10668 if (action_discarded & PRETEND)
87e5235d 10669 {
01b3c8ab
L
10670 asection *kept;
10671
c0f00686 10672 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10673 flinfo->info);
01b3c8ab 10674 if (kept != NULL)
87e5235d
AM
10675 {
10676 *ps = kept;
10677 continue;
10678 }
10679 }
c152c796
AM
10680 }
10681 }
10682 }
10683
10684 /* Relocate the section by invoking a back end routine.
10685
10686 The back end routine is responsible for adjusting the
10687 section contents as necessary, and (if using Rela relocs
10688 and generating a relocatable output file) adjusting the
10689 reloc addend as necessary.
10690
10691 The back end routine does not have to worry about setting
10692 the reloc address or the reloc symbol index.
10693
10694 The back end routine is given a pointer to the swapped in
10695 internal symbols, and can access the hash table entries
10696 for the external symbols via elf_sym_hashes (input_bfd).
10697
10698 When generating relocatable output, the back end routine
10699 must handle STB_LOCAL/STT_SECTION symbols specially. The
10700 output symbol is going to be a section symbol
10701 corresponding to the output section, which will require
10702 the addend to be adjusted. */
10703
8b127cbc 10704 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10705 input_bfd, o, contents,
10706 internal_relocs,
10707 isymbuf,
8b127cbc 10708 flinfo->sections);
ece5ef60 10709 if (!ret)
c152c796
AM
10710 return FALSE;
10711
ece5ef60 10712 if (ret == 2
0e1862bb 10713 || bfd_link_relocatable (flinfo->info)
8b127cbc 10714 || flinfo->info->emitrelocations)
c152c796
AM
10715 {
10716 Elf_Internal_Rela *irela;
d4730f92 10717 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10718 bfd_vma last_offset;
10719 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10720 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10721 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10722 unsigned int next_erel;
c152c796 10723 bfd_boolean rela_normal;
d4730f92 10724 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10725
d4730f92
BS
10726 esdi = elf_section_data (o);
10727 esdo = elf_section_data (o->output_section);
10728 rela_normal = FALSE;
c152c796
AM
10729
10730 /* Adjust the reloc addresses and symbol indices. */
10731
10732 irela = internal_relocs;
056bafd4 10733 irelaend = irela + o->reloc_count;
d4730f92
BS
10734 rel_hash = esdo->rel.hashes + esdo->rel.count;
10735 /* We start processing the REL relocs, if any. When we reach
10736 IRELAMID in the loop, we switch to the RELA relocs. */
10737 irelamid = irela;
10738 if (esdi->rel.hdr != NULL)
10739 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10740 * bed->s->int_rels_per_ext_rel);
eac338cf 10741 rel_hash_list = rel_hash;
d4730f92 10742 rela_hash_list = NULL;
c152c796 10743 last_offset = o->output_offset;
0e1862bb 10744 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10745 last_offset += o->output_section->vma;
10746 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10747 {
10748 unsigned long r_symndx;
10749 asection *sec;
10750 Elf_Internal_Sym sym;
10751
10752 if (next_erel == bed->s->int_rels_per_ext_rel)
10753 {
10754 rel_hash++;
10755 next_erel = 0;
10756 }
10757
d4730f92
BS
10758 if (irela == irelamid)
10759 {
10760 rel_hash = esdo->rela.hashes + esdo->rela.count;
10761 rela_hash_list = rel_hash;
10762 rela_normal = bed->rela_normal;
10763 }
10764
c152c796 10765 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10766 flinfo->info, o,
c152c796
AM
10767 irela->r_offset);
10768 if (irela->r_offset >= (bfd_vma) -2)
10769 {
10770 /* This is a reloc for a deleted entry or somesuch.
10771 Turn it into an R_*_NONE reloc, at the same
10772 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10773 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10774 being ordered. */
10775 irela->r_offset = last_offset;
10776 irela->r_info = 0;
10777 irela->r_addend = 0;
10778 continue;
10779 }
10780
10781 irela->r_offset += o->output_offset;
10782
10783 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10784 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10785 irela->r_offset += o->output_section->vma;
10786
10787 last_offset = irela->r_offset;
10788
10789 r_symndx = irela->r_info >> r_sym_shift;
10790 if (r_symndx == STN_UNDEF)
10791 continue;
10792
10793 if (r_symndx >= locsymcount
10794 || (elf_bad_symtab (input_bfd)
8b127cbc 10795 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10796 {
10797 struct elf_link_hash_entry *rh;
10798 unsigned long indx;
10799
10800 /* This is a reloc against a global symbol. We
10801 have not yet output all the local symbols, so
10802 we do not know the symbol index of any global
10803 symbol. We set the rel_hash entry for this
10804 reloc to point to the global hash table entry
10805 for this symbol. The symbol index is then
ee75fd95 10806 set at the end of bfd_elf_final_link. */
c152c796
AM
10807 indx = r_symndx - extsymoff;
10808 rh = elf_sym_hashes (input_bfd)[indx];
10809 while (rh->root.type == bfd_link_hash_indirect
10810 || rh->root.type == bfd_link_hash_warning)
10811 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10812
10813 /* Setting the index to -2 tells
10814 elf_link_output_extsym that this symbol is
10815 used by a reloc. */
10816 BFD_ASSERT (rh->indx < 0);
10817 rh->indx = -2;
c152c796
AM
10818 *rel_hash = rh;
10819
10820 continue;
10821 }
10822
10823 /* This is a reloc against a local symbol. */
10824
10825 *rel_hash = NULL;
10826 sym = isymbuf[r_symndx];
8b127cbc 10827 sec = flinfo->sections[r_symndx];
c152c796
AM
10828 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10829 {
10830 /* I suppose the backend ought to fill in the
10831 section of any STT_SECTION symbol against a
6a8d1586 10832 processor specific section. */
cf35638d 10833 r_symndx = STN_UNDEF;
6a8d1586
AM
10834 if (bfd_is_abs_section (sec))
10835 ;
c152c796
AM
10836 else if (sec == NULL || sec->owner == NULL)
10837 {
10838 bfd_set_error (bfd_error_bad_value);
10839 return FALSE;
10840 }
10841 else
10842 {
6a8d1586
AM
10843 asection *osec = sec->output_section;
10844
10845 /* If we have discarded a section, the output
10846 section will be the absolute section. In
ab96bf03
AM
10847 case of discarded SEC_MERGE sections, use
10848 the kept section. relocate_section should
10849 have already handled discarded linkonce
10850 sections. */
6a8d1586
AM
10851 if (bfd_is_abs_section (osec)
10852 && sec->kept_section != NULL
10853 && sec->kept_section->output_section != NULL)
10854 {
10855 osec = sec->kept_section->output_section;
10856 irela->r_addend -= osec->vma;
10857 }
10858
10859 if (!bfd_is_abs_section (osec))
10860 {
10861 r_symndx = osec->target_index;
cf35638d 10862 if (r_symndx == STN_UNDEF)
74541ad4 10863 {
051d833a
AM
10864 irela->r_addend += osec->vma;
10865 osec = _bfd_nearby_section (output_bfd, osec,
10866 osec->vma);
10867 irela->r_addend -= osec->vma;
10868 r_symndx = osec->target_index;
74541ad4 10869 }
6a8d1586 10870 }
c152c796
AM
10871 }
10872
10873 /* Adjust the addend according to where the
10874 section winds up in the output section. */
10875 if (rela_normal)
10876 irela->r_addend += sec->output_offset;
10877 }
10878 else
10879 {
8b127cbc 10880 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10881 {
10882 unsigned long shlink;
10883 const char *name;
10884 asection *osec;
6e0b88f1 10885 long indx;
c152c796 10886
8b127cbc 10887 if (flinfo->info->strip == strip_all)
c152c796
AM
10888 {
10889 /* You can't do ld -r -s. */
10890 bfd_set_error (bfd_error_invalid_operation);
10891 return FALSE;
10892 }
10893
10894 /* This symbol was skipped earlier, but
10895 since it is needed by a reloc, we
10896 must output it now. */
10897 shlink = symtab_hdr->sh_link;
10898 name = (bfd_elf_string_from_elf_section
10899 (input_bfd, shlink, sym.st_name));
10900 if (name == NULL)
10901 return FALSE;
10902
10903 osec = sec->output_section;
10904 sym.st_shndx =
10905 _bfd_elf_section_from_bfd_section (output_bfd,
10906 osec);
10907 if (sym.st_shndx == SHN_BAD)
10908 return FALSE;
10909
10910 sym.st_value += sec->output_offset;
0e1862bb 10911 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10912 {
10913 sym.st_value += osec->vma;
10914 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10915 {
10916 /* STT_TLS symbols are relative to PT_TLS
10917 segment base. */
8b127cbc 10918 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10919 ->tls_sec != NULL);
8b127cbc 10920 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10921 ->tls_sec->vma);
10922 }
10923 }
10924
6e0b88f1 10925 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10926 ret = elf_link_output_symstrtab (flinfo, name,
10927 &sym, sec,
10928 NULL);
6e0b88f1 10929 if (ret == 0)
c152c796 10930 return FALSE;
6e0b88f1 10931 else if (ret == 1)
8b127cbc 10932 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10933 else
10934 abort ();
c152c796
AM
10935 }
10936
8b127cbc 10937 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10938 }
10939
10940 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10941 | (irela->r_info & r_type_mask));
10942 }
10943
10944 /* Swap out the relocs. */
d4730f92
BS
10945 input_rel_hdr = esdi->rel.hdr;
10946 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10947 {
d4730f92
BS
10948 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10949 input_rel_hdr,
10950 internal_relocs,
10951 rel_hash_list))
10952 return FALSE;
c152c796
AM
10953 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10954 * bed->s->int_rels_per_ext_rel);
eac338cf 10955 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10956 }
10957
10958 input_rela_hdr = esdi->rela.hdr;
10959 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10960 {
eac338cf 10961 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10962 input_rela_hdr,
eac338cf 10963 internal_relocs,
d4730f92 10964 rela_hash_list))
c152c796
AM
10965 return FALSE;
10966 }
10967 }
10968 }
10969
10970 /* Write out the modified section contents. */
10971 if (bed->elf_backend_write_section
8b127cbc 10972 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10973 contents))
c152c796
AM
10974 {
10975 /* Section written out. */
10976 }
10977 else switch (o->sec_info_type)
10978 {
dbaa2011 10979 case SEC_INFO_TYPE_STABS:
c152c796
AM
10980 if (! (_bfd_write_section_stabs
10981 (output_bfd,
8b127cbc 10982 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10983 o, &elf_section_data (o)->sec_info, contents)))
10984 return FALSE;
10985 break;
dbaa2011 10986 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10987 if (! _bfd_write_merged_section (output_bfd, o,
10988 elf_section_data (o)->sec_info))
10989 return FALSE;
10990 break;
dbaa2011 10991 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10992 {
8b127cbc 10993 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10994 o, contents))
10995 return FALSE;
10996 }
10997 break;
2f0c68f2
CM
10998 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10999 {
11000 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11001 flinfo->info,
11002 o, contents))
11003 return FALSE;
11004 }
11005 break;
c152c796
AM
11006 default:
11007 {
310fd250
L
11008 if (! (o->flags & SEC_EXCLUDE))
11009 {
11010 file_ptr offset = (file_ptr) o->output_offset;
11011 bfd_size_type todo = o->size;
37b01f6a
DG
11012
11013 offset *= bfd_octets_per_byte (output_bfd);
11014
310fd250
L
11015 if ((o->flags & SEC_ELF_REVERSE_COPY))
11016 {
11017 /* Reverse-copy input section to output. */
11018 do
11019 {
11020 todo -= address_size;
11021 if (! bfd_set_section_contents (output_bfd,
11022 o->output_section,
11023 contents + todo,
11024 offset,
11025 address_size))
11026 return FALSE;
11027 if (todo == 0)
11028 break;
11029 offset += address_size;
11030 }
11031 while (1);
11032 }
11033 else if (! bfd_set_section_contents (output_bfd,
11034 o->output_section,
11035 contents,
11036 offset, todo))
11037 return FALSE;
11038 }
c152c796
AM
11039 }
11040 break;
11041 }
11042 }
11043
11044 return TRUE;
11045}
11046
11047/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11048 requested by the linker, and does not come from any input file. This
c152c796
AM
11049 is used to build constructor and destructor tables when linking
11050 with -Ur. */
11051
11052static bfd_boolean
11053elf_reloc_link_order (bfd *output_bfd,
11054 struct bfd_link_info *info,
11055 asection *output_section,
11056 struct bfd_link_order *link_order)
11057{
11058 reloc_howto_type *howto;
11059 long indx;
11060 bfd_vma offset;
11061 bfd_vma addend;
d4730f92 11062 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11063 struct elf_link_hash_entry **rel_hash_ptr;
11064 Elf_Internal_Shdr *rel_hdr;
11065 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11066 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11067 bfd_byte *erel;
11068 unsigned int i;
d4730f92 11069 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11070
11071 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11072 if (howto == NULL)
11073 {
11074 bfd_set_error (bfd_error_bad_value);
11075 return FALSE;
11076 }
11077
11078 addend = link_order->u.reloc.p->addend;
11079
d4730f92
BS
11080 if (esdo->rel.hdr)
11081 reldata = &esdo->rel;
11082 else if (esdo->rela.hdr)
11083 reldata = &esdo->rela;
11084 else
11085 {
11086 reldata = NULL;
11087 BFD_ASSERT (0);
11088 }
11089
c152c796 11090 /* Figure out the symbol index. */
d4730f92 11091 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11092 if (link_order->type == bfd_section_reloc_link_order)
11093 {
11094 indx = link_order->u.reloc.p->u.section->target_index;
11095 BFD_ASSERT (indx != 0);
11096 *rel_hash_ptr = NULL;
11097 }
11098 else
11099 {
11100 struct elf_link_hash_entry *h;
11101
11102 /* Treat a reloc against a defined symbol as though it were
11103 actually against the section. */
11104 h = ((struct elf_link_hash_entry *)
11105 bfd_wrapped_link_hash_lookup (output_bfd, info,
11106 link_order->u.reloc.p->u.name,
11107 FALSE, FALSE, TRUE));
11108 if (h != NULL
11109 && (h->root.type == bfd_link_hash_defined
11110 || h->root.type == bfd_link_hash_defweak))
11111 {
11112 asection *section;
11113
11114 section = h->root.u.def.section;
11115 indx = section->output_section->target_index;
11116 *rel_hash_ptr = NULL;
11117 /* It seems that we ought to add the symbol value to the
11118 addend here, but in practice it has already been added
11119 because it was passed to constructor_callback. */
11120 addend += section->output_section->vma + section->output_offset;
11121 }
11122 else if (h != NULL)
11123 {
11124 /* Setting the index to -2 tells elf_link_output_extsym that
11125 this symbol is used by a reloc. */
11126 h->indx = -2;
11127 *rel_hash_ptr = h;
11128 indx = 0;
11129 }
11130 else
11131 {
1a72702b
AM
11132 (*info->callbacks->unattached_reloc)
11133 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11134 indx = 0;
11135 }
11136 }
11137
11138 /* If this is an inplace reloc, we must write the addend into the
11139 object file. */
11140 if (howto->partial_inplace && addend != 0)
11141 {
11142 bfd_size_type size;
11143 bfd_reloc_status_type rstat;
11144 bfd_byte *buf;
11145 bfd_boolean ok;
11146 const char *sym_name;
11147
a50b1753
NC
11148 size = (bfd_size_type) bfd_get_reloc_size (howto);
11149 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11150 if (buf == NULL && size != 0)
c152c796
AM
11151 return FALSE;
11152 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11153 switch (rstat)
11154 {
11155 case bfd_reloc_ok:
11156 break;
11157
11158 default:
11159 case bfd_reloc_outofrange:
11160 abort ();
11161
11162 case bfd_reloc_overflow:
11163 if (link_order->type == bfd_section_reloc_link_order)
11164 sym_name = bfd_section_name (output_bfd,
11165 link_order->u.reloc.p->u.section);
11166 else
11167 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11168 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11169 howto->name, addend, NULL, NULL,
11170 (bfd_vma) 0);
c152c796
AM
11171 break;
11172 }
37b01f6a 11173
c152c796 11174 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11175 link_order->offset
11176 * bfd_octets_per_byte (output_bfd),
11177 size);
c152c796
AM
11178 free (buf);
11179 if (! ok)
11180 return FALSE;
11181 }
11182
11183 /* The address of a reloc is relative to the section in a
11184 relocatable file, and is a virtual address in an executable
11185 file. */
11186 offset = link_order->offset;
0e1862bb 11187 if (! bfd_link_relocatable (info))
c152c796
AM
11188 offset += output_section->vma;
11189
11190 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11191 {
11192 irel[i].r_offset = offset;
11193 irel[i].r_info = 0;
11194 irel[i].r_addend = 0;
11195 }
11196 if (bed->s->arch_size == 32)
11197 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11198 else
11199 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11200
d4730f92 11201 rel_hdr = reldata->hdr;
c152c796
AM
11202 erel = rel_hdr->contents;
11203 if (rel_hdr->sh_type == SHT_REL)
11204 {
d4730f92 11205 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11206 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11207 }
11208 else
11209 {
11210 irel[0].r_addend = addend;
d4730f92 11211 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11212 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11213 }
11214
d4730f92 11215 ++reldata->count;
c152c796
AM
11216
11217 return TRUE;
11218}
11219
0b52efa6
PB
11220
11221/* Get the output vma of the section pointed to by the sh_link field. */
11222
11223static bfd_vma
11224elf_get_linked_section_vma (struct bfd_link_order *p)
11225{
11226 Elf_Internal_Shdr **elf_shdrp;
11227 asection *s;
11228 int elfsec;
11229
11230 s = p->u.indirect.section;
11231 elf_shdrp = elf_elfsections (s->owner);
11232 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11233 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11234 /* PR 290:
11235 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11236 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11237 sh_info fields. Hence we could get the situation
11238 where elfsec is 0. */
11239 if (elfsec == 0)
11240 {
11241 const struct elf_backend_data *bed
11242 = get_elf_backend_data (s->owner);
11243 if (bed->link_order_error_handler)
d003868e 11244 bed->link_order_error_handler
695344c0 11245 /* xgettext:c-format */
d003868e 11246 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11247 return 0;
11248 }
11249 else
11250 {
11251 s = elf_shdrp[elfsec]->bfd_section;
11252 return s->output_section->vma + s->output_offset;
11253 }
0b52efa6
PB
11254}
11255
11256
11257/* Compare two sections based on the locations of the sections they are
11258 linked to. Used by elf_fixup_link_order. */
11259
11260static int
11261compare_link_order (const void * a, const void * b)
11262{
11263 bfd_vma apos;
11264 bfd_vma bpos;
11265
11266 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11267 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11268 if (apos < bpos)
11269 return -1;
11270 return apos > bpos;
11271}
11272
11273
11274/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11275 order as their linked sections. Returns false if this could not be done
11276 because an output section includes both ordered and unordered
11277 sections. Ideally we'd do this in the linker proper. */
11278
11279static bfd_boolean
11280elf_fixup_link_order (bfd *abfd, asection *o)
11281{
11282 int seen_linkorder;
11283 int seen_other;
11284 int n;
11285 struct bfd_link_order *p;
11286 bfd *sub;
11287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11288 unsigned elfsec;
0b52efa6 11289 struct bfd_link_order **sections;
d33cdfe3 11290 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11291 bfd_vma offset;
3b36f7e6 11292
d33cdfe3
L
11293 other_sec = NULL;
11294 linkorder_sec = NULL;
0b52efa6
PB
11295 seen_other = 0;
11296 seen_linkorder = 0;
8423293d 11297 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11298 {
d33cdfe3 11299 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11300 {
11301 s = p->u.indirect.section;
d33cdfe3
L
11302 sub = s->owner;
11303 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11304 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11305 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11306 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11307 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11308 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11309 {
11310 seen_linkorder++;
11311 linkorder_sec = s;
11312 }
0b52efa6 11313 else
d33cdfe3
L
11314 {
11315 seen_other++;
11316 other_sec = s;
11317 }
0b52efa6
PB
11318 }
11319 else
11320 seen_other++;
d33cdfe3
L
11321
11322 if (seen_other && seen_linkorder)
11323 {
11324 if (other_sec && linkorder_sec)
4eca0228 11325 _bfd_error_handler
695344c0 11326 /* xgettext:c-format */
4eca0228
AM
11327 (_("%A has both ordered [`%A' in %B] "
11328 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11329 o, linkorder_sec, linkorder_sec->owner,
11330 other_sec, other_sec->owner);
d33cdfe3 11331 else
4eca0228
AM
11332 _bfd_error_handler
11333 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11334 bfd_set_error (bfd_error_bad_value);
11335 return FALSE;
11336 }
0b52efa6
PB
11337 }
11338
11339 if (!seen_linkorder)
11340 return TRUE;
11341
0b52efa6 11342 sections = (struct bfd_link_order **)
14b1c01e
AM
11343 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11344 if (sections == NULL)
11345 return FALSE;
0b52efa6 11346 seen_linkorder = 0;
3b36f7e6 11347
8423293d 11348 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11349 {
11350 sections[seen_linkorder++] = p;
11351 }
11352 /* Sort the input sections in the order of their linked section. */
11353 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11354 compare_link_order);
11355
11356 /* Change the offsets of the sections. */
11357 offset = 0;
11358 for (n = 0; n < seen_linkorder; n++)
11359 {
11360 s = sections[n]->u.indirect.section;
461686a3 11361 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11362 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11363 sections[n]->offset = offset;
11364 offset += sections[n]->size;
11365 }
11366
4dd07732 11367 free (sections);
0b52efa6
PB
11368 return TRUE;
11369}
11370
76359541
TP
11371/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11372 Returns TRUE upon success, FALSE otherwise. */
11373
11374static bfd_boolean
11375elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11376{
11377 bfd_boolean ret = FALSE;
11378 bfd *implib_bfd;
11379 const struct elf_backend_data *bed;
11380 flagword flags;
11381 enum bfd_architecture arch;
11382 unsigned int mach;
11383 asymbol **sympp = NULL;
11384 long symsize;
11385 long symcount;
11386 long src_count;
11387 elf_symbol_type *osymbuf;
11388
11389 implib_bfd = info->out_implib_bfd;
11390 bed = get_elf_backend_data (abfd);
11391
11392 if (!bfd_set_format (implib_bfd, bfd_object))
11393 return FALSE;
11394
046734ff 11395 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11396 flags = bfd_get_file_flags (abfd);
11397 flags &= ~HAS_RELOC;
11398 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11399 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11400 return FALSE;
11401
11402 /* Copy architecture of output file to import library file. */
11403 arch = bfd_get_arch (abfd);
11404 mach = bfd_get_mach (abfd);
11405 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11406 && (abfd->target_defaulted
11407 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11408 return FALSE;
11409
11410 /* Get symbol table size. */
11411 symsize = bfd_get_symtab_upper_bound (abfd);
11412 if (symsize < 0)
11413 return FALSE;
11414
11415 /* Read in the symbol table. */
11416 sympp = (asymbol **) xmalloc (symsize);
11417 symcount = bfd_canonicalize_symtab (abfd, sympp);
11418 if (symcount < 0)
11419 goto free_sym_buf;
11420
11421 /* Allow the BFD backend to copy any private header data it
11422 understands from the output BFD to the import library BFD. */
11423 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11424 goto free_sym_buf;
11425
11426 /* Filter symbols to appear in the import library. */
11427 if (bed->elf_backend_filter_implib_symbols)
11428 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11429 symcount);
11430 else
11431 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11432 if (symcount == 0)
11433 {
5df1bc57 11434 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11435 _bfd_error_handler (_("%B: no symbol found for import library"),
11436 implib_bfd);
76359541
TP
11437 goto free_sym_buf;
11438 }
11439
11440
11441 /* Make symbols absolute. */
11442 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11443 sizeof (*osymbuf));
11444 for (src_count = 0; src_count < symcount; src_count++)
11445 {
11446 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11447 sizeof (*osymbuf));
11448 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11449 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11450 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11451 osymbuf[src_count].internal_elf_sym.st_value =
11452 osymbuf[src_count].symbol.value;
11453 sympp[src_count] = &osymbuf[src_count].symbol;
11454 }
11455
11456 bfd_set_symtab (implib_bfd, sympp, symcount);
11457
11458 /* Allow the BFD backend to copy any private data it understands
11459 from the output BFD to the import library BFD. This is done last
11460 to permit the routine to look at the filtered symbol table. */
11461 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11462 goto free_sym_buf;
11463
11464 if (!bfd_close (implib_bfd))
11465 goto free_sym_buf;
11466
11467 ret = TRUE;
11468
11469free_sym_buf:
11470 free (sympp);
11471 return ret;
11472}
11473
9f7c3e5e
AM
11474static void
11475elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11476{
11477 asection *o;
11478
11479 if (flinfo->symstrtab != NULL)
ef10c3ac 11480 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11481 if (flinfo->contents != NULL)
11482 free (flinfo->contents);
11483 if (flinfo->external_relocs != NULL)
11484 free (flinfo->external_relocs);
11485 if (flinfo->internal_relocs != NULL)
11486 free (flinfo->internal_relocs);
11487 if (flinfo->external_syms != NULL)
11488 free (flinfo->external_syms);
11489 if (flinfo->locsym_shndx != NULL)
11490 free (flinfo->locsym_shndx);
11491 if (flinfo->internal_syms != NULL)
11492 free (flinfo->internal_syms);
11493 if (flinfo->indices != NULL)
11494 free (flinfo->indices);
11495 if (flinfo->sections != NULL)
11496 free (flinfo->sections);
9f7c3e5e
AM
11497 if (flinfo->symshndxbuf != NULL)
11498 free (flinfo->symshndxbuf);
11499 for (o = obfd->sections; o != NULL; o = o->next)
11500 {
11501 struct bfd_elf_section_data *esdo = elf_section_data (o);
11502 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11503 free (esdo->rel.hashes);
11504 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11505 free (esdo->rela.hashes);
11506 }
11507}
0b52efa6 11508
c152c796
AM
11509/* Do the final step of an ELF link. */
11510
11511bfd_boolean
11512bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11513{
11514 bfd_boolean dynamic;
11515 bfd_boolean emit_relocs;
11516 bfd *dynobj;
8b127cbc 11517 struct elf_final_link_info flinfo;
91d6fa6a
NC
11518 asection *o;
11519 struct bfd_link_order *p;
11520 bfd *sub;
c152c796
AM
11521 bfd_size_type max_contents_size;
11522 bfd_size_type max_external_reloc_size;
11523 bfd_size_type max_internal_reloc_count;
11524 bfd_size_type max_sym_count;
11525 bfd_size_type max_sym_shndx_count;
c152c796
AM
11526 Elf_Internal_Sym elfsym;
11527 unsigned int i;
11528 Elf_Internal_Shdr *symtab_hdr;
11529 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11530 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11531 struct elf_outext_info eoinfo;
11532 bfd_boolean merged;
11533 size_t relativecount = 0;
11534 asection *reldyn = 0;
11535 bfd_size_type amt;
104d59d1
JM
11536 asection *attr_section = NULL;
11537 bfd_vma attr_size = 0;
11538 const char *std_attrs_section;
64f52338 11539 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11540
64f52338 11541 if (!is_elf_hash_table (htab))
c152c796
AM
11542 return FALSE;
11543
0e1862bb 11544 if (bfd_link_pic (info))
c152c796
AM
11545 abfd->flags |= DYNAMIC;
11546
64f52338
AM
11547 dynamic = htab->dynamic_sections_created;
11548 dynobj = htab->dynobj;
c152c796 11549
0e1862bb 11550 emit_relocs = (bfd_link_relocatable (info)
a4676736 11551 || info->emitrelocations);
c152c796 11552
8b127cbc
AM
11553 flinfo.info = info;
11554 flinfo.output_bfd = abfd;
ef10c3ac 11555 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11556 if (flinfo.symstrtab == NULL)
c152c796
AM
11557 return FALSE;
11558
11559 if (! dynamic)
11560 {
8b127cbc
AM
11561 flinfo.hash_sec = NULL;
11562 flinfo.symver_sec = NULL;
c152c796
AM
11563 }
11564 else
11565 {
3d4d4302 11566 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11567 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11568 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11569 /* Note that it is OK if symver_sec is NULL. */
11570 }
11571
8b127cbc
AM
11572 flinfo.contents = NULL;
11573 flinfo.external_relocs = NULL;
11574 flinfo.internal_relocs = NULL;
11575 flinfo.external_syms = NULL;
11576 flinfo.locsym_shndx = NULL;
11577 flinfo.internal_syms = NULL;
11578 flinfo.indices = NULL;
11579 flinfo.sections = NULL;
8b127cbc 11580 flinfo.symshndxbuf = NULL;
ffbc01cc 11581 flinfo.filesym_count = 0;
c152c796 11582
104d59d1
JM
11583 /* The object attributes have been merged. Remove the input
11584 sections from the link, and set the contents of the output
11585 secton. */
11586 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11587 for (o = abfd->sections; o != NULL; o = o->next)
11588 {
11589 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11590 || strcmp (o->name, ".gnu.attributes") == 0)
11591 {
11592 for (p = o->map_head.link_order; p != NULL; p = p->next)
11593 {
11594 asection *input_section;
11595
11596 if (p->type != bfd_indirect_link_order)
11597 continue;
11598 input_section = p->u.indirect.section;
11599 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11600 elf_link_input_bfd ignores this section. */
11601 input_section->flags &= ~SEC_HAS_CONTENTS;
11602 }
a0c8462f 11603
104d59d1
JM
11604 attr_size = bfd_elf_obj_attr_size (abfd);
11605 if (attr_size)
11606 {
11607 bfd_set_section_size (abfd, o, attr_size);
11608 attr_section = o;
11609 /* Skip this section later on. */
11610 o->map_head.link_order = NULL;
11611 }
11612 else
11613 o->flags |= SEC_EXCLUDE;
11614 }
11615 }
11616
c152c796
AM
11617 /* Count up the number of relocations we will output for each output
11618 section, so that we know the sizes of the reloc sections. We
11619 also figure out some maximum sizes. */
11620 max_contents_size = 0;
11621 max_external_reloc_size = 0;
11622 max_internal_reloc_count = 0;
11623 max_sym_count = 0;
11624 max_sym_shndx_count = 0;
11625 merged = FALSE;
11626 for (o = abfd->sections; o != NULL; o = o->next)
11627 {
11628 struct bfd_elf_section_data *esdo = elf_section_data (o);
11629 o->reloc_count = 0;
11630
8423293d 11631 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11632 {
11633 unsigned int reloc_count = 0;
9eaff861 11634 unsigned int additional_reloc_count = 0;
c152c796 11635 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11636
11637 if (p->type == bfd_section_reloc_link_order
11638 || p->type == bfd_symbol_reloc_link_order)
11639 reloc_count = 1;
11640 else if (p->type == bfd_indirect_link_order)
11641 {
11642 asection *sec;
11643
11644 sec = p->u.indirect.section;
c152c796
AM
11645
11646 /* Mark all sections which are to be included in the
11647 link. This will normally be every section. We need
11648 to do this so that we can identify any sections which
11649 the linker has decided to not include. */
11650 sec->linker_mark = TRUE;
11651
11652 if (sec->flags & SEC_MERGE)
11653 merged = TRUE;
11654
eea6121a
AM
11655 if (sec->rawsize > max_contents_size)
11656 max_contents_size = sec->rawsize;
11657 if (sec->size > max_contents_size)
11658 max_contents_size = sec->size;
c152c796 11659
c152c796
AM
11660 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11661 && (sec->owner->flags & DYNAMIC) == 0)
11662 {
11663 size_t sym_count;
11664
a961cdd5
AM
11665 /* We are interested in just local symbols, not all
11666 symbols. */
c152c796
AM
11667 if (elf_bad_symtab (sec->owner))
11668 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11669 / bed->s->sizeof_sym);
11670 else
11671 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11672
11673 if (sym_count > max_sym_count)
11674 max_sym_count = sym_count;
11675
11676 if (sym_count > max_sym_shndx_count
6a40cf0c 11677 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11678 max_sym_shndx_count = sym_count;
11679
a961cdd5
AM
11680 if (esdo->this_hdr.sh_type == SHT_REL
11681 || esdo->this_hdr.sh_type == SHT_RELA)
11682 /* Some backends use reloc_count in relocation sections
11683 to count particular types of relocs. Of course,
11684 reloc sections themselves can't have relocations. */
11685 ;
11686 else if (emit_relocs)
11687 {
11688 reloc_count = sec->reloc_count;
11689 if (bed->elf_backend_count_additional_relocs)
11690 {
11691 int c;
11692 c = (*bed->elf_backend_count_additional_relocs) (sec);
11693 additional_reloc_count += c;
11694 }
11695 }
11696 else if (bed->elf_backend_count_relocs)
11697 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11698
11699 esdi = elf_section_data (sec);
11700
c152c796
AM
11701 if ((sec->flags & SEC_RELOC) != 0)
11702 {
d4730f92 11703 size_t ext_size = 0;
c152c796 11704
d4730f92
BS
11705 if (esdi->rel.hdr != NULL)
11706 ext_size = esdi->rel.hdr->sh_size;
11707 if (esdi->rela.hdr != NULL)
11708 ext_size += esdi->rela.hdr->sh_size;
7326c758 11709
c152c796
AM
11710 if (ext_size > max_external_reloc_size)
11711 max_external_reloc_size = ext_size;
11712 if (sec->reloc_count > max_internal_reloc_count)
11713 max_internal_reloc_count = sec->reloc_count;
11714 }
11715 }
11716 }
11717
11718 if (reloc_count == 0)
11719 continue;
11720
9eaff861 11721 reloc_count += additional_reloc_count;
c152c796
AM
11722 o->reloc_count += reloc_count;
11723
0e1862bb 11724 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11725 {
d4730f92 11726 if (esdi->rel.hdr)
9eaff861 11727 {
491d01d3 11728 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11729 esdo->rel.count += additional_reloc_count;
11730 }
d4730f92 11731 if (esdi->rela.hdr)
9eaff861 11732 {
491d01d3 11733 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11734 esdo->rela.count += additional_reloc_count;
11735 }
d4730f92
BS
11736 }
11737 else
11738 {
11739 if (o->use_rela_p)
11740 esdo->rela.count += reloc_count;
2c2b4ed4 11741 else
d4730f92 11742 esdo->rel.count += reloc_count;
c152c796 11743 }
c152c796
AM
11744 }
11745
9eaff861 11746 if (o->reloc_count > 0)
c152c796
AM
11747 o->flags |= SEC_RELOC;
11748 else
11749 {
11750 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11751 set it (this is probably a bug) and if it is set
11752 assign_section_numbers will create a reloc section. */
11753 o->flags &=~ SEC_RELOC;
11754 }
11755
11756 /* If the SEC_ALLOC flag is not set, force the section VMA to
11757 zero. This is done in elf_fake_sections as well, but forcing
11758 the VMA to 0 here will ensure that relocs against these
11759 sections are handled correctly. */
11760 if ((o->flags & SEC_ALLOC) == 0
11761 && ! o->user_set_vma)
11762 o->vma = 0;
11763 }
11764
0e1862bb 11765 if (! bfd_link_relocatable (info) && merged)
64f52338 11766 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11767
11768 /* Figure out the file positions for everything but the symbol table
11769 and the relocs. We set symcount to force assign_section_numbers
11770 to create a symbol table. */
8539e4e8 11771 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11772 BFD_ASSERT (! abfd->output_has_begun);
11773 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11774 goto error_return;
11775
ee75fd95 11776 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11777 for (o = abfd->sections; o != NULL; o = o->next)
11778 {
d4730f92 11779 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11780 if ((o->flags & SEC_RELOC) != 0)
11781 {
d4730f92 11782 if (esdo->rel.hdr
9eaff861 11783 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11784 goto error_return;
11785
d4730f92 11786 if (esdo->rela.hdr
9eaff861 11787 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11788 goto error_return;
11789 }
11790
11791 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11792 to count upwards while actually outputting the relocations. */
d4730f92
BS
11793 esdo->rel.count = 0;
11794 esdo->rela.count = 0;
0ce398f1
L
11795
11796 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11797 {
11798 /* Cache the section contents so that they can be compressed
11799 later. Use bfd_malloc since it will be freed by
11800 bfd_compress_section_contents. */
11801 unsigned char *contents = esdo->this_hdr.contents;
11802 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11803 abort ();
11804 contents
11805 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11806 if (contents == NULL)
11807 goto error_return;
11808 esdo->this_hdr.contents = contents;
11809 }
c152c796
AM
11810 }
11811
c152c796 11812 /* We have now assigned file positions for all the sections except
a485e98e
AM
11813 .symtab, .strtab, and non-loaded reloc sections. We start the
11814 .symtab section at the current file position, and write directly
11815 to it. We build the .strtab section in memory. */
c152c796
AM
11816 bfd_get_symcount (abfd) = 0;
11817 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11818 /* sh_name is set in prep_headers. */
11819 symtab_hdr->sh_type = SHT_SYMTAB;
11820 /* sh_flags, sh_addr and sh_size all start off zero. */
11821 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11822 /* sh_link is set in assign_section_numbers. */
11823 /* sh_info is set below. */
11824 /* sh_offset is set just below. */
72de5009 11825 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11826
ef10c3ac
L
11827 if (max_sym_count < 20)
11828 max_sym_count = 20;
64f52338 11829 htab->strtabsize = max_sym_count;
ef10c3ac 11830 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11831 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11832 if (htab->strtab == NULL)
c152c796 11833 goto error_return;
ef10c3ac
L
11834 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11835 flinfo.symshndxbuf
11836 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11837 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11838
8539e4e8 11839 if (info->strip != strip_all || emit_relocs)
c152c796 11840 {
8539e4e8
AM
11841 file_ptr off = elf_next_file_pos (abfd);
11842
11843 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11844
11845 /* Note that at this point elf_next_file_pos (abfd) is
11846 incorrect. We do not yet know the size of the .symtab section.
11847 We correct next_file_pos below, after we do know the size. */
11848
11849 /* Start writing out the symbol table. The first symbol is always a
11850 dummy symbol. */
c152c796
AM
11851 elfsym.st_value = 0;
11852 elfsym.st_size = 0;
11853 elfsym.st_info = 0;
11854 elfsym.st_other = 0;
11855 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11856 elfsym.st_target_internal = 0;
ef10c3ac
L
11857 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11858 bfd_und_section_ptr, NULL) != 1)
c152c796 11859 goto error_return;
c152c796 11860
8539e4e8
AM
11861 /* Output a symbol for each section. We output these even if we are
11862 discarding local symbols, since they are used for relocs. These
11863 symbols have no names. We store the index of each one in the
11864 index field of the section, so that we can find it again when
11865 outputting relocs. */
11866
c152c796
AM
11867 elfsym.st_size = 0;
11868 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11869 elfsym.st_other = 0;
f0b5bb34 11870 elfsym.st_value = 0;
35fc36a8 11871 elfsym.st_target_internal = 0;
c152c796
AM
11872 for (i = 1; i < elf_numsections (abfd); i++)
11873 {
11874 o = bfd_section_from_elf_index (abfd, i);
11875 if (o != NULL)
f0b5bb34
AM
11876 {
11877 o->target_index = bfd_get_symcount (abfd);
11878 elfsym.st_shndx = i;
0e1862bb 11879 if (!bfd_link_relocatable (info))
f0b5bb34 11880 elfsym.st_value = o->vma;
ef10c3ac
L
11881 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11882 NULL) != 1)
f0b5bb34
AM
11883 goto error_return;
11884 }
c152c796
AM
11885 }
11886 }
11887
11888 /* Allocate some memory to hold information read in from the input
11889 files. */
11890 if (max_contents_size != 0)
11891 {
8b127cbc
AM
11892 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11893 if (flinfo.contents == NULL)
c152c796
AM
11894 goto error_return;
11895 }
11896
11897 if (max_external_reloc_size != 0)
11898 {
8b127cbc
AM
11899 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11900 if (flinfo.external_relocs == NULL)
c152c796
AM
11901 goto error_return;
11902 }
11903
11904 if (max_internal_reloc_count != 0)
11905 {
056bafd4 11906 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11907 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11908 if (flinfo.internal_relocs == NULL)
c152c796
AM
11909 goto error_return;
11910 }
11911
11912 if (max_sym_count != 0)
11913 {
11914 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11915 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11916 if (flinfo.external_syms == NULL)
c152c796
AM
11917 goto error_return;
11918
11919 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11920 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11921 if (flinfo.internal_syms == NULL)
c152c796
AM
11922 goto error_return;
11923
11924 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11925 flinfo.indices = (long int *) bfd_malloc (amt);
11926 if (flinfo.indices == NULL)
c152c796
AM
11927 goto error_return;
11928
11929 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11930 flinfo.sections = (asection **) bfd_malloc (amt);
11931 if (flinfo.sections == NULL)
c152c796
AM
11932 goto error_return;
11933 }
11934
11935 if (max_sym_shndx_count != 0)
11936 {
11937 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11938 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11939 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11940 goto error_return;
11941 }
11942
64f52338 11943 if (htab->tls_sec)
c152c796
AM
11944 {
11945 bfd_vma base, end = 0;
11946 asection *sec;
11947
64f52338 11948 for (sec = htab->tls_sec;
c152c796
AM
11949 sec && (sec->flags & SEC_THREAD_LOCAL);
11950 sec = sec->next)
11951 {
3a800eb9 11952 bfd_size_type size = sec->size;
c152c796 11953
3a800eb9
AM
11954 if (size == 0
11955 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11956 {
91d6fa6a
NC
11957 struct bfd_link_order *ord = sec->map_tail.link_order;
11958
11959 if (ord != NULL)
11960 size = ord->offset + ord->size;
c152c796
AM
11961 }
11962 end = sec->vma + size;
11963 }
64f52338 11964 base = htab->tls_sec->vma;
7dc98aea
RO
11965 /* Only align end of TLS section if static TLS doesn't have special
11966 alignment requirements. */
11967 if (bed->static_tls_alignment == 1)
64f52338
AM
11968 end = align_power (end, htab->tls_sec->alignment_power);
11969 htab->tls_size = end - base;
c152c796
AM
11970 }
11971
0b52efa6
PB
11972 /* Reorder SHF_LINK_ORDER sections. */
11973 for (o = abfd->sections; o != NULL; o = o->next)
11974 {
11975 if (!elf_fixup_link_order (abfd, o))
11976 return FALSE;
11977 }
11978
2f0c68f2
CM
11979 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11980 return FALSE;
11981
c152c796
AM
11982 /* Since ELF permits relocations to be against local symbols, we
11983 must have the local symbols available when we do the relocations.
11984 Since we would rather only read the local symbols once, and we
11985 would rather not keep them in memory, we handle all the
11986 relocations for a single input file at the same time.
11987
11988 Unfortunately, there is no way to know the total number of local
11989 symbols until we have seen all of them, and the local symbol
11990 indices precede the global symbol indices. This means that when
11991 we are generating relocatable output, and we see a reloc against
11992 a global symbol, we can not know the symbol index until we have
11993 finished examining all the local symbols to see which ones we are
11994 going to output. To deal with this, we keep the relocations in
11995 memory, and don't output them until the end of the link. This is
11996 an unfortunate waste of memory, but I don't see a good way around
11997 it. Fortunately, it only happens when performing a relocatable
11998 link, which is not the common case. FIXME: If keep_memory is set
11999 we could write the relocs out and then read them again; I don't
12000 know how bad the memory loss will be. */
12001
c72f2fb2 12002 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12003 sub->output_has_begun = FALSE;
12004 for (o = abfd->sections; o != NULL; o = o->next)
12005 {
8423293d 12006 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12007 {
12008 if (p->type == bfd_indirect_link_order
12009 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12010 == bfd_target_elf_flavour)
12011 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12012 {
12013 if (! sub->output_has_begun)
12014 {
8b127cbc 12015 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12016 goto error_return;
12017 sub->output_has_begun = TRUE;
12018 }
12019 }
12020 else if (p->type == bfd_section_reloc_link_order
12021 || p->type == bfd_symbol_reloc_link_order)
12022 {
12023 if (! elf_reloc_link_order (abfd, info, o, p))
12024 goto error_return;
12025 }
12026 else
12027 {
12028 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12029 {
12030 if (p->type == bfd_indirect_link_order
12031 && (bfd_get_flavour (sub)
12032 == bfd_target_elf_flavour)
12033 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12034 != bed->s->elfclass))
12035 {
12036 const char *iclass, *oclass;
12037
aebf9be7 12038 switch (bed->s->elfclass)
351f65ca 12039 {
aebf9be7
NC
12040 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12041 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12042 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12043 default: abort ();
351f65ca 12044 }
aebf9be7
NC
12045
12046 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12047 {
aebf9be7
NC
12048 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12049 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12050 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12051 default: abort ();
351f65ca
L
12052 }
12053
12054 bfd_set_error (bfd_error_wrong_format);
4eca0228 12055 _bfd_error_handler
695344c0 12056 /* xgettext:c-format */
351f65ca
L
12057 (_("%B: file class %s incompatible with %s"),
12058 sub, iclass, oclass);
12059 }
12060
12061 goto error_return;
12062 }
c152c796
AM
12063 }
12064 }
12065 }
12066
c0f00686
L
12067 /* Free symbol buffer if needed. */
12068 if (!info->reduce_memory_overheads)
12069 {
c72f2fb2 12070 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12071 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12072 && elf_tdata (sub)->symbuf)
c0f00686
L
12073 {
12074 free (elf_tdata (sub)->symbuf);
12075 elf_tdata (sub)->symbuf = NULL;
12076 }
12077 }
12078
c152c796
AM
12079 /* Output any global symbols that got converted to local in a
12080 version script or due to symbol visibility. We do this in a
12081 separate step since ELF requires all local symbols to appear
12082 prior to any global symbols. FIXME: We should only do this if
12083 some global symbols were, in fact, converted to become local.
12084 FIXME: Will this work correctly with the Irix 5 linker? */
12085 eoinfo.failed = FALSE;
8b127cbc 12086 eoinfo.flinfo = &flinfo;
c152c796 12087 eoinfo.localsyms = TRUE;
34a79995 12088 eoinfo.file_sym_done = FALSE;
7686d77d 12089 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12090 if (eoinfo.failed)
12091 return FALSE;
12092
4e617b1e
PB
12093 /* If backend needs to output some local symbols not present in the hash
12094 table, do it now. */
8539e4e8
AM
12095 if (bed->elf_backend_output_arch_local_syms
12096 && (info->strip != strip_all || emit_relocs))
4e617b1e 12097 {
6e0b88f1 12098 typedef int (*out_sym_func)
4e617b1e
PB
12099 (void *, const char *, Elf_Internal_Sym *, asection *,
12100 struct elf_link_hash_entry *);
12101
12102 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12103 (abfd, info, &flinfo,
12104 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12105 return FALSE;
12106 }
12107
c152c796
AM
12108 /* That wrote out all the local symbols. Finish up the symbol table
12109 with the global symbols. Even if we want to strip everything we
12110 can, we still need to deal with those global symbols that got
12111 converted to local in a version script. */
12112
12113 /* The sh_info field records the index of the first non local symbol. */
12114 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12115
12116 if (dynamic
64f52338
AM
12117 && htab->dynsym != NULL
12118 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12119 {
12120 Elf_Internal_Sym sym;
64f52338 12121 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12122
64f52338
AM
12123 o = htab->dynsym->output_section;
12124 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12125
12126 /* Write out the section symbols for the output sections. */
0e1862bb 12127 if (bfd_link_pic (info)
64f52338 12128 || htab->is_relocatable_executable)
c152c796
AM
12129 {
12130 asection *s;
12131
12132 sym.st_size = 0;
12133 sym.st_name = 0;
12134 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12135 sym.st_other = 0;
35fc36a8 12136 sym.st_target_internal = 0;
c152c796
AM
12137
12138 for (s = abfd->sections; s != NULL; s = s->next)
12139 {
12140 int indx;
12141 bfd_byte *dest;
12142 long dynindx;
12143
c152c796 12144 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12145 if (dynindx <= 0)
12146 continue;
12147 indx = elf_section_data (s)->this_idx;
c152c796
AM
12148 BFD_ASSERT (indx > 0);
12149 sym.st_shndx = indx;
c0d5a53d
L
12150 if (! check_dynsym (abfd, &sym))
12151 return FALSE;
c152c796
AM
12152 sym.st_value = s->vma;
12153 dest = dynsym + dynindx * bed->s->sizeof_sym;
12154 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12155 }
c152c796
AM
12156 }
12157
12158 /* Write out the local dynsyms. */
64f52338 12159 if (htab->dynlocal)
c152c796
AM
12160 {
12161 struct elf_link_local_dynamic_entry *e;
64f52338 12162 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12163 {
12164 asection *s;
12165 bfd_byte *dest;
12166
935bd1e0 12167 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12168 Note that we saved a word of storage and overwrote
12169 the original st_name with the dynstr_index. */
12170 sym = e->isym;
935bd1e0 12171 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12172
cb33740c
AM
12173 s = bfd_section_from_elf_index (e->input_bfd,
12174 e->isym.st_shndx);
12175 if (s != NULL)
c152c796 12176 {
c152c796
AM
12177 sym.st_shndx =
12178 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12179 if (! check_dynsym (abfd, &sym))
12180 return FALSE;
c152c796
AM
12181 sym.st_value = (s->output_section->vma
12182 + s->output_offset
12183 + e->isym.st_value);
12184 }
12185
c152c796
AM
12186 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12187 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12188 }
12189 }
c152c796
AM
12190 }
12191
12192 /* We get the global symbols from the hash table. */
12193 eoinfo.failed = FALSE;
12194 eoinfo.localsyms = FALSE;
8b127cbc 12195 eoinfo.flinfo = &flinfo;
7686d77d 12196 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12197 if (eoinfo.failed)
12198 return FALSE;
12199
12200 /* If backend needs to output some symbols not present in the hash
12201 table, do it now. */
8539e4e8
AM
12202 if (bed->elf_backend_output_arch_syms
12203 && (info->strip != strip_all || emit_relocs))
c152c796 12204 {
6e0b88f1 12205 typedef int (*out_sym_func)
c152c796
AM
12206 (void *, const char *, Elf_Internal_Sym *, asection *,
12207 struct elf_link_hash_entry *);
12208
12209 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12210 (abfd, info, &flinfo,
12211 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12212 return FALSE;
12213 }
12214
ef10c3ac
L
12215 /* Finalize the .strtab section. */
12216 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12217
12218 /* Swap out the .strtab section. */
12219 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12220 return FALSE;
12221
12222 /* Now we know the size of the symtab section. */
c152c796
AM
12223 if (bfd_get_symcount (abfd) > 0)
12224 {
ee3b52e9
L
12225 /* Finish up and write out the symbol string table (.strtab)
12226 section. */
ad32986f 12227 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12228 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12229
ad32986f 12230 if (elf_symtab_shndx_list (abfd))
8539e4e8 12231 {
ad32986f 12232 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12233
ad32986f
NC
12234 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12235 {
12236 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12237 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12238 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12239 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12240 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12241
ad32986f
NC
12242 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12243 off, TRUE);
12244
12245 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12246 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12247 return FALSE;
12248 }
8539e4e8 12249 }
ee3b52e9
L
12250
12251 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12252 /* sh_name was set in prep_headers. */
12253 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12254 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12255 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12256 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12257 symstrtab_hdr->sh_entsize = 0;
12258 symstrtab_hdr->sh_link = 0;
12259 symstrtab_hdr->sh_info = 0;
12260 /* sh_offset is set just below. */
12261 symstrtab_hdr->sh_addralign = 1;
12262
12263 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12264 off, TRUE);
12265 elf_next_file_pos (abfd) = off;
12266
c152c796 12267 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12268 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12269 return FALSE;
12270 }
12271
76359541
TP
12272 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12273 {
4eca0228
AM
12274 _bfd_error_handler (_("%B: failed to generate import library"),
12275 info->out_implib_bfd);
76359541
TP
12276 return FALSE;
12277 }
12278
c152c796
AM
12279 /* Adjust the relocs to have the correct symbol indices. */
12280 for (o = abfd->sections; o != NULL; o = o->next)
12281 {
d4730f92 12282 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12283 bfd_boolean sort;
10bbbc1d 12284
c152c796
AM
12285 if ((o->flags & SEC_RELOC) == 0)
12286 continue;
12287
28dbcedc 12288 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12289 if (esdo->rel.hdr != NULL
10bbbc1d 12290 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12291 return FALSE;
12292 if (esdo->rela.hdr != NULL
10bbbc1d 12293 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12294 return FALSE;
c152c796
AM
12295
12296 /* Set the reloc_count field to 0 to prevent write_relocs from
12297 trying to swap the relocs out itself. */
12298 o->reloc_count = 0;
12299 }
12300
12301 if (dynamic && info->combreloc && dynobj != NULL)
12302 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12303
12304 /* If we are linking against a dynamic object, or generating a
12305 shared library, finish up the dynamic linking information. */
12306 if (dynamic)
12307 {
12308 bfd_byte *dyncon, *dynconend;
12309
12310 /* Fix up .dynamic entries. */
3d4d4302 12311 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12312 BFD_ASSERT (o != NULL);
12313
12314 dyncon = o->contents;
eea6121a 12315 dynconend = o->contents + o->size;
c152c796
AM
12316 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12317 {
12318 Elf_Internal_Dyn dyn;
12319 const char *name;
12320 unsigned int type;
64487780
AM
12321 bfd_size_type sh_size;
12322 bfd_vma sh_addr;
c152c796
AM
12323
12324 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12325
12326 switch (dyn.d_tag)
12327 {
12328 default:
12329 continue;
12330 case DT_NULL:
12331 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12332 {
12333 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12334 {
12335 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12336 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12337 default: continue;
12338 }
12339 dyn.d_un.d_val = relativecount;
12340 relativecount = 0;
12341 break;
12342 }
12343 continue;
12344
12345 case DT_INIT:
12346 name = info->init_function;
12347 goto get_sym;
12348 case DT_FINI:
12349 name = info->fini_function;
12350 get_sym:
12351 {
12352 struct elf_link_hash_entry *h;
12353
64f52338 12354 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12355 if (h != NULL
12356 && (h->root.type == bfd_link_hash_defined
12357 || h->root.type == bfd_link_hash_defweak))
12358 {
bef26483 12359 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12360 o = h->root.u.def.section;
12361 if (o->output_section != NULL)
bef26483 12362 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12363 + o->output_offset);
12364 else
12365 {
12366 /* The symbol is imported from another shared
12367 library and does not apply to this one. */
bef26483 12368 dyn.d_un.d_ptr = 0;
c152c796
AM
12369 }
12370 break;
12371 }
12372 }
12373 continue;
12374
12375 case DT_PREINIT_ARRAYSZ:
12376 name = ".preinit_array";
4ade44b7 12377 goto get_out_size;
c152c796
AM
12378 case DT_INIT_ARRAYSZ:
12379 name = ".init_array";
4ade44b7 12380 goto get_out_size;
c152c796
AM
12381 case DT_FINI_ARRAYSZ:
12382 name = ".fini_array";
4ade44b7 12383 get_out_size:
c152c796
AM
12384 o = bfd_get_section_by_name (abfd, name);
12385 if (o == NULL)
12386 {
4eca0228 12387 _bfd_error_handler
4ade44b7 12388 (_("could not find section %s"), name);
c152c796
AM
12389 goto error_return;
12390 }
eea6121a 12391 if (o->size == 0)
4eca0228 12392 _bfd_error_handler
c152c796 12393 (_("warning: %s section has zero size"), name);
eea6121a 12394 dyn.d_un.d_val = o->size;
c152c796
AM
12395 break;
12396
12397 case DT_PREINIT_ARRAY:
12398 name = ".preinit_array";
4ade44b7 12399 goto get_out_vma;
c152c796
AM
12400 case DT_INIT_ARRAY:
12401 name = ".init_array";
4ade44b7 12402 goto get_out_vma;
c152c796
AM
12403 case DT_FINI_ARRAY:
12404 name = ".fini_array";
4ade44b7
AM
12405 get_out_vma:
12406 o = bfd_get_section_by_name (abfd, name);
12407 goto do_vma;
c152c796
AM
12408
12409 case DT_HASH:
12410 name = ".hash";
12411 goto get_vma;
fdc90cb4
JJ
12412 case DT_GNU_HASH:
12413 name = ".gnu.hash";
12414 goto get_vma;
c152c796
AM
12415 case DT_STRTAB:
12416 name = ".dynstr";
12417 goto get_vma;
12418 case DT_SYMTAB:
12419 name = ".dynsym";
12420 goto get_vma;
12421 case DT_VERDEF:
12422 name = ".gnu.version_d";
12423 goto get_vma;
12424 case DT_VERNEED:
12425 name = ".gnu.version_r";
12426 goto get_vma;
12427 case DT_VERSYM:
12428 name = ".gnu.version";
12429 get_vma:
4ade44b7
AM
12430 o = bfd_get_linker_section (dynobj, name);
12431 do_vma:
b3293efa 12432 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12433 {
4eca0228 12434 _bfd_error_handler
4ade44b7 12435 (_("could not find section %s"), name);
c152c796
AM
12436 goto error_return;
12437 }
894891db
NC
12438 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12439 {
4eca0228 12440 _bfd_error_handler
894891db
NC
12441 (_("warning: section '%s' is being made into a note"), name);
12442 bfd_set_error (bfd_error_nonrepresentable_section);
12443 goto error_return;
12444 }
4ade44b7 12445 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12446 break;
12447
12448 case DT_REL:
12449 case DT_RELA:
12450 case DT_RELSZ:
12451 case DT_RELASZ:
12452 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12453 type = SHT_REL;
12454 else
12455 type = SHT_RELA;
64487780
AM
12456 sh_size = 0;
12457 sh_addr = 0;
c152c796
AM
12458 for (i = 1; i < elf_numsections (abfd); i++)
12459 {
12460 Elf_Internal_Shdr *hdr;
12461
12462 hdr = elf_elfsections (abfd)[i];
12463 if (hdr->sh_type == type
12464 && (hdr->sh_flags & SHF_ALLOC) != 0)
12465 {
64487780
AM
12466 sh_size += hdr->sh_size;
12467 if (sh_addr == 0
12468 || sh_addr > hdr->sh_addr)
12469 sh_addr = hdr->sh_addr;
c152c796
AM
12470 }
12471 }
64487780 12472
64f52338
AM
12473 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12474 {
12475 /* Don't count procedure linkage table relocs in the
12476 overall reloc count. */
64487780
AM
12477 sh_size -= htab->srelplt->size;
12478 if (sh_size == 0)
12479 /* If the size is zero, make the address zero too.
12480 This is to avoid a glibc bug. If the backend
12481 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12482 zero, then we'll put DT_RELA at the end of
12483 DT_JMPREL. glibc will interpret the end of
12484 DT_RELA matching the end of DT_JMPREL as the
12485 case where DT_RELA includes DT_JMPREL, and for
12486 LD_BIND_NOW will decide that processing DT_RELA
12487 will process the PLT relocs too. Net result:
12488 No PLT relocs applied. */
12489 sh_addr = 0;
12490
64f52338
AM
12491 /* If .rela.plt is the first .rela section, exclude
12492 it from DT_RELA. */
64487780
AM
12493 else if (sh_addr == (htab->srelplt->output_section->vma
12494 + htab->srelplt->output_offset))
12495 sh_addr += htab->srelplt->size;
64f52338 12496 }
64487780
AM
12497
12498 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12499 dyn.d_un.d_val = sh_size;
12500 else
12501 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12502 break;
12503 }
12504 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12505 }
12506 }
12507
12508 /* If we have created any dynamic sections, then output them. */
12509 if (dynobj != NULL)
12510 {
12511 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12512 goto error_return;
12513
943284cc 12514 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12515 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12516 || info->error_textrel)
3d4d4302 12517 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12518 {
12519 bfd_byte *dyncon, *dynconend;
12520
943284cc
DJ
12521 dyncon = o->contents;
12522 dynconend = o->contents + o->size;
12523 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12524 {
12525 Elf_Internal_Dyn dyn;
12526
12527 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12528
12529 if (dyn.d_tag == DT_TEXTREL)
12530 {
c192a133
AM
12531 if (info->error_textrel)
12532 info->callbacks->einfo
12533 (_("%P%X: read-only segment has dynamic relocations.\n"));
12534 else
12535 info->callbacks->einfo
12536 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12537 break;
12538 }
12539 }
12540 }
12541
c152c796
AM
12542 for (o = dynobj->sections; o != NULL; o = o->next)
12543 {
12544 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12545 || o->size == 0
c152c796
AM
12546 || o->output_section == bfd_abs_section_ptr)
12547 continue;
12548 if ((o->flags & SEC_LINKER_CREATED) == 0)
12549 {
12550 /* At this point, we are only interested in sections
12551 created by _bfd_elf_link_create_dynamic_sections. */
12552 continue;
12553 }
64f52338 12554 if (htab->stab_info.stabstr == o)
3722b82f 12555 continue;
64f52338 12556 if (htab->eh_info.hdr_sec == o)
eea6121a 12557 continue;
3d4d4302 12558 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12559 {
12560 if (! bfd_set_section_contents (abfd, o->output_section,
12561 o->contents,
37b01f6a
DG
12562 (file_ptr) o->output_offset
12563 * bfd_octets_per_byte (abfd),
eea6121a 12564 o->size))
c152c796
AM
12565 goto error_return;
12566 }
12567 else
12568 {
12569 /* The contents of the .dynstr section are actually in a
12570 stringtab. */
8539e4e8
AM
12571 file_ptr off;
12572
c152c796
AM
12573 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12574 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12575 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12576 goto error_return;
12577 }
12578 }
12579 }
12580
7bdf4127 12581 if (!info->resolve_section_groups)
c152c796
AM
12582 {
12583 bfd_boolean failed = FALSE;
12584
7bdf4127 12585 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12586 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12587 if (failed)
12588 goto error_return;
12589 }
12590
12591 /* If we have optimized stabs strings, output them. */
64f52338 12592 if (htab->stab_info.stabstr != NULL)
c152c796 12593 {
64f52338 12594 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12595 goto error_return;
12596 }
12597
9f7c3e5e
AM
12598 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12599 goto error_return;
c152c796 12600
9f7c3e5e 12601 elf_final_link_free (abfd, &flinfo);
c152c796 12602
12bd6957 12603 elf_linker (abfd) = TRUE;
c152c796 12604
104d59d1
JM
12605 if (attr_section)
12606 {
a50b1753 12607 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12608 if (contents == NULL)
d0f16d5e 12609 return FALSE; /* Bail out and fail. */
104d59d1
JM
12610 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12611 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12612 free (contents);
12613 }
12614
c152c796
AM
12615 return TRUE;
12616
12617 error_return:
9f7c3e5e 12618 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12619 return FALSE;
12620}
12621\f
5241d853
RS
12622/* Initialize COOKIE for input bfd ABFD. */
12623
12624static bfd_boolean
12625init_reloc_cookie (struct elf_reloc_cookie *cookie,
12626 struct bfd_link_info *info, bfd *abfd)
12627{
12628 Elf_Internal_Shdr *symtab_hdr;
12629 const struct elf_backend_data *bed;
12630
12631 bed = get_elf_backend_data (abfd);
12632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12633
12634 cookie->abfd = abfd;
12635 cookie->sym_hashes = elf_sym_hashes (abfd);
12636 cookie->bad_symtab = elf_bad_symtab (abfd);
12637 if (cookie->bad_symtab)
12638 {
12639 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12640 cookie->extsymoff = 0;
12641 }
12642 else
12643 {
12644 cookie->locsymcount = symtab_hdr->sh_info;
12645 cookie->extsymoff = symtab_hdr->sh_info;
12646 }
12647
12648 if (bed->s->arch_size == 32)
12649 cookie->r_sym_shift = 8;
12650 else
12651 cookie->r_sym_shift = 32;
12652
12653 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12654 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12655 {
12656 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12657 cookie->locsymcount, 0,
12658 NULL, NULL, NULL);
12659 if (cookie->locsyms == NULL)
12660 {
12661 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12662 return FALSE;
12663 }
12664 if (info->keep_memory)
12665 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12666 }
12667 return TRUE;
12668}
12669
12670/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12671
12672static void
12673fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12674{
12675 Elf_Internal_Shdr *symtab_hdr;
12676
12677 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12678 if (cookie->locsyms != NULL
12679 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12680 free (cookie->locsyms);
12681}
12682
12683/* Initialize the relocation information in COOKIE for input section SEC
12684 of input bfd ABFD. */
12685
12686static bfd_boolean
12687init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12688 struct bfd_link_info *info, bfd *abfd,
12689 asection *sec)
12690{
5241d853
RS
12691 if (sec->reloc_count == 0)
12692 {
12693 cookie->rels = NULL;
12694 cookie->relend = NULL;
12695 }
12696 else
12697 {
5241d853
RS
12698 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12699 info->keep_memory);
12700 if (cookie->rels == NULL)
12701 return FALSE;
12702 cookie->rel = cookie->rels;
056bafd4 12703 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12704 }
12705 cookie->rel = cookie->rels;
12706 return TRUE;
12707}
12708
12709/* Free the memory allocated by init_reloc_cookie_rels,
12710 if appropriate. */
12711
12712static void
12713fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12714 asection *sec)
12715{
12716 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12717 free (cookie->rels);
12718}
12719
12720/* Initialize the whole of COOKIE for input section SEC. */
12721
12722static bfd_boolean
12723init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12724 struct bfd_link_info *info,
12725 asection *sec)
12726{
12727 if (!init_reloc_cookie (cookie, info, sec->owner))
12728 goto error1;
12729 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12730 goto error2;
12731 return TRUE;
12732
12733 error2:
12734 fini_reloc_cookie (cookie, sec->owner);
12735 error1:
12736 return FALSE;
12737}
12738
12739/* Free the memory allocated by init_reloc_cookie_for_section,
12740 if appropriate. */
12741
12742static void
12743fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12744 asection *sec)
12745{
12746 fini_reloc_cookie_rels (cookie, sec);
12747 fini_reloc_cookie (cookie, sec->owner);
12748}
12749\f
c152c796
AM
12750/* Garbage collect unused sections. */
12751
07adf181
AM
12752/* Default gc_mark_hook. */
12753
12754asection *
12755_bfd_elf_gc_mark_hook (asection *sec,
12756 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12757 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12758 struct elf_link_hash_entry *h,
12759 Elf_Internal_Sym *sym)
12760{
12761 if (h != NULL)
12762 {
12763 switch (h->root.type)
12764 {
12765 case bfd_link_hash_defined:
12766 case bfd_link_hash_defweak:
12767 return h->root.u.def.section;
12768
12769 case bfd_link_hash_common:
12770 return h->root.u.c.p->section;
12771
12772 default:
12773 break;
12774 }
12775 }
12776 else
12777 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12778
12779 return NULL;
12780}
12781
b7c871ed
L
12782/* Return the global debug definition section. */
12783
12784static asection *
12785elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12786 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12787 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12788 struct elf_link_hash_entry *h,
12789 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12790{
12791 if (h != NULL
12792 && (h->root.type == bfd_link_hash_defined
12793 || h->root.type == bfd_link_hash_defweak)
12794 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12795 return h->root.u.def.section;
12796
12797 return NULL;
12798}
12799
5241d853
RS
12800/* COOKIE->rel describes a relocation against section SEC, which is
12801 a section we've decided to keep. Return the section that contains
12802 the relocation symbol, or NULL if no section contains it. */
12803
12804asection *
12805_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12806 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12807 struct elf_reloc_cookie *cookie,
12808 bfd_boolean *start_stop)
5241d853
RS
12809{
12810 unsigned long r_symndx;
12811 struct elf_link_hash_entry *h;
12812
12813 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12814 if (r_symndx == STN_UNDEF)
5241d853
RS
12815 return NULL;
12816
12817 if (r_symndx >= cookie->locsymcount
12818 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12819 {
12820 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12821 if (h == NULL)
12822 {
12823 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12824 sec->owner);
12825 return NULL;
12826 }
5241d853
RS
12827 while (h->root.type == bfd_link_hash_indirect
12828 || h->root.type == bfd_link_hash_warning)
12829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12830 h->mark = 1;
4e6b54a6
AM
12831 /* If this symbol is weak and there is a non-weak definition, we
12832 keep the non-weak definition because many backends put
12833 dynamic reloc info on the non-weak definition for code
12834 handling copy relocs. */
60d67dc8
AM
12835 if (h->is_weakalias)
12836 weakdef (h)->mark = 1;
1cce69b9 12837
a6a4679f 12838 if (start_stop != NULL)
1cce69b9 12839 {
7dba9362
AM
12840 /* To work around a glibc bug, mark XXX input sections
12841 when there is a reference to __start_XXX or __stop_XXX
12842 symbols. */
cbd0eecf 12843 if (h->start_stop)
1cce69b9 12844 {
cbd0eecf 12845 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12846 *start_stop = !s->gc_mark;
12847 return s;
1cce69b9
AM
12848 }
12849 }
12850
5241d853
RS
12851 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12852 }
12853
12854 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12855 &cookie->locsyms[r_symndx]);
12856}
12857
12858/* COOKIE->rel describes a relocation against section SEC, which is
12859 a section we've decided to keep. Mark the section that contains
9d0a14d3 12860 the relocation symbol. */
5241d853
RS
12861
12862bfd_boolean
12863_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12864 asection *sec,
12865 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12866 struct elf_reloc_cookie *cookie)
5241d853
RS
12867{
12868 asection *rsec;
1cce69b9 12869 bfd_boolean start_stop = FALSE;
5241d853 12870
1cce69b9
AM
12871 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12872 while (rsec != NULL)
5241d853 12873 {
1cce69b9
AM
12874 if (!rsec->gc_mark)
12875 {
12876 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12877 || (rsec->owner->flags & DYNAMIC) != 0)
12878 rsec->gc_mark = 1;
12879 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12880 return FALSE;
12881 }
12882 if (!start_stop)
12883 break;
199af150 12884 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12885 }
12886 return TRUE;
12887}
12888
07adf181
AM
12889/* The mark phase of garbage collection. For a given section, mark
12890 it and any sections in this section's group, and all the sections
12891 which define symbols to which it refers. */
12892
ccfa59ea
AM
12893bfd_boolean
12894_bfd_elf_gc_mark (struct bfd_link_info *info,
12895 asection *sec,
6a5bb875 12896 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12897{
12898 bfd_boolean ret;
9d0a14d3 12899 asection *group_sec, *eh_frame;
c152c796
AM
12900
12901 sec->gc_mark = 1;
12902
12903 /* Mark all the sections in the group. */
12904 group_sec = elf_section_data (sec)->next_in_group;
12905 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12906 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12907 return FALSE;
12908
12909 /* Look through the section relocs. */
12910 ret = TRUE;
9d0a14d3
RS
12911 eh_frame = elf_eh_frame_section (sec->owner);
12912 if ((sec->flags & SEC_RELOC) != 0
12913 && sec->reloc_count > 0
12914 && sec != eh_frame)
c152c796 12915 {
5241d853 12916 struct elf_reloc_cookie cookie;
c152c796 12917
5241d853
RS
12918 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12919 ret = FALSE;
c152c796 12920 else
c152c796 12921 {
5241d853 12922 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12923 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12924 {
12925 ret = FALSE;
12926 break;
12927 }
12928 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12929 }
12930 }
9d0a14d3
RS
12931
12932 if (ret && eh_frame && elf_fde_list (sec))
12933 {
12934 struct elf_reloc_cookie cookie;
12935
12936 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12937 ret = FALSE;
12938 else
12939 {
12940 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12941 gc_mark_hook, &cookie))
12942 ret = FALSE;
12943 fini_reloc_cookie_for_section (&cookie, eh_frame);
12944 }
12945 }
12946
2f0c68f2
CM
12947 eh_frame = elf_section_eh_frame_entry (sec);
12948 if (ret && eh_frame && !eh_frame->gc_mark)
12949 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12950 ret = FALSE;
12951
c152c796
AM
12952 return ret;
12953}
12954
3c758495
TG
12955/* Scan and mark sections in a special or debug section group. */
12956
12957static void
12958_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12959{
12960 /* Point to first section of section group. */
12961 asection *ssec;
12962 /* Used to iterate the section group. */
12963 asection *msec;
12964
12965 bfd_boolean is_special_grp = TRUE;
12966 bfd_boolean is_debug_grp = TRUE;
12967
12968 /* First scan to see if group contains any section other than debug
12969 and special section. */
12970 ssec = msec = elf_next_in_group (grp);
12971 do
12972 {
12973 if ((msec->flags & SEC_DEBUGGING) == 0)
12974 is_debug_grp = FALSE;
12975
12976 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12977 is_special_grp = FALSE;
12978
12979 msec = elf_next_in_group (msec);
12980 }
12981 while (msec != ssec);
12982
12983 /* If this is a pure debug section group or pure special section group,
12984 keep all sections in this group. */
12985 if (is_debug_grp || is_special_grp)
12986 {
12987 do
12988 {
12989 msec->gc_mark = 1;
12990 msec = elf_next_in_group (msec);
12991 }
12992 while (msec != ssec);
12993 }
12994}
12995
7f6ab9f8
AM
12996/* Keep debug and special sections. */
12997
12998bfd_boolean
12999_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13000 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13001{
13002 bfd *ibfd;
13003
c72f2fb2 13004 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13005 {
13006 asection *isec;
13007 bfd_boolean some_kept;
b40bf0a2 13008 bfd_boolean debug_frag_seen;
b7c871ed 13009 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13010
13011 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13012 continue;
57963c05
AM
13013 isec = ibfd->sections;
13014 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13015 continue;
7f6ab9f8 13016
b40bf0a2
NC
13017 /* Ensure all linker created sections are kept,
13018 see if any other section is already marked,
13019 and note if we have any fragmented debug sections. */
b7c871ed 13020 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13021 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13022 {
13023 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13024 isec->gc_mark = 1;
eb026f09
AM
13025 else if (isec->gc_mark
13026 && (isec->flags & SEC_ALLOC) != 0
13027 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13028 some_kept = TRUE;
b40bf0a2 13029
535b785f 13030 if (!debug_frag_seen
b40bf0a2
NC
13031 && (isec->flags & SEC_DEBUGGING)
13032 && CONST_STRNEQ (isec->name, ".debug_line."))
13033 debug_frag_seen = TRUE;
7f6ab9f8
AM
13034 }
13035
eb026f09
AM
13036 /* If no non-note alloc section in this file will be kept, then
13037 we can toss out the debug and special sections. */
7f6ab9f8
AM
13038 if (!some_kept)
13039 continue;
13040
13041 /* Keep debug and special sections like .comment when they are
3c758495
TG
13042 not part of a group. Also keep section groups that contain
13043 just debug sections or special sections. */
7f6ab9f8 13044 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13045 {
13046 if ((isec->flags & SEC_GROUP) != 0)
13047 _bfd_elf_gc_mark_debug_special_section_group (isec);
13048 else if (((isec->flags & SEC_DEBUGGING) != 0
13049 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13050 && elf_next_in_group (isec) == NULL)
13051 isec->gc_mark = 1;
b7c871ed
L
13052 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13053 has_kept_debug_info = TRUE;
3c758495 13054 }
b40bf0a2 13055
b40bf0a2
NC
13056 /* Look for CODE sections which are going to be discarded,
13057 and find and discard any fragmented debug sections which
13058 are associated with that code section. */
b7c871ed
L
13059 if (debug_frag_seen)
13060 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13061 if ((isec->flags & SEC_CODE) != 0
13062 && isec->gc_mark == 0)
13063 {
13064 unsigned int ilen;
13065 asection *dsec;
b40bf0a2 13066
b7c871ed 13067 ilen = strlen (isec->name);
b40bf0a2 13068
b7c871ed 13069 /* Association is determined by the name of the debug
07d6d2b8 13070 section containing the name of the code section as
b7c871ed
L
13071 a suffix. For example .debug_line.text.foo is a
13072 debug section associated with .text.foo. */
13073 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13074 {
13075 unsigned int dlen;
b40bf0a2 13076
b7c871ed
L
13077 if (dsec->gc_mark == 0
13078 || (dsec->flags & SEC_DEBUGGING) == 0)
13079 continue;
b40bf0a2 13080
b7c871ed 13081 dlen = strlen (dsec->name);
b40bf0a2 13082
b7c871ed
L
13083 if (dlen > ilen
13084 && strncmp (dsec->name + (dlen - ilen),
13085 isec->name, ilen) == 0)
b40bf0a2 13086 dsec->gc_mark = 0;
b7c871ed 13087 }
b40bf0a2 13088 }
b7c871ed
L
13089
13090 /* Mark debug sections referenced by kept debug sections. */
13091 if (has_kept_debug_info)
13092 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13093 if (isec->gc_mark
13094 && (isec->flags & SEC_DEBUGGING) != 0)
13095 if (!_bfd_elf_gc_mark (info, isec,
13096 elf_gc_mark_debug_section))
13097 return FALSE;
7f6ab9f8
AM
13098 }
13099 return TRUE;
13100}
13101
c152c796 13102static bfd_boolean
ccabcbe5 13103elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13104{
13105 bfd *sub;
ccabcbe5 13106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13107
c72f2fb2 13108 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13109 {
13110 asection *o;
13111
b19a8f85 13112 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13113 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13114 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13115 continue;
57963c05
AM
13116 o = sub->sections;
13117 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13118 continue;
c152c796
AM
13119
13120 for (o = sub->sections; o != NULL; o = o->next)
13121 {
a33dafc3
L
13122 /* When any section in a section group is kept, we keep all
13123 sections in the section group. If the first member of
13124 the section group is excluded, we will also exclude the
13125 group section. */
13126 if (o->flags & SEC_GROUP)
13127 {
13128 asection *first = elf_next_in_group (o);
13129 o->gc_mark = first->gc_mark;
13130 }
c152c796 13131
1e7eae0d 13132 if (o->gc_mark)
c152c796
AM
13133 continue;
13134
13135 /* Skip sweeping sections already excluded. */
13136 if (o->flags & SEC_EXCLUDE)
13137 continue;
13138
13139 /* Since this is early in the link process, it is simple
13140 to remove a section from the output. */
13141 o->flags |= SEC_EXCLUDE;
13142
c55fe096 13143 if (info->print_gc_sections && o->size != 0)
695344c0 13144 /* xgettext:c-format */
c08bb8dd
AM
13145 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13146 o, sub);
c152c796
AM
13147 }
13148 }
13149
c152c796
AM
13150 return TRUE;
13151}
13152
13153/* Propagate collected vtable information. This is called through
13154 elf_link_hash_traverse. */
13155
13156static bfd_boolean
13157elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13158{
c152c796 13159 /* Those that are not vtables. */
cbd0eecf
L
13160 if (h->start_stop
13161 || h->u2.vtable == NULL
13162 || h->u2.vtable->parent == NULL)
c152c796
AM
13163 return TRUE;
13164
13165 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13166 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13167 return TRUE;
13168
13169 /* If we've already been done, exit. */
cbd0eecf 13170 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13171 return TRUE;
13172
13173 /* Make sure the parent's table is up to date. */
cbd0eecf 13174 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13175
cbd0eecf 13176 if (h->u2.vtable->used == NULL)
c152c796
AM
13177 {
13178 /* None of this table's entries were referenced. Re-use the
13179 parent's table. */
cbd0eecf
L
13180 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13181 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13182 }
13183 else
13184 {
13185 size_t n;
13186 bfd_boolean *cu, *pu;
13187
13188 /* Or the parent's entries into ours. */
cbd0eecf 13189 cu = h->u2.vtable->used;
c152c796 13190 cu[-1] = TRUE;
cbd0eecf 13191 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13192 if (pu != NULL)
13193 {
13194 const struct elf_backend_data *bed;
13195 unsigned int log_file_align;
13196
13197 bed = get_elf_backend_data (h->root.u.def.section->owner);
13198 log_file_align = bed->s->log_file_align;
cbd0eecf 13199 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13200 while (n--)
13201 {
13202 if (*pu)
13203 *cu = TRUE;
13204 pu++;
13205 cu++;
13206 }
13207 }
13208 }
13209
13210 return TRUE;
13211}
13212
13213static bfd_boolean
13214elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13215{
13216 asection *sec;
13217 bfd_vma hstart, hend;
13218 Elf_Internal_Rela *relstart, *relend, *rel;
13219 const struct elf_backend_data *bed;
13220 unsigned int log_file_align;
13221
c152c796
AM
13222 /* Take care of both those symbols that do not describe vtables as
13223 well as those that are not loaded. */
cbd0eecf
L
13224 if (h->start_stop
13225 || h->u2.vtable == NULL
13226 || h->u2.vtable->parent == NULL)
c152c796
AM
13227 return TRUE;
13228
13229 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13230 || h->root.type == bfd_link_hash_defweak);
13231
13232 sec = h->root.u.def.section;
13233 hstart = h->root.u.def.value;
13234 hend = hstart + h->size;
13235
13236 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13237 if (!relstart)
13238 return *(bfd_boolean *) okp = FALSE;
13239 bed = get_elf_backend_data (sec->owner);
13240 log_file_align = bed->s->log_file_align;
13241
056bafd4 13242 relend = relstart + sec->reloc_count;
c152c796
AM
13243
13244 for (rel = relstart; rel < relend; ++rel)
13245 if (rel->r_offset >= hstart && rel->r_offset < hend)
13246 {
13247 /* If the entry is in use, do nothing. */
cbd0eecf
L
13248 if (h->u2.vtable->used
13249 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13250 {
13251 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13252 if (h->u2.vtable->used[entry])
c152c796
AM
13253 continue;
13254 }
13255 /* Otherwise, kill it. */
13256 rel->r_offset = rel->r_info = rel->r_addend = 0;
13257 }
13258
13259 return TRUE;
13260}
13261
87538722
AM
13262/* Mark sections containing dynamically referenced symbols. When
13263 building shared libraries, we must assume that any visible symbol is
13264 referenced. */
715df9b8 13265
64d03ab5
AM
13266bfd_boolean
13267bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13268{
87538722 13269 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13270 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13271
715df9b8
EB
13272 if ((h->root.type == bfd_link_hash_defined
13273 || h->root.type == bfd_link_hash_defweak)
87538722 13274 && (h->ref_dynamic
c4621b33 13275 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13276 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13277 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13278 && (!bfd_link_executable (info)
22185505 13279 || info->gc_keep_exported
b407645f
AM
13280 || info->export_dynamic
13281 || (h->dynamic
13282 && d != NULL
13283 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13284 && (h->versioned >= versioned
54e8959c
L
13285 || !bfd_hide_sym_by_version (info->version_info,
13286 h->root.root.string)))))
715df9b8
EB
13287 h->root.u.def.section->flags |= SEC_KEEP;
13288
13289 return TRUE;
13290}
3b36f7e6 13291
74f0fb50
AM
13292/* Keep all sections containing symbols undefined on the command-line,
13293 and the section containing the entry symbol. */
13294
13295void
13296_bfd_elf_gc_keep (struct bfd_link_info *info)
13297{
13298 struct bfd_sym_chain *sym;
13299
13300 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13301 {
13302 struct elf_link_hash_entry *h;
13303
13304 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13305 FALSE, FALSE, FALSE);
13306
13307 if (h != NULL
13308 && (h->root.type == bfd_link_hash_defined
13309 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13310 && !bfd_is_abs_section (h->root.u.def.section)
13311 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13312 h->root.u.def.section->flags |= SEC_KEEP;
13313 }
13314}
13315
2f0c68f2
CM
13316bfd_boolean
13317bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13318 struct bfd_link_info *info)
13319{
13320 bfd *ibfd = info->input_bfds;
13321
13322 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13323 {
13324 asection *sec;
13325 struct elf_reloc_cookie cookie;
13326
13327 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13328 continue;
57963c05
AM
13329 sec = ibfd->sections;
13330 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13331 continue;
2f0c68f2
CM
13332
13333 if (!init_reloc_cookie (&cookie, info, ibfd))
13334 return FALSE;
13335
13336 for (sec = ibfd->sections; sec; sec = sec->next)
13337 {
13338 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13339 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13340 {
13341 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13342 fini_reloc_cookie_rels (&cookie, sec);
13343 }
13344 }
13345 }
13346 return TRUE;
13347}
13348
c152c796
AM
13349/* Do mark and sweep of unused sections. */
13350
13351bfd_boolean
13352bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13353{
13354 bfd_boolean ok = TRUE;
13355 bfd *sub;
6a5bb875 13356 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13357 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13358 struct elf_link_hash_table *htab;
c152c796 13359
64d03ab5 13360 if (!bed->can_gc_sections
715df9b8 13361 || !is_elf_hash_table (info->hash))
c152c796 13362 {
4eca0228 13363 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13364 return TRUE;
13365 }
13366
74f0fb50 13367 bed->gc_keep (info);
da44f4e5 13368 htab = elf_hash_table (info);
74f0fb50 13369
9d0a14d3
RS
13370 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13371 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13372 for (sub = info->input_bfds;
13373 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13374 sub = sub->link.next)
9d0a14d3
RS
13375 {
13376 asection *sec;
13377 struct elf_reloc_cookie cookie;
13378
57963c05
AM
13379 sec = sub->sections;
13380 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13381 continue;
9d0a14d3 13382 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13383 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13384 {
13385 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13386 if (elf_section_data (sec)->sec_info
13387 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13388 elf_eh_frame_section (sub) = sec;
13389 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13390 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13391 }
13392 }
9d0a14d3 13393
c152c796 13394 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13395 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13396 if (!ok)
13397 return FALSE;
13398
13399 /* Kill the vtable relocations that were not used. */
da44f4e5 13400 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13401 if (!ok)
13402 return FALSE;
13403
715df9b8 13404 /* Mark dynamically referenced symbols. */
22185505 13405 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13406 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13407
715df9b8 13408 /* Grovel through relocs to find out who stays ... */
64d03ab5 13409 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13410 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13411 {
13412 asection *o;
13413
b19a8f85 13414 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13415 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13416 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13417 continue;
13418
57963c05
AM
13419 o = sub->sections;
13420 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13421 continue;
13422
7f6ab9f8
AM
13423 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13424 Also treat note sections as a root, if the section is not part
13425 of a group. */
c152c796 13426 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13427 if (!o->gc_mark
13428 && (o->flags & SEC_EXCLUDE) == 0
24007750 13429 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13430 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13431 && elf_next_in_group (o) == NULL )))
13432 {
13433 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13434 return FALSE;
13435 }
c152c796
AM
13436 }
13437
6a5bb875 13438 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13439 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13440
c152c796 13441 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13442 return elf_gc_sweep (abfd, info);
c152c796
AM
13443}
13444\f
13445/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13446
13447bfd_boolean
13448bfd_elf_gc_record_vtinherit (bfd *abfd,
13449 asection *sec,
13450 struct elf_link_hash_entry *h,
13451 bfd_vma offset)
13452{
13453 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13454 struct elf_link_hash_entry **search, *child;
ef53be89 13455 size_t extsymcount;
c152c796
AM
13456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13457
13458 /* The sh_info field of the symtab header tells us where the
13459 external symbols start. We don't care about the local symbols at
13460 this point. */
13461 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13462 if (!elf_bad_symtab (abfd))
13463 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13464
13465 sym_hashes = elf_sym_hashes (abfd);
13466 sym_hashes_end = sym_hashes + extsymcount;
13467
13468 /* Hunt down the child symbol, which is in this section at the same
13469 offset as the relocation. */
13470 for (search = sym_hashes; search != sym_hashes_end; ++search)
13471 {
13472 if ((child = *search) != NULL
13473 && (child->root.type == bfd_link_hash_defined
13474 || child->root.type == bfd_link_hash_defweak)
13475 && child->root.u.def.section == sec
13476 && child->root.u.def.value == offset)
13477 goto win;
13478 }
13479
695344c0 13480 /* xgettext:c-format */
76cfced5
AM
13481 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13482 abfd, sec, offset);
c152c796
AM
13483 bfd_set_error (bfd_error_invalid_operation);
13484 return FALSE;
13485
13486 win:
cbd0eecf 13487 if (!child->u2.vtable)
f6e332e6 13488 {
cbd0eecf
L
13489 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13490 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13491 if (!child->u2.vtable)
f6e332e6
AM
13492 return FALSE;
13493 }
c152c796
AM
13494 if (!h)
13495 {
13496 /* This *should* only be the absolute section. It could potentially
13497 be that someone has defined a non-global vtable though, which
13498 would be bad. It isn't worth paging in the local symbols to be
13499 sure though; that case should simply be handled by the assembler. */
13500
cbd0eecf 13501 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13502 }
13503 else
cbd0eecf 13504 child->u2.vtable->parent = h;
c152c796
AM
13505
13506 return TRUE;
13507}
13508
13509/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13510
13511bfd_boolean
13512bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13513 asection *sec ATTRIBUTE_UNUSED,
13514 struct elf_link_hash_entry *h,
13515 bfd_vma addend)
13516{
13517 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13518 unsigned int log_file_align = bed->s->log_file_align;
13519
cbd0eecf 13520 if (!h->u2.vtable)
f6e332e6 13521 {
cbd0eecf
L
13522 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13523 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13524 if (!h->u2.vtable)
f6e332e6
AM
13525 return FALSE;
13526 }
13527
cbd0eecf 13528 if (addend >= h->u2.vtable->size)
c152c796
AM
13529 {
13530 size_t size, bytes, file_align;
cbd0eecf 13531 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13532
13533 /* While the symbol is undefined, we have to be prepared to handle
13534 a zero size. */
13535 file_align = 1 << log_file_align;
13536 if (h->root.type == bfd_link_hash_undefined)
13537 size = addend + file_align;
13538 else
13539 {
13540 size = h->size;
13541 if (addend >= size)
13542 {
13543 /* Oops! We've got a reference past the defined end of
13544 the table. This is probably a bug -- shall we warn? */
13545 size = addend + file_align;
13546 }
13547 }
13548 size = (size + file_align - 1) & -file_align;
13549
13550 /* Allocate one extra entry for use as a "done" flag for the
13551 consolidation pass. */
13552 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13553
13554 if (ptr)
13555 {
a50b1753 13556 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13557
13558 if (ptr != NULL)
13559 {
13560 size_t oldbytes;
13561
cbd0eecf 13562 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13563 * sizeof (bfd_boolean));
13564 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13565 }
13566 }
13567 else
a50b1753 13568 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13569
13570 if (ptr == NULL)
13571 return FALSE;
13572
13573 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13574 h->u2.vtable->used = ptr + 1;
13575 h->u2.vtable->size = size;
c152c796
AM
13576 }
13577
cbd0eecf 13578 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13579
13580 return TRUE;
13581}
13582
ae17ab41
CM
13583/* Map an ELF section header flag to its corresponding string. */
13584typedef struct
13585{
13586 char *flag_name;
13587 flagword flag_value;
13588} elf_flags_to_name_table;
13589
13590static elf_flags_to_name_table elf_flags_to_names [] =
13591{
13592 { "SHF_WRITE", SHF_WRITE },
13593 { "SHF_ALLOC", SHF_ALLOC },
13594 { "SHF_EXECINSTR", SHF_EXECINSTR },
13595 { "SHF_MERGE", SHF_MERGE },
13596 { "SHF_STRINGS", SHF_STRINGS },
13597 { "SHF_INFO_LINK", SHF_INFO_LINK},
13598 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13599 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13600 { "SHF_GROUP", SHF_GROUP },
13601 { "SHF_TLS", SHF_TLS },
13602 { "SHF_MASKOS", SHF_MASKOS },
13603 { "SHF_EXCLUDE", SHF_EXCLUDE },
13604};
13605
b9c361e0
JL
13606/* Returns TRUE if the section is to be included, otherwise FALSE. */
13607bfd_boolean
ae17ab41 13608bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13609 struct flag_info *flaginfo,
b9c361e0 13610 asection *section)
ae17ab41 13611{
8b127cbc 13612 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13613
8b127cbc 13614 if (!flaginfo->flags_initialized)
ae17ab41 13615 {
8b127cbc
AM
13616 bfd *obfd = info->output_bfd;
13617 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13618 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13619 int with_hex = 0;
13620 int without_hex = 0;
13621
8b127cbc 13622 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13623 {
b9c361e0 13624 unsigned i;
8b127cbc 13625 flagword (*lookup) (char *);
ae17ab41 13626
8b127cbc
AM
13627 lookup = bed->elf_backend_lookup_section_flags_hook;
13628 if (lookup != NULL)
ae17ab41 13629 {
8b127cbc 13630 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13631
13632 if (hexval != 0)
13633 {
13634 if (tf->with == with_flags)
13635 with_hex |= hexval;
13636 else if (tf->with == without_flags)
13637 without_hex |= hexval;
13638 tf->valid = TRUE;
13639 continue;
13640 }
ae17ab41 13641 }
8b127cbc 13642 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13643 {
8b127cbc 13644 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13645 {
13646 if (tf->with == with_flags)
13647 with_hex |= elf_flags_to_names[i].flag_value;
13648 else if (tf->with == without_flags)
13649 without_hex |= elf_flags_to_names[i].flag_value;
13650 tf->valid = TRUE;
13651 break;
13652 }
13653 }
8b127cbc 13654 if (!tf->valid)
b9c361e0 13655 {
68ffbac6 13656 info->callbacks->einfo
8b127cbc 13657 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13658 return FALSE;
ae17ab41
CM
13659 }
13660 }
8b127cbc
AM
13661 flaginfo->flags_initialized = TRUE;
13662 flaginfo->only_with_flags |= with_hex;
13663 flaginfo->not_with_flags |= without_hex;
ae17ab41 13664 }
ae17ab41 13665
8b127cbc 13666 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13667 return FALSE;
13668
8b127cbc 13669 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13670 return FALSE;
13671
13672 return TRUE;
ae17ab41
CM
13673}
13674
c152c796
AM
13675struct alloc_got_off_arg {
13676 bfd_vma gotoff;
10455f89 13677 struct bfd_link_info *info;
c152c796
AM
13678};
13679
13680/* We need a special top-level link routine to convert got reference counts
13681 to real got offsets. */
13682
13683static bfd_boolean
13684elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13685{
a50b1753 13686 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13687 bfd *obfd = gofarg->info->output_bfd;
13688 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13689
c152c796
AM
13690 if (h->got.refcount > 0)
13691 {
13692 h->got.offset = gofarg->gotoff;
10455f89 13693 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13694 }
13695 else
13696 h->got.offset = (bfd_vma) -1;
13697
13698 return TRUE;
13699}
13700
13701/* And an accompanying bit to work out final got entry offsets once
13702 we're done. Should be called from final_link. */
13703
13704bfd_boolean
13705bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13706 struct bfd_link_info *info)
13707{
13708 bfd *i;
13709 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13710 bfd_vma gotoff;
c152c796
AM
13711 struct alloc_got_off_arg gofarg;
13712
10455f89
HPN
13713 BFD_ASSERT (abfd == info->output_bfd);
13714
c152c796
AM
13715 if (! is_elf_hash_table (info->hash))
13716 return FALSE;
13717
13718 /* The GOT offset is relative to the .got section, but the GOT header is
13719 put into the .got.plt section, if the backend uses it. */
13720 if (bed->want_got_plt)
13721 gotoff = 0;
13722 else
13723 gotoff = bed->got_header_size;
13724
13725 /* Do the local .got entries first. */
c72f2fb2 13726 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13727 {
13728 bfd_signed_vma *local_got;
ef53be89 13729 size_t j, locsymcount;
c152c796
AM
13730 Elf_Internal_Shdr *symtab_hdr;
13731
13732 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13733 continue;
13734
13735 local_got = elf_local_got_refcounts (i);
13736 if (!local_got)
13737 continue;
13738
13739 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13740 if (elf_bad_symtab (i))
13741 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13742 else
13743 locsymcount = symtab_hdr->sh_info;
13744
13745 for (j = 0; j < locsymcount; ++j)
13746 {
13747 if (local_got[j] > 0)
13748 {
13749 local_got[j] = gotoff;
10455f89 13750 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13751 }
13752 else
13753 local_got[j] = (bfd_vma) -1;
13754 }
13755 }
13756
13757 /* Then the global .got entries. .plt refcounts are handled by
13758 adjust_dynamic_symbol */
13759 gofarg.gotoff = gotoff;
10455f89 13760 gofarg.info = info;
c152c796
AM
13761 elf_link_hash_traverse (elf_hash_table (info),
13762 elf_gc_allocate_got_offsets,
13763 &gofarg);
13764 return TRUE;
13765}
13766
13767/* Many folk need no more in the way of final link than this, once
13768 got entry reference counting is enabled. */
13769
13770bfd_boolean
13771bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13772{
13773 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13774 return FALSE;
13775
13776 /* Invoke the regular ELF backend linker to do all the work. */
13777 return bfd_elf_final_link (abfd, info);
13778}
13779
13780bfd_boolean
13781bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13782{
a50b1753 13783 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13784
13785 if (rcookie->bad_symtab)
13786 rcookie->rel = rcookie->rels;
13787
13788 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13789 {
13790 unsigned long r_symndx;
13791
13792 if (! rcookie->bad_symtab)
13793 if (rcookie->rel->r_offset > offset)
13794 return FALSE;
13795 if (rcookie->rel->r_offset != offset)
13796 continue;
13797
13798 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13799 if (r_symndx == STN_UNDEF)
c152c796
AM
13800 return TRUE;
13801
13802 if (r_symndx >= rcookie->locsymcount
13803 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13804 {
13805 struct elf_link_hash_entry *h;
13806
13807 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13808
13809 while (h->root.type == bfd_link_hash_indirect
13810 || h->root.type == bfd_link_hash_warning)
13811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13812
13813 if ((h->root.type == bfd_link_hash_defined
13814 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13815 && (h->root.u.def.section->owner != rcookie->abfd
13816 || h->root.u.def.section->kept_section != NULL
13817 || discarded_section (h->root.u.def.section)))
c152c796 13818 return TRUE;
c152c796
AM
13819 }
13820 else
13821 {
13822 /* It's not a relocation against a global symbol,
13823 but it could be a relocation against a local
13824 symbol for a discarded section. */
13825 asection *isec;
13826 Elf_Internal_Sym *isym;
13827
13828 /* Need to: get the symbol; get the section. */
13829 isym = &rcookie->locsyms[r_symndx];
cb33740c 13830 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13831 if (isec != NULL
13832 && (isec->kept_section != NULL
13833 || discarded_section (isec)))
cb33740c 13834 return TRUE;
c152c796
AM
13835 }
13836 return FALSE;
13837 }
13838 return FALSE;
13839}
13840
13841/* Discard unneeded references to discarded sections.
75938853
AM
13842 Returns -1 on error, 1 if any section's size was changed, 0 if
13843 nothing changed. This function assumes that the relocations are in
13844 sorted order, which is true for all known assemblers. */
c152c796 13845
75938853 13846int
c152c796
AM
13847bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13848{
13849 struct elf_reloc_cookie cookie;
18cd5bce 13850 asection *o;
c152c796 13851 bfd *abfd;
75938853 13852 int changed = 0;
c152c796
AM
13853
13854 if (info->traditional_format
13855 || !is_elf_hash_table (info->hash))
75938853 13856 return 0;
c152c796 13857
18cd5bce
AM
13858 o = bfd_get_section_by_name (output_bfd, ".stab");
13859 if (o != NULL)
c152c796 13860 {
18cd5bce 13861 asection *i;
c152c796 13862
18cd5bce 13863 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13864 {
18cd5bce
AM
13865 if (i->size == 0
13866 || i->reloc_count == 0
13867 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13868 continue;
c152c796 13869
18cd5bce
AM
13870 abfd = i->owner;
13871 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13872 continue;
c152c796 13873
18cd5bce 13874 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13875 return -1;
c152c796 13876
18cd5bce
AM
13877 if (_bfd_discard_section_stabs (abfd, i,
13878 elf_section_data (i)->sec_info,
5241d853
RS
13879 bfd_elf_reloc_symbol_deleted_p,
13880 &cookie))
75938853 13881 changed = 1;
18cd5bce
AM
13882
13883 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13884 }
18cd5bce
AM
13885 }
13886
2f0c68f2
CM
13887 o = NULL;
13888 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13889 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13890 if (o != NULL)
13891 {
13892 asection *i;
d7153c4a 13893 int eh_changed = 0;
79a94a2a 13894 unsigned int eh_alignment;
c152c796 13895
18cd5bce 13896 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13897 {
18cd5bce
AM
13898 if (i->size == 0)
13899 continue;
13900
13901 abfd = i->owner;
13902 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13903 continue;
13904
13905 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13906 return -1;
18cd5bce
AM
13907
13908 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13909 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13910 bfd_elf_reloc_symbol_deleted_p,
13911 &cookie))
d7153c4a
AM
13912 {
13913 eh_changed = 1;
13914 if (i->size != i->rawsize)
13915 changed = 1;
13916 }
18cd5bce
AM
13917
13918 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13919 }
9866ffe2 13920
79a94a2a 13921 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
13922 /* Skip over zero terminator, and prevent empty sections from
13923 adding alignment padding at the end. */
13924 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13925 if (i->size == 0)
13926 i->flags |= SEC_EXCLUDE;
13927 else if (i->size > 4)
13928 break;
13929 /* The last non-empty eh_frame section doesn't need padding. */
13930 if (i != NULL)
13931 i = i->map_tail.s;
13932 /* Any prior sections must pad the last FDE out to the output
13933 section alignment. Otherwise we might have zero padding
13934 between sections, which would be seen as a terminator. */
13935 for (; i != NULL; i = i->map_tail.s)
13936 if (i->size == 4)
13937 /* All but the last zero terminator should have been removed. */
13938 BFD_FAIL ();
13939 else
13940 {
13941 bfd_size_type size
13942 = (i->size + eh_alignment - 1) & -eh_alignment;
13943 if (i->size != size)
af471f82 13944 {
9866ffe2
AM
13945 i->size = size;
13946 changed = 1;
13947 eh_changed = 1;
af471f82 13948 }
9866ffe2 13949 }
d7153c4a
AM
13950 if (eh_changed)
13951 elf_link_hash_traverse (elf_hash_table (info),
13952 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13953 }
c152c796 13954
18cd5bce
AM
13955 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13956 {
13957 const struct elf_backend_data *bed;
57963c05 13958 asection *s;
c152c796 13959
18cd5bce
AM
13960 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13961 continue;
57963c05
AM
13962 s = abfd->sections;
13963 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13964 continue;
18cd5bce
AM
13965
13966 bed = get_elf_backend_data (abfd);
13967
13968 if (bed->elf_backend_discard_info != NULL)
13969 {
13970 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13971 return -1;
18cd5bce
AM
13972
13973 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13974 changed = 1;
18cd5bce
AM
13975
13976 fini_reloc_cookie (&cookie, abfd);
13977 }
c152c796
AM
13978 }
13979
2f0c68f2
CM
13980 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13981 _bfd_elf_end_eh_frame_parsing (info);
13982
13983 if (info->eh_frame_hdr_type
0e1862bb 13984 && !bfd_link_relocatable (info)
c152c796 13985 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13986 changed = 1;
c152c796 13987
75938853 13988 return changed;
c152c796 13989}
082b7297 13990
43e1669b 13991bfd_boolean
0c511000 13992_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13993 asection *sec,
c0f00686 13994 struct bfd_link_info *info)
082b7297
L
13995{
13996 flagword flags;
c77ec726 13997 const char *name, *key;
082b7297
L
13998 struct bfd_section_already_linked *l;
13999 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14000
c77ec726
AM
14001 if (sec->output_section == bfd_abs_section_ptr)
14002 return FALSE;
0c511000 14003
c77ec726 14004 flags = sec->flags;
0c511000 14005
c77ec726
AM
14006 /* Return if it isn't a linkonce section. A comdat group section
14007 also has SEC_LINK_ONCE set. */
14008 if ((flags & SEC_LINK_ONCE) == 0)
14009 return FALSE;
0c511000 14010
c77ec726
AM
14011 /* Don't put group member sections on our list of already linked
14012 sections. They are handled as a group via their group section. */
14013 if (elf_sec_group (sec) != NULL)
14014 return FALSE;
0c511000 14015
c77ec726
AM
14016 /* For a SHT_GROUP section, use the group signature as the key. */
14017 name = sec->name;
14018 if ((flags & SEC_GROUP) != 0
14019 && elf_next_in_group (sec) != NULL
14020 && elf_group_name (elf_next_in_group (sec)) != NULL)
14021 key = elf_group_name (elf_next_in_group (sec));
14022 else
14023 {
14024 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14025 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14026 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14027 key++;
0c511000 14028 else
c77ec726
AM
14029 /* Must be a user linkonce section that doesn't follow gcc's
14030 naming convention. In this case we won't be matching
14031 single member groups. */
14032 key = name;
0c511000 14033 }
6d2cd210 14034
c77ec726 14035 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14036
14037 for (l = already_linked_list->entry; l != NULL; l = l->next)
14038 {
c2370991 14039 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14040 sections with a signature of <key> (<key> is some string),
14041 and linkonce sections named .gnu.linkonce.<type>.<key>.
14042 Match like sections. LTO plugin sections are an exception.
14043 They are always named .gnu.linkonce.t.<key> and match either
14044 type of section. */
14045 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14046 && ((flags & SEC_GROUP) != 0
14047 || strcmp (name, l->sec->name) == 0))
14048 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14049 {
14050 /* The section has already been linked. See if we should
6d2cd210 14051 issue a warning. */
c77ec726
AM
14052 if (!_bfd_handle_already_linked (sec, l, info))
14053 return FALSE;
082b7297 14054
c77ec726 14055 if (flags & SEC_GROUP)
3d7f7666 14056 {
c77ec726
AM
14057 asection *first = elf_next_in_group (sec);
14058 asection *s = first;
3d7f7666 14059
c77ec726 14060 while (s != NULL)
3d7f7666 14061 {
c77ec726
AM
14062 s->output_section = bfd_abs_section_ptr;
14063 /* Record which group discards it. */
14064 s->kept_section = l->sec;
14065 s = elf_next_in_group (s);
14066 /* These lists are circular. */
14067 if (s == first)
14068 break;
3d7f7666
L
14069 }
14070 }
082b7297 14071
43e1669b 14072 return TRUE;
082b7297
L
14073 }
14074 }
14075
c77ec726
AM
14076 /* A single member comdat group section may be discarded by a
14077 linkonce section and vice versa. */
14078 if ((flags & SEC_GROUP) != 0)
3d7f7666 14079 {
c77ec726 14080 asection *first = elf_next_in_group (sec);
c2370991 14081
c77ec726
AM
14082 if (first != NULL && elf_next_in_group (first) == first)
14083 /* Check this single member group against linkonce sections. */
14084 for (l = already_linked_list->entry; l != NULL; l = l->next)
14085 if ((l->sec->flags & SEC_GROUP) == 0
14086 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14087 {
14088 first->output_section = bfd_abs_section_ptr;
14089 first->kept_section = l->sec;
14090 sec->output_section = bfd_abs_section_ptr;
14091 break;
14092 }
14093 }
14094 else
14095 /* Check this linkonce section against single member groups. */
14096 for (l = already_linked_list->entry; l != NULL; l = l->next)
14097 if (l->sec->flags & SEC_GROUP)
6d2cd210 14098 {
c77ec726 14099 asection *first = elf_next_in_group (l->sec);
6d2cd210 14100
c77ec726
AM
14101 if (first != NULL
14102 && elf_next_in_group (first) == first
14103 && bfd_elf_match_symbols_in_sections (first, sec, info))
14104 {
14105 sec->output_section = bfd_abs_section_ptr;
14106 sec->kept_section = first;
14107 break;
14108 }
6d2cd210 14109 }
0c511000 14110
c77ec726
AM
14111 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14112 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14113 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14114 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14115 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14116 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14117 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14118 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14119 The reverse order cannot happen as there is never a bfd with only the
14120 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14121 matter as here were are looking only for cross-bfd sections. */
14122
14123 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14124 for (l = already_linked_list->entry; l != NULL; l = l->next)
14125 if ((l->sec->flags & SEC_GROUP) == 0
14126 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14127 {
14128 if (abfd != l->sec->owner)
14129 sec->output_section = bfd_abs_section_ptr;
14130 break;
14131 }
80c29487 14132
082b7297 14133 /* This is the first section with this name. Record it. */
c77ec726 14134 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14135 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14136 return sec->output_section == bfd_abs_section_ptr;
082b7297 14137}
81e1b023 14138
a4d8e49b
L
14139bfd_boolean
14140_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14141{
14142 return sym->st_shndx == SHN_COMMON;
14143}
14144
14145unsigned int
14146_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14147{
14148 return SHN_COMMON;
14149}
14150
14151asection *
14152_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14153{
14154 return bfd_com_section_ptr;
14155}
10455f89
HPN
14156
14157bfd_vma
14158_bfd_elf_default_got_elt_size (bfd *abfd,
14159 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14160 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14161 bfd *ibfd ATTRIBUTE_UNUSED,
14162 unsigned long symndx ATTRIBUTE_UNUSED)
14163{
14164 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14165 return bed->s->arch_size / 8;
14166}
83bac4b0
NC
14167
14168/* Routines to support the creation of dynamic relocs. */
14169
83bac4b0
NC
14170/* Returns the name of the dynamic reloc section associated with SEC. */
14171
14172static const char *
14173get_dynamic_reloc_section_name (bfd * abfd,
14174 asection * sec,
14175 bfd_boolean is_rela)
14176{
ddcf1fcf
BS
14177 char *name;
14178 const char *old_name = bfd_get_section_name (NULL, sec);
14179 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14180
ddcf1fcf 14181 if (old_name == NULL)
83bac4b0
NC
14182 return NULL;
14183
ddcf1fcf 14184 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14185 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14186
14187 return name;
14188}
14189
14190/* Returns the dynamic reloc section associated with SEC.
14191 If necessary compute the name of the dynamic reloc section based
14192 on SEC's name (looked up in ABFD's string table) and the setting
14193 of IS_RELA. */
14194
14195asection *
14196_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14197 asection * sec,
14198 bfd_boolean is_rela)
14199{
14200 asection * reloc_sec = elf_section_data (sec)->sreloc;
14201
14202 if (reloc_sec == NULL)
14203 {
14204 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14205
14206 if (name != NULL)
14207 {
3d4d4302 14208 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14209
14210 if (reloc_sec != NULL)
14211 elf_section_data (sec)->sreloc = reloc_sec;
14212 }
14213 }
14214
14215 return reloc_sec;
14216}
14217
14218/* Returns the dynamic reloc section associated with SEC. If the
14219 section does not exist it is created and attached to the DYNOBJ
14220 bfd and stored in the SRELOC field of SEC's elf_section_data
14221 structure.
f8076f98 14222
83bac4b0
NC
14223 ALIGNMENT is the alignment for the newly created section and
14224 IS_RELA defines whether the name should be .rela.<SEC's name>
14225 or .rel.<SEC's name>. The section name is looked up in the
14226 string table associated with ABFD. */
14227
14228asection *
ca4be51c
AM
14229_bfd_elf_make_dynamic_reloc_section (asection *sec,
14230 bfd *dynobj,
14231 unsigned int alignment,
14232 bfd *abfd,
14233 bfd_boolean is_rela)
83bac4b0
NC
14234{
14235 asection * reloc_sec = elf_section_data (sec)->sreloc;
14236
14237 if (reloc_sec == NULL)
14238 {
14239 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14240
14241 if (name == NULL)
14242 return NULL;
14243
3d4d4302 14244 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14245
14246 if (reloc_sec == NULL)
14247 {
3d4d4302
AM
14248 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14249 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14250 if ((sec->flags & SEC_ALLOC) != 0)
14251 flags |= SEC_ALLOC | SEC_LOAD;
14252
3d4d4302 14253 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14254 if (reloc_sec != NULL)
14255 {
8877b5e5
AM
14256 /* _bfd_elf_get_sec_type_attr chooses a section type by
14257 name. Override as it may be wrong, eg. for a user
14258 section named "auto" we'll get ".relauto" which is
14259 seen to be a .rela section. */
14260 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14261 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14262 reloc_sec = NULL;
14263 }
14264 }
14265
14266 elf_section_data (sec)->sreloc = reloc_sec;
14267 }
14268
14269 return reloc_sec;
14270}
1338dd10 14271
bffebb6b
AM
14272/* Copy the ELF symbol type and other attributes for a linker script
14273 assignment from HSRC to HDEST. Generally this should be treated as
14274 if we found a strong non-dynamic definition for HDEST (except that
14275 ld ignores multiple definition errors). */
1338dd10 14276void
bffebb6b
AM
14277_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14278 struct bfd_link_hash_entry *hdest,
14279 struct bfd_link_hash_entry *hsrc)
1338dd10 14280{
bffebb6b
AM
14281 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14282 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14283 Elf_Internal_Sym isym;
1338dd10
PB
14284
14285 ehdest->type = ehsrc->type;
35fc36a8 14286 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14287
14288 isym.st_other = ehsrc->other;
b8417128 14289 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14290}
351f65ca
L
14291
14292/* Append a RELA relocation REL to section S in BFD. */
14293
14294void
14295elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14296{
14297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14298 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14299 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14300 bed->s->swap_reloca_out (abfd, rel, loc);
14301}
14302
14303/* Append a REL relocation REL to section S in BFD. */
14304
14305void
14306elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14307{
14308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14309 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14310 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14311 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14312}
7dba9362
AM
14313
14314/* Define __start, __stop, .startof. or .sizeof. symbol. */
14315
14316struct bfd_link_hash_entry *
14317bfd_elf_define_start_stop (struct bfd_link_info *info,
14318 const char *symbol, asection *sec)
14319{
487b6440 14320 struct elf_link_hash_entry *h;
7dba9362 14321
487b6440
AM
14322 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14323 FALSE, FALSE, TRUE);
14324 if (h != NULL
14325 && (h->root.type == bfd_link_hash_undefined
14326 || h->root.type == bfd_link_hash_undefweak
14327 || (h->ref_regular && !h->def_regular)))
7dba9362 14328 {
487b6440
AM
14329 h->root.type = bfd_link_hash_defined;
14330 h->root.u.def.section = sec;
14331 h->root.u.def.value = 0;
14332 h->def_regular = 1;
14333 h->def_dynamic = 0;
14334 h->start_stop = 1;
14335 h->u2.start_stop_section = sec;
14336 if (symbol[0] == '.')
14337 {
14338 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14339 const struct elf_backend_data *bed;
14340 bed = get_elf_backend_data (info->output_bfd);
14341 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440
AM
14342 }
14343 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14344 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14345 return &h->root;
7dba9362 14346 }
487b6440 14347 return NULL;
7dba9362 14348}
This page took 2.398793 seconds and 4 git commands to generate.