_bfd_elf_link_hash_hide_symbol calls in generic ELF code
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
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;
45d6a902
AM
1039
1040 *skip = FALSE;
1041 *override = FALSE;
1042
1043 sec = *psec;
1044 bind = ELF_ST_BIND (sym->st_info);
1045
1046 if (! bfd_is_und_section (sec))
1047 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1048 else
1049 h = ((struct elf_link_hash_entry *)
1050 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1051 if (h == NULL)
1052 return FALSE;
1053 *sym_hash = h;
252b5132 1054
88ba32a0
L
1055 bed = get_elf_backend_data (abfd);
1056
6e33951e 1057 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1058 if (h->versioned != unversioned)
6e33951e 1059 {
422f1182
L
1060 /* Symbol version is unknown or versioned. */
1061 new_version = strrchr (name, ELF_VER_CHR);
1062 if (new_version)
1063 {
1064 if (h->versioned == unknown)
1065 {
1066 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1067 h->versioned = versioned_hidden;
1068 else
1069 h->versioned = versioned;
1070 }
1071 new_version += 1;
1072 if (new_version[0] == '\0')
1073 new_version = NULL;
1074 }
1075 else
1076 h->versioned = unversioned;
6e33951e 1077 }
422f1182
L
1078 else
1079 new_version = NULL;
6e33951e 1080
90c984fc
L
1081 /* For merging, we only care about real symbols. But we need to make
1082 sure that indirect symbol dynamic flags are updated. */
1083 hi = h;
45d6a902
AM
1084 while (h->root.type == bfd_link_hash_indirect
1085 || h->root.type == bfd_link_hash_warning)
1086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1087
6e33951e
L
1088 if (!*matched)
1089 {
1090 if (hi == h || h->root.type == bfd_link_hash_new)
1091 *matched = TRUE;
1092 else
1093 {
ae7683d2 1094 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1095 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1096 true if the new symbol is only visible to the symbol with
6e33951e 1097 the same symbol version. */
422f1182
L
1098 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1099 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1100 if (!old_hidden && !new_hidden)
1101 /* The new symbol matches the existing symbol if both
1102 aren't hidden. */
1103 *matched = TRUE;
1104 else
1105 {
1106 /* OLD_VERSION is the symbol version of the existing
1107 symbol. */
422f1182
L
1108 char *old_version;
1109
1110 if (h->versioned >= versioned)
1111 old_version = strrchr (h->root.root.string,
1112 ELF_VER_CHR) + 1;
1113 else
1114 old_version = NULL;
6e33951e
L
1115
1116 /* The new symbol matches the existing symbol if they
1117 have the same symbol version. */
1118 *matched = (old_version == new_version
1119 || (old_version != NULL
1120 && new_version != NULL
1121 && strcmp (old_version, new_version) == 0));
1122 }
1123 }
1124 }
1125
934bce08
AM
1126 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1127 existing symbol. */
1128
1129 oldbfd = NULL;
1130 oldsec = NULL;
1131 switch (h->root.type)
1132 {
1133 default:
1134 break;
1135
1136 case bfd_link_hash_undefined:
1137 case bfd_link_hash_undefweak:
1138 oldbfd = h->root.u.undef.abfd;
1139 break;
1140
1141 case bfd_link_hash_defined:
1142 case bfd_link_hash_defweak:
1143 oldbfd = h->root.u.def.section->owner;
1144 oldsec = h->root.u.def.section;
1145 break;
1146
1147 case bfd_link_hash_common:
1148 oldbfd = h->root.u.c.p->section->owner;
1149 oldsec = h->root.u.c.p->section;
1150 if (pold_alignment)
1151 *pold_alignment = h->root.u.c.p->alignment_power;
1152 break;
1153 }
1154 if (poldbfd && *poldbfd == NULL)
1155 *poldbfd = oldbfd;
1156
1157 /* Differentiate strong and weak symbols. */
1158 newweak = bind == STB_WEAK;
1159 oldweak = (h->root.type == bfd_link_hash_defweak
1160 || h->root.type == bfd_link_hash_undefweak);
1161 if (pold_weak)
1162 *pold_weak = oldweak;
1163
1164 /* This code is for coping with dynamic objects, and is only useful
1165 if we are doing an ELF link. */
1166 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1167 return TRUE;
1168
40b36307 1169 /* We have to check it for every instance since the first few may be
ee659f1f 1170 references and not all compilers emit symbol type for undefined
40b36307
L
1171 symbols. */
1172 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1173
ee659f1f
AM
1174 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1175 respectively, is from a dynamic object. */
1176
1177 newdyn = (abfd->flags & DYNAMIC) != 0;
1178
1179 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1180 syms and defined syms in dynamic libraries respectively.
1181 ref_dynamic on the other hand can be set for a symbol defined in
1182 a dynamic library, and def_dynamic may not be set; When the
1183 definition in a dynamic lib is overridden by a definition in the
1184 executable use of the symbol in the dynamic lib becomes a
1185 reference to the executable symbol. */
1186 if (newdyn)
1187 {
1188 if (bfd_is_und_section (sec))
1189 {
1190 if (bind != STB_WEAK)
1191 {
1192 h->ref_dynamic_nonweak = 1;
1193 hi->ref_dynamic_nonweak = 1;
1194 }
1195 }
1196 else
1197 {
6e33951e
L
1198 /* Update the existing symbol only if they match. */
1199 if (*matched)
1200 h->dynamic_def = 1;
ee659f1f
AM
1201 hi->dynamic_def = 1;
1202 }
1203 }
1204
45d6a902
AM
1205 /* If we just created the symbol, mark it as being an ELF symbol.
1206 Other than that, there is nothing to do--there is no merge issue
1207 with a newly defined symbol--so we just return. */
1208
1209 if (h->root.type == bfd_link_hash_new)
252b5132 1210 {
f5385ebf 1211 h->non_elf = 0;
45d6a902
AM
1212 return TRUE;
1213 }
252b5132 1214
45d6a902
AM
1215 /* In cases involving weak versioned symbols, we may wind up trying
1216 to merge a symbol with itself. Catch that here, to avoid the
1217 confusion that results if we try to override a symbol with
1218 itself. The additional tests catch cases like
1219 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1220 dynamic object, which we do want to handle here. */
1221 if (abfd == oldbfd
895fa45f 1222 && (newweak || oldweak)
45d6a902 1223 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1224 || !h->def_regular))
45d6a902
AM
1225 return TRUE;
1226
707bba77 1227 olddyn = FALSE;
45d6a902
AM
1228 if (oldbfd != NULL)
1229 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1230 else if (oldsec != NULL)
45d6a902 1231 {
707bba77 1232 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1233 indices used by MIPS ELF. */
707bba77 1234 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1235 }
252b5132 1236
1a3b5c34
AM
1237 /* Handle a case where plugin_notice won't be called and thus won't
1238 set the non_ir_ref flags on the first pass over symbols. */
1239 if (oldbfd != NULL
1240 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1241 && newdyn != olddyn)
1242 {
1243 h->root.non_ir_ref_dynamic = TRUE;
1244 hi->root.non_ir_ref_dynamic = TRUE;
1245 }
1246
45d6a902
AM
1247 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1248 respectively, appear to be a definition rather than reference. */
1249
707bba77 1250 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1251
707bba77
AM
1252 olddef = (h->root.type != bfd_link_hash_undefined
1253 && h->root.type != bfd_link_hash_undefweak
202ac193 1254 && h->root.type != bfd_link_hash_common);
45d6a902 1255
0a36a439
L
1256 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1257 respectively, appear to be a function. */
1258
1259 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1260 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1261
1262 oldfunc = (h->type != STT_NOTYPE
1263 && bed->is_function_type (h->type));
1264
c5d37467 1265 if (!(newfunc && oldfunc)
5b677558
AM
1266 && ELF_ST_TYPE (sym->st_info) != h->type
1267 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1268 && h->type != STT_NOTYPE
c5d37467
AM
1269 && (newdef || bfd_is_com_section (sec))
1270 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1271 {
c5d37467
AM
1272 /* If creating a default indirect symbol ("foo" or "foo@") from
1273 a dynamic versioned definition ("foo@@") skip doing so if
1274 there is an existing regular definition with a different
1275 type. We don't want, for example, a "time" variable in the
1276 executable overriding a "time" function in a shared library. */
1277 if (newdyn
1278 && !olddyn)
1279 {
1280 *skip = TRUE;
1281 return TRUE;
1282 }
1283
1284 /* When adding a symbol from a regular object file after we have
1285 created indirect symbols, undo the indirection and any
1286 dynamic state. */
1287 if (hi != h
1288 && !newdyn
1289 && olddyn)
1290 {
1291 h = hi;
1292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1293 h->forced_local = 0;
1294 h->ref_dynamic = 0;
1295 h->def_dynamic = 0;
1296 h->dynamic_def = 0;
1297 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1298 {
1299 h->root.type = bfd_link_hash_undefined;
1300 h->root.u.undef.abfd = abfd;
1301 }
1302 else
1303 {
1304 h->root.type = bfd_link_hash_new;
1305 h->root.u.undef.abfd = NULL;
1306 }
1307 return TRUE;
1308 }
580a2b6e
L
1309 }
1310
4c34aff8
AM
1311 /* Check TLS symbols. We don't check undefined symbols introduced
1312 by "ld -u" which have no type (and oldbfd NULL), and we don't
1313 check symbols from plugins because they also have no type. */
1314 if (oldbfd != NULL
1315 && (oldbfd->flags & BFD_PLUGIN) == 0
1316 && (abfd->flags & BFD_PLUGIN) == 0
1317 && ELF_ST_TYPE (sym->st_info) != h->type
1318 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1319 {
1320 bfd *ntbfd, *tbfd;
1321 bfd_boolean ntdef, tdef;
1322 asection *ntsec, *tsec;
1323
1324 if (h->type == STT_TLS)
1325 {
3b36f7e6 1326 ntbfd = abfd;
7479dfd4
L
1327 ntsec = sec;
1328 ntdef = newdef;
1329 tbfd = oldbfd;
1330 tsec = oldsec;
1331 tdef = olddef;
1332 }
1333 else
1334 {
1335 ntbfd = oldbfd;
1336 ntsec = oldsec;
1337 ntdef = olddef;
1338 tbfd = abfd;
1339 tsec = sec;
1340 tdef = newdef;
1341 }
1342
1343 if (tdef && ntdef)
4eca0228 1344 _bfd_error_handler
695344c0 1345 /* xgettext:c-format */
191c0c42
AM
1346 (_("%s: TLS definition in %B section %A "
1347 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1348 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1349 else if (!tdef && !ntdef)
4eca0228 1350 _bfd_error_handler
695344c0 1351 /* xgettext:c-format */
191c0c42
AM
1352 (_("%s: TLS reference in %B "
1353 "mismatches non-TLS reference in %B"),
c08bb8dd 1354 h->root.root.string, tbfd, ntbfd);
7479dfd4 1355 else if (tdef)
4eca0228 1356 _bfd_error_handler
695344c0 1357 /* xgettext:c-format */
191c0c42
AM
1358 (_("%s: TLS definition in %B section %A "
1359 "mismatches non-TLS reference in %B"),
c08bb8dd 1360 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1361 else
4eca0228 1362 _bfd_error_handler
695344c0 1363 /* xgettext:c-format */
191c0c42
AM
1364 (_("%s: TLS reference in %B "
1365 "mismatches non-TLS definition in %B section %A"),
c08bb8dd 1366 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1367
1368 bfd_set_error (bfd_error_bad_value);
1369 return FALSE;
1370 }
1371
45d6a902
AM
1372 /* If the old symbol has non-default visibility, we ignore the new
1373 definition from a dynamic object. */
1374 if (newdyn
9c7a29a3 1375 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1376 && !bfd_is_und_section (sec))
1377 {
1378 *skip = TRUE;
1379 /* Make sure this symbol is dynamic. */
f5385ebf 1380 h->ref_dynamic = 1;
90c984fc 1381 hi->ref_dynamic = 1;
45d6a902
AM
1382 /* A protected symbol has external availability. Make sure it is
1383 recorded as dynamic.
1384
1385 FIXME: Should we check type and size for protected symbol? */
1386 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1387 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1388 else
1389 return TRUE;
1390 }
1391 else if (!newdyn
9c7a29a3 1392 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1393 && h->def_dynamic)
45d6a902
AM
1394 {
1395 /* If the new symbol with non-default visibility comes from a
1396 relocatable file and the old definition comes from a dynamic
1397 object, we remove the old definition. */
6c9b78e6 1398 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1399 {
1400 /* Handle the case where the old dynamic definition is
1401 default versioned. We need to copy the symbol info from
1402 the symbol with default version to the normal one if it
1403 was referenced before. */
1404 if (h->ref_regular)
1405 {
6c9b78e6 1406 hi->root.type = h->root.type;
d2dee3b2 1407 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1408 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1409
6c9b78e6 1410 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1411 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1412 {
aed81c4e
MR
1413 /* If the new symbol is hidden or internal, completely undo
1414 any dynamic link state. */
1415 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1416 h->forced_local = 0;
1417 h->ref_dynamic = 0;
d2dee3b2
L
1418 }
1419 else
aed81c4e
MR
1420 h->ref_dynamic = 1;
1421
1422 h->def_dynamic = 0;
aed81c4e
MR
1423 /* FIXME: Should we check type and size for protected symbol? */
1424 h->size = 0;
1425 h->type = 0;
1426
6c9b78e6 1427 h = hi;
d2dee3b2
L
1428 }
1429 else
6c9b78e6 1430 h = hi;
d2dee3b2 1431 }
1de1a317 1432
f5eda473
AM
1433 /* If the old symbol was undefined before, then it will still be
1434 on the undefs list. If the new symbol is undefined or
1435 common, we can't make it bfd_link_hash_new here, because new
1436 undefined or common symbols will be added to the undefs list
1437 by _bfd_generic_link_add_one_symbol. Symbols may not be
1438 added twice to the undefs list. Also, if the new symbol is
1439 undefweak then we don't want to lose the strong undef. */
1440 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1441 {
1de1a317 1442 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1443 h->root.u.undef.abfd = abfd;
1444 }
1445 else
1446 {
1447 h->root.type = bfd_link_hash_new;
1448 h->root.u.undef.abfd = NULL;
1449 }
1450
f5eda473 1451 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1452 {
f5eda473
AM
1453 /* If the new symbol is hidden or internal, completely undo
1454 any dynamic link state. */
1455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1456 h->forced_local = 0;
1457 h->ref_dynamic = 0;
45d6a902 1458 }
f5eda473
AM
1459 else
1460 h->ref_dynamic = 1;
1461 h->def_dynamic = 0;
45d6a902
AM
1462 /* FIXME: Should we check type and size for protected symbol? */
1463 h->size = 0;
1464 h->type = 0;
1465 return TRUE;
1466 }
14a793b2 1467
15b43f48
AM
1468 /* If a new weak symbol definition comes from a regular file and the
1469 old symbol comes from a dynamic library, we treat the new one as
1470 strong. Similarly, an old weak symbol definition from a regular
1471 file is treated as strong when the new symbol comes from a dynamic
1472 library. Further, an old weak symbol from a dynamic library is
1473 treated as strong if the new symbol is from a dynamic library.
1474 This reflects the way glibc's ld.so works.
1475
1476 Do this before setting *type_change_ok or *size_change_ok so that
1477 we warn properly when dynamic library symbols are overridden. */
1478
1479 if (newdef && !newdyn && olddyn)
0f8a2703 1480 newweak = FALSE;
15b43f48 1481 if (olddef && newdyn)
0f8a2703
AM
1482 oldweak = FALSE;
1483
d334575b 1484 /* Allow changes between different types of function symbol. */
0a36a439 1485 if (newfunc && oldfunc)
fcb93ecf
PB
1486 *type_change_ok = TRUE;
1487
79349b09
AM
1488 /* It's OK to change the type if either the existing symbol or the
1489 new symbol is weak. A type change is also OK if the old symbol
1490 is undefined and the new symbol is defined. */
252b5132 1491
79349b09
AM
1492 if (oldweak
1493 || newweak
1494 || (newdef
1495 && h->root.type == bfd_link_hash_undefined))
1496 *type_change_ok = TRUE;
1497
1498 /* It's OK to change the size if either the existing symbol or the
1499 new symbol is weak, or if the old symbol is undefined. */
1500
1501 if (*type_change_ok
1502 || h->root.type == bfd_link_hash_undefined)
1503 *size_change_ok = TRUE;
45d6a902 1504
45d6a902
AM
1505 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1506 symbol, respectively, appears to be a common symbol in a dynamic
1507 object. If a symbol appears in an uninitialized section, and is
1508 not weak, and is not a function, then it may be a common symbol
1509 which was resolved when the dynamic object was created. We want
1510 to treat such symbols specially, because they raise special
1511 considerations when setting the symbol size: if the symbol
1512 appears as a common symbol in a regular object, and the size in
1513 the regular object is larger, we must make sure that we use the
1514 larger size. This problematic case can always be avoided in C,
1515 but it must be handled correctly when using Fortran shared
1516 libraries.
1517
1518 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1519 likewise for OLDDYNCOMMON and OLDDEF.
1520
1521 Note that this test is just a heuristic, and that it is quite
1522 possible to have an uninitialized symbol in a shared object which
1523 is really a definition, rather than a common symbol. This could
1524 lead to some minor confusion when the symbol really is a common
1525 symbol in some regular object. However, I think it will be
1526 harmless. */
1527
1528 if (newdyn
1529 && newdef
79349b09 1530 && !newweak
45d6a902
AM
1531 && (sec->flags & SEC_ALLOC) != 0
1532 && (sec->flags & SEC_LOAD) == 0
1533 && sym->st_size > 0
0a36a439 1534 && !newfunc)
45d6a902
AM
1535 newdyncommon = TRUE;
1536 else
1537 newdyncommon = FALSE;
1538
1539 if (olddyn
1540 && olddef
1541 && h->root.type == bfd_link_hash_defined
f5385ebf 1542 && h->def_dynamic
45d6a902
AM
1543 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1544 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1545 && h->size > 0
0a36a439 1546 && !oldfunc)
45d6a902
AM
1547 olddyncommon = TRUE;
1548 else
1549 olddyncommon = FALSE;
1550
a4d8e49b
L
1551 /* We now know everything about the old and new symbols. We ask the
1552 backend to check if we can merge them. */
5d13b3b3
AM
1553 if (bed->merge_symbol != NULL)
1554 {
1555 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1556 return FALSE;
1557 sec = *psec;
1558 }
a4d8e49b 1559
45d6a902
AM
1560 /* If both the old and the new symbols look like common symbols in a
1561 dynamic object, set the size of the symbol to the larger of the
1562 two. */
1563
1564 if (olddyncommon
1565 && newdyncommon
1566 && sym->st_size != h->size)
1567 {
1568 /* Since we think we have two common symbols, issue a multiple
1569 common warning if desired. Note that we only warn if the
1570 size is different. If the size is the same, we simply let
1571 the old symbol override the new one as normally happens with
1572 symbols defined in dynamic objects. */
1573
1a72702b
AM
1574 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1575 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1576 if (sym->st_size > h->size)
1577 h->size = sym->st_size;
252b5132 1578
45d6a902 1579 *size_change_ok = TRUE;
252b5132
RH
1580 }
1581
45d6a902
AM
1582 /* If we are looking at a dynamic object, and we have found a
1583 definition, we need to see if the symbol was already defined by
1584 some other object. If so, we want to use the existing
1585 definition, and we do not want to report a multiple symbol
1586 definition error; we do this by clobbering *PSEC to be
1587 bfd_und_section_ptr.
1588
1589 We treat a common symbol as a definition if the symbol in the
1590 shared library is a function, since common symbols always
1591 represent variables; this can cause confusion in principle, but
1592 any such confusion would seem to indicate an erroneous program or
1593 shared library. We also permit a common symbol in a regular
8170f769 1594 object to override a weak symbol in a shared object. */
45d6a902
AM
1595
1596 if (newdyn
1597 && newdef
77cfaee6 1598 && (olddef
45d6a902 1599 || (h->root.type == bfd_link_hash_common
8170f769 1600 && (newweak || newfunc))))
45d6a902
AM
1601 {
1602 *override = TRUE;
1603 newdef = FALSE;
1604 newdyncommon = FALSE;
252b5132 1605
45d6a902
AM
1606 *psec = sec = bfd_und_section_ptr;
1607 *size_change_ok = TRUE;
252b5132 1608
45d6a902
AM
1609 /* If we get here when the old symbol is a common symbol, then
1610 we are explicitly letting it override a weak symbol or
1611 function in a dynamic object, and we don't want to warn about
1612 a type change. If the old symbol is a defined symbol, a type
1613 change warning may still be appropriate. */
252b5132 1614
45d6a902
AM
1615 if (h->root.type == bfd_link_hash_common)
1616 *type_change_ok = TRUE;
1617 }
1618
1619 /* Handle the special case of an old common symbol merging with a
1620 new symbol which looks like a common symbol in a shared object.
1621 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1622 common symbol, and let _bfd_generic_link_add_one_symbol do the
1623 right thing. */
45d6a902
AM
1624
1625 if (newdyncommon
1626 && h->root.type == bfd_link_hash_common)
1627 {
1628 *override = TRUE;
1629 newdef = FALSE;
1630 newdyncommon = FALSE;
1631 *pvalue = sym->st_size;
a4d8e49b 1632 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1633 *size_change_ok = TRUE;
1634 }
1635
c5e2cead 1636 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1637 if (newdef && olddef && newweak)
54ac0771 1638 {
35ed3f94 1639 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1640 if (!(oldbfd != NULL
1641 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1642 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1643 {
1644 newdef = FALSE;
1645 *skip = TRUE;
1646 }
54ac0771
L
1647
1648 /* Merge st_other. If the symbol already has a dynamic index,
1649 but visibility says it should not be visible, turn it into a
1650 local symbol. */
b8417128 1651 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1652 if (h->dynindx != -1)
1653 switch (ELF_ST_VISIBILITY (h->other))
1654 {
1655 case STV_INTERNAL:
1656 case STV_HIDDEN:
1657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1658 break;
1659 }
1660 }
c5e2cead 1661
45d6a902
AM
1662 /* If the old symbol is from a dynamic object, and the new symbol is
1663 a definition which is not from a dynamic object, then the new
1664 symbol overrides the old symbol. Symbols from regular files
1665 always take precedence over symbols from dynamic objects, even if
1666 they are defined after the dynamic object in the link.
1667
1668 As above, we again permit a common symbol in a regular object to
1669 override a definition in a shared object if the shared object
0f8a2703 1670 symbol is a function or is weak. */
45d6a902
AM
1671
1672 flip = NULL;
77cfaee6 1673 if (!newdyn
45d6a902
AM
1674 && (newdef
1675 || (bfd_is_com_section (sec)
0a36a439 1676 && (oldweak || oldfunc)))
45d6a902
AM
1677 && olddyn
1678 && olddef
f5385ebf 1679 && h->def_dynamic)
45d6a902
AM
1680 {
1681 /* Change the hash table entry to undefined, and let
1682 _bfd_generic_link_add_one_symbol do the right thing with the
1683 new definition. */
1684
1685 h->root.type = bfd_link_hash_undefined;
1686 h->root.u.undef.abfd = h->root.u.def.section->owner;
1687 *size_change_ok = TRUE;
1688
1689 olddef = FALSE;
1690 olddyncommon = FALSE;
1691
1692 /* We again permit a type change when a common symbol may be
1693 overriding a function. */
1694
1695 if (bfd_is_com_section (sec))
0a36a439
L
1696 {
1697 if (oldfunc)
1698 {
1699 /* If a common symbol overrides a function, make sure
1700 that it isn't defined dynamically nor has type
1701 function. */
1702 h->def_dynamic = 0;
1703 h->type = STT_NOTYPE;
1704 }
1705 *type_change_ok = TRUE;
1706 }
45d6a902 1707
6c9b78e6
AM
1708 if (hi->root.type == bfd_link_hash_indirect)
1709 flip = hi;
45d6a902
AM
1710 else
1711 /* This union may have been set to be non-NULL when this symbol
1712 was seen in a dynamic object. We must force the union to be
1713 NULL, so that it is correct for a regular symbol. */
1714 h->verinfo.vertree = NULL;
1715 }
1716
1717 /* Handle the special case of a new common symbol merging with an
1718 old symbol that looks like it might be a common symbol defined in
1719 a shared object. Note that we have already handled the case in
1720 which a new common symbol should simply override the definition
1721 in the shared library. */
1722
1723 if (! newdyn
1724 && bfd_is_com_section (sec)
1725 && olddyncommon)
1726 {
1727 /* It would be best if we could set the hash table entry to a
1728 common symbol, but we don't know what to use for the section
1729 or the alignment. */
1a72702b
AM
1730 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1731 bfd_link_hash_common, sym->st_size);
45d6a902 1732
4cc11e76 1733 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1734 larger, pretend that the new symbol has its size. */
1735
1736 if (h->size > *pvalue)
1737 *pvalue = h->size;
1738
af44c138
L
1739 /* We need to remember the alignment required by the symbol
1740 in the dynamic object. */
1741 BFD_ASSERT (pold_alignment);
1742 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1743
1744 olddef = FALSE;
1745 olddyncommon = FALSE;
1746
1747 h->root.type = bfd_link_hash_undefined;
1748 h->root.u.undef.abfd = h->root.u.def.section->owner;
1749
1750 *size_change_ok = TRUE;
1751 *type_change_ok = TRUE;
1752
6c9b78e6
AM
1753 if (hi->root.type == bfd_link_hash_indirect)
1754 flip = hi;
45d6a902
AM
1755 else
1756 h->verinfo.vertree = NULL;
1757 }
1758
1759 if (flip != NULL)
1760 {
1761 /* Handle the case where we had a versioned symbol in a dynamic
1762 library and now find a definition in a normal object. In this
1763 case, we make the versioned symbol point to the normal one. */
45d6a902 1764 flip->root.type = h->root.type;
00cbee0a 1765 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1766 h->root.type = bfd_link_hash_indirect;
1767 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1768 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1769 if (h->def_dynamic)
45d6a902 1770 {
f5385ebf
AM
1771 h->def_dynamic = 0;
1772 flip->ref_dynamic = 1;
45d6a902
AM
1773 }
1774 }
1775
45d6a902
AM
1776 return TRUE;
1777}
1778
1779/* This function is called to create an indirect symbol from the
1780 default for the symbol with the default version if needed. The
4f3fedcf 1781 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1782 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1783
28caa186 1784static bfd_boolean
268b6b39
AM
1785_bfd_elf_add_default_symbol (bfd *abfd,
1786 struct bfd_link_info *info,
1787 struct elf_link_hash_entry *h,
1788 const char *name,
1789 Elf_Internal_Sym *sym,
4f3fedcf
AM
1790 asection *sec,
1791 bfd_vma value,
1792 bfd **poldbfd,
e3c9d234 1793 bfd_boolean *dynsym)
45d6a902
AM
1794{
1795 bfd_boolean type_change_ok;
1796 bfd_boolean size_change_ok;
1797 bfd_boolean skip;
1798 char *shortname;
1799 struct elf_link_hash_entry *hi;
1800 struct bfd_link_hash_entry *bh;
9c5bfbb7 1801 const struct elf_backend_data *bed;
45d6a902
AM
1802 bfd_boolean collect;
1803 bfd_boolean dynamic;
e3c9d234 1804 bfd_boolean override;
45d6a902
AM
1805 char *p;
1806 size_t len, shortlen;
ffd65175 1807 asection *tmp_sec;
6e33951e 1808 bfd_boolean matched;
45d6a902 1809
422f1182
L
1810 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1811 return TRUE;
1812
45d6a902
AM
1813 /* If this symbol has a version, and it is the default version, we
1814 create an indirect symbol from the default name to the fully
1815 decorated name. This will cause external references which do not
1816 specify a version to be bound to this version of the symbol. */
1817 p = strchr (name, ELF_VER_CHR);
422f1182
L
1818 if (h->versioned == unknown)
1819 {
1820 if (p == NULL)
1821 {
1822 h->versioned = unversioned;
1823 return TRUE;
1824 }
1825 else
1826 {
1827 if (p[1] != ELF_VER_CHR)
1828 {
1829 h->versioned = versioned_hidden;
1830 return TRUE;
1831 }
1832 else
1833 h->versioned = versioned;
1834 }
1835 }
4373f8af
L
1836 else
1837 {
1838 /* PR ld/19073: We may see an unversioned definition after the
1839 default version. */
1840 if (p == NULL)
1841 return TRUE;
1842 }
45d6a902 1843
45d6a902
AM
1844 bed = get_elf_backend_data (abfd);
1845 collect = bed->collect;
1846 dynamic = (abfd->flags & DYNAMIC) != 0;
1847
1848 shortlen = p - name;
a50b1753 1849 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1850 if (shortname == NULL)
1851 return FALSE;
1852 memcpy (shortname, name, shortlen);
1853 shortname[shortlen] = '\0';
1854
1855 /* We are going to create a new symbol. Merge it with any existing
1856 symbol with this name. For the purposes of the merge, act as
1857 though we were defining the symbol we just defined, although we
1858 actually going to define an indirect symbol. */
1859 type_change_ok = FALSE;
1860 size_change_ok = FALSE;
6e33951e 1861 matched = TRUE;
ffd65175
AM
1862 tmp_sec = sec;
1863 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1864 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1865 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1866 return FALSE;
1867
1868 if (skip)
1869 goto nondefault;
1870
5b677558
AM
1871 if (hi->def_regular)
1872 {
1873 /* If the undecorated symbol will have a version added by a
1874 script different to H, then don't indirect to/from the
1875 undecorated symbol. This isn't ideal because we may not yet
1876 have seen symbol versions, if given by a script on the
1877 command line rather than via --version-script. */
1878 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1879 {
1880 bfd_boolean hide;
1881
1882 hi->verinfo.vertree
1883 = bfd_find_version_for_sym (info->version_info,
1884 hi->root.root.string, &hide);
1885 if (hi->verinfo.vertree != NULL && hide)
1886 {
1887 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1888 goto nondefault;
1889 }
1890 }
1891 if (hi->verinfo.vertree != NULL
1892 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1893 goto nondefault;
1894 }
1895
45d6a902
AM
1896 if (! override)
1897 {
c6e8a9a8 1898 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1899 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1900 {
1901 bh = &hi->root;
1902 if (! (_bfd_generic_link_add_one_symbol
1903 (info, abfd, shortname, BSF_INDIRECT,
1904 bfd_ind_section_ptr,
1905 0, name, FALSE, collect, &bh)))
1906 return FALSE;
1907 hi = (struct elf_link_hash_entry *) bh;
1908 }
45d6a902
AM
1909 }
1910 else
1911 {
1912 /* In this case the symbol named SHORTNAME is overriding the
1913 indirect symbol we want to add. We were planning on making
1914 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1915 is the name without a version. NAME is the fully versioned
1916 name, and it is the default version.
1917
1918 Overriding means that we already saw a definition for the
1919 symbol SHORTNAME in a regular object, and it is overriding
1920 the symbol defined in the dynamic object.
1921
1922 When this happens, we actually want to change NAME, the
1923 symbol we just added, to refer to SHORTNAME. This will cause
1924 references to NAME in the shared object to become references
1925 to SHORTNAME in the regular object. This is what we expect
1926 when we override a function in a shared object: that the
1927 references in the shared object will be mapped to the
1928 definition in the regular object. */
1929
1930 while (hi->root.type == bfd_link_hash_indirect
1931 || hi->root.type == bfd_link_hash_warning)
1932 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1933
1934 h->root.type = bfd_link_hash_indirect;
1935 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1936 if (h->def_dynamic)
45d6a902 1937 {
f5385ebf
AM
1938 h->def_dynamic = 0;
1939 hi->ref_dynamic = 1;
1940 if (hi->ref_regular
1941 || hi->def_regular)
45d6a902 1942 {
c152c796 1943 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1944 return FALSE;
1945 }
1946 }
1947
1948 /* Now set HI to H, so that the following code will set the
1949 other fields correctly. */
1950 hi = h;
1951 }
1952
fab4a87f
L
1953 /* Check if HI is a warning symbol. */
1954 if (hi->root.type == bfd_link_hash_warning)
1955 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1956
45d6a902
AM
1957 /* If there is a duplicate definition somewhere, then HI may not
1958 point to an indirect symbol. We will have reported an error to
1959 the user in that case. */
1960
1961 if (hi->root.type == bfd_link_hash_indirect)
1962 {
1963 struct elf_link_hash_entry *ht;
1964
45d6a902 1965 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1966 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1967
68c88cd4
AM
1968 /* A reference to the SHORTNAME symbol from a dynamic library
1969 will be satisfied by the versioned symbol at runtime. In
1970 effect, we have a reference to the versioned symbol. */
1971 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1972 hi->dynamic_def |= ht->dynamic_def;
1973
45d6a902
AM
1974 /* See if the new flags lead us to realize that the symbol must
1975 be dynamic. */
1976 if (! *dynsym)
1977 {
1978 if (! dynamic)
1979 {
0e1862bb 1980 if (! bfd_link_executable (info)
90c984fc 1981 || hi->def_dynamic
f5385ebf 1982 || hi->ref_dynamic)
45d6a902
AM
1983 *dynsym = TRUE;
1984 }
1985 else
1986 {
f5385ebf 1987 if (hi->ref_regular)
45d6a902
AM
1988 *dynsym = TRUE;
1989 }
1990 }
1991 }
1992
1993 /* We also need to define an indirection from the nondefault version
1994 of the symbol. */
1995
1996nondefault:
1997 len = strlen (name);
a50b1753 1998 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1999 if (shortname == NULL)
2000 return FALSE;
2001 memcpy (shortname, name, shortlen);
2002 memcpy (shortname + shortlen, p + 1, len - shortlen);
2003
2004 /* Once again, merge with any existing symbol. */
2005 type_change_ok = FALSE;
2006 size_change_ok = FALSE;
ffd65175
AM
2007 tmp_sec = sec;
2008 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2009 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2010 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2011 return FALSE;
2012
2013 if (skip)
2014 return TRUE;
2015
2016 if (override)
2017 {
2018 /* Here SHORTNAME is a versioned name, so we don't expect to see
2019 the type of override we do in the case above unless it is
4cc11e76 2020 overridden by a versioned definition. */
45d6a902
AM
2021 if (hi->root.type != bfd_link_hash_defined
2022 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2023 _bfd_error_handler
695344c0 2024 /* xgettext:c-format */
d003868e
AM
2025 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2026 abfd, shortname);
45d6a902
AM
2027 }
2028 else
2029 {
2030 bh = &hi->root;
2031 if (! (_bfd_generic_link_add_one_symbol
2032 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2033 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2034 return FALSE;
2035 hi = (struct elf_link_hash_entry *) bh;
2036
2037 /* If there is a duplicate definition somewhere, then HI may not
2038 point to an indirect symbol. We will have reported an error
2039 to the user in that case. */
2040
2041 if (hi->root.type == bfd_link_hash_indirect)
2042 {
fcfa13d2 2043 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2044 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2045 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2046
2047 /* See if the new flags lead us to realize that the symbol
2048 must be dynamic. */
2049 if (! *dynsym)
2050 {
2051 if (! dynamic)
2052 {
0e1862bb 2053 if (! bfd_link_executable (info)
f5385ebf 2054 || hi->ref_dynamic)
45d6a902
AM
2055 *dynsym = TRUE;
2056 }
2057 else
2058 {
f5385ebf 2059 if (hi->ref_regular)
45d6a902
AM
2060 *dynsym = TRUE;
2061 }
2062 }
2063 }
2064 }
2065
2066 return TRUE;
2067}
2068\f
2069/* This routine is used to export all defined symbols into the dynamic
2070 symbol table. It is called via elf_link_hash_traverse. */
2071
28caa186 2072static bfd_boolean
268b6b39 2073_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2074{
a50b1753 2075 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2076
2077 /* Ignore indirect symbols. These are added by the versioning code. */
2078 if (h->root.type == bfd_link_hash_indirect)
2079 return TRUE;
2080
7686d77d
AM
2081 /* Ignore this if we won't export it. */
2082 if (!eif->info->export_dynamic && !h->dynamic)
2083 return TRUE;
45d6a902
AM
2084
2085 if (h->dynindx == -1
fd91d419
L
2086 && (h->def_regular || h->ref_regular)
2087 && ! bfd_hide_sym_by_version (eif->info->version_info,
2088 h->root.root.string))
45d6a902 2089 {
fd91d419 2090 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2091 {
fd91d419
L
2092 eif->failed = TRUE;
2093 return FALSE;
45d6a902
AM
2094 }
2095 }
2096
2097 return TRUE;
2098}
2099\f
2100/* Look through the symbols which are defined in other shared
2101 libraries and referenced here. Update the list of version
2102 dependencies. This will be put into the .gnu.version_r section.
2103 This function is called via elf_link_hash_traverse. */
2104
28caa186 2105static bfd_boolean
268b6b39
AM
2106_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2107 void *data)
45d6a902 2108{
a50b1753 2109 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2110 Elf_Internal_Verneed *t;
2111 Elf_Internal_Vernaux *a;
2112 bfd_size_type amt;
2113
45d6a902
AM
2114 /* We only care about symbols defined in shared objects with version
2115 information. */
f5385ebf
AM
2116 if (!h->def_dynamic
2117 || h->def_regular
45d6a902 2118 || h->dynindx == -1
7b20f099
AM
2119 || h->verinfo.verdef == NULL
2120 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2121 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2122 return TRUE;
2123
2124 /* See if we already know about this version. */
28caa186
AM
2125 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2126 t != NULL;
2127 t = t->vn_nextref)
45d6a902
AM
2128 {
2129 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2130 continue;
2131
2132 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2133 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2134 return TRUE;
2135
2136 break;
2137 }
2138
2139 /* This is a new version. Add it to tree we are building. */
2140
2141 if (t == NULL)
2142 {
2143 amt = sizeof *t;
a50b1753 2144 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2145 if (t == NULL)
2146 {
2147 rinfo->failed = TRUE;
2148 return FALSE;
2149 }
2150
2151 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2152 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2153 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2154 }
2155
2156 amt = sizeof *a;
a50b1753 2157 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2158 if (a == NULL)
2159 {
2160 rinfo->failed = TRUE;
2161 return FALSE;
2162 }
45d6a902
AM
2163
2164 /* Note that we are copying a string pointer here, and testing it
2165 above. If bfd_elf_string_from_elf_section is ever changed to
2166 discard the string data when low in memory, this will have to be
2167 fixed. */
2168 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2169
2170 a->vna_flags = h->verinfo.verdef->vd_flags;
2171 a->vna_nextptr = t->vn_auxptr;
2172
2173 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2174 ++rinfo->vers;
2175
2176 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2177
2178 t->vn_auxptr = a;
2179
2180 return TRUE;
2181}
2182
2183/* Figure out appropriate versions for all the symbols. We may not
2184 have the version number script until we have read all of the input
2185 files, so until that point we don't know which symbols should be
2186 local. This function is called via elf_link_hash_traverse. */
2187
28caa186 2188static bfd_boolean
268b6b39 2189_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2190{
28caa186 2191 struct elf_info_failed *sinfo;
45d6a902 2192 struct bfd_link_info *info;
9c5bfbb7 2193 const struct elf_backend_data *bed;
45d6a902
AM
2194 struct elf_info_failed eif;
2195 char *p;
45d6a902 2196
a50b1753 2197 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2198 info = sinfo->info;
2199
45d6a902
AM
2200 /* Fix the symbol flags. */
2201 eif.failed = FALSE;
2202 eif.info = info;
2203 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2204 {
2205 if (eif.failed)
2206 sinfo->failed = TRUE;
2207 return FALSE;
2208 }
2209
2210 /* We only need version numbers for symbols defined in regular
2211 objects. */
f5385ebf 2212 if (!h->def_regular)
45d6a902
AM
2213 return TRUE;
2214
28caa186 2215 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2216 p = strchr (h->root.root.string, ELF_VER_CHR);
2217 if (p != NULL && h->verinfo.vertree == NULL)
2218 {
2219 struct bfd_elf_version_tree *t;
45d6a902 2220
45d6a902
AM
2221 ++p;
2222 if (*p == ELF_VER_CHR)
6e33951e 2223 ++p;
45d6a902
AM
2224
2225 /* If there is no version string, we can just return out. */
2226 if (*p == '\0')
6e33951e 2227 return TRUE;
45d6a902
AM
2228
2229 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2230 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2231 {
2232 if (strcmp (t->name, p) == 0)
2233 {
2234 size_t len;
2235 char *alc;
2236 struct bfd_elf_version_expr *d;
2237
2238 len = p - h->root.root.string;
a50b1753 2239 alc = (char *) bfd_malloc (len);
45d6a902 2240 if (alc == NULL)
14b1c01e
AM
2241 {
2242 sinfo->failed = TRUE;
2243 return FALSE;
2244 }
45d6a902
AM
2245 memcpy (alc, h->root.root.string, len - 1);
2246 alc[len - 1] = '\0';
2247 if (alc[len - 2] == ELF_VER_CHR)
2248 alc[len - 2] = '\0';
2249
2250 h->verinfo.vertree = t;
2251 t->used = TRUE;
2252 d = NULL;
2253
108ba305
JJ
2254 if (t->globals.list != NULL)
2255 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2256
2257 /* See if there is anything to force this symbol to
2258 local scope. */
108ba305 2259 if (d == NULL && t->locals.list != NULL)
45d6a902 2260 {
108ba305
JJ
2261 d = (*t->match) (&t->locals, NULL, alc);
2262 if (d != NULL
2263 && h->dynindx != -1
108ba305
JJ
2264 && ! info->export_dynamic)
2265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2266 }
2267
2268 free (alc);
2269 break;
2270 }
2271 }
2272
2273 /* If we are building an application, we need to create a
2274 version node for this version. */
0e1862bb 2275 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2276 {
2277 struct bfd_elf_version_tree **pp;
2278 int version_index;
2279
2280 /* If we aren't going to export this symbol, we don't need
2281 to worry about it. */
2282 if (h->dynindx == -1)
2283 return TRUE;
2284
ef53be89
AM
2285 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2286 sizeof *t);
45d6a902
AM
2287 if (t == NULL)
2288 {
2289 sinfo->failed = TRUE;
2290 return FALSE;
2291 }
2292
45d6a902 2293 t->name = p;
45d6a902
AM
2294 t->name_indx = (unsigned int) -1;
2295 t->used = TRUE;
2296
2297 version_index = 1;
2298 /* Don't count anonymous version tag. */
fd91d419
L
2299 if (sinfo->info->version_info != NULL
2300 && sinfo->info->version_info->vernum == 0)
45d6a902 2301 version_index = 0;
fd91d419
L
2302 for (pp = &sinfo->info->version_info;
2303 *pp != NULL;
2304 pp = &(*pp)->next)
45d6a902
AM
2305 ++version_index;
2306 t->vernum = version_index;
2307
2308 *pp = t;
2309
2310 h->verinfo.vertree = t;
2311 }
2312 else if (t == NULL)
2313 {
2314 /* We could not find the version for a symbol when
2315 generating a shared archive. Return an error. */
4eca0228 2316 _bfd_error_handler
695344c0 2317 /* xgettext:c-format */
c55fe096 2318 (_("%B: version node not found for symbol %s"),
28caa186 2319 info->output_bfd, h->root.root.string);
45d6a902
AM
2320 bfd_set_error (bfd_error_bad_value);
2321 sinfo->failed = TRUE;
2322 return FALSE;
2323 }
45d6a902
AM
2324 }
2325
2326 /* If we don't have a version for this symbol, see if we can find
2327 something. */
fd91d419 2328 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2329 {
1e8fa21e 2330 bfd_boolean hide;
ae5a3597 2331
fd91d419
L
2332 h->verinfo.vertree
2333 = bfd_find_version_for_sym (sinfo->info->version_info,
2334 h->root.root.string, &hide);
1e8fa21e
AM
2335 if (h->verinfo.vertree != NULL && hide)
2336 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2337 }
2338
2339 return TRUE;
2340}
2341\f
45d6a902
AM
2342/* Read and swap the relocs from the section indicated by SHDR. This
2343 may be either a REL or a RELA section. The relocations are
2344 translated into RELA relocations and stored in INTERNAL_RELOCS,
2345 which should have already been allocated to contain enough space.
2346 The EXTERNAL_RELOCS are a buffer where the external form of the
2347 relocations should be stored.
2348
2349 Returns FALSE if something goes wrong. */
2350
2351static bfd_boolean
268b6b39 2352elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2353 asection *sec,
268b6b39
AM
2354 Elf_Internal_Shdr *shdr,
2355 void *external_relocs,
2356 Elf_Internal_Rela *internal_relocs)
45d6a902 2357{
9c5bfbb7 2358 const struct elf_backend_data *bed;
268b6b39 2359 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2360 const bfd_byte *erela;
2361 const bfd_byte *erelaend;
2362 Elf_Internal_Rela *irela;
243ef1e0
L
2363 Elf_Internal_Shdr *symtab_hdr;
2364 size_t nsyms;
45d6a902 2365
45d6a902
AM
2366 /* Position ourselves at the start of the section. */
2367 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2368 return FALSE;
2369
2370 /* Read the relocations. */
2371 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2372 return FALSE;
2373
243ef1e0 2374 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2375 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2376
45d6a902
AM
2377 bed = get_elf_backend_data (abfd);
2378
2379 /* Convert the external relocations to the internal format. */
2380 if (shdr->sh_entsize == bed->s->sizeof_rel)
2381 swap_in = bed->s->swap_reloc_in;
2382 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2383 swap_in = bed->s->swap_reloca_in;
2384 else
2385 {
2386 bfd_set_error (bfd_error_wrong_format);
2387 return FALSE;
2388 }
2389
a50b1753 2390 erela = (const bfd_byte *) external_relocs;
51992aec 2391 erelaend = erela + shdr->sh_size;
45d6a902
AM
2392 irela = internal_relocs;
2393 while (erela < erelaend)
2394 {
243ef1e0
L
2395 bfd_vma r_symndx;
2396
45d6a902 2397 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2398 r_symndx = ELF32_R_SYM (irela->r_info);
2399 if (bed->s->arch_size == 64)
2400 r_symndx >>= 24;
ce98a316
NC
2401 if (nsyms > 0)
2402 {
2403 if ((size_t) r_symndx >= nsyms)
2404 {
4eca0228 2405 _bfd_error_handler
695344c0 2406 /* xgettext:c-format */
d42c267e 2407 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
76cfced5 2408 " for offset %#Lx in section `%A'"),
d42c267e 2409 abfd, r_symndx, (unsigned long) nsyms,
c08bb8dd 2410 irela->r_offset, sec);
ce98a316
NC
2411 bfd_set_error (bfd_error_bad_value);
2412 return FALSE;
2413 }
2414 }
cf35638d 2415 else if (r_symndx != STN_UNDEF)
243ef1e0 2416 {
4eca0228 2417 _bfd_error_handler
695344c0 2418 /* xgettext:c-format */
d42c267e 2419 (_("%B: non-zero symbol index (%#Lx)"
76cfced5 2420 " for offset %#Lx in section `%A'"
ce98a316 2421 " when the object file has no symbol table"),
d42c267e 2422 abfd, r_symndx,
c08bb8dd 2423 irela->r_offset, sec);
243ef1e0
L
2424 bfd_set_error (bfd_error_bad_value);
2425 return FALSE;
2426 }
45d6a902
AM
2427 irela += bed->s->int_rels_per_ext_rel;
2428 erela += shdr->sh_entsize;
2429 }
2430
2431 return TRUE;
2432}
2433
2434/* Read and swap the relocs for a section O. They may have been
2435 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2436 not NULL, they are used as buffers to read into. They are known to
2437 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2438 the return value is allocated using either malloc or bfd_alloc,
2439 according to the KEEP_MEMORY argument. If O has two relocation
2440 sections (both REL and RELA relocations), then the REL_HDR
2441 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2442 RELA_HDR relocations. */
45d6a902
AM
2443
2444Elf_Internal_Rela *
268b6b39
AM
2445_bfd_elf_link_read_relocs (bfd *abfd,
2446 asection *o,
2447 void *external_relocs,
2448 Elf_Internal_Rela *internal_relocs,
2449 bfd_boolean keep_memory)
45d6a902 2450{
268b6b39 2451 void *alloc1 = NULL;
45d6a902 2452 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2453 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2454 struct bfd_elf_section_data *esdo = elf_section_data (o);
2455 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2456
d4730f92
BS
2457 if (esdo->relocs != NULL)
2458 return esdo->relocs;
45d6a902
AM
2459
2460 if (o->reloc_count == 0)
2461 return NULL;
2462
45d6a902
AM
2463 if (internal_relocs == NULL)
2464 {
2465 bfd_size_type size;
2466
056bafd4 2467 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2468 if (keep_memory)
a50b1753 2469 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2470 else
a50b1753 2471 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2472 if (internal_relocs == NULL)
2473 goto error_return;
2474 }
2475
2476 if (external_relocs == NULL)
2477 {
d4730f92
BS
2478 bfd_size_type size = 0;
2479
2480 if (esdo->rel.hdr)
2481 size += esdo->rel.hdr->sh_size;
2482 if (esdo->rela.hdr)
2483 size += esdo->rela.hdr->sh_size;
45d6a902 2484
268b6b39 2485 alloc1 = bfd_malloc (size);
45d6a902
AM
2486 if (alloc1 == NULL)
2487 goto error_return;
2488 external_relocs = alloc1;
2489 }
2490
d4730f92
BS
2491 internal_rela_relocs = internal_relocs;
2492 if (esdo->rel.hdr)
2493 {
2494 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2495 external_relocs,
2496 internal_relocs))
2497 goto error_return;
2498 external_relocs = (((bfd_byte *) external_relocs)
2499 + esdo->rel.hdr->sh_size);
2500 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2501 * bed->s->int_rels_per_ext_rel);
2502 }
2503
2504 if (esdo->rela.hdr
2505 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2506 external_relocs,
2507 internal_rela_relocs)))
45d6a902
AM
2508 goto error_return;
2509
2510 /* Cache the results for next time, if we can. */
2511 if (keep_memory)
d4730f92 2512 esdo->relocs = internal_relocs;
45d6a902
AM
2513
2514 if (alloc1 != NULL)
2515 free (alloc1);
2516
2517 /* Don't free alloc2, since if it was allocated we are passing it
2518 back (under the name of internal_relocs). */
2519
2520 return internal_relocs;
2521
2522 error_return:
2523 if (alloc1 != NULL)
2524 free (alloc1);
2525 if (alloc2 != NULL)
4dd07732
AM
2526 {
2527 if (keep_memory)
2528 bfd_release (abfd, alloc2);
2529 else
2530 free (alloc2);
2531 }
45d6a902
AM
2532 return NULL;
2533}
2534
2535/* Compute the size of, and allocate space for, REL_HDR which is the
2536 section header for a section containing relocations for O. */
2537
28caa186 2538static bfd_boolean
9eaff861
AO
2539_bfd_elf_link_size_reloc_section (bfd *abfd,
2540 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2541{
9eaff861 2542 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2543
2544 /* That allows us to calculate the size of the section. */
9eaff861 2545 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2546
2547 /* The contents field must last into write_object_contents, so we
2548 allocate it with bfd_alloc rather than malloc. Also since we
2549 cannot be sure that the contents will actually be filled in,
2550 we zero the allocated space. */
a50b1753 2551 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2552 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2553 return FALSE;
2554
d4730f92 2555 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2556 {
2557 struct elf_link_hash_entry **p;
2558
ca4be51c
AM
2559 p = ((struct elf_link_hash_entry **)
2560 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2561 if (p == NULL)
2562 return FALSE;
2563
d4730f92 2564 reldata->hashes = p;
45d6a902
AM
2565 }
2566
2567 return TRUE;
2568}
2569
2570/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2571 originated from the section given by INPUT_REL_HDR) to the
2572 OUTPUT_BFD. */
2573
2574bfd_boolean
268b6b39
AM
2575_bfd_elf_link_output_relocs (bfd *output_bfd,
2576 asection *input_section,
2577 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2578 Elf_Internal_Rela *internal_relocs,
2579 struct elf_link_hash_entry **rel_hash
2580 ATTRIBUTE_UNUSED)
45d6a902
AM
2581{
2582 Elf_Internal_Rela *irela;
2583 Elf_Internal_Rela *irelaend;
2584 bfd_byte *erel;
d4730f92 2585 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2586 asection *output_section;
9c5bfbb7 2587 const struct elf_backend_data *bed;
268b6b39 2588 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2589 struct bfd_elf_section_data *esdo;
45d6a902
AM
2590
2591 output_section = input_section->output_section;
45d6a902 2592
d4730f92
BS
2593 bed = get_elf_backend_data (output_bfd);
2594 esdo = elf_section_data (output_section);
2595 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2596 {
d4730f92
BS
2597 output_reldata = &esdo->rel;
2598 swap_out = bed->s->swap_reloc_out;
45d6a902 2599 }
d4730f92
BS
2600 else if (esdo->rela.hdr
2601 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2602 {
d4730f92
BS
2603 output_reldata = &esdo->rela;
2604 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2605 }
2606 else
2607 {
4eca0228 2608 _bfd_error_handler
695344c0 2609 /* xgettext:c-format */
d003868e
AM
2610 (_("%B: relocation size mismatch in %B section %A"),
2611 output_bfd, input_section->owner, input_section);
297d8443 2612 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2613 return FALSE;
2614 }
2615
d4730f92
BS
2616 erel = output_reldata->hdr->contents;
2617 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2618 irela = internal_relocs;
2619 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2620 * bed->s->int_rels_per_ext_rel);
2621 while (irela < irelaend)
2622 {
2623 (*swap_out) (output_bfd, irela, erel);
2624 irela += bed->s->int_rels_per_ext_rel;
2625 erel += input_rel_hdr->sh_entsize;
2626 }
2627
2628 /* Bump the counter, so that we know where to add the next set of
2629 relocations. */
d4730f92 2630 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2631
2632 return TRUE;
2633}
2634\f
508c3946
L
2635/* Make weak undefined symbols in PIE dynamic. */
2636
2637bfd_boolean
2638_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2639 struct elf_link_hash_entry *h)
2640{
0e1862bb 2641 if (bfd_link_pie (info)
508c3946
L
2642 && h->dynindx == -1
2643 && h->root.type == bfd_link_hash_undefweak)
2644 return bfd_elf_link_record_dynamic_symbol (info, h);
2645
2646 return TRUE;
2647}
2648
45d6a902
AM
2649/* Fix up the flags for a symbol. This handles various cases which
2650 can only be fixed after all the input files are seen. This is
2651 currently called by both adjust_dynamic_symbol and
2652 assign_sym_version, which is unnecessary but perhaps more robust in
2653 the face of future changes. */
2654
28caa186 2655static bfd_boolean
268b6b39
AM
2656_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2657 struct elf_info_failed *eif)
45d6a902 2658{
33774f08 2659 const struct elf_backend_data *bed;
508c3946 2660
45d6a902
AM
2661 /* If this symbol was mentioned in a non-ELF file, try to set
2662 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2663 permit a non-ELF file to correctly refer to a symbol defined in
2664 an ELF dynamic object. */
f5385ebf 2665 if (h->non_elf)
45d6a902
AM
2666 {
2667 while (h->root.type == bfd_link_hash_indirect)
2668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2669
2670 if (h->root.type != bfd_link_hash_defined
2671 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2672 {
2673 h->ref_regular = 1;
2674 h->ref_regular_nonweak = 1;
2675 }
45d6a902
AM
2676 else
2677 {
2678 if (h->root.u.def.section->owner != NULL
2679 && (bfd_get_flavour (h->root.u.def.section->owner)
2680 == bfd_target_elf_flavour))
f5385ebf
AM
2681 {
2682 h->ref_regular = 1;
2683 h->ref_regular_nonweak = 1;
2684 }
45d6a902 2685 else
f5385ebf 2686 h->def_regular = 1;
45d6a902
AM
2687 }
2688
2689 if (h->dynindx == -1
f5385ebf
AM
2690 && (h->def_dynamic
2691 || h->ref_dynamic))
45d6a902 2692 {
c152c796 2693 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2694 {
2695 eif->failed = TRUE;
2696 return FALSE;
2697 }
2698 }
2699 }
2700 else
2701 {
f5385ebf 2702 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2703 was first seen in a non-ELF file. Fortunately, if the symbol
2704 was first seen in an ELF file, we're probably OK unless the
2705 symbol was defined in a non-ELF file. Catch that case here.
2706 FIXME: We're still in trouble if the symbol was first seen in
2707 a dynamic object, and then later in a non-ELF regular object. */
2708 if ((h->root.type == bfd_link_hash_defined
2709 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2710 && !h->def_regular
45d6a902
AM
2711 && (h->root.u.def.section->owner != NULL
2712 ? (bfd_get_flavour (h->root.u.def.section->owner)
2713 != bfd_target_elf_flavour)
2714 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2715 && !h->def_dynamic)))
2716 h->def_regular = 1;
45d6a902
AM
2717 }
2718
508c3946 2719 /* Backend specific symbol fixup. */
33774f08
AM
2720 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2721 if (bed->elf_backend_fixup_symbol
2722 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2723 return FALSE;
508c3946 2724
45d6a902
AM
2725 /* If this is a final link, and the symbol was defined as a common
2726 symbol in a regular object file, and there was no definition in
2727 any dynamic object, then the linker will have allocated space for
f5385ebf 2728 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2729 flag will not have been set. */
2730 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2731 && !h->def_regular
2732 && h->ref_regular
2733 && !h->def_dynamic
96f29d96 2734 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2735 h->def_regular = 1;
45d6a902 2736
4deb8f71
L
2737 /* If a weak undefined symbol has non-default visibility, we also
2738 hide it from the dynamic linker. */
2739 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2740 && h->root.type == bfd_link_hash_undefweak)
2741 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2742
2743 /* A hidden versioned symbol in executable should be forced local if
2744 it is is locally defined, not referenced by shared library and not
2745 exported. */
2746 else if (bfd_link_executable (eif->info)
2747 && h->versioned == versioned_hidden
2748 && !eif->info->export_dynamic
2749 && !h->dynamic
2750 && !h->ref_dynamic
2751 && h->def_regular)
2752 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2753
45d6a902
AM
2754 /* If -Bsymbolic was used (which means to bind references to global
2755 symbols to the definition within the shared object), and this
2756 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2757 need a PLT entry. Likewise, if the symbol has non-default
2758 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2759 will force it local. */
4deb8f71
L
2760 else if (h->needs_plt
2761 && bfd_link_pic (eif->info)
2762 && is_elf_hash_table (eif->info->hash)
2763 && (SYMBOLIC_BIND (eif->info, h)
2764 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2765 && h->def_regular)
45d6a902 2766 {
45d6a902
AM
2767 bfd_boolean force_local;
2768
45d6a902
AM
2769 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2770 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2771 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2772 }
2773
45d6a902
AM
2774 /* If this is a weak defined symbol in a dynamic object, and we know
2775 the real definition in the dynamic object, copy interesting flags
2776 over to the real definition. */
f6e332e6 2777 if (h->u.weakdef != NULL)
45d6a902 2778 {
45d6a902
AM
2779 /* If the real definition is defined by a regular object file,
2780 don't do anything special. See the longer description in
2781 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2782 if (h->u.weakdef->def_regular)
f6e332e6 2783 h->u.weakdef = NULL;
45d6a902 2784 else
a26587ba 2785 {
4e6b54a6
AM
2786 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2787
2788 while (h->root.type == bfd_link_hash_indirect)
2789 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2790
2791 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2792 || h->root.type == bfd_link_hash_defweak);
2793 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2794 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2795 || weakdef->root.type == bfd_link_hash_defweak);
2796 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2797 }
45d6a902
AM
2798 }
2799
2800 return TRUE;
2801}
2802
2803/* Make the backend pick a good value for a dynamic symbol. This is
2804 called via elf_link_hash_traverse, and also calls itself
2805 recursively. */
2806
28caa186 2807static bfd_boolean
268b6b39 2808_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2809{
a50b1753 2810 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2811 struct elf_link_hash_table *htab;
9c5bfbb7 2812 const struct elf_backend_data *bed;
45d6a902 2813
0eddce27 2814 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2815 return FALSE;
2816
45d6a902
AM
2817 /* Ignore indirect symbols. These are added by the versioning code. */
2818 if (h->root.type == bfd_link_hash_indirect)
2819 return TRUE;
2820
2821 /* Fix the symbol flags. */
2822 if (! _bfd_elf_fix_symbol_flags (h, eif))
2823 return FALSE;
2824
559192d8
AM
2825 htab = elf_hash_table (eif->info);
2826 bed = get_elf_backend_data (htab->dynobj);
2827
954b63d4
AM
2828 if (h->root.type == bfd_link_hash_undefweak)
2829 {
2830 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2831 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2832 else if (eif->info->dynamic_undefined_weak > 0
2833 && h->ref_regular
2834 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2835 && !bfd_hide_sym_by_version (eif->info->version_info,
2836 h->root.root.string))
2837 {
2838 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2839 {
2840 eif->failed = TRUE;
2841 return FALSE;
2842 }
2843 }
2844 }
2845
45d6a902
AM
2846 /* If this symbol does not require a PLT entry, and it is not
2847 defined by a dynamic object, or is not referenced by a regular
2848 object, ignore it. We do have to handle a weak defined symbol,
2849 even if no regular object refers to it, if we decided to add it
2850 to the dynamic symbol table. FIXME: Do we normally need to worry
2851 about symbols which are defined by one dynamic object and
2852 referenced by another one? */
f5385ebf 2853 if (!h->needs_plt
91e21fb7 2854 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2855 && (h->def_regular
2856 || !h->def_dynamic
2857 || (!h->ref_regular
f6e332e6 2858 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2859 {
a6aa5195 2860 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2861 return TRUE;
2862 }
2863
2864 /* If we've already adjusted this symbol, don't do it again. This
2865 can happen via a recursive call. */
f5385ebf 2866 if (h->dynamic_adjusted)
45d6a902
AM
2867 return TRUE;
2868
2869 /* Don't look at this symbol again. Note that we must set this
2870 after checking the above conditions, because we may look at a
2871 symbol once, decide not to do anything, and then get called
2872 recursively later after REF_REGULAR is set below. */
f5385ebf 2873 h->dynamic_adjusted = 1;
45d6a902
AM
2874
2875 /* If this is a weak definition, and we know a real definition, and
2876 the real symbol is not itself defined by a regular object file,
2877 then get a good value for the real definition. We handle the
2878 real symbol first, for the convenience of the backend routine.
2879
2880 Note that there is a confusing case here. If the real definition
2881 is defined by a regular object file, we don't get the real symbol
2882 from the dynamic object, but we do get the weak symbol. If the
2883 processor backend uses a COPY reloc, then if some routine in the
2884 dynamic object changes the real symbol, we will not see that
2885 change in the corresponding weak symbol. This is the way other
2886 ELF linkers work as well, and seems to be a result of the shared
2887 library model.
2888
2889 I will clarify this issue. Most SVR4 shared libraries define the
2890 variable _timezone and define timezone as a weak synonym. The
2891 tzset call changes _timezone. If you write
2892 extern int timezone;
2893 int _timezone = 5;
2894 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2895 you might expect that, since timezone is a synonym for _timezone,
2896 the same number will print both times. However, if the processor
2897 backend uses a COPY reloc, then actually timezone will be copied
2898 into your process image, and, since you define _timezone
2899 yourself, _timezone will not. Thus timezone and _timezone will
2900 wind up at different memory locations. The tzset call will set
2901 _timezone, leaving timezone unchanged. */
2902
f6e332e6 2903 if (h->u.weakdef != NULL)
45d6a902 2904 {
ec24dc88
AM
2905 /* If we get to this point, there is an implicit reference to
2906 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2907 h->u.weakdef->ref_regular = 1;
45d6a902 2908
ec24dc88
AM
2909 /* Ensure that the backend adjust_dynamic_symbol function sees
2910 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2911 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2912 return FALSE;
2913 }
2914
2915 /* If a symbol has no type and no size and does not require a PLT
2916 entry, then we are probably about to do the wrong thing here: we
2917 are probably going to create a COPY reloc for an empty object.
2918 This case can arise when a shared object is built with assembly
2919 code, and the assembly code fails to set the symbol type. */
2920 if (h->size == 0
2921 && h->type == STT_NOTYPE
f5385ebf 2922 && !h->needs_plt)
4eca0228 2923 _bfd_error_handler
45d6a902
AM
2924 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2925 h->root.root.string);
2926
45d6a902
AM
2927 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2928 {
2929 eif->failed = TRUE;
2930 return FALSE;
2931 }
2932
2933 return TRUE;
2934}
2935
027297b7
L
2936/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2937 DYNBSS. */
2938
2939bfd_boolean
6cabe1ea
AM
2940_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2941 struct elf_link_hash_entry *h,
027297b7
L
2942 asection *dynbss)
2943{
91ac5911 2944 unsigned int power_of_two;
027297b7
L
2945 bfd_vma mask;
2946 asection *sec = h->root.u.def.section;
2947
de194d85 2948 /* The section alignment of the definition is the maximum alignment
91ac5911
L
2949 requirement of symbols defined in the section. Since we don't
2950 know the symbol alignment requirement, we start with the
2951 maximum alignment and check low bits of the symbol address
2952 for the minimum alignment. */
2953 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2954 mask = ((bfd_vma) 1 << power_of_two) - 1;
2955 while ((h->root.u.def.value & mask) != 0)
2956 {
2957 mask >>= 1;
2958 --power_of_two;
2959 }
027297b7 2960
91ac5911
L
2961 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2962 dynbss))
027297b7
L
2963 {
2964 /* Adjust the section alignment if needed. */
2965 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2966 power_of_two))
027297b7
L
2967 return FALSE;
2968 }
2969
91ac5911 2970 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2971 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2972
2973 /* Define the symbol as being at this point in DYNBSS. */
2974 h->root.u.def.section = dynbss;
2975 h->root.u.def.value = dynbss->size;
2976
2977 /* Increment the size of DYNBSS to make room for the symbol. */
2978 dynbss->size += h->size;
2979
f7483970
L
2980 /* No error if extern_protected_data is true. */
2981 if (h->protected_def
889c2a67
L
2982 && (!info->extern_protected_data
2983 || (info->extern_protected_data < 0
2984 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2985 info->callbacks->einfo
2986 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2987 h->root.root.string);
6cabe1ea 2988
027297b7
L
2989 return TRUE;
2990}
2991
45d6a902
AM
2992/* Adjust all external symbols pointing into SEC_MERGE sections
2993 to reflect the object merging within the sections. */
2994
28caa186 2995static bfd_boolean
268b6b39 2996_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2997{
2998 asection *sec;
2999
45d6a902
AM
3000 if ((h->root.type == bfd_link_hash_defined
3001 || h->root.type == bfd_link_hash_defweak)
3002 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3003 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3004 {
a50b1753 3005 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3006
3007 h->root.u.def.value =
3008 _bfd_merged_section_offset (output_bfd,
3009 &h->root.u.def.section,
3010 elf_section_data (sec)->sec_info,
753731ee 3011 h->root.u.def.value);
45d6a902
AM
3012 }
3013
3014 return TRUE;
3015}
986a241f
RH
3016
3017/* Returns false if the symbol referred to by H should be considered
3018 to resolve local to the current module, and true if it should be
3019 considered to bind dynamically. */
3020
3021bfd_boolean
268b6b39
AM
3022_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3023 struct bfd_link_info *info,
89a2ee5a 3024 bfd_boolean not_local_protected)
986a241f
RH
3025{
3026 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3027 const struct elf_backend_data *bed;
3028 struct elf_link_hash_table *hash_table;
986a241f
RH
3029
3030 if (h == NULL)
3031 return FALSE;
3032
3033 while (h->root.type == bfd_link_hash_indirect
3034 || h->root.type == bfd_link_hash_warning)
3035 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3036
3037 /* If it was forced local, then clearly it's not dynamic. */
3038 if (h->dynindx == -1)
3039 return FALSE;
f5385ebf 3040 if (h->forced_local)
986a241f
RH
3041 return FALSE;
3042
3043 /* Identify the cases where name binding rules say that a
3044 visible symbol resolves locally. */
0e1862bb
L
3045 binding_stays_local_p = (bfd_link_executable (info)
3046 || SYMBOLIC_BIND (info, h));
986a241f
RH
3047
3048 switch (ELF_ST_VISIBILITY (h->other))
3049 {
3050 case STV_INTERNAL:
3051 case STV_HIDDEN:
3052 return FALSE;
3053
3054 case STV_PROTECTED:
fcb93ecf
PB
3055 hash_table = elf_hash_table (info);
3056 if (!is_elf_hash_table (hash_table))
3057 return FALSE;
3058
3059 bed = get_elf_backend_data (hash_table->dynobj);
3060
986a241f
RH
3061 /* Proper resolution for function pointer equality may require
3062 that these symbols perhaps be resolved dynamically, even though
3063 we should be resolving them to the current module. */
89a2ee5a 3064 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3065 binding_stays_local_p = TRUE;
3066 break;
3067
3068 default:
986a241f
RH
3069 break;
3070 }
3071
aa37626c 3072 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3073 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3074 return TRUE;
3075
986a241f
RH
3076 /* Otherwise, the symbol is dynamic if binding rules don't tell
3077 us that it remains local. */
3078 return !binding_stays_local_p;
3079}
f6c52c13
AM
3080
3081/* Return true if the symbol referred to by H should be considered
3082 to resolve local to the current module, and false otherwise. Differs
3083 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3084 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3085 for the place where dynindx == -1 is tested. If that test is true,
3086 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3087 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3088 defined symbols.
89a2ee5a
AM
3089 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3090 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3091 treatment of undefined weak symbols. For those that do not make
3092 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3093
3094bfd_boolean
268b6b39
AM
3095_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3096 struct bfd_link_info *info,
3097 bfd_boolean local_protected)
f6c52c13 3098{
fcb93ecf
PB
3099 const struct elf_backend_data *bed;
3100 struct elf_link_hash_table *hash_table;
3101
f6c52c13
AM
3102 /* If it's a local sym, of course we resolve locally. */
3103 if (h == NULL)
3104 return TRUE;
3105
d95edcac
L
3106 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3107 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3108 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3109 return TRUE;
3110
0fad2956
MR
3111 /* Forced local symbols resolve locally. */
3112 if (h->forced_local)
3113 return TRUE;
3114
7e2294f9
AO
3115 /* Common symbols that become definitions don't get the DEF_REGULAR
3116 flag set, so test it first, and don't bail out. */
3117 if (ELF_COMMON_DEF_P (h))
3118 /* Do nothing. */;
f6c52c13 3119 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3120 resolve locally. The sym is either undefined or dynamic. */
3121 else if (!h->def_regular)
f6c52c13
AM
3122 return FALSE;
3123
0fad2956 3124 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3125 if (h->dynindx == -1)
3126 return TRUE;
3127
3128 /* At this point, we know the symbol is defined and dynamic. In an
3129 executable it must resolve locally, likewise when building symbolic
3130 shared libraries. */
0e1862bb 3131 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3132 return TRUE;
3133
3134 /* Now deal with defined dynamic symbols in shared libraries. Ones
3135 with default visibility might not resolve locally. */
3136 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3137 return FALSE;
3138
fcb93ecf
PB
3139 hash_table = elf_hash_table (info);
3140 if (!is_elf_hash_table (hash_table))
3141 return TRUE;
3142
3143 bed = get_elf_backend_data (hash_table->dynobj);
3144
f7483970
L
3145 /* If extern_protected_data is false, STV_PROTECTED non-function
3146 symbols are local. */
889c2a67
L
3147 if ((!info->extern_protected_data
3148 || (info->extern_protected_data < 0
3149 && !bed->extern_protected_data))
3150 && !bed->is_function_type (h->type))
1c16dfa5
L
3151 return TRUE;
3152
f6c52c13 3153 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3154 symbols be treated as dynamic symbols. If the address of a
3155 function not defined in an executable is set to that function's
3156 plt entry in the executable, then the address of the function in
3157 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3158 return local_protected;
3159}
e1918d23
AM
3160
3161/* Caches some TLS segment info, and ensures that the TLS segment vma is
3162 aligned. Returns the first TLS output section. */
3163
3164struct bfd_section *
3165_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3166{
3167 struct bfd_section *sec, *tls;
3168 unsigned int align = 0;
3169
3170 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3171 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3172 break;
3173 tls = sec;
3174
3175 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3176 if (sec->alignment_power > align)
3177 align = sec->alignment_power;
3178
3179 elf_hash_table (info)->tls_sec = tls;
3180
3181 /* Ensure the alignment of the first section is the largest alignment,
3182 so that the tls segment starts aligned. */
3183 if (tls != NULL)
3184 tls->alignment_power = align;
3185
3186 return tls;
3187}
0ad989f9
L
3188
3189/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3190static bfd_boolean
3191is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3192 Elf_Internal_Sym *sym)
3193{
a4d8e49b
L
3194 const struct elf_backend_data *bed;
3195
0ad989f9
L
3196 /* Local symbols do not count, but target specific ones might. */
3197 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3198 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3199 return FALSE;
3200
fcb93ecf 3201 bed = get_elf_backend_data (abfd);
0ad989f9 3202 /* Function symbols do not count. */
fcb93ecf 3203 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3204 return FALSE;
3205
3206 /* If the section is undefined, then so is the symbol. */
3207 if (sym->st_shndx == SHN_UNDEF)
3208 return FALSE;
3209
3210 /* If the symbol is defined in the common section, then
3211 it is a common definition and so does not count. */
a4d8e49b 3212 if (bed->common_definition (sym))
0ad989f9
L
3213 return FALSE;
3214
3215 /* If the symbol is in a target specific section then we
3216 must rely upon the backend to tell us what it is. */
3217 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3218 /* FIXME - this function is not coded yet:
3219
3220 return _bfd_is_global_symbol_definition (abfd, sym);
3221
3222 Instead for now assume that the definition is not global,
3223 Even if this is wrong, at least the linker will behave
3224 in the same way that it used to do. */
3225 return FALSE;
3226
3227 return TRUE;
3228}
3229
3230/* Search the symbol table of the archive element of the archive ABFD
3231 whose archive map contains a mention of SYMDEF, and determine if
3232 the symbol is defined in this element. */
3233static bfd_boolean
3234elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3235{
3236 Elf_Internal_Shdr * hdr;
ef53be89
AM
3237 size_t symcount;
3238 size_t extsymcount;
3239 size_t extsymoff;
0ad989f9
L
3240 Elf_Internal_Sym *isymbuf;
3241 Elf_Internal_Sym *isym;
3242 Elf_Internal_Sym *isymend;
3243 bfd_boolean result;
3244
3245 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3246 if (abfd == NULL)
3247 return FALSE;
3248
3249 if (! bfd_check_format (abfd, bfd_object))
3250 return FALSE;
3251
7dc3990e
L
3252 /* Select the appropriate symbol table. If we don't know if the
3253 object file is an IR object, give linker LTO plugin a chance to
3254 get the correct symbol table. */
3255 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3256#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3257 || (abfd->plugin_format == bfd_plugin_unknown
3258 && bfd_link_plugin_object_p (abfd))
3259#endif
3260 )
3261 {
3262 /* Use the IR symbol table if the object has been claimed by
3263 plugin. */
3264 abfd = abfd->plugin_dummy_bfd;
3265 hdr = &elf_tdata (abfd)->symtab_hdr;
3266 }
3267 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3268 hdr = &elf_tdata (abfd)->symtab_hdr;
3269 else
3270 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3271
3272 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3273
3274 /* The sh_info field of the symtab header tells us where the
3275 external symbols start. We don't care about the local symbols. */
3276 if (elf_bad_symtab (abfd))
3277 {
3278 extsymcount = symcount;
3279 extsymoff = 0;
3280 }
3281 else
3282 {
3283 extsymcount = symcount - hdr->sh_info;
3284 extsymoff = hdr->sh_info;
3285 }
3286
3287 if (extsymcount == 0)
3288 return FALSE;
3289
3290 /* Read in the symbol table. */
3291 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3292 NULL, NULL, NULL);
3293 if (isymbuf == NULL)
3294 return FALSE;
3295
3296 /* Scan the symbol table looking for SYMDEF. */
3297 result = FALSE;
3298 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3299 {
3300 const char *name;
3301
3302 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3303 isym->st_name);
3304 if (name == NULL)
3305 break;
3306
3307 if (strcmp (name, symdef->name) == 0)
3308 {
3309 result = is_global_data_symbol_definition (abfd, isym);
3310 break;
3311 }
3312 }
3313
3314 free (isymbuf);
3315
3316 return result;
3317}
3318\f
5a580b3a
AM
3319/* Add an entry to the .dynamic table. */
3320
3321bfd_boolean
3322_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3323 bfd_vma tag,
3324 bfd_vma val)
3325{
3326 struct elf_link_hash_table *hash_table;
3327 const struct elf_backend_data *bed;
3328 asection *s;
3329 bfd_size_type newsize;
3330 bfd_byte *newcontents;
3331 Elf_Internal_Dyn dyn;
3332
3333 hash_table = elf_hash_table (info);
3334 if (! is_elf_hash_table (hash_table))
3335 return FALSE;
3336
3337 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3338 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3339 BFD_ASSERT (s != NULL);
3340
eea6121a 3341 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3342 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3343 if (newcontents == NULL)
3344 return FALSE;
3345
3346 dyn.d_tag = tag;
3347 dyn.d_un.d_val = val;
eea6121a 3348 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3349
eea6121a 3350 s->size = newsize;
5a580b3a
AM
3351 s->contents = newcontents;
3352
3353 return TRUE;
3354}
3355
3356/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3357 otherwise just check whether one already exists. Returns -1 on error,
3358 1 if a DT_NEEDED tag already exists, and 0 on success. */
3359
4ad4eba5 3360static int
7e9f0867
AM
3361elf_add_dt_needed_tag (bfd *abfd,
3362 struct bfd_link_info *info,
4ad4eba5
AM
3363 const char *soname,
3364 bfd_boolean do_it)
5a580b3a
AM
3365{
3366 struct elf_link_hash_table *hash_table;
ef53be89 3367 size_t strindex;
5a580b3a 3368
7e9f0867
AM
3369 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3370 return -1;
3371
5a580b3a 3372 hash_table = elf_hash_table (info);
5a580b3a 3373 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3374 if (strindex == (size_t) -1)
5a580b3a
AM
3375 return -1;
3376
02be4619 3377 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3378 {
3379 asection *sdyn;
3380 const struct elf_backend_data *bed;
3381 bfd_byte *extdyn;
3382
3383 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3384 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3385 if (sdyn != NULL)
3386 for (extdyn = sdyn->contents;
3387 extdyn < sdyn->contents + sdyn->size;
3388 extdyn += bed->s->sizeof_dyn)
3389 {
3390 Elf_Internal_Dyn dyn;
5a580b3a 3391
7e9f0867
AM
3392 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3393 if (dyn.d_tag == DT_NEEDED
3394 && dyn.d_un.d_val == strindex)
3395 {
3396 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3397 return 1;
3398 }
3399 }
5a580b3a
AM
3400 }
3401
3402 if (do_it)
3403 {
7e9f0867
AM
3404 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3405 return -1;
3406
5a580b3a
AM
3407 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3408 return -1;
3409 }
3410 else
3411 /* We were just checking for existence of the tag. */
3412 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3413
3414 return 0;
3415}
3416
7b15fa7a
AM
3417/* Return true if SONAME is on the needed list between NEEDED and STOP
3418 (or the end of list if STOP is NULL), and needed by a library that
3419 will be loaded. */
3420
010e5ae2 3421static bfd_boolean
7b15fa7a
AM
3422on_needed_list (const char *soname,
3423 struct bfd_link_needed_list *needed,
3424 struct bfd_link_needed_list *stop)
010e5ae2 3425{
7b15fa7a
AM
3426 struct bfd_link_needed_list *look;
3427 for (look = needed; look != stop; look = look->next)
3428 if (strcmp (soname, look->name) == 0
3429 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3430 /* If needed by a library that itself is not directly
3431 needed, recursively check whether that library is
3432 indirectly needed. Since we add DT_NEEDED entries to
3433 the end of the list, library dependencies appear after
3434 the library. Therefore search prior to the current
3435 LOOK, preventing possible infinite recursion. */
3436 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3437 return TRUE;
3438
3439 return FALSE;
3440}
3441
14160578 3442/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3443static int
3444elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3445{
3446 const struct elf_link_hash_entry *h1;
3447 const struct elf_link_hash_entry *h2;
10b7e05b 3448 bfd_signed_vma vdiff;
5a580b3a
AM
3449
3450 h1 = *(const struct elf_link_hash_entry **) arg1;
3451 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3452 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3453 if (vdiff != 0)
3454 return vdiff > 0 ? 1 : -1;
3455 else
3456 {
d3435ae8 3457 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3458 if (sdiff != 0)
3459 return sdiff > 0 ? 1 : -1;
3460 }
14160578
AM
3461 vdiff = h1->size - h2->size;
3462 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3463}
4ad4eba5 3464
5a580b3a
AM
3465/* This function is used to adjust offsets into .dynstr for
3466 dynamic symbols. This is called via elf_link_hash_traverse. */
3467
3468static bfd_boolean
3469elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3470{
a50b1753 3471 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3472
5a580b3a
AM
3473 if (h->dynindx != -1)
3474 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3475 return TRUE;
3476}
3477
3478/* Assign string offsets in .dynstr, update all structures referencing
3479 them. */
3480
4ad4eba5
AM
3481static bfd_boolean
3482elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3483{
3484 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3485 struct elf_link_local_dynamic_entry *entry;
3486 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3487 bfd *dynobj = hash_table->dynobj;
3488 asection *sdyn;
3489 bfd_size_type size;
3490 const struct elf_backend_data *bed;
3491 bfd_byte *extdyn;
3492
3493 _bfd_elf_strtab_finalize (dynstr);
3494 size = _bfd_elf_strtab_size (dynstr);
3495
3496 bed = get_elf_backend_data (dynobj);
3d4d4302 3497 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3498 BFD_ASSERT (sdyn != NULL);
3499
3500 /* Update all .dynamic entries referencing .dynstr strings. */
3501 for (extdyn = sdyn->contents;
eea6121a 3502 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3503 extdyn += bed->s->sizeof_dyn)
3504 {
3505 Elf_Internal_Dyn dyn;
3506
3507 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3508 switch (dyn.d_tag)
3509 {
3510 case DT_STRSZ:
3511 dyn.d_un.d_val = size;
3512 break;
3513 case DT_NEEDED:
3514 case DT_SONAME:
3515 case DT_RPATH:
3516 case DT_RUNPATH:
3517 case DT_FILTER:
3518 case DT_AUXILIARY:
7ee314fa
AM
3519 case DT_AUDIT:
3520 case DT_DEPAUDIT:
5a580b3a
AM
3521 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3522 break;
3523 default:
3524 continue;
3525 }
3526 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3527 }
3528
3529 /* Now update local dynamic symbols. */
3530 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3531 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3532 entry->isym.st_name);
3533
3534 /* And the rest of dynamic symbols. */
3535 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3536
3537 /* Adjust version definitions. */
3538 if (elf_tdata (output_bfd)->cverdefs)
3539 {
3540 asection *s;
3541 bfd_byte *p;
ef53be89 3542 size_t i;
5a580b3a
AM
3543 Elf_Internal_Verdef def;
3544 Elf_Internal_Verdaux defaux;
3545
3d4d4302 3546 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3547 p = s->contents;
3548 do
3549 {
3550 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3551 &def);
3552 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3553 if (def.vd_aux != sizeof (Elf_External_Verdef))
3554 continue;
5a580b3a
AM
3555 for (i = 0; i < def.vd_cnt; ++i)
3556 {
3557 _bfd_elf_swap_verdaux_in (output_bfd,
3558 (Elf_External_Verdaux *) p, &defaux);
3559 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3560 defaux.vda_name);
3561 _bfd_elf_swap_verdaux_out (output_bfd,
3562 &defaux, (Elf_External_Verdaux *) p);
3563 p += sizeof (Elf_External_Verdaux);
3564 }
3565 }
3566 while (def.vd_next);
3567 }
3568
3569 /* Adjust version references. */
3570 if (elf_tdata (output_bfd)->verref)
3571 {
3572 asection *s;
3573 bfd_byte *p;
ef53be89 3574 size_t i;
5a580b3a
AM
3575 Elf_Internal_Verneed need;
3576 Elf_Internal_Vernaux needaux;
3577
3d4d4302 3578 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3579 p = s->contents;
3580 do
3581 {
3582 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3583 &need);
3584 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3585 _bfd_elf_swap_verneed_out (output_bfd, &need,
3586 (Elf_External_Verneed *) p);
3587 p += sizeof (Elf_External_Verneed);
3588 for (i = 0; i < need.vn_cnt; ++i)
3589 {
3590 _bfd_elf_swap_vernaux_in (output_bfd,
3591 (Elf_External_Vernaux *) p, &needaux);
3592 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3593 needaux.vna_name);
3594 _bfd_elf_swap_vernaux_out (output_bfd,
3595 &needaux,
3596 (Elf_External_Vernaux *) p);
3597 p += sizeof (Elf_External_Vernaux);
3598 }
3599 }
3600 while (need.vn_next);
3601 }
3602
3603 return TRUE;
3604}
3605\f
13285a1b
AM
3606/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3607 The default is to only match when the INPUT and OUTPUT are exactly
3608 the same target. */
3609
3610bfd_boolean
3611_bfd_elf_default_relocs_compatible (const bfd_target *input,
3612 const bfd_target *output)
3613{
3614 return input == output;
3615}
3616
3617/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3618 This version is used when different targets for the same architecture
3619 are virtually identical. */
3620
3621bfd_boolean
3622_bfd_elf_relocs_compatible (const bfd_target *input,
3623 const bfd_target *output)
3624{
3625 const struct elf_backend_data *obed, *ibed;
3626
3627 if (input == output)
3628 return TRUE;
3629
3630 ibed = xvec_get_elf_backend_data (input);
3631 obed = xvec_get_elf_backend_data (output);
3632
3633 if (ibed->arch != obed->arch)
3634 return FALSE;
3635
3636 /* If both backends are using this function, deem them compatible. */
3637 return ibed->relocs_compatible == obed->relocs_compatible;
3638}
3639
e5034e59
AM
3640/* Make a special call to the linker "notice" function to tell it that
3641 we are about to handle an as-needed lib, or have finished
1b786873 3642 processing the lib. */
e5034e59
AM
3643
3644bfd_boolean
3645_bfd_elf_notice_as_needed (bfd *ibfd,
3646 struct bfd_link_info *info,
3647 enum notice_asneeded_action act)
3648{
46135103 3649 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3650}
3651
d9689752
L
3652/* Check relocations an ELF object file. */
3653
3654bfd_boolean
3655_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3656{
3657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3658 struct elf_link_hash_table *htab = elf_hash_table (info);
3659
3660 /* If this object is the same format as the output object, and it is
3661 not a shared library, then let the backend look through the
3662 relocs.
3663
3664 This is required to build global offset table entries and to
3665 arrange for dynamic relocs. It is not required for the
3666 particular common case of linking non PIC code, even when linking
3667 against shared libraries, but unfortunately there is no way of
3668 knowing whether an object file has been compiled PIC or not.
3669 Looking through the relocs is not particularly time consuming.
3670 The problem is that we must either (1) keep the relocs in memory,
3671 which causes the linker to require additional runtime memory or
3672 (2) read the relocs twice from the input file, which wastes time.
3673 This would be a good case for using mmap.
3674
3675 I have no idea how to handle linking PIC code into a file of a
3676 different format. It probably can't be done. */
3677 if ((abfd->flags & DYNAMIC) == 0
3678 && is_elf_hash_table (htab)
3679 && bed->check_relocs != NULL
3680 && elf_object_id (abfd) == elf_hash_table_id (htab)
3681 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3682 {
3683 asection *o;
3684
3685 for (o = abfd->sections; o != NULL; o = o->next)
3686 {
3687 Elf_Internal_Rela *internal_relocs;
3688 bfd_boolean ok;
3689
5ce03cea 3690 /* Don't check relocations in excluded sections. */
d9689752 3691 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3692 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3693 || o->reloc_count == 0
3694 || ((info->strip == strip_all || info->strip == strip_debugger)
3695 && (o->flags & SEC_DEBUGGING) != 0)
3696 || bfd_is_abs_section (o->output_section))
3697 continue;
3698
3699 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3700 info->keep_memory);
3701 if (internal_relocs == NULL)
3702 return FALSE;
3703
3704 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3705
3706 if (elf_section_data (o)->relocs != internal_relocs)
3707 free (internal_relocs);
3708
3709 if (! ok)
3710 return FALSE;
3711 }
3712 }
3713
3714 return TRUE;
3715}
3716
4ad4eba5
AM
3717/* Add symbols from an ELF object file to the linker hash table. */
3718
3719static bfd_boolean
3720elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3721{
a0c402a5 3722 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3723 Elf_Internal_Shdr *hdr;
ef53be89
AM
3724 size_t symcount;
3725 size_t extsymcount;
3726 size_t extsymoff;
4ad4eba5
AM
3727 struct elf_link_hash_entry **sym_hash;
3728 bfd_boolean dynamic;
3729 Elf_External_Versym *extversym = NULL;
3730 Elf_External_Versym *ever;
3731 struct elf_link_hash_entry *weaks;
3732 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3733 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3734 Elf_Internal_Sym *isymbuf = NULL;
3735 Elf_Internal_Sym *isym;
3736 Elf_Internal_Sym *isymend;
3737 const struct elf_backend_data *bed;
3738 bfd_boolean add_needed;
66eb6687 3739 struct elf_link_hash_table *htab;
4ad4eba5 3740 bfd_size_type amt;
66eb6687 3741 void *alloc_mark = NULL;
4f87808c
AM
3742 struct bfd_hash_entry **old_table = NULL;
3743 unsigned int old_size = 0;
3744 unsigned int old_count = 0;
66eb6687 3745 void *old_tab = NULL;
66eb6687
AM
3746 void *old_ent;
3747 struct bfd_link_hash_entry *old_undefs = NULL;
3748 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3749 void *old_strtab = NULL;
66eb6687 3750 size_t tabsize = 0;
db6a5d5f 3751 asection *s;
29a9f53e 3752 bfd_boolean just_syms;
4ad4eba5 3753
66eb6687 3754 htab = elf_hash_table (info);
4ad4eba5 3755 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3756
3757 if ((abfd->flags & DYNAMIC) == 0)
3758 dynamic = FALSE;
3759 else
3760 {
3761 dynamic = TRUE;
3762
3763 /* You can't use -r against a dynamic object. Also, there's no
3764 hope of using a dynamic object which does not exactly match
3765 the format of the output file. */
0e1862bb 3766 if (bfd_link_relocatable (info)
66eb6687 3767 || !is_elf_hash_table (htab)
f13a99db 3768 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3769 {
0e1862bb 3770 if (bfd_link_relocatable (info))
9a0789ec
NC
3771 bfd_set_error (bfd_error_invalid_operation);
3772 else
3773 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3774 goto error_return;
3775 }
3776 }
3777
a0c402a5
L
3778 ehdr = elf_elfheader (abfd);
3779 if (info->warn_alternate_em
3780 && bed->elf_machine_code != ehdr->e_machine
3781 && ((bed->elf_machine_alt1 != 0
3782 && ehdr->e_machine == bed->elf_machine_alt1)
3783 || (bed->elf_machine_alt2 != 0
3784 && ehdr->e_machine == bed->elf_machine_alt2)))
3785 info->callbacks->einfo
695344c0 3786 /* xgettext:c-format */
a0c402a5
L
3787 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3788 ehdr->e_machine, abfd, bed->elf_machine_code);
3789
4ad4eba5
AM
3790 /* As a GNU extension, any input sections which are named
3791 .gnu.warning.SYMBOL are treated as warning symbols for the given
3792 symbol. This differs from .gnu.warning sections, which generate
3793 warnings when they are included in an output file. */
dd98f8d2 3794 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3795 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3796 {
db6a5d5f 3797 const char *name;
4ad4eba5 3798
db6a5d5f
AM
3799 name = bfd_get_section_name (abfd, s);
3800 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3801 {
db6a5d5f
AM
3802 char *msg;
3803 bfd_size_type sz;
3804
3805 name += sizeof ".gnu.warning." - 1;
3806
3807 /* If this is a shared object, then look up the symbol
3808 in the hash table. If it is there, and it is already
3809 been defined, then we will not be using the entry
3810 from this shared object, so we don't need to warn.
3811 FIXME: If we see the definition in a regular object
3812 later on, we will warn, but we shouldn't. The only
3813 fix is to keep track of what warnings we are supposed
3814 to emit, and then handle them all at the end of the
3815 link. */
3816 if (dynamic)
4ad4eba5 3817 {
db6a5d5f
AM
3818 struct elf_link_hash_entry *h;
3819
3820 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3821
3822 /* FIXME: What about bfd_link_hash_common? */
3823 if (h != NULL
3824 && (h->root.type == bfd_link_hash_defined
3825 || h->root.type == bfd_link_hash_defweak))
3826 continue;
3827 }
4ad4eba5 3828
db6a5d5f
AM
3829 sz = s->size;
3830 msg = (char *) bfd_alloc (abfd, sz + 1);
3831 if (msg == NULL)
3832 goto error_return;
4ad4eba5 3833
db6a5d5f
AM
3834 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3835 goto error_return;
4ad4eba5 3836
db6a5d5f 3837 msg[sz] = '\0';
4ad4eba5 3838
db6a5d5f
AM
3839 if (! (_bfd_generic_link_add_one_symbol
3840 (info, abfd, name, BSF_WARNING, s, 0, msg,
3841 FALSE, bed->collect, NULL)))
3842 goto error_return;
4ad4eba5 3843
0e1862bb 3844 if (bfd_link_executable (info))
db6a5d5f
AM
3845 {
3846 /* Clobber the section size so that the warning does
3847 not get copied into the output file. */
3848 s->size = 0;
11d2f718 3849
db6a5d5f
AM
3850 /* Also set SEC_EXCLUDE, so that symbols defined in
3851 the warning section don't get copied to the output. */
3852 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3853 }
3854 }
3855 }
3856
29a9f53e
L
3857 just_syms = ((s = abfd->sections) != NULL
3858 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3859
4ad4eba5
AM
3860 add_needed = TRUE;
3861 if (! dynamic)
3862 {
3863 /* If we are creating a shared library, create all the dynamic
3864 sections immediately. We need to attach them to something,
3865 so we attach them to this BFD, provided it is the right
bf89386a
L
3866 format and is not from ld --just-symbols. Always create the
3867 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3868 are no input BFD's of the same format as the output, we can't
3869 make a shared library. */
3870 if (!just_syms
bf89386a 3871 && (bfd_link_pic (info)
9c1d7a08 3872 || (!bfd_link_relocatable (info)
3c5fce9b 3873 && info->nointerp
9c1d7a08 3874 && (info->export_dynamic || info->dynamic)))
66eb6687 3875 && is_elf_hash_table (htab)
f13a99db 3876 && info->output_bfd->xvec == abfd->xvec
66eb6687 3877 && !htab->dynamic_sections_created)
4ad4eba5
AM
3878 {
3879 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3880 goto error_return;
3881 }
3882 }
66eb6687 3883 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3884 goto error_return;
3885 else
3886 {
4ad4eba5 3887 const char *soname = NULL;
7ee314fa 3888 char *audit = NULL;
4ad4eba5 3889 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3890 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3891 int ret;
3892
3893 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3894 ld shouldn't allow it. */
29a9f53e 3895 if (just_syms)
92fd189d 3896 abort ();
4ad4eba5
AM
3897
3898 /* If this dynamic lib was specified on the command line with
3899 --as-needed in effect, then we don't want to add a DT_NEEDED
3900 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3901 in by another lib's DT_NEEDED. When --no-add-needed is used
3902 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3903 any dynamic library in DT_NEEDED tags in the dynamic lib at
3904 all. */
3905 add_needed = (elf_dyn_lib_class (abfd)
3906 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3907 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3908
3909 s = bfd_get_section_by_name (abfd, ".dynamic");
3910 if (s != NULL)
3911 {
3912 bfd_byte *dynbuf;
3913 bfd_byte *extdyn;
cb33740c 3914 unsigned int elfsec;
4ad4eba5
AM
3915 unsigned long shlink;
3916
eea6121a 3917 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3918 {
3919error_free_dyn:
3920 free (dynbuf);
3921 goto error_return;
3922 }
4ad4eba5
AM
3923
3924 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3925 if (elfsec == SHN_BAD)
4ad4eba5
AM
3926 goto error_free_dyn;
3927 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3928
3929 for (extdyn = dynbuf;
eea6121a 3930 extdyn < dynbuf + s->size;
4ad4eba5
AM
3931 extdyn += bed->s->sizeof_dyn)
3932 {
3933 Elf_Internal_Dyn dyn;
3934
3935 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3936 if (dyn.d_tag == DT_SONAME)
3937 {
3938 unsigned int tagv = dyn.d_un.d_val;
3939 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3940 if (soname == NULL)
3941 goto error_free_dyn;
3942 }
3943 if (dyn.d_tag == DT_NEEDED)
3944 {
3945 struct bfd_link_needed_list *n, **pn;
3946 char *fnm, *anm;
3947 unsigned int tagv = dyn.d_un.d_val;
3948
3949 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3950 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3951 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3952 if (n == NULL || fnm == NULL)
3953 goto error_free_dyn;
3954 amt = strlen (fnm) + 1;
a50b1753 3955 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3956 if (anm == NULL)
3957 goto error_free_dyn;
3958 memcpy (anm, fnm, amt);
3959 n->name = anm;
3960 n->by = abfd;
3961 n->next = NULL;
66eb6687 3962 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3963 ;
3964 *pn = n;
3965 }
3966 if (dyn.d_tag == DT_RUNPATH)
3967 {
3968 struct bfd_link_needed_list *n, **pn;
3969 char *fnm, *anm;
3970 unsigned int tagv = dyn.d_un.d_val;
3971
3972 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3973 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3974 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3975 if (n == NULL || fnm == NULL)
3976 goto error_free_dyn;
3977 amt = strlen (fnm) + 1;
a50b1753 3978 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3979 if (anm == NULL)
3980 goto error_free_dyn;
3981 memcpy (anm, fnm, amt);
3982 n->name = anm;
3983 n->by = abfd;
3984 n->next = NULL;
3985 for (pn = & runpath;
3986 *pn != NULL;
3987 pn = &(*pn)->next)
3988 ;
3989 *pn = n;
3990 }
3991 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3992 if (!runpath && dyn.d_tag == DT_RPATH)
3993 {
3994 struct bfd_link_needed_list *n, **pn;
3995 char *fnm, *anm;
3996 unsigned int tagv = dyn.d_un.d_val;
3997
3998 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3999 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4000 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4001 if (n == NULL || fnm == NULL)
4002 goto error_free_dyn;
4003 amt = strlen (fnm) + 1;
a50b1753 4004 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4005 if (anm == NULL)
f8703194 4006 goto error_free_dyn;
4ad4eba5
AM
4007 memcpy (anm, fnm, amt);
4008 n->name = anm;
4009 n->by = abfd;
4010 n->next = NULL;
4011 for (pn = & rpath;
4012 *pn != NULL;
4013 pn = &(*pn)->next)
4014 ;
4015 *pn = n;
4016 }
7ee314fa
AM
4017 if (dyn.d_tag == DT_AUDIT)
4018 {
4019 unsigned int tagv = dyn.d_un.d_val;
4020 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4021 }
4ad4eba5
AM
4022 }
4023
4024 free (dynbuf);
4025 }
4026
4027 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4028 frees all more recently bfd_alloc'd blocks as well. */
4029 if (runpath)
4030 rpath = runpath;
4031
4032 if (rpath)
4033 {
4034 struct bfd_link_needed_list **pn;
66eb6687 4035 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4036 ;
4037 *pn = rpath;
4038 }
4039
9acc85a6
AM
4040 /* If we have a PT_GNU_RELRO program header, mark as read-only
4041 all sections contained fully therein. This makes relro
4042 shared library sections appear as they will at run-time. */
4043 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4044 while (--phdr >= elf_tdata (abfd)->phdr)
4045 if (phdr->p_type == PT_GNU_RELRO)
4046 {
4047 for (s = abfd->sections; s != NULL; s = s->next)
4048 if ((s->flags & SEC_ALLOC) != 0
4049 && s->vma >= phdr->p_vaddr
4050 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4051 s->flags |= SEC_READONLY;
4052 break;
4053 }
4054
4ad4eba5
AM
4055 /* We do not want to include any of the sections in a dynamic
4056 object in the output file. We hack by simply clobbering the
4057 list of sections in the BFD. This could be handled more
4058 cleanly by, say, a new section flag; the existing
4059 SEC_NEVER_LOAD flag is not the one we want, because that one
4060 still implies that the section takes up space in the output
4061 file. */
4062 bfd_section_list_clear (abfd);
4063
4ad4eba5
AM
4064 /* Find the name to use in a DT_NEEDED entry that refers to this
4065 object. If the object has a DT_SONAME entry, we use it.
4066 Otherwise, if the generic linker stuck something in
4067 elf_dt_name, we use that. Otherwise, we just use the file
4068 name. */
4069 if (soname == NULL || *soname == '\0')
4070 {
4071 soname = elf_dt_name (abfd);
4072 if (soname == NULL || *soname == '\0')
4073 soname = bfd_get_filename (abfd);
4074 }
4075
4076 /* Save the SONAME because sometimes the linker emulation code
4077 will need to know it. */
4078 elf_dt_name (abfd) = soname;
4079
7e9f0867 4080 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4081 if (ret < 0)
4082 goto error_return;
4083
4084 /* If we have already included this dynamic object in the
4085 link, just ignore it. There is no reason to include a
4086 particular dynamic object more than once. */
4087 if (ret > 0)
4088 return TRUE;
7ee314fa
AM
4089
4090 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4091 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4092 }
4093
4094 /* If this is a dynamic object, we always link against the .dynsym
4095 symbol table, not the .symtab symbol table. The dynamic linker
4096 will only see the .dynsym symbol table, so there is no reason to
4097 look at .symtab for a dynamic object. */
4098
4099 if (! dynamic || elf_dynsymtab (abfd) == 0)
4100 hdr = &elf_tdata (abfd)->symtab_hdr;
4101 else
4102 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4103
4104 symcount = hdr->sh_size / bed->s->sizeof_sym;
4105
4106 /* The sh_info field of the symtab header tells us where the
4107 external symbols start. We don't care about the local symbols at
4108 this point. */
4109 if (elf_bad_symtab (abfd))
4110 {
4111 extsymcount = symcount;
4112 extsymoff = 0;
4113 }
4114 else
4115 {
4116 extsymcount = symcount - hdr->sh_info;
4117 extsymoff = hdr->sh_info;
4118 }
4119
f45794cb 4120 sym_hash = elf_sym_hashes (abfd);
012b2306 4121 if (extsymcount != 0)
4ad4eba5
AM
4122 {
4123 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4124 NULL, NULL, NULL);
4125 if (isymbuf == NULL)
4126 goto error_return;
4127
4ad4eba5 4128 if (sym_hash == NULL)
012b2306
AM
4129 {
4130 /* We store a pointer to the hash table entry for each
4131 external symbol. */
ef53be89
AM
4132 amt = extsymcount;
4133 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4134 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4135 if (sym_hash == NULL)
4136 goto error_free_sym;
4137 elf_sym_hashes (abfd) = sym_hash;
4138 }
4ad4eba5
AM
4139 }
4140
4141 if (dynamic)
4142 {
4143 /* Read in any version definitions. */
fc0e6df6
PB
4144 if (!_bfd_elf_slurp_version_tables (abfd,
4145 info->default_imported_symver))
4ad4eba5
AM
4146 goto error_free_sym;
4147
4148 /* Read in the symbol versions, but don't bother to convert them
4149 to internal format. */
4150 if (elf_dynversym (abfd) != 0)
4151 {
4152 Elf_Internal_Shdr *versymhdr;
4153
4154 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4155 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4156 if (extversym == NULL)
4157 goto error_free_sym;
4158 amt = versymhdr->sh_size;
4159 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4160 || bfd_bread (extversym, amt, abfd) != amt)
4161 goto error_free_vers;
4162 }
4163 }
4164
66eb6687
AM
4165 /* If we are loading an as-needed shared lib, save the symbol table
4166 state before we start adding symbols. If the lib turns out
4167 to be unneeded, restore the state. */
4168 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4169 {
4170 unsigned int i;
4171 size_t entsize;
4172
4173 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4174 {
4175 struct bfd_hash_entry *p;
2de92251 4176 struct elf_link_hash_entry *h;
66eb6687
AM
4177
4178 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4179 {
4180 h = (struct elf_link_hash_entry *) p;
4181 entsize += htab->root.table.entsize;
4182 if (h->root.type == bfd_link_hash_warning)
4183 entsize += htab->root.table.entsize;
4184 }
66eb6687
AM
4185 }
4186
4187 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4188 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4189 if (old_tab == NULL)
4190 goto error_free_vers;
4191
4192 /* Remember the current objalloc pointer, so that all mem for
4193 symbols added can later be reclaimed. */
4194 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4195 if (alloc_mark == NULL)
4196 goto error_free_vers;
4197
5061a885
AM
4198 /* Make a special call to the linker "notice" function to
4199 tell it that we are about to handle an as-needed lib. */
e5034e59 4200 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4201 goto error_free_vers;
5061a885 4202
f45794cb
AM
4203 /* Clone the symbol table. Remember some pointers into the
4204 symbol table, and dynamic symbol count. */
4205 old_ent = (char *) old_tab + tabsize;
66eb6687 4206 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4207 old_undefs = htab->root.undefs;
4208 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4209 old_table = htab->root.table.table;
4210 old_size = htab->root.table.size;
4211 old_count = htab->root.table.count;
5b677558
AM
4212 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4213 if (old_strtab == NULL)
4214 goto error_free_vers;
66eb6687
AM
4215
4216 for (i = 0; i < htab->root.table.size; i++)
4217 {
4218 struct bfd_hash_entry *p;
2de92251 4219 struct elf_link_hash_entry *h;
66eb6687
AM
4220
4221 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4222 {
4223 memcpy (old_ent, p, htab->root.table.entsize);
4224 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4225 h = (struct elf_link_hash_entry *) p;
4226 if (h->root.type == bfd_link_hash_warning)
4227 {
4228 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4229 old_ent = (char *) old_ent + htab->root.table.entsize;
4230 }
66eb6687
AM
4231 }
4232 }
4233 }
4ad4eba5 4234
66eb6687 4235 weaks = NULL;
4ad4eba5
AM
4236 ever = extversym != NULL ? extversym + extsymoff : NULL;
4237 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4238 isym < isymend;
4239 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4240 {
4241 int bind;
4242 bfd_vma value;
af44c138 4243 asection *sec, *new_sec;
4ad4eba5
AM
4244 flagword flags;
4245 const char *name;
4246 struct elf_link_hash_entry *h;
90c984fc 4247 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4248 bfd_boolean definition;
4249 bfd_boolean size_change_ok;
4250 bfd_boolean type_change_ok;
4251 bfd_boolean new_weakdef;
37a9e49a
L
4252 bfd_boolean new_weak;
4253 bfd_boolean old_weak;
4ad4eba5 4254 bfd_boolean override;
a4d8e49b 4255 bfd_boolean common;
97196564 4256 bfd_boolean discarded;
4ad4eba5
AM
4257 unsigned int old_alignment;
4258 bfd *old_bfd;
6e33951e 4259 bfd_boolean matched;
4ad4eba5
AM
4260
4261 override = FALSE;
4262
4263 flags = BSF_NO_FLAGS;
4264 sec = NULL;
4265 value = isym->st_value;
a4d8e49b 4266 common = bed->common_definition (isym);
2980ccad
L
4267 if (common && info->inhibit_common_definition)
4268 {
4269 /* Treat common symbol as undefined for --no-define-common. */
4270 isym->st_shndx = SHN_UNDEF;
4271 common = FALSE;
4272 }
97196564 4273 discarded = FALSE;
4ad4eba5
AM
4274
4275 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4276 switch (bind)
4ad4eba5 4277 {
3e7a7d11 4278 case STB_LOCAL:
4ad4eba5
AM
4279 /* This should be impossible, since ELF requires that all
4280 global symbols follow all local symbols, and that sh_info
4281 point to the first global symbol. Unfortunately, Irix 5
4282 screws this up. */
4283 continue;
3e7a7d11
NC
4284
4285 case STB_GLOBAL:
a4d8e49b 4286 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4287 flags = BSF_GLOBAL;
3e7a7d11
NC
4288 break;
4289
4290 case STB_WEAK:
4291 flags = BSF_WEAK;
4292 break;
4293
4294 case STB_GNU_UNIQUE:
4295 flags = BSF_GNU_UNIQUE;
4296 break;
4297
4298 default:
4ad4eba5 4299 /* Leave it up to the processor backend. */
3e7a7d11 4300 break;
4ad4eba5
AM
4301 }
4302
4303 if (isym->st_shndx == SHN_UNDEF)
4304 sec = bfd_und_section_ptr;
cb33740c
AM
4305 else if (isym->st_shndx == SHN_ABS)
4306 sec = bfd_abs_section_ptr;
4307 else if (isym->st_shndx == SHN_COMMON)
4308 {
4309 sec = bfd_com_section_ptr;
4310 /* What ELF calls the size we call the value. What ELF
4311 calls the value we call the alignment. */
4312 value = isym->st_size;
4313 }
4314 else
4ad4eba5
AM
4315 {
4316 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4317 if (sec == NULL)
4318 sec = bfd_abs_section_ptr;
dbaa2011 4319 else if (discarded_section (sec))
529fcb95 4320 {
e5d08002
L
4321 /* Symbols from discarded section are undefined. We keep
4322 its visibility. */
529fcb95 4323 sec = bfd_und_section_ptr;
97196564 4324 discarded = TRUE;
529fcb95
PB
4325 isym->st_shndx = SHN_UNDEF;
4326 }
4ad4eba5
AM
4327 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4328 value -= sec->vma;
4329 }
4ad4eba5
AM
4330
4331 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4332 isym->st_name);
4333 if (name == NULL)
4334 goto error_free_vers;
4335
4336 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4337 && (abfd->flags & BFD_PLUGIN) != 0)
4338 {
4339 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4340
4341 if (xc == NULL)
4342 {
4343 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4344 | SEC_EXCLUDE);
4345 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4346 if (xc == NULL)
4347 goto error_free_vers;
4348 }
4349 sec = xc;
4350 }
4351 else if (isym->st_shndx == SHN_COMMON
4352 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4353 && !bfd_link_relocatable (info))
4ad4eba5
AM
4354 {
4355 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4356
4357 if (tcomm == NULL)
4358 {
02d00247
AM
4359 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4360 | SEC_LINKER_CREATED);
4361 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4362 if (tcomm == NULL)
4ad4eba5
AM
4363 goto error_free_vers;
4364 }
4365 sec = tcomm;
4366 }
66eb6687 4367 else if (bed->elf_add_symbol_hook)
4ad4eba5 4368 {
66eb6687
AM
4369 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4370 &sec, &value))
4ad4eba5
AM
4371 goto error_free_vers;
4372
4373 /* The hook function sets the name to NULL if this symbol
4374 should be skipped for some reason. */
4375 if (name == NULL)
4376 continue;
4377 }
4378
4379 /* Sanity check that all possibilities were handled. */
4380 if (sec == NULL)
4381 {
4382 bfd_set_error (bfd_error_bad_value);
4383 goto error_free_vers;
4384 }
4385
191c0c42
AM
4386 /* Silently discard TLS symbols from --just-syms. There's
4387 no way to combine a static TLS block with a new TLS block
4388 for this executable. */
4389 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4390 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4391 continue;
4392
4ad4eba5
AM
4393 if (bfd_is_und_section (sec)
4394 || bfd_is_com_section (sec))
4395 definition = FALSE;
4396 else
4397 definition = TRUE;
4398
4399 size_change_ok = FALSE;
66eb6687 4400 type_change_ok = bed->type_change_ok;
37a9e49a 4401 old_weak = FALSE;
6e33951e 4402 matched = FALSE;
4ad4eba5
AM
4403 old_alignment = 0;
4404 old_bfd = NULL;
af44c138 4405 new_sec = sec;
4ad4eba5 4406
66eb6687 4407 if (is_elf_hash_table (htab))
4ad4eba5
AM
4408 {
4409 Elf_Internal_Versym iver;
4410 unsigned int vernum = 0;
4411 bfd_boolean skip;
4412
fc0e6df6 4413 if (ever == NULL)
4ad4eba5 4414 {
fc0e6df6
PB
4415 if (info->default_imported_symver)
4416 /* Use the default symbol version created earlier. */
4417 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4418 else
4419 iver.vs_vers = 0;
4420 }
4421 else
4422 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4423
4424 vernum = iver.vs_vers & VERSYM_VERSION;
4425
4426 /* If this is a hidden symbol, or if it is not version
4427 1, we append the version name to the symbol name.
cc86ff91
EB
4428 However, we do not modify a non-hidden absolute symbol
4429 if it is not a function, because it might be the version
4430 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4431 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4432 || (vernum > 1
4433 && (!bfd_is_abs_section (sec)
4434 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4435 {
4436 const char *verstr;
4437 size_t namelen, verlen, newlen;
4438 char *newname, *p;
4439
4440 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4441 {
fc0e6df6
PB
4442 if (vernum > elf_tdata (abfd)->cverdefs)
4443 verstr = NULL;
4444 else if (vernum > 1)
4445 verstr =
4446 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4447 else
4448 verstr = "";
4ad4eba5 4449
fc0e6df6 4450 if (verstr == NULL)
4ad4eba5 4451 {
4eca0228 4452 _bfd_error_handler
695344c0 4453 /* xgettext:c-format */
fc0e6df6
PB
4454 (_("%B: %s: invalid version %u (max %d)"),
4455 abfd, name, vernum,
4456 elf_tdata (abfd)->cverdefs);
4457 bfd_set_error (bfd_error_bad_value);
4458 goto error_free_vers;
4ad4eba5 4459 }
fc0e6df6
PB
4460 }
4461 else
4462 {
4463 /* We cannot simply test for the number of
4464 entries in the VERNEED section since the
4465 numbers for the needed versions do not start
4466 at 0. */
4467 Elf_Internal_Verneed *t;
4468
4469 verstr = NULL;
4470 for (t = elf_tdata (abfd)->verref;
4471 t != NULL;
4472 t = t->vn_nextref)
4ad4eba5 4473 {
fc0e6df6 4474 Elf_Internal_Vernaux *a;
4ad4eba5 4475
fc0e6df6
PB
4476 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4477 {
4478 if (a->vna_other == vernum)
4ad4eba5 4479 {
fc0e6df6
PB
4480 verstr = a->vna_nodename;
4481 break;
4ad4eba5 4482 }
4ad4eba5 4483 }
fc0e6df6
PB
4484 if (a != NULL)
4485 break;
4486 }
4487 if (verstr == NULL)
4488 {
4eca0228 4489 _bfd_error_handler
695344c0 4490 /* xgettext:c-format */
fc0e6df6
PB
4491 (_("%B: %s: invalid needed version %d"),
4492 abfd, name, vernum);
4493 bfd_set_error (bfd_error_bad_value);
4494 goto error_free_vers;
4ad4eba5 4495 }
4ad4eba5 4496 }
fc0e6df6
PB
4497
4498 namelen = strlen (name);
4499 verlen = strlen (verstr);
4500 newlen = namelen + verlen + 2;
4501 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4502 && isym->st_shndx != SHN_UNDEF)
4503 ++newlen;
4504
a50b1753 4505 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4506 if (newname == NULL)
4507 goto error_free_vers;
4508 memcpy (newname, name, namelen);
4509 p = newname + namelen;
4510 *p++ = ELF_VER_CHR;
4511 /* If this is a defined non-hidden version symbol,
4512 we add another @ to the name. This indicates the
4513 default version of the symbol. */
4514 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4515 && isym->st_shndx != SHN_UNDEF)
4516 *p++ = ELF_VER_CHR;
4517 memcpy (p, verstr, verlen + 1);
4518
4519 name = newname;
4ad4eba5
AM
4520 }
4521
cd3416da
AM
4522 /* If this symbol has default visibility and the user has
4523 requested we not re-export it, then mark it as hidden. */
a0d49154 4524 if (!bfd_is_und_section (sec)
cd3416da 4525 && !dynamic
ce875075 4526 && abfd->no_export
cd3416da
AM
4527 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4528 isym->st_other = (STV_HIDDEN
4529 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4530
4f3fedcf
AM
4531 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4532 sym_hash, &old_bfd, &old_weak,
4533 &old_alignment, &skip, &override,
6e33951e
L
4534 &type_change_ok, &size_change_ok,
4535 &matched))
4ad4eba5
AM
4536 goto error_free_vers;
4537
4538 if (skip)
4539 continue;
4540
6e33951e
L
4541 /* Override a definition only if the new symbol matches the
4542 existing one. */
4543 if (override && matched)
4ad4eba5
AM
4544 definition = FALSE;
4545
4546 h = *sym_hash;
4547 while (h->root.type == bfd_link_hash_indirect
4548 || h->root.type == bfd_link_hash_warning)
4549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4550
4ad4eba5 4551 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4552 && vernum > 1
4553 && definition)
4554 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4555 }
4556
4557 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4558 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4559 (struct bfd_link_hash_entry **) sym_hash)))
4560 goto error_free_vers;
4561
a43942db
MR
4562 if ((flags & BSF_GNU_UNIQUE)
4563 && (abfd->flags & DYNAMIC) == 0
4564 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4565 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4566
4ad4eba5 4567 h = *sym_hash;
90c984fc
L
4568 /* We need to make sure that indirect symbol dynamic flags are
4569 updated. */
4570 hi = h;
4ad4eba5
AM
4571 while (h->root.type == bfd_link_hash_indirect
4572 || h->root.type == bfd_link_hash_warning)
4573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4574
97196564
L
4575 /* Setting the index to -3 tells elf_link_output_extsym that
4576 this symbol is defined in a discarded section. */
4577 if (discarded)
4578 h->indx = -3;
4579
4ad4eba5
AM
4580 *sym_hash = h;
4581
37a9e49a 4582 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4583 new_weakdef = FALSE;
4584 if (dynamic
4585 && definition
37a9e49a 4586 && new_weak
fcb93ecf 4587 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4588 && is_elf_hash_table (htab)
f6e332e6 4589 && h->u.weakdef == NULL)
4ad4eba5
AM
4590 {
4591 /* Keep a list of all weak defined non function symbols from
4592 a dynamic object, using the weakdef field. Later in this
4593 function we will set the weakdef field to the correct
4594 value. We only put non-function symbols from dynamic
4595 objects on this list, because that happens to be the only
4596 time we need to know the normal symbol corresponding to a
4597 weak symbol, and the information is time consuming to
4598 figure out. If the weakdef field is not already NULL,
4599 then this symbol was already defined by some previous
4600 dynamic object, and we will be using that previous
4601 definition anyhow. */
4602
f6e332e6 4603 h->u.weakdef = weaks;
4ad4eba5
AM
4604 weaks = h;
4605 new_weakdef = TRUE;
4606 }
4607
4608 /* Set the alignment of a common symbol. */
a4d8e49b 4609 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4610 && h->root.type == bfd_link_hash_common)
4611 {
4612 unsigned int align;
4613
a4d8e49b 4614 if (common)
af44c138
L
4615 align = bfd_log2 (isym->st_value);
4616 else
4617 {
4618 /* The new symbol is a common symbol in a shared object.
4619 We need to get the alignment from the section. */
4620 align = new_sec->alignment_power;
4621 }
595213d4 4622 if (align > old_alignment)
4ad4eba5
AM
4623 h->root.u.c.p->alignment_power = align;
4624 else
4625 h->root.u.c.p->alignment_power = old_alignment;
4626 }
4627
66eb6687 4628 if (is_elf_hash_table (htab))
4ad4eba5 4629 {
4f3fedcf
AM
4630 /* Set a flag in the hash table entry indicating the type of
4631 reference or definition we just found. A dynamic symbol
4632 is one which is referenced or defined by both a regular
4633 object and a shared object. */
4634 bfd_boolean dynsym = FALSE;
4635
4636 /* Plugin symbols aren't normal. Don't set def_regular or
4637 ref_regular for them, or make them dynamic. */
4638 if ((abfd->flags & BFD_PLUGIN) != 0)
4639 ;
4640 else if (! dynamic)
4641 {
4642 if (! definition)
4643 {
4644 h->ref_regular = 1;
4645 if (bind != STB_WEAK)
4646 h->ref_regular_nonweak = 1;
4647 }
4648 else
4649 {
4650 h->def_regular = 1;
4651 if (h->def_dynamic)
4652 {
4653 h->def_dynamic = 0;
4654 h->ref_dynamic = 1;
4655 }
4656 }
4657
4658 /* If the indirect symbol has been forced local, don't
4659 make the real symbol dynamic. */
4660 if ((h == hi || !hi->forced_local)
0e1862bb 4661 && (bfd_link_dll (info)
4f3fedcf
AM
4662 || h->def_dynamic
4663 || h->ref_dynamic))
4664 dynsym = TRUE;
4665 }
4666 else
4667 {
4668 if (! definition)
4669 {
4670 h->ref_dynamic = 1;
4671 hi->ref_dynamic = 1;
4672 }
4673 else
4674 {
4675 h->def_dynamic = 1;
4676 hi->def_dynamic = 1;
4677 }
4678
4679 /* If the indirect symbol has been forced local, don't
4680 make the real symbol dynamic. */
4681 if ((h == hi || !hi->forced_local)
4682 && (h->def_regular
4683 || h->ref_regular
4684 || (h->u.weakdef != NULL
4685 && ! new_weakdef
4686 && h->u.weakdef->dynindx != -1)))
4687 dynsym = TRUE;
4688 }
4689
4690 /* Check to see if we need to add an indirect symbol for
4691 the default name. */
4692 if (definition
4693 || (!override && h->root.type == bfd_link_hash_common))
4694 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4695 sec, value, &old_bfd, &dynsym))
4696 goto error_free_vers;
4ad4eba5
AM
4697
4698 /* Check the alignment when a common symbol is involved. This
4699 can change when a common symbol is overridden by a normal
4700 definition or a common symbol is ignored due to the old
4701 normal definition. We need to make sure the maximum
4702 alignment is maintained. */
a4d8e49b 4703 if ((old_alignment || common)
4ad4eba5
AM
4704 && h->root.type != bfd_link_hash_common)
4705 {
4706 unsigned int common_align;
4707 unsigned int normal_align;
4708 unsigned int symbol_align;
4709 bfd *normal_bfd;
4710 bfd *common_bfd;
4711
3a81e825
AM
4712 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4713 || h->root.type == bfd_link_hash_defweak);
4714
4ad4eba5
AM
4715 symbol_align = ffs (h->root.u.def.value) - 1;
4716 if (h->root.u.def.section->owner != NULL
0616a280
AM
4717 && (h->root.u.def.section->owner->flags
4718 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4719 {
4720 normal_align = h->root.u.def.section->alignment_power;
4721 if (normal_align > symbol_align)
4722 normal_align = symbol_align;
4723 }
4724 else
4725 normal_align = symbol_align;
4726
4727 if (old_alignment)
4728 {
4729 common_align = old_alignment;
4730 common_bfd = old_bfd;
4731 normal_bfd = abfd;
4732 }
4733 else
4734 {
4735 common_align = bfd_log2 (isym->st_value);
4736 common_bfd = abfd;
4737 normal_bfd = old_bfd;
4738 }
4739
4740 if (normal_align < common_align)
d07676f8
NC
4741 {
4742 /* PR binutils/2735 */
4743 if (normal_bfd == NULL)
4eca0228 4744 _bfd_error_handler
695344c0 4745 /* xgettext:c-format */
4f3fedcf
AM
4746 (_("Warning: alignment %u of common symbol `%s' in %B is"
4747 " greater than the alignment (%u) of its section %A"),
c08bb8dd
AM
4748 1 << common_align, name, common_bfd,
4749 1 << normal_align, h->root.u.def.section);
d07676f8 4750 else
4eca0228 4751 _bfd_error_handler
695344c0 4752 /* xgettext:c-format */
d07676f8
NC
4753 (_("Warning: alignment %u of symbol `%s' in %B"
4754 " is smaller than %u in %B"),
c08bb8dd
AM
4755 1 << normal_align, name, normal_bfd,
4756 1 << common_align, common_bfd);
d07676f8 4757 }
4ad4eba5
AM
4758 }
4759
83ad0046 4760 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4761 if (isym->st_size != 0
4762 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4763 && (definition || h->size == 0))
4764 {
83ad0046
L
4765 if (h->size != 0
4766 && h->size != isym->st_size
4767 && ! size_change_ok)
4eca0228 4768 _bfd_error_handler
695344c0 4769 /* xgettext:c-format */
d003868e 4770 (_("Warning: size of symbol `%s' changed"
76cfced5
AM
4771 " from %Lu in %B to %Lu in %B"),
4772 name, h->size, old_bfd, isym->st_size, abfd);
4ad4eba5
AM
4773
4774 h->size = isym->st_size;
4775 }
4776
4777 /* If this is a common symbol, then we always want H->SIZE
4778 to be the size of the common symbol. The code just above
4779 won't fix the size if a common symbol becomes larger. We
4780 don't warn about a size change here, because that is
4f3fedcf 4781 covered by --warn-common. Allow changes between different
fcb93ecf 4782 function types. */
4ad4eba5
AM
4783 if (h->root.type == bfd_link_hash_common)
4784 h->size = h->root.u.c.size;
4785
4786 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4787 && ((definition && !new_weak)
4788 || (old_weak && h->root.type == bfd_link_hash_common)
4789 || h->type == STT_NOTYPE))
4ad4eba5 4790 {
2955ec4c
L
4791 unsigned int type = ELF_ST_TYPE (isym->st_info);
4792
4793 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4794 symbol. */
4795 if (type == STT_GNU_IFUNC
4796 && (abfd->flags & DYNAMIC) != 0)
4797 type = STT_FUNC;
4ad4eba5 4798
2955ec4c
L
4799 if (h->type != type)
4800 {
4801 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4802 /* xgettext:c-format */
4eca0228 4803 _bfd_error_handler
2955ec4c
L
4804 (_("Warning: type of symbol `%s' changed"
4805 " from %d to %d in %B"),
c08bb8dd 4806 name, h->type, type, abfd);
2955ec4c
L
4807
4808 h->type = type;
4809 }
4ad4eba5
AM
4810 }
4811
54ac0771 4812 /* Merge st_other field. */
b8417128 4813 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4814
c3df8c14 4815 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4816 if (definition
4817 && (sec->flags & SEC_DEBUGGING)
4818 && !bfd_link_relocatable (info))
c3df8c14
AM
4819 dynsym = FALSE;
4820
4f3fedcf
AM
4821 /* Nor should we make plugin symbols dynamic. */
4822 if ((abfd->flags & BFD_PLUGIN) != 0)
4823 dynsym = FALSE;
4824
35fc36a8 4825 if (definition)
35399224
L
4826 {
4827 h->target_internal = isym->st_target_internal;
4828 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4829 }
35fc36a8 4830
4ad4eba5
AM
4831 if (definition && !dynamic)
4832 {
4833 char *p = strchr (name, ELF_VER_CHR);
4834 if (p != NULL && p[1] != ELF_VER_CHR)
4835 {
4836 /* Queue non-default versions so that .symver x, x@FOO
4837 aliases can be checked. */
66eb6687 4838 if (!nondeflt_vers)
4ad4eba5 4839 {
66eb6687
AM
4840 amt = ((isymend - isym + 1)
4841 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4842 nondeflt_vers
4843 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4844 if (!nondeflt_vers)
4845 goto error_free_vers;
4ad4eba5 4846 }
66eb6687 4847 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4848 }
4849 }
4850
4851 if (dynsym && h->dynindx == -1)
4852 {
c152c796 4853 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4854 goto error_free_vers;
f6e332e6 4855 if (h->u.weakdef != NULL
4ad4eba5 4856 && ! new_weakdef
f6e332e6 4857 && h->u.weakdef->dynindx == -1)
4ad4eba5 4858 {
66eb6687 4859 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4860 goto error_free_vers;
4861 }
4862 }
1f599d0e 4863 else if (h->dynindx != -1)
4ad4eba5
AM
4864 /* If the symbol already has a dynamic index, but
4865 visibility says it should not be visible, turn it into
4866 a local symbol. */
4867 switch (ELF_ST_VISIBILITY (h->other))
4868 {
4869 case STV_INTERNAL:
4870 case STV_HIDDEN:
4871 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4872 dynsym = FALSE;
4873 break;
4874 }
4875
aef28989
L
4876 /* Don't add DT_NEEDED for references from the dummy bfd nor
4877 for unmatched symbol. */
4ad4eba5 4878 if (!add_needed
aef28989 4879 && matched
4ad4eba5 4880 && definition
010e5ae2 4881 && ((dynsym
ffa9430d 4882 && h->ref_regular_nonweak
4f3fedcf
AM
4883 && (old_bfd == NULL
4884 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4885 || (h->ref_dynamic_nonweak
010e5ae2 4886 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4887 && !on_needed_list (elf_dt_name (abfd),
4888 htab->needed, NULL))))
4ad4eba5
AM
4889 {
4890 int ret;
4891 const char *soname = elf_dt_name (abfd);
4892
16e4ecc0
AM
4893 info->callbacks->minfo ("%!", soname, old_bfd,
4894 h->root.root.string);
4895
4ad4eba5
AM
4896 /* A symbol from a library loaded via DT_NEEDED of some
4897 other library is referenced by a regular object.
e56f61be 4898 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4899 --no-add-needed is used and the reference was not
4900 a weak one. */
4f3fedcf 4901 if (old_bfd != NULL
b918acf9 4902 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4903 {
4eca0228 4904 _bfd_error_handler
695344c0 4905 /* xgettext:c-format */
3cbc5de0 4906 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4907 old_bfd, name);
ff5ac77b 4908 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4909 goto error_free_vers;
4910 }
4911
a50b1753 4912 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4913 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4914
4ad4eba5 4915 add_needed = TRUE;
7e9f0867 4916 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4917 if (ret < 0)
4918 goto error_free_vers;
4919
4920 BFD_ASSERT (ret == 0);
4921 }
4922 }
4923 }
4924
66eb6687
AM
4925 if (extversym != NULL)
4926 {
4927 free (extversym);
4928 extversym = NULL;
4929 }
4930
4931 if (isymbuf != NULL)
4932 {
4933 free (isymbuf);
4934 isymbuf = NULL;
4935 }
4936
4937 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4938 {
4939 unsigned int i;
4940
4941 /* Restore the symbol table. */
f45794cb
AM
4942 old_ent = (char *) old_tab + tabsize;
4943 memset (elf_sym_hashes (abfd), 0,
4944 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4945 htab->root.table.table = old_table;
4946 htab->root.table.size = old_size;
4947 htab->root.table.count = old_count;
66eb6687 4948 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4949 htab->root.undefs = old_undefs;
4950 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4951 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4952 free (old_strtab);
4953 old_strtab = NULL;
66eb6687
AM
4954 for (i = 0; i < htab->root.table.size; i++)
4955 {
4956 struct bfd_hash_entry *p;
4957 struct elf_link_hash_entry *h;
3e0882af
L
4958 bfd_size_type size;
4959 unsigned int alignment_power;
4070765b 4960 unsigned int non_ir_ref_dynamic;
66eb6687
AM
4961
4962 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4963 {
4964 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4965 if (h->root.type == bfd_link_hash_warning)
4966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4967
3e0882af
L
4968 /* Preserve the maximum alignment and size for common
4969 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4970 since it can still be loaded at run time by another
3e0882af
L
4971 dynamic lib. */
4972 if (h->root.type == bfd_link_hash_common)
4973 {
4974 size = h->root.u.c.size;
4975 alignment_power = h->root.u.c.p->alignment_power;
4976 }
4977 else
4978 {
4979 size = 0;
4980 alignment_power = 0;
4981 }
4070765b 4982 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
4983 will be exported when the dynamic lib becomes needed
4984 in the second pass. */
4070765b 4985 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
4986 memcpy (p, old_ent, htab->root.table.entsize);
4987 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4988 h = (struct elf_link_hash_entry *) p;
4989 if (h->root.type == bfd_link_hash_warning)
4990 {
4991 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4992 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4994 }
a4542f1b 4995 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4996 {
4997 if (size > h->root.u.c.size)
4998 h->root.u.c.size = size;
4999 if (alignment_power > h->root.u.c.p->alignment_power)
5000 h->root.u.c.p->alignment_power = alignment_power;
5001 }
4070765b 5002 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5003 }
5004 }
5005
5061a885
AM
5006 /* Make a special call to the linker "notice" function to
5007 tell it that symbols added for crefs may need to be removed. */
e5034e59 5008 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5009 goto error_free_vers;
5061a885 5010
66eb6687
AM
5011 free (old_tab);
5012 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5013 alloc_mark);
5014 if (nondeflt_vers != NULL)
5015 free (nondeflt_vers);
5016 return TRUE;
5017 }
2de92251 5018
66eb6687
AM
5019 if (old_tab != NULL)
5020 {
e5034e59 5021 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5022 goto error_free_vers;
66eb6687
AM
5023 free (old_tab);
5024 old_tab = NULL;
5025 }
5026
c6e8a9a8
L
5027 /* Now that all the symbols from this input file are created, if
5028 not performing a relocatable link, handle .symver foo, foo@BAR
5029 such that any relocs against foo become foo@BAR. */
0e1862bb 5030 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5031 {
ef53be89 5032 size_t cnt, symidx;
4ad4eba5
AM
5033
5034 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5035 {
5036 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5037 char *shortname, *p;
5038
5039 p = strchr (h->root.root.string, ELF_VER_CHR);
5040 if (p == NULL
5041 || (h->root.type != bfd_link_hash_defined
5042 && h->root.type != bfd_link_hash_defweak))
5043 continue;
5044
5045 amt = p - h->root.root.string;
a50b1753 5046 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5047 if (!shortname)
5048 goto error_free_vers;
4ad4eba5
AM
5049 memcpy (shortname, h->root.root.string, amt);
5050 shortname[amt] = '\0';
5051
5052 hi = (struct elf_link_hash_entry *)
66eb6687 5053 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5054 FALSE, FALSE, FALSE);
5055 if (hi != NULL
5056 && hi->root.type == h->root.type
5057 && hi->root.u.def.value == h->root.u.def.value
5058 && hi->root.u.def.section == h->root.u.def.section)
5059 {
5060 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5061 hi->root.type = bfd_link_hash_indirect;
5062 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5063 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5064 sym_hash = elf_sym_hashes (abfd);
5065 if (sym_hash)
5066 for (symidx = 0; symidx < extsymcount; ++symidx)
5067 if (sym_hash[symidx] == hi)
5068 {
5069 sym_hash[symidx] = h;
5070 break;
5071 }
5072 }
5073 free (shortname);
5074 }
5075 free (nondeflt_vers);
5076 nondeflt_vers = NULL;
5077 }
5078
4ad4eba5
AM
5079 /* Now set the weakdefs field correctly for all the weak defined
5080 symbols we found. The only way to do this is to search all the
5081 symbols. Since we only need the information for non functions in
5082 dynamic objects, that's the only time we actually put anything on
5083 the list WEAKS. We need this information so that if a regular
5084 object refers to a symbol defined weakly in a dynamic object, the
5085 real symbol in the dynamic object is also put in the dynamic
5086 symbols; we also must arrange for both symbols to point to the
5087 same memory location. We could handle the general case of symbol
5088 aliasing, but a general symbol alias can only be generated in
5089 assembler code, handling it correctly would be very time
5090 consuming, and other ELF linkers don't handle general aliasing
5091 either. */
5092 if (weaks != NULL)
5093 {
5094 struct elf_link_hash_entry **hpp;
5095 struct elf_link_hash_entry **hppend;
5096 struct elf_link_hash_entry **sorted_sym_hash;
5097 struct elf_link_hash_entry *h;
5098 size_t sym_count;
5099
5100 /* Since we have to search the whole symbol list for each weak
5101 defined symbol, search time for N weak defined symbols will be
5102 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5103 amt = extsymcount;
5104 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5105 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5106 if (sorted_sym_hash == NULL)
5107 goto error_return;
5108 sym_hash = sorted_sym_hash;
5109 hpp = elf_sym_hashes (abfd);
5110 hppend = hpp + extsymcount;
5111 sym_count = 0;
5112 for (; hpp < hppend; hpp++)
5113 {
5114 h = *hpp;
5115 if (h != NULL
5116 && h->root.type == bfd_link_hash_defined
fcb93ecf 5117 && !bed->is_function_type (h->type))
4ad4eba5
AM
5118 {
5119 *sym_hash = h;
5120 sym_hash++;
5121 sym_count++;
5122 }
5123 }
5124
5125 qsort (sorted_sym_hash, sym_count,
5126 sizeof (struct elf_link_hash_entry *),
5127 elf_sort_symbol);
5128
5129 while (weaks != NULL)
5130 {
5131 struct elf_link_hash_entry *hlook;
5132 asection *slook;
5133 bfd_vma vlook;
ed54588d 5134 size_t i, j, idx = 0;
4ad4eba5
AM
5135
5136 hlook = weaks;
f6e332e6
AM
5137 weaks = hlook->u.weakdef;
5138 hlook->u.weakdef = NULL;
4ad4eba5
AM
5139
5140 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5141 || hlook->root.type == bfd_link_hash_defweak
5142 || hlook->root.type == bfd_link_hash_common
5143 || hlook->root.type == bfd_link_hash_indirect);
5144 slook = hlook->root.u.def.section;
5145 vlook = hlook->root.u.def.value;
5146
4ad4eba5
AM
5147 i = 0;
5148 j = sym_count;
14160578 5149 while (i != j)
4ad4eba5
AM
5150 {
5151 bfd_signed_vma vdiff;
5152 idx = (i + j) / 2;
14160578 5153 h = sorted_sym_hash[idx];
4ad4eba5
AM
5154 vdiff = vlook - h->root.u.def.value;
5155 if (vdiff < 0)
5156 j = idx;
5157 else if (vdiff > 0)
5158 i = idx + 1;
5159 else
5160 {
d3435ae8 5161 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5162 if (sdiff < 0)
5163 j = idx;
5164 else if (sdiff > 0)
5165 i = idx + 1;
5166 else
14160578 5167 break;
4ad4eba5
AM
5168 }
5169 }
5170
5171 /* We didn't find a value/section match. */
14160578 5172 if (i == j)
4ad4eba5
AM
5173 continue;
5174
14160578
AM
5175 /* With multiple aliases, or when the weak symbol is already
5176 strongly defined, we have multiple matching symbols and
5177 the binary search above may land on any of them. Step
5178 one past the matching symbol(s). */
5179 while (++idx != j)
5180 {
5181 h = sorted_sym_hash[idx];
5182 if (h->root.u.def.section != slook
5183 || h->root.u.def.value != vlook)
5184 break;
5185 }
5186
5187 /* Now look back over the aliases. Since we sorted by size
5188 as well as value and section, we'll choose the one with
5189 the largest size. */
5190 while (idx-- != i)
4ad4eba5 5191 {
14160578 5192 h = sorted_sym_hash[idx];
4ad4eba5
AM
5193
5194 /* Stop if value or section doesn't match. */
14160578
AM
5195 if (h->root.u.def.section != slook
5196 || h->root.u.def.value != vlook)
4ad4eba5
AM
5197 break;
5198 else if (h != hlook)
5199 {
f6e332e6 5200 hlook->u.weakdef = h;
4ad4eba5
AM
5201
5202 /* If the weak definition is in the list of dynamic
5203 symbols, make sure the real definition is put
5204 there as well. */
5205 if (hlook->dynindx != -1 && h->dynindx == -1)
5206 {
c152c796 5207 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5208 {
5209 err_free_sym_hash:
5210 free (sorted_sym_hash);
5211 goto error_return;
5212 }
4ad4eba5
AM
5213 }
5214
5215 /* If the real definition is in the list of dynamic
5216 symbols, make sure the weak definition is put
5217 there as well. If we don't do this, then the
5218 dynamic loader might not merge the entries for the
5219 real definition and the weak definition. */
5220 if (h->dynindx != -1 && hlook->dynindx == -1)
5221 {
c152c796 5222 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5223 goto err_free_sym_hash;
4ad4eba5
AM
5224 }
5225 break;
5226 }
5227 }
5228 }
5229
5230 free (sorted_sym_hash);
5231 }
5232
33177bb1
AM
5233 if (bed->check_directives
5234 && !(*bed->check_directives) (abfd, info))
5235 return FALSE;
85fbca6a 5236
d9689752
L
5237 if (!info->check_relocs_after_open_input
5238 && !_bfd_elf_link_check_relocs (abfd, info))
5239 return FALSE;
4ad4eba5
AM
5240
5241 /* If this is a non-traditional link, try to optimize the handling
5242 of the .stab/.stabstr sections. */
5243 if (! dynamic
5244 && ! info->traditional_format
66eb6687 5245 && is_elf_hash_table (htab)
4ad4eba5
AM
5246 && (info->strip != strip_all && info->strip != strip_debugger))
5247 {
5248 asection *stabstr;
5249
5250 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5251 if (stabstr != NULL)
5252 {
5253 bfd_size_type string_offset = 0;
5254 asection *stab;
5255
5256 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5257 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5258 && (!stab->name[5] ||
5259 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5260 && (stab->flags & SEC_MERGE) == 0
5261 && !bfd_is_abs_section (stab->output_section))
5262 {
5263 struct bfd_elf_section_data *secdata;
5264
5265 secdata = elf_section_data (stab);
66eb6687
AM
5266 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5267 stabstr, &secdata->sec_info,
4ad4eba5
AM
5268 &string_offset))
5269 goto error_return;
5270 if (secdata->sec_info)
dbaa2011 5271 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5272 }
5273 }
5274 }
5275
66eb6687 5276 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5277 {
5278 /* Add this bfd to the loaded list. */
5279 struct elf_link_loaded_list *n;
5280
ca4be51c 5281 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5282 if (n == NULL)
5283 goto error_return;
5284 n->abfd = abfd;
66eb6687
AM
5285 n->next = htab->loaded;
5286 htab->loaded = n;
4ad4eba5
AM
5287 }
5288
5289 return TRUE;
5290
5291 error_free_vers:
66eb6687
AM
5292 if (old_tab != NULL)
5293 free (old_tab);
5b677558
AM
5294 if (old_strtab != NULL)
5295 free (old_strtab);
4ad4eba5
AM
5296 if (nondeflt_vers != NULL)
5297 free (nondeflt_vers);
5298 if (extversym != NULL)
5299 free (extversym);
5300 error_free_sym:
5301 if (isymbuf != NULL)
5302 free (isymbuf);
5303 error_return:
5304 return FALSE;
5305}
5306
8387904d
AM
5307/* Return the linker hash table entry of a symbol that might be
5308 satisfied by an archive symbol. Return -1 on error. */
5309
5310struct elf_link_hash_entry *
5311_bfd_elf_archive_symbol_lookup (bfd *abfd,
5312 struct bfd_link_info *info,
5313 const char *name)
5314{
5315 struct elf_link_hash_entry *h;
5316 char *p, *copy;
5317 size_t len, first;
5318
2a41f396 5319 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5320 if (h != NULL)
5321 return h;
5322
5323 /* If this is a default version (the name contains @@), look up the
5324 symbol again with only one `@' as well as without the version.
5325 The effect is that references to the symbol with and without the
5326 version will be matched by the default symbol in the archive. */
5327
5328 p = strchr (name, ELF_VER_CHR);
5329 if (p == NULL || p[1] != ELF_VER_CHR)
5330 return h;
5331
5332 /* First check with only one `@'. */
5333 len = strlen (name);
a50b1753 5334 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5335 if (copy == NULL)
5336 return (struct elf_link_hash_entry *) 0 - 1;
5337
5338 first = p - name + 1;
5339 memcpy (copy, name, first);
5340 memcpy (copy + first, name + first + 1, len - first);
5341
2a41f396 5342 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5343 if (h == NULL)
5344 {
5345 /* We also need to check references to the symbol without the
5346 version. */
5347 copy[first - 1] = '\0';
5348 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5349 FALSE, FALSE, TRUE);
8387904d
AM
5350 }
5351
5352 bfd_release (abfd, copy);
5353 return h;
5354}
5355
0ad989f9 5356/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5357 don't use _bfd_generic_link_add_archive_symbols because we need to
5358 handle versioned symbols.
0ad989f9
L
5359
5360 Fortunately, ELF archive handling is simpler than that done by
5361 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5362 oddities. In ELF, if we find a symbol in the archive map, and the
5363 symbol is currently undefined, we know that we must pull in that
5364 object file.
5365
5366 Unfortunately, we do have to make multiple passes over the symbol
5367 table until nothing further is resolved. */
5368
4ad4eba5
AM
5369static bfd_boolean
5370elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5371{
5372 symindex c;
13e570f8 5373 unsigned char *included = NULL;
0ad989f9
L
5374 carsym *symdefs;
5375 bfd_boolean loop;
5376 bfd_size_type amt;
8387904d
AM
5377 const struct elf_backend_data *bed;
5378 struct elf_link_hash_entry * (*archive_symbol_lookup)
5379 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5380
5381 if (! bfd_has_map (abfd))
5382 {
5383 /* An empty archive is a special case. */
5384 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5385 return TRUE;
5386 bfd_set_error (bfd_error_no_armap);
5387 return FALSE;
5388 }
5389
5390 /* Keep track of all symbols we know to be already defined, and all
5391 files we know to be already included. This is to speed up the
5392 second and subsequent passes. */
5393 c = bfd_ardata (abfd)->symdef_count;
5394 if (c == 0)
5395 return TRUE;
5396 amt = c;
13e570f8
AM
5397 amt *= sizeof (*included);
5398 included = (unsigned char *) bfd_zmalloc (amt);
5399 if (included == NULL)
5400 return FALSE;
0ad989f9
L
5401
5402 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5403 bed = get_elf_backend_data (abfd);
5404 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5405
5406 do
5407 {
5408 file_ptr last;
5409 symindex i;
5410 carsym *symdef;
5411 carsym *symdefend;
5412
5413 loop = FALSE;
5414 last = -1;
5415
5416 symdef = symdefs;
5417 symdefend = symdef + c;
5418 for (i = 0; symdef < symdefend; symdef++, i++)
5419 {
5420 struct elf_link_hash_entry *h;
5421 bfd *element;
5422 struct bfd_link_hash_entry *undefs_tail;
5423 symindex mark;
5424
13e570f8 5425 if (included[i])
0ad989f9
L
5426 continue;
5427 if (symdef->file_offset == last)
5428 {
5429 included[i] = TRUE;
5430 continue;
5431 }
5432
8387904d
AM
5433 h = archive_symbol_lookup (abfd, info, symdef->name);
5434 if (h == (struct elf_link_hash_entry *) 0 - 1)
5435 goto error_return;
0ad989f9
L
5436
5437 if (h == NULL)
5438 continue;
5439
5440 if (h->root.type == bfd_link_hash_common)
5441 {
5442 /* We currently have a common symbol. The archive map contains
5443 a reference to this symbol, so we may want to include it. We
5444 only want to include it however, if this archive element
5445 contains a definition of the symbol, not just another common
5446 declaration of it.
5447
5448 Unfortunately some archivers (including GNU ar) will put
5449 declarations of common symbols into their archive maps, as
5450 well as real definitions, so we cannot just go by the archive
5451 map alone. Instead we must read in the element's symbol
5452 table and check that to see what kind of symbol definition
5453 this is. */
5454 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5455 continue;
5456 }
5457 else if (h->root.type != bfd_link_hash_undefined)
5458 {
5459 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5460 /* Symbol must be defined. Don't check it again. */
5461 included[i] = TRUE;
0ad989f9
L
5462 continue;
5463 }
5464
5465 /* We need to include this archive member. */
5466 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5467 if (element == NULL)
5468 goto error_return;
5469
5470 if (! bfd_check_format (element, bfd_object))
5471 goto error_return;
5472
0ad989f9
L
5473 undefs_tail = info->hash->undefs_tail;
5474
0e144ba7
AM
5475 if (!(*info->callbacks
5476 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5477 continue;
0e144ba7 5478 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5479 goto error_return;
5480
5481 /* If there are any new undefined symbols, we need to make
5482 another pass through the archive in order to see whether
5483 they can be defined. FIXME: This isn't perfect, because
5484 common symbols wind up on undefs_tail and because an
5485 undefined symbol which is defined later on in this pass
5486 does not require another pass. This isn't a bug, but it
5487 does make the code less efficient than it could be. */
5488 if (undefs_tail != info->hash->undefs_tail)
5489 loop = TRUE;
5490
5491 /* Look backward to mark all symbols from this object file
5492 which we have already seen in this pass. */
5493 mark = i;
5494 do
5495 {
5496 included[mark] = TRUE;
5497 if (mark == 0)
5498 break;
5499 --mark;
5500 }
5501 while (symdefs[mark].file_offset == symdef->file_offset);
5502
5503 /* We mark subsequent symbols from this object file as we go
5504 on through the loop. */
5505 last = symdef->file_offset;
5506 }
5507 }
5508 while (loop);
5509
0ad989f9
L
5510 free (included);
5511
5512 return TRUE;
5513
5514 error_return:
0ad989f9
L
5515 if (included != NULL)
5516 free (included);
5517 return FALSE;
5518}
4ad4eba5
AM
5519
5520/* Given an ELF BFD, add symbols to the global hash table as
5521 appropriate. */
5522
5523bfd_boolean
5524bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5525{
5526 switch (bfd_get_format (abfd))
5527 {
5528 case bfd_object:
5529 return elf_link_add_object_symbols (abfd, info);
5530 case bfd_archive:
5531 return elf_link_add_archive_symbols (abfd, info);
5532 default:
5533 bfd_set_error (bfd_error_wrong_format);
5534 return FALSE;
5535 }
5536}
5a580b3a 5537\f
14b1c01e
AM
5538struct hash_codes_info
5539{
5540 unsigned long *hashcodes;
5541 bfd_boolean error;
5542};
a0c8462f 5543
5a580b3a
AM
5544/* This function will be called though elf_link_hash_traverse to store
5545 all hash value of the exported symbols in an array. */
5546
5547static bfd_boolean
5548elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5549{
a50b1753 5550 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5551 const char *name;
5a580b3a
AM
5552 unsigned long ha;
5553 char *alc = NULL;
5554
5a580b3a
AM
5555 /* Ignore indirect symbols. These are added by the versioning code. */
5556 if (h->dynindx == -1)
5557 return TRUE;
5558
5559 name = h->root.root.string;
422f1182 5560 if (h->versioned >= versioned)
5a580b3a 5561 {
422f1182
L
5562 char *p = strchr (name, ELF_VER_CHR);
5563 if (p != NULL)
14b1c01e 5564 {
422f1182
L
5565 alc = (char *) bfd_malloc (p - name + 1);
5566 if (alc == NULL)
5567 {
5568 inf->error = TRUE;
5569 return FALSE;
5570 }
5571 memcpy (alc, name, p - name);
5572 alc[p - name] = '\0';
5573 name = alc;
14b1c01e 5574 }
5a580b3a
AM
5575 }
5576
5577 /* Compute the hash value. */
5578 ha = bfd_elf_hash (name);
5579
5580 /* Store the found hash value in the array given as the argument. */
14b1c01e 5581 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5582
5583 /* And store it in the struct so that we can put it in the hash table
5584 later. */
f6e332e6 5585 h->u.elf_hash_value = ha;
5a580b3a
AM
5586
5587 if (alc != NULL)
5588 free (alc);
5589
5590 return TRUE;
5591}
5592
fdc90cb4
JJ
5593struct collect_gnu_hash_codes
5594{
5595 bfd *output_bfd;
5596 const struct elf_backend_data *bed;
5597 unsigned long int nsyms;
5598 unsigned long int maskbits;
5599 unsigned long int *hashcodes;
5600 unsigned long int *hashval;
5601 unsigned long int *indx;
5602 unsigned long int *counts;
5603 bfd_vma *bitmask;
5604 bfd_byte *contents;
5605 long int min_dynindx;
5606 unsigned long int bucketcount;
5607 unsigned long int symindx;
5608 long int local_indx;
5609 long int shift1, shift2;
5610 unsigned long int mask;
14b1c01e 5611 bfd_boolean error;
fdc90cb4
JJ
5612};
5613
5614/* This function will be called though elf_link_hash_traverse to store
5615 all hash value of the exported symbols in an array. */
5616
5617static bfd_boolean
5618elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5619{
a50b1753 5620 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5621 const char *name;
fdc90cb4
JJ
5622 unsigned long ha;
5623 char *alc = NULL;
5624
fdc90cb4
JJ
5625 /* Ignore indirect symbols. These are added by the versioning code. */
5626 if (h->dynindx == -1)
5627 return TRUE;
5628
5629 /* Ignore also local symbols and undefined symbols. */
5630 if (! (*s->bed->elf_hash_symbol) (h))
5631 return TRUE;
5632
5633 name = h->root.root.string;
422f1182 5634 if (h->versioned >= versioned)
fdc90cb4 5635 {
422f1182
L
5636 char *p = strchr (name, ELF_VER_CHR);
5637 if (p != NULL)
14b1c01e 5638 {
422f1182
L
5639 alc = (char *) bfd_malloc (p - name + 1);
5640 if (alc == NULL)
5641 {
5642 s->error = TRUE;
5643 return FALSE;
5644 }
5645 memcpy (alc, name, p - name);
5646 alc[p - name] = '\0';
5647 name = alc;
14b1c01e 5648 }
fdc90cb4
JJ
5649 }
5650
5651 /* Compute the hash value. */
5652 ha = bfd_elf_gnu_hash (name);
5653
5654 /* Store the found hash value in the array for compute_bucket_count,
5655 and also for .dynsym reordering purposes. */
5656 s->hashcodes[s->nsyms] = ha;
5657 s->hashval[h->dynindx] = ha;
5658 ++s->nsyms;
5659 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5660 s->min_dynindx = h->dynindx;
5661
5662 if (alc != NULL)
5663 free (alc);
5664
5665 return TRUE;
5666}
5667
5668/* This function will be called though elf_link_hash_traverse to do
5669 final dynaminc symbol renumbering. */
5670
5671static bfd_boolean
5672elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5673{
a50b1753 5674 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5675 unsigned long int bucket;
5676 unsigned long int val;
5677
fdc90cb4
JJ
5678 /* Ignore indirect symbols. */
5679 if (h->dynindx == -1)
5680 return TRUE;
5681
5682 /* Ignore also local symbols and undefined symbols. */
5683 if (! (*s->bed->elf_hash_symbol) (h))
5684 {
5685 if (h->dynindx >= s->min_dynindx)
5686 h->dynindx = s->local_indx++;
5687 return TRUE;
5688 }
5689
5690 bucket = s->hashval[h->dynindx] % s->bucketcount;
5691 val = (s->hashval[h->dynindx] >> s->shift1)
5692 & ((s->maskbits >> s->shift1) - 1);
5693 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5694 s->bitmask[val]
5695 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5696 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5697 if (s->counts[bucket] == 1)
5698 /* Last element terminates the chain. */
5699 val |= 1;
5700 bfd_put_32 (s->output_bfd, val,
5701 s->contents + (s->indx[bucket] - s->symindx) * 4);
5702 --s->counts[bucket];
5703 h->dynindx = s->indx[bucket]++;
5704 return TRUE;
5705}
5706
5707/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5708
5709bfd_boolean
5710_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5711{
5712 return !(h->forced_local
5713 || h->root.type == bfd_link_hash_undefined
5714 || h->root.type == bfd_link_hash_undefweak
5715 || ((h->root.type == bfd_link_hash_defined
5716 || h->root.type == bfd_link_hash_defweak)
5717 && h->root.u.def.section->output_section == NULL));
5718}
5719
5a580b3a
AM
5720/* Array used to determine the number of hash table buckets to use
5721 based on the number of symbols there are. If there are fewer than
5722 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5723 fewer than 37 we use 17 buckets, and so forth. We never use more
5724 than 32771 buckets. */
5725
5726static const size_t elf_buckets[] =
5727{
5728 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5729 16411, 32771, 0
5730};
5731
5732/* Compute bucket count for hashing table. We do not use a static set
5733 of possible tables sizes anymore. Instead we determine for all
5734 possible reasonable sizes of the table the outcome (i.e., the
5735 number of collisions etc) and choose the best solution. The
5736 weighting functions are not too simple to allow the table to grow
5737 without bounds. Instead one of the weighting factors is the size.
5738 Therefore the result is always a good payoff between few collisions
5739 (= short chain lengths) and table size. */
5740static size_t
b20dd2ce 5741compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5742 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5743 unsigned long int nsyms,
5744 int gnu_hash)
5a580b3a 5745{
5a580b3a 5746 size_t best_size = 0;
5a580b3a 5747 unsigned long int i;
5a580b3a 5748
5a580b3a
AM
5749 /* We have a problem here. The following code to optimize the table
5750 size requires an integer type with more the 32 bits. If
5751 BFD_HOST_U_64_BIT is set we know about such a type. */
5752#ifdef BFD_HOST_U_64_BIT
5753 if (info->optimize)
5754 {
5a580b3a
AM
5755 size_t minsize;
5756 size_t maxsize;
5757 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5758 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5759 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5760 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5761 unsigned long int *counts;
d40f3da9 5762 bfd_size_type amt;
0883b6e0 5763 unsigned int no_improvement_count = 0;
5a580b3a
AM
5764
5765 /* Possible optimization parameters: if we have NSYMS symbols we say
5766 that the hashing table must at least have NSYMS/4 and at most
5767 2*NSYMS buckets. */
5768 minsize = nsyms / 4;
5769 if (minsize == 0)
5770 minsize = 1;
5771 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5772 if (gnu_hash)
5773 {
5774 if (minsize < 2)
5775 minsize = 2;
5776 if ((best_size & 31) == 0)
5777 ++best_size;
5778 }
5a580b3a
AM
5779
5780 /* Create array where we count the collisions in. We must use bfd_malloc
5781 since the size could be large. */
5782 amt = maxsize;
5783 amt *= sizeof (unsigned long int);
a50b1753 5784 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5785 if (counts == NULL)
fdc90cb4 5786 return 0;
5a580b3a
AM
5787
5788 /* Compute the "optimal" size for the hash table. The criteria is a
5789 minimal chain length. The minor criteria is (of course) the size
5790 of the table. */
5791 for (i = minsize; i < maxsize; ++i)
5792 {
5793 /* Walk through the array of hashcodes and count the collisions. */
5794 BFD_HOST_U_64_BIT max;
5795 unsigned long int j;
5796 unsigned long int fact;
5797
fdc90cb4
JJ
5798 if (gnu_hash && (i & 31) == 0)
5799 continue;
5800
5a580b3a
AM
5801 memset (counts, '\0', i * sizeof (unsigned long int));
5802
5803 /* Determine how often each hash bucket is used. */
5804 for (j = 0; j < nsyms; ++j)
5805 ++counts[hashcodes[j] % i];
5806
5807 /* For the weight function we need some information about the
5808 pagesize on the target. This is information need not be 100%
5809 accurate. Since this information is not available (so far) we
5810 define it here to a reasonable default value. If it is crucial
5811 to have a better value some day simply define this value. */
5812# ifndef BFD_TARGET_PAGESIZE
5813# define BFD_TARGET_PAGESIZE (4096)
5814# endif
5815
fdc90cb4
JJ
5816 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5817 and the chains. */
5818 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5819
5820# if 1
5821 /* Variant 1: optimize for short chains. We add the squares
5822 of all the chain lengths (which favors many small chain
5823 over a few long chains). */
5824 for (j = 0; j < i; ++j)
5825 max += counts[j] * counts[j];
5826
5827 /* This adds penalties for the overall size of the table. */
fdc90cb4 5828 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5829 max *= fact * fact;
5830# else
5831 /* Variant 2: Optimize a lot more for small table. Here we
5832 also add squares of the size but we also add penalties for
5833 empty slots (the +1 term). */
5834 for (j = 0; j < i; ++j)
5835 max += (1 + counts[j]) * (1 + counts[j]);
5836
5837 /* The overall size of the table is considered, but not as
5838 strong as in variant 1, where it is squared. */
fdc90cb4 5839 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5840 max *= fact;
5841# endif
5842
5843 /* Compare with current best results. */
5844 if (max < best_chlen)
5845 {
5846 best_chlen = max;
5847 best_size = i;
ca4be51c 5848 no_improvement_count = 0;
5a580b3a 5849 }
0883b6e0
NC
5850 /* PR 11843: Avoid futile long searches for the best bucket size
5851 when there are a large number of symbols. */
5852 else if (++no_improvement_count == 100)
5853 break;
5a580b3a
AM
5854 }
5855
5856 free (counts);
5857 }
5858 else
5859#endif /* defined (BFD_HOST_U_64_BIT) */
5860 {
5861 /* This is the fallback solution if no 64bit type is available or if we
5862 are not supposed to spend much time on optimizations. We select the
5863 bucket count using a fixed set of numbers. */
5864 for (i = 0; elf_buckets[i] != 0; i++)
5865 {
5866 best_size = elf_buckets[i];
fdc90cb4 5867 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5868 break;
5869 }
fdc90cb4
JJ
5870 if (gnu_hash && best_size < 2)
5871 best_size = 2;
5a580b3a
AM
5872 }
5873
5a580b3a
AM
5874 return best_size;
5875}
5876
d0bf826b
AM
5877/* Size any SHT_GROUP section for ld -r. */
5878
5879bfd_boolean
5880_bfd_elf_size_group_sections (struct bfd_link_info *info)
5881{
5882 bfd *ibfd;
57963c05 5883 asection *s;
d0bf826b 5884
c72f2fb2 5885 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 5886 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
5887 && (s = ibfd->sections) != NULL
5888 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
5889 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5890 return FALSE;
5891 return TRUE;
5892}
5893
04c3a755
NS
5894/* Set a default stack segment size. The value in INFO wins. If it
5895 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5896 undefined it is initialized. */
5897
5898bfd_boolean
5899bfd_elf_stack_segment_size (bfd *output_bfd,
5900 struct bfd_link_info *info,
5901 const char *legacy_symbol,
5902 bfd_vma default_size)
5903{
5904 struct elf_link_hash_entry *h = NULL;
5905
5906 /* Look for legacy symbol. */
5907 if (legacy_symbol)
5908 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5909 FALSE, FALSE, FALSE);
5910 if (h && (h->root.type == bfd_link_hash_defined
5911 || h->root.type == bfd_link_hash_defweak)
5912 && h->def_regular
5913 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5914 {
5915 /* The symbol has no type if specified on the command line. */
5916 h->type = STT_OBJECT;
5917 if (info->stacksize)
695344c0 5918 /* xgettext:c-format */
4eca0228
AM
5919 _bfd_error_handler (_("%B: stack size specified and %s set"),
5920 output_bfd, legacy_symbol);
04c3a755 5921 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5922 /* xgettext:c-format */
4eca0228
AM
5923 _bfd_error_handler (_("%B: %s not absolute"),
5924 output_bfd, legacy_symbol);
04c3a755
NS
5925 else
5926 info->stacksize = h->root.u.def.value;
5927 }
5928
5929 if (!info->stacksize)
5930 /* If the user didn't set a size, or explicitly inhibit the
5931 size, set it now. */
5932 info->stacksize = default_size;
5933
5934 /* Provide the legacy symbol, if it is referenced. */
5935 if (h && (h->root.type == bfd_link_hash_undefined
5936 || h->root.type == bfd_link_hash_undefweak))
5937 {
5938 struct bfd_link_hash_entry *bh = NULL;
5939
5940 if (!(_bfd_generic_link_add_one_symbol
5941 (info, output_bfd, legacy_symbol,
5942 BSF_GLOBAL, bfd_abs_section_ptr,
5943 info->stacksize >= 0 ? info->stacksize : 0,
5944 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5945 return FALSE;
5946
5947 h = (struct elf_link_hash_entry *) bh;
5948 h->def_regular = 1;
5949 h->type = STT_OBJECT;
5950 }
5951
5952 return TRUE;
5953}
5954
b531344c
MR
5955/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5956
5957struct elf_gc_sweep_symbol_info
5958{
5959 struct bfd_link_info *info;
5960 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5961 bfd_boolean);
5962};
5963
5964static bfd_boolean
5965elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5966{
5967 if (!h->mark
5968 && (((h->root.type == bfd_link_hash_defined
5969 || h->root.type == bfd_link_hash_defweak)
5970 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5971 && h->root.u.def.section->gc_mark))
5972 || h->root.type == bfd_link_hash_undefined
5973 || h->root.type == bfd_link_hash_undefweak))
5974 {
5975 struct elf_gc_sweep_symbol_info *inf;
5976
5977 inf = (struct elf_gc_sweep_symbol_info *) data;
5978 (*inf->hide_symbol) (inf->info, h, TRUE);
5979 h->def_regular = 0;
5980 h->ref_regular = 0;
5981 h->ref_regular_nonweak = 0;
5982 }
5983
5984 return TRUE;
5985}
5986
5a580b3a
AM
5987/* Set up the sizes and contents of the ELF dynamic sections. This is
5988 called by the ELF linker emulation before_allocation routine. We
5989 must set the sizes of the sections before the linker sets the
5990 addresses of the various sections. */
5991
5992bfd_boolean
5993bfd_elf_size_dynamic_sections (bfd *output_bfd,
5994 const char *soname,
5995 const char *rpath,
5996 const char *filter_shlib,
7ee314fa
AM
5997 const char *audit,
5998 const char *depaudit,
5a580b3a
AM
5999 const char * const *auxiliary_filters,
6000 struct bfd_link_info *info,
fd91d419 6001 asection **sinterpptr)
5a580b3a 6002{
5a580b3a
AM
6003 bfd *dynobj;
6004 const struct elf_backend_data *bed;
5a580b3a
AM
6005
6006 *sinterpptr = NULL;
6007
5a580b3a
AM
6008 if (!is_elf_hash_table (info->hash))
6009 return TRUE;
6010
5a580b3a
AM
6011 dynobj = elf_hash_table (info)->dynobj;
6012
9a2a56cc 6013 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6014 {
902e9fc7
MR
6015 struct bfd_elf_version_tree *verdefs;
6016 struct elf_info_failed asvinfo;
5a580b3a
AM
6017 struct bfd_elf_version_tree *t;
6018 struct bfd_elf_version_expr *d;
902e9fc7 6019 asection *s;
e6699019 6020 size_t soname_indx;
7ee314fa 6021
5a580b3a
AM
6022 /* If we are supposed to export all symbols into the dynamic symbol
6023 table (this is not the normal case), then do so. */
55255dae 6024 if (info->export_dynamic
0e1862bb 6025 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6026 {
3d13f3e9
AM
6027 struct elf_info_failed eif;
6028
6029 eif.info = info;
6030 eif.failed = FALSE;
5a580b3a
AM
6031 elf_link_hash_traverse (elf_hash_table (info),
6032 _bfd_elf_export_symbol,
6033 &eif);
6034 if (eif.failed)
6035 return FALSE;
6036 }
6037
e6699019
L
6038 if (soname != NULL)
6039 {
6040 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6041 soname, TRUE);
6042 if (soname_indx == (size_t) -1
6043 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6044 return FALSE;
6045 }
6046 else
6047 soname_indx = (size_t) -1;
6048
5a580b3a 6049 /* Make all global versions with definition. */
fd91d419 6050 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6051 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6052 if (!d->symver && d->literal)
5a580b3a
AM
6053 {
6054 const char *verstr, *name;
6055 size_t namelen, verlen, newlen;
93252b1c 6056 char *newname, *p, leading_char;
5a580b3a
AM
6057 struct elf_link_hash_entry *newh;
6058
93252b1c 6059 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6060 name = d->pattern;
93252b1c 6061 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6062 verstr = t->name;
6063 verlen = strlen (verstr);
6064 newlen = namelen + verlen + 3;
6065
a50b1753 6066 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6067 if (newname == NULL)
6068 return FALSE;
93252b1c
MF
6069 newname[0] = leading_char;
6070 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6071
6072 /* Check the hidden versioned definition. */
6073 p = newname + namelen;
6074 *p++ = ELF_VER_CHR;
6075 memcpy (p, verstr, verlen + 1);
6076 newh = elf_link_hash_lookup (elf_hash_table (info),
6077 newname, FALSE, FALSE,
6078 FALSE);
6079 if (newh == NULL
6080 || (newh->root.type != bfd_link_hash_defined
6081 && newh->root.type != bfd_link_hash_defweak))
6082 {
6083 /* Check the default versioned definition. */
6084 *p++ = ELF_VER_CHR;
6085 memcpy (p, verstr, verlen + 1);
6086 newh = elf_link_hash_lookup (elf_hash_table (info),
6087 newname, FALSE, FALSE,
6088 FALSE);
6089 }
6090 free (newname);
6091
6092 /* Mark this version if there is a definition and it is
6093 not defined in a shared object. */
6094 if (newh != NULL
f5385ebf 6095 && !newh->def_dynamic
5a580b3a
AM
6096 && (newh->root.type == bfd_link_hash_defined
6097 || newh->root.type == bfd_link_hash_defweak))
6098 d->symver = 1;
6099 }
6100
6101 /* Attach all the symbols to their version information. */
5a580b3a 6102 asvinfo.info = info;
5a580b3a
AM
6103 asvinfo.failed = FALSE;
6104
6105 elf_link_hash_traverse (elf_hash_table (info),
6106 _bfd_elf_link_assign_sym_version,
6107 &asvinfo);
6108 if (asvinfo.failed)
6109 return FALSE;
6110
6111 if (!info->allow_undefined_version)
6112 {
6113 /* Check if all global versions have a definition. */
3d13f3e9 6114 bfd_boolean all_defined = TRUE;
fd91d419 6115 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6116 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6117 if (d->literal && !d->symver && !d->script)
5a580b3a 6118 {
4eca0228 6119 _bfd_error_handler
5a580b3a
AM
6120 (_("%s: undefined version: %s"),
6121 d->pattern, t->name);
6122 all_defined = FALSE;
6123 }
6124
6125 if (!all_defined)
6126 {
6127 bfd_set_error (bfd_error_bad_value);
6128 return FALSE;
6129 }
6130 }
6131
902e9fc7
MR
6132 /* Set up the version definition section. */
6133 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6134 BFD_ASSERT (s != NULL);
5a580b3a 6135
902e9fc7
MR
6136 /* We may have created additional version definitions if we are
6137 just linking a regular application. */
6138 verdefs = info->version_info;
5a580b3a 6139
902e9fc7
MR
6140 /* Skip anonymous version tag. */
6141 if (verdefs != NULL && verdefs->vernum == 0)
6142 verdefs = verdefs->next;
5a580b3a 6143
902e9fc7
MR
6144 if (verdefs == NULL && !info->create_default_symver)
6145 s->flags |= SEC_EXCLUDE;
6146 else
5a580b3a 6147 {
902e9fc7
MR
6148 unsigned int cdefs;
6149 bfd_size_type size;
6150 bfd_byte *p;
6151 Elf_Internal_Verdef def;
6152 Elf_Internal_Verdaux defaux;
6153 struct bfd_link_hash_entry *bh;
6154 struct elf_link_hash_entry *h;
6155 const char *name;
5a580b3a 6156
902e9fc7
MR
6157 cdefs = 0;
6158 size = 0;
5a580b3a 6159
902e9fc7
MR
6160 /* Make space for the base version. */
6161 size += sizeof (Elf_External_Verdef);
6162 size += sizeof (Elf_External_Verdaux);
6163 ++cdefs;
6164
6165 /* Make space for the default version. */
6166 if (info->create_default_symver)
6167 {
6168 size += sizeof (Elf_External_Verdef);
6169 ++cdefs;
3e3b46e5
PB
6170 }
6171
5a580b3a
AM
6172 for (t = verdefs; t != NULL; t = t->next)
6173 {
6174 struct bfd_elf_version_deps *n;
6175
a6cc6b3b
RO
6176 /* Don't emit base version twice. */
6177 if (t->vernum == 0)
6178 continue;
6179
5a580b3a
AM
6180 size += sizeof (Elf_External_Verdef);
6181 size += sizeof (Elf_External_Verdaux);
6182 ++cdefs;
6183
6184 for (n = t->deps; n != NULL; n = n->next)
6185 size += sizeof (Elf_External_Verdaux);
6186 }
6187
eea6121a 6188 s->size = size;
a50b1753 6189 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6190 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6191 return FALSE;
6192
6193 /* Fill in the version definition section. */
6194
6195 p = s->contents;
6196
6197 def.vd_version = VER_DEF_CURRENT;
6198 def.vd_flags = VER_FLG_BASE;
6199 def.vd_ndx = 1;
6200 def.vd_cnt = 1;
3e3b46e5
PB
6201 if (info->create_default_symver)
6202 {
6203 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6204 def.vd_next = sizeof (Elf_External_Verdef);
6205 }
6206 else
6207 {
6208 def.vd_aux = sizeof (Elf_External_Verdef);
6209 def.vd_next = (sizeof (Elf_External_Verdef)
6210 + sizeof (Elf_External_Verdaux));
6211 }
5a580b3a 6212
ef53be89 6213 if (soname_indx != (size_t) -1)
5a580b3a
AM
6214 {
6215 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6216 soname_indx);
6217 def.vd_hash = bfd_elf_hash (soname);
6218 defaux.vda_name = soname_indx;
3e3b46e5 6219 name = soname;
5a580b3a
AM
6220 }
6221 else
6222 {
ef53be89 6223 size_t indx;
5a580b3a 6224
06084812 6225 name = lbasename (output_bfd->filename);
5a580b3a
AM
6226 def.vd_hash = bfd_elf_hash (name);
6227 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6228 name, FALSE);
ef53be89 6229 if (indx == (size_t) -1)
5a580b3a
AM
6230 return FALSE;
6231 defaux.vda_name = indx;
6232 }
6233 defaux.vda_next = 0;
6234
6235 _bfd_elf_swap_verdef_out (output_bfd, &def,
6236 (Elf_External_Verdef *) p);
6237 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6238 if (info->create_default_symver)
6239 {
6240 /* Add a symbol representing this version. */
6241 bh = NULL;
6242 if (! (_bfd_generic_link_add_one_symbol
6243 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6244 0, NULL, FALSE,
6245 get_elf_backend_data (dynobj)->collect, &bh)))
6246 return FALSE;
6247 h = (struct elf_link_hash_entry *) bh;
6248 h->non_elf = 0;
6249 h->def_regular = 1;
6250 h->type = STT_OBJECT;
6251 h->verinfo.vertree = NULL;
6252
6253 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6254 return FALSE;
6255
6256 /* Create a duplicate of the base version with the same
6257 aux block, but different flags. */
6258 def.vd_flags = 0;
6259 def.vd_ndx = 2;
6260 def.vd_aux = sizeof (Elf_External_Verdef);
6261 if (verdefs)
6262 def.vd_next = (sizeof (Elf_External_Verdef)
6263 + sizeof (Elf_External_Verdaux));
6264 else
6265 def.vd_next = 0;
6266 _bfd_elf_swap_verdef_out (output_bfd, &def,
6267 (Elf_External_Verdef *) p);
6268 p += sizeof (Elf_External_Verdef);
6269 }
5a580b3a
AM
6270 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6271 (Elf_External_Verdaux *) p);
6272 p += sizeof (Elf_External_Verdaux);
6273
6274 for (t = verdefs; t != NULL; t = t->next)
6275 {
6276 unsigned int cdeps;
6277 struct bfd_elf_version_deps *n;
5a580b3a 6278
a6cc6b3b
RO
6279 /* Don't emit the base version twice. */
6280 if (t->vernum == 0)
6281 continue;
6282
5a580b3a
AM
6283 cdeps = 0;
6284 for (n = t->deps; n != NULL; n = n->next)
6285 ++cdeps;
6286
6287 /* Add a symbol representing this version. */
6288 bh = NULL;
6289 if (! (_bfd_generic_link_add_one_symbol
6290 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6291 0, NULL, FALSE,
6292 get_elf_backend_data (dynobj)->collect, &bh)))
6293 return FALSE;
6294 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6295 h->non_elf = 0;
6296 h->def_regular = 1;
5a580b3a
AM
6297 h->type = STT_OBJECT;
6298 h->verinfo.vertree = t;
6299
c152c796 6300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6301 return FALSE;
6302
6303 def.vd_version = VER_DEF_CURRENT;
6304 def.vd_flags = 0;
6305 if (t->globals.list == NULL
6306 && t->locals.list == NULL
6307 && ! t->used)
6308 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6309 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6310 def.vd_cnt = cdeps + 1;
6311 def.vd_hash = bfd_elf_hash (t->name);
6312 def.vd_aux = sizeof (Elf_External_Verdef);
6313 def.vd_next = 0;
a6cc6b3b
RO
6314
6315 /* If a basever node is next, it *must* be the last node in
6316 the chain, otherwise Verdef construction breaks. */
6317 if (t->next != NULL && t->next->vernum == 0)
6318 BFD_ASSERT (t->next->next == NULL);
6319
6320 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6321 def.vd_next = (sizeof (Elf_External_Verdef)
6322 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6323
6324 _bfd_elf_swap_verdef_out (output_bfd, &def,
6325 (Elf_External_Verdef *) p);
6326 p += sizeof (Elf_External_Verdef);
6327
6328 defaux.vda_name = h->dynstr_index;
6329 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6330 h->dynstr_index);
6331 defaux.vda_next = 0;
6332 if (t->deps != NULL)
6333 defaux.vda_next = sizeof (Elf_External_Verdaux);
6334 t->name_indx = defaux.vda_name;
6335
6336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 (Elf_External_Verdaux *) p);
6338 p += sizeof (Elf_External_Verdaux);
6339
6340 for (n = t->deps; n != NULL; n = n->next)
6341 {
6342 if (n->version_needed == NULL)
6343 {
6344 /* This can happen if there was an error in the
6345 version script. */
6346 defaux.vda_name = 0;
6347 }
6348 else
6349 {
6350 defaux.vda_name = n->version_needed->name_indx;
6351 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6352 defaux.vda_name);
6353 }
6354 if (n->next == NULL)
6355 defaux.vda_next = 0;
6356 else
6357 defaux.vda_next = sizeof (Elf_External_Verdaux);
6358
6359 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6360 (Elf_External_Verdaux *) p);
6361 p += sizeof (Elf_External_Verdaux);
6362 }
6363 }
6364
5a580b3a
AM
6365 elf_tdata (output_bfd)->cverdefs = cdefs;
6366 }
902e9fc7
MR
6367 }
6368
6369 bed = get_elf_backend_data (output_bfd);
6370
6371 if (info->gc_sections && bed->can_gc_sections)
6372 {
6373 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6374
6375 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6376 dynamic symbol table. */
902e9fc7
MR
6377 sweep_info.info = info;
6378 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6379 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6380 &sweep_info);
3d13f3e9
AM
6381 }
6382
6383 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6384 {
6385 asection *s;
6386 struct elf_find_verdep_info sinfo;
6387
6388 /* Work out the size of the version reference section. */
6389
6390 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6391 BFD_ASSERT (s != NULL);
902e9fc7 6392
3d13f3e9
AM
6393 sinfo.info = info;
6394 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6395 if (sinfo.vers == 0)
6396 sinfo.vers = 1;
6397 sinfo.failed = FALSE;
6398
6399 elf_link_hash_traverse (elf_hash_table (info),
6400 _bfd_elf_link_find_version_dependencies,
6401 &sinfo);
6402 if (sinfo.failed)
6403 return FALSE;
6404
6405 if (elf_tdata (output_bfd)->verref == NULL)
6406 s->flags |= SEC_EXCLUDE;
6407 else
6408 {
6409 Elf_Internal_Verneed *vn;
6410 unsigned int size;
6411 unsigned int crefs;
6412 bfd_byte *p;
6413
6414 /* Build the version dependency section. */
6415 size = 0;
6416 crefs = 0;
6417 for (vn = elf_tdata (output_bfd)->verref;
6418 vn != NULL;
6419 vn = vn->vn_nextref)
6420 {
6421 Elf_Internal_Vernaux *a;
6422
6423 size += sizeof (Elf_External_Verneed);
6424 ++crefs;
6425 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6426 size += sizeof (Elf_External_Vernaux);
6427 }
6428
6429 s->size = size;
6430 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6431 if (s->contents == NULL)
6432 return FALSE;
6433
6434 p = s->contents;
6435 for (vn = elf_tdata (output_bfd)->verref;
6436 vn != NULL;
6437 vn = vn->vn_nextref)
6438 {
6439 unsigned int caux;
6440 Elf_Internal_Vernaux *a;
6441 size_t indx;
6442
6443 caux = 0;
6444 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6445 ++caux;
6446
6447 vn->vn_version = VER_NEED_CURRENT;
6448 vn->vn_cnt = caux;
6449 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6450 elf_dt_name (vn->vn_bfd) != NULL
6451 ? elf_dt_name (vn->vn_bfd)
6452 : lbasename (vn->vn_bfd->filename),
6453 FALSE);
6454 if (indx == (size_t) -1)
6455 return FALSE;
6456 vn->vn_file = indx;
6457 vn->vn_aux = sizeof (Elf_External_Verneed);
6458 if (vn->vn_nextref == NULL)
6459 vn->vn_next = 0;
6460 else
6461 vn->vn_next = (sizeof (Elf_External_Verneed)
6462 + caux * sizeof (Elf_External_Vernaux));
6463
6464 _bfd_elf_swap_verneed_out (output_bfd, vn,
6465 (Elf_External_Verneed *) p);
6466 p += sizeof (Elf_External_Verneed);
6467
6468 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6469 {
6470 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6471 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6472 a->vna_nodename, FALSE);
6473 if (indx == (size_t) -1)
6474 return FALSE;
6475 a->vna_name = indx;
6476 if (a->vna_nextptr == NULL)
6477 a->vna_next = 0;
6478 else
6479 a->vna_next = sizeof (Elf_External_Vernaux);
6480
6481 _bfd_elf_swap_vernaux_out (output_bfd, a,
6482 (Elf_External_Vernaux *) p);
6483 p += sizeof (Elf_External_Vernaux);
6484 }
6485 }
6486
6487 elf_tdata (output_bfd)->cverrefs = crefs;
6488 }
902e9fc7
MR
6489 }
6490
6491 /* Any syms created from now on start with -1 in
6492 got.refcount/offset and plt.refcount/offset. */
6493 elf_hash_table (info)->init_got_refcount
6494 = elf_hash_table (info)->init_got_offset;
6495 elf_hash_table (info)->init_plt_refcount
6496 = elf_hash_table (info)->init_plt_offset;
6497
6498 if (bfd_link_relocatable (info)
6499 && !_bfd_elf_size_group_sections (info))
6500 return FALSE;
6501
6502 /* The backend may have to create some sections regardless of whether
6503 we're dynamic or not. */
6504 if (bed->elf_backend_always_size_sections
6505 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6506 return FALSE;
6507
6508 /* Determine any GNU_STACK segment requirements, after the backend
6509 has had a chance to set a default segment size. */
6510 if (info->execstack)
6511 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6512 else if (info->noexecstack)
6513 elf_stack_flags (output_bfd) = PF_R | PF_W;
6514 else
6515 {
6516 bfd *inputobj;
6517 asection *notesec = NULL;
6518 int exec = 0;
6519
6520 for (inputobj = info->input_bfds;
6521 inputobj;
6522 inputobj = inputobj->link.next)
6523 {
6524 asection *s;
6525
6526 if (inputobj->flags
6527 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6528 continue;
57963c05
AM
6529 s = inputobj->sections;
6530 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6531 continue;
6532
902e9fc7
MR
6533 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6534 if (s)
6535 {
6536 if (s->flags & SEC_CODE)
6537 exec = PF_X;
6538 notesec = s;
6539 }
6540 else if (bed->default_execstack)
6541 exec = PF_X;
6542 }
6543 if (notesec || info->stacksize > 0)
6544 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6545 if (notesec && exec && bfd_link_relocatable (info)
6546 && notesec->output_section != bfd_abs_section_ptr)
6547 notesec->output_section->flags |= SEC_CODE;
6548 }
6549
6550 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6551 {
6552 struct elf_info_failed eif;
6553 struct elf_link_hash_entry *h;
6554 asection *dynstr;
6555 asection *s;
6556
6557 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6558 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6559
902e9fc7
MR
6560 if (info->symbolic)
6561 {
6562 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6563 return FALSE;
6564 info->flags |= DF_SYMBOLIC;
6565 }
6566
6567 if (rpath != NULL)
6568 {
6569 size_t indx;
6570 bfd_vma tag;
6571
6572 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6573 TRUE);
6574 if (indx == (size_t) -1)
6575 return FALSE;
6576
6577 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6578 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6579 return FALSE;
6580 }
6581
6582 if (filter_shlib != NULL)
6583 {
6584 size_t indx;
6585
6586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6587 filter_shlib, TRUE);
6588 if (indx == (size_t) -1
6589 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6590 return FALSE;
6591 }
6592
6593 if (auxiliary_filters != NULL)
6594 {
6595 const char * const *p;
6596
6597 for (p = auxiliary_filters; *p != NULL; p++)
6598 {
6599 size_t indx;
6600
6601 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6602 *p, TRUE);
6603 if (indx == (size_t) -1
6604 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6605 return FALSE;
6606 }
6607 }
6608
6609 if (audit != NULL)
6610 {
6611 size_t indx;
6612
6613 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6614 TRUE);
6615 if (indx == (size_t) -1
6616 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6617 return FALSE;
6618 }
6619
6620 if (depaudit != NULL)
6621 {
6622 size_t indx;
6623
6624 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6625 TRUE);
6626 if (indx == (size_t) -1
6627 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6628 return FALSE;
6629 }
6630
6631 eif.info = info;
6632 eif.failed = FALSE;
6633
6634 /* Find all symbols which were defined in a dynamic object and make
6635 the backend pick a reasonable value for them. */
6636 elf_link_hash_traverse (elf_hash_table (info),
6637 _bfd_elf_adjust_dynamic_symbol,
6638 &eif);
6639 if (eif.failed)
6640 return FALSE;
6641
6642 /* Add some entries to the .dynamic section. We fill in some of the
6643 values later, in bfd_elf_final_link, but we must add the entries
6644 now so that we know the final size of the .dynamic section. */
6645
6646 /* If there are initialization and/or finalization functions to
6647 call then add the corresponding DT_INIT/DT_FINI entries. */
6648 h = (info->init_function
6649 ? elf_link_hash_lookup (elf_hash_table (info),
6650 info->init_function, FALSE,
6651 FALSE, FALSE)
6652 : NULL);
6653 if (h != NULL
6654 && (h->ref_regular
6655 || h->def_regular))
6656 {
6657 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6658 return FALSE;
6659 }
6660 h = (info->fini_function
6661 ? elf_link_hash_lookup (elf_hash_table (info),
6662 info->fini_function, FALSE,
6663 FALSE, FALSE)
6664 : NULL);
6665 if (h != NULL
6666 && (h->ref_regular
6667 || h->def_regular))
6668 {
6669 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6670 return FALSE;
6671 }
6672
6673 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6674 if (s != NULL && s->linker_has_input)
6675 {
6676 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6677 if (! bfd_link_executable (info))
6678 {
6679 bfd *sub;
6680 asection *o;
6681
57963c05
AM
6682 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6683 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6684 && (o = sub->sections) != NULL
6685 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6686 for (o = sub->sections; o != NULL; o = o->next)
6687 if (elf_section_data (o)->this_hdr.sh_type
6688 == SHT_PREINIT_ARRAY)
6689 {
6690 _bfd_error_handler
6691 (_("%B: .preinit_array section is not allowed in DSO"),
6692 sub);
6693 break;
6694 }
6695
6696 bfd_set_error (bfd_error_nonrepresentable_section);
6697 return FALSE;
6698 }
6699
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6701 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6702 return FALSE;
6703 }
6704 s = bfd_get_section_by_name (output_bfd, ".init_array");
6705 if (s != NULL && s->linker_has_input)
6706 {
6707 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6708 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6709 return FALSE;
6710 }
6711 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6712 if (s != NULL && s->linker_has_input)
6713 {
6714 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6715 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6716 return FALSE;
6717 }
6718
6719 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6720 /* If .dynstr is excluded from the link, we don't want any of
6721 these tags. Strictly, we should be checking each section
6722 individually; This quick check covers for the case where
6723 someone does a /DISCARD/ : { *(*) }. */
6724 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6725 {
6726 bfd_size_type strsize;
6727
6728 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6729 if ((info->emit_hash
6730 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6731 || (info->emit_gnu_hash
6732 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6733 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6734 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6735 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6736 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6737 bed->s->sizeof_sym))
6738 return FALSE;
6739 }
6740 }
6741
6742 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6743 return FALSE;
6744
6745 /* The backend must work out the sizes of all the other dynamic
6746 sections. */
6747 if (dynobj != NULL
6748 && bed->elf_backend_size_dynamic_sections != NULL
6749 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6750 return FALSE;
6751
6752 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6753 {
6754 unsigned long section_sym_count;
6755
6756 if (elf_tdata (output_bfd)->cverdefs)
6757 {
6758 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6759
6760 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6761 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6762 return FALSE;
6763 }
6764
6765 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6766 {
6767 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6768 return FALSE;
6769 }
6770 else if (info->flags & DF_BIND_NOW)
6771 {
6772 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6773 return FALSE;
6774 }
6775
6776 if (info->flags_1)
6777 {
6778 if (bfd_link_executable (info))
6779 info->flags_1 &= ~ (DF_1_INITFIRST
6780 | DF_1_NODELETE
6781 | DF_1_NOOPEN);
6782 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6783 return FALSE;
6784 }
6785
6786 if (elf_tdata (output_bfd)->cverrefs)
6787 {
6788 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6789
6790 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6791 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6792 return FALSE;
6793 }
5a580b3a 6794
8423293d
AM
6795 if ((elf_tdata (output_bfd)->cverrefs == 0
6796 && elf_tdata (output_bfd)->cverdefs == 0)
6797 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
3d13f3e9 6798 &section_sym_count) <= 1)
8423293d 6799 {
902e9fc7
MR
6800 asection *s;
6801
3d4d4302 6802 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6803 s->flags |= SEC_EXCLUDE;
6804 }
6805 }
6806 return TRUE;
6807}
6808
74541ad4
AM
6809/* Find the first non-excluded output section. We'll use its
6810 section symbol for some emitted relocs. */
6811void
6812_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6813{
6814 asection *s;
6815
6816 for (s = output_bfd->sections; s != NULL; s = s->next)
6817 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6818 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6819 {
6820 elf_hash_table (info)->text_index_section = s;
6821 break;
6822 }
6823}
6824
6825/* Find two non-excluded output sections, one for code, one for data.
6826 We'll use their section symbols for some emitted relocs. */
6827void
6828_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6829{
6830 asection *s;
6831
266b05cf
DJ
6832 /* Data first, since setting text_index_section changes
6833 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6834 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6835 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6836 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6837 {
266b05cf 6838 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6839 break;
6840 }
6841
6842 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6843 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6844 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6845 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6846 {
266b05cf 6847 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6848 break;
6849 }
6850
6851 if (elf_hash_table (info)->text_index_section == NULL)
6852 elf_hash_table (info)->text_index_section
6853 = elf_hash_table (info)->data_index_section;
6854}
6855
8423293d
AM
6856bfd_boolean
6857bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6858{
74541ad4 6859 const struct elf_backend_data *bed;
23ec1e32 6860 unsigned long section_sym_count;
96d01d93 6861 bfd_size_type dynsymcount = 0;
74541ad4 6862
8423293d
AM
6863 if (!is_elf_hash_table (info->hash))
6864 return TRUE;
6865
74541ad4
AM
6866 bed = get_elf_backend_data (output_bfd);
6867 (*bed->elf_backend_init_index_section) (output_bfd, info);
6868
23ec1e32
MR
6869 /* Assign dynsym indices. In a shared library we generate a section
6870 symbol for each output section, which come first. Next come all
6871 of the back-end allocated local dynamic syms, followed by the rest
6872 of the global symbols.
6873
6874 This is usually not needed for static binaries, however backends
6875 can request to always do it, e.g. the MIPS backend uses dynamic
6876 symbol counts to lay out GOT, which will be produced in the
6877 presence of GOT relocations even in static binaries (holding fixed
6878 data in that case, to satisfy those relocations). */
6879
6880 if (elf_hash_table (info)->dynamic_sections_created
6881 || bed->always_renumber_dynsyms)
6882 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6883 &section_sym_count);
6884
8423293d
AM
6885 if (elf_hash_table (info)->dynamic_sections_created)
6886 {
6887 bfd *dynobj;
8423293d 6888 asection *s;
8423293d
AM
6889 unsigned int dtagcount;
6890
6891 dynobj = elf_hash_table (info)->dynobj;
6892
5a580b3a 6893 /* Work out the size of the symbol version section. */
3d4d4302 6894 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6895 BFD_ASSERT (s != NULL);
d5486c43 6896 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6897 {
eea6121a 6898 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6899 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6900 if (s->contents == NULL)
6901 return FALSE;
6902
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6904 return FALSE;
6905 }
6906
6907 /* Set the size of the .dynsym and .hash sections. We counted
6908 the number of dynamic symbols in elf_link_add_object_symbols.
6909 We will build the contents of .dynsym and .hash when we build
6910 the final symbol table, because until then we do not know the
6911 correct value to give the symbols. We built the .dynstr
6912 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6913 s = elf_hash_table (info)->dynsym;
5a580b3a 6914 BFD_ASSERT (s != NULL);
eea6121a 6915 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6916
d5486c43
L
6917 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6918 if (s->contents == NULL)
6919 return FALSE;
5a580b3a 6920
d5486c43
L
6921 /* The first entry in .dynsym is a dummy symbol. Clear all the
6922 section syms, in case we don't output them all. */
6923 ++section_sym_count;
6924 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6925
fdc90cb4
JJ
6926 elf_hash_table (info)->bucketcount = 0;
6927
5a580b3a
AM
6928 /* Compute the size of the hashing table. As a side effect this
6929 computes the hash values for all the names we export. */
fdc90cb4
JJ
6930 if (info->emit_hash)
6931 {
6932 unsigned long int *hashcodes;
14b1c01e 6933 struct hash_codes_info hashinf;
fdc90cb4
JJ
6934 bfd_size_type amt;
6935 unsigned long int nsyms;
6936 size_t bucketcount;
6937 size_t hash_entry_size;
6938
6939 /* Compute the hash values for all exported symbols. At the same
6940 time store the values in an array so that we could use them for
6941 optimizations. */
6942 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6943 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6944 if (hashcodes == NULL)
6945 return FALSE;
14b1c01e
AM
6946 hashinf.hashcodes = hashcodes;
6947 hashinf.error = FALSE;
5a580b3a 6948
fdc90cb4
JJ
6949 /* Put all hash values in HASHCODES. */
6950 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6951 elf_collect_hash_codes, &hashinf);
6952 if (hashinf.error)
4dd07732
AM
6953 {
6954 free (hashcodes);
6955 return FALSE;
6956 }
5a580b3a 6957
14b1c01e 6958 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6959 bucketcount
6960 = compute_bucket_count (info, hashcodes, nsyms, 0);
6961 free (hashcodes);
6962
4b48e2f6 6963 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 6964 return FALSE;
5a580b3a 6965
fdc90cb4
JJ
6966 elf_hash_table (info)->bucketcount = bucketcount;
6967
3d4d4302 6968 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6969 BFD_ASSERT (s != NULL);
6970 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6971 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6972 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6973 if (s->contents == NULL)
6974 return FALSE;
6975
6976 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6977 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6978 s->contents + hash_entry_size);
6979 }
6980
6981 if (info->emit_gnu_hash)
6982 {
6983 size_t i, cnt;
6984 unsigned char *contents;
6985 struct collect_gnu_hash_codes cinfo;
6986 bfd_size_type amt;
6987 size_t bucketcount;
6988
6989 memset (&cinfo, 0, sizeof (cinfo));
6990
6991 /* Compute the hash values for all exported symbols. At the same
6992 time store the values in an array so that we could use them for
6993 optimizations. */
6994 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6995 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6996 if (cinfo.hashcodes == NULL)
6997 return FALSE;
6998
6999 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7000 cinfo.min_dynindx = -1;
7001 cinfo.output_bfd = output_bfd;
7002 cinfo.bed = bed;
7003
7004 /* Put all hash values in HASHCODES. */
7005 elf_link_hash_traverse (elf_hash_table (info),
7006 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7007 if (cinfo.error)
4dd07732
AM
7008 {
7009 free (cinfo.hashcodes);
7010 return FALSE;
7011 }
fdc90cb4
JJ
7012
7013 bucketcount
7014 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7015
7016 if (bucketcount == 0)
7017 {
7018 free (cinfo.hashcodes);
7019 return FALSE;
7020 }
7021
3d4d4302 7022 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7023 BFD_ASSERT (s != NULL);
7024
7025 if (cinfo.nsyms == 0)
7026 {
7027 /* Empty .gnu.hash section is special. */
7028 BFD_ASSERT (cinfo.min_dynindx == -1);
7029 free (cinfo.hashcodes);
7030 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7031 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7032 if (contents == NULL)
7033 return FALSE;
7034 s->contents = contents;
7035 /* 1 empty bucket. */
7036 bfd_put_32 (output_bfd, 1, contents);
7037 /* SYMIDX above the special symbol 0. */
7038 bfd_put_32 (output_bfd, 1, contents + 4);
7039 /* Just one word for bitmask. */
7040 bfd_put_32 (output_bfd, 1, contents + 8);
7041 /* Only hash fn bloom filter. */
7042 bfd_put_32 (output_bfd, 0, contents + 12);
7043 /* No hashes are valid - empty bitmask. */
7044 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7045 /* No hashes in the only bucket. */
7046 bfd_put_32 (output_bfd, 0,
7047 contents + 16 + bed->s->arch_size / 8);
7048 }
7049 else
7050 {
9e6619e2 7051 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7052 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7053
9e6619e2
AM
7054 x = cinfo.nsyms;
7055 maskbitslog2 = 1;
7056 while ((x >>= 1) != 0)
7057 ++maskbitslog2;
fdc90cb4
JJ
7058 if (maskbitslog2 < 3)
7059 maskbitslog2 = 5;
7060 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7061 maskbitslog2 = maskbitslog2 + 3;
7062 else
7063 maskbitslog2 = maskbitslog2 + 2;
7064 if (bed->s->arch_size == 64)
7065 {
7066 if (maskbitslog2 == 5)
7067 maskbitslog2 = 6;
7068 cinfo.shift1 = 6;
7069 }
7070 else
7071 cinfo.shift1 = 5;
7072 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7073 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7074 cinfo.maskbits = 1 << maskbitslog2;
7075 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7076 amt = bucketcount * sizeof (unsigned long int) * 2;
7077 amt += maskwords * sizeof (bfd_vma);
a50b1753 7078 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7079 if (cinfo.bitmask == NULL)
7080 {
7081 free (cinfo.hashcodes);
7082 return FALSE;
7083 }
7084
a50b1753 7085 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7086 cinfo.indx = cinfo.counts + bucketcount;
7087 cinfo.symindx = dynsymcount - cinfo.nsyms;
7088 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7089
7090 /* Determine how often each hash bucket is used. */
7091 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7092 for (i = 0; i < cinfo.nsyms; ++i)
7093 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7094
7095 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7096 if (cinfo.counts[i] != 0)
7097 {
7098 cinfo.indx[i] = cnt;
7099 cnt += cinfo.counts[i];
7100 }
7101 BFD_ASSERT (cnt == dynsymcount);
7102 cinfo.bucketcount = bucketcount;
7103 cinfo.local_indx = cinfo.min_dynindx;
7104
7105 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7106 s->size += cinfo.maskbits / 8;
a50b1753 7107 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7108 if (contents == NULL)
7109 {
7110 free (cinfo.bitmask);
7111 free (cinfo.hashcodes);
7112 return FALSE;
7113 }
7114
7115 s->contents = contents;
7116 bfd_put_32 (output_bfd, bucketcount, contents);
7117 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7118 bfd_put_32 (output_bfd, maskwords, contents + 8);
7119 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7120 contents += 16 + cinfo.maskbits / 8;
7121
7122 for (i = 0; i < bucketcount; ++i)
7123 {
7124 if (cinfo.counts[i] == 0)
7125 bfd_put_32 (output_bfd, 0, contents);
7126 else
7127 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7128 contents += 4;
7129 }
7130
7131 cinfo.contents = contents;
7132
7133 /* Renumber dynamic symbols, populate .gnu.hash section. */
7134 elf_link_hash_traverse (elf_hash_table (info),
7135 elf_renumber_gnu_hash_syms, &cinfo);
7136
7137 contents = s->contents + 16;
7138 for (i = 0; i < maskwords; ++i)
7139 {
7140 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7141 contents);
7142 contents += bed->s->arch_size / 8;
7143 }
7144
7145 free (cinfo.bitmask);
7146 free (cinfo.hashcodes);
7147 }
7148 }
5a580b3a 7149
3d4d4302 7150 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7151 BFD_ASSERT (s != NULL);
7152
4ad4eba5 7153 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7154
eea6121a 7155 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7156
7157 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7158 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7159 return FALSE;
7160 }
7161
7162 return TRUE;
7163}
4d269e42 7164\f
4d269e42
AM
7165/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7166
7167static void
7168merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7169 asection *sec)
7170{
dbaa2011
AM
7171 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7172 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7173}
7174
7175/* Finish SHF_MERGE section merging. */
7176
7177bfd_boolean
630993ec 7178_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7179{
7180 bfd *ibfd;
7181 asection *sec;
7182
7183 if (!is_elf_hash_table (info->hash))
7184 return FALSE;
7185
c72f2fb2 7186 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7187 if ((ibfd->flags & DYNAMIC) == 0
7188 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7189 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7190 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7191 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7192 if ((sec->flags & SEC_MERGE) != 0
7193 && !bfd_is_abs_section (sec->output_section))
7194 {
7195 struct bfd_elf_section_data *secdata;
7196
7197 secdata = elf_section_data (sec);
630993ec 7198 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7199 &elf_hash_table (info)->merge_info,
7200 sec, &secdata->sec_info))
7201 return FALSE;
7202 else if (secdata->sec_info)
dbaa2011 7203 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7204 }
7205
7206 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7207 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7208 merge_sections_remove_hook);
7209 return TRUE;
7210}
7211
7212/* Create an entry in an ELF linker hash table. */
7213
7214struct bfd_hash_entry *
7215_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7216 struct bfd_hash_table *table,
7217 const char *string)
7218{
7219 /* Allocate the structure if it has not already been allocated by a
7220 subclass. */
7221 if (entry == NULL)
7222 {
a50b1753 7223 entry = (struct bfd_hash_entry *)
ca4be51c 7224 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7225 if (entry == NULL)
7226 return entry;
7227 }
7228
7229 /* Call the allocation method of the superclass. */
7230 entry = _bfd_link_hash_newfunc (entry, table, string);
7231 if (entry != NULL)
7232 {
7233 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7234 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7235
7236 /* Set local fields. */
7237 ret->indx = -1;
7238 ret->dynindx = -1;
7239 ret->got = htab->init_got_refcount;
7240 ret->plt = htab->init_plt_refcount;
7241 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7242 - offsetof (struct elf_link_hash_entry, size)));
7243 /* Assume that we have been called by a non-ELF symbol reader.
7244 This flag is then reset by the code which reads an ELF input
7245 file. This ensures that a symbol created by a non-ELF symbol
7246 reader will have the flag set correctly. */
7247 ret->non_elf = 1;
7248 }
7249
7250 return entry;
7251}
7252
7253/* Copy data from an indirect symbol to its direct symbol, hiding the
7254 old indirect symbol. Also used for copying flags to a weakdef. */
7255
7256void
7257_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7258 struct elf_link_hash_entry *dir,
7259 struct elf_link_hash_entry *ind)
7260{
7261 struct elf_link_hash_table *htab;
7262
7263 /* Copy down any references that we may have already seen to the
e81830c5 7264 symbol which just became indirect. */
4d269e42 7265
422f1182 7266 if (dir->versioned != versioned_hidden)
e81830c5
AM
7267 dir->ref_dynamic |= ind->ref_dynamic;
7268 dir->ref_regular |= ind->ref_regular;
7269 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7270 dir->non_got_ref |= ind->non_got_ref;
7271 dir->needs_plt |= ind->needs_plt;
7272 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7273
7274 if (ind->root.type != bfd_link_hash_indirect)
7275 return;
7276
7277 /* Copy over the global and procedure linkage table refcount entries.
7278 These may have been already set up by a check_relocs routine. */
7279 htab = elf_hash_table (info);
7280 if (ind->got.refcount > htab->init_got_refcount.refcount)
7281 {
7282 if (dir->got.refcount < 0)
7283 dir->got.refcount = 0;
7284 dir->got.refcount += ind->got.refcount;
7285 ind->got.refcount = htab->init_got_refcount.refcount;
7286 }
7287
7288 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7289 {
7290 if (dir->plt.refcount < 0)
7291 dir->plt.refcount = 0;
7292 dir->plt.refcount += ind->plt.refcount;
7293 ind->plt.refcount = htab->init_plt_refcount.refcount;
7294 }
7295
7296 if (ind->dynindx != -1)
7297 {
7298 if (dir->dynindx != -1)
7299 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7300 dir->dynindx = ind->dynindx;
7301 dir->dynstr_index = ind->dynstr_index;
7302 ind->dynindx = -1;
7303 ind->dynstr_index = 0;
7304 }
7305}
7306
7307void
7308_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7309 struct elf_link_hash_entry *h,
7310 bfd_boolean force_local)
7311{
3aa14d16
L
7312 /* STT_GNU_IFUNC symbol must go through PLT. */
7313 if (h->type != STT_GNU_IFUNC)
7314 {
7315 h->plt = elf_hash_table (info)->init_plt_offset;
7316 h->needs_plt = 0;
7317 }
4d269e42
AM
7318 if (force_local)
7319 {
7320 h->forced_local = 1;
7321 if (h->dynindx != -1)
7322 {
4d269e42
AM
7323 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7324 h->dynstr_index);
641338d8
AM
7325 h->dynindx = -1;
7326 h->dynstr_index = 0;
4d269e42
AM
7327 }
7328 }
7329}
7330
7bf52ea2
AM
7331/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7332 caller. */
4d269e42
AM
7333
7334bfd_boolean
7335_bfd_elf_link_hash_table_init
7336 (struct elf_link_hash_table *table,
7337 bfd *abfd,
7338 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7339 struct bfd_hash_table *,
7340 const char *),
4dfe6ac6
NC
7341 unsigned int entsize,
7342 enum elf_target_id target_id)
4d269e42
AM
7343{
7344 bfd_boolean ret;
7345 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7346
4d269e42
AM
7347 table->init_got_refcount.refcount = can_refcount - 1;
7348 table->init_plt_refcount.refcount = can_refcount - 1;
7349 table->init_got_offset.offset = -(bfd_vma) 1;
7350 table->init_plt_offset.offset = -(bfd_vma) 1;
7351 /* The first dynamic symbol is a dummy. */
7352 table->dynsymcount = 1;
7353
7354 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7355
4d269e42 7356 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7357 table->hash_table_id = target_id;
4d269e42
AM
7358
7359 return ret;
7360}
7361
7362/* Create an ELF linker hash table. */
7363
7364struct bfd_link_hash_table *
7365_bfd_elf_link_hash_table_create (bfd *abfd)
7366{
7367 struct elf_link_hash_table *ret;
7368 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7369
7bf52ea2 7370 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7371 if (ret == NULL)
7372 return NULL;
7373
7374 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7375 sizeof (struct elf_link_hash_entry),
7376 GENERIC_ELF_DATA))
4d269e42
AM
7377 {
7378 free (ret);
7379 return NULL;
7380 }
d495ab0d 7381 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7382
7383 return &ret->root;
7384}
7385
9f7c3e5e
AM
7386/* Destroy an ELF linker hash table. */
7387
7388void
d495ab0d 7389_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7390{
d495ab0d
AM
7391 struct elf_link_hash_table *htab;
7392
7393 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7394 if (htab->dynstr != NULL)
7395 _bfd_elf_strtab_free (htab->dynstr);
7396 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7397 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7398}
7399
4d269e42
AM
7400/* This is a hook for the ELF emulation code in the generic linker to
7401 tell the backend linker what file name to use for the DT_NEEDED
7402 entry for a dynamic object. */
7403
7404void
7405bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7406{
7407 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7408 && bfd_get_format (abfd) == bfd_object)
7409 elf_dt_name (abfd) = name;
7410}
7411
7412int
7413bfd_elf_get_dyn_lib_class (bfd *abfd)
7414{
7415 int lib_class;
7416 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7417 && bfd_get_format (abfd) == bfd_object)
7418 lib_class = elf_dyn_lib_class (abfd);
7419 else
7420 lib_class = 0;
7421 return lib_class;
7422}
7423
7424void
7425bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7426{
7427 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7428 && bfd_get_format (abfd) == bfd_object)
7429 elf_dyn_lib_class (abfd) = lib_class;
7430}
7431
7432/* Get the list of DT_NEEDED entries for a link. This is a hook for
7433 the linker ELF emulation code. */
7434
7435struct bfd_link_needed_list *
7436bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7437 struct bfd_link_info *info)
7438{
7439 if (! is_elf_hash_table (info->hash))
7440 return NULL;
7441 return elf_hash_table (info)->needed;
7442}
7443
7444/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7445 hook for the linker ELF emulation code. */
7446
7447struct bfd_link_needed_list *
7448bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7449 struct bfd_link_info *info)
7450{
7451 if (! is_elf_hash_table (info->hash))
7452 return NULL;
7453 return elf_hash_table (info)->runpath;
7454}
7455
7456/* Get the name actually used for a dynamic object for a link. This
7457 is the SONAME entry if there is one. Otherwise, it is the string
7458 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7459
7460const char *
7461bfd_elf_get_dt_soname (bfd *abfd)
7462{
7463 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7464 && bfd_get_format (abfd) == bfd_object)
7465 return elf_dt_name (abfd);
7466 return NULL;
7467}
7468
7469/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7470 the ELF linker emulation code. */
7471
7472bfd_boolean
7473bfd_elf_get_bfd_needed_list (bfd *abfd,
7474 struct bfd_link_needed_list **pneeded)
7475{
7476 asection *s;
7477 bfd_byte *dynbuf = NULL;
cb33740c 7478 unsigned int elfsec;
4d269e42
AM
7479 unsigned long shlink;
7480 bfd_byte *extdyn, *extdynend;
7481 size_t extdynsize;
7482 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7483
7484 *pneeded = NULL;
7485
7486 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7487 || bfd_get_format (abfd) != bfd_object)
7488 return TRUE;
7489
7490 s = bfd_get_section_by_name (abfd, ".dynamic");
7491 if (s == NULL || s->size == 0)
7492 return TRUE;
7493
7494 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7495 goto error_return;
7496
7497 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7498 if (elfsec == SHN_BAD)
4d269e42
AM
7499 goto error_return;
7500
7501 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7502
4d269e42
AM
7503 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7504 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7505
7506 extdyn = dynbuf;
7507 extdynend = extdyn + s->size;
7508 for (; extdyn < extdynend; extdyn += extdynsize)
7509 {
7510 Elf_Internal_Dyn dyn;
7511
7512 (*swap_dyn_in) (abfd, extdyn, &dyn);
7513
7514 if (dyn.d_tag == DT_NULL)
7515 break;
7516
7517 if (dyn.d_tag == DT_NEEDED)
7518 {
7519 const char *string;
7520 struct bfd_link_needed_list *l;
7521 unsigned int tagv = dyn.d_un.d_val;
7522 bfd_size_type amt;
7523
7524 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7525 if (string == NULL)
7526 goto error_return;
7527
7528 amt = sizeof *l;
a50b1753 7529 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7530 if (l == NULL)
7531 goto error_return;
7532
7533 l->by = abfd;
7534 l->name = string;
7535 l->next = *pneeded;
7536 *pneeded = l;
7537 }
7538 }
7539
7540 free (dynbuf);
7541
7542 return TRUE;
7543
7544 error_return:
7545 if (dynbuf != NULL)
7546 free (dynbuf);
7547 return FALSE;
7548}
7549
7550struct elf_symbuf_symbol
7551{
7552 unsigned long st_name; /* Symbol name, index in string tbl */
7553 unsigned char st_info; /* Type and binding attributes */
7554 unsigned char st_other; /* Visibilty, and target specific */
7555};
7556
7557struct elf_symbuf_head
7558{
7559 struct elf_symbuf_symbol *ssym;
ef53be89 7560 size_t count;
4d269e42
AM
7561 unsigned int st_shndx;
7562};
7563
7564struct elf_symbol
7565{
7566 union
7567 {
7568 Elf_Internal_Sym *isym;
7569 struct elf_symbuf_symbol *ssym;
7570 } u;
7571 const char *name;
7572};
7573
7574/* Sort references to symbols by ascending section number. */
7575
7576static int
7577elf_sort_elf_symbol (const void *arg1, const void *arg2)
7578{
7579 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7580 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7581
7582 return s1->st_shndx - s2->st_shndx;
7583}
7584
7585static int
7586elf_sym_name_compare (const void *arg1, const void *arg2)
7587{
7588 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7589 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7590 return strcmp (s1->name, s2->name);
7591}
7592
7593static struct elf_symbuf_head *
ef53be89 7594elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7595{
14b1c01e 7596 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7597 struct elf_symbuf_symbol *ssym;
7598 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7599 size_t i, shndx_count, total_size;
4d269e42 7600
a50b1753 7601 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7602 if (indbuf == NULL)
7603 return NULL;
7604
7605 for (ind = indbuf, i = 0; i < symcount; i++)
7606 if (isymbuf[i].st_shndx != SHN_UNDEF)
7607 *ind++ = &isymbuf[i];
7608 indbufend = ind;
7609
7610 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7611 elf_sort_elf_symbol);
7612
7613 shndx_count = 0;
7614 if (indbufend > indbuf)
7615 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7616 if (ind[0]->st_shndx != ind[1]->st_shndx)
7617 shndx_count++;
7618
3ae181ee
L
7619 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7620 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7621 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7622 if (ssymbuf == NULL)
7623 {
7624 free (indbuf);
7625 return NULL;
7626 }
7627
3ae181ee 7628 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7629 ssymbuf->ssym = NULL;
7630 ssymbuf->count = shndx_count;
7631 ssymbuf->st_shndx = 0;
7632 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7633 {
7634 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7635 {
7636 ssymhead++;
7637 ssymhead->ssym = ssym;
7638 ssymhead->count = 0;
7639 ssymhead->st_shndx = (*ind)->st_shndx;
7640 }
7641 ssym->st_name = (*ind)->st_name;
7642 ssym->st_info = (*ind)->st_info;
7643 ssym->st_other = (*ind)->st_other;
7644 ssymhead->count++;
7645 }
ef53be89 7646 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7647 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7648 == total_size));
4d269e42
AM
7649
7650 free (indbuf);
7651 return ssymbuf;
7652}
7653
7654/* Check if 2 sections define the same set of local and global
7655 symbols. */
7656
8f317e31 7657static bfd_boolean
4d269e42
AM
7658bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7659 struct bfd_link_info *info)
7660{
7661 bfd *bfd1, *bfd2;
7662 const struct elf_backend_data *bed1, *bed2;
7663 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7664 size_t symcount1, symcount2;
4d269e42
AM
7665 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7666 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7667 Elf_Internal_Sym *isym, *isymend;
7668 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7669 size_t count1, count2, i;
cb33740c 7670 unsigned int shndx1, shndx2;
4d269e42
AM
7671 bfd_boolean result;
7672
7673 bfd1 = sec1->owner;
7674 bfd2 = sec2->owner;
7675
4d269e42
AM
7676 /* Both sections have to be in ELF. */
7677 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7678 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7679 return FALSE;
7680
7681 if (elf_section_type (sec1) != elf_section_type (sec2))
7682 return FALSE;
7683
4d269e42
AM
7684 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7685 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7686 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7687 return FALSE;
7688
7689 bed1 = get_elf_backend_data (bfd1);
7690 bed2 = get_elf_backend_data (bfd2);
7691 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7692 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7693 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7694 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7695
7696 if (symcount1 == 0 || symcount2 == 0)
7697 return FALSE;
7698
7699 result = FALSE;
7700 isymbuf1 = NULL;
7701 isymbuf2 = NULL;
a50b1753
NC
7702 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7703 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7704
7705 if (ssymbuf1 == NULL)
7706 {
7707 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7708 NULL, NULL, NULL);
7709 if (isymbuf1 == NULL)
7710 goto done;
7711
7712 if (!info->reduce_memory_overheads)
7713 elf_tdata (bfd1)->symbuf = ssymbuf1
7714 = elf_create_symbuf (symcount1, isymbuf1);
7715 }
7716
7717 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7718 {
7719 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7720 NULL, NULL, NULL);
7721 if (isymbuf2 == NULL)
7722 goto done;
7723
7724 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7725 elf_tdata (bfd2)->symbuf = ssymbuf2
7726 = elf_create_symbuf (symcount2, isymbuf2);
7727 }
7728
7729 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7730 {
7731 /* Optimized faster version. */
ef53be89 7732 size_t lo, hi, mid;
4d269e42
AM
7733 struct elf_symbol *symp;
7734 struct elf_symbuf_symbol *ssym, *ssymend;
7735
7736 lo = 0;
7737 hi = ssymbuf1->count;
7738 ssymbuf1++;
7739 count1 = 0;
7740 while (lo < hi)
7741 {
7742 mid = (lo + hi) / 2;
cb33740c 7743 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7744 hi = mid;
cb33740c 7745 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7746 lo = mid + 1;
7747 else
7748 {
7749 count1 = ssymbuf1[mid].count;
7750 ssymbuf1 += mid;
7751 break;
7752 }
7753 }
7754
7755 lo = 0;
7756 hi = ssymbuf2->count;
7757 ssymbuf2++;
7758 count2 = 0;
7759 while (lo < hi)
7760 {
7761 mid = (lo + hi) / 2;
cb33740c 7762 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7763 hi = mid;
cb33740c 7764 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7765 lo = mid + 1;
7766 else
7767 {
7768 count2 = ssymbuf2[mid].count;
7769 ssymbuf2 += mid;
7770 break;
7771 }
7772 }
7773
7774 if (count1 == 0 || count2 == 0 || count1 != count2)
7775 goto done;
7776
ca4be51c
AM
7777 symtable1
7778 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7779 symtable2
7780 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7781 if (symtable1 == NULL || symtable2 == NULL)
7782 goto done;
7783
7784 symp = symtable1;
7785 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7786 ssym < ssymend; ssym++, symp++)
7787 {
7788 symp->u.ssym = ssym;
7789 symp->name = bfd_elf_string_from_elf_section (bfd1,
7790 hdr1->sh_link,
7791 ssym->st_name);
7792 }
7793
7794 symp = symtable2;
7795 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7796 ssym < ssymend; ssym++, symp++)
7797 {
7798 symp->u.ssym = ssym;
7799 symp->name = bfd_elf_string_from_elf_section (bfd2,
7800 hdr2->sh_link,
7801 ssym->st_name);
7802 }
7803
7804 /* Sort symbol by name. */
7805 qsort (symtable1, count1, sizeof (struct elf_symbol),
7806 elf_sym_name_compare);
7807 qsort (symtable2, count1, sizeof (struct elf_symbol),
7808 elf_sym_name_compare);
7809
7810 for (i = 0; i < count1; i++)
7811 /* Two symbols must have the same binding, type and name. */
7812 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7813 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7814 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7815 goto done;
7816
7817 result = TRUE;
7818 goto done;
7819 }
7820
a50b1753
NC
7821 symtable1 = (struct elf_symbol *)
7822 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7823 symtable2 = (struct elf_symbol *)
7824 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7825 if (symtable1 == NULL || symtable2 == NULL)
7826 goto done;
7827
7828 /* Count definitions in the section. */
7829 count1 = 0;
7830 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7831 if (isym->st_shndx == shndx1)
4d269e42
AM
7832 symtable1[count1++].u.isym = isym;
7833
7834 count2 = 0;
7835 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7836 if (isym->st_shndx == shndx2)
4d269e42
AM
7837 symtable2[count2++].u.isym = isym;
7838
7839 if (count1 == 0 || count2 == 0 || count1 != count2)
7840 goto done;
7841
7842 for (i = 0; i < count1; i++)
7843 symtable1[i].name
7844 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7845 symtable1[i].u.isym->st_name);
7846
7847 for (i = 0; i < count2; i++)
7848 symtable2[i].name
7849 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7850 symtable2[i].u.isym->st_name);
7851
7852 /* Sort symbol by name. */
7853 qsort (symtable1, count1, sizeof (struct elf_symbol),
7854 elf_sym_name_compare);
7855 qsort (symtable2, count1, sizeof (struct elf_symbol),
7856 elf_sym_name_compare);
7857
7858 for (i = 0; i < count1; i++)
7859 /* Two symbols must have the same binding, type and name. */
7860 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7861 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7862 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7863 goto done;
7864
7865 result = TRUE;
7866
7867done:
7868 if (symtable1)
7869 free (symtable1);
7870 if (symtable2)
7871 free (symtable2);
7872 if (isymbuf1)
7873 free (isymbuf1);
7874 if (isymbuf2)
7875 free (isymbuf2);
7876
7877 return result;
7878}
7879
7880/* Return TRUE if 2 section types are compatible. */
7881
7882bfd_boolean
7883_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7884 bfd *bbfd, const asection *bsec)
7885{
7886 if (asec == NULL
7887 || bsec == NULL
7888 || abfd->xvec->flavour != bfd_target_elf_flavour
7889 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7890 return TRUE;
7891
7892 return elf_section_type (asec) == elf_section_type (bsec);
7893}
7894\f
c152c796
AM
7895/* Final phase of ELF linker. */
7896
7897/* A structure we use to avoid passing large numbers of arguments. */
7898
7899struct elf_final_link_info
7900{
7901 /* General link information. */
7902 struct bfd_link_info *info;
7903 /* Output BFD. */
7904 bfd *output_bfd;
7905 /* Symbol string table. */
ef10c3ac 7906 struct elf_strtab_hash *symstrtab;
c152c796
AM
7907 /* .hash section. */
7908 asection *hash_sec;
7909 /* symbol version section (.gnu.version). */
7910 asection *symver_sec;
7911 /* Buffer large enough to hold contents of any section. */
7912 bfd_byte *contents;
7913 /* Buffer large enough to hold external relocs of any section. */
7914 void *external_relocs;
7915 /* Buffer large enough to hold internal relocs of any section. */
7916 Elf_Internal_Rela *internal_relocs;
7917 /* Buffer large enough to hold external local symbols of any input
7918 BFD. */
7919 bfd_byte *external_syms;
7920 /* And a buffer for symbol section indices. */
7921 Elf_External_Sym_Shndx *locsym_shndx;
7922 /* Buffer large enough to hold internal local symbols of any input
7923 BFD. */
7924 Elf_Internal_Sym *internal_syms;
7925 /* Array large enough to hold a symbol index for each local symbol
7926 of any input BFD. */
7927 long *indices;
7928 /* Array large enough to hold a section pointer for each local
7929 symbol of any input BFD. */
7930 asection **sections;
ef10c3ac 7931 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7932 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7933 /* Number of STT_FILE syms seen. */
7934 size_t filesym_count;
c152c796
AM
7935};
7936
7937/* This struct is used to pass information to elf_link_output_extsym. */
7938
7939struct elf_outext_info
7940{
7941 bfd_boolean failed;
7942 bfd_boolean localsyms;
34a79995 7943 bfd_boolean file_sym_done;
8b127cbc 7944 struct elf_final_link_info *flinfo;
c152c796
AM
7945};
7946
d9352518
DB
7947
7948/* Support for evaluating a complex relocation.
7949
7950 Complex relocations are generalized, self-describing relocations. The
7951 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7952 relocations themselves.
d9352518
DB
7953
7954 The relocations are use a reserved elf-wide relocation type code (R_RELC
7955 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7956 information (start bit, end bit, word width, etc) into the addend. This
7957 information is extracted from CGEN-generated operand tables within gas.
7958
7959 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7960 internal) representing prefix-notation expressions, including but not
7961 limited to those sorts of expressions normally encoded as addends in the
7962 addend field. The symbol mangling format is:
7963
7964 <node> := <literal>
7965 | <unary-operator> ':' <node>
7966 | <binary-operator> ':' <node> ':' <node>
7967 ;
7968
7969 <literal> := 's' <digits=N> ':' <N character symbol name>
7970 | 'S' <digits=N> ':' <N character section name>
7971 | '#' <hexdigits>
7972 ;
7973
7974 <binary-operator> := as in C
7975 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7976
7977static void
a0c8462f
AM
7978set_symbol_value (bfd *bfd_with_globals,
7979 Elf_Internal_Sym *isymbuf,
7980 size_t locsymcount,
7981 size_t symidx,
7982 bfd_vma val)
d9352518 7983{
8977835c
AM
7984 struct elf_link_hash_entry **sym_hashes;
7985 struct elf_link_hash_entry *h;
7986 size_t extsymoff = locsymcount;
d9352518 7987
8977835c 7988 if (symidx < locsymcount)
d9352518 7989 {
8977835c
AM
7990 Elf_Internal_Sym *sym;
7991
7992 sym = isymbuf + symidx;
7993 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7994 {
7995 /* It is a local symbol: move it to the
7996 "absolute" section and give it a value. */
7997 sym->st_shndx = SHN_ABS;
7998 sym->st_value = val;
7999 return;
8000 }
8001 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8002 extsymoff = 0;
d9352518 8003 }
8977835c
AM
8004
8005 /* It is a global symbol: set its link type
8006 to "defined" and give it a value. */
8007
8008 sym_hashes = elf_sym_hashes (bfd_with_globals);
8009 h = sym_hashes [symidx - extsymoff];
8010 while (h->root.type == bfd_link_hash_indirect
8011 || h->root.type == bfd_link_hash_warning)
8012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8013 h->root.type = bfd_link_hash_defined;
8014 h->root.u.def.value = val;
8015 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8016}
8017
a0c8462f
AM
8018static bfd_boolean
8019resolve_symbol (const char *name,
8020 bfd *input_bfd,
8b127cbc 8021 struct elf_final_link_info *flinfo,
a0c8462f
AM
8022 bfd_vma *result,
8023 Elf_Internal_Sym *isymbuf,
8024 size_t locsymcount)
d9352518 8025{
a0c8462f
AM
8026 Elf_Internal_Sym *sym;
8027 struct bfd_link_hash_entry *global_entry;
8028 const char *candidate = NULL;
8029 Elf_Internal_Shdr *symtab_hdr;
8030 size_t i;
8031
d9352518
DB
8032 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8033
8034 for (i = 0; i < locsymcount; ++ i)
8035 {
8977835c 8036 sym = isymbuf + i;
d9352518
DB
8037
8038 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8039 continue;
8040
8041 candidate = bfd_elf_string_from_elf_section (input_bfd,
8042 symtab_hdr->sh_link,
8043 sym->st_name);
8044#ifdef DEBUG
0f02bbd9
AM
8045 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8046 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8047#endif
8048 if (candidate && strcmp (candidate, name) == 0)
8049 {
8b127cbc 8050 asection *sec = flinfo->sections [i];
d9352518 8051
0f02bbd9
AM
8052 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8053 *result += sec->output_offset + sec->output_section->vma;
d9352518 8054#ifdef DEBUG
0f02bbd9
AM
8055 printf ("Found symbol with value %8.8lx\n",
8056 (unsigned long) *result);
d9352518
DB
8057#endif
8058 return TRUE;
8059 }
8060 }
8061
8062 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8063 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8064 FALSE, FALSE, TRUE);
d9352518
DB
8065 if (!global_entry)
8066 return FALSE;
a0c8462f 8067
d9352518
DB
8068 if (global_entry->type == bfd_link_hash_defined
8069 || global_entry->type == bfd_link_hash_defweak)
8070 {
a0c8462f
AM
8071 *result = (global_entry->u.def.value
8072 + global_entry->u.def.section->output_section->vma
8073 + global_entry->u.def.section->output_offset);
d9352518 8074#ifdef DEBUG
0f02bbd9
AM
8075 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8076 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8077#endif
8078 return TRUE;
a0c8462f 8079 }
d9352518 8080
d9352518
DB
8081 return FALSE;
8082}
8083
37b01f6a
DG
8084/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8085 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8086 names like "foo.end" which is the end address of section "foo". */
8087
d9352518 8088static bfd_boolean
a0c8462f
AM
8089resolve_section (const char *name,
8090 asection *sections,
37b01f6a
DG
8091 bfd_vma *result,
8092 bfd * abfd)
d9352518 8093{
a0c8462f
AM
8094 asection *curr;
8095 unsigned int len;
d9352518 8096
a0c8462f 8097 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8098 if (strcmp (curr->name, name) == 0)
8099 {
8100 *result = curr->vma;
8101 return TRUE;
8102 }
8103
8104 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8105 /* FIXME: This could be coded more efficiently... */
a0c8462f 8106 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8107 {
8108 len = strlen (curr->name);
a0c8462f 8109 if (len > strlen (name))
d9352518
DB
8110 continue;
8111
8112 if (strncmp (curr->name, name, len) == 0)
8113 {
8114 if (strncmp (".end", name + len, 4) == 0)
8115 {
37b01f6a 8116 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8117 return TRUE;
8118 }
8119
8120 /* Insert more pseudo-section names here, if you like. */
8121 }
8122 }
a0c8462f 8123
d9352518
DB
8124 return FALSE;
8125}
8126
8127static void
a0c8462f 8128undefined_reference (const char *reftype, const char *name)
d9352518 8129{
695344c0 8130 /* xgettext:c-format */
a0c8462f
AM
8131 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8132 reftype, name);
d9352518
DB
8133}
8134
8135static bfd_boolean
a0c8462f
AM
8136eval_symbol (bfd_vma *result,
8137 const char **symp,
8138 bfd *input_bfd,
8b127cbc 8139 struct elf_final_link_info *flinfo,
a0c8462f
AM
8140 bfd_vma dot,
8141 Elf_Internal_Sym *isymbuf,
8142 size_t locsymcount,
8143 int signed_p)
d9352518 8144{
4b93929b
NC
8145 size_t len;
8146 size_t symlen;
a0c8462f
AM
8147 bfd_vma a;
8148 bfd_vma b;
4b93929b 8149 char symbuf[4096];
0f02bbd9 8150 const char *sym = *symp;
a0c8462f
AM
8151 const char *symend;
8152 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8153
8154 len = strlen (sym);
8155 symend = sym + len;
8156
4b93929b 8157 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8158 {
8159 bfd_set_error (bfd_error_invalid_operation);
8160 return FALSE;
8161 }
a0c8462f 8162
d9352518
DB
8163 switch (* sym)
8164 {
8165 case '.':
0f02bbd9
AM
8166 *result = dot;
8167 *symp = sym + 1;
d9352518
DB
8168 return TRUE;
8169
8170 case '#':
0f02bbd9
AM
8171 ++sym;
8172 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8173 return TRUE;
8174
8175 case 'S':
8176 symbol_is_section = TRUE;
1a0670f3 8177 /* Fall through. */
a0c8462f 8178 case 's':
0f02bbd9
AM
8179 ++sym;
8180 symlen = strtol (sym, (char **) symp, 10);
8181 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8182
4b93929b 8183 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8184 {
8185 bfd_set_error (bfd_error_invalid_operation);
8186 return FALSE;
8187 }
8188
8189 memcpy (symbuf, sym, symlen);
a0c8462f 8190 symbuf[symlen] = '\0';
0f02bbd9 8191 *symp = sym + symlen;
a0c8462f
AM
8192
8193 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8194 the symbol as a section, or vice-versa. so we're pretty liberal in our
8195 interpretation here; section means "try section first", not "must be a
8196 section", and likewise with symbol. */
8197
a0c8462f 8198 if (symbol_is_section)
d9352518 8199 {
37b01f6a 8200 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8201 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8202 isymbuf, locsymcount))
d9352518
DB
8203 {
8204 undefined_reference ("section", symbuf);
8205 return FALSE;
8206 }
a0c8462f
AM
8207 }
8208 else
d9352518 8209 {
8b127cbc 8210 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8211 isymbuf, locsymcount)
8b127cbc 8212 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8213 result, input_bfd))
d9352518
DB
8214 {
8215 undefined_reference ("symbol", symbuf);
8216 return FALSE;
8217 }
8218 }
8219
8220 return TRUE;
a0c8462f 8221
d9352518
DB
8222 /* All that remains are operators. */
8223
8224#define UNARY_OP(op) \
8225 if (strncmp (sym, #op, strlen (#op)) == 0) \
8226 { \
8227 sym += strlen (#op); \
a0c8462f
AM
8228 if (*sym == ':') \
8229 ++sym; \
0f02bbd9 8230 *symp = sym; \
8b127cbc 8231 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8232 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8233 return FALSE; \
8234 if (signed_p) \
0f02bbd9 8235 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8236 else \
8237 *result = op a; \
d9352518
DB
8238 return TRUE; \
8239 }
8240
8241#define BINARY_OP(op) \
8242 if (strncmp (sym, #op, strlen (#op)) == 0) \
8243 { \
8244 sym += strlen (#op); \
a0c8462f
AM
8245 if (*sym == ':') \
8246 ++sym; \
0f02bbd9 8247 *symp = sym; \
8b127cbc 8248 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8249 isymbuf, locsymcount, signed_p)) \
a0c8462f 8250 return FALSE; \
0f02bbd9 8251 ++*symp; \
8b127cbc 8252 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8253 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8254 return FALSE; \
8255 if (signed_p) \
0f02bbd9 8256 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8257 else \
8258 *result = a op b; \
d9352518
DB
8259 return TRUE; \
8260 }
8261
8262 default:
8263 UNARY_OP (0-);
8264 BINARY_OP (<<);
8265 BINARY_OP (>>);
8266 BINARY_OP (==);
8267 BINARY_OP (!=);
8268 BINARY_OP (<=);
8269 BINARY_OP (>=);
8270 BINARY_OP (&&);
8271 BINARY_OP (||);
8272 UNARY_OP (~);
8273 UNARY_OP (!);
8274 BINARY_OP (*);
8275 BINARY_OP (/);
8276 BINARY_OP (%);
8277 BINARY_OP (^);
8278 BINARY_OP (|);
8279 BINARY_OP (&);
8280 BINARY_OP (+);
8281 BINARY_OP (-);
8282 BINARY_OP (<);
8283 BINARY_OP (>);
8284#undef UNARY_OP
8285#undef BINARY_OP
8286 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8287 bfd_set_error (bfd_error_invalid_operation);
8288 return FALSE;
8289 }
8290}
8291
d9352518 8292static void
a0c8462f
AM
8293put_value (bfd_vma size,
8294 unsigned long chunksz,
8295 bfd *input_bfd,
8296 bfd_vma x,
8297 bfd_byte *location)
d9352518
DB
8298{
8299 location += (size - chunksz);
8300
41cd1ad1 8301 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8302 {
8303 switch (chunksz)
8304 {
d9352518
DB
8305 case 1:
8306 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8307 x >>= 8;
d9352518
DB
8308 break;
8309 case 2:
8310 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8311 x >>= 16;
d9352518
DB
8312 break;
8313 case 4:
8314 bfd_put_32 (input_bfd, x, location);
65164438
NC
8315 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8316 x >>= 16;
8317 x >>= 16;
d9352518 8318 break;
d9352518 8319#ifdef BFD64
41cd1ad1 8320 case 8:
d9352518 8321 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8322 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8323 x >>= 32;
8324 x >>= 32;
8325 break;
d9352518 8326#endif
41cd1ad1
NC
8327 default:
8328 abort ();
d9352518
DB
8329 break;
8330 }
8331 }
8332}
8333
a0c8462f
AM
8334static bfd_vma
8335get_value (bfd_vma size,
8336 unsigned long chunksz,
8337 bfd *input_bfd,
8338 bfd_byte *location)
d9352518 8339{
9b239e0e 8340 int shift;
d9352518
DB
8341 bfd_vma x = 0;
8342
9b239e0e
NC
8343 /* Sanity checks. */
8344 BFD_ASSERT (chunksz <= sizeof (x)
8345 && size >= chunksz
8346 && chunksz != 0
8347 && (size % chunksz) == 0
8348 && input_bfd != NULL
8349 && location != NULL);
8350
8351 if (chunksz == sizeof (x))
8352 {
8353 BFD_ASSERT (size == chunksz);
8354
8355 /* Make sure that we do not perform an undefined shift operation.
8356 We know that size == chunksz so there will only be one iteration
8357 of the loop below. */
8358 shift = 0;
8359 }
8360 else
8361 shift = 8 * chunksz;
8362
a0c8462f 8363 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8364 {
8365 switch (chunksz)
8366 {
d9352518 8367 case 1:
9b239e0e 8368 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8369 break;
8370 case 2:
9b239e0e 8371 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8372 break;
8373 case 4:
9b239e0e 8374 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8375 break;
d9352518 8376#ifdef BFD64
9b239e0e
NC
8377 case 8:
8378 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8379 break;
9b239e0e
NC
8380#endif
8381 default:
8382 abort ();
d9352518
DB
8383 }
8384 }
8385 return x;
8386}
8387
a0c8462f
AM
8388static void
8389decode_complex_addend (unsigned long *start, /* in bits */
8390 unsigned long *oplen, /* in bits */
8391 unsigned long *len, /* in bits */
8392 unsigned long *wordsz, /* in bytes */
8393 unsigned long *chunksz, /* in bytes */
8394 unsigned long *lsb0_p,
8395 unsigned long *signed_p,
8396 unsigned long *trunc_p,
8397 unsigned long encoded)
d9352518
DB
8398{
8399 * start = encoded & 0x3F;
8400 * len = (encoded >> 6) & 0x3F;
8401 * oplen = (encoded >> 12) & 0x3F;
8402 * wordsz = (encoded >> 18) & 0xF;
8403 * chunksz = (encoded >> 22) & 0xF;
8404 * lsb0_p = (encoded >> 27) & 1;
8405 * signed_p = (encoded >> 28) & 1;
8406 * trunc_p = (encoded >> 29) & 1;
8407}
8408
cdfeee4f 8409bfd_reloc_status_type
0f02bbd9 8410bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8411 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8412 bfd_byte *contents,
8413 Elf_Internal_Rela *rel,
8414 bfd_vma relocation)
d9352518 8415{
0f02bbd9
AM
8416 bfd_vma shift, x, mask;
8417 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8418 bfd_reloc_status_type r;
d9352518
DB
8419
8420 /* Perform this reloc, since it is complex.
8421 (this is not to say that it necessarily refers to a complex
8422 symbol; merely that it is a self-describing CGEN based reloc.
8423 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8424 word size, etc) encoded within it.). */
d9352518 8425
a0c8462f
AM
8426 decode_complex_addend (&start, &oplen, &len, &wordsz,
8427 &chunksz, &lsb0_p, &signed_p,
8428 &trunc_p, rel->r_addend);
d9352518
DB
8429
8430 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8431
8432 if (lsb0_p)
8433 shift = (start + 1) - len;
8434 else
8435 shift = (8 * wordsz) - (start + len);
8436
37b01f6a
DG
8437 x = get_value (wordsz, chunksz, input_bfd,
8438 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8439
8440#ifdef DEBUG
8441 printf ("Doing complex reloc: "
8442 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8443 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8444 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8445 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8446 oplen, (unsigned long) x, (unsigned long) mask,
8447 (unsigned long) relocation);
d9352518
DB
8448#endif
8449
cdfeee4f 8450 r = bfd_reloc_ok;
d9352518 8451 if (! trunc_p)
cdfeee4f
AM
8452 /* Now do an overflow check. */
8453 r = bfd_check_overflow ((signed_p
8454 ? complain_overflow_signed
8455 : complain_overflow_unsigned),
8456 len, 0, (8 * wordsz),
8457 relocation);
a0c8462f 8458
d9352518
DB
8459 /* Do the deed. */
8460 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8461
8462#ifdef DEBUG
8463 printf (" relocation: %8.8lx\n"
8464 " shifted mask: %8.8lx\n"
8465 " shifted/masked reloc: %8.8lx\n"
8466 " result: %8.8lx\n",
9ccb8af9
AM
8467 (unsigned long) relocation, (unsigned long) (mask << shift),
8468 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8469#endif
37b01f6a
DG
8470 put_value (wordsz, chunksz, input_bfd, x,
8471 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8472 return r;
d9352518
DB
8473}
8474
0e287786
AM
8475/* Functions to read r_offset from external (target order) reloc
8476 entry. Faster than bfd_getl32 et al, because we let the compiler
8477 know the value is aligned. */
53df40a4 8478
0e287786
AM
8479static bfd_vma
8480ext32l_r_offset (const void *p)
53df40a4
AM
8481{
8482 union aligned32
8483 {
8484 uint32_t v;
8485 unsigned char c[4];
8486 };
8487 const union aligned32 *a
0e287786 8488 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8489
8490 uint32_t aval = ( (uint32_t) a->c[0]
8491 | (uint32_t) a->c[1] << 8
8492 | (uint32_t) a->c[2] << 16
8493 | (uint32_t) a->c[3] << 24);
0e287786 8494 return aval;
53df40a4
AM
8495}
8496
0e287786
AM
8497static bfd_vma
8498ext32b_r_offset (const void *p)
53df40a4
AM
8499{
8500 union aligned32
8501 {
8502 uint32_t v;
8503 unsigned char c[4];
8504 };
8505 const union aligned32 *a
0e287786 8506 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8507
8508 uint32_t aval = ( (uint32_t) a->c[0] << 24
8509 | (uint32_t) a->c[1] << 16
8510 | (uint32_t) a->c[2] << 8
8511 | (uint32_t) a->c[3]);
0e287786 8512 return aval;
53df40a4
AM
8513}
8514
8515#ifdef BFD_HOST_64_BIT
0e287786
AM
8516static bfd_vma
8517ext64l_r_offset (const void *p)
53df40a4
AM
8518{
8519 union aligned64
8520 {
8521 uint64_t v;
8522 unsigned char c[8];
8523 };
8524 const union aligned64 *a
0e287786 8525 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8526
8527 uint64_t aval = ( (uint64_t) a->c[0]
8528 | (uint64_t) a->c[1] << 8
8529 | (uint64_t) a->c[2] << 16
8530 | (uint64_t) a->c[3] << 24
8531 | (uint64_t) a->c[4] << 32
8532 | (uint64_t) a->c[5] << 40
8533 | (uint64_t) a->c[6] << 48
8534 | (uint64_t) a->c[7] << 56);
0e287786 8535 return aval;
53df40a4
AM
8536}
8537
0e287786
AM
8538static bfd_vma
8539ext64b_r_offset (const void *p)
53df40a4
AM
8540{
8541 union aligned64
8542 {
8543 uint64_t v;
8544 unsigned char c[8];
8545 };
8546 const union aligned64 *a
0e287786 8547 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8548
8549 uint64_t aval = ( (uint64_t) a->c[0] << 56
8550 | (uint64_t) a->c[1] << 48
8551 | (uint64_t) a->c[2] << 40
8552 | (uint64_t) a->c[3] << 32
8553 | (uint64_t) a->c[4] << 24
8554 | (uint64_t) a->c[5] << 16
8555 | (uint64_t) a->c[6] << 8
8556 | (uint64_t) a->c[7]);
0e287786 8557 return aval;
53df40a4
AM
8558}
8559#endif
8560
c152c796
AM
8561/* When performing a relocatable link, the input relocations are
8562 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8563 referenced must be updated. Update all the relocations found in
8564 RELDATA. */
c152c796 8565
bca6d0e3 8566static bfd_boolean
c152c796 8567elf_link_adjust_relocs (bfd *abfd,
9eaff861 8568 asection *sec,
28dbcedc 8569 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8570 bfd_boolean sort,
8571 struct bfd_link_info *info)
c152c796
AM
8572{
8573 unsigned int i;
8574 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8575 bfd_byte *erela;
8576 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8577 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8578 bfd_vma r_type_mask;
8579 int r_sym_shift;
d4730f92
BS
8580 unsigned int count = reldata->count;
8581 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8582
d4730f92 8583 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8584 {
8585 swap_in = bed->s->swap_reloc_in;
8586 swap_out = bed->s->swap_reloc_out;
8587 }
d4730f92 8588 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8589 {
8590 swap_in = bed->s->swap_reloca_in;
8591 swap_out = bed->s->swap_reloca_out;
8592 }
8593 else
8594 abort ();
8595
8596 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8597 abort ();
8598
8599 if (bed->s->arch_size == 32)
8600 {
8601 r_type_mask = 0xff;
8602 r_sym_shift = 8;
8603 }
8604 else
8605 {
8606 r_type_mask = 0xffffffff;
8607 r_sym_shift = 32;
8608 }
8609
d4730f92
BS
8610 erela = reldata->hdr->contents;
8611 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8612 {
8613 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8614 unsigned int j;
8615
8616 if (*rel_hash == NULL)
8617 continue;
8618
10bbbc1d
NC
8619 if ((*rel_hash)->indx == -2
8620 && info->gc_sections
8621 && ! info->gc_keep_exported)
8622 {
8623 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8624 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8625 abfd, sec,
8626 (*rel_hash)->root.root.string);
8627 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
d42c267e 8628 abfd, sec);
10bbbc1d
NC
8629 bfd_set_error (bfd_error_invalid_operation);
8630 return FALSE;
8631 }
c152c796
AM
8632 BFD_ASSERT ((*rel_hash)->indx >= 0);
8633
8634 (*swap_in) (abfd, erela, irela);
8635 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8636 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8637 | (irela[j].r_info & r_type_mask));
8638 (*swap_out) (abfd, irela, erela);
8639 }
53df40a4 8640
9eaff861
AO
8641 if (bed->elf_backend_update_relocs)
8642 (*bed->elf_backend_update_relocs) (sec, reldata);
8643
0e287786 8644 if (sort && count != 0)
53df40a4 8645 {
0e287786
AM
8646 bfd_vma (*ext_r_off) (const void *);
8647 bfd_vma r_off;
8648 size_t elt_size;
8649 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8650 bfd_byte *buf = NULL;
28dbcedc
AM
8651
8652 if (bed->s->arch_size == 32)
8653 {
8654 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8655 ext_r_off = ext32l_r_offset;
28dbcedc 8656 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8657 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8658 else
8659 abort ();
8660 }
53df40a4 8661 else
28dbcedc 8662 {
53df40a4 8663#ifdef BFD_HOST_64_BIT
28dbcedc 8664 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8665 ext_r_off = ext64l_r_offset;
28dbcedc 8666 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8667 ext_r_off = ext64b_r_offset;
28dbcedc 8668 else
53df40a4 8669#endif
28dbcedc
AM
8670 abort ();
8671 }
0e287786 8672
bca6d0e3
AM
8673 /* Must use a stable sort here. A modified insertion sort,
8674 since the relocs are mostly sorted already. */
0e287786
AM
8675 elt_size = reldata->hdr->sh_entsize;
8676 base = reldata->hdr->contents;
8677 end = base + count * elt_size;
bca6d0e3 8678 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8679 abort ();
8680
8681 /* Ensure the first element is lowest. This acts as a sentinel,
8682 speeding the main loop below. */
8683 r_off = (*ext_r_off) (base);
8684 for (p = loc = base; (p += elt_size) < end; )
8685 {
8686 bfd_vma r_off2 = (*ext_r_off) (p);
8687 if (r_off > r_off2)
8688 {
8689 r_off = r_off2;
8690 loc = p;
8691 }
8692 }
8693 if (loc != base)
8694 {
8695 /* Don't just swap *base and *loc as that changes the order
8696 of the original base[0] and base[1] if they happen to
8697 have the same r_offset. */
bca6d0e3
AM
8698 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8699 memcpy (onebuf, loc, elt_size);
0e287786 8700 memmove (base + elt_size, base, loc - base);
bca6d0e3 8701 memcpy (base, onebuf, elt_size);
0e287786
AM
8702 }
8703
b29b8669 8704 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8705 {
8706 /* base to p is sorted, *p is next to insert. */
8707 r_off = (*ext_r_off) (p);
8708 /* Search the sorted region for location to insert. */
8709 loc = p - elt_size;
8710 while (r_off < (*ext_r_off) (loc))
8711 loc -= elt_size;
8712 loc += elt_size;
8713 if (loc != p)
8714 {
bca6d0e3
AM
8715 /* Chances are there is a run of relocs to insert here,
8716 from one of more input files. Files are not always
8717 linked in order due to the way elf_link_input_bfd is
8718 called. See pr17666. */
8719 size_t sortlen = p - loc;
8720 bfd_vma r_off2 = (*ext_r_off) (loc);
8721 size_t runlen = elt_size;
8722 size_t buf_size = 96 * 1024;
8723 while (p + runlen < end
8724 && (sortlen <= buf_size
8725 || runlen + elt_size <= buf_size)
8726 && r_off2 > (*ext_r_off) (p + runlen))
8727 runlen += elt_size;
8728 if (buf == NULL)
8729 {
8730 buf = bfd_malloc (buf_size);
8731 if (buf == NULL)
8732 return FALSE;
8733 }
8734 if (runlen < sortlen)
8735 {
8736 memcpy (buf, p, runlen);
8737 memmove (loc + runlen, loc, sortlen);
8738 memcpy (loc, buf, runlen);
8739 }
8740 else
8741 {
8742 memcpy (buf, loc, sortlen);
8743 memmove (loc, p, runlen);
8744 memcpy (loc + runlen, buf, sortlen);
8745 }
b29b8669 8746 p += runlen - elt_size;
0e287786
AM
8747 }
8748 }
8749 /* Hashes are no longer valid. */
28dbcedc
AM
8750 free (reldata->hashes);
8751 reldata->hashes = NULL;
bca6d0e3 8752 free (buf);
53df40a4 8753 }
bca6d0e3 8754 return TRUE;
c152c796
AM
8755}
8756
8757struct elf_link_sort_rela
8758{
8759 union {
8760 bfd_vma offset;
8761 bfd_vma sym_mask;
8762 } u;
8763 enum elf_reloc_type_class type;
8764 /* We use this as an array of size int_rels_per_ext_rel. */
8765 Elf_Internal_Rela rela[1];
8766};
8767
8768static int
8769elf_link_sort_cmp1 (const void *A, const void *B)
8770{
a50b1753
NC
8771 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8772 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8773 int relativea, relativeb;
8774
8775 relativea = a->type == reloc_class_relative;
8776 relativeb = b->type == reloc_class_relative;
8777
8778 if (relativea < relativeb)
8779 return 1;
8780 if (relativea > relativeb)
8781 return -1;
8782 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8783 return -1;
8784 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8785 return 1;
8786 if (a->rela->r_offset < b->rela->r_offset)
8787 return -1;
8788 if (a->rela->r_offset > b->rela->r_offset)
8789 return 1;
8790 return 0;
8791}
8792
8793static int
8794elf_link_sort_cmp2 (const void *A, const void *B)
8795{
a50b1753
NC
8796 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8797 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8798
7e612e98 8799 if (a->type < b->type)
c152c796 8800 return -1;
7e612e98 8801 if (a->type > b->type)
c152c796 8802 return 1;
7e612e98 8803 if (a->u.offset < b->u.offset)
c152c796 8804 return -1;
7e612e98 8805 if (a->u.offset > b->u.offset)
c152c796
AM
8806 return 1;
8807 if (a->rela->r_offset < b->rela->r_offset)
8808 return -1;
8809 if (a->rela->r_offset > b->rela->r_offset)
8810 return 1;
8811 return 0;
8812}
8813
8814static size_t
8815elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8816{
3410fea8 8817 asection *dynamic_relocs;
fc66a176
L
8818 asection *rela_dyn;
8819 asection *rel_dyn;
c152c796
AM
8820 bfd_size_type count, size;
8821 size_t i, ret, sort_elt, ext_size;
8822 bfd_byte *sort, *s_non_relative, *p;
8823 struct elf_link_sort_rela *sq;
8824 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8825 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8826 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8827 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8828 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8829 struct bfd_link_order *lo;
8830 bfd_vma r_sym_mask;
3410fea8 8831 bfd_boolean use_rela;
c152c796 8832
3410fea8
NC
8833 /* Find a dynamic reloc section. */
8834 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8835 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8836 if (rela_dyn != NULL && rela_dyn->size > 0
8837 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8838 {
3410fea8
NC
8839 bfd_boolean use_rela_initialised = FALSE;
8840
8841 /* This is just here to stop gcc from complaining.
c8e44c6d 8842 Its initialization checking code is not perfect. */
3410fea8
NC
8843 use_rela = TRUE;
8844
8845 /* Both sections are present. Examine the sizes
8846 of the indirect sections to help us choose. */
8847 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8848 if (lo->type == bfd_indirect_link_order)
8849 {
8850 asection *o = lo->u.indirect.section;
8851
8852 if ((o->size % bed->s->sizeof_rela) == 0)
8853 {
8854 if ((o->size % bed->s->sizeof_rel) == 0)
8855 /* Section size is divisible by both rel and rela sizes.
8856 It is of no help to us. */
8857 ;
8858 else
8859 {
8860 /* Section size is only divisible by rela. */
535b785f 8861 if (use_rela_initialised && !use_rela)
3410fea8 8862 {
c8e44c6d
AM
8863 _bfd_error_handler (_("%B: Unable to sort relocs - "
8864 "they are in more than one size"),
8865 abfd);
3410fea8
NC
8866 bfd_set_error (bfd_error_invalid_operation);
8867 return 0;
8868 }
8869 else
8870 {
8871 use_rela = TRUE;
8872 use_rela_initialised = TRUE;
8873 }
8874 }
8875 }
8876 else if ((o->size % bed->s->sizeof_rel) == 0)
8877 {
8878 /* Section size is only divisible by rel. */
535b785f 8879 if (use_rela_initialised && use_rela)
3410fea8 8880 {
c8e44c6d
AM
8881 _bfd_error_handler (_("%B: Unable to sort relocs - "
8882 "they are in more than one size"),
8883 abfd);
3410fea8
NC
8884 bfd_set_error (bfd_error_invalid_operation);
8885 return 0;
8886 }
8887 else
8888 {
8889 use_rela = FALSE;
8890 use_rela_initialised = TRUE;
8891 }
8892 }
8893 else
8894 {
c8e44c6d
AM
8895 /* The section size is not divisible by either -
8896 something is wrong. */
8897 _bfd_error_handler (_("%B: Unable to sort relocs - "
8898 "they are of an unknown size"), abfd);
3410fea8
NC
8899 bfd_set_error (bfd_error_invalid_operation);
8900 return 0;
8901 }
8902 }
8903
8904 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8905 if (lo->type == bfd_indirect_link_order)
8906 {
8907 asection *o = lo->u.indirect.section;
8908
8909 if ((o->size % bed->s->sizeof_rela) == 0)
8910 {
8911 if ((o->size % bed->s->sizeof_rel) == 0)
8912 /* Section size is divisible by both rel and rela sizes.
8913 It is of no help to us. */
8914 ;
8915 else
8916 {
8917 /* Section size is only divisible by rela. */
535b785f 8918 if (use_rela_initialised && !use_rela)
3410fea8 8919 {
c8e44c6d
AM
8920 _bfd_error_handler (_("%B: Unable to sort relocs - "
8921 "they are in more than one size"),
8922 abfd);
3410fea8
NC
8923 bfd_set_error (bfd_error_invalid_operation);
8924 return 0;
8925 }
8926 else
8927 {
8928 use_rela = TRUE;
8929 use_rela_initialised = TRUE;
8930 }
8931 }
8932 }
8933 else if ((o->size % bed->s->sizeof_rel) == 0)
8934 {
8935 /* Section size is only divisible by rel. */
535b785f 8936 if (use_rela_initialised && use_rela)
3410fea8 8937 {
c8e44c6d
AM
8938 _bfd_error_handler (_("%B: Unable to sort relocs - "
8939 "they are in more than one size"),
8940 abfd);
3410fea8
NC
8941 bfd_set_error (bfd_error_invalid_operation);
8942 return 0;
8943 }
8944 else
8945 {
8946 use_rela = FALSE;
8947 use_rela_initialised = TRUE;
8948 }
8949 }
8950 else
8951 {
c8e44c6d
AM
8952 /* The section size is not divisible by either -
8953 something is wrong. */
8954 _bfd_error_handler (_("%B: Unable to sort relocs - "
8955 "they are of an unknown size"), abfd);
3410fea8
NC
8956 bfd_set_error (bfd_error_invalid_operation);
8957 return 0;
8958 }
8959 }
8960
8961 if (! use_rela_initialised)
8962 /* Make a guess. */
8963 use_rela = TRUE;
c152c796 8964 }
fc66a176
L
8965 else if (rela_dyn != NULL && rela_dyn->size > 0)
8966 use_rela = TRUE;
8967 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8968 use_rela = FALSE;
c152c796 8969 else
fc66a176 8970 return 0;
3410fea8
NC
8971
8972 if (use_rela)
c152c796 8973 {
3410fea8 8974 dynamic_relocs = rela_dyn;
c152c796
AM
8975 ext_size = bed->s->sizeof_rela;
8976 swap_in = bed->s->swap_reloca_in;
8977 swap_out = bed->s->swap_reloca_out;
8978 }
3410fea8
NC
8979 else
8980 {
8981 dynamic_relocs = rel_dyn;
8982 ext_size = bed->s->sizeof_rel;
8983 swap_in = bed->s->swap_reloc_in;
8984 swap_out = bed->s->swap_reloc_out;
8985 }
c152c796
AM
8986
8987 size = 0;
3410fea8 8988 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8989 if (lo->type == bfd_indirect_link_order)
3410fea8 8990 size += lo->u.indirect.section->size;
c152c796 8991
3410fea8 8992 if (size != dynamic_relocs->size)
c152c796
AM
8993 return 0;
8994
8995 sort_elt = (sizeof (struct elf_link_sort_rela)
8996 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8997
8998 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8999 if (count == 0)
9000 return 0;
a50b1753 9001 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9002
c152c796
AM
9003 if (sort == NULL)
9004 {
9005 (*info->callbacks->warning)
9006 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9007 return 0;
9008 }
9009
9010 if (bed->s->arch_size == 32)
9011 r_sym_mask = ~(bfd_vma) 0xff;
9012 else
9013 r_sym_mask = ~(bfd_vma) 0xffffffff;
9014
3410fea8 9015 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9016 if (lo->type == bfd_indirect_link_order)
9017 {
9018 bfd_byte *erel, *erelend;
9019 asection *o = lo->u.indirect.section;
9020
1da212d6
AM
9021 if (o->contents == NULL && o->size != 0)
9022 {
9023 /* This is a reloc section that is being handled as a normal
9024 section. See bfd_section_from_shdr. We can't combine
9025 relocs in this case. */
9026 free (sort);
9027 return 0;
9028 }
c152c796 9029 erel = o->contents;
eea6121a 9030 erelend = o->contents + o->size;
c8e44c6d 9031 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9032
c152c796
AM
9033 while (erel < erelend)
9034 {
9035 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9036
c152c796 9037 (*swap_in) (abfd, erel, s->rela);
7e612e98 9038 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9039 s->u.sym_mask = r_sym_mask;
9040 p += sort_elt;
9041 erel += ext_size;
9042 }
9043 }
9044
9045 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9046
9047 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9048 {
9049 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9050 if (s->type != reloc_class_relative)
9051 break;
9052 }
9053 ret = i;
9054 s_non_relative = p;
9055
9056 sq = (struct elf_link_sort_rela *) s_non_relative;
9057 for (; i < count; i++, p += sort_elt)
9058 {
9059 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9060 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9061 sq = sp;
9062 sp->u.offset = sq->rela->r_offset;
9063 }
9064
9065 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9066
c8e44c6d
AM
9067 struct elf_link_hash_table *htab = elf_hash_table (info);
9068 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9069 {
9070 /* We have plt relocs in .rela.dyn. */
9071 sq = (struct elf_link_sort_rela *) sort;
9072 for (i = 0; i < count; i++)
9073 if (sq[count - i - 1].type != reloc_class_plt)
9074 break;
9075 if (i != 0 && htab->srelplt->size == i * ext_size)
9076 {
9077 struct bfd_link_order **plo;
9078 /* Put srelplt link_order last. This is so the output_offset
9079 set in the next loop is correct for DT_JMPREL. */
9080 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9081 if ((*plo)->type == bfd_indirect_link_order
9082 && (*plo)->u.indirect.section == htab->srelplt)
9083 {
9084 lo = *plo;
9085 *plo = lo->next;
9086 }
9087 else
9088 plo = &(*plo)->next;
9089 *plo = lo;
9090 lo->next = NULL;
9091 dynamic_relocs->map_tail.link_order = lo;
9092 }
9093 }
9094
9095 p = sort;
3410fea8 9096 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9097 if (lo->type == bfd_indirect_link_order)
9098 {
9099 bfd_byte *erel, *erelend;
9100 asection *o = lo->u.indirect.section;
9101
9102 erel = o->contents;
eea6121a 9103 erelend = o->contents + o->size;
c8e44c6d 9104 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9105 while (erel < erelend)
9106 {
9107 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9108 (*swap_out) (abfd, s->rela, erel);
9109 p += sort_elt;
9110 erel += ext_size;
9111 }
9112 }
9113
9114 free (sort);
3410fea8 9115 *psec = dynamic_relocs;
c152c796
AM
9116 return ret;
9117}
9118
ef10c3ac 9119/* Add a symbol to the output symbol string table. */
c152c796 9120
6e0b88f1 9121static int
ef10c3ac
L
9122elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9123 const char *name,
9124 Elf_Internal_Sym *elfsym,
9125 asection *input_sec,
9126 struct elf_link_hash_entry *h)
c152c796 9127{
6e0b88f1 9128 int (*output_symbol_hook)
c152c796
AM
9129 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9130 struct elf_link_hash_entry *);
ef10c3ac 9131 struct elf_link_hash_table *hash_table;
c152c796 9132 const struct elf_backend_data *bed;
ef10c3ac 9133 bfd_size_type strtabsize;
c152c796 9134
8539e4e8
AM
9135 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9136
8b127cbc 9137 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9138 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9139 if (output_symbol_hook != NULL)
9140 {
8b127cbc 9141 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9142 if (ret != 1)
9143 return ret;
c152c796
AM
9144 }
9145
ef10c3ac
L
9146 if (name == NULL
9147 || *name == '\0'
9148 || (input_sec->flags & SEC_EXCLUDE))
9149 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9150 else
9151 {
ef10c3ac
L
9152 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9153 to get the final offset for st_name. */
9154 elfsym->st_name
9155 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9156 name, FALSE);
c152c796 9157 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9158 return 0;
c152c796
AM
9159 }
9160
ef10c3ac
L
9161 hash_table = elf_hash_table (flinfo->info);
9162 strtabsize = hash_table->strtabsize;
9163 if (strtabsize <= hash_table->strtabcount)
c152c796 9164 {
ef10c3ac
L
9165 strtabsize += strtabsize;
9166 hash_table->strtabsize = strtabsize;
9167 strtabsize *= sizeof (*hash_table->strtab);
9168 hash_table->strtab
9169 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9170 strtabsize);
9171 if (hash_table->strtab == NULL)
6e0b88f1 9172 return 0;
c152c796 9173 }
ef10c3ac
L
9174 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9175 hash_table->strtab[hash_table->strtabcount].dest_index
9176 = hash_table->strtabcount;
9177 hash_table->strtab[hash_table->strtabcount].destshndx_index
9178 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9179
9180 bfd_get_symcount (flinfo->output_bfd) += 1;
9181 hash_table->strtabcount += 1;
9182
9183 return 1;
9184}
9185
9186/* Swap symbols out to the symbol table and flush the output symbols to
9187 the file. */
9188
9189static bfd_boolean
9190elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9191{
9192 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9193 bfd_size_type amt;
9194 size_t i;
ef10c3ac
L
9195 const struct elf_backend_data *bed;
9196 bfd_byte *symbuf;
9197 Elf_Internal_Shdr *hdr;
9198 file_ptr pos;
9199 bfd_boolean ret;
9200
9201 if (!hash_table->strtabcount)
9202 return TRUE;
9203
9204 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9205
9206 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9207
ef10c3ac
L
9208 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9209 symbuf = (bfd_byte *) bfd_malloc (amt);
9210 if (symbuf == NULL)
9211 return FALSE;
1b786873 9212
ef10c3ac 9213 if (flinfo->symshndxbuf)
c152c796 9214 {
ef53be89
AM
9215 amt = sizeof (Elf_External_Sym_Shndx);
9216 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9217 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9218 if (flinfo->symshndxbuf == NULL)
c152c796 9219 {
ef10c3ac
L
9220 free (symbuf);
9221 return FALSE;
c152c796 9222 }
c152c796
AM
9223 }
9224
ef10c3ac
L
9225 for (i = 0; i < hash_table->strtabcount; i++)
9226 {
9227 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9228 if (elfsym->sym.st_name == (unsigned long) -1)
9229 elfsym->sym.st_name = 0;
9230 else
9231 elfsym->sym.st_name
9232 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9233 elfsym->sym.st_name);
9234 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9235 ((bfd_byte *) symbuf
9236 + (elfsym->dest_index
9237 * bed->s->sizeof_sym)),
9238 (flinfo->symshndxbuf
9239 + elfsym->destshndx_index));
9240 }
9241
9242 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9243 pos = hdr->sh_offset + hdr->sh_size;
9244 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9245 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9246 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9247 {
9248 hdr->sh_size += amt;
9249 ret = TRUE;
9250 }
9251 else
9252 ret = FALSE;
c152c796 9253
ef10c3ac
L
9254 free (symbuf);
9255
9256 free (hash_table->strtab);
9257 hash_table->strtab = NULL;
9258
9259 return ret;
c152c796
AM
9260}
9261
c0d5a53d
L
9262/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9263
9264static bfd_boolean
9265check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9266{
4fbb74a6
AM
9267 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9268 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9269 {
9270 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9271 beyond 64k. */
4eca0228 9272 _bfd_error_handler
695344c0 9273 /* xgettext:c-format */
c0d5a53d 9274 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9275 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9276 bfd_set_error (bfd_error_nonrepresentable_section);
9277 return FALSE;
9278 }
9279 return TRUE;
9280}
9281
c152c796
AM
9282/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9283 allowing an unsatisfied unversioned symbol in the DSO to match a
9284 versioned symbol that would normally require an explicit version.
9285 We also handle the case that a DSO references a hidden symbol
9286 which may be satisfied by a versioned symbol in another DSO. */
9287
9288static bfd_boolean
9289elf_link_check_versioned_symbol (struct bfd_link_info *info,
9290 const struct elf_backend_data *bed,
9291 struct elf_link_hash_entry *h)
9292{
9293 bfd *abfd;
9294 struct elf_link_loaded_list *loaded;
9295
9296 if (!is_elf_hash_table (info->hash))
9297 return FALSE;
9298
90c984fc
L
9299 /* Check indirect symbol. */
9300 while (h->root.type == bfd_link_hash_indirect)
9301 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9302
c152c796
AM
9303 switch (h->root.type)
9304 {
9305 default:
9306 abfd = NULL;
9307 break;
9308
9309 case bfd_link_hash_undefined:
9310 case bfd_link_hash_undefweak:
9311 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9312 if (abfd == NULL
9313 || (abfd->flags & DYNAMIC) == 0
e56f61be 9314 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9315 return FALSE;
9316 break;
9317
9318 case bfd_link_hash_defined:
9319 case bfd_link_hash_defweak:
9320 abfd = h->root.u.def.section->owner;
9321 break;
9322
9323 case bfd_link_hash_common:
9324 abfd = h->root.u.c.p->section->owner;
9325 break;
9326 }
9327 BFD_ASSERT (abfd != NULL);
9328
9329 for (loaded = elf_hash_table (info)->loaded;
9330 loaded != NULL;
9331 loaded = loaded->next)
9332 {
9333 bfd *input;
9334 Elf_Internal_Shdr *hdr;
ef53be89
AM
9335 size_t symcount;
9336 size_t extsymcount;
9337 size_t extsymoff;
c152c796
AM
9338 Elf_Internal_Shdr *versymhdr;
9339 Elf_Internal_Sym *isym;
9340 Elf_Internal_Sym *isymend;
9341 Elf_Internal_Sym *isymbuf;
9342 Elf_External_Versym *ever;
9343 Elf_External_Versym *extversym;
9344
9345 input = loaded->abfd;
9346
9347 /* We check each DSO for a possible hidden versioned definition. */
9348 if (input == abfd
9349 || (input->flags & DYNAMIC) == 0
9350 || elf_dynversym (input) == 0)
9351 continue;
9352
9353 hdr = &elf_tdata (input)->dynsymtab_hdr;
9354
9355 symcount = hdr->sh_size / bed->s->sizeof_sym;
9356 if (elf_bad_symtab (input))
9357 {
9358 extsymcount = symcount;
9359 extsymoff = 0;
9360 }
9361 else
9362 {
9363 extsymcount = symcount - hdr->sh_info;
9364 extsymoff = hdr->sh_info;
9365 }
9366
9367 if (extsymcount == 0)
9368 continue;
9369
9370 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9371 NULL, NULL, NULL);
9372 if (isymbuf == NULL)
9373 return FALSE;
9374
9375 /* Read in any version definitions. */
9376 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9377 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9378 if (extversym == NULL)
9379 goto error_ret;
9380
9381 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9382 || (bfd_bread (extversym, versymhdr->sh_size, input)
9383 != versymhdr->sh_size))
9384 {
9385 free (extversym);
9386 error_ret:
9387 free (isymbuf);
9388 return FALSE;
9389 }
9390
9391 ever = extversym + extsymoff;
9392 isymend = isymbuf + extsymcount;
9393 for (isym = isymbuf; isym < isymend; isym++, ever++)
9394 {
9395 const char *name;
9396 Elf_Internal_Versym iver;
9397 unsigned short version_index;
9398
9399 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9400 || isym->st_shndx == SHN_UNDEF)
9401 continue;
9402
9403 name = bfd_elf_string_from_elf_section (input,
9404 hdr->sh_link,
9405 isym->st_name);
9406 if (strcmp (name, h->root.root.string) != 0)
9407 continue;
9408
9409 _bfd_elf_swap_versym_in (input, ever, &iver);
9410
d023c380
L
9411 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9412 && !(h->def_regular
9413 && h->forced_local))
c152c796
AM
9414 {
9415 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9416 have provided a definition for the undefined sym unless
9417 it is defined in a non-shared object and forced local.
9418 */
c152c796
AM
9419 abort ();
9420 }
9421
9422 version_index = iver.vs_vers & VERSYM_VERSION;
9423 if (version_index == 1 || version_index == 2)
9424 {
9425 /* This is the base or first version. We can use it. */
9426 free (extversym);
9427 free (isymbuf);
9428 return TRUE;
9429 }
9430 }
9431
9432 free (extversym);
9433 free (isymbuf);
9434 }
9435
9436 return FALSE;
9437}
9438
b8871f35
L
9439/* Convert ELF common symbol TYPE. */
9440
9441static int
9442elf_link_convert_common_type (struct bfd_link_info *info, int type)
9443{
9444 /* Commom symbol can only appear in relocatable link. */
9445 if (!bfd_link_relocatable (info))
9446 abort ();
9447 switch (info->elf_stt_common)
9448 {
9449 case unchanged:
9450 break;
9451 case elf_stt_common:
9452 type = STT_COMMON;
9453 break;
9454 case no_elf_stt_common:
9455 type = STT_OBJECT;
9456 break;
9457 }
9458 return type;
9459}
9460
c152c796
AM
9461/* Add an external symbol to the symbol table. This is called from
9462 the hash table traversal routine. When generating a shared object,
9463 we go through the symbol table twice. The first time we output
9464 anything that might have been forced to local scope in a version
9465 script. The second time we output the symbols that are still
9466 global symbols. */
9467
9468static bfd_boolean
7686d77d 9469elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9470{
7686d77d 9471 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9472 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9473 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9474 bfd_boolean strip;
9475 Elf_Internal_Sym sym;
9476 asection *input_sec;
9477 const struct elf_backend_data *bed;
6e0b88f1
AM
9478 long indx;
9479 int ret;
b8871f35 9480 unsigned int type;
c152c796
AM
9481
9482 if (h->root.type == bfd_link_hash_warning)
9483 {
9484 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9485 if (h->root.type == bfd_link_hash_new)
9486 return TRUE;
9487 }
9488
9489 /* Decide whether to output this symbol in this pass. */
9490 if (eoinfo->localsyms)
9491 {
4deb8f71 9492 if (!h->forced_local)
c152c796
AM
9493 return TRUE;
9494 }
9495 else
9496 {
4deb8f71 9497 if (h->forced_local)
c152c796
AM
9498 return TRUE;
9499 }
9500
8b127cbc 9501 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9502
12ac1cf5 9503 if (h->root.type == bfd_link_hash_undefined)
c152c796 9504 {
12ac1cf5
NC
9505 /* If we have an undefined symbol reference here then it must have
9506 come from a shared library that is being linked in. (Undefined
98da7939
L
9507 references in regular files have already been handled unless
9508 they are in unreferenced sections which are removed by garbage
9509 collection). */
12ac1cf5
NC
9510 bfd_boolean ignore_undef = FALSE;
9511
9512 /* Some symbols may be special in that the fact that they're
9513 undefined can be safely ignored - let backend determine that. */
9514 if (bed->elf_backend_ignore_undef_symbol)
9515 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9516
9517 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9518 if (!ignore_undef
12ac1cf5 9519 && h->ref_dynamic
8b127cbc
AM
9520 && (!h->ref_regular || flinfo->info->gc_sections)
9521 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9522 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9523 (*flinfo->info->callbacks->undefined_symbol)
9524 (flinfo->info, h->root.root.string,
9525 h->ref_regular ? NULL : h->root.u.undef.abfd,
9526 NULL, 0,
9527 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9528
9529 /* Strip a global symbol defined in a discarded section. */
9530 if (h->indx == -3)
9531 return TRUE;
c152c796
AM
9532 }
9533
9534 /* We should also warn if a forced local symbol is referenced from
9535 shared libraries. */
0e1862bb 9536 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9537 && h->forced_local
9538 && h->ref_dynamic
371a5866 9539 && h->def_regular
f5385ebf 9540 && !h->dynamic_def
ee659f1f 9541 && h->ref_dynamic_nonweak
8b127cbc 9542 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9543 {
17d078c5
AM
9544 bfd *def_bfd;
9545 const char *msg;
90c984fc
L
9546 struct elf_link_hash_entry *hi = h;
9547
9548 /* Check indirect symbol. */
9549 while (hi->root.type == bfd_link_hash_indirect)
9550 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9551
9552 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9553 /* xgettext:c-format */
17d078c5
AM
9554 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9555 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9556 /* xgettext:c-format */
17d078c5
AM
9557 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9558 else
695344c0 9559 /* xgettext:c-format */
17d078c5 9560 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9561 def_bfd = flinfo->output_bfd;
90c984fc
L
9562 if (hi->root.u.def.section != bfd_abs_section_ptr)
9563 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9564 _bfd_error_handler (msg, flinfo->output_bfd,
9565 h->root.root.string, def_bfd);
17d078c5 9566 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9567 eoinfo->failed = TRUE;
9568 return FALSE;
9569 }
9570
9571 /* We don't want to output symbols that have never been mentioned by
9572 a regular file, or that we have been told to strip. However, if
9573 h->indx is set to -2, the symbol is used by a reloc and we must
9574 output it. */
d983c8c5 9575 strip = FALSE;
c152c796 9576 if (h->indx == -2)
d983c8c5 9577 ;
f5385ebf 9578 else if ((h->def_dynamic
77cfaee6
AM
9579 || h->ref_dynamic
9580 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9581 && !h->def_regular
9582 && !h->ref_regular)
c152c796 9583 strip = TRUE;
8b127cbc 9584 else if (flinfo->info->strip == strip_all)
c152c796 9585 strip = TRUE;
8b127cbc
AM
9586 else if (flinfo->info->strip == strip_some
9587 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9588 h->root.root.string, FALSE, FALSE) == NULL)
9589 strip = TRUE;
d56d55e7
AM
9590 else if ((h->root.type == bfd_link_hash_defined
9591 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9592 && ((flinfo->info->strip_discarded
dbaa2011 9593 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9594 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9595 && h->root.u.def.section->owner != NULL
d56d55e7 9596 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9597 strip = TRUE;
9e2278f5
AM
9598 else if ((h->root.type == bfd_link_hash_undefined
9599 || h->root.type == bfd_link_hash_undefweak)
9600 && h->root.u.undef.abfd != NULL
9601 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9602 strip = TRUE;
c152c796 9603
b8871f35
L
9604 type = h->type;
9605
c152c796 9606 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9607 nothing else to do. However, if it is a forced local symbol or
9608 an ifunc symbol we need to give the backend finish_dynamic_symbol
9609 function a chance to make it dynamic. */
c152c796
AM
9610 if (strip
9611 && h->dynindx == -1
b8871f35 9612 && type != STT_GNU_IFUNC
f5385ebf 9613 && !h->forced_local)
c152c796
AM
9614 return TRUE;
9615
9616 sym.st_value = 0;
9617 sym.st_size = h->size;
9618 sym.st_other = h->other;
c152c796
AM
9619 switch (h->root.type)
9620 {
9621 default:
9622 case bfd_link_hash_new:
9623 case bfd_link_hash_warning:
9624 abort ();
9625 return FALSE;
9626
9627 case bfd_link_hash_undefined:
9628 case bfd_link_hash_undefweak:
9629 input_sec = bfd_und_section_ptr;
9630 sym.st_shndx = SHN_UNDEF;
9631 break;
9632
9633 case bfd_link_hash_defined:
9634 case bfd_link_hash_defweak:
9635 {
9636 input_sec = h->root.u.def.section;
9637 if (input_sec->output_section != NULL)
9638 {
9639 sym.st_shndx =
8b127cbc 9640 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9641 input_sec->output_section);
9642 if (sym.st_shndx == SHN_BAD)
9643 {
4eca0228 9644 _bfd_error_handler
695344c0 9645 /* xgettext:c-format */
d003868e 9646 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9647 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9648 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9649 eoinfo->failed = TRUE;
9650 return FALSE;
9651 }
9652
9653 /* ELF symbols in relocatable files are section relative,
9654 but in nonrelocatable files they are virtual
9655 addresses. */
9656 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9657 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9658 {
9659 sym.st_value += input_sec->output_section->vma;
9660 if (h->type == STT_TLS)
9661 {
8b127cbc 9662 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9663 if (tls_sec != NULL)
9664 sym.st_value -= tls_sec->vma;
c152c796
AM
9665 }
9666 }
9667 }
9668 else
9669 {
9670 BFD_ASSERT (input_sec->owner == NULL
9671 || (input_sec->owner->flags & DYNAMIC) != 0);
9672 sym.st_shndx = SHN_UNDEF;
9673 input_sec = bfd_und_section_ptr;
9674 }
9675 }
9676 break;
9677
9678 case bfd_link_hash_common:
9679 input_sec = h->root.u.c.p->section;
a4d8e49b 9680 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9681 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9682 break;
9683
9684 case bfd_link_hash_indirect:
9685 /* These symbols are created by symbol versioning. They point
9686 to the decorated version of the name. For example, if the
9687 symbol foo@@GNU_1.2 is the default, which should be used when
9688 foo is used with no version, then we add an indirect symbol
9689 foo which points to foo@@GNU_1.2. We ignore these symbols,
9690 since the indirected symbol is already in the hash table. */
9691 return TRUE;
9692 }
9693
b8871f35
L
9694 if (type == STT_COMMON || type == STT_OBJECT)
9695 switch (h->root.type)
9696 {
9697 case bfd_link_hash_common:
9698 type = elf_link_convert_common_type (flinfo->info, type);
9699 break;
9700 case bfd_link_hash_defined:
9701 case bfd_link_hash_defweak:
9702 if (bed->common_definition (&sym))
9703 type = elf_link_convert_common_type (flinfo->info, type);
9704 else
9705 type = STT_OBJECT;
9706 break;
9707 case bfd_link_hash_undefined:
9708 case bfd_link_hash_undefweak:
9709 break;
9710 default:
9711 abort ();
9712 }
9713
4deb8f71 9714 if (h->forced_local)
b8871f35
L
9715 {
9716 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9717 /* Turn off visibility on local symbol. */
9718 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9719 }
9720 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9721 else if (h->unique_global && h->def_regular)
9722 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9723 else if (h->root.type == bfd_link_hash_undefweak
9724 || h->root.type == bfd_link_hash_defweak)
9725 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9726 else
9727 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9728 sym.st_target_internal = h->target_internal;
9729
c152c796
AM
9730 /* Give the processor backend a chance to tweak the symbol value,
9731 and also to finish up anything that needs to be done for this
9732 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9733 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9734 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9735 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9736 && h->def_regular
0e1862bb 9737 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9738 || ((h->dynindx != -1
9739 || h->forced_local)
0e1862bb 9740 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9741 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9742 || h->root.type != bfd_link_hash_undefweak))
9743 || !h->forced_local)
8b127cbc 9744 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9745 {
9746 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9747 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9748 {
9749 eoinfo->failed = TRUE;
9750 return FALSE;
9751 }
9752 }
9753
9754 /* If we are marking the symbol as undefined, and there are no
9755 non-weak references to this symbol from a regular object, then
9756 mark the symbol as weak undefined; if there are non-weak
9757 references, mark the symbol as strong. We can't do this earlier,
9758 because it might not be marked as undefined until the
9759 finish_dynamic_symbol routine gets through with it. */
9760 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9761 && h->ref_regular
c152c796
AM
9762 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9763 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9764 {
9765 int bindtype;
b8871f35 9766 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9767
9768 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9769 if (type == STT_GNU_IFUNC)
9770 type = STT_FUNC;
c152c796 9771
f5385ebf 9772 if (h->ref_regular_nonweak)
c152c796
AM
9773 bindtype = STB_GLOBAL;
9774 else
9775 bindtype = STB_WEAK;
2955ec4c 9776 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9777 }
9778
bda987c2
CD
9779 /* If this is a symbol defined in a dynamic library, don't use the
9780 symbol size from the dynamic library. Relinking an executable
9781 against a new library may introduce gratuitous changes in the
9782 executable's symbols if we keep the size. */
9783 if (sym.st_shndx == SHN_UNDEF
9784 && !h->def_regular
9785 && h->def_dynamic)
9786 sym.st_size = 0;
9787
c152c796
AM
9788 /* If a non-weak symbol with non-default visibility is not defined
9789 locally, it is a fatal error. */
0e1862bb 9790 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9791 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9792 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9793 && h->root.type == bfd_link_hash_undefined
f5385ebf 9794 && !h->def_regular)
c152c796 9795 {
17d078c5
AM
9796 const char *msg;
9797
9798 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9799 /* xgettext:c-format */
17d078c5
AM
9800 msg = _("%B: protected symbol `%s' isn't defined");
9801 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9802 /* xgettext:c-format */
17d078c5
AM
9803 msg = _("%B: internal symbol `%s' isn't defined");
9804 else
695344c0 9805 /* xgettext:c-format */
17d078c5 9806 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9807 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9808 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9809 eoinfo->failed = TRUE;
9810 return FALSE;
9811 }
9812
9813 /* If this symbol should be put in the .dynsym section, then put it
9814 there now. We already know the symbol index. We also fill in
9815 the entry in the .hash section. */
cae1fbbb 9816 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9817 && h->dynindx != -1
8b127cbc 9818 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9819 {
c152c796
AM
9820 bfd_byte *esym;
9821
90c984fc
L
9822 /* Since there is no version information in the dynamic string,
9823 if there is no version info in symbol version section, we will
1659f720 9824 have a run-time problem if not linking executable, referenced
4deb8f71 9825 by shared library, or not bound locally. */
1659f720 9826 if (h->verinfo.verdef == NULL
0e1862bb 9827 && (!bfd_link_executable (flinfo->info)
1659f720
L
9828 || h->ref_dynamic
9829 || !h->def_regular))
90c984fc
L
9830 {
9831 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9832
9833 if (p && p [1] != '\0')
9834 {
4eca0228 9835 _bfd_error_handler
695344c0 9836 /* xgettext:c-format */
90c984fc
L
9837 (_("%B: No symbol version section for versioned symbol `%s'"),
9838 flinfo->output_bfd, h->root.root.string);
9839 eoinfo->failed = TRUE;
9840 return FALSE;
9841 }
9842 }
9843
c152c796 9844 sym.st_name = h->dynstr_index;
cae1fbbb
L
9845 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9846 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9847 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9848 {
9849 eoinfo->failed = TRUE;
9850 return FALSE;
9851 }
8b127cbc 9852 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9853
8b127cbc 9854 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9855 {
9856 size_t hash_entry_size;
9857 bfd_byte *bucketpos;
9858 bfd_vma chain;
41198d0c
L
9859 size_t bucketcount;
9860 size_t bucket;
9861
8b127cbc 9862 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9863 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9864
9865 hash_entry_size
8b127cbc
AM
9866 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9867 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9868 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9869 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9870 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9871 bucketpos);
9872 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9873 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9874 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9875 }
c152c796 9876
8b127cbc 9877 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9878 {
9879 Elf_Internal_Versym iversym;
9880 Elf_External_Versym *eversym;
9881
f5385ebf 9882 if (!h->def_regular)
c152c796 9883 {
7b20f099
AM
9884 if (h->verinfo.verdef == NULL
9885 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9886 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9887 iversym.vs_vers = 0;
9888 else
9889 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9890 }
9891 else
9892 {
9893 if (h->verinfo.vertree == NULL)
9894 iversym.vs_vers = 1;
9895 else
9896 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9897 if (flinfo->info->create_default_symver)
3e3b46e5 9898 iversym.vs_vers++;
c152c796
AM
9899 }
9900
422f1182 9901 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9902 defined locally. */
422f1182 9903 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9904 iversym.vs_vers |= VERSYM_HIDDEN;
9905
8b127cbc 9906 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9907 eversym += h->dynindx;
8b127cbc 9908 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9909 }
9910 }
9911
d983c8c5
AM
9912 /* If the symbol is undefined, and we didn't output it to .dynsym,
9913 strip it from .symtab too. Obviously we can't do this for
9914 relocatable output or when needed for --emit-relocs. */
9915 else if (input_sec == bfd_und_section_ptr
9916 && h->indx != -2
0e1862bb 9917 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9918 return TRUE;
9919 /* Also strip others that we couldn't earlier due to dynamic symbol
9920 processing. */
9921 if (strip)
9922 return TRUE;
9923 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9924 return TRUE;
9925
2ec55de3
AM
9926 /* Output a FILE symbol so that following locals are not associated
9927 with the wrong input file. We need one for forced local symbols
9928 if we've seen more than one FILE symbol or when we have exactly
9929 one FILE symbol but global symbols are present in a file other
9930 than the one with the FILE symbol. We also need one if linker
9931 defined symbols are present. In practice these conditions are
9932 always met, so just emit the FILE symbol unconditionally. */
9933 if (eoinfo->localsyms
9934 && !eoinfo->file_sym_done
9935 && eoinfo->flinfo->filesym_count != 0)
9936 {
9937 Elf_Internal_Sym fsym;
9938
9939 memset (&fsym, 0, sizeof (fsym));
9940 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9941 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9942 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9943 bfd_und_section_ptr, NULL))
2ec55de3
AM
9944 return FALSE;
9945
9946 eoinfo->file_sym_done = TRUE;
9947 }
9948
8b127cbc 9949 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9950 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9951 input_sec, h);
6e0b88f1 9952 if (ret == 0)
c152c796
AM
9953 {
9954 eoinfo->failed = TRUE;
9955 return FALSE;
9956 }
6e0b88f1
AM
9957 else if (ret == 1)
9958 h->indx = indx;
9959 else if (h->indx == -2)
9960 abort();
c152c796
AM
9961
9962 return TRUE;
9963}
9964
cdd3575c
AM
9965/* Return TRUE if special handling is done for relocs in SEC against
9966 symbols defined in discarded sections. */
9967
c152c796
AM
9968static bfd_boolean
9969elf_section_ignore_discarded_relocs (asection *sec)
9970{
9971 const struct elf_backend_data *bed;
9972
cdd3575c
AM
9973 switch (sec->sec_info_type)
9974 {
dbaa2011
AM
9975 case SEC_INFO_TYPE_STABS:
9976 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9977 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9978 return TRUE;
9979 default:
9980 break;
9981 }
c152c796
AM
9982
9983 bed = get_elf_backend_data (sec->owner);
9984 if (bed->elf_backend_ignore_discarded_relocs != NULL
9985 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9986 return TRUE;
9987
9988 return FALSE;
9989}
9990
9e66c942
AM
9991/* Return a mask saying how ld should treat relocations in SEC against
9992 symbols defined in discarded sections. If this function returns
9993 COMPLAIN set, ld will issue a warning message. If this function
9994 returns PRETEND set, and the discarded section was link-once and the
9995 same size as the kept link-once section, ld will pretend that the
9996 symbol was actually defined in the kept section. Otherwise ld will
9997 zero the reloc (at least that is the intent, but some cooperation by
9998 the target dependent code is needed, particularly for REL targets). */
9999
8a696751
AM
10000unsigned int
10001_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10002{
9e66c942 10003 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10004 return PRETEND;
cdd3575c
AM
10005
10006 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10007 return 0;
cdd3575c
AM
10008
10009 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10010 return 0;
cdd3575c 10011
9e66c942 10012 return COMPLAIN | PRETEND;
cdd3575c
AM
10013}
10014
3d7f7666
L
10015/* Find a match between a section and a member of a section group. */
10016
10017static asection *
c0f00686
L
10018match_group_member (asection *sec, asection *group,
10019 struct bfd_link_info *info)
3d7f7666
L
10020{
10021 asection *first = elf_next_in_group (group);
10022 asection *s = first;
10023
10024 while (s != NULL)
10025 {
c0f00686 10026 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10027 return s;
10028
83180ade 10029 s = elf_next_in_group (s);
3d7f7666
L
10030 if (s == first)
10031 break;
10032 }
10033
10034 return NULL;
10035}
10036
01b3c8ab 10037/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10038 to replace it. Return the replacement if it is OK. Otherwise return
10039 NULL. */
01b3c8ab
L
10040
10041asection *
c0f00686 10042_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10043{
10044 asection *kept;
10045
10046 kept = sec->kept_section;
10047 if (kept != NULL)
10048 {
c2370991 10049 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10050 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10051 if (kept != NULL
10052 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10053 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10054 kept = NULL;
c2370991 10055 sec->kept_section = kept;
01b3c8ab
L
10056 }
10057 return kept;
10058}
10059
c152c796
AM
10060/* Link an input file into the linker output file. This function
10061 handles all the sections and relocations of the input file at once.
10062 This is so that we only have to read the local symbols once, and
10063 don't have to keep them in memory. */
10064
10065static bfd_boolean
8b127cbc 10066elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10067{
ece5ef60 10068 int (*relocate_section)
c152c796
AM
10069 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10070 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10071 bfd *output_bfd;
10072 Elf_Internal_Shdr *symtab_hdr;
10073 size_t locsymcount;
10074 size_t extsymoff;
10075 Elf_Internal_Sym *isymbuf;
10076 Elf_Internal_Sym *isym;
10077 Elf_Internal_Sym *isymend;
10078 long *pindex;
10079 asection **ppsection;
10080 asection *o;
10081 const struct elf_backend_data *bed;
c152c796 10082 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10083 bfd_size_type address_size;
10084 bfd_vma r_type_mask;
10085 int r_sym_shift;
ffbc01cc 10086 bfd_boolean have_file_sym = FALSE;
c152c796 10087
8b127cbc 10088 output_bfd = flinfo->output_bfd;
c152c796
AM
10089 bed = get_elf_backend_data (output_bfd);
10090 relocate_section = bed->elf_backend_relocate_section;
10091
10092 /* If this is a dynamic object, we don't want to do anything here:
10093 we don't want the local symbols, and we don't want the section
10094 contents. */
10095 if ((input_bfd->flags & DYNAMIC) != 0)
10096 return TRUE;
10097
c152c796
AM
10098 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10099 if (elf_bad_symtab (input_bfd))
10100 {
10101 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10102 extsymoff = 0;
10103 }
10104 else
10105 {
10106 locsymcount = symtab_hdr->sh_info;
10107 extsymoff = symtab_hdr->sh_info;
10108 }
10109
10110 /* Read the local symbols. */
10111 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10112 if (isymbuf == NULL && locsymcount != 0)
10113 {
10114 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10115 flinfo->internal_syms,
10116 flinfo->external_syms,
10117 flinfo->locsym_shndx);
c152c796
AM
10118 if (isymbuf == NULL)
10119 return FALSE;
10120 }
10121
10122 /* Find local symbol sections and adjust values of symbols in
10123 SEC_MERGE sections. Write out those local symbols we know are
10124 going into the output file. */
10125 isymend = isymbuf + locsymcount;
8b127cbc 10126 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10127 isym < isymend;
10128 isym++, pindex++, ppsection++)
10129 {
10130 asection *isec;
10131 const char *name;
10132 Elf_Internal_Sym osym;
6e0b88f1
AM
10133 long indx;
10134 int ret;
c152c796
AM
10135
10136 *pindex = -1;
10137
10138 if (elf_bad_symtab (input_bfd))
10139 {
10140 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10141 {
10142 *ppsection = NULL;
10143 continue;
10144 }
10145 }
10146
10147 if (isym->st_shndx == SHN_UNDEF)
10148 isec = bfd_und_section_ptr;
c152c796
AM
10149 else if (isym->st_shndx == SHN_ABS)
10150 isec = bfd_abs_section_ptr;
10151 else if (isym->st_shndx == SHN_COMMON)
10152 isec = bfd_com_section_ptr;
10153 else
10154 {
cb33740c
AM
10155 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10156 if (isec == NULL)
10157 {
10158 /* Don't attempt to output symbols with st_shnx in the
10159 reserved range other than SHN_ABS and SHN_COMMON. */
10160 *ppsection = NULL;
10161 continue;
10162 }
dbaa2011 10163 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10164 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10165 isym->st_value =
10166 _bfd_merged_section_offset (output_bfd, &isec,
10167 elf_section_data (isec)->sec_info,
10168 isym->st_value);
c152c796
AM
10169 }
10170
10171 *ppsection = isec;
10172
d983c8c5
AM
10173 /* Don't output the first, undefined, symbol. In fact, don't
10174 output any undefined local symbol. */
10175 if (isec == bfd_und_section_ptr)
c152c796
AM
10176 continue;
10177
10178 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10179 {
10180 /* We never output section symbols. Instead, we use the
10181 section symbol of the corresponding section in the output
10182 file. */
10183 continue;
10184 }
10185
10186 /* If we are stripping all symbols, we don't want to output this
10187 one. */
8b127cbc 10188 if (flinfo->info->strip == strip_all)
c152c796
AM
10189 continue;
10190
10191 /* If we are discarding all local symbols, we don't want to
10192 output this one. If we are generating a relocatable output
10193 file, then some of the local symbols may be required by
10194 relocs; we output them below as we discover that they are
10195 needed. */
8b127cbc 10196 if (flinfo->info->discard == discard_all)
c152c796
AM
10197 continue;
10198
10199 /* If this symbol is defined in a section which we are
f02571c5
AM
10200 discarding, we don't need to keep it. */
10201 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10202 && isym->st_shndx < SHN_LORESERVE
10203 && bfd_section_removed_from_list (output_bfd,
10204 isec->output_section))
e75a280b
L
10205 continue;
10206
c152c796
AM
10207 /* Get the name of the symbol. */
10208 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10209 isym->st_name);
10210 if (name == NULL)
10211 return FALSE;
10212
10213 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10214 if ((flinfo->info->strip == strip_some
10215 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10216 == NULL))
8b127cbc 10217 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10218 && (isec->flags & SEC_MERGE)
10219 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10220 || flinfo->info->discard == discard_l)
c152c796
AM
10221 && bfd_is_local_label_name (input_bfd, name)))
10222 continue;
10223
ffbc01cc
AM
10224 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10225 {
ce875075
AM
10226 if (input_bfd->lto_output)
10227 /* -flto puts a temp file name here. This means builds
10228 are not reproducible. Discard the symbol. */
10229 continue;
ffbc01cc
AM
10230 have_file_sym = TRUE;
10231 flinfo->filesym_count += 1;
10232 }
10233 if (!have_file_sym)
10234 {
10235 /* In the absence of debug info, bfd_find_nearest_line uses
10236 FILE symbols to determine the source file for local
10237 function symbols. Provide a FILE symbol here if input
10238 files lack such, so that their symbols won't be
10239 associated with a previous input file. It's not the
10240 source file, but the best we can do. */
10241 have_file_sym = TRUE;
10242 flinfo->filesym_count += 1;
10243 memset (&osym, 0, sizeof (osym));
10244 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10245 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10246 if (!elf_link_output_symstrtab (flinfo,
10247 (input_bfd->lto_output ? NULL
10248 : input_bfd->filename),
10249 &osym, bfd_abs_section_ptr,
10250 NULL))
ffbc01cc
AM
10251 return FALSE;
10252 }
10253
c152c796
AM
10254 osym = *isym;
10255
10256 /* Adjust the section index for the output file. */
10257 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10258 isec->output_section);
10259 if (osym.st_shndx == SHN_BAD)
10260 return FALSE;
10261
c152c796
AM
10262 /* ELF symbols in relocatable files are section relative, but
10263 in executable files they are virtual addresses. Note that
10264 this code assumes that all ELF sections have an associated
10265 BFD section with a reasonable value for output_offset; below
10266 we assume that they also have a reasonable value for
10267 output_section. Any special sections must be set up to meet
10268 these requirements. */
10269 osym.st_value += isec->output_offset;
0e1862bb 10270 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10271 {
10272 osym.st_value += isec->output_section->vma;
10273 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10274 {
10275 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10276 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10277 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10278 }
10279 }
10280
6e0b88f1 10281 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10282 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10283 if (ret == 0)
c152c796 10284 return FALSE;
6e0b88f1
AM
10285 else if (ret == 1)
10286 *pindex = indx;
c152c796
AM
10287 }
10288
310fd250
L
10289 if (bed->s->arch_size == 32)
10290 {
10291 r_type_mask = 0xff;
10292 r_sym_shift = 8;
10293 address_size = 4;
10294 }
10295 else
10296 {
10297 r_type_mask = 0xffffffff;
10298 r_sym_shift = 32;
10299 address_size = 8;
10300 }
10301
c152c796
AM
10302 /* Relocate the contents of each section. */
10303 sym_hashes = elf_sym_hashes (input_bfd);
10304 for (o = input_bfd->sections; o != NULL; o = o->next)
10305 {
10306 bfd_byte *contents;
10307
10308 if (! o->linker_mark)
10309 {
10310 /* This section was omitted from the link. */
10311 continue;
10312 }
10313
7bdf4127 10314 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10315 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10316 {
10317 /* Deal with the group signature symbol. */
10318 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10319 unsigned long symndx = sec_data->this_hdr.sh_info;
10320 asection *osec = o->output_section;
10321
7bdf4127 10322 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10323 if (symndx >= locsymcount
10324 || (elf_bad_symtab (input_bfd)
8b127cbc 10325 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10326 {
10327 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10328 while (h->root.type == bfd_link_hash_indirect
10329 || h->root.type == bfd_link_hash_warning)
10330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10331 /* Arrange for symbol to be output. */
10332 h->indx = -2;
10333 elf_section_data (osec)->this_hdr.sh_info = -2;
10334 }
10335 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10336 {
10337 /* We'll use the output section target_index. */
8b127cbc 10338 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10339 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10340 }
10341 else
10342 {
8b127cbc 10343 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10344 {
10345 /* Otherwise output the local symbol now. */
10346 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10347 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10348 const char *name;
6e0b88f1
AM
10349 long indx;
10350 int ret;
bcacc0f5
AM
10351
10352 name = bfd_elf_string_from_elf_section (input_bfd,
10353 symtab_hdr->sh_link,
10354 sym.st_name);
10355 if (name == NULL)
10356 return FALSE;
10357
10358 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10359 sec);
10360 if (sym.st_shndx == SHN_BAD)
10361 return FALSE;
10362
10363 sym.st_value += o->output_offset;
10364
6e0b88f1 10365 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10366 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10367 NULL);
6e0b88f1 10368 if (ret == 0)
bcacc0f5 10369 return FALSE;
6e0b88f1 10370 else if (ret == 1)
8b127cbc 10371 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10372 else
10373 abort ();
bcacc0f5
AM
10374 }
10375 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10376 = flinfo->indices[symndx];
bcacc0f5
AM
10377 }
10378 }
10379
c152c796 10380 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10381 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10382 continue;
10383
10384 if ((o->flags & SEC_LINKER_CREATED) != 0)
10385 {
10386 /* Section was created by _bfd_elf_link_create_dynamic_sections
10387 or somesuch. */
10388 continue;
10389 }
10390
10391 /* Get the contents of the section. They have been cached by a
10392 relaxation routine. Note that o is a section in an input
10393 file, so the contents field will not have been set by any of
10394 the routines which work on output files. */
10395 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10396 {
10397 contents = elf_section_data (o)->this_hdr.contents;
10398 if (bed->caches_rawsize
10399 && o->rawsize != 0
10400 && o->rawsize < o->size)
10401 {
10402 memcpy (flinfo->contents, contents, o->rawsize);
10403 contents = flinfo->contents;
10404 }
10405 }
c152c796
AM
10406 else
10407 {
8b127cbc 10408 contents = flinfo->contents;
4a114e3e 10409 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10410 return FALSE;
10411 }
10412
10413 if ((o->flags & SEC_RELOC) != 0)
10414 {
10415 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10416 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10417 int action_discarded;
ece5ef60 10418 int ret;
c152c796
AM
10419
10420 /* Get the swapped relocs. */
10421 internal_relocs
8b127cbc
AM
10422 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10423 flinfo->internal_relocs, FALSE);
c152c796
AM
10424 if (internal_relocs == NULL
10425 && o->reloc_count > 0)
10426 return FALSE;
10427
310fd250
L
10428 /* We need to reverse-copy input .ctors/.dtors sections if
10429 they are placed in .init_array/.finit_array for output. */
10430 if (o->size > address_size
10431 && ((strncmp (o->name, ".ctors", 6) == 0
10432 && strcmp (o->output_section->name,
10433 ".init_array") == 0)
10434 || (strncmp (o->name, ".dtors", 6) == 0
10435 && strcmp (o->output_section->name,
10436 ".fini_array") == 0))
10437 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10438 {
056bafd4
MR
10439 if (o->size * bed->s->int_rels_per_ext_rel
10440 != o->reloc_count * address_size)
310fd250 10441 {
4eca0228 10442 _bfd_error_handler
695344c0 10443 /* xgettext:c-format */
310fd250
L
10444 (_("error: %B: size of section %A is not "
10445 "multiple of address size"),
10446 input_bfd, o);
8c6716e5 10447 bfd_set_error (bfd_error_bad_value);
310fd250
L
10448 return FALSE;
10449 }
10450 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10451 }
10452
0f02bbd9 10453 action_discarded = -1;
c152c796 10454 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10455 action_discarded = (*bed->action_discarded) (o);
10456
10457 /* Run through the relocs evaluating complex reloc symbols and
10458 looking for relocs against symbols from discarded sections
10459 or section symbols from removed link-once sections.
10460 Complain about relocs against discarded sections. Zero
10461 relocs against removed link-once sections. */
10462
10463 rel = internal_relocs;
056bafd4 10464 relend = rel + o->reloc_count;
0f02bbd9 10465 for ( ; rel < relend; rel++)
c152c796 10466 {
0f02bbd9
AM
10467 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10468 unsigned int s_type;
10469 asection **ps, *sec;
10470 struct elf_link_hash_entry *h = NULL;
10471 const char *sym_name;
c152c796 10472
0f02bbd9
AM
10473 if (r_symndx == STN_UNDEF)
10474 continue;
c152c796 10475
0f02bbd9
AM
10476 if (r_symndx >= locsymcount
10477 || (elf_bad_symtab (input_bfd)
8b127cbc 10478 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10479 {
10480 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10481
0f02bbd9
AM
10482 /* Badly formatted input files can contain relocs that
10483 reference non-existant symbols. Check here so that
10484 we do not seg fault. */
10485 if (h == NULL)
c152c796 10486 {
4eca0228 10487 _bfd_error_handler
695344c0 10488 /* xgettext:c-format */
76cfced5 10489 (_("error: %B contains a reloc (%#Lx) for section %A "
0f02bbd9 10490 "that references a non-existent global symbol"),
76cfced5 10491 input_bfd, rel->r_info, o);
0f02bbd9
AM
10492 bfd_set_error (bfd_error_bad_value);
10493 return FALSE;
10494 }
3b36f7e6 10495
0f02bbd9
AM
10496 while (h->root.type == bfd_link_hash_indirect
10497 || h->root.type == bfd_link_hash_warning)
10498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10499
0f02bbd9 10500 s_type = h->type;
cdd3575c 10501
9e2dec47 10502 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10503 mark the symbol as undefined. Note that the
10504 linker may attach linker created dynamic sections
10505 to the plugin bfd. Symbols defined in linker
10506 created sections are not plugin symbols. */
bc4e12de 10507 if ((h->root.non_ir_ref_regular
4070765b 10508 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10509 && (h->root.type == bfd_link_hash_defined
10510 || h->root.type == bfd_link_hash_defweak)
10511 && (h->root.u.def.section->flags
10512 & SEC_LINKER_CREATED) == 0
10513 && h->root.u.def.section->owner != NULL
10514 && (h->root.u.def.section->owner->flags
10515 & BFD_PLUGIN) != 0)
10516 {
10517 h->root.type = bfd_link_hash_undefined;
10518 h->root.u.undef.abfd = h->root.u.def.section->owner;
10519 }
10520
0f02bbd9
AM
10521 ps = NULL;
10522 if (h->root.type == bfd_link_hash_defined
10523 || h->root.type == bfd_link_hash_defweak)
10524 ps = &h->root.u.def.section;
10525
10526 sym_name = h->root.root.string;
10527 }
10528 else
10529 {
10530 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10531
10532 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10533 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10534 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10535 sym, *ps);
10536 }
c152c796 10537
c301e700 10538 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10539 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10540 {
10541 bfd_vma val;
10542 bfd_vma dot = (rel->r_offset
10543 + o->output_offset + o->output_section->vma);
10544#ifdef DEBUG
10545 printf ("Encountered a complex symbol!");
10546 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10547 input_bfd->filename, o->name,
10548 (long) (rel - internal_relocs));
0f02bbd9
AM
10549 printf (" symbol: idx %8.8lx, name %s\n",
10550 r_symndx, sym_name);
10551 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10552 (unsigned long) rel->r_info,
10553 (unsigned long) rel->r_offset);
10554#endif
8b127cbc 10555 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10556 isymbuf, locsymcount, s_type == STT_SRELC))
10557 return FALSE;
10558
10559 /* Symbol evaluated OK. Update to absolute value. */
10560 set_symbol_value (input_bfd, isymbuf, locsymcount,
10561 r_symndx, val);
10562 continue;
10563 }
10564
10565 if (action_discarded != -1 && ps != NULL)
10566 {
cdd3575c
AM
10567 /* Complain if the definition comes from a
10568 discarded section. */
dbaa2011 10569 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10570 {
cf35638d 10571 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10572 if (action_discarded & COMPLAIN)
8b127cbc 10573 (*flinfo->info->callbacks->einfo)
695344c0 10574 /* xgettext:c-format */
e1fffbe6 10575 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10576 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10577 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10578
87e5235d 10579 /* Try to do the best we can to support buggy old
e0ae6d6f 10580 versions of gcc. Pretend that the symbol is
87e5235d
AM
10581 really defined in the kept linkonce section.
10582 FIXME: This is quite broken. Modifying the
10583 symbol here means we will be changing all later
e0ae6d6f 10584 uses of the symbol, not just in this section. */
0f02bbd9 10585 if (action_discarded & PRETEND)
87e5235d 10586 {
01b3c8ab
L
10587 asection *kept;
10588
c0f00686 10589 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10590 flinfo->info);
01b3c8ab 10591 if (kept != NULL)
87e5235d
AM
10592 {
10593 *ps = kept;
10594 continue;
10595 }
10596 }
c152c796
AM
10597 }
10598 }
10599 }
10600
10601 /* Relocate the section by invoking a back end routine.
10602
10603 The back end routine is responsible for adjusting the
10604 section contents as necessary, and (if using Rela relocs
10605 and generating a relocatable output file) adjusting the
10606 reloc addend as necessary.
10607
10608 The back end routine does not have to worry about setting
10609 the reloc address or the reloc symbol index.
10610
10611 The back end routine is given a pointer to the swapped in
10612 internal symbols, and can access the hash table entries
10613 for the external symbols via elf_sym_hashes (input_bfd).
10614
10615 When generating relocatable output, the back end routine
10616 must handle STB_LOCAL/STT_SECTION symbols specially. The
10617 output symbol is going to be a section symbol
10618 corresponding to the output section, which will require
10619 the addend to be adjusted. */
10620
8b127cbc 10621 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10622 input_bfd, o, contents,
10623 internal_relocs,
10624 isymbuf,
8b127cbc 10625 flinfo->sections);
ece5ef60 10626 if (!ret)
c152c796
AM
10627 return FALSE;
10628
ece5ef60 10629 if (ret == 2
0e1862bb 10630 || bfd_link_relocatable (flinfo->info)
8b127cbc 10631 || flinfo->info->emitrelocations)
c152c796
AM
10632 {
10633 Elf_Internal_Rela *irela;
d4730f92 10634 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10635 bfd_vma last_offset;
10636 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10637 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10638 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10639 unsigned int next_erel;
c152c796 10640 bfd_boolean rela_normal;
d4730f92 10641 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10642
d4730f92
BS
10643 esdi = elf_section_data (o);
10644 esdo = elf_section_data (o->output_section);
10645 rela_normal = FALSE;
c152c796
AM
10646
10647 /* Adjust the reloc addresses and symbol indices. */
10648
10649 irela = internal_relocs;
056bafd4 10650 irelaend = irela + o->reloc_count;
d4730f92
BS
10651 rel_hash = esdo->rel.hashes + esdo->rel.count;
10652 /* We start processing the REL relocs, if any. When we reach
10653 IRELAMID in the loop, we switch to the RELA relocs. */
10654 irelamid = irela;
10655 if (esdi->rel.hdr != NULL)
10656 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10657 * bed->s->int_rels_per_ext_rel);
eac338cf 10658 rel_hash_list = rel_hash;
d4730f92 10659 rela_hash_list = NULL;
c152c796 10660 last_offset = o->output_offset;
0e1862bb 10661 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10662 last_offset += o->output_section->vma;
10663 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10664 {
10665 unsigned long r_symndx;
10666 asection *sec;
10667 Elf_Internal_Sym sym;
10668
10669 if (next_erel == bed->s->int_rels_per_ext_rel)
10670 {
10671 rel_hash++;
10672 next_erel = 0;
10673 }
10674
d4730f92
BS
10675 if (irela == irelamid)
10676 {
10677 rel_hash = esdo->rela.hashes + esdo->rela.count;
10678 rela_hash_list = rel_hash;
10679 rela_normal = bed->rela_normal;
10680 }
10681
c152c796 10682 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10683 flinfo->info, o,
c152c796
AM
10684 irela->r_offset);
10685 if (irela->r_offset >= (bfd_vma) -2)
10686 {
10687 /* This is a reloc for a deleted entry or somesuch.
10688 Turn it into an R_*_NONE reloc, at the same
10689 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10690 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10691 being ordered. */
10692 irela->r_offset = last_offset;
10693 irela->r_info = 0;
10694 irela->r_addend = 0;
10695 continue;
10696 }
10697
10698 irela->r_offset += o->output_offset;
10699
10700 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10701 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10702 irela->r_offset += o->output_section->vma;
10703
10704 last_offset = irela->r_offset;
10705
10706 r_symndx = irela->r_info >> r_sym_shift;
10707 if (r_symndx == STN_UNDEF)
10708 continue;
10709
10710 if (r_symndx >= locsymcount
10711 || (elf_bad_symtab (input_bfd)
8b127cbc 10712 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10713 {
10714 struct elf_link_hash_entry *rh;
10715 unsigned long indx;
10716
10717 /* This is a reloc against a global symbol. We
10718 have not yet output all the local symbols, so
10719 we do not know the symbol index of any global
10720 symbol. We set the rel_hash entry for this
10721 reloc to point to the global hash table entry
10722 for this symbol. The symbol index is then
ee75fd95 10723 set at the end of bfd_elf_final_link. */
c152c796
AM
10724 indx = r_symndx - extsymoff;
10725 rh = elf_sym_hashes (input_bfd)[indx];
10726 while (rh->root.type == bfd_link_hash_indirect
10727 || rh->root.type == bfd_link_hash_warning)
10728 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10729
10730 /* Setting the index to -2 tells
10731 elf_link_output_extsym that this symbol is
10732 used by a reloc. */
10733 BFD_ASSERT (rh->indx < 0);
10734 rh->indx = -2;
c152c796
AM
10735 *rel_hash = rh;
10736
10737 continue;
10738 }
10739
10740 /* This is a reloc against a local symbol. */
10741
10742 *rel_hash = NULL;
10743 sym = isymbuf[r_symndx];
8b127cbc 10744 sec = flinfo->sections[r_symndx];
c152c796
AM
10745 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10746 {
10747 /* I suppose the backend ought to fill in the
10748 section of any STT_SECTION symbol against a
6a8d1586 10749 processor specific section. */
cf35638d 10750 r_symndx = STN_UNDEF;
6a8d1586
AM
10751 if (bfd_is_abs_section (sec))
10752 ;
c152c796
AM
10753 else if (sec == NULL || sec->owner == NULL)
10754 {
10755 bfd_set_error (bfd_error_bad_value);
10756 return FALSE;
10757 }
10758 else
10759 {
6a8d1586
AM
10760 asection *osec = sec->output_section;
10761
10762 /* If we have discarded a section, the output
10763 section will be the absolute section. In
ab96bf03
AM
10764 case of discarded SEC_MERGE sections, use
10765 the kept section. relocate_section should
10766 have already handled discarded linkonce
10767 sections. */
6a8d1586
AM
10768 if (bfd_is_abs_section (osec)
10769 && sec->kept_section != NULL
10770 && sec->kept_section->output_section != NULL)
10771 {
10772 osec = sec->kept_section->output_section;
10773 irela->r_addend -= osec->vma;
10774 }
10775
10776 if (!bfd_is_abs_section (osec))
10777 {
10778 r_symndx = osec->target_index;
cf35638d 10779 if (r_symndx == STN_UNDEF)
74541ad4 10780 {
051d833a
AM
10781 irela->r_addend += osec->vma;
10782 osec = _bfd_nearby_section (output_bfd, osec,
10783 osec->vma);
10784 irela->r_addend -= osec->vma;
10785 r_symndx = osec->target_index;
74541ad4 10786 }
6a8d1586 10787 }
c152c796
AM
10788 }
10789
10790 /* Adjust the addend according to where the
10791 section winds up in the output section. */
10792 if (rela_normal)
10793 irela->r_addend += sec->output_offset;
10794 }
10795 else
10796 {
8b127cbc 10797 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10798 {
10799 unsigned long shlink;
10800 const char *name;
10801 asection *osec;
6e0b88f1 10802 long indx;
c152c796 10803
8b127cbc 10804 if (flinfo->info->strip == strip_all)
c152c796
AM
10805 {
10806 /* You can't do ld -r -s. */
10807 bfd_set_error (bfd_error_invalid_operation);
10808 return FALSE;
10809 }
10810
10811 /* This symbol was skipped earlier, but
10812 since it is needed by a reloc, we
10813 must output it now. */
10814 shlink = symtab_hdr->sh_link;
10815 name = (bfd_elf_string_from_elf_section
10816 (input_bfd, shlink, sym.st_name));
10817 if (name == NULL)
10818 return FALSE;
10819
10820 osec = sec->output_section;
10821 sym.st_shndx =
10822 _bfd_elf_section_from_bfd_section (output_bfd,
10823 osec);
10824 if (sym.st_shndx == SHN_BAD)
10825 return FALSE;
10826
10827 sym.st_value += sec->output_offset;
0e1862bb 10828 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10829 {
10830 sym.st_value += osec->vma;
10831 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10832 {
10833 /* STT_TLS symbols are relative to PT_TLS
10834 segment base. */
8b127cbc 10835 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10836 ->tls_sec != NULL);
8b127cbc 10837 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10838 ->tls_sec->vma);
10839 }
10840 }
10841
6e0b88f1 10842 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10843 ret = elf_link_output_symstrtab (flinfo, name,
10844 &sym, sec,
10845 NULL);
6e0b88f1 10846 if (ret == 0)
c152c796 10847 return FALSE;
6e0b88f1 10848 else if (ret == 1)
8b127cbc 10849 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10850 else
10851 abort ();
c152c796
AM
10852 }
10853
8b127cbc 10854 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10855 }
10856
10857 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10858 | (irela->r_info & r_type_mask));
10859 }
10860
10861 /* Swap out the relocs. */
d4730f92
BS
10862 input_rel_hdr = esdi->rel.hdr;
10863 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10864 {
d4730f92
BS
10865 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10866 input_rel_hdr,
10867 internal_relocs,
10868 rel_hash_list))
10869 return FALSE;
c152c796
AM
10870 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10871 * bed->s->int_rels_per_ext_rel);
eac338cf 10872 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10873 }
10874
10875 input_rela_hdr = esdi->rela.hdr;
10876 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10877 {
eac338cf 10878 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10879 input_rela_hdr,
eac338cf 10880 internal_relocs,
d4730f92 10881 rela_hash_list))
c152c796
AM
10882 return FALSE;
10883 }
10884 }
10885 }
10886
10887 /* Write out the modified section contents. */
10888 if (bed->elf_backend_write_section
8b127cbc 10889 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10890 contents))
c152c796
AM
10891 {
10892 /* Section written out. */
10893 }
10894 else switch (o->sec_info_type)
10895 {
dbaa2011 10896 case SEC_INFO_TYPE_STABS:
c152c796
AM
10897 if (! (_bfd_write_section_stabs
10898 (output_bfd,
8b127cbc 10899 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10900 o, &elf_section_data (o)->sec_info, contents)))
10901 return FALSE;
10902 break;
dbaa2011 10903 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10904 if (! _bfd_write_merged_section (output_bfd, o,
10905 elf_section_data (o)->sec_info))
10906 return FALSE;
10907 break;
dbaa2011 10908 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10909 {
8b127cbc 10910 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10911 o, contents))
10912 return FALSE;
10913 }
10914 break;
2f0c68f2
CM
10915 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10916 {
10917 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10918 flinfo->info,
10919 o, contents))
10920 return FALSE;
10921 }
10922 break;
c152c796
AM
10923 default:
10924 {
310fd250
L
10925 if (! (o->flags & SEC_EXCLUDE))
10926 {
10927 file_ptr offset = (file_ptr) o->output_offset;
10928 bfd_size_type todo = o->size;
37b01f6a
DG
10929
10930 offset *= bfd_octets_per_byte (output_bfd);
10931
310fd250
L
10932 if ((o->flags & SEC_ELF_REVERSE_COPY))
10933 {
10934 /* Reverse-copy input section to output. */
10935 do
10936 {
10937 todo -= address_size;
10938 if (! bfd_set_section_contents (output_bfd,
10939 o->output_section,
10940 contents + todo,
10941 offset,
10942 address_size))
10943 return FALSE;
10944 if (todo == 0)
10945 break;
10946 offset += address_size;
10947 }
10948 while (1);
10949 }
10950 else if (! bfd_set_section_contents (output_bfd,
10951 o->output_section,
10952 contents,
10953 offset, todo))
10954 return FALSE;
10955 }
c152c796
AM
10956 }
10957 break;
10958 }
10959 }
10960
10961 return TRUE;
10962}
10963
10964/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10965 requested by the linker, and does not come from any input file. This
c152c796
AM
10966 is used to build constructor and destructor tables when linking
10967 with -Ur. */
10968
10969static bfd_boolean
10970elf_reloc_link_order (bfd *output_bfd,
10971 struct bfd_link_info *info,
10972 asection *output_section,
10973 struct bfd_link_order *link_order)
10974{
10975 reloc_howto_type *howto;
10976 long indx;
10977 bfd_vma offset;
10978 bfd_vma addend;
d4730f92 10979 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10980 struct elf_link_hash_entry **rel_hash_ptr;
10981 Elf_Internal_Shdr *rel_hdr;
10982 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10983 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10984 bfd_byte *erel;
10985 unsigned int i;
d4730f92 10986 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10987
10988 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10989 if (howto == NULL)
10990 {
10991 bfd_set_error (bfd_error_bad_value);
10992 return FALSE;
10993 }
10994
10995 addend = link_order->u.reloc.p->addend;
10996
d4730f92
BS
10997 if (esdo->rel.hdr)
10998 reldata = &esdo->rel;
10999 else if (esdo->rela.hdr)
11000 reldata = &esdo->rela;
11001 else
11002 {
11003 reldata = NULL;
11004 BFD_ASSERT (0);
11005 }
11006
c152c796 11007 /* Figure out the symbol index. */
d4730f92 11008 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11009 if (link_order->type == bfd_section_reloc_link_order)
11010 {
11011 indx = link_order->u.reloc.p->u.section->target_index;
11012 BFD_ASSERT (indx != 0);
11013 *rel_hash_ptr = NULL;
11014 }
11015 else
11016 {
11017 struct elf_link_hash_entry *h;
11018
11019 /* Treat a reloc against a defined symbol as though it were
11020 actually against the section. */
11021 h = ((struct elf_link_hash_entry *)
11022 bfd_wrapped_link_hash_lookup (output_bfd, info,
11023 link_order->u.reloc.p->u.name,
11024 FALSE, FALSE, TRUE));
11025 if (h != NULL
11026 && (h->root.type == bfd_link_hash_defined
11027 || h->root.type == bfd_link_hash_defweak))
11028 {
11029 asection *section;
11030
11031 section = h->root.u.def.section;
11032 indx = section->output_section->target_index;
11033 *rel_hash_ptr = NULL;
11034 /* It seems that we ought to add the symbol value to the
11035 addend here, but in practice it has already been added
11036 because it was passed to constructor_callback. */
11037 addend += section->output_section->vma + section->output_offset;
11038 }
11039 else if (h != NULL)
11040 {
11041 /* Setting the index to -2 tells elf_link_output_extsym that
11042 this symbol is used by a reloc. */
11043 h->indx = -2;
11044 *rel_hash_ptr = h;
11045 indx = 0;
11046 }
11047 else
11048 {
1a72702b
AM
11049 (*info->callbacks->unattached_reloc)
11050 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11051 indx = 0;
11052 }
11053 }
11054
11055 /* If this is an inplace reloc, we must write the addend into the
11056 object file. */
11057 if (howto->partial_inplace && addend != 0)
11058 {
11059 bfd_size_type size;
11060 bfd_reloc_status_type rstat;
11061 bfd_byte *buf;
11062 bfd_boolean ok;
11063 const char *sym_name;
11064
a50b1753
NC
11065 size = (bfd_size_type) bfd_get_reloc_size (howto);
11066 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11067 if (buf == NULL && size != 0)
c152c796
AM
11068 return FALSE;
11069 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11070 switch (rstat)
11071 {
11072 case bfd_reloc_ok:
11073 break;
11074
11075 default:
11076 case bfd_reloc_outofrange:
11077 abort ();
11078
11079 case bfd_reloc_overflow:
11080 if (link_order->type == bfd_section_reloc_link_order)
11081 sym_name = bfd_section_name (output_bfd,
11082 link_order->u.reloc.p->u.section);
11083 else
11084 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11085 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11086 howto->name, addend, NULL, NULL,
11087 (bfd_vma) 0);
c152c796
AM
11088 break;
11089 }
37b01f6a 11090
c152c796 11091 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11092 link_order->offset
11093 * bfd_octets_per_byte (output_bfd),
11094 size);
c152c796
AM
11095 free (buf);
11096 if (! ok)
11097 return FALSE;
11098 }
11099
11100 /* The address of a reloc is relative to the section in a
11101 relocatable file, and is a virtual address in an executable
11102 file. */
11103 offset = link_order->offset;
0e1862bb 11104 if (! bfd_link_relocatable (info))
c152c796
AM
11105 offset += output_section->vma;
11106
11107 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11108 {
11109 irel[i].r_offset = offset;
11110 irel[i].r_info = 0;
11111 irel[i].r_addend = 0;
11112 }
11113 if (bed->s->arch_size == 32)
11114 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11115 else
11116 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11117
d4730f92 11118 rel_hdr = reldata->hdr;
c152c796
AM
11119 erel = rel_hdr->contents;
11120 if (rel_hdr->sh_type == SHT_REL)
11121 {
d4730f92 11122 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11123 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11124 }
11125 else
11126 {
11127 irel[0].r_addend = addend;
d4730f92 11128 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11129 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11130 }
11131
d4730f92 11132 ++reldata->count;
c152c796
AM
11133
11134 return TRUE;
11135}
11136
0b52efa6
PB
11137
11138/* Get the output vma of the section pointed to by the sh_link field. */
11139
11140static bfd_vma
11141elf_get_linked_section_vma (struct bfd_link_order *p)
11142{
11143 Elf_Internal_Shdr **elf_shdrp;
11144 asection *s;
11145 int elfsec;
11146
11147 s = p->u.indirect.section;
11148 elf_shdrp = elf_elfsections (s->owner);
11149 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11150 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11151 /* PR 290:
11152 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11153 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11154 sh_info fields. Hence we could get the situation
11155 where elfsec is 0. */
11156 if (elfsec == 0)
11157 {
11158 const struct elf_backend_data *bed
11159 = get_elf_backend_data (s->owner);
11160 if (bed->link_order_error_handler)
d003868e 11161 bed->link_order_error_handler
695344c0 11162 /* xgettext:c-format */
d003868e 11163 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11164 return 0;
11165 }
11166 else
11167 {
11168 s = elf_shdrp[elfsec]->bfd_section;
11169 return s->output_section->vma + s->output_offset;
11170 }
0b52efa6
PB
11171}
11172
11173
11174/* Compare two sections based on the locations of the sections they are
11175 linked to. Used by elf_fixup_link_order. */
11176
11177static int
11178compare_link_order (const void * a, const void * b)
11179{
11180 bfd_vma apos;
11181 bfd_vma bpos;
11182
11183 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11184 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11185 if (apos < bpos)
11186 return -1;
11187 return apos > bpos;
11188}
11189
11190
11191/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11192 order as their linked sections. Returns false if this could not be done
11193 because an output section includes both ordered and unordered
11194 sections. Ideally we'd do this in the linker proper. */
11195
11196static bfd_boolean
11197elf_fixup_link_order (bfd *abfd, asection *o)
11198{
11199 int seen_linkorder;
11200 int seen_other;
11201 int n;
11202 struct bfd_link_order *p;
11203 bfd *sub;
11204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11205 unsigned elfsec;
0b52efa6 11206 struct bfd_link_order **sections;
d33cdfe3 11207 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11208 bfd_vma offset;
3b36f7e6 11209
d33cdfe3
L
11210 other_sec = NULL;
11211 linkorder_sec = NULL;
0b52efa6
PB
11212 seen_other = 0;
11213 seen_linkorder = 0;
8423293d 11214 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11215 {
d33cdfe3 11216 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11217 {
11218 s = p->u.indirect.section;
d33cdfe3
L
11219 sub = s->owner;
11220 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11221 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11222 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11223 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11224 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11225 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11226 {
11227 seen_linkorder++;
11228 linkorder_sec = s;
11229 }
0b52efa6 11230 else
d33cdfe3
L
11231 {
11232 seen_other++;
11233 other_sec = s;
11234 }
0b52efa6
PB
11235 }
11236 else
11237 seen_other++;
d33cdfe3
L
11238
11239 if (seen_other && seen_linkorder)
11240 {
11241 if (other_sec && linkorder_sec)
4eca0228 11242 _bfd_error_handler
695344c0 11243 /* xgettext:c-format */
4eca0228
AM
11244 (_("%A has both ordered [`%A' in %B] "
11245 "and unordered [`%A' in %B] sections"),
63a5468a
AM
11246 o, linkorder_sec, linkorder_sec->owner,
11247 other_sec, other_sec->owner);
d33cdfe3 11248 else
4eca0228
AM
11249 _bfd_error_handler
11250 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11251 bfd_set_error (bfd_error_bad_value);
11252 return FALSE;
11253 }
0b52efa6
PB
11254 }
11255
11256 if (!seen_linkorder)
11257 return TRUE;
11258
0b52efa6 11259 sections = (struct bfd_link_order **)
14b1c01e
AM
11260 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11261 if (sections == NULL)
11262 return FALSE;
0b52efa6 11263 seen_linkorder = 0;
3b36f7e6 11264
8423293d 11265 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11266 {
11267 sections[seen_linkorder++] = p;
11268 }
11269 /* Sort the input sections in the order of their linked section. */
11270 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11271 compare_link_order);
11272
11273 /* Change the offsets of the sections. */
11274 offset = 0;
11275 for (n = 0; n < seen_linkorder; n++)
11276 {
11277 s = sections[n]->u.indirect.section;
461686a3 11278 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11279 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11280 sections[n]->offset = offset;
11281 offset += sections[n]->size;
11282 }
11283
4dd07732 11284 free (sections);
0b52efa6
PB
11285 return TRUE;
11286}
11287
76359541
TP
11288/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11289 Returns TRUE upon success, FALSE otherwise. */
11290
11291static bfd_boolean
11292elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11293{
11294 bfd_boolean ret = FALSE;
11295 bfd *implib_bfd;
11296 const struct elf_backend_data *bed;
11297 flagword flags;
11298 enum bfd_architecture arch;
11299 unsigned int mach;
11300 asymbol **sympp = NULL;
11301 long symsize;
11302 long symcount;
11303 long src_count;
11304 elf_symbol_type *osymbuf;
11305
11306 implib_bfd = info->out_implib_bfd;
11307 bed = get_elf_backend_data (abfd);
11308
11309 if (!bfd_set_format (implib_bfd, bfd_object))
11310 return FALSE;
11311
046734ff 11312 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11313 flags = bfd_get_file_flags (abfd);
11314 flags &= ~HAS_RELOC;
11315 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11316 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11317 return FALSE;
11318
11319 /* Copy architecture of output file to import library file. */
11320 arch = bfd_get_arch (abfd);
11321 mach = bfd_get_mach (abfd);
11322 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11323 && (abfd->target_defaulted
11324 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11325 return FALSE;
11326
11327 /* Get symbol table size. */
11328 symsize = bfd_get_symtab_upper_bound (abfd);
11329 if (symsize < 0)
11330 return FALSE;
11331
11332 /* Read in the symbol table. */
11333 sympp = (asymbol **) xmalloc (symsize);
11334 symcount = bfd_canonicalize_symtab (abfd, sympp);
11335 if (symcount < 0)
11336 goto free_sym_buf;
11337
11338 /* Allow the BFD backend to copy any private header data it
11339 understands from the output BFD to the import library BFD. */
11340 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11341 goto free_sym_buf;
11342
11343 /* Filter symbols to appear in the import library. */
11344 if (bed->elf_backend_filter_implib_symbols)
11345 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11346 symcount);
11347 else
11348 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11349 if (symcount == 0)
11350 {
5df1bc57 11351 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11352 _bfd_error_handler (_("%B: no symbol found for import library"),
11353 implib_bfd);
76359541
TP
11354 goto free_sym_buf;
11355 }
11356
11357
11358 /* Make symbols absolute. */
11359 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11360 sizeof (*osymbuf));
11361 for (src_count = 0; src_count < symcount; src_count++)
11362 {
11363 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11364 sizeof (*osymbuf));
11365 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11366 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11367 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11368 osymbuf[src_count].internal_elf_sym.st_value =
11369 osymbuf[src_count].symbol.value;
11370 sympp[src_count] = &osymbuf[src_count].symbol;
11371 }
11372
11373 bfd_set_symtab (implib_bfd, sympp, symcount);
11374
11375 /* Allow the BFD backend to copy any private data it understands
11376 from the output BFD to the import library BFD. This is done last
11377 to permit the routine to look at the filtered symbol table. */
11378 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11379 goto free_sym_buf;
11380
11381 if (!bfd_close (implib_bfd))
11382 goto free_sym_buf;
11383
11384 ret = TRUE;
11385
11386free_sym_buf:
11387 free (sympp);
11388 return ret;
11389}
11390
9f7c3e5e
AM
11391static void
11392elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11393{
11394 asection *o;
11395
11396 if (flinfo->symstrtab != NULL)
ef10c3ac 11397 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11398 if (flinfo->contents != NULL)
11399 free (flinfo->contents);
11400 if (flinfo->external_relocs != NULL)
11401 free (flinfo->external_relocs);
11402 if (flinfo->internal_relocs != NULL)
11403 free (flinfo->internal_relocs);
11404 if (flinfo->external_syms != NULL)
11405 free (flinfo->external_syms);
11406 if (flinfo->locsym_shndx != NULL)
11407 free (flinfo->locsym_shndx);
11408 if (flinfo->internal_syms != NULL)
11409 free (flinfo->internal_syms);
11410 if (flinfo->indices != NULL)
11411 free (flinfo->indices);
11412 if (flinfo->sections != NULL)
11413 free (flinfo->sections);
9f7c3e5e
AM
11414 if (flinfo->symshndxbuf != NULL)
11415 free (flinfo->symshndxbuf);
11416 for (o = obfd->sections; o != NULL; o = o->next)
11417 {
11418 struct bfd_elf_section_data *esdo = elf_section_data (o);
11419 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11420 free (esdo->rel.hashes);
11421 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11422 free (esdo->rela.hashes);
11423 }
11424}
0b52efa6 11425
c152c796
AM
11426/* Do the final step of an ELF link. */
11427
11428bfd_boolean
11429bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11430{
11431 bfd_boolean dynamic;
11432 bfd_boolean emit_relocs;
11433 bfd *dynobj;
8b127cbc 11434 struct elf_final_link_info flinfo;
91d6fa6a
NC
11435 asection *o;
11436 struct bfd_link_order *p;
11437 bfd *sub;
c152c796
AM
11438 bfd_size_type max_contents_size;
11439 bfd_size_type max_external_reloc_size;
11440 bfd_size_type max_internal_reloc_count;
11441 bfd_size_type max_sym_count;
11442 bfd_size_type max_sym_shndx_count;
c152c796
AM
11443 Elf_Internal_Sym elfsym;
11444 unsigned int i;
11445 Elf_Internal_Shdr *symtab_hdr;
11446 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11448 struct elf_outext_info eoinfo;
11449 bfd_boolean merged;
11450 size_t relativecount = 0;
11451 asection *reldyn = 0;
11452 bfd_size_type amt;
104d59d1
JM
11453 asection *attr_section = NULL;
11454 bfd_vma attr_size = 0;
11455 const char *std_attrs_section;
64f52338 11456 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11457
64f52338 11458 if (!is_elf_hash_table (htab))
c152c796
AM
11459 return FALSE;
11460
0e1862bb 11461 if (bfd_link_pic (info))
c152c796
AM
11462 abfd->flags |= DYNAMIC;
11463
64f52338
AM
11464 dynamic = htab->dynamic_sections_created;
11465 dynobj = htab->dynobj;
c152c796 11466
0e1862bb 11467 emit_relocs = (bfd_link_relocatable (info)
a4676736 11468 || info->emitrelocations);
c152c796 11469
8b127cbc
AM
11470 flinfo.info = info;
11471 flinfo.output_bfd = abfd;
ef10c3ac 11472 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11473 if (flinfo.symstrtab == NULL)
c152c796
AM
11474 return FALSE;
11475
11476 if (! dynamic)
11477 {
8b127cbc
AM
11478 flinfo.hash_sec = NULL;
11479 flinfo.symver_sec = NULL;
c152c796
AM
11480 }
11481 else
11482 {
3d4d4302 11483 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11484 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11485 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11486 /* Note that it is OK if symver_sec is NULL. */
11487 }
11488
8b127cbc
AM
11489 flinfo.contents = NULL;
11490 flinfo.external_relocs = NULL;
11491 flinfo.internal_relocs = NULL;
11492 flinfo.external_syms = NULL;
11493 flinfo.locsym_shndx = NULL;
11494 flinfo.internal_syms = NULL;
11495 flinfo.indices = NULL;
11496 flinfo.sections = NULL;
8b127cbc 11497 flinfo.symshndxbuf = NULL;
ffbc01cc 11498 flinfo.filesym_count = 0;
c152c796 11499
104d59d1
JM
11500 /* The object attributes have been merged. Remove the input
11501 sections from the link, and set the contents of the output
11502 secton. */
11503 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11504 for (o = abfd->sections; o != NULL; o = o->next)
11505 {
11506 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11507 || strcmp (o->name, ".gnu.attributes") == 0)
11508 {
11509 for (p = o->map_head.link_order; p != NULL; p = p->next)
11510 {
11511 asection *input_section;
11512
11513 if (p->type != bfd_indirect_link_order)
11514 continue;
11515 input_section = p->u.indirect.section;
11516 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11517 elf_link_input_bfd ignores this section. */
11518 input_section->flags &= ~SEC_HAS_CONTENTS;
11519 }
a0c8462f 11520
104d59d1
JM
11521 attr_size = bfd_elf_obj_attr_size (abfd);
11522 if (attr_size)
11523 {
11524 bfd_set_section_size (abfd, o, attr_size);
11525 attr_section = o;
11526 /* Skip this section later on. */
11527 o->map_head.link_order = NULL;
11528 }
11529 else
11530 o->flags |= SEC_EXCLUDE;
11531 }
11532 }
11533
c152c796
AM
11534 /* Count up the number of relocations we will output for each output
11535 section, so that we know the sizes of the reloc sections. We
11536 also figure out some maximum sizes. */
11537 max_contents_size = 0;
11538 max_external_reloc_size = 0;
11539 max_internal_reloc_count = 0;
11540 max_sym_count = 0;
11541 max_sym_shndx_count = 0;
11542 merged = FALSE;
11543 for (o = abfd->sections; o != NULL; o = o->next)
11544 {
11545 struct bfd_elf_section_data *esdo = elf_section_data (o);
11546 o->reloc_count = 0;
11547
8423293d 11548 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11549 {
11550 unsigned int reloc_count = 0;
9eaff861 11551 unsigned int additional_reloc_count = 0;
c152c796 11552 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11553
11554 if (p->type == bfd_section_reloc_link_order
11555 || p->type == bfd_symbol_reloc_link_order)
11556 reloc_count = 1;
11557 else if (p->type == bfd_indirect_link_order)
11558 {
11559 asection *sec;
11560
11561 sec = p->u.indirect.section;
c152c796
AM
11562
11563 /* Mark all sections which are to be included in the
11564 link. This will normally be every section. We need
11565 to do this so that we can identify any sections which
11566 the linker has decided to not include. */
11567 sec->linker_mark = TRUE;
11568
11569 if (sec->flags & SEC_MERGE)
11570 merged = TRUE;
11571
eea6121a
AM
11572 if (sec->rawsize > max_contents_size)
11573 max_contents_size = sec->rawsize;
11574 if (sec->size > max_contents_size)
11575 max_contents_size = sec->size;
c152c796 11576
c152c796
AM
11577 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11578 && (sec->owner->flags & DYNAMIC) == 0)
11579 {
11580 size_t sym_count;
11581
a961cdd5
AM
11582 /* We are interested in just local symbols, not all
11583 symbols. */
c152c796
AM
11584 if (elf_bad_symtab (sec->owner))
11585 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11586 / bed->s->sizeof_sym);
11587 else
11588 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11589
11590 if (sym_count > max_sym_count)
11591 max_sym_count = sym_count;
11592
11593 if (sym_count > max_sym_shndx_count
6a40cf0c 11594 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11595 max_sym_shndx_count = sym_count;
11596
a961cdd5
AM
11597 if (esdo->this_hdr.sh_type == SHT_REL
11598 || esdo->this_hdr.sh_type == SHT_RELA)
11599 /* Some backends use reloc_count in relocation sections
11600 to count particular types of relocs. Of course,
11601 reloc sections themselves can't have relocations. */
11602 ;
11603 else if (emit_relocs)
11604 {
11605 reloc_count = sec->reloc_count;
11606 if (bed->elf_backend_count_additional_relocs)
11607 {
11608 int c;
11609 c = (*bed->elf_backend_count_additional_relocs) (sec);
11610 additional_reloc_count += c;
11611 }
11612 }
11613 else if (bed->elf_backend_count_relocs)
11614 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11615
11616 esdi = elf_section_data (sec);
11617
c152c796
AM
11618 if ((sec->flags & SEC_RELOC) != 0)
11619 {
d4730f92 11620 size_t ext_size = 0;
c152c796 11621
d4730f92
BS
11622 if (esdi->rel.hdr != NULL)
11623 ext_size = esdi->rel.hdr->sh_size;
11624 if (esdi->rela.hdr != NULL)
11625 ext_size += esdi->rela.hdr->sh_size;
7326c758 11626
c152c796
AM
11627 if (ext_size > max_external_reloc_size)
11628 max_external_reloc_size = ext_size;
11629 if (sec->reloc_count > max_internal_reloc_count)
11630 max_internal_reloc_count = sec->reloc_count;
11631 }
11632 }
11633 }
11634
11635 if (reloc_count == 0)
11636 continue;
11637
9eaff861 11638 reloc_count += additional_reloc_count;
c152c796
AM
11639 o->reloc_count += reloc_count;
11640
0e1862bb 11641 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11642 {
d4730f92 11643 if (esdi->rel.hdr)
9eaff861 11644 {
491d01d3 11645 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11646 esdo->rel.count += additional_reloc_count;
11647 }
d4730f92 11648 if (esdi->rela.hdr)
9eaff861 11649 {
491d01d3 11650 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11651 esdo->rela.count += additional_reloc_count;
11652 }
d4730f92
BS
11653 }
11654 else
11655 {
11656 if (o->use_rela_p)
11657 esdo->rela.count += reloc_count;
2c2b4ed4 11658 else
d4730f92 11659 esdo->rel.count += reloc_count;
c152c796 11660 }
c152c796
AM
11661 }
11662
9eaff861 11663 if (o->reloc_count > 0)
c152c796
AM
11664 o->flags |= SEC_RELOC;
11665 else
11666 {
11667 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11668 set it (this is probably a bug) and if it is set
11669 assign_section_numbers will create a reloc section. */
11670 o->flags &=~ SEC_RELOC;
11671 }
11672
11673 /* If the SEC_ALLOC flag is not set, force the section VMA to
11674 zero. This is done in elf_fake_sections as well, but forcing
11675 the VMA to 0 here will ensure that relocs against these
11676 sections are handled correctly. */
11677 if ((o->flags & SEC_ALLOC) == 0
11678 && ! o->user_set_vma)
11679 o->vma = 0;
11680 }
11681
0e1862bb 11682 if (! bfd_link_relocatable (info) && merged)
64f52338 11683 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11684
11685 /* Figure out the file positions for everything but the symbol table
11686 and the relocs. We set symcount to force assign_section_numbers
11687 to create a symbol table. */
8539e4e8 11688 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11689 BFD_ASSERT (! abfd->output_has_begun);
11690 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11691 goto error_return;
11692
ee75fd95 11693 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11694 for (o = abfd->sections; o != NULL; o = o->next)
11695 {
d4730f92 11696 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11697 if ((o->flags & SEC_RELOC) != 0)
11698 {
d4730f92 11699 if (esdo->rel.hdr
9eaff861 11700 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11701 goto error_return;
11702
d4730f92 11703 if (esdo->rela.hdr
9eaff861 11704 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11705 goto error_return;
11706 }
11707
11708 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11709 to count upwards while actually outputting the relocations. */
d4730f92
BS
11710 esdo->rel.count = 0;
11711 esdo->rela.count = 0;
0ce398f1
L
11712
11713 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11714 {
11715 /* Cache the section contents so that they can be compressed
11716 later. Use bfd_malloc since it will be freed by
11717 bfd_compress_section_contents. */
11718 unsigned char *contents = esdo->this_hdr.contents;
11719 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11720 abort ();
11721 contents
11722 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11723 if (contents == NULL)
11724 goto error_return;
11725 esdo->this_hdr.contents = contents;
11726 }
c152c796
AM
11727 }
11728
c152c796 11729 /* We have now assigned file positions for all the sections except
a485e98e
AM
11730 .symtab, .strtab, and non-loaded reloc sections. We start the
11731 .symtab section at the current file position, and write directly
11732 to it. We build the .strtab section in memory. */
c152c796
AM
11733 bfd_get_symcount (abfd) = 0;
11734 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11735 /* sh_name is set in prep_headers. */
11736 symtab_hdr->sh_type = SHT_SYMTAB;
11737 /* sh_flags, sh_addr and sh_size all start off zero. */
11738 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11739 /* sh_link is set in assign_section_numbers. */
11740 /* sh_info is set below. */
11741 /* sh_offset is set just below. */
72de5009 11742 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11743
ef10c3ac
L
11744 if (max_sym_count < 20)
11745 max_sym_count = 20;
64f52338 11746 htab->strtabsize = max_sym_count;
ef10c3ac 11747 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11748 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11749 if (htab->strtab == NULL)
c152c796 11750 goto error_return;
ef10c3ac
L
11751 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11752 flinfo.symshndxbuf
11753 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11754 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11755
8539e4e8 11756 if (info->strip != strip_all || emit_relocs)
c152c796 11757 {
8539e4e8
AM
11758 file_ptr off = elf_next_file_pos (abfd);
11759
11760 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11761
11762 /* Note that at this point elf_next_file_pos (abfd) is
11763 incorrect. We do not yet know the size of the .symtab section.
11764 We correct next_file_pos below, after we do know the size. */
11765
11766 /* Start writing out the symbol table. The first symbol is always a
11767 dummy symbol. */
c152c796
AM
11768 elfsym.st_value = 0;
11769 elfsym.st_size = 0;
11770 elfsym.st_info = 0;
11771 elfsym.st_other = 0;
11772 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11773 elfsym.st_target_internal = 0;
ef10c3ac
L
11774 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11775 bfd_und_section_ptr, NULL) != 1)
c152c796 11776 goto error_return;
c152c796 11777
8539e4e8
AM
11778 /* Output a symbol for each section. We output these even if we are
11779 discarding local symbols, since they are used for relocs. These
11780 symbols have no names. We store the index of each one in the
11781 index field of the section, so that we can find it again when
11782 outputting relocs. */
11783
c152c796
AM
11784 elfsym.st_size = 0;
11785 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11786 elfsym.st_other = 0;
f0b5bb34 11787 elfsym.st_value = 0;
35fc36a8 11788 elfsym.st_target_internal = 0;
c152c796
AM
11789 for (i = 1; i < elf_numsections (abfd); i++)
11790 {
11791 o = bfd_section_from_elf_index (abfd, i);
11792 if (o != NULL)
f0b5bb34
AM
11793 {
11794 o->target_index = bfd_get_symcount (abfd);
11795 elfsym.st_shndx = i;
0e1862bb 11796 if (!bfd_link_relocatable (info))
f0b5bb34 11797 elfsym.st_value = o->vma;
ef10c3ac
L
11798 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11799 NULL) != 1)
f0b5bb34
AM
11800 goto error_return;
11801 }
c152c796
AM
11802 }
11803 }
11804
11805 /* Allocate some memory to hold information read in from the input
11806 files. */
11807 if (max_contents_size != 0)
11808 {
8b127cbc
AM
11809 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11810 if (flinfo.contents == NULL)
c152c796
AM
11811 goto error_return;
11812 }
11813
11814 if (max_external_reloc_size != 0)
11815 {
8b127cbc
AM
11816 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11817 if (flinfo.external_relocs == NULL)
c152c796
AM
11818 goto error_return;
11819 }
11820
11821 if (max_internal_reloc_count != 0)
11822 {
056bafd4 11823 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
11824 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11825 if (flinfo.internal_relocs == NULL)
c152c796
AM
11826 goto error_return;
11827 }
11828
11829 if (max_sym_count != 0)
11830 {
11831 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11832 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11833 if (flinfo.external_syms == NULL)
c152c796
AM
11834 goto error_return;
11835
11836 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11837 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11838 if (flinfo.internal_syms == NULL)
c152c796
AM
11839 goto error_return;
11840
11841 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11842 flinfo.indices = (long int *) bfd_malloc (amt);
11843 if (flinfo.indices == NULL)
c152c796
AM
11844 goto error_return;
11845
11846 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11847 flinfo.sections = (asection **) bfd_malloc (amt);
11848 if (flinfo.sections == NULL)
c152c796
AM
11849 goto error_return;
11850 }
11851
11852 if (max_sym_shndx_count != 0)
11853 {
11854 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11855 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11856 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11857 goto error_return;
11858 }
11859
64f52338 11860 if (htab->tls_sec)
c152c796
AM
11861 {
11862 bfd_vma base, end = 0;
11863 asection *sec;
11864
64f52338 11865 for (sec = htab->tls_sec;
c152c796
AM
11866 sec && (sec->flags & SEC_THREAD_LOCAL);
11867 sec = sec->next)
11868 {
3a800eb9 11869 bfd_size_type size = sec->size;
c152c796 11870
3a800eb9
AM
11871 if (size == 0
11872 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11873 {
91d6fa6a
NC
11874 struct bfd_link_order *ord = sec->map_tail.link_order;
11875
11876 if (ord != NULL)
11877 size = ord->offset + ord->size;
c152c796
AM
11878 }
11879 end = sec->vma + size;
11880 }
64f52338 11881 base = htab->tls_sec->vma;
7dc98aea
RO
11882 /* Only align end of TLS section if static TLS doesn't have special
11883 alignment requirements. */
11884 if (bed->static_tls_alignment == 1)
64f52338
AM
11885 end = align_power (end, htab->tls_sec->alignment_power);
11886 htab->tls_size = end - base;
c152c796
AM
11887 }
11888
0b52efa6
PB
11889 /* Reorder SHF_LINK_ORDER sections. */
11890 for (o = abfd->sections; o != NULL; o = o->next)
11891 {
11892 if (!elf_fixup_link_order (abfd, o))
11893 return FALSE;
11894 }
11895
2f0c68f2
CM
11896 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11897 return FALSE;
11898
c152c796
AM
11899 /* Since ELF permits relocations to be against local symbols, we
11900 must have the local symbols available when we do the relocations.
11901 Since we would rather only read the local symbols once, and we
11902 would rather not keep them in memory, we handle all the
11903 relocations for a single input file at the same time.
11904
11905 Unfortunately, there is no way to know the total number of local
11906 symbols until we have seen all of them, and the local symbol
11907 indices precede the global symbol indices. This means that when
11908 we are generating relocatable output, and we see a reloc against
11909 a global symbol, we can not know the symbol index until we have
11910 finished examining all the local symbols to see which ones we are
11911 going to output. To deal with this, we keep the relocations in
11912 memory, and don't output them until the end of the link. This is
11913 an unfortunate waste of memory, but I don't see a good way around
11914 it. Fortunately, it only happens when performing a relocatable
11915 link, which is not the common case. FIXME: If keep_memory is set
11916 we could write the relocs out and then read them again; I don't
11917 know how bad the memory loss will be. */
11918
c72f2fb2 11919 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11920 sub->output_has_begun = FALSE;
11921 for (o = abfd->sections; o != NULL; o = o->next)
11922 {
8423293d 11923 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11924 {
11925 if (p->type == bfd_indirect_link_order
11926 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11927 == bfd_target_elf_flavour)
11928 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11929 {
11930 if (! sub->output_has_begun)
11931 {
8b127cbc 11932 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11933 goto error_return;
11934 sub->output_has_begun = TRUE;
11935 }
11936 }
11937 else if (p->type == bfd_section_reloc_link_order
11938 || p->type == bfd_symbol_reloc_link_order)
11939 {
11940 if (! elf_reloc_link_order (abfd, info, o, p))
11941 goto error_return;
11942 }
11943 else
11944 {
11945 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11946 {
11947 if (p->type == bfd_indirect_link_order
11948 && (bfd_get_flavour (sub)
11949 == bfd_target_elf_flavour)
11950 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11951 != bed->s->elfclass))
11952 {
11953 const char *iclass, *oclass;
11954
aebf9be7 11955 switch (bed->s->elfclass)
351f65ca 11956 {
aebf9be7
NC
11957 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11958 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11959 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11960 default: abort ();
351f65ca 11961 }
aebf9be7
NC
11962
11963 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11964 {
aebf9be7
NC
11965 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11966 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11967 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11968 default: abort ();
351f65ca
L
11969 }
11970
11971 bfd_set_error (bfd_error_wrong_format);
4eca0228 11972 _bfd_error_handler
695344c0 11973 /* xgettext:c-format */
351f65ca
L
11974 (_("%B: file class %s incompatible with %s"),
11975 sub, iclass, oclass);
11976 }
11977
11978 goto error_return;
11979 }
c152c796
AM
11980 }
11981 }
11982 }
11983
c0f00686
L
11984 /* Free symbol buffer if needed. */
11985 if (!info->reduce_memory_overheads)
11986 {
c72f2fb2 11987 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11988 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11989 && elf_tdata (sub)->symbuf)
c0f00686
L
11990 {
11991 free (elf_tdata (sub)->symbuf);
11992 elf_tdata (sub)->symbuf = NULL;
11993 }
11994 }
11995
c152c796
AM
11996 /* Output any global symbols that got converted to local in a
11997 version script or due to symbol visibility. We do this in a
11998 separate step since ELF requires all local symbols to appear
11999 prior to any global symbols. FIXME: We should only do this if
12000 some global symbols were, in fact, converted to become local.
12001 FIXME: Will this work correctly with the Irix 5 linker? */
12002 eoinfo.failed = FALSE;
8b127cbc 12003 eoinfo.flinfo = &flinfo;
c152c796 12004 eoinfo.localsyms = TRUE;
34a79995 12005 eoinfo.file_sym_done = FALSE;
7686d77d 12006 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12007 if (eoinfo.failed)
12008 return FALSE;
12009
4e617b1e
PB
12010 /* If backend needs to output some local symbols not present in the hash
12011 table, do it now. */
8539e4e8
AM
12012 if (bed->elf_backend_output_arch_local_syms
12013 && (info->strip != strip_all || emit_relocs))
4e617b1e 12014 {
6e0b88f1 12015 typedef int (*out_sym_func)
4e617b1e
PB
12016 (void *, const char *, Elf_Internal_Sym *, asection *,
12017 struct elf_link_hash_entry *);
12018
12019 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12020 (abfd, info, &flinfo,
12021 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12022 return FALSE;
12023 }
12024
c152c796
AM
12025 /* That wrote out all the local symbols. Finish up the symbol table
12026 with the global symbols. Even if we want to strip everything we
12027 can, we still need to deal with those global symbols that got
12028 converted to local in a version script. */
12029
12030 /* The sh_info field records the index of the first non local symbol. */
12031 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12032
12033 if (dynamic
64f52338
AM
12034 && htab->dynsym != NULL
12035 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12036 {
12037 Elf_Internal_Sym sym;
64f52338 12038 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12039
64f52338
AM
12040 o = htab->dynsym->output_section;
12041 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12042
12043 /* Write out the section symbols for the output sections. */
0e1862bb 12044 if (bfd_link_pic (info)
64f52338 12045 || htab->is_relocatable_executable)
c152c796
AM
12046 {
12047 asection *s;
12048
12049 sym.st_size = 0;
12050 sym.st_name = 0;
12051 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12052 sym.st_other = 0;
35fc36a8 12053 sym.st_target_internal = 0;
c152c796
AM
12054
12055 for (s = abfd->sections; s != NULL; s = s->next)
12056 {
12057 int indx;
12058 bfd_byte *dest;
12059 long dynindx;
12060
c152c796 12061 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12062 if (dynindx <= 0)
12063 continue;
12064 indx = elf_section_data (s)->this_idx;
c152c796
AM
12065 BFD_ASSERT (indx > 0);
12066 sym.st_shndx = indx;
c0d5a53d
L
12067 if (! check_dynsym (abfd, &sym))
12068 return FALSE;
c152c796
AM
12069 sym.st_value = s->vma;
12070 dest = dynsym + dynindx * bed->s->sizeof_sym;
12071 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12072 }
c152c796
AM
12073 }
12074
12075 /* Write out the local dynsyms. */
64f52338 12076 if (htab->dynlocal)
c152c796
AM
12077 {
12078 struct elf_link_local_dynamic_entry *e;
64f52338 12079 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12080 {
12081 asection *s;
12082 bfd_byte *dest;
12083
935bd1e0 12084 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12085 Note that we saved a word of storage and overwrote
12086 the original st_name with the dynstr_index. */
12087 sym = e->isym;
935bd1e0 12088 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12089
cb33740c
AM
12090 s = bfd_section_from_elf_index (e->input_bfd,
12091 e->isym.st_shndx);
12092 if (s != NULL)
c152c796 12093 {
c152c796
AM
12094 sym.st_shndx =
12095 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12096 if (! check_dynsym (abfd, &sym))
12097 return FALSE;
c152c796
AM
12098 sym.st_value = (s->output_section->vma
12099 + s->output_offset
12100 + e->isym.st_value);
12101 }
12102
c152c796
AM
12103 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12104 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12105 }
12106 }
c152c796
AM
12107 }
12108
12109 /* We get the global symbols from the hash table. */
12110 eoinfo.failed = FALSE;
12111 eoinfo.localsyms = FALSE;
8b127cbc 12112 eoinfo.flinfo = &flinfo;
7686d77d 12113 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12114 if (eoinfo.failed)
12115 return FALSE;
12116
12117 /* If backend needs to output some symbols not present in the hash
12118 table, do it now. */
8539e4e8
AM
12119 if (bed->elf_backend_output_arch_syms
12120 && (info->strip != strip_all || emit_relocs))
c152c796 12121 {
6e0b88f1 12122 typedef int (*out_sym_func)
c152c796
AM
12123 (void *, const char *, Elf_Internal_Sym *, asection *,
12124 struct elf_link_hash_entry *);
12125
12126 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12127 (abfd, info, &flinfo,
12128 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12129 return FALSE;
12130 }
12131
ef10c3ac
L
12132 /* Finalize the .strtab section. */
12133 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12134
12135 /* Swap out the .strtab section. */
12136 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12137 return FALSE;
12138
12139 /* Now we know the size of the symtab section. */
c152c796
AM
12140 if (bfd_get_symcount (abfd) > 0)
12141 {
ee3b52e9
L
12142 /* Finish up and write out the symbol string table (.strtab)
12143 section. */
ad32986f 12144 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12145 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12146
ad32986f 12147 if (elf_symtab_shndx_list (abfd))
8539e4e8 12148 {
ad32986f 12149 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12150
ad32986f
NC
12151 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12152 {
12153 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12154 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12155 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12156 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12157 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12158
ad32986f
NC
12159 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12160 off, TRUE);
12161
12162 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12163 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12164 return FALSE;
12165 }
8539e4e8 12166 }
ee3b52e9
L
12167
12168 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12169 /* sh_name was set in prep_headers. */
12170 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12171 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12172 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12173 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12174 symstrtab_hdr->sh_entsize = 0;
12175 symstrtab_hdr->sh_link = 0;
12176 symstrtab_hdr->sh_info = 0;
12177 /* sh_offset is set just below. */
12178 symstrtab_hdr->sh_addralign = 1;
12179
12180 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12181 off, TRUE);
12182 elf_next_file_pos (abfd) = off;
12183
c152c796 12184 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12185 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12186 return FALSE;
12187 }
12188
76359541
TP
12189 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12190 {
4eca0228
AM
12191 _bfd_error_handler (_("%B: failed to generate import library"),
12192 info->out_implib_bfd);
76359541
TP
12193 return FALSE;
12194 }
12195
c152c796
AM
12196 /* Adjust the relocs to have the correct symbol indices. */
12197 for (o = abfd->sections; o != NULL; o = o->next)
12198 {
d4730f92 12199 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12200 bfd_boolean sort;
10bbbc1d 12201
c152c796
AM
12202 if ((o->flags & SEC_RELOC) == 0)
12203 continue;
12204
28dbcedc 12205 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12206 if (esdo->rel.hdr != NULL
10bbbc1d 12207 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12208 return FALSE;
12209 if (esdo->rela.hdr != NULL
10bbbc1d 12210 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12211 return FALSE;
c152c796
AM
12212
12213 /* Set the reloc_count field to 0 to prevent write_relocs from
12214 trying to swap the relocs out itself. */
12215 o->reloc_count = 0;
12216 }
12217
12218 if (dynamic && info->combreloc && dynobj != NULL)
12219 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12220
12221 /* If we are linking against a dynamic object, or generating a
12222 shared library, finish up the dynamic linking information. */
12223 if (dynamic)
12224 {
12225 bfd_byte *dyncon, *dynconend;
12226
12227 /* Fix up .dynamic entries. */
3d4d4302 12228 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12229 BFD_ASSERT (o != NULL);
12230
12231 dyncon = o->contents;
eea6121a 12232 dynconend = o->contents + o->size;
c152c796
AM
12233 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12234 {
12235 Elf_Internal_Dyn dyn;
12236 const char *name;
12237 unsigned int type;
64487780
AM
12238 bfd_size_type sh_size;
12239 bfd_vma sh_addr;
c152c796
AM
12240
12241 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12242
12243 switch (dyn.d_tag)
12244 {
12245 default:
12246 continue;
12247 case DT_NULL:
12248 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12249 {
12250 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12251 {
12252 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12253 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12254 default: continue;
12255 }
12256 dyn.d_un.d_val = relativecount;
12257 relativecount = 0;
12258 break;
12259 }
12260 continue;
12261
12262 case DT_INIT:
12263 name = info->init_function;
12264 goto get_sym;
12265 case DT_FINI:
12266 name = info->fini_function;
12267 get_sym:
12268 {
12269 struct elf_link_hash_entry *h;
12270
64f52338 12271 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12272 if (h != NULL
12273 && (h->root.type == bfd_link_hash_defined
12274 || h->root.type == bfd_link_hash_defweak))
12275 {
bef26483 12276 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12277 o = h->root.u.def.section;
12278 if (o->output_section != NULL)
bef26483 12279 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12280 + o->output_offset);
12281 else
12282 {
12283 /* The symbol is imported from another shared
12284 library and does not apply to this one. */
bef26483 12285 dyn.d_un.d_ptr = 0;
c152c796
AM
12286 }
12287 break;
12288 }
12289 }
12290 continue;
12291
12292 case DT_PREINIT_ARRAYSZ:
12293 name = ".preinit_array";
4ade44b7 12294 goto get_out_size;
c152c796
AM
12295 case DT_INIT_ARRAYSZ:
12296 name = ".init_array";
4ade44b7 12297 goto get_out_size;
c152c796
AM
12298 case DT_FINI_ARRAYSZ:
12299 name = ".fini_array";
4ade44b7 12300 get_out_size:
c152c796
AM
12301 o = bfd_get_section_by_name (abfd, name);
12302 if (o == NULL)
12303 {
4eca0228 12304 _bfd_error_handler
4ade44b7 12305 (_("could not find section %s"), name);
c152c796
AM
12306 goto error_return;
12307 }
eea6121a 12308 if (o->size == 0)
4eca0228 12309 _bfd_error_handler
c152c796 12310 (_("warning: %s section has zero size"), name);
eea6121a 12311 dyn.d_un.d_val = o->size;
c152c796
AM
12312 break;
12313
12314 case DT_PREINIT_ARRAY:
12315 name = ".preinit_array";
4ade44b7 12316 goto get_out_vma;
c152c796
AM
12317 case DT_INIT_ARRAY:
12318 name = ".init_array";
4ade44b7 12319 goto get_out_vma;
c152c796
AM
12320 case DT_FINI_ARRAY:
12321 name = ".fini_array";
4ade44b7
AM
12322 get_out_vma:
12323 o = bfd_get_section_by_name (abfd, name);
12324 goto do_vma;
c152c796
AM
12325
12326 case DT_HASH:
12327 name = ".hash";
12328 goto get_vma;
fdc90cb4
JJ
12329 case DT_GNU_HASH:
12330 name = ".gnu.hash";
12331 goto get_vma;
c152c796
AM
12332 case DT_STRTAB:
12333 name = ".dynstr";
12334 goto get_vma;
12335 case DT_SYMTAB:
12336 name = ".dynsym";
12337 goto get_vma;
12338 case DT_VERDEF:
12339 name = ".gnu.version_d";
12340 goto get_vma;
12341 case DT_VERNEED:
12342 name = ".gnu.version_r";
12343 goto get_vma;
12344 case DT_VERSYM:
12345 name = ".gnu.version";
12346 get_vma:
4ade44b7
AM
12347 o = bfd_get_linker_section (dynobj, name);
12348 do_vma:
b3293efa 12349 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12350 {
4eca0228 12351 _bfd_error_handler
4ade44b7 12352 (_("could not find section %s"), name);
c152c796
AM
12353 goto error_return;
12354 }
894891db
NC
12355 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12356 {
4eca0228 12357 _bfd_error_handler
894891db
NC
12358 (_("warning: section '%s' is being made into a note"), name);
12359 bfd_set_error (bfd_error_nonrepresentable_section);
12360 goto error_return;
12361 }
4ade44b7 12362 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12363 break;
12364
12365 case DT_REL:
12366 case DT_RELA:
12367 case DT_RELSZ:
12368 case DT_RELASZ:
12369 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12370 type = SHT_REL;
12371 else
12372 type = SHT_RELA;
64487780
AM
12373 sh_size = 0;
12374 sh_addr = 0;
c152c796
AM
12375 for (i = 1; i < elf_numsections (abfd); i++)
12376 {
12377 Elf_Internal_Shdr *hdr;
12378
12379 hdr = elf_elfsections (abfd)[i];
12380 if (hdr->sh_type == type
12381 && (hdr->sh_flags & SHF_ALLOC) != 0)
12382 {
64487780
AM
12383 sh_size += hdr->sh_size;
12384 if (sh_addr == 0
12385 || sh_addr > hdr->sh_addr)
12386 sh_addr = hdr->sh_addr;
c152c796
AM
12387 }
12388 }
64487780 12389
64f52338
AM
12390 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12391 {
12392 /* Don't count procedure linkage table relocs in the
12393 overall reloc count. */
64487780
AM
12394 sh_size -= htab->srelplt->size;
12395 if (sh_size == 0)
12396 /* If the size is zero, make the address zero too.
12397 This is to avoid a glibc bug. If the backend
12398 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12399 zero, then we'll put DT_RELA at the end of
12400 DT_JMPREL. glibc will interpret the end of
12401 DT_RELA matching the end of DT_JMPREL as the
12402 case where DT_RELA includes DT_JMPREL, and for
12403 LD_BIND_NOW will decide that processing DT_RELA
12404 will process the PLT relocs too. Net result:
12405 No PLT relocs applied. */
12406 sh_addr = 0;
12407
64f52338
AM
12408 /* If .rela.plt is the first .rela section, exclude
12409 it from DT_RELA. */
64487780
AM
12410 else if (sh_addr == (htab->srelplt->output_section->vma
12411 + htab->srelplt->output_offset))
12412 sh_addr += htab->srelplt->size;
64f52338 12413 }
64487780
AM
12414
12415 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12416 dyn.d_un.d_val = sh_size;
12417 else
12418 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12419 break;
12420 }
12421 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12422 }
12423 }
12424
12425 /* If we have created any dynamic sections, then output them. */
12426 if (dynobj != NULL)
12427 {
12428 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12429 goto error_return;
12430
943284cc 12431 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12432 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12433 || info->error_textrel)
3d4d4302 12434 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12435 {
12436 bfd_byte *dyncon, *dynconend;
12437
943284cc
DJ
12438 dyncon = o->contents;
12439 dynconend = o->contents + o->size;
12440 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12441 {
12442 Elf_Internal_Dyn dyn;
12443
12444 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12445
12446 if (dyn.d_tag == DT_TEXTREL)
12447 {
c192a133
AM
12448 if (info->error_textrel)
12449 info->callbacks->einfo
12450 (_("%P%X: read-only segment has dynamic relocations.\n"));
12451 else
12452 info->callbacks->einfo
12453 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12454 break;
12455 }
12456 }
12457 }
12458
c152c796
AM
12459 for (o = dynobj->sections; o != NULL; o = o->next)
12460 {
12461 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12462 || o->size == 0
c152c796
AM
12463 || o->output_section == bfd_abs_section_ptr)
12464 continue;
12465 if ((o->flags & SEC_LINKER_CREATED) == 0)
12466 {
12467 /* At this point, we are only interested in sections
12468 created by _bfd_elf_link_create_dynamic_sections. */
12469 continue;
12470 }
64f52338 12471 if (htab->stab_info.stabstr == o)
3722b82f 12472 continue;
64f52338 12473 if (htab->eh_info.hdr_sec == o)
eea6121a 12474 continue;
3d4d4302 12475 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12476 {
12477 if (! bfd_set_section_contents (abfd, o->output_section,
12478 o->contents,
37b01f6a
DG
12479 (file_ptr) o->output_offset
12480 * bfd_octets_per_byte (abfd),
eea6121a 12481 o->size))
c152c796
AM
12482 goto error_return;
12483 }
12484 else
12485 {
12486 /* The contents of the .dynstr section are actually in a
12487 stringtab. */
8539e4e8
AM
12488 file_ptr off;
12489
c152c796
AM
12490 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12491 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12492 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12493 goto error_return;
12494 }
12495 }
12496 }
12497
7bdf4127 12498 if (!info->resolve_section_groups)
c152c796
AM
12499 {
12500 bfd_boolean failed = FALSE;
12501
7bdf4127 12502 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12503 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12504 if (failed)
12505 goto error_return;
12506 }
12507
12508 /* If we have optimized stabs strings, output them. */
64f52338 12509 if (htab->stab_info.stabstr != NULL)
c152c796 12510 {
64f52338 12511 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12512 goto error_return;
12513 }
12514
9f7c3e5e
AM
12515 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12516 goto error_return;
c152c796 12517
9f7c3e5e 12518 elf_final_link_free (abfd, &flinfo);
c152c796 12519
12bd6957 12520 elf_linker (abfd) = TRUE;
c152c796 12521
104d59d1
JM
12522 if (attr_section)
12523 {
a50b1753 12524 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12525 if (contents == NULL)
d0f16d5e 12526 return FALSE; /* Bail out and fail. */
104d59d1
JM
12527 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12528 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12529 free (contents);
12530 }
12531
c152c796
AM
12532 return TRUE;
12533
12534 error_return:
9f7c3e5e 12535 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12536 return FALSE;
12537}
12538\f
5241d853
RS
12539/* Initialize COOKIE for input bfd ABFD. */
12540
12541static bfd_boolean
12542init_reloc_cookie (struct elf_reloc_cookie *cookie,
12543 struct bfd_link_info *info, bfd *abfd)
12544{
12545 Elf_Internal_Shdr *symtab_hdr;
12546 const struct elf_backend_data *bed;
12547
12548 bed = get_elf_backend_data (abfd);
12549 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12550
12551 cookie->abfd = abfd;
12552 cookie->sym_hashes = elf_sym_hashes (abfd);
12553 cookie->bad_symtab = elf_bad_symtab (abfd);
12554 if (cookie->bad_symtab)
12555 {
12556 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12557 cookie->extsymoff = 0;
12558 }
12559 else
12560 {
12561 cookie->locsymcount = symtab_hdr->sh_info;
12562 cookie->extsymoff = symtab_hdr->sh_info;
12563 }
12564
12565 if (bed->s->arch_size == 32)
12566 cookie->r_sym_shift = 8;
12567 else
12568 cookie->r_sym_shift = 32;
12569
12570 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12571 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12572 {
12573 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12574 cookie->locsymcount, 0,
12575 NULL, NULL, NULL);
12576 if (cookie->locsyms == NULL)
12577 {
12578 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12579 return FALSE;
12580 }
12581 if (info->keep_memory)
12582 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12583 }
12584 return TRUE;
12585}
12586
12587/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12588
12589static void
12590fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12591{
12592 Elf_Internal_Shdr *symtab_hdr;
12593
12594 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12595 if (cookie->locsyms != NULL
12596 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12597 free (cookie->locsyms);
12598}
12599
12600/* Initialize the relocation information in COOKIE for input section SEC
12601 of input bfd ABFD. */
12602
12603static bfd_boolean
12604init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12605 struct bfd_link_info *info, bfd *abfd,
12606 asection *sec)
12607{
5241d853
RS
12608 if (sec->reloc_count == 0)
12609 {
12610 cookie->rels = NULL;
12611 cookie->relend = NULL;
12612 }
12613 else
12614 {
5241d853
RS
12615 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12616 info->keep_memory);
12617 if (cookie->rels == NULL)
12618 return FALSE;
12619 cookie->rel = cookie->rels;
056bafd4 12620 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12621 }
12622 cookie->rel = cookie->rels;
12623 return TRUE;
12624}
12625
12626/* Free the memory allocated by init_reloc_cookie_rels,
12627 if appropriate. */
12628
12629static void
12630fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12631 asection *sec)
12632{
12633 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12634 free (cookie->rels);
12635}
12636
12637/* Initialize the whole of COOKIE for input section SEC. */
12638
12639static bfd_boolean
12640init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12641 struct bfd_link_info *info,
12642 asection *sec)
12643{
12644 if (!init_reloc_cookie (cookie, info, sec->owner))
12645 goto error1;
12646 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12647 goto error2;
12648 return TRUE;
12649
12650 error2:
12651 fini_reloc_cookie (cookie, sec->owner);
12652 error1:
12653 return FALSE;
12654}
12655
12656/* Free the memory allocated by init_reloc_cookie_for_section,
12657 if appropriate. */
12658
12659static void
12660fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12661 asection *sec)
12662{
12663 fini_reloc_cookie_rels (cookie, sec);
12664 fini_reloc_cookie (cookie, sec->owner);
12665}
12666\f
c152c796
AM
12667/* Garbage collect unused sections. */
12668
07adf181
AM
12669/* Default gc_mark_hook. */
12670
12671asection *
12672_bfd_elf_gc_mark_hook (asection *sec,
12673 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12674 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12675 struct elf_link_hash_entry *h,
12676 Elf_Internal_Sym *sym)
12677{
12678 if (h != NULL)
12679 {
12680 switch (h->root.type)
12681 {
12682 case bfd_link_hash_defined:
12683 case bfd_link_hash_defweak:
12684 return h->root.u.def.section;
12685
12686 case bfd_link_hash_common:
12687 return h->root.u.c.p->section;
12688
12689 default:
12690 break;
12691 }
12692 }
12693 else
12694 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12695
12696 return NULL;
12697}
12698
b7c871ed
L
12699/* Return the global debug definition section. */
12700
12701static asection *
12702elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12703 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12704 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12705 struct elf_link_hash_entry *h,
12706 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12707{
12708 if (h != NULL
12709 && (h->root.type == bfd_link_hash_defined
12710 || h->root.type == bfd_link_hash_defweak)
12711 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12712 return h->root.u.def.section;
12713
12714 return NULL;
12715}
12716
5241d853
RS
12717/* COOKIE->rel describes a relocation against section SEC, which is
12718 a section we've decided to keep. Return the section that contains
12719 the relocation symbol, or NULL if no section contains it. */
12720
12721asection *
12722_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12723 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12724 struct elf_reloc_cookie *cookie,
12725 bfd_boolean *start_stop)
5241d853
RS
12726{
12727 unsigned long r_symndx;
12728 struct elf_link_hash_entry *h;
12729
12730 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12731 if (r_symndx == STN_UNDEF)
5241d853
RS
12732 return NULL;
12733
12734 if (r_symndx >= cookie->locsymcount
12735 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12736 {
12737 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12738 if (h == NULL)
12739 {
12740 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12741 sec->owner);
12742 return NULL;
12743 }
5241d853
RS
12744 while (h->root.type == bfd_link_hash_indirect
12745 || h->root.type == bfd_link_hash_warning)
12746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12747 h->mark = 1;
4e6b54a6
AM
12748 /* If this symbol is weak and there is a non-weak definition, we
12749 keep the non-weak definition because many backends put
12750 dynamic reloc info on the non-weak definition for code
12751 handling copy relocs. */
12752 if (h->u.weakdef != NULL)
12753 h->u.weakdef->mark = 1;
1cce69b9 12754
a6a4679f 12755 if (start_stop != NULL)
1cce69b9 12756 {
7dba9362
AM
12757 /* To work around a glibc bug, mark XXX input sections
12758 when there is a reference to __start_XXX or __stop_XXX
12759 symbols. */
cbd0eecf 12760 if (h->start_stop)
1cce69b9 12761 {
cbd0eecf 12762 asection *s = h->u2.start_stop_section;
a6a4679f
AM
12763 *start_stop = !s->gc_mark;
12764 return s;
1cce69b9
AM
12765 }
12766 }
12767
5241d853
RS
12768 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12769 }
12770
12771 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12772 &cookie->locsyms[r_symndx]);
12773}
12774
12775/* COOKIE->rel describes a relocation against section SEC, which is
12776 a section we've decided to keep. Mark the section that contains
9d0a14d3 12777 the relocation symbol. */
5241d853
RS
12778
12779bfd_boolean
12780_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12781 asection *sec,
12782 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12783 struct elf_reloc_cookie *cookie)
5241d853
RS
12784{
12785 asection *rsec;
1cce69b9 12786 bfd_boolean start_stop = FALSE;
5241d853 12787
1cce69b9
AM
12788 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12789 while (rsec != NULL)
5241d853 12790 {
1cce69b9
AM
12791 if (!rsec->gc_mark)
12792 {
12793 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12794 || (rsec->owner->flags & DYNAMIC) != 0)
12795 rsec->gc_mark = 1;
12796 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12797 return FALSE;
12798 }
12799 if (!start_stop)
12800 break;
199af150 12801 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12802 }
12803 return TRUE;
12804}
12805
07adf181
AM
12806/* The mark phase of garbage collection. For a given section, mark
12807 it and any sections in this section's group, and all the sections
12808 which define symbols to which it refers. */
12809
ccfa59ea
AM
12810bfd_boolean
12811_bfd_elf_gc_mark (struct bfd_link_info *info,
12812 asection *sec,
6a5bb875 12813 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12814{
12815 bfd_boolean ret;
9d0a14d3 12816 asection *group_sec, *eh_frame;
c152c796
AM
12817
12818 sec->gc_mark = 1;
12819
12820 /* Mark all the sections in the group. */
12821 group_sec = elf_section_data (sec)->next_in_group;
12822 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12823 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12824 return FALSE;
12825
12826 /* Look through the section relocs. */
12827 ret = TRUE;
9d0a14d3
RS
12828 eh_frame = elf_eh_frame_section (sec->owner);
12829 if ((sec->flags & SEC_RELOC) != 0
12830 && sec->reloc_count > 0
12831 && sec != eh_frame)
c152c796 12832 {
5241d853 12833 struct elf_reloc_cookie cookie;
c152c796 12834
5241d853
RS
12835 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12836 ret = FALSE;
c152c796 12837 else
c152c796 12838 {
5241d853 12839 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12840 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12841 {
12842 ret = FALSE;
12843 break;
12844 }
12845 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12846 }
12847 }
9d0a14d3
RS
12848
12849 if (ret && eh_frame && elf_fde_list (sec))
12850 {
12851 struct elf_reloc_cookie cookie;
12852
12853 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12854 ret = FALSE;
12855 else
12856 {
12857 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12858 gc_mark_hook, &cookie))
12859 ret = FALSE;
12860 fini_reloc_cookie_for_section (&cookie, eh_frame);
12861 }
12862 }
12863
2f0c68f2
CM
12864 eh_frame = elf_section_eh_frame_entry (sec);
12865 if (ret && eh_frame && !eh_frame->gc_mark)
12866 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12867 ret = FALSE;
12868
c152c796
AM
12869 return ret;
12870}
12871
3c758495
TG
12872/* Scan and mark sections in a special or debug section group. */
12873
12874static void
12875_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12876{
12877 /* Point to first section of section group. */
12878 asection *ssec;
12879 /* Used to iterate the section group. */
12880 asection *msec;
12881
12882 bfd_boolean is_special_grp = TRUE;
12883 bfd_boolean is_debug_grp = TRUE;
12884
12885 /* First scan to see if group contains any section other than debug
12886 and special section. */
12887 ssec = msec = elf_next_in_group (grp);
12888 do
12889 {
12890 if ((msec->flags & SEC_DEBUGGING) == 0)
12891 is_debug_grp = FALSE;
12892
12893 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12894 is_special_grp = FALSE;
12895
12896 msec = elf_next_in_group (msec);
12897 }
12898 while (msec != ssec);
12899
12900 /* If this is a pure debug section group or pure special section group,
12901 keep all sections in this group. */
12902 if (is_debug_grp || is_special_grp)
12903 {
12904 do
12905 {
12906 msec->gc_mark = 1;
12907 msec = elf_next_in_group (msec);
12908 }
12909 while (msec != ssec);
12910 }
12911}
12912
7f6ab9f8
AM
12913/* Keep debug and special sections. */
12914
12915bfd_boolean
12916_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12917 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12918{
12919 bfd *ibfd;
12920
c72f2fb2 12921 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12922 {
12923 asection *isec;
12924 bfd_boolean some_kept;
b40bf0a2 12925 bfd_boolean debug_frag_seen;
b7c871ed 12926 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
12927
12928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12929 continue;
57963c05
AM
12930 isec = ibfd->sections;
12931 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12932 continue;
7f6ab9f8 12933
b40bf0a2
NC
12934 /* Ensure all linker created sections are kept,
12935 see if any other section is already marked,
12936 and note if we have any fragmented debug sections. */
b7c871ed 12937 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
12938 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12939 {
12940 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12941 isec->gc_mark = 1;
eb026f09
AM
12942 else if (isec->gc_mark
12943 && (isec->flags & SEC_ALLOC) != 0
12944 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 12945 some_kept = TRUE;
b40bf0a2 12946
535b785f 12947 if (!debug_frag_seen
b40bf0a2
NC
12948 && (isec->flags & SEC_DEBUGGING)
12949 && CONST_STRNEQ (isec->name, ".debug_line."))
12950 debug_frag_seen = TRUE;
7f6ab9f8
AM
12951 }
12952
eb026f09
AM
12953 /* If no non-note alloc section in this file will be kept, then
12954 we can toss out the debug and special sections. */
7f6ab9f8
AM
12955 if (!some_kept)
12956 continue;
12957
12958 /* Keep debug and special sections like .comment when they are
3c758495
TG
12959 not part of a group. Also keep section groups that contain
12960 just debug sections or special sections. */
7f6ab9f8 12961 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12962 {
12963 if ((isec->flags & SEC_GROUP) != 0)
12964 _bfd_elf_gc_mark_debug_special_section_group (isec);
12965 else if (((isec->flags & SEC_DEBUGGING) != 0
12966 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12967 && elf_next_in_group (isec) == NULL)
12968 isec->gc_mark = 1;
b7c871ed
L
12969 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12970 has_kept_debug_info = TRUE;
3c758495 12971 }
b40bf0a2 12972
b40bf0a2
NC
12973 /* Look for CODE sections which are going to be discarded,
12974 and find and discard any fragmented debug sections which
12975 are associated with that code section. */
b7c871ed
L
12976 if (debug_frag_seen)
12977 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12978 if ((isec->flags & SEC_CODE) != 0
12979 && isec->gc_mark == 0)
12980 {
12981 unsigned int ilen;
12982 asection *dsec;
b40bf0a2 12983
b7c871ed 12984 ilen = strlen (isec->name);
b40bf0a2 12985
b7c871ed
L
12986 /* Association is determined by the name of the debug
12987 section containing the name of the code section as
12988 a suffix. For example .debug_line.text.foo is a
12989 debug section associated with .text.foo. */
12990 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12991 {
12992 unsigned int dlen;
b40bf0a2 12993
b7c871ed
L
12994 if (dsec->gc_mark == 0
12995 || (dsec->flags & SEC_DEBUGGING) == 0)
12996 continue;
b40bf0a2 12997
b7c871ed 12998 dlen = strlen (dsec->name);
b40bf0a2 12999
b7c871ed
L
13000 if (dlen > ilen
13001 && strncmp (dsec->name + (dlen - ilen),
13002 isec->name, ilen) == 0)
b40bf0a2 13003 dsec->gc_mark = 0;
b7c871ed 13004 }
b40bf0a2 13005 }
b7c871ed
L
13006
13007 /* Mark debug sections referenced by kept debug sections. */
13008 if (has_kept_debug_info)
13009 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13010 if (isec->gc_mark
13011 && (isec->flags & SEC_DEBUGGING) != 0)
13012 if (!_bfd_elf_gc_mark (info, isec,
13013 elf_gc_mark_debug_section))
13014 return FALSE;
7f6ab9f8
AM
13015 }
13016 return TRUE;
13017}
13018
c152c796
AM
13019/* The sweep phase of garbage collection. Remove all garbage sections. */
13020
13021typedef bfd_boolean (*gc_sweep_hook_fn)
13022 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13023
13024static bfd_boolean
ccabcbe5 13025elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13026{
13027 bfd *sub;
ccabcbe5
AM
13028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13029 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 13030
c72f2fb2 13031 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13032 {
13033 asection *o;
13034
b19a8f85
L
13035 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13036 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13037 continue;
57963c05
AM
13038 o = sub->sections;
13039 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13040 continue;
c152c796
AM
13041
13042 for (o = sub->sections; o != NULL; o = o->next)
13043 {
a33dafc3
L
13044 /* When any section in a section group is kept, we keep all
13045 sections in the section group. If the first member of
13046 the section group is excluded, we will also exclude the
13047 group section. */
13048 if (o->flags & SEC_GROUP)
13049 {
13050 asection *first = elf_next_in_group (o);
13051 o->gc_mark = first->gc_mark;
13052 }
c152c796 13053
1e7eae0d 13054 if (o->gc_mark)
c152c796
AM
13055 continue;
13056
13057 /* Skip sweeping sections already excluded. */
13058 if (o->flags & SEC_EXCLUDE)
13059 continue;
13060
13061 /* Since this is early in the link process, it is simple
13062 to remove a section from the output. */
13063 o->flags |= SEC_EXCLUDE;
13064
c55fe096 13065 if (info->print_gc_sections && o->size != 0)
695344c0 13066 /* xgettext:c-format */
c08bb8dd
AM
13067 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13068 o, sub);
c17d87de 13069
c152c796
AM
13070 /* But we also have to update some of the relocation
13071 info we collected before. */
13072 if (gc_sweep_hook
e8aaee2a 13073 && (o->flags & SEC_RELOC) != 0
9850436d
AM
13074 && o->reloc_count != 0
13075 && !((info->strip == strip_all || info->strip == strip_debugger)
13076 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 13077 && !bfd_is_abs_section (o->output_section))
c152c796
AM
13078 {
13079 Elf_Internal_Rela *internal_relocs;
13080 bfd_boolean r;
13081
13082 internal_relocs
13083 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13084 info->keep_memory);
13085 if (internal_relocs == NULL)
13086 return FALSE;
13087
13088 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13089
13090 if (elf_section_data (o)->relocs != internal_relocs)
13091 free (internal_relocs);
13092
13093 if (!r)
13094 return FALSE;
13095 }
13096 }
13097 }
13098
c152c796
AM
13099 return TRUE;
13100}
13101
13102/* Propagate collected vtable information. This is called through
13103 elf_link_hash_traverse. */
13104
13105static bfd_boolean
13106elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13107{
c152c796 13108 /* Those that are not vtables. */
cbd0eecf
L
13109 if (h->start_stop
13110 || h->u2.vtable == NULL
13111 || h->u2.vtable->parent == NULL)
c152c796
AM
13112 return TRUE;
13113
13114 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13115 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13116 return TRUE;
13117
13118 /* If we've already been done, exit. */
cbd0eecf 13119 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13120 return TRUE;
13121
13122 /* Make sure the parent's table is up to date. */
cbd0eecf 13123 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13124
cbd0eecf 13125 if (h->u2.vtable->used == NULL)
c152c796
AM
13126 {
13127 /* None of this table's entries were referenced. Re-use the
13128 parent's table. */
cbd0eecf
L
13129 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13130 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13131 }
13132 else
13133 {
13134 size_t n;
13135 bfd_boolean *cu, *pu;
13136
13137 /* Or the parent's entries into ours. */
cbd0eecf 13138 cu = h->u2.vtable->used;
c152c796 13139 cu[-1] = TRUE;
cbd0eecf 13140 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13141 if (pu != NULL)
13142 {
13143 const struct elf_backend_data *bed;
13144 unsigned int log_file_align;
13145
13146 bed = get_elf_backend_data (h->root.u.def.section->owner);
13147 log_file_align = bed->s->log_file_align;
cbd0eecf 13148 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13149 while (n--)
13150 {
13151 if (*pu)
13152 *cu = TRUE;
13153 pu++;
13154 cu++;
13155 }
13156 }
13157 }
13158
13159 return TRUE;
13160}
13161
13162static bfd_boolean
13163elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13164{
13165 asection *sec;
13166 bfd_vma hstart, hend;
13167 Elf_Internal_Rela *relstart, *relend, *rel;
13168 const struct elf_backend_data *bed;
13169 unsigned int log_file_align;
13170
c152c796
AM
13171 /* Take care of both those symbols that do not describe vtables as
13172 well as those that are not loaded. */
cbd0eecf
L
13173 if (h->start_stop
13174 || h->u2.vtable == NULL
13175 || h->u2.vtable->parent == NULL)
c152c796
AM
13176 return TRUE;
13177
13178 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13179 || h->root.type == bfd_link_hash_defweak);
13180
13181 sec = h->root.u.def.section;
13182 hstart = h->root.u.def.value;
13183 hend = hstart + h->size;
13184
13185 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13186 if (!relstart)
13187 return *(bfd_boolean *) okp = FALSE;
13188 bed = get_elf_backend_data (sec->owner);
13189 log_file_align = bed->s->log_file_align;
13190
056bafd4 13191 relend = relstart + sec->reloc_count;
c152c796
AM
13192
13193 for (rel = relstart; rel < relend; ++rel)
13194 if (rel->r_offset >= hstart && rel->r_offset < hend)
13195 {
13196 /* If the entry is in use, do nothing. */
cbd0eecf
L
13197 if (h->u2.vtable->used
13198 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13199 {
13200 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13201 if (h->u2.vtable->used[entry])
c152c796
AM
13202 continue;
13203 }
13204 /* Otherwise, kill it. */
13205 rel->r_offset = rel->r_info = rel->r_addend = 0;
13206 }
13207
13208 return TRUE;
13209}
13210
87538722
AM
13211/* Mark sections containing dynamically referenced symbols. When
13212 building shared libraries, we must assume that any visible symbol is
13213 referenced. */
715df9b8 13214
64d03ab5
AM
13215bfd_boolean
13216bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13217{
87538722 13218 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13219 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13220
715df9b8
EB
13221 if ((h->root.type == bfd_link_hash_defined
13222 || h->root.type == bfd_link_hash_defweak)
87538722 13223 && (h->ref_dynamic
c4621b33 13224 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13225 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13226 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13227 && (!bfd_link_executable (info)
22185505 13228 || info->gc_keep_exported
b407645f
AM
13229 || info->export_dynamic
13230 || (h->dynamic
13231 && d != NULL
13232 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13233 && (h->versioned >= versioned
54e8959c
L
13234 || !bfd_hide_sym_by_version (info->version_info,
13235 h->root.root.string)))))
715df9b8
EB
13236 h->root.u.def.section->flags |= SEC_KEEP;
13237
13238 return TRUE;
13239}
3b36f7e6 13240
74f0fb50
AM
13241/* Keep all sections containing symbols undefined on the command-line,
13242 and the section containing the entry symbol. */
13243
13244void
13245_bfd_elf_gc_keep (struct bfd_link_info *info)
13246{
13247 struct bfd_sym_chain *sym;
13248
13249 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13250 {
13251 struct elf_link_hash_entry *h;
13252
13253 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13254 FALSE, FALSE, FALSE);
13255
13256 if (h != NULL
13257 && (h->root.type == bfd_link_hash_defined
13258 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13259 && !bfd_is_abs_section (h->root.u.def.section)
13260 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13261 h->root.u.def.section->flags |= SEC_KEEP;
13262 }
13263}
13264
2f0c68f2
CM
13265bfd_boolean
13266bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13267 struct bfd_link_info *info)
13268{
13269 bfd *ibfd = info->input_bfds;
13270
13271 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13272 {
13273 asection *sec;
13274 struct elf_reloc_cookie cookie;
13275
13276 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13277 continue;
57963c05
AM
13278 sec = ibfd->sections;
13279 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13280 continue;
2f0c68f2
CM
13281
13282 if (!init_reloc_cookie (&cookie, info, ibfd))
13283 return FALSE;
13284
13285 for (sec = ibfd->sections; sec; sec = sec->next)
13286 {
13287 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13288 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13289 {
13290 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13291 fini_reloc_cookie_rels (&cookie, sec);
13292 }
13293 }
13294 }
13295 return TRUE;
13296}
13297
c152c796
AM
13298/* Do mark and sweep of unused sections. */
13299
13300bfd_boolean
13301bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13302{
13303 bfd_boolean ok = TRUE;
13304 bfd *sub;
6a5bb875 13305 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13307 struct elf_link_hash_table *htab;
c152c796 13308
64d03ab5 13309 if (!bed->can_gc_sections
715df9b8 13310 || !is_elf_hash_table (info->hash))
c152c796 13311 {
4eca0228 13312 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13313 return TRUE;
13314 }
13315
74f0fb50 13316 bed->gc_keep (info);
da44f4e5 13317 htab = elf_hash_table (info);
74f0fb50 13318
9d0a14d3
RS
13319 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13320 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13321 for (sub = info->input_bfds;
13322 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13323 sub = sub->link.next)
9d0a14d3
RS
13324 {
13325 asection *sec;
13326 struct elf_reloc_cookie cookie;
13327
57963c05
AM
13328 sec = sub->sections;
13329 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13330 continue;
9d0a14d3 13331 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13332 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13333 {
13334 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13335 if (elf_section_data (sec)->sec_info
13336 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13337 elf_eh_frame_section (sub) = sec;
13338 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13339 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13340 }
13341 }
9d0a14d3 13342
c152c796 13343 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13344 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13345 if (!ok)
13346 return FALSE;
13347
13348 /* Kill the vtable relocations that were not used. */
da44f4e5 13349 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13350 if (!ok)
13351 return FALSE;
13352
715df9b8 13353 /* Mark dynamically referenced symbols. */
22185505 13354 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13355 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13356
715df9b8 13357 /* Grovel through relocs to find out who stays ... */
64d03ab5 13358 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13359 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13360 {
13361 asection *o;
13362
b19a8f85
L
13363 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13364 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13365 continue;
13366
57963c05
AM
13367 o = sub->sections;
13368 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13369 continue;
13370
7f6ab9f8
AM
13371 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13372 Also treat note sections as a root, if the section is not part
13373 of a group. */
c152c796 13374 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13375 if (!o->gc_mark
13376 && (o->flags & SEC_EXCLUDE) == 0
24007750 13377 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13378 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13379 && elf_next_in_group (o) == NULL )))
13380 {
13381 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13382 return FALSE;
13383 }
c152c796
AM
13384 }
13385
6a5bb875 13386 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13387 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13388
c152c796 13389 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13390 return elf_gc_sweep (abfd, info);
c152c796
AM
13391}
13392\f
13393/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13394
13395bfd_boolean
13396bfd_elf_gc_record_vtinherit (bfd *abfd,
13397 asection *sec,
13398 struct elf_link_hash_entry *h,
13399 bfd_vma offset)
13400{
13401 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13402 struct elf_link_hash_entry **search, *child;
ef53be89 13403 size_t extsymcount;
c152c796
AM
13404 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13405
13406 /* The sh_info field of the symtab header tells us where the
13407 external symbols start. We don't care about the local symbols at
13408 this point. */
13409 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13410 if (!elf_bad_symtab (abfd))
13411 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13412
13413 sym_hashes = elf_sym_hashes (abfd);
13414 sym_hashes_end = sym_hashes + extsymcount;
13415
13416 /* Hunt down the child symbol, which is in this section at the same
13417 offset as the relocation. */
13418 for (search = sym_hashes; search != sym_hashes_end; ++search)
13419 {
13420 if ((child = *search) != NULL
13421 && (child->root.type == bfd_link_hash_defined
13422 || child->root.type == bfd_link_hash_defweak)
13423 && child->root.u.def.section == sec
13424 && child->root.u.def.value == offset)
13425 goto win;
13426 }
13427
695344c0 13428 /* xgettext:c-format */
76cfced5
AM
13429 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13430 abfd, sec, offset);
c152c796
AM
13431 bfd_set_error (bfd_error_invalid_operation);
13432 return FALSE;
13433
13434 win:
cbd0eecf 13435 if (!child->u2.vtable)
f6e332e6 13436 {
cbd0eecf
L
13437 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13438 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13439 if (!child->u2.vtable)
f6e332e6
AM
13440 return FALSE;
13441 }
c152c796
AM
13442 if (!h)
13443 {
13444 /* This *should* only be the absolute section. It could potentially
13445 be that someone has defined a non-global vtable though, which
13446 would be bad. It isn't worth paging in the local symbols to be
13447 sure though; that case should simply be handled by the assembler. */
13448
cbd0eecf 13449 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13450 }
13451 else
cbd0eecf 13452 child->u2.vtable->parent = h;
c152c796
AM
13453
13454 return TRUE;
13455}
13456
13457/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13458
13459bfd_boolean
13460bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13461 asection *sec ATTRIBUTE_UNUSED,
13462 struct elf_link_hash_entry *h,
13463 bfd_vma addend)
13464{
13465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13466 unsigned int log_file_align = bed->s->log_file_align;
13467
cbd0eecf 13468 if (!h->u2.vtable)
f6e332e6 13469 {
cbd0eecf
L
13470 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13471 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13472 if (!h->u2.vtable)
f6e332e6
AM
13473 return FALSE;
13474 }
13475
cbd0eecf 13476 if (addend >= h->u2.vtable->size)
c152c796
AM
13477 {
13478 size_t size, bytes, file_align;
cbd0eecf 13479 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13480
13481 /* While the symbol is undefined, we have to be prepared to handle
13482 a zero size. */
13483 file_align = 1 << log_file_align;
13484 if (h->root.type == bfd_link_hash_undefined)
13485 size = addend + file_align;
13486 else
13487 {
13488 size = h->size;
13489 if (addend >= size)
13490 {
13491 /* Oops! We've got a reference past the defined end of
13492 the table. This is probably a bug -- shall we warn? */
13493 size = addend + file_align;
13494 }
13495 }
13496 size = (size + file_align - 1) & -file_align;
13497
13498 /* Allocate one extra entry for use as a "done" flag for the
13499 consolidation pass. */
13500 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13501
13502 if (ptr)
13503 {
a50b1753 13504 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13505
13506 if (ptr != NULL)
13507 {
13508 size_t oldbytes;
13509
cbd0eecf 13510 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13511 * sizeof (bfd_boolean));
13512 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13513 }
13514 }
13515 else
a50b1753 13516 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13517
13518 if (ptr == NULL)
13519 return FALSE;
13520
13521 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13522 h->u2.vtable->used = ptr + 1;
13523 h->u2.vtable->size = size;
c152c796
AM
13524 }
13525
cbd0eecf 13526 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13527
13528 return TRUE;
13529}
13530
ae17ab41
CM
13531/* Map an ELF section header flag to its corresponding string. */
13532typedef struct
13533{
13534 char *flag_name;
13535 flagword flag_value;
13536} elf_flags_to_name_table;
13537
13538static elf_flags_to_name_table elf_flags_to_names [] =
13539{
13540 { "SHF_WRITE", SHF_WRITE },
13541 { "SHF_ALLOC", SHF_ALLOC },
13542 { "SHF_EXECINSTR", SHF_EXECINSTR },
13543 { "SHF_MERGE", SHF_MERGE },
13544 { "SHF_STRINGS", SHF_STRINGS },
13545 { "SHF_INFO_LINK", SHF_INFO_LINK},
13546 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13547 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13548 { "SHF_GROUP", SHF_GROUP },
13549 { "SHF_TLS", SHF_TLS },
13550 { "SHF_MASKOS", SHF_MASKOS },
13551 { "SHF_EXCLUDE", SHF_EXCLUDE },
13552};
13553
b9c361e0
JL
13554/* Returns TRUE if the section is to be included, otherwise FALSE. */
13555bfd_boolean
ae17ab41 13556bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13557 struct flag_info *flaginfo,
b9c361e0 13558 asection *section)
ae17ab41 13559{
8b127cbc 13560 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13561
8b127cbc 13562 if (!flaginfo->flags_initialized)
ae17ab41 13563 {
8b127cbc
AM
13564 bfd *obfd = info->output_bfd;
13565 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13566 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13567 int with_hex = 0;
13568 int without_hex = 0;
13569
8b127cbc 13570 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13571 {
b9c361e0 13572 unsigned i;
8b127cbc 13573 flagword (*lookup) (char *);
ae17ab41 13574
8b127cbc
AM
13575 lookup = bed->elf_backend_lookup_section_flags_hook;
13576 if (lookup != NULL)
ae17ab41 13577 {
8b127cbc 13578 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13579
13580 if (hexval != 0)
13581 {
13582 if (tf->with == with_flags)
13583 with_hex |= hexval;
13584 else if (tf->with == without_flags)
13585 without_hex |= hexval;
13586 tf->valid = TRUE;
13587 continue;
13588 }
ae17ab41 13589 }
8b127cbc 13590 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13591 {
8b127cbc 13592 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13593 {
13594 if (tf->with == with_flags)
13595 with_hex |= elf_flags_to_names[i].flag_value;
13596 else if (tf->with == without_flags)
13597 without_hex |= elf_flags_to_names[i].flag_value;
13598 tf->valid = TRUE;
13599 break;
13600 }
13601 }
8b127cbc 13602 if (!tf->valid)
b9c361e0 13603 {
68ffbac6 13604 info->callbacks->einfo
8b127cbc 13605 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13606 return FALSE;
ae17ab41
CM
13607 }
13608 }
8b127cbc
AM
13609 flaginfo->flags_initialized = TRUE;
13610 flaginfo->only_with_flags |= with_hex;
13611 flaginfo->not_with_flags |= without_hex;
ae17ab41 13612 }
ae17ab41 13613
8b127cbc 13614 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13615 return FALSE;
13616
8b127cbc 13617 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13618 return FALSE;
13619
13620 return TRUE;
ae17ab41
CM
13621}
13622
c152c796
AM
13623struct alloc_got_off_arg {
13624 bfd_vma gotoff;
10455f89 13625 struct bfd_link_info *info;
c152c796
AM
13626};
13627
13628/* We need a special top-level link routine to convert got reference counts
13629 to real got offsets. */
13630
13631static bfd_boolean
13632elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13633{
a50b1753 13634 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13635 bfd *obfd = gofarg->info->output_bfd;
13636 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13637
c152c796
AM
13638 if (h->got.refcount > 0)
13639 {
13640 h->got.offset = gofarg->gotoff;
10455f89 13641 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13642 }
13643 else
13644 h->got.offset = (bfd_vma) -1;
13645
13646 return TRUE;
13647}
13648
13649/* And an accompanying bit to work out final got entry offsets once
13650 we're done. Should be called from final_link. */
13651
13652bfd_boolean
13653bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13654 struct bfd_link_info *info)
13655{
13656 bfd *i;
13657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13658 bfd_vma gotoff;
c152c796
AM
13659 struct alloc_got_off_arg gofarg;
13660
10455f89
HPN
13661 BFD_ASSERT (abfd == info->output_bfd);
13662
c152c796
AM
13663 if (! is_elf_hash_table (info->hash))
13664 return FALSE;
13665
13666 /* The GOT offset is relative to the .got section, but the GOT header is
13667 put into the .got.plt section, if the backend uses it. */
13668 if (bed->want_got_plt)
13669 gotoff = 0;
13670 else
13671 gotoff = bed->got_header_size;
13672
13673 /* Do the local .got entries first. */
c72f2fb2 13674 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13675 {
13676 bfd_signed_vma *local_got;
ef53be89 13677 size_t j, locsymcount;
c152c796
AM
13678 Elf_Internal_Shdr *symtab_hdr;
13679
13680 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13681 continue;
13682
13683 local_got = elf_local_got_refcounts (i);
13684 if (!local_got)
13685 continue;
13686
13687 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13688 if (elf_bad_symtab (i))
13689 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13690 else
13691 locsymcount = symtab_hdr->sh_info;
13692
13693 for (j = 0; j < locsymcount; ++j)
13694 {
13695 if (local_got[j] > 0)
13696 {
13697 local_got[j] = gotoff;
10455f89 13698 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13699 }
13700 else
13701 local_got[j] = (bfd_vma) -1;
13702 }
13703 }
13704
13705 /* Then the global .got entries. .plt refcounts are handled by
13706 adjust_dynamic_symbol */
13707 gofarg.gotoff = gotoff;
10455f89 13708 gofarg.info = info;
c152c796
AM
13709 elf_link_hash_traverse (elf_hash_table (info),
13710 elf_gc_allocate_got_offsets,
13711 &gofarg);
13712 return TRUE;
13713}
13714
13715/* Many folk need no more in the way of final link than this, once
13716 got entry reference counting is enabled. */
13717
13718bfd_boolean
13719bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13720{
13721 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13722 return FALSE;
13723
13724 /* Invoke the regular ELF backend linker to do all the work. */
13725 return bfd_elf_final_link (abfd, info);
13726}
13727
13728bfd_boolean
13729bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13730{
a50b1753 13731 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13732
13733 if (rcookie->bad_symtab)
13734 rcookie->rel = rcookie->rels;
13735
13736 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13737 {
13738 unsigned long r_symndx;
13739
13740 if (! rcookie->bad_symtab)
13741 if (rcookie->rel->r_offset > offset)
13742 return FALSE;
13743 if (rcookie->rel->r_offset != offset)
13744 continue;
13745
13746 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13747 if (r_symndx == STN_UNDEF)
c152c796
AM
13748 return TRUE;
13749
13750 if (r_symndx >= rcookie->locsymcount
13751 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13752 {
13753 struct elf_link_hash_entry *h;
13754
13755 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13756
13757 while (h->root.type == bfd_link_hash_indirect
13758 || h->root.type == bfd_link_hash_warning)
13759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13760
13761 if ((h->root.type == bfd_link_hash_defined
13762 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13763 && (h->root.u.def.section->owner != rcookie->abfd
13764 || h->root.u.def.section->kept_section != NULL
13765 || discarded_section (h->root.u.def.section)))
c152c796 13766 return TRUE;
c152c796
AM
13767 }
13768 else
13769 {
13770 /* It's not a relocation against a global symbol,
13771 but it could be a relocation against a local
13772 symbol for a discarded section. */
13773 asection *isec;
13774 Elf_Internal_Sym *isym;
13775
13776 /* Need to: get the symbol; get the section. */
13777 isym = &rcookie->locsyms[r_symndx];
cb33740c 13778 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13779 if (isec != NULL
13780 && (isec->kept_section != NULL
13781 || discarded_section (isec)))
cb33740c 13782 return TRUE;
c152c796
AM
13783 }
13784 return FALSE;
13785 }
13786 return FALSE;
13787}
13788
13789/* Discard unneeded references to discarded sections.
75938853
AM
13790 Returns -1 on error, 1 if any section's size was changed, 0 if
13791 nothing changed. This function assumes that the relocations are in
13792 sorted order, which is true for all known assemblers. */
c152c796 13793
75938853 13794int
c152c796
AM
13795bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13796{
13797 struct elf_reloc_cookie cookie;
18cd5bce 13798 asection *o;
c152c796 13799 bfd *abfd;
75938853 13800 int changed = 0;
c152c796
AM
13801
13802 if (info->traditional_format
13803 || !is_elf_hash_table (info->hash))
75938853 13804 return 0;
c152c796 13805
18cd5bce
AM
13806 o = bfd_get_section_by_name (output_bfd, ".stab");
13807 if (o != NULL)
c152c796 13808 {
18cd5bce 13809 asection *i;
c152c796 13810
18cd5bce 13811 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13812 {
18cd5bce
AM
13813 if (i->size == 0
13814 || i->reloc_count == 0
13815 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13816 continue;
c152c796 13817
18cd5bce
AM
13818 abfd = i->owner;
13819 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13820 continue;
c152c796 13821
18cd5bce 13822 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13823 return -1;
c152c796 13824
18cd5bce
AM
13825 if (_bfd_discard_section_stabs (abfd, i,
13826 elf_section_data (i)->sec_info,
5241d853
RS
13827 bfd_elf_reloc_symbol_deleted_p,
13828 &cookie))
75938853 13829 changed = 1;
18cd5bce
AM
13830
13831 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13832 }
18cd5bce
AM
13833 }
13834
2f0c68f2
CM
13835 o = NULL;
13836 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13837 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13838 if (o != NULL)
13839 {
13840 asection *i;
d7153c4a 13841 int eh_changed = 0;
79a94a2a 13842 unsigned int eh_alignment;
c152c796 13843
18cd5bce 13844 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13845 {
18cd5bce
AM
13846 if (i->size == 0)
13847 continue;
13848
13849 abfd = i->owner;
13850 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13851 continue;
13852
13853 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13854 return -1;
18cd5bce
AM
13855
13856 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13857 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13858 bfd_elf_reloc_symbol_deleted_p,
13859 &cookie))
d7153c4a
AM
13860 {
13861 eh_changed = 1;
13862 if (i->size != i->rawsize)
13863 changed = 1;
13864 }
18cd5bce
AM
13865
13866 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13867 }
9866ffe2 13868
79a94a2a 13869 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
13870 /* Skip over zero terminator, and prevent empty sections from
13871 adding alignment padding at the end. */
13872 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13873 if (i->size == 0)
13874 i->flags |= SEC_EXCLUDE;
13875 else if (i->size > 4)
13876 break;
13877 /* The last non-empty eh_frame section doesn't need padding. */
13878 if (i != NULL)
13879 i = i->map_tail.s;
13880 /* Any prior sections must pad the last FDE out to the output
13881 section alignment. Otherwise we might have zero padding
13882 between sections, which would be seen as a terminator. */
13883 for (; i != NULL; i = i->map_tail.s)
13884 if (i->size == 4)
13885 /* All but the last zero terminator should have been removed. */
13886 BFD_FAIL ();
13887 else
13888 {
13889 bfd_size_type size
13890 = (i->size + eh_alignment - 1) & -eh_alignment;
13891 if (i->size != size)
af471f82 13892 {
9866ffe2
AM
13893 i->size = size;
13894 changed = 1;
13895 eh_changed = 1;
af471f82 13896 }
9866ffe2 13897 }
d7153c4a
AM
13898 if (eh_changed)
13899 elf_link_hash_traverse (elf_hash_table (info),
13900 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 13901 }
c152c796 13902
18cd5bce
AM
13903 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13904 {
13905 const struct elf_backend_data *bed;
57963c05 13906 asection *s;
c152c796 13907
18cd5bce
AM
13908 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13909 continue;
57963c05
AM
13910 s = abfd->sections;
13911 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13912 continue;
18cd5bce
AM
13913
13914 bed = get_elf_backend_data (abfd);
13915
13916 if (bed->elf_backend_discard_info != NULL)
13917 {
13918 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13919 return -1;
18cd5bce
AM
13920
13921 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13922 changed = 1;
18cd5bce
AM
13923
13924 fini_reloc_cookie (&cookie, abfd);
13925 }
c152c796
AM
13926 }
13927
2f0c68f2
CM
13928 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13929 _bfd_elf_end_eh_frame_parsing (info);
13930
13931 if (info->eh_frame_hdr_type
0e1862bb 13932 && !bfd_link_relocatable (info)
c152c796 13933 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13934 changed = 1;
c152c796 13935
75938853 13936 return changed;
c152c796 13937}
082b7297 13938
43e1669b 13939bfd_boolean
0c511000 13940_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13941 asection *sec,
c0f00686 13942 struct bfd_link_info *info)
082b7297
L
13943{
13944 flagword flags;
c77ec726 13945 const char *name, *key;
082b7297
L
13946 struct bfd_section_already_linked *l;
13947 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13948
c77ec726
AM
13949 if (sec->output_section == bfd_abs_section_ptr)
13950 return FALSE;
0c511000 13951
c77ec726 13952 flags = sec->flags;
0c511000 13953
c77ec726
AM
13954 /* Return if it isn't a linkonce section. A comdat group section
13955 also has SEC_LINK_ONCE set. */
13956 if ((flags & SEC_LINK_ONCE) == 0)
13957 return FALSE;
0c511000 13958
c77ec726
AM
13959 /* Don't put group member sections on our list of already linked
13960 sections. They are handled as a group via their group section. */
13961 if (elf_sec_group (sec) != NULL)
13962 return FALSE;
0c511000 13963
c77ec726
AM
13964 /* For a SHT_GROUP section, use the group signature as the key. */
13965 name = sec->name;
13966 if ((flags & SEC_GROUP) != 0
13967 && elf_next_in_group (sec) != NULL
13968 && elf_group_name (elf_next_in_group (sec)) != NULL)
13969 key = elf_group_name (elf_next_in_group (sec));
13970 else
13971 {
13972 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13973 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13974 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13975 key++;
0c511000 13976 else
c77ec726
AM
13977 /* Must be a user linkonce section that doesn't follow gcc's
13978 naming convention. In this case we won't be matching
13979 single member groups. */
13980 key = name;
0c511000 13981 }
6d2cd210 13982
c77ec726 13983 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13984
13985 for (l = already_linked_list->entry; l != NULL; l = l->next)
13986 {
c2370991 13987 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13988 sections with a signature of <key> (<key> is some string),
13989 and linkonce sections named .gnu.linkonce.<type>.<key>.
13990 Match like sections. LTO plugin sections are an exception.
13991 They are always named .gnu.linkonce.t.<key> and match either
13992 type of section. */
13993 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13994 && ((flags & SEC_GROUP) != 0
13995 || strcmp (name, l->sec->name) == 0))
13996 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13997 {
13998 /* The section has already been linked. See if we should
6d2cd210 13999 issue a warning. */
c77ec726
AM
14000 if (!_bfd_handle_already_linked (sec, l, info))
14001 return FALSE;
082b7297 14002
c77ec726 14003 if (flags & SEC_GROUP)
3d7f7666 14004 {
c77ec726
AM
14005 asection *first = elf_next_in_group (sec);
14006 asection *s = first;
3d7f7666 14007
c77ec726 14008 while (s != NULL)
3d7f7666 14009 {
c77ec726
AM
14010 s->output_section = bfd_abs_section_ptr;
14011 /* Record which group discards it. */
14012 s->kept_section = l->sec;
14013 s = elf_next_in_group (s);
14014 /* These lists are circular. */
14015 if (s == first)
14016 break;
3d7f7666
L
14017 }
14018 }
082b7297 14019
43e1669b 14020 return TRUE;
082b7297
L
14021 }
14022 }
14023
c77ec726
AM
14024 /* A single member comdat group section may be discarded by a
14025 linkonce section and vice versa. */
14026 if ((flags & SEC_GROUP) != 0)
3d7f7666 14027 {
c77ec726 14028 asection *first = elf_next_in_group (sec);
c2370991 14029
c77ec726
AM
14030 if (first != NULL && elf_next_in_group (first) == first)
14031 /* Check this single member group against linkonce sections. */
14032 for (l = already_linked_list->entry; l != NULL; l = l->next)
14033 if ((l->sec->flags & SEC_GROUP) == 0
14034 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14035 {
14036 first->output_section = bfd_abs_section_ptr;
14037 first->kept_section = l->sec;
14038 sec->output_section = bfd_abs_section_ptr;
14039 break;
14040 }
14041 }
14042 else
14043 /* Check this linkonce section against single member groups. */
14044 for (l = already_linked_list->entry; l != NULL; l = l->next)
14045 if (l->sec->flags & SEC_GROUP)
6d2cd210 14046 {
c77ec726 14047 asection *first = elf_next_in_group (l->sec);
6d2cd210 14048
c77ec726
AM
14049 if (first != NULL
14050 && elf_next_in_group (first) == first
14051 && bfd_elf_match_symbols_in_sections (first, sec, info))
14052 {
14053 sec->output_section = bfd_abs_section_ptr;
14054 sec->kept_section = first;
14055 break;
14056 }
6d2cd210 14057 }
0c511000 14058
c77ec726
AM
14059 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14060 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14061 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14062 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14063 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14064 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14065 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14066 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14067 The reverse order cannot happen as there is never a bfd with only the
14068 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14069 matter as here were are looking only for cross-bfd sections. */
14070
14071 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14072 for (l = already_linked_list->entry; l != NULL; l = l->next)
14073 if ((l->sec->flags & SEC_GROUP) == 0
14074 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14075 {
14076 if (abfd != l->sec->owner)
14077 sec->output_section = bfd_abs_section_ptr;
14078 break;
14079 }
80c29487 14080
082b7297 14081 /* This is the first section with this name. Record it. */
c77ec726 14082 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14083 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14084 return sec->output_section == bfd_abs_section_ptr;
082b7297 14085}
81e1b023 14086
a4d8e49b
L
14087bfd_boolean
14088_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14089{
14090 return sym->st_shndx == SHN_COMMON;
14091}
14092
14093unsigned int
14094_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14095{
14096 return SHN_COMMON;
14097}
14098
14099asection *
14100_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14101{
14102 return bfd_com_section_ptr;
14103}
10455f89
HPN
14104
14105bfd_vma
14106_bfd_elf_default_got_elt_size (bfd *abfd,
14107 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14108 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14109 bfd *ibfd ATTRIBUTE_UNUSED,
14110 unsigned long symndx ATTRIBUTE_UNUSED)
14111{
14112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14113 return bed->s->arch_size / 8;
14114}
83bac4b0
NC
14115
14116/* Routines to support the creation of dynamic relocs. */
14117
83bac4b0
NC
14118/* Returns the name of the dynamic reloc section associated with SEC. */
14119
14120static const char *
14121get_dynamic_reloc_section_name (bfd * abfd,
14122 asection * sec,
14123 bfd_boolean is_rela)
14124{
ddcf1fcf
BS
14125 char *name;
14126 const char *old_name = bfd_get_section_name (NULL, sec);
14127 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14128
ddcf1fcf 14129 if (old_name == NULL)
83bac4b0
NC
14130 return NULL;
14131
ddcf1fcf 14132 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14133 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14134
14135 return name;
14136}
14137
14138/* Returns the dynamic reloc section associated with SEC.
14139 If necessary compute the name of the dynamic reloc section based
14140 on SEC's name (looked up in ABFD's string table) and the setting
14141 of IS_RELA. */
14142
14143asection *
14144_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14145 asection * sec,
14146 bfd_boolean is_rela)
14147{
14148 asection * reloc_sec = elf_section_data (sec)->sreloc;
14149
14150 if (reloc_sec == NULL)
14151 {
14152 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14153
14154 if (name != NULL)
14155 {
3d4d4302 14156 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14157
14158 if (reloc_sec != NULL)
14159 elf_section_data (sec)->sreloc = reloc_sec;
14160 }
14161 }
14162
14163 return reloc_sec;
14164}
14165
14166/* Returns the dynamic reloc section associated with SEC. If the
14167 section does not exist it is created and attached to the DYNOBJ
14168 bfd and stored in the SRELOC field of SEC's elf_section_data
14169 structure.
f8076f98 14170
83bac4b0
NC
14171 ALIGNMENT is the alignment for the newly created section and
14172 IS_RELA defines whether the name should be .rela.<SEC's name>
14173 or .rel.<SEC's name>. The section name is looked up in the
14174 string table associated with ABFD. */
14175
14176asection *
ca4be51c
AM
14177_bfd_elf_make_dynamic_reloc_section (asection *sec,
14178 bfd *dynobj,
14179 unsigned int alignment,
14180 bfd *abfd,
14181 bfd_boolean is_rela)
83bac4b0
NC
14182{
14183 asection * reloc_sec = elf_section_data (sec)->sreloc;
14184
14185 if (reloc_sec == NULL)
14186 {
14187 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14188
14189 if (name == NULL)
14190 return NULL;
14191
3d4d4302 14192 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14193
14194 if (reloc_sec == NULL)
14195 {
3d4d4302
AM
14196 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14197 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14198 if ((sec->flags & SEC_ALLOC) != 0)
14199 flags |= SEC_ALLOC | SEC_LOAD;
14200
3d4d4302 14201 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14202 if (reloc_sec != NULL)
14203 {
8877b5e5
AM
14204 /* _bfd_elf_get_sec_type_attr chooses a section type by
14205 name. Override as it may be wrong, eg. for a user
14206 section named "auto" we'll get ".relauto" which is
14207 seen to be a .rela section. */
14208 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14209 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14210 reloc_sec = NULL;
14211 }
14212 }
14213
14214 elf_section_data (sec)->sreloc = reloc_sec;
14215 }
14216
14217 return reloc_sec;
14218}
1338dd10 14219
bffebb6b
AM
14220/* Copy the ELF symbol type and other attributes for a linker script
14221 assignment from HSRC to HDEST. Generally this should be treated as
14222 if we found a strong non-dynamic definition for HDEST (except that
14223 ld ignores multiple definition errors). */
1338dd10 14224void
bffebb6b
AM
14225_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14226 struct bfd_link_hash_entry *hdest,
14227 struct bfd_link_hash_entry *hsrc)
1338dd10 14228{
bffebb6b
AM
14229 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14230 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14231 Elf_Internal_Sym isym;
1338dd10
PB
14232
14233 ehdest->type = ehsrc->type;
35fc36a8 14234 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14235
14236 isym.st_other = ehsrc->other;
b8417128 14237 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14238}
351f65ca
L
14239
14240/* Append a RELA relocation REL to section S in BFD. */
14241
14242void
14243elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14244{
14245 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14246 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14247 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14248 bed->s->swap_reloca_out (abfd, rel, loc);
14249}
14250
14251/* Append a REL relocation REL to section S in BFD. */
14252
14253void
14254elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14255{
14256 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14257 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14258 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14259 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14260}
7dba9362
AM
14261
14262/* Define __start, __stop, .startof. or .sizeof. symbol. */
14263
14264struct bfd_link_hash_entry *
14265bfd_elf_define_start_stop (struct bfd_link_info *info,
14266 const char *symbol, asection *sec)
14267{
487b6440 14268 struct elf_link_hash_entry *h;
7dba9362 14269
487b6440
AM
14270 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14271 FALSE, FALSE, TRUE);
14272 if (h != NULL
14273 && (h->root.type == bfd_link_hash_undefined
14274 || h->root.type == bfd_link_hash_undefweak
14275 || (h->ref_regular && !h->def_regular)))
7dba9362 14276 {
487b6440
AM
14277 h->root.type = bfd_link_hash_defined;
14278 h->root.u.def.section = sec;
14279 h->root.u.def.value = 0;
14280 h->def_regular = 1;
14281 h->def_dynamic = 0;
14282 h->start_stop = 1;
14283 h->u2.start_stop_section = sec;
14284 if (symbol[0] == '.')
14285 {
14286 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14287 const struct elf_backend_data *bed;
14288 bed = get_elf_backend_data (info->output_bfd);
14289 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440
AM
14290 }
14291 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14292 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14293 return &h->root;
7dba9362 14294 }
487b6440 14295 return NULL;
7dba9362 14296}
This page took 2.498629 seconds and 4 git commands to generate.