PR24337, segfault in _bfd_elf_rela_local_sym
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
82704155 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
07d6d2b8 79 return h->root.u.def.section;
2f0c68f2
CM
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
ad32986f 121 bh = &h->root;
d98685ac 122 }
ad32986f
NC
123 else
124 bh = NULL;
d98685ac 125
cf18fda4 126 bed = get_elf_backend_data (abfd);
d98685ac 127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 128 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
ad32986f 132 BFD_ASSERT (h != NULL);
d98685ac 133 h->def_regular = 1;
e28df02b 134 h->non_elf = 0;
12b2843a 135 h->root.linker_def = 1;
d98685ac 136 h->type = STT_OBJECT;
00b7642b
AM
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 139
ccabcbe5 140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
141 return h;
142}
143
b34976b6 144bfd_boolean
268b6b39 145_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
146{
147 flagword flags;
aad5d350 148 asection *s;
252b5132 149 struct elf_link_hash_entry *h;
9c5bfbb7 150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 151 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
152
153 /* This function may be called more than once. */
ce558b89 154 if (htab->sgot != NULL)
b34976b6 155 return TRUE;
252b5132 156
e5a52504 157 flags = bed->dynamic_sec_flags;
252b5132 158
14b2f831
AM
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
6de2ae4a
L
164 if (s == NULL
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
252b5132 168
14b2f831 169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
170 if (s == NULL
171 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
252b5132
RH
175 if (bed->want_got_plt)
176 {
14b2f831 177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 178 if (s == NULL
6de2ae4a
L
179 || !bfd_set_section_alignment (abfd, s,
180 bed->s->log_file_align))
b34976b6 181 return FALSE;
6de2ae4a 182 htab->sgotplt = s;
252b5132
RH
183 }
184
64e77c6d
L
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
187
2517a57f
AM
188 if (bed->want_got_sym)
189 {
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
6de2ae4a
L
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
2517a57f 196 elf_hash_table (info)->hgot = h;
d98685ac
AM
197 if (h == NULL)
198 return FALSE;
2517a57f 199 }
252b5132 200
b34976b6 201 return TRUE;
252b5132
RH
202}
203\f
7e9f0867
AM
204/* Create a strtab to hold the dynamic symbol names. */
205static bfd_boolean
206_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207{
208 struct elf_link_hash_table *hash_table;
209
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
6cd255ca
L
212 {
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 {
219 bfd *ibfd;
57963c05 220 asection *s;
6cd255ca 221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 222 if ((ibfd->flags
57963c05
AM
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
48e30f52
L
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
b531344c
MR
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
45d6a902 698
f5385ebf 699 h->def_regular = 1;
45d6a902 700
eb8476a6 701 if (hidden)
fe21a8fc 702 {
91d6fa6a 703 bed = get_elf_backend_data (output_bfd);
b8297068
AM
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
6fa3860b
PB
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
0e1862bb 711 if (!bfd_link_relocatable (info)
6fa3860b
PB
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
f5385ebf
AM
717 if ((h->def_dynamic
718 || h->ref_dynamic
6b3b0ab8
L
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 721 && !h->forced_local
45d6a902
AM
722 && h->dynindx == -1)
723 {
c152c796 724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
60d67dc8 730 if (h->is_weakalias)
45d6a902 731 {
60d67dc8
AM
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741}
42751cf3 742
8c58d23b
AM
743/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747int
c152c796
AM
748bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
8c58d23b
AM
751{
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
ef53be89 756 size_t dynstr_index;
8c58d23b
AM
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
0eddce27 761 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
a50b1753 770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 776 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 783 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
b34976b6 810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 811 if (dynstr_index == (size_t) -1)
8c58d23b
AM
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830}
831
30b30c21 832/* Return the dynindex of a local dynamic symbol. */
42751cf3 833
30b30c21 834long
268b6b39
AM
835_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
30b30c21
RH
838{
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845}
846
847/* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
b34976b6 851static bfd_boolean
268b6b39
AM
852elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
42751cf3 854{
a50b1753 855 size_t *count = (size_t *) data;
30b30c21 856
6fa3860b
PB
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864}
865
866
867/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870static bfd_boolean
871elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873{
a50b1753 874 size_t *count = (size_t *) data;
6fa3860b 875
6fa3860b
PB
876 if (!h->forced_local)
877 return TRUE;
878
42751cf3 879 if (h->dynindx != -1)
30b30c21
RH
880 h->dynindx = ++(*count);
881
b34976b6 882 return TRUE;
42751cf3 883}
30b30c21 884
aee6f5b4
AO
885/* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887bfd_boolean
d00dd7dc
AM
888_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
aee6f5b4 891{
74541ad4 892 struct elf_link_hash_table *htab;
ca55926c 893 asection *ip;
74541ad4 894
aee6f5b4
AO
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
74541ad4
AM
902 htab = elf_hash_table (info);
903 if (p == htab->tls_sec)
904 return FALSE;
905
906 if (htab->text_index_section != NULL)
907 return p != htab->text_index_section && p != htab->data_index_section;
908
ca55926c 909 return (htab->dynobj != NULL
3d4d4302 910 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 911 && ip->output_section == p);
aee6f5b4
AO
912
913 /* There shouldn't be section relative relocations
914 against any other section. */
915 default:
916 return TRUE;
917 }
918}
919
d00dd7dc
AM
920bfd_boolean
921_bfd_elf_omit_section_dynsym_all
922 (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
924 asection *p ATTRIBUTE_UNUSED)
925{
926 return TRUE;
927}
928
062e2358 929/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
930 symbol for each output section, which come first. Next come symbols
931 which have been forced to local binding. Then all of the back-end
932 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
933 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
934 (This prevents the early call before elf_backend_init_index_section
935 and strip_excluded_output_sections setting dynindx for sections
936 that are stripped.) */
30b30c21 937
554220db
AM
938static unsigned long
939_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
940 struct bfd_link_info *info,
941 unsigned long *section_sym_count)
30b30c21
RH
942{
943 unsigned long dynsymcount = 0;
63f452a8 944 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 945
0e1862bb
L
946 if (bfd_link_pic (info)
947 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 948 {
aee6f5b4 949 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
950 asection *p;
951 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 952 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 953 && (p->flags & SEC_ALLOC) != 0
7f923b7f 954 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 955 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
956 {
957 ++dynsymcount;
958 if (do_sec)
959 elf_section_data (p)->dynindx = dynsymcount;
960 }
961 else if (do_sec)
74541ad4 962 elf_section_data (p)->dynindx = 0;
30b30c21 963 }
63f452a8
AM
964 if (do_sec)
965 *section_sym_count = dynsymcount;
30b30c21 966
6fa3860b
PB
967 elf_link_hash_traverse (elf_hash_table (info),
968 elf_link_renumber_local_hash_table_dynsyms,
969 &dynsymcount);
970
30b30c21
RH
971 if (elf_hash_table (info)->dynlocal)
972 {
973 struct elf_link_local_dynamic_entry *p;
974 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
975 p->dynindx = ++dynsymcount;
976 }
90ac2420 977 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_hash_table_dynsyms,
981 &dynsymcount);
982
d5486c43
L
983 /* There is an unused NULL entry at the head of the table which we
984 must account for in our count even if the table is empty since it
985 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
986 .dynamic section. */
987 dynsymcount++;
30b30c21 988
ccabcbe5
AM
989 elf_hash_table (info)->dynsymcount = dynsymcount;
990 return dynsymcount;
30b30c21 991}
252b5132 992
54ac0771
L
993/* Merge st_other field. */
994
995static void
996elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 997 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 998 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
999{
1000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1001
1002 /* If st_other has a processor-specific meaning, specific
cd3416da 1003 code might be needed here. */
54ac0771
L
1004 if (bed->elf_backend_merge_symbol_attribute)
1005 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1006 dynamic);
1007
cd3416da 1008 if (!dynamic)
54ac0771 1009 {
cd3416da
AM
1010 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1011 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1012
cd3416da
AM
1013 /* Keep the most constraining visibility. Leave the remainder
1014 of the st_other field to elf_backend_merge_symbol_attribute. */
1015 if (symvis - 1 < hvis - 1)
1016 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1017 }
b8417128
AM
1018 else if (definition
1019 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1020 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1021 h->protected_def = 1;
54ac0771
L
1022}
1023
4f3fedcf
AM
1024/* This function is called when we want to merge a new symbol with an
1025 existing symbol. It handles the various cases which arise when we
1026 find a definition in a dynamic object, or when there is already a
1027 definition in a dynamic object. The new symbol is described by
1028 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1029 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1030 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1031 of an old common symbol. We set OVERRIDE if the old symbol is
1032 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1033 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1034 to change. By OK to change, we mean that we shouldn't warn if the
1035 type or size does change. */
45d6a902 1036
8a56bd02 1037static bfd_boolean
268b6b39
AM
1038_bfd_elf_merge_symbol (bfd *abfd,
1039 struct bfd_link_info *info,
1040 const char *name,
1041 Elf_Internal_Sym *sym,
1042 asection **psec,
1043 bfd_vma *pvalue,
4f3fedcf
AM
1044 struct elf_link_hash_entry **sym_hash,
1045 bfd **poldbfd,
37a9e49a 1046 bfd_boolean *pold_weak,
af44c138 1047 unsigned int *pold_alignment,
268b6b39
AM
1048 bfd_boolean *skip,
1049 bfd_boolean *override,
1050 bfd_boolean *type_change_ok,
6e33951e
L
1051 bfd_boolean *size_change_ok,
1052 bfd_boolean *matched)
252b5132 1053{
7479dfd4 1054 asection *sec, *oldsec;
45d6a902 1055 struct elf_link_hash_entry *h;
90c984fc 1056 struct elf_link_hash_entry *hi;
45d6a902
AM
1057 struct elf_link_hash_entry *flip;
1058 int bind;
1059 bfd *oldbfd;
1060 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1061 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1062 const struct elf_backend_data *bed;
6e33951e 1063 char *new_version;
93f4de39 1064 bfd_boolean default_sym = *matched;
45d6a902
AM
1065
1066 *skip = FALSE;
1067 *override = FALSE;
1068
1069 sec = *psec;
1070 bind = ELF_ST_BIND (sym->st_info);
1071
1072 if (! bfd_is_und_section (sec))
1073 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1074 else
1075 h = ((struct elf_link_hash_entry *)
1076 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1077 if (h == NULL)
1078 return FALSE;
1079 *sym_hash = h;
252b5132 1080
88ba32a0
L
1081 bed = get_elf_backend_data (abfd);
1082
6e33951e 1083 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1084 if (h->versioned != unversioned)
6e33951e 1085 {
422f1182
L
1086 /* Symbol version is unknown or versioned. */
1087 new_version = strrchr (name, ELF_VER_CHR);
1088 if (new_version)
1089 {
1090 if (h->versioned == unknown)
1091 {
1092 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1093 h->versioned = versioned_hidden;
1094 else
1095 h->versioned = versioned;
1096 }
1097 new_version += 1;
1098 if (new_version[0] == '\0')
1099 new_version = NULL;
1100 }
1101 else
1102 h->versioned = unversioned;
6e33951e 1103 }
422f1182
L
1104 else
1105 new_version = NULL;
6e33951e 1106
90c984fc
L
1107 /* For merging, we only care about real symbols. But we need to make
1108 sure that indirect symbol dynamic flags are updated. */
1109 hi = h;
45d6a902
AM
1110 while (h->root.type == bfd_link_hash_indirect
1111 || h->root.type == bfd_link_hash_warning)
1112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1113
6e33951e
L
1114 if (!*matched)
1115 {
1116 if (hi == h || h->root.type == bfd_link_hash_new)
1117 *matched = TRUE;
1118 else
1119 {
ae7683d2 1120 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1121 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1122 true if the new symbol is only visible to the symbol with
6e33951e 1123 the same symbol version. */
422f1182
L
1124 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1125 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1126 if (!old_hidden && !new_hidden)
1127 /* The new symbol matches the existing symbol if both
1128 aren't hidden. */
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_VERSION is the symbol version of the existing
1133 symbol. */
422f1182
L
1134 char *old_version;
1135
1136 if (h->versioned >= versioned)
1137 old_version = strrchr (h->root.root.string,
1138 ELF_VER_CHR) + 1;
1139 else
1140 old_version = NULL;
6e33951e
L
1141
1142 /* The new symbol matches the existing symbol if they
1143 have the same symbol version. */
1144 *matched = (old_version == new_version
1145 || (old_version != NULL
1146 && new_version != NULL
1147 && strcmp (old_version, new_version) == 0));
1148 }
1149 }
1150 }
1151
934bce08
AM
1152 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1153 existing symbol. */
1154
1155 oldbfd = NULL;
1156 oldsec = NULL;
1157 switch (h->root.type)
1158 {
1159 default:
1160 break;
1161
1162 case bfd_link_hash_undefined:
1163 case bfd_link_hash_undefweak:
1164 oldbfd = h->root.u.undef.abfd;
1165 break;
1166
1167 case bfd_link_hash_defined:
1168 case bfd_link_hash_defweak:
1169 oldbfd = h->root.u.def.section->owner;
1170 oldsec = h->root.u.def.section;
1171 break;
1172
1173 case bfd_link_hash_common:
1174 oldbfd = h->root.u.c.p->section->owner;
1175 oldsec = h->root.u.c.p->section;
1176 if (pold_alignment)
1177 *pold_alignment = h->root.u.c.p->alignment_power;
1178 break;
1179 }
1180 if (poldbfd && *poldbfd == NULL)
1181 *poldbfd = oldbfd;
1182
1183 /* Differentiate strong and weak symbols. */
1184 newweak = bind == STB_WEAK;
1185 oldweak = (h->root.type == bfd_link_hash_defweak
1186 || h->root.type == bfd_link_hash_undefweak);
1187 if (pold_weak)
1188 *pold_weak = oldweak;
1189
40b36307 1190 /* We have to check it for every instance since the first few may be
ee659f1f 1191 references and not all compilers emit symbol type for undefined
40b36307
L
1192 symbols. */
1193 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1194
ee659f1f
AM
1195 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1196 respectively, is from a dynamic object. */
1197
1198 newdyn = (abfd->flags & DYNAMIC) != 0;
1199
1200 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1201 syms and defined syms in dynamic libraries respectively.
1202 ref_dynamic on the other hand can be set for a symbol defined in
1203 a dynamic library, and def_dynamic may not be set; When the
1204 definition in a dynamic lib is overridden by a definition in the
1205 executable use of the symbol in the dynamic lib becomes a
1206 reference to the executable symbol. */
1207 if (newdyn)
1208 {
1209 if (bfd_is_und_section (sec))
1210 {
1211 if (bind != STB_WEAK)
1212 {
1213 h->ref_dynamic_nonweak = 1;
1214 hi->ref_dynamic_nonweak = 1;
1215 }
1216 }
1217 else
1218 {
6e33951e
L
1219 /* Update the existing symbol only if they match. */
1220 if (*matched)
1221 h->dynamic_def = 1;
ee659f1f
AM
1222 hi->dynamic_def = 1;
1223 }
1224 }
1225
45d6a902
AM
1226 /* If we just created the symbol, mark it as being an ELF symbol.
1227 Other than that, there is nothing to do--there is no merge issue
1228 with a newly defined symbol--so we just return. */
1229
1230 if (h->root.type == bfd_link_hash_new)
252b5132 1231 {
f5385ebf 1232 h->non_elf = 0;
45d6a902
AM
1233 return TRUE;
1234 }
252b5132 1235
45d6a902
AM
1236 /* In cases involving weak versioned symbols, we may wind up trying
1237 to merge a symbol with itself. Catch that here, to avoid the
1238 confusion that results if we try to override a symbol with
1239 itself. The additional tests catch cases like
1240 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1241 dynamic object, which we do want to handle here. */
1242 if (abfd == oldbfd
895fa45f 1243 && (newweak || oldweak)
45d6a902 1244 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1245 || !h->def_regular))
45d6a902
AM
1246 return TRUE;
1247
707bba77 1248 olddyn = FALSE;
45d6a902
AM
1249 if (oldbfd != NULL)
1250 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1251 else if (oldsec != NULL)
45d6a902 1252 {
707bba77 1253 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1254 indices used by MIPS ELF. */
707bba77 1255 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1256 }
252b5132 1257
1a3b5c34
AM
1258 /* Handle a case where plugin_notice won't be called and thus won't
1259 set the non_ir_ref flags on the first pass over symbols. */
1260 if (oldbfd != NULL
1261 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1262 && newdyn != olddyn)
1263 {
1264 h->root.non_ir_ref_dynamic = TRUE;
1265 hi->root.non_ir_ref_dynamic = TRUE;
1266 }
1267
45d6a902
AM
1268 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1269 respectively, appear to be a definition rather than reference. */
1270
707bba77 1271 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1272
707bba77
AM
1273 olddef = (h->root.type != bfd_link_hash_undefined
1274 && h->root.type != bfd_link_hash_undefweak
202ac193 1275 && h->root.type != bfd_link_hash_common);
45d6a902 1276
0a36a439
L
1277 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1278 respectively, appear to be a function. */
1279
1280 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1281 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1282
1283 oldfunc = (h->type != STT_NOTYPE
1284 && bed->is_function_type (h->type));
1285
c5d37467 1286 if (!(newfunc && oldfunc)
5b677558
AM
1287 && ELF_ST_TYPE (sym->st_info) != h->type
1288 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1289 && h->type != STT_NOTYPE
c5d37467
AM
1290 && (newdef || bfd_is_com_section (sec))
1291 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1292 {
c5d37467
AM
1293 /* If creating a default indirect symbol ("foo" or "foo@") from
1294 a dynamic versioned definition ("foo@@") skip doing so if
1295 there is an existing regular definition with a different
1296 type. We don't want, for example, a "time" variable in the
1297 executable overriding a "time" function in a shared library. */
1298 if (newdyn
1299 && !olddyn)
1300 {
1301 *skip = TRUE;
1302 return TRUE;
1303 }
1304
1305 /* When adding a symbol from a regular object file after we have
1306 created indirect symbols, undo the indirection and any
1307 dynamic state. */
1308 if (hi != h
1309 && !newdyn
1310 && olddyn)
1311 {
1312 h = hi;
1313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1314 h->forced_local = 0;
1315 h->ref_dynamic = 0;
1316 h->def_dynamic = 0;
1317 h->dynamic_def = 0;
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1319 {
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328 return TRUE;
1329 }
580a2b6e
L
1330 }
1331
4c34aff8
AM
1332 /* Check TLS symbols. We don't check undefined symbols introduced
1333 by "ld -u" which have no type (and oldbfd NULL), and we don't
1334 check symbols from plugins because they also have no type. */
1335 if (oldbfd != NULL
1336 && (oldbfd->flags & BFD_PLUGIN) == 0
1337 && (abfd->flags & BFD_PLUGIN) == 0
1338 && ELF_ST_TYPE (sym->st_info) != h->type
1339 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1340 {
1341 bfd *ntbfd, *tbfd;
1342 bfd_boolean ntdef, tdef;
1343 asection *ntsec, *tsec;
1344
1345 if (h->type == STT_TLS)
1346 {
3b36f7e6 1347 ntbfd = abfd;
7479dfd4
L
1348 ntsec = sec;
1349 ntdef = newdef;
1350 tbfd = oldbfd;
1351 tsec = oldsec;
1352 tdef = olddef;
1353 }
1354 else
1355 {
1356 ntbfd = oldbfd;
1357 ntsec = oldsec;
1358 ntdef = olddef;
1359 tbfd = abfd;
1360 tsec = sec;
1361 tdef = newdef;
1362 }
1363
1364 if (tdef && ntdef)
4eca0228 1365 _bfd_error_handler
695344c0 1366 /* xgettext:c-format */
871b3ab2
AM
1367 (_("%s: TLS definition in %pB section %pA "
1368 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1369 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1370 else if (!tdef && !ntdef)
4eca0228 1371 _bfd_error_handler
695344c0 1372 /* xgettext:c-format */
871b3ab2
AM
1373 (_("%s: TLS reference in %pB "
1374 "mismatches non-TLS reference in %pB"),
c08bb8dd 1375 h->root.root.string, tbfd, ntbfd);
7479dfd4 1376 else if (tdef)
4eca0228 1377 _bfd_error_handler
695344c0 1378 /* xgettext:c-format */
871b3ab2
AM
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS reference in %pB"),
c08bb8dd 1381 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1382 else
4eca0228 1383 _bfd_error_handler
695344c0 1384 /* xgettext:c-format */
871b3ab2
AM
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1387 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1388
1389 bfd_set_error (bfd_error_bad_value);
1390 return FALSE;
1391 }
1392
45d6a902
AM
1393 /* If the old symbol has non-default visibility, we ignore the new
1394 definition from a dynamic object. */
1395 if (newdyn
9c7a29a3 1396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1397 && !bfd_is_und_section (sec))
1398 {
1399 *skip = TRUE;
1400 /* Make sure this symbol is dynamic. */
f5385ebf 1401 h->ref_dynamic = 1;
90c984fc 1402 hi->ref_dynamic = 1;
45d6a902
AM
1403 /* A protected symbol has external availability. Make sure it is
1404 recorded as dynamic.
1405
1406 FIXME: Should we check type and size for protected symbol? */
1407 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1408 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1409 else
1410 return TRUE;
1411 }
1412 else if (!newdyn
9c7a29a3 1413 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1414 && h->def_dynamic)
45d6a902
AM
1415 {
1416 /* If the new symbol with non-default visibility comes from a
1417 relocatable file and the old definition comes from a dynamic
1418 object, we remove the old definition. */
6c9b78e6 1419 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1420 {
1421 /* Handle the case where the old dynamic definition is
1422 default versioned. We need to copy the symbol info from
1423 the symbol with default version to the normal one if it
1424 was referenced before. */
1425 if (h->ref_regular)
1426 {
6c9b78e6 1427 hi->root.type = h->root.type;
d2dee3b2 1428 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1429 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1430
6c9b78e6 1431 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1432 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1433 {
aed81c4e
MR
1434 /* If the new symbol is hidden or internal, completely undo
1435 any dynamic link state. */
1436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1437 h->forced_local = 0;
1438 h->ref_dynamic = 0;
d2dee3b2
L
1439 }
1440 else
aed81c4e
MR
1441 h->ref_dynamic = 1;
1442
1443 h->def_dynamic = 0;
aed81c4e
MR
1444 /* FIXME: Should we check type and size for protected symbol? */
1445 h->size = 0;
1446 h->type = 0;
1447
6c9b78e6 1448 h = hi;
d2dee3b2
L
1449 }
1450 else
6c9b78e6 1451 h = hi;
d2dee3b2 1452 }
1de1a317 1453
f5eda473
AM
1454 /* If the old symbol was undefined before, then it will still be
1455 on the undefs list. If the new symbol is undefined or
1456 common, we can't make it bfd_link_hash_new here, because new
1457 undefined or common symbols will be added to the undefs list
1458 by _bfd_generic_link_add_one_symbol. Symbols may not be
1459 added twice to the undefs list. Also, if the new symbol is
1460 undefweak then we don't want to lose the strong undef. */
1461 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1462 {
1de1a317 1463 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1464 h->root.u.undef.abfd = abfd;
1465 }
1466 else
1467 {
1468 h->root.type = bfd_link_hash_new;
1469 h->root.u.undef.abfd = NULL;
1470 }
1471
f5eda473 1472 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1473 {
f5eda473
AM
1474 /* If the new symbol is hidden or internal, completely undo
1475 any dynamic link state. */
1476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1477 h->forced_local = 0;
1478 h->ref_dynamic = 0;
45d6a902 1479 }
f5eda473
AM
1480 else
1481 h->ref_dynamic = 1;
1482 h->def_dynamic = 0;
45d6a902
AM
1483 /* FIXME: Should we check type and size for protected symbol? */
1484 h->size = 0;
1485 h->type = 0;
1486 return TRUE;
1487 }
14a793b2 1488
15b43f48
AM
1489 /* If a new weak symbol definition comes from a regular file and the
1490 old symbol comes from a dynamic library, we treat the new one as
1491 strong. Similarly, an old weak symbol definition from a regular
1492 file is treated as strong when the new symbol comes from a dynamic
1493 library. Further, an old weak symbol from a dynamic library is
1494 treated as strong if the new symbol is from a dynamic library.
1495 This reflects the way glibc's ld.so works.
1496
165f707a
AM
1497 Also allow a weak symbol to override a linker script symbol
1498 defined by an early pass over the script. This is done so the
1499 linker knows the symbol is defined in an object file, for the
1500 DEFINED script function.
1501
15b43f48
AM
1502 Do this before setting *type_change_ok or *size_change_ok so that
1503 we warn properly when dynamic library symbols are overridden. */
1504
165f707a 1505 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1506 newweak = FALSE;
15b43f48 1507 if (olddef && newdyn)
0f8a2703
AM
1508 oldweak = FALSE;
1509
d334575b 1510 /* Allow changes between different types of function symbol. */
0a36a439 1511 if (newfunc && oldfunc)
fcb93ecf
PB
1512 *type_change_ok = TRUE;
1513
79349b09
AM
1514 /* It's OK to change the type if either the existing symbol or the
1515 new symbol is weak. A type change is also OK if the old symbol
1516 is undefined and the new symbol is defined. */
252b5132 1517
79349b09
AM
1518 if (oldweak
1519 || newweak
1520 || (newdef
1521 && h->root.type == bfd_link_hash_undefined))
1522 *type_change_ok = TRUE;
1523
1524 /* It's OK to change the size if either the existing symbol or the
1525 new symbol is weak, or if the old symbol is undefined. */
1526
1527 if (*type_change_ok
1528 || h->root.type == bfd_link_hash_undefined)
1529 *size_change_ok = TRUE;
45d6a902 1530
45d6a902
AM
1531 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1532 symbol, respectively, appears to be a common symbol in a dynamic
1533 object. If a symbol appears in an uninitialized section, and is
1534 not weak, and is not a function, then it may be a common symbol
1535 which was resolved when the dynamic object was created. We want
1536 to treat such symbols specially, because they raise special
1537 considerations when setting the symbol size: if the symbol
1538 appears as a common symbol in a regular object, and the size in
1539 the regular object is larger, we must make sure that we use the
1540 larger size. This problematic case can always be avoided in C,
1541 but it must be handled correctly when using Fortran shared
1542 libraries.
1543
1544 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1545 likewise for OLDDYNCOMMON and OLDDEF.
1546
1547 Note that this test is just a heuristic, and that it is quite
1548 possible to have an uninitialized symbol in a shared object which
1549 is really a definition, rather than a common symbol. This could
1550 lead to some minor confusion when the symbol really is a common
1551 symbol in some regular object. However, I think it will be
1552 harmless. */
1553
1554 if (newdyn
1555 && newdef
79349b09 1556 && !newweak
45d6a902
AM
1557 && (sec->flags & SEC_ALLOC) != 0
1558 && (sec->flags & SEC_LOAD) == 0
1559 && sym->st_size > 0
0a36a439 1560 && !newfunc)
45d6a902
AM
1561 newdyncommon = TRUE;
1562 else
1563 newdyncommon = FALSE;
1564
1565 if (olddyn
1566 && olddef
1567 && h->root.type == bfd_link_hash_defined
f5385ebf 1568 && h->def_dynamic
45d6a902
AM
1569 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1570 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1571 && h->size > 0
0a36a439 1572 && !oldfunc)
45d6a902
AM
1573 olddyncommon = TRUE;
1574 else
1575 olddyncommon = FALSE;
1576
a4d8e49b
L
1577 /* We now know everything about the old and new symbols. We ask the
1578 backend to check if we can merge them. */
5d13b3b3
AM
1579 if (bed->merge_symbol != NULL)
1580 {
1581 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1582 return FALSE;
1583 sec = *psec;
1584 }
a4d8e49b 1585
a83ef4d1
L
1586 /* There are multiple definitions of a normal symbol. Skip the
1587 default symbol as well as definition from an IR object. */
93f4de39 1588 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1589 && !default_sym && h->def_regular
1590 && !(oldbfd != NULL
1591 && (oldbfd->flags & BFD_PLUGIN) != 0
1592 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1593 {
1594 /* Handle a multiple definition. */
1595 (*info->callbacks->multiple_definition) (info, &h->root,
1596 abfd, sec, *pvalue);
1597 *skip = TRUE;
1598 return TRUE;
1599 }
1600
45d6a902
AM
1601 /* If both the old and the new symbols look like common symbols in a
1602 dynamic object, set the size of the symbol to the larger of the
1603 two. */
1604
1605 if (olddyncommon
1606 && newdyncommon
1607 && sym->st_size != h->size)
1608 {
1609 /* Since we think we have two common symbols, issue a multiple
1610 common warning if desired. Note that we only warn if the
1611 size is different. If the size is the same, we simply let
1612 the old symbol override the new one as normally happens with
1613 symbols defined in dynamic objects. */
1614
1a72702b
AM
1615 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1616 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1617 if (sym->st_size > h->size)
1618 h->size = sym->st_size;
252b5132 1619
45d6a902 1620 *size_change_ok = TRUE;
252b5132
RH
1621 }
1622
45d6a902
AM
1623 /* If we are looking at a dynamic object, and we have found a
1624 definition, we need to see if the symbol was already defined by
1625 some other object. If so, we want to use the existing
1626 definition, and we do not want to report a multiple symbol
1627 definition error; we do this by clobbering *PSEC to be
1628 bfd_und_section_ptr.
1629
1630 We treat a common symbol as a definition if the symbol in the
1631 shared library is a function, since common symbols always
1632 represent variables; this can cause confusion in principle, but
1633 any such confusion would seem to indicate an erroneous program or
1634 shared library. We also permit a common symbol in a regular
8170f769 1635 object to override a weak symbol in a shared object. */
45d6a902
AM
1636
1637 if (newdyn
1638 && newdef
77cfaee6 1639 && (olddef
45d6a902 1640 || (h->root.type == bfd_link_hash_common
8170f769 1641 && (newweak || newfunc))))
45d6a902
AM
1642 {
1643 *override = TRUE;
1644 newdef = FALSE;
1645 newdyncommon = FALSE;
252b5132 1646
45d6a902
AM
1647 *psec = sec = bfd_und_section_ptr;
1648 *size_change_ok = TRUE;
252b5132 1649
45d6a902
AM
1650 /* If we get here when the old symbol is a common symbol, then
1651 we are explicitly letting it override a weak symbol or
1652 function in a dynamic object, and we don't want to warn about
1653 a type change. If the old symbol is a defined symbol, a type
1654 change warning may still be appropriate. */
252b5132 1655
45d6a902
AM
1656 if (h->root.type == bfd_link_hash_common)
1657 *type_change_ok = TRUE;
1658 }
1659
1660 /* Handle the special case of an old common symbol merging with a
1661 new symbol which looks like a common symbol in a shared object.
1662 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1663 common symbol, and let _bfd_generic_link_add_one_symbol do the
1664 right thing. */
45d6a902
AM
1665
1666 if (newdyncommon
1667 && h->root.type == bfd_link_hash_common)
1668 {
1669 *override = TRUE;
1670 newdef = FALSE;
1671 newdyncommon = FALSE;
1672 *pvalue = sym->st_size;
a4d8e49b 1673 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1674 *size_change_ok = TRUE;
1675 }
1676
c5e2cead 1677 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1678 if (newdef && olddef && newweak)
54ac0771 1679 {
35ed3f94 1680 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1681 if (!(oldbfd != NULL
1682 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1683 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1684 {
1685 newdef = FALSE;
1686 *skip = TRUE;
1687 }
54ac0771
L
1688
1689 /* Merge st_other. If the symbol already has a dynamic index,
1690 but visibility says it should not be visible, turn it into a
1691 local symbol. */
b8417128 1692 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1693 if (h->dynindx != -1)
1694 switch (ELF_ST_VISIBILITY (h->other))
1695 {
1696 case STV_INTERNAL:
1697 case STV_HIDDEN:
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1699 break;
1700 }
1701 }
c5e2cead 1702
45d6a902
AM
1703 /* If the old symbol is from a dynamic object, and the new symbol is
1704 a definition which is not from a dynamic object, then the new
1705 symbol overrides the old symbol. Symbols from regular files
1706 always take precedence over symbols from dynamic objects, even if
1707 they are defined after the dynamic object in the link.
1708
1709 As above, we again permit a common symbol in a regular object to
1710 override a definition in a shared object if the shared object
0f8a2703 1711 symbol is a function or is weak. */
45d6a902
AM
1712
1713 flip = NULL;
77cfaee6 1714 if (!newdyn
45d6a902
AM
1715 && (newdef
1716 || (bfd_is_com_section (sec)
0a36a439 1717 && (oldweak || oldfunc)))
45d6a902
AM
1718 && olddyn
1719 && olddef
f5385ebf 1720 && h->def_dynamic)
45d6a902
AM
1721 {
1722 /* Change the hash table entry to undefined, and let
1723 _bfd_generic_link_add_one_symbol do the right thing with the
1724 new definition. */
1725
1726 h->root.type = bfd_link_hash_undefined;
1727 h->root.u.undef.abfd = h->root.u.def.section->owner;
1728 *size_change_ok = TRUE;
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 /* We again permit a type change when a common symbol may be
1734 overriding a function. */
1735
1736 if (bfd_is_com_section (sec))
0a36a439
L
1737 {
1738 if (oldfunc)
1739 {
1740 /* If a common symbol overrides a function, make sure
1741 that it isn't defined dynamically nor has type
1742 function. */
1743 h->def_dynamic = 0;
1744 h->type = STT_NOTYPE;
1745 }
1746 *type_change_ok = TRUE;
1747 }
45d6a902 1748
6c9b78e6
AM
1749 if (hi->root.type == bfd_link_hash_indirect)
1750 flip = hi;
45d6a902
AM
1751 else
1752 /* This union may have been set to be non-NULL when this symbol
1753 was seen in a dynamic object. We must force the union to be
1754 NULL, so that it is correct for a regular symbol. */
1755 h->verinfo.vertree = NULL;
1756 }
1757
1758 /* Handle the special case of a new common symbol merging with an
1759 old symbol that looks like it might be a common symbol defined in
1760 a shared object. Note that we have already handled the case in
1761 which a new common symbol should simply override the definition
1762 in the shared library. */
1763
1764 if (! newdyn
1765 && bfd_is_com_section (sec)
1766 && olddyncommon)
1767 {
1768 /* It would be best if we could set the hash table entry to a
1769 common symbol, but we don't know what to use for the section
1770 or the alignment. */
1a72702b
AM
1771 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1772 bfd_link_hash_common, sym->st_size);
45d6a902 1773
4cc11e76 1774 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1775 larger, pretend that the new symbol has its size. */
1776
1777 if (h->size > *pvalue)
1778 *pvalue = h->size;
1779
af44c138
L
1780 /* We need to remember the alignment required by the symbol
1781 in the dynamic object. */
1782 BFD_ASSERT (pold_alignment);
1783 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1784
1785 olddef = FALSE;
1786 olddyncommon = FALSE;
1787
1788 h->root.type = bfd_link_hash_undefined;
1789 h->root.u.undef.abfd = h->root.u.def.section->owner;
1790
1791 *size_change_ok = TRUE;
1792 *type_change_ok = TRUE;
1793
6c9b78e6
AM
1794 if (hi->root.type == bfd_link_hash_indirect)
1795 flip = hi;
45d6a902
AM
1796 else
1797 h->verinfo.vertree = NULL;
1798 }
1799
1800 if (flip != NULL)
1801 {
1802 /* Handle the case where we had a versioned symbol in a dynamic
1803 library and now find a definition in a normal object. In this
1804 case, we make the versioned symbol point to the normal one. */
45d6a902 1805 flip->root.type = h->root.type;
00cbee0a 1806 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1807 h->root.type = bfd_link_hash_indirect;
1808 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1809 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1810 if (h->def_dynamic)
45d6a902 1811 {
f5385ebf
AM
1812 h->def_dynamic = 0;
1813 flip->ref_dynamic = 1;
45d6a902
AM
1814 }
1815 }
1816
45d6a902
AM
1817 return TRUE;
1818}
1819
1820/* This function is called to create an indirect symbol from the
1821 default for the symbol with the default version if needed. The
4f3fedcf 1822 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1823 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1824
28caa186 1825static bfd_boolean
268b6b39
AM
1826_bfd_elf_add_default_symbol (bfd *abfd,
1827 struct bfd_link_info *info,
1828 struct elf_link_hash_entry *h,
1829 const char *name,
1830 Elf_Internal_Sym *sym,
4f3fedcf
AM
1831 asection *sec,
1832 bfd_vma value,
1833 bfd **poldbfd,
e3c9d234 1834 bfd_boolean *dynsym)
45d6a902
AM
1835{
1836 bfd_boolean type_change_ok;
1837 bfd_boolean size_change_ok;
1838 bfd_boolean skip;
1839 char *shortname;
1840 struct elf_link_hash_entry *hi;
1841 struct bfd_link_hash_entry *bh;
9c5bfbb7 1842 const struct elf_backend_data *bed;
45d6a902
AM
1843 bfd_boolean collect;
1844 bfd_boolean dynamic;
e3c9d234 1845 bfd_boolean override;
45d6a902
AM
1846 char *p;
1847 size_t len, shortlen;
ffd65175 1848 asection *tmp_sec;
6e33951e 1849 bfd_boolean matched;
45d6a902 1850
422f1182
L
1851 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1852 return TRUE;
1853
45d6a902
AM
1854 /* If this symbol has a version, and it is the default version, we
1855 create an indirect symbol from the default name to the fully
1856 decorated name. This will cause external references which do not
1857 specify a version to be bound to this version of the symbol. */
1858 p = strchr (name, ELF_VER_CHR);
422f1182
L
1859 if (h->versioned == unknown)
1860 {
1861 if (p == NULL)
1862 {
1863 h->versioned = unversioned;
1864 return TRUE;
1865 }
1866 else
1867 {
1868 if (p[1] != ELF_VER_CHR)
1869 {
1870 h->versioned = versioned_hidden;
1871 return TRUE;
1872 }
1873 else
1874 h->versioned = versioned;
1875 }
1876 }
4373f8af
L
1877 else
1878 {
1879 /* PR ld/19073: We may see an unversioned definition after the
1880 default version. */
1881 if (p == NULL)
1882 return TRUE;
1883 }
45d6a902 1884
45d6a902
AM
1885 bed = get_elf_backend_data (abfd);
1886 collect = bed->collect;
1887 dynamic = (abfd->flags & DYNAMIC) != 0;
1888
1889 shortlen = p - name;
a50b1753 1890 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1891 if (shortname == NULL)
1892 return FALSE;
1893 memcpy (shortname, name, shortlen);
1894 shortname[shortlen] = '\0';
1895
1896 /* We are going to create a new symbol. Merge it with any existing
1897 symbol with this name. For the purposes of the merge, act as
1898 though we were defining the symbol we just defined, although we
1899 actually going to define an indirect symbol. */
1900 type_change_ok = FALSE;
1901 size_change_ok = FALSE;
6e33951e 1902 matched = TRUE;
ffd65175
AM
1903 tmp_sec = sec;
1904 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1905 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1906 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1907 return FALSE;
1908
1909 if (skip)
1910 goto nondefault;
1911
5b677558
AM
1912 if (hi->def_regular)
1913 {
1914 /* If the undecorated symbol will have a version added by a
1915 script different to H, then don't indirect to/from the
1916 undecorated symbol. This isn't ideal because we may not yet
1917 have seen symbol versions, if given by a script on the
1918 command line rather than via --version-script. */
1919 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1920 {
1921 bfd_boolean hide;
1922
1923 hi->verinfo.vertree
1924 = bfd_find_version_for_sym (info->version_info,
1925 hi->root.root.string, &hide);
1926 if (hi->verinfo.vertree != NULL && hide)
1927 {
1928 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1929 goto nondefault;
1930 }
1931 }
1932 if (hi->verinfo.vertree != NULL
1933 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 goto nondefault;
1935 }
1936
45d6a902
AM
1937 if (! override)
1938 {
c6e8a9a8 1939 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1940 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1941 {
1942 bh = &hi->root;
fbcc8baf 1943 if (bh->type == bfd_link_hash_defined
6cc71b82 1944 && bh->u.def.section->owner != NULL
fbcc8baf
L
1945 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1946 {
1947 /* Mark the previous definition from IR object as
1948 undefined so that the generic linker will override
1949 it. */
1950 bh->type = bfd_link_hash_undefined;
1951 bh->u.undef.abfd = bh->u.def.section->owner;
1952 }
c6e8a9a8
L
1953 if (! (_bfd_generic_link_add_one_symbol
1954 (info, abfd, shortname, BSF_INDIRECT,
1955 bfd_ind_section_ptr,
1956 0, name, FALSE, collect, &bh)))
1957 return FALSE;
1958 hi = (struct elf_link_hash_entry *) bh;
1959 }
45d6a902
AM
1960 }
1961 else
1962 {
1963 /* In this case the symbol named SHORTNAME is overriding the
1964 indirect symbol we want to add. We were planning on making
1965 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1966 is the name without a version. NAME is the fully versioned
1967 name, and it is the default version.
1968
1969 Overriding means that we already saw a definition for the
1970 symbol SHORTNAME in a regular object, and it is overriding
1971 the symbol defined in the dynamic object.
1972
1973 When this happens, we actually want to change NAME, the
1974 symbol we just added, to refer to SHORTNAME. This will cause
1975 references to NAME in the shared object to become references
1976 to SHORTNAME in the regular object. This is what we expect
1977 when we override a function in a shared object: that the
1978 references in the shared object will be mapped to the
1979 definition in the regular object. */
1980
1981 while (hi->root.type == bfd_link_hash_indirect
1982 || hi->root.type == bfd_link_hash_warning)
1983 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1984
1985 h->root.type = bfd_link_hash_indirect;
1986 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1987 if (h->def_dynamic)
45d6a902 1988 {
f5385ebf
AM
1989 h->def_dynamic = 0;
1990 hi->ref_dynamic = 1;
1991 if (hi->ref_regular
1992 || hi->def_regular)
45d6a902 1993 {
c152c796 1994 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1995 return FALSE;
1996 }
1997 }
1998
1999 /* Now set HI to H, so that the following code will set the
2000 other fields correctly. */
2001 hi = h;
2002 }
2003
fab4a87f
L
2004 /* Check if HI is a warning symbol. */
2005 if (hi->root.type == bfd_link_hash_warning)
2006 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2007
45d6a902
AM
2008 /* If there is a duplicate definition somewhere, then HI may not
2009 point to an indirect symbol. We will have reported an error to
2010 the user in that case. */
2011
2012 if (hi->root.type == bfd_link_hash_indirect)
2013 {
2014 struct elf_link_hash_entry *ht;
2015
45d6a902 2016 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2017 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2018
68c88cd4
AM
2019 /* A reference to the SHORTNAME symbol from a dynamic library
2020 will be satisfied by the versioned symbol at runtime. In
2021 effect, we have a reference to the versioned symbol. */
2022 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2023 hi->dynamic_def |= ht->dynamic_def;
2024
45d6a902
AM
2025 /* See if the new flags lead us to realize that the symbol must
2026 be dynamic. */
2027 if (! *dynsym)
2028 {
2029 if (! dynamic)
2030 {
0e1862bb 2031 if (! bfd_link_executable (info)
90c984fc 2032 || hi->def_dynamic
f5385ebf 2033 || hi->ref_dynamic)
45d6a902
AM
2034 *dynsym = TRUE;
2035 }
2036 else
2037 {
f5385ebf 2038 if (hi->ref_regular)
45d6a902
AM
2039 *dynsym = TRUE;
2040 }
2041 }
2042 }
2043
2044 /* We also need to define an indirection from the nondefault version
2045 of the symbol. */
2046
2047nondefault:
2048 len = strlen (name);
a50b1753 2049 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2050 if (shortname == NULL)
2051 return FALSE;
2052 memcpy (shortname, name, shortlen);
2053 memcpy (shortname + shortlen, p + 1, len - shortlen);
2054
2055 /* Once again, merge with any existing symbol. */
2056 type_change_ok = FALSE;
2057 size_change_ok = FALSE;
ffd65175
AM
2058 tmp_sec = sec;
2059 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2060 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2061 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2062 return FALSE;
2063
2064 if (skip)
2065 return TRUE;
2066
2067 if (override)
2068 {
2069 /* Here SHORTNAME is a versioned name, so we don't expect to see
2070 the type of override we do in the case above unless it is
4cc11e76 2071 overridden by a versioned definition. */
45d6a902
AM
2072 if (hi->root.type != bfd_link_hash_defined
2073 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2074 _bfd_error_handler
695344c0 2075 /* xgettext:c-format */
871b3ab2 2076 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2077 abfd, shortname);
45d6a902
AM
2078 }
2079 else
2080 {
2081 bh = &hi->root;
2082 if (! (_bfd_generic_link_add_one_symbol
2083 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2084 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2085 return FALSE;
2086 hi = (struct elf_link_hash_entry *) bh;
2087
2088 /* If there is a duplicate definition somewhere, then HI may not
2089 point to an indirect symbol. We will have reported an error
2090 to the user in that case. */
2091
2092 if (hi->root.type == bfd_link_hash_indirect)
2093 {
fcfa13d2 2094 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2095 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2096 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2097
2098 /* See if the new flags lead us to realize that the symbol
2099 must be dynamic. */
2100 if (! *dynsym)
2101 {
2102 if (! dynamic)
2103 {
0e1862bb 2104 if (! bfd_link_executable (info)
f5385ebf 2105 || hi->ref_dynamic)
45d6a902
AM
2106 *dynsym = TRUE;
2107 }
2108 else
2109 {
f5385ebf 2110 if (hi->ref_regular)
45d6a902
AM
2111 *dynsym = TRUE;
2112 }
2113 }
2114 }
2115 }
2116
2117 return TRUE;
2118}
2119\f
2120/* This routine is used to export all defined symbols into the dynamic
2121 symbol table. It is called via elf_link_hash_traverse. */
2122
28caa186 2123static bfd_boolean
268b6b39 2124_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2125{
a50b1753 2126 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2127
2128 /* Ignore indirect symbols. These are added by the versioning code. */
2129 if (h->root.type == bfd_link_hash_indirect)
2130 return TRUE;
2131
7686d77d
AM
2132 /* Ignore this if we won't export it. */
2133 if (!eif->info->export_dynamic && !h->dynamic)
2134 return TRUE;
45d6a902
AM
2135
2136 if (h->dynindx == -1
fd91d419
L
2137 && (h->def_regular || h->ref_regular)
2138 && ! bfd_hide_sym_by_version (eif->info->version_info,
2139 h->root.root.string))
45d6a902 2140 {
fd91d419 2141 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2142 {
fd91d419
L
2143 eif->failed = TRUE;
2144 return FALSE;
45d6a902
AM
2145 }
2146 }
2147
2148 return TRUE;
2149}
2150\f
2151/* Look through the symbols which are defined in other shared
2152 libraries and referenced here. Update the list of version
2153 dependencies. This will be put into the .gnu.version_r section.
2154 This function is called via elf_link_hash_traverse. */
2155
28caa186 2156static bfd_boolean
268b6b39
AM
2157_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2158 void *data)
45d6a902 2159{
a50b1753 2160 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2161 Elf_Internal_Verneed *t;
2162 Elf_Internal_Vernaux *a;
2163 bfd_size_type amt;
2164
45d6a902
AM
2165 /* We only care about symbols defined in shared objects with version
2166 information. */
f5385ebf
AM
2167 if (!h->def_dynamic
2168 || h->def_regular
45d6a902 2169 || h->dynindx == -1
7b20f099
AM
2170 || h->verinfo.verdef == NULL
2171 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2172 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2173 return TRUE;
2174
2175 /* See if we already know about this version. */
28caa186
AM
2176 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2177 t != NULL;
2178 t = t->vn_nextref)
45d6a902
AM
2179 {
2180 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2181 continue;
2182
2183 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2184 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2185 return TRUE;
2186
2187 break;
2188 }
2189
2190 /* This is a new version. Add it to tree we are building. */
2191
2192 if (t == NULL)
2193 {
2194 amt = sizeof *t;
a50b1753 2195 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2196 if (t == NULL)
2197 {
2198 rinfo->failed = TRUE;
2199 return FALSE;
2200 }
2201
2202 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2203 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2204 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2205 }
2206
2207 amt = sizeof *a;
a50b1753 2208 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2209 if (a == NULL)
2210 {
2211 rinfo->failed = TRUE;
2212 return FALSE;
2213 }
45d6a902
AM
2214
2215 /* Note that we are copying a string pointer here, and testing it
2216 above. If bfd_elf_string_from_elf_section is ever changed to
2217 discard the string data when low in memory, this will have to be
2218 fixed. */
2219 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2220
2221 a->vna_flags = h->verinfo.verdef->vd_flags;
2222 a->vna_nextptr = t->vn_auxptr;
2223
2224 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2225 ++rinfo->vers;
2226
2227 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2228
2229 t->vn_auxptr = a;
2230
2231 return TRUE;
2232}
2233
099bb8fb
L
2234/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2235 hidden. Set *T_P to NULL if there is no match. */
2236
2237static bfd_boolean
2238_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2239 struct elf_link_hash_entry *h,
2240 const char *version_p,
2241 struct bfd_elf_version_tree **t_p,
2242 bfd_boolean *hide)
2243{
2244 struct bfd_elf_version_tree *t;
2245
2246 /* Look for the version. If we find it, it is no longer weak. */
2247 for (t = info->version_info; t != NULL; t = t->next)
2248 {
2249 if (strcmp (t->name, version_p) == 0)
2250 {
2251 size_t len;
2252 char *alc;
2253 struct bfd_elf_version_expr *d;
2254
2255 len = version_p - h->root.root.string;
2256 alc = (char *) bfd_malloc (len);
2257 if (alc == NULL)
2258 return FALSE;
2259 memcpy (alc, h->root.root.string, len - 1);
2260 alc[len - 1] = '\0';
2261 if (alc[len - 2] == ELF_VER_CHR)
2262 alc[len - 2] = '\0';
2263
2264 h->verinfo.vertree = t;
2265 t->used = TRUE;
2266 d = NULL;
2267
2268 if (t->globals.list != NULL)
2269 d = (*t->match) (&t->globals, NULL, alc);
2270
2271 /* See if there is anything to force this symbol to
2272 local scope. */
2273 if (d == NULL && t->locals.list != NULL)
2274 {
2275 d = (*t->match) (&t->locals, NULL, alc);
2276 if (d != NULL
2277 && h->dynindx != -1
2278 && ! info->export_dynamic)
2279 *hide = TRUE;
2280 }
2281
2282 free (alc);
2283 break;
2284 }
2285 }
2286
2287 *t_p = t;
2288
2289 return TRUE;
2290}
2291
2292/* Return TRUE if the symbol H is hidden by version script. */
2293
2294bfd_boolean
2295_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h)
2297{
2298 const char *p;
2299 bfd_boolean hide = FALSE;
2300 const struct elf_backend_data *bed
2301 = get_elf_backend_data (info->output_bfd);
2302
2303 /* Version script only hides symbols defined in regular objects. */
2304 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2305 return TRUE;
2306
2307 p = strchr (h->root.root.string, ELF_VER_CHR);
2308 if (p != NULL && h->verinfo.vertree == NULL)
2309 {
2310 struct bfd_elf_version_tree *t;
2311
2312 ++p;
2313 if (*p == ELF_VER_CHR)
2314 ++p;
2315
2316 if (*p != '\0'
2317 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2318 && hide)
2319 {
2320 if (hide)
2321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2322 return TRUE;
2323 }
2324 }
2325
2326 /* If we don't have a version for this symbol, see if we can find
2327 something. */
2328 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2329 {
2330 h->verinfo.vertree
2331 = bfd_find_version_for_sym (info->version_info,
2332 h->root.root.string, &hide);
2333 if (h->verinfo.vertree != NULL && hide)
2334 {
2335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2336 return TRUE;
2337 }
2338 }
2339
2340 return FALSE;
2341}
2342
45d6a902
AM
2343/* Figure out appropriate versions for all the symbols. We may not
2344 have the version number script until we have read all of the input
2345 files, so until that point we don't know which symbols should be
2346 local. This function is called via elf_link_hash_traverse. */
2347
28caa186 2348static bfd_boolean
268b6b39 2349_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2350{
28caa186 2351 struct elf_info_failed *sinfo;
45d6a902 2352 struct bfd_link_info *info;
9c5bfbb7 2353 const struct elf_backend_data *bed;
45d6a902
AM
2354 struct elf_info_failed eif;
2355 char *p;
099bb8fb 2356 bfd_boolean hide;
45d6a902 2357
a50b1753 2358 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2359 info = sinfo->info;
2360
45d6a902
AM
2361 /* Fix the symbol flags. */
2362 eif.failed = FALSE;
2363 eif.info = info;
2364 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2365 {
2366 if (eif.failed)
2367 sinfo->failed = TRUE;
2368 return FALSE;
2369 }
2370
0a640d71
L
2371 bed = get_elf_backend_data (info->output_bfd);
2372
45d6a902
AM
2373 /* We only need version numbers for symbols defined in regular
2374 objects. */
f5385ebf 2375 if (!h->def_regular)
0a640d71
L
2376 {
2377 /* Hide symbols defined in discarded input sections. */
2378 if ((h->root.type == bfd_link_hash_defined
2379 || h->root.type == bfd_link_hash_defweak)
2380 && discarded_section (h->root.u.def.section))
2381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2382 return TRUE;
2383 }
45d6a902 2384
099bb8fb 2385 hide = FALSE;
45d6a902
AM
2386 p = strchr (h->root.root.string, ELF_VER_CHR);
2387 if (p != NULL && h->verinfo.vertree == NULL)
2388 {
2389 struct bfd_elf_version_tree *t;
45d6a902 2390
45d6a902
AM
2391 ++p;
2392 if (*p == ELF_VER_CHR)
6e33951e 2393 ++p;
45d6a902
AM
2394
2395 /* If there is no version string, we can just return out. */
2396 if (*p == '\0')
6e33951e 2397 return TRUE;
45d6a902 2398
099bb8fb 2399 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2400 {
099bb8fb
L
2401 sinfo->failed = TRUE;
2402 return FALSE;
45d6a902
AM
2403 }
2404
099bb8fb
L
2405 if (hide)
2406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2407
45d6a902
AM
2408 /* If we are building an application, we need to create a
2409 version node for this version. */
0e1862bb 2410 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2411 {
2412 struct bfd_elf_version_tree **pp;
2413 int version_index;
2414
2415 /* If we aren't going to export this symbol, we don't need
2416 to worry about it. */
2417 if (h->dynindx == -1)
2418 return TRUE;
2419
ef53be89
AM
2420 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2421 sizeof *t);
45d6a902
AM
2422 if (t == NULL)
2423 {
2424 sinfo->failed = TRUE;
2425 return FALSE;
2426 }
2427
45d6a902 2428 t->name = p;
45d6a902
AM
2429 t->name_indx = (unsigned int) -1;
2430 t->used = TRUE;
2431
2432 version_index = 1;
2433 /* Don't count anonymous version tag. */
fd91d419
L
2434 if (sinfo->info->version_info != NULL
2435 && sinfo->info->version_info->vernum == 0)
45d6a902 2436 version_index = 0;
fd91d419
L
2437 for (pp = &sinfo->info->version_info;
2438 *pp != NULL;
2439 pp = &(*pp)->next)
45d6a902
AM
2440 ++version_index;
2441 t->vernum = version_index;
2442
2443 *pp = t;
2444
2445 h->verinfo.vertree = t;
2446 }
2447 else if (t == NULL)
2448 {
2449 /* We could not find the version for a symbol when
2450 generating a shared archive. Return an error. */
4eca0228 2451 _bfd_error_handler
695344c0 2452 /* xgettext:c-format */
871b3ab2 2453 (_("%pB: version node not found for symbol %s"),
28caa186 2454 info->output_bfd, h->root.root.string);
45d6a902
AM
2455 bfd_set_error (bfd_error_bad_value);
2456 sinfo->failed = TRUE;
2457 return FALSE;
2458 }
45d6a902
AM
2459 }
2460
2461 /* If we don't have a version for this symbol, see if we can find
2462 something. */
099bb8fb
L
2463 if (!hide
2464 && h->verinfo.vertree == NULL
2465 && sinfo->info->version_info != NULL)
45d6a902 2466 {
fd91d419
L
2467 h->verinfo.vertree
2468 = bfd_find_version_for_sym (sinfo->info->version_info,
2469 h->root.root.string, &hide);
1e8fa21e
AM
2470 if (h->verinfo.vertree != NULL && hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2472 }
2473
2474 return TRUE;
2475}
2476\f
45d6a902
AM
2477/* Read and swap the relocs from the section indicated by SHDR. This
2478 may be either a REL or a RELA section. The relocations are
2479 translated into RELA relocations and stored in INTERNAL_RELOCS,
2480 which should have already been allocated to contain enough space.
2481 The EXTERNAL_RELOCS are a buffer where the external form of the
2482 relocations should be stored.
2483
2484 Returns FALSE if something goes wrong. */
2485
2486static bfd_boolean
268b6b39 2487elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2488 asection *sec,
268b6b39
AM
2489 Elf_Internal_Shdr *shdr,
2490 void *external_relocs,
2491 Elf_Internal_Rela *internal_relocs)
45d6a902 2492{
9c5bfbb7 2493 const struct elf_backend_data *bed;
268b6b39 2494 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2495 const bfd_byte *erela;
2496 const bfd_byte *erelaend;
2497 Elf_Internal_Rela *irela;
243ef1e0
L
2498 Elf_Internal_Shdr *symtab_hdr;
2499 size_t nsyms;
45d6a902 2500
45d6a902
AM
2501 /* Position ourselves at the start of the section. */
2502 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2503 return FALSE;
2504
2505 /* Read the relocations. */
2506 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2507 return FALSE;
2508
243ef1e0 2509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2510 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2511
45d6a902
AM
2512 bed = get_elf_backend_data (abfd);
2513
2514 /* Convert the external relocations to the internal format. */
2515 if (shdr->sh_entsize == bed->s->sizeof_rel)
2516 swap_in = bed->s->swap_reloc_in;
2517 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2518 swap_in = bed->s->swap_reloca_in;
2519 else
2520 {
2521 bfd_set_error (bfd_error_wrong_format);
2522 return FALSE;
2523 }
2524
a50b1753 2525 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2526 /* Setting erelaend like this and comparing with <= handles case of
2527 a fuzzed object with sh_size not a multiple of sh_entsize. */
2528 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2529 irela = internal_relocs;
f55b1e32 2530 while (erela <= erelaend)
45d6a902 2531 {
243ef1e0
L
2532 bfd_vma r_symndx;
2533
45d6a902 2534 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2535 r_symndx = ELF32_R_SYM (irela->r_info);
2536 if (bed->s->arch_size == 64)
2537 r_symndx >>= 24;
ce98a316
NC
2538 if (nsyms > 0)
2539 {
2540 if ((size_t) r_symndx >= nsyms)
2541 {
4eca0228 2542 _bfd_error_handler
695344c0 2543 /* xgettext:c-format */
2dcf00ce
AM
2544 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2545 " for offset %#" PRIx64 " in section `%pA'"),
2546 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2547 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2548 bfd_set_error (bfd_error_bad_value);
2549 return FALSE;
2550 }
2551 }
cf35638d 2552 else if (r_symndx != STN_UNDEF)
243ef1e0 2553 {
4eca0228 2554 _bfd_error_handler
695344c0 2555 /* xgettext:c-format */
2dcf00ce
AM
2556 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2557 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2558 " when the object file has no symbol table"),
2dcf00ce
AM
2559 abfd, (uint64_t) r_symndx,
2560 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2561 bfd_set_error (bfd_error_bad_value);
2562 return FALSE;
2563 }
45d6a902
AM
2564 irela += bed->s->int_rels_per_ext_rel;
2565 erela += shdr->sh_entsize;
2566 }
2567
2568 return TRUE;
2569}
2570
2571/* Read and swap the relocs for a section O. They may have been
2572 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2573 not NULL, they are used as buffers to read into. They are known to
2574 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2575 the return value is allocated using either malloc or bfd_alloc,
2576 according to the KEEP_MEMORY argument. If O has two relocation
2577 sections (both REL and RELA relocations), then the REL_HDR
2578 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2579 RELA_HDR relocations. */
45d6a902
AM
2580
2581Elf_Internal_Rela *
268b6b39
AM
2582_bfd_elf_link_read_relocs (bfd *abfd,
2583 asection *o,
2584 void *external_relocs,
2585 Elf_Internal_Rela *internal_relocs,
2586 bfd_boolean keep_memory)
45d6a902 2587{
268b6b39 2588 void *alloc1 = NULL;
45d6a902 2589 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2591 struct bfd_elf_section_data *esdo = elf_section_data (o);
2592 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2593
d4730f92
BS
2594 if (esdo->relocs != NULL)
2595 return esdo->relocs;
45d6a902
AM
2596
2597 if (o->reloc_count == 0)
2598 return NULL;
2599
45d6a902
AM
2600 if (internal_relocs == NULL)
2601 {
2602 bfd_size_type size;
2603
056bafd4 2604 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2605 if (keep_memory)
a50b1753 2606 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2607 else
a50b1753 2608 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2609 if (internal_relocs == NULL)
2610 goto error_return;
2611 }
2612
2613 if (external_relocs == NULL)
2614 {
d4730f92
BS
2615 bfd_size_type size = 0;
2616
2617 if (esdo->rel.hdr)
2618 size += esdo->rel.hdr->sh_size;
2619 if (esdo->rela.hdr)
2620 size += esdo->rela.hdr->sh_size;
45d6a902 2621
268b6b39 2622 alloc1 = bfd_malloc (size);
45d6a902
AM
2623 if (alloc1 == NULL)
2624 goto error_return;
2625 external_relocs = alloc1;
2626 }
2627
d4730f92
BS
2628 internal_rela_relocs = internal_relocs;
2629 if (esdo->rel.hdr)
2630 {
2631 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2632 external_relocs,
2633 internal_relocs))
2634 goto error_return;
2635 external_relocs = (((bfd_byte *) external_relocs)
2636 + esdo->rel.hdr->sh_size);
2637 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2638 * bed->s->int_rels_per_ext_rel);
2639 }
2640
2641 if (esdo->rela.hdr
2642 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2643 external_relocs,
2644 internal_rela_relocs)))
45d6a902
AM
2645 goto error_return;
2646
2647 /* Cache the results for next time, if we can. */
2648 if (keep_memory)
d4730f92 2649 esdo->relocs = internal_relocs;
45d6a902
AM
2650
2651 if (alloc1 != NULL)
2652 free (alloc1);
2653
2654 /* Don't free alloc2, since if it was allocated we are passing it
2655 back (under the name of internal_relocs). */
2656
2657 return internal_relocs;
2658
2659 error_return:
2660 if (alloc1 != NULL)
2661 free (alloc1);
2662 if (alloc2 != NULL)
4dd07732
AM
2663 {
2664 if (keep_memory)
2665 bfd_release (abfd, alloc2);
2666 else
2667 free (alloc2);
2668 }
45d6a902
AM
2669 return NULL;
2670}
2671
2672/* Compute the size of, and allocate space for, REL_HDR which is the
2673 section header for a section containing relocations for O. */
2674
28caa186 2675static bfd_boolean
9eaff861
AO
2676_bfd_elf_link_size_reloc_section (bfd *abfd,
2677 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2678{
9eaff861 2679 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2680
2681 /* That allows us to calculate the size of the section. */
9eaff861 2682 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2683
2684 /* The contents field must last into write_object_contents, so we
2685 allocate it with bfd_alloc rather than malloc. Also since we
2686 cannot be sure that the contents will actually be filled in,
2687 we zero the allocated space. */
a50b1753 2688 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2689 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2690 return FALSE;
2691
d4730f92 2692 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2693 {
2694 struct elf_link_hash_entry **p;
2695
ca4be51c
AM
2696 p = ((struct elf_link_hash_entry **)
2697 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2698 if (p == NULL)
2699 return FALSE;
2700
d4730f92 2701 reldata->hashes = p;
45d6a902
AM
2702 }
2703
2704 return TRUE;
2705}
2706
2707/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2708 originated from the section given by INPUT_REL_HDR) to the
2709 OUTPUT_BFD. */
2710
2711bfd_boolean
268b6b39
AM
2712_bfd_elf_link_output_relocs (bfd *output_bfd,
2713 asection *input_section,
2714 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2715 Elf_Internal_Rela *internal_relocs,
2716 struct elf_link_hash_entry **rel_hash
2717 ATTRIBUTE_UNUSED)
45d6a902
AM
2718{
2719 Elf_Internal_Rela *irela;
2720 Elf_Internal_Rela *irelaend;
2721 bfd_byte *erel;
d4730f92 2722 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2723 asection *output_section;
9c5bfbb7 2724 const struct elf_backend_data *bed;
268b6b39 2725 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2726 struct bfd_elf_section_data *esdo;
45d6a902
AM
2727
2728 output_section = input_section->output_section;
45d6a902 2729
d4730f92
BS
2730 bed = get_elf_backend_data (output_bfd);
2731 esdo = elf_section_data (output_section);
2732 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2733 {
d4730f92
BS
2734 output_reldata = &esdo->rel;
2735 swap_out = bed->s->swap_reloc_out;
45d6a902 2736 }
d4730f92
BS
2737 else if (esdo->rela.hdr
2738 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2739 {
d4730f92
BS
2740 output_reldata = &esdo->rela;
2741 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2742 }
2743 else
2744 {
4eca0228 2745 _bfd_error_handler
695344c0 2746 /* xgettext:c-format */
871b3ab2 2747 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2748 output_bfd, input_section->owner, input_section);
297d8443 2749 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2750 return FALSE;
2751 }
2752
d4730f92
BS
2753 erel = output_reldata->hdr->contents;
2754 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2755 irela = internal_relocs;
2756 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2757 * bed->s->int_rels_per_ext_rel);
2758 while (irela < irelaend)
2759 {
2760 (*swap_out) (output_bfd, irela, erel);
2761 irela += bed->s->int_rels_per_ext_rel;
2762 erel += input_rel_hdr->sh_entsize;
2763 }
2764
2765 /* Bump the counter, so that we know where to add the next set of
2766 relocations. */
d4730f92 2767 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2768
2769 return TRUE;
2770}
2771\f
508c3946
L
2772/* Make weak undefined symbols in PIE dynamic. */
2773
2774bfd_boolean
2775_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2776 struct elf_link_hash_entry *h)
2777{
0e1862bb 2778 if (bfd_link_pie (info)
508c3946
L
2779 && h->dynindx == -1
2780 && h->root.type == bfd_link_hash_undefweak)
2781 return bfd_elf_link_record_dynamic_symbol (info, h);
2782
2783 return TRUE;
2784}
2785
45d6a902
AM
2786/* Fix up the flags for a symbol. This handles various cases which
2787 can only be fixed after all the input files are seen. This is
2788 currently called by both adjust_dynamic_symbol and
2789 assign_sym_version, which is unnecessary but perhaps more robust in
2790 the face of future changes. */
2791
28caa186 2792static bfd_boolean
268b6b39
AM
2793_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2794 struct elf_info_failed *eif)
45d6a902 2795{
33774f08 2796 const struct elf_backend_data *bed;
508c3946 2797
45d6a902
AM
2798 /* If this symbol was mentioned in a non-ELF file, try to set
2799 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2800 permit a non-ELF file to correctly refer to a symbol defined in
2801 an ELF dynamic object. */
f5385ebf 2802 if (h->non_elf)
45d6a902
AM
2803 {
2804 while (h->root.type == bfd_link_hash_indirect)
2805 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2806
2807 if (h->root.type != bfd_link_hash_defined
2808 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2809 {
2810 h->ref_regular = 1;
2811 h->ref_regular_nonweak = 1;
2812 }
45d6a902
AM
2813 else
2814 {
2815 if (h->root.u.def.section->owner != NULL
2816 && (bfd_get_flavour (h->root.u.def.section->owner)
2817 == bfd_target_elf_flavour))
f5385ebf
AM
2818 {
2819 h->ref_regular = 1;
2820 h->ref_regular_nonweak = 1;
2821 }
45d6a902 2822 else
f5385ebf 2823 h->def_regular = 1;
45d6a902
AM
2824 }
2825
2826 if (h->dynindx == -1
f5385ebf
AM
2827 && (h->def_dynamic
2828 || h->ref_dynamic))
45d6a902 2829 {
c152c796 2830 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2831 {
2832 eif->failed = TRUE;
2833 return FALSE;
2834 }
2835 }
2836 }
2837 else
2838 {
f5385ebf 2839 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2840 was first seen in a non-ELF file. Fortunately, if the symbol
2841 was first seen in an ELF file, we're probably OK unless the
2842 symbol was defined in a non-ELF file. Catch that case here.
2843 FIXME: We're still in trouble if the symbol was first seen in
2844 a dynamic object, and then later in a non-ELF regular object. */
2845 if ((h->root.type == bfd_link_hash_defined
2846 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2847 && !h->def_regular
45d6a902
AM
2848 && (h->root.u.def.section->owner != NULL
2849 ? (bfd_get_flavour (h->root.u.def.section->owner)
2850 != bfd_target_elf_flavour)
2851 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2852 && !h->def_dynamic)))
2853 h->def_regular = 1;
45d6a902
AM
2854 }
2855
508c3946 2856 /* Backend specific symbol fixup. */
33774f08
AM
2857 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2858 if (bed->elf_backend_fixup_symbol
2859 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2860 return FALSE;
508c3946 2861
45d6a902
AM
2862 /* If this is a final link, and the symbol was defined as a common
2863 symbol in a regular object file, and there was no definition in
2864 any dynamic object, then the linker will have allocated space for
f5385ebf 2865 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2866 flag will not have been set. */
2867 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2868 && !h->def_regular
2869 && h->ref_regular
2870 && !h->def_dynamic
96f29d96 2871 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2872 h->def_regular = 1;
45d6a902 2873
af0bfb9c
AM
2874 /* Symbols defined in discarded sections shouldn't be dynamic. */
2875 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2876 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2877
4deb8f71
L
2878 /* If a weak undefined symbol has non-default visibility, we also
2879 hide it from the dynamic linker. */
af0bfb9c
AM
2880 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2881 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2883
2884 /* A hidden versioned symbol in executable should be forced local if
2885 it is is locally defined, not referenced by shared library and not
2886 exported. */
2887 else if (bfd_link_executable (eif->info)
2888 && h->versioned == versioned_hidden
2889 && !eif->info->export_dynamic
2890 && !h->dynamic
2891 && !h->ref_dynamic
2892 && h->def_regular)
2893 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2894
45d6a902
AM
2895 /* If -Bsymbolic was used (which means to bind references to global
2896 symbols to the definition within the shared object), and this
2897 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2898 need a PLT entry. Likewise, if the symbol has non-default
2899 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2900 will force it local. */
4deb8f71
L
2901 else if (h->needs_plt
2902 && bfd_link_pic (eif->info)
2903 && is_elf_hash_table (eif->info->hash)
2904 && (SYMBOLIC_BIND (eif->info, h)
2905 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2906 && h->def_regular)
45d6a902 2907 {
45d6a902
AM
2908 bfd_boolean force_local;
2909
45d6a902
AM
2910 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2911 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2913 }
2914
45d6a902
AM
2915 /* If this is a weak defined symbol in a dynamic object, and we know
2916 the real definition in the dynamic object, copy interesting flags
2917 over to the real definition. */
60d67dc8 2918 if (h->is_weakalias)
45d6a902 2919 {
60d67dc8
AM
2920 struct elf_link_hash_entry *def = weakdef (h);
2921
45d6a902
AM
2922 /* If the real definition is defined by a regular object file,
2923 don't do anything special. See the longer description in
2924 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2925 if (def->def_regular)
2926 {
2927 h = def;
2928 while ((h = h->u.alias) != def)
2929 h->is_weakalias = 0;
2930 }
45d6a902 2931 else
a26587ba 2932 {
4e6b54a6
AM
2933 while (h->root.type == bfd_link_hash_indirect)
2934 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2935 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2936 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2937 BFD_ASSERT (def->def_dynamic);
2938 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2939 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2940 }
45d6a902
AM
2941 }
2942
2943 return TRUE;
2944}
2945
2946/* Make the backend pick a good value for a dynamic symbol. This is
2947 called via elf_link_hash_traverse, and also calls itself
2948 recursively. */
2949
28caa186 2950static bfd_boolean
268b6b39 2951_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2952{
a50b1753 2953 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2954 struct elf_link_hash_table *htab;
9c5bfbb7 2955 const struct elf_backend_data *bed;
45d6a902 2956
0eddce27 2957 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2958 return FALSE;
2959
45d6a902
AM
2960 /* Ignore indirect symbols. These are added by the versioning code. */
2961 if (h->root.type == bfd_link_hash_indirect)
2962 return TRUE;
2963
2964 /* Fix the symbol flags. */
2965 if (! _bfd_elf_fix_symbol_flags (h, eif))
2966 return FALSE;
2967
559192d8
AM
2968 htab = elf_hash_table (eif->info);
2969 bed = get_elf_backend_data (htab->dynobj);
2970
954b63d4
AM
2971 if (h->root.type == bfd_link_hash_undefweak)
2972 {
2973 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2974 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2975 else if (eif->info->dynamic_undefined_weak > 0
2976 && h->ref_regular
2977 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2978 && !bfd_hide_sym_by_version (eif->info->version_info,
2979 h->root.root.string))
2980 {
2981 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2982 {
2983 eif->failed = TRUE;
2984 return FALSE;
2985 }
2986 }
2987 }
2988
45d6a902
AM
2989 /* If this symbol does not require a PLT entry, and it is not
2990 defined by a dynamic object, or is not referenced by a regular
2991 object, ignore it. We do have to handle a weak defined symbol,
2992 even if no regular object refers to it, if we decided to add it
2993 to the dynamic symbol table. FIXME: Do we normally need to worry
2994 about symbols which are defined by one dynamic object and
2995 referenced by another one? */
f5385ebf 2996 if (!h->needs_plt
91e21fb7 2997 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2998 && (h->def_regular
2999 || !h->def_dynamic
3000 || (!h->ref_regular
60d67dc8 3001 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3002 {
a6aa5195 3003 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3004 return TRUE;
3005 }
3006
3007 /* If we've already adjusted this symbol, don't do it again. This
3008 can happen via a recursive call. */
f5385ebf 3009 if (h->dynamic_adjusted)
45d6a902
AM
3010 return TRUE;
3011
3012 /* Don't look at this symbol again. Note that we must set this
3013 after checking the above conditions, because we may look at a
3014 symbol once, decide not to do anything, and then get called
3015 recursively later after REF_REGULAR is set below. */
f5385ebf 3016 h->dynamic_adjusted = 1;
45d6a902
AM
3017
3018 /* If this is a weak definition, and we know a real definition, and
3019 the real symbol is not itself defined by a regular object file,
3020 then get a good value for the real definition. We handle the
3021 real symbol first, for the convenience of the backend routine.
3022
3023 Note that there is a confusing case here. If the real definition
3024 is defined by a regular object file, we don't get the real symbol
3025 from the dynamic object, but we do get the weak symbol. If the
3026 processor backend uses a COPY reloc, then if some routine in the
3027 dynamic object changes the real symbol, we will not see that
3028 change in the corresponding weak symbol. This is the way other
3029 ELF linkers work as well, and seems to be a result of the shared
3030 library model.
3031
3032 I will clarify this issue. Most SVR4 shared libraries define the
3033 variable _timezone and define timezone as a weak synonym. The
3034 tzset call changes _timezone. If you write
3035 extern int timezone;
3036 int _timezone = 5;
3037 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3038 you might expect that, since timezone is a synonym for _timezone,
3039 the same number will print both times. However, if the processor
3040 backend uses a COPY reloc, then actually timezone will be copied
3041 into your process image, and, since you define _timezone
3042 yourself, _timezone will not. Thus timezone and _timezone will
3043 wind up at different memory locations. The tzset call will set
3044 _timezone, leaving timezone unchanged. */
3045
60d67dc8 3046 if (h->is_weakalias)
45d6a902 3047 {
60d67dc8
AM
3048 struct elf_link_hash_entry *def = weakdef (h);
3049
ec24dc88 3050 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3051 the alias by a regular object file via the weak symbol H. */
3052 def->ref_regular = 1;
45d6a902 3053
ec24dc88 3054 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3055 the strong alias before H by recursively calling ourselves. */
3056 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3057 return FALSE;
3058 }
3059
3060 /* If a symbol has no type and no size and does not require a PLT
3061 entry, then we are probably about to do the wrong thing here: we
3062 are probably going to create a COPY reloc for an empty object.
3063 This case can arise when a shared object is built with assembly
3064 code, and the assembly code fails to set the symbol type. */
3065 if (h->size == 0
3066 && h->type == STT_NOTYPE
f5385ebf 3067 && !h->needs_plt)
4eca0228 3068 _bfd_error_handler
45d6a902
AM
3069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3070 h->root.root.string);
3071
45d6a902
AM
3072 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3073 {
3074 eif->failed = TRUE;
3075 return FALSE;
3076 }
3077
3078 return TRUE;
3079}
3080
027297b7
L
3081/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3082 DYNBSS. */
3083
3084bfd_boolean
6cabe1ea
AM
3085_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3086 struct elf_link_hash_entry *h,
027297b7
L
3087 asection *dynbss)
3088{
91ac5911 3089 unsigned int power_of_two;
027297b7
L
3090 bfd_vma mask;
3091 asection *sec = h->root.u.def.section;
3092
de194d85 3093 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3094 requirement of symbols defined in the section. Since we don't
3095 know the symbol alignment requirement, we start with the
3096 maximum alignment and check low bits of the symbol address
3097 for the minimum alignment. */
3098 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3099 mask = ((bfd_vma) 1 << power_of_two) - 1;
3100 while ((h->root.u.def.value & mask) != 0)
3101 {
3102 mask >>= 1;
3103 --power_of_two;
3104 }
027297b7 3105
91ac5911
L
3106 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3107 dynbss))
027297b7
L
3108 {
3109 /* Adjust the section alignment if needed. */
3110 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3111 power_of_two))
027297b7
L
3112 return FALSE;
3113 }
3114
91ac5911 3115 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
f7483970
L
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
889c2a67
L
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3130 info->callbacks->einfo
c1c8c1ef 3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3132 h->root.root.string);
6cabe1ea 3133
027297b7
L
3134 return TRUE;
3135}
3136
45d6a902
AM
3137/* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
28caa186 3140static bfd_boolean
268b6b39 3141_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3142{
3143 asection *sec;
3144
45d6a902
AM
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3149 {
a50b1753 3150 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
753731ee 3156 h->root.u.def.value);
45d6a902
AM
3157 }
3158
3159 return TRUE;
3160}
986a241f
RH
3161
3162/* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166bfd_boolean
268b6b39
AM
3167_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
89a2ee5a 3169 bfd_boolean not_local_protected)
986a241f
RH
3170{
3171 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
986a241f
RH
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
f5385ebf 3185 if (h->forced_local)
986a241f
RH
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
0e1862bb
L
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
986a241f
RH
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
fcb93ecf
PB
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
986a241f
RH
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
89a2ee5a 3209 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
986a241f
RH
3214 break;
3215 }
3216
aa37626c 3217 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3219 return TRUE;
3220
986a241f
RH
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224}
f6c52c13
AM
3225
3226/* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3229 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
89a2ee5a
AM
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3238
3239bfd_boolean
268b6b39
AM
3240_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
f6c52c13 3243{
fcb93ecf
PB
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
f6c52c13
AM
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
d95edcac
L
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
0fad2956
MR
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
7e2294f9
AO
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
f6c52c13 3264 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
f6c52c13
AM
3267 return FALSE;
3268
0fad2956 3269 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
0e1862bb 3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
fcb93ecf
PB
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
f7483970
L
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
889c2a67
L
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
1c16dfa5
L
3296 return TRUE;
3297
f6c52c13 3298 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3303 return local_protected;
3304}
e1918d23
AM
3305
3306/* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309struct bfd_section *
3310_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311{
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332}
0ad989f9
L
3333
3334/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335static bfd_boolean
3336is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338{
a4d8e49b
L
3339 const struct elf_backend_data *bed;
3340
0ad989f9
L
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
fcb93ecf 3346 bed = get_elf_backend_data (abfd);
0ad989f9 3347 /* Function symbols do not count. */
fcb93ecf 3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
a4d8e49b 3357 if (bed->common_definition (sym))
0ad989f9
L
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373}
3374
3375/* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378static bfd_boolean
3379elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380{
3381 Elf_Internal_Shdr * hdr;
ef53be89
AM
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
0ad989f9
L
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
7dc3990e
L
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3401#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404#endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462}
3463\f
5a580b3a
AM
3464/* Add an entry to the .dynamic table. */
3465
3466bfd_boolean
3467_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470{
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
7f923b7f
AM
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
5a580b3a 3485 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3487 BFD_ASSERT (s != NULL);
3488
eea6121a 3489 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
eea6121a 3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3497
eea6121a 3498 s->size = newsize;
5a580b3a
AM
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502}
3503
3504/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3507
4ad4eba5 3508static int
7e9f0867
AM
3509elf_add_dt_needed_tag (bfd *abfd,
3510 struct bfd_link_info *info,
4ad4eba5
AM
3511 const char *soname,
3512 bfd_boolean do_it)
5a580b3a
AM
3513{
3514 struct elf_link_hash_table *hash_table;
ef53be89 3515 size_t strindex;
5a580b3a 3516
7e9f0867
AM
3517 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3518 return -1;
3519
5a580b3a 3520 hash_table = elf_hash_table (info);
5a580b3a 3521 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3522 if (strindex == (size_t) -1)
5a580b3a
AM
3523 return -1;
3524
02be4619 3525 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3526 {
3527 asection *sdyn;
3528 const struct elf_backend_data *bed;
3529 bfd_byte *extdyn;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3532 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3533 if (sdyn != NULL)
3534 for (extdyn = sdyn->contents;
3535 extdyn < sdyn->contents + sdyn->size;
3536 extdyn += bed->s->sizeof_dyn)
3537 {
3538 Elf_Internal_Dyn dyn;
5a580b3a 3539
7e9f0867
AM
3540 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3541 if (dyn.d_tag == DT_NEEDED
3542 && dyn.d_un.d_val == strindex)
3543 {
3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3545 return 1;
3546 }
3547 }
5a580b3a
AM
3548 }
3549
3550 if (do_it)
3551 {
7e9f0867
AM
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3553 return -1;
3554
5a580b3a
AM
3555 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3556 return -1;
3557 }
3558 else
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3561
3562 return 0;
3563}
3564
7b15fa7a
AM
3565/* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3567 will be loaded. */
3568
010e5ae2 3569static bfd_boolean
7b15fa7a
AM
3570on_needed_list (const char *soname,
3571 struct bfd_link_needed_list *needed,
3572 struct bfd_link_needed_list *stop)
010e5ae2 3573{
7b15fa7a
AM
3574 struct bfd_link_needed_list *look;
3575 for (look = needed; look != stop; look = look->next)
3576 if (strcmp (soname, look->name) == 0
3577 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3585 return TRUE;
3586
3587 return FALSE;
3588}
3589
14160578 3590/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3591static int
3592elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3593{
3594 const struct elf_link_hash_entry *h1;
3595 const struct elf_link_hash_entry *h2;
10b7e05b 3596 bfd_signed_vma vdiff;
5a580b3a
AM
3597
3598 h1 = *(const struct elf_link_hash_entry **) arg1;
3599 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3600 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3601 if (vdiff != 0)
3602 return vdiff > 0 ? 1 : -1;
3603 else
3604 {
d3435ae8 3605 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3606 if (sdiff != 0)
3607 return sdiff > 0 ? 1 : -1;
3608 }
14160578
AM
3609 vdiff = h1->size - h2->size;
3610 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3611}
4ad4eba5 3612
5a580b3a
AM
3613/* This function is used to adjust offsets into .dynstr for
3614 dynamic symbols. This is called via elf_link_hash_traverse. */
3615
3616static bfd_boolean
3617elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3618{
a50b1753 3619 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3620
5a580b3a
AM
3621 if (h->dynindx != -1)
3622 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3623 return TRUE;
3624}
3625
3626/* Assign string offsets in .dynstr, update all structures referencing
3627 them. */
3628
4ad4eba5
AM
3629static bfd_boolean
3630elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3631{
3632 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3633 struct elf_link_local_dynamic_entry *entry;
3634 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3635 bfd *dynobj = hash_table->dynobj;
3636 asection *sdyn;
3637 bfd_size_type size;
3638 const struct elf_backend_data *bed;
3639 bfd_byte *extdyn;
3640
3641 _bfd_elf_strtab_finalize (dynstr);
3642 size = _bfd_elf_strtab_size (dynstr);
3643
3644 bed = get_elf_backend_data (dynobj);
3d4d4302 3645 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3646 BFD_ASSERT (sdyn != NULL);
3647
3648 /* Update all .dynamic entries referencing .dynstr strings. */
3649 for (extdyn = sdyn->contents;
eea6121a 3650 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3651 extdyn += bed->s->sizeof_dyn)
3652 {
3653 Elf_Internal_Dyn dyn;
3654
3655 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3656 switch (dyn.d_tag)
3657 {
3658 case DT_STRSZ:
3659 dyn.d_un.d_val = size;
3660 break;
3661 case DT_NEEDED:
3662 case DT_SONAME:
3663 case DT_RPATH:
3664 case DT_RUNPATH:
3665 case DT_FILTER:
3666 case DT_AUXILIARY:
7ee314fa
AM
3667 case DT_AUDIT:
3668 case DT_DEPAUDIT:
5a580b3a
AM
3669 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3670 break;
3671 default:
3672 continue;
3673 }
3674 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3675 }
3676
3677 /* Now update local dynamic symbols. */
3678 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3679 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3680 entry->isym.st_name);
3681
3682 /* And the rest of dynamic symbols. */
3683 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3684
3685 /* Adjust version definitions. */
3686 if (elf_tdata (output_bfd)->cverdefs)
3687 {
3688 asection *s;
3689 bfd_byte *p;
ef53be89 3690 size_t i;
5a580b3a
AM
3691 Elf_Internal_Verdef def;
3692 Elf_Internal_Verdaux defaux;
3693
3d4d4302 3694 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3695 p = s->contents;
3696 do
3697 {
3698 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3699 &def);
3700 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3701 if (def.vd_aux != sizeof (Elf_External_Verdef))
3702 continue;
5a580b3a
AM
3703 for (i = 0; i < def.vd_cnt; ++i)
3704 {
3705 _bfd_elf_swap_verdaux_in (output_bfd,
3706 (Elf_External_Verdaux *) p, &defaux);
3707 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3708 defaux.vda_name);
3709 _bfd_elf_swap_verdaux_out (output_bfd,
3710 &defaux, (Elf_External_Verdaux *) p);
3711 p += sizeof (Elf_External_Verdaux);
3712 }
3713 }
3714 while (def.vd_next);
3715 }
3716
3717 /* Adjust version references. */
3718 if (elf_tdata (output_bfd)->verref)
3719 {
3720 asection *s;
3721 bfd_byte *p;
ef53be89 3722 size_t i;
5a580b3a
AM
3723 Elf_Internal_Verneed need;
3724 Elf_Internal_Vernaux needaux;
3725
3d4d4302 3726 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3727 p = s->contents;
3728 do
3729 {
3730 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3731 &need);
3732 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3733 _bfd_elf_swap_verneed_out (output_bfd, &need,
3734 (Elf_External_Verneed *) p);
3735 p += sizeof (Elf_External_Verneed);
3736 for (i = 0; i < need.vn_cnt; ++i)
3737 {
3738 _bfd_elf_swap_vernaux_in (output_bfd,
3739 (Elf_External_Vernaux *) p, &needaux);
3740 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3741 needaux.vna_name);
3742 _bfd_elf_swap_vernaux_out (output_bfd,
3743 &needaux,
3744 (Elf_External_Vernaux *) p);
3745 p += sizeof (Elf_External_Vernaux);
3746 }
3747 }
3748 while (need.vn_next);
3749 }
3750
3751 return TRUE;
3752}
3753\f
13285a1b
AM
3754/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 The default is to only match when the INPUT and OUTPUT are exactly
3756 the same target. */
3757
3758bfd_boolean
3759_bfd_elf_default_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3761{
3762 return input == output;
3763}
3764
3765/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3766 This version is used when different targets for the same architecture
3767 are virtually identical. */
3768
3769bfd_boolean
3770_bfd_elf_relocs_compatible (const bfd_target *input,
3771 const bfd_target *output)
3772{
3773 const struct elf_backend_data *obed, *ibed;
3774
3775 if (input == output)
3776 return TRUE;
3777
3778 ibed = xvec_get_elf_backend_data (input);
3779 obed = xvec_get_elf_backend_data (output);
3780
3781 if (ibed->arch != obed->arch)
3782 return FALSE;
3783
3784 /* If both backends are using this function, deem them compatible. */
3785 return ibed->relocs_compatible == obed->relocs_compatible;
3786}
3787
e5034e59
AM
3788/* Make a special call to the linker "notice" function to tell it that
3789 we are about to handle an as-needed lib, or have finished
1b786873 3790 processing the lib. */
e5034e59
AM
3791
3792bfd_boolean
3793_bfd_elf_notice_as_needed (bfd *ibfd,
3794 struct bfd_link_info *info,
3795 enum notice_asneeded_action act)
3796{
46135103 3797 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3798}
3799
d9689752
L
3800/* Check relocations an ELF object file. */
3801
3802bfd_boolean
3803_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3804{
3805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3806 struct elf_link_hash_table *htab = elf_hash_table (info);
3807
3808 /* If this object is the same format as the output object, and it is
3809 not a shared library, then let the backend look through the
3810 relocs.
3811
3812 This is required to build global offset table entries and to
3813 arrange for dynamic relocs. It is not required for the
3814 particular common case of linking non PIC code, even when linking
3815 against shared libraries, but unfortunately there is no way of
3816 knowing whether an object file has been compiled PIC or not.
3817 Looking through the relocs is not particularly time consuming.
3818 The problem is that we must either (1) keep the relocs in memory,
3819 which causes the linker to require additional runtime memory or
3820 (2) read the relocs twice from the input file, which wastes time.
3821 This would be a good case for using mmap.
3822
3823 I have no idea how to handle linking PIC code into a file of a
3824 different format. It probably can't be done. */
3825 if ((abfd->flags & DYNAMIC) == 0
3826 && is_elf_hash_table (htab)
3827 && bed->check_relocs != NULL
3828 && elf_object_id (abfd) == elf_hash_table_id (htab)
3829 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3830 {
3831 asection *o;
3832
3833 for (o = abfd->sections; o != NULL; o = o->next)
3834 {
3835 Elf_Internal_Rela *internal_relocs;
3836 bfd_boolean ok;
3837
5ce03cea 3838 /* Don't check relocations in excluded sections. */
d9689752 3839 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3840 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3841 || o->reloc_count == 0
3842 || ((info->strip == strip_all || info->strip == strip_debugger)
3843 && (o->flags & SEC_DEBUGGING) != 0)
3844 || bfd_is_abs_section (o->output_section))
3845 continue;
3846
3847 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3848 info->keep_memory);
3849 if (internal_relocs == NULL)
3850 return FALSE;
3851
3852 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3853
3854 if (elf_section_data (o)->relocs != internal_relocs)
3855 free (internal_relocs);
3856
3857 if (! ok)
3858 return FALSE;
3859 }
3860 }
3861
3862 return TRUE;
3863}
3864
4ad4eba5
AM
3865/* Add symbols from an ELF object file to the linker hash table. */
3866
3867static bfd_boolean
3868elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3869{
a0c402a5 3870 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3871 Elf_Internal_Shdr *hdr;
ef53be89
AM
3872 size_t symcount;
3873 size_t extsymcount;
3874 size_t extsymoff;
4ad4eba5
AM
3875 struct elf_link_hash_entry **sym_hash;
3876 bfd_boolean dynamic;
3877 Elf_External_Versym *extversym = NULL;
be22c732 3878 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3879 Elf_External_Versym *ever;
3880 struct elf_link_hash_entry *weaks;
3881 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3882 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3883 Elf_Internal_Sym *isymbuf = NULL;
3884 Elf_Internal_Sym *isym;
3885 Elf_Internal_Sym *isymend;
3886 const struct elf_backend_data *bed;
3887 bfd_boolean add_needed;
66eb6687 3888 struct elf_link_hash_table *htab;
4ad4eba5 3889 bfd_size_type amt;
66eb6687 3890 void *alloc_mark = NULL;
4f87808c
AM
3891 struct bfd_hash_entry **old_table = NULL;
3892 unsigned int old_size = 0;
3893 unsigned int old_count = 0;
66eb6687 3894 void *old_tab = NULL;
66eb6687
AM
3895 void *old_ent;
3896 struct bfd_link_hash_entry *old_undefs = NULL;
3897 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3898 void *old_strtab = NULL;
66eb6687 3899 size_t tabsize = 0;
db6a5d5f 3900 asection *s;
29a9f53e 3901 bfd_boolean just_syms;
4ad4eba5 3902
66eb6687 3903 htab = elf_hash_table (info);
4ad4eba5 3904 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3905
3906 if ((abfd->flags & DYNAMIC) == 0)
3907 dynamic = FALSE;
3908 else
3909 {
3910 dynamic = TRUE;
3911
3912 /* You can't use -r against a dynamic object. Also, there's no
3913 hope of using a dynamic object which does not exactly match
3914 the format of the output file. */
0e1862bb 3915 if (bfd_link_relocatable (info)
66eb6687 3916 || !is_elf_hash_table (htab)
f13a99db 3917 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3918 {
0e1862bb 3919 if (bfd_link_relocatable (info))
9a0789ec
NC
3920 bfd_set_error (bfd_error_invalid_operation);
3921 else
3922 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3923 goto error_return;
3924 }
3925 }
3926
a0c402a5
L
3927 ehdr = elf_elfheader (abfd);
3928 if (info->warn_alternate_em
3929 && bed->elf_machine_code != ehdr->e_machine
3930 && ((bed->elf_machine_alt1 != 0
3931 && ehdr->e_machine == bed->elf_machine_alt1)
3932 || (bed->elf_machine_alt2 != 0
3933 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3934 _bfd_error_handler
695344c0 3935 /* xgettext:c-format */
9793eb77 3936 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3937 ehdr->e_machine, abfd, bed->elf_machine_code);
3938
4ad4eba5
AM
3939 /* As a GNU extension, any input sections which are named
3940 .gnu.warning.SYMBOL are treated as warning symbols for the given
3941 symbol. This differs from .gnu.warning sections, which generate
3942 warnings when they are included in an output file. */
dd98f8d2 3943 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3944 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3945 {
db6a5d5f 3946 const char *name;
4ad4eba5 3947
db6a5d5f
AM
3948 name = bfd_get_section_name (abfd, s);
3949 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3950 {
db6a5d5f
AM
3951 char *msg;
3952 bfd_size_type sz;
3953
3954 name += sizeof ".gnu.warning." - 1;
3955
3956 /* If this is a shared object, then look up the symbol
3957 in the hash table. If it is there, and it is already
3958 been defined, then we will not be using the entry
3959 from this shared object, so we don't need to warn.
3960 FIXME: If we see the definition in a regular object
3961 later on, we will warn, but we shouldn't. The only
3962 fix is to keep track of what warnings we are supposed
3963 to emit, and then handle them all at the end of the
3964 link. */
3965 if (dynamic)
4ad4eba5 3966 {
db6a5d5f
AM
3967 struct elf_link_hash_entry *h;
3968
3969 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3970
3971 /* FIXME: What about bfd_link_hash_common? */
3972 if (h != NULL
3973 && (h->root.type == bfd_link_hash_defined
3974 || h->root.type == bfd_link_hash_defweak))
3975 continue;
3976 }
4ad4eba5 3977
db6a5d5f
AM
3978 sz = s->size;
3979 msg = (char *) bfd_alloc (abfd, sz + 1);
3980 if (msg == NULL)
3981 goto error_return;
4ad4eba5 3982
db6a5d5f
AM
3983 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3984 goto error_return;
4ad4eba5 3985
db6a5d5f 3986 msg[sz] = '\0';
4ad4eba5 3987
db6a5d5f
AM
3988 if (! (_bfd_generic_link_add_one_symbol
3989 (info, abfd, name, BSF_WARNING, s, 0, msg,
3990 FALSE, bed->collect, NULL)))
3991 goto error_return;
4ad4eba5 3992
0e1862bb 3993 if (bfd_link_executable (info))
db6a5d5f
AM
3994 {
3995 /* Clobber the section size so that the warning does
3996 not get copied into the output file. */
3997 s->size = 0;
11d2f718 3998
db6a5d5f
AM
3999 /* Also set SEC_EXCLUDE, so that symbols defined in
4000 the warning section don't get copied to the output. */
4001 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4002 }
4003 }
4004 }
4005
29a9f53e
L
4006 just_syms = ((s = abfd->sections) != NULL
4007 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4008
4ad4eba5
AM
4009 add_needed = TRUE;
4010 if (! dynamic)
4011 {
4012 /* If we are creating a shared library, create all the dynamic
4013 sections immediately. We need to attach them to something,
4014 so we attach them to this BFD, provided it is the right
bf89386a
L
4015 format and is not from ld --just-symbols. Always create the
4016 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4017 are no input BFD's of the same format as the output, we can't
4018 make a shared library. */
4019 if (!just_syms
bf89386a 4020 && (bfd_link_pic (info)
9c1d7a08 4021 || (!bfd_link_relocatable (info)
3c5fce9b 4022 && info->nointerp
9c1d7a08 4023 && (info->export_dynamic || info->dynamic)))
66eb6687 4024 && is_elf_hash_table (htab)
f13a99db 4025 && info->output_bfd->xvec == abfd->xvec
66eb6687 4026 && !htab->dynamic_sections_created)
4ad4eba5
AM
4027 {
4028 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4029 goto error_return;
4030 }
4031 }
66eb6687 4032 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4033 goto error_return;
4034 else
4035 {
4ad4eba5 4036 const char *soname = NULL;
7ee314fa 4037 char *audit = NULL;
4ad4eba5 4038 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4039 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4040 int ret;
4041
4042 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4043 ld shouldn't allow it. */
29a9f53e 4044 if (just_syms)
92fd189d 4045 abort ();
4ad4eba5
AM
4046
4047 /* If this dynamic lib was specified on the command line with
4048 --as-needed in effect, then we don't want to add a DT_NEEDED
4049 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4050 in by another lib's DT_NEEDED. When --no-add-needed is used
4051 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4052 any dynamic library in DT_NEEDED tags in the dynamic lib at
4053 all. */
4054 add_needed = (elf_dyn_lib_class (abfd)
4055 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4056 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4057
4058 s = bfd_get_section_by_name (abfd, ".dynamic");
4059 if (s != NULL)
4060 {
4061 bfd_byte *dynbuf;
4062 bfd_byte *extdyn;
cb33740c 4063 unsigned int elfsec;
4ad4eba5
AM
4064 unsigned long shlink;
4065
eea6121a 4066 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4067 {
4068error_free_dyn:
4069 free (dynbuf);
4070 goto error_return;
4071 }
4ad4eba5
AM
4072
4073 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4074 if (elfsec == SHN_BAD)
4ad4eba5
AM
4075 goto error_free_dyn;
4076 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4077
4078 for (extdyn = dynbuf;
eea6121a 4079 extdyn < dynbuf + s->size;
4ad4eba5
AM
4080 extdyn += bed->s->sizeof_dyn)
4081 {
4082 Elf_Internal_Dyn dyn;
4083
4084 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4085 if (dyn.d_tag == DT_SONAME)
4086 {
4087 unsigned int tagv = dyn.d_un.d_val;
4088 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4089 if (soname == NULL)
4090 goto error_free_dyn;
4091 }
4092 if (dyn.d_tag == DT_NEEDED)
4093 {
4094 struct bfd_link_needed_list *n, **pn;
4095 char *fnm, *anm;
4096 unsigned int tagv = dyn.d_un.d_val;
4097
4098 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
a50b1753 4104 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4105 if (anm == NULL)
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4108 n->name = anm;
4109 n->by = abfd;
4110 n->next = NULL;
66eb6687 4111 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4112 ;
4113 *pn = n;
4114 }
4115 if (dyn.d_tag == DT_RUNPATH)
4116 {
4117 struct bfd_link_needed_list *n, **pn;
4118 char *fnm, *anm;
4119 unsigned int tagv = dyn.d_un.d_val;
4120
4121 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4122 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4123 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4124 if (n == NULL || fnm == NULL)
4125 goto error_free_dyn;
4126 amt = strlen (fnm) + 1;
a50b1753 4127 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4128 if (anm == NULL)
4129 goto error_free_dyn;
4130 memcpy (anm, fnm, amt);
4131 n->name = anm;
4132 n->by = abfd;
4133 n->next = NULL;
4134 for (pn = & runpath;
4135 *pn != NULL;
4136 pn = &(*pn)->next)
4137 ;
4138 *pn = n;
4139 }
4140 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4141 if (!runpath && dyn.d_tag == DT_RPATH)
4142 {
4143 struct bfd_link_needed_list *n, **pn;
4144 char *fnm, *anm;
4145 unsigned int tagv = dyn.d_un.d_val;
4146
4147 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4148 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4149 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4150 if (n == NULL || fnm == NULL)
4151 goto error_free_dyn;
4152 amt = strlen (fnm) + 1;
a50b1753 4153 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4154 if (anm == NULL)
f8703194 4155 goto error_free_dyn;
4ad4eba5
AM
4156 memcpy (anm, fnm, amt);
4157 n->name = anm;
4158 n->by = abfd;
4159 n->next = NULL;
4160 for (pn = & rpath;
4161 *pn != NULL;
4162 pn = &(*pn)->next)
4163 ;
4164 *pn = n;
4165 }
7ee314fa
AM
4166 if (dyn.d_tag == DT_AUDIT)
4167 {
4168 unsigned int tagv = dyn.d_un.d_val;
4169 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4170 }
4ad4eba5
AM
4171 }
4172
4173 free (dynbuf);
4174 }
4175
4176 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4177 frees all more recently bfd_alloc'd blocks as well. */
4178 if (runpath)
4179 rpath = runpath;
4180
4181 if (rpath)
4182 {
4183 struct bfd_link_needed_list **pn;
66eb6687 4184 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4185 ;
4186 *pn = rpath;
4187 }
4188
9acc85a6
AM
4189 /* If we have a PT_GNU_RELRO program header, mark as read-only
4190 all sections contained fully therein. This makes relro
4191 shared library sections appear as they will at run-time. */
4192 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4193 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4194 if (phdr->p_type == PT_GNU_RELRO)
4195 {
4196 for (s = abfd->sections; s != NULL; s = s->next)
4197 if ((s->flags & SEC_ALLOC) != 0
4198 && s->vma >= phdr->p_vaddr
4199 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4200 s->flags |= SEC_READONLY;
4201 break;
4202 }
4203
4ad4eba5
AM
4204 /* We do not want to include any of the sections in a dynamic
4205 object in the output file. We hack by simply clobbering the
4206 list of sections in the BFD. This could be handled more
4207 cleanly by, say, a new section flag; the existing
4208 SEC_NEVER_LOAD flag is not the one we want, because that one
4209 still implies that the section takes up space in the output
4210 file. */
4211 bfd_section_list_clear (abfd);
4212
4ad4eba5
AM
4213 /* Find the name to use in a DT_NEEDED entry that refers to this
4214 object. If the object has a DT_SONAME entry, we use it.
4215 Otherwise, if the generic linker stuck something in
4216 elf_dt_name, we use that. Otherwise, we just use the file
4217 name. */
4218 if (soname == NULL || *soname == '\0')
4219 {
4220 soname = elf_dt_name (abfd);
4221 if (soname == NULL || *soname == '\0')
4222 soname = bfd_get_filename (abfd);
4223 }
4224
4225 /* Save the SONAME because sometimes the linker emulation code
4226 will need to know it. */
4227 elf_dt_name (abfd) = soname;
4228
7e9f0867 4229 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4230 if (ret < 0)
4231 goto error_return;
4232
4233 /* If we have already included this dynamic object in the
4234 link, just ignore it. There is no reason to include a
4235 particular dynamic object more than once. */
4236 if (ret > 0)
4237 return TRUE;
7ee314fa
AM
4238
4239 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4240 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4241 }
4242
4243 /* If this is a dynamic object, we always link against the .dynsym
4244 symbol table, not the .symtab symbol table. The dynamic linker
4245 will only see the .dynsym symbol table, so there is no reason to
4246 look at .symtab for a dynamic object. */
4247
4248 if (! dynamic || elf_dynsymtab (abfd) == 0)
4249 hdr = &elf_tdata (abfd)->symtab_hdr;
4250 else
4251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4252
4253 symcount = hdr->sh_size / bed->s->sizeof_sym;
4254
4255 /* The sh_info field of the symtab header tells us where the
4256 external symbols start. We don't care about the local symbols at
4257 this point. */
4258 if (elf_bad_symtab (abfd))
4259 {
4260 extsymcount = symcount;
4261 extsymoff = 0;
4262 }
4263 else
4264 {
4265 extsymcount = symcount - hdr->sh_info;
4266 extsymoff = hdr->sh_info;
4267 }
4268
f45794cb 4269 sym_hash = elf_sym_hashes (abfd);
012b2306 4270 if (extsymcount != 0)
4ad4eba5
AM
4271 {
4272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4273 NULL, NULL, NULL);
4274 if (isymbuf == NULL)
4275 goto error_return;
4276
4ad4eba5 4277 if (sym_hash == NULL)
012b2306
AM
4278 {
4279 /* We store a pointer to the hash table entry for each
4280 external symbol. */
ef53be89
AM
4281 amt = extsymcount;
4282 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4283 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4284 if (sym_hash == NULL)
4285 goto error_free_sym;
4286 elf_sym_hashes (abfd) = sym_hash;
4287 }
4ad4eba5
AM
4288 }
4289
4290 if (dynamic)
4291 {
4292 /* Read in any version definitions. */
fc0e6df6
PB
4293 if (!_bfd_elf_slurp_version_tables (abfd,
4294 info->default_imported_symver))
4ad4eba5
AM
4295 goto error_free_sym;
4296
4297 /* Read in the symbol versions, but don't bother to convert them
4298 to internal format. */
4299 if (elf_dynversym (abfd) != 0)
4300 {
4301 Elf_Internal_Shdr *versymhdr;
4302
4303 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
be22c732
NC
4304 amt = versymhdr->sh_size;
4305 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4ad4eba5
AM
4306 if (extversym == NULL)
4307 goto error_free_sym;
4ad4eba5
AM
4308 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4309 || bfd_bread (extversym, amt, abfd) != amt)
4310 goto error_free_vers;
be22c732 4311 extversym_end = extversym + (amt / sizeof (* extversym));
4ad4eba5
AM
4312 }
4313 }
4314
66eb6687
AM
4315 /* If we are loading an as-needed shared lib, save the symbol table
4316 state before we start adding symbols. If the lib turns out
4317 to be unneeded, restore the state. */
4318 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4319 {
4320 unsigned int i;
4321 size_t entsize;
4322
4323 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4324 {
4325 struct bfd_hash_entry *p;
2de92251 4326 struct elf_link_hash_entry *h;
66eb6687
AM
4327
4328 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4329 {
4330 h = (struct elf_link_hash_entry *) p;
4331 entsize += htab->root.table.entsize;
4332 if (h->root.type == bfd_link_hash_warning)
4333 entsize += htab->root.table.entsize;
4334 }
66eb6687
AM
4335 }
4336
4337 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4338 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4339 if (old_tab == NULL)
4340 goto error_free_vers;
4341
4342 /* Remember the current objalloc pointer, so that all mem for
4343 symbols added can later be reclaimed. */
4344 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4345 if (alloc_mark == NULL)
4346 goto error_free_vers;
4347
5061a885
AM
4348 /* Make a special call to the linker "notice" function to
4349 tell it that we are about to handle an as-needed lib. */
e5034e59 4350 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4351 goto error_free_vers;
5061a885 4352
f45794cb
AM
4353 /* Clone the symbol table. Remember some pointers into the
4354 symbol table, and dynamic symbol count. */
4355 old_ent = (char *) old_tab + tabsize;
66eb6687 4356 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4357 old_undefs = htab->root.undefs;
4358 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4359 old_table = htab->root.table.table;
4360 old_size = htab->root.table.size;
4361 old_count = htab->root.table.count;
5b677558
AM
4362 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4363 if (old_strtab == NULL)
4364 goto error_free_vers;
66eb6687
AM
4365
4366 for (i = 0; i < htab->root.table.size; i++)
4367 {
4368 struct bfd_hash_entry *p;
2de92251 4369 struct elf_link_hash_entry *h;
66eb6687
AM
4370
4371 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4372 {
4373 memcpy (old_ent, p, htab->root.table.entsize);
4374 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4375 h = (struct elf_link_hash_entry *) p;
4376 if (h->root.type == bfd_link_hash_warning)
4377 {
4378 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4379 old_ent = (char *) old_ent + htab->root.table.entsize;
4380 }
66eb6687
AM
4381 }
4382 }
4383 }
4ad4eba5 4384
66eb6687 4385 weaks = NULL;
be22c732
NC
4386 if (extversym == NULL)
4387 ever = NULL;
4388 else if (extversym + extsymoff < extversym_end)
4389 ever = extversym + extsymoff;
4390 else
4391 {
4392 /* xgettext:c-format */
4393 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4394 abfd, (long) extsymoff,
4395 (long) (extversym_end - extversym) / sizeof (* extversym));
4396 bfd_set_error (bfd_error_bad_value);
4397 goto error_free_vers;
4398 }
4399
4ad4eba5
AM
4400 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4401 isym < isymend;
4402 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4403 {
4404 int bind;
4405 bfd_vma value;
af44c138 4406 asection *sec, *new_sec;
4ad4eba5
AM
4407 flagword flags;
4408 const char *name;
4409 struct elf_link_hash_entry *h;
90c984fc 4410 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4411 bfd_boolean definition;
4412 bfd_boolean size_change_ok;
4413 bfd_boolean type_change_ok;
37a9e49a
L
4414 bfd_boolean new_weak;
4415 bfd_boolean old_weak;
4ad4eba5 4416 bfd_boolean override;
a4d8e49b 4417 bfd_boolean common;
97196564 4418 bfd_boolean discarded;
4ad4eba5
AM
4419 unsigned int old_alignment;
4420 bfd *old_bfd;
6e33951e 4421 bfd_boolean matched;
4ad4eba5
AM
4422
4423 override = FALSE;
4424
4425 flags = BSF_NO_FLAGS;
4426 sec = NULL;
4427 value = isym->st_value;
a4d8e49b 4428 common = bed->common_definition (isym);
2980ccad
L
4429 if (common && info->inhibit_common_definition)
4430 {
4431 /* Treat common symbol as undefined for --no-define-common. */
4432 isym->st_shndx = SHN_UNDEF;
4433 common = FALSE;
4434 }
97196564 4435 discarded = FALSE;
4ad4eba5
AM
4436
4437 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4438 switch (bind)
4ad4eba5 4439 {
3e7a7d11 4440 case STB_LOCAL:
4ad4eba5
AM
4441 /* This should be impossible, since ELF requires that all
4442 global symbols follow all local symbols, and that sh_info
4443 point to the first global symbol. Unfortunately, Irix 5
4444 screws this up. */
4445 continue;
3e7a7d11
NC
4446
4447 case STB_GLOBAL:
a4d8e49b 4448 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4449 flags = BSF_GLOBAL;
3e7a7d11
NC
4450 break;
4451
4452 case STB_WEAK:
4453 flags = BSF_WEAK;
4454 break;
4455
4456 case STB_GNU_UNIQUE:
4457 flags = BSF_GNU_UNIQUE;
4458 break;
4459
4460 default:
4ad4eba5 4461 /* Leave it up to the processor backend. */
3e7a7d11 4462 break;
4ad4eba5
AM
4463 }
4464
4465 if (isym->st_shndx == SHN_UNDEF)
4466 sec = bfd_und_section_ptr;
cb33740c
AM
4467 else if (isym->st_shndx == SHN_ABS)
4468 sec = bfd_abs_section_ptr;
4469 else if (isym->st_shndx == SHN_COMMON)
4470 {
4471 sec = bfd_com_section_ptr;
4472 /* What ELF calls the size we call the value. What ELF
4473 calls the value we call the alignment. */
4474 value = isym->st_size;
4475 }
4476 else
4ad4eba5
AM
4477 {
4478 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4479 if (sec == NULL)
4480 sec = bfd_abs_section_ptr;
dbaa2011 4481 else if (discarded_section (sec))
529fcb95 4482 {
e5d08002
L
4483 /* Symbols from discarded section are undefined. We keep
4484 its visibility. */
529fcb95 4485 sec = bfd_und_section_ptr;
97196564 4486 discarded = TRUE;
529fcb95
PB
4487 isym->st_shndx = SHN_UNDEF;
4488 }
4ad4eba5
AM
4489 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4490 value -= sec->vma;
4491 }
4ad4eba5
AM
4492
4493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4494 isym->st_name);
4495 if (name == NULL)
4496 goto error_free_vers;
4497
4498 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4499 && (abfd->flags & BFD_PLUGIN) != 0)
4500 {
4501 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4502
4503 if (xc == NULL)
4504 {
4505 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4506 | SEC_EXCLUDE);
4507 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4508 if (xc == NULL)
4509 goto error_free_vers;
4510 }
4511 sec = xc;
4512 }
4513 else if (isym->st_shndx == SHN_COMMON
4514 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4515 && !bfd_link_relocatable (info))
4ad4eba5
AM
4516 {
4517 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4518
4519 if (tcomm == NULL)
4520 {
02d00247
AM
4521 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4522 | SEC_LINKER_CREATED);
4523 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4524 if (tcomm == NULL)
4ad4eba5
AM
4525 goto error_free_vers;
4526 }
4527 sec = tcomm;
4528 }
66eb6687 4529 else if (bed->elf_add_symbol_hook)
4ad4eba5 4530 {
66eb6687
AM
4531 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4532 &sec, &value))
4ad4eba5
AM
4533 goto error_free_vers;
4534
4535 /* The hook function sets the name to NULL if this symbol
4536 should be skipped for some reason. */
4537 if (name == NULL)
4538 continue;
4539 }
4540
4541 /* Sanity check that all possibilities were handled. */
4542 if (sec == NULL)
4543 {
4544 bfd_set_error (bfd_error_bad_value);
4545 goto error_free_vers;
4546 }
4547
191c0c42
AM
4548 /* Silently discard TLS symbols from --just-syms. There's
4549 no way to combine a static TLS block with a new TLS block
4550 for this executable. */
4551 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4552 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4553 continue;
4554
4ad4eba5
AM
4555 if (bfd_is_und_section (sec)
4556 || bfd_is_com_section (sec))
4557 definition = FALSE;
4558 else
4559 definition = TRUE;
4560
4561 size_change_ok = FALSE;
66eb6687 4562 type_change_ok = bed->type_change_ok;
37a9e49a 4563 old_weak = FALSE;
6e33951e 4564 matched = FALSE;
4ad4eba5
AM
4565 old_alignment = 0;
4566 old_bfd = NULL;
af44c138 4567 new_sec = sec;
4ad4eba5 4568
66eb6687 4569 if (is_elf_hash_table (htab))
4ad4eba5
AM
4570 {
4571 Elf_Internal_Versym iver;
4572 unsigned int vernum = 0;
4573 bfd_boolean skip;
4574
fc0e6df6 4575 if (ever == NULL)
4ad4eba5 4576 {
fc0e6df6
PB
4577 if (info->default_imported_symver)
4578 /* Use the default symbol version created earlier. */
4579 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4580 else
4581 iver.vs_vers = 0;
4582 }
be22c732
NC
4583 else if (ever >= extversym_end)
4584 {
4585 /* xgettext:c-format */
4586 _bfd_error_handler (_("%pB: not enough version information"),
4587 abfd);
4588 bfd_set_error (bfd_error_bad_value);
4589 goto error_free_vers;
4590 }
fc0e6df6
PB
4591 else
4592 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4593
4594 vernum = iver.vs_vers & VERSYM_VERSION;
4595
4596 /* If this is a hidden symbol, or if it is not version
4597 1, we append the version name to the symbol name.
cc86ff91
EB
4598 However, we do not modify a non-hidden absolute symbol
4599 if it is not a function, because it might be the version
4600 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4601 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4602 || (vernum > 1
4603 && (!bfd_is_abs_section (sec)
4604 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4605 {
4606 const char *verstr;
4607 size_t namelen, verlen, newlen;
4608 char *newname, *p;
4609
4610 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4611 {
fc0e6df6
PB
4612 if (vernum > elf_tdata (abfd)->cverdefs)
4613 verstr = NULL;
4614 else if (vernum > 1)
4615 verstr =
4616 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4617 else
4618 verstr = "";
4ad4eba5 4619
fc0e6df6 4620 if (verstr == NULL)
4ad4eba5 4621 {
4eca0228 4622 _bfd_error_handler
695344c0 4623 /* xgettext:c-format */
871b3ab2 4624 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4625 abfd, name, vernum,
4626 elf_tdata (abfd)->cverdefs);
4627 bfd_set_error (bfd_error_bad_value);
4628 goto error_free_vers;
4ad4eba5 4629 }
fc0e6df6
PB
4630 }
4631 else
4632 {
4633 /* We cannot simply test for the number of
4634 entries in the VERNEED section since the
4635 numbers for the needed versions do not start
4636 at 0. */
4637 Elf_Internal_Verneed *t;
4638
4639 verstr = NULL;
4640 for (t = elf_tdata (abfd)->verref;
4641 t != NULL;
4642 t = t->vn_nextref)
4ad4eba5 4643 {
fc0e6df6 4644 Elf_Internal_Vernaux *a;
4ad4eba5 4645
fc0e6df6
PB
4646 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4647 {
4648 if (a->vna_other == vernum)
4ad4eba5 4649 {
fc0e6df6
PB
4650 verstr = a->vna_nodename;
4651 break;
4ad4eba5 4652 }
4ad4eba5 4653 }
fc0e6df6
PB
4654 if (a != NULL)
4655 break;
4656 }
4657 if (verstr == NULL)
4658 {
4eca0228 4659 _bfd_error_handler
695344c0 4660 /* xgettext:c-format */
871b3ab2 4661 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4662 abfd, name, vernum);
4663 bfd_set_error (bfd_error_bad_value);
4664 goto error_free_vers;
4ad4eba5 4665 }
4ad4eba5 4666 }
fc0e6df6
PB
4667
4668 namelen = strlen (name);
4669 verlen = strlen (verstr);
4670 newlen = namelen + verlen + 2;
4671 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4672 && isym->st_shndx != SHN_UNDEF)
4673 ++newlen;
4674
a50b1753 4675 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4676 if (newname == NULL)
4677 goto error_free_vers;
4678 memcpy (newname, name, namelen);
4679 p = newname + namelen;
4680 *p++ = ELF_VER_CHR;
4681 /* If this is a defined non-hidden version symbol,
4682 we add another @ to the name. This indicates the
4683 default version of the symbol. */
4684 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4685 && isym->st_shndx != SHN_UNDEF)
4686 *p++ = ELF_VER_CHR;
4687 memcpy (p, verstr, verlen + 1);
4688
4689 name = newname;
4ad4eba5
AM
4690 }
4691
cd3416da
AM
4692 /* If this symbol has default visibility and the user has
4693 requested we not re-export it, then mark it as hidden. */
a0d49154 4694 if (!bfd_is_und_section (sec)
cd3416da 4695 && !dynamic
ce875075 4696 && abfd->no_export
cd3416da
AM
4697 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4698 isym->st_other = (STV_HIDDEN
4699 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4700
4f3fedcf
AM
4701 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4702 sym_hash, &old_bfd, &old_weak,
4703 &old_alignment, &skip, &override,
6e33951e
L
4704 &type_change_ok, &size_change_ok,
4705 &matched))
4ad4eba5
AM
4706 goto error_free_vers;
4707
4708 if (skip)
4709 continue;
4710
6e33951e
L
4711 /* Override a definition only if the new symbol matches the
4712 existing one. */
4713 if (override && matched)
4ad4eba5
AM
4714 definition = FALSE;
4715
4716 h = *sym_hash;
4717 while (h->root.type == bfd_link_hash_indirect
4718 || h->root.type == bfd_link_hash_warning)
4719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4720
4ad4eba5 4721 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4722 && vernum > 1
4723 && definition)
4724 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4725 }
4726
4727 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4728 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4729 (struct bfd_link_hash_entry **) sym_hash)))
4730 goto error_free_vers;
4731
ac98f9e2
L
4732 if ((abfd->flags & DYNAMIC) == 0
4733 && (bfd_get_flavour (info->output_bfd)
4734 == bfd_target_elf_flavour))
4735 {
4736 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4737 elf_tdata (info->output_bfd)->has_gnu_symbols
4738 |= elf_gnu_symbol_ifunc;
4739 if ((flags & BSF_GNU_UNIQUE))
4740 elf_tdata (info->output_bfd)->has_gnu_symbols
4741 |= elf_gnu_symbol_unique;
4742 }
a43942db 4743
4ad4eba5 4744 h = *sym_hash;
90c984fc
L
4745 /* We need to make sure that indirect symbol dynamic flags are
4746 updated. */
4747 hi = h;
4ad4eba5
AM
4748 while (h->root.type == bfd_link_hash_indirect
4749 || h->root.type == bfd_link_hash_warning)
4750 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4751
97196564
L
4752 /* Setting the index to -3 tells elf_link_output_extsym that
4753 this symbol is defined in a discarded section. */
4754 if (discarded)
4755 h->indx = -3;
4756
4ad4eba5
AM
4757 *sym_hash = h;
4758
37a9e49a 4759 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4760 if (dynamic
4761 && definition
37a9e49a 4762 && new_weak
fcb93ecf 4763 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4764 && is_elf_hash_table (htab)
60d67dc8 4765 && h->u.alias == NULL)
4ad4eba5
AM
4766 {
4767 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4768 a dynamic object, using the alias field. Later in this
4769 function we will set the alias field to the correct
4ad4eba5
AM
4770 value. We only put non-function symbols from dynamic
4771 objects on this list, because that happens to be the only
4772 time we need to know the normal symbol corresponding to a
4773 weak symbol, and the information is time consuming to
60d67dc8 4774 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4775 then this symbol was already defined by some previous
4776 dynamic object, and we will be using that previous
4777 definition anyhow. */
4778
60d67dc8 4779 h->u.alias = weaks;
4ad4eba5 4780 weaks = h;
4ad4eba5
AM
4781 }
4782
4783 /* Set the alignment of a common symbol. */
a4d8e49b 4784 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4785 && h->root.type == bfd_link_hash_common)
4786 {
4787 unsigned int align;
4788
a4d8e49b 4789 if (common)
af44c138
L
4790 align = bfd_log2 (isym->st_value);
4791 else
4792 {
4793 /* The new symbol is a common symbol in a shared object.
4794 We need to get the alignment from the section. */
4795 align = new_sec->alignment_power;
4796 }
595213d4 4797 if (align > old_alignment)
4ad4eba5
AM
4798 h->root.u.c.p->alignment_power = align;
4799 else
4800 h->root.u.c.p->alignment_power = old_alignment;
4801 }
4802
66eb6687 4803 if (is_elf_hash_table (htab))
4ad4eba5 4804 {
4f3fedcf
AM
4805 /* Set a flag in the hash table entry indicating the type of
4806 reference or definition we just found. A dynamic symbol
4807 is one which is referenced or defined by both a regular
4808 object and a shared object. */
4809 bfd_boolean dynsym = FALSE;
4810
4811 /* Plugin symbols aren't normal. Don't set def_regular or
4812 ref_regular for them, or make them dynamic. */
4813 if ((abfd->flags & BFD_PLUGIN) != 0)
4814 ;
4815 else if (! dynamic)
4816 {
4817 if (! definition)
4818 {
4819 h->ref_regular = 1;
4820 if (bind != STB_WEAK)
4821 h->ref_regular_nonweak = 1;
4822 }
4823 else
4824 {
4825 h->def_regular = 1;
4826 if (h->def_dynamic)
4827 {
4828 h->def_dynamic = 0;
4829 h->ref_dynamic = 1;
4830 }
4831 }
4832
4833 /* If the indirect symbol has been forced local, don't
4834 make the real symbol dynamic. */
4835 if ((h == hi || !hi->forced_local)
0e1862bb 4836 && (bfd_link_dll (info)
4f3fedcf
AM
4837 || h->def_dynamic
4838 || h->ref_dynamic))
4839 dynsym = TRUE;
4840 }
4841 else
4842 {
4843 if (! definition)
4844 {
4845 h->ref_dynamic = 1;
4846 hi->ref_dynamic = 1;
4847 }
4848 else
4849 {
4850 h->def_dynamic = 1;
4851 hi->def_dynamic = 1;
4852 }
4853
4854 /* If the indirect symbol has been forced local, don't
4855 make the real symbol dynamic. */
4856 if ((h == hi || !hi->forced_local)
4857 && (h->def_regular
4858 || h->ref_regular
60d67dc8
AM
4859 || (h->is_weakalias
4860 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4861 dynsym = TRUE;
4862 }
4863
4864 /* Check to see if we need to add an indirect symbol for
4865 the default name. */
4866 if (definition
4867 || (!override && h->root.type == bfd_link_hash_common))
4868 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4869 sec, value, &old_bfd, &dynsym))
4870 goto error_free_vers;
4ad4eba5
AM
4871
4872 /* Check the alignment when a common symbol is involved. This
4873 can change when a common symbol is overridden by a normal
4874 definition or a common symbol is ignored due to the old
4875 normal definition. We need to make sure the maximum
4876 alignment is maintained. */
a4d8e49b 4877 if ((old_alignment || common)
4ad4eba5
AM
4878 && h->root.type != bfd_link_hash_common)
4879 {
4880 unsigned int common_align;
4881 unsigned int normal_align;
4882 unsigned int symbol_align;
4883 bfd *normal_bfd;
4884 bfd *common_bfd;
4885
3a81e825
AM
4886 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4887 || h->root.type == bfd_link_hash_defweak);
4888
4ad4eba5
AM
4889 symbol_align = ffs (h->root.u.def.value) - 1;
4890 if (h->root.u.def.section->owner != NULL
0616a280
AM
4891 && (h->root.u.def.section->owner->flags
4892 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4893 {
4894 normal_align = h->root.u.def.section->alignment_power;
4895 if (normal_align > symbol_align)
4896 normal_align = symbol_align;
4897 }
4898 else
4899 normal_align = symbol_align;
4900
4901 if (old_alignment)
4902 {
4903 common_align = old_alignment;
4904 common_bfd = old_bfd;
4905 normal_bfd = abfd;
4906 }
4907 else
4908 {
4909 common_align = bfd_log2 (isym->st_value);
4910 common_bfd = abfd;
4911 normal_bfd = old_bfd;
4912 }
4913
4914 if (normal_align < common_align)
d07676f8
NC
4915 {
4916 /* PR binutils/2735 */
4917 if (normal_bfd == NULL)
4eca0228 4918 _bfd_error_handler
695344c0 4919 /* xgettext:c-format */
9793eb77 4920 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4921 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4922 1 << common_align, name, common_bfd,
4923 1 << normal_align, h->root.u.def.section);
d07676f8 4924 else
4eca0228 4925 _bfd_error_handler
695344c0 4926 /* xgettext:c-format */
9793eb77 4927 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4928 " is smaller than %u in %pB"),
c08bb8dd
AM
4929 1 << normal_align, name, normal_bfd,
4930 1 << common_align, common_bfd);
d07676f8 4931 }
4ad4eba5
AM
4932 }
4933
83ad0046 4934 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4935 if (isym->st_size != 0
4936 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4937 && (definition || h->size == 0))
4938 {
83ad0046
L
4939 if (h->size != 0
4940 && h->size != isym->st_size
4941 && ! size_change_ok)
4eca0228 4942 _bfd_error_handler
695344c0 4943 /* xgettext:c-format */
9793eb77 4944 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4945 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4946 name, (uint64_t) h->size, old_bfd,
4947 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4948
4949 h->size = isym->st_size;
4950 }
4951
4952 /* If this is a common symbol, then we always want H->SIZE
4953 to be the size of the common symbol. The code just above
4954 won't fix the size if a common symbol becomes larger. We
4955 don't warn about a size change here, because that is
4f3fedcf 4956 covered by --warn-common. Allow changes between different
fcb93ecf 4957 function types. */
4ad4eba5
AM
4958 if (h->root.type == bfd_link_hash_common)
4959 h->size = h->root.u.c.size;
4960
4961 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4962 && ((definition && !new_weak)
4963 || (old_weak && h->root.type == bfd_link_hash_common)
4964 || h->type == STT_NOTYPE))
4ad4eba5 4965 {
2955ec4c
L
4966 unsigned int type = ELF_ST_TYPE (isym->st_info);
4967
4968 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4969 symbol. */
4970 if (type == STT_GNU_IFUNC
4971 && (abfd->flags & DYNAMIC) != 0)
4972 type = STT_FUNC;
4ad4eba5 4973
2955ec4c
L
4974 if (h->type != type)
4975 {
4976 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4977 /* xgettext:c-format */
4eca0228 4978 _bfd_error_handler
9793eb77 4979 (_("warning: type of symbol `%s' changed"
871b3ab2 4980 " from %d to %d in %pB"),
c08bb8dd 4981 name, h->type, type, abfd);
2955ec4c
L
4982
4983 h->type = type;
4984 }
4ad4eba5
AM
4985 }
4986
54ac0771 4987 /* Merge st_other field. */
b8417128 4988 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4989
c3df8c14 4990 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4991 if (definition
4992 && (sec->flags & SEC_DEBUGGING)
4993 && !bfd_link_relocatable (info))
c3df8c14
AM
4994 dynsym = FALSE;
4995
4f3fedcf
AM
4996 /* Nor should we make plugin symbols dynamic. */
4997 if ((abfd->flags & BFD_PLUGIN) != 0)
4998 dynsym = FALSE;
4999
35fc36a8 5000 if (definition)
35399224
L
5001 {
5002 h->target_internal = isym->st_target_internal;
5003 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5004 }
35fc36a8 5005
4ad4eba5
AM
5006 if (definition && !dynamic)
5007 {
5008 char *p = strchr (name, ELF_VER_CHR);
5009 if (p != NULL && p[1] != ELF_VER_CHR)
5010 {
5011 /* Queue non-default versions so that .symver x, x@FOO
5012 aliases can be checked. */
66eb6687 5013 if (!nondeflt_vers)
4ad4eba5 5014 {
66eb6687
AM
5015 amt = ((isymend - isym + 1)
5016 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5017 nondeflt_vers
5018 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5019 if (!nondeflt_vers)
5020 goto error_free_vers;
4ad4eba5 5021 }
66eb6687 5022 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5023 }
5024 }
5025
5026 if (dynsym && h->dynindx == -1)
5027 {
c152c796 5028 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5029 goto error_free_vers;
60d67dc8
AM
5030 if (h->is_weakalias
5031 && weakdef (h)->dynindx == -1)
4ad4eba5 5032 {
60d67dc8 5033 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5034 goto error_free_vers;
5035 }
5036 }
1f599d0e 5037 else if (h->dynindx != -1)
4ad4eba5
AM
5038 /* If the symbol already has a dynamic index, but
5039 visibility says it should not be visible, turn it into
5040 a local symbol. */
5041 switch (ELF_ST_VISIBILITY (h->other))
5042 {
5043 case STV_INTERNAL:
5044 case STV_HIDDEN:
5045 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5046 dynsym = FALSE;
5047 break;
5048 }
5049
aef28989
L
5050 /* Don't add DT_NEEDED for references from the dummy bfd nor
5051 for unmatched symbol. */
4ad4eba5 5052 if (!add_needed
aef28989 5053 && matched
4ad4eba5 5054 && definition
010e5ae2 5055 && ((dynsym
ffa9430d 5056 && h->ref_regular_nonweak
4f3fedcf
AM
5057 && (old_bfd == NULL
5058 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5059 || (h->ref_dynamic_nonweak
010e5ae2 5060 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5061 && !on_needed_list (elf_dt_name (abfd),
5062 htab->needed, NULL))))
4ad4eba5
AM
5063 {
5064 int ret;
5065 const char *soname = elf_dt_name (abfd);
5066
16e4ecc0
AM
5067 info->callbacks->minfo ("%!", soname, old_bfd,
5068 h->root.root.string);
5069
4ad4eba5
AM
5070 /* A symbol from a library loaded via DT_NEEDED of some
5071 other library is referenced by a regular object.
e56f61be 5072 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5073 --no-add-needed is used and the reference was not
5074 a weak one. */
4f3fedcf 5075 if (old_bfd != NULL
b918acf9 5076 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5077 {
4eca0228 5078 _bfd_error_handler
695344c0 5079 /* xgettext:c-format */
871b3ab2 5080 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5081 old_bfd, name);
ff5ac77b 5082 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5083 goto error_free_vers;
5084 }
5085
a50b1753 5086 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5087 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5088
4ad4eba5 5089 add_needed = TRUE;
7e9f0867 5090 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5091 if (ret < 0)
5092 goto error_free_vers;
5093
5094 BFD_ASSERT (ret == 0);
5095 }
5096 }
5097 }
5098
a83ef4d1
L
5099 if (info->lto_plugin_active
5100 && !bfd_link_relocatable (info)
5101 && (abfd->flags & BFD_PLUGIN) == 0
5102 && !just_syms
5103 && extsymcount)
5104 {
5105 int r_sym_shift;
5106
5107 if (bed->s->arch_size == 32)
5108 r_sym_shift = 8;
5109 else
5110 r_sym_shift = 32;
5111
5112 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5113 referenced in regular objects so that linker plugin will get
5114 the correct symbol resolution. */
5115
5116 sym_hash = elf_sym_hashes (abfd);
5117 for (s = abfd->sections; s != NULL; s = s->next)
5118 {
5119 Elf_Internal_Rela *internal_relocs;
5120 Elf_Internal_Rela *rel, *relend;
5121
5122 /* Don't check relocations in excluded sections. */
5123 if ((s->flags & SEC_RELOC) == 0
5124 || s->reloc_count == 0
5125 || (s->flags & SEC_EXCLUDE) != 0
5126 || ((info->strip == strip_all
5127 || info->strip == strip_debugger)
5128 && (s->flags & SEC_DEBUGGING) != 0))
5129 continue;
5130
5131 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5132 NULL,
5133 info->keep_memory);
5134 if (internal_relocs == NULL)
5135 goto error_free_vers;
5136
5137 rel = internal_relocs;
5138 relend = rel + s->reloc_count;
5139 for ( ; rel < relend; rel++)
5140 {
5141 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5142 struct elf_link_hash_entry *h;
5143
5144 /* Skip local symbols. */
5145 if (r_symndx < extsymoff)
5146 continue;
5147
5148 h = sym_hash[r_symndx - extsymoff];
5149 if (h != NULL)
5150 h->root.non_ir_ref_regular = 1;
5151 }
5152
5153 if (elf_section_data (s)->relocs != internal_relocs)
5154 free (internal_relocs);
5155 }
5156 }
5157
66eb6687
AM
5158 if (extversym != NULL)
5159 {
5160 free (extversym);
5161 extversym = NULL;
5162 }
5163
5164 if (isymbuf != NULL)
5165 {
5166 free (isymbuf);
5167 isymbuf = NULL;
5168 }
5169
5170 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5171 {
5172 unsigned int i;
5173
5174 /* Restore the symbol table. */
f45794cb
AM
5175 old_ent = (char *) old_tab + tabsize;
5176 memset (elf_sym_hashes (abfd), 0,
5177 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5178 htab->root.table.table = old_table;
5179 htab->root.table.size = old_size;
5180 htab->root.table.count = old_count;
66eb6687 5181 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5182 htab->root.undefs = old_undefs;
5183 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5184 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5185 free (old_strtab);
5186 old_strtab = NULL;
66eb6687
AM
5187 for (i = 0; i < htab->root.table.size; i++)
5188 {
5189 struct bfd_hash_entry *p;
5190 struct elf_link_hash_entry *h;
3e0882af
L
5191 bfd_size_type size;
5192 unsigned int alignment_power;
4070765b 5193 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5194
5195 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5196 {
5197 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5198 if (h->root.type == bfd_link_hash_warning)
5199 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5200
3e0882af
L
5201 /* Preserve the maximum alignment and size for common
5202 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5203 since it can still be loaded at run time by another
3e0882af
L
5204 dynamic lib. */
5205 if (h->root.type == bfd_link_hash_common)
5206 {
5207 size = h->root.u.c.size;
5208 alignment_power = h->root.u.c.p->alignment_power;
5209 }
5210 else
5211 {
5212 size = 0;
5213 alignment_power = 0;
5214 }
4070765b 5215 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5216 will be exported when the dynamic lib becomes needed
5217 in the second pass. */
4070765b 5218 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5219 memcpy (p, old_ent, htab->root.table.entsize);
5220 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5221 h = (struct elf_link_hash_entry *) p;
5222 if (h->root.type == bfd_link_hash_warning)
5223 {
5224 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5225 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5227 }
a4542f1b 5228 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5229 {
5230 if (size > h->root.u.c.size)
5231 h->root.u.c.size = size;
5232 if (alignment_power > h->root.u.c.p->alignment_power)
5233 h->root.u.c.p->alignment_power = alignment_power;
5234 }
4070765b 5235 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5236 }
5237 }
5238
5061a885
AM
5239 /* Make a special call to the linker "notice" function to
5240 tell it that symbols added for crefs may need to be removed. */
e5034e59 5241 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5242 goto error_free_vers;
5061a885 5243
66eb6687
AM
5244 free (old_tab);
5245 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5246 alloc_mark);
5247 if (nondeflt_vers != NULL)
5248 free (nondeflt_vers);
5249 return TRUE;
5250 }
2de92251 5251
66eb6687
AM
5252 if (old_tab != NULL)
5253 {
e5034e59 5254 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5255 goto error_free_vers;
66eb6687
AM
5256 free (old_tab);
5257 old_tab = NULL;
5258 }
5259
c6e8a9a8
L
5260 /* Now that all the symbols from this input file are created, if
5261 not performing a relocatable link, handle .symver foo, foo@BAR
5262 such that any relocs against foo become foo@BAR. */
0e1862bb 5263 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5264 {
ef53be89 5265 size_t cnt, symidx;
4ad4eba5
AM
5266
5267 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5268 {
5269 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5270 char *shortname, *p;
5271
5272 p = strchr (h->root.root.string, ELF_VER_CHR);
5273 if (p == NULL
5274 || (h->root.type != bfd_link_hash_defined
5275 && h->root.type != bfd_link_hash_defweak))
5276 continue;
5277
5278 amt = p - h->root.root.string;
a50b1753 5279 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5280 if (!shortname)
5281 goto error_free_vers;
4ad4eba5
AM
5282 memcpy (shortname, h->root.root.string, amt);
5283 shortname[amt] = '\0';
5284
5285 hi = (struct elf_link_hash_entry *)
66eb6687 5286 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5287 FALSE, FALSE, FALSE);
5288 if (hi != NULL
5289 && hi->root.type == h->root.type
5290 && hi->root.u.def.value == h->root.u.def.value
5291 && hi->root.u.def.section == h->root.u.def.section)
5292 {
5293 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5294 hi->root.type = bfd_link_hash_indirect;
5295 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5296 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5297 sym_hash = elf_sym_hashes (abfd);
5298 if (sym_hash)
5299 for (symidx = 0; symidx < extsymcount; ++symidx)
5300 if (sym_hash[symidx] == hi)
5301 {
5302 sym_hash[symidx] = h;
5303 break;
5304 }
5305 }
5306 free (shortname);
5307 }
5308 free (nondeflt_vers);
5309 nondeflt_vers = NULL;
5310 }
5311
60d67dc8 5312 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5313 symbols we found. The only way to do this is to search all the
5314 symbols. Since we only need the information for non functions in
5315 dynamic objects, that's the only time we actually put anything on
5316 the list WEAKS. We need this information so that if a regular
5317 object refers to a symbol defined weakly in a dynamic object, the
5318 real symbol in the dynamic object is also put in the dynamic
5319 symbols; we also must arrange for both symbols to point to the
5320 same memory location. We could handle the general case of symbol
5321 aliasing, but a general symbol alias can only be generated in
5322 assembler code, handling it correctly would be very time
5323 consuming, and other ELF linkers don't handle general aliasing
5324 either. */
5325 if (weaks != NULL)
5326 {
5327 struct elf_link_hash_entry **hpp;
5328 struct elf_link_hash_entry **hppend;
5329 struct elf_link_hash_entry **sorted_sym_hash;
5330 struct elf_link_hash_entry *h;
5331 size_t sym_count;
5332
5333 /* Since we have to search the whole symbol list for each weak
5334 defined symbol, search time for N weak defined symbols will be
5335 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5336 amt = extsymcount;
5337 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5338 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5339 if (sorted_sym_hash == NULL)
5340 goto error_return;
5341 sym_hash = sorted_sym_hash;
5342 hpp = elf_sym_hashes (abfd);
5343 hppend = hpp + extsymcount;
5344 sym_count = 0;
5345 for (; hpp < hppend; hpp++)
5346 {
5347 h = *hpp;
5348 if (h != NULL
5349 && h->root.type == bfd_link_hash_defined
fcb93ecf 5350 && !bed->is_function_type (h->type))
4ad4eba5
AM
5351 {
5352 *sym_hash = h;
5353 sym_hash++;
5354 sym_count++;
5355 }
5356 }
5357
5358 qsort (sorted_sym_hash, sym_count,
5359 sizeof (struct elf_link_hash_entry *),
5360 elf_sort_symbol);
5361
5362 while (weaks != NULL)
5363 {
5364 struct elf_link_hash_entry *hlook;
5365 asection *slook;
5366 bfd_vma vlook;
ed54588d 5367 size_t i, j, idx = 0;
4ad4eba5
AM
5368
5369 hlook = weaks;
60d67dc8
AM
5370 weaks = hlook->u.alias;
5371 hlook->u.alias = NULL;
4ad4eba5 5372
e3e53eed
AM
5373 if (hlook->root.type != bfd_link_hash_defined
5374 && hlook->root.type != bfd_link_hash_defweak)
5375 continue;
5376
4ad4eba5
AM
5377 slook = hlook->root.u.def.section;
5378 vlook = hlook->root.u.def.value;
5379
4ad4eba5
AM
5380 i = 0;
5381 j = sym_count;
14160578 5382 while (i != j)
4ad4eba5
AM
5383 {
5384 bfd_signed_vma vdiff;
5385 idx = (i + j) / 2;
14160578 5386 h = sorted_sym_hash[idx];
4ad4eba5
AM
5387 vdiff = vlook - h->root.u.def.value;
5388 if (vdiff < 0)
5389 j = idx;
5390 else if (vdiff > 0)
5391 i = idx + 1;
5392 else
5393 {
d3435ae8 5394 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5395 if (sdiff < 0)
5396 j = idx;
5397 else if (sdiff > 0)
5398 i = idx + 1;
5399 else
14160578 5400 break;
4ad4eba5
AM
5401 }
5402 }
5403
5404 /* We didn't find a value/section match. */
14160578 5405 if (i == j)
4ad4eba5
AM
5406 continue;
5407
14160578
AM
5408 /* With multiple aliases, or when the weak symbol is already
5409 strongly defined, we have multiple matching symbols and
5410 the binary search above may land on any of them. Step
5411 one past the matching symbol(s). */
5412 while (++idx != j)
5413 {
5414 h = sorted_sym_hash[idx];
5415 if (h->root.u.def.section != slook
5416 || h->root.u.def.value != vlook)
5417 break;
5418 }
5419
5420 /* Now look back over the aliases. Since we sorted by size
5421 as well as value and section, we'll choose the one with
5422 the largest size. */
5423 while (idx-- != i)
4ad4eba5 5424 {
14160578 5425 h = sorted_sym_hash[idx];
4ad4eba5
AM
5426
5427 /* Stop if value or section doesn't match. */
14160578
AM
5428 if (h->root.u.def.section != slook
5429 || h->root.u.def.value != vlook)
4ad4eba5
AM
5430 break;
5431 else if (h != hlook)
5432 {
60d67dc8
AM
5433 struct elf_link_hash_entry *t;
5434
5435 hlook->u.alias = h;
5436 hlook->is_weakalias = 1;
5437 t = h;
5438 if (t->u.alias != NULL)
5439 while (t->u.alias != h)
5440 t = t->u.alias;
5441 t->u.alias = hlook;
4ad4eba5
AM
5442
5443 /* If the weak definition is in the list of dynamic
5444 symbols, make sure the real definition is put
5445 there as well. */
5446 if (hlook->dynindx != -1 && h->dynindx == -1)
5447 {
c152c796 5448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5449 {
5450 err_free_sym_hash:
5451 free (sorted_sym_hash);
5452 goto error_return;
5453 }
4ad4eba5
AM
5454 }
5455
5456 /* If the real definition is in the list of dynamic
5457 symbols, make sure the weak definition is put
5458 there as well. If we don't do this, then the
5459 dynamic loader might not merge the entries for the
5460 real definition and the weak definition. */
5461 if (h->dynindx != -1 && hlook->dynindx == -1)
5462 {
c152c796 5463 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5464 goto err_free_sym_hash;
4ad4eba5
AM
5465 }
5466 break;
5467 }
5468 }
5469 }
5470
5471 free (sorted_sym_hash);
5472 }
5473
33177bb1
AM
5474 if (bed->check_directives
5475 && !(*bed->check_directives) (abfd, info))
5476 return FALSE;
85fbca6a 5477
4ad4eba5
AM
5478 /* If this is a non-traditional link, try to optimize the handling
5479 of the .stab/.stabstr sections. */
5480 if (! dynamic
5481 && ! info->traditional_format
66eb6687 5482 && is_elf_hash_table (htab)
4ad4eba5
AM
5483 && (info->strip != strip_all && info->strip != strip_debugger))
5484 {
5485 asection *stabstr;
5486
5487 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5488 if (stabstr != NULL)
5489 {
5490 bfd_size_type string_offset = 0;
5491 asection *stab;
5492
5493 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5494 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5495 && (!stab->name[5] ||
5496 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5497 && (stab->flags & SEC_MERGE) == 0
5498 && !bfd_is_abs_section (stab->output_section))
5499 {
5500 struct bfd_elf_section_data *secdata;
5501
5502 secdata = elf_section_data (stab);
66eb6687
AM
5503 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5504 stabstr, &secdata->sec_info,
4ad4eba5
AM
5505 &string_offset))
5506 goto error_return;
5507 if (secdata->sec_info)
dbaa2011 5508 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5509 }
5510 }
5511 }
5512
66eb6687 5513 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5514 {
5515 /* Add this bfd to the loaded list. */
5516 struct elf_link_loaded_list *n;
5517
ca4be51c 5518 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5519 if (n == NULL)
5520 goto error_return;
5521 n->abfd = abfd;
66eb6687
AM
5522 n->next = htab->loaded;
5523 htab->loaded = n;
4ad4eba5
AM
5524 }
5525
5526 return TRUE;
5527
5528 error_free_vers:
66eb6687
AM
5529 if (old_tab != NULL)
5530 free (old_tab);
5b677558
AM
5531 if (old_strtab != NULL)
5532 free (old_strtab);
4ad4eba5
AM
5533 if (nondeflt_vers != NULL)
5534 free (nondeflt_vers);
5535 if (extversym != NULL)
5536 free (extversym);
5537 error_free_sym:
5538 if (isymbuf != NULL)
5539 free (isymbuf);
5540 error_return:
5541 return FALSE;
5542}
5543
8387904d
AM
5544/* Return the linker hash table entry of a symbol that might be
5545 satisfied by an archive symbol. Return -1 on error. */
5546
5547struct elf_link_hash_entry *
5548_bfd_elf_archive_symbol_lookup (bfd *abfd,
5549 struct bfd_link_info *info,
5550 const char *name)
5551{
5552 struct elf_link_hash_entry *h;
5553 char *p, *copy;
5554 size_t len, first;
5555
2a41f396 5556 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5557 if (h != NULL)
5558 return h;
5559
5560 /* If this is a default version (the name contains @@), look up the
5561 symbol again with only one `@' as well as without the version.
5562 The effect is that references to the symbol with and without the
5563 version will be matched by the default symbol in the archive. */
5564
5565 p = strchr (name, ELF_VER_CHR);
5566 if (p == NULL || p[1] != ELF_VER_CHR)
5567 return h;
5568
5569 /* First check with only one `@'. */
5570 len = strlen (name);
a50b1753 5571 copy = (char *) bfd_alloc (abfd, len);
8387904d 5572 if (copy == NULL)
e99955cd 5573 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5574
5575 first = p - name + 1;
5576 memcpy (copy, name, first);
5577 memcpy (copy + first, name + first + 1, len - first);
5578
2a41f396 5579 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5580 if (h == NULL)
5581 {
5582 /* We also need to check references to the symbol without the
5583 version. */
5584 copy[first - 1] = '\0';
5585 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5586 FALSE, FALSE, TRUE);
8387904d
AM
5587 }
5588
5589 bfd_release (abfd, copy);
5590 return h;
5591}
5592
0ad989f9 5593/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5594 don't use _bfd_generic_link_add_archive_symbols because we need to
5595 handle versioned symbols.
0ad989f9
L
5596
5597 Fortunately, ELF archive handling is simpler than that done by
5598 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5599 oddities. In ELF, if we find a symbol in the archive map, and the
5600 symbol is currently undefined, we know that we must pull in that
5601 object file.
5602
5603 Unfortunately, we do have to make multiple passes over the symbol
5604 table until nothing further is resolved. */
5605
4ad4eba5
AM
5606static bfd_boolean
5607elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5608{
5609 symindex c;
13e570f8 5610 unsigned char *included = NULL;
0ad989f9
L
5611 carsym *symdefs;
5612 bfd_boolean loop;
5613 bfd_size_type amt;
8387904d
AM
5614 const struct elf_backend_data *bed;
5615 struct elf_link_hash_entry * (*archive_symbol_lookup)
5616 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5617
5618 if (! bfd_has_map (abfd))
5619 {
5620 /* An empty archive is a special case. */
5621 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5622 return TRUE;
5623 bfd_set_error (bfd_error_no_armap);
5624 return FALSE;
5625 }
5626
5627 /* Keep track of all symbols we know to be already defined, and all
5628 files we know to be already included. This is to speed up the
5629 second and subsequent passes. */
5630 c = bfd_ardata (abfd)->symdef_count;
5631 if (c == 0)
5632 return TRUE;
5633 amt = c;
13e570f8
AM
5634 amt *= sizeof (*included);
5635 included = (unsigned char *) bfd_zmalloc (amt);
5636 if (included == NULL)
5637 return FALSE;
0ad989f9
L
5638
5639 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5640 bed = get_elf_backend_data (abfd);
5641 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5642
5643 do
5644 {
5645 file_ptr last;
5646 symindex i;
5647 carsym *symdef;
5648 carsym *symdefend;
5649
5650 loop = FALSE;
5651 last = -1;
5652
5653 symdef = symdefs;
5654 symdefend = symdef + c;
5655 for (i = 0; symdef < symdefend; symdef++, i++)
5656 {
5657 struct elf_link_hash_entry *h;
5658 bfd *element;
5659 struct bfd_link_hash_entry *undefs_tail;
5660 symindex mark;
5661
13e570f8 5662 if (included[i])
0ad989f9
L
5663 continue;
5664 if (symdef->file_offset == last)
5665 {
5666 included[i] = TRUE;
5667 continue;
5668 }
5669
8387904d 5670 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5671 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5672 goto error_return;
0ad989f9
L
5673
5674 if (h == NULL)
5675 continue;
5676
5677 if (h->root.type == bfd_link_hash_common)
5678 {
5679 /* We currently have a common symbol. The archive map contains
5680 a reference to this symbol, so we may want to include it. We
5681 only want to include it however, if this archive element
5682 contains a definition of the symbol, not just another common
5683 declaration of it.
5684
5685 Unfortunately some archivers (including GNU ar) will put
5686 declarations of common symbols into their archive maps, as
5687 well as real definitions, so we cannot just go by the archive
5688 map alone. Instead we must read in the element's symbol
5689 table and check that to see what kind of symbol definition
5690 this is. */
5691 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5692 continue;
5693 }
5694 else if (h->root.type != bfd_link_hash_undefined)
5695 {
5696 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5697 /* Symbol must be defined. Don't check it again. */
5698 included[i] = TRUE;
0ad989f9
L
5699 continue;
5700 }
5701
5702 /* We need to include this archive member. */
5703 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5704 if (element == NULL)
5705 goto error_return;
5706
5707 if (! bfd_check_format (element, bfd_object))
5708 goto error_return;
5709
0ad989f9
L
5710 undefs_tail = info->hash->undefs_tail;
5711
0e144ba7
AM
5712 if (!(*info->callbacks
5713 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5714 continue;
0e144ba7 5715 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5716 goto error_return;
5717
5718 /* If there are any new undefined symbols, we need to make
5719 another pass through the archive in order to see whether
5720 they can be defined. FIXME: This isn't perfect, because
5721 common symbols wind up on undefs_tail and because an
5722 undefined symbol which is defined later on in this pass
5723 does not require another pass. This isn't a bug, but it
5724 does make the code less efficient than it could be. */
5725 if (undefs_tail != info->hash->undefs_tail)
5726 loop = TRUE;
5727
5728 /* Look backward to mark all symbols from this object file
5729 which we have already seen in this pass. */
5730 mark = i;
5731 do
5732 {
5733 included[mark] = TRUE;
5734 if (mark == 0)
5735 break;
5736 --mark;
5737 }
5738 while (symdefs[mark].file_offset == symdef->file_offset);
5739
5740 /* We mark subsequent symbols from this object file as we go
5741 on through the loop. */
5742 last = symdef->file_offset;
5743 }
5744 }
5745 while (loop);
5746
0ad989f9
L
5747 free (included);
5748
5749 return TRUE;
5750
5751 error_return:
0ad989f9
L
5752 if (included != NULL)
5753 free (included);
5754 return FALSE;
5755}
4ad4eba5
AM
5756
5757/* Given an ELF BFD, add symbols to the global hash table as
5758 appropriate. */
5759
5760bfd_boolean
5761bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5762{
5763 switch (bfd_get_format (abfd))
5764 {
5765 case bfd_object:
5766 return elf_link_add_object_symbols (abfd, info);
5767 case bfd_archive:
5768 return elf_link_add_archive_symbols (abfd, info);
5769 default:
5770 bfd_set_error (bfd_error_wrong_format);
5771 return FALSE;
5772 }
5773}
5a580b3a 5774\f
14b1c01e
AM
5775struct hash_codes_info
5776{
5777 unsigned long *hashcodes;
5778 bfd_boolean error;
5779};
a0c8462f 5780
5a580b3a
AM
5781/* This function will be called though elf_link_hash_traverse to store
5782 all hash value of the exported symbols in an array. */
5783
5784static bfd_boolean
5785elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5786{
a50b1753 5787 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5788 const char *name;
5a580b3a
AM
5789 unsigned long ha;
5790 char *alc = NULL;
5791
5a580b3a
AM
5792 /* Ignore indirect symbols. These are added by the versioning code. */
5793 if (h->dynindx == -1)
5794 return TRUE;
5795
5796 name = h->root.root.string;
422f1182 5797 if (h->versioned >= versioned)
5a580b3a 5798 {
422f1182
L
5799 char *p = strchr (name, ELF_VER_CHR);
5800 if (p != NULL)
14b1c01e 5801 {
422f1182
L
5802 alc = (char *) bfd_malloc (p - name + 1);
5803 if (alc == NULL)
5804 {
5805 inf->error = TRUE;
5806 return FALSE;
5807 }
5808 memcpy (alc, name, p - name);
5809 alc[p - name] = '\0';
5810 name = alc;
14b1c01e 5811 }
5a580b3a
AM
5812 }
5813
5814 /* Compute the hash value. */
5815 ha = bfd_elf_hash (name);
5816
5817 /* Store the found hash value in the array given as the argument. */
14b1c01e 5818 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5819
5820 /* And store it in the struct so that we can put it in the hash table
5821 later. */
f6e332e6 5822 h->u.elf_hash_value = ha;
5a580b3a
AM
5823
5824 if (alc != NULL)
5825 free (alc);
5826
5827 return TRUE;
5828}
5829
fdc90cb4
JJ
5830struct collect_gnu_hash_codes
5831{
5832 bfd *output_bfd;
5833 const struct elf_backend_data *bed;
5834 unsigned long int nsyms;
5835 unsigned long int maskbits;
5836 unsigned long int *hashcodes;
5837 unsigned long int *hashval;
5838 unsigned long int *indx;
5839 unsigned long int *counts;
5840 bfd_vma *bitmask;
5841 bfd_byte *contents;
5842 long int min_dynindx;
5843 unsigned long int bucketcount;
5844 unsigned long int symindx;
5845 long int local_indx;
5846 long int shift1, shift2;
5847 unsigned long int mask;
14b1c01e 5848 bfd_boolean error;
fdc90cb4
JJ
5849};
5850
5851/* This function will be called though elf_link_hash_traverse to store
5852 all hash value of the exported symbols in an array. */
5853
5854static bfd_boolean
5855elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5856{
a50b1753 5857 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5858 const char *name;
fdc90cb4
JJ
5859 unsigned long ha;
5860 char *alc = NULL;
5861
fdc90cb4
JJ
5862 /* Ignore indirect symbols. These are added by the versioning code. */
5863 if (h->dynindx == -1)
5864 return TRUE;
5865
5866 /* Ignore also local symbols and undefined symbols. */
5867 if (! (*s->bed->elf_hash_symbol) (h))
5868 return TRUE;
5869
5870 name = h->root.root.string;
422f1182 5871 if (h->versioned >= versioned)
fdc90cb4 5872 {
422f1182
L
5873 char *p = strchr (name, ELF_VER_CHR);
5874 if (p != NULL)
14b1c01e 5875 {
422f1182
L
5876 alc = (char *) bfd_malloc (p - name + 1);
5877 if (alc == NULL)
5878 {
5879 s->error = TRUE;
5880 return FALSE;
5881 }
5882 memcpy (alc, name, p - name);
5883 alc[p - name] = '\0';
5884 name = alc;
14b1c01e 5885 }
fdc90cb4
JJ
5886 }
5887
5888 /* Compute the hash value. */
5889 ha = bfd_elf_gnu_hash (name);
5890
5891 /* Store the found hash value in the array for compute_bucket_count,
5892 and also for .dynsym reordering purposes. */
5893 s->hashcodes[s->nsyms] = ha;
5894 s->hashval[h->dynindx] = ha;
5895 ++s->nsyms;
5896 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5897 s->min_dynindx = h->dynindx;
5898
5899 if (alc != NULL)
5900 free (alc);
5901
5902 return TRUE;
5903}
5904
5905/* This function will be called though elf_link_hash_traverse to do
5906 final dynaminc symbol renumbering. */
5907
5908static bfd_boolean
5909elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5910{
a50b1753 5911 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5912 unsigned long int bucket;
5913 unsigned long int val;
5914
fdc90cb4
JJ
5915 /* Ignore indirect symbols. */
5916 if (h->dynindx == -1)
5917 return TRUE;
5918
5919 /* Ignore also local symbols and undefined symbols. */
5920 if (! (*s->bed->elf_hash_symbol) (h))
5921 {
5922 if (h->dynindx >= s->min_dynindx)
5923 h->dynindx = s->local_indx++;
5924 return TRUE;
5925 }
5926
5927 bucket = s->hashval[h->dynindx] % s->bucketcount;
5928 val = (s->hashval[h->dynindx] >> s->shift1)
5929 & ((s->maskbits >> s->shift1) - 1);
5930 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5931 s->bitmask[val]
5932 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5933 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5934 if (s->counts[bucket] == 1)
5935 /* Last element terminates the chain. */
5936 val |= 1;
5937 bfd_put_32 (s->output_bfd, val,
5938 s->contents + (s->indx[bucket] - s->symindx) * 4);
5939 --s->counts[bucket];
5940 h->dynindx = s->indx[bucket]++;
5941 return TRUE;
5942}
5943
5944/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5945
5946bfd_boolean
5947_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5948{
5949 return !(h->forced_local
5950 || h->root.type == bfd_link_hash_undefined
5951 || h->root.type == bfd_link_hash_undefweak
5952 || ((h->root.type == bfd_link_hash_defined
5953 || h->root.type == bfd_link_hash_defweak)
5954 && h->root.u.def.section->output_section == NULL));
5955}
5956
5a580b3a
AM
5957/* Array used to determine the number of hash table buckets to use
5958 based on the number of symbols there are. If there are fewer than
5959 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5960 fewer than 37 we use 17 buckets, and so forth. We never use more
5961 than 32771 buckets. */
5962
5963static const size_t elf_buckets[] =
5964{
5965 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5966 16411, 32771, 0
5967};
5968
5969/* Compute bucket count for hashing table. We do not use a static set
5970 of possible tables sizes anymore. Instead we determine for all
5971 possible reasonable sizes of the table the outcome (i.e., the
5972 number of collisions etc) and choose the best solution. The
5973 weighting functions are not too simple to allow the table to grow
5974 without bounds. Instead one of the weighting factors is the size.
5975 Therefore the result is always a good payoff between few collisions
5976 (= short chain lengths) and table size. */
5977static size_t
b20dd2ce 5978compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5979 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5980 unsigned long int nsyms,
5981 int gnu_hash)
5a580b3a 5982{
5a580b3a 5983 size_t best_size = 0;
5a580b3a 5984 unsigned long int i;
5a580b3a 5985
5a580b3a
AM
5986 /* We have a problem here. The following code to optimize the table
5987 size requires an integer type with more the 32 bits. If
5988 BFD_HOST_U_64_BIT is set we know about such a type. */
5989#ifdef BFD_HOST_U_64_BIT
5990 if (info->optimize)
5991 {
5a580b3a
AM
5992 size_t minsize;
5993 size_t maxsize;
5994 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5995 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5996 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5997 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5998 unsigned long int *counts;
d40f3da9 5999 bfd_size_type amt;
0883b6e0 6000 unsigned int no_improvement_count = 0;
5a580b3a
AM
6001
6002 /* Possible optimization parameters: if we have NSYMS symbols we say
6003 that the hashing table must at least have NSYMS/4 and at most
6004 2*NSYMS buckets. */
6005 minsize = nsyms / 4;
6006 if (minsize == 0)
6007 minsize = 1;
6008 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6009 if (gnu_hash)
6010 {
6011 if (minsize < 2)
6012 minsize = 2;
6013 if ((best_size & 31) == 0)
6014 ++best_size;
6015 }
5a580b3a
AM
6016
6017 /* Create array where we count the collisions in. We must use bfd_malloc
6018 since the size could be large. */
6019 amt = maxsize;
6020 amt *= sizeof (unsigned long int);
a50b1753 6021 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6022 if (counts == NULL)
fdc90cb4 6023 return 0;
5a580b3a
AM
6024
6025 /* Compute the "optimal" size for the hash table. The criteria is a
6026 minimal chain length. The minor criteria is (of course) the size
6027 of the table. */
6028 for (i = minsize; i < maxsize; ++i)
6029 {
6030 /* Walk through the array of hashcodes and count the collisions. */
6031 BFD_HOST_U_64_BIT max;
6032 unsigned long int j;
6033 unsigned long int fact;
6034
fdc90cb4
JJ
6035 if (gnu_hash && (i & 31) == 0)
6036 continue;
6037
5a580b3a
AM
6038 memset (counts, '\0', i * sizeof (unsigned long int));
6039
6040 /* Determine how often each hash bucket is used. */
6041 for (j = 0; j < nsyms; ++j)
6042 ++counts[hashcodes[j] % i];
6043
6044 /* For the weight function we need some information about the
6045 pagesize on the target. This is information need not be 100%
6046 accurate. Since this information is not available (so far) we
6047 define it here to a reasonable default value. If it is crucial
6048 to have a better value some day simply define this value. */
6049# ifndef BFD_TARGET_PAGESIZE
6050# define BFD_TARGET_PAGESIZE (4096)
6051# endif
6052
fdc90cb4
JJ
6053 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6054 and the chains. */
6055 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6056
6057# if 1
6058 /* Variant 1: optimize for short chains. We add the squares
6059 of all the chain lengths (which favors many small chain
6060 over a few long chains). */
6061 for (j = 0; j < i; ++j)
6062 max += counts[j] * counts[j];
6063
6064 /* This adds penalties for the overall size of the table. */
fdc90cb4 6065 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6066 max *= fact * fact;
6067# else
6068 /* Variant 2: Optimize a lot more for small table. Here we
6069 also add squares of the size but we also add penalties for
6070 empty slots (the +1 term). */
6071 for (j = 0; j < i; ++j)
6072 max += (1 + counts[j]) * (1 + counts[j]);
6073
6074 /* The overall size of the table is considered, but not as
6075 strong as in variant 1, where it is squared. */
fdc90cb4 6076 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6077 max *= fact;
6078# endif
6079
6080 /* Compare with current best results. */
6081 if (max < best_chlen)
6082 {
6083 best_chlen = max;
6084 best_size = i;
ca4be51c 6085 no_improvement_count = 0;
5a580b3a 6086 }
0883b6e0
NC
6087 /* PR 11843: Avoid futile long searches for the best bucket size
6088 when there are a large number of symbols. */
6089 else if (++no_improvement_count == 100)
6090 break;
5a580b3a
AM
6091 }
6092
6093 free (counts);
6094 }
6095 else
6096#endif /* defined (BFD_HOST_U_64_BIT) */
6097 {
6098 /* This is the fallback solution if no 64bit type is available or if we
6099 are not supposed to spend much time on optimizations. We select the
6100 bucket count using a fixed set of numbers. */
6101 for (i = 0; elf_buckets[i] != 0; i++)
6102 {
6103 best_size = elf_buckets[i];
fdc90cb4 6104 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6105 break;
6106 }
fdc90cb4
JJ
6107 if (gnu_hash && best_size < 2)
6108 best_size = 2;
5a580b3a
AM
6109 }
6110
5a580b3a
AM
6111 return best_size;
6112}
6113
d0bf826b
AM
6114/* Size any SHT_GROUP section for ld -r. */
6115
6116bfd_boolean
6117_bfd_elf_size_group_sections (struct bfd_link_info *info)
6118{
6119 bfd *ibfd;
57963c05 6120 asection *s;
d0bf826b 6121
c72f2fb2 6122 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6123 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6124 && (s = ibfd->sections) != NULL
6125 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6126 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6127 return FALSE;
6128 return TRUE;
6129}
6130
04c3a755
NS
6131/* Set a default stack segment size. The value in INFO wins. If it
6132 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6133 undefined it is initialized. */
6134
6135bfd_boolean
6136bfd_elf_stack_segment_size (bfd *output_bfd,
6137 struct bfd_link_info *info,
6138 const char *legacy_symbol,
6139 bfd_vma default_size)
6140{
6141 struct elf_link_hash_entry *h = NULL;
6142
6143 /* Look for legacy symbol. */
6144 if (legacy_symbol)
6145 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6146 FALSE, FALSE, FALSE);
6147 if (h && (h->root.type == bfd_link_hash_defined
6148 || h->root.type == bfd_link_hash_defweak)
6149 && h->def_regular
6150 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6151 {
6152 /* The symbol has no type if specified on the command line. */
6153 h->type = STT_OBJECT;
6154 if (info->stacksize)
695344c0 6155 /* xgettext:c-format */
871b3ab2 6156 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6157 output_bfd, legacy_symbol);
04c3a755 6158 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6159 /* xgettext:c-format */
871b3ab2 6160 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6161 output_bfd, legacy_symbol);
04c3a755
NS
6162 else
6163 info->stacksize = h->root.u.def.value;
6164 }
6165
6166 if (!info->stacksize)
6167 /* If the user didn't set a size, or explicitly inhibit the
6168 size, set it now. */
6169 info->stacksize = default_size;
6170
6171 /* Provide the legacy symbol, if it is referenced. */
6172 if (h && (h->root.type == bfd_link_hash_undefined
6173 || h->root.type == bfd_link_hash_undefweak))
6174 {
6175 struct bfd_link_hash_entry *bh = NULL;
6176
6177 if (!(_bfd_generic_link_add_one_symbol
6178 (info, output_bfd, legacy_symbol,
6179 BSF_GLOBAL, bfd_abs_section_ptr,
6180 info->stacksize >= 0 ? info->stacksize : 0,
6181 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6182 return FALSE;
6183
6184 h = (struct elf_link_hash_entry *) bh;
6185 h->def_regular = 1;
6186 h->type = STT_OBJECT;
6187 }
6188
6189 return TRUE;
6190}
6191
b531344c
MR
6192/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6193
6194struct elf_gc_sweep_symbol_info
6195{
6196 struct bfd_link_info *info;
6197 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6198 bfd_boolean);
6199};
6200
6201static bfd_boolean
6202elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6203{
6204 if (!h->mark
6205 && (((h->root.type == bfd_link_hash_defined
6206 || h->root.type == bfd_link_hash_defweak)
6207 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6208 && h->root.u.def.section->gc_mark))
6209 || h->root.type == bfd_link_hash_undefined
6210 || h->root.type == bfd_link_hash_undefweak))
6211 {
6212 struct elf_gc_sweep_symbol_info *inf;
6213
6214 inf = (struct elf_gc_sweep_symbol_info *) data;
6215 (*inf->hide_symbol) (inf->info, h, TRUE);
6216 h->def_regular = 0;
6217 h->ref_regular = 0;
6218 h->ref_regular_nonweak = 0;
6219 }
6220
6221 return TRUE;
6222}
6223
5a580b3a
AM
6224/* Set up the sizes and contents of the ELF dynamic sections. This is
6225 called by the ELF linker emulation before_allocation routine. We
6226 must set the sizes of the sections before the linker sets the
6227 addresses of the various sections. */
6228
6229bfd_boolean
6230bfd_elf_size_dynamic_sections (bfd *output_bfd,
6231 const char *soname,
6232 const char *rpath,
6233 const char *filter_shlib,
7ee314fa
AM
6234 const char *audit,
6235 const char *depaudit,
5a580b3a
AM
6236 const char * const *auxiliary_filters,
6237 struct bfd_link_info *info,
fd91d419 6238 asection **sinterpptr)
5a580b3a 6239{
5a580b3a
AM
6240 bfd *dynobj;
6241 const struct elf_backend_data *bed;
5a580b3a
AM
6242
6243 *sinterpptr = NULL;
6244
5a580b3a
AM
6245 if (!is_elf_hash_table (info->hash))
6246 return TRUE;
6247
5a580b3a
AM
6248 dynobj = elf_hash_table (info)->dynobj;
6249
9a2a56cc 6250 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6251 {
902e9fc7
MR
6252 struct bfd_elf_version_tree *verdefs;
6253 struct elf_info_failed asvinfo;
5a580b3a
AM
6254 struct bfd_elf_version_tree *t;
6255 struct bfd_elf_version_expr *d;
902e9fc7 6256 asection *s;
e6699019 6257 size_t soname_indx;
7ee314fa 6258
5a580b3a
AM
6259 /* If we are supposed to export all symbols into the dynamic symbol
6260 table (this is not the normal case), then do so. */
55255dae 6261 if (info->export_dynamic
0e1862bb 6262 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6263 {
3d13f3e9
AM
6264 struct elf_info_failed eif;
6265
6266 eif.info = info;
6267 eif.failed = FALSE;
5a580b3a
AM
6268 elf_link_hash_traverse (elf_hash_table (info),
6269 _bfd_elf_export_symbol,
6270 &eif);
6271 if (eif.failed)
6272 return FALSE;
6273 }
6274
e6699019
L
6275 if (soname != NULL)
6276 {
6277 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6278 soname, TRUE);
6279 if (soname_indx == (size_t) -1
6280 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6281 return FALSE;
6282 }
6283 else
6284 soname_indx = (size_t) -1;
6285
5a580b3a 6286 /* Make all global versions with definition. */
fd91d419 6287 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6288 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6289 if (!d->symver && d->literal)
5a580b3a
AM
6290 {
6291 const char *verstr, *name;
6292 size_t namelen, verlen, newlen;
93252b1c 6293 char *newname, *p, leading_char;
5a580b3a
AM
6294 struct elf_link_hash_entry *newh;
6295
93252b1c 6296 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6297 name = d->pattern;
93252b1c 6298 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6299 verstr = t->name;
6300 verlen = strlen (verstr);
6301 newlen = namelen + verlen + 3;
6302
a50b1753 6303 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6304 if (newname == NULL)
6305 return FALSE;
93252b1c
MF
6306 newname[0] = leading_char;
6307 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6308
6309 /* Check the hidden versioned definition. */
6310 p = newname + namelen;
6311 *p++ = ELF_VER_CHR;
6312 memcpy (p, verstr, verlen + 1);
6313 newh = elf_link_hash_lookup (elf_hash_table (info),
6314 newname, FALSE, FALSE,
6315 FALSE);
6316 if (newh == NULL
6317 || (newh->root.type != bfd_link_hash_defined
6318 && newh->root.type != bfd_link_hash_defweak))
6319 {
6320 /* Check the default versioned definition. */
6321 *p++ = ELF_VER_CHR;
6322 memcpy (p, verstr, verlen + 1);
6323 newh = elf_link_hash_lookup (elf_hash_table (info),
6324 newname, FALSE, FALSE,
6325 FALSE);
6326 }
6327 free (newname);
6328
6329 /* Mark this version if there is a definition and it is
6330 not defined in a shared object. */
6331 if (newh != NULL
f5385ebf 6332 && !newh->def_dynamic
5a580b3a
AM
6333 && (newh->root.type == bfd_link_hash_defined
6334 || newh->root.type == bfd_link_hash_defweak))
6335 d->symver = 1;
6336 }
6337
6338 /* Attach all the symbols to their version information. */
5a580b3a 6339 asvinfo.info = info;
5a580b3a
AM
6340 asvinfo.failed = FALSE;
6341
6342 elf_link_hash_traverse (elf_hash_table (info),
6343 _bfd_elf_link_assign_sym_version,
6344 &asvinfo);
6345 if (asvinfo.failed)
6346 return FALSE;
6347
6348 if (!info->allow_undefined_version)
6349 {
6350 /* Check if all global versions have a definition. */
3d13f3e9 6351 bfd_boolean all_defined = TRUE;
fd91d419 6352 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6353 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6354 if (d->literal && !d->symver && !d->script)
5a580b3a 6355 {
4eca0228 6356 _bfd_error_handler
5a580b3a
AM
6357 (_("%s: undefined version: %s"),
6358 d->pattern, t->name);
6359 all_defined = FALSE;
6360 }
6361
6362 if (!all_defined)
6363 {
6364 bfd_set_error (bfd_error_bad_value);
6365 return FALSE;
6366 }
6367 }
6368
902e9fc7
MR
6369 /* Set up the version definition section. */
6370 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6371 BFD_ASSERT (s != NULL);
5a580b3a 6372
902e9fc7
MR
6373 /* We may have created additional version definitions if we are
6374 just linking a regular application. */
6375 verdefs = info->version_info;
5a580b3a 6376
902e9fc7
MR
6377 /* Skip anonymous version tag. */
6378 if (verdefs != NULL && verdefs->vernum == 0)
6379 verdefs = verdefs->next;
5a580b3a 6380
902e9fc7
MR
6381 if (verdefs == NULL && !info->create_default_symver)
6382 s->flags |= SEC_EXCLUDE;
6383 else
5a580b3a 6384 {
902e9fc7
MR
6385 unsigned int cdefs;
6386 bfd_size_type size;
6387 bfd_byte *p;
6388 Elf_Internal_Verdef def;
6389 Elf_Internal_Verdaux defaux;
6390 struct bfd_link_hash_entry *bh;
6391 struct elf_link_hash_entry *h;
6392 const char *name;
5a580b3a 6393
902e9fc7
MR
6394 cdefs = 0;
6395 size = 0;
5a580b3a 6396
902e9fc7
MR
6397 /* Make space for the base version. */
6398 size += sizeof (Elf_External_Verdef);
6399 size += sizeof (Elf_External_Verdaux);
6400 ++cdefs;
6401
6402 /* Make space for the default version. */
6403 if (info->create_default_symver)
6404 {
6405 size += sizeof (Elf_External_Verdef);
6406 ++cdefs;
3e3b46e5
PB
6407 }
6408
5a580b3a
AM
6409 for (t = verdefs; t != NULL; t = t->next)
6410 {
6411 struct bfd_elf_version_deps *n;
6412
a6cc6b3b
RO
6413 /* Don't emit base version twice. */
6414 if (t->vernum == 0)
6415 continue;
6416
5a580b3a
AM
6417 size += sizeof (Elf_External_Verdef);
6418 size += sizeof (Elf_External_Verdaux);
6419 ++cdefs;
6420
6421 for (n = t->deps; n != NULL; n = n->next)
6422 size += sizeof (Elf_External_Verdaux);
6423 }
6424
eea6121a 6425 s->size = size;
a50b1753 6426 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6427 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6428 return FALSE;
6429
6430 /* Fill in the version definition section. */
6431
6432 p = s->contents;
6433
6434 def.vd_version = VER_DEF_CURRENT;
6435 def.vd_flags = VER_FLG_BASE;
6436 def.vd_ndx = 1;
6437 def.vd_cnt = 1;
3e3b46e5
PB
6438 if (info->create_default_symver)
6439 {
6440 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6441 def.vd_next = sizeof (Elf_External_Verdef);
6442 }
6443 else
6444 {
6445 def.vd_aux = sizeof (Elf_External_Verdef);
6446 def.vd_next = (sizeof (Elf_External_Verdef)
6447 + sizeof (Elf_External_Verdaux));
6448 }
5a580b3a 6449
ef53be89 6450 if (soname_indx != (size_t) -1)
5a580b3a
AM
6451 {
6452 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6453 soname_indx);
6454 def.vd_hash = bfd_elf_hash (soname);
6455 defaux.vda_name = soname_indx;
3e3b46e5 6456 name = soname;
5a580b3a
AM
6457 }
6458 else
6459 {
ef53be89 6460 size_t indx;
5a580b3a 6461
06084812 6462 name = lbasename (output_bfd->filename);
5a580b3a
AM
6463 def.vd_hash = bfd_elf_hash (name);
6464 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6465 name, FALSE);
ef53be89 6466 if (indx == (size_t) -1)
5a580b3a
AM
6467 return FALSE;
6468 defaux.vda_name = indx;
6469 }
6470 defaux.vda_next = 0;
6471
6472 _bfd_elf_swap_verdef_out (output_bfd, &def,
6473 (Elf_External_Verdef *) p);
6474 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6475 if (info->create_default_symver)
6476 {
6477 /* Add a symbol representing this version. */
6478 bh = NULL;
6479 if (! (_bfd_generic_link_add_one_symbol
6480 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6481 0, NULL, FALSE,
6482 get_elf_backend_data (dynobj)->collect, &bh)))
6483 return FALSE;
6484 h = (struct elf_link_hash_entry *) bh;
6485 h->non_elf = 0;
6486 h->def_regular = 1;
6487 h->type = STT_OBJECT;
6488 h->verinfo.vertree = NULL;
6489
6490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6491 return FALSE;
6492
6493 /* Create a duplicate of the base version with the same
6494 aux block, but different flags. */
6495 def.vd_flags = 0;
6496 def.vd_ndx = 2;
6497 def.vd_aux = sizeof (Elf_External_Verdef);
6498 if (verdefs)
6499 def.vd_next = (sizeof (Elf_External_Verdef)
6500 + sizeof (Elf_External_Verdaux));
6501 else
6502 def.vd_next = 0;
6503 _bfd_elf_swap_verdef_out (output_bfd, &def,
6504 (Elf_External_Verdef *) p);
6505 p += sizeof (Elf_External_Verdef);
6506 }
5a580b3a
AM
6507 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6508 (Elf_External_Verdaux *) p);
6509 p += sizeof (Elf_External_Verdaux);
6510
6511 for (t = verdefs; t != NULL; t = t->next)
6512 {
6513 unsigned int cdeps;
6514 struct bfd_elf_version_deps *n;
5a580b3a 6515
a6cc6b3b
RO
6516 /* Don't emit the base version twice. */
6517 if (t->vernum == 0)
6518 continue;
6519
5a580b3a
AM
6520 cdeps = 0;
6521 for (n = t->deps; n != NULL; n = n->next)
6522 ++cdeps;
6523
6524 /* Add a symbol representing this version. */
6525 bh = NULL;
6526 if (! (_bfd_generic_link_add_one_symbol
6527 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6528 0, NULL, FALSE,
6529 get_elf_backend_data (dynobj)->collect, &bh)))
6530 return FALSE;
6531 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6532 h->non_elf = 0;
6533 h->def_regular = 1;
5a580b3a
AM
6534 h->type = STT_OBJECT;
6535 h->verinfo.vertree = t;
6536
c152c796 6537 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6538 return FALSE;
6539
6540 def.vd_version = VER_DEF_CURRENT;
6541 def.vd_flags = 0;
6542 if (t->globals.list == NULL
6543 && t->locals.list == NULL
6544 && ! t->used)
6545 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6546 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6547 def.vd_cnt = cdeps + 1;
6548 def.vd_hash = bfd_elf_hash (t->name);
6549 def.vd_aux = sizeof (Elf_External_Verdef);
6550 def.vd_next = 0;
a6cc6b3b
RO
6551
6552 /* If a basever node is next, it *must* be the last node in
6553 the chain, otherwise Verdef construction breaks. */
6554 if (t->next != NULL && t->next->vernum == 0)
6555 BFD_ASSERT (t->next->next == NULL);
6556
6557 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6558 def.vd_next = (sizeof (Elf_External_Verdef)
6559 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6560
6561 _bfd_elf_swap_verdef_out (output_bfd, &def,
6562 (Elf_External_Verdef *) p);
6563 p += sizeof (Elf_External_Verdef);
6564
6565 defaux.vda_name = h->dynstr_index;
6566 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6567 h->dynstr_index);
6568 defaux.vda_next = 0;
6569 if (t->deps != NULL)
6570 defaux.vda_next = sizeof (Elf_External_Verdaux);
6571 t->name_indx = defaux.vda_name;
6572
6573 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6574 (Elf_External_Verdaux *) p);
6575 p += sizeof (Elf_External_Verdaux);
6576
6577 for (n = t->deps; n != NULL; n = n->next)
6578 {
6579 if (n->version_needed == NULL)
6580 {
6581 /* This can happen if there was an error in the
6582 version script. */
6583 defaux.vda_name = 0;
6584 }
6585 else
6586 {
6587 defaux.vda_name = n->version_needed->name_indx;
6588 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6589 defaux.vda_name);
6590 }
6591 if (n->next == NULL)
6592 defaux.vda_next = 0;
6593 else
6594 defaux.vda_next = sizeof (Elf_External_Verdaux);
6595
6596 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6597 (Elf_External_Verdaux *) p);
6598 p += sizeof (Elf_External_Verdaux);
6599 }
6600 }
6601
5a580b3a
AM
6602 elf_tdata (output_bfd)->cverdefs = cdefs;
6603 }
902e9fc7
MR
6604 }
6605
6606 bed = get_elf_backend_data (output_bfd);
6607
6608 if (info->gc_sections && bed->can_gc_sections)
6609 {
6610 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6611
6612 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6613 dynamic symbol table. */
902e9fc7
MR
6614 sweep_info.info = info;
6615 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6616 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6617 &sweep_info);
3d13f3e9
AM
6618 }
6619
6620 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6621 {
6622 asection *s;
6623 struct elf_find_verdep_info sinfo;
6624
6625 /* Work out the size of the version reference section. */
6626
6627 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6628 BFD_ASSERT (s != NULL);
902e9fc7 6629
3d13f3e9
AM
6630 sinfo.info = info;
6631 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6632 if (sinfo.vers == 0)
6633 sinfo.vers = 1;
6634 sinfo.failed = FALSE;
6635
6636 elf_link_hash_traverse (elf_hash_table (info),
6637 _bfd_elf_link_find_version_dependencies,
6638 &sinfo);
6639 if (sinfo.failed)
6640 return FALSE;
6641
6642 if (elf_tdata (output_bfd)->verref == NULL)
6643 s->flags |= SEC_EXCLUDE;
6644 else
6645 {
6646 Elf_Internal_Verneed *vn;
6647 unsigned int size;
6648 unsigned int crefs;
6649 bfd_byte *p;
6650
6651 /* Build the version dependency section. */
6652 size = 0;
6653 crefs = 0;
6654 for (vn = elf_tdata (output_bfd)->verref;
6655 vn != NULL;
6656 vn = vn->vn_nextref)
6657 {
6658 Elf_Internal_Vernaux *a;
6659
6660 size += sizeof (Elf_External_Verneed);
6661 ++crefs;
6662 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6663 size += sizeof (Elf_External_Vernaux);
6664 }
6665
6666 s->size = size;
6667 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6668 if (s->contents == NULL)
6669 return FALSE;
6670
6671 p = s->contents;
6672 for (vn = elf_tdata (output_bfd)->verref;
6673 vn != NULL;
6674 vn = vn->vn_nextref)
6675 {
6676 unsigned int caux;
6677 Elf_Internal_Vernaux *a;
6678 size_t indx;
6679
6680 caux = 0;
6681 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6682 ++caux;
6683
6684 vn->vn_version = VER_NEED_CURRENT;
6685 vn->vn_cnt = caux;
6686 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6687 elf_dt_name (vn->vn_bfd) != NULL
6688 ? elf_dt_name (vn->vn_bfd)
6689 : lbasename (vn->vn_bfd->filename),
6690 FALSE);
6691 if (indx == (size_t) -1)
6692 return FALSE;
6693 vn->vn_file = indx;
6694 vn->vn_aux = sizeof (Elf_External_Verneed);
6695 if (vn->vn_nextref == NULL)
6696 vn->vn_next = 0;
6697 else
6698 vn->vn_next = (sizeof (Elf_External_Verneed)
6699 + caux * sizeof (Elf_External_Vernaux));
6700
6701 _bfd_elf_swap_verneed_out (output_bfd, vn,
6702 (Elf_External_Verneed *) p);
6703 p += sizeof (Elf_External_Verneed);
6704
6705 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6706 {
6707 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6708 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6709 a->vna_nodename, FALSE);
6710 if (indx == (size_t) -1)
6711 return FALSE;
6712 a->vna_name = indx;
6713 if (a->vna_nextptr == NULL)
6714 a->vna_next = 0;
6715 else
6716 a->vna_next = sizeof (Elf_External_Vernaux);
6717
6718 _bfd_elf_swap_vernaux_out (output_bfd, a,
6719 (Elf_External_Vernaux *) p);
6720 p += sizeof (Elf_External_Vernaux);
6721 }
6722 }
6723
6724 elf_tdata (output_bfd)->cverrefs = crefs;
6725 }
902e9fc7
MR
6726 }
6727
6728 /* Any syms created from now on start with -1 in
6729 got.refcount/offset and plt.refcount/offset. */
6730 elf_hash_table (info)->init_got_refcount
6731 = elf_hash_table (info)->init_got_offset;
6732 elf_hash_table (info)->init_plt_refcount
6733 = elf_hash_table (info)->init_plt_offset;
6734
6735 if (bfd_link_relocatable (info)
6736 && !_bfd_elf_size_group_sections (info))
6737 return FALSE;
6738
6739 /* The backend may have to create some sections regardless of whether
6740 we're dynamic or not. */
6741 if (bed->elf_backend_always_size_sections
6742 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6743 return FALSE;
6744
6745 /* Determine any GNU_STACK segment requirements, after the backend
6746 has had a chance to set a default segment size. */
6747 if (info->execstack)
6748 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6749 else if (info->noexecstack)
6750 elf_stack_flags (output_bfd) = PF_R | PF_W;
6751 else
6752 {
6753 bfd *inputobj;
6754 asection *notesec = NULL;
6755 int exec = 0;
6756
6757 for (inputobj = info->input_bfds;
6758 inputobj;
6759 inputobj = inputobj->link.next)
6760 {
6761 asection *s;
6762
6763 if (inputobj->flags
6764 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6765 continue;
57963c05
AM
6766 s = inputobj->sections;
6767 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6768 continue;
6769
902e9fc7
MR
6770 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6771 if (s)
6772 {
6773 if (s->flags & SEC_CODE)
6774 exec = PF_X;
6775 notesec = s;
6776 }
6777 else if (bed->default_execstack)
6778 exec = PF_X;
6779 }
6780 if (notesec || info->stacksize > 0)
6781 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6782 if (notesec && exec && bfd_link_relocatable (info)
6783 && notesec->output_section != bfd_abs_section_ptr)
6784 notesec->output_section->flags |= SEC_CODE;
6785 }
6786
6787 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6788 {
6789 struct elf_info_failed eif;
6790 struct elf_link_hash_entry *h;
6791 asection *dynstr;
6792 asection *s;
6793
6794 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6795 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6796
902e9fc7
MR
6797 if (info->symbolic)
6798 {
6799 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6800 return FALSE;
6801 info->flags |= DF_SYMBOLIC;
6802 }
6803
6804 if (rpath != NULL)
6805 {
6806 size_t indx;
6807 bfd_vma tag;
6808
6809 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6810 TRUE);
6811 if (indx == (size_t) -1)
6812 return FALSE;
6813
6814 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6815 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6816 return FALSE;
6817 }
6818
6819 if (filter_shlib != NULL)
6820 {
6821 size_t indx;
6822
6823 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6824 filter_shlib, TRUE);
6825 if (indx == (size_t) -1
6826 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6827 return FALSE;
6828 }
6829
6830 if (auxiliary_filters != NULL)
6831 {
6832 const char * const *p;
6833
6834 for (p = auxiliary_filters; *p != NULL; p++)
6835 {
6836 size_t indx;
6837
6838 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6839 *p, TRUE);
6840 if (indx == (size_t) -1
6841 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6842 return FALSE;
6843 }
6844 }
6845
6846 if (audit != NULL)
6847 {
6848 size_t indx;
6849
6850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6851 TRUE);
6852 if (indx == (size_t) -1
6853 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6854 return FALSE;
6855 }
6856
6857 if (depaudit != NULL)
6858 {
6859 size_t indx;
6860
6861 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6862 TRUE);
6863 if (indx == (size_t) -1
6864 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6865 return FALSE;
6866 }
6867
6868 eif.info = info;
6869 eif.failed = FALSE;
6870
6871 /* Find all symbols which were defined in a dynamic object and make
6872 the backend pick a reasonable value for them. */
6873 elf_link_hash_traverse (elf_hash_table (info),
6874 _bfd_elf_adjust_dynamic_symbol,
6875 &eif);
6876 if (eif.failed)
6877 return FALSE;
6878
6879 /* Add some entries to the .dynamic section. We fill in some of the
6880 values later, in bfd_elf_final_link, but we must add the entries
6881 now so that we know the final size of the .dynamic section. */
6882
6883 /* If there are initialization and/or finalization functions to
6884 call then add the corresponding DT_INIT/DT_FINI entries. */
6885 h = (info->init_function
6886 ? elf_link_hash_lookup (elf_hash_table (info),
6887 info->init_function, FALSE,
6888 FALSE, FALSE)
6889 : NULL);
6890 if (h != NULL
6891 && (h->ref_regular
6892 || h->def_regular))
6893 {
6894 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6895 return FALSE;
6896 }
6897 h = (info->fini_function
6898 ? elf_link_hash_lookup (elf_hash_table (info),
6899 info->fini_function, FALSE,
6900 FALSE, FALSE)
6901 : NULL);
6902 if (h != NULL
6903 && (h->ref_regular
6904 || h->def_regular))
6905 {
6906 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6907 return FALSE;
6908 }
6909
6910 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6911 if (s != NULL && s->linker_has_input)
6912 {
6913 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6914 if (! bfd_link_executable (info))
6915 {
6916 bfd *sub;
6917 asection *o;
6918
57963c05
AM
6919 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6920 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6921 && (o = sub->sections) != NULL
6922 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6923 for (o = sub->sections; o != NULL; o = o->next)
6924 if (elf_section_data (o)->this_hdr.sh_type
6925 == SHT_PREINIT_ARRAY)
6926 {
6927 _bfd_error_handler
871b3ab2 6928 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6929 sub);
6930 break;
6931 }
6932
6933 bfd_set_error (bfd_error_nonrepresentable_section);
6934 return FALSE;
6935 }
6936
6937 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6938 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6939 return FALSE;
6940 }
6941 s = bfd_get_section_by_name (output_bfd, ".init_array");
6942 if (s != NULL && s->linker_has_input)
6943 {
6944 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6945 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6946 return FALSE;
6947 }
6948 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6949 if (s != NULL && s->linker_has_input)
6950 {
6951 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6952 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6953 return FALSE;
6954 }
6955
6956 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6957 /* If .dynstr is excluded from the link, we don't want any of
6958 these tags. Strictly, we should be checking each section
6959 individually; This quick check covers for the case where
6960 someone does a /DISCARD/ : { *(*) }. */
6961 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6962 {
6963 bfd_size_type strsize;
6964
6965 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6966 if ((info->emit_hash
6967 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6968 || (info->emit_gnu_hash
6969 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6970 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6972 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6973 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6974 bed->s->sizeof_sym))
6975 return FALSE;
6976 }
6977 }
6978
6979 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6980 return FALSE;
6981
6982 /* The backend must work out the sizes of all the other dynamic
6983 sections. */
6984 if (dynobj != NULL
6985 && bed->elf_backend_size_dynamic_sections != NULL
6986 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6987 return FALSE;
6988
6989 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6990 {
902e9fc7
MR
6991 if (elf_tdata (output_bfd)->cverdefs)
6992 {
6993 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6994
6995 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6996 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6997 return FALSE;
6998 }
6999
7000 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7001 {
7002 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7003 return FALSE;
7004 }
7005 else if (info->flags & DF_BIND_NOW)
7006 {
7007 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7008 return FALSE;
7009 }
7010
7011 if (info->flags_1)
7012 {
7013 if (bfd_link_executable (info))
7014 info->flags_1 &= ~ (DF_1_INITFIRST
7015 | DF_1_NODELETE
7016 | DF_1_NOOPEN);
7017 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7018 return FALSE;
7019 }
7020
7021 if (elf_tdata (output_bfd)->cverrefs)
7022 {
7023 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7024
7025 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7026 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7027 return FALSE;
7028 }
5a580b3a 7029
8423293d
AM
7030 if ((elf_tdata (output_bfd)->cverrefs == 0
7031 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7032 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7033 {
902e9fc7
MR
7034 asection *s;
7035
3d4d4302 7036 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7037 s->flags |= SEC_EXCLUDE;
7038 }
7039 }
7040 return TRUE;
7041}
7042
74541ad4
AM
7043/* Find the first non-excluded output section. We'll use its
7044 section symbol for some emitted relocs. */
7045void
7046_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7047{
7048 asection *s;
7049
7050 for (s = output_bfd->sections; s != NULL; s = s->next)
7051 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7052 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7053 {
7054 elf_hash_table (info)->text_index_section = s;
7055 break;
7056 }
7057}
7058
7059/* Find two non-excluded output sections, one for code, one for data.
7060 We'll use their section symbols for some emitted relocs. */
7061void
7062_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7063{
7064 asection *s;
7065
266b05cf 7066 /* Data first, since setting text_index_section changes
7f923b7f 7067 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7068 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7069 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7070 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7071 {
266b05cf 7072 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7073 break;
7074 }
7075
7076 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7077 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7078 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7079 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7080 {
266b05cf 7081 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7082 break;
7083 }
7084
7085 if (elf_hash_table (info)->text_index_section == NULL)
7086 elf_hash_table (info)->text_index_section
7087 = elf_hash_table (info)->data_index_section;
7088}
7089
8423293d
AM
7090bfd_boolean
7091bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7092{
74541ad4 7093 const struct elf_backend_data *bed;
23ec1e32 7094 unsigned long section_sym_count;
96d01d93 7095 bfd_size_type dynsymcount = 0;
74541ad4 7096
8423293d
AM
7097 if (!is_elf_hash_table (info->hash))
7098 return TRUE;
7099
74541ad4
AM
7100 bed = get_elf_backend_data (output_bfd);
7101 (*bed->elf_backend_init_index_section) (output_bfd, info);
7102
23ec1e32
MR
7103 /* Assign dynsym indices. In a shared library we generate a section
7104 symbol for each output section, which come first. Next come all
7105 of the back-end allocated local dynamic syms, followed by the rest
7106 of the global symbols.
7107
7108 This is usually not needed for static binaries, however backends
7109 can request to always do it, e.g. the MIPS backend uses dynamic
7110 symbol counts to lay out GOT, which will be produced in the
7111 presence of GOT relocations even in static binaries (holding fixed
7112 data in that case, to satisfy those relocations). */
7113
7114 if (elf_hash_table (info)->dynamic_sections_created
7115 || bed->always_renumber_dynsyms)
7116 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7117 &section_sym_count);
7118
8423293d
AM
7119 if (elf_hash_table (info)->dynamic_sections_created)
7120 {
7121 bfd *dynobj;
8423293d 7122 asection *s;
8423293d
AM
7123 unsigned int dtagcount;
7124
7125 dynobj = elf_hash_table (info)->dynobj;
7126
5a580b3a 7127 /* Work out the size of the symbol version section. */
3d4d4302 7128 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7129 BFD_ASSERT (s != NULL);
d5486c43 7130 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7131 {
eea6121a 7132 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7133 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7134 if (s->contents == NULL)
7135 return FALSE;
7136
7137 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7138 return FALSE;
7139 }
7140
7141 /* Set the size of the .dynsym and .hash sections. We counted
7142 the number of dynamic symbols in elf_link_add_object_symbols.
7143 We will build the contents of .dynsym and .hash when we build
7144 the final symbol table, because until then we do not know the
7145 correct value to give the symbols. We built the .dynstr
7146 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7147 s = elf_hash_table (info)->dynsym;
5a580b3a 7148 BFD_ASSERT (s != NULL);
eea6121a 7149 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7150
d5486c43
L
7151 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7152 if (s->contents == NULL)
7153 return FALSE;
5a580b3a 7154
d5486c43
L
7155 /* The first entry in .dynsym is a dummy symbol. Clear all the
7156 section syms, in case we don't output them all. */
7157 ++section_sym_count;
7158 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7159
fdc90cb4
JJ
7160 elf_hash_table (info)->bucketcount = 0;
7161
5a580b3a
AM
7162 /* Compute the size of the hashing table. As a side effect this
7163 computes the hash values for all the names we export. */
fdc90cb4
JJ
7164 if (info->emit_hash)
7165 {
7166 unsigned long int *hashcodes;
14b1c01e 7167 struct hash_codes_info hashinf;
fdc90cb4
JJ
7168 bfd_size_type amt;
7169 unsigned long int nsyms;
7170 size_t bucketcount;
7171 size_t hash_entry_size;
7172
7173 /* Compute the hash values for all exported symbols. At the same
7174 time store the values in an array so that we could use them for
7175 optimizations. */
7176 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7177 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7178 if (hashcodes == NULL)
7179 return FALSE;
14b1c01e
AM
7180 hashinf.hashcodes = hashcodes;
7181 hashinf.error = FALSE;
5a580b3a 7182
fdc90cb4
JJ
7183 /* Put all hash values in HASHCODES. */
7184 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7185 elf_collect_hash_codes, &hashinf);
7186 if (hashinf.error)
4dd07732
AM
7187 {
7188 free (hashcodes);
7189 return FALSE;
7190 }
5a580b3a 7191
14b1c01e 7192 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7193 bucketcount
7194 = compute_bucket_count (info, hashcodes, nsyms, 0);
7195 free (hashcodes);
7196
4b48e2f6 7197 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7198 return FALSE;
5a580b3a 7199
fdc90cb4
JJ
7200 elf_hash_table (info)->bucketcount = bucketcount;
7201
3d4d4302 7202 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7203 BFD_ASSERT (s != NULL);
7204 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7205 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7206 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7207 if (s->contents == NULL)
7208 return FALSE;
7209
7210 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7211 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7212 s->contents + hash_entry_size);
7213 }
7214
7215 if (info->emit_gnu_hash)
7216 {
7217 size_t i, cnt;
7218 unsigned char *contents;
7219 struct collect_gnu_hash_codes cinfo;
7220 bfd_size_type amt;
7221 size_t bucketcount;
7222
7223 memset (&cinfo, 0, sizeof (cinfo));
7224
7225 /* Compute the hash values for all exported symbols. At the same
7226 time store the values in an array so that we could use them for
7227 optimizations. */
7228 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7229 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7230 if (cinfo.hashcodes == NULL)
7231 return FALSE;
7232
7233 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7234 cinfo.min_dynindx = -1;
7235 cinfo.output_bfd = output_bfd;
7236 cinfo.bed = bed;
7237
7238 /* Put all hash values in HASHCODES. */
7239 elf_link_hash_traverse (elf_hash_table (info),
7240 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7241 if (cinfo.error)
4dd07732
AM
7242 {
7243 free (cinfo.hashcodes);
7244 return FALSE;
7245 }
fdc90cb4
JJ
7246
7247 bucketcount
7248 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7249
7250 if (bucketcount == 0)
7251 {
7252 free (cinfo.hashcodes);
7253 return FALSE;
7254 }
7255
3d4d4302 7256 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7257 BFD_ASSERT (s != NULL);
7258
7259 if (cinfo.nsyms == 0)
7260 {
7261 /* Empty .gnu.hash section is special. */
7262 BFD_ASSERT (cinfo.min_dynindx == -1);
7263 free (cinfo.hashcodes);
7264 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7265 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7266 if (contents == NULL)
7267 return FALSE;
7268 s->contents = contents;
7269 /* 1 empty bucket. */
7270 bfd_put_32 (output_bfd, 1, contents);
7271 /* SYMIDX above the special symbol 0. */
7272 bfd_put_32 (output_bfd, 1, contents + 4);
7273 /* Just one word for bitmask. */
7274 bfd_put_32 (output_bfd, 1, contents + 8);
7275 /* Only hash fn bloom filter. */
7276 bfd_put_32 (output_bfd, 0, contents + 12);
7277 /* No hashes are valid - empty bitmask. */
7278 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7279 /* No hashes in the only bucket. */
7280 bfd_put_32 (output_bfd, 0,
7281 contents + 16 + bed->s->arch_size / 8);
7282 }
7283 else
7284 {
9e6619e2 7285 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7286 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7287
9e6619e2
AM
7288 x = cinfo.nsyms;
7289 maskbitslog2 = 1;
7290 while ((x >>= 1) != 0)
7291 ++maskbitslog2;
fdc90cb4
JJ
7292 if (maskbitslog2 < 3)
7293 maskbitslog2 = 5;
7294 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7295 maskbitslog2 = maskbitslog2 + 3;
7296 else
7297 maskbitslog2 = maskbitslog2 + 2;
7298 if (bed->s->arch_size == 64)
7299 {
7300 if (maskbitslog2 == 5)
7301 maskbitslog2 = 6;
7302 cinfo.shift1 = 6;
7303 }
7304 else
7305 cinfo.shift1 = 5;
7306 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7307 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7308 cinfo.maskbits = 1 << maskbitslog2;
7309 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7310 amt = bucketcount * sizeof (unsigned long int) * 2;
7311 amt += maskwords * sizeof (bfd_vma);
a50b1753 7312 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7313 if (cinfo.bitmask == NULL)
7314 {
7315 free (cinfo.hashcodes);
7316 return FALSE;
7317 }
7318
a50b1753 7319 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7320 cinfo.indx = cinfo.counts + bucketcount;
7321 cinfo.symindx = dynsymcount - cinfo.nsyms;
7322 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7323
7324 /* Determine how often each hash bucket is used. */
7325 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7326 for (i = 0; i < cinfo.nsyms; ++i)
7327 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7328
7329 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7330 if (cinfo.counts[i] != 0)
7331 {
7332 cinfo.indx[i] = cnt;
7333 cnt += cinfo.counts[i];
7334 }
7335 BFD_ASSERT (cnt == dynsymcount);
7336 cinfo.bucketcount = bucketcount;
7337 cinfo.local_indx = cinfo.min_dynindx;
7338
7339 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7340 s->size += cinfo.maskbits / 8;
a50b1753 7341 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7342 if (contents == NULL)
7343 {
7344 free (cinfo.bitmask);
7345 free (cinfo.hashcodes);
7346 return FALSE;
7347 }
7348
7349 s->contents = contents;
7350 bfd_put_32 (output_bfd, bucketcount, contents);
7351 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7352 bfd_put_32 (output_bfd, maskwords, contents + 8);
7353 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7354 contents += 16 + cinfo.maskbits / 8;
7355
7356 for (i = 0; i < bucketcount; ++i)
7357 {
7358 if (cinfo.counts[i] == 0)
7359 bfd_put_32 (output_bfd, 0, contents);
7360 else
7361 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7362 contents += 4;
7363 }
7364
7365 cinfo.contents = contents;
7366
7367 /* Renumber dynamic symbols, populate .gnu.hash section. */
7368 elf_link_hash_traverse (elf_hash_table (info),
7369 elf_renumber_gnu_hash_syms, &cinfo);
7370
7371 contents = s->contents + 16;
7372 for (i = 0; i < maskwords; ++i)
7373 {
7374 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7375 contents);
7376 contents += bed->s->arch_size / 8;
7377 }
7378
7379 free (cinfo.bitmask);
7380 free (cinfo.hashcodes);
7381 }
7382 }
5a580b3a 7383
3d4d4302 7384 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7385 BFD_ASSERT (s != NULL);
7386
4ad4eba5 7387 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7388
eea6121a 7389 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7390
7391 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7392 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7393 return FALSE;
7394 }
7395
7396 return TRUE;
7397}
4d269e42 7398\f
4d269e42
AM
7399/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7400
7401static void
7402merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7403 asection *sec)
7404{
dbaa2011
AM
7405 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7406 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7407}
7408
7409/* Finish SHF_MERGE section merging. */
7410
7411bfd_boolean
630993ec 7412_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7413{
7414 bfd *ibfd;
7415 asection *sec;
7416
7417 if (!is_elf_hash_table (info->hash))
7418 return FALSE;
7419
c72f2fb2 7420 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7421 if ((ibfd->flags & DYNAMIC) == 0
7422 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7423 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7424 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7425 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7426 if ((sec->flags & SEC_MERGE) != 0
7427 && !bfd_is_abs_section (sec->output_section))
7428 {
7429 struct bfd_elf_section_data *secdata;
7430
7431 secdata = elf_section_data (sec);
630993ec 7432 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7433 &elf_hash_table (info)->merge_info,
7434 sec, &secdata->sec_info))
7435 return FALSE;
7436 else if (secdata->sec_info)
dbaa2011 7437 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7438 }
7439
7440 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7441 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7442 merge_sections_remove_hook);
7443 return TRUE;
7444}
7445
7446/* Create an entry in an ELF linker hash table. */
7447
7448struct bfd_hash_entry *
7449_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7450 struct bfd_hash_table *table,
7451 const char *string)
7452{
7453 /* Allocate the structure if it has not already been allocated by a
7454 subclass. */
7455 if (entry == NULL)
7456 {
a50b1753 7457 entry = (struct bfd_hash_entry *)
ca4be51c 7458 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7459 if (entry == NULL)
7460 return entry;
7461 }
7462
7463 /* Call the allocation method of the superclass. */
7464 entry = _bfd_link_hash_newfunc (entry, table, string);
7465 if (entry != NULL)
7466 {
7467 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7468 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7469
7470 /* Set local fields. */
7471 ret->indx = -1;
7472 ret->dynindx = -1;
7473 ret->got = htab->init_got_refcount;
7474 ret->plt = htab->init_plt_refcount;
7475 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7476 - offsetof (struct elf_link_hash_entry, size)));
7477 /* Assume that we have been called by a non-ELF symbol reader.
7478 This flag is then reset by the code which reads an ELF input
7479 file. This ensures that a symbol created by a non-ELF symbol
7480 reader will have the flag set correctly. */
7481 ret->non_elf = 1;
7482 }
7483
7484 return entry;
7485}
7486
7487/* Copy data from an indirect symbol to its direct symbol, hiding the
7488 old indirect symbol. Also used for copying flags to a weakdef. */
7489
7490void
7491_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7492 struct elf_link_hash_entry *dir,
7493 struct elf_link_hash_entry *ind)
7494{
7495 struct elf_link_hash_table *htab;
7496
7497 /* Copy down any references that we may have already seen to the
e81830c5 7498 symbol which just became indirect. */
4d269e42 7499
422f1182 7500 if (dir->versioned != versioned_hidden)
e81830c5
AM
7501 dir->ref_dynamic |= ind->ref_dynamic;
7502 dir->ref_regular |= ind->ref_regular;
7503 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7504 dir->non_got_ref |= ind->non_got_ref;
7505 dir->needs_plt |= ind->needs_plt;
7506 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7507
7508 if (ind->root.type != bfd_link_hash_indirect)
7509 return;
7510
7511 /* Copy over the global and procedure linkage table refcount entries.
7512 These may have been already set up by a check_relocs routine. */
7513 htab = elf_hash_table (info);
7514 if (ind->got.refcount > htab->init_got_refcount.refcount)
7515 {
7516 if (dir->got.refcount < 0)
7517 dir->got.refcount = 0;
7518 dir->got.refcount += ind->got.refcount;
7519 ind->got.refcount = htab->init_got_refcount.refcount;
7520 }
7521
7522 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7523 {
7524 if (dir->plt.refcount < 0)
7525 dir->plt.refcount = 0;
7526 dir->plt.refcount += ind->plt.refcount;
7527 ind->plt.refcount = htab->init_plt_refcount.refcount;
7528 }
7529
7530 if (ind->dynindx != -1)
7531 {
7532 if (dir->dynindx != -1)
7533 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7534 dir->dynindx = ind->dynindx;
7535 dir->dynstr_index = ind->dynstr_index;
7536 ind->dynindx = -1;
7537 ind->dynstr_index = 0;
7538 }
7539}
7540
7541void
7542_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7543 struct elf_link_hash_entry *h,
7544 bfd_boolean force_local)
7545{
3aa14d16
L
7546 /* STT_GNU_IFUNC symbol must go through PLT. */
7547 if (h->type != STT_GNU_IFUNC)
7548 {
7549 h->plt = elf_hash_table (info)->init_plt_offset;
7550 h->needs_plt = 0;
7551 }
4d269e42
AM
7552 if (force_local)
7553 {
7554 h->forced_local = 1;
7555 if (h->dynindx != -1)
7556 {
4d269e42
AM
7557 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7558 h->dynstr_index);
641338d8
AM
7559 h->dynindx = -1;
7560 h->dynstr_index = 0;
4d269e42
AM
7561 }
7562 }
7563}
7564
34a87bb0
L
7565/* Hide a symbol. */
7566
7567void
7568_bfd_elf_link_hide_symbol (bfd *output_bfd,
7569 struct bfd_link_info *info,
7570 struct bfd_link_hash_entry *h)
7571{
7572 if (is_elf_hash_table (info->hash))
7573 {
7574 const struct elf_backend_data *bed
7575 = get_elf_backend_data (output_bfd);
7576 struct elf_link_hash_entry *eh
7577 = (struct elf_link_hash_entry *) h;
7578 bed->elf_backend_hide_symbol (info, eh, TRUE);
7579 eh->def_dynamic = 0;
7580 eh->ref_dynamic = 0;
7581 eh->dynamic_def = 0;
7582 }
7583}
7584
7bf52ea2
AM
7585/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7586 caller. */
4d269e42
AM
7587
7588bfd_boolean
7589_bfd_elf_link_hash_table_init
7590 (struct elf_link_hash_table *table,
7591 bfd *abfd,
7592 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7593 struct bfd_hash_table *,
7594 const char *),
4dfe6ac6
NC
7595 unsigned int entsize,
7596 enum elf_target_id target_id)
4d269e42
AM
7597{
7598 bfd_boolean ret;
7599 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7600
4d269e42
AM
7601 table->init_got_refcount.refcount = can_refcount - 1;
7602 table->init_plt_refcount.refcount = can_refcount - 1;
7603 table->init_got_offset.offset = -(bfd_vma) 1;
7604 table->init_plt_offset.offset = -(bfd_vma) 1;
7605 /* The first dynamic symbol is a dummy. */
7606 table->dynsymcount = 1;
7607
7608 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7609
4d269e42 7610 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7611 table->hash_table_id = target_id;
4d269e42
AM
7612
7613 return ret;
7614}
7615
7616/* Create an ELF linker hash table. */
7617
7618struct bfd_link_hash_table *
7619_bfd_elf_link_hash_table_create (bfd *abfd)
7620{
7621 struct elf_link_hash_table *ret;
7622 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7623
7bf52ea2 7624 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7625 if (ret == NULL)
7626 return NULL;
7627
7628 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7629 sizeof (struct elf_link_hash_entry),
7630 GENERIC_ELF_DATA))
4d269e42
AM
7631 {
7632 free (ret);
7633 return NULL;
7634 }
d495ab0d 7635 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7636
7637 return &ret->root;
7638}
7639
9f7c3e5e
AM
7640/* Destroy an ELF linker hash table. */
7641
7642void
d495ab0d 7643_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7644{
d495ab0d
AM
7645 struct elf_link_hash_table *htab;
7646
7647 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7648 if (htab->dynstr != NULL)
7649 _bfd_elf_strtab_free (htab->dynstr);
7650 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7651 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7652}
7653
4d269e42
AM
7654/* This is a hook for the ELF emulation code in the generic linker to
7655 tell the backend linker what file name to use for the DT_NEEDED
7656 entry for a dynamic object. */
7657
7658void
7659bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7660{
7661 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7662 && bfd_get_format (abfd) == bfd_object)
7663 elf_dt_name (abfd) = name;
7664}
7665
7666int
7667bfd_elf_get_dyn_lib_class (bfd *abfd)
7668{
7669 int lib_class;
7670 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7671 && bfd_get_format (abfd) == bfd_object)
7672 lib_class = elf_dyn_lib_class (abfd);
7673 else
7674 lib_class = 0;
7675 return lib_class;
7676}
7677
7678void
7679bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7680{
7681 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7682 && bfd_get_format (abfd) == bfd_object)
7683 elf_dyn_lib_class (abfd) = lib_class;
7684}
7685
7686/* Get the list of DT_NEEDED entries for a link. This is a hook for
7687 the linker ELF emulation code. */
7688
7689struct bfd_link_needed_list *
7690bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7691 struct bfd_link_info *info)
7692{
7693 if (! is_elf_hash_table (info->hash))
7694 return NULL;
7695 return elf_hash_table (info)->needed;
7696}
7697
7698/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7699 hook for the linker ELF emulation code. */
7700
7701struct bfd_link_needed_list *
7702bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7703 struct bfd_link_info *info)
7704{
7705 if (! is_elf_hash_table (info->hash))
7706 return NULL;
7707 return elf_hash_table (info)->runpath;
7708}
7709
7710/* Get the name actually used for a dynamic object for a link. This
7711 is the SONAME entry if there is one. Otherwise, it is the string
7712 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7713
7714const char *
7715bfd_elf_get_dt_soname (bfd *abfd)
7716{
7717 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7718 && bfd_get_format (abfd) == bfd_object)
7719 return elf_dt_name (abfd);
7720 return NULL;
7721}
7722
7723/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7724 the ELF linker emulation code. */
7725
7726bfd_boolean
7727bfd_elf_get_bfd_needed_list (bfd *abfd,
7728 struct bfd_link_needed_list **pneeded)
7729{
7730 asection *s;
7731 bfd_byte *dynbuf = NULL;
cb33740c 7732 unsigned int elfsec;
4d269e42
AM
7733 unsigned long shlink;
7734 bfd_byte *extdyn, *extdynend;
7735 size_t extdynsize;
7736 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7737
7738 *pneeded = NULL;
7739
7740 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7741 || bfd_get_format (abfd) != bfd_object)
7742 return TRUE;
7743
7744 s = bfd_get_section_by_name (abfd, ".dynamic");
7745 if (s == NULL || s->size == 0)
7746 return TRUE;
7747
7748 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7749 goto error_return;
7750
7751 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7752 if (elfsec == SHN_BAD)
4d269e42
AM
7753 goto error_return;
7754
7755 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7756
4d269e42
AM
7757 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7758 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7759
7760 extdyn = dynbuf;
7761 extdynend = extdyn + s->size;
7762 for (; extdyn < extdynend; extdyn += extdynsize)
7763 {
7764 Elf_Internal_Dyn dyn;
7765
7766 (*swap_dyn_in) (abfd, extdyn, &dyn);
7767
7768 if (dyn.d_tag == DT_NULL)
7769 break;
7770
7771 if (dyn.d_tag == DT_NEEDED)
7772 {
7773 const char *string;
7774 struct bfd_link_needed_list *l;
7775 unsigned int tagv = dyn.d_un.d_val;
7776 bfd_size_type amt;
7777
7778 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7779 if (string == NULL)
7780 goto error_return;
7781
7782 amt = sizeof *l;
a50b1753 7783 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7784 if (l == NULL)
7785 goto error_return;
7786
7787 l->by = abfd;
7788 l->name = string;
7789 l->next = *pneeded;
7790 *pneeded = l;
7791 }
7792 }
7793
7794 free (dynbuf);
7795
7796 return TRUE;
7797
7798 error_return:
7799 if (dynbuf != NULL)
7800 free (dynbuf);
7801 return FALSE;
7802}
7803
7804struct elf_symbuf_symbol
7805{
7806 unsigned long st_name; /* Symbol name, index in string tbl */
7807 unsigned char st_info; /* Type and binding attributes */
7808 unsigned char st_other; /* Visibilty, and target specific */
7809};
7810
7811struct elf_symbuf_head
7812{
7813 struct elf_symbuf_symbol *ssym;
ef53be89 7814 size_t count;
4d269e42
AM
7815 unsigned int st_shndx;
7816};
7817
7818struct elf_symbol
7819{
7820 union
7821 {
7822 Elf_Internal_Sym *isym;
7823 struct elf_symbuf_symbol *ssym;
7824 } u;
7825 const char *name;
7826};
7827
7828/* Sort references to symbols by ascending section number. */
7829
7830static int
7831elf_sort_elf_symbol (const void *arg1, const void *arg2)
7832{
7833 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7834 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7835
7836 return s1->st_shndx - s2->st_shndx;
7837}
7838
7839static int
7840elf_sym_name_compare (const void *arg1, const void *arg2)
7841{
7842 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7843 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7844 return strcmp (s1->name, s2->name);
7845}
7846
7847static struct elf_symbuf_head *
ef53be89 7848elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7849{
14b1c01e 7850 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7851 struct elf_symbuf_symbol *ssym;
7852 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7853 size_t i, shndx_count, total_size;
4d269e42 7854
a50b1753 7855 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7856 if (indbuf == NULL)
7857 return NULL;
7858
7859 for (ind = indbuf, i = 0; i < symcount; i++)
7860 if (isymbuf[i].st_shndx != SHN_UNDEF)
7861 *ind++ = &isymbuf[i];
7862 indbufend = ind;
7863
7864 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7865 elf_sort_elf_symbol);
7866
7867 shndx_count = 0;
7868 if (indbufend > indbuf)
7869 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7870 if (ind[0]->st_shndx != ind[1]->st_shndx)
7871 shndx_count++;
7872
3ae181ee
L
7873 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7874 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7875 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7876 if (ssymbuf == NULL)
7877 {
7878 free (indbuf);
7879 return NULL;
7880 }
7881
3ae181ee 7882 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7883 ssymbuf->ssym = NULL;
7884 ssymbuf->count = shndx_count;
7885 ssymbuf->st_shndx = 0;
7886 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7887 {
7888 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7889 {
7890 ssymhead++;
7891 ssymhead->ssym = ssym;
7892 ssymhead->count = 0;
7893 ssymhead->st_shndx = (*ind)->st_shndx;
7894 }
7895 ssym->st_name = (*ind)->st_name;
7896 ssym->st_info = (*ind)->st_info;
7897 ssym->st_other = (*ind)->st_other;
7898 ssymhead->count++;
7899 }
ef53be89 7900 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7901 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7902 == total_size));
4d269e42
AM
7903
7904 free (indbuf);
7905 return ssymbuf;
7906}
7907
7908/* Check if 2 sections define the same set of local and global
7909 symbols. */
7910
8f317e31 7911static bfd_boolean
4d269e42
AM
7912bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7913 struct bfd_link_info *info)
7914{
7915 bfd *bfd1, *bfd2;
7916 const struct elf_backend_data *bed1, *bed2;
7917 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7918 size_t symcount1, symcount2;
4d269e42
AM
7919 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7920 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7921 Elf_Internal_Sym *isym, *isymend;
7922 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7923 size_t count1, count2, i;
cb33740c 7924 unsigned int shndx1, shndx2;
4d269e42
AM
7925 bfd_boolean result;
7926
7927 bfd1 = sec1->owner;
7928 bfd2 = sec2->owner;
7929
4d269e42
AM
7930 /* Both sections have to be in ELF. */
7931 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7932 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7933 return FALSE;
7934
7935 if (elf_section_type (sec1) != elf_section_type (sec2))
7936 return FALSE;
7937
4d269e42
AM
7938 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7939 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7940 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7941 return FALSE;
7942
7943 bed1 = get_elf_backend_data (bfd1);
7944 bed2 = get_elf_backend_data (bfd2);
7945 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7946 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7947 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7948 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7949
7950 if (symcount1 == 0 || symcount2 == 0)
7951 return FALSE;
7952
7953 result = FALSE;
7954 isymbuf1 = NULL;
7955 isymbuf2 = NULL;
a50b1753
NC
7956 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7957 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7958
7959 if (ssymbuf1 == NULL)
7960 {
7961 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7962 NULL, NULL, NULL);
7963 if (isymbuf1 == NULL)
7964 goto done;
7965
7966 if (!info->reduce_memory_overheads)
7967 elf_tdata (bfd1)->symbuf = ssymbuf1
7968 = elf_create_symbuf (symcount1, isymbuf1);
7969 }
7970
7971 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7972 {
7973 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7974 NULL, NULL, NULL);
7975 if (isymbuf2 == NULL)
7976 goto done;
7977
7978 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7979 elf_tdata (bfd2)->symbuf = ssymbuf2
7980 = elf_create_symbuf (symcount2, isymbuf2);
7981 }
7982
7983 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7984 {
7985 /* Optimized faster version. */
ef53be89 7986 size_t lo, hi, mid;
4d269e42
AM
7987 struct elf_symbol *symp;
7988 struct elf_symbuf_symbol *ssym, *ssymend;
7989
7990 lo = 0;
7991 hi = ssymbuf1->count;
7992 ssymbuf1++;
7993 count1 = 0;
7994 while (lo < hi)
7995 {
7996 mid = (lo + hi) / 2;
cb33740c 7997 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7998 hi = mid;
cb33740c 7999 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8000 lo = mid + 1;
8001 else
8002 {
8003 count1 = ssymbuf1[mid].count;
8004 ssymbuf1 += mid;
8005 break;
8006 }
8007 }
8008
8009 lo = 0;
8010 hi = ssymbuf2->count;
8011 ssymbuf2++;
8012 count2 = 0;
8013 while (lo < hi)
8014 {
8015 mid = (lo + hi) / 2;
cb33740c 8016 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8017 hi = mid;
cb33740c 8018 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8019 lo = mid + 1;
8020 else
8021 {
8022 count2 = ssymbuf2[mid].count;
8023 ssymbuf2 += mid;
8024 break;
8025 }
8026 }
8027
8028 if (count1 == 0 || count2 == 0 || count1 != count2)
8029 goto done;
8030
ca4be51c
AM
8031 symtable1
8032 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8033 symtable2
8034 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8035 if (symtable1 == NULL || symtable2 == NULL)
8036 goto done;
8037
8038 symp = symtable1;
8039 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8040 ssym < ssymend; ssym++, symp++)
8041 {
8042 symp->u.ssym = ssym;
8043 symp->name = bfd_elf_string_from_elf_section (bfd1,
8044 hdr1->sh_link,
8045 ssym->st_name);
8046 }
8047
8048 symp = symtable2;
8049 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8050 ssym < ssymend; ssym++, symp++)
8051 {
8052 symp->u.ssym = ssym;
8053 symp->name = bfd_elf_string_from_elf_section (bfd2,
8054 hdr2->sh_link,
8055 ssym->st_name);
8056 }
8057
8058 /* Sort symbol by name. */
8059 qsort (symtable1, count1, sizeof (struct elf_symbol),
8060 elf_sym_name_compare);
8061 qsort (symtable2, count1, sizeof (struct elf_symbol),
8062 elf_sym_name_compare);
8063
8064 for (i = 0; i < count1; i++)
8065 /* Two symbols must have the same binding, type and name. */
8066 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8067 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8068 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8069 goto done;
8070
8071 result = TRUE;
8072 goto done;
8073 }
8074
a50b1753
NC
8075 symtable1 = (struct elf_symbol *)
8076 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8077 symtable2 = (struct elf_symbol *)
8078 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8079 if (symtable1 == NULL || symtable2 == NULL)
8080 goto done;
8081
8082 /* Count definitions in the section. */
8083 count1 = 0;
8084 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8085 if (isym->st_shndx == shndx1)
4d269e42
AM
8086 symtable1[count1++].u.isym = isym;
8087
8088 count2 = 0;
8089 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8090 if (isym->st_shndx == shndx2)
4d269e42
AM
8091 symtable2[count2++].u.isym = isym;
8092
8093 if (count1 == 0 || count2 == 0 || count1 != count2)
8094 goto done;
8095
8096 for (i = 0; i < count1; i++)
8097 symtable1[i].name
8098 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8099 symtable1[i].u.isym->st_name);
8100
8101 for (i = 0; i < count2; i++)
8102 symtable2[i].name
8103 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8104 symtable2[i].u.isym->st_name);
8105
8106 /* Sort symbol by name. */
8107 qsort (symtable1, count1, sizeof (struct elf_symbol),
8108 elf_sym_name_compare);
8109 qsort (symtable2, count1, sizeof (struct elf_symbol),
8110 elf_sym_name_compare);
8111
8112 for (i = 0; i < count1; i++)
8113 /* Two symbols must have the same binding, type and name. */
8114 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8115 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8116 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8117 goto done;
8118
8119 result = TRUE;
8120
8121done:
8122 if (symtable1)
8123 free (symtable1);
8124 if (symtable2)
8125 free (symtable2);
8126 if (isymbuf1)
8127 free (isymbuf1);
8128 if (isymbuf2)
8129 free (isymbuf2);
8130
8131 return result;
8132}
8133
8134/* Return TRUE if 2 section types are compatible. */
8135
8136bfd_boolean
8137_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8138 bfd *bbfd, const asection *bsec)
8139{
8140 if (asec == NULL
8141 || bsec == NULL
8142 || abfd->xvec->flavour != bfd_target_elf_flavour
8143 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8144 return TRUE;
8145
8146 return elf_section_type (asec) == elf_section_type (bsec);
8147}
8148\f
c152c796
AM
8149/* Final phase of ELF linker. */
8150
8151/* A structure we use to avoid passing large numbers of arguments. */
8152
8153struct elf_final_link_info
8154{
8155 /* General link information. */
8156 struct bfd_link_info *info;
8157 /* Output BFD. */
8158 bfd *output_bfd;
8159 /* Symbol string table. */
ef10c3ac 8160 struct elf_strtab_hash *symstrtab;
c152c796
AM
8161 /* .hash section. */
8162 asection *hash_sec;
8163 /* symbol version section (.gnu.version). */
8164 asection *symver_sec;
8165 /* Buffer large enough to hold contents of any section. */
8166 bfd_byte *contents;
8167 /* Buffer large enough to hold external relocs of any section. */
8168 void *external_relocs;
8169 /* Buffer large enough to hold internal relocs of any section. */
8170 Elf_Internal_Rela *internal_relocs;
8171 /* Buffer large enough to hold external local symbols of any input
8172 BFD. */
8173 bfd_byte *external_syms;
8174 /* And a buffer for symbol section indices. */
8175 Elf_External_Sym_Shndx *locsym_shndx;
8176 /* Buffer large enough to hold internal local symbols of any input
8177 BFD. */
8178 Elf_Internal_Sym *internal_syms;
8179 /* Array large enough to hold a symbol index for each local symbol
8180 of any input BFD. */
8181 long *indices;
8182 /* Array large enough to hold a section pointer for each local
8183 symbol of any input BFD. */
8184 asection **sections;
ef10c3ac 8185 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8186 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8187 /* Number of STT_FILE syms seen. */
8188 size_t filesym_count;
c152c796
AM
8189};
8190
8191/* This struct is used to pass information to elf_link_output_extsym. */
8192
8193struct elf_outext_info
8194{
8195 bfd_boolean failed;
8196 bfd_boolean localsyms;
34a79995 8197 bfd_boolean file_sym_done;
8b127cbc 8198 struct elf_final_link_info *flinfo;
c152c796
AM
8199};
8200
d9352518
DB
8201
8202/* Support for evaluating a complex relocation.
8203
8204 Complex relocations are generalized, self-describing relocations. The
8205 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8206 relocations themselves.
d9352518
DB
8207
8208 The relocations are use a reserved elf-wide relocation type code (R_RELC
8209 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8210 information (start bit, end bit, word width, etc) into the addend. This
8211 information is extracted from CGEN-generated operand tables within gas.
8212
8213 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8214 internal) representing prefix-notation expressions, including but not
8215 limited to those sorts of expressions normally encoded as addends in the
8216 addend field. The symbol mangling format is:
8217
8218 <node> := <literal>
07d6d2b8
AM
8219 | <unary-operator> ':' <node>
8220 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8221 ;
8222
8223 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8224 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8225 | '#' <hexdigits>
8226 ;
8227
8228 <binary-operator> := as in C
8229 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8230
8231static void
a0c8462f
AM
8232set_symbol_value (bfd *bfd_with_globals,
8233 Elf_Internal_Sym *isymbuf,
8234 size_t locsymcount,
8235 size_t symidx,
8236 bfd_vma val)
d9352518 8237{
8977835c
AM
8238 struct elf_link_hash_entry **sym_hashes;
8239 struct elf_link_hash_entry *h;
8240 size_t extsymoff = locsymcount;
d9352518 8241
8977835c 8242 if (symidx < locsymcount)
d9352518 8243 {
8977835c
AM
8244 Elf_Internal_Sym *sym;
8245
8246 sym = isymbuf + symidx;
8247 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8248 {
8249 /* It is a local symbol: move it to the
8250 "absolute" section and give it a value. */
8251 sym->st_shndx = SHN_ABS;
8252 sym->st_value = val;
8253 return;
8254 }
8255 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8256 extsymoff = 0;
d9352518 8257 }
8977835c
AM
8258
8259 /* It is a global symbol: set its link type
8260 to "defined" and give it a value. */
8261
8262 sym_hashes = elf_sym_hashes (bfd_with_globals);
8263 h = sym_hashes [symidx - extsymoff];
8264 while (h->root.type == bfd_link_hash_indirect
8265 || h->root.type == bfd_link_hash_warning)
8266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8267 h->root.type = bfd_link_hash_defined;
8268 h->root.u.def.value = val;
8269 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8270}
8271
a0c8462f
AM
8272static bfd_boolean
8273resolve_symbol (const char *name,
8274 bfd *input_bfd,
8b127cbc 8275 struct elf_final_link_info *flinfo,
a0c8462f
AM
8276 bfd_vma *result,
8277 Elf_Internal_Sym *isymbuf,
8278 size_t locsymcount)
d9352518 8279{
a0c8462f
AM
8280 Elf_Internal_Sym *sym;
8281 struct bfd_link_hash_entry *global_entry;
8282 const char *candidate = NULL;
8283 Elf_Internal_Shdr *symtab_hdr;
8284 size_t i;
8285
d9352518
DB
8286 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8287
8288 for (i = 0; i < locsymcount; ++ i)
8289 {
8977835c 8290 sym = isymbuf + i;
d9352518
DB
8291
8292 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8293 continue;
8294
8295 candidate = bfd_elf_string_from_elf_section (input_bfd,
8296 symtab_hdr->sh_link,
8297 sym->st_name);
8298#ifdef DEBUG
0f02bbd9
AM
8299 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8300 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8301#endif
8302 if (candidate && strcmp (candidate, name) == 0)
8303 {
8b127cbc 8304 asection *sec = flinfo->sections [i];
d9352518 8305
0f02bbd9
AM
8306 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8307 *result += sec->output_offset + sec->output_section->vma;
d9352518 8308#ifdef DEBUG
0f02bbd9
AM
8309 printf ("Found symbol with value %8.8lx\n",
8310 (unsigned long) *result);
d9352518
DB
8311#endif
8312 return TRUE;
8313 }
8314 }
8315
8316 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8317 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8318 FALSE, FALSE, TRUE);
d9352518
DB
8319 if (!global_entry)
8320 return FALSE;
a0c8462f 8321
d9352518
DB
8322 if (global_entry->type == bfd_link_hash_defined
8323 || global_entry->type == bfd_link_hash_defweak)
8324 {
a0c8462f
AM
8325 *result = (global_entry->u.def.value
8326 + global_entry->u.def.section->output_section->vma
8327 + global_entry->u.def.section->output_offset);
d9352518 8328#ifdef DEBUG
0f02bbd9
AM
8329 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8330 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8331#endif
8332 return TRUE;
a0c8462f 8333 }
d9352518 8334
d9352518
DB
8335 return FALSE;
8336}
8337
37b01f6a
DG
8338/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8339 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8340 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8341
d9352518 8342static bfd_boolean
a0c8462f
AM
8343resolve_section (const char *name,
8344 asection *sections,
37b01f6a
DG
8345 bfd_vma *result,
8346 bfd * abfd)
d9352518 8347{
a0c8462f
AM
8348 asection *curr;
8349 unsigned int len;
d9352518 8350
a0c8462f 8351 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8352 if (strcmp (curr->name, name) == 0)
8353 {
8354 *result = curr->vma;
8355 return TRUE;
8356 }
8357
8358 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8359 /* FIXME: This could be coded more efficiently... */
a0c8462f 8360 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8361 {
8362 len = strlen (curr->name);
a0c8462f 8363 if (len > strlen (name))
d9352518
DB
8364 continue;
8365
8366 if (strncmp (curr->name, name, len) == 0)
8367 {
8368 if (strncmp (".end", name + len, 4) == 0)
8369 {
37b01f6a 8370 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8371 return TRUE;
8372 }
8373
8374 /* Insert more pseudo-section names here, if you like. */
8375 }
8376 }
a0c8462f 8377
d9352518
DB
8378 return FALSE;
8379}
8380
8381static void
a0c8462f 8382undefined_reference (const char *reftype, const char *name)
d9352518 8383{
695344c0 8384 /* xgettext:c-format */
a0c8462f
AM
8385 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8386 reftype, name);
d9352518
DB
8387}
8388
8389static bfd_boolean
a0c8462f
AM
8390eval_symbol (bfd_vma *result,
8391 const char **symp,
8392 bfd *input_bfd,
8b127cbc 8393 struct elf_final_link_info *flinfo,
a0c8462f
AM
8394 bfd_vma dot,
8395 Elf_Internal_Sym *isymbuf,
8396 size_t locsymcount,
8397 int signed_p)
d9352518 8398{
4b93929b
NC
8399 size_t len;
8400 size_t symlen;
a0c8462f
AM
8401 bfd_vma a;
8402 bfd_vma b;
4b93929b 8403 char symbuf[4096];
0f02bbd9 8404 const char *sym = *symp;
a0c8462f
AM
8405 const char *symend;
8406 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8407
8408 len = strlen (sym);
8409 symend = sym + len;
8410
4b93929b 8411 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8412 {
8413 bfd_set_error (bfd_error_invalid_operation);
8414 return FALSE;
8415 }
a0c8462f 8416
d9352518
DB
8417 switch (* sym)
8418 {
8419 case '.':
0f02bbd9
AM
8420 *result = dot;
8421 *symp = sym + 1;
d9352518
DB
8422 return TRUE;
8423
8424 case '#':
0f02bbd9
AM
8425 ++sym;
8426 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8427 return TRUE;
8428
8429 case 'S':
8430 symbol_is_section = TRUE;
1a0670f3 8431 /* Fall through. */
a0c8462f 8432 case 's':
0f02bbd9
AM
8433 ++sym;
8434 symlen = strtol (sym, (char **) symp, 10);
8435 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8436
4b93929b 8437 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8438 {
8439 bfd_set_error (bfd_error_invalid_operation);
8440 return FALSE;
8441 }
8442
8443 memcpy (symbuf, sym, symlen);
a0c8462f 8444 symbuf[symlen] = '\0';
0f02bbd9 8445 *symp = sym + symlen;
a0c8462f
AM
8446
8447 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8448 the symbol as a section, or vice-versa. so we're pretty liberal in our
8449 interpretation here; section means "try section first", not "must be a
8450 section", and likewise with symbol. */
8451
a0c8462f 8452 if (symbol_is_section)
d9352518 8453 {
37b01f6a 8454 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8455 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8456 isymbuf, locsymcount))
d9352518
DB
8457 {
8458 undefined_reference ("section", symbuf);
8459 return FALSE;
8460 }
a0c8462f
AM
8461 }
8462 else
d9352518 8463 {
8b127cbc 8464 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8465 isymbuf, locsymcount)
8b127cbc 8466 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8467 result, input_bfd))
d9352518
DB
8468 {
8469 undefined_reference ("symbol", symbuf);
8470 return FALSE;
8471 }
8472 }
8473
8474 return TRUE;
a0c8462f 8475
d9352518
DB
8476 /* All that remains are operators. */
8477
8478#define UNARY_OP(op) \
8479 if (strncmp (sym, #op, strlen (#op)) == 0) \
8480 { \
8481 sym += strlen (#op); \
a0c8462f
AM
8482 if (*sym == ':') \
8483 ++sym; \
0f02bbd9 8484 *symp = sym; \
8b127cbc 8485 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8486 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8487 return FALSE; \
8488 if (signed_p) \
0f02bbd9 8489 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8490 else \
8491 *result = op a; \
d9352518
DB
8492 return TRUE; \
8493 }
8494
8495#define BINARY_OP(op) \
8496 if (strncmp (sym, #op, strlen (#op)) == 0) \
8497 { \
8498 sym += strlen (#op); \
a0c8462f
AM
8499 if (*sym == ':') \
8500 ++sym; \
0f02bbd9 8501 *symp = sym; \
8b127cbc 8502 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8503 isymbuf, locsymcount, signed_p)) \
a0c8462f 8504 return FALSE; \
0f02bbd9 8505 ++*symp; \
8b127cbc 8506 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8507 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8508 return FALSE; \
8509 if (signed_p) \
0f02bbd9 8510 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8511 else \
8512 *result = a op b; \
d9352518
DB
8513 return TRUE; \
8514 }
8515
8516 default:
8517 UNARY_OP (0-);
8518 BINARY_OP (<<);
8519 BINARY_OP (>>);
8520 BINARY_OP (==);
8521 BINARY_OP (!=);
8522 BINARY_OP (<=);
8523 BINARY_OP (>=);
8524 BINARY_OP (&&);
8525 BINARY_OP (||);
8526 UNARY_OP (~);
8527 UNARY_OP (!);
8528 BINARY_OP (*);
8529 BINARY_OP (/);
8530 BINARY_OP (%);
8531 BINARY_OP (^);
8532 BINARY_OP (|);
8533 BINARY_OP (&);
8534 BINARY_OP (+);
8535 BINARY_OP (-);
8536 BINARY_OP (<);
8537 BINARY_OP (>);
8538#undef UNARY_OP
8539#undef BINARY_OP
8540 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8541 bfd_set_error (bfd_error_invalid_operation);
8542 return FALSE;
8543 }
8544}
8545
d9352518 8546static void
a0c8462f
AM
8547put_value (bfd_vma size,
8548 unsigned long chunksz,
8549 bfd *input_bfd,
8550 bfd_vma x,
8551 bfd_byte *location)
d9352518
DB
8552{
8553 location += (size - chunksz);
8554
41cd1ad1 8555 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8556 {
8557 switch (chunksz)
8558 {
d9352518
DB
8559 case 1:
8560 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8561 x >>= 8;
d9352518
DB
8562 break;
8563 case 2:
8564 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8565 x >>= 16;
d9352518
DB
8566 break;
8567 case 4:
8568 bfd_put_32 (input_bfd, x, location);
65164438
NC
8569 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8570 x >>= 16;
8571 x >>= 16;
d9352518 8572 break;
d9352518 8573#ifdef BFD64
41cd1ad1 8574 case 8:
d9352518 8575 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8576 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8577 x >>= 32;
8578 x >>= 32;
8579 break;
d9352518 8580#endif
41cd1ad1
NC
8581 default:
8582 abort ();
d9352518
DB
8583 break;
8584 }
8585 }
8586}
8587
a0c8462f
AM
8588static bfd_vma
8589get_value (bfd_vma size,
8590 unsigned long chunksz,
8591 bfd *input_bfd,
8592 bfd_byte *location)
d9352518 8593{
9b239e0e 8594 int shift;
d9352518
DB
8595 bfd_vma x = 0;
8596
9b239e0e
NC
8597 /* Sanity checks. */
8598 BFD_ASSERT (chunksz <= sizeof (x)
8599 && size >= chunksz
8600 && chunksz != 0
8601 && (size % chunksz) == 0
8602 && input_bfd != NULL
8603 && location != NULL);
8604
8605 if (chunksz == sizeof (x))
8606 {
8607 BFD_ASSERT (size == chunksz);
8608
8609 /* Make sure that we do not perform an undefined shift operation.
8610 We know that size == chunksz so there will only be one iteration
8611 of the loop below. */
8612 shift = 0;
8613 }
8614 else
8615 shift = 8 * chunksz;
8616
a0c8462f 8617 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8618 {
8619 switch (chunksz)
8620 {
d9352518 8621 case 1:
9b239e0e 8622 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8623 break;
8624 case 2:
9b239e0e 8625 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8626 break;
8627 case 4:
9b239e0e 8628 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8629 break;
d9352518 8630#ifdef BFD64
9b239e0e
NC
8631 case 8:
8632 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8633 break;
9b239e0e
NC
8634#endif
8635 default:
8636 abort ();
d9352518
DB
8637 }
8638 }
8639 return x;
8640}
8641
a0c8462f
AM
8642static void
8643decode_complex_addend (unsigned long *start, /* in bits */
8644 unsigned long *oplen, /* in bits */
8645 unsigned long *len, /* in bits */
8646 unsigned long *wordsz, /* in bytes */
8647 unsigned long *chunksz, /* in bytes */
8648 unsigned long *lsb0_p,
8649 unsigned long *signed_p,
8650 unsigned long *trunc_p,
8651 unsigned long encoded)
d9352518 8652{
07d6d2b8
AM
8653 * start = encoded & 0x3F;
8654 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8655 * oplen = (encoded >> 12) & 0x3F;
8656 * wordsz = (encoded >> 18) & 0xF;
8657 * chunksz = (encoded >> 22) & 0xF;
8658 * lsb0_p = (encoded >> 27) & 1;
8659 * signed_p = (encoded >> 28) & 1;
8660 * trunc_p = (encoded >> 29) & 1;
8661}
8662
cdfeee4f 8663bfd_reloc_status_type
0f02bbd9 8664bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8665 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8666 bfd_byte *contents,
8667 Elf_Internal_Rela *rel,
8668 bfd_vma relocation)
d9352518 8669{
0f02bbd9
AM
8670 bfd_vma shift, x, mask;
8671 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8672 bfd_reloc_status_type r;
d9352518
DB
8673
8674 /* Perform this reloc, since it is complex.
8675 (this is not to say that it necessarily refers to a complex
8676 symbol; merely that it is a self-describing CGEN based reloc.
8677 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8678 word size, etc) encoded within it.). */
d9352518 8679
a0c8462f
AM
8680 decode_complex_addend (&start, &oplen, &len, &wordsz,
8681 &chunksz, &lsb0_p, &signed_p,
8682 &trunc_p, rel->r_addend);
d9352518
DB
8683
8684 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8685
8686 if (lsb0_p)
8687 shift = (start + 1) - len;
8688 else
8689 shift = (8 * wordsz) - (start + len);
8690
37b01f6a
DG
8691 x = get_value (wordsz, chunksz, input_bfd,
8692 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8693
8694#ifdef DEBUG
8695 printf ("Doing complex reloc: "
8696 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8697 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8698 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8699 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8700 oplen, (unsigned long) x, (unsigned long) mask,
8701 (unsigned long) relocation);
d9352518
DB
8702#endif
8703
cdfeee4f 8704 r = bfd_reloc_ok;
d9352518 8705 if (! trunc_p)
cdfeee4f
AM
8706 /* Now do an overflow check. */
8707 r = bfd_check_overflow ((signed_p
8708 ? complain_overflow_signed
8709 : complain_overflow_unsigned),
8710 len, 0, (8 * wordsz),
8711 relocation);
a0c8462f 8712
d9352518
DB
8713 /* Do the deed. */
8714 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8715
8716#ifdef DEBUG
8717 printf (" relocation: %8.8lx\n"
8718 " shifted mask: %8.8lx\n"
8719 " shifted/masked reloc: %8.8lx\n"
8720 " result: %8.8lx\n",
9ccb8af9
AM
8721 (unsigned long) relocation, (unsigned long) (mask << shift),
8722 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8723#endif
37b01f6a
DG
8724 put_value (wordsz, chunksz, input_bfd, x,
8725 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8726 return r;
d9352518
DB
8727}
8728
0e287786
AM
8729/* Functions to read r_offset from external (target order) reloc
8730 entry. Faster than bfd_getl32 et al, because we let the compiler
8731 know the value is aligned. */
53df40a4 8732
0e287786
AM
8733static bfd_vma
8734ext32l_r_offset (const void *p)
53df40a4
AM
8735{
8736 union aligned32
8737 {
8738 uint32_t v;
8739 unsigned char c[4];
8740 };
8741 const union aligned32 *a
0e287786 8742 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8743
8744 uint32_t aval = ( (uint32_t) a->c[0]
8745 | (uint32_t) a->c[1] << 8
8746 | (uint32_t) a->c[2] << 16
8747 | (uint32_t) a->c[3] << 24);
0e287786 8748 return aval;
53df40a4
AM
8749}
8750
0e287786
AM
8751static bfd_vma
8752ext32b_r_offset (const void *p)
53df40a4
AM
8753{
8754 union aligned32
8755 {
8756 uint32_t v;
8757 unsigned char c[4];
8758 };
8759 const union aligned32 *a
0e287786 8760 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8761
8762 uint32_t aval = ( (uint32_t) a->c[0] << 24
8763 | (uint32_t) a->c[1] << 16
8764 | (uint32_t) a->c[2] << 8
8765 | (uint32_t) a->c[3]);
0e287786 8766 return aval;
53df40a4
AM
8767}
8768
8769#ifdef BFD_HOST_64_BIT
0e287786
AM
8770static bfd_vma
8771ext64l_r_offset (const void *p)
53df40a4
AM
8772{
8773 union aligned64
8774 {
8775 uint64_t v;
8776 unsigned char c[8];
8777 };
8778 const union aligned64 *a
0e287786 8779 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8780
8781 uint64_t aval = ( (uint64_t) a->c[0]
8782 | (uint64_t) a->c[1] << 8
8783 | (uint64_t) a->c[2] << 16
8784 | (uint64_t) a->c[3] << 24
8785 | (uint64_t) a->c[4] << 32
8786 | (uint64_t) a->c[5] << 40
8787 | (uint64_t) a->c[6] << 48
8788 | (uint64_t) a->c[7] << 56);
0e287786 8789 return aval;
53df40a4
AM
8790}
8791
0e287786
AM
8792static bfd_vma
8793ext64b_r_offset (const void *p)
53df40a4
AM
8794{
8795 union aligned64
8796 {
8797 uint64_t v;
8798 unsigned char c[8];
8799 };
8800 const union aligned64 *a
0e287786 8801 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8802
8803 uint64_t aval = ( (uint64_t) a->c[0] << 56
8804 | (uint64_t) a->c[1] << 48
8805 | (uint64_t) a->c[2] << 40
8806 | (uint64_t) a->c[3] << 32
8807 | (uint64_t) a->c[4] << 24
8808 | (uint64_t) a->c[5] << 16
8809 | (uint64_t) a->c[6] << 8
8810 | (uint64_t) a->c[7]);
0e287786 8811 return aval;
53df40a4
AM
8812}
8813#endif
8814
c152c796
AM
8815/* When performing a relocatable link, the input relocations are
8816 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8817 referenced must be updated. Update all the relocations found in
8818 RELDATA. */
c152c796 8819
bca6d0e3 8820static bfd_boolean
c152c796 8821elf_link_adjust_relocs (bfd *abfd,
9eaff861 8822 asection *sec,
28dbcedc 8823 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8824 bfd_boolean sort,
8825 struct bfd_link_info *info)
c152c796
AM
8826{
8827 unsigned int i;
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8829 bfd_byte *erela;
8830 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8831 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8832 bfd_vma r_type_mask;
8833 int r_sym_shift;
d4730f92
BS
8834 unsigned int count = reldata->count;
8835 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8836
d4730f92 8837 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8838 {
8839 swap_in = bed->s->swap_reloc_in;
8840 swap_out = bed->s->swap_reloc_out;
8841 }
d4730f92 8842 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8843 {
8844 swap_in = bed->s->swap_reloca_in;
8845 swap_out = bed->s->swap_reloca_out;
8846 }
8847 else
8848 abort ();
8849
8850 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8851 abort ();
8852
8853 if (bed->s->arch_size == 32)
8854 {
8855 r_type_mask = 0xff;
8856 r_sym_shift = 8;
8857 }
8858 else
8859 {
8860 r_type_mask = 0xffffffff;
8861 r_sym_shift = 32;
8862 }
8863
d4730f92
BS
8864 erela = reldata->hdr->contents;
8865 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8866 {
8867 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8868 unsigned int j;
8869
8870 if (*rel_hash == NULL)
8871 continue;
8872
10bbbc1d
NC
8873 if ((*rel_hash)->indx == -2
8874 && info->gc_sections
8875 && ! info->gc_keep_exported)
8876 {
8877 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8878 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8879 abfd, sec,
8880 (*rel_hash)->root.root.string);
9793eb77 8881 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8882 abfd, sec);
10bbbc1d
NC
8883 bfd_set_error (bfd_error_invalid_operation);
8884 return FALSE;
8885 }
c152c796
AM
8886 BFD_ASSERT ((*rel_hash)->indx >= 0);
8887
8888 (*swap_in) (abfd, erela, irela);
8889 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8890 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8891 | (irela[j].r_info & r_type_mask));
8892 (*swap_out) (abfd, irela, erela);
8893 }
53df40a4 8894
9eaff861
AO
8895 if (bed->elf_backend_update_relocs)
8896 (*bed->elf_backend_update_relocs) (sec, reldata);
8897
0e287786 8898 if (sort && count != 0)
53df40a4 8899 {
0e287786
AM
8900 bfd_vma (*ext_r_off) (const void *);
8901 bfd_vma r_off;
8902 size_t elt_size;
8903 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8904 bfd_byte *buf = NULL;
28dbcedc
AM
8905
8906 if (bed->s->arch_size == 32)
8907 {
8908 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8909 ext_r_off = ext32l_r_offset;
28dbcedc 8910 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8911 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8912 else
8913 abort ();
8914 }
53df40a4 8915 else
28dbcedc 8916 {
53df40a4 8917#ifdef BFD_HOST_64_BIT
28dbcedc 8918 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8919 ext_r_off = ext64l_r_offset;
28dbcedc 8920 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8921 ext_r_off = ext64b_r_offset;
28dbcedc 8922 else
53df40a4 8923#endif
28dbcedc
AM
8924 abort ();
8925 }
0e287786 8926
bca6d0e3
AM
8927 /* Must use a stable sort here. A modified insertion sort,
8928 since the relocs are mostly sorted already. */
0e287786
AM
8929 elt_size = reldata->hdr->sh_entsize;
8930 base = reldata->hdr->contents;
8931 end = base + count * elt_size;
bca6d0e3 8932 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8933 abort ();
8934
8935 /* Ensure the first element is lowest. This acts as a sentinel,
8936 speeding the main loop below. */
8937 r_off = (*ext_r_off) (base);
8938 for (p = loc = base; (p += elt_size) < end; )
8939 {
8940 bfd_vma r_off2 = (*ext_r_off) (p);
8941 if (r_off > r_off2)
8942 {
8943 r_off = r_off2;
8944 loc = p;
8945 }
8946 }
8947 if (loc != base)
8948 {
8949 /* Don't just swap *base and *loc as that changes the order
8950 of the original base[0] and base[1] if they happen to
8951 have the same r_offset. */
bca6d0e3
AM
8952 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8953 memcpy (onebuf, loc, elt_size);
0e287786 8954 memmove (base + elt_size, base, loc - base);
bca6d0e3 8955 memcpy (base, onebuf, elt_size);
0e287786
AM
8956 }
8957
b29b8669 8958 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8959 {
8960 /* base to p is sorted, *p is next to insert. */
8961 r_off = (*ext_r_off) (p);
8962 /* Search the sorted region for location to insert. */
8963 loc = p - elt_size;
8964 while (r_off < (*ext_r_off) (loc))
8965 loc -= elt_size;
8966 loc += elt_size;
8967 if (loc != p)
8968 {
bca6d0e3
AM
8969 /* Chances are there is a run of relocs to insert here,
8970 from one of more input files. Files are not always
8971 linked in order due to the way elf_link_input_bfd is
8972 called. See pr17666. */
8973 size_t sortlen = p - loc;
8974 bfd_vma r_off2 = (*ext_r_off) (loc);
8975 size_t runlen = elt_size;
8976 size_t buf_size = 96 * 1024;
8977 while (p + runlen < end
8978 && (sortlen <= buf_size
8979 || runlen + elt_size <= buf_size)
8980 && r_off2 > (*ext_r_off) (p + runlen))
8981 runlen += elt_size;
8982 if (buf == NULL)
8983 {
8984 buf = bfd_malloc (buf_size);
8985 if (buf == NULL)
8986 return FALSE;
8987 }
8988 if (runlen < sortlen)
8989 {
8990 memcpy (buf, p, runlen);
8991 memmove (loc + runlen, loc, sortlen);
8992 memcpy (loc, buf, runlen);
8993 }
8994 else
8995 {
8996 memcpy (buf, loc, sortlen);
8997 memmove (loc, p, runlen);
8998 memcpy (loc + runlen, buf, sortlen);
8999 }
b29b8669 9000 p += runlen - elt_size;
0e287786
AM
9001 }
9002 }
9003 /* Hashes are no longer valid. */
28dbcedc
AM
9004 free (reldata->hashes);
9005 reldata->hashes = NULL;
bca6d0e3 9006 free (buf);
53df40a4 9007 }
bca6d0e3 9008 return TRUE;
c152c796
AM
9009}
9010
9011struct elf_link_sort_rela
9012{
9013 union {
9014 bfd_vma offset;
9015 bfd_vma sym_mask;
9016 } u;
9017 enum elf_reloc_type_class type;
9018 /* We use this as an array of size int_rels_per_ext_rel. */
9019 Elf_Internal_Rela rela[1];
9020};
9021
9022static int
9023elf_link_sort_cmp1 (const void *A, const void *B)
9024{
a50b1753
NC
9025 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9026 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9027 int relativea, relativeb;
9028
9029 relativea = a->type == reloc_class_relative;
9030 relativeb = b->type == reloc_class_relative;
9031
9032 if (relativea < relativeb)
9033 return 1;
9034 if (relativea > relativeb)
9035 return -1;
9036 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9037 return -1;
9038 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9039 return 1;
9040 if (a->rela->r_offset < b->rela->r_offset)
9041 return -1;
9042 if (a->rela->r_offset > b->rela->r_offset)
9043 return 1;
9044 return 0;
9045}
9046
9047static int
9048elf_link_sort_cmp2 (const void *A, const void *B)
9049{
a50b1753
NC
9050 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9051 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9052
7e612e98 9053 if (a->type < b->type)
c152c796 9054 return -1;
7e612e98 9055 if (a->type > b->type)
c152c796 9056 return 1;
7e612e98 9057 if (a->u.offset < b->u.offset)
c152c796 9058 return -1;
7e612e98 9059 if (a->u.offset > b->u.offset)
c152c796
AM
9060 return 1;
9061 if (a->rela->r_offset < b->rela->r_offset)
9062 return -1;
9063 if (a->rela->r_offset > b->rela->r_offset)
9064 return 1;
9065 return 0;
9066}
9067
9068static size_t
9069elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9070{
3410fea8 9071 asection *dynamic_relocs;
fc66a176
L
9072 asection *rela_dyn;
9073 asection *rel_dyn;
c152c796
AM
9074 bfd_size_type count, size;
9075 size_t i, ret, sort_elt, ext_size;
9076 bfd_byte *sort, *s_non_relative, *p;
9077 struct elf_link_sort_rela *sq;
9078 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9079 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9080 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9081 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9082 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9083 struct bfd_link_order *lo;
9084 bfd_vma r_sym_mask;
3410fea8 9085 bfd_boolean use_rela;
c152c796 9086
3410fea8
NC
9087 /* Find a dynamic reloc section. */
9088 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9089 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9090 if (rela_dyn != NULL && rela_dyn->size > 0
9091 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9092 {
3410fea8
NC
9093 bfd_boolean use_rela_initialised = FALSE;
9094
9095 /* This is just here to stop gcc from complaining.
c8e44c6d 9096 Its initialization checking code is not perfect. */
3410fea8
NC
9097 use_rela = TRUE;
9098
9099 /* Both sections are present. Examine the sizes
9100 of the indirect sections to help us choose. */
9101 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9102 if (lo->type == bfd_indirect_link_order)
9103 {
9104 asection *o = lo->u.indirect.section;
9105
9106 if ((o->size % bed->s->sizeof_rela) == 0)
9107 {
9108 if ((o->size % bed->s->sizeof_rel) == 0)
9109 /* Section size is divisible by both rel and rela sizes.
9110 It is of no help to us. */
9111 ;
9112 else
9113 {
9114 /* Section size is only divisible by rela. */
535b785f 9115 if (use_rela_initialised && !use_rela)
3410fea8 9116 {
9793eb77 9117 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9118 "they are in more than one size"),
9119 abfd);
3410fea8
NC
9120 bfd_set_error (bfd_error_invalid_operation);
9121 return 0;
9122 }
9123 else
9124 {
9125 use_rela = TRUE;
9126 use_rela_initialised = TRUE;
9127 }
9128 }
9129 }
9130 else if ((o->size % bed->s->sizeof_rel) == 0)
9131 {
9132 /* Section size is only divisible by rel. */
535b785f 9133 if (use_rela_initialised && use_rela)
3410fea8 9134 {
9793eb77 9135 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9136 "they are in more than one size"),
9137 abfd);
3410fea8
NC
9138 bfd_set_error (bfd_error_invalid_operation);
9139 return 0;
9140 }
9141 else
9142 {
9143 use_rela = FALSE;
9144 use_rela_initialised = TRUE;
9145 }
9146 }
9147 else
9148 {
c8e44c6d
AM
9149 /* The section size is not divisible by either -
9150 something is wrong. */
9793eb77 9151 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9152 "they are of an unknown size"), abfd);
3410fea8
NC
9153 bfd_set_error (bfd_error_invalid_operation);
9154 return 0;
9155 }
9156 }
9157
9158 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9159 if (lo->type == bfd_indirect_link_order)
9160 {
9161 asection *o = lo->u.indirect.section;
9162
9163 if ((o->size % bed->s->sizeof_rela) == 0)
9164 {
9165 if ((o->size % bed->s->sizeof_rel) == 0)
9166 /* Section size is divisible by both rel and rela sizes.
9167 It is of no help to us. */
9168 ;
9169 else
9170 {
9171 /* Section size is only divisible by rela. */
535b785f 9172 if (use_rela_initialised && !use_rela)
3410fea8 9173 {
9793eb77 9174 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9175 "they are in more than one size"),
9176 abfd);
3410fea8
NC
9177 bfd_set_error (bfd_error_invalid_operation);
9178 return 0;
9179 }
9180 else
9181 {
9182 use_rela = TRUE;
9183 use_rela_initialised = TRUE;
9184 }
9185 }
9186 }
9187 else if ((o->size % bed->s->sizeof_rel) == 0)
9188 {
9189 /* Section size is only divisible by rel. */
535b785f 9190 if (use_rela_initialised && use_rela)
3410fea8 9191 {
9793eb77 9192 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9193 "they are in more than one size"),
9194 abfd);
3410fea8
NC
9195 bfd_set_error (bfd_error_invalid_operation);
9196 return 0;
9197 }
9198 else
9199 {
9200 use_rela = FALSE;
9201 use_rela_initialised = TRUE;
9202 }
9203 }
9204 else
9205 {
c8e44c6d
AM
9206 /* The section size is not divisible by either -
9207 something is wrong. */
9793eb77 9208 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9209 "they are of an unknown size"), abfd);
3410fea8
NC
9210 bfd_set_error (bfd_error_invalid_operation);
9211 return 0;
9212 }
9213 }
9214
9215 if (! use_rela_initialised)
9216 /* Make a guess. */
9217 use_rela = TRUE;
c152c796 9218 }
fc66a176
L
9219 else if (rela_dyn != NULL && rela_dyn->size > 0)
9220 use_rela = TRUE;
9221 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9222 use_rela = FALSE;
c152c796 9223 else
fc66a176 9224 return 0;
3410fea8
NC
9225
9226 if (use_rela)
c152c796 9227 {
3410fea8 9228 dynamic_relocs = rela_dyn;
c152c796
AM
9229 ext_size = bed->s->sizeof_rela;
9230 swap_in = bed->s->swap_reloca_in;
9231 swap_out = bed->s->swap_reloca_out;
9232 }
3410fea8
NC
9233 else
9234 {
9235 dynamic_relocs = rel_dyn;
9236 ext_size = bed->s->sizeof_rel;
9237 swap_in = bed->s->swap_reloc_in;
9238 swap_out = bed->s->swap_reloc_out;
9239 }
c152c796
AM
9240
9241 size = 0;
3410fea8 9242 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9243 if (lo->type == bfd_indirect_link_order)
3410fea8 9244 size += lo->u.indirect.section->size;
c152c796 9245
3410fea8 9246 if (size != dynamic_relocs->size)
c152c796
AM
9247 return 0;
9248
9249 sort_elt = (sizeof (struct elf_link_sort_rela)
9250 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9251
9252 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9253 if (count == 0)
9254 return 0;
a50b1753 9255 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9256
c152c796
AM
9257 if (sort == NULL)
9258 {
9259 (*info->callbacks->warning)
9793eb77 9260 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9261 return 0;
9262 }
9263
9264 if (bed->s->arch_size == 32)
9265 r_sym_mask = ~(bfd_vma) 0xff;
9266 else
9267 r_sym_mask = ~(bfd_vma) 0xffffffff;
9268
3410fea8 9269 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9270 if (lo->type == bfd_indirect_link_order)
9271 {
9272 bfd_byte *erel, *erelend;
9273 asection *o = lo->u.indirect.section;
9274
1da212d6
AM
9275 if (o->contents == NULL && o->size != 0)
9276 {
9277 /* This is a reloc section that is being handled as a normal
9278 section. See bfd_section_from_shdr. We can't combine
9279 relocs in this case. */
9280 free (sort);
9281 return 0;
9282 }
c152c796 9283 erel = o->contents;
eea6121a 9284 erelend = o->contents + o->size;
c8e44c6d 9285 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9286
c152c796
AM
9287 while (erel < erelend)
9288 {
9289 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9290
c152c796 9291 (*swap_in) (abfd, erel, s->rela);
7e612e98 9292 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9293 s->u.sym_mask = r_sym_mask;
9294 p += sort_elt;
9295 erel += ext_size;
9296 }
9297 }
9298
9299 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9300
9301 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9302 {
9303 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9304 if (s->type != reloc_class_relative)
9305 break;
9306 }
9307 ret = i;
9308 s_non_relative = p;
9309
9310 sq = (struct elf_link_sort_rela *) s_non_relative;
9311 for (; i < count; i++, p += sort_elt)
9312 {
9313 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9314 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9315 sq = sp;
9316 sp->u.offset = sq->rela->r_offset;
9317 }
9318
9319 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9320
c8e44c6d
AM
9321 struct elf_link_hash_table *htab = elf_hash_table (info);
9322 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9323 {
9324 /* We have plt relocs in .rela.dyn. */
9325 sq = (struct elf_link_sort_rela *) sort;
9326 for (i = 0; i < count; i++)
9327 if (sq[count - i - 1].type != reloc_class_plt)
9328 break;
9329 if (i != 0 && htab->srelplt->size == i * ext_size)
9330 {
9331 struct bfd_link_order **plo;
9332 /* Put srelplt link_order last. This is so the output_offset
9333 set in the next loop is correct for DT_JMPREL. */
9334 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9335 if ((*plo)->type == bfd_indirect_link_order
9336 && (*plo)->u.indirect.section == htab->srelplt)
9337 {
9338 lo = *plo;
9339 *plo = lo->next;
9340 }
9341 else
9342 plo = &(*plo)->next;
9343 *plo = lo;
9344 lo->next = NULL;
9345 dynamic_relocs->map_tail.link_order = lo;
9346 }
9347 }
9348
9349 p = sort;
3410fea8 9350 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9351 if (lo->type == bfd_indirect_link_order)
9352 {
9353 bfd_byte *erel, *erelend;
9354 asection *o = lo->u.indirect.section;
9355
9356 erel = o->contents;
eea6121a 9357 erelend = o->contents + o->size;
c8e44c6d 9358 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9359 while (erel < erelend)
9360 {
9361 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9362 (*swap_out) (abfd, s->rela, erel);
9363 p += sort_elt;
9364 erel += ext_size;
9365 }
9366 }
9367
9368 free (sort);
3410fea8 9369 *psec = dynamic_relocs;
c152c796
AM
9370 return ret;
9371}
9372
ef10c3ac 9373/* Add a symbol to the output symbol string table. */
c152c796 9374
6e0b88f1 9375static int
ef10c3ac
L
9376elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9377 const char *name,
9378 Elf_Internal_Sym *elfsym,
9379 asection *input_sec,
9380 struct elf_link_hash_entry *h)
c152c796 9381{
6e0b88f1 9382 int (*output_symbol_hook)
c152c796
AM
9383 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9384 struct elf_link_hash_entry *);
ef10c3ac 9385 struct elf_link_hash_table *hash_table;
c152c796 9386 const struct elf_backend_data *bed;
ef10c3ac 9387 bfd_size_type strtabsize;
c152c796 9388
8539e4e8
AM
9389 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9390
8b127cbc 9391 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9392 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9393 if (output_symbol_hook != NULL)
9394 {
8b127cbc 9395 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9396 if (ret != 1)
9397 return ret;
c152c796
AM
9398 }
9399
ef10c3ac
L
9400 if (name == NULL
9401 || *name == '\0'
9402 || (input_sec->flags & SEC_EXCLUDE))
9403 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9404 else
9405 {
ef10c3ac
L
9406 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9407 to get the final offset for st_name. */
9408 elfsym->st_name
9409 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9410 name, FALSE);
c152c796 9411 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9412 return 0;
c152c796
AM
9413 }
9414
ef10c3ac
L
9415 hash_table = elf_hash_table (flinfo->info);
9416 strtabsize = hash_table->strtabsize;
9417 if (strtabsize <= hash_table->strtabcount)
c152c796 9418 {
ef10c3ac
L
9419 strtabsize += strtabsize;
9420 hash_table->strtabsize = strtabsize;
9421 strtabsize *= sizeof (*hash_table->strtab);
9422 hash_table->strtab
9423 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9424 strtabsize);
9425 if (hash_table->strtab == NULL)
6e0b88f1 9426 return 0;
c152c796 9427 }
ef10c3ac
L
9428 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9429 hash_table->strtab[hash_table->strtabcount].dest_index
9430 = hash_table->strtabcount;
9431 hash_table->strtab[hash_table->strtabcount].destshndx_index
9432 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9433
9434 bfd_get_symcount (flinfo->output_bfd) += 1;
9435 hash_table->strtabcount += 1;
9436
9437 return 1;
9438}
9439
9440/* Swap symbols out to the symbol table and flush the output symbols to
9441 the file. */
9442
9443static bfd_boolean
9444elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9445{
9446 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9447 bfd_size_type amt;
9448 size_t i;
ef10c3ac
L
9449 const struct elf_backend_data *bed;
9450 bfd_byte *symbuf;
9451 Elf_Internal_Shdr *hdr;
9452 file_ptr pos;
9453 bfd_boolean ret;
9454
9455 if (!hash_table->strtabcount)
9456 return TRUE;
9457
9458 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9459
9460 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9461
ef10c3ac
L
9462 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9463 symbuf = (bfd_byte *) bfd_malloc (amt);
9464 if (symbuf == NULL)
9465 return FALSE;
1b786873 9466
ef10c3ac 9467 if (flinfo->symshndxbuf)
c152c796 9468 {
ef53be89
AM
9469 amt = sizeof (Elf_External_Sym_Shndx);
9470 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9471 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9472 if (flinfo->symshndxbuf == NULL)
c152c796 9473 {
ef10c3ac
L
9474 free (symbuf);
9475 return FALSE;
c152c796 9476 }
c152c796
AM
9477 }
9478
ef10c3ac
L
9479 for (i = 0; i < hash_table->strtabcount; i++)
9480 {
9481 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9482 if (elfsym->sym.st_name == (unsigned long) -1)
9483 elfsym->sym.st_name = 0;
9484 else
9485 elfsym->sym.st_name
9486 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9487 elfsym->sym.st_name);
9488 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9489 ((bfd_byte *) symbuf
9490 + (elfsym->dest_index
9491 * bed->s->sizeof_sym)),
9492 (flinfo->symshndxbuf
9493 + elfsym->destshndx_index));
9494 }
9495
9496 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9497 pos = hdr->sh_offset + hdr->sh_size;
9498 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9499 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9500 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9501 {
9502 hdr->sh_size += amt;
9503 ret = TRUE;
9504 }
9505 else
9506 ret = FALSE;
c152c796 9507
ef10c3ac
L
9508 free (symbuf);
9509
9510 free (hash_table->strtab);
9511 hash_table->strtab = NULL;
9512
9513 return ret;
c152c796
AM
9514}
9515
c0d5a53d
L
9516/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9517
9518static bfd_boolean
9519check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9520{
4fbb74a6
AM
9521 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9522 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9523 {
9524 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9525 beyond 64k. */
4eca0228 9526 _bfd_error_handler
695344c0 9527 /* xgettext:c-format */
9793eb77 9528 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9529 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9530 bfd_set_error (bfd_error_nonrepresentable_section);
9531 return FALSE;
9532 }
9533 return TRUE;
9534}
9535
c152c796
AM
9536/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9537 allowing an unsatisfied unversioned symbol in the DSO to match a
9538 versioned symbol that would normally require an explicit version.
9539 We also handle the case that a DSO references a hidden symbol
9540 which may be satisfied by a versioned symbol in another DSO. */
9541
9542static bfd_boolean
9543elf_link_check_versioned_symbol (struct bfd_link_info *info,
9544 const struct elf_backend_data *bed,
9545 struct elf_link_hash_entry *h)
9546{
9547 bfd *abfd;
9548 struct elf_link_loaded_list *loaded;
9549
9550 if (!is_elf_hash_table (info->hash))
9551 return FALSE;
9552
90c984fc
L
9553 /* Check indirect symbol. */
9554 while (h->root.type == bfd_link_hash_indirect)
9555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9556
c152c796
AM
9557 switch (h->root.type)
9558 {
9559 default:
9560 abfd = NULL;
9561 break;
9562
9563 case bfd_link_hash_undefined:
9564 case bfd_link_hash_undefweak:
9565 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9566 if (abfd == NULL
9567 || (abfd->flags & DYNAMIC) == 0
e56f61be 9568 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9569 return FALSE;
9570 break;
9571
9572 case bfd_link_hash_defined:
9573 case bfd_link_hash_defweak:
9574 abfd = h->root.u.def.section->owner;
9575 break;
9576
9577 case bfd_link_hash_common:
9578 abfd = h->root.u.c.p->section->owner;
9579 break;
9580 }
9581 BFD_ASSERT (abfd != NULL);
9582
9583 for (loaded = elf_hash_table (info)->loaded;
9584 loaded != NULL;
9585 loaded = loaded->next)
9586 {
9587 bfd *input;
9588 Elf_Internal_Shdr *hdr;
ef53be89
AM
9589 size_t symcount;
9590 size_t extsymcount;
9591 size_t extsymoff;
c152c796
AM
9592 Elf_Internal_Shdr *versymhdr;
9593 Elf_Internal_Sym *isym;
9594 Elf_Internal_Sym *isymend;
9595 Elf_Internal_Sym *isymbuf;
9596 Elf_External_Versym *ever;
9597 Elf_External_Versym *extversym;
9598
9599 input = loaded->abfd;
9600
9601 /* We check each DSO for a possible hidden versioned definition. */
9602 if (input == abfd
9603 || (input->flags & DYNAMIC) == 0
9604 || elf_dynversym (input) == 0)
9605 continue;
9606
9607 hdr = &elf_tdata (input)->dynsymtab_hdr;
9608
9609 symcount = hdr->sh_size / bed->s->sizeof_sym;
9610 if (elf_bad_symtab (input))
9611 {
9612 extsymcount = symcount;
9613 extsymoff = 0;
9614 }
9615 else
9616 {
9617 extsymcount = symcount - hdr->sh_info;
9618 extsymoff = hdr->sh_info;
9619 }
9620
9621 if (extsymcount == 0)
9622 continue;
9623
9624 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9625 NULL, NULL, NULL);
9626 if (isymbuf == NULL)
9627 return FALSE;
9628
9629 /* Read in any version definitions. */
9630 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9631 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9632 if (extversym == NULL)
9633 goto error_ret;
9634
9635 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9636 || (bfd_bread (extversym, versymhdr->sh_size, input)
9637 != versymhdr->sh_size))
9638 {
9639 free (extversym);
9640 error_ret:
9641 free (isymbuf);
9642 return FALSE;
9643 }
9644
9645 ever = extversym + extsymoff;
9646 isymend = isymbuf + extsymcount;
9647 for (isym = isymbuf; isym < isymend; isym++, ever++)
9648 {
9649 const char *name;
9650 Elf_Internal_Versym iver;
9651 unsigned short version_index;
9652
9653 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9654 || isym->st_shndx == SHN_UNDEF)
9655 continue;
9656
9657 name = bfd_elf_string_from_elf_section (input,
9658 hdr->sh_link,
9659 isym->st_name);
9660 if (strcmp (name, h->root.root.string) != 0)
9661 continue;
9662
9663 _bfd_elf_swap_versym_in (input, ever, &iver);
9664
d023c380
L
9665 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9666 && !(h->def_regular
9667 && h->forced_local))
c152c796
AM
9668 {
9669 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9670 have provided a definition for the undefined sym unless
9671 it is defined in a non-shared object and forced local.
9672 */
c152c796
AM
9673 abort ();
9674 }
9675
9676 version_index = iver.vs_vers & VERSYM_VERSION;
9677 if (version_index == 1 || version_index == 2)
9678 {
9679 /* This is the base or first version. We can use it. */
9680 free (extversym);
9681 free (isymbuf);
9682 return TRUE;
9683 }
9684 }
9685
9686 free (extversym);
9687 free (isymbuf);
9688 }
9689
9690 return FALSE;
9691}
9692
b8871f35
L
9693/* Convert ELF common symbol TYPE. */
9694
9695static int
9696elf_link_convert_common_type (struct bfd_link_info *info, int type)
9697{
9698 /* Commom symbol can only appear in relocatable link. */
9699 if (!bfd_link_relocatable (info))
9700 abort ();
9701 switch (info->elf_stt_common)
9702 {
9703 case unchanged:
9704 break;
9705 case elf_stt_common:
9706 type = STT_COMMON;
9707 break;
9708 case no_elf_stt_common:
9709 type = STT_OBJECT;
9710 break;
9711 }
9712 return type;
9713}
9714
c152c796
AM
9715/* Add an external symbol to the symbol table. This is called from
9716 the hash table traversal routine. When generating a shared object,
9717 we go through the symbol table twice. The first time we output
9718 anything that might have been forced to local scope in a version
9719 script. The second time we output the symbols that are still
9720 global symbols. */
9721
9722static bfd_boolean
7686d77d 9723elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9724{
7686d77d 9725 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9726 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9727 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9728 bfd_boolean strip;
9729 Elf_Internal_Sym sym;
9730 asection *input_sec;
9731 const struct elf_backend_data *bed;
6e0b88f1
AM
9732 long indx;
9733 int ret;
b8871f35 9734 unsigned int type;
c152c796
AM
9735
9736 if (h->root.type == bfd_link_hash_warning)
9737 {
9738 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9739 if (h->root.type == bfd_link_hash_new)
9740 return TRUE;
9741 }
9742
9743 /* Decide whether to output this symbol in this pass. */
9744 if (eoinfo->localsyms)
9745 {
4deb8f71 9746 if (!h->forced_local)
c152c796
AM
9747 return TRUE;
9748 }
9749 else
9750 {
4deb8f71 9751 if (h->forced_local)
c152c796
AM
9752 return TRUE;
9753 }
9754
8b127cbc 9755 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9756
12ac1cf5 9757 if (h->root.type == bfd_link_hash_undefined)
c152c796 9758 {
12ac1cf5
NC
9759 /* If we have an undefined symbol reference here then it must have
9760 come from a shared library that is being linked in. (Undefined
98da7939
L
9761 references in regular files have already been handled unless
9762 they are in unreferenced sections which are removed by garbage
9763 collection). */
12ac1cf5
NC
9764 bfd_boolean ignore_undef = FALSE;
9765
9766 /* Some symbols may be special in that the fact that they're
9767 undefined can be safely ignored - let backend determine that. */
9768 if (bed->elf_backend_ignore_undef_symbol)
9769 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9770
9771 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9772 if (!ignore_undef
12ac1cf5 9773 && h->ref_dynamic
8b127cbc
AM
9774 && (!h->ref_regular || flinfo->info->gc_sections)
9775 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9776 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9777 (*flinfo->info->callbacks->undefined_symbol)
9778 (flinfo->info, h->root.root.string,
9779 h->ref_regular ? NULL : h->root.u.undef.abfd,
9780 NULL, 0,
9781 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9782
9783 /* Strip a global symbol defined in a discarded section. */
9784 if (h->indx == -3)
9785 return TRUE;
c152c796
AM
9786 }
9787
9788 /* We should also warn if a forced local symbol is referenced from
9789 shared libraries. */
0e1862bb 9790 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9791 && h->forced_local
9792 && h->ref_dynamic
371a5866 9793 && h->def_regular
f5385ebf 9794 && !h->dynamic_def
ee659f1f 9795 && h->ref_dynamic_nonweak
8b127cbc 9796 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9797 {
17d078c5
AM
9798 bfd *def_bfd;
9799 const char *msg;
90c984fc
L
9800 struct elf_link_hash_entry *hi = h;
9801
9802 /* Check indirect symbol. */
9803 while (hi->root.type == bfd_link_hash_indirect)
9804 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9805
9806 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9807 /* xgettext:c-format */
871b3ab2 9808 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9809 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9810 /* xgettext:c-format */
871b3ab2 9811 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9812 else
695344c0 9813 /* xgettext:c-format */
871b3ab2 9814 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9815 def_bfd = flinfo->output_bfd;
90c984fc
L
9816 if (hi->root.u.def.section != bfd_abs_section_ptr)
9817 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9818 _bfd_error_handler (msg, flinfo->output_bfd,
9819 h->root.root.string, def_bfd);
17d078c5 9820 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9821 eoinfo->failed = TRUE;
9822 return FALSE;
9823 }
9824
9825 /* We don't want to output symbols that have never been mentioned by
9826 a regular file, or that we have been told to strip. However, if
9827 h->indx is set to -2, the symbol is used by a reloc and we must
9828 output it. */
d983c8c5 9829 strip = FALSE;
c152c796 9830 if (h->indx == -2)
d983c8c5 9831 ;
f5385ebf 9832 else if ((h->def_dynamic
77cfaee6
AM
9833 || h->ref_dynamic
9834 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9835 && !h->def_regular
9836 && !h->ref_regular)
c152c796 9837 strip = TRUE;
8b127cbc 9838 else if (flinfo->info->strip == strip_all)
c152c796 9839 strip = TRUE;
8b127cbc
AM
9840 else if (flinfo->info->strip == strip_some
9841 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9842 h->root.root.string, FALSE, FALSE) == NULL)
9843 strip = TRUE;
d56d55e7
AM
9844 else if ((h->root.type == bfd_link_hash_defined
9845 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9846 && ((flinfo->info->strip_discarded
dbaa2011 9847 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9848 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9849 && h->root.u.def.section->owner != NULL
d56d55e7 9850 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9851 strip = TRUE;
9e2278f5
AM
9852 else if ((h->root.type == bfd_link_hash_undefined
9853 || h->root.type == bfd_link_hash_undefweak)
9854 && h->root.u.undef.abfd != NULL
9855 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9856 strip = TRUE;
c152c796 9857
b8871f35
L
9858 type = h->type;
9859
c152c796 9860 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9861 nothing else to do. However, if it is a forced local symbol or
9862 an ifunc symbol we need to give the backend finish_dynamic_symbol
9863 function a chance to make it dynamic. */
c152c796
AM
9864 if (strip
9865 && h->dynindx == -1
b8871f35 9866 && type != STT_GNU_IFUNC
f5385ebf 9867 && !h->forced_local)
c152c796
AM
9868 return TRUE;
9869
9870 sym.st_value = 0;
9871 sym.st_size = h->size;
9872 sym.st_other = h->other;
c152c796
AM
9873 switch (h->root.type)
9874 {
9875 default:
9876 case bfd_link_hash_new:
9877 case bfd_link_hash_warning:
9878 abort ();
9879 return FALSE;
9880
9881 case bfd_link_hash_undefined:
9882 case bfd_link_hash_undefweak:
9883 input_sec = bfd_und_section_ptr;
9884 sym.st_shndx = SHN_UNDEF;
9885 break;
9886
9887 case bfd_link_hash_defined:
9888 case bfd_link_hash_defweak:
9889 {
9890 input_sec = h->root.u.def.section;
9891 if (input_sec->output_section != NULL)
9892 {
9893 sym.st_shndx =
8b127cbc 9894 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9895 input_sec->output_section);
9896 if (sym.st_shndx == SHN_BAD)
9897 {
4eca0228 9898 _bfd_error_handler
695344c0 9899 /* xgettext:c-format */
871b3ab2 9900 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9901 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9902 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9903 eoinfo->failed = TRUE;
9904 return FALSE;
9905 }
9906
9907 /* ELF symbols in relocatable files are section relative,
9908 but in nonrelocatable files they are virtual
9909 addresses. */
9910 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9911 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9912 {
9913 sym.st_value += input_sec->output_section->vma;
9914 if (h->type == STT_TLS)
9915 {
8b127cbc 9916 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9917 if (tls_sec != NULL)
9918 sym.st_value -= tls_sec->vma;
c152c796
AM
9919 }
9920 }
9921 }
9922 else
9923 {
9924 BFD_ASSERT (input_sec->owner == NULL
9925 || (input_sec->owner->flags & DYNAMIC) != 0);
9926 sym.st_shndx = SHN_UNDEF;
9927 input_sec = bfd_und_section_ptr;
9928 }
9929 }
9930 break;
9931
9932 case bfd_link_hash_common:
9933 input_sec = h->root.u.c.p->section;
a4d8e49b 9934 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9935 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9936 break;
9937
9938 case bfd_link_hash_indirect:
9939 /* These symbols are created by symbol versioning. They point
9940 to the decorated version of the name. For example, if the
9941 symbol foo@@GNU_1.2 is the default, which should be used when
9942 foo is used with no version, then we add an indirect symbol
9943 foo which points to foo@@GNU_1.2. We ignore these symbols,
9944 since the indirected symbol is already in the hash table. */
9945 return TRUE;
9946 }
9947
b8871f35
L
9948 if (type == STT_COMMON || type == STT_OBJECT)
9949 switch (h->root.type)
9950 {
9951 case bfd_link_hash_common:
9952 type = elf_link_convert_common_type (flinfo->info, type);
9953 break;
9954 case bfd_link_hash_defined:
9955 case bfd_link_hash_defweak:
9956 if (bed->common_definition (&sym))
9957 type = elf_link_convert_common_type (flinfo->info, type);
9958 else
9959 type = STT_OBJECT;
9960 break;
9961 case bfd_link_hash_undefined:
9962 case bfd_link_hash_undefweak:
9963 break;
9964 default:
9965 abort ();
9966 }
9967
4deb8f71 9968 if (h->forced_local)
b8871f35
L
9969 {
9970 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9971 /* Turn off visibility on local symbol. */
9972 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9973 }
9974 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9975 else if (h->unique_global && h->def_regular)
9976 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9977 else if (h->root.type == bfd_link_hash_undefweak
9978 || h->root.type == bfd_link_hash_defweak)
9979 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9980 else
9981 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9982 sym.st_target_internal = h->target_internal;
9983
c152c796
AM
9984 /* Give the processor backend a chance to tweak the symbol value,
9985 and also to finish up anything that needs to be done for this
9986 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9987 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9988 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9989 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9990 && h->def_regular
0e1862bb 9991 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9992 || ((h->dynindx != -1
9993 || h->forced_local)
0e1862bb 9994 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9995 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9996 || h->root.type != bfd_link_hash_undefweak))
9997 || !h->forced_local)
8b127cbc 9998 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9999 {
10000 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10001 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10002 {
10003 eoinfo->failed = TRUE;
10004 return FALSE;
10005 }
10006 }
10007
10008 /* If we are marking the symbol as undefined, and there are no
10009 non-weak references to this symbol from a regular object, then
10010 mark the symbol as weak undefined; if there are non-weak
10011 references, mark the symbol as strong. We can't do this earlier,
10012 because it might not be marked as undefined until the
10013 finish_dynamic_symbol routine gets through with it. */
10014 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10015 && h->ref_regular
c152c796
AM
10016 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10017 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10018 {
10019 int bindtype;
b8871f35 10020 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10021
10022 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10023 if (type == STT_GNU_IFUNC)
10024 type = STT_FUNC;
c152c796 10025
f5385ebf 10026 if (h->ref_regular_nonweak)
c152c796
AM
10027 bindtype = STB_GLOBAL;
10028 else
10029 bindtype = STB_WEAK;
2955ec4c 10030 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10031 }
10032
bda987c2
CD
10033 /* If this is a symbol defined in a dynamic library, don't use the
10034 symbol size from the dynamic library. Relinking an executable
10035 against a new library may introduce gratuitous changes in the
10036 executable's symbols if we keep the size. */
10037 if (sym.st_shndx == SHN_UNDEF
10038 && !h->def_regular
10039 && h->def_dynamic)
10040 sym.st_size = 0;
10041
c152c796
AM
10042 /* If a non-weak symbol with non-default visibility is not defined
10043 locally, it is a fatal error. */
0e1862bb 10044 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10045 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10046 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10047 && h->root.type == bfd_link_hash_undefined
f5385ebf 10048 && !h->def_regular)
c152c796 10049 {
17d078c5
AM
10050 const char *msg;
10051
10052 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10053 /* xgettext:c-format */
871b3ab2 10054 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10055 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10056 /* xgettext:c-format */
871b3ab2 10057 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10058 else
695344c0 10059 /* xgettext:c-format */
871b3ab2 10060 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10061 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10062 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10063 eoinfo->failed = TRUE;
10064 return FALSE;
10065 }
10066
10067 /* If this symbol should be put in the .dynsym section, then put it
10068 there now. We already know the symbol index. We also fill in
10069 the entry in the .hash section. */
1c2649ed
EB
10070 if (h->dynindx != -1
10071 && elf_hash_table (flinfo->info)->dynamic_sections_created
10072 && elf_hash_table (flinfo->info)->dynsym != NULL
10073 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10074 {
c152c796
AM
10075 bfd_byte *esym;
10076
90c984fc
L
10077 /* Since there is no version information in the dynamic string,
10078 if there is no version info in symbol version section, we will
1659f720 10079 have a run-time problem if not linking executable, referenced
4deb8f71 10080 by shared library, or not bound locally. */
1659f720 10081 if (h->verinfo.verdef == NULL
0e1862bb 10082 && (!bfd_link_executable (flinfo->info)
1659f720
L
10083 || h->ref_dynamic
10084 || !h->def_regular))
90c984fc
L
10085 {
10086 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10087
10088 if (p && p [1] != '\0')
10089 {
4eca0228 10090 _bfd_error_handler
695344c0 10091 /* xgettext:c-format */
9793eb77 10092 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10093 flinfo->output_bfd, h->root.root.string);
10094 eoinfo->failed = TRUE;
10095 return FALSE;
10096 }
10097 }
10098
c152c796 10099 sym.st_name = h->dynstr_index;
cae1fbbb
L
10100 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10101 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10102 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10103 {
10104 eoinfo->failed = TRUE;
10105 return FALSE;
10106 }
8b127cbc 10107 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10108
8b127cbc 10109 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10110 {
10111 size_t hash_entry_size;
10112 bfd_byte *bucketpos;
10113 bfd_vma chain;
41198d0c
L
10114 size_t bucketcount;
10115 size_t bucket;
10116
8b127cbc 10117 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10118 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10119
10120 hash_entry_size
8b127cbc
AM
10121 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10122 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10123 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10124 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10125 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10126 bucketpos);
10127 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10128 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10129 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10130 }
c152c796 10131
8b127cbc 10132 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10133 {
10134 Elf_Internal_Versym iversym;
10135 Elf_External_Versym *eversym;
10136
f5385ebf 10137 if (!h->def_regular)
c152c796 10138 {
7b20f099
AM
10139 if (h->verinfo.verdef == NULL
10140 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10141 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10142 iversym.vs_vers = 0;
10143 else
10144 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10145 }
10146 else
10147 {
10148 if (h->verinfo.vertree == NULL)
10149 iversym.vs_vers = 1;
10150 else
10151 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10152 if (flinfo->info->create_default_symver)
3e3b46e5 10153 iversym.vs_vers++;
c152c796
AM
10154 }
10155
422f1182 10156 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10157 defined locally. */
422f1182 10158 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10159 iversym.vs_vers |= VERSYM_HIDDEN;
10160
8b127cbc 10161 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10162 eversym += h->dynindx;
8b127cbc 10163 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10164 }
10165 }
10166
d983c8c5
AM
10167 /* If the symbol is undefined, and we didn't output it to .dynsym,
10168 strip it from .symtab too. Obviously we can't do this for
10169 relocatable output or when needed for --emit-relocs. */
10170 else if (input_sec == bfd_und_section_ptr
10171 && h->indx != -2
66cae560
NC
10172 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10173 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10174 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10175 return TRUE;
66cae560 10176
d983c8c5
AM
10177 /* Also strip others that we couldn't earlier due to dynamic symbol
10178 processing. */
10179 if (strip)
10180 return TRUE;
10181 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10182 return TRUE;
10183
2ec55de3
AM
10184 /* Output a FILE symbol so that following locals are not associated
10185 with the wrong input file. We need one for forced local symbols
10186 if we've seen more than one FILE symbol or when we have exactly
10187 one FILE symbol but global symbols are present in a file other
10188 than the one with the FILE symbol. We also need one if linker
10189 defined symbols are present. In practice these conditions are
10190 always met, so just emit the FILE symbol unconditionally. */
10191 if (eoinfo->localsyms
10192 && !eoinfo->file_sym_done
10193 && eoinfo->flinfo->filesym_count != 0)
10194 {
10195 Elf_Internal_Sym fsym;
10196
10197 memset (&fsym, 0, sizeof (fsym));
10198 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10199 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10200 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10201 bfd_und_section_ptr, NULL))
2ec55de3
AM
10202 return FALSE;
10203
10204 eoinfo->file_sym_done = TRUE;
10205 }
10206
8b127cbc 10207 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10208 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10209 input_sec, h);
6e0b88f1 10210 if (ret == 0)
c152c796
AM
10211 {
10212 eoinfo->failed = TRUE;
10213 return FALSE;
10214 }
6e0b88f1
AM
10215 else if (ret == 1)
10216 h->indx = indx;
10217 else if (h->indx == -2)
10218 abort();
c152c796
AM
10219
10220 return TRUE;
10221}
10222
cdd3575c
AM
10223/* Return TRUE if special handling is done for relocs in SEC against
10224 symbols defined in discarded sections. */
10225
c152c796
AM
10226static bfd_boolean
10227elf_section_ignore_discarded_relocs (asection *sec)
10228{
10229 const struct elf_backend_data *bed;
10230
cdd3575c
AM
10231 switch (sec->sec_info_type)
10232 {
dbaa2011
AM
10233 case SEC_INFO_TYPE_STABS:
10234 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10235 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10236 return TRUE;
10237 default:
10238 break;
10239 }
c152c796
AM
10240
10241 bed = get_elf_backend_data (sec->owner);
10242 if (bed->elf_backend_ignore_discarded_relocs != NULL
10243 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10244 return TRUE;
10245
10246 return FALSE;
10247}
10248
9e66c942
AM
10249/* Return a mask saying how ld should treat relocations in SEC against
10250 symbols defined in discarded sections. If this function returns
10251 COMPLAIN set, ld will issue a warning message. If this function
10252 returns PRETEND set, and the discarded section was link-once and the
10253 same size as the kept link-once section, ld will pretend that the
10254 symbol was actually defined in the kept section. Otherwise ld will
10255 zero the reloc (at least that is the intent, but some cooperation by
10256 the target dependent code is needed, particularly for REL targets). */
10257
8a696751
AM
10258unsigned int
10259_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10260{
9e66c942 10261 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10262 return PRETEND;
cdd3575c
AM
10263
10264 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10265 return 0;
cdd3575c
AM
10266
10267 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10268 return 0;
cdd3575c 10269
9e66c942 10270 return COMPLAIN | PRETEND;
cdd3575c
AM
10271}
10272
3d7f7666
L
10273/* Find a match between a section and a member of a section group. */
10274
10275static asection *
c0f00686
L
10276match_group_member (asection *sec, asection *group,
10277 struct bfd_link_info *info)
3d7f7666
L
10278{
10279 asection *first = elf_next_in_group (group);
10280 asection *s = first;
10281
10282 while (s != NULL)
10283 {
c0f00686 10284 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10285 return s;
10286
83180ade 10287 s = elf_next_in_group (s);
3d7f7666
L
10288 if (s == first)
10289 break;
10290 }
10291
10292 return NULL;
10293}
10294
01b3c8ab 10295/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10296 to replace it. Return the replacement if it is OK. Otherwise return
10297 NULL. */
01b3c8ab
L
10298
10299asection *
c0f00686 10300_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10301{
10302 asection *kept;
10303
10304 kept = sec->kept_section;
10305 if (kept != NULL)
10306 {
c2370991 10307 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10308 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10309 if (kept != NULL
10310 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10311 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10312 kept = NULL;
c2370991 10313 sec->kept_section = kept;
01b3c8ab
L
10314 }
10315 return kept;
10316}
10317
c152c796
AM
10318/* Link an input file into the linker output file. This function
10319 handles all the sections and relocations of the input file at once.
10320 This is so that we only have to read the local symbols once, and
10321 don't have to keep them in memory. */
10322
10323static bfd_boolean
8b127cbc 10324elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10325{
ece5ef60 10326 int (*relocate_section)
c152c796
AM
10327 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10328 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10329 bfd *output_bfd;
10330 Elf_Internal_Shdr *symtab_hdr;
10331 size_t locsymcount;
10332 size_t extsymoff;
10333 Elf_Internal_Sym *isymbuf;
10334 Elf_Internal_Sym *isym;
10335 Elf_Internal_Sym *isymend;
10336 long *pindex;
10337 asection **ppsection;
10338 asection *o;
10339 const struct elf_backend_data *bed;
c152c796 10340 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10341 bfd_size_type address_size;
10342 bfd_vma r_type_mask;
10343 int r_sym_shift;
ffbc01cc 10344 bfd_boolean have_file_sym = FALSE;
c152c796 10345
8b127cbc 10346 output_bfd = flinfo->output_bfd;
c152c796
AM
10347 bed = get_elf_backend_data (output_bfd);
10348 relocate_section = bed->elf_backend_relocate_section;
10349
10350 /* If this is a dynamic object, we don't want to do anything here:
10351 we don't want the local symbols, and we don't want the section
10352 contents. */
10353 if ((input_bfd->flags & DYNAMIC) != 0)
10354 return TRUE;
10355
c152c796
AM
10356 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10357 if (elf_bad_symtab (input_bfd))
10358 {
10359 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10360 extsymoff = 0;
10361 }
10362 else
10363 {
10364 locsymcount = symtab_hdr->sh_info;
10365 extsymoff = symtab_hdr->sh_info;
10366 }
10367
10368 /* Read the local symbols. */
10369 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10370 if (isymbuf == NULL && locsymcount != 0)
10371 {
10372 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10373 flinfo->internal_syms,
10374 flinfo->external_syms,
10375 flinfo->locsym_shndx);
c152c796
AM
10376 if (isymbuf == NULL)
10377 return FALSE;
10378 }
10379
10380 /* Find local symbol sections and adjust values of symbols in
10381 SEC_MERGE sections. Write out those local symbols we know are
10382 going into the output file. */
10383 isymend = isymbuf + locsymcount;
8b127cbc 10384 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10385 isym < isymend;
10386 isym++, pindex++, ppsection++)
10387 {
10388 asection *isec;
10389 const char *name;
10390 Elf_Internal_Sym osym;
6e0b88f1
AM
10391 long indx;
10392 int ret;
c152c796
AM
10393
10394 *pindex = -1;
10395
10396 if (elf_bad_symtab (input_bfd))
10397 {
10398 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10399 {
10400 *ppsection = NULL;
10401 continue;
10402 }
10403 }
10404
10405 if (isym->st_shndx == SHN_UNDEF)
10406 isec = bfd_und_section_ptr;
c152c796
AM
10407 else if (isym->st_shndx == SHN_ABS)
10408 isec = bfd_abs_section_ptr;
10409 else if (isym->st_shndx == SHN_COMMON)
10410 isec = bfd_com_section_ptr;
10411 else
10412 {
cb33740c
AM
10413 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10414 if (isec == NULL)
10415 {
10416 /* Don't attempt to output symbols with st_shnx in the
10417 reserved range other than SHN_ABS and SHN_COMMON. */
10418 *ppsection = NULL;
10419 continue;
10420 }
dbaa2011 10421 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10422 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10423 isym->st_value =
10424 _bfd_merged_section_offset (output_bfd, &isec,
10425 elf_section_data (isec)->sec_info,
10426 isym->st_value);
c152c796
AM
10427 }
10428
10429 *ppsection = isec;
10430
d983c8c5
AM
10431 /* Don't output the first, undefined, symbol. In fact, don't
10432 output any undefined local symbol. */
10433 if (isec == bfd_und_section_ptr)
c152c796
AM
10434 continue;
10435
10436 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10437 {
10438 /* We never output section symbols. Instead, we use the
10439 section symbol of the corresponding section in the output
10440 file. */
10441 continue;
10442 }
10443
10444 /* If we are stripping all symbols, we don't want to output this
10445 one. */
8b127cbc 10446 if (flinfo->info->strip == strip_all)
c152c796
AM
10447 continue;
10448
10449 /* If we are discarding all local symbols, we don't want to
10450 output this one. If we are generating a relocatable output
10451 file, then some of the local symbols may be required by
10452 relocs; we output them below as we discover that they are
10453 needed. */
8b127cbc 10454 if (flinfo->info->discard == discard_all)
c152c796
AM
10455 continue;
10456
10457 /* If this symbol is defined in a section which we are
f02571c5
AM
10458 discarding, we don't need to keep it. */
10459 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10460 && isym->st_shndx < SHN_LORESERVE
10461 && bfd_section_removed_from_list (output_bfd,
10462 isec->output_section))
e75a280b
L
10463 continue;
10464
c152c796
AM
10465 /* Get the name of the symbol. */
10466 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10467 isym->st_name);
10468 if (name == NULL)
10469 return FALSE;
10470
10471 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10472 if ((flinfo->info->strip == strip_some
10473 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10474 == NULL))
8b127cbc 10475 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10476 && (isec->flags & SEC_MERGE)
10477 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10478 || flinfo->info->discard == discard_l)
c152c796
AM
10479 && bfd_is_local_label_name (input_bfd, name)))
10480 continue;
10481
ffbc01cc
AM
10482 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10483 {
ce875075
AM
10484 if (input_bfd->lto_output)
10485 /* -flto puts a temp file name here. This means builds
10486 are not reproducible. Discard the symbol. */
10487 continue;
ffbc01cc
AM
10488 have_file_sym = TRUE;
10489 flinfo->filesym_count += 1;
10490 }
10491 if (!have_file_sym)
10492 {
10493 /* In the absence of debug info, bfd_find_nearest_line uses
10494 FILE symbols to determine the source file for local
10495 function symbols. Provide a FILE symbol here if input
10496 files lack such, so that their symbols won't be
10497 associated with a previous input file. It's not the
10498 source file, but the best we can do. */
10499 have_file_sym = TRUE;
10500 flinfo->filesym_count += 1;
10501 memset (&osym, 0, sizeof (osym));
10502 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10503 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10504 if (!elf_link_output_symstrtab (flinfo,
10505 (input_bfd->lto_output ? NULL
10506 : input_bfd->filename),
10507 &osym, bfd_abs_section_ptr,
10508 NULL))
ffbc01cc
AM
10509 return FALSE;
10510 }
10511
c152c796
AM
10512 osym = *isym;
10513
10514 /* Adjust the section index for the output file. */
10515 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10516 isec->output_section);
10517 if (osym.st_shndx == SHN_BAD)
10518 return FALSE;
10519
c152c796
AM
10520 /* ELF symbols in relocatable files are section relative, but
10521 in executable files they are virtual addresses. Note that
10522 this code assumes that all ELF sections have an associated
10523 BFD section with a reasonable value for output_offset; below
10524 we assume that they also have a reasonable value for
10525 output_section. Any special sections must be set up to meet
10526 these requirements. */
10527 osym.st_value += isec->output_offset;
0e1862bb 10528 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10529 {
10530 osym.st_value += isec->output_section->vma;
10531 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10532 {
10533 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10534 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10535 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10536 else
10537 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10538 STT_NOTYPE);
c152c796
AM
10539 }
10540 }
10541
6e0b88f1 10542 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10543 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10544 if (ret == 0)
c152c796 10545 return FALSE;
6e0b88f1
AM
10546 else if (ret == 1)
10547 *pindex = indx;
c152c796
AM
10548 }
10549
310fd250
L
10550 if (bed->s->arch_size == 32)
10551 {
10552 r_type_mask = 0xff;
10553 r_sym_shift = 8;
10554 address_size = 4;
10555 }
10556 else
10557 {
10558 r_type_mask = 0xffffffff;
10559 r_sym_shift = 32;
10560 address_size = 8;
10561 }
10562
c152c796
AM
10563 /* Relocate the contents of each section. */
10564 sym_hashes = elf_sym_hashes (input_bfd);
10565 for (o = input_bfd->sections; o != NULL; o = o->next)
10566 {
10567 bfd_byte *contents;
10568
10569 if (! o->linker_mark)
10570 {
10571 /* This section was omitted from the link. */
10572 continue;
10573 }
10574
7bdf4127 10575 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10576 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10577 {
10578 /* Deal with the group signature symbol. */
10579 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10580 unsigned long symndx = sec_data->this_hdr.sh_info;
10581 asection *osec = o->output_section;
10582
7bdf4127 10583 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10584 if (symndx >= locsymcount
10585 || (elf_bad_symtab (input_bfd)
8b127cbc 10586 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10587 {
10588 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10589 while (h->root.type == bfd_link_hash_indirect
10590 || h->root.type == bfd_link_hash_warning)
10591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10592 /* Arrange for symbol to be output. */
10593 h->indx = -2;
10594 elf_section_data (osec)->this_hdr.sh_info = -2;
10595 }
10596 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10597 {
10598 /* We'll use the output section target_index. */
8b127cbc 10599 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10600 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10601 }
10602 else
10603 {
8b127cbc 10604 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10605 {
10606 /* Otherwise output the local symbol now. */
10607 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10608 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10609 const char *name;
6e0b88f1
AM
10610 long indx;
10611 int ret;
bcacc0f5
AM
10612
10613 name = bfd_elf_string_from_elf_section (input_bfd,
10614 symtab_hdr->sh_link,
10615 sym.st_name);
10616 if (name == NULL)
10617 return FALSE;
10618
10619 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10620 sec);
10621 if (sym.st_shndx == SHN_BAD)
10622 return FALSE;
10623
10624 sym.st_value += o->output_offset;
10625
6e0b88f1 10626 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10627 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10628 NULL);
6e0b88f1 10629 if (ret == 0)
bcacc0f5 10630 return FALSE;
6e0b88f1 10631 else if (ret == 1)
8b127cbc 10632 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10633 else
10634 abort ();
bcacc0f5
AM
10635 }
10636 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10637 = flinfo->indices[symndx];
bcacc0f5
AM
10638 }
10639 }
10640
c152c796 10641 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10642 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10643 continue;
10644
10645 if ((o->flags & SEC_LINKER_CREATED) != 0)
10646 {
10647 /* Section was created by _bfd_elf_link_create_dynamic_sections
10648 or somesuch. */
10649 continue;
10650 }
10651
10652 /* Get the contents of the section. They have been cached by a
10653 relaxation routine. Note that o is a section in an input
10654 file, so the contents field will not have been set by any of
10655 the routines which work on output files. */
10656 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10657 {
10658 contents = elf_section_data (o)->this_hdr.contents;
10659 if (bed->caches_rawsize
10660 && o->rawsize != 0
10661 && o->rawsize < o->size)
10662 {
10663 memcpy (flinfo->contents, contents, o->rawsize);
10664 contents = flinfo->contents;
10665 }
10666 }
c152c796
AM
10667 else
10668 {
8b127cbc 10669 contents = flinfo->contents;
4a114e3e 10670 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10671 return FALSE;
10672 }
10673
10674 if ((o->flags & SEC_RELOC) != 0)
10675 {
10676 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10677 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10678 int action_discarded;
ece5ef60 10679 int ret;
c152c796
AM
10680
10681 /* Get the swapped relocs. */
10682 internal_relocs
8b127cbc
AM
10683 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10684 flinfo->internal_relocs, FALSE);
c152c796
AM
10685 if (internal_relocs == NULL
10686 && o->reloc_count > 0)
10687 return FALSE;
10688
310fd250
L
10689 /* We need to reverse-copy input .ctors/.dtors sections if
10690 they are placed in .init_array/.finit_array for output. */
10691 if (o->size > address_size
10692 && ((strncmp (o->name, ".ctors", 6) == 0
10693 && strcmp (o->output_section->name,
10694 ".init_array") == 0)
10695 || (strncmp (o->name, ".dtors", 6) == 0
10696 && strcmp (o->output_section->name,
10697 ".fini_array") == 0))
10698 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10699 {
056bafd4
MR
10700 if (o->size * bed->s->int_rels_per_ext_rel
10701 != o->reloc_count * address_size)
310fd250 10702 {
4eca0228 10703 _bfd_error_handler
695344c0 10704 /* xgettext:c-format */
871b3ab2 10705 (_("error: %pB: size of section %pA is not "
310fd250
L
10706 "multiple of address size"),
10707 input_bfd, o);
8c6716e5 10708 bfd_set_error (bfd_error_bad_value);
310fd250
L
10709 return FALSE;
10710 }
10711 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10712 }
10713
0f02bbd9 10714 action_discarded = -1;
c152c796 10715 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10716 action_discarded = (*bed->action_discarded) (o);
10717
10718 /* Run through the relocs evaluating complex reloc symbols and
10719 looking for relocs against symbols from discarded sections
10720 or section symbols from removed link-once sections.
10721 Complain about relocs against discarded sections. Zero
10722 relocs against removed link-once sections. */
10723
10724 rel = internal_relocs;
056bafd4 10725 relend = rel + o->reloc_count;
0f02bbd9 10726 for ( ; rel < relend; rel++)
c152c796 10727 {
0f02bbd9
AM
10728 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10729 unsigned int s_type;
10730 asection **ps, *sec;
10731 struct elf_link_hash_entry *h = NULL;
10732 const char *sym_name;
c152c796 10733
0f02bbd9
AM
10734 if (r_symndx == STN_UNDEF)
10735 continue;
c152c796 10736
0f02bbd9
AM
10737 if (r_symndx >= locsymcount
10738 || (elf_bad_symtab (input_bfd)
8b127cbc 10739 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10740 {
10741 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10742
0f02bbd9
AM
10743 /* Badly formatted input files can contain relocs that
10744 reference non-existant symbols. Check here so that
10745 we do not seg fault. */
10746 if (h == NULL)
c152c796 10747 {
4eca0228 10748 _bfd_error_handler
695344c0 10749 /* xgettext:c-format */
2dcf00ce 10750 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10751 "that references a non-existent global symbol"),
2dcf00ce 10752 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10753 bfd_set_error (bfd_error_bad_value);
10754 return FALSE;
10755 }
3b36f7e6 10756
0f02bbd9
AM
10757 while (h->root.type == bfd_link_hash_indirect
10758 || h->root.type == bfd_link_hash_warning)
10759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10760
0f02bbd9 10761 s_type = h->type;
cdd3575c 10762
9e2dec47 10763 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10764 mark the symbol as undefined. Note that the
10765 linker may attach linker created dynamic sections
10766 to the plugin bfd. Symbols defined in linker
10767 created sections are not plugin symbols. */
bc4e12de 10768 if ((h->root.non_ir_ref_regular
4070765b 10769 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10770 && (h->root.type == bfd_link_hash_defined
10771 || h->root.type == bfd_link_hash_defweak)
10772 && (h->root.u.def.section->flags
10773 & SEC_LINKER_CREATED) == 0
10774 && h->root.u.def.section->owner != NULL
10775 && (h->root.u.def.section->owner->flags
10776 & BFD_PLUGIN) != 0)
10777 {
10778 h->root.type = bfd_link_hash_undefined;
10779 h->root.u.undef.abfd = h->root.u.def.section->owner;
10780 }
10781
0f02bbd9
AM
10782 ps = NULL;
10783 if (h->root.type == bfd_link_hash_defined
10784 || h->root.type == bfd_link_hash_defweak)
10785 ps = &h->root.u.def.section;
10786
10787 sym_name = h->root.root.string;
10788 }
10789 else
10790 {
10791 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10792
10793 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10794 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10795 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10796 sym, *ps);
10797 }
c152c796 10798
c301e700 10799 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10800 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10801 {
10802 bfd_vma val;
10803 bfd_vma dot = (rel->r_offset
10804 + o->output_offset + o->output_section->vma);
10805#ifdef DEBUG
10806 printf ("Encountered a complex symbol!");
10807 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10808 input_bfd->filename, o->name,
10809 (long) (rel - internal_relocs));
0f02bbd9
AM
10810 printf (" symbol: idx %8.8lx, name %s\n",
10811 r_symndx, sym_name);
10812 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10813 (unsigned long) rel->r_info,
10814 (unsigned long) rel->r_offset);
10815#endif
8b127cbc 10816 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10817 isymbuf, locsymcount, s_type == STT_SRELC))
10818 return FALSE;
10819
10820 /* Symbol evaluated OK. Update to absolute value. */
10821 set_symbol_value (input_bfd, isymbuf, locsymcount,
10822 r_symndx, val);
10823 continue;
10824 }
10825
10826 if (action_discarded != -1 && ps != NULL)
10827 {
cdd3575c
AM
10828 /* Complain if the definition comes from a
10829 discarded section. */
dbaa2011 10830 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10831 {
cf35638d 10832 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10833 if (action_discarded & COMPLAIN)
8b127cbc 10834 (*flinfo->info->callbacks->einfo)
695344c0 10835 /* xgettext:c-format */
871b3ab2
AM
10836 (_("%X`%s' referenced in section `%pA' of %pB: "
10837 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10838 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10839
87e5235d 10840 /* Try to do the best we can to support buggy old
e0ae6d6f 10841 versions of gcc. Pretend that the symbol is
87e5235d
AM
10842 really defined in the kept linkonce section.
10843 FIXME: This is quite broken. Modifying the
10844 symbol here means we will be changing all later
e0ae6d6f 10845 uses of the symbol, not just in this section. */
0f02bbd9 10846 if (action_discarded & PRETEND)
87e5235d 10847 {
01b3c8ab
L
10848 asection *kept;
10849
c0f00686 10850 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10851 flinfo->info);
01b3c8ab 10852 if (kept != NULL)
87e5235d
AM
10853 {
10854 *ps = kept;
10855 continue;
10856 }
10857 }
c152c796
AM
10858 }
10859 }
10860 }
10861
10862 /* Relocate the section by invoking a back end routine.
10863
10864 The back end routine is responsible for adjusting the
10865 section contents as necessary, and (if using Rela relocs
10866 and generating a relocatable output file) adjusting the
10867 reloc addend as necessary.
10868
10869 The back end routine does not have to worry about setting
10870 the reloc address or the reloc symbol index.
10871
10872 The back end routine is given a pointer to the swapped in
10873 internal symbols, and can access the hash table entries
10874 for the external symbols via elf_sym_hashes (input_bfd).
10875
10876 When generating relocatable output, the back end routine
10877 must handle STB_LOCAL/STT_SECTION symbols specially. The
10878 output symbol is going to be a section symbol
10879 corresponding to the output section, which will require
10880 the addend to be adjusted. */
10881
8b127cbc 10882 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10883 input_bfd, o, contents,
10884 internal_relocs,
10885 isymbuf,
8b127cbc 10886 flinfo->sections);
ece5ef60 10887 if (!ret)
c152c796
AM
10888 return FALSE;
10889
ece5ef60 10890 if (ret == 2
0e1862bb 10891 || bfd_link_relocatable (flinfo->info)
8b127cbc 10892 || flinfo->info->emitrelocations)
c152c796
AM
10893 {
10894 Elf_Internal_Rela *irela;
d4730f92 10895 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10896 bfd_vma last_offset;
10897 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10898 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10899 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10900 unsigned int next_erel;
c152c796 10901 bfd_boolean rela_normal;
d4730f92 10902 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10903
d4730f92
BS
10904 esdi = elf_section_data (o);
10905 esdo = elf_section_data (o->output_section);
10906 rela_normal = FALSE;
c152c796
AM
10907
10908 /* Adjust the reloc addresses and symbol indices. */
10909
10910 irela = internal_relocs;
056bafd4 10911 irelaend = irela + o->reloc_count;
d4730f92
BS
10912 rel_hash = esdo->rel.hashes + esdo->rel.count;
10913 /* We start processing the REL relocs, if any. When we reach
10914 IRELAMID in the loop, we switch to the RELA relocs. */
10915 irelamid = irela;
10916 if (esdi->rel.hdr != NULL)
10917 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10918 * bed->s->int_rels_per_ext_rel);
eac338cf 10919 rel_hash_list = rel_hash;
d4730f92 10920 rela_hash_list = NULL;
c152c796 10921 last_offset = o->output_offset;
0e1862bb 10922 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10923 last_offset += o->output_section->vma;
10924 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10925 {
10926 unsigned long r_symndx;
10927 asection *sec;
10928 Elf_Internal_Sym sym;
10929
10930 if (next_erel == bed->s->int_rels_per_ext_rel)
10931 {
10932 rel_hash++;
10933 next_erel = 0;
10934 }
10935
d4730f92
BS
10936 if (irela == irelamid)
10937 {
10938 rel_hash = esdo->rela.hashes + esdo->rela.count;
10939 rela_hash_list = rel_hash;
10940 rela_normal = bed->rela_normal;
10941 }
10942
c152c796 10943 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10944 flinfo->info, o,
c152c796
AM
10945 irela->r_offset);
10946 if (irela->r_offset >= (bfd_vma) -2)
10947 {
10948 /* This is a reloc for a deleted entry or somesuch.
10949 Turn it into an R_*_NONE reloc, at the same
10950 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10951 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10952 being ordered. */
10953 irela->r_offset = last_offset;
10954 irela->r_info = 0;
10955 irela->r_addend = 0;
10956 continue;
10957 }
10958
10959 irela->r_offset += o->output_offset;
10960
10961 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10962 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10963 irela->r_offset += o->output_section->vma;
10964
10965 last_offset = irela->r_offset;
10966
10967 r_symndx = irela->r_info >> r_sym_shift;
10968 if (r_symndx == STN_UNDEF)
10969 continue;
10970
10971 if (r_symndx >= locsymcount
10972 || (elf_bad_symtab (input_bfd)
8b127cbc 10973 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10974 {
10975 struct elf_link_hash_entry *rh;
10976 unsigned long indx;
10977
10978 /* This is a reloc against a global symbol. We
10979 have not yet output all the local symbols, so
10980 we do not know the symbol index of any global
10981 symbol. We set the rel_hash entry for this
10982 reloc to point to the global hash table entry
10983 for this symbol. The symbol index is then
ee75fd95 10984 set at the end of bfd_elf_final_link. */
c152c796
AM
10985 indx = r_symndx - extsymoff;
10986 rh = elf_sym_hashes (input_bfd)[indx];
10987 while (rh->root.type == bfd_link_hash_indirect
10988 || rh->root.type == bfd_link_hash_warning)
10989 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10990
10991 /* Setting the index to -2 tells
10992 elf_link_output_extsym that this symbol is
10993 used by a reloc. */
10994 BFD_ASSERT (rh->indx < 0);
10995 rh->indx = -2;
c152c796
AM
10996 *rel_hash = rh;
10997
10998 continue;
10999 }
11000
11001 /* This is a reloc against a local symbol. */
11002
11003 *rel_hash = NULL;
11004 sym = isymbuf[r_symndx];
8b127cbc 11005 sec = flinfo->sections[r_symndx];
c152c796
AM
11006 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11007 {
11008 /* I suppose the backend ought to fill in the
11009 section of any STT_SECTION symbol against a
6a8d1586 11010 processor specific section. */
cf35638d 11011 r_symndx = STN_UNDEF;
6a8d1586
AM
11012 if (bfd_is_abs_section (sec))
11013 ;
c152c796
AM
11014 else if (sec == NULL || sec->owner == NULL)
11015 {
11016 bfd_set_error (bfd_error_bad_value);
11017 return FALSE;
11018 }
11019 else
11020 {
6a8d1586
AM
11021 asection *osec = sec->output_section;
11022
11023 /* If we have discarded a section, the output
11024 section will be the absolute section. In
ab96bf03
AM
11025 case of discarded SEC_MERGE sections, use
11026 the kept section. relocate_section should
11027 have already handled discarded linkonce
11028 sections. */
6a8d1586
AM
11029 if (bfd_is_abs_section (osec)
11030 && sec->kept_section != NULL
11031 && sec->kept_section->output_section != NULL)
11032 {
11033 osec = sec->kept_section->output_section;
11034 irela->r_addend -= osec->vma;
11035 }
11036
11037 if (!bfd_is_abs_section (osec))
11038 {
11039 r_symndx = osec->target_index;
cf35638d 11040 if (r_symndx == STN_UNDEF)
74541ad4 11041 {
051d833a
AM
11042 irela->r_addend += osec->vma;
11043 osec = _bfd_nearby_section (output_bfd, osec,
11044 osec->vma);
11045 irela->r_addend -= osec->vma;
11046 r_symndx = osec->target_index;
74541ad4 11047 }
6a8d1586 11048 }
c152c796
AM
11049 }
11050
11051 /* Adjust the addend according to where the
11052 section winds up in the output section. */
11053 if (rela_normal)
11054 irela->r_addend += sec->output_offset;
11055 }
11056 else
11057 {
8b127cbc 11058 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11059 {
11060 unsigned long shlink;
11061 const char *name;
11062 asection *osec;
6e0b88f1 11063 long indx;
c152c796 11064
8b127cbc 11065 if (flinfo->info->strip == strip_all)
c152c796
AM
11066 {
11067 /* You can't do ld -r -s. */
11068 bfd_set_error (bfd_error_invalid_operation);
11069 return FALSE;
11070 }
11071
11072 /* This symbol was skipped earlier, but
11073 since it is needed by a reloc, we
11074 must output it now. */
11075 shlink = symtab_hdr->sh_link;
11076 name = (bfd_elf_string_from_elf_section
11077 (input_bfd, shlink, sym.st_name));
11078 if (name == NULL)
11079 return FALSE;
11080
11081 osec = sec->output_section;
11082 sym.st_shndx =
11083 _bfd_elf_section_from_bfd_section (output_bfd,
11084 osec);
11085 if (sym.st_shndx == SHN_BAD)
11086 return FALSE;
11087
11088 sym.st_value += sec->output_offset;
0e1862bb 11089 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11090 {
11091 sym.st_value += osec->vma;
11092 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11093 {
102def4d
AM
11094 struct elf_link_hash_table *htab
11095 = elf_hash_table (flinfo->info);
11096
c152c796
AM
11097 /* STT_TLS symbols are relative to PT_TLS
11098 segment base. */
102def4d
AM
11099 if (htab->tls_sec != NULL)
11100 sym.st_value -= htab->tls_sec->vma;
11101 else
11102 sym.st_info
11103 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11104 STT_NOTYPE);
c152c796
AM
11105 }
11106 }
11107
6e0b88f1 11108 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11109 ret = elf_link_output_symstrtab (flinfo, name,
11110 &sym, sec,
11111 NULL);
6e0b88f1 11112 if (ret == 0)
c152c796 11113 return FALSE;
6e0b88f1 11114 else if (ret == 1)
8b127cbc 11115 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11116 else
11117 abort ();
c152c796
AM
11118 }
11119
8b127cbc 11120 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11121 }
11122
11123 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11124 | (irela->r_info & r_type_mask));
11125 }
11126
11127 /* Swap out the relocs. */
d4730f92
BS
11128 input_rel_hdr = esdi->rel.hdr;
11129 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11130 {
d4730f92
BS
11131 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11132 input_rel_hdr,
11133 internal_relocs,
11134 rel_hash_list))
11135 return FALSE;
c152c796
AM
11136 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11137 * bed->s->int_rels_per_ext_rel);
eac338cf 11138 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11139 }
11140
11141 input_rela_hdr = esdi->rela.hdr;
11142 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11143 {
eac338cf 11144 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11145 input_rela_hdr,
eac338cf 11146 internal_relocs,
d4730f92 11147 rela_hash_list))
c152c796
AM
11148 return FALSE;
11149 }
11150 }
11151 }
11152
11153 /* Write out the modified section contents. */
11154 if (bed->elf_backend_write_section
8b127cbc 11155 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11156 contents))
c152c796
AM
11157 {
11158 /* Section written out. */
11159 }
11160 else switch (o->sec_info_type)
11161 {
dbaa2011 11162 case SEC_INFO_TYPE_STABS:
c152c796
AM
11163 if (! (_bfd_write_section_stabs
11164 (output_bfd,
8b127cbc 11165 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11166 o, &elf_section_data (o)->sec_info, contents)))
11167 return FALSE;
11168 break;
dbaa2011 11169 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11170 if (! _bfd_write_merged_section (output_bfd, o,
11171 elf_section_data (o)->sec_info))
11172 return FALSE;
11173 break;
dbaa2011 11174 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11175 {
8b127cbc 11176 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11177 o, contents))
11178 return FALSE;
11179 }
11180 break;
2f0c68f2
CM
11181 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11182 {
11183 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11184 flinfo->info,
11185 o, contents))
11186 return FALSE;
11187 }
11188 break;
c152c796
AM
11189 default:
11190 {
310fd250
L
11191 if (! (o->flags & SEC_EXCLUDE))
11192 {
11193 file_ptr offset = (file_ptr) o->output_offset;
11194 bfd_size_type todo = o->size;
37b01f6a
DG
11195
11196 offset *= bfd_octets_per_byte (output_bfd);
11197
310fd250
L
11198 if ((o->flags & SEC_ELF_REVERSE_COPY))
11199 {
11200 /* Reverse-copy input section to output. */
11201 do
11202 {
11203 todo -= address_size;
11204 if (! bfd_set_section_contents (output_bfd,
11205 o->output_section,
11206 contents + todo,
11207 offset,
11208 address_size))
11209 return FALSE;
11210 if (todo == 0)
11211 break;
11212 offset += address_size;
11213 }
11214 while (1);
11215 }
11216 else if (! bfd_set_section_contents (output_bfd,
11217 o->output_section,
11218 contents,
11219 offset, todo))
11220 return FALSE;
11221 }
c152c796
AM
11222 }
11223 break;
11224 }
11225 }
11226
11227 return TRUE;
11228}
11229
11230/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11231 requested by the linker, and does not come from any input file. This
c152c796
AM
11232 is used to build constructor and destructor tables when linking
11233 with -Ur. */
11234
11235static bfd_boolean
11236elf_reloc_link_order (bfd *output_bfd,
11237 struct bfd_link_info *info,
11238 asection *output_section,
11239 struct bfd_link_order *link_order)
11240{
11241 reloc_howto_type *howto;
11242 long indx;
11243 bfd_vma offset;
11244 bfd_vma addend;
d4730f92 11245 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11246 struct elf_link_hash_entry **rel_hash_ptr;
11247 Elf_Internal_Shdr *rel_hdr;
11248 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11249 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11250 bfd_byte *erel;
11251 unsigned int i;
d4730f92 11252 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11253
11254 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11255 if (howto == NULL)
11256 {
11257 bfd_set_error (bfd_error_bad_value);
11258 return FALSE;
11259 }
11260
11261 addend = link_order->u.reloc.p->addend;
11262
d4730f92
BS
11263 if (esdo->rel.hdr)
11264 reldata = &esdo->rel;
11265 else if (esdo->rela.hdr)
11266 reldata = &esdo->rela;
11267 else
11268 {
11269 reldata = NULL;
11270 BFD_ASSERT (0);
11271 }
11272
c152c796 11273 /* Figure out the symbol index. */
d4730f92 11274 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11275 if (link_order->type == bfd_section_reloc_link_order)
11276 {
11277 indx = link_order->u.reloc.p->u.section->target_index;
11278 BFD_ASSERT (indx != 0);
11279 *rel_hash_ptr = NULL;
11280 }
11281 else
11282 {
11283 struct elf_link_hash_entry *h;
11284
11285 /* Treat a reloc against a defined symbol as though it were
11286 actually against the section. */
11287 h = ((struct elf_link_hash_entry *)
11288 bfd_wrapped_link_hash_lookup (output_bfd, info,
11289 link_order->u.reloc.p->u.name,
11290 FALSE, FALSE, TRUE));
11291 if (h != NULL
11292 && (h->root.type == bfd_link_hash_defined
11293 || h->root.type == bfd_link_hash_defweak))
11294 {
11295 asection *section;
11296
11297 section = h->root.u.def.section;
11298 indx = section->output_section->target_index;
11299 *rel_hash_ptr = NULL;
11300 /* It seems that we ought to add the symbol value to the
11301 addend here, but in practice it has already been added
11302 because it was passed to constructor_callback. */
11303 addend += section->output_section->vma + section->output_offset;
11304 }
11305 else if (h != NULL)
11306 {
11307 /* Setting the index to -2 tells elf_link_output_extsym that
11308 this symbol is used by a reloc. */
11309 h->indx = -2;
11310 *rel_hash_ptr = h;
11311 indx = 0;
11312 }
11313 else
11314 {
1a72702b
AM
11315 (*info->callbacks->unattached_reloc)
11316 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11317 indx = 0;
11318 }
11319 }
11320
11321 /* If this is an inplace reloc, we must write the addend into the
11322 object file. */
11323 if (howto->partial_inplace && addend != 0)
11324 {
11325 bfd_size_type size;
11326 bfd_reloc_status_type rstat;
11327 bfd_byte *buf;
11328 bfd_boolean ok;
11329 const char *sym_name;
11330
a50b1753
NC
11331 size = (bfd_size_type) bfd_get_reloc_size (howto);
11332 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11333 if (buf == NULL && size != 0)
c152c796
AM
11334 return FALSE;
11335 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11336 switch (rstat)
11337 {
11338 case bfd_reloc_ok:
11339 break;
11340
11341 default:
11342 case bfd_reloc_outofrange:
11343 abort ();
11344
11345 case bfd_reloc_overflow:
11346 if (link_order->type == bfd_section_reloc_link_order)
11347 sym_name = bfd_section_name (output_bfd,
11348 link_order->u.reloc.p->u.section);
11349 else
11350 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11351 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11352 howto->name, addend, NULL, NULL,
11353 (bfd_vma) 0);
c152c796
AM
11354 break;
11355 }
37b01f6a 11356
c152c796 11357 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11358 link_order->offset
11359 * bfd_octets_per_byte (output_bfd),
11360 size);
c152c796
AM
11361 free (buf);
11362 if (! ok)
11363 return FALSE;
11364 }
11365
11366 /* The address of a reloc is relative to the section in a
11367 relocatable file, and is a virtual address in an executable
11368 file. */
11369 offset = link_order->offset;
0e1862bb 11370 if (! bfd_link_relocatable (info))
c152c796
AM
11371 offset += output_section->vma;
11372
11373 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11374 {
11375 irel[i].r_offset = offset;
11376 irel[i].r_info = 0;
11377 irel[i].r_addend = 0;
11378 }
11379 if (bed->s->arch_size == 32)
11380 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11381 else
11382 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11383
d4730f92 11384 rel_hdr = reldata->hdr;
c152c796
AM
11385 erel = rel_hdr->contents;
11386 if (rel_hdr->sh_type == SHT_REL)
11387 {
d4730f92 11388 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11389 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11390 }
11391 else
11392 {
11393 irel[0].r_addend = addend;
d4730f92 11394 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11395 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11396 }
11397
d4730f92 11398 ++reldata->count;
c152c796
AM
11399
11400 return TRUE;
11401}
11402
0b52efa6
PB
11403
11404/* Get the output vma of the section pointed to by the sh_link field. */
11405
11406static bfd_vma
11407elf_get_linked_section_vma (struct bfd_link_order *p)
11408{
11409 Elf_Internal_Shdr **elf_shdrp;
11410 asection *s;
11411 int elfsec;
11412
11413 s = p->u.indirect.section;
11414 elf_shdrp = elf_elfsections (s->owner);
11415 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11416 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11417 /* PR 290:
11418 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11419 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11420 sh_info fields. Hence we could get the situation
11421 where elfsec is 0. */
11422 if (elfsec == 0)
11423 {
11424 const struct elf_backend_data *bed
11425 = get_elf_backend_data (s->owner);
11426 if (bed->link_order_error_handler)
d003868e 11427 bed->link_order_error_handler
695344c0 11428 /* xgettext:c-format */
871b3ab2 11429 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11430 return 0;
11431 }
11432 else
11433 {
11434 s = elf_shdrp[elfsec]->bfd_section;
11435 return s->output_section->vma + s->output_offset;
11436 }
0b52efa6
PB
11437}
11438
11439
11440/* Compare two sections based on the locations of the sections they are
11441 linked to. Used by elf_fixup_link_order. */
11442
11443static int
11444compare_link_order (const void * a, const void * b)
11445{
11446 bfd_vma apos;
11447 bfd_vma bpos;
11448
11449 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11450 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11451 if (apos < bpos)
11452 return -1;
11453 return apos > bpos;
11454}
11455
11456
11457/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11458 order as their linked sections. Returns false if this could not be done
11459 because an output section includes both ordered and unordered
11460 sections. Ideally we'd do this in the linker proper. */
11461
11462static bfd_boolean
11463elf_fixup_link_order (bfd *abfd, asection *o)
11464{
11465 int seen_linkorder;
11466 int seen_other;
11467 int n;
11468 struct bfd_link_order *p;
11469 bfd *sub;
11470 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11471 unsigned elfsec;
0b52efa6 11472 struct bfd_link_order **sections;
d33cdfe3 11473 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11474 bfd_vma offset;
3b36f7e6 11475
d33cdfe3
L
11476 other_sec = NULL;
11477 linkorder_sec = NULL;
0b52efa6
PB
11478 seen_other = 0;
11479 seen_linkorder = 0;
8423293d 11480 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11481 {
d33cdfe3 11482 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11483 {
11484 s = p->u.indirect.section;
d33cdfe3
L
11485 sub = s->owner;
11486 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11487 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11488 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11489 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11490 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11491 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11492 {
11493 seen_linkorder++;
11494 linkorder_sec = s;
11495 }
0b52efa6 11496 else
d33cdfe3
L
11497 {
11498 seen_other++;
11499 other_sec = s;
11500 }
0b52efa6
PB
11501 }
11502 else
11503 seen_other++;
d33cdfe3
L
11504
11505 if (seen_other && seen_linkorder)
11506 {
11507 if (other_sec && linkorder_sec)
4eca0228 11508 _bfd_error_handler
695344c0 11509 /* xgettext:c-format */
871b3ab2
AM
11510 (_("%pA has both ordered [`%pA' in %pB] "
11511 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11512 o, linkorder_sec, linkorder_sec->owner,
11513 other_sec, other_sec->owner);
d33cdfe3 11514 else
4eca0228 11515 _bfd_error_handler
871b3ab2 11516 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11517 bfd_set_error (bfd_error_bad_value);
11518 return FALSE;
11519 }
0b52efa6
PB
11520 }
11521
11522 if (!seen_linkorder)
11523 return TRUE;
11524
0b52efa6 11525 sections = (struct bfd_link_order **)
14b1c01e
AM
11526 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11527 if (sections == NULL)
11528 return FALSE;
0b52efa6 11529 seen_linkorder = 0;
3b36f7e6 11530
8423293d 11531 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11532 {
11533 sections[seen_linkorder++] = p;
11534 }
11535 /* Sort the input sections in the order of their linked section. */
11536 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11537 compare_link_order);
11538
11539 /* Change the offsets of the sections. */
11540 offset = 0;
11541 for (n = 0; n < seen_linkorder; n++)
11542 {
11543 s = sections[n]->u.indirect.section;
461686a3 11544 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11545 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11546 sections[n]->offset = offset;
11547 offset += sections[n]->size;
11548 }
11549
4dd07732 11550 free (sections);
0b52efa6
PB
11551 return TRUE;
11552}
11553
76359541
TP
11554/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11555 Returns TRUE upon success, FALSE otherwise. */
11556
11557static bfd_boolean
11558elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11559{
11560 bfd_boolean ret = FALSE;
11561 bfd *implib_bfd;
11562 const struct elf_backend_data *bed;
11563 flagword flags;
11564 enum bfd_architecture arch;
11565 unsigned int mach;
11566 asymbol **sympp = NULL;
11567 long symsize;
11568 long symcount;
11569 long src_count;
11570 elf_symbol_type *osymbuf;
11571
11572 implib_bfd = info->out_implib_bfd;
11573 bed = get_elf_backend_data (abfd);
11574
11575 if (!bfd_set_format (implib_bfd, bfd_object))
11576 return FALSE;
11577
046734ff 11578 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11579 flags = bfd_get_file_flags (abfd);
11580 flags &= ~HAS_RELOC;
11581 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11582 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11583 return FALSE;
11584
11585 /* Copy architecture of output file to import library file. */
11586 arch = bfd_get_arch (abfd);
11587 mach = bfd_get_mach (abfd);
11588 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11589 && (abfd->target_defaulted
11590 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11591 return FALSE;
11592
11593 /* Get symbol table size. */
11594 symsize = bfd_get_symtab_upper_bound (abfd);
11595 if (symsize < 0)
11596 return FALSE;
11597
11598 /* Read in the symbol table. */
11599 sympp = (asymbol **) xmalloc (symsize);
11600 symcount = bfd_canonicalize_symtab (abfd, sympp);
11601 if (symcount < 0)
11602 goto free_sym_buf;
11603
11604 /* Allow the BFD backend to copy any private header data it
11605 understands from the output BFD to the import library BFD. */
11606 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11607 goto free_sym_buf;
11608
11609 /* Filter symbols to appear in the import library. */
11610 if (bed->elf_backend_filter_implib_symbols)
11611 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11612 symcount);
11613 else
11614 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11615 if (symcount == 0)
11616 {
5df1bc57 11617 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11618 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11619 implib_bfd);
76359541
TP
11620 goto free_sym_buf;
11621 }
11622
11623
11624 /* Make symbols absolute. */
11625 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11626 sizeof (*osymbuf));
11627 for (src_count = 0; src_count < symcount; src_count++)
11628 {
11629 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11630 sizeof (*osymbuf));
11631 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11632 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11633 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11634 osymbuf[src_count].internal_elf_sym.st_value =
11635 osymbuf[src_count].symbol.value;
11636 sympp[src_count] = &osymbuf[src_count].symbol;
11637 }
11638
11639 bfd_set_symtab (implib_bfd, sympp, symcount);
11640
11641 /* Allow the BFD backend to copy any private data it understands
11642 from the output BFD to the import library BFD. This is done last
11643 to permit the routine to look at the filtered symbol table. */
11644 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11645 goto free_sym_buf;
11646
11647 if (!bfd_close (implib_bfd))
11648 goto free_sym_buf;
11649
11650 ret = TRUE;
11651
11652free_sym_buf:
11653 free (sympp);
11654 return ret;
11655}
11656
9f7c3e5e
AM
11657static void
11658elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11659{
11660 asection *o;
11661
11662 if (flinfo->symstrtab != NULL)
ef10c3ac 11663 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11664 if (flinfo->contents != NULL)
11665 free (flinfo->contents);
11666 if (flinfo->external_relocs != NULL)
11667 free (flinfo->external_relocs);
11668 if (flinfo->internal_relocs != NULL)
11669 free (flinfo->internal_relocs);
11670 if (flinfo->external_syms != NULL)
11671 free (flinfo->external_syms);
11672 if (flinfo->locsym_shndx != NULL)
11673 free (flinfo->locsym_shndx);
11674 if (flinfo->internal_syms != NULL)
11675 free (flinfo->internal_syms);
11676 if (flinfo->indices != NULL)
11677 free (flinfo->indices);
11678 if (flinfo->sections != NULL)
11679 free (flinfo->sections);
9f7c3e5e
AM
11680 if (flinfo->symshndxbuf != NULL)
11681 free (flinfo->symshndxbuf);
11682 for (o = obfd->sections; o != NULL; o = o->next)
11683 {
11684 struct bfd_elf_section_data *esdo = elf_section_data (o);
11685 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11686 free (esdo->rel.hashes);
11687 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11688 free (esdo->rela.hashes);
11689 }
11690}
0b52efa6 11691
c152c796
AM
11692/* Do the final step of an ELF link. */
11693
11694bfd_boolean
11695bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11696{
11697 bfd_boolean dynamic;
11698 bfd_boolean emit_relocs;
11699 bfd *dynobj;
8b127cbc 11700 struct elf_final_link_info flinfo;
91d6fa6a
NC
11701 asection *o;
11702 struct bfd_link_order *p;
11703 bfd *sub;
c152c796
AM
11704 bfd_size_type max_contents_size;
11705 bfd_size_type max_external_reloc_size;
11706 bfd_size_type max_internal_reloc_count;
11707 bfd_size_type max_sym_count;
11708 bfd_size_type max_sym_shndx_count;
c152c796
AM
11709 Elf_Internal_Sym elfsym;
11710 unsigned int i;
11711 Elf_Internal_Shdr *symtab_hdr;
11712 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11713 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11714 struct elf_outext_info eoinfo;
11715 bfd_boolean merged;
11716 size_t relativecount = 0;
11717 asection *reldyn = 0;
11718 bfd_size_type amt;
104d59d1
JM
11719 asection *attr_section = NULL;
11720 bfd_vma attr_size = 0;
11721 const char *std_attrs_section;
64f52338 11722 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11723
64f52338 11724 if (!is_elf_hash_table (htab))
c152c796
AM
11725 return FALSE;
11726
0e1862bb 11727 if (bfd_link_pic (info))
c152c796
AM
11728 abfd->flags |= DYNAMIC;
11729
64f52338
AM
11730 dynamic = htab->dynamic_sections_created;
11731 dynobj = htab->dynobj;
c152c796 11732
0e1862bb 11733 emit_relocs = (bfd_link_relocatable (info)
a4676736 11734 || info->emitrelocations);
c152c796 11735
8b127cbc
AM
11736 flinfo.info = info;
11737 flinfo.output_bfd = abfd;
ef10c3ac 11738 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11739 if (flinfo.symstrtab == NULL)
c152c796
AM
11740 return FALSE;
11741
11742 if (! dynamic)
11743 {
8b127cbc
AM
11744 flinfo.hash_sec = NULL;
11745 flinfo.symver_sec = NULL;
c152c796
AM
11746 }
11747 else
11748 {
3d4d4302 11749 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11750 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11751 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11752 /* Note that it is OK if symver_sec is NULL. */
11753 }
11754
8b127cbc
AM
11755 flinfo.contents = NULL;
11756 flinfo.external_relocs = NULL;
11757 flinfo.internal_relocs = NULL;
11758 flinfo.external_syms = NULL;
11759 flinfo.locsym_shndx = NULL;
11760 flinfo.internal_syms = NULL;
11761 flinfo.indices = NULL;
11762 flinfo.sections = NULL;
8b127cbc 11763 flinfo.symshndxbuf = NULL;
ffbc01cc 11764 flinfo.filesym_count = 0;
c152c796 11765
104d59d1
JM
11766 /* The object attributes have been merged. Remove the input
11767 sections from the link, and set the contents of the output
11768 secton. */
11769 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11770 for (o = abfd->sections; o != NULL; o = o->next)
11771 {
5270eddc 11772 bfd_boolean remove_section = FALSE;
b8a6ced7 11773
104d59d1
JM
11774 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11775 || strcmp (o->name, ".gnu.attributes") == 0)
11776 {
11777 for (p = o->map_head.link_order; p != NULL; p = p->next)
11778 {
11779 asection *input_section;
11780
11781 if (p->type != bfd_indirect_link_order)
11782 continue;
11783 input_section = p->u.indirect.section;
11784 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11785 elf_link_input_bfd ignores this section. */
11786 input_section->flags &= ~SEC_HAS_CONTENTS;
11787 }
a0c8462f 11788
104d59d1 11789 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11790 bfd_set_section_size (abfd, o, attr_size);
11791 /* Skip this section later on. */
11792 o->map_head.link_order = NULL;
104d59d1 11793 if (attr_size)
b8a6ced7 11794 attr_section = o;
104d59d1 11795 else
5270eddc 11796 remove_section = TRUE;
104d59d1 11797 }
6e5e9d58
AM
11798 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11799 {
11800 /* Remove empty group section from linker output. */
5270eddc 11801 remove_section = TRUE;
b8a6ced7 11802 }
5270eddc 11803 if (remove_section)
b8a6ced7 11804 {
6e5e9d58
AM
11805 o->flags |= SEC_EXCLUDE;
11806 bfd_section_list_remove (abfd, o);
11807 abfd->section_count--;
11808 }
104d59d1
JM
11809 }
11810
c152c796
AM
11811 /* Count up the number of relocations we will output for each output
11812 section, so that we know the sizes of the reloc sections. We
11813 also figure out some maximum sizes. */
11814 max_contents_size = 0;
11815 max_external_reloc_size = 0;
11816 max_internal_reloc_count = 0;
11817 max_sym_count = 0;
11818 max_sym_shndx_count = 0;
11819 merged = FALSE;
11820 for (o = abfd->sections; o != NULL; o = o->next)
11821 {
11822 struct bfd_elf_section_data *esdo = elf_section_data (o);
11823 o->reloc_count = 0;
11824
8423293d 11825 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11826 {
11827 unsigned int reloc_count = 0;
9eaff861 11828 unsigned int additional_reloc_count = 0;
c152c796 11829 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11830
11831 if (p->type == bfd_section_reloc_link_order
11832 || p->type == bfd_symbol_reloc_link_order)
11833 reloc_count = 1;
11834 else if (p->type == bfd_indirect_link_order)
11835 {
11836 asection *sec;
11837
11838 sec = p->u.indirect.section;
c152c796
AM
11839
11840 /* Mark all sections which are to be included in the
11841 link. This will normally be every section. We need
11842 to do this so that we can identify any sections which
11843 the linker has decided to not include. */
11844 sec->linker_mark = TRUE;
11845
11846 if (sec->flags & SEC_MERGE)
11847 merged = TRUE;
11848
eea6121a
AM
11849 if (sec->rawsize > max_contents_size)
11850 max_contents_size = sec->rawsize;
11851 if (sec->size > max_contents_size)
11852 max_contents_size = sec->size;
c152c796 11853
c152c796
AM
11854 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11855 && (sec->owner->flags & DYNAMIC) == 0)
11856 {
11857 size_t sym_count;
11858
a961cdd5
AM
11859 /* We are interested in just local symbols, not all
11860 symbols. */
c152c796
AM
11861 if (elf_bad_symtab (sec->owner))
11862 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11863 / bed->s->sizeof_sym);
11864 else
11865 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11866
11867 if (sym_count > max_sym_count)
11868 max_sym_count = sym_count;
11869
11870 if (sym_count > max_sym_shndx_count
6a40cf0c 11871 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11872 max_sym_shndx_count = sym_count;
11873
a961cdd5
AM
11874 if (esdo->this_hdr.sh_type == SHT_REL
11875 || esdo->this_hdr.sh_type == SHT_RELA)
11876 /* Some backends use reloc_count in relocation sections
11877 to count particular types of relocs. Of course,
11878 reloc sections themselves can't have relocations. */
11879 ;
11880 else if (emit_relocs)
11881 {
11882 reloc_count = sec->reloc_count;
11883 if (bed->elf_backend_count_additional_relocs)
11884 {
11885 int c;
11886 c = (*bed->elf_backend_count_additional_relocs) (sec);
11887 additional_reloc_count += c;
11888 }
11889 }
11890 else if (bed->elf_backend_count_relocs)
11891 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11892
11893 esdi = elf_section_data (sec);
11894
c152c796
AM
11895 if ((sec->flags & SEC_RELOC) != 0)
11896 {
d4730f92 11897 size_t ext_size = 0;
c152c796 11898
d4730f92
BS
11899 if (esdi->rel.hdr != NULL)
11900 ext_size = esdi->rel.hdr->sh_size;
11901 if (esdi->rela.hdr != NULL)
11902 ext_size += esdi->rela.hdr->sh_size;
7326c758 11903
c152c796
AM
11904 if (ext_size > max_external_reloc_size)
11905 max_external_reloc_size = ext_size;
11906 if (sec->reloc_count > max_internal_reloc_count)
11907 max_internal_reloc_count = sec->reloc_count;
11908 }
11909 }
11910 }
11911
11912 if (reloc_count == 0)
11913 continue;
11914
9eaff861 11915 reloc_count += additional_reloc_count;
c152c796
AM
11916 o->reloc_count += reloc_count;
11917
0e1862bb 11918 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11919 {
d4730f92 11920 if (esdi->rel.hdr)
9eaff861 11921 {
491d01d3 11922 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11923 esdo->rel.count += additional_reloc_count;
11924 }
d4730f92 11925 if (esdi->rela.hdr)
9eaff861 11926 {
491d01d3 11927 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11928 esdo->rela.count += additional_reloc_count;
11929 }
d4730f92
BS
11930 }
11931 else
11932 {
11933 if (o->use_rela_p)
11934 esdo->rela.count += reloc_count;
2c2b4ed4 11935 else
d4730f92 11936 esdo->rel.count += reloc_count;
c152c796 11937 }
c152c796
AM
11938 }
11939
9eaff861 11940 if (o->reloc_count > 0)
c152c796
AM
11941 o->flags |= SEC_RELOC;
11942 else
11943 {
11944 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11945 set it (this is probably a bug) and if it is set
11946 assign_section_numbers will create a reloc section. */
11947 o->flags &=~ SEC_RELOC;
11948 }
11949
11950 /* If the SEC_ALLOC flag is not set, force the section VMA to
11951 zero. This is done in elf_fake_sections as well, but forcing
11952 the VMA to 0 here will ensure that relocs against these
11953 sections are handled correctly. */
11954 if ((o->flags & SEC_ALLOC) == 0
11955 && ! o->user_set_vma)
11956 o->vma = 0;
11957 }
11958
0e1862bb 11959 if (! bfd_link_relocatable (info) && merged)
64f52338 11960 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11961
11962 /* Figure out the file positions for everything but the symbol table
11963 and the relocs. We set symcount to force assign_section_numbers
11964 to create a symbol table. */
8539e4e8 11965 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11966 BFD_ASSERT (! abfd->output_has_begun);
11967 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11968 goto error_return;
11969
ee75fd95 11970 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11971 for (o = abfd->sections; o != NULL; o = o->next)
11972 {
d4730f92 11973 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11974 if ((o->flags & SEC_RELOC) != 0)
11975 {
d4730f92 11976 if (esdo->rel.hdr
9eaff861 11977 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11978 goto error_return;
11979
d4730f92 11980 if (esdo->rela.hdr
9eaff861 11981 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11982 goto error_return;
11983 }
11984
11985 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11986 to count upwards while actually outputting the relocations. */
d4730f92
BS
11987 esdo->rel.count = 0;
11988 esdo->rela.count = 0;
0ce398f1
L
11989
11990 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11991 {
11992 /* Cache the section contents so that they can be compressed
11993 later. Use bfd_malloc since it will be freed by
11994 bfd_compress_section_contents. */
11995 unsigned char *contents = esdo->this_hdr.contents;
11996 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11997 abort ();
11998 contents
11999 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12000 if (contents == NULL)
12001 goto error_return;
12002 esdo->this_hdr.contents = contents;
12003 }
c152c796
AM
12004 }
12005
c152c796 12006 /* We have now assigned file positions for all the sections except
a485e98e
AM
12007 .symtab, .strtab, and non-loaded reloc sections. We start the
12008 .symtab section at the current file position, and write directly
12009 to it. We build the .strtab section in memory. */
c152c796
AM
12010 bfd_get_symcount (abfd) = 0;
12011 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12012 /* sh_name is set in prep_headers. */
12013 symtab_hdr->sh_type = SHT_SYMTAB;
12014 /* sh_flags, sh_addr and sh_size all start off zero. */
12015 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12016 /* sh_link is set in assign_section_numbers. */
12017 /* sh_info is set below. */
12018 /* sh_offset is set just below. */
72de5009 12019 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12020
ef10c3ac
L
12021 if (max_sym_count < 20)
12022 max_sym_count = 20;
64f52338 12023 htab->strtabsize = max_sym_count;
ef10c3ac 12024 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12025 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12026 if (htab->strtab == NULL)
c152c796 12027 goto error_return;
ef10c3ac
L
12028 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12029 flinfo.symshndxbuf
12030 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12031 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12032
8539e4e8 12033 if (info->strip != strip_all || emit_relocs)
c152c796 12034 {
8539e4e8
AM
12035 file_ptr off = elf_next_file_pos (abfd);
12036
12037 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12038
12039 /* Note that at this point elf_next_file_pos (abfd) is
12040 incorrect. We do not yet know the size of the .symtab section.
12041 We correct next_file_pos below, after we do know the size. */
12042
12043 /* Start writing out the symbol table. The first symbol is always a
12044 dummy symbol. */
c152c796
AM
12045 elfsym.st_value = 0;
12046 elfsym.st_size = 0;
12047 elfsym.st_info = 0;
12048 elfsym.st_other = 0;
12049 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12050 elfsym.st_target_internal = 0;
ef10c3ac
L
12051 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12052 bfd_und_section_ptr, NULL) != 1)
c152c796 12053 goto error_return;
c152c796 12054
8539e4e8
AM
12055 /* Output a symbol for each section. We output these even if we are
12056 discarding local symbols, since they are used for relocs. These
12057 symbols have no names. We store the index of each one in the
12058 index field of the section, so that we can find it again when
12059 outputting relocs. */
12060
c152c796
AM
12061 elfsym.st_size = 0;
12062 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12063 elfsym.st_other = 0;
f0b5bb34 12064 elfsym.st_value = 0;
35fc36a8 12065 elfsym.st_target_internal = 0;
c152c796
AM
12066 for (i = 1; i < elf_numsections (abfd); i++)
12067 {
12068 o = bfd_section_from_elf_index (abfd, i);
12069 if (o != NULL)
f0b5bb34
AM
12070 {
12071 o->target_index = bfd_get_symcount (abfd);
12072 elfsym.st_shndx = i;
0e1862bb 12073 if (!bfd_link_relocatable (info))
f0b5bb34 12074 elfsym.st_value = o->vma;
ef10c3ac
L
12075 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12076 NULL) != 1)
f0b5bb34
AM
12077 goto error_return;
12078 }
c152c796
AM
12079 }
12080 }
12081
12082 /* Allocate some memory to hold information read in from the input
12083 files. */
12084 if (max_contents_size != 0)
12085 {
8b127cbc
AM
12086 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12087 if (flinfo.contents == NULL)
c152c796
AM
12088 goto error_return;
12089 }
12090
12091 if (max_external_reloc_size != 0)
12092 {
8b127cbc
AM
12093 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12094 if (flinfo.external_relocs == NULL)
c152c796
AM
12095 goto error_return;
12096 }
12097
12098 if (max_internal_reloc_count != 0)
12099 {
056bafd4 12100 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12101 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12102 if (flinfo.internal_relocs == NULL)
c152c796
AM
12103 goto error_return;
12104 }
12105
12106 if (max_sym_count != 0)
12107 {
12108 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12109 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12110 if (flinfo.external_syms == NULL)
c152c796
AM
12111 goto error_return;
12112
12113 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12114 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12115 if (flinfo.internal_syms == NULL)
c152c796
AM
12116 goto error_return;
12117
12118 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12119 flinfo.indices = (long int *) bfd_malloc (amt);
12120 if (flinfo.indices == NULL)
c152c796
AM
12121 goto error_return;
12122
12123 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12124 flinfo.sections = (asection **) bfd_malloc (amt);
12125 if (flinfo.sections == NULL)
c152c796
AM
12126 goto error_return;
12127 }
12128
12129 if (max_sym_shndx_count != 0)
12130 {
12131 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12132 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12133 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12134 goto error_return;
12135 }
12136
64f52338 12137 if (htab->tls_sec)
c152c796
AM
12138 {
12139 bfd_vma base, end = 0;
12140 asection *sec;
12141
64f52338 12142 for (sec = htab->tls_sec;
c152c796
AM
12143 sec && (sec->flags & SEC_THREAD_LOCAL);
12144 sec = sec->next)
12145 {
3a800eb9 12146 bfd_size_type size = sec->size;
c152c796 12147
3a800eb9
AM
12148 if (size == 0
12149 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12150 {
91d6fa6a
NC
12151 struct bfd_link_order *ord = sec->map_tail.link_order;
12152
12153 if (ord != NULL)
12154 size = ord->offset + ord->size;
c152c796
AM
12155 }
12156 end = sec->vma + size;
12157 }
64f52338 12158 base = htab->tls_sec->vma;
7dc98aea
RO
12159 /* Only align end of TLS section if static TLS doesn't have special
12160 alignment requirements. */
12161 if (bed->static_tls_alignment == 1)
64f52338
AM
12162 end = align_power (end, htab->tls_sec->alignment_power);
12163 htab->tls_size = end - base;
c152c796
AM
12164 }
12165
0b52efa6
PB
12166 /* Reorder SHF_LINK_ORDER sections. */
12167 for (o = abfd->sections; o != NULL; o = o->next)
12168 {
12169 if (!elf_fixup_link_order (abfd, o))
12170 return FALSE;
12171 }
12172
2f0c68f2
CM
12173 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12174 return FALSE;
12175
c152c796
AM
12176 /* Since ELF permits relocations to be against local symbols, we
12177 must have the local symbols available when we do the relocations.
12178 Since we would rather only read the local symbols once, and we
12179 would rather not keep them in memory, we handle all the
12180 relocations for a single input file at the same time.
12181
12182 Unfortunately, there is no way to know the total number of local
12183 symbols until we have seen all of them, and the local symbol
12184 indices precede the global symbol indices. This means that when
12185 we are generating relocatable output, and we see a reloc against
12186 a global symbol, we can not know the symbol index until we have
12187 finished examining all the local symbols to see which ones we are
12188 going to output. To deal with this, we keep the relocations in
12189 memory, and don't output them until the end of the link. This is
12190 an unfortunate waste of memory, but I don't see a good way around
12191 it. Fortunately, it only happens when performing a relocatable
12192 link, which is not the common case. FIXME: If keep_memory is set
12193 we could write the relocs out and then read them again; I don't
12194 know how bad the memory loss will be. */
12195
c72f2fb2 12196 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12197 sub->output_has_begun = FALSE;
12198 for (o = abfd->sections; o != NULL; o = o->next)
12199 {
8423293d 12200 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12201 {
12202 if (p->type == bfd_indirect_link_order
12203 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12204 == bfd_target_elf_flavour)
12205 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12206 {
12207 if (! sub->output_has_begun)
12208 {
8b127cbc 12209 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12210 goto error_return;
12211 sub->output_has_begun = TRUE;
12212 }
12213 }
12214 else if (p->type == bfd_section_reloc_link_order
12215 || p->type == bfd_symbol_reloc_link_order)
12216 {
12217 if (! elf_reloc_link_order (abfd, info, o, p))
12218 goto error_return;
12219 }
12220 else
12221 {
12222 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12223 {
12224 if (p->type == bfd_indirect_link_order
12225 && (bfd_get_flavour (sub)
12226 == bfd_target_elf_flavour)
12227 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12228 != bed->s->elfclass))
12229 {
12230 const char *iclass, *oclass;
12231
aebf9be7 12232 switch (bed->s->elfclass)
351f65ca 12233 {
aebf9be7
NC
12234 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12235 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12236 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12237 default: abort ();
351f65ca 12238 }
aebf9be7
NC
12239
12240 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12241 {
aebf9be7
NC
12242 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12243 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12244 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12245 default: abort ();
351f65ca
L
12246 }
12247
12248 bfd_set_error (bfd_error_wrong_format);
4eca0228 12249 _bfd_error_handler
695344c0 12250 /* xgettext:c-format */
871b3ab2 12251 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12252 sub, iclass, oclass);
12253 }
12254
12255 goto error_return;
12256 }
c152c796
AM
12257 }
12258 }
12259 }
12260
c0f00686
L
12261 /* Free symbol buffer if needed. */
12262 if (!info->reduce_memory_overheads)
12263 {
c72f2fb2 12264 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12265 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12266 && elf_tdata (sub)->symbuf)
c0f00686
L
12267 {
12268 free (elf_tdata (sub)->symbuf);
12269 elf_tdata (sub)->symbuf = NULL;
12270 }
12271 }
12272
c152c796
AM
12273 /* Output any global symbols that got converted to local in a
12274 version script or due to symbol visibility. We do this in a
12275 separate step since ELF requires all local symbols to appear
12276 prior to any global symbols. FIXME: We should only do this if
12277 some global symbols were, in fact, converted to become local.
12278 FIXME: Will this work correctly with the Irix 5 linker? */
12279 eoinfo.failed = FALSE;
8b127cbc 12280 eoinfo.flinfo = &flinfo;
c152c796 12281 eoinfo.localsyms = TRUE;
34a79995 12282 eoinfo.file_sym_done = FALSE;
7686d77d 12283 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12284 if (eoinfo.failed)
12285 return FALSE;
12286
4e617b1e
PB
12287 /* If backend needs to output some local symbols not present in the hash
12288 table, do it now. */
8539e4e8
AM
12289 if (bed->elf_backend_output_arch_local_syms
12290 && (info->strip != strip_all || emit_relocs))
4e617b1e 12291 {
6e0b88f1 12292 typedef int (*out_sym_func)
4e617b1e
PB
12293 (void *, const char *, Elf_Internal_Sym *, asection *,
12294 struct elf_link_hash_entry *);
12295
12296 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12297 (abfd, info, &flinfo,
12298 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12299 return FALSE;
12300 }
12301
c152c796
AM
12302 /* That wrote out all the local symbols. Finish up the symbol table
12303 with the global symbols. Even if we want to strip everything we
12304 can, we still need to deal with those global symbols that got
12305 converted to local in a version script. */
12306
12307 /* The sh_info field records the index of the first non local symbol. */
12308 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12309
12310 if (dynamic
64f52338
AM
12311 && htab->dynsym != NULL
12312 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12313 {
12314 Elf_Internal_Sym sym;
64f52338 12315 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12316
64f52338
AM
12317 o = htab->dynsym->output_section;
12318 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12319
12320 /* Write out the section symbols for the output sections. */
0e1862bb 12321 if (bfd_link_pic (info)
64f52338 12322 || htab->is_relocatable_executable)
c152c796
AM
12323 {
12324 asection *s;
12325
12326 sym.st_size = 0;
12327 sym.st_name = 0;
12328 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12329 sym.st_other = 0;
35fc36a8 12330 sym.st_target_internal = 0;
c152c796
AM
12331
12332 for (s = abfd->sections; s != NULL; s = s->next)
12333 {
12334 int indx;
12335 bfd_byte *dest;
12336 long dynindx;
12337
c152c796 12338 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12339 if (dynindx <= 0)
12340 continue;
12341 indx = elf_section_data (s)->this_idx;
c152c796
AM
12342 BFD_ASSERT (indx > 0);
12343 sym.st_shndx = indx;
c0d5a53d
L
12344 if (! check_dynsym (abfd, &sym))
12345 return FALSE;
c152c796
AM
12346 sym.st_value = s->vma;
12347 dest = dynsym + dynindx * bed->s->sizeof_sym;
12348 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12349 }
c152c796
AM
12350 }
12351
12352 /* Write out the local dynsyms. */
64f52338 12353 if (htab->dynlocal)
c152c796
AM
12354 {
12355 struct elf_link_local_dynamic_entry *e;
64f52338 12356 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12357 {
12358 asection *s;
12359 bfd_byte *dest;
12360
935bd1e0 12361 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12362 Note that we saved a word of storage and overwrote
12363 the original st_name with the dynstr_index. */
12364 sym = e->isym;
935bd1e0 12365 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12366
cb33740c
AM
12367 s = bfd_section_from_elf_index (e->input_bfd,
12368 e->isym.st_shndx);
12369 if (s != NULL)
c152c796 12370 {
c152c796
AM
12371 sym.st_shndx =
12372 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12373 if (! check_dynsym (abfd, &sym))
12374 return FALSE;
c152c796
AM
12375 sym.st_value = (s->output_section->vma
12376 + s->output_offset
12377 + e->isym.st_value);
12378 }
12379
c152c796
AM
12380 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12381 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12382 }
12383 }
c152c796
AM
12384 }
12385
12386 /* We get the global symbols from the hash table. */
12387 eoinfo.failed = FALSE;
12388 eoinfo.localsyms = FALSE;
8b127cbc 12389 eoinfo.flinfo = &flinfo;
7686d77d 12390 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12391 if (eoinfo.failed)
12392 return FALSE;
12393
12394 /* If backend needs to output some symbols not present in the hash
12395 table, do it now. */
8539e4e8
AM
12396 if (bed->elf_backend_output_arch_syms
12397 && (info->strip != strip_all || emit_relocs))
c152c796 12398 {
6e0b88f1 12399 typedef int (*out_sym_func)
c152c796
AM
12400 (void *, const char *, Elf_Internal_Sym *, asection *,
12401 struct elf_link_hash_entry *);
12402
12403 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12404 (abfd, info, &flinfo,
12405 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12406 return FALSE;
12407 }
12408
ef10c3ac
L
12409 /* Finalize the .strtab section. */
12410 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12411
12412 /* Swap out the .strtab section. */
12413 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12414 return FALSE;
12415
12416 /* Now we know the size of the symtab section. */
c152c796
AM
12417 if (bfd_get_symcount (abfd) > 0)
12418 {
ee3b52e9
L
12419 /* Finish up and write out the symbol string table (.strtab)
12420 section. */
ad32986f 12421 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12422 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12423
ad32986f 12424 if (elf_symtab_shndx_list (abfd))
8539e4e8 12425 {
ad32986f 12426 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12427
ad32986f
NC
12428 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12429 {
12430 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12431 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12432 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12433 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12434 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12435
ad32986f
NC
12436 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12437 off, TRUE);
12438
12439 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12440 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12441 return FALSE;
12442 }
8539e4e8 12443 }
ee3b52e9
L
12444
12445 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12446 /* sh_name was set in prep_headers. */
12447 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12448 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12449 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12450 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12451 symstrtab_hdr->sh_entsize = 0;
12452 symstrtab_hdr->sh_link = 0;
12453 symstrtab_hdr->sh_info = 0;
12454 /* sh_offset is set just below. */
12455 symstrtab_hdr->sh_addralign = 1;
12456
12457 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12458 off, TRUE);
12459 elf_next_file_pos (abfd) = off;
12460
c152c796 12461 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12462 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12463 return FALSE;
12464 }
12465
76359541
TP
12466 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12467 {
871b3ab2 12468 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12469 info->out_implib_bfd);
76359541
TP
12470 return FALSE;
12471 }
12472
c152c796
AM
12473 /* Adjust the relocs to have the correct symbol indices. */
12474 for (o = abfd->sections; o != NULL; o = o->next)
12475 {
d4730f92 12476 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12477 bfd_boolean sort;
10bbbc1d 12478
c152c796
AM
12479 if ((o->flags & SEC_RELOC) == 0)
12480 continue;
12481
28dbcedc 12482 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12483 if (esdo->rel.hdr != NULL
10bbbc1d 12484 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12485 return FALSE;
12486 if (esdo->rela.hdr != NULL
10bbbc1d 12487 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12488 return FALSE;
c152c796
AM
12489
12490 /* Set the reloc_count field to 0 to prevent write_relocs from
12491 trying to swap the relocs out itself. */
12492 o->reloc_count = 0;
12493 }
12494
12495 if (dynamic && info->combreloc && dynobj != NULL)
12496 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12497
12498 /* If we are linking against a dynamic object, or generating a
12499 shared library, finish up the dynamic linking information. */
12500 if (dynamic)
12501 {
12502 bfd_byte *dyncon, *dynconend;
12503
12504 /* Fix up .dynamic entries. */
3d4d4302 12505 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12506 BFD_ASSERT (o != NULL);
12507
12508 dyncon = o->contents;
eea6121a 12509 dynconend = o->contents + o->size;
c152c796
AM
12510 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12511 {
12512 Elf_Internal_Dyn dyn;
12513 const char *name;
12514 unsigned int type;
64487780
AM
12515 bfd_size_type sh_size;
12516 bfd_vma sh_addr;
c152c796
AM
12517
12518 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12519
12520 switch (dyn.d_tag)
12521 {
12522 default:
12523 continue;
12524 case DT_NULL:
12525 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12526 {
12527 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12528 {
12529 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12530 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12531 default: continue;
12532 }
12533 dyn.d_un.d_val = relativecount;
12534 relativecount = 0;
12535 break;
12536 }
12537 continue;
12538
12539 case DT_INIT:
12540 name = info->init_function;
12541 goto get_sym;
12542 case DT_FINI:
12543 name = info->fini_function;
12544 get_sym:
12545 {
12546 struct elf_link_hash_entry *h;
12547
64f52338 12548 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12549 if (h != NULL
12550 && (h->root.type == bfd_link_hash_defined
12551 || h->root.type == bfd_link_hash_defweak))
12552 {
bef26483 12553 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12554 o = h->root.u.def.section;
12555 if (o->output_section != NULL)
bef26483 12556 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12557 + o->output_offset);
12558 else
12559 {
12560 /* The symbol is imported from another shared
12561 library and does not apply to this one. */
bef26483 12562 dyn.d_un.d_ptr = 0;
c152c796
AM
12563 }
12564 break;
12565 }
12566 }
12567 continue;
12568
12569 case DT_PREINIT_ARRAYSZ:
12570 name = ".preinit_array";
4ade44b7 12571 goto get_out_size;
c152c796
AM
12572 case DT_INIT_ARRAYSZ:
12573 name = ".init_array";
4ade44b7 12574 goto get_out_size;
c152c796
AM
12575 case DT_FINI_ARRAYSZ:
12576 name = ".fini_array";
4ade44b7 12577 get_out_size:
c152c796
AM
12578 o = bfd_get_section_by_name (abfd, name);
12579 if (o == NULL)
12580 {
4eca0228 12581 _bfd_error_handler
4ade44b7 12582 (_("could not find section %s"), name);
c152c796
AM
12583 goto error_return;
12584 }
eea6121a 12585 if (o->size == 0)
4eca0228 12586 _bfd_error_handler
c152c796 12587 (_("warning: %s section has zero size"), name);
eea6121a 12588 dyn.d_un.d_val = o->size;
c152c796
AM
12589 break;
12590
12591 case DT_PREINIT_ARRAY:
12592 name = ".preinit_array";
4ade44b7 12593 goto get_out_vma;
c152c796
AM
12594 case DT_INIT_ARRAY:
12595 name = ".init_array";
4ade44b7 12596 goto get_out_vma;
c152c796
AM
12597 case DT_FINI_ARRAY:
12598 name = ".fini_array";
4ade44b7
AM
12599 get_out_vma:
12600 o = bfd_get_section_by_name (abfd, name);
12601 goto do_vma;
c152c796
AM
12602
12603 case DT_HASH:
12604 name = ".hash";
12605 goto get_vma;
fdc90cb4
JJ
12606 case DT_GNU_HASH:
12607 name = ".gnu.hash";
12608 goto get_vma;
c152c796
AM
12609 case DT_STRTAB:
12610 name = ".dynstr";
12611 goto get_vma;
12612 case DT_SYMTAB:
12613 name = ".dynsym";
12614 goto get_vma;
12615 case DT_VERDEF:
12616 name = ".gnu.version_d";
12617 goto get_vma;
12618 case DT_VERNEED:
12619 name = ".gnu.version_r";
12620 goto get_vma;
12621 case DT_VERSYM:
12622 name = ".gnu.version";
12623 get_vma:
4ade44b7
AM
12624 o = bfd_get_linker_section (dynobj, name);
12625 do_vma:
b3293efa 12626 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12627 {
4eca0228 12628 _bfd_error_handler
4ade44b7 12629 (_("could not find section %s"), name);
c152c796
AM
12630 goto error_return;
12631 }
894891db
NC
12632 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12633 {
4eca0228 12634 _bfd_error_handler
894891db
NC
12635 (_("warning: section '%s' is being made into a note"), name);
12636 bfd_set_error (bfd_error_nonrepresentable_section);
12637 goto error_return;
12638 }
4ade44b7 12639 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12640 break;
12641
12642 case DT_REL:
12643 case DT_RELA:
12644 case DT_RELSZ:
12645 case DT_RELASZ:
12646 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12647 type = SHT_REL;
12648 else
12649 type = SHT_RELA;
64487780
AM
12650 sh_size = 0;
12651 sh_addr = 0;
c152c796
AM
12652 for (i = 1; i < elf_numsections (abfd); i++)
12653 {
12654 Elf_Internal_Shdr *hdr;
12655
12656 hdr = elf_elfsections (abfd)[i];
12657 if (hdr->sh_type == type
12658 && (hdr->sh_flags & SHF_ALLOC) != 0)
12659 {
64487780
AM
12660 sh_size += hdr->sh_size;
12661 if (sh_addr == 0
12662 || sh_addr > hdr->sh_addr)
12663 sh_addr = hdr->sh_addr;
c152c796
AM
12664 }
12665 }
64487780 12666
64f52338
AM
12667 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12668 {
12669 /* Don't count procedure linkage table relocs in the
12670 overall reloc count. */
64487780
AM
12671 sh_size -= htab->srelplt->size;
12672 if (sh_size == 0)
12673 /* If the size is zero, make the address zero too.
12674 This is to avoid a glibc bug. If the backend
12675 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12676 zero, then we'll put DT_RELA at the end of
12677 DT_JMPREL. glibc will interpret the end of
12678 DT_RELA matching the end of DT_JMPREL as the
12679 case where DT_RELA includes DT_JMPREL, and for
12680 LD_BIND_NOW will decide that processing DT_RELA
12681 will process the PLT relocs too. Net result:
12682 No PLT relocs applied. */
12683 sh_addr = 0;
12684
64f52338
AM
12685 /* If .rela.plt is the first .rela section, exclude
12686 it from DT_RELA. */
64487780
AM
12687 else if (sh_addr == (htab->srelplt->output_section->vma
12688 + htab->srelplt->output_offset))
12689 sh_addr += htab->srelplt->size;
64f52338 12690 }
64487780
AM
12691
12692 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12693 dyn.d_un.d_val = sh_size;
12694 else
12695 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12696 break;
12697 }
12698 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12699 }
12700 }
12701
12702 /* If we have created any dynamic sections, then output them. */
12703 if (dynobj != NULL)
12704 {
12705 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12706 goto error_return;
12707
943284cc 12708 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12709 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12710 || info->error_textrel)
3d4d4302 12711 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12712 {
12713 bfd_byte *dyncon, *dynconend;
12714
943284cc
DJ
12715 dyncon = o->contents;
12716 dynconend = o->contents + o->size;
12717 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12718 {
12719 Elf_Internal_Dyn dyn;
12720
12721 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12722
12723 if (dyn.d_tag == DT_TEXTREL)
12724 {
c192a133
AM
12725 if (info->error_textrel)
12726 info->callbacks->einfo
9793eb77 12727 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12728 else
12729 info->callbacks->einfo
9793eb77 12730 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12731 break;
12732 }
12733 }
12734 }
12735
c152c796
AM
12736 for (o = dynobj->sections; o != NULL; o = o->next)
12737 {
12738 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12739 || o->size == 0
c152c796
AM
12740 || o->output_section == bfd_abs_section_ptr)
12741 continue;
12742 if ((o->flags & SEC_LINKER_CREATED) == 0)
12743 {
12744 /* At this point, we are only interested in sections
12745 created by _bfd_elf_link_create_dynamic_sections. */
12746 continue;
12747 }
64f52338 12748 if (htab->stab_info.stabstr == o)
3722b82f 12749 continue;
64f52338 12750 if (htab->eh_info.hdr_sec == o)
eea6121a 12751 continue;
3d4d4302 12752 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12753 {
12754 if (! bfd_set_section_contents (abfd, o->output_section,
12755 o->contents,
37b01f6a
DG
12756 (file_ptr) o->output_offset
12757 * bfd_octets_per_byte (abfd),
eea6121a 12758 o->size))
c152c796
AM
12759 goto error_return;
12760 }
12761 else
12762 {
12763 /* The contents of the .dynstr section are actually in a
12764 stringtab. */
8539e4e8
AM
12765 file_ptr off;
12766
c152c796
AM
12767 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12768 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12769 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12770 goto error_return;
12771 }
12772 }
12773 }
12774
7bdf4127 12775 if (!info->resolve_section_groups)
c152c796
AM
12776 {
12777 bfd_boolean failed = FALSE;
12778
7bdf4127 12779 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12780 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12781 if (failed)
12782 goto error_return;
12783 }
12784
12785 /* If we have optimized stabs strings, output them. */
64f52338 12786 if (htab->stab_info.stabstr != NULL)
c152c796 12787 {
64f52338 12788 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12789 goto error_return;
12790 }
12791
9f7c3e5e
AM
12792 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12793 goto error_return;
c152c796 12794
9f7c3e5e 12795 elf_final_link_free (abfd, &flinfo);
c152c796 12796
12bd6957 12797 elf_linker (abfd) = TRUE;
c152c796 12798
104d59d1
JM
12799 if (attr_section)
12800 {
a50b1753 12801 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12802 if (contents == NULL)
d0f16d5e 12803 return FALSE; /* Bail out and fail. */
104d59d1
JM
12804 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12805 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12806 free (contents);
12807 }
12808
c152c796
AM
12809 return TRUE;
12810
12811 error_return:
9f7c3e5e 12812 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12813 return FALSE;
12814}
12815\f
5241d853
RS
12816/* Initialize COOKIE for input bfd ABFD. */
12817
12818static bfd_boolean
12819init_reloc_cookie (struct elf_reloc_cookie *cookie,
12820 struct bfd_link_info *info, bfd *abfd)
12821{
12822 Elf_Internal_Shdr *symtab_hdr;
12823 const struct elf_backend_data *bed;
12824
12825 bed = get_elf_backend_data (abfd);
12826 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12827
12828 cookie->abfd = abfd;
12829 cookie->sym_hashes = elf_sym_hashes (abfd);
12830 cookie->bad_symtab = elf_bad_symtab (abfd);
12831 if (cookie->bad_symtab)
12832 {
12833 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12834 cookie->extsymoff = 0;
12835 }
12836 else
12837 {
12838 cookie->locsymcount = symtab_hdr->sh_info;
12839 cookie->extsymoff = symtab_hdr->sh_info;
12840 }
12841
12842 if (bed->s->arch_size == 32)
12843 cookie->r_sym_shift = 8;
12844 else
12845 cookie->r_sym_shift = 32;
12846
12847 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12848 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12849 {
12850 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12851 cookie->locsymcount, 0,
12852 NULL, NULL, NULL);
12853 if (cookie->locsyms == NULL)
12854 {
12855 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12856 return FALSE;
12857 }
12858 if (info->keep_memory)
12859 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12860 }
12861 return TRUE;
12862}
12863
12864/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12865
12866static void
12867fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12868{
12869 Elf_Internal_Shdr *symtab_hdr;
12870
12871 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12872 if (cookie->locsyms != NULL
12873 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12874 free (cookie->locsyms);
12875}
12876
12877/* Initialize the relocation information in COOKIE for input section SEC
12878 of input bfd ABFD. */
12879
12880static bfd_boolean
12881init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12882 struct bfd_link_info *info, bfd *abfd,
12883 asection *sec)
12884{
5241d853
RS
12885 if (sec->reloc_count == 0)
12886 {
12887 cookie->rels = NULL;
12888 cookie->relend = NULL;
12889 }
12890 else
12891 {
5241d853
RS
12892 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12893 info->keep_memory);
12894 if (cookie->rels == NULL)
12895 return FALSE;
12896 cookie->rel = cookie->rels;
056bafd4 12897 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12898 }
12899 cookie->rel = cookie->rels;
12900 return TRUE;
12901}
12902
12903/* Free the memory allocated by init_reloc_cookie_rels,
12904 if appropriate. */
12905
12906static void
12907fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12908 asection *sec)
12909{
12910 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12911 free (cookie->rels);
12912}
12913
12914/* Initialize the whole of COOKIE for input section SEC. */
12915
12916static bfd_boolean
12917init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12918 struct bfd_link_info *info,
12919 asection *sec)
12920{
12921 if (!init_reloc_cookie (cookie, info, sec->owner))
12922 goto error1;
12923 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12924 goto error2;
12925 return TRUE;
12926
12927 error2:
12928 fini_reloc_cookie (cookie, sec->owner);
12929 error1:
12930 return FALSE;
12931}
12932
12933/* Free the memory allocated by init_reloc_cookie_for_section,
12934 if appropriate. */
12935
12936static void
12937fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12938 asection *sec)
12939{
12940 fini_reloc_cookie_rels (cookie, sec);
12941 fini_reloc_cookie (cookie, sec->owner);
12942}
12943\f
c152c796
AM
12944/* Garbage collect unused sections. */
12945
07adf181
AM
12946/* Default gc_mark_hook. */
12947
12948asection *
12949_bfd_elf_gc_mark_hook (asection *sec,
12950 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12951 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12952 struct elf_link_hash_entry *h,
12953 Elf_Internal_Sym *sym)
12954{
12955 if (h != NULL)
12956 {
12957 switch (h->root.type)
12958 {
12959 case bfd_link_hash_defined:
12960 case bfd_link_hash_defweak:
12961 return h->root.u.def.section;
12962
12963 case bfd_link_hash_common:
12964 return h->root.u.c.p->section;
12965
12966 default:
12967 break;
12968 }
12969 }
12970 else
12971 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12972
12973 return NULL;
12974}
12975
9e223787 12976/* Return the debug definition section. */
b7c871ed
L
12977
12978static asection *
12979elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12980 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12981 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12982 struct elf_link_hash_entry *h,
9e223787 12983 Elf_Internal_Sym *sym)
b7c871ed 12984{
9e223787
L
12985 if (h != NULL)
12986 {
12987 /* Return the global debug definition section. */
12988 if ((h->root.type == bfd_link_hash_defined
12989 || h->root.type == bfd_link_hash_defweak)
12990 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12991 return h->root.u.def.section;
12992 }
12993 else
12994 {
12995 /* Return the local debug definition section. */
12996 asection *isec = bfd_section_from_elf_index (sec->owner,
12997 sym->st_shndx);
12998 if ((isec->flags & SEC_DEBUGGING) != 0)
12999 return isec;
13000 }
b7c871ed
L
13001
13002 return NULL;
13003}
13004
5241d853
RS
13005/* COOKIE->rel describes a relocation against section SEC, which is
13006 a section we've decided to keep. Return the section that contains
13007 the relocation symbol, or NULL if no section contains it. */
13008
13009asection *
13010_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13011 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13012 struct elf_reloc_cookie *cookie,
13013 bfd_boolean *start_stop)
5241d853
RS
13014{
13015 unsigned long r_symndx;
13016 struct elf_link_hash_entry *h;
13017
13018 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13019 if (r_symndx == STN_UNDEF)
5241d853
RS
13020 return NULL;
13021
13022 if (r_symndx >= cookie->locsymcount
13023 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13024 {
13025 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13026 if (h == NULL)
13027 {
871b3ab2 13028 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13029 sec->owner);
13030 return NULL;
13031 }
5241d853
RS
13032 while (h->root.type == bfd_link_hash_indirect
13033 || h->root.type == bfd_link_hash_warning)
13034 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13035 h->mark = 1;
4e6b54a6
AM
13036 /* If this symbol is weak and there is a non-weak definition, we
13037 keep the non-weak definition because many backends put
13038 dynamic reloc info on the non-weak definition for code
13039 handling copy relocs. */
60d67dc8
AM
13040 if (h->is_weakalias)
13041 weakdef (h)->mark = 1;
1cce69b9 13042
a6a4679f 13043 if (start_stop != NULL)
1cce69b9 13044 {
7dba9362
AM
13045 /* To work around a glibc bug, mark XXX input sections
13046 when there is a reference to __start_XXX or __stop_XXX
13047 symbols. */
cbd0eecf 13048 if (h->start_stop)
1cce69b9 13049 {
cbd0eecf 13050 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13051 *start_stop = !s->gc_mark;
13052 return s;
1cce69b9
AM
13053 }
13054 }
13055
5241d853
RS
13056 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13057 }
13058
13059 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13060 &cookie->locsyms[r_symndx]);
13061}
13062
13063/* COOKIE->rel describes a relocation against section SEC, which is
13064 a section we've decided to keep. Mark the section that contains
9d0a14d3 13065 the relocation symbol. */
5241d853
RS
13066
13067bfd_boolean
13068_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13069 asection *sec,
13070 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13071 struct elf_reloc_cookie *cookie)
5241d853
RS
13072{
13073 asection *rsec;
1cce69b9 13074 bfd_boolean start_stop = FALSE;
5241d853 13075
1cce69b9
AM
13076 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13077 while (rsec != NULL)
5241d853 13078 {
1cce69b9
AM
13079 if (!rsec->gc_mark)
13080 {
13081 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13082 || (rsec->owner->flags & DYNAMIC) != 0)
13083 rsec->gc_mark = 1;
13084 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13085 return FALSE;
13086 }
13087 if (!start_stop)
13088 break;
199af150 13089 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13090 }
13091 return TRUE;
13092}
13093
07adf181
AM
13094/* The mark phase of garbage collection. For a given section, mark
13095 it and any sections in this section's group, and all the sections
13096 which define symbols to which it refers. */
13097
ccfa59ea
AM
13098bfd_boolean
13099_bfd_elf_gc_mark (struct bfd_link_info *info,
13100 asection *sec,
6a5bb875 13101 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13102{
13103 bfd_boolean ret;
9d0a14d3 13104 asection *group_sec, *eh_frame;
c152c796
AM
13105
13106 sec->gc_mark = 1;
13107
13108 /* Mark all the sections in the group. */
13109 group_sec = elf_section_data (sec)->next_in_group;
13110 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13111 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13112 return FALSE;
13113
13114 /* Look through the section relocs. */
13115 ret = TRUE;
9d0a14d3
RS
13116 eh_frame = elf_eh_frame_section (sec->owner);
13117 if ((sec->flags & SEC_RELOC) != 0
13118 && sec->reloc_count > 0
13119 && sec != eh_frame)
c152c796 13120 {
5241d853 13121 struct elf_reloc_cookie cookie;
c152c796 13122
5241d853
RS
13123 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13124 ret = FALSE;
c152c796 13125 else
c152c796 13126 {
5241d853 13127 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13128 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13129 {
13130 ret = FALSE;
13131 break;
13132 }
13133 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13134 }
13135 }
9d0a14d3
RS
13136
13137 if (ret && eh_frame && elf_fde_list (sec))
13138 {
13139 struct elf_reloc_cookie cookie;
13140
13141 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13142 ret = FALSE;
13143 else
13144 {
13145 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13146 gc_mark_hook, &cookie))
13147 ret = FALSE;
13148 fini_reloc_cookie_for_section (&cookie, eh_frame);
13149 }
13150 }
13151
2f0c68f2
CM
13152 eh_frame = elf_section_eh_frame_entry (sec);
13153 if (ret && eh_frame && !eh_frame->gc_mark)
13154 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13155 ret = FALSE;
13156
c152c796
AM
13157 return ret;
13158}
13159
3c758495
TG
13160/* Scan and mark sections in a special or debug section group. */
13161
13162static void
13163_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13164{
13165 /* Point to first section of section group. */
13166 asection *ssec;
13167 /* Used to iterate the section group. */
13168 asection *msec;
13169
13170 bfd_boolean is_special_grp = TRUE;
13171 bfd_boolean is_debug_grp = TRUE;
13172
13173 /* First scan to see if group contains any section other than debug
13174 and special section. */
13175 ssec = msec = elf_next_in_group (grp);
13176 do
13177 {
13178 if ((msec->flags & SEC_DEBUGGING) == 0)
13179 is_debug_grp = FALSE;
13180
13181 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13182 is_special_grp = FALSE;
13183
13184 msec = elf_next_in_group (msec);
13185 }
13186 while (msec != ssec);
13187
13188 /* If this is a pure debug section group or pure special section group,
13189 keep all sections in this group. */
13190 if (is_debug_grp || is_special_grp)
13191 {
13192 do
13193 {
13194 msec->gc_mark = 1;
13195 msec = elf_next_in_group (msec);
13196 }
13197 while (msec != ssec);
13198 }
13199}
13200
7f6ab9f8
AM
13201/* Keep debug and special sections. */
13202
13203bfd_boolean
13204_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13205 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13206{
13207 bfd *ibfd;
13208
c72f2fb2 13209 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13210 {
13211 asection *isec;
13212 bfd_boolean some_kept;
b40bf0a2 13213 bfd_boolean debug_frag_seen;
b7c871ed 13214 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13215
13216 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13217 continue;
57963c05
AM
13218 isec = ibfd->sections;
13219 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13220 continue;
7f6ab9f8 13221
b40bf0a2
NC
13222 /* Ensure all linker created sections are kept,
13223 see if any other section is already marked,
13224 and note if we have any fragmented debug sections. */
b7c871ed 13225 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13226 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13227 {
13228 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13229 isec->gc_mark = 1;
eb026f09
AM
13230 else if (isec->gc_mark
13231 && (isec->flags & SEC_ALLOC) != 0
13232 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13233 some_kept = TRUE;
b40bf0a2 13234
535b785f 13235 if (!debug_frag_seen
b40bf0a2
NC
13236 && (isec->flags & SEC_DEBUGGING)
13237 && CONST_STRNEQ (isec->name, ".debug_line."))
13238 debug_frag_seen = TRUE;
7f6ab9f8
AM
13239 }
13240
eb026f09
AM
13241 /* If no non-note alloc section in this file will be kept, then
13242 we can toss out the debug and special sections. */
7f6ab9f8
AM
13243 if (!some_kept)
13244 continue;
13245
13246 /* Keep debug and special sections like .comment when they are
3c758495
TG
13247 not part of a group. Also keep section groups that contain
13248 just debug sections or special sections. */
7f6ab9f8 13249 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13250 {
13251 if ((isec->flags & SEC_GROUP) != 0)
13252 _bfd_elf_gc_mark_debug_special_section_group (isec);
13253 else if (((isec->flags & SEC_DEBUGGING) != 0
13254 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13255 && elf_next_in_group (isec) == NULL)
13256 isec->gc_mark = 1;
b7c871ed
L
13257 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13258 has_kept_debug_info = TRUE;
3c758495 13259 }
b40bf0a2 13260
b40bf0a2
NC
13261 /* Look for CODE sections which are going to be discarded,
13262 and find and discard any fragmented debug sections which
13263 are associated with that code section. */
b7c871ed
L
13264 if (debug_frag_seen)
13265 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13266 if ((isec->flags & SEC_CODE) != 0
13267 && isec->gc_mark == 0)
13268 {
13269 unsigned int ilen;
13270 asection *dsec;
b40bf0a2 13271
b7c871ed 13272 ilen = strlen (isec->name);
b40bf0a2 13273
b7c871ed 13274 /* Association is determined by the name of the debug
07d6d2b8 13275 section containing the name of the code section as
b7c871ed
L
13276 a suffix. For example .debug_line.text.foo is a
13277 debug section associated with .text.foo. */
13278 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13279 {
13280 unsigned int dlen;
b40bf0a2 13281
b7c871ed
L
13282 if (dsec->gc_mark == 0
13283 || (dsec->flags & SEC_DEBUGGING) == 0)
13284 continue;
b40bf0a2 13285
b7c871ed 13286 dlen = strlen (dsec->name);
b40bf0a2 13287
b7c871ed
L
13288 if (dlen > ilen
13289 && strncmp (dsec->name + (dlen - ilen),
13290 isec->name, ilen) == 0)
b40bf0a2 13291 dsec->gc_mark = 0;
b7c871ed 13292 }
b40bf0a2 13293 }
b7c871ed
L
13294
13295 /* Mark debug sections referenced by kept debug sections. */
13296 if (has_kept_debug_info)
13297 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13298 if (isec->gc_mark
13299 && (isec->flags & SEC_DEBUGGING) != 0)
13300 if (!_bfd_elf_gc_mark (info, isec,
13301 elf_gc_mark_debug_section))
13302 return FALSE;
7f6ab9f8
AM
13303 }
13304 return TRUE;
13305}
13306
c152c796 13307static bfd_boolean
ccabcbe5 13308elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13309{
13310 bfd *sub;
ccabcbe5 13311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13312
c72f2fb2 13313 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13314 {
13315 asection *o;
13316
b19a8f85 13317 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13318 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13319 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13320 continue;
57963c05
AM
13321 o = sub->sections;
13322 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13323 continue;
c152c796
AM
13324
13325 for (o = sub->sections; o != NULL; o = o->next)
13326 {
a33dafc3
L
13327 /* When any section in a section group is kept, we keep all
13328 sections in the section group. If the first member of
13329 the section group is excluded, we will also exclude the
13330 group section. */
13331 if (o->flags & SEC_GROUP)
13332 {
13333 asection *first = elf_next_in_group (o);
13334 o->gc_mark = first->gc_mark;
13335 }
c152c796 13336
1e7eae0d 13337 if (o->gc_mark)
c152c796
AM
13338 continue;
13339
13340 /* Skip sweeping sections already excluded. */
13341 if (o->flags & SEC_EXCLUDE)
13342 continue;
13343
13344 /* Since this is early in the link process, it is simple
13345 to remove a section from the output. */
13346 o->flags |= SEC_EXCLUDE;
13347
c55fe096 13348 if (info->print_gc_sections && o->size != 0)
695344c0 13349 /* xgettext:c-format */
9793eb77 13350 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13351 o, sub);
c152c796
AM
13352 }
13353 }
13354
c152c796
AM
13355 return TRUE;
13356}
13357
13358/* Propagate collected vtable information. This is called through
13359 elf_link_hash_traverse. */
13360
13361static bfd_boolean
13362elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13363{
c152c796 13364 /* Those that are not vtables. */
cbd0eecf
L
13365 if (h->start_stop
13366 || h->u2.vtable == NULL
13367 || h->u2.vtable->parent == NULL)
c152c796
AM
13368 return TRUE;
13369
13370 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13371 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13372 return TRUE;
13373
13374 /* If we've already been done, exit. */
cbd0eecf 13375 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13376 return TRUE;
13377
13378 /* Make sure the parent's table is up to date. */
cbd0eecf 13379 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13380
cbd0eecf 13381 if (h->u2.vtable->used == NULL)
c152c796
AM
13382 {
13383 /* None of this table's entries were referenced. Re-use the
13384 parent's table. */
cbd0eecf
L
13385 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13386 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13387 }
13388 else
13389 {
13390 size_t n;
13391 bfd_boolean *cu, *pu;
13392
13393 /* Or the parent's entries into ours. */
cbd0eecf 13394 cu = h->u2.vtable->used;
c152c796 13395 cu[-1] = TRUE;
cbd0eecf 13396 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13397 if (pu != NULL)
13398 {
13399 const struct elf_backend_data *bed;
13400 unsigned int log_file_align;
13401
13402 bed = get_elf_backend_data (h->root.u.def.section->owner);
13403 log_file_align = bed->s->log_file_align;
cbd0eecf 13404 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13405 while (n--)
13406 {
13407 if (*pu)
13408 *cu = TRUE;
13409 pu++;
13410 cu++;
13411 }
13412 }
13413 }
13414
13415 return TRUE;
13416}
13417
13418static bfd_boolean
13419elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13420{
13421 asection *sec;
13422 bfd_vma hstart, hend;
13423 Elf_Internal_Rela *relstart, *relend, *rel;
13424 const struct elf_backend_data *bed;
13425 unsigned int log_file_align;
13426
c152c796
AM
13427 /* Take care of both those symbols that do not describe vtables as
13428 well as those that are not loaded. */
cbd0eecf
L
13429 if (h->start_stop
13430 || h->u2.vtable == NULL
13431 || h->u2.vtable->parent == NULL)
c152c796
AM
13432 return TRUE;
13433
13434 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13435 || h->root.type == bfd_link_hash_defweak);
13436
13437 sec = h->root.u.def.section;
13438 hstart = h->root.u.def.value;
13439 hend = hstart + h->size;
13440
13441 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13442 if (!relstart)
13443 return *(bfd_boolean *) okp = FALSE;
13444 bed = get_elf_backend_data (sec->owner);
13445 log_file_align = bed->s->log_file_align;
13446
056bafd4 13447 relend = relstart + sec->reloc_count;
c152c796
AM
13448
13449 for (rel = relstart; rel < relend; ++rel)
13450 if (rel->r_offset >= hstart && rel->r_offset < hend)
13451 {
13452 /* If the entry is in use, do nothing. */
cbd0eecf
L
13453 if (h->u2.vtable->used
13454 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13455 {
13456 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13457 if (h->u2.vtable->used[entry])
c152c796
AM
13458 continue;
13459 }
13460 /* Otherwise, kill it. */
13461 rel->r_offset = rel->r_info = rel->r_addend = 0;
13462 }
13463
13464 return TRUE;
13465}
13466
87538722
AM
13467/* Mark sections containing dynamically referenced symbols. When
13468 building shared libraries, we must assume that any visible symbol is
13469 referenced. */
715df9b8 13470
64d03ab5
AM
13471bfd_boolean
13472bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13473{
87538722 13474 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13476
715df9b8
EB
13477 if ((h->root.type == bfd_link_hash_defined
13478 || h->root.type == bfd_link_hash_defweak)
d664fd41 13479 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13480 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13481 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13482 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13483 && (!bfd_link_executable (info)
22185505 13484 || info->gc_keep_exported
b407645f
AM
13485 || info->export_dynamic
13486 || (h->dynamic
13487 && d != NULL
13488 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13489 && (h->versioned >= versioned
54e8959c
L
13490 || !bfd_hide_sym_by_version (info->version_info,
13491 h->root.root.string)))))
715df9b8
EB
13492 h->root.u.def.section->flags |= SEC_KEEP;
13493
13494 return TRUE;
13495}
3b36f7e6 13496
74f0fb50
AM
13497/* Keep all sections containing symbols undefined on the command-line,
13498 and the section containing the entry symbol. */
13499
13500void
13501_bfd_elf_gc_keep (struct bfd_link_info *info)
13502{
13503 struct bfd_sym_chain *sym;
13504
13505 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13506 {
13507 struct elf_link_hash_entry *h;
13508
13509 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13510 FALSE, FALSE, FALSE);
13511
13512 if (h != NULL
13513 && (h->root.type == bfd_link_hash_defined
13514 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13515 && !bfd_is_abs_section (h->root.u.def.section)
13516 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13517 h->root.u.def.section->flags |= SEC_KEEP;
13518 }
13519}
13520
2f0c68f2
CM
13521bfd_boolean
13522bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13523 struct bfd_link_info *info)
13524{
13525 bfd *ibfd = info->input_bfds;
13526
13527 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13528 {
13529 asection *sec;
13530 struct elf_reloc_cookie cookie;
13531
13532 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13533 continue;
57963c05
AM
13534 sec = ibfd->sections;
13535 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13536 continue;
2f0c68f2
CM
13537
13538 if (!init_reloc_cookie (&cookie, info, ibfd))
13539 return FALSE;
13540
13541 for (sec = ibfd->sections; sec; sec = sec->next)
13542 {
13543 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13544 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13545 {
13546 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13547 fini_reloc_cookie_rels (&cookie, sec);
13548 }
13549 }
13550 }
13551 return TRUE;
13552}
13553
c152c796
AM
13554/* Do mark and sweep of unused sections. */
13555
13556bfd_boolean
13557bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13558{
13559 bfd_boolean ok = TRUE;
13560 bfd *sub;
6a5bb875 13561 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13563 struct elf_link_hash_table *htab;
c152c796 13564
64d03ab5 13565 if (!bed->can_gc_sections
715df9b8 13566 || !is_elf_hash_table (info->hash))
c152c796 13567 {
9793eb77 13568 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13569 return TRUE;
13570 }
13571
74f0fb50 13572 bed->gc_keep (info);
da44f4e5 13573 htab = elf_hash_table (info);
74f0fb50 13574
9d0a14d3
RS
13575 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13576 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13577 for (sub = info->input_bfds;
13578 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13579 sub = sub->link.next)
9d0a14d3
RS
13580 {
13581 asection *sec;
13582 struct elf_reloc_cookie cookie;
13583
57963c05
AM
13584 sec = sub->sections;
13585 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13586 continue;
9d0a14d3 13587 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13588 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13589 {
13590 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13591 if (elf_section_data (sec)->sec_info
13592 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13593 elf_eh_frame_section (sub) = sec;
13594 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13595 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13596 }
13597 }
9d0a14d3 13598
c152c796 13599 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13600 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13601 if (!ok)
13602 return FALSE;
13603
13604 /* Kill the vtable relocations that were not used. */
da44f4e5 13605 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13606 if (!ok)
13607 return FALSE;
13608
715df9b8 13609 /* Mark dynamically referenced symbols. */
22185505 13610 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13611 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13612
715df9b8 13613 /* Grovel through relocs to find out who stays ... */
64d03ab5 13614 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13615 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13616 {
13617 asection *o;
13618
b19a8f85 13619 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13620 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13621 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13622 continue;
13623
57963c05
AM
13624 o = sub->sections;
13625 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13626 continue;
13627
7f6ab9f8
AM
13628 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13629 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13630 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13631 well as FINI_ARRAY sections for ld -r. */
c152c796 13632 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13633 if (!o->gc_mark
13634 && (o->flags & SEC_EXCLUDE) == 0
24007750 13635 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13636 || (bfd_link_relocatable (info)
13637 && ((elf_section_data (o)->this_hdr.sh_type
13638 == SHT_PREINIT_ARRAY)
13639 || (elf_section_data (o)->this_hdr.sh_type
13640 == SHT_INIT_ARRAY)
13641 || (elf_section_data (o)->this_hdr.sh_type
13642 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13643 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13644 && elf_next_in_group (o) == NULL )))
13645 {
13646 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13647 return FALSE;
13648 }
c152c796
AM
13649 }
13650
6a5bb875 13651 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13652 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13653
c152c796 13654 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13655 return elf_gc_sweep (abfd, info);
c152c796
AM
13656}
13657\f
13658/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13659
13660bfd_boolean
13661bfd_elf_gc_record_vtinherit (bfd *abfd,
13662 asection *sec,
13663 struct elf_link_hash_entry *h,
13664 bfd_vma offset)
13665{
13666 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13667 struct elf_link_hash_entry **search, *child;
ef53be89 13668 size_t extsymcount;
c152c796
AM
13669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13670
13671 /* The sh_info field of the symtab header tells us where the
13672 external symbols start. We don't care about the local symbols at
13673 this point. */
13674 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13675 if (!elf_bad_symtab (abfd))
13676 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13677
13678 sym_hashes = elf_sym_hashes (abfd);
13679 sym_hashes_end = sym_hashes + extsymcount;
13680
13681 /* Hunt down the child symbol, which is in this section at the same
13682 offset as the relocation. */
13683 for (search = sym_hashes; search != sym_hashes_end; ++search)
13684 {
13685 if ((child = *search) != NULL
13686 && (child->root.type == bfd_link_hash_defined
13687 || child->root.type == bfd_link_hash_defweak)
13688 && child->root.u.def.section == sec
13689 && child->root.u.def.value == offset)
13690 goto win;
13691 }
13692
695344c0 13693 /* xgettext:c-format */
9793eb77 13694 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13695 abfd, sec, (uint64_t) offset);
c152c796
AM
13696 bfd_set_error (bfd_error_invalid_operation);
13697 return FALSE;
13698
13699 win:
cbd0eecf 13700 if (!child->u2.vtable)
f6e332e6 13701 {
cbd0eecf
L
13702 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13703 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13704 if (!child->u2.vtable)
f6e332e6
AM
13705 return FALSE;
13706 }
c152c796
AM
13707 if (!h)
13708 {
13709 /* This *should* only be the absolute section. It could potentially
13710 be that someone has defined a non-global vtable though, which
13711 would be bad. It isn't worth paging in the local symbols to be
13712 sure though; that case should simply be handled by the assembler. */
13713
cbd0eecf 13714 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13715 }
13716 else
cbd0eecf 13717 child->u2.vtable->parent = h;
c152c796
AM
13718
13719 return TRUE;
13720}
13721
13722/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13723
13724bfd_boolean
13725bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13726 asection *sec ATTRIBUTE_UNUSED,
13727 struct elf_link_hash_entry *h,
13728 bfd_vma addend)
13729{
13730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13731 unsigned int log_file_align = bed->s->log_file_align;
13732
cbd0eecf 13733 if (!h->u2.vtable)
f6e332e6 13734 {
cbd0eecf
L
13735 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13736 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13737 if (!h->u2.vtable)
f6e332e6
AM
13738 return FALSE;
13739 }
13740
cbd0eecf 13741 if (addend >= h->u2.vtable->size)
c152c796
AM
13742 {
13743 size_t size, bytes, file_align;
cbd0eecf 13744 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13745
13746 /* While the symbol is undefined, we have to be prepared to handle
13747 a zero size. */
13748 file_align = 1 << log_file_align;
13749 if (h->root.type == bfd_link_hash_undefined)
13750 size = addend + file_align;
13751 else
13752 {
13753 size = h->size;
13754 if (addend >= size)
13755 {
13756 /* Oops! We've got a reference past the defined end of
13757 the table. This is probably a bug -- shall we warn? */
13758 size = addend + file_align;
13759 }
13760 }
13761 size = (size + file_align - 1) & -file_align;
13762
13763 /* Allocate one extra entry for use as a "done" flag for the
13764 consolidation pass. */
13765 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13766
13767 if (ptr)
13768 {
a50b1753 13769 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13770
13771 if (ptr != NULL)
13772 {
13773 size_t oldbytes;
13774
cbd0eecf 13775 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13776 * sizeof (bfd_boolean));
13777 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13778 }
13779 }
13780 else
a50b1753 13781 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13782
13783 if (ptr == NULL)
13784 return FALSE;
13785
13786 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13787 h->u2.vtable->used = ptr + 1;
13788 h->u2.vtable->size = size;
c152c796
AM
13789 }
13790
cbd0eecf 13791 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13792
13793 return TRUE;
13794}
13795
ae17ab41
CM
13796/* Map an ELF section header flag to its corresponding string. */
13797typedef struct
13798{
13799 char *flag_name;
13800 flagword flag_value;
13801} elf_flags_to_name_table;
13802
13803static elf_flags_to_name_table elf_flags_to_names [] =
13804{
13805 { "SHF_WRITE", SHF_WRITE },
13806 { "SHF_ALLOC", SHF_ALLOC },
13807 { "SHF_EXECINSTR", SHF_EXECINSTR },
13808 { "SHF_MERGE", SHF_MERGE },
13809 { "SHF_STRINGS", SHF_STRINGS },
13810 { "SHF_INFO_LINK", SHF_INFO_LINK},
13811 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13812 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13813 { "SHF_GROUP", SHF_GROUP },
13814 { "SHF_TLS", SHF_TLS },
13815 { "SHF_MASKOS", SHF_MASKOS },
13816 { "SHF_EXCLUDE", SHF_EXCLUDE },
13817};
13818
b9c361e0
JL
13819/* Returns TRUE if the section is to be included, otherwise FALSE. */
13820bfd_boolean
ae17ab41 13821bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13822 struct flag_info *flaginfo,
b9c361e0 13823 asection *section)
ae17ab41 13824{
8b127cbc 13825 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13826
8b127cbc 13827 if (!flaginfo->flags_initialized)
ae17ab41 13828 {
8b127cbc
AM
13829 bfd *obfd = info->output_bfd;
13830 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13831 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13832 int with_hex = 0;
13833 int without_hex = 0;
13834
8b127cbc 13835 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13836 {
b9c361e0 13837 unsigned i;
8b127cbc 13838 flagword (*lookup) (char *);
ae17ab41 13839
8b127cbc
AM
13840 lookup = bed->elf_backend_lookup_section_flags_hook;
13841 if (lookup != NULL)
ae17ab41 13842 {
8b127cbc 13843 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13844
13845 if (hexval != 0)
13846 {
13847 if (tf->with == with_flags)
13848 with_hex |= hexval;
13849 else if (tf->with == without_flags)
13850 without_hex |= hexval;
13851 tf->valid = TRUE;
13852 continue;
13853 }
ae17ab41 13854 }
8b127cbc 13855 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13856 {
8b127cbc 13857 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13858 {
13859 if (tf->with == with_flags)
13860 with_hex |= elf_flags_to_names[i].flag_value;
13861 else if (tf->with == without_flags)
13862 without_hex |= elf_flags_to_names[i].flag_value;
13863 tf->valid = TRUE;
13864 break;
13865 }
13866 }
8b127cbc 13867 if (!tf->valid)
b9c361e0 13868 {
68ffbac6 13869 info->callbacks->einfo
9793eb77 13870 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13871 return FALSE;
ae17ab41
CM
13872 }
13873 }
8b127cbc
AM
13874 flaginfo->flags_initialized = TRUE;
13875 flaginfo->only_with_flags |= with_hex;
13876 flaginfo->not_with_flags |= without_hex;
ae17ab41 13877 }
ae17ab41 13878
8b127cbc 13879 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13880 return FALSE;
13881
8b127cbc 13882 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13883 return FALSE;
13884
13885 return TRUE;
ae17ab41
CM
13886}
13887
c152c796
AM
13888struct alloc_got_off_arg {
13889 bfd_vma gotoff;
10455f89 13890 struct bfd_link_info *info;
c152c796
AM
13891};
13892
13893/* We need a special top-level link routine to convert got reference counts
13894 to real got offsets. */
13895
13896static bfd_boolean
13897elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13898{
a50b1753 13899 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13900 bfd *obfd = gofarg->info->output_bfd;
13901 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13902
c152c796
AM
13903 if (h->got.refcount > 0)
13904 {
13905 h->got.offset = gofarg->gotoff;
10455f89 13906 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13907 }
13908 else
13909 h->got.offset = (bfd_vma) -1;
13910
13911 return TRUE;
13912}
13913
13914/* And an accompanying bit to work out final got entry offsets once
13915 we're done. Should be called from final_link. */
13916
13917bfd_boolean
13918bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13919 struct bfd_link_info *info)
13920{
13921 bfd *i;
13922 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13923 bfd_vma gotoff;
c152c796
AM
13924 struct alloc_got_off_arg gofarg;
13925
10455f89
HPN
13926 BFD_ASSERT (abfd == info->output_bfd);
13927
c152c796
AM
13928 if (! is_elf_hash_table (info->hash))
13929 return FALSE;
13930
13931 /* The GOT offset is relative to the .got section, but the GOT header is
13932 put into the .got.plt section, if the backend uses it. */
13933 if (bed->want_got_plt)
13934 gotoff = 0;
13935 else
13936 gotoff = bed->got_header_size;
13937
13938 /* Do the local .got entries first. */
c72f2fb2 13939 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13940 {
13941 bfd_signed_vma *local_got;
ef53be89 13942 size_t j, locsymcount;
c152c796
AM
13943 Elf_Internal_Shdr *symtab_hdr;
13944
13945 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13946 continue;
13947
13948 local_got = elf_local_got_refcounts (i);
13949 if (!local_got)
13950 continue;
13951
13952 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13953 if (elf_bad_symtab (i))
13954 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13955 else
13956 locsymcount = symtab_hdr->sh_info;
13957
13958 for (j = 0; j < locsymcount; ++j)
13959 {
13960 if (local_got[j] > 0)
13961 {
13962 local_got[j] = gotoff;
10455f89 13963 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13964 }
13965 else
13966 local_got[j] = (bfd_vma) -1;
13967 }
13968 }
13969
13970 /* Then the global .got entries. .plt refcounts are handled by
13971 adjust_dynamic_symbol */
13972 gofarg.gotoff = gotoff;
10455f89 13973 gofarg.info = info;
c152c796
AM
13974 elf_link_hash_traverse (elf_hash_table (info),
13975 elf_gc_allocate_got_offsets,
13976 &gofarg);
13977 return TRUE;
13978}
13979
13980/* Many folk need no more in the way of final link than this, once
13981 got entry reference counting is enabled. */
13982
13983bfd_boolean
13984bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13985{
13986 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13987 return FALSE;
13988
13989 /* Invoke the regular ELF backend linker to do all the work. */
13990 return bfd_elf_final_link (abfd, info);
13991}
13992
13993bfd_boolean
13994bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13995{
a50b1753 13996 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13997
13998 if (rcookie->bad_symtab)
13999 rcookie->rel = rcookie->rels;
14000
14001 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14002 {
14003 unsigned long r_symndx;
14004
14005 if (! rcookie->bad_symtab)
14006 if (rcookie->rel->r_offset > offset)
14007 return FALSE;
14008 if (rcookie->rel->r_offset != offset)
14009 continue;
14010
14011 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14012 if (r_symndx == STN_UNDEF)
c152c796
AM
14013 return TRUE;
14014
14015 if (r_symndx >= rcookie->locsymcount
14016 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14017 {
14018 struct elf_link_hash_entry *h;
14019
14020 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14021
14022 while (h->root.type == bfd_link_hash_indirect
14023 || h->root.type == bfd_link_hash_warning)
14024 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14025
14026 if ((h->root.type == bfd_link_hash_defined
14027 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14028 && (h->root.u.def.section->owner != rcookie->abfd
14029 || h->root.u.def.section->kept_section != NULL
14030 || discarded_section (h->root.u.def.section)))
c152c796 14031 return TRUE;
c152c796
AM
14032 }
14033 else
14034 {
14035 /* It's not a relocation against a global symbol,
14036 but it could be a relocation against a local
14037 symbol for a discarded section. */
14038 asection *isec;
14039 Elf_Internal_Sym *isym;
14040
14041 /* Need to: get the symbol; get the section. */
14042 isym = &rcookie->locsyms[r_symndx];
cb33740c 14043 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14044 if (isec != NULL
14045 && (isec->kept_section != NULL
14046 || discarded_section (isec)))
cb33740c 14047 return TRUE;
c152c796
AM
14048 }
14049 return FALSE;
14050 }
14051 return FALSE;
14052}
14053
14054/* Discard unneeded references to discarded sections.
75938853
AM
14055 Returns -1 on error, 1 if any section's size was changed, 0 if
14056 nothing changed. This function assumes that the relocations are in
14057 sorted order, which is true for all known assemblers. */
c152c796 14058
75938853 14059int
c152c796
AM
14060bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14061{
14062 struct elf_reloc_cookie cookie;
18cd5bce 14063 asection *o;
c152c796 14064 bfd *abfd;
75938853 14065 int changed = 0;
c152c796
AM
14066
14067 if (info->traditional_format
14068 || !is_elf_hash_table (info->hash))
75938853 14069 return 0;
c152c796 14070
18cd5bce
AM
14071 o = bfd_get_section_by_name (output_bfd, ".stab");
14072 if (o != NULL)
c152c796 14073 {
18cd5bce 14074 asection *i;
c152c796 14075
18cd5bce 14076 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14077 {
18cd5bce
AM
14078 if (i->size == 0
14079 || i->reloc_count == 0
14080 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14081 continue;
c152c796 14082
18cd5bce
AM
14083 abfd = i->owner;
14084 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14085 continue;
c152c796 14086
18cd5bce 14087 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14088 return -1;
c152c796 14089
18cd5bce
AM
14090 if (_bfd_discard_section_stabs (abfd, i,
14091 elf_section_data (i)->sec_info,
5241d853
RS
14092 bfd_elf_reloc_symbol_deleted_p,
14093 &cookie))
75938853 14094 changed = 1;
18cd5bce
AM
14095
14096 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14097 }
18cd5bce
AM
14098 }
14099
2f0c68f2
CM
14100 o = NULL;
14101 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14102 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14103 if (o != NULL)
14104 {
14105 asection *i;
d7153c4a 14106 int eh_changed = 0;
79a94a2a 14107 unsigned int eh_alignment;
c152c796 14108
18cd5bce 14109 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14110 {
18cd5bce
AM
14111 if (i->size == 0)
14112 continue;
14113
14114 abfd = i->owner;
14115 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14116 continue;
14117
14118 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14119 return -1;
18cd5bce
AM
14120
14121 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14122 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14123 bfd_elf_reloc_symbol_deleted_p,
14124 &cookie))
d7153c4a
AM
14125 {
14126 eh_changed = 1;
14127 if (i->size != i->rawsize)
14128 changed = 1;
14129 }
18cd5bce
AM
14130
14131 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14132 }
9866ffe2 14133
79a94a2a 14134 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14135 /* Skip over zero terminator, and prevent empty sections from
14136 adding alignment padding at the end. */
14137 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14138 if (i->size == 0)
14139 i->flags |= SEC_EXCLUDE;
14140 else if (i->size > 4)
14141 break;
14142 /* The last non-empty eh_frame section doesn't need padding. */
14143 if (i != NULL)
14144 i = i->map_tail.s;
14145 /* Any prior sections must pad the last FDE out to the output
14146 section alignment. Otherwise we might have zero padding
14147 between sections, which would be seen as a terminator. */
14148 for (; i != NULL; i = i->map_tail.s)
14149 if (i->size == 4)
14150 /* All but the last zero terminator should have been removed. */
14151 BFD_FAIL ();
14152 else
14153 {
14154 bfd_size_type size
14155 = (i->size + eh_alignment - 1) & -eh_alignment;
14156 if (i->size != size)
af471f82 14157 {
9866ffe2
AM
14158 i->size = size;
14159 changed = 1;
14160 eh_changed = 1;
af471f82 14161 }
9866ffe2 14162 }
d7153c4a
AM
14163 if (eh_changed)
14164 elf_link_hash_traverse (elf_hash_table (info),
14165 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14166 }
c152c796 14167
18cd5bce
AM
14168 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14169 {
14170 const struct elf_backend_data *bed;
57963c05 14171 asection *s;
c152c796 14172
18cd5bce
AM
14173 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14174 continue;
57963c05
AM
14175 s = abfd->sections;
14176 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14177 continue;
18cd5bce
AM
14178
14179 bed = get_elf_backend_data (abfd);
14180
14181 if (bed->elf_backend_discard_info != NULL)
14182 {
14183 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14184 return -1;
18cd5bce
AM
14185
14186 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14187 changed = 1;
18cd5bce
AM
14188
14189 fini_reloc_cookie (&cookie, abfd);
14190 }
c152c796
AM
14191 }
14192
2f0c68f2
CM
14193 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14194 _bfd_elf_end_eh_frame_parsing (info);
14195
14196 if (info->eh_frame_hdr_type
0e1862bb 14197 && !bfd_link_relocatable (info)
c152c796 14198 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14199 changed = 1;
c152c796 14200
75938853 14201 return changed;
c152c796 14202}
082b7297 14203
43e1669b 14204bfd_boolean
0c511000 14205_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14206 asection *sec,
c0f00686 14207 struct bfd_link_info *info)
082b7297
L
14208{
14209 flagword flags;
c77ec726 14210 const char *name, *key;
082b7297
L
14211 struct bfd_section_already_linked *l;
14212 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14213
c77ec726
AM
14214 if (sec->output_section == bfd_abs_section_ptr)
14215 return FALSE;
0c511000 14216
c77ec726 14217 flags = sec->flags;
0c511000 14218
c77ec726
AM
14219 /* Return if it isn't a linkonce section. A comdat group section
14220 also has SEC_LINK_ONCE set. */
14221 if ((flags & SEC_LINK_ONCE) == 0)
14222 return FALSE;
0c511000 14223
c77ec726
AM
14224 /* Don't put group member sections on our list of already linked
14225 sections. They are handled as a group via their group section. */
14226 if (elf_sec_group (sec) != NULL)
14227 return FALSE;
0c511000 14228
c77ec726
AM
14229 /* For a SHT_GROUP section, use the group signature as the key. */
14230 name = sec->name;
14231 if ((flags & SEC_GROUP) != 0
14232 && elf_next_in_group (sec) != NULL
14233 && elf_group_name (elf_next_in_group (sec)) != NULL)
14234 key = elf_group_name (elf_next_in_group (sec));
14235 else
14236 {
14237 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14238 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14239 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14240 key++;
0c511000 14241 else
c77ec726
AM
14242 /* Must be a user linkonce section that doesn't follow gcc's
14243 naming convention. In this case we won't be matching
14244 single member groups. */
14245 key = name;
0c511000 14246 }
6d2cd210 14247
c77ec726 14248 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14249
14250 for (l = already_linked_list->entry; l != NULL; l = l->next)
14251 {
c2370991 14252 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14253 sections with a signature of <key> (<key> is some string),
14254 and linkonce sections named .gnu.linkonce.<type>.<key>.
14255 Match like sections. LTO plugin sections are an exception.
14256 They are always named .gnu.linkonce.t.<key> and match either
14257 type of section. */
14258 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14259 && ((flags & SEC_GROUP) != 0
14260 || strcmp (name, l->sec->name) == 0))
14261 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14262 {
14263 /* The section has already been linked. See if we should
6d2cd210 14264 issue a warning. */
c77ec726
AM
14265 if (!_bfd_handle_already_linked (sec, l, info))
14266 return FALSE;
082b7297 14267
c77ec726 14268 if (flags & SEC_GROUP)
3d7f7666 14269 {
c77ec726
AM
14270 asection *first = elf_next_in_group (sec);
14271 asection *s = first;
3d7f7666 14272
c77ec726 14273 while (s != NULL)
3d7f7666 14274 {
c77ec726
AM
14275 s->output_section = bfd_abs_section_ptr;
14276 /* Record which group discards it. */
14277 s->kept_section = l->sec;
14278 s = elf_next_in_group (s);
14279 /* These lists are circular. */
14280 if (s == first)
14281 break;
3d7f7666
L
14282 }
14283 }
082b7297 14284
43e1669b 14285 return TRUE;
082b7297
L
14286 }
14287 }
14288
c77ec726
AM
14289 /* A single member comdat group section may be discarded by a
14290 linkonce section and vice versa. */
14291 if ((flags & SEC_GROUP) != 0)
3d7f7666 14292 {
c77ec726 14293 asection *first = elf_next_in_group (sec);
c2370991 14294
c77ec726
AM
14295 if (first != NULL && elf_next_in_group (first) == first)
14296 /* Check this single member group against linkonce sections. */
14297 for (l = already_linked_list->entry; l != NULL; l = l->next)
14298 if ((l->sec->flags & SEC_GROUP) == 0
14299 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14300 {
14301 first->output_section = bfd_abs_section_ptr;
14302 first->kept_section = l->sec;
14303 sec->output_section = bfd_abs_section_ptr;
14304 break;
14305 }
14306 }
14307 else
14308 /* Check this linkonce section against single member groups. */
14309 for (l = already_linked_list->entry; l != NULL; l = l->next)
14310 if (l->sec->flags & SEC_GROUP)
6d2cd210 14311 {
c77ec726 14312 asection *first = elf_next_in_group (l->sec);
6d2cd210 14313
c77ec726
AM
14314 if (first != NULL
14315 && elf_next_in_group (first) == first
14316 && bfd_elf_match_symbols_in_sections (first, sec, info))
14317 {
14318 sec->output_section = bfd_abs_section_ptr;
14319 sec->kept_section = first;
14320 break;
14321 }
6d2cd210 14322 }
0c511000 14323
c77ec726
AM
14324 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14325 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14326 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14327 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14328 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14329 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14330 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14331 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14332 The reverse order cannot happen as there is never a bfd with only the
14333 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14334 matter as here were are looking only for cross-bfd sections. */
14335
14336 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14337 for (l = already_linked_list->entry; l != NULL; l = l->next)
14338 if ((l->sec->flags & SEC_GROUP) == 0
14339 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14340 {
14341 if (abfd != l->sec->owner)
14342 sec->output_section = bfd_abs_section_ptr;
14343 break;
14344 }
80c29487 14345
082b7297 14346 /* This is the first section with this name. Record it. */
c77ec726 14347 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14348 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14349 return sec->output_section == bfd_abs_section_ptr;
082b7297 14350}
81e1b023 14351
a4d8e49b
L
14352bfd_boolean
14353_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14354{
14355 return sym->st_shndx == SHN_COMMON;
14356}
14357
14358unsigned int
14359_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14360{
14361 return SHN_COMMON;
14362}
14363
14364asection *
14365_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14366{
14367 return bfd_com_section_ptr;
14368}
10455f89
HPN
14369
14370bfd_vma
14371_bfd_elf_default_got_elt_size (bfd *abfd,
14372 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14373 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14374 bfd *ibfd ATTRIBUTE_UNUSED,
14375 unsigned long symndx ATTRIBUTE_UNUSED)
14376{
14377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14378 return bed->s->arch_size / 8;
14379}
83bac4b0
NC
14380
14381/* Routines to support the creation of dynamic relocs. */
14382
83bac4b0
NC
14383/* Returns the name of the dynamic reloc section associated with SEC. */
14384
14385static const char *
14386get_dynamic_reloc_section_name (bfd * abfd,
14387 asection * sec,
14388 bfd_boolean is_rela)
14389{
ddcf1fcf
BS
14390 char *name;
14391 const char *old_name = bfd_get_section_name (NULL, sec);
14392 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14393
ddcf1fcf 14394 if (old_name == NULL)
83bac4b0
NC
14395 return NULL;
14396
ddcf1fcf 14397 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14398 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14399
14400 return name;
14401}
14402
14403/* Returns the dynamic reloc section associated with SEC.
14404 If necessary compute the name of the dynamic reloc section based
14405 on SEC's name (looked up in ABFD's string table) and the setting
14406 of IS_RELA. */
14407
14408asection *
14409_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14410 asection * sec,
14411 bfd_boolean is_rela)
14412{
14413 asection * reloc_sec = elf_section_data (sec)->sreloc;
14414
14415 if (reloc_sec == NULL)
14416 {
14417 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14418
14419 if (name != NULL)
14420 {
3d4d4302 14421 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14422
14423 if (reloc_sec != NULL)
14424 elf_section_data (sec)->sreloc = reloc_sec;
14425 }
14426 }
14427
14428 return reloc_sec;
14429}
14430
14431/* Returns the dynamic reloc section associated with SEC. If the
14432 section does not exist it is created and attached to the DYNOBJ
14433 bfd and stored in the SRELOC field of SEC's elf_section_data
14434 structure.
f8076f98 14435
83bac4b0
NC
14436 ALIGNMENT is the alignment for the newly created section and
14437 IS_RELA defines whether the name should be .rela.<SEC's name>
14438 or .rel.<SEC's name>. The section name is looked up in the
14439 string table associated with ABFD. */
14440
14441asection *
ca4be51c
AM
14442_bfd_elf_make_dynamic_reloc_section (asection *sec,
14443 bfd *dynobj,
14444 unsigned int alignment,
14445 bfd *abfd,
14446 bfd_boolean is_rela)
83bac4b0
NC
14447{
14448 asection * reloc_sec = elf_section_data (sec)->sreloc;
14449
14450 if (reloc_sec == NULL)
14451 {
14452 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14453
14454 if (name == NULL)
14455 return NULL;
14456
3d4d4302 14457 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14458
14459 if (reloc_sec == NULL)
14460 {
3d4d4302
AM
14461 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14462 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14463 if ((sec->flags & SEC_ALLOC) != 0)
14464 flags |= SEC_ALLOC | SEC_LOAD;
14465
3d4d4302 14466 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14467 if (reloc_sec != NULL)
14468 {
8877b5e5
AM
14469 /* _bfd_elf_get_sec_type_attr chooses a section type by
14470 name. Override as it may be wrong, eg. for a user
14471 section named "auto" we'll get ".relauto" which is
14472 seen to be a .rela section. */
14473 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14474 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14475 reloc_sec = NULL;
14476 }
14477 }
14478
14479 elf_section_data (sec)->sreloc = reloc_sec;
14480 }
14481
14482 return reloc_sec;
14483}
1338dd10 14484
bffebb6b
AM
14485/* Copy the ELF symbol type and other attributes for a linker script
14486 assignment from HSRC to HDEST. Generally this should be treated as
14487 if we found a strong non-dynamic definition for HDEST (except that
14488 ld ignores multiple definition errors). */
1338dd10 14489void
bffebb6b
AM
14490_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14491 struct bfd_link_hash_entry *hdest,
14492 struct bfd_link_hash_entry *hsrc)
1338dd10 14493{
bffebb6b
AM
14494 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14495 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14496 Elf_Internal_Sym isym;
1338dd10
PB
14497
14498 ehdest->type = ehsrc->type;
35fc36a8 14499 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14500
14501 isym.st_other = ehsrc->other;
b8417128 14502 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14503}
351f65ca
L
14504
14505/* Append a RELA relocation REL to section S in BFD. */
14506
14507void
14508elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14509{
14510 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14511 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14512 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14513 bed->s->swap_reloca_out (abfd, rel, loc);
14514}
14515
14516/* Append a REL relocation REL to section S in BFD. */
14517
14518void
14519elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14520{
14521 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14522 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14523 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14524 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14525}
7dba9362
AM
14526
14527/* Define __start, __stop, .startof. or .sizeof. symbol. */
14528
14529struct bfd_link_hash_entry *
14530bfd_elf_define_start_stop (struct bfd_link_info *info,
14531 const char *symbol, asection *sec)
14532{
487b6440 14533 struct elf_link_hash_entry *h;
7dba9362 14534
487b6440
AM
14535 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14536 FALSE, FALSE, TRUE);
14537 if (h != NULL
14538 && (h->root.type == bfd_link_hash_undefined
14539 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14540 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14541 {
bf3077a6 14542 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14543 h->root.type = bfd_link_hash_defined;
14544 h->root.u.def.section = sec;
14545 h->root.u.def.value = 0;
14546 h->def_regular = 1;
14547 h->def_dynamic = 0;
14548 h->start_stop = 1;
14549 h->u2.start_stop_section = sec;
14550 if (symbol[0] == '.')
14551 {
14552 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14553 const struct elf_backend_data *bed;
14554 bed = get_elf_backend_data (info->output_bfd);
14555 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14556 }
36b8fda5
AM
14557 else
14558 {
14559 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14560 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14561 if (was_dynamic)
36b8fda5
AM
14562 bfd_elf_link_record_dynamic_symbol (info, h);
14563 }
487b6440 14564 return &h->root;
7dba9362 14565 }
487b6440 14566 return NULL;
7dba9362 14567}
This page took 2.657624 seconds and 4 git commands to generate.