]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Use %pA and %pB in messages rather than %A and %B
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
219d1afa 2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
07d6d2b8 80 return h->root.u.def.section;
2f0c68f2
CM
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
57963c05 221 asection *s;
6cd255ca 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 223 if ((ibfd->flags
57963c05
AM
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
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
d00dd7dc
AM
884_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
886 asection *p)
aee6f5b4 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
d00dd7dc
AM
916bfd_boolean
917_bfd_elf_omit_section_dynsym_all
918 (bfd *output_bfd ATTRIBUTE_UNUSED,
919 struct bfd_link_info *info ATTRIBUTE_UNUSED,
920 asection *p ATTRIBUTE_UNUSED)
921{
922 return TRUE;
923}
924
062e2358 925/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
926 symbol for each output section, which come first. Next come symbols
927 which have been forced to local binding. Then all of the back-end
928 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
929 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
930 (This prevents the early call before elf_backend_init_index_section
931 and strip_excluded_output_sections setting dynindx for sections
932 that are stripped.) */
30b30c21 933
554220db
AM
934static unsigned long
935_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
936 struct bfd_link_info *info,
937 unsigned long *section_sym_count)
30b30c21
RH
938{
939 unsigned long dynsymcount = 0;
63f452a8 940 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 941
0e1862bb
L
942 if (bfd_link_pic (info)
943 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 944 {
aee6f5b4 945 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
946 asection *p;
947 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 948 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
949 && (p->flags & SEC_ALLOC) != 0
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
74541ad4 957 elf_section_data (p)->dynindx = 0;
30b30c21 958 }
63f452a8
AM
959 if (do_sec)
960 *section_sym_count = dynsymcount;
30b30c21 961
6fa3860b
PB
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
30b30c21
RH
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
90ac2420 972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
d5486c43
L
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
30b30c21 983
ccabcbe5
AM
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
30b30c21 986}
252b5132 987
54ac0771
L
988/* Merge st_other field. */
989
990static void
991elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 992 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 993 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
994{
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
cd3416da 998 code might be needed here. */
54ac0771
L
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
cd3416da 1003 if (!dynamic)
54ac0771 1004 {
cd3416da
AM
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1007
cd3416da
AM
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1012 }
b8417128
AM
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1016 h->protected_def = 1;
54ac0771
L
1017}
1018
4f3fedcf
AM
1019/* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
45d6a902 1031
8a56bd02 1032static bfd_boolean
268b6b39
AM
1033_bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
4f3fedcf
AM
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
37a9e49a 1041 bfd_boolean *pold_weak,
af44c138 1042 unsigned int *pold_alignment,
268b6b39
AM
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
6e33951e
L
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
252b5132 1048{
7479dfd4 1049 asection *sec, *oldsec;
45d6a902 1050 struct elf_link_hash_entry *h;
90c984fc 1051 struct elf_link_hash_entry *hi;
45d6a902
AM
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1057 const struct elf_backend_data *bed;
6e33951e 1058 char *new_version;
93f4de39 1059 bfd_boolean default_sym = *matched;
45d6a902
AM
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
252b5132 1075
88ba32a0
L
1076 bed = get_elf_backend_data (abfd);
1077
6e33951e 1078 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1079 if (h->versioned != unversioned)
6e33951e 1080 {
422f1182
L
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
6e33951e 1098 }
422f1182
L
1099 else
1100 new_version = NULL;
6e33951e 1101
90c984fc
L
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
45d6a902
AM
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
6e33951e
L
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
ae7683d2 1115 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1116 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1117 true if the new symbol is only visible to the symbol with
6e33951e 1118 the same symbol version. */
422f1182
L
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
422f1182
L
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
6e33951e
L
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
934bce08
AM
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
40b36307 1185 /* We have to check it for every instance since the first few may be
ee659f1f 1186 references and not all compilers emit symbol type for undefined
40b36307
L
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
ee659f1f
AM
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
6e33951e
L
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
ee659f1f
AM
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
45d6a902
AM
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
252b5132 1226 {
f5385ebf 1227 h->non_elf = 0;
45d6a902
AM
1228 return TRUE;
1229 }
252b5132 1230
45d6a902
AM
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
895fa45f 1238 && (newweak || oldweak)
45d6a902 1239 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1240 || !h->def_regular))
45d6a902
AM
1241 return TRUE;
1242
707bba77 1243 olddyn = FALSE;
45d6a902
AM
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1246 else if (oldsec != NULL)
45d6a902 1247 {
707bba77 1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1249 indices used by MIPS ELF. */
707bba77 1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1251 }
252b5132 1252
1a3b5c34
AM
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
45d6a902
AM
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
707bba77 1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1267
707bba77
AM
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
202ac193 1270 && h->root.type != bfd_link_hash_common);
45d6a902 1271
0a36a439
L
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
c5d37467 1281 if (!(newfunc && oldfunc)
5b677558
AM
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
c5d37467
AM
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1287 {
c5d37467
AM
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
580a2b6e
L
1325 }
1326
4c34aff8
AM
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
3b36f7e6 1342 ntbfd = abfd;
7479dfd4
L
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
4eca0228 1360 _bfd_error_handler
695344c0 1361 /* xgettext:c-format */
871b3ab2
AM
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1365 else if (!tdef && !ntdef)
4eca0228 1366 _bfd_error_handler
695344c0 1367 /* xgettext:c-format */
871b3ab2
AM
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
c08bb8dd 1370 h->root.root.string, tbfd, ntbfd);
7479dfd4 1371 else if (tdef)
4eca0228 1372 _bfd_error_handler
695344c0 1373 /* xgettext:c-format */
871b3ab2
AM
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
c08bb8dd 1376 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1377 else
4eca0228 1378 _bfd_error_handler
695344c0 1379 /* xgettext:c-format */
871b3ab2
AM
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1382 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
45d6a902
AM
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
9c7a29a3 1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
f5385ebf 1396 h->ref_dynamic = 1;
90c984fc 1397 hi->ref_dynamic = 1;
45d6a902
AM
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1403 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
9c7a29a3 1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1409 && h->def_dynamic)
45d6a902
AM
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
6c9b78e6 1414 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
6c9b78e6 1422 hi->root.type = h->root.type;
d2dee3b2 1423 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1425
6c9b78e6 1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1428 {
aed81c4e
MR
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
d2dee3b2
L
1434 }
1435 else
aed81c4e
MR
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
aed81c4e
MR
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
6c9b78e6 1443 h = hi;
d2dee3b2
L
1444 }
1445 else
6c9b78e6 1446 h = hi;
d2dee3b2 1447 }
1de1a317 1448
f5eda473
AM
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1457 {
1de1a317 1458 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
f5eda473 1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1468 {
f5eda473
AM
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
45d6a902 1474 }
f5eda473
AM
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
45d6a902
AM
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
14a793b2 1483
15b43f48
AM
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
165f707a
AM
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
15b43f48
AM
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
165f707a 1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1501 newweak = FALSE;
15b43f48 1502 if (olddef && newdyn)
0f8a2703
AM
1503 oldweak = FALSE;
1504
d334575b 1505 /* Allow changes between different types of function symbol. */
0a36a439 1506 if (newfunc && oldfunc)
fcb93ecf
PB
1507 *type_change_ok = TRUE;
1508
79349b09
AM
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
252b5132 1512
79349b09
AM
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
45d6a902 1525
45d6a902
AM
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
79349b09 1551 && !newweak
45d6a902
AM
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
0a36a439 1555 && !newfunc)
45d6a902
AM
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
f5385ebf 1563 && h->def_dynamic
45d6a902
AM
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
0a36a439 1567 && !oldfunc)
45d6a902
AM
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
a4d8e49b
L
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
5d13b3b3
AM
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
a4d8e49b 1580
a83ef4d1
L
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
93f4de39 1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
45d6a902
AM
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1a72702b
AM
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
252b5132 1614
45d6a902 1615 *size_change_ok = TRUE;
252b5132
RH
1616 }
1617
45d6a902
AM
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
8170f769 1630 object to override a weak symbol in a shared object. */
45d6a902
AM
1631
1632 if (newdyn
1633 && newdef
77cfaee6 1634 && (olddef
45d6a902 1635 || (h->root.type == bfd_link_hash_common
8170f769 1636 && (newweak || newfunc))))
45d6a902
AM
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
252b5132 1641
45d6a902
AM
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
252b5132 1644
45d6a902
AM
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
252b5132 1650
45d6a902
AM
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
45d6a902
AM
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
a4d8e49b 1668 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1669 *size_change_ok = TRUE;
1670 }
1671
c5e2cead 1672 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1673 if (newdef && olddef && newweak)
54ac0771 1674 {
35ed3f94 1675 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1678 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
54ac0771
L
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
b8417128 1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
c5e2cead 1697
45d6a902
AM
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
0f8a2703 1706 symbol is a function or is weak. */
45d6a902
AM
1707
1708 flip = NULL;
77cfaee6 1709 if (!newdyn
45d6a902
AM
1710 && (newdef
1711 || (bfd_is_com_section (sec)
0a36a439 1712 && (oldweak || oldfunc)))
45d6a902
AM
1713 && olddyn
1714 && olddef
f5385ebf 1715 && h->def_dynamic)
45d6a902
AM
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
0a36a439
L
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
45d6a902 1743
6c9b78e6
AM
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
45d6a902
AM
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1a72702b
AM
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
45d6a902 1768
4cc11e76 1769 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
af44c138
L
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
6c9b78e6
AM
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
45d6a902
AM
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
45d6a902 1800 flip->root.type = h->root.type;
00cbee0a 1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1805 if (h->def_dynamic)
45d6a902 1806 {
f5385ebf
AM
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
45d6a902
AM
1809 }
1810 }
1811
45d6a902
AM
1812 return TRUE;
1813}
1814
1815/* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
4f3fedcf 1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1818 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1819
28caa186 1820static bfd_boolean
268b6b39
AM
1821_bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
4f3fedcf
AM
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
e3c9d234 1829 bfd_boolean *dynsym)
45d6a902
AM
1830{
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
9c5bfbb7 1837 const struct elf_backend_data *bed;
45d6a902
AM
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
e3c9d234 1840 bfd_boolean override;
45d6a902
AM
1841 char *p;
1842 size_t len, shortlen;
ffd65175 1843 asection *tmp_sec;
6e33951e 1844 bfd_boolean matched;
45d6a902 1845
422f1182
L
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
45d6a902
AM
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
422f1182
L
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
4373f8af
L
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
45d6a902 1879
45d6a902
AM
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
a50b1753 1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
6e33951e 1897 matched = TRUE;
ffd65175
AM
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1900 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1901 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
5b677558
AM
1907 if (hi->def_regular)
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
45d6a902
AM
1932 if (! override)
1933 {
c6e8a9a8 1934 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1935 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1936 {
1937 bh = &hi->root;
1938 if (! (_bfd_generic_link_add_one_symbol
1939 (info, abfd, shortname, BSF_INDIRECT,
1940 bfd_ind_section_ptr,
1941 0, name, FALSE, collect, &bh)))
1942 return FALSE;
1943 hi = (struct elf_link_hash_entry *) bh;
1944 }
45d6a902
AM
1945 }
1946 else
1947 {
1948 /* In this case the symbol named SHORTNAME is overriding the
1949 indirect symbol we want to add. We were planning on making
1950 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1951 is the name without a version. NAME is the fully versioned
1952 name, and it is the default version.
1953
1954 Overriding means that we already saw a definition for the
1955 symbol SHORTNAME in a regular object, and it is overriding
1956 the symbol defined in the dynamic object.
1957
1958 When this happens, we actually want to change NAME, the
1959 symbol we just added, to refer to SHORTNAME. This will cause
1960 references to NAME in the shared object to become references
1961 to SHORTNAME in the regular object. This is what we expect
1962 when we override a function in a shared object: that the
1963 references in the shared object will be mapped to the
1964 definition in the regular object. */
1965
1966 while (hi->root.type == bfd_link_hash_indirect
1967 || hi->root.type == bfd_link_hash_warning)
1968 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1969
1970 h->root.type = bfd_link_hash_indirect;
1971 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1972 if (h->def_dynamic)
45d6a902 1973 {
f5385ebf
AM
1974 h->def_dynamic = 0;
1975 hi->ref_dynamic = 1;
1976 if (hi->ref_regular
1977 || hi->def_regular)
45d6a902 1978 {
c152c796 1979 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1980 return FALSE;
1981 }
1982 }
1983
1984 /* Now set HI to H, so that the following code will set the
1985 other fields correctly. */
1986 hi = h;
1987 }
1988
fab4a87f
L
1989 /* Check if HI is a warning symbol. */
1990 if (hi->root.type == bfd_link_hash_warning)
1991 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1992
45d6a902
AM
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error to
1995 the user in that case. */
1996
1997 if (hi->root.type == bfd_link_hash_indirect)
1998 {
1999 struct elf_link_hash_entry *ht;
2000
45d6a902 2001 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2002 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2003
68c88cd4
AM
2004 /* A reference to the SHORTNAME symbol from a dynamic library
2005 will be satisfied by the versioned symbol at runtime. In
2006 effect, we have a reference to the versioned symbol. */
2007 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2008 hi->dynamic_def |= ht->dynamic_def;
2009
45d6a902
AM
2010 /* See if the new flags lead us to realize that the symbol must
2011 be dynamic. */
2012 if (! *dynsym)
2013 {
2014 if (! dynamic)
2015 {
0e1862bb 2016 if (! bfd_link_executable (info)
90c984fc 2017 || hi->def_dynamic
f5385ebf 2018 || hi->ref_dynamic)
45d6a902
AM
2019 *dynsym = TRUE;
2020 }
2021 else
2022 {
f5385ebf 2023 if (hi->ref_regular)
45d6a902
AM
2024 *dynsym = TRUE;
2025 }
2026 }
2027 }
2028
2029 /* We also need to define an indirection from the nondefault version
2030 of the symbol. */
2031
2032nondefault:
2033 len = strlen (name);
a50b1753 2034 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2035 if (shortname == NULL)
2036 return FALSE;
2037 memcpy (shortname, name, shortlen);
2038 memcpy (shortname + shortlen, p + 1, len - shortlen);
2039
2040 /* Once again, merge with any existing symbol. */
2041 type_change_ok = FALSE;
2042 size_change_ok = FALSE;
ffd65175
AM
2043 tmp_sec = sec;
2044 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2045 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2046 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2047 return FALSE;
2048
2049 if (skip)
2050 return TRUE;
2051
2052 if (override)
2053 {
2054 /* Here SHORTNAME is a versioned name, so we don't expect to see
2055 the type of override we do in the case above unless it is
4cc11e76 2056 overridden by a versioned definition. */
45d6a902
AM
2057 if (hi->root.type != bfd_link_hash_defined
2058 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2059 _bfd_error_handler
695344c0 2060 /* xgettext:c-format */
871b3ab2 2061 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2062 abfd, shortname);
45d6a902
AM
2063 }
2064 else
2065 {
2066 bh = &hi->root;
2067 if (! (_bfd_generic_link_add_one_symbol
2068 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2069 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2070 return FALSE;
2071 hi = (struct elf_link_hash_entry *) bh;
2072
2073 /* If there is a duplicate definition somewhere, then HI may not
2074 point to an indirect symbol. We will have reported an error
2075 to the user in that case. */
2076
2077 if (hi->root.type == bfd_link_hash_indirect)
2078 {
fcfa13d2 2079 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2080 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2081 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2082
2083 /* See if the new flags lead us to realize that the symbol
2084 must be dynamic. */
2085 if (! *dynsym)
2086 {
2087 if (! dynamic)
2088 {
0e1862bb 2089 if (! bfd_link_executable (info)
f5385ebf 2090 || hi->ref_dynamic)
45d6a902
AM
2091 *dynsym = TRUE;
2092 }
2093 else
2094 {
f5385ebf 2095 if (hi->ref_regular)
45d6a902
AM
2096 *dynsym = TRUE;
2097 }
2098 }
2099 }
2100 }
2101
2102 return TRUE;
2103}
2104\f
2105/* This routine is used to export all defined symbols into the dynamic
2106 symbol table. It is called via elf_link_hash_traverse. */
2107
28caa186 2108static bfd_boolean
268b6b39 2109_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2110{
a50b1753 2111 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2112
2113 /* Ignore indirect symbols. These are added by the versioning code. */
2114 if (h->root.type == bfd_link_hash_indirect)
2115 return TRUE;
2116
7686d77d
AM
2117 /* Ignore this if we won't export it. */
2118 if (!eif->info->export_dynamic && !h->dynamic)
2119 return TRUE;
45d6a902
AM
2120
2121 if (h->dynindx == -1
fd91d419
L
2122 && (h->def_regular || h->ref_regular)
2123 && ! bfd_hide_sym_by_version (eif->info->version_info,
2124 h->root.root.string))
45d6a902 2125 {
fd91d419 2126 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2127 {
fd91d419
L
2128 eif->failed = TRUE;
2129 return FALSE;
45d6a902
AM
2130 }
2131 }
2132
2133 return TRUE;
2134}
2135\f
2136/* Look through the symbols which are defined in other shared
2137 libraries and referenced here. Update the list of version
2138 dependencies. This will be put into the .gnu.version_r section.
2139 This function is called via elf_link_hash_traverse. */
2140
28caa186 2141static bfd_boolean
268b6b39
AM
2142_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2143 void *data)
45d6a902 2144{
a50b1753 2145 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2146 Elf_Internal_Verneed *t;
2147 Elf_Internal_Vernaux *a;
2148 bfd_size_type amt;
2149
45d6a902
AM
2150 /* We only care about symbols defined in shared objects with version
2151 information. */
f5385ebf
AM
2152 if (!h->def_dynamic
2153 || h->def_regular
45d6a902 2154 || h->dynindx == -1
7b20f099
AM
2155 || h->verinfo.verdef == NULL
2156 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2157 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2158 return TRUE;
2159
2160 /* See if we already know about this version. */
28caa186
AM
2161 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2162 t != NULL;
2163 t = t->vn_nextref)
45d6a902
AM
2164 {
2165 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2166 continue;
2167
2168 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2169 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2170 return TRUE;
2171
2172 break;
2173 }
2174
2175 /* This is a new version. Add it to tree we are building. */
2176
2177 if (t == NULL)
2178 {
2179 amt = sizeof *t;
a50b1753 2180 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2181 if (t == NULL)
2182 {
2183 rinfo->failed = TRUE;
2184 return FALSE;
2185 }
2186
2187 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2188 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2189 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2190 }
2191
2192 amt = sizeof *a;
a50b1753 2193 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2194 if (a == NULL)
2195 {
2196 rinfo->failed = TRUE;
2197 return FALSE;
2198 }
45d6a902
AM
2199
2200 /* Note that we are copying a string pointer here, and testing it
2201 above. If bfd_elf_string_from_elf_section is ever changed to
2202 discard the string data when low in memory, this will have to be
2203 fixed. */
2204 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2205
2206 a->vna_flags = h->verinfo.verdef->vd_flags;
2207 a->vna_nextptr = t->vn_auxptr;
2208
2209 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2210 ++rinfo->vers;
2211
2212 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2213
2214 t->vn_auxptr = a;
2215
2216 return TRUE;
2217}
2218
2219/* Figure out appropriate versions for all the symbols. We may not
2220 have the version number script until we have read all of the input
2221 files, so until that point we don't know which symbols should be
2222 local. This function is called via elf_link_hash_traverse. */
2223
28caa186 2224static bfd_boolean
268b6b39 2225_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2226{
28caa186 2227 struct elf_info_failed *sinfo;
45d6a902 2228 struct bfd_link_info *info;
9c5bfbb7 2229 const struct elf_backend_data *bed;
45d6a902
AM
2230 struct elf_info_failed eif;
2231 char *p;
45d6a902 2232
a50b1753 2233 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2234 info = sinfo->info;
2235
45d6a902
AM
2236 /* Fix the symbol flags. */
2237 eif.failed = FALSE;
2238 eif.info = info;
2239 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2240 {
2241 if (eif.failed)
2242 sinfo->failed = TRUE;
2243 return FALSE;
2244 }
2245
2246 /* We only need version numbers for symbols defined in regular
2247 objects. */
f5385ebf 2248 if (!h->def_regular)
45d6a902
AM
2249 return TRUE;
2250
28caa186 2251 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2252 p = strchr (h->root.root.string, ELF_VER_CHR);
2253 if (p != NULL && h->verinfo.vertree == NULL)
2254 {
2255 struct bfd_elf_version_tree *t;
45d6a902 2256
45d6a902
AM
2257 ++p;
2258 if (*p == ELF_VER_CHR)
6e33951e 2259 ++p;
45d6a902
AM
2260
2261 /* If there is no version string, we can just return out. */
2262 if (*p == '\0')
6e33951e 2263 return TRUE;
45d6a902
AM
2264
2265 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2266 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2267 {
2268 if (strcmp (t->name, p) == 0)
2269 {
2270 size_t len;
2271 char *alc;
2272 struct bfd_elf_version_expr *d;
2273
2274 len = p - h->root.root.string;
a50b1753 2275 alc = (char *) bfd_malloc (len);
45d6a902 2276 if (alc == NULL)
14b1c01e
AM
2277 {
2278 sinfo->failed = TRUE;
2279 return FALSE;
2280 }
45d6a902
AM
2281 memcpy (alc, h->root.root.string, len - 1);
2282 alc[len - 1] = '\0';
2283 if (alc[len - 2] == ELF_VER_CHR)
2284 alc[len - 2] = '\0';
2285
2286 h->verinfo.vertree = t;
2287 t->used = TRUE;
2288 d = NULL;
2289
108ba305
JJ
2290 if (t->globals.list != NULL)
2291 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2292
2293 /* See if there is anything to force this symbol to
2294 local scope. */
108ba305 2295 if (d == NULL && t->locals.list != NULL)
45d6a902 2296 {
108ba305
JJ
2297 d = (*t->match) (&t->locals, NULL, alc);
2298 if (d != NULL
2299 && h->dynindx != -1
108ba305
JJ
2300 && ! info->export_dynamic)
2301 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2302 }
2303
2304 free (alc);
2305 break;
2306 }
2307 }
2308
2309 /* If we are building an application, we need to create a
2310 version node for this version. */
0e1862bb 2311 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2312 {
2313 struct bfd_elf_version_tree **pp;
2314 int version_index;
2315
2316 /* If we aren't going to export this symbol, we don't need
2317 to worry about it. */
2318 if (h->dynindx == -1)
2319 return TRUE;
2320
ef53be89
AM
2321 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2322 sizeof *t);
45d6a902
AM
2323 if (t == NULL)
2324 {
2325 sinfo->failed = TRUE;
2326 return FALSE;
2327 }
2328
45d6a902 2329 t->name = p;
45d6a902
AM
2330 t->name_indx = (unsigned int) -1;
2331 t->used = TRUE;
2332
2333 version_index = 1;
2334 /* Don't count anonymous version tag. */
fd91d419
L
2335 if (sinfo->info->version_info != NULL
2336 && sinfo->info->version_info->vernum == 0)
45d6a902 2337 version_index = 0;
fd91d419
L
2338 for (pp = &sinfo->info->version_info;
2339 *pp != NULL;
2340 pp = &(*pp)->next)
45d6a902
AM
2341 ++version_index;
2342 t->vernum = version_index;
2343
2344 *pp = t;
2345
2346 h->verinfo.vertree = t;
2347 }
2348 else if (t == NULL)
2349 {
2350 /* We could not find the version for a symbol when
2351 generating a shared archive. Return an error. */
4eca0228 2352 _bfd_error_handler
695344c0 2353 /* xgettext:c-format */
871b3ab2 2354 (_("%pB: version node not found for symbol %s"),
28caa186 2355 info->output_bfd, h->root.root.string);
45d6a902
AM
2356 bfd_set_error (bfd_error_bad_value);
2357 sinfo->failed = TRUE;
2358 return FALSE;
2359 }
45d6a902
AM
2360 }
2361
2362 /* If we don't have a version for this symbol, see if we can find
2363 something. */
fd91d419 2364 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2365 {
1e8fa21e 2366 bfd_boolean hide;
ae5a3597 2367
fd91d419
L
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (sinfo->info->version_info,
2370 h->root.root.string, &hide);
1e8fa21e
AM
2371 if (h->verinfo.vertree != NULL && hide)
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2373 }
2374
2375 return TRUE;
2376}
2377\f
45d6a902
AM
2378/* Read and swap the relocs from the section indicated by SHDR. This
2379 may be either a REL or a RELA section. The relocations are
2380 translated into RELA relocations and stored in INTERNAL_RELOCS,
2381 which should have already been allocated to contain enough space.
2382 The EXTERNAL_RELOCS are a buffer where the external form of the
2383 relocations should be stored.
2384
2385 Returns FALSE if something goes wrong. */
2386
2387static bfd_boolean
268b6b39 2388elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2389 asection *sec,
268b6b39
AM
2390 Elf_Internal_Shdr *shdr,
2391 void *external_relocs,
2392 Elf_Internal_Rela *internal_relocs)
45d6a902 2393{
9c5bfbb7 2394 const struct elf_backend_data *bed;
268b6b39 2395 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2396 const bfd_byte *erela;
2397 const bfd_byte *erelaend;
2398 Elf_Internal_Rela *irela;
243ef1e0
L
2399 Elf_Internal_Shdr *symtab_hdr;
2400 size_t nsyms;
45d6a902 2401
45d6a902
AM
2402 /* Position ourselves at the start of the section. */
2403 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2404 return FALSE;
2405
2406 /* Read the relocations. */
2407 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2408 return FALSE;
2409
243ef1e0 2410 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2411 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2412
45d6a902
AM
2413 bed = get_elf_backend_data (abfd);
2414
2415 /* Convert the external relocations to the internal format. */
2416 if (shdr->sh_entsize == bed->s->sizeof_rel)
2417 swap_in = bed->s->swap_reloc_in;
2418 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2419 swap_in = bed->s->swap_reloca_in;
2420 else
2421 {
2422 bfd_set_error (bfd_error_wrong_format);
2423 return FALSE;
2424 }
2425
a50b1753 2426 erela = (const bfd_byte *) external_relocs;
51992aec 2427 erelaend = erela + shdr->sh_size;
45d6a902
AM
2428 irela = internal_relocs;
2429 while (erela < erelaend)
2430 {
243ef1e0
L
2431 bfd_vma r_symndx;
2432
45d6a902 2433 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2434 r_symndx = ELF32_R_SYM (irela->r_info);
2435 if (bed->s->arch_size == 64)
2436 r_symndx >>= 24;
ce98a316
NC
2437 if (nsyms > 0)
2438 {
2439 if ((size_t) r_symndx >= nsyms)
2440 {
4eca0228 2441 _bfd_error_handler
695344c0 2442 /* xgettext:c-format */
871b3ab2
AM
2443 (_("%pB: bad reloc symbol index (%#Lx >= %#lx)"
2444 " for offset %#Lx in section `%pA'"),
d42c267e 2445 abfd, r_symndx, (unsigned long) nsyms,
c08bb8dd 2446 irela->r_offset, sec);
ce98a316
NC
2447 bfd_set_error (bfd_error_bad_value);
2448 return FALSE;
2449 }
2450 }
cf35638d 2451 else if (r_symndx != STN_UNDEF)
243ef1e0 2452 {
4eca0228 2453 _bfd_error_handler
695344c0 2454 /* xgettext:c-format */
871b3ab2
AM
2455 (_("%pB: non-zero symbol index (%#Lx)"
2456 " for offset %#Lx in section `%pA'"
ce98a316 2457 " when the object file has no symbol table"),
d42c267e 2458 abfd, r_symndx,
c08bb8dd 2459 irela->r_offset, sec);
243ef1e0
L
2460 bfd_set_error (bfd_error_bad_value);
2461 return FALSE;
2462 }
45d6a902
AM
2463 irela += bed->s->int_rels_per_ext_rel;
2464 erela += shdr->sh_entsize;
2465 }
2466
2467 return TRUE;
2468}
2469
2470/* Read and swap the relocs for a section O. They may have been
2471 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2472 not NULL, they are used as buffers to read into. They are known to
2473 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2474 the return value is allocated using either malloc or bfd_alloc,
2475 according to the KEEP_MEMORY argument. If O has two relocation
2476 sections (both REL and RELA relocations), then the REL_HDR
2477 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2478 RELA_HDR relocations. */
45d6a902
AM
2479
2480Elf_Internal_Rela *
268b6b39
AM
2481_bfd_elf_link_read_relocs (bfd *abfd,
2482 asection *o,
2483 void *external_relocs,
2484 Elf_Internal_Rela *internal_relocs,
2485 bfd_boolean keep_memory)
45d6a902 2486{
268b6b39 2487 void *alloc1 = NULL;
45d6a902 2488 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2490 struct bfd_elf_section_data *esdo = elf_section_data (o);
2491 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2492
d4730f92
BS
2493 if (esdo->relocs != NULL)
2494 return esdo->relocs;
45d6a902
AM
2495
2496 if (o->reloc_count == 0)
2497 return NULL;
2498
45d6a902
AM
2499 if (internal_relocs == NULL)
2500 {
2501 bfd_size_type size;
2502
056bafd4 2503 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2504 if (keep_memory)
a50b1753 2505 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2506 else
a50b1753 2507 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2508 if (internal_relocs == NULL)
2509 goto error_return;
2510 }
2511
2512 if (external_relocs == NULL)
2513 {
d4730f92
BS
2514 bfd_size_type size = 0;
2515
2516 if (esdo->rel.hdr)
2517 size += esdo->rel.hdr->sh_size;
2518 if (esdo->rela.hdr)
2519 size += esdo->rela.hdr->sh_size;
45d6a902 2520
268b6b39 2521 alloc1 = bfd_malloc (size);
45d6a902
AM
2522 if (alloc1 == NULL)
2523 goto error_return;
2524 external_relocs = alloc1;
2525 }
2526
d4730f92
BS
2527 internal_rela_relocs = internal_relocs;
2528 if (esdo->rel.hdr)
2529 {
2530 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2531 external_relocs,
2532 internal_relocs))
2533 goto error_return;
2534 external_relocs = (((bfd_byte *) external_relocs)
2535 + esdo->rel.hdr->sh_size);
2536 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2537 * bed->s->int_rels_per_ext_rel);
2538 }
2539
2540 if (esdo->rela.hdr
2541 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2542 external_relocs,
2543 internal_rela_relocs)))
45d6a902
AM
2544 goto error_return;
2545
2546 /* Cache the results for next time, if we can. */
2547 if (keep_memory)
d4730f92 2548 esdo->relocs = internal_relocs;
45d6a902
AM
2549
2550 if (alloc1 != NULL)
2551 free (alloc1);
2552
2553 /* Don't free alloc2, since if it was allocated we are passing it
2554 back (under the name of internal_relocs). */
2555
2556 return internal_relocs;
2557
2558 error_return:
2559 if (alloc1 != NULL)
2560 free (alloc1);
2561 if (alloc2 != NULL)
4dd07732
AM
2562 {
2563 if (keep_memory)
2564 bfd_release (abfd, alloc2);
2565 else
2566 free (alloc2);
2567 }
45d6a902
AM
2568 return NULL;
2569}
2570
2571/* Compute the size of, and allocate space for, REL_HDR which is the
2572 section header for a section containing relocations for O. */
2573
28caa186 2574static bfd_boolean
9eaff861
AO
2575_bfd_elf_link_size_reloc_section (bfd *abfd,
2576 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2577{
9eaff861 2578 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2579
2580 /* That allows us to calculate the size of the section. */
9eaff861 2581 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2582
2583 /* The contents field must last into write_object_contents, so we
2584 allocate it with bfd_alloc rather than malloc. Also since we
2585 cannot be sure that the contents will actually be filled in,
2586 we zero the allocated space. */
a50b1753 2587 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2588 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2589 return FALSE;
2590
d4730f92 2591 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2592 {
2593 struct elf_link_hash_entry **p;
2594
ca4be51c
AM
2595 p = ((struct elf_link_hash_entry **)
2596 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2597 if (p == NULL)
2598 return FALSE;
2599
d4730f92 2600 reldata->hashes = p;
45d6a902
AM
2601 }
2602
2603 return TRUE;
2604}
2605
2606/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2607 originated from the section given by INPUT_REL_HDR) to the
2608 OUTPUT_BFD. */
2609
2610bfd_boolean
268b6b39
AM
2611_bfd_elf_link_output_relocs (bfd *output_bfd,
2612 asection *input_section,
2613 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2614 Elf_Internal_Rela *internal_relocs,
2615 struct elf_link_hash_entry **rel_hash
2616 ATTRIBUTE_UNUSED)
45d6a902
AM
2617{
2618 Elf_Internal_Rela *irela;
2619 Elf_Internal_Rela *irelaend;
2620 bfd_byte *erel;
d4730f92 2621 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2622 asection *output_section;
9c5bfbb7 2623 const struct elf_backend_data *bed;
268b6b39 2624 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2625 struct bfd_elf_section_data *esdo;
45d6a902
AM
2626
2627 output_section = input_section->output_section;
45d6a902 2628
d4730f92
BS
2629 bed = get_elf_backend_data (output_bfd);
2630 esdo = elf_section_data (output_section);
2631 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2632 {
d4730f92
BS
2633 output_reldata = &esdo->rel;
2634 swap_out = bed->s->swap_reloc_out;
45d6a902 2635 }
d4730f92
BS
2636 else if (esdo->rela.hdr
2637 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2638 {
d4730f92
BS
2639 output_reldata = &esdo->rela;
2640 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2641 }
2642 else
2643 {
4eca0228 2644 _bfd_error_handler
695344c0 2645 /* xgettext:c-format */
871b3ab2 2646 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2647 output_bfd, input_section->owner, input_section);
297d8443 2648 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2649 return FALSE;
2650 }
2651
d4730f92
BS
2652 erel = output_reldata->hdr->contents;
2653 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2654 irela = internal_relocs;
2655 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2656 * bed->s->int_rels_per_ext_rel);
2657 while (irela < irelaend)
2658 {
2659 (*swap_out) (output_bfd, irela, erel);
2660 irela += bed->s->int_rels_per_ext_rel;
2661 erel += input_rel_hdr->sh_entsize;
2662 }
2663
2664 /* Bump the counter, so that we know where to add the next set of
2665 relocations. */
d4730f92 2666 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2667
2668 return TRUE;
2669}
2670\f
508c3946
L
2671/* Make weak undefined symbols in PIE dynamic. */
2672
2673bfd_boolean
2674_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2675 struct elf_link_hash_entry *h)
2676{
0e1862bb 2677 if (bfd_link_pie (info)
508c3946
L
2678 && h->dynindx == -1
2679 && h->root.type == bfd_link_hash_undefweak)
2680 return bfd_elf_link_record_dynamic_symbol (info, h);
2681
2682 return TRUE;
2683}
2684
45d6a902
AM
2685/* Fix up the flags for a symbol. This handles various cases which
2686 can only be fixed after all the input files are seen. This is
2687 currently called by both adjust_dynamic_symbol and
2688 assign_sym_version, which is unnecessary but perhaps more robust in
2689 the face of future changes. */
2690
28caa186 2691static bfd_boolean
268b6b39
AM
2692_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2693 struct elf_info_failed *eif)
45d6a902 2694{
33774f08 2695 const struct elf_backend_data *bed;
508c3946 2696
45d6a902
AM
2697 /* If this symbol was mentioned in a non-ELF file, try to set
2698 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2699 permit a non-ELF file to correctly refer to a symbol defined in
2700 an ELF dynamic object. */
f5385ebf 2701 if (h->non_elf)
45d6a902
AM
2702 {
2703 while (h->root.type == bfd_link_hash_indirect)
2704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2705
2706 if (h->root.type != bfd_link_hash_defined
2707 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2708 {
2709 h->ref_regular = 1;
2710 h->ref_regular_nonweak = 1;
2711 }
45d6a902
AM
2712 else
2713 {
2714 if (h->root.u.def.section->owner != NULL
2715 && (bfd_get_flavour (h->root.u.def.section->owner)
2716 == bfd_target_elf_flavour))
f5385ebf
AM
2717 {
2718 h->ref_regular = 1;
2719 h->ref_regular_nonweak = 1;
2720 }
45d6a902 2721 else
f5385ebf 2722 h->def_regular = 1;
45d6a902
AM
2723 }
2724
2725 if (h->dynindx == -1
f5385ebf
AM
2726 && (h->def_dynamic
2727 || h->ref_dynamic))
45d6a902 2728 {
c152c796 2729 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2730 {
2731 eif->failed = TRUE;
2732 return FALSE;
2733 }
2734 }
2735 }
2736 else
2737 {
f5385ebf 2738 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2739 was first seen in a non-ELF file. Fortunately, if the symbol
2740 was first seen in an ELF file, we're probably OK unless the
2741 symbol was defined in a non-ELF file. Catch that case here.
2742 FIXME: We're still in trouble if the symbol was first seen in
2743 a dynamic object, and then later in a non-ELF regular object. */
2744 if ((h->root.type == bfd_link_hash_defined
2745 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2746 && !h->def_regular
45d6a902
AM
2747 && (h->root.u.def.section->owner != NULL
2748 ? (bfd_get_flavour (h->root.u.def.section->owner)
2749 != bfd_target_elf_flavour)
2750 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2751 && !h->def_dynamic)))
2752 h->def_regular = 1;
45d6a902
AM
2753 }
2754
508c3946 2755 /* Backend specific symbol fixup. */
33774f08
AM
2756 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2757 if (bed->elf_backend_fixup_symbol
2758 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2759 return FALSE;
508c3946 2760
45d6a902
AM
2761 /* If this is a final link, and the symbol was defined as a common
2762 symbol in a regular object file, and there was no definition in
2763 any dynamic object, then the linker will have allocated space for
f5385ebf 2764 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2765 flag will not have been set. */
2766 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2767 && !h->def_regular
2768 && h->ref_regular
2769 && !h->def_dynamic
96f29d96 2770 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2771 h->def_regular = 1;
45d6a902 2772
4deb8f71
L
2773 /* If a weak undefined symbol has non-default visibility, we also
2774 hide it from the dynamic linker. */
2775 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2776 && h->root.type == bfd_link_hash_undefweak)
2777 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2778
2779 /* A hidden versioned symbol in executable should be forced local if
2780 it is is locally defined, not referenced by shared library and not
2781 exported. */
2782 else if (bfd_link_executable (eif->info)
2783 && h->versioned == versioned_hidden
2784 && !eif->info->export_dynamic
2785 && !h->dynamic
2786 && !h->ref_dynamic
2787 && h->def_regular)
2788 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2789
45d6a902
AM
2790 /* If -Bsymbolic was used (which means to bind references to global
2791 symbols to the definition within the shared object), and this
2792 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2793 need a PLT entry. Likewise, if the symbol has non-default
2794 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2795 will force it local. */
4deb8f71
L
2796 else if (h->needs_plt
2797 && bfd_link_pic (eif->info)
2798 && is_elf_hash_table (eif->info->hash)
2799 && (SYMBOLIC_BIND (eif->info, h)
2800 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2801 && h->def_regular)
45d6a902 2802 {
45d6a902
AM
2803 bfd_boolean force_local;
2804
45d6a902
AM
2805 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2806 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2807 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2808 }
2809
45d6a902
AM
2810 /* If this is a weak defined symbol in a dynamic object, and we know
2811 the real definition in the dynamic object, copy interesting flags
2812 over to the real definition. */
60d67dc8 2813 if (h->is_weakalias)
45d6a902 2814 {
60d67dc8
AM
2815 struct elf_link_hash_entry *def = weakdef (h);
2816
45d6a902
AM
2817 /* If the real definition is defined by a regular object file,
2818 don't do anything special. See the longer description in
2819 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2820 if (def->def_regular)
2821 {
2822 h = def;
2823 while ((h = h->u.alias) != def)
2824 h->is_weakalias = 0;
2825 }
45d6a902 2826 else
a26587ba 2827 {
4e6b54a6
AM
2828 while (h->root.type == bfd_link_hash_indirect)
2829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2830 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2831 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2832 BFD_ASSERT (def->def_dynamic);
2833 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2834 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2835 }
45d6a902
AM
2836 }
2837
2838 return TRUE;
2839}
2840
2841/* Make the backend pick a good value for a dynamic symbol. This is
2842 called via elf_link_hash_traverse, and also calls itself
2843 recursively. */
2844
28caa186 2845static bfd_boolean
268b6b39 2846_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2847{
a50b1753 2848 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2849 struct elf_link_hash_table *htab;
9c5bfbb7 2850 const struct elf_backend_data *bed;
45d6a902 2851
0eddce27 2852 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2853 return FALSE;
2854
45d6a902
AM
2855 /* Ignore indirect symbols. These are added by the versioning code. */
2856 if (h->root.type == bfd_link_hash_indirect)
2857 return TRUE;
2858
2859 /* Fix the symbol flags. */
2860 if (! _bfd_elf_fix_symbol_flags (h, eif))
2861 return FALSE;
2862
559192d8
AM
2863 htab = elf_hash_table (eif->info);
2864 bed = get_elf_backend_data (htab->dynobj);
2865
954b63d4
AM
2866 if (h->root.type == bfd_link_hash_undefweak)
2867 {
2868 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2870 else if (eif->info->dynamic_undefined_weak > 0
2871 && h->ref_regular
2872 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2873 && !bfd_hide_sym_by_version (eif->info->version_info,
2874 h->root.root.string))
2875 {
2876 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2877 {
2878 eif->failed = TRUE;
2879 return FALSE;
2880 }
2881 }
2882 }
2883
45d6a902
AM
2884 /* If this symbol does not require a PLT entry, and it is not
2885 defined by a dynamic object, or is not referenced by a regular
2886 object, ignore it. We do have to handle a weak defined symbol,
2887 even if no regular object refers to it, if we decided to add it
2888 to the dynamic symbol table. FIXME: Do we normally need to worry
2889 about symbols which are defined by one dynamic object and
2890 referenced by another one? */
f5385ebf 2891 if (!h->needs_plt
91e21fb7 2892 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2893 && (h->def_regular
2894 || !h->def_dynamic
2895 || (!h->ref_regular
60d67dc8 2896 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2897 {
a6aa5195 2898 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2899 return TRUE;
2900 }
2901
2902 /* If we've already adjusted this symbol, don't do it again. This
2903 can happen via a recursive call. */
f5385ebf 2904 if (h->dynamic_adjusted)
45d6a902
AM
2905 return TRUE;
2906
2907 /* Don't look at this symbol again. Note that we must set this
2908 after checking the above conditions, because we may look at a
2909 symbol once, decide not to do anything, and then get called
2910 recursively later after REF_REGULAR is set below. */
f5385ebf 2911 h->dynamic_adjusted = 1;
45d6a902
AM
2912
2913 /* If this is a weak definition, and we know a real definition, and
2914 the real symbol is not itself defined by a regular object file,
2915 then get a good value for the real definition. We handle the
2916 real symbol first, for the convenience of the backend routine.
2917
2918 Note that there is a confusing case here. If the real definition
2919 is defined by a regular object file, we don't get the real symbol
2920 from the dynamic object, but we do get the weak symbol. If the
2921 processor backend uses a COPY reloc, then if some routine in the
2922 dynamic object changes the real symbol, we will not see that
2923 change in the corresponding weak symbol. This is the way other
2924 ELF linkers work as well, and seems to be a result of the shared
2925 library model.
2926
2927 I will clarify this issue. Most SVR4 shared libraries define the
2928 variable _timezone and define timezone as a weak synonym. The
2929 tzset call changes _timezone. If you write
2930 extern int timezone;
2931 int _timezone = 5;
2932 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 you might expect that, since timezone is a synonym for _timezone,
2934 the same number will print both times. However, if the processor
2935 backend uses a COPY reloc, then actually timezone will be copied
2936 into your process image, and, since you define _timezone
2937 yourself, _timezone will not. Thus timezone and _timezone will
2938 wind up at different memory locations. The tzset call will set
2939 _timezone, leaving timezone unchanged. */
2940
60d67dc8 2941 if (h->is_weakalias)
45d6a902 2942 {
60d67dc8
AM
2943 struct elf_link_hash_entry *def = weakdef (h);
2944
ec24dc88 2945 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
2946 the alias by a regular object file via the weak symbol H. */
2947 def->ref_regular = 1;
45d6a902 2948
ec24dc88 2949 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
2950 the strong alias before H by recursively calling ourselves. */
2951 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
2952 return FALSE;
2953 }
2954
2955 /* If a symbol has no type and no size and does not require a PLT
2956 entry, then we are probably about to do the wrong thing here: we
2957 are probably going to create a COPY reloc for an empty object.
2958 This case can arise when a shared object is built with assembly
2959 code, and the assembly code fails to set the symbol type. */
2960 if (h->size == 0
2961 && h->type == STT_NOTYPE
f5385ebf 2962 && !h->needs_plt)
4eca0228 2963 _bfd_error_handler
45d6a902
AM
2964 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 h->root.root.string);
2966
45d6a902
AM
2967 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2968 {
2969 eif->failed = TRUE;
2970 return FALSE;
2971 }
2972
2973 return TRUE;
2974}
2975
027297b7
L
2976/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2977 DYNBSS. */
2978
2979bfd_boolean
6cabe1ea
AM
2980_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2981 struct elf_link_hash_entry *h,
027297b7
L
2982 asection *dynbss)
2983{
91ac5911 2984 unsigned int power_of_two;
027297b7
L
2985 bfd_vma mask;
2986 asection *sec = h->root.u.def.section;
2987
de194d85 2988 /* The section alignment of the definition is the maximum alignment
91ac5911
L
2989 requirement of symbols defined in the section. Since we don't
2990 know the symbol alignment requirement, we start with the
2991 maximum alignment and check low bits of the symbol address
2992 for the minimum alignment. */
2993 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2994 mask = ((bfd_vma) 1 << power_of_two) - 1;
2995 while ((h->root.u.def.value & mask) != 0)
2996 {
2997 mask >>= 1;
2998 --power_of_two;
2999 }
027297b7 3000
91ac5911
L
3001 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3002 dynbss))
027297b7
L
3003 {
3004 /* Adjust the section alignment if needed. */
3005 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3006 power_of_two))
027297b7
L
3007 return FALSE;
3008 }
3009
91ac5911 3010 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3011 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3012
3013 /* Define the symbol as being at this point in DYNBSS. */
3014 h->root.u.def.section = dynbss;
3015 h->root.u.def.value = dynbss->size;
3016
3017 /* Increment the size of DYNBSS to make room for the symbol. */
3018 dynbss->size += h->size;
3019
f7483970
L
3020 /* No error if extern_protected_data is true. */
3021 if (h->protected_def
889c2a67
L
3022 && (!info->extern_protected_data
3023 || (info->extern_protected_data < 0
3024 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
3025 info->callbacks->einfo
3026 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3027 h->root.root.string);
6cabe1ea 3028
027297b7
L
3029 return TRUE;
3030}
3031
45d6a902
AM
3032/* Adjust all external symbols pointing into SEC_MERGE sections
3033 to reflect the object merging within the sections. */
3034
28caa186 3035static bfd_boolean
268b6b39 3036_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3037{
3038 asection *sec;
3039
45d6a902
AM
3040 if ((h->root.type == bfd_link_hash_defined
3041 || h->root.type == bfd_link_hash_defweak)
3042 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3043 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3044 {
a50b1753 3045 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3046
3047 h->root.u.def.value =
3048 _bfd_merged_section_offset (output_bfd,
3049 &h->root.u.def.section,
3050 elf_section_data (sec)->sec_info,
753731ee 3051 h->root.u.def.value);
45d6a902
AM
3052 }
3053
3054 return TRUE;
3055}
986a241f
RH
3056
3057/* Returns false if the symbol referred to by H should be considered
3058 to resolve local to the current module, and true if it should be
3059 considered to bind dynamically. */
3060
3061bfd_boolean
268b6b39
AM
3062_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3063 struct bfd_link_info *info,
89a2ee5a 3064 bfd_boolean not_local_protected)
986a241f
RH
3065{
3066 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3067 const struct elf_backend_data *bed;
3068 struct elf_link_hash_table *hash_table;
986a241f
RH
3069
3070 if (h == NULL)
3071 return FALSE;
3072
3073 while (h->root.type == bfd_link_hash_indirect
3074 || h->root.type == bfd_link_hash_warning)
3075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3076
3077 /* If it was forced local, then clearly it's not dynamic. */
3078 if (h->dynindx == -1)
3079 return FALSE;
f5385ebf 3080 if (h->forced_local)
986a241f
RH
3081 return FALSE;
3082
3083 /* Identify the cases where name binding rules say that a
3084 visible symbol resolves locally. */
0e1862bb
L
3085 binding_stays_local_p = (bfd_link_executable (info)
3086 || SYMBOLIC_BIND (info, h));
986a241f
RH
3087
3088 switch (ELF_ST_VISIBILITY (h->other))
3089 {
3090 case STV_INTERNAL:
3091 case STV_HIDDEN:
3092 return FALSE;
3093
3094 case STV_PROTECTED:
fcb93ecf
PB
3095 hash_table = elf_hash_table (info);
3096 if (!is_elf_hash_table (hash_table))
3097 return FALSE;
3098
3099 bed = get_elf_backend_data (hash_table->dynobj);
3100
986a241f
RH
3101 /* Proper resolution for function pointer equality may require
3102 that these symbols perhaps be resolved dynamically, even though
3103 we should be resolving them to the current module. */
89a2ee5a 3104 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3105 binding_stays_local_p = TRUE;
3106 break;
3107
3108 default:
986a241f
RH
3109 break;
3110 }
3111
aa37626c 3112 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3113 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3114 return TRUE;
3115
986a241f
RH
3116 /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 us that it remains local. */
3118 return !binding_stays_local_p;
3119}
f6c52c13
AM
3120
3121/* Return true if the symbol referred to by H should be considered
3122 to resolve local to the current module, and false otherwise. Differs
3123 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3124 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3125 for the place where dynindx == -1 is tested. If that test is true,
3126 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3128 defined symbols.
89a2ee5a
AM
3129 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 treatment of undefined weak symbols. For those that do not make
3132 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3133
3134bfd_boolean
268b6b39
AM
3135_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3136 struct bfd_link_info *info,
3137 bfd_boolean local_protected)
f6c52c13 3138{
fcb93ecf
PB
3139 const struct elf_backend_data *bed;
3140 struct elf_link_hash_table *hash_table;
3141
f6c52c13
AM
3142 /* If it's a local sym, of course we resolve locally. */
3143 if (h == NULL)
3144 return TRUE;
3145
d95edcac
L
3146 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3148 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3149 return TRUE;
3150
0fad2956
MR
3151 /* Forced local symbols resolve locally. */
3152 if (h->forced_local)
3153 return TRUE;
3154
7e2294f9
AO
3155 /* Common symbols that become definitions don't get the DEF_REGULAR
3156 flag set, so test it first, and don't bail out. */
3157 if (ELF_COMMON_DEF_P (h))
3158 /* Do nothing. */;
f6c52c13 3159 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3160 resolve locally. The sym is either undefined or dynamic. */
3161 else if (!h->def_regular)
f6c52c13
AM
3162 return FALSE;
3163
0fad2956 3164 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3165 if (h->dynindx == -1)
3166 return TRUE;
3167
3168 /* At this point, we know the symbol is defined and dynamic. In an
3169 executable it must resolve locally, likewise when building symbolic
3170 shared libraries. */
0e1862bb 3171 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3172 return TRUE;
3173
3174 /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 with default visibility might not resolve locally. */
3176 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3177 return FALSE;
3178
fcb93ecf
PB
3179 hash_table = elf_hash_table (info);
3180 if (!is_elf_hash_table (hash_table))
3181 return TRUE;
3182
3183 bed = get_elf_backend_data (hash_table->dynobj);
3184
f7483970
L
3185 /* If extern_protected_data is false, STV_PROTECTED non-function
3186 symbols are local. */
889c2a67
L
3187 if ((!info->extern_protected_data
3188 || (info->extern_protected_data < 0
3189 && !bed->extern_protected_data))
3190 && !bed->is_function_type (h->type))
1c16dfa5
L
3191 return TRUE;
3192
f6c52c13 3193 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3194 symbols be treated as dynamic symbols. If the address of a
3195 function not defined in an executable is set to that function's
3196 plt entry in the executable, then the address of the function in
3197 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3198 return local_protected;
3199}
e1918d23
AM
3200
3201/* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 aligned. Returns the first TLS output section. */
3203
3204struct bfd_section *
3205_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3206{
3207 struct bfd_section *sec, *tls;
3208 unsigned int align = 0;
3209
3210 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3211 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3212 break;
3213 tls = sec;
3214
3215 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3216 if (sec->alignment_power > align)
3217 align = sec->alignment_power;
3218
3219 elf_hash_table (info)->tls_sec = tls;
3220
3221 /* Ensure the alignment of the first section is the largest alignment,
3222 so that the tls segment starts aligned. */
3223 if (tls != NULL)
3224 tls->alignment_power = align;
3225
3226 return tls;
3227}
0ad989f9
L
3228
3229/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3230static bfd_boolean
3231is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3232 Elf_Internal_Sym *sym)
3233{
a4d8e49b
L
3234 const struct elf_backend_data *bed;
3235
0ad989f9
L
3236 /* Local symbols do not count, but target specific ones might. */
3237 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3238 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3239 return FALSE;
3240
fcb93ecf 3241 bed = get_elf_backend_data (abfd);
0ad989f9 3242 /* Function symbols do not count. */
fcb93ecf 3243 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3244 return FALSE;
3245
3246 /* If the section is undefined, then so is the symbol. */
3247 if (sym->st_shndx == SHN_UNDEF)
3248 return FALSE;
3249
3250 /* If the symbol is defined in the common section, then
3251 it is a common definition and so does not count. */
a4d8e49b 3252 if (bed->common_definition (sym))
0ad989f9
L
3253 return FALSE;
3254
3255 /* If the symbol is in a target specific section then we
3256 must rely upon the backend to tell us what it is. */
3257 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3258 /* FIXME - this function is not coded yet:
3259
3260 return _bfd_is_global_symbol_definition (abfd, sym);
3261
3262 Instead for now assume that the definition is not global,
3263 Even if this is wrong, at least the linker will behave
3264 in the same way that it used to do. */
3265 return FALSE;
3266
3267 return TRUE;
3268}
3269
3270/* Search the symbol table of the archive element of the archive ABFD
3271 whose archive map contains a mention of SYMDEF, and determine if
3272 the symbol is defined in this element. */
3273static bfd_boolean
3274elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3275{
3276 Elf_Internal_Shdr * hdr;
ef53be89
AM
3277 size_t symcount;
3278 size_t extsymcount;
3279 size_t extsymoff;
0ad989f9
L
3280 Elf_Internal_Sym *isymbuf;
3281 Elf_Internal_Sym *isym;
3282 Elf_Internal_Sym *isymend;
3283 bfd_boolean result;
3284
3285 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3286 if (abfd == NULL)
3287 return FALSE;
3288
3289 if (! bfd_check_format (abfd, bfd_object))
3290 return FALSE;
3291
7dc3990e
L
3292 /* Select the appropriate symbol table. If we don't know if the
3293 object file is an IR object, give linker LTO plugin a chance to
3294 get the correct symbol table. */
3295 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3296#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3297 || (abfd->plugin_format == bfd_plugin_unknown
3298 && bfd_link_plugin_object_p (abfd))
3299#endif
3300 )
3301 {
3302 /* Use the IR symbol table if the object has been claimed by
3303 plugin. */
3304 abfd = abfd->plugin_dummy_bfd;
3305 hdr = &elf_tdata (abfd)->symtab_hdr;
3306 }
3307 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3308 hdr = &elf_tdata (abfd)->symtab_hdr;
3309 else
3310 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3311
3312 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3313
3314 /* The sh_info field of the symtab header tells us where the
3315 external symbols start. We don't care about the local symbols. */
3316 if (elf_bad_symtab (abfd))
3317 {
3318 extsymcount = symcount;
3319 extsymoff = 0;
3320 }
3321 else
3322 {
3323 extsymcount = symcount - hdr->sh_info;
3324 extsymoff = hdr->sh_info;
3325 }
3326
3327 if (extsymcount == 0)
3328 return FALSE;
3329
3330 /* Read in the symbol table. */
3331 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3332 NULL, NULL, NULL);
3333 if (isymbuf == NULL)
3334 return FALSE;
3335
3336 /* Scan the symbol table looking for SYMDEF. */
3337 result = FALSE;
3338 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3339 {
3340 const char *name;
3341
3342 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3343 isym->st_name);
3344 if (name == NULL)
3345 break;
3346
3347 if (strcmp (name, symdef->name) == 0)
3348 {
3349 result = is_global_data_symbol_definition (abfd, isym);
3350 break;
3351 }
3352 }
3353
3354 free (isymbuf);
3355
3356 return result;
3357}
3358\f
5a580b3a
AM
3359/* Add an entry to the .dynamic table. */
3360
3361bfd_boolean
3362_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3363 bfd_vma tag,
3364 bfd_vma val)
3365{
3366 struct elf_link_hash_table *hash_table;
3367 const struct elf_backend_data *bed;
3368 asection *s;
3369 bfd_size_type newsize;
3370 bfd_byte *newcontents;
3371 Elf_Internal_Dyn dyn;
3372
3373 hash_table = elf_hash_table (info);
3374 if (! is_elf_hash_table (hash_table))
3375 return FALSE;
3376
3377 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3378 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3379 BFD_ASSERT (s != NULL);
3380
eea6121a 3381 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3382 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3383 if (newcontents == NULL)
3384 return FALSE;
3385
3386 dyn.d_tag = tag;
3387 dyn.d_un.d_val = val;
eea6121a 3388 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3389
eea6121a 3390 s->size = newsize;
5a580b3a
AM
3391 s->contents = newcontents;
3392
3393 return TRUE;
3394}
3395
3396/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3397 otherwise just check whether one already exists. Returns -1 on error,
3398 1 if a DT_NEEDED tag already exists, and 0 on success. */
3399
4ad4eba5 3400static int
7e9f0867
AM
3401elf_add_dt_needed_tag (bfd *abfd,
3402 struct bfd_link_info *info,
4ad4eba5
AM
3403 const char *soname,
3404 bfd_boolean do_it)
5a580b3a
AM
3405{
3406 struct elf_link_hash_table *hash_table;
ef53be89 3407 size_t strindex;
5a580b3a 3408
7e9f0867
AM
3409 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3410 return -1;
3411
5a580b3a 3412 hash_table = elf_hash_table (info);
5a580b3a 3413 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3414 if (strindex == (size_t) -1)
5a580b3a
AM
3415 return -1;
3416
02be4619 3417 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3418 {
3419 asection *sdyn;
3420 const struct elf_backend_data *bed;
3421 bfd_byte *extdyn;
3422
3423 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3424 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3425 if (sdyn != NULL)
3426 for (extdyn = sdyn->contents;
3427 extdyn < sdyn->contents + sdyn->size;
3428 extdyn += bed->s->sizeof_dyn)
3429 {
3430 Elf_Internal_Dyn dyn;
5a580b3a 3431
7e9f0867
AM
3432 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3433 if (dyn.d_tag == DT_NEEDED
3434 && dyn.d_un.d_val == strindex)
3435 {
3436 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3437 return 1;
3438 }
3439 }
5a580b3a
AM
3440 }
3441
3442 if (do_it)
3443 {
7e9f0867
AM
3444 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3445 return -1;
3446
5a580b3a
AM
3447 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3448 return -1;
3449 }
3450 else
3451 /* We were just checking for existence of the tag. */
3452 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3453
3454 return 0;
3455}
3456
7b15fa7a
AM
3457/* Return true if SONAME is on the needed list between NEEDED and STOP
3458 (or the end of list if STOP is NULL), and needed by a library that
3459 will be loaded. */
3460
010e5ae2 3461static bfd_boolean
7b15fa7a
AM
3462on_needed_list (const char *soname,
3463 struct bfd_link_needed_list *needed,
3464 struct bfd_link_needed_list *stop)
010e5ae2 3465{
7b15fa7a
AM
3466 struct bfd_link_needed_list *look;
3467 for (look = needed; look != stop; look = look->next)
3468 if (strcmp (soname, look->name) == 0
3469 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3470 /* If needed by a library that itself is not directly
3471 needed, recursively check whether that library is
3472 indirectly needed. Since we add DT_NEEDED entries to
3473 the end of the list, library dependencies appear after
3474 the library. Therefore search prior to the current
3475 LOOK, preventing possible infinite recursion. */
3476 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3477 return TRUE;
3478
3479 return FALSE;
3480}
3481
14160578 3482/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3483static int
3484elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3485{
3486 const struct elf_link_hash_entry *h1;
3487 const struct elf_link_hash_entry *h2;
10b7e05b 3488 bfd_signed_vma vdiff;
5a580b3a
AM
3489
3490 h1 = *(const struct elf_link_hash_entry **) arg1;
3491 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3492 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3493 if (vdiff != 0)
3494 return vdiff > 0 ? 1 : -1;
3495 else
3496 {
d3435ae8 3497 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3498 if (sdiff != 0)
3499 return sdiff > 0 ? 1 : -1;
3500 }
14160578
AM
3501 vdiff = h1->size - h2->size;
3502 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3503}
4ad4eba5 3504
5a580b3a
AM
3505/* This function is used to adjust offsets into .dynstr for
3506 dynamic symbols. This is called via elf_link_hash_traverse. */
3507
3508static bfd_boolean
3509elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3510{
a50b1753 3511 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3512
5a580b3a
AM
3513 if (h->dynindx != -1)
3514 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3515 return TRUE;
3516}
3517
3518/* Assign string offsets in .dynstr, update all structures referencing
3519 them. */
3520
4ad4eba5
AM
3521static bfd_boolean
3522elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3523{
3524 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3525 struct elf_link_local_dynamic_entry *entry;
3526 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3527 bfd *dynobj = hash_table->dynobj;
3528 asection *sdyn;
3529 bfd_size_type size;
3530 const struct elf_backend_data *bed;
3531 bfd_byte *extdyn;
3532
3533 _bfd_elf_strtab_finalize (dynstr);
3534 size = _bfd_elf_strtab_size (dynstr);
3535
3536 bed = get_elf_backend_data (dynobj);
3d4d4302 3537 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3538 BFD_ASSERT (sdyn != NULL);
3539
3540 /* Update all .dynamic entries referencing .dynstr strings. */
3541 for (extdyn = sdyn->contents;
eea6121a 3542 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3543 extdyn += bed->s->sizeof_dyn)
3544 {
3545 Elf_Internal_Dyn dyn;
3546
3547 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3548 switch (dyn.d_tag)
3549 {
3550 case DT_STRSZ:
3551 dyn.d_un.d_val = size;
3552 break;
3553 case DT_NEEDED:
3554 case DT_SONAME:
3555 case DT_RPATH:
3556 case DT_RUNPATH:
3557 case DT_FILTER:
3558 case DT_AUXILIARY:
7ee314fa
AM
3559 case DT_AUDIT:
3560 case DT_DEPAUDIT:
5a580b3a
AM
3561 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3562 break;
3563 default:
3564 continue;
3565 }
3566 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3567 }
3568
3569 /* Now update local dynamic symbols. */
3570 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3571 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3572 entry->isym.st_name);
3573
3574 /* And the rest of dynamic symbols. */
3575 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3576
3577 /* Adjust version definitions. */
3578 if (elf_tdata (output_bfd)->cverdefs)
3579 {
3580 asection *s;
3581 bfd_byte *p;
ef53be89 3582 size_t i;
5a580b3a
AM
3583 Elf_Internal_Verdef def;
3584 Elf_Internal_Verdaux defaux;
3585
3d4d4302 3586 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3587 p = s->contents;
3588 do
3589 {
3590 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3591 &def);
3592 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3593 if (def.vd_aux != sizeof (Elf_External_Verdef))
3594 continue;
5a580b3a
AM
3595 for (i = 0; i < def.vd_cnt; ++i)
3596 {
3597 _bfd_elf_swap_verdaux_in (output_bfd,
3598 (Elf_External_Verdaux *) p, &defaux);
3599 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3600 defaux.vda_name);
3601 _bfd_elf_swap_verdaux_out (output_bfd,
3602 &defaux, (Elf_External_Verdaux *) p);
3603 p += sizeof (Elf_External_Verdaux);
3604 }
3605 }
3606 while (def.vd_next);
3607 }
3608
3609 /* Adjust version references. */
3610 if (elf_tdata (output_bfd)->verref)
3611 {
3612 asection *s;
3613 bfd_byte *p;
ef53be89 3614 size_t i;
5a580b3a
AM
3615 Elf_Internal_Verneed need;
3616 Elf_Internal_Vernaux needaux;
3617
3d4d4302 3618 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3619 p = s->contents;
3620 do
3621 {
3622 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3623 &need);
3624 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3625 _bfd_elf_swap_verneed_out (output_bfd, &need,
3626 (Elf_External_Verneed *) p);
3627 p += sizeof (Elf_External_Verneed);
3628 for (i = 0; i < need.vn_cnt; ++i)
3629 {
3630 _bfd_elf_swap_vernaux_in (output_bfd,
3631 (Elf_External_Vernaux *) p, &needaux);
3632 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3633 needaux.vna_name);
3634 _bfd_elf_swap_vernaux_out (output_bfd,
3635 &needaux,
3636 (Elf_External_Vernaux *) p);
3637 p += sizeof (Elf_External_Vernaux);
3638 }
3639 }
3640 while (need.vn_next);
3641 }
3642
3643 return TRUE;
3644}
3645\f
13285a1b
AM
3646/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3647 The default is to only match when the INPUT and OUTPUT are exactly
3648 the same target. */
3649
3650bfd_boolean
3651_bfd_elf_default_relocs_compatible (const bfd_target *input,
3652 const bfd_target *output)
3653{
3654 return input == output;
3655}
3656
3657/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3658 This version is used when different targets for the same architecture
3659 are virtually identical. */
3660
3661bfd_boolean
3662_bfd_elf_relocs_compatible (const bfd_target *input,
3663 const bfd_target *output)
3664{
3665 const struct elf_backend_data *obed, *ibed;
3666
3667 if (input == output)
3668 return TRUE;
3669
3670 ibed = xvec_get_elf_backend_data (input);
3671 obed = xvec_get_elf_backend_data (output);
3672
3673 if (ibed->arch != obed->arch)
3674 return FALSE;
3675
3676 /* If both backends are using this function, deem them compatible. */
3677 return ibed->relocs_compatible == obed->relocs_compatible;
3678}
3679
e5034e59
AM
3680/* Make a special call to the linker "notice" function to tell it that
3681 we are about to handle an as-needed lib, or have finished
1b786873 3682 processing the lib. */
e5034e59
AM
3683
3684bfd_boolean
3685_bfd_elf_notice_as_needed (bfd *ibfd,
3686 struct bfd_link_info *info,
3687 enum notice_asneeded_action act)
3688{
46135103 3689 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3690}
3691
d9689752
L
3692/* Check relocations an ELF object file. */
3693
3694bfd_boolean
3695_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3696{
3697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3698 struct elf_link_hash_table *htab = elf_hash_table (info);
3699
3700 /* If this object is the same format as the output object, and it is
3701 not a shared library, then let the backend look through the
3702 relocs.
3703
3704 This is required to build global offset table entries and to
3705 arrange for dynamic relocs. It is not required for the
3706 particular common case of linking non PIC code, even when linking
3707 against shared libraries, but unfortunately there is no way of
3708 knowing whether an object file has been compiled PIC or not.
3709 Looking through the relocs is not particularly time consuming.
3710 The problem is that we must either (1) keep the relocs in memory,
3711 which causes the linker to require additional runtime memory or
3712 (2) read the relocs twice from the input file, which wastes time.
3713 This would be a good case for using mmap.
3714
3715 I have no idea how to handle linking PIC code into a file of a
3716 different format. It probably can't be done. */
3717 if ((abfd->flags & DYNAMIC) == 0
3718 && is_elf_hash_table (htab)
3719 && bed->check_relocs != NULL
3720 && elf_object_id (abfd) == elf_hash_table_id (htab)
3721 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3722 {
3723 asection *o;
3724
3725 for (o = abfd->sections; o != NULL; o = o->next)
3726 {
3727 Elf_Internal_Rela *internal_relocs;
3728 bfd_boolean ok;
3729
5ce03cea 3730 /* Don't check relocations in excluded sections. */
d9689752 3731 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3732 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3733 || o->reloc_count == 0
3734 || ((info->strip == strip_all || info->strip == strip_debugger)
3735 && (o->flags & SEC_DEBUGGING) != 0)
3736 || bfd_is_abs_section (o->output_section))
3737 continue;
3738
3739 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3740 info->keep_memory);
3741 if (internal_relocs == NULL)
3742 return FALSE;
3743
3744 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3745
3746 if (elf_section_data (o)->relocs != internal_relocs)
3747 free (internal_relocs);
3748
3749 if (! ok)
3750 return FALSE;
3751 }
3752 }
3753
3754 return TRUE;
3755}
3756
4ad4eba5
AM
3757/* Add symbols from an ELF object file to the linker hash table. */
3758
3759static bfd_boolean
3760elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3761{
a0c402a5 3762 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3763 Elf_Internal_Shdr *hdr;
ef53be89
AM
3764 size_t symcount;
3765 size_t extsymcount;
3766 size_t extsymoff;
4ad4eba5
AM
3767 struct elf_link_hash_entry **sym_hash;
3768 bfd_boolean dynamic;
3769 Elf_External_Versym *extversym = NULL;
3770 Elf_External_Versym *ever;
3771 struct elf_link_hash_entry *weaks;
3772 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3773 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3774 Elf_Internal_Sym *isymbuf = NULL;
3775 Elf_Internal_Sym *isym;
3776 Elf_Internal_Sym *isymend;
3777 const struct elf_backend_data *bed;
3778 bfd_boolean add_needed;
66eb6687 3779 struct elf_link_hash_table *htab;
4ad4eba5 3780 bfd_size_type amt;
66eb6687 3781 void *alloc_mark = NULL;
4f87808c
AM
3782 struct bfd_hash_entry **old_table = NULL;
3783 unsigned int old_size = 0;
3784 unsigned int old_count = 0;
66eb6687 3785 void *old_tab = NULL;
66eb6687
AM
3786 void *old_ent;
3787 struct bfd_link_hash_entry *old_undefs = NULL;
3788 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3789 void *old_strtab = NULL;
66eb6687 3790 size_t tabsize = 0;
db6a5d5f 3791 asection *s;
29a9f53e 3792 bfd_boolean just_syms;
4ad4eba5 3793
66eb6687 3794 htab = elf_hash_table (info);
4ad4eba5 3795 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3796
3797 if ((abfd->flags & DYNAMIC) == 0)
3798 dynamic = FALSE;
3799 else
3800 {
3801 dynamic = TRUE;
3802
3803 /* You can't use -r against a dynamic object. Also, there's no
3804 hope of using a dynamic object which does not exactly match
3805 the format of the output file. */
0e1862bb 3806 if (bfd_link_relocatable (info)
66eb6687 3807 || !is_elf_hash_table (htab)
f13a99db 3808 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3809 {
0e1862bb 3810 if (bfd_link_relocatable (info))
9a0789ec
NC
3811 bfd_set_error (bfd_error_invalid_operation);
3812 else
3813 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3814 goto error_return;
3815 }
3816 }
3817
a0c402a5
L
3818 ehdr = elf_elfheader (abfd);
3819 if (info->warn_alternate_em
3820 && bed->elf_machine_code != ehdr->e_machine
3821 && ((bed->elf_machine_alt1 != 0
3822 && ehdr->e_machine == bed->elf_machine_alt1)
3823 || (bed->elf_machine_alt2 != 0
3824 && ehdr->e_machine == bed->elf_machine_alt2)))
3825 info->callbacks->einfo
695344c0 3826 /* xgettext:c-format */
871b3ab2 3827 (_("%P: alternate ELF machine code found (%d) in %pB, expecting %d\n"),
a0c402a5
L
3828 ehdr->e_machine, abfd, bed->elf_machine_code);
3829
4ad4eba5
AM
3830 /* As a GNU extension, any input sections which are named
3831 .gnu.warning.SYMBOL are treated as warning symbols for the given
3832 symbol. This differs from .gnu.warning sections, which generate
3833 warnings when they are included in an output file. */
dd98f8d2 3834 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3835 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3836 {
db6a5d5f 3837 const char *name;
4ad4eba5 3838
db6a5d5f
AM
3839 name = bfd_get_section_name (abfd, s);
3840 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3841 {
db6a5d5f
AM
3842 char *msg;
3843 bfd_size_type sz;
3844
3845 name += sizeof ".gnu.warning." - 1;
3846
3847 /* If this is a shared object, then look up the symbol
3848 in the hash table. If it is there, and it is already
3849 been defined, then we will not be using the entry
3850 from this shared object, so we don't need to warn.
3851 FIXME: If we see the definition in a regular object
3852 later on, we will warn, but we shouldn't. The only
3853 fix is to keep track of what warnings we are supposed
3854 to emit, and then handle them all at the end of the
3855 link. */
3856 if (dynamic)
4ad4eba5 3857 {
db6a5d5f
AM
3858 struct elf_link_hash_entry *h;
3859
3860 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3861
3862 /* FIXME: What about bfd_link_hash_common? */
3863 if (h != NULL
3864 && (h->root.type == bfd_link_hash_defined
3865 || h->root.type == bfd_link_hash_defweak))
3866 continue;
3867 }
4ad4eba5 3868
db6a5d5f
AM
3869 sz = s->size;
3870 msg = (char *) bfd_alloc (abfd, sz + 1);
3871 if (msg == NULL)
3872 goto error_return;
4ad4eba5 3873
db6a5d5f
AM
3874 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3875 goto error_return;
4ad4eba5 3876
db6a5d5f 3877 msg[sz] = '\0';
4ad4eba5 3878
db6a5d5f
AM
3879 if (! (_bfd_generic_link_add_one_symbol
3880 (info, abfd, name, BSF_WARNING, s, 0, msg,
3881 FALSE, bed->collect, NULL)))
3882 goto error_return;
4ad4eba5 3883
0e1862bb 3884 if (bfd_link_executable (info))
db6a5d5f
AM
3885 {
3886 /* Clobber the section size so that the warning does
3887 not get copied into the output file. */
3888 s->size = 0;
11d2f718 3889
db6a5d5f
AM
3890 /* Also set SEC_EXCLUDE, so that symbols defined in
3891 the warning section don't get copied to the output. */
3892 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3893 }
3894 }
3895 }
3896
29a9f53e
L
3897 just_syms = ((s = abfd->sections) != NULL
3898 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3899
4ad4eba5
AM
3900 add_needed = TRUE;
3901 if (! dynamic)
3902 {
3903 /* If we are creating a shared library, create all the dynamic
3904 sections immediately. We need to attach them to something,
3905 so we attach them to this BFD, provided it is the right
bf89386a
L
3906 format and is not from ld --just-symbols. Always create the
3907 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3908 are no input BFD's of the same format as the output, we can't
3909 make a shared library. */
3910 if (!just_syms
bf89386a 3911 && (bfd_link_pic (info)
9c1d7a08 3912 || (!bfd_link_relocatable (info)
3c5fce9b 3913 && info->nointerp
9c1d7a08 3914 && (info->export_dynamic || info->dynamic)))
66eb6687 3915 && is_elf_hash_table (htab)
f13a99db 3916 && info->output_bfd->xvec == abfd->xvec
66eb6687 3917 && !htab->dynamic_sections_created)
4ad4eba5
AM
3918 {
3919 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3920 goto error_return;
3921 }
3922 }
66eb6687 3923 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3924 goto error_return;
3925 else
3926 {
4ad4eba5 3927 const char *soname = NULL;
7ee314fa 3928 char *audit = NULL;
4ad4eba5 3929 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3930 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3931 int ret;
3932
3933 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3934 ld shouldn't allow it. */
29a9f53e 3935 if (just_syms)
92fd189d 3936 abort ();
4ad4eba5
AM
3937
3938 /* If this dynamic lib was specified on the command line with
3939 --as-needed in effect, then we don't want to add a DT_NEEDED
3940 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3941 in by another lib's DT_NEEDED. When --no-add-needed is used
3942 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3943 any dynamic library in DT_NEEDED tags in the dynamic lib at
3944 all. */
3945 add_needed = (elf_dyn_lib_class (abfd)
3946 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3947 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3948
3949 s = bfd_get_section_by_name (abfd, ".dynamic");
3950 if (s != NULL)
3951 {
3952 bfd_byte *dynbuf;
3953 bfd_byte *extdyn;
cb33740c 3954 unsigned int elfsec;
4ad4eba5
AM
3955 unsigned long shlink;
3956
eea6121a 3957 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3958 {
3959error_free_dyn:
3960 free (dynbuf);
3961 goto error_return;
3962 }
4ad4eba5
AM
3963
3964 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3965 if (elfsec == SHN_BAD)
4ad4eba5
AM
3966 goto error_free_dyn;
3967 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3968
3969 for (extdyn = dynbuf;
eea6121a 3970 extdyn < dynbuf + s->size;
4ad4eba5
AM
3971 extdyn += bed->s->sizeof_dyn)
3972 {
3973 Elf_Internal_Dyn dyn;
3974
3975 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3976 if (dyn.d_tag == DT_SONAME)
3977 {
3978 unsigned int tagv = dyn.d_un.d_val;
3979 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3980 if (soname == NULL)
3981 goto error_free_dyn;
3982 }
3983 if (dyn.d_tag == DT_NEEDED)
3984 {
3985 struct bfd_link_needed_list *n, **pn;
3986 char *fnm, *anm;
3987 unsigned int tagv = dyn.d_un.d_val;
3988
3989 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3990 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3991 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3992 if (n == NULL || fnm == NULL)
3993 goto error_free_dyn;
3994 amt = strlen (fnm) + 1;
a50b1753 3995 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3996 if (anm == NULL)
3997 goto error_free_dyn;
3998 memcpy (anm, fnm, amt);
3999 n->name = anm;
4000 n->by = abfd;
4001 n->next = NULL;
66eb6687 4002 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4003 ;
4004 *pn = n;
4005 }
4006 if (dyn.d_tag == DT_RUNPATH)
4007 {
4008 struct bfd_link_needed_list *n, **pn;
4009 char *fnm, *anm;
4010 unsigned int tagv = dyn.d_un.d_val;
4011
4012 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4013 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4014 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4015 if (n == NULL || fnm == NULL)
4016 goto error_free_dyn;
4017 amt = strlen (fnm) + 1;
a50b1753 4018 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4019 if (anm == NULL)
4020 goto error_free_dyn;
4021 memcpy (anm, fnm, amt);
4022 n->name = anm;
4023 n->by = abfd;
4024 n->next = NULL;
4025 for (pn = & runpath;
4026 *pn != NULL;
4027 pn = &(*pn)->next)
4028 ;
4029 *pn = n;
4030 }
4031 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4032 if (!runpath && dyn.d_tag == DT_RPATH)
4033 {
4034 struct bfd_link_needed_list *n, **pn;
4035 char *fnm, *anm;
4036 unsigned int tagv = dyn.d_un.d_val;
4037
4038 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4039 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4040 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4041 if (n == NULL || fnm == NULL)
4042 goto error_free_dyn;
4043 amt = strlen (fnm) + 1;
a50b1753 4044 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4045 if (anm == NULL)
f8703194 4046 goto error_free_dyn;
4ad4eba5
AM
4047 memcpy (anm, fnm, amt);
4048 n->name = anm;
4049 n->by = abfd;
4050 n->next = NULL;
4051 for (pn = & rpath;
4052 *pn != NULL;
4053 pn = &(*pn)->next)
4054 ;
4055 *pn = n;
4056 }
7ee314fa
AM
4057 if (dyn.d_tag == DT_AUDIT)
4058 {
4059 unsigned int tagv = dyn.d_un.d_val;
4060 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4061 }
4ad4eba5
AM
4062 }
4063
4064 free (dynbuf);
4065 }
4066
4067 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4068 frees all more recently bfd_alloc'd blocks as well. */
4069 if (runpath)
4070 rpath = runpath;
4071
4072 if (rpath)
4073 {
4074 struct bfd_link_needed_list **pn;
66eb6687 4075 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4076 ;
4077 *pn = rpath;
4078 }
4079
9acc85a6
AM
4080 /* If we have a PT_GNU_RELRO program header, mark as read-only
4081 all sections contained fully therein. This makes relro
4082 shared library sections appear as they will at run-time. */
4083 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4084 while (--phdr >= elf_tdata (abfd)->phdr)
4085 if (phdr->p_type == PT_GNU_RELRO)
4086 {
4087 for (s = abfd->sections; s != NULL; s = s->next)
4088 if ((s->flags & SEC_ALLOC) != 0
4089 && s->vma >= phdr->p_vaddr
4090 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4091 s->flags |= SEC_READONLY;
4092 break;
4093 }
4094
4ad4eba5
AM
4095 /* We do not want to include any of the sections in a dynamic
4096 object in the output file. We hack by simply clobbering the
4097 list of sections in the BFD. This could be handled more
4098 cleanly by, say, a new section flag; the existing
4099 SEC_NEVER_LOAD flag is not the one we want, because that one
4100 still implies that the section takes up space in the output
4101 file. */
4102 bfd_section_list_clear (abfd);
4103
4ad4eba5
AM
4104 /* Find the name to use in a DT_NEEDED entry that refers to this
4105 object. If the object has a DT_SONAME entry, we use it.
4106 Otherwise, if the generic linker stuck something in
4107 elf_dt_name, we use that. Otherwise, we just use the file
4108 name. */
4109 if (soname == NULL || *soname == '\0')
4110 {
4111 soname = elf_dt_name (abfd);
4112 if (soname == NULL || *soname == '\0')
4113 soname = bfd_get_filename (abfd);
4114 }
4115
4116 /* Save the SONAME because sometimes the linker emulation code
4117 will need to know it. */
4118 elf_dt_name (abfd) = soname;
4119
7e9f0867 4120 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4121 if (ret < 0)
4122 goto error_return;
4123
4124 /* If we have already included this dynamic object in the
4125 link, just ignore it. There is no reason to include a
4126 particular dynamic object more than once. */
4127 if (ret > 0)
4128 return TRUE;
7ee314fa
AM
4129
4130 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4131 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4132 }
4133
4134 /* If this is a dynamic object, we always link against the .dynsym
4135 symbol table, not the .symtab symbol table. The dynamic linker
4136 will only see the .dynsym symbol table, so there is no reason to
4137 look at .symtab for a dynamic object. */
4138
4139 if (! dynamic || elf_dynsymtab (abfd) == 0)
4140 hdr = &elf_tdata (abfd)->symtab_hdr;
4141 else
4142 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4143
4144 symcount = hdr->sh_size / bed->s->sizeof_sym;
4145
4146 /* The sh_info field of the symtab header tells us where the
4147 external symbols start. We don't care about the local symbols at
4148 this point. */
4149 if (elf_bad_symtab (abfd))
4150 {
4151 extsymcount = symcount;
4152 extsymoff = 0;
4153 }
4154 else
4155 {
4156 extsymcount = symcount - hdr->sh_info;
4157 extsymoff = hdr->sh_info;
4158 }
4159
f45794cb 4160 sym_hash = elf_sym_hashes (abfd);
012b2306 4161 if (extsymcount != 0)
4ad4eba5
AM
4162 {
4163 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4164 NULL, NULL, NULL);
4165 if (isymbuf == NULL)
4166 goto error_return;
4167
4ad4eba5 4168 if (sym_hash == NULL)
012b2306
AM
4169 {
4170 /* We store a pointer to the hash table entry for each
4171 external symbol. */
ef53be89
AM
4172 amt = extsymcount;
4173 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4174 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4175 if (sym_hash == NULL)
4176 goto error_free_sym;
4177 elf_sym_hashes (abfd) = sym_hash;
4178 }
4ad4eba5
AM
4179 }
4180
4181 if (dynamic)
4182 {
4183 /* Read in any version definitions. */
fc0e6df6
PB
4184 if (!_bfd_elf_slurp_version_tables (abfd,
4185 info->default_imported_symver))
4ad4eba5
AM
4186 goto error_free_sym;
4187
4188 /* Read in the symbol versions, but don't bother to convert them
4189 to internal format. */
4190 if (elf_dynversym (abfd) != 0)
4191 {
4192 Elf_Internal_Shdr *versymhdr;
4193
4194 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4195 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4196 if (extversym == NULL)
4197 goto error_free_sym;
4198 amt = versymhdr->sh_size;
4199 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4200 || bfd_bread (extversym, amt, abfd) != amt)
4201 goto error_free_vers;
4202 }
4203 }
4204
66eb6687
AM
4205 /* If we are loading an as-needed shared lib, save the symbol table
4206 state before we start adding symbols. If the lib turns out
4207 to be unneeded, restore the state. */
4208 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4209 {
4210 unsigned int i;
4211 size_t entsize;
4212
4213 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4214 {
4215 struct bfd_hash_entry *p;
2de92251 4216 struct elf_link_hash_entry *h;
66eb6687
AM
4217
4218 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4219 {
4220 h = (struct elf_link_hash_entry *) p;
4221 entsize += htab->root.table.entsize;
4222 if (h->root.type == bfd_link_hash_warning)
4223 entsize += htab->root.table.entsize;
4224 }
66eb6687
AM
4225 }
4226
4227 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4228 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4229 if (old_tab == NULL)
4230 goto error_free_vers;
4231
4232 /* Remember the current objalloc pointer, so that all mem for
4233 symbols added can later be reclaimed. */
4234 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4235 if (alloc_mark == NULL)
4236 goto error_free_vers;
4237
5061a885
AM
4238 /* Make a special call to the linker "notice" function to
4239 tell it that we are about to handle an as-needed lib. */
e5034e59 4240 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4241 goto error_free_vers;
5061a885 4242
f45794cb
AM
4243 /* Clone the symbol table. Remember some pointers into the
4244 symbol table, and dynamic symbol count. */
4245 old_ent = (char *) old_tab + tabsize;
66eb6687 4246 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4247 old_undefs = htab->root.undefs;
4248 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4249 old_table = htab->root.table.table;
4250 old_size = htab->root.table.size;
4251 old_count = htab->root.table.count;
5b677558
AM
4252 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4253 if (old_strtab == NULL)
4254 goto error_free_vers;
66eb6687
AM
4255
4256 for (i = 0; i < htab->root.table.size; i++)
4257 {
4258 struct bfd_hash_entry *p;
2de92251 4259 struct elf_link_hash_entry *h;
66eb6687
AM
4260
4261 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4262 {
4263 memcpy (old_ent, p, htab->root.table.entsize);
4264 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4265 h = (struct elf_link_hash_entry *) p;
4266 if (h->root.type == bfd_link_hash_warning)
4267 {
4268 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4269 old_ent = (char *) old_ent + htab->root.table.entsize;
4270 }
66eb6687
AM
4271 }
4272 }
4273 }
4ad4eba5 4274
66eb6687 4275 weaks = NULL;
4ad4eba5
AM
4276 ever = extversym != NULL ? extversym + extsymoff : NULL;
4277 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4278 isym < isymend;
4279 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4280 {
4281 int bind;
4282 bfd_vma value;
af44c138 4283 asection *sec, *new_sec;
4ad4eba5
AM
4284 flagword flags;
4285 const char *name;
4286 struct elf_link_hash_entry *h;
90c984fc 4287 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4288 bfd_boolean definition;
4289 bfd_boolean size_change_ok;
4290 bfd_boolean type_change_ok;
37a9e49a
L
4291 bfd_boolean new_weak;
4292 bfd_boolean old_weak;
4ad4eba5 4293 bfd_boolean override;
a4d8e49b 4294 bfd_boolean common;
97196564 4295 bfd_boolean discarded;
4ad4eba5
AM
4296 unsigned int old_alignment;
4297 bfd *old_bfd;
6e33951e 4298 bfd_boolean matched;
4ad4eba5
AM
4299
4300 override = FALSE;
4301
4302 flags = BSF_NO_FLAGS;
4303 sec = NULL;
4304 value = isym->st_value;
a4d8e49b 4305 common = bed->common_definition (isym);
2980ccad
L
4306 if (common && info->inhibit_common_definition)
4307 {
4308 /* Treat common symbol as undefined for --no-define-common. */
4309 isym->st_shndx = SHN_UNDEF;
4310 common = FALSE;
4311 }
97196564 4312 discarded = FALSE;
4ad4eba5
AM
4313
4314 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4315 switch (bind)
4ad4eba5 4316 {
3e7a7d11 4317 case STB_LOCAL:
4ad4eba5
AM
4318 /* This should be impossible, since ELF requires that all
4319 global symbols follow all local symbols, and that sh_info
4320 point to the first global symbol. Unfortunately, Irix 5
4321 screws this up. */
4322 continue;
3e7a7d11
NC
4323
4324 case STB_GLOBAL:
a4d8e49b 4325 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4326 flags = BSF_GLOBAL;
3e7a7d11
NC
4327 break;
4328
4329 case STB_WEAK:
4330 flags = BSF_WEAK;
4331 break;
4332
4333 case STB_GNU_UNIQUE:
4334 flags = BSF_GNU_UNIQUE;
4335 break;
4336
4337 default:
4ad4eba5 4338 /* Leave it up to the processor backend. */
3e7a7d11 4339 break;
4ad4eba5
AM
4340 }
4341
4342 if (isym->st_shndx == SHN_UNDEF)
4343 sec = bfd_und_section_ptr;
cb33740c
AM
4344 else if (isym->st_shndx == SHN_ABS)
4345 sec = bfd_abs_section_ptr;
4346 else if (isym->st_shndx == SHN_COMMON)
4347 {
4348 sec = bfd_com_section_ptr;
4349 /* What ELF calls the size we call the value. What ELF
4350 calls the value we call the alignment. */
4351 value = isym->st_size;
4352 }
4353 else
4ad4eba5
AM
4354 {
4355 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4356 if (sec == NULL)
4357 sec = bfd_abs_section_ptr;
dbaa2011 4358 else if (discarded_section (sec))
529fcb95 4359 {
e5d08002
L
4360 /* Symbols from discarded section are undefined. We keep
4361 its visibility. */
529fcb95 4362 sec = bfd_und_section_ptr;
97196564 4363 discarded = TRUE;
529fcb95
PB
4364 isym->st_shndx = SHN_UNDEF;
4365 }
4ad4eba5
AM
4366 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4367 value -= sec->vma;
4368 }
4ad4eba5
AM
4369
4370 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4371 isym->st_name);
4372 if (name == NULL)
4373 goto error_free_vers;
4374
4375 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4376 && (abfd->flags & BFD_PLUGIN) != 0)
4377 {
4378 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4379
4380 if (xc == NULL)
4381 {
4382 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4383 | SEC_EXCLUDE);
4384 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4385 if (xc == NULL)
4386 goto error_free_vers;
4387 }
4388 sec = xc;
4389 }
4390 else if (isym->st_shndx == SHN_COMMON
4391 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4392 && !bfd_link_relocatable (info))
4ad4eba5
AM
4393 {
4394 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4395
4396 if (tcomm == NULL)
4397 {
02d00247
AM
4398 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4399 | SEC_LINKER_CREATED);
4400 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4401 if (tcomm == NULL)
4ad4eba5
AM
4402 goto error_free_vers;
4403 }
4404 sec = tcomm;
4405 }
66eb6687 4406 else if (bed->elf_add_symbol_hook)
4ad4eba5 4407 {
66eb6687
AM
4408 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4409 &sec, &value))
4ad4eba5
AM
4410 goto error_free_vers;
4411
4412 /* The hook function sets the name to NULL if this symbol
4413 should be skipped for some reason. */
4414 if (name == NULL)
4415 continue;
4416 }
4417
4418 /* Sanity check that all possibilities were handled. */
4419 if (sec == NULL)
4420 {
4421 bfd_set_error (bfd_error_bad_value);
4422 goto error_free_vers;
4423 }
4424
191c0c42
AM
4425 /* Silently discard TLS symbols from --just-syms. There's
4426 no way to combine a static TLS block with a new TLS block
4427 for this executable. */
4428 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4429 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4430 continue;
4431
4ad4eba5
AM
4432 if (bfd_is_und_section (sec)
4433 || bfd_is_com_section (sec))
4434 definition = FALSE;
4435 else
4436 definition = TRUE;
4437
4438 size_change_ok = FALSE;
66eb6687 4439 type_change_ok = bed->type_change_ok;
37a9e49a 4440 old_weak = FALSE;
6e33951e 4441 matched = FALSE;
4ad4eba5
AM
4442 old_alignment = 0;
4443 old_bfd = NULL;
af44c138 4444 new_sec = sec;
4ad4eba5 4445
66eb6687 4446 if (is_elf_hash_table (htab))
4ad4eba5
AM
4447 {
4448 Elf_Internal_Versym iver;
4449 unsigned int vernum = 0;
4450 bfd_boolean skip;
4451
fc0e6df6 4452 if (ever == NULL)
4ad4eba5 4453 {
fc0e6df6
PB
4454 if (info->default_imported_symver)
4455 /* Use the default symbol version created earlier. */
4456 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4457 else
4458 iver.vs_vers = 0;
4459 }
4460 else
4461 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4462
4463 vernum = iver.vs_vers & VERSYM_VERSION;
4464
4465 /* If this is a hidden symbol, or if it is not version
4466 1, we append the version name to the symbol name.
cc86ff91
EB
4467 However, we do not modify a non-hidden absolute symbol
4468 if it is not a function, because it might be the version
4469 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4470 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4471 || (vernum > 1
4472 && (!bfd_is_abs_section (sec)
4473 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4474 {
4475 const char *verstr;
4476 size_t namelen, verlen, newlen;
4477 char *newname, *p;
4478
4479 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4480 {
fc0e6df6
PB
4481 if (vernum > elf_tdata (abfd)->cverdefs)
4482 verstr = NULL;
4483 else if (vernum > 1)
4484 verstr =
4485 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4486 else
4487 verstr = "";
4ad4eba5 4488
fc0e6df6 4489 if (verstr == NULL)
4ad4eba5 4490 {
4eca0228 4491 _bfd_error_handler
695344c0 4492 /* xgettext:c-format */
871b3ab2 4493 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4494 abfd, name, vernum,
4495 elf_tdata (abfd)->cverdefs);
4496 bfd_set_error (bfd_error_bad_value);
4497 goto error_free_vers;
4ad4eba5 4498 }
fc0e6df6
PB
4499 }
4500 else
4501 {
4502 /* We cannot simply test for the number of
4503 entries in the VERNEED section since the
4504 numbers for the needed versions do not start
4505 at 0. */
4506 Elf_Internal_Verneed *t;
4507
4508 verstr = NULL;
4509 for (t = elf_tdata (abfd)->verref;
4510 t != NULL;
4511 t = t->vn_nextref)
4ad4eba5 4512 {
fc0e6df6 4513 Elf_Internal_Vernaux *a;
4ad4eba5 4514
fc0e6df6
PB
4515 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4516 {
4517 if (a->vna_other == vernum)
4ad4eba5 4518 {
fc0e6df6
PB
4519 verstr = a->vna_nodename;
4520 break;
4ad4eba5 4521 }
4ad4eba5 4522 }
fc0e6df6
PB
4523 if (a != NULL)
4524 break;
4525 }
4526 if (verstr == NULL)
4527 {
4eca0228 4528 _bfd_error_handler
695344c0 4529 /* xgettext:c-format */
871b3ab2 4530 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4531 abfd, name, vernum);
4532 bfd_set_error (bfd_error_bad_value);
4533 goto error_free_vers;
4ad4eba5 4534 }
4ad4eba5 4535 }
fc0e6df6
PB
4536
4537 namelen = strlen (name);
4538 verlen = strlen (verstr);
4539 newlen = namelen + verlen + 2;
4540 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4541 && isym->st_shndx != SHN_UNDEF)
4542 ++newlen;
4543
a50b1753 4544 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4545 if (newname == NULL)
4546 goto error_free_vers;
4547 memcpy (newname, name, namelen);
4548 p = newname + namelen;
4549 *p++ = ELF_VER_CHR;
4550 /* If this is a defined non-hidden version symbol,
4551 we add another @ to the name. This indicates the
4552 default version of the symbol. */
4553 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4554 && isym->st_shndx != SHN_UNDEF)
4555 *p++ = ELF_VER_CHR;
4556 memcpy (p, verstr, verlen + 1);
4557
4558 name = newname;
4ad4eba5
AM
4559 }
4560
cd3416da
AM
4561 /* If this symbol has default visibility and the user has
4562 requested we not re-export it, then mark it as hidden. */
a0d49154 4563 if (!bfd_is_und_section (sec)
cd3416da 4564 && !dynamic
ce875075 4565 && abfd->no_export
cd3416da
AM
4566 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4567 isym->st_other = (STV_HIDDEN
4568 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4569
4f3fedcf
AM
4570 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4571 sym_hash, &old_bfd, &old_weak,
4572 &old_alignment, &skip, &override,
6e33951e
L
4573 &type_change_ok, &size_change_ok,
4574 &matched))
4ad4eba5
AM
4575 goto error_free_vers;
4576
4577 if (skip)
4578 continue;
4579
6e33951e
L
4580 /* Override a definition only if the new symbol matches the
4581 existing one. */
4582 if (override && matched)
4ad4eba5
AM
4583 definition = FALSE;
4584
4585 h = *sym_hash;
4586 while (h->root.type == bfd_link_hash_indirect
4587 || h->root.type == bfd_link_hash_warning)
4588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4589
4ad4eba5 4590 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4591 && vernum > 1
4592 && definition)
4593 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4594 }
4595
4596 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4597 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4598 (struct bfd_link_hash_entry **) sym_hash)))
4599 goto error_free_vers;
4600
a43942db
MR
4601 if ((flags & BSF_GNU_UNIQUE)
4602 && (abfd->flags & DYNAMIC) == 0
4603 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4604 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4605
4ad4eba5 4606 h = *sym_hash;
90c984fc
L
4607 /* We need to make sure that indirect symbol dynamic flags are
4608 updated. */
4609 hi = h;
4ad4eba5
AM
4610 while (h->root.type == bfd_link_hash_indirect
4611 || h->root.type == bfd_link_hash_warning)
4612 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4613
97196564
L
4614 /* Setting the index to -3 tells elf_link_output_extsym that
4615 this symbol is defined in a discarded section. */
4616 if (discarded)
4617 h->indx = -3;
4618
4ad4eba5
AM
4619 *sym_hash = h;
4620
37a9e49a 4621 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4622 if (dynamic
4623 && definition
37a9e49a 4624 && new_weak
fcb93ecf 4625 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4626 && is_elf_hash_table (htab)
60d67dc8 4627 && h->u.alias == NULL)
4ad4eba5
AM
4628 {
4629 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4630 a dynamic object, using the alias field. Later in this
4631 function we will set the alias field to the correct
4ad4eba5
AM
4632 value. We only put non-function symbols from dynamic
4633 objects on this list, because that happens to be the only
4634 time we need to know the normal symbol corresponding to a
4635 weak symbol, and the information is time consuming to
60d67dc8 4636 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4637 then this symbol was already defined by some previous
4638 dynamic object, and we will be using that previous
4639 definition anyhow. */
4640
60d67dc8 4641 h->u.alias = weaks;
4ad4eba5 4642 weaks = h;
4ad4eba5
AM
4643 }
4644
4645 /* Set the alignment of a common symbol. */
a4d8e49b 4646 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4647 && h->root.type == bfd_link_hash_common)
4648 {
4649 unsigned int align;
4650
a4d8e49b 4651 if (common)
af44c138
L
4652 align = bfd_log2 (isym->st_value);
4653 else
4654 {
4655 /* The new symbol is a common symbol in a shared object.
4656 We need to get the alignment from the section. */
4657 align = new_sec->alignment_power;
4658 }
595213d4 4659 if (align > old_alignment)
4ad4eba5
AM
4660 h->root.u.c.p->alignment_power = align;
4661 else
4662 h->root.u.c.p->alignment_power = old_alignment;
4663 }
4664
66eb6687 4665 if (is_elf_hash_table (htab))
4ad4eba5 4666 {
4f3fedcf
AM
4667 /* Set a flag in the hash table entry indicating the type of
4668 reference or definition we just found. A dynamic symbol
4669 is one which is referenced or defined by both a regular
4670 object and a shared object. */
4671 bfd_boolean dynsym = FALSE;
4672
4673 /* Plugin symbols aren't normal. Don't set def_regular or
4674 ref_regular for them, or make them dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4676 ;
4677 else if (! dynamic)
4678 {
4679 if (! definition)
4680 {
4681 h->ref_regular = 1;
4682 if (bind != STB_WEAK)
4683 h->ref_regular_nonweak = 1;
4684 }
4685 else
4686 {
4687 h->def_regular = 1;
4688 if (h->def_dynamic)
4689 {
4690 h->def_dynamic = 0;
4691 h->ref_dynamic = 1;
4692 }
4693 }
4694
4695 /* If the indirect symbol has been forced local, don't
4696 make the real symbol dynamic. */
4697 if ((h == hi || !hi->forced_local)
0e1862bb 4698 && (bfd_link_dll (info)
4f3fedcf
AM
4699 || h->def_dynamic
4700 || h->ref_dynamic))
4701 dynsym = TRUE;
4702 }
4703 else
4704 {
4705 if (! definition)
4706 {
4707 h->ref_dynamic = 1;
4708 hi->ref_dynamic = 1;
4709 }
4710 else
4711 {
4712 h->def_dynamic = 1;
4713 hi->def_dynamic = 1;
4714 }
4715
4716 /* If the indirect symbol has been forced local, don't
4717 make the real symbol dynamic. */
4718 if ((h == hi || !hi->forced_local)
4719 && (h->def_regular
4720 || h->ref_regular
60d67dc8
AM
4721 || (h->is_weakalias
4722 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4723 dynsym = TRUE;
4724 }
4725
4726 /* Check to see if we need to add an indirect symbol for
4727 the default name. */
4728 if (definition
4729 || (!override && h->root.type == bfd_link_hash_common))
4730 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4731 sec, value, &old_bfd, &dynsym))
4732 goto error_free_vers;
4ad4eba5
AM
4733
4734 /* Check the alignment when a common symbol is involved. This
4735 can change when a common symbol is overridden by a normal
4736 definition or a common symbol is ignored due to the old
4737 normal definition. We need to make sure the maximum
4738 alignment is maintained. */
a4d8e49b 4739 if ((old_alignment || common)
4ad4eba5
AM
4740 && h->root.type != bfd_link_hash_common)
4741 {
4742 unsigned int common_align;
4743 unsigned int normal_align;
4744 unsigned int symbol_align;
4745 bfd *normal_bfd;
4746 bfd *common_bfd;
4747
3a81e825
AM
4748 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4749 || h->root.type == bfd_link_hash_defweak);
4750
4ad4eba5
AM
4751 symbol_align = ffs (h->root.u.def.value) - 1;
4752 if (h->root.u.def.section->owner != NULL
0616a280
AM
4753 && (h->root.u.def.section->owner->flags
4754 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4755 {
4756 normal_align = h->root.u.def.section->alignment_power;
4757 if (normal_align > symbol_align)
4758 normal_align = symbol_align;
4759 }
4760 else
4761 normal_align = symbol_align;
4762
4763 if (old_alignment)
4764 {
4765 common_align = old_alignment;
4766 common_bfd = old_bfd;
4767 normal_bfd = abfd;
4768 }
4769 else
4770 {
4771 common_align = bfd_log2 (isym->st_value);
4772 common_bfd = abfd;
4773 normal_bfd = old_bfd;
4774 }
4775
4776 if (normal_align < common_align)
d07676f8
NC
4777 {
4778 /* PR binutils/2735 */
4779 if (normal_bfd == NULL)
4eca0228 4780 _bfd_error_handler
695344c0 4781 /* xgettext:c-format */
871b3ab2
AM
4782 (_("Warning: alignment %u of common symbol `%s' in %pB is"
4783 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4784 1 << common_align, name, common_bfd,
4785 1 << normal_align, h->root.u.def.section);
d07676f8 4786 else
4eca0228 4787 _bfd_error_handler
695344c0 4788 /* xgettext:c-format */
871b3ab2
AM
4789 (_("Warning: alignment %u of symbol `%s' in %pB"
4790 " is smaller than %u in %pB"),
c08bb8dd
AM
4791 1 << normal_align, name, normal_bfd,
4792 1 << common_align, common_bfd);
d07676f8 4793 }
4ad4eba5
AM
4794 }
4795
83ad0046 4796 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4797 if (isym->st_size != 0
4798 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4799 && (definition || h->size == 0))
4800 {
83ad0046
L
4801 if (h->size != 0
4802 && h->size != isym->st_size
4803 && ! size_change_ok)
4eca0228 4804 _bfd_error_handler
695344c0 4805 /* xgettext:c-format */
d003868e 4806 (_("Warning: size of symbol `%s' changed"
871b3ab2 4807 " from %Lu in %pB to %Lu in %pB"),
76cfced5 4808 name, h->size, old_bfd, isym->st_size, abfd);
4ad4eba5
AM
4809
4810 h->size = isym->st_size;
4811 }
4812
4813 /* If this is a common symbol, then we always want H->SIZE
4814 to be the size of the common symbol. The code just above
4815 won't fix the size if a common symbol becomes larger. We
4816 don't warn about a size change here, because that is
4f3fedcf 4817 covered by --warn-common. Allow changes between different
fcb93ecf 4818 function types. */
4ad4eba5
AM
4819 if (h->root.type == bfd_link_hash_common)
4820 h->size = h->root.u.c.size;
4821
4822 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4823 && ((definition && !new_weak)
4824 || (old_weak && h->root.type == bfd_link_hash_common)
4825 || h->type == STT_NOTYPE))
4ad4eba5 4826 {
2955ec4c
L
4827 unsigned int type = ELF_ST_TYPE (isym->st_info);
4828
4829 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4830 symbol. */
4831 if (type == STT_GNU_IFUNC
4832 && (abfd->flags & DYNAMIC) != 0)
4833 type = STT_FUNC;
4ad4eba5 4834
2955ec4c
L
4835 if (h->type != type)
4836 {
4837 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4838 /* xgettext:c-format */
4eca0228 4839 _bfd_error_handler
2955ec4c 4840 (_("Warning: type of symbol `%s' changed"
871b3ab2 4841 " from %d to %d in %pB"),
c08bb8dd 4842 name, h->type, type, abfd);
2955ec4c
L
4843
4844 h->type = type;
4845 }
4ad4eba5
AM
4846 }
4847
54ac0771 4848 /* Merge st_other field. */
b8417128 4849 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4850
c3df8c14 4851 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4852 if (definition
4853 && (sec->flags & SEC_DEBUGGING)
4854 && !bfd_link_relocatable (info))
c3df8c14
AM
4855 dynsym = FALSE;
4856
4f3fedcf
AM
4857 /* Nor should we make plugin symbols dynamic. */
4858 if ((abfd->flags & BFD_PLUGIN) != 0)
4859 dynsym = FALSE;
4860
35fc36a8 4861 if (definition)
35399224
L
4862 {
4863 h->target_internal = isym->st_target_internal;
4864 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4865 }
35fc36a8 4866
4ad4eba5
AM
4867 if (definition && !dynamic)
4868 {
4869 char *p = strchr (name, ELF_VER_CHR);
4870 if (p != NULL && p[1] != ELF_VER_CHR)
4871 {
4872 /* Queue non-default versions so that .symver x, x@FOO
4873 aliases can be checked. */
66eb6687 4874 if (!nondeflt_vers)
4ad4eba5 4875 {
66eb6687
AM
4876 amt = ((isymend - isym + 1)
4877 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4878 nondeflt_vers
4879 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4880 if (!nondeflt_vers)
4881 goto error_free_vers;
4ad4eba5 4882 }
66eb6687 4883 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4884 }
4885 }
4886
4887 if (dynsym && h->dynindx == -1)
4888 {
c152c796 4889 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4890 goto error_free_vers;
60d67dc8
AM
4891 if (h->is_weakalias
4892 && weakdef (h)->dynindx == -1)
4ad4eba5 4893 {
60d67dc8 4894 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
4895 goto error_free_vers;
4896 }
4897 }
1f599d0e 4898 else if (h->dynindx != -1)
4ad4eba5
AM
4899 /* If the symbol already has a dynamic index, but
4900 visibility says it should not be visible, turn it into
4901 a local symbol. */
4902 switch (ELF_ST_VISIBILITY (h->other))
4903 {
4904 case STV_INTERNAL:
4905 case STV_HIDDEN:
4906 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4907 dynsym = FALSE;
4908 break;
4909 }
4910
aef28989
L
4911 /* Don't add DT_NEEDED for references from the dummy bfd nor
4912 for unmatched symbol. */
4ad4eba5 4913 if (!add_needed
aef28989 4914 && matched
4ad4eba5 4915 && definition
010e5ae2 4916 && ((dynsym
ffa9430d 4917 && h->ref_regular_nonweak
4f3fedcf
AM
4918 && (old_bfd == NULL
4919 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4920 || (h->ref_dynamic_nonweak
010e5ae2 4921 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4922 && !on_needed_list (elf_dt_name (abfd),
4923 htab->needed, NULL))))
4ad4eba5
AM
4924 {
4925 int ret;
4926 const char *soname = elf_dt_name (abfd);
4927
16e4ecc0
AM
4928 info->callbacks->minfo ("%!", soname, old_bfd,
4929 h->root.root.string);
4930
4ad4eba5
AM
4931 /* A symbol from a library loaded via DT_NEEDED of some
4932 other library is referenced by a regular object.
e56f61be 4933 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4934 --no-add-needed is used and the reference was not
4935 a weak one. */
4f3fedcf 4936 if (old_bfd != NULL
b918acf9 4937 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4938 {
4eca0228 4939 _bfd_error_handler
695344c0 4940 /* xgettext:c-format */
871b3ab2 4941 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 4942 old_bfd, name);
ff5ac77b 4943 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4944 goto error_free_vers;
4945 }
4946
a50b1753 4947 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4948 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4949
4ad4eba5 4950 add_needed = TRUE;
7e9f0867 4951 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4952 if (ret < 0)
4953 goto error_free_vers;
4954
4955 BFD_ASSERT (ret == 0);
4956 }
4957 }
4958 }
4959
a83ef4d1
L
4960 if (info->lto_plugin_active
4961 && !bfd_link_relocatable (info)
4962 && (abfd->flags & BFD_PLUGIN) == 0
4963 && !just_syms
4964 && extsymcount)
4965 {
4966 int r_sym_shift;
4967
4968 if (bed->s->arch_size == 32)
4969 r_sym_shift = 8;
4970 else
4971 r_sym_shift = 32;
4972
4973 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4974 referenced in regular objects so that linker plugin will get
4975 the correct symbol resolution. */
4976
4977 sym_hash = elf_sym_hashes (abfd);
4978 for (s = abfd->sections; s != NULL; s = s->next)
4979 {
4980 Elf_Internal_Rela *internal_relocs;
4981 Elf_Internal_Rela *rel, *relend;
4982
4983 /* Don't check relocations in excluded sections. */
4984 if ((s->flags & SEC_RELOC) == 0
4985 || s->reloc_count == 0
4986 || (s->flags & SEC_EXCLUDE) != 0
4987 || ((info->strip == strip_all
4988 || info->strip == strip_debugger)
4989 && (s->flags & SEC_DEBUGGING) != 0))
4990 continue;
4991
4992 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4993 NULL,
4994 info->keep_memory);
4995 if (internal_relocs == NULL)
4996 goto error_free_vers;
4997
4998 rel = internal_relocs;
4999 relend = rel + s->reloc_count;
5000 for ( ; rel < relend; rel++)
5001 {
5002 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5003 struct elf_link_hash_entry *h;
5004
5005 /* Skip local symbols. */
5006 if (r_symndx < extsymoff)
5007 continue;
5008
5009 h = sym_hash[r_symndx - extsymoff];
5010 if (h != NULL)
5011 h->root.non_ir_ref_regular = 1;
5012 }
5013
5014 if (elf_section_data (s)->relocs != internal_relocs)
5015 free (internal_relocs);
5016 }
5017 }
5018
66eb6687
AM
5019 if (extversym != NULL)
5020 {
5021 free (extversym);
5022 extversym = NULL;
5023 }
5024
5025 if (isymbuf != NULL)
5026 {
5027 free (isymbuf);
5028 isymbuf = NULL;
5029 }
5030
5031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5032 {
5033 unsigned int i;
5034
5035 /* Restore the symbol table. */
f45794cb
AM
5036 old_ent = (char *) old_tab + tabsize;
5037 memset (elf_sym_hashes (abfd), 0,
5038 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5039 htab->root.table.table = old_table;
5040 htab->root.table.size = old_size;
5041 htab->root.table.count = old_count;
66eb6687 5042 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5043 htab->root.undefs = old_undefs;
5044 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5045 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5046 free (old_strtab);
5047 old_strtab = NULL;
66eb6687
AM
5048 for (i = 0; i < htab->root.table.size; i++)
5049 {
5050 struct bfd_hash_entry *p;
5051 struct elf_link_hash_entry *h;
3e0882af
L
5052 bfd_size_type size;
5053 unsigned int alignment_power;
4070765b 5054 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5055
5056 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5057 {
5058 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5059 if (h->root.type == bfd_link_hash_warning)
5060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5061
3e0882af
L
5062 /* Preserve the maximum alignment and size for common
5063 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5064 since it can still be loaded at run time by another
3e0882af
L
5065 dynamic lib. */
5066 if (h->root.type == bfd_link_hash_common)
5067 {
5068 size = h->root.u.c.size;
5069 alignment_power = h->root.u.c.p->alignment_power;
5070 }
5071 else
5072 {
5073 size = 0;
5074 alignment_power = 0;
5075 }
4070765b 5076 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5077 will be exported when the dynamic lib becomes needed
5078 in the second pass. */
4070765b 5079 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5080 memcpy (p, old_ent, htab->root.table.entsize);
5081 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5082 h = (struct elf_link_hash_entry *) p;
5083 if (h->root.type == bfd_link_hash_warning)
5084 {
5085 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5086 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5088 }
a4542f1b 5089 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5090 {
5091 if (size > h->root.u.c.size)
5092 h->root.u.c.size = size;
5093 if (alignment_power > h->root.u.c.p->alignment_power)
5094 h->root.u.c.p->alignment_power = alignment_power;
5095 }
4070765b 5096 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5097 }
5098 }
5099
5061a885
AM
5100 /* Make a special call to the linker "notice" function to
5101 tell it that symbols added for crefs may need to be removed. */
e5034e59 5102 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5103 goto error_free_vers;
5061a885 5104
66eb6687
AM
5105 free (old_tab);
5106 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5107 alloc_mark);
5108 if (nondeflt_vers != NULL)
5109 free (nondeflt_vers);
5110 return TRUE;
5111 }
2de92251 5112
66eb6687
AM
5113 if (old_tab != NULL)
5114 {
e5034e59 5115 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5116 goto error_free_vers;
66eb6687
AM
5117 free (old_tab);
5118 old_tab = NULL;
5119 }
5120
c6e8a9a8
L
5121 /* Now that all the symbols from this input file are created, if
5122 not performing a relocatable link, handle .symver foo, foo@BAR
5123 such that any relocs against foo become foo@BAR. */
0e1862bb 5124 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5125 {
ef53be89 5126 size_t cnt, symidx;
4ad4eba5
AM
5127
5128 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5129 {
5130 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5131 char *shortname, *p;
5132
5133 p = strchr (h->root.root.string, ELF_VER_CHR);
5134 if (p == NULL
5135 || (h->root.type != bfd_link_hash_defined
5136 && h->root.type != bfd_link_hash_defweak))
5137 continue;
5138
5139 amt = p - h->root.root.string;
a50b1753 5140 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5141 if (!shortname)
5142 goto error_free_vers;
4ad4eba5
AM
5143 memcpy (shortname, h->root.root.string, amt);
5144 shortname[amt] = '\0';
5145
5146 hi = (struct elf_link_hash_entry *)
66eb6687 5147 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5148 FALSE, FALSE, FALSE);
5149 if (hi != NULL
5150 && hi->root.type == h->root.type
5151 && hi->root.u.def.value == h->root.u.def.value
5152 && hi->root.u.def.section == h->root.u.def.section)
5153 {
5154 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5155 hi->root.type = bfd_link_hash_indirect;
5156 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5157 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5158 sym_hash = elf_sym_hashes (abfd);
5159 if (sym_hash)
5160 for (symidx = 0; symidx < extsymcount; ++symidx)
5161 if (sym_hash[symidx] == hi)
5162 {
5163 sym_hash[symidx] = h;
5164 break;
5165 }
5166 }
5167 free (shortname);
5168 }
5169 free (nondeflt_vers);
5170 nondeflt_vers = NULL;
5171 }
5172
60d67dc8 5173 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5174 symbols we found. The only way to do this is to search all the
5175 symbols. Since we only need the information for non functions in
5176 dynamic objects, that's the only time we actually put anything on
5177 the list WEAKS. We need this information so that if a regular
5178 object refers to a symbol defined weakly in a dynamic object, the
5179 real symbol in the dynamic object is also put in the dynamic
5180 symbols; we also must arrange for both symbols to point to the
5181 same memory location. We could handle the general case of symbol
5182 aliasing, but a general symbol alias can only be generated in
5183 assembler code, handling it correctly would be very time
5184 consuming, and other ELF linkers don't handle general aliasing
5185 either. */
5186 if (weaks != NULL)
5187 {
5188 struct elf_link_hash_entry **hpp;
5189 struct elf_link_hash_entry **hppend;
5190 struct elf_link_hash_entry **sorted_sym_hash;
5191 struct elf_link_hash_entry *h;
5192 size_t sym_count;
5193
5194 /* Since we have to search the whole symbol list for each weak
5195 defined symbol, search time for N weak defined symbols will be
5196 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5197 amt = extsymcount;
5198 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5199 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5200 if (sorted_sym_hash == NULL)
5201 goto error_return;
5202 sym_hash = sorted_sym_hash;
5203 hpp = elf_sym_hashes (abfd);
5204 hppend = hpp + extsymcount;
5205 sym_count = 0;
5206 for (; hpp < hppend; hpp++)
5207 {
5208 h = *hpp;
5209 if (h != NULL
5210 && h->root.type == bfd_link_hash_defined
fcb93ecf 5211 && !bed->is_function_type (h->type))
4ad4eba5
AM
5212 {
5213 *sym_hash = h;
5214 sym_hash++;
5215 sym_count++;
5216 }
5217 }
5218
5219 qsort (sorted_sym_hash, sym_count,
5220 sizeof (struct elf_link_hash_entry *),
5221 elf_sort_symbol);
5222
5223 while (weaks != NULL)
5224 {
5225 struct elf_link_hash_entry *hlook;
5226 asection *slook;
5227 bfd_vma vlook;
ed54588d 5228 size_t i, j, idx = 0;
4ad4eba5
AM
5229
5230 hlook = weaks;
60d67dc8
AM
5231 weaks = hlook->u.alias;
5232 hlook->u.alias = NULL;
4ad4eba5 5233
e3e53eed
AM
5234 if (hlook->root.type != bfd_link_hash_defined
5235 && hlook->root.type != bfd_link_hash_defweak)
5236 continue;
5237
4ad4eba5
AM
5238 slook = hlook->root.u.def.section;
5239 vlook = hlook->root.u.def.value;
5240
4ad4eba5
AM
5241 i = 0;
5242 j = sym_count;
14160578 5243 while (i != j)
4ad4eba5
AM
5244 {
5245 bfd_signed_vma vdiff;
5246 idx = (i + j) / 2;
14160578 5247 h = sorted_sym_hash[idx];
4ad4eba5
AM
5248 vdiff = vlook - h->root.u.def.value;
5249 if (vdiff < 0)
5250 j = idx;
5251 else if (vdiff > 0)
5252 i = idx + 1;
5253 else
5254 {
d3435ae8 5255 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5256 if (sdiff < 0)
5257 j = idx;
5258 else if (sdiff > 0)
5259 i = idx + 1;
5260 else
14160578 5261 break;
4ad4eba5
AM
5262 }
5263 }
5264
5265 /* We didn't find a value/section match. */
14160578 5266 if (i == j)
4ad4eba5
AM
5267 continue;
5268
14160578
AM
5269 /* With multiple aliases, or when the weak symbol is already
5270 strongly defined, we have multiple matching symbols and
5271 the binary search above may land on any of them. Step
5272 one past the matching symbol(s). */
5273 while (++idx != j)
5274 {
5275 h = sorted_sym_hash[idx];
5276 if (h->root.u.def.section != slook
5277 || h->root.u.def.value != vlook)
5278 break;
5279 }
5280
5281 /* Now look back over the aliases. Since we sorted by size
5282 as well as value and section, we'll choose the one with
5283 the largest size. */
5284 while (idx-- != i)
4ad4eba5 5285 {
14160578 5286 h = sorted_sym_hash[idx];
4ad4eba5
AM
5287
5288 /* Stop if value or section doesn't match. */
14160578
AM
5289 if (h->root.u.def.section != slook
5290 || h->root.u.def.value != vlook)
4ad4eba5
AM
5291 break;
5292 else if (h != hlook)
5293 {
60d67dc8
AM
5294 struct elf_link_hash_entry *t;
5295
5296 hlook->u.alias = h;
5297 hlook->is_weakalias = 1;
5298 t = h;
5299 if (t->u.alias != NULL)
5300 while (t->u.alias != h)
5301 t = t->u.alias;
5302 t->u.alias = hlook;
4ad4eba5
AM
5303
5304 /* If the weak definition is in the list of dynamic
5305 symbols, make sure the real definition is put
5306 there as well. */
5307 if (hlook->dynindx != -1 && h->dynindx == -1)
5308 {
c152c796 5309 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5310 {
5311 err_free_sym_hash:
5312 free (sorted_sym_hash);
5313 goto error_return;
5314 }
4ad4eba5
AM
5315 }
5316
5317 /* If the real definition is in the list of dynamic
5318 symbols, make sure the weak definition is put
5319 there as well. If we don't do this, then the
5320 dynamic loader might not merge the entries for the
5321 real definition and the weak definition. */
5322 if (h->dynindx != -1 && hlook->dynindx == -1)
5323 {
c152c796 5324 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5325 goto err_free_sym_hash;
4ad4eba5
AM
5326 }
5327 break;
5328 }
5329 }
5330 }
5331
5332 free (sorted_sym_hash);
5333 }
5334
33177bb1
AM
5335 if (bed->check_directives
5336 && !(*bed->check_directives) (abfd, info))
5337 return FALSE;
85fbca6a 5338
4ad4eba5
AM
5339 /* If this is a non-traditional link, try to optimize the handling
5340 of the .stab/.stabstr sections. */
5341 if (! dynamic
5342 && ! info->traditional_format
66eb6687 5343 && is_elf_hash_table (htab)
4ad4eba5
AM
5344 && (info->strip != strip_all && info->strip != strip_debugger))
5345 {
5346 asection *stabstr;
5347
5348 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5349 if (stabstr != NULL)
5350 {
5351 bfd_size_type string_offset = 0;
5352 asection *stab;
5353
5354 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5355 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5356 && (!stab->name[5] ||
5357 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5358 && (stab->flags & SEC_MERGE) == 0
5359 && !bfd_is_abs_section (stab->output_section))
5360 {
5361 struct bfd_elf_section_data *secdata;
5362
5363 secdata = elf_section_data (stab);
66eb6687
AM
5364 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5365 stabstr, &secdata->sec_info,
4ad4eba5
AM
5366 &string_offset))
5367 goto error_return;
5368 if (secdata->sec_info)
dbaa2011 5369 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5370 }
5371 }
5372 }
5373
66eb6687 5374 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5375 {
5376 /* Add this bfd to the loaded list. */
5377 struct elf_link_loaded_list *n;
5378
ca4be51c 5379 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5380 if (n == NULL)
5381 goto error_return;
5382 n->abfd = abfd;
66eb6687
AM
5383 n->next = htab->loaded;
5384 htab->loaded = n;
4ad4eba5
AM
5385 }
5386
5387 return TRUE;
5388
5389 error_free_vers:
66eb6687
AM
5390 if (old_tab != NULL)
5391 free (old_tab);
5b677558
AM
5392 if (old_strtab != NULL)
5393 free (old_strtab);
4ad4eba5
AM
5394 if (nondeflt_vers != NULL)
5395 free (nondeflt_vers);
5396 if (extversym != NULL)
5397 free (extversym);
5398 error_free_sym:
5399 if (isymbuf != NULL)
5400 free (isymbuf);
5401 error_return:
5402 return FALSE;
5403}
5404
8387904d
AM
5405/* Return the linker hash table entry of a symbol that might be
5406 satisfied by an archive symbol. Return -1 on error. */
5407
5408struct elf_link_hash_entry *
5409_bfd_elf_archive_symbol_lookup (bfd *abfd,
5410 struct bfd_link_info *info,
5411 const char *name)
5412{
5413 struct elf_link_hash_entry *h;
5414 char *p, *copy;
5415 size_t len, first;
5416
2a41f396 5417 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5418 if (h != NULL)
5419 return h;
5420
5421 /* If this is a default version (the name contains @@), look up the
5422 symbol again with only one `@' as well as without the version.
5423 The effect is that references to the symbol with and without the
5424 version will be matched by the default symbol in the archive. */
5425
5426 p = strchr (name, ELF_VER_CHR);
5427 if (p == NULL || p[1] != ELF_VER_CHR)
5428 return h;
5429
5430 /* First check with only one `@'. */
5431 len = strlen (name);
a50b1753 5432 copy = (char *) bfd_alloc (abfd, len);
8387904d 5433 if (copy == NULL)
e99955cd 5434 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5435
5436 first = p - name + 1;
5437 memcpy (copy, name, first);
5438 memcpy (copy + first, name + first + 1, len - first);
5439
2a41f396 5440 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5441 if (h == NULL)
5442 {
5443 /* We also need to check references to the symbol without the
5444 version. */
5445 copy[first - 1] = '\0';
5446 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5447 FALSE, FALSE, TRUE);
8387904d
AM
5448 }
5449
5450 bfd_release (abfd, copy);
5451 return h;
5452}
5453
0ad989f9 5454/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5455 don't use _bfd_generic_link_add_archive_symbols because we need to
5456 handle versioned symbols.
0ad989f9
L
5457
5458 Fortunately, ELF archive handling is simpler than that done by
5459 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5460 oddities. In ELF, if we find a symbol in the archive map, and the
5461 symbol is currently undefined, we know that we must pull in that
5462 object file.
5463
5464 Unfortunately, we do have to make multiple passes over the symbol
5465 table until nothing further is resolved. */
5466
4ad4eba5
AM
5467static bfd_boolean
5468elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5469{
5470 symindex c;
13e570f8 5471 unsigned char *included = NULL;
0ad989f9
L
5472 carsym *symdefs;
5473 bfd_boolean loop;
5474 bfd_size_type amt;
8387904d
AM
5475 const struct elf_backend_data *bed;
5476 struct elf_link_hash_entry * (*archive_symbol_lookup)
5477 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5478
5479 if (! bfd_has_map (abfd))
5480 {
5481 /* An empty archive is a special case. */
5482 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5483 return TRUE;
5484 bfd_set_error (bfd_error_no_armap);
5485 return FALSE;
5486 }
5487
5488 /* Keep track of all symbols we know to be already defined, and all
5489 files we know to be already included. This is to speed up the
5490 second and subsequent passes. */
5491 c = bfd_ardata (abfd)->symdef_count;
5492 if (c == 0)
5493 return TRUE;
5494 amt = c;
13e570f8
AM
5495 amt *= sizeof (*included);
5496 included = (unsigned char *) bfd_zmalloc (amt);
5497 if (included == NULL)
5498 return FALSE;
0ad989f9
L
5499
5500 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5501 bed = get_elf_backend_data (abfd);
5502 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5503
5504 do
5505 {
5506 file_ptr last;
5507 symindex i;
5508 carsym *symdef;
5509 carsym *symdefend;
5510
5511 loop = FALSE;
5512 last = -1;
5513
5514 symdef = symdefs;
5515 symdefend = symdef + c;
5516 for (i = 0; symdef < symdefend; symdef++, i++)
5517 {
5518 struct elf_link_hash_entry *h;
5519 bfd *element;
5520 struct bfd_link_hash_entry *undefs_tail;
5521 symindex mark;
5522
13e570f8 5523 if (included[i])
0ad989f9
L
5524 continue;
5525 if (symdef->file_offset == last)
5526 {
5527 included[i] = TRUE;
5528 continue;
5529 }
5530
8387904d 5531 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5532 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5533 goto error_return;
0ad989f9
L
5534
5535 if (h == NULL)
5536 continue;
5537
5538 if (h->root.type == bfd_link_hash_common)
5539 {
5540 /* We currently have a common symbol. The archive map contains
5541 a reference to this symbol, so we may want to include it. We
5542 only want to include it however, if this archive element
5543 contains a definition of the symbol, not just another common
5544 declaration of it.
5545
5546 Unfortunately some archivers (including GNU ar) will put
5547 declarations of common symbols into their archive maps, as
5548 well as real definitions, so we cannot just go by the archive
5549 map alone. Instead we must read in the element's symbol
5550 table and check that to see what kind of symbol definition
5551 this is. */
5552 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5553 continue;
5554 }
5555 else if (h->root.type != bfd_link_hash_undefined)
5556 {
5557 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5558 /* Symbol must be defined. Don't check it again. */
5559 included[i] = TRUE;
0ad989f9
L
5560 continue;
5561 }
5562
5563 /* We need to include this archive member. */
5564 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5565 if (element == NULL)
5566 goto error_return;
5567
5568 if (! bfd_check_format (element, bfd_object))
5569 goto error_return;
5570
0ad989f9
L
5571 undefs_tail = info->hash->undefs_tail;
5572
0e144ba7
AM
5573 if (!(*info->callbacks
5574 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5575 continue;
0e144ba7 5576 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5577 goto error_return;
5578
5579 /* If there are any new undefined symbols, we need to make
5580 another pass through the archive in order to see whether
5581 they can be defined. FIXME: This isn't perfect, because
5582 common symbols wind up on undefs_tail and because an
5583 undefined symbol which is defined later on in this pass
5584 does not require another pass. This isn't a bug, but it
5585 does make the code less efficient than it could be. */
5586 if (undefs_tail != info->hash->undefs_tail)
5587 loop = TRUE;
5588
5589 /* Look backward to mark all symbols from this object file
5590 which we have already seen in this pass. */
5591 mark = i;
5592 do
5593 {
5594 included[mark] = TRUE;
5595 if (mark == 0)
5596 break;
5597 --mark;
5598 }
5599 while (symdefs[mark].file_offset == symdef->file_offset);
5600
5601 /* We mark subsequent symbols from this object file as we go
5602 on through the loop. */
5603 last = symdef->file_offset;
5604 }
5605 }
5606 while (loop);
5607
0ad989f9
L
5608 free (included);
5609
5610 return TRUE;
5611
5612 error_return:
0ad989f9
L
5613 if (included != NULL)
5614 free (included);
5615 return FALSE;
5616}
4ad4eba5
AM
5617
5618/* Given an ELF BFD, add symbols to the global hash table as
5619 appropriate. */
5620
5621bfd_boolean
5622bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5623{
5624 switch (bfd_get_format (abfd))
5625 {
5626 case bfd_object:
5627 return elf_link_add_object_symbols (abfd, info);
5628 case bfd_archive:
5629 return elf_link_add_archive_symbols (abfd, info);
5630 default:
5631 bfd_set_error (bfd_error_wrong_format);
5632 return FALSE;
5633 }
5634}
5a580b3a 5635\f
14b1c01e
AM
5636struct hash_codes_info
5637{
5638 unsigned long *hashcodes;
5639 bfd_boolean error;
5640};
a0c8462f 5641
5a580b3a
AM
5642/* This function will be called though elf_link_hash_traverse to store
5643 all hash value of the exported symbols in an array. */
5644
5645static bfd_boolean
5646elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5647{
a50b1753 5648 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5649 const char *name;
5a580b3a
AM
5650 unsigned long ha;
5651 char *alc = NULL;
5652
5a580b3a
AM
5653 /* Ignore indirect symbols. These are added by the versioning code. */
5654 if (h->dynindx == -1)
5655 return TRUE;
5656
5657 name = h->root.root.string;
422f1182 5658 if (h->versioned >= versioned)
5a580b3a 5659 {
422f1182
L
5660 char *p = strchr (name, ELF_VER_CHR);
5661 if (p != NULL)
14b1c01e 5662 {
422f1182
L
5663 alc = (char *) bfd_malloc (p - name + 1);
5664 if (alc == NULL)
5665 {
5666 inf->error = TRUE;
5667 return FALSE;
5668 }
5669 memcpy (alc, name, p - name);
5670 alc[p - name] = '\0';
5671 name = alc;
14b1c01e 5672 }
5a580b3a
AM
5673 }
5674
5675 /* Compute the hash value. */
5676 ha = bfd_elf_hash (name);
5677
5678 /* Store the found hash value in the array given as the argument. */
14b1c01e 5679 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5680
5681 /* And store it in the struct so that we can put it in the hash table
5682 later. */
f6e332e6 5683 h->u.elf_hash_value = ha;
5a580b3a
AM
5684
5685 if (alc != NULL)
5686 free (alc);
5687
5688 return TRUE;
5689}
5690
fdc90cb4
JJ
5691struct collect_gnu_hash_codes
5692{
5693 bfd *output_bfd;
5694 const struct elf_backend_data *bed;
5695 unsigned long int nsyms;
5696 unsigned long int maskbits;
5697 unsigned long int *hashcodes;
5698 unsigned long int *hashval;
5699 unsigned long int *indx;
5700 unsigned long int *counts;
5701 bfd_vma *bitmask;
5702 bfd_byte *contents;
5703 long int min_dynindx;
5704 unsigned long int bucketcount;
5705 unsigned long int symindx;
5706 long int local_indx;
5707 long int shift1, shift2;
5708 unsigned long int mask;
14b1c01e 5709 bfd_boolean error;
fdc90cb4
JJ
5710};
5711
5712/* This function will be called though elf_link_hash_traverse to store
5713 all hash value of the exported symbols in an array. */
5714
5715static bfd_boolean
5716elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5717{
a50b1753 5718 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5719 const char *name;
fdc90cb4
JJ
5720 unsigned long ha;
5721 char *alc = NULL;
5722
fdc90cb4
JJ
5723 /* Ignore indirect symbols. These are added by the versioning code. */
5724 if (h->dynindx == -1)
5725 return TRUE;
5726
5727 /* Ignore also local symbols and undefined symbols. */
5728 if (! (*s->bed->elf_hash_symbol) (h))
5729 return TRUE;
5730
5731 name = h->root.root.string;
422f1182 5732 if (h->versioned >= versioned)
fdc90cb4 5733 {
422f1182
L
5734 char *p = strchr (name, ELF_VER_CHR);
5735 if (p != NULL)
14b1c01e 5736 {
422f1182
L
5737 alc = (char *) bfd_malloc (p - name + 1);
5738 if (alc == NULL)
5739 {
5740 s->error = TRUE;
5741 return FALSE;
5742 }
5743 memcpy (alc, name, p - name);
5744 alc[p - name] = '\0';
5745 name = alc;
14b1c01e 5746 }
fdc90cb4
JJ
5747 }
5748
5749 /* Compute the hash value. */
5750 ha = bfd_elf_gnu_hash (name);
5751
5752 /* Store the found hash value in the array for compute_bucket_count,
5753 and also for .dynsym reordering purposes. */
5754 s->hashcodes[s->nsyms] = ha;
5755 s->hashval[h->dynindx] = ha;
5756 ++s->nsyms;
5757 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5758 s->min_dynindx = h->dynindx;
5759
5760 if (alc != NULL)
5761 free (alc);
5762
5763 return TRUE;
5764}
5765
5766/* This function will be called though elf_link_hash_traverse to do
5767 final dynaminc symbol renumbering. */
5768
5769static bfd_boolean
5770elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5771{
a50b1753 5772 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5773 unsigned long int bucket;
5774 unsigned long int val;
5775
fdc90cb4
JJ
5776 /* Ignore indirect symbols. */
5777 if (h->dynindx == -1)
5778 return TRUE;
5779
5780 /* Ignore also local symbols and undefined symbols. */
5781 if (! (*s->bed->elf_hash_symbol) (h))
5782 {
5783 if (h->dynindx >= s->min_dynindx)
5784 h->dynindx = s->local_indx++;
5785 return TRUE;
5786 }
5787
5788 bucket = s->hashval[h->dynindx] % s->bucketcount;
5789 val = (s->hashval[h->dynindx] >> s->shift1)
5790 & ((s->maskbits >> s->shift1) - 1);
5791 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5792 s->bitmask[val]
5793 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5794 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5795 if (s->counts[bucket] == 1)
5796 /* Last element terminates the chain. */
5797 val |= 1;
5798 bfd_put_32 (s->output_bfd, val,
5799 s->contents + (s->indx[bucket] - s->symindx) * 4);
5800 --s->counts[bucket];
5801 h->dynindx = s->indx[bucket]++;
5802 return TRUE;
5803}
5804
5805/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5806
5807bfd_boolean
5808_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5809{
5810 return !(h->forced_local
5811 || h->root.type == bfd_link_hash_undefined
5812 || h->root.type == bfd_link_hash_undefweak
5813 || ((h->root.type == bfd_link_hash_defined
5814 || h->root.type == bfd_link_hash_defweak)
5815 && h->root.u.def.section->output_section == NULL));
5816}
5817
5a580b3a
AM
5818/* Array used to determine the number of hash table buckets to use
5819 based on the number of symbols there are. If there are fewer than
5820 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5821 fewer than 37 we use 17 buckets, and so forth. We never use more
5822 than 32771 buckets. */
5823
5824static const size_t elf_buckets[] =
5825{
5826 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5827 16411, 32771, 0
5828};
5829
5830/* Compute bucket count for hashing table. We do not use a static set
5831 of possible tables sizes anymore. Instead we determine for all
5832 possible reasonable sizes of the table the outcome (i.e., the
5833 number of collisions etc) and choose the best solution. The
5834 weighting functions are not too simple to allow the table to grow
5835 without bounds. Instead one of the weighting factors is the size.
5836 Therefore the result is always a good payoff between few collisions
5837 (= short chain lengths) and table size. */
5838static size_t
b20dd2ce 5839compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5840 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5841 unsigned long int nsyms,
5842 int gnu_hash)
5a580b3a 5843{
5a580b3a 5844 size_t best_size = 0;
5a580b3a 5845 unsigned long int i;
5a580b3a 5846
5a580b3a
AM
5847 /* We have a problem here. The following code to optimize the table
5848 size requires an integer type with more the 32 bits. If
5849 BFD_HOST_U_64_BIT is set we know about such a type. */
5850#ifdef BFD_HOST_U_64_BIT
5851 if (info->optimize)
5852 {
5a580b3a
AM
5853 size_t minsize;
5854 size_t maxsize;
5855 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5856 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5857 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5858 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5859 unsigned long int *counts;
d40f3da9 5860 bfd_size_type amt;
0883b6e0 5861 unsigned int no_improvement_count = 0;
5a580b3a
AM
5862
5863 /* Possible optimization parameters: if we have NSYMS symbols we say
5864 that the hashing table must at least have NSYMS/4 and at most
5865 2*NSYMS buckets. */
5866 minsize = nsyms / 4;
5867 if (minsize == 0)
5868 minsize = 1;
5869 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5870 if (gnu_hash)
5871 {
5872 if (minsize < 2)
5873 minsize = 2;
5874 if ((best_size & 31) == 0)
5875 ++best_size;
5876 }
5a580b3a
AM
5877
5878 /* Create array where we count the collisions in. We must use bfd_malloc
5879 since the size could be large. */
5880 amt = maxsize;
5881 amt *= sizeof (unsigned long int);
a50b1753 5882 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5883 if (counts == NULL)
fdc90cb4 5884 return 0;
5a580b3a
AM
5885
5886 /* Compute the "optimal" size for the hash table. The criteria is a
5887 minimal chain length. The minor criteria is (of course) the size
5888 of the table. */
5889 for (i = minsize; i < maxsize; ++i)
5890 {
5891 /* Walk through the array of hashcodes and count the collisions. */
5892 BFD_HOST_U_64_BIT max;
5893 unsigned long int j;
5894 unsigned long int fact;
5895
fdc90cb4
JJ
5896 if (gnu_hash && (i & 31) == 0)
5897 continue;
5898
5a580b3a
AM
5899 memset (counts, '\0', i * sizeof (unsigned long int));
5900
5901 /* Determine how often each hash bucket is used. */
5902 for (j = 0; j < nsyms; ++j)
5903 ++counts[hashcodes[j] % i];
5904
5905 /* For the weight function we need some information about the
5906 pagesize on the target. This is information need not be 100%
5907 accurate. Since this information is not available (so far) we
5908 define it here to a reasonable default value. If it is crucial
5909 to have a better value some day simply define this value. */
5910# ifndef BFD_TARGET_PAGESIZE
5911# define BFD_TARGET_PAGESIZE (4096)
5912# endif
5913
fdc90cb4
JJ
5914 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5915 and the chains. */
5916 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5917
5918# if 1
5919 /* Variant 1: optimize for short chains. We add the squares
5920 of all the chain lengths (which favors many small chain
5921 over a few long chains). */
5922 for (j = 0; j < i; ++j)
5923 max += counts[j] * counts[j];
5924
5925 /* This adds penalties for the overall size of the table. */
fdc90cb4 5926 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5927 max *= fact * fact;
5928# else
5929 /* Variant 2: Optimize a lot more for small table. Here we
5930 also add squares of the size but we also add penalties for
5931 empty slots (the +1 term). */
5932 for (j = 0; j < i; ++j)
5933 max += (1 + counts[j]) * (1 + counts[j]);
5934
5935 /* The overall size of the table is considered, but not as
5936 strong as in variant 1, where it is squared. */
fdc90cb4 5937 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5938 max *= fact;
5939# endif
5940
5941 /* Compare with current best results. */
5942 if (max < best_chlen)
5943 {
5944 best_chlen = max;
5945 best_size = i;
ca4be51c 5946 no_improvement_count = 0;
5a580b3a 5947 }
0883b6e0
NC
5948 /* PR 11843: Avoid futile long searches for the best bucket size
5949 when there are a large number of symbols. */
5950 else if (++no_improvement_count == 100)
5951 break;
5a580b3a
AM
5952 }
5953
5954 free (counts);
5955 }
5956 else
5957#endif /* defined (BFD_HOST_U_64_BIT) */
5958 {
5959 /* This is the fallback solution if no 64bit type is available or if we
5960 are not supposed to spend much time on optimizations. We select the
5961 bucket count using a fixed set of numbers. */
5962 for (i = 0; elf_buckets[i] != 0; i++)
5963 {
5964 best_size = elf_buckets[i];
fdc90cb4 5965 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5966 break;
5967 }
fdc90cb4
JJ
5968 if (gnu_hash && best_size < 2)
5969 best_size = 2;
5a580b3a
AM
5970 }
5971
5a580b3a
AM
5972 return best_size;
5973}
5974
d0bf826b
AM
5975/* Size any SHT_GROUP section for ld -r. */
5976
5977bfd_boolean
5978_bfd_elf_size_group_sections (struct bfd_link_info *info)
5979{
5980 bfd *ibfd;
57963c05 5981 asection *s;
d0bf826b 5982
c72f2fb2 5983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 5984 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
5985 && (s = ibfd->sections) != NULL
5986 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
5987 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5988 return FALSE;
5989 return TRUE;
5990}
5991
04c3a755
NS
5992/* Set a default stack segment size. The value in INFO wins. If it
5993 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5994 undefined it is initialized. */
5995
5996bfd_boolean
5997bfd_elf_stack_segment_size (bfd *output_bfd,
5998 struct bfd_link_info *info,
5999 const char *legacy_symbol,
6000 bfd_vma default_size)
6001{
6002 struct elf_link_hash_entry *h = NULL;
6003
6004 /* Look for legacy symbol. */
6005 if (legacy_symbol)
6006 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6007 FALSE, FALSE, FALSE);
6008 if (h && (h->root.type == bfd_link_hash_defined
6009 || h->root.type == bfd_link_hash_defweak)
6010 && h->def_regular
6011 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6012 {
6013 /* The symbol has no type if specified on the command line. */
6014 h->type = STT_OBJECT;
6015 if (info->stacksize)
695344c0 6016 /* xgettext:c-format */
871b3ab2 6017 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6018 output_bfd, legacy_symbol);
04c3a755 6019 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6020 /* xgettext:c-format */
871b3ab2 6021 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6022 output_bfd, legacy_symbol);
04c3a755
NS
6023 else
6024 info->stacksize = h->root.u.def.value;
6025 }
6026
6027 if (!info->stacksize)
6028 /* If the user didn't set a size, or explicitly inhibit the
6029 size, set it now. */
6030 info->stacksize = default_size;
6031
6032 /* Provide the legacy symbol, if it is referenced. */
6033 if (h && (h->root.type == bfd_link_hash_undefined
6034 || h->root.type == bfd_link_hash_undefweak))
6035 {
6036 struct bfd_link_hash_entry *bh = NULL;
6037
6038 if (!(_bfd_generic_link_add_one_symbol
6039 (info, output_bfd, legacy_symbol,
6040 BSF_GLOBAL, bfd_abs_section_ptr,
6041 info->stacksize >= 0 ? info->stacksize : 0,
6042 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6043 return FALSE;
6044
6045 h = (struct elf_link_hash_entry *) bh;
6046 h->def_regular = 1;
6047 h->type = STT_OBJECT;
6048 }
6049
6050 return TRUE;
6051}
6052
b531344c
MR
6053/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6054
6055struct elf_gc_sweep_symbol_info
6056{
6057 struct bfd_link_info *info;
6058 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6059 bfd_boolean);
6060};
6061
6062static bfd_boolean
6063elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6064{
6065 if (!h->mark
6066 && (((h->root.type == bfd_link_hash_defined
6067 || h->root.type == bfd_link_hash_defweak)
6068 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6069 && h->root.u.def.section->gc_mark))
6070 || h->root.type == bfd_link_hash_undefined
6071 || h->root.type == bfd_link_hash_undefweak))
6072 {
6073 struct elf_gc_sweep_symbol_info *inf;
6074
6075 inf = (struct elf_gc_sweep_symbol_info *) data;
6076 (*inf->hide_symbol) (inf->info, h, TRUE);
6077 h->def_regular = 0;
6078 h->ref_regular = 0;
6079 h->ref_regular_nonweak = 0;
6080 }
6081
6082 return TRUE;
6083}
6084
5a580b3a
AM
6085/* Set up the sizes and contents of the ELF dynamic sections. This is
6086 called by the ELF linker emulation before_allocation routine. We
6087 must set the sizes of the sections before the linker sets the
6088 addresses of the various sections. */
6089
6090bfd_boolean
6091bfd_elf_size_dynamic_sections (bfd *output_bfd,
6092 const char *soname,
6093 const char *rpath,
6094 const char *filter_shlib,
7ee314fa
AM
6095 const char *audit,
6096 const char *depaudit,
5a580b3a
AM
6097 const char * const *auxiliary_filters,
6098 struct bfd_link_info *info,
fd91d419 6099 asection **sinterpptr)
5a580b3a 6100{
5a580b3a
AM
6101 bfd *dynobj;
6102 const struct elf_backend_data *bed;
5a580b3a
AM
6103
6104 *sinterpptr = NULL;
6105
5a580b3a
AM
6106 if (!is_elf_hash_table (info->hash))
6107 return TRUE;
6108
5a580b3a
AM
6109 dynobj = elf_hash_table (info)->dynobj;
6110
9a2a56cc 6111 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6112 {
902e9fc7
MR
6113 struct bfd_elf_version_tree *verdefs;
6114 struct elf_info_failed asvinfo;
5a580b3a
AM
6115 struct bfd_elf_version_tree *t;
6116 struct bfd_elf_version_expr *d;
902e9fc7 6117 asection *s;
e6699019 6118 size_t soname_indx;
7ee314fa 6119
5a580b3a
AM
6120 /* If we are supposed to export all symbols into the dynamic symbol
6121 table (this is not the normal case), then do so. */
55255dae 6122 if (info->export_dynamic
0e1862bb 6123 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6124 {
3d13f3e9
AM
6125 struct elf_info_failed eif;
6126
6127 eif.info = info;
6128 eif.failed = FALSE;
5a580b3a
AM
6129 elf_link_hash_traverse (elf_hash_table (info),
6130 _bfd_elf_export_symbol,
6131 &eif);
6132 if (eif.failed)
6133 return FALSE;
6134 }
6135
e6699019
L
6136 if (soname != NULL)
6137 {
6138 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6139 soname, TRUE);
6140 if (soname_indx == (size_t) -1
6141 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6142 return FALSE;
6143 }
6144 else
6145 soname_indx = (size_t) -1;
6146
5a580b3a 6147 /* Make all global versions with definition. */
fd91d419 6148 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6149 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6150 if (!d->symver && d->literal)
5a580b3a
AM
6151 {
6152 const char *verstr, *name;
6153 size_t namelen, verlen, newlen;
93252b1c 6154 char *newname, *p, leading_char;
5a580b3a
AM
6155 struct elf_link_hash_entry *newh;
6156
93252b1c 6157 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6158 name = d->pattern;
93252b1c 6159 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6160 verstr = t->name;
6161 verlen = strlen (verstr);
6162 newlen = namelen + verlen + 3;
6163
a50b1753 6164 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6165 if (newname == NULL)
6166 return FALSE;
93252b1c
MF
6167 newname[0] = leading_char;
6168 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6169
6170 /* Check the hidden versioned definition. */
6171 p = newname + namelen;
6172 *p++ = ELF_VER_CHR;
6173 memcpy (p, verstr, verlen + 1);
6174 newh = elf_link_hash_lookup (elf_hash_table (info),
6175 newname, FALSE, FALSE,
6176 FALSE);
6177 if (newh == NULL
6178 || (newh->root.type != bfd_link_hash_defined
6179 && newh->root.type != bfd_link_hash_defweak))
6180 {
6181 /* Check the default versioned definition. */
6182 *p++ = ELF_VER_CHR;
6183 memcpy (p, verstr, verlen + 1);
6184 newh = elf_link_hash_lookup (elf_hash_table (info),
6185 newname, FALSE, FALSE,
6186 FALSE);
6187 }
6188 free (newname);
6189
6190 /* Mark this version if there is a definition and it is
6191 not defined in a shared object. */
6192 if (newh != NULL
f5385ebf 6193 && !newh->def_dynamic
5a580b3a
AM
6194 && (newh->root.type == bfd_link_hash_defined
6195 || newh->root.type == bfd_link_hash_defweak))
6196 d->symver = 1;
6197 }
6198
6199 /* Attach all the symbols to their version information. */
5a580b3a 6200 asvinfo.info = info;
5a580b3a
AM
6201 asvinfo.failed = FALSE;
6202
6203 elf_link_hash_traverse (elf_hash_table (info),
6204 _bfd_elf_link_assign_sym_version,
6205 &asvinfo);
6206 if (asvinfo.failed)
6207 return FALSE;
6208
6209 if (!info->allow_undefined_version)
6210 {
6211 /* Check if all global versions have a definition. */
3d13f3e9 6212 bfd_boolean all_defined = TRUE;
fd91d419 6213 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6214 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6215 if (d->literal && !d->symver && !d->script)
5a580b3a 6216 {
4eca0228 6217 _bfd_error_handler
5a580b3a
AM
6218 (_("%s: undefined version: %s"),
6219 d->pattern, t->name);
6220 all_defined = FALSE;
6221 }
6222
6223 if (!all_defined)
6224 {
6225 bfd_set_error (bfd_error_bad_value);
6226 return FALSE;
6227 }
6228 }
6229
902e9fc7
MR
6230 /* Set up the version definition section. */
6231 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6232 BFD_ASSERT (s != NULL);
5a580b3a 6233
902e9fc7
MR
6234 /* We may have created additional version definitions if we are
6235 just linking a regular application. */
6236 verdefs = info->version_info;
5a580b3a 6237
902e9fc7
MR
6238 /* Skip anonymous version tag. */
6239 if (verdefs != NULL && verdefs->vernum == 0)
6240 verdefs = verdefs->next;
5a580b3a 6241
902e9fc7
MR
6242 if (verdefs == NULL && !info->create_default_symver)
6243 s->flags |= SEC_EXCLUDE;
6244 else
5a580b3a 6245 {
902e9fc7
MR
6246 unsigned int cdefs;
6247 bfd_size_type size;
6248 bfd_byte *p;
6249 Elf_Internal_Verdef def;
6250 Elf_Internal_Verdaux defaux;
6251 struct bfd_link_hash_entry *bh;
6252 struct elf_link_hash_entry *h;
6253 const char *name;
5a580b3a 6254
902e9fc7
MR
6255 cdefs = 0;
6256 size = 0;
5a580b3a 6257
902e9fc7
MR
6258 /* Make space for the base version. */
6259 size += sizeof (Elf_External_Verdef);
6260 size += sizeof (Elf_External_Verdaux);
6261 ++cdefs;
6262
6263 /* Make space for the default version. */
6264 if (info->create_default_symver)
6265 {
6266 size += sizeof (Elf_External_Verdef);
6267 ++cdefs;
3e3b46e5
PB
6268 }
6269
5a580b3a
AM
6270 for (t = verdefs; t != NULL; t = t->next)
6271 {
6272 struct bfd_elf_version_deps *n;
6273
a6cc6b3b
RO
6274 /* Don't emit base version twice. */
6275 if (t->vernum == 0)
6276 continue;
6277
5a580b3a
AM
6278 size += sizeof (Elf_External_Verdef);
6279 size += sizeof (Elf_External_Verdaux);
6280 ++cdefs;
6281
6282 for (n = t->deps; n != NULL; n = n->next)
6283 size += sizeof (Elf_External_Verdaux);
6284 }
6285
eea6121a 6286 s->size = size;
a50b1753 6287 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6288 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6289 return FALSE;
6290
6291 /* Fill in the version definition section. */
6292
6293 p = s->contents;
6294
6295 def.vd_version = VER_DEF_CURRENT;
6296 def.vd_flags = VER_FLG_BASE;
6297 def.vd_ndx = 1;
6298 def.vd_cnt = 1;
3e3b46e5
PB
6299 if (info->create_default_symver)
6300 {
6301 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6302 def.vd_next = sizeof (Elf_External_Verdef);
6303 }
6304 else
6305 {
6306 def.vd_aux = sizeof (Elf_External_Verdef);
6307 def.vd_next = (sizeof (Elf_External_Verdef)
6308 + sizeof (Elf_External_Verdaux));
6309 }
5a580b3a 6310
ef53be89 6311 if (soname_indx != (size_t) -1)
5a580b3a
AM
6312 {
6313 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6314 soname_indx);
6315 def.vd_hash = bfd_elf_hash (soname);
6316 defaux.vda_name = soname_indx;
3e3b46e5 6317 name = soname;
5a580b3a
AM
6318 }
6319 else
6320 {
ef53be89 6321 size_t indx;
5a580b3a 6322
06084812 6323 name = lbasename (output_bfd->filename);
5a580b3a
AM
6324 def.vd_hash = bfd_elf_hash (name);
6325 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6326 name, FALSE);
ef53be89 6327 if (indx == (size_t) -1)
5a580b3a
AM
6328 return FALSE;
6329 defaux.vda_name = indx;
6330 }
6331 defaux.vda_next = 0;
6332
6333 _bfd_elf_swap_verdef_out (output_bfd, &def,
6334 (Elf_External_Verdef *) p);
6335 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6336 if (info->create_default_symver)
6337 {
6338 /* Add a symbol representing this version. */
6339 bh = NULL;
6340 if (! (_bfd_generic_link_add_one_symbol
6341 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6342 0, NULL, FALSE,
6343 get_elf_backend_data (dynobj)->collect, &bh)))
6344 return FALSE;
6345 h = (struct elf_link_hash_entry *) bh;
6346 h->non_elf = 0;
6347 h->def_regular = 1;
6348 h->type = STT_OBJECT;
6349 h->verinfo.vertree = NULL;
6350
6351 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6352 return FALSE;
6353
6354 /* Create a duplicate of the base version with the same
6355 aux block, but different flags. */
6356 def.vd_flags = 0;
6357 def.vd_ndx = 2;
6358 def.vd_aux = sizeof (Elf_External_Verdef);
6359 if (verdefs)
6360 def.vd_next = (sizeof (Elf_External_Verdef)
6361 + sizeof (Elf_External_Verdaux));
6362 else
6363 def.vd_next = 0;
6364 _bfd_elf_swap_verdef_out (output_bfd, &def,
6365 (Elf_External_Verdef *) p);
6366 p += sizeof (Elf_External_Verdef);
6367 }
5a580b3a
AM
6368 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6369 (Elf_External_Verdaux *) p);
6370 p += sizeof (Elf_External_Verdaux);
6371
6372 for (t = verdefs; t != NULL; t = t->next)
6373 {
6374 unsigned int cdeps;
6375 struct bfd_elf_version_deps *n;
5a580b3a 6376
a6cc6b3b
RO
6377 /* Don't emit the base version twice. */
6378 if (t->vernum == 0)
6379 continue;
6380
5a580b3a
AM
6381 cdeps = 0;
6382 for (n = t->deps; n != NULL; n = n->next)
6383 ++cdeps;
6384
6385 /* Add a symbol representing this version. */
6386 bh = NULL;
6387 if (! (_bfd_generic_link_add_one_symbol
6388 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6389 0, NULL, FALSE,
6390 get_elf_backend_data (dynobj)->collect, &bh)))
6391 return FALSE;
6392 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6393 h->non_elf = 0;
6394 h->def_regular = 1;
5a580b3a
AM
6395 h->type = STT_OBJECT;
6396 h->verinfo.vertree = t;
6397
c152c796 6398 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6399 return FALSE;
6400
6401 def.vd_version = VER_DEF_CURRENT;
6402 def.vd_flags = 0;
6403 if (t->globals.list == NULL
6404 && t->locals.list == NULL
6405 && ! t->used)
6406 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6407 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6408 def.vd_cnt = cdeps + 1;
6409 def.vd_hash = bfd_elf_hash (t->name);
6410 def.vd_aux = sizeof (Elf_External_Verdef);
6411 def.vd_next = 0;
a6cc6b3b
RO
6412
6413 /* If a basever node is next, it *must* be the last node in
6414 the chain, otherwise Verdef construction breaks. */
6415 if (t->next != NULL && t->next->vernum == 0)
6416 BFD_ASSERT (t->next->next == NULL);
6417
6418 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6419 def.vd_next = (sizeof (Elf_External_Verdef)
6420 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6421
6422 _bfd_elf_swap_verdef_out (output_bfd, &def,
6423 (Elf_External_Verdef *) p);
6424 p += sizeof (Elf_External_Verdef);
6425
6426 defaux.vda_name = h->dynstr_index;
6427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6428 h->dynstr_index);
6429 defaux.vda_next = 0;
6430 if (t->deps != NULL)
6431 defaux.vda_next = sizeof (Elf_External_Verdaux);
6432 t->name_indx = defaux.vda_name;
6433
6434 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6435 (Elf_External_Verdaux *) p);
6436 p += sizeof (Elf_External_Verdaux);
6437
6438 for (n = t->deps; n != NULL; n = n->next)
6439 {
6440 if (n->version_needed == NULL)
6441 {
6442 /* This can happen if there was an error in the
6443 version script. */
6444 defaux.vda_name = 0;
6445 }
6446 else
6447 {
6448 defaux.vda_name = n->version_needed->name_indx;
6449 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6450 defaux.vda_name);
6451 }
6452 if (n->next == NULL)
6453 defaux.vda_next = 0;
6454 else
6455 defaux.vda_next = sizeof (Elf_External_Verdaux);
6456
6457 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6458 (Elf_External_Verdaux *) p);
6459 p += sizeof (Elf_External_Verdaux);
6460 }
6461 }
6462
5a580b3a
AM
6463 elf_tdata (output_bfd)->cverdefs = cdefs;
6464 }
902e9fc7
MR
6465 }
6466
6467 bed = get_elf_backend_data (output_bfd);
6468
6469 if (info->gc_sections && bed->can_gc_sections)
6470 {
6471 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6472
6473 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6474 dynamic symbol table. */
902e9fc7
MR
6475 sweep_info.info = info;
6476 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6477 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6478 &sweep_info);
3d13f3e9
AM
6479 }
6480
6481 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6482 {
6483 asection *s;
6484 struct elf_find_verdep_info sinfo;
6485
6486 /* Work out the size of the version reference section. */
6487
6488 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6489 BFD_ASSERT (s != NULL);
902e9fc7 6490
3d13f3e9
AM
6491 sinfo.info = info;
6492 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6493 if (sinfo.vers == 0)
6494 sinfo.vers = 1;
6495 sinfo.failed = FALSE;
6496
6497 elf_link_hash_traverse (elf_hash_table (info),
6498 _bfd_elf_link_find_version_dependencies,
6499 &sinfo);
6500 if (sinfo.failed)
6501 return FALSE;
6502
6503 if (elf_tdata (output_bfd)->verref == NULL)
6504 s->flags |= SEC_EXCLUDE;
6505 else
6506 {
6507 Elf_Internal_Verneed *vn;
6508 unsigned int size;
6509 unsigned int crefs;
6510 bfd_byte *p;
6511
6512 /* Build the version dependency section. */
6513 size = 0;
6514 crefs = 0;
6515 for (vn = elf_tdata (output_bfd)->verref;
6516 vn != NULL;
6517 vn = vn->vn_nextref)
6518 {
6519 Elf_Internal_Vernaux *a;
6520
6521 size += sizeof (Elf_External_Verneed);
6522 ++crefs;
6523 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6524 size += sizeof (Elf_External_Vernaux);
6525 }
6526
6527 s->size = size;
6528 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6529 if (s->contents == NULL)
6530 return FALSE;
6531
6532 p = s->contents;
6533 for (vn = elf_tdata (output_bfd)->verref;
6534 vn != NULL;
6535 vn = vn->vn_nextref)
6536 {
6537 unsigned int caux;
6538 Elf_Internal_Vernaux *a;
6539 size_t indx;
6540
6541 caux = 0;
6542 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6543 ++caux;
6544
6545 vn->vn_version = VER_NEED_CURRENT;
6546 vn->vn_cnt = caux;
6547 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6548 elf_dt_name (vn->vn_bfd) != NULL
6549 ? elf_dt_name (vn->vn_bfd)
6550 : lbasename (vn->vn_bfd->filename),
6551 FALSE);
6552 if (indx == (size_t) -1)
6553 return FALSE;
6554 vn->vn_file = indx;
6555 vn->vn_aux = sizeof (Elf_External_Verneed);
6556 if (vn->vn_nextref == NULL)
6557 vn->vn_next = 0;
6558 else
6559 vn->vn_next = (sizeof (Elf_External_Verneed)
6560 + caux * sizeof (Elf_External_Vernaux));
6561
6562 _bfd_elf_swap_verneed_out (output_bfd, vn,
6563 (Elf_External_Verneed *) p);
6564 p += sizeof (Elf_External_Verneed);
6565
6566 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6567 {
6568 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6569 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6570 a->vna_nodename, FALSE);
6571 if (indx == (size_t) -1)
6572 return FALSE;
6573 a->vna_name = indx;
6574 if (a->vna_nextptr == NULL)
6575 a->vna_next = 0;
6576 else
6577 a->vna_next = sizeof (Elf_External_Vernaux);
6578
6579 _bfd_elf_swap_vernaux_out (output_bfd, a,
6580 (Elf_External_Vernaux *) p);
6581 p += sizeof (Elf_External_Vernaux);
6582 }
6583 }
6584
6585 elf_tdata (output_bfd)->cverrefs = crefs;
6586 }
902e9fc7
MR
6587 }
6588
6589 /* Any syms created from now on start with -1 in
6590 got.refcount/offset and plt.refcount/offset. */
6591 elf_hash_table (info)->init_got_refcount
6592 = elf_hash_table (info)->init_got_offset;
6593 elf_hash_table (info)->init_plt_refcount
6594 = elf_hash_table (info)->init_plt_offset;
6595
6596 if (bfd_link_relocatable (info)
6597 && !_bfd_elf_size_group_sections (info))
6598 return FALSE;
6599
6600 /* The backend may have to create some sections regardless of whether
6601 we're dynamic or not. */
6602 if (bed->elf_backend_always_size_sections
6603 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6604 return FALSE;
6605
6606 /* Determine any GNU_STACK segment requirements, after the backend
6607 has had a chance to set a default segment size. */
6608 if (info->execstack)
6609 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6610 else if (info->noexecstack)
6611 elf_stack_flags (output_bfd) = PF_R | PF_W;
6612 else
6613 {
6614 bfd *inputobj;
6615 asection *notesec = NULL;
6616 int exec = 0;
6617
6618 for (inputobj = info->input_bfds;
6619 inputobj;
6620 inputobj = inputobj->link.next)
6621 {
6622 asection *s;
6623
6624 if (inputobj->flags
6625 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6626 continue;
57963c05
AM
6627 s = inputobj->sections;
6628 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6629 continue;
6630
902e9fc7
MR
6631 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6632 if (s)
6633 {
6634 if (s->flags & SEC_CODE)
6635 exec = PF_X;
6636 notesec = s;
6637 }
6638 else if (bed->default_execstack)
6639 exec = PF_X;
6640 }
6641 if (notesec || info->stacksize > 0)
6642 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6643 if (notesec && exec && bfd_link_relocatable (info)
6644 && notesec->output_section != bfd_abs_section_ptr)
6645 notesec->output_section->flags |= SEC_CODE;
6646 }
6647
6648 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6649 {
6650 struct elf_info_failed eif;
6651 struct elf_link_hash_entry *h;
6652 asection *dynstr;
6653 asection *s;
6654
6655 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6656 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6657
902e9fc7
MR
6658 if (info->symbolic)
6659 {
6660 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6661 return FALSE;
6662 info->flags |= DF_SYMBOLIC;
6663 }
6664
6665 if (rpath != NULL)
6666 {
6667 size_t indx;
6668 bfd_vma tag;
6669
6670 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6671 TRUE);
6672 if (indx == (size_t) -1)
6673 return FALSE;
6674
6675 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6676 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6677 return FALSE;
6678 }
6679
6680 if (filter_shlib != NULL)
6681 {
6682 size_t indx;
6683
6684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685 filter_shlib, TRUE);
6686 if (indx == (size_t) -1
6687 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6688 return FALSE;
6689 }
6690
6691 if (auxiliary_filters != NULL)
6692 {
6693 const char * const *p;
6694
6695 for (p = auxiliary_filters; *p != NULL; p++)
6696 {
6697 size_t indx;
6698
6699 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6700 *p, TRUE);
6701 if (indx == (size_t) -1
6702 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6703 return FALSE;
6704 }
6705 }
6706
6707 if (audit != NULL)
6708 {
6709 size_t indx;
6710
6711 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6712 TRUE);
6713 if (indx == (size_t) -1
6714 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6715 return FALSE;
6716 }
6717
6718 if (depaudit != NULL)
6719 {
6720 size_t indx;
6721
6722 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6723 TRUE);
6724 if (indx == (size_t) -1
6725 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6726 return FALSE;
6727 }
6728
6729 eif.info = info;
6730 eif.failed = FALSE;
6731
6732 /* Find all symbols which were defined in a dynamic object and make
6733 the backend pick a reasonable value for them. */
6734 elf_link_hash_traverse (elf_hash_table (info),
6735 _bfd_elf_adjust_dynamic_symbol,
6736 &eif);
6737 if (eif.failed)
6738 return FALSE;
6739
6740 /* Add some entries to the .dynamic section. We fill in some of the
6741 values later, in bfd_elf_final_link, but we must add the entries
6742 now so that we know the final size of the .dynamic section. */
6743
6744 /* If there are initialization and/or finalization functions to
6745 call then add the corresponding DT_INIT/DT_FINI entries. */
6746 h = (info->init_function
6747 ? elf_link_hash_lookup (elf_hash_table (info),
6748 info->init_function, FALSE,
6749 FALSE, FALSE)
6750 : NULL);
6751 if (h != NULL
6752 && (h->ref_regular
6753 || h->def_regular))
6754 {
6755 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6756 return FALSE;
6757 }
6758 h = (info->fini_function
6759 ? elf_link_hash_lookup (elf_hash_table (info),
6760 info->fini_function, FALSE,
6761 FALSE, FALSE)
6762 : NULL);
6763 if (h != NULL
6764 && (h->ref_regular
6765 || h->def_regular))
6766 {
6767 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6768 return FALSE;
6769 }
6770
6771 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6772 if (s != NULL && s->linker_has_input)
6773 {
6774 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6775 if (! bfd_link_executable (info))
6776 {
6777 bfd *sub;
6778 asection *o;
6779
57963c05
AM
6780 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6781 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6782 && (o = sub->sections) != NULL
6783 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6784 for (o = sub->sections; o != NULL; o = o->next)
6785 if (elf_section_data (o)->this_hdr.sh_type
6786 == SHT_PREINIT_ARRAY)
6787 {
6788 _bfd_error_handler
871b3ab2 6789 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6790 sub);
6791 break;
6792 }
6793
6794 bfd_set_error (bfd_error_nonrepresentable_section);
6795 return FALSE;
6796 }
6797
6798 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6799 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6800 return FALSE;
6801 }
6802 s = bfd_get_section_by_name (output_bfd, ".init_array");
6803 if (s != NULL && s->linker_has_input)
6804 {
6805 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6806 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6807 return FALSE;
6808 }
6809 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6810 if (s != NULL && s->linker_has_input)
6811 {
6812 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6813 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6814 return FALSE;
6815 }
6816
6817 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6818 /* If .dynstr is excluded from the link, we don't want any of
6819 these tags. Strictly, we should be checking each section
6820 individually; This quick check covers for the case where
6821 someone does a /DISCARD/ : { *(*) }. */
6822 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6823 {
6824 bfd_size_type strsize;
6825
6826 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6827 if ((info->emit_hash
6828 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6829 || (info->emit_gnu_hash
6830 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6831 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6832 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6833 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6834 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6835 bed->s->sizeof_sym))
6836 return FALSE;
6837 }
6838 }
6839
6840 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6841 return FALSE;
6842
6843 /* The backend must work out the sizes of all the other dynamic
6844 sections. */
6845 if (dynobj != NULL
6846 && bed->elf_backend_size_dynamic_sections != NULL
6847 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6848 return FALSE;
6849
6850 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6851 {
902e9fc7
MR
6852 if (elf_tdata (output_bfd)->cverdefs)
6853 {
6854 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6855
6856 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6857 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6858 return FALSE;
6859 }
6860
6861 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6862 {
6863 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6864 return FALSE;
6865 }
6866 else if (info->flags & DF_BIND_NOW)
6867 {
6868 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6869 return FALSE;
6870 }
6871
6872 if (info->flags_1)
6873 {
6874 if (bfd_link_executable (info))
6875 info->flags_1 &= ~ (DF_1_INITFIRST
6876 | DF_1_NODELETE
6877 | DF_1_NOOPEN);
6878 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6879 return FALSE;
6880 }
6881
6882 if (elf_tdata (output_bfd)->cverrefs)
6883 {
6884 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6885
6886 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6887 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6888 return FALSE;
6889 }
5a580b3a 6890
8423293d
AM
6891 if ((elf_tdata (output_bfd)->cverrefs == 0
6892 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 6893 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 6894 {
902e9fc7
MR
6895 asection *s;
6896
3d4d4302 6897 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6898 s->flags |= SEC_EXCLUDE;
6899 }
6900 }
6901 return TRUE;
6902}
6903
74541ad4
AM
6904/* Find the first non-excluded output section. We'll use its
6905 section symbol for some emitted relocs. */
6906void
6907_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6908{
6909 asection *s;
6910
6911 for (s = output_bfd->sections; s != NULL; s = s->next)
6912 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 6913 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
6914 {
6915 elf_hash_table (info)->text_index_section = s;
6916 break;
6917 }
6918}
6919
6920/* Find two non-excluded output sections, one for code, one for data.
6921 We'll use their section symbols for some emitted relocs. */
6922void
6923_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6924{
6925 asection *s;
6926
266b05cf
DJ
6927 /* Data first, since setting text_index_section changes
6928 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6929 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6930 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 6931 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 6932 {
266b05cf 6933 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6934 break;
6935 }
6936
6937 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6938 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6939 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 6940 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 6941 {
266b05cf 6942 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6943 break;
6944 }
6945
6946 if (elf_hash_table (info)->text_index_section == NULL)
6947 elf_hash_table (info)->text_index_section
6948 = elf_hash_table (info)->data_index_section;
6949}
6950
8423293d
AM
6951bfd_boolean
6952bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6953{
74541ad4 6954 const struct elf_backend_data *bed;
23ec1e32 6955 unsigned long section_sym_count;
96d01d93 6956 bfd_size_type dynsymcount = 0;
74541ad4 6957
8423293d
AM
6958 if (!is_elf_hash_table (info->hash))
6959 return TRUE;
6960
74541ad4
AM
6961 bed = get_elf_backend_data (output_bfd);
6962 (*bed->elf_backend_init_index_section) (output_bfd, info);
6963
23ec1e32
MR
6964 /* Assign dynsym indices. In a shared library we generate a section
6965 symbol for each output section, which come first. Next come all
6966 of the back-end allocated local dynamic syms, followed by the rest
6967 of the global symbols.
6968
6969 This is usually not needed for static binaries, however backends
6970 can request to always do it, e.g. the MIPS backend uses dynamic
6971 symbol counts to lay out GOT, which will be produced in the
6972 presence of GOT relocations even in static binaries (holding fixed
6973 data in that case, to satisfy those relocations). */
6974
6975 if (elf_hash_table (info)->dynamic_sections_created
6976 || bed->always_renumber_dynsyms)
6977 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6978 &section_sym_count);
6979
8423293d
AM
6980 if (elf_hash_table (info)->dynamic_sections_created)
6981 {
6982 bfd *dynobj;
8423293d 6983 asection *s;
8423293d
AM
6984 unsigned int dtagcount;
6985
6986 dynobj = elf_hash_table (info)->dynobj;
6987
5a580b3a 6988 /* Work out the size of the symbol version section. */
3d4d4302 6989 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6990 BFD_ASSERT (s != NULL);
d5486c43 6991 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6992 {
eea6121a 6993 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6994 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6995 if (s->contents == NULL)
6996 return FALSE;
6997
6998 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6999 return FALSE;
7000 }
7001
7002 /* Set the size of the .dynsym and .hash sections. We counted
7003 the number of dynamic symbols in elf_link_add_object_symbols.
7004 We will build the contents of .dynsym and .hash when we build
7005 the final symbol table, because until then we do not know the
7006 correct value to give the symbols. We built the .dynstr
7007 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7008 s = elf_hash_table (info)->dynsym;
5a580b3a 7009 BFD_ASSERT (s != NULL);
eea6121a 7010 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7011
d5486c43
L
7012 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7013 if (s->contents == NULL)
7014 return FALSE;
5a580b3a 7015
d5486c43
L
7016 /* The first entry in .dynsym is a dummy symbol. Clear all the
7017 section syms, in case we don't output them all. */
7018 ++section_sym_count;
7019 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7020
fdc90cb4
JJ
7021 elf_hash_table (info)->bucketcount = 0;
7022
5a580b3a
AM
7023 /* Compute the size of the hashing table. As a side effect this
7024 computes the hash values for all the names we export. */
fdc90cb4
JJ
7025 if (info->emit_hash)
7026 {
7027 unsigned long int *hashcodes;
14b1c01e 7028 struct hash_codes_info hashinf;
fdc90cb4
JJ
7029 bfd_size_type amt;
7030 unsigned long int nsyms;
7031 size_t bucketcount;
7032 size_t hash_entry_size;
7033
7034 /* Compute the hash values for all exported symbols. At the same
7035 time store the values in an array so that we could use them for
7036 optimizations. */
7037 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7038 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7039 if (hashcodes == NULL)
7040 return FALSE;
14b1c01e
AM
7041 hashinf.hashcodes = hashcodes;
7042 hashinf.error = FALSE;
5a580b3a 7043
fdc90cb4
JJ
7044 /* Put all hash values in HASHCODES. */
7045 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7046 elf_collect_hash_codes, &hashinf);
7047 if (hashinf.error)
4dd07732
AM
7048 {
7049 free (hashcodes);
7050 return FALSE;
7051 }
5a580b3a 7052
14b1c01e 7053 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7054 bucketcount
7055 = compute_bucket_count (info, hashcodes, nsyms, 0);
7056 free (hashcodes);
7057
4b48e2f6 7058 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7059 return FALSE;
5a580b3a 7060
fdc90cb4
JJ
7061 elf_hash_table (info)->bucketcount = bucketcount;
7062
3d4d4302 7063 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7064 BFD_ASSERT (s != NULL);
7065 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7066 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7067 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7068 if (s->contents == NULL)
7069 return FALSE;
7070
7071 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7072 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7073 s->contents + hash_entry_size);
7074 }
7075
7076 if (info->emit_gnu_hash)
7077 {
7078 size_t i, cnt;
7079 unsigned char *contents;
7080 struct collect_gnu_hash_codes cinfo;
7081 bfd_size_type amt;
7082 size_t bucketcount;
7083
7084 memset (&cinfo, 0, sizeof (cinfo));
7085
7086 /* Compute the hash values for all exported symbols. At the same
7087 time store the values in an array so that we could use them for
7088 optimizations. */
7089 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7090 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7091 if (cinfo.hashcodes == NULL)
7092 return FALSE;
7093
7094 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7095 cinfo.min_dynindx = -1;
7096 cinfo.output_bfd = output_bfd;
7097 cinfo.bed = bed;
7098
7099 /* Put all hash values in HASHCODES. */
7100 elf_link_hash_traverse (elf_hash_table (info),
7101 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7102 if (cinfo.error)
4dd07732
AM
7103 {
7104 free (cinfo.hashcodes);
7105 return FALSE;
7106 }
fdc90cb4
JJ
7107
7108 bucketcount
7109 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7110
7111 if (bucketcount == 0)
7112 {
7113 free (cinfo.hashcodes);
7114 return FALSE;
7115 }
7116
3d4d4302 7117 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7118 BFD_ASSERT (s != NULL);
7119
7120 if (cinfo.nsyms == 0)
7121 {
7122 /* Empty .gnu.hash section is special. */
7123 BFD_ASSERT (cinfo.min_dynindx == -1);
7124 free (cinfo.hashcodes);
7125 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7126 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7127 if (contents == NULL)
7128 return FALSE;
7129 s->contents = contents;
7130 /* 1 empty bucket. */
7131 bfd_put_32 (output_bfd, 1, contents);
7132 /* SYMIDX above the special symbol 0. */
7133 bfd_put_32 (output_bfd, 1, contents + 4);
7134 /* Just one word for bitmask. */
7135 bfd_put_32 (output_bfd, 1, contents + 8);
7136 /* Only hash fn bloom filter. */
7137 bfd_put_32 (output_bfd, 0, contents + 12);
7138 /* No hashes are valid - empty bitmask. */
7139 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7140 /* No hashes in the only bucket. */
7141 bfd_put_32 (output_bfd, 0,
7142 contents + 16 + bed->s->arch_size / 8);
7143 }
7144 else
7145 {
9e6619e2 7146 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7147 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7148
9e6619e2
AM
7149 x = cinfo.nsyms;
7150 maskbitslog2 = 1;
7151 while ((x >>= 1) != 0)
7152 ++maskbitslog2;
fdc90cb4
JJ
7153 if (maskbitslog2 < 3)
7154 maskbitslog2 = 5;
7155 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7156 maskbitslog2 = maskbitslog2 + 3;
7157 else
7158 maskbitslog2 = maskbitslog2 + 2;
7159 if (bed->s->arch_size == 64)
7160 {
7161 if (maskbitslog2 == 5)
7162 maskbitslog2 = 6;
7163 cinfo.shift1 = 6;
7164 }
7165 else
7166 cinfo.shift1 = 5;
7167 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7168 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7169 cinfo.maskbits = 1 << maskbitslog2;
7170 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7171 amt = bucketcount * sizeof (unsigned long int) * 2;
7172 amt += maskwords * sizeof (bfd_vma);
a50b1753 7173 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7174 if (cinfo.bitmask == NULL)
7175 {
7176 free (cinfo.hashcodes);
7177 return FALSE;
7178 }
7179
a50b1753 7180 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7181 cinfo.indx = cinfo.counts + bucketcount;
7182 cinfo.symindx = dynsymcount - cinfo.nsyms;
7183 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7184
7185 /* Determine how often each hash bucket is used. */
7186 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7187 for (i = 0; i < cinfo.nsyms; ++i)
7188 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7189
7190 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7191 if (cinfo.counts[i] != 0)
7192 {
7193 cinfo.indx[i] = cnt;
7194 cnt += cinfo.counts[i];
7195 }
7196 BFD_ASSERT (cnt == dynsymcount);
7197 cinfo.bucketcount = bucketcount;
7198 cinfo.local_indx = cinfo.min_dynindx;
7199
7200 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7201 s->size += cinfo.maskbits / 8;
a50b1753 7202 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7203 if (contents == NULL)
7204 {
7205 free (cinfo.bitmask);
7206 free (cinfo.hashcodes);
7207 return FALSE;
7208 }
7209
7210 s->contents = contents;
7211 bfd_put_32 (output_bfd, bucketcount, contents);
7212 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7213 bfd_put_32 (output_bfd, maskwords, contents + 8);
7214 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7215 contents += 16 + cinfo.maskbits / 8;
7216
7217 for (i = 0; i < bucketcount; ++i)
7218 {
7219 if (cinfo.counts[i] == 0)
7220 bfd_put_32 (output_bfd, 0, contents);
7221 else
7222 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7223 contents += 4;
7224 }
7225
7226 cinfo.contents = contents;
7227
7228 /* Renumber dynamic symbols, populate .gnu.hash section. */
7229 elf_link_hash_traverse (elf_hash_table (info),
7230 elf_renumber_gnu_hash_syms, &cinfo);
7231
7232 contents = s->contents + 16;
7233 for (i = 0; i < maskwords; ++i)
7234 {
7235 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7236 contents);
7237 contents += bed->s->arch_size / 8;
7238 }
7239
7240 free (cinfo.bitmask);
7241 free (cinfo.hashcodes);
7242 }
7243 }
5a580b3a 7244
3d4d4302 7245 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7246 BFD_ASSERT (s != NULL);
7247
4ad4eba5 7248 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7249
eea6121a 7250 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7251
7252 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7253 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7254 return FALSE;
7255 }
7256
7257 return TRUE;
7258}
4d269e42 7259\f
4d269e42
AM
7260/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7261
7262static void
7263merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7264 asection *sec)
7265{
dbaa2011
AM
7266 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7267 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7268}
7269
7270/* Finish SHF_MERGE section merging. */
7271
7272bfd_boolean
630993ec 7273_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7274{
7275 bfd *ibfd;
7276 asection *sec;
7277
7278 if (!is_elf_hash_table (info->hash))
7279 return FALSE;
7280
c72f2fb2 7281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7282 if ((ibfd->flags & DYNAMIC) == 0
7283 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7284 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7285 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7286 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7287 if ((sec->flags & SEC_MERGE) != 0
7288 && !bfd_is_abs_section (sec->output_section))
7289 {
7290 struct bfd_elf_section_data *secdata;
7291
7292 secdata = elf_section_data (sec);
630993ec 7293 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7294 &elf_hash_table (info)->merge_info,
7295 sec, &secdata->sec_info))
7296 return FALSE;
7297 else if (secdata->sec_info)
dbaa2011 7298 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7299 }
7300
7301 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7302 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7303 merge_sections_remove_hook);
7304 return TRUE;
7305}
7306
7307/* Create an entry in an ELF linker hash table. */
7308
7309struct bfd_hash_entry *
7310_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7311 struct bfd_hash_table *table,
7312 const char *string)
7313{
7314 /* Allocate the structure if it has not already been allocated by a
7315 subclass. */
7316 if (entry == NULL)
7317 {
a50b1753 7318 entry = (struct bfd_hash_entry *)
ca4be51c 7319 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7320 if (entry == NULL)
7321 return entry;
7322 }
7323
7324 /* Call the allocation method of the superclass. */
7325 entry = _bfd_link_hash_newfunc (entry, table, string);
7326 if (entry != NULL)
7327 {
7328 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7329 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7330
7331 /* Set local fields. */
7332 ret->indx = -1;
7333 ret->dynindx = -1;
7334 ret->got = htab->init_got_refcount;
7335 ret->plt = htab->init_plt_refcount;
7336 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7337 - offsetof (struct elf_link_hash_entry, size)));
7338 /* Assume that we have been called by a non-ELF symbol reader.
7339 This flag is then reset by the code which reads an ELF input
7340 file. This ensures that a symbol created by a non-ELF symbol
7341 reader will have the flag set correctly. */
7342 ret->non_elf = 1;
7343 }
7344
7345 return entry;
7346}
7347
7348/* Copy data from an indirect symbol to its direct symbol, hiding the
7349 old indirect symbol. Also used for copying flags to a weakdef. */
7350
7351void
7352_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7353 struct elf_link_hash_entry *dir,
7354 struct elf_link_hash_entry *ind)
7355{
7356 struct elf_link_hash_table *htab;
7357
7358 /* Copy down any references that we may have already seen to the
e81830c5 7359 symbol which just became indirect. */
4d269e42 7360
422f1182 7361 if (dir->versioned != versioned_hidden)
e81830c5
AM
7362 dir->ref_dynamic |= ind->ref_dynamic;
7363 dir->ref_regular |= ind->ref_regular;
7364 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7365 dir->non_got_ref |= ind->non_got_ref;
7366 dir->needs_plt |= ind->needs_plt;
7367 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7368
7369 if (ind->root.type != bfd_link_hash_indirect)
7370 return;
7371
7372 /* Copy over the global and procedure linkage table refcount entries.
7373 These may have been already set up by a check_relocs routine. */
7374 htab = elf_hash_table (info);
7375 if (ind->got.refcount > htab->init_got_refcount.refcount)
7376 {
7377 if (dir->got.refcount < 0)
7378 dir->got.refcount = 0;
7379 dir->got.refcount += ind->got.refcount;
7380 ind->got.refcount = htab->init_got_refcount.refcount;
7381 }
7382
7383 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7384 {
7385 if (dir->plt.refcount < 0)
7386 dir->plt.refcount = 0;
7387 dir->plt.refcount += ind->plt.refcount;
7388 ind->plt.refcount = htab->init_plt_refcount.refcount;
7389 }
7390
7391 if (ind->dynindx != -1)
7392 {
7393 if (dir->dynindx != -1)
7394 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7395 dir->dynindx = ind->dynindx;
7396 dir->dynstr_index = ind->dynstr_index;
7397 ind->dynindx = -1;
7398 ind->dynstr_index = 0;
7399 }
7400}
7401
7402void
7403_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7404 struct elf_link_hash_entry *h,
7405 bfd_boolean force_local)
7406{
3aa14d16
L
7407 /* STT_GNU_IFUNC symbol must go through PLT. */
7408 if (h->type != STT_GNU_IFUNC)
7409 {
7410 h->plt = elf_hash_table (info)->init_plt_offset;
7411 h->needs_plt = 0;
7412 }
4d269e42
AM
7413 if (force_local)
7414 {
7415 h->forced_local = 1;
7416 if (h->dynindx != -1)
7417 {
4d269e42
AM
7418 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7419 h->dynstr_index);
641338d8
AM
7420 h->dynindx = -1;
7421 h->dynstr_index = 0;
4d269e42
AM
7422 }
7423 }
7424}
7425
7bf52ea2
AM
7426/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7427 caller. */
4d269e42
AM
7428
7429bfd_boolean
7430_bfd_elf_link_hash_table_init
7431 (struct elf_link_hash_table *table,
7432 bfd *abfd,
7433 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7434 struct bfd_hash_table *,
7435 const char *),
4dfe6ac6
NC
7436 unsigned int entsize,
7437 enum elf_target_id target_id)
4d269e42
AM
7438{
7439 bfd_boolean ret;
7440 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7441
4d269e42
AM
7442 table->init_got_refcount.refcount = can_refcount - 1;
7443 table->init_plt_refcount.refcount = can_refcount - 1;
7444 table->init_got_offset.offset = -(bfd_vma) 1;
7445 table->init_plt_offset.offset = -(bfd_vma) 1;
7446 /* The first dynamic symbol is a dummy. */
7447 table->dynsymcount = 1;
7448
7449 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7450
4d269e42 7451 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7452 table->hash_table_id = target_id;
4d269e42
AM
7453
7454 return ret;
7455}
7456
7457/* Create an ELF linker hash table. */
7458
7459struct bfd_link_hash_table *
7460_bfd_elf_link_hash_table_create (bfd *abfd)
7461{
7462 struct elf_link_hash_table *ret;
7463 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7464
7bf52ea2 7465 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7466 if (ret == NULL)
7467 return NULL;
7468
7469 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7470 sizeof (struct elf_link_hash_entry),
7471 GENERIC_ELF_DATA))
4d269e42
AM
7472 {
7473 free (ret);
7474 return NULL;
7475 }
d495ab0d 7476 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7477
7478 return &ret->root;
7479}
7480
9f7c3e5e
AM
7481/* Destroy an ELF linker hash table. */
7482
7483void
d495ab0d 7484_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7485{
d495ab0d
AM
7486 struct elf_link_hash_table *htab;
7487
7488 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7489 if (htab->dynstr != NULL)
7490 _bfd_elf_strtab_free (htab->dynstr);
7491 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7492 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7493}
7494
4d269e42
AM
7495/* This is a hook for the ELF emulation code in the generic linker to
7496 tell the backend linker what file name to use for the DT_NEEDED
7497 entry for a dynamic object. */
7498
7499void
7500bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7501{
7502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7503 && bfd_get_format (abfd) == bfd_object)
7504 elf_dt_name (abfd) = name;
7505}
7506
7507int
7508bfd_elf_get_dyn_lib_class (bfd *abfd)
7509{
7510 int lib_class;
7511 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7512 && bfd_get_format (abfd) == bfd_object)
7513 lib_class = elf_dyn_lib_class (abfd);
7514 else
7515 lib_class = 0;
7516 return lib_class;
7517}
7518
7519void
7520bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7521{
7522 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7523 && bfd_get_format (abfd) == bfd_object)
7524 elf_dyn_lib_class (abfd) = lib_class;
7525}
7526
7527/* Get the list of DT_NEEDED entries for a link. This is a hook for
7528 the linker ELF emulation code. */
7529
7530struct bfd_link_needed_list *
7531bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7532 struct bfd_link_info *info)
7533{
7534 if (! is_elf_hash_table (info->hash))
7535 return NULL;
7536 return elf_hash_table (info)->needed;
7537}
7538
7539/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7540 hook for the linker ELF emulation code. */
7541
7542struct bfd_link_needed_list *
7543bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7544 struct bfd_link_info *info)
7545{
7546 if (! is_elf_hash_table (info->hash))
7547 return NULL;
7548 return elf_hash_table (info)->runpath;
7549}
7550
7551/* Get the name actually used for a dynamic object for a link. This
7552 is the SONAME entry if there is one. Otherwise, it is the string
7553 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7554
7555const char *
7556bfd_elf_get_dt_soname (bfd *abfd)
7557{
7558 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7559 && bfd_get_format (abfd) == bfd_object)
7560 return elf_dt_name (abfd);
7561 return NULL;
7562}
7563
7564/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7565 the ELF linker emulation code. */
7566
7567bfd_boolean
7568bfd_elf_get_bfd_needed_list (bfd *abfd,
7569 struct bfd_link_needed_list **pneeded)
7570{
7571 asection *s;
7572 bfd_byte *dynbuf = NULL;
cb33740c 7573 unsigned int elfsec;
4d269e42
AM
7574 unsigned long shlink;
7575 bfd_byte *extdyn, *extdynend;
7576 size_t extdynsize;
7577 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7578
7579 *pneeded = NULL;
7580
7581 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7582 || bfd_get_format (abfd) != bfd_object)
7583 return TRUE;
7584
7585 s = bfd_get_section_by_name (abfd, ".dynamic");
7586 if (s == NULL || s->size == 0)
7587 return TRUE;
7588
7589 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7590 goto error_return;
7591
7592 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7593 if (elfsec == SHN_BAD)
4d269e42
AM
7594 goto error_return;
7595
7596 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7597
4d269e42
AM
7598 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7599 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7600
7601 extdyn = dynbuf;
7602 extdynend = extdyn + s->size;
7603 for (; extdyn < extdynend; extdyn += extdynsize)
7604 {
7605 Elf_Internal_Dyn dyn;
7606
7607 (*swap_dyn_in) (abfd, extdyn, &dyn);
7608
7609 if (dyn.d_tag == DT_NULL)
7610 break;
7611
7612 if (dyn.d_tag == DT_NEEDED)
7613 {
7614 const char *string;
7615 struct bfd_link_needed_list *l;
7616 unsigned int tagv = dyn.d_un.d_val;
7617 bfd_size_type amt;
7618
7619 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7620 if (string == NULL)
7621 goto error_return;
7622
7623 amt = sizeof *l;
a50b1753 7624 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7625 if (l == NULL)
7626 goto error_return;
7627
7628 l->by = abfd;
7629 l->name = string;
7630 l->next = *pneeded;
7631 *pneeded = l;
7632 }
7633 }
7634
7635 free (dynbuf);
7636
7637 return TRUE;
7638
7639 error_return:
7640 if (dynbuf != NULL)
7641 free (dynbuf);
7642 return FALSE;
7643}
7644
7645struct elf_symbuf_symbol
7646{
7647 unsigned long st_name; /* Symbol name, index in string tbl */
7648 unsigned char st_info; /* Type and binding attributes */
7649 unsigned char st_other; /* Visibilty, and target specific */
7650};
7651
7652struct elf_symbuf_head
7653{
7654 struct elf_symbuf_symbol *ssym;
ef53be89 7655 size_t count;
4d269e42
AM
7656 unsigned int st_shndx;
7657};
7658
7659struct elf_symbol
7660{
7661 union
7662 {
7663 Elf_Internal_Sym *isym;
7664 struct elf_symbuf_symbol *ssym;
7665 } u;
7666 const char *name;
7667};
7668
7669/* Sort references to symbols by ascending section number. */
7670
7671static int
7672elf_sort_elf_symbol (const void *arg1, const void *arg2)
7673{
7674 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7675 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7676
7677 return s1->st_shndx - s2->st_shndx;
7678}
7679
7680static int
7681elf_sym_name_compare (const void *arg1, const void *arg2)
7682{
7683 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7684 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7685 return strcmp (s1->name, s2->name);
7686}
7687
7688static struct elf_symbuf_head *
ef53be89 7689elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7690{
14b1c01e 7691 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7692 struct elf_symbuf_symbol *ssym;
7693 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7694 size_t i, shndx_count, total_size;
4d269e42 7695
a50b1753 7696 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7697 if (indbuf == NULL)
7698 return NULL;
7699
7700 for (ind = indbuf, i = 0; i < symcount; i++)
7701 if (isymbuf[i].st_shndx != SHN_UNDEF)
7702 *ind++ = &isymbuf[i];
7703 indbufend = ind;
7704
7705 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7706 elf_sort_elf_symbol);
7707
7708 shndx_count = 0;
7709 if (indbufend > indbuf)
7710 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7711 if (ind[0]->st_shndx != ind[1]->st_shndx)
7712 shndx_count++;
7713
3ae181ee
L
7714 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7715 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7716 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7717 if (ssymbuf == NULL)
7718 {
7719 free (indbuf);
7720 return NULL;
7721 }
7722
3ae181ee 7723 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7724 ssymbuf->ssym = NULL;
7725 ssymbuf->count = shndx_count;
7726 ssymbuf->st_shndx = 0;
7727 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7728 {
7729 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7730 {
7731 ssymhead++;
7732 ssymhead->ssym = ssym;
7733 ssymhead->count = 0;
7734 ssymhead->st_shndx = (*ind)->st_shndx;
7735 }
7736 ssym->st_name = (*ind)->st_name;
7737 ssym->st_info = (*ind)->st_info;
7738 ssym->st_other = (*ind)->st_other;
7739 ssymhead->count++;
7740 }
ef53be89 7741 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7742 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7743 == total_size));
4d269e42
AM
7744
7745 free (indbuf);
7746 return ssymbuf;
7747}
7748
7749/* Check if 2 sections define the same set of local and global
7750 symbols. */
7751
8f317e31 7752static bfd_boolean
4d269e42
AM
7753bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7754 struct bfd_link_info *info)
7755{
7756 bfd *bfd1, *bfd2;
7757 const struct elf_backend_data *bed1, *bed2;
7758 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7759 size_t symcount1, symcount2;
4d269e42
AM
7760 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7761 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7762 Elf_Internal_Sym *isym, *isymend;
7763 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7764 size_t count1, count2, i;
cb33740c 7765 unsigned int shndx1, shndx2;
4d269e42
AM
7766 bfd_boolean result;
7767
7768 bfd1 = sec1->owner;
7769 bfd2 = sec2->owner;
7770
4d269e42
AM
7771 /* Both sections have to be in ELF. */
7772 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7773 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7774 return FALSE;
7775
7776 if (elf_section_type (sec1) != elf_section_type (sec2))
7777 return FALSE;
7778
4d269e42
AM
7779 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7780 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7781 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7782 return FALSE;
7783
7784 bed1 = get_elf_backend_data (bfd1);
7785 bed2 = get_elf_backend_data (bfd2);
7786 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7787 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7788 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7789 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7790
7791 if (symcount1 == 0 || symcount2 == 0)
7792 return FALSE;
7793
7794 result = FALSE;
7795 isymbuf1 = NULL;
7796 isymbuf2 = NULL;
a50b1753
NC
7797 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7798 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7799
7800 if (ssymbuf1 == NULL)
7801 {
7802 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7803 NULL, NULL, NULL);
7804 if (isymbuf1 == NULL)
7805 goto done;
7806
7807 if (!info->reduce_memory_overheads)
7808 elf_tdata (bfd1)->symbuf = ssymbuf1
7809 = elf_create_symbuf (symcount1, isymbuf1);
7810 }
7811
7812 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7813 {
7814 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7815 NULL, NULL, NULL);
7816 if (isymbuf2 == NULL)
7817 goto done;
7818
7819 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7820 elf_tdata (bfd2)->symbuf = ssymbuf2
7821 = elf_create_symbuf (symcount2, isymbuf2);
7822 }
7823
7824 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7825 {
7826 /* Optimized faster version. */
ef53be89 7827 size_t lo, hi, mid;
4d269e42
AM
7828 struct elf_symbol *symp;
7829 struct elf_symbuf_symbol *ssym, *ssymend;
7830
7831 lo = 0;
7832 hi = ssymbuf1->count;
7833 ssymbuf1++;
7834 count1 = 0;
7835 while (lo < hi)
7836 {
7837 mid = (lo + hi) / 2;
cb33740c 7838 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7839 hi = mid;
cb33740c 7840 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7841 lo = mid + 1;
7842 else
7843 {
7844 count1 = ssymbuf1[mid].count;
7845 ssymbuf1 += mid;
7846 break;
7847 }
7848 }
7849
7850 lo = 0;
7851 hi = ssymbuf2->count;
7852 ssymbuf2++;
7853 count2 = 0;
7854 while (lo < hi)
7855 {
7856 mid = (lo + hi) / 2;
cb33740c 7857 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7858 hi = mid;
cb33740c 7859 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7860 lo = mid + 1;
7861 else
7862 {
7863 count2 = ssymbuf2[mid].count;
7864 ssymbuf2 += mid;
7865 break;
7866 }
7867 }
7868
7869 if (count1 == 0 || count2 == 0 || count1 != count2)
7870 goto done;
7871
ca4be51c
AM
7872 symtable1
7873 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7874 symtable2
7875 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7876 if (symtable1 == NULL || symtable2 == NULL)
7877 goto done;
7878
7879 symp = symtable1;
7880 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7881 ssym < ssymend; ssym++, symp++)
7882 {
7883 symp->u.ssym = ssym;
7884 symp->name = bfd_elf_string_from_elf_section (bfd1,
7885 hdr1->sh_link,
7886 ssym->st_name);
7887 }
7888
7889 symp = symtable2;
7890 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7891 ssym < ssymend; ssym++, symp++)
7892 {
7893 symp->u.ssym = ssym;
7894 symp->name = bfd_elf_string_from_elf_section (bfd2,
7895 hdr2->sh_link,
7896 ssym->st_name);
7897 }
7898
7899 /* Sort symbol by name. */
7900 qsort (symtable1, count1, sizeof (struct elf_symbol),
7901 elf_sym_name_compare);
7902 qsort (symtable2, count1, sizeof (struct elf_symbol),
7903 elf_sym_name_compare);
7904
7905 for (i = 0; i < count1; i++)
7906 /* Two symbols must have the same binding, type and name. */
7907 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7908 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7909 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7910 goto done;
7911
7912 result = TRUE;
7913 goto done;
7914 }
7915
a50b1753
NC
7916 symtable1 = (struct elf_symbol *)
7917 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7918 symtable2 = (struct elf_symbol *)
7919 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7920 if (symtable1 == NULL || symtable2 == NULL)
7921 goto done;
7922
7923 /* Count definitions in the section. */
7924 count1 = 0;
7925 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7926 if (isym->st_shndx == shndx1)
4d269e42
AM
7927 symtable1[count1++].u.isym = isym;
7928
7929 count2 = 0;
7930 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7931 if (isym->st_shndx == shndx2)
4d269e42
AM
7932 symtable2[count2++].u.isym = isym;
7933
7934 if (count1 == 0 || count2 == 0 || count1 != count2)
7935 goto done;
7936
7937 for (i = 0; i < count1; i++)
7938 symtable1[i].name
7939 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7940 symtable1[i].u.isym->st_name);
7941
7942 for (i = 0; i < count2; i++)
7943 symtable2[i].name
7944 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7945 symtable2[i].u.isym->st_name);
7946
7947 /* Sort symbol by name. */
7948 qsort (symtable1, count1, sizeof (struct elf_symbol),
7949 elf_sym_name_compare);
7950 qsort (symtable2, count1, sizeof (struct elf_symbol),
7951 elf_sym_name_compare);
7952
7953 for (i = 0; i < count1; i++)
7954 /* Two symbols must have the same binding, type and name. */
7955 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7956 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7957 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7958 goto done;
7959
7960 result = TRUE;
7961
7962done:
7963 if (symtable1)
7964 free (symtable1);
7965 if (symtable2)
7966 free (symtable2);
7967 if (isymbuf1)
7968 free (isymbuf1);
7969 if (isymbuf2)
7970 free (isymbuf2);
7971
7972 return result;
7973}
7974
7975/* Return TRUE if 2 section types are compatible. */
7976
7977bfd_boolean
7978_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7979 bfd *bbfd, const asection *bsec)
7980{
7981 if (asec == NULL
7982 || bsec == NULL
7983 || abfd->xvec->flavour != bfd_target_elf_flavour
7984 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7985 return TRUE;
7986
7987 return elf_section_type (asec) == elf_section_type (bsec);
7988}
7989\f
c152c796
AM
7990/* Final phase of ELF linker. */
7991
7992/* A structure we use to avoid passing large numbers of arguments. */
7993
7994struct elf_final_link_info
7995{
7996 /* General link information. */
7997 struct bfd_link_info *info;
7998 /* Output BFD. */
7999 bfd *output_bfd;
8000 /* Symbol string table. */
ef10c3ac 8001 struct elf_strtab_hash *symstrtab;
c152c796
AM
8002 /* .hash section. */
8003 asection *hash_sec;
8004 /* symbol version section (.gnu.version). */
8005 asection *symver_sec;
8006 /* Buffer large enough to hold contents of any section. */
8007 bfd_byte *contents;
8008 /* Buffer large enough to hold external relocs of any section. */
8009 void *external_relocs;
8010 /* Buffer large enough to hold internal relocs of any section. */
8011 Elf_Internal_Rela *internal_relocs;
8012 /* Buffer large enough to hold external local symbols of any input
8013 BFD. */
8014 bfd_byte *external_syms;
8015 /* And a buffer for symbol section indices. */
8016 Elf_External_Sym_Shndx *locsym_shndx;
8017 /* Buffer large enough to hold internal local symbols of any input
8018 BFD. */
8019 Elf_Internal_Sym *internal_syms;
8020 /* Array large enough to hold a symbol index for each local symbol
8021 of any input BFD. */
8022 long *indices;
8023 /* Array large enough to hold a section pointer for each local
8024 symbol of any input BFD. */
8025 asection **sections;
ef10c3ac 8026 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8027 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8028 /* Number of STT_FILE syms seen. */
8029 size_t filesym_count;
c152c796
AM
8030};
8031
8032/* This struct is used to pass information to elf_link_output_extsym. */
8033
8034struct elf_outext_info
8035{
8036 bfd_boolean failed;
8037 bfd_boolean localsyms;
34a79995 8038 bfd_boolean file_sym_done;
8b127cbc 8039 struct elf_final_link_info *flinfo;
c152c796
AM
8040};
8041
d9352518
DB
8042
8043/* Support for evaluating a complex relocation.
8044
8045 Complex relocations are generalized, self-describing relocations. The
8046 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8047 relocations themselves.
d9352518
DB
8048
8049 The relocations are use a reserved elf-wide relocation type code (R_RELC
8050 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8051 information (start bit, end bit, word width, etc) into the addend. This
8052 information is extracted from CGEN-generated operand tables within gas.
8053
8054 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8055 internal) representing prefix-notation expressions, including but not
8056 limited to those sorts of expressions normally encoded as addends in the
8057 addend field. The symbol mangling format is:
8058
8059 <node> := <literal>
07d6d2b8
AM
8060 | <unary-operator> ':' <node>
8061 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8062 ;
8063
8064 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8065 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8066 | '#' <hexdigits>
8067 ;
8068
8069 <binary-operator> := as in C
8070 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8071
8072static void
a0c8462f
AM
8073set_symbol_value (bfd *bfd_with_globals,
8074 Elf_Internal_Sym *isymbuf,
8075 size_t locsymcount,
8076 size_t symidx,
8077 bfd_vma val)
d9352518 8078{
8977835c
AM
8079 struct elf_link_hash_entry **sym_hashes;
8080 struct elf_link_hash_entry *h;
8081 size_t extsymoff = locsymcount;
d9352518 8082
8977835c 8083 if (symidx < locsymcount)
d9352518 8084 {
8977835c
AM
8085 Elf_Internal_Sym *sym;
8086
8087 sym = isymbuf + symidx;
8088 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8089 {
8090 /* It is a local symbol: move it to the
8091 "absolute" section and give it a value. */
8092 sym->st_shndx = SHN_ABS;
8093 sym->st_value = val;
8094 return;
8095 }
8096 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8097 extsymoff = 0;
d9352518 8098 }
8977835c
AM
8099
8100 /* It is a global symbol: set its link type
8101 to "defined" and give it a value. */
8102
8103 sym_hashes = elf_sym_hashes (bfd_with_globals);
8104 h = sym_hashes [symidx - extsymoff];
8105 while (h->root.type == bfd_link_hash_indirect
8106 || h->root.type == bfd_link_hash_warning)
8107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8108 h->root.type = bfd_link_hash_defined;
8109 h->root.u.def.value = val;
8110 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8111}
8112
a0c8462f
AM
8113static bfd_boolean
8114resolve_symbol (const char *name,
8115 bfd *input_bfd,
8b127cbc 8116 struct elf_final_link_info *flinfo,
a0c8462f
AM
8117 bfd_vma *result,
8118 Elf_Internal_Sym *isymbuf,
8119 size_t locsymcount)
d9352518 8120{
a0c8462f
AM
8121 Elf_Internal_Sym *sym;
8122 struct bfd_link_hash_entry *global_entry;
8123 const char *candidate = NULL;
8124 Elf_Internal_Shdr *symtab_hdr;
8125 size_t i;
8126
d9352518
DB
8127 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8128
8129 for (i = 0; i < locsymcount; ++ i)
8130 {
8977835c 8131 sym = isymbuf + i;
d9352518
DB
8132
8133 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8134 continue;
8135
8136 candidate = bfd_elf_string_from_elf_section (input_bfd,
8137 symtab_hdr->sh_link,
8138 sym->st_name);
8139#ifdef DEBUG
0f02bbd9
AM
8140 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8141 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8142#endif
8143 if (candidate && strcmp (candidate, name) == 0)
8144 {
8b127cbc 8145 asection *sec = flinfo->sections [i];
d9352518 8146
0f02bbd9
AM
8147 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8148 *result += sec->output_offset + sec->output_section->vma;
d9352518 8149#ifdef DEBUG
0f02bbd9
AM
8150 printf ("Found symbol with value %8.8lx\n",
8151 (unsigned long) *result);
d9352518
DB
8152#endif
8153 return TRUE;
8154 }
8155 }
8156
8157 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8158 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8159 FALSE, FALSE, TRUE);
d9352518
DB
8160 if (!global_entry)
8161 return FALSE;
a0c8462f 8162
d9352518
DB
8163 if (global_entry->type == bfd_link_hash_defined
8164 || global_entry->type == bfd_link_hash_defweak)
8165 {
a0c8462f
AM
8166 *result = (global_entry->u.def.value
8167 + global_entry->u.def.section->output_section->vma
8168 + global_entry->u.def.section->output_offset);
d9352518 8169#ifdef DEBUG
0f02bbd9
AM
8170 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8171 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8172#endif
8173 return TRUE;
a0c8462f 8174 }
d9352518 8175
d9352518
DB
8176 return FALSE;
8177}
8178
37b01f6a
DG
8179/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8180 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8181 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8182
d9352518 8183static bfd_boolean
a0c8462f
AM
8184resolve_section (const char *name,
8185 asection *sections,
37b01f6a
DG
8186 bfd_vma *result,
8187 bfd * abfd)
d9352518 8188{
a0c8462f
AM
8189 asection *curr;
8190 unsigned int len;
d9352518 8191
a0c8462f 8192 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8193 if (strcmp (curr->name, name) == 0)
8194 {
8195 *result = curr->vma;
8196 return TRUE;
8197 }
8198
8199 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8200 /* FIXME: This could be coded more efficiently... */
a0c8462f 8201 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8202 {
8203 len = strlen (curr->name);
a0c8462f 8204 if (len > strlen (name))
d9352518
DB
8205 continue;
8206
8207 if (strncmp (curr->name, name, len) == 0)
8208 {
8209 if (strncmp (".end", name + len, 4) == 0)
8210 {
37b01f6a 8211 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8212 return TRUE;
8213 }
8214
8215 /* Insert more pseudo-section names here, if you like. */
8216 }
8217 }
a0c8462f 8218
d9352518
DB
8219 return FALSE;
8220}
8221
8222static void
a0c8462f 8223undefined_reference (const char *reftype, const char *name)
d9352518 8224{
695344c0 8225 /* xgettext:c-format */
a0c8462f
AM
8226 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8227 reftype, name);
d9352518
DB
8228}
8229
8230static bfd_boolean
a0c8462f
AM
8231eval_symbol (bfd_vma *result,
8232 const char **symp,
8233 bfd *input_bfd,
8b127cbc 8234 struct elf_final_link_info *flinfo,
a0c8462f
AM
8235 bfd_vma dot,
8236 Elf_Internal_Sym *isymbuf,
8237 size_t locsymcount,
8238 int signed_p)
d9352518 8239{
4b93929b
NC
8240 size_t len;
8241 size_t symlen;
a0c8462f
AM
8242 bfd_vma a;
8243 bfd_vma b;
4b93929b 8244 char symbuf[4096];
0f02bbd9 8245 const char *sym = *symp;
a0c8462f
AM
8246 const char *symend;
8247 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8248
8249 len = strlen (sym);
8250 symend = sym + len;
8251
4b93929b 8252 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8253 {
8254 bfd_set_error (bfd_error_invalid_operation);
8255 return FALSE;
8256 }
a0c8462f 8257
d9352518
DB
8258 switch (* sym)
8259 {
8260 case '.':
0f02bbd9
AM
8261 *result = dot;
8262 *symp = sym + 1;
d9352518
DB
8263 return TRUE;
8264
8265 case '#':
0f02bbd9
AM
8266 ++sym;
8267 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8268 return TRUE;
8269
8270 case 'S':
8271 symbol_is_section = TRUE;
1a0670f3 8272 /* Fall through. */
a0c8462f 8273 case 's':
0f02bbd9
AM
8274 ++sym;
8275 symlen = strtol (sym, (char **) symp, 10);
8276 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8277
4b93929b 8278 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8279 {
8280 bfd_set_error (bfd_error_invalid_operation);
8281 return FALSE;
8282 }
8283
8284 memcpy (symbuf, sym, symlen);
a0c8462f 8285 symbuf[symlen] = '\0';
0f02bbd9 8286 *symp = sym + symlen;
a0c8462f
AM
8287
8288 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8289 the symbol as a section, or vice-versa. so we're pretty liberal in our
8290 interpretation here; section means "try section first", not "must be a
8291 section", and likewise with symbol. */
8292
a0c8462f 8293 if (symbol_is_section)
d9352518 8294 {
37b01f6a 8295 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8296 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8297 isymbuf, locsymcount))
d9352518
DB
8298 {
8299 undefined_reference ("section", symbuf);
8300 return FALSE;
8301 }
a0c8462f
AM
8302 }
8303 else
d9352518 8304 {
8b127cbc 8305 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8306 isymbuf, locsymcount)
8b127cbc 8307 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8308 result, input_bfd))
d9352518
DB
8309 {
8310 undefined_reference ("symbol", symbuf);
8311 return FALSE;
8312 }
8313 }
8314
8315 return TRUE;
a0c8462f 8316
d9352518
DB
8317 /* All that remains are operators. */
8318
8319#define UNARY_OP(op) \
8320 if (strncmp (sym, #op, strlen (#op)) == 0) \
8321 { \
8322 sym += strlen (#op); \
a0c8462f
AM
8323 if (*sym == ':') \
8324 ++sym; \
0f02bbd9 8325 *symp = sym; \
8b127cbc 8326 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8327 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8328 return FALSE; \
8329 if (signed_p) \
0f02bbd9 8330 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8331 else \
8332 *result = op a; \
d9352518
DB
8333 return TRUE; \
8334 }
8335
8336#define BINARY_OP(op) \
8337 if (strncmp (sym, #op, strlen (#op)) == 0) \
8338 { \
8339 sym += strlen (#op); \
a0c8462f
AM
8340 if (*sym == ':') \
8341 ++sym; \
0f02bbd9 8342 *symp = sym; \
8b127cbc 8343 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8344 isymbuf, locsymcount, signed_p)) \
a0c8462f 8345 return FALSE; \
0f02bbd9 8346 ++*symp; \
8b127cbc 8347 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8348 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8349 return FALSE; \
8350 if (signed_p) \
0f02bbd9 8351 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8352 else \
8353 *result = a op b; \
d9352518
DB
8354 return TRUE; \
8355 }
8356
8357 default:
8358 UNARY_OP (0-);
8359 BINARY_OP (<<);
8360 BINARY_OP (>>);
8361 BINARY_OP (==);
8362 BINARY_OP (!=);
8363 BINARY_OP (<=);
8364 BINARY_OP (>=);
8365 BINARY_OP (&&);
8366 BINARY_OP (||);
8367 UNARY_OP (~);
8368 UNARY_OP (!);
8369 BINARY_OP (*);
8370 BINARY_OP (/);
8371 BINARY_OP (%);
8372 BINARY_OP (^);
8373 BINARY_OP (|);
8374 BINARY_OP (&);
8375 BINARY_OP (+);
8376 BINARY_OP (-);
8377 BINARY_OP (<);
8378 BINARY_OP (>);
8379#undef UNARY_OP
8380#undef BINARY_OP
8381 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8382 bfd_set_error (bfd_error_invalid_operation);
8383 return FALSE;
8384 }
8385}
8386
d9352518 8387static void
a0c8462f
AM
8388put_value (bfd_vma size,
8389 unsigned long chunksz,
8390 bfd *input_bfd,
8391 bfd_vma x,
8392 bfd_byte *location)
d9352518
DB
8393{
8394 location += (size - chunksz);
8395
41cd1ad1 8396 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8397 {
8398 switch (chunksz)
8399 {
d9352518
DB
8400 case 1:
8401 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8402 x >>= 8;
d9352518
DB
8403 break;
8404 case 2:
8405 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8406 x >>= 16;
d9352518
DB
8407 break;
8408 case 4:
8409 bfd_put_32 (input_bfd, x, location);
65164438
NC
8410 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8411 x >>= 16;
8412 x >>= 16;
d9352518 8413 break;
d9352518 8414#ifdef BFD64
41cd1ad1 8415 case 8:
d9352518 8416 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8417 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8418 x >>= 32;
8419 x >>= 32;
8420 break;
d9352518 8421#endif
41cd1ad1
NC
8422 default:
8423 abort ();
d9352518
DB
8424 break;
8425 }
8426 }
8427}
8428
a0c8462f
AM
8429static bfd_vma
8430get_value (bfd_vma size,
8431 unsigned long chunksz,
8432 bfd *input_bfd,
8433 bfd_byte *location)
d9352518 8434{
9b239e0e 8435 int shift;
d9352518
DB
8436 bfd_vma x = 0;
8437
9b239e0e
NC
8438 /* Sanity checks. */
8439 BFD_ASSERT (chunksz <= sizeof (x)
8440 && size >= chunksz
8441 && chunksz != 0
8442 && (size % chunksz) == 0
8443 && input_bfd != NULL
8444 && location != NULL);
8445
8446 if (chunksz == sizeof (x))
8447 {
8448 BFD_ASSERT (size == chunksz);
8449
8450 /* Make sure that we do not perform an undefined shift operation.
8451 We know that size == chunksz so there will only be one iteration
8452 of the loop below. */
8453 shift = 0;
8454 }
8455 else
8456 shift = 8 * chunksz;
8457
a0c8462f 8458 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8459 {
8460 switch (chunksz)
8461 {
d9352518 8462 case 1:
9b239e0e 8463 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8464 break;
8465 case 2:
9b239e0e 8466 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8467 break;
8468 case 4:
9b239e0e 8469 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8470 break;
d9352518 8471#ifdef BFD64
9b239e0e
NC
8472 case 8:
8473 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8474 break;
9b239e0e
NC
8475#endif
8476 default:
8477 abort ();
d9352518
DB
8478 }
8479 }
8480 return x;
8481}
8482
a0c8462f
AM
8483static void
8484decode_complex_addend (unsigned long *start, /* in bits */
8485 unsigned long *oplen, /* in bits */
8486 unsigned long *len, /* in bits */
8487 unsigned long *wordsz, /* in bytes */
8488 unsigned long *chunksz, /* in bytes */
8489 unsigned long *lsb0_p,
8490 unsigned long *signed_p,
8491 unsigned long *trunc_p,
8492 unsigned long encoded)
d9352518 8493{
07d6d2b8
AM
8494 * start = encoded & 0x3F;
8495 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8496 * oplen = (encoded >> 12) & 0x3F;
8497 * wordsz = (encoded >> 18) & 0xF;
8498 * chunksz = (encoded >> 22) & 0xF;
8499 * lsb0_p = (encoded >> 27) & 1;
8500 * signed_p = (encoded >> 28) & 1;
8501 * trunc_p = (encoded >> 29) & 1;
8502}
8503
cdfeee4f 8504bfd_reloc_status_type
0f02bbd9 8505bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8506 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8507 bfd_byte *contents,
8508 Elf_Internal_Rela *rel,
8509 bfd_vma relocation)
d9352518 8510{
0f02bbd9
AM
8511 bfd_vma shift, x, mask;
8512 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8513 bfd_reloc_status_type r;
d9352518
DB
8514
8515 /* Perform this reloc, since it is complex.
8516 (this is not to say that it necessarily refers to a complex
8517 symbol; merely that it is a self-describing CGEN based reloc.
8518 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8519 word size, etc) encoded within it.). */
d9352518 8520
a0c8462f
AM
8521 decode_complex_addend (&start, &oplen, &len, &wordsz,
8522 &chunksz, &lsb0_p, &signed_p,
8523 &trunc_p, rel->r_addend);
d9352518
DB
8524
8525 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8526
8527 if (lsb0_p)
8528 shift = (start + 1) - len;
8529 else
8530 shift = (8 * wordsz) - (start + len);
8531
37b01f6a
DG
8532 x = get_value (wordsz, chunksz, input_bfd,
8533 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8534
8535#ifdef DEBUG
8536 printf ("Doing complex reloc: "
8537 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8538 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8539 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8540 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8541 oplen, (unsigned long) x, (unsigned long) mask,
8542 (unsigned long) relocation);
d9352518
DB
8543#endif
8544
cdfeee4f 8545 r = bfd_reloc_ok;
d9352518 8546 if (! trunc_p)
cdfeee4f
AM
8547 /* Now do an overflow check. */
8548 r = bfd_check_overflow ((signed_p
8549 ? complain_overflow_signed
8550 : complain_overflow_unsigned),
8551 len, 0, (8 * wordsz),
8552 relocation);
a0c8462f 8553
d9352518
DB
8554 /* Do the deed. */
8555 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8556
8557#ifdef DEBUG
8558 printf (" relocation: %8.8lx\n"
8559 " shifted mask: %8.8lx\n"
8560 " shifted/masked reloc: %8.8lx\n"
8561 " result: %8.8lx\n",
9ccb8af9
AM
8562 (unsigned long) relocation, (unsigned long) (mask << shift),
8563 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8564#endif
37b01f6a
DG
8565 put_value (wordsz, chunksz, input_bfd, x,
8566 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8567 return r;
d9352518
DB
8568}
8569
0e287786
AM
8570/* Functions to read r_offset from external (target order) reloc
8571 entry. Faster than bfd_getl32 et al, because we let the compiler
8572 know the value is aligned. */
53df40a4 8573
0e287786
AM
8574static bfd_vma
8575ext32l_r_offset (const void *p)
53df40a4
AM
8576{
8577 union aligned32
8578 {
8579 uint32_t v;
8580 unsigned char c[4];
8581 };
8582 const union aligned32 *a
0e287786 8583 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8584
8585 uint32_t aval = ( (uint32_t) a->c[0]
8586 | (uint32_t) a->c[1] << 8
8587 | (uint32_t) a->c[2] << 16
8588 | (uint32_t) a->c[3] << 24);
0e287786 8589 return aval;
53df40a4
AM
8590}
8591
0e287786
AM
8592static bfd_vma
8593ext32b_r_offset (const void *p)
53df40a4
AM
8594{
8595 union aligned32
8596 {
8597 uint32_t v;
8598 unsigned char c[4];
8599 };
8600 const union aligned32 *a
0e287786 8601 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8602
8603 uint32_t aval = ( (uint32_t) a->c[0] << 24
8604 | (uint32_t) a->c[1] << 16
8605 | (uint32_t) a->c[2] << 8
8606 | (uint32_t) a->c[3]);
0e287786 8607 return aval;
53df40a4
AM
8608}
8609
8610#ifdef BFD_HOST_64_BIT
0e287786
AM
8611static bfd_vma
8612ext64l_r_offset (const void *p)
53df40a4
AM
8613{
8614 union aligned64
8615 {
8616 uint64_t v;
8617 unsigned char c[8];
8618 };
8619 const union aligned64 *a
0e287786 8620 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8621
8622 uint64_t aval = ( (uint64_t) a->c[0]
8623 | (uint64_t) a->c[1] << 8
8624 | (uint64_t) a->c[2] << 16
8625 | (uint64_t) a->c[3] << 24
8626 | (uint64_t) a->c[4] << 32
8627 | (uint64_t) a->c[5] << 40
8628 | (uint64_t) a->c[6] << 48
8629 | (uint64_t) a->c[7] << 56);
0e287786 8630 return aval;
53df40a4
AM
8631}
8632
0e287786
AM
8633static bfd_vma
8634ext64b_r_offset (const void *p)
53df40a4
AM
8635{
8636 union aligned64
8637 {
8638 uint64_t v;
8639 unsigned char c[8];
8640 };
8641 const union aligned64 *a
0e287786 8642 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8643
8644 uint64_t aval = ( (uint64_t) a->c[0] << 56
8645 | (uint64_t) a->c[1] << 48
8646 | (uint64_t) a->c[2] << 40
8647 | (uint64_t) a->c[3] << 32
8648 | (uint64_t) a->c[4] << 24
8649 | (uint64_t) a->c[5] << 16
8650 | (uint64_t) a->c[6] << 8
8651 | (uint64_t) a->c[7]);
0e287786 8652 return aval;
53df40a4
AM
8653}
8654#endif
8655
c152c796
AM
8656/* When performing a relocatable link, the input relocations are
8657 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8658 referenced must be updated. Update all the relocations found in
8659 RELDATA. */
c152c796 8660
bca6d0e3 8661static bfd_boolean
c152c796 8662elf_link_adjust_relocs (bfd *abfd,
9eaff861 8663 asection *sec,
28dbcedc 8664 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8665 bfd_boolean sort,
8666 struct bfd_link_info *info)
c152c796
AM
8667{
8668 unsigned int i;
8669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8670 bfd_byte *erela;
8671 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8672 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8673 bfd_vma r_type_mask;
8674 int r_sym_shift;
d4730f92
BS
8675 unsigned int count = reldata->count;
8676 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8677
d4730f92 8678 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8679 {
8680 swap_in = bed->s->swap_reloc_in;
8681 swap_out = bed->s->swap_reloc_out;
8682 }
d4730f92 8683 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8684 {
8685 swap_in = bed->s->swap_reloca_in;
8686 swap_out = bed->s->swap_reloca_out;
8687 }
8688 else
8689 abort ();
8690
8691 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8692 abort ();
8693
8694 if (bed->s->arch_size == 32)
8695 {
8696 r_type_mask = 0xff;
8697 r_sym_shift = 8;
8698 }
8699 else
8700 {
8701 r_type_mask = 0xffffffff;
8702 r_sym_shift = 32;
8703 }
8704
d4730f92
BS
8705 erela = reldata->hdr->contents;
8706 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8707 {
8708 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8709 unsigned int j;
8710
8711 if (*rel_hash == NULL)
8712 continue;
8713
10bbbc1d
NC
8714 if ((*rel_hash)->indx == -2
8715 && info->gc_sections
8716 && ! info->gc_keep_exported)
8717 {
8718 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
871b3ab2 8719 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection."),
10bbbc1d
NC
8720 abfd, sec,
8721 (*rel_hash)->root.root.string);
871b3ab2 8722 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled."),
d42c267e 8723 abfd, sec);
10bbbc1d
NC
8724 bfd_set_error (bfd_error_invalid_operation);
8725 return FALSE;
8726 }
c152c796
AM
8727 BFD_ASSERT ((*rel_hash)->indx >= 0);
8728
8729 (*swap_in) (abfd, erela, irela);
8730 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8731 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8732 | (irela[j].r_info & r_type_mask));
8733 (*swap_out) (abfd, irela, erela);
8734 }
53df40a4 8735
9eaff861
AO
8736 if (bed->elf_backend_update_relocs)
8737 (*bed->elf_backend_update_relocs) (sec, reldata);
8738
0e287786 8739 if (sort && count != 0)
53df40a4 8740 {
0e287786
AM
8741 bfd_vma (*ext_r_off) (const void *);
8742 bfd_vma r_off;
8743 size_t elt_size;
8744 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8745 bfd_byte *buf = NULL;
28dbcedc
AM
8746
8747 if (bed->s->arch_size == 32)
8748 {
8749 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8750 ext_r_off = ext32l_r_offset;
28dbcedc 8751 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8752 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8753 else
8754 abort ();
8755 }
53df40a4 8756 else
28dbcedc 8757 {
53df40a4 8758#ifdef BFD_HOST_64_BIT
28dbcedc 8759 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8760 ext_r_off = ext64l_r_offset;
28dbcedc 8761 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8762 ext_r_off = ext64b_r_offset;
28dbcedc 8763 else
53df40a4 8764#endif
28dbcedc
AM
8765 abort ();
8766 }
0e287786 8767
bca6d0e3
AM
8768 /* Must use a stable sort here. A modified insertion sort,
8769 since the relocs are mostly sorted already. */
0e287786
AM
8770 elt_size = reldata->hdr->sh_entsize;
8771 base = reldata->hdr->contents;
8772 end = base + count * elt_size;
bca6d0e3 8773 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8774 abort ();
8775
8776 /* Ensure the first element is lowest. This acts as a sentinel,
8777 speeding the main loop below. */
8778 r_off = (*ext_r_off) (base);
8779 for (p = loc = base; (p += elt_size) < end; )
8780 {
8781 bfd_vma r_off2 = (*ext_r_off) (p);
8782 if (r_off > r_off2)
8783 {
8784 r_off = r_off2;
8785 loc = p;
8786 }
8787 }
8788 if (loc != base)
8789 {
8790 /* Don't just swap *base and *loc as that changes the order
8791 of the original base[0] and base[1] if they happen to
8792 have the same r_offset. */
bca6d0e3
AM
8793 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8794 memcpy (onebuf, loc, elt_size);
0e287786 8795 memmove (base + elt_size, base, loc - base);
bca6d0e3 8796 memcpy (base, onebuf, elt_size);
0e287786
AM
8797 }
8798
b29b8669 8799 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8800 {
8801 /* base to p is sorted, *p is next to insert. */
8802 r_off = (*ext_r_off) (p);
8803 /* Search the sorted region for location to insert. */
8804 loc = p - elt_size;
8805 while (r_off < (*ext_r_off) (loc))
8806 loc -= elt_size;
8807 loc += elt_size;
8808 if (loc != p)
8809 {
bca6d0e3
AM
8810 /* Chances are there is a run of relocs to insert here,
8811 from one of more input files. Files are not always
8812 linked in order due to the way elf_link_input_bfd is
8813 called. See pr17666. */
8814 size_t sortlen = p - loc;
8815 bfd_vma r_off2 = (*ext_r_off) (loc);
8816 size_t runlen = elt_size;
8817 size_t buf_size = 96 * 1024;
8818 while (p + runlen < end
8819 && (sortlen <= buf_size
8820 || runlen + elt_size <= buf_size)
8821 && r_off2 > (*ext_r_off) (p + runlen))
8822 runlen += elt_size;
8823 if (buf == NULL)
8824 {
8825 buf = bfd_malloc (buf_size);
8826 if (buf == NULL)
8827 return FALSE;
8828 }
8829 if (runlen < sortlen)
8830 {
8831 memcpy (buf, p, runlen);
8832 memmove (loc + runlen, loc, sortlen);
8833 memcpy (loc, buf, runlen);
8834 }
8835 else
8836 {
8837 memcpy (buf, loc, sortlen);
8838 memmove (loc, p, runlen);
8839 memcpy (loc + runlen, buf, sortlen);
8840 }
b29b8669 8841 p += runlen - elt_size;
0e287786
AM
8842 }
8843 }
8844 /* Hashes are no longer valid. */
28dbcedc
AM
8845 free (reldata->hashes);
8846 reldata->hashes = NULL;
bca6d0e3 8847 free (buf);
53df40a4 8848 }
bca6d0e3 8849 return TRUE;
c152c796
AM
8850}
8851
8852struct elf_link_sort_rela
8853{
8854 union {
8855 bfd_vma offset;
8856 bfd_vma sym_mask;
8857 } u;
8858 enum elf_reloc_type_class type;
8859 /* We use this as an array of size int_rels_per_ext_rel. */
8860 Elf_Internal_Rela rela[1];
8861};
8862
8863static int
8864elf_link_sort_cmp1 (const void *A, const void *B)
8865{
a50b1753
NC
8866 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8867 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8868 int relativea, relativeb;
8869
8870 relativea = a->type == reloc_class_relative;
8871 relativeb = b->type == reloc_class_relative;
8872
8873 if (relativea < relativeb)
8874 return 1;
8875 if (relativea > relativeb)
8876 return -1;
8877 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8878 return -1;
8879 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8880 return 1;
8881 if (a->rela->r_offset < b->rela->r_offset)
8882 return -1;
8883 if (a->rela->r_offset > b->rela->r_offset)
8884 return 1;
8885 return 0;
8886}
8887
8888static int
8889elf_link_sort_cmp2 (const void *A, const void *B)
8890{
a50b1753
NC
8891 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8892 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8893
7e612e98 8894 if (a->type < b->type)
c152c796 8895 return -1;
7e612e98 8896 if (a->type > b->type)
c152c796 8897 return 1;
7e612e98 8898 if (a->u.offset < b->u.offset)
c152c796 8899 return -1;
7e612e98 8900 if (a->u.offset > b->u.offset)
c152c796
AM
8901 return 1;
8902 if (a->rela->r_offset < b->rela->r_offset)
8903 return -1;
8904 if (a->rela->r_offset > b->rela->r_offset)
8905 return 1;
8906 return 0;
8907}
8908
8909static size_t
8910elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8911{
3410fea8 8912 asection *dynamic_relocs;
fc66a176
L
8913 asection *rela_dyn;
8914 asection *rel_dyn;
c152c796
AM
8915 bfd_size_type count, size;
8916 size_t i, ret, sort_elt, ext_size;
8917 bfd_byte *sort, *s_non_relative, *p;
8918 struct elf_link_sort_rela *sq;
8919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8920 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8921 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8922 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8923 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8924 struct bfd_link_order *lo;
8925 bfd_vma r_sym_mask;
3410fea8 8926 bfd_boolean use_rela;
c152c796 8927
3410fea8
NC
8928 /* Find a dynamic reloc section. */
8929 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8930 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8931 if (rela_dyn != NULL && rela_dyn->size > 0
8932 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8933 {
3410fea8
NC
8934 bfd_boolean use_rela_initialised = FALSE;
8935
8936 /* This is just here to stop gcc from complaining.
c8e44c6d 8937 Its initialization checking code is not perfect. */
3410fea8
NC
8938 use_rela = TRUE;
8939
8940 /* Both sections are present. Examine the sizes
8941 of the indirect sections to help us choose. */
8942 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8943 if (lo->type == bfd_indirect_link_order)
8944 {
8945 asection *o = lo->u.indirect.section;
8946
8947 if ((o->size % bed->s->sizeof_rela) == 0)
8948 {
8949 if ((o->size % bed->s->sizeof_rel) == 0)
8950 /* Section size is divisible by both rel and rela sizes.
8951 It is of no help to us. */
8952 ;
8953 else
8954 {
8955 /* Section size is only divisible by rela. */
535b785f 8956 if (use_rela_initialised && !use_rela)
3410fea8 8957 {
871b3ab2 8958 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d
AM
8959 "they are in more than one size"),
8960 abfd);
3410fea8
NC
8961 bfd_set_error (bfd_error_invalid_operation);
8962 return 0;
8963 }
8964 else
8965 {
8966 use_rela = TRUE;
8967 use_rela_initialised = TRUE;
8968 }
8969 }
8970 }
8971 else if ((o->size % bed->s->sizeof_rel) == 0)
8972 {
8973 /* Section size is only divisible by rel. */
535b785f 8974 if (use_rela_initialised && use_rela)
3410fea8 8975 {
871b3ab2 8976 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d
AM
8977 "they are in more than one size"),
8978 abfd);
3410fea8
NC
8979 bfd_set_error (bfd_error_invalid_operation);
8980 return 0;
8981 }
8982 else
8983 {
8984 use_rela = FALSE;
8985 use_rela_initialised = TRUE;
8986 }
8987 }
8988 else
8989 {
c8e44c6d
AM
8990 /* The section size is not divisible by either -
8991 something is wrong. */
871b3ab2 8992 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d 8993 "they are of an unknown size"), abfd);
3410fea8
NC
8994 bfd_set_error (bfd_error_invalid_operation);
8995 return 0;
8996 }
8997 }
8998
8999 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9000 if (lo->type == bfd_indirect_link_order)
9001 {
9002 asection *o = lo->u.indirect.section;
9003
9004 if ((o->size % bed->s->sizeof_rela) == 0)
9005 {
9006 if ((o->size % bed->s->sizeof_rel) == 0)
9007 /* Section size is divisible by both rel and rela sizes.
9008 It is of no help to us. */
9009 ;
9010 else
9011 {
9012 /* Section size is only divisible by rela. */
535b785f 9013 if (use_rela_initialised && !use_rela)
3410fea8 9014 {
871b3ab2 9015 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d
AM
9016 "they are in more than one size"),
9017 abfd);
3410fea8
NC
9018 bfd_set_error (bfd_error_invalid_operation);
9019 return 0;
9020 }
9021 else
9022 {
9023 use_rela = TRUE;
9024 use_rela_initialised = TRUE;
9025 }
9026 }
9027 }
9028 else if ((o->size % bed->s->sizeof_rel) == 0)
9029 {
9030 /* Section size is only divisible by rel. */
535b785f 9031 if (use_rela_initialised && use_rela)
3410fea8 9032 {
871b3ab2 9033 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d
AM
9034 "they are in more than one size"),
9035 abfd);
3410fea8
NC
9036 bfd_set_error (bfd_error_invalid_operation);
9037 return 0;
9038 }
9039 else
9040 {
9041 use_rela = FALSE;
9042 use_rela_initialised = TRUE;
9043 }
9044 }
9045 else
9046 {
c8e44c6d
AM
9047 /* The section size is not divisible by either -
9048 something is wrong. */
871b3ab2 9049 _bfd_error_handler (_("%pB: Unable to sort relocs - "
c8e44c6d 9050 "they are of an unknown size"), abfd);
3410fea8
NC
9051 bfd_set_error (bfd_error_invalid_operation);
9052 return 0;
9053 }
9054 }
9055
9056 if (! use_rela_initialised)
9057 /* Make a guess. */
9058 use_rela = TRUE;
c152c796 9059 }
fc66a176
L
9060 else if (rela_dyn != NULL && rela_dyn->size > 0)
9061 use_rela = TRUE;
9062 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9063 use_rela = FALSE;
c152c796 9064 else
fc66a176 9065 return 0;
3410fea8
NC
9066
9067 if (use_rela)
c152c796 9068 {
3410fea8 9069 dynamic_relocs = rela_dyn;
c152c796
AM
9070 ext_size = bed->s->sizeof_rela;
9071 swap_in = bed->s->swap_reloca_in;
9072 swap_out = bed->s->swap_reloca_out;
9073 }
3410fea8
NC
9074 else
9075 {
9076 dynamic_relocs = rel_dyn;
9077 ext_size = bed->s->sizeof_rel;
9078 swap_in = bed->s->swap_reloc_in;
9079 swap_out = bed->s->swap_reloc_out;
9080 }
c152c796
AM
9081
9082 size = 0;
3410fea8 9083 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9084 if (lo->type == bfd_indirect_link_order)
3410fea8 9085 size += lo->u.indirect.section->size;
c152c796 9086
3410fea8 9087 if (size != dynamic_relocs->size)
c152c796
AM
9088 return 0;
9089
9090 sort_elt = (sizeof (struct elf_link_sort_rela)
9091 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9092
9093 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9094 if (count == 0)
9095 return 0;
a50b1753 9096 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9097
c152c796
AM
9098 if (sort == NULL)
9099 {
9100 (*info->callbacks->warning)
9101 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9102 return 0;
9103 }
9104
9105 if (bed->s->arch_size == 32)
9106 r_sym_mask = ~(bfd_vma) 0xff;
9107 else
9108 r_sym_mask = ~(bfd_vma) 0xffffffff;
9109
3410fea8 9110 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9111 if (lo->type == bfd_indirect_link_order)
9112 {
9113 bfd_byte *erel, *erelend;
9114 asection *o = lo->u.indirect.section;
9115
1da212d6
AM
9116 if (o->contents == NULL && o->size != 0)
9117 {
9118 /* This is a reloc section that is being handled as a normal
9119 section. See bfd_section_from_shdr. We can't combine
9120 relocs in this case. */
9121 free (sort);
9122 return 0;
9123 }
c152c796 9124 erel = o->contents;
eea6121a 9125 erelend = o->contents + o->size;
c8e44c6d 9126 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9127
c152c796
AM
9128 while (erel < erelend)
9129 {
9130 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9131
c152c796 9132 (*swap_in) (abfd, erel, s->rela);
7e612e98 9133 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9134 s->u.sym_mask = r_sym_mask;
9135 p += sort_elt;
9136 erel += ext_size;
9137 }
9138 }
9139
9140 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9141
9142 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9143 {
9144 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9145 if (s->type != reloc_class_relative)
9146 break;
9147 }
9148 ret = i;
9149 s_non_relative = p;
9150
9151 sq = (struct elf_link_sort_rela *) s_non_relative;
9152 for (; i < count; i++, p += sort_elt)
9153 {
9154 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9155 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9156 sq = sp;
9157 sp->u.offset = sq->rela->r_offset;
9158 }
9159
9160 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9161
c8e44c6d
AM
9162 struct elf_link_hash_table *htab = elf_hash_table (info);
9163 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9164 {
9165 /* We have plt relocs in .rela.dyn. */
9166 sq = (struct elf_link_sort_rela *) sort;
9167 for (i = 0; i < count; i++)
9168 if (sq[count - i - 1].type != reloc_class_plt)
9169 break;
9170 if (i != 0 && htab->srelplt->size == i * ext_size)
9171 {
9172 struct bfd_link_order **plo;
9173 /* Put srelplt link_order last. This is so the output_offset
9174 set in the next loop is correct for DT_JMPREL. */
9175 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9176 if ((*plo)->type == bfd_indirect_link_order
9177 && (*plo)->u.indirect.section == htab->srelplt)
9178 {
9179 lo = *plo;
9180 *plo = lo->next;
9181 }
9182 else
9183 plo = &(*plo)->next;
9184 *plo = lo;
9185 lo->next = NULL;
9186 dynamic_relocs->map_tail.link_order = lo;
9187 }
9188 }
9189
9190 p = sort;
3410fea8 9191 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9192 if (lo->type == bfd_indirect_link_order)
9193 {
9194 bfd_byte *erel, *erelend;
9195 asection *o = lo->u.indirect.section;
9196
9197 erel = o->contents;
eea6121a 9198 erelend = o->contents + o->size;
c8e44c6d 9199 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9200 while (erel < erelend)
9201 {
9202 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9203 (*swap_out) (abfd, s->rela, erel);
9204 p += sort_elt;
9205 erel += ext_size;
9206 }
9207 }
9208
9209 free (sort);
3410fea8 9210 *psec = dynamic_relocs;
c152c796
AM
9211 return ret;
9212}
9213
ef10c3ac 9214/* Add a symbol to the output symbol string table. */
c152c796 9215
6e0b88f1 9216static int
ef10c3ac
L
9217elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9218 const char *name,
9219 Elf_Internal_Sym *elfsym,
9220 asection *input_sec,
9221 struct elf_link_hash_entry *h)
c152c796 9222{
6e0b88f1 9223 int (*output_symbol_hook)
c152c796
AM
9224 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9225 struct elf_link_hash_entry *);
ef10c3ac 9226 struct elf_link_hash_table *hash_table;
c152c796 9227 const struct elf_backend_data *bed;
ef10c3ac 9228 bfd_size_type strtabsize;
c152c796 9229
8539e4e8
AM
9230 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9231
8b127cbc 9232 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9233 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9234 if (output_symbol_hook != NULL)
9235 {
8b127cbc 9236 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9237 if (ret != 1)
9238 return ret;
c152c796
AM
9239 }
9240
ef10c3ac
L
9241 if (name == NULL
9242 || *name == '\0'
9243 || (input_sec->flags & SEC_EXCLUDE))
9244 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9245 else
9246 {
ef10c3ac
L
9247 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9248 to get the final offset for st_name. */
9249 elfsym->st_name
9250 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9251 name, FALSE);
c152c796 9252 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9253 return 0;
c152c796
AM
9254 }
9255
ef10c3ac
L
9256 hash_table = elf_hash_table (flinfo->info);
9257 strtabsize = hash_table->strtabsize;
9258 if (strtabsize <= hash_table->strtabcount)
c152c796 9259 {
ef10c3ac
L
9260 strtabsize += strtabsize;
9261 hash_table->strtabsize = strtabsize;
9262 strtabsize *= sizeof (*hash_table->strtab);
9263 hash_table->strtab
9264 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9265 strtabsize);
9266 if (hash_table->strtab == NULL)
6e0b88f1 9267 return 0;
c152c796 9268 }
ef10c3ac
L
9269 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9270 hash_table->strtab[hash_table->strtabcount].dest_index
9271 = hash_table->strtabcount;
9272 hash_table->strtab[hash_table->strtabcount].destshndx_index
9273 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9274
9275 bfd_get_symcount (flinfo->output_bfd) += 1;
9276 hash_table->strtabcount += 1;
9277
9278 return 1;
9279}
9280
9281/* Swap symbols out to the symbol table and flush the output symbols to
9282 the file. */
9283
9284static bfd_boolean
9285elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9286{
9287 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9288 bfd_size_type amt;
9289 size_t i;
ef10c3ac
L
9290 const struct elf_backend_data *bed;
9291 bfd_byte *symbuf;
9292 Elf_Internal_Shdr *hdr;
9293 file_ptr pos;
9294 bfd_boolean ret;
9295
9296 if (!hash_table->strtabcount)
9297 return TRUE;
9298
9299 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9300
9301 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9302
ef10c3ac
L
9303 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9304 symbuf = (bfd_byte *) bfd_malloc (amt);
9305 if (symbuf == NULL)
9306 return FALSE;
1b786873 9307
ef10c3ac 9308 if (flinfo->symshndxbuf)
c152c796 9309 {
ef53be89
AM
9310 amt = sizeof (Elf_External_Sym_Shndx);
9311 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9312 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9313 if (flinfo->symshndxbuf == NULL)
c152c796 9314 {
ef10c3ac
L
9315 free (symbuf);
9316 return FALSE;
c152c796 9317 }
c152c796
AM
9318 }
9319
ef10c3ac
L
9320 for (i = 0; i < hash_table->strtabcount; i++)
9321 {
9322 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9323 if (elfsym->sym.st_name == (unsigned long) -1)
9324 elfsym->sym.st_name = 0;
9325 else
9326 elfsym->sym.st_name
9327 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9328 elfsym->sym.st_name);
9329 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9330 ((bfd_byte *) symbuf
9331 + (elfsym->dest_index
9332 * bed->s->sizeof_sym)),
9333 (flinfo->symshndxbuf
9334 + elfsym->destshndx_index));
9335 }
9336
9337 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9338 pos = hdr->sh_offset + hdr->sh_size;
9339 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9340 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9341 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9342 {
9343 hdr->sh_size += amt;
9344 ret = TRUE;
9345 }
9346 else
9347 ret = FALSE;
c152c796 9348
ef10c3ac
L
9349 free (symbuf);
9350
9351 free (hash_table->strtab);
9352 hash_table->strtab = NULL;
9353
9354 return ret;
c152c796
AM
9355}
9356
c0d5a53d
L
9357/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9358
9359static bfd_boolean
9360check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9361{
4fbb74a6
AM
9362 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9363 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9364 {
9365 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9366 beyond 64k. */
4eca0228 9367 _bfd_error_handler
695344c0 9368 /* xgettext:c-format */
871b3ab2 9369 (_("%pB: Too many sections: %d (>= %d)"),
4fbb74a6 9370 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9371 bfd_set_error (bfd_error_nonrepresentable_section);
9372 return FALSE;
9373 }
9374 return TRUE;
9375}
9376
c152c796
AM
9377/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9378 allowing an unsatisfied unversioned symbol in the DSO to match a
9379 versioned symbol that would normally require an explicit version.
9380 We also handle the case that a DSO references a hidden symbol
9381 which may be satisfied by a versioned symbol in another DSO. */
9382
9383static bfd_boolean
9384elf_link_check_versioned_symbol (struct bfd_link_info *info,
9385 const struct elf_backend_data *bed,
9386 struct elf_link_hash_entry *h)
9387{
9388 bfd *abfd;
9389 struct elf_link_loaded_list *loaded;
9390
9391 if (!is_elf_hash_table (info->hash))
9392 return FALSE;
9393
90c984fc
L
9394 /* Check indirect symbol. */
9395 while (h->root.type == bfd_link_hash_indirect)
9396 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9397
c152c796
AM
9398 switch (h->root.type)
9399 {
9400 default:
9401 abfd = NULL;
9402 break;
9403
9404 case bfd_link_hash_undefined:
9405 case bfd_link_hash_undefweak:
9406 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9407 if (abfd == NULL
9408 || (abfd->flags & DYNAMIC) == 0
e56f61be 9409 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9410 return FALSE;
9411 break;
9412
9413 case bfd_link_hash_defined:
9414 case bfd_link_hash_defweak:
9415 abfd = h->root.u.def.section->owner;
9416 break;
9417
9418 case bfd_link_hash_common:
9419 abfd = h->root.u.c.p->section->owner;
9420 break;
9421 }
9422 BFD_ASSERT (abfd != NULL);
9423
9424 for (loaded = elf_hash_table (info)->loaded;
9425 loaded != NULL;
9426 loaded = loaded->next)
9427 {
9428 bfd *input;
9429 Elf_Internal_Shdr *hdr;
ef53be89
AM
9430 size_t symcount;
9431 size_t extsymcount;
9432 size_t extsymoff;
c152c796
AM
9433 Elf_Internal_Shdr *versymhdr;
9434 Elf_Internal_Sym *isym;
9435 Elf_Internal_Sym *isymend;
9436 Elf_Internal_Sym *isymbuf;
9437 Elf_External_Versym *ever;
9438 Elf_External_Versym *extversym;
9439
9440 input = loaded->abfd;
9441
9442 /* We check each DSO for a possible hidden versioned definition. */
9443 if (input == abfd
9444 || (input->flags & DYNAMIC) == 0
9445 || elf_dynversym (input) == 0)
9446 continue;
9447
9448 hdr = &elf_tdata (input)->dynsymtab_hdr;
9449
9450 symcount = hdr->sh_size / bed->s->sizeof_sym;
9451 if (elf_bad_symtab (input))
9452 {
9453 extsymcount = symcount;
9454 extsymoff = 0;
9455 }
9456 else
9457 {
9458 extsymcount = symcount - hdr->sh_info;
9459 extsymoff = hdr->sh_info;
9460 }
9461
9462 if (extsymcount == 0)
9463 continue;
9464
9465 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9466 NULL, NULL, NULL);
9467 if (isymbuf == NULL)
9468 return FALSE;
9469
9470 /* Read in any version definitions. */
9471 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9472 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9473 if (extversym == NULL)
9474 goto error_ret;
9475
9476 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9477 || (bfd_bread (extversym, versymhdr->sh_size, input)
9478 != versymhdr->sh_size))
9479 {
9480 free (extversym);
9481 error_ret:
9482 free (isymbuf);
9483 return FALSE;
9484 }
9485
9486 ever = extversym + extsymoff;
9487 isymend = isymbuf + extsymcount;
9488 for (isym = isymbuf; isym < isymend; isym++, ever++)
9489 {
9490 const char *name;
9491 Elf_Internal_Versym iver;
9492 unsigned short version_index;
9493
9494 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9495 || isym->st_shndx == SHN_UNDEF)
9496 continue;
9497
9498 name = bfd_elf_string_from_elf_section (input,
9499 hdr->sh_link,
9500 isym->st_name);
9501 if (strcmp (name, h->root.root.string) != 0)
9502 continue;
9503
9504 _bfd_elf_swap_versym_in (input, ever, &iver);
9505
d023c380
L
9506 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9507 && !(h->def_regular
9508 && h->forced_local))
c152c796
AM
9509 {
9510 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9511 have provided a definition for the undefined sym unless
9512 it is defined in a non-shared object and forced local.
9513 */
c152c796
AM
9514 abort ();
9515 }
9516
9517 version_index = iver.vs_vers & VERSYM_VERSION;
9518 if (version_index == 1 || version_index == 2)
9519 {
9520 /* This is the base or first version. We can use it. */
9521 free (extversym);
9522 free (isymbuf);
9523 return TRUE;
9524 }
9525 }
9526
9527 free (extversym);
9528 free (isymbuf);
9529 }
9530
9531 return FALSE;
9532}
9533
b8871f35
L
9534/* Convert ELF common symbol TYPE. */
9535
9536static int
9537elf_link_convert_common_type (struct bfd_link_info *info, int type)
9538{
9539 /* Commom symbol can only appear in relocatable link. */
9540 if (!bfd_link_relocatable (info))
9541 abort ();
9542 switch (info->elf_stt_common)
9543 {
9544 case unchanged:
9545 break;
9546 case elf_stt_common:
9547 type = STT_COMMON;
9548 break;
9549 case no_elf_stt_common:
9550 type = STT_OBJECT;
9551 break;
9552 }
9553 return type;
9554}
9555
c152c796
AM
9556/* Add an external symbol to the symbol table. This is called from
9557 the hash table traversal routine. When generating a shared object,
9558 we go through the symbol table twice. The first time we output
9559 anything that might have been forced to local scope in a version
9560 script. The second time we output the symbols that are still
9561 global symbols. */
9562
9563static bfd_boolean
7686d77d 9564elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9565{
7686d77d 9566 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9567 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9568 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9569 bfd_boolean strip;
9570 Elf_Internal_Sym sym;
9571 asection *input_sec;
9572 const struct elf_backend_data *bed;
6e0b88f1
AM
9573 long indx;
9574 int ret;
b8871f35 9575 unsigned int type;
c152c796
AM
9576
9577 if (h->root.type == bfd_link_hash_warning)
9578 {
9579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9580 if (h->root.type == bfd_link_hash_new)
9581 return TRUE;
9582 }
9583
9584 /* Decide whether to output this symbol in this pass. */
9585 if (eoinfo->localsyms)
9586 {
4deb8f71 9587 if (!h->forced_local)
c152c796
AM
9588 return TRUE;
9589 }
9590 else
9591 {
4deb8f71 9592 if (h->forced_local)
c152c796
AM
9593 return TRUE;
9594 }
9595
8b127cbc 9596 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9597
12ac1cf5 9598 if (h->root.type == bfd_link_hash_undefined)
c152c796 9599 {
12ac1cf5
NC
9600 /* If we have an undefined symbol reference here then it must have
9601 come from a shared library that is being linked in. (Undefined
98da7939
L
9602 references in regular files have already been handled unless
9603 they are in unreferenced sections which are removed by garbage
9604 collection). */
12ac1cf5
NC
9605 bfd_boolean ignore_undef = FALSE;
9606
9607 /* Some symbols may be special in that the fact that they're
9608 undefined can be safely ignored - let backend determine that. */
9609 if (bed->elf_backend_ignore_undef_symbol)
9610 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9611
9612 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9613 if (!ignore_undef
12ac1cf5 9614 && h->ref_dynamic
8b127cbc
AM
9615 && (!h->ref_regular || flinfo->info->gc_sections)
9616 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9617 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9618 (*flinfo->info->callbacks->undefined_symbol)
9619 (flinfo->info, h->root.root.string,
9620 h->ref_regular ? NULL : h->root.u.undef.abfd,
9621 NULL, 0,
9622 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9623
9624 /* Strip a global symbol defined in a discarded section. */
9625 if (h->indx == -3)
9626 return TRUE;
c152c796
AM
9627 }
9628
9629 /* We should also warn if a forced local symbol is referenced from
9630 shared libraries. */
0e1862bb 9631 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9632 && h->forced_local
9633 && h->ref_dynamic
371a5866 9634 && h->def_regular
f5385ebf 9635 && !h->dynamic_def
ee659f1f 9636 && h->ref_dynamic_nonweak
8b127cbc 9637 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9638 {
17d078c5
AM
9639 bfd *def_bfd;
9640 const char *msg;
90c984fc
L
9641 struct elf_link_hash_entry *hi = h;
9642
9643 /* Check indirect symbol. */
9644 while (hi->root.type == bfd_link_hash_indirect)
9645 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9646
9647 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9648 /* xgettext:c-format */
871b3ab2 9649 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9650 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9651 /* xgettext:c-format */
871b3ab2 9652 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9653 else
695344c0 9654 /* xgettext:c-format */
871b3ab2 9655 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9656 def_bfd = flinfo->output_bfd;
90c984fc
L
9657 if (hi->root.u.def.section != bfd_abs_section_ptr)
9658 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9659 _bfd_error_handler (msg, flinfo->output_bfd,
9660 h->root.root.string, def_bfd);
17d078c5 9661 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9662 eoinfo->failed = TRUE;
9663 return FALSE;
9664 }
9665
9666 /* We don't want to output symbols that have never been mentioned by
9667 a regular file, or that we have been told to strip. However, if
9668 h->indx is set to -2, the symbol is used by a reloc and we must
9669 output it. */
d983c8c5 9670 strip = FALSE;
c152c796 9671 if (h->indx == -2)
d983c8c5 9672 ;
f5385ebf 9673 else if ((h->def_dynamic
77cfaee6
AM
9674 || h->ref_dynamic
9675 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9676 && !h->def_regular
9677 && !h->ref_regular)
c152c796 9678 strip = TRUE;
8b127cbc 9679 else if (flinfo->info->strip == strip_all)
c152c796 9680 strip = TRUE;
8b127cbc
AM
9681 else if (flinfo->info->strip == strip_some
9682 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9683 h->root.root.string, FALSE, FALSE) == NULL)
9684 strip = TRUE;
d56d55e7
AM
9685 else if ((h->root.type == bfd_link_hash_defined
9686 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9687 && ((flinfo->info->strip_discarded
dbaa2011 9688 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9689 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9690 && h->root.u.def.section->owner != NULL
d56d55e7 9691 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9692 strip = TRUE;
9e2278f5
AM
9693 else if ((h->root.type == bfd_link_hash_undefined
9694 || h->root.type == bfd_link_hash_undefweak)
9695 && h->root.u.undef.abfd != NULL
9696 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9697 strip = TRUE;
c152c796 9698
b8871f35
L
9699 type = h->type;
9700
c152c796 9701 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9702 nothing else to do. However, if it is a forced local symbol or
9703 an ifunc symbol we need to give the backend finish_dynamic_symbol
9704 function a chance to make it dynamic. */
c152c796
AM
9705 if (strip
9706 && h->dynindx == -1
b8871f35 9707 && type != STT_GNU_IFUNC
f5385ebf 9708 && !h->forced_local)
c152c796
AM
9709 return TRUE;
9710
9711 sym.st_value = 0;
9712 sym.st_size = h->size;
9713 sym.st_other = h->other;
c152c796
AM
9714 switch (h->root.type)
9715 {
9716 default:
9717 case bfd_link_hash_new:
9718 case bfd_link_hash_warning:
9719 abort ();
9720 return FALSE;
9721
9722 case bfd_link_hash_undefined:
9723 case bfd_link_hash_undefweak:
9724 input_sec = bfd_und_section_ptr;
9725 sym.st_shndx = SHN_UNDEF;
9726 break;
9727
9728 case bfd_link_hash_defined:
9729 case bfd_link_hash_defweak:
9730 {
9731 input_sec = h->root.u.def.section;
9732 if (input_sec->output_section != NULL)
9733 {
9734 sym.st_shndx =
8b127cbc 9735 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9736 input_sec->output_section);
9737 if (sym.st_shndx == SHN_BAD)
9738 {
4eca0228 9739 _bfd_error_handler
695344c0 9740 /* xgettext:c-format */
871b3ab2 9741 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9742 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9743 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9744 eoinfo->failed = TRUE;
9745 return FALSE;
9746 }
9747
9748 /* ELF symbols in relocatable files are section relative,
9749 but in nonrelocatable files they are virtual
9750 addresses. */
9751 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9752 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9753 {
9754 sym.st_value += input_sec->output_section->vma;
9755 if (h->type == STT_TLS)
9756 {
8b127cbc 9757 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9758 if (tls_sec != NULL)
9759 sym.st_value -= tls_sec->vma;
c152c796
AM
9760 }
9761 }
9762 }
9763 else
9764 {
9765 BFD_ASSERT (input_sec->owner == NULL
9766 || (input_sec->owner->flags & DYNAMIC) != 0);
9767 sym.st_shndx = SHN_UNDEF;
9768 input_sec = bfd_und_section_ptr;
9769 }
9770 }
9771 break;
9772
9773 case bfd_link_hash_common:
9774 input_sec = h->root.u.c.p->section;
a4d8e49b 9775 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9776 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9777 break;
9778
9779 case bfd_link_hash_indirect:
9780 /* These symbols are created by symbol versioning. They point
9781 to the decorated version of the name. For example, if the
9782 symbol foo@@GNU_1.2 is the default, which should be used when
9783 foo is used with no version, then we add an indirect symbol
9784 foo which points to foo@@GNU_1.2. We ignore these symbols,
9785 since the indirected symbol is already in the hash table. */
9786 return TRUE;
9787 }
9788
b8871f35
L
9789 if (type == STT_COMMON || type == STT_OBJECT)
9790 switch (h->root.type)
9791 {
9792 case bfd_link_hash_common:
9793 type = elf_link_convert_common_type (flinfo->info, type);
9794 break;
9795 case bfd_link_hash_defined:
9796 case bfd_link_hash_defweak:
9797 if (bed->common_definition (&sym))
9798 type = elf_link_convert_common_type (flinfo->info, type);
9799 else
9800 type = STT_OBJECT;
9801 break;
9802 case bfd_link_hash_undefined:
9803 case bfd_link_hash_undefweak:
9804 break;
9805 default:
9806 abort ();
9807 }
9808
4deb8f71 9809 if (h->forced_local)
b8871f35
L
9810 {
9811 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9812 /* Turn off visibility on local symbol. */
9813 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9814 }
9815 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9816 else if (h->unique_global && h->def_regular)
9817 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9818 else if (h->root.type == bfd_link_hash_undefweak
9819 || h->root.type == bfd_link_hash_defweak)
9820 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9821 else
9822 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9823 sym.st_target_internal = h->target_internal;
9824
c152c796
AM
9825 /* Give the processor backend a chance to tweak the symbol value,
9826 and also to finish up anything that needs to be done for this
9827 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9828 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9829 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9830 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9831 && h->def_regular
0e1862bb 9832 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9833 || ((h->dynindx != -1
9834 || h->forced_local)
0e1862bb 9835 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9836 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9837 || h->root.type != bfd_link_hash_undefweak))
9838 || !h->forced_local)
8b127cbc 9839 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9840 {
9841 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9842 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9843 {
9844 eoinfo->failed = TRUE;
9845 return FALSE;
9846 }
9847 }
9848
9849 /* If we are marking the symbol as undefined, and there are no
9850 non-weak references to this symbol from a regular object, then
9851 mark the symbol as weak undefined; if there are non-weak
9852 references, mark the symbol as strong. We can't do this earlier,
9853 because it might not be marked as undefined until the
9854 finish_dynamic_symbol routine gets through with it. */
9855 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9856 && h->ref_regular
c152c796
AM
9857 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9858 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9859 {
9860 int bindtype;
b8871f35 9861 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9862
9863 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9864 if (type == STT_GNU_IFUNC)
9865 type = STT_FUNC;
c152c796 9866
f5385ebf 9867 if (h->ref_regular_nonweak)
c152c796
AM
9868 bindtype = STB_GLOBAL;
9869 else
9870 bindtype = STB_WEAK;
2955ec4c 9871 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9872 }
9873
bda987c2
CD
9874 /* If this is a symbol defined in a dynamic library, don't use the
9875 symbol size from the dynamic library. Relinking an executable
9876 against a new library may introduce gratuitous changes in the
9877 executable's symbols if we keep the size. */
9878 if (sym.st_shndx == SHN_UNDEF
9879 && !h->def_regular
9880 && h->def_dynamic)
9881 sym.st_size = 0;
9882
c152c796
AM
9883 /* If a non-weak symbol with non-default visibility is not defined
9884 locally, it is a fatal error. */
0e1862bb 9885 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9886 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9887 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9888 && h->root.type == bfd_link_hash_undefined
f5385ebf 9889 && !h->def_regular)
c152c796 9890 {
17d078c5
AM
9891 const char *msg;
9892
9893 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9894 /* xgettext:c-format */
871b3ab2 9895 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 9896 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9897 /* xgettext:c-format */
871b3ab2 9898 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 9899 else
695344c0 9900 /* xgettext:c-format */
871b3ab2 9901 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 9902 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9903 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9904 eoinfo->failed = TRUE;
9905 return FALSE;
9906 }
9907
9908 /* If this symbol should be put in the .dynsym section, then put it
9909 there now. We already know the symbol index. We also fill in
9910 the entry in the .hash section. */
cae1fbbb 9911 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9912 && h->dynindx != -1
8b127cbc 9913 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9914 {
c152c796
AM
9915 bfd_byte *esym;
9916
90c984fc
L
9917 /* Since there is no version information in the dynamic string,
9918 if there is no version info in symbol version section, we will
1659f720 9919 have a run-time problem if not linking executable, referenced
4deb8f71 9920 by shared library, or not bound locally. */
1659f720 9921 if (h->verinfo.verdef == NULL
0e1862bb 9922 && (!bfd_link_executable (flinfo->info)
1659f720
L
9923 || h->ref_dynamic
9924 || !h->def_regular))
90c984fc
L
9925 {
9926 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9927
9928 if (p && p [1] != '\0')
9929 {
4eca0228 9930 _bfd_error_handler
695344c0 9931 /* xgettext:c-format */
871b3ab2 9932 (_("%pB: No symbol version section for versioned symbol `%s'"),
90c984fc
L
9933 flinfo->output_bfd, h->root.root.string);
9934 eoinfo->failed = TRUE;
9935 return FALSE;
9936 }
9937 }
9938
c152c796 9939 sym.st_name = h->dynstr_index;
cae1fbbb
L
9940 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9941 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9942 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9943 {
9944 eoinfo->failed = TRUE;
9945 return FALSE;
9946 }
8b127cbc 9947 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9948
8b127cbc 9949 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9950 {
9951 size_t hash_entry_size;
9952 bfd_byte *bucketpos;
9953 bfd_vma chain;
41198d0c
L
9954 size_t bucketcount;
9955 size_t bucket;
9956
8b127cbc 9957 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9958 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9959
9960 hash_entry_size
8b127cbc
AM
9961 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9962 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9963 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9964 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9965 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9966 bucketpos);
9967 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9968 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9969 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9970 }
c152c796 9971
8b127cbc 9972 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9973 {
9974 Elf_Internal_Versym iversym;
9975 Elf_External_Versym *eversym;
9976
f5385ebf 9977 if (!h->def_regular)
c152c796 9978 {
7b20f099
AM
9979 if (h->verinfo.verdef == NULL
9980 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9981 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9982 iversym.vs_vers = 0;
9983 else
9984 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9985 }
9986 else
9987 {
9988 if (h->verinfo.vertree == NULL)
9989 iversym.vs_vers = 1;
9990 else
9991 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9992 if (flinfo->info->create_default_symver)
3e3b46e5 9993 iversym.vs_vers++;
c152c796
AM
9994 }
9995
422f1182 9996 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9997 defined locally. */
422f1182 9998 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9999 iversym.vs_vers |= VERSYM_HIDDEN;
10000
8b127cbc 10001 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10002 eversym += h->dynindx;
8b127cbc 10003 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10004 }
10005 }
10006
d983c8c5
AM
10007 /* If the symbol is undefined, and we didn't output it to .dynsym,
10008 strip it from .symtab too. Obviously we can't do this for
10009 relocatable output or when needed for --emit-relocs. */
10010 else if (input_sec == bfd_und_section_ptr
10011 && h->indx != -2
66cae560
NC
10012 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10013 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10014 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10015 return TRUE;
66cae560 10016
d983c8c5
AM
10017 /* Also strip others that we couldn't earlier due to dynamic symbol
10018 processing. */
10019 if (strip)
10020 return TRUE;
10021 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10022 return TRUE;
10023
2ec55de3
AM
10024 /* Output a FILE symbol so that following locals are not associated
10025 with the wrong input file. We need one for forced local symbols
10026 if we've seen more than one FILE symbol or when we have exactly
10027 one FILE symbol but global symbols are present in a file other
10028 than the one with the FILE symbol. We also need one if linker
10029 defined symbols are present. In practice these conditions are
10030 always met, so just emit the FILE symbol unconditionally. */
10031 if (eoinfo->localsyms
10032 && !eoinfo->file_sym_done
10033 && eoinfo->flinfo->filesym_count != 0)
10034 {
10035 Elf_Internal_Sym fsym;
10036
10037 memset (&fsym, 0, sizeof (fsym));
10038 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10039 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10040 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10041 bfd_und_section_ptr, NULL))
2ec55de3
AM
10042 return FALSE;
10043
10044 eoinfo->file_sym_done = TRUE;
10045 }
10046
8b127cbc 10047 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10048 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10049 input_sec, h);
6e0b88f1 10050 if (ret == 0)
c152c796
AM
10051 {
10052 eoinfo->failed = TRUE;
10053 return FALSE;
10054 }
6e0b88f1
AM
10055 else if (ret == 1)
10056 h->indx = indx;
10057 else if (h->indx == -2)
10058 abort();
c152c796
AM
10059
10060 return TRUE;
10061}
10062
cdd3575c
AM
10063/* Return TRUE if special handling is done for relocs in SEC against
10064 symbols defined in discarded sections. */
10065
c152c796
AM
10066static bfd_boolean
10067elf_section_ignore_discarded_relocs (asection *sec)
10068{
10069 const struct elf_backend_data *bed;
10070
cdd3575c
AM
10071 switch (sec->sec_info_type)
10072 {
dbaa2011
AM
10073 case SEC_INFO_TYPE_STABS:
10074 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10075 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10076 return TRUE;
10077 default:
10078 break;
10079 }
c152c796
AM
10080
10081 bed = get_elf_backend_data (sec->owner);
10082 if (bed->elf_backend_ignore_discarded_relocs != NULL
10083 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10084 return TRUE;
10085
10086 return FALSE;
10087}
10088
9e66c942
AM
10089/* Return a mask saying how ld should treat relocations in SEC against
10090 symbols defined in discarded sections. If this function returns
10091 COMPLAIN set, ld will issue a warning message. If this function
10092 returns PRETEND set, and the discarded section was link-once and the
10093 same size as the kept link-once section, ld will pretend that the
10094 symbol was actually defined in the kept section. Otherwise ld will
10095 zero the reloc (at least that is the intent, but some cooperation by
10096 the target dependent code is needed, particularly for REL targets). */
10097
8a696751
AM
10098unsigned int
10099_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10100{
9e66c942 10101 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10102 return PRETEND;
cdd3575c
AM
10103
10104 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10105 return 0;
cdd3575c
AM
10106
10107 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10108 return 0;
cdd3575c 10109
9e66c942 10110 return COMPLAIN | PRETEND;
cdd3575c
AM
10111}
10112
3d7f7666
L
10113/* Find a match between a section and a member of a section group. */
10114
10115static asection *
c0f00686
L
10116match_group_member (asection *sec, asection *group,
10117 struct bfd_link_info *info)
3d7f7666
L
10118{
10119 asection *first = elf_next_in_group (group);
10120 asection *s = first;
10121
10122 while (s != NULL)
10123 {
c0f00686 10124 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10125 return s;
10126
83180ade 10127 s = elf_next_in_group (s);
3d7f7666
L
10128 if (s == first)
10129 break;
10130 }
10131
10132 return NULL;
10133}
10134
01b3c8ab 10135/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10136 to replace it. Return the replacement if it is OK. Otherwise return
10137 NULL. */
01b3c8ab
L
10138
10139asection *
c0f00686 10140_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10141{
10142 asection *kept;
10143
10144 kept = sec->kept_section;
10145 if (kept != NULL)
10146 {
c2370991 10147 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10148 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10149 if (kept != NULL
10150 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10151 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10152 kept = NULL;
c2370991 10153 sec->kept_section = kept;
01b3c8ab
L
10154 }
10155 return kept;
10156}
10157
c152c796
AM
10158/* Link an input file into the linker output file. This function
10159 handles all the sections and relocations of the input file at once.
10160 This is so that we only have to read the local symbols once, and
10161 don't have to keep them in memory. */
10162
10163static bfd_boolean
8b127cbc 10164elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10165{
ece5ef60 10166 int (*relocate_section)
c152c796
AM
10167 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10168 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10169 bfd *output_bfd;
10170 Elf_Internal_Shdr *symtab_hdr;
10171 size_t locsymcount;
10172 size_t extsymoff;
10173 Elf_Internal_Sym *isymbuf;
10174 Elf_Internal_Sym *isym;
10175 Elf_Internal_Sym *isymend;
10176 long *pindex;
10177 asection **ppsection;
10178 asection *o;
10179 const struct elf_backend_data *bed;
c152c796 10180 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10181 bfd_size_type address_size;
10182 bfd_vma r_type_mask;
10183 int r_sym_shift;
ffbc01cc 10184 bfd_boolean have_file_sym = FALSE;
c152c796 10185
8b127cbc 10186 output_bfd = flinfo->output_bfd;
c152c796
AM
10187 bed = get_elf_backend_data (output_bfd);
10188 relocate_section = bed->elf_backend_relocate_section;
10189
10190 /* If this is a dynamic object, we don't want to do anything here:
10191 we don't want the local symbols, and we don't want the section
10192 contents. */
10193 if ((input_bfd->flags & DYNAMIC) != 0)
10194 return TRUE;
10195
c152c796
AM
10196 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10197 if (elf_bad_symtab (input_bfd))
10198 {
10199 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10200 extsymoff = 0;
10201 }
10202 else
10203 {
10204 locsymcount = symtab_hdr->sh_info;
10205 extsymoff = symtab_hdr->sh_info;
10206 }
10207
10208 /* Read the local symbols. */
10209 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10210 if (isymbuf == NULL && locsymcount != 0)
10211 {
10212 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10213 flinfo->internal_syms,
10214 flinfo->external_syms,
10215 flinfo->locsym_shndx);
c152c796
AM
10216 if (isymbuf == NULL)
10217 return FALSE;
10218 }
10219
10220 /* Find local symbol sections and adjust values of symbols in
10221 SEC_MERGE sections. Write out those local symbols we know are
10222 going into the output file. */
10223 isymend = isymbuf + locsymcount;
8b127cbc 10224 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10225 isym < isymend;
10226 isym++, pindex++, ppsection++)
10227 {
10228 asection *isec;
10229 const char *name;
10230 Elf_Internal_Sym osym;
6e0b88f1
AM
10231 long indx;
10232 int ret;
c152c796
AM
10233
10234 *pindex = -1;
10235
10236 if (elf_bad_symtab (input_bfd))
10237 {
10238 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10239 {
10240 *ppsection = NULL;
10241 continue;
10242 }
10243 }
10244
10245 if (isym->st_shndx == SHN_UNDEF)
10246 isec = bfd_und_section_ptr;
c152c796
AM
10247 else if (isym->st_shndx == SHN_ABS)
10248 isec = bfd_abs_section_ptr;
10249 else if (isym->st_shndx == SHN_COMMON)
10250 isec = bfd_com_section_ptr;
10251 else
10252 {
cb33740c
AM
10253 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10254 if (isec == NULL)
10255 {
10256 /* Don't attempt to output symbols with st_shnx in the
10257 reserved range other than SHN_ABS and SHN_COMMON. */
10258 *ppsection = NULL;
10259 continue;
10260 }
dbaa2011 10261 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10262 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10263 isym->st_value =
10264 _bfd_merged_section_offset (output_bfd, &isec,
10265 elf_section_data (isec)->sec_info,
10266 isym->st_value);
c152c796
AM
10267 }
10268
10269 *ppsection = isec;
10270
d983c8c5
AM
10271 /* Don't output the first, undefined, symbol. In fact, don't
10272 output any undefined local symbol. */
10273 if (isec == bfd_und_section_ptr)
c152c796
AM
10274 continue;
10275
10276 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10277 {
10278 /* We never output section symbols. Instead, we use the
10279 section symbol of the corresponding section in the output
10280 file. */
10281 continue;
10282 }
10283
10284 /* If we are stripping all symbols, we don't want to output this
10285 one. */
8b127cbc 10286 if (flinfo->info->strip == strip_all)
c152c796
AM
10287 continue;
10288
10289 /* If we are discarding all local symbols, we don't want to
10290 output this one. If we are generating a relocatable output
10291 file, then some of the local symbols may be required by
10292 relocs; we output them below as we discover that they are
10293 needed. */
8b127cbc 10294 if (flinfo->info->discard == discard_all)
c152c796
AM
10295 continue;
10296
10297 /* If this symbol is defined in a section which we are
f02571c5
AM
10298 discarding, we don't need to keep it. */
10299 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10300 && isym->st_shndx < SHN_LORESERVE
10301 && bfd_section_removed_from_list (output_bfd,
10302 isec->output_section))
e75a280b
L
10303 continue;
10304
c152c796
AM
10305 /* Get the name of the symbol. */
10306 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10307 isym->st_name);
10308 if (name == NULL)
10309 return FALSE;
10310
10311 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10312 if ((flinfo->info->strip == strip_some
10313 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10314 == NULL))
8b127cbc 10315 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10316 && (isec->flags & SEC_MERGE)
10317 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10318 || flinfo->info->discard == discard_l)
c152c796
AM
10319 && bfd_is_local_label_name (input_bfd, name)))
10320 continue;
10321
ffbc01cc
AM
10322 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10323 {
ce875075
AM
10324 if (input_bfd->lto_output)
10325 /* -flto puts a temp file name here. This means builds
10326 are not reproducible. Discard the symbol. */
10327 continue;
ffbc01cc
AM
10328 have_file_sym = TRUE;
10329 flinfo->filesym_count += 1;
10330 }
10331 if (!have_file_sym)
10332 {
10333 /* In the absence of debug info, bfd_find_nearest_line uses
10334 FILE symbols to determine the source file for local
10335 function symbols. Provide a FILE symbol here if input
10336 files lack such, so that their symbols won't be
10337 associated with a previous input file. It's not the
10338 source file, but the best we can do. */
10339 have_file_sym = TRUE;
10340 flinfo->filesym_count += 1;
10341 memset (&osym, 0, sizeof (osym));
10342 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10343 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10344 if (!elf_link_output_symstrtab (flinfo,
10345 (input_bfd->lto_output ? NULL
10346 : input_bfd->filename),
10347 &osym, bfd_abs_section_ptr,
10348 NULL))
ffbc01cc
AM
10349 return FALSE;
10350 }
10351
c152c796
AM
10352 osym = *isym;
10353
10354 /* Adjust the section index for the output file. */
10355 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10356 isec->output_section);
10357 if (osym.st_shndx == SHN_BAD)
10358 return FALSE;
10359
c152c796
AM
10360 /* ELF symbols in relocatable files are section relative, but
10361 in executable files they are virtual addresses. Note that
10362 this code assumes that all ELF sections have an associated
10363 BFD section with a reasonable value for output_offset; below
10364 we assume that they also have a reasonable value for
10365 output_section. Any special sections must be set up to meet
10366 these requirements. */
10367 osym.st_value += isec->output_offset;
0e1862bb 10368 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10369 {
10370 osym.st_value += isec->output_section->vma;
10371 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10372 {
10373 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10374 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10375 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10376 }
10377 }
10378
6e0b88f1 10379 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10380 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10381 if (ret == 0)
c152c796 10382 return FALSE;
6e0b88f1
AM
10383 else if (ret == 1)
10384 *pindex = indx;
c152c796
AM
10385 }
10386
310fd250
L
10387 if (bed->s->arch_size == 32)
10388 {
10389 r_type_mask = 0xff;
10390 r_sym_shift = 8;
10391 address_size = 4;
10392 }
10393 else
10394 {
10395 r_type_mask = 0xffffffff;
10396 r_sym_shift = 32;
10397 address_size = 8;
10398 }
10399
c152c796
AM
10400 /* Relocate the contents of each section. */
10401 sym_hashes = elf_sym_hashes (input_bfd);
10402 for (o = input_bfd->sections; o != NULL; o = o->next)
10403 {
10404 bfd_byte *contents;
10405
10406 if (! o->linker_mark)
10407 {
10408 /* This section was omitted from the link. */
10409 continue;
10410 }
10411
7bdf4127 10412 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10413 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10414 {
10415 /* Deal with the group signature symbol. */
10416 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10417 unsigned long symndx = sec_data->this_hdr.sh_info;
10418 asection *osec = o->output_section;
10419
7bdf4127 10420 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10421 if (symndx >= locsymcount
10422 || (elf_bad_symtab (input_bfd)
8b127cbc 10423 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10424 {
10425 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10426 while (h->root.type == bfd_link_hash_indirect
10427 || h->root.type == bfd_link_hash_warning)
10428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10429 /* Arrange for symbol to be output. */
10430 h->indx = -2;
10431 elf_section_data (osec)->this_hdr.sh_info = -2;
10432 }
10433 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10434 {
10435 /* We'll use the output section target_index. */
8b127cbc 10436 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10437 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10438 }
10439 else
10440 {
8b127cbc 10441 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10442 {
10443 /* Otherwise output the local symbol now. */
10444 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10445 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10446 const char *name;
6e0b88f1
AM
10447 long indx;
10448 int ret;
bcacc0f5
AM
10449
10450 name = bfd_elf_string_from_elf_section (input_bfd,
10451 symtab_hdr->sh_link,
10452 sym.st_name);
10453 if (name == NULL)
10454 return FALSE;
10455
10456 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10457 sec);
10458 if (sym.st_shndx == SHN_BAD)
10459 return FALSE;
10460
10461 sym.st_value += o->output_offset;
10462
6e0b88f1 10463 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10464 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10465 NULL);
6e0b88f1 10466 if (ret == 0)
bcacc0f5 10467 return FALSE;
6e0b88f1 10468 else if (ret == 1)
8b127cbc 10469 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10470 else
10471 abort ();
bcacc0f5
AM
10472 }
10473 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10474 = flinfo->indices[symndx];
bcacc0f5
AM
10475 }
10476 }
10477
c152c796 10478 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10479 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10480 continue;
10481
10482 if ((o->flags & SEC_LINKER_CREATED) != 0)
10483 {
10484 /* Section was created by _bfd_elf_link_create_dynamic_sections
10485 or somesuch. */
10486 continue;
10487 }
10488
10489 /* Get the contents of the section. They have been cached by a
10490 relaxation routine. Note that o is a section in an input
10491 file, so the contents field will not have been set by any of
10492 the routines which work on output files. */
10493 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10494 {
10495 contents = elf_section_data (o)->this_hdr.contents;
10496 if (bed->caches_rawsize
10497 && o->rawsize != 0
10498 && o->rawsize < o->size)
10499 {
10500 memcpy (flinfo->contents, contents, o->rawsize);
10501 contents = flinfo->contents;
10502 }
10503 }
c152c796
AM
10504 else
10505 {
8b127cbc 10506 contents = flinfo->contents;
4a114e3e 10507 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10508 return FALSE;
10509 }
10510
10511 if ((o->flags & SEC_RELOC) != 0)
10512 {
10513 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10514 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10515 int action_discarded;
ece5ef60 10516 int ret;
c152c796
AM
10517
10518 /* Get the swapped relocs. */
10519 internal_relocs
8b127cbc
AM
10520 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10521 flinfo->internal_relocs, FALSE);
c152c796
AM
10522 if (internal_relocs == NULL
10523 && o->reloc_count > 0)
10524 return FALSE;
10525
310fd250
L
10526 /* We need to reverse-copy input .ctors/.dtors sections if
10527 they are placed in .init_array/.finit_array for output. */
10528 if (o->size > address_size
10529 && ((strncmp (o->name, ".ctors", 6) == 0
10530 && strcmp (o->output_section->name,
10531 ".init_array") == 0)
10532 || (strncmp (o->name, ".dtors", 6) == 0
10533 && strcmp (o->output_section->name,
10534 ".fini_array") == 0))
10535 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10536 {
056bafd4
MR
10537 if (o->size * bed->s->int_rels_per_ext_rel
10538 != o->reloc_count * address_size)
310fd250 10539 {
4eca0228 10540 _bfd_error_handler
695344c0 10541 /* xgettext:c-format */
871b3ab2 10542 (_("error: %pB: size of section %pA is not "
310fd250
L
10543 "multiple of address size"),
10544 input_bfd, o);
8c6716e5 10545 bfd_set_error (bfd_error_bad_value);
310fd250
L
10546 return FALSE;
10547 }
10548 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10549 }
10550
0f02bbd9 10551 action_discarded = -1;
c152c796 10552 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10553 action_discarded = (*bed->action_discarded) (o);
10554
10555 /* Run through the relocs evaluating complex reloc symbols and
10556 looking for relocs against symbols from discarded sections
10557 or section symbols from removed link-once sections.
10558 Complain about relocs against discarded sections. Zero
10559 relocs against removed link-once sections. */
10560
10561 rel = internal_relocs;
056bafd4 10562 relend = rel + o->reloc_count;
0f02bbd9 10563 for ( ; rel < relend; rel++)
c152c796 10564 {
0f02bbd9
AM
10565 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10566 unsigned int s_type;
10567 asection **ps, *sec;
10568 struct elf_link_hash_entry *h = NULL;
10569 const char *sym_name;
c152c796 10570
0f02bbd9
AM
10571 if (r_symndx == STN_UNDEF)
10572 continue;
c152c796 10573
0f02bbd9
AM
10574 if (r_symndx >= locsymcount
10575 || (elf_bad_symtab (input_bfd)
8b127cbc 10576 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10577 {
10578 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10579
0f02bbd9
AM
10580 /* Badly formatted input files can contain relocs that
10581 reference non-existant symbols. Check here so that
10582 we do not seg fault. */
10583 if (h == NULL)
c152c796 10584 {
4eca0228 10585 _bfd_error_handler
695344c0 10586 /* xgettext:c-format */
871b3ab2 10587 (_("error: %pB contains a reloc (%#Lx) for section %pA "
0f02bbd9 10588 "that references a non-existent global symbol"),
76cfced5 10589 input_bfd, rel->r_info, o);
0f02bbd9
AM
10590 bfd_set_error (bfd_error_bad_value);
10591 return FALSE;
10592 }
3b36f7e6 10593
0f02bbd9
AM
10594 while (h->root.type == bfd_link_hash_indirect
10595 || h->root.type == bfd_link_hash_warning)
10596 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10597
0f02bbd9 10598 s_type = h->type;
cdd3575c 10599
9e2dec47 10600 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10601 mark the symbol as undefined. Note that the
10602 linker may attach linker created dynamic sections
10603 to the plugin bfd. Symbols defined in linker
10604 created sections are not plugin symbols. */
bc4e12de 10605 if ((h->root.non_ir_ref_regular
4070765b 10606 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10607 && (h->root.type == bfd_link_hash_defined
10608 || h->root.type == bfd_link_hash_defweak)
10609 && (h->root.u.def.section->flags
10610 & SEC_LINKER_CREATED) == 0
10611 && h->root.u.def.section->owner != NULL
10612 && (h->root.u.def.section->owner->flags
10613 & BFD_PLUGIN) != 0)
10614 {
10615 h->root.type = bfd_link_hash_undefined;
10616 h->root.u.undef.abfd = h->root.u.def.section->owner;
10617 }
10618
0f02bbd9
AM
10619 ps = NULL;
10620 if (h->root.type == bfd_link_hash_defined
10621 || h->root.type == bfd_link_hash_defweak)
10622 ps = &h->root.u.def.section;
10623
10624 sym_name = h->root.root.string;
10625 }
10626 else
10627 {
10628 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10629
10630 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10631 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10632 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10633 sym, *ps);
10634 }
c152c796 10635
c301e700 10636 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10637 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10638 {
10639 bfd_vma val;
10640 bfd_vma dot = (rel->r_offset
10641 + o->output_offset + o->output_section->vma);
10642#ifdef DEBUG
10643 printf ("Encountered a complex symbol!");
10644 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10645 input_bfd->filename, o->name,
10646 (long) (rel - internal_relocs));
0f02bbd9
AM
10647 printf (" symbol: idx %8.8lx, name %s\n",
10648 r_symndx, sym_name);
10649 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10650 (unsigned long) rel->r_info,
10651 (unsigned long) rel->r_offset);
10652#endif
8b127cbc 10653 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10654 isymbuf, locsymcount, s_type == STT_SRELC))
10655 return FALSE;
10656
10657 /* Symbol evaluated OK. Update to absolute value. */
10658 set_symbol_value (input_bfd, isymbuf, locsymcount,
10659 r_symndx, val);
10660 continue;
10661 }
10662
10663 if (action_discarded != -1 && ps != NULL)
10664 {
cdd3575c
AM
10665 /* Complain if the definition comes from a
10666 discarded section. */
dbaa2011 10667 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10668 {
cf35638d 10669 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10670 if (action_discarded & COMPLAIN)
8b127cbc 10671 (*flinfo->info->callbacks->einfo)
695344c0 10672 /* xgettext:c-format */
871b3ab2
AM
10673 (_("%X`%s' referenced in section `%pA' of %pB: "
10674 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10675 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10676
87e5235d 10677 /* Try to do the best we can to support buggy old
e0ae6d6f 10678 versions of gcc. Pretend that the symbol is
87e5235d
AM
10679 really defined in the kept linkonce section.
10680 FIXME: This is quite broken. Modifying the
10681 symbol here means we will be changing all later
e0ae6d6f 10682 uses of the symbol, not just in this section. */
0f02bbd9 10683 if (action_discarded & PRETEND)
87e5235d 10684 {
01b3c8ab
L
10685 asection *kept;
10686
c0f00686 10687 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10688 flinfo->info);
01b3c8ab 10689 if (kept != NULL)
87e5235d
AM
10690 {
10691 *ps = kept;
10692 continue;
10693 }
10694 }
c152c796
AM
10695 }
10696 }
10697 }
10698
10699 /* Relocate the section by invoking a back end routine.
10700
10701 The back end routine is responsible for adjusting the
10702 section contents as necessary, and (if using Rela relocs
10703 and generating a relocatable output file) adjusting the
10704 reloc addend as necessary.
10705
10706 The back end routine does not have to worry about setting
10707 the reloc address or the reloc symbol index.
10708
10709 The back end routine is given a pointer to the swapped in
10710 internal symbols, and can access the hash table entries
10711 for the external symbols via elf_sym_hashes (input_bfd).
10712
10713 When generating relocatable output, the back end routine
10714 must handle STB_LOCAL/STT_SECTION symbols specially. The
10715 output symbol is going to be a section symbol
10716 corresponding to the output section, which will require
10717 the addend to be adjusted. */
10718
8b127cbc 10719 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10720 input_bfd, o, contents,
10721 internal_relocs,
10722 isymbuf,
8b127cbc 10723 flinfo->sections);
ece5ef60 10724 if (!ret)
c152c796
AM
10725 return FALSE;
10726
ece5ef60 10727 if (ret == 2
0e1862bb 10728 || bfd_link_relocatable (flinfo->info)
8b127cbc 10729 || flinfo->info->emitrelocations)
c152c796
AM
10730 {
10731 Elf_Internal_Rela *irela;
d4730f92 10732 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10733 bfd_vma last_offset;
10734 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10735 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10736 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10737 unsigned int next_erel;
c152c796 10738 bfd_boolean rela_normal;
d4730f92 10739 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10740
d4730f92
BS
10741 esdi = elf_section_data (o);
10742 esdo = elf_section_data (o->output_section);
10743 rela_normal = FALSE;
c152c796
AM
10744
10745 /* Adjust the reloc addresses and symbol indices. */
10746
10747 irela = internal_relocs;
056bafd4 10748 irelaend = irela + o->reloc_count;
d4730f92
BS
10749 rel_hash = esdo->rel.hashes + esdo->rel.count;
10750 /* We start processing the REL relocs, if any. When we reach
10751 IRELAMID in the loop, we switch to the RELA relocs. */
10752 irelamid = irela;
10753 if (esdi->rel.hdr != NULL)
10754 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10755 * bed->s->int_rels_per_ext_rel);
eac338cf 10756 rel_hash_list = rel_hash;
d4730f92 10757 rela_hash_list = NULL;
c152c796 10758 last_offset = o->output_offset;
0e1862bb 10759 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10760 last_offset += o->output_section->vma;
10761 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10762 {
10763 unsigned long r_symndx;
10764 asection *sec;
10765 Elf_Internal_Sym sym;
10766
10767 if (next_erel == bed->s->int_rels_per_ext_rel)
10768 {
10769 rel_hash++;
10770 next_erel = 0;
10771 }
10772
d4730f92
BS
10773 if (irela == irelamid)
10774 {
10775 rel_hash = esdo->rela.hashes + esdo->rela.count;
10776 rela_hash_list = rel_hash;
10777 rela_normal = bed->rela_normal;
10778 }
10779
c152c796 10780 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10781 flinfo->info, o,
c152c796
AM
10782 irela->r_offset);
10783 if (irela->r_offset >= (bfd_vma) -2)
10784 {
10785 /* This is a reloc for a deleted entry or somesuch.
10786 Turn it into an R_*_NONE reloc, at the same
10787 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10788 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10789 being ordered. */
10790 irela->r_offset = last_offset;
10791 irela->r_info = 0;
10792 irela->r_addend = 0;
10793 continue;
10794 }
10795
10796 irela->r_offset += o->output_offset;
10797
10798 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10799 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10800 irela->r_offset += o->output_section->vma;
10801
10802 last_offset = irela->r_offset;
10803
10804 r_symndx = irela->r_info >> r_sym_shift;
10805 if (r_symndx == STN_UNDEF)
10806 continue;
10807
10808 if (r_symndx >= locsymcount
10809 || (elf_bad_symtab (input_bfd)
8b127cbc 10810 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10811 {
10812 struct elf_link_hash_entry *rh;
10813 unsigned long indx;
10814
10815 /* This is a reloc against a global symbol. We
10816 have not yet output all the local symbols, so
10817 we do not know the symbol index of any global
10818 symbol. We set the rel_hash entry for this
10819 reloc to point to the global hash table entry
10820 for this symbol. The symbol index is then
ee75fd95 10821 set at the end of bfd_elf_final_link. */
c152c796
AM
10822 indx = r_symndx - extsymoff;
10823 rh = elf_sym_hashes (input_bfd)[indx];
10824 while (rh->root.type == bfd_link_hash_indirect
10825 || rh->root.type == bfd_link_hash_warning)
10826 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10827
10828 /* Setting the index to -2 tells
10829 elf_link_output_extsym that this symbol is
10830 used by a reloc. */
10831 BFD_ASSERT (rh->indx < 0);
10832 rh->indx = -2;
c152c796
AM
10833 *rel_hash = rh;
10834
10835 continue;
10836 }
10837
10838 /* This is a reloc against a local symbol. */
10839
10840 *rel_hash = NULL;
10841 sym = isymbuf[r_symndx];
8b127cbc 10842 sec = flinfo->sections[r_symndx];
c152c796
AM
10843 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10844 {
10845 /* I suppose the backend ought to fill in the
10846 section of any STT_SECTION symbol against a
6a8d1586 10847 processor specific section. */
cf35638d 10848 r_symndx = STN_UNDEF;
6a8d1586
AM
10849 if (bfd_is_abs_section (sec))
10850 ;
c152c796
AM
10851 else if (sec == NULL || sec->owner == NULL)
10852 {
10853 bfd_set_error (bfd_error_bad_value);
10854 return FALSE;
10855 }
10856 else
10857 {
6a8d1586
AM
10858 asection *osec = sec->output_section;
10859
10860 /* If we have discarded a section, the output
10861 section will be the absolute section. In
ab96bf03
AM
10862 case of discarded SEC_MERGE sections, use
10863 the kept section. relocate_section should
10864 have already handled discarded linkonce
10865 sections. */
6a8d1586
AM
10866 if (bfd_is_abs_section (osec)
10867 && sec->kept_section != NULL
10868 && sec->kept_section->output_section != NULL)
10869 {
10870 osec = sec->kept_section->output_section;
10871 irela->r_addend -= osec->vma;
10872 }
10873
10874 if (!bfd_is_abs_section (osec))
10875 {
10876 r_symndx = osec->target_index;
cf35638d 10877 if (r_symndx == STN_UNDEF)
74541ad4 10878 {
051d833a
AM
10879 irela->r_addend += osec->vma;
10880 osec = _bfd_nearby_section (output_bfd, osec,
10881 osec->vma);
10882 irela->r_addend -= osec->vma;
10883 r_symndx = osec->target_index;
74541ad4 10884 }
6a8d1586 10885 }
c152c796
AM
10886 }
10887
10888 /* Adjust the addend according to where the
10889 section winds up in the output section. */
10890 if (rela_normal)
10891 irela->r_addend += sec->output_offset;
10892 }
10893 else
10894 {
8b127cbc 10895 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10896 {
10897 unsigned long shlink;
10898 const char *name;
10899 asection *osec;
6e0b88f1 10900 long indx;
c152c796 10901
8b127cbc 10902 if (flinfo->info->strip == strip_all)
c152c796
AM
10903 {
10904 /* You can't do ld -r -s. */
10905 bfd_set_error (bfd_error_invalid_operation);
10906 return FALSE;
10907 }
10908
10909 /* This symbol was skipped earlier, but
10910 since it is needed by a reloc, we
10911 must output it now. */
10912 shlink = symtab_hdr->sh_link;
10913 name = (bfd_elf_string_from_elf_section
10914 (input_bfd, shlink, sym.st_name));
10915 if (name == NULL)
10916 return FALSE;
10917
10918 osec = sec->output_section;
10919 sym.st_shndx =
10920 _bfd_elf_section_from_bfd_section (output_bfd,
10921 osec);
10922 if (sym.st_shndx == SHN_BAD)
10923 return FALSE;
10924
10925 sym.st_value += sec->output_offset;
0e1862bb 10926 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10927 {
10928 sym.st_value += osec->vma;
10929 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10930 {
10931 /* STT_TLS symbols are relative to PT_TLS
10932 segment base. */
8b127cbc 10933 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10934 ->tls_sec != NULL);
8b127cbc 10935 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10936 ->tls_sec->vma);
10937 }
10938 }
10939
6e0b88f1 10940 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10941 ret = elf_link_output_symstrtab (flinfo, name,
10942 &sym, sec,
10943 NULL);
6e0b88f1 10944 if (ret == 0)
c152c796 10945 return FALSE;
6e0b88f1 10946 else if (ret == 1)
8b127cbc 10947 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10948 else
10949 abort ();
c152c796
AM
10950 }
10951
8b127cbc 10952 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10953 }
10954
10955 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10956 | (irela->r_info & r_type_mask));
10957 }
10958
10959 /* Swap out the relocs. */
d4730f92
BS
10960 input_rel_hdr = esdi->rel.hdr;
10961 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10962 {
d4730f92
BS
10963 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10964 input_rel_hdr,
10965 internal_relocs,
10966 rel_hash_list))
10967 return FALSE;
c152c796
AM
10968 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10969 * bed->s->int_rels_per_ext_rel);
eac338cf 10970 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10971 }
10972
10973 input_rela_hdr = esdi->rela.hdr;
10974 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10975 {
eac338cf 10976 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10977 input_rela_hdr,
eac338cf 10978 internal_relocs,
d4730f92 10979 rela_hash_list))
c152c796
AM
10980 return FALSE;
10981 }
10982 }
10983 }
10984
10985 /* Write out the modified section contents. */
10986 if (bed->elf_backend_write_section
8b127cbc 10987 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10988 contents))
c152c796
AM
10989 {
10990 /* Section written out. */
10991 }
10992 else switch (o->sec_info_type)
10993 {
dbaa2011 10994 case SEC_INFO_TYPE_STABS:
c152c796
AM
10995 if (! (_bfd_write_section_stabs
10996 (output_bfd,
8b127cbc 10997 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10998 o, &elf_section_data (o)->sec_info, contents)))
10999 return FALSE;
11000 break;
dbaa2011 11001 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11002 if (! _bfd_write_merged_section (output_bfd, o,
11003 elf_section_data (o)->sec_info))
11004 return FALSE;
11005 break;
dbaa2011 11006 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11007 {
8b127cbc 11008 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11009 o, contents))
11010 return FALSE;
11011 }
11012 break;
2f0c68f2
CM
11013 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11014 {
11015 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11016 flinfo->info,
11017 o, contents))
11018 return FALSE;
11019 }
11020 break;
c152c796
AM
11021 default:
11022 {
310fd250
L
11023 if (! (o->flags & SEC_EXCLUDE))
11024 {
11025 file_ptr offset = (file_ptr) o->output_offset;
11026 bfd_size_type todo = o->size;
37b01f6a
DG
11027
11028 offset *= bfd_octets_per_byte (output_bfd);
11029
310fd250
L
11030 if ((o->flags & SEC_ELF_REVERSE_COPY))
11031 {
11032 /* Reverse-copy input section to output. */
11033 do
11034 {
11035 todo -= address_size;
11036 if (! bfd_set_section_contents (output_bfd,
11037 o->output_section,
11038 contents + todo,
11039 offset,
11040 address_size))
11041 return FALSE;
11042 if (todo == 0)
11043 break;
11044 offset += address_size;
11045 }
11046 while (1);
11047 }
11048 else if (! bfd_set_section_contents (output_bfd,
11049 o->output_section,
11050 contents,
11051 offset, todo))
11052 return FALSE;
11053 }
c152c796
AM
11054 }
11055 break;
11056 }
11057 }
11058
11059 return TRUE;
11060}
11061
11062/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11063 requested by the linker, and does not come from any input file. This
c152c796
AM
11064 is used to build constructor and destructor tables when linking
11065 with -Ur. */
11066
11067static bfd_boolean
11068elf_reloc_link_order (bfd *output_bfd,
11069 struct bfd_link_info *info,
11070 asection *output_section,
11071 struct bfd_link_order *link_order)
11072{
11073 reloc_howto_type *howto;
11074 long indx;
11075 bfd_vma offset;
11076 bfd_vma addend;
d4730f92 11077 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11078 struct elf_link_hash_entry **rel_hash_ptr;
11079 Elf_Internal_Shdr *rel_hdr;
11080 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11081 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11082 bfd_byte *erel;
11083 unsigned int i;
d4730f92 11084 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11085
11086 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11087 if (howto == NULL)
11088 {
11089 bfd_set_error (bfd_error_bad_value);
11090 return FALSE;
11091 }
11092
11093 addend = link_order->u.reloc.p->addend;
11094
d4730f92
BS
11095 if (esdo->rel.hdr)
11096 reldata = &esdo->rel;
11097 else if (esdo->rela.hdr)
11098 reldata = &esdo->rela;
11099 else
11100 {
11101 reldata = NULL;
11102 BFD_ASSERT (0);
11103 }
11104
c152c796 11105 /* Figure out the symbol index. */
d4730f92 11106 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11107 if (link_order->type == bfd_section_reloc_link_order)
11108 {
11109 indx = link_order->u.reloc.p->u.section->target_index;
11110 BFD_ASSERT (indx != 0);
11111 *rel_hash_ptr = NULL;
11112 }
11113 else
11114 {
11115 struct elf_link_hash_entry *h;
11116
11117 /* Treat a reloc against a defined symbol as though it were
11118 actually against the section. */
11119 h = ((struct elf_link_hash_entry *)
11120 bfd_wrapped_link_hash_lookup (output_bfd, info,
11121 link_order->u.reloc.p->u.name,
11122 FALSE, FALSE, TRUE));
11123 if (h != NULL
11124 && (h->root.type == bfd_link_hash_defined
11125 || h->root.type == bfd_link_hash_defweak))
11126 {
11127 asection *section;
11128
11129 section = h->root.u.def.section;
11130 indx = section->output_section->target_index;
11131 *rel_hash_ptr = NULL;
11132 /* It seems that we ought to add the symbol value to the
11133 addend here, but in practice it has already been added
11134 because it was passed to constructor_callback. */
11135 addend += section->output_section->vma + section->output_offset;
11136 }
11137 else if (h != NULL)
11138 {
11139 /* Setting the index to -2 tells elf_link_output_extsym that
11140 this symbol is used by a reloc. */
11141 h->indx = -2;
11142 *rel_hash_ptr = h;
11143 indx = 0;
11144 }
11145 else
11146 {
1a72702b
AM
11147 (*info->callbacks->unattached_reloc)
11148 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11149 indx = 0;
11150 }
11151 }
11152
11153 /* If this is an inplace reloc, we must write the addend into the
11154 object file. */
11155 if (howto->partial_inplace && addend != 0)
11156 {
11157 bfd_size_type size;
11158 bfd_reloc_status_type rstat;
11159 bfd_byte *buf;
11160 bfd_boolean ok;
11161 const char *sym_name;
11162
a50b1753
NC
11163 size = (bfd_size_type) bfd_get_reloc_size (howto);
11164 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11165 if (buf == NULL && size != 0)
c152c796
AM
11166 return FALSE;
11167 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11168 switch (rstat)
11169 {
11170 case bfd_reloc_ok:
11171 break;
11172
11173 default:
11174 case bfd_reloc_outofrange:
11175 abort ();
11176
11177 case bfd_reloc_overflow:
11178 if (link_order->type == bfd_section_reloc_link_order)
11179 sym_name = bfd_section_name (output_bfd,
11180 link_order->u.reloc.p->u.section);
11181 else
11182 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11183 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11184 howto->name, addend, NULL, NULL,
11185 (bfd_vma) 0);
c152c796
AM
11186 break;
11187 }
37b01f6a 11188
c152c796 11189 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11190 link_order->offset
11191 * bfd_octets_per_byte (output_bfd),
11192 size);
c152c796
AM
11193 free (buf);
11194 if (! ok)
11195 return FALSE;
11196 }
11197
11198 /* The address of a reloc is relative to the section in a
11199 relocatable file, and is a virtual address in an executable
11200 file. */
11201 offset = link_order->offset;
0e1862bb 11202 if (! bfd_link_relocatable (info))
c152c796
AM
11203 offset += output_section->vma;
11204
11205 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11206 {
11207 irel[i].r_offset = offset;
11208 irel[i].r_info = 0;
11209 irel[i].r_addend = 0;
11210 }
11211 if (bed->s->arch_size == 32)
11212 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11213 else
11214 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11215
d4730f92 11216 rel_hdr = reldata->hdr;
c152c796
AM
11217 erel = rel_hdr->contents;
11218 if (rel_hdr->sh_type == SHT_REL)
11219 {
d4730f92 11220 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11221 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11222 }
11223 else
11224 {
11225 irel[0].r_addend = addend;
d4730f92 11226 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11227 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11228 }
11229
d4730f92 11230 ++reldata->count;
c152c796
AM
11231
11232 return TRUE;
11233}
11234
0b52efa6
PB
11235
11236/* Get the output vma of the section pointed to by the sh_link field. */
11237
11238static bfd_vma
11239elf_get_linked_section_vma (struct bfd_link_order *p)
11240{
11241 Elf_Internal_Shdr **elf_shdrp;
11242 asection *s;
11243 int elfsec;
11244
11245 s = p->u.indirect.section;
11246 elf_shdrp = elf_elfsections (s->owner);
11247 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11248 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11249 /* PR 290:
11250 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11251 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11252 sh_info fields. Hence we could get the situation
11253 where elfsec is 0. */
11254 if (elfsec == 0)
11255 {
11256 const struct elf_backend_data *bed
11257 = get_elf_backend_data (s->owner);
11258 if (bed->link_order_error_handler)
d003868e 11259 bed->link_order_error_handler
695344c0 11260 /* xgettext:c-format */
871b3ab2 11261 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11262 return 0;
11263 }
11264 else
11265 {
11266 s = elf_shdrp[elfsec]->bfd_section;
11267 return s->output_section->vma + s->output_offset;
11268 }
0b52efa6
PB
11269}
11270
11271
11272/* Compare two sections based on the locations of the sections they are
11273 linked to. Used by elf_fixup_link_order. */
11274
11275static int
11276compare_link_order (const void * a, const void * b)
11277{
11278 bfd_vma apos;
11279 bfd_vma bpos;
11280
11281 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11282 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11283 if (apos < bpos)
11284 return -1;
11285 return apos > bpos;
11286}
11287
11288
11289/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11290 order as their linked sections. Returns false if this could not be done
11291 because an output section includes both ordered and unordered
11292 sections. Ideally we'd do this in the linker proper. */
11293
11294static bfd_boolean
11295elf_fixup_link_order (bfd *abfd, asection *o)
11296{
11297 int seen_linkorder;
11298 int seen_other;
11299 int n;
11300 struct bfd_link_order *p;
11301 bfd *sub;
11302 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11303 unsigned elfsec;
0b52efa6 11304 struct bfd_link_order **sections;
d33cdfe3 11305 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11306 bfd_vma offset;
3b36f7e6 11307
d33cdfe3
L
11308 other_sec = NULL;
11309 linkorder_sec = NULL;
0b52efa6
PB
11310 seen_other = 0;
11311 seen_linkorder = 0;
8423293d 11312 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11313 {
d33cdfe3 11314 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11315 {
11316 s = p->u.indirect.section;
d33cdfe3
L
11317 sub = s->owner;
11318 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11319 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11320 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11321 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11322 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11323 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11324 {
11325 seen_linkorder++;
11326 linkorder_sec = s;
11327 }
0b52efa6 11328 else
d33cdfe3
L
11329 {
11330 seen_other++;
11331 other_sec = s;
11332 }
0b52efa6
PB
11333 }
11334 else
11335 seen_other++;
d33cdfe3
L
11336
11337 if (seen_other && seen_linkorder)
11338 {
11339 if (other_sec && linkorder_sec)
4eca0228 11340 _bfd_error_handler
695344c0 11341 /* xgettext:c-format */
871b3ab2
AM
11342 (_("%pA has both ordered [`%pA' in %pB] "
11343 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11344 o, linkorder_sec, linkorder_sec->owner,
11345 other_sec, other_sec->owner);
d33cdfe3 11346 else
4eca0228 11347 _bfd_error_handler
871b3ab2 11348 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11349 bfd_set_error (bfd_error_bad_value);
11350 return FALSE;
11351 }
0b52efa6
PB
11352 }
11353
11354 if (!seen_linkorder)
11355 return TRUE;
11356
0b52efa6 11357 sections = (struct bfd_link_order **)
14b1c01e
AM
11358 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11359 if (sections == NULL)
11360 return FALSE;
0b52efa6 11361 seen_linkorder = 0;
3b36f7e6 11362
8423293d 11363 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11364 {
11365 sections[seen_linkorder++] = p;
11366 }
11367 /* Sort the input sections in the order of their linked section. */
11368 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11369 compare_link_order);
11370
11371 /* Change the offsets of the sections. */
11372 offset = 0;
11373 for (n = 0; n < seen_linkorder; n++)
11374 {
11375 s = sections[n]->u.indirect.section;
461686a3 11376 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11377 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11378 sections[n]->offset = offset;
11379 offset += sections[n]->size;
11380 }
11381
4dd07732 11382 free (sections);
0b52efa6
PB
11383 return TRUE;
11384}
11385
76359541
TP
11386/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11387 Returns TRUE upon success, FALSE otherwise. */
11388
11389static bfd_boolean
11390elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11391{
11392 bfd_boolean ret = FALSE;
11393 bfd *implib_bfd;
11394 const struct elf_backend_data *bed;
11395 flagword flags;
11396 enum bfd_architecture arch;
11397 unsigned int mach;
11398 asymbol **sympp = NULL;
11399 long symsize;
11400 long symcount;
11401 long src_count;
11402 elf_symbol_type *osymbuf;
11403
11404 implib_bfd = info->out_implib_bfd;
11405 bed = get_elf_backend_data (abfd);
11406
11407 if (!bfd_set_format (implib_bfd, bfd_object))
11408 return FALSE;
11409
046734ff 11410 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11411 flags = bfd_get_file_flags (abfd);
11412 flags &= ~HAS_RELOC;
11413 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11414 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11415 return FALSE;
11416
11417 /* Copy architecture of output file to import library file. */
11418 arch = bfd_get_arch (abfd);
11419 mach = bfd_get_mach (abfd);
11420 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11421 && (abfd->target_defaulted
11422 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11423 return FALSE;
11424
11425 /* Get symbol table size. */
11426 symsize = bfd_get_symtab_upper_bound (abfd);
11427 if (symsize < 0)
11428 return FALSE;
11429
11430 /* Read in the symbol table. */
11431 sympp = (asymbol **) xmalloc (symsize);
11432 symcount = bfd_canonicalize_symtab (abfd, sympp);
11433 if (symcount < 0)
11434 goto free_sym_buf;
11435
11436 /* Allow the BFD backend to copy any private header data it
11437 understands from the output BFD to the import library BFD. */
11438 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11439 goto free_sym_buf;
11440
11441 /* Filter symbols to appear in the import library. */
11442 if (bed->elf_backend_filter_implib_symbols)
11443 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11444 symcount);
11445 else
11446 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11447 if (symcount == 0)
11448 {
5df1bc57 11449 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11450 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11451 implib_bfd);
76359541
TP
11452 goto free_sym_buf;
11453 }
11454
11455
11456 /* Make symbols absolute. */
11457 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11458 sizeof (*osymbuf));
11459 for (src_count = 0; src_count < symcount; src_count++)
11460 {
11461 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11462 sizeof (*osymbuf));
11463 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11464 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11465 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11466 osymbuf[src_count].internal_elf_sym.st_value =
11467 osymbuf[src_count].symbol.value;
11468 sympp[src_count] = &osymbuf[src_count].symbol;
11469 }
11470
11471 bfd_set_symtab (implib_bfd, sympp, symcount);
11472
11473 /* Allow the BFD backend to copy any private data it understands
11474 from the output BFD to the import library BFD. This is done last
11475 to permit the routine to look at the filtered symbol table. */
11476 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11477 goto free_sym_buf;
11478
11479 if (!bfd_close (implib_bfd))
11480 goto free_sym_buf;
11481
11482 ret = TRUE;
11483
11484free_sym_buf:
11485 free (sympp);
11486 return ret;
11487}
11488
9f7c3e5e
AM
11489static void
11490elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11491{
11492 asection *o;
11493
11494 if (flinfo->symstrtab != NULL)
ef10c3ac 11495 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11496 if (flinfo->contents != NULL)
11497 free (flinfo->contents);
11498 if (flinfo->external_relocs != NULL)
11499 free (flinfo->external_relocs);
11500 if (flinfo->internal_relocs != NULL)
11501 free (flinfo->internal_relocs);
11502 if (flinfo->external_syms != NULL)
11503 free (flinfo->external_syms);
11504 if (flinfo->locsym_shndx != NULL)
11505 free (flinfo->locsym_shndx);
11506 if (flinfo->internal_syms != NULL)
11507 free (flinfo->internal_syms);
11508 if (flinfo->indices != NULL)
11509 free (flinfo->indices);
11510 if (flinfo->sections != NULL)
11511 free (flinfo->sections);
9f7c3e5e
AM
11512 if (flinfo->symshndxbuf != NULL)
11513 free (flinfo->symshndxbuf);
11514 for (o = obfd->sections; o != NULL; o = o->next)
11515 {
11516 struct bfd_elf_section_data *esdo = elf_section_data (o);
11517 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11518 free (esdo->rel.hashes);
11519 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11520 free (esdo->rela.hashes);
11521 }
11522}
0b52efa6 11523
c152c796
AM
11524/* Do the final step of an ELF link. */
11525
11526bfd_boolean
11527bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11528{
11529 bfd_boolean dynamic;
11530 bfd_boolean emit_relocs;
11531 bfd *dynobj;
8b127cbc 11532 struct elf_final_link_info flinfo;
91d6fa6a
NC
11533 asection *o;
11534 struct bfd_link_order *p;
11535 bfd *sub;
c152c796
AM
11536 bfd_size_type max_contents_size;
11537 bfd_size_type max_external_reloc_size;
11538 bfd_size_type max_internal_reloc_count;
11539 bfd_size_type max_sym_count;
11540 bfd_size_type max_sym_shndx_count;
c152c796
AM
11541 Elf_Internal_Sym elfsym;
11542 unsigned int i;
11543 Elf_Internal_Shdr *symtab_hdr;
11544 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11546 struct elf_outext_info eoinfo;
11547 bfd_boolean merged;
11548 size_t relativecount = 0;
11549 asection *reldyn = 0;
11550 bfd_size_type amt;
104d59d1
JM
11551 asection *attr_section = NULL;
11552 bfd_vma attr_size = 0;
11553 const char *std_attrs_section;
64f52338 11554 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11555
64f52338 11556 if (!is_elf_hash_table (htab))
c152c796
AM
11557 return FALSE;
11558
0e1862bb 11559 if (bfd_link_pic (info))
c152c796
AM
11560 abfd->flags |= DYNAMIC;
11561
64f52338
AM
11562 dynamic = htab->dynamic_sections_created;
11563 dynobj = htab->dynobj;
c152c796 11564
0e1862bb 11565 emit_relocs = (bfd_link_relocatable (info)
a4676736 11566 || info->emitrelocations);
c152c796 11567
8b127cbc
AM
11568 flinfo.info = info;
11569 flinfo.output_bfd = abfd;
ef10c3ac 11570 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11571 if (flinfo.symstrtab == NULL)
c152c796
AM
11572 return FALSE;
11573
11574 if (! dynamic)
11575 {
8b127cbc
AM
11576 flinfo.hash_sec = NULL;
11577 flinfo.symver_sec = NULL;
c152c796
AM
11578 }
11579 else
11580 {
3d4d4302 11581 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11582 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11583 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11584 /* Note that it is OK if symver_sec is NULL. */
11585 }
11586
8b127cbc
AM
11587 flinfo.contents = NULL;
11588 flinfo.external_relocs = NULL;
11589 flinfo.internal_relocs = NULL;
11590 flinfo.external_syms = NULL;
11591 flinfo.locsym_shndx = NULL;
11592 flinfo.internal_syms = NULL;
11593 flinfo.indices = NULL;
11594 flinfo.sections = NULL;
8b127cbc 11595 flinfo.symshndxbuf = NULL;
ffbc01cc 11596 flinfo.filesym_count = 0;
c152c796 11597
104d59d1
JM
11598 /* The object attributes have been merged. Remove the input
11599 sections from the link, and set the contents of the output
11600 secton. */
11601 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11602 for (o = abfd->sections; o != NULL; o = o->next)
11603 {
11604 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11605 || strcmp (o->name, ".gnu.attributes") == 0)
11606 {
11607 for (p = o->map_head.link_order; p != NULL; p = p->next)
11608 {
11609 asection *input_section;
11610
11611 if (p->type != bfd_indirect_link_order)
11612 continue;
11613 input_section = p->u.indirect.section;
11614 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11615 elf_link_input_bfd ignores this section. */
11616 input_section->flags &= ~SEC_HAS_CONTENTS;
11617 }
a0c8462f 11618
104d59d1
JM
11619 attr_size = bfd_elf_obj_attr_size (abfd);
11620 if (attr_size)
11621 {
11622 bfd_set_section_size (abfd, o, attr_size);
11623 attr_section = o;
11624 /* Skip this section later on. */
11625 o->map_head.link_order = NULL;
11626 }
11627 else
11628 o->flags |= SEC_EXCLUDE;
11629 }
6e5e9d58
AM
11630 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11631 {
11632 /* Remove empty group section from linker output. */
11633 o->flags |= SEC_EXCLUDE;
11634 bfd_section_list_remove (abfd, o);
11635 abfd->section_count--;
11636 }
104d59d1
JM
11637 }
11638
c152c796
AM
11639 /* Count up the number of relocations we will output for each output
11640 section, so that we know the sizes of the reloc sections. We
11641 also figure out some maximum sizes. */
11642 max_contents_size = 0;
11643 max_external_reloc_size = 0;
11644 max_internal_reloc_count = 0;
11645 max_sym_count = 0;
11646 max_sym_shndx_count = 0;
11647 merged = FALSE;
11648 for (o = abfd->sections; o != NULL; o = o->next)
11649 {
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 o->reloc_count = 0;
11652
8423293d 11653 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11654 {
11655 unsigned int reloc_count = 0;
9eaff861 11656 unsigned int additional_reloc_count = 0;
c152c796 11657 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11658
11659 if (p->type == bfd_section_reloc_link_order
11660 || p->type == bfd_symbol_reloc_link_order)
11661 reloc_count = 1;
11662 else if (p->type == bfd_indirect_link_order)
11663 {
11664 asection *sec;
11665
11666 sec = p->u.indirect.section;
c152c796
AM
11667
11668 /* Mark all sections which are to be included in the
11669 link. This will normally be every section. We need
11670 to do this so that we can identify any sections which
11671 the linker has decided to not include. */
11672 sec->linker_mark = TRUE;
11673
11674 if (sec->flags & SEC_MERGE)
11675 merged = TRUE;
11676
eea6121a
AM
11677 if (sec->rawsize > max_contents_size)
11678 max_contents_size = sec->rawsize;
11679 if (sec->size > max_contents_size)
11680 max_contents_size = sec->size;
c152c796 11681
c152c796
AM
11682 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11683 && (sec->owner->flags & DYNAMIC) == 0)
11684 {
11685 size_t sym_count;
11686
a961cdd5
AM
11687 /* We are interested in just local symbols, not all
11688 symbols. */
c152c796
AM
11689 if (elf_bad_symtab (sec->owner))
11690 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11691 / bed->s->sizeof_sym);
11692 else
11693 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11694
11695 if (sym_count > max_sym_count)
11696 max_sym_count = sym_count;
11697
11698 if (sym_count > max_sym_shndx_count
6a40cf0c 11699 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11700 max_sym_shndx_count = sym_count;
11701
a961cdd5
AM
11702 if (esdo->this_hdr.sh_type == SHT_REL
11703 || esdo->this_hdr.sh_type == SHT_RELA)
11704 /* Some backends use reloc_count in relocation sections
11705 to count particular types of relocs. Of course,
11706 reloc sections themselves can't have relocations. */
11707 ;
11708 else if (emit_relocs)
11709 {
11710 reloc_count = sec->reloc_count;
11711 if (bed->elf_backend_count_additional_relocs)
11712 {
11713 int c;
11714 c = (*bed->elf_backend_count_additional_relocs) (sec);
11715 additional_reloc_count += c;
11716 }
11717 }
11718 else if (bed->elf_backend_count_relocs)
11719 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11720
11721 esdi = elf_section_data (sec);
11722
c152c796
AM
11723 if ((sec->flags & SEC_RELOC) != 0)
11724 {
d4730f92 11725 size_t ext_size = 0;
c152c796 11726
d4730f92
BS
11727 if (esdi->rel.hdr != NULL)
11728 ext_size = esdi->rel.hdr->sh_size;
11729 if (esdi->rela.hdr != NULL)
11730 ext_size += esdi->rela.hdr->sh_size;
7326c758 11731
c152c796
AM
11732 if (ext_size > max_external_reloc_size)
11733 max_external_reloc_size = ext_size;
11734 if (sec->reloc_count > max_internal_reloc_count)
11735 max_internal_reloc_count = sec->reloc_count;
11736 }
11737 }
11738 }
11739
11740 if (reloc_count == 0)
11741 continue;
11742
9eaff861 11743 reloc_count += additional_reloc_count;
c152c796
AM
11744 o->reloc_count += reloc_count;
11745
0e1862bb 11746 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11747 {
d4730f92 11748 if (esdi->rel.hdr)
9eaff861 11749 {
491d01d3 11750 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11751 esdo->rel.count += additional_reloc_count;
11752 }
d4730f92 11753 if (esdi->rela.hdr)
9eaff861 11754 {
491d01d3 11755 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11756 esdo->rela.count += additional_reloc_count;
11757 }
d4730f92
BS
11758 }
11759 else
11760 {
11761 if (o->use_rela_p)
11762 esdo->rela.count += reloc_count;
2c2b4ed4 11763 else
d4730f92 11764 esdo->rel.count += reloc_count;
c152c796 11765 }
c152c796
AM
11766 }
11767
9eaff861 11768 if (o->reloc_count > 0)
c152c796
AM
11769 o->flags |= SEC_RELOC;
11770 else
11771 {
11772 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11773 set it (this is probably a bug) and if it is set
11774 assign_section_numbers will create a reloc section. */
11775 o->flags &=~ SEC_RELOC;
11776 }
11777
11778 /* If the SEC_ALLOC flag is not set, force the section VMA to
11779 zero. This is done in elf_fake_sections as well, but forcing
11780 the VMA to 0 here will ensure that relocs against these
11781 sections are handled correctly. */
11782 if ((o->flags & SEC_ALLOC) == 0
11783 && ! o->user_set_vma)
11784 o->vma = 0;
11785 }
11786
0e1862bb 11787 if (! bfd_link_relocatable (info) && merged)
64f52338 11788 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11789
11790 /* Figure out the file positions for everything but the symbol table
11791 and the relocs. We set symcount to force assign_section_numbers
11792 to create a symbol table. */
8539e4e8 11793 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11794 BFD_ASSERT (! abfd->output_has_begun);
11795 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11796 goto error_return;
11797
ee75fd95 11798 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11799 for (o = abfd->sections; o != NULL; o = o->next)
11800 {
d4730f92 11801 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11802 if ((o->flags & SEC_RELOC) != 0)
11803 {
d4730f92 11804 if (esdo->rel.hdr
9eaff861 11805 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11806 goto error_return;
11807
d4730f92 11808 if (esdo->rela.hdr
9eaff861 11809 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11810 goto error_return;
11811 }
11812
11813 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11814 to count upwards while actually outputting the relocations. */
d4730f92
BS
11815 esdo->rel.count = 0;
11816 esdo->rela.count = 0;
0ce398f1
L
11817
11818 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11819 {
11820 /* Cache the section contents so that they can be compressed
11821 later. Use bfd_malloc since it will be freed by
11822 bfd_compress_section_contents. */
11823 unsigned char *contents = esdo->this_hdr.contents;
11824 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11825 abort ();
11826 contents
11827 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11828 if (contents == NULL)
11829 goto error_return;
11830 esdo->this_hdr.contents = contents;
11831 }
c152c796
AM
11832 }
11833
c152c796 11834 /* We have now assigned file positions for all the sections except
a485e98e
AM
11835 .symtab, .strtab, and non-loaded reloc sections. We start the
11836 .symtab section at the current file position, and write directly
11837 to it. We build the .strtab section in memory. */
c152c796
AM
11838 bfd_get_symcount (abfd) = 0;
11839 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11840 /* sh_name is set in prep_headers. */
11841 symtab_hdr->sh_type = SHT_SYMTAB;
11842 /* sh_flags, sh_addr and sh_size all start off zero. */
11843 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11844 /* sh_link is set in assign_section_numbers. */
11845 /* sh_info is set below. */
11846 /* sh_offset is set just below. */
72de5009 11847 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11848
ef10c3ac
L
11849 if (max_sym_count < 20)
11850 max_sym_count = 20;
64f52338 11851 htab->strtabsize = max_sym_count;
ef10c3ac 11852 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11853 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11854 if (htab->strtab == NULL)
c152c796 11855 goto error_return;
ef10c3ac
L
11856 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11857 flinfo.symshndxbuf
11858 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11859 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11860
8539e4e8 11861 if (info->strip != strip_all || emit_relocs)
c152c796 11862 {
8539e4e8
AM
11863 file_ptr off = elf_next_file_pos (abfd);
11864
11865 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11866
11867 /* Note that at this point elf_next_file_pos (abfd) is
11868 incorrect. We do not yet know the size of the .symtab section.
11869 We correct next_file_pos below, after we do know the size. */
11870
11871 /* Start writing out the symbol table. The first symbol is always a
11872 dummy symbol. */
c152c796
AM
11873 elfsym.st_value = 0;
11874 elfsym.st_size = 0;
11875 elfsym.st_info = 0;
11876 elfsym.st_other = 0;
11877 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11878 elfsym.st_target_internal = 0;
ef10c3ac
L
11879 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11880 bfd_und_section_ptr, NULL) != 1)
c152c796 11881 goto error_return;
c152c796 11882
8539e4e8
AM
11883 /* Output a symbol for each section. We output these even if we are
11884 discarding local symbols, since they are used for relocs. These
11885 symbols have no names. We store the index of each one in the
11886 index field of the section, so that we can find it again when
11887 outputting relocs. */
11888
c152c796
AM
11889 elfsym.st_size = 0;
11890 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11891 elfsym.st_other = 0;
f0b5bb34 11892 elfsym.st_value = 0;
35fc36a8 11893 elfsym.st_target_internal = 0;
c152c796
AM
11894 for (i = 1; i < elf_numsections (abfd); i++)
11895 {
11896 o = bfd_section_from_elf_index (abfd, i);
11897 if (o != NULL)
f0b5bb34
AM
11898 {
11899 o->target_index = bfd_get_symcount (abfd);
11900 elfsym.st_shndx = i;
0e1862bb 11901 if (!bfd_link_relocatable (info))
f0b5bb34 11902 elfsym.st_value = o->vma;
ef10c3ac
L
11903 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11904 NULL) != 1)
f0b5bb34
AM
11905 goto error_return;
11906 }
c152c796
AM
11907 }
11908 }
11909
11910 /* Allocate some memory to hold information read in from the input
11911 files. */
11912 if (max_contents_size != 0)
11913 {
8b127cbc
AM
11914 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11915 if (flinfo.contents == NULL)
c152c796
AM
11916 goto error_return;
11917 }
11918
11919 if (max_external_reloc_size != 0)
11920 {
8b127cbc
AM
11921 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11922 if (flinfo.external_relocs == NULL)
c152c796
AM
11923 goto error_return;
11924 }
11925
11926 if (max_internal_reloc_count != 0)
11927 {
056bafd4 11928 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11929 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11930 if (flinfo.internal_relocs == NULL)
c152c796
AM
11931 goto error_return;
11932 }
11933
11934 if (max_sym_count != 0)
11935 {
11936 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11937 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11938 if (flinfo.external_syms == NULL)
c152c796
AM
11939 goto error_return;
11940
11941 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11942 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11943 if (flinfo.internal_syms == NULL)
c152c796
AM
11944 goto error_return;
11945
11946 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11947 flinfo.indices = (long int *) bfd_malloc (amt);
11948 if (flinfo.indices == NULL)
c152c796
AM
11949 goto error_return;
11950
11951 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11952 flinfo.sections = (asection **) bfd_malloc (amt);
11953 if (flinfo.sections == NULL)
c152c796
AM
11954 goto error_return;
11955 }
11956
11957 if (max_sym_shndx_count != 0)
11958 {
11959 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11960 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11961 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11962 goto error_return;
11963 }
11964
64f52338 11965 if (htab->tls_sec)
c152c796
AM
11966 {
11967 bfd_vma base, end = 0;
11968 asection *sec;
11969
64f52338 11970 for (sec = htab->tls_sec;
c152c796
AM
11971 sec && (sec->flags & SEC_THREAD_LOCAL);
11972 sec = sec->next)
11973 {
3a800eb9 11974 bfd_size_type size = sec->size;
c152c796 11975
3a800eb9
AM
11976 if (size == 0
11977 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11978 {
91d6fa6a
NC
11979 struct bfd_link_order *ord = sec->map_tail.link_order;
11980
11981 if (ord != NULL)
11982 size = ord->offset + ord->size;
c152c796
AM
11983 }
11984 end = sec->vma + size;
11985 }
64f52338 11986 base = htab->tls_sec->vma;
7dc98aea
RO
11987 /* Only align end of TLS section if static TLS doesn't have special
11988 alignment requirements. */
11989 if (bed->static_tls_alignment == 1)
64f52338
AM
11990 end = align_power (end, htab->tls_sec->alignment_power);
11991 htab->tls_size = end - base;
c152c796
AM
11992 }
11993
0b52efa6
PB
11994 /* Reorder SHF_LINK_ORDER sections. */
11995 for (o = abfd->sections; o != NULL; o = o->next)
11996 {
11997 if (!elf_fixup_link_order (abfd, o))
11998 return FALSE;
11999 }
12000
2f0c68f2
CM
12001 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12002 return FALSE;
12003
c152c796
AM
12004 /* Since ELF permits relocations to be against local symbols, we
12005 must have the local symbols available when we do the relocations.
12006 Since we would rather only read the local symbols once, and we
12007 would rather not keep them in memory, we handle all the
12008 relocations for a single input file at the same time.
12009
12010 Unfortunately, there is no way to know the total number of local
12011 symbols until we have seen all of them, and the local symbol
12012 indices precede the global symbol indices. This means that when
12013 we are generating relocatable output, and we see a reloc against
12014 a global symbol, we can not know the symbol index until we have
12015 finished examining all the local symbols to see which ones we are
12016 going to output. To deal with this, we keep the relocations in
12017 memory, and don't output them until the end of the link. This is
12018 an unfortunate waste of memory, but I don't see a good way around
12019 it. Fortunately, it only happens when performing a relocatable
12020 link, which is not the common case. FIXME: If keep_memory is set
12021 we could write the relocs out and then read them again; I don't
12022 know how bad the memory loss will be. */
12023
c72f2fb2 12024 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12025 sub->output_has_begun = FALSE;
12026 for (o = abfd->sections; o != NULL; o = o->next)
12027 {
8423293d 12028 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12029 {
12030 if (p->type == bfd_indirect_link_order
12031 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12032 == bfd_target_elf_flavour)
12033 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12034 {
12035 if (! sub->output_has_begun)
12036 {
8b127cbc 12037 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12038 goto error_return;
12039 sub->output_has_begun = TRUE;
12040 }
12041 }
12042 else if (p->type == bfd_section_reloc_link_order
12043 || p->type == bfd_symbol_reloc_link_order)
12044 {
12045 if (! elf_reloc_link_order (abfd, info, o, p))
12046 goto error_return;
12047 }
12048 else
12049 {
12050 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12051 {
12052 if (p->type == bfd_indirect_link_order
12053 && (bfd_get_flavour (sub)
12054 == bfd_target_elf_flavour)
12055 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12056 != bed->s->elfclass))
12057 {
12058 const char *iclass, *oclass;
12059
aebf9be7 12060 switch (bed->s->elfclass)
351f65ca 12061 {
aebf9be7
NC
12062 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12063 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12064 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12065 default: abort ();
351f65ca 12066 }
aebf9be7
NC
12067
12068 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12069 {
aebf9be7
NC
12070 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12071 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12072 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12073 default: abort ();
351f65ca
L
12074 }
12075
12076 bfd_set_error (bfd_error_wrong_format);
4eca0228 12077 _bfd_error_handler
695344c0 12078 /* xgettext:c-format */
871b3ab2 12079 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12080 sub, iclass, oclass);
12081 }
12082
12083 goto error_return;
12084 }
c152c796
AM
12085 }
12086 }
12087 }
12088
c0f00686
L
12089 /* Free symbol buffer if needed. */
12090 if (!info->reduce_memory_overheads)
12091 {
c72f2fb2 12092 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12093 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12094 && elf_tdata (sub)->symbuf)
c0f00686
L
12095 {
12096 free (elf_tdata (sub)->symbuf);
12097 elf_tdata (sub)->symbuf = NULL;
12098 }
12099 }
12100
c152c796
AM
12101 /* Output any global symbols that got converted to local in a
12102 version script or due to symbol visibility. We do this in a
12103 separate step since ELF requires all local symbols to appear
12104 prior to any global symbols. FIXME: We should only do this if
12105 some global symbols were, in fact, converted to become local.
12106 FIXME: Will this work correctly with the Irix 5 linker? */
12107 eoinfo.failed = FALSE;
8b127cbc 12108 eoinfo.flinfo = &flinfo;
c152c796 12109 eoinfo.localsyms = TRUE;
34a79995 12110 eoinfo.file_sym_done = FALSE;
7686d77d 12111 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12112 if (eoinfo.failed)
12113 return FALSE;
12114
4e617b1e
PB
12115 /* If backend needs to output some local symbols not present in the hash
12116 table, do it now. */
8539e4e8
AM
12117 if (bed->elf_backend_output_arch_local_syms
12118 && (info->strip != strip_all || emit_relocs))
4e617b1e 12119 {
6e0b88f1 12120 typedef int (*out_sym_func)
4e617b1e
PB
12121 (void *, const char *, Elf_Internal_Sym *, asection *,
12122 struct elf_link_hash_entry *);
12123
12124 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12125 (abfd, info, &flinfo,
12126 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12127 return FALSE;
12128 }
12129
c152c796
AM
12130 /* That wrote out all the local symbols. Finish up the symbol table
12131 with the global symbols. Even if we want to strip everything we
12132 can, we still need to deal with those global symbols that got
12133 converted to local in a version script. */
12134
12135 /* The sh_info field records the index of the first non local symbol. */
12136 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12137
12138 if (dynamic
64f52338
AM
12139 && htab->dynsym != NULL
12140 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12141 {
12142 Elf_Internal_Sym sym;
64f52338 12143 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12144
64f52338
AM
12145 o = htab->dynsym->output_section;
12146 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12147
12148 /* Write out the section symbols for the output sections. */
0e1862bb 12149 if (bfd_link_pic (info)
64f52338 12150 || htab->is_relocatable_executable)
c152c796
AM
12151 {
12152 asection *s;
12153
12154 sym.st_size = 0;
12155 sym.st_name = 0;
12156 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12157 sym.st_other = 0;
35fc36a8 12158 sym.st_target_internal = 0;
c152c796
AM
12159
12160 for (s = abfd->sections; s != NULL; s = s->next)
12161 {
12162 int indx;
12163 bfd_byte *dest;
12164 long dynindx;
12165
c152c796 12166 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12167 if (dynindx <= 0)
12168 continue;
12169 indx = elf_section_data (s)->this_idx;
c152c796
AM
12170 BFD_ASSERT (indx > 0);
12171 sym.st_shndx = indx;
c0d5a53d
L
12172 if (! check_dynsym (abfd, &sym))
12173 return FALSE;
c152c796
AM
12174 sym.st_value = s->vma;
12175 dest = dynsym + dynindx * bed->s->sizeof_sym;
12176 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12177 }
c152c796
AM
12178 }
12179
12180 /* Write out the local dynsyms. */
64f52338 12181 if (htab->dynlocal)
c152c796
AM
12182 {
12183 struct elf_link_local_dynamic_entry *e;
64f52338 12184 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12185 {
12186 asection *s;
12187 bfd_byte *dest;
12188
935bd1e0 12189 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12190 Note that we saved a word of storage and overwrote
12191 the original st_name with the dynstr_index. */
12192 sym = e->isym;
935bd1e0 12193 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12194
cb33740c
AM
12195 s = bfd_section_from_elf_index (e->input_bfd,
12196 e->isym.st_shndx);
12197 if (s != NULL)
c152c796 12198 {
c152c796
AM
12199 sym.st_shndx =
12200 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12201 if (! check_dynsym (abfd, &sym))
12202 return FALSE;
c152c796
AM
12203 sym.st_value = (s->output_section->vma
12204 + s->output_offset
12205 + e->isym.st_value);
12206 }
12207
c152c796
AM
12208 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12209 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12210 }
12211 }
c152c796
AM
12212 }
12213
12214 /* We get the global symbols from the hash table. */
12215 eoinfo.failed = FALSE;
12216 eoinfo.localsyms = FALSE;
8b127cbc 12217 eoinfo.flinfo = &flinfo;
7686d77d 12218 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12219 if (eoinfo.failed)
12220 return FALSE;
12221
12222 /* If backend needs to output some symbols not present in the hash
12223 table, do it now. */
8539e4e8
AM
12224 if (bed->elf_backend_output_arch_syms
12225 && (info->strip != strip_all || emit_relocs))
c152c796 12226 {
6e0b88f1 12227 typedef int (*out_sym_func)
c152c796
AM
12228 (void *, const char *, Elf_Internal_Sym *, asection *,
12229 struct elf_link_hash_entry *);
12230
12231 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12232 (abfd, info, &flinfo,
12233 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12234 return FALSE;
12235 }
12236
ef10c3ac
L
12237 /* Finalize the .strtab section. */
12238 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12239
12240 /* Swap out the .strtab section. */
12241 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12242 return FALSE;
12243
12244 /* Now we know the size of the symtab section. */
c152c796
AM
12245 if (bfd_get_symcount (abfd) > 0)
12246 {
ee3b52e9
L
12247 /* Finish up and write out the symbol string table (.strtab)
12248 section. */
ad32986f 12249 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12250 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12251
ad32986f 12252 if (elf_symtab_shndx_list (abfd))
8539e4e8 12253 {
ad32986f 12254 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12255
ad32986f
NC
12256 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12257 {
12258 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12259 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12260 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12261 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12262 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12263
ad32986f
NC
12264 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12265 off, TRUE);
12266
12267 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12268 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12269 return FALSE;
12270 }
8539e4e8 12271 }
ee3b52e9
L
12272
12273 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12274 /* sh_name was set in prep_headers. */
12275 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12276 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12277 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12278 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12279 symstrtab_hdr->sh_entsize = 0;
12280 symstrtab_hdr->sh_link = 0;
12281 symstrtab_hdr->sh_info = 0;
12282 /* sh_offset is set just below. */
12283 symstrtab_hdr->sh_addralign = 1;
12284
12285 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12286 off, TRUE);
12287 elf_next_file_pos (abfd) = off;
12288
c152c796 12289 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12290 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12291 return FALSE;
12292 }
12293
76359541
TP
12294 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12295 {
871b3ab2 12296 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12297 info->out_implib_bfd);
76359541
TP
12298 return FALSE;
12299 }
12300
c152c796
AM
12301 /* Adjust the relocs to have the correct symbol indices. */
12302 for (o = abfd->sections; o != NULL; o = o->next)
12303 {
d4730f92 12304 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12305 bfd_boolean sort;
10bbbc1d 12306
c152c796
AM
12307 if ((o->flags & SEC_RELOC) == 0)
12308 continue;
12309
28dbcedc 12310 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12311 if (esdo->rel.hdr != NULL
10bbbc1d 12312 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12313 return FALSE;
12314 if (esdo->rela.hdr != NULL
10bbbc1d 12315 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12316 return FALSE;
c152c796
AM
12317
12318 /* Set the reloc_count field to 0 to prevent write_relocs from
12319 trying to swap the relocs out itself. */
12320 o->reloc_count = 0;
12321 }
12322
12323 if (dynamic && info->combreloc && dynobj != NULL)
12324 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12325
12326 /* If we are linking against a dynamic object, or generating a
12327 shared library, finish up the dynamic linking information. */
12328 if (dynamic)
12329 {
12330 bfd_byte *dyncon, *dynconend;
12331
12332 /* Fix up .dynamic entries. */
3d4d4302 12333 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12334 BFD_ASSERT (o != NULL);
12335
12336 dyncon = o->contents;
eea6121a 12337 dynconend = o->contents + o->size;
c152c796
AM
12338 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12339 {
12340 Elf_Internal_Dyn dyn;
12341 const char *name;
12342 unsigned int type;
64487780
AM
12343 bfd_size_type sh_size;
12344 bfd_vma sh_addr;
c152c796
AM
12345
12346 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12347
12348 switch (dyn.d_tag)
12349 {
12350 default:
12351 continue;
12352 case DT_NULL:
12353 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12354 {
12355 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12356 {
12357 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12358 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12359 default: continue;
12360 }
12361 dyn.d_un.d_val = relativecount;
12362 relativecount = 0;
12363 break;
12364 }
12365 continue;
12366
12367 case DT_INIT:
12368 name = info->init_function;
12369 goto get_sym;
12370 case DT_FINI:
12371 name = info->fini_function;
12372 get_sym:
12373 {
12374 struct elf_link_hash_entry *h;
12375
64f52338 12376 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12377 if (h != NULL
12378 && (h->root.type == bfd_link_hash_defined
12379 || h->root.type == bfd_link_hash_defweak))
12380 {
bef26483 12381 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12382 o = h->root.u.def.section;
12383 if (o->output_section != NULL)
bef26483 12384 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12385 + o->output_offset);
12386 else
12387 {
12388 /* The symbol is imported from another shared
12389 library and does not apply to this one. */
bef26483 12390 dyn.d_un.d_ptr = 0;
c152c796
AM
12391 }
12392 break;
12393 }
12394 }
12395 continue;
12396
12397 case DT_PREINIT_ARRAYSZ:
12398 name = ".preinit_array";
4ade44b7 12399 goto get_out_size;
c152c796
AM
12400 case DT_INIT_ARRAYSZ:
12401 name = ".init_array";
4ade44b7 12402 goto get_out_size;
c152c796
AM
12403 case DT_FINI_ARRAYSZ:
12404 name = ".fini_array";
4ade44b7 12405 get_out_size:
c152c796
AM
12406 o = bfd_get_section_by_name (abfd, name);
12407 if (o == NULL)
12408 {
4eca0228 12409 _bfd_error_handler
4ade44b7 12410 (_("could not find section %s"), name);
c152c796
AM
12411 goto error_return;
12412 }
eea6121a 12413 if (o->size == 0)
4eca0228 12414 _bfd_error_handler
c152c796 12415 (_("warning: %s section has zero size"), name);
eea6121a 12416 dyn.d_un.d_val = o->size;
c152c796
AM
12417 break;
12418
12419 case DT_PREINIT_ARRAY:
12420 name = ".preinit_array";
4ade44b7 12421 goto get_out_vma;
c152c796
AM
12422 case DT_INIT_ARRAY:
12423 name = ".init_array";
4ade44b7 12424 goto get_out_vma;
c152c796
AM
12425 case DT_FINI_ARRAY:
12426 name = ".fini_array";
4ade44b7
AM
12427 get_out_vma:
12428 o = bfd_get_section_by_name (abfd, name);
12429 goto do_vma;
c152c796
AM
12430
12431 case DT_HASH:
12432 name = ".hash";
12433 goto get_vma;
fdc90cb4
JJ
12434 case DT_GNU_HASH:
12435 name = ".gnu.hash";
12436 goto get_vma;
c152c796
AM
12437 case DT_STRTAB:
12438 name = ".dynstr";
12439 goto get_vma;
12440 case DT_SYMTAB:
12441 name = ".dynsym";
12442 goto get_vma;
12443 case DT_VERDEF:
12444 name = ".gnu.version_d";
12445 goto get_vma;
12446 case DT_VERNEED:
12447 name = ".gnu.version_r";
12448 goto get_vma;
12449 case DT_VERSYM:
12450 name = ".gnu.version";
12451 get_vma:
4ade44b7
AM
12452 o = bfd_get_linker_section (dynobj, name);
12453 do_vma:
b3293efa 12454 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12455 {
4eca0228 12456 _bfd_error_handler
4ade44b7 12457 (_("could not find section %s"), name);
c152c796
AM
12458 goto error_return;
12459 }
894891db
NC
12460 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12461 {
4eca0228 12462 _bfd_error_handler
894891db
NC
12463 (_("warning: section '%s' is being made into a note"), name);
12464 bfd_set_error (bfd_error_nonrepresentable_section);
12465 goto error_return;
12466 }
4ade44b7 12467 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12468 break;
12469
12470 case DT_REL:
12471 case DT_RELA:
12472 case DT_RELSZ:
12473 case DT_RELASZ:
12474 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12475 type = SHT_REL;
12476 else
12477 type = SHT_RELA;
64487780
AM
12478 sh_size = 0;
12479 sh_addr = 0;
c152c796
AM
12480 for (i = 1; i < elf_numsections (abfd); i++)
12481 {
12482 Elf_Internal_Shdr *hdr;
12483
12484 hdr = elf_elfsections (abfd)[i];
12485 if (hdr->sh_type == type
12486 && (hdr->sh_flags & SHF_ALLOC) != 0)
12487 {
64487780
AM
12488 sh_size += hdr->sh_size;
12489 if (sh_addr == 0
12490 || sh_addr > hdr->sh_addr)
12491 sh_addr = hdr->sh_addr;
c152c796
AM
12492 }
12493 }
64487780 12494
64f52338
AM
12495 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12496 {
12497 /* Don't count procedure linkage table relocs in the
12498 overall reloc count. */
64487780
AM
12499 sh_size -= htab->srelplt->size;
12500 if (sh_size == 0)
12501 /* If the size is zero, make the address zero too.
12502 This is to avoid a glibc bug. If the backend
12503 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12504 zero, then we'll put DT_RELA at the end of
12505 DT_JMPREL. glibc will interpret the end of
12506 DT_RELA matching the end of DT_JMPREL as the
12507 case where DT_RELA includes DT_JMPREL, and for
12508 LD_BIND_NOW will decide that processing DT_RELA
12509 will process the PLT relocs too. Net result:
12510 No PLT relocs applied. */
12511 sh_addr = 0;
12512
64f52338
AM
12513 /* If .rela.plt is the first .rela section, exclude
12514 it from DT_RELA. */
64487780
AM
12515 else if (sh_addr == (htab->srelplt->output_section->vma
12516 + htab->srelplt->output_offset))
12517 sh_addr += htab->srelplt->size;
64f52338 12518 }
64487780
AM
12519
12520 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12521 dyn.d_un.d_val = sh_size;
12522 else
12523 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12524 break;
12525 }
12526 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12527 }
12528 }
12529
12530 /* If we have created any dynamic sections, then output them. */
12531 if (dynobj != NULL)
12532 {
12533 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12534 goto error_return;
12535
943284cc 12536 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12537 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12538 || info->error_textrel)
3d4d4302 12539 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12540 {
12541 bfd_byte *dyncon, *dynconend;
12542
943284cc
DJ
12543 dyncon = o->contents;
12544 dynconend = o->contents + o->size;
12545 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12546 {
12547 Elf_Internal_Dyn dyn;
12548
12549 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12550
12551 if (dyn.d_tag == DT_TEXTREL)
12552 {
c192a133
AM
12553 if (info->error_textrel)
12554 info->callbacks->einfo
12555 (_("%P%X: read-only segment has dynamic relocations.\n"));
12556 else
12557 info->callbacks->einfo
12558 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12559 break;
12560 }
12561 }
12562 }
12563
c152c796
AM
12564 for (o = dynobj->sections; o != NULL; o = o->next)
12565 {
12566 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12567 || o->size == 0
c152c796
AM
12568 || o->output_section == bfd_abs_section_ptr)
12569 continue;
12570 if ((o->flags & SEC_LINKER_CREATED) == 0)
12571 {
12572 /* At this point, we are only interested in sections
12573 created by _bfd_elf_link_create_dynamic_sections. */
12574 continue;
12575 }
64f52338 12576 if (htab->stab_info.stabstr == o)
3722b82f 12577 continue;
64f52338 12578 if (htab->eh_info.hdr_sec == o)
eea6121a 12579 continue;
3d4d4302 12580 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12581 {
12582 if (! bfd_set_section_contents (abfd, o->output_section,
12583 o->contents,
37b01f6a
DG
12584 (file_ptr) o->output_offset
12585 * bfd_octets_per_byte (abfd),
eea6121a 12586 o->size))
c152c796
AM
12587 goto error_return;
12588 }
12589 else
12590 {
12591 /* The contents of the .dynstr section are actually in a
12592 stringtab. */
8539e4e8
AM
12593 file_ptr off;
12594
c152c796
AM
12595 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12596 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12597 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12598 goto error_return;
12599 }
12600 }
12601 }
12602
7bdf4127 12603 if (!info->resolve_section_groups)
c152c796
AM
12604 {
12605 bfd_boolean failed = FALSE;
12606
7bdf4127 12607 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12608 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12609 if (failed)
12610 goto error_return;
12611 }
12612
12613 /* If we have optimized stabs strings, output them. */
64f52338 12614 if (htab->stab_info.stabstr != NULL)
c152c796 12615 {
64f52338 12616 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12617 goto error_return;
12618 }
12619
9f7c3e5e
AM
12620 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12621 goto error_return;
c152c796 12622
9f7c3e5e 12623 elf_final_link_free (abfd, &flinfo);
c152c796 12624
12bd6957 12625 elf_linker (abfd) = TRUE;
c152c796 12626
104d59d1
JM
12627 if (attr_section)
12628 {
a50b1753 12629 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12630 if (contents == NULL)
d0f16d5e 12631 return FALSE; /* Bail out and fail. */
104d59d1
JM
12632 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12633 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12634 free (contents);
12635 }
12636
c152c796
AM
12637 return TRUE;
12638
12639 error_return:
9f7c3e5e 12640 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12641 return FALSE;
12642}
12643\f
5241d853
RS
12644/* Initialize COOKIE for input bfd ABFD. */
12645
12646static bfd_boolean
12647init_reloc_cookie (struct elf_reloc_cookie *cookie,
12648 struct bfd_link_info *info, bfd *abfd)
12649{
12650 Elf_Internal_Shdr *symtab_hdr;
12651 const struct elf_backend_data *bed;
12652
12653 bed = get_elf_backend_data (abfd);
12654 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12655
12656 cookie->abfd = abfd;
12657 cookie->sym_hashes = elf_sym_hashes (abfd);
12658 cookie->bad_symtab = elf_bad_symtab (abfd);
12659 if (cookie->bad_symtab)
12660 {
12661 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12662 cookie->extsymoff = 0;
12663 }
12664 else
12665 {
12666 cookie->locsymcount = symtab_hdr->sh_info;
12667 cookie->extsymoff = symtab_hdr->sh_info;
12668 }
12669
12670 if (bed->s->arch_size == 32)
12671 cookie->r_sym_shift = 8;
12672 else
12673 cookie->r_sym_shift = 32;
12674
12675 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12676 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12677 {
12678 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12679 cookie->locsymcount, 0,
12680 NULL, NULL, NULL);
12681 if (cookie->locsyms == NULL)
12682 {
12683 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12684 return FALSE;
12685 }
12686 if (info->keep_memory)
12687 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12688 }
12689 return TRUE;
12690}
12691
12692/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12693
12694static void
12695fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12696{
12697 Elf_Internal_Shdr *symtab_hdr;
12698
12699 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12700 if (cookie->locsyms != NULL
12701 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12702 free (cookie->locsyms);
12703}
12704
12705/* Initialize the relocation information in COOKIE for input section SEC
12706 of input bfd ABFD. */
12707
12708static bfd_boolean
12709init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12710 struct bfd_link_info *info, bfd *abfd,
12711 asection *sec)
12712{
5241d853
RS
12713 if (sec->reloc_count == 0)
12714 {
12715 cookie->rels = NULL;
12716 cookie->relend = NULL;
12717 }
12718 else
12719 {
5241d853
RS
12720 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12721 info->keep_memory);
12722 if (cookie->rels == NULL)
12723 return FALSE;
12724 cookie->rel = cookie->rels;
056bafd4 12725 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12726 }
12727 cookie->rel = cookie->rels;
12728 return TRUE;
12729}
12730
12731/* Free the memory allocated by init_reloc_cookie_rels,
12732 if appropriate. */
12733
12734static void
12735fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12736 asection *sec)
12737{
12738 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12739 free (cookie->rels);
12740}
12741
12742/* Initialize the whole of COOKIE for input section SEC. */
12743
12744static bfd_boolean
12745init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12746 struct bfd_link_info *info,
12747 asection *sec)
12748{
12749 if (!init_reloc_cookie (cookie, info, sec->owner))
12750 goto error1;
12751 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12752 goto error2;
12753 return TRUE;
12754
12755 error2:
12756 fini_reloc_cookie (cookie, sec->owner);
12757 error1:
12758 return FALSE;
12759}
12760
12761/* Free the memory allocated by init_reloc_cookie_for_section,
12762 if appropriate. */
12763
12764static void
12765fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12766 asection *sec)
12767{
12768 fini_reloc_cookie_rels (cookie, sec);
12769 fini_reloc_cookie (cookie, sec->owner);
12770}
12771\f
c152c796
AM
12772/* Garbage collect unused sections. */
12773
07adf181
AM
12774/* Default gc_mark_hook. */
12775
12776asection *
12777_bfd_elf_gc_mark_hook (asection *sec,
12778 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12779 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12780 struct elf_link_hash_entry *h,
12781 Elf_Internal_Sym *sym)
12782{
12783 if (h != NULL)
12784 {
12785 switch (h->root.type)
12786 {
12787 case bfd_link_hash_defined:
12788 case bfd_link_hash_defweak:
12789 return h->root.u.def.section;
12790
12791 case bfd_link_hash_common:
12792 return h->root.u.c.p->section;
12793
12794 default:
12795 break;
12796 }
12797 }
12798 else
12799 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12800
12801 return NULL;
12802}
12803
b7c871ed
L
12804/* Return the global debug definition section. */
12805
12806static asection *
12807elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12808 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12809 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12810 struct elf_link_hash_entry *h,
12811 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12812{
12813 if (h != NULL
12814 && (h->root.type == bfd_link_hash_defined
12815 || h->root.type == bfd_link_hash_defweak)
12816 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12817 return h->root.u.def.section;
12818
12819 return NULL;
12820}
12821
5241d853
RS
12822/* COOKIE->rel describes a relocation against section SEC, which is
12823 a section we've decided to keep. Return the section that contains
12824 the relocation symbol, or NULL if no section contains it. */
12825
12826asection *
12827_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12828 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12829 struct elf_reloc_cookie *cookie,
12830 bfd_boolean *start_stop)
5241d853
RS
12831{
12832 unsigned long r_symndx;
12833 struct elf_link_hash_entry *h;
12834
12835 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12836 if (r_symndx == STN_UNDEF)
5241d853
RS
12837 return NULL;
12838
12839 if (r_symndx >= cookie->locsymcount
12840 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12841 {
12842 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12843 if (h == NULL)
12844 {
871b3ab2 12845 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
12846 sec->owner);
12847 return NULL;
12848 }
5241d853
RS
12849 while (h->root.type == bfd_link_hash_indirect
12850 || h->root.type == bfd_link_hash_warning)
12851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12852 h->mark = 1;
4e6b54a6
AM
12853 /* If this symbol is weak and there is a non-weak definition, we
12854 keep the non-weak definition because many backends put
12855 dynamic reloc info on the non-weak definition for code
12856 handling copy relocs. */
60d67dc8
AM
12857 if (h->is_weakalias)
12858 weakdef (h)->mark = 1;
1cce69b9 12859
a6a4679f 12860 if (start_stop != NULL)
1cce69b9 12861 {
7dba9362
AM
12862 /* To work around a glibc bug, mark XXX input sections
12863 when there is a reference to __start_XXX or __stop_XXX
12864 symbols. */
cbd0eecf 12865 if (h->start_stop)
1cce69b9 12866 {
cbd0eecf 12867 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12868 *start_stop = !s->gc_mark;
12869 return s;
1cce69b9
AM
12870 }
12871 }
12872
5241d853
RS
12873 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12874 }
12875
12876 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12877 &cookie->locsyms[r_symndx]);
12878}
12879
12880/* COOKIE->rel describes a relocation against section SEC, which is
12881 a section we've decided to keep. Mark the section that contains
9d0a14d3 12882 the relocation symbol. */
5241d853
RS
12883
12884bfd_boolean
12885_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12886 asection *sec,
12887 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12888 struct elf_reloc_cookie *cookie)
5241d853
RS
12889{
12890 asection *rsec;
1cce69b9 12891 bfd_boolean start_stop = FALSE;
5241d853 12892
1cce69b9
AM
12893 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12894 while (rsec != NULL)
5241d853 12895 {
1cce69b9
AM
12896 if (!rsec->gc_mark)
12897 {
12898 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12899 || (rsec->owner->flags & DYNAMIC) != 0)
12900 rsec->gc_mark = 1;
12901 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12902 return FALSE;
12903 }
12904 if (!start_stop)
12905 break;
199af150 12906 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12907 }
12908 return TRUE;
12909}
12910
07adf181
AM
12911/* The mark phase of garbage collection. For a given section, mark
12912 it and any sections in this section's group, and all the sections
12913 which define symbols to which it refers. */
12914
ccfa59ea
AM
12915bfd_boolean
12916_bfd_elf_gc_mark (struct bfd_link_info *info,
12917 asection *sec,
6a5bb875 12918 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12919{
12920 bfd_boolean ret;
9d0a14d3 12921 asection *group_sec, *eh_frame;
c152c796
AM
12922
12923 sec->gc_mark = 1;
12924
12925 /* Mark all the sections in the group. */
12926 group_sec = elf_section_data (sec)->next_in_group;
12927 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12928 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12929 return FALSE;
12930
12931 /* Look through the section relocs. */
12932 ret = TRUE;
9d0a14d3
RS
12933 eh_frame = elf_eh_frame_section (sec->owner);
12934 if ((sec->flags & SEC_RELOC) != 0
12935 && sec->reloc_count > 0
12936 && sec != eh_frame)
c152c796 12937 {
5241d853 12938 struct elf_reloc_cookie cookie;
c152c796 12939
5241d853
RS
12940 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12941 ret = FALSE;
c152c796 12942 else
c152c796 12943 {
5241d853 12944 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12945 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12946 {
12947 ret = FALSE;
12948 break;
12949 }
12950 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12951 }
12952 }
9d0a14d3
RS
12953
12954 if (ret && eh_frame && elf_fde_list (sec))
12955 {
12956 struct elf_reloc_cookie cookie;
12957
12958 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12959 ret = FALSE;
12960 else
12961 {
12962 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12963 gc_mark_hook, &cookie))
12964 ret = FALSE;
12965 fini_reloc_cookie_for_section (&cookie, eh_frame);
12966 }
12967 }
12968
2f0c68f2
CM
12969 eh_frame = elf_section_eh_frame_entry (sec);
12970 if (ret && eh_frame && !eh_frame->gc_mark)
12971 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12972 ret = FALSE;
12973
c152c796
AM
12974 return ret;
12975}
12976
3c758495
TG
12977/* Scan and mark sections in a special or debug section group. */
12978
12979static void
12980_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12981{
12982 /* Point to first section of section group. */
12983 asection *ssec;
12984 /* Used to iterate the section group. */
12985 asection *msec;
12986
12987 bfd_boolean is_special_grp = TRUE;
12988 bfd_boolean is_debug_grp = TRUE;
12989
12990 /* First scan to see if group contains any section other than debug
12991 and special section. */
12992 ssec = msec = elf_next_in_group (grp);
12993 do
12994 {
12995 if ((msec->flags & SEC_DEBUGGING) == 0)
12996 is_debug_grp = FALSE;
12997
12998 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12999 is_special_grp = FALSE;
13000
13001 msec = elf_next_in_group (msec);
13002 }
13003 while (msec != ssec);
13004
13005 /* If this is a pure debug section group or pure special section group,
13006 keep all sections in this group. */
13007 if (is_debug_grp || is_special_grp)
13008 {
13009 do
13010 {
13011 msec->gc_mark = 1;
13012 msec = elf_next_in_group (msec);
13013 }
13014 while (msec != ssec);
13015 }
13016}
13017
7f6ab9f8
AM
13018/* Keep debug and special sections. */
13019
13020bfd_boolean
13021_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13022 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13023{
13024 bfd *ibfd;
13025
c72f2fb2 13026 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13027 {
13028 asection *isec;
13029 bfd_boolean some_kept;
b40bf0a2 13030 bfd_boolean debug_frag_seen;
b7c871ed 13031 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13032
13033 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13034 continue;
57963c05
AM
13035 isec = ibfd->sections;
13036 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13037 continue;
7f6ab9f8 13038
b40bf0a2
NC
13039 /* Ensure all linker created sections are kept,
13040 see if any other section is already marked,
13041 and note if we have any fragmented debug sections. */
b7c871ed 13042 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13043 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13044 {
13045 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13046 isec->gc_mark = 1;
eb026f09
AM
13047 else if (isec->gc_mark
13048 && (isec->flags & SEC_ALLOC) != 0
13049 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13050 some_kept = TRUE;
b40bf0a2 13051
535b785f 13052 if (!debug_frag_seen
b40bf0a2
NC
13053 && (isec->flags & SEC_DEBUGGING)
13054 && CONST_STRNEQ (isec->name, ".debug_line."))
13055 debug_frag_seen = TRUE;
7f6ab9f8
AM
13056 }
13057
eb026f09
AM
13058 /* If no non-note alloc section in this file will be kept, then
13059 we can toss out the debug and special sections. */
7f6ab9f8
AM
13060 if (!some_kept)
13061 continue;
13062
13063 /* Keep debug and special sections like .comment when they are
3c758495
TG
13064 not part of a group. Also keep section groups that contain
13065 just debug sections or special sections. */
7f6ab9f8 13066 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13067 {
13068 if ((isec->flags & SEC_GROUP) != 0)
13069 _bfd_elf_gc_mark_debug_special_section_group (isec);
13070 else if (((isec->flags & SEC_DEBUGGING) != 0
13071 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13072 && elf_next_in_group (isec) == NULL)
13073 isec->gc_mark = 1;
b7c871ed
L
13074 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13075 has_kept_debug_info = TRUE;
3c758495 13076 }
b40bf0a2 13077
b40bf0a2
NC
13078 /* Look for CODE sections which are going to be discarded,
13079 and find and discard any fragmented debug sections which
13080 are associated with that code section. */
b7c871ed
L
13081 if (debug_frag_seen)
13082 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13083 if ((isec->flags & SEC_CODE) != 0
13084 && isec->gc_mark == 0)
13085 {
13086 unsigned int ilen;
13087 asection *dsec;
b40bf0a2 13088
b7c871ed 13089 ilen = strlen (isec->name);
b40bf0a2 13090
b7c871ed 13091 /* Association is determined by the name of the debug
07d6d2b8 13092 section containing the name of the code section as
b7c871ed
L
13093 a suffix. For example .debug_line.text.foo is a
13094 debug section associated with .text.foo. */
13095 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13096 {
13097 unsigned int dlen;
b40bf0a2 13098
b7c871ed
L
13099 if (dsec->gc_mark == 0
13100 || (dsec->flags & SEC_DEBUGGING) == 0)
13101 continue;
b40bf0a2 13102
b7c871ed 13103 dlen = strlen (dsec->name);
b40bf0a2 13104
b7c871ed
L
13105 if (dlen > ilen
13106 && strncmp (dsec->name + (dlen - ilen),
13107 isec->name, ilen) == 0)
b40bf0a2 13108 dsec->gc_mark = 0;
b7c871ed 13109 }
b40bf0a2 13110 }
b7c871ed
L
13111
13112 /* Mark debug sections referenced by kept debug sections. */
13113 if (has_kept_debug_info)
13114 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13115 if (isec->gc_mark
13116 && (isec->flags & SEC_DEBUGGING) != 0)
13117 if (!_bfd_elf_gc_mark (info, isec,
13118 elf_gc_mark_debug_section))
13119 return FALSE;
7f6ab9f8
AM
13120 }
13121 return TRUE;
13122}
13123
c152c796 13124static bfd_boolean
ccabcbe5 13125elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13126{
13127 bfd *sub;
ccabcbe5 13128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13129
c72f2fb2 13130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13131 {
13132 asection *o;
13133
b19a8f85 13134 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13135 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13136 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13137 continue;
57963c05
AM
13138 o = sub->sections;
13139 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13140 continue;
c152c796
AM
13141
13142 for (o = sub->sections; o != NULL; o = o->next)
13143 {
a33dafc3
L
13144 /* When any section in a section group is kept, we keep all
13145 sections in the section group. If the first member of
13146 the section group is excluded, we will also exclude the
13147 group section. */
13148 if (o->flags & SEC_GROUP)
13149 {
13150 asection *first = elf_next_in_group (o);
13151 o->gc_mark = first->gc_mark;
13152 }
c152c796 13153
1e7eae0d 13154 if (o->gc_mark)
c152c796
AM
13155 continue;
13156
13157 /* Skip sweeping sections already excluded. */
13158 if (o->flags & SEC_EXCLUDE)
13159 continue;
13160
13161 /* Since this is early in the link process, it is simple
13162 to remove a section from the output. */
13163 o->flags |= SEC_EXCLUDE;
13164
c55fe096 13165 if (info->print_gc_sections && o->size != 0)
695344c0 13166 /* xgettext:c-format */
871b3ab2 13167 _bfd_error_handler (_("Removing unused section '%pA' in file '%pB'"),
c08bb8dd 13168 o, sub);
c152c796
AM
13169 }
13170 }
13171
c152c796
AM
13172 return TRUE;
13173}
13174
13175/* Propagate collected vtable information. This is called through
13176 elf_link_hash_traverse. */
13177
13178static bfd_boolean
13179elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13180{
c152c796 13181 /* Those that are not vtables. */
cbd0eecf
L
13182 if (h->start_stop
13183 || h->u2.vtable == NULL
13184 || h->u2.vtable->parent == NULL)
c152c796
AM
13185 return TRUE;
13186
13187 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13188 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13189 return TRUE;
13190
13191 /* If we've already been done, exit. */
cbd0eecf 13192 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13193 return TRUE;
13194
13195 /* Make sure the parent's table is up to date. */
cbd0eecf 13196 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13197
cbd0eecf 13198 if (h->u2.vtable->used == NULL)
c152c796
AM
13199 {
13200 /* None of this table's entries were referenced. Re-use the
13201 parent's table. */
cbd0eecf
L
13202 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13203 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13204 }
13205 else
13206 {
13207 size_t n;
13208 bfd_boolean *cu, *pu;
13209
13210 /* Or the parent's entries into ours. */
cbd0eecf 13211 cu = h->u2.vtable->used;
c152c796 13212 cu[-1] = TRUE;
cbd0eecf 13213 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13214 if (pu != NULL)
13215 {
13216 const struct elf_backend_data *bed;
13217 unsigned int log_file_align;
13218
13219 bed = get_elf_backend_data (h->root.u.def.section->owner);
13220 log_file_align = bed->s->log_file_align;
cbd0eecf 13221 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13222 while (n--)
13223 {
13224 if (*pu)
13225 *cu = TRUE;
13226 pu++;
13227 cu++;
13228 }
13229 }
13230 }
13231
13232 return TRUE;
13233}
13234
13235static bfd_boolean
13236elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13237{
13238 asection *sec;
13239 bfd_vma hstart, hend;
13240 Elf_Internal_Rela *relstart, *relend, *rel;
13241 const struct elf_backend_data *bed;
13242 unsigned int log_file_align;
13243
c152c796
AM
13244 /* Take care of both those symbols that do not describe vtables as
13245 well as those that are not loaded. */
cbd0eecf
L
13246 if (h->start_stop
13247 || h->u2.vtable == NULL
13248 || h->u2.vtable->parent == NULL)
c152c796
AM
13249 return TRUE;
13250
13251 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13252 || h->root.type == bfd_link_hash_defweak);
13253
13254 sec = h->root.u.def.section;
13255 hstart = h->root.u.def.value;
13256 hend = hstart + h->size;
13257
13258 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13259 if (!relstart)
13260 return *(bfd_boolean *) okp = FALSE;
13261 bed = get_elf_backend_data (sec->owner);
13262 log_file_align = bed->s->log_file_align;
13263
056bafd4 13264 relend = relstart + sec->reloc_count;
c152c796
AM
13265
13266 for (rel = relstart; rel < relend; ++rel)
13267 if (rel->r_offset >= hstart && rel->r_offset < hend)
13268 {
13269 /* If the entry is in use, do nothing. */
cbd0eecf
L
13270 if (h->u2.vtable->used
13271 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13272 {
13273 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13274 if (h->u2.vtable->used[entry])
c152c796
AM
13275 continue;
13276 }
13277 /* Otherwise, kill it. */
13278 rel->r_offset = rel->r_info = rel->r_addend = 0;
13279 }
13280
13281 return TRUE;
13282}
13283
87538722
AM
13284/* Mark sections containing dynamically referenced symbols. When
13285 building shared libraries, we must assume that any visible symbol is
13286 referenced. */
715df9b8 13287
64d03ab5
AM
13288bfd_boolean
13289bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13290{
87538722 13291 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13292 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13293
715df9b8
EB
13294 if ((h->root.type == bfd_link_hash_defined
13295 || h->root.type == bfd_link_hash_defweak)
d664fd41 13296 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13297 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13298 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13299 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13300 && (!bfd_link_executable (info)
22185505 13301 || info->gc_keep_exported
b407645f
AM
13302 || info->export_dynamic
13303 || (h->dynamic
13304 && d != NULL
13305 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13306 && (h->versioned >= versioned
54e8959c
L
13307 || !bfd_hide_sym_by_version (info->version_info,
13308 h->root.root.string)))))
715df9b8
EB
13309 h->root.u.def.section->flags |= SEC_KEEP;
13310
13311 return TRUE;
13312}
3b36f7e6 13313
74f0fb50
AM
13314/* Keep all sections containing symbols undefined on the command-line,
13315 and the section containing the entry symbol. */
13316
13317void
13318_bfd_elf_gc_keep (struct bfd_link_info *info)
13319{
13320 struct bfd_sym_chain *sym;
13321
13322 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13323 {
13324 struct elf_link_hash_entry *h;
13325
13326 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13327 FALSE, FALSE, FALSE);
13328
13329 if (h != NULL
13330 && (h->root.type == bfd_link_hash_defined
13331 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13332 && !bfd_is_abs_section (h->root.u.def.section)
13333 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13334 h->root.u.def.section->flags |= SEC_KEEP;
13335 }
13336}
13337
2f0c68f2
CM
13338bfd_boolean
13339bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13340 struct bfd_link_info *info)
13341{
13342 bfd *ibfd = info->input_bfds;
13343
13344 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13345 {
13346 asection *sec;
13347 struct elf_reloc_cookie cookie;
13348
13349 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13350 continue;
57963c05
AM
13351 sec = ibfd->sections;
13352 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13353 continue;
2f0c68f2
CM
13354
13355 if (!init_reloc_cookie (&cookie, info, ibfd))
13356 return FALSE;
13357
13358 for (sec = ibfd->sections; sec; sec = sec->next)
13359 {
13360 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13361 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13362 {
13363 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13364 fini_reloc_cookie_rels (&cookie, sec);
13365 }
13366 }
13367 }
13368 return TRUE;
13369}
13370
c152c796
AM
13371/* Do mark and sweep of unused sections. */
13372
13373bfd_boolean
13374bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13375{
13376 bfd_boolean ok = TRUE;
13377 bfd *sub;
6a5bb875 13378 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13380 struct elf_link_hash_table *htab;
c152c796 13381
64d03ab5 13382 if (!bed->can_gc_sections
715df9b8 13383 || !is_elf_hash_table (info->hash))
c152c796 13384 {
4eca0228 13385 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13386 return TRUE;
13387 }
13388
74f0fb50 13389 bed->gc_keep (info);
da44f4e5 13390 htab = elf_hash_table (info);
74f0fb50 13391
9d0a14d3
RS
13392 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13393 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13394 for (sub = info->input_bfds;
13395 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13396 sub = sub->link.next)
9d0a14d3
RS
13397 {
13398 asection *sec;
13399 struct elf_reloc_cookie cookie;
13400
57963c05
AM
13401 sec = sub->sections;
13402 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13403 continue;
9d0a14d3 13404 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13405 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13406 {
13407 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13408 if (elf_section_data (sec)->sec_info
13409 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13410 elf_eh_frame_section (sub) = sec;
13411 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13412 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13413 }
13414 }
9d0a14d3 13415
c152c796 13416 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13417 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13418 if (!ok)
13419 return FALSE;
13420
13421 /* Kill the vtable relocations that were not used. */
da44f4e5 13422 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13423 if (!ok)
13424 return FALSE;
13425
715df9b8 13426 /* Mark dynamically referenced symbols. */
22185505 13427 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13428 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13429
715df9b8 13430 /* Grovel through relocs to find out who stays ... */
64d03ab5 13431 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13432 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13433 {
13434 asection *o;
13435
b19a8f85 13436 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13437 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13438 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13439 continue;
13440
57963c05
AM
13441 o = sub->sections;
13442 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13443 continue;
13444
7f6ab9f8
AM
13445 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13446 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13447 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13448 well as FINI_ARRAY sections for ld -r. */
c152c796 13449 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13450 if (!o->gc_mark
13451 && (o->flags & SEC_EXCLUDE) == 0
24007750 13452 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13453 || (bfd_link_relocatable (info)
13454 && ((elf_section_data (o)->this_hdr.sh_type
13455 == SHT_PREINIT_ARRAY)
13456 || (elf_section_data (o)->this_hdr.sh_type
13457 == SHT_INIT_ARRAY)
13458 || (elf_section_data (o)->this_hdr.sh_type
13459 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13460 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13461 && elf_next_in_group (o) == NULL )))
13462 {
13463 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13464 return FALSE;
13465 }
c152c796
AM
13466 }
13467
6a5bb875 13468 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13469 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13470
c152c796 13471 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13472 return elf_gc_sweep (abfd, info);
c152c796
AM
13473}
13474\f
13475/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13476
13477bfd_boolean
13478bfd_elf_gc_record_vtinherit (bfd *abfd,
13479 asection *sec,
13480 struct elf_link_hash_entry *h,
13481 bfd_vma offset)
13482{
13483 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13484 struct elf_link_hash_entry **search, *child;
ef53be89 13485 size_t extsymcount;
c152c796
AM
13486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13487
13488 /* The sh_info field of the symtab header tells us where the
13489 external symbols start. We don't care about the local symbols at
13490 this point. */
13491 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13492 if (!elf_bad_symtab (abfd))
13493 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13494
13495 sym_hashes = elf_sym_hashes (abfd);
13496 sym_hashes_end = sym_hashes + extsymcount;
13497
13498 /* Hunt down the child symbol, which is in this section at the same
13499 offset as the relocation. */
13500 for (search = sym_hashes; search != sym_hashes_end; ++search)
13501 {
13502 if ((child = *search) != NULL
13503 && (child->root.type == bfd_link_hash_defined
13504 || child->root.type == bfd_link_hash_defweak)
13505 && child->root.u.def.section == sec
13506 && child->root.u.def.value == offset)
13507 goto win;
13508 }
13509
695344c0 13510 /* xgettext:c-format */
871b3ab2 13511 _bfd_error_handler (_("%pB: %pA+%#Lx: No symbol found for INHERIT"),
76cfced5 13512 abfd, sec, offset);
c152c796
AM
13513 bfd_set_error (bfd_error_invalid_operation);
13514 return FALSE;
13515
13516 win:
cbd0eecf 13517 if (!child->u2.vtable)
f6e332e6 13518 {
cbd0eecf
L
13519 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13520 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13521 if (!child->u2.vtable)
f6e332e6
AM
13522 return FALSE;
13523 }
c152c796
AM
13524 if (!h)
13525 {
13526 /* This *should* only be the absolute section. It could potentially
13527 be that someone has defined a non-global vtable though, which
13528 would be bad. It isn't worth paging in the local symbols to be
13529 sure though; that case should simply be handled by the assembler. */
13530
cbd0eecf 13531 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13532 }
13533 else
cbd0eecf 13534 child->u2.vtable->parent = h;
c152c796
AM
13535
13536 return TRUE;
13537}
13538
13539/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13540
13541bfd_boolean
13542bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13543 asection *sec ATTRIBUTE_UNUSED,
13544 struct elf_link_hash_entry *h,
13545 bfd_vma addend)
13546{
13547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13548 unsigned int log_file_align = bed->s->log_file_align;
13549
cbd0eecf 13550 if (!h->u2.vtable)
f6e332e6 13551 {
cbd0eecf
L
13552 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13553 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13554 if (!h->u2.vtable)
f6e332e6
AM
13555 return FALSE;
13556 }
13557
cbd0eecf 13558 if (addend >= h->u2.vtable->size)
c152c796
AM
13559 {
13560 size_t size, bytes, file_align;
cbd0eecf 13561 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13562
13563 /* While the symbol is undefined, we have to be prepared to handle
13564 a zero size. */
13565 file_align = 1 << log_file_align;
13566 if (h->root.type == bfd_link_hash_undefined)
13567 size = addend + file_align;
13568 else
13569 {
13570 size = h->size;
13571 if (addend >= size)
13572 {
13573 /* Oops! We've got a reference past the defined end of
13574 the table. This is probably a bug -- shall we warn? */
13575 size = addend + file_align;
13576 }
13577 }
13578 size = (size + file_align - 1) & -file_align;
13579
13580 /* Allocate one extra entry for use as a "done" flag for the
13581 consolidation pass. */
13582 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13583
13584 if (ptr)
13585 {
a50b1753 13586 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13587
13588 if (ptr != NULL)
13589 {
13590 size_t oldbytes;
13591
cbd0eecf 13592 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13593 * sizeof (bfd_boolean));
13594 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13595 }
13596 }
13597 else
a50b1753 13598 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13599
13600 if (ptr == NULL)
13601 return FALSE;
13602
13603 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13604 h->u2.vtable->used = ptr + 1;
13605 h->u2.vtable->size = size;
c152c796
AM
13606 }
13607
cbd0eecf 13608 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13609
13610 return TRUE;
13611}
13612
ae17ab41
CM
13613/* Map an ELF section header flag to its corresponding string. */
13614typedef struct
13615{
13616 char *flag_name;
13617 flagword flag_value;
13618} elf_flags_to_name_table;
13619
13620static elf_flags_to_name_table elf_flags_to_names [] =
13621{
13622 { "SHF_WRITE", SHF_WRITE },
13623 { "SHF_ALLOC", SHF_ALLOC },
13624 { "SHF_EXECINSTR", SHF_EXECINSTR },
13625 { "SHF_MERGE", SHF_MERGE },
13626 { "SHF_STRINGS", SHF_STRINGS },
13627 { "SHF_INFO_LINK", SHF_INFO_LINK},
13628 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13629 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13630 { "SHF_GROUP", SHF_GROUP },
13631 { "SHF_TLS", SHF_TLS },
13632 { "SHF_MASKOS", SHF_MASKOS },
13633 { "SHF_EXCLUDE", SHF_EXCLUDE },
13634};
13635
b9c361e0
JL
13636/* Returns TRUE if the section is to be included, otherwise FALSE. */
13637bfd_boolean
ae17ab41 13638bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13639 struct flag_info *flaginfo,
b9c361e0 13640 asection *section)
ae17ab41 13641{
8b127cbc 13642 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13643
8b127cbc 13644 if (!flaginfo->flags_initialized)
ae17ab41 13645 {
8b127cbc
AM
13646 bfd *obfd = info->output_bfd;
13647 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13648 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13649 int with_hex = 0;
13650 int without_hex = 0;
13651
8b127cbc 13652 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13653 {
b9c361e0 13654 unsigned i;
8b127cbc 13655 flagword (*lookup) (char *);
ae17ab41 13656
8b127cbc
AM
13657 lookup = bed->elf_backend_lookup_section_flags_hook;
13658 if (lookup != NULL)
ae17ab41 13659 {
8b127cbc 13660 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13661
13662 if (hexval != 0)
13663 {
13664 if (tf->with == with_flags)
13665 with_hex |= hexval;
13666 else if (tf->with == without_flags)
13667 without_hex |= hexval;
13668 tf->valid = TRUE;
13669 continue;
13670 }
ae17ab41 13671 }
8b127cbc 13672 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13673 {
8b127cbc 13674 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13675 {
13676 if (tf->with == with_flags)
13677 with_hex |= elf_flags_to_names[i].flag_value;
13678 else if (tf->with == without_flags)
13679 without_hex |= elf_flags_to_names[i].flag_value;
13680 tf->valid = TRUE;
13681 break;
13682 }
13683 }
8b127cbc 13684 if (!tf->valid)
b9c361e0 13685 {
68ffbac6 13686 info->callbacks->einfo
8b127cbc 13687 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13688 return FALSE;
ae17ab41
CM
13689 }
13690 }
8b127cbc
AM
13691 flaginfo->flags_initialized = TRUE;
13692 flaginfo->only_with_flags |= with_hex;
13693 flaginfo->not_with_flags |= without_hex;
ae17ab41 13694 }
ae17ab41 13695
8b127cbc 13696 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13697 return FALSE;
13698
8b127cbc 13699 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13700 return FALSE;
13701
13702 return TRUE;
ae17ab41
CM
13703}
13704
c152c796
AM
13705struct alloc_got_off_arg {
13706 bfd_vma gotoff;
10455f89 13707 struct bfd_link_info *info;
c152c796
AM
13708};
13709
13710/* We need a special top-level link routine to convert got reference counts
13711 to real got offsets. */
13712
13713static bfd_boolean
13714elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13715{
a50b1753 13716 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13717 bfd *obfd = gofarg->info->output_bfd;
13718 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13719
c152c796
AM
13720 if (h->got.refcount > 0)
13721 {
13722 h->got.offset = gofarg->gotoff;
10455f89 13723 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13724 }
13725 else
13726 h->got.offset = (bfd_vma) -1;
13727
13728 return TRUE;
13729}
13730
13731/* And an accompanying bit to work out final got entry offsets once
13732 we're done. Should be called from final_link. */
13733
13734bfd_boolean
13735bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13736 struct bfd_link_info *info)
13737{
13738 bfd *i;
13739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13740 bfd_vma gotoff;
c152c796
AM
13741 struct alloc_got_off_arg gofarg;
13742
10455f89
HPN
13743 BFD_ASSERT (abfd == info->output_bfd);
13744
c152c796
AM
13745 if (! is_elf_hash_table (info->hash))
13746 return FALSE;
13747
13748 /* The GOT offset is relative to the .got section, but the GOT header is
13749 put into the .got.plt section, if the backend uses it. */
13750 if (bed->want_got_plt)
13751 gotoff = 0;
13752 else
13753 gotoff = bed->got_header_size;
13754
13755 /* Do the local .got entries first. */
c72f2fb2 13756 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13757 {
13758 bfd_signed_vma *local_got;
ef53be89 13759 size_t j, locsymcount;
c152c796
AM
13760 Elf_Internal_Shdr *symtab_hdr;
13761
13762 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13763 continue;
13764
13765 local_got = elf_local_got_refcounts (i);
13766 if (!local_got)
13767 continue;
13768
13769 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13770 if (elf_bad_symtab (i))
13771 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13772 else
13773 locsymcount = symtab_hdr->sh_info;
13774
13775 for (j = 0; j < locsymcount; ++j)
13776 {
13777 if (local_got[j] > 0)
13778 {
13779 local_got[j] = gotoff;
10455f89 13780 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13781 }
13782 else
13783 local_got[j] = (bfd_vma) -1;
13784 }
13785 }
13786
13787 /* Then the global .got entries. .plt refcounts are handled by
13788 adjust_dynamic_symbol */
13789 gofarg.gotoff = gotoff;
10455f89 13790 gofarg.info = info;
c152c796
AM
13791 elf_link_hash_traverse (elf_hash_table (info),
13792 elf_gc_allocate_got_offsets,
13793 &gofarg);
13794 return TRUE;
13795}
13796
13797/* Many folk need no more in the way of final link than this, once
13798 got entry reference counting is enabled. */
13799
13800bfd_boolean
13801bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13802{
13803 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13804 return FALSE;
13805
13806 /* Invoke the regular ELF backend linker to do all the work. */
13807 return bfd_elf_final_link (abfd, info);
13808}
13809
13810bfd_boolean
13811bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13812{
a50b1753 13813 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13814
13815 if (rcookie->bad_symtab)
13816 rcookie->rel = rcookie->rels;
13817
13818 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13819 {
13820 unsigned long r_symndx;
13821
13822 if (! rcookie->bad_symtab)
13823 if (rcookie->rel->r_offset > offset)
13824 return FALSE;
13825 if (rcookie->rel->r_offset != offset)
13826 continue;
13827
13828 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13829 if (r_symndx == STN_UNDEF)
c152c796
AM
13830 return TRUE;
13831
13832 if (r_symndx >= rcookie->locsymcount
13833 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13834 {
13835 struct elf_link_hash_entry *h;
13836
13837 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13838
13839 while (h->root.type == bfd_link_hash_indirect
13840 || h->root.type == bfd_link_hash_warning)
13841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13842
13843 if ((h->root.type == bfd_link_hash_defined
13844 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13845 && (h->root.u.def.section->owner != rcookie->abfd
13846 || h->root.u.def.section->kept_section != NULL
13847 || discarded_section (h->root.u.def.section)))
c152c796 13848 return TRUE;
c152c796
AM
13849 }
13850 else
13851 {
13852 /* It's not a relocation against a global symbol,
13853 but it could be a relocation against a local
13854 symbol for a discarded section. */
13855 asection *isec;
13856 Elf_Internal_Sym *isym;
13857
13858 /* Need to: get the symbol; get the section. */
13859 isym = &rcookie->locsyms[r_symndx];
cb33740c 13860 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13861 if (isec != NULL
13862 && (isec->kept_section != NULL
13863 || discarded_section (isec)))
cb33740c 13864 return TRUE;
c152c796
AM
13865 }
13866 return FALSE;
13867 }
13868 return FALSE;
13869}
13870
13871/* Discard unneeded references to discarded sections.
75938853
AM
13872 Returns -1 on error, 1 if any section's size was changed, 0 if
13873 nothing changed. This function assumes that the relocations are in
13874 sorted order, which is true for all known assemblers. */
c152c796 13875
75938853 13876int
c152c796
AM
13877bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13878{
13879 struct elf_reloc_cookie cookie;
18cd5bce 13880 asection *o;
c152c796 13881 bfd *abfd;
75938853 13882 int changed = 0;
c152c796
AM
13883
13884 if (info->traditional_format
13885 || !is_elf_hash_table (info->hash))
75938853 13886 return 0;
c152c796 13887
18cd5bce
AM
13888 o = bfd_get_section_by_name (output_bfd, ".stab");
13889 if (o != NULL)
c152c796 13890 {
18cd5bce 13891 asection *i;
c152c796 13892
18cd5bce 13893 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13894 {
18cd5bce
AM
13895 if (i->size == 0
13896 || i->reloc_count == 0
13897 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13898 continue;
c152c796 13899
18cd5bce
AM
13900 abfd = i->owner;
13901 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13902 continue;
c152c796 13903
18cd5bce 13904 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13905 return -1;
c152c796 13906
18cd5bce
AM
13907 if (_bfd_discard_section_stabs (abfd, i,
13908 elf_section_data (i)->sec_info,
5241d853
RS
13909 bfd_elf_reloc_symbol_deleted_p,
13910 &cookie))
75938853 13911 changed = 1;
18cd5bce
AM
13912
13913 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13914 }
18cd5bce
AM
13915 }
13916
2f0c68f2
CM
13917 o = NULL;
13918 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13919 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13920 if (o != NULL)
13921 {
13922 asection *i;
d7153c4a 13923 int eh_changed = 0;
79a94a2a 13924 unsigned int eh_alignment;
c152c796 13925
18cd5bce 13926 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13927 {
18cd5bce
AM
13928 if (i->size == 0)
13929 continue;
13930
13931 abfd = i->owner;
13932 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13933 continue;
13934
13935 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13936 return -1;
18cd5bce
AM
13937
13938 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13939 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13940 bfd_elf_reloc_symbol_deleted_p,
13941 &cookie))
d7153c4a
AM
13942 {
13943 eh_changed = 1;
13944 if (i->size != i->rawsize)
13945 changed = 1;
13946 }
18cd5bce
AM
13947
13948 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13949 }
9866ffe2 13950
79a94a2a 13951 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
13952 /* Skip over zero terminator, and prevent empty sections from
13953 adding alignment padding at the end. */
13954 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13955 if (i->size == 0)
13956 i->flags |= SEC_EXCLUDE;
13957 else if (i->size > 4)
13958 break;
13959 /* The last non-empty eh_frame section doesn't need padding. */
13960 if (i != NULL)
13961 i = i->map_tail.s;
13962 /* Any prior sections must pad the last FDE out to the output
13963 section alignment. Otherwise we might have zero padding
13964 between sections, which would be seen as a terminator. */
13965 for (; i != NULL; i = i->map_tail.s)
13966 if (i->size == 4)
13967 /* All but the last zero terminator should have been removed. */
13968 BFD_FAIL ();
13969 else
13970 {
13971 bfd_size_type size
13972 = (i->size + eh_alignment - 1) & -eh_alignment;
13973 if (i->size != size)
af471f82 13974 {
9866ffe2
AM
13975 i->size = size;
13976 changed = 1;
13977 eh_changed = 1;
af471f82 13978 }
9866ffe2 13979 }
d7153c4a
AM
13980 if (eh_changed)
13981 elf_link_hash_traverse (elf_hash_table (info),
13982 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13983 }
c152c796 13984
18cd5bce
AM
13985 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13986 {
13987 const struct elf_backend_data *bed;
57963c05 13988 asection *s;
c152c796 13989
18cd5bce
AM
13990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13991 continue;
57963c05
AM
13992 s = abfd->sections;
13993 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13994 continue;
18cd5bce
AM
13995
13996 bed = get_elf_backend_data (abfd);
13997
13998 if (bed->elf_backend_discard_info != NULL)
13999 {
14000 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14001 return -1;
18cd5bce
AM
14002
14003 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14004 changed = 1;
18cd5bce
AM
14005
14006 fini_reloc_cookie (&cookie, abfd);
14007 }
c152c796
AM
14008 }
14009
2f0c68f2
CM
14010 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14011 _bfd_elf_end_eh_frame_parsing (info);
14012
14013 if (info->eh_frame_hdr_type
0e1862bb 14014 && !bfd_link_relocatable (info)
c152c796 14015 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14016 changed = 1;
c152c796 14017
75938853 14018 return changed;
c152c796 14019}
082b7297 14020
43e1669b 14021bfd_boolean
0c511000 14022_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14023 asection *sec,
c0f00686 14024 struct bfd_link_info *info)
082b7297
L
14025{
14026 flagword flags;
c77ec726 14027 const char *name, *key;
082b7297
L
14028 struct bfd_section_already_linked *l;
14029 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14030
c77ec726
AM
14031 if (sec->output_section == bfd_abs_section_ptr)
14032 return FALSE;
0c511000 14033
c77ec726 14034 flags = sec->flags;
0c511000 14035
c77ec726
AM
14036 /* Return if it isn't a linkonce section. A comdat group section
14037 also has SEC_LINK_ONCE set. */
14038 if ((flags & SEC_LINK_ONCE) == 0)
14039 return FALSE;
0c511000 14040
c77ec726
AM
14041 /* Don't put group member sections on our list of already linked
14042 sections. They are handled as a group via their group section. */
14043 if (elf_sec_group (sec) != NULL)
14044 return FALSE;
0c511000 14045
c77ec726
AM
14046 /* For a SHT_GROUP section, use the group signature as the key. */
14047 name = sec->name;
14048 if ((flags & SEC_GROUP) != 0
14049 && elf_next_in_group (sec) != NULL
14050 && elf_group_name (elf_next_in_group (sec)) != NULL)
14051 key = elf_group_name (elf_next_in_group (sec));
14052 else
14053 {
14054 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14055 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14056 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14057 key++;
0c511000 14058 else
c77ec726
AM
14059 /* Must be a user linkonce section that doesn't follow gcc's
14060 naming convention. In this case we won't be matching
14061 single member groups. */
14062 key = name;
0c511000 14063 }
6d2cd210 14064
c77ec726 14065 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14066
14067 for (l = already_linked_list->entry; l != NULL; l = l->next)
14068 {
c2370991 14069 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14070 sections with a signature of <key> (<key> is some string),
14071 and linkonce sections named .gnu.linkonce.<type>.<key>.
14072 Match like sections. LTO plugin sections are an exception.
14073 They are always named .gnu.linkonce.t.<key> and match either
14074 type of section. */
14075 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14076 && ((flags & SEC_GROUP) != 0
14077 || strcmp (name, l->sec->name) == 0))
14078 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14079 {
14080 /* The section has already been linked. See if we should
6d2cd210 14081 issue a warning. */
c77ec726
AM
14082 if (!_bfd_handle_already_linked (sec, l, info))
14083 return FALSE;
082b7297 14084
c77ec726 14085 if (flags & SEC_GROUP)
3d7f7666 14086 {
c77ec726
AM
14087 asection *first = elf_next_in_group (sec);
14088 asection *s = first;
3d7f7666 14089
c77ec726 14090 while (s != NULL)
3d7f7666 14091 {
c77ec726
AM
14092 s->output_section = bfd_abs_section_ptr;
14093 /* Record which group discards it. */
14094 s->kept_section = l->sec;
14095 s = elf_next_in_group (s);
14096 /* These lists are circular. */
14097 if (s == first)
14098 break;
3d7f7666
L
14099 }
14100 }
082b7297 14101
43e1669b 14102 return TRUE;
082b7297
L
14103 }
14104 }
14105
c77ec726
AM
14106 /* A single member comdat group section may be discarded by a
14107 linkonce section and vice versa. */
14108 if ((flags & SEC_GROUP) != 0)
3d7f7666 14109 {
c77ec726 14110 asection *first = elf_next_in_group (sec);
c2370991 14111
c77ec726
AM
14112 if (first != NULL && elf_next_in_group (first) == first)
14113 /* Check this single member group against linkonce sections. */
14114 for (l = already_linked_list->entry; l != NULL; l = l->next)
14115 if ((l->sec->flags & SEC_GROUP) == 0
14116 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14117 {
14118 first->output_section = bfd_abs_section_ptr;
14119 first->kept_section = l->sec;
14120 sec->output_section = bfd_abs_section_ptr;
14121 break;
14122 }
14123 }
14124 else
14125 /* Check this linkonce section against single member groups. */
14126 for (l = already_linked_list->entry; l != NULL; l = l->next)
14127 if (l->sec->flags & SEC_GROUP)
6d2cd210 14128 {
c77ec726 14129 asection *first = elf_next_in_group (l->sec);
6d2cd210 14130
c77ec726
AM
14131 if (first != NULL
14132 && elf_next_in_group (first) == first
14133 && bfd_elf_match_symbols_in_sections (first, sec, info))
14134 {
14135 sec->output_section = bfd_abs_section_ptr;
14136 sec->kept_section = first;
14137 break;
14138 }
6d2cd210 14139 }
0c511000 14140
c77ec726
AM
14141 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14142 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14143 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14144 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14145 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14146 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14147 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14148 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14149 The reverse order cannot happen as there is never a bfd with only the
14150 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14151 matter as here were are looking only for cross-bfd sections. */
14152
14153 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14154 for (l = already_linked_list->entry; l != NULL; l = l->next)
14155 if ((l->sec->flags & SEC_GROUP) == 0
14156 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14157 {
14158 if (abfd != l->sec->owner)
14159 sec->output_section = bfd_abs_section_ptr;
14160 break;
14161 }
80c29487 14162
082b7297 14163 /* This is the first section with this name. Record it. */
c77ec726 14164 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14165 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14166 return sec->output_section == bfd_abs_section_ptr;
082b7297 14167}
81e1b023 14168
a4d8e49b
L
14169bfd_boolean
14170_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14171{
14172 return sym->st_shndx == SHN_COMMON;
14173}
14174
14175unsigned int
14176_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14177{
14178 return SHN_COMMON;
14179}
14180
14181asection *
14182_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14183{
14184 return bfd_com_section_ptr;
14185}
10455f89
HPN
14186
14187bfd_vma
14188_bfd_elf_default_got_elt_size (bfd *abfd,
14189 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14190 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14191 bfd *ibfd ATTRIBUTE_UNUSED,
14192 unsigned long symndx ATTRIBUTE_UNUSED)
14193{
14194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14195 return bed->s->arch_size / 8;
14196}
83bac4b0
NC
14197
14198/* Routines to support the creation of dynamic relocs. */
14199
83bac4b0
NC
14200/* Returns the name of the dynamic reloc section associated with SEC. */
14201
14202static const char *
14203get_dynamic_reloc_section_name (bfd * abfd,
14204 asection * sec,
14205 bfd_boolean is_rela)
14206{
ddcf1fcf
BS
14207 char *name;
14208 const char *old_name = bfd_get_section_name (NULL, sec);
14209 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14210
ddcf1fcf 14211 if (old_name == NULL)
83bac4b0
NC
14212 return NULL;
14213
ddcf1fcf 14214 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14215 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14216
14217 return name;
14218}
14219
14220/* Returns the dynamic reloc section associated with SEC.
14221 If necessary compute the name of the dynamic reloc section based
14222 on SEC's name (looked up in ABFD's string table) and the setting
14223 of IS_RELA. */
14224
14225asection *
14226_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14227 asection * sec,
14228 bfd_boolean is_rela)
14229{
14230 asection * reloc_sec = elf_section_data (sec)->sreloc;
14231
14232 if (reloc_sec == NULL)
14233 {
14234 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14235
14236 if (name != NULL)
14237 {
3d4d4302 14238 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14239
14240 if (reloc_sec != NULL)
14241 elf_section_data (sec)->sreloc = reloc_sec;
14242 }
14243 }
14244
14245 return reloc_sec;
14246}
14247
14248/* Returns the dynamic reloc section associated with SEC. If the
14249 section does not exist it is created and attached to the DYNOBJ
14250 bfd and stored in the SRELOC field of SEC's elf_section_data
14251 structure.
f8076f98 14252
83bac4b0
NC
14253 ALIGNMENT is the alignment for the newly created section and
14254 IS_RELA defines whether the name should be .rela.<SEC's name>
14255 or .rel.<SEC's name>. The section name is looked up in the
14256 string table associated with ABFD. */
14257
14258asection *
ca4be51c
AM
14259_bfd_elf_make_dynamic_reloc_section (asection *sec,
14260 bfd *dynobj,
14261 unsigned int alignment,
14262 bfd *abfd,
14263 bfd_boolean is_rela)
83bac4b0
NC
14264{
14265 asection * reloc_sec = elf_section_data (sec)->sreloc;
14266
14267 if (reloc_sec == NULL)
14268 {
14269 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14270
14271 if (name == NULL)
14272 return NULL;
14273
3d4d4302 14274 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14275
14276 if (reloc_sec == NULL)
14277 {
3d4d4302
AM
14278 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14279 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14280 if ((sec->flags & SEC_ALLOC) != 0)
14281 flags |= SEC_ALLOC | SEC_LOAD;
14282
3d4d4302 14283 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14284 if (reloc_sec != NULL)
14285 {
8877b5e5
AM
14286 /* _bfd_elf_get_sec_type_attr chooses a section type by
14287 name. Override as it may be wrong, eg. for a user
14288 section named "auto" we'll get ".relauto" which is
14289 seen to be a .rela section. */
14290 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14291 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14292 reloc_sec = NULL;
14293 }
14294 }
14295
14296 elf_section_data (sec)->sreloc = reloc_sec;
14297 }
14298
14299 return reloc_sec;
14300}
1338dd10 14301
bffebb6b
AM
14302/* Copy the ELF symbol type and other attributes for a linker script
14303 assignment from HSRC to HDEST. Generally this should be treated as
14304 if we found a strong non-dynamic definition for HDEST (except that
14305 ld ignores multiple definition errors). */
1338dd10 14306void
bffebb6b
AM
14307_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14308 struct bfd_link_hash_entry *hdest,
14309 struct bfd_link_hash_entry *hsrc)
1338dd10 14310{
bffebb6b
AM
14311 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14312 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14313 Elf_Internal_Sym isym;
1338dd10
PB
14314
14315 ehdest->type = ehsrc->type;
35fc36a8 14316 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14317
14318 isym.st_other = ehsrc->other;
b8417128 14319 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14320}
351f65ca
L
14321
14322/* Append a RELA relocation REL to section S in BFD. */
14323
14324void
14325elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14326{
14327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14328 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14329 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14330 bed->s->swap_reloca_out (abfd, rel, loc);
14331}
14332
14333/* Append a REL relocation REL to section S in BFD. */
14334
14335void
14336elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14337{
14338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14339 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14340 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14341 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14342}
7dba9362
AM
14343
14344/* Define __start, __stop, .startof. or .sizeof. symbol. */
14345
14346struct bfd_link_hash_entry *
14347bfd_elf_define_start_stop (struct bfd_link_info *info,
14348 const char *symbol, asection *sec)
14349{
487b6440 14350 struct elf_link_hash_entry *h;
7dba9362 14351
487b6440
AM
14352 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14353 FALSE, FALSE, TRUE);
14354 if (h != NULL
14355 && (h->root.type == bfd_link_hash_undefined
14356 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14357 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14358 {
bf3077a6 14359 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14360 h->root.type = bfd_link_hash_defined;
14361 h->root.u.def.section = sec;
14362 h->root.u.def.value = 0;
14363 h->def_regular = 1;
14364 h->def_dynamic = 0;
14365 h->start_stop = 1;
14366 h->u2.start_stop_section = sec;
14367 if (symbol[0] == '.')
14368 {
14369 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14370 const struct elf_backend_data *bed;
14371 bed = get_elf_backend_data (info->output_bfd);
14372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14373 }
36b8fda5
AM
14374 else
14375 {
14376 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14377 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14378 if (was_dynamic)
36b8fda5
AM
14379 bfd_elf_link_record_dynamic_symbol (info, h);
14380 }
487b6440 14381 return &h->root;
7dba9362 14382 }
487b6440 14383 return NULL;
7dba9362 14384}