]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Add libmsvcrt, libmsvcrt-os and libucrtbase to the list of libraries for which the...
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
ad32986f 122 bh = &h->root;
d98685ac 123 }
ad32986f
NC
124 else
125 bh = NULL;
d98685ac 126
cf18fda4 127 bed = get_elf_backend_data (abfd);
d98685ac 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 129 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
ad32986f 133 BFD_ASSERT (h != NULL);
d98685ac 134 h->def_regular = 1;
e28df02b 135 h->non_elf = 0;
12b2843a 136 h->root.linker_def = 1;
d98685ac 137 h->type = STT_OBJECT;
00b7642b
AM
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 140
ccabcbe5 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
142 return h;
143}
144
b34976b6 145bfd_boolean
268b6b39 146_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
147{
148 flagword flags;
aad5d350 149 asection *s;
252b5132 150 struct elf_link_hash_entry *h;
9c5bfbb7 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 152 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
153
154 /* This function may be called more than once. */
ce558b89 155 if (htab->sgot != NULL)
b34976b6 156 return TRUE;
252b5132 157
e5a52504 158 flags = bed->dynamic_sec_flags;
252b5132 159
14b2f831
AM
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
6de2ae4a
L
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
252b5132 169
14b2f831 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
252b5132
RH
176 if (bed->want_got_plt)
177 {
14b2f831 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 179 if (s == NULL
6de2ae4a
L
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
b34976b6 182 return FALSE;
6de2ae4a 183 htab->sgotplt = s;
252b5132
RH
184 }
185
64e77c6d
L
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
2517a57f
AM
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
6de2ae4a
L
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
2517a57f 197 elf_hash_table (info)->hgot = h;
d98685ac
AM
198 if (h == NULL)
199 return FALSE;
2517a57f 200 }
252b5132 201
b34976b6 202 return TRUE;
252b5132
RH
203}
204\f
7e9f0867
AM
205/* Create a strtab to hold the dynamic symbol names. */
206static bfd_boolean
207_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208{
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
6cd255ca
L
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
57963c05 221 asection *s;
6cd255ca 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 223 if ((ibfd->flags
57963c05
AM
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
589 h->dynamic = 1;
590}
591
45d6a902
AM
592/* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
594
595bfd_boolean
fe21a8fc
L
596bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
268b6b39 598 const char *name,
fe21a8fc
L
599 bfd_boolean provide,
600 bfd_boolean hidden)
45d6a902 601{
00cbee0a 602 struct elf_link_hash_entry *h, *hv;
4ea42fb7 603 struct elf_link_hash_table *htab;
00cbee0a 604 const struct elf_backend_data *bed;
45d6a902 605
0eddce27 606 if (!is_elf_hash_table (info->hash))
45d6a902
AM
607 return TRUE;
608
4ea42fb7
AM
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 611 if (h == NULL)
4ea42fb7 612 return provide;
45d6a902 613
8e2a4f11
AM
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
616
0f550b3d
L
617 if (h->versioned == unknown)
618 {
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
622 {
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
627 }
628 }
629
73ec947d
AM
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
633 {
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
636 }
637
00cbee0a 638 switch (h->root.type)
77cfaee6 639 {
00cbee0a
L
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
4ea42fb7 649 h->root.type = bfd_link_hash_new;
77cfaee6
AM
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
652 break;
653 case bfd_link_hash_new:
00cbee0a
L
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 657 the versioned symbol point to this one. */
00cbee0a
L
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
8e2a4f11
AM
670 default:
671 BFD_FAIL ();
c2596ca5 672 return FALSE;
55255dae 673 }
45d6a902
AM
674
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
f5385ebf
AM
680 && h->def_dynamic
681 && !h->def_regular)
45d6a902
AM
682 h->root.type = bfd_link_hash_undefined;
683
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
b531344c
MR
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
45d6a902 688 if (!provide
f5385ebf
AM
689 && h->def_dynamic
690 && !h->def_regular)
b531344c
MR
691 h->verinfo.verdef = NULL;
692
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
45d6a902 695
f5385ebf 696 h->def_regular = 1;
45d6a902 697
eb8476a6 698 if (hidden)
fe21a8fc 699 {
91d6fa6a 700 bed = get_elf_backend_data (output_bfd);
b8297068
AM
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
704 }
705
6fa3860b
PB
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
0e1862bb 708 if (!bfd_link_relocatable (info)
6fa3860b
PB
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
713
f5385ebf
AM
714 if ((h->def_dynamic
715 || h->ref_dynamic
6b3b0ab8
L
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
718 && h->dynindx == -1)
719 {
c152c796 720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
721 return FALSE;
722
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
f6e332e6
AM
726 if (h->u.weakdef != NULL
727 && h->u.weakdef->dynindx == -1)
45d6a902 728 {
f6e332e6 729 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
730 return FALSE;
731 }
732 }
733
734 return TRUE;
735}
42751cf3 736
8c58d23b
AM
737/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
738 success, and 2 on a failure caused by attempting to record a symbol
739 in a discarded section, eg. a discarded link-once section symbol. */
740
741int
c152c796
AM
742bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
743 bfd *input_bfd,
744 long input_indx)
8c58d23b
AM
745{
746 bfd_size_type amt;
747 struct elf_link_local_dynamic_entry *entry;
748 struct elf_link_hash_table *eht;
749 struct elf_strtab_hash *dynstr;
ef53be89 750 size_t dynstr_index;
8c58d23b
AM
751 char *name;
752 Elf_External_Sym_Shndx eshndx;
753 char esym[sizeof (Elf64_External_Sym)];
754
0eddce27 755 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
756 return 0;
757
758 /* See if the entry exists already. */
759 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
760 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
761 return 1;
762
763 amt = sizeof (*entry);
a50b1753 764 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
765 if (entry == NULL)
766 return 0;
767
768 /* Go find the symbol, so that we can find it's name. */
769 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 770 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
771 {
772 bfd_release (input_bfd, entry);
773 return 0;
774 }
775
776 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 777 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
778 {
779 asection *s;
780
781 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
782 if (s == NULL || bfd_is_abs_section (s->output_section))
783 {
784 /* We can still bfd_release here as nothing has done another
785 bfd_alloc. We can't do this later in this function. */
786 bfd_release (input_bfd, entry);
787 return 2;
788 }
789 }
790
791 name = (bfd_elf_string_from_elf_section
792 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
793 entry->isym.st_name));
794
795 dynstr = elf_hash_table (info)->dynstr;
796 if (dynstr == NULL)
797 {
798 /* Create a strtab to hold the dynamic symbol names. */
799 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
800 if (dynstr == NULL)
801 return 0;
802 }
803
b34976b6 804 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 805 if (dynstr_index == (size_t) -1)
8c58d23b
AM
806 return 0;
807 entry->isym.st_name = dynstr_index;
808
809 eht = elf_hash_table (info);
810
811 entry->next = eht->dynlocal;
812 eht->dynlocal = entry;
813 entry->input_bfd = input_bfd;
814 entry->input_indx = input_indx;
815 eht->dynsymcount++;
816
817 /* Whatever binding the symbol had before, it's now local. */
818 entry->isym.st_info
819 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
820
821 /* The dynindx will be set at the end of size_dynamic_sections. */
822
823 return 1;
824}
825
30b30c21 826/* Return the dynindex of a local dynamic symbol. */
42751cf3 827
30b30c21 828long
268b6b39
AM
829_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
830 bfd *input_bfd,
831 long input_indx)
30b30c21
RH
832{
833 struct elf_link_local_dynamic_entry *e;
834
835 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
836 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
837 return e->dynindx;
838 return -1;
839}
840
841/* This function is used to renumber the dynamic symbols, if some of
842 them are removed because they are marked as local. This is called
843 via elf_link_hash_traverse. */
844
b34976b6 845static bfd_boolean
268b6b39
AM
846elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
847 void *data)
42751cf3 848{
a50b1753 849 size_t *count = (size_t *) data;
30b30c21 850
6fa3860b
PB
851 if (h->forced_local)
852 return TRUE;
853
854 if (h->dynindx != -1)
855 h->dynindx = ++(*count);
856
857 return TRUE;
858}
859
860
861/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
862 STB_LOCAL binding. */
863
864static bfd_boolean
865elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
866 void *data)
867{
a50b1753 868 size_t *count = (size_t *) data;
6fa3860b 869
6fa3860b
PB
870 if (!h->forced_local)
871 return TRUE;
872
42751cf3 873 if (h->dynindx != -1)
30b30c21
RH
874 h->dynindx = ++(*count);
875
b34976b6 876 return TRUE;
42751cf3 877}
30b30c21 878
aee6f5b4
AO
879/* Return true if the dynamic symbol for a given section should be
880 omitted when creating a shared library. */
881bfd_boolean
882_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
883 struct bfd_link_info *info,
884 asection *p)
885{
74541ad4 886 struct elf_link_hash_table *htab;
ca55926c 887 asection *ip;
74541ad4 888
aee6f5b4
AO
889 switch (elf_section_data (p)->this_hdr.sh_type)
890 {
891 case SHT_PROGBITS:
892 case SHT_NOBITS:
893 /* If sh_type is yet undecided, assume it could be
894 SHT_PROGBITS/SHT_NOBITS. */
895 case SHT_NULL:
74541ad4
AM
896 htab = elf_hash_table (info);
897 if (p == htab->tls_sec)
898 return FALSE;
899
900 if (htab->text_index_section != NULL)
901 return p != htab->text_index_section && p != htab->data_index_section;
902
ca55926c 903 return (htab->dynobj != NULL
3d4d4302 904 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 905 && ip->output_section == p);
aee6f5b4
AO
906
907 /* There shouldn't be section relative relocations
908 against any other section. */
909 default:
910 return TRUE;
911 }
912}
913
062e2358 914/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
915 symbol for each output section, which come first. Next come symbols
916 which have been forced to local binding. Then all of the back-end
917 allocated local dynamic syms, followed by the rest of the global
918 symbols. */
30b30c21 919
554220db
AM
920static unsigned long
921_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
922 struct bfd_link_info *info,
923 unsigned long *section_sym_count)
30b30c21
RH
924{
925 unsigned long dynsymcount = 0;
926
0e1862bb
L
927 if (bfd_link_pic (info)
928 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 929 {
aee6f5b4 930 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
931 asection *p;
932 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 933 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
934 && (p->flags & SEC_ALLOC) != 0
935 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
936 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
937 else
938 elf_section_data (p)->dynindx = 0;
30b30c21 939 }
554220db 940 *section_sym_count = dynsymcount;
30b30c21 941
6fa3860b
PB
942 elf_link_hash_traverse (elf_hash_table (info),
943 elf_link_renumber_local_hash_table_dynsyms,
944 &dynsymcount);
945
30b30c21
RH
946 if (elf_hash_table (info)->dynlocal)
947 {
948 struct elf_link_local_dynamic_entry *p;
949 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
950 p->dynindx = ++dynsymcount;
951 }
90ac2420 952 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
953
954 elf_link_hash_traverse (elf_hash_table (info),
955 elf_link_renumber_hash_table_dynsyms,
956 &dynsymcount);
957
d5486c43
L
958 /* There is an unused NULL entry at the head of the table which we
959 must account for in our count even if the table is empty since it
960 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
961 .dynamic section. */
962 dynsymcount++;
30b30c21 963
ccabcbe5
AM
964 elf_hash_table (info)->dynsymcount = dynsymcount;
965 return dynsymcount;
30b30c21 966}
252b5132 967
54ac0771
L
968/* Merge st_other field. */
969
970static void
971elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 972 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 973 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
974{
975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
976
977 /* If st_other has a processor-specific meaning, specific
cd3416da 978 code might be needed here. */
54ac0771
L
979 if (bed->elf_backend_merge_symbol_attribute)
980 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
981 dynamic);
982
cd3416da 983 if (!dynamic)
54ac0771 984 {
cd3416da
AM
985 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
986 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 987
cd3416da
AM
988 /* Keep the most constraining visibility. Leave the remainder
989 of the st_other field to elf_backend_merge_symbol_attribute. */
990 if (symvis - 1 < hvis - 1)
991 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 992 }
b8417128
AM
993 else if (definition
994 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
995 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 996 h->protected_def = 1;
54ac0771
L
997}
998
4f3fedcf
AM
999/* This function is called when we want to merge a new symbol with an
1000 existing symbol. It handles the various cases which arise when we
1001 find a definition in a dynamic object, or when there is already a
1002 definition in a dynamic object. The new symbol is described by
1003 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1004 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1005 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1006 of an old common symbol. We set OVERRIDE if the old symbol is
1007 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1008 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1009 to change. By OK to change, we mean that we shouldn't warn if the
1010 type or size does change. */
45d6a902 1011
8a56bd02 1012static bfd_boolean
268b6b39
AM
1013_bfd_elf_merge_symbol (bfd *abfd,
1014 struct bfd_link_info *info,
1015 const char *name,
1016 Elf_Internal_Sym *sym,
1017 asection **psec,
1018 bfd_vma *pvalue,
4f3fedcf
AM
1019 struct elf_link_hash_entry **sym_hash,
1020 bfd **poldbfd,
37a9e49a 1021 bfd_boolean *pold_weak,
af44c138 1022 unsigned int *pold_alignment,
268b6b39
AM
1023 bfd_boolean *skip,
1024 bfd_boolean *override,
1025 bfd_boolean *type_change_ok,
6e33951e
L
1026 bfd_boolean *size_change_ok,
1027 bfd_boolean *matched)
252b5132 1028{
7479dfd4 1029 asection *sec, *oldsec;
45d6a902 1030 struct elf_link_hash_entry *h;
90c984fc 1031 struct elf_link_hash_entry *hi;
45d6a902
AM
1032 struct elf_link_hash_entry *flip;
1033 int bind;
1034 bfd *oldbfd;
1035 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1036 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1037 const struct elf_backend_data *bed;
6e33951e 1038 char *new_version;
93f4de39 1039 bfd_boolean default_sym = *matched;
45d6a902
AM
1040
1041 *skip = FALSE;
1042 *override = FALSE;
1043
1044 sec = *psec;
1045 bind = ELF_ST_BIND (sym->st_info);
1046
1047 if (! bfd_is_und_section (sec))
1048 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1049 else
1050 h = ((struct elf_link_hash_entry *)
1051 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1052 if (h == NULL)
1053 return FALSE;
1054 *sym_hash = h;
252b5132 1055
88ba32a0
L
1056 bed = get_elf_backend_data (abfd);
1057
6e33951e 1058 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1059 if (h->versioned != unversioned)
6e33951e 1060 {
422f1182
L
1061 /* Symbol version is unknown or versioned. */
1062 new_version = strrchr (name, ELF_VER_CHR);
1063 if (new_version)
1064 {
1065 if (h->versioned == unknown)
1066 {
1067 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1068 h->versioned = versioned_hidden;
1069 else
1070 h->versioned = versioned;
1071 }
1072 new_version += 1;
1073 if (new_version[0] == '\0')
1074 new_version = NULL;
1075 }
1076 else
1077 h->versioned = unversioned;
6e33951e 1078 }
422f1182
L
1079 else
1080 new_version = NULL;
6e33951e 1081
90c984fc
L
1082 /* For merging, we only care about real symbols. But we need to make
1083 sure that indirect symbol dynamic flags are updated. */
1084 hi = h;
45d6a902
AM
1085 while (h->root.type == bfd_link_hash_indirect
1086 || h->root.type == bfd_link_hash_warning)
1087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1088
6e33951e
L
1089 if (!*matched)
1090 {
1091 if (hi == h || h->root.type == bfd_link_hash_new)
1092 *matched = TRUE;
1093 else
1094 {
ae7683d2 1095 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1096 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1097 true if the new symbol is only visible to the symbol with
6e33951e 1098 the same symbol version. */
422f1182
L
1099 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1100 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1101 if (!old_hidden && !new_hidden)
1102 /* The new symbol matches the existing symbol if both
1103 aren't hidden. */
1104 *matched = TRUE;
1105 else
1106 {
1107 /* OLD_VERSION is the symbol version of the existing
1108 symbol. */
422f1182
L
1109 char *old_version;
1110
1111 if (h->versioned >= versioned)
1112 old_version = strrchr (h->root.root.string,
1113 ELF_VER_CHR) + 1;
1114 else
1115 old_version = NULL;
6e33951e
L
1116
1117 /* The new symbol matches the existing symbol if they
1118 have the same symbol version. */
1119 *matched = (old_version == new_version
1120 || (old_version != NULL
1121 && new_version != NULL
1122 && strcmp (old_version, new_version) == 0));
1123 }
1124 }
1125 }
1126
934bce08
AM
1127 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1128 existing symbol. */
1129
1130 oldbfd = NULL;
1131 oldsec = NULL;
1132 switch (h->root.type)
1133 {
1134 default:
1135 break;
1136
1137 case bfd_link_hash_undefined:
1138 case bfd_link_hash_undefweak:
1139 oldbfd = h->root.u.undef.abfd;
1140 break;
1141
1142 case bfd_link_hash_defined:
1143 case bfd_link_hash_defweak:
1144 oldbfd = h->root.u.def.section->owner;
1145 oldsec = h->root.u.def.section;
1146 break;
1147
1148 case bfd_link_hash_common:
1149 oldbfd = h->root.u.c.p->section->owner;
1150 oldsec = h->root.u.c.p->section;
1151 if (pold_alignment)
1152 *pold_alignment = h->root.u.c.p->alignment_power;
1153 break;
1154 }
1155 if (poldbfd && *poldbfd == NULL)
1156 *poldbfd = oldbfd;
1157
1158 /* Differentiate strong and weak symbols. */
1159 newweak = bind == STB_WEAK;
1160 oldweak = (h->root.type == bfd_link_hash_defweak
1161 || h->root.type == bfd_link_hash_undefweak);
1162 if (pold_weak)
1163 *pold_weak = oldweak;
1164
40b36307 1165 /* We have to check it for every instance since the first few may be
ee659f1f 1166 references and not all compilers emit symbol type for undefined
40b36307
L
1167 symbols. */
1168 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1169
ee659f1f
AM
1170 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1171 respectively, is from a dynamic object. */
1172
1173 newdyn = (abfd->flags & DYNAMIC) != 0;
1174
1175 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1176 syms and defined syms in dynamic libraries respectively.
1177 ref_dynamic on the other hand can be set for a symbol defined in
1178 a dynamic library, and def_dynamic may not be set; When the
1179 definition in a dynamic lib is overridden by a definition in the
1180 executable use of the symbol in the dynamic lib becomes a
1181 reference to the executable symbol. */
1182 if (newdyn)
1183 {
1184 if (bfd_is_und_section (sec))
1185 {
1186 if (bind != STB_WEAK)
1187 {
1188 h->ref_dynamic_nonweak = 1;
1189 hi->ref_dynamic_nonweak = 1;
1190 }
1191 }
1192 else
1193 {
6e33951e
L
1194 /* Update the existing symbol only if they match. */
1195 if (*matched)
1196 h->dynamic_def = 1;
ee659f1f
AM
1197 hi->dynamic_def = 1;
1198 }
1199 }
1200
45d6a902
AM
1201 /* If we just created the symbol, mark it as being an ELF symbol.
1202 Other than that, there is nothing to do--there is no merge issue
1203 with a newly defined symbol--so we just return. */
1204
1205 if (h->root.type == bfd_link_hash_new)
252b5132 1206 {
f5385ebf 1207 h->non_elf = 0;
45d6a902
AM
1208 return TRUE;
1209 }
252b5132 1210
45d6a902
AM
1211 /* In cases involving weak versioned symbols, we may wind up trying
1212 to merge a symbol with itself. Catch that here, to avoid the
1213 confusion that results if we try to override a symbol with
1214 itself. The additional tests catch cases like
1215 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1216 dynamic object, which we do want to handle here. */
1217 if (abfd == oldbfd
895fa45f 1218 && (newweak || oldweak)
45d6a902 1219 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1220 || !h->def_regular))
45d6a902
AM
1221 return TRUE;
1222
707bba77 1223 olddyn = FALSE;
45d6a902
AM
1224 if (oldbfd != NULL)
1225 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1226 else if (oldsec != NULL)
45d6a902 1227 {
707bba77 1228 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1229 indices used by MIPS ELF. */
707bba77 1230 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1231 }
252b5132 1232
1a3b5c34
AM
1233 /* Handle a case where plugin_notice won't be called and thus won't
1234 set the non_ir_ref flags on the first pass over symbols. */
1235 if (oldbfd != NULL
1236 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1237 && newdyn != olddyn)
1238 {
1239 h->root.non_ir_ref_dynamic = TRUE;
1240 hi->root.non_ir_ref_dynamic = TRUE;
1241 }
1242
45d6a902
AM
1243 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1244 respectively, appear to be a definition rather than reference. */
1245
707bba77 1246 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1247
707bba77
AM
1248 olddef = (h->root.type != bfd_link_hash_undefined
1249 && h->root.type != bfd_link_hash_undefweak
202ac193 1250 && h->root.type != bfd_link_hash_common);
45d6a902 1251
0a36a439
L
1252 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1253 respectively, appear to be a function. */
1254
1255 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1256 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1257
1258 oldfunc = (h->type != STT_NOTYPE
1259 && bed->is_function_type (h->type));
1260
c5d37467 1261 if (!(newfunc && oldfunc)
5b677558
AM
1262 && ELF_ST_TYPE (sym->st_info) != h->type
1263 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1264 && h->type != STT_NOTYPE
c5d37467
AM
1265 && (newdef || bfd_is_com_section (sec))
1266 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1267 {
c5d37467
AM
1268 /* If creating a default indirect symbol ("foo" or "foo@") from
1269 a dynamic versioned definition ("foo@@") skip doing so if
1270 there is an existing regular definition with a different
1271 type. We don't want, for example, a "time" variable in the
1272 executable overriding a "time" function in a shared library. */
1273 if (newdyn
1274 && !olddyn)
1275 {
1276 *skip = TRUE;
1277 return TRUE;
1278 }
1279
1280 /* When adding a symbol from a regular object file after we have
1281 created indirect symbols, undo the indirection and any
1282 dynamic state. */
1283 if (hi != h
1284 && !newdyn
1285 && olddyn)
1286 {
1287 h = hi;
1288 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1289 h->forced_local = 0;
1290 h->ref_dynamic = 0;
1291 h->def_dynamic = 0;
1292 h->dynamic_def = 0;
1293 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1294 {
1295 h->root.type = bfd_link_hash_undefined;
1296 h->root.u.undef.abfd = abfd;
1297 }
1298 else
1299 {
1300 h->root.type = bfd_link_hash_new;
1301 h->root.u.undef.abfd = NULL;
1302 }
1303 return TRUE;
1304 }
580a2b6e
L
1305 }
1306
4c34aff8
AM
1307 /* Check TLS symbols. We don't check undefined symbols introduced
1308 by "ld -u" which have no type (and oldbfd NULL), and we don't
1309 check symbols from plugins because they also have no type. */
1310 if (oldbfd != NULL
1311 && (oldbfd->flags & BFD_PLUGIN) == 0
1312 && (abfd->flags & BFD_PLUGIN) == 0
1313 && ELF_ST_TYPE (sym->st_info) != h->type
1314 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1315 {
1316 bfd *ntbfd, *tbfd;
1317 bfd_boolean ntdef, tdef;
1318 asection *ntsec, *tsec;
1319
1320 if (h->type == STT_TLS)
1321 {
3b36f7e6 1322 ntbfd = abfd;
7479dfd4
L
1323 ntsec = sec;
1324 ntdef = newdef;
1325 tbfd = oldbfd;
1326 tsec = oldsec;
1327 tdef = olddef;
1328 }
1329 else
1330 {
1331 ntbfd = oldbfd;
1332 ntsec = oldsec;
1333 ntdef = olddef;
1334 tbfd = abfd;
1335 tsec = sec;
1336 tdef = newdef;
1337 }
1338
1339 if (tdef && ntdef)
4eca0228 1340 _bfd_error_handler
695344c0 1341 /* xgettext:c-format */
191c0c42
AM
1342 (_("%s: TLS definition in %B section %A "
1343 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1344 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1345 else if (!tdef && !ntdef)
4eca0228 1346 _bfd_error_handler
695344c0 1347 /* xgettext:c-format */
191c0c42
AM
1348 (_("%s: TLS reference in %B "
1349 "mismatches non-TLS reference in %B"),
c08bb8dd 1350 h->root.root.string, tbfd, ntbfd);
7479dfd4 1351 else if (tdef)
4eca0228 1352 _bfd_error_handler
695344c0 1353 /* xgettext:c-format */
191c0c42
AM
1354 (_("%s: TLS definition in %B section %A "
1355 "mismatches non-TLS reference in %B"),
c08bb8dd 1356 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1357 else
4eca0228 1358 _bfd_error_handler
695344c0 1359 /* xgettext:c-format */
191c0c42
AM
1360 (_("%s: TLS reference in %B "
1361 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1362 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1363
1364 bfd_set_error (bfd_error_bad_value);
1365 return FALSE;
1366 }
1367
45d6a902
AM
1368 /* If the old symbol has non-default visibility, we ignore the new
1369 definition from a dynamic object. */
1370 if (newdyn
9c7a29a3 1371 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1372 && !bfd_is_und_section (sec))
1373 {
1374 *skip = TRUE;
1375 /* Make sure this symbol is dynamic. */
f5385ebf 1376 h->ref_dynamic = 1;
90c984fc 1377 hi->ref_dynamic = 1;
45d6a902
AM
1378 /* A protected symbol has external availability. Make sure it is
1379 recorded as dynamic.
1380
1381 FIXME: Should we check type and size for protected symbol? */
1382 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1383 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1384 else
1385 return TRUE;
1386 }
1387 else if (!newdyn
9c7a29a3 1388 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1389 && h->def_dynamic)
45d6a902
AM
1390 {
1391 /* If the new symbol with non-default visibility comes from a
1392 relocatable file and the old definition comes from a dynamic
1393 object, we remove the old definition. */
6c9b78e6 1394 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1395 {
1396 /* Handle the case where the old dynamic definition is
1397 default versioned. We need to copy the symbol info from
1398 the symbol with default version to the normal one if it
1399 was referenced before. */
1400 if (h->ref_regular)
1401 {
6c9b78e6 1402 hi->root.type = h->root.type;
d2dee3b2 1403 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1404 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1405
6c9b78e6 1406 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1407 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1408 {
aed81c4e
MR
1409 /* If the new symbol is hidden or internal, completely undo
1410 any dynamic link state. */
1411 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1412 h->forced_local = 0;
1413 h->ref_dynamic = 0;
d2dee3b2
L
1414 }
1415 else
aed81c4e
MR
1416 h->ref_dynamic = 1;
1417
1418 h->def_dynamic = 0;
aed81c4e
MR
1419 /* FIXME: Should we check type and size for protected symbol? */
1420 h->size = 0;
1421 h->type = 0;
1422
6c9b78e6 1423 h = hi;
d2dee3b2
L
1424 }
1425 else
6c9b78e6 1426 h = hi;
d2dee3b2 1427 }
1de1a317 1428
f5eda473
AM
1429 /* If the old symbol was undefined before, then it will still be
1430 on the undefs list. If the new symbol is undefined or
1431 common, we can't make it bfd_link_hash_new here, because new
1432 undefined or common symbols will be added to the undefs list
1433 by _bfd_generic_link_add_one_symbol. Symbols may not be
1434 added twice to the undefs list. Also, if the new symbol is
1435 undefweak then we don't want to lose the strong undef. */
1436 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1437 {
1de1a317 1438 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1439 h->root.u.undef.abfd = abfd;
1440 }
1441 else
1442 {
1443 h->root.type = bfd_link_hash_new;
1444 h->root.u.undef.abfd = NULL;
1445 }
1446
f5eda473 1447 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1448 {
f5eda473
AM
1449 /* If the new symbol is hidden or internal, completely undo
1450 any dynamic link state. */
1451 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1452 h->forced_local = 0;
1453 h->ref_dynamic = 0;
45d6a902 1454 }
f5eda473
AM
1455 else
1456 h->ref_dynamic = 1;
1457 h->def_dynamic = 0;
45d6a902
AM
1458 /* FIXME: Should we check type and size for protected symbol? */
1459 h->size = 0;
1460 h->type = 0;
1461 return TRUE;
1462 }
14a793b2 1463
15b43f48
AM
1464 /* If a new weak symbol definition comes from a regular file and the
1465 old symbol comes from a dynamic library, we treat the new one as
1466 strong. Similarly, an old weak symbol definition from a regular
1467 file is treated as strong when the new symbol comes from a dynamic
1468 library. Further, an old weak symbol from a dynamic library is
1469 treated as strong if the new symbol is from a dynamic library.
1470 This reflects the way glibc's ld.so works.
1471
1472 Do this before setting *type_change_ok or *size_change_ok so that
1473 we warn properly when dynamic library symbols are overridden. */
1474
1475 if (newdef && !newdyn && olddyn)
0f8a2703 1476 newweak = FALSE;
15b43f48 1477 if (olddef && newdyn)
0f8a2703
AM
1478 oldweak = FALSE;
1479
d334575b 1480 /* Allow changes between different types of function symbol. */
0a36a439 1481 if (newfunc && oldfunc)
fcb93ecf
PB
1482 *type_change_ok = TRUE;
1483
79349b09
AM
1484 /* It's OK to change the type if either the existing symbol or the
1485 new symbol is weak. A type change is also OK if the old symbol
1486 is undefined and the new symbol is defined. */
252b5132 1487
79349b09
AM
1488 if (oldweak
1489 || newweak
1490 || (newdef
1491 && h->root.type == bfd_link_hash_undefined))
1492 *type_change_ok = TRUE;
1493
1494 /* It's OK to change the size if either the existing symbol or the
1495 new symbol is weak, or if the old symbol is undefined. */
1496
1497 if (*type_change_ok
1498 || h->root.type == bfd_link_hash_undefined)
1499 *size_change_ok = TRUE;
45d6a902 1500
45d6a902
AM
1501 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1502 symbol, respectively, appears to be a common symbol in a dynamic
1503 object. If a symbol appears in an uninitialized section, and is
1504 not weak, and is not a function, then it may be a common symbol
1505 which was resolved when the dynamic object was created. We want
1506 to treat such symbols specially, because they raise special
1507 considerations when setting the symbol size: if the symbol
1508 appears as a common symbol in a regular object, and the size in
1509 the regular object is larger, we must make sure that we use the
1510 larger size. This problematic case can always be avoided in C,
1511 but it must be handled correctly when using Fortran shared
1512 libraries.
1513
1514 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1515 likewise for OLDDYNCOMMON and OLDDEF.
1516
1517 Note that this test is just a heuristic, and that it is quite
1518 possible to have an uninitialized symbol in a shared object which
1519 is really a definition, rather than a common symbol. This could
1520 lead to some minor confusion when the symbol really is a common
1521 symbol in some regular object. However, I think it will be
1522 harmless. */
1523
1524 if (newdyn
1525 && newdef
79349b09 1526 && !newweak
45d6a902
AM
1527 && (sec->flags & SEC_ALLOC) != 0
1528 && (sec->flags & SEC_LOAD) == 0
1529 && sym->st_size > 0
0a36a439 1530 && !newfunc)
45d6a902
AM
1531 newdyncommon = TRUE;
1532 else
1533 newdyncommon = FALSE;
1534
1535 if (olddyn
1536 && olddef
1537 && h->root.type == bfd_link_hash_defined
f5385ebf 1538 && h->def_dynamic
45d6a902
AM
1539 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1540 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1541 && h->size > 0
0a36a439 1542 && !oldfunc)
45d6a902
AM
1543 olddyncommon = TRUE;
1544 else
1545 olddyncommon = FALSE;
1546
a4d8e49b
L
1547 /* We now know everything about the old and new symbols. We ask the
1548 backend to check if we can merge them. */
5d13b3b3
AM
1549 if (bed->merge_symbol != NULL)
1550 {
1551 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1552 return FALSE;
1553 sec = *psec;
1554 }
a4d8e49b 1555
93f4de39
RL
1556 /* There are multiple definitions of a normal symbol.
1557 Skip the default symbol as well. */
1558 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1559 && !default_sym && h->def_regular)
1560 {
1561 /* Handle a multiple definition. */
1562 (*info->callbacks->multiple_definition) (info, &h->root,
1563 abfd, sec, *pvalue);
1564 *skip = TRUE;
1565 return TRUE;
1566 }
1567
45d6a902
AM
1568 /* If both the old and the new symbols look like common symbols in a
1569 dynamic object, set the size of the symbol to the larger of the
1570 two. */
1571
1572 if (olddyncommon
1573 && newdyncommon
1574 && sym->st_size != h->size)
1575 {
1576 /* Since we think we have two common symbols, issue a multiple
1577 common warning if desired. Note that we only warn if the
1578 size is different. If the size is the same, we simply let
1579 the old symbol override the new one as normally happens with
1580 symbols defined in dynamic objects. */
1581
1a72702b
AM
1582 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1583 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1584 if (sym->st_size > h->size)
1585 h->size = sym->st_size;
252b5132 1586
45d6a902 1587 *size_change_ok = TRUE;
252b5132
RH
1588 }
1589
45d6a902
AM
1590 /* If we are looking at a dynamic object, and we have found a
1591 definition, we need to see if the symbol was already defined by
1592 some other object. If so, we want to use the existing
1593 definition, and we do not want to report a multiple symbol
1594 definition error; we do this by clobbering *PSEC to be
1595 bfd_und_section_ptr.
1596
1597 We treat a common symbol as a definition if the symbol in the
1598 shared library is a function, since common symbols always
1599 represent variables; this can cause confusion in principle, but
1600 any such confusion would seem to indicate an erroneous program or
1601 shared library. We also permit a common symbol in a regular
8170f769 1602 object to override a weak symbol in a shared object. */
45d6a902
AM
1603
1604 if (newdyn
1605 && newdef
77cfaee6 1606 && (olddef
45d6a902 1607 || (h->root.type == bfd_link_hash_common
8170f769 1608 && (newweak || newfunc))))
45d6a902
AM
1609 {
1610 *override = TRUE;
1611 newdef = FALSE;
1612 newdyncommon = FALSE;
252b5132 1613
45d6a902
AM
1614 *psec = sec = bfd_und_section_ptr;
1615 *size_change_ok = TRUE;
252b5132 1616
45d6a902
AM
1617 /* If we get here when the old symbol is a common symbol, then
1618 we are explicitly letting it override a weak symbol or
1619 function in a dynamic object, and we don't want to warn about
1620 a type change. If the old symbol is a defined symbol, a type
1621 change warning may still be appropriate. */
252b5132 1622
45d6a902
AM
1623 if (h->root.type == bfd_link_hash_common)
1624 *type_change_ok = TRUE;
1625 }
1626
1627 /* Handle the special case of an old common symbol merging with a
1628 new symbol which looks like a common symbol in a shared object.
1629 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1630 common symbol, and let _bfd_generic_link_add_one_symbol do the
1631 right thing. */
45d6a902
AM
1632
1633 if (newdyncommon
1634 && h->root.type == bfd_link_hash_common)
1635 {
1636 *override = TRUE;
1637 newdef = FALSE;
1638 newdyncommon = FALSE;
1639 *pvalue = sym->st_size;
a4d8e49b 1640 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1641 *size_change_ok = TRUE;
1642 }
1643
c5e2cead 1644 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1645 if (newdef && olddef && newweak)
54ac0771 1646 {
35ed3f94 1647 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1648 if (!(oldbfd != NULL
1649 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1650 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1651 {
1652 newdef = FALSE;
1653 *skip = TRUE;
1654 }
54ac0771
L
1655
1656 /* Merge st_other. If the symbol already has a dynamic index,
1657 but visibility says it should not be visible, turn it into a
1658 local symbol. */
b8417128 1659 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1660 if (h->dynindx != -1)
1661 switch (ELF_ST_VISIBILITY (h->other))
1662 {
1663 case STV_INTERNAL:
1664 case STV_HIDDEN:
1665 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1666 break;
1667 }
1668 }
c5e2cead 1669
45d6a902
AM
1670 /* If the old symbol is from a dynamic object, and the new symbol is
1671 a definition which is not from a dynamic object, then the new
1672 symbol overrides the old symbol. Symbols from regular files
1673 always take precedence over symbols from dynamic objects, even if
1674 they are defined after the dynamic object in the link.
1675
1676 As above, we again permit a common symbol in a regular object to
1677 override a definition in a shared object if the shared object
0f8a2703 1678 symbol is a function or is weak. */
45d6a902
AM
1679
1680 flip = NULL;
77cfaee6 1681 if (!newdyn
45d6a902
AM
1682 && (newdef
1683 || (bfd_is_com_section (sec)
0a36a439 1684 && (oldweak || oldfunc)))
45d6a902
AM
1685 && olddyn
1686 && olddef
f5385ebf 1687 && h->def_dynamic)
45d6a902
AM
1688 {
1689 /* Change the hash table entry to undefined, and let
1690 _bfd_generic_link_add_one_symbol do the right thing with the
1691 new definition. */
1692
1693 h->root.type = bfd_link_hash_undefined;
1694 h->root.u.undef.abfd = h->root.u.def.section->owner;
1695 *size_change_ok = TRUE;
1696
1697 olddef = FALSE;
1698 olddyncommon = FALSE;
1699
1700 /* We again permit a type change when a common symbol may be
1701 overriding a function. */
1702
1703 if (bfd_is_com_section (sec))
0a36a439
L
1704 {
1705 if (oldfunc)
1706 {
1707 /* If a common symbol overrides a function, make sure
1708 that it isn't defined dynamically nor has type
1709 function. */
1710 h->def_dynamic = 0;
1711 h->type = STT_NOTYPE;
1712 }
1713 *type_change_ok = TRUE;
1714 }
45d6a902 1715
6c9b78e6
AM
1716 if (hi->root.type == bfd_link_hash_indirect)
1717 flip = hi;
45d6a902
AM
1718 else
1719 /* This union may have been set to be non-NULL when this symbol
1720 was seen in a dynamic object. We must force the union to be
1721 NULL, so that it is correct for a regular symbol. */
1722 h->verinfo.vertree = NULL;
1723 }
1724
1725 /* Handle the special case of a new common symbol merging with an
1726 old symbol that looks like it might be a common symbol defined in
1727 a shared object. Note that we have already handled the case in
1728 which a new common symbol should simply override the definition
1729 in the shared library. */
1730
1731 if (! newdyn
1732 && bfd_is_com_section (sec)
1733 && olddyncommon)
1734 {
1735 /* It would be best if we could set the hash table entry to a
1736 common symbol, but we don't know what to use for the section
1737 or the alignment. */
1a72702b
AM
1738 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1739 bfd_link_hash_common, sym->st_size);
45d6a902 1740
4cc11e76 1741 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1742 larger, pretend that the new symbol has its size. */
1743
1744 if (h->size > *pvalue)
1745 *pvalue = h->size;
1746
af44c138
L
1747 /* We need to remember the alignment required by the symbol
1748 in the dynamic object. */
1749 BFD_ASSERT (pold_alignment);
1750 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1751
1752 olddef = FALSE;
1753 olddyncommon = FALSE;
1754
1755 h->root.type = bfd_link_hash_undefined;
1756 h->root.u.undef.abfd = h->root.u.def.section->owner;
1757
1758 *size_change_ok = TRUE;
1759 *type_change_ok = TRUE;
1760
6c9b78e6
AM
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
45d6a902
AM
1763 else
1764 h->verinfo.vertree = NULL;
1765 }
1766
1767 if (flip != NULL)
1768 {
1769 /* Handle the case where we had a versioned symbol in a dynamic
1770 library and now find a definition in a normal object. In this
1771 case, we make the versioned symbol point to the normal one. */
45d6a902 1772 flip->root.type = h->root.type;
00cbee0a 1773 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1774 h->root.type = bfd_link_hash_indirect;
1775 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1776 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1777 if (h->def_dynamic)
45d6a902 1778 {
f5385ebf
AM
1779 h->def_dynamic = 0;
1780 flip->ref_dynamic = 1;
45d6a902
AM
1781 }
1782 }
1783
45d6a902
AM
1784 return TRUE;
1785}
1786
1787/* This function is called to create an indirect symbol from the
1788 default for the symbol with the default version if needed. The
4f3fedcf 1789 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1790 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1791
28caa186 1792static bfd_boolean
268b6b39
AM
1793_bfd_elf_add_default_symbol (bfd *abfd,
1794 struct bfd_link_info *info,
1795 struct elf_link_hash_entry *h,
1796 const char *name,
1797 Elf_Internal_Sym *sym,
4f3fedcf
AM
1798 asection *sec,
1799 bfd_vma value,
1800 bfd **poldbfd,
e3c9d234 1801 bfd_boolean *dynsym)
45d6a902
AM
1802{
1803 bfd_boolean type_change_ok;
1804 bfd_boolean size_change_ok;
1805 bfd_boolean skip;
1806 char *shortname;
1807 struct elf_link_hash_entry *hi;
1808 struct bfd_link_hash_entry *bh;
9c5bfbb7 1809 const struct elf_backend_data *bed;
45d6a902
AM
1810 bfd_boolean collect;
1811 bfd_boolean dynamic;
e3c9d234 1812 bfd_boolean override;
45d6a902
AM
1813 char *p;
1814 size_t len, shortlen;
ffd65175 1815 asection *tmp_sec;
6e33951e 1816 bfd_boolean matched;
45d6a902 1817
422f1182
L
1818 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1819 return TRUE;
1820
45d6a902
AM
1821 /* If this symbol has a version, and it is the default version, we
1822 create an indirect symbol from the default name to the fully
1823 decorated name. This will cause external references which do not
1824 specify a version to be bound to this version of the symbol. */
1825 p = strchr (name, ELF_VER_CHR);
422f1182
L
1826 if (h->versioned == unknown)
1827 {
1828 if (p == NULL)
1829 {
1830 h->versioned = unversioned;
1831 return TRUE;
1832 }
1833 else
1834 {
1835 if (p[1] != ELF_VER_CHR)
1836 {
1837 h->versioned = versioned_hidden;
1838 return TRUE;
1839 }
1840 else
1841 h->versioned = versioned;
1842 }
1843 }
4373f8af
L
1844 else
1845 {
1846 /* PR ld/19073: We may see an unversioned definition after the
1847 default version. */
1848 if (p == NULL)
1849 return TRUE;
1850 }
45d6a902 1851
45d6a902
AM
1852 bed = get_elf_backend_data (abfd);
1853 collect = bed->collect;
1854 dynamic = (abfd->flags & DYNAMIC) != 0;
1855
1856 shortlen = p - name;
a50b1753 1857 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1858 if (shortname == NULL)
1859 return FALSE;
1860 memcpy (shortname, name, shortlen);
1861 shortname[shortlen] = '\0';
1862
1863 /* We are going to create a new symbol. Merge it with any existing
1864 symbol with this name. For the purposes of the merge, act as
1865 though we were defining the symbol we just defined, although we
1866 actually going to define an indirect symbol. */
1867 type_change_ok = FALSE;
1868 size_change_ok = FALSE;
6e33951e 1869 matched = TRUE;
ffd65175
AM
1870 tmp_sec = sec;
1871 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1872 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1873 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1874 return FALSE;
1875
1876 if (skip)
1877 goto nondefault;
1878
5b677558
AM
1879 if (hi->def_regular)
1880 {
1881 /* If the undecorated symbol will have a version added by a
1882 script different to H, then don't indirect to/from the
1883 undecorated symbol. This isn't ideal because we may not yet
1884 have seen symbol versions, if given by a script on the
1885 command line rather than via --version-script. */
1886 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1887 {
1888 bfd_boolean hide;
1889
1890 hi->verinfo.vertree
1891 = bfd_find_version_for_sym (info->version_info,
1892 hi->root.root.string, &hide);
1893 if (hi->verinfo.vertree != NULL && hide)
1894 {
1895 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1896 goto nondefault;
1897 }
1898 }
1899 if (hi->verinfo.vertree != NULL
1900 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1901 goto nondefault;
1902 }
1903
45d6a902
AM
1904 if (! override)
1905 {
c6e8a9a8 1906 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1907 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1908 {
1909 bh = &hi->root;
1910 if (! (_bfd_generic_link_add_one_symbol
1911 (info, abfd, shortname, BSF_INDIRECT,
1912 bfd_ind_section_ptr,
1913 0, name, FALSE, collect, &bh)))
1914 return FALSE;
1915 hi = (struct elf_link_hash_entry *) bh;
1916 }
45d6a902
AM
1917 }
1918 else
1919 {
1920 /* In this case the symbol named SHORTNAME is overriding the
1921 indirect symbol we want to add. We were planning on making
1922 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1923 is the name without a version. NAME is the fully versioned
1924 name, and it is the default version.
1925
1926 Overriding means that we already saw a definition for the
1927 symbol SHORTNAME in a regular object, and it is overriding
1928 the symbol defined in the dynamic object.
1929
1930 When this happens, we actually want to change NAME, the
1931 symbol we just added, to refer to SHORTNAME. This will cause
1932 references to NAME in the shared object to become references
1933 to SHORTNAME in the regular object. This is what we expect
1934 when we override a function in a shared object: that the
1935 references in the shared object will be mapped to the
1936 definition in the regular object. */
1937
1938 while (hi->root.type == bfd_link_hash_indirect
1939 || hi->root.type == bfd_link_hash_warning)
1940 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1941
1942 h->root.type = bfd_link_hash_indirect;
1943 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1944 if (h->def_dynamic)
45d6a902 1945 {
f5385ebf
AM
1946 h->def_dynamic = 0;
1947 hi->ref_dynamic = 1;
1948 if (hi->ref_regular
1949 || hi->def_regular)
45d6a902 1950 {
c152c796 1951 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1952 return FALSE;
1953 }
1954 }
1955
1956 /* Now set HI to H, so that the following code will set the
1957 other fields correctly. */
1958 hi = h;
1959 }
1960
fab4a87f
L
1961 /* Check if HI is a warning symbol. */
1962 if (hi->root.type == bfd_link_hash_warning)
1963 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1964
45d6a902
AM
1965 /* If there is a duplicate definition somewhere, then HI may not
1966 point to an indirect symbol. We will have reported an error to
1967 the user in that case. */
1968
1969 if (hi->root.type == bfd_link_hash_indirect)
1970 {
1971 struct elf_link_hash_entry *ht;
1972
45d6a902 1973 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1974 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1975
68c88cd4
AM
1976 /* A reference to the SHORTNAME symbol from a dynamic library
1977 will be satisfied by the versioned symbol at runtime. In
1978 effect, we have a reference to the versioned symbol. */
1979 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1980 hi->dynamic_def |= ht->dynamic_def;
1981
45d6a902
AM
1982 /* See if the new flags lead us to realize that the symbol must
1983 be dynamic. */
1984 if (! *dynsym)
1985 {
1986 if (! dynamic)
1987 {
0e1862bb 1988 if (! bfd_link_executable (info)
90c984fc 1989 || hi->def_dynamic
f5385ebf 1990 || hi->ref_dynamic)
45d6a902
AM
1991 *dynsym = TRUE;
1992 }
1993 else
1994 {
f5385ebf 1995 if (hi->ref_regular)
45d6a902
AM
1996 *dynsym = TRUE;
1997 }
1998 }
1999 }
2000
2001 /* We also need to define an indirection from the nondefault version
2002 of the symbol. */
2003
2004nondefault:
2005 len = strlen (name);
a50b1753 2006 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2007 if (shortname == NULL)
2008 return FALSE;
2009 memcpy (shortname, name, shortlen);
2010 memcpy (shortname + shortlen, p + 1, len - shortlen);
2011
2012 /* Once again, merge with any existing symbol. */
2013 type_change_ok = FALSE;
2014 size_change_ok = FALSE;
ffd65175
AM
2015 tmp_sec = sec;
2016 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2017 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2018 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2019 return FALSE;
2020
2021 if (skip)
2022 return TRUE;
2023
2024 if (override)
2025 {
2026 /* Here SHORTNAME is a versioned name, so we don't expect to see
2027 the type of override we do in the case above unless it is
4cc11e76 2028 overridden by a versioned definition. */
45d6a902
AM
2029 if (hi->root.type != bfd_link_hash_defined
2030 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2031 _bfd_error_handler
695344c0 2032 /* xgettext:c-format */
d003868e
AM
2033 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2034 abfd, shortname);
45d6a902
AM
2035 }
2036 else
2037 {
2038 bh = &hi->root;
2039 if (! (_bfd_generic_link_add_one_symbol
2040 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2041 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2042 return FALSE;
2043 hi = (struct elf_link_hash_entry *) bh;
2044
2045 /* If there is a duplicate definition somewhere, then HI may not
2046 point to an indirect symbol. We will have reported an error
2047 to the user in that case. */
2048
2049 if (hi->root.type == bfd_link_hash_indirect)
2050 {
fcfa13d2 2051 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2052 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2053 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2054
2055 /* See if the new flags lead us to realize that the symbol
2056 must be dynamic. */
2057 if (! *dynsym)
2058 {
2059 if (! dynamic)
2060 {
0e1862bb 2061 if (! bfd_link_executable (info)
f5385ebf 2062 || hi->ref_dynamic)
45d6a902
AM
2063 *dynsym = TRUE;
2064 }
2065 else
2066 {
f5385ebf 2067 if (hi->ref_regular)
45d6a902
AM
2068 *dynsym = TRUE;
2069 }
2070 }
2071 }
2072 }
2073
2074 return TRUE;
2075}
2076\f
2077/* This routine is used to export all defined symbols into the dynamic
2078 symbol table. It is called via elf_link_hash_traverse. */
2079
28caa186 2080static bfd_boolean
268b6b39 2081_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2082{
a50b1753 2083 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2084
2085 /* Ignore indirect symbols. These are added by the versioning code. */
2086 if (h->root.type == bfd_link_hash_indirect)
2087 return TRUE;
2088
7686d77d
AM
2089 /* Ignore this if we won't export it. */
2090 if (!eif->info->export_dynamic && !h->dynamic)
2091 return TRUE;
45d6a902
AM
2092
2093 if (h->dynindx == -1
fd91d419
L
2094 && (h->def_regular || h->ref_regular)
2095 && ! bfd_hide_sym_by_version (eif->info->version_info,
2096 h->root.root.string))
45d6a902 2097 {
fd91d419 2098 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2099 {
fd91d419
L
2100 eif->failed = TRUE;
2101 return FALSE;
45d6a902
AM
2102 }
2103 }
2104
2105 return TRUE;
2106}
2107\f
2108/* Look through the symbols which are defined in other shared
2109 libraries and referenced here. Update the list of version
2110 dependencies. This will be put into the .gnu.version_r section.
2111 This function is called via elf_link_hash_traverse. */
2112
28caa186 2113static bfd_boolean
268b6b39
AM
2114_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2115 void *data)
45d6a902 2116{
a50b1753 2117 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2118 Elf_Internal_Verneed *t;
2119 Elf_Internal_Vernaux *a;
2120 bfd_size_type amt;
2121
45d6a902
AM
2122 /* We only care about symbols defined in shared objects with version
2123 information. */
f5385ebf
AM
2124 if (!h->def_dynamic
2125 || h->def_regular
45d6a902 2126 || h->dynindx == -1
7b20f099
AM
2127 || h->verinfo.verdef == NULL
2128 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2129 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2130 return TRUE;
2131
2132 /* See if we already know about this version. */
28caa186
AM
2133 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2134 t != NULL;
2135 t = t->vn_nextref)
45d6a902
AM
2136 {
2137 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2138 continue;
2139
2140 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2141 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2142 return TRUE;
2143
2144 break;
2145 }
2146
2147 /* This is a new version. Add it to tree we are building. */
2148
2149 if (t == NULL)
2150 {
2151 amt = sizeof *t;
a50b1753 2152 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2153 if (t == NULL)
2154 {
2155 rinfo->failed = TRUE;
2156 return FALSE;
2157 }
2158
2159 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2160 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2161 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2162 }
2163
2164 amt = sizeof *a;
a50b1753 2165 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2166 if (a == NULL)
2167 {
2168 rinfo->failed = TRUE;
2169 return FALSE;
2170 }
45d6a902
AM
2171
2172 /* Note that we are copying a string pointer here, and testing it
2173 above. If bfd_elf_string_from_elf_section is ever changed to
2174 discard the string data when low in memory, this will have to be
2175 fixed. */
2176 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2177
2178 a->vna_flags = h->verinfo.verdef->vd_flags;
2179 a->vna_nextptr = t->vn_auxptr;
2180
2181 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2182 ++rinfo->vers;
2183
2184 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2185
2186 t->vn_auxptr = a;
2187
2188 return TRUE;
2189}
2190
2191/* Figure out appropriate versions for all the symbols. We may not
2192 have the version number script until we have read all of the input
2193 files, so until that point we don't know which symbols should be
2194 local. This function is called via elf_link_hash_traverse. */
2195
28caa186 2196static bfd_boolean
268b6b39 2197_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2198{
28caa186 2199 struct elf_info_failed *sinfo;
45d6a902 2200 struct bfd_link_info *info;
9c5bfbb7 2201 const struct elf_backend_data *bed;
45d6a902
AM
2202 struct elf_info_failed eif;
2203 char *p;
45d6a902 2204
a50b1753 2205 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2206 info = sinfo->info;
2207
45d6a902
AM
2208 /* Fix the symbol flags. */
2209 eif.failed = FALSE;
2210 eif.info = info;
2211 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2212 {
2213 if (eif.failed)
2214 sinfo->failed = TRUE;
2215 return FALSE;
2216 }
2217
2218 /* We only need version numbers for symbols defined in regular
2219 objects. */
f5385ebf 2220 if (!h->def_regular)
45d6a902
AM
2221 return TRUE;
2222
28caa186 2223 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2224 p = strchr (h->root.root.string, ELF_VER_CHR);
2225 if (p != NULL && h->verinfo.vertree == NULL)
2226 {
2227 struct bfd_elf_version_tree *t;
45d6a902 2228
45d6a902
AM
2229 ++p;
2230 if (*p == ELF_VER_CHR)
6e33951e 2231 ++p;
45d6a902
AM
2232
2233 /* If there is no version string, we can just return out. */
2234 if (*p == '\0')
6e33951e 2235 return TRUE;
45d6a902
AM
2236
2237 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2238 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2239 {
2240 if (strcmp (t->name, p) == 0)
2241 {
2242 size_t len;
2243 char *alc;
2244 struct bfd_elf_version_expr *d;
2245
2246 len = p - h->root.root.string;
a50b1753 2247 alc = (char *) bfd_malloc (len);
45d6a902 2248 if (alc == NULL)
14b1c01e
AM
2249 {
2250 sinfo->failed = TRUE;
2251 return FALSE;
2252 }
45d6a902
AM
2253 memcpy (alc, h->root.root.string, len - 1);
2254 alc[len - 1] = '\0';
2255 if (alc[len - 2] == ELF_VER_CHR)
2256 alc[len - 2] = '\0';
2257
2258 h->verinfo.vertree = t;
2259 t->used = TRUE;
2260 d = NULL;
2261
108ba305
JJ
2262 if (t->globals.list != NULL)
2263 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2264
2265 /* See if there is anything to force this symbol to
2266 local scope. */
108ba305 2267 if (d == NULL && t->locals.list != NULL)
45d6a902 2268 {
108ba305
JJ
2269 d = (*t->match) (&t->locals, NULL, alc);
2270 if (d != NULL
2271 && h->dynindx != -1
108ba305
JJ
2272 && ! info->export_dynamic)
2273 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2274 }
2275
2276 free (alc);
2277 break;
2278 }
2279 }
2280
2281 /* If we are building an application, we need to create a
2282 version node for this version. */
0e1862bb 2283 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2284 {
2285 struct bfd_elf_version_tree **pp;
2286 int version_index;
2287
2288 /* If we aren't going to export this symbol, we don't need
2289 to worry about it. */
2290 if (h->dynindx == -1)
2291 return TRUE;
2292
ef53be89
AM
2293 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2294 sizeof *t);
45d6a902
AM
2295 if (t == NULL)
2296 {
2297 sinfo->failed = TRUE;
2298 return FALSE;
2299 }
2300
45d6a902 2301 t->name = p;
45d6a902
AM
2302 t->name_indx = (unsigned int) -1;
2303 t->used = TRUE;
2304
2305 version_index = 1;
2306 /* Don't count anonymous version tag. */
fd91d419
L
2307 if (sinfo->info->version_info != NULL
2308 && sinfo->info->version_info->vernum == 0)
45d6a902 2309 version_index = 0;
fd91d419
L
2310 for (pp = &sinfo->info->version_info;
2311 *pp != NULL;
2312 pp = &(*pp)->next)
45d6a902
AM
2313 ++version_index;
2314 t->vernum = version_index;
2315
2316 *pp = t;
2317
2318 h->verinfo.vertree = t;
2319 }
2320 else if (t == NULL)
2321 {
2322 /* We could not find the version for a symbol when
2323 generating a shared archive. Return an error. */
4eca0228 2324 _bfd_error_handler
695344c0 2325 /* xgettext:c-format */
c55fe096 2326 (_("%B: version node not found for symbol %s"),
28caa186 2327 info->output_bfd, h->root.root.string);
45d6a902
AM
2328 bfd_set_error (bfd_error_bad_value);
2329 sinfo->failed = TRUE;
2330 return FALSE;
2331 }
45d6a902
AM
2332 }
2333
2334 /* If we don't have a version for this symbol, see if we can find
2335 something. */
fd91d419 2336 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2337 {
1e8fa21e 2338 bfd_boolean hide;
ae5a3597 2339
fd91d419
L
2340 h->verinfo.vertree
2341 = bfd_find_version_for_sym (sinfo->info->version_info,
2342 h->root.root.string, &hide);
1e8fa21e
AM
2343 if (h->verinfo.vertree != NULL && hide)
2344 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2345 }
2346
2347 return TRUE;
2348}
2349\f
45d6a902
AM
2350/* Read and swap the relocs from the section indicated by SHDR. This
2351 may be either a REL or a RELA section. The relocations are
2352 translated into RELA relocations and stored in INTERNAL_RELOCS,
2353 which should have already been allocated to contain enough space.
2354 The EXTERNAL_RELOCS are a buffer where the external form of the
2355 relocations should be stored.
2356
2357 Returns FALSE if something goes wrong. */
2358
2359static bfd_boolean
268b6b39 2360elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2361 asection *sec,
268b6b39
AM
2362 Elf_Internal_Shdr *shdr,
2363 void *external_relocs,
2364 Elf_Internal_Rela *internal_relocs)
45d6a902 2365{
9c5bfbb7 2366 const struct elf_backend_data *bed;
268b6b39 2367 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2368 const bfd_byte *erela;
2369 const bfd_byte *erelaend;
2370 Elf_Internal_Rela *irela;
243ef1e0
L
2371 Elf_Internal_Shdr *symtab_hdr;
2372 size_t nsyms;
45d6a902 2373
45d6a902
AM
2374 /* Position ourselves at the start of the section. */
2375 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2376 return FALSE;
2377
2378 /* Read the relocations. */
2379 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2380 return FALSE;
2381
243ef1e0 2382 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2383 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2384
45d6a902
AM
2385 bed = get_elf_backend_data (abfd);
2386
2387 /* Convert the external relocations to the internal format. */
2388 if (shdr->sh_entsize == bed->s->sizeof_rel)
2389 swap_in = bed->s->swap_reloc_in;
2390 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2391 swap_in = bed->s->swap_reloca_in;
2392 else
2393 {
2394 bfd_set_error (bfd_error_wrong_format);
2395 return FALSE;
2396 }
2397
a50b1753 2398 erela = (const bfd_byte *) external_relocs;
51992aec 2399 erelaend = erela + shdr->sh_size;
45d6a902
AM
2400 irela = internal_relocs;
2401 while (erela < erelaend)
2402 {
243ef1e0
L
2403 bfd_vma r_symndx;
2404
45d6a902 2405 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2406 r_symndx = ELF32_R_SYM (irela->r_info);
2407 if (bed->s->arch_size == 64)
2408 r_symndx >>= 24;
ce98a316
NC
2409 if (nsyms > 0)
2410 {
2411 if ((size_t) r_symndx >= nsyms)
2412 {
4eca0228 2413 _bfd_error_handler
695344c0 2414 /* xgettext:c-format */
d42c267e 2415 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
76cfced5 2416 " for offset %#Lx in section `%A'"),
d42c267e 2417 abfd, r_symndx, (unsigned long) nsyms,
c08bb8dd 2418 irela->r_offset, sec);
ce98a316
NC
2419 bfd_set_error (bfd_error_bad_value);
2420 return FALSE;
2421 }
2422 }
cf35638d 2423 else if (r_symndx != STN_UNDEF)
243ef1e0 2424 {
4eca0228 2425 _bfd_error_handler
695344c0 2426 /* xgettext:c-format */
d42c267e 2427 (_("%B: non-zero symbol index (%#Lx)"
76cfced5 2428 " for offset %#Lx in section `%A'"
ce98a316 2429 " when the object file has no symbol table"),
d42c267e 2430 abfd, r_symndx,
c08bb8dd 2431 irela->r_offset, sec);
243ef1e0
L
2432 bfd_set_error (bfd_error_bad_value);
2433 return FALSE;
2434 }
45d6a902
AM
2435 irela += bed->s->int_rels_per_ext_rel;
2436 erela += shdr->sh_entsize;
2437 }
2438
2439 return TRUE;
2440}
2441
2442/* Read and swap the relocs for a section O. They may have been
2443 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2444 not NULL, they are used as buffers to read into. They are known to
2445 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2446 the return value is allocated using either malloc or bfd_alloc,
2447 according to the KEEP_MEMORY argument. If O has two relocation
2448 sections (both REL and RELA relocations), then the REL_HDR
2449 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2450 RELA_HDR relocations. */
45d6a902
AM
2451
2452Elf_Internal_Rela *
268b6b39
AM
2453_bfd_elf_link_read_relocs (bfd *abfd,
2454 asection *o,
2455 void *external_relocs,
2456 Elf_Internal_Rela *internal_relocs,
2457 bfd_boolean keep_memory)
45d6a902 2458{
268b6b39 2459 void *alloc1 = NULL;
45d6a902 2460 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2461 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2462 struct bfd_elf_section_data *esdo = elf_section_data (o);
2463 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2464
d4730f92
BS
2465 if (esdo->relocs != NULL)
2466 return esdo->relocs;
45d6a902
AM
2467
2468 if (o->reloc_count == 0)
2469 return NULL;
2470
45d6a902
AM
2471 if (internal_relocs == NULL)
2472 {
2473 bfd_size_type size;
2474
056bafd4 2475 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2476 if (keep_memory)
a50b1753 2477 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2478 else
a50b1753 2479 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2480 if (internal_relocs == NULL)
2481 goto error_return;
2482 }
2483
2484 if (external_relocs == NULL)
2485 {
d4730f92
BS
2486 bfd_size_type size = 0;
2487
2488 if (esdo->rel.hdr)
2489 size += esdo->rel.hdr->sh_size;
2490 if (esdo->rela.hdr)
2491 size += esdo->rela.hdr->sh_size;
45d6a902 2492
268b6b39 2493 alloc1 = bfd_malloc (size);
45d6a902
AM
2494 if (alloc1 == NULL)
2495 goto error_return;
2496 external_relocs = alloc1;
2497 }
2498
d4730f92
BS
2499 internal_rela_relocs = internal_relocs;
2500 if (esdo->rel.hdr)
2501 {
2502 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2503 external_relocs,
2504 internal_relocs))
2505 goto error_return;
2506 external_relocs = (((bfd_byte *) external_relocs)
2507 + esdo->rel.hdr->sh_size);
2508 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2509 * bed->s->int_rels_per_ext_rel);
2510 }
2511
2512 if (esdo->rela.hdr
2513 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2514 external_relocs,
2515 internal_rela_relocs)))
45d6a902
AM
2516 goto error_return;
2517
2518 /* Cache the results for next time, if we can. */
2519 if (keep_memory)
d4730f92 2520 esdo->relocs = internal_relocs;
45d6a902
AM
2521
2522 if (alloc1 != NULL)
2523 free (alloc1);
2524
2525 /* Don't free alloc2, since if it was allocated we are passing it
2526 back (under the name of internal_relocs). */
2527
2528 return internal_relocs;
2529
2530 error_return:
2531 if (alloc1 != NULL)
2532 free (alloc1);
2533 if (alloc2 != NULL)
4dd07732
AM
2534 {
2535 if (keep_memory)
2536 bfd_release (abfd, alloc2);
2537 else
2538 free (alloc2);
2539 }
45d6a902
AM
2540 return NULL;
2541}
2542
2543/* Compute the size of, and allocate space for, REL_HDR which is the
2544 section header for a section containing relocations for O. */
2545
28caa186 2546static bfd_boolean
9eaff861
AO
2547_bfd_elf_link_size_reloc_section (bfd *abfd,
2548 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2549{
9eaff861 2550 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2551
2552 /* That allows us to calculate the size of the section. */
9eaff861 2553 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2554
2555 /* The contents field must last into write_object_contents, so we
2556 allocate it with bfd_alloc rather than malloc. Also since we
2557 cannot be sure that the contents will actually be filled in,
2558 we zero the allocated space. */
a50b1753 2559 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2560 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2561 return FALSE;
2562
d4730f92 2563 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2564 {
2565 struct elf_link_hash_entry **p;
2566
ca4be51c
AM
2567 p = ((struct elf_link_hash_entry **)
2568 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2569 if (p == NULL)
2570 return FALSE;
2571
d4730f92 2572 reldata->hashes = p;
45d6a902
AM
2573 }
2574
2575 return TRUE;
2576}
2577
2578/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2579 originated from the section given by INPUT_REL_HDR) to the
2580 OUTPUT_BFD. */
2581
2582bfd_boolean
268b6b39
AM
2583_bfd_elf_link_output_relocs (bfd *output_bfd,
2584 asection *input_section,
2585 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2586 Elf_Internal_Rela *internal_relocs,
2587 struct elf_link_hash_entry **rel_hash
2588 ATTRIBUTE_UNUSED)
45d6a902
AM
2589{
2590 Elf_Internal_Rela *irela;
2591 Elf_Internal_Rela *irelaend;
2592 bfd_byte *erel;
d4730f92 2593 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2594 asection *output_section;
9c5bfbb7 2595 const struct elf_backend_data *bed;
268b6b39 2596 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2597 struct bfd_elf_section_data *esdo;
45d6a902
AM
2598
2599 output_section = input_section->output_section;
45d6a902 2600
d4730f92
BS
2601 bed = get_elf_backend_data (output_bfd);
2602 esdo = elf_section_data (output_section);
2603 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2604 {
d4730f92
BS
2605 output_reldata = &esdo->rel;
2606 swap_out = bed->s->swap_reloc_out;
45d6a902 2607 }
d4730f92
BS
2608 else if (esdo->rela.hdr
2609 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2610 {
d4730f92
BS
2611 output_reldata = &esdo->rela;
2612 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2613 }
2614 else
2615 {
4eca0228 2616 _bfd_error_handler
695344c0 2617 /* xgettext:c-format */
d003868e
AM
2618 (_("%B: relocation size mismatch in %B section %A"),
2619 output_bfd, input_section->owner, input_section);
297d8443 2620 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2621 return FALSE;
2622 }
2623
d4730f92
BS
2624 erel = output_reldata->hdr->contents;
2625 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2626 irela = internal_relocs;
2627 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2628 * bed->s->int_rels_per_ext_rel);
2629 while (irela < irelaend)
2630 {
2631 (*swap_out) (output_bfd, irela, erel);
2632 irela += bed->s->int_rels_per_ext_rel;
2633 erel += input_rel_hdr->sh_entsize;
2634 }
2635
2636 /* Bump the counter, so that we know where to add the next set of
2637 relocations. */
d4730f92 2638 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2639
2640 return TRUE;
2641}
2642\f
508c3946
L
2643/* Make weak undefined symbols in PIE dynamic. */
2644
2645bfd_boolean
2646_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2647 struct elf_link_hash_entry *h)
2648{
0e1862bb 2649 if (bfd_link_pie (info)
508c3946
L
2650 && h->dynindx == -1
2651 && h->root.type == bfd_link_hash_undefweak)
2652 return bfd_elf_link_record_dynamic_symbol (info, h);
2653
2654 return TRUE;
2655}
2656
45d6a902
AM
2657/* Fix up the flags for a symbol. This handles various cases which
2658 can only be fixed after all the input files are seen. This is
2659 currently called by both adjust_dynamic_symbol and
2660 assign_sym_version, which is unnecessary but perhaps more robust in
2661 the face of future changes. */
2662
28caa186 2663static bfd_boolean
268b6b39
AM
2664_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2665 struct elf_info_failed *eif)
45d6a902 2666{
33774f08 2667 const struct elf_backend_data *bed;
508c3946 2668
45d6a902
AM
2669 /* If this symbol was mentioned in a non-ELF file, try to set
2670 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2671 permit a non-ELF file to correctly refer to a symbol defined in
2672 an ELF dynamic object. */
f5385ebf 2673 if (h->non_elf)
45d6a902
AM
2674 {
2675 while (h->root.type == bfd_link_hash_indirect)
2676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2677
2678 if (h->root.type != bfd_link_hash_defined
2679 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2680 {
2681 h->ref_regular = 1;
2682 h->ref_regular_nonweak = 1;
2683 }
45d6a902
AM
2684 else
2685 {
2686 if (h->root.u.def.section->owner != NULL
2687 && (bfd_get_flavour (h->root.u.def.section->owner)
2688 == bfd_target_elf_flavour))
f5385ebf
AM
2689 {
2690 h->ref_regular = 1;
2691 h->ref_regular_nonweak = 1;
2692 }
45d6a902 2693 else
f5385ebf 2694 h->def_regular = 1;
45d6a902
AM
2695 }
2696
2697 if (h->dynindx == -1
f5385ebf
AM
2698 && (h->def_dynamic
2699 || h->ref_dynamic))
45d6a902 2700 {
c152c796 2701 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2702 {
2703 eif->failed = TRUE;
2704 return FALSE;
2705 }
2706 }
2707 }
2708 else
2709 {
f5385ebf 2710 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2711 was first seen in a non-ELF file. Fortunately, if the symbol
2712 was first seen in an ELF file, we're probably OK unless the
2713 symbol was defined in a non-ELF file. Catch that case here.
2714 FIXME: We're still in trouble if the symbol was first seen in
2715 a dynamic object, and then later in a non-ELF regular object. */
2716 if ((h->root.type == bfd_link_hash_defined
2717 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2718 && !h->def_regular
45d6a902
AM
2719 && (h->root.u.def.section->owner != NULL
2720 ? (bfd_get_flavour (h->root.u.def.section->owner)
2721 != bfd_target_elf_flavour)
2722 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2723 && !h->def_dynamic)))
2724 h->def_regular = 1;
45d6a902
AM
2725 }
2726
508c3946 2727 /* Backend specific symbol fixup. */
33774f08
AM
2728 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2729 if (bed->elf_backend_fixup_symbol
2730 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2731 return FALSE;
508c3946 2732
45d6a902
AM
2733 /* If this is a final link, and the symbol was defined as a common
2734 symbol in a regular object file, and there was no definition in
2735 any dynamic object, then the linker will have allocated space for
f5385ebf 2736 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2737 flag will not have been set. */
2738 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2739 && !h->def_regular
2740 && h->ref_regular
2741 && !h->def_dynamic
96f29d96 2742 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2743 h->def_regular = 1;
45d6a902 2744
4deb8f71
L
2745 /* If a weak undefined symbol has non-default visibility, we also
2746 hide it from the dynamic linker. */
2747 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2748 && h->root.type == bfd_link_hash_undefweak)
2749 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2750
2751 /* A hidden versioned symbol in executable should be forced local if
2752 it is is locally defined, not referenced by shared library and not
2753 exported. */
2754 else if (bfd_link_executable (eif->info)
2755 && h->versioned == versioned_hidden
2756 && !eif->info->export_dynamic
2757 && !h->dynamic
2758 && !h->ref_dynamic
2759 && h->def_regular)
2760 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2761
45d6a902
AM
2762 /* If -Bsymbolic was used (which means to bind references to global
2763 symbols to the definition within the shared object), and this
2764 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2765 need a PLT entry. Likewise, if the symbol has non-default
2766 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2767 will force it local. */
4deb8f71
L
2768 else if (h->needs_plt
2769 && bfd_link_pic (eif->info)
2770 && is_elf_hash_table (eif->info->hash)
2771 && (SYMBOLIC_BIND (eif->info, h)
2772 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2773 && h->def_regular)
45d6a902 2774 {
45d6a902
AM
2775 bfd_boolean force_local;
2776
45d6a902
AM
2777 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2778 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2779 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2780 }
2781
45d6a902
AM
2782 /* If this is a weak defined symbol in a dynamic object, and we know
2783 the real definition in the dynamic object, copy interesting flags
2784 over to the real definition. */
f6e332e6 2785 if (h->u.weakdef != NULL)
45d6a902 2786 {
45d6a902
AM
2787 /* If the real definition is defined by a regular object file,
2788 don't do anything special. See the longer description in
2789 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2790 if (h->u.weakdef->def_regular)
f6e332e6 2791 h->u.weakdef = NULL;
45d6a902 2792 else
a26587ba 2793 {
4e6b54a6
AM
2794 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2795
2796 while (h->root.type == bfd_link_hash_indirect)
2797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2798
2799 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2800 || h->root.type == bfd_link_hash_defweak);
2801 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2802 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2803 || weakdef->root.type == bfd_link_hash_defweak);
2804 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2805 }
45d6a902
AM
2806 }
2807
2808 return TRUE;
2809}
2810
2811/* Make the backend pick a good value for a dynamic symbol. This is
2812 called via elf_link_hash_traverse, and also calls itself
2813 recursively. */
2814
28caa186 2815static bfd_boolean
268b6b39 2816_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2817{
a50b1753 2818 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2819 struct elf_link_hash_table *htab;
9c5bfbb7 2820 const struct elf_backend_data *bed;
45d6a902 2821
0eddce27 2822 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2823 return FALSE;
2824
45d6a902
AM
2825 /* Ignore indirect symbols. These are added by the versioning code. */
2826 if (h->root.type == bfd_link_hash_indirect)
2827 return TRUE;
2828
2829 /* Fix the symbol flags. */
2830 if (! _bfd_elf_fix_symbol_flags (h, eif))
2831 return FALSE;
2832
559192d8
AM
2833 htab = elf_hash_table (eif->info);
2834 bed = get_elf_backend_data (htab->dynobj);
2835
954b63d4
AM
2836 if (h->root.type == bfd_link_hash_undefweak)
2837 {
2838 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2839 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2840 else if (eif->info->dynamic_undefined_weak > 0
2841 && h->ref_regular
2842 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2843 && !bfd_hide_sym_by_version (eif->info->version_info,
2844 h->root.root.string))
2845 {
2846 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2847 {
2848 eif->failed = TRUE;
2849 return FALSE;
2850 }
2851 }
2852 }
2853
45d6a902
AM
2854 /* If this symbol does not require a PLT entry, and it is not
2855 defined by a dynamic object, or is not referenced by a regular
2856 object, ignore it. We do have to handle a weak defined symbol,
2857 even if no regular object refers to it, if we decided to add it
2858 to the dynamic symbol table. FIXME: Do we normally need to worry
2859 about symbols which are defined by one dynamic object and
2860 referenced by another one? */
f5385ebf 2861 if (!h->needs_plt
91e21fb7 2862 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2863 && (h->def_regular
2864 || !h->def_dynamic
2865 || (!h->ref_regular
f6e332e6 2866 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2867 {
a6aa5195 2868 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2869 return TRUE;
2870 }
2871
2872 /* If we've already adjusted this symbol, don't do it again. This
2873 can happen via a recursive call. */
f5385ebf 2874 if (h->dynamic_adjusted)
45d6a902
AM
2875 return TRUE;
2876
2877 /* Don't look at this symbol again. Note that we must set this
2878 after checking the above conditions, because we may look at a
2879 symbol once, decide not to do anything, and then get called
2880 recursively later after REF_REGULAR is set below. */
f5385ebf 2881 h->dynamic_adjusted = 1;
45d6a902
AM
2882
2883 /* If this is a weak definition, and we know a real definition, and
2884 the real symbol is not itself defined by a regular object file,
2885 then get a good value for the real definition. We handle the
2886 real symbol first, for the convenience of the backend routine.
2887
2888 Note that there is a confusing case here. If the real definition
2889 is defined by a regular object file, we don't get the real symbol
2890 from the dynamic object, but we do get the weak symbol. If the
2891 processor backend uses a COPY reloc, then if some routine in the
2892 dynamic object changes the real symbol, we will not see that
2893 change in the corresponding weak symbol. This is the way other
2894 ELF linkers work as well, and seems to be a result of the shared
2895 library model.
2896
2897 I will clarify this issue. Most SVR4 shared libraries define the
2898 variable _timezone and define timezone as a weak synonym. The
2899 tzset call changes _timezone. If you write
2900 extern int timezone;
2901 int _timezone = 5;
2902 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2903 you might expect that, since timezone is a synonym for _timezone,
2904 the same number will print both times. However, if the processor
2905 backend uses a COPY reloc, then actually timezone will be copied
2906 into your process image, and, since you define _timezone
2907 yourself, _timezone will not. Thus timezone and _timezone will
2908 wind up at different memory locations. The tzset call will set
2909 _timezone, leaving timezone unchanged. */
2910
f6e332e6 2911 if (h->u.weakdef != NULL)
45d6a902 2912 {
ec24dc88
AM
2913 /* If we get to this point, there is an implicit reference to
2914 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2915 h->u.weakdef->ref_regular = 1;
45d6a902 2916
ec24dc88
AM
2917 /* Ensure that the backend adjust_dynamic_symbol function sees
2918 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2919 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2920 return FALSE;
2921 }
2922
2923 /* If a symbol has no type and no size and does not require a PLT
2924 entry, then we are probably about to do the wrong thing here: we
2925 are probably going to create a COPY reloc for an empty object.
2926 This case can arise when a shared object is built with assembly
2927 code, and the assembly code fails to set the symbol type. */
2928 if (h->size == 0
2929 && h->type == STT_NOTYPE
f5385ebf 2930 && !h->needs_plt)
4eca0228 2931 _bfd_error_handler
45d6a902
AM
2932 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2933 h->root.root.string);
2934
45d6a902
AM
2935 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2936 {
2937 eif->failed = TRUE;
2938 return FALSE;
2939 }
2940
2941 return TRUE;
2942}
2943
027297b7
L
2944/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2945 DYNBSS. */
2946
2947bfd_boolean
6cabe1ea
AM
2948_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2949 struct elf_link_hash_entry *h,
027297b7
L
2950 asection *dynbss)
2951{
91ac5911 2952 unsigned int power_of_two;
027297b7
L
2953 bfd_vma mask;
2954 asection *sec = h->root.u.def.section;
2955
de194d85 2956 /* The section alignment of the definition is the maximum alignment
91ac5911
L
2957 requirement of symbols defined in the section. Since we don't
2958 know the symbol alignment requirement, we start with the
2959 maximum alignment and check low bits of the symbol address
2960 for the minimum alignment. */
2961 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2962 mask = ((bfd_vma) 1 << power_of_two) - 1;
2963 while ((h->root.u.def.value & mask) != 0)
2964 {
2965 mask >>= 1;
2966 --power_of_two;
2967 }
027297b7 2968
91ac5911
L
2969 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2970 dynbss))
027297b7
L
2971 {
2972 /* Adjust the section alignment if needed. */
2973 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2974 power_of_two))
027297b7
L
2975 return FALSE;
2976 }
2977
91ac5911 2978 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2979 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2980
2981 /* Define the symbol as being at this point in DYNBSS. */
2982 h->root.u.def.section = dynbss;
2983 h->root.u.def.value = dynbss->size;
2984
2985 /* Increment the size of DYNBSS to make room for the symbol. */
2986 dynbss->size += h->size;
2987
f7483970
L
2988 /* No error if extern_protected_data is true. */
2989 if (h->protected_def
889c2a67
L
2990 && (!info->extern_protected_data
2991 || (info->extern_protected_data < 0
2992 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2993 info->callbacks->einfo
2994 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2995 h->root.root.string);
6cabe1ea 2996
027297b7
L
2997 return TRUE;
2998}
2999
45d6a902
AM
3000/* Adjust all external symbols pointing into SEC_MERGE sections
3001 to reflect the object merging within the sections. */
3002
28caa186 3003static bfd_boolean
268b6b39 3004_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3005{
3006 asection *sec;
3007
45d6a902
AM
3008 if ((h->root.type == bfd_link_hash_defined
3009 || h->root.type == bfd_link_hash_defweak)
3010 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3011 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3012 {
a50b1753 3013 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3014
3015 h->root.u.def.value =
3016 _bfd_merged_section_offset (output_bfd,
3017 &h->root.u.def.section,
3018 elf_section_data (sec)->sec_info,
753731ee 3019 h->root.u.def.value);
45d6a902
AM
3020 }
3021
3022 return TRUE;
3023}
986a241f
RH
3024
3025/* Returns false if the symbol referred to by H should be considered
3026 to resolve local to the current module, and true if it should be
3027 considered to bind dynamically. */
3028
3029bfd_boolean
268b6b39
AM
3030_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3031 struct bfd_link_info *info,
89a2ee5a 3032 bfd_boolean not_local_protected)
986a241f
RH
3033{
3034 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3035 const struct elf_backend_data *bed;
3036 struct elf_link_hash_table *hash_table;
986a241f
RH
3037
3038 if (h == NULL)
3039 return FALSE;
3040
3041 while (h->root.type == bfd_link_hash_indirect
3042 || h->root.type == bfd_link_hash_warning)
3043 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3044
3045 /* If it was forced local, then clearly it's not dynamic. */
3046 if (h->dynindx == -1)
3047 return FALSE;
f5385ebf 3048 if (h->forced_local)
986a241f
RH
3049 return FALSE;
3050
3051 /* Identify the cases where name binding rules say that a
3052 visible symbol resolves locally. */
0e1862bb
L
3053 binding_stays_local_p = (bfd_link_executable (info)
3054 || SYMBOLIC_BIND (info, h));
986a241f
RH
3055
3056 switch (ELF_ST_VISIBILITY (h->other))
3057 {
3058 case STV_INTERNAL:
3059 case STV_HIDDEN:
3060 return FALSE;
3061
3062 case STV_PROTECTED:
fcb93ecf
PB
3063 hash_table = elf_hash_table (info);
3064 if (!is_elf_hash_table (hash_table))
3065 return FALSE;
3066
3067 bed = get_elf_backend_data (hash_table->dynobj);
3068
986a241f
RH
3069 /* Proper resolution for function pointer equality may require
3070 that these symbols perhaps be resolved dynamically, even though
3071 we should be resolving them to the current module. */
89a2ee5a 3072 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3073 binding_stays_local_p = TRUE;
3074 break;
3075
3076 default:
986a241f
RH
3077 break;
3078 }
3079
aa37626c 3080 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3081 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3082 return TRUE;
3083
986a241f
RH
3084 /* Otherwise, the symbol is dynamic if binding rules don't tell
3085 us that it remains local. */
3086 return !binding_stays_local_p;
3087}
f6c52c13
AM
3088
3089/* Return true if the symbol referred to by H should be considered
3090 to resolve local to the current module, and false otherwise. Differs
3091 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3092 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3093 for the place where dynindx == -1 is tested. If that test is true,
3094 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3095 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3096 defined symbols.
89a2ee5a
AM
3097 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3098 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3099 treatment of undefined weak symbols. For those that do not make
3100 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3101
3102bfd_boolean
268b6b39
AM
3103_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3104 struct bfd_link_info *info,
3105 bfd_boolean local_protected)
f6c52c13 3106{
fcb93ecf
PB
3107 const struct elf_backend_data *bed;
3108 struct elf_link_hash_table *hash_table;
3109
f6c52c13
AM
3110 /* If it's a local sym, of course we resolve locally. */
3111 if (h == NULL)
3112 return TRUE;
3113
d95edcac
L
3114 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3115 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3116 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3117 return TRUE;
3118
0fad2956
MR
3119 /* Forced local symbols resolve locally. */
3120 if (h->forced_local)
3121 return TRUE;
3122
7e2294f9
AO
3123 /* Common symbols that become definitions don't get the DEF_REGULAR
3124 flag set, so test it first, and don't bail out. */
3125 if (ELF_COMMON_DEF_P (h))
3126 /* Do nothing. */;
f6c52c13 3127 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3128 resolve locally. The sym is either undefined or dynamic. */
3129 else if (!h->def_regular)
f6c52c13
AM
3130 return FALSE;
3131
0fad2956 3132 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3133 if (h->dynindx == -1)
3134 return TRUE;
3135
3136 /* At this point, we know the symbol is defined and dynamic. In an
3137 executable it must resolve locally, likewise when building symbolic
3138 shared libraries. */
0e1862bb 3139 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3140 return TRUE;
3141
3142 /* Now deal with defined dynamic symbols in shared libraries. Ones
3143 with default visibility might not resolve locally. */
3144 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3145 return FALSE;
3146
fcb93ecf
PB
3147 hash_table = elf_hash_table (info);
3148 if (!is_elf_hash_table (hash_table))
3149 return TRUE;
3150
3151 bed = get_elf_backend_data (hash_table->dynobj);
3152
f7483970
L
3153 /* If extern_protected_data is false, STV_PROTECTED non-function
3154 symbols are local. */
889c2a67
L
3155 if ((!info->extern_protected_data
3156 || (info->extern_protected_data < 0
3157 && !bed->extern_protected_data))
3158 && !bed->is_function_type (h->type))
1c16dfa5
L
3159 return TRUE;
3160
f6c52c13 3161 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3162 symbols be treated as dynamic symbols. If the address of a
3163 function not defined in an executable is set to that function's
3164 plt entry in the executable, then the address of the function in
3165 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3166 return local_protected;
3167}
e1918d23
AM
3168
3169/* Caches some TLS segment info, and ensures that the TLS segment vma is
3170 aligned. Returns the first TLS output section. */
3171
3172struct bfd_section *
3173_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3174{
3175 struct bfd_section *sec, *tls;
3176 unsigned int align = 0;
3177
3178 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3179 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3180 break;
3181 tls = sec;
3182
3183 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3184 if (sec->alignment_power > align)
3185 align = sec->alignment_power;
3186
3187 elf_hash_table (info)->tls_sec = tls;
3188
3189 /* Ensure the alignment of the first section is the largest alignment,
3190 so that the tls segment starts aligned. */
3191 if (tls != NULL)
3192 tls->alignment_power = align;
3193
3194 return tls;
3195}
0ad989f9
L
3196
3197/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3198static bfd_boolean
3199is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3200 Elf_Internal_Sym *sym)
3201{
a4d8e49b
L
3202 const struct elf_backend_data *bed;
3203
0ad989f9
L
3204 /* Local symbols do not count, but target specific ones might. */
3205 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3206 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3207 return FALSE;
3208
fcb93ecf 3209 bed = get_elf_backend_data (abfd);
0ad989f9 3210 /* Function symbols do not count. */
fcb93ecf 3211 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3212 return FALSE;
3213
3214 /* If the section is undefined, then so is the symbol. */
3215 if (sym->st_shndx == SHN_UNDEF)
3216 return FALSE;
3217
3218 /* If the symbol is defined in the common section, then
3219 it is a common definition and so does not count. */
a4d8e49b 3220 if (bed->common_definition (sym))
0ad989f9
L
3221 return FALSE;
3222
3223 /* If the symbol is in a target specific section then we
3224 must rely upon the backend to tell us what it is. */
3225 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3226 /* FIXME - this function is not coded yet:
3227
3228 return _bfd_is_global_symbol_definition (abfd, sym);
3229
3230 Instead for now assume that the definition is not global,
3231 Even if this is wrong, at least the linker will behave
3232 in the same way that it used to do. */
3233 return FALSE;
3234
3235 return TRUE;
3236}
3237
3238/* Search the symbol table of the archive element of the archive ABFD
3239 whose archive map contains a mention of SYMDEF, and determine if
3240 the symbol is defined in this element. */
3241static bfd_boolean
3242elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3243{
3244 Elf_Internal_Shdr * hdr;
ef53be89
AM
3245 size_t symcount;
3246 size_t extsymcount;
3247 size_t extsymoff;
0ad989f9
L
3248 Elf_Internal_Sym *isymbuf;
3249 Elf_Internal_Sym *isym;
3250 Elf_Internal_Sym *isymend;
3251 bfd_boolean result;
3252
3253 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3254 if (abfd == NULL)
3255 return FALSE;
3256
3257 if (! bfd_check_format (abfd, bfd_object))
3258 return FALSE;
3259
7dc3990e
L
3260 /* Select the appropriate symbol table. If we don't know if the
3261 object file is an IR object, give linker LTO plugin a chance to
3262 get the correct symbol table. */
3263 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3264#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3265 || (abfd->plugin_format == bfd_plugin_unknown
3266 && bfd_link_plugin_object_p (abfd))
3267#endif
3268 )
3269 {
3270 /* Use the IR symbol table if the object has been claimed by
3271 plugin. */
3272 abfd = abfd->plugin_dummy_bfd;
3273 hdr = &elf_tdata (abfd)->symtab_hdr;
3274 }
3275 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3276 hdr = &elf_tdata (abfd)->symtab_hdr;
3277 else
3278 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3279
3280 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3281
3282 /* The sh_info field of the symtab header tells us where the
3283 external symbols start. We don't care about the local symbols. */
3284 if (elf_bad_symtab (abfd))
3285 {
3286 extsymcount = symcount;
3287 extsymoff = 0;
3288 }
3289 else
3290 {
3291 extsymcount = symcount - hdr->sh_info;
3292 extsymoff = hdr->sh_info;
3293 }
3294
3295 if (extsymcount == 0)
3296 return FALSE;
3297
3298 /* Read in the symbol table. */
3299 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3300 NULL, NULL, NULL);
3301 if (isymbuf == NULL)
3302 return FALSE;
3303
3304 /* Scan the symbol table looking for SYMDEF. */
3305 result = FALSE;
3306 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3307 {
3308 const char *name;
3309
3310 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3311 isym->st_name);
3312 if (name == NULL)
3313 break;
3314
3315 if (strcmp (name, symdef->name) == 0)
3316 {
3317 result = is_global_data_symbol_definition (abfd, isym);
3318 break;
3319 }
3320 }
3321
3322 free (isymbuf);
3323
3324 return result;
3325}
3326\f
5a580b3a
AM
3327/* Add an entry to the .dynamic table. */
3328
3329bfd_boolean
3330_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3331 bfd_vma tag,
3332 bfd_vma val)
3333{
3334 struct elf_link_hash_table *hash_table;
3335 const struct elf_backend_data *bed;
3336 asection *s;
3337 bfd_size_type newsize;
3338 bfd_byte *newcontents;
3339 Elf_Internal_Dyn dyn;
3340
3341 hash_table = elf_hash_table (info);
3342 if (! is_elf_hash_table (hash_table))
3343 return FALSE;
3344
3345 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3346 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3347 BFD_ASSERT (s != NULL);
3348
eea6121a 3349 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3350 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3351 if (newcontents == NULL)
3352 return FALSE;
3353
3354 dyn.d_tag = tag;
3355 dyn.d_un.d_val = val;
eea6121a 3356 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3357
eea6121a 3358 s->size = newsize;
5a580b3a
AM
3359 s->contents = newcontents;
3360
3361 return TRUE;
3362}
3363
3364/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3365 otherwise just check whether one already exists. Returns -1 on error,
3366 1 if a DT_NEEDED tag already exists, and 0 on success. */
3367
4ad4eba5 3368static int
7e9f0867
AM
3369elf_add_dt_needed_tag (bfd *abfd,
3370 struct bfd_link_info *info,
4ad4eba5
AM
3371 const char *soname,
3372 bfd_boolean do_it)
5a580b3a
AM
3373{
3374 struct elf_link_hash_table *hash_table;
ef53be89 3375 size_t strindex;
5a580b3a 3376
7e9f0867
AM
3377 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3378 return -1;
3379
5a580b3a 3380 hash_table = elf_hash_table (info);
5a580b3a 3381 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3382 if (strindex == (size_t) -1)
5a580b3a
AM
3383 return -1;
3384
02be4619 3385 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3386 {
3387 asection *sdyn;
3388 const struct elf_backend_data *bed;
3389 bfd_byte *extdyn;
3390
3391 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3392 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3393 if (sdyn != NULL)
3394 for (extdyn = sdyn->contents;
3395 extdyn < sdyn->contents + sdyn->size;
3396 extdyn += bed->s->sizeof_dyn)
3397 {
3398 Elf_Internal_Dyn dyn;
5a580b3a 3399
7e9f0867
AM
3400 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3401 if (dyn.d_tag == DT_NEEDED
3402 && dyn.d_un.d_val == strindex)
3403 {
3404 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3405 return 1;
3406 }
3407 }
5a580b3a
AM
3408 }
3409
3410 if (do_it)
3411 {
7e9f0867
AM
3412 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3413 return -1;
3414
5a580b3a
AM
3415 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3416 return -1;
3417 }
3418 else
3419 /* We were just checking for existence of the tag. */
3420 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3421
3422 return 0;
3423}
3424
7b15fa7a
AM
3425/* Return true if SONAME is on the needed list between NEEDED and STOP
3426 (or the end of list if STOP is NULL), and needed by a library that
3427 will be loaded. */
3428
010e5ae2 3429static bfd_boolean
7b15fa7a
AM
3430on_needed_list (const char *soname,
3431 struct bfd_link_needed_list *needed,
3432 struct bfd_link_needed_list *stop)
010e5ae2 3433{
7b15fa7a
AM
3434 struct bfd_link_needed_list *look;
3435 for (look = needed; look != stop; look = look->next)
3436 if (strcmp (soname, look->name) == 0
3437 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3438 /* If needed by a library that itself is not directly
3439 needed, recursively check whether that library is
3440 indirectly needed. Since we add DT_NEEDED entries to
3441 the end of the list, library dependencies appear after
3442 the library. Therefore search prior to the current
3443 LOOK, preventing possible infinite recursion. */
3444 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3445 return TRUE;
3446
3447 return FALSE;
3448}
3449
14160578 3450/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3451static int
3452elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3453{
3454 const struct elf_link_hash_entry *h1;
3455 const struct elf_link_hash_entry *h2;
10b7e05b 3456 bfd_signed_vma vdiff;
5a580b3a
AM
3457
3458 h1 = *(const struct elf_link_hash_entry **) arg1;
3459 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3460 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3461 if (vdiff != 0)
3462 return vdiff > 0 ? 1 : -1;
3463 else
3464 {
d3435ae8 3465 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3466 if (sdiff != 0)
3467 return sdiff > 0 ? 1 : -1;
3468 }
14160578
AM
3469 vdiff = h1->size - h2->size;
3470 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3471}
4ad4eba5 3472
5a580b3a
AM
3473/* This function is used to adjust offsets into .dynstr for
3474 dynamic symbols. This is called via elf_link_hash_traverse. */
3475
3476static bfd_boolean
3477elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3478{
a50b1753 3479 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3480
5a580b3a
AM
3481 if (h->dynindx != -1)
3482 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3483 return TRUE;
3484}
3485
3486/* Assign string offsets in .dynstr, update all structures referencing
3487 them. */
3488
4ad4eba5
AM
3489static bfd_boolean
3490elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3491{
3492 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3493 struct elf_link_local_dynamic_entry *entry;
3494 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3495 bfd *dynobj = hash_table->dynobj;
3496 asection *sdyn;
3497 bfd_size_type size;
3498 const struct elf_backend_data *bed;
3499 bfd_byte *extdyn;
3500
3501 _bfd_elf_strtab_finalize (dynstr);
3502 size = _bfd_elf_strtab_size (dynstr);
3503
3504 bed = get_elf_backend_data (dynobj);
3d4d4302 3505 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3506 BFD_ASSERT (sdyn != NULL);
3507
3508 /* Update all .dynamic entries referencing .dynstr strings. */
3509 for (extdyn = sdyn->contents;
eea6121a 3510 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3511 extdyn += bed->s->sizeof_dyn)
3512 {
3513 Elf_Internal_Dyn dyn;
3514
3515 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3516 switch (dyn.d_tag)
3517 {
3518 case DT_STRSZ:
3519 dyn.d_un.d_val = size;
3520 break;
3521 case DT_NEEDED:
3522 case DT_SONAME:
3523 case DT_RPATH:
3524 case DT_RUNPATH:
3525 case DT_FILTER:
3526 case DT_AUXILIARY:
7ee314fa
AM
3527 case DT_AUDIT:
3528 case DT_DEPAUDIT:
5a580b3a
AM
3529 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3530 break;
3531 default:
3532 continue;
3533 }
3534 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3535 }
3536
3537 /* Now update local dynamic symbols. */
3538 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3539 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3540 entry->isym.st_name);
3541
3542 /* And the rest of dynamic symbols. */
3543 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3544
3545 /* Adjust version definitions. */
3546 if (elf_tdata (output_bfd)->cverdefs)
3547 {
3548 asection *s;
3549 bfd_byte *p;
ef53be89 3550 size_t i;
5a580b3a
AM
3551 Elf_Internal_Verdef def;
3552 Elf_Internal_Verdaux defaux;
3553
3d4d4302 3554 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3555 p = s->contents;
3556 do
3557 {
3558 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3559 &def);
3560 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3561 if (def.vd_aux != sizeof (Elf_External_Verdef))
3562 continue;
5a580b3a
AM
3563 for (i = 0; i < def.vd_cnt; ++i)
3564 {
3565 _bfd_elf_swap_verdaux_in (output_bfd,
3566 (Elf_External_Verdaux *) p, &defaux);
3567 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3568 defaux.vda_name);
3569 _bfd_elf_swap_verdaux_out (output_bfd,
3570 &defaux, (Elf_External_Verdaux *) p);
3571 p += sizeof (Elf_External_Verdaux);
3572 }
3573 }
3574 while (def.vd_next);
3575 }
3576
3577 /* Adjust version references. */
3578 if (elf_tdata (output_bfd)->verref)
3579 {
3580 asection *s;
3581 bfd_byte *p;
ef53be89 3582 size_t i;
5a580b3a
AM
3583 Elf_Internal_Verneed need;
3584 Elf_Internal_Vernaux needaux;
3585
3d4d4302 3586 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3587 p = s->contents;
3588 do
3589 {
3590 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3591 &need);
3592 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3593 _bfd_elf_swap_verneed_out (output_bfd, &need,
3594 (Elf_External_Verneed *) p);
3595 p += sizeof (Elf_External_Verneed);
3596 for (i = 0; i < need.vn_cnt; ++i)
3597 {
3598 _bfd_elf_swap_vernaux_in (output_bfd,
3599 (Elf_External_Vernaux *) p, &needaux);
3600 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3601 needaux.vna_name);
3602 _bfd_elf_swap_vernaux_out (output_bfd,
3603 &needaux,
3604 (Elf_External_Vernaux *) p);
3605 p += sizeof (Elf_External_Vernaux);
3606 }
3607 }
3608 while (need.vn_next);
3609 }
3610
3611 return TRUE;
3612}
3613\f
13285a1b
AM
3614/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3615 The default is to only match when the INPUT and OUTPUT are exactly
3616 the same target. */
3617
3618bfd_boolean
3619_bfd_elf_default_relocs_compatible (const bfd_target *input,
3620 const bfd_target *output)
3621{
3622 return input == output;
3623}
3624
3625/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3626 This version is used when different targets for the same architecture
3627 are virtually identical. */
3628
3629bfd_boolean
3630_bfd_elf_relocs_compatible (const bfd_target *input,
3631 const bfd_target *output)
3632{
3633 const struct elf_backend_data *obed, *ibed;
3634
3635 if (input == output)
3636 return TRUE;
3637
3638 ibed = xvec_get_elf_backend_data (input);
3639 obed = xvec_get_elf_backend_data (output);
3640
3641 if (ibed->arch != obed->arch)
3642 return FALSE;
3643
3644 /* If both backends are using this function, deem them compatible. */
3645 return ibed->relocs_compatible == obed->relocs_compatible;
3646}
3647
e5034e59
AM
3648/* Make a special call to the linker "notice" function to tell it that
3649 we are about to handle an as-needed lib, or have finished
1b786873 3650 processing the lib. */
e5034e59
AM
3651
3652bfd_boolean
3653_bfd_elf_notice_as_needed (bfd *ibfd,
3654 struct bfd_link_info *info,
3655 enum notice_asneeded_action act)
3656{
46135103 3657 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3658}
3659
d9689752
L
3660/* Check relocations an ELF object file. */
3661
3662bfd_boolean
3663_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3664{
3665 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3666 struct elf_link_hash_table *htab = elf_hash_table (info);
3667
3668 /* If this object is the same format as the output object, and it is
3669 not a shared library, then let the backend look through the
3670 relocs.
3671
3672 This is required to build global offset table entries and to
3673 arrange for dynamic relocs. It is not required for the
3674 particular common case of linking non PIC code, even when linking
3675 against shared libraries, but unfortunately there is no way of
3676 knowing whether an object file has been compiled PIC or not.
3677 Looking through the relocs is not particularly time consuming.
3678 The problem is that we must either (1) keep the relocs in memory,
3679 which causes the linker to require additional runtime memory or
3680 (2) read the relocs twice from the input file, which wastes time.
3681 This would be a good case for using mmap.
3682
3683 I have no idea how to handle linking PIC code into a file of a
3684 different format. It probably can't be done. */
3685 if ((abfd->flags & DYNAMIC) == 0
3686 && is_elf_hash_table (htab)
3687 && bed->check_relocs != NULL
3688 && elf_object_id (abfd) == elf_hash_table_id (htab)
3689 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3690 {
3691 asection *o;
3692
3693 for (o = abfd->sections; o != NULL; o = o->next)
3694 {
3695 Elf_Internal_Rela *internal_relocs;
3696 bfd_boolean ok;
3697
5ce03cea 3698 /* Don't check relocations in excluded sections. */
d9689752 3699 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3700 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3701 || o->reloc_count == 0
3702 || ((info->strip == strip_all || info->strip == strip_debugger)
3703 && (o->flags & SEC_DEBUGGING) != 0)
3704 || bfd_is_abs_section (o->output_section))
3705 continue;
3706
3707 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3708 info->keep_memory);
3709 if (internal_relocs == NULL)
3710 return FALSE;
3711
3712 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3713
3714 if (elf_section_data (o)->relocs != internal_relocs)
3715 free (internal_relocs);
3716
3717 if (! ok)
3718 return FALSE;
3719 }
3720 }
3721
3722 return TRUE;
3723}
3724
4ad4eba5
AM
3725/* Add symbols from an ELF object file to the linker hash table. */
3726
3727static bfd_boolean
3728elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3729{
a0c402a5 3730 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3731 Elf_Internal_Shdr *hdr;
ef53be89
AM
3732 size_t symcount;
3733 size_t extsymcount;
3734 size_t extsymoff;
4ad4eba5
AM
3735 struct elf_link_hash_entry **sym_hash;
3736 bfd_boolean dynamic;
3737 Elf_External_Versym *extversym = NULL;
3738 Elf_External_Versym *ever;
3739 struct elf_link_hash_entry *weaks;
3740 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3741 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3742 Elf_Internal_Sym *isymbuf = NULL;
3743 Elf_Internal_Sym *isym;
3744 Elf_Internal_Sym *isymend;
3745 const struct elf_backend_data *bed;
3746 bfd_boolean add_needed;
66eb6687 3747 struct elf_link_hash_table *htab;
4ad4eba5 3748 bfd_size_type amt;
66eb6687 3749 void *alloc_mark = NULL;
4f87808c
AM
3750 struct bfd_hash_entry **old_table = NULL;
3751 unsigned int old_size = 0;
3752 unsigned int old_count = 0;
66eb6687 3753 void *old_tab = NULL;
66eb6687
AM
3754 void *old_ent;
3755 struct bfd_link_hash_entry *old_undefs = NULL;
3756 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3757 void *old_strtab = NULL;
66eb6687 3758 size_t tabsize = 0;
db6a5d5f 3759 asection *s;
29a9f53e 3760 bfd_boolean just_syms;
4ad4eba5 3761
66eb6687 3762 htab = elf_hash_table (info);
4ad4eba5 3763 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3764
3765 if ((abfd->flags & DYNAMIC) == 0)
3766 dynamic = FALSE;
3767 else
3768 {
3769 dynamic = TRUE;
3770
3771 /* You can't use -r against a dynamic object. Also, there's no
3772 hope of using a dynamic object which does not exactly match
3773 the format of the output file. */
0e1862bb 3774 if (bfd_link_relocatable (info)
66eb6687 3775 || !is_elf_hash_table (htab)
f13a99db 3776 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3777 {
0e1862bb 3778 if (bfd_link_relocatable (info))
9a0789ec
NC
3779 bfd_set_error (bfd_error_invalid_operation);
3780 else
3781 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3782 goto error_return;
3783 }
3784 }
3785
a0c402a5
L
3786 ehdr = elf_elfheader (abfd);
3787 if (info->warn_alternate_em
3788 && bed->elf_machine_code != ehdr->e_machine
3789 && ((bed->elf_machine_alt1 != 0
3790 && ehdr->e_machine == bed->elf_machine_alt1)
3791 || (bed->elf_machine_alt2 != 0
3792 && ehdr->e_machine == bed->elf_machine_alt2)))
3793 info->callbacks->einfo
695344c0 3794 /* xgettext:c-format */
a0c402a5
L
3795 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3796 ehdr->e_machine, abfd, bed->elf_machine_code);
3797
4ad4eba5
AM
3798 /* As a GNU extension, any input sections which are named
3799 .gnu.warning.SYMBOL are treated as warning symbols for the given
3800 symbol. This differs from .gnu.warning sections, which generate
3801 warnings when they are included in an output file. */
dd98f8d2 3802 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3803 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3804 {
db6a5d5f 3805 const char *name;
4ad4eba5 3806
db6a5d5f
AM
3807 name = bfd_get_section_name (abfd, s);
3808 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3809 {
db6a5d5f
AM
3810 char *msg;
3811 bfd_size_type sz;
3812
3813 name += sizeof ".gnu.warning." - 1;
3814
3815 /* If this is a shared object, then look up the symbol
3816 in the hash table. If it is there, and it is already
3817 been defined, then we will not be using the entry
3818 from this shared object, so we don't need to warn.
3819 FIXME: If we see the definition in a regular object
3820 later on, we will warn, but we shouldn't. The only
3821 fix is to keep track of what warnings we are supposed
3822 to emit, and then handle them all at the end of the
3823 link. */
3824 if (dynamic)
4ad4eba5 3825 {
db6a5d5f
AM
3826 struct elf_link_hash_entry *h;
3827
3828 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3829
3830 /* FIXME: What about bfd_link_hash_common? */
3831 if (h != NULL
3832 && (h->root.type == bfd_link_hash_defined
3833 || h->root.type == bfd_link_hash_defweak))
3834 continue;
3835 }
4ad4eba5 3836
db6a5d5f
AM
3837 sz = s->size;
3838 msg = (char *) bfd_alloc (abfd, sz + 1);
3839 if (msg == NULL)
3840 goto error_return;
4ad4eba5 3841
db6a5d5f
AM
3842 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3843 goto error_return;
4ad4eba5 3844
db6a5d5f 3845 msg[sz] = '\0';
4ad4eba5 3846
db6a5d5f
AM
3847 if (! (_bfd_generic_link_add_one_symbol
3848 (info, abfd, name, BSF_WARNING, s, 0, msg,
3849 FALSE, bed->collect, NULL)))
3850 goto error_return;
4ad4eba5 3851
0e1862bb 3852 if (bfd_link_executable (info))
db6a5d5f
AM
3853 {
3854 /* Clobber the section size so that the warning does
3855 not get copied into the output file. */
3856 s->size = 0;
11d2f718 3857
db6a5d5f
AM
3858 /* Also set SEC_EXCLUDE, so that symbols defined in
3859 the warning section don't get copied to the output. */
3860 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3861 }
3862 }
3863 }
3864
29a9f53e
L
3865 just_syms = ((s = abfd->sections) != NULL
3866 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3867
4ad4eba5
AM
3868 add_needed = TRUE;
3869 if (! dynamic)
3870 {
3871 /* If we are creating a shared library, create all the dynamic
3872 sections immediately. We need to attach them to something,
3873 so we attach them to this BFD, provided it is the right
bf89386a
L
3874 format and is not from ld --just-symbols. Always create the
3875 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3876 are no input BFD's of the same format as the output, we can't
3877 make a shared library. */
3878 if (!just_syms
bf89386a 3879 && (bfd_link_pic (info)
9c1d7a08 3880 || (!bfd_link_relocatable (info)
3c5fce9b 3881 && info->nointerp
9c1d7a08 3882 && (info->export_dynamic || info->dynamic)))
66eb6687 3883 && is_elf_hash_table (htab)
f13a99db 3884 && info->output_bfd->xvec == abfd->xvec
66eb6687 3885 && !htab->dynamic_sections_created)
4ad4eba5
AM
3886 {
3887 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3888 goto error_return;
3889 }
3890 }
66eb6687 3891 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3892 goto error_return;
3893 else
3894 {
4ad4eba5 3895 const char *soname = NULL;
7ee314fa 3896 char *audit = NULL;
4ad4eba5 3897 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3898 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3899 int ret;
3900
3901 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3902 ld shouldn't allow it. */
29a9f53e 3903 if (just_syms)
92fd189d 3904 abort ();
4ad4eba5
AM
3905
3906 /* If this dynamic lib was specified on the command line with
3907 --as-needed in effect, then we don't want to add a DT_NEEDED
3908 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3909 in by another lib's DT_NEEDED. When --no-add-needed is used
3910 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3911 any dynamic library in DT_NEEDED tags in the dynamic lib at
3912 all. */
3913 add_needed = (elf_dyn_lib_class (abfd)
3914 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3915 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3916
3917 s = bfd_get_section_by_name (abfd, ".dynamic");
3918 if (s != NULL)
3919 {
3920 bfd_byte *dynbuf;
3921 bfd_byte *extdyn;
cb33740c 3922 unsigned int elfsec;
4ad4eba5
AM
3923 unsigned long shlink;
3924
eea6121a 3925 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3926 {
3927error_free_dyn:
3928 free (dynbuf);
3929 goto error_return;
3930 }
4ad4eba5
AM
3931
3932 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3933 if (elfsec == SHN_BAD)
4ad4eba5
AM
3934 goto error_free_dyn;
3935 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3936
3937 for (extdyn = dynbuf;
eea6121a 3938 extdyn < dynbuf + s->size;
4ad4eba5
AM
3939 extdyn += bed->s->sizeof_dyn)
3940 {
3941 Elf_Internal_Dyn dyn;
3942
3943 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3944 if (dyn.d_tag == DT_SONAME)
3945 {
3946 unsigned int tagv = dyn.d_un.d_val;
3947 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3948 if (soname == NULL)
3949 goto error_free_dyn;
3950 }
3951 if (dyn.d_tag == DT_NEEDED)
3952 {
3953 struct bfd_link_needed_list *n, **pn;
3954 char *fnm, *anm;
3955 unsigned int tagv = dyn.d_un.d_val;
3956
3957 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3958 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3959 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3960 if (n == NULL || fnm == NULL)
3961 goto error_free_dyn;
3962 amt = strlen (fnm) + 1;
a50b1753 3963 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3964 if (anm == NULL)
3965 goto error_free_dyn;
3966 memcpy (anm, fnm, amt);
3967 n->name = anm;
3968 n->by = abfd;
3969 n->next = NULL;
66eb6687 3970 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3971 ;
3972 *pn = n;
3973 }
3974 if (dyn.d_tag == DT_RUNPATH)
3975 {
3976 struct bfd_link_needed_list *n, **pn;
3977 char *fnm, *anm;
3978 unsigned int tagv = dyn.d_un.d_val;
3979
3980 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3981 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3982 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3983 if (n == NULL || fnm == NULL)
3984 goto error_free_dyn;
3985 amt = strlen (fnm) + 1;
a50b1753 3986 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3987 if (anm == NULL)
3988 goto error_free_dyn;
3989 memcpy (anm, fnm, amt);
3990 n->name = anm;
3991 n->by = abfd;
3992 n->next = NULL;
3993 for (pn = & runpath;
3994 *pn != NULL;
3995 pn = &(*pn)->next)
3996 ;
3997 *pn = n;
3998 }
3999 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4000 if (!runpath && dyn.d_tag == DT_RPATH)
4001 {
4002 struct bfd_link_needed_list *n, **pn;
4003 char *fnm, *anm;
4004 unsigned int tagv = dyn.d_un.d_val;
4005
4006 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4007 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4008 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4009 if (n == NULL || fnm == NULL)
4010 goto error_free_dyn;
4011 amt = strlen (fnm) + 1;
a50b1753 4012 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4013 if (anm == NULL)
f8703194 4014 goto error_free_dyn;
4ad4eba5
AM
4015 memcpy (anm, fnm, amt);
4016 n->name = anm;
4017 n->by = abfd;
4018 n->next = NULL;
4019 for (pn = & rpath;
4020 *pn != NULL;
4021 pn = &(*pn)->next)
4022 ;
4023 *pn = n;
4024 }
7ee314fa
AM
4025 if (dyn.d_tag == DT_AUDIT)
4026 {
4027 unsigned int tagv = dyn.d_un.d_val;
4028 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4029 }
4ad4eba5
AM
4030 }
4031
4032 free (dynbuf);
4033 }
4034
4035 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4036 frees all more recently bfd_alloc'd blocks as well. */
4037 if (runpath)
4038 rpath = runpath;
4039
4040 if (rpath)
4041 {
4042 struct bfd_link_needed_list **pn;
66eb6687 4043 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4044 ;
4045 *pn = rpath;
4046 }
4047
9acc85a6
AM
4048 /* If we have a PT_GNU_RELRO program header, mark as read-only
4049 all sections contained fully therein. This makes relro
4050 shared library sections appear as they will at run-time. */
4051 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4052 while (--phdr >= elf_tdata (abfd)->phdr)
4053 if (phdr->p_type == PT_GNU_RELRO)
4054 {
4055 for (s = abfd->sections; s != NULL; s = s->next)
4056 if ((s->flags & SEC_ALLOC) != 0
4057 && s->vma >= phdr->p_vaddr
4058 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4059 s->flags |= SEC_READONLY;
4060 break;
4061 }
4062
4ad4eba5
AM
4063 /* We do not want to include any of the sections in a dynamic
4064 object in the output file. We hack by simply clobbering the
4065 list of sections in the BFD. This could be handled more
4066 cleanly by, say, a new section flag; the existing
4067 SEC_NEVER_LOAD flag is not the one we want, because that one
4068 still implies that the section takes up space in the output
4069 file. */
4070 bfd_section_list_clear (abfd);
4071
4ad4eba5
AM
4072 /* Find the name to use in a DT_NEEDED entry that refers to this
4073 object. If the object has a DT_SONAME entry, we use it.
4074 Otherwise, if the generic linker stuck something in
4075 elf_dt_name, we use that. Otherwise, we just use the file
4076 name. */
4077 if (soname == NULL || *soname == '\0')
4078 {
4079 soname = elf_dt_name (abfd);
4080 if (soname == NULL || *soname == '\0')
4081 soname = bfd_get_filename (abfd);
4082 }
4083
4084 /* Save the SONAME because sometimes the linker emulation code
4085 will need to know it. */
4086 elf_dt_name (abfd) = soname;
4087
7e9f0867 4088 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4089 if (ret < 0)
4090 goto error_return;
4091
4092 /* If we have already included this dynamic object in the
4093 link, just ignore it. There is no reason to include a
4094 particular dynamic object more than once. */
4095 if (ret > 0)
4096 return TRUE;
7ee314fa
AM
4097
4098 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4099 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4100 }
4101
4102 /* If this is a dynamic object, we always link against the .dynsym
4103 symbol table, not the .symtab symbol table. The dynamic linker
4104 will only see the .dynsym symbol table, so there is no reason to
4105 look at .symtab for a dynamic object. */
4106
4107 if (! dynamic || elf_dynsymtab (abfd) == 0)
4108 hdr = &elf_tdata (abfd)->symtab_hdr;
4109 else
4110 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4111
4112 symcount = hdr->sh_size / bed->s->sizeof_sym;
4113
4114 /* The sh_info field of the symtab header tells us where the
4115 external symbols start. We don't care about the local symbols at
4116 this point. */
4117 if (elf_bad_symtab (abfd))
4118 {
4119 extsymcount = symcount;
4120 extsymoff = 0;
4121 }
4122 else
4123 {
4124 extsymcount = symcount - hdr->sh_info;
4125 extsymoff = hdr->sh_info;
4126 }
4127
f45794cb 4128 sym_hash = elf_sym_hashes (abfd);
012b2306 4129 if (extsymcount != 0)
4ad4eba5
AM
4130 {
4131 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4132 NULL, NULL, NULL);
4133 if (isymbuf == NULL)
4134 goto error_return;
4135
4ad4eba5 4136 if (sym_hash == NULL)
012b2306
AM
4137 {
4138 /* We store a pointer to the hash table entry for each
4139 external symbol. */
ef53be89
AM
4140 amt = extsymcount;
4141 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4142 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4143 if (sym_hash == NULL)
4144 goto error_free_sym;
4145 elf_sym_hashes (abfd) = sym_hash;
4146 }
4ad4eba5
AM
4147 }
4148
4149 if (dynamic)
4150 {
4151 /* Read in any version definitions. */
fc0e6df6
PB
4152 if (!_bfd_elf_slurp_version_tables (abfd,
4153 info->default_imported_symver))
4ad4eba5
AM
4154 goto error_free_sym;
4155
4156 /* Read in the symbol versions, but don't bother to convert them
4157 to internal format. */
4158 if (elf_dynversym (abfd) != 0)
4159 {
4160 Elf_Internal_Shdr *versymhdr;
4161
4162 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4163 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4164 if (extversym == NULL)
4165 goto error_free_sym;
4166 amt = versymhdr->sh_size;
4167 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4168 || bfd_bread (extversym, amt, abfd) != amt)
4169 goto error_free_vers;
4170 }
4171 }
4172
66eb6687
AM
4173 /* If we are loading an as-needed shared lib, save the symbol table
4174 state before we start adding symbols. If the lib turns out
4175 to be unneeded, restore the state. */
4176 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4177 {
4178 unsigned int i;
4179 size_t entsize;
4180
4181 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4182 {
4183 struct bfd_hash_entry *p;
2de92251 4184 struct elf_link_hash_entry *h;
66eb6687
AM
4185
4186 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4187 {
4188 h = (struct elf_link_hash_entry *) p;
4189 entsize += htab->root.table.entsize;
4190 if (h->root.type == bfd_link_hash_warning)
4191 entsize += htab->root.table.entsize;
4192 }
66eb6687
AM
4193 }
4194
4195 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4196 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4197 if (old_tab == NULL)
4198 goto error_free_vers;
4199
4200 /* Remember the current objalloc pointer, so that all mem for
4201 symbols added can later be reclaimed. */
4202 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4203 if (alloc_mark == NULL)
4204 goto error_free_vers;
4205
5061a885
AM
4206 /* Make a special call to the linker "notice" function to
4207 tell it that we are about to handle an as-needed lib. */
e5034e59 4208 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4209 goto error_free_vers;
5061a885 4210
f45794cb
AM
4211 /* Clone the symbol table. Remember some pointers into the
4212 symbol table, and dynamic symbol count. */
4213 old_ent = (char *) old_tab + tabsize;
66eb6687 4214 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4215 old_undefs = htab->root.undefs;
4216 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4217 old_table = htab->root.table.table;
4218 old_size = htab->root.table.size;
4219 old_count = htab->root.table.count;
5b677558
AM
4220 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4221 if (old_strtab == NULL)
4222 goto error_free_vers;
66eb6687
AM
4223
4224 for (i = 0; i < htab->root.table.size; i++)
4225 {
4226 struct bfd_hash_entry *p;
2de92251 4227 struct elf_link_hash_entry *h;
66eb6687
AM
4228
4229 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4230 {
4231 memcpy (old_ent, p, htab->root.table.entsize);
4232 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4233 h = (struct elf_link_hash_entry *) p;
4234 if (h->root.type == bfd_link_hash_warning)
4235 {
4236 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4237 old_ent = (char *) old_ent + htab->root.table.entsize;
4238 }
66eb6687
AM
4239 }
4240 }
4241 }
4ad4eba5 4242
66eb6687 4243 weaks = NULL;
4ad4eba5
AM
4244 ever = extversym != NULL ? extversym + extsymoff : NULL;
4245 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4246 isym < isymend;
4247 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4248 {
4249 int bind;
4250 bfd_vma value;
af44c138 4251 asection *sec, *new_sec;
4ad4eba5
AM
4252 flagword flags;
4253 const char *name;
4254 struct elf_link_hash_entry *h;
90c984fc 4255 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4256 bfd_boolean definition;
4257 bfd_boolean size_change_ok;
4258 bfd_boolean type_change_ok;
4259 bfd_boolean new_weakdef;
37a9e49a
L
4260 bfd_boolean new_weak;
4261 bfd_boolean old_weak;
4ad4eba5 4262 bfd_boolean override;
a4d8e49b 4263 bfd_boolean common;
97196564 4264 bfd_boolean discarded;
4ad4eba5
AM
4265 unsigned int old_alignment;
4266 bfd *old_bfd;
6e33951e 4267 bfd_boolean matched;
4ad4eba5
AM
4268
4269 override = FALSE;
4270
4271 flags = BSF_NO_FLAGS;
4272 sec = NULL;
4273 value = isym->st_value;
a4d8e49b 4274 common = bed->common_definition (isym);
2980ccad
L
4275 if (common && info->inhibit_common_definition)
4276 {
4277 /* Treat common symbol as undefined for --no-define-common. */
4278 isym->st_shndx = SHN_UNDEF;
4279 common = FALSE;
4280 }
97196564 4281 discarded = FALSE;
4ad4eba5
AM
4282
4283 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4284 switch (bind)
4ad4eba5 4285 {
3e7a7d11 4286 case STB_LOCAL:
4ad4eba5
AM
4287 /* This should be impossible, since ELF requires that all
4288 global symbols follow all local symbols, and that sh_info
4289 point to the first global symbol. Unfortunately, Irix 5
4290 screws this up. */
4291 continue;
3e7a7d11
NC
4292
4293 case STB_GLOBAL:
a4d8e49b 4294 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4295 flags = BSF_GLOBAL;
3e7a7d11
NC
4296 break;
4297
4298 case STB_WEAK:
4299 flags = BSF_WEAK;
4300 break;
4301
4302 case STB_GNU_UNIQUE:
4303 flags = BSF_GNU_UNIQUE;
4304 break;
4305
4306 default:
4ad4eba5 4307 /* Leave it up to the processor backend. */
3e7a7d11 4308 break;
4ad4eba5
AM
4309 }
4310
4311 if (isym->st_shndx == SHN_UNDEF)
4312 sec = bfd_und_section_ptr;
cb33740c
AM
4313 else if (isym->st_shndx == SHN_ABS)
4314 sec = bfd_abs_section_ptr;
4315 else if (isym->st_shndx == SHN_COMMON)
4316 {
4317 sec = bfd_com_section_ptr;
4318 /* What ELF calls the size we call the value. What ELF
4319 calls the value we call the alignment. */
4320 value = isym->st_size;
4321 }
4322 else
4ad4eba5
AM
4323 {
4324 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4325 if (sec == NULL)
4326 sec = bfd_abs_section_ptr;
dbaa2011 4327 else if (discarded_section (sec))
529fcb95 4328 {
e5d08002
L
4329 /* Symbols from discarded section are undefined. We keep
4330 its visibility. */
529fcb95 4331 sec = bfd_und_section_ptr;
97196564 4332 discarded = TRUE;
529fcb95
PB
4333 isym->st_shndx = SHN_UNDEF;
4334 }
4ad4eba5
AM
4335 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4336 value -= sec->vma;
4337 }
4ad4eba5
AM
4338
4339 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4340 isym->st_name);
4341 if (name == NULL)
4342 goto error_free_vers;
4343
4344 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4345 && (abfd->flags & BFD_PLUGIN) != 0)
4346 {
4347 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4348
4349 if (xc == NULL)
4350 {
4351 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4352 | SEC_EXCLUDE);
4353 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4354 if (xc == NULL)
4355 goto error_free_vers;
4356 }
4357 sec = xc;
4358 }
4359 else if (isym->st_shndx == SHN_COMMON
4360 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4361 && !bfd_link_relocatable (info))
4ad4eba5
AM
4362 {
4363 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4364
4365 if (tcomm == NULL)
4366 {
02d00247
AM
4367 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4368 | SEC_LINKER_CREATED);
4369 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4370 if (tcomm == NULL)
4ad4eba5
AM
4371 goto error_free_vers;
4372 }
4373 sec = tcomm;
4374 }
66eb6687 4375 else if (bed->elf_add_symbol_hook)
4ad4eba5 4376 {
66eb6687
AM
4377 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4378 &sec, &value))
4ad4eba5
AM
4379 goto error_free_vers;
4380
4381 /* The hook function sets the name to NULL if this symbol
4382 should be skipped for some reason. */
4383 if (name == NULL)
4384 continue;
4385 }
4386
4387 /* Sanity check that all possibilities were handled. */
4388 if (sec == NULL)
4389 {
4390 bfd_set_error (bfd_error_bad_value);
4391 goto error_free_vers;
4392 }
4393
191c0c42
AM
4394 /* Silently discard TLS symbols from --just-syms. There's
4395 no way to combine a static TLS block with a new TLS block
4396 for this executable. */
4397 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4398 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4399 continue;
4400
4ad4eba5
AM
4401 if (bfd_is_und_section (sec)
4402 || bfd_is_com_section (sec))
4403 definition = FALSE;
4404 else
4405 definition = TRUE;
4406
4407 size_change_ok = FALSE;
66eb6687 4408 type_change_ok = bed->type_change_ok;
37a9e49a 4409 old_weak = FALSE;
6e33951e 4410 matched = FALSE;
4ad4eba5
AM
4411 old_alignment = 0;
4412 old_bfd = NULL;
af44c138 4413 new_sec = sec;
4ad4eba5 4414
66eb6687 4415 if (is_elf_hash_table (htab))
4ad4eba5
AM
4416 {
4417 Elf_Internal_Versym iver;
4418 unsigned int vernum = 0;
4419 bfd_boolean skip;
4420
fc0e6df6 4421 if (ever == NULL)
4ad4eba5 4422 {
fc0e6df6
PB
4423 if (info->default_imported_symver)
4424 /* Use the default symbol version created earlier. */
4425 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4426 else
4427 iver.vs_vers = 0;
4428 }
4429 else
4430 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4431
4432 vernum = iver.vs_vers & VERSYM_VERSION;
4433
4434 /* If this is a hidden symbol, or if it is not version
4435 1, we append the version name to the symbol name.
cc86ff91
EB
4436 However, we do not modify a non-hidden absolute symbol
4437 if it is not a function, because it might be the version
4438 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4439 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4440 || (vernum > 1
4441 && (!bfd_is_abs_section (sec)
4442 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4443 {
4444 const char *verstr;
4445 size_t namelen, verlen, newlen;
4446 char *newname, *p;
4447
4448 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4449 {
fc0e6df6
PB
4450 if (vernum > elf_tdata (abfd)->cverdefs)
4451 verstr = NULL;
4452 else if (vernum > 1)
4453 verstr =
4454 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4455 else
4456 verstr = "";
4ad4eba5 4457
fc0e6df6 4458 if (verstr == NULL)
4ad4eba5 4459 {
4eca0228 4460 _bfd_error_handler
695344c0 4461 /* xgettext:c-format */
fc0e6df6
PB
4462 (_("%B: %s: invalid version %u (max %d)"),
4463 abfd, name, vernum,
4464 elf_tdata (abfd)->cverdefs);
4465 bfd_set_error (bfd_error_bad_value);
4466 goto error_free_vers;
4ad4eba5 4467 }
fc0e6df6
PB
4468 }
4469 else
4470 {
4471 /* We cannot simply test for the number of
4472 entries in the VERNEED section since the
4473 numbers for the needed versions do not start
4474 at 0. */
4475 Elf_Internal_Verneed *t;
4476
4477 verstr = NULL;
4478 for (t = elf_tdata (abfd)->verref;
4479 t != NULL;
4480 t = t->vn_nextref)
4ad4eba5 4481 {
fc0e6df6 4482 Elf_Internal_Vernaux *a;
4ad4eba5 4483
fc0e6df6
PB
4484 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4485 {
4486 if (a->vna_other == vernum)
4ad4eba5 4487 {
fc0e6df6
PB
4488 verstr = a->vna_nodename;
4489 break;
4ad4eba5 4490 }
4ad4eba5 4491 }
fc0e6df6
PB
4492 if (a != NULL)
4493 break;
4494 }
4495 if (verstr == NULL)
4496 {
4eca0228 4497 _bfd_error_handler
695344c0 4498 /* xgettext:c-format */
fc0e6df6
PB
4499 (_("%B: %s: invalid needed version %d"),
4500 abfd, name, vernum);
4501 bfd_set_error (bfd_error_bad_value);
4502 goto error_free_vers;
4ad4eba5 4503 }
4ad4eba5 4504 }
fc0e6df6
PB
4505
4506 namelen = strlen (name);
4507 verlen = strlen (verstr);
4508 newlen = namelen + verlen + 2;
4509 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4510 && isym->st_shndx != SHN_UNDEF)
4511 ++newlen;
4512
a50b1753 4513 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4514 if (newname == NULL)
4515 goto error_free_vers;
4516 memcpy (newname, name, namelen);
4517 p = newname + namelen;
4518 *p++ = ELF_VER_CHR;
4519 /* If this is a defined non-hidden version symbol,
4520 we add another @ to the name. This indicates the
4521 default version of the symbol. */
4522 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4523 && isym->st_shndx != SHN_UNDEF)
4524 *p++ = ELF_VER_CHR;
4525 memcpy (p, verstr, verlen + 1);
4526
4527 name = newname;
4ad4eba5
AM
4528 }
4529
cd3416da
AM
4530 /* If this symbol has default visibility and the user has
4531 requested we not re-export it, then mark it as hidden. */
a0d49154 4532 if (!bfd_is_und_section (sec)
cd3416da 4533 && !dynamic
ce875075 4534 && abfd->no_export
cd3416da
AM
4535 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4536 isym->st_other = (STV_HIDDEN
4537 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4538
4f3fedcf
AM
4539 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4540 sym_hash, &old_bfd, &old_weak,
4541 &old_alignment, &skip, &override,
6e33951e
L
4542 &type_change_ok, &size_change_ok,
4543 &matched))
4ad4eba5
AM
4544 goto error_free_vers;
4545
4546 if (skip)
4547 continue;
4548
6e33951e
L
4549 /* Override a definition only if the new symbol matches the
4550 existing one. */
4551 if (override && matched)
4ad4eba5
AM
4552 definition = FALSE;
4553
4554 h = *sym_hash;
4555 while (h->root.type == bfd_link_hash_indirect
4556 || h->root.type == bfd_link_hash_warning)
4557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4558
4ad4eba5 4559 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4560 && vernum > 1
4561 && definition)
4562 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4563 }
4564
4565 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4566 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4567 (struct bfd_link_hash_entry **) sym_hash)))
4568 goto error_free_vers;
4569
a43942db
MR
4570 if ((flags & BSF_GNU_UNIQUE)
4571 && (abfd->flags & DYNAMIC) == 0
4572 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4573 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4574
4ad4eba5 4575 h = *sym_hash;
90c984fc
L
4576 /* We need to make sure that indirect symbol dynamic flags are
4577 updated. */
4578 hi = h;
4ad4eba5
AM
4579 while (h->root.type == bfd_link_hash_indirect
4580 || h->root.type == bfd_link_hash_warning)
4581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4582
97196564
L
4583 /* Setting the index to -3 tells elf_link_output_extsym that
4584 this symbol is defined in a discarded section. */
4585 if (discarded)
4586 h->indx = -3;
4587
4ad4eba5
AM
4588 *sym_hash = h;
4589
37a9e49a 4590 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4591 new_weakdef = FALSE;
4592 if (dynamic
4593 && definition
37a9e49a 4594 && new_weak
fcb93ecf 4595 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4596 && is_elf_hash_table (htab)
f6e332e6 4597 && h->u.weakdef == NULL)
4ad4eba5
AM
4598 {
4599 /* Keep a list of all weak defined non function symbols from
4600 a dynamic object, using the weakdef field. Later in this
4601 function we will set the weakdef field to the correct
4602 value. We only put non-function symbols from dynamic
4603 objects on this list, because that happens to be the only
4604 time we need to know the normal symbol corresponding to a
4605 weak symbol, and the information is time consuming to
4606 figure out. If the weakdef field is not already NULL,
4607 then this symbol was already defined by some previous
4608 dynamic object, and we will be using that previous
4609 definition anyhow. */
4610
f6e332e6 4611 h->u.weakdef = weaks;
4ad4eba5
AM
4612 weaks = h;
4613 new_weakdef = TRUE;
4614 }
4615
4616 /* Set the alignment of a common symbol. */
a4d8e49b 4617 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4618 && h->root.type == bfd_link_hash_common)
4619 {
4620 unsigned int align;
4621
a4d8e49b 4622 if (common)
af44c138
L
4623 align = bfd_log2 (isym->st_value);
4624 else
4625 {
4626 /* The new symbol is a common symbol in a shared object.
4627 We need to get the alignment from the section. */
4628 align = new_sec->alignment_power;
4629 }
595213d4 4630 if (align > old_alignment)
4ad4eba5
AM
4631 h->root.u.c.p->alignment_power = align;
4632 else
4633 h->root.u.c.p->alignment_power = old_alignment;
4634 }
4635
66eb6687 4636 if (is_elf_hash_table (htab))
4ad4eba5 4637 {
4f3fedcf
AM
4638 /* Set a flag in the hash table entry indicating the type of
4639 reference or definition we just found. A dynamic symbol
4640 is one which is referenced or defined by both a regular
4641 object and a shared object. */
4642 bfd_boolean dynsym = FALSE;
4643
4644 /* Plugin symbols aren't normal. Don't set def_regular or
4645 ref_regular for them, or make them dynamic. */
4646 if ((abfd->flags & BFD_PLUGIN) != 0)
4647 ;
4648 else if (! dynamic)
4649 {
4650 if (! definition)
4651 {
4652 h->ref_regular = 1;
4653 if (bind != STB_WEAK)
4654 h->ref_regular_nonweak = 1;
4655 }
4656 else
4657 {
4658 h->def_regular = 1;
4659 if (h->def_dynamic)
4660 {
4661 h->def_dynamic = 0;
4662 h->ref_dynamic = 1;
4663 }
4664 }
4665
4666 /* If the indirect symbol has been forced local, don't
4667 make the real symbol dynamic. */
4668 if ((h == hi || !hi->forced_local)
0e1862bb 4669 && (bfd_link_dll (info)
4f3fedcf
AM
4670 || h->def_dynamic
4671 || h->ref_dynamic))
4672 dynsym = TRUE;
4673 }
4674 else
4675 {
4676 if (! definition)
4677 {
4678 h->ref_dynamic = 1;
4679 hi->ref_dynamic = 1;
4680 }
4681 else
4682 {
4683 h->def_dynamic = 1;
4684 hi->def_dynamic = 1;
4685 }
4686
4687 /* If the indirect symbol has been forced local, don't
4688 make the real symbol dynamic. */
4689 if ((h == hi || !hi->forced_local)
4690 && (h->def_regular
4691 || h->ref_regular
4692 || (h->u.weakdef != NULL
4693 && ! new_weakdef
4694 && h->u.weakdef->dynindx != -1)))
4695 dynsym = TRUE;
4696 }
4697
4698 /* Check to see if we need to add an indirect symbol for
4699 the default name. */
4700 if (definition
4701 || (!override && h->root.type == bfd_link_hash_common))
4702 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4703 sec, value, &old_bfd, &dynsym))
4704 goto error_free_vers;
4ad4eba5
AM
4705
4706 /* Check the alignment when a common symbol is involved. This
4707 can change when a common symbol is overridden by a normal
4708 definition or a common symbol is ignored due to the old
4709 normal definition. We need to make sure the maximum
4710 alignment is maintained. */
a4d8e49b 4711 if ((old_alignment || common)
4ad4eba5
AM
4712 && h->root.type != bfd_link_hash_common)
4713 {
4714 unsigned int common_align;
4715 unsigned int normal_align;
4716 unsigned int symbol_align;
4717 bfd *normal_bfd;
4718 bfd *common_bfd;
4719
3a81e825
AM
4720 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4721 || h->root.type == bfd_link_hash_defweak);
4722
4ad4eba5
AM
4723 symbol_align = ffs (h->root.u.def.value) - 1;
4724 if (h->root.u.def.section->owner != NULL
0616a280
AM
4725 && (h->root.u.def.section->owner->flags
4726 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4727 {
4728 normal_align = h->root.u.def.section->alignment_power;
4729 if (normal_align > symbol_align)
4730 normal_align = symbol_align;
4731 }
4732 else
4733 normal_align = symbol_align;
4734
4735 if (old_alignment)
4736 {
4737 common_align = old_alignment;
4738 common_bfd = old_bfd;
4739 normal_bfd = abfd;
4740 }
4741 else
4742 {
4743 common_align = bfd_log2 (isym->st_value);
4744 common_bfd = abfd;
4745 normal_bfd = old_bfd;
4746 }
4747
4748 if (normal_align < common_align)
d07676f8
NC
4749 {
4750 /* PR binutils/2735 */
4751 if (normal_bfd == NULL)
4eca0228 4752 _bfd_error_handler
695344c0 4753 /* xgettext:c-format */
4f3fedcf
AM
4754 (_("Warning: alignment %u of common symbol `%s' in %B is"
4755 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4756 1 << common_align, name, common_bfd,
4757 1 << normal_align, h->root.u.def.section);
d07676f8 4758 else
4eca0228 4759 _bfd_error_handler
695344c0 4760 /* xgettext:c-format */
d07676f8
NC
4761 (_("Warning: alignment %u of symbol `%s' in %B"
4762 " is smaller than %u in %B"),
c08bb8dd
AM
4763 1 << normal_align, name, normal_bfd,
4764 1 << common_align, common_bfd);
d07676f8 4765 }
4ad4eba5
AM
4766 }
4767
83ad0046 4768 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4769 if (isym->st_size != 0
4770 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4771 && (definition || h->size == 0))
4772 {
83ad0046
L
4773 if (h->size != 0
4774 && h->size != isym->st_size
4775 && ! size_change_ok)
4eca0228 4776 _bfd_error_handler
695344c0 4777 /* xgettext:c-format */
d003868e 4778 (_("Warning: size of symbol `%s' changed"
76cfced5
AM
4779 " from %Lu in %B to %Lu in %B"),
4780 name, h->size, old_bfd, isym->st_size, abfd);
4ad4eba5
AM
4781
4782 h->size = isym->st_size;
4783 }
4784
4785 /* If this is a common symbol, then we always want H->SIZE
4786 to be the size of the common symbol. The code just above
4787 won't fix the size if a common symbol becomes larger. We
4788 don't warn about a size change here, because that is
4f3fedcf 4789 covered by --warn-common. Allow changes between different
fcb93ecf 4790 function types. */
4ad4eba5
AM
4791 if (h->root.type == bfd_link_hash_common)
4792 h->size = h->root.u.c.size;
4793
4794 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4795 && ((definition && !new_weak)
4796 || (old_weak && h->root.type == bfd_link_hash_common)
4797 || h->type == STT_NOTYPE))
4ad4eba5 4798 {
2955ec4c
L
4799 unsigned int type = ELF_ST_TYPE (isym->st_info);
4800
4801 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4802 symbol. */
4803 if (type == STT_GNU_IFUNC
4804 && (abfd->flags & DYNAMIC) != 0)
4805 type = STT_FUNC;
4ad4eba5 4806
2955ec4c
L
4807 if (h->type != type)
4808 {
4809 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4810 /* xgettext:c-format */
4eca0228 4811 _bfd_error_handler
2955ec4c
L
4812 (_("Warning: type of symbol `%s' changed"
4813 " from %d to %d in %B"),
c08bb8dd 4814 name, h->type, type, abfd);
2955ec4c
L
4815
4816 h->type = type;
4817 }
4ad4eba5
AM
4818 }
4819
54ac0771 4820 /* Merge st_other field. */
b8417128 4821 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4822
c3df8c14 4823 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4824 if (definition
4825 && (sec->flags & SEC_DEBUGGING)
4826 && !bfd_link_relocatable (info))
c3df8c14
AM
4827 dynsym = FALSE;
4828
4f3fedcf
AM
4829 /* Nor should we make plugin symbols dynamic. */
4830 if ((abfd->flags & BFD_PLUGIN) != 0)
4831 dynsym = FALSE;
4832
35fc36a8 4833 if (definition)
35399224
L
4834 {
4835 h->target_internal = isym->st_target_internal;
4836 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4837 }
35fc36a8 4838
4ad4eba5
AM
4839 if (definition && !dynamic)
4840 {
4841 char *p = strchr (name, ELF_VER_CHR);
4842 if (p != NULL && p[1] != ELF_VER_CHR)
4843 {
4844 /* Queue non-default versions so that .symver x, x@FOO
4845 aliases can be checked. */
66eb6687 4846 if (!nondeflt_vers)
4ad4eba5 4847 {
66eb6687
AM
4848 amt = ((isymend - isym + 1)
4849 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4850 nondeflt_vers
4851 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4852 if (!nondeflt_vers)
4853 goto error_free_vers;
4ad4eba5 4854 }
66eb6687 4855 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4856 }
4857 }
4858
4859 if (dynsym && h->dynindx == -1)
4860 {
c152c796 4861 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4862 goto error_free_vers;
f6e332e6 4863 if (h->u.weakdef != NULL
4ad4eba5 4864 && ! new_weakdef
f6e332e6 4865 && h->u.weakdef->dynindx == -1)
4ad4eba5 4866 {
66eb6687 4867 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4868 goto error_free_vers;
4869 }
4870 }
1f599d0e 4871 else if (h->dynindx != -1)
4ad4eba5
AM
4872 /* If the symbol already has a dynamic index, but
4873 visibility says it should not be visible, turn it into
4874 a local symbol. */
4875 switch (ELF_ST_VISIBILITY (h->other))
4876 {
4877 case STV_INTERNAL:
4878 case STV_HIDDEN:
4879 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4880 dynsym = FALSE;
4881 break;
4882 }
4883
aef28989
L
4884 /* Don't add DT_NEEDED for references from the dummy bfd nor
4885 for unmatched symbol. */
4ad4eba5 4886 if (!add_needed
aef28989 4887 && matched
4ad4eba5 4888 && definition
010e5ae2 4889 && ((dynsym
ffa9430d 4890 && h->ref_regular_nonweak
4f3fedcf
AM
4891 && (old_bfd == NULL
4892 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4893 || (h->ref_dynamic_nonweak
010e5ae2 4894 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4895 && !on_needed_list (elf_dt_name (abfd),
4896 htab->needed, NULL))))
4ad4eba5
AM
4897 {
4898 int ret;
4899 const char *soname = elf_dt_name (abfd);
4900
16e4ecc0
AM
4901 info->callbacks->minfo ("%!", soname, old_bfd,
4902 h->root.root.string);
4903
4ad4eba5
AM
4904 /* A symbol from a library loaded via DT_NEEDED of some
4905 other library is referenced by a regular object.
e56f61be 4906 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4907 --no-add-needed is used and the reference was not
4908 a weak one. */
4f3fedcf 4909 if (old_bfd != NULL
b918acf9 4910 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4911 {
4eca0228 4912 _bfd_error_handler
695344c0 4913 /* xgettext:c-format */
3cbc5de0 4914 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4915 old_bfd, name);
ff5ac77b 4916 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4917 goto error_free_vers;
4918 }
4919
a50b1753 4920 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4921 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4922
4ad4eba5 4923 add_needed = TRUE;
7e9f0867 4924 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4925 if (ret < 0)
4926 goto error_free_vers;
4927
4928 BFD_ASSERT (ret == 0);
4929 }
4930 }
4931 }
4932
66eb6687
AM
4933 if (extversym != NULL)
4934 {
4935 free (extversym);
4936 extversym = NULL;
4937 }
4938
4939 if (isymbuf != NULL)
4940 {
4941 free (isymbuf);
4942 isymbuf = NULL;
4943 }
4944
4945 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4946 {
4947 unsigned int i;
4948
4949 /* Restore the symbol table. */
f45794cb
AM
4950 old_ent = (char *) old_tab + tabsize;
4951 memset (elf_sym_hashes (abfd), 0,
4952 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4953 htab->root.table.table = old_table;
4954 htab->root.table.size = old_size;
4955 htab->root.table.count = old_count;
66eb6687 4956 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4957 htab->root.undefs = old_undefs;
4958 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4959 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4960 free (old_strtab);
4961 old_strtab = NULL;
66eb6687
AM
4962 for (i = 0; i < htab->root.table.size; i++)
4963 {
4964 struct bfd_hash_entry *p;
4965 struct elf_link_hash_entry *h;
3e0882af
L
4966 bfd_size_type size;
4967 unsigned int alignment_power;
4070765b 4968 unsigned int non_ir_ref_dynamic;
66eb6687
AM
4969
4970 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4971 {
4972 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4973 if (h->root.type == bfd_link_hash_warning)
4974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4975
3e0882af
L
4976 /* Preserve the maximum alignment and size for common
4977 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4978 since it can still be loaded at run time by another
3e0882af
L
4979 dynamic lib. */
4980 if (h->root.type == bfd_link_hash_common)
4981 {
4982 size = h->root.u.c.size;
4983 alignment_power = h->root.u.c.p->alignment_power;
4984 }
4985 else
4986 {
4987 size = 0;
4988 alignment_power = 0;
4989 }
4070765b 4990 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
4991 will be exported when the dynamic lib becomes needed
4992 in the second pass. */
4070765b 4993 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
4994 memcpy (p, old_ent, htab->root.table.entsize);
4995 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4996 h = (struct elf_link_hash_entry *) p;
4997 if (h->root.type == bfd_link_hash_warning)
4998 {
4999 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5000 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5002 }
a4542f1b 5003 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5004 {
5005 if (size > h->root.u.c.size)
5006 h->root.u.c.size = size;
5007 if (alignment_power > h->root.u.c.p->alignment_power)
5008 h->root.u.c.p->alignment_power = alignment_power;
5009 }
4070765b 5010 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5011 }
5012 }
5013
5061a885
AM
5014 /* Make a special call to the linker "notice" function to
5015 tell it that symbols added for crefs may need to be removed. */
e5034e59 5016 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5017 goto error_free_vers;
5061a885 5018
66eb6687
AM
5019 free (old_tab);
5020 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5021 alloc_mark);
5022 if (nondeflt_vers != NULL)
5023 free (nondeflt_vers);
5024 return TRUE;
5025 }
2de92251 5026
66eb6687
AM
5027 if (old_tab != NULL)
5028 {
e5034e59 5029 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5030 goto error_free_vers;
66eb6687
AM
5031 free (old_tab);
5032 old_tab = NULL;
5033 }
5034
c6e8a9a8
L
5035 /* Now that all the symbols from this input file are created, if
5036 not performing a relocatable link, handle .symver foo, foo@BAR
5037 such that any relocs against foo become foo@BAR. */
0e1862bb 5038 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5039 {
ef53be89 5040 size_t cnt, symidx;
4ad4eba5
AM
5041
5042 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5043 {
5044 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5045 char *shortname, *p;
5046
5047 p = strchr (h->root.root.string, ELF_VER_CHR);
5048 if (p == NULL
5049 || (h->root.type != bfd_link_hash_defined
5050 && h->root.type != bfd_link_hash_defweak))
5051 continue;
5052
5053 amt = p - h->root.root.string;
a50b1753 5054 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5055 if (!shortname)
5056 goto error_free_vers;
4ad4eba5
AM
5057 memcpy (shortname, h->root.root.string, amt);
5058 shortname[amt] = '\0';
5059
5060 hi = (struct elf_link_hash_entry *)
66eb6687 5061 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5062 FALSE, FALSE, FALSE);
5063 if (hi != NULL
5064 && hi->root.type == h->root.type
5065 && hi->root.u.def.value == h->root.u.def.value
5066 && hi->root.u.def.section == h->root.u.def.section)
5067 {
5068 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5069 hi->root.type = bfd_link_hash_indirect;
5070 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5071 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5072 sym_hash = elf_sym_hashes (abfd);
5073 if (sym_hash)
5074 for (symidx = 0; symidx < extsymcount; ++symidx)
5075 if (sym_hash[symidx] == hi)
5076 {
5077 sym_hash[symidx] = h;
5078 break;
5079 }
5080 }
5081 free (shortname);
5082 }
5083 free (nondeflt_vers);
5084 nondeflt_vers = NULL;
5085 }
5086
4ad4eba5
AM
5087 /* Now set the weakdefs field correctly for all the weak defined
5088 symbols we found. The only way to do this is to search all the
5089 symbols. Since we only need the information for non functions in
5090 dynamic objects, that's the only time we actually put anything on
5091 the list WEAKS. We need this information so that if a regular
5092 object refers to a symbol defined weakly in a dynamic object, the
5093 real symbol in the dynamic object is also put in the dynamic
5094 symbols; we also must arrange for both symbols to point to the
5095 same memory location. We could handle the general case of symbol
5096 aliasing, but a general symbol alias can only be generated in
5097 assembler code, handling it correctly would be very time
5098 consuming, and other ELF linkers don't handle general aliasing
5099 either. */
5100 if (weaks != NULL)
5101 {
5102 struct elf_link_hash_entry **hpp;
5103 struct elf_link_hash_entry **hppend;
5104 struct elf_link_hash_entry **sorted_sym_hash;
5105 struct elf_link_hash_entry *h;
5106 size_t sym_count;
5107
5108 /* Since we have to search the whole symbol list for each weak
5109 defined symbol, search time for N weak defined symbols will be
5110 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5111 amt = extsymcount;
5112 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5113 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5114 if (sorted_sym_hash == NULL)
5115 goto error_return;
5116 sym_hash = sorted_sym_hash;
5117 hpp = elf_sym_hashes (abfd);
5118 hppend = hpp + extsymcount;
5119 sym_count = 0;
5120 for (; hpp < hppend; hpp++)
5121 {
5122 h = *hpp;
5123 if (h != NULL
5124 && h->root.type == bfd_link_hash_defined
fcb93ecf 5125 && !bed->is_function_type (h->type))
4ad4eba5
AM
5126 {
5127 *sym_hash = h;
5128 sym_hash++;
5129 sym_count++;
5130 }
5131 }
5132
5133 qsort (sorted_sym_hash, sym_count,
5134 sizeof (struct elf_link_hash_entry *),
5135 elf_sort_symbol);
5136
5137 while (weaks != NULL)
5138 {
5139 struct elf_link_hash_entry *hlook;
5140 asection *slook;
5141 bfd_vma vlook;
ed54588d 5142 size_t i, j, idx = 0;
4ad4eba5
AM
5143
5144 hlook = weaks;
f6e332e6
AM
5145 weaks = hlook->u.weakdef;
5146 hlook->u.weakdef = NULL;
4ad4eba5 5147
e3e53eed
AM
5148 if (hlook->root.type != bfd_link_hash_defined
5149 && hlook->root.type != bfd_link_hash_defweak)
5150 continue;
5151
4ad4eba5
AM
5152 slook = hlook->root.u.def.section;
5153 vlook = hlook->root.u.def.value;
5154
4ad4eba5
AM
5155 i = 0;
5156 j = sym_count;
14160578 5157 while (i != j)
4ad4eba5
AM
5158 {
5159 bfd_signed_vma vdiff;
5160 idx = (i + j) / 2;
14160578 5161 h = sorted_sym_hash[idx];
4ad4eba5
AM
5162 vdiff = vlook - h->root.u.def.value;
5163 if (vdiff < 0)
5164 j = idx;
5165 else if (vdiff > 0)
5166 i = idx + 1;
5167 else
5168 {
d3435ae8 5169 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5170 if (sdiff < 0)
5171 j = idx;
5172 else if (sdiff > 0)
5173 i = idx + 1;
5174 else
14160578 5175 break;
4ad4eba5
AM
5176 }
5177 }
5178
5179 /* We didn't find a value/section match. */
14160578 5180 if (i == j)
4ad4eba5
AM
5181 continue;
5182
14160578
AM
5183 /* With multiple aliases, or when the weak symbol is already
5184 strongly defined, we have multiple matching symbols and
5185 the binary search above may land on any of them. Step
5186 one past the matching symbol(s). */
5187 while (++idx != j)
5188 {
5189 h = sorted_sym_hash[idx];
5190 if (h->root.u.def.section != slook
5191 || h->root.u.def.value != vlook)
5192 break;
5193 }
5194
5195 /* Now look back over the aliases. Since we sorted by size
5196 as well as value and section, we'll choose the one with
5197 the largest size. */
5198 while (idx-- != i)
4ad4eba5 5199 {
14160578 5200 h = sorted_sym_hash[idx];
4ad4eba5
AM
5201
5202 /* Stop if value or section doesn't match. */
14160578
AM
5203 if (h->root.u.def.section != slook
5204 || h->root.u.def.value != vlook)
4ad4eba5
AM
5205 break;
5206 else if (h != hlook)
5207 {
f6e332e6 5208 hlook->u.weakdef = h;
4ad4eba5
AM
5209
5210 /* If the weak definition is in the list of dynamic
5211 symbols, make sure the real definition is put
5212 there as well. */
5213 if (hlook->dynindx != -1 && h->dynindx == -1)
5214 {
c152c796 5215 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5216 {
5217 err_free_sym_hash:
5218 free (sorted_sym_hash);
5219 goto error_return;
5220 }
4ad4eba5
AM
5221 }
5222
5223 /* If the real definition is in the list of dynamic
5224 symbols, make sure the weak definition is put
5225 there as well. If we don't do this, then the
5226 dynamic loader might not merge the entries for the
5227 real definition and the weak definition. */
5228 if (h->dynindx != -1 && hlook->dynindx == -1)
5229 {
c152c796 5230 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5231 goto err_free_sym_hash;
4ad4eba5
AM
5232 }
5233 break;
5234 }
5235 }
5236 }
5237
5238 free (sorted_sym_hash);
5239 }
5240
33177bb1
AM
5241 if (bed->check_directives
5242 && !(*bed->check_directives) (abfd, info))
5243 return FALSE;
85fbca6a 5244
4ad4eba5
AM
5245 /* If this is a non-traditional link, try to optimize the handling
5246 of the .stab/.stabstr sections. */
5247 if (! dynamic
5248 && ! info->traditional_format
66eb6687 5249 && is_elf_hash_table (htab)
4ad4eba5
AM
5250 && (info->strip != strip_all && info->strip != strip_debugger))
5251 {
5252 asection *stabstr;
5253
5254 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5255 if (stabstr != NULL)
5256 {
5257 bfd_size_type string_offset = 0;
5258 asection *stab;
5259
5260 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5261 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5262 && (!stab->name[5] ||
5263 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5264 && (stab->flags & SEC_MERGE) == 0
5265 && !bfd_is_abs_section (stab->output_section))
5266 {
5267 struct bfd_elf_section_data *secdata;
5268
5269 secdata = elf_section_data (stab);
66eb6687
AM
5270 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5271 stabstr, &secdata->sec_info,
4ad4eba5
AM
5272 &string_offset))
5273 goto error_return;
5274 if (secdata->sec_info)
dbaa2011 5275 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5276 }
5277 }
5278 }
5279
66eb6687 5280 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5281 {
5282 /* Add this bfd to the loaded list. */
5283 struct elf_link_loaded_list *n;
5284
ca4be51c 5285 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5286 if (n == NULL)
5287 goto error_return;
5288 n->abfd = abfd;
66eb6687
AM
5289 n->next = htab->loaded;
5290 htab->loaded = n;
4ad4eba5
AM
5291 }
5292
5293 return TRUE;
5294
5295 error_free_vers:
66eb6687
AM
5296 if (old_tab != NULL)
5297 free (old_tab);
5b677558
AM
5298 if (old_strtab != NULL)
5299 free (old_strtab);
4ad4eba5
AM
5300 if (nondeflt_vers != NULL)
5301 free (nondeflt_vers);
5302 if (extversym != NULL)
5303 free (extversym);
5304 error_free_sym:
5305 if (isymbuf != NULL)
5306 free (isymbuf);
5307 error_return:
5308 return FALSE;
5309}
5310
8387904d
AM
5311/* Return the linker hash table entry of a symbol that might be
5312 satisfied by an archive symbol. Return -1 on error. */
5313
5314struct elf_link_hash_entry *
5315_bfd_elf_archive_symbol_lookup (bfd *abfd,
5316 struct bfd_link_info *info,
5317 const char *name)
5318{
5319 struct elf_link_hash_entry *h;
5320 char *p, *copy;
5321 size_t len, first;
5322
2a41f396 5323 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5324 if (h != NULL)
5325 return h;
5326
5327 /* If this is a default version (the name contains @@), look up the
5328 symbol again with only one `@' as well as without the version.
5329 The effect is that references to the symbol with and without the
5330 version will be matched by the default symbol in the archive. */
5331
5332 p = strchr (name, ELF_VER_CHR);
5333 if (p == NULL || p[1] != ELF_VER_CHR)
5334 return h;
5335
5336 /* First check with only one `@'. */
5337 len = strlen (name);
a50b1753 5338 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5339 if (copy == NULL)
5340 return (struct elf_link_hash_entry *) 0 - 1;
5341
5342 first = p - name + 1;
5343 memcpy (copy, name, first);
5344 memcpy (copy + first, name + first + 1, len - first);
5345
2a41f396 5346 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5347 if (h == NULL)
5348 {
5349 /* We also need to check references to the symbol without the
5350 version. */
5351 copy[first - 1] = '\0';
5352 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5353 FALSE, FALSE, TRUE);
8387904d
AM
5354 }
5355
5356 bfd_release (abfd, copy);
5357 return h;
5358}
5359
0ad989f9 5360/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5361 don't use _bfd_generic_link_add_archive_symbols because we need to
5362 handle versioned symbols.
0ad989f9
L
5363
5364 Fortunately, ELF archive handling is simpler than that done by
5365 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5366 oddities. In ELF, if we find a symbol in the archive map, and the
5367 symbol is currently undefined, we know that we must pull in that
5368 object file.
5369
5370 Unfortunately, we do have to make multiple passes over the symbol
5371 table until nothing further is resolved. */
5372
4ad4eba5
AM
5373static bfd_boolean
5374elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5375{
5376 symindex c;
13e570f8 5377 unsigned char *included = NULL;
0ad989f9
L
5378 carsym *symdefs;
5379 bfd_boolean loop;
5380 bfd_size_type amt;
8387904d
AM
5381 const struct elf_backend_data *bed;
5382 struct elf_link_hash_entry * (*archive_symbol_lookup)
5383 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5384
5385 if (! bfd_has_map (abfd))
5386 {
5387 /* An empty archive is a special case. */
5388 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5389 return TRUE;
5390 bfd_set_error (bfd_error_no_armap);
5391 return FALSE;
5392 }
5393
5394 /* Keep track of all symbols we know to be already defined, and all
5395 files we know to be already included. This is to speed up the
5396 second and subsequent passes. */
5397 c = bfd_ardata (abfd)->symdef_count;
5398 if (c == 0)
5399 return TRUE;
5400 amt = c;
13e570f8
AM
5401 amt *= sizeof (*included);
5402 included = (unsigned char *) bfd_zmalloc (amt);
5403 if (included == NULL)
5404 return FALSE;
0ad989f9
L
5405
5406 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5407 bed = get_elf_backend_data (abfd);
5408 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5409
5410 do
5411 {
5412 file_ptr last;
5413 symindex i;
5414 carsym *symdef;
5415 carsym *symdefend;
5416
5417 loop = FALSE;
5418 last = -1;
5419
5420 symdef = symdefs;
5421 symdefend = symdef + c;
5422 for (i = 0; symdef < symdefend; symdef++, i++)
5423 {
5424 struct elf_link_hash_entry *h;
5425 bfd *element;
5426 struct bfd_link_hash_entry *undefs_tail;
5427 symindex mark;
5428
13e570f8 5429 if (included[i])
0ad989f9
L
5430 continue;
5431 if (symdef->file_offset == last)
5432 {
5433 included[i] = TRUE;
5434 continue;
5435 }
5436
8387904d
AM
5437 h = archive_symbol_lookup (abfd, info, symdef->name);
5438 if (h == (struct elf_link_hash_entry *) 0 - 1)
5439 goto error_return;
0ad989f9
L
5440
5441 if (h == NULL)
5442 continue;
5443
5444 if (h->root.type == bfd_link_hash_common)
5445 {
5446 /* We currently have a common symbol. The archive map contains
5447 a reference to this symbol, so we may want to include it. We
5448 only want to include it however, if this archive element
5449 contains a definition of the symbol, not just another common
5450 declaration of it.
5451
5452 Unfortunately some archivers (including GNU ar) will put
5453 declarations of common symbols into their archive maps, as
5454 well as real definitions, so we cannot just go by the archive
5455 map alone. Instead we must read in the element's symbol
5456 table and check that to see what kind of symbol definition
5457 this is. */
5458 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5459 continue;
5460 }
5461 else if (h->root.type != bfd_link_hash_undefined)
5462 {
5463 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5464 /* Symbol must be defined. Don't check it again. */
5465 included[i] = TRUE;
0ad989f9
L
5466 continue;
5467 }
5468
5469 /* We need to include this archive member. */
5470 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5471 if (element == NULL)
5472 goto error_return;
5473
5474 if (! bfd_check_format (element, bfd_object))
5475 goto error_return;
5476
0ad989f9
L
5477 undefs_tail = info->hash->undefs_tail;
5478
0e144ba7
AM
5479 if (!(*info->callbacks
5480 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5481 continue;
0e144ba7 5482 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5483 goto error_return;
5484
5485 /* If there are any new undefined symbols, we need to make
5486 another pass through the archive in order to see whether
5487 they can be defined. FIXME: This isn't perfect, because
5488 common symbols wind up on undefs_tail and because an
5489 undefined symbol which is defined later on in this pass
5490 does not require another pass. This isn't a bug, but it
5491 does make the code less efficient than it could be. */
5492 if (undefs_tail != info->hash->undefs_tail)
5493 loop = TRUE;
5494
5495 /* Look backward to mark all symbols from this object file
5496 which we have already seen in this pass. */
5497 mark = i;
5498 do
5499 {
5500 included[mark] = TRUE;
5501 if (mark == 0)
5502 break;
5503 --mark;
5504 }
5505 while (symdefs[mark].file_offset == symdef->file_offset);
5506
5507 /* We mark subsequent symbols from this object file as we go
5508 on through the loop. */
5509 last = symdef->file_offset;
5510 }
5511 }
5512 while (loop);
5513
0ad989f9
L
5514 free (included);
5515
5516 return TRUE;
5517
5518 error_return:
0ad989f9
L
5519 if (included != NULL)
5520 free (included);
5521 return FALSE;
5522}
4ad4eba5
AM
5523
5524/* Given an ELF BFD, add symbols to the global hash table as
5525 appropriate. */
5526
5527bfd_boolean
5528bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5529{
5530 switch (bfd_get_format (abfd))
5531 {
5532 case bfd_object:
5533 return elf_link_add_object_symbols (abfd, info);
5534 case bfd_archive:
5535 return elf_link_add_archive_symbols (abfd, info);
5536 default:
5537 bfd_set_error (bfd_error_wrong_format);
5538 return FALSE;
5539 }
5540}
5a580b3a 5541\f
14b1c01e
AM
5542struct hash_codes_info
5543{
5544 unsigned long *hashcodes;
5545 bfd_boolean error;
5546};
a0c8462f 5547
5a580b3a
AM
5548/* This function will be called though elf_link_hash_traverse to store
5549 all hash value of the exported symbols in an array. */
5550
5551static bfd_boolean
5552elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5553{
a50b1753 5554 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5555 const char *name;
5a580b3a
AM
5556 unsigned long ha;
5557 char *alc = NULL;
5558
5a580b3a
AM
5559 /* Ignore indirect symbols. These are added by the versioning code. */
5560 if (h->dynindx == -1)
5561 return TRUE;
5562
5563 name = h->root.root.string;
422f1182 5564 if (h->versioned >= versioned)
5a580b3a 5565 {
422f1182
L
5566 char *p = strchr (name, ELF_VER_CHR);
5567 if (p != NULL)
14b1c01e 5568 {
422f1182
L
5569 alc = (char *) bfd_malloc (p - name + 1);
5570 if (alc == NULL)
5571 {
5572 inf->error = TRUE;
5573 return FALSE;
5574 }
5575 memcpy (alc, name, p - name);
5576 alc[p - name] = '\0';
5577 name = alc;
14b1c01e 5578 }
5a580b3a
AM
5579 }
5580
5581 /* Compute the hash value. */
5582 ha = bfd_elf_hash (name);
5583
5584 /* Store the found hash value in the array given as the argument. */
14b1c01e 5585 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5586
5587 /* And store it in the struct so that we can put it in the hash table
5588 later. */
f6e332e6 5589 h->u.elf_hash_value = ha;
5a580b3a
AM
5590
5591 if (alc != NULL)
5592 free (alc);
5593
5594 return TRUE;
5595}
5596
fdc90cb4
JJ
5597struct collect_gnu_hash_codes
5598{
5599 bfd *output_bfd;
5600 const struct elf_backend_data *bed;
5601 unsigned long int nsyms;
5602 unsigned long int maskbits;
5603 unsigned long int *hashcodes;
5604 unsigned long int *hashval;
5605 unsigned long int *indx;
5606 unsigned long int *counts;
5607 bfd_vma *bitmask;
5608 bfd_byte *contents;
5609 long int min_dynindx;
5610 unsigned long int bucketcount;
5611 unsigned long int symindx;
5612 long int local_indx;
5613 long int shift1, shift2;
5614 unsigned long int mask;
14b1c01e 5615 bfd_boolean error;
fdc90cb4
JJ
5616};
5617
5618/* This function will be called though elf_link_hash_traverse to store
5619 all hash value of the exported symbols in an array. */
5620
5621static bfd_boolean
5622elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5623{
a50b1753 5624 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5625 const char *name;
fdc90cb4
JJ
5626 unsigned long ha;
5627 char *alc = NULL;
5628
fdc90cb4
JJ
5629 /* Ignore indirect symbols. These are added by the versioning code. */
5630 if (h->dynindx == -1)
5631 return TRUE;
5632
5633 /* Ignore also local symbols and undefined symbols. */
5634 if (! (*s->bed->elf_hash_symbol) (h))
5635 return TRUE;
5636
5637 name = h->root.root.string;
422f1182 5638 if (h->versioned >= versioned)
fdc90cb4 5639 {
422f1182
L
5640 char *p = strchr (name, ELF_VER_CHR);
5641 if (p != NULL)
14b1c01e 5642 {
422f1182
L
5643 alc = (char *) bfd_malloc (p - name + 1);
5644 if (alc == NULL)
5645 {
5646 s->error = TRUE;
5647 return FALSE;
5648 }
5649 memcpy (alc, name, p - name);
5650 alc[p - name] = '\0';
5651 name = alc;
14b1c01e 5652 }
fdc90cb4
JJ
5653 }
5654
5655 /* Compute the hash value. */
5656 ha = bfd_elf_gnu_hash (name);
5657
5658 /* Store the found hash value in the array for compute_bucket_count,
5659 and also for .dynsym reordering purposes. */
5660 s->hashcodes[s->nsyms] = ha;
5661 s->hashval[h->dynindx] = ha;
5662 ++s->nsyms;
5663 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5664 s->min_dynindx = h->dynindx;
5665
5666 if (alc != NULL)
5667 free (alc);
5668
5669 return TRUE;
5670}
5671
5672/* This function will be called though elf_link_hash_traverse to do
5673 final dynaminc symbol renumbering. */
5674
5675static bfd_boolean
5676elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5677{
a50b1753 5678 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5679 unsigned long int bucket;
5680 unsigned long int val;
5681
fdc90cb4
JJ
5682 /* Ignore indirect symbols. */
5683 if (h->dynindx == -1)
5684 return TRUE;
5685
5686 /* Ignore also local symbols and undefined symbols. */
5687 if (! (*s->bed->elf_hash_symbol) (h))
5688 {
5689 if (h->dynindx >= s->min_dynindx)
5690 h->dynindx = s->local_indx++;
5691 return TRUE;
5692 }
5693
5694 bucket = s->hashval[h->dynindx] % s->bucketcount;
5695 val = (s->hashval[h->dynindx] >> s->shift1)
5696 & ((s->maskbits >> s->shift1) - 1);
5697 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5698 s->bitmask[val]
5699 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5700 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5701 if (s->counts[bucket] == 1)
5702 /* Last element terminates the chain. */
5703 val |= 1;
5704 bfd_put_32 (s->output_bfd, val,
5705 s->contents + (s->indx[bucket] - s->symindx) * 4);
5706 --s->counts[bucket];
5707 h->dynindx = s->indx[bucket]++;
5708 return TRUE;
5709}
5710
5711/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5712
5713bfd_boolean
5714_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5715{
5716 return !(h->forced_local
5717 || h->root.type == bfd_link_hash_undefined
5718 || h->root.type == bfd_link_hash_undefweak
5719 || ((h->root.type == bfd_link_hash_defined
5720 || h->root.type == bfd_link_hash_defweak)
5721 && h->root.u.def.section->output_section == NULL));
5722}
5723
5a580b3a
AM
5724/* Array used to determine the number of hash table buckets to use
5725 based on the number of symbols there are. If there are fewer than
5726 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5727 fewer than 37 we use 17 buckets, and so forth. We never use more
5728 than 32771 buckets. */
5729
5730static const size_t elf_buckets[] =
5731{
5732 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5733 16411, 32771, 0
5734};
5735
5736/* Compute bucket count for hashing table. We do not use a static set
5737 of possible tables sizes anymore. Instead we determine for all
5738 possible reasonable sizes of the table the outcome (i.e., the
5739 number of collisions etc) and choose the best solution. The
5740 weighting functions are not too simple to allow the table to grow
5741 without bounds. Instead one of the weighting factors is the size.
5742 Therefore the result is always a good payoff between few collisions
5743 (= short chain lengths) and table size. */
5744static size_t
b20dd2ce 5745compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5746 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5747 unsigned long int nsyms,
5748 int gnu_hash)
5a580b3a 5749{
5a580b3a 5750 size_t best_size = 0;
5a580b3a 5751 unsigned long int i;
5a580b3a 5752
5a580b3a
AM
5753 /* We have a problem here. The following code to optimize the table
5754 size requires an integer type with more the 32 bits. If
5755 BFD_HOST_U_64_BIT is set we know about such a type. */
5756#ifdef BFD_HOST_U_64_BIT
5757 if (info->optimize)
5758 {
5a580b3a
AM
5759 size_t minsize;
5760 size_t maxsize;
5761 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5762 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5763 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5764 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5765 unsigned long int *counts;
d40f3da9 5766 bfd_size_type amt;
0883b6e0 5767 unsigned int no_improvement_count = 0;
5a580b3a
AM
5768
5769 /* Possible optimization parameters: if we have NSYMS symbols we say
5770 that the hashing table must at least have NSYMS/4 and at most
5771 2*NSYMS buckets. */
5772 minsize = nsyms / 4;
5773 if (minsize == 0)
5774 minsize = 1;
5775 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5776 if (gnu_hash)
5777 {
5778 if (minsize < 2)
5779 minsize = 2;
5780 if ((best_size & 31) == 0)
5781 ++best_size;
5782 }
5a580b3a
AM
5783
5784 /* Create array where we count the collisions in. We must use bfd_malloc
5785 since the size could be large. */
5786 amt = maxsize;
5787 amt *= sizeof (unsigned long int);
a50b1753 5788 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5789 if (counts == NULL)
fdc90cb4 5790 return 0;
5a580b3a
AM
5791
5792 /* Compute the "optimal" size for the hash table. The criteria is a
5793 minimal chain length. The minor criteria is (of course) the size
5794 of the table. */
5795 for (i = minsize; i < maxsize; ++i)
5796 {
5797 /* Walk through the array of hashcodes and count the collisions. */
5798 BFD_HOST_U_64_BIT max;
5799 unsigned long int j;
5800 unsigned long int fact;
5801
fdc90cb4
JJ
5802 if (gnu_hash && (i & 31) == 0)
5803 continue;
5804
5a580b3a
AM
5805 memset (counts, '\0', i * sizeof (unsigned long int));
5806
5807 /* Determine how often each hash bucket is used. */
5808 for (j = 0; j < nsyms; ++j)
5809 ++counts[hashcodes[j] % i];
5810
5811 /* For the weight function we need some information about the
5812 pagesize on the target. This is information need not be 100%
5813 accurate. Since this information is not available (so far) we
5814 define it here to a reasonable default value. If it is crucial
5815 to have a better value some day simply define this value. */
5816# ifndef BFD_TARGET_PAGESIZE
5817# define BFD_TARGET_PAGESIZE (4096)
5818# endif
5819
fdc90cb4
JJ
5820 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5821 and the chains. */
5822 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5823
5824# if 1
5825 /* Variant 1: optimize for short chains. We add the squares
5826 of all the chain lengths (which favors many small chain
5827 over a few long chains). */
5828 for (j = 0; j < i; ++j)
5829 max += counts[j] * counts[j];
5830
5831 /* This adds penalties for the overall size of the table. */
fdc90cb4 5832 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5833 max *= fact * fact;
5834# else
5835 /* Variant 2: Optimize a lot more for small table. Here we
5836 also add squares of the size but we also add penalties for
5837 empty slots (the +1 term). */
5838 for (j = 0; j < i; ++j)
5839 max += (1 + counts[j]) * (1 + counts[j]);
5840
5841 /* The overall size of the table is considered, but not as
5842 strong as in variant 1, where it is squared. */
fdc90cb4 5843 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5844 max *= fact;
5845# endif
5846
5847 /* Compare with current best results. */
5848 if (max < best_chlen)
5849 {
5850 best_chlen = max;
5851 best_size = i;
ca4be51c 5852 no_improvement_count = 0;
5a580b3a 5853 }
0883b6e0
NC
5854 /* PR 11843: Avoid futile long searches for the best bucket size
5855 when there are a large number of symbols. */
5856 else if (++no_improvement_count == 100)
5857 break;
5a580b3a
AM
5858 }
5859
5860 free (counts);
5861 }
5862 else
5863#endif /* defined (BFD_HOST_U_64_BIT) */
5864 {
5865 /* This is the fallback solution if no 64bit type is available or if we
5866 are not supposed to spend much time on optimizations. We select the
5867 bucket count using a fixed set of numbers. */
5868 for (i = 0; elf_buckets[i] != 0; i++)
5869 {
5870 best_size = elf_buckets[i];
fdc90cb4 5871 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5872 break;
5873 }
fdc90cb4
JJ
5874 if (gnu_hash && best_size < 2)
5875 best_size = 2;
5a580b3a
AM
5876 }
5877
5a580b3a
AM
5878 return best_size;
5879}
5880
d0bf826b
AM
5881/* Size any SHT_GROUP section for ld -r. */
5882
5883bfd_boolean
5884_bfd_elf_size_group_sections (struct bfd_link_info *info)
5885{
5886 bfd *ibfd;
57963c05 5887 asection *s;
d0bf826b 5888
c72f2fb2 5889 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 5890 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
5891 && (s = ibfd->sections) != NULL
5892 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
5893 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5894 return FALSE;
5895 return TRUE;
5896}
5897
04c3a755
NS
5898/* Set a default stack segment size. The value in INFO wins. If it
5899 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5900 undefined it is initialized. */
5901
5902bfd_boolean
5903bfd_elf_stack_segment_size (bfd *output_bfd,
5904 struct bfd_link_info *info,
5905 const char *legacy_symbol,
5906 bfd_vma default_size)
5907{
5908 struct elf_link_hash_entry *h = NULL;
5909
5910 /* Look for legacy symbol. */
5911 if (legacy_symbol)
5912 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5913 FALSE, FALSE, FALSE);
5914 if (h && (h->root.type == bfd_link_hash_defined
5915 || h->root.type == bfd_link_hash_defweak)
5916 && h->def_regular
5917 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5918 {
5919 /* The symbol has no type if specified on the command line. */
5920 h->type = STT_OBJECT;
5921 if (info->stacksize)
695344c0 5922 /* xgettext:c-format */
4eca0228
AM
5923 _bfd_error_handler (_("%B: stack size specified and %s set"),
5924 output_bfd, legacy_symbol);
04c3a755 5925 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5926 /* xgettext:c-format */
4eca0228
AM
5927 _bfd_error_handler (_("%B: %s not absolute"),
5928 output_bfd, legacy_symbol);
04c3a755
NS
5929 else
5930 info->stacksize = h->root.u.def.value;
5931 }
5932
5933 if (!info->stacksize)
5934 /* If the user didn't set a size, or explicitly inhibit the
5935 size, set it now. */
5936 info->stacksize = default_size;
5937
5938 /* Provide the legacy symbol, if it is referenced. */
5939 if (h && (h->root.type == bfd_link_hash_undefined
5940 || h->root.type == bfd_link_hash_undefweak))
5941 {
5942 struct bfd_link_hash_entry *bh = NULL;
5943
5944 if (!(_bfd_generic_link_add_one_symbol
5945 (info, output_bfd, legacy_symbol,
5946 BSF_GLOBAL, bfd_abs_section_ptr,
5947 info->stacksize >= 0 ? info->stacksize : 0,
5948 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5949 return FALSE;
5950
5951 h = (struct elf_link_hash_entry *) bh;
5952 h->def_regular = 1;
5953 h->type = STT_OBJECT;
5954 }
5955
5956 return TRUE;
5957}
5958
b531344c
MR
5959/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5960
5961struct elf_gc_sweep_symbol_info
5962{
5963 struct bfd_link_info *info;
5964 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5965 bfd_boolean);
5966};
5967
5968static bfd_boolean
5969elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5970{
5971 if (!h->mark
5972 && (((h->root.type == bfd_link_hash_defined
5973 || h->root.type == bfd_link_hash_defweak)
5974 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5975 && h->root.u.def.section->gc_mark))
5976 || h->root.type == bfd_link_hash_undefined
5977 || h->root.type == bfd_link_hash_undefweak))
5978 {
5979 struct elf_gc_sweep_symbol_info *inf;
5980
5981 inf = (struct elf_gc_sweep_symbol_info *) data;
5982 (*inf->hide_symbol) (inf->info, h, TRUE);
5983 h->def_regular = 0;
5984 h->ref_regular = 0;
5985 h->ref_regular_nonweak = 0;
5986 }
5987
5988 return TRUE;
5989}
5990
5a580b3a
AM
5991/* Set up the sizes and contents of the ELF dynamic sections. This is
5992 called by the ELF linker emulation before_allocation routine. We
5993 must set the sizes of the sections before the linker sets the
5994 addresses of the various sections. */
5995
5996bfd_boolean
5997bfd_elf_size_dynamic_sections (bfd *output_bfd,
5998 const char *soname,
5999 const char *rpath,
6000 const char *filter_shlib,
7ee314fa
AM
6001 const char *audit,
6002 const char *depaudit,
5a580b3a
AM
6003 const char * const *auxiliary_filters,
6004 struct bfd_link_info *info,
fd91d419 6005 asection **sinterpptr)
5a580b3a 6006{
5a580b3a
AM
6007 bfd *dynobj;
6008 const struct elf_backend_data *bed;
5a580b3a
AM
6009
6010 *sinterpptr = NULL;
6011
5a580b3a
AM
6012 if (!is_elf_hash_table (info->hash))
6013 return TRUE;
6014
5a580b3a
AM
6015 dynobj = elf_hash_table (info)->dynobj;
6016
9a2a56cc 6017 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6018 {
902e9fc7
MR
6019 struct bfd_elf_version_tree *verdefs;
6020 struct elf_info_failed asvinfo;
5a580b3a
AM
6021 struct bfd_elf_version_tree *t;
6022 struct bfd_elf_version_expr *d;
902e9fc7 6023 asection *s;
e6699019 6024 size_t soname_indx;
7ee314fa 6025
5a580b3a
AM
6026 /* If we are supposed to export all symbols into the dynamic symbol
6027 table (this is not the normal case), then do so. */
55255dae 6028 if (info->export_dynamic
0e1862bb 6029 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6030 {
3d13f3e9
AM
6031 struct elf_info_failed eif;
6032
6033 eif.info = info;
6034 eif.failed = FALSE;
5a580b3a
AM
6035 elf_link_hash_traverse (elf_hash_table (info),
6036 _bfd_elf_export_symbol,
6037 &eif);
6038 if (eif.failed)
6039 return FALSE;
6040 }
6041
e6699019
L
6042 if (soname != NULL)
6043 {
6044 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6045 soname, TRUE);
6046 if (soname_indx == (size_t) -1
6047 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6048 return FALSE;
6049 }
6050 else
6051 soname_indx = (size_t) -1;
6052
5a580b3a 6053 /* Make all global versions with definition. */
fd91d419 6054 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6055 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6056 if (!d->symver && d->literal)
5a580b3a
AM
6057 {
6058 const char *verstr, *name;
6059 size_t namelen, verlen, newlen;
93252b1c 6060 char *newname, *p, leading_char;
5a580b3a
AM
6061 struct elf_link_hash_entry *newh;
6062
93252b1c 6063 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6064 name = d->pattern;
93252b1c 6065 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6066 verstr = t->name;
6067 verlen = strlen (verstr);
6068 newlen = namelen + verlen + 3;
6069
a50b1753 6070 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6071 if (newname == NULL)
6072 return FALSE;
93252b1c
MF
6073 newname[0] = leading_char;
6074 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6075
6076 /* Check the hidden versioned definition. */
6077 p = newname + namelen;
6078 *p++ = ELF_VER_CHR;
6079 memcpy (p, verstr, verlen + 1);
6080 newh = elf_link_hash_lookup (elf_hash_table (info),
6081 newname, FALSE, FALSE,
6082 FALSE);
6083 if (newh == NULL
6084 || (newh->root.type != bfd_link_hash_defined
6085 && newh->root.type != bfd_link_hash_defweak))
6086 {
6087 /* Check the default versioned definition. */
6088 *p++ = ELF_VER_CHR;
6089 memcpy (p, verstr, verlen + 1);
6090 newh = elf_link_hash_lookup (elf_hash_table (info),
6091 newname, FALSE, FALSE,
6092 FALSE);
6093 }
6094 free (newname);
6095
6096 /* Mark this version if there is a definition and it is
6097 not defined in a shared object. */
6098 if (newh != NULL
f5385ebf 6099 && !newh->def_dynamic
5a580b3a
AM
6100 && (newh->root.type == bfd_link_hash_defined
6101 || newh->root.type == bfd_link_hash_defweak))
6102 d->symver = 1;
6103 }
6104
6105 /* Attach all the symbols to their version information. */
5a580b3a 6106 asvinfo.info = info;
5a580b3a
AM
6107 asvinfo.failed = FALSE;
6108
6109 elf_link_hash_traverse (elf_hash_table (info),
6110 _bfd_elf_link_assign_sym_version,
6111 &asvinfo);
6112 if (asvinfo.failed)
6113 return FALSE;
6114
6115 if (!info->allow_undefined_version)
6116 {
6117 /* Check if all global versions have a definition. */
3d13f3e9 6118 bfd_boolean all_defined = TRUE;
fd91d419 6119 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6120 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6121 if (d->literal && !d->symver && !d->script)
5a580b3a 6122 {
4eca0228 6123 _bfd_error_handler
5a580b3a
AM
6124 (_("%s: undefined version: %s"),
6125 d->pattern, t->name);
6126 all_defined = FALSE;
6127 }
6128
6129 if (!all_defined)
6130 {
6131 bfd_set_error (bfd_error_bad_value);
6132 return FALSE;
6133 }
6134 }
6135
902e9fc7
MR
6136 /* Set up the version definition section. */
6137 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6138 BFD_ASSERT (s != NULL);
5a580b3a 6139
902e9fc7
MR
6140 /* We may have created additional version definitions if we are
6141 just linking a regular application. */
6142 verdefs = info->version_info;
5a580b3a 6143
902e9fc7
MR
6144 /* Skip anonymous version tag. */
6145 if (verdefs != NULL && verdefs->vernum == 0)
6146 verdefs = verdefs->next;
5a580b3a 6147
902e9fc7
MR
6148 if (verdefs == NULL && !info->create_default_symver)
6149 s->flags |= SEC_EXCLUDE;
6150 else
5a580b3a 6151 {
902e9fc7
MR
6152 unsigned int cdefs;
6153 bfd_size_type size;
6154 bfd_byte *p;
6155 Elf_Internal_Verdef def;
6156 Elf_Internal_Verdaux defaux;
6157 struct bfd_link_hash_entry *bh;
6158 struct elf_link_hash_entry *h;
6159 const char *name;
5a580b3a 6160
902e9fc7
MR
6161 cdefs = 0;
6162 size = 0;
5a580b3a 6163
902e9fc7
MR
6164 /* Make space for the base version. */
6165 size += sizeof (Elf_External_Verdef);
6166 size += sizeof (Elf_External_Verdaux);
6167 ++cdefs;
6168
6169 /* Make space for the default version. */
6170 if (info->create_default_symver)
6171 {
6172 size += sizeof (Elf_External_Verdef);
6173 ++cdefs;
3e3b46e5
PB
6174 }
6175
5a580b3a
AM
6176 for (t = verdefs; t != NULL; t = t->next)
6177 {
6178 struct bfd_elf_version_deps *n;
6179
a6cc6b3b
RO
6180 /* Don't emit base version twice. */
6181 if (t->vernum == 0)
6182 continue;
6183
5a580b3a
AM
6184 size += sizeof (Elf_External_Verdef);
6185 size += sizeof (Elf_External_Verdaux);
6186 ++cdefs;
6187
6188 for (n = t->deps; n != NULL; n = n->next)
6189 size += sizeof (Elf_External_Verdaux);
6190 }
6191
eea6121a 6192 s->size = size;
a50b1753 6193 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6194 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6195 return FALSE;
6196
6197 /* Fill in the version definition section. */
6198
6199 p = s->contents;
6200
6201 def.vd_version = VER_DEF_CURRENT;
6202 def.vd_flags = VER_FLG_BASE;
6203 def.vd_ndx = 1;
6204 def.vd_cnt = 1;
3e3b46e5
PB
6205 if (info->create_default_symver)
6206 {
6207 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6208 def.vd_next = sizeof (Elf_External_Verdef);
6209 }
6210 else
6211 {
6212 def.vd_aux = sizeof (Elf_External_Verdef);
6213 def.vd_next = (sizeof (Elf_External_Verdef)
6214 + sizeof (Elf_External_Verdaux));
6215 }
5a580b3a 6216
ef53be89 6217 if (soname_indx != (size_t) -1)
5a580b3a
AM
6218 {
6219 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6220 soname_indx);
6221 def.vd_hash = bfd_elf_hash (soname);
6222 defaux.vda_name = soname_indx;
3e3b46e5 6223 name = soname;
5a580b3a
AM
6224 }
6225 else
6226 {
ef53be89 6227 size_t indx;
5a580b3a 6228
06084812 6229 name = lbasename (output_bfd->filename);
5a580b3a
AM
6230 def.vd_hash = bfd_elf_hash (name);
6231 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6232 name, FALSE);
ef53be89 6233 if (indx == (size_t) -1)
5a580b3a
AM
6234 return FALSE;
6235 defaux.vda_name = indx;
6236 }
6237 defaux.vda_next = 0;
6238
6239 _bfd_elf_swap_verdef_out (output_bfd, &def,
6240 (Elf_External_Verdef *) p);
6241 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6242 if (info->create_default_symver)
6243 {
6244 /* Add a symbol representing this version. */
6245 bh = NULL;
6246 if (! (_bfd_generic_link_add_one_symbol
6247 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6248 0, NULL, FALSE,
6249 get_elf_backend_data (dynobj)->collect, &bh)))
6250 return FALSE;
6251 h = (struct elf_link_hash_entry *) bh;
6252 h->non_elf = 0;
6253 h->def_regular = 1;
6254 h->type = STT_OBJECT;
6255 h->verinfo.vertree = NULL;
6256
6257 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6258 return FALSE;
6259
6260 /* Create a duplicate of the base version with the same
6261 aux block, but different flags. */
6262 def.vd_flags = 0;
6263 def.vd_ndx = 2;
6264 def.vd_aux = sizeof (Elf_External_Verdef);
6265 if (verdefs)
6266 def.vd_next = (sizeof (Elf_External_Verdef)
6267 + sizeof (Elf_External_Verdaux));
6268 else
6269 def.vd_next = 0;
6270 _bfd_elf_swap_verdef_out (output_bfd, &def,
6271 (Elf_External_Verdef *) p);
6272 p += sizeof (Elf_External_Verdef);
6273 }
5a580b3a
AM
6274 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6275 (Elf_External_Verdaux *) p);
6276 p += sizeof (Elf_External_Verdaux);
6277
6278 for (t = verdefs; t != NULL; t = t->next)
6279 {
6280 unsigned int cdeps;
6281 struct bfd_elf_version_deps *n;
5a580b3a 6282
a6cc6b3b
RO
6283 /* Don't emit the base version twice. */
6284 if (t->vernum == 0)
6285 continue;
6286
5a580b3a
AM
6287 cdeps = 0;
6288 for (n = t->deps; n != NULL; n = n->next)
6289 ++cdeps;
6290
6291 /* Add a symbol representing this version. */
6292 bh = NULL;
6293 if (! (_bfd_generic_link_add_one_symbol
6294 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6295 0, NULL, FALSE,
6296 get_elf_backend_data (dynobj)->collect, &bh)))
6297 return FALSE;
6298 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6299 h->non_elf = 0;
6300 h->def_regular = 1;
5a580b3a
AM
6301 h->type = STT_OBJECT;
6302 h->verinfo.vertree = t;
6303
c152c796 6304 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6305 return FALSE;
6306
6307 def.vd_version = VER_DEF_CURRENT;
6308 def.vd_flags = 0;
6309 if (t->globals.list == NULL
6310 && t->locals.list == NULL
6311 && ! t->used)
6312 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6313 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6314 def.vd_cnt = cdeps + 1;
6315 def.vd_hash = bfd_elf_hash (t->name);
6316 def.vd_aux = sizeof (Elf_External_Verdef);
6317 def.vd_next = 0;
a6cc6b3b
RO
6318
6319 /* If a basever node is next, it *must* be the last node in
6320 the chain, otherwise Verdef construction breaks. */
6321 if (t->next != NULL && t->next->vernum == 0)
6322 BFD_ASSERT (t->next->next == NULL);
6323
6324 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6325 def.vd_next = (sizeof (Elf_External_Verdef)
6326 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6327
6328 _bfd_elf_swap_verdef_out (output_bfd, &def,
6329 (Elf_External_Verdef *) p);
6330 p += sizeof (Elf_External_Verdef);
6331
6332 defaux.vda_name = h->dynstr_index;
6333 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6334 h->dynstr_index);
6335 defaux.vda_next = 0;
6336 if (t->deps != NULL)
6337 defaux.vda_next = sizeof (Elf_External_Verdaux);
6338 t->name_indx = defaux.vda_name;
6339
6340 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6341 (Elf_External_Verdaux *) p);
6342 p += sizeof (Elf_External_Verdaux);
6343
6344 for (n = t->deps; n != NULL; n = n->next)
6345 {
6346 if (n->version_needed == NULL)
6347 {
6348 /* This can happen if there was an error in the
6349 version script. */
6350 defaux.vda_name = 0;
6351 }
6352 else
6353 {
6354 defaux.vda_name = n->version_needed->name_indx;
6355 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6356 defaux.vda_name);
6357 }
6358 if (n->next == NULL)
6359 defaux.vda_next = 0;
6360 else
6361 defaux.vda_next = sizeof (Elf_External_Verdaux);
6362
6363 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6364 (Elf_External_Verdaux *) p);
6365 p += sizeof (Elf_External_Verdaux);
6366 }
6367 }
6368
5a580b3a
AM
6369 elf_tdata (output_bfd)->cverdefs = cdefs;
6370 }
902e9fc7
MR
6371 }
6372
6373 bed = get_elf_backend_data (output_bfd);
6374
6375 if (info->gc_sections && bed->can_gc_sections)
6376 {
6377 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6378
6379 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6380 dynamic symbol table. */
902e9fc7
MR
6381 sweep_info.info = info;
6382 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6383 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6384 &sweep_info);
3d13f3e9
AM
6385 }
6386
6387 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6388 {
6389 asection *s;
6390 struct elf_find_verdep_info sinfo;
6391
6392 /* Work out the size of the version reference section. */
6393
6394 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6395 BFD_ASSERT (s != NULL);
902e9fc7 6396
3d13f3e9
AM
6397 sinfo.info = info;
6398 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6399 if (sinfo.vers == 0)
6400 sinfo.vers = 1;
6401 sinfo.failed = FALSE;
6402
6403 elf_link_hash_traverse (elf_hash_table (info),
6404 _bfd_elf_link_find_version_dependencies,
6405 &sinfo);
6406 if (sinfo.failed)
6407 return FALSE;
6408
6409 if (elf_tdata (output_bfd)->verref == NULL)
6410 s->flags |= SEC_EXCLUDE;
6411 else
6412 {
6413 Elf_Internal_Verneed *vn;
6414 unsigned int size;
6415 unsigned int crefs;
6416 bfd_byte *p;
6417
6418 /* Build the version dependency section. */
6419 size = 0;
6420 crefs = 0;
6421 for (vn = elf_tdata (output_bfd)->verref;
6422 vn != NULL;
6423 vn = vn->vn_nextref)
6424 {
6425 Elf_Internal_Vernaux *a;
6426
6427 size += sizeof (Elf_External_Verneed);
6428 ++crefs;
6429 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6430 size += sizeof (Elf_External_Vernaux);
6431 }
6432
6433 s->size = size;
6434 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6435 if (s->contents == NULL)
6436 return FALSE;
6437
6438 p = s->contents;
6439 for (vn = elf_tdata (output_bfd)->verref;
6440 vn != NULL;
6441 vn = vn->vn_nextref)
6442 {
6443 unsigned int caux;
6444 Elf_Internal_Vernaux *a;
6445 size_t indx;
6446
6447 caux = 0;
6448 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6449 ++caux;
6450
6451 vn->vn_version = VER_NEED_CURRENT;
6452 vn->vn_cnt = caux;
6453 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6454 elf_dt_name (vn->vn_bfd) != NULL
6455 ? elf_dt_name (vn->vn_bfd)
6456 : lbasename (vn->vn_bfd->filename),
6457 FALSE);
6458 if (indx == (size_t) -1)
6459 return FALSE;
6460 vn->vn_file = indx;
6461 vn->vn_aux = sizeof (Elf_External_Verneed);
6462 if (vn->vn_nextref == NULL)
6463 vn->vn_next = 0;
6464 else
6465 vn->vn_next = (sizeof (Elf_External_Verneed)
6466 + caux * sizeof (Elf_External_Vernaux));
6467
6468 _bfd_elf_swap_verneed_out (output_bfd, vn,
6469 (Elf_External_Verneed *) p);
6470 p += sizeof (Elf_External_Verneed);
6471
6472 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6473 {
6474 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6475 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6476 a->vna_nodename, FALSE);
6477 if (indx == (size_t) -1)
6478 return FALSE;
6479 a->vna_name = indx;
6480 if (a->vna_nextptr == NULL)
6481 a->vna_next = 0;
6482 else
6483 a->vna_next = sizeof (Elf_External_Vernaux);
6484
6485 _bfd_elf_swap_vernaux_out (output_bfd, a,
6486 (Elf_External_Vernaux *) p);
6487 p += sizeof (Elf_External_Vernaux);
6488 }
6489 }
6490
6491 elf_tdata (output_bfd)->cverrefs = crefs;
6492 }
902e9fc7
MR
6493 }
6494
6495 /* Any syms created from now on start with -1 in
6496 got.refcount/offset and plt.refcount/offset. */
6497 elf_hash_table (info)->init_got_refcount
6498 = elf_hash_table (info)->init_got_offset;
6499 elf_hash_table (info)->init_plt_refcount
6500 = elf_hash_table (info)->init_plt_offset;
6501
6502 if (bfd_link_relocatable (info)
6503 && !_bfd_elf_size_group_sections (info))
6504 return FALSE;
6505
6506 /* The backend may have to create some sections regardless of whether
6507 we're dynamic or not. */
6508 if (bed->elf_backend_always_size_sections
6509 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6510 return FALSE;
6511
6512 /* Determine any GNU_STACK segment requirements, after the backend
6513 has had a chance to set a default segment size. */
6514 if (info->execstack)
6515 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6516 else if (info->noexecstack)
6517 elf_stack_flags (output_bfd) = PF_R | PF_W;
6518 else
6519 {
6520 bfd *inputobj;
6521 asection *notesec = NULL;
6522 int exec = 0;
6523
6524 for (inputobj = info->input_bfds;
6525 inputobj;
6526 inputobj = inputobj->link.next)
6527 {
6528 asection *s;
6529
6530 if (inputobj->flags
6531 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6532 continue;
57963c05
AM
6533 s = inputobj->sections;
6534 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6535 continue;
6536
902e9fc7
MR
6537 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6538 if (s)
6539 {
6540 if (s->flags & SEC_CODE)
6541 exec = PF_X;
6542 notesec = s;
6543 }
6544 else if (bed->default_execstack)
6545 exec = PF_X;
6546 }
6547 if (notesec || info->stacksize > 0)
6548 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6549 if (notesec && exec && bfd_link_relocatable (info)
6550 && notesec->output_section != bfd_abs_section_ptr)
6551 notesec->output_section->flags |= SEC_CODE;
6552 }
6553
6554 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6555 {
6556 struct elf_info_failed eif;
6557 struct elf_link_hash_entry *h;
6558 asection *dynstr;
6559 asection *s;
6560
6561 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6562 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6563
902e9fc7
MR
6564 if (info->symbolic)
6565 {
6566 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6567 return FALSE;
6568 info->flags |= DF_SYMBOLIC;
6569 }
6570
6571 if (rpath != NULL)
6572 {
6573 size_t indx;
6574 bfd_vma tag;
6575
6576 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6577 TRUE);
6578 if (indx == (size_t) -1)
6579 return FALSE;
6580
6581 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6582 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6583 return FALSE;
6584 }
6585
6586 if (filter_shlib != NULL)
6587 {
6588 size_t indx;
6589
6590 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6591 filter_shlib, TRUE);
6592 if (indx == (size_t) -1
6593 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6594 return FALSE;
6595 }
6596
6597 if (auxiliary_filters != NULL)
6598 {
6599 const char * const *p;
6600
6601 for (p = auxiliary_filters; *p != NULL; p++)
6602 {
6603 size_t indx;
6604
6605 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6606 *p, TRUE);
6607 if (indx == (size_t) -1
6608 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6609 return FALSE;
6610 }
6611 }
6612
6613 if (audit != NULL)
6614 {
6615 size_t indx;
6616
6617 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6618 TRUE);
6619 if (indx == (size_t) -1
6620 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6621 return FALSE;
6622 }
6623
6624 if (depaudit != NULL)
6625 {
6626 size_t indx;
6627
6628 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6629 TRUE);
6630 if (indx == (size_t) -1
6631 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6632 return FALSE;
6633 }
6634
6635 eif.info = info;
6636 eif.failed = FALSE;
6637
6638 /* Find all symbols which were defined in a dynamic object and make
6639 the backend pick a reasonable value for them. */
6640 elf_link_hash_traverse (elf_hash_table (info),
6641 _bfd_elf_adjust_dynamic_symbol,
6642 &eif);
6643 if (eif.failed)
6644 return FALSE;
6645
6646 /* Add some entries to the .dynamic section. We fill in some of the
6647 values later, in bfd_elf_final_link, but we must add the entries
6648 now so that we know the final size of the .dynamic section. */
6649
6650 /* If there are initialization and/or finalization functions to
6651 call then add the corresponding DT_INIT/DT_FINI entries. */
6652 h = (info->init_function
6653 ? elf_link_hash_lookup (elf_hash_table (info),
6654 info->init_function, FALSE,
6655 FALSE, FALSE)
6656 : NULL);
6657 if (h != NULL
6658 && (h->ref_regular
6659 || h->def_regular))
6660 {
6661 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6662 return FALSE;
6663 }
6664 h = (info->fini_function
6665 ? elf_link_hash_lookup (elf_hash_table (info),
6666 info->fini_function, FALSE,
6667 FALSE, FALSE)
6668 : NULL);
6669 if (h != NULL
6670 && (h->ref_regular
6671 || h->def_regular))
6672 {
6673 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6674 return FALSE;
6675 }
6676
6677 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6678 if (s != NULL && s->linker_has_input)
6679 {
6680 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6681 if (! bfd_link_executable (info))
6682 {
6683 bfd *sub;
6684 asection *o;
6685
57963c05
AM
6686 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6687 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6688 && (o = sub->sections) != NULL
6689 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6690 for (o = sub->sections; o != NULL; o = o->next)
6691 if (elf_section_data (o)->this_hdr.sh_type
6692 == SHT_PREINIT_ARRAY)
6693 {
6694 _bfd_error_handler
6695 (_("%B: .preinit_array section is not allowed in DSO"),
6696 sub);
6697 break;
6698 }
6699
6700 bfd_set_error (bfd_error_nonrepresentable_section);
6701 return FALSE;
6702 }
6703
6704 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6705 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6706 return FALSE;
6707 }
6708 s = bfd_get_section_by_name (output_bfd, ".init_array");
6709 if (s != NULL && s->linker_has_input)
6710 {
6711 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6712 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6713 return FALSE;
6714 }
6715 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6716 if (s != NULL && s->linker_has_input)
6717 {
6718 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6719 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6720 return FALSE;
6721 }
6722
6723 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6724 /* If .dynstr is excluded from the link, we don't want any of
6725 these tags. Strictly, we should be checking each section
6726 individually; This quick check covers for the case where
6727 someone does a /DISCARD/ : { *(*) }. */
6728 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6729 {
6730 bfd_size_type strsize;
6731
6732 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6733 if ((info->emit_hash
6734 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6735 || (info->emit_gnu_hash
6736 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6737 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6738 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6739 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6740 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6741 bed->s->sizeof_sym))
6742 return FALSE;
6743 }
6744 }
6745
6746 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6747 return FALSE;
6748
6749 /* The backend must work out the sizes of all the other dynamic
6750 sections. */
6751 if (dynobj != NULL
6752 && bed->elf_backend_size_dynamic_sections != NULL
6753 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6754 return FALSE;
6755
6756 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6757 {
6758 unsigned long section_sym_count;
6759
6760 if (elf_tdata (output_bfd)->cverdefs)
6761 {
6762 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6763
6764 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6765 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6766 return FALSE;
6767 }
6768
6769 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6770 {
6771 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6772 return FALSE;
6773 }
6774 else if (info->flags & DF_BIND_NOW)
6775 {
6776 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6777 return FALSE;
6778 }
6779
6780 if (info->flags_1)
6781 {
6782 if (bfd_link_executable (info))
6783 info->flags_1 &= ~ (DF_1_INITFIRST
6784 | DF_1_NODELETE
6785 | DF_1_NOOPEN);
6786 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6787 return FALSE;
6788 }
6789
6790 if (elf_tdata (output_bfd)->cverrefs)
6791 {
6792 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6793
6794 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6795 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6796 return FALSE;
6797 }
5a580b3a 6798
8423293d
AM
6799 if ((elf_tdata (output_bfd)->cverrefs == 0
6800 && elf_tdata (output_bfd)->cverdefs == 0)
6801 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
3d13f3e9 6802 &section_sym_count) <= 1)
8423293d 6803 {
902e9fc7
MR
6804 asection *s;
6805
3d4d4302 6806 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6807 s->flags |= SEC_EXCLUDE;
6808 }
6809 }
6810 return TRUE;
6811}
6812
74541ad4
AM
6813/* Find the first non-excluded output section. We'll use its
6814 section symbol for some emitted relocs. */
6815void
6816_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6817{
6818 asection *s;
6819
6820 for (s = output_bfd->sections; s != NULL; s = s->next)
6821 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6822 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6823 {
6824 elf_hash_table (info)->text_index_section = s;
6825 break;
6826 }
6827}
6828
6829/* Find two non-excluded output sections, one for code, one for data.
6830 We'll use their section symbols for some emitted relocs. */
6831void
6832_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6833{
6834 asection *s;
6835
266b05cf
DJ
6836 /* Data first, since setting text_index_section changes
6837 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6838 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6839 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6840 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6841 {
266b05cf 6842 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6843 break;
6844 }
6845
6846 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6847 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6848 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6849 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6850 {
266b05cf 6851 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6852 break;
6853 }
6854
6855 if (elf_hash_table (info)->text_index_section == NULL)
6856 elf_hash_table (info)->text_index_section
6857 = elf_hash_table (info)->data_index_section;
6858}
6859
8423293d
AM
6860bfd_boolean
6861bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6862{
74541ad4 6863 const struct elf_backend_data *bed;
23ec1e32 6864 unsigned long section_sym_count;
96d01d93 6865 bfd_size_type dynsymcount = 0;
74541ad4 6866
8423293d
AM
6867 if (!is_elf_hash_table (info->hash))
6868 return TRUE;
6869
74541ad4
AM
6870 bed = get_elf_backend_data (output_bfd);
6871 (*bed->elf_backend_init_index_section) (output_bfd, info);
6872
23ec1e32
MR
6873 /* Assign dynsym indices. In a shared library we generate a section
6874 symbol for each output section, which come first. Next come all
6875 of the back-end allocated local dynamic syms, followed by the rest
6876 of the global symbols.
6877
6878 This is usually not needed for static binaries, however backends
6879 can request to always do it, e.g. the MIPS backend uses dynamic
6880 symbol counts to lay out GOT, which will be produced in the
6881 presence of GOT relocations even in static binaries (holding fixed
6882 data in that case, to satisfy those relocations). */
6883
6884 if (elf_hash_table (info)->dynamic_sections_created
6885 || bed->always_renumber_dynsyms)
6886 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6887 &section_sym_count);
6888
8423293d
AM
6889 if (elf_hash_table (info)->dynamic_sections_created)
6890 {
6891 bfd *dynobj;
8423293d 6892 asection *s;
8423293d
AM
6893 unsigned int dtagcount;
6894
6895 dynobj = elf_hash_table (info)->dynobj;
6896
5a580b3a 6897 /* Work out the size of the symbol version section. */
3d4d4302 6898 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6899 BFD_ASSERT (s != NULL);
d5486c43 6900 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6901 {
eea6121a 6902 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6903 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6904 if (s->contents == NULL)
6905 return FALSE;
6906
6907 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6908 return FALSE;
6909 }
6910
6911 /* Set the size of the .dynsym and .hash sections. We counted
6912 the number of dynamic symbols in elf_link_add_object_symbols.
6913 We will build the contents of .dynsym and .hash when we build
6914 the final symbol table, because until then we do not know the
6915 correct value to give the symbols. We built the .dynstr
6916 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6917 s = elf_hash_table (info)->dynsym;
5a580b3a 6918 BFD_ASSERT (s != NULL);
eea6121a 6919 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6920
d5486c43
L
6921 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6922 if (s->contents == NULL)
6923 return FALSE;
5a580b3a 6924
d5486c43
L
6925 /* The first entry in .dynsym is a dummy symbol. Clear all the
6926 section syms, in case we don't output them all. */
6927 ++section_sym_count;
6928 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6929
fdc90cb4
JJ
6930 elf_hash_table (info)->bucketcount = 0;
6931
5a580b3a
AM
6932 /* Compute the size of the hashing table. As a side effect this
6933 computes the hash values for all the names we export. */
fdc90cb4
JJ
6934 if (info->emit_hash)
6935 {
6936 unsigned long int *hashcodes;
14b1c01e 6937 struct hash_codes_info hashinf;
fdc90cb4
JJ
6938 bfd_size_type amt;
6939 unsigned long int nsyms;
6940 size_t bucketcount;
6941 size_t hash_entry_size;
6942
6943 /* Compute the hash values for all exported symbols. At the same
6944 time store the values in an array so that we could use them for
6945 optimizations. */
6946 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6947 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6948 if (hashcodes == NULL)
6949 return FALSE;
14b1c01e
AM
6950 hashinf.hashcodes = hashcodes;
6951 hashinf.error = FALSE;
5a580b3a 6952
fdc90cb4
JJ
6953 /* Put all hash values in HASHCODES. */
6954 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6955 elf_collect_hash_codes, &hashinf);
6956 if (hashinf.error)
4dd07732
AM
6957 {
6958 free (hashcodes);
6959 return FALSE;
6960 }
5a580b3a 6961
14b1c01e 6962 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6963 bucketcount
6964 = compute_bucket_count (info, hashcodes, nsyms, 0);
6965 free (hashcodes);
6966
4b48e2f6 6967 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 6968 return FALSE;
5a580b3a 6969
fdc90cb4
JJ
6970 elf_hash_table (info)->bucketcount = bucketcount;
6971
3d4d4302 6972 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6973 BFD_ASSERT (s != NULL);
6974 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6975 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6976 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6977 if (s->contents == NULL)
6978 return FALSE;
6979
6980 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6981 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6982 s->contents + hash_entry_size);
6983 }
6984
6985 if (info->emit_gnu_hash)
6986 {
6987 size_t i, cnt;
6988 unsigned char *contents;
6989 struct collect_gnu_hash_codes cinfo;
6990 bfd_size_type amt;
6991 size_t bucketcount;
6992
6993 memset (&cinfo, 0, sizeof (cinfo));
6994
6995 /* Compute the hash values for all exported symbols. At the same
6996 time store the values in an array so that we could use them for
6997 optimizations. */
6998 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6999 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7000 if (cinfo.hashcodes == NULL)
7001 return FALSE;
7002
7003 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7004 cinfo.min_dynindx = -1;
7005 cinfo.output_bfd = output_bfd;
7006 cinfo.bed = bed;
7007
7008 /* Put all hash values in HASHCODES. */
7009 elf_link_hash_traverse (elf_hash_table (info),
7010 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7011 if (cinfo.error)
4dd07732
AM
7012 {
7013 free (cinfo.hashcodes);
7014 return FALSE;
7015 }
fdc90cb4
JJ
7016
7017 bucketcount
7018 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7019
7020 if (bucketcount == 0)
7021 {
7022 free (cinfo.hashcodes);
7023 return FALSE;
7024 }
7025
3d4d4302 7026 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7027 BFD_ASSERT (s != NULL);
7028
7029 if (cinfo.nsyms == 0)
7030 {
7031 /* Empty .gnu.hash section is special. */
7032 BFD_ASSERT (cinfo.min_dynindx == -1);
7033 free (cinfo.hashcodes);
7034 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7035 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7036 if (contents == NULL)
7037 return FALSE;
7038 s->contents = contents;
7039 /* 1 empty bucket. */
7040 bfd_put_32 (output_bfd, 1, contents);
7041 /* SYMIDX above the special symbol 0. */
7042 bfd_put_32 (output_bfd, 1, contents + 4);
7043 /* Just one word for bitmask. */
7044 bfd_put_32 (output_bfd, 1, contents + 8);
7045 /* Only hash fn bloom filter. */
7046 bfd_put_32 (output_bfd, 0, contents + 12);
7047 /* No hashes are valid - empty bitmask. */
7048 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7049 /* No hashes in the only bucket. */
7050 bfd_put_32 (output_bfd, 0,
7051 contents + 16 + bed->s->arch_size / 8);
7052 }
7053 else
7054 {
9e6619e2 7055 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7056 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7057
9e6619e2
AM
7058 x = cinfo.nsyms;
7059 maskbitslog2 = 1;
7060 while ((x >>= 1) != 0)
7061 ++maskbitslog2;
fdc90cb4
JJ
7062 if (maskbitslog2 < 3)
7063 maskbitslog2 = 5;
7064 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7065 maskbitslog2 = maskbitslog2 + 3;
7066 else
7067 maskbitslog2 = maskbitslog2 + 2;
7068 if (bed->s->arch_size == 64)
7069 {
7070 if (maskbitslog2 == 5)
7071 maskbitslog2 = 6;
7072 cinfo.shift1 = 6;
7073 }
7074 else
7075 cinfo.shift1 = 5;
7076 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7077 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7078 cinfo.maskbits = 1 << maskbitslog2;
7079 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7080 amt = bucketcount * sizeof (unsigned long int) * 2;
7081 amt += maskwords * sizeof (bfd_vma);
a50b1753 7082 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7083 if (cinfo.bitmask == NULL)
7084 {
7085 free (cinfo.hashcodes);
7086 return FALSE;
7087 }
7088
a50b1753 7089 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7090 cinfo.indx = cinfo.counts + bucketcount;
7091 cinfo.symindx = dynsymcount - cinfo.nsyms;
7092 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7093
7094 /* Determine how often each hash bucket is used. */
7095 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7096 for (i = 0; i < cinfo.nsyms; ++i)
7097 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7098
7099 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7100 if (cinfo.counts[i] != 0)
7101 {
7102 cinfo.indx[i] = cnt;
7103 cnt += cinfo.counts[i];
7104 }
7105 BFD_ASSERT (cnt == dynsymcount);
7106 cinfo.bucketcount = bucketcount;
7107 cinfo.local_indx = cinfo.min_dynindx;
7108
7109 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7110 s->size += cinfo.maskbits / 8;
a50b1753 7111 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7112 if (contents == NULL)
7113 {
7114 free (cinfo.bitmask);
7115 free (cinfo.hashcodes);
7116 return FALSE;
7117 }
7118
7119 s->contents = contents;
7120 bfd_put_32 (output_bfd, bucketcount, contents);
7121 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7122 bfd_put_32 (output_bfd, maskwords, contents + 8);
7123 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7124 contents += 16 + cinfo.maskbits / 8;
7125
7126 for (i = 0; i < bucketcount; ++i)
7127 {
7128 if (cinfo.counts[i] == 0)
7129 bfd_put_32 (output_bfd, 0, contents);
7130 else
7131 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7132 contents += 4;
7133 }
7134
7135 cinfo.contents = contents;
7136
7137 /* Renumber dynamic symbols, populate .gnu.hash section. */
7138 elf_link_hash_traverse (elf_hash_table (info),
7139 elf_renumber_gnu_hash_syms, &cinfo);
7140
7141 contents = s->contents + 16;
7142 for (i = 0; i < maskwords; ++i)
7143 {
7144 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7145 contents);
7146 contents += bed->s->arch_size / 8;
7147 }
7148
7149 free (cinfo.bitmask);
7150 free (cinfo.hashcodes);
7151 }
7152 }
5a580b3a 7153
3d4d4302 7154 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7155 BFD_ASSERT (s != NULL);
7156
4ad4eba5 7157 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7158
eea6121a 7159 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7160
7161 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7163 return FALSE;
7164 }
7165
7166 return TRUE;
7167}
4d269e42 7168\f
4d269e42
AM
7169/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7170
7171static void
7172merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7173 asection *sec)
7174{
dbaa2011
AM
7175 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7176 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7177}
7178
7179/* Finish SHF_MERGE section merging. */
7180
7181bfd_boolean
630993ec 7182_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7183{
7184 bfd *ibfd;
7185 asection *sec;
7186
7187 if (!is_elf_hash_table (info->hash))
7188 return FALSE;
7189
c72f2fb2 7190 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7191 if ((ibfd->flags & DYNAMIC) == 0
7192 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7193 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7194 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7195 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7196 if ((sec->flags & SEC_MERGE) != 0
7197 && !bfd_is_abs_section (sec->output_section))
7198 {
7199 struct bfd_elf_section_data *secdata;
7200
7201 secdata = elf_section_data (sec);
630993ec 7202 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7203 &elf_hash_table (info)->merge_info,
7204 sec, &secdata->sec_info))
7205 return FALSE;
7206 else if (secdata->sec_info)
dbaa2011 7207 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7208 }
7209
7210 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7211 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7212 merge_sections_remove_hook);
7213 return TRUE;
7214}
7215
7216/* Create an entry in an ELF linker hash table. */
7217
7218struct bfd_hash_entry *
7219_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7220 struct bfd_hash_table *table,
7221 const char *string)
7222{
7223 /* Allocate the structure if it has not already been allocated by a
7224 subclass. */
7225 if (entry == NULL)
7226 {
a50b1753 7227 entry = (struct bfd_hash_entry *)
ca4be51c 7228 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7229 if (entry == NULL)
7230 return entry;
7231 }
7232
7233 /* Call the allocation method of the superclass. */
7234 entry = _bfd_link_hash_newfunc (entry, table, string);
7235 if (entry != NULL)
7236 {
7237 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7238 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7239
7240 /* Set local fields. */
7241 ret->indx = -1;
7242 ret->dynindx = -1;
7243 ret->got = htab->init_got_refcount;
7244 ret->plt = htab->init_plt_refcount;
7245 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7246 - offsetof (struct elf_link_hash_entry, size)));
7247 /* Assume that we have been called by a non-ELF symbol reader.
7248 This flag is then reset by the code which reads an ELF input
7249 file. This ensures that a symbol created by a non-ELF symbol
7250 reader will have the flag set correctly. */
7251 ret->non_elf = 1;
7252 }
7253
7254 return entry;
7255}
7256
7257/* Copy data from an indirect symbol to its direct symbol, hiding the
7258 old indirect symbol. Also used for copying flags to a weakdef. */
7259
7260void
7261_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7262 struct elf_link_hash_entry *dir,
7263 struct elf_link_hash_entry *ind)
7264{
7265 struct elf_link_hash_table *htab;
7266
7267 /* Copy down any references that we may have already seen to the
e81830c5 7268 symbol which just became indirect. */
4d269e42 7269
422f1182 7270 if (dir->versioned != versioned_hidden)
e81830c5
AM
7271 dir->ref_dynamic |= ind->ref_dynamic;
7272 dir->ref_regular |= ind->ref_regular;
7273 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7274 dir->non_got_ref |= ind->non_got_ref;
7275 dir->needs_plt |= ind->needs_plt;
7276 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7277
7278 if (ind->root.type != bfd_link_hash_indirect)
7279 return;
7280
7281 /* Copy over the global and procedure linkage table refcount entries.
7282 These may have been already set up by a check_relocs routine. */
7283 htab = elf_hash_table (info);
7284 if (ind->got.refcount > htab->init_got_refcount.refcount)
7285 {
7286 if (dir->got.refcount < 0)
7287 dir->got.refcount = 0;
7288 dir->got.refcount += ind->got.refcount;
7289 ind->got.refcount = htab->init_got_refcount.refcount;
7290 }
7291
7292 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7293 {
7294 if (dir->plt.refcount < 0)
7295 dir->plt.refcount = 0;
7296 dir->plt.refcount += ind->plt.refcount;
7297 ind->plt.refcount = htab->init_plt_refcount.refcount;
7298 }
7299
7300 if (ind->dynindx != -1)
7301 {
7302 if (dir->dynindx != -1)
7303 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7304 dir->dynindx = ind->dynindx;
7305 dir->dynstr_index = ind->dynstr_index;
7306 ind->dynindx = -1;
7307 ind->dynstr_index = 0;
7308 }
7309}
7310
7311void
7312_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7313 struct elf_link_hash_entry *h,
7314 bfd_boolean force_local)
7315{
3aa14d16
L
7316 /* STT_GNU_IFUNC symbol must go through PLT. */
7317 if (h->type != STT_GNU_IFUNC)
7318 {
7319 h->plt = elf_hash_table (info)->init_plt_offset;
7320 h->needs_plt = 0;
7321 }
4d269e42
AM
7322 if (force_local)
7323 {
7324 h->forced_local = 1;
7325 if (h->dynindx != -1)
7326 {
4d269e42
AM
7327 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7328 h->dynstr_index);
641338d8
AM
7329 h->dynindx = -1;
7330 h->dynstr_index = 0;
4d269e42
AM
7331 }
7332 }
7333}
7334
7bf52ea2
AM
7335/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7336 caller. */
4d269e42
AM
7337
7338bfd_boolean
7339_bfd_elf_link_hash_table_init
7340 (struct elf_link_hash_table *table,
7341 bfd *abfd,
7342 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7343 struct bfd_hash_table *,
7344 const char *),
4dfe6ac6
NC
7345 unsigned int entsize,
7346 enum elf_target_id target_id)
4d269e42
AM
7347{
7348 bfd_boolean ret;
7349 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7350
4d269e42
AM
7351 table->init_got_refcount.refcount = can_refcount - 1;
7352 table->init_plt_refcount.refcount = can_refcount - 1;
7353 table->init_got_offset.offset = -(bfd_vma) 1;
7354 table->init_plt_offset.offset = -(bfd_vma) 1;
7355 /* The first dynamic symbol is a dummy. */
7356 table->dynsymcount = 1;
7357
7358 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7359
4d269e42 7360 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7361 table->hash_table_id = target_id;
4d269e42
AM
7362
7363 return ret;
7364}
7365
7366/* Create an ELF linker hash table. */
7367
7368struct bfd_link_hash_table *
7369_bfd_elf_link_hash_table_create (bfd *abfd)
7370{
7371 struct elf_link_hash_table *ret;
7372 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7373
7bf52ea2 7374 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7375 if (ret == NULL)
7376 return NULL;
7377
7378 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7379 sizeof (struct elf_link_hash_entry),
7380 GENERIC_ELF_DATA))
4d269e42
AM
7381 {
7382 free (ret);
7383 return NULL;
7384 }
d495ab0d 7385 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7386
7387 return &ret->root;
7388}
7389
9f7c3e5e
AM
7390/* Destroy an ELF linker hash table. */
7391
7392void
d495ab0d 7393_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7394{
d495ab0d
AM
7395 struct elf_link_hash_table *htab;
7396
7397 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7398 if (htab->dynstr != NULL)
7399 _bfd_elf_strtab_free (htab->dynstr);
7400 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7401 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7402}
7403
4d269e42
AM
7404/* This is a hook for the ELF emulation code in the generic linker to
7405 tell the backend linker what file name to use for the DT_NEEDED
7406 entry for a dynamic object. */
7407
7408void
7409bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7410{
7411 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7412 && bfd_get_format (abfd) == bfd_object)
7413 elf_dt_name (abfd) = name;
7414}
7415
7416int
7417bfd_elf_get_dyn_lib_class (bfd *abfd)
7418{
7419 int lib_class;
7420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7421 && bfd_get_format (abfd) == bfd_object)
7422 lib_class = elf_dyn_lib_class (abfd);
7423 else
7424 lib_class = 0;
7425 return lib_class;
7426}
7427
7428void
7429bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7430{
7431 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7432 && bfd_get_format (abfd) == bfd_object)
7433 elf_dyn_lib_class (abfd) = lib_class;
7434}
7435
7436/* Get the list of DT_NEEDED entries for a link. This is a hook for
7437 the linker ELF emulation code. */
7438
7439struct bfd_link_needed_list *
7440bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7441 struct bfd_link_info *info)
7442{
7443 if (! is_elf_hash_table (info->hash))
7444 return NULL;
7445 return elf_hash_table (info)->needed;
7446}
7447
7448/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7449 hook for the linker ELF emulation code. */
7450
7451struct bfd_link_needed_list *
7452bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7453 struct bfd_link_info *info)
7454{
7455 if (! is_elf_hash_table (info->hash))
7456 return NULL;
7457 return elf_hash_table (info)->runpath;
7458}
7459
7460/* Get the name actually used for a dynamic object for a link. This
7461 is the SONAME entry if there is one. Otherwise, it is the string
7462 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7463
7464const char *
7465bfd_elf_get_dt_soname (bfd *abfd)
7466{
7467 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7468 && bfd_get_format (abfd) == bfd_object)
7469 return elf_dt_name (abfd);
7470 return NULL;
7471}
7472
7473/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7474 the ELF linker emulation code. */
7475
7476bfd_boolean
7477bfd_elf_get_bfd_needed_list (bfd *abfd,
7478 struct bfd_link_needed_list **pneeded)
7479{
7480 asection *s;
7481 bfd_byte *dynbuf = NULL;
cb33740c 7482 unsigned int elfsec;
4d269e42
AM
7483 unsigned long shlink;
7484 bfd_byte *extdyn, *extdynend;
7485 size_t extdynsize;
7486 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7487
7488 *pneeded = NULL;
7489
7490 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7491 || bfd_get_format (abfd) != bfd_object)
7492 return TRUE;
7493
7494 s = bfd_get_section_by_name (abfd, ".dynamic");
7495 if (s == NULL || s->size == 0)
7496 return TRUE;
7497
7498 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7499 goto error_return;
7500
7501 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7502 if (elfsec == SHN_BAD)
4d269e42
AM
7503 goto error_return;
7504
7505 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7506
4d269e42
AM
7507 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7508 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7509
7510 extdyn = dynbuf;
7511 extdynend = extdyn + s->size;
7512 for (; extdyn < extdynend; extdyn += extdynsize)
7513 {
7514 Elf_Internal_Dyn dyn;
7515
7516 (*swap_dyn_in) (abfd, extdyn, &dyn);
7517
7518 if (dyn.d_tag == DT_NULL)
7519 break;
7520
7521 if (dyn.d_tag == DT_NEEDED)
7522 {
7523 const char *string;
7524 struct bfd_link_needed_list *l;
7525 unsigned int tagv = dyn.d_un.d_val;
7526 bfd_size_type amt;
7527
7528 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7529 if (string == NULL)
7530 goto error_return;
7531
7532 amt = sizeof *l;
a50b1753 7533 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7534 if (l == NULL)
7535 goto error_return;
7536
7537 l->by = abfd;
7538 l->name = string;
7539 l->next = *pneeded;
7540 *pneeded = l;
7541 }
7542 }
7543
7544 free (dynbuf);
7545
7546 return TRUE;
7547
7548 error_return:
7549 if (dynbuf != NULL)
7550 free (dynbuf);
7551 return FALSE;
7552}
7553
7554struct elf_symbuf_symbol
7555{
7556 unsigned long st_name; /* Symbol name, index in string tbl */
7557 unsigned char st_info; /* Type and binding attributes */
7558 unsigned char st_other; /* Visibilty, and target specific */
7559};
7560
7561struct elf_symbuf_head
7562{
7563 struct elf_symbuf_symbol *ssym;
ef53be89 7564 size_t count;
4d269e42
AM
7565 unsigned int st_shndx;
7566};
7567
7568struct elf_symbol
7569{
7570 union
7571 {
7572 Elf_Internal_Sym *isym;
7573 struct elf_symbuf_symbol *ssym;
7574 } u;
7575 const char *name;
7576};
7577
7578/* Sort references to symbols by ascending section number. */
7579
7580static int
7581elf_sort_elf_symbol (const void *arg1, const void *arg2)
7582{
7583 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7584 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7585
7586 return s1->st_shndx - s2->st_shndx;
7587}
7588
7589static int
7590elf_sym_name_compare (const void *arg1, const void *arg2)
7591{
7592 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7593 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7594 return strcmp (s1->name, s2->name);
7595}
7596
7597static struct elf_symbuf_head *
ef53be89 7598elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7599{
14b1c01e 7600 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7601 struct elf_symbuf_symbol *ssym;
7602 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7603 size_t i, shndx_count, total_size;
4d269e42 7604
a50b1753 7605 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7606 if (indbuf == NULL)
7607 return NULL;
7608
7609 for (ind = indbuf, i = 0; i < symcount; i++)
7610 if (isymbuf[i].st_shndx != SHN_UNDEF)
7611 *ind++ = &isymbuf[i];
7612 indbufend = ind;
7613
7614 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7615 elf_sort_elf_symbol);
7616
7617 shndx_count = 0;
7618 if (indbufend > indbuf)
7619 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7620 if (ind[0]->st_shndx != ind[1]->st_shndx)
7621 shndx_count++;
7622
3ae181ee
L
7623 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7624 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7625 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7626 if (ssymbuf == NULL)
7627 {
7628 free (indbuf);
7629 return NULL;
7630 }
7631
3ae181ee 7632 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7633 ssymbuf->ssym = NULL;
7634 ssymbuf->count = shndx_count;
7635 ssymbuf->st_shndx = 0;
7636 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7637 {
7638 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7639 {
7640 ssymhead++;
7641 ssymhead->ssym = ssym;
7642 ssymhead->count = 0;
7643 ssymhead->st_shndx = (*ind)->st_shndx;
7644 }
7645 ssym->st_name = (*ind)->st_name;
7646 ssym->st_info = (*ind)->st_info;
7647 ssym->st_other = (*ind)->st_other;
7648 ssymhead->count++;
7649 }
ef53be89 7650 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7651 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7652 == total_size));
4d269e42
AM
7653
7654 free (indbuf);
7655 return ssymbuf;
7656}
7657
7658/* Check if 2 sections define the same set of local and global
7659 symbols. */
7660
8f317e31 7661static bfd_boolean
4d269e42
AM
7662bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7663 struct bfd_link_info *info)
7664{
7665 bfd *bfd1, *bfd2;
7666 const struct elf_backend_data *bed1, *bed2;
7667 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7668 size_t symcount1, symcount2;
4d269e42
AM
7669 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7670 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7671 Elf_Internal_Sym *isym, *isymend;
7672 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7673 size_t count1, count2, i;
cb33740c 7674 unsigned int shndx1, shndx2;
4d269e42
AM
7675 bfd_boolean result;
7676
7677 bfd1 = sec1->owner;
7678 bfd2 = sec2->owner;
7679
4d269e42
AM
7680 /* Both sections have to be in ELF. */
7681 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7682 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7683 return FALSE;
7684
7685 if (elf_section_type (sec1) != elf_section_type (sec2))
7686 return FALSE;
7687
4d269e42
AM
7688 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7689 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7690 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7691 return FALSE;
7692
7693 bed1 = get_elf_backend_data (bfd1);
7694 bed2 = get_elf_backend_data (bfd2);
7695 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7696 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7697 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7698 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7699
7700 if (symcount1 == 0 || symcount2 == 0)
7701 return FALSE;
7702
7703 result = FALSE;
7704 isymbuf1 = NULL;
7705 isymbuf2 = NULL;
a50b1753
NC
7706 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7707 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7708
7709 if (ssymbuf1 == NULL)
7710 {
7711 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7712 NULL, NULL, NULL);
7713 if (isymbuf1 == NULL)
7714 goto done;
7715
7716 if (!info->reduce_memory_overheads)
7717 elf_tdata (bfd1)->symbuf = ssymbuf1
7718 = elf_create_symbuf (symcount1, isymbuf1);
7719 }
7720
7721 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7722 {
7723 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7724 NULL, NULL, NULL);
7725 if (isymbuf2 == NULL)
7726 goto done;
7727
7728 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7729 elf_tdata (bfd2)->symbuf = ssymbuf2
7730 = elf_create_symbuf (symcount2, isymbuf2);
7731 }
7732
7733 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7734 {
7735 /* Optimized faster version. */
ef53be89 7736 size_t lo, hi, mid;
4d269e42
AM
7737 struct elf_symbol *symp;
7738 struct elf_symbuf_symbol *ssym, *ssymend;
7739
7740 lo = 0;
7741 hi = ssymbuf1->count;
7742 ssymbuf1++;
7743 count1 = 0;
7744 while (lo < hi)
7745 {
7746 mid = (lo + hi) / 2;
cb33740c 7747 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7748 hi = mid;
cb33740c 7749 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7750 lo = mid + 1;
7751 else
7752 {
7753 count1 = ssymbuf1[mid].count;
7754 ssymbuf1 += mid;
7755 break;
7756 }
7757 }
7758
7759 lo = 0;
7760 hi = ssymbuf2->count;
7761 ssymbuf2++;
7762 count2 = 0;
7763 while (lo < hi)
7764 {
7765 mid = (lo + hi) / 2;
cb33740c 7766 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7767 hi = mid;
cb33740c 7768 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7769 lo = mid + 1;
7770 else
7771 {
7772 count2 = ssymbuf2[mid].count;
7773 ssymbuf2 += mid;
7774 break;
7775 }
7776 }
7777
7778 if (count1 == 0 || count2 == 0 || count1 != count2)
7779 goto done;
7780
ca4be51c
AM
7781 symtable1
7782 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7783 symtable2
7784 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7785 if (symtable1 == NULL || symtable2 == NULL)
7786 goto done;
7787
7788 symp = symtable1;
7789 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7790 ssym < ssymend; ssym++, symp++)
7791 {
7792 symp->u.ssym = ssym;
7793 symp->name = bfd_elf_string_from_elf_section (bfd1,
7794 hdr1->sh_link,
7795 ssym->st_name);
7796 }
7797
7798 symp = symtable2;
7799 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7800 ssym < ssymend; ssym++, symp++)
7801 {
7802 symp->u.ssym = ssym;
7803 symp->name = bfd_elf_string_from_elf_section (bfd2,
7804 hdr2->sh_link,
7805 ssym->st_name);
7806 }
7807
7808 /* Sort symbol by name. */
7809 qsort (symtable1, count1, sizeof (struct elf_symbol),
7810 elf_sym_name_compare);
7811 qsort (symtable2, count1, sizeof (struct elf_symbol),
7812 elf_sym_name_compare);
7813
7814 for (i = 0; i < count1; i++)
7815 /* Two symbols must have the same binding, type and name. */
7816 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7817 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7818 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7819 goto done;
7820
7821 result = TRUE;
7822 goto done;
7823 }
7824
a50b1753
NC
7825 symtable1 = (struct elf_symbol *)
7826 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7827 symtable2 = (struct elf_symbol *)
7828 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7829 if (symtable1 == NULL || symtable2 == NULL)
7830 goto done;
7831
7832 /* Count definitions in the section. */
7833 count1 = 0;
7834 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7835 if (isym->st_shndx == shndx1)
4d269e42
AM
7836 symtable1[count1++].u.isym = isym;
7837
7838 count2 = 0;
7839 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7840 if (isym->st_shndx == shndx2)
4d269e42
AM
7841 symtable2[count2++].u.isym = isym;
7842
7843 if (count1 == 0 || count2 == 0 || count1 != count2)
7844 goto done;
7845
7846 for (i = 0; i < count1; i++)
7847 symtable1[i].name
7848 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7849 symtable1[i].u.isym->st_name);
7850
7851 for (i = 0; i < count2; i++)
7852 symtable2[i].name
7853 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7854 symtable2[i].u.isym->st_name);
7855
7856 /* Sort symbol by name. */
7857 qsort (symtable1, count1, sizeof (struct elf_symbol),
7858 elf_sym_name_compare);
7859 qsort (symtable2, count1, sizeof (struct elf_symbol),
7860 elf_sym_name_compare);
7861
7862 for (i = 0; i < count1; i++)
7863 /* Two symbols must have the same binding, type and name. */
7864 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7865 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7866 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7867 goto done;
7868
7869 result = TRUE;
7870
7871done:
7872 if (symtable1)
7873 free (symtable1);
7874 if (symtable2)
7875 free (symtable2);
7876 if (isymbuf1)
7877 free (isymbuf1);
7878 if (isymbuf2)
7879 free (isymbuf2);
7880
7881 return result;
7882}
7883
7884/* Return TRUE if 2 section types are compatible. */
7885
7886bfd_boolean
7887_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7888 bfd *bbfd, const asection *bsec)
7889{
7890 if (asec == NULL
7891 || bsec == NULL
7892 || abfd->xvec->flavour != bfd_target_elf_flavour
7893 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7894 return TRUE;
7895
7896 return elf_section_type (asec) == elf_section_type (bsec);
7897}
7898\f
c152c796
AM
7899/* Final phase of ELF linker. */
7900
7901/* A structure we use to avoid passing large numbers of arguments. */
7902
7903struct elf_final_link_info
7904{
7905 /* General link information. */
7906 struct bfd_link_info *info;
7907 /* Output BFD. */
7908 bfd *output_bfd;
7909 /* Symbol string table. */
ef10c3ac 7910 struct elf_strtab_hash *symstrtab;
c152c796
AM
7911 /* .hash section. */
7912 asection *hash_sec;
7913 /* symbol version section (.gnu.version). */
7914 asection *symver_sec;
7915 /* Buffer large enough to hold contents of any section. */
7916 bfd_byte *contents;
7917 /* Buffer large enough to hold external relocs of any section. */
7918 void *external_relocs;
7919 /* Buffer large enough to hold internal relocs of any section. */
7920 Elf_Internal_Rela *internal_relocs;
7921 /* Buffer large enough to hold external local symbols of any input
7922 BFD. */
7923 bfd_byte *external_syms;
7924 /* And a buffer for symbol section indices. */
7925 Elf_External_Sym_Shndx *locsym_shndx;
7926 /* Buffer large enough to hold internal local symbols of any input
7927 BFD. */
7928 Elf_Internal_Sym *internal_syms;
7929 /* Array large enough to hold a symbol index for each local symbol
7930 of any input BFD. */
7931 long *indices;
7932 /* Array large enough to hold a section pointer for each local
7933 symbol of any input BFD. */
7934 asection **sections;
ef10c3ac 7935 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7936 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7937 /* Number of STT_FILE syms seen. */
7938 size_t filesym_count;
c152c796
AM
7939};
7940
7941/* This struct is used to pass information to elf_link_output_extsym. */
7942
7943struct elf_outext_info
7944{
7945 bfd_boolean failed;
7946 bfd_boolean localsyms;
34a79995 7947 bfd_boolean file_sym_done;
8b127cbc 7948 struct elf_final_link_info *flinfo;
c152c796
AM
7949};
7950
d9352518
DB
7951
7952/* Support for evaluating a complex relocation.
7953
7954 Complex relocations are generalized, self-describing relocations. The
7955 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7956 relocations themselves.
d9352518
DB
7957
7958 The relocations are use a reserved elf-wide relocation type code (R_RELC
7959 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7960 information (start bit, end bit, word width, etc) into the addend. This
7961 information is extracted from CGEN-generated operand tables within gas.
7962
7963 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7964 internal) representing prefix-notation expressions, including but not
7965 limited to those sorts of expressions normally encoded as addends in the
7966 addend field. The symbol mangling format is:
7967
7968 <node> := <literal>
7969 | <unary-operator> ':' <node>
7970 | <binary-operator> ':' <node> ':' <node>
7971 ;
7972
7973 <literal> := 's' <digits=N> ':' <N character symbol name>
7974 | 'S' <digits=N> ':' <N character section name>
7975 | '#' <hexdigits>
7976 ;
7977
7978 <binary-operator> := as in C
7979 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7980
7981static void
a0c8462f
AM
7982set_symbol_value (bfd *bfd_with_globals,
7983 Elf_Internal_Sym *isymbuf,
7984 size_t locsymcount,
7985 size_t symidx,
7986 bfd_vma val)
d9352518 7987{
8977835c
AM
7988 struct elf_link_hash_entry **sym_hashes;
7989 struct elf_link_hash_entry *h;
7990 size_t extsymoff = locsymcount;
d9352518 7991
8977835c 7992 if (symidx < locsymcount)
d9352518 7993 {
8977835c
AM
7994 Elf_Internal_Sym *sym;
7995
7996 sym = isymbuf + symidx;
7997 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7998 {
7999 /* It is a local symbol: move it to the
8000 "absolute" section and give it a value. */
8001 sym->st_shndx = SHN_ABS;
8002 sym->st_value = val;
8003 return;
8004 }
8005 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8006 extsymoff = 0;
d9352518 8007 }
8977835c
AM
8008
8009 /* It is a global symbol: set its link type
8010 to "defined" and give it a value. */
8011
8012 sym_hashes = elf_sym_hashes (bfd_with_globals);
8013 h = sym_hashes [symidx - extsymoff];
8014 while (h->root.type == bfd_link_hash_indirect
8015 || h->root.type == bfd_link_hash_warning)
8016 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8017 h->root.type = bfd_link_hash_defined;
8018 h->root.u.def.value = val;
8019 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8020}
8021
a0c8462f
AM
8022static bfd_boolean
8023resolve_symbol (const char *name,
8024 bfd *input_bfd,
8b127cbc 8025 struct elf_final_link_info *flinfo,
a0c8462f
AM
8026 bfd_vma *result,
8027 Elf_Internal_Sym *isymbuf,
8028 size_t locsymcount)
d9352518 8029{
a0c8462f
AM
8030 Elf_Internal_Sym *sym;
8031 struct bfd_link_hash_entry *global_entry;
8032 const char *candidate = NULL;
8033 Elf_Internal_Shdr *symtab_hdr;
8034 size_t i;
8035
d9352518
DB
8036 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8037
8038 for (i = 0; i < locsymcount; ++ i)
8039 {
8977835c 8040 sym = isymbuf + i;
d9352518
DB
8041
8042 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8043 continue;
8044
8045 candidate = bfd_elf_string_from_elf_section (input_bfd,
8046 symtab_hdr->sh_link,
8047 sym->st_name);
8048#ifdef DEBUG
0f02bbd9
AM
8049 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8050 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8051#endif
8052 if (candidate && strcmp (candidate, name) == 0)
8053 {
8b127cbc 8054 asection *sec = flinfo->sections [i];
d9352518 8055
0f02bbd9
AM
8056 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8057 *result += sec->output_offset + sec->output_section->vma;
d9352518 8058#ifdef DEBUG
0f02bbd9
AM
8059 printf ("Found symbol with value %8.8lx\n",
8060 (unsigned long) *result);
d9352518
DB
8061#endif
8062 return TRUE;
8063 }
8064 }
8065
8066 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8067 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8068 FALSE, FALSE, TRUE);
d9352518
DB
8069 if (!global_entry)
8070 return FALSE;
a0c8462f 8071
d9352518
DB
8072 if (global_entry->type == bfd_link_hash_defined
8073 || global_entry->type == bfd_link_hash_defweak)
8074 {
a0c8462f
AM
8075 *result = (global_entry->u.def.value
8076 + global_entry->u.def.section->output_section->vma
8077 + global_entry->u.def.section->output_offset);
d9352518 8078#ifdef DEBUG
0f02bbd9
AM
8079 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8080 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8081#endif
8082 return TRUE;
a0c8462f 8083 }
d9352518 8084
d9352518
DB
8085 return FALSE;
8086}
8087
37b01f6a
DG
8088/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8089 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8090 names like "foo.end" which is the end address of section "foo". */
8091
d9352518 8092static bfd_boolean
a0c8462f
AM
8093resolve_section (const char *name,
8094 asection *sections,
37b01f6a
DG
8095 bfd_vma *result,
8096 bfd * abfd)
d9352518 8097{
a0c8462f
AM
8098 asection *curr;
8099 unsigned int len;
d9352518 8100
a0c8462f 8101 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8102 if (strcmp (curr->name, name) == 0)
8103 {
8104 *result = curr->vma;
8105 return TRUE;
8106 }
8107
8108 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8109 /* FIXME: This could be coded more efficiently... */
a0c8462f 8110 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8111 {
8112 len = strlen (curr->name);
a0c8462f 8113 if (len > strlen (name))
d9352518
DB
8114 continue;
8115
8116 if (strncmp (curr->name, name, len) == 0)
8117 {
8118 if (strncmp (".end", name + len, 4) == 0)
8119 {
37b01f6a 8120 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8121 return TRUE;
8122 }
8123
8124 /* Insert more pseudo-section names here, if you like. */
8125 }
8126 }
a0c8462f 8127
d9352518
DB
8128 return FALSE;
8129}
8130
8131static void
a0c8462f 8132undefined_reference (const char *reftype, const char *name)
d9352518 8133{
695344c0 8134 /* xgettext:c-format */
a0c8462f
AM
8135 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8136 reftype, name);
d9352518
DB
8137}
8138
8139static bfd_boolean
a0c8462f
AM
8140eval_symbol (bfd_vma *result,
8141 const char **symp,
8142 bfd *input_bfd,
8b127cbc 8143 struct elf_final_link_info *flinfo,
a0c8462f
AM
8144 bfd_vma dot,
8145 Elf_Internal_Sym *isymbuf,
8146 size_t locsymcount,
8147 int signed_p)
d9352518 8148{
4b93929b
NC
8149 size_t len;
8150 size_t symlen;
a0c8462f
AM
8151 bfd_vma a;
8152 bfd_vma b;
4b93929b 8153 char symbuf[4096];
0f02bbd9 8154 const char *sym = *symp;
a0c8462f
AM
8155 const char *symend;
8156 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8157
8158 len = strlen (sym);
8159 symend = sym + len;
8160
4b93929b 8161 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8162 {
8163 bfd_set_error (bfd_error_invalid_operation);
8164 return FALSE;
8165 }
a0c8462f 8166
d9352518
DB
8167 switch (* sym)
8168 {
8169 case '.':
0f02bbd9
AM
8170 *result = dot;
8171 *symp = sym + 1;
d9352518
DB
8172 return TRUE;
8173
8174 case '#':
0f02bbd9
AM
8175 ++sym;
8176 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8177 return TRUE;
8178
8179 case 'S':
8180 symbol_is_section = TRUE;
1a0670f3 8181 /* Fall through. */
a0c8462f 8182 case 's':
0f02bbd9
AM
8183 ++sym;
8184 symlen = strtol (sym, (char **) symp, 10);
8185 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8186
4b93929b 8187 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8188 {
8189 bfd_set_error (bfd_error_invalid_operation);
8190 return FALSE;
8191 }
8192
8193 memcpy (symbuf, sym, symlen);
a0c8462f 8194 symbuf[symlen] = '\0';
0f02bbd9 8195 *symp = sym + symlen;
a0c8462f
AM
8196
8197 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8198 the symbol as a section, or vice-versa. so we're pretty liberal in our
8199 interpretation here; section means "try section first", not "must be a
8200 section", and likewise with symbol. */
8201
a0c8462f 8202 if (symbol_is_section)
d9352518 8203 {
37b01f6a 8204 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8205 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8206 isymbuf, locsymcount))
d9352518
DB
8207 {
8208 undefined_reference ("section", symbuf);
8209 return FALSE;
8210 }
a0c8462f
AM
8211 }
8212 else
d9352518 8213 {
8b127cbc 8214 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8215 isymbuf, locsymcount)
8b127cbc 8216 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8217 result, input_bfd))
d9352518
DB
8218 {
8219 undefined_reference ("symbol", symbuf);
8220 return FALSE;
8221 }
8222 }
8223
8224 return TRUE;
a0c8462f 8225
d9352518
DB
8226 /* All that remains are operators. */
8227
8228#define UNARY_OP(op) \
8229 if (strncmp (sym, #op, strlen (#op)) == 0) \
8230 { \
8231 sym += strlen (#op); \
a0c8462f
AM
8232 if (*sym == ':') \
8233 ++sym; \
0f02bbd9 8234 *symp = sym; \
8b127cbc 8235 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8236 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8237 return FALSE; \
8238 if (signed_p) \
0f02bbd9 8239 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8240 else \
8241 *result = op a; \
d9352518
DB
8242 return TRUE; \
8243 }
8244
8245#define BINARY_OP(op) \
8246 if (strncmp (sym, #op, strlen (#op)) == 0) \
8247 { \
8248 sym += strlen (#op); \
a0c8462f
AM
8249 if (*sym == ':') \
8250 ++sym; \
0f02bbd9 8251 *symp = sym; \
8b127cbc 8252 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8253 isymbuf, locsymcount, signed_p)) \
a0c8462f 8254 return FALSE; \
0f02bbd9 8255 ++*symp; \
8b127cbc 8256 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8257 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8258 return FALSE; \
8259 if (signed_p) \
0f02bbd9 8260 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8261 else \
8262 *result = a op b; \
d9352518
DB
8263 return TRUE; \
8264 }
8265
8266 default:
8267 UNARY_OP (0-);
8268 BINARY_OP (<<);
8269 BINARY_OP (>>);
8270 BINARY_OP (==);
8271 BINARY_OP (!=);
8272 BINARY_OP (<=);
8273 BINARY_OP (>=);
8274 BINARY_OP (&&);
8275 BINARY_OP (||);
8276 UNARY_OP (~);
8277 UNARY_OP (!);
8278 BINARY_OP (*);
8279 BINARY_OP (/);
8280 BINARY_OP (%);
8281 BINARY_OP (^);
8282 BINARY_OP (|);
8283 BINARY_OP (&);
8284 BINARY_OP (+);
8285 BINARY_OP (-);
8286 BINARY_OP (<);
8287 BINARY_OP (>);
8288#undef UNARY_OP
8289#undef BINARY_OP
8290 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8291 bfd_set_error (bfd_error_invalid_operation);
8292 return FALSE;
8293 }
8294}
8295
d9352518 8296static void
a0c8462f
AM
8297put_value (bfd_vma size,
8298 unsigned long chunksz,
8299 bfd *input_bfd,
8300 bfd_vma x,
8301 bfd_byte *location)
d9352518
DB
8302{
8303 location += (size - chunksz);
8304
41cd1ad1 8305 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8306 {
8307 switch (chunksz)
8308 {
d9352518
DB
8309 case 1:
8310 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8311 x >>= 8;
d9352518
DB
8312 break;
8313 case 2:
8314 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8315 x >>= 16;
d9352518
DB
8316 break;
8317 case 4:
8318 bfd_put_32 (input_bfd, x, location);
65164438
NC
8319 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8320 x >>= 16;
8321 x >>= 16;
d9352518 8322 break;
d9352518 8323#ifdef BFD64
41cd1ad1 8324 case 8:
d9352518 8325 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8326 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8327 x >>= 32;
8328 x >>= 32;
8329 break;
d9352518 8330#endif
41cd1ad1
NC
8331 default:
8332 abort ();
d9352518
DB
8333 break;
8334 }
8335 }
8336}
8337
a0c8462f
AM
8338static bfd_vma
8339get_value (bfd_vma size,
8340 unsigned long chunksz,
8341 bfd *input_bfd,
8342 bfd_byte *location)
d9352518 8343{
9b239e0e 8344 int shift;
d9352518
DB
8345 bfd_vma x = 0;
8346
9b239e0e
NC
8347 /* Sanity checks. */
8348 BFD_ASSERT (chunksz <= sizeof (x)
8349 && size >= chunksz
8350 && chunksz != 0
8351 && (size % chunksz) == 0
8352 && input_bfd != NULL
8353 && location != NULL);
8354
8355 if (chunksz == sizeof (x))
8356 {
8357 BFD_ASSERT (size == chunksz);
8358
8359 /* Make sure that we do not perform an undefined shift operation.
8360 We know that size == chunksz so there will only be one iteration
8361 of the loop below. */
8362 shift = 0;
8363 }
8364 else
8365 shift = 8 * chunksz;
8366
a0c8462f 8367 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8368 {
8369 switch (chunksz)
8370 {
d9352518 8371 case 1:
9b239e0e 8372 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8373 break;
8374 case 2:
9b239e0e 8375 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8376 break;
8377 case 4:
9b239e0e 8378 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8379 break;
d9352518 8380#ifdef BFD64
9b239e0e
NC
8381 case 8:
8382 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8383 break;
9b239e0e
NC
8384#endif
8385 default:
8386 abort ();
d9352518
DB
8387 }
8388 }
8389 return x;
8390}
8391
a0c8462f
AM
8392static void
8393decode_complex_addend (unsigned long *start, /* in bits */
8394 unsigned long *oplen, /* in bits */
8395 unsigned long *len, /* in bits */
8396 unsigned long *wordsz, /* in bytes */
8397 unsigned long *chunksz, /* in bytes */
8398 unsigned long *lsb0_p,
8399 unsigned long *signed_p,
8400 unsigned long *trunc_p,
8401 unsigned long encoded)
d9352518
DB
8402{
8403 * start = encoded & 0x3F;
8404 * len = (encoded >> 6) & 0x3F;
8405 * oplen = (encoded >> 12) & 0x3F;
8406 * wordsz = (encoded >> 18) & 0xF;
8407 * chunksz = (encoded >> 22) & 0xF;
8408 * lsb0_p = (encoded >> 27) & 1;
8409 * signed_p = (encoded >> 28) & 1;
8410 * trunc_p = (encoded >> 29) & 1;
8411}
8412
cdfeee4f 8413bfd_reloc_status_type
0f02bbd9 8414bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8415 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8416 bfd_byte *contents,
8417 Elf_Internal_Rela *rel,
8418 bfd_vma relocation)
d9352518 8419{
0f02bbd9
AM
8420 bfd_vma shift, x, mask;
8421 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8422 bfd_reloc_status_type r;
d9352518
DB
8423
8424 /* Perform this reloc, since it is complex.
8425 (this is not to say that it necessarily refers to a complex
8426 symbol; merely that it is a self-describing CGEN based reloc.
8427 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8428 word size, etc) encoded within it.). */
d9352518 8429
a0c8462f
AM
8430 decode_complex_addend (&start, &oplen, &len, &wordsz,
8431 &chunksz, &lsb0_p, &signed_p,
8432 &trunc_p, rel->r_addend);
d9352518
DB
8433
8434 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8435
8436 if (lsb0_p)
8437 shift = (start + 1) - len;
8438 else
8439 shift = (8 * wordsz) - (start + len);
8440
37b01f6a
DG
8441 x = get_value (wordsz, chunksz, input_bfd,
8442 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8443
8444#ifdef DEBUG
8445 printf ("Doing complex reloc: "
8446 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8447 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8448 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8449 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8450 oplen, (unsigned long) x, (unsigned long) mask,
8451 (unsigned long) relocation);
d9352518
DB
8452#endif
8453
cdfeee4f 8454 r = bfd_reloc_ok;
d9352518 8455 if (! trunc_p)
cdfeee4f
AM
8456 /* Now do an overflow check. */
8457 r = bfd_check_overflow ((signed_p
8458 ? complain_overflow_signed
8459 : complain_overflow_unsigned),
8460 len, 0, (8 * wordsz),
8461 relocation);
a0c8462f 8462
d9352518
DB
8463 /* Do the deed. */
8464 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8465
8466#ifdef DEBUG
8467 printf (" relocation: %8.8lx\n"
8468 " shifted mask: %8.8lx\n"
8469 " shifted/masked reloc: %8.8lx\n"
8470 " result: %8.8lx\n",
9ccb8af9
AM
8471 (unsigned long) relocation, (unsigned long) (mask << shift),
8472 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8473#endif
37b01f6a
DG
8474 put_value (wordsz, chunksz, input_bfd, x,
8475 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8476 return r;
d9352518
DB
8477}
8478
0e287786
AM
8479/* Functions to read r_offset from external (target order) reloc
8480 entry. Faster than bfd_getl32 et al, because we let the compiler
8481 know the value is aligned. */
53df40a4 8482
0e287786
AM
8483static bfd_vma
8484ext32l_r_offset (const void *p)
53df40a4
AM
8485{
8486 union aligned32
8487 {
8488 uint32_t v;
8489 unsigned char c[4];
8490 };
8491 const union aligned32 *a
0e287786 8492 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8493
8494 uint32_t aval = ( (uint32_t) a->c[0]
8495 | (uint32_t) a->c[1] << 8
8496 | (uint32_t) a->c[2] << 16
8497 | (uint32_t) a->c[3] << 24);
0e287786 8498 return aval;
53df40a4
AM
8499}
8500
0e287786
AM
8501static bfd_vma
8502ext32b_r_offset (const void *p)
53df40a4
AM
8503{
8504 union aligned32
8505 {
8506 uint32_t v;
8507 unsigned char c[4];
8508 };
8509 const union aligned32 *a
0e287786 8510 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8511
8512 uint32_t aval = ( (uint32_t) a->c[0] << 24
8513 | (uint32_t) a->c[1] << 16
8514 | (uint32_t) a->c[2] << 8
8515 | (uint32_t) a->c[3]);
0e287786 8516 return aval;
53df40a4
AM
8517}
8518
8519#ifdef BFD_HOST_64_BIT
0e287786
AM
8520static bfd_vma
8521ext64l_r_offset (const void *p)
53df40a4
AM
8522{
8523 union aligned64
8524 {
8525 uint64_t v;
8526 unsigned char c[8];
8527 };
8528 const union aligned64 *a
0e287786 8529 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8530
8531 uint64_t aval = ( (uint64_t) a->c[0]
8532 | (uint64_t) a->c[1] << 8
8533 | (uint64_t) a->c[2] << 16
8534 | (uint64_t) a->c[3] << 24
8535 | (uint64_t) a->c[4] << 32
8536 | (uint64_t) a->c[5] << 40
8537 | (uint64_t) a->c[6] << 48
8538 | (uint64_t) a->c[7] << 56);
0e287786 8539 return aval;
53df40a4
AM
8540}
8541
0e287786
AM
8542static bfd_vma
8543ext64b_r_offset (const void *p)
53df40a4
AM
8544{
8545 union aligned64
8546 {
8547 uint64_t v;
8548 unsigned char c[8];
8549 };
8550 const union aligned64 *a
0e287786 8551 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8552
8553 uint64_t aval = ( (uint64_t) a->c[0] << 56
8554 | (uint64_t) a->c[1] << 48
8555 | (uint64_t) a->c[2] << 40
8556 | (uint64_t) a->c[3] << 32
8557 | (uint64_t) a->c[4] << 24
8558 | (uint64_t) a->c[5] << 16
8559 | (uint64_t) a->c[6] << 8
8560 | (uint64_t) a->c[7]);
0e287786 8561 return aval;
53df40a4
AM
8562}
8563#endif
8564
c152c796
AM
8565/* When performing a relocatable link, the input relocations are
8566 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8567 referenced must be updated. Update all the relocations found in
8568 RELDATA. */
c152c796 8569
bca6d0e3 8570static bfd_boolean
c152c796 8571elf_link_adjust_relocs (bfd *abfd,
9eaff861 8572 asection *sec,
28dbcedc 8573 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8574 bfd_boolean sort,
8575 struct bfd_link_info *info)
c152c796
AM
8576{
8577 unsigned int i;
8578 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8579 bfd_byte *erela;
8580 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8581 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8582 bfd_vma r_type_mask;
8583 int r_sym_shift;
d4730f92
BS
8584 unsigned int count = reldata->count;
8585 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8586
d4730f92 8587 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8588 {
8589 swap_in = bed->s->swap_reloc_in;
8590 swap_out = bed->s->swap_reloc_out;
8591 }
d4730f92 8592 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8593 {
8594 swap_in = bed->s->swap_reloca_in;
8595 swap_out = bed->s->swap_reloca_out;
8596 }
8597 else
8598 abort ();
8599
8600 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8601 abort ();
8602
8603 if (bed->s->arch_size == 32)
8604 {
8605 r_type_mask = 0xff;
8606 r_sym_shift = 8;
8607 }
8608 else
8609 {
8610 r_type_mask = 0xffffffff;
8611 r_sym_shift = 32;
8612 }
8613
d4730f92
BS
8614 erela = reldata->hdr->contents;
8615 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8616 {
8617 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8618 unsigned int j;
8619
8620 if (*rel_hash == NULL)
8621 continue;
8622
10bbbc1d
NC
8623 if ((*rel_hash)->indx == -2
8624 && info->gc_sections
8625 && ! info->gc_keep_exported)
8626 {
8627 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8628 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8629 abfd, sec,
8630 (*rel_hash)->root.root.string);
8631 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
d42c267e 8632 abfd, sec);
10bbbc1d
NC
8633 bfd_set_error (bfd_error_invalid_operation);
8634 return FALSE;
8635 }
c152c796
AM
8636 BFD_ASSERT ((*rel_hash)->indx >= 0);
8637
8638 (*swap_in) (abfd, erela, irela);
8639 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8640 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8641 | (irela[j].r_info & r_type_mask));
8642 (*swap_out) (abfd, irela, erela);
8643 }
53df40a4 8644
9eaff861
AO
8645 if (bed->elf_backend_update_relocs)
8646 (*bed->elf_backend_update_relocs) (sec, reldata);
8647
0e287786 8648 if (sort && count != 0)
53df40a4 8649 {
0e287786
AM
8650 bfd_vma (*ext_r_off) (const void *);
8651 bfd_vma r_off;
8652 size_t elt_size;
8653 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8654 bfd_byte *buf = NULL;
28dbcedc
AM
8655
8656 if (bed->s->arch_size == 32)
8657 {
8658 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8659 ext_r_off = ext32l_r_offset;
28dbcedc 8660 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8661 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8662 else
8663 abort ();
8664 }
53df40a4 8665 else
28dbcedc 8666 {
53df40a4 8667#ifdef BFD_HOST_64_BIT
28dbcedc 8668 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8669 ext_r_off = ext64l_r_offset;
28dbcedc 8670 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8671 ext_r_off = ext64b_r_offset;
28dbcedc 8672 else
53df40a4 8673#endif
28dbcedc
AM
8674 abort ();
8675 }
0e287786 8676
bca6d0e3
AM
8677 /* Must use a stable sort here. A modified insertion sort,
8678 since the relocs are mostly sorted already. */
0e287786
AM
8679 elt_size = reldata->hdr->sh_entsize;
8680 base = reldata->hdr->contents;
8681 end = base + count * elt_size;
bca6d0e3 8682 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8683 abort ();
8684
8685 /* Ensure the first element is lowest. This acts as a sentinel,
8686 speeding the main loop below. */
8687 r_off = (*ext_r_off) (base);
8688 for (p = loc = base; (p += elt_size) < end; )
8689 {
8690 bfd_vma r_off2 = (*ext_r_off) (p);
8691 if (r_off > r_off2)
8692 {
8693 r_off = r_off2;
8694 loc = p;
8695 }
8696 }
8697 if (loc != base)
8698 {
8699 /* Don't just swap *base and *loc as that changes the order
8700 of the original base[0] and base[1] if they happen to
8701 have the same r_offset. */
bca6d0e3
AM
8702 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8703 memcpy (onebuf, loc, elt_size);
0e287786 8704 memmove (base + elt_size, base, loc - base);
bca6d0e3 8705 memcpy (base, onebuf, elt_size);
0e287786
AM
8706 }
8707
b29b8669 8708 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8709 {
8710 /* base to p is sorted, *p is next to insert. */
8711 r_off = (*ext_r_off) (p);
8712 /* Search the sorted region for location to insert. */
8713 loc = p - elt_size;
8714 while (r_off < (*ext_r_off) (loc))
8715 loc -= elt_size;
8716 loc += elt_size;
8717 if (loc != p)
8718 {
bca6d0e3
AM
8719 /* Chances are there is a run of relocs to insert here,
8720 from one of more input files. Files are not always
8721 linked in order due to the way elf_link_input_bfd is
8722 called. See pr17666. */
8723 size_t sortlen = p - loc;
8724 bfd_vma r_off2 = (*ext_r_off) (loc);
8725 size_t runlen = elt_size;
8726 size_t buf_size = 96 * 1024;
8727 while (p + runlen < end
8728 && (sortlen <= buf_size
8729 || runlen + elt_size <= buf_size)
8730 && r_off2 > (*ext_r_off) (p + runlen))
8731 runlen += elt_size;
8732 if (buf == NULL)
8733 {
8734 buf = bfd_malloc (buf_size);
8735 if (buf == NULL)
8736 return FALSE;
8737 }
8738 if (runlen < sortlen)
8739 {
8740 memcpy (buf, p, runlen);
8741 memmove (loc + runlen, loc, sortlen);
8742 memcpy (loc, buf, runlen);
8743 }
8744 else
8745 {
8746 memcpy (buf, loc, sortlen);
8747 memmove (loc, p, runlen);
8748 memcpy (loc + runlen, buf, sortlen);
8749 }
b29b8669 8750 p += runlen - elt_size;
0e287786
AM
8751 }
8752 }
8753 /* Hashes are no longer valid. */
28dbcedc
AM
8754 free (reldata->hashes);
8755 reldata->hashes = NULL;
bca6d0e3 8756 free (buf);
53df40a4 8757 }
bca6d0e3 8758 return TRUE;
c152c796
AM
8759}
8760
8761struct elf_link_sort_rela
8762{
8763 union {
8764 bfd_vma offset;
8765 bfd_vma sym_mask;
8766 } u;
8767 enum elf_reloc_type_class type;
8768 /* We use this as an array of size int_rels_per_ext_rel. */
8769 Elf_Internal_Rela rela[1];
8770};
8771
8772static int
8773elf_link_sort_cmp1 (const void *A, const void *B)
8774{
a50b1753
NC
8775 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8776 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8777 int relativea, relativeb;
8778
8779 relativea = a->type == reloc_class_relative;
8780 relativeb = b->type == reloc_class_relative;
8781
8782 if (relativea < relativeb)
8783 return 1;
8784 if (relativea > relativeb)
8785 return -1;
8786 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8787 return -1;
8788 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8789 return 1;
8790 if (a->rela->r_offset < b->rela->r_offset)
8791 return -1;
8792 if (a->rela->r_offset > b->rela->r_offset)
8793 return 1;
8794 return 0;
8795}
8796
8797static int
8798elf_link_sort_cmp2 (const void *A, const void *B)
8799{
a50b1753
NC
8800 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8801 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8802
7e612e98 8803 if (a->type < b->type)
c152c796 8804 return -1;
7e612e98 8805 if (a->type > b->type)
c152c796 8806 return 1;
7e612e98 8807 if (a->u.offset < b->u.offset)
c152c796 8808 return -1;
7e612e98 8809 if (a->u.offset > b->u.offset)
c152c796
AM
8810 return 1;
8811 if (a->rela->r_offset < b->rela->r_offset)
8812 return -1;
8813 if (a->rela->r_offset > b->rela->r_offset)
8814 return 1;
8815 return 0;
8816}
8817
8818static size_t
8819elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8820{
3410fea8 8821 asection *dynamic_relocs;
fc66a176
L
8822 asection *rela_dyn;
8823 asection *rel_dyn;
c152c796
AM
8824 bfd_size_type count, size;
8825 size_t i, ret, sort_elt, ext_size;
8826 bfd_byte *sort, *s_non_relative, *p;
8827 struct elf_link_sort_rela *sq;
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8829 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8830 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8831 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8832 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8833 struct bfd_link_order *lo;
8834 bfd_vma r_sym_mask;
3410fea8 8835 bfd_boolean use_rela;
c152c796 8836
3410fea8
NC
8837 /* Find a dynamic reloc section. */
8838 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8839 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8840 if (rela_dyn != NULL && rela_dyn->size > 0
8841 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8842 {
3410fea8
NC
8843 bfd_boolean use_rela_initialised = FALSE;
8844
8845 /* This is just here to stop gcc from complaining.
c8e44c6d 8846 Its initialization checking code is not perfect. */
3410fea8
NC
8847 use_rela = TRUE;
8848
8849 /* Both sections are present. Examine the sizes
8850 of the indirect sections to help us choose. */
8851 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8852 if (lo->type == bfd_indirect_link_order)
8853 {
8854 asection *o = lo->u.indirect.section;
8855
8856 if ((o->size % bed->s->sizeof_rela) == 0)
8857 {
8858 if ((o->size % bed->s->sizeof_rel) == 0)
8859 /* Section size is divisible by both rel and rela sizes.
8860 It is of no help to us. */
8861 ;
8862 else
8863 {
8864 /* Section size is only divisible by rela. */
535b785f 8865 if (use_rela_initialised && !use_rela)
3410fea8 8866 {
c8e44c6d
AM
8867 _bfd_error_handler (_("%B: Unable to sort relocs - "
8868 "they are in more than one size"),
8869 abfd);
3410fea8
NC
8870 bfd_set_error (bfd_error_invalid_operation);
8871 return 0;
8872 }
8873 else
8874 {
8875 use_rela = TRUE;
8876 use_rela_initialised = TRUE;
8877 }
8878 }
8879 }
8880 else if ((o->size % bed->s->sizeof_rel) == 0)
8881 {
8882 /* Section size is only divisible by rel. */
535b785f 8883 if (use_rela_initialised && use_rela)
3410fea8 8884 {
c8e44c6d
AM
8885 _bfd_error_handler (_("%B: Unable to sort relocs - "
8886 "they are in more than one size"),
8887 abfd);
3410fea8
NC
8888 bfd_set_error (bfd_error_invalid_operation);
8889 return 0;
8890 }
8891 else
8892 {
8893 use_rela = FALSE;
8894 use_rela_initialised = TRUE;
8895 }
8896 }
8897 else
8898 {
c8e44c6d
AM
8899 /* The section size is not divisible by either -
8900 something is wrong. */
8901 _bfd_error_handler (_("%B: Unable to sort relocs - "
8902 "they are of an unknown size"), abfd);
3410fea8
NC
8903 bfd_set_error (bfd_error_invalid_operation);
8904 return 0;
8905 }
8906 }
8907
8908 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8909 if (lo->type == bfd_indirect_link_order)
8910 {
8911 asection *o = lo->u.indirect.section;
8912
8913 if ((o->size % bed->s->sizeof_rela) == 0)
8914 {
8915 if ((o->size % bed->s->sizeof_rel) == 0)
8916 /* Section size is divisible by both rel and rela sizes.
8917 It is of no help to us. */
8918 ;
8919 else
8920 {
8921 /* Section size is only divisible by rela. */
535b785f 8922 if (use_rela_initialised && !use_rela)
3410fea8 8923 {
c8e44c6d
AM
8924 _bfd_error_handler (_("%B: Unable to sort relocs - "
8925 "they are in more than one size"),
8926 abfd);
3410fea8
NC
8927 bfd_set_error (bfd_error_invalid_operation);
8928 return 0;
8929 }
8930 else
8931 {
8932 use_rela = TRUE;
8933 use_rela_initialised = TRUE;
8934 }
8935 }
8936 }
8937 else if ((o->size % bed->s->sizeof_rel) == 0)
8938 {
8939 /* Section size is only divisible by rel. */
535b785f 8940 if (use_rela_initialised && use_rela)
3410fea8 8941 {
c8e44c6d
AM
8942 _bfd_error_handler (_("%B: Unable to sort relocs - "
8943 "they are in more than one size"),
8944 abfd);
3410fea8
NC
8945 bfd_set_error (bfd_error_invalid_operation);
8946 return 0;
8947 }
8948 else
8949 {
8950 use_rela = FALSE;
8951 use_rela_initialised = TRUE;
8952 }
8953 }
8954 else
8955 {
c8e44c6d
AM
8956 /* The section size is not divisible by either -
8957 something is wrong. */
8958 _bfd_error_handler (_("%B: Unable to sort relocs - "
8959 "they are of an unknown size"), abfd);
3410fea8
NC
8960 bfd_set_error (bfd_error_invalid_operation);
8961 return 0;
8962 }
8963 }
8964
8965 if (! use_rela_initialised)
8966 /* Make a guess. */
8967 use_rela = TRUE;
c152c796 8968 }
fc66a176
L
8969 else if (rela_dyn != NULL && rela_dyn->size > 0)
8970 use_rela = TRUE;
8971 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8972 use_rela = FALSE;
c152c796 8973 else
fc66a176 8974 return 0;
3410fea8
NC
8975
8976 if (use_rela)
c152c796 8977 {
3410fea8 8978 dynamic_relocs = rela_dyn;
c152c796
AM
8979 ext_size = bed->s->sizeof_rela;
8980 swap_in = bed->s->swap_reloca_in;
8981 swap_out = bed->s->swap_reloca_out;
8982 }
3410fea8
NC
8983 else
8984 {
8985 dynamic_relocs = rel_dyn;
8986 ext_size = bed->s->sizeof_rel;
8987 swap_in = bed->s->swap_reloc_in;
8988 swap_out = bed->s->swap_reloc_out;
8989 }
c152c796
AM
8990
8991 size = 0;
3410fea8 8992 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8993 if (lo->type == bfd_indirect_link_order)
3410fea8 8994 size += lo->u.indirect.section->size;
c152c796 8995
3410fea8 8996 if (size != dynamic_relocs->size)
c152c796
AM
8997 return 0;
8998
8999 sort_elt = (sizeof (struct elf_link_sort_rela)
9000 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9001
9002 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9003 if (count == 0)
9004 return 0;
a50b1753 9005 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9006
c152c796
AM
9007 if (sort == NULL)
9008 {
9009 (*info->callbacks->warning)
9010 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9011 return 0;
9012 }
9013
9014 if (bed->s->arch_size == 32)
9015 r_sym_mask = ~(bfd_vma) 0xff;
9016 else
9017 r_sym_mask = ~(bfd_vma) 0xffffffff;
9018
3410fea8 9019 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9020 if (lo->type == bfd_indirect_link_order)
9021 {
9022 bfd_byte *erel, *erelend;
9023 asection *o = lo->u.indirect.section;
9024
1da212d6
AM
9025 if (o->contents == NULL && o->size != 0)
9026 {
9027 /* This is a reloc section that is being handled as a normal
9028 section. See bfd_section_from_shdr. We can't combine
9029 relocs in this case. */
9030 free (sort);
9031 return 0;
9032 }
c152c796 9033 erel = o->contents;
eea6121a 9034 erelend = o->contents + o->size;
c8e44c6d 9035 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9036
c152c796
AM
9037 while (erel < erelend)
9038 {
9039 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9040
c152c796 9041 (*swap_in) (abfd, erel, s->rela);
7e612e98 9042 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9043 s->u.sym_mask = r_sym_mask;
9044 p += sort_elt;
9045 erel += ext_size;
9046 }
9047 }
9048
9049 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9050
9051 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9052 {
9053 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9054 if (s->type != reloc_class_relative)
9055 break;
9056 }
9057 ret = i;
9058 s_non_relative = p;
9059
9060 sq = (struct elf_link_sort_rela *) s_non_relative;
9061 for (; i < count; i++, p += sort_elt)
9062 {
9063 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9064 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9065 sq = sp;
9066 sp->u.offset = sq->rela->r_offset;
9067 }
9068
9069 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9070
c8e44c6d
AM
9071 struct elf_link_hash_table *htab = elf_hash_table (info);
9072 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9073 {
9074 /* We have plt relocs in .rela.dyn. */
9075 sq = (struct elf_link_sort_rela *) sort;
9076 for (i = 0; i < count; i++)
9077 if (sq[count - i - 1].type != reloc_class_plt)
9078 break;
9079 if (i != 0 && htab->srelplt->size == i * ext_size)
9080 {
9081 struct bfd_link_order **plo;
9082 /* Put srelplt link_order last. This is so the output_offset
9083 set in the next loop is correct for DT_JMPREL. */
9084 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9085 if ((*plo)->type == bfd_indirect_link_order
9086 && (*plo)->u.indirect.section == htab->srelplt)
9087 {
9088 lo = *plo;
9089 *plo = lo->next;
9090 }
9091 else
9092 plo = &(*plo)->next;
9093 *plo = lo;
9094 lo->next = NULL;
9095 dynamic_relocs->map_tail.link_order = lo;
9096 }
9097 }
9098
9099 p = sort;
3410fea8 9100 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9101 if (lo->type == bfd_indirect_link_order)
9102 {
9103 bfd_byte *erel, *erelend;
9104 asection *o = lo->u.indirect.section;
9105
9106 erel = o->contents;
eea6121a 9107 erelend = o->contents + o->size;
c8e44c6d 9108 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9109 while (erel < erelend)
9110 {
9111 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9112 (*swap_out) (abfd, s->rela, erel);
9113 p += sort_elt;
9114 erel += ext_size;
9115 }
9116 }
9117
9118 free (sort);
3410fea8 9119 *psec = dynamic_relocs;
c152c796
AM
9120 return ret;
9121}
9122
ef10c3ac 9123/* Add a symbol to the output symbol string table. */
c152c796 9124
6e0b88f1 9125static int
ef10c3ac
L
9126elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9127 const char *name,
9128 Elf_Internal_Sym *elfsym,
9129 asection *input_sec,
9130 struct elf_link_hash_entry *h)
c152c796 9131{
6e0b88f1 9132 int (*output_symbol_hook)
c152c796
AM
9133 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9134 struct elf_link_hash_entry *);
ef10c3ac 9135 struct elf_link_hash_table *hash_table;
c152c796 9136 const struct elf_backend_data *bed;
ef10c3ac 9137 bfd_size_type strtabsize;
c152c796 9138
8539e4e8
AM
9139 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9140
8b127cbc 9141 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9142 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9143 if (output_symbol_hook != NULL)
9144 {
8b127cbc 9145 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9146 if (ret != 1)
9147 return ret;
c152c796
AM
9148 }
9149
ef10c3ac
L
9150 if (name == NULL
9151 || *name == '\0'
9152 || (input_sec->flags & SEC_EXCLUDE))
9153 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9154 else
9155 {
ef10c3ac
L
9156 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9157 to get the final offset for st_name. */
9158 elfsym->st_name
9159 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9160 name, FALSE);
c152c796 9161 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9162 return 0;
c152c796
AM
9163 }
9164
ef10c3ac
L
9165 hash_table = elf_hash_table (flinfo->info);
9166 strtabsize = hash_table->strtabsize;
9167 if (strtabsize <= hash_table->strtabcount)
c152c796 9168 {
ef10c3ac
L
9169 strtabsize += strtabsize;
9170 hash_table->strtabsize = strtabsize;
9171 strtabsize *= sizeof (*hash_table->strtab);
9172 hash_table->strtab
9173 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9174 strtabsize);
9175 if (hash_table->strtab == NULL)
6e0b88f1 9176 return 0;
c152c796 9177 }
ef10c3ac
L
9178 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9179 hash_table->strtab[hash_table->strtabcount].dest_index
9180 = hash_table->strtabcount;
9181 hash_table->strtab[hash_table->strtabcount].destshndx_index
9182 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9183
9184 bfd_get_symcount (flinfo->output_bfd) += 1;
9185 hash_table->strtabcount += 1;
9186
9187 return 1;
9188}
9189
9190/* Swap symbols out to the symbol table and flush the output symbols to
9191 the file. */
9192
9193static bfd_boolean
9194elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9195{
9196 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9197 bfd_size_type amt;
9198 size_t i;
ef10c3ac
L
9199 const struct elf_backend_data *bed;
9200 bfd_byte *symbuf;
9201 Elf_Internal_Shdr *hdr;
9202 file_ptr pos;
9203 bfd_boolean ret;
9204
9205 if (!hash_table->strtabcount)
9206 return TRUE;
9207
9208 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9209
9210 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9211
ef10c3ac
L
9212 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9213 symbuf = (bfd_byte *) bfd_malloc (amt);
9214 if (symbuf == NULL)
9215 return FALSE;
1b786873 9216
ef10c3ac 9217 if (flinfo->symshndxbuf)
c152c796 9218 {
ef53be89
AM
9219 amt = sizeof (Elf_External_Sym_Shndx);
9220 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9221 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9222 if (flinfo->symshndxbuf == NULL)
c152c796 9223 {
ef10c3ac
L
9224 free (symbuf);
9225 return FALSE;
c152c796 9226 }
c152c796
AM
9227 }
9228
ef10c3ac
L
9229 for (i = 0; i < hash_table->strtabcount; i++)
9230 {
9231 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9232 if (elfsym->sym.st_name == (unsigned long) -1)
9233 elfsym->sym.st_name = 0;
9234 else
9235 elfsym->sym.st_name
9236 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9237 elfsym->sym.st_name);
9238 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9239 ((bfd_byte *) symbuf
9240 + (elfsym->dest_index
9241 * bed->s->sizeof_sym)),
9242 (flinfo->symshndxbuf
9243 + elfsym->destshndx_index));
9244 }
9245
9246 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9247 pos = hdr->sh_offset + hdr->sh_size;
9248 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9249 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9250 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9251 {
9252 hdr->sh_size += amt;
9253 ret = TRUE;
9254 }
9255 else
9256 ret = FALSE;
c152c796 9257
ef10c3ac
L
9258 free (symbuf);
9259
9260 free (hash_table->strtab);
9261 hash_table->strtab = NULL;
9262
9263 return ret;
c152c796
AM
9264}
9265
c0d5a53d
L
9266/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9267
9268static bfd_boolean
9269check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9270{
4fbb74a6
AM
9271 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9272 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9273 {
9274 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9275 beyond 64k. */
4eca0228 9276 _bfd_error_handler
695344c0 9277 /* xgettext:c-format */
c0d5a53d 9278 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9279 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9280 bfd_set_error (bfd_error_nonrepresentable_section);
9281 return FALSE;
9282 }
9283 return TRUE;
9284}
9285
c152c796
AM
9286/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9287 allowing an unsatisfied unversioned symbol in the DSO to match a
9288 versioned symbol that would normally require an explicit version.
9289 We also handle the case that a DSO references a hidden symbol
9290 which may be satisfied by a versioned symbol in another DSO. */
9291
9292static bfd_boolean
9293elf_link_check_versioned_symbol (struct bfd_link_info *info,
9294 const struct elf_backend_data *bed,
9295 struct elf_link_hash_entry *h)
9296{
9297 bfd *abfd;
9298 struct elf_link_loaded_list *loaded;
9299
9300 if (!is_elf_hash_table (info->hash))
9301 return FALSE;
9302
90c984fc
L
9303 /* Check indirect symbol. */
9304 while (h->root.type == bfd_link_hash_indirect)
9305 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9306
c152c796
AM
9307 switch (h->root.type)
9308 {
9309 default:
9310 abfd = NULL;
9311 break;
9312
9313 case bfd_link_hash_undefined:
9314 case bfd_link_hash_undefweak:
9315 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9316 if (abfd == NULL
9317 || (abfd->flags & DYNAMIC) == 0
e56f61be 9318 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9319 return FALSE;
9320 break;
9321
9322 case bfd_link_hash_defined:
9323 case bfd_link_hash_defweak:
9324 abfd = h->root.u.def.section->owner;
9325 break;
9326
9327 case bfd_link_hash_common:
9328 abfd = h->root.u.c.p->section->owner;
9329 break;
9330 }
9331 BFD_ASSERT (abfd != NULL);
9332
9333 for (loaded = elf_hash_table (info)->loaded;
9334 loaded != NULL;
9335 loaded = loaded->next)
9336 {
9337 bfd *input;
9338 Elf_Internal_Shdr *hdr;
ef53be89
AM
9339 size_t symcount;
9340 size_t extsymcount;
9341 size_t extsymoff;
c152c796
AM
9342 Elf_Internal_Shdr *versymhdr;
9343 Elf_Internal_Sym *isym;
9344 Elf_Internal_Sym *isymend;
9345 Elf_Internal_Sym *isymbuf;
9346 Elf_External_Versym *ever;
9347 Elf_External_Versym *extversym;
9348
9349 input = loaded->abfd;
9350
9351 /* We check each DSO for a possible hidden versioned definition. */
9352 if (input == abfd
9353 || (input->flags & DYNAMIC) == 0
9354 || elf_dynversym (input) == 0)
9355 continue;
9356
9357 hdr = &elf_tdata (input)->dynsymtab_hdr;
9358
9359 symcount = hdr->sh_size / bed->s->sizeof_sym;
9360 if (elf_bad_symtab (input))
9361 {
9362 extsymcount = symcount;
9363 extsymoff = 0;
9364 }
9365 else
9366 {
9367 extsymcount = symcount - hdr->sh_info;
9368 extsymoff = hdr->sh_info;
9369 }
9370
9371 if (extsymcount == 0)
9372 continue;
9373
9374 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9375 NULL, NULL, NULL);
9376 if (isymbuf == NULL)
9377 return FALSE;
9378
9379 /* Read in any version definitions. */
9380 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9381 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9382 if (extversym == NULL)
9383 goto error_ret;
9384
9385 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9386 || (bfd_bread (extversym, versymhdr->sh_size, input)
9387 != versymhdr->sh_size))
9388 {
9389 free (extversym);
9390 error_ret:
9391 free (isymbuf);
9392 return FALSE;
9393 }
9394
9395 ever = extversym + extsymoff;
9396 isymend = isymbuf + extsymcount;
9397 for (isym = isymbuf; isym < isymend; isym++, ever++)
9398 {
9399 const char *name;
9400 Elf_Internal_Versym iver;
9401 unsigned short version_index;
9402
9403 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9404 || isym->st_shndx == SHN_UNDEF)
9405 continue;
9406
9407 name = bfd_elf_string_from_elf_section (input,
9408 hdr->sh_link,
9409 isym->st_name);
9410 if (strcmp (name, h->root.root.string) != 0)
9411 continue;
9412
9413 _bfd_elf_swap_versym_in (input, ever, &iver);
9414
d023c380
L
9415 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9416 && !(h->def_regular
9417 && h->forced_local))
c152c796
AM
9418 {
9419 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9420 have provided a definition for the undefined sym unless
9421 it is defined in a non-shared object and forced local.
9422 */
c152c796
AM
9423 abort ();
9424 }
9425
9426 version_index = iver.vs_vers & VERSYM_VERSION;
9427 if (version_index == 1 || version_index == 2)
9428 {
9429 /* This is the base or first version. We can use it. */
9430 free (extversym);
9431 free (isymbuf);
9432 return TRUE;
9433 }
9434 }
9435
9436 free (extversym);
9437 free (isymbuf);
9438 }
9439
9440 return FALSE;
9441}
9442
b8871f35
L
9443/* Convert ELF common symbol TYPE. */
9444
9445static int
9446elf_link_convert_common_type (struct bfd_link_info *info, int type)
9447{
9448 /* Commom symbol can only appear in relocatable link. */
9449 if (!bfd_link_relocatable (info))
9450 abort ();
9451 switch (info->elf_stt_common)
9452 {
9453 case unchanged:
9454 break;
9455 case elf_stt_common:
9456 type = STT_COMMON;
9457 break;
9458 case no_elf_stt_common:
9459 type = STT_OBJECT;
9460 break;
9461 }
9462 return type;
9463}
9464
c152c796
AM
9465/* Add an external symbol to the symbol table. This is called from
9466 the hash table traversal routine. When generating a shared object,
9467 we go through the symbol table twice. The first time we output
9468 anything that might have been forced to local scope in a version
9469 script. The second time we output the symbols that are still
9470 global symbols. */
9471
9472static bfd_boolean
7686d77d 9473elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9474{
7686d77d 9475 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9476 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9477 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9478 bfd_boolean strip;
9479 Elf_Internal_Sym sym;
9480 asection *input_sec;
9481 const struct elf_backend_data *bed;
6e0b88f1
AM
9482 long indx;
9483 int ret;
b8871f35 9484 unsigned int type;
c152c796
AM
9485
9486 if (h->root.type == bfd_link_hash_warning)
9487 {
9488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9489 if (h->root.type == bfd_link_hash_new)
9490 return TRUE;
9491 }
9492
9493 /* Decide whether to output this symbol in this pass. */
9494 if (eoinfo->localsyms)
9495 {
4deb8f71 9496 if (!h->forced_local)
c152c796
AM
9497 return TRUE;
9498 }
9499 else
9500 {
4deb8f71 9501 if (h->forced_local)
c152c796
AM
9502 return TRUE;
9503 }
9504
8b127cbc 9505 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9506
12ac1cf5 9507 if (h->root.type == bfd_link_hash_undefined)
c152c796 9508 {
12ac1cf5
NC
9509 /* If we have an undefined symbol reference here then it must have
9510 come from a shared library that is being linked in. (Undefined
98da7939
L
9511 references in regular files have already been handled unless
9512 they are in unreferenced sections which are removed by garbage
9513 collection). */
12ac1cf5
NC
9514 bfd_boolean ignore_undef = FALSE;
9515
9516 /* Some symbols may be special in that the fact that they're
9517 undefined can be safely ignored - let backend determine that. */
9518 if (bed->elf_backend_ignore_undef_symbol)
9519 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9520
9521 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9522 if (!ignore_undef
12ac1cf5 9523 && h->ref_dynamic
8b127cbc
AM
9524 && (!h->ref_regular || flinfo->info->gc_sections)
9525 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9526 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9527 (*flinfo->info->callbacks->undefined_symbol)
9528 (flinfo->info, h->root.root.string,
9529 h->ref_regular ? NULL : h->root.u.undef.abfd,
9530 NULL, 0,
9531 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9532
9533 /* Strip a global symbol defined in a discarded section. */
9534 if (h->indx == -3)
9535 return TRUE;
c152c796
AM
9536 }
9537
9538 /* We should also warn if a forced local symbol is referenced from
9539 shared libraries. */
0e1862bb 9540 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9541 && h->forced_local
9542 && h->ref_dynamic
371a5866 9543 && h->def_regular
f5385ebf 9544 && !h->dynamic_def
ee659f1f 9545 && h->ref_dynamic_nonweak
8b127cbc 9546 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9547 {
17d078c5
AM
9548 bfd *def_bfd;
9549 const char *msg;
90c984fc
L
9550 struct elf_link_hash_entry *hi = h;
9551
9552 /* Check indirect symbol. */
9553 while (hi->root.type == bfd_link_hash_indirect)
9554 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9555
9556 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9557 /* xgettext:c-format */
17d078c5
AM
9558 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9559 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9560 /* xgettext:c-format */
17d078c5
AM
9561 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9562 else
695344c0 9563 /* xgettext:c-format */
17d078c5 9564 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9565 def_bfd = flinfo->output_bfd;
90c984fc
L
9566 if (hi->root.u.def.section != bfd_abs_section_ptr)
9567 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9568 _bfd_error_handler (msg, flinfo->output_bfd,
9569 h->root.root.string, def_bfd);
17d078c5 9570 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9571 eoinfo->failed = TRUE;
9572 return FALSE;
9573 }
9574
9575 /* We don't want to output symbols that have never been mentioned by
9576 a regular file, or that we have been told to strip. However, if
9577 h->indx is set to -2, the symbol is used by a reloc and we must
9578 output it. */
d983c8c5 9579 strip = FALSE;
c152c796 9580 if (h->indx == -2)
d983c8c5 9581 ;
f5385ebf 9582 else if ((h->def_dynamic
77cfaee6
AM
9583 || h->ref_dynamic
9584 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9585 && !h->def_regular
9586 && !h->ref_regular)
c152c796 9587 strip = TRUE;
8b127cbc 9588 else if (flinfo->info->strip == strip_all)
c152c796 9589 strip = TRUE;
8b127cbc
AM
9590 else if (flinfo->info->strip == strip_some
9591 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9592 h->root.root.string, FALSE, FALSE) == NULL)
9593 strip = TRUE;
d56d55e7
AM
9594 else if ((h->root.type == bfd_link_hash_defined
9595 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9596 && ((flinfo->info->strip_discarded
dbaa2011 9597 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9598 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9599 && h->root.u.def.section->owner != NULL
d56d55e7 9600 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9601 strip = TRUE;
9e2278f5
AM
9602 else if ((h->root.type == bfd_link_hash_undefined
9603 || h->root.type == bfd_link_hash_undefweak)
9604 && h->root.u.undef.abfd != NULL
9605 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9606 strip = TRUE;
c152c796 9607
b8871f35
L
9608 type = h->type;
9609
c152c796 9610 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9611 nothing else to do. However, if it is a forced local symbol or
9612 an ifunc symbol we need to give the backend finish_dynamic_symbol
9613 function a chance to make it dynamic. */
c152c796
AM
9614 if (strip
9615 && h->dynindx == -1
b8871f35 9616 && type != STT_GNU_IFUNC
f5385ebf 9617 && !h->forced_local)
c152c796
AM
9618 return TRUE;
9619
9620 sym.st_value = 0;
9621 sym.st_size = h->size;
9622 sym.st_other = h->other;
c152c796
AM
9623 switch (h->root.type)
9624 {
9625 default:
9626 case bfd_link_hash_new:
9627 case bfd_link_hash_warning:
9628 abort ();
9629 return FALSE;
9630
9631 case bfd_link_hash_undefined:
9632 case bfd_link_hash_undefweak:
9633 input_sec = bfd_und_section_ptr;
9634 sym.st_shndx = SHN_UNDEF;
9635 break;
9636
9637 case bfd_link_hash_defined:
9638 case bfd_link_hash_defweak:
9639 {
9640 input_sec = h->root.u.def.section;
9641 if (input_sec->output_section != NULL)
9642 {
9643 sym.st_shndx =
8b127cbc 9644 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9645 input_sec->output_section);
9646 if (sym.st_shndx == SHN_BAD)
9647 {
4eca0228 9648 _bfd_error_handler
695344c0 9649 /* xgettext:c-format */
d003868e 9650 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9651 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9652 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9653 eoinfo->failed = TRUE;
9654 return FALSE;
9655 }
9656
9657 /* ELF symbols in relocatable files are section relative,
9658 but in nonrelocatable files they are virtual
9659 addresses. */
9660 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9661 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9662 {
9663 sym.st_value += input_sec->output_section->vma;
9664 if (h->type == STT_TLS)
9665 {
8b127cbc 9666 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9667 if (tls_sec != NULL)
9668 sym.st_value -= tls_sec->vma;
c152c796
AM
9669 }
9670 }
9671 }
9672 else
9673 {
9674 BFD_ASSERT (input_sec->owner == NULL
9675 || (input_sec->owner->flags & DYNAMIC) != 0);
9676 sym.st_shndx = SHN_UNDEF;
9677 input_sec = bfd_und_section_ptr;
9678 }
9679 }
9680 break;
9681
9682 case bfd_link_hash_common:
9683 input_sec = h->root.u.c.p->section;
a4d8e49b 9684 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9685 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9686 break;
9687
9688 case bfd_link_hash_indirect:
9689 /* These symbols are created by symbol versioning. They point
9690 to the decorated version of the name. For example, if the
9691 symbol foo@@GNU_1.2 is the default, which should be used when
9692 foo is used with no version, then we add an indirect symbol
9693 foo which points to foo@@GNU_1.2. We ignore these symbols,
9694 since the indirected symbol is already in the hash table. */
9695 return TRUE;
9696 }
9697
b8871f35
L
9698 if (type == STT_COMMON || type == STT_OBJECT)
9699 switch (h->root.type)
9700 {
9701 case bfd_link_hash_common:
9702 type = elf_link_convert_common_type (flinfo->info, type);
9703 break;
9704 case bfd_link_hash_defined:
9705 case bfd_link_hash_defweak:
9706 if (bed->common_definition (&sym))
9707 type = elf_link_convert_common_type (flinfo->info, type);
9708 else
9709 type = STT_OBJECT;
9710 break;
9711 case bfd_link_hash_undefined:
9712 case bfd_link_hash_undefweak:
9713 break;
9714 default:
9715 abort ();
9716 }
9717
4deb8f71 9718 if (h->forced_local)
b8871f35
L
9719 {
9720 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9721 /* Turn off visibility on local symbol. */
9722 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9723 }
9724 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9725 else if (h->unique_global && h->def_regular)
9726 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9727 else if (h->root.type == bfd_link_hash_undefweak
9728 || h->root.type == bfd_link_hash_defweak)
9729 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9730 else
9731 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9732 sym.st_target_internal = h->target_internal;
9733
c152c796
AM
9734 /* Give the processor backend a chance to tweak the symbol value,
9735 and also to finish up anything that needs to be done for this
9736 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9737 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9738 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9739 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9740 && h->def_regular
0e1862bb 9741 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9742 || ((h->dynindx != -1
9743 || h->forced_local)
0e1862bb 9744 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9745 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9746 || h->root.type != bfd_link_hash_undefweak))
9747 || !h->forced_local)
8b127cbc 9748 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9749 {
9750 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9751 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9752 {
9753 eoinfo->failed = TRUE;
9754 return FALSE;
9755 }
9756 }
9757
9758 /* If we are marking the symbol as undefined, and there are no
9759 non-weak references to this symbol from a regular object, then
9760 mark the symbol as weak undefined; if there are non-weak
9761 references, mark the symbol as strong. We can't do this earlier,
9762 because it might not be marked as undefined until the
9763 finish_dynamic_symbol routine gets through with it. */
9764 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9765 && h->ref_regular
c152c796
AM
9766 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9767 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9768 {
9769 int bindtype;
b8871f35 9770 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9771
9772 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9773 if (type == STT_GNU_IFUNC)
9774 type = STT_FUNC;
c152c796 9775
f5385ebf 9776 if (h->ref_regular_nonweak)
c152c796
AM
9777 bindtype = STB_GLOBAL;
9778 else
9779 bindtype = STB_WEAK;
2955ec4c 9780 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9781 }
9782
bda987c2
CD
9783 /* If this is a symbol defined in a dynamic library, don't use the
9784 symbol size from the dynamic library. Relinking an executable
9785 against a new library may introduce gratuitous changes in the
9786 executable's symbols if we keep the size. */
9787 if (sym.st_shndx == SHN_UNDEF
9788 && !h->def_regular
9789 && h->def_dynamic)
9790 sym.st_size = 0;
9791
c152c796
AM
9792 /* If a non-weak symbol with non-default visibility is not defined
9793 locally, it is a fatal error. */
0e1862bb 9794 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9795 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9796 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9797 && h->root.type == bfd_link_hash_undefined
f5385ebf 9798 && !h->def_regular)
c152c796 9799 {
17d078c5
AM
9800 const char *msg;
9801
9802 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9803 /* xgettext:c-format */
17d078c5
AM
9804 msg = _("%B: protected symbol `%s' isn't defined");
9805 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9806 /* xgettext:c-format */
17d078c5
AM
9807 msg = _("%B: internal symbol `%s' isn't defined");
9808 else
695344c0 9809 /* xgettext:c-format */
17d078c5 9810 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9811 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9812 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9813 eoinfo->failed = TRUE;
9814 return FALSE;
9815 }
9816
9817 /* If this symbol should be put in the .dynsym section, then put it
9818 there now. We already know the symbol index. We also fill in
9819 the entry in the .hash section. */
cae1fbbb 9820 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9821 && h->dynindx != -1
8b127cbc 9822 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9823 {
c152c796
AM
9824 bfd_byte *esym;
9825
90c984fc
L
9826 /* Since there is no version information in the dynamic string,
9827 if there is no version info in symbol version section, we will
1659f720 9828 have a run-time problem if not linking executable, referenced
4deb8f71 9829 by shared library, or not bound locally. */
1659f720 9830 if (h->verinfo.verdef == NULL
0e1862bb 9831 && (!bfd_link_executable (flinfo->info)
1659f720
L
9832 || h->ref_dynamic
9833 || !h->def_regular))
90c984fc
L
9834 {
9835 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9836
9837 if (p && p [1] != '\0')
9838 {
4eca0228 9839 _bfd_error_handler
695344c0 9840 /* xgettext:c-format */
90c984fc
L
9841 (_("%B: No symbol version section for versioned symbol `%s'"),
9842 flinfo->output_bfd, h->root.root.string);
9843 eoinfo->failed = TRUE;
9844 return FALSE;
9845 }
9846 }
9847
c152c796 9848 sym.st_name = h->dynstr_index;
cae1fbbb
L
9849 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9850 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9851 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9852 {
9853 eoinfo->failed = TRUE;
9854 return FALSE;
9855 }
8b127cbc 9856 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9857
8b127cbc 9858 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9859 {
9860 size_t hash_entry_size;
9861 bfd_byte *bucketpos;
9862 bfd_vma chain;
41198d0c
L
9863 size_t bucketcount;
9864 size_t bucket;
9865
8b127cbc 9866 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9867 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9868
9869 hash_entry_size
8b127cbc
AM
9870 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9871 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9872 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9873 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9874 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9875 bucketpos);
9876 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9877 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9878 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9879 }
c152c796 9880
8b127cbc 9881 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9882 {
9883 Elf_Internal_Versym iversym;
9884 Elf_External_Versym *eversym;
9885
f5385ebf 9886 if (!h->def_regular)
c152c796 9887 {
7b20f099
AM
9888 if (h->verinfo.verdef == NULL
9889 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9890 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9891 iversym.vs_vers = 0;
9892 else
9893 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9894 }
9895 else
9896 {
9897 if (h->verinfo.vertree == NULL)
9898 iversym.vs_vers = 1;
9899 else
9900 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9901 if (flinfo->info->create_default_symver)
3e3b46e5 9902 iversym.vs_vers++;
c152c796
AM
9903 }
9904
422f1182 9905 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9906 defined locally. */
422f1182 9907 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9908 iversym.vs_vers |= VERSYM_HIDDEN;
9909
8b127cbc 9910 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9911 eversym += h->dynindx;
8b127cbc 9912 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9913 }
9914 }
9915
d983c8c5
AM
9916 /* If the symbol is undefined, and we didn't output it to .dynsym,
9917 strip it from .symtab too. Obviously we can't do this for
9918 relocatable output or when needed for --emit-relocs. */
9919 else if (input_sec == bfd_und_section_ptr
9920 && h->indx != -2
66cae560
NC
9921 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9922 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 9923 && !bfd_link_relocatable (flinfo->info))
d983c8c5 9924 return TRUE;
66cae560 9925
d983c8c5
AM
9926 /* Also strip others that we couldn't earlier due to dynamic symbol
9927 processing. */
9928 if (strip)
9929 return TRUE;
9930 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9931 return TRUE;
9932
2ec55de3
AM
9933 /* Output a FILE symbol so that following locals are not associated
9934 with the wrong input file. We need one for forced local symbols
9935 if we've seen more than one FILE symbol or when we have exactly
9936 one FILE symbol but global symbols are present in a file other
9937 than the one with the FILE symbol. We also need one if linker
9938 defined symbols are present. In practice these conditions are
9939 always met, so just emit the FILE symbol unconditionally. */
9940 if (eoinfo->localsyms
9941 && !eoinfo->file_sym_done
9942 && eoinfo->flinfo->filesym_count != 0)
9943 {
9944 Elf_Internal_Sym fsym;
9945
9946 memset (&fsym, 0, sizeof (fsym));
9947 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9948 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9949 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9950 bfd_und_section_ptr, NULL))
2ec55de3
AM
9951 return FALSE;
9952
9953 eoinfo->file_sym_done = TRUE;
9954 }
9955
8b127cbc 9956 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9957 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9958 input_sec, h);
6e0b88f1 9959 if (ret == 0)
c152c796
AM
9960 {
9961 eoinfo->failed = TRUE;
9962 return FALSE;
9963 }
6e0b88f1
AM
9964 else if (ret == 1)
9965 h->indx = indx;
9966 else if (h->indx == -2)
9967 abort();
c152c796
AM
9968
9969 return TRUE;
9970}
9971
cdd3575c
AM
9972/* Return TRUE if special handling is done for relocs in SEC against
9973 symbols defined in discarded sections. */
9974
c152c796
AM
9975static bfd_boolean
9976elf_section_ignore_discarded_relocs (asection *sec)
9977{
9978 const struct elf_backend_data *bed;
9979
cdd3575c
AM
9980 switch (sec->sec_info_type)
9981 {
dbaa2011
AM
9982 case SEC_INFO_TYPE_STABS:
9983 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9984 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9985 return TRUE;
9986 default:
9987 break;
9988 }
c152c796
AM
9989
9990 bed = get_elf_backend_data (sec->owner);
9991 if (bed->elf_backend_ignore_discarded_relocs != NULL
9992 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9993 return TRUE;
9994
9995 return FALSE;
9996}
9997
9e66c942
AM
9998/* Return a mask saying how ld should treat relocations in SEC against
9999 symbols defined in discarded sections. If this function returns
10000 COMPLAIN set, ld will issue a warning message. If this function
10001 returns PRETEND set, and the discarded section was link-once and the
10002 same size as the kept link-once section, ld will pretend that the
10003 symbol was actually defined in the kept section. Otherwise ld will
10004 zero the reloc (at least that is the intent, but some cooperation by
10005 the target dependent code is needed, particularly for REL targets). */
10006
8a696751
AM
10007unsigned int
10008_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10009{
9e66c942 10010 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10011 return PRETEND;
cdd3575c
AM
10012
10013 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10014 return 0;
cdd3575c
AM
10015
10016 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10017 return 0;
cdd3575c 10018
9e66c942 10019 return COMPLAIN | PRETEND;
cdd3575c
AM
10020}
10021
3d7f7666
L
10022/* Find a match between a section and a member of a section group. */
10023
10024static asection *
c0f00686
L
10025match_group_member (asection *sec, asection *group,
10026 struct bfd_link_info *info)
3d7f7666
L
10027{
10028 asection *first = elf_next_in_group (group);
10029 asection *s = first;
10030
10031 while (s != NULL)
10032 {
c0f00686 10033 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10034 return s;
10035
83180ade 10036 s = elf_next_in_group (s);
3d7f7666
L
10037 if (s == first)
10038 break;
10039 }
10040
10041 return NULL;
10042}
10043
01b3c8ab 10044/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10045 to replace it. Return the replacement if it is OK. Otherwise return
10046 NULL. */
01b3c8ab
L
10047
10048asection *
c0f00686 10049_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10050{
10051 asection *kept;
10052
10053 kept = sec->kept_section;
10054 if (kept != NULL)
10055 {
c2370991 10056 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10057 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10058 if (kept != NULL
10059 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10060 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10061 kept = NULL;
c2370991 10062 sec->kept_section = kept;
01b3c8ab
L
10063 }
10064 return kept;
10065}
10066
c152c796
AM
10067/* Link an input file into the linker output file. This function
10068 handles all the sections and relocations of the input file at once.
10069 This is so that we only have to read the local symbols once, and
10070 don't have to keep them in memory. */
10071
10072static bfd_boolean
8b127cbc 10073elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10074{
ece5ef60 10075 int (*relocate_section)
c152c796
AM
10076 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10077 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10078 bfd *output_bfd;
10079 Elf_Internal_Shdr *symtab_hdr;
10080 size_t locsymcount;
10081 size_t extsymoff;
10082 Elf_Internal_Sym *isymbuf;
10083 Elf_Internal_Sym *isym;
10084 Elf_Internal_Sym *isymend;
10085 long *pindex;
10086 asection **ppsection;
10087 asection *o;
10088 const struct elf_backend_data *bed;
c152c796 10089 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10090 bfd_size_type address_size;
10091 bfd_vma r_type_mask;
10092 int r_sym_shift;
ffbc01cc 10093 bfd_boolean have_file_sym = FALSE;
c152c796 10094
8b127cbc 10095 output_bfd = flinfo->output_bfd;
c152c796
AM
10096 bed = get_elf_backend_data (output_bfd);
10097 relocate_section = bed->elf_backend_relocate_section;
10098
10099 /* If this is a dynamic object, we don't want to do anything here:
10100 we don't want the local symbols, and we don't want the section
10101 contents. */
10102 if ((input_bfd->flags & DYNAMIC) != 0)
10103 return TRUE;
10104
c152c796
AM
10105 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10106 if (elf_bad_symtab (input_bfd))
10107 {
10108 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10109 extsymoff = 0;
10110 }
10111 else
10112 {
10113 locsymcount = symtab_hdr->sh_info;
10114 extsymoff = symtab_hdr->sh_info;
10115 }
10116
10117 /* Read the local symbols. */
10118 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10119 if (isymbuf == NULL && locsymcount != 0)
10120 {
10121 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10122 flinfo->internal_syms,
10123 flinfo->external_syms,
10124 flinfo->locsym_shndx);
c152c796
AM
10125 if (isymbuf == NULL)
10126 return FALSE;
10127 }
10128
10129 /* Find local symbol sections and adjust values of symbols in
10130 SEC_MERGE sections. Write out those local symbols we know are
10131 going into the output file. */
10132 isymend = isymbuf + locsymcount;
8b127cbc 10133 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10134 isym < isymend;
10135 isym++, pindex++, ppsection++)
10136 {
10137 asection *isec;
10138 const char *name;
10139 Elf_Internal_Sym osym;
6e0b88f1
AM
10140 long indx;
10141 int ret;
c152c796
AM
10142
10143 *pindex = -1;
10144
10145 if (elf_bad_symtab (input_bfd))
10146 {
10147 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10148 {
10149 *ppsection = NULL;
10150 continue;
10151 }
10152 }
10153
10154 if (isym->st_shndx == SHN_UNDEF)
10155 isec = bfd_und_section_ptr;
c152c796
AM
10156 else if (isym->st_shndx == SHN_ABS)
10157 isec = bfd_abs_section_ptr;
10158 else if (isym->st_shndx == SHN_COMMON)
10159 isec = bfd_com_section_ptr;
10160 else
10161 {
cb33740c
AM
10162 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10163 if (isec == NULL)
10164 {
10165 /* Don't attempt to output symbols with st_shnx in the
10166 reserved range other than SHN_ABS and SHN_COMMON. */
10167 *ppsection = NULL;
10168 continue;
10169 }
dbaa2011 10170 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10171 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10172 isym->st_value =
10173 _bfd_merged_section_offset (output_bfd, &isec,
10174 elf_section_data (isec)->sec_info,
10175 isym->st_value);
c152c796
AM
10176 }
10177
10178 *ppsection = isec;
10179
d983c8c5
AM
10180 /* Don't output the first, undefined, symbol. In fact, don't
10181 output any undefined local symbol. */
10182 if (isec == bfd_und_section_ptr)
c152c796
AM
10183 continue;
10184
10185 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10186 {
10187 /* We never output section symbols. Instead, we use the
10188 section symbol of the corresponding section in the output
10189 file. */
10190 continue;
10191 }
10192
10193 /* If we are stripping all symbols, we don't want to output this
10194 one. */
8b127cbc 10195 if (flinfo->info->strip == strip_all)
c152c796
AM
10196 continue;
10197
10198 /* If we are discarding all local symbols, we don't want to
10199 output this one. If we are generating a relocatable output
10200 file, then some of the local symbols may be required by
10201 relocs; we output them below as we discover that they are
10202 needed. */
8b127cbc 10203 if (flinfo->info->discard == discard_all)
c152c796
AM
10204 continue;
10205
10206 /* If this symbol is defined in a section which we are
f02571c5
AM
10207 discarding, we don't need to keep it. */
10208 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10209 && isym->st_shndx < SHN_LORESERVE
10210 && bfd_section_removed_from_list (output_bfd,
10211 isec->output_section))
e75a280b
L
10212 continue;
10213
c152c796
AM
10214 /* Get the name of the symbol. */
10215 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10216 isym->st_name);
10217 if (name == NULL)
10218 return FALSE;
10219
10220 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10221 if ((flinfo->info->strip == strip_some
10222 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10223 == NULL))
8b127cbc 10224 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10225 && (isec->flags & SEC_MERGE)
10226 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10227 || flinfo->info->discard == discard_l)
c152c796
AM
10228 && bfd_is_local_label_name (input_bfd, name)))
10229 continue;
10230
ffbc01cc
AM
10231 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10232 {
ce875075
AM
10233 if (input_bfd->lto_output)
10234 /* -flto puts a temp file name here. This means builds
10235 are not reproducible. Discard the symbol. */
10236 continue;
ffbc01cc
AM
10237 have_file_sym = TRUE;
10238 flinfo->filesym_count += 1;
10239 }
10240 if (!have_file_sym)
10241 {
10242 /* In the absence of debug info, bfd_find_nearest_line uses
10243 FILE symbols to determine the source file for local
10244 function symbols. Provide a FILE symbol here if input
10245 files lack such, so that their symbols won't be
10246 associated with a previous input file. It's not the
10247 source file, but the best we can do. */
10248 have_file_sym = TRUE;
10249 flinfo->filesym_count += 1;
10250 memset (&osym, 0, sizeof (osym));
10251 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10252 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10253 if (!elf_link_output_symstrtab (flinfo,
10254 (input_bfd->lto_output ? NULL
10255 : input_bfd->filename),
10256 &osym, bfd_abs_section_ptr,
10257 NULL))
ffbc01cc
AM
10258 return FALSE;
10259 }
10260
c152c796
AM
10261 osym = *isym;
10262
10263 /* Adjust the section index for the output file. */
10264 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10265 isec->output_section);
10266 if (osym.st_shndx == SHN_BAD)
10267 return FALSE;
10268
c152c796
AM
10269 /* ELF symbols in relocatable files are section relative, but
10270 in executable files they are virtual addresses. Note that
10271 this code assumes that all ELF sections have an associated
10272 BFD section with a reasonable value for output_offset; below
10273 we assume that they also have a reasonable value for
10274 output_section. Any special sections must be set up to meet
10275 these requirements. */
10276 osym.st_value += isec->output_offset;
0e1862bb 10277 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10278 {
10279 osym.st_value += isec->output_section->vma;
10280 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10281 {
10282 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10283 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10284 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10285 }
10286 }
10287
6e0b88f1 10288 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10289 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10290 if (ret == 0)
c152c796 10291 return FALSE;
6e0b88f1
AM
10292 else if (ret == 1)
10293 *pindex = indx;
c152c796
AM
10294 }
10295
310fd250
L
10296 if (bed->s->arch_size == 32)
10297 {
10298 r_type_mask = 0xff;
10299 r_sym_shift = 8;
10300 address_size = 4;
10301 }
10302 else
10303 {
10304 r_type_mask = 0xffffffff;
10305 r_sym_shift = 32;
10306 address_size = 8;
10307 }
10308
c152c796
AM
10309 /* Relocate the contents of each section. */
10310 sym_hashes = elf_sym_hashes (input_bfd);
10311 for (o = input_bfd->sections; o != NULL; o = o->next)
10312 {
10313 bfd_byte *contents;
10314
10315 if (! o->linker_mark)
10316 {
10317 /* This section was omitted from the link. */
10318 continue;
10319 }
10320
7bdf4127 10321 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10322 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10323 {
10324 /* Deal with the group signature symbol. */
10325 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10326 unsigned long symndx = sec_data->this_hdr.sh_info;
10327 asection *osec = o->output_section;
10328
7bdf4127 10329 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10330 if (symndx >= locsymcount
10331 || (elf_bad_symtab (input_bfd)
8b127cbc 10332 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10333 {
10334 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10335 while (h->root.type == bfd_link_hash_indirect
10336 || h->root.type == bfd_link_hash_warning)
10337 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10338 /* Arrange for symbol to be output. */
10339 h->indx = -2;
10340 elf_section_data (osec)->this_hdr.sh_info = -2;
10341 }
10342 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10343 {
10344 /* We'll use the output section target_index. */
8b127cbc 10345 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10346 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10347 }
10348 else
10349 {
8b127cbc 10350 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10351 {
10352 /* Otherwise output the local symbol now. */
10353 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10354 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10355 const char *name;
6e0b88f1
AM
10356 long indx;
10357 int ret;
bcacc0f5
AM
10358
10359 name = bfd_elf_string_from_elf_section (input_bfd,
10360 symtab_hdr->sh_link,
10361 sym.st_name);
10362 if (name == NULL)
10363 return FALSE;
10364
10365 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10366 sec);
10367 if (sym.st_shndx == SHN_BAD)
10368 return FALSE;
10369
10370 sym.st_value += o->output_offset;
10371
6e0b88f1 10372 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10373 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10374 NULL);
6e0b88f1 10375 if (ret == 0)
bcacc0f5 10376 return FALSE;
6e0b88f1 10377 else if (ret == 1)
8b127cbc 10378 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10379 else
10380 abort ();
bcacc0f5
AM
10381 }
10382 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10383 = flinfo->indices[symndx];
bcacc0f5
AM
10384 }
10385 }
10386
c152c796 10387 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10388 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10389 continue;
10390
10391 if ((o->flags & SEC_LINKER_CREATED) != 0)
10392 {
10393 /* Section was created by _bfd_elf_link_create_dynamic_sections
10394 or somesuch. */
10395 continue;
10396 }
10397
10398 /* Get the contents of the section. They have been cached by a
10399 relaxation routine. Note that o is a section in an input
10400 file, so the contents field will not have been set by any of
10401 the routines which work on output files. */
10402 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10403 {
10404 contents = elf_section_data (o)->this_hdr.contents;
10405 if (bed->caches_rawsize
10406 && o->rawsize != 0
10407 && o->rawsize < o->size)
10408 {
10409 memcpy (flinfo->contents, contents, o->rawsize);
10410 contents = flinfo->contents;
10411 }
10412 }
c152c796
AM
10413 else
10414 {
8b127cbc 10415 contents = flinfo->contents;
4a114e3e 10416 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10417 return FALSE;
10418 }
10419
10420 if ((o->flags & SEC_RELOC) != 0)
10421 {
10422 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10423 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10424 int action_discarded;
ece5ef60 10425 int ret;
c152c796
AM
10426
10427 /* Get the swapped relocs. */
10428 internal_relocs
8b127cbc
AM
10429 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10430 flinfo->internal_relocs, FALSE);
c152c796
AM
10431 if (internal_relocs == NULL
10432 && o->reloc_count > 0)
10433 return FALSE;
10434
310fd250
L
10435 /* We need to reverse-copy input .ctors/.dtors sections if
10436 they are placed in .init_array/.finit_array for output. */
10437 if (o->size > address_size
10438 && ((strncmp (o->name, ".ctors", 6) == 0
10439 && strcmp (o->output_section->name,
10440 ".init_array") == 0)
10441 || (strncmp (o->name, ".dtors", 6) == 0
10442 && strcmp (o->output_section->name,
10443 ".fini_array") == 0))
10444 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10445 {
056bafd4
MR
10446 if (o->size * bed->s->int_rels_per_ext_rel
10447 != o->reloc_count * address_size)
310fd250 10448 {
4eca0228 10449 _bfd_error_handler
695344c0 10450 /* xgettext:c-format */
310fd250
L
10451 (_("error: %B: size of section %A is not "
10452 "multiple of address size"),
10453 input_bfd, o);
8c6716e5 10454 bfd_set_error (bfd_error_bad_value);
310fd250
L
10455 return FALSE;
10456 }
10457 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10458 }
10459
0f02bbd9 10460 action_discarded = -1;
c152c796 10461 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10462 action_discarded = (*bed->action_discarded) (o);
10463
10464 /* Run through the relocs evaluating complex reloc symbols and
10465 looking for relocs against symbols from discarded sections
10466 or section symbols from removed link-once sections.
10467 Complain about relocs against discarded sections. Zero
10468 relocs against removed link-once sections. */
10469
10470 rel = internal_relocs;
056bafd4 10471 relend = rel + o->reloc_count;
0f02bbd9 10472 for ( ; rel < relend; rel++)
c152c796 10473 {
0f02bbd9
AM
10474 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10475 unsigned int s_type;
10476 asection **ps, *sec;
10477 struct elf_link_hash_entry *h = NULL;
10478 const char *sym_name;
c152c796 10479
0f02bbd9
AM
10480 if (r_symndx == STN_UNDEF)
10481 continue;
c152c796 10482
0f02bbd9
AM
10483 if (r_symndx >= locsymcount
10484 || (elf_bad_symtab (input_bfd)
8b127cbc 10485 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10486 {
10487 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10488
0f02bbd9
AM
10489 /* Badly formatted input files can contain relocs that
10490 reference non-existant symbols. Check here so that
10491 we do not seg fault. */
10492 if (h == NULL)
c152c796 10493 {
4eca0228 10494 _bfd_error_handler
695344c0 10495 /* xgettext:c-format */
76cfced5 10496 (_("error: %B contains a reloc (%#Lx) for section %A "
0f02bbd9 10497 "that references a non-existent global symbol"),
76cfced5 10498 input_bfd, rel->r_info, o);
0f02bbd9
AM
10499 bfd_set_error (bfd_error_bad_value);
10500 return FALSE;
10501 }
3b36f7e6 10502
0f02bbd9
AM
10503 while (h->root.type == bfd_link_hash_indirect
10504 || h->root.type == bfd_link_hash_warning)
10505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10506
0f02bbd9 10507 s_type = h->type;
cdd3575c 10508
9e2dec47 10509 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10510 mark the symbol as undefined. Note that the
10511 linker may attach linker created dynamic sections
10512 to the plugin bfd. Symbols defined in linker
10513 created sections are not plugin symbols. */
bc4e12de 10514 if ((h->root.non_ir_ref_regular
4070765b 10515 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10516 && (h->root.type == bfd_link_hash_defined
10517 || h->root.type == bfd_link_hash_defweak)
10518 && (h->root.u.def.section->flags
10519 & SEC_LINKER_CREATED) == 0
10520 && h->root.u.def.section->owner != NULL
10521 && (h->root.u.def.section->owner->flags
10522 & BFD_PLUGIN) != 0)
10523 {
10524 h->root.type = bfd_link_hash_undefined;
10525 h->root.u.undef.abfd = h->root.u.def.section->owner;
10526 }
10527
0f02bbd9
AM
10528 ps = NULL;
10529 if (h->root.type == bfd_link_hash_defined
10530 || h->root.type == bfd_link_hash_defweak)
10531 ps = &h->root.u.def.section;
10532
10533 sym_name = h->root.root.string;
10534 }
10535 else
10536 {
10537 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10538
10539 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10540 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10541 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10542 sym, *ps);
10543 }
c152c796 10544
c301e700 10545 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10546 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10547 {
10548 bfd_vma val;
10549 bfd_vma dot = (rel->r_offset
10550 + o->output_offset + o->output_section->vma);
10551#ifdef DEBUG
10552 printf ("Encountered a complex symbol!");
10553 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10554 input_bfd->filename, o->name,
10555 (long) (rel - internal_relocs));
0f02bbd9
AM
10556 printf (" symbol: idx %8.8lx, name %s\n",
10557 r_symndx, sym_name);
10558 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10559 (unsigned long) rel->r_info,
10560 (unsigned long) rel->r_offset);
10561#endif
8b127cbc 10562 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10563 isymbuf, locsymcount, s_type == STT_SRELC))
10564 return FALSE;
10565
10566 /* Symbol evaluated OK. Update to absolute value. */
10567 set_symbol_value (input_bfd, isymbuf, locsymcount,
10568 r_symndx, val);
10569 continue;
10570 }
10571
10572 if (action_discarded != -1 && ps != NULL)
10573 {
cdd3575c
AM
10574 /* Complain if the definition comes from a
10575 discarded section. */
dbaa2011 10576 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10577 {
cf35638d 10578 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10579 if (action_discarded & COMPLAIN)
8b127cbc 10580 (*flinfo->info->callbacks->einfo)
695344c0 10581 /* xgettext:c-format */
e1fffbe6 10582 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10583 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10584 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10585
87e5235d 10586 /* Try to do the best we can to support buggy old
e0ae6d6f 10587 versions of gcc. Pretend that the symbol is
87e5235d
AM
10588 really defined in the kept linkonce section.
10589 FIXME: This is quite broken. Modifying the
10590 symbol here means we will be changing all later
e0ae6d6f 10591 uses of the symbol, not just in this section. */
0f02bbd9 10592 if (action_discarded & PRETEND)
87e5235d 10593 {
01b3c8ab
L
10594 asection *kept;
10595
c0f00686 10596 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10597 flinfo->info);
01b3c8ab 10598 if (kept != NULL)
87e5235d
AM
10599 {
10600 *ps = kept;
10601 continue;
10602 }
10603 }
c152c796
AM
10604 }
10605 }
10606 }
10607
10608 /* Relocate the section by invoking a back end routine.
10609
10610 The back end routine is responsible for adjusting the
10611 section contents as necessary, and (if using Rela relocs
10612 and generating a relocatable output file) adjusting the
10613 reloc addend as necessary.
10614
10615 The back end routine does not have to worry about setting
10616 the reloc address or the reloc symbol index.
10617
10618 The back end routine is given a pointer to the swapped in
10619 internal symbols, and can access the hash table entries
10620 for the external symbols via elf_sym_hashes (input_bfd).
10621
10622 When generating relocatable output, the back end routine
10623 must handle STB_LOCAL/STT_SECTION symbols specially. The
10624 output symbol is going to be a section symbol
10625 corresponding to the output section, which will require
10626 the addend to be adjusted. */
10627
8b127cbc 10628 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10629 input_bfd, o, contents,
10630 internal_relocs,
10631 isymbuf,
8b127cbc 10632 flinfo->sections);
ece5ef60 10633 if (!ret)
c152c796
AM
10634 return FALSE;
10635
ece5ef60 10636 if (ret == 2
0e1862bb 10637 || bfd_link_relocatable (flinfo->info)
8b127cbc 10638 || flinfo->info->emitrelocations)
c152c796
AM
10639 {
10640 Elf_Internal_Rela *irela;
d4730f92 10641 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10642 bfd_vma last_offset;
10643 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10644 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10645 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10646 unsigned int next_erel;
c152c796 10647 bfd_boolean rela_normal;
d4730f92 10648 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10649
d4730f92
BS
10650 esdi = elf_section_data (o);
10651 esdo = elf_section_data (o->output_section);
10652 rela_normal = FALSE;
c152c796
AM
10653
10654 /* Adjust the reloc addresses and symbol indices. */
10655
10656 irela = internal_relocs;
056bafd4 10657 irelaend = irela + o->reloc_count;
d4730f92
BS
10658 rel_hash = esdo->rel.hashes + esdo->rel.count;
10659 /* We start processing the REL relocs, if any. When we reach
10660 IRELAMID in the loop, we switch to the RELA relocs. */
10661 irelamid = irela;
10662 if (esdi->rel.hdr != NULL)
10663 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10664 * bed->s->int_rels_per_ext_rel);
eac338cf 10665 rel_hash_list = rel_hash;
d4730f92 10666 rela_hash_list = NULL;
c152c796 10667 last_offset = o->output_offset;
0e1862bb 10668 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10669 last_offset += o->output_section->vma;
10670 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10671 {
10672 unsigned long r_symndx;
10673 asection *sec;
10674 Elf_Internal_Sym sym;
10675
10676 if (next_erel == bed->s->int_rels_per_ext_rel)
10677 {
10678 rel_hash++;
10679 next_erel = 0;
10680 }
10681
d4730f92
BS
10682 if (irela == irelamid)
10683 {
10684 rel_hash = esdo->rela.hashes + esdo->rela.count;
10685 rela_hash_list = rel_hash;
10686 rela_normal = bed->rela_normal;
10687 }
10688
c152c796 10689 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10690 flinfo->info, o,
c152c796
AM
10691 irela->r_offset);
10692 if (irela->r_offset >= (bfd_vma) -2)
10693 {
10694 /* This is a reloc for a deleted entry or somesuch.
10695 Turn it into an R_*_NONE reloc, at the same
10696 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10697 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10698 being ordered. */
10699 irela->r_offset = last_offset;
10700 irela->r_info = 0;
10701 irela->r_addend = 0;
10702 continue;
10703 }
10704
10705 irela->r_offset += o->output_offset;
10706
10707 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10708 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10709 irela->r_offset += o->output_section->vma;
10710
10711 last_offset = irela->r_offset;
10712
10713 r_symndx = irela->r_info >> r_sym_shift;
10714 if (r_symndx == STN_UNDEF)
10715 continue;
10716
10717 if (r_symndx >= locsymcount
10718 || (elf_bad_symtab (input_bfd)
8b127cbc 10719 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10720 {
10721 struct elf_link_hash_entry *rh;
10722 unsigned long indx;
10723
10724 /* This is a reloc against a global symbol. We
10725 have not yet output all the local symbols, so
10726 we do not know the symbol index of any global
10727 symbol. We set the rel_hash entry for this
10728 reloc to point to the global hash table entry
10729 for this symbol. The symbol index is then
ee75fd95 10730 set at the end of bfd_elf_final_link. */
c152c796
AM
10731 indx = r_symndx - extsymoff;
10732 rh = elf_sym_hashes (input_bfd)[indx];
10733 while (rh->root.type == bfd_link_hash_indirect
10734 || rh->root.type == bfd_link_hash_warning)
10735 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10736
10737 /* Setting the index to -2 tells
10738 elf_link_output_extsym that this symbol is
10739 used by a reloc. */
10740 BFD_ASSERT (rh->indx < 0);
10741 rh->indx = -2;
c152c796
AM
10742 *rel_hash = rh;
10743
10744 continue;
10745 }
10746
10747 /* This is a reloc against a local symbol. */
10748
10749 *rel_hash = NULL;
10750 sym = isymbuf[r_symndx];
8b127cbc 10751 sec = flinfo->sections[r_symndx];
c152c796
AM
10752 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10753 {
10754 /* I suppose the backend ought to fill in the
10755 section of any STT_SECTION symbol against a
6a8d1586 10756 processor specific section. */
cf35638d 10757 r_symndx = STN_UNDEF;
6a8d1586
AM
10758 if (bfd_is_abs_section (sec))
10759 ;
c152c796
AM
10760 else if (sec == NULL || sec->owner == NULL)
10761 {
10762 bfd_set_error (bfd_error_bad_value);
10763 return FALSE;
10764 }
10765 else
10766 {
6a8d1586
AM
10767 asection *osec = sec->output_section;
10768
10769 /* If we have discarded a section, the output
10770 section will be the absolute section. In
ab96bf03
AM
10771 case of discarded SEC_MERGE sections, use
10772 the kept section. relocate_section should
10773 have already handled discarded linkonce
10774 sections. */
6a8d1586
AM
10775 if (bfd_is_abs_section (osec)
10776 && sec->kept_section != NULL
10777 && sec->kept_section->output_section != NULL)
10778 {
10779 osec = sec->kept_section->output_section;
10780 irela->r_addend -= osec->vma;
10781 }
10782
10783 if (!bfd_is_abs_section (osec))
10784 {
10785 r_symndx = osec->target_index;
cf35638d 10786 if (r_symndx == STN_UNDEF)
74541ad4 10787 {
051d833a
AM
10788 irela->r_addend += osec->vma;
10789 osec = _bfd_nearby_section (output_bfd, osec,
10790 osec->vma);
10791 irela->r_addend -= osec->vma;
10792 r_symndx = osec->target_index;
74541ad4 10793 }
6a8d1586 10794 }
c152c796
AM
10795 }
10796
10797 /* Adjust the addend according to where the
10798 section winds up in the output section. */
10799 if (rela_normal)
10800 irela->r_addend += sec->output_offset;
10801 }
10802 else
10803 {
8b127cbc 10804 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10805 {
10806 unsigned long shlink;
10807 const char *name;
10808 asection *osec;
6e0b88f1 10809 long indx;
c152c796 10810
8b127cbc 10811 if (flinfo->info->strip == strip_all)
c152c796
AM
10812 {
10813 /* You can't do ld -r -s. */
10814 bfd_set_error (bfd_error_invalid_operation);
10815 return FALSE;
10816 }
10817
10818 /* This symbol was skipped earlier, but
10819 since it is needed by a reloc, we
10820 must output it now. */
10821 shlink = symtab_hdr->sh_link;
10822 name = (bfd_elf_string_from_elf_section
10823 (input_bfd, shlink, sym.st_name));
10824 if (name == NULL)
10825 return FALSE;
10826
10827 osec = sec->output_section;
10828 sym.st_shndx =
10829 _bfd_elf_section_from_bfd_section (output_bfd,
10830 osec);
10831 if (sym.st_shndx == SHN_BAD)
10832 return FALSE;
10833
10834 sym.st_value += sec->output_offset;
0e1862bb 10835 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10836 {
10837 sym.st_value += osec->vma;
10838 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10839 {
10840 /* STT_TLS symbols are relative to PT_TLS
10841 segment base. */
8b127cbc 10842 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10843 ->tls_sec != NULL);
8b127cbc 10844 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10845 ->tls_sec->vma);
10846 }
10847 }
10848
6e0b88f1 10849 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10850 ret = elf_link_output_symstrtab (flinfo, name,
10851 &sym, sec,
10852 NULL);
6e0b88f1 10853 if (ret == 0)
c152c796 10854 return FALSE;
6e0b88f1 10855 else if (ret == 1)
8b127cbc 10856 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10857 else
10858 abort ();
c152c796
AM
10859 }
10860
8b127cbc 10861 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10862 }
10863
10864 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10865 | (irela->r_info & r_type_mask));
10866 }
10867
10868 /* Swap out the relocs. */
d4730f92
BS
10869 input_rel_hdr = esdi->rel.hdr;
10870 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10871 {
d4730f92
BS
10872 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10873 input_rel_hdr,
10874 internal_relocs,
10875 rel_hash_list))
10876 return FALSE;
c152c796
AM
10877 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10878 * bed->s->int_rels_per_ext_rel);
eac338cf 10879 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10880 }
10881
10882 input_rela_hdr = esdi->rela.hdr;
10883 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10884 {
eac338cf 10885 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10886 input_rela_hdr,
eac338cf 10887 internal_relocs,
d4730f92 10888 rela_hash_list))
c152c796
AM
10889 return FALSE;
10890 }
10891 }
10892 }
10893
10894 /* Write out the modified section contents. */
10895 if (bed->elf_backend_write_section
8b127cbc 10896 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10897 contents))
c152c796
AM
10898 {
10899 /* Section written out. */
10900 }
10901 else switch (o->sec_info_type)
10902 {
dbaa2011 10903 case SEC_INFO_TYPE_STABS:
c152c796
AM
10904 if (! (_bfd_write_section_stabs
10905 (output_bfd,
8b127cbc 10906 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10907 o, &elf_section_data (o)->sec_info, contents)))
10908 return FALSE;
10909 break;
dbaa2011 10910 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10911 if (! _bfd_write_merged_section (output_bfd, o,
10912 elf_section_data (o)->sec_info))
10913 return FALSE;
10914 break;
dbaa2011 10915 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10916 {
8b127cbc 10917 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10918 o, contents))
10919 return FALSE;
10920 }
10921 break;
2f0c68f2
CM
10922 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10923 {
10924 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10925 flinfo->info,
10926 o, contents))
10927 return FALSE;
10928 }
10929 break;
c152c796
AM
10930 default:
10931 {
310fd250
L
10932 if (! (o->flags & SEC_EXCLUDE))
10933 {
10934 file_ptr offset = (file_ptr) o->output_offset;
10935 bfd_size_type todo = o->size;
37b01f6a
DG
10936
10937 offset *= bfd_octets_per_byte (output_bfd);
10938
310fd250
L
10939 if ((o->flags & SEC_ELF_REVERSE_COPY))
10940 {
10941 /* Reverse-copy input section to output. */
10942 do
10943 {
10944 todo -= address_size;
10945 if (! bfd_set_section_contents (output_bfd,
10946 o->output_section,
10947 contents + todo,
10948 offset,
10949 address_size))
10950 return FALSE;
10951 if (todo == 0)
10952 break;
10953 offset += address_size;
10954 }
10955 while (1);
10956 }
10957 else if (! bfd_set_section_contents (output_bfd,
10958 o->output_section,
10959 contents,
10960 offset, todo))
10961 return FALSE;
10962 }
c152c796
AM
10963 }
10964 break;
10965 }
10966 }
10967
10968 return TRUE;
10969}
10970
10971/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10972 requested by the linker, and does not come from any input file. This
c152c796
AM
10973 is used to build constructor and destructor tables when linking
10974 with -Ur. */
10975
10976static bfd_boolean
10977elf_reloc_link_order (bfd *output_bfd,
10978 struct bfd_link_info *info,
10979 asection *output_section,
10980 struct bfd_link_order *link_order)
10981{
10982 reloc_howto_type *howto;
10983 long indx;
10984 bfd_vma offset;
10985 bfd_vma addend;
d4730f92 10986 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10987 struct elf_link_hash_entry **rel_hash_ptr;
10988 Elf_Internal_Shdr *rel_hdr;
10989 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10990 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10991 bfd_byte *erel;
10992 unsigned int i;
d4730f92 10993 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10994
10995 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10996 if (howto == NULL)
10997 {
10998 bfd_set_error (bfd_error_bad_value);
10999 return FALSE;
11000 }
11001
11002 addend = link_order->u.reloc.p->addend;
11003
d4730f92
BS
11004 if (esdo->rel.hdr)
11005 reldata = &esdo->rel;
11006 else if (esdo->rela.hdr)
11007 reldata = &esdo->rela;
11008 else
11009 {
11010 reldata = NULL;
11011 BFD_ASSERT (0);
11012 }
11013
c152c796 11014 /* Figure out the symbol index. */
d4730f92 11015 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11016 if (link_order->type == bfd_section_reloc_link_order)
11017 {
11018 indx = link_order->u.reloc.p->u.section->target_index;
11019 BFD_ASSERT (indx != 0);
11020 *rel_hash_ptr = NULL;
11021 }
11022 else
11023 {
11024 struct elf_link_hash_entry *h;
11025
11026 /* Treat a reloc against a defined symbol as though it were
11027 actually against the section. */
11028 h = ((struct elf_link_hash_entry *)
11029 bfd_wrapped_link_hash_lookup (output_bfd, info,
11030 link_order->u.reloc.p->u.name,
11031 FALSE, FALSE, TRUE));
11032 if (h != NULL
11033 && (h->root.type == bfd_link_hash_defined
11034 || h->root.type == bfd_link_hash_defweak))
11035 {
11036 asection *section;
11037
11038 section = h->root.u.def.section;
11039 indx = section->output_section->target_index;
11040 *rel_hash_ptr = NULL;
11041 /* It seems that we ought to add the symbol value to the
11042 addend here, but in practice it has already been added
11043 because it was passed to constructor_callback. */
11044 addend += section->output_section->vma + section->output_offset;
11045 }
11046 else if (h != NULL)
11047 {
11048 /* Setting the index to -2 tells elf_link_output_extsym that
11049 this symbol is used by a reloc. */
11050 h->indx = -2;
11051 *rel_hash_ptr = h;
11052 indx = 0;
11053 }
11054 else
11055 {
1a72702b
AM
11056 (*info->callbacks->unattached_reloc)
11057 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11058 indx = 0;
11059 }
11060 }
11061
11062 /* If this is an inplace reloc, we must write the addend into the
11063 object file. */
11064 if (howto->partial_inplace && addend != 0)
11065 {
11066 bfd_size_type size;
11067 bfd_reloc_status_type rstat;
11068 bfd_byte *buf;
11069 bfd_boolean ok;
11070 const char *sym_name;
11071
a50b1753
NC
11072 size = (bfd_size_type) bfd_get_reloc_size (howto);
11073 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11074 if (buf == NULL && size != 0)
c152c796
AM
11075 return FALSE;
11076 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11077 switch (rstat)
11078 {
11079 case bfd_reloc_ok:
11080 break;
11081
11082 default:
11083 case bfd_reloc_outofrange:
11084 abort ();
11085
11086 case bfd_reloc_overflow:
11087 if (link_order->type == bfd_section_reloc_link_order)
11088 sym_name = bfd_section_name (output_bfd,
11089 link_order->u.reloc.p->u.section);
11090 else
11091 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11092 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11093 howto->name, addend, NULL, NULL,
11094 (bfd_vma) 0);
c152c796
AM
11095 break;
11096 }
37b01f6a 11097
c152c796 11098 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11099 link_order->offset
11100 * bfd_octets_per_byte (output_bfd),
11101 size);
c152c796
AM
11102 free (buf);
11103 if (! ok)
11104 return FALSE;
11105 }
11106
11107 /* The address of a reloc is relative to the section in a
11108 relocatable file, and is a virtual address in an executable
11109 file. */
11110 offset = link_order->offset;
0e1862bb 11111 if (! bfd_link_relocatable (info))
c152c796
AM
11112 offset += output_section->vma;
11113
11114 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11115 {
11116 irel[i].r_offset = offset;
11117 irel[i].r_info = 0;
11118 irel[i].r_addend = 0;
11119 }
11120 if (bed->s->arch_size == 32)
11121 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11122 else
11123 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11124
d4730f92 11125 rel_hdr = reldata->hdr;
c152c796
AM
11126 erel = rel_hdr->contents;
11127 if (rel_hdr->sh_type == SHT_REL)
11128 {
d4730f92 11129 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11130 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11131 }
11132 else
11133 {
11134 irel[0].r_addend = addend;
d4730f92 11135 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11136 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11137 }
11138
d4730f92 11139 ++reldata->count;
c152c796
AM
11140
11141 return TRUE;
11142}
11143
0b52efa6
PB
11144
11145/* Get the output vma of the section pointed to by the sh_link field. */
11146
11147static bfd_vma
11148elf_get_linked_section_vma (struct bfd_link_order *p)
11149{
11150 Elf_Internal_Shdr **elf_shdrp;
11151 asection *s;
11152 int elfsec;
11153
11154 s = p->u.indirect.section;
11155 elf_shdrp = elf_elfsections (s->owner);
11156 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11157 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11158 /* PR 290:
11159 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11160 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11161 sh_info fields. Hence we could get the situation
11162 where elfsec is 0. */
11163 if (elfsec == 0)
11164 {
11165 const struct elf_backend_data *bed
11166 = get_elf_backend_data (s->owner);
11167 if (bed->link_order_error_handler)
d003868e 11168 bed->link_order_error_handler
695344c0 11169 /* xgettext:c-format */
d003868e 11170 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11171 return 0;
11172 }
11173 else
11174 {
11175 s = elf_shdrp[elfsec]->bfd_section;
11176 return s->output_section->vma + s->output_offset;
11177 }
0b52efa6
PB
11178}
11179
11180
11181/* Compare two sections based on the locations of the sections they are
11182 linked to. Used by elf_fixup_link_order. */
11183
11184static int
11185compare_link_order (const void * a, const void * b)
11186{
11187 bfd_vma apos;
11188 bfd_vma bpos;
11189
11190 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11191 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11192 if (apos < bpos)
11193 return -1;
11194 return apos > bpos;
11195}
11196
11197
11198/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11199 order as their linked sections. Returns false if this could not be done
11200 because an output section includes both ordered and unordered
11201 sections. Ideally we'd do this in the linker proper. */
11202
11203static bfd_boolean
11204elf_fixup_link_order (bfd *abfd, asection *o)
11205{
11206 int seen_linkorder;
11207 int seen_other;
11208 int n;
11209 struct bfd_link_order *p;
11210 bfd *sub;
11211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11212 unsigned elfsec;
0b52efa6 11213 struct bfd_link_order **sections;
d33cdfe3 11214 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11215 bfd_vma offset;
3b36f7e6 11216
d33cdfe3
L
11217 other_sec = NULL;
11218 linkorder_sec = NULL;
0b52efa6
PB
11219 seen_other = 0;
11220 seen_linkorder = 0;
8423293d 11221 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11222 {
d33cdfe3 11223 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11224 {
11225 s = p->u.indirect.section;
d33cdfe3
L
11226 sub = s->owner;
11227 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11228 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11229 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11230 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11231 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11232 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11233 {
11234 seen_linkorder++;
11235 linkorder_sec = s;
11236 }
0b52efa6 11237 else
d33cdfe3
L
11238 {
11239 seen_other++;
11240 other_sec = s;
11241 }
0b52efa6
PB
11242 }
11243 else
11244 seen_other++;
d33cdfe3
L
11245
11246 if (seen_other && seen_linkorder)
11247 {
11248 if (other_sec && linkorder_sec)
4eca0228 11249 _bfd_error_handler
695344c0 11250 /* xgettext:c-format */
4eca0228
AM
11251 (_("%A has both ordered [`%A' in %B] "
11252 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11253 o, linkorder_sec, linkorder_sec->owner,
11254 other_sec, other_sec->owner);
d33cdfe3 11255 else
4eca0228
AM
11256 _bfd_error_handler
11257 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11258 bfd_set_error (bfd_error_bad_value);
11259 return FALSE;
11260 }
0b52efa6
PB
11261 }
11262
11263 if (!seen_linkorder)
11264 return TRUE;
11265
0b52efa6 11266 sections = (struct bfd_link_order **)
14b1c01e
AM
11267 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11268 if (sections == NULL)
11269 return FALSE;
0b52efa6 11270 seen_linkorder = 0;
3b36f7e6 11271
8423293d 11272 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11273 {
11274 sections[seen_linkorder++] = p;
11275 }
11276 /* Sort the input sections in the order of their linked section. */
11277 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11278 compare_link_order);
11279
11280 /* Change the offsets of the sections. */
11281 offset = 0;
11282 for (n = 0; n < seen_linkorder; n++)
11283 {
11284 s = sections[n]->u.indirect.section;
461686a3 11285 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11286 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11287 sections[n]->offset = offset;
11288 offset += sections[n]->size;
11289 }
11290
4dd07732 11291 free (sections);
0b52efa6
PB
11292 return TRUE;
11293}
11294
76359541
TP
11295/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11296 Returns TRUE upon success, FALSE otherwise. */
11297
11298static bfd_boolean
11299elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11300{
11301 bfd_boolean ret = FALSE;
11302 bfd *implib_bfd;
11303 const struct elf_backend_data *bed;
11304 flagword flags;
11305 enum bfd_architecture arch;
11306 unsigned int mach;
11307 asymbol **sympp = NULL;
11308 long symsize;
11309 long symcount;
11310 long src_count;
11311 elf_symbol_type *osymbuf;
11312
11313 implib_bfd = info->out_implib_bfd;
11314 bed = get_elf_backend_data (abfd);
11315
11316 if (!bfd_set_format (implib_bfd, bfd_object))
11317 return FALSE;
11318
046734ff 11319 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11320 flags = bfd_get_file_flags (abfd);
11321 flags &= ~HAS_RELOC;
11322 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11323 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11324 return FALSE;
11325
11326 /* Copy architecture of output file to import library file. */
11327 arch = bfd_get_arch (abfd);
11328 mach = bfd_get_mach (abfd);
11329 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11330 && (abfd->target_defaulted
11331 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11332 return FALSE;
11333
11334 /* Get symbol table size. */
11335 symsize = bfd_get_symtab_upper_bound (abfd);
11336 if (symsize < 0)
11337 return FALSE;
11338
11339 /* Read in the symbol table. */
11340 sympp = (asymbol **) xmalloc (symsize);
11341 symcount = bfd_canonicalize_symtab (abfd, sympp);
11342 if (symcount < 0)
11343 goto free_sym_buf;
11344
11345 /* Allow the BFD backend to copy any private header data it
11346 understands from the output BFD to the import library BFD. */
11347 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11348 goto free_sym_buf;
11349
11350 /* Filter symbols to appear in the import library. */
11351 if (bed->elf_backend_filter_implib_symbols)
11352 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11353 symcount);
11354 else
11355 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11356 if (symcount == 0)
11357 {
5df1bc57 11358 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11359 _bfd_error_handler (_("%B: no symbol found for import library"),
11360 implib_bfd);
76359541
TP
11361 goto free_sym_buf;
11362 }
11363
11364
11365 /* Make symbols absolute. */
11366 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11367 sizeof (*osymbuf));
11368 for (src_count = 0; src_count < symcount; src_count++)
11369 {
11370 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11371 sizeof (*osymbuf));
11372 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11373 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11374 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11375 osymbuf[src_count].internal_elf_sym.st_value =
11376 osymbuf[src_count].symbol.value;
11377 sympp[src_count] = &osymbuf[src_count].symbol;
11378 }
11379
11380 bfd_set_symtab (implib_bfd, sympp, symcount);
11381
11382 /* Allow the BFD backend to copy any private data it understands
11383 from the output BFD to the import library BFD. This is done last
11384 to permit the routine to look at the filtered symbol table. */
11385 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11386 goto free_sym_buf;
11387
11388 if (!bfd_close (implib_bfd))
11389 goto free_sym_buf;
11390
11391 ret = TRUE;
11392
11393free_sym_buf:
11394 free (sympp);
11395 return ret;
11396}
11397
9f7c3e5e
AM
11398static void
11399elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11400{
11401 asection *o;
11402
11403 if (flinfo->symstrtab != NULL)
ef10c3ac 11404 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11405 if (flinfo->contents != NULL)
11406 free (flinfo->contents);
11407 if (flinfo->external_relocs != NULL)
11408 free (flinfo->external_relocs);
11409 if (flinfo->internal_relocs != NULL)
11410 free (flinfo->internal_relocs);
11411 if (flinfo->external_syms != NULL)
11412 free (flinfo->external_syms);
11413 if (flinfo->locsym_shndx != NULL)
11414 free (flinfo->locsym_shndx);
11415 if (flinfo->internal_syms != NULL)
11416 free (flinfo->internal_syms);
11417 if (flinfo->indices != NULL)
11418 free (flinfo->indices);
11419 if (flinfo->sections != NULL)
11420 free (flinfo->sections);
9f7c3e5e
AM
11421 if (flinfo->symshndxbuf != NULL)
11422 free (flinfo->symshndxbuf);
11423 for (o = obfd->sections; o != NULL; o = o->next)
11424 {
11425 struct bfd_elf_section_data *esdo = elf_section_data (o);
11426 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11427 free (esdo->rel.hashes);
11428 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11429 free (esdo->rela.hashes);
11430 }
11431}
0b52efa6 11432
c152c796
AM
11433/* Do the final step of an ELF link. */
11434
11435bfd_boolean
11436bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11437{
11438 bfd_boolean dynamic;
11439 bfd_boolean emit_relocs;
11440 bfd *dynobj;
8b127cbc 11441 struct elf_final_link_info flinfo;
91d6fa6a
NC
11442 asection *o;
11443 struct bfd_link_order *p;
11444 bfd *sub;
c152c796
AM
11445 bfd_size_type max_contents_size;
11446 bfd_size_type max_external_reloc_size;
11447 bfd_size_type max_internal_reloc_count;
11448 bfd_size_type max_sym_count;
11449 bfd_size_type max_sym_shndx_count;
c152c796
AM
11450 Elf_Internal_Sym elfsym;
11451 unsigned int i;
11452 Elf_Internal_Shdr *symtab_hdr;
11453 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11454 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11455 struct elf_outext_info eoinfo;
11456 bfd_boolean merged;
11457 size_t relativecount = 0;
11458 asection *reldyn = 0;
11459 bfd_size_type amt;
104d59d1
JM
11460 asection *attr_section = NULL;
11461 bfd_vma attr_size = 0;
11462 const char *std_attrs_section;
64f52338 11463 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11464
64f52338 11465 if (!is_elf_hash_table (htab))
c152c796
AM
11466 return FALSE;
11467
0e1862bb 11468 if (bfd_link_pic (info))
c152c796
AM
11469 abfd->flags |= DYNAMIC;
11470
64f52338
AM
11471 dynamic = htab->dynamic_sections_created;
11472 dynobj = htab->dynobj;
c152c796 11473
0e1862bb 11474 emit_relocs = (bfd_link_relocatable (info)
a4676736 11475 || info->emitrelocations);
c152c796 11476
8b127cbc
AM
11477 flinfo.info = info;
11478 flinfo.output_bfd = abfd;
ef10c3ac 11479 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11480 if (flinfo.symstrtab == NULL)
c152c796
AM
11481 return FALSE;
11482
11483 if (! dynamic)
11484 {
8b127cbc
AM
11485 flinfo.hash_sec = NULL;
11486 flinfo.symver_sec = NULL;
c152c796
AM
11487 }
11488 else
11489 {
3d4d4302 11490 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11491 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11492 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11493 /* Note that it is OK if symver_sec is NULL. */
11494 }
11495
8b127cbc
AM
11496 flinfo.contents = NULL;
11497 flinfo.external_relocs = NULL;
11498 flinfo.internal_relocs = NULL;
11499 flinfo.external_syms = NULL;
11500 flinfo.locsym_shndx = NULL;
11501 flinfo.internal_syms = NULL;
11502 flinfo.indices = NULL;
11503 flinfo.sections = NULL;
8b127cbc 11504 flinfo.symshndxbuf = NULL;
ffbc01cc 11505 flinfo.filesym_count = 0;
c152c796 11506
104d59d1
JM
11507 /* The object attributes have been merged. Remove the input
11508 sections from the link, and set the contents of the output
11509 secton. */
11510 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11511 for (o = abfd->sections; o != NULL; o = o->next)
11512 {
11513 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11514 || strcmp (o->name, ".gnu.attributes") == 0)
11515 {
11516 for (p = o->map_head.link_order; p != NULL; p = p->next)
11517 {
11518 asection *input_section;
11519
11520 if (p->type != bfd_indirect_link_order)
11521 continue;
11522 input_section = p->u.indirect.section;
11523 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11524 elf_link_input_bfd ignores this section. */
11525 input_section->flags &= ~SEC_HAS_CONTENTS;
11526 }
a0c8462f 11527
104d59d1
JM
11528 attr_size = bfd_elf_obj_attr_size (abfd);
11529 if (attr_size)
11530 {
11531 bfd_set_section_size (abfd, o, attr_size);
11532 attr_section = o;
11533 /* Skip this section later on. */
11534 o->map_head.link_order = NULL;
11535 }
11536 else
11537 o->flags |= SEC_EXCLUDE;
11538 }
11539 }
11540
c152c796
AM
11541 /* Count up the number of relocations we will output for each output
11542 section, so that we know the sizes of the reloc sections. We
11543 also figure out some maximum sizes. */
11544 max_contents_size = 0;
11545 max_external_reloc_size = 0;
11546 max_internal_reloc_count = 0;
11547 max_sym_count = 0;
11548 max_sym_shndx_count = 0;
11549 merged = FALSE;
11550 for (o = abfd->sections; o != NULL; o = o->next)
11551 {
11552 struct bfd_elf_section_data *esdo = elf_section_data (o);
11553 o->reloc_count = 0;
11554
8423293d 11555 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11556 {
11557 unsigned int reloc_count = 0;
9eaff861 11558 unsigned int additional_reloc_count = 0;
c152c796 11559 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11560
11561 if (p->type == bfd_section_reloc_link_order
11562 || p->type == bfd_symbol_reloc_link_order)
11563 reloc_count = 1;
11564 else if (p->type == bfd_indirect_link_order)
11565 {
11566 asection *sec;
11567
11568 sec = p->u.indirect.section;
c152c796
AM
11569
11570 /* Mark all sections which are to be included in the
11571 link. This will normally be every section. We need
11572 to do this so that we can identify any sections which
11573 the linker has decided to not include. */
11574 sec->linker_mark = TRUE;
11575
11576 if (sec->flags & SEC_MERGE)
11577 merged = TRUE;
11578
eea6121a
AM
11579 if (sec->rawsize > max_contents_size)
11580 max_contents_size = sec->rawsize;
11581 if (sec->size > max_contents_size)
11582 max_contents_size = sec->size;
c152c796 11583
c152c796
AM
11584 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11585 && (sec->owner->flags & DYNAMIC) == 0)
11586 {
11587 size_t sym_count;
11588
a961cdd5
AM
11589 /* We are interested in just local symbols, not all
11590 symbols. */
c152c796
AM
11591 if (elf_bad_symtab (sec->owner))
11592 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11593 / bed->s->sizeof_sym);
11594 else
11595 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11596
11597 if (sym_count > max_sym_count)
11598 max_sym_count = sym_count;
11599
11600 if (sym_count > max_sym_shndx_count
6a40cf0c 11601 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11602 max_sym_shndx_count = sym_count;
11603
a961cdd5
AM
11604 if (esdo->this_hdr.sh_type == SHT_REL
11605 || esdo->this_hdr.sh_type == SHT_RELA)
11606 /* Some backends use reloc_count in relocation sections
11607 to count particular types of relocs. Of course,
11608 reloc sections themselves can't have relocations. */
11609 ;
11610 else if (emit_relocs)
11611 {
11612 reloc_count = sec->reloc_count;
11613 if (bed->elf_backend_count_additional_relocs)
11614 {
11615 int c;
11616 c = (*bed->elf_backend_count_additional_relocs) (sec);
11617 additional_reloc_count += c;
11618 }
11619 }
11620 else if (bed->elf_backend_count_relocs)
11621 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11622
11623 esdi = elf_section_data (sec);
11624
c152c796
AM
11625 if ((sec->flags & SEC_RELOC) != 0)
11626 {
d4730f92 11627 size_t ext_size = 0;
c152c796 11628
d4730f92
BS
11629 if (esdi->rel.hdr != NULL)
11630 ext_size = esdi->rel.hdr->sh_size;
11631 if (esdi->rela.hdr != NULL)
11632 ext_size += esdi->rela.hdr->sh_size;
7326c758 11633
c152c796
AM
11634 if (ext_size > max_external_reloc_size)
11635 max_external_reloc_size = ext_size;
11636 if (sec->reloc_count > max_internal_reloc_count)
11637 max_internal_reloc_count = sec->reloc_count;
11638 }
11639 }
11640 }
11641
11642 if (reloc_count == 0)
11643 continue;
11644
9eaff861 11645 reloc_count += additional_reloc_count;
c152c796
AM
11646 o->reloc_count += reloc_count;
11647
0e1862bb 11648 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11649 {
d4730f92 11650 if (esdi->rel.hdr)
9eaff861 11651 {
491d01d3 11652 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11653 esdo->rel.count += additional_reloc_count;
11654 }
d4730f92 11655 if (esdi->rela.hdr)
9eaff861 11656 {
491d01d3 11657 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11658 esdo->rela.count += additional_reloc_count;
11659 }
d4730f92
BS
11660 }
11661 else
11662 {
11663 if (o->use_rela_p)
11664 esdo->rela.count += reloc_count;
2c2b4ed4 11665 else
d4730f92 11666 esdo->rel.count += reloc_count;
c152c796 11667 }
c152c796
AM
11668 }
11669
9eaff861 11670 if (o->reloc_count > 0)
c152c796
AM
11671 o->flags |= SEC_RELOC;
11672 else
11673 {
11674 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11675 set it (this is probably a bug) and if it is set
11676 assign_section_numbers will create a reloc section. */
11677 o->flags &=~ SEC_RELOC;
11678 }
11679
11680 /* If the SEC_ALLOC flag is not set, force the section VMA to
11681 zero. This is done in elf_fake_sections as well, but forcing
11682 the VMA to 0 here will ensure that relocs against these
11683 sections are handled correctly. */
11684 if ((o->flags & SEC_ALLOC) == 0
11685 && ! o->user_set_vma)
11686 o->vma = 0;
11687 }
11688
0e1862bb 11689 if (! bfd_link_relocatable (info) && merged)
64f52338 11690 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11691
11692 /* Figure out the file positions for everything but the symbol table
11693 and the relocs. We set symcount to force assign_section_numbers
11694 to create a symbol table. */
8539e4e8 11695 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11696 BFD_ASSERT (! abfd->output_has_begun);
11697 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11698 goto error_return;
11699
ee75fd95 11700 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11701 for (o = abfd->sections; o != NULL; o = o->next)
11702 {
d4730f92 11703 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11704 if ((o->flags & SEC_RELOC) != 0)
11705 {
d4730f92 11706 if (esdo->rel.hdr
9eaff861 11707 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11708 goto error_return;
11709
d4730f92 11710 if (esdo->rela.hdr
9eaff861 11711 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11712 goto error_return;
11713 }
11714
11715 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11716 to count upwards while actually outputting the relocations. */
d4730f92
BS
11717 esdo->rel.count = 0;
11718 esdo->rela.count = 0;
0ce398f1
L
11719
11720 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11721 {
11722 /* Cache the section contents so that they can be compressed
11723 later. Use bfd_malloc since it will be freed by
11724 bfd_compress_section_contents. */
11725 unsigned char *contents = esdo->this_hdr.contents;
11726 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11727 abort ();
11728 contents
11729 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11730 if (contents == NULL)
11731 goto error_return;
11732 esdo->this_hdr.contents = contents;
11733 }
c152c796
AM
11734 }
11735
c152c796 11736 /* We have now assigned file positions for all the sections except
a485e98e
AM
11737 .symtab, .strtab, and non-loaded reloc sections. We start the
11738 .symtab section at the current file position, and write directly
11739 to it. We build the .strtab section in memory. */
c152c796
AM
11740 bfd_get_symcount (abfd) = 0;
11741 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11742 /* sh_name is set in prep_headers. */
11743 symtab_hdr->sh_type = SHT_SYMTAB;
11744 /* sh_flags, sh_addr and sh_size all start off zero. */
11745 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11746 /* sh_link is set in assign_section_numbers. */
11747 /* sh_info is set below. */
11748 /* sh_offset is set just below. */
72de5009 11749 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11750
ef10c3ac
L
11751 if (max_sym_count < 20)
11752 max_sym_count = 20;
64f52338 11753 htab->strtabsize = max_sym_count;
ef10c3ac 11754 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11755 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11756 if (htab->strtab == NULL)
c152c796 11757 goto error_return;
ef10c3ac
L
11758 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11759 flinfo.symshndxbuf
11760 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11761 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11762
8539e4e8 11763 if (info->strip != strip_all || emit_relocs)
c152c796 11764 {
8539e4e8
AM
11765 file_ptr off = elf_next_file_pos (abfd);
11766
11767 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11768
11769 /* Note that at this point elf_next_file_pos (abfd) is
11770 incorrect. We do not yet know the size of the .symtab section.
11771 We correct next_file_pos below, after we do know the size. */
11772
11773 /* Start writing out the symbol table. The first symbol is always a
11774 dummy symbol. */
c152c796
AM
11775 elfsym.st_value = 0;
11776 elfsym.st_size = 0;
11777 elfsym.st_info = 0;
11778 elfsym.st_other = 0;
11779 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11780 elfsym.st_target_internal = 0;
ef10c3ac
L
11781 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11782 bfd_und_section_ptr, NULL) != 1)
c152c796 11783 goto error_return;
c152c796 11784
8539e4e8
AM
11785 /* Output a symbol for each section. We output these even if we are
11786 discarding local symbols, since they are used for relocs. These
11787 symbols have no names. We store the index of each one in the
11788 index field of the section, so that we can find it again when
11789 outputting relocs. */
11790
c152c796
AM
11791 elfsym.st_size = 0;
11792 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11793 elfsym.st_other = 0;
f0b5bb34 11794 elfsym.st_value = 0;
35fc36a8 11795 elfsym.st_target_internal = 0;
c152c796
AM
11796 for (i = 1; i < elf_numsections (abfd); i++)
11797 {
11798 o = bfd_section_from_elf_index (abfd, i);
11799 if (o != NULL)
f0b5bb34
AM
11800 {
11801 o->target_index = bfd_get_symcount (abfd);
11802 elfsym.st_shndx = i;
0e1862bb 11803 if (!bfd_link_relocatable (info))
f0b5bb34 11804 elfsym.st_value = o->vma;
ef10c3ac
L
11805 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11806 NULL) != 1)
f0b5bb34
AM
11807 goto error_return;
11808 }
c152c796
AM
11809 }
11810 }
11811
11812 /* Allocate some memory to hold information read in from the input
11813 files. */
11814 if (max_contents_size != 0)
11815 {
8b127cbc
AM
11816 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11817 if (flinfo.contents == NULL)
c152c796
AM
11818 goto error_return;
11819 }
11820
11821 if (max_external_reloc_size != 0)
11822 {
8b127cbc
AM
11823 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11824 if (flinfo.external_relocs == NULL)
c152c796
AM
11825 goto error_return;
11826 }
11827
11828 if (max_internal_reloc_count != 0)
11829 {
056bafd4 11830 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11831 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11832 if (flinfo.internal_relocs == NULL)
c152c796
AM
11833 goto error_return;
11834 }
11835
11836 if (max_sym_count != 0)
11837 {
11838 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11839 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11840 if (flinfo.external_syms == NULL)
c152c796
AM
11841 goto error_return;
11842
11843 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11844 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11845 if (flinfo.internal_syms == NULL)
c152c796
AM
11846 goto error_return;
11847
11848 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11849 flinfo.indices = (long int *) bfd_malloc (amt);
11850 if (flinfo.indices == NULL)
c152c796
AM
11851 goto error_return;
11852
11853 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11854 flinfo.sections = (asection **) bfd_malloc (amt);
11855 if (flinfo.sections == NULL)
c152c796
AM
11856 goto error_return;
11857 }
11858
11859 if (max_sym_shndx_count != 0)
11860 {
11861 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11862 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11863 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11864 goto error_return;
11865 }
11866
64f52338 11867 if (htab->tls_sec)
c152c796
AM
11868 {
11869 bfd_vma base, end = 0;
11870 asection *sec;
11871
64f52338 11872 for (sec = htab->tls_sec;
c152c796
AM
11873 sec && (sec->flags & SEC_THREAD_LOCAL);
11874 sec = sec->next)
11875 {
3a800eb9 11876 bfd_size_type size = sec->size;
c152c796 11877
3a800eb9
AM
11878 if (size == 0
11879 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11880 {
91d6fa6a
NC
11881 struct bfd_link_order *ord = sec->map_tail.link_order;
11882
11883 if (ord != NULL)
11884 size = ord->offset + ord->size;
c152c796
AM
11885 }
11886 end = sec->vma + size;
11887 }
64f52338 11888 base = htab->tls_sec->vma;
7dc98aea
RO
11889 /* Only align end of TLS section if static TLS doesn't have special
11890 alignment requirements. */
11891 if (bed->static_tls_alignment == 1)
64f52338
AM
11892 end = align_power (end, htab->tls_sec->alignment_power);
11893 htab->tls_size = end - base;
c152c796
AM
11894 }
11895
0b52efa6
PB
11896 /* Reorder SHF_LINK_ORDER sections. */
11897 for (o = abfd->sections; o != NULL; o = o->next)
11898 {
11899 if (!elf_fixup_link_order (abfd, o))
11900 return FALSE;
11901 }
11902
2f0c68f2
CM
11903 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11904 return FALSE;
11905
c152c796
AM
11906 /* Since ELF permits relocations to be against local symbols, we
11907 must have the local symbols available when we do the relocations.
11908 Since we would rather only read the local symbols once, and we
11909 would rather not keep them in memory, we handle all the
11910 relocations for a single input file at the same time.
11911
11912 Unfortunately, there is no way to know the total number of local
11913 symbols until we have seen all of them, and the local symbol
11914 indices precede the global symbol indices. This means that when
11915 we are generating relocatable output, and we see a reloc against
11916 a global symbol, we can not know the symbol index until we have
11917 finished examining all the local symbols to see which ones we are
11918 going to output. To deal with this, we keep the relocations in
11919 memory, and don't output them until the end of the link. This is
11920 an unfortunate waste of memory, but I don't see a good way around
11921 it. Fortunately, it only happens when performing a relocatable
11922 link, which is not the common case. FIXME: If keep_memory is set
11923 we could write the relocs out and then read them again; I don't
11924 know how bad the memory loss will be. */
11925
c72f2fb2 11926 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11927 sub->output_has_begun = FALSE;
11928 for (o = abfd->sections; o != NULL; o = o->next)
11929 {
8423293d 11930 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11931 {
11932 if (p->type == bfd_indirect_link_order
11933 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11934 == bfd_target_elf_flavour)
11935 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11936 {
11937 if (! sub->output_has_begun)
11938 {
8b127cbc 11939 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11940 goto error_return;
11941 sub->output_has_begun = TRUE;
11942 }
11943 }
11944 else if (p->type == bfd_section_reloc_link_order
11945 || p->type == bfd_symbol_reloc_link_order)
11946 {
11947 if (! elf_reloc_link_order (abfd, info, o, p))
11948 goto error_return;
11949 }
11950 else
11951 {
11952 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11953 {
11954 if (p->type == bfd_indirect_link_order
11955 && (bfd_get_flavour (sub)
11956 == bfd_target_elf_flavour)
11957 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11958 != bed->s->elfclass))
11959 {
11960 const char *iclass, *oclass;
11961
aebf9be7 11962 switch (bed->s->elfclass)
351f65ca 11963 {
aebf9be7
NC
11964 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11965 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11966 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11967 default: abort ();
351f65ca 11968 }
aebf9be7
NC
11969
11970 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11971 {
aebf9be7
NC
11972 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11973 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11974 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11975 default: abort ();
351f65ca
L
11976 }
11977
11978 bfd_set_error (bfd_error_wrong_format);
4eca0228 11979 _bfd_error_handler
695344c0 11980 /* xgettext:c-format */
351f65ca
L
11981 (_("%B: file class %s incompatible with %s"),
11982 sub, iclass, oclass);
11983 }
11984
11985 goto error_return;
11986 }
c152c796
AM
11987 }
11988 }
11989 }
11990
c0f00686
L
11991 /* Free symbol buffer if needed. */
11992 if (!info->reduce_memory_overheads)
11993 {
c72f2fb2 11994 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11995 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11996 && elf_tdata (sub)->symbuf)
c0f00686
L
11997 {
11998 free (elf_tdata (sub)->symbuf);
11999 elf_tdata (sub)->symbuf = NULL;
12000 }
12001 }
12002
c152c796
AM
12003 /* Output any global symbols that got converted to local in a
12004 version script or due to symbol visibility. We do this in a
12005 separate step since ELF requires all local symbols to appear
12006 prior to any global symbols. FIXME: We should only do this if
12007 some global symbols were, in fact, converted to become local.
12008 FIXME: Will this work correctly with the Irix 5 linker? */
12009 eoinfo.failed = FALSE;
8b127cbc 12010 eoinfo.flinfo = &flinfo;
c152c796 12011 eoinfo.localsyms = TRUE;
34a79995 12012 eoinfo.file_sym_done = FALSE;
7686d77d 12013 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12014 if (eoinfo.failed)
12015 return FALSE;
12016
4e617b1e
PB
12017 /* If backend needs to output some local symbols not present in the hash
12018 table, do it now. */
8539e4e8
AM
12019 if (bed->elf_backend_output_arch_local_syms
12020 && (info->strip != strip_all || emit_relocs))
4e617b1e 12021 {
6e0b88f1 12022 typedef int (*out_sym_func)
4e617b1e
PB
12023 (void *, const char *, Elf_Internal_Sym *, asection *,
12024 struct elf_link_hash_entry *);
12025
12026 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12027 (abfd, info, &flinfo,
12028 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12029 return FALSE;
12030 }
12031
c152c796
AM
12032 /* That wrote out all the local symbols. Finish up the symbol table
12033 with the global symbols. Even if we want to strip everything we
12034 can, we still need to deal with those global symbols that got
12035 converted to local in a version script. */
12036
12037 /* The sh_info field records the index of the first non local symbol. */
12038 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12039
12040 if (dynamic
64f52338
AM
12041 && htab->dynsym != NULL
12042 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12043 {
12044 Elf_Internal_Sym sym;
64f52338 12045 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12046
64f52338
AM
12047 o = htab->dynsym->output_section;
12048 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12049
12050 /* Write out the section symbols for the output sections. */
0e1862bb 12051 if (bfd_link_pic (info)
64f52338 12052 || htab->is_relocatable_executable)
c152c796
AM
12053 {
12054 asection *s;
12055
12056 sym.st_size = 0;
12057 sym.st_name = 0;
12058 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12059 sym.st_other = 0;
35fc36a8 12060 sym.st_target_internal = 0;
c152c796
AM
12061
12062 for (s = abfd->sections; s != NULL; s = s->next)
12063 {
12064 int indx;
12065 bfd_byte *dest;
12066 long dynindx;
12067
c152c796 12068 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12069 if (dynindx <= 0)
12070 continue;
12071 indx = elf_section_data (s)->this_idx;
c152c796
AM
12072 BFD_ASSERT (indx > 0);
12073 sym.st_shndx = indx;
c0d5a53d
L
12074 if (! check_dynsym (abfd, &sym))
12075 return FALSE;
c152c796
AM
12076 sym.st_value = s->vma;
12077 dest = dynsym + dynindx * bed->s->sizeof_sym;
12078 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12079 }
c152c796
AM
12080 }
12081
12082 /* Write out the local dynsyms. */
64f52338 12083 if (htab->dynlocal)
c152c796
AM
12084 {
12085 struct elf_link_local_dynamic_entry *e;
64f52338 12086 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12087 {
12088 asection *s;
12089 bfd_byte *dest;
12090
935bd1e0 12091 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12092 Note that we saved a word of storage and overwrote
12093 the original st_name with the dynstr_index. */
12094 sym = e->isym;
935bd1e0 12095 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12096
cb33740c
AM
12097 s = bfd_section_from_elf_index (e->input_bfd,
12098 e->isym.st_shndx);
12099 if (s != NULL)
c152c796 12100 {
c152c796
AM
12101 sym.st_shndx =
12102 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12103 if (! check_dynsym (abfd, &sym))
12104 return FALSE;
c152c796
AM
12105 sym.st_value = (s->output_section->vma
12106 + s->output_offset
12107 + e->isym.st_value);
12108 }
12109
c152c796
AM
12110 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12111 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12112 }
12113 }
c152c796
AM
12114 }
12115
12116 /* We get the global symbols from the hash table. */
12117 eoinfo.failed = FALSE;
12118 eoinfo.localsyms = FALSE;
8b127cbc 12119 eoinfo.flinfo = &flinfo;
7686d77d 12120 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12121 if (eoinfo.failed)
12122 return FALSE;
12123
12124 /* If backend needs to output some symbols not present in the hash
12125 table, do it now. */
8539e4e8
AM
12126 if (bed->elf_backend_output_arch_syms
12127 && (info->strip != strip_all || emit_relocs))
c152c796 12128 {
6e0b88f1 12129 typedef int (*out_sym_func)
c152c796
AM
12130 (void *, const char *, Elf_Internal_Sym *, asection *,
12131 struct elf_link_hash_entry *);
12132
12133 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12134 (abfd, info, &flinfo,
12135 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12136 return FALSE;
12137 }
12138
ef10c3ac
L
12139 /* Finalize the .strtab section. */
12140 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12141
12142 /* Swap out the .strtab section. */
12143 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12144 return FALSE;
12145
12146 /* Now we know the size of the symtab section. */
c152c796
AM
12147 if (bfd_get_symcount (abfd) > 0)
12148 {
ee3b52e9
L
12149 /* Finish up and write out the symbol string table (.strtab)
12150 section. */
ad32986f 12151 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12152 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12153
ad32986f 12154 if (elf_symtab_shndx_list (abfd))
8539e4e8 12155 {
ad32986f 12156 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12157
ad32986f
NC
12158 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12159 {
12160 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12161 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12162 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12163 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12164 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12165
ad32986f
NC
12166 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12167 off, TRUE);
12168
12169 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12170 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12171 return FALSE;
12172 }
8539e4e8 12173 }
ee3b52e9
L
12174
12175 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12176 /* sh_name was set in prep_headers. */
12177 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12178 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12179 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12180 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12181 symstrtab_hdr->sh_entsize = 0;
12182 symstrtab_hdr->sh_link = 0;
12183 symstrtab_hdr->sh_info = 0;
12184 /* sh_offset is set just below. */
12185 symstrtab_hdr->sh_addralign = 1;
12186
12187 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12188 off, TRUE);
12189 elf_next_file_pos (abfd) = off;
12190
c152c796 12191 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12192 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12193 return FALSE;
12194 }
12195
76359541
TP
12196 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12197 {
4eca0228
AM
12198 _bfd_error_handler (_("%B: failed to generate import library"),
12199 info->out_implib_bfd);
76359541
TP
12200 return FALSE;
12201 }
12202
c152c796
AM
12203 /* Adjust the relocs to have the correct symbol indices. */
12204 for (o = abfd->sections; o != NULL; o = o->next)
12205 {
d4730f92 12206 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12207 bfd_boolean sort;
10bbbc1d 12208
c152c796
AM
12209 if ((o->flags & SEC_RELOC) == 0)
12210 continue;
12211
28dbcedc 12212 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12213 if (esdo->rel.hdr != NULL
10bbbc1d 12214 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12215 return FALSE;
12216 if (esdo->rela.hdr != NULL
10bbbc1d 12217 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12218 return FALSE;
c152c796
AM
12219
12220 /* Set the reloc_count field to 0 to prevent write_relocs from
12221 trying to swap the relocs out itself. */
12222 o->reloc_count = 0;
12223 }
12224
12225 if (dynamic && info->combreloc && dynobj != NULL)
12226 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12227
12228 /* If we are linking against a dynamic object, or generating a
12229 shared library, finish up the dynamic linking information. */
12230 if (dynamic)
12231 {
12232 bfd_byte *dyncon, *dynconend;
12233
12234 /* Fix up .dynamic entries. */
3d4d4302 12235 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12236 BFD_ASSERT (o != NULL);
12237
12238 dyncon = o->contents;
eea6121a 12239 dynconend = o->contents + o->size;
c152c796
AM
12240 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12241 {
12242 Elf_Internal_Dyn dyn;
12243 const char *name;
12244 unsigned int type;
64487780
AM
12245 bfd_size_type sh_size;
12246 bfd_vma sh_addr;
c152c796
AM
12247
12248 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12249
12250 switch (dyn.d_tag)
12251 {
12252 default:
12253 continue;
12254 case DT_NULL:
12255 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12256 {
12257 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12258 {
12259 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12260 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12261 default: continue;
12262 }
12263 dyn.d_un.d_val = relativecount;
12264 relativecount = 0;
12265 break;
12266 }
12267 continue;
12268
12269 case DT_INIT:
12270 name = info->init_function;
12271 goto get_sym;
12272 case DT_FINI:
12273 name = info->fini_function;
12274 get_sym:
12275 {
12276 struct elf_link_hash_entry *h;
12277
64f52338 12278 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12279 if (h != NULL
12280 && (h->root.type == bfd_link_hash_defined
12281 || h->root.type == bfd_link_hash_defweak))
12282 {
bef26483 12283 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12284 o = h->root.u.def.section;
12285 if (o->output_section != NULL)
bef26483 12286 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12287 + o->output_offset);
12288 else
12289 {
12290 /* The symbol is imported from another shared
12291 library and does not apply to this one. */
bef26483 12292 dyn.d_un.d_ptr = 0;
c152c796
AM
12293 }
12294 break;
12295 }
12296 }
12297 continue;
12298
12299 case DT_PREINIT_ARRAYSZ:
12300 name = ".preinit_array";
4ade44b7 12301 goto get_out_size;
c152c796
AM
12302 case DT_INIT_ARRAYSZ:
12303 name = ".init_array";
4ade44b7 12304 goto get_out_size;
c152c796
AM
12305 case DT_FINI_ARRAYSZ:
12306 name = ".fini_array";
4ade44b7 12307 get_out_size:
c152c796
AM
12308 o = bfd_get_section_by_name (abfd, name);
12309 if (o == NULL)
12310 {
4eca0228 12311 _bfd_error_handler
4ade44b7 12312 (_("could not find section %s"), name);
c152c796
AM
12313 goto error_return;
12314 }
eea6121a 12315 if (o->size == 0)
4eca0228 12316 _bfd_error_handler
c152c796 12317 (_("warning: %s section has zero size"), name);
eea6121a 12318 dyn.d_un.d_val = o->size;
c152c796
AM
12319 break;
12320
12321 case DT_PREINIT_ARRAY:
12322 name = ".preinit_array";
4ade44b7 12323 goto get_out_vma;
c152c796
AM
12324 case DT_INIT_ARRAY:
12325 name = ".init_array";
4ade44b7 12326 goto get_out_vma;
c152c796
AM
12327 case DT_FINI_ARRAY:
12328 name = ".fini_array";
4ade44b7
AM
12329 get_out_vma:
12330 o = bfd_get_section_by_name (abfd, name);
12331 goto do_vma;
c152c796
AM
12332
12333 case DT_HASH:
12334 name = ".hash";
12335 goto get_vma;
fdc90cb4
JJ
12336 case DT_GNU_HASH:
12337 name = ".gnu.hash";
12338 goto get_vma;
c152c796
AM
12339 case DT_STRTAB:
12340 name = ".dynstr";
12341 goto get_vma;
12342 case DT_SYMTAB:
12343 name = ".dynsym";
12344 goto get_vma;
12345 case DT_VERDEF:
12346 name = ".gnu.version_d";
12347 goto get_vma;
12348 case DT_VERNEED:
12349 name = ".gnu.version_r";
12350 goto get_vma;
12351 case DT_VERSYM:
12352 name = ".gnu.version";
12353 get_vma:
4ade44b7
AM
12354 o = bfd_get_linker_section (dynobj, name);
12355 do_vma:
b3293efa 12356 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12357 {
4eca0228 12358 _bfd_error_handler
4ade44b7 12359 (_("could not find section %s"), name);
c152c796
AM
12360 goto error_return;
12361 }
894891db
NC
12362 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12363 {
4eca0228 12364 _bfd_error_handler
894891db
NC
12365 (_("warning: section '%s' is being made into a note"), name);
12366 bfd_set_error (bfd_error_nonrepresentable_section);
12367 goto error_return;
12368 }
4ade44b7 12369 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12370 break;
12371
12372 case DT_REL:
12373 case DT_RELA:
12374 case DT_RELSZ:
12375 case DT_RELASZ:
12376 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12377 type = SHT_REL;
12378 else
12379 type = SHT_RELA;
64487780
AM
12380 sh_size = 0;
12381 sh_addr = 0;
c152c796
AM
12382 for (i = 1; i < elf_numsections (abfd); i++)
12383 {
12384 Elf_Internal_Shdr *hdr;
12385
12386 hdr = elf_elfsections (abfd)[i];
12387 if (hdr->sh_type == type
12388 && (hdr->sh_flags & SHF_ALLOC) != 0)
12389 {
64487780
AM
12390 sh_size += hdr->sh_size;
12391 if (sh_addr == 0
12392 || sh_addr > hdr->sh_addr)
12393 sh_addr = hdr->sh_addr;
c152c796
AM
12394 }
12395 }
64487780 12396
64f52338
AM
12397 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12398 {
12399 /* Don't count procedure linkage table relocs in the
12400 overall reloc count. */
64487780
AM
12401 sh_size -= htab->srelplt->size;
12402 if (sh_size == 0)
12403 /* If the size is zero, make the address zero too.
12404 This is to avoid a glibc bug. If the backend
12405 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12406 zero, then we'll put DT_RELA at the end of
12407 DT_JMPREL. glibc will interpret the end of
12408 DT_RELA matching the end of DT_JMPREL as the
12409 case where DT_RELA includes DT_JMPREL, and for
12410 LD_BIND_NOW will decide that processing DT_RELA
12411 will process the PLT relocs too. Net result:
12412 No PLT relocs applied. */
12413 sh_addr = 0;
12414
64f52338
AM
12415 /* If .rela.plt is the first .rela section, exclude
12416 it from DT_RELA. */
64487780
AM
12417 else if (sh_addr == (htab->srelplt->output_section->vma
12418 + htab->srelplt->output_offset))
12419 sh_addr += htab->srelplt->size;
64f52338 12420 }
64487780
AM
12421
12422 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12423 dyn.d_un.d_val = sh_size;
12424 else
12425 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12426 break;
12427 }
12428 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12429 }
12430 }
12431
12432 /* If we have created any dynamic sections, then output them. */
12433 if (dynobj != NULL)
12434 {
12435 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12436 goto error_return;
12437
943284cc 12438 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12439 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12440 || info->error_textrel)
3d4d4302 12441 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12442 {
12443 bfd_byte *dyncon, *dynconend;
12444
943284cc
DJ
12445 dyncon = o->contents;
12446 dynconend = o->contents + o->size;
12447 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12448 {
12449 Elf_Internal_Dyn dyn;
12450
12451 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12452
12453 if (dyn.d_tag == DT_TEXTREL)
12454 {
c192a133
AM
12455 if (info->error_textrel)
12456 info->callbacks->einfo
12457 (_("%P%X: read-only segment has dynamic relocations.\n"));
12458 else
12459 info->callbacks->einfo
12460 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12461 break;
12462 }
12463 }
12464 }
12465
c152c796
AM
12466 for (o = dynobj->sections; o != NULL; o = o->next)
12467 {
12468 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12469 || o->size == 0
c152c796
AM
12470 || o->output_section == bfd_abs_section_ptr)
12471 continue;
12472 if ((o->flags & SEC_LINKER_CREATED) == 0)
12473 {
12474 /* At this point, we are only interested in sections
12475 created by _bfd_elf_link_create_dynamic_sections. */
12476 continue;
12477 }
64f52338 12478 if (htab->stab_info.stabstr == o)
3722b82f 12479 continue;
64f52338 12480 if (htab->eh_info.hdr_sec == o)
eea6121a 12481 continue;
3d4d4302 12482 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12483 {
12484 if (! bfd_set_section_contents (abfd, o->output_section,
12485 o->contents,
37b01f6a
DG
12486 (file_ptr) o->output_offset
12487 * bfd_octets_per_byte (abfd),
eea6121a 12488 o->size))
c152c796
AM
12489 goto error_return;
12490 }
12491 else
12492 {
12493 /* The contents of the .dynstr section are actually in a
12494 stringtab. */
8539e4e8
AM
12495 file_ptr off;
12496
c152c796
AM
12497 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12498 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12499 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12500 goto error_return;
12501 }
12502 }
12503 }
12504
7bdf4127 12505 if (!info->resolve_section_groups)
c152c796
AM
12506 {
12507 bfd_boolean failed = FALSE;
12508
7bdf4127 12509 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12510 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12511 if (failed)
12512 goto error_return;
12513 }
12514
12515 /* If we have optimized stabs strings, output them. */
64f52338 12516 if (htab->stab_info.stabstr != NULL)
c152c796 12517 {
64f52338 12518 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12519 goto error_return;
12520 }
12521
9f7c3e5e
AM
12522 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12523 goto error_return;
c152c796 12524
9f7c3e5e 12525 elf_final_link_free (abfd, &flinfo);
c152c796 12526
12bd6957 12527 elf_linker (abfd) = TRUE;
c152c796 12528
104d59d1
JM
12529 if (attr_section)
12530 {
a50b1753 12531 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12532 if (contents == NULL)
d0f16d5e 12533 return FALSE; /* Bail out and fail. */
104d59d1
JM
12534 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12535 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12536 free (contents);
12537 }
12538
c152c796
AM
12539 return TRUE;
12540
12541 error_return:
9f7c3e5e 12542 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12543 return FALSE;
12544}
12545\f
5241d853
RS
12546/* Initialize COOKIE for input bfd ABFD. */
12547
12548static bfd_boolean
12549init_reloc_cookie (struct elf_reloc_cookie *cookie,
12550 struct bfd_link_info *info, bfd *abfd)
12551{
12552 Elf_Internal_Shdr *symtab_hdr;
12553 const struct elf_backend_data *bed;
12554
12555 bed = get_elf_backend_data (abfd);
12556 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12557
12558 cookie->abfd = abfd;
12559 cookie->sym_hashes = elf_sym_hashes (abfd);
12560 cookie->bad_symtab = elf_bad_symtab (abfd);
12561 if (cookie->bad_symtab)
12562 {
12563 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12564 cookie->extsymoff = 0;
12565 }
12566 else
12567 {
12568 cookie->locsymcount = symtab_hdr->sh_info;
12569 cookie->extsymoff = symtab_hdr->sh_info;
12570 }
12571
12572 if (bed->s->arch_size == 32)
12573 cookie->r_sym_shift = 8;
12574 else
12575 cookie->r_sym_shift = 32;
12576
12577 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12578 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12579 {
12580 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12581 cookie->locsymcount, 0,
12582 NULL, NULL, NULL);
12583 if (cookie->locsyms == NULL)
12584 {
12585 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12586 return FALSE;
12587 }
12588 if (info->keep_memory)
12589 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12590 }
12591 return TRUE;
12592}
12593
12594/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12595
12596static void
12597fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12598{
12599 Elf_Internal_Shdr *symtab_hdr;
12600
12601 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12602 if (cookie->locsyms != NULL
12603 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12604 free (cookie->locsyms);
12605}
12606
12607/* Initialize the relocation information in COOKIE for input section SEC
12608 of input bfd ABFD. */
12609
12610static bfd_boolean
12611init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12612 struct bfd_link_info *info, bfd *abfd,
12613 asection *sec)
12614{
5241d853
RS
12615 if (sec->reloc_count == 0)
12616 {
12617 cookie->rels = NULL;
12618 cookie->relend = NULL;
12619 }
12620 else
12621 {
5241d853
RS
12622 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12623 info->keep_memory);
12624 if (cookie->rels == NULL)
12625 return FALSE;
12626 cookie->rel = cookie->rels;
056bafd4 12627 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12628 }
12629 cookie->rel = cookie->rels;
12630 return TRUE;
12631}
12632
12633/* Free the memory allocated by init_reloc_cookie_rels,
12634 if appropriate. */
12635
12636static void
12637fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12638 asection *sec)
12639{
12640 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12641 free (cookie->rels);
12642}
12643
12644/* Initialize the whole of COOKIE for input section SEC. */
12645
12646static bfd_boolean
12647init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12648 struct bfd_link_info *info,
12649 asection *sec)
12650{
12651 if (!init_reloc_cookie (cookie, info, sec->owner))
12652 goto error1;
12653 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12654 goto error2;
12655 return TRUE;
12656
12657 error2:
12658 fini_reloc_cookie (cookie, sec->owner);
12659 error1:
12660 return FALSE;
12661}
12662
12663/* Free the memory allocated by init_reloc_cookie_for_section,
12664 if appropriate. */
12665
12666static void
12667fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12668 asection *sec)
12669{
12670 fini_reloc_cookie_rels (cookie, sec);
12671 fini_reloc_cookie (cookie, sec->owner);
12672}
12673\f
c152c796
AM
12674/* Garbage collect unused sections. */
12675
07adf181
AM
12676/* Default gc_mark_hook. */
12677
12678asection *
12679_bfd_elf_gc_mark_hook (asection *sec,
12680 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12681 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12682 struct elf_link_hash_entry *h,
12683 Elf_Internal_Sym *sym)
12684{
12685 if (h != NULL)
12686 {
12687 switch (h->root.type)
12688 {
12689 case bfd_link_hash_defined:
12690 case bfd_link_hash_defweak:
12691 return h->root.u.def.section;
12692
12693 case bfd_link_hash_common:
12694 return h->root.u.c.p->section;
12695
12696 default:
12697 break;
12698 }
12699 }
12700 else
12701 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12702
12703 return NULL;
12704}
12705
b7c871ed
L
12706/* Return the global debug definition section. */
12707
12708static asection *
12709elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12710 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12711 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12712 struct elf_link_hash_entry *h,
12713 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12714{
12715 if (h != NULL
12716 && (h->root.type == bfd_link_hash_defined
12717 || h->root.type == bfd_link_hash_defweak)
12718 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12719 return h->root.u.def.section;
12720
12721 return NULL;
12722}
12723
5241d853
RS
12724/* COOKIE->rel describes a relocation against section SEC, which is
12725 a section we've decided to keep. Return the section that contains
12726 the relocation symbol, or NULL if no section contains it. */
12727
12728asection *
12729_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12730 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12731 struct elf_reloc_cookie *cookie,
12732 bfd_boolean *start_stop)
5241d853
RS
12733{
12734 unsigned long r_symndx;
12735 struct elf_link_hash_entry *h;
12736
12737 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12738 if (r_symndx == STN_UNDEF)
5241d853
RS
12739 return NULL;
12740
12741 if (r_symndx >= cookie->locsymcount
12742 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12743 {
12744 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12745 if (h == NULL)
12746 {
12747 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12748 sec->owner);
12749 return NULL;
12750 }
5241d853
RS
12751 while (h->root.type == bfd_link_hash_indirect
12752 || h->root.type == bfd_link_hash_warning)
12753 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12754 h->mark = 1;
4e6b54a6
AM
12755 /* If this symbol is weak and there is a non-weak definition, we
12756 keep the non-weak definition because many backends put
12757 dynamic reloc info on the non-weak definition for code
12758 handling copy relocs. */
12759 if (h->u.weakdef != NULL)
12760 h->u.weakdef->mark = 1;
1cce69b9 12761
a6a4679f 12762 if (start_stop != NULL)
1cce69b9 12763 {
7dba9362
AM
12764 /* To work around a glibc bug, mark XXX input sections
12765 when there is a reference to __start_XXX or __stop_XXX
12766 symbols. */
cbd0eecf 12767 if (h->start_stop)
1cce69b9 12768 {
cbd0eecf 12769 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12770 *start_stop = !s->gc_mark;
12771 return s;
1cce69b9
AM
12772 }
12773 }
12774
5241d853
RS
12775 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12776 }
12777
12778 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12779 &cookie->locsyms[r_symndx]);
12780}
12781
12782/* COOKIE->rel describes a relocation against section SEC, which is
12783 a section we've decided to keep. Mark the section that contains
9d0a14d3 12784 the relocation symbol. */
5241d853
RS
12785
12786bfd_boolean
12787_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12788 asection *sec,
12789 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12790 struct elf_reloc_cookie *cookie)
5241d853
RS
12791{
12792 asection *rsec;
1cce69b9 12793 bfd_boolean start_stop = FALSE;
5241d853 12794
1cce69b9
AM
12795 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12796 while (rsec != NULL)
5241d853 12797 {
1cce69b9
AM
12798 if (!rsec->gc_mark)
12799 {
12800 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12801 || (rsec->owner->flags & DYNAMIC) != 0)
12802 rsec->gc_mark = 1;
12803 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12804 return FALSE;
12805 }
12806 if (!start_stop)
12807 break;
199af150 12808 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12809 }
12810 return TRUE;
12811}
12812
07adf181
AM
12813/* The mark phase of garbage collection. For a given section, mark
12814 it and any sections in this section's group, and all the sections
12815 which define symbols to which it refers. */
12816
ccfa59ea
AM
12817bfd_boolean
12818_bfd_elf_gc_mark (struct bfd_link_info *info,
12819 asection *sec,
6a5bb875 12820 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12821{
12822 bfd_boolean ret;
9d0a14d3 12823 asection *group_sec, *eh_frame;
c152c796
AM
12824
12825 sec->gc_mark = 1;
12826
12827 /* Mark all the sections in the group. */
12828 group_sec = elf_section_data (sec)->next_in_group;
12829 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12830 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12831 return FALSE;
12832
12833 /* Look through the section relocs. */
12834 ret = TRUE;
9d0a14d3
RS
12835 eh_frame = elf_eh_frame_section (sec->owner);
12836 if ((sec->flags & SEC_RELOC) != 0
12837 && sec->reloc_count > 0
12838 && sec != eh_frame)
c152c796 12839 {
5241d853 12840 struct elf_reloc_cookie cookie;
c152c796 12841
5241d853
RS
12842 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12843 ret = FALSE;
c152c796 12844 else
c152c796 12845 {
5241d853 12846 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12847 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12848 {
12849 ret = FALSE;
12850 break;
12851 }
12852 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12853 }
12854 }
9d0a14d3
RS
12855
12856 if (ret && eh_frame && elf_fde_list (sec))
12857 {
12858 struct elf_reloc_cookie cookie;
12859
12860 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12861 ret = FALSE;
12862 else
12863 {
12864 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12865 gc_mark_hook, &cookie))
12866 ret = FALSE;
12867 fini_reloc_cookie_for_section (&cookie, eh_frame);
12868 }
12869 }
12870
2f0c68f2
CM
12871 eh_frame = elf_section_eh_frame_entry (sec);
12872 if (ret && eh_frame && !eh_frame->gc_mark)
12873 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12874 ret = FALSE;
12875
c152c796
AM
12876 return ret;
12877}
12878
3c758495
TG
12879/* Scan and mark sections in a special or debug section group. */
12880
12881static void
12882_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12883{
12884 /* Point to first section of section group. */
12885 asection *ssec;
12886 /* Used to iterate the section group. */
12887 asection *msec;
12888
12889 bfd_boolean is_special_grp = TRUE;
12890 bfd_boolean is_debug_grp = TRUE;
12891
12892 /* First scan to see if group contains any section other than debug
12893 and special section. */
12894 ssec = msec = elf_next_in_group (grp);
12895 do
12896 {
12897 if ((msec->flags & SEC_DEBUGGING) == 0)
12898 is_debug_grp = FALSE;
12899
12900 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12901 is_special_grp = FALSE;
12902
12903 msec = elf_next_in_group (msec);
12904 }
12905 while (msec != ssec);
12906
12907 /* If this is a pure debug section group or pure special section group,
12908 keep all sections in this group. */
12909 if (is_debug_grp || is_special_grp)
12910 {
12911 do
12912 {
12913 msec->gc_mark = 1;
12914 msec = elf_next_in_group (msec);
12915 }
12916 while (msec != ssec);
12917 }
12918}
12919
7f6ab9f8
AM
12920/* Keep debug and special sections. */
12921
12922bfd_boolean
12923_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12924 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12925{
12926 bfd *ibfd;
12927
c72f2fb2 12928 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12929 {
12930 asection *isec;
12931 bfd_boolean some_kept;
b40bf0a2 12932 bfd_boolean debug_frag_seen;
b7c871ed 12933 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
12934
12935 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12936 continue;
57963c05
AM
12937 isec = ibfd->sections;
12938 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12939 continue;
7f6ab9f8 12940
b40bf0a2
NC
12941 /* Ensure all linker created sections are kept,
12942 see if any other section is already marked,
12943 and note if we have any fragmented debug sections. */
b7c871ed 12944 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
12945 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12946 {
12947 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12948 isec->gc_mark = 1;
eb026f09
AM
12949 else if (isec->gc_mark
12950 && (isec->flags & SEC_ALLOC) != 0
12951 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 12952 some_kept = TRUE;
b40bf0a2 12953
535b785f 12954 if (!debug_frag_seen
b40bf0a2
NC
12955 && (isec->flags & SEC_DEBUGGING)
12956 && CONST_STRNEQ (isec->name, ".debug_line."))
12957 debug_frag_seen = TRUE;
7f6ab9f8
AM
12958 }
12959
eb026f09
AM
12960 /* If no non-note alloc section in this file will be kept, then
12961 we can toss out the debug and special sections. */
7f6ab9f8
AM
12962 if (!some_kept)
12963 continue;
12964
12965 /* Keep debug and special sections like .comment when they are
3c758495
TG
12966 not part of a group. Also keep section groups that contain
12967 just debug sections or special sections. */
7f6ab9f8 12968 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12969 {
12970 if ((isec->flags & SEC_GROUP) != 0)
12971 _bfd_elf_gc_mark_debug_special_section_group (isec);
12972 else if (((isec->flags & SEC_DEBUGGING) != 0
12973 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12974 && elf_next_in_group (isec) == NULL)
12975 isec->gc_mark = 1;
b7c871ed
L
12976 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12977 has_kept_debug_info = TRUE;
3c758495 12978 }
b40bf0a2 12979
b40bf0a2
NC
12980 /* Look for CODE sections which are going to be discarded,
12981 and find and discard any fragmented debug sections which
12982 are associated with that code section. */
b7c871ed
L
12983 if (debug_frag_seen)
12984 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12985 if ((isec->flags & SEC_CODE) != 0
12986 && isec->gc_mark == 0)
12987 {
12988 unsigned int ilen;
12989 asection *dsec;
b40bf0a2 12990
b7c871ed 12991 ilen = strlen (isec->name);
b40bf0a2 12992
b7c871ed
L
12993 /* Association is determined by the name of the debug
12994 section containing the name of the code section as
12995 a suffix. For example .debug_line.text.foo is a
12996 debug section associated with .text.foo. */
12997 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12998 {
12999 unsigned int dlen;
b40bf0a2 13000
b7c871ed
L
13001 if (dsec->gc_mark == 0
13002 || (dsec->flags & SEC_DEBUGGING) == 0)
13003 continue;
b40bf0a2 13004
b7c871ed 13005 dlen = strlen (dsec->name);
b40bf0a2 13006
b7c871ed
L
13007 if (dlen > ilen
13008 && strncmp (dsec->name + (dlen - ilen),
13009 isec->name, ilen) == 0)
b40bf0a2 13010 dsec->gc_mark = 0;
b7c871ed 13011 }
b40bf0a2 13012 }
b7c871ed
L
13013
13014 /* Mark debug sections referenced by kept debug sections. */
13015 if (has_kept_debug_info)
13016 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13017 if (isec->gc_mark
13018 && (isec->flags & SEC_DEBUGGING) != 0)
13019 if (!_bfd_elf_gc_mark (info, isec,
13020 elf_gc_mark_debug_section))
13021 return FALSE;
7f6ab9f8
AM
13022 }
13023 return TRUE;
13024}
13025
c152c796 13026static bfd_boolean
ccabcbe5 13027elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13028{
13029 bfd *sub;
ccabcbe5 13030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13031
c72f2fb2 13032 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13033 {
13034 asection *o;
13035
b19a8f85 13036 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13037 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13038 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13039 continue;
57963c05
AM
13040 o = sub->sections;
13041 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13042 continue;
c152c796
AM
13043
13044 for (o = sub->sections; o != NULL; o = o->next)
13045 {
a33dafc3
L
13046 /* When any section in a section group is kept, we keep all
13047 sections in the section group. If the first member of
13048 the section group is excluded, we will also exclude the
13049 group section. */
13050 if (o->flags & SEC_GROUP)
13051 {
13052 asection *first = elf_next_in_group (o);
13053 o->gc_mark = first->gc_mark;
13054 }
c152c796 13055
1e7eae0d 13056 if (o->gc_mark)
c152c796
AM
13057 continue;
13058
13059 /* Skip sweeping sections already excluded. */
13060 if (o->flags & SEC_EXCLUDE)
13061 continue;
13062
13063 /* Since this is early in the link process, it is simple
13064 to remove a section from the output. */
13065 o->flags |= SEC_EXCLUDE;
13066
c55fe096 13067 if (info->print_gc_sections && o->size != 0)
695344c0 13068 /* xgettext:c-format */
c08bb8dd
AM
13069 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13070 o, sub);
c152c796
AM
13071 }
13072 }
13073
c152c796
AM
13074 return TRUE;
13075}
13076
13077/* Propagate collected vtable information. This is called through
13078 elf_link_hash_traverse. */
13079
13080static bfd_boolean
13081elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13082{
c152c796 13083 /* Those that are not vtables. */
cbd0eecf
L
13084 if (h->start_stop
13085 || h->u2.vtable == NULL
13086 || h->u2.vtable->parent == NULL)
c152c796
AM
13087 return TRUE;
13088
13089 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13090 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13091 return TRUE;
13092
13093 /* If we've already been done, exit. */
cbd0eecf 13094 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13095 return TRUE;
13096
13097 /* Make sure the parent's table is up to date. */
cbd0eecf 13098 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13099
cbd0eecf 13100 if (h->u2.vtable->used == NULL)
c152c796
AM
13101 {
13102 /* None of this table's entries were referenced. Re-use the
13103 parent's table. */
cbd0eecf
L
13104 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13105 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13106 }
13107 else
13108 {
13109 size_t n;
13110 bfd_boolean *cu, *pu;
13111
13112 /* Or the parent's entries into ours. */
cbd0eecf 13113 cu = h->u2.vtable->used;
c152c796 13114 cu[-1] = TRUE;
cbd0eecf 13115 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13116 if (pu != NULL)
13117 {
13118 const struct elf_backend_data *bed;
13119 unsigned int log_file_align;
13120
13121 bed = get_elf_backend_data (h->root.u.def.section->owner);
13122 log_file_align = bed->s->log_file_align;
cbd0eecf 13123 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13124 while (n--)
13125 {
13126 if (*pu)
13127 *cu = TRUE;
13128 pu++;
13129 cu++;
13130 }
13131 }
13132 }
13133
13134 return TRUE;
13135}
13136
13137static bfd_boolean
13138elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13139{
13140 asection *sec;
13141 bfd_vma hstart, hend;
13142 Elf_Internal_Rela *relstart, *relend, *rel;
13143 const struct elf_backend_data *bed;
13144 unsigned int log_file_align;
13145
c152c796
AM
13146 /* Take care of both those symbols that do not describe vtables as
13147 well as those that are not loaded. */
cbd0eecf
L
13148 if (h->start_stop
13149 || h->u2.vtable == NULL
13150 || h->u2.vtable->parent == NULL)
c152c796
AM
13151 return TRUE;
13152
13153 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13154 || h->root.type == bfd_link_hash_defweak);
13155
13156 sec = h->root.u.def.section;
13157 hstart = h->root.u.def.value;
13158 hend = hstart + h->size;
13159
13160 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13161 if (!relstart)
13162 return *(bfd_boolean *) okp = FALSE;
13163 bed = get_elf_backend_data (sec->owner);
13164 log_file_align = bed->s->log_file_align;
13165
056bafd4 13166 relend = relstart + sec->reloc_count;
c152c796
AM
13167
13168 for (rel = relstart; rel < relend; ++rel)
13169 if (rel->r_offset >= hstart && rel->r_offset < hend)
13170 {
13171 /* If the entry is in use, do nothing. */
cbd0eecf
L
13172 if (h->u2.vtable->used
13173 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13174 {
13175 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13176 if (h->u2.vtable->used[entry])
c152c796
AM
13177 continue;
13178 }
13179 /* Otherwise, kill it. */
13180 rel->r_offset = rel->r_info = rel->r_addend = 0;
13181 }
13182
13183 return TRUE;
13184}
13185
87538722
AM
13186/* Mark sections containing dynamically referenced symbols. When
13187 building shared libraries, we must assume that any visible symbol is
13188 referenced. */
715df9b8 13189
64d03ab5
AM
13190bfd_boolean
13191bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13192{
87538722 13193 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13194 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13195
715df9b8
EB
13196 if ((h->root.type == bfd_link_hash_defined
13197 || h->root.type == bfd_link_hash_defweak)
87538722 13198 && (h->ref_dynamic
c4621b33 13199 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13200 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13201 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13202 && (!bfd_link_executable (info)
22185505 13203 || info->gc_keep_exported
b407645f
AM
13204 || info->export_dynamic
13205 || (h->dynamic
13206 && d != NULL
13207 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13208 && (h->versioned >= versioned
54e8959c
L
13209 || !bfd_hide_sym_by_version (info->version_info,
13210 h->root.root.string)))))
715df9b8
EB
13211 h->root.u.def.section->flags |= SEC_KEEP;
13212
13213 return TRUE;
13214}
3b36f7e6 13215
74f0fb50
AM
13216/* Keep all sections containing symbols undefined on the command-line,
13217 and the section containing the entry symbol. */
13218
13219void
13220_bfd_elf_gc_keep (struct bfd_link_info *info)
13221{
13222 struct bfd_sym_chain *sym;
13223
13224 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13225 {
13226 struct elf_link_hash_entry *h;
13227
13228 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13229 FALSE, FALSE, FALSE);
13230
13231 if (h != NULL
13232 && (h->root.type == bfd_link_hash_defined
13233 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13234 && !bfd_is_abs_section (h->root.u.def.section)
13235 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13236 h->root.u.def.section->flags |= SEC_KEEP;
13237 }
13238}
13239
2f0c68f2
CM
13240bfd_boolean
13241bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13242 struct bfd_link_info *info)
13243{
13244 bfd *ibfd = info->input_bfds;
13245
13246 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13247 {
13248 asection *sec;
13249 struct elf_reloc_cookie cookie;
13250
13251 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13252 continue;
57963c05
AM
13253 sec = ibfd->sections;
13254 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13255 continue;
2f0c68f2
CM
13256
13257 if (!init_reloc_cookie (&cookie, info, ibfd))
13258 return FALSE;
13259
13260 for (sec = ibfd->sections; sec; sec = sec->next)
13261 {
13262 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13263 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13264 {
13265 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13266 fini_reloc_cookie_rels (&cookie, sec);
13267 }
13268 }
13269 }
13270 return TRUE;
13271}
13272
c152c796
AM
13273/* Do mark and sweep of unused sections. */
13274
13275bfd_boolean
13276bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13277{
13278 bfd_boolean ok = TRUE;
13279 bfd *sub;
6a5bb875 13280 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13282 struct elf_link_hash_table *htab;
c152c796 13283
64d03ab5 13284 if (!bed->can_gc_sections
715df9b8 13285 || !is_elf_hash_table (info->hash))
c152c796 13286 {
4eca0228 13287 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13288 return TRUE;
13289 }
13290
74f0fb50 13291 bed->gc_keep (info);
da44f4e5 13292 htab = elf_hash_table (info);
74f0fb50 13293
9d0a14d3
RS
13294 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13295 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13296 for (sub = info->input_bfds;
13297 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13298 sub = sub->link.next)
9d0a14d3
RS
13299 {
13300 asection *sec;
13301 struct elf_reloc_cookie cookie;
13302
57963c05
AM
13303 sec = sub->sections;
13304 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13305 continue;
9d0a14d3 13306 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13307 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13308 {
13309 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13310 if (elf_section_data (sec)->sec_info
13311 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13312 elf_eh_frame_section (sub) = sec;
13313 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13314 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13315 }
13316 }
9d0a14d3 13317
c152c796 13318 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13319 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13320 if (!ok)
13321 return FALSE;
13322
13323 /* Kill the vtable relocations that were not used. */
da44f4e5 13324 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13325 if (!ok)
13326 return FALSE;
13327
715df9b8 13328 /* Mark dynamically referenced symbols. */
22185505 13329 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13330 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13331
715df9b8 13332 /* Grovel through relocs to find out who stays ... */
64d03ab5 13333 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13334 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13335 {
13336 asection *o;
13337
b19a8f85 13338 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13339 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13340 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13341 continue;
13342
57963c05
AM
13343 o = sub->sections;
13344 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13345 continue;
13346
7f6ab9f8
AM
13347 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13348 Also treat note sections as a root, if the section is not part
13349 of a group. */
c152c796 13350 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13351 if (!o->gc_mark
13352 && (o->flags & SEC_EXCLUDE) == 0
24007750 13353 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13354 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13355 && elf_next_in_group (o) == NULL )))
13356 {
13357 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13358 return FALSE;
13359 }
c152c796
AM
13360 }
13361
6a5bb875 13362 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13363 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13364
c152c796 13365 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13366 return elf_gc_sweep (abfd, info);
c152c796
AM
13367}
13368\f
13369/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13370
13371bfd_boolean
13372bfd_elf_gc_record_vtinherit (bfd *abfd,
13373 asection *sec,
13374 struct elf_link_hash_entry *h,
13375 bfd_vma offset)
13376{
13377 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13378 struct elf_link_hash_entry **search, *child;
ef53be89 13379 size_t extsymcount;
c152c796
AM
13380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13381
13382 /* The sh_info field of the symtab header tells us where the
13383 external symbols start. We don't care about the local symbols at
13384 this point. */
13385 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13386 if (!elf_bad_symtab (abfd))
13387 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13388
13389 sym_hashes = elf_sym_hashes (abfd);
13390 sym_hashes_end = sym_hashes + extsymcount;
13391
13392 /* Hunt down the child symbol, which is in this section at the same
13393 offset as the relocation. */
13394 for (search = sym_hashes; search != sym_hashes_end; ++search)
13395 {
13396 if ((child = *search) != NULL
13397 && (child->root.type == bfd_link_hash_defined
13398 || child->root.type == bfd_link_hash_defweak)
13399 && child->root.u.def.section == sec
13400 && child->root.u.def.value == offset)
13401 goto win;
13402 }
13403
695344c0 13404 /* xgettext:c-format */
76cfced5
AM
13405 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13406 abfd, sec, offset);
c152c796
AM
13407 bfd_set_error (bfd_error_invalid_operation);
13408 return FALSE;
13409
13410 win:
cbd0eecf 13411 if (!child->u2.vtable)
f6e332e6 13412 {
cbd0eecf
L
13413 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13414 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13415 if (!child->u2.vtable)
f6e332e6
AM
13416 return FALSE;
13417 }
c152c796
AM
13418 if (!h)
13419 {
13420 /* This *should* only be the absolute section. It could potentially
13421 be that someone has defined a non-global vtable though, which
13422 would be bad. It isn't worth paging in the local symbols to be
13423 sure though; that case should simply be handled by the assembler. */
13424
cbd0eecf 13425 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13426 }
13427 else
cbd0eecf 13428 child->u2.vtable->parent = h;
c152c796
AM
13429
13430 return TRUE;
13431}
13432
13433/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13434
13435bfd_boolean
13436bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13437 asection *sec ATTRIBUTE_UNUSED,
13438 struct elf_link_hash_entry *h,
13439 bfd_vma addend)
13440{
13441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13442 unsigned int log_file_align = bed->s->log_file_align;
13443
cbd0eecf 13444 if (!h->u2.vtable)
f6e332e6 13445 {
cbd0eecf
L
13446 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13447 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13448 if (!h->u2.vtable)
f6e332e6
AM
13449 return FALSE;
13450 }
13451
cbd0eecf 13452 if (addend >= h->u2.vtable->size)
c152c796
AM
13453 {
13454 size_t size, bytes, file_align;
cbd0eecf 13455 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13456
13457 /* While the symbol is undefined, we have to be prepared to handle
13458 a zero size. */
13459 file_align = 1 << log_file_align;
13460 if (h->root.type == bfd_link_hash_undefined)
13461 size = addend + file_align;
13462 else
13463 {
13464 size = h->size;
13465 if (addend >= size)
13466 {
13467 /* Oops! We've got a reference past the defined end of
13468 the table. This is probably a bug -- shall we warn? */
13469 size = addend + file_align;
13470 }
13471 }
13472 size = (size + file_align - 1) & -file_align;
13473
13474 /* Allocate one extra entry for use as a "done" flag for the
13475 consolidation pass. */
13476 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13477
13478 if (ptr)
13479 {
a50b1753 13480 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13481
13482 if (ptr != NULL)
13483 {
13484 size_t oldbytes;
13485
cbd0eecf 13486 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13487 * sizeof (bfd_boolean));
13488 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13489 }
13490 }
13491 else
a50b1753 13492 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13493
13494 if (ptr == NULL)
13495 return FALSE;
13496
13497 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13498 h->u2.vtable->used = ptr + 1;
13499 h->u2.vtable->size = size;
c152c796
AM
13500 }
13501
cbd0eecf 13502 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13503
13504 return TRUE;
13505}
13506
ae17ab41
CM
13507/* Map an ELF section header flag to its corresponding string. */
13508typedef struct
13509{
13510 char *flag_name;
13511 flagword flag_value;
13512} elf_flags_to_name_table;
13513
13514static elf_flags_to_name_table elf_flags_to_names [] =
13515{
13516 { "SHF_WRITE", SHF_WRITE },
13517 { "SHF_ALLOC", SHF_ALLOC },
13518 { "SHF_EXECINSTR", SHF_EXECINSTR },
13519 { "SHF_MERGE", SHF_MERGE },
13520 { "SHF_STRINGS", SHF_STRINGS },
13521 { "SHF_INFO_LINK", SHF_INFO_LINK},
13522 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13523 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13524 { "SHF_GROUP", SHF_GROUP },
13525 { "SHF_TLS", SHF_TLS },
13526 { "SHF_MASKOS", SHF_MASKOS },
13527 { "SHF_EXCLUDE", SHF_EXCLUDE },
13528};
13529
b9c361e0
JL
13530/* Returns TRUE if the section is to be included, otherwise FALSE. */
13531bfd_boolean
ae17ab41 13532bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13533 struct flag_info *flaginfo,
b9c361e0 13534 asection *section)
ae17ab41 13535{
8b127cbc 13536 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13537
8b127cbc 13538 if (!flaginfo->flags_initialized)
ae17ab41 13539 {
8b127cbc
AM
13540 bfd *obfd = info->output_bfd;
13541 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13542 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13543 int with_hex = 0;
13544 int without_hex = 0;
13545
8b127cbc 13546 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13547 {
b9c361e0 13548 unsigned i;
8b127cbc 13549 flagword (*lookup) (char *);
ae17ab41 13550
8b127cbc
AM
13551 lookup = bed->elf_backend_lookup_section_flags_hook;
13552 if (lookup != NULL)
ae17ab41 13553 {
8b127cbc 13554 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13555
13556 if (hexval != 0)
13557 {
13558 if (tf->with == with_flags)
13559 with_hex |= hexval;
13560 else if (tf->with == without_flags)
13561 without_hex |= hexval;
13562 tf->valid = TRUE;
13563 continue;
13564 }
ae17ab41 13565 }
8b127cbc 13566 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13567 {
8b127cbc 13568 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13569 {
13570 if (tf->with == with_flags)
13571 with_hex |= elf_flags_to_names[i].flag_value;
13572 else if (tf->with == without_flags)
13573 without_hex |= elf_flags_to_names[i].flag_value;
13574 tf->valid = TRUE;
13575 break;
13576 }
13577 }
8b127cbc 13578 if (!tf->valid)
b9c361e0 13579 {
68ffbac6 13580 info->callbacks->einfo
8b127cbc 13581 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13582 return FALSE;
ae17ab41
CM
13583 }
13584 }
8b127cbc
AM
13585 flaginfo->flags_initialized = TRUE;
13586 flaginfo->only_with_flags |= with_hex;
13587 flaginfo->not_with_flags |= without_hex;
ae17ab41 13588 }
ae17ab41 13589
8b127cbc 13590 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13591 return FALSE;
13592
8b127cbc 13593 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13594 return FALSE;
13595
13596 return TRUE;
ae17ab41
CM
13597}
13598
c152c796
AM
13599struct alloc_got_off_arg {
13600 bfd_vma gotoff;
10455f89 13601 struct bfd_link_info *info;
c152c796
AM
13602};
13603
13604/* We need a special top-level link routine to convert got reference counts
13605 to real got offsets. */
13606
13607static bfd_boolean
13608elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13609{
a50b1753 13610 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13611 bfd *obfd = gofarg->info->output_bfd;
13612 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13613
c152c796
AM
13614 if (h->got.refcount > 0)
13615 {
13616 h->got.offset = gofarg->gotoff;
10455f89 13617 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13618 }
13619 else
13620 h->got.offset = (bfd_vma) -1;
13621
13622 return TRUE;
13623}
13624
13625/* And an accompanying bit to work out final got entry offsets once
13626 we're done. Should be called from final_link. */
13627
13628bfd_boolean
13629bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13630 struct bfd_link_info *info)
13631{
13632 bfd *i;
13633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13634 bfd_vma gotoff;
c152c796
AM
13635 struct alloc_got_off_arg gofarg;
13636
10455f89
HPN
13637 BFD_ASSERT (abfd == info->output_bfd);
13638
c152c796
AM
13639 if (! is_elf_hash_table (info->hash))
13640 return FALSE;
13641
13642 /* The GOT offset is relative to the .got section, but the GOT header is
13643 put into the .got.plt section, if the backend uses it. */
13644 if (bed->want_got_plt)
13645 gotoff = 0;
13646 else
13647 gotoff = bed->got_header_size;
13648
13649 /* Do the local .got entries first. */
c72f2fb2 13650 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13651 {
13652 bfd_signed_vma *local_got;
ef53be89 13653 size_t j, locsymcount;
c152c796
AM
13654 Elf_Internal_Shdr *symtab_hdr;
13655
13656 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13657 continue;
13658
13659 local_got = elf_local_got_refcounts (i);
13660 if (!local_got)
13661 continue;
13662
13663 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13664 if (elf_bad_symtab (i))
13665 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13666 else
13667 locsymcount = symtab_hdr->sh_info;
13668
13669 for (j = 0; j < locsymcount; ++j)
13670 {
13671 if (local_got[j] > 0)
13672 {
13673 local_got[j] = gotoff;
10455f89 13674 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13675 }
13676 else
13677 local_got[j] = (bfd_vma) -1;
13678 }
13679 }
13680
13681 /* Then the global .got entries. .plt refcounts are handled by
13682 adjust_dynamic_symbol */
13683 gofarg.gotoff = gotoff;
10455f89 13684 gofarg.info = info;
c152c796
AM
13685 elf_link_hash_traverse (elf_hash_table (info),
13686 elf_gc_allocate_got_offsets,
13687 &gofarg);
13688 return TRUE;
13689}
13690
13691/* Many folk need no more in the way of final link than this, once
13692 got entry reference counting is enabled. */
13693
13694bfd_boolean
13695bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13696{
13697 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13698 return FALSE;
13699
13700 /* Invoke the regular ELF backend linker to do all the work. */
13701 return bfd_elf_final_link (abfd, info);
13702}
13703
13704bfd_boolean
13705bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13706{
a50b1753 13707 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13708
13709 if (rcookie->bad_symtab)
13710 rcookie->rel = rcookie->rels;
13711
13712 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13713 {
13714 unsigned long r_symndx;
13715
13716 if (! rcookie->bad_symtab)
13717 if (rcookie->rel->r_offset > offset)
13718 return FALSE;
13719 if (rcookie->rel->r_offset != offset)
13720 continue;
13721
13722 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13723 if (r_symndx == STN_UNDEF)
c152c796
AM
13724 return TRUE;
13725
13726 if (r_symndx >= rcookie->locsymcount
13727 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13728 {
13729 struct elf_link_hash_entry *h;
13730
13731 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13732
13733 while (h->root.type == bfd_link_hash_indirect
13734 || h->root.type == bfd_link_hash_warning)
13735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13736
13737 if ((h->root.type == bfd_link_hash_defined
13738 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13739 && (h->root.u.def.section->owner != rcookie->abfd
13740 || h->root.u.def.section->kept_section != NULL
13741 || discarded_section (h->root.u.def.section)))
c152c796 13742 return TRUE;
c152c796
AM
13743 }
13744 else
13745 {
13746 /* It's not a relocation against a global symbol,
13747 but it could be a relocation against a local
13748 symbol for a discarded section. */
13749 asection *isec;
13750 Elf_Internal_Sym *isym;
13751
13752 /* Need to: get the symbol; get the section. */
13753 isym = &rcookie->locsyms[r_symndx];
cb33740c 13754 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13755 if (isec != NULL
13756 && (isec->kept_section != NULL
13757 || discarded_section (isec)))
cb33740c 13758 return TRUE;
c152c796
AM
13759 }
13760 return FALSE;
13761 }
13762 return FALSE;
13763}
13764
13765/* Discard unneeded references to discarded sections.
75938853
AM
13766 Returns -1 on error, 1 if any section's size was changed, 0 if
13767 nothing changed. This function assumes that the relocations are in
13768 sorted order, which is true for all known assemblers. */
c152c796 13769
75938853 13770int
c152c796
AM
13771bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13772{
13773 struct elf_reloc_cookie cookie;
18cd5bce 13774 asection *o;
c152c796 13775 bfd *abfd;
75938853 13776 int changed = 0;
c152c796
AM
13777
13778 if (info->traditional_format
13779 || !is_elf_hash_table (info->hash))
75938853 13780 return 0;
c152c796 13781
18cd5bce
AM
13782 o = bfd_get_section_by_name (output_bfd, ".stab");
13783 if (o != NULL)
c152c796 13784 {
18cd5bce 13785 asection *i;
c152c796 13786
18cd5bce 13787 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13788 {
18cd5bce
AM
13789 if (i->size == 0
13790 || i->reloc_count == 0
13791 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13792 continue;
c152c796 13793
18cd5bce
AM
13794 abfd = i->owner;
13795 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13796 continue;
c152c796 13797
18cd5bce 13798 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13799 return -1;
c152c796 13800
18cd5bce
AM
13801 if (_bfd_discard_section_stabs (abfd, i,
13802 elf_section_data (i)->sec_info,
5241d853
RS
13803 bfd_elf_reloc_symbol_deleted_p,
13804 &cookie))
75938853 13805 changed = 1;
18cd5bce
AM
13806
13807 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13808 }
18cd5bce
AM
13809 }
13810
2f0c68f2
CM
13811 o = NULL;
13812 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13813 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13814 if (o != NULL)
13815 {
13816 asection *i;
d7153c4a 13817 int eh_changed = 0;
79a94a2a 13818 unsigned int eh_alignment;
c152c796 13819
18cd5bce 13820 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13821 {
18cd5bce
AM
13822 if (i->size == 0)
13823 continue;
13824
13825 abfd = i->owner;
13826 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13827 continue;
13828
13829 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13830 return -1;
18cd5bce
AM
13831
13832 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13833 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13834 bfd_elf_reloc_symbol_deleted_p,
13835 &cookie))
d7153c4a
AM
13836 {
13837 eh_changed = 1;
13838 if (i->size != i->rawsize)
13839 changed = 1;
13840 }
18cd5bce
AM
13841
13842 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13843 }
9866ffe2 13844
79a94a2a 13845 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
13846 /* Skip over zero terminator, and prevent empty sections from
13847 adding alignment padding at the end. */
13848 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13849 if (i->size == 0)
13850 i->flags |= SEC_EXCLUDE;
13851 else if (i->size > 4)
13852 break;
13853 /* The last non-empty eh_frame section doesn't need padding. */
13854 if (i != NULL)
13855 i = i->map_tail.s;
13856 /* Any prior sections must pad the last FDE out to the output
13857 section alignment. Otherwise we might have zero padding
13858 between sections, which would be seen as a terminator. */
13859 for (; i != NULL; i = i->map_tail.s)
13860 if (i->size == 4)
13861 /* All but the last zero terminator should have been removed. */
13862 BFD_FAIL ();
13863 else
13864 {
13865 bfd_size_type size
13866 = (i->size + eh_alignment - 1) & -eh_alignment;
13867 if (i->size != size)
af471f82 13868 {
9866ffe2
AM
13869 i->size = size;
13870 changed = 1;
13871 eh_changed = 1;
af471f82 13872 }
9866ffe2 13873 }
d7153c4a
AM
13874 if (eh_changed)
13875 elf_link_hash_traverse (elf_hash_table (info),
13876 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13877 }
c152c796 13878
18cd5bce
AM
13879 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13880 {
13881 const struct elf_backend_data *bed;
57963c05 13882 asection *s;
c152c796 13883
18cd5bce
AM
13884 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13885 continue;
57963c05
AM
13886 s = abfd->sections;
13887 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13888 continue;
18cd5bce
AM
13889
13890 bed = get_elf_backend_data (abfd);
13891
13892 if (bed->elf_backend_discard_info != NULL)
13893 {
13894 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13895 return -1;
18cd5bce
AM
13896
13897 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13898 changed = 1;
18cd5bce
AM
13899
13900 fini_reloc_cookie (&cookie, abfd);
13901 }
c152c796
AM
13902 }
13903
2f0c68f2
CM
13904 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13905 _bfd_elf_end_eh_frame_parsing (info);
13906
13907 if (info->eh_frame_hdr_type
0e1862bb 13908 && !bfd_link_relocatable (info)
c152c796 13909 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13910 changed = 1;
c152c796 13911
75938853 13912 return changed;
c152c796 13913}
082b7297 13914
43e1669b 13915bfd_boolean
0c511000 13916_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13917 asection *sec,
c0f00686 13918 struct bfd_link_info *info)
082b7297
L
13919{
13920 flagword flags;
c77ec726 13921 const char *name, *key;
082b7297
L
13922 struct bfd_section_already_linked *l;
13923 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13924
c77ec726
AM
13925 if (sec->output_section == bfd_abs_section_ptr)
13926 return FALSE;
0c511000 13927
c77ec726 13928 flags = sec->flags;
0c511000 13929
c77ec726
AM
13930 /* Return if it isn't a linkonce section. A comdat group section
13931 also has SEC_LINK_ONCE set. */
13932 if ((flags & SEC_LINK_ONCE) == 0)
13933 return FALSE;
0c511000 13934
c77ec726
AM
13935 /* Don't put group member sections on our list of already linked
13936 sections. They are handled as a group via their group section. */
13937 if (elf_sec_group (sec) != NULL)
13938 return FALSE;
0c511000 13939
c77ec726
AM
13940 /* For a SHT_GROUP section, use the group signature as the key. */
13941 name = sec->name;
13942 if ((flags & SEC_GROUP) != 0
13943 && elf_next_in_group (sec) != NULL
13944 && elf_group_name (elf_next_in_group (sec)) != NULL)
13945 key = elf_group_name (elf_next_in_group (sec));
13946 else
13947 {
13948 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13949 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13950 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13951 key++;
0c511000 13952 else
c77ec726
AM
13953 /* Must be a user linkonce section that doesn't follow gcc's
13954 naming convention. In this case we won't be matching
13955 single member groups. */
13956 key = name;
0c511000 13957 }
6d2cd210 13958
c77ec726 13959 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13960
13961 for (l = already_linked_list->entry; l != NULL; l = l->next)
13962 {
c2370991 13963 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13964 sections with a signature of <key> (<key> is some string),
13965 and linkonce sections named .gnu.linkonce.<type>.<key>.
13966 Match like sections. LTO plugin sections are an exception.
13967 They are always named .gnu.linkonce.t.<key> and match either
13968 type of section. */
13969 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13970 && ((flags & SEC_GROUP) != 0
13971 || strcmp (name, l->sec->name) == 0))
13972 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13973 {
13974 /* The section has already been linked. See if we should
6d2cd210 13975 issue a warning. */
c77ec726
AM
13976 if (!_bfd_handle_already_linked (sec, l, info))
13977 return FALSE;
082b7297 13978
c77ec726 13979 if (flags & SEC_GROUP)
3d7f7666 13980 {
c77ec726
AM
13981 asection *first = elf_next_in_group (sec);
13982 asection *s = first;
3d7f7666 13983
c77ec726 13984 while (s != NULL)
3d7f7666 13985 {
c77ec726
AM
13986 s->output_section = bfd_abs_section_ptr;
13987 /* Record which group discards it. */
13988 s->kept_section = l->sec;
13989 s = elf_next_in_group (s);
13990 /* These lists are circular. */
13991 if (s == first)
13992 break;
3d7f7666
L
13993 }
13994 }
082b7297 13995
43e1669b 13996 return TRUE;
082b7297
L
13997 }
13998 }
13999
c77ec726
AM
14000 /* A single member comdat group section may be discarded by a
14001 linkonce section and vice versa. */
14002 if ((flags & SEC_GROUP) != 0)
3d7f7666 14003 {
c77ec726 14004 asection *first = elf_next_in_group (sec);
c2370991 14005
c77ec726
AM
14006 if (first != NULL && elf_next_in_group (first) == first)
14007 /* Check this single member group against linkonce sections. */
14008 for (l = already_linked_list->entry; l != NULL; l = l->next)
14009 if ((l->sec->flags & SEC_GROUP) == 0
14010 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14011 {
14012 first->output_section = bfd_abs_section_ptr;
14013 first->kept_section = l->sec;
14014 sec->output_section = bfd_abs_section_ptr;
14015 break;
14016 }
14017 }
14018 else
14019 /* Check this linkonce section against single member groups. */
14020 for (l = already_linked_list->entry; l != NULL; l = l->next)
14021 if (l->sec->flags & SEC_GROUP)
6d2cd210 14022 {
c77ec726 14023 asection *first = elf_next_in_group (l->sec);
6d2cd210 14024
c77ec726
AM
14025 if (first != NULL
14026 && elf_next_in_group (first) == first
14027 && bfd_elf_match_symbols_in_sections (first, sec, info))
14028 {
14029 sec->output_section = bfd_abs_section_ptr;
14030 sec->kept_section = first;
14031 break;
14032 }
6d2cd210 14033 }
0c511000 14034
c77ec726
AM
14035 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14036 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14037 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14038 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14039 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14040 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14041 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14042 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14043 The reverse order cannot happen as there is never a bfd with only the
14044 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14045 matter as here were are looking only for cross-bfd sections. */
14046
14047 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14048 for (l = already_linked_list->entry; l != NULL; l = l->next)
14049 if ((l->sec->flags & SEC_GROUP) == 0
14050 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14051 {
14052 if (abfd != l->sec->owner)
14053 sec->output_section = bfd_abs_section_ptr;
14054 break;
14055 }
80c29487 14056
082b7297 14057 /* This is the first section with this name. Record it. */
c77ec726 14058 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14059 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14060 return sec->output_section == bfd_abs_section_ptr;
082b7297 14061}
81e1b023 14062
a4d8e49b
L
14063bfd_boolean
14064_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14065{
14066 return sym->st_shndx == SHN_COMMON;
14067}
14068
14069unsigned int
14070_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14071{
14072 return SHN_COMMON;
14073}
14074
14075asection *
14076_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14077{
14078 return bfd_com_section_ptr;
14079}
10455f89
HPN
14080
14081bfd_vma
14082_bfd_elf_default_got_elt_size (bfd *abfd,
14083 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14084 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14085 bfd *ibfd ATTRIBUTE_UNUSED,
14086 unsigned long symndx ATTRIBUTE_UNUSED)
14087{
14088 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14089 return bed->s->arch_size / 8;
14090}
83bac4b0
NC
14091
14092/* Routines to support the creation of dynamic relocs. */
14093
83bac4b0
NC
14094/* Returns the name of the dynamic reloc section associated with SEC. */
14095
14096static const char *
14097get_dynamic_reloc_section_name (bfd * abfd,
14098 asection * sec,
14099 bfd_boolean is_rela)
14100{
ddcf1fcf
BS
14101 char *name;
14102 const char *old_name = bfd_get_section_name (NULL, sec);
14103 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14104
ddcf1fcf 14105 if (old_name == NULL)
83bac4b0
NC
14106 return NULL;
14107
ddcf1fcf 14108 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14109 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14110
14111 return name;
14112}
14113
14114/* Returns the dynamic reloc section associated with SEC.
14115 If necessary compute the name of the dynamic reloc section based
14116 on SEC's name (looked up in ABFD's string table) and the setting
14117 of IS_RELA. */
14118
14119asection *
14120_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14121 asection * sec,
14122 bfd_boolean is_rela)
14123{
14124 asection * reloc_sec = elf_section_data (sec)->sreloc;
14125
14126 if (reloc_sec == NULL)
14127 {
14128 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14129
14130 if (name != NULL)
14131 {
3d4d4302 14132 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14133
14134 if (reloc_sec != NULL)
14135 elf_section_data (sec)->sreloc = reloc_sec;
14136 }
14137 }
14138
14139 return reloc_sec;
14140}
14141
14142/* Returns the dynamic reloc section associated with SEC. If the
14143 section does not exist it is created and attached to the DYNOBJ
14144 bfd and stored in the SRELOC field of SEC's elf_section_data
14145 structure.
f8076f98 14146
83bac4b0
NC
14147 ALIGNMENT is the alignment for the newly created section and
14148 IS_RELA defines whether the name should be .rela.<SEC's name>
14149 or .rel.<SEC's name>. The section name is looked up in the
14150 string table associated with ABFD. */
14151
14152asection *
ca4be51c
AM
14153_bfd_elf_make_dynamic_reloc_section (asection *sec,
14154 bfd *dynobj,
14155 unsigned int alignment,
14156 bfd *abfd,
14157 bfd_boolean is_rela)
83bac4b0
NC
14158{
14159 asection * reloc_sec = elf_section_data (sec)->sreloc;
14160
14161 if (reloc_sec == NULL)
14162 {
14163 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14164
14165 if (name == NULL)
14166 return NULL;
14167
3d4d4302 14168 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14169
14170 if (reloc_sec == NULL)
14171 {
3d4d4302
AM
14172 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14173 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14174 if ((sec->flags & SEC_ALLOC) != 0)
14175 flags |= SEC_ALLOC | SEC_LOAD;
14176
3d4d4302 14177 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14178 if (reloc_sec != NULL)
14179 {
8877b5e5
AM
14180 /* _bfd_elf_get_sec_type_attr chooses a section type by
14181 name. Override as it may be wrong, eg. for a user
14182 section named "auto" we'll get ".relauto" which is
14183 seen to be a .rela section. */
14184 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14185 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14186 reloc_sec = NULL;
14187 }
14188 }
14189
14190 elf_section_data (sec)->sreloc = reloc_sec;
14191 }
14192
14193 return reloc_sec;
14194}
1338dd10 14195
bffebb6b
AM
14196/* Copy the ELF symbol type and other attributes for a linker script
14197 assignment from HSRC to HDEST. Generally this should be treated as
14198 if we found a strong non-dynamic definition for HDEST (except that
14199 ld ignores multiple definition errors). */
1338dd10 14200void
bffebb6b
AM
14201_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14202 struct bfd_link_hash_entry *hdest,
14203 struct bfd_link_hash_entry *hsrc)
1338dd10 14204{
bffebb6b
AM
14205 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14206 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14207 Elf_Internal_Sym isym;
1338dd10
PB
14208
14209 ehdest->type = ehsrc->type;
35fc36a8 14210 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14211
14212 isym.st_other = ehsrc->other;
b8417128 14213 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14214}
351f65ca
L
14215
14216/* Append a RELA relocation REL to section S in BFD. */
14217
14218void
14219elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14220{
14221 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14222 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14223 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14224 bed->s->swap_reloca_out (abfd, rel, loc);
14225}
14226
14227/* Append a REL relocation REL to section S in BFD. */
14228
14229void
14230elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14231{
14232 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14233 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14234 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14235 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14236}
7dba9362
AM
14237
14238/* Define __start, __stop, .startof. or .sizeof. symbol. */
14239
14240struct bfd_link_hash_entry *
14241bfd_elf_define_start_stop (struct bfd_link_info *info,
14242 const char *symbol, asection *sec)
14243{
487b6440 14244 struct elf_link_hash_entry *h;
7dba9362 14245
487b6440
AM
14246 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14247 FALSE, FALSE, TRUE);
14248 if (h != NULL
14249 && (h->root.type == bfd_link_hash_undefined
14250 || h->root.type == bfd_link_hash_undefweak
14251 || (h->ref_regular && !h->def_regular)))
7dba9362 14252 {
487b6440
AM
14253 h->root.type = bfd_link_hash_defined;
14254 h->root.u.def.section = sec;
14255 h->root.u.def.value = 0;
14256 h->def_regular = 1;
14257 h->def_dynamic = 0;
14258 h->start_stop = 1;
14259 h->u2.start_stop_section = sec;
14260 if (symbol[0] == '.')
14261 {
14262 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14263 const struct elf_backend_data *bed;
14264 bed = get_elf_backend_data (info->output_bfd);
14265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440
AM
14266 }
14267 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14268 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14269 return &h->root;
7dba9362 14270 }
487b6440 14271 return NULL;
7dba9362 14272}