Begone elf_linker
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
82704155 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
07d6d2b8 79 return h->root.u.def.section;
2f0c68f2
CM
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
ad32986f 121 bh = &h->root;
d98685ac 122 }
ad32986f
NC
123 else
124 bh = NULL;
d98685ac 125
cf18fda4 126 bed = get_elf_backend_data (abfd);
d98685ac 127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 128 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
ad32986f 132 BFD_ASSERT (h != NULL);
d98685ac 133 h->def_regular = 1;
e28df02b 134 h->non_elf = 0;
12b2843a 135 h->root.linker_def = 1;
d98685ac 136 h->type = STT_OBJECT;
00b7642b
AM
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 139
ccabcbe5 140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
141 return h;
142}
143
b34976b6 144bfd_boolean
268b6b39 145_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
146{
147 flagword flags;
aad5d350 148 asection *s;
252b5132 149 struct elf_link_hash_entry *h;
9c5bfbb7 150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 151 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
152
153 /* This function may be called more than once. */
ce558b89 154 if (htab->sgot != NULL)
b34976b6 155 return TRUE;
252b5132 156
e5a52504 157 flags = bed->dynamic_sec_flags;
252b5132 158
14b2f831
AM
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
6de2ae4a
L
164 if (s == NULL
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
252b5132 168
14b2f831 169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
170 if (s == NULL
171 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
252b5132
RH
175 if (bed->want_got_plt)
176 {
14b2f831 177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 178 if (s == NULL
6de2ae4a
L
179 || !bfd_set_section_alignment (abfd, s,
180 bed->s->log_file_align))
b34976b6 181 return FALSE;
6de2ae4a 182 htab->sgotplt = s;
252b5132
RH
183 }
184
64e77c6d
L
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
187
2517a57f
AM
188 if (bed->want_got_sym)
189 {
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
6de2ae4a
L
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
2517a57f 196 elf_hash_table (info)->hgot = h;
d98685ac
AM
197 if (h == NULL)
198 return FALSE;
2517a57f 199 }
252b5132 200
b34976b6 201 return TRUE;
252b5132
RH
202}
203\f
7e9f0867
AM
204/* Create a strtab to hold the dynamic symbol names. */
205static bfd_boolean
206_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207{
208 struct elf_link_hash_table *hash_table;
209
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
6cd255ca
L
212 {
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 {
219 bfd *ibfd;
57963c05 220 asection *s;
6cd255ca 221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 222 if ((ibfd->flags
57963c05
AM
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
48e30f52
L
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
b531344c
MR
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
45d6a902 698
f5385ebf 699 h->def_regular = 1;
45d6a902 700
eb8476a6 701 if (hidden)
fe21a8fc 702 {
91d6fa6a 703 bed = get_elf_backend_data (output_bfd);
b8297068
AM
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
6fa3860b
PB
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
0e1862bb 711 if (!bfd_link_relocatable (info)
6fa3860b
PB
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
f5385ebf
AM
717 if ((h->def_dynamic
718 || h->ref_dynamic
6b3b0ab8
L
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 721 && !h->forced_local
45d6a902
AM
722 && h->dynindx == -1)
723 {
c152c796 724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
60d67dc8 730 if (h->is_weakalias)
45d6a902 731 {
60d67dc8
AM
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741}
42751cf3 742
8c58d23b
AM
743/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747int
c152c796
AM
748bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
8c58d23b
AM
751{
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
ef53be89 756 size_t dynstr_index;
8c58d23b
AM
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
0eddce27 761 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
a50b1753 770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 776 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 783 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
b34976b6 810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 811 if (dynstr_index == (size_t) -1)
8c58d23b
AM
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830}
831
30b30c21 832/* Return the dynindex of a local dynamic symbol. */
42751cf3 833
30b30c21 834long
268b6b39
AM
835_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
30b30c21
RH
838{
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845}
846
847/* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
b34976b6 851static bfd_boolean
268b6b39
AM
852elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
42751cf3 854{
a50b1753 855 size_t *count = (size_t *) data;
30b30c21 856
6fa3860b
PB
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864}
865
866
867/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870static bfd_boolean
871elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873{
a50b1753 874 size_t *count = (size_t *) data;
6fa3860b 875
6fa3860b
PB
876 if (!h->forced_local)
877 return TRUE;
878
42751cf3 879 if (h->dynindx != -1)
30b30c21
RH
880 h->dynindx = ++(*count);
881
b34976b6 882 return TRUE;
42751cf3 883}
30b30c21 884
aee6f5b4
AO
885/* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887bfd_boolean
d00dd7dc
AM
888_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
aee6f5b4 891{
74541ad4 892 struct elf_link_hash_table *htab;
ca55926c 893 asection *ip;
74541ad4 894
aee6f5b4
AO
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
74541ad4 902 htab = elf_hash_table (info);
74541ad4
AM
903 if (htab->text_index_section != NULL)
904 return p != htab->text_index_section && p != htab->data_index_section;
905
ca55926c 906 return (htab->dynobj != NULL
3d4d4302 907 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 908 && ip->output_section == p);
aee6f5b4
AO
909
910 /* There shouldn't be section relative relocations
911 against any other section. */
912 default:
913 return TRUE;
914 }
915}
916
d00dd7dc
AM
917bfd_boolean
918_bfd_elf_omit_section_dynsym_all
919 (bfd *output_bfd ATTRIBUTE_UNUSED,
920 struct bfd_link_info *info ATTRIBUTE_UNUSED,
921 asection *p ATTRIBUTE_UNUSED)
922{
923 return TRUE;
924}
925
062e2358 926/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
927 symbol for each output section, which come first. Next come symbols
928 which have been forced to local binding. Then all of the back-end
929 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
930 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
931 (This prevents the early call before elf_backend_init_index_section
932 and strip_excluded_output_sections setting dynindx for sections
933 that are stripped.) */
30b30c21 934
554220db
AM
935static unsigned long
936_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
937 struct bfd_link_info *info,
938 unsigned long *section_sym_count)
30b30c21
RH
939{
940 unsigned long dynsymcount = 0;
63f452a8 941 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 942
0e1862bb
L
943 if (bfd_link_pic (info)
944 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 945 {
aee6f5b4 946 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
947 asection *p;
948 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 949 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 950 && (p->flags & SEC_ALLOC) != 0
7f923b7f 951 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 952 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
953 {
954 ++dynsymcount;
955 if (do_sec)
956 elf_section_data (p)->dynindx = dynsymcount;
957 }
958 else if (do_sec)
74541ad4 959 elf_section_data (p)->dynindx = 0;
30b30c21 960 }
63f452a8
AM
961 if (do_sec)
962 *section_sym_count = dynsymcount;
30b30c21 963
6fa3860b
PB
964 elf_link_hash_traverse (elf_hash_table (info),
965 elf_link_renumber_local_hash_table_dynsyms,
966 &dynsymcount);
967
30b30c21
RH
968 if (elf_hash_table (info)->dynlocal)
969 {
970 struct elf_link_local_dynamic_entry *p;
971 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
972 p->dynindx = ++dynsymcount;
973 }
90ac2420 974 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
975
976 elf_link_hash_traverse (elf_hash_table (info),
977 elf_link_renumber_hash_table_dynsyms,
978 &dynsymcount);
979
d5486c43
L
980 /* There is an unused NULL entry at the head of the table which we
981 must account for in our count even if the table is empty since it
982 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
983 .dynamic section. */
984 dynsymcount++;
30b30c21 985
ccabcbe5
AM
986 elf_hash_table (info)->dynsymcount = dynsymcount;
987 return dynsymcount;
30b30c21 988}
252b5132 989
54ac0771
L
990/* Merge st_other field. */
991
992static void
993elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 994 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 995 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
996{
997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
998
999 /* If st_other has a processor-specific meaning, specific
cd3416da 1000 code might be needed here. */
54ac0771
L
1001 if (bed->elf_backend_merge_symbol_attribute)
1002 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1003 dynamic);
1004
cd3416da 1005 if (!dynamic)
54ac0771 1006 {
cd3416da
AM
1007 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1008 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1009
cd3416da
AM
1010 /* Keep the most constraining visibility. Leave the remainder
1011 of the st_other field to elf_backend_merge_symbol_attribute. */
1012 if (symvis - 1 < hvis - 1)
1013 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1014 }
b8417128
AM
1015 else if (definition
1016 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1017 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1018 h->protected_def = 1;
54ac0771
L
1019}
1020
4f3fedcf
AM
1021/* This function is called when we want to merge a new symbol with an
1022 existing symbol. It handles the various cases which arise when we
1023 find a definition in a dynamic object, or when there is already a
1024 definition in a dynamic object. The new symbol is described by
1025 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1026 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1027 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1028 of an old common symbol. We set OVERRIDE if the old symbol is
1029 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1030 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1031 to change. By OK to change, we mean that we shouldn't warn if the
1032 type or size does change. */
45d6a902 1033
8a56bd02 1034static bfd_boolean
268b6b39
AM
1035_bfd_elf_merge_symbol (bfd *abfd,
1036 struct bfd_link_info *info,
1037 const char *name,
1038 Elf_Internal_Sym *sym,
1039 asection **psec,
1040 bfd_vma *pvalue,
4f3fedcf
AM
1041 struct elf_link_hash_entry **sym_hash,
1042 bfd **poldbfd,
37a9e49a 1043 bfd_boolean *pold_weak,
af44c138 1044 unsigned int *pold_alignment,
268b6b39
AM
1045 bfd_boolean *skip,
1046 bfd_boolean *override,
1047 bfd_boolean *type_change_ok,
6e33951e
L
1048 bfd_boolean *size_change_ok,
1049 bfd_boolean *matched)
252b5132 1050{
7479dfd4 1051 asection *sec, *oldsec;
45d6a902 1052 struct elf_link_hash_entry *h;
90c984fc 1053 struct elf_link_hash_entry *hi;
45d6a902
AM
1054 struct elf_link_hash_entry *flip;
1055 int bind;
1056 bfd *oldbfd;
1057 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1058 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1059 const struct elf_backend_data *bed;
6e33951e 1060 char *new_version;
93f4de39 1061 bfd_boolean default_sym = *matched;
45d6a902
AM
1062
1063 *skip = FALSE;
1064 *override = FALSE;
1065
1066 sec = *psec;
1067 bind = ELF_ST_BIND (sym->st_info);
1068
1069 if (! bfd_is_und_section (sec))
1070 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1071 else
1072 h = ((struct elf_link_hash_entry *)
1073 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1074 if (h == NULL)
1075 return FALSE;
1076 *sym_hash = h;
252b5132 1077
88ba32a0
L
1078 bed = get_elf_backend_data (abfd);
1079
6e33951e 1080 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1081 if (h->versioned != unversioned)
6e33951e 1082 {
422f1182
L
1083 /* Symbol version is unknown or versioned. */
1084 new_version = strrchr (name, ELF_VER_CHR);
1085 if (new_version)
1086 {
1087 if (h->versioned == unknown)
1088 {
1089 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1090 h->versioned = versioned_hidden;
1091 else
1092 h->versioned = versioned;
1093 }
1094 new_version += 1;
1095 if (new_version[0] == '\0')
1096 new_version = NULL;
1097 }
1098 else
1099 h->versioned = unversioned;
6e33951e 1100 }
422f1182
L
1101 else
1102 new_version = NULL;
6e33951e 1103
90c984fc
L
1104 /* For merging, we only care about real symbols. But we need to make
1105 sure that indirect symbol dynamic flags are updated. */
1106 hi = h;
45d6a902
AM
1107 while (h->root.type == bfd_link_hash_indirect
1108 || h->root.type == bfd_link_hash_warning)
1109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1110
6e33951e
L
1111 if (!*matched)
1112 {
1113 if (hi == h || h->root.type == bfd_link_hash_new)
1114 *matched = TRUE;
1115 else
1116 {
ae7683d2 1117 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1118 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1119 true if the new symbol is only visible to the symbol with
6e33951e 1120 the same symbol version. */
422f1182
L
1121 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1122 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1123 if (!old_hidden && !new_hidden)
1124 /* The new symbol matches the existing symbol if both
1125 aren't hidden. */
1126 *matched = TRUE;
1127 else
1128 {
1129 /* OLD_VERSION is the symbol version of the existing
1130 symbol. */
422f1182
L
1131 char *old_version;
1132
1133 if (h->versioned >= versioned)
1134 old_version = strrchr (h->root.root.string,
1135 ELF_VER_CHR) + 1;
1136 else
1137 old_version = NULL;
6e33951e
L
1138
1139 /* The new symbol matches the existing symbol if they
1140 have the same symbol version. */
1141 *matched = (old_version == new_version
1142 || (old_version != NULL
1143 && new_version != NULL
1144 && strcmp (old_version, new_version) == 0));
1145 }
1146 }
1147 }
1148
934bce08
AM
1149 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1150 existing symbol. */
1151
1152 oldbfd = NULL;
1153 oldsec = NULL;
1154 switch (h->root.type)
1155 {
1156 default:
1157 break;
1158
1159 case bfd_link_hash_undefined:
1160 case bfd_link_hash_undefweak:
1161 oldbfd = h->root.u.undef.abfd;
1162 break;
1163
1164 case bfd_link_hash_defined:
1165 case bfd_link_hash_defweak:
1166 oldbfd = h->root.u.def.section->owner;
1167 oldsec = h->root.u.def.section;
1168 break;
1169
1170 case bfd_link_hash_common:
1171 oldbfd = h->root.u.c.p->section->owner;
1172 oldsec = h->root.u.c.p->section;
1173 if (pold_alignment)
1174 *pold_alignment = h->root.u.c.p->alignment_power;
1175 break;
1176 }
1177 if (poldbfd && *poldbfd == NULL)
1178 *poldbfd = oldbfd;
1179
1180 /* Differentiate strong and weak symbols. */
1181 newweak = bind == STB_WEAK;
1182 oldweak = (h->root.type == bfd_link_hash_defweak
1183 || h->root.type == bfd_link_hash_undefweak);
1184 if (pold_weak)
1185 *pold_weak = oldweak;
1186
40b36307 1187 /* We have to check it for every instance since the first few may be
ee659f1f 1188 references and not all compilers emit symbol type for undefined
40b36307
L
1189 symbols. */
1190 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1191
ee659f1f
AM
1192 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1193 respectively, is from a dynamic object. */
1194
1195 newdyn = (abfd->flags & DYNAMIC) != 0;
1196
1197 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1198 syms and defined syms in dynamic libraries respectively.
1199 ref_dynamic on the other hand can be set for a symbol defined in
1200 a dynamic library, and def_dynamic may not be set; When the
1201 definition in a dynamic lib is overridden by a definition in the
1202 executable use of the symbol in the dynamic lib becomes a
1203 reference to the executable symbol. */
1204 if (newdyn)
1205 {
1206 if (bfd_is_und_section (sec))
1207 {
1208 if (bind != STB_WEAK)
1209 {
1210 h->ref_dynamic_nonweak = 1;
1211 hi->ref_dynamic_nonweak = 1;
1212 }
1213 }
1214 else
1215 {
6e33951e
L
1216 /* Update the existing symbol only if they match. */
1217 if (*matched)
1218 h->dynamic_def = 1;
ee659f1f
AM
1219 hi->dynamic_def = 1;
1220 }
1221 }
1222
45d6a902
AM
1223 /* If we just created the symbol, mark it as being an ELF symbol.
1224 Other than that, there is nothing to do--there is no merge issue
1225 with a newly defined symbol--so we just return. */
1226
1227 if (h->root.type == bfd_link_hash_new)
252b5132 1228 {
f5385ebf 1229 h->non_elf = 0;
45d6a902
AM
1230 return TRUE;
1231 }
252b5132 1232
45d6a902
AM
1233 /* In cases involving weak versioned symbols, we may wind up trying
1234 to merge a symbol with itself. Catch that here, to avoid the
1235 confusion that results if we try to override a symbol with
1236 itself. The additional tests catch cases like
1237 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1238 dynamic object, which we do want to handle here. */
1239 if (abfd == oldbfd
895fa45f 1240 && (newweak || oldweak)
45d6a902 1241 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1242 || !h->def_regular))
45d6a902
AM
1243 return TRUE;
1244
707bba77 1245 olddyn = FALSE;
45d6a902
AM
1246 if (oldbfd != NULL)
1247 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1248 else if (oldsec != NULL)
45d6a902 1249 {
707bba77 1250 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1251 indices used by MIPS ELF. */
707bba77 1252 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1253 }
252b5132 1254
1a3b5c34
AM
1255 /* Handle a case where plugin_notice won't be called and thus won't
1256 set the non_ir_ref flags on the first pass over symbols. */
1257 if (oldbfd != NULL
1258 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1259 && newdyn != olddyn)
1260 {
1261 h->root.non_ir_ref_dynamic = TRUE;
1262 hi->root.non_ir_ref_dynamic = TRUE;
1263 }
1264
45d6a902
AM
1265 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1266 respectively, appear to be a definition rather than reference. */
1267
707bba77 1268 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1269
707bba77
AM
1270 olddef = (h->root.type != bfd_link_hash_undefined
1271 && h->root.type != bfd_link_hash_undefweak
202ac193 1272 && h->root.type != bfd_link_hash_common);
45d6a902 1273
0a36a439
L
1274 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1275 respectively, appear to be a function. */
1276
1277 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1278 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1279
1280 oldfunc = (h->type != STT_NOTYPE
1281 && bed->is_function_type (h->type));
1282
c5d37467 1283 if (!(newfunc && oldfunc)
5b677558
AM
1284 && ELF_ST_TYPE (sym->st_info) != h->type
1285 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1286 && h->type != STT_NOTYPE
c5d37467
AM
1287 && (newdef || bfd_is_com_section (sec))
1288 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1289 {
c5d37467
AM
1290 /* If creating a default indirect symbol ("foo" or "foo@") from
1291 a dynamic versioned definition ("foo@@") skip doing so if
1292 there is an existing regular definition with a different
1293 type. We don't want, for example, a "time" variable in the
1294 executable overriding a "time" function in a shared library. */
1295 if (newdyn
1296 && !olddyn)
1297 {
1298 *skip = TRUE;
1299 return TRUE;
1300 }
1301
1302 /* When adding a symbol from a regular object file after we have
1303 created indirect symbols, undo the indirection and any
1304 dynamic state. */
1305 if (hi != h
1306 && !newdyn
1307 && olddyn)
1308 {
1309 h = hi;
1310 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1311 h->forced_local = 0;
1312 h->ref_dynamic = 0;
1313 h->def_dynamic = 0;
1314 h->dynamic_def = 0;
1315 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1316 {
1317 h->root.type = bfd_link_hash_undefined;
1318 h->root.u.undef.abfd = abfd;
1319 }
1320 else
1321 {
1322 h->root.type = bfd_link_hash_new;
1323 h->root.u.undef.abfd = NULL;
1324 }
1325 return TRUE;
1326 }
580a2b6e
L
1327 }
1328
4c34aff8
AM
1329 /* Check TLS symbols. We don't check undefined symbols introduced
1330 by "ld -u" which have no type (and oldbfd NULL), and we don't
1331 check symbols from plugins because they also have no type. */
1332 if (oldbfd != NULL
1333 && (oldbfd->flags & BFD_PLUGIN) == 0
1334 && (abfd->flags & BFD_PLUGIN) == 0
1335 && ELF_ST_TYPE (sym->st_info) != h->type
1336 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1337 {
1338 bfd *ntbfd, *tbfd;
1339 bfd_boolean ntdef, tdef;
1340 asection *ntsec, *tsec;
1341
1342 if (h->type == STT_TLS)
1343 {
3b36f7e6 1344 ntbfd = abfd;
7479dfd4
L
1345 ntsec = sec;
1346 ntdef = newdef;
1347 tbfd = oldbfd;
1348 tsec = oldsec;
1349 tdef = olddef;
1350 }
1351 else
1352 {
1353 ntbfd = oldbfd;
1354 ntsec = oldsec;
1355 ntdef = olddef;
1356 tbfd = abfd;
1357 tsec = sec;
1358 tdef = newdef;
1359 }
1360
1361 if (tdef && ntdef)
4eca0228 1362 _bfd_error_handler
695344c0 1363 /* xgettext:c-format */
871b3ab2
AM
1364 (_("%s: TLS definition in %pB section %pA "
1365 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1366 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1367 else if (!tdef && !ntdef)
4eca0228 1368 _bfd_error_handler
695344c0 1369 /* xgettext:c-format */
871b3ab2
AM
1370 (_("%s: TLS reference in %pB "
1371 "mismatches non-TLS reference in %pB"),
c08bb8dd 1372 h->root.root.string, tbfd, ntbfd);
7479dfd4 1373 else if (tdef)
4eca0228 1374 _bfd_error_handler
695344c0 1375 /* xgettext:c-format */
871b3ab2
AM
1376 (_("%s: TLS definition in %pB section %pA "
1377 "mismatches non-TLS reference in %pB"),
c08bb8dd 1378 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1379 else
4eca0228 1380 _bfd_error_handler
695344c0 1381 /* xgettext:c-format */
871b3ab2
AM
1382 (_("%s: TLS reference in %pB "
1383 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1384 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1385
1386 bfd_set_error (bfd_error_bad_value);
1387 return FALSE;
1388 }
1389
45d6a902
AM
1390 /* If the old symbol has non-default visibility, we ignore the new
1391 definition from a dynamic object. */
1392 if (newdyn
9c7a29a3 1393 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1394 && !bfd_is_und_section (sec))
1395 {
1396 *skip = TRUE;
1397 /* Make sure this symbol is dynamic. */
f5385ebf 1398 h->ref_dynamic = 1;
90c984fc 1399 hi->ref_dynamic = 1;
45d6a902
AM
1400 /* A protected symbol has external availability. Make sure it is
1401 recorded as dynamic.
1402
1403 FIXME: Should we check type and size for protected symbol? */
1404 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1405 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1406 else
1407 return TRUE;
1408 }
1409 else if (!newdyn
9c7a29a3 1410 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1411 && h->def_dynamic)
45d6a902
AM
1412 {
1413 /* If the new symbol with non-default visibility comes from a
1414 relocatable file and the old definition comes from a dynamic
1415 object, we remove the old definition. */
6c9b78e6 1416 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1417 {
1418 /* Handle the case where the old dynamic definition is
1419 default versioned. We need to copy the symbol info from
1420 the symbol with default version to the normal one if it
1421 was referenced before. */
1422 if (h->ref_regular)
1423 {
6c9b78e6 1424 hi->root.type = h->root.type;
d2dee3b2 1425 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1426 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1427
6c9b78e6 1428 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1429 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1430 {
aed81c4e
MR
1431 /* If the new symbol is hidden or internal, completely undo
1432 any dynamic link state. */
1433 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1434 h->forced_local = 0;
1435 h->ref_dynamic = 0;
d2dee3b2
L
1436 }
1437 else
aed81c4e
MR
1438 h->ref_dynamic = 1;
1439
1440 h->def_dynamic = 0;
aed81c4e
MR
1441 /* FIXME: Should we check type and size for protected symbol? */
1442 h->size = 0;
1443 h->type = 0;
1444
6c9b78e6 1445 h = hi;
d2dee3b2
L
1446 }
1447 else
6c9b78e6 1448 h = hi;
d2dee3b2 1449 }
1de1a317 1450
f5eda473
AM
1451 /* If the old symbol was undefined before, then it will still be
1452 on the undefs list. If the new symbol is undefined or
1453 common, we can't make it bfd_link_hash_new here, because new
1454 undefined or common symbols will be added to the undefs list
1455 by _bfd_generic_link_add_one_symbol. Symbols may not be
1456 added twice to the undefs list. Also, if the new symbol is
1457 undefweak then we don't want to lose the strong undef. */
1458 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1459 {
1de1a317 1460 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1461 h->root.u.undef.abfd = abfd;
1462 }
1463 else
1464 {
1465 h->root.type = bfd_link_hash_new;
1466 h->root.u.undef.abfd = NULL;
1467 }
1468
f5eda473 1469 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1470 {
f5eda473
AM
1471 /* If the new symbol is hidden or internal, completely undo
1472 any dynamic link state. */
1473 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1474 h->forced_local = 0;
1475 h->ref_dynamic = 0;
45d6a902 1476 }
f5eda473
AM
1477 else
1478 h->ref_dynamic = 1;
1479 h->def_dynamic = 0;
45d6a902
AM
1480 /* FIXME: Should we check type and size for protected symbol? */
1481 h->size = 0;
1482 h->type = 0;
1483 return TRUE;
1484 }
14a793b2 1485
15b43f48
AM
1486 /* If a new weak symbol definition comes from a regular file and the
1487 old symbol comes from a dynamic library, we treat the new one as
1488 strong. Similarly, an old weak symbol definition from a regular
1489 file is treated as strong when the new symbol comes from a dynamic
1490 library. Further, an old weak symbol from a dynamic library is
1491 treated as strong if the new symbol is from a dynamic library.
1492 This reflects the way glibc's ld.so works.
1493
165f707a
AM
1494 Also allow a weak symbol to override a linker script symbol
1495 defined by an early pass over the script. This is done so the
1496 linker knows the symbol is defined in an object file, for the
1497 DEFINED script function.
1498
15b43f48
AM
1499 Do this before setting *type_change_ok or *size_change_ok so that
1500 we warn properly when dynamic library symbols are overridden. */
1501
165f707a 1502 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1503 newweak = FALSE;
15b43f48 1504 if (olddef && newdyn)
0f8a2703
AM
1505 oldweak = FALSE;
1506
d334575b 1507 /* Allow changes between different types of function symbol. */
0a36a439 1508 if (newfunc && oldfunc)
fcb93ecf
PB
1509 *type_change_ok = TRUE;
1510
79349b09
AM
1511 /* It's OK to change the type if either the existing symbol or the
1512 new symbol is weak. A type change is also OK if the old symbol
1513 is undefined and the new symbol is defined. */
252b5132 1514
79349b09
AM
1515 if (oldweak
1516 || newweak
1517 || (newdef
1518 && h->root.type == bfd_link_hash_undefined))
1519 *type_change_ok = TRUE;
1520
1521 /* It's OK to change the size if either the existing symbol or the
1522 new symbol is weak, or if the old symbol is undefined. */
1523
1524 if (*type_change_ok
1525 || h->root.type == bfd_link_hash_undefined)
1526 *size_change_ok = TRUE;
45d6a902 1527
45d6a902
AM
1528 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1529 symbol, respectively, appears to be a common symbol in a dynamic
1530 object. If a symbol appears in an uninitialized section, and is
1531 not weak, and is not a function, then it may be a common symbol
1532 which was resolved when the dynamic object was created. We want
1533 to treat such symbols specially, because they raise special
1534 considerations when setting the symbol size: if the symbol
1535 appears as a common symbol in a regular object, and the size in
1536 the regular object is larger, we must make sure that we use the
1537 larger size. This problematic case can always be avoided in C,
1538 but it must be handled correctly when using Fortran shared
1539 libraries.
1540
1541 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1542 likewise for OLDDYNCOMMON and OLDDEF.
1543
1544 Note that this test is just a heuristic, and that it is quite
1545 possible to have an uninitialized symbol in a shared object which
1546 is really a definition, rather than a common symbol. This could
1547 lead to some minor confusion when the symbol really is a common
1548 symbol in some regular object. However, I think it will be
1549 harmless. */
1550
1551 if (newdyn
1552 && newdef
79349b09 1553 && !newweak
45d6a902
AM
1554 && (sec->flags & SEC_ALLOC) != 0
1555 && (sec->flags & SEC_LOAD) == 0
1556 && sym->st_size > 0
0a36a439 1557 && !newfunc)
45d6a902
AM
1558 newdyncommon = TRUE;
1559 else
1560 newdyncommon = FALSE;
1561
1562 if (olddyn
1563 && olddef
1564 && h->root.type == bfd_link_hash_defined
f5385ebf 1565 && h->def_dynamic
45d6a902
AM
1566 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1567 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1568 && h->size > 0
0a36a439 1569 && !oldfunc)
45d6a902
AM
1570 olddyncommon = TRUE;
1571 else
1572 olddyncommon = FALSE;
1573
a4d8e49b
L
1574 /* We now know everything about the old and new symbols. We ask the
1575 backend to check if we can merge them. */
5d13b3b3
AM
1576 if (bed->merge_symbol != NULL)
1577 {
1578 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1579 return FALSE;
1580 sec = *psec;
1581 }
a4d8e49b 1582
a83ef4d1
L
1583 /* There are multiple definitions of a normal symbol. Skip the
1584 default symbol as well as definition from an IR object. */
93f4de39 1585 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1586 && !default_sym && h->def_regular
1587 && !(oldbfd != NULL
1588 && (oldbfd->flags & BFD_PLUGIN) != 0
1589 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1590 {
1591 /* Handle a multiple definition. */
1592 (*info->callbacks->multiple_definition) (info, &h->root,
1593 abfd, sec, *pvalue);
1594 *skip = TRUE;
1595 return TRUE;
1596 }
1597
45d6a902
AM
1598 /* If both the old and the new symbols look like common symbols in a
1599 dynamic object, set the size of the symbol to the larger of the
1600 two. */
1601
1602 if (olddyncommon
1603 && newdyncommon
1604 && sym->st_size != h->size)
1605 {
1606 /* Since we think we have two common symbols, issue a multiple
1607 common warning if desired. Note that we only warn if the
1608 size is different. If the size is the same, we simply let
1609 the old symbol override the new one as normally happens with
1610 symbols defined in dynamic objects. */
1611
1a72702b
AM
1612 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1613 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1614 if (sym->st_size > h->size)
1615 h->size = sym->st_size;
252b5132 1616
45d6a902 1617 *size_change_ok = TRUE;
252b5132
RH
1618 }
1619
45d6a902
AM
1620 /* If we are looking at a dynamic object, and we have found a
1621 definition, we need to see if the symbol was already defined by
1622 some other object. If so, we want to use the existing
1623 definition, and we do not want to report a multiple symbol
1624 definition error; we do this by clobbering *PSEC to be
1625 bfd_und_section_ptr.
1626
1627 We treat a common symbol as a definition if the symbol in the
1628 shared library is a function, since common symbols always
1629 represent variables; this can cause confusion in principle, but
1630 any such confusion would seem to indicate an erroneous program or
1631 shared library. We also permit a common symbol in a regular
8170f769 1632 object to override a weak symbol in a shared object. */
45d6a902
AM
1633
1634 if (newdyn
1635 && newdef
77cfaee6 1636 && (olddef
45d6a902 1637 || (h->root.type == bfd_link_hash_common
8170f769 1638 && (newweak || newfunc))))
45d6a902
AM
1639 {
1640 *override = TRUE;
1641 newdef = FALSE;
1642 newdyncommon = FALSE;
252b5132 1643
45d6a902
AM
1644 *psec = sec = bfd_und_section_ptr;
1645 *size_change_ok = TRUE;
252b5132 1646
45d6a902
AM
1647 /* If we get here when the old symbol is a common symbol, then
1648 we are explicitly letting it override a weak symbol or
1649 function in a dynamic object, and we don't want to warn about
1650 a type change. If the old symbol is a defined symbol, a type
1651 change warning may still be appropriate. */
252b5132 1652
45d6a902
AM
1653 if (h->root.type == bfd_link_hash_common)
1654 *type_change_ok = TRUE;
1655 }
1656
1657 /* Handle the special case of an old common symbol merging with a
1658 new symbol which looks like a common symbol in a shared object.
1659 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1660 common symbol, and let _bfd_generic_link_add_one_symbol do the
1661 right thing. */
45d6a902
AM
1662
1663 if (newdyncommon
1664 && h->root.type == bfd_link_hash_common)
1665 {
1666 *override = TRUE;
1667 newdef = FALSE;
1668 newdyncommon = FALSE;
1669 *pvalue = sym->st_size;
a4d8e49b 1670 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1671 *size_change_ok = TRUE;
1672 }
1673
c5e2cead 1674 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1675 if (newdef && olddef && newweak)
54ac0771 1676 {
35ed3f94 1677 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1678 if (!(oldbfd != NULL
1679 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1680 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1681 {
1682 newdef = FALSE;
1683 *skip = TRUE;
1684 }
54ac0771
L
1685
1686 /* Merge st_other. If the symbol already has a dynamic index,
1687 but visibility says it should not be visible, turn it into a
1688 local symbol. */
b8417128 1689 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1690 if (h->dynindx != -1)
1691 switch (ELF_ST_VISIBILITY (h->other))
1692 {
1693 case STV_INTERNAL:
1694 case STV_HIDDEN:
1695 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1696 break;
1697 }
1698 }
c5e2cead 1699
45d6a902
AM
1700 /* If the old symbol is from a dynamic object, and the new symbol is
1701 a definition which is not from a dynamic object, then the new
1702 symbol overrides the old symbol. Symbols from regular files
1703 always take precedence over symbols from dynamic objects, even if
1704 they are defined after the dynamic object in the link.
1705
1706 As above, we again permit a common symbol in a regular object to
1707 override a definition in a shared object if the shared object
0f8a2703 1708 symbol is a function or is weak. */
45d6a902
AM
1709
1710 flip = NULL;
77cfaee6 1711 if (!newdyn
45d6a902
AM
1712 && (newdef
1713 || (bfd_is_com_section (sec)
0a36a439 1714 && (oldweak || oldfunc)))
45d6a902
AM
1715 && olddyn
1716 && olddef
f5385ebf 1717 && h->def_dynamic)
45d6a902
AM
1718 {
1719 /* Change the hash table entry to undefined, and let
1720 _bfd_generic_link_add_one_symbol do the right thing with the
1721 new definition. */
1722
1723 h->root.type = bfd_link_hash_undefined;
1724 h->root.u.undef.abfd = h->root.u.def.section->owner;
1725 *size_change_ok = TRUE;
1726
1727 olddef = FALSE;
1728 olddyncommon = FALSE;
1729
1730 /* We again permit a type change when a common symbol may be
1731 overriding a function. */
1732
1733 if (bfd_is_com_section (sec))
0a36a439
L
1734 {
1735 if (oldfunc)
1736 {
1737 /* If a common symbol overrides a function, make sure
1738 that it isn't defined dynamically nor has type
1739 function. */
1740 h->def_dynamic = 0;
1741 h->type = STT_NOTYPE;
1742 }
1743 *type_change_ok = TRUE;
1744 }
45d6a902 1745
6c9b78e6
AM
1746 if (hi->root.type == bfd_link_hash_indirect)
1747 flip = hi;
45d6a902
AM
1748 else
1749 /* This union may have been set to be non-NULL when this symbol
1750 was seen in a dynamic object. We must force the union to be
1751 NULL, so that it is correct for a regular symbol. */
1752 h->verinfo.vertree = NULL;
1753 }
1754
1755 /* Handle the special case of a new common symbol merging with an
1756 old symbol that looks like it might be a common symbol defined in
1757 a shared object. Note that we have already handled the case in
1758 which a new common symbol should simply override the definition
1759 in the shared library. */
1760
1761 if (! newdyn
1762 && bfd_is_com_section (sec)
1763 && olddyncommon)
1764 {
1765 /* It would be best if we could set the hash table entry to a
1766 common symbol, but we don't know what to use for the section
1767 or the alignment. */
1a72702b
AM
1768 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1769 bfd_link_hash_common, sym->st_size);
45d6a902 1770
4cc11e76 1771 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1772 larger, pretend that the new symbol has its size. */
1773
1774 if (h->size > *pvalue)
1775 *pvalue = h->size;
1776
af44c138
L
1777 /* We need to remember the alignment required by the symbol
1778 in the dynamic object. */
1779 BFD_ASSERT (pold_alignment);
1780 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1781
1782 olddef = FALSE;
1783 olddyncommon = FALSE;
1784
1785 h->root.type = bfd_link_hash_undefined;
1786 h->root.u.undef.abfd = h->root.u.def.section->owner;
1787
1788 *size_change_ok = TRUE;
1789 *type_change_ok = TRUE;
1790
6c9b78e6
AM
1791 if (hi->root.type == bfd_link_hash_indirect)
1792 flip = hi;
45d6a902
AM
1793 else
1794 h->verinfo.vertree = NULL;
1795 }
1796
1797 if (flip != NULL)
1798 {
1799 /* Handle the case where we had a versioned symbol in a dynamic
1800 library and now find a definition in a normal object. In this
1801 case, we make the versioned symbol point to the normal one. */
45d6a902 1802 flip->root.type = h->root.type;
00cbee0a 1803 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1804 h->root.type = bfd_link_hash_indirect;
1805 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1806 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1807 if (h->def_dynamic)
45d6a902 1808 {
f5385ebf
AM
1809 h->def_dynamic = 0;
1810 flip->ref_dynamic = 1;
45d6a902
AM
1811 }
1812 }
1813
45d6a902
AM
1814 return TRUE;
1815}
1816
1817/* This function is called to create an indirect symbol from the
1818 default for the symbol with the default version if needed. The
4f3fedcf 1819 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1820 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1821
28caa186 1822static bfd_boolean
268b6b39
AM
1823_bfd_elf_add_default_symbol (bfd *abfd,
1824 struct bfd_link_info *info,
1825 struct elf_link_hash_entry *h,
1826 const char *name,
1827 Elf_Internal_Sym *sym,
4f3fedcf
AM
1828 asection *sec,
1829 bfd_vma value,
1830 bfd **poldbfd,
e3c9d234 1831 bfd_boolean *dynsym)
45d6a902
AM
1832{
1833 bfd_boolean type_change_ok;
1834 bfd_boolean size_change_ok;
1835 bfd_boolean skip;
1836 char *shortname;
1837 struct elf_link_hash_entry *hi;
1838 struct bfd_link_hash_entry *bh;
9c5bfbb7 1839 const struct elf_backend_data *bed;
45d6a902
AM
1840 bfd_boolean collect;
1841 bfd_boolean dynamic;
e3c9d234 1842 bfd_boolean override;
45d6a902
AM
1843 char *p;
1844 size_t len, shortlen;
ffd65175 1845 asection *tmp_sec;
6e33951e 1846 bfd_boolean matched;
45d6a902 1847
422f1182
L
1848 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1849 return TRUE;
1850
45d6a902
AM
1851 /* If this symbol has a version, and it is the default version, we
1852 create an indirect symbol from the default name to the fully
1853 decorated name. This will cause external references which do not
1854 specify a version to be bound to this version of the symbol. */
1855 p = strchr (name, ELF_VER_CHR);
422f1182
L
1856 if (h->versioned == unknown)
1857 {
1858 if (p == NULL)
1859 {
1860 h->versioned = unversioned;
1861 return TRUE;
1862 }
1863 else
1864 {
1865 if (p[1] != ELF_VER_CHR)
1866 {
1867 h->versioned = versioned_hidden;
1868 return TRUE;
1869 }
1870 else
1871 h->versioned = versioned;
1872 }
1873 }
4373f8af
L
1874 else
1875 {
1876 /* PR ld/19073: We may see an unversioned definition after the
1877 default version. */
1878 if (p == NULL)
1879 return TRUE;
1880 }
45d6a902 1881
45d6a902
AM
1882 bed = get_elf_backend_data (abfd);
1883 collect = bed->collect;
1884 dynamic = (abfd->flags & DYNAMIC) != 0;
1885
1886 shortlen = p - name;
a50b1753 1887 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1888 if (shortname == NULL)
1889 return FALSE;
1890 memcpy (shortname, name, shortlen);
1891 shortname[shortlen] = '\0';
1892
1893 /* We are going to create a new symbol. Merge it with any existing
1894 symbol with this name. For the purposes of the merge, act as
1895 though we were defining the symbol we just defined, although we
1896 actually going to define an indirect symbol. */
1897 type_change_ok = FALSE;
1898 size_change_ok = FALSE;
6e33951e 1899 matched = TRUE;
ffd65175
AM
1900 tmp_sec = sec;
1901 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1902 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1903 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1904 return FALSE;
1905
1906 if (skip)
1907 goto nondefault;
1908
5b677558
AM
1909 if (hi->def_regular)
1910 {
1911 /* If the undecorated symbol will have a version added by a
1912 script different to H, then don't indirect to/from the
1913 undecorated symbol. This isn't ideal because we may not yet
1914 have seen symbol versions, if given by a script on the
1915 command line rather than via --version-script. */
1916 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1917 {
1918 bfd_boolean hide;
1919
1920 hi->verinfo.vertree
1921 = bfd_find_version_for_sym (info->version_info,
1922 hi->root.root.string, &hide);
1923 if (hi->verinfo.vertree != NULL && hide)
1924 {
1925 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1926 goto nondefault;
1927 }
1928 }
1929 if (hi->verinfo.vertree != NULL
1930 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1931 goto nondefault;
1932 }
1933
45d6a902
AM
1934 if (! override)
1935 {
c6e8a9a8 1936 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1937 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1938 {
1939 bh = &hi->root;
fbcc8baf 1940 if (bh->type == bfd_link_hash_defined
6cc71b82 1941 && bh->u.def.section->owner != NULL
fbcc8baf
L
1942 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1943 {
1944 /* Mark the previous definition from IR object as
1945 undefined so that the generic linker will override
1946 it. */
1947 bh->type = bfd_link_hash_undefined;
1948 bh->u.undef.abfd = bh->u.def.section->owner;
1949 }
c6e8a9a8
L
1950 if (! (_bfd_generic_link_add_one_symbol
1951 (info, abfd, shortname, BSF_INDIRECT,
1952 bfd_ind_section_ptr,
1953 0, name, FALSE, collect, &bh)))
1954 return FALSE;
1955 hi = (struct elf_link_hash_entry *) bh;
1956 }
45d6a902
AM
1957 }
1958 else
1959 {
1960 /* In this case the symbol named SHORTNAME is overriding the
1961 indirect symbol we want to add. We were planning on making
1962 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1963 is the name without a version. NAME is the fully versioned
1964 name, and it is the default version.
1965
1966 Overriding means that we already saw a definition for the
1967 symbol SHORTNAME in a regular object, and it is overriding
1968 the symbol defined in the dynamic object.
1969
1970 When this happens, we actually want to change NAME, the
1971 symbol we just added, to refer to SHORTNAME. This will cause
1972 references to NAME in the shared object to become references
1973 to SHORTNAME in the regular object. This is what we expect
1974 when we override a function in a shared object: that the
1975 references in the shared object will be mapped to the
1976 definition in the regular object. */
1977
1978 while (hi->root.type == bfd_link_hash_indirect
1979 || hi->root.type == bfd_link_hash_warning)
1980 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1981
1982 h->root.type = bfd_link_hash_indirect;
1983 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1984 if (h->def_dynamic)
45d6a902 1985 {
f5385ebf
AM
1986 h->def_dynamic = 0;
1987 hi->ref_dynamic = 1;
1988 if (hi->ref_regular
1989 || hi->def_regular)
45d6a902 1990 {
c152c796 1991 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1992 return FALSE;
1993 }
1994 }
1995
1996 /* Now set HI to H, so that the following code will set the
1997 other fields correctly. */
1998 hi = h;
1999 }
2000
fab4a87f
L
2001 /* Check if HI is a warning symbol. */
2002 if (hi->root.type == bfd_link_hash_warning)
2003 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2004
45d6a902
AM
2005 /* If there is a duplicate definition somewhere, then HI may not
2006 point to an indirect symbol. We will have reported an error to
2007 the user in that case. */
2008
2009 if (hi->root.type == bfd_link_hash_indirect)
2010 {
2011 struct elf_link_hash_entry *ht;
2012
45d6a902 2013 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2014 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2015
68c88cd4
AM
2016 /* A reference to the SHORTNAME symbol from a dynamic library
2017 will be satisfied by the versioned symbol at runtime. In
2018 effect, we have a reference to the versioned symbol. */
2019 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2020 hi->dynamic_def |= ht->dynamic_def;
2021
45d6a902
AM
2022 /* See if the new flags lead us to realize that the symbol must
2023 be dynamic. */
2024 if (! *dynsym)
2025 {
2026 if (! dynamic)
2027 {
0e1862bb 2028 if (! bfd_link_executable (info)
90c984fc 2029 || hi->def_dynamic
f5385ebf 2030 || hi->ref_dynamic)
45d6a902
AM
2031 *dynsym = TRUE;
2032 }
2033 else
2034 {
f5385ebf 2035 if (hi->ref_regular)
45d6a902
AM
2036 *dynsym = TRUE;
2037 }
2038 }
2039 }
2040
2041 /* We also need to define an indirection from the nondefault version
2042 of the symbol. */
2043
2044nondefault:
2045 len = strlen (name);
a50b1753 2046 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2047 if (shortname == NULL)
2048 return FALSE;
2049 memcpy (shortname, name, shortlen);
2050 memcpy (shortname + shortlen, p + 1, len - shortlen);
2051
2052 /* Once again, merge with any existing symbol. */
2053 type_change_ok = FALSE;
2054 size_change_ok = FALSE;
ffd65175
AM
2055 tmp_sec = sec;
2056 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2057 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2058 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2059 return FALSE;
2060
2061 if (skip)
2062 return TRUE;
2063
2064 if (override)
2065 {
2066 /* Here SHORTNAME is a versioned name, so we don't expect to see
2067 the type of override we do in the case above unless it is
4cc11e76 2068 overridden by a versioned definition. */
45d6a902
AM
2069 if (hi->root.type != bfd_link_hash_defined
2070 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2071 _bfd_error_handler
695344c0 2072 /* xgettext:c-format */
871b3ab2 2073 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2074 abfd, shortname);
45d6a902
AM
2075 }
2076 else
2077 {
2078 bh = &hi->root;
2079 if (! (_bfd_generic_link_add_one_symbol
2080 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2081 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2082 return FALSE;
2083 hi = (struct elf_link_hash_entry *) bh;
2084
2085 /* If there is a duplicate definition somewhere, then HI may not
2086 point to an indirect symbol. We will have reported an error
2087 to the user in that case. */
2088
2089 if (hi->root.type == bfd_link_hash_indirect)
2090 {
fcfa13d2 2091 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2092 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2093 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2094
2095 /* See if the new flags lead us to realize that the symbol
2096 must be dynamic. */
2097 if (! *dynsym)
2098 {
2099 if (! dynamic)
2100 {
0e1862bb 2101 if (! bfd_link_executable (info)
f5385ebf 2102 || hi->ref_dynamic)
45d6a902
AM
2103 *dynsym = TRUE;
2104 }
2105 else
2106 {
f5385ebf 2107 if (hi->ref_regular)
45d6a902
AM
2108 *dynsym = TRUE;
2109 }
2110 }
2111 }
2112 }
2113
2114 return TRUE;
2115}
2116\f
2117/* This routine is used to export all defined symbols into the dynamic
2118 symbol table. It is called via elf_link_hash_traverse. */
2119
28caa186 2120static bfd_boolean
268b6b39 2121_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2122{
a50b1753 2123 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2124
2125 /* Ignore indirect symbols. These are added by the versioning code. */
2126 if (h->root.type == bfd_link_hash_indirect)
2127 return TRUE;
2128
7686d77d
AM
2129 /* Ignore this if we won't export it. */
2130 if (!eif->info->export_dynamic && !h->dynamic)
2131 return TRUE;
45d6a902
AM
2132
2133 if (h->dynindx == -1
fd91d419
L
2134 && (h->def_regular || h->ref_regular)
2135 && ! bfd_hide_sym_by_version (eif->info->version_info,
2136 h->root.root.string))
45d6a902 2137 {
fd91d419 2138 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2139 {
fd91d419
L
2140 eif->failed = TRUE;
2141 return FALSE;
45d6a902
AM
2142 }
2143 }
2144
2145 return TRUE;
2146}
2147\f
2148/* Look through the symbols which are defined in other shared
2149 libraries and referenced here. Update the list of version
2150 dependencies. This will be put into the .gnu.version_r section.
2151 This function is called via elf_link_hash_traverse. */
2152
28caa186 2153static bfd_boolean
268b6b39
AM
2154_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2155 void *data)
45d6a902 2156{
a50b1753 2157 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2158 Elf_Internal_Verneed *t;
2159 Elf_Internal_Vernaux *a;
2160 bfd_size_type amt;
2161
45d6a902
AM
2162 /* We only care about symbols defined in shared objects with version
2163 information. */
f5385ebf
AM
2164 if (!h->def_dynamic
2165 || h->def_regular
45d6a902 2166 || h->dynindx == -1
7b20f099
AM
2167 || h->verinfo.verdef == NULL
2168 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2169 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2170 return TRUE;
2171
2172 /* See if we already know about this version. */
28caa186
AM
2173 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2174 t != NULL;
2175 t = t->vn_nextref)
45d6a902
AM
2176 {
2177 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2178 continue;
2179
2180 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2181 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2182 return TRUE;
2183
2184 break;
2185 }
2186
2187 /* This is a new version. Add it to tree we are building. */
2188
2189 if (t == NULL)
2190 {
2191 amt = sizeof *t;
a50b1753 2192 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2193 if (t == NULL)
2194 {
2195 rinfo->failed = TRUE;
2196 return FALSE;
2197 }
2198
2199 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2200 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2201 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2202 }
2203
2204 amt = sizeof *a;
a50b1753 2205 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2206 if (a == NULL)
2207 {
2208 rinfo->failed = TRUE;
2209 return FALSE;
2210 }
45d6a902
AM
2211
2212 /* Note that we are copying a string pointer here, and testing it
2213 above. If bfd_elf_string_from_elf_section is ever changed to
2214 discard the string data when low in memory, this will have to be
2215 fixed. */
2216 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2217
2218 a->vna_flags = h->verinfo.verdef->vd_flags;
2219 a->vna_nextptr = t->vn_auxptr;
2220
2221 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2222 ++rinfo->vers;
2223
2224 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2225
2226 t->vn_auxptr = a;
2227
2228 return TRUE;
2229}
2230
099bb8fb
L
2231/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2232 hidden. Set *T_P to NULL if there is no match. */
2233
2234static bfd_boolean
2235_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2236 struct elf_link_hash_entry *h,
2237 const char *version_p,
2238 struct bfd_elf_version_tree **t_p,
2239 bfd_boolean *hide)
2240{
2241 struct bfd_elf_version_tree *t;
2242
2243 /* Look for the version. If we find it, it is no longer weak. */
2244 for (t = info->version_info; t != NULL; t = t->next)
2245 {
2246 if (strcmp (t->name, version_p) == 0)
2247 {
2248 size_t len;
2249 char *alc;
2250 struct bfd_elf_version_expr *d;
2251
2252 len = version_p - h->root.root.string;
2253 alc = (char *) bfd_malloc (len);
2254 if (alc == NULL)
2255 return FALSE;
2256 memcpy (alc, h->root.root.string, len - 1);
2257 alc[len - 1] = '\0';
2258 if (alc[len - 2] == ELF_VER_CHR)
2259 alc[len - 2] = '\0';
2260
2261 h->verinfo.vertree = t;
2262 t->used = TRUE;
2263 d = NULL;
2264
2265 if (t->globals.list != NULL)
2266 d = (*t->match) (&t->globals, NULL, alc);
2267
2268 /* See if there is anything to force this symbol to
2269 local scope. */
2270 if (d == NULL && t->locals.list != NULL)
2271 {
2272 d = (*t->match) (&t->locals, NULL, alc);
2273 if (d != NULL
2274 && h->dynindx != -1
2275 && ! info->export_dynamic)
2276 *hide = TRUE;
2277 }
2278
2279 free (alc);
2280 break;
2281 }
2282 }
2283
2284 *t_p = t;
2285
2286 return TRUE;
2287}
2288
2289/* Return TRUE if the symbol H is hidden by version script. */
2290
2291bfd_boolean
2292_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2293 struct elf_link_hash_entry *h)
2294{
2295 const char *p;
2296 bfd_boolean hide = FALSE;
2297 const struct elf_backend_data *bed
2298 = get_elf_backend_data (info->output_bfd);
2299
2300 /* Version script only hides symbols defined in regular objects. */
2301 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2302 return TRUE;
2303
2304 p = strchr (h->root.root.string, ELF_VER_CHR);
2305 if (p != NULL && h->verinfo.vertree == NULL)
2306 {
2307 struct bfd_elf_version_tree *t;
2308
2309 ++p;
2310 if (*p == ELF_VER_CHR)
2311 ++p;
2312
2313 if (*p != '\0'
2314 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2315 && hide)
2316 {
2317 if (hide)
2318 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2319 return TRUE;
2320 }
2321 }
2322
2323 /* If we don't have a version for this symbol, see if we can find
2324 something. */
2325 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2326 {
2327 h->verinfo.vertree
2328 = bfd_find_version_for_sym (info->version_info,
2329 h->root.root.string, &hide);
2330 if (h->verinfo.vertree != NULL && hide)
2331 {
2332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2333 return TRUE;
2334 }
2335 }
2336
2337 return FALSE;
2338}
2339
45d6a902
AM
2340/* Figure out appropriate versions for all the symbols. We may not
2341 have the version number script until we have read all of the input
2342 files, so until that point we don't know which symbols should be
2343 local. This function is called via elf_link_hash_traverse. */
2344
28caa186 2345static bfd_boolean
268b6b39 2346_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2347{
28caa186 2348 struct elf_info_failed *sinfo;
45d6a902 2349 struct bfd_link_info *info;
9c5bfbb7 2350 const struct elf_backend_data *bed;
45d6a902
AM
2351 struct elf_info_failed eif;
2352 char *p;
099bb8fb 2353 bfd_boolean hide;
45d6a902 2354
a50b1753 2355 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2356 info = sinfo->info;
2357
45d6a902
AM
2358 /* Fix the symbol flags. */
2359 eif.failed = FALSE;
2360 eif.info = info;
2361 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2362 {
2363 if (eif.failed)
2364 sinfo->failed = TRUE;
2365 return FALSE;
2366 }
2367
0a640d71
L
2368 bed = get_elf_backend_data (info->output_bfd);
2369
45d6a902
AM
2370 /* We only need version numbers for symbols defined in regular
2371 objects. */
f5385ebf 2372 if (!h->def_regular)
0a640d71
L
2373 {
2374 /* Hide symbols defined in discarded input sections. */
2375 if ((h->root.type == bfd_link_hash_defined
2376 || h->root.type == bfd_link_hash_defweak)
2377 && discarded_section (h->root.u.def.section))
2378 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2379 return TRUE;
2380 }
45d6a902 2381
099bb8fb 2382 hide = FALSE;
45d6a902
AM
2383 p = strchr (h->root.root.string, ELF_VER_CHR);
2384 if (p != NULL && h->verinfo.vertree == NULL)
2385 {
2386 struct bfd_elf_version_tree *t;
45d6a902 2387
45d6a902
AM
2388 ++p;
2389 if (*p == ELF_VER_CHR)
6e33951e 2390 ++p;
45d6a902
AM
2391
2392 /* If there is no version string, we can just return out. */
2393 if (*p == '\0')
6e33951e 2394 return TRUE;
45d6a902 2395
099bb8fb 2396 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2397 {
099bb8fb
L
2398 sinfo->failed = TRUE;
2399 return FALSE;
45d6a902
AM
2400 }
2401
099bb8fb
L
2402 if (hide)
2403 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2404
45d6a902
AM
2405 /* If we are building an application, we need to create a
2406 version node for this version. */
0e1862bb 2407 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2408 {
2409 struct bfd_elf_version_tree **pp;
2410 int version_index;
2411
2412 /* If we aren't going to export this symbol, we don't need
2413 to worry about it. */
2414 if (h->dynindx == -1)
2415 return TRUE;
2416
ef53be89
AM
2417 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2418 sizeof *t);
45d6a902
AM
2419 if (t == NULL)
2420 {
2421 sinfo->failed = TRUE;
2422 return FALSE;
2423 }
2424
45d6a902 2425 t->name = p;
45d6a902
AM
2426 t->name_indx = (unsigned int) -1;
2427 t->used = TRUE;
2428
2429 version_index = 1;
2430 /* Don't count anonymous version tag. */
fd91d419
L
2431 if (sinfo->info->version_info != NULL
2432 && sinfo->info->version_info->vernum == 0)
45d6a902 2433 version_index = 0;
fd91d419
L
2434 for (pp = &sinfo->info->version_info;
2435 *pp != NULL;
2436 pp = &(*pp)->next)
45d6a902
AM
2437 ++version_index;
2438 t->vernum = version_index;
2439
2440 *pp = t;
2441
2442 h->verinfo.vertree = t;
2443 }
2444 else if (t == NULL)
2445 {
2446 /* We could not find the version for a symbol when
2447 generating a shared archive. Return an error. */
4eca0228 2448 _bfd_error_handler
695344c0 2449 /* xgettext:c-format */
871b3ab2 2450 (_("%pB: version node not found for symbol %s"),
28caa186 2451 info->output_bfd, h->root.root.string);
45d6a902
AM
2452 bfd_set_error (bfd_error_bad_value);
2453 sinfo->failed = TRUE;
2454 return FALSE;
2455 }
45d6a902
AM
2456 }
2457
2458 /* If we don't have a version for this symbol, see if we can find
2459 something. */
099bb8fb
L
2460 if (!hide
2461 && h->verinfo.vertree == NULL
2462 && sinfo->info->version_info != NULL)
45d6a902 2463 {
fd91d419
L
2464 h->verinfo.vertree
2465 = bfd_find_version_for_sym (sinfo->info->version_info,
2466 h->root.root.string, &hide);
1e8fa21e
AM
2467 if (h->verinfo.vertree != NULL && hide)
2468 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2469 }
2470
2471 return TRUE;
2472}
2473\f
45d6a902
AM
2474/* Read and swap the relocs from the section indicated by SHDR. This
2475 may be either a REL or a RELA section. The relocations are
2476 translated into RELA relocations and stored in INTERNAL_RELOCS,
2477 which should have already been allocated to contain enough space.
2478 The EXTERNAL_RELOCS are a buffer where the external form of the
2479 relocations should be stored.
2480
2481 Returns FALSE if something goes wrong. */
2482
2483static bfd_boolean
268b6b39 2484elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2485 asection *sec,
268b6b39
AM
2486 Elf_Internal_Shdr *shdr,
2487 void *external_relocs,
2488 Elf_Internal_Rela *internal_relocs)
45d6a902 2489{
9c5bfbb7 2490 const struct elf_backend_data *bed;
268b6b39 2491 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2492 const bfd_byte *erela;
2493 const bfd_byte *erelaend;
2494 Elf_Internal_Rela *irela;
243ef1e0
L
2495 Elf_Internal_Shdr *symtab_hdr;
2496 size_t nsyms;
45d6a902 2497
45d6a902
AM
2498 /* Position ourselves at the start of the section. */
2499 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2500 return FALSE;
2501
2502 /* Read the relocations. */
2503 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2504 return FALSE;
2505
243ef1e0 2506 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2507 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2508
45d6a902
AM
2509 bed = get_elf_backend_data (abfd);
2510
2511 /* Convert the external relocations to the internal format. */
2512 if (shdr->sh_entsize == bed->s->sizeof_rel)
2513 swap_in = bed->s->swap_reloc_in;
2514 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2515 swap_in = bed->s->swap_reloca_in;
2516 else
2517 {
2518 bfd_set_error (bfd_error_wrong_format);
2519 return FALSE;
2520 }
2521
a50b1753 2522 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2523 /* Setting erelaend like this and comparing with <= handles case of
2524 a fuzzed object with sh_size not a multiple of sh_entsize. */
2525 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2526 irela = internal_relocs;
f55b1e32 2527 while (erela <= erelaend)
45d6a902 2528 {
243ef1e0
L
2529 bfd_vma r_symndx;
2530
45d6a902 2531 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2532 r_symndx = ELF32_R_SYM (irela->r_info);
2533 if (bed->s->arch_size == 64)
2534 r_symndx >>= 24;
ce98a316
NC
2535 if (nsyms > 0)
2536 {
2537 if ((size_t) r_symndx >= nsyms)
2538 {
4eca0228 2539 _bfd_error_handler
695344c0 2540 /* xgettext:c-format */
2dcf00ce
AM
2541 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2542 " for offset %#" PRIx64 " in section `%pA'"),
2543 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2544 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2545 bfd_set_error (bfd_error_bad_value);
2546 return FALSE;
2547 }
2548 }
cf35638d 2549 else if (r_symndx != STN_UNDEF)
243ef1e0 2550 {
4eca0228 2551 _bfd_error_handler
695344c0 2552 /* xgettext:c-format */
2dcf00ce
AM
2553 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2554 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2555 " when the object file has no symbol table"),
2dcf00ce
AM
2556 abfd, (uint64_t) r_symndx,
2557 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2558 bfd_set_error (bfd_error_bad_value);
2559 return FALSE;
2560 }
45d6a902
AM
2561 irela += bed->s->int_rels_per_ext_rel;
2562 erela += shdr->sh_entsize;
2563 }
2564
2565 return TRUE;
2566}
2567
2568/* Read and swap the relocs for a section O. They may have been
2569 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2570 not NULL, they are used as buffers to read into. They are known to
2571 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2572 the return value is allocated using either malloc or bfd_alloc,
2573 according to the KEEP_MEMORY argument. If O has two relocation
2574 sections (both REL and RELA relocations), then the REL_HDR
2575 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2576 RELA_HDR relocations. */
45d6a902
AM
2577
2578Elf_Internal_Rela *
268b6b39
AM
2579_bfd_elf_link_read_relocs (bfd *abfd,
2580 asection *o,
2581 void *external_relocs,
2582 Elf_Internal_Rela *internal_relocs,
2583 bfd_boolean keep_memory)
45d6a902 2584{
268b6b39 2585 void *alloc1 = NULL;
45d6a902 2586 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2588 struct bfd_elf_section_data *esdo = elf_section_data (o);
2589 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2590
d4730f92
BS
2591 if (esdo->relocs != NULL)
2592 return esdo->relocs;
45d6a902
AM
2593
2594 if (o->reloc_count == 0)
2595 return NULL;
2596
45d6a902
AM
2597 if (internal_relocs == NULL)
2598 {
2599 bfd_size_type size;
2600
056bafd4 2601 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2602 if (keep_memory)
a50b1753 2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2604 else
a50b1753 2605 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2606 if (internal_relocs == NULL)
2607 goto error_return;
2608 }
2609
2610 if (external_relocs == NULL)
2611 {
d4730f92
BS
2612 bfd_size_type size = 0;
2613
2614 if (esdo->rel.hdr)
2615 size += esdo->rel.hdr->sh_size;
2616 if (esdo->rela.hdr)
2617 size += esdo->rela.hdr->sh_size;
45d6a902 2618
268b6b39 2619 alloc1 = bfd_malloc (size);
45d6a902
AM
2620 if (alloc1 == NULL)
2621 goto error_return;
2622 external_relocs = alloc1;
2623 }
2624
d4730f92
BS
2625 internal_rela_relocs = internal_relocs;
2626 if (esdo->rel.hdr)
2627 {
2628 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2629 external_relocs,
2630 internal_relocs))
2631 goto error_return;
2632 external_relocs = (((bfd_byte *) external_relocs)
2633 + esdo->rel.hdr->sh_size);
2634 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2635 * bed->s->int_rels_per_ext_rel);
2636 }
2637
2638 if (esdo->rela.hdr
2639 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2640 external_relocs,
2641 internal_rela_relocs)))
45d6a902
AM
2642 goto error_return;
2643
2644 /* Cache the results for next time, if we can. */
2645 if (keep_memory)
d4730f92 2646 esdo->relocs = internal_relocs;
45d6a902
AM
2647
2648 if (alloc1 != NULL)
2649 free (alloc1);
2650
2651 /* Don't free alloc2, since if it was allocated we are passing it
2652 back (under the name of internal_relocs). */
2653
2654 return internal_relocs;
2655
2656 error_return:
2657 if (alloc1 != NULL)
2658 free (alloc1);
2659 if (alloc2 != NULL)
4dd07732
AM
2660 {
2661 if (keep_memory)
2662 bfd_release (abfd, alloc2);
2663 else
2664 free (alloc2);
2665 }
45d6a902
AM
2666 return NULL;
2667}
2668
2669/* Compute the size of, and allocate space for, REL_HDR which is the
2670 section header for a section containing relocations for O. */
2671
28caa186 2672static bfd_boolean
9eaff861
AO
2673_bfd_elf_link_size_reloc_section (bfd *abfd,
2674 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2675{
9eaff861 2676 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2677
2678 /* That allows us to calculate the size of the section. */
9eaff861 2679 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2680
2681 /* The contents field must last into write_object_contents, so we
2682 allocate it with bfd_alloc rather than malloc. Also since we
2683 cannot be sure that the contents will actually be filled in,
2684 we zero the allocated space. */
a50b1753 2685 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2686 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2687 return FALSE;
2688
d4730f92 2689 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2690 {
2691 struct elf_link_hash_entry **p;
2692
ca4be51c
AM
2693 p = ((struct elf_link_hash_entry **)
2694 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2695 if (p == NULL)
2696 return FALSE;
2697
d4730f92 2698 reldata->hashes = p;
45d6a902
AM
2699 }
2700
2701 return TRUE;
2702}
2703
2704/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2705 originated from the section given by INPUT_REL_HDR) to the
2706 OUTPUT_BFD. */
2707
2708bfd_boolean
268b6b39
AM
2709_bfd_elf_link_output_relocs (bfd *output_bfd,
2710 asection *input_section,
2711 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2712 Elf_Internal_Rela *internal_relocs,
2713 struct elf_link_hash_entry **rel_hash
2714 ATTRIBUTE_UNUSED)
45d6a902
AM
2715{
2716 Elf_Internal_Rela *irela;
2717 Elf_Internal_Rela *irelaend;
2718 bfd_byte *erel;
d4730f92 2719 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2720 asection *output_section;
9c5bfbb7 2721 const struct elf_backend_data *bed;
268b6b39 2722 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2723 struct bfd_elf_section_data *esdo;
45d6a902
AM
2724
2725 output_section = input_section->output_section;
45d6a902 2726
d4730f92
BS
2727 bed = get_elf_backend_data (output_bfd);
2728 esdo = elf_section_data (output_section);
2729 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2730 {
d4730f92
BS
2731 output_reldata = &esdo->rel;
2732 swap_out = bed->s->swap_reloc_out;
45d6a902 2733 }
d4730f92
BS
2734 else if (esdo->rela.hdr
2735 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2736 {
d4730f92
BS
2737 output_reldata = &esdo->rela;
2738 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2739 }
2740 else
2741 {
4eca0228 2742 _bfd_error_handler
695344c0 2743 /* xgettext:c-format */
871b3ab2 2744 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2745 output_bfd, input_section->owner, input_section);
297d8443 2746 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2747 return FALSE;
2748 }
2749
d4730f92
BS
2750 erel = output_reldata->hdr->contents;
2751 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2752 irela = internal_relocs;
2753 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2754 * bed->s->int_rels_per_ext_rel);
2755 while (irela < irelaend)
2756 {
2757 (*swap_out) (output_bfd, irela, erel);
2758 irela += bed->s->int_rels_per_ext_rel;
2759 erel += input_rel_hdr->sh_entsize;
2760 }
2761
2762 /* Bump the counter, so that we know where to add the next set of
2763 relocations. */
d4730f92 2764 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2765
2766 return TRUE;
2767}
2768\f
508c3946
L
2769/* Make weak undefined symbols in PIE dynamic. */
2770
2771bfd_boolean
2772_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2773 struct elf_link_hash_entry *h)
2774{
0e1862bb 2775 if (bfd_link_pie (info)
508c3946
L
2776 && h->dynindx == -1
2777 && h->root.type == bfd_link_hash_undefweak)
2778 return bfd_elf_link_record_dynamic_symbol (info, h);
2779
2780 return TRUE;
2781}
2782
45d6a902
AM
2783/* Fix up the flags for a symbol. This handles various cases which
2784 can only be fixed after all the input files are seen. This is
2785 currently called by both adjust_dynamic_symbol and
2786 assign_sym_version, which is unnecessary but perhaps more robust in
2787 the face of future changes. */
2788
28caa186 2789static bfd_boolean
268b6b39
AM
2790_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2791 struct elf_info_failed *eif)
45d6a902 2792{
33774f08 2793 const struct elf_backend_data *bed;
508c3946 2794
45d6a902
AM
2795 /* If this symbol was mentioned in a non-ELF file, try to set
2796 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2797 permit a non-ELF file to correctly refer to a symbol defined in
2798 an ELF dynamic object. */
f5385ebf 2799 if (h->non_elf)
45d6a902
AM
2800 {
2801 while (h->root.type == bfd_link_hash_indirect)
2802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2803
2804 if (h->root.type != bfd_link_hash_defined
2805 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2806 {
2807 h->ref_regular = 1;
2808 h->ref_regular_nonweak = 1;
2809 }
45d6a902
AM
2810 else
2811 {
2812 if (h->root.u.def.section->owner != NULL
2813 && (bfd_get_flavour (h->root.u.def.section->owner)
2814 == bfd_target_elf_flavour))
f5385ebf
AM
2815 {
2816 h->ref_regular = 1;
2817 h->ref_regular_nonweak = 1;
2818 }
45d6a902 2819 else
f5385ebf 2820 h->def_regular = 1;
45d6a902
AM
2821 }
2822
2823 if (h->dynindx == -1
f5385ebf
AM
2824 && (h->def_dynamic
2825 || h->ref_dynamic))
45d6a902 2826 {
c152c796 2827 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2828 {
2829 eif->failed = TRUE;
2830 return FALSE;
2831 }
2832 }
2833 }
2834 else
2835 {
f5385ebf 2836 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2837 was first seen in a non-ELF file. Fortunately, if the symbol
2838 was first seen in an ELF file, we're probably OK unless the
2839 symbol was defined in a non-ELF file. Catch that case here.
2840 FIXME: We're still in trouble if the symbol was first seen in
2841 a dynamic object, and then later in a non-ELF regular object. */
2842 if ((h->root.type == bfd_link_hash_defined
2843 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2844 && !h->def_regular
45d6a902
AM
2845 && (h->root.u.def.section->owner != NULL
2846 ? (bfd_get_flavour (h->root.u.def.section->owner)
2847 != bfd_target_elf_flavour)
2848 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2849 && !h->def_dynamic)))
2850 h->def_regular = 1;
45d6a902
AM
2851 }
2852
508c3946 2853 /* Backend specific symbol fixup. */
33774f08
AM
2854 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2855 if (bed->elf_backend_fixup_symbol
2856 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2857 return FALSE;
508c3946 2858
45d6a902
AM
2859 /* If this is a final link, and the symbol was defined as a common
2860 symbol in a regular object file, and there was no definition in
2861 any dynamic object, then the linker will have allocated space for
f5385ebf 2862 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2863 flag will not have been set. */
2864 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2865 && !h->def_regular
2866 && h->ref_regular
2867 && !h->def_dynamic
96f29d96 2868 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2869 h->def_regular = 1;
45d6a902 2870
af0bfb9c
AM
2871 /* Symbols defined in discarded sections shouldn't be dynamic. */
2872 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2873 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2874
4deb8f71
L
2875 /* If a weak undefined symbol has non-default visibility, we also
2876 hide it from the dynamic linker. */
af0bfb9c
AM
2877 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2878 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2879 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2880
2881 /* A hidden versioned symbol in executable should be forced local if
2882 it is is locally defined, not referenced by shared library and not
2883 exported. */
2884 else if (bfd_link_executable (eif->info)
2885 && h->versioned == versioned_hidden
2886 && !eif->info->export_dynamic
2887 && !h->dynamic
2888 && !h->ref_dynamic
2889 && h->def_regular)
2890 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2891
45d6a902
AM
2892 /* If -Bsymbolic was used (which means to bind references to global
2893 symbols to the definition within the shared object), and this
2894 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2895 need a PLT entry. Likewise, if the symbol has non-default
2896 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2897 will force it local. */
4deb8f71
L
2898 else if (h->needs_plt
2899 && bfd_link_pic (eif->info)
2900 && is_elf_hash_table (eif->info->hash)
2901 && (SYMBOLIC_BIND (eif->info, h)
2902 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2903 && h->def_regular)
45d6a902 2904 {
45d6a902
AM
2905 bfd_boolean force_local;
2906
45d6a902
AM
2907 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2908 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2909 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2910 }
2911
45d6a902
AM
2912 /* If this is a weak defined symbol in a dynamic object, and we know
2913 the real definition in the dynamic object, copy interesting flags
2914 over to the real definition. */
60d67dc8 2915 if (h->is_weakalias)
45d6a902 2916 {
60d67dc8
AM
2917 struct elf_link_hash_entry *def = weakdef (h);
2918
45d6a902
AM
2919 /* If the real definition is defined by a regular object file,
2920 don't do anything special. See the longer description in
5b9d7a9a
AM
2921 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2922 bfd_link_hash_defined as it was when put on the alias list
2923 then it must have originally been a versioned symbol (for
2924 which a non-versioned indirect symbol is created) and later
2925 a definition for the non-versioned symbol is found. In that
2926 case the indirection is flipped with the versioned symbol
2927 becoming an indirect pointing at the non-versioned symbol.
2928 Thus, not an alias any more. */
2929 if (def->def_regular
2930 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2931 {
2932 h = def;
2933 while ((h = h->u.alias) != def)
2934 h->is_weakalias = 0;
2935 }
45d6a902 2936 else
a26587ba 2937 {
4e6b54a6
AM
2938 while (h->root.type == bfd_link_hash_indirect)
2939 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2940 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2941 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2942 BFD_ASSERT (def->def_dynamic);
60d67dc8 2943 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2944 }
45d6a902
AM
2945 }
2946
2947 return TRUE;
2948}
2949
2950/* Make the backend pick a good value for a dynamic symbol. This is
2951 called via elf_link_hash_traverse, and also calls itself
2952 recursively. */
2953
28caa186 2954static bfd_boolean
268b6b39 2955_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2956{
a50b1753 2957 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2958 struct elf_link_hash_table *htab;
9c5bfbb7 2959 const struct elf_backend_data *bed;
45d6a902 2960
0eddce27 2961 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2962 return FALSE;
2963
45d6a902
AM
2964 /* Ignore indirect symbols. These are added by the versioning code. */
2965 if (h->root.type == bfd_link_hash_indirect)
2966 return TRUE;
2967
2968 /* Fix the symbol flags. */
2969 if (! _bfd_elf_fix_symbol_flags (h, eif))
2970 return FALSE;
2971
559192d8
AM
2972 htab = elf_hash_table (eif->info);
2973 bed = get_elf_backend_data (htab->dynobj);
2974
954b63d4
AM
2975 if (h->root.type == bfd_link_hash_undefweak)
2976 {
2977 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2978 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2979 else if (eif->info->dynamic_undefined_weak > 0
2980 && h->ref_regular
2981 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2982 && !bfd_hide_sym_by_version (eif->info->version_info,
2983 h->root.root.string))
2984 {
2985 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2986 {
2987 eif->failed = TRUE;
2988 return FALSE;
2989 }
2990 }
2991 }
2992
45d6a902
AM
2993 /* If this symbol does not require a PLT entry, and it is not
2994 defined by a dynamic object, or is not referenced by a regular
2995 object, ignore it. We do have to handle a weak defined symbol,
2996 even if no regular object refers to it, if we decided to add it
2997 to the dynamic symbol table. FIXME: Do we normally need to worry
2998 about symbols which are defined by one dynamic object and
2999 referenced by another one? */
f5385ebf 3000 if (!h->needs_plt
91e21fb7 3001 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3002 && (h->def_regular
3003 || !h->def_dynamic
3004 || (!h->ref_regular
60d67dc8 3005 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3006 {
a6aa5195 3007 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3008 return TRUE;
3009 }
3010
3011 /* If we've already adjusted this symbol, don't do it again. This
3012 can happen via a recursive call. */
f5385ebf 3013 if (h->dynamic_adjusted)
45d6a902
AM
3014 return TRUE;
3015
3016 /* Don't look at this symbol again. Note that we must set this
3017 after checking the above conditions, because we may look at a
3018 symbol once, decide not to do anything, and then get called
3019 recursively later after REF_REGULAR is set below. */
f5385ebf 3020 h->dynamic_adjusted = 1;
45d6a902
AM
3021
3022 /* If this is a weak definition, and we know a real definition, and
3023 the real symbol is not itself defined by a regular object file,
3024 then get a good value for the real definition. We handle the
3025 real symbol first, for the convenience of the backend routine.
3026
3027 Note that there is a confusing case here. If the real definition
3028 is defined by a regular object file, we don't get the real symbol
3029 from the dynamic object, but we do get the weak symbol. If the
3030 processor backend uses a COPY reloc, then if some routine in the
3031 dynamic object changes the real symbol, we will not see that
3032 change in the corresponding weak symbol. This is the way other
3033 ELF linkers work as well, and seems to be a result of the shared
3034 library model.
3035
3036 I will clarify this issue. Most SVR4 shared libraries define the
3037 variable _timezone and define timezone as a weak synonym. The
3038 tzset call changes _timezone. If you write
3039 extern int timezone;
3040 int _timezone = 5;
3041 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3042 you might expect that, since timezone is a synonym for _timezone,
3043 the same number will print both times. However, if the processor
3044 backend uses a COPY reloc, then actually timezone will be copied
3045 into your process image, and, since you define _timezone
3046 yourself, _timezone will not. Thus timezone and _timezone will
3047 wind up at different memory locations. The tzset call will set
3048 _timezone, leaving timezone unchanged. */
3049
60d67dc8 3050 if (h->is_weakalias)
45d6a902 3051 {
60d67dc8
AM
3052 struct elf_link_hash_entry *def = weakdef (h);
3053
ec24dc88 3054 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3055 the alias by a regular object file via the weak symbol H. */
3056 def->ref_regular = 1;
45d6a902 3057
ec24dc88 3058 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3059 the strong alias before H by recursively calling ourselves. */
3060 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3061 return FALSE;
3062 }
3063
3064 /* If a symbol has no type and no size and does not require a PLT
3065 entry, then we are probably about to do the wrong thing here: we
3066 are probably going to create a COPY reloc for an empty object.
3067 This case can arise when a shared object is built with assembly
3068 code, and the assembly code fails to set the symbol type. */
3069 if (h->size == 0
3070 && h->type == STT_NOTYPE
f5385ebf 3071 && !h->needs_plt)
4eca0228 3072 _bfd_error_handler
45d6a902
AM
3073 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3074 h->root.root.string);
3075
45d6a902
AM
3076 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3077 {
3078 eif->failed = TRUE;
3079 return FALSE;
3080 }
3081
3082 return TRUE;
3083}
3084
027297b7
L
3085/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3086 DYNBSS. */
3087
3088bfd_boolean
6cabe1ea
AM
3089_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3090 struct elf_link_hash_entry *h,
027297b7
L
3091 asection *dynbss)
3092{
91ac5911 3093 unsigned int power_of_two;
027297b7
L
3094 bfd_vma mask;
3095 asection *sec = h->root.u.def.section;
3096
de194d85 3097 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3098 requirement of symbols defined in the section. Since we don't
3099 know the symbol alignment requirement, we start with the
3100 maximum alignment and check low bits of the symbol address
3101 for the minimum alignment. */
3102 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3103 mask = ((bfd_vma) 1 << power_of_two) - 1;
3104 while ((h->root.u.def.value & mask) != 0)
3105 {
3106 mask >>= 1;
3107 --power_of_two;
3108 }
027297b7 3109
91ac5911
L
3110 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3111 dynbss))
027297b7
L
3112 {
3113 /* Adjust the section alignment if needed. */
3114 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3115 power_of_two))
027297b7
L
3116 return FALSE;
3117 }
3118
91ac5911 3119 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3120 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3121
3122 /* Define the symbol as being at this point in DYNBSS. */
3123 h->root.u.def.section = dynbss;
3124 h->root.u.def.value = dynbss->size;
3125
3126 /* Increment the size of DYNBSS to make room for the symbol. */
3127 dynbss->size += h->size;
3128
f7483970
L
3129 /* No error if extern_protected_data is true. */
3130 if (h->protected_def
889c2a67
L
3131 && (!info->extern_protected_data
3132 || (info->extern_protected_data < 0
3133 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3134 info->callbacks->einfo
c1c8c1ef 3135 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3136 h->root.root.string);
6cabe1ea 3137
027297b7
L
3138 return TRUE;
3139}
3140
45d6a902
AM
3141/* Adjust all external symbols pointing into SEC_MERGE sections
3142 to reflect the object merging within the sections. */
3143
28caa186 3144static bfd_boolean
268b6b39 3145_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3146{
3147 asection *sec;
3148
45d6a902
AM
3149 if ((h->root.type == bfd_link_hash_defined
3150 || h->root.type == bfd_link_hash_defweak)
3151 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3152 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3153 {
a50b1753 3154 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3155
3156 h->root.u.def.value =
3157 _bfd_merged_section_offset (output_bfd,
3158 &h->root.u.def.section,
3159 elf_section_data (sec)->sec_info,
753731ee 3160 h->root.u.def.value);
45d6a902
AM
3161 }
3162
3163 return TRUE;
3164}
986a241f
RH
3165
3166/* Returns false if the symbol referred to by H should be considered
3167 to resolve local to the current module, and true if it should be
3168 considered to bind dynamically. */
3169
3170bfd_boolean
268b6b39
AM
3171_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3172 struct bfd_link_info *info,
89a2ee5a 3173 bfd_boolean not_local_protected)
986a241f
RH
3174{
3175 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3176 const struct elf_backend_data *bed;
3177 struct elf_link_hash_table *hash_table;
986a241f
RH
3178
3179 if (h == NULL)
3180 return FALSE;
3181
3182 while (h->root.type == bfd_link_hash_indirect
3183 || h->root.type == bfd_link_hash_warning)
3184 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3185
3186 /* If it was forced local, then clearly it's not dynamic. */
3187 if (h->dynindx == -1)
3188 return FALSE;
f5385ebf 3189 if (h->forced_local)
986a241f
RH
3190 return FALSE;
3191
3192 /* Identify the cases where name binding rules say that a
3193 visible symbol resolves locally. */
0e1862bb
L
3194 binding_stays_local_p = (bfd_link_executable (info)
3195 || SYMBOLIC_BIND (info, h));
986a241f
RH
3196
3197 switch (ELF_ST_VISIBILITY (h->other))
3198 {
3199 case STV_INTERNAL:
3200 case STV_HIDDEN:
3201 return FALSE;
3202
3203 case STV_PROTECTED:
fcb93ecf
PB
3204 hash_table = elf_hash_table (info);
3205 if (!is_elf_hash_table (hash_table))
3206 return FALSE;
3207
3208 bed = get_elf_backend_data (hash_table->dynobj);
3209
986a241f
RH
3210 /* Proper resolution for function pointer equality may require
3211 that these symbols perhaps be resolved dynamically, even though
3212 we should be resolving them to the current module. */
89a2ee5a 3213 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3214 binding_stays_local_p = TRUE;
3215 break;
3216
3217 default:
986a241f
RH
3218 break;
3219 }
3220
aa37626c 3221 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3222 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3223 return TRUE;
3224
986a241f
RH
3225 /* Otherwise, the symbol is dynamic if binding rules don't tell
3226 us that it remains local. */
3227 return !binding_stays_local_p;
3228}
f6c52c13
AM
3229
3230/* Return true if the symbol referred to by H should be considered
3231 to resolve local to the current module, and false otherwise. Differs
3232 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3233 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3234 for the place where dynindx == -1 is tested. If that test is true,
3235 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3236 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3237 defined symbols.
89a2ee5a
AM
3238 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3239 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3240 treatment of undefined weak symbols. For those that do not make
3241 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3242
3243bfd_boolean
268b6b39
AM
3244_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3245 struct bfd_link_info *info,
3246 bfd_boolean local_protected)
f6c52c13 3247{
fcb93ecf
PB
3248 const struct elf_backend_data *bed;
3249 struct elf_link_hash_table *hash_table;
3250
f6c52c13
AM
3251 /* If it's a local sym, of course we resolve locally. */
3252 if (h == NULL)
3253 return TRUE;
3254
d95edcac
L
3255 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3256 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3257 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3258 return TRUE;
3259
0fad2956
MR
3260 /* Forced local symbols resolve locally. */
3261 if (h->forced_local)
3262 return TRUE;
3263
7e2294f9
AO
3264 /* Common symbols that become definitions don't get the DEF_REGULAR
3265 flag set, so test it first, and don't bail out. */
3266 if (ELF_COMMON_DEF_P (h))
3267 /* Do nothing. */;
f6c52c13 3268 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3269 resolve locally. The sym is either undefined or dynamic. */
3270 else if (!h->def_regular)
f6c52c13
AM
3271 return FALSE;
3272
0fad2956 3273 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3274 if (h->dynindx == -1)
3275 return TRUE;
3276
3277 /* At this point, we know the symbol is defined and dynamic. In an
3278 executable it must resolve locally, likewise when building symbolic
3279 shared libraries. */
0e1862bb 3280 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3281 return TRUE;
3282
3283 /* Now deal with defined dynamic symbols in shared libraries. Ones
3284 with default visibility might not resolve locally. */
3285 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3286 return FALSE;
3287
fcb93ecf
PB
3288 hash_table = elf_hash_table (info);
3289 if (!is_elf_hash_table (hash_table))
3290 return TRUE;
3291
3292 bed = get_elf_backend_data (hash_table->dynobj);
3293
f7483970
L
3294 /* If extern_protected_data is false, STV_PROTECTED non-function
3295 symbols are local. */
889c2a67
L
3296 if ((!info->extern_protected_data
3297 || (info->extern_protected_data < 0
3298 && !bed->extern_protected_data))
3299 && !bed->is_function_type (h->type))
1c16dfa5
L
3300 return TRUE;
3301
f6c52c13 3302 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3303 symbols be treated as dynamic symbols. If the address of a
3304 function not defined in an executable is set to that function's
3305 plt entry in the executable, then the address of the function in
3306 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3307 return local_protected;
3308}
e1918d23
AM
3309
3310/* Caches some TLS segment info, and ensures that the TLS segment vma is
3311 aligned. Returns the first TLS output section. */
3312
3313struct bfd_section *
3314_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3315{
3316 struct bfd_section *sec, *tls;
3317 unsigned int align = 0;
3318
3319 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3320 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3321 break;
3322 tls = sec;
3323
3324 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3325 if (sec->alignment_power > align)
3326 align = sec->alignment_power;
3327
3328 elf_hash_table (info)->tls_sec = tls;
3329
3330 /* Ensure the alignment of the first section is the largest alignment,
3331 so that the tls segment starts aligned. */
3332 if (tls != NULL)
3333 tls->alignment_power = align;
3334
3335 return tls;
3336}
0ad989f9
L
3337
3338/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3339static bfd_boolean
3340is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3341 Elf_Internal_Sym *sym)
3342{
a4d8e49b
L
3343 const struct elf_backend_data *bed;
3344
0ad989f9
L
3345 /* Local symbols do not count, but target specific ones might. */
3346 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3347 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3348 return FALSE;
3349
fcb93ecf 3350 bed = get_elf_backend_data (abfd);
0ad989f9 3351 /* Function symbols do not count. */
fcb93ecf 3352 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3353 return FALSE;
3354
3355 /* If the section is undefined, then so is the symbol. */
3356 if (sym->st_shndx == SHN_UNDEF)
3357 return FALSE;
3358
3359 /* If the symbol is defined in the common section, then
3360 it is a common definition and so does not count. */
a4d8e49b 3361 if (bed->common_definition (sym))
0ad989f9
L
3362 return FALSE;
3363
3364 /* If the symbol is in a target specific section then we
3365 must rely upon the backend to tell us what it is. */
3366 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3367 /* FIXME - this function is not coded yet:
3368
3369 return _bfd_is_global_symbol_definition (abfd, sym);
3370
3371 Instead for now assume that the definition is not global,
3372 Even if this is wrong, at least the linker will behave
3373 in the same way that it used to do. */
3374 return FALSE;
3375
3376 return TRUE;
3377}
3378
3379/* Search the symbol table of the archive element of the archive ABFD
3380 whose archive map contains a mention of SYMDEF, and determine if
3381 the symbol is defined in this element. */
3382static bfd_boolean
3383elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3384{
3385 Elf_Internal_Shdr * hdr;
ef53be89
AM
3386 size_t symcount;
3387 size_t extsymcount;
3388 size_t extsymoff;
0ad989f9
L
3389 Elf_Internal_Sym *isymbuf;
3390 Elf_Internal_Sym *isym;
3391 Elf_Internal_Sym *isymend;
3392 bfd_boolean result;
3393
3394 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3395 if (abfd == NULL)
3396 return FALSE;
3397
3398 if (! bfd_check_format (abfd, bfd_object))
3399 return FALSE;
3400
7dc3990e
L
3401 /* Select the appropriate symbol table. If we don't know if the
3402 object file is an IR object, give linker LTO plugin a chance to
3403 get the correct symbol table. */
3404 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3405#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3406 || (abfd->plugin_format == bfd_plugin_unknown
3407 && bfd_link_plugin_object_p (abfd))
3408#endif
3409 )
3410 {
3411 /* Use the IR symbol table if the object has been claimed by
3412 plugin. */
3413 abfd = abfd->plugin_dummy_bfd;
3414 hdr = &elf_tdata (abfd)->symtab_hdr;
3415 }
3416 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3417 hdr = &elf_tdata (abfd)->symtab_hdr;
3418 else
3419 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3420
3421 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3422
3423 /* The sh_info field of the symtab header tells us where the
3424 external symbols start. We don't care about the local symbols. */
3425 if (elf_bad_symtab (abfd))
3426 {
3427 extsymcount = symcount;
3428 extsymoff = 0;
3429 }
3430 else
3431 {
3432 extsymcount = symcount - hdr->sh_info;
3433 extsymoff = hdr->sh_info;
3434 }
3435
3436 if (extsymcount == 0)
3437 return FALSE;
3438
3439 /* Read in the symbol table. */
3440 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3441 NULL, NULL, NULL);
3442 if (isymbuf == NULL)
3443 return FALSE;
3444
3445 /* Scan the symbol table looking for SYMDEF. */
3446 result = FALSE;
3447 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3448 {
3449 const char *name;
3450
3451 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 isym->st_name);
3453 if (name == NULL)
3454 break;
3455
3456 if (strcmp (name, symdef->name) == 0)
3457 {
3458 result = is_global_data_symbol_definition (abfd, isym);
3459 break;
3460 }
3461 }
3462
3463 free (isymbuf);
3464
3465 return result;
3466}
3467\f
5a580b3a
AM
3468/* Add an entry to the .dynamic table. */
3469
3470bfd_boolean
3471_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3472 bfd_vma tag,
3473 bfd_vma val)
3474{
3475 struct elf_link_hash_table *hash_table;
3476 const struct elf_backend_data *bed;
3477 asection *s;
3478 bfd_size_type newsize;
3479 bfd_byte *newcontents;
3480 Elf_Internal_Dyn dyn;
3481
3482 hash_table = elf_hash_table (info);
3483 if (! is_elf_hash_table (hash_table))
3484 return FALSE;
3485
7f923b7f
AM
3486 if (tag == DT_RELA || tag == DT_REL)
3487 hash_table->dynamic_relocs = TRUE;
3488
5a580b3a 3489 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3490 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3491 BFD_ASSERT (s != NULL);
3492
eea6121a 3493 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3494 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3495 if (newcontents == NULL)
3496 return FALSE;
3497
3498 dyn.d_tag = tag;
3499 dyn.d_un.d_val = val;
eea6121a 3500 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3501
eea6121a 3502 s->size = newsize;
5a580b3a
AM
3503 s->contents = newcontents;
3504
3505 return TRUE;
3506}
3507
3508/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3509 otherwise just check whether one already exists. Returns -1 on error,
3510 1 if a DT_NEEDED tag already exists, and 0 on success. */
3511
4ad4eba5 3512static int
7e9f0867
AM
3513elf_add_dt_needed_tag (bfd *abfd,
3514 struct bfd_link_info *info,
4ad4eba5
AM
3515 const char *soname,
3516 bfd_boolean do_it)
5a580b3a
AM
3517{
3518 struct elf_link_hash_table *hash_table;
ef53be89 3519 size_t strindex;
5a580b3a 3520
7e9f0867
AM
3521 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3522 return -1;
3523
5a580b3a 3524 hash_table = elf_hash_table (info);
5a580b3a 3525 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3526 if (strindex == (size_t) -1)
5a580b3a
AM
3527 return -1;
3528
02be4619 3529 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3530 {
3531 asection *sdyn;
3532 const struct elf_backend_data *bed;
3533 bfd_byte *extdyn;
3534
3535 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3536 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3537 if (sdyn != NULL)
3538 for (extdyn = sdyn->contents;
3539 extdyn < sdyn->contents + sdyn->size;
3540 extdyn += bed->s->sizeof_dyn)
3541 {
3542 Elf_Internal_Dyn dyn;
5a580b3a 3543
7e9f0867
AM
3544 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3545 if (dyn.d_tag == DT_NEEDED
3546 && dyn.d_un.d_val == strindex)
3547 {
3548 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3549 return 1;
3550 }
3551 }
5a580b3a
AM
3552 }
3553
3554 if (do_it)
3555 {
7e9f0867
AM
3556 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3557 return -1;
3558
5a580b3a
AM
3559 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3560 return -1;
3561 }
3562 else
3563 /* We were just checking for existence of the tag. */
3564 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3565
3566 return 0;
3567}
3568
7b15fa7a
AM
3569/* Return true if SONAME is on the needed list between NEEDED and STOP
3570 (or the end of list if STOP is NULL), and needed by a library that
3571 will be loaded. */
3572
010e5ae2 3573static bfd_boolean
7b15fa7a
AM
3574on_needed_list (const char *soname,
3575 struct bfd_link_needed_list *needed,
3576 struct bfd_link_needed_list *stop)
010e5ae2 3577{
7b15fa7a
AM
3578 struct bfd_link_needed_list *look;
3579 for (look = needed; look != stop; look = look->next)
3580 if (strcmp (soname, look->name) == 0
3581 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3582 /* If needed by a library that itself is not directly
3583 needed, recursively check whether that library is
3584 indirectly needed. Since we add DT_NEEDED entries to
3585 the end of the list, library dependencies appear after
3586 the library. Therefore search prior to the current
3587 LOOK, preventing possible infinite recursion. */
3588 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3589 return TRUE;
3590
3591 return FALSE;
3592}
3593
14160578 3594/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3595static int
3596elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3597{
3598 const struct elf_link_hash_entry *h1;
3599 const struct elf_link_hash_entry *h2;
10b7e05b 3600 bfd_signed_vma vdiff;
5a580b3a
AM
3601
3602 h1 = *(const struct elf_link_hash_entry **) arg1;
3603 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3604 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3605 if (vdiff != 0)
3606 return vdiff > 0 ? 1 : -1;
3607 else
3608 {
d3435ae8 3609 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3610 if (sdiff != 0)
3611 return sdiff > 0 ? 1 : -1;
3612 }
14160578
AM
3613 vdiff = h1->size - h2->size;
3614 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3615}
4ad4eba5 3616
5a580b3a
AM
3617/* This function is used to adjust offsets into .dynstr for
3618 dynamic symbols. This is called via elf_link_hash_traverse. */
3619
3620static bfd_boolean
3621elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3622{
a50b1753 3623 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3624
5a580b3a
AM
3625 if (h->dynindx != -1)
3626 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3627 return TRUE;
3628}
3629
3630/* Assign string offsets in .dynstr, update all structures referencing
3631 them. */
3632
4ad4eba5
AM
3633static bfd_boolean
3634elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3635{
3636 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3637 struct elf_link_local_dynamic_entry *entry;
3638 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3639 bfd *dynobj = hash_table->dynobj;
3640 asection *sdyn;
3641 bfd_size_type size;
3642 const struct elf_backend_data *bed;
3643 bfd_byte *extdyn;
3644
3645 _bfd_elf_strtab_finalize (dynstr);
3646 size = _bfd_elf_strtab_size (dynstr);
3647
3648 bed = get_elf_backend_data (dynobj);
3d4d4302 3649 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3650 BFD_ASSERT (sdyn != NULL);
3651
3652 /* Update all .dynamic entries referencing .dynstr strings. */
3653 for (extdyn = sdyn->contents;
eea6121a 3654 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3655 extdyn += bed->s->sizeof_dyn)
3656 {
3657 Elf_Internal_Dyn dyn;
3658
3659 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3660 switch (dyn.d_tag)
3661 {
3662 case DT_STRSZ:
3663 dyn.d_un.d_val = size;
3664 break;
3665 case DT_NEEDED:
3666 case DT_SONAME:
3667 case DT_RPATH:
3668 case DT_RUNPATH:
3669 case DT_FILTER:
3670 case DT_AUXILIARY:
7ee314fa
AM
3671 case DT_AUDIT:
3672 case DT_DEPAUDIT:
5a580b3a
AM
3673 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3674 break;
3675 default:
3676 continue;
3677 }
3678 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3679 }
3680
3681 /* Now update local dynamic symbols. */
3682 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3683 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3684 entry->isym.st_name);
3685
3686 /* And the rest of dynamic symbols. */
3687 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3688
3689 /* Adjust version definitions. */
3690 if (elf_tdata (output_bfd)->cverdefs)
3691 {
3692 asection *s;
3693 bfd_byte *p;
ef53be89 3694 size_t i;
5a580b3a
AM
3695 Elf_Internal_Verdef def;
3696 Elf_Internal_Verdaux defaux;
3697
3d4d4302 3698 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3699 p = s->contents;
3700 do
3701 {
3702 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3703 &def);
3704 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3705 if (def.vd_aux != sizeof (Elf_External_Verdef))
3706 continue;
5a580b3a
AM
3707 for (i = 0; i < def.vd_cnt; ++i)
3708 {
3709 _bfd_elf_swap_verdaux_in (output_bfd,
3710 (Elf_External_Verdaux *) p, &defaux);
3711 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3712 defaux.vda_name);
3713 _bfd_elf_swap_verdaux_out (output_bfd,
3714 &defaux, (Elf_External_Verdaux *) p);
3715 p += sizeof (Elf_External_Verdaux);
3716 }
3717 }
3718 while (def.vd_next);
3719 }
3720
3721 /* Adjust version references. */
3722 if (elf_tdata (output_bfd)->verref)
3723 {
3724 asection *s;
3725 bfd_byte *p;
ef53be89 3726 size_t i;
5a580b3a
AM
3727 Elf_Internal_Verneed need;
3728 Elf_Internal_Vernaux needaux;
3729
3d4d4302 3730 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3731 p = s->contents;
3732 do
3733 {
3734 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3735 &need);
3736 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3737 _bfd_elf_swap_verneed_out (output_bfd, &need,
3738 (Elf_External_Verneed *) p);
3739 p += sizeof (Elf_External_Verneed);
3740 for (i = 0; i < need.vn_cnt; ++i)
3741 {
3742 _bfd_elf_swap_vernaux_in (output_bfd,
3743 (Elf_External_Vernaux *) p, &needaux);
3744 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3745 needaux.vna_name);
3746 _bfd_elf_swap_vernaux_out (output_bfd,
3747 &needaux,
3748 (Elf_External_Vernaux *) p);
3749 p += sizeof (Elf_External_Vernaux);
3750 }
3751 }
3752 while (need.vn_next);
3753 }
3754
3755 return TRUE;
3756}
3757\f
13285a1b
AM
3758/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3759 The default is to only match when the INPUT and OUTPUT are exactly
3760 the same target. */
3761
3762bfd_boolean
3763_bfd_elf_default_relocs_compatible (const bfd_target *input,
3764 const bfd_target *output)
3765{
3766 return input == output;
3767}
3768
3769/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3770 This version is used when different targets for the same architecture
3771 are virtually identical. */
3772
3773bfd_boolean
3774_bfd_elf_relocs_compatible (const bfd_target *input,
3775 const bfd_target *output)
3776{
3777 const struct elf_backend_data *obed, *ibed;
3778
3779 if (input == output)
3780 return TRUE;
3781
3782 ibed = xvec_get_elf_backend_data (input);
3783 obed = xvec_get_elf_backend_data (output);
3784
3785 if (ibed->arch != obed->arch)
3786 return FALSE;
3787
3788 /* If both backends are using this function, deem them compatible. */
3789 return ibed->relocs_compatible == obed->relocs_compatible;
3790}
3791
e5034e59
AM
3792/* Make a special call to the linker "notice" function to tell it that
3793 we are about to handle an as-needed lib, or have finished
1b786873 3794 processing the lib. */
e5034e59
AM
3795
3796bfd_boolean
3797_bfd_elf_notice_as_needed (bfd *ibfd,
3798 struct bfd_link_info *info,
3799 enum notice_asneeded_action act)
3800{
46135103 3801 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3802}
3803
d9689752
L
3804/* Check relocations an ELF object file. */
3805
3806bfd_boolean
3807_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3808{
3809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3810 struct elf_link_hash_table *htab = elf_hash_table (info);
3811
3812 /* If this object is the same format as the output object, and it is
3813 not a shared library, then let the backend look through the
3814 relocs.
3815
3816 This is required to build global offset table entries and to
3817 arrange for dynamic relocs. It is not required for the
3818 particular common case of linking non PIC code, even when linking
3819 against shared libraries, but unfortunately there is no way of
3820 knowing whether an object file has been compiled PIC or not.
3821 Looking through the relocs is not particularly time consuming.
3822 The problem is that we must either (1) keep the relocs in memory,
3823 which causes the linker to require additional runtime memory or
3824 (2) read the relocs twice from the input file, which wastes time.
3825 This would be a good case for using mmap.
3826
3827 I have no idea how to handle linking PIC code into a file of a
3828 different format. It probably can't be done. */
3829 if ((abfd->flags & DYNAMIC) == 0
3830 && is_elf_hash_table (htab)
3831 && bed->check_relocs != NULL
3832 && elf_object_id (abfd) == elf_hash_table_id (htab)
3833 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3834 {
3835 asection *o;
3836
3837 for (o = abfd->sections; o != NULL; o = o->next)
3838 {
3839 Elf_Internal_Rela *internal_relocs;
3840 bfd_boolean ok;
3841
5ce03cea 3842 /* Don't check relocations in excluded sections. */
d9689752 3843 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3844 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3845 || o->reloc_count == 0
3846 || ((info->strip == strip_all || info->strip == strip_debugger)
3847 && (o->flags & SEC_DEBUGGING) != 0)
3848 || bfd_is_abs_section (o->output_section))
3849 continue;
3850
3851 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3852 info->keep_memory);
3853 if (internal_relocs == NULL)
3854 return FALSE;
3855
3856 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3857
3858 if (elf_section_data (o)->relocs != internal_relocs)
3859 free (internal_relocs);
3860
3861 if (! ok)
3862 return FALSE;
3863 }
3864 }
3865
3866 return TRUE;
3867}
3868
4ad4eba5
AM
3869/* Add symbols from an ELF object file to the linker hash table. */
3870
3871static bfd_boolean
3872elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3873{
a0c402a5 3874 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3875 Elf_Internal_Shdr *hdr;
ef53be89
AM
3876 size_t symcount;
3877 size_t extsymcount;
3878 size_t extsymoff;
4ad4eba5
AM
3879 struct elf_link_hash_entry **sym_hash;
3880 bfd_boolean dynamic;
3881 Elf_External_Versym *extversym = NULL;
be22c732 3882 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3883 Elf_External_Versym *ever;
3884 struct elf_link_hash_entry *weaks;
3885 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3886 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3887 Elf_Internal_Sym *isymbuf = NULL;
3888 Elf_Internal_Sym *isym;
3889 Elf_Internal_Sym *isymend;
3890 const struct elf_backend_data *bed;
3891 bfd_boolean add_needed;
66eb6687 3892 struct elf_link_hash_table *htab;
4ad4eba5 3893 bfd_size_type amt;
66eb6687 3894 void *alloc_mark = NULL;
4f87808c
AM
3895 struct bfd_hash_entry **old_table = NULL;
3896 unsigned int old_size = 0;
3897 unsigned int old_count = 0;
66eb6687 3898 void *old_tab = NULL;
66eb6687
AM
3899 void *old_ent;
3900 struct bfd_link_hash_entry *old_undefs = NULL;
3901 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3902 void *old_strtab = NULL;
66eb6687 3903 size_t tabsize = 0;
db6a5d5f 3904 asection *s;
29a9f53e 3905 bfd_boolean just_syms;
4ad4eba5 3906
66eb6687 3907 htab = elf_hash_table (info);
4ad4eba5 3908 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3909
3910 if ((abfd->flags & DYNAMIC) == 0)
3911 dynamic = FALSE;
3912 else
3913 {
3914 dynamic = TRUE;
3915
3916 /* You can't use -r against a dynamic object. Also, there's no
3917 hope of using a dynamic object which does not exactly match
3918 the format of the output file. */
0e1862bb 3919 if (bfd_link_relocatable (info)
66eb6687 3920 || !is_elf_hash_table (htab)
f13a99db 3921 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3922 {
0e1862bb 3923 if (bfd_link_relocatable (info))
9a0789ec
NC
3924 bfd_set_error (bfd_error_invalid_operation);
3925 else
3926 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3927 goto error_return;
3928 }
3929 }
3930
a0c402a5
L
3931 ehdr = elf_elfheader (abfd);
3932 if (info->warn_alternate_em
3933 && bed->elf_machine_code != ehdr->e_machine
3934 && ((bed->elf_machine_alt1 != 0
3935 && ehdr->e_machine == bed->elf_machine_alt1)
3936 || (bed->elf_machine_alt2 != 0
3937 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3938 _bfd_error_handler
695344c0 3939 /* xgettext:c-format */
9793eb77 3940 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3941 ehdr->e_machine, abfd, bed->elf_machine_code);
3942
4ad4eba5
AM
3943 /* As a GNU extension, any input sections which are named
3944 .gnu.warning.SYMBOL are treated as warning symbols for the given
3945 symbol. This differs from .gnu.warning sections, which generate
3946 warnings when they are included in an output file. */
dd98f8d2 3947 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3948 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3949 {
db6a5d5f 3950 const char *name;
4ad4eba5 3951
db6a5d5f
AM
3952 name = bfd_get_section_name (abfd, s);
3953 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3954 {
db6a5d5f
AM
3955 char *msg;
3956 bfd_size_type sz;
3957
3958 name += sizeof ".gnu.warning." - 1;
3959
3960 /* If this is a shared object, then look up the symbol
3961 in the hash table. If it is there, and it is already
3962 been defined, then we will not be using the entry
3963 from this shared object, so we don't need to warn.
3964 FIXME: If we see the definition in a regular object
3965 later on, we will warn, but we shouldn't. The only
3966 fix is to keep track of what warnings we are supposed
3967 to emit, and then handle them all at the end of the
3968 link. */
3969 if (dynamic)
4ad4eba5 3970 {
db6a5d5f
AM
3971 struct elf_link_hash_entry *h;
3972
3973 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3974
3975 /* FIXME: What about bfd_link_hash_common? */
3976 if (h != NULL
3977 && (h->root.type == bfd_link_hash_defined
3978 || h->root.type == bfd_link_hash_defweak))
3979 continue;
3980 }
4ad4eba5 3981
db6a5d5f
AM
3982 sz = s->size;
3983 msg = (char *) bfd_alloc (abfd, sz + 1);
3984 if (msg == NULL)
3985 goto error_return;
4ad4eba5 3986
db6a5d5f
AM
3987 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3988 goto error_return;
4ad4eba5 3989
db6a5d5f 3990 msg[sz] = '\0';
4ad4eba5 3991
db6a5d5f
AM
3992 if (! (_bfd_generic_link_add_one_symbol
3993 (info, abfd, name, BSF_WARNING, s, 0, msg,
3994 FALSE, bed->collect, NULL)))
3995 goto error_return;
4ad4eba5 3996
0e1862bb 3997 if (bfd_link_executable (info))
db6a5d5f
AM
3998 {
3999 /* Clobber the section size so that the warning does
4000 not get copied into the output file. */
4001 s->size = 0;
11d2f718 4002
db6a5d5f
AM
4003 /* Also set SEC_EXCLUDE, so that symbols defined in
4004 the warning section don't get copied to the output. */
4005 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4006 }
4007 }
4008 }
4009
29a9f53e
L
4010 just_syms = ((s = abfd->sections) != NULL
4011 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4012
4ad4eba5
AM
4013 add_needed = TRUE;
4014 if (! dynamic)
4015 {
4016 /* If we are creating a shared library, create all the dynamic
4017 sections immediately. We need to attach them to something,
4018 so we attach them to this BFD, provided it is the right
bf89386a
L
4019 format and is not from ld --just-symbols. Always create the
4020 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4021 are no input BFD's of the same format as the output, we can't
4022 make a shared library. */
4023 if (!just_syms
bf89386a 4024 && (bfd_link_pic (info)
9c1d7a08 4025 || (!bfd_link_relocatable (info)
3c5fce9b 4026 && info->nointerp
9c1d7a08 4027 && (info->export_dynamic || info->dynamic)))
66eb6687 4028 && is_elf_hash_table (htab)
f13a99db 4029 && info->output_bfd->xvec == abfd->xvec
66eb6687 4030 && !htab->dynamic_sections_created)
4ad4eba5
AM
4031 {
4032 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4033 goto error_return;
4034 }
4035 }
66eb6687 4036 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4037 goto error_return;
4038 else
4039 {
4ad4eba5 4040 const char *soname = NULL;
7ee314fa 4041 char *audit = NULL;
4ad4eba5 4042 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4043 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4044 int ret;
4045
4046 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4047 ld shouldn't allow it. */
29a9f53e 4048 if (just_syms)
92fd189d 4049 abort ();
4ad4eba5
AM
4050
4051 /* If this dynamic lib was specified on the command line with
4052 --as-needed in effect, then we don't want to add a DT_NEEDED
4053 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4054 in by another lib's DT_NEEDED. When --no-add-needed is used
4055 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4056 any dynamic library in DT_NEEDED tags in the dynamic lib at
4057 all. */
4058 add_needed = (elf_dyn_lib_class (abfd)
4059 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4060 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4061
4062 s = bfd_get_section_by_name (abfd, ".dynamic");
4063 if (s != NULL)
4064 {
4065 bfd_byte *dynbuf;
4066 bfd_byte *extdyn;
cb33740c 4067 unsigned int elfsec;
4ad4eba5
AM
4068 unsigned long shlink;
4069
eea6121a 4070 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4071 {
4072error_free_dyn:
4073 free (dynbuf);
4074 goto error_return;
4075 }
4ad4eba5
AM
4076
4077 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4078 if (elfsec == SHN_BAD)
4ad4eba5
AM
4079 goto error_free_dyn;
4080 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4081
4082 for (extdyn = dynbuf;
9bff840e 4083 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4084 extdyn += bed->s->sizeof_dyn)
4085 {
4086 Elf_Internal_Dyn dyn;
4087
4088 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4089 if (dyn.d_tag == DT_SONAME)
4090 {
4091 unsigned int tagv = dyn.d_un.d_val;
4092 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4093 if (soname == NULL)
4094 goto error_free_dyn;
4095 }
4096 if (dyn.d_tag == DT_NEEDED)
4097 {
4098 struct bfd_link_needed_list *n, **pn;
4099 char *fnm, *anm;
4100 unsigned int tagv = dyn.d_un.d_val;
4101
4102 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4103 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4104 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4105 if (n == NULL || fnm == NULL)
4106 goto error_free_dyn;
4107 amt = strlen (fnm) + 1;
a50b1753 4108 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4109 if (anm == NULL)
4110 goto error_free_dyn;
4111 memcpy (anm, fnm, amt);
4112 n->name = anm;
4113 n->by = abfd;
4114 n->next = NULL;
66eb6687 4115 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4116 ;
4117 *pn = n;
4118 }
4119 if (dyn.d_tag == DT_RUNPATH)
4120 {
4121 struct bfd_link_needed_list *n, **pn;
4122 char *fnm, *anm;
4123 unsigned int tagv = dyn.d_un.d_val;
4124
4125 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4126 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4127 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4128 if (n == NULL || fnm == NULL)
4129 goto error_free_dyn;
4130 amt = strlen (fnm) + 1;
a50b1753 4131 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4132 if (anm == NULL)
4133 goto error_free_dyn;
4134 memcpy (anm, fnm, amt);
4135 n->name = anm;
4136 n->by = abfd;
4137 n->next = NULL;
4138 for (pn = & runpath;
4139 *pn != NULL;
4140 pn = &(*pn)->next)
4141 ;
4142 *pn = n;
4143 }
4144 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4145 if (!runpath && dyn.d_tag == DT_RPATH)
4146 {
4147 struct bfd_link_needed_list *n, **pn;
4148 char *fnm, *anm;
4149 unsigned int tagv = dyn.d_un.d_val;
4150
4151 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4152 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4153 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4154 if (n == NULL || fnm == NULL)
4155 goto error_free_dyn;
4156 amt = strlen (fnm) + 1;
a50b1753 4157 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4158 if (anm == NULL)
f8703194 4159 goto error_free_dyn;
4ad4eba5
AM
4160 memcpy (anm, fnm, amt);
4161 n->name = anm;
4162 n->by = abfd;
4163 n->next = NULL;
4164 for (pn = & rpath;
4165 *pn != NULL;
4166 pn = &(*pn)->next)
4167 ;
4168 *pn = n;
4169 }
7ee314fa
AM
4170 if (dyn.d_tag == DT_AUDIT)
4171 {
4172 unsigned int tagv = dyn.d_un.d_val;
4173 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4174 }
4ad4eba5
AM
4175 }
4176
4177 free (dynbuf);
4178 }
4179
4180 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4181 frees all more recently bfd_alloc'd blocks as well. */
4182 if (runpath)
4183 rpath = runpath;
4184
4185 if (rpath)
4186 {
4187 struct bfd_link_needed_list **pn;
66eb6687 4188 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4189 ;
4190 *pn = rpath;
4191 }
4192
9acc85a6
AM
4193 /* If we have a PT_GNU_RELRO program header, mark as read-only
4194 all sections contained fully therein. This makes relro
4195 shared library sections appear as they will at run-time. */
4196 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4197 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4198 if (phdr->p_type == PT_GNU_RELRO)
4199 {
4200 for (s = abfd->sections; s != NULL; s = s->next)
4201 if ((s->flags & SEC_ALLOC) != 0
4202 && s->vma >= phdr->p_vaddr
4203 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4204 s->flags |= SEC_READONLY;
4205 break;
4206 }
4207
4ad4eba5
AM
4208 /* We do not want to include any of the sections in a dynamic
4209 object in the output file. We hack by simply clobbering the
4210 list of sections in the BFD. This could be handled more
4211 cleanly by, say, a new section flag; the existing
4212 SEC_NEVER_LOAD flag is not the one we want, because that one
4213 still implies that the section takes up space in the output
4214 file. */
4215 bfd_section_list_clear (abfd);
4216
4ad4eba5
AM
4217 /* Find the name to use in a DT_NEEDED entry that refers to this
4218 object. If the object has a DT_SONAME entry, we use it.
4219 Otherwise, if the generic linker stuck something in
4220 elf_dt_name, we use that. Otherwise, we just use the file
4221 name. */
4222 if (soname == NULL || *soname == '\0')
4223 {
4224 soname = elf_dt_name (abfd);
4225 if (soname == NULL || *soname == '\0')
4226 soname = bfd_get_filename (abfd);
4227 }
4228
4229 /* Save the SONAME because sometimes the linker emulation code
4230 will need to know it. */
4231 elf_dt_name (abfd) = soname;
4232
7e9f0867 4233 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4234 if (ret < 0)
4235 goto error_return;
4236
4237 /* If we have already included this dynamic object in the
4238 link, just ignore it. There is no reason to include a
4239 particular dynamic object more than once. */
4240 if (ret > 0)
4241 return TRUE;
7ee314fa
AM
4242
4243 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4244 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4245 }
4246
4247 /* If this is a dynamic object, we always link against the .dynsym
4248 symbol table, not the .symtab symbol table. The dynamic linker
4249 will only see the .dynsym symbol table, so there is no reason to
4250 look at .symtab for a dynamic object. */
4251
4252 if (! dynamic || elf_dynsymtab (abfd) == 0)
4253 hdr = &elf_tdata (abfd)->symtab_hdr;
4254 else
4255 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4256
4257 symcount = hdr->sh_size / bed->s->sizeof_sym;
4258
4259 /* The sh_info field of the symtab header tells us where the
4260 external symbols start. We don't care about the local symbols at
4261 this point. */
4262 if (elf_bad_symtab (abfd))
4263 {
4264 extsymcount = symcount;
4265 extsymoff = 0;
4266 }
4267 else
4268 {
4269 extsymcount = symcount - hdr->sh_info;
4270 extsymoff = hdr->sh_info;
4271 }
4272
f45794cb 4273 sym_hash = elf_sym_hashes (abfd);
012b2306 4274 if (extsymcount != 0)
4ad4eba5
AM
4275 {
4276 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4277 NULL, NULL, NULL);
4278 if (isymbuf == NULL)
4279 goto error_return;
4280
4ad4eba5 4281 if (sym_hash == NULL)
012b2306
AM
4282 {
4283 /* We store a pointer to the hash table entry for each
4284 external symbol. */
ef53be89
AM
4285 amt = extsymcount;
4286 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4287 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4288 if (sym_hash == NULL)
4289 goto error_free_sym;
4290 elf_sym_hashes (abfd) = sym_hash;
4291 }
4ad4eba5
AM
4292 }
4293
4294 if (dynamic)
4295 {
4296 /* Read in any version definitions. */
fc0e6df6
PB
4297 if (!_bfd_elf_slurp_version_tables (abfd,
4298 info->default_imported_symver))
4ad4eba5
AM
4299 goto error_free_sym;
4300
4301 /* Read in the symbol versions, but don't bother to convert them
4302 to internal format. */
4303 if (elf_dynversym (abfd) != 0)
4304 {
4305 Elf_Internal_Shdr *versymhdr;
4306
4307 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
be22c732
NC
4308 amt = versymhdr->sh_size;
4309 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4ad4eba5
AM
4310 if (extversym == NULL)
4311 goto error_free_sym;
4ad4eba5
AM
4312 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4313 || bfd_bread (extversym, amt, abfd) != amt)
4314 goto error_free_vers;
be22c732 4315 extversym_end = extversym + (amt / sizeof (* extversym));
4ad4eba5
AM
4316 }
4317 }
4318
66eb6687
AM
4319 /* If we are loading an as-needed shared lib, save the symbol table
4320 state before we start adding symbols. If the lib turns out
4321 to be unneeded, restore the state. */
4322 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4323 {
4324 unsigned int i;
4325 size_t entsize;
4326
4327 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4328 {
4329 struct bfd_hash_entry *p;
2de92251 4330 struct elf_link_hash_entry *h;
66eb6687
AM
4331
4332 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4333 {
4334 h = (struct elf_link_hash_entry *) p;
4335 entsize += htab->root.table.entsize;
4336 if (h->root.type == bfd_link_hash_warning)
4337 entsize += htab->root.table.entsize;
4338 }
66eb6687
AM
4339 }
4340
4341 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4342 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4343 if (old_tab == NULL)
4344 goto error_free_vers;
4345
4346 /* Remember the current objalloc pointer, so that all mem for
4347 symbols added can later be reclaimed. */
4348 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4349 if (alloc_mark == NULL)
4350 goto error_free_vers;
4351
5061a885
AM
4352 /* Make a special call to the linker "notice" function to
4353 tell it that we are about to handle an as-needed lib. */
e5034e59 4354 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4355 goto error_free_vers;
5061a885 4356
f45794cb
AM
4357 /* Clone the symbol table. Remember some pointers into the
4358 symbol table, and dynamic symbol count. */
4359 old_ent = (char *) old_tab + tabsize;
66eb6687 4360 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4361 old_undefs = htab->root.undefs;
4362 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4363 old_table = htab->root.table.table;
4364 old_size = htab->root.table.size;
4365 old_count = htab->root.table.count;
5b677558
AM
4366 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4367 if (old_strtab == NULL)
4368 goto error_free_vers;
66eb6687
AM
4369
4370 for (i = 0; i < htab->root.table.size; i++)
4371 {
4372 struct bfd_hash_entry *p;
2de92251 4373 struct elf_link_hash_entry *h;
66eb6687
AM
4374
4375 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4376 {
4377 memcpy (old_ent, p, htab->root.table.entsize);
4378 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4379 h = (struct elf_link_hash_entry *) p;
4380 if (h->root.type == bfd_link_hash_warning)
4381 {
4382 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4383 old_ent = (char *) old_ent + htab->root.table.entsize;
4384 }
66eb6687
AM
4385 }
4386 }
4387 }
4ad4eba5 4388
66eb6687 4389 weaks = NULL;
be22c732
NC
4390 if (extversym == NULL)
4391 ever = NULL;
4392 else if (extversym + extsymoff < extversym_end)
4393 ever = extversym + extsymoff;
4394 else
4395 {
4396 /* xgettext:c-format */
4397 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4398 abfd, (long) extsymoff,
4399 (long) (extversym_end - extversym) / sizeof (* extversym));
4400 bfd_set_error (bfd_error_bad_value);
4401 goto error_free_vers;
4402 }
4403
4ad4eba5
AM
4404 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4405 isym < isymend;
4406 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4407 {
4408 int bind;
4409 bfd_vma value;
af44c138 4410 asection *sec, *new_sec;
4ad4eba5
AM
4411 flagword flags;
4412 const char *name;
4413 struct elf_link_hash_entry *h;
90c984fc 4414 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4415 bfd_boolean definition;
4416 bfd_boolean size_change_ok;
4417 bfd_boolean type_change_ok;
37a9e49a
L
4418 bfd_boolean new_weak;
4419 bfd_boolean old_weak;
4ad4eba5 4420 bfd_boolean override;
a4d8e49b 4421 bfd_boolean common;
97196564 4422 bfd_boolean discarded;
4ad4eba5
AM
4423 unsigned int old_alignment;
4424 bfd *old_bfd;
6e33951e 4425 bfd_boolean matched;
4ad4eba5
AM
4426
4427 override = FALSE;
4428
4429 flags = BSF_NO_FLAGS;
4430 sec = NULL;
4431 value = isym->st_value;
a4d8e49b 4432 common = bed->common_definition (isym);
2980ccad
L
4433 if (common && info->inhibit_common_definition)
4434 {
4435 /* Treat common symbol as undefined for --no-define-common. */
4436 isym->st_shndx = SHN_UNDEF;
4437 common = FALSE;
4438 }
97196564 4439 discarded = FALSE;
4ad4eba5
AM
4440
4441 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4442 switch (bind)
4ad4eba5 4443 {
3e7a7d11 4444 case STB_LOCAL:
4ad4eba5
AM
4445 /* This should be impossible, since ELF requires that all
4446 global symbols follow all local symbols, and that sh_info
4447 point to the first global symbol. Unfortunately, Irix 5
4448 screws this up. */
fe3fef62
AM
4449 if (elf_bad_symtab (abfd))
4450 continue;
4451
4452 /* If we aren't prepared to handle locals within the globals
6835821b 4453 then we'll likely segfault on a NULL section. */
fe3fef62
AM
4454 bfd_set_error (bfd_error_bad_value);
4455 goto error_free_vers;
3e7a7d11
NC
4456
4457 case STB_GLOBAL:
a4d8e49b 4458 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4459 flags = BSF_GLOBAL;
3e7a7d11
NC
4460 break;
4461
4462 case STB_WEAK:
4463 flags = BSF_WEAK;
4464 break;
4465
4466 case STB_GNU_UNIQUE:
4467 flags = BSF_GNU_UNIQUE;
4468 break;
4469
4470 default:
4ad4eba5 4471 /* Leave it up to the processor backend. */
3e7a7d11 4472 break;
4ad4eba5
AM
4473 }
4474
4475 if (isym->st_shndx == SHN_UNDEF)
4476 sec = bfd_und_section_ptr;
cb33740c
AM
4477 else if (isym->st_shndx == SHN_ABS)
4478 sec = bfd_abs_section_ptr;
4479 else if (isym->st_shndx == SHN_COMMON)
4480 {
4481 sec = bfd_com_section_ptr;
4482 /* What ELF calls the size we call the value. What ELF
4483 calls the value we call the alignment. */
4484 value = isym->st_size;
4485 }
4486 else
4ad4eba5
AM
4487 {
4488 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4489 if (sec == NULL)
4490 sec = bfd_abs_section_ptr;
dbaa2011 4491 else if (discarded_section (sec))
529fcb95 4492 {
e5d08002
L
4493 /* Symbols from discarded section are undefined. We keep
4494 its visibility. */
529fcb95 4495 sec = bfd_und_section_ptr;
97196564 4496 discarded = TRUE;
529fcb95
PB
4497 isym->st_shndx = SHN_UNDEF;
4498 }
4ad4eba5
AM
4499 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4500 value -= sec->vma;
4501 }
4ad4eba5
AM
4502
4503 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4504 isym->st_name);
4505 if (name == NULL)
4506 goto error_free_vers;
4507
4508 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4509 && (abfd->flags & BFD_PLUGIN) != 0)
4510 {
4511 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4512
4513 if (xc == NULL)
4514 {
4515 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4516 | SEC_EXCLUDE);
4517 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4518 if (xc == NULL)
4519 goto error_free_vers;
4520 }
4521 sec = xc;
4522 }
4523 else if (isym->st_shndx == SHN_COMMON
4524 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4525 && !bfd_link_relocatable (info))
4ad4eba5
AM
4526 {
4527 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4528
4529 if (tcomm == NULL)
4530 {
02d00247
AM
4531 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4532 | SEC_LINKER_CREATED);
4533 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4534 if (tcomm == NULL)
4ad4eba5
AM
4535 goto error_free_vers;
4536 }
4537 sec = tcomm;
4538 }
66eb6687 4539 else if (bed->elf_add_symbol_hook)
4ad4eba5 4540 {
66eb6687
AM
4541 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4542 &sec, &value))
4ad4eba5
AM
4543 goto error_free_vers;
4544
4545 /* The hook function sets the name to NULL if this symbol
4546 should be skipped for some reason. */
4547 if (name == NULL)
4548 continue;
4549 }
4550
4551 /* Sanity check that all possibilities were handled. */
4552 if (sec == NULL)
4553 {
4554 bfd_set_error (bfd_error_bad_value);
4555 goto error_free_vers;
4556 }
4557
191c0c42
AM
4558 /* Silently discard TLS symbols from --just-syms. There's
4559 no way to combine a static TLS block with a new TLS block
4560 for this executable. */
4561 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4562 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4563 continue;
4564
4ad4eba5
AM
4565 if (bfd_is_und_section (sec)
4566 || bfd_is_com_section (sec))
4567 definition = FALSE;
4568 else
4569 definition = TRUE;
4570
4571 size_change_ok = FALSE;
66eb6687 4572 type_change_ok = bed->type_change_ok;
37a9e49a 4573 old_weak = FALSE;
6e33951e 4574 matched = FALSE;
4ad4eba5
AM
4575 old_alignment = 0;
4576 old_bfd = NULL;
af44c138 4577 new_sec = sec;
4ad4eba5 4578
66eb6687 4579 if (is_elf_hash_table (htab))
4ad4eba5
AM
4580 {
4581 Elf_Internal_Versym iver;
4582 unsigned int vernum = 0;
4583 bfd_boolean skip;
4584
fc0e6df6 4585 if (ever == NULL)
4ad4eba5 4586 {
fc0e6df6
PB
4587 if (info->default_imported_symver)
4588 /* Use the default symbol version created earlier. */
4589 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4590 else
4591 iver.vs_vers = 0;
4592 }
be22c732
NC
4593 else if (ever >= extversym_end)
4594 {
4595 /* xgettext:c-format */
4596 _bfd_error_handler (_("%pB: not enough version information"),
4597 abfd);
4598 bfd_set_error (bfd_error_bad_value);
4599 goto error_free_vers;
4600 }
fc0e6df6
PB
4601 else
4602 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4603
4604 vernum = iver.vs_vers & VERSYM_VERSION;
4605
4606 /* If this is a hidden symbol, or if it is not version
4607 1, we append the version name to the symbol name.
cc86ff91
EB
4608 However, we do not modify a non-hidden absolute symbol
4609 if it is not a function, because it might be the version
4610 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4611 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4612 || (vernum > 1
4613 && (!bfd_is_abs_section (sec)
4614 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4615 {
4616 const char *verstr;
4617 size_t namelen, verlen, newlen;
4618 char *newname, *p;
4619
4620 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4621 {
fc0e6df6
PB
4622 if (vernum > elf_tdata (abfd)->cverdefs)
4623 verstr = NULL;
4624 else if (vernum > 1)
4625 verstr =
4626 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4627 else
4628 verstr = "";
4ad4eba5 4629
fc0e6df6 4630 if (verstr == NULL)
4ad4eba5 4631 {
4eca0228 4632 _bfd_error_handler
695344c0 4633 /* xgettext:c-format */
871b3ab2 4634 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4635 abfd, name, vernum,
4636 elf_tdata (abfd)->cverdefs);
4637 bfd_set_error (bfd_error_bad_value);
4638 goto error_free_vers;
4ad4eba5 4639 }
fc0e6df6
PB
4640 }
4641 else
4642 {
4643 /* We cannot simply test for the number of
4644 entries in the VERNEED section since the
4645 numbers for the needed versions do not start
4646 at 0. */
4647 Elf_Internal_Verneed *t;
4648
4649 verstr = NULL;
4650 for (t = elf_tdata (abfd)->verref;
4651 t != NULL;
4652 t = t->vn_nextref)
4ad4eba5 4653 {
fc0e6df6 4654 Elf_Internal_Vernaux *a;
4ad4eba5 4655
fc0e6df6
PB
4656 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4657 {
4658 if (a->vna_other == vernum)
4ad4eba5 4659 {
fc0e6df6
PB
4660 verstr = a->vna_nodename;
4661 break;
4ad4eba5 4662 }
4ad4eba5 4663 }
fc0e6df6
PB
4664 if (a != NULL)
4665 break;
4666 }
4667 if (verstr == NULL)
4668 {
4eca0228 4669 _bfd_error_handler
695344c0 4670 /* xgettext:c-format */
871b3ab2 4671 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4672 abfd, name, vernum);
4673 bfd_set_error (bfd_error_bad_value);
4674 goto error_free_vers;
4ad4eba5 4675 }
4ad4eba5 4676 }
fc0e6df6
PB
4677
4678 namelen = strlen (name);
4679 verlen = strlen (verstr);
4680 newlen = namelen + verlen + 2;
4681 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4682 && isym->st_shndx != SHN_UNDEF)
4683 ++newlen;
4684
a50b1753 4685 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4686 if (newname == NULL)
4687 goto error_free_vers;
4688 memcpy (newname, name, namelen);
4689 p = newname + namelen;
4690 *p++ = ELF_VER_CHR;
4691 /* If this is a defined non-hidden version symbol,
4692 we add another @ to the name. This indicates the
4693 default version of the symbol. */
4694 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4695 && isym->st_shndx != SHN_UNDEF)
4696 *p++ = ELF_VER_CHR;
4697 memcpy (p, verstr, verlen + 1);
4698
4699 name = newname;
4ad4eba5
AM
4700 }
4701
cd3416da
AM
4702 /* If this symbol has default visibility and the user has
4703 requested we not re-export it, then mark it as hidden. */
a0d49154 4704 if (!bfd_is_und_section (sec)
cd3416da 4705 && !dynamic
ce875075 4706 && abfd->no_export
cd3416da
AM
4707 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4708 isym->st_other = (STV_HIDDEN
4709 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4710
4f3fedcf
AM
4711 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4712 sym_hash, &old_bfd, &old_weak,
4713 &old_alignment, &skip, &override,
6e33951e
L
4714 &type_change_ok, &size_change_ok,
4715 &matched))
4ad4eba5
AM
4716 goto error_free_vers;
4717
4718 if (skip)
4719 continue;
4720
6e33951e
L
4721 /* Override a definition only if the new symbol matches the
4722 existing one. */
4723 if (override && matched)
4ad4eba5
AM
4724 definition = FALSE;
4725
4726 h = *sym_hash;
4727 while (h->root.type == bfd_link_hash_indirect
4728 || h->root.type == bfd_link_hash_warning)
4729 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4730
4ad4eba5 4731 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4732 && vernum > 1
4733 && definition)
4734 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4735 }
4736
4737 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4738 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4739 (struct bfd_link_hash_entry **) sym_hash)))
4740 goto error_free_vers;
4741
4742 h = *sym_hash;
90c984fc
L
4743 /* We need to make sure that indirect symbol dynamic flags are
4744 updated. */
4745 hi = h;
4ad4eba5
AM
4746 while (h->root.type == bfd_link_hash_indirect
4747 || h->root.type == bfd_link_hash_warning)
4748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4749
97196564
L
4750 /* Setting the index to -3 tells elf_link_output_extsym that
4751 this symbol is defined in a discarded section. */
4752 if (discarded)
4753 h->indx = -3;
4754
4ad4eba5
AM
4755 *sym_hash = h;
4756
37a9e49a 4757 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4758 if (dynamic
4759 && definition
37a9e49a 4760 && new_weak
fcb93ecf 4761 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4762 && is_elf_hash_table (htab)
60d67dc8 4763 && h->u.alias == NULL)
4ad4eba5
AM
4764 {
4765 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4766 a dynamic object, using the alias field. Later in this
4767 function we will set the alias field to the correct
4ad4eba5
AM
4768 value. We only put non-function symbols from dynamic
4769 objects on this list, because that happens to be the only
4770 time we need to know the normal symbol corresponding to a
4771 weak symbol, and the information is time consuming to
60d67dc8 4772 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4773 then this symbol was already defined by some previous
4774 dynamic object, and we will be using that previous
4775 definition anyhow. */
4776
60d67dc8 4777 h->u.alias = weaks;
4ad4eba5 4778 weaks = h;
4ad4eba5
AM
4779 }
4780
4781 /* Set the alignment of a common symbol. */
a4d8e49b 4782 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4783 && h->root.type == bfd_link_hash_common)
4784 {
4785 unsigned int align;
4786
a4d8e49b 4787 if (common)
af44c138
L
4788 align = bfd_log2 (isym->st_value);
4789 else
4790 {
4791 /* The new symbol is a common symbol in a shared object.
4792 We need to get the alignment from the section. */
4793 align = new_sec->alignment_power;
4794 }
595213d4 4795 if (align > old_alignment)
4ad4eba5
AM
4796 h->root.u.c.p->alignment_power = align;
4797 else
4798 h->root.u.c.p->alignment_power = old_alignment;
4799 }
4800
66eb6687 4801 if (is_elf_hash_table (htab))
4ad4eba5 4802 {
4f3fedcf
AM
4803 /* Set a flag in the hash table entry indicating the type of
4804 reference or definition we just found. A dynamic symbol
4805 is one which is referenced or defined by both a regular
4806 object and a shared object. */
4807 bfd_boolean dynsym = FALSE;
4808
4809 /* Plugin symbols aren't normal. Don't set def_regular or
4810 ref_regular for them, or make them dynamic. */
4811 if ((abfd->flags & BFD_PLUGIN) != 0)
4812 ;
4813 else if (! dynamic)
4814 {
4815 if (! definition)
4816 {
4817 h->ref_regular = 1;
4818 if (bind != STB_WEAK)
4819 h->ref_regular_nonweak = 1;
4820 }
4821 else
4822 {
4823 h->def_regular = 1;
4824 if (h->def_dynamic)
4825 {
4826 h->def_dynamic = 0;
4827 h->ref_dynamic = 1;
4828 }
4829 }
4830
4831 /* If the indirect symbol has been forced local, don't
4832 make the real symbol dynamic. */
4833 if ((h == hi || !hi->forced_local)
0e1862bb 4834 && (bfd_link_dll (info)
4f3fedcf
AM
4835 || h->def_dynamic
4836 || h->ref_dynamic))
4837 dynsym = TRUE;
4838 }
4839 else
4840 {
4841 if (! definition)
4842 {
4843 h->ref_dynamic = 1;
4844 hi->ref_dynamic = 1;
4845 }
4846 else
4847 {
4848 h->def_dynamic = 1;
4849 hi->def_dynamic = 1;
4850 }
4851
4852 /* If the indirect symbol has been forced local, don't
4853 make the real symbol dynamic. */
4854 if ((h == hi || !hi->forced_local)
4855 && (h->def_regular
4856 || h->ref_regular
60d67dc8
AM
4857 || (h->is_weakalias
4858 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4859 dynsym = TRUE;
4860 }
4861
4862 /* Check to see if we need to add an indirect symbol for
4863 the default name. */
4864 if (definition
4865 || (!override && h->root.type == bfd_link_hash_common))
4866 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4867 sec, value, &old_bfd, &dynsym))
4868 goto error_free_vers;
4ad4eba5
AM
4869
4870 /* Check the alignment when a common symbol is involved. This
4871 can change when a common symbol is overridden by a normal
4872 definition or a common symbol is ignored due to the old
4873 normal definition. We need to make sure the maximum
4874 alignment is maintained. */
a4d8e49b 4875 if ((old_alignment || common)
4ad4eba5
AM
4876 && h->root.type != bfd_link_hash_common)
4877 {
4878 unsigned int common_align;
4879 unsigned int normal_align;
4880 unsigned int symbol_align;
4881 bfd *normal_bfd;
4882 bfd *common_bfd;
4883
3a81e825
AM
4884 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4885 || h->root.type == bfd_link_hash_defweak);
4886
4ad4eba5
AM
4887 symbol_align = ffs (h->root.u.def.value) - 1;
4888 if (h->root.u.def.section->owner != NULL
0616a280
AM
4889 && (h->root.u.def.section->owner->flags
4890 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4891 {
4892 normal_align = h->root.u.def.section->alignment_power;
4893 if (normal_align > symbol_align)
4894 normal_align = symbol_align;
4895 }
4896 else
4897 normal_align = symbol_align;
4898
4899 if (old_alignment)
4900 {
4901 common_align = old_alignment;
4902 common_bfd = old_bfd;
4903 normal_bfd = abfd;
4904 }
4905 else
4906 {
4907 common_align = bfd_log2 (isym->st_value);
4908 common_bfd = abfd;
4909 normal_bfd = old_bfd;
4910 }
4911
4912 if (normal_align < common_align)
d07676f8
NC
4913 {
4914 /* PR binutils/2735 */
4915 if (normal_bfd == NULL)
4eca0228 4916 _bfd_error_handler
695344c0 4917 /* xgettext:c-format */
9793eb77 4918 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4919 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4920 1 << common_align, name, common_bfd,
4921 1 << normal_align, h->root.u.def.section);
d07676f8 4922 else
4eca0228 4923 _bfd_error_handler
695344c0 4924 /* xgettext:c-format */
9793eb77 4925 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4926 " is smaller than %u in %pB"),
c08bb8dd
AM
4927 1 << normal_align, name, normal_bfd,
4928 1 << common_align, common_bfd);
d07676f8 4929 }
4ad4eba5
AM
4930 }
4931
83ad0046 4932 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4933 if (isym->st_size != 0
4934 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4935 && (definition || h->size == 0))
4936 {
83ad0046
L
4937 if (h->size != 0
4938 && h->size != isym->st_size
4939 && ! size_change_ok)
4eca0228 4940 _bfd_error_handler
695344c0 4941 /* xgettext:c-format */
9793eb77 4942 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4943 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4944 name, (uint64_t) h->size, old_bfd,
4945 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4946
4947 h->size = isym->st_size;
4948 }
4949
4950 /* If this is a common symbol, then we always want H->SIZE
4951 to be the size of the common symbol. The code just above
4952 won't fix the size if a common symbol becomes larger. We
4953 don't warn about a size change here, because that is
4f3fedcf 4954 covered by --warn-common. Allow changes between different
fcb93ecf 4955 function types. */
4ad4eba5
AM
4956 if (h->root.type == bfd_link_hash_common)
4957 h->size = h->root.u.c.size;
4958
4959 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4960 && ((definition && !new_weak)
4961 || (old_weak && h->root.type == bfd_link_hash_common)
4962 || h->type == STT_NOTYPE))
4ad4eba5 4963 {
2955ec4c
L
4964 unsigned int type = ELF_ST_TYPE (isym->st_info);
4965
4966 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4967 symbol. */
4968 if (type == STT_GNU_IFUNC
4969 && (abfd->flags & DYNAMIC) != 0)
4970 type = STT_FUNC;
4ad4eba5 4971
2955ec4c
L
4972 if (h->type != type)
4973 {
4974 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4975 /* xgettext:c-format */
4eca0228 4976 _bfd_error_handler
9793eb77 4977 (_("warning: type of symbol `%s' changed"
871b3ab2 4978 " from %d to %d in %pB"),
c08bb8dd 4979 name, h->type, type, abfd);
2955ec4c
L
4980
4981 h->type = type;
4982 }
4ad4eba5
AM
4983 }
4984
54ac0771 4985 /* Merge st_other field. */
b8417128 4986 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4987
c3df8c14 4988 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4989 if (definition
4990 && (sec->flags & SEC_DEBUGGING)
4991 && !bfd_link_relocatable (info))
c3df8c14
AM
4992 dynsym = FALSE;
4993
4f3fedcf
AM
4994 /* Nor should we make plugin symbols dynamic. */
4995 if ((abfd->flags & BFD_PLUGIN) != 0)
4996 dynsym = FALSE;
4997
35fc36a8 4998 if (definition)
35399224
L
4999 {
5000 h->target_internal = isym->st_target_internal;
5001 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5002 }
35fc36a8 5003
4ad4eba5
AM
5004 if (definition && !dynamic)
5005 {
5006 char *p = strchr (name, ELF_VER_CHR);
5007 if (p != NULL && p[1] != ELF_VER_CHR)
5008 {
5009 /* Queue non-default versions so that .symver x, x@FOO
5010 aliases can be checked. */
66eb6687 5011 if (!nondeflt_vers)
4ad4eba5 5012 {
66eb6687
AM
5013 amt = ((isymend - isym + 1)
5014 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5015 nondeflt_vers
5016 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5017 if (!nondeflt_vers)
5018 goto error_free_vers;
4ad4eba5 5019 }
66eb6687 5020 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5021 }
5022 }
5023
5024 if (dynsym && h->dynindx == -1)
5025 {
c152c796 5026 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5027 goto error_free_vers;
60d67dc8
AM
5028 if (h->is_weakalias
5029 && weakdef (h)->dynindx == -1)
4ad4eba5 5030 {
60d67dc8 5031 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5032 goto error_free_vers;
5033 }
5034 }
1f599d0e 5035 else if (h->dynindx != -1)
4ad4eba5
AM
5036 /* If the symbol already has a dynamic index, but
5037 visibility says it should not be visible, turn it into
5038 a local symbol. */
5039 switch (ELF_ST_VISIBILITY (h->other))
5040 {
5041 case STV_INTERNAL:
5042 case STV_HIDDEN:
5043 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5044 dynsym = FALSE;
5045 break;
5046 }
5047
aef28989
L
5048 /* Don't add DT_NEEDED for references from the dummy bfd nor
5049 for unmatched symbol. */
4ad4eba5 5050 if (!add_needed
aef28989 5051 && matched
4ad4eba5 5052 && definition
010e5ae2 5053 && ((dynsym
ffa9430d 5054 && h->ref_regular_nonweak
4f3fedcf
AM
5055 && (old_bfd == NULL
5056 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5057 || (h->ref_dynamic_nonweak
010e5ae2 5058 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5059 && !on_needed_list (elf_dt_name (abfd),
5060 htab->needed, NULL))))
4ad4eba5
AM
5061 {
5062 int ret;
5063 const char *soname = elf_dt_name (abfd);
5064
16e4ecc0
AM
5065 info->callbacks->minfo ("%!", soname, old_bfd,
5066 h->root.root.string);
5067
4ad4eba5
AM
5068 /* A symbol from a library loaded via DT_NEEDED of some
5069 other library is referenced by a regular object.
e56f61be 5070 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5071 --no-add-needed is used and the reference was not
5072 a weak one. */
4f3fedcf 5073 if (old_bfd != NULL
b918acf9 5074 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5075 {
4eca0228 5076 _bfd_error_handler
695344c0 5077 /* xgettext:c-format */
871b3ab2 5078 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5079 old_bfd, name);
ff5ac77b 5080 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5081 goto error_free_vers;
5082 }
5083
a50b1753 5084 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5085 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5086
4ad4eba5 5087 add_needed = TRUE;
7e9f0867 5088 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5089 if (ret < 0)
5090 goto error_free_vers;
5091
5092 BFD_ASSERT (ret == 0);
5093 }
5094 }
5095 }
5096
a83ef4d1
L
5097 if (info->lto_plugin_active
5098 && !bfd_link_relocatable (info)
5099 && (abfd->flags & BFD_PLUGIN) == 0
5100 && !just_syms
5101 && extsymcount)
5102 {
5103 int r_sym_shift;
5104
5105 if (bed->s->arch_size == 32)
5106 r_sym_shift = 8;
5107 else
5108 r_sym_shift = 32;
5109
5110 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5111 referenced in regular objects so that linker plugin will get
5112 the correct symbol resolution. */
5113
5114 sym_hash = elf_sym_hashes (abfd);
5115 for (s = abfd->sections; s != NULL; s = s->next)
5116 {
5117 Elf_Internal_Rela *internal_relocs;
5118 Elf_Internal_Rela *rel, *relend;
5119
5120 /* Don't check relocations in excluded sections. */
5121 if ((s->flags & SEC_RELOC) == 0
5122 || s->reloc_count == 0
5123 || (s->flags & SEC_EXCLUDE) != 0
5124 || ((info->strip == strip_all
5125 || info->strip == strip_debugger)
5126 && (s->flags & SEC_DEBUGGING) != 0))
5127 continue;
5128
5129 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5130 NULL,
5131 info->keep_memory);
5132 if (internal_relocs == NULL)
5133 goto error_free_vers;
5134
5135 rel = internal_relocs;
5136 relend = rel + s->reloc_count;
5137 for ( ; rel < relend; rel++)
5138 {
5139 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5140 struct elf_link_hash_entry *h;
5141
5142 /* Skip local symbols. */
5143 if (r_symndx < extsymoff)
5144 continue;
5145
5146 h = sym_hash[r_symndx - extsymoff];
5147 if (h != NULL)
5148 h->root.non_ir_ref_regular = 1;
5149 }
5150
5151 if (elf_section_data (s)->relocs != internal_relocs)
5152 free (internal_relocs);
5153 }
5154 }
5155
66eb6687
AM
5156 if (extversym != NULL)
5157 {
5158 free (extversym);
5159 extversym = NULL;
5160 }
5161
5162 if (isymbuf != NULL)
5163 {
5164 free (isymbuf);
5165 isymbuf = NULL;
5166 }
5167
5168 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5169 {
5170 unsigned int i;
5171
5172 /* Restore the symbol table. */
f45794cb
AM
5173 old_ent = (char *) old_tab + tabsize;
5174 memset (elf_sym_hashes (abfd), 0,
5175 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5176 htab->root.table.table = old_table;
5177 htab->root.table.size = old_size;
5178 htab->root.table.count = old_count;
66eb6687 5179 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5180 htab->root.undefs = old_undefs;
5181 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5182 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5183 free (old_strtab);
5184 old_strtab = NULL;
66eb6687
AM
5185 for (i = 0; i < htab->root.table.size; i++)
5186 {
5187 struct bfd_hash_entry *p;
5188 struct elf_link_hash_entry *h;
3e0882af
L
5189 bfd_size_type size;
5190 unsigned int alignment_power;
4070765b 5191 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5192
5193 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5194 {
5195 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5196 if (h->root.type == bfd_link_hash_warning)
5197 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5198
3e0882af
L
5199 /* Preserve the maximum alignment and size for common
5200 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5201 since it can still be loaded at run time by another
3e0882af
L
5202 dynamic lib. */
5203 if (h->root.type == bfd_link_hash_common)
5204 {
5205 size = h->root.u.c.size;
5206 alignment_power = h->root.u.c.p->alignment_power;
5207 }
5208 else
5209 {
5210 size = 0;
5211 alignment_power = 0;
5212 }
4070765b 5213 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5214 will be exported when the dynamic lib becomes needed
5215 in the second pass. */
4070765b 5216 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5217 memcpy (p, old_ent, htab->root.table.entsize);
5218 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5219 h = (struct elf_link_hash_entry *) p;
5220 if (h->root.type == bfd_link_hash_warning)
5221 {
5222 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5223 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5224 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5225 }
a4542f1b 5226 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5227 {
5228 if (size > h->root.u.c.size)
5229 h->root.u.c.size = size;
5230 if (alignment_power > h->root.u.c.p->alignment_power)
5231 h->root.u.c.p->alignment_power = alignment_power;
5232 }
4070765b 5233 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5234 }
5235 }
5236
5061a885
AM
5237 /* Make a special call to the linker "notice" function to
5238 tell it that symbols added for crefs may need to be removed. */
e5034e59 5239 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5240 goto error_free_vers;
5061a885 5241
66eb6687
AM
5242 free (old_tab);
5243 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5244 alloc_mark);
5245 if (nondeflt_vers != NULL)
5246 free (nondeflt_vers);
5247 return TRUE;
5248 }
2de92251 5249
66eb6687
AM
5250 if (old_tab != NULL)
5251 {
e5034e59 5252 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5253 goto error_free_vers;
66eb6687
AM
5254 free (old_tab);
5255 old_tab = NULL;
5256 }
5257
c6e8a9a8
L
5258 /* Now that all the symbols from this input file are created, if
5259 not performing a relocatable link, handle .symver foo, foo@BAR
5260 such that any relocs against foo become foo@BAR. */
0e1862bb 5261 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5262 {
ef53be89 5263 size_t cnt, symidx;
4ad4eba5
AM
5264
5265 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5266 {
5267 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5268 char *shortname, *p;
5269
5270 p = strchr (h->root.root.string, ELF_VER_CHR);
5271 if (p == NULL
5272 || (h->root.type != bfd_link_hash_defined
5273 && h->root.type != bfd_link_hash_defweak))
5274 continue;
5275
5276 amt = p - h->root.root.string;
a50b1753 5277 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5278 if (!shortname)
5279 goto error_free_vers;
4ad4eba5
AM
5280 memcpy (shortname, h->root.root.string, amt);
5281 shortname[amt] = '\0';
5282
5283 hi = (struct elf_link_hash_entry *)
66eb6687 5284 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5285 FALSE, FALSE, FALSE);
5286 if (hi != NULL
5287 && hi->root.type == h->root.type
5288 && hi->root.u.def.value == h->root.u.def.value
5289 && hi->root.u.def.section == h->root.u.def.section)
5290 {
5291 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5292 hi->root.type = bfd_link_hash_indirect;
5293 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5294 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5295 sym_hash = elf_sym_hashes (abfd);
5296 if (sym_hash)
5297 for (symidx = 0; symidx < extsymcount; ++symidx)
5298 if (sym_hash[symidx] == hi)
5299 {
5300 sym_hash[symidx] = h;
5301 break;
5302 }
5303 }
5304 free (shortname);
5305 }
5306 free (nondeflt_vers);
5307 nondeflt_vers = NULL;
5308 }
5309
60d67dc8 5310 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5311 symbols we found. The only way to do this is to search all the
5312 symbols. Since we only need the information for non functions in
5313 dynamic objects, that's the only time we actually put anything on
5314 the list WEAKS. We need this information so that if a regular
5315 object refers to a symbol defined weakly in a dynamic object, the
5316 real symbol in the dynamic object is also put in the dynamic
5317 symbols; we also must arrange for both symbols to point to the
5318 same memory location. We could handle the general case of symbol
5319 aliasing, but a general symbol alias can only be generated in
5320 assembler code, handling it correctly would be very time
5321 consuming, and other ELF linkers don't handle general aliasing
5322 either. */
5323 if (weaks != NULL)
5324 {
5325 struct elf_link_hash_entry **hpp;
5326 struct elf_link_hash_entry **hppend;
5327 struct elf_link_hash_entry **sorted_sym_hash;
5328 struct elf_link_hash_entry *h;
5329 size_t sym_count;
5330
5331 /* Since we have to search the whole symbol list for each weak
5332 defined symbol, search time for N weak defined symbols will be
5333 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5334 amt = extsymcount;
5335 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5336 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5337 if (sorted_sym_hash == NULL)
5338 goto error_return;
5339 sym_hash = sorted_sym_hash;
5340 hpp = elf_sym_hashes (abfd);
5341 hppend = hpp + extsymcount;
5342 sym_count = 0;
5343 for (; hpp < hppend; hpp++)
5344 {
5345 h = *hpp;
5346 if (h != NULL
5347 && h->root.type == bfd_link_hash_defined
fcb93ecf 5348 && !bed->is_function_type (h->type))
4ad4eba5
AM
5349 {
5350 *sym_hash = h;
5351 sym_hash++;
5352 sym_count++;
5353 }
5354 }
5355
5356 qsort (sorted_sym_hash, sym_count,
5357 sizeof (struct elf_link_hash_entry *),
5358 elf_sort_symbol);
5359
5360 while (weaks != NULL)
5361 {
5362 struct elf_link_hash_entry *hlook;
5363 asection *slook;
5364 bfd_vma vlook;
ed54588d 5365 size_t i, j, idx = 0;
4ad4eba5
AM
5366
5367 hlook = weaks;
60d67dc8
AM
5368 weaks = hlook->u.alias;
5369 hlook->u.alias = NULL;
4ad4eba5 5370
e3e53eed
AM
5371 if (hlook->root.type != bfd_link_hash_defined
5372 && hlook->root.type != bfd_link_hash_defweak)
5373 continue;
5374
4ad4eba5
AM
5375 slook = hlook->root.u.def.section;
5376 vlook = hlook->root.u.def.value;
5377
4ad4eba5
AM
5378 i = 0;
5379 j = sym_count;
14160578 5380 while (i != j)
4ad4eba5
AM
5381 {
5382 bfd_signed_vma vdiff;
5383 idx = (i + j) / 2;
14160578 5384 h = sorted_sym_hash[idx];
4ad4eba5
AM
5385 vdiff = vlook - h->root.u.def.value;
5386 if (vdiff < 0)
5387 j = idx;
5388 else if (vdiff > 0)
5389 i = idx + 1;
5390 else
5391 {
d3435ae8 5392 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5393 if (sdiff < 0)
5394 j = idx;
5395 else if (sdiff > 0)
5396 i = idx + 1;
5397 else
14160578 5398 break;
4ad4eba5
AM
5399 }
5400 }
5401
5402 /* We didn't find a value/section match. */
14160578 5403 if (i == j)
4ad4eba5
AM
5404 continue;
5405
14160578
AM
5406 /* With multiple aliases, or when the weak symbol is already
5407 strongly defined, we have multiple matching symbols and
5408 the binary search above may land on any of them. Step
5409 one past the matching symbol(s). */
5410 while (++idx != j)
5411 {
5412 h = sorted_sym_hash[idx];
5413 if (h->root.u.def.section != slook
5414 || h->root.u.def.value != vlook)
5415 break;
5416 }
5417
5418 /* Now look back over the aliases. Since we sorted by size
5419 as well as value and section, we'll choose the one with
5420 the largest size. */
5421 while (idx-- != i)
4ad4eba5 5422 {
14160578 5423 h = sorted_sym_hash[idx];
4ad4eba5
AM
5424
5425 /* Stop if value or section doesn't match. */
14160578
AM
5426 if (h->root.u.def.section != slook
5427 || h->root.u.def.value != vlook)
4ad4eba5
AM
5428 break;
5429 else if (h != hlook)
5430 {
60d67dc8
AM
5431 struct elf_link_hash_entry *t;
5432
5433 hlook->u.alias = h;
5434 hlook->is_weakalias = 1;
5435 t = h;
5436 if (t->u.alias != NULL)
5437 while (t->u.alias != h)
5438 t = t->u.alias;
5439 t->u.alias = hlook;
4ad4eba5
AM
5440
5441 /* If the weak definition is in the list of dynamic
5442 symbols, make sure the real definition is put
5443 there as well. */
5444 if (hlook->dynindx != -1 && h->dynindx == -1)
5445 {
c152c796 5446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5447 {
5448 err_free_sym_hash:
5449 free (sorted_sym_hash);
5450 goto error_return;
5451 }
4ad4eba5
AM
5452 }
5453
5454 /* If the real definition is in the list of dynamic
5455 symbols, make sure the weak definition is put
5456 there as well. If we don't do this, then the
5457 dynamic loader might not merge the entries for the
5458 real definition and the weak definition. */
5459 if (h->dynindx != -1 && hlook->dynindx == -1)
5460 {
c152c796 5461 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5462 goto err_free_sym_hash;
4ad4eba5
AM
5463 }
5464 break;
5465 }
5466 }
5467 }
5468
5469 free (sorted_sym_hash);
5470 }
5471
33177bb1
AM
5472 if (bed->check_directives
5473 && !(*bed->check_directives) (abfd, info))
5474 return FALSE;
85fbca6a 5475
4ad4eba5
AM
5476 /* If this is a non-traditional link, try to optimize the handling
5477 of the .stab/.stabstr sections. */
5478 if (! dynamic
5479 && ! info->traditional_format
66eb6687 5480 && is_elf_hash_table (htab)
4ad4eba5
AM
5481 && (info->strip != strip_all && info->strip != strip_debugger))
5482 {
5483 asection *stabstr;
5484
5485 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5486 if (stabstr != NULL)
5487 {
5488 bfd_size_type string_offset = 0;
5489 asection *stab;
5490
5491 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5492 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5493 && (!stab->name[5] ||
5494 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5495 && (stab->flags & SEC_MERGE) == 0
5496 && !bfd_is_abs_section (stab->output_section))
5497 {
5498 struct bfd_elf_section_data *secdata;
5499
5500 secdata = elf_section_data (stab);
66eb6687
AM
5501 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5502 stabstr, &secdata->sec_info,
4ad4eba5
AM
5503 &string_offset))
5504 goto error_return;
5505 if (secdata->sec_info)
dbaa2011 5506 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5507 }
5508 }
5509 }
5510
66eb6687 5511 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5512 {
5513 /* Add this bfd to the loaded list. */
5514 struct elf_link_loaded_list *n;
5515
ca4be51c 5516 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5517 if (n == NULL)
5518 goto error_return;
5519 n->abfd = abfd;
66eb6687
AM
5520 n->next = htab->loaded;
5521 htab->loaded = n;
4ad4eba5
AM
5522 }
5523
5524 return TRUE;
5525
5526 error_free_vers:
66eb6687
AM
5527 if (old_tab != NULL)
5528 free (old_tab);
5b677558
AM
5529 if (old_strtab != NULL)
5530 free (old_strtab);
4ad4eba5
AM
5531 if (nondeflt_vers != NULL)
5532 free (nondeflt_vers);
5533 if (extversym != NULL)
5534 free (extversym);
5535 error_free_sym:
5536 if (isymbuf != NULL)
5537 free (isymbuf);
5538 error_return:
5539 return FALSE;
5540}
5541
8387904d
AM
5542/* Return the linker hash table entry of a symbol that might be
5543 satisfied by an archive symbol. Return -1 on error. */
5544
5545struct elf_link_hash_entry *
5546_bfd_elf_archive_symbol_lookup (bfd *abfd,
5547 struct bfd_link_info *info,
5548 const char *name)
5549{
5550 struct elf_link_hash_entry *h;
5551 char *p, *copy;
5552 size_t len, first;
5553
2a41f396 5554 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5555 if (h != NULL)
5556 return h;
5557
5558 /* If this is a default version (the name contains @@), look up the
5559 symbol again with only one `@' as well as without the version.
5560 The effect is that references to the symbol with and without the
5561 version will be matched by the default symbol in the archive. */
5562
5563 p = strchr (name, ELF_VER_CHR);
5564 if (p == NULL || p[1] != ELF_VER_CHR)
5565 return h;
5566
5567 /* First check with only one `@'. */
5568 len = strlen (name);
a50b1753 5569 copy = (char *) bfd_alloc (abfd, len);
8387904d 5570 if (copy == NULL)
e99955cd 5571 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5572
5573 first = p - name + 1;
5574 memcpy (copy, name, first);
5575 memcpy (copy + first, name + first + 1, len - first);
5576
2a41f396 5577 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5578 if (h == NULL)
5579 {
5580 /* We also need to check references to the symbol without the
5581 version. */
5582 copy[first - 1] = '\0';
5583 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5584 FALSE, FALSE, TRUE);
8387904d
AM
5585 }
5586
5587 bfd_release (abfd, copy);
5588 return h;
5589}
5590
0ad989f9 5591/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5592 don't use _bfd_generic_link_add_archive_symbols because we need to
5593 handle versioned symbols.
0ad989f9
L
5594
5595 Fortunately, ELF archive handling is simpler than that done by
5596 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5597 oddities. In ELF, if we find a symbol in the archive map, and the
5598 symbol is currently undefined, we know that we must pull in that
5599 object file.
5600
5601 Unfortunately, we do have to make multiple passes over the symbol
5602 table until nothing further is resolved. */
5603
4ad4eba5
AM
5604static bfd_boolean
5605elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5606{
5607 symindex c;
13e570f8 5608 unsigned char *included = NULL;
0ad989f9
L
5609 carsym *symdefs;
5610 bfd_boolean loop;
5611 bfd_size_type amt;
8387904d
AM
5612 const struct elf_backend_data *bed;
5613 struct elf_link_hash_entry * (*archive_symbol_lookup)
5614 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5615
5616 if (! bfd_has_map (abfd))
5617 {
5618 /* An empty archive is a special case. */
5619 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5620 return TRUE;
5621 bfd_set_error (bfd_error_no_armap);
5622 return FALSE;
5623 }
5624
5625 /* Keep track of all symbols we know to be already defined, and all
5626 files we know to be already included. This is to speed up the
5627 second and subsequent passes. */
5628 c = bfd_ardata (abfd)->symdef_count;
5629 if (c == 0)
5630 return TRUE;
5631 amt = c;
13e570f8
AM
5632 amt *= sizeof (*included);
5633 included = (unsigned char *) bfd_zmalloc (amt);
5634 if (included == NULL)
5635 return FALSE;
0ad989f9
L
5636
5637 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5638 bed = get_elf_backend_data (abfd);
5639 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5640
5641 do
5642 {
5643 file_ptr last;
5644 symindex i;
5645 carsym *symdef;
5646 carsym *symdefend;
5647
5648 loop = FALSE;
5649 last = -1;
5650
5651 symdef = symdefs;
5652 symdefend = symdef + c;
5653 for (i = 0; symdef < symdefend; symdef++, i++)
5654 {
5655 struct elf_link_hash_entry *h;
5656 bfd *element;
5657 struct bfd_link_hash_entry *undefs_tail;
5658 symindex mark;
5659
13e570f8 5660 if (included[i])
0ad989f9
L
5661 continue;
5662 if (symdef->file_offset == last)
5663 {
5664 included[i] = TRUE;
5665 continue;
5666 }
5667
8387904d 5668 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5669 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5670 goto error_return;
0ad989f9
L
5671
5672 if (h == NULL)
5673 continue;
5674
5675 if (h->root.type == bfd_link_hash_common)
5676 {
5677 /* We currently have a common symbol. The archive map contains
5678 a reference to this symbol, so we may want to include it. We
5679 only want to include it however, if this archive element
5680 contains a definition of the symbol, not just another common
5681 declaration of it.
5682
5683 Unfortunately some archivers (including GNU ar) will put
5684 declarations of common symbols into their archive maps, as
5685 well as real definitions, so we cannot just go by the archive
5686 map alone. Instead we must read in the element's symbol
5687 table and check that to see what kind of symbol definition
5688 this is. */
5689 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5690 continue;
5691 }
5692 else if (h->root.type != bfd_link_hash_undefined)
5693 {
5694 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5695 /* Symbol must be defined. Don't check it again. */
5696 included[i] = TRUE;
0ad989f9
L
5697 continue;
5698 }
5699
5700 /* We need to include this archive member. */
5701 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5702 if (element == NULL)
5703 goto error_return;
5704
5705 if (! bfd_check_format (element, bfd_object))
5706 goto error_return;
5707
0ad989f9
L
5708 undefs_tail = info->hash->undefs_tail;
5709
0e144ba7
AM
5710 if (!(*info->callbacks
5711 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5712 continue;
0e144ba7 5713 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5714 goto error_return;
5715
5716 /* If there are any new undefined symbols, we need to make
5717 another pass through the archive in order to see whether
5718 they can be defined. FIXME: This isn't perfect, because
5719 common symbols wind up on undefs_tail and because an
5720 undefined symbol which is defined later on in this pass
5721 does not require another pass. This isn't a bug, but it
5722 does make the code less efficient than it could be. */
5723 if (undefs_tail != info->hash->undefs_tail)
5724 loop = TRUE;
5725
5726 /* Look backward to mark all symbols from this object file
5727 which we have already seen in this pass. */
5728 mark = i;
5729 do
5730 {
5731 included[mark] = TRUE;
5732 if (mark == 0)
5733 break;
5734 --mark;
5735 }
5736 while (symdefs[mark].file_offset == symdef->file_offset);
5737
5738 /* We mark subsequent symbols from this object file as we go
5739 on through the loop. */
5740 last = symdef->file_offset;
5741 }
5742 }
5743 while (loop);
5744
0ad989f9
L
5745 free (included);
5746
5747 return TRUE;
5748
5749 error_return:
0ad989f9
L
5750 if (included != NULL)
5751 free (included);
5752 return FALSE;
5753}
4ad4eba5
AM
5754
5755/* Given an ELF BFD, add symbols to the global hash table as
5756 appropriate. */
5757
5758bfd_boolean
5759bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5760{
5761 switch (bfd_get_format (abfd))
5762 {
5763 case bfd_object:
5764 return elf_link_add_object_symbols (abfd, info);
5765 case bfd_archive:
5766 return elf_link_add_archive_symbols (abfd, info);
5767 default:
5768 bfd_set_error (bfd_error_wrong_format);
5769 return FALSE;
5770 }
5771}
5a580b3a 5772\f
14b1c01e
AM
5773struct hash_codes_info
5774{
5775 unsigned long *hashcodes;
5776 bfd_boolean error;
5777};
a0c8462f 5778
5a580b3a
AM
5779/* This function will be called though elf_link_hash_traverse to store
5780 all hash value of the exported symbols in an array. */
5781
5782static bfd_boolean
5783elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5784{
a50b1753 5785 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5786 const char *name;
5a580b3a
AM
5787 unsigned long ha;
5788 char *alc = NULL;
5789
5a580b3a
AM
5790 /* Ignore indirect symbols. These are added by the versioning code. */
5791 if (h->dynindx == -1)
5792 return TRUE;
5793
5794 name = h->root.root.string;
422f1182 5795 if (h->versioned >= versioned)
5a580b3a 5796 {
422f1182
L
5797 char *p = strchr (name, ELF_VER_CHR);
5798 if (p != NULL)
14b1c01e 5799 {
422f1182
L
5800 alc = (char *) bfd_malloc (p - name + 1);
5801 if (alc == NULL)
5802 {
5803 inf->error = TRUE;
5804 return FALSE;
5805 }
5806 memcpy (alc, name, p - name);
5807 alc[p - name] = '\0';
5808 name = alc;
14b1c01e 5809 }
5a580b3a
AM
5810 }
5811
5812 /* Compute the hash value. */
5813 ha = bfd_elf_hash (name);
5814
5815 /* Store the found hash value in the array given as the argument. */
14b1c01e 5816 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5817
5818 /* And store it in the struct so that we can put it in the hash table
5819 later. */
f6e332e6 5820 h->u.elf_hash_value = ha;
5a580b3a
AM
5821
5822 if (alc != NULL)
5823 free (alc);
5824
5825 return TRUE;
5826}
5827
fdc90cb4
JJ
5828struct collect_gnu_hash_codes
5829{
5830 bfd *output_bfd;
5831 const struct elf_backend_data *bed;
5832 unsigned long int nsyms;
5833 unsigned long int maskbits;
5834 unsigned long int *hashcodes;
5835 unsigned long int *hashval;
5836 unsigned long int *indx;
5837 unsigned long int *counts;
5838 bfd_vma *bitmask;
5839 bfd_byte *contents;
5840 long int min_dynindx;
5841 unsigned long int bucketcount;
5842 unsigned long int symindx;
5843 long int local_indx;
5844 long int shift1, shift2;
5845 unsigned long int mask;
14b1c01e 5846 bfd_boolean error;
fdc90cb4
JJ
5847};
5848
5849/* This function will be called though elf_link_hash_traverse to store
5850 all hash value of the exported symbols in an array. */
5851
5852static bfd_boolean
5853elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5854{
a50b1753 5855 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5856 const char *name;
fdc90cb4
JJ
5857 unsigned long ha;
5858 char *alc = NULL;
5859
fdc90cb4
JJ
5860 /* Ignore indirect symbols. These are added by the versioning code. */
5861 if (h->dynindx == -1)
5862 return TRUE;
5863
5864 /* Ignore also local symbols and undefined symbols. */
5865 if (! (*s->bed->elf_hash_symbol) (h))
5866 return TRUE;
5867
5868 name = h->root.root.string;
422f1182 5869 if (h->versioned >= versioned)
fdc90cb4 5870 {
422f1182
L
5871 char *p = strchr (name, ELF_VER_CHR);
5872 if (p != NULL)
14b1c01e 5873 {
422f1182
L
5874 alc = (char *) bfd_malloc (p - name + 1);
5875 if (alc == NULL)
5876 {
5877 s->error = TRUE;
5878 return FALSE;
5879 }
5880 memcpy (alc, name, p - name);
5881 alc[p - name] = '\0';
5882 name = alc;
14b1c01e 5883 }
fdc90cb4
JJ
5884 }
5885
5886 /* Compute the hash value. */
5887 ha = bfd_elf_gnu_hash (name);
5888
5889 /* Store the found hash value in the array for compute_bucket_count,
5890 and also for .dynsym reordering purposes. */
5891 s->hashcodes[s->nsyms] = ha;
5892 s->hashval[h->dynindx] = ha;
5893 ++s->nsyms;
5894 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5895 s->min_dynindx = h->dynindx;
5896
5897 if (alc != NULL)
5898 free (alc);
5899
5900 return TRUE;
5901}
5902
5903/* This function will be called though elf_link_hash_traverse to do
5904 final dynaminc symbol renumbering. */
5905
5906static bfd_boolean
5907elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5908{
a50b1753 5909 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5910 unsigned long int bucket;
5911 unsigned long int val;
5912
fdc90cb4
JJ
5913 /* Ignore indirect symbols. */
5914 if (h->dynindx == -1)
5915 return TRUE;
5916
5917 /* Ignore also local symbols and undefined symbols. */
5918 if (! (*s->bed->elf_hash_symbol) (h))
5919 {
5920 if (h->dynindx >= s->min_dynindx)
5921 h->dynindx = s->local_indx++;
5922 return TRUE;
5923 }
5924
5925 bucket = s->hashval[h->dynindx] % s->bucketcount;
5926 val = (s->hashval[h->dynindx] >> s->shift1)
5927 & ((s->maskbits >> s->shift1) - 1);
5928 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5929 s->bitmask[val]
5930 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5931 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5932 if (s->counts[bucket] == 1)
5933 /* Last element terminates the chain. */
5934 val |= 1;
5935 bfd_put_32 (s->output_bfd, val,
5936 s->contents + (s->indx[bucket] - s->symindx) * 4);
5937 --s->counts[bucket];
5938 h->dynindx = s->indx[bucket]++;
5939 return TRUE;
5940}
5941
5942/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5943
5944bfd_boolean
5945_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5946{
5947 return !(h->forced_local
5948 || h->root.type == bfd_link_hash_undefined
5949 || h->root.type == bfd_link_hash_undefweak
5950 || ((h->root.type == bfd_link_hash_defined
5951 || h->root.type == bfd_link_hash_defweak)
5952 && h->root.u.def.section->output_section == NULL));
5953}
5954
5a580b3a
AM
5955/* Array used to determine the number of hash table buckets to use
5956 based on the number of symbols there are. If there are fewer than
5957 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5958 fewer than 37 we use 17 buckets, and so forth. We never use more
5959 than 32771 buckets. */
5960
5961static const size_t elf_buckets[] =
5962{
5963 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5964 16411, 32771, 0
5965};
5966
5967/* Compute bucket count for hashing table. We do not use a static set
5968 of possible tables sizes anymore. Instead we determine for all
5969 possible reasonable sizes of the table the outcome (i.e., the
5970 number of collisions etc) and choose the best solution. The
5971 weighting functions are not too simple to allow the table to grow
5972 without bounds. Instead one of the weighting factors is the size.
5973 Therefore the result is always a good payoff between few collisions
5974 (= short chain lengths) and table size. */
5975static size_t
b20dd2ce 5976compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5977 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5978 unsigned long int nsyms,
5979 int gnu_hash)
5a580b3a 5980{
5a580b3a 5981 size_t best_size = 0;
5a580b3a 5982 unsigned long int i;
5a580b3a 5983
5a580b3a
AM
5984 /* We have a problem here. The following code to optimize the table
5985 size requires an integer type with more the 32 bits. If
5986 BFD_HOST_U_64_BIT is set we know about such a type. */
5987#ifdef BFD_HOST_U_64_BIT
5988 if (info->optimize)
5989 {
5a580b3a
AM
5990 size_t minsize;
5991 size_t maxsize;
5992 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5993 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5994 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5995 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5996 unsigned long int *counts;
d40f3da9 5997 bfd_size_type amt;
0883b6e0 5998 unsigned int no_improvement_count = 0;
5a580b3a
AM
5999
6000 /* Possible optimization parameters: if we have NSYMS symbols we say
6001 that the hashing table must at least have NSYMS/4 and at most
6002 2*NSYMS buckets. */
6003 minsize = nsyms / 4;
6004 if (minsize == 0)
6005 minsize = 1;
6006 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6007 if (gnu_hash)
6008 {
6009 if (minsize < 2)
6010 minsize = 2;
6011 if ((best_size & 31) == 0)
6012 ++best_size;
6013 }
5a580b3a
AM
6014
6015 /* Create array where we count the collisions in. We must use bfd_malloc
6016 since the size could be large. */
6017 amt = maxsize;
6018 amt *= sizeof (unsigned long int);
a50b1753 6019 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6020 if (counts == NULL)
fdc90cb4 6021 return 0;
5a580b3a
AM
6022
6023 /* Compute the "optimal" size for the hash table. The criteria is a
6024 minimal chain length. The minor criteria is (of course) the size
6025 of the table. */
6026 for (i = minsize; i < maxsize; ++i)
6027 {
6028 /* Walk through the array of hashcodes and count the collisions. */
6029 BFD_HOST_U_64_BIT max;
6030 unsigned long int j;
6031 unsigned long int fact;
6032
fdc90cb4
JJ
6033 if (gnu_hash && (i & 31) == 0)
6034 continue;
6035
5a580b3a
AM
6036 memset (counts, '\0', i * sizeof (unsigned long int));
6037
6038 /* Determine how often each hash bucket is used. */
6039 for (j = 0; j < nsyms; ++j)
6040 ++counts[hashcodes[j] % i];
6041
6042 /* For the weight function we need some information about the
6043 pagesize on the target. This is information need not be 100%
6044 accurate. Since this information is not available (so far) we
6045 define it here to a reasonable default value. If it is crucial
6046 to have a better value some day simply define this value. */
6047# ifndef BFD_TARGET_PAGESIZE
6048# define BFD_TARGET_PAGESIZE (4096)
6049# endif
6050
fdc90cb4
JJ
6051 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6052 and the chains. */
6053 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6054
6055# if 1
6056 /* Variant 1: optimize for short chains. We add the squares
6057 of all the chain lengths (which favors many small chain
6058 over a few long chains). */
6059 for (j = 0; j < i; ++j)
6060 max += counts[j] * counts[j];
6061
6062 /* This adds penalties for the overall size of the table. */
fdc90cb4 6063 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6064 max *= fact * fact;
6065# else
6066 /* Variant 2: Optimize a lot more for small table. Here we
6067 also add squares of the size but we also add penalties for
6068 empty slots (the +1 term). */
6069 for (j = 0; j < i; ++j)
6070 max += (1 + counts[j]) * (1 + counts[j]);
6071
6072 /* The overall size of the table is considered, but not as
6073 strong as in variant 1, where it is squared. */
fdc90cb4 6074 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6075 max *= fact;
6076# endif
6077
6078 /* Compare with current best results. */
6079 if (max < best_chlen)
6080 {
6081 best_chlen = max;
6082 best_size = i;
ca4be51c 6083 no_improvement_count = 0;
5a580b3a 6084 }
0883b6e0
NC
6085 /* PR 11843: Avoid futile long searches for the best bucket size
6086 when there are a large number of symbols. */
6087 else if (++no_improvement_count == 100)
6088 break;
5a580b3a
AM
6089 }
6090
6091 free (counts);
6092 }
6093 else
6094#endif /* defined (BFD_HOST_U_64_BIT) */
6095 {
6096 /* This is the fallback solution if no 64bit type is available or if we
6097 are not supposed to spend much time on optimizations. We select the
6098 bucket count using a fixed set of numbers. */
6099 for (i = 0; elf_buckets[i] != 0; i++)
6100 {
6101 best_size = elf_buckets[i];
fdc90cb4 6102 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6103 break;
6104 }
fdc90cb4
JJ
6105 if (gnu_hash && best_size < 2)
6106 best_size = 2;
5a580b3a
AM
6107 }
6108
5a580b3a
AM
6109 return best_size;
6110}
6111
d0bf826b
AM
6112/* Size any SHT_GROUP section for ld -r. */
6113
6114bfd_boolean
6115_bfd_elf_size_group_sections (struct bfd_link_info *info)
6116{
6117 bfd *ibfd;
57963c05 6118 asection *s;
d0bf826b 6119
c72f2fb2 6120 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6121 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6122 && (s = ibfd->sections) != NULL
6123 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6124 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6125 return FALSE;
6126 return TRUE;
6127}
6128
04c3a755
NS
6129/* Set a default stack segment size. The value in INFO wins. If it
6130 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6131 undefined it is initialized. */
6132
6133bfd_boolean
6134bfd_elf_stack_segment_size (bfd *output_bfd,
6135 struct bfd_link_info *info,
6136 const char *legacy_symbol,
6137 bfd_vma default_size)
6138{
6139 struct elf_link_hash_entry *h = NULL;
6140
6141 /* Look for legacy symbol. */
6142 if (legacy_symbol)
6143 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6144 FALSE, FALSE, FALSE);
6145 if (h && (h->root.type == bfd_link_hash_defined
6146 || h->root.type == bfd_link_hash_defweak)
6147 && h->def_regular
6148 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6149 {
6150 /* The symbol has no type if specified on the command line. */
6151 h->type = STT_OBJECT;
6152 if (info->stacksize)
695344c0 6153 /* xgettext:c-format */
871b3ab2 6154 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6155 output_bfd, legacy_symbol);
04c3a755 6156 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6157 /* xgettext:c-format */
871b3ab2 6158 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6159 output_bfd, legacy_symbol);
04c3a755
NS
6160 else
6161 info->stacksize = h->root.u.def.value;
6162 }
6163
6164 if (!info->stacksize)
6165 /* If the user didn't set a size, or explicitly inhibit the
6166 size, set it now. */
6167 info->stacksize = default_size;
6168
6169 /* Provide the legacy symbol, if it is referenced. */
6170 if (h && (h->root.type == bfd_link_hash_undefined
6171 || h->root.type == bfd_link_hash_undefweak))
6172 {
6173 struct bfd_link_hash_entry *bh = NULL;
6174
6175 if (!(_bfd_generic_link_add_one_symbol
6176 (info, output_bfd, legacy_symbol,
6177 BSF_GLOBAL, bfd_abs_section_ptr,
6178 info->stacksize >= 0 ? info->stacksize : 0,
6179 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6180 return FALSE;
6181
6182 h = (struct elf_link_hash_entry *) bh;
6183 h->def_regular = 1;
6184 h->type = STT_OBJECT;
6185 }
6186
6187 return TRUE;
6188}
6189
b531344c
MR
6190/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6191
6192struct elf_gc_sweep_symbol_info
6193{
6194 struct bfd_link_info *info;
6195 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6196 bfd_boolean);
6197};
6198
6199static bfd_boolean
6200elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6201{
6202 if (!h->mark
6203 && (((h->root.type == bfd_link_hash_defined
6204 || h->root.type == bfd_link_hash_defweak)
6205 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6206 && h->root.u.def.section->gc_mark))
6207 || h->root.type == bfd_link_hash_undefined
6208 || h->root.type == bfd_link_hash_undefweak))
6209 {
6210 struct elf_gc_sweep_symbol_info *inf;
6211
6212 inf = (struct elf_gc_sweep_symbol_info *) data;
6213 (*inf->hide_symbol) (inf->info, h, TRUE);
6214 h->def_regular = 0;
6215 h->ref_regular = 0;
6216 h->ref_regular_nonweak = 0;
6217 }
6218
6219 return TRUE;
6220}
6221
5a580b3a
AM
6222/* Set up the sizes and contents of the ELF dynamic sections. This is
6223 called by the ELF linker emulation before_allocation routine. We
6224 must set the sizes of the sections before the linker sets the
6225 addresses of the various sections. */
6226
6227bfd_boolean
6228bfd_elf_size_dynamic_sections (bfd *output_bfd,
6229 const char *soname,
6230 const char *rpath,
6231 const char *filter_shlib,
7ee314fa
AM
6232 const char *audit,
6233 const char *depaudit,
5a580b3a
AM
6234 const char * const *auxiliary_filters,
6235 struct bfd_link_info *info,
fd91d419 6236 asection **sinterpptr)
5a580b3a 6237{
5a580b3a
AM
6238 bfd *dynobj;
6239 const struct elf_backend_data *bed;
5a580b3a
AM
6240
6241 *sinterpptr = NULL;
6242
5a580b3a
AM
6243 if (!is_elf_hash_table (info->hash))
6244 return TRUE;
6245
5a580b3a
AM
6246 dynobj = elf_hash_table (info)->dynobj;
6247
9a2a56cc 6248 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6249 {
902e9fc7
MR
6250 struct bfd_elf_version_tree *verdefs;
6251 struct elf_info_failed asvinfo;
5a580b3a
AM
6252 struct bfd_elf_version_tree *t;
6253 struct bfd_elf_version_expr *d;
902e9fc7 6254 asection *s;
e6699019 6255 size_t soname_indx;
7ee314fa 6256
5a580b3a
AM
6257 /* If we are supposed to export all symbols into the dynamic symbol
6258 table (this is not the normal case), then do so. */
55255dae 6259 if (info->export_dynamic
0e1862bb 6260 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6261 {
3d13f3e9
AM
6262 struct elf_info_failed eif;
6263
6264 eif.info = info;
6265 eif.failed = FALSE;
5a580b3a
AM
6266 elf_link_hash_traverse (elf_hash_table (info),
6267 _bfd_elf_export_symbol,
6268 &eif);
6269 if (eif.failed)
6270 return FALSE;
6271 }
6272
e6699019
L
6273 if (soname != NULL)
6274 {
6275 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6276 soname, TRUE);
6277 if (soname_indx == (size_t) -1
6278 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6279 return FALSE;
6280 }
6281 else
6282 soname_indx = (size_t) -1;
6283
5a580b3a 6284 /* Make all global versions with definition. */
fd91d419 6285 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6286 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6287 if (!d->symver && d->literal)
5a580b3a
AM
6288 {
6289 const char *verstr, *name;
6290 size_t namelen, verlen, newlen;
93252b1c 6291 char *newname, *p, leading_char;
5a580b3a
AM
6292 struct elf_link_hash_entry *newh;
6293
93252b1c 6294 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6295 name = d->pattern;
93252b1c 6296 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6297 verstr = t->name;
6298 verlen = strlen (verstr);
6299 newlen = namelen + verlen + 3;
6300
a50b1753 6301 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6302 if (newname == NULL)
6303 return FALSE;
93252b1c
MF
6304 newname[0] = leading_char;
6305 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6306
6307 /* Check the hidden versioned definition. */
6308 p = newname + namelen;
6309 *p++ = ELF_VER_CHR;
6310 memcpy (p, verstr, verlen + 1);
6311 newh = elf_link_hash_lookup (elf_hash_table (info),
6312 newname, FALSE, FALSE,
6313 FALSE);
6314 if (newh == NULL
6315 || (newh->root.type != bfd_link_hash_defined
6316 && newh->root.type != bfd_link_hash_defweak))
6317 {
6318 /* Check the default versioned definition. */
6319 *p++ = ELF_VER_CHR;
6320 memcpy (p, verstr, verlen + 1);
6321 newh = elf_link_hash_lookup (elf_hash_table (info),
6322 newname, FALSE, FALSE,
6323 FALSE);
6324 }
6325 free (newname);
6326
6327 /* Mark this version if there is a definition and it is
6328 not defined in a shared object. */
6329 if (newh != NULL
f5385ebf 6330 && !newh->def_dynamic
5a580b3a
AM
6331 && (newh->root.type == bfd_link_hash_defined
6332 || newh->root.type == bfd_link_hash_defweak))
6333 d->symver = 1;
6334 }
6335
6336 /* Attach all the symbols to their version information. */
5a580b3a 6337 asvinfo.info = info;
5a580b3a
AM
6338 asvinfo.failed = FALSE;
6339
6340 elf_link_hash_traverse (elf_hash_table (info),
6341 _bfd_elf_link_assign_sym_version,
6342 &asvinfo);
6343 if (asvinfo.failed)
6344 return FALSE;
6345
6346 if (!info->allow_undefined_version)
6347 {
6348 /* Check if all global versions have a definition. */
3d13f3e9 6349 bfd_boolean all_defined = TRUE;
fd91d419 6350 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6351 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6352 if (d->literal && !d->symver && !d->script)
5a580b3a 6353 {
4eca0228 6354 _bfd_error_handler
5a580b3a
AM
6355 (_("%s: undefined version: %s"),
6356 d->pattern, t->name);
6357 all_defined = FALSE;
6358 }
6359
6360 if (!all_defined)
6361 {
6362 bfd_set_error (bfd_error_bad_value);
6363 return FALSE;
6364 }
6365 }
6366
902e9fc7
MR
6367 /* Set up the version definition section. */
6368 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6369 BFD_ASSERT (s != NULL);
5a580b3a 6370
902e9fc7
MR
6371 /* We may have created additional version definitions if we are
6372 just linking a regular application. */
6373 verdefs = info->version_info;
5a580b3a 6374
902e9fc7
MR
6375 /* Skip anonymous version tag. */
6376 if (verdefs != NULL && verdefs->vernum == 0)
6377 verdefs = verdefs->next;
5a580b3a 6378
902e9fc7
MR
6379 if (verdefs == NULL && !info->create_default_symver)
6380 s->flags |= SEC_EXCLUDE;
6381 else
5a580b3a 6382 {
902e9fc7
MR
6383 unsigned int cdefs;
6384 bfd_size_type size;
6385 bfd_byte *p;
6386 Elf_Internal_Verdef def;
6387 Elf_Internal_Verdaux defaux;
6388 struct bfd_link_hash_entry *bh;
6389 struct elf_link_hash_entry *h;
6390 const char *name;
5a580b3a 6391
902e9fc7
MR
6392 cdefs = 0;
6393 size = 0;
5a580b3a 6394
902e9fc7
MR
6395 /* Make space for the base version. */
6396 size += sizeof (Elf_External_Verdef);
6397 size += sizeof (Elf_External_Verdaux);
6398 ++cdefs;
6399
6400 /* Make space for the default version. */
6401 if (info->create_default_symver)
6402 {
6403 size += sizeof (Elf_External_Verdef);
6404 ++cdefs;
3e3b46e5
PB
6405 }
6406
5a580b3a
AM
6407 for (t = verdefs; t != NULL; t = t->next)
6408 {
6409 struct bfd_elf_version_deps *n;
6410
a6cc6b3b
RO
6411 /* Don't emit base version twice. */
6412 if (t->vernum == 0)
6413 continue;
6414
5a580b3a
AM
6415 size += sizeof (Elf_External_Verdef);
6416 size += sizeof (Elf_External_Verdaux);
6417 ++cdefs;
6418
6419 for (n = t->deps; n != NULL; n = n->next)
6420 size += sizeof (Elf_External_Verdaux);
6421 }
6422
eea6121a 6423 s->size = size;
a50b1753 6424 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6425 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6426 return FALSE;
6427
6428 /* Fill in the version definition section. */
6429
6430 p = s->contents;
6431
6432 def.vd_version = VER_DEF_CURRENT;
6433 def.vd_flags = VER_FLG_BASE;
6434 def.vd_ndx = 1;
6435 def.vd_cnt = 1;
3e3b46e5
PB
6436 if (info->create_default_symver)
6437 {
6438 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6439 def.vd_next = sizeof (Elf_External_Verdef);
6440 }
6441 else
6442 {
6443 def.vd_aux = sizeof (Elf_External_Verdef);
6444 def.vd_next = (sizeof (Elf_External_Verdef)
6445 + sizeof (Elf_External_Verdaux));
6446 }
5a580b3a 6447
ef53be89 6448 if (soname_indx != (size_t) -1)
5a580b3a
AM
6449 {
6450 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6451 soname_indx);
6452 def.vd_hash = bfd_elf_hash (soname);
6453 defaux.vda_name = soname_indx;
3e3b46e5 6454 name = soname;
5a580b3a
AM
6455 }
6456 else
6457 {
ef53be89 6458 size_t indx;
5a580b3a 6459
06084812 6460 name = lbasename (output_bfd->filename);
5a580b3a
AM
6461 def.vd_hash = bfd_elf_hash (name);
6462 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6463 name, FALSE);
ef53be89 6464 if (indx == (size_t) -1)
5a580b3a
AM
6465 return FALSE;
6466 defaux.vda_name = indx;
6467 }
6468 defaux.vda_next = 0;
6469
6470 _bfd_elf_swap_verdef_out (output_bfd, &def,
6471 (Elf_External_Verdef *) p);
6472 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6473 if (info->create_default_symver)
6474 {
6475 /* Add a symbol representing this version. */
6476 bh = NULL;
6477 if (! (_bfd_generic_link_add_one_symbol
6478 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6479 0, NULL, FALSE,
6480 get_elf_backend_data (dynobj)->collect, &bh)))
6481 return FALSE;
6482 h = (struct elf_link_hash_entry *) bh;
6483 h->non_elf = 0;
6484 h->def_regular = 1;
6485 h->type = STT_OBJECT;
6486 h->verinfo.vertree = NULL;
6487
6488 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6489 return FALSE;
6490
6491 /* Create a duplicate of the base version with the same
6492 aux block, but different flags. */
6493 def.vd_flags = 0;
6494 def.vd_ndx = 2;
6495 def.vd_aux = sizeof (Elf_External_Verdef);
6496 if (verdefs)
6497 def.vd_next = (sizeof (Elf_External_Verdef)
6498 + sizeof (Elf_External_Verdaux));
6499 else
6500 def.vd_next = 0;
6501 _bfd_elf_swap_verdef_out (output_bfd, &def,
6502 (Elf_External_Verdef *) p);
6503 p += sizeof (Elf_External_Verdef);
6504 }
5a580b3a
AM
6505 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6506 (Elf_External_Verdaux *) p);
6507 p += sizeof (Elf_External_Verdaux);
6508
6509 for (t = verdefs; t != NULL; t = t->next)
6510 {
6511 unsigned int cdeps;
6512 struct bfd_elf_version_deps *n;
5a580b3a 6513
a6cc6b3b
RO
6514 /* Don't emit the base version twice. */
6515 if (t->vernum == 0)
6516 continue;
6517
5a580b3a
AM
6518 cdeps = 0;
6519 for (n = t->deps; n != NULL; n = n->next)
6520 ++cdeps;
6521
6522 /* Add a symbol representing this version. */
6523 bh = NULL;
6524 if (! (_bfd_generic_link_add_one_symbol
6525 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6526 0, NULL, FALSE,
6527 get_elf_backend_data (dynobj)->collect, &bh)))
6528 return FALSE;
6529 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6530 h->non_elf = 0;
6531 h->def_regular = 1;
5a580b3a
AM
6532 h->type = STT_OBJECT;
6533 h->verinfo.vertree = t;
6534
c152c796 6535 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6536 return FALSE;
6537
6538 def.vd_version = VER_DEF_CURRENT;
6539 def.vd_flags = 0;
6540 if (t->globals.list == NULL
6541 && t->locals.list == NULL
6542 && ! t->used)
6543 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6544 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6545 def.vd_cnt = cdeps + 1;
6546 def.vd_hash = bfd_elf_hash (t->name);
6547 def.vd_aux = sizeof (Elf_External_Verdef);
6548 def.vd_next = 0;
a6cc6b3b
RO
6549
6550 /* If a basever node is next, it *must* be the last node in
6551 the chain, otherwise Verdef construction breaks. */
6552 if (t->next != NULL && t->next->vernum == 0)
6553 BFD_ASSERT (t->next->next == NULL);
6554
6555 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6556 def.vd_next = (sizeof (Elf_External_Verdef)
6557 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6558
6559 _bfd_elf_swap_verdef_out (output_bfd, &def,
6560 (Elf_External_Verdef *) p);
6561 p += sizeof (Elf_External_Verdef);
6562
6563 defaux.vda_name = h->dynstr_index;
6564 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6565 h->dynstr_index);
6566 defaux.vda_next = 0;
6567 if (t->deps != NULL)
6568 defaux.vda_next = sizeof (Elf_External_Verdaux);
6569 t->name_indx = defaux.vda_name;
6570
6571 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6572 (Elf_External_Verdaux *) p);
6573 p += sizeof (Elf_External_Verdaux);
6574
6575 for (n = t->deps; n != NULL; n = n->next)
6576 {
6577 if (n->version_needed == NULL)
6578 {
6579 /* This can happen if there was an error in the
6580 version script. */
6581 defaux.vda_name = 0;
6582 }
6583 else
6584 {
6585 defaux.vda_name = n->version_needed->name_indx;
6586 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6587 defaux.vda_name);
6588 }
6589 if (n->next == NULL)
6590 defaux.vda_next = 0;
6591 else
6592 defaux.vda_next = sizeof (Elf_External_Verdaux);
6593
6594 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6595 (Elf_External_Verdaux *) p);
6596 p += sizeof (Elf_External_Verdaux);
6597 }
6598 }
6599
5a580b3a
AM
6600 elf_tdata (output_bfd)->cverdefs = cdefs;
6601 }
902e9fc7
MR
6602 }
6603
6604 bed = get_elf_backend_data (output_bfd);
6605
6606 if (info->gc_sections && bed->can_gc_sections)
6607 {
6608 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6609
6610 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6611 dynamic symbol table. */
902e9fc7
MR
6612 sweep_info.info = info;
6613 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6614 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6615 &sweep_info);
3d13f3e9
AM
6616 }
6617
6618 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6619 {
6620 asection *s;
6621 struct elf_find_verdep_info sinfo;
6622
6623 /* Work out the size of the version reference section. */
6624
6625 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6626 BFD_ASSERT (s != NULL);
902e9fc7 6627
3d13f3e9
AM
6628 sinfo.info = info;
6629 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6630 if (sinfo.vers == 0)
6631 sinfo.vers = 1;
6632 sinfo.failed = FALSE;
6633
6634 elf_link_hash_traverse (elf_hash_table (info),
6635 _bfd_elf_link_find_version_dependencies,
6636 &sinfo);
6637 if (sinfo.failed)
6638 return FALSE;
6639
6640 if (elf_tdata (output_bfd)->verref == NULL)
6641 s->flags |= SEC_EXCLUDE;
6642 else
6643 {
6644 Elf_Internal_Verneed *vn;
6645 unsigned int size;
6646 unsigned int crefs;
6647 bfd_byte *p;
6648
6649 /* Build the version dependency section. */
6650 size = 0;
6651 crefs = 0;
6652 for (vn = elf_tdata (output_bfd)->verref;
6653 vn != NULL;
6654 vn = vn->vn_nextref)
6655 {
6656 Elf_Internal_Vernaux *a;
6657
6658 size += sizeof (Elf_External_Verneed);
6659 ++crefs;
6660 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6661 size += sizeof (Elf_External_Vernaux);
6662 }
6663
6664 s->size = size;
6665 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6666 if (s->contents == NULL)
6667 return FALSE;
6668
6669 p = s->contents;
6670 for (vn = elf_tdata (output_bfd)->verref;
6671 vn != NULL;
6672 vn = vn->vn_nextref)
6673 {
6674 unsigned int caux;
6675 Elf_Internal_Vernaux *a;
6676 size_t indx;
6677
6678 caux = 0;
6679 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6680 ++caux;
6681
6682 vn->vn_version = VER_NEED_CURRENT;
6683 vn->vn_cnt = caux;
6684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685 elf_dt_name (vn->vn_bfd) != NULL
6686 ? elf_dt_name (vn->vn_bfd)
6687 : lbasename (vn->vn_bfd->filename),
6688 FALSE);
6689 if (indx == (size_t) -1)
6690 return FALSE;
6691 vn->vn_file = indx;
6692 vn->vn_aux = sizeof (Elf_External_Verneed);
6693 if (vn->vn_nextref == NULL)
6694 vn->vn_next = 0;
6695 else
6696 vn->vn_next = (sizeof (Elf_External_Verneed)
6697 + caux * sizeof (Elf_External_Vernaux));
6698
6699 _bfd_elf_swap_verneed_out (output_bfd, vn,
6700 (Elf_External_Verneed *) p);
6701 p += sizeof (Elf_External_Verneed);
6702
6703 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6704 {
6705 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6706 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6707 a->vna_nodename, FALSE);
6708 if (indx == (size_t) -1)
6709 return FALSE;
6710 a->vna_name = indx;
6711 if (a->vna_nextptr == NULL)
6712 a->vna_next = 0;
6713 else
6714 a->vna_next = sizeof (Elf_External_Vernaux);
6715
6716 _bfd_elf_swap_vernaux_out (output_bfd, a,
6717 (Elf_External_Vernaux *) p);
6718 p += sizeof (Elf_External_Vernaux);
6719 }
6720 }
6721
6722 elf_tdata (output_bfd)->cverrefs = crefs;
6723 }
902e9fc7
MR
6724 }
6725
6726 /* Any syms created from now on start with -1 in
6727 got.refcount/offset and plt.refcount/offset. */
6728 elf_hash_table (info)->init_got_refcount
6729 = elf_hash_table (info)->init_got_offset;
6730 elf_hash_table (info)->init_plt_refcount
6731 = elf_hash_table (info)->init_plt_offset;
6732
6733 if (bfd_link_relocatable (info)
6734 && !_bfd_elf_size_group_sections (info))
6735 return FALSE;
6736
6737 /* The backend may have to create some sections regardless of whether
6738 we're dynamic or not. */
6739 if (bed->elf_backend_always_size_sections
6740 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6741 return FALSE;
6742
6743 /* Determine any GNU_STACK segment requirements, after the backend
6744 has had a chance to set a default segment size. */
6745 if (info->execstack)
6746 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6747 else if (info->noexecstack)
6748 elf_stack_flags (output_bfd) = PF_R | PF_W;
6749 else
6750 {
6751 bfd *inputobj;
6752 asection *notesec = NULL;
6753 int exec = 0;
6754
6755 for (inputobj = info->input_bfds;
6756 inputobj;
6757 inputobj = inputobj->link.next)
6758 {
6759 asection *s;
6760
6761 if (inputobj->flags
6762 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6763 continue;
57963c05
AM
6764 s = inputobj->sections;
6765 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6766 continue;
6767
902e9fc7
MR
6768 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6769 if (s)
6770 {
6771 if (s->flags & SEC_CODE)
6772 exec = PF_X;
6773 notesec = s;
6774 }
6775 else if (bed->default_execstack)
6776 exec = PF_X;
6777 }
6778 if (notesec || info->stacksize > 0)
6779 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6780 if (notesec && exec && bfd_link_relocatable (info)
6781 && notesec->output_section != bfd_abs_section_ptr)
6782 notesec->output_section->flags |= SEC_CODE;
6783 }
6784
6785 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6786 {
6787 struct elf_info_failed eif;
6788 struct elf_link_hash_entry *h;
6789 asection *dynstr;
6790 asection *s;
6791
6792 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6793 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6794
902e9fc7
MR
6795 if (info->symbolic)
6796 {
6797 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6798 return FALSE;
6799 info->flags |= DF_SYMBOLIC;
6800 }
6801
6802 if (rpath != NULL)
6803 {
6804 size_t indx;
6805 bfd_vma tag;
6806
6807 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6808 TRUE);
6809 if (indx == (size_t) -1)
6810 return FALSE;
6811
6812 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6813 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6814 return FALSE;
6815 }
6816
6817 if (filter_shlib != NULL)
6818 {
6819 size_t indx;
6820
6821 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6822 filter_shlib, TRUE);
6823 if (indx == (size_t) -1
6824 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6825 return FALSE;
6826 }
6827
6828 if (auxiliary_filters != NULL)
6829 {
6830 const char * const *p;
6831
6832 for (p = auxiliary_filters; *p != NULL; p++)
6833 {
6834 size_t indx;
6835
6836 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6837 *p, TRUE);
6838 if (indx == (size_t) -1
6839 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6840 return FALSE;
6841 }
6842 }
6843
6844 if (audit != NULL)
6845 {
6846 size_t indx;
6847
6848 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6849 TRUE);
6850 if (indx == (size_t) -1
6851 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6852 return FALSE;
6853 }
6854
6855 if (depaudit != NULL)
6856 {
6857 size_t indx;
6858
6859 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6860 TRUE);
6861 if (indx == (size_t) -1
6862 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6863 return FALSE;
6864 }
6865
6866 eif.info = info;
6867 eif.failed = FALSE;
6868
6869 /* Find all symbols which were defined in a dynamic object and make
6870 the backend pick a reasonable value for them. */
6871 elf_link_hash_traverse (elf_hash_table (info),
6872 _bfd_elf_adjust_dynamic_symbol,
6873 &eif);
6874 if (eif.failed)
6875 return FALSE;
6876
6877 /* Add some entries to the .dynamic section. We fill in some of the
6878 values later, in bfd_elf_final_link, but we must add the entries
6879 now so that we know the final size of the .dynamic section. */
6880
6881 /* If there are initialization and/or finalization functions to
6882 call then add the corresponding DT_INIT/DT_FINI entries. */
6883 h = (info->init_function
6884 ? elf_link_hash_lookup (elf_hash_table (info),
6885 info->init_function, FALSE,
6886 FALSE, FALSE)
6887 : NULL);
6888 if (h != NULL
6889 && (h->ref_regular
6890 || h->def_regular))
6891 {
6892 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6893 return FALSE;
6894 }
6895 h = (info->fini_function
6896 ? elf_link_hash_lookup (elf_hash_table (info),
6897 info->fini_function, FALSE,
6898 FALSE, FALSE)
6899 : NULL);
6900 if (h != NULL
6901 && (h->ref_regular
6902 || h->def_regular))
6903 {
6904 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6905 return FALSE;
6906 }
6907
6908 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6909 if (s != NULL && s->linker_has_input)
6910 {
6911 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6912 if (! bfd_link_executable (info))
6913 {
6914 bfd *sub;
6915 asection *o;
6916
57963c05
AM
6917 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6918 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6919 && (o = sub->sections) != NULL
6920 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6921 for (o = sub->sections; o != NULL; o = o->next)
6922 if (elf_section_data (o)->this_hdr.sh_type
6923 == SHT_PREINIT_ARRAY)
6924 {
6925 _bfd_error_handler
871b3ab2 6926 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6927 sub);
6928 break;
6929 }
6930
6931 bfd_set_error (bfd_error_nonrepresentable_section);
6932 return FALSE;
6933 }
6934
6935 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6936 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6937 return FALSE;
6938 }
6939 s = bfd_get_section_by_name (output_bfd, ".init_array");
6940 if (s != NULL && s->linker_has_input)
6941 {
6942 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6943 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6944 return FALSE;
6945 }
6946 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6947 if (s != NULL && s->linker_has_input)
6948 {
6949 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6950 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6951 return FALSE;
6952 }
6953
6954 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6955 /* If .dynstr is excluded from the link, we don't want any of
6956 these tags. Strictly, we should be checking each section
6957 individually; This quick check covers for the case where
6958 someone does a /DISCARD/ : { *(*) }. */
6959 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6960 {
6961 bfd_size_type strsize;
6962
6963 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6964 if ((info->emit_hash
6965 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6966 || (info->emit_gnu_hash
6967 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6968 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6969 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6970 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6972 bed->s->sizeof_sym))
6973 return FALSE;
6974 }
6975 }
6976
6977 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6978 return FALSE;
6979
6980 /* The backend must work out the sizes of all the other dynamic
6981 sections. */
6982 if (dynobj != NULL
6983 && bed->elf_backend_size_dynamic_sections != NULL
6984 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6985 return FALSE;
6986
6987 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6988 {
902e9fc7
MR
6989 if (elf_tdata (output_bfd)->cverdefs)
6990 {
6991 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6992
6993 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6994 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6995 return FALSE;
6996 }
6997
6998 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6999 {
7000 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7001 return FALSE;
7002 }
7003 else if (info->flags & DF_BIND_NOW)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7006 return FALSE;
7007 }
7008
7009 if (info->flags_1)
7010 {
7011 if (bfd_link_executable (info))
7012 info->flags_1 &= ~ (DF_1_INITFIRST
7013 | DF_1_NODELETE
7014 | DF_1_NOOPEN);
7015 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7016 return FALSE;
7017 }
7018
7019 if (elf_tdata (output_bfd)->cverrefs)
7020 {
7021 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7022
7023 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7024 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7025 return FALSE;
7026 }
5a580b3a 7027
8423293d
AM
7028 if ((elf_tdata (output_bfd)->cverrefs == 0
7029 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7030 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7031 {
902e9fc7
MR
7032 asection *s;
7033
3d4d4302 7034 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7035 s->flags |= SEC_EXCLUDE;
7036 }
7037 }
7038 return TRUE;
7039}
7040
74541ad4
AM
7041/* Find the first non-excluded output section. We'll use its
7042 section symbol for some emitted relocs. */
7043void
7044_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7045{
7046 asection *s;
f26a3287 7047 asection *found = NULL;
74541ad4
AM
7048
7049 for (s = output_bfd->sections; s != NULL; s = s->next)
7050 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7051 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7052 {
f26a3287
AM
7053 found = s;
7054 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7055 break;
74541ad4 7056 }
f26a3287 7057 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7058}
7059
7060/* Find two non-excluded output sections, one for code, one for data.
7061 We'll use their section symbols for some emitted relocs. */
7062void
7063_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7064{
7065 asection *s;
f26a3287 7066 asection *found = NULL;
74541ad4 7067
266b05cf 7068 /* Data first, since setting text_index_section changes
7f923b7f 7069 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7070 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7071 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7072 && !(s->flags & SEC_READONLY)
d00dd7dc 7073 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7074 {
f26a3287
AM
7075 found = s;
7076 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7077 break;
74541ad4 7078 }
f26a3287 7079 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7080
7081 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7082 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7083 && (s->flags & SEC_READONLY)
d00dd7dc 7084 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7085 {
f26a3287 7086 found = s;
74541ad4
AM
7087 break;
7088 }
f26a3287 7089 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7090}
7091
8423293d
AM
7092bfd_boolean
7093bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7094{
74541ad4 7095 const struct elf_backend_data *bed;
23ec1e32 7096 unsigned long section_sym_count;
96d01d93 7097 bfd_size_type dynsymcount = 0;
74541ad4 7098
8423293d
AM
7099 if (!is_elf_hash_table (info->hash))
7100 return TRUE;
7101
74541ad4
AM
7102 bed = get_elf_backend_data (output_bfd);
7103 (*bed->elf_backend_init_index_section) (output_bfd, info);
7104
23ec1e32
MR
7105 /* Assign dynsym indices. In a shared library we generate a section
7106 symbol for each output section, which come first. Next come all
7107 of the back-end allocated local dynamic syms, followed by the rest
7108 of the global symbols.
7109
7110 This is usually not needed for static binaries, however backends
7111 can request to always do it, e.g. the MIPS backend uses dynamic
7112 symbol counts to lay out GOT, which will be produced in the
7113 presence of GOT relocations even in static binaries (holding fixed
7114 data in that case, to satisfy those relocations). */
7115
7116 if (elf_hash_table (info)->dynamic_sections_created
7117 || bed->always_renumber_dynsyms)
7118 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7119 &section_sym_count);
7120
8423293d
AM
7121 if (elf_hash_table (info)->dynamic_sections_created)
7122 {
7123 bfd *dynobj;
8423293d 7124 asection *s;
8423293d
AM
7125 unsigned int dtagcount;
7126
7127 dynobj = elf_hash_table (info)->dynobj;
7128
5a580b3a 7129 /* Work out the size of the symbol version section. */
3d4d4302 7130 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7131 BFD_ASSERT (s != NULL);
d5486c43 7132 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7133 {
eea6121a 7134 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7135 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7136 if (s->contents == NULL)
7137 return FALSE;
7138
7139 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7140 return FALSE;
7141 }
7142
7143 /* Set the size of the .dynsym and .hash sections. We counted
7144 the number of dynamic symbols in elf_link_add_object_symbols.
7145 We will build the contents of .dynsym and .hash when we build
7146 the final symbol table, because until then we do not know the
7147 correct value to give the symbols. We built the .dynstr
7148 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7149 s = elf_hash_table (info)->dynsym;
5a580b3a 7150 BFD_ASSERT (s != NULL);
eea6121a 7151 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7152
d5486c43
L
7153 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7154 if (s->contents == NULL)
7155 return FALSE;
5a580b3a 7156
d5486c43
L
7157 /* The first entry in .dynsym is a dummy symbol. Clear all the
7158 section syms, in case we don't output them all. */
7159 ++section_sym_count;
7160 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7161
fdc90cb4
JJ
7162 elf_hash_table (info)->bucketcount = 0;
7163
5a580b3a
AM
7164 /* Compute the size of the hashing table. As a side effect this
7165 computes the hash values for all the names we export. */
fdc90cb4
JJ
7166 if (info->emit_hash)
7167 {
7168 unsigned long int *hashcodes;
14b1c01e 7169 struct hash_codes_info hashinf;
fdc90cb4
JJ
7170 bfd_size_type amt;
7171 unsigned long int nsyms;
7172 size_t bucketcount;
7173 size_t hash_entry_size;
7174
7175 /* Compute the hash values for all exported symbols. At the same
7176 time store the values in an array so that we could use them for
7177 optimizations. */
7178 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7179 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7180 if (hashcodes == NULL)
7181 return FALSE;
14b1c01e
AM
7182 hashinf.hashcodes = hashcodes;
7183 hashinf.error = FALSE;
5a580b3a 7184
fdc90cb4
JJ
7185 /* Put all hash values in HASHCODES. */
7186 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7187 elf_collect_hash_codes, &hashinf);
7188 if (hashinf.error)
4dd07732
AM
7189 {
7190 free (hashcodes);
7191 return FALSE;
7192 }
5a580b3a 7193
14b1c01e 7194 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7195 bucketcount
7196 = compute_bucket_count (info, hashcodes, nsyms, 0);
7197 free (hashcodes);
7198
4b48e2f6 7199 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7200 return FALSE;
5a580b3a 7201
fdc90cb4
JJ
7202 elf_hash_table (info)->bucketcount = bucketcount;
7203
3d4d4302 7204 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7205 BFD_ASSERT (s != NULL);
7206 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7207 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7208 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7209 if (s->contents == NULL)
7210 return FALSE;
7211
7212 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7213 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7214 s->contents + hash_entry_size);
7215 }
7216
7217 if (info->emit_gnu_hash)
7218 {
7219 size_t i, cnt;
7220 unsigned char *contents;
7221 struct collect_gnu_hash_codes cinfo;
7222 bfd_size_type amt;
7223 size_t bucketcount;
7224
7225 memset (&cinfo, 0, sizeof (cinfo));
7226
7227 /* Compute the hash values for all exported symbols. At the same
7228 time store the values in an array so that we could use them for
7229 optimizations. */
7230 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7231 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7232 if (cinfo.hashcodes == NULL)
7233 return FALSE;
7234
7235 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7236 cinfo.min_dynindx = -1;
7237 cinfo.output_bfd = output_bfd;
7238 cinfo.bed = bed;
7239
7240 /* Put all hash values in HASHCODES. */
7241 elf_link_hash_traverse (elf_hash_table (info),
7242 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7243 if (cinfo.error)
4dd07732
AM
7244 {
7245 free (cinfo.hashcodes);
7246 return FALSE;
7247 }
fdc90cb4
JJ
7248
7249 bucketcount
7250 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7251
7252 if (bucketcount == 0)
7253 {
7254 free (cinfo.hashcodes);
7255 return FALSE;
7256 }
7257
3d4d4302 7258 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7259 BFD_ASSERT (s != NULL);
7260
7261 if (cinfo.nsyms == 0)
7262 {
7263 /* Empty .gnu.hash section is special. */
7264 BFD_ASSERT (cinfo.min_dynindx == -1);
7265 free (cinfo.hashcodes);
7266 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7267 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7268 if (contents == NULL)
7269 return FALSE;
7270 s->contents = contents;
7271 /* 1 empty bucket. */
7272 bfd_put_32 (output_bfd, 1, contents);
7273 /* SYMIDX above the special symbol 0. */
7274 bfd_put_32 (output_bfd, 1, contents + 4);
7275 /* Just one word for bitmask. */
7276 bfd_put_32 (output_bfd, 1, contents + 8);
7277 /* Only hash fn bloom filter. */
7278 bfd_put_32 (output_bfd, 0, contents + 12);
7279 /* No hashes are valid - empty bitmask. */
7280 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7281 /* No hashes in the only bucket. */
7282 bfd_put_32 (output_bfd, 0,
7283 contents + 16 + bed->s->arch_size / 8);
7284 }
7285 else
7286 {
9e6619e2 7287 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7288 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7289
9e6619e2
AM
7290 x = cinfo.nsyms;
7291 maskbitslog2 = 1;
7292 while ((x >>= 1) != 0)
7293 ++maskbitslog2;
fdc90cb4
JJ
7294 if (maskbitslog2 < 3)
7295 maskbitslog2 = 5;
7296 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7297 maskbitslog2 = maskbitslog2 + 3;
7298 else
7299 maskbitslog2 = maskbitslog2 + 2;
7300 if (bed->s->arch_size == 64)
7301 {
7302 if (maskbitslog2 == 5)
7303 maskbitslog2 = 6;
7304 cinfo.shift1 = 6;
7305 }
7306 else
7307 cinfo.shift1 = 5;
7308 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7309 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7310 cinfo.maskbits = 1 << maskbitslog2;
7311 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7312 amt = bucketcount * sizeof (unsigned long int) * 2;
7313 amt += maskwords * sizeof (bfd_vma);
a50b1753 7314 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7315 if (cinfo.bitmask == NULL)
7316 {
7317 free (cinfo.hashcodes);
7318 return FALSE;
7319 }
7320
a50b1753 7321 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7322 cinfo.indx = cinfo.counts + bucketcount;
7323 cinfo.symindx = dynsymcount - cinfo.nsyms;
7324 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7325
7326 /* Determine how often each hash bucket is used. */
7327 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7328 for (i = 0; i < cinfo.nsyms; ++i)
7329 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7330
7331 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7332 if (cinfo.counts[i] != 0)
7333 {
7334 cinfo.indx[i] = cnt;
7335 cnt += cinfo.counts[i];
7336 }
7337 BFD_ASSERT (cnt == dynsymcount);
7338 cinfo.bucketcount = bucketcount;
7339 cinfo.local_indx = cinfo.min_dynindx;
7340
7341 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7342 s->size += cinfo.maskbits / 8;
a50b1753 7343 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7344 if (contents == NULL)
7345 {
7346 free (cinfo.bitmask);
7347 free (cinfo.hashcodes);
7348 return FALSE;
7349 }
7350
7351 s->contents = contents;
7352 bfd_put_32 (output_bfd, bucketcount, contents);
7353 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7354 bfd_put_32 (output_bfd, maskwords, contents + 8);
7355 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7356 contents += 16 + cinfo.maskbits / 8;
7357
7358 for (i = 0; i < bucketcount; ++i)
7359 {
7360 if (cinfo.counts[i] == 0)
7361 bfd_put_32 (output_bfd, 0, contents);
7362 else
7363 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7364 contents += 4;
7365 }
7366
7367 cinfo.contents = contents;
7368
7369 /* Renumber dynamic symbols, populate .gnu.hash section. */
7370 elf_link_hash_traverse (elf_hash_table (info),
7371 elf_renumber_gnu_hash_syms, &cinfo);
7372
7373 contents = s->contents + 16;
7374 for (i = 0; i < maskwords; ++i)
7375 {
7376 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7377 contents);
7378 contents += bed->s->arch_size / 8;
7379 }
7380
7381 free (cinfo.bitmask);
7382 free (cinfo.hashcodes);
7383 }
7384 }
5a580b3a 7385
3d4d4302 7386 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7387 BFD_ASSERT (s != NULL);
7388
4ad4eba5 7389 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7390
eea6121a 7391 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7392
7393 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7394 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7395 return FALSE;
7396 }
7397
7398 return TRUE;
7399}
4d269e42 7400\f
4d269e42
AM
7401/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7402
7403static void
7404merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7405 asection *sec)
7406{
dbaa2011
AM
7407 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7408 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7409}
7410
7411/* Finish SHF_MERGE section merging. */
7412
7413bfd_boolean
630993ec 7414_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7415{
7416 bfd *ibfd;
7417 asection *sec;
7418
7419 if (!is_elf_hash_table (info->hash))
7420 return FALSE;
7421
c72f2fb2 7422 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7423 if ((ibfd->flags & DYNAMIC) == 0
7424 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7425 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7426 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7427 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7428 if ((sec->flags & SEC_MERGE) != 0
7429 && !bfd_is_abs_section (sec->output_section))
7430 {
7431 struct bfd_elf_section_data *secdata;
7432
7433 secdata = elf_section_data (sec);
630993ec 7434 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7435 &elf_hash_table (info)->merge_info,
7436 sec, &secdata->sec_info))
7437 return FALSE;
7438 else if (secdata->sec_info)
dbaa2011 7439 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7440 }
7441
7442 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7443 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7444 merge_sections_remove_hook);
7445 return TRUE;
7446}
7447
7448/* Create an entry in an ELF linker hash table. */
7449
7450struct bfd_hash_entry *
7451_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7452 struct bfd_hash_table *table,
7453 const char *string)
7454{
7455 /* Allocate the structure if it has not already been allocated by a
7456 subclass. */
7457 if (entry == NULL)
7458 {
a50b1753 7459 entry = (struct bfd_hash_entry *)
ca4be51c 7460 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7461 if (entry == NULL)
7462 return entry;
7463 }
7464
7465 /* Call the allocation method of the superclass. */
7466 entry = _bfd_link_hash_newfunc (entry, table, string);
7467 if (entry != NULL)
7468 {
7469 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7470 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7471
7472 /* Set local fields. */
7473 ret->indx = -1;
7474 ret->dynindx = -1;
7475 ret->got = htab->init_got_refcount;
7476 ret->plt = htab->init_plt_refcount;
7477 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7478 - offsetof (struct elf_link_hash_entry, size)));
7479 /* Assume that we have been called by a non-ELF symbol reader.
7480 This flag is then reset by the code which reads an ELF input
7481 file. This ensures that a symbol created by a non-ELF symbol
7482 reader will have the flag set correctly. */
7483 ret->non_elf = 1;
7484 }
7485
7486 return entry;
7487}
7488
7489/* Copy data from an indirect symbol to its direct symbol, hiding the
7490 old indirect symbol. Also used for copying flags to a weakdef. */
7491
7492void
7493_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7494 struct elf_link_hash_entry *dir,
7495 struct elf_link_hash_entry *ind)
7496{
7497 struct elf_link_hash_table *htab;
7498
7499 /* Copy down any references that we may have already seen to the
e81830c5 7500 symbol which just became indirect. */
4d269e42 7501
422f1182 7502 if (dir->versioned != versioned_hidden)
e81830c5
AM
7503 dir->ref_dynamic |= ind->ref_dynamic;
7504 dir->ref_regular |= ind->ref_regular;
7505 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7506 dir->non_got_ref |= ind->non_got_ref;
7507 dir->needs_plt |= ind->needs_plt;
7508 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7509
7510 if (ind->root.type != bfd_link_hash_indirect)
7511 return;
7512
7513 /* Copy over the global and procedure linkage table refcount entries.
7514 These may have been already set up by a check_relocs routine. */
7515 htab = elf_hash_table (info);
7516 if (ind->got.refcount > htab->init_got_refcount.refcount)
7517 {
7518 if (dir->got.refcount < 0)
7519 dir->got.refcount = 0;
7520 dir->got.refcount += ind->got.refcount;
7521 ind->got.refcount = htab->init_got_refcount.refcount;
7522 }
7523
7524 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7525 {
7526 if (dir->plt.refcount < 0)
7527 dir->plt.refcount = 0;
7528 dir->plt.refcount += ind->plt.refcount;
7529 ind->plt.refcount = htab->init_plt_refcount.refcount;
7530 }
7531
7532 if (ind->dynindx != -1)
7533 {
7534 if (dir->dynindx != -1)
7535 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7536 dir->dynindx = ind->dynindx;
7537 dir->dynstr_index = ind->dynstr_index;
7538 ind->dynindx = -1;
7539 ind->dynstr_index = 0;
7540 }
7541}
7542
7543void
7544_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7545 struct elf_link_hash_entry *h,
7546 bfd_boolean force_local)
7547{
3aa14d16
L
7548 /* STT_GNU_IFUNC symbol must go through PLT. */
7549 if (h->type != STT_GNU_IFUNC)
7550 {
7551 h->plt = elf_hash_table (info)->init_plt_offset;
7552 h->needs_plt = 0;
7553 }
4d269e42
AM
7554 if (force_local)
7555 {
7556 h->forced_local = 1;
7557 if (h->dynindx != -1)
7558 {
4d269e42
AM
7559 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7560 h->dynstr_index);
641338d8
AM
7561 h->dynindx = -1;
7562 h->dynstr_index = 0;
4d269e42
AM
7563 }
7564 }
7565}
7566
34a87bb0
L
7567/* Hide a symbol. */
7568
7569void
7570_bfd_elf_link_hide_symbol (bfd *output_bfd,
7571 struct bfd_link_info *info,
7572 struct bfd_link_hash_entry *h)
7573{
7574 if (is_elf_hash_table (info->hash))
7575 {
7576 const struct elf_backend_data *bed
7577 = get_elf_backend_data (output_bfd);
7578 struct elf_link_hash_entry *eh
7579 = (struct elf_link_hash_entry *) h;
7580 bed->elf_backend_hide_symbol (info, eh, TRUE);
7581 eh->def_dynamic = 0;
7582 eh->ref_dynamic = 0;
7583 eh->dynamic_def = 0;
7584 }
7585}
7586
7bf52ea2
AM
7587/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7588 caller. */
4d269e42
AM
7589
7590bfd_boolean
7591_bfd_elf_link_hash_table_init
7592 (struct elf_link_hash_table *table,
7593 bfd *abfd,
7594 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7595 struct bfd_hash_table *,
7596 const char *),
4dfe6ac6
NC
7597 unsigned int entsize,
7598 enum elf_target_id target_id)
4d269e42
AM
7599{
7600 bfd_boolean ret;
7601 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7602
4d269e42
AM
7603 table->init_got_refcount.refcount = can_refcount - 1;
7604 table->init_plt_refcount.refcount = can_refcount - 1;
7605 table->init_got_offset.offset = -(bfd_vma) 1;
7606 table->init_plt_offset.offset = -(bfd_vma) 1;
7607 /* The first dynamic symbol is a dummy. */
7608 table->dynsymcount = 1;
7609
7610 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7611
4d269e42 7612 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7613 table->hash_table_id = target_id;
4d269e42
AM
7614
7615 return ret;
7616}
7617
7618/* Create an ELF linker hash table. */
7619
7620struct bfd_link_hash_table *
7621_bfd_elf_link_hash_table_create (bfd *abfd)
7622{
7623 struct elf_link_hash_table *ret;
7624 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7625
7bf52ea2 7626 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7627 if (ret == NULL)
7628 return NULL;
7629
7630 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7631 sizeof (struct elf_link_hash_entry),
7632 GENERIC_ELF_DATA))
4d269e42
AM
7633 {
7634 free (ret);
7635 return NULL;
7636 }
d495ab0d 7637 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7638
7639 return &ret->root;
7640}
7641
9f7c3e5e
AM
7642/* Destroy an ELF linker hash table. */
7643
7644void
d495ab0d 7645_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7646{
d495ab0d
AM
7647 struct elf_link_hash_table *htab;
7648
7649 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7650 if (htab->dynstr != NULL)
7651 _bfd_elf_strtab_free (htab->dynstr);
7652 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7653 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7654}
7655
4d269e42
AM
7656/* This is a hook for the ELF emulation code in the generic linker to
7657 tell the backend linker what file name to use for the DT_NEEDED
7658 entry for a dynamic object. */
7659
7660void
7661bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7662{
7663 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7664 && bfd_get_format (abfd) == bfd_object)
7665 elf_dt_name (abfd) = name;
7666}
7667
7668int
7669bfd_elf_get_dyn_lib_class (bfd *abfd)
7670{
7671 int lib_class;
7672 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7673 && bfd_get_format (abfd) == bfd_object)
7674 lib_class = elf_dyn_lib_class (abfd);
7675 else
7676 lib_class = 0;
7677 return lib_class;
7678}
7679
7680void
7681bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7682{
7683 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7684 && bfd_get_format (abfd) == bfd_object)
7685 elf_dyn_lib_class (abfd) = lib_class;
7686}
7687
7688/* Get the list of DT_NEEDED entries for a link. This is a hook for
7689 the linker ELF emulation code. */
7690
7691struct bfd_link_needed_list *
7692bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7693 struct bfd_link_info *info)
7694{
7695 if (! is_elf_hash_table (info->hash))
7696 return NULL;
7697 return elf_hash_table (info)->needed;
7698}
7699
7700/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7701 hook for the linker ELF emulation code. */
7702
7703struct bfd_link_needed_list *
7704bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7705 struct bfd_link_info *info)
7706{
7707 if (! is_elf_hash_table (info->hash))
7708 return NULL;
7709 return elf_hash_table (info)->runpath;
7710}
7711
7712/* Get the name actually used for a dynamic object for a link. This
7713 is the SONAME entry if there is one. Otherwise, it is the string
7714 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7715
7716const char *
7717bfd_elf_get_dt_soname (bfd *abfd)
7718{
7719 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7720 && bfd_get_format (abfd) == bfd_object)
7721 return elf_dt_name (abfd);
7722 return NULL;
7723}
7724
7725/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7726 the ELF linker emulation code. */
7727
7728bfd_boolean
7729bfd_elf_get_bfd_needed_list (bfd *abfd,
7730 struct bfd_link_needed_list **pneeded)
7731{
7732 asection *s;
7733 bfd_byte *dynbuf = NULL;
cb33740c 7734 unsigned int elfsec;
4d269e42
AM
7735 unsigned long shlink;
7736 bfd_byte *extdyn, *extdynend;
7737 size_t extdynsize;
7738 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7739
7740 *pneeded = NULL;
7741
7742 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7743 || bfd_get_format (abfd) != bfd_object)
7744 return TRUE;
7745
7746 s = bfd_get_section_by_name (abfd, ".dynamic");
7747 if (s == NULL || s->size == 0)
7748 return TRUE;
7749
7750 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7751 goto error_return;
7752
7753 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7754 if (elfsec == SHN_BAD)
4d269e42
AM
7755 goto error_return;
7756
7757 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7758
4d269e42
AM
7759 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7760 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7761
7762 extdyn = dynbuf;
7763 extdynend = extdyn + s->size;
7764 for (; extdyn < extdynend; extdyn += extdynsize)
7765 {
7766 Elf_Internal_Dyn dyn;
7767
7768 (*swap_dyn_in) (abfd, extdyn, &dyn);
7769
7770 if (dyn.d_tag == DT_NULL)
7771 break;
7772
7773 if (dyn.d_tag == DT_NEEDED)
7774 {
7775 const char *string;
7776 struct bfd_link_needed_list *l;
7777 unsigned int tagv = dyn.d_un.d_val;
7778 bfd_size_type amt;
7779
7780 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7781 if (string == NULL)
7782 goto error_return;
7783
7784 amt = sizeof *l;
a50b1753 7785 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7786 if (l == NULL)
7787 goto error_return;
7788
7789 l->by = abfd;
7790 l->name = string;
7791 l->next = *pneeded;
7792 *pneeded = l;
7793 }
7794 }
7795
7796 free (dynbuf);
7797
7798 return TRUE;
7799
7800 error_return:
7801 if (dynbuf != NULL)
7802 free (dynbuf);
7803 return FALSE;
7804}
7805
7806struct elf_symbuf_symbol
7807{
7808 unsigned long st_name; /* Symbol name, index in string tbl */
7809 unsigned char st_info; /* Type and binding attributes */
7810 unsigned char st_other; /* Visibilty, and target specific */
7811};
7812
7813struct elf_symbuf_head
7814{
7815 struct elf_symbuf_symbol *ssym;
ef53be89 7816 size_t count;
4d269e42
AM
7817 unsigned int st_shndx;
7818};
7819
7820struct elf_symbol
7821{
7822 union
7823 {
7824 Elf_Internal_Sym *isym;
7825 struct elf_symbuf_symbol *ssym;
7826 } u;
7827 const char *name;
7828};
7829
7830/* Sort references to symbols by ascending section number. */
7831
7832static int
7833elf_sort_elf_symbol (const void *arg1, const void *arg2)
7834{
7835 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7836 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7837
7838 return s1->st_shndx - s2->st_shndx;
7839}
7840
7841static int
7842elf_sym_name_compare (const void *arg1, const void *arg2)
7843{
7844 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7845 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7846 return strcmp (s1->name, s2->name);
7847}
7848
7849static struct elf_symbuf_head *
ef53be89 7850elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7851{
14b1c01e 7852 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7853 struct elf_symbuf_symbol *ssym;
7854 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7855 size_t i, shndx_count, total_size;
4d269e42 7856
a50b1753 7857 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7858 if (indbuf == NULL)
7859 return NULL;
7860
7861 for (ind = indbuf, i = 0; i < symcount; i++)
7862 if (isymbuf[i].st_shndx != SHN_UNDEF)
7863 *ind++ = &isymbuf[i];
7864 indbufend = ind;
7865
7866 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7867 elf_sort_elf_symbol);
7868
7869 shndx_count = 0;
7870 if (indbufend > indbuf)
7871 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7872 if (ind[0]->st_shndx != ind[1]->st_shndx)
7873 shndx_count++;
7874
3ae181ee
L
7875 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7876 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7877 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7878 if (ssymbuf == NULL)
7879 {
7880 free (indbuf);
7881 return NULL;
7882 }
7883
3ae181ee 7884 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7885 ssymbuf->ssym = NULL;
7886 ssymbuf->count = shndx_count;
7887 ssymbuf->st_shndx = 0;
7888 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7889 {
7890 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7891 {
7892 ssymhead++;
7893 ssymhead->ssym = ssym;
7894 ssymhead->count = 0;
7895 ssymhead->st_shndx = (*ind)->st_shndx;
7896 }
7897 ssym->st_name = (*ind)->st_name;
7898 ssym->st_info = (*ind)->st_info;
7899 ssym->st_other = (*ind)->st_other;
7900 ssymhead->count++;
7901 }
ef53be89 7902 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7903 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7904 == total_size));
4d269e42
AM
7905
7906 free (indbuf);
7907 return ssymbuf;
7908}
7909
7910/* Check if 2 sections define the same set of local and global
7911 symbols. */
7912
8f317e31 7913static bfd_boolean
4d269e42
AM
7914bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7915 struct bfd_link_info *info)
7916{
7917 bfd *bfd1, *bfd2;
7918 const struct elf_backend_data *bed1, *bed2;
7919 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7920 size_t symcount1, symcount2;
4d269e42
AM
7921 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7922 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7923 Elf_Internal_Sym *isym, *isymend;
7924 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7925 size_t count1, count2, i;
cb33740c 7926 unsigned int shndx1, shndx2;
4d269e42
AM
7927 bfd_boolean result;
7928
7929 bfd1 = sec1->owner;
7930 bfd2 = sec2->owner;
7931
4d269e42
AM
7932 /* Both sections have to be in ELF. */
7933 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7934 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7935 return FALSE;
7936
7937 if (elf_section_type (sec1) != elf_section_type (sec2))
7938 return FALSE;
7939
4d269e42
AM
7940 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7941 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7942 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7943 return FALSE;
7944
7945 bed1 = get_elf_backend_data (bfd1);
7946 bed2 = get_elf_backend_data (bfd2);
7947 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7948 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7949 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7950 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7951
7952 if (symcount1 == 0 || symcount2 == 0)
7953 return FALSE;
7954
7955 result = FALSE;
7956 isymbuf1 = NULL;
7957 isymbuf2 = NULL;
a50b1753
NC
7958 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7959 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7960
7961 if (ssymbuf1 == NULL)
7962 {
7963 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7964 NULL, NULL, NULL);
7965 if (isymbuf1 == NULL)
7966 goto done;
7967
7968 if (!info->reduce_memory_overheads)
7969 elf_tdata (bfd1)->symbuf = ssymbuf1
7970 = elf_create_symbuf (symcount1, isymbuf1);
7971 }
7972
7973 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7974 {
7975 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7976 NULL, NULL, NULL);
7977 if (isymbuf2 == NULL)
7978 goto done;
7979
7980 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7981 elf_tdata (bfd2)->symbuf = ssymbuf2
7982 = elf_create_symbuf (symcount2, isymbuf2);
7983 }
7984
7985 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7986 {
7987 /* Optimized faster version. */
ef53be89 7988 size_t lo, hi, mid;
4d269e42
AM
7989 struct elf_symbol *symp;
7990 struct elf_symbuf_symbol *ssym, *ssymend;
7991
7992 lo = 0;
7993 hi = ssymbuf1->count;
7994 ssymbuf1++;
7995 count1 = 0;
7996 while (lo < hi)
7997 {
7998 mid = (lo + hi) / 2;
cb33740c 7999 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8000 hi = mid;
cb33740c 8001 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8002 lo = mid + 1;
8003 else
8004 {
8005 count1 = ssymbuf1[mid].count;
8006 ssymbuf1 += mid;
8007 break;
8008 }
8009 }
8010
8011 lo = 0;
8012 hi = ssymbuf2->count;
8013 ssymbuf2++;
8014 count2 = 0;
8015 while (lo < hi)
8016 {
8017 mid = (lo + hi) / 2;
cb33740c 8018 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8019 hi = mid;
cb33740c 8020 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8021 lo = mid + 1;
8022 else
8023 {
8024 count2 = ssymbuf2[mid].count;
8025 ssymbuf2 += mid;
8026 break;
8027 }
8028 }
8029
8030 if (count1 == 0 || count2 == 0 || count1 != count2)
8031 goto done;
8032
ca4be51c
AM
8033 symtable1
8034 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8035 symtable2
8036 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8037 if (symtable1 == NULL || symtable2 == NULL)
8038 goto done;
8039
8040 symp = symtable1;
8041 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8042 ssym < ssymend; ssym++, symp++)
8043 {
8044 symp->u.ssym = ssym;
8045 symp->name = bfd_elf_string_from_elf_section (bfd1,
8046 hdr1->sh_link,
8047 ssym->st_name);
8048 }
8049
8050 symp = symtable2;
8051 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8052 ssym < ssymend; ssym++, symp++)
8053 {
8054 symp->u.ssym = ssym;
8055 symp->name = bfd_elf_string_from_elf_section (bfd2,
8056 hdr2->sh_link,
8057 ssym->st_name);
8058 }
8059
8060 /* Sort symbol by name. */
8061 qsort (symtable1, count1, sizeof (struct elf_symbol),
8062 elf_sym_name_compare);
8063 qsort (symtable2, count1, sizeof (struct elf_symbol),
8064 elf_sym_name_compare);
8065
8066 for (i = 0; i < count1; i++)
8067 /* Two symbols must have the same binding, type and name. */
8068 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8069 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8070 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8071 goto done;
8072
8073 result = TRUE;
8074 goto done;
8075 }
8076
a50b1753
NC
8077 symtable1 = (struct elf_symbol *)
8078 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8079 symtable2 = (struct elf_symbol *)
8080 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8081 if (symtable1 == NULL || symtable2 == NULL)
8082 goto done;
8083
8084 /* Count definitions in the section. */
8085 count1 = 0;
8086 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8087 if (isym->st_shndx == shndx1)
4d269e42
AM
8088 symtable1[count1++].u.isym = isym;
8089
8090 count2 = 0;
8091 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8092 if (isym->st_shndx == shndx2)
4d269e42
AM
8093 symtable2[count2++].u.isym = isym;
8094
8095 if (count1 == 0 || count2 == 0 || count1 != count2)
8096 goto done;
8097
8098 for (i = 0; i < count1; i++)
8099 symtable1[i].name
8100 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8101 symtable1[i].u.isym->st_name);
8102
8103 for (i = 0; i < count2; i++)
8104 symtable2[i].name
8105 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8106 symtable2[i].u.isym->st_name);
8107
8108 /* Sort symbol by name. */
8109 qsort (symtable1, count1, sizeof (struct elf_symbol),
8110 elf_sym_name_compare);
8111 qsort (symtable2, count1, sizeof (struct elf_symbol),
8112 elf_sym_name_compare);
8113
8114 for (i = 0; i < count1; i++)
8115 /* Two symbols must have the same binding, type and name. */
8116 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8117 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8118 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8119 goto done;
8120
8121 result = TRUE;
8122
8123done:
8124 if (symtable1)
8125 free (symtable1);
8126 if (symtable2)
8127 free (symtable2);
8128 if (isymbuf1)
8129 free (isymbuf1);
8130 if (isymbuf2)
8131 free (isymbuf2);
8132
8133 return result;
8134}
8135
8136/* Return TRUE if 2 section types are compatible. */
8137
8138bfd_boolean
8139_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8140 bfd *bbfd, const asection *bsec)
8141{
8142 if (asec == NULL
8143 || bsec == NULL
8144 || abfd->xvec->flavour != bfd_target_elf_flavour
8145 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8146 return TRUE;
8147
8148 return elf_section_type (asec) == elf_section_type (bsec);
8149}
8150\f
c152c796
AM
8151/* Final phase of ELF linker. */
8152
8153/* A structure we use to avoid passing large numbers of arguments. */
8154
8155struct elf_final_link_info
8156{
8157 /* General link information. */
8158 struct bfd_link_info *info;
8159 /* Output BFD. */
8160 bfd *output_bfd;
8161 /* Symbol string table. */
ef10c3ac 8162 struct elf_strtab_hash *symstrtab;
c152c796
AM
8163 /* .hash section. */
8164 asection *hash_sec;
8165 /* symbol version section (.gnu.version). */
8166 asection *symver_sec;
8167 /* Buffer large enough to hold contents of any section. */
8168 bfd_byte *contents;
8169 /* Buffer large enough to hold external relocs of any section. */
8170 void *external_relocs;
8171 /* Buffer large enough to hold internal relocs of any section. */
8172 Elf_Internal_Rela *internal_relocs;
8173 /* Buffer large enough to hold external local symbols of any input
8174 BFD. */
8175 bfd_byte *external_syms;
8176 /* And a buffer for symbol section indices. */
8177 Elf_External_Sym_Shndx *locsym_shndx;
8178 /* Buffer large enough to hold internal local symbols of any input
8179 BFD. */
8180 Elf_Internal_Sym *internal_syms;
8181 /* Array large enough to hold a symbol index for each local symbol
8182 of any input BFD. */
8183 long *indices;
8184 /* Array large enough to hold a section pointer for each local
8185 symbol of any input BFD. */
8186 asection **sections;
ef10c3ac 8187 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8188 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8189 /* Number of STT_FILE syms seen. */
8190 size_t filesym_count;
c152c796
AM
8191};
8192
8193/* This struct is used to pass information to elf_link_output_extsym. */
8194
8195struct elf_outext_info
8196{
8197 bfd_boolean failed;
8198 bfd_boolean localsyms;
34a79995 8199 bfd_boolean file_sym_done;
8b127cbc 8200 struct elf_final_link_info *flinfo;
c152c796
AM
8201};
8202
d9352518
DB
8203
8204/* Support for evaluating a complex relocation.
8205
8206 Complex relocations are generalized, self-describing relocations. The
8207 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8208 relocations themselves.
d9352518
DB
8209
8210 The relocations are use a reserved elf-wide relocation type code (R_RELC
8211 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8212 information (start bit, end bit, word width, etc) into the addend. This
8213 information is extracted from CGEN-generated operand tables within gas.
8214
8215 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8216 internal) representing prefix-notation expressions, including but not
8217 limited to those sorts of expressions normally encoded as addends in the
8218 addend field. The symbol mangling format is:
8219
8220 <node> := <literal>
07d6d2b8
AM
8221 | <unary-operator> ':' <node>
8222 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8223 ;
8224
8225 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8226 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8227 | '#' <hexdigits>
8228 ;
8229
8230 <binary-operator> := as in C
8231 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8232
8233static void
a0c8462f
AM
8234set_symbol_value (bfd *bfd_with_globals,
8235 Elf_Internal_Sym *isymbuf,
8236 size_t locsymcount,
8237 size_t symidx,
8238 bfd_vma val)
d9352518 8239{
8977835c
AM
8240 struct elf_link_hash_entry **sym_hashes;
8241 struct elf_link_hash_entry *h;
8242 size_t extsymoff = locsymcount;
d9352518 8243
8977835c 8244 if (symidx < locsymcount)
d9352518 8245 {
8977835c
AM
8246 Elf_Internal_Sym *sym;
8247
8248 sym = isymbuf + symidx;
8249 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8250 {
8251 /* It is a local symbol: move it to the
8252 "absolute" section and give it a value. */
8253 sym->st_shndx = SHN_ABS;
8254 sym->st_value = val;
8255 return;
8256 }
8257 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8258 extsymoff = 0;
d9352518 8259 }
8977835c
AM
8260
8261 /* It is a global symbol: set its link type
8262 to "defined" and give it a value. */
8263
8264 sym_hashes = elf_sym_hashes (bfd_with_globals);
8265 h = sym_hashes [symidx - extsymoff];
8266 while (h->root.type == bfd_link_hash_indirect
8267 || h->root.type == bfd_link_hash_warning)
8268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8269 h->root.type = bfd_link_hash_defined;
8270 h->root.u.def.value = val;
8271 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8272}
8273
a0c8462f
AM
8274static bfd_boolean
8275resolve_symbol (const char *name,
8276 bfd *input_bfd,
8b127cbc 8277 struct elf_final_link_info *flinfo,
a0c8462f
AM
8278 bfd_vma *result,
8279 Elf_Internal_Sym *isymbuf,
8280 size_t locsymcount)
d9352518 8281{
a0c8462f
AM
8282 Elf_Internal_Sym *sym;
8283 struct bfd_link_hash_entry *global_entry;
8284 const char *candidate = NULL;
8285 Elf_Internal_Shdr *symtab_hdr;
8286 size_t i;
8287
d9352518
DB
8288 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8289
8290 for (i = 0; i < locsymcount; ++ i)
8291 {
8977835c 8292 sym = isymbuf + i;
d9352518
DB
8293
8294 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8295 continue;
8296
8297 candidate = bfd_elf_string_from_elf_section (input_bfd,
8298 symtab_hdr->sh_link,
8299 sym->st_name);
8300#ifdef DEBUG
0f02bbd9
AM
8301 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8302 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8303#endif
8304 if (candidate && strcmp (candidate, name) == 0)
8305 {
8b127cbc 8306 asection *sec = flinfo->sections [i];
d9352518 8307
0f02bbd9
AM
8308 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8309 *result += sec->output_offset + sec->output_section->vma;
d9352518 8310#ifdef DEBUG
0f02bbd9
AM
8311 printf ("Found symbol with value %8.8lx\n",
8312 (unsigned long) *result);
d9352518
DB
8313#endif
8314 return TRUE;
8315 }
8316 }
8317
8318 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8319 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8320 FALSE, FALSE, TRUE);
d9352518
DB
8321 if (!global_entry)
8322 return FALSE;
a0c8462f 8323
d9352518
DB
8324 if (global_entry->type == bfd_link_hash_defined
8325 || global_entry->type == bfd_link_hash_defweak)
8326 {
a0c8462f
AM
8327 *result = (global_entry->u.def.value
8328 + global_entry->u.def.section->output_section->vma
8329 + global_entry->u.def.section->output_offset);
d9352518 8330#ifdef DEBUG
0f02bbd9
AM
8331 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8332 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8333#endif
8334 return TRUE;
a0c8462f 8335 }
d9352518 8336
d9352518
DB
8337 return FALSE;
8338}
8339
37b01f6a
DG
8340/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8341 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8342 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8343
d9352518 8344static bfd_boolean
a0c8462f
AM
8345resolve_section (const char *name,
8346 asection *sections,
37b01f6a
DG
8347 bfd_vma *result,
8348 bfd * abfd)
d9352518 8349{
a0c8462f
AM
8350 asection *curr;
8351 unsigned int len;
d9352518 8352
a0c8462f 8353 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8354 if (strcmp (curr->name, name) == 0)
8355 {
8356 *result = curr->vma;
8357 return TRUE;
8358 }
8359
8360 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8361 /* FIXME: This could be coded more efficiently... */
a0c8462f 8362 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8363 {
8364 len = strlen (curr->name);
a0c8462f 8365 if (len > strlen (name))
d9352518
DB
8366 continue;
8367
8368 if (strncmp (curr->name, name, len) == 0)
8369 {
8370 if (strncmp (".end", name + len, 4) == 0)
8371 {
37b01f6a 8372 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8373 return TRUE;
8374 }
8375
8376 /* Insert more pseudo-section names here, if you like. */
8377 }
8378 }
a0c8462f 8379
d9352518
DB
8380 return FALSE;
8381}
8382
8383static void
a0c8462f 8384undefined_reference (const char *reftype, const char *name)
d9352518 8385{
695344c0 8386 /* xgettext:c-format */
a0c8462f
AM
8387 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8388 reftype, name);
d9352518
DB
8389}
8390
8391static bfd_boolean
a0c8462f
AM
8392eval_symbol (bfd_vma *result,
8393 const char **symp,
8394 bfd *input_bfd,
8b127cbc 8395 struct elf_final_link_info *flinfo,
a0c8462f
AM
8396 bfd_vma dot,
8397 Elf_Internal_Sym *isymbuf,
8398 size_t locsymcount,
8399 int signed_p)
d9352518 8400{
4b93929b
NC
8401 size_t len;
8402 size_t symlen;
a0c8462f
AM
8403 bfd_vma a;
8404 bfd_vma b;
4b93929b 8405 char symbuf[4096];
0f02bbd9 8406 const char *sym = *symp;
a0c8462f
AM
8407 const char *symend;
8408 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8409
8410 len = strlen (sym);
8411 symend = sym + len;
8412
4b93929b 8413 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8414 {
8415 bfd_set_error (bfd_error_invalid_operation);
8416 return FALSE;
8417 }
a0c8462f 8418
d9352518
DB
8419 switch (* sym)
8420 {
8421 case '.':
0f02bbd9
AM
8422 *result = dot;
8423 *symp = sym + 1;
d9352518
DB
8424 return TRUE;
8425
8426 case '#':
0f02bbd9
AM
8427 ++sym;
8428 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8429 return TRUE;
8430
8431 case 'S':
8432 symbol_is_section = TRUE;
1a0670f3 8433 /* Fall through. */
a0c8462f 8434 case 's':
0f02bbd9
AM
8435 ++sym;
8436 symlen = strtol (sym, (char **) symp, 10);
8437 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8438
4b93929b 8439 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8440 {
8441 bfd_set_error (bfd_error_invalid_operation);
8442 return FALSE;
8443 }
8444
8445 memcpy (symbuf, sym, symlen);
a0c8462f 8446 symbuf[symlen] = '\0';
0f02bbd9 8447 *symp = sym + symlen;
a0c8462f
AM
8448
8449 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8450 the symbol as a section, or vice-versa. so we're pretty liberal in our
8451 interpretation here; section means "try section first", not "must be a
8452 section", and likewise with symbol. */
8453
a0c8462f 8454 if (symbol_is_section)
d9352518 8455 {
37b01f6a 8456 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8457 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8458 isymbuf, locsymcount))
d9352518
DB
8459 {
8460 undefined_reference ("section", symbuf);
8461 return FALSE;
8462 }
a0c8462f
AM
8463 }
8464 else
d9352518 8465 {
8b127cbc 8466 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8467 isymbuf, locsymcount)
8b127cbc 8468 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8469 result, input_bfd))
d9352518
DB
8470 {
8471 undefined_reference ("symbol", symbuf);
8472 return FALSE;
8473 }
8474 }
8475
8476 return TRUE;
a0c8462f 8477
d9352518
DB
8478 /* All that remains are operators. */
8479
8480#define UNARY_OP(op) \
8481 if (strncmp (sym, #op, strlen (#op)) == 0) \
8482 { \
8483 sym += strlen (#op); \
a0c8462f
AM
8484 if (*sym == ':') \
8485 ++sym; \
0f02bbd9 8486 *symp = sym; \
8b127cbc 8487 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8488 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8489 return FALSE; \
8490 if (signed_p) \
0f02bbd9 8491 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8492 else \
8493 *result = op a; \
d9352518
DB
8494 return TRUE; \
8495 }
8496
8497#define BINARY_OP(op) \
8498 if (strncmp (sym, #op, strlen (#op)) == 0) \
8499 { \
8500 sym += strlen (#op); \
a0c8462f
AM
8501 if (*sym == ':') \
8502 ++sym; \
0f02bbd9 8503 *symp = sym; \
8b127cbc 8504 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8505 isymbuf, locsymcount, signed_p)) \
a0c8462f 8506 return FALSE; \
0f02bbd9 8507 ++*symp; \
8b127cbc 8508 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8509 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8510 return FALSE; \
8511 if (signed_p) \
0f02bbd9 8512 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8513 else \
8514 *result = a op b; \
d9352518
DB
8515 return TRUE; \
8516 }
8517
8518 default:
8519 UNARY_OP (0-);
8520 BINARY_OP (<<);
8521 BINARY_OP (>>);
8522 BINARY_OP (==);
8523 BINARY_OP (!=);
8524 BINARY_OP (<=);
8525 BINARY_OP (>=);
8526 BINARY_OP (&&);
8527 BINARY_OP (||);
8528 UNARY_OP (~);
8529 UNARY_OP (!);
8530 BINARY_OP (*);
8531 BINARY_OP (/);
8532 BINARY_OP (%);
8533 BINARY_OP (^);
8534 BINARY_OP (|);
8535 BINARY_OP (&);
8536 BINARY_OP (+);
8537 BINARY_OP (-);
8538 BINARY_OP (<);
8539 BINARY_OP (>);
8540#undef UNARY_OP
8541#undef BINARY_OP
8542 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8543 bfd_set_error (bfd_error_invalid_operation);
8544 return FALSE;
8545 }
8546}
8547
d9352518 8548static void
a0c8462f
AM
8549put_value (bfd_vma size,
8550 unsigned long chunksz,
8551 bfd *input_bfd,
8552 bfd_vma x,
8553 bfd_byte *location)
d9352518
DB
8554{
8555 location += (size - chunksz);
8556
41cd1ad1 8557 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8558 {
8559 switch (chunksz)
8560 {
d9352518
DB
8561 case 1:
8562 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8563 x >>= 8;
d9352518
DB
8564 break;
8565 case 2:
8566 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8567 x >>= 16;
d9352518
DB
8568 break;
8569 case 4:
8570 bfd_put_32 (input_bfd, x, location);
65164438
NC
8571 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8572 x >>= 16;
8573 x >>= 16;
d9352518 8574 break;
d9352518 8575#ifdef BFD64
41cd1ad1 8576 case 8:
d9352518 8577 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8578 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8579 x >>= 32;
8580 x >>= 32;
8581 break;
d9352518 8582#endif
41cd1ad1
NC
8583 default:
8584 abort ();
d9352518
DB
8585 break;
8586 }
8587 }
8588}
8589
a0c8462f
AM
8590static bfd_vma
8591get_value (bfd_vma size,
8592 unsigned long chunksz,
8593 bfd *input_bfd,
8594 bfd_byte *location)
d9352518 8595{
9b239e0e 8596 int shift;
d9352518
DB
8597 bfd_vma x = 0;
8598
9b239e0e
NC
8599 /* Sanity checks. */
8600 BFD_ASSERT (chunksz <= sizeof (x)
8601 && size >= chunksz
8602 && chunksz != 0
8603 && (size % chunksz) == 0
8604 && input_bfd != NULL
8605 && location != NULL);
8606
8607 if (chunksz == sizeof (x))
8608 {
8609 BFD_ASSERT (size == chunksz);
8610
8611 /* Make sure that we do not perform an undefined shift operation.
8612 We know that size == chunksz so there will only be one iteration
8613 of the loop below. */
8614 shift = 0;
8615 }
8616 else
8617 shift = 8 * chunksz;
8618
a0c8462f 8619 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8620 {
8621 switch (chunksz)
8622 {
d9352518 8623 case 1:
9b239e0e 8624 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8625 break;
8626 case 2:
9b239e0e 8627 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8628 break;
8629 case 4:
9b239e0e 8630 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8631 break;
d9352518 8632#ifdef BFD64
9b239e0e
NC
8633 case 8:
8634 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8635 break;
9b239e0e
NC
8636#endif
8637 default:
8638 abort ();
d9352518
DB
8639 }
8640 }
8641 return x;
8642}
8643
a0c8462f
AM
8644static void
8645decode_complex_addend (unsigned long *start, /* in bits */
8646 unsigned long *oplen, /* in bits */
8647 unsigned long *len, /* in bits */
8648 unsigned long *wordsz, /* in bytes */
8649 unsigned long *chunksz, /* in bytes */
8650 unsigned long *lsb0_p,
8651 unsigned long *signed_p,
8652 unsigned long *trunc_p,
8653 unsigned long encoded)
d9352518 8654{
07d6d2b8
AM
8655 * start = encoded & 0x3F;
8656 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8657 * oplen = (encoded >> 12) & 0x3F;
8658 * wordsz = (encoded >> 18) & 0xF;
8659 * chunksz = (encoded >> 22) & 0xF;
8660 * lsb0_p = (encoded >> 27) & 1;
8661 * signed_p = (encoded >> 28) & 1;
8662 * trunc_p = (encoded >> 29) & 1;
8663}
8664
cdfeee4f 8665bfd_reloc_status_type
0f02bbd9 8666bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8667 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8668 bfd_byte *contents,
8669 Elf_Internal_Rela *rel,
8670 bfd_vma relocation)
d9352518 8671{
0f02bbd9
AM
8672 bfd_vma shift, x, mask;
8673 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8674 bfd_reloc_status_type r;
d9352518
DB
8675
8676 /* Perform this reloc, since it is complex.
8677 (this is not to say that it necessarily refers to a complex
8678 symbol; merely that it is a self-describing CGEN based reloc.
8679 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8680 word size, etc) encoded within it.). */
d9352518 8681
a0c8462f
AM
8682 decode_complex_addend (&start, &oplen, &len, &wordsz,
8683 &chunksz, &lsb0_p, &signed_p,
8684 &trunc_p, rel->r_addend);
d9352518
DB
8685
8686 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8687
8688 if (lsb0_p)
8689 shift = (start + 1) - len;
8690 else
8691 shift = (8 * wordsz) - (start + len);
8692
37b01f6a
DG
8693 x = get_value (wordsz, chunksz, input_bfd,
8694 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8695
8696#ifdef DEBUG
8697 printf ("Doing complex reloc: "
8698 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8699 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8700 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8701 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8702 oplen, (unsigned long) x, (unsigned long) mask,
8703 (unsigned long) relocation);
d9352518
DB
8704#endif
8705
cdfeee4f 8706 r = bfd_reloc_ok;
d9352518 8707 if (! trunc_p)
cdfeee4f
AM
8708 /* Now do an overflow check. */
8709 r = bfd_check_overflow ((signed_p
8710 ? complain_overflow_signed
8711 : complain_overflow_unsigned),
8712 len, 0, (8 * wordsz),
8713 relocation);
a0c8462f 8714
d9352518
DB
8715 /* Do the deed. */
8716 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8717
8718#ifdef DEBUG
8719 printf (" relocation: %8.8lx\n"
8720 " shifted mask: %8.8lx\n"
8721 " shifted/masked reloc: %8.8lx\n"
8722 " result: %8.8lx\n",
9ccb8af9
AM
8723 (unsigned long) relocation, (unsigned long) (mask << shift),
8724 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8725#endif
37b01f6a
DG
8726 put_value (wordsz, chunksz, input_bfd, x,
8727 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8728 return r;
d9352518
DB
8729}
8730
0e287786
AM
8731/* Functions to read r_offset from external (target order) reloc
8732 entry. Faster than bfd_getl32 et al, because we let the compiler
8733 know the value is aligned. */
53df40a4 8734
0e287786
AM
8735static bfd_vma
8736ext32l_r_offset (const void *p)
53df40a4
AM
8737{
8738 union aligned32
8739 {
8740 uint32_t v;
8741 unsigned char c[4];
8742 };
8743 const union aligned32 *a
0e287786 8744 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8745
8746 uint32_t aval = ( (uint32_t) a->c[0]
8747 | (uint32_t) a->c[1] << 8
8748 | (uint32_t) a->c[2] << 16
8749 | (uint32_t) a->c[3] << 24);
0e287786 8750 return aval;
53df40a4
AM
8751}
8752
0e287786
AM
8753static bfd_vma
8754ext32b_r_offset (const void *p)
53df40a4
AM
8755{
8756 union aligned32
8757 {
8758 uint32_t v;
8759 unsigned char c[4];
8760 };
8761 const union aligned32 *a
0e287786 8762 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8763
8764 uint32_t aval = ( (uint32_t) a->c[0] << 24
8765 | (uint32_t) a->c[1] << 16
8766 | (uint32_t) a->c[2] << 8
8767 | (uint32_t) a->c[3]);
0e287786 8768 return aval;
53df40a4
AM
8769}
8770
8771#ifdef BFD_HOST_64_BIT
0e287786
AM
8772static bfd_vma
8773ext64l_r_offset (const void *p)
53df40a4
AM
8774{
8775 union aligned64
8776 {
8777 uint64_t v;
8778 unsigned char c[8];
8779 };
8780 const union aligned64 *a
0e287786 8781 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8782
8783 uint64_t aval = ( (uint64_t) a->c[0]
8784 | (uint64_t) a->c[1] << 8
8785 | (uint64_t) a->c[2] << 16
8786 | (uint64_t) a->c[3] << 24
8787 | (uint64_t) a->c[4] << 32
8788 | (uint64_t) a->c[5] << 40
8789 | (uint64_t) a->c[6] << 48
8790 | (uint64_t) a->c[7] << 56);
0e287786 8791 return aval;
53df40a4
AM
8792}
8793
0e287786
AM
8794static bfd_vma
8795ext64b_r_offset (const void *p)
53df40a4
AM
8796{
8797 union aligned64
8798 {
8799 uint64_t v;
8800 unsigned char c[8];
8801 };
8802 const union aligned64 *a
0e287786 8803 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8804
8805 uint64_t aval = ( (uint64_t) a->c[0] << 56
8806 | (uint64_t) a->c[1] << 48
8807 | (uint64_t) a->c[2] << 40
8808 | (uint64_t) a->c[3] << 32
8809 | (uint64_t) a->c[4] << 24
8810 | (uint64_t) a->c[5] << 16
8811 | (uint64_t) a->c[6] << 8
8812 | (uint64_t) a->c[7]);
0e287786 8813 return aval;
53df40a4
AM
8814}
8815#endif
8816
c152c796
AM
8817/* When performing a relocatable link, the input relocations are
8818 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8819 referenced must be updated. Update all the relocations found in
8820 RELDATA. */
c152c796 8821
bca6d0e3 8822static bfd_boolean
c152c796 8823elf_link_adjust_relocs (bfd *abfd,
9eaff861 8824 asection *sec,
28dbcedc 8825 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8826 bfd_boolean sort,
8827 struct bfd_link_info *info)
c152c796
AM
8828{
8829 unsigned int i;
8830 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8831 bfd_byte *erela;
8832 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8833 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8834 bfd_vma r_type_mask;
8835 int r_sym_shift;
d4730f92
BS
8836 unsigned int count = reldata->count;
8837 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8838
d4730f92 8839 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8840 {
8841 swap_in = bed->s->swap_reloc_in;
8842 swap_out = bed->s->swap_reloc_out;
8843 }
d4730f92 8844 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8845 {
8846 swap_in = bed->s->swap_reloca_in;
8847 swap_out = bed->s->swap_reloca_out;
8848 }
8849 else
8850 abort ();
8851
8852 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8853 abort ();
8854
8855 if (bed->s->arch_size == 32)
8856 {
8857 r_type_mask = 0xff;
8858 r_sym_shift = 8;
8859 }
8860 else
8861 {
8862 r_type_mask = 0xffffffff;
8863 r_sym_shift = 32;
8864 }
8865
d4730f92
BS
8866 erela = reldata->hdr->contents;
8867 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8868 {
8869 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8870 unsigned int j;
8871
8872 if (*rel_hash == NULL)
8873 continue;
8874
10bbbc1d
NC
8875 if ((*rel_hash)->indx == -2
8876 && info->gc_sections
8877 && ! info->gc_keep_exported)
8878 {
8879 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8880 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8881 abfd, sec,
8882 (*rel_hash)->root.root.string);
9793eb77 8883 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8884 abfd, sec);
10bbbc1d
NC
8885 bfd_set_error (bfd_error_invalid_operation);
8886 return FALSE;
8887 }
c152c796
AM
8888 BFD_ASSERT ((*rel_hash)->indx >= 0);
8889
8890 (*swap_in) (abfd, erela, irela);
8891 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8892 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8893 | (irela[j].r_info & r_type_mask));
8894 (*swap_out) (abfd, irela, erela);
8895 }
53df40a4 8896
9eaff861
AO
8897 if (bed->elf_backend_update_relocs)
8898 (*bed->elf_backend_update_relocs) (sec, reldata);
8899
0e287786 8900 if (sort && count != 0)
53df40a4 8901 {
0e287786
AM
8902 bfd_vma (*ext_r_off) (const void *);
8903 bfd_vma r_off;
8904 size_t elt_size;
8905 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8906 bfd_byte *buf = NULL;
28dbcedc
AM
8907
8908 if (bed->s->arch_size == 32)
8909 {
8910 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8911 ext_r_off = ext32l_r_offset;
28dbcedc 8912 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8913 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8914 else
8915 abort ();
8916 }
53df40a4 8917 else
28dbcedc 8918 {
53df40a4 8919#ifdef BFD_HOST_64_BIT
28dbcedc 8920 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8921 ext_r_off = ext64l_r_offset;
28dbcedc 8922 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8923 ext_r_off = ext64b_r_offset;
28dbcedc 8924 else
53df40a4 8925#endif
28dbcedc
AM
8926 abort ();
8927 }
0e287786 8928
bca6d0e3
AM
8929 /* Must use a stable sort here. A modified insertion sort,
8930 since the relocs are mostly sorted already. */
0e287786
AM
8931 elt_size = reldata->hdr->sh_entsize;
8932 base = reldata->hdr->contents;
8933 end = base + count * elt_size;
bca6d0e3 8934 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8935 abort ();
8936
8937 /* Ensure the first element is lowest. This acts as a sentinel,
8938 speeding the main loop below. */
8939 r_off = (*ext_r_off) (base);
8940 for (p = loc = base; (p += elt_size) < end; )
8941 {
8942 bfd_vma r_off2 = (*ext_r_off) (p);
8943 if (r_off > r_off2)
8944 {
8945 r_off = r_off2;
8946 loc = p;
8947 }
8948 }
8949 if (loc != base)
8950 {
8951 /* Don't just swap *base and *loc as that changes the order
8952 of the original base[0] and base[1] if they happen to
8953 have the same r_offset. */
bca6d0e3
AM
8954 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8955 memcpy (onebuf, loc, elt_size);
0e287786 8956 memmove (base + elt_size, base, loc - base);
bca6d0e3 8957 memcpy (base, onebuf, elt_size);
0e287786
AM
8958 }
8959
b29b8669 8960 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8961 {
8962 /* base to p is sorted, *p is next to insert. */
8963 r_off = (*ext_r_off) (p);
8964 /* Search the sorted region for location to insert. */
8965 loc = p - elt_size;
8966 while (r_off < (*ext_r_off) (loc))
8967 loc -= elt_size;
8968 loc += elt_size;
8969 if (loc != p)
8970 {
bca6d0e3
AM
8971 /* Chances are there is a run of relocs to insert here,
8972 from one of more input files. Files are not always
8973 linked in order due to the way elf_link_input_bfd is
8974 called. See pr17666. */
8975 size_t sortlen = p - loc;
8976 bfd_vma r_off2 = (*ext_r_off) (loc);
8977 size_t runlen = elt_size;
8978 size_t buf_size = 96 * 1024;
8979 while (p + runlen < end
8980 && (sortlen <= buf_size
8981 || runlen + elt_size <= buf_size)
8982 && r_off2 > (*ext_r_off) (p + runlen))
8983 runlen += elt_size;
8984 if (buf == NULL)
8985 {
8986 buf = bfd_malloc (buf_size);
8987 if (buf == NULL)
8988 return FALSE;
8989 }
8990 if (runlen < sortlen)
8991 {
8992 memcpy (buf, p, runlen);
8993 memmove (loc + runlen, loc, sortlen);
8994 memcpy (loc, buf, runlen);
8995 }
8996 else
8997 {
8998 memcpy (buf, loc, sortlen);
8999 memmove (loc, p, runlen);
9000 memcpy (loc + runlen, buf, sortlen);
9001 }
b29b8669 9002 p += runlen - elt_size;
0e287786
AM
9003 }
9004 }
9005 /* Hashes are no longer valid. */
28dbcedc
AM
9006 free (reldata->hashes);
9007 reldata->hashes = NULL;
bca6d0e3 9008 free (buf);
53df40a4 9009 }
bca6d0e3 9010 return TRUE;
c152c796
AM
9011}
9012
9013struct elf_link_sort_rela
9014{
9015 union {
9016 bfd_vma offset;
9017 bfd_vma sym_mask;
9018 } u;
9019 enum elf_reloc_type_class type;
9020 /* We use this as an array of size int_rels_per_ext_rel. */
9021 Elf_Internal_Rela rela[1];
9022};
9023
9024static int
9025elf_link_sort_cmp1 (const void *A, const void *B)
9026{
a50b1753
NC
9027 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9028 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9029 int relativea, relativeb;
9030
9031 relativea = a->type == reloc_class_relative;
9032 relativeb = b->type == reloc_class_relative;
9033
9034 if (relativea < relativeb)
9035 return 1;
9036 if (relativea > relativeb)
9037 return -1;
9038 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9039 return -1;
9040 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9041 return 1;
9042 if (a->rela->r_offset < b->rela->r_offset)
9043 return -1;
9044 if (a->rela->r_offset > b->rela->r_offset)
9045 return 1;
9046 return 0;
9047}
9048
9049static int
9050elf_link_sort_cmp2 (const void *A, const void *B)
9051{
a50b1753
NC
9052 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9053 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9054
7e612e98 9055 if (a->type < b->type)
c152c796 9056 return -1;
7e612e98 9057 if (a->type > b->type)
c152c796 9058 return 1;
7e612e98 9059 if (a->u.offset < b->u.offset)
c152c796 9060 return -1;
7e612e98 9061 if (a->u.offset > b->u.offset)
c152c796
AM
9062 return 1;
9063 if (a->rela->r_offset < b->rela->r_offset)
9064 return -1;
9065 if (a->rela->r_offset > b->rela->r_offset)
9066 return 1;
9067 return 0;
9068}
9069
9070static size_t
9071elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9072{
3410fea8 9073 asection *dynamic_relocs;
fc66a176
L
9074 asection *rela_dyn;
9075 asection *rel_dyn;
c152c796
AM
9076 bfd_size_type count, size;
9077 size_t i, ret, sort_elt, ext_size;
9078 bfd_byte *sort, *s_non_relative, *p;
9079 struct elf_link_sort_rela *sq;
9080 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9081 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9082 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9083 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9084 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9085 struct bfd_link_order *lo;
9086 bfd_vma r_sym_mask;
3410fea8 9087 bfd_boolean use_rela;
c152c796 9088
3410fea8
NC
9089 /* Find a dynamic reloc section. */
9090 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9091 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9092 if (rela_dyn != NULL && rela_dyn->size > 0
9093 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9094 {
3410fea8
NC
9095 bfd_boolean use_rela_initialised = FALSE;
9096
9097 /* This is just here to stop gcc from complaining.
c8e44c6d 9098 Its initialization checking code is not perfect. */
3410fea8
NC
9099 use_rela = TRUE;
9100
9101 /* Both sections are present. Examine the sizes
9102 of the indirect sections to help us choose. */
9103 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9104 if (lo->type == bfd_indirect_link_order)
9105 {
9106 asection *o = lo->u.indirect.section;
9107
9108 if ((o->size % bed->s->sizeof_rela) == 0)
9109 {
9110 if ((o->size % bed->s->sizeof_rel) == 0)
9111 /* Section size is divisible by both rel and rela sizes.
9112 It is of no help to us. */
9113 ;
9114 else
9115 {
9116 /* Section size is only divisible by rela. */
535b785f 9117 if (use_rela_initialised && !use_rela)
3410fea8 9118 {
9793eb77 9119 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9120 "they are in more than one size"),
9121 abfd);
3410fea8
NC
9122 bfd_set_error (bfd_error_invalid_operation);
9123 return 0;
9124 }
9125 else
9126 {
9127 use_rela = TRUE;
9128 use_rela_initialised = TRUE;
9129 }
9130 }
9131 }
9132 else if ((o->size % bed->s->sizeof_rel) == 0)
9133 {
9134 /* Section size is only divisible by rel. */
535b785f 9135 if (use_rela_initialised && use_rela)
3410fea8 9136 {
9793eb77 9137 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9138 "they are in more than one size"),
9139 abfd);
3410fea8
NC
9140 bfd_set_error (bfd_error_invalid_operation);
9141 return 0;
9142 }
9143 else
9144 {
9145 use_rela = FALSE;
9146 use_rela_initialised = TRUE;
9147 }
9148 }
9149 else
9150 {
c8e44c6d
AM
9151 /* The section size is not divisible by either -
9152 something is wrong. */
9793eb77 9153 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9154 "they are of an unknown size"), abfd);
3410fea8
NC
9155 bfd_set_error (bfd_error_invalid_operation);
9156 return 0;
9157 }
9158 }
9159
9160 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9161 if (lo->type == bfd_indirect_link_order)
9162 {
9163 asection *o = lo->u.indirect.section;
9164
9165 if ((o->size % bed->s->sizeof_rela) == 0)
9166 {
9167 if ((o->size % bed->s->sizeof_rel) == 0)
9168 /* Section size is divisible by both rel and rela sizes.
9169 It is of no help to us. */
9170 ;
9171 else
9172 {
9173 /* Section size is only divisible by rela. */
535b785f 9174 if (use_rela_initialised && !use_rela)
3410fea8 9175 {
9793eb77 9176 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9177 "they are in more than one size"),
9178 abfd);
3410fea8
NC
9179 bfd_set_error (bfd_error_invalid_operation);
9180 return 0;
9181 }
9182 else
9183 {
9184 use_rela = TRUE;
9185 use_rela_initialised = TRUE;
9186 }
9187 }
9188 }
9189 else if ((o->size % bed->s->sizeof_rel) == 0)
9190 {
9191 /* Section size is only divisible by rel. */
535b785f 9192 if (use_rela_initialised && use_rela)
3410fea8 9193 {
9793eb77 9194 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9195 "they are in more than one size"),
9196 abfd);
3410fea8
NC
9197 bfd_set_error (bfd_error_invalid_operation);
9198 return 0;
9199 }
9200 else
9201 {
9202 use_rela = FALSE;
9203 use_rela_initialised = TRUE;
9204 }
9205 }
9206 else
9207 {
c8e44c6d
AM
9208 /* The section size is not divisible by either -
9209 something is wrong. */
9793eb77 9210 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9211 "they are of an unknown size"), abfd);
3410fea8
NC
9212 bfd_set_error (bfd_error_invalid_operation);
9213 return 0;
9214 }
9215 }
9216
9217 if (! use_rela_initialised)
9218 /* Make a guess. */
9219 use_rela = TRUE;
c152c796 9220 }
fc66a176
L
9221 else if (rela_dyn != NULL && rela_dyn->size > 0)
9222 use_rela = TRUE;
9223 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9224 use_rela = FALSE;
c152c796 9225 else
fc66a176 9226 return 0;
3410fea8
NC
9227
9228 if (use_rela)
c152c796 9229 {
3410fea8 9230 dynamic_relocs = rela_dyn;
c152c796
AM
9231 ext_size = bed->s->sizeof_rela;
9232 swap_in = bed->s->swap_reloca_in;
9233 swap_out = bed->s->swap_reloca_out;
9234 }
3410fea8
NC
9235 else
9236 {
9237 dynamic_relocs = rel_dyn;
9238 ext_size = bed->s->sizeof_rel;
9239 swap_in = bed->s->swap_reloc_in;
9240 swap_out = bed->s->swap_reloc_out;
9241 }
c152c796
AM
9242
9243 size = 0;
3410fea8 9244 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9245 if (lo->type == bfd_indirect_link_order)
3410fea8 9246 size += lo->u.indirect.section->size;
c152c796 9247
3410fea8 9248 if (size != dynamic_relocs->size)
c152c796
AM
9249 return 0;
9250
9251 sort_elt = (sizeof (struct elf_link_sort_rela)
9252 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9253
9254 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9255 if (count == 0)
9256 return 0;
a50b1753 9257 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9258
c152c796
AM
9259 if (sort == NULL)
9260 {
9261 (*info->callbacks->warning)
9793eb77 9262 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9263 return 0;
9264 }
9265
9266 if (bed->s->arch_size == 32)
9267 r_sym_mask = ~(bfd_vma) 0xff;
9268 else
9269 r_sym_mask = ~(bfd_vma) 0xffffffff;
9270
3410fea8 9271 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9272 if (lo->type == bfd_indirect_link_order)
9273 {
9274 bfd_byte *erel, *erelend;
9275 asection *o = lo->u.indirect.section;
9276
1da212d6
AM
9277 if (o->contents == NULL && o->size != 0)
9278 {
9279 /* This is a reloc section that is being handled as a normal
9280 section. See bfd_section_from_shdr. We can't combine
9281 relocs in this case. */
9282 free (sort);
9283 return 0;
9284 }
c152c796 9285 erel = o->contents;
eea6121a 9286 erelend = o->contents + o->size;
c8e44c6d 9287 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9288
c152c796
AM
9289 while (erel < erelend)
9290 {
9291 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9292
c152c796 9293 (*swap_in) (abfd, erel, s->rela);
7e612e98 9294 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9295 s->u.sym_mask = r_sym_mask;
9296 p += sort_elt;
9297 erel += ext_size;
9298 }
9299 }
9300
9301 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9302
9303 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9304 {
9305 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9306 if (s->type != reloc_class_relative)
9307 break;
9308 }
9309 ret = i;
9310 s_non_relative = p;
9311
9312 sq = (struct elf_link_sort_rela *) s_non_relative;
9313 for (; i < count; i++, p += sort_elt)
9314 {
9315 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9316 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9317 sq = sp;
9318 sp->u.offset = sq->rela->r_offset;
9319 }
9320
9321 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9322
c8e44c6d
AM
9323 struct elf_link_hash_table *htab = elf_hash_table (info);
9324 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9325 {
9326 /* We have plt relocs in .rela.dyn. */
9327 sq = (struct elf_link_sort_rela *) sort;
9328 for (i = 0; i < count; i++)
9329 if (sq[count - i - 1].type != reloc_class_plt)
9330 break;
9331 if (i != 0 && htab->srelplt->size == i * ext_size)
9332 {
9333 struct bfd_link_order **plo;
9334 /* Put srelplt link_order last. This is so the output_offset
9335 set in the next loop is correct for DT_JMPREL. */
9336 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9337 if ((*plo)->type == bfd_indirect_link_order
9338 && (*plo)->u.indirect.section == htab->srelplt)
9339 {
9340 lo = *plo;
9341 *plo = lo->next;
9342 }
9343 else
9344 plo = &(*plo)->next;
9345 *plo = lo;
9346 lo->next = NULL;
9347 dynamic_relocs->map_tail.link_order = lo;
9348 }
9349 }
9350
9351 p = sort;
3410fea8 9352 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9353 if (lo->type == bfd_indirect_link_order)
9354 {
9355 bfd_byte *erel, *erelend;
9356 asection *o = lo->u.indirect.section;
9357
9358 erel = o->contents;
eea6121a 9359 erelend = o->contents + o->size;
c8e44c6d 9360 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9361 while (erel < erelend)
9362 {
9363 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9364 (*swap_out) (abfd, s->rela, erel);
9365 p += sort_elt;
9366 erel += ext_size;
9367 }
9368 }
9369
9370 free (sort);
3410fea8 9371 *psec = dynamic_relocs;
c152c796
AM
9372 return ret;
9373}
9374
ef10c3ac 9375/* Add a symbol to the output symbol string table. */
c152c796 9376
6e0b88f1 9377static int
ef10c3ac
L
9378elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9379 const char *name,
9380 Elf_Internal_Sym *elfsym,
9381 asection *input_sec,
9382 struct elf_link_hash_entry *h)
c152c796 9383{
6e0b88f1 9384 int (*output_symbol_hook)
c152c796
AM
9385 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9386 struct elf_link_hash_entry *);
ef10c3ac 9387 struct elf_link_hash_table *hash_table;
c152c796 9388 const struct elf_backend_data *bed;
ef10c3ac 9389 bfd_size_type strtabsize;
c152c796 9390
8539e4e8
AM
9391 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9392
8b127cbc 9393 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9394 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9395 if (output_symbol_hook != NULL)
9396 {
8b127cbc 9397 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9398 if (ret != 1)
9399 return ret;
c152c796
AM
9400 }
9401
06f44071
AM
9402 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9403 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9404 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9405 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9406
ef10c3ac
L
9407 if (name == NULL
9408 || *name == '\0'
9409 || (input_sec->flags & SEC_EXCLUDE))
9410 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9411 else
9412 {
ef10c3ac
L
9413 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9414 to get the final offset for st_name. */
9415 elfsym->st_name
9416 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9417 name, FALSE);
c152c796 9418 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9419 return 0;
c152c796
AM
9420 }
9421
ef10c3ac
L
9422 hash_table = elf_hash_table (flinfo->info);
9423 strtabsize = hash_table->strtabsize;
9424 if (strtabsize <= hash_table->strtabcount)
c152c796 9425 {
ef10c3ac
L
9426 strtabsize += strtabsize;
9427 hash_table->strtabsize = strtabsize;
9428 strtabsize *= sizeof (*hash_table->strtab);
9429 hash_table->strtab
9430 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9431 strtabsize);
9432 if (hash_table->strtab == NULL)
6e0b88f1 9433 return 0;
c152c796 9434 }
ef10c3ac
L
9435 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9436 hash_table->strtab[hash_table->strtabcount].dest_index
9437 = hash_table->strtabcount;
9438 hash_table->strtab[hash_table->strtabcount].destshndx_index
9439 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9440
9441 bfd_get_symcount (flinfo->output_bfd) += 1;
9442 hash_table->strtabcount += 1;
9443
9444 return 1;
9445}
9446
9447/* Swap symbols out to the symbol table and flush the output symbols to
9448 the file. */
9449
9450static bfd_boolean
9451elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9452{
9453 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9454 bfd_size_type amt;
9455 size_t i;
ef10c3ac
L
9456 const struct elf_backend_data *bed;
9457 bfd_byte *symbuf;
9458 Elf_Internal_Shdr *hdr;
9459 file_ptr pos;
9460 bfd_boolean ret;
9461
9462 if (!hash_table->strtabcount)
9463 return TRUE;
9464
9465 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9466
9467 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9468
ef10c3ac
L
9469 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9470 symbuf = (bfd_byte *) bfd_malloc (amt);
9471 if (symbuf == NULL)
9472 return FALSE;
1b786873 9473
ef10c3ac 9474 if (flinfo->symshndxbuf)
c152c796 9475 {
ef53be89
AM
9476 amt = sizeof (Elf_External_Sym_Shndx);
9477 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9478 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9479 if (flinfo->symshndxbuf == NULL)
c152c796 9480 {
ef10c3ac
L
9481 free (symbuf);
9482 return FALSE;
c152c796 9483 }
c152c796
AM
9484 }
9485
ef10c3ac
L
9486 for (i = 0; i < hash_table->strtabcount; i++)
9487 {
9488 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9489 if (elfsym->sym.st_name == (unsigned long) -1)
9490 elfsym->sym.st_name = 0;
9491 else
9492 elfsym->sym.st_name
9493 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9494 elfsym->sym.st_name);
9495 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9496 ((bfd_byte *) symbuf
9497 + (elfsym->dest_index
9498 * bed->s->sizeof_sym)),
9499 (flinfo->symshndxbuf
9500 + elfsym->destshndx_index));
9501 }
9502
9503 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9504 pos = hdr->sh_offset + hdr->sh_size;
9505 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9506 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9507 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9508 {
9509 hdr->sh_size += amt;
9510 ret = TRUE;
9511 }
9512 else
9513 ret = FALSE;
c152c796 9514
ef10c3ac
L
9515 free (symbuf);
9516
9517 free (hash_table->strtab);
9518 hash_table->strtab = NULL;
9519
9520 return ret;
c152c796
AM
9521}
9522
c0d5a53d
L
9523/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9524
9525static bfd_boolean
9526check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9527{
4fbb74a6
AM
9528 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9529 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9530 {
9531 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9532 beyond 64k. */
4eca0228 9533 _bfd_error_handler
695344c0 9534 /* xgettext:c-format */
9793eb77 9535 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9536 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9537 bfd_set_error (bfd_error_nonrepresentable_section);
9538 return FALSE;
9539 }
9540 return TRUE;
9541}
9542
c152c796
AM
9543/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9544 allowing an unsatisfied unversioned symbol in the DSO to match a
9545 versioned symbol that would normally require an explicit version.
9546 We also handle the case that a DSO references a hidden symbol
9547 which may be satisfied by a versioned symbol in another DSO. */
9548
9549static bfd_boolean
9550elf_link_check_versioned_symbol (struct bfd_link_info *info,
9551 const struct elf_backend_data *bed,
9552 struct elf_link_hash_entry *h)
9553{
9554 bfd *abfd;
9555 struct elf_link_loaded_list *loaded;
9556
9557 if (!is_elf_hash_table (info->hash))
9558 return FALSE;
9559
90c984fc
L
9560 /* Check indirect symbol. */
9561 while (h->root.type == bfd_link_hash_indirect)
9562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9563
c152c796
AM
9564 switch (h->root.type)
9565 {
9566 default:
9567 abfd = NULL;
9568 break;
9569
9570 case bfd_link_hash_undefined:
9571 case bfd_link_hash_undefweak:
9572 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9573 if (abfd == NULL
9574 || (abfd->flags & DYNAMIC) == 0
e56f61be 9575 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9576 return FALSE;
9577 break;
9578
9579 case bfd_link_hash_defined:
9580 case bfd_link_hash_defweak:
9581 abfd = h->root.u.def.section->owner;
9582 break;
9583
9584 case bfd_link_hash_common:
9585 abfd = h->root.u.c.p->section->owner;
9586 break;
9587 }
9588 BFD_ASSERT (abfd != NULL);
9589
9590 for (loaded = elf_hash_table (info)->loaded;
9591 loaded != NULL;
9592 loaded = loaded->next)
9593 {
9594 bfd *input;
9595 Elf_Internal_Shdr *hdr;
ef53be89
AM
9596 size_t symcount;
9597 size_t extsymcount;
9598 size_t extsymoff;
c152c796
AM
9599 Elf_Internal_Shdr *versymhdr;
9600 Elf_Internal_Sym *isym;
9601 Elf_Internal_Sym *isymend;
9602 Elf_Internal_Sym *isymbuf;
9603 Elf_External_Versym *ever;
9604 Elf_External_Versym *extversym;
9605
9606 input = loaded->abfd;
9607
9608 /* We check each DSO for a possible hidden versioned definition. */
9609 if (input == abfd
9610 || (input->flags & DYNAMIC) == 0
9611 || elf_dynversym (input) == 0)
9612 continue;
9613
9614 hdr = &elf_tdata (input)->dynsymtab_hdr;
9615
9616 symcount = hdr->sh_size / bed->s->sizeof_sym;
9617 if (elf_bad_symtab (input))
9618 {
9619 extsymcount = symcount;
9620 extsymoff = 0;
9621 }
9622 else
9623 {
9624 extsymcount = symcount - hdr->sh_info;
9625 extsymoff = hdr->sh_info;
9626 }
9627
9628 if (extsymcount == 0)
9629 continue;
9630
9631 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9632 NULL, NULL, NULL);
9633 if (isymbuf == NULL)
9634 return FALSE;
9635
9636 /* Read in any version definitions. */
9637 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9638 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9639 if (extversym == NULL)
9640 goto error_ret;
9641
9642 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9643 || (bfd_bread (extversym, versymhdr->sh_size, input)
9644 != versymhdr->sh_size))
9645 {
9646 free (extversym);
9647 error_ret:
9648 free (isymbuf);
9649 return FALSE;
9650 }
9651
9652 ever = extversym + extsymoff;
9653 isymend = isymbuf + extsymcount;
9654 for (isym = isymbuf; isym < isymend; isym++, ever++)
9655 {
9656 const char *name;
9657 Elf_Internal_Versym iver;
9658 unsigned short version_index;
9659
9660 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9661 || isym->st_shndx == SHN_UNDEF)
9662 continue;
9663
9664 name = bfd_elf_string_from_elf_section (input,
9665 hdr->sh_link,
9666 isym->st_name);
9667 if (strcmp (name, h->root.root.string) != 0)
9668 continue;
9669
9670 _bfd_elf_swap_versym_in (input, ever, &iver);
9671
d023c380
L
9672 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9673 && !(h->def_regular
9674 && h->forced_local))
c152c796
AM
9675 {
9676 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9677 have provided a definition for the undefined sym unless
9678 it is defined in a non-shared object and forced local.
9679 */
c152c796
AM
9680 abort ();
9681 }
9682
9683 version_index = iver.vs_vers & VERSYM_VERSION;
9684 if (version_index == 1 || version_index == 2)
9685 {
9686 /* This is the base or first version. We can use it. */
9687 free (extversym);
9688 free (isymbuf);
9689 return TRUE;
9690 }
9691 }
9692
9693 free (extversym);
9694 free (isymbuf);
9695 }
9696
9697 return FALSE;
9698}
9699
b8871f35
L
9700/* Convert ELF common symbol TYPE. */
9701
9702static int
9703elf_link_convert_common_type (struct bfd_link_info *info, int type)
9704{
9705 /* Commom symbol can only appear in relocatable link. */
9706 if (!bfd_link_relocatable (info))
9707 abort ();
9708 switch (info->elf_stt_common)
9709 {
9710 case unchanged:
9711 break;
9712 case elf_stt_common:
9713 type = STT_COMMON;
9714 break;
9715 case no_elf_stt_common:
9716 type = STT_OBJECT;
9717 break;
9718 }
9719 return type;
9720}
9721
c152c796
AM
9722/* Add an external symbol to the symbol table. This is called from
9723 the hash table traversal routine. When generating a shared object,
9724 we go through the symbol table twice. The first time we output
9725 anything that might have been forced to local scope in a version
9726 script. The second time we output the symbols that are still
9727 global symbols. */
9728
9729static bfd_boolean
7686d77d 9730elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9731{
7686d77d 9732 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9733 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9734 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9735 bfd_boolean strip;
9736 Elf_Internal_Sym sym;
9737 asection *input_sec;
9738 const struct elf_backend_data *bed;
6e0b88f1
AM
9739 long indx;
9740 int ret;
b8871f35 9741 unsigned int type;
c152c796
AM
9742
9743 if (h->root.type == bfd_link_hash_warning)
9744 {
9745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9746 if (h->root.type == bfd_link_hash_new)
9747 return TRUE;
9748 }
9749
9750 /* Decide whether to output this symbol in this pass. */
9751 if (eoinfo->localsyms)
9752 {
4deb8f71 9753 if (!h->forced_local)
c152c796
AM
9754 return TRUE;
9755 }
9756 else
9757 {
4deb8f71 9758 if (h->forced_local)
c152c796
AM
9759 return TRUE;
9760 }
9761
8b127cbc 9762 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9763
12ac1cf5 9764 if (h->root.type == bfd_link_hash_undefined)
c152c796 9765 {
12ac1cf5
NC
9766 /* If we have an undefined symbol reference here then it must have
9767 come from a shared library that is being linked in. (Undefined
98da7939
L
9768 references in regular files have already been handled unless
9769 they are in unreferenced sections which are removed by garbage
9770 collection). */
12ac1cf5
NC
9771 bfd_boolean ignore_undef = FALSE;
9772
9773 /* Some symbols may be special in that the fact that they're
9774 undefined can be safely ignored - let backend determine that. */
9775 if (bed->elf_backend_ignore_undef_symbol)
9776 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9777
9778 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9779 if (!ignore_undef
c54f1524 9780 && h->ref_dynamic_nonweak
8b127cbc
AM
9781 && (!h->ref_regular || flinfo->info->gc_sections)
9782 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9783 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9784 (*flinfo->info->callbacks->undefined_symbol)
9785 (flinfo->info, h->root.root.string,
9786 h->ref_regular ? NULL : h->root.u.undef.abfd,
9787 NULL, 0,
9788 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9789
9790 /* Strip a global symbol defined in a discarded section. */
9791 if (h->indx == -3)
9792 return TRUE;
c152c796
AM
9793 }
9794
9795 /* We should also warn if a forced local symbol is referenced from
9796 shared libraries. */
0e1862bb 9797 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9798 && h->forced_local
9799 && h->ref_dynamic
371a5866 9800 && h->def_regular
f5385ebf 9801 && !h->dynamic_def
ee659f1f 9802 && h->ref_dynamic_nonweak
8b127cbc 9803 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9804 {
17d078c5
AM
9805 bfd *def_bfd;
9806 const char *msg;
90c984fc
L
9807 struct elf_link_hash_entry *hi = h;
9808
9809 /* Check indirect symbol. */
9810 while (hi->root.type == bfd_link_hash_indirect)
9811 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9812
9813 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9814 /* xgettext:c-format */
871b3ab2 9815 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9816 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9817 /* xgettext:c-format */
871b3ab2 9818 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9819 else
695344c0 9820 /* xgettext:c-format */
871b3ab2 9821 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9822 def_bfd = flinfo->output_bfd;
90c984fc
L
9823 if (hi->root.u.def.section != bfd_abs_section_ptr)
9824 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9825 _bfd_error_handler (msg, flinfo->output_bfd,
9826 h->root.root.string, def_bfd);
17d078c5 9827 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9828 eoinfo->failed = TRUE;
9829 return FALSE;
9830 }
9831
9832 /* We don't want to output symbols that have never been mentioned by
9833 a regular file, or that we have been told to strip. However, if
9834 h->indx is set to -2, the symbol is used by a reloc and we must
9835 output it. */
d983c8c5 9836 strip = FALSE;
c152c796 9837 if (h->indx == -2)
d983c8c5 9838 ;
f5385ebf 9839 else if ((h->def_dynamic
77cfaee6
AM
9840 || h->ref_dynamic
9841 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9842 && !h->def_regular
9843 && !h->ref_regular)
c152c796 9844 strip = TRUE;
8b127cbc 9845 else if (flinfo->info->strip == strip_all)
c152c796 9846 strip = TRUE;
8b127cbc
AM
9847 else if (flinfo->info->strip == strip_some
9848 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9849 h->root.root.string, FALSE, FALSE) == NULL)
9850 strip = TRUE;
d56d55e7
AM
9851 else if ((h->root.type == bfd_link_hash_defined
9852 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9853 && ((flinfo->info->strip_discarded
dbaa2011 9854 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9855 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9856 && h->root.u.def.section->owner != NULL
d56d55e7 9857 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9858 strip = TRUE;
9e2278f5
AM
9859 else if ((h->root.type == bfd_link_hash_undefined
9860 || h->root.type == bfd_link_hash_undefweak)
9861 && h->root.u.undef.abfd != NULL
9862 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9863 strip = TRUE;
c152c796 9864
b8871f35
L
9865 type = h->type;
9866
c152c796 9867 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9868 nothing else to do. However, if it is a forced local symbol or
9869 an ifunc symbol we need to give the backend finish_dynamic_symbol
9870 function a chance to make it dynamic. */
c152c796
AM
9871 if (strip
9872 && h->dynindx == -1
b8871f35 9873 && type != STT_GNU_IFUNC
f5385ebf 9874 && !h->forced_local)
c152c796
AM
9875 return TRUE;
9876
9877 sym.st_value = 0;
9878 sym.st_size = h->size;
9879 sym.st_other = h->other;
c152c796
AM
9880 switch (h->root.type)
9881 {
9882 default:
9883 case bfd_link_hash_new:
9884 case bfd_link_hash_warning:
9885 abort ();
9886 return FALSE;
9887
9888 case bfd_link_hash_undefined:
9889 case bfd_link_hash_undefweak:
9890 input_sec = bfd_und_section_ptr;
9891 sym.st_shndx = SHN_UNDEF;
9892 break;
9893
9894 case bfd_link_hash_defined:
9895 case bfd_link_hash_defweak:
9896 {
9897 input_sec = h->root.u.def.section;
9898 if (input_sec->output_section != NULL)
9899 {
9900 sym.st_shndx =
8b127cbc 9901 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9902 input_sec->output_section);
9903 if (sym.st_shndx == SHN_BAD)
9904 {
4eca0228 9905 _bfd_error_handler
695344c0 9906 /* xgettext:c-format */
871b3ab2 9907 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9908 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9909 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9910 eoinfo->failed = TRUE;
9911 return FALSE;
9912 }
9913
9914 /* ELF symbols in relocatable files are section relative,
9915 but in nonrelocatable files they are virtual
9916 addresses. */
9917 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9918 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9919 {
9920 sym.st_value += input_sec->output_section->vma;
9921 if (h->type == STT_TLS)
9922 {
8b127cbc 9923 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9924 if (tls_sec != NULL)
9925 sym.st_value -= tls_sec->vma;
c152c796
AM
9926 }
9927 }
9928 }
9929 else
9930 {
9931 BFD_ASSERT (input_sec->owner == NULL
9932 || (input_sec->owner->flags & DYNAMIC) != 0);
9933 sym.st_shndx = SHN_UNDEF;
9934 input_sec = bfd_und_section_ptr;
9935 }
9936 }
9937 break;
9938
9939 case bfd_link_hash_common:
9940 input_sec = h->root.u.c.p->section;
a4d8e49b 9941 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9942 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9943 break;
9944
9945 case bfd_link_hash_indirect:
9946 /* These symbols are created by symbol versioning. They point
9947 to the decorated version of the name. For example, if the
9948 symbol foo@@GNU_1.2 is the default, which should be used when
9949 foo is used with no version, then we add an indirect symbol
9950 foo which points to foo@@GNU_1.2. We ignore these symbols,
9951 since the indirected symbol is already in the hash table. */
9952 return TRUE;
9953 }
9954
b8871f35
L
9955 if (type == STT_COMMON || type == STT_OBJECT)
9956 switch (h->root.type)
9957 {
9958 case bfd_link_hash_common:
9959 type = elf_link_convert_common_type (flinfo->info, type);
9960 break;
9961 case bfd_link_hash_defined:
9962 case bfd_link_hash_defweak:
9963 if (bed->common_definition (&sym))
9964 type = elf_link_convert_common_type (flinfo->info, type);
9965 else
9966 type = STT_OBJECT;
9967 break;
9968 case bfd_link_hash_undefined:
9969 case bfd_link_hash_undefweak:
9970 break;
9971 default:
9972 abort ();
9973 }
9974
4deb8f71 9975 if (h->forced_local)
b8871f35
L
9976 {
9977 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9978 /* Turn off visibility on local symbol. */
9979 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9980 }
9981 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9982 else if (h->unique_global && h->def_regular)
9983 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9984 else if (h->root.type == bfd_link_hash_undefweak
9985 || h->root.type == bfd_link_hash_defweak)
9986 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9987 else
9988 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9989 sym.st_target_internal = h->target_internal;
9990
c152c796
AM
9991 /* Give the processor backend a chance to tweak the symbol value,
9992 and also to finish up anything that needs to be done for this
9993 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9994 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9995 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9996 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9997 && h->def_regular
0e1862bb 9998 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9999 || ((h->dynindx != -1
10000 || h->forced_local)
0e1862bb 10001 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10002 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10003 || h->root.type != bfd_link_hash_undefweak))
10004 || !h->forced_local)
8b127cbc 10005 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10006 {
10007 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10008 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10009 {
10010 eoinfo->failed = TRUE;
10011 return FALSE;
10012 }
10013 }
10014
10015 /* If we are marking the symbol as undefined, and there are no
10016 non-weak references to this symbol from a regular object, then
10017 mark the symbol as weak undefined; if there are non-weak
10018 references, mark the symbol as strong. We can't do this earlier,
10019 because it might not be marked as undefined until the
10020 finish_dynamic_symbol routine gets through with it. */
10021 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10022 && h->ref_regular
c152c796
AM
10023 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10024 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10025 {
10026 int bindtype;
b8871f35 10027 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10028
10029 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10030 if (type == STT_GNU_IFUNC)
10031 type = STT_FUNC;
c152c796 10032
f5385ebf 10033 if (h->ref_regular_nonweak)
c152c796
AM
10034 bindtype = STB_GLOBAL;
10035 else
10036 bindtype = STB_WEAK;
2955ec4c 10037 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10038 }
10039
bda987c2
CD
10040 /* If this is a symbol defined in a dynamic library, don't use the
10041 symbol size from the dynamic library. Relinking an executable
10042 against a new library may introduce gratuitous changes in the
10043 executable's symbols if we keep the size. */
10044 if (sym.st_shndx == SHN_UNDEF
10045 && !h->def_regular
10046 && h->def_dynamic)
10047 sym.st_size = 0;
10048
c152c796
AM
10049 /* If a non-weak symbol with non-default visibility is not defined
10050 locally, it is a fatal error. */
0e1862bb 10051 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10052 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10053 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10054 && h->root.type == bfd_link_hash_undefined
f5385ebf 10055 && !h->def_regular)
c152c796 10056 {
17d078c5
AM
10057 const char *msg;
10058
10059 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10060 /* xgettext:c-format */
871b3ab2 10061 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10062 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10063 /* xgettext:c-format */
871b3ab2 10064 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10065 else
695344c0 10066 /* xgettext:c-format */
871b3ab2 10067 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10068 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10069 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10070 eoinfo->failed = TRUE;
10071 return FALSE;
10072 }
10073
10074 /* If this symbol should be put in the .dynsym section, then put it
10075 there now. We already know the symbol index. We also fill in
10076 the entry in the .hash section. */
1c2649ed
EB
10077 if (h->dynindx != -1
10078 && elf_hash_table (flinfo->info)->dynamic_sections_created
10079 && elf_hash_table (flinfo->info)->dynsym != NULL
10080 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10081 {
c152c796
AM
10082 bfd_byte *esym;
10083
90c984fc
L
10084 /* Since there is no version information in the dynamic string,
10085 if there is no version info in symbol version section, we will
1659f720 10086 have a run-time problem if not linking executable, referenced
4deb8f71 10087 by shared library, or not bound locally. */
1659f720 10088 if (h->verinfo.verdef == NULL
0e1862bb 10089 && (!bfd_link_executable (flinfo->info)
1659f720
L
10090 || h->ref_dynamic
10091 || !h->def_regular))
90c984fc
L
10092 {
10093 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10094
10095 if (p && p [1] != '\0')
10096 {
4eca0228 10097 _bfd_error_handler
695344c0 10098 /* xgettext:c-format */
9793eb77 10099 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10100 flinfo->output_bfd, h->root.root.string);
10101 eoinfo->failed = TRUE;
10102 return FALSE;
10103 }
10104 }
10105
c152c796 10106 sym.st_name = h->dynstr_index;
cae1fbbb
L
10107 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10108 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10109 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10110 {
10111 eoinfo->failed = TRUE;
10112 return FALSE;
10113 }
8b127cbc 10114 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10115
8b127cbc 10116 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10117 {
10118 size_t hash_entry_size;
10119 bfd_byte *bucketpos;
10120 bfd_vma chain;
41198d0c
L
10121 size_t bucketcount;
10122 size_t bucket;
10123
8b127cbc 10124 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10125 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10126
10127 hash_entry_size
8b127cbc
AM
10128 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10129 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10130 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10131 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10132 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10133 bucketpos);
10134 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10135 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10136 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10137 }
c152c796 10138
8b127cbc 10139 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10140 {
10141 Elf_Internal_Versym iversym;
10142 Elf_External_Versym *eversym;
10143
f5385ebf 10144 if (!h->def_regular)
c152c796 10145 {
7b20f099
AM
10146 if (h->verinfo.verdef == NULL
10147 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10148 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10149 iversym.vs_vers = 0;
10150 else
10151 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10152 }
10153 else
10154 {
10155 if (h->verinfo.vertree == NULL)
10156 iversym.vs_vers = 1;
10157 else
10158 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10159 if (flinfo->info->create_default_symver)
3e3b46e5 10160 iversym.vs_vers++;
c152c796
AM
10161 }
10162
422f1182 10163 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10164 defined locally. */
422f1182 10165 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10166 iversym.vs_vers |= VERSYM_HIDDEN;
10167
8b127cbc 10168 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10169 eversym += h->dynindx;
8b127cbc 10170 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10171 }
10172 }
10173
d983c8c5
AM
10174 /* If the symbol is undefined, and we didn't output it to .dynsym,
10175 strip it from .symtab too. Obviously we can't do this for
10176 relocatable output or when needed for --emit-relocs. */
10177 else if (input_sec == bfd_und_section_ptr
10178 && h->indx != -2
66cae560
NC
10179 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10180 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10181 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10182 return TRUE;
66cae560 10183
d983c8c5
AM
10184 /* Also strip others that we couldn't earlier due to dynamic symbol
10185 processing. */
10186 if (strip)
10187 return TRUE;
10188 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10189 return TRUE;
10190
2ec55de3
AM
10191 /* Output a FILE symbol so that following locals are not associated
10192 with the wrong input file. We need one for forced local symbols
10193 if we've seen more than one FILE symbol or when we have exactly
10194 one FILE symbol but global symbols are present in a file other
10195 than the one with the FILE symbol. We also need one if linker
10196 defined symbols are present. In practice these conditions are
10197 always met, so just emit the FILE symbol unconditionally. */
10198 if (eoinfo->localsyms
10199 && !eoinfo->file_sym_done
10200 && eoinfo->flinfo->filesym_count != 0)
10201 {
10202 Elf_Internal_Sym fsym;
10203
10204 memset (&fsym, 0, sizeof (fsym));
10205 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10206 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10207 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10208 bfd_und_section_ptr, NULL))
2ec55de3
AM
10209 return FALSE;
10210
10211 eoinfo->file_sym_done = TRUE;
10212 }
10213
8b127cbc 10214 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10215 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10216 input_sec, h);
6e0b88f1 10217 if (ret == 0)
c152c796
AM
10218 {
10219 eoinfo->failed = TRUE;
10220 return FALSE;
10221 }
6e0b88f1
AM
10222 else if (ret == 1)
10223 h->indx = indx;
10224 else if (h->indx == -2)
10225 abort();
c152c796
AM
10226
10227 return TRUE;
10228}
10229
cdd3575c
AM
10230/* Return TRUE if special handling is done for relocs in SEC against
10231 symbols defined in discarded sections. */
10232
c152c796
AM
10233static bfd_boolean
10234elf_section_ignore_discarded_relocs (asection *sec)
10235{
10236 const struct elf_backend_data *bed;
10237
cdd3575c
AM
10238 switch (sec->sec_info_type)
10239 {
dbaa2011
AM
10240 case SEC_INFO_TYPE_STABS:
10241 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10242 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10243 return TRUE;
10244 default:
10245 break;
10246 }
c152c796
AM
10247
10248 bed = get_elf_backend_data (sec->owner);
10249 if (bed->elf_backend_ignore_discarded_relocs != NULL
10250 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10251 return TRUE;
10252
10253 return FALSE;
10254}
10255
9e66c942
AM
10256/* Return a mask saying how ld should treat relocations in SEC against
10257 symbols defined in discarded sections. If this function returns
10258 COMPLAIN set, ld will issue a warning message. If this function
10259 returns PRETEND set, and the discarded section was link-once and the
10260 same size as the kept link-once section, ld will pretend that the
10261 symbol was actually defined in the kept section. Otherwise ld will
10262 zero the reloc (at least that is the intent, but some cooperation by
10263 the target dependent code is needed, particularly for REL targets). */
10264
8a696751
AM
10265unsigned int
10266_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10267{
9e66c942 10268 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10269 return PRETEND;
cdd3575c
AM
10270
10271 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10272 return 0;
cdd3575c
AM
10273
10274 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10275 return 0;
cdd3575c 10276
9e66c942 10277 return COMPLAIN | PRETEND;
cdd3575c
AM
10278}
10279
3d7f7666
L
10280/* Find a match between a section and a member of a section group. */
10281
10282static asection *
c0f00686
L
10283match_group_member (asection *sec, asection *group,
10284 struct bfd_link_info *info)
3d7f7666
L
10285{
10286 asection *first = elf_next_in_group (group);
10287 asection *s = first;
10288
10289 while (s != NULL)
10290 {
c0f00686 10291 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10292 return s;
10293
83180ade 10294 s = elf_next_in_group (s);
3d7f7666
L
10295 if (s == first)
10296 break;
10297 }
10298
10299 return NULL;
10300}
10301
01b3c8ab 10302/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10303 to replace it. Return the replacement if it is OK. Otherwise return
10304 NULL. */
01b3c8ab
L
10305
10306asection *
c0f00686 10307_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10308{
10309 asection *kept;
10310
10311 kept = sec->kept_section;
10312 if (kept != NULL)
10313 {
c2370991 10314 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10315 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10316 if (kept != NULL
10317 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10318 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10319 kept = NULL;
c2370991 10320 sec->kept_section = kept;
01b3c8ab
L
10321 }
10322 return kept;
10323}
10324
c152c796
AM
10325/* Link an input file into the linker output file. This function
10326 handles all the sections and relocations of the input file at once.
10327 This is so that we only have to read the local symbols once, and
10328 don't have to keep them in memory. */
10329
10330static bfd_boolean
8b127cbc 10331elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10332{
ece5ef60 10333 int (*relocate_section)
c152c796
AM
10334 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10335 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10336 bfd *output_bfd;
10337 Elf_Internal_Shdr *symtab_hdr;
10338 size_t locsymcount;
10339 size_t extsymoff;
10340 Elf_Internal_Sym *isymbuf;
10341 Elf_Internal_Sym *isym;
10342 Elf_Internal_Sym *isymend;
10343 long *pindex;
10344 asection **ppsection;
10345 asection *o;
10346 const struct elf_backend_data *bed;
c152c796 10347 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10348 bfd_size_type address_size;
10349 bfd_vma r_type_mask;
10350 int r_sym_shift;
ffbc01cc 10351 bfd_boolean have_file_sym = FALSE;
c152c796 10352
8b127cbc 10353 output_bfd = flinfo->output_bfd;
c152c796
AM
10354 bed = get_elf_backend_data (output_bfd);
10355 relocate_section = bed->elf_backend_relocate_section;
10356
10357 /* If this is a dynamic object, we don't want to do anything here:
10358 we don't want the local symbols, and we don't want the section
10359 contents. */
10360 if ((input_bfd->flags & DYNAMIC) != 0)
10361 return TRUE;
10362
c152c796
AM
10363 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10364 if (elf_bad_symtab (input_bfd))
10365 {
10366 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10367 extsymoff = 0;
10368 }
10369 else
10370 {
10371 locsymcount = symtab_hdr->sh_info;
10372 extsymoff = symtab_hdr->sh_info;
10373 }
10374
10375 /* Read the local symbols. */
10376 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10377 if (isymbuf == NULL && locsymcount != 0)
10378 {
10379 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10380 flinfo->internal_syms,
10381 flinfo->external_syms,
10382 flinfo->locsym_shndx);
c152c796
AM
10383 if (isymbuf == NULL)
10384 return FALSE;
10385 }
10386
10387 /* Find local symbol sections and adjust values of symbols in
10388 SEC_MERGE sections. Write out those local symbols we know are
10389 going into the output file. */
10390 isymend = isymbuf + locsymcount;
8b127cbc 10391 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10392 isym < isymend;
10393 isym++, pindex++, ppsection++)
10394 {
10395 asection *isec;
10396 const char *name;
10397 Elf_Internal_Sym osym;
6e0b88f1
AM
10398 long indx;
10399 int ret;
c152c796
AM
10400
10401 *pindex = -1;
10402
10403 if (elf_bad_symtab (input_bfd))
10404 {
10405 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10406 {
10407 *ppsection = NULL;
10408 continue;
10409 }
10410 }
10411
10412 if (isym->st_shndx == SHN_UNDEF)
10413 isec = bfd_und_section_ptr;
c152c796
AM
10414 else if (isym->st_shndx == SHN_ABS)
10415 isec = bfd_abs_section_ptr;
10416 else if (isym->st_shndx == SHN_COMMON)
10417 isec = bfd_com_section_ptr;
10418 else
10419 {
cb33740c
AM
10420 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10421 if (isec == NULL)
10422 {
10423 /* Don't attempt to output symbols with st_shnx in the
10424 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10425 isec = bfd_und_section_ptr;
cb33740c 10426 }
dbaa2011 10427 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10428 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10429 isym->st_value =
10430 _bfd_merged_section_offset (output_bfd, &isec,
10431 elf_section_data (isec)->sec_info,
10432 isym->st_value);
c152c796
AM
10433 }
10434
10435 *ppsection = isec;
10436
d983c8c5
AM
10437 /* Don't output the first, undefined, symbol. In fact, don't
10438 output any undefined local symbol. */
10439 if (isec == bfd_und_section_ptr)
c152c796
AM
10440 continue;
10441
10442 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10443 {
10444 /* We never output section symbols. Instead, we use the
10445 section symbol of the corresponding section in the output
10446 file. */
10447 continue;
10448 }
10449
10450 /* If we are stripping all symbols, we don't want to output this
10451 one. */
8b127cbc 10452 if (flinfo->info->strip == strip_all)
c152c796
AM
10453 continue;
10454
10455 /* If we are discarding all local symbols, we don't want to
10456 output this one. If we are generating a relocatable output
10457 file, then some of the local symbols may be required by
10458 relocs; we output them below as we discover that they are
10459 needed. */
8b127cbc 10460 if (flinfo->info->discard == discard_all)
c152c796
AM
10461 continue;
10462
10463 /* If this symbol is defined in a section which we are
f02571c5
AM
10464 discarding, we don't need to keep it. */
10465 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10466 && isym->st_shndx < SHN_LORESERVE
10467 && bfd_section_removed_from_list (output_bfd,
10468 isec->output_section))
e75a280b
L
10469 continue;
10470
c152c796
AM
10471 /* Get the name of the symbol. */
10472 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10473 isym->st_name);
10474 if (name == NULL)
10475 return FALSE;
10476
10477 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10478 if ((flinfo->info->strip == strip_some
10479 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10480 == NULL))
8b127cbc 10481 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10482 && (isec->flags & SEC_MERGE)
10483 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10484 || flinfo->info->discard == discard_l)
c152c796
AM
10485 && bfd_is_local_label_name (input_bfd, name)))
10486 continue;
10487
ffbc01cc
AM
10488 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10489 {
ce875075
AM
10490 if (input_bfd->lto_output)
10491 /* -flto puts a temp file name here. This means builds
10492 are not reproducible. Discard the symbol. */
10493 continue;
ffbc01cc
AM
10494 have_file_sym = TRUE;
10495 flinfo->filesym_count += 1;
10496 }
10497 if (!have_file_sym)
10498 {
10499 /* In the absence of debug info, bfd_find_nearest_line uses
10500 FILE symbols to determine the source file for local
10501 function symbols. Provide a FILE symbol here if input
10502 files lack such, so that their symbols won't be
10503 associated with a previous input file. It's not the
10504 source file, but the best we can do. */
10505 have_file_sym = TRUE;
10506 flinfo->filesym_count += 1;
10507 memset (&osym, 0, sizeof (osym));
10508 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10509 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10510 if (!elf_link_output_symstrtab (flinfo,
10511 (input_bfd->lto_output ? NULL
10512 : input_bfd->filename),
10513 &osym, bfd_abs_section_ptr,
10514 NULL))
ffbc01cc
AM
10515 return FALSE;
10516 }
10517
c152c796
AM
10518 osym = *isym;
10519
10520 /* Adjust the section index for the output file. */
10521 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10522 isec->output_section);
10523 if (osym.st_shndx == SHN_BAD)
10524 return FALSE;
10525
c152c796
AM
10526 /* ELF symbols in relocatable files are section relative, but
10527 in executable files they are virtual addresses. Note that
10528 this code assumes that all ELF sections have an associated
10529 BFD section with a reasonable value for output_offset; below
10530 we assume that they also have a reasonable value for
10531 output_section. Any special sections must be set up to meet
10532 these requirements. */
10533 osym.st_value += isec->output_offset;
0e1862bb 10534 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10535 {
10536 osym.st_value += isec->output_section->vma;
10537 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10538 {
10539 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10540 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10541 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10542 else
10543 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10544 STT_NOTYPE);
c152c796
AM
10545 }
10546 }
10547
6e0b88f1 10548 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10549 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10550 if (ret == 0)
c152c796 10551 return FALSE;
6e0b88f1
AM
10552 else if (ret == 1)
10553 *pindex = indx;
c152c796
AM
10554 }
10555
310fd250
L
10556 if (bed->s->arch_size == 32)
10557 {
10558 r_type_mask = 0xff;
10559 r_sym_shift = 8;
10560 address_size = 4;
10561 }
10562 else
10563 {
10564 r_type_mask = 0xffffffff;
10565 r_sym_shift = 32;
10566 address_size = 8;
10567 }
10568
c152c796
AM
10569 /* Relocate the contents of each section. */
10570 sym_hashes = elf_sym_hashes (input_bfd);
10571 for (o = input_bfd->sections; o != NULL; o = o->next)
10572 {
10573 bfd_byte *contents;
10574
10575 if (! o->linker_mark)
10576 {
10577 /* This section was omitted from the link. */
10578 continue;
10579 }
10580
7bdf4127 10581 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10582 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10583 {
10584 /* Deal with the group signature symbol. */
10585 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10586 unsigned long symndx = sec_data->this_hdr.sh_info;
10587 asection *osec = o->output_section;
10588
7bdf4127 10589 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10590 if (symndx >= locsymcount
10591 || (elf_bad_symtab (input_bfd)
8b127cbc 10592 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10593 {
10594 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10595 while (h->root.type == bfd_link_hash_indirect
10596 || h->root.type == bfd_link_hash_warning)
10597 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10598 /* Arrange for symbol to be output. */
10599 h->indx = -2;
10600 elf_section_data (osec)->this_hdr.sh_info = -2;
10601 }
10602 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10603 {
10604 /* We'll use the output section target_index. */
8b127cbc 10605 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10606 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10607 }
10608 else
10609 {
8b127cbc 10610 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10611 {
10612 /* Otherwise output the local symbol now. */
10613 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10614 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10615 const char *name;
6e0b88f1
AM
10616 long indx;
10617 int ret;
bcacc0f5
AM
10618
10619 name = bfd_elf_string_from_elf_section (input_bfd,
10620 symtab_hdr->sh_link,
10621 sym.st_name);
10622 if (name == NULL)
10623 return FALSE;
10624
10625 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10626 sec);
10627 if (sym.st_shndx == SHN_BAD)
10628 return FALSE;
10629
10630 sym.st_value += o->output_offset;
10631
6e0b88f1 10632 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10633 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10634 NULL);
6e0b88f1 10635 if (ret == 0)
bcacc0f5 10636 return FALSE;
6e0b88f1 10637 else if (ret == 1)
8b127cbc 10638 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10639 else
10640 abort ();
bcacc0f5
AM
10641 }
10642 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10643 = flinfo->indices[symndx];
bcacc0f5
AM
10644 }
10645 }
10646
c152c796 10647 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10648 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10649 continue;
10650
10651 if ((o->flags & SEC_LINKER_CREATED) != 0)
10652 {
10653 /* Section was created by _bfd_elf_link_create_dynamic_sections
10654 or somesuch. */
10655 continue;
10656 }
10657
10658 /* Get the contents of the section. They have been cached by a
10659 relaxation routine. Note that o is a section in an input
10660 file, so the contents field will not have been set by any of
10661 the routines which work on output files. */
10662 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10663 {
10664 contents = elf_section_data (o)->this_hdr.contents;
10665 if (bed->caches_rawsize
10666 && o->rawsize != 0
10667 && o->rawsize < o->size)
10668 {
10669 memcpy (flinfo->contents, contents, o->rawsize);
10670 contents = flinfo->contents;
10671 }
10672 }
c152c796
AM
10673 else
10674 {
8b127cbc 10675 contents = flinfo->contents;
4a114e3e 10676 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10677 return FALSE;
10678 }
10679
10680 if ((o->flags & SEC_RELOC) != 0)
10681 {
10682 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10683 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10684 int action_discarded;
ece5ef60 10685 int ret;
c152c796
AM
10686
10687 /* Get the swapped relocs. */
10688 internal_relocs
8b127cbc
AM
10689 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10690 flinfo->internal_relocs, FALSE);
c152c796
AM
10691 if (internal_relocs == NULL
10692 && o->reloc_count > 0)
10693 return FALSE;
10694
310fd250
L
10695 /* We need to reverse-copy input .ctors/.dtors sections if
10696 they are placed in .init_array/.finit_array for output. */
10697 if (o->size > address_size
10698 && ((strncmp (o->name, ".ctors", 6) == 0
10699 && strcmp (o->output_section->name,
10700 ".init_array") == 0)
10701 || (strncmp (o->name, ".dtors", 6) == 0
10702 && strcmp (o->output_section->name,
10703 ".fini_array") == 0))
10704 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10705 {
056bafd4
MR
10706 if (o->size * bed->s->int_rels_per_ext_rel
10707 != o->reloc_count * address_size)
310fd250 10708 {
4eca0228 10709 _bfd_error_handler
695344c0 10710 /* xgettext:c-format */
871b3ab2 10711 (_("error: %pB: size of section %pA is not "
310fd250
L
10712 "multiple of address size"),
10713 input_bfd, o);
8c6716e5 10714 bfd_set_error (bfd_error_bad_value);
310fd250
L
10715 return FALSE;
10716 }
10717 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10718 }
10719
0f02bbd9 10720 action_discarded = -1;
c152c796 10721 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10722 action_discarded = (*bed->action_discarded) (o);
10723
10724 /* Run through the relocs evaluating complex reloc symbols and
10725 looking for relocs against symbols from discarded sections
10726 or section symbols from removed link-once sections.
10727 Complain about relocs against discarded sections. Zero
10728 relocs against removed link-once sections. */
10729
10730 rel = internal_relocs;
056bafd4 10731 relend = rel + o->reloc_count;
0f02bbd9 10732 for ( ; rel < relend; rel++)
c152c796 10733 {
0f02bbd9
AM
10734 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10735 unsigned int s_type;
10736 asection **ps, *sec;
10737 struct elf_link_hash_entry *h = NULL;
10738 const char *sym_name;
c152c796 10739
0f02bbd9
AM
10740 if (r_symndx == STN_UNDEF)
10741 continue;
c152c796 10742
0f02bbd9
AM
10743 if (r_symndx >= locsymcount
10744 || (elf_bad_symtab (input_bfd)
8b127cbc 10745 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10746 {
10747 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10748
0f02bbd9
AM
10749 /* Badly formatted input files can contain relocs that
10750 reference non-existant symbols. Check here so that
10751 we do not seg fault. */
10752 if (h == NULL)
c152c796 10753 {
4eca0228 10754 _bfd_error_handler
695344c0 10755 /* xgettext:c-format */
2dcf00ce 10756 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10757 "that references a non-existent global symbol"),
2dcf00ce 10758 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10759 bfd_set_error (bfd_error_bad_value);
10760 return FALSE;
10761 }
3b36f7e6 10762
0f02bbd9
AM
10763 while (h->root.type == bfd_link_hash_indirect
10764 || h->root.type == bfd_link_hash_warning)
10765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10766
0f02bbd9 10767 s_type = h->type;
cdd3575c 10768
9e2dec47 10769 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10770 mark the symbol as undefined. Note that the
10771 linker may attach linker created dynamic sections
10772 to the plugin bfd. Symbols defined in linker
10773 created sections are not plugin symbols. */
bc4e12de 10774 if ((h->root.non_ir_ref_regular
4070765b 10775 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10776 && (h->root.type == bfd_link_hash_defined
10777 || h->root.type == bfd_link_hash_defweak)
10778 && (h->root.u.def.section->flags
10779 & SEC_LINKER_CREATED) == 0
10780 && h->root.u.def.section->owner != NULL
10781 && (h->root.u.def.section->owner->flags
10782 & BFD_PLUGIN) != 0)
10783 {
10784 h->root.type = bfd_link_hash_undefined;
10785 h->root.u.undef.abfd = h->root.u.def.section->owner;
10786 }
10787
0f02bbd9
AM
10788 ps = NULL;
10789 if (h->root.type == bfd_link_hash_defined
10790 || h->root.type == bfd_link_hash_defweak)
10791 ps = &h->root.u.def.section;
10792
10793 sym_name = h->root.root.string;
10794 }
10795 else
10796 {
10797 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10798
10799 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10800 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10801 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10802 sym, *ps);
10803 }
c152c796 10804
c301e700 10805 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10806 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10807 {
10808 bfd_vma val;
10809 bfd_vma dot = (rel->r_offset
10810 + o->output_offset + o->output_section->vma);
10811#ifdef DEBUG
10812 printf ("Encountered a complex symbol!");
10813 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10814 input_bfd->filename, o->name,
10815 (long) (rel - internal_relocs));
0f02bbd9
AM
10816 printf (" symbol: idx %8.8lx, name %s\n",
10817 r_symndx, sym_name);
10818 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10819 (unsigned long) rel->r_info,
10820 (unsigned long) rel->r_offset);
10821#endif
8b127cbc 10822 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10823 isymbuf, locsymcount, s_type == STT_SRELC))
10824 return FALSE;
10825
10826 /* Symbol evaluated OK. Update to absolute value. */
10827 set_symbol_value (input_bfd, isymbuf, locsymcount,
10828 r_symndx, val);
10829 continue;
10830 }
10831
10832 if (action_discarded != -1 && ps != NULL)
10833 {
cdd3575c
AM
10834 /* Complain if the definition comes from a
10835 discarded section. */
dbaa2011 10836 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10837 {
cf35638d 10838 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10839 if (action_discarded & COMPLAIN)
8b127cbc 10840 (*flinfo->info->callbacks->einfo)
695344c0 10841 /* xgettext:c-format */
871b3ab2
AM
10842 (_("%X`%s' referenced in section `%pA' of %pB: "
10843 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10844 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10845
87e5235d 10846 /* Try to do the best we can to support buggy old
e0ae6d6f 10847 versions of gcc. Pretend that the symbol is
87e5235d
AM
10848 really defined in the kept linkonce section.
10849 FIXME: This is quite broken. Modifying the
10850 symbol here means we will be changing all later
e0ae6d6f 10851 uses of the symbol, not just in this section. */
0f02bbd9 10852 if (action_discarded & PRETEND)
87e5235d 10853 {
01b3c8ab
L
10854 asection *kept;
10855
c0f00686 10856 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10857 flinfo->info);
01b3c8ab 10858 if (kept != NULL)
87e5235d
AM
10859 {
10860 *ps = kept;
10861 continue;
10862 }
10863 }
c152c796
AM
10864 }
10865 }
10866 }
10867
10868 /* Relocate the section by invoking a back end routine.
10869
10870 The back end routine is responsible for adjusting the
10871 section contents as necessary, and (if using Rela relocs
10872 and generating a relocatable output file) adjusting the
10873 reloc addend as necessary.
10874
10875 The back end routine does not have to worry about setting
10876 the reloc address or the reloc symbol index.
10877
10878 The back end routine is given a pointer to the swapped in
10879 internal symbols, and can access the hash table entries
10880 for the external symbols via elf_sym_hashes (input_bfd).
10881
10882 When generating relocatable output, the back end routine
10883 must handle STB_LOCAL/STT_SECTION symbols specially. The
10884 output symbol is going to be a section symbol
10885 corresponding to the output section, which will require
10886 the addend to be adjusted. */
10887
8b127cbc 10888 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10889 input_bfd, o, contents,
10890 internal_relocs,
10891 isymbuf,
8b127cbc 10892 flinfo->sections);
ece5ef60 10893 if (!ret)
c152c796
AM
10894 return FALSE;
10895
ece5ef60 10896 if (ret == 2
0e1862bb 10897 || bfd_link_relocatable (flinfo->info)
8b127cbc 10898 || flinfo->info->emitrelocations)
c152c796
AM
10899 {
10900 Elf_Internal_Rela *irela;
d4730f92 10901 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10902 bfd_vma last_offset;
10903 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10904 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10905 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10906 unsigned int next_erel;
c152c796 10907 bfd_boolean rela_normal;
d4730f92 10908 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10909
d4730f92
BS
10910 esdi = elf_section_data (o);
10911 esdo = elf_section_data (o->output_section);
10912 rela_normal = FALSE;
c152c796
AM
10913
10914 /* Adjust the reloc addresses and symbol indices. */
10915
10916 irela = internal_relocs;
056bafd4 10917 irelaend = irela + o->reloc_count;
d4730f92
BS
10918 rel_hash = esdo->rel.hashes + esdo->rel.count;
10919 /* We start processing the REL relocs, if any. When we reach
10920 IRELAMID in the loop, we switch to the RELA relocs. */
10921 irelamid = irela;
10922 if (esdi->rel.hdr != NULL)
10923 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10924 * bed->s->int_rels_per_ext_rel);
eac338cf 10925 rel_hash_list = rel_hash;
d4730f92 10926 rela_hash_list = NULL;
c152c796 10927 last_offset = o->output_offset;
0e1862bb 10928 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10929 last_offset += o->output_section->vma;
10930 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10931 {
10932 unsigned long r_symndx;
10933 asection *sec;
10934 Elf_Internal_Sym sym;
10935
10936 if (next_erel == bed->s->int_rels_per_ext_rel)
10937 {
10938 rel_hash++;
10939 next_erel = 0;
10940 }
10941
d4730f92
BS
10942 if (irela == irelamid)
10943 {
10944 rel_hash = esdo->rela.hashes + esdo->rela.count;
10945 rela_hash_list = rel_hash;
10946 rela_normal = bed->rela_normal;
10947 }
10948
c152c796 10949 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10950 flinfo->info, o,
c152c796
AM
10951 irela->r_offset);
10952 if (irela->r_offset >= (bfd_vma) -2)
10953 {
10954 /* This is a reloc for a deleted entry or somesuch.
10955 Turn it into an R_*_NONE reloc, at the same
10956 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10957 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10958 being ordered. */
10959 irela->r_offset = last_offset;
10960 irela->r_info = 0;
10961 irela->r_addend = 0;
10962 continue;
10963 }
10964
10965 irela->r_offset += o->output_offset;
10966
10967 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10968 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10969 irela->r_offset += o->output_section->vma;
10970
10971 last_offset = irela->r_offset;
10972
10973 r_symndx = irela->r_info >> r_sym_shift;
10974 if (r_symndx == STN_UNDEF)
10975 continue;
10976
10977 if (r_symndx >= locsymcount
10978 || (elf_bad_symtab (input_bfd)
8b127cbc 10979 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10980 {
10981 struct elf_link_hash_entry *rh;
10982 unsigned long indx;
10983
10984 /* This is a reloc against a global symbol. We
10985 have not yet output all the local symbols, so
10986 we do not know the symbol index of any global
10987 symbol. We set the rel_hash entry for this
10988 reloc to point to the global hash table entry
10989 for this symbol. The symbol index is then
ee75fd95 10990 set at the end of bfd_elf_final_link. */
c152c796
AM
10991 indx = r_symndx - extsymoff;
10992 rh = elf_sym_hashes (input_bfd)[indx];
10993 while (rh->root.type == bfd_link_hash_indirect
10994 || rh->root.type == bfd_link_hash_warning)
10995 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10996
10997 /* Setting the index to -2 tells
10998 elf_link_output_extsym that this symbol is
10999 used by a reloc. */
11000 BFD_ASSERT (rh->indx < 0);
11001 rh->indx = -2;
c152c796
AM
11002 *rel_hash = rh;
11003
11004 continue;
11005 }
11006
11007 /* This is a reloc against a local symbol. */
11008
11009 *rel_hash = NULL;
11010 sym = isymbuf[r_symndx];
8b127cbc 11011 sec = flinfo->sections[r_symndx];
c152c796
AM
11012 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11013 {
11014 /* I suppose the backend ought to fill in the
11015 section of any STT_SECTION symbol against a
6a8d1586 11016 processor specific section. */
cf35638d 11017 r_symndx = STN_UNDEF;
6a8d1586
AM
11018 if (bfd_is_abs_section (sec))
11019 ;
c152c796
AM
11020 else if (sec == NULL || sec->owner == NULL)
11021 {
11022 bfd_set_error (bfd_error_bad_value);
11023 return FALSE;
11024 }
11025 else
11026 {
6a8d1586
AM
11027 asection *osec = sec->output_section;
11028
11029 /* If we have discarded a section, the output
11030 section will be the absolute section. In
ab96bf03
AM
11031 case of discarded SEC_MERGE sections, use
11032 the kept section. relocate_section should
11033 have already handled discarded linkonce
11034 sections. */
6a8d1586
AM
11035 if (bfd_is_abs_section (osec)
11036 && sec->kept_section != NULL
11037 && sec->kept_section->output_section != NULL)
11038 {
11039 osec = sec->kept_section->output_section;
11040 irela->r_addend -= osec->vma;
11041 }
11042
11043 if (!bfd_is_abs_section (osec))
11044 {
11045 r_symndx = osec->target_index;
cf35638d 11046 if (r_symndx == STN_UNDEF)
74541ad4 11047 {
051d833a
AM
11048 irela->r_addend += osec->vma;
11049 osec = _bfd_nearby_section (output_bfd, osec,
11050 osec->vma);
11051 irela->r_addend -= osec->vma;
11052 r_symndx = osec->target_index;
74541ad4 11053 }
6a8d1586 11054 }
c152c796
AM
11055 }
11056
11057 /* Adjust the addend according to where the
11058 section winds up in the output section. */
11059 if (rela_normal)
11060 irela->r_addend += sec->output_offset;
11061 }
11062 else
11063 {
8b127cbc 11064 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11065 {
11066 unsigned long shlink;
11067 const char *name;
11068 asection *osec;
6e0b88f1 11069 long indx;
c152c796 11070
8b127cbc 11071 if (flinfo->info->strip == strip_all)
c152c796
AM
11072 {
11073 /* You can't do ld -r -s. */
11074 bfd_set_error (bfd_error_invalid_operation);
11075 return FALSE;
11076 }
11077
11078 /* This symbol was skipped earlier, but
11079 since it is needed by a reloc, we
11080 must output it now. */
11081 shlink = symtab_hdr->sh_link;
11082 name = (bfd_elf_string_from_elf_section
11083 (input_bfd, shlink, sym.st_name));
11084 if (name == NULL)
11085 return FALSE;
11086
11087 osec = sec->output_section;
11088 sym.st_shndx =
11089 _bfd_elf_section_from_bfd_section (output_bfd,
11090 osec);
11091 if (sym.st_shndx == SHN_BAD)
11092 return FALSE;
11093
11094 sym.st_value += sec->output_offset;
0e1862bb 11095 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11096 {
11097 sym.st_value += osec->vma;
11098 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11099 {
102def4d
AM
11100 struct elf_link_hash_table *htab
11101 = elf_hash_table (flinfo->info);
11102
c152c796
AM
11103 /* STT_TLS symbols are relative to PT_TLS
11104 segment base. */
102def4d
AM
11105 if (htab->tls_sec != NULL)
11106 sym.st_value -= htab->tls_sec->vma;
11107 else
11108 sym.st_info
11109 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11110 STT_NOTYPE);
c152c796
AM
11111 }
11112 }
11113
6e0b88f1 11114 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11115 ret = elf_link_output_symstrtab (flinfo, name,
11116 &sym, sec,
11117 NULL);
6e0b88f1 11118 if (ret == 0)
c152c796 11119 return FALSE;
6e0b88f1 11120 else if (ret == 1)
8b127cbc 11121 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11122 else
11123 abort ();
c152c796
AM
11124 }
11125
8b127cbc 11126 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11127 }
11128
11129 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11130 | (irela->r_info & r_type_mask));
11131 }
11132
11133 /* Swap out the relocs. */
d4730f92
BS
11134 input_rel_hdr = esdi->rel.hdr;
11135 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11136 {
d4730f92
BS
11137 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11138 input_rel_hdr,
11139 internal_relocs,
11140 rel_hash_list))
11141 return FALSE;
c152c796
AM
11142 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11143 * bed->s->int_rels_per_ext_rel);
eac338cf 11144 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11145 }
11146
11147 input_rela_hdr = esdi->rela.hdr;
11148 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11149 {
eac338cf 11150 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11151 input_rela_hdr,
eac338cf 11152 internal_relocs,
d4730f92 11153 rela_hash_list))
c152c796
AM
11154 return FALSE;
11155 }
11156 }
11157 }
11158
11159 /* Write out the modified section contents. */
11160 if (bed->elf_backend_write_section
8b127cbc 11161 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11162 contents))
c152c796
AM
11163 {
11164 /* Section written out. */
11165 }
11166 else switch (o->sec_info_type)
11167 {
dbaa2011 11168 case SEC_INFO_TYPE_STABS:
c152c796
AM
11169 if (! (_bfd_write_section_stabs
11170 (output_bfd,
8b127cbc 11171 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11172 o, &elf_section_data (o)->sec_info, contents)))
11173 return FALSE;
11174 break;
dbaa2011 11175 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11176 if (! _bfd_write_merged_section (output_bfd, o,
11177 elf_section_data (o)->sec_info))
11178 return FALSE;
11179 break;
dbaa2011 11180 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11181 {
8b127cbc 11182 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11183 o, contents))
11184 return FALSE;
11185 }
11186 break;
2f0c68f2
CM
11187 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11188 {
11189 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11190 flinfo->info,
11191 o, contents))
11192 return FALSE;
11193 }
11194 break;
c152c796
AM
11195 default:
11196 {
310fd250
L
11197 if (! (o->flags & SEC_EXCLUDE))
11198 {
11199 file_ptr offset = (file_ptr) o->output_offset;
11200 bfd_size_type todo = o->size;
37b01f6a
DG
11201
11202 offset *= bfd_octets_per_byte (output_bfd);
11203
310fd250
L
11204 if ((o->flags & SEC_ELF_REVERSE_COPY))
11205 {
11206 /* Reverse-copy input section to output. */
11207 do
11208 {
11209 todo -= address_size;
11210 if (! bfd_set_section_contents (output_bfd,
11211 o->output_section,
11212 contents + todo,
11213 offset,
11214 address_size))
11215 return FALSE;
11216 if (todo == 0)
11217 break;
11218 offset += address_size;
11219 }
11220 while (1);
11221 }
11222 else if (! bfd_set_section_contents (output_bfd,
11223 o->output_section,
11224 contents,
11225 offset, todo))
11226 return FALSE;
11227 }
c152c796
AM
11228 }
11229 break;
11230 }
11231 }
11232
11233 return TRUE;
11234}
11235
11236/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11237 requested by the linker, and does not come from any input file. This
c152c796
AM
11238 is used to build constructor and destructor tables when linking
11239 with -Ur. */
11240
11241static bfd_boolean
11242elf_reloc_link_order (bfd *output_bfd,
11243 struct bfd_link_info *info,
11244 asection *output_section,
11245 struct bfd_link_order *link_order)
11246{
11247 reloc_howto_type *howto;
11248 long indx;
11249 bfd_vma offset;
11250 bfd_vma addend;
d4730f92 11251 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11252 struct elf_link_hash_entry **rel_hash_ptr;
11253 Elf_Internal_Shdr *rel_hdr;
11254 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11255 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11256 bfd_byte *erel;
11257 unsigned int i;
d4730f92 11258 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11259
11260 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11261 if (howto == NULL)
11262 {
11263 bfd_set_error (bfd_error_bad_value);
11264 return FALSE;
11265 }
11266
11267 addend = link_order->u.reloc.p->addend;
11268
d4730f92
BS
11269 if (esdo->rel.hdr)
11270 reldata = &esdo->rel;
11271 else if (esdo->rela.hdr)
11272 reldata = &esdo->rela;
11273 else
11274 {
11275 reldata = NULL;
11276 BFD_ASSERT (0);
11277 }
11278
c152c796 11279 /* Figure out the symbol index. */
d4730f92 11280 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11281 if (link_order->type == bfd_section_reloc_link_order)
11282 {
11283 indx = link_order->u.reloc.p->u.section->target_index;
11284 BFD_ASSERT (indx != 0);
11285 *rel_hash_ptr = NULL;
11286 }
11287 else
11288 {
11289 struct elf_link_hash_entry *h;
11290
11291 /* Treat a reloc against a defined symbol as though it were
11292 actually against the section. */
11293 h = ((struct elf_link_hash_entry *)
11294 bfd_wrapped_link_hash_lookup (output_bfd, info,
11295 link_order->u.reloc.p->u.name,
11296 FALSE, FALSE, TRUE));
11297 if (h != NULL
11298 && (h->root.type == bfd_link_hash_defined
11299 || h->root.type == bfd_link_hash_defweak))
11300 {
11301 asection *section;
11302
11303 section = h->root.u.def.section;
11304 indx = section->output_section->target_index;
11305 *rel_hash_ptr = NULL;
11306 /* It seems that we ought to add the symbol value to the
11307 addend here, but in practice it has already been added
11308 because it was passed to constructor_callback. */
11309 addend += section->output_section->vma + section->output_offset;
11310 }
11311 else if (h != NULL)
11312 {
11313 /* Setting the index to -2 tells elf_link_output_extsym that
11314 this symbol is used by a reloc. */
11315 h->indx = -2;
11316 *rel_hash_ptr = h;
11317 indx = 0;
11318 }
11319 else
11320 {
1a72702b
AM
11321 (*info->callbacks->unattached_reloc)
11322 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11323 indx = 0;
11324 }
11325 }
11326
11327 /* If this is an inplace reloc, we must write the addend into the
11328 object file. */
11329 if (howto->partial_inplace && addend != 0)
11330 {
11331 bfd_size_type size;
11332 bfd_reloc_status_type rstat;
11333 bfd_byte *buf;
11334 bfd_boolean ok;
11335 const char *sym_name;
11336
a50b1753
NC
11337 size = (bfd_size_type) bfd_get_reloc_size (howto);
11338 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11339 if (buf == NULL && size != 0)
c152c796
AM
11340 return FALSE;
11341 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11342 switch (rstat)
11343 {
11344 case bfd_reloc_ok:
11345 break;
11346
11347 default:
11348 case bfd_reloc_outofrange:
11349 abort ();
11350
11351 case bfd_reloc_overflow:
11352 if (link_order->type == bfd_section_reloc_link_order)
11353 sym_name = bfd_section_name (output_bfd,
11354 link_order->u.reloc.p->u.section);
11355 else
11356 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11357 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11358 howto->name, addend, NULL, NULL,
11359 (bfd_vma) 0);
c152c796
AM
11360 break;
11361 }
37b01f6a 11362
c152c796 11363 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11364 link_order->offset
11365 * bfd_octets_per_byte (output_bfd),
11366 size);
c152c796
AM
11367 free (buf);
11368 if (! ok)
11369 return FALSE;
11370 }
11371
11372 /* The address of a reloc is relative to the section in a
11373 relocatable file, and is a virtual address in an executable
11374 file. */
11375 offset = link_order->offset;
0e1862bb 11376 if (! bfd_link_relocatable (info))
c152c796
AM
11377 offset += output_section->vma;
11378
11379 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11380 {
11381 irel[i].r_offset = offset;
11382 irel[i].r_info = 0;
11383 irel[i].r_addend = 0;
11384 }
11385 if (bed->s->arch_size == 32)
11386 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11387 else
11388 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11389
d4730f92 11390 rel_hdr = reldata->hdr;
c152c796
AM
11391 erel = rel_hdr->contents;
11392 if (rel_hdr->sh_type == SHT_REL)
11393 {
d4730f92 11394 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11395 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11396 }
11397 else
11398 {
11399 irel[0].r_addend = addend;
d4730f92 11400 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11401 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11402 }
11403
d4730f92 11404 ++reldata->count;
c152c796
AM
11405
11406 return TRUE;
11407}
11408
0b52efa6
PB
11409
11410/* Get the output vma of the section pointed to by the sh_link field. */
11411
11412static bfd_vma
11413elf_get_linked_section_vma (struct bfd_link_order *p)
11414{
11415 Elf_Internal_Shdr **elf_shdrp;
11416 asection *s;
11417 int elfsec;
11418
11419 s = p->u.indirect.section;
11420 elf_shdrp = elf_elfsections (s->owner);
11421 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11422 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11423 /* PR 290:
11424 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11425 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11426 sh_info fields. Hence we could get the situation
11427 where elfsec is 0. */
11428 if (elfsec == 0)
11429 {
11430 const struct elf_backend_data *bed
11431 = get_elf_backend_data (s->owner);
11432 if (bed->link_order_error_handler)
d003868e 11433 bed->link_order_error_handler
695344c0 11434 /* xgettext:c-format */
871b3ab2 11435 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11436 return 0;
11437 }
11438 else
11439 {
11440 s = elf_shdrp[elfsec]->bfd_section;
11441 return s->output_section->vma + s->output_offset;
11442 }
0b52efa6
PB
11443}
11444
11445
11446/* Compare two sections based on the locations of the sections they are
11447 linked to. Used by elf_fixup_link_order. */
11448
11449static int
11450compare_link_order (const void * a, const void * b)
11451{
11452 bfd_vma apos;
11453 bfd_vma bpos;
11454
11455 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11456 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11457 if (apos < bpos)
11458 return -1;
11459 return apos > bpos;
11460}
11461
11462
11463/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11464 order as their linked sections. Returns false if this could not be done
11465 because an output section includes both ordered and unordered
11466 sections. Ideally we'd do this in the linker proper. */
11467
11468static bfd_boolean
11469elf_fixup_link_order (bfd *abfd, asection *o)
11470{
11471 int seen_linkorder;
11472 int seen_other;
11473 int n;
11474 struct bfd_link_order *p;
11475 bfd *sub;
11476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11477 unsigned elfsec;
0b52efa6 11478 struct bfd_link_order **sections;
d33cdfe3 11479 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11480 bfd_vma offset;
3b36f7e6 11481
d33cdfe3
L
11482 other_sec = NULL;
11483 linkorder_sec = NULL;
0b52efa6
PB
11484 seen_other = 0;
11485 seen_linkorder = 0;
8423293d 11486 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11487 {
d33cdfe3 11488 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11489 {
11490 s = p->u.indirect.section;
d33cdfe3
L
11491 sub = s->owner;
11492 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11493 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11494 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11495 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11496 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11497 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11498 {
11499 seen_linkorder++;
11500 linkorder_sec = s;
11501 }
0b52efa6 11502 else
d33cdfe3
L
11503 {
11504 seen_other++;
11505 other_sec = s;
11506 }
0b52efa6
PB
11507 }
11508 else
11509 seen_other++;
d33cdfe3
L
11510
11511 if (seen_other && seen_linkorder)
11512 {
11513 if (other_sec && linkorder_sec)
4eca0228 11514 _bfd_error_handler
695344c0 11515 /* xgettext:c-format */
871b3ab2
AM
11516 (_("%pA has both ordered [`%pA' in %pB] "
11517 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11518 o, linkorder_sec, linkorder_sec->owner,
11519 other_sec, other_sec->owner);
d33cdfe3 11520 else
4eca0228 11521 _bfd_error_handler
871b3ab2 11522 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11523 bfd_set_error (bfd_error_bad_value);
11524 return FALSE;
11525 }
0b52efa6
PB
11526 }
11527
11528 if (!seen_linkorder)
11529 return TRUE;
11530
0b52efa6 11531 sections = (struct bfd_link_order **)
14b1c01e
AM
11532 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11533 if (sections == NULL)
11534 return FALSE;
0b52efa6 11535 seen_linkorder = 0;
3b36f7e6 11536
8423293d 11537 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11538 {
11539 sections[seen_linkorder++] = p;
11540 }
11541 /* Sort the input sections in the order of their linked section. */
11542 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11543 compare_link_order);
11544
11545 /* Change the offsets of the sections. */
11546 offset = 0;
11547 for (n = 0; n < seen_linkorder; n++)
11548 {
11549 s = sections[n]->u.indirect.section;
461686a3 11550 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11551 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11552 sections[n]->offset = offset;
11553 offset += sections[n]->size;
11554 }
11555
4dd07732 11556 free (sections);
0b52efa6
PB
11557 return TRUE;
11558}
11559
76359541
TP
11560/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11561 Returns TRUE upon success, FALSE otherwise. */
11562
11563static bfd_boolean
11564elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11565{
11566 bfd_boolean ret = FALSE;
11567 bfd *implib_bfd;
11568 const struct elf_backend_data *bed;
11569 flagword flags;
11570 enum bfd_architecture arch;
11571 unsigned int mach;
11572 asymbol **sympp = NULL;
11573 long symsize;
11574 long symcount;
11575 long src_count;
11576 elf_symbol_type *osymbuf;
11577
11578 implib_bfd = info->out_implib_bfd;
11579 bed = get_elf_backend_data (abfd);
11580
11581 if (!bfd_set_format (implib_bfd, bfd_object))
11582 return FALSE;
11583
046734ff 11584 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11585 flags = bfd_get_file_flags (abfd);
11586 flags &= ~HAS_RELOC;
11587 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11588 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11589 return FALSE;
11590
11591 /* Copy architecture of output file to import library file. */
11592 arch = bfd_get_arch (abfd);
11593 mach = bfd_get_mach (abfd);
11594 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11595 && (abfd->target_defaulted
11596 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11597 return FALSE;
11598
11599 /* Get symbol table size. */
11600 symsize = bfd_get_symtab_upper_bound (abfd);
11601 if (symsize < 0)
11602 return FALSE;
11603
11604 /* Read in the symbol table. */
11605 sympp = (asymbol **) xmalloc (symsize);
11606 symcount = bfd_canonicalize_symtab (abfd, sympp);
11607 if (symcount < 0)
11608 goto free_sym_buf;
11609
11610 /* Allow the BFD backend to copy any private header data it
11611 understands from the output BFD to the import library BFD. */
11612 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11613 goto free_sym_buf;
11614
11615 /* Filter symbols to appear in the import library. */
11616 if (bed->elf_backend_filter_implib_symbols)
11617 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11618 symcount);
11619 else
11620 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11621 if (symcount == 0)
11622 {
5df1bc57 11623 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11624 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11625 implib_bfd);
76359541
TP
11626 goto free_sym_buf;
11627 }
11628
11629
11630 /* Make symbols absolute. */
11631 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11632 sizeof (*osymbuf));
11633 for (src_count = 0; src_count < symcount; src_count++)
11634 {
11635 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11636 sizeof (*osymbuf));
11637 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11638 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11639 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11640 osymbuf[src_count].internal_elf_sym.st_value =
11641 osymbuf[src_count].symbol.value;
11642 sympp[src_count] = &osymbuf[src_count].symbol;
11643 }
11644
11645 bfd_set_symtab (implib_bfd, sympp, symcount);
11646
11647 /* Allow the BFD backend to copy any private data it understands
11648 from the output BFD to the import library BFD. This is done last
11649 to permit the routine to look at the filtered symbol table. */
11650 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11651 goto free_sym_buf;
11652
11653 if (!bfd_close (implib_bfd))
11654 goto free_sym_buf;
11655
11656 ret = TRUE;
11657
11658free_sym_buf:
11659 free (sympp);
11660 return ret;
11661}
11662
9f7c3e5e
AM
11663static void
11664elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11665{
11666 asection *o;
11667
11668 if (flinfo->symstrtab != NULL)
ef10c3ac 11669 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11670 if (flinfo->contents != NULL)
11671 free (flinfo->contents);
11672 if (flinfo->external_relocs != NULL)
11673 free (flinfo->external_relocs);
11674 if (flinfo->internal_relocs != NULL)
11675 free (flinfo->internal_relocs);
11676 if (flinfo->external_syms != NULL)
11677 free (flinfo->external_syms);
11678 if (flinfo->locsym_shndx != NULL)
11679 free (flinfo->locsym_shndx);
11680 if (flinfo->internal_syms != NULL)
11681 free (flinfo->internal_syms);
11682 if (flinfo->indices != NULL)
11683 free (flinfo->indices);
11684 if (flinfo->sections != NULL)
11685 free (flinfo->sections);
a0f6fd21
AM
11686 if (flinfo->symshndxbuf != NULL
11687 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
11688 free (flinfo->symshndxbuf);
11689 for (o = obfd->sections; o != NULL; o = o->next)
11690 {
11691 struct bfd_elf_section_data *esdo = elf_section_data (o);
11692 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11693 free (esdo->rel.hashes);
11694 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11695 free (esdo->rela.hashes);
11696 }
11697}
0b52efa6 11698
c152c796
AM
11699/* Do the final step of an ELF link. */
11700
11701bfd_boolean
11702bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11703{
11704 bfd_boolean dynamic;
11705 bfd_boolean emit_relocs;
11706 bfd *dynobj;
8b127cbc 11707 struct elf_final_link_info flinfo;
91d6fa6a
NC
11708 asection *o;
11709 struct bfd_link_order *p;
11710 bfd *sub;
c152c796
AM
11711 bfd_size_type max_contents_size;
11712 bfd_size_type max_external_reloc_size;
11713 bfd_size_type max_internal_reloc_count;
11714 bfd_size_type max_sym_count;
11715 bfd_size_type max_sym_shndx_count;
c152c796
AM
11716 Elf_Internal_Sym elfsym;
11717 unsigned int i;
11718 Elf_Internal_Shdr *symtab_hdr;
11719 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11720 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11721 struct elf_outext_info eoinfo;
11722 bfd_boolean merged;
11723 size_t relativecount = 0;
11724 asection *reldyn = 0;
11725 bfd_size_type amt;
104d59d1
JM
11726 asection *attr_section = NULL;
11727 bfd_vma attr_size = 0;
11728 const char *std_attrs_section;
64f52338 11729 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11730
64f52338 11731 if (!is_elf_hash_table (htab))
c152c796
AM
11732 return FALSE;
11733
0e1862bb 11734 if (bfd_link_pic (info))
c152c796
AM
11735 abfd->flags |= DYNAMIC;
11736
64f52338
AM
11737 dynamic = htab->dynamic_sections_created;
11738 dynobj = htab->dynobj;
c152c796 11739
0e1862bb 11740 emit_relocs = (bfd_link_relocatable (info)
a4676736 11741 || info->emitrelocations);
c152c796 11742
8b127cbc
AM
11743 flinfo.info = info;
11744 flinfo.output_bfd = abfd;
ef10c3ac 11745 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11746 if (flinfo.symstrtab == NULL)
c152c796
AM
11747 return FALSE;
11748
11749 if (! dynamic)
11750 {
8b127cbc
AM
11751 flinfo.hash_sec = NULL;
11752 flinfo.symver_sec = NULL;
c152c796
AM
11753 }
11754 else
11755 {
3d4d4302 11756 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11757 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11758 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11759 /* Note that it is OK if symver_sec is NULL. */
11760 }
11761
8b127cbc
AM
11762 flinfo.contents = NULL;
11763 flinfo.external_relocs = NULL;
11764 flinfo.internal_relocs = NULL;
11765 flinfo.external_syms = NULL;
11766 flinfo.locsym_shndx = NULL;
11767 flinfo.internal_syms = NULL;
11768 flinfo.indices = NULL;
11769 flinfo.sections = NULL;
8b127cbc 11770 flinfo.symshndxbuf = NULL;
ffbc01cc 11771 flinfo.filesym_count = 0;
c152c796 11772
104d59d1
JM
11773 /* The object attributes have been merged. Remove the input
11774 sections from the link, and set the contents of the output
11775 secton. */
11776 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11777 for (o = abfd->sections; o != NULL; o = o->next)
11778 {
5270eddc 11779 bfd_boolean remove_section = FALSE;
b8a6ced7 11780
104d59d1
JM
11781 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11782 || strcmp (o->name, ".gnu.attributes") == 0)
11783 {
11784 for (p = o->map_head.link_order; p != NULL; p = p->next)
11785 {
11786 asection *input_section;
11787
11788 if (p->type != bfd_indirect_link_order)
11789 continue;
11790 input_section = p->u.indirect.section;
11791 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11792 elf_link_input_bfd ignores this section. */
11793 input_section->flags &= ~SEC_HAS_CONTENTS;
11794 }
a0c8462f 11795
104d59d1 11796 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11797 bfd_set_section_size (abfd, o, attr_size);
11798 /* Skip this section later on. */
11799 o->map_head.link_order = NULL;
104d59d1 11800 if (attr_size)
b8a6ced7 11801 attr_section = o;
104d59d1 11802 else
5270eddc 11803 remove_section = TRUE;
104d59d1 11804 }
6e5e9d58
AM
11805 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11806 {
11807 /* Remove empty group section from linker output. */
5270eddc 11808 remove_section = TRUE;
b8a6ced7 11809 }
5270eddc 11810 if (remove_section)
b8a6ced7 11811 {
6e5e9d58
AM
11812 o->flags |= SEC_EXCLUDE;
11813 bfd_section_list_remove (abfd, o);
11814 abfd->section_count--;
11815 }
104d59d1
JM
11816 }
11817
c152c796
AM
11818 /* Count up the number of relocations we will output for each output
11819 section, so that we know the sizes of the reloc sections. We
11820 also figure out some maximum sizes. */
11821 max_contents_size = 0;
11822 max_external_reloc_size = 0;
11823 max_internal_reloc_count = 0;
11824 max_sym_count = 0;
11825 max_sym_shndx_count = 0;
11826 merged = FALSE;
11827 for (o = abfd->sections; o != NULL; o = o->next)
11828 {
11829 struct bfd_elf_section_data *esdo = elf_section_data (o);
11830 o->reloc_count = 0;
11831
8423293d 11832 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11833 {
11834 unsigned int reloc_count = 0;
9eaff861 11835 unsigned int additional_reloc_count = 0;
c152c796 11836 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11837
11838 if (p->type == bfd_section_reloc_link_order
11839 || p->type == bfd_symbol_reloc_link_order)
11840 reloc_count = 1;
11841 else if (p->type == bfd_indirect_link_order)
11842 {
11843 asection *sec;
11844
11845 sec = p->u.indirect.section;
c152c796
AM
11846
11847 /* Mark all sections which are to be included in the
11848 link. This will normally be every section. We need
11849 to do this so that we can identify any sections which
11850 the linker has decided to not include. */
11851 sec->linker_mark = TRUE;
11852
11853 if (sec->flags & SEC_MERGE)
11854 merged = TRUE;
11855
eea6121a
AM
11856 if (sec->rawsize > max_contents_size)
11857 max_contents_size = sec->rawsize;
11858 if (sec->size > max_contents_size)
11859 max_contents_size = sec->size;
c152c796 11860
c152c796
AM
11861 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11862 && (sec->owner->flags & DYNAMIC) == 0)
11863 {
11864 size_t sym_count;
11865
a961cdd5
AM
11866 /* We are interested in just local symbols, not all
11867 symbols. */
c152c796
AM
11868 if (elf_bad_symtab (sec->owner))
11869 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11870 / bed->s->sizeof_sym);
11871 else
11872 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11873
11874 if (sym_count > max_sym_count)
11875 max_sym_count = sym_count;
11876
11877 if (sym_count > max_sym_shndx_count
6a40cf0c 11878 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11879 max_sym_shndx_count = sym_count;
11880
a961cdd5
AM
11881 if (esdo->this_hdr.sh_type == SHT_REL
11882 || esdo->this_hdr.sh_type == SHT_RELA)
11883 /* Some backends use reloc_count in relocation sections
11884 to count particular types of relocs. Of course,
11885 reloc sections themselves can't have relocations. */
11886 ;
11887 else if (emit_relocs)
11888 {
11889 reloc_count = sec->reloc_count;
11890 if (bed->elf_backend_count_additional_relocs)
11891 {
11892 int c;
11893 c = (*bed->elf_backend_count_additional_relocs) (sec);
11894 additional_reloc_count += c;
11895 }
11896 }
11897 else if (bed->elf_backend_count_relocs)
11898 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11899
11900 esdi = elf_section_data (sec);
11901
c152c796
AM
11902 if ((sec->flags & SEC_RELOC) != 0)
11903 {
d4730f92 11904 size_t ext_size = 0;
c152c796 11905
d4730f92
BS
11906 if (esdi->rel.hdr != NULL)
11907 ext_size = esdi->rel.hdr->sh_size;
11908 if (esdi->rela.hdr != NULL)
11909 ext_size += esdi->rela.hdr->sh_size;
7326c758 11910
c152c796
AM
11911 if (ext_size > max_external_reloc_size)
11912 max_external_reloc_size = ext_size;
11913 if (sec->reloc_count > max_internal_reloc_count)
11914 max_internal_reloc_count = sec->reloc_count;
11915 }
11916 }
11917 }
11918
11919 if (reloc_count == 0)
11920 continue;
11921
9eaff861 11922 reloc_count += additional_reloc_count;
c152c796
AM
11923 o->reloc_count += reloc_count;
11924
0e1862bb 11925 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11926 {
d4730f92 11927 if (esdi->rel.hdr)
9eaff861 11928 {
491d01d3 11929 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11930 esdo->rel.count += additional_reloc_count;
11931 }
d4730f92 11932 if (esdi->rela.hdr)
9eaff861 11933 {
491d01d3 11934 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11935 esdo->rela.count += additional_reloc_count;
11936 }
d4730f92
BS
11937 }
11938 else
11939 {
11940 if (o->use_rela_p)
11941 esdo->rela.count += reloc_count;
2c2b4ed4 11942 else
d4730f92 11943 esdo->rel.count += reloc_count;
c152c796 11944 }
c152c796
AM
11945 }
11946
9eaff861 11947 if (o->reloc_count > 0)
c152c796
AM
11948 o->flags |= SEC_RELOC;
11949 else
11950 {
11951 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11952 set it (this is probably a bug) and if it is set
11953 assign_section_numbers will create a reloc section. */
11954 o->flags &=~ SEC_RELOC;
11955 }
11956
11957 /* If the SEC_ALLOC flag is not set, force the section VMA to
11958 zero. This is done in elf_fake_sections as well, but forcing
11959 the VMA to 0 here will ensure that relocs against these
11960 sections are handled correctly. */
11961 if ((o->flags & SEC_ALLOC) == 0
11962 && ! o->user_set_vma)
11963 o->vma = 0;
11964 }
11965
0e1862bb 11966 if (! bfd_link_relocatable (info) && merged)
64f52338 11967 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11968
11969 /* Figure out the file positions for everything but the symbol table
11970 and the relocs. We set symcount to force assign_section_numbers
11971 to create a symbol table. */
8539e4e8 11972 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11973 BFD_ASSERT (! abfd->output_has_begun);
11974 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11975 goto error_return;
11976
ee75fd95 11977 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11978 for (o = abfd->sections; o != NULL; o = o->next)
11979 {
d4730f92 11980 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11981 if ((o->flags & SEC_RELOC) != 0)
11982 {
d4730f92 11983 if (esdo->rel.hdr
9eaff861 11984 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11985 goto error_return;
11986
d4730f92 11987 if (esdo->rela.hdr
9eaff861 11988 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11989 goto error_return;
11990 }
11991
11992 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11993 to count upwards while actually outputting the relocations. */
d4730f92
BS
11994 esdo->rel.count = 0;
11995 esdo->rela.count = 0;
0ce398f1
L
11996
11997 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11998 {
11999 /* Cache the section contents so that they can be compressed
12000 later. Use bfd_malloc since it will be freed by
12001 bfd_compress_section_contents. */
12002 unsigned char *contents = esdo->this_hdr.contents;
12003 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12004 abort ();
12005 contents
12006 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12007 if (contents == NULL)
12008 goto error_return;
12009 esdo->this_hdr.contents = contents;
12010 }
c152c796
AM
12011 }
12012
c152c796 12013 /* We have now assigned file positions for all the sections except
a485e98e
AM
12014 .symtab, .strtab, and non-loaded reloc sections. We start the
12015 .symtab section at the current file position, and write directly
12016 to it. We build the .strtab section in memory. */
c152c796
AM
12017 bfd_get_symcount (abfd) = 0;
12018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12019 /* sh_name is set in prep_headers. */
12020 symtab_hdr->sh_type = SHT_SYMTAB;
12021 /* sh_flags, sh_addr and sh_size all start off zero. */
12022 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12023 /* sh_link is set in assign_section_numbers. */
12024 /* sh_info is set below. */
12025 /* sh_offset is set just below. */
72de5009 12026 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12027
ef10c3ac
L
12028 if (max_sym_count < 20)
12029 max_sym_count = 20;
64f52338 12030 htab->strtabsize = max_sym_count;
ef10c3ac 12031 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12032 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12033 if (htab->strtab == NULL)
c152c796 12034 goto error_return;
ef10c3ac
L
12035 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12036 flinfo.symshndxbuf
12037 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12038 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12039
8539e4e8 12040 if (info->strip != strip_all || emit_relocs)
c152c796 12041 {
8539e4e8
AM
12042 file_ptr off = elf_next_file_pos (abfd);
12043
12044 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12045
12046 /* Note that at this point elf_next_file_pos (abfd) is
12047 incorrect. We do not yet know the size of the .symtab section.
12048 We correct next_file_pos below, after we do know the size. */
12049
12050 /* Start writing out the symbol table. The first symbol is always a
12051 dummy symbol. */
c152c796
AM
12052 elfsym.st_value = 0;
12053 elfsym.st_size = 0;
12054 elfsym.st_info = 0;
12055 elfsym.st_other = 0;
12056 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12057 elfsym.st_target_internal = 0;
ef10c3ac
L
12058 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12059 bfd_und_section_ptr, NULL) != 1)
c152c796 12060 goto error_return;
c152c796 12061
8539e4e8
AM
12062 /* Output a symbol for each section. We output these even if we are
12063 discarding local symbols, since they are used for relocs. These
12064 symbols have no names. We store the index of each one in the
12065 index field of the section, so that we can find it again when
12066 outputting relocs. */
12067
c152c796
AM
12068 elfsym.st_size = 0;
12069 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12070 elfsym.st_other = 0;
f0b5bb34 12071 elfsym.st_value = 0;
35fc36a8 12072 elfsym.st_target_internal = 0;
c152c796
AM
12073 for (i = 1; i < elf_numsections (abfd); i++)
12074 {
12075 o = bfd_section_from_elf_index (abfd, i);
12076 if (o != NULL)
f0b5bb34
AM
12077 {
12078 o->target_index = bfd_get_symcount (abfd);
12079 elfsym.st_shndx = i;
0e1862bb 12080 if (!bfd_link_relocatable (info))
f0b5bb34 12081 elfsym.st_value = o->vma;
ef10c3ac
L
12082 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12083 NULL) != 1)
f0b5bb34
AM
12084 goto error_return;
12085 }
c152c796
AM
12086 }
12087 }
12088
12089 /* Allocate some memory to hold information read in from the input
12090 files. */
12091 if (max_contents_size != 0)
12092 {
8b127cbc
AM
12093 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12094 if (flinfo.contents == NULL)
c152c796
AM
12095 goto error_return;
12096 }
12097
12098 if (max_external_reloc_size != 0)
12099 {
8b127cbc
AM
12100 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12101 if (flinfo.external_relocs == NULL)
c152c796
AM
12102 goto error_return;
12103 }
12104
12105 if (max_internal_reloc_count != 0)
12106 {
056bafd4 12107 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12108 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12109 if (flinfo.internal_relocs == NULL)
c152c796
AM
12110 goto error_return;
12111 }
12112
12113 if (max_sym_count != 0)
12114 {
12115 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12116 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12117 if (flinfo.external_syms == NULL)
c152c796
AM
12118 goto error_return;
12119
12120 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12121 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12122 if (flinfo.internal_syms == NULL)
c152c796
AM
12123 goto error_return;
12124
12125 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12126 flinfo.indices = (long int *) bfd_malloc (amt);
12127 if (flinfo.indices == NULL)
c152c796
AM
12128 goto error_return;
12129
12130 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12131 flinfo.sections = (asection **) bfd_malloc (amt);
12132 if (flinfo.sections == NULL)
c152c796
AM
12133 goto error_return;
12134 }
12135
12136 if (max_sym_shndx_count != 0)
12137 {
12138 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12139 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12140 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12141 goto error_return;
12142 }
12143
64f52338 12144 if (htab->tls_sec)
c152c796
AM
12145 {
12146 bfd_vma base, end = 0;
12147 asection *sec;
12148
64f52338 12149 for (sec = htab->tls_sec;
c152c796
AM
12150 sec && (sec->flags & SEC_THREAD_LOCAL);
12151 sec = sec->next)
12152 {
3a800eb9 12153 bfd_size_type size = sec->size;
c152c796 12154
3a800eb9
AM
12155 if (size == 0
12156 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12157 {
91d6fa6a
NC
12158 struct bfd_link_order *ord = sec->map_tail.link_order;
12159
12160 if (ord != NULL)
12161 size = ord->offset + ord->size;
c152c796
AM
12162 }
12163 end = sec->vma + size;
12164 }
64f52338 12165 base = htab->tls_sec->vma;
7dc98aea
RO
12166 /* Only align end of TLS section if static TLS doesn't have special
12167 alignment requirements. */
12168 if (bed->static_tls_alignment == 1)
64f52338
AM
12169 end = align_power (end, htab->tls_sec->alignment_power);
12170 htab->tls_size = end - base;
c152c796
AM
12171 }
12172
0b52efa6
PB
12173 /* Reorder SHF_LINK_ORDER sections. */
12174 for (o = abfd->sections; o != NULL; o = o->next)
12175 {
12176 if (!elf_fixup_link_order (abfd, o))
12177 return FALSE;
12178 }
12179
2f0c68f2
CM
12180 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12181 return FALSE;
12182
c152c796
AM
12183 /* Since ELF permits relocations to be against local symbols, we
12184 must have the local symbols available when we do the relocations.
12185 Since we would rather only read the local symbols once, and we
12186 would rather not keep them in memory, we handle all the
12187 relocations for a single input file at the same time.
12188
12189 Unfortunately, there is no way to know the total number of local
12190 symbols until we have seen all of them, and the local symbol
12191 indices precede the global symbol indices. This means that when
12192 we are generating relocatable output, and we see a reloc against
12193 a global symbol, we can not know the symbol index until we have
12194 finished examining all the local symbols to see which ones we are
12195 going to output. To deal with this, we keep the relocations in
12196 memory, and don't output them until the end of the link. This is
12197 an unfortunate waste of memory, but I don't see a good way around
12198 it. Fortunately, it only happens when performing a relocatable
12199 link, which is not the common case. FIXME: If keep_memory is set
12200 we could write the relocs out and then read them again; I don't
12201 know how bad the memory loss will be. */
12202
c72f2fb2 12203 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12204 sub->output_has_begun = FALSE;
12205 for (o = abfd->sections; o != NULL; o = o->next)
12206 {
8423293d 12207 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12208 {
12209 if (p->type == bfd_indirect_link_order
12210 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12211 == bfd_target_elf_flavour)
12212 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12213 {
12214 if (! sub->output_has_begun)
12215 {
8b127cbc 12216 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12217 goto error_return;
12218 sub->output_has_begun = TRUE;
12219 }
12220 }
12221 else if (p->type == bfd_section_reloc_link_order
12222 || p->type == bfd_symbol_reloc_link_order)
12223 {
12224 if (! elf_reloc_link_order (abfd, info, o, p))
12225 goto error_return;
12226 }
12227 else
12228 {
12229 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12230 {
12231 if (p->type == bfd_indirect_link_order
12232 && (bfd_get_flavour (sub)
12233 == bfd_target_elf_flavour)
12234 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12235 != bed->s->elfclass))
12236 {
12237 const char *iclass, *oclass;
12238
aebf9be7 12239 switch (bed->s->elfclass)
351f65ca 12240 {
aebf9be7
NC
12241 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12242 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12243 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12244 default: abort ();
351f65ca 12245 }
aebf9be7
NC
12246
12247 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12248 {
aebf9be7
NC
12249 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12250 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12251 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12252 default: abort ();
351f65ca
L
12253 }
12254
12255 bfd_set_error (bfd_error_wrong_format);
4eca0228 12256 _bfd_error_handler
695344c0 12257 /* xgettext:c-format */
871b3ab2 12258 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12259 sub, iclass, oclass);
12260 }
12261
12262 goto error_return;
12263 }
c152c796
AM
12264 }
12265 }
12266 }
12267
c0f00686
L
12268 /* Free symbol buffer if needed. */
12269 if (!info->reduce_memory_overheads)
12270 {
c72f2fb2 12271 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12272 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12273 && elf_tdata (sub)->symbuf)
c0f00686
L
12274 {
12275 free (elf_tdata (sub)->symbuf);
12276 elf_tdata (sub)->symbuf = NULL;
12277 }
12278 }
12279
c152c796
AM
12280 /* Output any global symbols that got converted to local in a
12281 version script or due to symbol visibility. We do this in a
12282 separate step since ELF requires all local symbols to appear
12283 prior to any global symbols. FIXME: We should only do this if
12284 some global symbols were, in fact, converted to become local.
12285 FIXME: Will this work correctly with the Irix 5 linker? */
12286 eoinfo.failed = FALSE;
8b127cbc 12287 eoinfo.flinfo = &flinfo;
c152c796 12288 eoinfo.localsyms = TRUE;
34a79995 12289 eoinfo.file_sym_done = FALSE;
7686d77d 12290 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12291 if (eoinfo.failed)
12292 return FALSE;
12293
4e617b1e
PB
12294 /* If backend needs to output some local symbols not present in the hash
12295 table, do it now. */
8539e4e8
AM
12296 if (bed->elf_backend_output_arch_local_syms
12297 && (info->strip != strip_all || emit_relocs))
4e617b1e 12298 {
6e0b88f1 12299 typedef int (*out_sym_func)
4e617b1e
PB
12300 (void *, const char *, Elf_Internal_Sym *, asection *,
12301 struct elf_link_hash_entry *);
12302
12303 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12304 (abfd, info, &flinfo,
12305 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12306 return FALSE;
12307 }
12308
c152c796
AM
12309 /* That wrote out all the local symbols. Finish up the symbol table
12310 with the global symbols. Even if we want to strip everything we
12311 can, we still need to deal with those global symbols that got
12312 converted to local in a version script. */
12313
12314 /* The sh_info field records the index of the first non local symbol. */
12315 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12316
12317 if (dynamic
64f52338
AM
12318 && htab->dynsym != NULL
12319 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12320 {
12321 Elf_Internal_Sym sym;
64f52338 12322 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12323
64f52338
AM
12324 o = htab->dynsym->output_section;
12325 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12326
12327 /* Write out the section symbols for the output sections. */
0e1862bb 12328 if (bfd_link_pic (info)
64f52338 12329 || htab->is_relocatable_executable)
c152c796
AM
12330 {
12331 asection *s;
12332
12333 sym.st_size = 0;
12334 sym.st_name = 0;
12335 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12336 sym.st_other = 0;
35fc36a8 12337 sym.st_target_internal = 0;
c152c796
AM
12338
12339 for (s = abfd->sections; s != NULL; s = s->next)
12340 {
12341 int indx;
12342 bfd_byte *dest;
12343 long dynindx;
12344
c152c796 12345 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12346 if (dynindx <= 0)
12347 continue;
12348 indx = elf_section_data (s)->this_idx;
c152c796
AM
12349 BFD_ASSERT (indx > 0);
12350 sym.st_shndx = indx;
c0d5a53d
L
12351 if (! check_dynsym (abfd, &sym))
12352 return FALSE;
c152c796
AM
12353 sym.st_value = s->vma;
12354 dest = dynsym + dynindx * bed->s->sizeof_sym;
12355 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12356 }
c152c796
AM
12357 }
12358
12359 /* Write out the local dynsyms. */
64f52338 12360 if (htab->dynlocal)
c152c796
AM
12361 {
12362 struct elf_link_local_dynamic_entry *e;
64f52338 12363 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12364 {
12365 asection *s;
12366 bfd_byte *dest;
12367
935bd1e0 12368 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12369 Note that we saved a word of storage and overwrote
12370 the original st_name with the dynstr_index. */
12371 sym = e->isym;
935bd1e0 12372 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12373
cb33740c
AM
12374 s = bfd_section_from_elf_index (e->input_bfd,
12375 e->isym.st_shndx);
12376 if (s != NULL)
c152c796 12377 {
c152c796
AM
12378 sym.st_shndx =
12379 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12380 if (! check_dynsym (abfd, &sym))
12381 return FALSE;
c152c796
AM
12382 sym.st_value = (s->output_section->vma
12383 + s->output_offset
12384 + e->isym.st_value);
12385 }
12386
c152c796
AM
12387 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12388 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12389 }
12390 }
c152c796
AM
12391 }
12392
12393 /* We get the global symbols from the hash table. */
12394 eoinfo.failed = FALSE;
12395 eoinfo.localsyms = FALSE;
8b127cbc 12396 eoinfo.flinfo = &flinfo;
7686d77d 12397 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12398 if (eoinfo.failed)
12399 return FALSE;
12400
12401 /* If backend needs to output some symbols not present in the hash
12402 table, do it now. */
8539e4e8
AM
12403 if (bed->elf_backend_output_arch_syms
12404 && (info->strip != strip_all || emit_relocs))
c152c796 12405 {
6e0b88f1 12406 typedef int (*out_sym_func)
c152c796
AM
12407 (void *, const char *, Elf_Internal_Sym *, asection *,
12408 struct elf_link_hash_entry *);
12409
12410 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12411 (abfd, info, &flinfo,
12412 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12413 return FALSE;
12414 }
12415
ef10c3ac
L
12416 /* Finalize the .strtab section. */
12417 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12418
12419 /* Swap out the .strtab section. */
12420 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12421 return FALSE;
12422
12423 /* Now we know the size of the symtab section. */
c152c796
AM
12424 if (bfd_get_symcount (abfd) > 0)
12425 {
ee3b52e9
L
12426 /* Finish up and write out the symbol string table (.strtab)
12427 section. */
ad32986f 12428 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12429 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12430
ad32986f 12431 if (elf_symtab_shndx_list (abfd))
8539e4e8 12432 {
ad32986f 12433 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12434
ad32986f
NC
12435 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12436 {
12437 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12438 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12439 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12440 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12441 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12442
ad32986f
NC
12443 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12444 off, TRUE);
12445
12446 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12447 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12448 return FALSE;
12449 }
8539e4e8 12450 }
ee3b52e9
L
12451
12452 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12453 /* sh_name was set in prep_headers. */
12454 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12455 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12456 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12457 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12458 symstrtab_hdr->sh_entsize = 0;
12459 symstrtab_hdr->sh_link = 0;
12460 symstrtab_hdr->sh_info = 0;
12461 /* sh_offset is set just below. */
12462 symstrtab_hdr->sh_addralign = 1;
12463
12464 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12465 off, TRUE);
12466 elf_next_file_pos (abfd) = off;
12467
c152c796 12468 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12469 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12470 return FALSE;
12471 }
12472
76359541
TP
12473 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12474 {
871b3ab2 12475 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12476 info->out_implib_bfd);
76359541
TP
12477 return FALSE;
12478 }
12479
c152c796
AM
12480 /* Adjust the relocs to have the correct symbol indices. */
12481 for (o = abfd->sections; o != NULL; o = o->next)
12482 {
d4730f92 12483 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12484 bfd_boolean sort;
10bbbc1d 12485
c152c796
AM
12486 if ((o->flags & SEC_RELOC) == 0)
12487 continue;
12488
28dbcedc 12489 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12490 if (esdo->rel.hdr != NULL
10bbbc1d 12491 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12492 return FALSE;
12493 if (esdo->rela.hdr != NULL
10bbbc1d 12494 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12495 return FALSE;
c152c796
AM
12496
12497 /* Set the reloc_count field to 0 to prevent write_relocs from
12498 trying to swap the relocs out itself. */
12499 o->reloc_count = 0;
12500 }
12501
12502 if (dynamic && info->combreloc && dynobj != NULL)
12503 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12504
12505 /* If we are linking against a dynamic object, or generating a
12506 shared library, finish up the dynamic linking information. */
12507 if (dynamic)
12508 {
12509 bfd_byte *dyncon, *dynconend;
12510
12511 /* Fix up .dynamic entries. */
3d4d4302 12512 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12513 BFD_ASSERT (o != NULL);
12514
12515 dyncon = o->contents;
eea6121a 12516 dynconend = o->contents + o->size;
c152c796
AM
12517 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12518 {
12519 Elf_Internal_Dyn dyn;
12520 const char *name;
12521 unsigned int type;
64487780
AM
12522 bfd_size_type sh_size;
12523 bfd_vma sh_addr;
c152c796
AM
12524
12525 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12526
12527 switch (dyn.d_tag)
12528 {
12529 default:
12530 continue;
12531 case DT_NULL:
12532 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12533 {
12534 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12535 {
12536 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12537 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12538 default: continue;
12539 }
12540 dyn.d_un.d_val = relativecount;
12541 relativecount = 0;
12542 break;
12543 }
12544 continue;
12545
12546 case DT_INIT:
12547 name = info->init_function;
12548 goto get_sym;
12549 case DT_FINI:
12550 name = info->fini_function;
12551 get_sym:
12552 {
12553 struct elf_link_hash_entry *h;
12554
64f52338 12555 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12556 if (h != NULL
12557 && (h->root.type == bfd_link_hash_defined
12558 || h->root.type == bfd_link_hash_defweak))
12559 {
bef26483 12560 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12561 o = h->root.u.def.section;
12562 if (o->output_section != NULL)
bef26483 12563 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12564 + o->output_offset);
12565 else
12566 {
12567 /* The symbol is imported from another shared
12568 library and does not apply to this one. */
bef26483 12569 dyn.d_un.d_ptr = 0;
c152c796
AM
12570 }
12571 break;
12572 }
12573 }
12574 continue;
12575
12576 case DT_PREINIT_ARRAYSZ:
12577 name = ".preinit_array";
4ade44b7 12578 goto get_out_size;
c152c796
AM
12579 case DT_INIT_ARRAYSZ:
12580 name = ".init_array";
4ade44b7 12581 goto get_out_size;
c152c796
AM
12582 case DT_FINI_ARRAYSZ:
12583 name = ".fini_array";
4ade44b7 12584 get_out_size:
c152c796
AM
12585 o = bfd_get_section_by_name (abfd, name);
12586 if (o == NULL)
12587 {
4eca0228 12588 _bfd_error_handler
4ade44b7 12589 (_("could not find section %s"), name);
c152c796
AM
12590 goto error_return;
12591 }
eea6121a 12592 if (o->size == 0)
4eca0228 12593 _bfd_error_handler
c152c796 12594 (_("warning: %s section has zero size"), name);
eea6121a 12595 dyn.d_un.d_val = o->size;
c152c796
AM
12596 break;
12597
12598 case DT_PREINIT_ARRAY:
12599 name = ".preinit_array";
4ade44b7 12600 goto get_out_vma;
c152c796
AM
12601 case DT_INIT_ARRAY:
12602 name = ".init_array";
4ade44b7 12603 goto get_out_vma;
c152c796
AM
12604 case DT_FINI_ARRAY:
12605 name = ".fini_array";
4ade44b7
AM
12606 get_out_vma:
12607 o = bfd_get_section_by_name (abfd, name);
12608 goto do_vma;
c152c796
AM
12609
12610 case DT_HASH:
12611 name = ".hash";
12612 goto get_vma;
fdc90cb4
JJ
12613 case DT_GNU_HASH:
12614 name = ".gnu.hash";
12615 goto get_vma;
c152c796
AM
12616 case DT_STRTAB:
12617 name = ".dynstr";
12618 goto get_vma;
12619 case DT_SYMTAB:
12620 name = ".dynsym";
12621 goto get_vma;
12622 case DT_VERDEF:
12623 name = ".gnu.version_d";
12624 goto get_vma;
12625 case DT_VERNEED:
12626 name = ".gnu.version_r";
12627 goto get_vma;
12628 case DT_VERSYM:
12629 name = ".gnu.version";
12630 get_vma:
4ade44b7
AM
12631 o = bfd_get_linker_section (dynobj, name);
12632 do_vma:
b3293efa 12633 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12634 {
4eca0228 12635 _bfd_error_handler
4ade44b7 12636 (_("could not find section %s"), name);
c152c796
AM
12637 goto error_return;
12638 }
894891db
NC
12639 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12640 {
4eca0228 12641 _bfd_error_handler
894891db
NC
12642 (_("warning: section '%s' is being made into a note"), name);
12643 bfd_set_error (bfd_error_nonrepresentable_section);
12644 goto error_return;
12645 }
4ade44b7 12646 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12647 break;
12648
12649 case DT_REL:
12650 case DT_RELA:
12651 case DT_RELSZ:
12652 case DT_RELASZ:
12653 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12654 type = SHT_REL;
12655 else
12656 type = SHT_RELA;
64487780
AM
12657 sh_size = 0;
12658 sh_addr = 0;
c152c796
AM
12659 for (i = 1; i < elf_numsections (abfd); i++)
12660 {
12661 Elf_Internal_Shdr *hdr;
12662
12663 hdr = elf_elfsections (abfd)[i];
12664 if (hdr->sh_type == type
12665 && (hdr->sh_flags & SHF_ALLOC) != 0)
12666 {
64487780
AM
12667 sh_size += hdr->sh_size;
12668 if (sh_addr == 0
12669 || sh_addr > hdr->sh_addr)
12670 sh_addr = hdr->sh_addr;
c152c796
AM
12671 }
12672 }
64487780 12673
64f52338
AM
12674 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12675 {
12676 /* Don't count procedure linkage table relocs in the
12677 overall reloc count. */
64487780
AM
12678 sh_size -= htab->srelplt->size;
12679 if (sh_size == 0)
12680 /* If the size is zero, make the address zero too.
12681 This is to avoid a glibc bug. If the backend
12682 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12683 zero, then we'll put DT_RELA at the end of
12684 DT_JMPREL. glibc will interpret the end of
12685 DT_RELA matching the end of DT_JMPREL as the
12686 case where DT_RELA includes DT_JMPREL, and for
12687 LD_BIND_NOW will decide that processing DT_RELA
12688 will process the PLT relocs too. Net result:
12689 No PLT relocs applied. */
12690 sh_addr = 0;
12691
64f52338
AM
12692 /* If .rela.plt is the first .rela section, exclude
12693 it from DT_RELA. */
64487780
AM
12694 else if (sh_addr == (htab->srelplt->output_section->vma
12695 + htab->srelplt->output_offset))
12696 sh_addr += htab->srelplt->size;
64f52338 12697 }
64487780
AM
12698
12699 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12700 dyn.d_un.d_val = sh_size;
12701 else
12702 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12703 break;
12704 }
12705 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12706 }
12707 }
12708
12709 /* If we have created any dynamic sections, then output them. */
12710 if (dynobj != NULL)
12711 {
12712 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12713 goto error_return;
12714
943284cc 12715 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12716 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12717 || info->error_textrel)
3d4d4302 12718 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12719 {
12720 bfd_byte *dyncon, *dynconend;
12721
943284cc
DJ
12722 dyncon = o->contents;
12723 dynconend = o->contents + o->size;
12724 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12725 {
12726 Elf_Internal_Dyn dyn;
12727
12728 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12729
12730 if (dyn.d_tag == DT_TEXTREL)
12731 {
c192a133
AM
12732 if (info->error_textrel)
12733 info->callbacks->einfo
9793eb77 12734 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12735 else
12736 info->callbacks->einfo
9793eb77 12737 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12738 break;
12739 }
12740 }
12741 }
12742
c152c796
AM
12743 for (o = dynobj->sections; o != NULL; o = o->next)
12744 {
12745 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12746 || o->size == 0
c152c796
AM
12747 || o->output_section == bfd_abs_section_ptr)
12748 continue;
12749 if ((o->flags & SEC_LINKER_CREATED) == 0)
12750 {
12751 /* At this point, we are only interested in sections
12752 created by _bfd_elf_link_create_dynamic_sections. */
12753 continue;
12754 }
64f52338 12755 if (htab->stab_info.stabstr == o)
3722b82f 12756 continue;
64f52338 12757 if (htab->eh_info.hdr_sec == o)
eea6121a 12758 continue;
3d4d4302 12759 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12760 {
12761 if (! bfd_set_section_contents (abfd, o->output_section,
12762 o->contents,
37b01f6a
DG
12763 (file_ptr) o->output_offset
12764 * bfd_octets_per_byte (abfd),
eea6121a 12765 o->size))
c152c796
AM
12766 goto error_return;
12767 }
12768 else
12769 {
12770 /* The contents of the .dynstr section are actually in a
12771 stringtab. */
8539e4e8
AM
12772 file_ptr off;
12773
c152c796
AM
12774 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12775 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12776 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12777 goto error_return;
12778 }
12779 }
12780 }
12781
7bdf4127 12782 if (!info->resolve_section_groups)
c152c796
AM
12783 {
12784 bfd_boolean failed = FALSE;
12785
7bdf4127 12786 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12787 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12788 if (failed)
12789 goto error_return;
12790 }
12791
12792 /* If we have optimized stabs strings, output them. */
64f52338 12793 if (htab->stab_info.stabstr != NULL)
c152c796 12794 {
64f52338 12795 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12796 goto error_return;
12797 }
12798
9f7c3e5e
AM
12799 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12800 goto error_return;
c152c796 12801
9f7c3e5e 12802 elf_final_link_free (abfd, &flinfo);
c152c796 12803
104d59d1
JM
12804 if (attr_section)
12805 {
a50b1753 12806 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12807 if (contents == NULL)
d0f16d5e 12808 return FALSE; /* Bail out and fail. */
104d59d1
JM
12809 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12810 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12811 free (contents);
12812 }
12813
c152c796
AM
12814 return TRUE;
12815
12816 error_return:
9f7c3e5e 12817 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12818 return FALSE;
12819}
12820\f
5241d853
RS
12821/* Initialize COOKIE for input bfd ABFD. */
12822
12823static bfd_boolean
12824init_reloc_cookie (struct elf_reloc_cookie *cookie,
12825 struct bfd_link_info *info, bfd *abfd)
12826{
12827 Elf_Internal_Shdr *symtab_hdr;
12828 const struct elf_backend_data *bed;
12829
12830 bed = get_elf_backend_data (abfd);
12831 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12832
12833 cookie->abfd = abfd;
12834 cookie->sym_hashes = elf_sym_hashes (abfd);
12835 cookie->bad_symtab = elf_bad_symtab (abfd);
12836 if (cookie->bad_symtab)
12837 {
12838 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12839 cookie->extsymoff = 0;
12840 }
12841 else
12842 {
12843 cookie->locsymcount = symtab_hdr->sh_info;
12844 cookie->extsymoff = symtab_hdr->sh_info;
12845 }
12846
12847 if (bed->s->arch_size == 32)
12848 cookie->r_sym_shift = 8;
12849 else
12850 cookie->r_sym_shift = 32;
12851
12852 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12853 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12854 {
12855 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12856 cookie->locsymcount, 0,
12857 NULL, NULL, NULL);
12858 if (cookie->locsyms == NULL)
12859 {
12860 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12861 return FALSE;
12862 }
12863 if (info->keep_memory)
12864 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12865 }
12866 return TRUE;
12867}
12868
12869/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12870
12871static void
12872fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12873{
12874 Elf_Internal_Shdr *symtab_hdr;
12875
12876 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12877 if (cookie->locsyms != NULL
12878 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12879 free (cookie->locsyms);
12880}
12881
12882/* Initialize the relocation information in COOKIE for input section SEC
12883 of input bfd ABFD. */
12884
12885static bfd_boolean
12886init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12887 struct bfd_link_info *info, bfd *abfd,
12888 asection *sec)
12889{
5241d853
RS
12890 if (sec->reloc_count == 0)
12891 {
12892 cookie->rels = NULL;
12893 cookie->relend = NULL;
12894 }
12895 else
12896 {
5241d853
RS
12897 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12898 info->keep_memory);
12899 if (cookie->rels == NULL)
12900 return FALSE;
12901 cookie->rel = cookie->rels;
056bafd4 12902 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12903 }
12904 cookie->rel = cookie->rels;
12905 return TRUE;
12906}
12907
12908/* Free the memory allocated by init_reloc_cookie_rels,
12909 if appropriate. */
12910
12911static void
12912fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12913 asection *sec)
12914{
12915 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12916 free (cookie->rels);
12917}
12918
12919/* Initialize the whole of COOKIE for input section SEC. */
12920
12921static bfd_boolean
12922init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12923 struct bfd_link_info *info,
12924 asection *sec)
12925{
12926 if (!init_reloc_cookie (cookie, info, sec->owner))
12927 goto error1;
12928 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12929 goto error2;
12930 return TRUE;
12931
12932 error2:
12933 fini_reloc_cookie (cookie, sec->owner);
12934 error1:
12935 return FALSE;
12936}
12937
12938/* Free the memory allocated by init_reloc_cookie_for_section,
12939 if appropriate. */
12940
12941static void
12942fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12943 asection *sec)
12944{
12945 fini_reloc_cookie_rels (cookie, sec);
12946 fini_reloc_cookie (cookie, sec->owner);
12947}
12948\f
c152c796
AM
12949/* Garbage collect unused sections. */
12950
07adf181
AM
12951/* Default gc_mark_hook. */
12952
12953asection *
12954_bfd_elf_gc_mark_hook (asection *sec,
12955 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12956 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12957 struct elf_link_hash_entry *h,
12958 Elf_Internal_Sym *sym)
12959{
12960 if (h != NULL)
12961 {
12962 switch (h->root.type)
12963 {
12964 case bfd_link_hash_defined:
12965 case bfd_link_hash_defweak:
12966 return h->root.u.def.section;
12967
12968 case bfd_link_hash_common:
12969 return h->root.u.c.p->section;
12970
12971 default:
12972 break;
12973 }
12974 }
12975 else
12976 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12977
12978 return NULL;
12979}
12980
9e223787 12981/* Return the debug definition section. */
b7c871ed
L
12982
12983static asection *
12984elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12985 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12986 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12987 struct elf_link_hash_entry *h,
9e223787 12988 Elf_Internal_Sym *sym)
b7c871ed 12989{
9e223787
L
12990 if (h != NULL)
12991 {
12992 /* Return the global debug definition section. */
12993 if ((h->root.type == bfd_link_hash_defined
12994 || h->root.type == bfd_link_hash_defweak)
12995 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12996 return h->root.u.def.section;
12997 }
12998 else
12999 {
13000 /* Return the local debug definition section. */
13001 asection *isec = bfd_section_from_elf_index (sec->owner,
13002 sym->st_shndx);
13003 if ((isec->flags & SEC_DEBUGGING) != 0)
13004 return isec;
13005 }
b7c871ed
L
13006
13007 return NULL;
13008}
13009
5241d853
RS
13010/* COOKIE->rel describes a relocation against section SEC, which is
13011 a section we've decided to keep. Return the section that contains
13012 the relocation symbol, or NULL if no section contains it. */
13013
13014asection *
13015_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13016 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13017 struct elf_reloc_cookie *cookie,
13018 bfd_boolean *start_stop)
5241d853
RS
13019{
13020 unsigned long r_symndx;
13021 struct elf_link_hash_entry *h;
13022
13023 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13024 if (r_symndx == STN_UNDEF)
5241d853
RS
13025 return NULL;
13026
13027 if (r_symndx >= cookie->locsymcount
13028 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13029 {
13030 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13031 if (h == NULL)
13032 {
871b3ab2 13033 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13034 sec->owner);
13035 return NULL;
13036 }
5241d853
RS
13037 while (h->root.type == bfd_link_hash_indirect
13038 || h->root.type == bfd_link_hash_warning)
13039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13040 h->mark = 1;
4e6b54a6
AM
13041 /* If this symbol is weak and there is a non-weak definition, we
13042 keep the non-weak definition because many backends put
13043 dynamic reloc info on the non-weak definition for code
13044 handling copy relocs. */
60d67dc8
AM
13045 if (h->is_weakalias)
13046 weakdef (h)->mark = 1;
1cce69b9 13047
a6a4679f 13048 if (start_stop != NULL)
1cce69b9 13049 {
7dba9362
AM
13050 /* To work around a glibc bug, mark XXX input sections
13051 when there is a reference to __start_XXX or __stop_XXX
13052 symbols. */
cbd0eecf 13053 if (h->start_stop)
1cce69b9 13054 {
cbd0eecf 13055 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13056 *start_stop = !s->gc_mark;
13057 return s;
1cce69b9
AM
13058 }
13059 }
13060
5241d853
RS
13061 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13062 }
13063
13064 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13065 &cookie->locsyms[r_symndx]);
13066}
13067
13068/* COOKIE->rel describes a relocation against section SEC, which is
13069 a section we've decided to keep. Mark the section that contains
9d0a14d3 13070 the relocation symbol. */
5241d853
RS
13071
13072bfd_boolean
13073_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13074 asection *sec,
13075 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13076 struct elf_reloc_cookie *cookie)
5241d853
RS
13077{
13078 asection *rsec;
1cce69b9 13079 bfd_boolean start_stop = FALSE;
5241d853 13080
1cce69b9
AM
13081 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13082 while (rsec != NULL)
5241d853 13083 {
1cce69b9
AM
13084 if (!rsec->gc_mark)
13085 {
13086 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13087 || (rsec->owner->flags & DYNAMIC) != 0)
13088 rsec->gc_mark = 1;
13089 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13090 return FALSE;
13091 }
13092 if (!start_stop)
13093 break;
199af150 13094 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13095 }
13096 return TRUE;
13097}
13098
07adf181
AM
13099/* The mark phase of garbage collection. For a given section, mark
13100 it and any sections in this section's group, and all the sections
13101 which define symbols to which it refers. */
13102
ccfa59ea
AM
13103bfd_boolean
13104_bfd_elf_gc_mark (struct bfd_link_info *info,
13105 asection *sec,
6a5bb875 13106 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13107{
13108 bfd_boolean ret;
9d0a14d3 13109 asection *group_sec, *eh_frame;
c152c796
AM
13110
13111 sec->gc_mark = 1;
13112
13113 /* Mark all the sections in the group. */
13114 group_sec = elf_section_data (sec)->next_in_group;
13115 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13116 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13117 return FALSE;
13118
13119 /* Look through the section relocs. */
13120 ret = TRUE;
9d0a14d3
RS
13121 eh_frame = elf_eh_frame_section (sec->owner);
13122 if ((sec->flags & SEC_RELOC) != 0
13123 && sec->reloc_count > 0
13124 && sec != eh_frame)
c152c796 13125 {
5241d853 13126 struct elf_reloc_cookie cookie;
c152c796 13127
5241d853
RS
13128 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13129 ret = FALSE;
c152c796 13130 else
c152c796 13131 {
5241d853 13132 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13133 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13134 {
13135 ret = FALSE;
13136 break;
13137 }
13138 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13139 }
13140 }
9d0a14d3
RS
13141
13142 if (ret && eh_frame && elf_fde_list (sec))
13143 {
13144 struct elf_reloc_cookie cookie;
13145
13146 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13147 ret = FALSE;
13148 else
13149 {
13150 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13151 gc_mark_hook, &cookie))
13152 ret = FALSE;
13153 fini_reloc_cookie_for_section (&cookie, eh_frame);
13154 }
13155 }
13156
2f0c68f2
CM
13157 eh_frame = elf_section_eh_frame_entry (sec);
13158 if (ret && eh_frame && !eh_frame->gc_mark)
13159 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13160 ret = FALSE;
13161
c152c796
AM
13162 return ret;
13163}
13164
3c758495
TG
13165/* Scan and mark sections in a special or debug section group. */
13166
13167static void
13168_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13169{
13170 /* Point to first section of section group. */
13171 asection *ssec;
13172 /* Used to iterate the section group. */
13173 asection *msec;
13174
13175 bfd_boolean is_special_grp = TRUE;
13176 bfd_boolean is_debug_grp = TRUE;
13177
13178 /* First scan to see if group contains any section other than debug
13179 and special section. */
13180 ssec = msec = elf_next_in_group (grp);
13181 do
13182 {
13183 if ((msec->flags & SEC_DEBUGGING) == 0)
13184 is_debug_grp = FALSE;
13185
13186 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13187 is_special_grp = FALSE;
13188
13189 msec = elf_next_in_group (msec);
13190 }
13191 while (msec != ssec);
13192
13193 /* If this is a pure debug section group or pure special section group,
13194 keep all sections in this group. */
13195 if (is_debug_grp || is_special_grp)
13196 {
13197 do
13198 {
13199 msec->gc_mark = 1;
13200 msec = elf_next_in_group (msec);
13201 }
13202 while (msec != ssec);
13203 }
13204}
13205
7f6ab9f8
AM
13206/* Keep debug and special sections. */
13207
13208bfd_boolean
13209_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13210 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13211{
13212 bfd *ibfd;
13213
c72f2fb2 13214 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13215 {
13216 asection *isec;
13217 bfd_boolean some_kept;
b40bf0a2 13218 bfd_boolean debug_frag_seen;
b7c871ed 13219 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13220
13221 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13222 continue;
57963c05
AM
13223 isec = ibfd->sections;
13224 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13225 continue;
7f6ab9f8 13226
b40bf0a2
NC
13227 /* Ensure all linker created sections are kept,
13228 see if any other section is already marked,
13229 and note if we have any fragmented debug sections. */
b7c871ed 13230 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13231 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13232 {
13233 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13234 isec->gc_mark = 1;
eb026f09
AM
13235 else if (isec->gc_mark
13236 && (isec->flags & SEC_ALLOC) != 0
13237 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13238 some_kept = TRUE;
b40bf0a2 13239
535b785f 13240 if (!debug_frag_seen
b40bf0a2
NC
13241 && (isec->flags & SEC_DEBUGGING)
13242 && CONST_STRNEQ (isec->name, ".debug_line."))
13243 debug_frag_seen = TRUE;
7f6ab9f8
AM
13244 }
13245
eb026f09
AM
13246 /* If no non-note alloc section in this file will be kept, then
13247 we can toss out the debug and special sections. */
7f6ab9f8
AM
13248 if (!some_kept)
13249 continue;
13250
13251 /* Keep debug and special sections like .comment when they are
3c758495
TG
13252 not part of a group. Also keep section groups that contain
13253 just debug sections or special sections. */
7f6ab9f8 13254 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13255 {
13256 if ((isec->flags & SEC_GROUP) != 0)
13257 _bfd_elf_gc_mark_debug_special_section_group (isec);
13258 else if (((isec->flags & SEC_DEBUGGING) != 0
13259 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13260 && elf_next_in_group (isec) == NULL)
13261 isec->gc_mark = 1;
b7c871ed
L
13262 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13263 has_kept_debug_info = TRUE;
3c758495 13264 }
b40bf0a2 13265
b40bf0a2
NC
13266 /* Look for CODE sections which are going to be discarded,
13267 and find and discard any fragmented debug sections which
13268 are associated with that code section. */
b7c871ed
L
13269 if (debug_frag_seen)
13270 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13271 if ((isec->flags & SEC_CODE) != 0
13272 && isec->gc_mark == 0)
13273 {
13274 unsigned int ilen;
13275 asection *dsec;
b40bf0a2 13276
b7c871ed 13277 ilen = strlen (isec->name);
b40bf0a2 13278
b7c871ed 13279 /* Association is determined by the name of the debug
07d6d2b8 13280 section containing the name of the code section as
b7c871ed
L
13281 a suffix. For example .debug_line.text.foo is a
13282 debug section associated with .text.foo. */
13283 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13284 {
13285 unsigned int dlen;
b40bf0a2 13286
b7c871ed
L
13287 if (dsec->gc_mark == 0
13288 || (dsec->flags & SEC_DEBUGGING) == 0)
13289 continue;
b40bf0a2 13290
b7c871ed 13291 dlen = strlen (dsec->name);
b40bf0a2 13292
b7c871ed
L
13293 if (dlen > ilen
13294 && strncmp (dsec->name + (dlen - ilen),
13295 isec->name, ilen) == 0)
b40bf0a2 13296 dsec->gc_mark = 0;
b7c871ed 13297 }
b40bf0a2 13298 }
b7c871ed
L
13299
13300 /* Mark debug sections referenced by kept debug sections. */
13301 if (has_kept_debug_info)
13302 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13303 if (isec->gc_mark
13304 && (isec->flags & SEC_DEBUGGING) != 0)
13305 if (!_bfd_elf_gc_mark (info, isec,
13306 elf_gc_mark_debug_section))
13307 return FALSE;
7f6ab9f8
AM
13308 }
13309 return TRUE;
13310}
13311
c152c796 13312static bfd_boolean
ccabcbe5 13313elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13314{
13315 bfd *sub;
ccabcbe5 13316 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13317
c72f2fb2 13318 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13319 {
13320 asection *o;
13321
b19a8f85 13322 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13323 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13324 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13325 continue;
57963c05
AM
13326 o = sub->sections;
13327 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13328 continue;
c152c796
AM
13329
13330 for (o = sub->sections; o != NULL; o = o->next)
13331 {
a33dafc3
L
13332 /* When any section in a section group is kept, we keep all
13333 sections in the section group. If the first member of
13334 the section group is excluded, we will also exclude the
13335 group section. */
13336 if (o->flags & SEC_GROUP)
13337 {
13338 asection *first = elf_next_in_group (o);
13339 o->gc_mark = first->gc_mark;
13340 }
c152c796 13341
1e7eae0d 13342 if (o->gc_mark)
c152c796
AM
13343 continue;
13344
13345 /* Skip sweeping sections already excluded. */
13346 if (o->flags & SEC_EXCLUDE)
13347 continue;
13348
13349 /* Since this is early in the link process, it is simple
13350 to remove a section from the output. */
13351 o->flags |= SEC_EXCLUDE;
13352
c55fe096 13353 if (info->print_gc_sections && o->size != 0)
695344c0 13354 /* xgettext:c-format */
9793eb77 13355 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13356 o, sub);
c152c796
AM
13357 }
13358 }
13359
c152c796
AM
13360 return TRUE;
13361}
13362
13363/* Propagate collected vtable information. This is called through
13364 elf_link_hash_traverse. */
13365
13366static bfd_boolean
13367elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13368{
c152c796 13369 /* Those that are not vtables. */
cbd0eecf
L
13370 if (h->start_stop
13371 || h->u2.vtable == NULL
13372 || h->u2.vtable->parent == NULL)
c152c796
AM
13373 return TRUE;
13374
13375 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13376 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13377 return TRUE;
13378
13379 /* If we've already been done, exit. */
cbd0eecf 13380 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13381 return TRUE;
13382
13383 /* Make sure the parent's table is up to date. */
cbd0eecf 13384 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13385
cbd0eecf 13386 if (h->u2.vtable->used == NULL)
c152c796
AM
13387 {
13388 /* None of this table's entries were referenced. Re-use the
13389 parent's table. */
cbd0eecf
L
13390 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13391 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13392 }
13393 else
13394 {
13395 size_t n;
13396 bfd_boolean *cu, *pu;
13397
13398 /* Or the parent's entries into ours. */
cbd0eecf 13399 cu = h->u2.vtable->used;
c152c796 13400 cu[-1] = TRUE;
cbd0eecf 13401 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13402 if (pu != NULL)
13403 {
13404 const struct elf_backend_data *bed;
13405 unsigned int log_file_align;
13406
13407 bed = get_elf_backend_data (h->root.u.def.section->owner);
13408 log_file_align = bed->s->log_file_align;
cbd0eecf 13409 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13410 while (n--)
13411 {
13412 if (*pu)
13413 *cu = TRUE;
13414 pu++;
13415 cu++;
13416 }
13417 }
13418 }
13419
13420 return TRUE;
13421}
13422
13423static bfd_boolean
13424elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13425{
13426 asection *sec;
13427 bfd_vma hstart, hend;
13428 Elf_Internal_Rela *relstart, *relend, *rel;
13429 const struct elf_backend_data *bed;
13430 unsigned int log_file_align;
13431
c152c796
AM
13432 /* Take care of both those symbols that do not describe vtables as
13433 well as those that are not loaded. */
cbd0eecf
L
13434 if (h->start_stop
13435 || h->u2.vtable == NULL
13436 || h->u2.vtable->parent == NULL)
c152c796
AM
13437 return TRUE;
13438
13439 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13440 || h->root.type == bfd_link_hash_defweak);
13441
13442 sec = h->root.u.def.section;
13443 hstart = h->root.u.def.value;
13444 hend = hstart + h->size;
13445
13446 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13447 if (!relstart)
13448 return *(bfd_boolean *) okp = FALSE;
13449 bed = get_elf_backend_data (sec->owner);
13450 log_file_align = bed->s->log_file_align;
13451
056bafd4 13452 relend = relstart + sec->reloc_count;
c152c796
AM
13453
13454 for (rel = relstart; rel < relend; ++rel)
13455 if (rel->r_offset >= hstart && rel->r_offset < hend)
13456 {
13457 /* If the entry is in use, do nothing. */
cbd0eecf
L
13458 if (h->u2.vtable->used
13459 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13460 {
13461 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13462 if (h->u2.vtable->used[entry])
c152c796
AM
13463 continue;
13464 }
13465 /* Otherwise, kill it. */
13466 rel->r_offset = rel->r_info = rel->r_addend = 0;
13467 }
13468
13469 return TRUE;
13470}
13471
87538722
AM
13472/* Mark sections containing dynamically referenced symbols. When
13473 building shared libraries, we must assume that any visible symbol is
13474 referenced. */
715df9b8 13475
64d03ab5
AM
13476bfd_boolean
13477bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13478{
87538722 13479 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13480 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13481
715df9b8
EB
13482 if ((h->root.type == bfd_link_hash_defined
13483 || h->root.type == bfd_link_hash_defweak)
d664fd41 13484 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13485 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13486 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13487 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13488 && (!bfd_link_executable (info)
22185505 13489 || info->gc_keep_exported
b407645f
AM
13490 || info->export_dynamic
13491 || (h->dynamic
13492 && d != NULL
13493 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13494 && (h->versioned >= versioned
54e8959c
L
13495 || !bfd_hide_sym_by_version (info->version_info,
13496 h->root.root.string)))))
715df9b8
EB
13497 h->root.u.def.section->flags |= SEC_KEEP;
13498
13499 return TRUE;
13500}
3b36f7e6 13501
74f0fb50
AM
13502/* Keep all sections containing symbols undefined on the command-line,
13503 and the section containing the entry symbol. */
13504
13505void
13506_bfd_elf_gc_keep (struct bfd_link_info *info)
13507{
13508 struct bfd_sym_chain *sym;
13509
13510 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13511 {
13512 struct elf_link_hash_entry *h;
13513
13514 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13515 FALSE, FALSE, FALSE);
13516
13517 if (h != NULL
13518 && (h->root.type == bfd_link_hash_defined
13519 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13520 && !bfd_is_abs_section (h->root.u.def.section)
13521 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13522 h->root.u.def.section->flags |= SEC_KEEP;
13523 }
13524}
13525
2f0c68f2
CM
13526bfd_boolean
13527bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13528 struct bfd_link_info *info)
13529{
13530 bfd *ibfd = info->input_bfds;
13531
13532 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13533 {
13534 asection *sec;
13535 struct elf_reloc_cookie cookie;
13536
13537 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13538 continue;
57963c05
AM
13539 sec = ibfd->sections;
13540 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13541 continue;
2f0c68f2
CM
13542
13543 if (!init_reloc_cookie (&cookie, info, ibfd))
13544 return FALSE;
13545
13546 for (sec = ibfd->sections; sec; sec = sec->next)
13547 {
13548 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13549 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13550 {
13551 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13552 fini_reloc_cookie_rels (&cookie, sec);
13553 }
13554 }
13555 }
13556 return TRUE;
13557}
13558
c152c796
AM
13559/* Do mark and sweep of unused sections. */
13560
13561bfd_boolean
13562bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13563{
13564 bfd_boolean ok = TRUE;
13565 bfd *sub;
6a5bb875 13566 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13567 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13568 struct elf_link_hash_table *htab;
c152c796 13569
64d03ab5 13570 if (!bed->can_gc_sections
715df9b8 13571 || !is_elf_hash_table (info->hash))
c152c796 13572 {
9793eb77 13573 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13574 return TRUE;
13575 }
13576
74f0fb50 13577 bed->gc_keep (info);
da44f4e5 13578 htab = elf_hash_table (info);
74f0fb50 13579
9d0a14d3
RS
13580 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13581 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13582 for (sub = info->input_bfds;
13583 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13584 sub = sub->link.next)
9d0a14d3
RS
13585 {
13586 asection *sec;
13587 struct elf_reloc_cookie cookie;
13588
57963c05
AM
13589 sec = sub->sections;
13590 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13591 continue;
9d0a14d3 13592 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13593 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13594 {
13595 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13596 if (elf_section_data (sec)->sec_info
13597 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13598 elf_eh_frame_section (sub) = sec;
13599 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13600 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13601 }
13602 }
9d0a14d3 13603
c152c796 13604 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13605 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13606 if (!ok)
13607 return FALSE;
13608
13609 /* Kill the vtable relocations that were not used. */
da44f4e5 13610 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13611 if (!ok)
13612 return FALSE;
13613
715df9b8 13614 /* Mark dynamically referenced symbols. */
22185505 13615 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13616 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13617
715df9b8 13618 /* Grovel through relocs to find out who stays ... */
64d03ab5 13619 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13620 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13621 {
13622 asection *o;
13623
b19a8f85 13624 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13625 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13626 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13627 continue;
13628
57963c05
AM
13629 o = sub->sections;
13630 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13631 continue;
13632
7f6ab9f8
AM
13633 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13634 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13635 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13636 well as FINI_ARRAY sections for ld -r. */
c152c796 13637 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13638 if (!o->gc_mark
13639 && (o->flags & SEC_EXCLUDE) == 0
24007750 13640 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13641 || (bfd_link_relocatable (info)
13642 && ((elf_section_data (o)->this_hdr.sh_type
13643 == SHT_PREINIT_ARRAY)
13644 || (elf_section_data (o)->this_hdr.sh_type
13645 == SHT_INIT_ARRAY)
13646 || (elf_section_data (o)->this_hdr.sh_type
13647 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13648 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13649 && elf_next_in_group (o) == NULL )))
13650 {
13651 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13652 return FALSE;
13653 }
c152c796
AM
13654 }
13655
6a5bb875 13656 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13657 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13658
c152c796 13659 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13660 return elf_gc_sweep (abfd, info);
c152c796
AM
13661}
13662\f
13663/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13664
13665bfd_boolean
13666bfd_elf_gc_record_vtinherit (bfd *abfd,
13667 asection *sec,
13668 struct elf_link_hash_entry *h,
13669 bfd_vma offset)
13670{
13671 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13672 struct elf_link_hash_entry **search, *child;
ef53be89 13673 size_t extsymcount;
c152c796
AM
13674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13675
13676 /* The sh_info field of the symtab header tells us where the
13677 external symbols start. We don't care about the local symbols at
13678 this point. */
13679 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13680 if (!elf_bad_symtab (abfd))
13681 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13682
13683 sym_hashes = elf_sym_hashes (abfd);
13684 sym_hashes_end = sym_hashes + extsymcount;
13685
13686 /* Hunt down the child symbol, which is in this section at the same
13687 offset as the relocation. */
13688 for (search = sym_hashes; search != sym_hashes_end; ++search)
13689 {
13690 if ((child = *search) != NULL
13691 && (child->root.type == bfd_link_hash_defined
13692 || child->root.type == bfd_link_hash_defweak)
13693 && child->root.u.def.section == sec
13694 && child->root.u.def.value == offset)
13695 goto win;
13696 }
13697
695344c0 13698 /* xgettext:c-format */
9793eb77 13699 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13700 abfd, sec, (uint64_t) offset);
c152c796
AM
13701 bfd_set_error (bfd_error_invalid_operation);
13702 return FALSE;
13703
13704 win:
cbd0eecf 13705 if (!child->u2.vtable)
f6e332e6 13706 {
cbd0eecf
L
13707 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13708 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13709 if (!child->u2.vtable)
f6e332e6
AM
13710 return FALSE;
13711 }
c152c796
AM
13712 if (!h)
13713 {
13714 /* This *should* only be the absolute section. It could potentially
13715 be that someone has defined a non-global vtable though, which
13716 would be bad. It isn't worth paging in the local symbols to be
13717 sure though; that case should simply be handled by the assembler. */
13718
cbd0eecf 13719 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13720 }
13721 else
cbd0eecf 13722 child->u2.vtable->parent = h;
c152c796
AM
13723
13724 return TRUE;
13725}
13726
13727/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13728
13729bfd_boolean
a0ea3a14 13730bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
13731 struct elf_link_hash_entry *h,
13732 bfd_vma addend)
13733{
13734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13735 unsigned int log_file_align = bed->s->log_file_align;
13736
a0ea3a14
L
13737 if (!h)
13738 {
13739 /* xgettext:c-format */
13740 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13741 abfd, sec);
13742 bfd_set_error (bfd_error_bad_value);
13743 return FALSE;
13744 }
13745
cbd0eecf 13746 if (!h->u2.vtable)
f6e332e6 13747 {
cbd0eecf
L
13748 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13749 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13750 if (!h->u2.vtable)
f6e332e6
AM
13751 return FALSE;
13752 }
13753
cbd0eecf 13754 if (addend >= h->u2.vtable->size)
c152c796
AM
13755 {
13756 size_t size, bytes, file_align;
cbd0eecf 13757 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13758
13759 /* While the symbol is undefined, we have to be prepared to handle
13760 a zero size. */
13761 file_align = 1 << log_file_align;
13762 if (h->root.type == bfd_link_hash_undefined)
13763 size = addend + file_align;
13764 else
13765 {
13766 size = h->size;
13767 if (addend >= size)
13768 {
13769 /* Oops! We've got a reference past the defined end of
13770 the table. This is probably a bug -- shall we warn? */
13771 size = addend + file_align;
13772 }
13773 }
13774 size = (size + file_align - 1) & -file_align;
13775
13776 /* Allocate one extra entry for use as a "done" flag for the
13777 consolidation pass. */
13778 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13779
13780 if (ptr)
13781 {
a50b1753 13782 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13783
13784 if (ptr != NULL)
13785 {
13786 size_t oldbytes;
13787
cbd0eecf 13788 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13789 * sizeof (bfd_boolean));
13790 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13791 }
13792 }
13793 else
a50b1753 13794 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13795
13796 if (ptr == NULL)
13797 return FALSE;
13798
13799 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13800 h->u2.vtable->used = ptr + 1;
13801 h->u2.vtable->size = size;
c152c796
AM
13802 }
13803
cbd0eecf 13804 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13805
13806 return TRUE;
13807}
13808
ae17ab41
CM
13809/* Map an ELF section header flag to its corresponding string. */
13810typedef struct
13811{
13812 char *flag_name;
13813 flagword flag_value;
13814} elf_flags_to_name_table;
13815
13816static elf_flags_to_name_table elf_flags_to_names [] =
13817{
13818 { "SHF_WRITE", SHF_WRITE },
13819 { "SHF_ALLOC", SHF_ALLOC },
13820 { "SHF_EXECINSTR", SHF_EXECINSTR },
13821 { "SHF_MERGE", SHF_MERGE },
13822 { "SHF_STRINGS", SHF_STRINGS },
13823 { "SHF_INFO_LINK", SHF_INFO_LINK},
13824 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13825 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13826 { "SHF_GROUP", SHF_GROUP },
13827 { "SHF_TLS", SHF_TLS },
13828 { "SHF_MASKOS", SHF_MASKOS },
13829 { "SHF_EXCLUDE", SHF_EXCLUDE },
13830};
13831
b9c361e0
JL
13832/* Returns TRUE if the section is to be included, otherwise FALSE. */
13833bfd_boolean
ae17ab41 13834bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13835 struct flag_info *flaginfo,
b9c361e0 13836 asection *section)
ae17ab41 13837{
8b127cbc 13838 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13839
8b127cbc 13840 if (!flaginfo->flags_initialized)
ae17ab41 13841 {
8b127cbc
AM
13842 bfd *obfd = info->output_bfd;
13843 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13844 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13845 int with_hex = 0;
13846 int without_hex = 0;
13847
8b127cbc 13848 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13849 {
b9c361e0 13850 unsigned i;
8b127cbc 13851 flagword (*lookup) (char *);
ae17ab41 13852
8b127cbc
AM
13853 lookup = bed->elf_backend_lookup_section_flags_hook;
13854 if (lookup != NULL)
ae17ab41 13855 {
8b127cbc 13856 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13857
13858 if (hexval != 0)
13859 {
13860 if (tf->with == with_flags)
13861 with_hex |= hexval;
13862 else if (tf->with == without_flags)
13863 without_hex |= hexval;
13864 tf->valid = TRUE;
13865 continue;
13866 }
ae17ab41 13867 }
8b127cbc 13868 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13869 {
8b127cbc 13870 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13871 {
13872 if (tf->with == with_flags)
13873 with_hex |= elf_flags_to_names[i].flag_value;
13874 else if (tf->with == without_flags)
13875 without_hex |= elf_flags_to_names[i].flag_value;
13876 tf->valid = TRUE;
13877 break;
13878 }
13879 }
8b127cbc 13880 if (!tf->valid)
b9c361e0 13881 {
68ffbac6 13882 info->callbacks->einfo
9793eb77 13883 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13884 return FALSE;
ae17ab41
CM
13885 }
13886 }
8b127cbc
AM
13887 flaginfo->flags_initialized = TRUE;
13888 flaginfo->only_with_flags |= with_hex;
13889 flaginfo->not_with_flags |= without_hex;
ae17ab41 13890 }
ae17ab41 13891
8b127cbc 13892 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13893 return FALSE;
13894
8b127cbc 13895 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13896 return FALSE;
13897
13898 return TRUE;
ae17ab41
CM
13899}
13900
c152c796
AM
13901struct alloc_got_off_arg {
13902 bfd_vma gotoff;
10455f89 13903 struct bfd_link_info *info;
c152c796
AM
13904};
13905
13906/* We need a special top-level link routine to convert got reference counts
13907 to real got offsets. */
13908
13909static bfd_boolean
13910elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13911{
a50b1753 13912 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13913 bfd *obfd = gofarg->info->output_bfd;
13914 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13915
c152c796
AM
13916 if (h->got.refcount > 0)
13917 {
13918 h->got.offset = gofarg->gotoff;
10455f89 13919 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13920 }
13921 else
13922 h->got.offset = (bfd_vma) -1;
13923
13924 return TRUE;
13925}
13926
13927/* And an accompanying bit to work out final got entry offsets once
13928 we're done. Should be called from final_link. */
13929
13930bfd_boolean
13931bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13932 struct bfd_link_info *info)
13933{
13934 bfd *i;
13935 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13936 bfd_vma gotoff;
c152c796
AM
13937 struct alloc_got_off_arg gofarg;
13938
10455f89
HPN
13939 BFD_ASSERT (abfd == info->output_bfd);
13940
c152c796
AM
13941 if (! is_elf_hash_table (info->hash))
13942 return FALSE;
13943
13944 /* The GOT offset is relative to the .got section, but the GOT header is
13945 put into the .got.plt section, if the backend uses it. */
13946 if (bed->want_got_plt)
13947 gotoff = 0;
13948 else
13949 gotoff = bed->got_header_size;
13950
13951 /* Do the local .got entries first. */
c72f2fb2 13952 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13953 {
13954 bfd_signed_vma *local_got;
ef53be89 13955 size_t j, locsymcount;
c152c796
AM
13956 Elf_Internal_Shdr *symtab_hdr;
13957
13958 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13959 continue;
13960
13961 local_got = elf_local_got_refcounts (i);
13962 if (!local_got)
13963 continue;
13964
13965 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13966 if (elf_bad_symtab (i))
13967 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13968 else
13969 locsymcount = symtab_hdr->sh_info;
13970
13971 for (j = 0; j < locsymcount; ++j)
13972 {
13973 if (local_got[j] > 0)
13974 {
13975 local_got[j] = gotoff;
10455f89 13976 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13977 }
13978 else
13979 local_got[j] = (bfd_vma) -1;
13980 }
13981 }
13982
13983 /* Then the global .got entries. .plt refcounts are handled by
13984 adjust_dynamic_symbol */
13985 gofarg.gotoff = gotoff;
10455f89 13986 gofarg.info = info;
c152c796
AM
13987 elf_link_hash_traverse (elf_hash_table (info),
13988 elf_gc_allocate_got_offsets,
13989 &gofarg);
13990 return TRUE;
13991}
13992
13993/* Many folk need no more in the way of final link than this, once
13994 got entry reference counting is enabled. */
13995
13996bfd_boolean
13997bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13998{
13999 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14000 return FALSE;
14001
14002 /* Invoke the regular ELF backend linker to do all the work. */
14003 return bfd_elf_final_link (abfd, info);
14004}
14005
14006bfd_boolean
14007bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14008{
a50b1753 14009 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14010
14011 if (rcookie->bad_symtab)
14012 rcookie->rel = rcookie->rels;
14013
14014 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14015 {
14016 unsigned long r_symndx;
14017
14018 if (! rcookie->bad_symtab)
14019 if (rcookie->rel->r_offset > offset)
14020 return FALSE;
14021 if (rcookie->rel->r_offset != offset)
14022 continue;
14023
14024 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14025 if (r_symndx == STN_UNDEF)
c152c796
AM
14026 return TRUE;
14027
14028 if (r_symndx >= rcookie->locsymcount
14029 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14030 {
14031 struct elf_link_hash_entry *h;
14032
14033 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14034
14035 while (h->root.type == bfd_link_hash_indirect
14036 || h->root.type == bfd_link_hash_warning)
14037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14038
14039 if ((h->root.type == bfd_link_hash_defined
14040 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14041 && (h->root.u.def.section->owner != rcookie->abfd
14042 || h->root.u.def.section->kept_section != NULL
14043 || discarded_section (h->root.u.def.section)))
c152c796 14044 return TRUE;
c152c796
AM
14045 }
14046 else
14047 {
14048 /* It's not a relocation against a global symbol,
14049 but it could be a relocation against a local
14050 symbol for a discarded section. */
14051 asection *isec;
14052 Elf_Internal_Sym *isym;
14053
14054 /* Need to: get the symbol; get the section. */
14055 isym = &rcookie->locsyms[r_symndx];
cb33740c 14056 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14057 if (isec != NULL
14058 && (isec->kept_section != NULL
14059 || discarded_section (isec)))
cb33740c 14060 return TRUE;
c152c796
AM
14061 }
14062 return FALSE;
14063 }
14064 return FALSE;
14065}
14066
14067/* Discard unneeded references to discarded sections.
75938853
AM
14068 Returns -1 on error, 1 if any section's size was changed, 0 if
14069 nothing changed. This function assumes that the relocations are in
14070 sorted order, which is true for all known assemblers. */
c152c796 14071
75938853 14072int
c152c796
AM
14073bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14074{
14075 struct elf_reloc_cookie cookie;
18cd5bce 14076 asection *o;
c152c796 14077 bfd *abfd;
75938853 14078 int changed = 0;
c152c796
AM
14079
14080 if (info->traditional_format
14081 || !is_elf_hash_table (info->hash))
75938853 14082 return 0;
c152c796 14083
18cd5bce
AM
14084 o = bfd_get_section_by_name (output_bfd, ".stab");
14085 if (o != NULL)
c152c796 14086 {
18cd5bce 14087 asection *i;
c152c796 14088
18cd5bce 14089 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14090 {
18cd5bce
AM
14091 if (i->size == 0
14092 || i->reloc_count == 0
14093 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14094 continue;
c152c796 14095
18cd5bce
AM
14096 abfd = i->owner;
14097 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14098 continue;
c152c796 14099
18cd5bce 14100 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14101 return -1;
c152c796 14102
18cd5bce
AM
14103 if (_bfd_discard_section_stabs (abfd, i,
14104 elf_section_data (i)->sec_info,
5241d853
RS
14105 bfd_elf_reloc_symbol_deleted_p,
14106 &cookie))
75938853 14107 changed = 1;
18cd5bce
AM
14108
14109 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14110 }
18cd5bce
AM
14111 }
14112
2f0c68f2
CM
14113 o = NULL;
14114 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14115 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14116 if (o != NULL)
14117 {
14118 asection *i;
d7153c4a 14119 int eh_changed = 0;
79a94a2a 14120 unsigned int eh_alignment;
c152c796 14121
18cd5bce 14122 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14123 {
18cd5bce
AM
14124 if (i->size == 0)
14125 continue;
14126
14127 abfd = i->owner;
14128 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14129 continue;
14130
14131 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14132 return -1;
18cd5bce
AM
14133
14134 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14135 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14136 bfd_elf_reloc_symbol_deleted_p,
14137 &cookie))
d7153c4a
AM
14138 {
14139 eh_changed = 1;
14140 if (i->size != i->rawsize)
14141 changed = 1;
14142 }
18cd5bce
AM
14143
14144 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14145 }
9866ffe2 14146
79a94a2a 14147 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14148 /* Skip over zero terminator, and prevent empty sections from
14149 adding alignment padding at the end. */
14150 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14151 if (i->size == 0)
14152 i->flags |= SEC_EXCLUDE;
14153 else if (i->size > 4)
14154 break;
14155 /* The last non-empty eh_frame section doesn't need padding. */
14156 if (i != NULL)
14157 i = i->map_tail.s;
14158 /* Any prior sections must pad the last FDE out to the output
14159 section alignment. Otherwise we might have zero padding
14160 between sections, which would be seen as a terminator. */
14161 for (; i != NULL; i = i->map_tail.s)
14162 if (i->size == 4)
14163 /* All but the last zero terminator should have been removed. */
14164 BFD_FAIL ();
14165 else
14166 {
14167 bfd_size_type size
14168 = (i->size + eh_alignment - 1) & -eh_alignment;
14169 if (i->size != size)
af471f82 14170 {
9866ffe2
AM
14171 i->size = size;
14172 changed = 1;
14173 eh_changed = 1;
af471f82 14174 }
9866ffe2 14175 }
d7153c4a
AM
14176 if (eh_changed)
14177 elf_link_hash_traverse (elf_hash_table (info),
14178 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14179 }
c152c796 14180
18cd5bce
AM
14181 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14182 {
14183 const struct elf_backend_data *bed;
57963c05 14184 asection *s;
c152c796 14185
18cd5bce
AM
14186 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14187 continue;
57963c05
AM
14188 s = abfd->sections;
14189 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14190 continue;
18cd5bce
AM
14191
14192 bed = get_elf_backend_data (abfd);
14193
14194 if (bed->elf_backend_discard_info != NULL)
14195 {
14196 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14197 return -1;
18cd5bce
AM
14198
14199 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14200 changed = 1;
18cd5bce
AM
14201
14202 fini_reloc_cookie (&cookie, abfd);
14203 }
c152c796
AM
14204 }
14205
2f0c68f2
CM
14206 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14207 _bfd_elf_end_eh_frame_parsing (info);
14208
14209 if (info->eh_frame_hdr_type
0e1862bb 14210 && !bfd_link_relocatable (info)
c152c796 14211 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14212 changed = 1;
c152c796 14213
75938853 14214 return changed;
c152c796 14215}
082b7297 14216
43e1669b 14217bfd_boolean
0c511000 14218_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14219 asection *sec,
c0f00686 14220 struct bfd_link_info *info)
082b7297
L
14221{
14222 flagword flags;
c77ec726 14223 const char *name, *key;
082b7297
L
14224 struct bfd_section_already_linked *l;
14225 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14226
c77ec726
AM
14227 if (sec->output_section == bfd_abs_section_ptr)
14228 return FALSE;
0c511000 14229
c77ec726 14230 flags = sec->flags;
0c511000 14231
c77ec726
AM
14232 /* Return if it isn't a linkonce section. A comdat group section
14233 also has SEC_LINK_ONCE set. */
14234 if ((flags & SEC_LINK_ONCE) == 0)
14235 return FALSE;
0c511000 14236
c77ec726
AM
14237 /* Don't put group member sections on our list of already linked
14238 sections. They are handled as a group via their group section. */
14239 if (elf_sec_group (sec) != NULL)
14240 return FALSE;
0c511000 14241
c77ec726
AM
14242 /* For a SHT_GROUP section, use the group signature as the key. */
14243 name = sec->name;
14244 if ((flags & SEC_GROUP) != 0
14245 && elf_next_in_group (sec) != NULL
14246 && elf_group_name (elf_next_in_group (sec)) != NULL)
14247 key = elf_group_name (elf_next_in_group (sec));
14248 else
14249 {
14250 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14251 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14252 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14253 key++;
0c511000 14254 else
c77ec726
AM
14255 /* Must be a user linkonce section that doesn't follow gcc's
14256 naming convention. In this case we won't be matching
14257 single member groups. */
14258 key = name;
0c511000 14259 }
6d2cd210 14260
c77ec726 14261 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14262
14263 for (l = already_linked_list->entry; l != NULL; l = l->next)
14264 {
c2370991 14265 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14266 sections with a signature of <key> (<key> is some string),
14267 and linkonce sections named .gnu.linkonce.<type>.<key>.
14268 Match like sections. LTO plugin sections are an exception.
14269 They are always named .gnu.linkonce.t.<key> and match either
14270 type of section. */
14271 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14272 && ((flags & SEC_GROUP) != 0
14273 || strcmp (name, l->sec->name) == 0))
14274 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14275 {
14276 /* The section has already been linked. See if we should
6d2cd210 14277 issue a warning. */
c77ec726
AM
14278 if (!_bfd_handle_already_linked (sec, l, info))
14279 return FALSE;
082b7297 14280
c77ec726 14281 if (flags & SEC_GROUP)
3d7f7666 14282 {
c77ec726
AM
14283 asection *first = elf_next_in_group (sec);
14284 asection *s = first;
3d7f7666 14285
c77ec726 14286 while (s != NULL)
3d7f7666 14287 {
c77ec726
AM
14288 s->output_section = bfd_abs_section_ptr;
14289 /* Record which group discards it. */
14290 s->kept_section = l->sec;
14291 s = elf_next_in_group (s);
14292 /* These lists are circular. */
14293 if (s == first)
14294 break;
3d7f7666
L
14295 }
14296 }
082b7297 14297
43e1669b 14298 return TRUE;
082b7297
L
14299 }
14300 }
14301
c77ec726
AM
14302 /* A single member comdat group section may be discarded by a
14303 linkonce section and vice versa. */
14304 if ((flags & SEC_GROUP) != 0)
3d7f7666 14305 {
c77ec726 14306 asection *first = elf_next_in_group (sec);
c2370991 14307
c77ec726
AM
14308 if (first != NULL && elf_next_in_group (first) == first)
14309 /* Check this single member group against linkonce sections. */
14310 for (l = already_linked_list->entry; l != NULL; l = l->next)
14311 if ((l->sec->flags & SEC_GROUP) == 0
14312 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14313 {
14314 first->output_section = bfd_abs_section_ptr;
14315 first->kept_section = l->sec;
14316 sec->output_section = bfd_abs_section_ptr;
14317 break;
14318 }
14319 }
14320 else
14321 /* Check this linkonce section against single member groups. */
14322 for (l = already_linked_list->entry; l != NULL; l = l->next)
14323 if (l->sec->flags & SEC_GROUP)
6d2cd210 14324 {
c77ec726 14325 asection *first = elf_next_in_group (l->sec);
6d2cd210 14326
c77ec726
AM
14327 if (first != NULL
14328 && elf_next_in_group (first) == first
14329 && bfd_elf_match_symbols_in_sections (first, sec, info))
14330 {
14331 sec->output_section = bfd_abs_section_ptr;
14332 sec->kept_section = first;
14333 break;
14334 }
6d2cd210 14335 }
0c511000 14336
c77ec726
AM
14337 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14338 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14339 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14340 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14341 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14342 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14343 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14344 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14345 The reverse order cannot happen as there is never a bfd with only the
14346 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14347 matter as here were are looking only for cross-bfd sections. */
14348
14349 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14350 for (l = already_linked_list->entry; l != NULL; l = l->next)
14351 if ((l->sec->flags & SEC_GROUP) == 0
14352 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14353 {
14354 if (abfd != l->sec->owner)
14355 sec->output_section = bfd_abs_section_ptr;
14356 break;
14357 }
80c29487 14358
082b7297 14359 /* This is the first section with this name. Record it. */
c77ec726 14360 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14361 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14362 return sec->output_section == bfd_abs_section_ptr;
082b7297 14363}
81e1b023 14364
a4d8e49b
L
14365bfd_boolean
14366_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14367{
14368 return sym->st_shndx == SHN_COMMON;
14369}
14370
14371unsigned int
14372_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14373{
14374 return SHN_COMMON;
14375}
14376
14377asection *
14378_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14379{
14380 return bfd_com_section_ptr;
14381}
10455f89
HPN
14382
14383bfd_vma
14384_bfd_elf_default_got_elt_size (bfd *abfd,
14385 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14386 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14387 bfd *ibfd ATTRIBUTE_UNUSED,
14388 unsigned long symndx ATTRIBUTE_UNUSED)
14389{
14390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14391 return bed->s->arch_size / 8;
14392}
83bac4b0
NC
14393
14394/* Routines to support the creation of dynamic relocs. */
14395
83bac4b0
NC
14396/* Returns the name of the dynamic reloc section associated with SEC. */
14397
14398static const char *
14399get_dynamic_reloc_section_name (bfd * abfd,
14400 asection * sec,
14401 bfd_boolean is_rela)
14402{
ddcf1fcf
BS
14403 char *name;
14404 const char *old_name = bfd_get_section_name (NULL, sec);
14405 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14406
ddcf1fcf 14407 if (old_name == NULL)
83bac4b0
NC
14408 return NULL;
14409
ddcf1fcf 14410 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14411 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14412
14413 return name;
14414}
14415
14416/* Returns the dynamic reloc section associated with SEC.
14417 If necessary compute the name of the dynamic reloc section based
14418 on SEC's name (looked up in ABFD's string table) and the setting
14419 of IS_RELA. */
14420
14421asection *
14422_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14423 asection * sec,
14424 bfd_boolean is_rela)
14425{
14426 asection * reloc_sec = elf_section_data (sec)->sreloc;
14427
14428 if (reloc_sec == NULL)
14429 {
14430 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14431
14432 if (name != NULL)
14433 {
3d4d4302 14434 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14435
14436 if (reloc_sec != NULL)
14437 elf_section_data (sec)->sreloc = reloc_sec;
14438 }
14439 }
14440
14441 return reloc_sec;
14442}
14443
14444/* Returns the dynamic reloc section associated with SEC. If the
14445 section does not exist it is created and attached to the DYNOBJ
14446 bfd and stored in the SRELOC field of SEC's elf_section_data
14447 structure.
f8076f98 14448
83bac4b0
NC
14449 ALIGNMENT is the alignment for the newly created section and
14450 IS_RELA defines whether the name should be .rela.<SEC's name>
14451 or .rel.<SEC's name>. The section name is looked up in the
14452 string table associated with ABFD. */
14453
14454asection *
ca4be51c
AM
14455_bfd_elf_make_dynamic_reloc_section (asection *sec,
14456 bfd *dynobj,
14457 unsigned int alignment,
14458 bfd *abfd,
14459 bfd_boolean is_rela)
83bac4b0
NC
14460{
14461 asection * reloc_sec = elf_section_data (sec)->sreloc;
14462
14463 if (reloc_sec == NULL)
14464 {
14465 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14466
14467 if (name == NULL)
14468 return NULL;
14469
3d4d4302 14470 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14471
14472 if (reloc_sec == NULL)
14473 {
3d4d4302
AM
14474 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14475 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14476 if ((sec->flags & SEC_ALLOC) != 0)
14477 flags |= SEC_ALLOC | SEC_LOAD;
14478
3d4d4302 14479 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14480 if (reloc_sec != NULL)
14481 {
8877b5e5
AM
14482 /* _bfd_elf_get_sec_type_attr chooses a section type by
14483 name. Override as it may be wrong, eg. for a user
14484 section named "auto" we'll get ".relauto" which is
14485 seen to be a .rela section. */
14486 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14487 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14488 reloc_sec = NULL;
14489 }
14490 }
14491
14492 elf_section_data (sec)->sreloc = reloc_sec;
14493 }
14494
14495 return reloc_sec;
14496}
1338dd10 14497
bffebb6b
AM
14498/* Copy the ELF symbol type and other attributes for a linker script
14499 assignment from HSRC to HDEST. Generally this should be treated as
14500 if we found a strong non-dynamic definition for HDEST (except that
14501 ld ignores multiple definition errors). */
1338dd10 14502void
bffebb6b
AM
14503_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14504 struct bfd_link_hash_entry *hdest,
14505 struct bfd_link_hash_entry *hsrc)
1338dd10 14506{
bffebb6b
AM
14507 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14508 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14509 Elf_Internal_Sym isym;
1338dd10
PB
14510
14511 ehdest->type = ehsrc->type;
35fc36a8 14512 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14513
14514 isym.st_other = ehsrc->other;
b8417128 14515 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14516}
351f65ca
L
14517
14518/* Append a RELA relocation REL to section S in BFD. */
14519
14520void
14521elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14522{
14523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14524 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14525 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14526 bed->s->swap_reloca_out (abfd, rel, loc);
14527}
14528
14529/* Append a REL relocation REL to section S in BFD. */
14530
14531void
14532elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14533{
14534 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14535 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14536 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14537 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14538}
7dba9362
AM
14539
14540/* Define __start, __stop, .startof. or .sizeof. symbol. */
14541
14542struct bfd_link_hash_entry *
14543bfd_elf_define_start_stop (struct bfd_link_info *info,
14544 const char *symbol, asection *sec)
14545{
487b6440 14546 struct elf_link_hash_entry *h;
7dba9362 14547
487b6440
AM
14548 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14549 FALSE, FALSE, TRUE);
14550 if (h != NULL
14551 && (h->root.type == bfd_link_hash_undefined
14552 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14553 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14554 {
bf3077a6 14555 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14556 h->root.type = bfd_link_hash_defined;
14557 h->root.u.def.section = sec;
14558 h->root.u.def.value = 0;
14559 h->def_regular = 1;
14560 h->def_dynamic = 0;
14561 h->start_stop = 1;
14562 h->u2.start_stop_section = sec;
14563 if (symbol[0] == '.')
14564 {
14565 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14566 const struct elf_backend_data *bed;
14567 bed = get_elf_backend_data (info->output_bfd);
14568 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14569 }
36b8fda5
AM
14570 else
14571 {
14572 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14573 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14574 if (was_dynamic)
36b8fda5
AM
14575 bfd_elf_link_record_dynamic_symbol (info, h);
14576 }
487b6440 14577 return &h->root;
7dba9362 14578 }
487b6440 14579 return NULL;
7dba9362 14580}
This page took 2.884758 seconds and 4 git commands to generate.