Don't complain undefined weak dynamic reference
[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;
9bff840e 4079 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
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. */
fe3fef62
AM
4445 if (elf_bad_symtab (abfd))
4446 continue;
4447
4448 /* If we aren't prepared to handle locals within the globals
6835821b 4449 then we'll likely segfault on a NULL section. */
fe3fef62
AM
4450 bfd_set_error (bfd_error_bad_value);
4451 goto error_free_vers;
3e7a7d11
NC
4452
4453 case STB_GLOBAL:
a4d8e49b 4454 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4455 flags = BSF_GLOBAL;
3e7a7d11
NC
4456 break;
4457
4458 case STB_WEAK:
4459 flags = BSF_WEAK;
4460 break;
4461
4462 case STB_GNU_UNIQUE:
4463 flags = BSF_GNU_UNIQUE;
4464 break;
4465
4466 default:
4ad4eba5 4467 /* Leave it up to the processor backend. */
3e7a7d11 4468 break;
4ad4eba5
AM
4469 }
4470
4471 if (isym->st_shndx == SHN_UNDEF)
4472 sec = bfd_und_section_ptr;
cb33740c
AM
4473 else if (isym->st_shndx == SHN_ABS)
4474 sec = bfd_abs_section_ptr;
4475 else if (isym->st_shndx == SHN_COMMON)
4476 {
4477 sec = bfd_com_section_ptr;
4478 /* What ELF calls the size we call the value. What ELF
4479 calls the value we call the alignment. */
4480 value = isym->st_size;
4481 }
4482 else
4ad4eba5
AM
4483 {
4484 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4485 if (sec == NULL)
4486 sec = bfd_abs_section_ptr;
dbaa2011 4487 else if (discarded_section (sec))
529fcb95 4488 {
e5d08002
L
4489 /* Symbols from discarded section are undefined. We keep
4490 its visibility. */
529fcb95 4491 sec = bfd_und_section_ptr;
97196564 4492 discarded = TRUE;
529fcb95
PB
4493 isym->st_shndx = SHN_UNDEF;
4494 }
4ad4eba5
AM
4495 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4496 value -= sec->vma;
4497 }
4ad4eba5
AM
4498
4499 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4500 isym->st_name);
4501 if (name == NULL)
4502 goto error_free_vers;
4503
4504 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4505 && (abfd->flags & BFD_PLUGIN) != 0)
4506 {
4507 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4508
4509 if (xc == NULL)
4510 {
4511 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4512 | SEC_EXCLUDE);
4513 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4514 if (xc == NULL)
4515 goto error_free_vers;
4516 }
4517 sec = xc;
4518 }
4519 else if (isym->st_shndx == SHN_COMMON
4520 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4521 && !bfd_link_relocatable (info))
4ad4eba5
AM
4522 {
4523 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4524
4525 if (tcomm == NULL)
4526 {
02d00247
AM
4527 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4528 | SEC_LINKER_CREATED);
4529 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4530 if (tcomm == NULL)
4ad4eba5
AM
4531 goto error_free_vers;
4532 }
4533 sec = tcomm;
4534 }
66eb6687 4535 else if (bed->elf_add_symbol_hook)
4ad4eba5 4536 {
66eb6687
AM
4537 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4538 &sec, &value))
4ad4eba5
AM
4539 goto error_free_vers;
4540
4541 /* The hook function sets the name to NULL if this symbol
4542 should be skipped for some reason. */
4543 if (name == NULL)
4544 continue;
4545 }
4546
4547 /* Sanity check that all possibilities were handled. */
4548 if (sec == NULL)
4549 {
4550 bfd_set_error (bfd_error_bad_value);
4551 goto error_free_vers;
4552 }
4553
191c0c42
AM
4554 /* Silently discard TLS symbols from --just-syms. There's
4555 no way to combine a static TLS block with a new TLS block
4556 for this executable. */
4557 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4558 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4559 continue;
4560
4ad4eba5
AM
4561 if (bfd_is_und_section (sec)
4562 || bfd_is_com_section (sec))
4563 definition = FALSE;
4564 else
4565 definition = TRUE;
4566
4567 size_change_ok = FALSE;
66eb6687 4568 type_change_ok = bed->type_change_ok;
37a9e49a 4569 old_weak = FALSE;
6e33951e 4570 matched = FALSE;
4ad4eba5
AM
4571 old_alignment = 0;
4572 old_bfd = NULL;
af44c138 4573 new_sec = sec;
4ad4eba5 4574
66eb6687 4575 if (is_elf_hash_table (htab))
4ad4eba5
AM
4576 {
4577 Elf_Internal_Versym iver;
4578 unsigned int vernum = 0;
4579 bfd_boolean skip;
4580
fc0e6df6 4581 if (ever == NULL)
4ad4eba5 4582 {
fc0e6df6
PB
4583 if (info->default_imported_symver)
4584 /* Use the default symbol version created earlier. */
4585 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4586 else
4587 iver.vs_vers = 0;
4588 }
be22c732
NC
4589 else if (ever >= extversym_end)
4590 {
4591 /* xgettext:c-format */
4592 _bfd_error_handler (_("%pB: not enough version information"),
4593 abfd);
4594 bfd_set_error (bfd_error_bad_value);
4595 goto error_free_vers;
4596 }
fc0e6df6
PB
4597 else
4598 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4599
4600 vernum = iver.vs_vers & VERSYM_VERSION;
4601
4602 /* If this is a hidden symbol, or if it is not version
4603 1, we append the version name to the symbol name.
cc86ff91
EB
4604 However, we do not modify a non-hidden absolute symbol
4605 if it is not a function, because it might be the version
4606 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4607 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4608 || (vernum > 1
4609 && (!bfd_is_abs_section (sec)
4610 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4611 {
4612 const char *verstr;
4613 size_t namelen, verlen, newlen;
4614 char *newname, *p;
4615
4616 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4617 {
fc0e6df6
PB
4618 if (vernum > elf_tdata (abfd)->cverdefs)
4619 verstr = NULL;
4620 else if (vernum > 1)
4621 verstr =
4622 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4623 else
4624 verstr = "";
4ad4eba5 4625
fc0e6df6 4626 if (verstr == NULL)
4ad4eba5 4627 {
4eca0228 4628 _bfd_error_handler
695344c0 4629 /* xgettext:c-format */
871b3ab2 4630 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4631 abfd, name, vernum,
4632 elf_tdata (abfd)->cverdefs);
4633 bfd_set_error (bfd_error_bad_value);
4634 goto error_free_vers;
4ad4eba5 4635 }
fc0e6df6
PB
4636 }
4637 else
4638 {
4639 /* We cannot simply test for the number of
4640 entries in the VERNEED section since the
4641 numbers for the needed versions do not start
4642 at 0. */
4643 Elf_Internal_Verneed *t;
4644
4645 verstr = NULL;
4646 for (t = elf_tdata (abfd)->verref;
4647 t != NULL;
4648 t = t->vn_nextref)
4ad4eba5 4649 {
fc0e6df6 4650 Elf_Internal_Vernaux *a;
4ad4eba5 4651
fc0e6df6
PB
4652 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4653 {
4654 if (a->vna_other == vernum)
4ad4eba5 4655 {
fc0e6df6
PB
4656 verstr = a->vna_nodename;
4657 break;
4ad4eba5 4658 }
4ad4eba5 4659 }
fc0e6df6
PB
4660 if (a != NULL)
4661 break;
4662 }
4663 if (verstr == NULL)
4664 {
4eca0228 4665 _bfd_error_handler
695344c0 4666 /* xgettext:c-format */
871b3ab2 4667 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4668 abfd, name, vernum);
4669 bfd_set_error (bfd_error_bad_value);
4670 goto error_free_vers;
4ad4eba5 4671 }
4ad4eba5 4672 }
fc0e6df6
PB
4673
4674 namelen = strlen (name);
4675 verlen = strlen (verstr);
4676 newlen = namelen + verlen + 2;
4677 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4678 && isym->st_shndx != SHN_UNDEF)
4679 ++newlen;
4680
a50b1753 4681 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4682 if (newname == NULL)
4683 goto error_free_vers;
4684 memcpy (newname, name, namelen);
4685 p = newname + namelen;
4686 *p++ = ELF_VER_CHR;
4687 /* If this is a defined non-hidden version symbol,
4688 we add another @ to the name. This indicates the
4689 default version of the symbol. */
4690 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4691 && isym->st_shndx != SHN_UNDEF)
4692 *p++ = ELF_VER_CHR;
4693 memcpy (p, verstr, verlen + 1);
4694
4695 name = newname;
4ad4eba5
AM
4696 }
4697
cd3416da
AM
4698 /* If this symbol has default visibility and the user has
4699 requested we not re-export it, then mark it as hidden. */
a0d49154 4700 if (!bfd_is_und_section (sec)
cd3416da 4701 && !dynamic
ce875075 4702 && abfd->no_export
cd3416da
AM
4703 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4704 isym->st_other = (STV_HIDDEN
4705 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4706
4f3fedcf
AM
4707 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4708 sym_hash, &old_bfd, &old_weak,
4709 &old_alignment, &skip, &override,
6e33951e
L
4710 &type_change_ok, &size_change_ok,
4711 &matched))
4ad4eba5
AM
4712 goto error_free_vers;
4713
4714 if (skip)
4715 continue;
4716
6e33951e
L
4717 /* Override a definition only if the new symbol matches the
4718 existing one. */
4719 if (override && matched)
4ad4eba5
AM
4720 definition = FALSE;
4721
4722 h = *sym_hash;
4723 while (h->root.type == bfd_link_hash_indirect
4724 || h->root.type == bfd_link_hash_warning)
4725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4726
4ad4eba5 4727 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4728 && vernum > 1
4729 && definition)
4730 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4731 }
4732
4733 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4734 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4735 (struct bfd_link_hash_entry **) sym_hash)))
4736 goto error_free_vers;
4737
ac98f9e2
L
4738 if ((abfd->flags & DYNAMIC) == 0
4739 && (bfd_get_flavour (info->output_bfd)
4740 == bfd_target_elf_flavour))
4741 {
4742 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4743 elf_tdata (info->output_bfd)->has_gnu_symbols
4744 |= elf_gnu_symbol_ifunc;
4745 if ((flags & BSF_GNU_UNIQUE))
4746 elf_tdata (info->output_bfd)->has_gnu_symbols
4747 |= elf_gnu_symbol_unique;
4748 }
a43942db 4749
4ad4eba5 4750 h = *sym_hash;
90c984fc
L
4751 /* We need to make sure that indirect symbol dynamic flags are
4752 updated. */
4753 hi = h;
4ad4eba5
AM
4754 while (h->root.type == bfd_link_hash_indirect
4755 || h->root.type == bfd_link_hash_warning)
4756 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4757
97196564
L
4758 /* Setting the index to -3 tells elf_link_output_extsym that
4759 this symbol is defined in a discarded section. */
4760 if (discarded)
4761 h->indx = -3;
4762
4ad4eba5
AM
4763 *sym_hash = h;
4764
37a9e49a 4765 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4766 if (dynamic
4767 && definition
37a9e49a 4768 && new_weak
fcb93ecf 4769 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4770 && is_elf_hash_table (htab)
60d67dc8 4771 && h->u.alias == NULL)
4ad4eba5
AM
4772 {
4773 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4774 a dynamic object, using the alias field. Later in this
4775 function we will set the alias field to the correct
4ad4eba5
AM
4776 value. We only put non-function symbols from dynamic
4777 objects on this list, because that happens to be the only
4778 time we need to know the normal symbol corresponding to a
4779 weak symbol, and the information is time consuming to
60d67dc8 4780 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4781 then this symbol was already defined by some previous
4782 dynamic object, and we will be using that previous
4783 definition anyhow. */
4784
60d67dc8 4785 h->u.alias = weaks;
4ad4eba5 4786 weaks = h;
4ad4eba5
AM
4787 }
4788
4789 /* Set the alignment of a common symbol. */
a4d8e49b 4790 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4791 && h->root.type == bfd_link_hash_common)
4792 {
4793 unsigned int align;
4794
a4d8e49b 4795 if (common)
af44c138
L
4796 align = bfd_log2 (isym->st_value);
4797 else
4798 {
4799 /* The new symbol is a common symbol in a shared object.
4800 We need to get the alignment from the section. */
4801 align = new_sec->alignment_power;
4802 }
595213d4 4803 if (align > old_alignment)
4ad4eba5
AM
4804 h->root.u.c.p->alignment_power = align;
4805 else
4806 h->root.u.c.p->alignment_power = old_alignment;
4807 }
4808
66eb6687 4809 if (is_elf_hash_table (htab))
4ad4eba5 4810 {
4f3fedcf
AM
4811 /* Set a flag in the hash table entry indicating the type of
4812 reference or definition we just found. A dynamic symbol
4813 is one which is referenced or defined by both a regular
4814 object and a shared object. */
4815 bfd_boolean dynsym = FALSE;
4816
4817 /* Plugin symbols aren't normal. Don't set def_regular or
4818 ref_regular for them, or make them dynamic. */
4819 if ((abfd->flags & BFD_PLUGIN) != 0)
4820 ;
4821 else if (! dynamic)
4822 {
4823 if (! definition)
4824 {
4825 h->ref_regular = 1;
4826 if (bind != STB_WEAK)
4827 h->ref_regular_nonweak = 1;
4828 }
4829 else
4830 {
4831 h->def_regular = 1;
4832 if (h->def_dynamic)
4833 {
4834 h->def_dynamic = 0;
4835 h->ref_dynamic = 1;
4836 }
4837 }
4838
4839 /* If the indirect symbol has been forced local, don't
4840 make the real symbol dynamic. */
4841 if ((h == hi || !hi->forced_local)
0e1862bb 4842 && (bfd_link_dll (info)
4f3fedcf
AM
4843 || h->def_dynamic
4844 || h->ref_dynamic))
4845 dynsym = TRUE;
4846 }
4847 else
4848 {
4849 if (! definition)
4850 {
4851 h->ref_dynamic = 1;
4852 hi->ref_dynamic = 1;
4853 }
4854 else
4855 {
4856 h->def_dynamic = 1;
4857 hi->def_dynamic = 1;
4858 }
4859
4860 /* If the indirect symbol has been forced local, don't
4861 make the real symbol dynamic. */
4862 if ((h == hi || !hi->forced_local)
4863 && (h->def_regular
4864 || h->ref_regular
60d67dc8
AM
4865 || (h->is_weakalias
4866 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4867 dynsym = TRUE;
4868 }
4869
4870 /* Check to see if we need to add an indirect symbol for
4871 the default name. */
4872 if (definition
4873 || (!override && h->root.type == bfd_link_hash_common))
4874 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4875 sec, value, &old_bfd, &dynsym))
4876 goto error_free_vers;
4ad4eba5
AM
4877
4878 /* Check the alignment when a common symbol is involved. This
4879 can change when a common symbol is overridden by a normal
4880 definition or a common symbol is ignored due to the old
4881 normal definition. We need to make sure the maximum
4882 alignment is maintained. */
a4d8e49b 4883 if ((old_alignment || common)
4ad4eba5
AM
4884 && h->root.type != bfd_link_hash_common)
4885 {
4886 unsigned int common_align;
4887 unsigned int normal_align;
4888 unsigned int symbol_align;
4889 bfd *normal_bfd;
4890 bfd *common_bfd;
4891
3a81e825
AM
4892 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4893 || h->root.type == bfd_link_hash_defweak);
4894
4ad4eba5
AM
4895 symbol_align = ffs (h->root.u.def.value) - 1;
4896 if (h->root.u.def.section->owner != NULL
0616a280
AM
4897 && (h->root.u.def.section->owner->flags
4898 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4899 {
4900 normal_align = h->root.u.def.section->alignment_power;
4901 if (normal_align > symbol_align)
4902 normal_align = symbol_align;
4903 }
4904 else
4905 normal_align = symbol_align;
4906
4907 if (old_alignment)
4908 {
4909 common_align = old_alignment;
4910 common_bfd = old_bfd;
4911 normal_bfd = abfd;
4912 }
4913 else
4914 {
4915 common_align = bfd_log2 (isym->st_value);
4916 common_bfd = abfd;
4917 normal_bfd = old_bfd;
4918 }
4919
4920 if (normal_align < common_align)
d07676f8
NC
4921 {
4922 /* PR binutils/2735 */
4923 if (normal_bfd == NULL)
4eca0228 4924 _bfd_error_handler
695344c0 4925 /* xgettext:c-format */
9793eb77 4926 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4927 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4928 1 << common_align, name, common_bfd,
4929 1 << normal_align, h->root.u.def.section);
d07676f8 4930 else
4eca0228 4931 _bfd_error_handler
695344c0 4932 /* xgettext:c-format */
9793eb77 4933 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4934 " is smaller than %u in %pB"),
c08bb8dd
AM
4935 1 << normal_align, name, normal_bfd,
4936 1 << common_align, common_bfd);
d07676f8 4937 }
4ad4eba5
AM
4938 }
4939
83ad0046 4940 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4941 if (isym->st_size != 0
4942 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4943 && (definition || h->size == 0))
4944 {
83ad0046
L
4945 if (h->size != 0
4946 && h->size != isym->st_size
4947 && ! size_change_ok)
4eca0228 4948 _bfd_error_handler
695344c0 4949 /* xgettext:c-format */
9793eb77 4950 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4951 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4952 name, (uint64_t) h->size, old_bfd,
4953 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4954
4955 h->size = isym->st_size;
4956 }
4957
4958 /* If this is a common symbol, then we always want H->SIZE
4959 to be the size of the common symbol. The code just above
4960 won't fix the size if a common symbol becomes larger. We
4961 don't warn about a size change here, because that is
4f3fedcf 4962 covered by --warn-common. Allow changes between different
fcb93ecf 4963 function types. */
4ad4eba5
AM
4964 if (h->root.type == bfd_link_hash_common)
4965 h->size = h->root.u.c.size;
4966
4967 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4968 && ((definition && !new_weak)
4969 || (old_weak && h->root.type == bfd_link_hash_common)
4970 || h->type == STT_NOTYPE))
4ad4eba5 4971 {
2955ec4c
L
4972 unsigned int type = ELF_ST_TYPE (isym->st_info);
4973
4974 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4975 symbol. */
4976 if (type == STT_GNU_IFUNC
4977 && (abfd->flags & DYNAMIC) != 0)
4978 type = STT_FUNC;
4ad4eba5 4979
2955ec4c
L
4980 if (h->type != type)
4981 {
4982 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4983 /* xgettext:c-format */
4eca0228 4984 _bfd_error_handler
9793eb77 4985 (_("warning: type of symbol `%s' changed"
871b3ab2 4986 " from %d to %d in %pB"),
c08bb8dd 4987 name, h->type, type, abfd);
2955ec4c
L
4988
4989 h->type = type;
4990 }
4ad4eba5
AM
4991 }
4992
54ac0771 4993 /* Merge st_other field. */
b8417128 4994 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4995
c3df8c14 4996 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4997 if (definition
4998 && (sec->flags & SEC_DEBUGGING)
4999 && !bfd_link_relocatable (info))
c3df8c14
AM
5000 dynsym = FALSE;
5001
4f3fedcf
AM
5002 /* Nor should we make plugin symbols dynamic. */
5003 if ((abfd->flags & BFD_PLUGIN) != 0)
5004 dynsym = FALSE;
5005
35fc36a8 5006 if (definition)
35399224
L
5007 {
5008 h->target_internal = isym->st_target_internal;
5009 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5010 }
35fc36a8 5011
4ad4eba5
AM
5012 if (definition && !dynamic)
5013 {
5014 char *p = strchr (name, ELF_VER_CHR);
5015 if (p != NULL && p[1] != ELF_VER_CHR)
5016 {
5017 /* Queue non-default versions so that .symver x, x@FOO
5018 aliases can be checked. */
66eb6687 5019 if (!nondeflt_vers)
4ad4eba5 5020 {
66eb6687
AM
5021 amt = ((isymend - isym + 1)
5022 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5023 nondeflt_vers
5024 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5025 if (!nondeflt_vers)
5026 goto error_free_vers;
4ad4eba5 5027 }
66eb6687 5028 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5029 }
5030 }
5031
5032 if (dynsym && h->dynindx == -1)
5033 {
c152c796 5034 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5035 goto error_free_vers;
60d67dc8
AM
5036 if (h->is_weakalias
5037 && weakdef (h)->dynindx == -1)
4ad4eba5 5038 {
60d67dc8 5039 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5040 goto error_free_vers;
5041 }
5042 }
1f599d0e 5043 else if (h->dynindx != -1)
4ad4eba5
AM
5044 /* If the symbol already has a dynamic index, but
5045 visibility says it should not be visible, turn it into
5046 a local symbol. */
5047 switch (ELF_ST_VISIBILITY (h->other))
5048 {
5049 case STV_INTERNAL:
5050 case STV_HIDDEN:
5051 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5052 dynsym = FALSE;
5053 break;
5054 }
5055
aef28989
L
5056 /* Don't add DT_NEEDED for references from the dummy bfd nor
5057 for unmatched symbol. */
4ad4eba5 5058 if (!add_needed
aef28989 5059 && matched
4ad4eba5 5060 && definition
010e5ae2 5061 && ((dynsym
ffa9430d 5062 && h->ref_regular_nonweak
4f3fedcf
AM
5063 && (old_bfd == NULL
5064 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5065 || (h->ref_dynamic_nonweak
010e5ae2 5066 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5067 && !on_needed_list (elf_dt_name (abfd),
5068 htab->needed, NULL))))
4ad4eba5
AM
5069 {
5070 int ret;
5071 const char *soname = elf_dt_name (abfd);
5072
16e4ecc0
AM
5073 info->callbacks->minfo ("%!", soname, old_bfd,
5074 h->root.root.string);
5075
4ad4eba5
AM
5076 /* A symbol from a library loaded via DT_NEEDED of some
5077 other library is referenced by a regular object.
e56f61be 5078 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5079 --no-add-needed is used and the reference was not
5080 a weak one. */
4f3fedcf 5081 if (old_bfd != NULL
b918acf9 5082 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5083 {
4eca0228 5084 _bfd_error_handler
695344c0 5085 /* xgettext:c-format */
871b3ab2 5086 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5087 old_bfd, name);
ff5ac77b 5088 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5089 goto error_free_vers;
5090 }
5091
a50b1753 5092 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5093 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5094
4ad4eba5 5095 add_needed = TRUE;
7e9f0867 5096 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5097 if (ret < 0)
5098 goto error_free_vers;
5099
5100 BFD_ASSERT (ret == 0);
5101 }
5102 }
5103 }
5104
a83ef4d1
L
5105 if (info->lto_plugin_active
5106 && !bfd_link_relocatable (info)
5107 && (abfd->flags & BFD_PLUGIN) == 0
5108 && !just_syms
5109 && extsymcount)
5110 {
5111 int r_sym_shift;
5112
5113 if (bed->s->arch_size == 32)
5114 r_sym_shift = 8;
5115 else
5116 r_sym_shift = 32;
5117
5118 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5119 referenced in regular objects so that linker plugin will get
5120 the correct symbol resolution. */
5121
5122 sym_hash = elf_sym_hashes (abfd);
5123 for (s = abfd->sections; s != NULL; s = s->next)
5124 {
5125 Elf_Internal_Rela *internal_relocs;
5126 Elf_Internal_Rela *rel, *relend;
5127
5128 /* Don't check relocations in excluded sections. */
5129 if ((s->flags & SEC_RELOC) == 0
5130 || s->reloc_count == 0
5131 || (s->flags & SEC_EXCLUDE) != 0
5132 || ((info->strip == strip_all
5133 || info->strip == strip_debugger)
5134 && (s->flags & SEC_DEBUGGING) != 0))
5135 continue;
5136
5137 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5138 NULL,
5139 info->keep_memory);
5140 if (internal_relocs == NULL)
5141 goto error_free_vers;
5142
5143 rel = internal_relocs;
5144 relend = rel + s->reloc_count;
5145 for ( ; rel < relend; rel++)
5146 {
5147 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5148 struct elf_link_hash_entry *h;
5149
5150 /* Skip local symbols. */
5151 if (r_symndx < extsymoff)
5152 continue;
5153
5154 h = sym_hash[r_symndx - extsymoff];
5155 if (h != NULL)
5156 h->root.non_ir_ref_regular = 1;
5157 }
5158
5159 if (elf_section_data (s)->relocs != internal_relocs)
5160 free (internal_relocs);
5161 }
5162 }
5163
66eb6687
AM
5164 if (extversym != NULL)
5165 {
5166 free (extversym);
5167 extversym = NULL;
5168 }
5169
5170 if (isymbuf != NULL)
5171 {
5172 free (isymbuf);
5173 isymbuf = NULL;
5174 }
5175
5176 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5177 {
5178 unsigned int i;
5179
5180 /* Restore the symbol table. */
f45794cb
AM
5181 old_ent = (char *) old_tab + tabsize;
5182 memset (elf_sym_hashes (abfd), 0,
5183 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5184 htab->root.table.table = old_table;
5185 htab->root.table.size = old_size;
5186 htab->root.table.count = old_count;
66eb6687 5187 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5188 htab->root.undefs = old_undefs;
5189 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5190 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5191 free (old_strtab);
5192 old_strtab = NULL;
66eb6687
AM
5193 for (i = 0; i < htab->root.table.size; i++)
5194 {
5195 struct bfd_hash_entry *p;
5196 struct elf_link_hash_entry *h;
3e0882af
L
5197 bfd_size_type size;
5198 unsigned int alignment_power;
4070765b 5199 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5200
5201 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5202 {
5203 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5204 if (h->root.type == bfd_link_hash_warning)
5205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5206
3e0882af
L
5207 /* Preserve the maximum alignment and size for common
5208 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5209 since it can still be loaded at run time by another
3e0882af
L
5210 dynamic lib. */
5211 if (h->root.type == bfd_link_hash_common)
5212 {
5213 size = h->root.u.c.size;
5214 alignment_power = h->root.u.c.p->alignment_power;
5215 }
5216 else
5217 {
5218 size = 0;
5219 alignment_power = 0;
5220 }
4070765b 5221 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5222 will be exported when the dynamic lib becomes needed
5223 in the second pass. */
4070765b 5224 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5225 memcpy (p, old_ent, htab->root.table.entsize);
5226 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5227 h = (struct elf_link_hash_entry *) p;
5228 if (h->root.type == bfd_link_hash_warning)
5229 {
5230 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5231 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5233 }
a4542f1b 5234 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5235 {
5236 if (size > h->root.u.c.size)
5237 h->root.u.c.size = size;
5238 if (alignment_power > h->root.u.c.p->alignment_power)
5239 h->root.u.c.p->alignment_power = alignment_power;
5240 }
4070765b 5241 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5242 }
5243 }
5244
5061a885
AM
5245 /* Make a special call to the linker "notice" function to
5246 tell it that symbols added for crefs may need to be removed. */
e5034e59 5247 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5248 goto error_free_vers;
5061a885 5249
66eb6687
AM
5250 free (old_tab);
5251 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5252 alloc_mark);
5253 if (nondeflt_vers != NULL)
5254 free (nondeflt_vers);
5255 return TRUE;
5256 }
2de92251 5257
66eb6687
AM
5258 if (old_tab != NULL)
5259 {
e5034e59 5260 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5261 goto error_free_vers;
66eb6687
AM
5262 free (old_tab);
5263 old_tab = NULL;
5264 }
5265
c6e8a9a8
L
5266 /* Now that all the symbols from this input file are created, if
5267 not performing a relocatable link, handle .symver foo, foo@BAR
5268 such that any relocs against foo become foo@BAR. */
0e1862bb 5269 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5270 {
ef53be89 5271 size_t cnt, symidx;
4ad4eba5
AM
5272
5273 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5274 {
5275 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5276 char *shortname, *p;
5277
5278 p = strchr (h->root.root.string, ELF_VER_CHR);
5279 if (p == NULL
5280 || (h->root.type != bfd_link_hash_defined
5281 && h->root.type != bfd_link_hash_defweak))
5282 continue;
5283
5284 amt = p - h->root.root.string;
a50b1753 5285 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5286 if (!shortname)
5287 goto error_free_vers;
4ad4eba5
AM
5288 memcpy (shortname, h->root.root.string, amt);
5289 shortname[amt] = '\0';
5290
5291 hi = (struct elf_link_hash_entry *)
66eb6687 5292 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5293 FALSE, FALSE, FALSE);
5294 if (hi != NULL
5295 && hi->root.type == h->root.type
5296 && hi->root.u.def.value == h->root.u.def.value
5297 && hi->root.u.def.section == h->root.u.def.section)
5298 {
5299 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5300 hi->root.type = bfd_link_hash_indirect;
5301 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5302 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5303 sym_hash = elf_sym_hashes (abfd);
5304 if (sym_hash)
5305 for (symidx = 0; symidx < extsymcount; ++symidx)
5306 if (sym_hash[symidx] == hi)
5307 {
5308 sym_hash[symidx] = h;
5309 break;
5310 }
5311 }
5312 free (shortname);
5313 }
5314 free (nondeflt_vers);
5315 nondeflt_vers = NULL;
5316 }
5317
60d67dc8 5318 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5319 symbols we found. The only way to do this is to search all the
5320 symbols. Since we only need the information for non functions in
5321 dynamic objects, that's the only time we actually put anything on
5322 the list WEAKS. We need this information so that if a regular
5323 object refers to a symbol defined weakly in a dynamic object, the
5324 real symbol in the dynamic object is also put in the dynamic
5325 symbols; we also must arrange for both symbols to point to the
5326 same memory location. We could handle the general case of symbol
5327 aliasing, but a general symbol alias can only be generated in
5328 assembler code, handling it correctly would be very time
5329 consuming, and other ELF linkers don't handle general aliasing
5330 either. */
5331 if (weaks != NULL)
5332 {
5333 struct elf_link_hash_entry **hpp;
5334 struct elf_link_hash_entry **hppend;
5335 struct elf_link_hash_entry **sorted_sym_hash;
5336 struct elf_link_hash_entry *h;
5337 size_t sym_count;
5338
5339 /* Since we have to search the whole symbol list for each weak
5340 defined symbol, search time for N weak defined symbols will be
5341 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5342 amt = extsymcount;
5343 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5344 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5345 if (sorted_sym_hash == NULL)
5346 goto error_return;
5347 sym_hash = sorted_sym_hash;
5348 hpp = elf_sym_hashes (abfd);
5349 hppend = hpp + extsymcount;
5350 sym_count = 0;
5351 for (; hpp < hppend; hpp++)
5352 {
5353 h = *hpp;
5354 if (h != NULL
5355 && h->root.type == bfd_link_hash_defined
fcb93ecf 5356 && !bed->is_function_type (h->type))
4ad4eba5
AM
5357 {
5358 *sym_hash = h;
5359 sym_hash++;
5360 sym_count++;
5361 }
5362 }
5363
5364 qsort (sorted_sym_hash, sym_count,
5365 sizeof (struct elf_link_hash_entry *),
5366 elf_sort_symbol);
5367
5368 while (weaks != NULL)
5369 {
5370 struct elf_link_hash_entry *hlook;
5371 asection *slook;
5372 bfd_vma vlook;
ed54588d 5373 size_t i, j, idx = 0;
4ad4eba5
AM
5374
5375 hlook = weaks;
60d67dc8
AM
5376 weaks = hlook->u.alias;
5377 hlook->u.alias = NULL;
4ad4eba5 5378
e3e53eed
AM
5379 if (hlook->root.type != bfd_link_hash_defined
5380 && hlook->root.type != bfd_link_hash_defweak)
5381 continue;
5382
4ad4eba5
AM
5383 slook = hlook->root.u.def.section;
5384 vlook = hlook->root.u.def.value;
5385
4ad4eba5
AM
5386 i = 0;
5387 j = sym_count;
14160578 5388 while (i != j)
4ad4eba5
AM
5389 {
5390 bfd_signed_vma vdiff;
5391 idx = (i + j) / 2;
14160578 5392 h = sorted_sym_hash[idx];
4ad4eba5
AM
5393 vdiff = vlook - h->root.u.def.value;
5394 if (vdiff < 0)
5395 j = idx;
5396 else if (vdiff > 0)
5397 i = idx + 1;
5398 else
5399 {
d3435ae8 5400 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5401 if (sdiff < 0)
5402 j = idx;
5403 else if (sdiff > 0)
5404 i = idx + 1;
5405 else
14160578 5406 break;
4ad4eba5
AM
5407 }
5408 }
5409
5410 /* We didn't find a value/section match. */
14160578 5411 if (i == j)
4ad4eba5
AM
5412 continue;
5413
14160578
AM
5414 /* With multiple aliases, or when the weak symbol is already
5415 strongly defined, we have multiple matching symbols and
5416 the binary search above may land on any of them. Step
5417 one past the matching symbol(s). */
5418 while (++idx != j)
5419 {
5420 h = sorted_sym_hash[idx];
5421 if (h->root.u.def.section != slook
5422 || h->root.u.def.value != vlook)
5423 break;
5424 }
5425
5426 /* Now look back over the aliases. Since we sorted by size
5427 as well as value and section, we'll choose the one with
5428 the largest size. */
5429 while (idx-- != i)
4ad4eba5 5430 {
14160578 5431 h = sorted_sym_hash[idx];
4ad4eba5
AM
5432
5433 /* Stop if value or section doesn't match. */
14160578
AM
5434 if (h->root.u.def.section != slook
5435 || h->root.u.def.value != vlook)
4ad4eba5
AM
5436 break;
5437 else if (h != hlook)
5438 {
60d67dc8
AM
5439 struct elf_link_hash_entry *t;
5440
5441 hlook->u.alias = h;
5442 hlook->is_weakalias = 1;
5443 t = h;
5444 if (t->u.alias != NULL)
5445 while (t->u.alias != h)
5446 t = t->u.alias;
5447 t->u.alias = hlook;
4ad4eba5
AM
5448
5449 /* If the weak definition is in the list of dynamic
5450 symbols, make sure the real definition is put
5451 there as well. */
5452 if (hlook->dynindx != -1 && h->dynindx == -1)
5453 {
c152c796 5454 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5455 {
5456 err_free_sym_hash:
5457 free (sorted_sym_hash);
5458 goto error_return;
5459 }
4ad4eba5
AM
5460 }
5461
5462 /* If the real definition is in the list of dynamic
5463 symbols, make sure the weak definition is put
5464 there as well. If we don't do this, then the
5465 dynamic loader might not merge the entries for the
5466 real definition and the weak definition. */
5467 if (h->dynindx != -1 && hlook->dynindx == -1)
5468 {
c152c796 5469 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5470 goto err_free_sym_hash;
4ad4eba5
AM
5471 }
5472 break;
5473 }
5474 }
5475 }
5476
5477 free (sorted_sym_hash);
5478 }
5479
33177bb1
AM
5480 if (bed->check_directives
5481 && !(*bed->check_directives) (abfd, info))
5482 return FALSE;
85fbca6a 5483
4ad4eba5
AM
5484 /* If this is a non-traditional link, try to optimize the handling
5485 of the .stab/.stabstr sections. */
5486 if (! dynamic
5487 && ! info->traditional_format
66eb6687 5488 && is_elf_hash_table (htab)
4ad4eba5
AM
5489 && (info->strip != strip_all && info->strip != strip_debugger))
5490 {
5491 asection *stabstr;
5492
5493 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5494 if (stabstr != NULL)
5495 {
5496 bfd_size_type string_offset = 0;
5497 asection *stab;
5498
5499 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5500 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5501 && (!stab->name[5] ||
5502 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5503 && (stab->flags & SEC_MERGE) == 0
5504 && !bfd_is_abs_section (stab->output_section))
5505 {
5506 struct bfd_elf_section_data *secdata;
5507
5508 secdata = elf_section_data (stab);
66eb6687
AM
5509 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5510 stabstr, &secdata->sec_info,
4ad4eba5
AM
5511 &string_offset))
5512 goto error_return;
5513 if (secdata->sec_info)
dbaa2011 5514 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5515 }
5516 }
5517 }
5518
66eb6687 5519 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5520 {
5521 /* Add this bfd to the loaded list. */
5522 struct elf_link_loaded_list *n;
5523
ca4be51c 5524 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5525 if (n == NULL)
5526 goto error_return;
5527 n->abfd = abfd;
66eb6687
AM
5528 n->next = htab->loaded;
5529 htab->loaded = n;
4ad4eba5
AM
5530 }
5531
5532 return TRUE;
5533
5534 error_free_vers:
66eb6687
AM
5535 if (old_tab != NULL)
5536 free (old_tab);
5b677558
AM
5537 if (old_strtab != NULL)
5538 free (old_strtab);
4ad4eba5
AM
5539 if (nondeflt_vers != NULL)
5540 free (nondeflt_vers);
5541 if (extversym != NULL)
5542 free (extversym);
5543 error_free_sym:
5544 if (isymbuf != NULL)
5545 free (isymbuf);
5546 error_return:
5547 return FALSE;
5548}
5549
8387904d
AM
5550/* Return the linker hash table entry of a symbol that might be
5551 satisfied by an archive symbol. Return -1 on error. */
5552
5553struct elf_link_hash_entry *
5554_bfd_elf_archive_symbol_lookup (bfd *abfd,
5555 struct bfd_link_info *info,
5556 const char *name)
5557{
5558 struct elf_link_hash_entry *h;
5559 char *p, *copy;
5560 size_t len, first;
5561
2a41f396 5562 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5563 if (h != NULL)
5564 return h;
5565
5566 /* If this is a default version (the name contains @@), look up the
5567 symbol again with only one `@' as well as without the version.
5568 The effect is that references to the symbol with and without the
5569 version will be matched by the default symbol in the archive. */
5570
5571 p = strchr (name, ELF_VER_CHR);
5572 if (p == NULL || p[1] != ELF_VER_CHR)
5573 return h;
5574
5575 /* First check with only one `@'. */
5576 len = strlen (name);
a50b1753 5577 copy = (char *) bfd_alloc (abfd, len);
8387904d 5578 if (copy == NULL)
e99955cd 5579 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5580
5581 first = p - name + 1;
5582 memcpy (copy, name, first);
5583 memcpy (copy + first, name + first + 1, len - first);
5584
2a41f396 5585 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5586 if (h == NULL)
5587 {
5588 /* We also need to check references to the symbol without the
5589 version. */
5590 copy[first - 1] = '\0';
5591 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5592 FALSE, FALSE, TRUE);
8387904d
AM
5593 }
5594
5595 bfd_release (abfd, copy);
5596 return h;
5597}
5598
0ad989f9 5599/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5600 don't use _bfd_generic_link_add_archive_symbols because we need to
5601 handle versioned symbols.
0ad989f9
L
5602
5603 Fortunately, ELF archive handling is simpler than that done by
5604 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5605 oddities. In ELF, if we find a symbol in the archive map, and the
5606 symbol is currently undefined, we know that we must pull in that
5607 object file.
5608
5609 Unfortunately, we do have to make multiple passes over the symbol
5610 table until nothing further is resolved. */
5611
4ad4eba5
AM
5612static bfd_boolean
5613elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5614{
5615 symindex c;
13e570f8 5616 unsigned char *included = NULL;
0ad989f9
L
5617 carsym *symdefs;
5618 bfd_boolean loop;
5619 bfd_size_type amt;
8387904d
AM
5620 const struct elf_backend_data *bed;
5621 struct elf_link_hash_entry * (*archive_symbol_lookup)
5622 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5623
5624 if (! bfd_has_map (abfd))
5625 {
5626 /* An empty archive is a special case. */
5627 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5628 return TRUE;
5629 bfd_set_error (bfd_error_no_armap);
5630 return FALSE;
5631 }
5632
5633 /* Keep track of all symbols we know to be already defined, and all
5634 files we know to be already included. This is to speed up the
5635 second and subsequent passes. */
5636 c = bfd_ardata (abfd)->symdef_count;
5637 if (c == 0)
5638 return TRUE;
5639 amt = c;
13e570f8
AM
5640 amt *= sizeof (*included);
5641 included = (unsigned char *) bfd_zmalloc (amt);
5642 if (included == NULL)
5643 return FALSE;
0ad989f9
L
5644
5645 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5646 bed = get_elf_backend_data (abfd);
5647 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5648
5649 do
5650 {
5651 file_ptr last;
5652 symindex i;
5653 carsym *symdef;
5654 carsym *symdefend;
5655
5656 loop = FALSE;
5657 last = -1;
5658
5659 symdef = symdefs;
5660 symdefend = symdef + c;
5661 for (i = 0; symdef < symdefend; symdef++, i++)
5662 {
5663 struct elf_link_hash_entry *h;
5664 bfd *element;
5665 struct bfd_link_hash_entry *undefs_tail;
5666 symindex mark;
5667
13e570f8 5668 if (included[i])
0ad989f9
L
5669 continue;
5670 if (symdef->file_offset == last)
5671 {
5672 included[i] = TRUE;
5673 continue;
5674 }
5675
8387904d 5676 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5677 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5678 goto error_return;
0ad989f9
L
5679
5680 if (h == NULL)
5681 continue;
5682
5683 if (h->root.type == bfd_link_hash_common)
5684 {
5685 /* We currently have a common symbol. The archive map contains
5686 a reference to this symbol, so we may want to include it. We
5687 only want to include it however, if this archive element
5688 contains a definition of the symbol, not just another common
5689 declaration of it.
5690
5691 Unfortunately some archivers (including GNU ar) will put
5692 declarations of common symbols into their archive maps, as
5693 well as real definitions, so we cannot just go by the archive
5694 map alone. Instead we must read in the element's symbol
5695 table and check that to see what kind of symbol definition
5696 this is. */
5697 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5698 continue;
5699 }
5700 else if (h->root.type != bfd_link_hash_undefined)
5701 {
5702 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5703 /* Symbol must be defined. Don't check it again. */
5704 included[i] = TRUE;
0ad989f9
L
5705 continue;
5706 }
5707
5708 /* We need to include this archive member. */
5709 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5710 if (element == NULL)
5711 goto error_return;
5712
5713 if (! bfd_check_format (element, bfd_object))
5714 goto error_return;
5715
0ad989f9
L
5716 undefs_tail = info->hash->undefs_tail;
5717
0e144ba7
AM
5718 if (!(*info->callbacks
5719 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5720 continue;
0e144ba7 5721 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5722 goto error_return;
5723
5724 /* If there are any new undefined symbols, we need to make
5725 another pass through the archive in order to see whether
5726 they can be defined. FIXME: This isn't perfect, because
5727 common symbols wind up on undefs_tail and because an
5728 undefined symbol which is defined later on in this pass
5729 does not require another pass. This isn't a bug, but it
5730 does make the code less efficient than it could be. */
5731 if (undefs_tail != info->hash->undefs_tail)
5732 loop = TRUE;
5733
5734 /* Look backward to mark all symbols from this object file
5735 which we have already seen in this pass. */
5736 mark = i;
5737 do
5738 {
5739 included[mark] = TRUE;
5740 if (mark == 0)
5741 break;
5742 --mark;
5743 }
5744 while (symdefs[mark].file_offset == symdef->file_offset);
5745
5746 /* We mark subsequent symbols from this object file as we go
5747 on through the loop. */
5748 last = symdef->file_offset;
5749 }
5750 }
5751 while (loop);
5752
0ad989f9
L
5753 free (included);
5754
5755 return TRUE;
5756
5757 error_return:
0ad989f9
L
5758 if (included != NULL)
5759 free (included);
5760 return FALSE;
5761}
4ad4eba5
AM
5762
5763/* Given an ELF BFD, add symbols to the global hash table as
5764 appropriate. */
5765
5766bfd_boolean
5767bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5768{
5769 switch (bfd_get_format (abfd))
5770 {
5771 case bfd_object:
5772 return elf_link_add_object_symbols (abfd, info);
5773 case bfd_archive:
5774 return elf_link_add_archive_symbols (abfd, info);
5775 default:
5776 bfd_set_error (bfd_error_wrong_format);
5777 return FALSE;
5778 }
5779}
5a580b3a 5780\f
14b1c01e
AM
5781struct hash_codes_info
5782{
5783 unsigned long *hashcodes;
5784 bfd_boolean error;
5785};
a0c8462f 5786
5a580b3a
AM
5787/* This function will be called though elf_link_hash_traverse to store
5788 all hash value of the exported symbols in an array. */
5789
5790static bfd_boolean
5791elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5792{
a50b1753 5793 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5794 const char *name;
5a580b3a
AM
5795 unsigned long ha;
5796 char *alc = NULL;
5797
5a580b3a
AM
5798 /* Ignore indirect symbols. These are added by the versioning code. */
5799 if (h->dynindx == -1)
5800 return TRUE;
5801
5802 name = h->root.root.string;
422f1182 5803 if (h->versioned >= versioned)
5a580b3a 5804 {
422f1182
L
5805 char *p = strchr (name, ELF_VER_CHR);
5806 if (p != NULL)
14b1c01e 5807 {
422f1182
L
5808 alc = (char *) bfd_malloc (p - name + 1);
5809 if (alc == NULL)
5810 {
5811 inf->error = TRUE;
5812 return FALSE;
5813 }
5814 memcpy (alc, name, p - name);
5815 alc[p - name] = '\0';
5816 name = alc;
14b1c01e 5817 }
5a580b3a
AM
5818 }
5819
5820 /* Compute the hash value. */
5821 ha = bfd_elf_hash (name);
5822
5823 /* Store the found hash value in the array given as the argument. */
14b1c01e 5824 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5825
5826 /* And store it in the struct so that we can put it in the hash table
5827 later. */
f6e332e6 5828 h->u.elf_hash_value = ha;
5a580b3a
AM
5829
5830 if (alc != NULL)
5831 free (alc);
5832
5833 return TRUE;
5834}
5835
fdc90cb4
JJ
5836struct collect_gnu_hash_codes
5837{
5838 bfd *output_bfd;
5839 const struct elf_backend_data *bed;
5840 unsigned long int nsyms;
5841 unsigned long int maskbits;
5842 unsigned long int *hashcodes;
5843 unsigned long int *hashval;
5844 unsigned long int *indx;
5845 unsigned long int *counts;
5846 bfd_vma *bitmask;
5847 bfd_byte *contents;
5848 long int min_dynindx;
5849 unsigned long int bucketcount;
5850 unsigned long int symindx;
5851 long int local_indx;
5852 long int shift1, shift2;
5853 unsigned long int mask;
14b1c01e 5854 bfd_boolean error;
fdc90cb4
JJ
5855};
5856
5857/* This function will be called though elf_link_hash_traverse to store
5858 all hash value of the exported symbols in an array. */
5859
5860static bfd_boolean
5861elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5862{
a50b1753 5863 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5864 const char *name;
fdc90cb4
JJ
5865 unsigned long ha;
5866 char *alc = NULL;
5867
fdc90cb4
JJ
5868 /* Ignore indirect symbols. These are added by the versioning code. */
5869 if (h->dynindx == -1)
5870 return TRUE;
5871
5872 /* Ignore also local symbols and undefined symbols. */
5873 if (! (*s->bed->elf_hash_symbol) (h))
5874 return TRUE;
5875
5876 name = h->root.root.string;
422f1182 5877 if (h->versioned >= versioned)
fdc90cb4 5878 {
422f1182
L
5879 char *p = strchr (name, ELF_VER_CHR);
5880 if (p != NULL)
14b1c01e 5881 {
422f1182
L
5882 alc = (char *) bfd_malloc (p - name + 1);
5883 if (alc == NULL)
5884 {
5885 s->error = TRUE;
5886 return FALSE;
5887 }
5888 memcpy (alc, name, p - name);
5889 alc[p - name] = '\0';
5890 name = alc;
14b1c01e 5891 }
fdc90cb4
JJ
5892 }
5893
5894 /* Compute the hash value. */
5895 ha = bfd_elf_gnu_hash (name);
5896
5897 /* Store the found hash value in the array for compute_bucket_count,
5898 and also for .dynsym reordering purposes. */
5899 s->hashcodes[s->nsyms] = ha;
5900 s->hashval[h->dynindx] = ha;
5901 ++s->nsyms;
5902 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5903 s->min_dynindx = h->dynindx;
5904
5905 if (alc != NULL)
5906 free (alc);
5907
5908 return TRUE;
5909}
5910
5911/* This function will be called though elf_link_hash_traverse to do
5912 final dynaminc symbol renumbering. */
5913
5914static bfd_boolean
5915elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5916{
a50b1753 5917 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5918 unsigned long int bucket;
5919 unsigned long int val;
5920
fdc90cb4
JJ
5921 /* Ignore indirect symbols. */
5922 if (h->dynindx == -1)
5923 return TRUE;
5924
5925 /* Ignore also local symbols and undefined symbols. */
5926 if (! (*s->bed->elf_hash_symbol) (h))
5927 {
5928 if (h->dynindx >= s->min_dynindx)
5929 h->dynindx = s->local_indx++;
5930 return TRUE;
5931 }
5932
5933 bucket = s->hashval[h->dynindx] % s->bucketcount;
5934 val = (s->hashval[h->dynindx] >> s->shift1)
5935 & ((s->maskbits >> s->shift1) - 1);
5936 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5937 s->bitmask[val]
5938 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5939 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5940 if (s->counts[bucket] == 1)
5941 /* Last element terminates the chain. */
5942 val |= 1;
5943 bfd_put_32 (s->output_bfd, val,
5944 s->contents + (s->indx[bucket] - s->symindx) * 4);
5945 --s->counts[bucket];
5946 h->dynindx = s->indx[bucket]++;
5947 return TRUE;
5948}
5949
5950/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5951
5952bfd_boolean
5953_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5954{
5955 return !(h->forced_local
5956 || h->root.type == bfd_link_hash_undefined
5957 || h->root.type == bfd_link_hash_undefweak
5958 || ((h->root.type == bfd_link_hash_defined
5959 || h->root.type == bfd_link_hash_defweak)
5960 && h->root.u.def.section->output_section == NULL));
5961}
5962
5a580b3a
AM
5963/* Array used to determine the number of hash table buckets to use
5964 based on the number of symbols there are. If there are fewer than
5965 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5966 fewer than 37 we use 17 buckets, and so forth. We never use more
5967 than 32771 buckets. */
5968
5969static const size_t elf_buckets[] =
5970{
5971 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5972 16411, 32771, 0
5973};
5974
5975/* Compute bucket count for hashing table. We do not use a static set
5976 of possible tables sizes anymore. Instead we determine for all
5977 possible reasonable sizes of the table the outcome (i.e., the
5978 number of collisions etc) and choose the best solution. The
5979 weighting functions are not too simple to allow the table to grow
5980 without bounds. Instead one of the weighting factors is the size.
5981 Therefore the result is always a good payoff between few collisions
5982 (= short chain lengths) and table size. */
5983static size_t
b20dd2ce 5984compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5985 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5986 unsigned long int nsyms,
5987 int gnu_hash)
5a580b3a 5988{
5a580b3a 5989 size_t best_size = 0;
5a580b3a 5990 unsigned long int i;
5a580b3a 5991
5a580b3a
AM
5992 /* We have a problem here. The following code to optimize the table
5993 size requires an integer type with more the 32 bits. If
5994 BFD_HOST_U_64_BIT is set we know about such a type. */
5995#ifdef BFD_HOST_U_64_BIT
5996 if (info->optimize)
5997 {
5a580b3a
AM
5998 size_t minsize;
5999 size_t maxsize;
6000 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6001 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6002 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6003 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6004 unsigned long int *counts;
d40f3da9 6005 bfd_size_type amt;
0883b6e0 6006 unsigned int no_improvement_count = 0;
5a580b3a
AM
6007
6008 /* Possible optimization parameters: if we have NSYMS symbols we say
6009 that the hashing table must at least have NSYMS/4 and at most
6010 2*NSYMS buckets. */
6011 minsize = nsyms / 4;
6012 if (minsize == 0)
6013 minsize = 1;
6014 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6015 if (gnu_hash)
6016 {
6017 if (minsize < 2)
6018 minsize = 2;
6019 if ((best_size & 31) == 0)
6020 ++best_size;
6021 }
5a580b3a
AM
6022
6023 /* Create array where we count the collisions in. We must use bfd_malloc
6024 since the size could be large. */
6025 amt = maxsize;
6026 amt *= sizeof (unsigned long int);
a50b1753 6027 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6028 if (counts == NULL)
fdc90cb4 6029 return 0;
5a580b3a
AM
6030
6031 /* Compute the "optimal" size for the hash table. The criteria is a
6032 minimal chain length. The minor criteria is (of course) the size
6033 of the table. */
6034 for (i = minsize; i < maxsize; ++i)
6035 {
6036 /* Walk through the array of hashcodes and count the collisions. */
6037 BFD_HOST_U_64_BIT max;
6038 unsigned long int j;
6039 unsigned long int fact;
6040
fdc90cb4
JJ
6041 if (gnu_hash && (i & 31) == 0)
6042 continue;
6043
5a580b3a
AM
6044 memset (counts, '\0', i * sizeof (unsigned long int));
6045
6046 /* Determine how often each hash bucket is used. */
6047 for (j = 0; j < nsyms; ++j)
6048 ++counts[hashcodes[j] % i];
6049
6050 /* For the weight function we need some information about the
6051 pagesize on the target. This is information need not be 100%
6052 accurate. Since this information is not available (so far) we
6053 define it here to a reasonable default value. If it is crucial
6054 to have a better value some day simply define this value. */
6055# ifndef BFD_TARGET_PAGESIZE
6056# define BFD_TARGET_PAGESIZE (4096)
6057# endif
6058
fdc90cb4
JJ
6059 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6060 and the chains. */
6061 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6062
6063# if 1
6064 /* Variant 1: optimize for short chains. We add the squares
6065 of all the chain lengths (which favors many small chain
6066 over a few long chains). */
6067 for (j = 0; j < i; ++j)
6068 max += counts[j] * counts[j];
6069
6070 /* This adds penalties for the overall size of the table. */
fdc90cb4 6071 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6072 max *= fact * fact;
6073# else
6074 /* Variant 2: Optimize a lot more for small table. Here we
6075 also add squares of the size but we also add penalties for
6076 empty slots (the +1 term). */
6077 for (j = 0; j < i; ++j)
6078 max += (1 + counts[j]) * (1 + counts[j]);
6079
6080 /* The overall size of the table is considered, but not as
6081 strong as in variant 1, where it is squared. */
fdc90cb4 6082 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6083 max *= fact;
6084# endif
6085
6086 /* Compare with current best results. */
6087 if (max < best_chlen)
6088 {
6089 best_chlen = max;
6090 best_size = i;
ca4be51c 6091 no_improvement_count = 0;
5a580b3a 6092 }
0883b6e0
NC
6093 /* PR 11843: Avoid futile long searches for the best bucket size
6094 when there are a large number of symbols. */
6095 else if (++no_improvement_count == 100)
6096 break;
5a580b3a
AM
6097 }
6098
6099 free (counts);
6100 }
6101 else
6102#endif /* defined (BFD_HOST_U_64_BIT) */
6103 {
6104 /* This is the fallback solution if no 64bit type is available or if we
6105 are not supposed to spend much time on optimizations. We select the
6106 bucket count using a fixed set of numbers. */
6107 for (i = 0; elf_buckets[i] != 0; i++)
6108 {
6109 best_size = elf_buckets[i];
fdc90cb4 6110 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6111 break;
6112 }
fdc90cb4
JJ
6113 if (gnu_hash && best_size < 2)
6114 best_size = 2;
5a580b3a
AM
6115 }
6116
5a580b3a
AM
6117 return best_size;
6118}
6119
d0bf826b
AM
6120/* Size any SHT_GROUP section for ld -r. */
6121
6122bfd_boolean
6123_bfd_elf_size_group_sections (struct bfd_link_info *info)
6124{
6125 bfd *ibfd;
57963c05 6126 asection *s;
d0bf826b 6127
c72f2fb2 6128 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6129 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6130 && (s = ibfd->sections) != NULL
6131 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6132 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6133 return FALSE;
6134 return TRUE;
6135}
6136
04c3a755
NS
6137/* Set a default stack segment size. The value in INFO wins. If it
6138 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6139 undefined it is initialized. */
6140
6141bfd_boolean
6142bfd_elf_stack_segment_size (bfd *output_bfd,
6143 struct bfd_link_info *info,
6144 const char *legacy_symbol,
6145 bfd_vma default_size)
6146{
6147 struct elf_link_hash_entry *h = NULL;
6148
6149 /* Look for legacy symbol. */
6150 if (legacy_symbol)
6151 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6152 FALSE, FALSE, FALSE);
6153 if (h && (h->root.type == bfd_link_hash_defined
6154 || h->root.type == bfd_link_hash_defweak)
6155 && h->def_regular
6156 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6157 {
6158 /* The symbol has no type if specified on the command line. */
6159 h->type = STT_OBJECT;
6160 if (info->stacksize)
695344c0 6161 /* xgettext:c-format */
871b3ab2 6162 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6163 output_bfd, legacy_symbol);
04c3a755 6164 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6165 /* xgettext:c-format */
871b3ab2 6166 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6167 output_bfd, legacy_symbol);
04c3a755
NS
6168 else
6169 info->stacksize = h->root.u.def.value;
6170 }
6171
6172 if (!info->stacksize)
6173 /* If the user didn't set a size, or explicitly inhibit the
6174 size, set it now. */
6175 info->stacksize = default_size;
6176
6177 /* Provide the legacy symbol, if it is referenced. */
6178 if (h && (h->root.type == bfd_link_hash_undefined
6179 || h->root.type == bfd_link_hash_undefweak))
6180 {
6181 struct bfd_link_hash_entry *bh = NULL;
6182
6183 if (!(_bfd_generic_link_add_one_symbol
6184 (info, output_bfd, legacy_symbol,
6185 BSF_GLOBAL, bfd_abs_section_ptr,
6186 info->stacksize >= 0 ? info->stacksize : 0,
6187 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6188 return FALSE;
6189
6190 h = (struct elf_link_hash_entry *) bh;
6191 h->def_regular = 1;
6192 h->type = STT_OBJECT;
6193 }
6194
6195 return TRUE;
6196}
6197
b531344c
MR
6198/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6199
6200struct elf_gc_sweep_symbol_info
6201{
6202 struct bfd_link_info *info;
6203 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6204 bfd_boolean);
6205};
6206
6207static bfd_boolean
6208elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6209{
6210 if (!h->mark
6211 && (((h->root.type == bfd_link_hash_defined
6212 || h->root.type == bfd_link_hash_defweak)
6213 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6214 && h->root.u.def.section->gc_mark))
6215 || h->root.type == bfd_link_hash_undefined
6216 || h->root.type == bfd_link_hash_undefweak))
6217 {
6218 struct elf_gc_sweep_symbol_info *inf;
6219
6220 inf = (struct elf_gc_sweep_symbol_info *) data;
6221 (*inf->hide_symbol) (inf->info, h, TRUE);
6222 h->def_regular = 0;
6223 h->ref_regular = 0;
6224 h->ref_regular_nonweak = 0;
6225 }
6226
6227 return TRUE;
6228}
6229
5a580b3a
AM
6230/* Set up the sizes and contents of the ELF dynamic sections. This is
6231 called by the ELF linker emulation before_allocation routine. We
6232 must set the sizes of the sections before the linker sets the
6233 addresses of the various sections. */
6234
6235bfd_boolean
6236bfd_elf_size_dynamic_sections (bfd *output_bfd,
6237 const char *soname,
6238 const char *rpath,
6239 const char *filter_shlib,
7ee314fa
AM
6240 const char *audit,
6241 const char *depaudit,
5a580b3a
AM
6242 const char * const *auxiliary_filters,
6243 struct bfd_link_info *info,
fd91d419 6244 asection **sinterpptr)
5a580b3a 6245{
5a580b3a
AM
6246 bfd *dynobj;
6247 const struct elf_backend_data *bed;
5a580b3a
AM
6248
6249 *sinterpptr = NULL;
6250
5a580b3a
AM
6251 if (!is_elf_hash_table (info->hash))
6252 return TRUE;
6253
5a580b3a
AM
6254 dynobj = elf_hash_table (info)->dynobj;
6255
9a2a56cc 6256 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6257 {
902e9fc7
MR
6258 struct bfd_elf_version_tree *verdefs;
6259 struct elf_info_failed asvinfo;
5a580b3a
AM
6260 struct bfd_elf_version_tree *t;
6261 struct bfd_elf_version_expr *d;
902e9fc7 6262 asection *s;
e6699019 6263 size_t soname_indx;
7ee314fa 6264
5a580b3a
AM
6265 /* If we are supposed to export all symbols into the dynamic symbol
6266 table (this is not the normal case), then do so. */
55255dae 6267 if (info->export_dynamic
0e1862bb 6268 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6269 {
3d13f3e9
AM
6270 struct elf_info_failed eif;
6271
6272 eif.info = info;
6273 eif.failed = FALSE;
5a580b3a
AM
6274 elf_link_hash_traverse (elf_hash_table (info),
6275 _bfd_elf_export_symbol,
6276 &eif);
6277 if (eif.failed)
6278 return FALSE;
6279 }
6280
e6699019
L
6281 if (soname != NULL)
6282 {
6283 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6284 soname, TRUE);
6285 if (soname_indx == (size_t) -1
6286 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6287 return FALSE;
6288 }
6289 else
6290 soname_indx = (size_t) -1;
6291
5a580b3a 6292 /* Make all global versions with definition. */
fd91d419 6293 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6294 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6295 if (!d->symver && d->literal)
5a580b3a
AM
6296 {
6297 const char *verstr, *name;
6298 size_t namelen, verlen, newlen;
93252b1c 6299 char *newname, *p, leading_char;
5a580b3a
AM
6300 struct elf_link_hash_entry *newh;
6301
93252b1c 6302 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6303 name = d->pattern;
93252b1c 6304 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6305 verstr = t->name;
6306 verlen = strlen (verstr);
6307 newlen = namelen + verlen + 3;
6308
a50b1753 6309 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6310 if (newname == NULL)
6311 return FALSE;
93252b1c
MF
6312 newname[0] = leading_char;
6313 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6314
6315 /* Check the hidden versioned definition. */
6316 p = newname + namelen;
6317 *p++ = ELF_VER_CHR;
6318 memcpy (p, verstr, verlen + 1);
6319 newh = elf_link_hash_lookup (elf_hash_table (info),
6320 newname, FALSE, FALSE,
6321 FALSE);
6322 if (newh == NULL
6323 || (newh->root.type != bfd_link_hash_defined
6324 && newh->root.type != bfd_link_hash_defweak))
6325 {
6326 /* Check the default versioned definition. */
6327 *p++ = ELF_VER_CHR;
6328 memcpy (p, verstr, verlen + 1);
6329 newh = elf_link_hash_lookup (elf_hash_table (info),
6330 newname, FALSE, FALSE,
6331 FALSE);
6332 }
6333 free (newname);
6334
6335 /* Mark this version if there is a definition and it is
6336 not defined in a shared object. */
6337 if (newh != NULL
f5385ebf 6338 && !newh->def_dynamic
5a580b3a
AM
6339 && (newh->root.type == bfd_link_hash_defined
6340 || newh->root.type == bfd_link_hash_defweak))
6341 d->symver = 1;
6342 }
6343
6344 /* Attach all the symbols to their version information. */
5a580b3a 6345 asvinfo.info = info;
5a580b3a
AM
6346 asvinfo.failed = FALSE;
6347
6348 elf_link_hash_traverse (elf_hash_table (info),
6349 _bfd_elf_link_assign_sym_version,
6350 &asvinfo);
6351 if (asvinfo.failed)
6352 return FALSE;
6353
6354 if (!info->allow_undefined_version)
6355 {
6356 /* Check if all global versions have a definition. */
3d13f3e9 6357 bfd_boolean all_defined = TRUE;
fd91d419 6358 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6359 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6360 if (d->literal && !d->symver && !d->script)
5a580b3a 6361 {
4eca0228 6362 _bfd_error_handler
5a580b3a
AM
6363 (_("%s: undefined version: %s"),
6364 d->pattern, t->name);
6365 all_defined = FALSE;
6366 }
6367
6368 if (!all_defined)
6369 {
6370 bfd_set_error (bfd_error_bad_value);
6371 return FALSE;
6372 }
6373 }
6374
902e9fc7
MR
6375 /* Set up the version definition section. */
6376 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6377 BFD_ASSERT (s != NULL);
5a580b3a 6378
902e9fc7
MR
6379 /* We may have created additional version definitions if we are
6380 just linking a regular application. */
6381 verdefs = info->version_info;
5a580b3a 6382
902e9fc7
MR
6383 /* Skip anonymous version tag. */
6384 if (verdefs != NULL && verdefs->vernum == 0)
6385 verdefs = verdefs->next;
5a580b3a 6386
902e9fc7
MR
6387 if (verdefs == NULL && !info->create_default_symver)
6388 s->flags |= SEC_EXCLUDE;
6389 else
5a580b3a 6390 {
902e9fc7
MR
6391 unsigned int cdefs;
6392 bfd_size_type size;
6393 bfd_byte *p;
6394 Elf_Internal_Verdef def;
6395 Elf_Internal_Verdaux defaux;
6396 struct bfd_link_hash_entry *bh;
6397 struct elf_link_hash_entry *h;
6398 const char *name;
5a580b3a 6399
902e9fc7
MR
6400 cdefs = 0;
6401 size = 0;
5a580b3a 6402
902e9fc7
MR
6403 /* Make space for the base version. */
6404 size += sizeof (Elf_External_Verdef);
6405 size += sizeof (Elf_External_Verdaux);
6406 ++cdefs;
6407
6408 /* Make space for the default version. */
6409 if (info->create_default_symver)
6410 {
6411 size += sizeof (Elf_External_Verdef);
6412 ++cdefs;
3e3b46e5
PB
6413 }
6414
5a580b3a
AM
6415 for (t = verdefs; t != NULL; t = t->next)
6416 {
6417 struct bfd_elf_version_deps *n;
6418
a6cc6b3b
RO
6419 /* Don't emit base version twice. */
6420 if (t->vernum == 0)
6421 continue;
6422
5a580b3a
AM
6423 size += sizeof (Elf_External_Verdef);
6424 size += sizeof (Elf_External_Verdaux);
6425 ++cdefs;
6426
6427 for (n = t->deps; n != NULL; n = n->next)
6428 size += sizeof (Elf_External_Verdaux);
6429 }
6430
eea6121a 6431 s->size = size;
a50b1753 6432 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6433 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6434 return FALSE;
6435
6436 /* Fill in the version definition section. */
6437
6438 p = s->contents;
6439
6440 def.vd_version = VER_DEF_CURRENT;
6441 def.vd_flags = VER_FLG_BASE;
6442 def.vd_ndx = 1;
6443 def.vd_cnt = 1;
3e3b46e5
PB
6444 if (info->create_default_symver)
6445 {
6446 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6447 def.vd_next = sizeof (Elf_External_Verdef);
6448 }
6449 else
6450 {
6451 def.vd_aux = sizeof (Elf_External_Verdef);
6452 def.vd_next = (sizeof (Elf_External_Verdef)
6453 + sizeof (Elf_External_Verdaux));
6454 }
5a580b3a 6455
ef53be89 6456 if (soname_indx != (size_t) -1)
5a580b3a
AM
6457 {
6458 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6459 soname_indx);
6460 def.vd_hash = bfd_elf_hash (soname);
6461 defaux.vda_name = soname_indx;
3e3b46e5 6462 name = soname;
5a580b3a
AM
6463 }
6464 else
6465 {
ef53be89 6466 size_t indx;
5a580b3a 6467
06084812 6468 name = lbasename (output_bfd->filename);
5a580b3a
AM
6469 def.vd_hash = bfd_elf_hash (name);
6470 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6471 name, FALSE);
ef53be89 6472 if (indx == (size_t) -1)
5a580b3a
AM
6473 return FALSE;
6474 defaux.vda_name = indx;
6475 }
6476 defaux.vda_next = 0;
6477
6478 _bfd_elf_swap_verdef_out (output_bfd, &def,
6479 (Elf_External_Verdef *) p);
6480 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6481 if (info->create_default_symver)
6482 {
6483 /* Add a symbol representing this version. */
6484 bh = NULL;
6485 if (! (_bfd_generic_link_add_one_symbol
6486 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6487 0, NULL, FALSE,
6488 get_elf_backend_data (dynobj)->collect, &bh)))
6489 return FALSE;
6490 h = (struct elf_link_hash_entry *) bh;
6491 h->non_elf = 0;
6492 h->def_regular = 1;
6493 h->type = STT_OBJECT;
6494 h->verinfo.vertree = NULL;
6495
6496 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6497 return FALSE;
6498
6499 /* Create a duplicate of the base version with the same
6500 aux block, but different flags. */
6501 def.vd_flags = 0;
6502 def.vd_ndx = 2;
6503 def.vd_aux = sizeof (Elf_External_Verdef);
6504 if (verdefs)
6505 def.vd_next = (sizeof (Elf_External_Verdef)
6506 + sizeof (Elf_External_Verdaux));
6507 else
6508 def.vd_next = 0;
6509 _bfd_elf_swap_verdef_out (output_bfd, &def,
6510 (Elf_External_Verdef *) p);
6511 p += sizeof (Elf_External_Verdef);
6512 }
5a580b3a
AM
6513 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6514 (Elf_External_Verdaux *) p);
6515 p += sizeof (Elf_External_Verdaux);
6516
6517 for (t = verdefs; t != NULL; t = t->next)
6518 {
6519 unsigned int cdeps;
6520 struct bfd_elf_version_deps *n;
5a580b3a 6521
a6cc6b3b
RO
6522 /* Don't emit the base version twice. */
6523 if (t->vernum == 0)
6524 continue;
6525
5a580b3a
AM
6526 cdeps = 0;
6527 for (n = t->deps; n != NULL; n = n->next)
6528 ++cdeps;
6529
6530 /* Add a symbol representing this version. */
6531 bh = NULL;
6532 if (! (_bfd_generic_link_add_one_symbol
6533 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6534 0, NULL, FALSE,
6535 get_elf_backend_data (dynobj)->collect, &bh)))
6536 return FALSE;
6537 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6538 h->non_elf = 0;
6539 h->def_regular = 1;
5a580b3a
AM
6540 h->type = STT_OBJECT;
6541 h->verinfo.vertree = t;
6542
c152c796 6543 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6544 return FALSE;
6545
6546 def.vd_version = VER_DEF_CURRENT;
6547 def.vd_flags = 0;
6548 if (t->globals.list == NULL
6549 && t->locals.list == NULL
6550 && ! t->used)
6551 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6552 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6553 def.vd_cnt = cdeps + 1;
6554 def.vd_hash = bfd_elf_hash (t->name);
6555 def.vd_aux = sizeof (Elf_External_Verdef);
6556 def.vd_next = 0;
a6cc6b3b
RO
6557
6558 /* If a basever node is next, it *must* be the last node in
6559 the chain, otherwise Verdef construction breaks. */
6560 if (t->next != NULL && t->next->vernum == 0)
6561 BFD_ASSERT (t->next->next == NULL);
6562
6563 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6564 def.vd_next = (sizeof (Elf_External_Verdef)
6565 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6566
6567 _bfd_elf_swap_verdef_out (output_bfd, &def,
6568 (Elf_External_Verdef *) p);
6569 p += sizeof (Elf_External_Verdef);
6570
6571 defaux.vda_name = h->dynstr_index;
6572 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6573 h->dynstr_index);
6574 defaux.vda_next = 0;
6575 if (t->deps != NULL)
6576 defaux.vda_next = sizeof (Elf_External_Verdaux);
6577 t->name_indx = defaux.vda_name;
6578
6579 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6580 (Elf_External_Verdaux *) p);
6581 p += sizeof (Elf_External_Verdaux);
6582
6583 for (n = t->deps; n != NULL; n = n->next)
6584 {
6585 if (n->version_needed == NULL)
6586 {
6587 /* This can happen if there was an error in the
6588 version script. */
6589 defaux.vda_name = 0;
6590 }
6591 else
6592 {
6593 defaux.vda_name = n->version_needed->name_indx;
6594 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6595 defaux.vda_name);
6596 }
6597 if (n->next == NULL)
6598 defaux.vda_next = 0;
6599 else
6600 defaux.vda_next = sizeof (Elf_External_Verdaux);
6601
6602 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6603 (Elf_External_Verdaux *) p);
6604 p += sizeof (Elf_External_Verdaux);
6605 }
6606 }
6607
5a580b3a
AM
6608 elf_tdata (output_bfd)->cverdefs = cdefs;
6609 }
902e9fc7
MR
6610 }
6611
6612 bed = get_elf_backend_data (output_bfd);
6613
6614 if (info->gc_sections && bed->can_gc_sections)
6615 {
6616 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6617
6618 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6619 dynamic symbol table. */
902e9fc7
MR
6620 sweep_info.info = info;
6621 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6622 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6623 &sweep_info);
3d13f3e9
AM
6624 }
6625
6626 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6627 {
6628 asection *s;
6629 struct elf_find_verdep_info sinfo;
6630
6631 /* Work out the size of the version reference section. */
6632
6633 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6634 BFD_ASSERT (s != NULL);
902e9fc7 6635
3d13f3e9
AM
6636 sinfo.info = info;
6637 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6638 if (sinfo.vers == 0)
6639 sinfo.vers = 1;
6640 sinfo.failed = FALSE;
6641
6642 elf_link_hash_traverse (elf_hash_table (info),
6643 _bfd_elf_link_find_version_dependencies,
6644 &sinfo);
6645 if (sinfo.failed)
6646 return FALSE;
6647
6648 if (elf_tdata (output_bfd)->verref == NULL)
6649 s->flags |= SEC_EXCLUDE;
6650 else
6651 {
6652 Elf_Internal_Verneed *vn;
6653 unsigned int size;
6654 unsigned int crefs;
6655 bfd_byte *p;
6656
6657 /* Build the version dependency section. */
6658 size = 0;
6659 crefs = 0;
6660 for (vn = elf_tdata (output_bfd)->verref;
6661 vn != NULL;
6662 vn = vn->vn_nextref)
6663 {
6664 Elf_Internal_Vernaux *a;
6665
6666 size += sizeof (Elf_External_Verneed);
6667 ++crefs;
6668 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6669 size += sizeof (Elf_External_Vernaux);
6670 }
6671
6672 s->size = size;
6673 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6674 if (s->contents == NULL)
6675 return FALSE;
6676
6677 p = s->contents;
6678 for (vn = elf_tdata (output_bfd)->verref;
6679 vn != NULL;
6680 vn = vn->vn_nextref)
6681 {
6682 unsigned int caux;
6683 Elf_Internal_Vernaux *a;
6684 size_t indx;
6685
6686 caux = 0;
6687 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6688 ++caux;
6689
6690 vn->vn_version = VER_NEED_CURRENT;
6691 vn->vn_cnt = caux;
6692 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6693 elf_dt_name (vn->vn_bfd) != NULL
6694 ? elf_dt_name (vn->vn_bfd)
6695 : lbasename (vn->vn_bfd->filename),
6696 FALSE);
6697 if (indx == (size_t) -1)
6698 return FALSE;
6699 vn->vn_file = indx;
6700 vn->vn_aux = sizeof (Elf_External_Verneed);
6701 if (vn->vn_nextref == NULL)
6702 vn->vn_next = 0;
6703 else
6704 vn->vn_next = (sizeof (Elf_External_Verneed)
6705 + caux * sizeof (Elf_External_Vernaux));
6706
6707 _bfd_elf_swap_verneed_out (output_bfd, vn,
6708 (Elf_External_Verneed *) p);
6709 p += sizeof (Elf_External_Verneed);
6710
6711 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6712 {
6713 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6714 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6715 a->vna_nodename, FALSE);
6716 if (indx == (size_t) -1)
6717 return FALSE;
6718 a->vna_name = indx;
6719 if (a->vna_nextptr == NULL)
6720 a->vna_next = 0;
6721 else
6722 a->vna_next = sizeof (Elf_External_Vernaux);
6723
6724 _bfd_elf_swap_vernaux_out (output_bfd, a,
6725 (Elf_External_Vernaux *) p);
6726 p += sizeof (Elf_External_Vernaux);
6727 }
6728 }
6729
6730 elf_tdata (output_bfd)->cverrefs = crefs;
6731 }
902e9fc7
MR
6732 }
6733
6734 /* Any syms created from now on start with -1 in
6735 got.refcount/offset and plt.refcount/offset. */
6736 elf_hash_table (info)->init_got_refcount
6737 = elf_hash_table (info)->init_got_offset;
6738 elf_hash_table (info)->init_plt_refcount
6739 = elf_hash_table (info)->init_plt_offset;
6740
6741 if (bfd_link_relocatable (info)
6742 && !_bfd_elf_size_group_sections (info))
6743 return FALSE;
6744
6745 /* The backend may have to create some sections regardless of whether
6746 we're dynamic or not. */
6747 if (bed->elf_backend_always_size_sections
6748 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6749 return FALSE;
6750
6751 /* Determine any GNU_STACK segment requirements, after the backend
6752 has had a chance to set a default segment size. */
6753 if (info->execstack)
6754 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6755 else if (info->noexecstack)
6756 elf_stack_flags (output_bfd) = PF_R | PF_W;
6757 else
6758 {
6759 bfd *inputobj;
6760 asection *notesec = NULL;
6761 int exec = 0;
6762
6763 for (inputobj = info->input_bfds;
6764 inputobj;
6765 inputobj = inputobj->link.next)
6766 {
6767 asection *s;
6768
6769 if (inputobj->flags
6770 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6771 continue;
57963c05
AM
6772 s = inputobj->sections;
6773 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6774 continue;
6775
902e9fc7
MR
6776 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6777 if (s)
6778 {
6779 if (s->flags & SEC_CODE)
6780 exec = PF_X;
6781 notesec = s;
6782 }
6783 else if (bed->default_execstack)
6784 exec = PF_X;
6785 }
6786 if (notesec || info->stacksize > 0)
6787 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6788 if (notesec && exec && bfd_link_relocatable (info)
6789 && notesec->output_section != bfd_abs_section_ptr)
6790 notesec->output_section->flags |= SEC_CODE;
6791 }
6792
6793 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6794 {
6795 struct elf_info_failed eif;
6796 struct elf_link_hash_entry *h;
6797 asection *dynstr;
6798 asection *s;
6799
6800 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6801 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6802
902e9fc7
MR
6803 if (info->symbolic)
6804 {
6805 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6806 return FALSE;
6807 info->flags |= DF_SYMBOLIC;
6808 }
6809
6810 if (rpath != NULL)
6811 {
6812 size_t indx;
6813 bfd_vma tag;
6814
6815 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6816 TRUE);
6817 if (indx == (size_t) -1)
6818 return FALSE;
6819
6820 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6821 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6822 return FALSE;
6823 }
6824
6825 if (filter_shlib != NULL)
6826 {
6827 size_t indx;
6828
6829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6830 filter_shlib, TRUE);
6831 if (indx == (size_t) -1
6832 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6833 return FALSE;
6834 }
6835
6836 if (auxiliary_filters != NULL)
6837 {
6838 const char * const *p;
6839
6840 for (p = auxiliary_filters; *p != NULL; p++)
6841 {
6842 size_t indx;
6843
6844 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6845 *p, TRUE);
6846 if (indx == (size_t) -1
6847 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6848 return FALSE;
6849 }
6850 }
6851
6852 if (audit != NULL)
6853 {
6854 size_t indx;
6855
6856 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6857 TRUE);
6858 if (indx == (size_t) -1
6859 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6860 return FALSE;
6861 }
6862
6863 if (depaudit != NULL)
6864 {
6865 size_t indx;
6866
6867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6868 TRUE);
6869 if (indx == (size_t) -1
6870 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6871 return FALSE;
6872 }
6873
6874 eif.info = info;
6875 eif.failed = FALSE;
6876
6877 /* Find all symbols which were defined in a dynamic object and make
6878 the backend pick a reasonable value for them. */
6879 elf_link_hash_traverse (elf_hash_table (info),
6880 _bfd_elf_adjust_dynamic_symbol,
6881 &eif);
6882 if (eif.failed)
6883 return FALSE;
6884
6885 /* Add some entries to the .dynamic section. We fill in some of the
6886 values later, in bfd_elf_final_link, but we must add the entries
6887 now so that we know the final size of the .dynamic section. */
6888
6889 /* If there are initialization and/or finalization functions to
6890 call then add the corresponding DT_INIT/DT_FINI entries. */
6891 h = (info->init_function
6892 ? elf_link_hash_lookup (elf_hash_table (info),
6893 info->init_function, FALSE,
6894 FALSE, FALSE)
6895 : NULL);
6896 if (h != NULL
6897 && (h->ref_regular
6898 || h->def_regular))
6899 {
6900 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6901 return FALSE;
6902 }
6903 h = (info->fini_function
6904 ? elf_link_hash_lookup (elf_hash_table (info),
6905 info->fini_function, FALSE,
6906 FALSE, FALSE)
6907 : NULL);
6908 if (h != NULL
6909 && (h->ref_regular
6910 || h->def_regular))
6911 {
6912 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6913 return FALSE;
6914 }
6915
6916 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6917 if (s != NULL && s->linker_has_input)
6918 {
6919 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6920 if (! bfd_link_executable (info))
6921 {
6922 bfd *sub;
6923 asection *o;
6924
57963c05
AM
6925 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6926 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6927 && (o = sub->sections) != NULL
6928 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6929 for (o = sub->sections; o != NULL; o = o->next)
6930 if (elf_section_data (o)->this_hdr.sh_type
6931 == SHT_PREINIT_ARRAY)
6932 {
6933 _bfd_error_handler
871b3ab2 6934 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6935 sub);
6936 break;
6937 }
6938
6939 bfd_set_error (bfd_error_nonrepresentable_section);
6940 return FALSE;
6941 }
6942
6943 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6944 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6945 return FALSE;
6946 }
6947 s = bfd_get_section_by_name (output_bfd, ".init_array");
6948 if (s != NULL && s->linker_has_input)
6949 {
6950 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6951 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6952 return FALSE;
6953 }
6954 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6955 if (s != NULL && s->linker_has_input)
6956 {
6957 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6958 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6959 return FALSE;
6960 }
6961
6962 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6963 /* If .dynstr is excluded from the link, we don't want any of
6964 these tags. Strictly, we should be checking each section
6965 individually; This quick check covers for the case where
6966 someone does a /DISCARD/ : { *(*) }. */
6967 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6968 {
6969 bfd_size_type strsize;
6970
6971 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6972 if ((info->emit_hash
6973 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6974 || (info->emit_gnu_hash
6975 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6976 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6977 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6978 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6979 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6980 bed->s->sizeof_sym))
6981 return FALSE;
6982 }
6983 }
6984
6985 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6986 return FALSE;
6987
6988 /* The backend must work out the sizes of all the other dynamic
6989 sections. */
6990 if (dynobj != NULL
6991 && bed->elf_backend_size_dynamic_sections != NULL
6992 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6993 return FALSE;
6994
6995 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6996 {
902e9fc7
MR
6997 if (elf_tdata (output_bfd)->cverdefs)
6998 {
6999 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7000
7001 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7002 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7003 return FALSE;
7004 }
7005
7006 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7007 {
7008 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7009 return FALSE;
7010 }
7011 else if (info->flags & DF_BIND_NOW)
7012 {
7013 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7014 return FALSE;
7015 }
7016
7017 if (info->flags_1)
7018 {
7019 if (bfd_link_executable (info))
7020 info->flags_1 &= ~ (DF_1_INITFIRST
7021 | DF_1_NODELETE
7022 | DF_1_NOOPEN);
7023 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7024 return FALSE;
7025 }
7026
7027 if (elf_tdata (output_bfd)->cverrefs)
7028 {
7029 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7030
7031 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7032 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7033 return FALSE;
7034 }
5a580b3a 7035
8423293d
AM
7036 if ((elf_tdata (output_bfd)->cverrefs == 0
7037 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7038 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7039 {
902e9fc7
MR
7040 asection *s;
7041
3d4d4302 7042 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7043 s->flags |= SEC_EXCLUDE;
7044 }
7045 }
7046 return TRUE;
7047}
7048
74541ad4
AM
7049/* Find the first non-excluded output section. We'll use its
7050 section symbol for some emitted relocs. */
7051void
7052_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7053{
7054 asection *s;
7055
7056 for (s = output_bfd->sections; s != NULL; s = s->next)
7057 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7058 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7059 {
7060 elf_hash_table (info)->text_index_section = s;
7061 break;
7062 }
7063}
7064
7065/* Find two non-excluded output sections, one for code, one for data.
7066 We'll use their section symbols for some emitted relocs. */
7067void
7068_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7069{
7070 asection *s;
7071
266b05cf 7072 /* Data first, since setting text_index_section changes
7f923b7f 7073 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7074 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7075 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7076 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7077 {
266b05cf 7078 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7079 break;
7080 }
7081
7082 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7083 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7084 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7085 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7086 {
266b05cf 7087 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7088 break;
7089 }
7090
7091 if (elf_hash_table (info)->text_index_section == NULL)
7092 elf_hash_table (info)->text_index_section
7093 = elf_hash_table (info)->data_index_section;
7094}
7095
8423293d
AM
7096bfd_boolean
7097bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7098{
74541ad4 7099 const struct elf_backend_data *bed;
23ec1e32 7100 unsigned long section_sym_count;
96d01d93 7101 bfd_size_type dynsymcount = 0;
74541ad4 7102
8423293d
AM
7103 if (!is_elf_hash_table (info->hash))
7104 return TRUE;
7105
74541ad4
AM
7106 bed = get_elf_backend_data (output_bfd);
7107 (*bed->elf_backend_init_index_section) (output_bfd, info);
7108
23ec1e32
MR
7109 /* Assign dynsym indices. In a shared library we generate a section
7110 symbol for each output section, which come first. Next come all
7111 of the back-end allocated local dynamic syms, followed by the rest
7112 of the global symbols.
7113
7114 This is usually not needed for static binaries, however backends
7115 can request to always do it, e.g. the MIPS backend uses dynamic
7116 symbol counts to lay out GOT, which will be produced in the
7117 presence of GOT relocations even in static binaries (holding fixed
7118 data in that case, to satisfy those relocations). */
7119
7120 if (elf_hash_table (info)->dynamic_sections_created
7121 || bed->always_renumber_dynsyms)
7122 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7123 &section_sym_count);
7124
8423293d
AM
7125 if (elf_hash_table (info)->dynamic_sections_created)
7126 {
7127 bfd *dynobj;
8423293d 7128 asection *s;
8423293d
AM
7129 unsigned int dtagcount;
7130
7131 dynobj = elf_hash_table (info)->dynobj;
7132
5a580b3a 7133 /* Work out the size of the symbol version section. */
3d4d4302 7134 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7135 BFD_ASSERT (s != NULL);
d5486c43 7136 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7137 {
eea6121a 7138 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7139 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7140 if (s->contents == NULL)
7141 return FALSE;
7142
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7144 return FALSE;
7145 }
7146
7147 /* Set the size of the .dynsym and .hash sections. We counted
7148 the number of dynamic symbols in elf_link_add_object_symbols.
7149 We will build the contents of .dynsym and .hash when we build
7150 the final symbol table, because until then we do not know the
7151 correct value to give the symbols. We built the .dynstr
7152 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7153 s = elf_hash_table (info)->dynsym;
5a580b3a 7154 BFD_ASSERT (s != NULL);
eea6121a 7155 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7156
d5486c43
L
7157 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7158 if (s->contents == NULL)
7159 return FALSE;
5a580b3a 7160
d5486c43
L
7161 /* The first entry in .dynsym is a dummy symbol. Clear all the
7162 section syms, in case we don't output them all. */
7163 ++section_sym_count;
7164 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7165
fdc90cb4
JJ
7166 elf_hash_table (info)->bucketcount = 0;
7167
5a580b3a
AM
7168 /* Compute the size of the hashing table. As a side effect this
7169 computes the hash values for all the names we export. */
fdc90cb4
JJ
7170 if (info->emit_hash)
7171 {
7172 unsigned long int *hashcodes;
14b1c01e 7173 struct hash_codes_info hashinf;
fdc90cb4
JJ
7174 bfd_size_type amt;
7175 unsigned long int nsyms;
7176 size_t bucketcount;
7177 size_t hash_entry_size;
7178
7179 /* Compute the hash values for all exported symbols. At the same
7180 time store the values in an array so that we could use them for
7181 optimizations. */
7182 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7183 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7184 if (hashcodes == NULL)
7185 return FALSE;
14b1c01e
AM
7186 hashinf.hashcodes = hashcodes;
7187 hashinf.error = FALSE;
5a580b3a 7188
fdc90cb4
JJ
7189 /* Put all hash values in HASHCODES. */
7190 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7191 elf_collect_hash_codes, &hashinf);
7192 if (hashinf.error)
4dd07732
AM
7193 {
7194 free (hashcodes);
7195 return FALSE;
7196 }
5a580b3a 7197
14b1c01e 7198 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7199 bucketcount
7200 = compute_bucket_count (info, hashcodes, nsyms, 0);
7201 free (hashcodes);
7202
4b48e2f6 7203 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7204 return FALSE;
5a580b3a 7205
fdc90cb4
JJ
7206 elf_hash_table (info)->bucketcount = bucketcount;
7207
3d4d4302 7208 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7209 BFD_ASSERT (s != NULL);
7210 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7211 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7212 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7213 if (s->contents == NULL)
7214 return FALSE;
7215
7216 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7217 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7218 s->contents + hash_entry_size);
7219 }
7220
7221 if (info->emit_gnu_hash)
7222 {
7223 size_t i, cnt;
7224 unsigned char *contents;
7225 struct collect_gnu_hash_codes cinfo;
7226 bfd_size_type amt;
7227 size_t bucketcount;
7228
7229 memset (&cinfo, 0, sizeof (cinfo));
7230
7231 /* Compute the hash values for all exported symbols. At the same
7232 time store the values in an array so that we could use them for
7233 optimizations. */
7234 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7235 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7236 if (cinfo.hashcodes == NULL)
7237 return FALSE;
7238
7239 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7240 cinfo.min_dynindx = -1;
7241 cinfo.output_bfd = output_bfd;
7242 cinfo.bed = bed;
7243
7244 /* Put all hash values in HASHCODES. */
7245 elf_link_hash_traverse (elf_hash_table (info),
7246 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7247 if (cinfo.error)
4dd07732
AM
7248 {
7249 free (cinfo.hashcodes);
7250 return FALSE;
7251 }
fdc90cb4
JJ
7252
7253 bucketcount
7254 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7255
7256 if (bucketcount == 0)
7257 {
7258 free (cinfo.hashcodes);
7259 return FALSE;
7260 }
7261
3d4d4302 7262 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7263 BFD_ASSERT (s != NULL);
7264
7265 if (cinfo.nsyms == 0)
7266 {
7267 /* Empty .gnu.hash section is special. */
7268 BFD_ASSERT (cinfo.min_dynindx == -1);
7269 free (cinfo.hashcodes);
7270 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7271 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7272 if (contents == NULL)
7273 return FALSE;
7274 s->contents = contents;
7275 /* 1 empty bucket. */
7276 bfd_put_32 (output_bfd, 1, contents);
7277 /* SYMIDX above the special symbol 0. */
7278 bfd_put_32 (output_bfd, 1, contents + 4);
7279 /* Just one word for bitmask. */
7280 bfd_put_32 (output_bfd, 1, contents + 8);
7281 /* Only hash fn bloom filter. */
7282 bfd_put_32 (output_bfd, 0, contents + 12);
7283 /* No hashes are valid - empty bitmask. */
7284 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7285 /* No hashes in the only bucket. */
7286 bfd_put_32 (output_bfd, 0,
7287 contents + 16 + bed->s->arch_size / 8);
7288 }
7289 else
7290 {
9e6619e2 7291 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7292 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7293
9e6619e2
AM
7294 x = cinfo.nsyms;
7295 maskbitslog2 = 1;
7296 while ((x >>= 1) != 0)
7297 ++maskbitslog2;
fdc90cb4
JJ
7298 if (maskbitslog2 < 3)
7299 maskbitslog2 = 5;
7300 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7301 maskbitslog2 = maskbitslog2 + 3;
7302 else
7303 maskbitslog2 = maskbitslog2 + 2;
7304 if (bed->s->arch_size == 64)
7305 {
7306 if (maskbitslog2 == 5)
7307 maskbitslog2 = 6;
7308 cinfo.shift1 = 6;
7309 }
7310 else
7311 cinfo.shift1 = 5;
7312 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7313 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7314 cinfo.maskbits = 1 << maskbitslog2;
7315 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7316 amt = bucketcount * sizeof (unsigned long int) * 2;
7317 amt += maskwords * sizeof (bfd_vma);
a50b1753 7318 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7319 if (cinfo.bitmask == NULL)
7320 {
7321 free (cinfo.hashcodes);
7322 return FALSE;
7323 }
7324
a50b1753 7325 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7326 cinfo.indx = cinfo.counts + bucketcount;
7327 cinfo.symindx = dynsymcount - cinfo.nsyms;
7328 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7329
7330 /* Determine how often each hash bucket is used. */
7331 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7332 for (i = 0; i < cinfo.nsyms; ++i)
7333 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7334
7335 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7336 if (cinfo.counts[i] != 0)
7337 {
7338 cinfo.indx[i] = cnt;
7339 cnt += cinfo.counts[i];
7340 }
7341 BFD_ASSERT (cnt == dynsymcount);
7342 cinfo.bucketcount = bucketcount;
7343 cinfo.local_indx = cinfo.min_dynindx;
7344
7345 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7346 s->size += cinfo.maskbits / 8;
a50b1753 7347 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7348 if (contents == NULL)
7349 {
7350 free (cinfo.bitmask);
7351 free (cinfo.hashcodes);
7352 return FALSE;
7353 }
7354
7355 s->contents = contents;
7356 bfd_put_32 (output_bfd, bucketcount, contents);
7357 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7358 bfd_put_32 (output_bfd, maskwords, contents + 8);
7359 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7360 contents += 16 + cinfo.maskbits / 8;
7361
7362 for (i = 0; i < bucketcount; ++i)
7363 {
7364 if (cinfo.counts[i] == 0)
7365 bfd_put_32 (output_bfd, 0, contents);
7366 else
7367 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7368 contents += 4;
7369 }
7370
7371 cinfo.contents = contents;
7372
7373 /* Renumber dynamic symbols, populate .gnu.hash section. */
7374 elf_link_hash_traverse (elf_hash_table (info),
7375 elf_renumber_gnu_hash_syms, &cinfo);
7376
7377 contents = s->contents + 16;
7378 for (i = 0; i < maskwords; ++i)
7379 {
7380 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7381 contents);
7382 contents += bed->s->arch_size / 8;
7383 }
7384
7385 free (cinfo.bitmask);
7386 free (cinfo.hashcodes);
7387 }
7388 }
5a580b3a 7389
3d4d4302 7390 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7391 BFD_ASSERT (s != NULL);
7392
4ad4eba5 7393 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7394
eea6121a 7395 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7396
7397 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7398 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7399 return FALSE;
7400 }
7401
7402 return TRUE;
7403}
4d269e42 7404\f
4d269e42
AM
7405/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7406
7407static void
7408merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7409 asection *sec)
7410{
dbaa2011
AM
7411 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7412 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7413}
7414
7415/* Finish SHF_MERGE section merging. */
7416
7417bfd_boolean
630993ec 7418_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7419{
7420 bfd *ibfd;
7421 asection *sec;
7422
7423 if (!is_elf_hash_table (info->hash))
7424 return FALSE;
7425
c72f2fb2 7426 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7427 if ((ibfd->flags & DYNAMIC) == 0
7428 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7429 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7430 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7431 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7432 if ((sec->flags & SEC_MERGE) != 0
7433 && !bfd_is_abs_section (sec->output_section))
7434 {
7435 struct bfd_elf_section_data *secdata;
7436
7437 secdata = elf_section_data (sec);
630993ec 7438 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7439 &elf_hash_table (info)->merge_info,
7440 sec, &secdata->sec_info))
7441 return FALSE;
7442 else if (secdata->sec_info)
dbaa2011 7443 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7444 }
7445
7446 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7447 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7448 merge_sections_remove_hook);
7449 return TRUE;
7450}
7451
7452/* Create an entry in an ELF linker hash table. */
7453
7454struct bfd_hash_entry *
7455_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7456 struct bfd_hash_table *table,
7457 const char *string)
7458{
7459 /* Allocate the structure if it has not already been allocated by a
7460 subclass. */
7461 if (entry == NULL)
7462 {
a50b1753 7463 entry = (struct bfd_hash_entry *)
ca4be51c 7464 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7465 if (entry == NULL)
7466 return entry;
7467 }
7468
7469 /* Call the allocation method of the superclass. */
7470 entry = _bfd_link_hash_newfunc (entry, table, string);
7471 if (entry != NULL)
7472 {
7473 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7474 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7475
7476 /* Set local fields. */
7477 ret->indx = -1;
7478 ret->dynindx = -1;
7479 ret->got = htab->init_got_refcount;
7480 ret->plt = htab->init_plt_refcount;
7481 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7482 - offsetof (struct elf_link_hash_entry, size)));
7483 /* Assume that we have been called by a non-ELF symbol reader.
7484 This flag is then reset by the code which reads an ELF input
7485 file. This ensures that a symbol created by a non-ELF symbol
7486 reader will have the flag set correctly. */
7487 ret->non_elf = 1;
7488 }
7489
7490 return entry;
7491}
7492
7493/* Copy data from an indirect symbol to its direct symbol, hiding the
7494 old indirect symbol. Also used for copying flags to a weakdef. */
7495
7496void
7497_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7498 struct elf_link_hash_entry *dir,
7499 struct elf_link_hash_entry *ind)
7500{
7501 struct elf_link_hash_table *htab;
7502
7503 /* Copy down any references that we may have already seen to the
e81830c5 7504 symbol which just became indirect. */
4d269e42 7505
422f1182 7506 if (dir->versioned != versioned_hidden)
e81830c5
AM
7507 dir->ref_dynamic |= ind->ref_dynamic;
7508 dir->ref_regular |= ind->ref_regular;
7509 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7510 dir->non_got_ref |= ind->non_got_ref;
7511 dir->needs_plt |= ind->needs_plt;
7512 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7513
7514 if (ind->root.type != bfd_link_hash_indirect)
7515 return;
7516
7517 /* Copy over the global and procedure linkage table refcount entries.
7518 These may have been already set up by a check_relocs routine. */
7519 htab = elf_hash_table (info);
7520 if (ind->got.refcount > htab->init_got_refcount.refcount)
7521 {
7522 if (dir->got.refcount < 0)
7523 dir->got.refcount = 0;
7524 dir->got.refcount += ind->got.refcount;
7525 ind->got.refcount = htab->init_got_refcount.refcount;
7526 }
7527
7528 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7529 {
7530 if (dir->plt.refcount < 0)
7531 dir->plt.refcount = 0;
7532 dir->plt.refcount += ind->plt.refcount;
7533 ind->plt.refcount = htab->init_plt_refcount.refcount;
7534 }
7535
7536 if (ind->dynindx != -1)
7537 {
7538 if (dir->dynindx != -1)
7539 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7540 dir->dynindx = ind->dynindx;
7541 dir->dynstr_index = ind->dynstr_index;
7542 ind->dynindx = -1;
7543 ind->dynstr_index = 0;
7544 }
7545}
7546
7547void
7548_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7549 struct elf_link_hash_entry *h,
7550 bfd_boolean force_local)
7551{
3aa14d16
L
7552 /* STT_GNU_IFUNC symbol must go through PLT. */
7553 if (h->type != STT_GNU_IFUNC)
7554 {
7555 h->plt = elf_hash_table (info)->init_plt_offset;
7556 h->needs_plt = 0;
7557 }
4d269e42
AM
7558 if (force_local)
7559 {
7560 h->forced_local = 1;
7561 if (h->dynindx != -1)
7562 {
4d269e42
AM
7563 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7564 h->dynstr_index);
641338d8
AM
7565 h->dynindx = -1;
7566 h->dynstr_index = 0;
4d269e42
AM
7567 }
7568 }
7569}
7570
34a87bb0
L
7571/* Hide a symbol. */
7572
7573void
7574_bfd_elf_link_hide_symbol (bfd *output_bfd,
7575 struct bfd_link_info *info,
7576 struct bfd_link_hash_entry *h)
7577{
7578 if (is_elf_hash_table (info->hash))
7579 {
7580 const struct elf_backend_data *bed
7581 = get_elf_backend_data (output_bfd);
7582 struct elf_link_hash_entry *eh
7583 = (struct elf_link_hash_entry *) h;
7584 bed->elf_backend_hide_symbol (info, eh, TRUE);
7585 eh->def_dynamic = 0;
7586 eh->ref_dynamic = 0;
7587 eh->dynamic_def = 0;
7588 }
7589}
7590
7bf52ea2
AM
7591/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7592 caller. */
4d269e42
AM
7593
7594bfd_boolean
7595_bfd_elf_link_hash_table_init
7596 (struct elf_link_hash_table *table,
7597 bfd *abfd,
7598 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7599 struct bfd_hash_table *,
7600 const char *),
4dfe6ac6
NC
7601 unsigned int entsize,
7602 enum elf_target_id target_id)
4d269e42
AM
7603{
7604 bfd_boolean ret;
7605 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7606
4d269e42
AM
7607 table->init_got_refcount.refcount = can_refcount - 1;
7608 table->init_plt_refcount.refcount = can_refcount - 1;
7609 table->init_got_offset.offset = -(bfd_vma) 1;
7610 table->init_plt_offset.offset = -(bfd_vma) 1;
7611 /* The first dynamic symbol is a dummy. */
7612 table->dynsymcount = 1;
7613
7614 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7615
4d269e42 7616 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7617 table->hash_table_id = target_id;
4d269e42
AM
7618
7619 return ret;
7620}
7621
7622/* Create an ELF linker hash table. */
7623
7624struct bfd_link_hash_table *
7625_bfd_elf_link_hash_table_create (bfd *abfd)
7626{
7627 struct elf_link_hash_table *ret;
7628 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7629
7bf52ea2 7630 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7631 if (ret == NULL)
7632 return NULL;
7633
7634 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7635 sizeof (struct elf_link_hash_entry),
7636 GENERIC_ELF_DATA))
4d269e42
AM
7637 {
7638 free (ret);
7639 return NULL;
7640 }
d495ab0d 7641 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7642
7643 return &ret->root;
7644}
7645
9f7c3e5e
AM
7646/* Destroy an ELF linker hash table. */
7647
7648void
d495ab0d 7649_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7650{
d495ab0d
AM
7651 struct elf_link_hash_table *htab;
7652
7653 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7654 if (htab->dynstr != NULL)
7655 _bfd_elf_strtab_free (htab->dynstr);
7656 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7657 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7658}
7659
4d269e42
AM
7660/* This is a hook for the ELF emulation code in the generic linker to
7661 tell the backend linker what file name to use for the DT_NEEDED
7662 entry for a dynamic object. */
7663
7664void
7665bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7666{
7667 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7668 && bfd_get_format (abfd) == bfd_object)
7669 elf_dt_name (abfd) = name;
7670}
7671
7672int
7673bfd_elf_get_dyn_lib_class (bfd *abfd)
7674{
7675 int lib_class;
7676 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7677 && bfd_get_format (abfd) == bfd_object)
7678 lib_class = elf_dyn_lib_class (abfd);
7679 else
7680 lib_class = 0;
7681 return lib_class;
7682}
7683
7684void
7685bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7686{
7687 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7688 && bfd_get_format (abfd) == bfd_object)
7689 elf_dyn_lib_class (abfd) = lib_class;
7690}
7691
7692/* Get the list of DT_NEEDED entries for a link. This is a hook for
7693 the linker ELF emulation code. */
7694
7695struct bfd_link_needed_list *
7696bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7697 struct bfd_link_info *info)
7698{
7699 if (! is_elf_hash_table (info->hash))
7700 return NULL;
7701 return elf_hash_table (info)->needed;
7702}
7703
7704/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7705 hook for the linker ELF emulation code. */
7706
7707struct bfd_link_needed_list *
7708bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7709 struct bfd_link_info *info)
7710{
7711 if (! is_elf_hash_table (info->hash))
7712 return NULL;
7713 return elf_hash_table (info)->runpath;
7714}
7715
7716/* Get the name actually used for a dynamic object for a link. This
7717 is the SONAME entry if there is one. Otherwise, it is the string
7718 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7719
7720const char *
7721bfd_elf_get_dt_soname (bfd *abfd)
7722{
7723 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7724 && bfd_get_format (abfd) == bfd_object)
7725 return elf_dt_name (abfd);
7726 return NULL;
7727}
7728
7729/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7730 the ELF linker emulation code. */
7731
7732bfd_boolean
7733bfd_elf_get_bfd_needed_list (bfd *abfd,
7734 struct bfd_link_needed_list **pneeded)
7735{
7736 asection *s;
7737 bfd_byte *dynbuf = NULL;
cb33740c 7738 unsigned int elfsec;
4d269e42
AM
7739 unsigned long shlink;
7740 bfd_byte *extdyn, *extdynend;
7741 size_t extdynsize;
7742 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7743
7744 *pneeded = NULL;
7745
7746 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7747 || bfd_get_format (abfd) != bfd_object)
7748 return TRUE;
7749
7750 s = bfd_get_section_by_name (abfd, ".dynamic");
7751 if (s == NULL || s->size == 0)
7752 return TRUE;
7753
7754 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7755 goto error_return;
7756
7757 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7758 if (elfsec == SHN_BAD)
4d269e42
AM
7759 goto error_return;
7760
7761 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7762
4d269e42
AM
7763 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7764 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7765
7766 extdyn = dynbuf;
7767 extdynend = extdyn + s->size;
7768 for (; extdyn < extdynend; extdyn += extdynsize)
7769 {
7770 Elf_Internal_Dyn dyn;
7771
7772 (*swap_dyn_in) (abfd, extdyn, &dyn);
7773
7774 if (dyn.d_tag == DT_NULL)
7775 break;
7776
7777 if (dyn.d_tag == DT_NEEDED)
7778 {
7779 const char *string;
7780 struct bfd_link_needed_list *l;
7781 unsigned int tagv = dyn.d_un.d_val;
7782 bfd_size_type amt;
7783
7784 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7785 if (string == NULL)
7786 goto error_return;
7787
7788 amt = sizeof *l;
a50b1753 7789 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7790 if (l == NULL)
7791 goto error_return;
7792
7793 l->by = abfd;
7794 l->name = string;
7795 l->next = *pneeded;
7796 *pneeded = l;
7797 }
7798 }
7799
7800 free (dynbuf);
7801
7802 return TRUE;
7803
7804 error_return:
7805 if (dynbuf != NULL)
7806 free (dynbuf);
7807 return FALSE;
7808}
7809
7810struct elf_symbuf_symbol
7811{
7812 unsigned long st_name; /* Symbol name, index in string tbl */
7813 unsigned char st_info; /* Type and binding attributes */
7814 unsigned char st_other; /* Visibilty, and target specific */
7815};
7816
7817struct elf_symbuf_head
7818{
7819 struct elf_symbuf_symbol *ssym;
ef53be89 7820 size_t count;
4d269e42
AM
7821 unsigned int st_shndx;
7822};
7823
7824struct elf_symbol
7825{
7826 union
7827 {
7828 Elf_Internal_Sym *isym;
7829 struct elf_symbuf_symbol *ssym;
7830 } u;
7831 const char *name;
7832};
7833
7834/* Sort references to symbols by ascending section number. */
7835
7836static int
7837elf_sort_elf_symbol (const void *arg1, const void *arg2)
7838{
7839 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7840 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7841
7842 return s1->st_shndx - s2->st_shndx;
7843}
7844
7845static int
7846elf_sym_name_compare (const void *arg1, const void *arg2)
7847{
7848 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7849 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7850 return strcmp (s1->name, s2->name);
7851}
7852
7853static struct elf_symbuf_head *
ef53be89 7854elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7855{
14b1c01e 7856 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7857 struct elf_symbuf_symbol *ssym;
7858 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7859 size_t i, shndx_count, total_size;
4d269e42 7860
a50b1753 7861 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7862 if (indbuf == NULL)
7863 return NULL;
7864
7865 for (ind = indbuf, i = 0; i < symcount; i++)
7866 if (isymbuf[i].st_shndx != SHN_UNDEF)
7867 *ind++ = &isymbuf[i];
7868 indbufend = ind;
7869
7870 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7871 elf_sort_elf_symbol);
7872
7873 shndx_count = 0;
7874 if (indbufend > indbuf)
7875 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7876 if (ind[0]->st_shndx != ind[1]->st_shndx)
7877 shndx_count++;
7878
3ae181ee
L
7879 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7880 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7881 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7882 if (ssymbuf == NULL)
7883 {
7884 free (indbuf);
7885 return NULL;
7886 }
7887
3ae181ee 7888 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7889 ssymbuf->ssym = NULL;
7890 ssymbuf->count = shndx_count;
7891 ssymbuf->st_shndx = 0;
7892 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7893 {
7894 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7895 {
7896 ssymhead++;
7897 ssymhead->ssym = ssym;
7898 ssymhead->count = 0;
7899 ssymhead->st_shndx = (*ind)->st_shndx;
7900 }
7901 ssym->st_name = (*ind)->st_name;
7902 ssym->st_info = (*ind)->st_info;
7903 ssym->st_other = (*ind)->st_other;
7904 ssymhead->count++;
7905 }
ef53be89 7906 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7907 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7908 == total_size));
4d269e42
AM
7909
7910 free (indbuf);
7911 return ssymbuf;
7912}
7913
7914/* Check if 2 sections define the same set of local and global
7915 symbols. */
7916
8f317e31 7917static bfd_boolean
4d269e42
AM
7918bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7919 struct bfd_link_info *info)
7920{
7921 bfd *bfd1, *bfd2;
7922 const struct elf_backend_data *bed1, *bed2;
7923 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7924 size_t symcount1, symcount2;
4d269e42
AM
7925 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7926 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7927 Elf_Internal_Sym *isym, *isymend;
7928 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7929 size_t count1, count2, i;
cb33740c 7930 unsigned int shndx1, shndx2;
4d269e42
AM
7931 bfd_boolean result;
7932
7933 bfd1 = sec1->owner;
7934 bfd2 = sec2->owner;
7935
4d269e42
AM
7936 /* Both sections have to be in ELF. */
7937 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7938 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7939 return FALSE;
7940
7941 if (elf_section_type (sec1) != elf_section_type (sec2))
7942 return FALSE;
7943
4d269e42
AM
7944 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7945 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7946 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7947 return FALSE;
7948
7949 bed1 = get_elf_backend_data (bfd1);
7950 bed2 = get_elf_backend_data (bfd2);
7951 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7952 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7953 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7954 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7955
7956 if (symcount1 == 0 || symcount2 == 0)
7957 return FALSE;
7958
7959 result = FALSE;
7960 isymbuf1 = NULL;
7961 isymbuf2 = NULL;
a50b1753
NC
7962 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7963 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7964
7965 if (ssymbuf1 == NULL)
7966 {
7967 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7968 NULL, NULL, NULL);
7969 if (isymbuf1 == NULL)
7970 goto done;
7971
7972 if (!info->reduce_memory_overheads)
7973 elf_tdata (bfd1)->symbuf = ssymbuf1
7974 = elf_create_symbuf (symcount1, isymbuf1);
7975 }
7976
7977 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7978 {
7979 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7980 NULL, NULL, NULL);
7981 if (isymbuf2 == NULL)
7982 goto done;
7983
7984 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7985 elf_tdata (bfd2)->symbuf = ssymbuf2
7986 = elf_create_symbuf (symcount2, isymbuf2);
7987 }
7988
7989 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7990 {
7991 /* Optimized faster version. */
ef53be89 7992 size_t lo, hi, mid;
4d269e42
AM
7993 struct elf_symbol *symp;
7994 struct elf_symbuf_symbol *ssym, *ssymend;
7995
7996 lo = 0;
7997 hi = ssymbuf1->count;
7998 ssymbuf1++;
7999 count1 = 0;
8000 while (lo < hi)
8001 {
8002 mid = (lo + hi) / 2;
cb33740c 8003 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8004 hi = mid;
cb33740c 8005 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8006 lo = mid + 1;
8007 else
8008 {
8009 count1 = ssymbuf1[mid].count;
8010 ssymbuf1 += mid;
8011 break;
8012 }
8013 }
8014
8015 lo = 0;
8016 hi = ssymbuf2->count;
8017 ssymbuf2++;
8018 count2 = 0;
8019 while (lo < hi)
8020 {
8021 mid = (lo + hi) / 2;
cb33740c 8022 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8023 hi = mid;
cb33740c 8024 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8025 lo = mid + 1;
8026 else
8027 {
8028 count2 = ssymbuf2[mid].count;
8029 ssymbuf2 += mid;
8030 break;
8031 }
8032 }
8033
8034 if (count1 == 0 || count2 == 0 || count1 != count2)
8035 goto done;
8036
ca4be51c
AM
8037 symtable1
8038 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8039 symtable2
8040 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8041 if (symtable1 == NULL || symtable2 == NULL)
8042 goto done;
8043
8044 symp = symtable1;
8045 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8046 ssym < ssymend; ssym++, symp++)
8047 {
8048 symp->u.ssym = ssym;
8049 symp->name = bfd_elf_string_from_elf_section (bfd1,
8050 hdr1->sh_link,
8051 ssym->st_name);
8052 }
8053
8054 symp = symtable2;
8055 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8056 ssym < ssymend; ssym++, symp++)
8057 {
8058 symp->u.ssym = ssym;
8059 symp->name = bfd_elf_string_from_elf_section (bfd2,
8060 hdr2->sh_link,
8061 ssym->st_name);
8062 }
8063
8064 /* Sort symbol by name. */
8065 qsort (symtable1, count1, sizeof (struct elf_symbol),
8066 elf_sym_name_compare);
8067 qsort (symtable2, count1, sizeof (struct elf_symbol),
8068 elf_sym_name_compare);
8069
8070 for (i = 0; i < count1; i++)
8071 /* Two symbols must have the same binding, type and name. */
8072 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8073 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8074 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8075 goto done;
8076
8077 result = TRUE;
8078 goto done;
8079 }
8080
a50b1753
NC
8081 symtable1 = (struct elf_symbol *)
8082 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8083 symtable2 = (struct elf_symbol *)
8084 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8085 if (symtable1 == NULL || symtable2 == NULL)
8086 goto done;
8087
8088 /* Count definitions in the section. */
8089 count1 = 0;
8090 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8091 if (isym->st_shndx == shndx1)
4d269e42
AM
8092 symtable1[count1++].u.isym = isym;
8093
8094 count2 = 0;
8095 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8096 if (isym->st_shndx == shndx2)
4d269e42
AM
8097 symtable2[count2++].u.isym = isym;
8098
8099 if (count1 == 0 || count2 == 0 || count1 != count2)
8100 goto done;
8101
8102 for (i = 0; i < count1; i++)
8103 symtable1[i].name
8104 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8105 symtable1[i].u.isym->st_name);
8106
8107 for (i = 0; i < count2; i++)
8108 symtable2[i].name
8109 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8110 symtable2[i].u.isym->st_name);
8111
8112 /* Sort symbol by name. */
8113 qsort (symtable1, count1, sizeof (struct elf_symbol),
8114 elf_sym_name_compare);
8115 qsort (symtable2, count1, sizeof (struct elf_symbol),
8116 elf_sym_name_compare);
8117
8118 for (i = 0; i < count1; i++)
8119 /* Two symbols must have the same binding, type and name. */
8120 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8121 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8122 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8123 goto done;
8124
8125 result = TRUE;
8126
8127done:
8128 if (symtable1)
8129 free (symtable1);
8130 if (symtable2)
8131 free (symtable2);
8132 if (isymbuf1)
8133 free (isymbuf1);
8134 if (isymbuf2)
8135 free (isymbuf2);
8136
8137 return result;
8138}
8139
8140/* Return TRUE if 2 section types are compatible. */
8141
8142bfd_boolean
8143_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8144 bfd *bbfd, const asection *bsec)
8145{
8146 if (asec == NULL
8147 || bsec == NULL
8148 || abfd->xvec->flavour != bfd_target_elf_flavour
8149 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8150 return TRUE;
8151
8152 return elf_section_type (asec) == elf_section_type (bsec);
8153}
8154\f
c152c796
AM
8155/* Final phase of ELF linker. */
8156
8157/* A structure we use to avoid passing large numbers of arguments. */
8158
8159struct elf_final_link_info
8160{
8161 /* General link information. */
8162 struct bfd_link_info *info;
8163 /* Output BFD. */
8164 bfd *output_bfd;
8165 /* Symbol string table. */
ef10c3ac 8166 struct elf_strtab_hash *symstrtab;
c152c796
AM
8167 /* .hash section. */
8168 asection *hash_sec;
8169 /* symbol version section (.gnu.version). */
8170 asection *symver_sec;
8171 /* Buffer large enough to hold contents of any section. */
8172 bfd_byte *contents;
8173 /* Buffer large enough to hold external relocs of any section. */
8174 void *external_relocs;
8175 /* Buffer large enough to hold internal relocs of any section. */
8176 Elf_Internal_Rela *internal_relocs;
8177 /* Buffer large enough to hold external local symbols of any input
8178 BFD. */
8179 bfd_byte *external_syms;
8180 /* And a buffer for symbol section indices. */
8181 Elf_External_Sym_Shndx *locsym_shndx;
8182 /* Buffer large enough to hold internal local symbols of any input
8183 BFD. */
8184 Elf_Internal_Sym *internal_syms;
8185 /* Array large enough to hold a symbol index for each local symbol
8186 of any input BFD. */
8187 long *indices;
8188 /* Array large enough to hold a section pointer for each local
8189 symbol of any input BFD. */
8190 asection **sections;
ef10c3ac 8191 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8192 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8193 /* Number of STT_FILE syms seen. */
8194 size_t filesym_count;
c152c796
AM
8195};
8196
8197/* This struct is used to pass information to elf_link_output_extsym. */
8198
8199struct elf_outext_info
8200{
8201 bfd_boolean failed;
8202 bfd_boolean localsyms;
34a79995 8203 bfd_boolean file_sym_done;
8b127cbc 8204 struct elf_final_link_info *flinfo;
c152c796
AM
8205};
8206
d9352518
DB
8207
8208/* Support for evaluating a complex relocation.
8209
8210 Complex relocations are generalized, self-describing relocations. The
8211 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8212 relocations themselves.
d9352518
DB
8213
8214 The relocations are use a reserved elf-wide relocation type code (R_RELC
8215 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8216 information (start bit, end bit, word width, etc) into the addend. This
8217 information is extracted from CGEN-generated operand tables within gas.
8218
8219 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8220 internal) representing prefix-notation expressions, including but not
8221 limited to those sorts of expressions normally encoded as addends in the
8222 addend field. The symbol mangling format is:
8223
8224 <node> := <literal>
07d6d2b8
AM
8225 | <unary-operator> ':' <node>
8226 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8227 ;
8228
8229 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8230 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8231 | '#' <hexdigits>
8232 ;
8233
8234 <binary-operator> := as in C
8235 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8236
8237static void
a0c8462f
AM
8238set_symbol_value (bfd *bfd_with_globals,
8239 Elf_Internal_Sym *isymbuf,
8240 size_t locsymcount,
8241 size_t symidx,
8242 bfd_vma val)
d9352518 8243{
8977835c
AM
8244 struct elf_link_hash_entry **sym_hashes;
8245 struct elf_link_hash_entry *h;
8246 size_t extsymoff = locsymcount;
d9352518 8247
8977835c 8248 if (symidx < locsymcount)
d9352518 8249 {
8977835c
AM
8250 Elf_Internal_Sym *sym;
8251
8252 sym = isymbuf + symidx;
8253 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8254 {
8255 /* It is a local symbol: move it to the
8256 "absolute" section and give it a value. */
8257 sym->st_shndx = SHN_ABS;
8258 sym->st_value = val;
8259 return;
8260 }
8261 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8262 extsymoff = 0;
d9352518 8263 }
8977835c
AM
8264
8265 /* It is a global symbol: set its link type
8266 to "defined" and give it a value. */
8267
8268 sym_hashes = elf_sym_hashes (bfd_with_globals);
8269 h = sym_hashes [symidx - extsymoff];
8270 while (h->root.type == bfd_link_hash_indirect
8271 || h->root.type == bfd_link_hash_warning)
8272 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8273 h->root.type = bfd_link_hash_defined;
8274 h->root.u.def.value = val;
8275 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8276}
8277
a0c8462f
AM
8278static bfd_boolean
8279resolve_symbol (const char *name,
8280 bfd *input_bfd,
8b127cbc 8281 struct elf_final_link_info *flinfo,
a0c8462f
AM
8282 bfd_vma *result,
8283 Elf_Internal_Sym *isymbuf,
8284 size_t locsymcount)
d9352518 8285{
a0c8462f
AM
8286 Elf_Internal_Sym *sym;
8287 struct bfd_link_hash_entry *global_entry;
8288 const char *candidate = NULL;
8289 Elf_Internal_Shdr *symtab_hdr;
8290 size_t i;
8291
d9352518
DB
8292 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8293
8294 for (i = 0; i < locsymcount; ++ i)
8295 {
8977835c 8296 sym = isymbuf + i;
d9352518
DB
8297
8298 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8299 continue;
8300
8301 candidate = bfd_elf_string_from_elf_section (input_bfd,
8302 symtab_hdr->sh_link,
8303 sym->st_name);
8304#ifdef DEBUG
0f02bbd9
AM
8305 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8306 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8307#endif
8308 if (candidate && strcmp (candidate, name) == 0)
8309 {
8b127cbc 8310 asection *sec = flinfo->sections [i];
d9352518 8311
0f02bbd9
AM
8312 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8313 *result += sec->output_offset + sec->output_section->vma;
d9352518 8314#ifdef DEBUG
0f02bbd9
AM
8315 printf ("Found symbol with value %8.8lx\n",
8316 (unsigned long) *result);
d9352518
DB
8317#endif
8318 return TRUE;
8319 }
8320 }
8321
8322 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8323 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8324 FALSE, FALSE, TRUE);
d9352518
DB
8325 if (!global_entry)
8326 return FALSE;
a0c8462f 8327
d9352518
DB
8328 if (global_entry->type == bfd_link_hash_defined
8329 || global_entry->type == bfd_link_hash_defweak)
8330 {
a0c8462f
AM
8331 *result = (global_entry->u.def.value
8332 + global_entry->u.def.section->output_section->vma
8333 + global_entry->u.def.section->output_offset);
d9352518 8334#ifdef DEBUG
0f02bbd9
AM
8335 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8336 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8337#endif
8338 return TRUE;
a0c8462f 8339 }
d9352518 8340
d9352518
DB
8341 return FALSE;
8342}
8343
37b01f6a
DG
8344/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8345 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8346 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8347
d9352518 8348static bfd_boolean
a0c8462f
AM
8349resolve_section (const char *name,
8350 asection *sections,
37b01f6a
DG
8351 bfd_vma *result,
8352 bfd * abfd)
d9352518 8353{
a0c8462f
AM
8354 asection *curr;
8355 unsigned int len;
d9352518 8356
a0c8462f 8357 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8358 if (strcmp (curr->name, name) == 0)
8359 {
8360 *result = curr->vma;
8361 return TRUE;
8362 }
8363
8364 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8365 /* FIXME: This could be coded more efficiently... */
a0c8462f 8366 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8367 {
8368 len = strlen (curr->name);
a0c8462f 8369 if (len > strlen (name))
d9352518
DB
8370 continue;
8371
8372 if (strncmp (curr->name, name, len) == 0)
8373 {
8374 if (strncmp (".end", name + len, 4) == 0)
8375 {
37b01f6a 8376 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8377 return TRUE;
8378 }
8379
8380 /* Insert more pseudo-section names here, if you like. */
8381 }
8382 }
a0c8462f 8383
d9352518
DB
8384 return FALSE;
8385}
8386
8387static void
a0c8462f 8388undefined_reference (const char *reftype, const char *name)
d9352518 8389{
695344c0 8390 /* xgettext:c-format */
a0c8462f
AM
8391 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8392 reftype, name);
d9352518
DB
8393}
8394
8395static bfd_boolean
a0c8462f
AM
8396eval_symbol (bfd_vma *result,
8397 const char **symp,
8398 bfd *input_bfd,
8b127cbc 8399 struct elf_final_link_info *flinfo,
a0c8462f
AM
8400 bfd_vma dot,
8401 Elf_Internal_Sym *isymbuf,
8402 size_t locsymcount,
8403 int signed_p)
d9352518 8404{
4b93929b
NC
8405 size_t len;
8406 size_t symlen;
a0c8462f
AM
8407 bfd_vma a;
8408 bfd_vma b;
4b93929b 8409 char symbuf[4096];
0f02bbd9 8410 const char *sym = *symp;
a0c8462f
AM
8411 const char *symend;
8412 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8413
8414 len = strlen (sym);
8415 symend = sym + len;
8416
4b93929b 8417 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8418 {
8419 bfd_set_error (bfd_error_invalid_operation);
8420 return FALSE;
8421 }
a0c8462f 8422
d9352518
DB
8423 switch (* sym)
8424 {
8425 case '.':
0f02bbd9
AM
8426 *result = dot;
8427 *symp = sym + 1;
d9352518
DB
8428 return TRUE;
8429
8430 case '#':
0f02bbd9
AM
8431 ++sym;
8432 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8433 return TRUE;
8434
8435 case 'S':
8436 symbol_is_section = TRUE;
1a0670f3 8437 /* Fall through. */
a0c8462f 8438 case 's':
0f02bbd9
AM
8439 ++sym;
8440 symlen = strtol (sym, (char **) symp, 10);
8441 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8442
4b93929b 8443 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8444 {
8445 bfd_set_error (bfd_error_invalid_operation);
8446 return FALSE;
8447 }
8448
8449 memcpy (symbuf, sym, symlen);
a0c8462f 8450 symbuf[symlen] = '\0';
0f02bbd9 8451 *symp = sym + symlen;
a0c8462f
AM
8452
8453 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8454 the symbol as a section, or vice-versa. so we're pretty liberal in our
8455 interpretation here; section means "try section first", not "must be a
8456 section", and likewise with symbol. */
8457
a0c8462f 8458 if (symbol_is_section)
d9352518 8459 {
37b01f6a 8460 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8461 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8462 isymbuf, locsymcount))
d9352518
DB
8463 {
8464 undefined_reference ("section", symbuf);
8465 return FALSE;
8466 }
a0c8462f
AM
8467 }
8468 else
d9352518 8469 {
8b127cbc 8470 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8471 isymbuf, locsymcount)
8b127cbc 8472 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8473 result, input_bfd))
d9352518
DB
8474 {
8475 undefined_reference ("symbol", symbuf);
8476 return FALSE;
8477 }
8478 }
8479
8480 return TRUE;
a0c8462f 8481
d9352518
DB
8482 /* All that remains are operators. */
8483
8484#define UNARY_OP(op) \
8485 if (strncmp (sym, #op, strlen (#op)) == 0) \
8486 { \
8487 sym += strlen (#op); \
a0c8462f
AM
8488 if (*sym == ':') \
8489 ++sym; \
0f02bbd9 8490 *symp = sym; \
8b127cbc 8491 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8492 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8493 return FALSE; \
8494 if (signed_p) \
0f02bbd9 8495 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8496 else \
8497 *result = op a; \
d9352518
DB
8498 return TRUE; \
8499 }
8500
8501#define BINARY_OP(op) \
8502 if (strncmp (sym, #op, strlen (#op)) == 0) \
8503 { \
8504 sym += strlen (#op); \
a0c8462f
AM
8505 if (*sym == ':') \
8506 ++sym; \
0f02bbd9 8507 *symp = sym; \
8b127cbc 8508 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8509 isymbuf, locsymcount, signed_p)) \
a0c8462f 8510 return FALSE; \
0f02bbd9 8511 ++*symp; \
8b127cbc 8512 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8513 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8514 return FALSE; \
8515 if (signed_p) \
0f02bbd9 8516 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8517 else \
8518 *result = a op b; \
d9352518
DB
8519 return TRUE; \
8520 }
8521
8522 default:
8523 UNARY_OP (0-);
8524 BINARY_OP (<<);
8525 BINARY_OP (>>);
8526 BINARY_OP (==);
8527 BINARY_OP (!=);
8528 BINARY_OP (<=);
8529 BINARY_OP (>=);
8530 BINARY_OP (&&);
8531 BINARY_OP (||);
8532 UNARY_OP (~);
8533 UNARY_OP (!);
8534 BINARY_OP (*);
8535 BINARY_OP (/);
8536 BINARY_OP (%);
8537 BINARY_OP (^);
8538 BINARY_OP (|);
8539 BINARY_OP (&);
8540 BINARY_OP (+);
8541 BINARY_OP (-);
8542 BINARY_OP (<);
8543 BINARY_OP (>);
8544#undef UNARY_OP
8545#undef BINARY_OP
8546 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8547 bfd_set_error (bfd_error_invalid_operation);
8548 return FALSE;
8549 }
8550}
8551
d9352518 8552static void
a0c8462f
AM
8553put_value (bfd_vma size,
8554 unsigned long chunksz,
8555 bfd *input_bfd,
8556 bfd_vma x,
8557 bfd_byte *location)
d9352518
DB
8558{
8559 location += (size - chunksz);
8560
41cd1ad1 8561 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8562 {
8563 switch (chunksz)
8564 {
d9352518
DB
8565 case 1:
8566 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8567 x >>= 8;
d9352518
DB
8568 break;
8569 case 2:
8570 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8571 x >>= 16;
d9352518
DB
8572 break;
8573 case 4:
8574 bfd_put_32 (input_bfd, x, location);
65164438
NC
8575 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8576 x >>= 16;
8577 x >>= 16;
d9352518 8578 break;
d9352518 8579#ifdef BFD64
41cd1ad1 8580 case 8:
d9352518 8581 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8582 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8583 x >>= 32;
8584 x >>= 32;
8585 break;
d9352518 8586#endif
41cd1ad1
NC
8587 default:
8588 abort ();
d9352518
DB
8589 break;
8590 }
8591 }
8592}
8593
a0c8462f
AM
8594static bfd_vma
8595get_value (bfd_vma size,
8596 unsigned long chunksz,
8597 bfd *input_bfd,
8598 bfd_byte *location)
d9352518 8599{
9b239e0e 8600 int shift;
d9352518
DB
8601 bfd_vma x = 0;
8602
9b239e0e
NC
8603 /* Sanity checks. */
8604 BFD_ASSERT (chunksz <= sizeof (x)
8605 && size >= chunksz
8606 && chunksz != 0
8607 && (size % chunksz) == 0
8608 && input_bfd != NULL
8609 && location != NULL);
8610
8611 if (chunksz == sizeof (x))
8612 {
8613 BFD_ASSERT (size == chunksz);
8614
8615 /* Make sure that we do not perform an undefined shift operation.
8616 We know that size == chunksz so there will only be one iteration
8617 of the loop below. */
8618 shift = 0;
8619 }
8620 else
8621 shift = 8 * chunksz;
8622
a0c8462f 8623 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8624 {
8625 switch (chunksz)
8626 {
d9352518 8627 case 1:
9b239e0e 8628 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8629 break;
8630 case 2:
9b239e0e 8631 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8632 break;
8633 case 4:
9b239e0e 8634 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8635 break;
d9352518 8636#ifdef BFD64
9b239e0e
NC
8637 case 8:
8638 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8639 break;
9b239e0e
NC
8640#endif
8641 default:
8642 abort ();
d9352518
DB
8643 }
8644 }
8645 return x;
8646}
8647
a0c8462f
AM
8648static void
8649decode_complex_addend (unsigned long *start, /* in bits */
8650 unsigned long *oplen, /* in bits */
8651 unsigned long *len, /* in bits */
8652 unsigned long *wordsz, /* in bytes */
8653 unsigned long *chunksz, /* in bytes */
8654 unsigned long *lsb0_p,
8655 unsigned long *signed_p,
8656 unsigned long *trunc_p,
8657 unsigned long encoded)
d9352518 8658{
07d6d2b8
AM
8659 * start = encoded & 0x3F;
8660 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8661 * oplen = (encoded >> 12) & 0x3F;
8662 * wordsz = (encoded >> 18) & 0xF;
8663 * chunksz = (encoded >> 22) & 0xF;
8664 * lsb0_p = (encoded >> 27) & 1;
8665 * signed_p = (encoded >> 28) & 1;
8666 * trunc_p = (encoded >> 29) & 1;
8667}
8668
cdfeee4f 8669bfd_reloc_status_type
0f02bbd9 8670bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8671 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8672 bfd_byte *contents,
8673 Elf_Internal_Rela *rel,
8674 bfd_vma relocation)
d9352518 8675{
0f02bbd9
AM
8676 bfd_vma shift, x, mask;
8677 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8678 bfd_reloc_status_type r;
d9352518
DB
8679
8680 /* Perform this reloc, since it is complex.
8681 (this is not to say that it necessarily refers to a complex
8682 symbol; merely that it is a self-describing CGEN based reloc.
8683 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8684 word size, etc) encoded within it.). */
d9352518 8685
a0c8462f
AM
8686 decode_complex_addend (&start, &oplen, &len, &wordsz,
8687 &chunksz, &lsb0_p, &signed_p,
8688 &trunc_p, rel->r_addend);
d9352518
DB
8689
8690 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8691
8692 if (lsb0_p)
8693 shift = (start + 1) - len;
8694 else
8695 shift = (8 * wordsz) - (start + len);
8696
37b01f6a
DG
8697 x = get_value (wordsz, chunksz, input_bfd,
8698 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8699
8700#ifdef DEBUG
8701 printf ("Doing complex reloc: "
8702 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8703 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8704 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8705 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8706 oplen, (unsigned long) x, (unsigned long) mask,
8707 (unsigned long) relocation);
d9352518
DB
8708#endif
8709
cdfeee4f 8710 r = bfd_reloc_ok;
d9352518 8711 if (! trunc_p)
cdfeee4f
AM
8712 /* Now do an overflow check. */
8713 r = bfd_check_overflow ((signed_p
8714 ? complain_overflow_signed
8715 : complain_overflow_unsigned),
8716 len, 0, (8 * wordsz),
8717 relocation);
a0c8462f 8718
d9352518
DB
8719 /* Do the deed. */
8720 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8721
8722#ifdef DEBUG
8723 printf (" relocation: %8.8lx\n"
8724 " shifted mask: %8.8lx\n"
8725 " shifted/masked reloc: %8.8lx\n"
8726 " result: %8.8lx\n",
9ccb8af9
AM
8727 (unsigned long) relocation, (unsigned long) (mask << shift),
8728 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8729#endif
37b01f6a
DG
8730 put_value (wordsz, chunksz, input_bfd, x,
8731 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8732 return r;
d9352518
DB
8733}
8734
0e287786
AM
8735/* Functions to read r_offset from external (target order) reloc
8736 entry. Faster than bfd_getl32 et al, because we let the compiler
8737 know the value is aligned. */
53df40a4 8738
0e287786
AM
8739static bfd_vma
8740ext32l_r_offset (const void *p)
53df40a4
AM
8741{
8742 union aligned32
8743 {
8744 uint32_t v;
8745 unsigned char c[4];
8746 };
8747 const union aligned32 *a
0e287786 8748 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8749
8750 uint32_t aval = ( (uint32_t) a->c[0]
8751 | (uint32_t) a->c[1] << 8
8752 | (uint32_t) a->c[2] << 16
8753 | (uint32_t) a->c[3] << 24);
0e287786 8754 return aval;
53df40a4
AM
8755}
8756
0e287786
AM
8757static bfd_vma
8758ext32b_r_offset (const void *p)
53df40a4
AM
8759{
8760 union aligned32
8761 {
8762 uint32_t v;
8763 unsigned char c[4];
8764 };
8765 const union aligned32 *a
0e287786 8766 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8767
8768 uint32_t aval = ( (uint32_t) a->c[0] << 24
8769 | (uint32_t) a->c[1] << 16
8770 | (uint32_t) a->c[2] << 8
8771 | (uint32_t) a->c[3]);
0e287786 8772 return aval;
53df40a4
AM
8773}
8774
8775#ifdef BFD_HOST_64_BIT
0e287786
AM
8776static bfd_vma
8777ext64l_r_offset (const void *p)
53df40a4
AM
8778{
8779 union aligned64
8780 {
8781 uint64_t v;
8782 unsigned char c[8];
8783 };
8784 const union aligned64 *a
0e287786 8785 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8786
8787 uint64_t aval = ( (uint64_t) a->c[0]
8788 | (uint64_t) a->c[1] << 8
8789 | (uint64_t) a->c[2] << 16
8790 | (uint64_t) a->c[3] << 24
8791 | (uint64_t) a->c[4] << 32
8792 | (uint64_t) a->c[5] << 40
8793 | (uint64_t) a->c[6] << 48
8794 | (uint64_t) a->c[7] << 56);
0e287786 8795 return aval;
53df40a4
AM
8796}
8797
0e287786
AM
8798static bfd_vma
8799ext64b_r_offset (const void *p)
53df40a4
AM
8800{
8801 union aligned64
8802 {
8803 uint64_t v;
8804 unsigned char c[8];
8805 };
8806 const union aligned64 *a
0e287786 8807 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8808
8809 uint64_t aval = ( (uint64_t) a->c[0] << 56
8810 | (uint64_t) a->c[1] << 48
8811 | (uint64_t) a->c[2] << 40
8812 | (uint64_t) a->c[3] << 32
8813 | (uint64_t) a->c[4] << 24
8814 | (uint64_t) a->c[5] << 16
8815 | (uint64_t) a->c[6] << 8
8816 | (uint64_t) a->c[7]);
0e287786 8817 return aval;
53df40a4
AM
8818}
8819#endif
8820
c152c796
AM
8821/* When performing a relocatable link, the input relocations are
8822 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8823 referenced must be updated. Update all the relocations found in
8824 RELDATA. */
c152c796 8825
bca6d0e3 8826static bfd_boolean
c152c796 8827elf_link_adjust_relocs (bfd *abfd,
9eaff861 8828 asection *sec,
28dbcedc 8829 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8830 bfd_boolean sort,
8831 struct bfd_link_info *info)
c152c796
AM
8832{
8833 unsigned int i;
8834 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8835 bfd_byte *erela;
8836 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8837 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8838 bfd_vma r_type_mask;
8839 int r_sym_shift;
d4730f92
BS
8840 unsigned int count = reldata->count;
8841 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8842
d4730f92 8843 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8844 {
8845 swap_in = bed->s->swap_reloc_in;
8846 swap_out = bed->s->swap_reloc_out;
8847 }
d4730f92 8848 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8849 {
8850 swap_in = bed->s->swap_reloca_in;
8851 swap_out = bed->s->swap_reloca_out;
8852 }
8853 else
8854 abort ();
8855
8856 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8857 abort ();
8858
8859 if (bed->s->arch_size == 32)
8860 {
8861 r_type_mask = 0xff;
8862 r_sym_shift = 8;
8863 }
8864 else
8865 {
8866 r_type_mask = 0xffffffff;
8867 r_sym_shift = 32;
8868 }
8869
d4730f92
BS
8870 erela = reldata->hdr->contents;
8871 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8872 {
8873 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8874 unsigned int j;
8875
8876 if (*rel_hash == NULL)
8877 continue;
8878
10bbbc1d
NC
8879 if ((*rel_hash)->indx == -2
8880 && info->gc_sections
8881 && ! info->gc_keep_exported)
8882 {
8883 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8884 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8885 abfd, sec,
8886 (*rel_hash)->root.root.string);
9793eb77 8887 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8888 abfd, sec);
10bbbc1d
NC
8889 bfd_set_error (bfd_error_invalid_operation);
8890 return FALSE;
8891 }
c152c796
AM
8892 BFD_ASSERT ((*rel_hash)->indx >= 0);
8893
8894 (*swap_in) (abfd, erela, irela);
8895 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8896 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8897 | (irela[j].r_info & r_type_mask));
8898 (*swap_out) (abfd, irela, erela);
8899 }
53df40a4 8900
9eaff861
AO
8901 if (bed->elf_backend_update_relocs)
8902 (*bed->elf_backend_update_relocs) (sec, reldata);
8903
0e287786 8904 if (sort && count != 0)
53df40a4 8905 {
0e287786
AM
8906 bfd_vma (*ext_r_off) (const void *);
8907 bfd_vma r_off;
8908 size_t elt_size;
8909 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8910 bfd_byte *buf = NULL;
28dbcedc
AM
8911
8912 if (bed->s->arch_size == 32)
8913 {
8914 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8915 ext_r_off = ext32l_r_offset;
28dbcedc 8916 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8917 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8918 else
8919 abort ();
8920 }
53df40a4 8921 else
28dbcedc 8922 {
53df40a4 8923#ifdef BFD_HOST_64_BIT
28dbcedc 8924 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8925 ext_r_off = ext64l_r_offset;
28dbcedc 8926 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8927 ext_r_off = ext64b_r_offset;
28dbcedc 8928 else
53df40a4 8929#endif
28dbcedc
AM
8930 abort ();
8931 }
0e287786 8932
bca6d0e3
AM
8933 /* Must use a stable sort here. A modified insertion sort,
8934 since the relocs are mostly sorted already. */
0e287786
AM
8935 elt_size = reldata->hdr->sh_entsize;
8936 base = reldata->hdr->contents;
8937 end = base + count * elt_size;
bca6d0e3 8938 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8939 abort ();
8940
8941 /* Ensure the first element is lowest. This acts as a sentinel,
8942 speeding the main loop below. */
8943 r_off = (*ext_r_off) (base);
8944 for (p = loc = base; (p += elt_size) < end; )
8945 {
8946 bfd_vma r_off2 = (*ext_r_off) (p);
8947 if (r_off > r_off2)
8948 {
8949 r_off = r_off2;
8950 loc = p;
8951 }
8952 }
8953 if (loc != base)
8954 {
8955 /* Don't just swap *base and *loc as that changes the order
8956 of the original base[0] and base[1] if they happen to
8957 have the same r_offset. */
bca6d0e3
AM
8958 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8959 memcpy (onebuf, loc, elt_size);
0e287786 8960 memmove (base + elt_size, base, loc - base);
bca6d0e3 8961 memcpy (base, onebuf, elt_size);
0e287786
AM
8962 }
8963
b29b8669 8964 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8965 {
8966 /* base to p is sorted, *p is next to insert. */
8967 r_off = (*ext_r_off) (p);
8968 /* Search the sorted region for location to insert. */
8969 loc = p - elt_size;
8970 while (r_off < (*ext_r_off) (loc))
8971 loc -= elt_size;
8972 loc += elt_size;
8973 if (loc != p)
8974 {
bca6d0e3
AM
8975 /* Chances are there is a run of relocs to insert here,
8976 from one of more input files. Files are not always
8977 linked in order due to the way elf_link_input_bfd is
8978 called. See pr17666. */
8979 size_t sortlen = p - loc;
8980 bfd_vma r_off2 = (*ext_r_off) (loc);
8981 size_t runlen = elt_size;
8982 size_t buf_size = 96 * 1024;
8983 while (p + runlen < end
8984 && (sortlen <= buf_size
8985 || runlen + elt_size <= buf_size)
8986 && r_off2 > (*ext_r_off) (p + runlen))
8987 runlen += elt_size;
8988 if (buf == NULL)
8989 {
8990 buf = bfd_malloc (buf_size);
8991 if (buf == NULL)
8992 return FALSE;
8993 }
8994 if (runlen < sortlen)
8995 {
8996 memcpy (buf, p, runlen);
8997 memmove (loc + runlen, loc, sortlen);
8998 memcpy (loc, buf, runlen);
8999 }
9000 else
9001 {
9002 memcpy (buf, loc, sortlen);
9003 memmove (loc, p, runlen);
9004 memcpy (loc + runlen, buf, sortlen);
9005 }
b29b8669 9006 p += runlen - elt_size;
0e287786
AM
9007 }
9008 }
9009 /* Hashes are no longer valid. */
28dbcedc
AM
9010 free (reldata->hashes);
9011 reldata->hashes = NULL;
bca6d0e3 9012 free (buf);
53df40a4 9013 }
bca6d0e3 9014 return TRUE;
c152c796
AM
9015}
9016
9017struct elf_link_sort_rela
9018{
9019 union {
9020 bfd_vma offset;
9021 bfd_vma sym_mask;
9022 } u;
9023 enum elf_reloc_type_class type;
9024 /* We use this as an array of size int_rels_per_ext_rel. */
9025 Elf_Internal_Rela rela[1];
9026};
9027
9028static int
9029elf_link_sort_cmp1 (const void *A, const void *B)
9030{
a50b1753
NC
9031 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9032 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9033 int relativea, relativeb;
9034
9035 relativea = a->type == reloc_class_relative;
9036 relativeb = b->type == reloc_class_relative;
9037
9038 if (relativea < relativeb)
9039 return 1;
9040 if (relativea > relativeb)
9041 return -1;
9042 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9043 return -1;
9044 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9045 return 1;
9046 if (a->rela->r_offset < b->rela->r_offset)
9047 return -1;
9048 if (a->rela->r_offset > b->rela->r_offset)
9049 return 1;
9050 return 0;
9051}
9052
9053static int
9054elf_link_sort_cmp2 (const void *A, const void *B)
9055{
a50b1753
NC
9056 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9057 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9058
7e612e98 9059 if (a->type < b->type)
c152c796 9060 return -1;
7e612e98 9061 if (a->type > b->type)
c152c796 9062 return 1;
7e612e98 9063 if (a->u.offset < b->u.offset)
c152c796 9064 return -1;
7e612e98 9065 if (a->u.offset > b->u.offset)
c152c796
AM
9066 return 1;
9067 if (a->rela->r_offset < b->rela->r_offset)
9068 return -1;
9069 if (a->rela->r_offset > b->rela->r_offset)
9070 return 1;
9071 return 0;
9072}
9073
9074static size_t
9075elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9076{
3410fea8 9077 asection *dynamic_relocs;
fc66a176
L
9078 asection *rela_dyn;
9079 asection *rel_dyn;
c152c796
AM
9080 bfd_size_type count, size;
9081 size_t i, ret, sort_elt, ext_size;
9082 bfd_byte *sort, *s_non_relative, *p;
9083 struct elf_link_sort_rela *sq;
9084 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9085 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9086 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9087 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9088 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9089 struct bfd_link_order *lo;
9090 bfd_vma r_sym_mask;
3410fea8 9091 bfd_boolean use_rela;
c152c796 9092
3410fea8
NC
9093 /* Find a dynamic reloc section. */
9094 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9095 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9096 if (rela_dyn != NULL && rela_dyn->size > 0
9097 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9098 {
3410fea8
NC
9099 bfd_boolean use_rela_initialised = FALSE;
9100
9101 /* This is just here to stop gcc from complaining.
c8e44c6d 9102 Its initialization checking code is not perfect. */
3410fea8
NC
9103 use_rela = TRUE;
9104
9105 /* Both sections are present. Examine the sizes
9106 of the indirect sections to help us choose. */
9107 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9108 if (lo->type == bfd_indirect_link_order)
9109 {
9110 asection *o = lo->u.indirect.section;
9111
9112 if ((o->size % bed->s->sizeof_rela) == 0)
9113 {
9114 if ((o->size % bed->s->sizeof_rel) == 0)
9115 /* Section size is divisible by both rel and rela sizes.
9116 It is of no help to us. */
9117 ;
9118 else
9119 {
9120 /* Section size is only divisible by rela. */
535b785f 9121 if (use_rela_initialised && !use_rela)
3410fea8 9122 {
9793eb77 9123 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9124 "they are in more than one size"),
9125 abfd);
3410fea8
NC
9126 bfd_set_error (bfd_error_invalid_operation);
9127 return 0;
9128 }
9129 else
9130 {
9131 use_rela = TRUE;
9132 use_rela_initialised = TRUE;
9133 }
9134 }
9135 }
9136 else if ((o->size % bed->s->sizeof_rel) == 0)
9137 {
9138 /* Section size is only divisible by rel. */
535b785f 9139 if (use_rela_initialised && use_rela)
3410fea8 9140 {
9793eb77 9141 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9142 "they are in more than one size"),
9143 abfd);
3410fea8
NC
9144 bfd_set_error (bfd_error_invalid_operation);
9145 return 0;
9146 }
9147 else
9148 {
9149 use_rela = FALSE;
9150 use_rela_initialised = TRUE;
9151 }
9152 }
9153 else
9154 {
c8e44c6d
AM
9155 /* The section size is not divisible by either -
9156 something is wrong. */
9793eb77 9157 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9158 "they are of an unknown size"), abfd);
3410fea8
NC
9159 bfd_set_error (bfd_error_invalid_operation);
9160 return 0;
9161 }
9162 }
9163
9164 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9165 if (lo->type == bfd_indirect_link_order)
9166 {
9167 asection *o = lo->u.indirect.section;
9168
9169 if ((o->size % bed->s->sizeof_rela) == 0)
9170 {
9171 if ((o->size % bed->s->sizeof_rel) == 0)
9172 /* Section size is divisible by both rel and rela sizes.
9173 It is of no help to us. */
9174 ;
9175 else
9176 {
9177 /* Section size is only divisible by rela. */
535b785f 9178 if (use_rela_initialised && !use_rela)
3410fea8 9179 {
9793eb77 9180 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9181 "they are in more than one size"),
9182 abfd);
3410fea8
NC
9183 bfd_set_error (bfd_error_invalid_operation);
9184 return 0;
9185 }
9186 else
9187 {
9188 use_rela = TRUE;
9189 use_rela_initialised = TRUE;
9190 }
9191 }
9192 }
9193 else if ((o->size % bed->s->sizeof_rel) == 0)
9194 {
9195 /* Section size is only divisible by rel. */
535b785f 9196 if (use_rela_initialised && use_rela)
3410fea8 9197 {
9793eb77 9198 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9199 "they are in more than one size"),
9200 abfd);
3410fea8
NC
9201 bfd_set_error (bfd_error_invalid_operation);
9202 return 0;
9203 }
9204 else
9205 {
9206 use_rela = FALSE;
9207 use_rela_initialised = TRUE;
9208 }
9209 }
9210 else
9211 {
c8e44c6d
AM
9212 /* The section size is not divisible by either -
9213 something is wrong. */
9793eb77 9214 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9215 "they are of an unknown size"), abfd);
3410fea8
NC
9216 bfd_set_error (bfd_error_invalid_operation);
9217 return 0;
9218 }
9219 }
9220
9221 if (! use_rela_initialised)
9222 /* Make a guess. */
9223 use_rela = TRUE;
c152c796 9224 }
fc66a176
L
9225 else if (rela_dyn != NULL && rela_dyn->size > 0)
9226 use_rela = TRUE;
9227 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9228 use_rela = FALSE;
c152c796 9229 else
fc66a176 9230 return 0;
3410fea8
NC
9231
9232 if (use_rela)
c152c796 9233 {
3410fea8 9234 dynamic_relocs = rela_dyn;
c152c796
AM
9235 ext_size = bed->s->sizeof_rela;
9236 swap_in = bed->s->swap_reloca_in;
9237 swap_out = bed->s->swap_reloca_out;
9238 }
3410fea8
NC
9239 else
9240 {
9241 dynamic_relocs = rel_dyn;
9242 ext_size = bed->s->sizeof_rel;
9243 swap_in = bed->s->swap_reloc_in;
9244 swap_out = bed->s->swap_reloc_out;
9245 }
c152c796
AM
9246
9247 size = 0;
3410fea8 9248 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9249 if (lo->type == bfd_indirect_link_order)
3410fea8 9250 size += lo->u.indirect.section->size;
c152c796 9251
3410fea8 9252 if (size != dynamic_relocs->size)
c152c796
AM
9253 return 0;
9254
9255 sort_elt = (sizeof (struct elf_link_sort_rela)
9256 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9257
9258 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9259 if (count == 0)
9260 return 0;
a50b1753 9261 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9262
c152c796
AM
9263 if (sort == NULL)
9264 {
9265 (*info->callbacks->warning)
9793eb77 9266 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9267 return 0;
9268 }
9269
9270 if (bed->s->arch_size == 32)
9271 r_sym_mask = ~(bfd_vma) 0xff;
9272 else
9273 r_sym_mask = ~(bfd_vma) 0xffffffff;
9274
3410fea8 9275 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9276 if (lo->type == bfd_indirect_link_order)
9277 {
9278 bfd_byte *erel, *erelend;
9279 asection *o = lo->u.indirect.section;
9280
1da212d6
AM
9281 if (o->contents == NULL && o->size != 0)
9282 {
9283 /* This is a reloc section that is being handled as a normal
9284 section. See bfd_section_from_shdr. We can't combine
9285 relocs in this case. */
9286 free (sort);
9287 return 0;
9288 }
c152c796 9289 erel = o->contents;
eea6121a 9290 erelend = o->contents + o->size;
c8e44c6d 9291 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9292
c152c796
AM
9293 while (erel < erelend)
9294 {
9295 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9296
c152c796 9297 (*swap_in) (abfd, erel, s->rela);
7e612e98 9298 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9299 s->u.sym_mask = r_sym_mask;
9300 p += sort_elt;
9301 erel += ext_size;
9302 }
9303 }
9304
9305 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9306
9307 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9308 {
9309 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9310 if (s->type != reloc_class_relative)
9311 break;
9312 }
9313 ret = i;
9314 s_non_relative = p;
9315
9316 sq = (struct elf_link_sort_rela *) s_non_relative;
9317 for (; i < count; i++, p += sort_elt)
9318 {
9319 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9320 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9321 sq = sp;
9322 sp->u.offset = sq->rela->r_offset;
9323 }
9324
9325 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9326
c8e44c6d
AM
9327 struct elf_link_hash_table *htab = elf_hash_table (info);
9328 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9329 {
9330 /* We have plt relocs in .rela.dyn. */
9331 sq = (struct elf_link_sort_rela *) sort;
9332 for (i = 0; i < count; i++)
9333 if (sq[count - i - 1].type != reloc_class_plt)
9334 break;
9335 if (i != 0 && htab->srelplt->size == i * ext_size)
9336 {
9337 struct bfd_link_order **plo;
9338 /* Put srelplt link_order last. This is so the output_offset
9339 set in the next loop is correct for DT_JMPREL. */
9340 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9341 if ((*plo)->type == bfd_indirect_link_order
9342 && (*plo)->u.indirect.section == htab->srelplt)
9343 {
9344 lo = *plo;
9345 *plo = lo->next;
9346 }
9347 else
9348 plo = &(*plo)->next;
9349 *plo = lo;
9350 lo->next = NULL;
9351 dynamic_relocs->map_tail.link_order = lo;
9352 }
9353 }
9354
9355 p = sort;
3410fea8 9356 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9357 if (lo->type == bfd_indirect_link_order)
9358 {
9359 bfd_byte *erel, *erelend;
9360 asection *o = lo->u.indirect.section;
9361
9362 erel = o->contents;
eea6121a 9363 erelend = o->contents + o->size;
c8e44c6d 9364 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9365 while (erel < erelend)
9366 {
9367 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9368 (*swap_out) (abfd, s->rela, erel);
9369 p += sort_elt;
9370 erel += ext_size;
9371 }
9372 }
9373
9374 free (sort);
3410fea8 9375 *psec = dynamic_relocs;
c152c796
AM
9376 return ret;
9377}
9378
ef10c3ac 9379/* Add a symbol to the output symbol string table. */
c152c796 9380
6e0b88f1 9381static int
ef10c3ac
L
9382elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9383 const char *name,
9384 Elf_Internal_Sym *elfsym,
9385 asection *input_sec,
9386 struct elf_link_hash_entry *h)
c152c796 9387{
6e0b88f1 9388 int (*output_symbol_hook)
c152c796
AM
9389 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9390 struct elf_link_hash_entry *);
ef10c3ac 9391 struct elf_link_hash_table *hash_table;
c152c796 9392 const struct elf_backend_data *bed;
ef10c3ac 9393 bfd_size_type strtabsize;
c152c796 9394
8539e4e8
AM
9395 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9396
8b127cbc 9397 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9398 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9399 if (output_symbol_hook != NULL)
9400 {
8b127cbc 9401 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9402 if (ret != 1)
9403 return ret;
c152c796
AM
9404 }
9405
ef10c3ac
L
9406 if (name == NULL
9407 || *name == '\0'
9408 || (input_sec->flags & SEC_EXCLUDE))
9409 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9410 else
9411 {
ef10c3ac
L
9412 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9413 to get the final offset for st_name. */
9414 elfsym->st_name
9415 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9416 name, FALSE);
c152c796 9417 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9418 return 0;
c152c796
AM
9419 }
9420
ef10c3ac
L
9421 hash_table = elf_hash_table (flinfo->info);
9422 strtabsize = hash_table->strtabsize;
9423 if (strtabsize <= hash_table->strtabcount)
c152c796 9424 {
ef10c3ac
L
9425 strtabsize += strtabsize;
9426 hash_table->strtabsize = strtabsize;
9427 strtabsize *= sizeof (*hash_table->strtab);
9428 hash_table->strtab
9429 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9430 strtabsize);
9431 if (hash_table->strtab == NULL)
6e0b88f1 9432 return 0;
c152c796 9433 }
ef10c3ac
L
9434 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9435 hash_table->strtab[hash_table->strtabcount].dest_index
9436 = hash_table->strtabcount;
9437 hash_table->strtab[hash_table->strtabcount].destshndx_index
9438 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9439
9440 bfd_get_symcount (flinfo->output_bfd) += 1;
9441 hash_table->strtabcount += 1;
9442
9443 return 1;
9444}
9445
9446/* Swap symbols out to the symbol table and flush the output symbols to
9447 the file. */
9448
9449static bfd_boolean
9450elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9451{
9452 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9453 bfd_size_type amt;
9454 size_t i;
ef10c3ac
L
9455 const struct elf_backend_data *bed;
9456 bfd_byte *symbuf;
9457 Elf_Internal_Shdr *hdr;
9458 file_ptr pos;
9459 bfd_boolean ret;
9460
9461 if (!hash_table->strtabcount)
9462 return TRUE;
9463
9464 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9465
9466 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9467
ef10c3ac
L
9468 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9469 symbuf = (bfd_byte *) bfd_malloc (amt);
9470 if (symbuf == NULL)
9471 return FALSE;
1b786873 9472
ef10c3ac 9473 if (flinfo->symshndxbuf)
c152c796 9474 {
ef53be89
AM
9475 amt = sizeof (Elf_External_Sym_Shndx);
9476 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9477 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9478 if (flinfo->symshndxbuf == NULL)
c152c796 9479 {
ef10c3ac
L
9480 free (symbuf);
9481 return FALSE;
c152c796 9482 }
c152c796
AM
9483 }
9484
ef10c3ac
L
9485 for (i = 0; i < hash_table->strtabcount; i++)
9486 {
9487 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9488 if (elfsym->sym.st_name == (unsigned long) -1)
9489 elfsym->sym.st_name = 0;
9490 else
9491 elfsym->sym.st_name
9492 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9493 elfsym->sym.st_name);
9494 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9495 ((bfd_byte *) symbuf
9496 + (elfsym->dest_index
9497 * bed->s->sizeof_sym)),
9498 (flinfo->symshndxbuf
9499 + elfsym->destshndx_index));
9500 }
9501
9502 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9503 pos = hdr->sh_offset + hdr->sh_size;
9504 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9505 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9506 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9507 {
9508 hdr->sh_size += amt;
9509 ret = TRUE;
9510 }
9511 else
9512 ret = FALSE;
c152c796 9513
ef10c3ac
L
9514 free (symbuf);
9515
9516 free (hash_table->strtab);
9517 hash_table->strtab = NULL;
9518
9519 return ret;
c152c796
AM
9520}
9521
c0d5a53d
L
9522/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9523
9524static bfd_boolean
9525check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9526{
4fbb74a6
AM
9527 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9528 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9529 {
9530 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9531 beyond 64k. */
4eca0228 9532 _bfd_error_handler
695344c0 9533 /* xgettext:c-format */
9793eb77 9534 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9535 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9536 bfd_set_error (bfd_error_nonrepresentable_section);
9537 return FALSE;
9538 }
9539 return TRUE;
9540}
9541
c152c796
AM
9542/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9543 allowing an unsatisfied unversioned symbol in the DSO to match a
9544 versioned symbol that would normally require an explicit version.
9545 We also handle the case that a DSO references a hidden symbol
9546 which may be satisfied by a versioned symbol in another DSO. */
9547
9548static bfd_boolean
9549elf_link_check_versioned_symbol (struct bfd_link_info *info,
9550 const struct elf_backend_data *bed,
9551 struct elf_link_hash_entry *h)
9552{
9553 bfd *abfd;
9554 struct elf_link_loaded_list *loaded;
9555
9556 if (!is_elf_hash_table (info->hash))
9557 return FALSE;
9558
90c984fc
L
9559 /* Check indirect symbol. */
9560 while (h->root.type == bfd_link_hash_indirect)
9561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9562
c152c796
AM
9563 switch (h->root.type)
9564 {
9565 default:
9566 abfd = NULL;
9567 break;
9568
9569 case bfd_link_hash_undefined:
9570 case bfd_link_hash_undefweak:
9571 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9572 if (abfd == NULL
9573 || (abfd->flags & DYNAMIC) == 0
e56f61be 9574 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9575 return FALSE;
9576 break;
9577
9578 case bfd_link_hash_defined:
9579 case bfd_link_hash_defweak:
9580 abfd = h->root.u.def.section->owner;
9581 break;
9582
9583 case bfd_link_hash_common:
9584 abfd = h->root.u.c.p->section->owner;
9585 break;
9586 }
9587 BFD_ASSERT (abfd != NULL);
9588
9589 for (loaded = elf_hash_table (info)->loaded;
9590 loaded != NULL;
9591 loaded = loaded->next)
9592 {
9593 bfd *input;
9594 Elf_Internal_Shdr *hdr;
ef53be89
AM
9595 size_t symcount;
9596 size_t extsymcount;
9597 size_t extsymoff;
c152c796
AM
9598 Elf_Internal_Shdr *versymhdr;
9599 Elf_Internal_Sym *isym;
9600 Elf_Internal_Sym *isymend;
9601 Elf_Internal_Sym *isymbuf;
9602 Elf_External_Versym *ever;
9603 Elf_External_Versym *extversym;
9604
9605 input = loaded->abfd;
9606
9607 /* We check each DSO for a possible hidden versioned definition. */
9608 if (input == abfd
9609 || (input->flags & DYNAMIC) == 0
9610 || elf_dynversym (input) == 0)
9611 continue;
9612
9613 hdr = &elf_tdata (input)->dynsymtab_hdr;
9614
9615 symcount = hdr->sh_size / bed->s->sizeof_sym;
9616 if (elf_bad_symtab (input))
9617 {
9618 extsymcount = symcount;
9619 extsymoff = 0;
9620 }
9621 else
9622 {
9623 extsymcount = symcount - hdr->sh_info;
9624 extsymoff = hdr->sh_info;
9625 }
9626
9627 if (extsymcount == 0)
9628 continue;
9629
9630 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9631 NULL, NULL, NULL);
9632 if (isymbuf == NULL)
9633 return FALSE;
9634
9635 /* Read in any version definitions. */
9636 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9637 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9638 if (extversym == NULL)
9639 goto error_ret;
9640
9641 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9642 || (bfd_bread (extversym, versymhdr->sh_size, input)
9643 != versymhdr->sh_size))
9644 {
9645 free (extversym);
9646 error_ret:
9647 free (isymbuf);
9648 return FALSE;
9649 }
9650
9651 ever = extversym + extsymoff;
9652 isymend = isymbuf + extsymcount;
9653 for (isym = isymbuf; isym < isymend; isym++, ever++)
9654 {
9655 const char *name;
9656 Elf_Internal_Versym iver;
9657 unsigned short version_index;
9658
9659 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9660 || isym->st_shndx == SHN_UNDEF)
9661 continue;
9662
9663 name = bfd_elf_string_from_elf_section (input,
9664 hdr->sh_link,
9665 isym->st_name);
9666 if (strcmp (name, h->root.root.string) != 0)
9667 continue;
9668
9669 _bfd_elf_swap_versym_in (input, ever, &iver);
9670
d023c380
L
9671 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9672 && !(h->def_regular
9673 && h->forced_local))
c152c796
AM
9674 {
9675 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9676 have provided a definition for the undefined sym unless
9677 it is defined in a non-shared object and forced local.
9678 */
c152c796
AM
9679 abort ();
9680 }
9681
9682 version_index = iver.vs_vers & VERSYM_VERSION;
9683 if (version_index == 1 || version_index == 2)
9684 {
9685 /* This is the base or first version. We can use it. */
9686 free (extversym);
9687 free (isymbuf);
9688 return TRUE;
9689 }
9690 }
9691
9692 free (extversym);
9693 free (isymbuf);
9694 }
9695
9696 return FALSE;
9697}
9698
b8871f35
L
9699/* Convert ELF common symbol TYPE. */
9700
9701static int
9702elf_link_convert_common_type (struct bfd_link_info *info, int type)
9703{
9704 /* Commom symbol can only appear in relocatable link. */
9705 if (!bfd_link_relocatable (info))
9706 abort ();
9707 switch (info->elf_stt_common)
9708 {
9709 case unchanged:
9710 break;
9711 case elf_stt_common:
9712 type = STT_COMMON;
9713 break;
9714 case no_elf_stt_common:
9715 type = STT_OBJECT;
9716 break;
9717 }
9718 return type;
9719}
9720
c152c796
AM
9721/* Add an external symbol to the symbol table. This is called from
9722 the hash table traversal routine. When generating a shared object,
9723 we go through the symbol table twice. The first time we output
9724 anything that might have been forced to local scope in a version
9725 script. The second time we output the symbols that are still
9726 global symbols. */
9727
9728static bfd_boolean
7686d77d 9729elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9730{
7686d77d 9731 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9732 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9733 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9734 bfd_boolean strip;
9735 Elf_Internal_Sym sym;
9736 asection *input_sec;
9737 const struct elf_backend_data *bed;
6e0b88f1
AM
9738 long indx;
9739 int ret;
b8871f35 9740 unsigned int type;
c152c796
AM
9741
9742 if (h->root.type == bfd_link_hash_warning)
9743 {
9744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9745 if (h->root.type == bfd_link_hash_new)
9746 return TRUE;
9747 }
9748
9749 /* Decide whether to output this symbol in this pass. */
9750 if (eoinfo->localsyms)
9751 {
4deb8f71 9752 if (!h->forced_local)
c152c796
AM
9753 return TRUE;
9754 }
9755 else
9756 {
4deb8f71 9757 if (h->forced_local)
c152c796
AM
9758 return TRUE;
9759 }
9760
8b127cbc 9761 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9762
12ac1cf5 9763 if (h->root.type == bfd_link_hash_undefined)
c152c796 9764 {
12ac1cf5
NC
9765 /* If we have an undefined symbol reference here then it must have
9766 come from a shared library that is being linked in. (Undefined
98da7939
L
9767 references in regular files have already been handled unless
9768 they are in unreferenced sections which are removed by garbage
9769 collection). */
12ac1cf5
NC
9770 bfd_boolean ignore_undef = FALSE;
9771
9772 /* Some symbols may be special in that the fact that they're
9773 undefined can be safely ignored - let backend determine that. */
9774 if (bed->elf_backend_ignore_undef_symbol)
9775 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9776
9777 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9778 if (!ignore_undef
c54f1524 9779 && h->ref_dynamic_nonweak
8b127cbc
AM
9780 && (!h->ref_regular || flinfo->info->gc_sections)
9781 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9782 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9783 (*flinfo->info->callbacks->undefined_symbol)
9784 (flinfo->info, h->root.root.string,
9785 h->ref_regular ? NULL : h->root.u.undef.abfd,
9786 NULL, 0,
9787 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9788
9789 /* Strip a global symbol defined in a discarded section. */
9790 if (h->indx == -3)
9791 return TRUE;
c152c796
AM
9792 }
9793
9794 /* We should also warn if a forced local symbol is referenced from
9795 shared libraries. */
0e1862bb 9796 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9797 && h->forced_local
9798 && h->ref_dynamic
371a5866 9799 && h->def_regular
f5385ebf 9800 && !h->dynamic_def
ee659f1f 9801 && h->ref_dynamic_nonweak
8b127cbc 9802 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9803 {
17d078c5
AM
9804 bfd *def_bfd;
9805 const char *msg;
90c984fc
L
9806 struct elf_link_hash_entry *hi = h;
9807
9808 /* Check indirect symbol. */
9809 while (hi->root.type == bfd_link_hash_indirect)
9810 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9811
9812 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9813 /* xgettext:c-format */
871b3ab2 9814 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9815 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9816 /* xgettext:c-format */
871b3ab2 9817 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9818 else
695344c0 9819 /* xgettext:c-format */
871b3ab2 9820 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9821 def_bfd = flinfo->output_bfd;
90c984fc
L
9822 if (hi->root.u.def.section != bfd_abs_section_ptr)
9823 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9824 _bfd_error_handler (msg, flinfo->output_bfd,
9825 h->root.root.string, def_bfd);
17d078c5 9826 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9827 eoinfo->failed = TRUE;
9828 return FALSE;
9829 }
9830
9831 /* We don't want to output symbols that have never been mentioned by
9832 a regular file, or that we have been told to strip. However, if
9833 h->indx is set to -2, the symbol is used by a reloc and we must
9834 output it. */
d983c8c5 9835 strip = FALSE;
c152c796 9836 if (h->indx == -2)
d983c8c5 9837 ;
f5385ebf 9838 else if ((h->def_dynamic
77cfaee6
AM
9839 || h->ref_dynamic
9840 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9841 && !h->def_regular
9842 && !h->ref_regular)
c152c796 9843 strip = TRUE;
8b127cbc 9844 else if (flinfo->info->strip == strip_all)
c152c796 9845 strip = TRUE;
8b127cbc
AM
9846 else if (flinfo->info->strip == strip_some
9847 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9848 h->root.root.string, FALSE, FALSE) == NULL)
9849 strip = TRUE;
d56d55e7
AM
9850 else if ((h->root.type == bfd_link_hash_defined
9851 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9852 && ((flinfo->info->strip_discarded
dbaa2011 9853 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9854 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9855 && h->root.u.def.section->owner != NULL
d56d55e7 9856 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9857 strip = TRUE;
9e2278f5
AM
9858 else if ((h->root.type == bfd_link_hash_undefined
9859 || h->root.type == bfd_link_hash_undefweak)
9860 && h->root.u.undef.abfd != NULL
9861 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9862 strip = TRUE;
c152c796 9863
b8871f35
L
9864 type = h->type;
9865
c152c796 9866 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9867 nothing else to do. However, if it is a forced local symbol or
9868 an ifunc symbol we need to give the backend finish_dynamic_symbol
9869 function a chance to make it dynamic. */
c152c796
AM
9870 if (strip
9871 && h->dynindx == -1
b8871f35 9872 && type != STT_GNU_IFUNC
f5385ebf 9873 && !h->forced_local)
c152c796
AM
9874 return TRUE;
9875
9876 sym.st_value = 0;
9877 sym.st_size = h->size;
9878 sym.st_other = h->other;
c152c796
AM
9879 switch (h->root.type)
9880 {
9881 default:
9882 case bfd_link_hash_new:
9883 case bfd_link_hash_warning:
9884 abort ();
9885 return FALSE;
9886
9887 case bfd_link_hash_undefined:
9888 case bfd_link_hash_undefweak:
9889 input_sec = bfd_und_section_ptr;
9890 sym.st_shndx = SHN_UNDEF;
9891 break;
9892
9893 case bfd_link_hash_defined:
9894 case bfd_link_hash_defweak:
9895 {
9896 input_sec = h->root.u.def.section;
9897 if (input_sec->output_section != NULL)
9898 {
9899 sym.st_shndx =
8b127cbc 9900 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9901 input_sec->output_section);
9902 if (sym.st_shndx == SHN_BAD)
9903 {
4eca0228 9904 _bfd_error_handler
695344c0 9905 /* xgettext:c-format */
871b3ab2 9906 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9907 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9908 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9909 eoinfo->failed = TRUE;
9910 return FALSE;
9911 }
9912
9913 /* ELF symbols in relocatable files are section relative,
9914 but in nonrelocatable files they are virtual
9915 addresses. */
9916 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9917 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9918 {
9919 sym.st_value += input_sec->output_section->vma;
9920 if (h->type == STT_TLS)
9921 {
8b127cbc 9922 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9923 if (tls_sec != NULL)
9924 sym.st_value -= tls_sec->vma;
c152c796
AM
9925 }
9926 }
9927 }
9928 else
9929 {
9930 BFD_ASSERT (input_sec->owner == NULL
9931 || (input_sec->owner->flags & DYNAMIC) != 0);
9932 sym.st_shndx = SHN_UNDEF;
9933 input_sec = bfd_und_section_ptr;
9934 }
9935 }
9936 break;
9937
9938 case bfd_link_hash_common:
9939 input_sec = h->root.u.c.p->section;
a4d8e49b 9940 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9941 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9942 break;
9943
9944 case bfd_link_hash_indirect:
9945 /* These symbols are created by symbol versioning. They point
9946 to the decorated version of the name. For example, if the
9947 symbol foo@@GNU_1.2 is the default, which should be used when
9948 foo is used with no version, then we add an indirect symbol
9949 foo which points to foo@@GNU_1.2. We ignore these symbols,
9950 since the indirected symbol is already in the hash table. */
9951 return TRUE;
9952 }
9953
b8871f35
L
9954 if (type == STT_COMMON || type == STT_OBJECT)
9955 switch (h->root.type)
9956 {
9957 case bfd_link_hash_common:
9958 type = elf_link_convert_common_type (flinfo->info, type);
9959 break;
9960 case bfd_link_hash_defined:
9961 case bfd_link_hash_defweak:
9962 if (bed->common_definition (&sym))
9963 type = elf_link_convert_common_type (flinfo->info, type);
9964 else
9965 type = STT_OBJECT;
9966 break;
9967 case bfd_link_hash_undefined:
9968 case bfd_link_hash_undefweak:
9969 break;
9970 default:
9971 abort ();
9972 }
9973
4deb8f71 9974 if (h->forced_local)
b8871f35
L
9975 {
9976 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9977 /* Turn off visibility on local symbol. */
9978 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9979 }
9980 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9981 else if (h->unique_global && h->def_regular)
9982 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9983 else if (h->root.type == bfd_link_hash_undefweak
9984 || h->root.type == bfd_link_hash_defweak)
9985 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9986 else
9987 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9988 sym.st_target_internal = h->target_internal;
9989
c152c796
AM
9990 /* Give the processor backend a chance to tweak the symbol value,
9991 and also to finish up anything that needs to be done for this
9992 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9993 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9994 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9995 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9996 && h->def_regular
0e1862bb 9997 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9998 || ((h->dynindx != -1
9999 || h->forced_local)
0e1862bb 10000 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10001 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10002 || h->root.type != bfd_link_hash_undefweak))
10003 || !h->forced_local)
8b127cbc 10004 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10005 {
10006 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10007 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10008 {
10009 eoinfo->failed = TRUE;
10010 return FALSE;
10011 }
10012 }
10013
10014 /* If we are marking the symbol as undefined, and there are no
10015 non-weak references to this symbol from a regular object, then
10016 mark the symbol as weak undefined; if there are non-weak
10017 references, mark the symbol as strong. We can't do this earlier,
10018 because it might not be marked as undefined until the
10019 finish_dynamic_symbol routine gets through with it. */
10020 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10021 && h->ref_regular
c152c796
AM
10022 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10023 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10024 {
10025 int bindtype;
b8871f35 10026 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10027
10028 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10029 if (type == STT_GNU_IFUNC)
10030 type = STT_FUNC;
c152c796 10031
f5385ebf 10032 if (h->ref_regular_nonweak)
c152c796
AM
10033 bindtype = STB_GLOBAL;
10034 else
10035 bindtype = STB_WEAK;
2955ec4c 10036 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10037 }
10038
bda987c2
CD
10039 /* If this is a symbol defined in a dynamic library, don't use the
10040 symbol size from the dynamic library. Relinking an executable
10041 against a new library may introduce gratuitous changes in the
10042 executable's symbols if we keep the size. */
10043 if (sym.st_shndx == SHN_UNDEF
10044 && !h->def_regular
10045 && h->def_dynamic)
10046 sym.st_size = 0;
10047
c152c796
AM
10048 /* If a non-weak symbol with non-default visibility is not defined
10049 locally, it is a fatal error. */
0e1862bb 10050 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10051 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10052 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10053 && h->root.type == bfd_link_hash_undefined
f5385ebf 10054 && !h->def_regular)
c152c796 10055 {
17d078c5
AM
10056 const char *msg;
10057
10058 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10059 /* xgettext:c-format */
871b3ab2 10060 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10061 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10062 /* xgettext:c-format */
871b3ab2 10063 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10064 else
695344c0 10065 /* xgettext:c-format */
871b3ab2 10066 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10067 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10068 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10069 eoinfo->failed = TRUE;
10070 return FALSE;
10071 }
10072
10073 /* If this symbol should be put in the .dynsym section, then put it
10074 there now. We already know the symbol index. We also fill in
10075 the entry in the .hash section. */
1c2649ed
EB
10076 if (h->dynindx != -1
10077 && elf_hash_table (flinfo->info)->dynamic_sections_created
10078 && elf_hash_table (flinfo->info)->dynsym != NULL
10079 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10080 {
c152c796
AM
10081 bfd_byte *esym;
10082
90c984fc
L
10083 /* Since there is no version information in the dynamic string,
10084 if there is no version info in symbol version section, we will
1659f720 10085 have a run-time problem if not linking executable, referenced
4deb8f71 10086 by shared library, or not bound locally. */
1659f720 10087 if (h->verinfo.verdef == NULL
0e1862bb 10088 && (!bfd_link_executable (flinfo->info)
1659f720
L
10089 || h->ref_dynamic
10090 || !h->def_regular))
90c984fc
L
10091 {
10092 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10093
10094 if (p && p [1] != '\0')
10095 {
4eca0228 10096 _bfd_error_handler
695344c0 10097 /* xgettext:c-format */
9793eb77 10098 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10099 flinfo->output_bfd, h->root.root.string);
10100 eoinfo->failed = TRUE;
10101 return FALSE;
10102 }
10103 }
10104
c152c796 10105 sym.st_name = h->dynstr_index;
cae1fbbb
L
10106 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10107 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10108 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10109 {
10110 eoinfo->failed = TRUE;
10111 return FALSE;
10112 }
8b127cbc 10113 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10114
8b127cbc 10115 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10116 {
10117 size_t hash_entry_size;
10118 bfd_byte *bucketpos;
10119 bfd_vma chain;
41198d0c
L
10120 size_t bucketcount;
10121 size_t bucket;
10122
8b127cbc 10123 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10124 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10125
10126 hash_entry_size
8b127cbc
AM
10127 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10128 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10129 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10130 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10131 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10132 bucketpos);
10133 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10134 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10135 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10136 }
c152c796 10137
8b127cbc 10138 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10139 {
10140 Elf_Internal_Versym iversym;
10141 Elf_External_Versym *eversym;
10142
f5385ebf 10143 if (!h->def_regular)
c152c796 10144 {
7b20f099
AM
10145 if (h->verinfo.verdef == NULL
10146 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10147 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10148 iversym.vs_vers = 0;
10149 else
10150 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10151 }
10152 else
10153 {
10154 if (h->verinfo.vertree == NULL)
10155 iversym.vs_vers = 1;
10156 else
10157 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10158 if (flinfo->info->create_default_symver)
3e3b46e5 10159 iversym.vs_vers++;
c152c796
AM
10160 }
10161
422f1182 10162 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10163 defined locally. */
422f1182 10164 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10165 iversym.vs_vers |= VERSYM_HIDDEN;
10166
8b127cbc 10167 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10168 eversym += h->dynindx;
8b127cbc 10169 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10170 }
10171 }
10172
d983c8c5
AM
10173 /* If the symbol is undefined, and we didn't output it to .dynsym,
10174 strip it from .symtab too. Obviously we can't do this for
10175 relocatable output or when needed for --emit-relocs. */
10176 else if (input_sec == bfd_und_section_ptr
10177 && h->indx != -2
66cae560
NC
10178 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10179 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10180 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10181 return TRUE;
66cae560 10182
d983c8c5
AM
10183 /* Also strip others that we couldn't earlier due to dynamic symbol
10184 processing. */
10185 if (strip)
10186 return TRUE;
10187 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10188 return TRUE;
10189
2ec55de3
AM
10190 /* Output a FILE symbol so that following locals are not associated
10191 with the wrong input file. We need one for forced local symbols
10192 if we've seen more than one FILE symbol or when we have exactly
10193 one FILE symbol but global symbols are present in a file other
10194 than the one with the FILE symbol. We also need one if linker
10195 defined symbols are present. In practice these conditions are
10196 always met, so just emit the FILE symbol unconditionally. */
10197 if (eoinfo->localsyms
10198 && !eoinfo->file_sym_done
10199 && eoinfo->flinfo->filesym_count != 0)
10200 {
10201 Elf_Internal_Sym fsym;
10202
10203 memset (&fsym, 0, sizeof (fsym));
10204 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10205 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10206 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10207 bfd_und_section_ptr, NULL))
2ec55de3
AM
10208 return FALSE;
10209
10210 eoinfo->file_sym_done = TRUE;
10211 }
10212
8b127cbc 10213 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10214 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10215 input_sec, h);
6e0b88f1 10216 if (ret == 0)
c152c796
AM
10217 {
10218 eoinfo->failed = TRUE;
10219 return FALSE;
10220 }
6e0b88f1
AM
10221 else if (ret == 1)
10222 h->indx = indx;
10223 else if (h->indx == -2)
10224 abort();
c152c796
AM
10225
10226 return TRUE;
10227}
10228
cdd3575c
AM
10229/* Return TRUE if special handling is done for relocs in SEC against
10230 symbols defined in discarded sections. */
10231
c152c796
AM
10232static bfd_boolean
10233elf_section_ignore_discarded_relocs (asection *sec)
10234{
10235 const struct elf_backend_data *bed;
10236
cdd3575c
AM
10237 switch (sec->sec_info_type)
10238 {
dbaa2011
AM
10239 case SEC_INFO_TYPE_STABS:
10240 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10241 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10242 return TRUE;
10243 default:
10244 break;
10245 }
c152c796
AM
10246
10247 bed = get_elf_backend_data (sec->owner);
10248 if (bed->elf_backend_ignore_discarded_relocs != NULL
10249 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10250 return TRUE;
10251
10252 return FALSE;
10253}
10254
9e66c942
AM
10255/* Return a mask saying how ld should treat relocations in SEC against
10256 symbols defined in discarded sections. If this function returns
10257 COMPLAIN set, ld will issue a warning message. If this function
10258 returns PRETEND set, and the discarded section was link-once and the
10259 same size as the kept link-once section, ld will pretend that the
10260 symbol was actually defined in the kept section. Otherwise ld will
10261 zero the reloc (at least that is the intent, but some cooperation by
10262 the target dependent code is needed, particularly for REL targets). */
10263
8a696751
AM
10264unsigned int
10265_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10266{
9e66c942 10267 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10268 return PRETEND;
cdd3575c
AM
10269
10270 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10271 return 0;
cdd3575c
AM
10272
10273 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10274 return 0;
cdd3575c 10275
9e66c942 10276 return COMPLAIN | PRETEND;
cdd3575c
AM
10277}
10278
3d7f7666
L
10279/* Find a match between a section and a member of a section group. */
10280
10281static asection *
c0f00686
L
10282match_group_member (asection *sec, asection *group,
10283 struct bfd_link_info *info)
3d7f7666
L
10284{
10285 asection *first = elf_next_in_group (group);
10286 asection *s = first;
10287
10288 while (s != NULL)
10289 {
c0f00686 10290 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10291 return s;
10292
83180ade 10293 s = elf_next_in_group (s);
3d7f7666
L
10294 if (s == first)
10295 break;
10296 }
10297
10298 return NULL;
10299}
10300
01b3c8ab 10301/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10302 to replace it. Return the replacement if it is OK. Otherwise return
10303 NULL. */
01b3c8ab
L
10304
10305asection *
c0f00686 10306_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10307{
10308 asection *kept;
10309
10310 kept = sec->kept_section;
10311 if (kept != NULL)
10312 {
c2370991 10313 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10314 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10315 if (kept != NULL
10316 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10317 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10318 kept = NULL;
c2370991 10319 sec->kept_section = kept;
01b3c8ab
L
10320 }
10321 return kept;
10322}
10323
c152c796
AM
10324/* Link an input file into the linker output file. This function
10325 handles all the sections and relocations of the input file at once.
10326 This is so that we only have to read the local symbols once, and
10327 don't have to keep them in memory. */
10328
10329static bfd_boolean
8b127cbc 10330elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10331{
ece5ef60 10332 int (*relocate_section)
c152c796
AM
10333 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10334 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10335 bfd *output_bfd;
10336 Elf_Internal_Shdr *symtab_hdr;
10337 size_t locsymcount;
10338 size_t extsymoff;
10339 Elf_Internal_Sym *isymbuf;
10340 Elf_Internal_Sym *isym;
10341 Elf_Internal_Sym *isymend;
10342 long *pindex;
10343 asection **ppsection;
10344 asection *o;
10345 const struct elf_backend_data *bed;
c152c796 10346 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10347 bfd_size_type address_size;
10348 bfd_vma r_type_mask;
10349 int r_sym_shift;
ffbc01cc 10350 bfd_boolean have_file_sym = FALSE;
c152c796 10351
8b127cbc 10352 output_bfd = flinfo->output_bfd;
c152c796
AM
10353 bed = get_elf_backend_data (output_bfd);
10354 relocate_section = bed->elf_backend_relocate_section;
10355
10356 /* If this is a dynamic object, we don't want to do anything here:
10357 we don't want the local symbols, and we don't want the section
10358 contents. */
10359 if ((input_bfd->flags & DYNAMIC) != 0)
10360 return TRUE;
10361
c152c796
AM
10362 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10363 if (elf_bad_symtab (input_bfd))
10364 {
10365 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10366 extsymoff = 0;
10367 }
10368 else
10369 {
10370 locsymcount = symtab_hdr->sh_info;
10371 extsymoff = symtab_hdr->sh_info;
10372 }
10373
10374 /* Read the local symbols. */
10375 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10376 if (isymbuf == NULL && locsymcount != 0)
10377 {
10378 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10379 flinfo->internal_syms,
10380 flinfo->external_syms,
10381 flinfo->locsym_shndx);
c152c796
AM
10382 if (isymbuf == NULL)
10383 return FALSE;
10384 }
10385
10386 /* Find local symbol sections and adjust values of symbols in
10387 SEC_MERGE sections. Write out those local symbols we know are
10388 going into the output file. */
10389 isymend = isymbuf + locsymcount;
8b127cbc 10390 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10391 isym < isymend;
10392 isym++, pindex++, ppsection++)
10393 {
10394 asection *isec;
10395 const char *name;
10396 Elf_Internal_Sym osym;
6e0b88f1
AM
10397 long indx;
10398 int ret;
c152c796
AM
10399
10400 *pindex = -1;
10401
10402 if (elf_bad_symtab (input_bfd))
10403 {
10404 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10405 {
10406 *ppsection = NULL;
10407 continue;
10408 }
10409 }
10410
10411 if (isym->st_shndx == SHN_UNDEF)
10412 isec = bfd_und_section_ptr;
c152c796
AM
10413 else if (isym->st_shndx == SHN_ABS)
10414 isec = bfd_abs_section_ptr;
10415 else if (isym->st_shndx == SHN_COMMON)
10416 isec = bfd_com_section_ptr;
10417 else
10418 {
cb33740c
AM
10419 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10420 if (isec == NULL)
10421 {
10422 /* Don't attempt to output symbols with st_shnx in the
10423 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10424 isec = bfd_und_section_ptr;
cb33740c 10425 }
dbaa2011 10426 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10427 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10428 isym->st_value =
10429 _bfd_merged_section_offset (output_bfd, &isec,
10430 elf_section_data (isec)->sec_info,
10431 isym->st_value);
c152c796
AM
10432 }
10433
10434 *ppsection = isec;
10435
d983c8c5
AM
10436 /* Don't output the first, undefined, symbol. In fact, don't
10437 output any undefined local symbol. */
10438 if (isec == bfd_und_section_ptr)
c152c796
AM
10439 continue;
10440
10441 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10442 {
10443 /* We never output section symbols. Instead, we use the
10444 section symbol of the corresponding section in the output
10445 file. */
10446 continue;
10447 }
10448
10449 /* If we are stripping all symbols, we don't want to output this
10450 one. */
8b127cbc 10451 if (flinfo->info->strip == strip_all)
c152c796
AM
10452 continue;
10453
10454 /* If we are discarding all local symbols, we don't want to
10455 output this one. If we are generating a relocatable output
10456 file, then some of the local symbols may be required by
10457 relocs; we output them below as we discover that they are
10458 needed. */
8b127cbc 10459 if (flinfo->info->discard == discard_all)
c152c796
AM
10460 continue;
10461
10462 /* If this symbol is defined in a section which we are
f02571c5
AM
10463 discarding, we don't need to keep it. */
10464 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10465 && isym->st_shndx < SHN_LORESERVE
10466 && bfd_section_removed_from_list (output_bfd,
10467 isec->output_section))
e75a280b
L
10468 continue;
10469
c152c796
AM
10470 /* Get the name of the symbol. */
10471 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10472 isym->st_name);
10473 if (name == NULL)
10474 return FALSE;
10475
10476 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10477 if ((flinfo->info->strip == strip_some
10478 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10479 == NULL))
8b127cbc 10480 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10481 && (isec->flags & SEC_MERGE)
10482 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10483 || flinfo->info->discard == discard_l)
c152c796
AM
10484 && bfd_is_local_label_name (input_bfd, name)))
10485 continue;
10486
ffbc01cc
AM
10487 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10488 {
ce875075
AM
10489 if (input_bfd->lto_output)
10490 /* -flto puts a temp file name here. This means builds
10491 are not reproducible. Discard the symbol. */
10492 continue;
ffbc01cc
AM
10493 have_file_sym = TRUE;
10494 flinfo->filesym_count += 1;
10495 }
10496 if (!have_file_sym)
10497 {
10498 /* In the absence of debug info, bfd_find_nearest_line uses
10499 FILE symbols to determine the source file for local
10500 function symbols. Provide a FILE symbol here if input
10501 files lack such, so that their symbols won't be
10502 associated with a previous input file. It's not the
10503 source file, but the best we can do. */
10504 have_file_sym = TRUE;
10505 flinfo->filesym_count += 1;
10506 memset (&osym, 0, sizeof (osym));
10507 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10508 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10509 if (!elf_link_output_symstrtab (flinfo,
10510 (input_bfd->lto_output ? NULL
10511 : input_bfd->filename),
10512 &osym, bfd_abs_section_ptr,
10513 NULL))
ffbc01cc
AM
10514 return FALSE;
10515 }
10516
c152c796
AM
10517 osym = *isym;
10518
10519 /* Adjust the section index for the output file. */
10520 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10521 isec->output_section);
10522 if (osym.st_shndx == SHN_BAD)
10523 return FALSE;
10524
c152c796
AM
10525 /* ELF symbols in relocatable files are section relative, but
10526 in executable files they are virtual addresses. Note that
10527 this code assumes that all ELF sections have an associated
10528 BFD section with a reasonable value for output_offset; below
10529 we assume that they also have a reasonable value for
10530 output_section. Any special sections must be set up to meet
10531 these requirements. */
10532 osym.st_value += isec->output_offset;
0e1862bb 10533 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10534 {
10535 osym.st_value += isec->output_section->vma;
10536 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10537 {
10538 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10539 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10540 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10541 else
10542 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10543 STT_NOTYPE);
c152c796
AM
10544 }
10545 }
10546
6e0b88f1 10547 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10548 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10549 if (ret == 0)
c152c796 10550 return FALSE;
6e0b88f1
AM
10551 else if (ret == 1)
10552 *pindex = indx;
c152c796
AM
10553 }
10554
310fd250
L
10555 if (bed->s->arch_size == 32)
10556 {
10557 r_type_mask = 0xff;
10558 r_sym_shift = 8;
10559 address_size = 4;
10560 }
10561 else
10562 {
10563 r_type_mask = 0xffffffff;
10564 r_sym_shift = 32;
10565 address_size = 8;
10566 }
10567
c152c796
AM
10568 /* Relocate the contents of each section. */
10569 sym_hashes = elf_sym_hashes (input_bfd);
10570 for (o = input_bfd->sections; o != NULL; o = o->next)
10571 {
10572 bfd_byte *contents;
10573
10574 if (! o->linker_mark)
10575 {
10576 /* This section was omitted from the link. */
10577 continue;
10578 }
10579
7bdf4127 10580 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10581 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10582 {
10583 /* Deal with the group signature symbol. */
10584 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10585 unsigned long symndx = sec_data->this_hdr.sh_info;
10586 asection *osec = o->output_section;
10587
7bdf4127 10588 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10589 if (symndx >= locsymcount
10590 || (elf_bad_symtab (input_bfd)
8b127cbc 10591 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10592 {
10593 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10594 while (h->root.type == bfd_link_hash_indirect
10595 || h->root.type == bfd_link_hash_warning)
10596 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10597 /* Arrange for symbol to be output. */
10598 h->indx = -2;
10599 elf_section_data (osec)->this_hdr.sh_info = -2;
10600 }
10601 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10602 {
10603 /* We'll use the output section target_index. */
8b127cbc 10604 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10605 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10606 }
10607 else
10608 {
8b127cbc 10609 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10610 {
10611 /* Otherwise output the local symbol now. */
10612 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10613 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10614 const char *name;
6e0b88f1
AM
10615 long indx;
10616 int ret;
bcacc0f5
AM
10617
10618 name = bfd_elf_string_from_elf_section (input_bfd,
10619 symtab_hdr->sh_link,
10620 sym.st_name);
10621 if (name == NULL)
10622 return FALSE;
10623
10624 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10625 sec);
10626 if (sym.st_shndx == SHN_BAD)
10627 return FALSE;
10628
10629 sym.st_value += o->output_offset;
10630
6e0b88f1 10631 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10632 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10633 NULL);
6e0b88f1 10634 if (ret == 0)
bcacc0f5 10635 return FALSE;
6e0b88f1 10636 else if (ret == 1)
8b127cbc 10637 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10638 else
10639 abort ();
bcacc0f5
AM
10640 }
10641 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10642 = flinfo->indices[symndx];
bcacc0f5
AM
10643 }
10644 }
10645
c152c796 10646 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10647 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10648 continue;
10649
10650 if ((o->flags & SEC_LINKER_CREATED) != 0)
10651 {
10652 /* Section was created by _bfd_elf_link_create_dynamic_sections
10653 or somesuch. */
10654 continue;
10655 }
10656
10657 /* Get the contents of the section. They have been cached by a
10658 relaxation routine. Note that o is a section in an input
10659 file, so the contents field will not have been set by any of
10660 the routines which work on output files. */
10661 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10662 {
10663 contents = elf_section_data (o)->this_hdr.contents;
10664 if (bed->caches_rawsize
10665 && o->rawsize != 0
10666 && o->rawsize < o->size)
10667 {
10668 memcpy (flinfo->contents, contents, o->rawsize);
10669 contents = flinfo->contents;
10670 }
10671 }
c152c796
AM
10672 else
10673 {
8b127cbc 10674 contents = flinfo->contents;
4a114e3e 10675 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10676 return FALSE;
10677 }
10678
10679 if ((o->flags & SEC_RELOC) != 0)
10680 {
10681 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10682 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10683 int action_discarded;
ece5ef60 10684 int ret;
c152c796
AM
10685
10686 /* Get the swapped relocs. */
10687 internal_relocs
8b127cbc
AM
10688 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10689 flinfo->internal_relocs, FALSE);
c152c796
AM
10690 if (internal_relocs == NULL
10691 && o->reloc_count > 0)
10692 return FALSE;
10693
310fd250
L
10694 /* We need to reverse-copy input .ctors/.dtors sections if
10695 they are placed in .init_array/.finit_array for output. */
10696 if (o->size > address_size
10697 && ((strncmp (o->name, ".ctors", 6) == 0
10698 && strcmp (o->output_section->name,
10699 ".init_array") == 0)
10700 || (strncmp (o->name, ".dtors", 6) == 0
10701 && strcmp (o->output_section->name,
10702 ".fini_array") == 0))
10703 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10704 {
056bafd4
MR
10705 if (o->size * bed->s->int_rels_per_ext_rel
10706 != o->reloc_count * address_size)
310fd250 10707 {
4eca0228 10708 _bfd_error_handler
695344c0 10709 /* xgettext:c-format */
871b3ab2 10710 (_("error: %pB: size of section %pA is not "
310fd250
L
10711 "multiple of address size"),
10712 input_bfd, o);
8c6716e5 10713 bfd_set_error (bfd_error_bad_value);
310fd250
L
10714 return FALSE;
10715 }
10716 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10717 }
10718
0f02bbd9 10719 action_discarded = -1;
c152c796 10720 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10721 action_discarded = (*bed->action_discarded) (o);
10722
10723 /* Run through the relocs evaluating complex reloc symbols and
10724 looking for relocs against symbols from discarded sections
10725 or section symbols from removed link-once sections.
10726 Complain about relocs against discarded sections. Zero
10727 relocs against removed link-once sections. */
10728
10729 rel = internal_relocs;
056bafd4 10730 relend = rel + o->reloc_count;
0f02bbd9 10731 for ( ; rel < relend; rel++)
c152c796 10732 {
0f02bbd9
AM
10733 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10734 unsigned int s_type;
10735 asection **ps, *sec;
10736 struct elf_link_hash_entry *h = NULL;
10737 const char *sym_name;
c152c796 10738
0f02bbd9
AM
10739 if (r_symndx == STN_UNDEF)
10740 continue;
c152c796 10741
0f02bbd9
AM
10742 if (r_symndx >= locsymcount
10743 || (elf_bad_symtab (input_bfd)
8b127cbc 10744 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10745 {
10746 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10747
0f02bbd9
AM
10748 /* Badly formatted input files can contain relocs that
10749 reference non-existant symbols. Check here so that
10750 we do not seg fault. */
10751 if (h == NULL)
c152c796 10752 {
4eca0228 10753 _bfd_error_handler
695344c0 10754 /* xgettext:c-format */
2dcf00ce 10755 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10756 "that references a non-existent global symbol"),
2dcf00ce 10757 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10758 bfd_set_error (bfd_error_bad_value);
10759 return FALSE;
10760 }
3b36f7e6 10761
0f02bbd9
AM
10762 while (h->root.type == bfd_link_hash_indirect
10763 || h->root.type == bfd_link_hash_warning)
10764 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10765
0f02bbd9 10766 s_type = h->type;
cdd3575c 10767
9e2dec47 10768 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10769 mark the symbol as undefined. Note that the
10770 linker may attach linker created dynamic sections
10771 to the plugin bfd. Symbols defined in linker
10772 created sections are not plugin symbols. */
bc4e12de 10773 if ((h->root.non_ir_ref_regular
4070765b 10774 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10775 && (h->root.type == bfd_link_hash_defined
10776 || h->root.type == bfd_link_hash_defweak)
10777 && (h->root.u.def.section->flags
10778 & SEC_LINKER_CREATED) == 0
10779 && h->root.u.def.section->owner != NULL
10780 && (h->root.u.def.section->owner->flags
10781 & BFD_PLUGIN) != 0)
10782 {
10783 h->root.type = bfd_link_hash_undefined;
10784 h->root.u.undef.abfd = h->root.u.def.section->owner;
10785 }
10786
0f02bbd9
AM
10787 ps = NULL;
10788 if (h->root.type == bfd_link_hash_defined
10789 || h->root.type == bfd_link_hash_defweak)
10790 ps = &h->root.u.def.section;
10791
10792 sym_name = h->root.root.string;
10793 }
10794 else
10795 {
10796 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10797
10798 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10799 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10800 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10801 sym, *ps);
10802 }
c152c796 10803
c301e700 10804 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10805 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10806 {
10807 bfd_vma val;
10808 bfd_vma dot = (rel->r_offset
10809 + o->output_offset + o->output_section->vma);
10810#ifdef DEBUG
10811 printf ("Encountered a complex symbol!");
10812 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10813 input_bfd->filename, o->name,
10814 (long) (rel - internal_relocs));
0f02bbd9
AM
10815 printf (" symbol: idx %8.8lx, name %s\n",
10816 r_symndx, sym_name);
10817 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10818 (unsigned long) rel->r_info,
10819 (unsigned long) rel->r_offset);
10820#endif
8b127cbc 10821 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10822 isymbuf, locsymcount, s_type == STT_SRELC))
10823 return FALSE;
10824
10825 /* Symbol evaluated OK. Update to absolute value. */
10826 set_symbol_value (input_bfd, isymbuf, locsymcount,
10827 r_symndx, val);
10828 continue;
10829 }
10830
10831 if (action_discarded != -1 && ps != NULL)
10832 {
cdd3575c
AM
10833 /* Complain if the definition comes from a
10834 discarded section. */
dbaa2011 10835 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10836 {
cf35638d 10837 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10838 if (action_discarded & COMPLAIN)
8b127cbc 10839 (*flinfo->info->callbacks->einfo)
695344c0 10840 /* xgettext:c-format */
871b3ab2
AM
10841 (_("%X`%s' referenced in section `%pA' of %pB: "
10842 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10843 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10844
87e5235d 10845 /* Try to do the best we can to support buggy old
e0ae6d6f 10846 versions of gcc. Pretend that the symbol is
87e5235d
AM
10847 really defined in the kept linkonce section.
10848 FIXME: This is quite broken. Modifying the
10849 symbol here means we will be changing all later
e0ae6d6f 10850 uses of the symbol, not just in this section. */
0f02bbd9 10851 if (action_discarded & PRETEND)
87e5235d 10852 {
01b3c8ab
L
10853 asection *kept;
10854
c0f00686 10855 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10856 flinfo->info);
01b3c8ab 10857 if (kept != NULL)
87e5235d
AM
10858 {
10859 *ps = kept;
10860 continue;
10861 }
10862 }
c152c796
AM
10863 }
10864 }
10865 }
10866
10867 /* Relocate the section by invoking a back end routine.
10868
10869 The back end routine is responsible for adjusting the
10870 section contents as necessary, and (if using Rela relocs
10871 and generating a relocatable output file) adjusting the
10872 reloc addend as necessary.
10873
10874 The back end routine does not have to worry about setting
10875 the reloc address or the reloc symbol index.
10876
10877 The back end routine is given a pointer to the swapped in
10878 internal symbols, and can access the hash table entries
10879 for the external symbols via elf_sym_hashes (input_bfd).
10880
10881 When generating relocatable output, the back end routine
10882 must handle STB_LOCAL/STT_SECTION symbols specially. The
10883 output symbol is going to be a section symbol
10884 corresponding to the output section, which will require
10885 the addend to be adjusted. */
10886
8b127cbc 10887 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10888 input_bfd, o, contents,
10889 internal_relocs,
10890 isymbuf,
8b127cbc 10891 flinfo->sections);
ece5ef60 10892 if (!ret)
c152c796
AM
10893 return FALSE;
10894
ece5ef60 10895 if (ret == 2
0e1862bb 10896 || bfd_link_relocatable (flinfo->info)
8b127cbc 10897 || flinfo->info->emitrelocations)
c152c796
AM
10898 {
10899 Elf_Internal_Rela *irela;
d4730f92 10900 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10901 bfd_vma last_offset;
10902 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10903 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10904 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10905 unsigned int next_erel;
c152c796 10906 bfd_boolean rela_normal;
d4730f92 10907 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10908
d4730f92
BS
10909 esdi = elf_section_data (o);
10910 esdo = elf_section_data (o->output_section);
10911 rela_normal = FALSE;
c152c796
AM
10912
10913 /* Adjust the reloc addresses and symbol indices. */
10914
10915 irela = internal_relocs;
056bafd4 10916 irelaend = irela + o->reloc_count;
d4730f92
BS
10917 rel_hash = esdo->rel.hashes + esdo->rel.count;
10918 /* We start processing the REL relocs, if any. When we reach
10919 IRELAMID in the loop, we switch to the RELA relocs. */
10920 irelamid = irela;
10921 if (esdi->rel.hdr != NULL)
10922 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10923 * bed->s->int_rels_per_ext_rel);
eac338cf 10924 rel_hash_list = rel_hash;
d4730f92 10925 rela_hash_list = NULL;
c152c796 10926 last_offset = o->output_offset;
0e1862bb 10927 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10928 last_offset += o->output_section->vma;
10929 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10930 {
10931 unsigned long r_symndx;
10932 asection *sec;
10933 Elf_Internal_Sym sym;
10934
10935 if (next_erel == bed->s->int_rels_per_ext_rel)
10936 {
10937 rel_hash++;
10938 next_erel = 0;
10939 }
10940
d4730f92
BS
10941 if (irela == irelamid)
10942 {
10943 rel_hash = esdo->rela.hashes + esdo->rela.count;
10944 rela_hash_list = rel_hash;
10945 rela_normal = bed->rela_normal;
10946 }
10947
c152c796 10948 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10949 flinfo->info, o,
c152c796
AM
10950 irela->r_offset);
10951 if (irela->r_offset >= (bfd_vma) -2)
10952 {
10953 /* This is a reloc for a deleted entry or somesuch.
10954 Turn it into an R_*_NONE reloc, at the same
10955 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10956 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10957 being ordered. */
10958 irela->r_offset = last_offset;
10959 irela->r_info = 0;
10960 irela->r_addend = 0;
10961 continue;
10962 }
10963
10964 irela->r_offset += o->output_offset;
10965
10966 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10967 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10968 irela->r_offset += o->output_section->vma;
10969
10970 last_offset = irela->r_offset;
10971
10972 r_symndx = irela->r_info >> r_sym_shift;
10973 if (r_symndx == STN_UNDEF)
10974 continue;
10975
10976 if (r_symndx >= locsymcount
10977 || (elf_bad_symtab (input_bfd)
8b127cbc 10978 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10979 {
10980 struct elf_link_hash_entry *rh;
10981 unsigned long indx;
10982
10983 /* This is a reloc against a global symbol. We
10984 have not yet output all the local symbols, so
10985 we do not know the symbol index of any global
10986 symbol. We set the rel_hash entry for this
10987 reloc to point to the global hash table entry
10988 for this symbol. The symbol index is then
ee75fd95 10989 set at the end of bfd_elf_final_link. */
c152c796
AM
10990 indx = r_symndx - extsymoff;
10991 rh = elf_sym_hashes (input_bfd)[indx];
10992 while (rh->root.type == bfd_link_hash_indirect
10993 || rh->root.type == bfd_link_hash_warning)
10994 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10995
10996 /* Setting the index to -2 tells
10997 elf_link_output_extsym that this symbol is
10998 used by a reloc. */
10999 BFD_ASSERT (rh->indx < 0);
11000 rh->indx = -2;
c152c796
AM
11001 *rel_hash = rh;
11002
11003 continue;
11004 }
11005
11006 /* This is a reloc against a local symbol. */
11007
11008 *rel_hash = NULL;
11009 sym = isymbuf[r_symndx];
8b127cbc 11010 sec = flinfo->sections[r_symndx];
c152c796
AM
11011 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11012 {
11013 /* I suppose the backend ought to fill in the
11014 section of any STT_SECTION symbol against a
6a8d1586 11015 processor specific section. */
cf35638d 11016 r_symndx = STN_UNDEF;
6a8d1586
AM
11017 if (bfd_is_abs_section (sec))
11018 ;
c152c796
AM
11019 else if (sec == NULL || sec->owner == NULL)
11020 {
11021 bfd_set_error (bfd_error_bad_value);
11022 return FALSE;
11023 }
11024 else
11025 {
6a8d1586
AM
11026 asection *osec = sec->output_section;
11027
11028 /* If we have discarded a section, the output
11029 section will be the absolute section. In
ab96bf03
AM
11030 case of discarded SEC_MERGE sections, use
11031 the kept section. relocate_section should
11032 have already handled discarded linkonce
11033 sections. */
6a8d1586
AM
11034 if (bfd_is_abs_section (osec)
11035 && sec->kept_section != NULL
11036 && sec->kept_section->output_section != NULL)
11037 {
11038 osec = sec->kept_section->output_section;
11039 irela->r_addend -= osec->vma;
11040 }
11041
11042 if (!bfd_is_abs_section (osec))
11043 {
11044 r_symndx = osec->target_index;
cf35638d 11045 if (r_symndx == STN_UNDEF)
74541ad4 11046 {
051d833a
AM
11047 irela->r_addend += osec->vma;
11048 osec = _bfd_nearby_section (output_bfd, osec,
11049 osec->vma);
11050 irela->r_addend -= osec->vma;
11051 r_symndx = osec->target_index;
74541ad4 11052 }
6a8d1586 11053 }
c152c796
AM
11054 }
11055
11056 /* Adjust the addend according to where the
11057 section winds up in the output section. */
11058 if (rela_normal)
11059 irela->r_addend += sec->output_offset;
11060 }
11061 else
11062 {
8b127cbc 11063 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11064 {
11065 unsigned long shlink;
11066 const char *name;
11067 asection *osec;
6e0b88f1 11068 long indx;
c152c796 11069
8b127cbc 11070 if (flinfo->info->strip == strip_all)
c152c796
AM
11071 {
11072 /* You can't do ld -r -s. */
11073 bfd_set_error (bfd_error_invalid_operation);
11074 return FALSE;
11075 }
11076
11077 /* This symbol was skipped earlier, but
11078 since it is needed by a reloc, we
11079 must output it now. */
11080 shlink = symtab_hdr->sh_link;
11081 name = (bfd_elf_string_from_elf_section
11082 (input_bfd, shlink, sym.st_name));
11083 if (name == NULL)
11084 return FALSE;
11085
11086 osec = sec->output_section;
11087 sym.st_shndx =
11088 _bfd_elf_section_from_bfd_section (output_bfd,
11089 osec);
11090 if (sym.st_shndx == SHN_BAD)
11091 return FALSE;
11092
11093 sym.st_value += sec->output_offset;
0e1862bb 11094 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11095 {
11096 sym.st_value += osec->vma;
11097 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11098 {
102def4d
AM
11099 struct elf_link_hash_table *htab
11100 = elf_hash_table (flinfo->info);
11101
c152c796
AM
11102 /* STT_TLS symbols are relative to PT_TLS
11103 segment base. */
102def4d
AM
11104 if (htab->tls_sec != NULL)
11105 sym.st_value -= htab->tls_sec->vma;
11106 else
11107 sym.st_info
11108 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11109 STT_NOTYPE);
c152c796
AM
11110 }
11111 }
11112
6e0b88f1 11113 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11114 ret = elf_link_output_symstrtab (flinfo, name,
11115 &sym, sec,
11116 NULL);
6e0b88f1 11117 if (ret == 0)
c152c796 11118 return FALSE;
6e0b88f1 11119 else if (ret == 1)
8b127cbc 11120 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11121 else
11122 abort ();
c152c796
AM
11123 }
11124
8b127cbc 11125 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11126 }
11127
11128 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11129 | (irela->r_info & r_type_mask));
11130 }
11131
11132 /* Swap out the relocs. */
d4730f92
BS
11133 input_rel_hdr = esdi->rel.hdr;
11134 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11135 {
d4730f92
BS
11136 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11137 input_rel_hdr,
11138 internal_relocs,
11139 rel_hash_list))
11140 return FALSE;
c152c796
AM
11141 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11142 * bed->s->int_rels_per_ext_rel);
eac338cf 11143 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11144 }
11145
11146 input_rela_hdr = esdi->rela.hdr;
11147 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11148 {
eac338cf 11149 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11150 input_rela_hdr,
eac338cf 11151 internal_relocs,
d4730f92 11152 rela_hash_list))
c152c796
AM
11153 return FALSE;
11154 }
11155 }
11156 }
11157
11158 /* Write out the modified section contents. */
11159 if (bed->elf_backend_write_section
8b127cbc 11160 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11161 contents))
c152c796
AM
11162 {
11163 /* Section written out. */
11164 }
11165 else switch (o->sec_info_type)
11166 {
dbaa2011 11167 case SEC_INFO_TYPE_STABS:
c152c796
AM
11168 if (! (_bfd_write_section_stabs
11169 (output_bfd,
8b127cbc 11170 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11171 o, &elf_section_data (o)->sec_info, contents)))
11172 return FALSE;
11173 break;
dbaa2011 11174 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11175 if (! _bfd_write_merged_section (output_bfd, o,
11176 elf_section_data (o)->sec_info))
11177 return FALSE;
11178 break;
dbaa2011 11179 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11180 {
8b127cbc 11181 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11182 o, contents))
11183 return FALSE;
11184 }
11185 break;
2f0c68f2
CM
11186 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11187 {
11188 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11189 flinfo->info,
11190 o, contents))
11191 return FALSE;
11192 }
11193 break;
c152c796
AM
11194 default:
11195 {
310fd250
L
11196 if (! (o->flags & SEC_EXCLUDE))
11197 {
11198 file_ptr offset = (file_ptr) o->output_offset;
11199 bfd_size_type todo = o->size;
37b01f6a
DG
11200
11201 offset *= bfd_octets_per_byte (output_bfd);
11202
310fd250
L
11203 if ((o->flags & SEC_ELF_REVERSE_COPY))
11204 {
11205 /* Reverse-copy input section to output. */
11206 do
11207 {
11208 todo -= address_size;
11209 if (! bfd_set_section_contents (output_bfd,
11210 o->output_section,
11211 contents + todo,
11212 offset,
11213 address_size))
11214 return FALSE;
11215 if (todo == 0)
11216 break;
11217 offset += address_size;
11218 }
11219 while (1);
11220 }
11221 else if (! bfd_set_section_contents (output_bfd,
11222 o->output_section,
11223 contents,
11224 offset, todo))
11225 return FALSE;
11226 }
c152c796
AM
11227 }
11228 break;
11229 }
11230 }
11231
11232 return TRUE;
11233}
11234
11235/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11236 requested by the linker, and does not come from any input file. This
c152c796
AM
11237 is used to build constructor and destructor tables when linking
11238 with -Ur. */
11239
11240static bfd_boolean
11241elf_reloc_link_order (bfd *output_bfd,
11242 struct bfd_link_info *info,
11243 asection *output_section,
11244 struct bfd_link_order *link_order)
11245{
11246 reloc_howto_type *howto;
11247 long indx;
11248 bfd_vma offset;
11249 bfd_vma addend;
d4730f92 11250 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11251 struct elf_link_hash_entry **rel_hash_ptr;
11252 Elf_Internal_Shdr *rel_hdr;
11253 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11254 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11255 bfd_byte *erel;
11256 unsigned int i;
d4730f92 11257 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11258
11259 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11260 if (howto == NULL)
11261 {
11262 bfd_set_error (bfd_error_bad_value);
11263 return FALSE;
11264 }
11265
11266 addend = link_order->u.reloc.p->addend;
11267
d4730f92
BS
11268 if (esdo->rel.hdr)
11269 reldata = &esdo->rel;
11270 else if (esdo->rela.hdr)
11271 reldata = &esdo->rela;
11272 else
11273 {
11274 reldata = NULL;
11275 BFD_ASSERT (0);
11276 }
11277
c152c796 11278 /* Figure out the symbol index. */
d4730f92 11279 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11280 if (link_order->type == bfd_section_reloc_link_order)
11281 {
11282 indx = link_order->u.reloc.p->u.section->target_index;
11283 BFD_ASSERT (indx != 0);
11284 *rel_hash_ptr = NULL;
11285 }
11286 else
11287 {
11288 struct elf_link_hash_entry *h;
11289
11290 /* Treat a reloc against a defined symbol as though it were
11291 actually against the section. */
11292 h = ((struct elf_link_hash_entry *)
11293 bfd_wrapped_link_hash_lookup (output_bfd, info,
11294 link_order->u.reloc.p->u.name,
11295 FALSE, FALSE, TRUE));
11296 if (h != NULL
11297 && (h->root.type == bfd_link_hash_defined
11298 || h->root.type == bfd_link_hash_defweak))
11299 {
11300 asection *section;
11301
11302 section = h->root.u.def.section;
11303 indx = section->output_section->target_index;
11304 *rel_hash_ptr = NULL;
11305 /* It seems that we ought to add the symbol value to the
11306 addend here, but in practice it has already been added
11307 because it was passed to constructor_callback. */
11308 addend += section->output_section->vma + section->output_offset;
11309 }
11310 else if (h != NULL)
11311 {
11312 /* Setting the index to -2 tells elf_link_output_extsym that
11313 this symbol is used by a reloc. */
11314 h->indx = -2;
11315 *rel_hash_ptr = h;
11316 indx = 0;
11317 }
11318 else
11319 {
1a72702b
AM
11320 (*info->callbacks->unattached_reloc)
11321 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11322 indx = 0;
11323 }
11324 }
11325
11326 /* If this is an inplace reloc, we must write the addend into the
11327 object file. */
11328 if (howto->partial_inplace && addend != 0)
11329 {
11330 bfd_size_type size;
11331 bfd_reloc_status_type rstat;
11332 bfd_byte *buf;
11333 bfd_boolean ok;
11334 const char *sym_name;
11335
a50b1753
NC
11336 size = (bfd_size_type) bfd_get_reloc_size (howto);
11337 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11338 if (buf == NULL && size != 0)
c152c796
AM
11339 return FALSE;
11340 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11341 switch (rstat)
11342 {
11343 case bfd_reloc_ok:
11344 break;
11345
11346 default:
11347 case bfd_reloc_outofrange:
11348 abort ();
11349
11350 case bfd_reloc_overflow:
11351 if (link_order->type == bfd_section_reloc_link_order)
11352 sym_name = bfd_section_name (output_bfd,
11353 link_order->u.reloc.p->u.section);
11354 else
11355 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11356 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11357 howto->name, addend, NULL, NULL,
11358 (bfd_vma) 0);
c152c796
AM
11359 break;
11360 }
37b01f6a 11361
c152c796 11362 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11363 link_order->offset
11364 * bfd_octets_per_byte (output_bfd),
11365 size);
c152c796
AM
11366 free (buf);
11367 if (! ok)
11368 return FALSE;
11369 }
11370
11371 /* The address of a reloc is relative to the section in a
11372 relocatable file, and is a virtual address in an executable
11373 file. */
11374 offset = link_order->offset;
0e1862bb 11375 if (! bfd_link_relocatable (info))
c152c796
AM
11376 offset += output_section->vma;
11377
11378 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11379 {
11380 irel[i].r_offset = offset;
11381 irel[i].r_info = 0;
11382 irel[i].r_addend = 0;
11383 }
11384 if (bed->s->arch_size == 32)
11385 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11386 else
11387 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11388
d4730f92 11389 rel_hdr = reldata->hdr;
c152c796
AM
11390 erel = rel_hdr->contents;
11391 if (rel_hdr->sh_type == SHT_REL)
11392 {
d4730f92 11393 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11394 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11395 }
11396 else
11397 {
11398 irel[0].r_addend = addend;
d4730f92 11399 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11400 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11401 }
11402
d4730f92 11403 ++reldata->count;
c152c796
AM
11404
11405 return TRUE;
11406}
11407
0b52efa6
PB
11408
11409/* Get the output vma of the section pointed to by the sh_link field. */
11410
11411static bfd_vma
11412elf_get_linked_section_vma (struct bfd_link_order *p)
11413{
11414 Elf_Internal_Shdr **elf_shdrp;
11415 asection *s;
11416 int elfsec;
11417
11418 s = p->u.indirect.section;
11419 elf_shdrp = elf_elfsections (s->owner);
11420 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11421 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11422 /* PR 290:
11423 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11424 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11425 sh_info fields. Hence we could get the situation
11426 where elfsec is 0. */
11427 if (elfsec == 0)
11428 {
11429 const struct elf_backend_data *bed
11430 = get_elf_backend_data (s->owner);
11431 if (bed->link_order_error_handler)
d003868e 11432 bed->link_order_error_handler
695344c0 11433 /* xgettext:c-format */
871b3ab2 11434 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11435 return 0;
11436 }
11437 else
11438 {
11439 s = elf_shdrp[elfsec]->bfd_section;
11440 return s->output_section->vma + s->output_offset;
11441 }
0b52efa6
PB
11442}
11443
11444
11445/* Compare two sections based on the locations of the sections they are
11446 linked to. Used by elf_fixup_link_order. */
11447
11448static int
11449compare_link_order (const void * a, const void * b)
11450{
11451 bfd_vma apos;
11452 bfd_vma bpos;
11453
11454 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11455 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11456 if (apos < bpos)
11457 return -1;
11458 return apos > bpos;
11459}
11460
11461
11462/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11463 order as their linked sections. Returns false if this could not be done
11464 because an output section includes both ordered and unordered
11465 sections. Ideally we'd do this in the linker proper. */
11466
11467static bfd_boolean
11468elf_fixup_link_order (bfd *abfd, asection *o)
11469{
11470 int seen_linkorder;
11471 int seen_other;
11472 int n;
11473 struct bfd_link_order *p;
11474 bfd *sub;
11475 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11476 unsigned elfsec;
0b52efa6 11477 struct bfd_link_order **sections;
d33cdfe3 11478 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11479 bfd_vma offset;
3b36f7e6 11480
d33cdfe3
L
11481 other_sec = NULL;
11482 linkorder_sec = NULL;
0b52efa6
PB
11483 seen_other = 0;
11484 seen_linkorder = 0;
8423293d 11485 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11486 {
d33cdfe3 11487 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11488 {
11489 s = p->u.indirect.section;
d33cdfe3
L
11490 sub = s->owner;
11491 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11492 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11493 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11494 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11495 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11496 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11497 {
11498 seen_linkorder++;
11499 linkorder_sec = s;
11500 }
0b52efa6 11501 else
d33cdfe3
L
11502 {
11503 seen_other++;
11504 other_sec = s;
11505 }
0b52efa6
PB
11506 }
11507 else
11508 seen_other++;
d33cdfe3
L
11509
11510 if (seen_other && seen_linkorder)
11511 {
11512 if (other_sec && linkorder_sec)
4eca0228 11513 _bfd_error_handler
695344c0 11514 /* xgettext:c-format */
871b3ab2
AM
11515 (_("%pA has both ordered [`%pA' in %pB] "
11516 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11517 o, linkorder_sec, linkorder_sec->owner,
11518 other_sec, other_sec->owner);
d33cdfe3 11519 else
4eca0228 11520 _bfd_error_handler
871b3ab2 11521 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11522 bfd_set_error (bfd_error_bad_value);
11523 return FALSE;
11524 }
0b52efa6
PB
11525 }
11526
11527 if (!seen_linkorder)
11528 return TRUE;
11529
0b52efa6 11530 sections = (struct bfd_link_order **)
14b1c01e
AM
11531 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11532 if (sections == NULL)
11533 return FALSE;
0b52efa6 11534 seen_linkorder = 0;
3b36f7e6 11535
8423293d 11536 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11537 {
11538 sections[seen_linkorder++] = p;
11539 }
11540 /* Sort the input sections in the order of their linked section. */
11541 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11542 compare_link_order);
11543
11544 /* Change the offsets of the sections. */
11545 offset = 0;
11546 for (n = 0; n < seen_linkorder; n++)
11547 {
11548 s = sections[n]->u.indirect.section;
461686a3 11549 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11550 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11551 sections[n]->offset = offset;
11552 offset += sections[n]->size;
11553 }
11554
4dd07732 11555 free (sections);
0b52efa6
PB
11556 return TRUE;
11557}
11558
76359541
TP
11559/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11560 Returns TRUE upon success, FALSE otherwise. */
11561
11562static bfd_boolean
11563elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11564{
11565 bfd_boolean ret = FALSE;
11566 bfd *implib_bfd;
11567 const struct elf_backend_data *bed;
11568 flagword flags;
11569 enum bfd_architecture arch;
11570 unsigned int mach;
11571 asymbol **sympp = NULL;
11572 long symsize;
11573 long symcount;
11574 long src_count;
11575 elf_symbol_type *osymbuf;
11576
11577 implib_bfd = info->out_implib_bfd;
11578 bed = get_elf_backend_data (abfd);
11579
11580 if (!bfd_set_format (implib_bfd, bfd_object))
11581 return FALSE;
11582
046734ff 11583 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11584 flags = bfd_get_file_flags (abfd);
11585 flags &= ~HAS_RELOC;
11586 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11587 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11588 return FALSE;
11589
11590 /* Copy architecture of output file to import library file. */
11591 arch = bfd_get_arch (abfd);
11592 mach = bfd_get_mach (abfd);
11593 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11594 && (abfd->target_defaulted
11595 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11596 return FALSE;
11597
11598 /* Get symbol table size. */
11599 symsize = bfd_get_symtab_upper_bound (abfd);
11600 if (symsize < 0)
11601 return FALSE;
11602
11603 /* Read in the symbol table. */
11604 sympp = (asymbol **) xmalloc (symsize);
11605 symcount = bfd_canonicalize_symtab (abfd, sympp);
11606 if (symcount < 0)
11607 goto free_sym_buf;
11608
11609 /* Allow the BFD backend to copy any private header data it
11610 understands from the output BFD to the import library BFD. */
11611 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11612 goto free_sym_buf;
11613
11614 /* Filter symbols to appear in the import library. */
11615 if (bed->elf_backend_filter_implib_symbols)
11616 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11617 symcount);
11618 else
11619 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11620 if (symcount == 0)
11621 {
5df1bc57 11622 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11623 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11624 implib_bfd);
76359541
TP
11625 goto free_sym_buf;
11626 }
11627
11628
11629 /* Make symbols absolute. */
11630 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11631 sizeof (*osymbuf));
11632 for (src_count = 0; src_count < symcount; src_count++)
11633 {
11634 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11635 sizeof (*osymbuf));
11636 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11637 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11638 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11639 osymbuf[src_count].internal_elf_sym.st_value =
11640 osymbuf[src_count].symbol.value;
11641 sympp[src_count] = &osymbuf[src_count].symbol;
11642 }
11643
11644 bfd_set_symtab (implib_bfd, sympp, symcount);
11645
11646 /* Allow the BFD backend to copy any private data it understands
11647 from the output BFD to the import library BFD. This is done last
11648 to permit the routine to look at the filtered symbol table. */
11649 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11650 goto free_sym_buf;
11651
11652 if (!bfd_close (implib_bfd))
11653 goto free_sym_buf;
11654
11655 ret = TRUE;
11656
11657free_sym_buf:
11658 free (sympp);
11659 return ret;
11660}
11661
9f7c3e5e
AM
11662static void
11663elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11664{
11665 asection *o;
11666
11667 if (flinfo->symstrtab != NULL)
ef10c3ac 11668 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11669 if (flinfo->contents != NULL)
11670 free (flinfo->contents);
11671 if (flinfo->external_relocs != NULL)
11672 free (flinfo->external_relocs);
11673 if (flinfo->internal_relocs != NULL)
11674 free (flinfo->internal_relocs);
11675 if (flinfo->external_syms != NULL)
11676 free (flinfo->external_syms);
11677 if (flinfo->locsym_shndx != NULL)
11678 free (flinfo->locsym_shndx);
11679 if (flinfo->internal_syms != NULL)
11680 free (flinfo->internal_syms);
11681 if (flinfo->indices != NULL)
11682 free (flinfo->indices);
11683 if (flinfo->sections != NULL)
11684 free (flinfo->sections);
9f7c3e5e
AM
11685 if (flinfo->symshndxbuf != NULL)
11686 free (flinfo->symshndxbuf);
11687 for (o = obfd->sections; o != NULL; o = o->next)
11688 {
11689 struct bfd_elf_section_data *esdo = elf_section_data (o);
11690 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11691 free (esdo->rel.hashes);
11692 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11693 free (esdo->rela.hashes);
11694 }
11695}
0b52efa6 11696
c152c796
AM
11697/* Do the final step of an ELF link. */
11698
11699bfd_boolean
11700bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11701{
11702 bfd_boolean dynamic;
11703 bfd_boolean emit_relocs;
11704 bfd *dynobj;
8b127cbc 11705 struct elf_final_link_info flinfo;
91d6fa6a
NC
11706 asection *o;
11707 struct bfd_link_order *p;
11708 bfd *sub;
c152c796
AM
11709 bfd_size_type max_contents_size;
11710 bfd_size_type max_external_reloc_size;
11711 bfd_size_type max_internal_reloc_count;
11712 bfd_size_type max_sym_count;
11713 bfd_size_type max_sym_shndx_count;
c152c796
AM
11714 Elf_Internal_Sym elfsym;
11715 unsigned int i;
11716 Elf_Internal_Shdr *symtab_hdr;
11717 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11718 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11719 struct elf_outext_info eoinfo;
11720 bfd_boolean merged;
11721 size_t relativecount = 0;
11722 asection *reldyn = 0;
11723 bfd_size_type amt;
104d59d1
JM
11724 asection *attr_section = NULL;
11725 bfd_vma attr_size = 0;
11726 const char *std_attrs_section;
64f52338 11727 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11728
64f52338 11729 if (!is_elf_hash_table (htab))
c152c796
AM
11730 return FALSE;
11731
0e1862bb 11732 if (bfd_link_pic (info))
c152c796
AM
11733 abfd->flags |= DYNAMIC;
11734
64f52338
AM
11735 dynamic = htab->dynamic_sections_created;
11736 dynobj = htab->dynobj;
c152c796 11737
0e1862bb 11738 emit_relocs = (bfd_link_relocatable (info)
a4676736 11739 || info->emitrelocations);
c152c796 11740
8b127cbc
AM
11741 flinfo.info = info;
11742 flinfo.output_bfd = abfd;
ef10c3ac 11743 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11744 if (flinfo.symstrtab == NULL)
c152c796
AM
11745 return FALSE;
11746
11747 if (! dynamic)
11748 {
8b127cbc
AM
11749 flinfo.hash_sec = NULL;
11750 flinfo.symver_sec = NULL;
c152c796
AM
11751 }
11752 else
11753 {
3d4d4302 11754 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11755 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11756 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11757 /* Note that it is OK if symver_sec is NULL. */
11758 }
11759
8b127cbc
AM
11760 flinfo.contents = NULL;
11761 flinfo.external_relocs = NULL;
11762 flinfo.internal_relocs = NULL;
11763 flinfo.external_syms = NULL;
11764 flinfo.locsym_shndx = NULL;
11765 flinfo.internal_syms = NULL;
11766 flinfo.indices = NULL;
11767 flinfo.sections = NULL;
8b127cbc 11768 flinfo.symshndxbuf = NULL;
ffbc01cc 11769 flinfo.filesym_count = 0;
c152c796 11770
104d59d1
JM
11771 /* The object attributes have been merged. Remove the input
11772 sections from the link, and set the contents of the output
11773 secton. */
11774 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11775 for (o = abfd->sections; o != NULL; o = o->next)
11776 {
5270eddc 11777 bfd_boolean remove_section = FALSE;
b8a6ced7 11778
104d59d1
JM
11779 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11780 || strcmp (o->name, ".gnu.attributes") == 0)
11781 {
11782 for (p = o->map_head.link_order; p != NULL; p = p->next)
11783 {
11784 asection *input_section;
11785
11786 if (p->type != bfd_indirect_link_order)
11787 continue;
11788 input_section = p->u.indirect.section;
11789 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11790 elf_link_input_bfd ignores this section. */
11791 input_section->flags &= ~SEC_HAS_CONTENTS;
11792 }
a0c8462f 11793
104d59d1 11794 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11795 bfd_set_section_size (abfd, o, attr_size);
11796 /* Skip this section later on. */
11797 o->map_head.link_order = NULL;
104d59d1 11798 if (attr_size)
b8a6ced7 11799 attr_section = o;
104d59d1 11800 else
5270eddc 11801 remove_section = TRUE;
104d59d1 11802 }
6e5e9d58
AM
11803 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11804 {
11805 /* Remove empty group section from linker output. */
5270eddc 11806 remove_section = TRUE;
b8a6ced7 11807 }
5270eddc 11808 if (remove_section)
b8a6ced7 11809 {
6e5e9d58
AM
11810 o->flags |= SEC_EXCLUDE;
11811 bfd_section_list_remove (abfd, o);
11812 abfd->section_count--;
11813 }
104d59d1
JM
11814 }
11815
c152c796
AM
11816 /* Count up the number of relocations we will output for each output
11817 section, so that we know the sizes of the reloc sections. We
11818 also figure out some maximum sizes. */
11819 max_contents_size = 0;
11820 max_external_reloc_size = 0;
11821 max_internal_reloc_count = 0;
11822 max_sym_count = 0;
11823 max_sym_shndx_count = 0;
11824 merged = FALSE;
11825 for (o = abfd->sections; o != NULL; o = o->next)
11826 {
11827 struct bfd_elf_section_data *esdo = elf_section_data (o);
11828 o->reloc_count = 0;
11829
8423293d 11830 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11831 {
11832 unsigned int reloc_count = 0;
9eaff861 11833 unsigned int additional_reloc_count = 0;
c152c796 11834 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11835
11836 if (p->type == bfd_section_reloc_link_order
11837 || p->type == bfd_symbol_reloc_link_order)
11838 reloc_count = 1;
11839 else if (p->type == bfd_indirect_link_order)
11840 {
11841 asection *sec;
11842
11843 sec = p->u.indirect.section;
c152c796
AM
11844
11845 /* Mark all sections which are to be included in the
11846 link. This will normally be every section. We need
11847 to do this so that we can identify any sections which
11848 the linker has decided to not include. */
11849 sec->linker_mark = TRUE;
11850
11851 if (sec->flags & SEC_MERGE)
11852 merged = TRUE;
11853
eea6121a
AM
11854 if (sec->rawsize > max_contents_size)
11855 max_contents_size = sec->rawsize;
11856 if (sec->size > max_contents_size)
11857 max_contents_size = sec->size;
c152c796 11858
c152c796
AM
11859 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11860 && (sec->owner->flags & DYNAMIC) == 0)
11861 {
11862 size_t sym_count;
11863
a961cdd5
AM
11864 /* We are interested in just local symbols, not all
11865 symbols. */
c152c796
AM
11866 if (elf_bad_symtab (sec->owner))
11867 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11868 / bed->s->sizeof_sym);
11869 else
11870 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11871
11872 if (sym_count > max_sym_count)
11873 max_sym_count = sym_count;
11874
11875 if (sym_count > max_sym_shndx_count
6a40cf0c 11876 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11877 max_sym_shndx_count = sym_count;
11878
a961cdd5
AM
11879 if (esdo->this_hdr.sh_type == SHT_REL
11880 || esdo->this_hdr.sh_type == SHT_RELA)
11881 /* Some backends use reloc_count in relocation sections
11882 to count particular types of relocs. Of course,
11883 reloc sections themselves can't have relocations. */
11884 ;
11885 else if (emit_relocs)
11886 {
11887 reloc_count = sec->reloc_count;
11888 if (bed->elf_backend_count_additional_relocs)
11889 {
11890 int c;
11891 c = (*bed->elf_backend_count_additional_relocs) (sec);
11892 additional_reloc_count += c;
11893 }
11894 }
11895 else if (bed->elf_backend_count_relocs)
11896 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11897
11898 esdi = elf_section_data (sec);
11899
c152c796
AM
11900 if ((sec->flags & SEC_RELOC) != 0)
11901 {
d4730f92 11902 size_t ext_size = 0;
c152c796 11903
d4730f92
BS
11904 if (esdi->rel.hdr != NULL)
11905 ext_size = esdi->rel.hdr->sh_size;
11906 if (esdi->rela.hdr != NULL)
11907 ext_size += esdi->rela.hdr->sh_size;
7326c758 11908
c152c796
AM
11909 if (ext_size > max_external_reloc_size)
11910 max_external_reloc_size = ext_size;
11911 if (sec->reloc_count > max_internal_reloc_count)
11912 max_internal_reloc_count = sec->reloc_count;
11913 }
11914 }
11915 }
11916
11917 if (reloc_count == 0)
11918 continue;
11919
9eaff861 11920 reloc_count += additional_reloc_count;
c152c796
AM
11921 o->reloc_count += reloc_count;
11922
0e1862bb 11923 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11924 {
d4730f92 11925 if (esdi->rel.hdr)
9eaff861 11926 {
491d01d3 11927 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11928 esdo->rel.count += additional_reloc_count;
11929 }
d4730f92 11930 if (esdi->rela.hdr)
9eaff861 11931 {
491d01d3 11932 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11933 esdo->rela.count += additional_reloc_count;
11934 }
d4730f92
BS
11935 }
11936 else
11937 {
11938 if (o->use_rela_p)
11939 esdo->rela.count += reloc_count;
2c2b4ed4 11940 else
d4730f92 11941 esdo->rel.count += reloc_count;
c152c796 11942 }
c152c796
AM
11943 }
11944
9eaff861 11945 if (o->reloc_count > 0)
c152c796
AM
11946 o->flags |= SEC_RELOC;
11947 else
11948 {
11949 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11950 set it (this is probably a bug) and if it is set
11951 assign_section_numbers will create a reloc section. */
11952 o->flags &=~ SEC_RELOC;
11953 }
11954
11955 /* If the SEC_ALLOC flag is not set, force the section VMA to
11956 zero. This is done in elf_fake_sections as well, but forcing
11957 the VMA to 0 here will ensure that relocs against these
11958 sections are handled correctly. */
11959 if ((o->flags & SEC_ALLOC) == 0
11960 && ! o->user_set_vma)
11961 o->vma = 0;
11962 }
11963
0e1862bb 11964 if (! bfd_link_relocatable (info) && merged)
64f52338 11965 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11966
11967 /* Figure out the file positions for everything but the symbol table
11968 and the relocs. We set symcount to force assign_section_numbers
11969 to create a symbol table. */
8539e4e8 11970 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11971 BFD_ASSERT (! abfd->output_has_begun);
11972 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11973 goto error_return;
11974
ee75fd95 11975 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11976 for (o = abfd->sections; o != NULL; o = o->next)
11977 {
d4730f92 11978 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11979 if ((o->flags & SEC_RELOC) != 0)
11980 {
d4730f92 11981 if (esdo->rel.hdr
9eaff861 11982 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11983 goto error_return;
11984
d4730f92 11985 if (esdo->rela.hdr
9eaff861 11986 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11987 goto error_return;
11988 }
11989
11990 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11991 to count upwards while actually outputting the relocations. */
d4730f92
BS
11992 esdo->rel.count = 0;
11993 esdo->rela.count = 0;
0ce398f1
L
11994
11995 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11996 {
11997 /* Cache the section contents so that they can be compressed
11998 later. Use bfd_malloc since it will be freed by
11999 bfd_compress_section_contents. */
12000 unsigned char *contents = esdo->this_hdr.contents;
12001 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12002 abort ();
12003 contents
12004 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12005 if (contents == NULL)
12006 goto error_return;
12007 esdo->this_hdr.contents = contents;
12008 }
c152c796
AM
12009 }
12010
c152c796 12011 /* We have now assigned file positions for all the sections except
a485e98e
AM
12012 .symtab, .strtab, and non-loaded reloc sections. We start the
12013 .symtab section at the current file position, and write directly
12014 to it. We build the .strtab section in memory. */
c152c796
AM
12015 bfd_get_symcount (abfd) = 0;
12016 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12017 /* sh_name is set in prep_headers. */
12018 symtab_hdr->sh_type = SHT_SYMTAB;
12019 /* sh_flags, sh_addr and sh_size all start off zero. */
12020 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12021 /* sh_link is set in assign_section_numbers. */
12022 /* sh_info is set below. */
12023 /* sh_offset is set just below. */
72de5009 12024 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12025
ef10c3ac
L
12026 if (max_sym_count < 20)
12027 max_sym_count = 20;
64f52338 12028 htab->strtabsize = max_sym_count;
ef10c3ac 12029 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12030 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12031 if (htab->strtab == NULL)
c152c796 12032 goto error_return;
ef10c3ac
L
12033 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12034 flinfo.symshndxbuf
12035 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12036 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12037
8539e4e8 12038 if (info->strip != strip_all || emit_relocs)
c152c796 12039 {
8539e4e8
AM
12040 file_ptr off = elf_next_file_pos (abfd);
12041
12042 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12043
12044 /* Note that at this point elf_next_file_pos (abfd) is
12045 incorrect. We do not yet know the size of the .symtab section.
12046 We correct next_file_pos below, after we do know the size. */
12047
12048 /* Start writing out the symbol table. The first symbol is always a
12049 dummy symbol. */
c152c796
AM
12050 elfsym.st_value = 0;
12051 elfsym.st_size = 0;
12052 elfsym.st_info = 0;
12053 elfsym.st_other = 0;
12054 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12055 elfsym.st_target_internal = 0;
ef10c3ac
L
12056 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12057 bfd_und_section_ptr, NULL) != 1)
c152c796 12058 goto error_return;
c152c796 12059
8539e4e8
AM
12060 /* Output a symbol for each section. We output these even if we are
12061 discarding local symbols, since they are used for relocs. These
12062 symbols have no names. We store the index of each one in the
12063 index field of the section, so that we can find it again when
12064 outputting relocs. */
12065
c152c796
AM
12066 elfsym.st_size = 0;
12067 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12068 elfsym.st_other = 0;
f0b5bb34 12069 elfsym.st_value = 0;
35fc36a8 12070 elfsym.st_target_internal = 0;
c152c796
AM
12071 for (i = 1; i < elf_numsections (abfd); i++)
12072 {
12073 o = bfd_section_from_elf_index (abfd, i);
12074 if (o != NULL)
f0b5bb34
AM
12075 {
12076 o->target_index = bfd_get_symcount (abfd);
12077 elfsym.st_shndx = i;
0e1862bb 12078 if (!bfd_link_relocatable (info))
f0b5bb34 12079 elfsym.st_value = o->vma;
ef10c3ac
L
12080 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12081 NULL) != 1)
f0b5bb34
AM
12082 goto error_return;
12083 }
c152c796
AM
12084 }
12085 }
12086
12087 /* Allocate some memory to hold information read in from the input
12088 files. */
12089 if (max_contents_size != 0)
12090 {
8b127cbc
AM
12091 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12092 if (flinfo.contents == NULL)
c152c796
AM
12093 goto error_return;
12094 }
12095
12096 if (max_external_reloc_size != 0)
12097 {
8b127cbc
AM
12098 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12099 if (flinfo.external_relocs == NULL)
c152c796
AM
12100 goto error_return;
12101 }
12102
12103 if (max_internal_reloc_count != 0)
12104 {
056bafd4 12105 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12106 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12107 if (flinfo.internal_relocs == NULL)
c152c796
AM
12108 goto error_return;
12109 }
12110
12111 if (max_sym_count != 0)
12112 {
12113 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12114 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12115 if (flinfo.external_syms == NULL)
c152c796
AM
12116 goto error_return;
12117
12118 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12119 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12120 if (flinfo.internal_syms == NULL)
c152c796
AM
12121 goto error_return;
12122
12123 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12124 flinfo.indices = (long int *) bfd_malloc (amt);
12125 if (flinfo.indices == NULL)
c152c796
AM
12126 goto error_return;
12127
12128 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12129 flinfo.sections = (asection **) bfd_malloc (amt);
12130 if (flinfo.sections == NULL)
c152c796
AM
12131 goto error_return;
12132 }
12133
12134 if (max_sym_shndx_count != 0)
12135 {
12136 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12137 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12138 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12139 goto error_return;
12140 }
12141
64f52338 12142 if (htab->tls_sec)
c152c796
AM
12143 {
12144 bfd_vma base, end = 0;
12145 asection *sec;
12146
64f52338 12147 for (sec = htab->tls_sec;
c152c796
AM
12148 sec && (sec->flags & SEC_THREAD_LOCAL);
12149 sec = sec->next)
12150 {
3a800eb9 12151 bfd_size_type size = sec->size;
c152c796 12152
3a800eb9
AM
12153 if (size == 0
12154 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12155 {
91d6fa6a
NC
12156 struct bfd_link_order *ord = sec->map_tail.link_order;
12157
12158 if (ord != NULL)
12159 size = ord->offset + ord->size;
c152c796
AM
12160 }
12161 end = sec->vma + size;
12162 }
64f52338 12163 base = htab->tls_sec->vma;
7dc98aea
RO
12164 /* Only align end of TLS section if static TLS doesn't have special
12165 alignment requirements. */
12166 if (bed->static_tls_alignment == 1)
64f52338
AM
12167 end = align_power (end, htab->tls_sec->alignment_power);
12168 htab->tls_size = end - base;
c152c796
AM
12169 }
12170
0b52efa6
PB
12171 /* Reorder SHF_LINK_ORDER sections. */
12172 for (o = abfd->sections; o != NULL; o = o->next)
12173 {
12174 if (!elf_fixup_link_order (abfd, o))
12175 return FALSE;
12176 }
12177
2f0c68f2
CM
12178 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12179 return FALSE;
12180
c152c796
AM
12181 /* Since ELF permits relocations to be against local symbols, we
12182 must have the local symbols available when we do the relocations.
12183 Since we would rather only read the local symbols once, and we
12184 would rather not keep them in memory, we handle all the
12185 relocations for a single input file at the same time.
12186
12187 Unfortunately, there is no way to know the total number of local
12188 symbols until we have seen all of them, and the local symbol
12189 indices precede the global symbol indices. This means that when
12190 we are generating relocatable output, and we see a reloc against
12191 a global symbol, we can not know the symbol index until we have
12192 finished examining all the local symbols to see which ones we are
12193 going to output. To deal with this, we keep the relocations in
12194 memory, and don't output them until the end of the link. This is
12195 an unfortunate waste of memory, but I don't see a good way around
12196 it. Fortunately, it only happens when performing a relocatable
12197 link, which is not the common case. FIXME: If keep_memory is set
12198 we could write the relocs out and then read them again; I don't
12199 know how bad the memory loss will be. */
12200
c72f2fb2 12201 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12202 sub->output_has_begun = FALSE;
12203 for (o = abfd->sections; o != NULL; o = o->next)
12204 {
8423293d 12205 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12206 {
12207 if (p->type == bfd_indirect_link_order
12208 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12209 == bfd_target_elf_flavour)
12210 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12211 {
12212 if (! sub->output_has_begun)
12213 {
8b127cbc 12214 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12215 goto error_return;
12216 sub->output_has_begun = TRUE;
12217 }
12218 }
12219 else if (p->type == bfd_section_reloc_link_order
12220 || p->type == bfd_symbol_reloc_link_order)
12221 {
12222 if (! elf_reloc_link_order (abfd, info, o, p))
12223 goto error_return;
12224 }
12225 else
12226 {
12227 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12228 {
12229 if (p->type == bfd_indirect_link_order
12230 && (bfd_get_flavour (sub)
12231 == bfd_target_elf_flavour)
12232 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12233 != bed->s->elfclass))
12234 {
12235 const char *iclass, *oclass;
12236
aebf9be7 12237 switch (bed->s->elfclass)
351f65ca 12238 {
aebf9be7
NC
12239 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12240 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12241 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12242 default: abort ();
351f65ca 12243 }
aebf9be7
NC
12244
12245 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12246 {
aebf9be7
NC
12247 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12248 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12249 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12250 default: abort ();
351f65ca
L
12251 }
12252
12253 bfd_set_error (bfd_error_wrong_format);
4eca0228 12254 _bfd_error_handler
695344c0 12255 /* xgettext:c-format */
871b3ab2 12256 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12257 sub, iclass, oclass);
12258 }
12259
12260 goto error_return;
12261 }
c152c796
AM
12262 }
12263 }
12264 }
12265
c0f00686
L
12266 /* Free symbol buffer if needed. */
12267 if (!info->reduce_memory_overheads)
12268 {
c72f2fb2 12269 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12270 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12271 && elf_tdata (sub)->symbuf)
c0f00686
L
12272 {
12273 free (elf_tdata (sub)->symbuf);
12274 elf_tdata (sub)->symbuf = NULL;
12275 }
12276 }
12277
c152c796
AM
12278 /* Output any global symbols that got converted to local in a
12279 version script or due to symbol visibility. We do this in a
12280 separate step since ELF requires all local symbols to appear
12281 prior to any global symbols. FIXME: We should only do this if
12282 some global symbols were, in fact, converted to become local.
12283 FIXME: Will this work correctly with the Irix 5 linker? */
12284 eoinfo.failed = FALSE;
8b127cbc 12285 eoinfo.flinfo = &flinfo;
c152c796 12286 eoinfo.localsyms = TRUE;
34a79995 12287 eoinfo.file_sym_done = FALSE;
7686d77d 12288 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12289 if (eoinfo.failed)
12290 return FALSE;
12291
4e617b1e
PB
12292 /* If backend needs to output some local symbols not present in the hash
12293 table, do it now. */
8539e4e8
AM
12294 if (bed->elf_backend_output_arch_local_syms
12295 && (info->strip != strip_all || emit_relocs))
4e617b1e 12296 {
6e0b88f1 12297 typedef int (*out_sym_func)
4e617b1e
PB
12298 (void *, const char *, Elf_Internal_Sym *, asection *,
12299 struct elf_link_hash_entry *);
12300
12301 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12302 (abfd, info, &flinfo,
12303 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12304 return FALSE;
12305 }
12306
c152c796
AM
12307 /* That wrote out all the local symbols. Finish up the symbol table
12308 with the global symbols. Even if we want to strip everything we
12309 can, we still need to deal with those global symbols that got
12310 converted to local in a version script. */
12311
12312 /* The sh_info field records the index of the first non local symbol. */
12313 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12314
12315 if (dynamic
64f52338
AM
12316 && htab->dynsym != NULL
12317 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12318 {
12319 Elf_Internal_Sym sym;
64f52338 12320 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12321
64f52338
AM
12322 o = htab->dynsym->output_section;
12323 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12324
12325 /* Write out the section symbols for the output sections. */
0e1862bb 12326 if (bfd_link_pic (info)
64f52338 12327 || htab->is_relocatable_executable)
c152c796
AM
12328 {
12329 asection *s;
12330
12331 sym.st_size = 0;
12332 sym.st_name = 0;
12333 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12334 sym.st_other = 0;
35fc36a8 12335 sym.st_target_internal = 0;
c152c796
AM
12336
12337 for (s = abfd->sections; s != NULL; s = s->next)
12338 {
12339 int indx;
12340 bfd_byte *dest;
12341 long dynindx;
12342
c152c796 12343 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12344 if (dynindx <= 0)
12345 continue;
12346 indx = elf_section_data (s)->this_idx;
c152c796
AM
12347 BFD_ASSERT (indx > 0);
12348 sym.st_shndx = indx;
c0d5a53d
L
12349 if (! check_dynsym (abfd, &sym))
12350 return FALSE;
c152c796
AM
12351 sym.st_value = s->vma;
12352 dest = dynsym + dynindx * bed->s->sizeof_sym;
12353 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12354 }
c152c796
AM
12355 }
12356
12357 /* Write out the local dynsyms. */
64f52338 12358 if (htab->dynlocal)
c152c796
AM
12359 {
12360 struct elf_link_local_dynamic_entry *e;
64f52338 12361 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12362 {
12363 asection *s;
12364 bfd_byte *dest;
12365
935bd1e0 12366 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12367 Note that we saved a word of storage and overwrote
12368 the original st_name with the dynstr_index. */
12369 sym = e->isym;
935bd1e0 12370 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12371
cb33740c
AM
12372 s = bfd_section_from_elf_index (e->input_bfd,
12373 e->isym.st_shndx);
12374 if (s != NULL)
c152c796 12375 {
c152c796
AM
12376 sym.st_shndx =
12377 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12378 if (! check_dynsym (abfd, &sym))
12379 return FALSE;
c152c796
AM
12380 sym.st_value = (s->output_section->vma
12381 + s->output_offset
12382 + e->isym.st_value);
12383 }
12384
c152c796
AM
12385 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12386 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12387 }
12388 }
c152c796
AM
12389 }
12390
12391 /* We get the global symbols from the hash table. */
12392 eoinfo.failed = FALSE;
12393 eoinfo.localsyms = FALSE;
8b127cbc 12394 eoinfo.flinfo = &flinfo;
7686d77d 12395 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12396 if (eoinfo.failed)
12397 return FALSE;
12398
12399 /* If backend needs to output some symbols not present in the hash
12400 table, do it now. */
8539e4e8
AM
12401 if (bed->elf_backend_output_arch_syms
12402 && (info->strip != strip_all || emit_relocs))
c152c796 12403 {
6e0b88f1 12404 typedef int (*out_sym_func)
c152c796
AM
12405 (void *, const char *, Elf_Internal_Sym *, asection *,
12406 struct elf_link_hash_entry *);
12407
12408 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12409 (abfd, info, &flinfo,
12410 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12411 return FALSE;
12412 }
12413
ef10c3ac
L
12414 /* Finalize the .strtab section. */
12415 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12416
12417 /* Swap out the .strtab section. */
12418 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12419 return FALSE;
12420
12421 /* Now we know the size of the symtab section. */
c152c796
AM
12422 if (bfd_get_symcount (abfd) > 0)
12423 {
ee3b52e9
L
12424 /* Finish up and write out the symbol string table (.strtab)
12425 section. */
ad32986f 12426 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12427 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12428
ad32986f 12429 if (elf_symtab_shndx_list (abfd))
8539e4e8 12430 {
ad32986f 12431 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12432
ad32986f
NC
12433 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12434 {
12435 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12436 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12437 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12438 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12439 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12440
ad32986f
NC
12441 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12442 off, TRUE);
12443
12444 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12445 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12446 return FALSE;
12447 }
8539e4e8 12448 }
ee3b52e9
L
12449
12450 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12451 /* sh_name was set in prep_headers. */
12452 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12453 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12454 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12455 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12456 symstrtab_hdr->sh_entsize = 0;
12457 symstrtab_hdr->sh_link = 0;
12458 symstrtab_hdr->sh_info = 0;
12459 /* sh_offset is set just below. */
12460 symstrtab_hdr->sh_addralign = 1;
12461
12462 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12463 off, TRUE);
12464 elf_next_file_pos (abfd) = off;
12465
c152c796 12466 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12467 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12468 return FALSE;
12469 }
12470
76359541
TP
12471 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12472 {
871b3ab2 12473 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12474 info->out_implib_bfd);
76359541
TP
12475 return FALSE;
12476 }
12477
c152c796
AM
12478 /* Adjust the relocs to have the correct symbol indices. */
12479 for (o = abfd->sections; o != NULL; o = o->next)
12480 {
d4730f92 12481 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12482 bfd_boolean sort;
10bbbc1d 12483
c152c796
AM
12484 if ((o->flags & SEC_RELOC) == 0)
12485 continue;
12486
28dbcedc 12487 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12488 if (esdo->rel.hdr != NULL
10bbbc1d 12489 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12490 return FALSE;
12491 if (esdo->rela.hdr != NULL
10bbbc1d 12492 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12493 return FALSE;
c152c796
AM
12494
12495 /* Set the reloc_count field to 0 to prevent write_relocs from
12496 trying to swap the relocs out itself. */
12497 o->reloc_count = 0;
12498 }
12499
12500 if (dynamic && info->combreloc && dynobj != NULL)
12501 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12502
12503 /* If we are linking against a dynamic object, or generating a
12504 shared library, finish up the dynamic linking information. */
12505 if (dynamic)
12506 {
12507 bfd_byte *dyncon, *dynconend;
12508
12509 /* Fix up .dynamic entries. */
3d4d4302 12510 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12511 BFD_ASSERT (o != NULL);
12512
12513 dyncon = o->contents;
eea6121a 12514 dynconend = o->contents + o->size;
c152c796
AM
12515 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12516 {
12517 Elf_Internal_Dyn dyn;
12518 const char *name;
12519 unsigned int type;
64487780
AM
12520 bfd_size_type sh_size;
12521 bfd_vma sh_addr;
c152c796
AM
12522
12523 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12524
12525 switch (dyn.d_tag)
12526 {
12527 default:
12528 continue;
12529 case DT_NULL:
12530 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12531 {
12532 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12533 {
12534 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12535 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12536 default: continue;
12537 }
12538 dyn.d_un.d_val = relativecount;
12539 relativecount = 0;
12540 break;
12541 }
12542 continue;
12543
12544 case DT_INIT:
12545 name = info->init_function;
12546 goto get_sym;
12547 case DT_FINI:
12548 name = info->fini_function;
12549 get_sym:
12550 {
12551 struct elf_link_hash_entry *h;
12552
64f52338 12553 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12554 if (h != NULL
12555 && (h->root.type == bfd_link_hash_defined
12556 || h->root.type == bfd_link_hash_defweak))
12557 {
bef26483 12558 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12559 o = h->root.u.def.section;
12560 if (o->output_section != NULL)
bef26483 12561 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12562 + o->output_offset);
12563 else
12564 {
12565 /* The symbol is imported from another shared
12566 library and does not apply to this one. */
bef26483 12567 dyn.d_un.d_ptr = 0;
c152c796
AM
12568 }
12569 break;
12570 }
12571 }
12572 continue;
12573
12574 case DT_PREINIT_ARRAYSZ:
12575 name = ".preinit_array";
4ade44b7 12576 goto get_out_size;
c152c796
AM
12577 case DT_INIT_ARRAYSZ:
12578 name = ".init_array";
4ade44b7 12579 goto get_out_size;
c152c796
AM
12580 case DT_FINI_ARRAYSZ:
12581 name = ".fini_array";
4ade44b7 12582 get_out_size:
c152c796
AM
12583 o = bfd_get_section_by_name (abfd, name);
12584 if (o == NULL)
12585 {
4eca0228 12586 _bfd_error_handler
4ade44b7 12587 (_("could not find section %s"), name);
c152c796
AM
12588 goto error_return;
12589 }
eea6121a 12590 if (o->size == 0)
4eca0228 12591 _bfd_error_handler
c152c796 12592 (_("warning: %s section has zero size"), name);
eea6121a 12593 dyn.d_un.d_val = o->size;
c152c796
AM
12594 break;
12595
12596 case DT_PREINIT_ARRAY:
12597 name = ".preinit_array";
4ade44b7 12598 goto get_out_vma;
c152c796
AM
12599 case DT_INIT_ARRAY:
12600 name = ".init_array";
4ade44b7 12601 goto get_out_vma;
c152c796
AM
12602 case DT_FINI_ARRAY:
12603 name = ".fini_array";
4ade44b7
AM
12604 get_out_vma:
12605 o = bfd_get_section_by_name (abfd, name);
12606 goto do_vma;
c152c796
AM
12607
12608 case DT_HASH:
12609 name = ".hash";
12610 goto get_vma;
fdc90cb4
JJ
12611 case DT_GNU_HASH:
12612 name = ".gnu.hash";
12613 goto get_vma;
c152c796
AM
12614 case DT_STRTAB:
12615 name = ".dynstr";
12616 goto get_vma;
12617 case DT_SYMTAB:
12618 name = ".dynsym";
12619 goto get_vma;
12620 case DT_VERDEF:
12621 name = ".gnu.version_d";
12622 goto get_vma;
12623 case DT_VERNEED:
12624 name = ".gnu.version_r";
12625 goto get_vma;
12626 case DT_VERSYM:
12627 name = ".gnu.version";
12628 get_vma:
4ade44b7
AM
12629 o = bfd_get_linker_section (dynobj, name);
12630 do_vma:
b3293efa 12631 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12632 {
4eca0228 12633 _bfd_error_handler
4ade44b7 12634 (_("could not find section %s"), name);
c152c796
AM
12635 goto error_return;
12636 }
894891db
NC
12637 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12638 {
4eca0228 12639 _bfd_error_handler
894891db
NC
12640 (_("warning: section '%s' is being made into a note"), name);
12641 bfd_set_error (bfd_error_nonrepresentable_section);
12642 goto error_return;
12643 }
4ade44b7 12644 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12645 break;
12646
12647 case DT_REL:
12648 case DT_RELA:
12649 case DT_RELSZ:
12650 case DT_RELASZ:
12651 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12652 type = SHT_REL;
12653 else
12654 type = SHT_RELA;
64487780
AM
12655 sh_size = 0;
12656 sh_addr = 0;
c152c796
AM
12657 for (i = 1; i < elf_numsections (abfd); i++)
12658 {
12659 Elf_Internal_Shdr *hdr;
12660
12661 hdr = elf_elfsections (abfd)[i];
12662 if (hdr->sh_type == type
12663 && (hdr->sh_flags & SHF_ALLOC) != 0)
12664 {
64487780
AM
12665 sh_size += hdr->sh_size;
12666 if (sh_addr == 0
12667 || sh_addr > hdr->sh_addr)
12668 sh_addr = hdr->sh_addr;
c152c796
AM
12669 }
12670 }
64487780 12671
64f52338
AM
12672 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12673 {
12674 /* Don't count procedure linkage table relocs in the
12675 overall reloc count. */
64487780
AM
12676 sh_size -= htab->srelplt->size;
12677 if (sh_size == 0)
12678 /* If the size is zero, make the address zero too.
12679 This is to avoid a glibc bug. If the backend
12680 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12681 zero, then we'll put DT_RELA at the end of
12682 DT_JMPREL. glibc will interpret the end of
12683 DT_RELA matching the end of DT_JMPREL as the
12684 case where DT_RELA includes DT_JMPREL, and for
12685 LD_BIND_NOW will decide that processing DT_RELA
12686 will process the PLT relocs too. Net result:
12687 No PLT relocs applied. */
12688 sh_addr = 0;
12689
64f52338
AM
12690 /* If .rela.plt is the first .rela section, exclude
12691 it from DT_RELA. */
64487780
AM
12692 else if (sh_addr == (htab->srelplt->output_section->vma
12693 + htab->srelplt->output_offset))
12694 sh_addr += htab->srelplt->size;
64f52338 12695 }
64487780
AM
12696
12697 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12698 dyn.d_un.d_val = sh_size;
12699 else
12700 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12701 break;
12702 }
12703 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12704 }
12705 }
12706
12707 /* If we have created any dynamic sections, then output them. */
12708 if (dynobj != NULL)
12709 {
12710 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12711 goto error_return;
12712
943284cc 12713 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12714 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12715 || info->error_textrel)
3d4d4302 12716 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12717 {
12718 bfd_byte *dyncon, *dynconend;
12719
943284cc
DJ
12720 dyncon = o->contents;
12721 dynconend = o->contents + o->size;
12722 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12723 {
12724 Elf_Internal_Dyn dyn;
12725
12726 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12727
12728 if (dyn.d_tag == DT_TEXTREL)
12729 {
c192a133
AM
12730 if (info->error_textrel)
12731 info->callbacks->einfo
9793eb77 12732 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12733 else
12734 info->callbacks->einfo
9793eb77 12735 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12736 break;
12737 }
12738 }
12739 }
12740
c152c796
AM
12741 for (o = dynobj->sections; o != NULL; o = o->next)
12742 {
12743 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12744 || o->size == 0
c152c796
AM
12745 || o->output_section == bfd_abs_section_ptr)
12746 continue;
12747 if ((o->flags & SEC_LINKER_CREATED) == 0)
12748 {
12749 /* At this point, we are only interested in sections
12750 created by _bfd_elf_link_create_dynamic_sections. */
12751 continue;
12752 }
64f52338 12753 if (htab->stab_info.stabstr == o)
3722b82f 12754 continue;
64f52338 12755 if (htab->eh_info.hdr_sec == o)
eea6121a 12756 continue;
3d4d4302 12757 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12758 {
12759 if (! bfd_set_section_contents (abfd, o->output_section,
12760 o->contents,
37b01f6a
DG
12761 (file_ptr) o->output_offset
12762 * bfd_octets_per_byte (abfd),
eea6121a 12763 o->size))
c152c796
AM
12764 goto error_return;
12765 }
12766 else
12767 {
12768 /* The contents of the .dynstr section are actually in a
12769 stringtab. */
8539e4e8
AM
12770 file_ptr off;
12771
c152c796
AM
12772 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12773 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12774 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12775 goto error_return;
12776 }
12777 }
12778 }
12779
7bdf4127 12780 if (!info->resolve_section_groups)
c152c796
AM
12781 {
12782 bfd_boolean failed = FALSE;
12783
7bdf4127 12784 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12785 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12786 if (failed)
12787 goto error_return;
12788 }
12789
12790 /* If we have optimized stabs strings, output them. */
64f52338 12791 if (htab->stab_info.stabstr != NULL)
c152c796 12792 {
64f52338 12793 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12794 goto error_return;
12795 }
12796
9f7c3e5e
AM
12797 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12798 goto error_return;
c152c796 12799
9f7c3e5e 12800 elf_final_link_free (abfd, &flinfo);
c152c796 12801
12bd6957 12802 elf_linker (abfd) = TRUE;
c152c796 12803
104d59d1
JM
12804 if (attr_section)
12805 {
a50b1753 12806 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12807 if (contents == NULL)
d0f16d5e 12808 return FALSE; /* Bail out and fail. */
104d59d1
JM
12809 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12810 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12811 free (contents);
12812 }
12813
c152c796
AM
12814 return TRUE;
12815
12816 error_return:
9f7c3e5e 12817 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12818 return FALSE;
12819}
12820\f
5241d853
RS
12821/* Initialize COOKIE for input bfd ABFD. */
12822
12823static bfd_boolean
12824init_reloc_cookie (struct elf_reloc_cookie *cookie,
12825 struct bfd_link_info *info, bfd *abfd)
12826{
12827 Elf_Internal_Shdr *symtab_hdr;
12828 const struct elf_backend_data *bed;
12829
12830 bed = get_elf_backend_data (abfd);
12831 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12832
12833 cookie->abfd = abfd;
12834 cookie->sym_hashes = elf_sym_hashes (abfd);
12835 cookie->bad_symtab = elf_bad_symtab (abfd);
12836 if (cookie->bad_symtab)
12837 {
12838 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12839 cookie->extsymoff = 0;
12840 }
12841 else
12842 {
12843 cookie->locsymcount = symtab_hdr->sh_info;
12844 cookie->extsymoff = symtab_hdr->sh_info;
12845 }
12846
12847 if (bed->s->arch_size == 32)
12848 cookie->r_sym_shift = 8;
12849 else
12850 cookie->r_sym_shift = 32;
12851
12852 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12853 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12854 {
12855 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12856 cookie->locsymcount, 0,
12857 NULL, NULL, NULL);
12858 if (cookie->locsyms == NULL)
12859 {
12860 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12861 return FALSE;
12862 }
12863 if (info->keep_memory)
12864 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12865 }
12866 return TRUE;
12867}
12868
12869/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12870
12871static void
12872fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12873{
12874 Elf_Internal_Shdr *symtab_hdr;
12875
12876 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12877 if (cookie->locsyms != NULL
12878 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12879 free (cookie->locsyms);
12880}
12881
12882/* Initialize the relocation information in COOKIE for input section SEC
12883 of input bfd ABFD. */
12884
12885static bfd_boolean
12886init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12887 struct bfd_link_info *info, bfd *abfd,
12888 asection *sec)
12889{
5241d853
RS
12890 if (sec->reloc_count == 0)
12891 {
12892 cookie->rels = NULL;
12893 cookie->relend = NULL;
12894 }
12895 else
12896 {
5241d853
RS
12897 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12898 info->keep_memory);
12899 if (cookie->rels == NULL)
12900 return FALSE;
12901 cookie->rel = cookie->rels;
056bafd4 12902 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12903 }
12904 cookie->rel = cookie->rels;
12905 return TRUE;
12906}
12907
12908/* Free the memory allocated by init_reloc_cookie_rels,
12909 if appropriate. */
12910
12911static void
12912fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12913 asection *sec)
12914{
12915 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12916 free (cookie->rels);
12917}
12918
12919/* Initialize the whole of COOKIE for input section SEC. */
12920
12921static bfd_boolean
12922init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12923 struct bfd_link_info *info,
12924 asection *sec)
12925{
12926 if (!init_reloc_cookie (cookie, info, sec->owner))
12927 goto error1;
12928 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12929 goto error2;
12930 return TRUE;
12931
12932 error2:
12933 fini_reloc_cookie (cookie, sec->owner);
12934 error1:
12935 return FALSE;
12936}
12937
12938/* Free the memory allocated by init_reloc_cookie_for_section,
12939 if appropriate. */
12940
12941static void
12942fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12943 asection *sec)
12944{
12945 fini_reloc_cookie_rels (cookie, sec);
12946 fini_reloc_cookie (cookie, sec->owner);
12947}
12948\f
c152c796
AM
12949/* Garbage collect unused sections. */
12950
07adf181
AM
12951/* Default gc_mark_hook. */
12952
12953asection *
12954_bfd_elf_gc_mark_hook (asection *sec,
12955 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12956 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12957 struct elf_link_hash_entry *h,
12958 Elf_Internal_Sym *sym)
12959{
12960 if (h != NULL)
12961 {
12962 switch (h->root.type)
12963 {
12964 case bfd_link_hash_defined:
12965 case bfd_link_hash_defweak:
12966 return h->root.u.def.section;
12967
12968 case bfd_link_hash_common:
12969 return h->root.u.c.p->section;
12970
12971 default:
12972 break;
12973 }
12974 }
12975 else
12976 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12977
12978 return NULL;
12979}
12980
9e223787 12981/* Return the debug definition section. */
b7c871ed
L
12982
12983static asection *
12984elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12985 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12986 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12987 struct elf_link_hash_entry *h,
9e223787 12988 Elf_Internal_Sym *sym)
b7c871ed 12989{
9e223787
L
12990 if (h != NULL)
12991 {
12992 /* Return the global debug definition section. */
12993 if ((h->root.type == bfd_link_hash_defined
12994 || h->root.type == bfd_link_hash_defweak)
12995 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12996 return h->root.u.def.section;
12997 }
12998 else
12999 {
13000 /* Return the local debug definition section. */
13001 asection *isec = bfd_section_from_elf_index (sec->owner,
13002 sym->st_shndx);
13003 if ((isec->flags & SEC_DEBUGGING) != 0)
13004 return isec;
13005 }
b7c871ed
L
13006
13007 return NULL;
13008}
13009
5241d853
RS
13010/* COOKIE->rel describes a relocation against section SEC, which is
13011 a section we've decided to keep. Return the section that contains
13012 the relocation symbol, or NULL if no section contains it. */
13013
13014asection *
13015_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13016 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13017 struct elf_reloc_cookie *cookie,
13018 bfd_boolean *start_stop)
5241d853
RS
13019{
13020 unsigned long r_symndx;
13021 struct elf_link_hash_entry *h;
13022
13023 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13024 if (r_symndx == STN_UNDEF)
5241d853
RS
13025 return NULL;
13026
13027 if (r_symndx >= cookie->locsymcount
13028 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13029 {
13030 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13031 if (h == NULL)
13032 {
871b3ab2 13033 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13034 sec->owner);
13035 return NULL;
13036 }
5241d853
RS
13037 while (h->root.type == bfd_link_hash_indirect
13038 || h->root.type == bfd_link_hash_warning)
13039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13040 h->mark = 1;
4e6b54a6
AM
13041 /* If this symbol is weak and there is a non-weak definition, we
13042 keep the non-weak definition because many backends put
13043 dynamic reloc info on the non-weak definition for code
13044 handling copy relocs. */
60d67dc8
AM
13045 if (h->is_weakalias)
13046 weakdef (h)->mark = 1;
1cce69b9 13047
a6a4679f 13048 if (start_stop != NULL)
1cce69b9 13049 {
7dba9362
AM
13050 /* To work around a glibc bug, mark XXX input sections
13051 when there is a reference to __start_XXX or __stop_XXX
13052 symbols. */
cbd0eecf 13053 if (h->start_stop)
1cce69b9 13054 {
cbd0eecf 13055 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13056 *start_stop = !s->gc_mark;
13057 return s;
1cce69b9
AM
13058 }
13059 }
13060
5241d853
RS
13061 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13062 }
13063
13064 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13065 &cookie->locsyms[r_symndx]);
13066}
13067
13068/* COOKIE->rel describes a relocation against section SEC, which is
13069 a section we've decided to keep. Mark the section that contains
9d0a14d3 13070 the relocation symbol. */
5241d853
RS
13071
13072bfd_boolean
13073_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13074 asection *sec,
13075 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13076 struct elf_reloc_cookie *cookie)
5241d853
RS
13077{
13078 asection *rsec;
1cce69b9 13079 bfd_boolean start_stop = FALSE;
5241d853 13080
1cce69b9
AM
13081 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13082 while (rsec != NULL)
5241d853 13083 {
1cce69b9
AM
13084 if (!rsec->gc_mark)
13085 {
13086 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13087 || (rsec->owner->flags & DYNAMIC) != 0)
13088 rsec->gc_mark = 1;
13089 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13090 return FALSE;
13091 }
13092 if (!start_stop)
13093 break;
199af150 13094 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13095 }
13096 return TRUE;
13097}
13098
07adf181
AM
13099/* The mark phase of garbage collection. For a given section, mark
13100 it and any sections in this section's group, and all the sections
13101 which define symbols to which it refers. */
13102
ccfa59ea
AM
13103bfd_boolean
13104_bfd_elf_gc_mark (struct bfd_link_info *info,
13105 asection *sec,
6a5bb875 13106 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13107{
13108 bfd_boolean ret;
9d0a14d3 13109 asection *group_sec, *eh_frame;
c152c796
AM
13110
13111 sec->gc_mark = 1;
13112
13113 /* Mark all the sections in the group. */
13114 group_sec = elf_section_data (sec)->next_in_group;
13115 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13116 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13117 return FALSE;
13118
13119 /* Look through the section relocs. */
13120 ret = TRUE;
9d0a14d3
RS
13121 eh_frame = elf_eh_frame_section (sec->owner);
13122 if ((sec->flags & SEC_RELOC) != 0
13123 && sec->reloc_count > 0
13124 && sec != eh_frame)
c152c796 13125 {
5241d853 13126 struct elf_reloc_cookie cookie;
c152c796 13127
5241d853
RS
13128 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13129 ret = FALSE;
c152c796 13130 else
c152c796 13131 {
5241d853 13132 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13133 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13134 {
13135 ret = FALSE;
13136 break;
13137 }
13138 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13139 }
13140 }
9d0a14d3
RS
13141
13142 if (ret && eh_frame && elf_fde_list (sec))
13143 {
13144 struct elf_reloc_cookie cookie;
13145
13146 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13147 ret = FALSE;
13148 else
13149 {
13150 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13151 gc_mark_hook, &cookie))
13152 ret = FALSE;
13153 fini_reloc_cookie_for_section (&cookie, eh_frame);
13154 }
13155 }
13156
2f0c68f2
CM
13157 eh_frame = elf_section_eh_frame_entry (sec);
13158 if (ret && eh_frame && !eh_frame->gc_mark)
13159 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13160 ret = FALSE;
13161
c152c796
AM
13162 return ret;
13163}
13164
3c758495
TG
13165/* Scan and mark sections in a special or debug section group. */
13166
13167static void
13168_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13169{
13170 /* Point to first section of section group. */
13171 asection *ssec;
13172 /* Used to iterate the section group. */
13173 asection *msec;
13174
13175 bfd_boolean is_special_grp = TRUE;
13176 bfd_boolean is_debug_grp = TRUE;
13177
13178 /* First scan to see if group contains any section other than debug
13179 and special section. */
13180 ssec = msec = elf_next_in_group (grp);
13181 do
13182 {
13183 if ((msec->flags & SEC_DEBUGGING) == 0)
13184 is_debug_grp = FALSE;
13185
13186 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13187 is_special_grp = FALSE;
13188
13189 msec = elf_next_in_group (msec);
13190 }
13191 while (msec != ssec);
13192
13193 /* If this is a pure debug section group or pure special section group,
13194 keep all sections in this group. */
13195 if (is_debug_grp || is_special_grp)
13196 {
13197 do
13198 {
13199 msec->gc_mark = 1;
13200 msec = elf_next_in_group (msec);
13201 }
13202 while (msec != ssec);
13203 }
13204}
13205
7f6ab9f8
AM
13206/* Keep debug and special sections. */
13207
13208bfd_boolean
13209_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13210 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13211{
13212 bfd *ibfd;
13213
c72f2fb2 13214 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13215 {
13216 asection *isec;
13217 bfd_boolean some_kept;
b40bf0a2 13218 bfd_boolean debug_frag_seen;
b7c871ed 13219 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13220
13221 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13222 continue;
57963c05
AM
13223 isec = ibfd->sections;
13224 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13225 continue;
7f6ab9f8 13226
b40bf0a2
NC
13227 /* Ensure all linker created sections are kept,
13228 see if any other section is already marked,
13229 and note if we have any fragmented debug sections. */
b7c871ed 13230 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13231 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13232 {
13233 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13234 isec->gc_mark = 1;
eb026f09
AM
13235 else if (isec->gc_mark
13236 && (isec->flags & SEC_ALLOC) != 0
13237 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13238 some_kept = TRUE;
b40bf0a2 13239
535b785f 13240 if (!debug_frag_seen
b40bf0a2
NC
13241 && (isec->flags & SEC_DEBUGGING)
13242 && CONST_STRNEQ (isec->name, ".debug_line."))
13243 debug_frag_seen = TRUE;
7f6ab9f8
AM
13244 }
13245
eb026f09
AM
13246 /* If no non-note alloc section in this file will be kept, then
13247 we can toss out the debug and special sections. */
7f6ab9f8
AM
13248 if (!some_kept)
13249 continue;
13250
13251 /* Keep debug and special sections like .comment when they are
3c758495
TG
13252 not part of a group. Also keep section groups that contain
13253 just debug sections or special sections. */
7f6ab9f8 13254 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13255 {
13256 if ((isec->flags & SEC_GROUP) != 0)
13257 _bfd_elf_gc_mark_debug_special_section_group (isec);
13258 else if (((isec->flags & SEC_DEBUGGING) != 0
13259 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13260 && elf_next_in_group (isec) == NULL)
13261 isec->gc_mark = 1;
b7c871ed
L
13262 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13263 has_kept_debug_info = TRUE;
3c758495 13264 }
b40bf0a2 13265
b40bf0a2
NC
13266 /* Look for CODE sections which are going to be discarded,
13267 and find and discard any fragmented debug sections which
13268 are associated with that code section. */
b7c871ed
L
13269 if (debug_frag_seen)
13270 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13271 if ((isec->flags & SEC_CODE) != 0
13272 && isec->gc_mark == 0)
13273 {
13274 unsigned int ilen;
13275 asection *dsec;
b40bf0a2 13276
b7c871ed 13277 ilen = strlen (isec->name);
b40bf0a2 13278
b7c871ed 13279 /* Association is determined by the name of the debug
07d6d2b8 13280 section containing the name of the code section as
b7c871ed
L
13281 a suffix. For example .debug_line.text.foo is a
13282 debug section associated with .text.foo. */
13283 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13284 {
13285 unsigned int dlen;
b40bf0a2 13286
b7c871ed
L
13287 if (dsec->gc_mark == 0
13288 || (dsec->flags & SEC_DEBUGGING) == 0)
13289 continue;
b40bf0a2 13290
b7c871ed 13291 dlen = strlen (dsec->name);
b40bf0a2 13292
b7c871ed
L
13293 if (dlen > ilen
13294 && strncmp (dsec->name + (dlen - ilen),
13295 isec->name, ilen) == 0)
b40bf0a2 13296 dsec->gc_mark = 0;
b7c871ed 13297 }
b40bf0a2 13298 }
b7c871ed
L
13299
13300 /* Mark debug sections referenced by kept debug sections. */
13301 if (has_kept_debug_info)
13302 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13303 if (isec->gc_mark
13304 && (isec->flags & SEC_DEBUGGING) != 0)
13305 if (!_bfd_elf_gc_mark (info, isec,
13306 elf_gc_mark_debug_section))
13307 return FALSE;
7f6ab9f8
AM
13308 }
13309 return TRUE;
13310}
13311
c152c796 13312static bfd_boolean
ccabcbe5 13313elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13314{
13315 bfd *sub;
ccabcbe5 13316 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13317
c72f2fb2 13318 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13319 {
13320 asection *o;
13321
b19a8f85 13322 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13323 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13324 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13325 continue;
57963c05
AM
13326 o = sub->sections;
13327 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13328 continue;
c152c796
AM
13329
13330 for (o = sub->sections; o != NULL; o = o->next)
13331 {
a33dafc3
L
13332 /* When any section in a section group is kept, we keep all
13333 sections in the section group. If the first member of
13334 the section group is excluded, we will also exclude the
13335 group section. */
13336 if (o->flags & SEC_GROUP)
13337 {
13338 asection *first = elf_next_in_group (o);
13339 o->gc_mark = first->gc_mark;
13340 }
c152c796 13341
1e7eae0d 13342 if (o->gc_mark)
c152c796
AM
13343 continue;
13344
13345 /* Skip sweeping sections already excluded. */
13346 if (o->flags & SEC_EXCLUDE)
13347 continue;
13348
13349 /* Since this is early in the link process, it is simple
13350 to remove a section from the output. */
13351 o->flags |= SEC_EXCLUDE;
13352
c55fe096 13353 if (info->print_gc_sections && o->size != 0)
695344c0 13354 /* xgettext:c-format */
9793eb77 13355 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13356 o, sub);
c152c796
AM
13357 }
13358 }
13359
c152c796
AM
13360 return TRUE;
13361}
13362
13363/* Propagate collected vtable information. This is called through
13364 elf_link_hash_traverse. */
13365
13366static bfd_boolean
13367elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13368{
c152c796 13369 /* Those that are not vtables. */
cbd0eecf
L
13370 if (h->start_stop
13371 || h->u2.vtable == NULL
13372 || h->u2.vtable->parent == NULL)
c152c796
AM
13373 return TRUE;
13374
13375 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13376 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13377 return TRUE;
13378
13379 /* If we've already been done, exit. */
cbd0eecf 13380 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13381 return TRUE;
13382
13383 /* Make sure the parent's table is up to date. */
cbd0eecf 13384 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13385
cbd0eecf 13386 if (h->u2.vtable->used == NULL)
c152c796
AM
13387 {
13388 /* None of this table's entries were referenced. Re-use the
13389 parent's table. */
cbd0eecf
L
13390 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13391 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13392 }
13393 else
13394 {
13395 size_t n;
13396 bfd_boolean *cu, *pu;
13397
13398 /* Or the parent's entries into ours. */
cbd0eecf 13399 cu = h->u2.vtable->used;
c152c796 13400 cu[-1] = TRUE;
cbd0eecf 13401 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13402 if (pu != NULL)
13403 {
13404 const struct elf_backend_data *bed;
13405 unsigned int log_file_align;
13406
13407 bed = get_elf_backend_data (h->root.u.def.section->owner);
13408 log_file_align = bed->s->log_file_align;
cbd0eecf 13409 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13410 while (n--)
13411 {
13412 if (*pu)
13413 *cu = TRUE;
13414 pu++;
13415 cu++;
13416 }
13417 }
13418 }
13419
13420 return TRUE;
13421}
13422
13423static bfd_boolean
13424elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13425{
13426 asection *sec;
13427 bfd_vma hstart, hend;
13428 Elf_Internal_Rela *relstart, *relend, *rel;
13429 const struct elf_backend_data *bed;
13430 unsigned int log_file_align;
13431
c152c796
AM
13432 /* Take care of both those symbols that do not describe vtables as
13433 well as those that are not loaded. */
cbd0eecf
L
13434 if (h->start_stop
13435 || h->u2.vtable == NULL
13436 || h->u2.vtable->parent == NULL)
c152c796
AM
13437 return TRUE;
13438
13439 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13440 || h->root.type == bfd_link_hash_defweak);
13441
13442 sec = h->root.u.def.section;
13443 hstart = h->root.u.def.value;
13444 hend = hstart + h->size;
13445
13446 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13447 if (!relstart)
13448 return *(bfd_boolean *) okp = FALSE;
13449 bed = get_elf_backend_data (sec->owner);
13450 log_file_align = bed->s->log_file_align;
13451
056bafd4 13452 relend = relstart + sec->reloc_count;
c152c796
AM
13453
13454 for (rel = relstart; rel < relend; ++rel)
13455 if (rel->r_offset >= hstart && rel->r_offset < hend)
13456 {
13457 /* If the entry is in use, do nothing. */
cbd0eecf
L
13458 if (h->u2.vtable->used
13459 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13460 {
13461 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13462 if (h->u2.vtable->used[entry])
c152c796
AM
13463 continue;
13464 }
13465 /* Otherwise, kill it. */
13466 rel->r_offset = rel->r_info = rel->r_addend = 0;
13467 }
13468
13469 return TRUE;
13470}
13471
87538722
AM
13472/* Mark sections containing dynamically referenced symbols. When
13473 building shared libraries, we must assume that any visible symbol is
13474 referenced. */
715df9b8 13475
64d03ab5
AM
13476bfd_boolean
13477bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13478{
87538722 13479 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13480 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13481
715df9b8
EB
13482 if ((h->root.type == bfd_link_hash_defined
13483 || h->root.type == bfd_link_hash_defweak)
d664fd41 13484 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13485 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13486 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13487 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13488 && (!bfd_link_executable (info)
22185505 13489 || info->gc_keep_exported
b407645f
AM
13490 || info->export_dynamic
13491 || (h->dynamic
13492 && d != NULL
13493 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13494 && (h->versioned >= versioned
54e8959c
L
13495 || !bfd_hide_sym_by_version (info->version_info,
13496 h->root.root.string)))))
715df9b8
EB
13497 h->root.u.def.section->flags |= SEC_KEEP;
13498
13499 return TRUE;
13500}
3b36f7e6 13501
74f0fb50
AM
13502/* Keep all sections containing symbols undefined on the command-line,
13503 and the section containing the entry symbol. */
13504
13505void
13506_bfd_elf_gc_keep (struct bfd_link_info *info)
13507{
13508 struct bfd_sym_chain *sym;
13509
13510 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13511 {
13512 struct elf_link_hash_entry *h;
13513
13514 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13515 FALSE, FALSE, FALSE);
13516
13517 if (h != NULL
13518 && (h->root.type == bfd_link_hash_defined
13519 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13520 && !bfd_is_abs_section (h->root.u.def.section)
13521 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13522 h->root.u.def.section->flags |= SEC_KEEP;
13523 }
13524}
13525
2f0c68f2
CM
13526bfd_boolean
13527bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13528 struct bfd_link_info *info)
13529{
13530 bfd *ibfd = info->input_bfds;
13531
13532 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13533 {
13534 asection *sec;
13535 struct elf_reloc_cookie cookie;
13536
13537 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13538 continue;
57963c05
AM
13539 sec = ibfd->sections;
13540 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13541 continue;
2f0c68f2
CM
13542
13543 if (!init_reloc_cookie (&cookie, info, ibfd))
13544 return FALSE;
13545
13546 for (sec = ibfd->sections; sec; sec = sec->next)
13547 {
13548 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13549 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13550 {
13551 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13552 fini_reloc_cookie_rels (&cookie, sec);
13553 }
13554 }
13555 }
13556 return TRUE;
13557}
13558
c152c796
AM
13559/* Do mark and sweep of unused sections. */
13560
13561bfd_boolean
13562bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13563{
13564 bfd_boolean ok = TRUE;
13565 bfd *sub;
6a5bb875 13566 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13567 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13568 struct elf_link_hash_table *htab;
c152c796 13569
64d03ab5 13570 if (!bed->can_gc_sections
715df9b8 13571 || !is_elf_hash_table (info->hash))
c152c796 13572 {
9793eb77 13573 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13574 return TRUE;
13575 }
13576
74f0fb50 13577 bed->gc_keep (info);
da44f4e5 13578 htab = elf_hash_table (info);
74f0fb50 13579
9d0a14d3
RS
13580 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13581 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13582 for (sub = info->input_bfds;
13583 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13584 sub = sub->link.next)
9d0a14d3
RS
13585 {
13586 asection *sec;
13587 struct elf_reloc_cookie cookie;
13588
57963c05
AM
13589 sec = sub->sections;
13590 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13591 continue;
9d0a14d3 13592 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13593 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13594 {
13595 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13596 if (elf_section_data (sec)->sec_info
13597 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13598 elf_eh_frame_section (sub) = sec;
13599 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13600 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13601 }
13602 }
9d0a14d3 13603
c152c796 13604 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13605 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13606 if (!ok)
13607 return FALSE;
13608
13609 /* Kill the vtable relocations that were not used. */
da44f4e5 13610 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13611 if (!ok)
13612 return FALSE;
13613
715df9b8 13614 /* Mark dynamically referenced symbols. */
22185505 13615 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13616 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13617
715df9b8 13618 /* Grovel through relocs to find out who stays ... */
64d03ab5 13619 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13620 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13621 {
13622 asection *o;
13623
b19a8f85 13624 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13625 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13626 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13627 continue;
13628
57963c05
AM
13629 o = sub->sections;
13630 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13631 continue;
13632
7f6ab9f8
AM
13633 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13634 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13635 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13636 well as FINI_ARRAY sections for ld -r. */
c152c796 13637 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13638 if (!o->gc_mark
13639 && (o->flags & SEC_EXCLUDE) == 0
24007750 13640 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13641 || (bfd_link_relocatable (info)
13642 && ((elf_section_data (o)->this_hdr.sh_type
13643 == SHT_PREINIT_ARRAY)
13644 || (elf_section_data (o)->this_hdr.sh_type
13645 == SHT_INIT_ARRAY)
13646 || (elf_section_data (o)->this_hdr.sh_type
13647 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13648 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13649 && elf_next_in_group (o) == NULL )))
13650 {
13651 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13652 return FALSE;
13653 }
c152c796
AM
13654 }
13655
6a5bb875 13656 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13657 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13658
c152c796 13659 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13660 return elf_gc_sweep (abfd, info);
c152c796
AM
13661}
13662\f
13663/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13664
13665bfd_boolean
13666bfd_elf_gc_record_vtinherit (bfd *abfd,
13667 asection *sec,
13668 struct elf_link_hash_entry *h,
13669 bfd_vma offset)
13670{
13671 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13672 struct elf_link_hash_entry **search, *child;
ef53be89 13673 size_t extsymcount;
c152c796
AM
13674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13675
13676 /* The sh_info field of the symtab header tells us where the
13677 external symbols start. We don't care about the local symbols at
13678 this point. */
13679 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13680 if (!elf_bad_symtab (abfd))
13681 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13682
13683 sym_hashes = elf_sym_hashes (abfd);
13684 sym_hashes_end = sym_hashes + extsymcount;
13685
13686 /* Hunt down the child symbol, which is in this section at the same
13687 offset as the relocation. */
13688 for (search = sym_hashes; search != sym_hashes_end; ++search)
13689 {
13690 if ((child = *search) != NULL
13691 && (child->root.type == bfd_link_hash_defined
13692 || child->root.type == bfd_link_hash_defweak)
13693 && child->root.u.def.section == sec
13694 && child->root.u.def.value == offset)
13695 goto win;
13696 }
13697
695344c0 13698 /* xgettext:c-format */
9793eb77 13699 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13700 abfd, sec, (uint64_t) offset);
c152c796
AM
13701 bfd_set_error (bfd_error_invalid_operation);
13702 return FALSE;
13703
13704 win:
cbd0eecf 13705 if (!child->u2.vtable)
f6e332e6 13706 {
cbd0eecf
L
13707 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13708 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13709 if (!child->u2.vtable)
f6e332e6
AM
13710 return FALSE;
13711 }
c152c796
AM
13712 if (!h)
13713 {
13714 /* This *should* only be the absolute section. It could potentially
13715 be that someone has defined a non-global vtable though, which
13716 would be bad. It isn't worth paging in the local symbols to be
13717 sure though; that case should simply be handled by the assembler. */
13718
cbd0eecf 13719 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13720 }
13721 else
cbd0eecf 13722 child->u2.vtable->parent = h;
c152c796
AM
13723
13724 return TRUE;
13725}
13726
13727/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13728
13729bfd_boolean
a0ea3a14 13730bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13731 struct elf_link_hash_entry *h,
13732 bfd_vma addend)
13733{
13734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13735 unsigned int log_file_align = bed->s->log_file_align;
13736
a0ea3a14
L
13737 if (!h)
13738 {
13739 /* xgettext:c-format */
13740 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13741 abfd, sec);
13742 bfd_set_error (bfd_error_bad_value);
13743 return FALSE;
13744 }
13745
cbd0eecf 13746 if (!h->u2.vtable)
f6e332e6 13747 {
cbd0eecf
L
13748 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13749 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13750 if (!h->u2.vtable)
f6e332e6
AM
13751 return FALSE;
13752 }
13753
cbd0eecf 13754 if (addend >= h->u2.vtable->size)
c152c796
AM
13755 {
13756 size_t size, bytes, file_align;
cbd0eecf 13757 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13758
13759 /* While the symbol is undefined, we have to be prepared to handle
13760 a zero size. */
13761 file_align = 1 << log_file_align;
13762 if (h->root.type == bfd_link_hash_undefined)
13763 size = addend + file_align;
13764 else
13765 {
13766 size = h->size;
13767 if (addend >= size)
13768 {
13769 /* Oops! We've got a reference past the defined end of
13770 the table. This is probably a bug -- shall we warn? */
13771 size = addend + file_align;
13772 }
13773 }
13774 size = (size + file_align - 1) & -file_align;
13775
13776 /* Allocate one extra entry for use as a "done" flag for the
13777 consolidation pass. */
13778 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13779
13780 if (ptr)
13781 {
a50b1753 13782 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13783
13784 if (ptr != NULL)
13785 {
13786 size_t oldbytes;
13787
cbd0eecf 13788 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13789 * sizeof (bfd_boolean));
13790 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13791 }
13792 }
13793 else
a50b1753 13794 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13795
13796 if (ptr == NULL)
13797 return FALSE;
13798
13799 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13800 h->u2.vtable->used = ptr + 1;
13801 h->u2.vtable->size = size;
c152c796
AM
13802 }
13803
cbd0eecf 13804 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13805
13806 return TRUE;
13807}
13808
ae17ab41
CM
13809/* Map an ELF section header flag to its corresponding string. */
13810typedef struct
13811{
13812 char *flag_name;
13813 flagword flag_value;
13814} elf_flags_to_name_table;
13815
13816static elf_flags_to_name_table elf_flags_to_names [] =
13817{
13818 { "SHF_WRITE", SHF_WRITE },
13819 { "SHF_ALLOC", SHF_ALLOC },
13820 { "SHF_EXECINSTR", SHF_EXECINSTR },
13821 { "SHF_MERGE", SHF_MERGE },
13822 { "SHF_STRINGS", SHF_STRINGS },
13823 { "SHF_INFO_LINK", SHF_INFO_LINK},
13824 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13825 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13826 { "SHF_GROUP", SHF_GROUP },
13827 { "SHF_TLS", SHF_TLS },
13828 { "SHF_MASKOS", SHF_MASKOS },
13829 { "SHF_EXCLUDE", SHF_EXCLUDE },
13830};
13831
b9c361e0
JL
13832/* Returns TRUE if the section is to be included, otherwise FALSE. */
13833bfd_boolean
ae17ab41 13834bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13835 struct flag_info *flaginfo,
b9c361e0 13836 asection *section)
ae17ab41 13837{
8b127cbc 13838 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13839
8b127cbc 13840 if (!flaginfo->flags_initialized)
ae17ab41 13841 {
8b127cbc
AM
13842 bfd *obfd = info->output_bfd;
13843 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13844 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13845 int with_hex = 0;
13846 int without_hex = 0;
13847
8b127cbc 13848 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13849 {
b9c361e0 13850 unsigned i;
8b127cbc 13851 flagword (*lookup) (char *);
ae17ab41 13852
8b127cbc
AM
13853 lookup = bed->elf_backend_lookup_section_flags_hook;
13854 if (lookup != NULL)
ae17ab41 13855 {
8b127cbc 13856 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13857
13858 if (hexval != 0)
13859 {
13860 if (tf->with == with_flags)
13861 with_hex |= hexval;
13862 else if (tf->with == without_flags)
13863 without_hex |= hexval;
13864 tf->valid = TRUE;
13865 continue;
13866 }
ae17ab41 13867 }
8b127cbc 13868 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13869 {
8b127cbc 13870 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13871 {
13872 if (tf->with == with_flags)
13873 with_hex |= elf_flags_to_names[i].flag_value;
13874 else if (tf->with == without_flags)
13875 without_hex |= elf_flags_to_names[i].flag_value;
13876 tf->valid = TRUE;
13877 break;
13878 }
13879 }
8b127cbc 13880 if (!tf->valid)
b9c361e0 13881 {
68ffbac6 13882 info->callbacks->einfo
9793eb77 13883 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13884 return FALSE;
ae17ab41
CM
13885 }
13886 }
8b127cbc
AM
13887 flaginfo->flags_initialized = TRUE;
13888 flaginfo->only_with_flags |= with_hex;
13889 flaginfo->not_with_flags |= without_hex;
ae17ab41 13890 }
ae17ab41 13891
8b127cbc 13892 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13893 return FALSE;
13894
8b127cbc 13895 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13896 return FALSE;
13897
13898 return TRUE;
ae17ab41
CM
13899}
13900
c152c796
AM
13901struct alloc_got_off_arg {
13902 bfd_vma gotoff;
10455f89 13903 struct bfd_link_info *info;
c152c796
AM
13904};
13905
13906/* We need a special top-level link routine to convert got reference counts
13907 to real got offsets. */
13908
13909static bfd_boolean
13910elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13911{
a50b1753 13912 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13913 bfd *obfd = gofarg->info->output_bfd;
13914 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13915
c152c796
AM
13916 if (h->got.refcount > 0)
13917 {
13918 h->got.offset = gofarg->gotoff;
10455f89 13919 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13920 }
13921 else
13922 h->got.offset = (bfd_vma) -1;
13923
13924 return TRUE;
13925}
13926
13927/* And an accompanying bit to work out final got entry offsets once
13928 we're done. Should be called from final_link. */
13929
13930bfd_boolean
13931bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13932 struct bfd_link_info *info)
13933{
13934 bfd *i;
13935 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13936 bfd_vma gotoff;
c152c796
AM
13937 struct alloc_got_off_arg gofarg;
13938
10455f89
HPN
13939 BFD_ASSERT (abfd == info->output_bfd);
13940
c152c796
AM
13941 if (! is_elf_hash_table (info->hash))
13942 return FALSE;
13943
13944 /* The GOT offset is relative to the .got section, but the GOT header is
13945 put into the .got.plt section, if the backend uses it. */
13946 if (bed->want_got_plt)
13947 gotoff = 0;
13948 else
13949 gotoff = bed->got_header_size;
13950
13951 /* Do the local .got entries first. */
c72f2fb2 13952 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13953 {
13954 bfd_signed_vma *local_got;
ef53be89 13955 size_t j, locsymcount;
c152c796
AM
13956 Elf_Internal_Shdr *symtab_hdr;
13957
13958 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13959 continue;
13960
13961 local_got = elf_local_got_refcounts (i);
13962 if (!local_got)
13963 continue;
13964
13965 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13966 if (elf_bad_symtab (i))
13967 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13968 else
13969 locsymcount = symtab_hdr->sh_info;
13970
13971 for (j = 0; j < locsymcount; ++j)
13972 {
13973 if (local_got[j] > 0)
13974 {
13975 local_got[j] = gotoff;
10455f89 13976 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13977 }
13978 else
13979 local_got[j] = (bfd_vma) -1;
13980 }
13981 }
13982
13983 /* Then the global .got entries. .plt refcounts are handled by
13984 adjust_dynamic_symbol */
13985 gofarg.gotoff = gotoff;
10455f89 13986 gofarg.info = info;
c152c796
AM
13987 elf_link_hash_traverse (elf_hash_table (info),
13988 elf_gc_allocate_got_offsets,
13989 &gofarg);
13990 return TRUE;
13991}
13992
13993/* Many folk need no more in the way of final link than this, once
13994 got entry reference counting is enabled. */
13995
13996bfd_boolean
13997bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13998{
13999 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14000 return FALSE;
14001
14002 /* Invoke the regular ELF backend linker to do all the work. */
14003 return bfd_elf_final_link (abfd, info);
14004}
14005
14006bfd_boolean
14007bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14008{
a50b1753 14009 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14010
14011 if (rcookie->bad_symtab)
14012 rcookie->rel = rcookie->rels;
14013
14014 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14015 {
14016 unsigned long r_symndx;
14017
14018 if (! rcookie->bad_symtab)
14019 if (rcookie->rel->r_offset > offset)
14020 return FALSE;
14021 if (rcookie->rel->r_offset != offset)
14022 continue;
14023
14024 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14025 if (r_symndx == STN_UNDEF)
c152c796
AM
14026 return TRUE;
14027
14028 if (r_symndx >= rcookie->locsymcount
14029 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14030 {
14031 struct elf_link_hash_entry *h;
14032
14033 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14034
14035 while (h->root.type == bfd_link_hash_indirect
14036 || h->root.type == bfd_link_hash_warning)
14037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14038
14039 if ((h->root.type == bfd_link_hash_defined
14040 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14041 && (h->root.u.def.section->owner != rcookie->abfd
14042 || h->root.u.def.section->kept_section != NULL
14043 || discarded_section (h->root.u.def.section)))
c152c796 14044 return TRUE;
c152c796
AM
14045 }
14046 else
14047 {
14048 /* It's not a relocation against a global symbol,
14049 but it could be a relocation against a local
14050 symbol for a discarded section. */
14051 asection *isec;
14052 Elf_Internal_Sym *isym;
14053
14054 /* Need to: get the symbol; get the section. */
14055 isym = &rcookie->locsyms[r_symndx];
cb33740c 14056 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14057 if (isec != NULL
14058 && (isec->kept_section != NULL
14059 || discarded_section (isec)))
cb33740c 14060 return TRUE;
c152c796
AM
14061 }
14062 return FALSE;
14063 }
14064 return FALSE;
14065}
14066
14067/* Discard unneeded references to discarded sections.
75938853
AM
14068 Returns -1 on error, 1 if any section's size was changed, 0 if
14069 nothing changed. This function assumes that the relocations are in
14070 sorted order, which is true for all known assemblers. */
c152c796 14071
75938853 14072int
c152c796
AM
14073bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14074{
14075 struct elf_reloc_cookie cookie;
18cd5bce 14076 asection *o;
c152c796 14077 bfd *abfd;
75938853 14078 int changed = 0;
c152c796
AM
14079
14080 if (info->traditional_format
14081 || !is_elf_hash_table (info->hash))
75938853 14082 return 0;
c152c796 14083
18cd5bce
AM
14084 o = bfd_get_section_by_name (output_bfd, ".stab");
14085 if (o != NULL)
c152c796 14086 {
18cd5bce 14087 asection *i;
c152c796 14088
18cd5bce 14089 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14090 {
18cd5bce
AM
14091 if (i->size == 0
14092 || i->reloc_count == 0
14093 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14094 continue;
c152c796 14095
18cd5bce
AM
14096 abfd = i->owner;
14097 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14098 continue;
c152c796 14099
18cd5bce 14100 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14101 return -1;
c152c796 14102
18cd5bce
AM
14103 if (_bfd_discard_section_stabs (abfd, i,
14104 elf_section_data (i)->sec_info,
5241d853
RS
14105 bfd_elf_reloc_symbol_deleted_p,
14106 &cookie))
75938853 14107 changed = 1;
18cd5bce
AM
14108
14109 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14110 }
18cd5bce
AM
14111 }
14112
2f0c68f2
CM
14113 o = NULL;
14114 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14115 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14116 if (o != NULL)
14117 {
14118 asection *i;
d7153c4a 14119 int eh_changed = 0;
79a94a2a 14120 unsigned int eh_alignment;
c152c796 14121
18cd5bce 14122 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14123 {
18cd5bce
AM
14124 if (i->size == 0)
14125 continue;
14126
14127 abfd = i->owner;
14128 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14129 continue;
14130
14131 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14132 return -1;
18cd5bce
AM
14133
14134 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14135 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14136 bfd_elf_reloc_symbol_deleted_p,
14137 &cookie))
d7153c4a
AM
14138 {
14139 eh_changed = 1;
14140 if (i->size != i->rawsize)
14141 changed = 1;
14142 }
18cd5bce
AM
14143
14144 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14145 }
9866ffe2 14146
79a94a2a 14147 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14148 /* Skip over zero terminator, and prevent empty sections from
14149 adding alignment padding at the end. */
14150 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14151 if (i->size == 0)
14152 i->flags |= SEC_EXCLUDE;
14153 else if (i->size > 4)
14154 break;
14155 /* The last non-empty eh_frame section doesn't need padding. */
14156 if (i != NULL)
14157 i = i->map_tail.s;
14158 /* Any prior sections must pad the last FDE out to the output
14159 section alignment. Otherwise we might have zero padding
14160 between sections, which would be seen as a terminator. */
14161 for (; i != NULL; i = i->map_tail.s)
14162 if (i->size == 4)
14163 /* All but the last zero terminator should have been removed. */
14164 BFD_FAIL ();
14165 else
14166 {
14167 bfd_size_type size
14168 = (i->size + eh_alignment - 1) & -eh_alignment;
14169 if (i->size != size)
af471f82 14170 {
9866ffe2
AM
14171 i->size = size;
14172 changed = 1;
14173 eh_changed = 1;
af471f82 14174 }
9866ffe2 14175 }
d7153c4a
AM
14176 if (eh_changed)
14177 elf_link_hash_traverse (elf_hash_table (info),
14178 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14179 }
c152c796 14180
18cd5bce
AM
14181 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14182 {
14183 const struct elf_backend_data *bed;
57963c05 14184 asection *s;
c152c796 14185
18cd5bce
AM
14186 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14187 continue;
57963c05
AM
14188 s = abfd->sections;
14189 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14190 continue;
18cd5bce
AM
14191
14192 bed = get_elf_backend_data (abfd);
14193
14194 if (bed->elf_backend_discard_info != NULL)
14195 {
14196 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14197 return -1;
18cd5bce
AM
14198
14199 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14200 changed = 1;
18cd5bce
AM
14201
14202 fini_reloc_cookie (&cookie, abfd);
14203 }
c152c796
AM
14204 }
14205
2f0c68f2
CM
14206 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14207 _bfd_elf_end_eh_frame_parsing (info);
14208
14209 if (info->eh_frame_hdr_type
0e1862bb 14210 && !bfd_link_relocatable (info)
c152c796 14211 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14212 changed = 1;
c152c796 14213
75938853 14214 return changed;
c152c796 14215}
082b7297 14216
43e1669b 14217bfd_boolean
0c511000 14218_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14219 asection *sec,
c0f00686 14220 struct bfd_link_info *info)
082b7297
L
14221{
14222 flagword flags;
c77ec726 14223 const char *name, *key;
082b7297
L
14224 struct bfd_section_already_linked *l;
14225 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14226
c77ec726
AM
14227 if (sec->output_section == bfd_abs_section_ptr)
14228 return FALSE;
0c511000 14229
c77ec726 14230 flags = sec->flags;
0c511000 14231
c77ec726
AM
14232 /* Return if it isn't a linkonce section. A comdat group section
14233 also has SEC_LINK_ONCE set. */
14234 if ((flags & SEC_LINK_ONCE) == 0)
14235 return FALSE;
0c511000 14236
c77ec726
AM
14237 /* Don't put group member sections on our list of already linked
14238 sections. They are handled as a group via their group section. */
14239 if (elf_sec_group (sec) != NULL)
14240 return FALSE;
0c511000 14241
c77ec726
AM
14242 /* For a SHT_GROUP section, use the group signature as the key. */
14243 name = sec->name;
14244 if ((flags & SEC_GROUP) != 0
14245 && elf_next_in_group (sec) != NULL
14246 && elf_group_name (elf_next_in_group (sec)) != NULL)
14247 key = elf_group_name (elf_next_in_group (sec));
14248 else
14249 {
14250 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14251 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14252 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14253 key++;
0c511000 14254 else
c77ec726
AM
14255 /* Must be a user linkonce section that doesn't follow gcc's
14256 naming convention. In this case we won't be matching
14257 single member groups. */
14258 key = name;
0c511000 14259 }
6d2cd210 14260
c77ec726 14261 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14262
14263 for (l = already_linked_list->entry; l != NULL; l = l->next)
14264 {
c2370991 14265 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14266 sections with a signature of <key> (<key> is some string),
14267 and linkonce sections named .gnu.linkonce.<type>.<key>.
14268 Match like sections. LTO plugin sections are an exception.
14269 They are always named .gnu.linkonce.t.<key> and match either
14270 type of section. */
14271 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14272 && ((flags & SEC_GROUP) != 0
14273 || strcmp (name, l->sec->name) == 0))
14274 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14275 {
14276 /* The section has already been linked. See if we should
6d2cd210 14277 issue a warning. */
c77ec726
AM
14278 if (!_bfd_handle_already_linked (sec, l, info))
14279 return FALSE;
082b7297 14280
c77ec726 14281 if (flags & SEC_GROUP)
3d7f7666 14282 {
c77ec726
AM
14283 asection *first = elf_next_in_group (sec);
14284 asection *s = first;
3d7f7666 14285
c77ec726 14286 while (s != NULL)
3d7f7666 14287 {
c77ec726
AM
14288 s->output_section = bfd_abs_section_ptr;
14289 /* Record which group discards it. */
14290 s->kept_section = l->sec;
14291 s = elf_next_in_group (s);
14292 /* These lists are circular. */
14293 if (s == first)
14294 break;
3d7f7666
L
14295 }
14296 }
082b7297 14297
43e1669b 14298 return TRUE;
082b7297
L
14299 }
14300 }
14301
c77ec726
AM
14302 /* A single member comdat group section may be discarded by a
14303 linkonce section and vice versa. */
14304 if ((flags & SEC_GROUP) != 0)
3d7f7666 14305 {
c77ec726 14306 asection *first = elf_next_in_group (sec);
c2370991 14307
c77ec726
AM
14308 if (first != NULL && elf_next_in_group (first) == first)
14309 /* Check this single member group against linkonce sections. */
14310 for (l = already_linked_list->entry; l != NULL; l = l->next)
14311 if ((l->sec->flags & SEC_GROUP) == 0
14312 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14313 {
14314 first->output_section = bfd_abs_section_ptr;
14315 first->kept_section = l->sec;
14316 sec->output_section = bfd_abs_section_ptr;
14317 break;
14318 }
14319 }
14320 else
14321 /* Check this linkonce section against single member groups. */
14322 for (l = already_linked_list->entry; l != NULL; l = l->next)
14323 if (l->sec->flags & SEC_GROUP)
6d2cd210 14324 {
c77ec726 14325 asection *first = elf_next_in_group (l->sec);
6d2cd210 14326
c77ec726
AM
14327 if (first != NULL
14328 && elf_next_in_group (first) == first
14329 && bfd_elf_match_symbols_in_sections (first, sec, info))
14330 {
14331 sec->output_section = bfd_abs_section_ptr;
14332 sec->kept_section = first;
14333 break;
14334 }
6d2cd210 14335 }
0c511000 14336
c77ec726
AM
14337 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14338 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14339 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14340 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14341 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14342 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14343 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14344 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14345 The reverse order cannot happen as there is never a bfd with only the
14346 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14347 matter as here were are looking only for cross-bfd sections. */
14348
14349 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14350 for (l = already_linked_list->entry; l != NULL; l = l->next)
14351 if ((l->sec->flags & SEC_GROUP) == 0
14352 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14353 {
14354 if (abfd != l->sec->owner)
14355 sec->output_section = bfd_abs_section_ptr;
14356 break;
14357 }
80c29487 14358
082b7297 14359 /* This is the first section with this name. Record it. */
c77ec726 14360 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14361 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14362 return sec->output_section == bfd_abs_section_ptr;
082b7297 14363}
81e1b023 14364
a4d8e49b
L
14365bfd_boolean
14366_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14367{
14368 return sym->st_shndx == SHN_COMMON;
14369}
14370
14371unsigned int
14372_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14373{
14374 return SHN_COMMON;
14375}
14376
14377asection *
14378_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14379{
14380 return bfd_com_section_ptr;
14381}
10455f89
HPN
14382
14383bfd_vma
14384_bfd_elf_default_got_elt_size (bfd *abfd,
14385 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14386 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14387 bfd *ibfd ATTRIBUTE_UNUSED,
14388 unsigned long symndx ATTRIBUTE_UNUSED)
14389{
14390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14391 return bed->s->arch_size / 8;
14392}
83bac4b0
NC
14393
14394/* Routines to support the creation of dynamic relocs. */
14395
83bac4b0
NC
14396/* Returns the name of the dynamic reloc section associated with SEC. */
14397
14398static const char *
14399get_dynamic_reloc_section_name (bfd * abfd,
14400 asection * sec,
14401 bfd_boolean is_rela)
14402{
ddcf1fcf
BS
14403 char *name;
14404 const char *old_name = bfd_get_section_name (NULL, sec);
14405 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14406
ddcf1fcf 14407 if (old_name == NULL)
83bac4b0
NC
14408 return NULL;
14409
ddcf1fcf 14410 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14411 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14412
14413 return name;
14414}
14415
14416/* Returns the dynamic reloc section associated with SEC.
14417 If necessary compute the name of the dynamic reloc section based
14418 on SEC's name (looked up in ABFD's string table) and the setting
14419 of IS_RELA. */
14420
14421asection *
14422_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14423 asection * sec,
14424 bfd_boolean is_rela)
14425{
14426 asection * reloc_sec = elf_section_data (sec)->sreloc;
14427
14428 if (reloc_sec == NULL)
14429 {
14430 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14431
14432 if (name != NULL)
14433 {
3d4d4302 14434 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14435
14436 if (reloc_sec != NULL)
14437 elf_section_data (sec)->sreloc = reloc_sec;
14438 }
14439 }
14440
14441 return reloc_sec;
14442}
14443
14444/* Returns the dynamic reloc section associated with SEC. If the
14445 section does not exist it is created and attached to the DYNOBJ
14446 bfd and stored in the SRELOC field of SEC's elf_section_data
14447 structure.
f8076f98 14448
83bac4b0
NC
14449 ALIGNMENT is the alignment for the newly created section and
14450 IS_RELA defines whether the name should be .rela.<SEC's name>
14451 or .rel.<SEC's name>. The section name is looked up in the
14452 string table associated with ABFD. */
14453
14454asection *
ca4be51c
AM
14455_bfd_elf_make_dynamic_reloc_section (asection *sec,
14456 bfd *dynobj,
14457 unsigned int alignment,
14458 bfd *abfd,
14459 bfd_boolean is_rela)
83bac4b0
NC
14460{
14461 asection * reloc_sec = elf_section_data (sec)->sreloc;
14462
14463 if (reloc_sec == NULL)
14464 {
14465 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14466
14467 if (name == NULL)
14468 return NULL;
14469
3d4d4302 14470 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14471
14472 if (reloc_sec == NULL)
14473 {
3d4d4302
AM
14474 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14475 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14476 if ((sec->flags & SEC_ALLOC) != 0)
14477 flags |= SEC_ALLOC | SEC_LOAD;
14478
3d4d4302 14479 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14480 if (reloc_sec != NULL)
14481 {
8877b5e5
AM
14482 /* _bfd_elf_get_sec_type_attr chooses a section type by
14483 name. Override as it may be wrong, eg. for a user
14484 section named "auto" we'll get ".relauto" which is
14485 seen to be a .rela section. */
14486 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14487 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14488 reloc_sec = NULL;
14489 }
14490 }
14491
14492 elf_section_data (sec)->sreloc = reloc_sec;
14493 }
14494
14495 return reloc_sec;
14496}
1338dd10 14497
bffebb6b
AM
14498/* Copy the ELF symbol type and other attributes for a linker script
14499 assignment from HSRC to HDEST. Generally this should be treated as
14500 if we found a strong non-dynamic definition for HDEST (except that
14501 ld ignores multiple definition errors). */
1338dd10 14502void
bffebb6b
AM
14503_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14504 struct bfd_link_hash_entry *hdest,
14505 struct bfd_link_hash_entry *hsrc)
1338dd10 14506{
bffebb6b
AM
14507 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14508 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14509 Elf_Internal_Sym isym;
1338dd10
PB
14510
14511 ehdest->type = ehsrc->type;
35fc36a8 14512 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14513
14514 isym.st_other = ehsrc->other;
b8417128 14515 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14516}
351f65ca
L
14517
14518/* Append a RELA relocation REL to section S in BFD. */
14519
14520void
14521elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14522{
14523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14524 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14525 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14526 bed->s->swap_reloca_out (abfd, rel, loc);
14527}
14528
14529/* Append a REL relocation REL to section S in BFD. */
14530
14531void
14532elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14533{
14534 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14535 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14536 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14537 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14538}
7dba9362
AM
14539
14540/* Define __start, __stop, .startof. or .sizeof. symbol. */
14541
14542struct bfd_link_hash_entry *
14543bfd_elf_define_start_stop (struct bfd_link_info *info,
14544 const char *symbol, asection *sec)
14545{
487b6440 14546 struct elf_link_hash_entry *h;
7dba9362 14547
487b6440
AM
14548 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14549 FALSE, FALSE, TRUE);
14550 if (h != NULL
14551 && (h->root.type == bfd_link_hash_undefined
14552 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14553 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14554 {
bf3077a6 14555 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14556 h->root.type = bfd_link_hash_defined;
14557 h->root.u.def.section = sec;
14558 h->root.u.def.value = 0;
14559 h->def_regular = 1;
14560 h->def_dynamic = 0;
14561 h->start_stop = 1;
14562 h->u2.start_stop_section = sec;
14563 if (symbol[0] == '.')
14564 {
14565 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14566 const struct elf_backend_data *bed;
14567 bed = get_elf_backend_data (info->output_bfd);
14568 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14569 }
36b8fda5
AM
14570 else
14571 {
14572 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14573 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14574 if (was_dynamic)
36b8fda5
AM
14575 bfd_elf_link_record_dynamic_symbol (info, h);
14576 }
487b6440 14577 return &h->root;
7dba9362 14578 }
487b6440 14579 return NULL;
7dba9362 14580}
This page took 2.505444 seconds and 4 git commands to generate.