Fix colors in TUI mode in MS-Windows build with ncurses
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
82704155 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
28caa186
AM
35/* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38struct elf_info_failed
39{
40 struct bfd_link_info *info;
28caa186
AM
41 bfd_boolean failed;
42};
43
44/* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47struct elf_find_verdep_info
48{
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55};
56
57static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
2f0c68f2
CM
60asection *
61_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64{
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
07d6d2b8 79 return h->root.u.def.section;
2f0c68f2
CM
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99}
100
d98685ac
AM
101/* Define a symbol in a dynamic linkage section. */
102
103struct elf_link_hash_entry *
104_bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108{
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
ccabcbe5 111 const struct elf_backend_data *bed;
d98685ac
AM
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
ad32986f 121 bh = &h->root;
d98685ac 122 }
ad32986f
NC
123 else
124 bh = NULL;
d98685ac 125
cf18fda4 126 bed = get_elf_backend_data (abfd);
d98685ac 127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 128 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
ad32986f 132 BFD_ASSERT (h != NULL);
d98685ac 133 h->def_regular = 1;
e28df02b 134 h->non_elf = 0;
12b2843a 135 h->root.linker_def = 1;
d98685ac 136 h->type = STT_OBJECT;
00b7642b
AM
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 139
ccabcbe5 140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
141 return h;
142}
143
b34976b6 144bfd_boolean
268b6b39 145_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
146{
147 flagword flags;
aad5d350 148 asection *s;
252b5132 149 struct elf_link_hash_entry *h;
9c5bfbb7 150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 151 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
152
153 /* This function may be called more than once. */
ce558b89 154 if (htab->sgot != NULL)
b34976b6 155 return TRUE;
252b5132 156
e5a52504 157 flags = bed->dynamic_sec_flags;
252b5132 158
14b2f831
AM
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
6de2ae4a
L
164 if (s == NULL
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
252b5132 168
14b2f831 169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
170 if (s == NULL
171 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
252b5132
RH
175 if (bed->want_got_plt)
176 {
14b2f831 177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 178 if (s == NULL
6de2ae4a
L
179 || !bfd_set_section_alignment (abfd, s,
180 bed->s->log_file_align))
b34976b6 181 return FALSE;
6de2ae4a 182 htab->sgotplt = s;
252b5132
RH
183 }
184
64e77c6d
L
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
187
2517a57f
AM
188 if (bed->want_got_sym)
189 {
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
6de2ae4a
L
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
2517a57f 196 elf_hash_table (info)->hgot = h;
d98685ac
AM
197 if (h == NULL)
198 return FALSE;
2517a57f 199 }
252b5132 200
b34976b6 201 return TRUE;
252b5132
RH
202}
203\f
7e9f0867
AM
204/* Create a strtab to hold the dynamic symbol names. */
205static bfd_boolean
206_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207{
208 struct elf_link_hash_table *hash_table;
209
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
6cd255ca
L
212 {
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 {
219 bfd *ibfd;
57963c05 220 asection *s;
6cd255ca 221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 222 if ((ibfd->flags
57963c05
AM
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
7e9f0867
AM
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243}
244
45d6a902
AM
245/* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
252b5132 251
b34976b6 252bfd_boolean
268b6b39 253_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 254{
45d6a902 255 flagword flags;
91d6fa6a 256 asection *s;
9c5bfbb7 257 const struct elf_backend_data *bed;
9637f6ef 258 struct elf_link_hash_entry *h;
252b5132 259
0eddce27 260 if (! is_elf_hash_table (info->hash))
45d6a902
AM
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
7e9f0867
AM
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
45d6a902 268
7e9f0867 269 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
45d6a902
AM
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
9b8b325a 276 if (bfd_link_executable (info) && !info->nointerp)
252b5132 277 {
14b2f831
AM
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
3496cb2a 280 if (s == NULL)
45d6a902
AM
281 return FALSE;
282 }
bb0deeff 283
45d6a902
AM
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
14b2f831
AM
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
45d6a902 288 if (s == NULL
45d6a902
AM
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
45d6a902
AM
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
45d6a902
AM
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
45d6a902
AM
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
cae1fbbb 309 elf_hash_table (info)->dynsym = s;
45d6a902 310
14b2f831
AM
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
3496cb2a 313 if (s == NULL)
45d6a902
AM
314 return FALSE;
315
14b2f831 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 317 if (s == NULL
45d6a902
AM
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
9637f6ef
L
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
45d6a902
AM
330 return FALSE;
331
fdc90cb4
JJ
332 if (info->emit_hash)
333 {
14b2f831
AM
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
fdc90cb4
JJ
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
14b2f831
AM
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
fdc90cb4
JJ
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
45d6a902
AM
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
894891db
NC
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368}
369
370/* Create dynamic sections when linking against a dynamic object. */
371
372bfd_boolean
268b6b39 373_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
374{
375 flagword flags, pltflags;
7325306f 376 struct elf_link_hash_entry *h;
45d6a902 377 asection *s;
9c5bfbb7 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 379 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 380
252b5132
RH
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
e5a52504 383 flags = bed->dynamic_sec_flags;
252b5132
RH
384
385 pltflags = flags;
252b5132 386 if (bed->plt_not_loaded)
6df4d94c
MM
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
5d1634d7 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
14b2f831 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 397 if (s == NULL
252b5132 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 399 return FALSE;
6de2ae4a 400 htab->splt = s;
252b5132 401
d98685ac
AM
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
7325306f
RS
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
252b5132 412
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
252b5132 417 if (s == NULL
45d6a902 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 419 return FALSE;
6de2ae4a 420 htab->srelplt = s;
252b5132
RH
421
422 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 423 return FALSE;
252b5132 424
3018b441
RH
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
14b2f831 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 434 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 435 if (s == NULL)
b34976b6 436 return FALSE;
9d19e4fd 437 htab->sdynbss = s;
252b5132 438
5474d94f
AM
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
5474d94f 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 445 flags);
5474d94f
AM
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
3018b441 451 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
9d19e4fd 462 if (bfd_link_executable (info))
3018b441 463 {
14b2f831
AM
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
3018b441 468 if (s == NULL
45d6a902 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 470 return FALSE;
9d19e4fd 471 htab->srelbss = s;
5474d94f
AM
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
3018b441 485 }
252b5132
RH
486 }
487
b34976b6 488 return TRUE;
252b5132
RH
489}
490\f
252b5132
RH
491/* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
b34976b6 499bfd_boolean
c152c796
AM
500bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
252b5132
RH
502{
503 if (h->dynindx == -1)
504 {
2b0f7ef9 505 struct elf_strtab_hash *dynstr;
68b6ddd0 506 char *p;
252b5132 507 const char *name;
ef53be89 508 size_t indx;
252b5132 509
7a13edea
NC
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
9d6eee78
L
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
38048eb9 520 {
f5385ebf 521 h->forced_local = 1;
67687978
PB
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
7a13edea 524 }
0444bdd4 525
7a13edea
NC
526 default:
527 break;
528 }
529
252b5132
RH
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 538 if (dynstr == NULL)
b34976b6 539 return FALSE;
252b5132
RH
540 }
541
542 /* We don't put any version information in the dynamic string
aad5d350 543 table. */
252b5132
RH
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
252b5132 558
ef53be89 559 if (indx == (size_t) -1)
b34976b6 560 return FALSE;
252b5132
RH
561 h->dynstr_index = indx;
562 }
563
b34976b6 564 return TRUE;
252b5132 565}
45d6a902 566\f
55255dae
L
567/* Mark a symbol dynamic. */
568
28caa186 569static void
55255dae 570bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
55255dae 573{
40b36307 574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 575
40b36307 576 /* It may be called more than once on the same H. */
0e1862bb 577 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
578 return;
579
40b36307
L
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
b8871f35 582 || h->type == STT_COMMON
40b36307 583 || (sym != NULL
b8871f35
L
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 586 || (d != NULL
73ec947d 587 && h->non_elf
40b36307 588 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
589 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
55255dae
L
595}
596
45d6a902
AM
597/* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600bfd_boolean
fe21a8fc
L
601bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
268b6b39 603 const char *name,
fe21a8fc
L
604 bfd_boolean provide,
605 bfd_boolean hidden)
45d6a902 606{
00cbee0a 607 struct elf_link_hash_entry *h, *hv;
4ea42fb7 608 struct elf_link_hash_table *htab;
00cbee0a 609 const struct elf_backend_data *bed;
45d6a902 610
0eddce27 611 if (!is_elf_hash_table (info->hash))
45d6a902
AM
612 return TRUE;
613
4ea42fb7
AM
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 616 if (h == NULL)
4ea42fb7 617 return provide;
45d6a902 618
8e2a4f11
AM
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
0f550b3d
L
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
73ec947d
AM
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
00cbee0a 643 switch (h->root.type)
77cfaee6 644 {
00cbee0a
L
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
4ea42fb7 654 h->root.type = bfd_link_hash_new;
77cfaee6
AM
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
657 break;
658 case bfd_link_hash_new:
00cbee0a
L
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 662 the versioned symbol point to this one. */
00cbee0a
L
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
8e2a4f11
AM
675 default:
676 BFD_FAIL ();
c2596ca5 677 return FALSE;
55255dae 678 }
45d6a902
AM
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
f5385ebf
AM
685 && h->def_dynamic
686 && !h->def_regular)
45d6a902
AM
687 h->root.type = bfd_link_hash_undefined;
688
48e30f52
L
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
692 more. */
693 if (h->def_dynamic && !h->def_regular)
b531344c
MR
694 h->verinfo.verdef = NULL;
695
696 /* Make sure this symbol is not garbage collected. */
697 h->mark = 1;
45d6a902 698
f5385ebf 699 h->def_regular = 1;
45d6a902 700
eb8476a6 701 if (hidden)
fe21a8fc 702 {
91d6fa6a 703 bed = get_elf_backend_data (output_bfd);
b8297068
AM
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 }
708
6fa3860b
PB
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
710 and executables. */
0e1862bb 711 if (!bfd_link_relocatable (info)
6fa3860b
PB
712 && h->dynindx != -1
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
715 h->forced_local = 1;
716
f5385ebf
AM
717 if ((h->def_dynamic
718 || h->ref_dynamic
6b3b0ab8
L
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 721 && !h->forced_local
45d6a902
AM
722 && h->dynindx == -1)
723 {
c152c796 724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
725 return FALSE;
726
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
60d67dc8 730 if (h->is_weakalias)
45d6a902 731 {
60d67dc8
AM
732 struct elf_link_hash_entry *def = weakdef (h);
733
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
736 return FALSE;
737 }
738 }
739
740 return TRUE;
741}
42751cf3 742
8c58d23b
AM
743/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
746
747int
c152c796
AM
748bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
8c58d23b
AM
751{
752 bfd_size_type amt;
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
ef53be89 756 size_t dynstr_index;
8c58d23b
AM
757 char *name;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
760
0eddce27 761 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
762 return 0;
763
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 return 1;
768
769 amt = sizeof (*entry);
a50b1753 770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
771 if (entry == NULL)
772 return 0;
773
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 776 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
777 {
778 bfd_release (input_bfd, entry);
779 return 0;
780 }
781
782 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 783 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
784 {
785 asection *s;
786
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
789 {
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
793 return 2;
794 }
795 }
796
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
800
801 dynstr = elf_hash_table (info)->dynstr;
802 if (dynstr == NULL)
803 {
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 if (dynstr == NULL)
807 return 0;
808 }
809
b34976b6 810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 811 if (dynstr_index == (size_t) -1)
8c58d23b
AM
812 return 0;
813 entry->isym.st_name = dynstr_index;
814
815 eht = elf_hash_table (info);
816
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
821 eht->dynsymcount++;
822
823 /* Whatever binding the symbol had before, it's now local. */
824 entry->isym.st_info
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
826
827 /* The dynindx will be set at the end of size_dynamic_sections. */
828
829 return 1;
830}
831
30b30c21 832/* Return the dynindex of a local dynamic symbol. */
42751cf3 833
30b30c21 834long
268b6b39
AM
835_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
836 bfd *input_bfd,
837 long input_indx)
30b30c21
RH
838{
839 struct elf_link_local_dynamic_entry *e;
840
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 return e->dynindx;
844 return -1;
845}
846
847/* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
850
b34976b6 851static bfd_boolean
268b6b39
AM
852elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 void *data)
42751cf3 854{
a50b1753 855 size_t *count = (size_t *) data;
30b30c21 856
6fa3860b
PB
857 if (h->forced_local)
858 return TRUE;
859
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
862
863 return TRUE;
864}
865
866
867/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
869
870static bfd_boolean
871elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 void *data)
873{
a50b1753 874 size_t *count = (size_t *) data;
6fa3860b 875
6fa3860b
PB
876 if (!h->forced_local)
877 return TRUE;
878
42751cf3 879 if (h->dynindx != -1)
30b30c21
RH
880 h->dynindx = ++(*count);
881
b34976b6 882 return TRUE;
42751cf3 883}
30b30c21 884
aee6f5b4
AO
885/* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
887bfd_boolean
d00dd7dc
AM
888_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
890 asection *p)
aee6f5b4 891{
74541ad4 892 struct elf_link_hash_table *htab;
ca55926c 893 asection *ip;
74541ad4 894
aee6f5b4
AO
895 switch (elf_section_data (p)->this_hdr.sh_type)
896 {
897 case SHT_PROGBITS:
898 case SHT_NOBITS:
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
901 case SHT_NULL:
74541ad4
AM
902 htab = elf_hash_table (info);
903 if (p == htab->tls_sec)
904 return FALSE;
905
906 if (htab->text_index_section != NULL)
907 return p != htab->text_index_section && p != htab->data_index_section;
908
ca55926c 909 return (htab->dynobj != NULL
3d4d4302 910 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 911 && ip->output_section == p);
aee6f5b4
AO
912
913 /* There shouldn't be section relative relocations
914 against any other section. */
915 default:
916 return TRUE;
917 }
918}
919
d00dd7dc
AM
920bfd_boolean
921_bfd_elf_omit_section_dynsym_all
922 (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
924 asection *p ATTRIBUTE_UNUSED)
925{
926 return TRUE;
927}
928
062e2358 929/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
930 symbol for each output section, which come first. Next come symbols
931 which have been forced to local binding. Then all of the back-end
932 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
933 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
934 (This prevents the early call before elf_backend_init_index_section
935 and strip_excluded_output_sections setting dynindx for sections
936 that are stripped.) */
30b30c21 937
554220db
AM
938static unsigned long
939_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
940 struct bfd_link_info *info,
941 unsigned long *section_sym_count)
30b30c21
RH
942{
943 unsigned long dynsymcount = 0;
63f452a8 944 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 945
0e1862bb
L
946 if (bfd_link_pic (info)
947 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 948 {
aee6f5b4 949 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
950 asection *p;
951 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 952 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 953 && (p->flags & SEC_ALLOC) != 0
7f923b7f 954 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 955 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
956 {
957 ++dynsymcount;
958 if (do_sec)
959 elf_section_data (p)->dynindx = dynsymcount;
960 }
961 else if (do_sec)
74541ad4 962 elf_section_data (p)->dynindx = 0;
30b30c21 963 }
63f452a8
AM
964 if (do_sec)
965 *section_sym_count = dynsymcount;
30b30c21 966
6fa3860b
PB
967 elf_link_hash_traverse (elf_hash_table (info),
968 elf_link_renumber_local_hash_table_dynsyms,
969 &dynsymcount);
970
30b30c21
RH
971 if (elf_hash_table (info)->dynlocal)
972 {
973 struct elf_link_local_dynamic_entry *p;
974 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
975 p->dynindx = ++dynsymcount;
976 }
90ac2420 977 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
978
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_hash_table_dynsyms,
981 &dynsymcount);
982
d5486c43
L
983 /* There is an unused NULL entry at the head of the table which we
984 must account for in our count even if the table is empty since it
985 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
986 .dynamic section. */
987 dynsymcount++;
30b30c21 988
ccabcbe5
AM
989 elf_hash_table (info)->dynsymcount = dynsymcount;
990 return dynsymcount;
30b30c21 991}
252b5132 992
54ac0771
L
993/* Merge st_other field. */
994
995static void
996elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 997 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 998 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
999{
1000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1001
1002 /* If st_other has a processor-specific meaning, specific
cd3416da 1003 code might be needed here. */
54ac0771
L
1004 if (bed->elf_backend_merge_symbol_attribute)
1005 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1006 dynamic);
1007
cd3416da 1008 if (!dynamic)
54ac0771 1009 {
cd3416da
AM
1010 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1011 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1012
cd3416da
AM
1013 /* Keep the most constraining visibility. Leave the remainder
1014 of the st_other field to elf_backend_merge_symbol_attribute. */
1015 if (symvis - 1 < hvis - 1)
1016 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1017 }
b8417128
AM
1018 else if (definition
1019 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1020 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1021 h->protected_def = 1;
54ac0771
L
1022}
1023
4f3fedcf
AM
1024/* This function is called when we want to merge a new symbol with an
1025 existing symbol. It handles the various cases which arise when we
1026 find a definition in a dynamic object, or when there is already a
1027 definition in a dynamic object. The new symbol is described by
1028 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1029 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1030 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1031 of an old common symbol. We set OVERRIDE if the old symbol is
1032 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1033 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1034 to change. By OK to change, we mean that we shouldn't warn if the
1035 type or size does change. */
45d6a902 1036
8a56bd02 1037static bfd_boolean
268b6b39
AM
1038_bfd_elf_merge_symbol (bfd *abfd,
1039 struct bfd_link_info *info,
1040 const char *name,
1041 Elf_Internal_Sym *sym,
1042 asection **psec,
1043 bfd_vma *pvalue,
4f3fedcf
AM
1044 struct elf_link_hash_entry **sym_hash,
1045 bfd **poldbfd,
37a9e49a 1046 bfd_boolean *pold_weak,
af44c138 1047 unsigned int *pold_alignment,
268b6b39
AM
1048 bfd_boolean *skip,
1049 bfd_boolean *override,
1050 bfd_boolean *type_change_ok,
6e33951e
L
1051 bfd_boolean *size_change_ok,
1052 bfd_boolean *matched)
252b5132 1053{
7479dfd4 1054 asection *sec, *oldsec;
45d6a902 1055 struct elf_link_hash_entry *h;
90c984fc 1056 struct elf_link_hash_entry *hi;
45d6a902
AM
1057 struct elf_link_hash_entry *flip;
1058 int bind;
1059 bfd *oldbfd;
1060 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1061 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1062 const struct elf_backend_data *bed;
6e33951e 1063 char *new_version;
93f4de39 1064 bfd_boolean default_sym = *matched;
45d6a902
AM
1065
1066 *skip = FALSE;
1067 *override = FALSE;
1068
1069 sec = *psec;
1070 bind = ELF_ST_BIND (sym->st_info);
1071
1072 if (! bfd_is_und_section (sec))
1073 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1074 else
1075 h = ((struct elf_link_hash_entry *)
1076 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1077 if (h == NULL)
1078 return FALSE;
1079 *sym_hash = h;
252b5132 1080
88ba32a0
L
1081 bed = get_elf_backend_data (abfd);
1082
6e33951e 1083 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1084 if (h->versioned != unversioned)
6e33951e 1085 {
422f1182
L
1086 /* Symbol version is unknown or versioned. */
1087 new_version = strrchr (name, ELF_VER_CHR);
1088 if (new_version)
1089 {
1090 if (h->versioned == unknown)
1091 {
1092 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1093 h->versioned = versioned_hidden;
1094 else
1095 h->versioned = versioned;
1096 }
1097 new_version += 1;
1098 if (new_version[0] == '\0')
1099 new_version = NULL;
1100 }
1101 else
1102 h->versioned = unversioned;
6e33951e 1103 }
422f1182
L
1104 else
1105 new_version = NULL;
6e33951e 1106
90c984fc
L
1107 /* For merging, we only care about real symbols. But we need to make
1108 sure that indirect symbol dynamic flags are updated. */
1109 hi = h;
45d6a902
AM
1110 while (h->root.type == bfd_link_hash_indirect
1111 || h->root.type == bfd_link_hash_warning)
1112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1113
6e33951e
L
1114 if (!*matched)
1115 {
1116 if (hi == h || h->root.type == bfd_link_hash_new)
1117 *matched = TRUE;
1118 else
1119 {
ae7683d2 1120 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1121 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1122 true if the new symbol is only visible to the symbol with
6e33951e 1123 the same symbol version. */
422f1182
L
1124 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1125 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1126 if (!old_hidden && !new_hidden)
1127 /* The new symbol matches the existing symbol if both
1128 aren't hidden. */
1129 *matched = TRUE;
1130 else
1131 {
1132 /* OLD_VERSION is the symbol version of the existing
1133 symbol. */
422f1182
L
1134 char *old_version;
1135
1136 if (h->versioned >= versioned)
1137 old_version = strrchr (h->root.root.string,
1138 ELF_VER_CHR) + 1;
1139 else
1140 old_version = NULL;
6e33951e
L
1141
1142 /* The new symbol matches the existing symbol if they
1143 have the same symbol version. */
1144 *matched = (old_version == new_version
1145 || (old_version != NULL
1146 && new_version != NULL
1147 && strcmp (old_version, new_version) == 0));
1148 }
1149 }
1150 }
1151
934bce08
AM
1152 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1153 existing symbol. */
1154
1155 oldbfd = NULL;
1156 oldsec = NULL;
1157 switch (h->root.type)
1158 {
1159 default:
1160 break;
1161
1162 case bfd_link_hash_undefined:
1163 case bfd_link_hash_undefweak:
1164 oldbfd = h->root.u.undef.abfd;
1165 break;
1166
1167 case bfd_link_hash_defined:
1168 case bfd_link_hash_defweak:
1169 oldbfd = h->root.u.def.section->owner;
1170 oldsec = h->root.u.def.section;
1171 break;
1172
1173 case bfd_link_hash_common:
1174 oldbfd = h->root.u.c.p->section->owner;
1175 oldsec = h->root.u.c.p->section;
1176 if (pold_alignment)
1177 *pold_alignment = h->root.u.c.p->alignment_power;
1178 break;
1179 }
1180 if (poldbfd && *poldbfd == NULL)
1181 *poldbfd = oldbfd;
1182
1183 /* Differentiate strong and weak symbols. */
1184 newweak = bind == STB_WEAK;
1185 oldweak = (h->root.type == bfd_link_hash_defweak
1186 || h->root.type == bfd_link_hash_undefweak);
1187 if (pold_weak)
1188 *pold_weak = oldweak;
1189
40b36307 1190 /* We have to check it for every instance since the first few may be
ee659f1f 1191 references and not all compilers emit symbol type for undefined
40b36307
L
1192 symbols. */
1193 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1194
ee659f1f
AM
1195 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1196 respectively, is from a dynamic object. */
1197
1198 newdyn = (abfd->flags & DYNAMIC) != 0;
1199
1200 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1201 syms and defined syms in dynamic libraries respectively.
1202 ref_dynamic on the other hand can be set for a symbol defined in
1203 a dynamic library, and def_dynamic may not be set; When the
1204 definition in a dynamic lib is overridden by a definition in the
1205 executable use of the symbol in the dynamic lib becomes a
1206 reference to the executable symbol. */
1207 if (newdyn)
1208 {
1209 if (bfd_is_und_section (sec))
1210 {
1211 if (bind != STB_WEAK)
1212 {
1213 h->ref_dynamic_nonweak = 1;
1214 hi->ref_dynamic_nonweak = 1;
1215 }
1216 }
1217 else
1218 {
6e33951e
L
1219 /* Update the existing symbol only if they match. */
1220 if (*matched)
1221 h->dynamic_def = 1;
ee659f1f
AM
1222 hi->dynamic_def = 1;
1223 }
1224 }
1225
45d6a902
AM
1226 /* If we just created the symbol, mark it as being an ELF symbol.
1227 Other than that, there is nothing to do--there is no merge issue
1228 with a newly defined symbol--so we just return. */
1229
1230 if (h->root.type == bfd_link_hash_new)
252b5132 1231 {
f5385ebf 1232 h->non_elf = 0;
45d6a902
AM
1233 return TRUE;
1234 }
252b5132 1235
45d6a902
AM
1236 /* In cases involving weak versioned symbols, we may wind up trying
1237 to merge a symbol with itself. Catch that here, to avoid the
1238 confusion that results if we try to override a symbol with
1239 itself. The additional tests catch cases like
1240 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1241 dynamic object, which we do want to handle here. */
1242 if (abfd == oldbfd
895fa45f 1243 && (newweak || oldweak)
45d6a902 1244 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1245 || !h->def_regular))
45d6a902
AM
1246 return TRUE;
1247
707bba77 1248 olddyn = FALSE;
45d6a902
AM
1249 if (oldbfd != NULL)
1250 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1251 else if (oldsec != NULL)
45d6a902 1252 {
707bba77 1253 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1254 indices used by MIPS ELF. */
707bba77 1255 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1256 }
252b5132 1257
1a3b5c34
AM
1258 /* Handle a case where plugin_notice won't be called and thus won't
1259 set the non_ir_ref flags on the first pass over symbols. */
1260 if (oldbfd != NULL
1261 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1262 && newdyn != olddyn)
1263 {
1264 h->root.non_ir_ref_dynamic = TRUE;
1265 hi->root.non_ir_ref_dynamic = TRUE;
1266 }
1267
45d6a902
AM
1268 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1269 respectively, appear to be a definition rather than reference. */
1270
707bba77 1271 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1272
707bba77
AM
1273 olddef = (h->root.type != bfd_link_hash_undefined
1274 && h->root.type != bfd_link_hash_undefweak
202ac193 1275 && h->root.type != bfd_link_hash_common);
45d6a902 1276
0a36a439
L
1277 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1278 respectively, appear to be a function. */
1279
1280 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1281 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1282
1283 oldfunc = (h->type != STT_NOTYPE
1284 && bed->is_function_type (h->type));
1285
c5d37467 1286 if (!(newfunc && oldfunc)
5b677558
AM
1287 && ELF_ST_TYPE (sym->st_info) != h->type
1288 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1289 && h->type != STT_NOTYPE
c5d37467
AM
1290 && (newdef || bfd_is_com_section (sec))
1291 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1292 {
c5d37467
AM
1293 /* If creating a default indirect symbol ("foo" or "foo@") from
1294 a dynamic versioned definition ("foo@@") skip doing so if
1295 there is an existing regular definition with a different
1296 type. We don't want, for example, a "time" variable in the
1297 executable overriding a "time" function in a shared library. */
1298 if (newdyn
1299 && !olddyn)
1300 {
1301 *skip = TRUE;
1302 return TRUE;
1303 }
1304
1305 /* When adding a symbol from a regular object file after we have
1306 created indirect symbols, undo the indirection and any
1307 dynamic state. */
1308 if (hi != h
1309 && !newdyn
1310 && olddyn)
1311 {
1312 h = hi;
1313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1314 h->forced_local = 0;
1315 h->ref_dynamic = 0;
1316 h->def_dynamic = 0;
1317 h->dynamic_def = 0;
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1319 {
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328 return TRUE;
1329 }
580a2b6e
L
1330 }
1331
4c34aff8
AM
1332 /* Check TLS symbols. We don't check undefined symbols introduced
1333 by "ld -u" which have no type (and oldbfd NULL), and we don't
1334 check symbols from plugins because they also have no type. */
1335 if (oldbfd != NULL
1336 && (oldbfd->flags & BFD_PLUGIN) == 0
1337 && (abfd->flags & BFD_PLUGIN) == 0
1338 && ELF_ST_TYPE (sym->st_info) != h->type
1339 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1340 {
1341 bfd *ntbfd, *tbfd;
1342 bfd_boolean ntdef, tdef;
1343 asection *ntsec, *tsec;
1344
1345 if (h->type == STT_TLS)
1346 {
3b36f7e6 1347 ntbfd = abfd;
7479dfd4
L
1348 ntsec = sec;
1349 ntdef = newdef;
1350 tbfd = oldbfd;
1351 tsec = oldsec;
1352 tdef = olddef;
1353 }
1354 else
1355 {
1356 ntbfd = oldbfd;
1357 ntsec = oldsec;
1358 ntdef = olddef;
1359 tbfd = abfd;
1360 tsec = sec;
1361 tdef = newdef;
1362 }
1363
1364 if (tdef && ntdef)
4eca0228 1365 _bfd_error_handler
695344c0 1366 /* xgettext:c-format */
871b3ab2
AM
1367 (_("%s: TLS definition in %pB section %pA "
1368 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1369 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1370 else if (!tdef && !ntdef)
4eca0228 1371 _bfd_error_handler
695344c0 1372 /* xgettext:c-format */
871b3ab2
AM
1373 (_("%s: TLS reference in %pB "
1374 "mismatches non-TLS reference in %pB"),
c08bb8dd 1375 h->root.root.string, tbfd, ntbfd);
7479dfd4 1376 else if (tdef)
4eca0228 1377 _bfd_error_handler
695344c0 1378 /* xgettext:c-format */
871b3ab2
AM
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS reference in %pB"),
c08bb8dd 1381 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1382 else
4eca0228 1383 _bfd_error_handler
695344c0 1384 /* xgettext:c-format */
871b3ab2
AM
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1387 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1388
1389 bfd_set_error (bfd_error_bad_value);
1390 return FALSE;
1391 }
1392
45d6a902
AM
1393 /* If the old symbol has non-default visibility, we ignore the new
1394 definition from a dynamic object. */
1395 if (newdyn
9c7a29a3 1396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1397 && !bfd_is_und_section (sec))
1398 {
1399 *skip = TRUE;
1400 /* Make sure this symbol is dynamic. */
f5385ebf 1401 h->ref_dynamic = 1;
90c984fc 1402 hi->ref_dynamic = 1;
45d6a902
AM
1403 /* A protected symbol has external availability. Make sure it is
1404 recorded as dynamic.
1405
1406 FIXME: Should we check type and size for protected symbol? */
1407 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1408 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1409 else
1410 return TRUE;
1411 }
1412 else if (!newdyn
9c7a29a3 1413 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1414 && h->def_dynamic)
45d6a902
AM
1415 {
1416 /* If the new symbol with non-default visibility comes from a
1417 relocatable file and the old definition comes from a dynamic
1418 object, we remove the old definition. */
6c9b78e6 1419 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1420 {
1421 /* Handle the case where the old dynamic definition is
1422 default versioned. We need to copy the symbol info from
1423 the symbol with default version to the normal one if it
1424 was referenced before. */
1425 if (h->ref_regular)
1426 {
6c9b78e6 1427 hi->root.type = h->root.type;
d2dee3b2 1428 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1429 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1430
6c9b78e6 1431 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1432 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1433 {
aed81c4e
MR
1434 /* If the new symbol is hidden or internal, completely undo
1435 any dynamic link state. */
1436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1437 h->forced_local = 0;
1438 h->ref_dynamic = 0;
d2dee3b2
L
1439 }
1440 else
aed81c4e
MR
1441 h->ref_dynamic = 1;
1442
1443 h->def_dynamic = 0;
aed81c4e
MR
1444 /* FIXME: Should we check type and size for protected symbol? */
1445 h->size = 0;
1446 h->type = 0;
1447
6c9b78e6 1448 h = hi;
d2dee3b2
L
1449 }
1450 else
6c9b78e6 1451 h = hi;
d2dee3b2 1452 }
1de1a317 1453
f5eda473
AM
1454 /* If the old symbol was undefined before, then it will still be
1455 on the undefs list. If the new symbol is undefined or
1456 common, we can't make it bfd_link_hash_new here, because new
1457 undefined or common symbols will be added to the undefs list
1458 by _bfd_generic_link_add_one_symbol. Symbols may not be
1459 added twice to the undefs list. Also, if the new symbol is
1460 undefweak then we don't want to lose the strong undef. */
1461 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1462 {
1de1a317 1463 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1464 h->root.u.undef.abfd = abfd;
1465 }
1466 else
1467 {
1468 h->root.type = bfd_link_hash_new;
1469 h->root.u.undef.abfd = NULL;
1470 }
1471
f5eda473 1472 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1473 {
f5eda473
AM
1474 /* If the new symbol is hidden or internal, completely undo
1475 any dynamic link state. */
1476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1477 h->forced_local = 0;
1478 h->ref_dynamic = 0;
45d6a902 1479 }
f5eda473
AM
1480 else
1481 h->ref_dynamic = 1;
1482 h->def_dynamic = 0;
45d6a902
AM
1483 /* FIXME: Should we check type and size for protected symbol? */
1484 h->size = 0;
1485 h->type = 0;
1486 return TRUE;
1487 }
14a793b2 1488
15b43f48
AM
1489 /* If a new weak symbol definition comes from a regular file and the
1490 old symbol comes from a dynamic library, we treat the new one as
1491 strong. Similarly, an old weak symbol definition from a regular
1492 file is treated as strong when the new symbol comes from a dynamic
1493 library. Further, an old weak symbol from a dynamic library is
1494 treated as strong if the new symbol is from a dynamic library.
1495 This reflects the way glibc's ld.so works.
1496
165f707a
AM
1497 Also allow a weak symbol to override a linker script symbol
1498 defined by an early pass over the script. This is done so the
1499 linker knows the symbol is defined in an object file, for the
1500 DEFINED script function.
1501
15b43f48
AM
1502 Do this before setting *type_change_ok or *size_change_ok so that
1503 we warn properly when dynamic library symbols are overridden. */
1504
165f707a 1505 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1506 newweak = FALSE;
15b43f48 1507 if (olddef && newdyn)
0f8a2703
AM
1508 oldweak = FALSE;
1509
d334575b 1510 /* Allow changes between different types of function symbol. */
0a36a439 1511 if (newfunc && oldfunc)
fcb93ecf
PB
1512 *type_change_ok = TRUE;
1513
79349b09
AM
1514 /* It's OK to change the type if either the existing symbol or the
1515 new symbol is weak. A type change is also OK if the old symbol
1516 is undefined and the new symbol is defined. */
252b5132 1517
79349b09
AM
1518 if (oldweak
1519 || newweak
1520 || (newdef
1521 && h->root.type == bfd_link_hash_undefined))
1522 *type_change_ok = TRUE;
1523
1524 /* It's OK to change the size if either the existing symbol or the
1525 new symbol is weak, or if the old symbol is undefined. */
1526
1527 if (*type_change_ok
1528 || h->root.type == bfd_link_hash_undefined)
1529 *size_change_ok = TRUE;
45d6a902 1530
45d6a902
AM
1531 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1532 symbol, respectively, appears to be a common symbol in a dynamic
1533 object. If a symbol appears in an uninitialized section, and is
1534 not weak, and is not a function, then it may be a common symbol
1535 which was resolved when the dynamic object was created. We want
1536 to treat such symbols specially, because they raise special
1537 considerations when setting the symbol size: if the symbol
1538 appears as a common symbol in a regular object, and the size in
1539 the regular object is larger, we must make sure that we use the
1540 larger size. This problematic case can always be avoided in C,
1541 but it must be handled correctly when using Fortran shared
1542 libraries.
1543
1544 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1545 likewise for OLDDYNCOMMON and OLDDEF.
1546
1547 Note that this test is just a heuristic, and that it is quite
1548 possible to have an uninitialized symbol in a shared object which
1549 is really a definition, rather than a common symbol. This could
1550 lead to some minor confusion when the symbol really is a common
1551 symbol in some regular object. However, I think it will be
1552 harmless. */
1553
1554 if (newdyn
1555 && newdef
79349b09 1556 && !newweak
45d6a902
AM
1557 && (sec->flags & SEC_ALLOC) != 0
1558 && (sec->flags & SEC_LOAD) == 0
1559 && sym->st_size > 0
0a36a439 1560 && !newfunc)
45d6a902
AM
1561 newdyncommon = TRUE;
1562 else
1563 newdyncommon = FALSE;
1564
1565 if (olddyn
1566 && olddef
1567 && h->root.type == bfd_link_hash_defined
f5385ebf 1568 && h->def_dynamic
45d6a902
AM
1569 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1570 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1571 && h->size > 0
0a36a439 1572 && !oldfunc)
45d6a902
AM
1573 olddyncommon = TRUE;
1574 else
1575 olddyncommon = FALSE;
1576
a4d8e49b
L
1577 /* We now know everything about the old and new symbols. We ask the
1578 backend to check if we can merge them. */
5d13b3b3
AM
1579 if (bed->merge_symbol != NULL)
1580 {
1581 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1582 return FALSE;
1583 sec = *psec;
1584 }
a4d8e49b 1585
a83ef4d1
L
1586 /* There are multiple definitions of a normal symbol. Skip the
1587 default symbol as well as definition from an IR object. */
93f4de39 1588 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1589 && !default_sym && h->def_regular
1590 && !(oldbfd != NULL
1591 && (oldbfd->flags & BFD_PLUGIN) != 0
1592 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1593 {
1594 /* Handle a multiple definition. */
1595 (*info->callbacks->multiple_definition) (info, &h->root,
1596 abfd, sec, *pvalue);
1597 *skip = TRUE;
1598 return TRUE;
1599 }
1600
45d6a902
AM
1601 /* If both the old and the new symbols look like common symbols in a
1602 dynamic object, set the size of the symbol to the larger of the
1603 two. */
1604
1605 if (olddyncommon
1606 && newdyncommon
1607 && sym->st_size != h->size)
1608 {
1609 /* Since we think we have two common symbols, issue a multiple
1610 common warning if desired. Note that we only warn if the
1611 size is different. If the size is the same, we simply let
1612 the old symbol override the new one as normally happens with
1613 symbols defined in dynamic objects. */
1614
1a72702b
AM
1615 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1616 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1617 if (sym->st_size > h->size)
1618 h->size = sym->st_size;
252b5132 1619
45d6a902 1620 *size_change_ok = TRUE;
252b5132
RH
1621 }
1622
45d6a902
AM
1623 /* If we are looking at a dynamic object, and we have found a
1624 definition, we need to see if the symbol was already defined by
1625 some other object. If so, we want to use the existing
1626 definition, and we do not want to report a multiple symbol
1627 definition error; we do this by clobbering *PSEC to be
1628 bfd_und_section_ptr.
1629
1630 We treat a common symbol as a definition if the symbol in the
1631 shared library is a function, since common symbols always
1632 represent variables; this can cause confusion in principle, but
1633 any such confusion would seem to indicate an erroneous program or
1634 shared library. We also permit a common symbol in a regular
8170f769 1635 object to override a weak symbol in a shared object. */
45d6a902
AM
1636
1637 if (newdyn
1638 && newdef
77cfaee6 1639 && (olddef
45d6a902 1640 || (h->root.type == bfd_link_hash_common
8170f769 1641 && (newweak || newfunc))))
45d6a902
AM
1642 {
1643 *override = TRUE;
1644 newdef = FALSE;
1645 newdyncommon = FALSE;
252b5132 1646
45d6a902
AM
1647 *psec = sec = bfd_und_section_ptr;
1648 *size_change_ok = TRUE;
252b5132 1649
45d6a902
AM
1650 /* If we get here when the old symbol is a common symbol, then
1651 we are explicitly letting it override a weak symbol or
1652 function in a dynamic object, and we don't want to warn about
1653 a type change. If the old symbol is a defined symbol, a type
1654 change warning may still be appropriate. */
252b5132 1655
45d6a902
AM
1656 if (h->root.type == bfd_link_hash_common)
1657 *type_change_ok = TRUE;
1658 }
1659
1660 /* Handle the special case of an old common symbol merging with a
1661 new symbol which looks like a common symbol in a shared object.
1662 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1663 common symbol, and let _bfd_generic_link_add_one_symbol do the
1664 right thing. */
45d6a902
AM
1665
1666 if (newdyncommon
1667 && h->root.type == bfd_link_hash_common)
1668 {
1669 *override = TRUE;
1670 newdef = FALSE;
1671 newdyncommon = FALSE;
1672 *pvalue = sym->st_size;
a4d8e49b 1673 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1674 *size_change_ok = TRUE;
1675 }
1676
c5e2cead 1677 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1678 if (newdef && olddef && newweak)
54ac0771 1679 {
35ed3f94 1680 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1681 if (!(oldbfd != NULL
1682 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1683 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1684 {
1685 newdef = FALSE;
1686 *skip = TRUE;
1687 }
54ac0771
L
1688
1689 /* Merge st_other. If the symbol already has a dynamic index,
1690 but visibility says it should not be visible, turn it into a
1691 local symbol. */
b8417128 1692 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1693 if (h->dynindx != -1)
1694 switch (ELF_ST_VISIBILITY (h->other))
1695 {
1696 case STV_INTERNAL:
1697 case STV_HIDDEN:
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1699 break;
1700 }
1701 }
c5e2cead 1702
45d6a902
AM
1703 /* If the old symbol is from a dynamic object, and the new symbol is
1704 a definition which is not from a dynamic object, then the new
1705 symbol overrides the old symbol. Symbols from regular files
1706 always take precedence over symbols from dynamic objects, even if
1707 they are defined after the dynamic object in the link.
1708
1709 As above, we again permit a common symbol in a regular object to
1710 override a definition in a shared object if the shared object
0f8a2703 1711 symbol is a function or is weak. */
45d6a902
AM
1712
1713 flip = NULL;
77cfaee6 1714 if (!newdyn
45d6a902
AM
1715 && (newdef
1716 || (bfd_is_com_section (sec)
0a36a439 1717 && (oldweak || oldfunc)))
45d6a902
AM
1718 && olddyn
1719 && olddef
f5385ebf 1720 && h->def_dynamic)
45d6a902
AM
1721 {
1722 /* Change the hash table entry to undefined, and let
1723 _bfd_generic_link_add_one_symbol do the right thing with the
1724 new definition. */
1725
1726 h->root.type = bfd_link_hash_undefined;
1727 h->root.u.undef.abfd = h->root.u.def.section->owner;
1728 *size_change_ok = TRUE;
1729
1730 olddef = FALSE;
1731 olddyncommon = FALSE;
1732
1733 /* We again permit a type change when a common symbol may be
1734 overriding a function. */
1735
1736 if (bfd_is_com_section (sec))
0a36a439
L
1737 {
1738 if (oldfunc)
1739 {
1740 /* If a common symbol overrides a function, make sure
1741 that it isn't defined dynamically nor has type
1742 function. */
1743 h->def_dynamic = 0;
1744 h->type = STT_NOTYPE;
1745 }
1746 *type_change_ok = TRUE;
1747 }
45d6a902 1748
6c9b78e6
AM
1749 if (hi->root.type == bfd_link_hash_indirect)
1750 flip = hi;
45d6a902
AM
1751 else
1752 /* This union may have been set to be non-NULL when this symbol
1753 was seen in a dynamic object. We must force the union to be
1754 NULL, so that it is correct for a regular symbol. */
1755 h->verinfo.vertree = NULL;
1756 }
1757
1758 /* Handle the special case of a new common symbol merging with an
1759 old symbol that looks like it might be a common symbol defined in
1760 a shared object. Note that we have already handled the case in
1761 which a new common symbol should simply override the definition
1762 in the shared library. */
1763
1764 if (! newdyn
1765 && bfd_is_com_section (sec)
1766 && olddyncommon)
1767 {
1768 /* It would be best if we could set the hash table entry to a
1769 common symbol, but we don't know what to use for the section
1770 or the alignment. */
1a72702b
AM
1771 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1772 bfd_link_hash_common, sym->st_size);
45d6a902 1773
4cc11e76 1774 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1775 larger, pretend that the new symbol has its size. */
1776
1777 if (h->size > *pvalue)
1778 *pvalue = h->size;
1779
af44c138
L
1780 /* We need to remember the alignment required by the symbol
1781 in the dynamic object. */
1782 BFD_ASSERT (pold_alignment);
1783 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1784
1785 olddef = FALSE;
1786 olddyncommon = FALSE;
1787
1788 h->root.type = bfd_link_hash_undefined;
1789 h->root.u.undef.abfd = h->root.u.def.section->owner;
1790
1791 *size_change_ok = TRUE;
1792 *type_change_ok = TRUE;
1793
6c9b78e6
AM
1794 if (hi->root.type == bfd_link_hash_indirect)
1795 flip = hi;
45d6a902
AM
1796 else
1797 h->verinfo.vertree = NULL;
1798 }
1799
1800 if (flip != NULL)
1801 {
1802 /* Handle the case where we had a versioned symbol in a dynamic
1803 library and now find a definition in a normal object. In this
1804 case, we make the versioned symbol point to the normal one. */
45d6a902 1805 flip->root.type = h->root.type;
00cbee0a 1806 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1807 h->root.type = bfd_link_hash_indirect;
1808 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1809 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1810 if (h->def_dynamic)
45d6a902 1811 {
f5385ebf
AM
1812 h->def_dynamic = 0;
1813 flip->ref_dynamic = 1;
45d6a902
AM
1814 }
1815 }
1816
45d6a902
AM
1817 return TRUE;
1818}
1819
1820/* This function is called to create an indirect symbol from the
1821 default for the symbol with the default version if needed. The
4f3fedcf 1822 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1823 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1824
28caa186 1825static bfd_boolean
268b6b39
AM
1826_bfd_elf_add_default_symbol (bfd *abfd,
1827 struct bfd_link_info *info,
1828 struct elf_link_hash_entry *h,
1829 const char *name,
1830 Elf_Internal_Sym *sym,
4f3fedcf
AM
1831 asection *sec,
1832 bfd_vma value,
1833 bfd **poldbfd,
e3c9d234 1834 bfd_boolean *dynsym)
45d6a902
AM
1835{
1836 bfd_boolean type_change_ok;
1837 bfd_boolean size_change_ok;
1838 bfd_boolean skip;
1839 char *shortname;
1840 struct elf_link_hash_entry *hi;
1841 struct bfd_link_hash_entry *bh;
9c5bfbb7 1842 const struct elf_backend_data *bed;
45d6a902
AM
1843 bfd_boolean collect;
1844 bfd_boolean dynamic;
e3c9d234 1845 bfd_boolean override;
45d6a902
AM
1846 char *p;
1847 size_t len, shortlen;
ffd65175 1848 asection *tmp_sec;
6e33951e 1849 bfd_boolean matched;
45d6a902 1850
422f1182
L
1851 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1852 return TRUE;
1853
45d6a902
AM
1854 /* If this symbol has a version, and it is the default version, we
1855 create an indirect symbol from the default name to the fully
1856 decorated name. This will cause external references which do not
1857 specify a version to be bound to this version of the symbol. */
1858 p = strchr (name, ELF_VER_CHR);
422f1182
L
1859 if (h->versioned == unknown)
1860 {
1861 if (p == NULL)
1862 {
1863 h->versioned = unversioned;
1864 return TRUE;
1865 }
1866 else
1867 {
1868 if (p[1] != ELF_VER_CHR)
1869 {
1870 h->versioned = versioned_hidden;
1871 return TRUE;
1872 }
1873 else
1874 h->versioned = versioned;
1875 }
1876 }
4373f8af
L
1877 else
1878 {
1879 /* PR ld/19073: We may see an unversioned definition after the
1880 default version. */
1881 if (p == NULL)
1882 return TRUE;
1883 }
45d6a902 1884
45d6a902
AM
1885 bed = get_elf_backend_data (abfd);
1886 collect = bed->collect;
1887 dynamic = (abfd->flags & DYNAMIC) != 0;
1888
1889 shortlen = p - name;
a50b1753 1890 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1891 if (shortname == NULL)
1892 return FALSE;
1893 memcpy (shortname, name, shortlen);
1894 shortname[shortlen] = '\0';
1895
1896 /* We are going to create a new symbol. Merge it with any existing
1897 symbol with this name. For the purposes of the merge, act as
1898 though we were defining the symbol we just defined, although we
1899 actually going to define an indirect symbol. */
1900 type_change_ok = FALSE;
1901 size_change_ok = FALSE;
6e33951e 1902 matched = TRUE;
ffd65175
AM
1903 tmp_sec = sec;
1904 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1905 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1906 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1907 return FALSE;
1908
1909 if (skip)
1910 goto nondefault;
1911
5b677558
AM
1912 if (hi->def_regular)
1913 {
1914 /* If the undecorated symbol will have a version added by a
1915 script different to H, then don't indirect to/from the
1916 undecorated symbol. This isn't ideal because we may not yet
1917 have seen symbol versions, if given by a script on the
1918 command line rather than via --version-script. */
1919 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1920 {
1921 bfd_boolean hide;
1922
1923 hi->verinfo.vertree
1924 = bfd_find_version_for_sym (info->version_info,
1925 hi->root.root.string, &hide);
1926 if (hi->verinfo.vertree != NULL && hide)
1927 {
1928 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1929 goto nondefault;
1930 }
1931 }
1932 if (hi->verinfo.vertree != NULL
1933 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 goto nondefault;
1935 }
1936
45d6a902
AM
1937 if (! override)
1938 {
c6e8a9a8 1939 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1940 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1941 {
1942 bh = &hi->root;
fbcc8baf
L
1943 if (bh->type == bfd_link_hash_defined
1944 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1945 {
1946 /* Mark the previous definition from IR object as
1947 undefined so that the generic linker will override
1948 it. */
1949 bh->type = bfd_link_hash_undefined;
1950 bh->u.undef.abfd = bh->u.def.section->owner;
1951 }
c6e8a9a8
L
1952 if (! (_bfd_generic_link_add_one_symbol
1953 (info, abfd, shortname, BSF_INDIRECT,
1954 bfd_ind_section_ptr,
1955 0, name, FALSE, collect, &bh)))
1956 return FALSE;
1957 hi = (struct elf_link_hash_entry *) bh;
1958 }
45d6a902
AM
1959 }
1960 else
1961 {
1962 /* In this case the symbol named SHORTNAME is overriding the
1963 indirect symbol we want to add. We were planning on making
1964 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1965 is the name without a version. NAME is the fully versioned
1966 name, and it is the default version.
1967
1968 Overriding means that we already saw a definition for the
1969 symbol SHORTNAME in a regular object, and it is overriding
1970 the symbol defined in the dynamic object.
1971
1972 When this happens, we actually want to change NAME, the
1973 symbol we just added, to refer to SHORTNAME. This will cause
1974 references to NAME in the shared object to become references
1975 to SHORTNAME in the regular object. This is what we expect
1976 when we override a function in a shared object: that the
1977 references in the shared object will be mapped to the
1978 definition in the regular object. */
1979
1980 while (hi->root.type == bfd_link_hash_indirect
1981 || hi->root.type == bfd_link_hash_warning)
1982 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1983
1984 h->root.type = bfd_link_hash_indirect;
1985 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1986 if (h->def_dynamic)
45d6a902 1987 {
f5385ebf
AM
1988 h->def_dynamic = 0;
1989 hi->ref_dynamic = 1;
1990 if (hi->ref_regular
1991 || hi->def_regular)
45d6a902 1992 {
c152c796 1993 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1994 return FALSE;
1995 }
1996 }
1997
1998 /* Now set HI to H, so that the following code will set the
1999 other fields correctly. */
2000 hi = h;
2001 }
2002
fab4a87f
L
2003 /* Check if HI is a warning symbol. */
2004 if (hi->root.type == bfd_link_hash_warning)
2005 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2006
45d6a902
AM
2007 /* If there is a duplicate definition somewhere, then HI may not
2008 point to an indirect symbol. We will have reported an error to
2009 the user in that case. */
2010
2011 if (hi->root.type == bfd_link_hash_indirect)
2012 {
2013 struct elf_link_hash_entry *ht;
2014
45d6a902 2015 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2016 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2017
68c88cd4
AM
2018 /* A reference to the SHORTNAME symbol from a dynamic library
2019 will be satisfied by the versioned symbol at runtime. In
2020 effect, we have a reference to the versioned symbol. */
2021 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2022 hi->dynamic_def |= ht->dynamic_def;
2023
45d6a902
AM
2024 /* See if the new flags lead us to realize that the symbol must
2025 be dynamic. */
2026 if (! *dynsym)
2027 {
2028 if (! dynamic)
2029 {
0e1862bb 2030 if (! bfd_link_executable (info)
90c984fc 2031 || hi->def_dynamic
f5385ebf 2032 || hi->ref_dynamic)
45d6a902
AM
2033 *dynsym = TRUE;
2034 }
2035 else
2036 {
f5385ebf 2037 if (hi->ref_regular)
45d6a902
AM
2038 *dynsym = TRUE;
2039 }
2040 }
2041 }
2042
2043 /* We also need to define an indirection from the nondefault version
2044 of the symbol. */
2045
2046nondefault:
2047 len = strlen (name);
a50b1753 2048 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2049 if (shortname == NULL)
2050 return FALSE;
2051 memcpy (shortname, name, shortlen);
2052 memcpy (shortname + shortlen, p + 1, len - shortlen);
2053
2054 /* Once again, merge with any existing symbol. */
2055 type_change_ok = FALSE;
2056 size_change_ok = FALSE;
ffd65175
AM
2057 tmp_sec = sec;
2058 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2059 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2060 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2061 return FALSE;
2062
2063 if (skip)
2064 return TRUE;
2065
2066 if (override)
2067 {
2068 /* Here SHORTNAME is a versioned name, so we don't expect to see
2069 the type of override we do in the case above unless it is
4cc11e76 2070 overridden by a versioned definition. */
45d6a902
AM
2071 if (hi->root.type != bfd_link_hash_defined
2072 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2073 _bfd_error_handler
695344c0 2074 /* xgettext:c-format */
871b3ab2 2075 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2076 abfd, shortname);
45d6a902
AM
2077 }
2078 else
2079 {
2080 bh = &hi->root;
2081 if (! (_bfd_generic_link_add_one_symbol
2082 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2083 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2084 return FALSE;
2085 hi = (struct elf_link_hash_entry *) bh;
2086
2087 /* If there is a duplicate definition somewhere, then HI may not
2088 point to an indirect symbol. We will have reported an error
2089 to the user in that case. */
2090
2091 if (hi->root.type == bfd_link_hash_indirect)
2092 {
fcfa13d2 2093 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2094 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2095 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2096
2097 /* See if the new flags lead us to realize that the symbol
2098 must be dynamic. */
2099 if (! *dynsym)
2100 {
2101 if (! dynamic)
2102 {
0e1862bb 2103 if (! bfd_link_executable (info)
f5385ebf 2104 || hi->ref_dynamic)
45d6a902
AM
2105 *dynsym = TRUE;
2106 }
2107 else
2108 {
f5385ebf 2109 if (hi->ref_regular)
45d6a902
AM
2110 *dynsym = TRUE;
2111 }
2112 }
2113 }
2114 }
2115
2116 return TRUE;
2117}
2118\f
2119/* This routine is used to export all defined symbols into the dynamic
2120 symbol table. It is called via elf_link_hash_traverse. */
2121
28caa186 2122static bfd_boolean
268b6b39 2123_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2124{
a50b1753 2125 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2126
2127 /* Ignore indirect symbols. These are added by the versioning code. */
2128 if (h->root.type == bfd_link_hash_indirect)
2129 return TRUE;
2130
7686d77d
AM
2131 /* Ignore this if we won't export it. */
2132 if (!eif->info->export_dynamic && !h->dynamic)
2133 return TRUE;
45d6a902
AM
2134
2135 if (h->dynindx == -1
fd91d419
L
2136 && (h->def_regular || h->ref_regular)
2137 && ! bfd_hide_sym_by_version (eif->info->version_info,
2138 h->root.root.string))
45d6a902 2139 {
fd91d419 2140 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2141 {
fd91d419
L
2142 eif->failed = TRUE;
2143 return FALSE;
45d6a902
AM
2144 }
2145 }
2146
2147 return TRUE;
2148}
2149\f
2150/* Look through the symbols which are defined in other shared
2151 libraries and referenced here. Update the list of version
2152 dependencies. This will be put into the .gnu.version_r section.
2153 This function is called via elf_link_hash_traverse. */
2154
28caa186 2155static bfd_boolean
268b6b39
AM
2156_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2157 void *data)
45d6a902 2158{
a50b1753 2159 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2160 Elf_Internal_Verneed *t;
2161 Elf_Internal_Vernaux *a;
2162 bfd_size_type amt;
2163
45d6a902
AM
2164 /* We only care about symbols defined in shared objects with version
2165 information. */
f5385ebf
AM
2166 if (!h->def_dynamic
2167 || h->def_regular
45d6a902 2168 || h->dynindx == -1
7b20f099
AM
2169 || h->verinfo.verdef == NULL
2170 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2171 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2172 return TRUE;
2173
2174 /* See if we already know about this version. */
28caa186
AM
2175 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2176 t != NULL;
2177 t = t->vn_nextref)
45d6a902
AM
2178 {
2179 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2180 continue;
2181
2182 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2183 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2184 return TRUE;
2185
2186 break;
2187 }
2188
2189 /* This is a new version. Add it to tree we are building. */
2190
2191 if (t == NULL)
2192 {
2193 amt = sizeof *t;
a50b1753 2194 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2195 if (t == NULL)
2196 {
2197 rinfo->failed = TRUE;
2198 return FALSE;
2199 }
2200
2201 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2202 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2203 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2204 }
2205
2206 amt = sizeof *a;
a50b1753 2207 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2208 if (a == NULL)
2209 {
2210 rinfo->failed = TRUE;
2211 return FALSE;
2212 }
45d6a902
AM
2213
2214 /* Note that we are copying a string pointer here, and testing it
2215 above. If bfd_elf_string_from_elf_section is ever changed to
2216 discard the string data when low in memory, this will have to be
2217 fixed. */
2218 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2219
2220 a->vna_flags = h->verinfo.verdef->vd_flags;
2221 a->vna_nextptr = t->vn_auxptr;
2222
2223 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2224 ++rinfo->vers;
2225
2226 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2227
2228 t->vn_auxptr = a;
2229
2230 return TRUE;
2231}
2232
099bb8fb
L
2233/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2234 hidden. Set *T_P to NULL if there is no match. */
2235
2236static bfd_boolean
2237_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2238 struct elf_link_hash_entry *h,
2239 const char *version_p,
2240 struct bfd_elf_version_tree **t_p,
2241 bfd_boolean *hide)
2242{
2243 struct bfd_elf_version_tree *t;
2244
2245 /* Look for the version. If we find it, it is no longer weak. */
2246 for (t = info->version_info; t != NULL; t = t->next)
2247 {
2248 if (strcmp (t->name, version_p) == 0)
2249 {
2250 size_t len;
2251 char *alc;
2252 struct bfd_elf_version_expr *d;
2253
2254 len = version_p - h->root.root.string;
2255 alc = (char *) bfd_malloc (len);
2256 if (alc == NULL)
2257 return FALSE;
2258 memcpy (alc, h->root.root.string, len - 1);
2259 alc[len - 1] = '\0';
2260 if (alc[len - 2] == ELF_VER_CHR)
2261 alc[len - 2] = '\0';
2262
2263 h->verinfo.vertree = t;
2264 t->used = TRUE;
2265 d = NULL;
2266
2267 if (t->globals.list != NULL)
2268 d = (*t->match) (&t->globals, NULL, alc);
2269
2270 /* See if there is anything to force this symbol to
2271 local scope. */
2272 if (d == NULL && t->locals.list != NULL)
2273 {
2274 d = (*t->match) (&t->locals, NULL, alc);
2275 if (d != NULL
2276 && h->dynindx != -1
2277 && ! info->export_dynamic)
2278 *hide = TRUE;
2279 }
2280
2281 free (alc);
2282 break;
2283 }
2284 }
2285
2286 *t_p = t;
2287
2288 return TRUE;
2289}
2290
2291/* Return TRUE if the symbol H is hidden by version script. */
2292
2293bfd_boolean
2294_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2295 struct elf_link_hash_entry *h)
2296{
2297 const char *p;
2298 bfd_boolean hide = FALSE;
2299 const struct elf_backend_data *bed
2300 = get_elf_backend_data (info->output_bfd);
2301
2302 /* Version script only hides symbols defined in regular objects. */
2303 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2304 return TRUE;
2305
2306 p = strchr (h->root.root.string, ELF_VER_CHR);
2307 if (p != NULL && h->verinfo.vertree == NULL)
2308 {
2309 struct bfd_elf_version_tree *t;
2310
2311 ++p;
2312 if (*p == ELF_VER_CHR)
2313 ++p;
2314
2315 if (*p != '\0'
2316 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2317 && hide)
2318 {
2319 if (hide)
2320 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2321 return TRUE;
2322 }
2323 }
2324
2325 /* If we don't have a version for this symbol, see if we can find
2326 something. */
2327 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2328 {
2329 h->verinfo.vertree
2330 = bfd_find_version_for_sym (info->version_info,
2331 h->root.root.string, &hide);
2332 if (h->verinfo.vertree != NULL && hide)
2333 {
2334 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2335 return TRUE;
2336 }
2337 }
2338
2339 return FALSE;
2340}
2341
45d6a902
AM
2342/* Figure out appropriate versions for all the symbols. We may not
2343 have the version number script until we have read all of the input
2344 files, so until that point we don't know which symbols should be
2345 local. This function is called via elf_link_hash_traverse. */
2346
28caa186 2347static bfd_boolean
268b6b39 2348_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2349{
28caa186 2350 struct elf_info_failed *sinfo;
45d6a902 2351 struct bfd_link_info *info;
9c5bfbb7 2352 const struct elf_backend_data *bed;
45d6a902
AM
2353 struct elf_info_failed eif;
2354 char *p;
099bb8fb 2355 bfd_boolean hide;
45d6a902 2356
a50b1753 2357 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2358 info = sinfo->info;
2359
45d6a902
AM
2360 /* Fix the symbol flags. */
2361 eif.failed = FALSE;
2362 eif.info = info;
2363 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2364 {
2365 if (eif.failed)
2366 sinfo->failed = TRUE;
2367 return FALSE;
2368 }
2369
0a640d71
L
2370 bed = get_elf_backend_data (info->output_bfd);
2371
45d6a902
AM
2372 /* We only need version numbers for symbols defined in regular
2373 objects. */
f5385ebf 2374 if (!h->def_regular)
0a640d71
L
2375 {
2376 /* Hide symbols defined in discarded input sections. */
2377 if ((h->root.type == bfd_link_hash_defined
2378 || h->root.type == bfd_link_hash_defweak)
2379 && discarded_section (h->root.u.def.section))
2380 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2381 return TRUE;
2382 }
45d6a902 2383
099bb8fb 2384 hide = FALSE;
45d6a902
AM
2385 p = strchr (h->root.root.string, ELF_VER_CHR);
2386 if (p != NULL && h->verinfo.vertree == NULL)
2387 {
2388 struct bfd_elf_version_tree *t;
45d6a902 2389
45d6a902
AM
2390 ++p;
2391 if (*p == ELF_VER_CHR)
6e33951e 2392 ++p;
45d6a902
AM
2393
2394 /* If there is no version string, we can just return out. */
2395 if (*p == '\0')
6e33951e 2396 return TRUE;
45d6a902 2397
099bb8fb 2398 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2399 {
099bb8fb
L
2400 sinfo->failed = TRUE;
2401 return FALSE;
45d6a902
AM
2402 }
2403
099bb8fb
L
2404 if (hide)
2405 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2406
45d6a902
AM
2407 /* If we are building an application, we need to create a
2408 version node for this version. */
0e1862bb 2409 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2410 {
2411 struct bfd_elf_version_tree **pp;
2412 int version_index;
2413
2414 /* If we aren't going to export this symbol, we don't need
2415 to worry about it. */
2416 if (h->dynindx == -1)
2417 return TRUE;
2418
ef53be89
AM
2419 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2420 sizeof *t);
45d6a902
AM
2421 if (t == NULL)
2422 {
2423 sinfo->failed = TRUE;
2424 return FALSE;
2425 }
2426
45d6a902 2427 t->name = p;
45d6a902
AM
2428 t->name_indx = (unsigned int) -1;
2429 t->used = TRUE;
2430
2431 version_index = 1;
2432 /* Don't count anonymous version tag. */
fd91d419
L
2433 if (sinfo->info->version_info != NULL
2434 && sinfo->info->version_info->vernum == 0)
45d6a902 2435 version_index = 0;
fd91d419
L
2436 for (pp = &sinfo->info->version_info;
2437 *pp != NULL;
2438 pp = &(*pp)->next)
45d6a902
AM
2439 ++version_index;
2440 t->vernum = version_index;
2441
2442 *pp = t;
2443
2444 h->verinfo.vertree = t;
2445 }
2446 else if (t == NULL)
2447 {
2448 /* We could not find the version for a symbol when
2449 generating a shared archive. Return an error. */
4eca0228 2450 _bfd_error_handler
695344c0 2451 /* xgettext:c-format */
871b3ab2 2452 (_("%pB: version node not found for symbol %s"),
28caa186 2453 info->output_bfd, h->root.root.string);
45d6a902
AM
2454 bfd_set_error (bfd_error_bad_value);
2455 sinfo->failed = TRUE;
2456 return FALSE;
2457 }
45d6a902
AM
2458 }
2459
2460 /* If we don't have a version for this symbol, see if we can find
2461 something. */
099bb8fb
L
2462 if (!hide
2463 && h->verinfo.vertree == NULL
2464 && sinfo->info->version_info != NULL)
45d6a902 2465 {
fd91d419
L
2466 h->verinfo.vertree
2467 = bfd_find_version_for_sym (sinfo->info->version_info,
2468 h->root.root.string, &hide);
1e8fa21e
AM
2469 if (h->verinfo.vertree != NULL && hide)
2470 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2471 }
2472
2473 return TRUE;
2474}
2475\f
45d6a902
AM
2476/* Read and swap the relocs from the section indicated by SHDR. This
2477 may be either a REL or a RELA section. The relocations are
2478 translated into RELA relocations and stored in INTERNAL_RELOCS,
2479 which should have already been allocated to contain enough space.
2480 The EXTERNAL_RELOCS are a buffer where the external form of the
2481 relocations should be stored.
2482
2483 Returns FALSE if something goes wrong. */
2484
2485static bfd_boolean
268b6b39 2486elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2487 asection *sec,
268b6b39
AM
2488 Elf_Internal_Shdr *shdr,
2489 void *external_relocs,
2490 Elf_Internal_Rela *internal_relocs)
45d6a902 2491{
9c5bfbb7 2492 const struct elf_backend_data *bed;
268b6b39 2493 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2494 const bfd_byte *erela;
2495 const bfd_byte *erelaend;
2496 Elf_Internal_Rela *irela;
243ef1e0
L
2497 Elf_Internal_Shdr *symtab_hdr;
2498 size_t nsyms;
45d6a902 2499
45d6a902
AM
2500 /* Position ourselves at the start of the section. */
2501 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2502 return FALSE;
2503
2504 /* Read the relocations. */
2505 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2506 return FALSE;
2507
243ef1e0 2508 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2509 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2510
45d6a902
AM
2511 bed = get_elf_backend_data (abfd);
2512
2513 /* Convert the external relocations to the internal format. */
2514 if (shdr->sh_entsize == bed->s->sizeof_rel)
2515 swap_in = bed->s->swap_reloc_in;
2516 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2517 swap_in = bed->s->swap_reloca_in;
2518 else
2519 {
2520 bfd_set_error (bfd_error_wrong_format);
2521 return FALSE;
2522 }
2523
a50b1753 2524 erela = (const bfd_byte *) external_relocs;
51992aec 2525 erelaend = erela + shdr->sh_size;
45d6a902
AM
2526 irela = internal_relocs;
2527 while (erela < erelaend)
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
2921 _bfd_elf_adjust_dynamic_symbol, below. */
60d67dc8
AM
2922 if (def->def_regular)
2923 {
2924 h = def;
2925 while ((h = h->u.alias) != def)
2926 h->is_weakalias = 0;
2927 }
45d6a902 2928 else
a26587ba 2929 {
4e6b54a6
AM
2930 while (h->root.type == bfd_link_hash_indirect)
2931 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2932 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2933 || h->root.type == bfd_link_hash_defweak);
60d67dc8
AM
2934 BFD_ASSERT (def->def_dynamic);
2935 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2936 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2937 }
45d6a902
AM
2938 }
2939
2940 return TRUE;
2941}
2942
2943/* Make the backend pick a good value for a dynamic symbol. This is
2944 called via elf_link_hash_traverse, and also calls itself
2945 recursively. */
2946
28caa186 2947static bfd_boolean
268b6b39 2948_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2949{
a50b1753 2950 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2951 struct elf_link_hash_table *htab;
9c5bfbb7 2952 const struct elf_backend_data *bed;
45d6a902 2953
0eddce27 2954 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2955 return FALSE;
2956
45d6a902
AM
2957 /* Ignore indirect symbols. These are added by the versioning code. */
2958 if (h->root.type == bfd_link_hash_indirect)
2959 return TRUE;
2960
2961 /* Fix the symbol flags. */
2962 if (! _bfd_elf_fix_symbol_flags (h, eif))
2963 return FALSE;
2964
559192d8
AM
2965 htab = elf_hash_table (eif->info);
2966 bed = get_elf_backend_data (htab->dynobj);
2967
954b63d4
AM
2968 if (h->root.type == bfd_link_hash_undefweak)
2969 {
2970 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2971 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2972 else if (eif->info->dynamic_undefined_weak > 0
2973 && h->ref_regular
2974 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2975 && !bfd_hide_sym_by_version (eif->info->version_info,
2976 h->root.root.string))
2977 {
2978 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2979 {
2980 eif->failed = TRUE;
2981 return FALSE;
2982 }
2983 }
2984 }
2985
45d6a902
AM
2986 /* If this symbol does not require a PLT entry, and it is not
2987 defined by a dynamic object, or is not referenced by a regular
2988 object, ignore it. We do have to handle a weak defined symbol,
2989 even if no regular object refers to it, if we decided to add it
2990 to the dynamic symbol table. FIXME: Do we normally need to worry
2991 about symbols which are defined by one dynamic object and
2992 referenced by another one? */
f5385ebf 2993 if (!h->needs_plt
91e21fb7 2994 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2995 && (h->def_regular
2996 || !h->def_dynamic
2997 || (!h->ref_regular
60d67dc8 2998 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 2999 {
a6aa5195 3000 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3001 return TRUE;
3002 }
3003
3004 /* If we've already adjusted this symbol, don't do it again. This
3005 can happen via a recursive call. */
f5385ebf 3006 if (h->dynamic_adjusted)
45d6a902
AM
3007 return TRUE;
3008
3009 /* Don't look at this symbol again. Note that we must set this
3010 after checking the above conditions, because we may look at a
3011 symbol once, decide not to do anything, and then get called
3012 recursively later after REF_REGULAR is set below. */
f5385ebf 3013 h->dynamic_adjusted = 1;
45d6a902
AM
3014
3015 /* If this is a weak definition, and we know a real definition, and
3016 the real symbol is not itself defined by a regular object file,
3017 then get a good value for the real definition. We handle the
3018 real symbol first, for the convenience of the backend routine.
3019
3020 Note that there is a confusing case here. If the real definition
3021 is defined by a regular object file, we don't get the real symbol
3022 from the dynamic object, but we do get the weak symbol. If the
3023 processor backend uses a COPY reloc, then if some routine in the
3024 dynamic object changes the real symbol, we will not see that
3025 change in the corresponding weak symbol. This is the way other
3026 ELF linkers work as well, and seems to be a result of the shared
3027 library model.
3028
3029 I will clarify this issue. Most SVR4 shared libraries define the
3030 variable _timezone and define timezone as a weak synonym. The
3031 tzset call changes _timezone. If you write
3032 extern int timezone;
3033 int _timezone = 5;
3034 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3035 you might expect that, since timezone is a synonym for _timezone,
3036 the same number will print both times. However, if the processor
3037 backend uses a COPY reloc, then actually timezone will be copied
3038 into your process image, and, since you define _timezone
3039 yourself, _timezone will not. Thus timezone and _timezone will
3040 wind up at different memory locations. The tzset call will set
3041 _timezone, leaving timezone unchanged. */
3042
60d67dc8 3043 if (h->is_weakalias)
45d6a902 3044 {
60d67dc8
AM
3045 struct elf_link_hash_entry *def = weakdef (h);
3046
ec24dc88 3047 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3048 the alias by a regular object file via the weak symbol H. */
3049 def->ref_regular = 1;
45d6a902 3050
ec24dc88 3051 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3052 the strong alias before H by recursively calling ourselves. */
3053 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3054 return FALSE;
3055 }
3056
3057 /* If a symbol has no type and no size and does not require a PLT
3058 entry, then we are probably about to do the wrong thing here: we
3059 are probably going to create a COPY reloc for an empty object.
3060 This case can arise when a shared object is built with assembly
3061 code, and the assembly code fails to set the symbol type. */
3062 if (h->size == 0
3063 && h->type == STT_NOTYPE
f5385ebf 3064 && !h->needs_plt)
4eca0228 3065 _bfd_error_handler
45d6a902
AM
3066 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3067 h->root.root.string);
3068
45d6a902
AM
3069 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3070 {
3071 eif->failed = TRUE;
3072 return FALSE;
3073 }
3074
3075 return TRUE;
3076}
3077
027297b7
L
3078/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3079 DYNBSS. */
3080
3081bfd_boolean
6cabe1ea
AM
3082_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3083 struct elf_link_hash_entry *h,
027297b7
L
3084 asection *dynbss)
3085{
91ac5911 3086 unsigned int power_of_two;
027297b7
L
3087 bfd_vma mask;
3088 asection *sec = h->root.u.def.section;
3089
de194d85 3090 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3091 requirement of symbols defined in the section. Since we don't
3092 know the symbol alignment requirement, we start with the
3093 maximum alignment and check low bits of the symbol address
3094 for the minimum alignment. */
3095 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3096 mask = ((bfd_vma) 1 << power_of_two) - 1;
3097 while ((h->root.u.def.value & mask) != 0)
3098 {
3099 mask >>= 1;
3100 --power_of_two;
3101 }
027297b7 3102
91ac5911
L
3103 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3104 dynbss))
027297b7
L
3105 {
3106 /* Adjust the section alignment if needed. */
3107 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 3108 power_of_two))
027297b7
L
3109 return FALSE;
3110 }
3111
91ac5911 3112 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3113 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3114
3115 /* Define the symbol as being at this point in DYNBSS. */
3116 h->root.u.def.section = dynbss;
3117 h->root.u.def.value = dynbss->size;
3118
3119 /* Increment the size of DYNBSS to make room for the symbol. */
3120 dynbss->size += h->size;
3121
f7483970
L
3122 /* No error if extern_protected_data is true. */
3123 if (h->protected_def
889c2a67
L
3124 && (!info->extern_protected_data
3125 || (info->extern_protected_data < 0
3126 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3127 info->callbacks->einfo
c1c8c1ef 3128 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3129 h->root.root.string);
6cabe1ea 3130
027297b7
L
3131 return TRUE;
3132}
3133
45d6a902
AM
3134/* Adjust all external symbols pointing into SEC_MERGE sections
3135 to reflect the object merging within the sections. */
3136
28caa186 3137static bfd_boolean
268b6b39 3138_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3139{
3140 asection *sec;
3141
45d6a902
AM
3142 if ((h->root.type == bfd_link_hash_defined
3143 || h->root.type == bfd_link_hash_defweak)
3144 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3145 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3146 {
a50b1753 3147 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3148
3149 h->root.u.def.value =
3150 _bfd_merged_section_offset (output_bfd,
3151 &h->root.u.def.section,
3152 elf_section_data (sec)->sec_info,
753731ee 3153 h->root.u.def.value);
45d6a902
AM
3154 }
3155
3156 return TRUE;
3157}
986a241f
RH
3158
3159/* Returns false if the symbol referred to by H should be considered
3160 to resolve local to the current module, and true if it should be
3161 considered to bind dynamically. */
3162
3163bfd_boolean
268b6b39
AM
3164_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3165 struct bfd_link_info *info,
89a2ee5a 3166 bfd_boolean not_local_protected)
986a241f
RH
3167{
3168 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3169 const struct elf_backend_data *bed;
3170 struct elf_link_hash_table *hash_table;
986a241f
RH
3171
3172 if (h == NULL)
3173 return FALSE;
3174
3175 while (h->root.type == bfd_link_hash_indirect
3176 || h->root.type == bfd_link_hash_warning)
3177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3178
3179 /* If it was forced local, then clearly it's not dynamic. */
3180 if (h->dynindx == -1)
3181 return FALSE;
f5385ebf 3182 if (h->forced_local)
986a241f
RH
3183 return FALSE;
3184
3185 /* Identify the cases where name binding rules say that a
3186 visible symbol resolves locally. */
0e1862bb
L
3187 binding_stays_local_p = (bfd_link_executable (info)
3188 || SYMBOLIC_BIND (info, h));
986a241f
RH
3189
3190 switch (ELF_ST_VISIBILITY (h->other))
3191 {
3192 case STV_INTERNAL:
3193 case STV_HIDDEN:
3194 return FALSE;
3195
3196 case STV_PROTECTED:
fcb93ecf
PB
3197 hash_table = elf_hash_table (info);
3198 if (!is_elf_hash_table (hash_table))
3199 return FALSE;
3200
3201 bed = get_elf_backend_data (hash_table->dynobj);
3202
986a241f
RH
3203 /* Proper resolution for function pointer equality may require
3204 that these symbols perhaps be resolved dynamically, even though
3205 we should be resolving them to the current module. */
89a2ee5a 3206 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3207 binding_stays_local_p = TRUE;
3208 break;
3209
3210 default:
986a241f
RH
3211 break;
3212 }
3213
aa37626c 3214 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3215 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3216 return TRUE;
3217
986a241f
RH
3218 /* Otherwise, the symbol is dynamic if binding rules don't tell
3219 us that it remains local. */
3220 return !binding_stays_local_p;
3221}
f6c52c13
AM
3222
3223/* Return true if the symbol referred to by H should be considered
3224 to resolve local to the current module, and false otherwise. Differs
3225 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3226 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3227 for the place where dynindx == -1 is tested. If that test is true,
3228 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3229 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3230 defined symbols.
89a2ee5a
AM
3231 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3232 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3233 treatment of undefined weak symbols. For those that do not make
3234 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3235
3236bfd_boolean
268b6b39
AM
3237_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3238 struct bfd_link_info *info,
3239 bfd_boolean local_protected)
f6c52c13 3240{
fcb93ecf
PB
3241 const struct elf_backend_data *bed;
3242 struct elf_link_hash_table *hash_table;
3243
f6c52c13
AM
3244 /* If it's a local sym, of course we resolve locally. */
3245 if (h == NULL)
3246 return TRUE;
3247
d95edcac
L
3248 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3249 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3250 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3251 return TRUE;
3252
0fad2956
MR
3253 /* Forced local symbols resolve locally. */
3254 if (h->forced_local)
3255 return TRUE;
3256
7e2294f9
AO
3257 /* Common symbols that become definitions don't get the DEF_REGULAR
3258 flag set, so test it first, and don't bail out. */
3259 if (ELF_COMMON_DEF_P (h))
3260 /* Do nothing. */;
f6c52c13 3261 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3262 resolve locally. The sym is either undefined or dynamic. */
3263 else if (!h->def_regular)
f6c52c13
AM
3264 return FALSE;
3265
0fad2956 3266 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3267 if (h->dynindx == -1)
3268 return TRUE;
3269
3270 /* At this point, we know the symbol is defined and dynamic. In an
3271 executable it must resolve locally, likewise when building symbolic
3272 shared libraries. */
0e1862bb 3273 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3274 return TRUE;
3275
3276 /* Now deal with defined dynamic symbols in shared libraries. Ones
3277 with default visibility might not resolve locally. */
3278 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3279 return FALSE;
3280
fcb93ecf
PB
3281 hash_table = elf_hash_table (info);
3282 if (!is_elf_hash_table (hash_table))
3283 return TRUE;
3284
3285 bed = get_elf_backend_data (hash_table->dynobj);
3286
f7483970
L
3287 /* If extern_protected_data is false, STV_PROTECTED non-function
3288 symbols are local. */
889c2a67
L
3289 if ((!info->extern_protected_data
3290 || (info->extern_protected_data < 0
3291 && !bed->extern_protected_data))
3292 && !bed->is_function_type (h->type))
1c16dfa5
L
3293 return TRUE;
3294
f6c52c13 3295 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3296 symbols be treated as dynamic symbols. If the address of a
3297 function not defined in an executable is set to that function's
3298 plt entry in the executable, then the address of the function in
3299 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3300 return local_protected;
3301}
e1918d23
AM
3302
3303/* Caches some TLS segment info, and ensures that the TLS segment vma is
3304 aligned. Returns the first TLS output section. */
3305
3306struct bfd_section *
3307_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3308{
3309 struct bfd_section *sec, *tls;
3310 unsigned int align = 0;
3311
3312 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3313 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3314 break;
3315 tls = sec;
3316
3317 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3318 if (sec->alignment_power > align)
3319 align = sec->alignment_power;
3320
3321 elf_hash_table (info)->tls_sec = tls;
3322
3323 /* Ensure the alignment of the first section is the largest alignment,
3324 so that the tls segment starts aligned. */
3325 if (tls != NULL)
3326 tls->alignment_power = align;
3327
3328 return tls;
3329}
0ad989f9
L
3330
3331/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3332static bfd_boolean
3333is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3334 Elf_Internal_Sym *sym)
3335{
a4d8e49b
L
3336 const struct elf_backend_data *bed;
3337
0ad989f9
L
3338 /* Local symbols do not count, but target specific ones might. */
3339 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3340 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3341 return FALSE;
3342
fcb93ecf 3343 bed = get_elf_backend_data (abfd);
0ad989f9 3344 /* Function symbols do not count. */
fcb93ecf 3345 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3346 return FALSE;
3347
3348 /* If the section is undefined, then so is the symbol. */
3349 if (sym->st_shndx == SHN_UNDEF)
3350 return FALSE;
3351
3352 /* If the symbol is defined in the common section, then
3353 it is a common definition and so does not count. */
a4d8e49b 3354 if (bed->common_definition (sym))
0ad989f9
L
3355 return FALSE;
3356
3357 /* If the symbol is in a target specific section then we
3358 must rely upon the backend to tell us what it is. */
3359 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3360 /* FIXME - this function is not coded yet:
3361
3362 return _bfd_is_global_symbol_definition (abfd, sym);
3363
3364 Instead for now assume that the definition is not global,
3365 Even if this is wrong, at least the linker will behave
3366 in the same way that it used to do. */
3367 return FALSE;
3368
3369 return TRUE;
3370}
3371
3372/* Search the symbol table of the archive element of the archive ABFD
3373 whose archive map contains a mention of SYMDEF, and determine if
3374 the symbol is defined in this element. */
3375static bfd_boolean
3376elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3377{
3378 Elf_Internal_Shdr * hdr;
ef53be89
AM
3379 size_t symcount;
3380 size_t extsymcount;
3381 size_t extsymoff;
0ad989f9
L
3382 Elf_Internal_Sym *isymbuf;
3383 Elf_Internal_Sym *isym;
3384 Elf_Internal_Sym *isymend;
3385 bfd_boolean result;
3386
3387 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3388 if (abfd == NULL)
3389 return FALSE;
3390
3391 if (! bfd_check_format (abfd, bfd_object))
3392 return FALSE;
3393
7dc3990e
L
3394 /* Select the appropriate symbol table. If we don't know if the
3395 object file is an IR object, give linker LTO plugin a chance to
3396 get the correct symbol table. */
3397 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3398#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3399 || (abfd->plugin_format == bfd_plugin_unknown
3400 && bfd_link_plugin_object_p (abfd))
3401#endif
3402 )
3403 {
3404 /* Use the IR symbol table if the object has been claimed by
3405 plugin. */
3406 abfd = abfd->plugin_dummy_bfd;
3407 hdr = &elf_tdata (abfd)->symtab_hdr;
3408 }
3409 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 else
3412 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3413
3414 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3415
3416 /* The sh_info field of the symtab header tells us where the
3417 external symbols start. We don't care about the local symbols. */
3418 if (elf_bad_symtab (abfd))
3419 {
3420 extsymcount = symcount;
3421 extsymoff = 0;
3422 }
3423 else
3424 {
3425 extsymcount = symcount - hdr->sh_info;
3426 extsymoff = hdr->sh_info;
3427 }
3428
3429 if (extsymcount == 0)
3430 return FALSE;
3431
3432 /* Read in the symbol table. */
3433 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3434 NULL, NULL, NULL);
3435 if (isymbuf == NULL)
3436 return FALSE;
3437
3438 /* Scan the symbol table looking for SYMDEF. */
3439 result = FALSE;
3440 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3441 {
3442 const char *name;
3443
3444 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3445 isym->st_name);
3446 if (name == NULL)
3447 break;
3448
3449 if (strcmp (name, symdef->name) == 0)
3450 {
3451 result = is_global_data_symbol_definition (abfd, isym);
3452 break;
3453 }
3454 }
3455
3456 free (isymbuf);
3457
3458 return result;
3459}
3460\f
5a580b3a
AM
3461/* Add an entry to the .dynamic table. */
3462
3463bfd_boolean
3464_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3465 bfd_vma tag,
3466 bfd_vma val)
3467{
3468 struct elf_link_hash_table *hash_table;
3469 const struct elf_backend_data *bed;
3470 asection *s;
3471 bfd_size_type newsize;
3472 bfd_byte *newcontents;
3473 Elf_Internal_Dyn dyn;
3474
3475 hash_table = elf_hash_table (info);
3476 if (! is_elf_hash_table (hash_table))
3477 return FALSE;
3478
7f923b7f
AM
3479 if (tag == DT_RELA || tag == DT_REL)
3480 hash_table->dynamic_relocs = TRUE;
3481
5a580b3a 3482 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3483 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3484 BFD_ASSERT (s != NULL);
3485
eea6121a 3486 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3487 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3488 if (newcontents == NULL)
3489 return FALSE;
3490
3491 dyn.d_tag = tag;
3492 dyn.d_un.d_val = val;
eea6121a 3493 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3494
eea6121a 3495 s->size = newsize;
5a580b3a
AM
3496 s->contents = newcontents;
3497
3498 return TRUE;
3499}
3500
3501/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3502 otherwise just check whether one already exists. Returns -1 on error,
3503 1 if a DT_NEEDED tag already exists, and 0 on success. */
3504
4ad4eba5 3505static int
7e9f0867
AM
3506elf_add_dt_needed_tag (bfd *abfd,
3507 struct bfd_link_info *info,
4ad4eba5
AM
3508 const char *soname,
3509 bfd_boolean do_it)
5a580b3a
AM
3510{
3511 struct elf_link_hash_table *hash_table;
ef53be89 3512 size_t strindex;
5a580b3a 3513
7e9f0867
AM
3514 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3515 return -1;
3516
5a580b3a 3517 hash_table = elf_hash_table (info);
5a580b3a 3518 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3519 if (strindex == (size_t) -1)
5a580b3a
AM
3520 return -1;
3521
02be4619 3522 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3523 {
3524 asection *sdyn;
3525 const struct elf_backend_data *bed;
3526 bfd_byte *extdyn;
3527
3528 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3529 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3530 if (sdyn != NULL)
3531 for (extdyn = sdyn->contents;
3532 extdyn < sdyn->contents + sdyn->size;
3533 extdyn += bed->s->sizeof_dyn)
3534 {
3535 Elf_Internal_Dyn dyn;
5a580b3a 3536
7e9f0867
AM
3537 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3538 if (dyn.d_tag == DT_NEEDED
3539 && dyn.d_un.d_val == strindex)
3540 {
3541 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3542 return 1;
3543 }
3544 }
5a580b3a
AM
3545 }
3546
3547 if (do_it)
3548 {
7e9f0867
AM
3549 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3550 return -1;
3551
5a580b3a
AM
3552 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3553 return -1;
3554 }
3555 else
3556 /* We were just checking for existence of the tag. */
3557 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3558
3559 return 0;
3560}
3561
7b15fa7a
AM
3562/* Return true if SONAME is on the needed list between NEEDED and STOP
3563 (or the end of list if STOP is NULL), and needed by a library that
3564 will be loaded. */
3565
010e5ae2 3566static bfd_boolean
7b15fa7a
AM
3567on_needed_list (const char *soname,
3568 struct bfd_link_needed_list *needed,
3569 struct bfd_link_needed_list *stop)
010e5ae2 3570{
7b15fa7a
AM
3571 struct bfd_link_needed_list *look;
3572 for (look = needed; look != stop; look = look->next)
3573 if (strcmp (soname, look->name) == 0
3574 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3575 /* If needed by a library that itself is not directly
3576 needed, recursively check whether that library is
3577 indirectly needed. Since we add DT_NEEDED entries to
3578 the end of the list, library dependencies appear after
3579 the library. Therefore search prior to the current
3580 LOOK, preventing possible infinite recursion. */
3581 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3582 return TRUE;
3583
3584 return FALSE;
3585}
3586
14160578 3587/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3588static int
3589elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3590{
3591 const struct elf_link_hash_entry *h1;
3592 const struct elf_link_hash_entry *h2;
10b7e05b 3593 bfd_signed_vma vdiff;
5a580b3a
AM
3594
3595 h1 = *(const struct elf_link_hash_entry **) arg1;
3596 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3597 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3598 if (vdiff != 0)
3599 return vdiff > 0 ? 1 : -1;
3600 else
3601 {
d3435ae8 3602 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3603 if (sdiff != 0)
3604 return sdiff > 0 ? 1 : -1;
3605 }
14160578
AM
3606 vdiff = h1->size - h2->size;
3607 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3608}
4ad4eba5 3609
5a580b3a
AM
3610/* This function is used to adjust offsets into .dynstr for
3611 dynamic symbols. This is called via elf_link_hash_traverse. */
3612
3613static bfd_boolean
3614elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3615{
a50b1753 3616 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3617
5a580b3a
AM
3618 if (h->dynindx != -1)
3619 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3620 return TRUE;
3621}
3622
3623/* Assign string offsets in .dynstr, update all structures referencing
3624 them. */
3625
4ad4eba5
AM
3626static bfd_boolean
3627elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3628{
3629 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3630 struct elf_link_local_dynamic_entry *entry;
3631 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3632 bfd *dynobj = hash_table->dynobj;
3633 asection *sdyn;
3634 bfd_size_type size;
3635 const struct elf_backend_data *bed;
3636 bfd_byte *extdyn;
3637
3638 _bfd_elf_strtab_finalize (dynstr);
3639 size = _bfd_elf_strtab_size (dynstr);
3640
3641 bed = get_elf_backend_data (dynobj);
3d4d4302 3642 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3643 BFD_ASSERT (sdyn != NULL);
3644
3645 /* Update all .dynamic entries referencing .dynstr strings. */
3646 for (extdyn = sdyn->contents;
eea6121a 3647 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3648 extdyn += bed->s->sizeof_dyn)
3649 {
3650 Elf_Internal_Dyn dyn;
3651
3652 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3653 switch (dyn.d_tag)
3654 {
3655 case DT_STRSZ:
3656 dyn.d_un.d_val = size;
3657 break;
3658 case DT_NEEDED:
3659 case DT_SONAME:
3660 case DT_RPATH:
3661 case DT_RUNPATH:
3662 case DT_FILTER:
3663 case DT_AUXILIARY:
7ee314fa
AM
3664 case DT_AUDIT:
3665 case DT_DEPAUDIT:
5a580b3a
AM
3666 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3667 break;
3668 default:
3669 continue;
3670 }
3671 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3672 }
3673
3674 /* Now update local dynamic symbols. */
3675 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3676 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3677 entry->isym.st_name);
3678
3679 /* And the rest of dynamic symbols. */
3680 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3681
3682 /* Adjust version definitions. */
3683 if (elf_tdata (output_bfd)->cverdefs)
3684 {
3685 asection *s;
3686 bfd_byte *p;
ef53be89 3687 size_t i;
5a580b3a
AM
3688 Elf_Internal_Verdef def;
3689 Elf_Internal_Verdaux defaux;
3690
3d4d4302 3691 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3692 p = s->contents;
3693 do
3694 {
3695 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3696 &def);
3697 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3698 if (def.vd_aux != sizeof (Elf_External_Verdef))
3699 continue;
5a580b3a
AM
3700 for (i = 0; i < def.vd_cnt; ++i)
3701 {
3702 _bfd_elf_swap_verdaux_in (output_bfd,
3703 (Elf_External_Verdaux *) p, &defaux);
3704 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3705 defaux.vda_name);
3706 _bfd_elf_swap_verdaux_out (output_bfd,
3707 &defaux, (Elf_External_Verdaux *) p);
3708 p += sizeof (Elf_External_Verdaux);
3709 }
3710 }
3711 while (def.vd_next);
3712 }
3713
3714 /* Adjust version references. */
3715 if (elf_tdata (output_bfd)->verref)
3716 {
3717 asection *s;
3718 bfd_byte *p;
ef53be89 3719 size_t i;
5a580b3a
AM
3720 Elf_Internal_Verneed need;
3721 Elf_Internal_Vernaux needaux;
3722
3d4d4302 3723 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3724 p = s->contents;
3725 do
3726 {
3727 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3728 &need);
3729 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3730 _bfd_elf_swap_verneed_out (output_bfd, &need,
3731 (Elf_External_Verneed *) p);
3732 p += sizeof (Elf_External_Verneed);
3733 for (i = 0; i < need.vn_cnt; ++i)
3734 {
3735 _bfd_elf_swap_vernaux_in (output_bfd,
3736 (Elf_External_Vernaux *) p, &needaux);
3737 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3738 needaux.vna_name);
3739 _bfd_elf_swap_vernaux_out (output_bfd,
3740 &needaux,
3741 (Elf_External_Vernaux *) p);
3742 p += sizeof (Elf_External_Vernaux);
3743 }
3744 }
3745 while (need.vn_next);
3746 }
3747
3748 return TRUE;
3749}
3750\f
13285a1b
AM
3751/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3752 The default is to only match when the INPUT and OUTPUT are exactly
3753 the same target. */
3754
3755bfd_boolean
3756_bfd_elf_default_relocs_compatible (const bfd_target *input,
3757 const bfd_target *output)
3758{
3759 return input == output;
3760}
3761
3762/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3763 This version is used when different targets for the same architecture
3764 are virtually identical. */
3765
3766bfd_boolean
3767_bfd_elf_relocs_compatible (const bfd_target *input,
3768 const bfd_target *output)
3769{
3770 const struct elf_backend_data *obed, *ibed;
3771
3772 if (input == output)
3773 return TRUE;
3774
3775 ibed = xvec_get_elf_backend_data (input);
3776 obed = xvec_get_elf_backend_data (output);
3777
3778 if (ibed->arch != obed->arch)
3779 return FALSE;
3780
3781 /* If both backends are using this function, deem them compatible. */
3782 return ibed->relocs_compatible == obed->relocs_compatible;
3783}
3784
e5034e59
AM
3785/* Make a special call to the linker "notice" function to tell it that
3786 we are about to handle an as-needed lib, or have finished
1b786873 3787 processing the lib. */
e5034e59
AM
3788
3789bfd_boolean
3790_bfd_elf_notice_as_needed (bfd *ibfd,
3791 struct bfd_link_info *info,
3792 enum notice_asneeded_action act)
3793{
46135103 3794 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3795}
3796
d9689752
L
3797/* Check relocations an ELF object file. */
3798
3799bfd_boolean
3800_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3801{
3802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3803 struct elf_link_hash_table *htab = elf_hash_table (info);
3804
3805 /* If this object is the same format as the output object, and it is
3806 not a shared library, then let the backend look through the
3807 relocs.
3808
3809 This is required to build global offset table entries and to
3810 arrange for dynamic relocs. It is not required for the
3811 particular common case of linking non PIC code, even when linking
3812 against shared libraries, but unfortunately there is no way of
3813 knowing whether an object file has been compiled PIC or not.
3814 Looking through the relocs is not particularly time consuming.
3815 The problem is that we must either (1) keep the relocs in memory,
3816 which causes the linker to require additional runtime memory or
3817 (2) read the relocs twice from the input file, which wastes time.
3818 This would be a good case for using mmap.
3819
3820 I have no idea how to handle linking PIC code into a file of a
3821 different format. It probably can't be done. */
3822 if ((abfd->flags & DYNAMIC) == 0
3823 && is_elf_hash_table (htab)
3824 && bed->check_relocs != NULL
3825 && elf_object_id (abfd) == elf_hash_table_id (htab)
3826 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3827 {
3828 asection *o;
3829
3830 for (o = abfd->sections; o != NULL; o = o->next)
3831 {
3832 Elf_Internal_Rela *internal_relocs;
3833 bfd_boolean ok;
3834
5ce03cea 3835 /* Don't check relocations in excluded sections. */
d9689752 3836 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3837 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3838 || o->reloc_count == 0
3839 || ((info->strip == strip_all || info->strip == strip_debugger)
3840 && (o->flags & SEC_DEBUGGING) != 0)
3841 || bfd_is_abs_section (o->output_section))
3842 continue;
3843
3844 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3845 info->keep_memory);
3846 if (internal_relocs == NULL)
3847 return FALSE;
3848
3849 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3850
3851 if (elf_section_data (o)->relocs != internal_relocs)
3852 free (internal_relocs);
3853
3854 if (! ok)
3855 return FALSE;
3856 }
3857 }
3858
3859 return TRUE;
3860}
3861
4ad4eba5
AM
3862/* Add symbols from an ELF object file to the linker hash table. */
3863
3864static bfd_boolean
3865elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3866{
a0c402a5 3867 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3868 Elf_Internal_Shdr *hdr;
ef53be89
AM
3869 size_t symcount;
3870 size_t extsymcount;
3871 size_t extsymoff;
4ad4eba5
AM
3872 struct elf_link_hash_entry **sym_hash;
3873 bfd_boolean dynamic;
3874 Elf_External_Versym *extversym = NULL;
be22c732 3875 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
3876 Elf_External_Versym *ever;
3877 struct elf_link_hash_entry *weaks;
3878 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3879 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3880 Elf_Internal_Sym *isymbuf = NULL;
3881 Elf_Internal_Sym *isym;
3882 Elf_Internal_Sym *isymend;
3883 const struct elf_backend_data *bed;
3884 bfd_boolean add_needed;
66eb6687 3885 struct elf_link_hash_table *htab;
4ad4eba5 3886 bfd_size_type amt;
66eb6687 3887 void *alloc_mark = NULL;
4f87808c
AM
3888 struct bfd_hash_entry **old_table = NULL;
3889 unsigned int old_size = 0;
3890 unsigned int old_count = 0;
66eb6687 3891 void *old_tab = NULL;
66eb6687
AM
3892 void *old_ent;
3893 struct bfd_link_hash_entry *old_undefs = NULL;
3894 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3895 void *old_strtab = NULL;
66eb6687 3896 size_t tabsize = 0;
db6a5d5f 3897 asection *s;
29a9f53e 3898 bfd_boolean just_syms;
4ad4eba5 3899
66eb6687 3900 htab = elf_hash_table (info);
4ad4eba5 3901 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3902
3903 if ((abfd->flags & DYNAMIC) == 0)
3904 dynamic = FALSE;
3905 else
3906 {
3907 dynamic = TRUE;
3908
3909 /* You can't use -r against a dynamic object. Also, there's no
3910 hope of using a dynamic object which does not exactly match
3911 the format of the output file. */
0e1862bb 3912 if (bfd_link_relocatable (info)
66eb6687 3913 || !is_elf_hash_table (htab)
f13a99db 3914 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3915 {
0e1862bb 3916 if (bfd_link_relocatable (info))
9a0789ec
NC
3917 bfd_set_error (bfd_error_invalid_operation);
3918 else
3919 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3920 goto error_return;
3921 }
3922 }
3923
a0c402a5
L
3924 ehdr = elf_elfheader (abfd);
3925 if (info->warn_alternate_em
3926 && bed->elf_machine_code != ehdr->e_machine
3927 && ((bed->elf_machine_alt1 != 0
3928 && ehdr->e_machine == bed->elf_machine_alt1)
3929 || (bed->elf_machine_alt2 != 0
3930 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 3931 _bfd_error_handler
695344c0 3932 /* xgettext:c-format */
9793eb77 3933 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
3934 ehdr->e_machine, abfd, bed->elf_machine_code);
3935
4ad4eba5
AM
3936 /* As a GNU extension, any input sections which are named
3937 .gnu.warning.SYMBOL are treated as warning symbols for the given
3938 symbol. This differs from .gnu.warning sections, which generate
3939 warnings when they are included in an output file. */
dd98f8d2 3940 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3941 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3942 {
db6a5d5f 3943 const char *name;
4ad4eba5 3944
db6a5d5f
AM
3945 name = bfd_get_section_name (abfd, s);
3946 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3947 {
db6a5d5f
AM
3948 char *msg;
3949 bfd_size_type sz;
3950
3951 name += sizeof ".gnu.warning." - 1;
3952
3953 /* If this is a shared object, then look up the symbol
3954 in the hash table. If it is there, and it is already
3955 been defined, then we will not be using the entry
3956 from this shared object, so we don't need to warn.
3957 FIXME: If we see the definition in a regular object
3958 later on, we will warn, but we shouldn't. The only
3959 fix is to keep track of what warnings we are supposed
3960 to emit, and then handle them all at the end of the
3961 link. */
3962 if (dynamic)
4ad4eba5 3963 {
db6a5d5f
AM
3964 struct elf_link_hash_entry *h;
3965
3966 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3967
3968 /* FIXME: What about bfd_link_hash_common? */
3969 if (h != NULL
3970 && (h->root.type == bfd_link_hash_defined
3971 || h->root.type == bfd_link_hash_defweak))
3972 continue;
3973 }
4ad4eba5 3974
db6a5d5f
AM
3975 sz = s->size;
3976 msg = (char *) bfd_alloc (abfd, sz + 1);
3977 if (msg == NULL)
3978 goto error_return;
4ad4eba5 3979
db6a5d5f
AM
3980 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3981 goto error_return;
4ad4eba5 3982
db6a5d5f 3983 msg[sz] = '\0';
4ad4eba5 3984
db6a5d5f
AM
3985 if (! (_bfd_generic_link_add_one_symbol
3986 (info, abfd, name, BSF_WARNING, s, 0, msg,
3987 FALSE, bed->collect, NULL)))
3988 goto error_return;
4ad4eba5 3989
0e1862bb 3990 if (bfd_link_executable (info))
db6a5d5f
AM
3991 {
3992 /* Clobber the section size so that the warning does
3993 not get copied into the output file. */
3994 s->size = 0;
11d2f718 3995
db6a5d5f
AM
3996 /* Also set SEC_EXCLUDE, so that symbols defined in
3997 the warning section don't get copied to the output. */
3998 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3999 }
4000 }
4001 }
4002
29a9f53e
L
4003 just_syms = ((s = abfd->sections) != NULL
4004 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4005
4ad4eba5
AM
4006 add_needed = TRUE;
4007 if (! dynamic)
4008 {
4009 /* If we are creating a shared library, create all the dynamic
4010 sections immediately. We need to attach them to something,
4011 so we attach them to this BFD, provided it is the right
bf89386a
L
4012 format and is not from ld --just-symbols. Always create the
4013 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4014 are no input BFD's of the same format as the output, we can't
4015 make a shared library. */
4016 if (!just_syms
bf89386a 4017 && (bfd_link_pic (info)
9c1d7a08 4018 || (!bfd_link_relocatable (info)
3c5fce9b 4019 && info->nointerp
9c1d7a08 4020 && (info->export_dynamic || info->dynamic)))
66eb6687 4021 && is_elf_hash_table (htab)
f13a99db 4022 && info->output_bfd->xvec == abfd->xvec
66eb6687 4023 && !htab->dynamic_sections_created)
4ad4eba5
AM
4024 {
4025 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4026 goto error_return;
4027 }
4028 }
66eb6687 4029 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4030 goto error_return;
4031 else
4032 {
4ad4eba5 4033 const char *soname = NULL;
7ee314fa 4034 char *audit = NULL;
4ad4eba5 4035 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4036 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
4037 int ret;
4038
4039 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4040 ld shouldn't allow it. */
29a9f53e 4041 if (just_syms)
92fd189d 4042 abort ();
4ad4eba5
AM
4043
4044 /* If this dynamic lib was specified on the command line with
4045 --as-needed in effect, then we don't want to add a DT_NEEDED
4046 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4047 in by another lib's DT_NEEDED. When --no-add-needed is used
4048 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4049 any dynamic library in DT_NEEDED tags in the dynamic lib at
4050 all. */
4051 add_needed = (elf_dyn_lib_class (abfd)
4052 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4053 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4054
4055 s = bfd_get_section_by_name (abfd, ".dynamic");
4056 if (s != NULL)
4057 {
4058 bfd_byte *dynbuf;
4059 bfd_byte *extdyn;
cb33740c 4060 unsigned int elfsec;
4ad4eba5
AM
4061 unsigned long shlink;
4062
eea6121a 4063 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
4064 {
4065error_free_dyn:
4066 free (dynbuf);
4067 goto error_return;
4068 }
4ad4eba5
AM
4069
4070 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4071 if (elfsec == SHN_BAD)
4ad4eba5
AM
4072 goto error_free_dyn;
4073 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4074
4075 for (extdyn = dynbuf;
eea6121a 4076 extdyn < dynbuf + s->size;
4ad4eba5
AM
4077 extdyn += bed->s->sizeof_dyn)
4078 {
4079 Elf_Internal_Dyn dyn;
4080
4081 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4082 if (dyn.d_tag == DT_SONAME)
4083 {
4084 unsigned int tagv = dyn.d_un.d_val;
4085 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4086 if (soname == NULL)
4087 goto error_free_dyn;
4088 }
4089 if (dyn.d_tag == DT_NEEDED)
4090 {
4091 struct bfd_link_needed_list *n, **pn;
4092 char *fnm, *anm;
4093 unsigned int tagv = dyn.d_un.d_val;
4094
4095 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4096 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4097 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4098 if (n == NULL || fnm == NULL)
4099 goto error_free_dyn;
4100 amt = strlen (fnm) + 1;
a50b1753 4101 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4102 if (anm == NULL)
4103 goto error_free_dyn;
4104 memcpy (anm, fnm, amt);
4105 n->name = anm;
4106 n->by = abfd;
4107 n->next = NULL;
66eb6687 4108 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4109 ;
4110 *pn = n;
4111 }
4112 if (dyn.d_tag == DT_RUNPATH)
4113 {
4114 struct bfd_link_needed_list *n, **pn;
4115 char *fnm, *anm;
4116 unsigned int tagv = dyn.d_un.d_val;
4117
4118 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4119 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4120 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4121 if (n == NULL || fnm == NULL)
4122 goto error_free_dyn;
4123 amt = strlen (fnm) + 1;
a50b1753 4124 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4125 if (anm == NULL)
4126 goto error_free_dyn;
4127 memcpy (anm, fnm, amt);
4128 n->name = anm;
4129 n->by = abfd;
4130 n->next = NULL;
4131 for (pn = & runpath;
4132 *pn != NULL;
4133 pn = &(*pn)->next)
4134 ;
4135 *pn = n;
4136 }
4137 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4138 if (!runpath && dyn.d_tag == DT_RPATH)
4139 {
4140 struct bfd_link_needed_list *n, **pn;
4141 char *fnm, *anm;
4142 unsigned int tagv = dyn.d_un.d_val;
4143
4144 amt = sizeof (struct bfd_link_needed_list);
a50b1753 4145 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4146 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4147 if (n == NULL || fnm == NULL)
4148 goto error_free_dyn;
4149 amt = strlen (fnm) + 1;
a50b1753 4150 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4151 if (anm == NULL)
f8703194 4152 goto error_free_dyn;
4ad4eba5
AM
4153 memcpy (anm, fnm, amt);
4154 n->name = anm;
4155 n->by = abfd;
4156 n->next = NULL;
4157 for (pn = & rpath;
4158 *pn != NULL;
4159 pn = &(*pn)->next)
4160 ;
4161 *pn = n;
4162 }
7ee314fa
AM
4163 if (dyn.d_tag == DT_AUDIT)
4164 {
4165 unsigned int tagv = dyn.d_un.d_val;
4166 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4167 }
4ad4eba5
AM
4168 }
4169
4170 free (dynbuf);
4171 }
4172
4173 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4174 frees all more recently bfd_alloc'd blocks as well. */
4175 if (runpath)
4176 rpath = runpath;
4177
4178 if (rpath)
4179 {
4180 struct bfd_link_needed_list **pn;
66eb6687 4181 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4182 ;
4183 *pn = rpath;
4184 }
4185
9acc85a6
AM
4186 /* If we have a PT_GNU_RELRO program header, mark as read-only
4187 all sections contained fully therein. This makes relro
4188 shared library sections appear as they will at run-time. */
4189 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4190 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4191 if (phdr->p_type == PT_GNU_RELRO)
4192 {
4193 for (s = abfd->sections; s != NULL; s = s->next)
4194 if ((s->flags & SEC_ALLOC) != 0
4195 && s->vma >= phdr->p_vaddr
4196 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4197 s->flags |= SEC_READONLY;
4198 break;
4199 }
4200
4ad4eba5
AM
4201 /* We do not want to include any of the sections in a dynamic
4202 object in the output file. We hack by simply clobbering the
4203 list of sections in the BFD. This could be handled more
4204 cleanly by, say, a new section flag; the existing
4205 SEC_NEVER_LOAD flag is not the one we want, because that one
4206 still implies that the section takes up space in the output
4207 file. */
4208 bfd_section_list_clear (abfd);
4209
4ad4eba5
AM
4210 /* Find the name to use in a DT_NEEDED entry that refers to this
4211 object. If the object has a DT_SONAME entry, we use it.
4212 Otherwise, if the generic linker stuck something in
4213 elf_dt_name, we use that. Otherwise, we just use the file
4214 name. */
4215 if (soname == NULL || *soname == '\0')
4216 {
4217 soname = elf_dt_name (abfd);
4218 if (soname == NULL || *soname == '\0')
4219 soname = bfd_get_filename (abfd);
4220 }
4221
4222 /* Save the SONAME because sometimes the linker emulation code
4223 will need to know it. */
4224 elf_dt_name (abfd) = soname;
4225
7e9f0867 4226 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4227 if (ret < 0)
4228 goto error_return;
4229
4230 /* If we have already included this dynamic object in the
4231 link, just ignore it. There is no reason to include a
4232 particular dynamic object more than once. */
4233 if (ret > 0)
4234 return TRUE;
7ee314fa
AM
4235
4236 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4237 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4238 }
4239
4240 /* If this is a dynamic object, we always link against the .dynsym
4241 symbol table, not the .symtab symbol table. The dynamic linker
4242 will only see the .dynsym symbol table, so there is no reason to
4243 look at .symtab for a dynamic object. */
4244
4245 if (! dynamic || elf_dynsymtab (abfd) == 0)
4246 hdr = &elf_tdata (abfd)->symtab_hdr;
4247 else
4248 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4249
4250 symcount = hdr->sh_size / bed->s->sizeof_sym;
4251
4252 /* The sh_info field of the symtab header tells us where the
4253 external symbols start. We don't care about the local symbols at
4254 this point. */
4255 if (elf_bad_symtab (abfd))
4256 {
4257 extsymcount = symcount;
4258 extsymoff = 0;
4259 }
4260 else
4261 {
4262 extsymcount = symcount - hdr->sh_info;
4263 extsymoff = hdr->sh_info;
4264 }
4265
f45794cb 4266 sym_hash = elf_sym_hashes (abfd);
012b2306 4267 if (extsymcount != 0)
4ad4eba5
AM
4268 {
4269 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4270 NULL, NULL, NULL);
4271 if (isymbuf == NULL)
4272 goto error_return;
4273
4ad4eba5 4274 if (sym_hash == NULL)
012b2306
AM
4275 {
4276 /* We store a pointer to the hash table entry for each
4277 external symbol. */
ef53be89
AM
4278 amt = extsymcount;
4279 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4280 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4281 if (sym_hash == NULL)
4282 goto error_free_sym;
4283 elf_sym_hashes (abfd) = sym_hash;
4284 }
4ad4eba5
AM
4285 }
4286
4287 if (dynamic)
4288 {
4289 /* Read in any version definitions. */
fc0e6df6
PB
4290 if (!_bfd_elf_slurp_version_tables (abfd,
4291 info->default_imported_symver))
4ad4eba5
AM
4292 goto error_free_sym;
4293
4294 /* Read in the symbol versions, but don't bother to convert them
4295 to internal format. */
4296 if (elf_dynversym (abfd) != 0)
4297 {
4298 Elf_Internal_Shdr *versymhdr;
4299
4300 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
be22c732
NC
4301 amt = versymhdr->sh_size;
4302 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4ad4eba5
AM
4303 if (extversym == NULL)
4304 goto error_free_sym;
4ad4eba5
AM
4305 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4306 || bfd_bread (extversym, amt, abfd) != amt)
4307 goto error_free_vers;
be22c732 4308 extversym_end = extversym + (amt / sizeof (* extversym));
4ad4eba5
AM
4309 }
4310 }
4311
66eb6687
AM
4312 /* If we are loading an as-needed shared lib, save the symbol table
4313 state before we start adding symbols. If the lib turns out
4314 to be unneeded, restore the state. */
4315 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4316 {
4317 unsigned int i;
4318 size_t entsize;
4319
4320 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4321 {
4322 struct bfd_hash_entry *p;
2de92251 4323 struct elf_link_hash_entry *h;
66eb6687
AM
4324
4325 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4326 {
4327 h = (struct elf_link_hash_entry *) p;
4328 entsize += htab->root.table.entsize;
4329 if (h->root.type == bfd_link_hash_warning)
4330 entsize += htab->root.table.entsize;
4331 }
66eb6687
AM
4332 }
4333
4334 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4335 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4336 if (old_tab == NULL)
4337 goto error_free_vers;
4338
4339 /* Remember the current objalloc pointer, so that all mem for
4340 symbols added can later be reclaimed. */
4341 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4342 if (alloc_mark == NULL)
4343 goto error_free_vers;
4344
5061a885
AM
4345 /* Make a special call to the linker "notice" function to
4346 tell it that we are about to handle an as-needed lib. */
e5034e59 4347 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4348 goto error_free_vers;
5061a885 4349
f45794cb
AM
4350 /* Clone the symbol table. Remember some pointers into the
4351 symbol table, and dynamic symbol count. */
4352 old_ent = (char *) old_tab + tabsize;
66eb6687 4353 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4354 old_undefs = htab->root.undefs;
4355 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4356 old_table = htab->root.table.table;
4357 old_size = htab->root.table.size;
4358 old_count = htab->root.table.count;
5b677558
AM
4359 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4360 if (old_strtab == NULL)
4361 goto error_free_vers;
66eb6687
AM
4362
4363 for (i = 0; i < htab->root.table.size; i++)
4364 {
4365 struct bfd_hash_entry *p;
2de92251 4366 struct elf_link_hash_entry *h;
66eb6687
AM
4367
4368 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4369 {
4370 memcpy (old_ent, p, htab->root.table.entsize);
4371 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4372 h = (struct elf_link_hash_entry *) p;
4373 if (h->root.type == bfd_link_hash_warning)
4374 {
4375 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4376 old_ent = (char *) old_ent + htab->root.table.entsize;
4377 }
66eb6687
AM
4378 }
4379 }
4380 }
4ad4eba5 4381
66eb6687 4382 weaks = NULL;
be22c732
NC
4383 if (extversym == NULL)
4384 ever = NULL;
4385 else if (extversym + extsymoff < extversym_end)
4386 ever = extversym + extsymoff;
4387 else
4388 {
4389 /* xgettext:c-format */
4390 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4391 abfd, (long) extsymoff,
4392 (long) (extversym_end - extversym) / sizeof (* extversym));
4393 bfd_set_error (bfd_error_bad_value);
4394 goto error_free_vers;
4395 }
4396
4ad4eba5
AM
4397 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4398 isym < isymend;
4399 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4400 {
4401 int bind;
4402 bfd_vma value;
af44c138 4403 asection *sec, *new_sec;
4ad4eba5
AM
4404 flagword flags;
4405 const char *name;
4406 struct elf_link_hash_entry *h;
90c984fc 4407 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4408 bfd_boolean definition;
4409 bfd_boolean size_change_ok;
4410 bfd_boolean type_change_ok;
37a9e49a
L
4411 bfd_boolean new_weak;
4412 bfd_boolean old_weak;
4ad4eba5 4413 bfd_boolean override;
a4d8e49b 4414 bfd_boolean common;
97196564 4415 bfd_boolean discarded;
4ad4eba5
AM
4416 unsigned int old_alignment;
4417 bfd *old_bfd;
6e33951e 4418 bfd_boolean matched;
4ad4eba5
AM
4419
4420 override = FALSE;
4421
4422 flags = BSF_NO_FLAGS;
4423 sec = NULL;
4424 value = isym->st_value;
a4d8e49b 4425 common = bed->common_definition (isym);
2980ccad
L
4426 if (common && info->inhibit_common_definition)
4427 {
4428 /* Treat common symbol as undefined for --no-define-common. */
4429 isym->st_shndx = SHN_UNDEF;
4430 common = FALSE;
4431 }
97196564 4432 discarded = FALSE;
4ad4eba5
AM
4433
4434 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4435 switch (bind)
4ad4eba5 4436 {
3e7a7d11 4437 case STB_LOCAL:
4ad4eba5
AM
4438 /* This should be impossible, since ELF requires that all
4439 global symbols follow all local symbols, and that sh_info
4440 point to the first global symbol. Unfortunately, Irix 5
4441 screws this up. */
4442 continue;
3e7a7d11
NC
4443
4444 case STB_GLOBAL:
a4d8e49b 4445 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4446 flags = BSF_GLOBAL;
3e7a7d11
NC
4447 break;
4448
4449 case STB_WEAK:
4450 flags = BSF_WEAK;
4451 break;
4452
4453 case STB_GNU_UNIQUE:
4454 flags = BSF_GNU_UNIQUE;
4455 break;
4456
4457 default:
4ad4eba5 4458 /* Leave it up to the processor backend. */
3e7a7d11 4459 break;
4ad4eba5
AM
4460 }
4461
4462 if (isym->st_shndx == SHN_UNDEF)
4463 sec = bfd_und_section_ptr;
cb33740c
AM
4464 else if (isym->st_shndx == SHN_ABS)
4465 sec = bfd_abs_section_ptr;
4466 else if (isym->st_shndx == SHN_COMMON)
4467 {
4468 sec = bfd_com_section_ptr;
4469 /* What ELF calls the size we call the value. What ELF
4470 calls the value we call the alignment. */
4471 value = isym->st_size;
4472 }
4473 else
4ad4eba5
AM
4474 {
4475 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4476 if (sec == NULL)
4477 sec = bfd_abs_section_ptr;
dbaa2011 4478 else if (discarded_section (sec))
529fcb95 4479 {
e5d08002
L
4480 /* Symbols from discarded section are undefined. We keep
4481 its visibility. */
529fcb95 4482 sec = bfd_und_section_ptr;
97196564 4483 discarded = TRUE;
529fcb95
PB
4484 isym->st_shndx = SHN_UNDEF;
4485 }
4ad4eba5
AM
4486 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4487 value -= sec->vma;
4488 }
4ad4eba5
AM
4489
4490 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4491 isym->st_name);
4492 if (name == NULL)
4493 goto error_free_vers;
4494
4495 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4496 && (abfd->flags & BFD_PLUGIN) != 0)
4497 {
4498 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4499
4500 if (xc == NULL)
4501 {
4502 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4503 | SEC_EXCLUDE);
4504 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4505 if (xc == NULL)
4506 goto error_free_vers;
4507 }
4508 sec = xc;
4509 }
4510 else if (isym->st_shndx == SHN_COMMON
4511 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4512 && !bfd_link_relocatable (info))
4ad4eba5
AM
4513 {
4514 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4515
4516 if (tcomm == NULL)
4517 {
02d00247
AM
4518 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4519 | SEC_LINKER_CREATED);
4520 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4521 if (tcomm == NULL)
4ad4eba5
AM
4522 goto error_free_vers;
4523 }
4524 sec = tcomm;
4525 }
66eb6687 4526 else if (bed->elf_add_symbol_hook)
4ad4eba5 4527 {
66eb6687
AM
4528 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4529 &sec, &value))
4ad4eba5
AM
4530 goto error_free_vers;
4531
4532 /* The hook function sets the name to NULL if this symbol
4533 should be skipped for some reason. */
4534 if (name == NULL)
4535 continue;
4536 }
4537
4538 /* Sanity check that all possibilities were handled. */
4539 if (sec == NULL)
4540 {
4541 bfd_set_error (bfd_error_bad_value);
4542 goto error_free_vers;
4543 }
4544
191c0c42
AM
4545 /* Silently discard TLS symbols from --just-syms. There's
4546 no way to combine a static TLS block with a new TLS block
4547 for this executable. */
4548 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4549 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4550 continue;
4551
4ad4eba5
AM
4552 if (bfd_is_und_section (sec)
4553 || bfd_is_com_section (sec))
4554 definition = FALSE;
4555 else
4556 definition = TRUE;
4557
4558 size_change_ok = FALSE;
66eb6687 4559 type_change_ok = bed->type_change_ok;
37a9e49a 4560 old_weak = FALSE;
6e33951e 4561 matched = FALSE;
4ad4eba5
AM
4562 old_alignment = 0;
4563 old_bfd = NULL;
af44c138 4564 new_sec = sec;
4ad4eba5 4565
66eb6687 4566 if (is_elf_hash_table (htab))
4ad4eba5
AM
4567 {
4568 Elf_Internal_Versym iver;
4569 unsigned int vernum = 0;
4570 bfd_boolean skip;
4571
fc0e6df6 4572 if (ever == NULL)
4ad4eba5 4573 {
fc0e6df6
PB
4574 if (info->default_imported_symver)
4575 /* Use the default symbol version created earlier. */
4576 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4577 else
4578 iver.vs_vers = 0;
4579 }
be22c732
NC
4580 else if (ever >= extversym_end)
4581 {
4582 /* xgettext:c-format */
4583 _bfd_error_handler (_("%pB: not enough version information"),
4584 abfd);
4585 bfd_set_error (bfd_error_bad_value);
4586 goto error_free_vers;
4587 }
fc0e6df6
PB
4588 else
4589 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4590
4591 vernum = iver.vs_vers & VERSYM_VERSION;
4592
4593 /* If this is a hidden symbol, or if it is not version
4594 1, we append the version name to the symbol name.
cc86ff91
EB
4595 However, we do not modify a non-hidden absolute symbol
4596 if it is not a function, because it might be the version
4597 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4598 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4599 || (vernum > 1
4600 && (!bfd_is_abs_section (sec)
4601 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4602 {
4603 const char *verstr;
4604 size_t namelen, verlen, newlen;
4605 char *newname, *p;
4606
4607 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4608 {
fc0e6df6
PB
4609 if (vernum > elf_tdata (abfd)->cverdefs)
4610 verstr = NULL;
4611 else if (vernum > 1)
4612 verstr =
4613 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4614 else
4615 verstr = "";
4ad4eba5 4616
fc0e6df6 4617 if (verstr == NULL)
4ad4eba5 4618 {
4eca0228 4619 _bfd_error_handler
695344c0 4620 /* xgettext:c-format */
871b3ab2 4621 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4622 abfd, name, vernum,
4623 elf_tdata (abfd)->cverdefs);
4624 bfd_set_error (bfd_error_bad_value);
4625 goto error_free_vers;
4ad4eba5 4626 }
fc0e6df6
PB
4627 }
4628 else
4629 {
4630 /* We cannot simply test for the number of
4631 entries in the VERNEED section since the
4632 numbers for the needed versions do not start
4633 at 0. */
4634 Elf_Internal_Verneed *t;
4635
4636 verstr = NULL;
4637 for (t = elf_tdata (abfd)->verref;
4638 t != NULL;
4639 t = t->vn_nextref)
4ad4eba5 4640 {
fc0e6df6 4641 Elf_Internal_Vernaux *a;
4ad4eba5 4642
fc0e6df6
PB
4643 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4644 {
4645 if (a->vna_other == vernum)
4ad4eba5 4646 {
fc0e6df6
PB
4647 verstr = a->vna_nodename;
4648 break;
4ad4eba5 4649 }
4ad4eba5 4650 }
fc0e6df6
PB
4651 if (a != NULL)
4652 break;
4653 }
4654 if (verstr == NULL)
4655 {
4eca0228 4656 _bfd_error_handler
695344c0 4657 /* xgettext:c-format */
871b3ab2 4658 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4659 abfd, name, vernum);
4660 bfd_set_error (bfd_error_bad_value);
4661 goto error_free_vers;
4ad4eba5 4662 }
4ad4eba5 4663 }
fc0e6df6
PB
4664
4665 namelen = strlen (name);
4666 verlen = strlen (verstr);
4667 newlen = namelen + verlen + 2;
4668 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4669 && isym->st_shndx != SHN_UNDEF)
4670 ++newlen;
4671
a50b1753 4672 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4673 if (newname == NULL)
4674 goto error_free_vers;
4675 memcpy (newname, name, namelen);
4676 p = newname + namelen;
4677 *p++ = ELF_VER_CHR;
4678 /* If this is a defined non-hidden version symbol,
4679 we add another @ to the name. This indicates the
4680 default version of the symbol. */
4681 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4682 && isym->st_shndx != SHN_UNDEF)
4683 *p++ = ELF_VER_CHR;
4684 memcpy (p, verstr, verlen + 1);
4685
4686 name = newname;
4ad4eba5
AM
4687 }
4688
cd3416da
AM
4689 /* If this symbol has default visibility and the user has
4690 requested we not re-export it, then mark it as hidden. */
a0d49154 4691 if (!bfd_is_und_section (sec)
cd3416da 4692 && !dynamic
ce875075 4693 && abfd->no_export
cd3416da
AM
4694 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4695 isym->st_other = (STV_HIDDEN
4696 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4697
4f3fedcf
AM
4698 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4699 sym_hash, &old_bfd, &old_weak,
4700 &old_alignment, &skip, &override,
6e33951e
L
4701 &type_change_ok, &size_change_ok,
4702 &matched))
4ad4eba5
AM
4703 goto error_free_vers;
4704
4705 if (skip)
4706 continue;
4707
6e33951e
L
4708 /* Override a definition only if the new symbol matches the
4709 existing one. */
4710 if (override && matched)
4ad4eba5
AM
4711 definition = FALSE;
4712
4713 h = *sym_hash;
4714 while (h->root.type == bfd_link_hash_indirect
4715 || h->root.type == bfd_link_hash_warning)
4716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4717
4ad4eba5 4718 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4719 && vernum > 1
4720 && definition)
4721 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4722 }
4723
4724 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4725 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4726 (struct bfd_link_hash_entry **) sym_hash)))
4727 goto error_free_vers;
4728
ac98f9e2
L
4729 if ((abfd->flags & DYNAMIC) == 0
4730 && (bfd_get_flavour (info->output_bfd)
4731 == bfd_target_elf_flavour))
4732 {
4733 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4734 elf_tdata (info->output_bfd)->has_gnu_symbols
4735 |= elf_gnu_symbol_ifunc;
4736 if ((flags & BSF_GNU_UNIQUE))
4737 elf_tdata (info->output_bfd)->has_gnu_symbols
4738 |= elf_gnu_symbol_unique;
4739 }
a43942db 4740
4ad4eba5 4741 h = *sym_hash;
90c984fc
L
4742 /* We need to make sure that indirect symbol dynamic flags are
4743 updated. */
4744 hi = h;
4ad4eba5
AM
4745 while (h->root.type == bfd_link_hash_indirect
4746 || h->root.type == bfd_link_hash_warning)
4747 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4748
97196564
L
4749 /* Setting the index to -3 tells elf_link_output_extsym that
4750 this symbol is defined in a discarded section. */
4751 if (discarded)
4752 h->indx = -3;
4753
4ad4eba5
AM
4754 *sym_hash = h;
4755
37a9e49a 4756 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4757 if (dynamic
4758 && definition
37a9e49a 4759 && new_weak
fcb93ecf 4760 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4761 && is_elf_hash_table (htab)
60d67dc8 4762 && h->u.alias == NULL)
4ad4eba5
AM
4763 {
4764 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4765 a dynamic object, using the alias field. Later in this
4766 function we will set the alias field to the correct
4ad4eba5
AM
4767 value. We only put non-function symbols from dynamic
4768 objects on this list, because that happens to be the only
4769 time we need to know the normal symbol corresponding to a
4770 weak symbol, and the information is time consuming to
60d67dc8 4771 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4772 then this symbol was already defined by some previous
4773 dynamic object, and we will be using that previous
4774 definition anyhow. */
4775
60d67dc8 4776 h->u.alias = weaks;
4ad4eba5 4777 weaks = h;
4ad4eba5
AM
4778 }
4779
4780 /* Set the alignment of a common symbol. */
a4d8e49b 4781 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4782 && h->root.type == bfd_link_hash_common)
4783 {
4784 unsigned int align;
4785
a4d8e49b 4786 if (common)
af44c138
L
4787 align = bfd_log2 (isym->st_value);
4788 else
4789 {
4790 /* The new symbol is a common symbol in a shared object.
4791 We need to get the alignment from the section. */
4792 align = new_sec->alignment_power;
4793 }
595213d4 4794 if (align > old_alignment)
4ad4eba5
AM
4795 h->root.u.c.p->alignment_power = align;
4796 else
4797 h->root.u.c.p->alignment_power = old_alignment;
4798 }
4799
66eb6687 4800 if (is_elf_hash_table (htab))
4ad4eba5 4801 {
4f3fedcf
AM
4802 /* Set a flag in the hash table entry indicating the type of
4803 reference or definition we just found. A dynamic symbol
4804 is one which is referenced or defined by both a regular
4805 object and a shared object. */
4806 bfd_boolean dynsym = FALSE;
4807
4808 /* Plugin symbols aren't normal. Don't set def_regular or
4809 ref_regular for them, or make them dynamic. */
4810 if ((abfd->flags & BFD_PLUGIN) != 0)
4811 ;
4812 else if (! dynamic)
4813 {
4814 if (! definition)
4815 {
4816 h->ref_regular = 1;
4817 if (bind != STB_WEAK)
4818 h->ref_regular_nonweak = 1;
4819 }
4820 else
4821 {
4822 h->def_regular = 1;
4823 if (h->def_dynamic)
4824 {
4825 h->def_dynamic = 0;
4826 h->ref_dynamic = 1;
4827 }
4828 }
4829
4830 /* If the indirect symbol has been forced local, don't
4831 make the real symbol dynamic. */
4832 if ((h == hi || !hi->forced_local)
0e1862bb 4833 && (bfd_link_dll (info)
4f3fedcf
AM
4834 || h->def_dynamic
4835 || h->ref_dynamic))
4836 dynsym = TRUE;
4837 }
4838 else
4839 {
4840 if (! definition)
4841 {
4842 h->ref_dynamic = 1;
4843 hi->ref_dynamic = 1;
4844 }
4845 else
4846 {
4847 h->def_dynamic = 1;
4848 hi->def_dynamic = 1;
4849 }
4850
4851 /* If the indirect symbol has been forced local, don't
4852 make the real symbol dynamic. */
4853 if ((h == hi || !hi->forced_local)
4854 && (h->def_regular
4855 || h->ref_regular
60d67dc8
AM
4856 || (h->is_weakalias
4857 && weakdef (h)->dynindx != -1)))
4f3fedcf
AM
4858 dynsym = TRUE;
4859 }
4860
4861 /* Check to see if we need to add an indirect symbol for
4862 the default name. */
4863 if (definition
4864 || (!override && h->root.type == bfd_link_hash_common))
4865 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4866 sec, value, &old_bfd, &dynsym))
4867 goto error_free_vers;
4ad4eba5
AM
4868
4869 /* Check the alignment when a common symbol is involved. This
4870 can change when a common symbol is overridden by a normal
4871 definition or a common symbol is ignored due to the old
4872 normal definition. We need to make sure the maximum
4873 alignment is maintained. */
a4d8e49b 4874 if ((old_alignment || common)
4ad4eba5
AM
4875 && h->root.type != bfd_link_hash_common)
4876 {
4877 unsigned int common_align;
4878 unsigned int normal_align;
4879 unsigned int symbol_align;
4880 bfd *normal_bfd;
4881 bfd *common_bfd;
4882
3a81e825
AM
4883 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4884 || h->root.type == bfd_link_hash_defweak);
4885
4ad4eba5
AM
4886 symbol_align = ffs (h->root.u.def.value) - 1;
4887 if (h->root.u.def.section->owner != NULL
0616a280
AM
4888 && (h->root.u.def.section->owner->flags
4889 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4890 {
4891 normal_align = h->root.u.def.section->alignment_power;
4892 if (normal_align > symbol_align)
4893 normal_align = symbol_align;
4894 }
4895 else
4896 normal_align = symbol_align;
4897
4898 if (old_alignment)
4899 {
4900 common_align = old_alignment;
4901 common_bfd = old_bfd;
4902 normal_bfd = abfd;
4903 }
4904 else
4905 {
4906 common_align = bfd_log2 (isym->st_value);
4907 common_bfd = abfd;
4908 normal_bfd = old_bfd;
4909 }
4910
4911 if (normal_align < common_align)
d07676f8
NC
4912 {
4913 /* PR binutils/2735 */
4914 if (normal_bfd == NULL)
4eca0228 4915 _bfd_error_handler
695344c0 4916 /* xgettext:c-format */
9793eb77 4917 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 4918 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
4919 1 << common_align, name, common_bfd,
4920 1 << normal_align, h->root.u.def.section);
d07676f8 4921 else
4eca0228 4922 _bfd_error_handler
695344c0 4923 /* xgettext:c-format */
9793eb77 4924 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 4925 " is smaller than %u in %pB"),
c08bb8dd
AM
4926 1 << normal_align, name, normal_bfd,
4927 1 << common_align, common_bfd);
d07676f8 4928 }
4ad4eba5
AM
4929 }
4930
83ad0046 4931 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4932 if (isym->st_size != 0
4933 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4934 && (definition || h->size == 0))
4935 {
83ad0046
L
4936 if (h->size != 0
4937 && h->size != isym->st_size
4938 && ! size_change_ok)
4eca0228 4939 _bfd_error_handler
695344c0 4940 /* xgettext:c-format */
9793eb77 4941 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
4942 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4943 name, (uint64_t) h->size, old_bfd,
4944 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
4945
4946 h->size = isym->st_size;
4947 }
4948
4949 /* If this is a common symbol, then we always want H->SIZE
4950 to be the size of the common symbol. The code just above
4951 won't fix the size if a common symbol becomes larger. We
4952 don't warn about a size change here, because that is
4f3fedcf 4953 covered by --warn-common. Allow changes between different
fcb93ecf 4954 function types. */
4ad4eba5
AM
4955 if (h->root.type == bfd_link_hash_common)
4956 h->size = h->root.u.c.size;
4957
4958 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4959 && ((definition && !new_weak)
4960 || (old_weak && h->root.type == bfd_link_hash_common)
4961 || h->type == STT_NOTYPE))
4ad4eba5 4962 {
2955ec4c
L
4963 unsigned int type = ELF_ST_TYPE (isym->st_info);
4964
4965 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4966 symbol. */
4967 if (type == STT_GNU_IFUNC
4968 && (abfd->flags & DYNAMIC) != 0)
4969 type = STT_FUNC;
4ad4eba5 4970
2955ec4c
L
4971 if (h->type != type)
4972 {
4973 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4974 /* xgettext:c-format */
4eca0228 4975 _bfd_error_handler
9793eb77 4976 (_("warning: type of symbol `%s' changed"
871b3ab2 4977 " from %d to %d in %pB"),
c08bb8dd 4978 name, h->type, type, abfd);
2955ec4c
L
4979
4980 h->type = type;
4981 }
4ad4eba5
AM
4982 }
4983
54ac0771 4984 /* Merge st_other field. */
b8417128 4985 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4986
c3df8c14 4987 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4988 if (definition
4989 && (sec->flags & SEC_DEBUGGING)
4990 && !bfd_link_relocatable (info))
c3df8c14
AM
4991 dynsym = FALSE;
4992
4f3fedcf
AM
4993 /* Nor should we make plugin symbols dynamic. */
4994 if ((abfd->flags & BFD_PLUGIN) != 0)
4995 dynsym = FALSE;
4996
35fc36a8 4997 if (definition)
35399224
L
4998 {
4999 h->target_internal = isym->st_target_internal;
5000 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5001 }
35fc36a8 5002
4ad4eba5
AM
5003 if (definition && !dynamic)
5004 {
5005 char *p = strchr (name, ELF_VER_CHR);
5006 if (p != NULL && p[1] != ELF_VER_CHR)
5007 {
5008 /* Queue non-default versions so that .symver x, x@FOO
5009 aliases can be checked. */
66eb6687 5010 if (!nondeflt_vers)
4ad4eba5 5011 {
66eb6687
AM
5012 amt = ((isymend - isym + 1)
5013 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5014 nondeflt_vers
5015 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5016 if (!nondeflt_vers)
5017 goto error_free_vers;
4ad4eba5 5018 }
66eb6687 5019 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5020 }
5021 }
5022
5023 if (dynsym && h->dynindx == -1)
5024 {
c152c796 5025 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5026 goto error_free_vers;
60d67dc8
AM
5027 if (h->is_weakalias
5028 && weakdef (h)->dynindx == -1)
4ad4eba5 5029 {
60d67dc8 5030 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5031 goto error_free_vers;
5032 }
5033 }
1f599d0e 5034 else if (h->dynindx != -1)
4ad4eba5
AM
5035 /* If the symbol already has a dynamic index, but
5036 visibility says it should not be visible, turn it into
5037 a local symbol. */
5038 switch (ELF_ST_VISIBILITY (h->other))
5039 {
5040 case STV_INTERNAL:
5041 case STV_HIDDEN:
5042 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5043 dynsym = FALSE;
5044 break;
5045 }
5046
aef28989
L
5047 /* Don't add DT_NEEDED for references from the dummy bfd nor
5048 for unmatched symbol. */
4ad4eba5 5049 if (!add_needed
aef28989 5050 && matched
4ad4eba5 5051 && definition
010e5ae2 5052 && ((dynsym
ffa9430d 5053 && h->ref_regular_nonweak
4f3fedcf
AM
5054 && (old_bfd == NULL
5055 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 5056 || (h->ref_dynamic_nonweak
010e5ae2 5057 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5058 && !on_needed_list (elf_dt_name (abfd),
5059 htab->needed, NULL))))
4ad4eba5
AM
5060 {
5061 int ret;
5062 const char *soname = elf_dt_name (abfd);
5063
16e4ecc0
AM
5064 info->callbacks->minfo ("%!", soname, old_bfd,
5065 h->root.root.string);
5066
4ad4eba5
AM
5067 /* A symbol from a library loaded via DT_NEEDED of some
5068 other library is referenced by a regular object.
e56f61be 5069 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5070 --no-add-needed is used and the reference was not
5071 a weak one. */
4f3fedcf 5072 if (old_bfd != NULL
b918acf9 5073 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5074 {
4eca0228 5075 _bfd_error_handler
695344c0 5076 /* xgettext:c-format */
871b3ab2 5077 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5078 old_bfd, name);
ff5ac77b 5079 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5080 goto error_free_vers;
5081 }
5082
a50b1753 5083 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5084 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5085
4ad4eba5 5086 add_needed = TRUE;
7e9f0867 5087 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
5088 if (ret < 0)
5089 goto error_free_vers;
5090
5091 BFD_ASSERT (ret == 0);
5092 }
5093 }
5094 }
5095
a83ef4d1
L
5096 if (info->lto_plugin_active
5097 && !bfd_link_relocatable (info)
5098 && (abfd->flags & BFD_PLUGIN) == 0
5099 && !just_syms
5100 && extsymcount)
5101 {
5102 int r_sym_shift;
5103
5104 if (bed->s->arch_size == 32)
5105 r_sym_shift = 8;
5106 else
5107 r_sym_shift = 32;
5108
5109 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5110 referenced in regular objects so that linker plugin will get
5111 the correct symbol resolution. */
5112
5113 sym_hash = elf_sym_hashes (abfd);
5114 for (s = abfd->sections; s != NULL; s = s->next)
5115 {
5116 Elf_Internal_Rela *internal_relocs;
5117 Elf_Internal_Rela *rel, *relend;
5118
5119 /* Don't check relocations in excluded sections. */
5120 if ((s->flags & SEC_RELOC) == 0
5121 || s->reloc_count == 0
5122 || (s->flags & SEC_EXCLUDE) != 0
5123 || ((info->strip == strip_all
5124 || info->strip == strip_debugger)
5125 && (s->flags & SEC_DEBUGGING) != 0))
5126 continue;
5127
5128 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5129 NULL,
5130 info->keep_memory);
5131 if (internal_relocs == NULL)
5132 goto error_free_vers;
5133
5134 rel = internal_relocs;
5135 relend = rel + s->reloc_count;
5136 for ( ; rel < relend; rel++)
5137 {
5138 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5139 struct elf_link_hash_entry *h;
5140
5141 /* Skip local symbols. */
5142 if (r_symndx < extsymoff)
5143 continue;
5144
5145 h = sym_hash[r_symndx - extsymoff];
5146 if (h != NULL)
5147 h->root.non_ir_ref_regular = 1;
5148 }
5149
5150 if (elf_section_data (s)->relocs != internal_relocs)
5151 free (internal_relocs);
5152 }
5153 }
5154
66eb6687
AM
5155 if (extversym != NULL)
5156 {
5157 free (extversym);
5158 extversym = NULL;
5159 }
5160
5161 if (isymbuf != NULL)
5162 {
5163 free (isymbuf);
5164 isymbuf = NULL;
5165 }
5166
5167 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5168 {
5169 unsigned int i;
5170
5171 /* Restore the symbol table. */
f45794cb
AM
5172 old_ent = (char *) old_tab + tabsize;
5173 memset (elf_sym_hashes (abfd), 0,
5174 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5175 htab->root.table.table = old_table;
5176 htab->root.table.size = old_size;
5177 htab->root.table.count = old_count;
66eb6687 5178 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5179 htab->root.undefs = old_undefs;
5180 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
5181 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5182 free (old_strtab);
5183 old_strtab = NULL;
66eb6687
AM
5184 for (i = 0; i < htab->root.table.size; i++)
5185 {
5186 struct bfd_hash_entry *p;
5187 struct elf_link_hash_entry *h;
3e0882af
L
5188 bfd_size_type size;
5189 unsigned int alignment_power;
4070765b 5190 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5191
5192 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5193 {
5194 h = (struct elf_link_hash_entry *) p;
2de92251
AM
5195 if (h->root.type == bfd_link_hash_warning)
5196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5197
3e0882af
L
5198 /* Preserve the maximum alignment and size for common
5199 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 5200 since it can still be loaded at run time by another
3e0882af
L
5201 dynamic lib. */
5202 if (h->root.type == bfd_link_hash_common)
5203 {
5204 size = h->root.u.c.size;
5205 alignment_power = h->root.u.c.p->alignment_power;
5206 }
5207 else
5208 {
5209 size = 0;
5210 alignment_power = 0;
5211 }
4070765b 5212 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5213 will be exported when the dynamic lib becomes needed
5214 in the second pass. */
4070765b 5215 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
66eb6687
AM
5216 memcpy (p, old_ent, htab->root.table.entsize);
5217 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5218 h = (struct elf_link_hash_entry *) p;
5219 if (h->root.type == bfd_link_hash_warning)
5220 {
5221 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5222 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 5223 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 5224 }
a4542f1b 5225 if (h->root.type == bfd_link_hash_common)
3e0882af
L
5226 {
5227 if (size > h->root.u.c.size)
5228 h->root.u.c.size = size;
5229 if (alignment_power > h->root.u.c.p->alignment_power)
5230 h->root.u.c.p->alignment_power = alignment_power;
5231 }
4070765b 5232 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5233 }
5234 }
5235
5061a885
AM
5236 /* Make a special call to the linker "notice" function to
5237 tell it that symbols added for crefs may need to be removed. */
e5034e59 5238 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5239 goto error_free_vers;
5061a885 5240
66eb6687
AM
5241 free (old_tab);
5242 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5243 alloc_mark);
5244 if (nondeflt_vers != NULL)
5245 free (nondeflt_vers);
5246 return TRUE;
5247 }
2de92251 5248
66eb6687
AM
5249 if (old_tab != NULL)
5250 {
e5034e59 5251 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5252 goto error_free_vers;
66eb6687
AM
5253 free (old_tab);
5254 old_tab = NULL;
5255 }
5256
c6e8a9a8
L
5257 /* Now that all the symbols from this input file are created, if
5258 not performing a relocatable link, handle .symver foo, foo@BAR
5259 such that any relocs against foo become foo@BAR. */
0e1862bb 5260 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5261 {
ef53be89 5262 size_t cnt, symidx;
4ad4eba5
AM
5263
5264 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5265 {
5266 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5267 char *shortname, *p;
5268
5269 p = strchr (h->root.root.string, ELF_VER_CHR);
5270 if (p == NULL
5271 || (h->root.type != bfd_link_hash_defined
5272 && h->root.type != bfd_link_hash_defweak))
5273 continue;
5274
5275 amt = p - h->root.root.string;
a50b1753 5276 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5277 if (!shortname)
5278 goto error_free_vers;
4ad4eba5
AM
5279 memcpy (shortname, h->root.root.string, amt);
5280 shortname[amt] = '\0';
5281
5282 hi = (struct elf_link_hash_entry *)
66eb6687 5283 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5284 FALSE, FALSE, FALSE);
5285 if (hi != NULL
5286 && hi->root.type == h->root.type
5287 && hi->root.u.def.value == h->root.u.def.value
5288 && hi->root.u.def.section == h->root.u.def.section)
5289 {
5290 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5291 hi->root.type = bfd_link_hash_indirect;
5292 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5293 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5294 sym_hash = elf_sym_hashes (abfd);
5295 if (sym_hash)
5296 for (symidx = 0; symidx < extsymcount; ++symidx)
5297 if (sym_hash[symidx] == hi)
5298 {
5299 sym_hash[symidx] = h;
5300 break;
5301 }
5302 }
5303 free (shortname);
5304 }
5305 free (nondeflt_vers);
5306 nondeflt_vers = NULL;
5307 }
5308
60d67dc8 5309 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5310 symbols we found. The only way to do this is to search all the
5311 symbols. Since we only need the information for non functions in
5312 dynamic objects, that's the only time we actually put anything on
5313 the list WEAKS. We need this information so that if a regular
5314 object refers to a symbol defined weakly in a dynamic object, the
5315 real symbol in the dynamic object is also put in the dynamic
5316 symbols; we also must arrange for both symbols to point to the
5317 same memory location. We could handle the general case of symbol
5318 aliasing, but a general symbol alias can only be generated in
5319 assembler code, handling it correctly would be very time
5320 consuming, and other ELF linkers don't handle general aliasing
5321 either. */
5322 if (weaks != NULL)
5323 {
5324 struct elf_link_hash_entry **hpp;
5325 struct elf_link_hash_entry **hppend;
5326 struct elf_link_hash_entry **sorted_sym_hash;
5327 struct elf_link_hash_entry *h;
5328 size_t sym_count;
5329
5330 /* Since we have to search the whole symbol list for each weak
5331 defined symbol, search time for N weak defined symbols will be
5332 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5333 amt = extsymcount;
5334 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5335 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5336 if (sorted_sym_hash == NULL)
5337 goto error_return;
5338 sym_hash = sorted_sym_hash;
5339 hpp = elf_sym_hashes (abfd);
5340 hppend = hpp + extsymcount;
5341 sym_count = 0;
5342 for (; hpp < hppend; hpp++)
5343 {
5344 h = *hpp;
5345 if (h != NULL
5346 && h->root.type == bfd_link_hash_defined
fcb93ecf 5347 && !bed->is_function_type (h->type))
4ad4eba5
AM
5348 {
5349 *sym_hash = h;
5350 sym_hash++;
5351 sym_count++;
5352 }
5353 }
5354
5355 qsort (sorted_sym_hash, sym_count,
5356 sizeof (struct elf_link_hash_entry *),
5357 elf_sort_symbol);
5358
5359 while (weaks != NULL)
5360 {
5361 struct elf_link_hash_entry *hlook;
5362 asection *slook;
5363 bfd_vma vlook;
ed54588d 5364 size_t i, j, idx = 0;
4ad4eba5
AM
5365
5366 hlook = weaks;
60d67dc8
AM
5367 weaks = hlook->u.alias;
5368 hlook->u.alias = NULL;
4ad4eba5 5369
e3e53eed
AM
5370 if (hlook->root.type != bfd_link_hash_defined
5371 && hlook->root.type != bfd_link_hash_defweak)
5372 continue;
5373
4ad4eba5
AM
5374 slook = hlook->root.u.def.section;
5375 vlook = hlook->root.u.def.value;
5376
4ad4eba5
AM
5377 i = 0;
5378 j = sym_count;
14160578 5379 while (i != j)
4ad4eba5
AM
5380 {
5381 bfd_signed_vma vdiff;
5382 idx = (i + j) / 2;
14160578 5383 h = sorted_sym_hash[idx];
4ad4eba5
AM
5384 vdiff = vlook - h->root.u.def.value;
5385 if (vdiff < 0)
5386 j = idx;
5387 else if (vdiff > 0)
5388 i = idx + 1;
5389 else
5390 {
d3435ae8 5391 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5392 if (sdiff < 0)
5393 j = idx;
5394 else if (sdiff > 0)
5395 i = idx + 1;
5396 else
14160578 5397 break;
4ad4eba5
AM
5398 }
5399 }
5400
5401 /* We didn't find a value/section match. */
14160578 5402 if (i == j)
4ad4eba5
AM
5403 continue;
5404
14160578
AM
5405 /* With multiple aliases, or when the weak symbol is already
5406 strongly defined, we have multiple matching symbols and
5407 the binary search above may land on any of them. Step
5408 one past the matching symbol(s). */
5409 while (++idx != j)
5410 {
5411 h = sorted_sym_hash[idx];
5412 if (h->root.u.def.section != slook
5413 || h->root.u.def.value != vlook)
5414 break;
5415 }
5416
5417 /* Now look back over the aliases. Since we sorted by size
5418 as well as value and section, we'll choose the one with
5419 the largest size. */
5420 while (idx-- != i)
4ad4eba5 5421 {
14160578 5422 h = sorted_sym_hash[idx];
4ad4eba5
AM
5423
5424 /* Stop if value or section doesn't match. */
14160578
AM
5425 if (h->root.u.def.section != slook
5426 || h->root.u.def.value != vlook)
4ad4eba5
AM
5427 break;
5428 else if (h != hlook)
5429 {
60d67dc8
AM
5430 struct elf_link_hash_entry *t;
5431
5432 hlook->u.alias = h;
5433 hlook->is_weakalias = 1;
5434 t = h;
5435 if (t->u.alias != NULL)
5436 while (t->u.alias != h)
5437 t = t->u.alias;
5438 t->u.alias = hlook;
4ad4eba5
AM
5439
5440 /* If the weak definition is in the list of dynamic
5441 symbols, make sure the real definition is put
5442 there as well. */
5443 if (hlook->dynindx != -1 && h->dynindx == -1)
5444 {
c152c796 5445 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5446 {
5447 err_free_sym_hash:
5448 free (sorted_sym_hash);
5449 goto error_return;
5450 }
4ad4eba5
AM
5451 }
5452
5453 /* If the real definition is in the list of dynamic
5454 symbols, make sure the weak definition is put
5455 there as well. If we don't do this, then the
5456 dynamic loader might not merge the entries for the
5457 real definition and the weak definition. */
5458 if (h->dynindx != -1 && hlook->dynindx == -1)
5459 {
c152c796 5460 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5461 goto err_free_sym_hash;
4ad4eba5
AM
5462 }
5463 break;
5464 }
5465 }
5466 }
5467
5468 free (sorted_sym_hash);
5469 }
5470
33177bb1
AM
5471 if (bed->check_directives
5472 && !(*bed->check_directives) (abfd, info))
5473 return FALSE;
85fbca6a 5474
4ad4eba5
AM
5475 /* If this is a non-traditional link, try to optimize the handling
5476 of the .stab/.stabstr sections. */
5477 if (! dynamic
5478 && ! info->traditional_format
66eb6687 5479 && is_elf_hash_table (htab)
4ad4eba5
AM
5480 && (info->strip != strip_all && info->strip != strip_debugger))
5481 {
5482 asection *stabstr;
5483
5484 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5485 if (stabstr != NULL)
5486 {
5487 bfd_size_type string_offset = 0;
5488 asection *stab;
5489
5490 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5491 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5492 && (!stab->name[5] ||
5493 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5494 && (stab->flags & SEC_MERGE) == 0
5495 && !bfd_is_abs_section (stab->output_section))
5496 {
5497 struct bfd_elf_section_data *secdata;
5498
5499 secdata = elf_section_data (stab);
66eb6687
AM
5500 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5501 stabstr, &secdata->sec_info,
4ad4eba5
AM
5502 &string_offset))
5503 goto error_return;
5504 if (secdata->sec_info)
dbaa2011 5505 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5506 }
5507 }
5508 }
5509
66eb6687 5510 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5511 {
5512 /* Add this bfd to the loaded list. */
5513 struct elf_link_loaded_list *n;
5514
ca4be51c 5515 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5516 if (n == NULL)
5517 goto error_return;
5518 n->abfd = abfd;
66eb6687
AM
5519 n->next = htab->loaded;
5520 htab->loaded = n;
4ad4eba5
AM
5521 }
5522
5523 return TRUE;
5524
5525 error_free_vers:
66eb6687
AM
5526 if (old_tab != NULL)
5527 free (old_tab);
5b677558
AM
5528 if (old_strtab != NULL)
5529 free (old_strtab);
4ad4eba5
AM
5530 if (nondeflt_vers != NULL)
5531 free (nondeflt_vers);
5532 if (extversym != NULL)
5533 free (extversym);
5534 error_free_sym:
5535 if (isymbuf != NULL)
5536 free (isymbuf);
5537 error_return:
5538 return FALSE;
5539}
5540
8387904d
AM
5541/* Return the linker hash table entry of a symbol that might be
5542 satisfied by an archive symbol. Return -1 on error. */
5543
5544struct elf_link_hash_entry *
5545_bfd_elf_archive_symbol_lookup (bfd *abfd,
5546 struct bfd_link_info *info,
5547 const char *name)
5548{
5549 struct elf_link_hash_entry *h;
5550 char *p, *copy;
5551 size_t len, first;
5552
2a41f396 5553 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5554 if (h != NULL)
5555 return h;
5556
5557 /* If this is a default version (the name contains @@), look up the
5558 symbol again with only one `@' as well as without the version.
5559 The effect is that references to the symbol with and without the
5560 version will be matched by the default symbol in the archive. */
5561
5562 p = strchr (name, ELF_VER_CHR);
5563 if (p == NULL || p[1] != ELF_VER_CHR)
5564 return h;
5565
5566 /* First check with only one `@'. */
5567 len = strlen (name);
a50b1753 5568 copy = (char *) bfd_alloc (abfd, len);
8387904d 5569 if (copy == NULL)
e99955cd 5570 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5571
5572 first = p - name + 1;
5573 memcpy (copy, name, first);
5574 memcpy (copy + first, name + first + 1, len - first);
5575
2a41f396 5576 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5577 if (h == NULL)
5578 {
5579 /* We also need to check references to the symbol without the
5580 version. */
5581 copy[first - 1] = '\0';
5582 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5583 FALSE, FALSE, TRUE);
8387904d
AM
5584 }
5585
5586 bfd_release (abfd, copy);
5587 return h;
5588}
5589
0ad989f9 5590/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5591 don't use _bfd_generic_link_add_archive_symbols because we need to
5592 handle versioned symbols.
0ad989f9
L
5593
5594 Fortunately, ELF archive handling is simpler than that done by
5595 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5596 oddities. In ELF, if we find a symbol in the archive map, and the
5597 symbol is currently undefined, we know that we must pull in that
5598 object file.
5599
5600 Unfortunately, we do have to make multiple passes over the symbol
5601 table until nothing further is resolved. */
5602
4ad4eba5
AM
5603static bfd_boolean
5604elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5605{
5606 symindex c;
13e570f8 5607 unsigned char *included = NULL;
0ad989f9
L
5608 carsym *symdefs;
5609 bfd_boolean loop;
5610 bfd_size_type amt;
8387904d
AM
5611 const struct elf_backend_data *bed;
5612 struct elf_link_hash_entry * (*archive_symbol_lookup)
5613 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5614
5615 if (! bfd_has_map (abfd))
5616 {
5617 /* An empty archive is a special case. */
5618 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5619 return TRUE;
5620 bfd_set_error (bfd_error_no_armap);
5621 return FALSE;
5622 }
5623
5624 /* Keep track of all symbols we know to be already defined, and all
5625 files we know to be already included. This is to speed up the
5626 second and subsequent passes. */
5627 c = bfd_ardata (abfd)->symdef_count;
5628 if (c == 0)
5629 return TRUE;
5630 amt = c;
13e570f8
AM
5631 amt *= sizeof (*included);
5632 included = (unsigned char *) bfd_zmalloc (amt);
5633 if (included == NULL)
5634 return FALSE;
0ad989f9
L
5635
5636 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5637 bed = get_elf_backend_data (abfd);
5638 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5639
5640 do
5641 {
5642 file_ptr last;
5643 symindex i;
5644 carsym *symdef;
5645 carsym *symdefend;
5646
5647 loop = FALSE;
5648 last = -1;
5649
5650 symdef = symdefs;
5651 symdefend = symdef + c;
5652 for (i = 0; symdef < symdefend; symdef++, i++)
5653 {
5654 struct elf_link_hash_entry *h;
5655 bfd *element;
5656 struct bfd_link_hash_entry *undefs_tail;
5657 symindex mark;
5658
13e570f8 5659 if (included[i])
0ad989f9
L
5660 continue;
5661 if (symdef->file_offset == last)
5662 {
5663 included[i] = TRUE;
5664 continue;
5665 }
5666
8387904d 5667 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5668 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5669 goto error_return;
0ad989f9
L
5670
5671 if (h == NULL)
5672 continue;
5673
5674 if (h->root.type == bfd_link_hash_common)
5675 {
5676 /* We currently have a common symbol. The archive map contains
5677 a reference to this symbol, so we may want to include it. We
5678 only want to include it however, if this archive element
5679 contains a definition of the symbol, not just another common
5680 declaration of it.
5681
5682 Unfortunately some archivers (including GNU ar) will put
5683 declarations of common symbols into their archive maps, as
5684 well as real definitions, so we cannot just go by the archive
5685 map alone. Instead we must read in the element's symbol
5686 table and check that to see what kind of symbol definition
5687 this is. */
5688 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5689 continue;
5690 }
5691 else if (h->root.type != bfd_link_hash_undefined)
5692 {
5693 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5694 /* Symbol must be defined. Don't check it again. */
5695 included[i] = TRUE;
0ad989f9
L
5696 continue;
5697 }
5698
5699 /* We need to include this archive member. */
5700 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5701 if (element == NULL)
5702 goto error_return;
5703
5704 if (! bfd_check_format (element, bfd_object))
5705 goto error_return;
5706
0ad989f9
L
5707 undefs_tail = info->hash->undefs_tail;
5708
0e144ba7
AM
5709 if (!(*info->callbacks
5710 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5711 continue;
0e144ba7 5712 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5713 goto error_return;
5714
5715 /* If there are any new undefined symbols, we need to make
5716 another pass through the archive in order to see whether
5717 they can be defined. FIXME: This isn't perfect, because
5718 common symbols wind up on undefs_tail and because an
5719 undefined symbol which is defined later on in this pass
5720 does not require another pass. This isn't a bug, but it
5721 does make the code less efficient than it could be. */
5722 if (undefs_tail != info->hash->undefs_tail)
5723 loop = TRUE;
5724
5725 /* Look backward to mark all symbols from this object file
5726 which we have already seen in this pass. */
5727 mark = i;
5728 do
5729 {
5730 included[mark] = TRUE;
5731 if (mark == 0)
5732 break;
5733 --mark;
5734 }
5735 while (symdefs[mark].file_offset == symdef->file_offset);
5736
5737 /* We mark subsequent symbols from this object file as we go
5738 on through the loop. */
5739 last = symdef->file_offset;
5740 }
5741 }
5742 while (loop);
5743
0ad989f9
L
5744 free (included);
5745
5746 return TRUE;
5747
5748 error_return:
0ad989f9
L
5749 if (included != NULL)
5750 free (included);
5751 return FALSE;
5752}
4ad4eba5
AM
5753
5754/* Given an ELF BFD, add symbols to the global hash table as
5755 appropriate. */
5756
5757bfd_boolean
5758bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5759{
5760 switch (bfd_get_format (abfd))
5761 {
5762 case bfd_object:
5763 return elf_link_add_object_symbols (abfd, info);
5764 case bfd_archive:
5765 return elf_link_add_archive_symbols (abfd, info);
5766 default:
5767 bfd_set_error (bfd_error_wrong_format);
5768 return FALSE;
5769 }
5770}
5a580b3a 5771\f
14b1c01e
AM
5772struct hash_codes_info
5773{
5774 unsigned long *hashcodes;
5775 bfd_boolean error;
5776};
a0c8462f 5777
5a580b3a
AM
5778/* This function will be called though elf_link_hash_traverse to store
5779 all hash value of the exported symbols in an array. */
5780
5781static bfd_boolean
5782elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5783{
a50b1753 5784 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5785 const char *name;
5a580b3a
AM
5786 unsigned long ha;
5787 char *alc = NULL;
5788
5a580b3a
AM
5789 /* Ignore indirect symbols. These are added by the versioning code. */
5790 if (h->dynindx == -1)
5791 return TRUE;
5792
5793 name = h->root.root.string;
422f1182 5794 if (h->versioned >= versioned)
5a580b3a 5795 {
422f1182
L
5796 char *p = strchr (name, ELF_VER_CHR);
5797 if (p != NULL)
14b1c01e 5798 {
422f1182
L
5799 alc = (char *) bfd_malloc (p - name + 1);
5800 if (alc == NULL)
5801 {
5802 inf->error = TRUE;
5803 return FALSE;
5804 }
5805 memcpy (alc, name, p - name);
5806 alc[p - name] = '\0';
5807 name = alc;
14b1c01e 5808 }
5a580b3a
AM
5809 }
5810
5811 /* Compute the hash value. */
5812 ha = bfd_elf_hash (name);
5813
5814 /* Store the found hash value in the array given as the argument. */
14b1c01e 5815 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5816
5817 /* And store it in the struct so that we can put it in the hash table
5818 later. */
f6e332e6 5819 h->u.elf_hash_value = ha;
5a580b3a
AM
5820
5821 if (alc != NULL)
5822 free (alc);
5823
5824 return TRUE;
5825}
5826
fdc90cb4
JJ
5827struct collect_gnu_hash_codes
5828{
5829 bfd *output_bfd;
5830 const struct elf_backend_data *bed;
5831 unsigned long int nsyms;
5832 unsigned long int maskbits;
5833 unsigned long int *hashcodes;
5834 unsigned long int *hashval;
5835 unsigned long int *indx;
5836 unsigned long int *counts;
5837 bfd_vma *bitmask;
5838 bfd_byte *contents;
5839 long int min_dynindx;
5840 unsigned long int bucketcount;
5841 unsigned long int symindx;
5842 long int local_indx;
5843 long int shift1, shift2;
5844 unsigned long int mask;
14b1c01e 5845 bfd_boolean error;
fdc90cb4
JJ
5846};
5847
5848/* This function will be called though elf_link_hash_traverse to store
5849 all hash value of the exported symbols in an array. */
5850
5851static bfd_boolean
5852elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5853{
a50b1753 5854 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5855 const char *name;
fdc90cb4
JJ
5856 unsigned long ha;
5857 char *alc = NULL;
5858
fdc90cb4
JJ
5859 /* Ignore indirect symbols. These are added by the versioning code. */
5860 if (h->dynindx == -1)
5861 return TRUE;
5862
5863 /* Ignore also local symbols and undefined symbols. */
5864 if (! (*s->bed->elf_hash_symbol) (h))
5865 return TRUE;
5866
5867 name = h->root.root.string;
422f1182 5868 if (h->versioned >= versioned)
fdc90cb4 5869 {
422f1182
L
5870 char *p = strchr (name, ELF_VER_CHR);
5871 if (p != NULL)
14b1c01e 5872 {
422f1182
L
5873 alc = (char *) bfd_malloc (p - name + 1);
5874 if (alc == NULL)
5875 {
5876 s->error = TRUE;
5877 return FALSE;
5878 }
5879 memcpy (alc, name, p - name);
5880 alc[p - name] = '\0';
5881 name = alc;
14b1c01e 5882 }
fdc90cb4
JJ
5883 }
5884
5885 /* Compute the hash value. */
5886 ha = bfd_elf_gnu_hash (name);
5887
5888 /* Store the found hash value in the array for compute_bucket_count,
5889 and also for .dynsym reordering purposes. */
5890 s->hashcodes[s->nsyms] = ha;
5891 s->hashval[h->dynindx] = ha;
5892 ++s->nsyms;
5893 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5894 s->min_dynindx = h->dynindx;
5895
5896 if (alc != NULL)
5897 free (alc);
5898
5899 return TRUE;
5900}
5901
5902/* This function will be called though elf_link_hash_traverse to do
5903 final dynaminc symbol renumbering. */
5904
5905static bfd_boolean
5906elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5907{
a50b1753 5908 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5909 unsigned long int bucket;
5910 unsigned long int val;
5911
fdc90cb4
JJ
5912 /* Ignore indirect symbols. */
5913 if (h->dynindx == -1)
5914 return TRUE;
5915
5916 /* Ignore also local symbols and undefined symbols. */
5917 if (! (*s->bed->elf_hash_symbol) (h))
5918 {
5919 if (h->dynindx >= s->min_dynindx)
5920 h->dynindx = s->local_indx++;
5921 return TRUE;
5922 }
5923
5924 bucket = s->hashval[h->dynindx] % s->bucketcount;
5925 val = (s->hashval[h->dynindx] >> s->shift1)
5926 & ((s->maskbits >> s->shift1) - 1);
5927 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5928 s->bitmask[val]
5929 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5930 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5931 if (s->counts[bucket] == 1)
5932 /* Last element terminates the chain. */
5933 val |= 1;
5934 bfd_put_32 (s->output_bfd, val,
5935 s->contents + (s->indx[bucket] - s->symindx) * 4);
5936 --s->counts[bucket];
5937 h->dynindx = s->indx[bucket]++;
5938 return TRUE;
5939}
5940
5941/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5942
5943bfd_boolean
5944_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5945{
5946 return !(h->forced_local
5947 || h->root.type == bfd_link_hash_undefined
5948 || h->root.type == bfd_link_hash_undefweak
5949 || ((h->root.type == bfd_link_hash_defined
5950 || h->root.type == bfd_link_hash_defweak)
5951 && h->root.u.def.section->output_section == NULL));
5952}
5953
5a580b3a
AM
5954/* Array used to determine the number of hash table buckets to use
5955 based on the number of symbols there are. If there are fewer than
5956 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5957 fewer than 37 we use 17 buckets, and so forth. We never use more
5958 than 32771 buckets. */
5959
5960static const size_t elf_buckets[] =
5961{
5962 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5963 16411, 32771, 0
5964};
5965
5966/* Compute bucket count for hashing table. We do not use a static set
5967 of possible tables sizes anymore. Instead we determine for all
5968 possible reasonable sizes of the table the outcome (i.e., the
5969 number of collisions etc) and choose the best solution. The
5970 weighting functions are not too simple to allow the table to grow
5971 without bounds. Instead one of the weighting factors is the size.
5972 Therefore the result is always a good payoff between few collisions
5973 (= short chain lengths) and table size. */
5974static size_t
b20dd2ce 5975compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5976 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5977 unsigned long int nsyms,
5978 int gnu_hash)
5a580b3a 5979{
5a580b3a 5980 size_t best_size = 0;
5a580b3a 5981 unsigned long int i;
5a580b3a 5982
5a580b3a
AM
5983 /* We have a problem here. The following code to optimize the table
5984 size requires an integer type with more the 32 bits. If
5985 BFD_HOST_U_64_BIT is set we know about such a type. */
5986#ifdef BFD_HOST_U_64_BIT
5987 if (info->optimize)
5988 {
5a580b3a
AM
5989 size_t minsize;
5990 size_t maxsize;
5991 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5992 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5993 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5994 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5995 unsigned long int *counts;
d40f3da9 5996 bfd_size_type amt;
0883b6e0 5997 unsigned int no_improvement_count = 0;
5a580b3a
AM
5998
5999 /* Possible optimization parameters: if we have NSYMS symbols we say
6000 that the hashing table must at least have NSYMS/4 and at most
6001 2*NSYMS buckets. */
6002 minsize = nsyms / 4;
6003 if (minsize == 0)
6004 minsize = 1;
6005 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6006 if (gnu_hash)
6007 {
6008 if (minsize < 2)
6009 minsize = 2;
6010 if ((best_size & 31) == 0)
6011 ++best_size;
6012 }
5a580b3a
AM
6013
6014 /* Create array where we count the collisions in. We must use bfd_malloc
6015 since the size could be large. */
6016 amt = maxsize;
6017 amt *= sizeof (unsigned long int);
a50b1753 6018 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6019 if (counts == NULL)
fdc90cb4 6020 return 0;
5a580b3a
AM
6021
6022 /* Compute the "optimal" size for the hash table. The criteria is a
6023 minimal chain length. The minor criteria is (of course) the size
6024 of the table. */
6025 for (i = minsize; i < maxsize; ++i)
6026 {
6027 /* Walk through the array of hashcodes and count the collisions. */
6028 BFD_HOST_U_64_BIT max;
6029 unsigned long int j;
6030 unsigned long int fact;
6031
fdc90cb4
JJ
6032 if (gnu_hash && (i & 31) == 0)
6033 continue;
6034
5a580b3a
AM
6035 memset (counts, '\0', i * sizeof (unsigned long int));
6036
6037 /* Determine how often each hash bucket is used. */
6038 for (j = 0; j < nsyms; ++j)
6039 ++counts[hashcodes[j] % i];
6040
6041 /* For the weight function we need some information about the
6042 pagesize on the target. This is information need not be 100%
6043 accurate. Since this information is not available (so far) we
6044 define it here to a reasonable default value. If it is crucial
6045 to have a better value some day simply define this value. */
6046# ifndef BFD_TARGET_PAGESIZE
6047# define BFD_TARGET_PAGESIZE (4096)
6048# endif
6049
fdc90cb4
JJ
6050 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6051 and the chains. */
6052 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6053
6054# if 1
6055 /* Variant 1: optimize for short chains. We add the squares
6056 of all the chain lengths (which favors many small chain
6057 over a few long chains). */
6058 for (j = 0; j < i; ++j)
6059 max += counts[j] * counts[j];
6060
6061 /* This adds penalties for the overall size of the table. */
fdc90cb4 6062 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6063 max *= fact * fact;
6064# else
6065 /* Variant 2: Optimize a lot more for small table. Here we
6066 also add squares of the size but we also add penalties for
6067 empty slots (the +1 term). */
6068 for (j = 0; j < i; ++j)
6069 max += (1 + counts[j]) * (1 + counts[j]);
6070
6071 /* The overall size of the table is considered, but not as
6072 strong as in variant 1, where it is squared. */
fdc90cb4 6073 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6074 max *= fact;
6075# endif
6076
6077 /* Compare with current best results. */
6078 if (max < best_chlen)
6079 {
6080 best_chlen = max;
6081 best_size = i;
ca4be51c 6082 no_improvement_count = 0;
5a580b3a 6083 }
0883b6e0
NC
6084 /* PR 11843: Avoid futile long searches for the best bucket size
6085 when there are a large number of symbols. */
6086 else if (++no_improvement_count == 100)
6087 break;
5a580b3a
AM
6088 }
6089
6090 free (counts);
6091 }
6092 else
6093#endif /* defined (BFD_HOST_U_64_BIT) */
6094 {
6095 /* This is the fallback solution if no 64bit type is available or if we
6096 are not supposed to spend much time on optimizations. We select the
6097 bucket count using a fixed set of numbers. */
6098 for (i = 0; elf_buckets[i] != 0; i++)
6099 {
6100 best_size = elf_buckets[i];
fdc90cb4 6101 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6102 break;
6103 }
fdc90cb4
JJ
6104 if (gnu_hash && best_size < 2)
6105 best_size = 2;
5a580b3a
AM
6106 }
6107
5a580b3a
AM
6108 return best_size;
6109}
6110
d0bf826b
AM
6111/* Size any SHT_GROUP section for ld -r. */
6112
6113bfd_boolean
6114_bfd_elf_size_group_sections (struct bfd_link_info *info)
6115{
6116 bfd *ibfd;
57963c05 6117 asection *s;
d0bf826b 6118
c72f2fb2 6119 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6120 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6121 && (s = ibfd->sections) != NULL
6122 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6123 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6124 return FALSE;
6125 return TRUE;
6126}
6127
04c3a755
NS
6128/* Set a default stack segment size. The value in INFO wins. If it
6129 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6130 undefined it is initialized. */
6131
6132bfd_boolean
6133bfd_elf_stack_segment_size (bfd *output_bfd,
6134 struct bfd_link_info *info,
6135 const char *legacy_symbol,
6136 bfd_vma default_size)
6137{
6138 struct elf_link_hash_entry *h = NULL;
6139
6140 /* Look for legacy symbol. */
6141 if (legacy_symbol)
6142 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6143 FALSE, FALSE, FALSE);
6144 if (h && (h->root.type == bfd_link_hash_defined
6145 || h->root.type == bfd_link_hash_defweak)
6146 && h->def_regular
6147 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6148 {
6149 /* The symbol has no type if specified on the command line. */
6150 h->type = STT_OBJECT;
6151 if (info->stacksize)
695344c0 6152 /* xgettext:c-format */
871b3ab2 6153 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6154 output_bfd, legacy_symbol);
04c3a755 6155 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6156 /* xgettext:c-format */
871b3ab2 6157 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6158 output_bfd, legacy_symbol);
04c3a755
NS
6159 else
6160 info->stacksize = h->root.u.def.value;
6161 }
6162
6163 if (!info->stacksize)
6164 /* If the user didn't set a size, or explicitly inhibit the
6165 size, set it now. */
6166 info->stacksize = default_size;
6167
6168 /* Provide the legacy symbol, if it is referenced. */
6169 if (h && (h->root.type == bfd_link_hash_undefined
6170 || h->root.type == bfd_link_hash_undefweak))
6171 {
6172 struct bfd_link_hash_entry *bh = NULL;
6173
6174 if (!(_bfd_generic_link_add_one_symbol
6175 (info, output_bfd, legacy_symbol,
6176 BSF_GLOBAL, bfd_abs_section_ptr,
6177 info->stacksize >= 0 ? info->stacksize : 0,
6178 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6179 return FALSE;
6180
6181 h = (struct elf_link_hash_entry *) bh;
6182 h->def_regular = 1;
6183 h->type = STT_OBJECT;
6184 }
6185
6186 return TRUE;
6187}
6188
b531344c
MR
6189/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6190
6191struct elf_gc_sweep_symbol_info
6192{
6193 struct bfd_link_info *info;
6194 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6195 bfd_boolean);
6196};
6197
6198static bfd_boolean
6199elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6200{
6201 if (!h->mark
6202 && (((h->root.type == bfd_link_hash_defined
6203 || h->root.type == bfd_link_hash_defweak)
6204 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6205 && h->root.u.def.section->gc_mark))
6206 || h->root.type == bfd_link_hash_undefined
6207 || h->root.type == bfd_link_hash_undefweak))
6208 {
6209 struct elf_gc_sweep_symbol_info *inf;
6210
6211 inf = (struct elf_gc_sweep_symbol_info *) data;
6212 (*inf->hide_symbol) (inf->info, h, TRUE);
6213 h->def_regular = 0;
6214 h->ref_regular = 0;
6215 h->ref_regular_nonweak = 0;
6216 }
6217
6218 return TRUE;
6219}
6220
5a580b3a
AM
6221/* Set up the sizes and contents of the ELF dynamic sections. This is
6222 called by the ELF linker emulation before_allocation routine. We
6223 must set the sizes of the sections before the linker sets the
6224 addresses of the various sections. */
6225
6226bfd_boolean
6227bfd_elf_size_dynamic_sections (bfd *output_bfd,
6228 const char *soname,
6229 const char *rpath,
6230 const char *filter_shlib,
7ee314fa
AM
6231 const char *audit,
6232 const char *depaudit,
5a580b3a
AM
6233 const char * const *auxiliary_filters,
6234 struct bfd_link_info *info,
fd91d419 6235 asection **sinterpptr)
5a580b3a 6236{
5a580b3a
AM
6237 bfd *dynobj;
6238 const struct elf_backend_data *bed;
5a580b3a
AM
6239
6240 *sinterpptr = NULL;
6241
5a580b3a
AM
6242 if (!is_elf_hash_table (info->hash))
6243 return TRUE;
6244
5a580b3a
AM
6245 dynobj = elf_hash_table (info)->dynobj;
6246
9a2a56cc 6247 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6248 {
902e9fc7
MR
6249 struct bfd_elf_version_tree *verdefs;
6250 struct elf_info_failed asvinfo;
5a580b3a
AM
6251 struct bfd_elf_version_tree *t;
6252 struct bfd_elf_version_expr *d;
902e9fc7 6253 asection *s;
e6699019 6254 size_t soname_indx;
7ee314fa 6255
5a580b3a
AM
6256 /* If we are supposed to export all symbols into the dynamic symbol
6257 table (this is not the normal case), then do so. */
55255dae 6258 if (info->export_dynamic
0e1862bb 6259 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6260 {
3d13f3e9
AM
6261 struct elf_info_failed eif;
6262
6263 eif.info = info;
6264 eif.failed = FALSE;
5a580b3a
AM
6265 elf_link_hash_traverse (elf_hash_table (info),
6266 _bfd_elf_export_symbol,
6267 &eif);
6268 if (eif.failed)
6269 return FALSE;
6270 }
6271
e6699019
L
6272 if (soname != NULL)
6273 {
6274 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6275 soname, TRUE);
6276 if (soname_indx == (size_t) -1
6277 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6278 return FALSE;
6279 }
6280 else
6281 soname_indx = (size_t) -1;
6282
5a580b3a 6283 /* Make all global versions with definition. */
fd91d419 6284 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6285 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6286 if (!d->symver && d->literal)
5a580b3a
AM
6287 {
6288 const char *verstr, *name;
6289 size_t namelen, verlen, newlen;
93252b1c 6290 char *newname, *p, leading_char;
5a580b3a
AM
6291 struct elf_link_hash_entry *newh;
6292
93252b1c 6293 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6294 name = d->pattern;
93252b1c 6295 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6296 verstr = t->name;
6297 verlen = strlen (verstr);
6298 newlen = namelen + verlen + 3;
6299
a50b1753 6300 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6301 if (newname == NULL)
6302 return FALSE;
93252b1c
MF
6303 newname[0] = leading_char;
6304 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6305
6306 /* Check the hidden versioned definition. */
6307 p = newname + namelen;
6308 *p++ = ELF_VER_CHR;
6309 memcpy (p, verstr, verlen + 1);
6310 newh = elf_link_hash_lookup (elf_hash_table (info),
6311 newname, FALSE, FALSE,
6312 FALSE);
6313 if (newh == NULL
6314 || (newh->root.type != bfd_link_hash_defined
6315 && newh->root.type != bfd_link_hash_defweak))
6316 {
6317 /* Check the default versioned definition. */
6318 *p++ = ELF_VER_CHR;
6319 memcpy (p, verstr, verlen + 1);
6320 newh = elf_link_hash_lookup (elf_hash_table (info),
6321 newname, FALSE, FALSE,
6322 FALSE);
6323 }
6324 free (newname);
6325
6326 /* Mark this version if there is a definition and it is
6327 not defined in a shared object. */
6328 if (newh != NULL
f5385ebf 6329 && !newh->def_dynamic
5a580b3a
AM
6330 && (newh->root.type == bfd_link_hash_defined
6331 || newh->root.type == bfd_link_hash_defweak))
6332 d->symver = 1;
6333 }
6334
6335 /* Attach all the symbols to their version information. */
5a580b3a 6336 asvinfo.info = info;
5a580b3a
AM
6337 asvinfo.failed = FALSE;
6338
6339 elf_link_hash_traverse (elf_hash_table (info),
6340 _bfd_elf_link_assign_sym_version,
6341 &asvinfo);
6342 if (asvinfo.failed)
6343 return FALSE;
6344
6345 if (!info->allow_undefined_version)
6346 {
6347 /* Check if all global versions have a definition. */
3d13f3e9 6348 bfd_boolean all_defined = TRUE;
fd91d419 6349 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6350 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6351 if (d->literal && !d->symver && !d->script)
5a580b3a 6352 {
4eca0228 6353 _bfd_error_handler
5a580b3a
AM
6354 (_("%s: undefined version: %s"),
6355 d->pattern, t->name);
6356 all_defined = FALSE;
6357 }
6358
6359 if (!all_defined)
6360 {
6361 bfd_set_error (bfd_error_bad_value);
6362 return FALSE;
6363 }
6364 }
6365
902e9fc7
MR
6366 /* Set up the version definition section. */
6367 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6368 BFD_ASSERT (s != NULL);
5a580b3a 6369
902e9fc7
MR
6370 /* We may have created additional version definitions if we are
6371 just linking a regular application. */
6372 verdefs = info->version_info;
5a580b3a 6373
902e9fc7
MR
6374 /* Skip anonymous version tag. */
6375 if (verdefs != NULL && verdefs->vernum == 0)
6376 verdefs = verdefs->next;
5a580b3a 6377
902e9fc7
MR
6378 if (verdefs == NULL && !info->create_default_symver)
6379 s->flags |= SEC_EXCLUDE;
6380 else
5a580b3a 6381 {
902e9fc7
MR
6382 unsigned int cdefs;
6383 bfd_size_type size;
6384 bfd_byte *p;
6385 Elf_Internal_Verdef def;
6386 Elf_Internal_Verdaux defaux;
6387 struct bfd_link_hash_entry *bh;
6388 struct elf_link_hash_entry *h;
6389 const char *name;
5a580b3a 6390
902e9fc7
MR
6391 cdefs = 0;
6392 size = 0;
5a580b3a 6393
902e9fc7
MR
6394 /* Make space for the base version. */
6395 size += sizeof (Elf_External_Verdef);
6396 size += sizeof (Elf_External_Verdaux);
6397 ++cdefs;
6398
6399 /* Make space for the default version. */
6400 if (info->create_default_symver)
6401 {
6402 size += sizeof (Elf_External_Verdef);
6403 ++cdefs;
3e3b46e5
PB
6404 }
6405
5a580b3a
AM
6406 for (t = verdefs; t != NULL; t = t->next)
6407 {
6408 struct bfd_elf_version_deps *n;
6409
a6cc6b3b
RO
6410 /* Don't emit base version twice. */
6411 if (t->vernum == 0)
6412 continue;
6413
5a580b3a
AM
6414 size += sizeof (Elf_External_Verdef);
6415 size += sizeof (Elf_External_Verdaux);
6416 ++cdefs;
6417
6418 for (n = t->deps; n != NULL; n = n->next)
6419 size += sizeof (Elf_External_Verdaux);
6420 }
6421
eea6121a 6422 s->size = size;
a50b1753 6423 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6424 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6425 return FALSE;
6426
6427 /* Fill in the version definition section. */
6428
6429 p = s->contents;
6430
6431 def.vd_version = VER_DEF_CURRENT;
6432 def.vd_flags = VER_FLG_BASE;
6433 def.vd_ndx = 1;
6434 def.vd_cnt = 1;
3e3b46e5
PB
6435 if (info->create_default_symver)
6436 {
6437 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6438 def.vd_next = sizeof (Elf_External_Verdef);
6439 }
6440 else
6441 {
6442 def.vd_aux = sizeof (Elf_External_Verdef);
6443 def.vd_next = (sizeof (Elf_External_Verdef)
6444 + sizeof (Elf_External_Verdaux));
6445 }
5a580b3a 6446
ef53be89 6447 if (soname_indx != (size_t) -1)
5a580b3a
AM
6448 {
6449 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6450 soname_indx);
6451 def.vd_hash = bfd_elf_hash (soname);
6452 defaux.vda_name = soname_indx;
3e3b46e5 6453 name = soname;
5a580b3a
AM
6454 }
6455 else
6456 {
ef53be89 6457 size_t indx;
5a580b3a 6458
06084812 6459 name = lbasename (output_bfd->filename);
5a580b3a
AM
6460 def.vd_hash = bfd_elf_hash (name);
6461 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6462 name, FALSE);
ef53be89 6463 if (indx == (size_t) -1)
5a580b3a
AM
6464 return FALSE;
6465 defaux.vda_name = indx;
6466 }
6467 defaux.vda_next = 0;
6468
6469 _bfd_elf_swap_verdef_out (output_bfd, &def,
6470 (Elf_External_Verdef *) p);
6471 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6472 if (info->create_default_symver)
6473 {
6474 /* Add a symbol representing this version. */
6475 bh = NULL;
6476 if (! (_bfd_generic_link_add_one_symbol
6477 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6478 0, NULL, FALSE,
6479 get_elf_backend_data (dynobj)->collect, &bh)))
6480 return FALSE;
6481 h = (struct elf_link_hash_entry *) bh;
6482 h->non_elf = 0;
6483 h->def_regular = 1;
6484 h->type = STT_OBJECT;
6485 h->verinfo.vertree = NULL;
6486
6487 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6488 return FALSE;
6489
6490 /* Create a duplicate of the base version with the same
6491 aux block, but different flags. */
6492 def.vd_flags = 0;
6493 def.vd_ndx = 2;
6494 def.vd_aux = sizeof (Elf_External_Verdef);
6495 if (verdefs)
6496 def.vd_next = (sizeof (Elf_External_Verdef)
6497 + sizeof (Elf_External_Verdaux));
6498 else
6499 def.vd_next = 0;
6500 _bfd_elf_swap_verdef_out (output_bfd, &def,
6501 (Elf_External_Verdef *) p);
6502 p += sizeof (Elf_External_Verdef);
6503 }
5a580b3a
AM
6504 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6505 (Elf_External_Verdaux *) p);
6506 p += sizeof (Elf_External_Verdaux);
6507
6508 for (t = verdefs; t != NULL; t = t->next)
6509 {
6510 unsigned int cdeps;
6511 struct bfd_elf_version_deps *n;
5a580b3a 6512
a6cc6b3b
RO
6513 /* Don't emit the base version twice. */
6514 if (t->vernum == 0)
6515 continue;
6516
5a580b3a
AM
6517 cdeps = 0;
6518 for (n = t->deps; n != NULL; n = n->next)
6519 ++cdeps;
6520
6521 /* Add a symbol representing this version. */
6522 bh = NULL;
6523 if (! (_bfd_generic_link_add_one_symbol
6524 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6525 0, NULL, FALSE,
6526 get_elf_backend_data (dynobj)->collect, &bh)))
6527 return FALSE;
6528 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6529 h->non_elf = 0;
6530 h->def_regular = 1;
5a580b3a
AM
6531 h->type = STT_OBJECT;
6532 h->verinfo.vertree = t;
6533
c152c796 6534 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6535 return FALSE;
6536
6537 def.vd_version = VER_DEF_CURRENT;
6538 def.vd_flags = 0;
6539 if (t->globals.list == NULL
6540 && t->locals.list == NULL
6541 && ! t->used)
6542 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6543 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6544 def.vd_cnt = cdeps + 1;
6545 def.vd_hash = bfd_elf_hash (t->name);
6546 def.vd_aux = sizeof (Elf_External_Verdef);
6547 def.vd_next = 0;
a6cc6b3b
RO
6548
6549 /* If a basever node is next, it *must* be the last node in
6550 the chain, otherwise Verdef construction breaks. */
6551 if (t->next != NULL && t->next->vernum == 0)
6552 BFD_ASSERT (t->next->next == NULL);
6553
6554 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6555 def.vd_next = (sizeof (Elf_External_Verdef)
6556 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6557
6558 _bfd_elf_swap_verdef_out (output_bfd, &def,
6559 (Elf_External_Verdef *) p);
6560 p += sizeof (Elf_External_Verdef);
6561
6562 defaux.vda_name = h->dynstr_index;
6563 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6564 h->dynstr_index);
6565 defaux.vda_next = 0;
6566 if (t->deps != NULL)
6567 defaux.vda_next = sizeof (Elf_External_Verdaux);
6568 t->name_indx = defaux.vda_name;
6569
6570 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6571 (Elf_External_Verdaux *) p);
6572 p += sizeof (Elf_External_Verdaux);
6573
6574 for (n = t->deps; n != NULL; n = n->next)
6575 {
6576 if (n->version_needed == NULL)
6577 {
6578 /* This can happen if there was an error in the
6579 version script. */
6580 defaux.vda_name = 0;
6581 }
6582 else
6583 {
6584 defaux.vda_name = n->version_needed->name_indx;
6585 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6586 defaux.vda_name);
6587 }
6588 if (n->next == NULL)
6589 defaux.vda_next = 0;
6590 else
6591 defaux.vda_next = sizeof (Elf_External_Verdaux);
6592
6593 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6594 (Elf_External_Verdaux *) p);
6595 p += sizeof (Elf_External_Verdaux);
6596 }
6597 }
6598
5a580b3a
AM
6599 elf_tdata (output_bfd)->cverdefs = cdefs;
6600 }
902e9fc7
MR
6601 }
6602
6603 bed = get_elf_backend_data (output_bfd);
6604
6605 if (info->gc_sections && bed->can_gc_sections)
6606 {
6607 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6608
6609 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6610 dynamic symbol table. */
902e9fc7
MR
6611 sweep_info.info = info;
6612 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6613 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6614 &sweep_info);
3d13f3e9
AM
6615 }
6616
6617 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6618 {
6619 asection *s;
6620 struct elf_find_verdep_info sinfo;
6621
6622 /* Work out the size of the version reference section. */
6623
6624 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6625 BFD_ASSERT (s != NULL);
902e9fc7 6626
3d13f3e9
AM
6627 sinfo.info = info;
6628 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6629 if (sinfo.vers == 0)
6630 sinfo.vers = 1;
6631 sinfo.failed = FALSE;
6632
6633 elf_link_hash_traverse (elf_hash_table (info),
6634 _bfd_elf_link_find_version_dependencies,
6635 &sinfo);
6636 if (sinfo.failed)
6637 return FALSE;
6638
6639 if (elf_tdata (output_bfd)->verref == NULL)
6640 s->flags |= SEC_EXCLUDE;
6641 else
6642 {
6643 Elf_Internal_Verneed *vn;
6644 unsigned int size;
6645 unsigned int crefs;
6646 bfd_byte *p;
6647
6648 /* Build the version dependency section. */
6649 size = 0;
6650 crefs = 0;
6651 for (vn = elf_tdata (output_bfd)->verref;
6652 vn != NULL;
6653 vn = vn->vn_nextref)
6654 {
6655 Elf_Internal_Vernaux *a;
6656
6657 size += sizeof (Elf_External_Verneed);
6658 ++crefs;
6659 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6660 size += sizeof (Elf_External_Vernaux);
6661 }
6662
6663 s->size = size;
6664 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6665 if (s->contents == NULL)
6666 return FALSE;
6667
6668 p = s->contents;
6669 for (vn = elf_tdata (output_bfd)->verref;
6670 vn != NULL;
6671 vn = vn->vn_nextref)
6672 {
6673 unsigned int caux;
6674 Elf_Internal_Vernaux *a;
6675 size_t indx;
6676
6677 caux = 0;
6678 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6679 ++caux;
6680
6681 vn->vn_version = VER_NEED_CURRENT;
6682 vn->vn_cnt = caux;
6683 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6684 elf_dt_name (vn->vn_bfd) != NULL
6685 ? elf_dt_name (vn->vn_bfd)
6686 : lbasename (vn->vn_bfd->filename),
6687 FALSE);
6688 if (indx == (size_t) -1)
6689 return FALSE;
6690 vn->vn_file = indx;
6691 vn->vn_aux = sizeof (Elf_External_Verneed);
6692 if (vn->vn_nextref == NULL)
6693 vn->vn_next = 0;
6694 else
6695 vn->vn_next = (sizeof (Elf_External_Verneed)
6696 + caux * sizeof (Elf_External_Vernaux));
6697
6698 _bfd_elf_swap_verneed_out (output_bfd, vn,
6699 (Elf_External_Verneed *) p);
6700 p += sizeof (Elf_External_Verneed);
6701
6702 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6703 {
6704 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6705 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6706 a->vna_nodename, FALSE);
6707 if (indx == (size_t) -1)
6708 return FALSE;
6709 a->vna_name = indx;
6710 if (a->vna_nextptr == NULL)
6711 a->vna_next = 0;
6712 else
6713 a->vna_next = sizeof (Elf_External_Vernaux);
6714
6715 _bfd_elf_swap_vernaux_out (output_bfd, a,
6716 (Elf_External_Vernaux *) p);
6717 p += sizeof (Elf_External_Vernaux);
6718 }
6719 }
6720
6721 elf_tdata (output_bfd)->cverrefs = crefs;
6722 }
902e9fc7
MR
6723 }
6724
6725 /* Any syms created from now on start with -1 in
6726 got.refcount/offset and plt.refcount/offset. */
6727 elf_hash_table (info)->init_got_refcount
6728 = elf_hash_table (info)->init_got_offset;
6729 elf_hash_table (info)->init_plt_refcount
6730 = elf_hash_table (info)->init_plt_offset;
6731
6732 if (bfd_link_relocatable (info)
6733 && !_bfd_elf_size_group_sections (info))
6734 return FALSE;
6735
6736 /* The backend may have to create some sections regardless of whether
6737 we're dynamic or not. */
6738 if (bed->elf_backend_always_size_sections
6739 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6740 return FALSE;
6741
6742 /* Determine any GNU_STACK segment requirements, after the backend
6743 has had a chance to set a default segment size. */
6744 if (info->execstack)
6745 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6746 else if (info->noexecstack)
6747 elf_stack_flags (output_bfd) = PF_R | PF_W;
6748 else
6749 {
6750 bfd *inputobj;
6751 asection *notesec = NULL;
6752 int exec = 0;
6753
6754 for (inputobj = info->input_bfds;
6755 inputobj;
6756 inputobj = inputobj->link.next)
6757 {
6758 asection *s;
6759
6760 if (inputobj->flags
6761 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6762 continue;
57963c05
AM
6763 s = inputobj->sections;
6764 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6765 continue;
6766
902e9fc7
MR
6767 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6768 if (s)
6769 {
6770 if (s->flags & SEC_CODE)
6771 exec = PF_X;
6772 notesec = s;
6773 }
6774 else if (bed->default_execstack)
6775 exec = PF_X;
6776 }
6777 if (notesec || info->stacksize > 0)
6778 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6779 if (notesec && exec && bfd_link_relocatable (info)
6780 && notesec->output_section != bfd_abs_section_ptr)
6781 notesec->output_section->flags |= SEC_CODE;
6782 }
6783
6784 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6785 {
6786 struct elf_info_failed eif;
6787 struct elf_link_hash_entry *h;
6788 asection *dynstr;
6789 asection *s;
6790
6791 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6792 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6793
902e9fc7
MR
6794 if (info->symbolic)
6795 {
6796 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6797 return FALSE;
6798 info->flags |= DF_SYMBOLIC;
6799 }
6800
6801 if (rpath != NULL)
6802 {
6803 size_t indx;
6804 bfd_vma tag;
6805
6806 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6807 TRUE);
6808 if (indx == (size_t) -1)
6809 return FALSE;
6810
6811 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6812 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6813 return FALSE;
6814 }
6815
6816 if (filter_shlib != NULL)
6817 {
6818 size_t indx;
6819
6820 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6821 filter_shlib, TRUE);
6822 if (indx == (size_t) -1
6823 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6824 return FALSE;
6825 }
6826
6827 if (auxiliary_filters != NULL)
6828 {
6829 const char * const *p;
6830
6831 for (p = auxiliary_filters; *p != NULL; p++)
6832 {
6833 size_t indx;
6834
6835 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6836 *p, TRUE);
6837 if (indx == (size_t) -1
6838 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6839 return FALSE;
6840 }
6841 }
6842
6843 if (audit != NULL)
6844 {
6845 size_t indx;
6846
6847 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6848 TRUE);
6849 if (indx == (size_t) -1
6850 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6851 return FALSE;
6852 }
6853
6854 if (depaudit != NULL)
6855 {
6856 size_t indx;
6857
6858 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6859 TRUE);
6860 if (indx == (size_t) -1
6861 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6862 return FALSE;
6863 }
6864
6865 eif.info = info;
6866 eif.failed = FALSE;
6867
6868 /* Find all symbols which were defined in a dynamic object and make
6869 the backend pick a reasonable value for them. */
6870 elf_link_hash_traverse (elf_hash_table (info),
6871 _bfd_elf_adjust_dynamic_symbol,
6872 &eif);
6873 if (eif.failed)
6874 return FALSE;
6875
6876 /* Add some entries to the .dynamic section. We fill in some of the
6877 values later, in bfd_elf_final_link, but we must add the entries
6878 now so that we know the final size of the .dynamic section. */
6879
6880 /* If there are initialization and/or finalization functions to
6881 call then add the corresponding DT_INIT/DT_FINI entries. */
6882 h = (info->init_function
6883 ? elf_link_hash_lookup (elf_hash_table (info),
6884 info->init_function, FALSE,
6885 FALSE, FALSE)
6886 : NULL);
6887 if (h != NULL
6888 && (h->ref_regular
6889 || h->def_regular))
6890 {
6891 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6892 return FALSE;
6893 }
6894 h = (info->fini_function
6895 ? elf_link_hash_lookup (elf_hash_table (info),
6896 info->fini_function, FALSE,
6897 FALSE, FALSE)
6898 : NULL);
6899 if (h != NULL
6900 && (h->ref_regular
6901 || h->def_regular))
6902 {
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6904 return FALSE;
6905 }
6906
6907 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6908 if (s != NULL && s->linker_has_input)
6909 {
6910 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6911 if (! bfd_link_executable (info))
6912 {
6913 bfd *sub;
6914 asection *o;
6915
57963c05
AM
6916 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6917 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6918 && (o = sub->sections) != NULL
6919 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
6920 for (o = sub->sections; o != NULL; o = o->next)
6921 if (elf_section_data (o)->this_hdr.sh_type
6922 == SHT_PREINIT_ARRAY)
6923 {
6924 _bfd_error_handler
871b3ab2 6925 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
6926 sub);
6927 break;
6928 }
6929
6930 bfd_set_error (bfd_error_nonrepresentable_section);
6931 return FALSE;
6932 }
6933
6934 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6935 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6936 return FALSE;
6937 }
6938 s = bfd_get_section_by_name (output_bfd, ".init_array");
6939 if (s != NULL && s->linker_has_input)
6940 {
6941 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6942 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6943 return FALSE;
6944 }
6945 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6946 if (s != NULL && s->linker_has_input)
6947 {
6948 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6949 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6950 return FALSE;
6951 }
6952
6953 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6954 /* If .dynstr is excluded from the link, we don't want any of
6955 these tags. Strictly, we should be checking each section
6956 individually; This quick check covers for the case where
6957 someone does a /DISCARD/ : { *(*) }. */
6958 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6959 {
6960 bfd_size_type strsize;
6961
6962 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6963 if ((info->emit_hash
6964 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6965 || (info->emit_gnu_hash
6966 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6967 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6968 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6969 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6970 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6971 bed->s->sizeof_sym))
6972 return FALSE;
6973 }
6974 }
6975
6976 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6977 return FALSE;
6978
6979 /* The backend must work out the sizes of all the other dynamic
6980 sections. */
6981 if (dynobj != NULL
6982 && bed->elf_backend_size_dynamic_sections != NULL
6983 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6984 return FALSE;
6985
6986 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6987 {
902e9fc7
MR
6988 if (elf_tdata (output_bfd)->cverdefs)
6989 {
6990 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6991
6992 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6993 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6994 return FALSE;
6995 }
6996
6997 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6998 {
6999 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7000 return FALSE;
7001 }
7002 else if (info->flags & DF_BIND_NOW)
7003 {
7004 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7005 return FALSE;
7006 }
7007
7008 if (info->flags_1)
7009 {
7010 if (bfd_link_executable (info))
7011 info->flags_1 &= ~ (DF_1_INITFIRST
7012 | DF_1_NODELETE
7013 | DF_1_NOOPEN);
7014 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7015 return FALSE;
7016 }
7017
7018 if (elf_tdata (output_bfd)->cverrefs)
7019 {
7020 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7021
7022 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7023 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7024 return FALSE;
7025 }
5a580b3a 7026
8423293d
AM
7027 if ((elf_tdata (output_bfd)->cverrefs == 0
7028 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7029 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7030 {
902e9fc7
MR
7031 asection *s;
7032
3d4d4302 7033 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7034 s->flags |= SEC_EXCLUDE;
7035 }
7036 }
7037 return TRUE;
7038}
7039
74541ad4
AM
7040/* Find the first non-excluded output section. We'll use its
7041 section symbol for some emitted relocs. */
7042void
7043_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7044{
7045 asection *s;
7046
7047 for (s = output_bfd->sections; s != NULL; s = s->next)
7048 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7049 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4
AM
7050 {
7051 elf_hash_table (info)->text_index_section = s;
7052 break;
7053 }
7054}
7055
7056/* Find two non-excluded output sections, one for code, one for data.
7057 We'll use their section symbols for some emitted relocs. */
7058void
7059_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7060{
7061 asection *s;
7062
266b05cf 7063 /* Data first, since setting text_index_section changes
7f923b7f 7064 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7065 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 7066 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
d00dd7dc 7067 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7068 {
266b05cf 7069 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
7070 break;
7071 }
7072
7073 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
7074 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7075 == (SEC_ALLOC | SEC_READONLY))
d00dd7dc 7076 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7077 {
266b05cf 7078 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
7079 break;
7080 }
7081
7082 if (elf_hash_table (info)->text_index_section == NULL)
7083 elf_hash_table (info)->text_index_section
7084 = elf_hash_table (info)->data_index_section;
7085}
7086
8423293d
AM
7087bfd_boolean
7088bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7089{
74541ad4 7090 const struct elf_backend_data *bed;
23ec1e32 7091 unsigned long section_sym_count;
96d01d93 7092 bfd_size_type dynsymcount = 0;
74541ad4 7093
8423293d
AM
7094 if (!is_elf_hash_table (info->hash))
7095 return TRUE;
7096
74541ad4
AM
7097 bed = get_elf_backend_data (output_bfd);
7098 (*bed->elf_backend_init_index_section) (output_bfd, info);
7099
23ec1e32
MR
7100 /* Assign dynsym indices. In a shared library we generate a section
7101 symbol for each output section, which come first. Next come all
7102 of the back-end allocated local dynamic syms, followed by the rest
7103 of the global symbols.
7104
7105 This is usually not needed for static binaries, however backends
7106 can request to always do it, e.g. the MIPS backend uses dynamic
7107 symbol counts to lay out GOT, which will be produced in the
7108 presence of GOT relocations even in static binaries (holding fixed
7109 data in that case, to satisfy those relocations). */
7110
7111 if (elf_hash_table (info)->dynamic_sections_created
7112 || bed->always_renumber_dynsyms)
7113 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7114 &section_sym_count);
7115
8423293d
AM
7116 if (elf_hash_table (info)->dynamic_sections_created)
7117 {
7118 bfd *dynobj;
8423293d 7119 asection *s;
8423293d
AM
7120 unsigned int dtagcount;
7121
7122 dynobj = elf_hash_table (info)->dynobj;
7123
5a580b3a 7124 /* Work out the size of the symbol version section. */
3d4d4302 7125 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7126 BFD_ASSERT (s != NULL);
d5486c43 7127 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7128 {
eea6121a 7129 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7130 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7131 if (s->contents == NULL)
7132 return FALSE;
7133
7134 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7135 return FALSE;
7136 }
7137
7138 /* Set the size of the .dynsym and .hash sections. We counted
7139 the number of dynamic symbols in elf_link_add_object_symbols.
7140 We will build the contents of .dynsym and .hash when we build
7141 the final symbol table, because until then we do not know the
7142 correct value to give the symbols. We built the .dynstr
7143 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7144 s = elf_hash_table (info)->dynsym;
5a580b3a 7145 BFD_ASSERT (s != NULL);
eea6121a 7146 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7147
d5486c43
L
7148 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7149 if (s->contents == NULL)
7150 return FALSE;
5a580b3a 7151
d5486c43
L
7152 /* The first entry in .dynsym is a dummy symbol. Clear all the
7153 section syms, in case we don't output them all. */
7154 ++section_sym_count;
7155 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7156
fdc90cb4
JJ
7157 elf_hash_table (info)->bucketcount = 0;
7158
5a580b3a
AM
7159 /* Compute the size of the hashing table. As a side effect this
7160 computes the hash values for all the names we export. */
fdc90cb4
JJ
7161 if (info->emit_hash)
7162 {
7163 unsigned long int *hashcodes;
14b1c01e 7164 struct hash_codes_info hashinf;
fdc90cb4
JJ
7165 bfd_size_type amt;
7166 unsigned long int nsyms;
7167 size_t bucketcount;
7168 size_t hash_entry_size;
7169
7170 /* Compute the hash values for all exported symbols. At the same
7171 time store the values in an array so that we could use them for
7172 optimizations. */
7173 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7174 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7175 if (hashcodes == NULL)
7176 return FALSE;
14b1c01e
AM
7177 hashinf.hashcodes = hashcodes;
7178 hashinf.error = FALSE;
5a580b3a 7179
fdc90cb4
JJ
7180 /* Put all hash values in HASHCODES. */
7181 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7182 elf_collect_hash_codes, &hashinf);
7183 if (hashinf.error)
4dd07732
AM
7184 {
7185 free (hashcodes);
7186 return FALSE;
7187 }
5a580b3a 7188
14b1c01e 7189 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7190 bucketcount
7191 = compute_bucket_count (info, hashcodes, nsyms, 0);
7192 free (hashcodes);
7193
4b48e2f6 7194 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7195 return FALSE;
5a580b3a 7196
fdc90cb4
JJ
7197 elf_hash_table (info)->bucketcount = bucketcount;
7198
3d4d4302 7199 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7200 BFD_ASSERT (s != NULL);
7201 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7202 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7203 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7204 if (s->contents == NULL)
7205 return FALSE;
7206
7207 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7208 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7209 s->contents + hash_entry_size);
7210 }
7211
7212 if (info->emit_gnu_hash)
7213 {
7214 size_t i, cnt;
7215 unsigned char *contents;
7216 struct collect_gnu_hash_codes cinfo;
7217 bfd_size_type amt;
7218 size_t bucketcount;
7219
7220 memset (&cinfo, 0, sizeof (cinfo));
7221
7222 /* Compute the hash values for all exported symbols. At the same
7223 time store the values in an array so that we could use them for
7224 optimizations. */
7225 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7226 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7227 if (cinfo.hashcodes == NULL)
7228 return FALSE;
7229
7230 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7231 cinfo.min_dynindx = -1;
7232 cinfo.output_bfd = output_bfd;
7233 cinfo.bed = bed;
7234
7235 /* Put all hash values in HASHCODES. */
7236 elf_link_hash_traverse (elf_hash_table (info),
7237 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7238 if (cinfo.error)
4dd07732
AM
7239 {
7240 free (cinfo.hashcodes);
7241 return FALSE;
7242 }
fdc90cb4
JJ
7243
7244 bucketcount
7245 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7246
7247 if (bucketcount == 0)
7248 {
7249 free (cinfo.hashcodes);
7250 return FALSE;
7251 }
7252
3d4d4302 7253 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
7254 BFD_ASSERT (s != NULL);
7255
7256 if (cinfo.nsyms == 0)
7257 {
7258 /* Empty .gnu.hash section is special. */
7259 BFD_ASSERT (cinfo.min_dynindx == -1);
7260 free (cinfo.hashcodes);
7261 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7262 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7263 if (contents == NULL)
7264 return FALSE;
7265 s->contents = contents;
7266 /* 1 empty bucket. */
7267 bfd_put_32 (output_bfd, 1, contents);
7268 /* SYMIDX above the special symbol 0. */
7269 bfd_put_32 (output_bfd, 1, contents + 4);
7270 /* Just one word for bitmask. */
7271 bfd_put_32 (output_bfd, 1, contents + 8);
7272 /* Only hash fn bloom filter. */
7273 bfd_put_32 (output_bfd, 0, contents + 12);
7274 /* No hashes are valid - empty bitmask. */
7275 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7276 /* No hashes in the only bucket. */
7277 bfd_put_32 (output_bfd, 0,
7278 contents + 16 + bed->s->arch_size / 8);
7279 }
7280 else
7281 {
9e6619e2 7282 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7283 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7284
9e6619e2
AM
7285 x = cinfo.nsyms;
7286 maskbitslog2 = 1;
7287 while ((x >>= 1) != 0)
7288 ++maskbitslog2;
fdc90cb4
JJ
7289 if (maskbitslog2 < 3)
7290 maskbitslog2 = 5;
7291 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7292 maskbitslog2 = maskbitslog2 + 3;
7293 else
7294 maskbitslog2 = maskbitslog2 + 2;
7295 if (bed->s->arch_size == 64)
7296 {
7297 if (maskbitslog2 == 5)
7298 maskbitslog2 = 6;
7299 cinfo.shift1 = 6;
7300 }
7301 else
7302 cinfo.shift1 = 5;
7303 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7304 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7305 cinfo.maskbits = 1 << maskbitslog2;
7306 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7307 amt = bucketcount * sizeof (unsigned long int) * 2;
7308 amt += maskwords * sizeof (bfd_vma);
a50b1753 7309 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7310 if (cinfo.bitmask == NULL)
7311 {
7312 free (cinfo.hashcodes);
7313 return FALSE;
7314 }
7315
a50b1753 7316 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7317 cinfo.indx = cinfo.counts + bucketcount;
7318 cinfo.symindx = dynsymcount - cinfo.nsyms;
7319 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7320
7321 /* Determine how often each hash bucket is used. */
7322 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7323 for (i = 0; i < cinfo.nsyms; ++i)
7324 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7325
7326 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7327 if (cinfo.counts[i] != 0)
7328 {
7329 cinfo.indx[i] = cnt;
7330 cnt += cinfo.counts[i];
7331 }
7332 BFD_ASSERT (cnt == dynsymcount);
7333 cinfo.bucketcount = bucketcount;
7334 cinfo.local_indx = cinfo.min_dynindx;
7335
7336 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7337 s->size += cinfo.maskbits / 8;
a50b1753 7338 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7339 if (contents == NULL)
7340 {
7341 free (cinfo.bitmask);
7342 free (cinfo.hashcodes);
7343 return FALSE;
7344 }
7345
7346 s->contents = contents;
7347 bfd_put_32 (output_bfd, bucketcount, contents);
7348 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7349 bfd_put_32 (output_bfd, maskwords, contents + 8);
7350 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7351 contents += 16 + cinfo.maskbits / 8;
7352
7353 for (i = 0; i < bucketcount; ++i)
7354 {
7355 if (cinfo.counts[i] == 0)
7356 bfd_put_32 (output_bfd, 0, contents);
7357 else
7358 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7359 contents += 4;
7360 }
7361
7362 cinfo.contents = contents;
7363
7364 /* Renumber dynamic symbols, populate .gnu.hash section. */
7365 elf_link_hash_traverse (elf_hash_table (info),
7366 elf_renumber_gnu_hash_syms, &cinfo);
7367
7368 contents = s->contents + 16;
7369 for (i = 0; i < maskwords; ++i)
7370 {
7371 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7372 contents);
7373 contents += bed->s->arch_size / 8;
7374 }
7375
7376 free (cinfo.bitmask);
7377 free (cinfo.hashcodes);
7378 }
7379 }
5a580b3a 7380
3d4d4302 7381 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7382 BFD_ASSERT (s != NULL);
7383
4ad4eba5 7384 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7385
eea6121a 7386 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7387
7388 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7389 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7390 return FALSE;
7391 }
7392
7393 return TRUE;
7394}
4d269e42 7395\f
4d269e42
AM
7396/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7397
7398static void
7399merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7400 asection *sec)
7401{
dbaa2011
AM
7402 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7403 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7404}
7405
7406/* Finish SHF_MERGE section merging. */
7407
7408bfd_boolean
630993ec 7409_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7410{
7411 bfd *ibfd;
7412 asection *sec;
7413
7414 if (!is_elf_hash_table (info->hash))
7415 return FALSE;
7416
c72f2fb2 7417 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7418 if ((ibfd->flags & DYNAMIC) == 0
7419 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7420 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7421 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7422 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7423 if ((sec->flags & SEC_MERGE) != 0
7424 && !bfd_is_abs_section (sec->output_section))
7425 {
7426 struct bfd_elf_section_data *secdata;
7427
7428 secdata = elf_section_data (sec);
630993ec 7429 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7430 &elf_hash_table (info)->merge_info,
7431 sec, &secdata->sec_info))
7432 return FALSE;
7433 else if (secdata->sec_info)
dbaa2011 7434 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7435 }
7436
7437 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7438 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7439 merge_sections_remove_hook);
7440 return TRUE;
7441}
7442
7443/* Create an entry in an ELF linker hash table. */
7444
7445struct bfd_hash_entry *
7446_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7447 struct bfd_hash_table *table,
7448 const char *string)
7449{
7450 /* Allocate the structure if it has not already been allocated by a
7451 subclass. */
7452 if (entry == NULL)
7453 {
a50b1753 7454 entry = (struct bfd_hash_entry *)
ca4be51c 7455 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7456 if (entry == NULL)
7457 return entry;
7458 }
7459
7460 /* Call the allocation method of the superclass. */
7461 entry = _bfd_link_hash_newfunc (entry, table, string);
7462 if (entry != NULL)
7463 {
7464 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7465 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7466
7467 /* Set local fields. */
7468 ret->indx = -1;
7469 ret->dynindx = -1;
7470 ret->got = htab->init_got_refcount;
7471 ret->plt = htab->init_plt_refcount;
7472 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7473 - offsetof (struct elf_link_hash_entry, size)));
7474 /* Assume that we have been called by a non-ELF symbol reader.
7475 This flag is then reset by the code which reads an ELF input
7476 file. This ensures that a symbol created by a non-ELF symbol
7477 reader will have the flag set correctly. */
7478 ret->non_elf = 1;
7479 }
7480
7481 return entry;
7482}
7483
7484/* Copy data from an indirect symbol to its direct symbol, hiding the
7485 old indirect symbol. Also used for copying flags to a weakdef. */
7486
7487void
7488_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7489 struct elf_link_hash_entry *dir,
7490 struct elf_link_hash_entry *ind)
7491{
7492 struct elf_link_hash_table *htab;
7493
7494 /* Copy down any references that we may have already seen to the
e81830c5 7495 symbol which just became indirect. */
4d269e42 7496
422f1182 7497 if (dir->versioned != versioned_hidden)
e81830c5
AM
7498 dir->ref_dynamic |= ind->ref_dynamic;
7499 dir->ref_regular |= ind->ref_regular;
7500 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7501 dir->non_got_ref |= ind->non_got_ref;
7502 dir->needs_plt |= ind->needs_plt;
7503 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7504
7505 if (ind->root.type != bfd_link_hash_indirect)
7506 return;
7507
7508 /* Copy over the global and procedure linkage table refcount entries.
7509 These may have been already set up by a check_relocs routine. */
7510 htab = elf_hash_table (info);
7511 if (ind->got.refcount > htab->init_got_refcount.refcount)
7512 {
7513 if (dir->got.refcount < 0)
7514 dir->got.refcount = 0;
7515 dir->got.refcount += ind->got.refcount;
7516 ind->got.refcount = htab->init_got_refcount.refcount;
7517 }
7518
7519 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7520 {
7521 if (dir->plt.refcount < 0)
7522 dir->plt.refcount = 0;
7523 dir->plt.refcount += ind->plt.refcount;
7524 ind->plt.refcount = htab->init_plt_refcount.refcount;
7525 }
7526
7527 if (ind->dynindx != -1)
7528 {
7529 if (dir->dynindx != -1)
7530 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7531 dir->dynindx = ind->dynindx;
7532 dir->dynstr_index = ind->dynstr_index;
7533 ind->dynindx = -1;
7534 ind->dynstr_index = 0;
7535 }
7536}
7537
7538void
7539_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7540 struct elf_link_hash_entry *h,
7541 bfd_boolean force_local)
7542{
3aa14d16
L
7543 /* STT_GNU_IFUNC symbol must go through PLT. */
7544 if (h->type != STT_GNU_IFUNC)
7545 {
7546 h->plt = elf_hash_table (info)->init_plt_offset;
7547 h->needs_plt = 0;
7548 }
4d269e42
AM
7549 if (force_local)
7550 {
7551 h->forced_local = 1;
7552 if (h->dynindx != -1)
7553 {
4d269e42
AM
7554 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7555 h->dynstr_index);
641338d8
AM
7556 h->dynindx = -1;
7557 h->dynstr_index = 0;
4d269e42
AM
7558 }
7559 }
7560}
7561
34a87bb0
L
7562/* Hide a symbol. */
7563
7564void
7565_bfd_elf_link_hide_symbol (bfd *output_bfd,
7566 struct bfd_link_info *info,
7567 struct bfd_link_hash_entry *h)
7568{
7569 if (is_elf_hash_table (info->hash))
7570 {
7571 const struct elf_backend_data *bed
7572 = get_elf_backend_data (output_bfd);
7573 struct elf_link_hash_entry *eh
7574 = (struct elf_link_hash_entry *) h;
7575 bed->elf_backend_hide_symbol (info, eh, TRUE);
7576 eh->def_dynamic = 0;
7577 eh->ref_dynamic = 0;
7578 eh->dynamic_def = 0;
7579 }
7580}
7581
7bf52ea2
AM
7582/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7583 caller. */
4d269e42
AM
7584
7585bfd_boolean
7586_bfd_elf_link_hash_table_init
7587 (struct elf_link_hash_table *table,
7588 bfd *abfd,
7589 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7590 struct bfd_hash_table *,
7591 const char *),
4dfe6ac6
NC
7592 unsigned int entsize,
7593 enum elf_target_id target_id)
4d269e42
AM
7594{
7595 bfd_boolean ret;
7596 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7597
4d269e42
AM
7598 table->init_got_refcount.refcount = can_refcount - 1;
7599 table->init_plt_refcount.refcount = can_refcount - 1;
7600 table->init_got_offset.offset = -(bfd_vma) 1;
7601 table->init_plt_offset.offset = -(bfd_vma) 1;
7602 /* The first dynamic symbol is a dummy. */
7603 table->dynsymcount = 1;
7604
7605 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7606
4d269e42 7607 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7608 table->hash_table_id = target_id;
4d269e42
AM
7609
7610 return ret;
7611}
7612
7613/* Create an ELF linker hash table. */
7614
7615struct bfd_link_hash_table *
7616_bfd_elf_link_hash_table_create (bfd *abfd)
7617{
7618 struct elf_link_hash_table *ret;
7619 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7620
7bf52ea2 7621 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7622 if (ret == NULL)
7623 return NULL;
7624
7625 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7626 sizeof (struct elf_link_hash_entry),
7627 GENERIC_ELF_DATA))
4d269e42
AM
7628 {
7629 free (ret);
7630 return NULL;
7631 }
d495ab0d 7632 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7633
7634 return &ret->root;
7635}
7636
9f7c3e5e
AM
7637/* Destroy an ELF linker hash table. */
7638
7639void
d495ab0d 7640_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7641{
d495ab0d
AM
7642 struct elf_link_hash_table *htab;
7643
7644 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7645 if (htab->dynstr != NULL)
7646 _bfd_elf_strtab_free (htab->dynstr);
7647 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7648 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7649}
7650
4d269e42
AM
7651/* This is a hook for the ELF emulation code in the generic linker to
7652 tell the backend linker what file name to use for the DT_NEEDED
7653 entry for a dynamic object. */
7654
7655void
7656bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7657{
7658 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7659 && bfd_get_format (abfd) == bfd_object)
7660 elf_dt_name (abfd) = name;
7661}
7662
7663int
7664bfd_elf_get_dyn_lib_class (bfd *abfd)
7665{
7666 int lib_class;
7667 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7668 && bfd_get_format (abfd) == bfd_object)
7669 lib_class = elf_dyn_lib_class (abfd);
7670 else
7671 lib_class = 0;
7672 return lib_class;
7673}
7674
7675void
7676bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7677{
7678 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7679 && bfd_get_format (abfd) == bfd_object)
7680 elf_dyn_lib_class (abfd) = lib_class;
7681}
7682
7683/* Get the list of DT_NEEDED entries for a link. This is a hook for
7684 the linker ELF emulation code. */
7685
7686struct bfd_link_needed_list *
7687bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7688 struct bfd_link_info *info)
7689{
7690 if (! is_elf_hash_table (info->hash))
7691 return NULL;
7692 return elf_hash_table (info)->needed;
7693}
7694
7695/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7696 hook for the linker ELF emulation code. */
7697
7698struct bfd_link_needed_list *
7699bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7700 struct bfd_link_info *info)
7701{
7702 if (! is_elf_hash_table (info->hash))
7703 return NULL;
7704 return elf_hash_table (info)->runpath;
7705}
7706
7707/* Get the name actually used for a dynamic object for a link. This
7708 is the SONAME entry if there is one. Otherwise, it is the string
7709 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7710
7711const char *
7712bfd_elf_get_dt_soname (bfd *abfd)
7713{
7714 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7715 && bfd_get_format (abfd) == bfd_object)
7716 return elf_dt_name (abfd);
7717 return NULL;
7718}
7719
7720/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7721 the ELF linker emulation code. */
7722
7723bfd_boolean
7724bfd_elf_get_bfd_needed_list (bfd *abfd,
7725 struct bfd_link_needed_list **pneeded)
7726{
7727 asection *s;
7728 bfd_byte *dynbuf = NULL;
cb33740c 7729 unsigned int elfsec;
4d269e42
AM
7730 unsigned long shlink;
7731 bfd_byte *extdyn, *extdynend;
7732 size_t extdynsize;
7733 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7734
7735 *pneeded = NULL;
7736
7737 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7738 || bfd_get_format (abfd) != bfd_object)
7739 return TRUE;
7740
7741 s = bfd_get_section_by_name (abfd, ".dynamic");
7742 if (s == NULL || s->size == 0)
7743 return TRUE;
7744
7745 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7746 goto error_return;
7747
7748 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7749 if (elfsec == SHN_BAD)
4d269e42
AM
7750 goto error_return;
7751
7752 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7753
4d269e42
AM
7754 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7755 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7756
7757 extdyn = dynbuf;
7758 extdynend = extdyn + s->size;
7759 for (; extdyn < extdynend; extdyn += extdynsize)
7760 {
7761 Elf_Internal_Dyn dyn;
7762
7763 (*swap_dyn_in) (abfd, extdyn, &dyn);
7764
7765 if (dyn.d_tag == DT_NULL)
7766 break;
7767
7768 if (dyn.d_tag == DT_NEEDED)
7769 {
7770 const char *string;
7771 struct bfd_link_needed_list *l;
7772 unsigned int tagv = dyn.d_un.d_val;
7773 bfd_size_type amt;
7774
7775 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7776 if (string == NULL)
7777 goto error_return;
7778
7779 amt = sizeof *l;
a50b1753 7780 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7781 if (l == NULL)
7782 goto error_return;
7783
7784 l->by = abfd;
7785 l->name = string;
7786 l->next = *pneeded;
7787 *pneeded = l;
7788 }
7789 }
7790
7791 free (dynbuf);
7792
7793 return TRUE;
7794
7795 error_return:
7796 if (dynbuf != NULL)
7797 free (dynbuf);
7798 return FALSE;
7799}
7800
7801struct elf_symbuf_symbol
7802{
7803 unsigned long st_name; /* Symbol name, index in string tbl */
7804 unsigned char st_info; /* Type and binding attributes */
7805 unsigned char st_other; /* Visibilty, and target specific */
7806};
7807
7808struct elf_symbuf_head
7809{
7810 struct elf_symbuf_symbol *ssym;
ef53be89 7811 size_t count;
4d269e42
AM
7812 unsigned int st_shndx;
7813};
7814
7815struct elf_symbol
7816{
7817 union
7818 {
7819 Elf_Internal_Sym *isym;
7820 struct elf_symbuf_symbol *ssym;
7821 } u;
7822 const char *name;
7823};
7824
7825/* Sort references to symbols by ascending section number. */
7826
7827static int
7828elf_sort_elf_symbol (const void *arg1, const void *arg2)
7829{
7830 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7831 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7832
7833 return s1->st_shndx - s2->st_shndx;
7834}
7835
7836static int
7837elf_sym_name_compare (const void *arg1, const void *arg2)
7838{
7839 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7840 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7841 return strcmp (s1->name, s2->name);
7842}
7843
7844static struct elf_symbuf_head *
ef53be89 7845elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7846{
14b1c01e 7847 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7848 struct elf_symbuf_symbol *ssym;
7849 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7850 size_t i, shndx_count, total_size;
4d269e42 7851
a50b1753 7852 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7853 if (indbuf == NULL)
7854 return NULL;
7855
7856 for (ind = indbuf, i = 0; i < symcount; i++)
7857 if (isymbuf[i].st_shndx != SHN_UNDEF)
7858 *ind++ = &isymbuf[i];
7859 indbufend = ind;
7860
7861 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7862 elf_sort_elf_symbol);
7863
7864 shndx_count = 0;
7865 if (indbufend > indbuf)
7866 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7867 if (ind[0]->st_shndx != ind[1]->st_shndx)
7868 shndx_count++;
7869
3ae181ee
L
7870 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7871 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7872 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7873 if (ssymbuf == NULL)
7874 {
7875 free (indbuf);
7876 return NULL;
7877 }
7878
3ae181ee 7879 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7880 ssymbuf->ssym = NULL;
7881 ssymbuf->count = shndx_count;
7882 ssymbuf->st_shndx = 0;
7883 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7884 {
7885 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7886 {
7887 ssymhead++;
7888 ssymhead->ssym = ssym;
7889 ssymhead->count = 0;
7890 ssymhead->st_shndx = (*ind)->st_shndx;
7891 }
7892 ssym->st_name = (*ind)->st_name;
7893 ssym->st_info = (*ind)->st_info;
7894 ssym->st_other = (*ind)->st_other;
7895 ssymhead->count++;
7896 }
ef53be89 7897 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7898 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7899 == total_size));
4d269e42
AM
7900
7901 free (indbuf);
7902 return ssymbuf;
7903}
7904
7905/* Check if 2 sections define the same set of local and global
7906 symbols. */
7907
8f317e31 7908static bfd_boolean
4d269e42
AM
7909bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7910 struct bfd_link_info *info)
7911{
7912 bfd *bfd1, *bfd2;
7913 const struct elf_backend_data *bed1, *bed2;
7914 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7915 size_t symcount1, symcount2;
4d269e42
AM
7916 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7917 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7918 Elf_Internal_Sym *isym, *isymend;
7919 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7920 size_t count1, count2, i;
cb33740c 7921 unsigned int shndx1, shndx2;
4d269e42
AM
7922 bfd_boolean result;
7923
7924 bfd1 = sec1->owner;
7925 bfd2 = sec2->owner;
7926
4d269e42
AM
7927 /* Both sections have to be in ELF. */
7928 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7929 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7930 return FALSE;
7931
7932 if (elf_section_type (sec1) != elf_section_type (sec2))
7933 return FALSE;
7934
4d269e42
AM
7935 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7936 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7937 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7938 return FALSE;
7939
7940 bed1 = get_elf_backend_data (bfd1);
7941 bed2 = get_elf_backend_data (bfd2);
7942 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7943 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7944 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7945 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7946
7947 if (symcount1 == 0 || symcount2 == 0)
7948 return FALSE;
7949
7950 result = FALSE;
7951 isymbuf1 = NULL;
7952 isymbuf2 = NULL;
a50b1753
NC
7953 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7954 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7955
7956 if (ssymbuf1 == NULL)
7957 {
7958 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7959 NULL, NULL, NULL);
7960 if (isymbuf1 == NULL)
7961 goto done;
7962
7963 if (!info->reduce_memory_overheads)
7964 elf_tdata (bfd1)->symbuf = ssymbuf1
7965 = elf_create_symbuf (symcount1, isymbuf1);
7966 }
7967
7968 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7969 {
7970 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7971 NULL, NULL, NULL);
7972 if (isymbuf2 == NULL)
7973 goto done;
7974
7975 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7976 elf_tdata (bfd2)->symbuf = ssymbuf2
7977 = elf_create_symbuf (symcount2, isymbuf2);
7978 }
7979
7980 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7981 {
7982 /* Optimized faster version. */
ef53be89 7983 size_t lo, hi, mid;
4d269e42
AM
7984 struct elf_symbol *symp;
7985 struct elf_symbuf_symbol *ssym, *ssymend;
7986
7987 lo = 0;
7988 hi = ssymbuf1->count;
7989 ssymbuf1++;
7990 count1 = 0;
7991 while (lo < hi)
7992 {
7993 mid = (lo + hi) / 2;
cb33740c 7994 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7995 hi = mid;
cb33740c 7996 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7997 lo = mid + 1;
7998 else
7999 {
8000 count1 = ssymbuf1[mid].count;
8001 ssymbuf1 += mid;
8002 break;
8003 }
8004 }
8005
8006 lo = 0;
8007 hi = ssymbuf2->count;
8008 ssymbuf2++;
8009 count2 = 0;
8010 while (lo < hi)
8011 {
8012 mid = (lo + hi) / 2;
cb33740c 8013 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8014 hi = mid;
cb33740c 8015 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8016 lo = mid + 1;
8017 else
8018 {
8019 count2 = ssymbuf2[mid].count;
8020 ssymbuf2 += mid;
8021 break;
8022 }
8023 }
8024
8025 if (count1 == 0 || count2 == 0 || count1 != count2)
8026 goto done;
8027
ca4be51c
AM
8028 symtable1
8029 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8030 symtable2
8031 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8032 if (symtable1 == NULL || symtable2 == NULL)
8033 goto done;
8034
8035 symp = symtable1;
8036 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8037 ssym < ssymend; ssym++, symp++)
8038 {
8039 symp->u.ssym = ssym;
8040 symp->name = bfd_elf_string_from_elf_section (bfd1,
8041 hdr1->sh_link,
8042 ssym->st_name);
8043 }
8044
8045 symp = symtable2;
8046 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8047 ssym < ssymend; ssym++, symp++)
8048 {
8049 symp->u.ssym = ssym;
8050 symp->name = bfd_elf_string_from_elf_section (bfd2,
8051 hdr2->sh_link,
8052 ssym->st_name);
8053 }
8054
8055 /* Sort symbol by name. */
8056 qsort (symtable1, count1, sizeof (struct elf_symbol),
8057 elf_sym_name_compare);
8058 qsort (symtable2, count1, sizeof (struct elf_symbol),
8059 elf_sym_name_compare);
8060
8061 for (i = 0; i < count1; i++)
8062 /* Two symbols must have the same binding, type and name. */
8063 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8064 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8065 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8066 goto done;
8067
8068 result = TRUE;
8069 goto done;
8070 }
8071
a50b1753
NC
8072 symtable1 = (struct elf_symbol *)
8073 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8074 symtable2 = (struct elf_symbol *)
8075 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8076 if (symtable1 == NULL || symtable2 == NULL)
8077 goto done;
8078
8079 /* Count definitions in the section. */
8080 count1 = 0;
8081 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8082 if (isym->st_shndx == shndx1)
4d269e42
AM
8083 symtable1[count1++].u.isym = isym;
8084
8085 count2 = 0;
8086 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8087 if (isym->st_shndx == shndx2)
4d269e42
AM
8088 symtable2[count2++].u.isym = isym;
8089
8090 if (count1 == 0 || count2 == 0 || count1 != count2)
8091 goto done;
8092
8093 for (i = 0; i < count1; i++)
8094 symtable1[i].name
8095 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8096 symtable1[i].u.isym->st_name);
8097
8098 for (i = 0; i < count2; i++)
8099 symtable2[i].name
8100 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8101 symtable2[i].u.isym->st_name);
8102
8103 /* Sort symbol by name. */
8104 qsort (symtable1, count1, sizeof (struct elf_symbol),
8105 elf_sym_name_compare);
8106 qsort (symtable2, count1, sizeof (struct elf_symbol),
8107 elf_sym_name_compare);
8108
8109 for (i = 0; i < count1; i++)
8110 /* Two symbols must have the same binding, type and name. */
8111 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8112 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8113 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8114 goto done;
8115
8116 result = TRUE;
8117
8118done:
8119 if (symtable1)
8120 free (symtable1);
8121 if (symtable2)
8122 free (symtable2);
8123 if (isymbuf1)
8124 free (isymbuf1);
8125 if (isymbuf2)
8126 free (isymbuf2);
8127
8128 return result;
8129}
8130
8131/* Return TRUE if 2 section types are compatible. */
8132
8133bfd_boolean
8134_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8135 bfd *bbfd, const asection *bsec)
8136{
8137 if (asec == NULL
8138 || bsec == NULL
8139 || abfd->xvec->flavour != bfd_target_elf_flavour
8140 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8141 return TRUE;
8142
8143 return elf_section_type (asec) == elf_section_type (bsec);
8144}
8145\f
c152c796
AM
8146/* Final phase of ELF linker. */
8147
8148/* A structure we use to avoid passing large numbers of arguments. */
8149
8150struct elf_final_link_info
8151{
8152 /* General link information. */
8153 struct bfd_link_info *info;
8154 /* Output BFD. */
8155 bfd *output_bfd;
8156 /* Symbol string table. */
ef10c3ac 8157 struct elf_strtab_hash *symstrtab;
c152c796
AM
8158 /* .hash section. */
8159 asection *hash_sec;
8160 /* symbol version section (.gnu.version). */
8161 asection *symver_sec;
8162 /* Buffer large enough to hold contents of any section. */
8163 bfd_byte *contents;
8164 /* Buffer large enough to hold external relocs of any section. */
8165 void *external_relocs;
8166 /* Buffer large enough to hold internal relocs of any section. */
8167 Elf_Internal_Rela *internal_relocs;
8168 /* Buffer large enough to hold external local symbols of any input
8169 BFD. */
8170 bfd_byte *external_syms;
8171 /* And a buffer for symbol section indices. */
8172 Elf_External_Sym_Shndx *locsym_shndx;
8173 /* Buffer large enough to hold internal local symbols of any input
8174 BFD. */
8175 Elf_Internal_Sym *internal_syms;
8176 /* Array large enough to hold a symbol index for each local symbol
8177 of any input BFD. */
8178 long *indices;
8179 /* Array large enough to hold a section pointer for each local
8180 symbol of any input BFD. */
8181 asection **sections;
ef10c3ac 8182 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8183 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8184 /* Number of STT_FILE syms seen. */
8185 size_t filesym_count;
c152c796
AM
8186};
8187
8188/* This struct is used to pass information to elf_link_output_extsym. */
8189
8190struct elf_outext_info
8191{
8192 bfd_boolean failed;
8193 bfd_boolean localsyms;
34a79995 8194 bfd_boolean file_sym_done;
8b127cbc 8195 struct elf_final_link_info *flinfo;
c152c796
AM
8196};
8197
d9352518
DB
8198
8199/* Support for evaluating a complex relocation.
8200
8201 Complex relocations are generalized, self-describing relocations. The
8202 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8203 relocations themselves.
d9352518
DB
8204
8205 The relocations are use a reserved elf-wide relocation type code (R_RELC
8206 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8207 information (start bit, end bit, word width, etc) into the addend. This
8208 information is extracted from CGEN-generated operand tables within gas.
8209
8210 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8211 internal) representing prefix-notation expressions, including but not
8212 limited to those sorts of expressions normally encoded as addends in the
8213 addend field. The symbol mangling format is:
8214
8215 <node> := <literal>
07d6d2b8
AM
8216 | <unary-operator> ':' <node>
8217 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8218 ;
8219
8220 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8221 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8222 | '#' <hexdigits>
8223 ;
8224
8225 <binary-operator> := as in C
8226 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8227
8228static void
a0c8462f
AM
8229set_symbol_value (bfd *bfd_with_globals,
8230 Elf_Internal_Sym *isymbuf,
8231 size_t locsymcount,
8232 size_t symidx,
8233 bfd_vma val)
d9352518 8234{
8977835c
AM
8235 struct elf_link_hash_entry **sym_hashes;
8236 struct elf_link_hash_entry *h;
8237 size_t extsymoff = locsymcount;
d9352518 8238
8977835c 8239 if (symidx < locsymcount)
d9352518 8240 {
8977835c
AM
8241 Elf_Internal_Sym *sym;
8242
8243 sym = isymbuf + symidx;
8244 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8245 {
8246 /* It is a local symbol: move it to the
8247 "absolute" section and give it a value. */
8248 sym->st_shndx = SHN_ABS;
8249 sym->st_value = val;
8250 return;
8251 }
8252 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8253 extsymoff = 0;
d9352518 8254 }
8977835c
AM
8255
8256 /* It is a global symbol: set its link type
8257 to "defined" and give it a value. */
8258
8259 sym_hashes = elf_sym_hashes (bfd_with_globals);
8260 h = sym_hashes [symidx - extsymoff];
8261 while (h->root.type == bfd_link_hash_indirect
8262 || h->root.type == bfd_link_hash_warning)
8263 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8264 h->root.type = bfd_link_hash_defined;
8265 h->root.u.def.value = val;
8266 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8267}
8268
a0c8462f
AM
8269static bfd_boolean
8270resolve_symbol (const char *name,
8271 bfd *input_bfd,
8b127cbc 8272 struct elf_final_link_info *flinfo,
a0c8462f
AM
8273 bfd_vma *result,
8274 Elf_Internal_Sym *isymbuf,
8275 size_t locsymcount)
d9352518 8276{
a0c8462f
AM
8277 Elf_Internal_Sym *sym;
8278 struct bfd_link_hash_entry *global_entry;
8279 const char *candidate = NULL;
8280 Elf_Internal_Shdr *symtab_hdr;
8281 size_t i;
8282
d9352518
DB
8283 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8284
8285 for (i = 0; i < locsymcount; ++ i)
8286 {
8977835c 8287 sym = isymbuf + i;
d9352518
DB
8288
8289 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8290 continue;
8291
8292 candidate = bfd_elf_string_from_elf_section (input_bfd,
8293 symtab_hdr->sh_link,
8294 sym->st_name);
8295#ifdef DEBUG
0f02bbd9
AM
8296 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8297 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8298#endif
8299 if (candidate && strcmp (candidate, name) == 0)
8300 {
8b127cbc 8301 asection *sec = flinfo->sections [i];
d9352518 8302
0f02bbd9
AM
8303 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8304 *result += sec->output_offset + sec->output_section->vma;
d9352518 8305#ifdef DEBUG
0f02bbd9
AM
8306 printf ("Found symbol with value %8.8lx\n",
8307 (unsigned long) *result);
d9352518
DB
8308#endif
8309 return TRUE;
8310 }
8311 }
8312
8313 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8314 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8315 FALSE, FALSE, TRUE);
d9352518
DB
8316 if (!global_entry)
8317 return FALSE;
a0c8462f 8318
d9352518
DB
8319 if (global_entry->type == bfd_link_hash_defined
8320 || global_entry->type == bfd_link_hash_defweak)
8321 {
a0c8462f
AM
8322 *result = (global_entry->u.def.value
8323 + global_entry->u.def.section->output_section->vma
8324 + global_entry->u.def.section->output_offset);
d9352518 8325#ifdef DEBUG
0f02bbd9
AM
8326 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8327 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8328#endif
8329 return TRUE;
a0c8462f 8330 }
d9352518 8331
d9352518
DB
8332 return FALSE;
8333}
8334
37b01f6a
DG
8335/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8336 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8337 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8338
d9352518 8339static bfd_boolean
a0c8462f
AM
8340resolve_section (const char *name,
8341 asection *sections,
37b01f6a
DG
8342 bfd_vma *result,
8343 bfd * abfd)
d9352518 8344{
a0c8462f
AM
8345 asection *curr;
8346 unsigned int len;
d9352518 8347
a0c8462f 8348 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8349 if (strcmp (curr->name, name) == 0)
8350 {
8351 *result = curr->vma;
8352 return TRUE;
8353 }
8354
8355 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8356 /* FIXME: This could be coded more efficiently... */
a0c8462f 8357 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8358 {
8359 len = strlen (curr->name);
a0c8462f 8360 if (len > strlen (name))
d9352518
DB
8361 continue;
8362
8363 if (strncmp (curr->name, name, len) == 0)
8364 {
8365 if (strncmp (".end", name + len, 4) == 0)
8366 {
37b01f6a 8367 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8368 return TRUE;
8369 }
8370
8371 /* Insert more pseudo-section names here, if you like. */
8372 }
8373 }
a0c8462f 8374
d9352518
DB
8375 return FALSE;
8376}
8377
8378static void
a0c8462f 8379undefined_reference (const char *reftype, const char *name)
d9352518 8380{
695344c0 8381 /* xgettext:c-format */
a0c8462f
AM
8382 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8383 reftype, name);
d9352518
DB
8384}
8385
8386static bfd_boolean
a0c8462f
AM
8387eval_symbol (bfd_vma *result,
8388 const char **symp,
8389 bfd *input_bfd,
8b127cbc 8390 struct elf_final_link_info *flinfo,
a0c8462f
AM
8391 bfd_vma dot,
8392 Elf_Internal_Sym *isymbuf,
8393 size_t locsymcount,
8394 int signed_p)
d9352518 8395{
4b93929b
NC
8396 size_t len;
8397 size_t symlen;
a0c8462f
AM
8398 bfd_vma a;
8399 bfd_vma b;
4b93929b 8400 char symbuf[4096];
0f02bbd9 8401 const char *sym = *symp;
a0c8462f
AM
8402 const char *symend;
8403 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8404
8405 len = strlen (sym);
8406 symend = sym + len;
8407
4b93929b 8408 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8409 {
8410 bfd_set_error (bfd_error_invalid_operation);
8411 return FALSE;
8412 }
a0c8462f 8413
d9352518
DB
8414 switch (* sym)
8415 {
8416 case '.':
0f02bbd9
AM
8417 *result = dot;
8418 *symp = sym + 1;
d9352518
DB
8419 return TRUE;
8420
8421 case '#':
0f02bbd9
AM
8422 ++sym;
8423 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8424 return TRUE;
8425
8426 case 'S':
8427 symbol_is_section = TRUE;
1a0670f3 8428 /* Fall through. */
a0c8462f 8429 case 's':
0f02bbd9
AM
8430 ++sym;
8431 symlen = strtol (sym, (char **) symp, 10);
8432 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8433
4b93929b 8434 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8435 {
8436 bfd_set_error (bfd_error_invalid_operation);
8437 return FALSE;
8438 }
8439
8440 memcpy (symbuf, sym, symlen);
a0c8462f 8441 symbuf[symlen] = '\0';
0f02bbd9 8442 *symp = sym + symlen;
a0c8462f
AM
8443
8444 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8445 the symbol as a section, or vice-versa. so we're pretty liberal in our
8446 interpretation here; section means "try section first", not "must be a
8447 section", and likewise with symbol. */
8448
a0c8462f 8449 if (symbol_is_section)
d9352518 8450 {
37b01f6a 8451 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8452 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8453 isymbuf, locsymcount))
d9352518
DB
8454 {
8455 undefined_reference ("section", symbuf);
8456 return FALSE;
8457 }
a0c8462f
AM
8458 }
8459 else
d9352518 8460 {
8b127cbc 8461 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8462 isymbuf, locsymcount)
8b127cbc 8463 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8464 result, input_bfd))
d9352518
DB
8465 {
8466 undefined_reference ("symbol", symbuf);
8467 return FALSE;
8468 }
8469 }
8470
8471 return TRUE;
a0c8462f 8472
d9352518
DB
8473 /* All that remains are operators. */
8474
8475#define UNARY_OP(op) \
8476 if (strncmp (sym, #op, strlen (#op)) == 0) \
8477 { \
8478 sym += strlen (#op); \
a0c8462f
AM
8479 if (*sym == ':') \
8480 ++sym; \
0f02bbd9 8481 *symp = sym; \
8b127cbc 8482 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8483 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8484 return FALSE; \
8485 if (signed_p) \
0f02bbd9 8486 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8487 else \
8488 *result = op a; \
d9352518
DB
8489 return TRUE; \
8490 }
8491
8492#define BINARY_OP(op) \
8493 if (strncmp (sym, #op, strlen (#op)) == 0) \
8494 { \
8495 sym += strlen (#op); \
a0c8462f
AM
8496 if (*sym == ':') \
8497 ++sym; \
0f02bbd9 8498 *symp = sym; \
8b127cbc 8499 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8500 isymbuf, locsymcount, signed_p)) \
a0c8462f 8501 return FALSE; \
0f02bbd9 8502 ++*symp; \
8b127cbc 8503 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8504 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8505 return FALSE; \
8506 if (signed_p) \
0f02bbd9 8507 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8508 else \
8509 *result = a op b; \
d9352518
DB
8510 return TRUE; \
8511 }
8512
8513 default:
8514 UNARY_OP (0-);
8515 BINARY_OP (<<);
8516 BINARY_OP (>>);
8517 BINARY_OP (==);
8518 BINARY_OP (!=);
8519 BINARY_OP (<=);
8520 BINARY_OP (>=);
8521 BINARY_OP (&&);
8522 BINARY_OP (||);
8523 UNARY_OP (~);
8524 UNARY_OP (!);
8525 BINARY_OP (*);
8526 BINARY_OP (/);
8527 BINARY_OP (%);
8528 BINARY_OP (^);
8529 BINARY_OP (|);
8530 BINARY_OP (&);
8531 BINARY_OP (+);
8532 BINARY_OP (-);
8533 BINARY_OP (<);
8534 BINARY_OP (>);
8535#undef UNARY_OP
8536#undef BINARY_OP
8537 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8538 bfd_set_error (bfd_error_invalid_operation);
8539 return FALSE;
8540 }
8541}
8542
d9352518 8543static void
a0c8462f
AM
8544put_value (bfd_vma size,
8545 unsigned long chunksz,
8546 bfd *input_bfd,
8547 bfd_vma x,
8548 bfd_byte *location)
d9352518
DB
8549{
8550 location += (size - chunksz);
8551
41cd1ad1 8552 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8553 {
8554 switch (chunksz)
8555 {
d9352518
DB
8556 case 1:
8557 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8558 x >>= 8;
d9352518
DB
8559 break;
8560 case 2:
8561 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8562 x >>= 16;
d9352518
DB
8563 break;
8564 case 4:
8565 bfd_put_32 (input_bfd, x, location);
65164438
NC
8566 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8567 x >>= 16;
8568 x >>= 16;
d9352518 8569 break;
d9352518 8570#ifdef BFD64
41cd1ad1 8571 case 8:
d9352518 8572 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8573 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8574 x >>= 32;
8575 x >>= 32;
8576 break;
d9352518 8577#endif
41cd1ad1
NC
8578 default:
8579 abort ();
d9352518
DB
8580 break;
8581 }
8582 }
8583}
8584
a0c8462f
AM
8585static bfd_vma
8586get_value (bfd_vma size,
8587 unsigned long chunksz,
8588 bfd *input_bfd,
8589 bfd_byte *location)
d9352518 8590{
9b239e0e 8591 int shift;
d9352518
DB
8592 bfd_vma x = 0;
8593
9b239e0e
NC
8594 /* Sanity checks. */
8595 BFD_ASSERT (chunksz <= sizeof (x)
8596 && size >= chunksz
8597 && chunksz != 0
8598 && (size % chunksz) == 0
8599 && input_bfd != NULL
8600 && location != NULL);
8601
8602 if (chunksz == sizeof (x))
8603 {
8604 BFD_ASSERT (size == chunksz);
8605
8606 /* Make sure that we do not perform an undefined shift operation.
8607 We know that size == chunksz so there will only be one iteration
8608 of the loop below. */
8609 shift = 0;
8610 }
8611 else
8612 shift = 8 * chunksz;
8613
a0c8462f 8614 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8615 {
8616 switch (chunksz)
8617 {
d9352518 8618 case 1:
9b239e0e 8619 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8620 break;
8621 case 2:
9b239e0e 8622 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8623 break;
8624 case 4:
9b239e0e 8625 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8626 break;
d9352518 8627#ifdef BFD64
9b239e0e
NC
8628 case 8:
8629 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8630 break;
9b239e0e
NC
8631#endif
8632 default:
8633 abort ();
d9352518
DB
8634 }
8635 }
8636 return x;
8637}
8638
a0c8462f
AM
8639static void
8640decode_complex_addend (unsigned long *start, /* in bits */
8641 unsigned long *oplen, /* in bits */
8642 unsigned long *len, /* in bits */
8643 unsigned long *wordsz, /* in bytes */
8644 unsigned long *chunksz, /* in bytes */
8645 unsigned long *lsb0_p,
8646 unsigned long *signed_p,
8647 unsigned long *trunc_p,
8648 unsigned long encoded)
d9352518 8649{
07d6d2b8
AM
8650 * start = encoded & 0x3F;
8651 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8652 * oplen = (encoded >> 12) & 0x3F;
8653 * wordsz = (encoded >> 18) & 0xF;
8654 * chunksz = (encoded >> 22) & 0xF;
8655 * lsb0_p = (encoded >> 27) & 1;
8656 * signed_p = (encoded >> 28) & 1;
8657 * trunc_p = (encoded >> 29) & 1;
8658}
8659
cdfeee4f 8660bfd_reloc_status_type
0f02bbd9 8661bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8662 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8663 bfd_byte *contents,
8664 Elf_Internal_Rela *rel,
8665 bfd_vma relocation)
d9352518 8666{
0f02bbd9
AM
8667 bfd_vma shift, x, mask;
8668 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8669 bfd_reloc_status_type r;
d9352518
DB
8670
8671 /* Perform this reloc, since it is complex.
8672 (this is not to say that it necessarily refers to a complex
8673 symbol; merely that it is a self-describing CGEN based reloc.
8674 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8675 word size, etc) encoded within it.). */
d9352518 8676
a0c8462f
AM
8677 decode_complex_addend (&start, &oplen, &len, &wordsz,
8678 &chunksz, &lsb0_p, &signed_p,
8679 &trunc_p, rel->r_addend);
d9352518
DB
8680
8681 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8682
8683 if (lsb0_p)
8684 shift = (start + 1) - len;
8685 else
8686 shift = (8 * wordsz) - (start + len);
8687
37b01f6a
DG
8688 x = get_value (wordsz, chunksz, input_bfd,
8689 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8690
8691#ifdef DEBUG
8692 printf ("Doing complex reloc: "
8693 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8694 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8695 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8696 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8697 oplen, (unsigned long) x, (unsigned long) mask,
8698 (unsigned long) relocation);
d9352518
DB
8699#endif
8700
cdfeee4f 8701 r = bfd_reloc_ok;
d9352518 8702 if (! trunc_p)
cdfeee4f
AM
8703 /* Now do an overflow check. */
8704 r = bfd_check_overflow ((signed_p
8705 ? complain_overflow_signed
8706 : complain_overflow_unsigned),
8707 len, 0, (8 * wordsz),
8708 relocation);
a0c8462f 8709
d9352518
DB
8710 /* Do the deed. */
8711 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8712
8713#ifdef DEBUG
8714 printf (" relocation: %8.8lx\n"
8715 " shifted mask: %8.8lx\n"
8716 " shifted/masked reloc: %8.8lx\n"
8717 " result: %8.8lx\n",
9ccb8af9
AM
8718 (unsigned long) relocation, (unsigned long) (mask << shift),
8719 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8720#endif
37b01f6a
DG
8721 put_value (wordsz, chunksz, input_bfd, x,
8722 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8723 return r;
d9352518
DB
8724}
8725
0e287786
AM
8726/* Functions to read r_offset from external (target order) reloc
8727 entry. Faster than bfd_getl32 et al, because we let the compiler
8728 know the value is aligned. */
53df40a4 8729
0e287786
AM
8730static bfd_vma
8731ext32l_r_offset (const void *p)
53df40a4
AM
8732{
8733 union aligned32
8734 {
8735 uint32_t v;
8736 unsigned char c[4];
8737 };
8738 const union aligned32 *a
0e287786 8739 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8740
8741 uint32_t aval = ( (uint32_t) a->c[0]
8742 | (uint32_t) a->c[1] << 8
8743 | (uint32_t) a->c[2] << 16
8744 | (uint32_t) a->c[3] << 24);
0e287786 8745 return aval;
53df40a4
AM
8746}
8747
0e287786
AM
8748static bfd_vma
8749ext32b_r_offset (const void *p)
53df40a4
AM
8750{
8751 union aligned32
8752 {
8753 uint32_t v;
8754 unsigned char c[4];
8755 };
8756 const union aligned32 *a
0e287786 8757 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8758
8759 uint32_t aval = ( (uint32_t) a->c[0] << 24
8760 | (uint32_t) a->c[1] << 16
8761 | (uint32_t) a->c[2] << 8
8762 | (uint32_t) a->c[3]);
0e287786 8763 return aval;
53df40a4
AM
8764}
8765
8766#ifdef BFD_HOST_64_BIT
0e287786
AM
8767static bfd_vma
8768ext64l_r_offset (const void *p)
53df40a4
AM
8769{
8770 union aligned64
8771 {
8772 uint64_t v;
8773 unsigned char c[8];
8774 };
8775 const union aligned64 *a
0e287786 8776 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8777
8778 uint64_t aval = ( (uint64_t) a->c[0]
8779 | (uint64_t) a->c[1] << 8
8780 | (uint64_t) a->c[2] << 16
8781 | (uint64_t) a->c[3] << 24
8782 | (uint64_t) a->c[4] << 32
8783 | (uint64_t) a->c[5] << 40
8784 | (uint64_t) a->c[6] << 48
8785 | (uint64_t) a->c[7] << 56);
0e287786 8786 return aval;
53df40a4
AM
8787}
8788
0e287786
AM
8789static bfd_vma
8790ext64b_r_offset (const void *p)
53df40a4
AM
8791{
8792 union aligned64
8793 {
8794 uint64_t v;
8795 unsigned char c[8];
8796 };
8797 const union aligned64 *a
0e287786 8798 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8799
8800 uint64_t aval = ( (uint64_t) a->c[0] << 56
8801 | (uint64_t) a->c[1] << 48
8802 | (uint64_t) a->c[2] << 40
8803 | (uint64_t) a->c[3] << 32
8804 | (uint64_t) a->c[4] << 24
8805 | (uint64_t) a->c[5] << 16
8806 | (uint64_t) a->c[6] << 8
8807 | (uint64_t) a->c[7]);
0e287786 8808 return aval;
53df40a4
AM
8809}
8810#endif
8811
c152c796
AM
8812/* When performing a relocatable link, the input relocations are
8813 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8814 referenced must be updated. Update all the relocations found in
8815 RELDATA. */
c152c796 8816
bca6d0e3 8817static bfd_boolean
c152c796 8818elf_link_adjust_relocs (bfd *abfd,
9eaff861 8819 asection *sec,
28dbcedc 8820 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
8821 bfd_boolean sort,
8822 struct bfd_link_info *info)
c152c796
AM
8823{
8824 unsigned int i;
8825 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8826 bfd_byte *erela;
8827 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8828 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8829 bfd_vma r_type_mask;
8830 int r_sym_shift;
d4730f92
BS
8831 unsigned int count = reldata->count;
8832 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8833
d4730f92 8834 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8835 {
8836 swap_in = bed->s->swap_reloc_in;
8837 swap_out = bed->s->swap_reloc_out;
8838 }
d4730f92 8839 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8840 {
8841 swap_in = bed->s->swap_reloca_in;
8842 swap_out = bed->s->swap_reloca_out;
8843 }
8844 else
8845 abort ();
8846
8847 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8848 abort ();
8849
8850 if (bed->s->arch_size == 32)
8851 {
8852 r_type_mask = 0xff;
8853 r_sym_shift = 8;
8854 }
8855 else
8856 {
8857 r_type_mask = 0xffffffff;
8858 r_sym_shift = 32;
8859 }
8860
d4730f92
BS
8861 erela = reldata->hdr->contents;
8862 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8863 {
8864 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8865 unsigned int j;
8866
8867 if (*rel_hash == NULL)
8868 continue;
8869
10bbbc1d
NC
8870 if ((*rel_hash)->indx == -2
8871 && info->gc_sections
8872 && ! info->gc_keep_exported)
8873 {
8874 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 8875 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
8876 abfd, sec,
8877 (*rel_hash)->root.root.string);
9793eb77 8878 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 8879 abfd, sec);
10bbbc1d
NC
8880 bfd_set_error (bfd_error_invalid_operation);
8881 return FALSE;
8882 }
c152c796
AM
8883 BFD_ASSERT ((*rel_hash)->indx >= 0);
8884
8885 (*swap_in) (abfd, erela, irela);
8886 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8887 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8888 | (irela[j].r_info & r_type_mask));
8889 (*swap_out) (abfd, irela, erela);
8890 }
53df40a4 8891
9eaff861
AO
8892 if (bed->elf_backend_update_relocs)
8893 (*bed->elf_backend_update_relocs) (sec, reldata);
8894
0e287786 8895 if (sort && count != 0)
53df40a4 8896 {
0e287786
AM
8897 bfd_vma (*ext_r_off) (const void *);
8898 bfd_vma r_off;
8899 size_t elt_size;
8900 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8901 bfd_byte *buf = NULL;
28dbcedc
AM
8902
8903 if (bed->s->arch_size == 32)
8904 {
8905 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8906 ext_r_off = ext32l_r_offset;
28dbcedc 8907 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8908 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8909 else
8910 abort ();
8911 }
53df40a4 8912 else
28dbcedc 8913 {
53df40a4 8914#ifdef BFD_HOST_64_BIT
28dbcedc 8915 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8916 ext_r_off = ext64l_r_offset;
28dbcedc 8917 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8918 ext_r_off = ext64b_r_offset;
28dbcedc 8919 else
53df40a4 8920#endif
28dbcedc
AM
8921 abort ();
8922 }
0e287786 8923
bca6d0e3
AM
8924 /* Must use a stable sort here. A modified insertion sort,
8925 since the relocs are mostly sorted already. */
0e287786
AM
8926 elt_size = reldata->hdr->sh_entsize;
8927 base = reldata->hdr->contents;
8928 end = base + count * elt_size;
bca6d0e3 8929 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8930 abort ();
8931
8932 /* Ensure the first element is lowest. This acts as a sentinel,
8933 speeding the main loop below. */
8934 r_off = (*ext_r_off) (base);
8935 for (p = loc = base; (p += elt_size) < end; )
8936 {
8937 bfd_vma r_off2 = (*ext_r_off) (p);
8938 if (r_off > r_off2)
8939 {
8940 r_off = r_off2;
8941 loc = p;
8942 }
8943 }
8944 if (loc != base)
8945 {
8946 /* Don't just swap *base and *loc as that changes the order
8947 of the original base[0] and base[1] if they happen to
8948 have the same r_offset. */
bca6d0e3
AM
8949 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8950 memcpy (onebuf, loc, elt_size);
0e287786 8951 memmove (base + elt_size, base, loc - base);
bca6d0e3 8952 memcpy (base, onebuf, elt_size);
0e287786
AM
8953 }
8954
b29b8669 8955 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8956 {
8957 /* base to p is sorted, *p is next to insert. */
8958 r_off = (*ext_r_off) (p);
8959 /* Search the sorted region for location to insert. */
8960 loc = p - elt_size;
8961 while (r_off < (*ext_r_off) (loc))
8962 loc -= elt_size;
8963 loc += elt_size;
8964 if (loc != p)
8965 {
bca6d0e3
AM
8966 /* Chances are there is a run of relocs to insert here,
8967 from one of more input files. Files are not always
8968 linked in order due to the way elf_link_input_bfd is
8969 called. See pr17666. */
8970 size_t sortlen = p - loc;
8971 bfd_vma r_off2 = (*ext_r_off) (loc);
8972 size_t runlen = elt_size;
8973 size_t buf_size = 96 * 1024;
8974 while (p + runlen < end
8975 && (sortlen <= buf_size
8976 || runlen + elt_size <= buf_size)
8977 && r_off2 > (*ext_r_off) (p + runlen))
8978 runlen += elt_size;
8979 if (buf == NULL)
8980 {
8981 buf = bfd_malloc (buf_size);
8982 if (buf == NULL)
8983 return FALSE;
8984 }
8985 if (runlen < sortlen)
8986 {
8987 memcpy (buf, p, runlen);
8988 memmove (loc + runlen, loc, sortlen);
8989 memcpy (loc, buf, runlen);
8990 }
8991 else
8992 {
8993 memcpy (buf, loc, sortlen);
8994 memmove (loc, p, runlen);
8995 memcpy (loc + runlen, buf, sortlen);
8996 }
b29b8669 8997 p += runlen - elt_size;
0e287786
AM
8998 }
8999 }
9000 /* Hashes are no longer valid. */
28dbcedc
AM
9001 free (reldata->hashes);
9002 reldata->hashes = NULL;
bca6d0e3 9003 free (buf);
53df40a4 9004 }
bca6d0e3 9005 return TRUE;
c152c796
AM
9006}
9007
9008struct elf_link_sort_rela
9009{
9010 union {
9011 bfd_vma offset;
9012 bfd_vma sym_mask;
9013 } u;
9014 enum elf_reloc_type_class type;
9015 /* We use this as an array of size int_rels_per_ext_rel. */
9016 Elf_Internal_Rela rela[1];
9017};
9018
9019static int
9020elf_link_sort_cmp1 (const void *A, const void *B)
9021{
a50b1753
NC
9022 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9023 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9024 int relativea, relativeb;
9025
9026 relativea = a->type == reloc_class_relative;
9027 relativeb = b->type == reloc_class_relative;
9028
9029 if (relativea < relativeb)
9030 return 1;
9031 if (relativea > relativeb)
9032 return -1;
9033 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9034 return -1;
9035 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9036 return 1;
9037 if (a->rela->r_offset < b->rela->r_offset)
9038 return -1;
9039 if (a->rela->r_offset > b->rela->r_offset)
9040 return 1;
9041 return 0;
9042}
9043
9044static int
9045elf_link_sort_cmp2 (const void *A, const void *B)
9046{
a50b1753
NC
9047 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9048 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9049
7e612e98 9050 if (a->type < b->type)
c152c796 9051 return -1;
7e612e98 9052 if (a->type > b->type)
c152c796 9053 return 1;
7e612e98 9054 if (a->u.offset < b->u.offset)
c152c796 9055 return -1;
7e612e98 9056 if (a->u.offset > b->u.offset)
c152c796
AM
9057 return 1;
9058 if (a->rela->r_offset < b->rela->r_offset)
9059 return -1;
9060 if (a->rela->r_offset > b->rela->r_offset)
9061 return 1;
9062 return 0;
9063}
9064
9065static size_t
9066elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9067{
3410fea8 9068 asection *dynamic_relocs;
fc66a176
L
9069 asection *rela_dyn;
9070 asection *rel_dyn;
c152c796
AM
9071 bfd_size_type count, size;
9072 size_t i, ret, sort_elt, ext_size;
9073 bfd_byte *sort, *s_non_relative, *p;
9074 struct elf_link_sort_rela *sq;
9075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9076 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 9077 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
9078 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9079 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9080 struct bfd_link_order *lo;
9081 bfd_vma r_sym_mask;
3410fea8 9082 bfd_boolean use_rela;
c152c796 9083
3410fea8
NC
9084 /* Find a dynamic reloc section. */
9085 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9086 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9087 if (rela_dyn != NULL && rela_dyn->size > 0
9088 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9089 {
3410fea8
NC
9090 bfd_boolean use_rela_initialised = FALSE;
9091
9092 /* This is just here to stop gcc from complaining.
c8e44c6d 9093 Its initialization checking code is not perfect. */
3410fea8
NC
9094 use_rela = TRUE;
9095
9096 /* Both sections are present. Examine the sizes
9097 of the indirect sections to help us choose. */
9098 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9099 if (lo->type == bfd_indirect_link_order)
9100 {
9101 asection *o = lo->u.indirect.section;
9102
9103 if ((o->size % bed->s->sizeof_rela) == 0)
9104 {
9105 if ((o->size % bed->s->sizeof_rel) == 0)
9106 /* Section size is divisible by both rel and rela sizes.
9107 It is of no help to us. */
9108 ;
9109 else
9110 {
9111 /* Section size is only divisible by rela. */
535b785f 9112 if (use_rela_initialised && !use_rela)
3410fea8 9113 {
9793eb77 9114 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9115 "they are in more than one size"),
9116 abfd);
3410fea8
NC
9117 bfd_set_error (bfd_error_invalid_operation);
9118 return 0;
9119 }
9120 else
9121 {
9122 use_rela = TRUE;
9123 use_rela_initialised = TRUE;
9124 }
9125 }
9126 }
9127 else if ((o->size % bed->s->sizeof_rel) == 0)
9128 {
9129 /* Section size is only divisible by rel. */
535b785f 9130 if (use_rela_initialised && use_rela)
3410fea8 9131 {
9793eb77 9132 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9133 "they are in more than one size"),
9134 abfd);
3410fea8
NC
9135 bfd_set_error (bfd_error_invalid_operation);
9136 return 0;
9137 }
9138 else
9139 {
9140 use_rela = FALSE;
9141 use_rela_initialised = TRUE;
9142 }
9143 }
9144 else
9145 {
c8e44c6d
AM
9146 /* The section size is not divisible by either -
9147 something is wrong. */
9793eb77 9148 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9149 "they are of an unknown size"), abfd);
3410fea8
NC
9150 bfd_set_error (bfd_error_invalid_operation);
9151 return 0;
9152 }
9153 }
9154
9155 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9156 if (lo->type == bfd_indirect_link_order)
9157 {
9158 asection *o = lo->u.indirect.section;
9159
9160 if ((o->size % bed->s->sizeof_rela) == 0)
9161 {
9162 if ((o->size % bed->s->sizeof_rel) == 0)
9163 /* Section size is divisible by both rel and rela sizes.
9164 It is of no help to us. */
9165 ;
9166 else
9167 {
9168 /* Section size is only divisible by rela. */
535b785f 9169 if (use_rela_initialised && !use_rela)
3410fea8 9170 {
9793eb77 9171 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9172 "they are in more than one size"),
9173 abfd);
3410fea8
NC
9174 bfd_set_error (bfd_error_invalid_operation);
9175 return 0;
9176 }
9177 else
9178 {
9179 use_rela = TRUE;
9180 use_rela_initialised = TRUE;
9181 }
9182 }
9183 }
9184 else if ((o->size % bed->s->sizeof_rel) == 0)
9185 {
9186 /* Section size is only divisible by rel. */
535b785f 9187 if (use_rela_initialised && use_rela)
3410fea8 9188 {
9793eb77 9189 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9190 "they are in more than one size"),
9191 abfd);
3410fea8
NC
9192 bfd_set_error (bfd_error_invalid_operation);
9193 return 0;
9194 }
9195 else
9196 {
9197 use_rela = FALSE;
9198 use_rela_initialised = TRUE;
9199 }
9200 }
9201 else
9202 {
c8e44c6d
AM
9203 /* The section size is not divisible by either -
9204 something is wrong. */
9793eb77 9205 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9206 "they are of an unknown size"), abfd);
3410fea8
NC
9207 bfd_set_error (bfd_error_invalid_operation);
9208 return 0;
9209 }
9210 }
9211
9212 if (! use_rela_initialised)
9213 /* Make a guess. */
9214 use_rela = TRUE;
c152c796 9215 }
fc66a176
L
9216 else if (rela_dyn != NULL && rela_dyn->size > 0)
9217 use_rela = TRUE;
9218 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9219 use_rela = FALSE;
c152c796 9220 else
fc66a176 9221 return 0;
3410fea8
NC
9222
9223 if (use_rela)
c152c796 9224 {
3410fea8 9225 dynamic_relocs = rela_dyn;
c152c796
AM
9226 ext_size = bed->s->sizeof_rela;
9227 swap_in = bed->s->swap_reloca_in;
9228 swap_out = bed->s->swap_reloca_out;
9229 }
3410fea8
NC
9230 else
9231 {
9232 dynamic_relocs = rel_dyn;
9233 ext_size = bed->s->sizeof_rel;
9234 swap_in = bed->s->swap_reloc_in;
9235 swap_out = bed->s->swap_reloc_out;
9236 }
c152c796
AM
9237
9238 size = 0;
3410fea8 9239 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9240 if (lo->type == bfd_indirect_link_order)
3410fea8 9241 size += lo->u.indirect.section->size;
c152c796 9242
3410fea8 9243 if (size != dynamic_relocs->size)
c152c796
AM
9244 return 0;
9245
9246 sort_elt = (sizeof (struct elf_link_sort_rela)
9247 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9248
9249 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9250 if (count == 0)
9251 return 0;
a50b1753 9252 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9253
c152c796
AM
9254 if (sort == NULL)
9255 {
9256 (*info->callbacks->warning)
9793eb77 9257 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9258 return 0;
9259 }
9260
9261 if (bed->s->arch_size == 32)
9262 r_sym_mask = ~(bfd_vma) 0xff;
9263 else
9264 r_sym_mask = ~(bfd_vma) 0xffffffff;
9265
3410fea8 9266 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9267 if (lo->type == bfd_indirect_link_order)
9268 {
9269 bfd_byte *erel, *erelend;
9270 asection *o = lo->u.indirect.section;
9271
1da212d6
AM
9272 if (o->contents == NULL && o->size != 0)
9273 {
9274 /* This is a reloc section that is being handled as a normal
9275 section. See bfd_section_from_shdr. We can't combine
9276 relocs in this case. */
9277 free (sort);
9278 return 0;
9279 }
c152c796 9280 erel = o->contents;
eea6121a 9281 erelend = o->contents + o->size;
c8e44c6d 9282 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9283
c152c796
AM
9284 while (erel < erelend)
9285 {
9286 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9287
c152c796 9288 (*swap_in) (abfd, erel, s->rela);
7e612e98 9289 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9290 s->u.sym_mask = r_sym_mask;
9291 p += sort_elt;
9292 erel += ext_size;
9293 }
9294 }
9295
9296 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9297
9298 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9299 {
9300 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9301 if (s->type != reloc_class_relative)
9302 break;
9303 }
9304 ret = i;
9305 s_non_relative = p;
9306
9307 sq = (struct elf_link_sort_rela *) s_non_relative;
9308 for (; i < count; i++, p += sort_elt)
9309 {
9310 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9311 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9312 sq = sp;
9313 sp->u.offset = sq->rela->r_offset;
9314 }
9315
9316 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9317
c8e44c6d
AM
9318 struct elf_link_hash_table *htab = elf_hash_table (info);
9319 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9320 {
9321 /* We have plt relocs in .rela.dyn. */
9322 sq = (struct elf_link_sort_rela *) sort;
9323 for (i = 0; i < count; i++)
9324 if (sq[count - i - 1].type != reloc_class_plt)
9325 break;
9326 if (i != 0 && htab->srelplt->size == i * ext_size)
9327 {
9328 struct bfd_link_order **plo;
9329 /* Put srelplt link_order last. This is so the output_offset
9330 set in the next loop is correct for DT_JMPREL. */
9331 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9332 if ((*plo)->type == bfd_indirect_link_order
9333 && (*plo)->u.indirect.section == htab->srelplt)
9334 {
9335 lo = *plo;
9336 *plo = lo->next;
9337 }
9338 else
9339 plo = &(*plo)->next;
9340 *plo = lo;
9341 lo->next = NULL;
9342 dynamic_relocs->map_tail.link_order = lo;
9343 }
9344 }
9345
9346 p = sort;
3410fea8 9347 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9348 if (lo->type == bfd_indirect_link_order)
9349 {
9350 bfd_byte *erel, *erelend;
9351 asection *o = lo->u.indirect.section;
9352
9353 erel = o->contents;
eea6121a 9354 erelend = o->contents + o->size;
c8e44c6d 9355 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9356 while (erel < erelend)
9357 {
9358 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9359 (*swap_out) (abfd, s->rela, erel);
9360 p += sort_elt;
9361 erel += ext_size;
9362 }
9363 }
9364
9365 free (sort);
3410fea8 9366 *psec = dynamic_relocs;
c152c796
AM
9367 return ret;
9368}
9369
ef10c3ac 9370/* Add a symbol to the output symbol string table. */
c152c796 9371
6e0b88f1 9372static int
ef10c3ac
L
9373elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9374 const char *name,
9375 Elf_Internal_Sym *elfsym,
9376 asection *input_sec,
9377 struct elf_link_hash_entry *h)
c152c796 9378{
6e0b88f1 9379 int (*output_symbol_hook)
c152c796
AM
9380 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9381 struct elf_link_hash_entry *);
ef10c3ac 9382 struct elf_link_hash_table *hash_table;
c152c796 9383 const struct elf_backend_data *bed;
ef10c3ac 9384 bfd_size_type strtabsize;
c152c796 9385
8539e4e8
AM
9386 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9387
8b127cbc 9388 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9389 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9390 if (output_symbol_hook != NULL)
9391 {
8b127cbc 9392 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9393 if (ret != 1)
9394 return ret;
c152c796
AM
9395 }
9396
ef10c3ac
L
9397 if (name == NULL
9398 || *name == '\0'
9399 || (input_sec->flags & SEC_EXCLUDE))
9400 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9401 else
9402 {
ef10c3ac
L
9403 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9404 to get the final offset for st_name. */
9405 elfsym->st_name
9406 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9407 name, FALSE);
c152c796 9408 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9409 return 0;
c152c796
AM
9410 }
9411
ef10c3ac
L
9412 hash_table = elf_hash_table (flinfo->info);
9413 strtabsize = hash_table->strtabsize;
9414 if (strtabsize <= hash_table->strtabcount)
c152c796 9415 {
ef10c3ac
L
9416 strtabsize += strtabsize;
9417 hash_table->strtabsize = strtabsize;
9418 strtabsize *= sizeof (*hash_table->strtab);
9419 hash_table->strtab
9420 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9421 strtabsize);
9422 if (hash_table->strtab == NULL)
6e0b88f1 9423 return 0;
c152c796 9424 }
ef10c3ac
L
9425 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9426 hash_table->strtab[hash_table->strtabcount].dest_index
9427 = hash_table->strtabcount;
9428 hash_table->strtab[hash_table->strtabcount].destshndx_index
9429 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9430
9431 bfd_get_symcount (flinfo->output_bfd) += 1;
9432 hash_table->strtabcount += 1;
9433
9434 return 1;
9435}
9436
9437/* Swap symbols out to the symbol table and flush the output symbols to
9438 the file. */
9439
9440static bfd_boolean
9441elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9442{
9443 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9444 bfd_size_type amt;
9445 size_t i;
ef10c3ac
L
9446 const struct elf_backend_data *bed;
9447 bfd_byte *symbuf;
9448 Elf_Internal_Shdr *hdr;
9449 file_ptr pos;
9450 bfd_boolean ret;
9451
9452 if (!hash_table->strtabcount)
9453 return TRUE;
9454
9455 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9456
9457 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9458
ef10c3ac
L
9459 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9460 symbuf = (bfd_byte *) bfd_malloc (amt);
9461 if (symbuf == NULL)
9462 return FALSE;
1b786873 9463
ef10c3ac 9464 if (flinfo->symshndxbuf)
c152c796 9465 {
ef53be89
AM
9466 amt = sizeof (Elf_External_Sym_Shndx);
9467 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9468 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9469 if (flinfo->symshndxbuf == NULL)
c152c796 9470 {
ef10c3ac
L
9471 free (symbuf);
9472 return FALSE;
c152c796 9473 }
c152c796
AM
9474 }
9475
ef10c3ac
L
9476 for (i = 0; i < hash_table->strtabcount; i++)
9477 {
9478 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9479 if (elfsym->sym.st_name == (unsigned long) -1)
9480 elfsym->sym.st_name = 0;
9481 else
9482 elfsym->sym.st_name
9483 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9484 elfsym->sym.st_name);
9485 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9486 ((bfd_byte *) symbuf
9487 + (elfsym->dest_index
9488 * bed->s->sizeof_sym)),
9489 (flinfo->symshndxbuf
9490 + elfsym->destshndx_index));
9491 }
9492
9493 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9494 pos = hdr->sh_offset + hdr->sh_size;
9495 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9496 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9497 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9498 {
9499 hdr->sh_size += amt;
9500 ret = TRUE;
9501 }
9502 else
9503 ret = FALSE;
c152c796 9504
ef10c3ac
L
9505 free (symbuf);
9506
9507 free (hash_table->strtab);
9508 hash_table->strtab = NULL;
9509
9510 return ret;
c152c796
AM
9511}
9512
c0d5a53d
L
9513/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9514
9515static bfd_boolean
9516check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9517{
4fbb74a6
AM
9518 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9519 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9520 {
9521 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9522 beyond 64k. */
4eca0228 9523 _bfd_error_handler
695344c0 9524 /* xgettext:c-format */
9793eb77 9525 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9526 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9527 bfd_set_error (bfd_error_nonrepresentable_section);
9528 return FALSE;
9529 }
9530 return TRUE;
9531}
9532
c152c796
AM
9533/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9534 allowing an unsatisfied unversioned symbol in the DSO to match a
9535 versioned symbol that would normally require an explicit version.
9536 We also handle the case that a DSO references a hidden symbol
9537 which may be satisfied by a versioned symbol in another DSO. */
9538
9539static bfd_boolean
9540elf_link_check_versioned_symbol (struct bfd_link_info *info,
9541 const struct elf_backend_data *bed,
9542 struct elf_link_hash_entry *h)
9543{
9544 bfd *abfd;
9545 struct elf_link_loaded_list *loaded;
9546
9547 if (!is_elf_hash_table (info->hash))
9548 return FALSE;
9549
90c984fc
L
9550 /* Check indirect symbol. */
9551 while (h->root.type == bfd_link_hash_indirect)
9552 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9553
c152c796
AM
9554 switch (h->root.type)
9555 {
9556 default:
9557 abfd = NULL;
9558 break;
9559
9560 case bfd_link_hash_undefined:
9561 case bfd_link_hash_undefweak:
9562 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9563 if (abfd == NULL
9564 || (abfd->flags & DYNAMIC) == 0
e56f61be 9565 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9566 return FALSE;
9567 break;
9568
9569 case bfd_link_hash_defined:
9570 case bfd_link_hash_defweak:
9571 abfd = h->root.u.def.section->owner;
9572 break;
9573
9574 case bfd_link_hash_common:
9575 abfd = h->root.u.c.p->section->owner;
9576 break;
9577 }
9578 BFD_ASSERT (abfd != NULL);
9579
9580 for (loaded = elf_hash_table (info)->loaded;
9581 loaded != NULL;
9582 loaded = loaded->next)
9583 {
9584 bfd *input;
9585 Elf_Internal_Shdr *hdr;
ef53be89
AM
9586 size_t symcount;
9587 size_t extsymcount;
9588 size_t extsymoff;
c152c796
AM
9589 Elf_Internal_Shdr *versymhdr;
9590 Elf_Internal_Sym *isym;
9591 Elf_Internal_Sym *isymend;
9592 Elf_Internal_Sym *isymbuf;
9593 Elf_External_Versym *ever;
9594 Elf_External_Versym *extversym;
9595
9596 input = loaded->abfd;
9597
9598 /* We check each DSO for a possible hidden versioned definition. */
9599 if (input == abfd
9600 || (input->flags & DYNAMIC) == 0
9601 || elf_dynversym (input) == 0)
9602 continue;
9603
9604 hdr = &elf_tdata (input)->dynsymtab_hdr;
9605
9606 symcount = hdr->sh_size / bed->s->sizeof_sym;
9607 if (elf_bad_symtab (input))
9608 {
9609 extsymcount = symcount;
9610 extsymoff = 0;
9611 }
9612 else
9613 {
9614 extsymcount = symcount - hdr->sh_info;
9615 extsymoff = hdr->sh_info;
9616 }
9617
9618 if (extsymcount == 0)
9619 continue;
9620
9621 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9622 NULL, NULL, NULL);
9623 if (isymbuf == NULL)
9624 return FALSE;
9625
9626 /* Read in any version definitions. */
9627 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9628 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9629 if (extversym == NULL)
9630 goto error_ret;
9631
9632 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9633 || (bfd_bread (extversym, versymhdr->sh_size, input)
9634 != versymhdr->sh_size))
9635 {
9636 free (extversym);
9637 error_ret:
9638 free (isymbuf);
9639 return FALSE;
9640 }
9641
9642 ever = extversym + extsymoff;
9643 isymend = isymbuf + extsymcount;
9644 for (isym = isymbuf; isym < isymend; isym++, ever++)
9645 {
9646 const char *name;
9647 Elf_Internal_Versym iver;
9648 unsigned short version_index;
9649
9650 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9651 || isym->st_shndx == SHN_UNDEF)
9652 continue;
9653
9654 name = bfd_elf_string_from_elf_section (input,
9655 hdr->sh_link,
9656 isym->st_name);
9657 if (strcmp (name, h->root.root.string) != 0)
9658 continue;
9659
9660 _bfd_elf_swap_versym_in (input, ever, &iver);
9661
d023c380
L
9662 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9663 && !(h->def_regular
9664 && h->forced_local))
c152c796
AM
9665 {
9666 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9667 have provided a definition for the undefined sym unless
9668 it is defined in a non-shared object and forced local.
9669 */
c152c796
AM
9670 abort ();
9671 }
9672
9673 version_index = iver.vs_vers & VERSYM_VERSION;
9674 if (version_index == 1 || version_index == 2)
9675 {
9676 /* This is the base or first version. We can use it. */
9677 free (extversym);
9678 free (isymbuf);
9679 return TRUE;
9680 }
9681 }
9682
9683 free (extversym);
9684 free (isymbuf);
9685 }
9686
9687 return FALSE;
9688}
9689
b8871f35
L
9690/* Convert ELF common symbol TYPE. */
9691
9692static int
9693elf_link_convert_common_type (struct bfd_link_info *info, int type)
9694{
9695 /* Commom symbol can only appear in relocatable link. */
9696 if (!bfd_link_relocatable (info))
9697 abort ();
9698 switch (info->elf_stt_common)
9699 {
9700 case unchanged:
9701 break;
9702 case elf_stt_common:
9703 type = STT_COMMON;
9704 break;
9705 case no_elf_stt_common:
9706 type = STT_OBJECT;
9707 break;
9708 }
9709 return type;
9710}
9711
c152c796
AM
9712/* Add an external symbol to the symbol table. This is called from
9713 the hash table traversal routine. When generating a shared object,
9714 we go through the symbol table twice. The first time we output
9715 anything that might have been forced to local scope in a version
9716 script. The second time we output the symbols that are still
9717 global symbols. */
9718
9719static bfd_boolean
7686d77d 9720elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9721{
7686d77d 9722 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9723 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9724 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9725 bfd_boolean strip;
9726 Elf_Internal_Sym sym;
9727 asection *input_sec;
9728 const struct elf_backend_data *bed;
6e0b88f1
AM
9729 long indx;
9730 int ret;
b8871f35 9731 unsigned int type;
c152c796
AM
9732
9733 if (h->root.type == bfd_link_hash_warning)
9734 {
9735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9736 if (h->root.type == bfd_link_hash_new)
9737 return TRUE;
9738 }
9739
9740 /* Decide whether to output this symbol in this pass. */
9741 if (eoinfo->localsyms)
9742 {
4deb8f71 9743 if (!h->forced_local)
c152c796
AM
9744 return TRUE;
9745 }
9746 else
9747 {
4deb8f71 9748 if (h->forced_local)
c152c796
AM
9749 return TRUE;
9750 }
9751
8b127cbc 9752 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9753
12ac1cf5 9754 if (h->root.type == bfd_link_hash_undefined)
c152c796 9755 {
12ac1cf5
NC
9756 /* If we have an undefined symbol reference here then it must have
9757 come from a shared library that is being linked in. (Undefined
98da7939
L
9758 references in regular files have already been handled unless
9759 they are in unreferenced sections which are removed by garbage
9760 collection). */
12ac1cf5
NC
9761 bfd_boolean ignore_undef = FALSE;
9762
9763 /* Some symbols may be special in that the fact that they're
9764 undefined can be safely ignored - let backend determine that. */
9765 if (bed->elf_backend_ignore_undef_symbol)
9766 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9767
9768 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9769 if (!ignore_undef
12ac1cf5 9770 && h->ref_dynamic
8b127cbc
AM
9771 && (!h->ref_regular || flinfo->info->gc_sections)
9772 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9773 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9774 (*flinfo->info->callbacks->undefined_symbol)
9775 (flinfo->info, h->root.root.string,
9776 h->ref_regular ? NULL : h->root.u.undef.abfd,
9777 NULL, 0,
9778 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9779
9780 /* Strip a global symbol defined in a discarded section. */
9781 if (h->indx == -3)
9782 return TRUE;
c152c796
AM
9783 }
9784
9785 /* We should also warn if a forced local symbol is referenced from
9786 shared libraries. */
0e1862bb 9787 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9788 && h->forced_local
9789 && h->ref_dynamic
371a5866 9790 && h->def_regular
f5385ebf 9791 && !h->dynamic_def
ee659f1f 9792 && h->ref_dynamic_nonweak
8b127cbc 9793 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9794 {
17d078c5
AM
9795 bfd *def_bfd;
9796 const char *msg;
90c984fc
L
9797 struct elf_link_hash_entry *hi = h;
9798
9799 /* Check indirect symbol. */
9800 while (hi->root.type == bfd_link_hash_indirect)
9801 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9802
9803 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9804 /* xgettext:c-format */
871b3ab2 9805 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 9806 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9807 /* xgettext:c-format */
871b3ab2 9808 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 9809 else
695344c0 9810 /* xgettext:c-format */
871b3ab2 9811 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 9812 def_bfd = flinfo->output_bfd;
90c984fc
L
9813 if (hi->root.u.def.section != bfd_abs_section_ptr)
9814 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
9815 _bfd_error_handler (msg, flinfo->output_bfd,
9816 h->root.root.string, def_bfd);
17d078c5 9817 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9818 eoinfo->failed = TRUE;
9819 return FALSE;
9820 }
9821
9822 /* We don't want to output symbols that have never been mentioned by
9823 a regular file, or that we have been told to strip. However, if
9824 h->indx is set to -2, the symbol is used by a reloc and we must
9825 output it. */
d983c8c5 9826 strip = FALSE;
c152c796 9827 if (h->indx == -2)
d983c8c5 9828 ;
f5385ebf 9829 else if ((h->def_dynamic
77cfaee6
AM
9830 || h->ref_dynamic
9831 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9832 && !h->def_regular
9833 && !h->ref_regular)
c152c796 9834 strip = TRUE;
8b127cbc 9835 else if (flinfo->info->strip == strip_all)
c152c796 9836 strip = TRUE;
8b127cbc
AM
9837 else if (flinfo->info->strip == strip_some
9838 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9839 h->root.root.string, FALSE, FALSE) == NULL)
9840 strip = TRUE;
d56d55e7
AM
9841 else if ((h->root.type == bfd_link_hash_defined
9842 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9843 && ((flinfo->info->strip_discarded
dbaa2011 9844 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9845 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9846 && h->root.u.def.section->owner != NULL
d56d55e7 9847 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9848 strip = TRUE;
9e2278f5
AM
9849 else if ((h->root.type == bfd_link_hash_undefined
9850 || h->root.type == bfd_link_hash_undefweak)
9851 && h->root.u.undef.abfd != NULL
9852 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9853 strip = TRUE;
c152c796 9854
b8871f35
L
9855 type = h->type;
9856
c152c796 9857 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9858 nothing else to do. However, if it is a forced local symbol or
9859 an ifunc symbol we need to give the backend finish_dynamic_symbol
9860 function a chance to make it dynamic. */
c152c796
AM
9861 if (strip
9862 && h->dynindx == -1
b8871f35 9863 && type != STT_GNU_IFUNC
f5385ebf 9864 && !h->forced_local)
c152c796
AM
9865 return TRUE;
9866
9867 sym.st_value = 0;
9868 sym.st_size = h->size;
9869 sym.st_other = h->other;
c152c796
AM
9870 switch (h->root.type)
9871 {
9872 default:
9873 case bfd_link_hash_new:
9874 case bfd_link_hash_warning:
9875 abort ();
9876 return FALSE;
9877
9878 case bfd_link_hash_undefined:
9879 case bfd_link_hash_undefweak:
9880 input_sec = bfd_und_section_ptr;
9881 sym.st_shndx = SHN_UNDEF;
9882 break;
9883
9884 case bfd_link_hash_defined:
9885 case bfd_link_hash_defweak:
9886 {
9887 input_sec = h->root.u.def.section;
9888 if (input_sec->output_section != NULL)
9889 {
9890 sym.st_shndx =
8b127cbc 9891 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9892 input_sec->output_section);
9893 if (sym.st_shndx == SHN_BAD)
9894 {
4eca0228 9895 _bfd_error_handler
695344c0 9896 /* xgettext:c-format */
871b3ab2 9897 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 9898 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9899 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9900 eoinfo->failed = TRUE;
9901 return FALSE;
9902 }
9903
9904 /* ELF symbols in relocatable files are section relative,
9905 but in nonrelocatable files they are virtual
9906 addresses. */
9907 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9908 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9909 {
9910 sym.st_value += input_sec->output_section->vma;
9911 if (h->type == STT_TLS)
9912 {
8b127cbc 9913 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9914 if (tls_sec != NULL)
9915 sym.st_value -= tls_sec->vma;
c152c796
AM
9916 }
9917 }
9918 }
9919 else
9920 {
9921 BFD_ASSERT (input_sec->owner == NULL
9922 || (input_sec->owner->flags & DYNAMIC) != 0);
9923 sym.st_shndx = SHN_UNDEF;
9924 input_sec = bfd_und_section_ptr;
9925 }
9926 }
9927 break;
9928
9929 case bfd_link_hash_common:
9930 input_sec = h->root.u.c.p->section;
a4d8e49b 9931 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9932 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9933 break;
9934
9935 case bfd_link_hash_indirect:
9936 /* These symbols are created by symbol versioning. They point
9937 to the decorated version of the name. For example, if the
9938 symbol foo@@GNU_1.2 is the default, which should be used when
9939 foo is used with no version, then we add an indirect symbol
9940 foo which points to foo@@GNU_1.2. We ignore these symbols,
9941 since the indirected symbol is already in the hash table. */
9942 return TRUE;
9943 }
9944
b8871f35
L
9945 if (type == STT_COMMON || type == STT_OBJECT)
9946 switch (h->root.type)
9947 {
9948 case bfd_link_hash_common:
9949 type = elf_link_convert_common_type (flinfo->info, type);
9950 break;
9951 case bfd_link_hash_defined:
9952 case bfd_link_hash_defweak:
9953 if (bed->common_definition (&sym))
9954 type = elf_link_convert_common_type (flinfo->info, type);
9955 else
9956 type = STT_OBJECT;
9957 break;
9958 case bfd_link_hash_undefined:
9959 case bfd_link_hash_undefweak:
9960 break;
9961 default:
9962 abort ();
9963 }
9964
4deb8f71 9965 if (h->forced_local)
b8871f35
L
9966 {
9967 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9968 /* Turn off visibility on local symbol. */
9969 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9970 }
9971 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9972 else if (h->unique_global && h->def_regular)
9973 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9974 else if (h->root.type == bfd_link_hash_undefweak
9975 || h->root.type == bfd_link_hash_defweak)
9976 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9977 else
9978 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9979 sym.st_target_internal = h->target_internal;
9980
c152c796
AM
9981 /* Give the processor backend a chance to tweak the symbol value,
9982 and also to finish up anything that needs to be done for this
9983 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9984 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9985 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9986 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9987 && h->def_regular
0e1862bb 9988 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9989 || ((h->dynindx != -1
9990 || h->forced_local)
0e1862bb 9991 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9992 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9993 || h->root.type != bfd_link_hash_undefweak))
9994 || !h->forced_local)
8b127cbc 9995 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9996 {
9997 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9998 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9999 {
10000 eoinfo->failed = TRUE;
10001 return FALSE;
10002 }
10003 }
10004
10005 /* If we are marking the symbol as undefined, and there are no
10006 non-weak references to this symbol from a regular object, then
10007 mark the symbol as weak undefined; if there are non-weak
10008 references, mark the symbol as strong. We can't do this earlier,
10009 because it might not be marked as undefined until the
10010 finish_dynamic_symbol routine gets through with it. */
10011 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10012 && h->ref_regular
c152c796
AM
10013 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10014 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10015 {
10016 int bindtype;
b8871f35 10017 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10018
10019 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10020 if (type == STT_GNU_IFUNC)
10021 type = STT_FUNC;
c152c796 10022
f5385ebf 10023 if (h->ref_regular_nonweak)
c152c796
AM
10024 bindtype = STB_GLOBAL;
10025 else
10026 bindtype = STB_WEAK;
2955ec4c 10027 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10028 }
10029
bda987c2
CD
10030 /* If this is a symbol defined in a dynamic library, don't use the
10031 symbol size from the dynamic library. Relinking an executable
10032 against a new library may introduce gratuitous changes in the
10033 executable's symbols if we keep the size. */
10034 if (sym.st_shndx == SHN_UNDEF
10035 && !h->def_regular
10036 && h->def_dynamic)
10037 sym.st_size = 0;
10038
c152c796
AM
10039 /* If a non-weak symbol with non-default visibility is not defined
10040 locally, it is a fatal error. */
0e1862bb 10041 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10042 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10043 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10044 && h->root.type == bfd_link_hash_undefined
f5385ebf 10045 && !h->def_regular)
c152c796 10046 {
17d078c5
AM
10047 const char *msg;
10048
10049 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10050 /* xgettext:c-format */
871b3ab2 10051 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10052 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10053 /* xgettext:c-format */
871b3ab2 10054 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10055 else
695344c0 10056 /* xgettext:c-format */
871b3ab2 10057 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10058 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10059 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10060 eoinfo->failed = TRUE;
10061 return FALSE;
10062 }
10063
10064 /* If this symbol should be put in the .dynsym section, then put it
10065 there now. We already know the symbol index. We also fill in
10066 the entry in the .hash section. */
1c2649ed
EB
10067 if (h->dynindx != -1
10068 && elf_hash_table (flinfo->info)->dynamic_sections_created
10069 && elf_hash_table (flinfo->info)->dynsym != NULL
10070 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10071 {
c152c796
AM
10072 bfd_byte *esym;
10073
90c984fc
L
10074 /* Since there is no version information in the dynamic string,
10075 if there is no version info in symbol version section, we will
1659f720 10076 have a run-time problem if not linking executable, referenced
4deb8f71 10077 by shared library, or not bound locally. */
1659f720 10078 if (h->verinfo.verdef == NULL
0e1862bb 10079 && (!bfd_link_executable (flinfo->info)
1659f720
L
10080 || h->ref_dynamic
10081 || !h->def_regular))
90c984fc
L
10082 {
10083 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10084
10085 if (p && p [1] != '\0')
10086 {
4eca0228 10087 _bfd_error_handler
695344c0 10088 /* xgettext:c-format */
9793eb77 10089 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10090 flinfo->output_bfd, h->root.root.string);
10091 eoinfo->failed = TRUE;
10092 return FALSE;
10093 }
10094 }
10095
c152c796 10096 sym.st_name = h->dynstr_index;
cae1fbbb
L
10097 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10098 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10099 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10100 {
10101 eoinfo->failed = TRUE;
10102 return FALSE;
10103 }
8b127cbc 10104 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10105
8b127cbc 10106 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10107 {
10108 size_t hash_entry_size;
10109 bfd_byte *bucketpos;
10110 bfd_vma chain;
41198d0c
L
10111 size_t bucketcount;
10112 size_t bucket;
10113
8b127cbc 10114 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10115 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10116
10117 hash_entry_size
8b127cbc
AM
10118 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10119 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10120 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10121 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10122 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10123 bucketpos);
10124 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10125 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10126 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10127 }
c152c796 10128
8b127cbc 10129 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10130 {
10131 Elf_Internal_Versym iversym;
10132 Elf_External_Versym *eversym;
10133
f5385ebf 10134 if (!h->def_regular)
c152c796 10135 {
7b20f099
AM
10136 if (h->verinfo.verdef == NULL
10137 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10138 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10139 iversym.vs_vers = 0;
10140 else
10141 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10142 }
10143 else
10144 {
10145 if (h->verinfo.vertree == NULL)
10146 iversym.vs_vers = 1;
10147 else
10148 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10149 if (flinfo->info->create_default_symver)
3e3b46e5 10150 iversym.vs_vers++;
c152c796
AM
10151 }
10152
422f1182 10153 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10154 defined locally. */
422f1182 10155 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10156 iversym.vs_vers |= VERSYM_HIDDEN;
10157
8b127cbc 10158 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10159 eversym += h->dynindx;
8b127cbc 10160 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10161 }
10162 }
10163
d983c8c5
AM
10164 /* If the symbol is undefined, and we didn't output it to .dynsym,
10165 strip it from .symtab too. Obviously we can't do this for
10166 relocatable output or when needed for --emit-relocs. */
10167 else if (input_sec == bfd_und_section_ptr
10168 && h->indx != -2
66cae560
NC
10169 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10170 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10171 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10172 return TRUE;
66cae560 10173
d983c8c5
AM
10174 /* Also strip others that we couldn't earlier due to dynamic symbol
10175 processing. */
10176 if (strip)
10177 return TRUE;
10178 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10179 return TRUE;
10180
2ec55de3
AM
10181 /* Output a FILE symbol so that following locals are not associated
10182 with the wrong input file. We need one for forced local symbols
10183 if we've seen more than one FILE symbol or when we have exactly
10184 one FILE symbol but global symbols are present in a file other
10185 than the one with the FILE symbol. We also need one if linker
10186 defined symbols are present. In practice these conditions are
10187 always met, so just emit the FILE symbol unconditionally. */
10188 if (eoinfo->localsyms
10189 && !eoinfo->file_sym_done
10190 && eoinfo->flinfo->filesym_count != 0)
10191 {
10192 Elf_Internal_Sym fsym;
10193
10194 memset (&fsym, 0, sizeof (fsym));
10195 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10196 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10197 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10198 bfd_und_section_ptr, NULL))
2ec55de3
AM
10199 return FALSE;
10200
10201 eoinfo->file_sym_done = TRUE;
10202 }
10203
8b127cbc 10204 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10205 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10206 input_sec, h);
6e0b88f1 10207 if (ret == 0)
c152c796
AM
10208 {
10209 eoinfo->failed = TRUE;
10210 return FALSE;
10211 }
6e0b88f1
AM
10212 else if (ret == 1)
10213 h->indx = indx;
10214 else if (h->indx == -2)
10215 abort();
c152c796
AM
10216
10217 return TRUE;
10218}
10219
cdd3575c
AM
10220/* Return TRUE if special handling is done for relocs in SEC against
10221 symbols defined in discarded sections. */
10222
c152c796
AM
10223static bfd_boolean
10224elf_section_ignore_discarded_relocs (asection *sec)
10225{
10226 const struct elf_backend_data *bed;
10227
cdd3575c
AM
10228 switch (sec->sec_info_type)
10229 {
dbaa2011
AM
10230 case SEC_INFO_TYPE_STABS:
10231 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10232 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10233 return TRUE;
10234 default:
10235 break;
10236 }
c152c796
AM
10237
10238 bed = get_elf_backend_data (sec->owner);
10239 if (bed->elf_backend_ignore_discarded_relocs != NULL
10240 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10241 return TRUE;
10242
10243 return FALSE;
10244}
10245
9e66c942
AM
10246/* Return a mask saying how ld should treat relocations in SEC against
10247 symbols defined in discarded sections. If this function returns
10248 COMPLAIN set, ld will issue a warning message. If this function
10249 returns PRETEND set, and the discarded section was link-once and the
10250 same size as the kept link-once section, ld will pretend that the
10251 symbol was actually defined in the kept section. Otherwise ld will
10252 zero the reloc (at least that is the intent, but some cooperation by
10253 the target dependent code is needed, particularly for REL targets). */
10254
8a696751
AM
10255unsigned int
10256_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10257{
9e66c942 10258 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10259 return PRETEND;
cdd3575c
AM
10260
10261 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10262 return 0;
cdd3575c
AM
10263
10264 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10265 return 0;
cdd3575c 10266
9e66c942 10267 return COMPLAIN | PRETEND;
cdd3575c
AM
10268}
10269
3d7f7666
L
10270/* Find a match between a section and a member of a section group. */
10271
10272static asection *
c0f00686
L
10273match_group_member (asection *sec, asection *group,
10274 struct bfd_link_info *info)
3d7f7666
L
10275{
10276 asection *first = elf_next_in_group (group);
10277 asection *s = first;
10278
10279 while (s != NULL)
10280 {
c0f00686 10281 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10282 return s;
10283
83180ade 10284 s = elf_next_in_group (s);
3d7f7666
L
10285 if (s == first)
10286 break;
10287 }
10288
10289 return NULL;
10290}
10291
01b3c8ab 10292/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10293 to replace it. Return the replacement if it is OK. Otherwise return
10294 NULL. */
01b3c8ab
L
10295
10296asection *
c0f00686 10297_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10298{
10299 asection *kept;
10300
10301 kept = sec->kept_section;
10302 if (kept != NULL)
10303 {
c2370991 10304 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10305 kept = match_group_member (sec, kept, info);
1dd2625f
BW
10306 if (kept != NULL
10307 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10308 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 10309 kept = NULL;
c2370991 10310 sec->kept_section = kept;
01b3c8ab
L
10311 }
10312 return kept;
10313}
10314
c152c796
AM
10315/* Link an input file into the linker output file. This function
10316 handles all the sections and relocations of the input file at once.
10317 This is so that we only have to read the local symbols once, and
10318 don't have to keep them in memory. */
10319
10320static bfd_boolean
8b127cbc 10321elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10322{
ece5ef60 10323 int (*relocate_section)
c152c796
AM
10324 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10325 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10326 bfd *output_bfd;
10327 Elf_Internal_Shdr *symtab_hdr;
10328 size_t locsymcount;
10329 size_t extsymoff;
10330 Elf_Internal_Sym *isymbuf;
10331 Elf_Internal_Sym *isym;
10332 Elf_Internal_Sym *isymend;
10333 long *pindex;
10334 asection **ppsection;
10335 asection *o;
10336 const struct elf_backend_data *bed;
c152c796 10337 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10338 bfd_size_type address_size;
10339 bfd_vma r_type_mask;
10340 int r_sym_shift;
ffbc01cc 10341 bfd_boolean have_file_sym = FALSE;
c152c796 10342
8b127cbc 10343 output_bfd = flinfo->output_bfd;
c152c796
AM
10344 bed = get_elf_backend_data (output_bfd);
10345 relocate_section = bed->elf_backend_relocate_section;
10346
10347 /* If this is a dynamic object, we don't want to do anything here:
10348 we don't want the local symbols, and we don't want the section
10349 contents. */
10350 if ((input_bfd->flags & DYNAMIC) != 0)
10351 return TRUE;
10352
c152c796
AM
10353 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10354 if (elf_bad_symtab (input_bfd))
10355 {
10356 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10357 extsymoff = 0;
10358 }
10359 else
10360 {
10361 locsymcount = symtab_hdr->sh_info;
10362 extsymoff = symtab_hdr->sh_info;
10363 }
10364
10365 /* Read the local symbols. */
10366 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10367 if (isymbuf == NULL && locsymcount != 0)
10368 {
10369 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10370 flinfo->internal_syms,
10371 flinfo->external_syms,
10372 flinfo->locsym_shndx);
c152c796
AM
10373 if (isymbuf == NULL)
10374 return FALSE;
10375 }
10376
10377 /* Find local symbol sections and adjust values of symbols in
10378 SEC_MERGE sections. Write out those local symbols we know are
10379 going into the output file. */
10380 isymend = isymbuf + locsymcount;
8b127cbc 10381 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10382 isym < isymend;
10383 isym++, pindex++, ppsection++)
10384 {
10385 asection *isec;
10386 const char *name;
10387 Elf_Internal_Sym osym;
6e0b88f1
AM
10388 long indx;
10389 int ret;
c152c796
AM
10390
10391 *pindex = -1;
10392
10393 if (elf_bad_symtab (input_bfd))
10394 {
10395 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10396 {
10397 *ppsection = NULL;
10398 continue;
10399 }
10400 }
10401
10402 if (isym->st_shndx == SHN_UNDEF)
10403 isec = bfd_und_section_ptr;
c152c796
AM
10404 else if (isym->st_shndx == SHN_ABS)
10405 isec = bfd_abs_section_ptr;
10406 else if (isym->st_shndx == SHN_COMMON)
10407 isec = bfd_com_section_ptr;
10408 else
10409 {
cb33740c
AM
10410 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10411 if (isec == NULL)
10412 {
10413 /* Don't attempt to output symbols with st_shnx in the
10414 reserved range other than SHN_ABS and SHN_COMMON. */
10415 *ppsection = NULL;
10416 continue;
10417 }
dbaa2011 10418 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10419 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10420 isym->st_value =
10421 _bfd_merged_section_offset (output_bfd, &isec,
10422 elf_section_data (isec)->sec_info,
10423 isym->st_value);
c152c796
AM
10424 }
10425
10426 *ppsection = isec;
10427
d983c8c5
AM
10428 /* Don't output the first, undefined, symbol. In fact, don't
10429 output any undefined local symbol. */
10430 if (isec == bfd_und_section_ptr)
c152c796
AM
10431 continue;
10432
10433 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10434 {
10435 /* We never output section symbols. Instead, we use the
10436 section symbol of the corresponding section in the output
10437 file. */
10438 continue;
10439 }
10440
10441 /* If we are stripping all symbols, we don't want to output this
10442 one. */
8b127cbc 10443 if (flinfo->info->strip == strip_all)
c152c796
AM
10444 continue;
10445
10446 /* If we are discarding all local symbols, we don't want to
10447 output this one. If we are generating a relocatable output
10448 file, then some of the local symbols may be required by
10449 relocs; we output them below as we discover that they are
10450 needed. */
8b127cbc 10451 if (flinfo->info->discard == discard_all)
c152c796
AM
10452 continue;
10453
10454 /* If this symbol is defined in a section which we are
f02571c5
AM
10455 discarding, we don't need to keep it. */
10456 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10457 && isym->st_shndx < SHN_LORESERVE
10458 && bfd_section_removed_from_list (output_bfd,
10459 isec->output_section))
e75a280b
L
10460 continue;
10461
c152c796
AM
10462 /* Get the name of the symbol. */
10463 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10464 isym->st_name);
10465 if (name == NULL)
10466 return FALSE;
10467
10468 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10469 if ((flinfo->info->strip == strip_some
10470 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10471 == NULL))
8b127cbc 10472 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10473 && (isec->flags & SEC_MERGE)
10474 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10475 || flinfo->info->discard == discard_l)
c152c796
AM
10476 && bfd_is_local_label_name (input_bfd, name)))
10477 continue;
10478
ffbc01cc
AM
10479 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10480 {
ce875075
AM
10481 if (input_bfd->lto_output)
10482 /* -flto puts a temp file name here. This means builds
10483 are not reproducible. Discard the symbol. */
10484 continue;
ffbc01cc
AM
10485 have_file_sym = TRUE;
10486 flinfo->filesym_count += 1;
10487 }
10488 if (!have_file_sym)
10489 {
10490 /* In the absence of debug info, bfd_find_nearest_line uses
10491 FILE symbols to determine the source file for local
10492 function symbols. Provide a FILE symbol here if input
10493 files lack such, so that their symbols won't be
10494 associated with a previous input file. It's not the
10495 source file, but the best we can do. */
10496 have_file_sym = TRUE;
10497 flinfo->filesym_count += 1;
10498 memset (&osym, 0, sizeof (osym));
10499 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10500 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10501 if (!elf_link_output_symstrtab (flinfo,
10502 (input_bfd->lto_output ? NULL
10503 : input_bfd->filename),
10504 &osym, bfd_abs_section_ptr,
10505 NULL))
ffbc01cc
AM
10506 return FALSE;
10507 }
10508
c152c796
AM
10509 osym = *isym;
10510
10511 /* Adjust the section index for the output file. */
10512 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10513 isec->output_section);
10514 if (osym.st_shndx == SHN_BAD)
10515 return FALSE;
10516
c152c796
AM
10517 /* ELF symbols in relocatable files are section relative, but
10518 in executable files they are virtual addresses. Note that
10519 this code assumes that all ELF sections have an associated
10520 BFD section with a reasonable value for output_offset; below
10521 we assume that they also have a reasonable value for
10522 output_section. Any special sections must be set up to meet
10523 these requirements. */
10524 osym.st_value += isec->output_offset;
0e1862bb 10525 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10526 {
10527 osym.st_value += isec->output_section->vma;
10528 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10529 {
10530 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10531 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10532 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10533 else
10534 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10535 STT_NOTYPE);
c152c796
AM
10536 }
10537 }
10538
6e0b88f1 10539 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10540 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10541 if (ret == 0)
c152c796 10542 return FALSE;
6e0b88f1
AM
10543 else if (ret == 1)
10544 *pindex = indx;
c152c796
AM
10545 }
10546
310fd250
L
10547 if (bed->s->arch_size == 32)
10548 {
10549 r_type_mask = 0xff;
10550 r_sym_shift = 8;
10551 address_size = 4;
10552 }
10553 else
10554 {
10555 r_type_mask = 0xffffffff;
10556 r_sym_shift = 32;
10557 address_size = 8;
10558 }
10559
c152c796
AM
10560 /* Relocate the contents of each section. */
10561 sym_hashes = elf_sym_hashes (input_bfd);
10562 for (o = input_bfd->sections; o != NULL; o = o->next)
10563 {
10564 bfd_byte *contents;
10565
10566 if (! o->linker_mark)
10567 {
10568 /* This section was omitted from the link. */
10569 continue;
10570 }
10571
7bdf4127 10572 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10573 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10574 {
10575 /* Deal with the group signature symbol. */
10576 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10577 unsigned long symndx = sec_data->this_hdr.sh_info;
10578 asection *osec = o->output_section;
10579
7bdf4127 10580 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
10581 if (symndx >= locsymcount
10582 || (elf_bad_symtab (input_bfd)
8b127cbc 10583 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10584 {
10585 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10586 while (h->root.type == bfd_link_hash_indirect
10587 || h->root.type == bfd_link_hash_warning)
10588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10589 /* Arrange for symbol to be output. */
10590 h->indx = -2;
10591 elf_section_data (osec)->this_hdr.sh_info = -2;
10592 }
10593 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10594 {
10595 /* We'll use the output section target_index. */
8b127cbc 10596 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10597 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10598 }
10599 else
10600 {
8b127cbc 10601 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10602 {
10603 /* Otherwise output the local symbol now. */
10604 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10605 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10606 const char *name;
6e0b88f1
AM
10607 long indx;
10608 int ret;
bcacc0f5
AM
10609
10610 name = bfd_elf_string_from_elf_section (input_bfd,
10611 symtab_hdr->sh_link,
10612 sym.st_name);
10613 if (name == NULL)
10614 return FALSE;
10615
10616 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10617 sec);
10618 if (sym.st_shndx == SHN_BAD)
10619 return FALSE;
10620
10621 sym.st_value += o->output_offset;
10622
6e0b88f1 10623 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10624 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10625 NULL);
6e0b88f1 10626 if (ret == 0)
bcacc0f5 10627 return FALSE;
6e0b88f1 10628 else if (ret == 1)
8b127cbc 10629 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10630 else
10631 abort ();
bcacc0f5
AM
10632 }
10633 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10634 = flinfo->indices[symndx];
bcacc0f5
AM
10635 }
10636 }
10637
c152c796 10638 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10639 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10640 continue;
10641
10642 if ((o->flags & SEC_LINKER_CREATED) != 0)
10643 {
10644 /* Section was created by _bfd_elf_link_create_dynamic_sections
10645 or somesuch. */
10646 continue;
10647 }
10648
10649 /* Get the contents of the section. They have been cached by a
10650 relaxation routine. Note that o is a section in an input
10651 file, so the contents field will not have been set by any of
10652 the routines which work on output files. */
10653 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10654 {
10655 contents = elf_section_data (o)->this_hdr.contents;
10656 if (bed->caches_rawsize
10657 && o->rawsize != 0
10658 && o->rawsize < o->size)
10659 {
10660 memcpy (flinfo->contents, contents, o->rawsize);
10661 contents = flinfo->contents;
10662 }
10663 }
c152c796
AM
10664 else
10665 {
8b127cbc 10666 contents = flinfo->contents;
4a114e3e 10667 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10668 return FALSE;
10669 }
10670
10671 if ((o->flags & SEC_RELOC) != 0)
10672 {
10673 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10674 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10675 int action_discarded;
ece5ef60 10676 int ret;
c152c796
AM
10677
10678 /* Get the swapped relocs. */
10679 internal_relocs
8b127cbc
AM
10680 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10681 flinfo->internal_relocs, FALSE);
c152c796
AM
10682 if (internal_relocs == NULL
10683 && o->reloc_count > 0)
10684 return FALSE;
10685
310fd250
L
10686 /* We need to reverse-copy input .ctors/.dtors sections if
10687 they are placed in .init_array/.finit_array for output. */
10688 if (o->size > address_size
10689 && ((strncmp (o->name, ".ctors", 6) == 0
10690 && strcmp (o->output_section->name,
10691 ".init_array") == 0)
10692 || (strncmp (o->name, ".dtors", 6) == 0
10693 && strcmp (o->output_section->name,
10694 ".fini_array") == 0))
10695 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10696 {
056bafd4
MR
10697 if (o->size * bed->s->int_rels_per_ext_rel
10698 != o->reloc_count * address_size)
310fd250 10699 {
4eca0228 10700 _bfd_error_handler
695344c0 10701 /* xgettext:c-format */
871b3ab2 10702 (_("error: %pB: size of section %pA is not "
310fd250
L
10703 "multiple of address size"),
10704 input_bfd, o);
8c6716e5 10705 bfd_set_error (bfd_error_bad_value);
310fd250
L
10706 return FALSE;
10707 }
10708 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10709 }
10710
0f02bbd9 10711 action_discarded = -1;
c152c796 10712 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10713 action_discarded = (*bed->action_discarded) (o);
10714
10715 /* Run through the relocs evaluating complex reloc symbols and
10716 looking for relocs against symbols from discarded sections
10717 or section symbols from removed link-once sections.
10718 Complain about relocs against discarded sections. Zero
10719 relocs against removed link-once sections. */
10720
10721 rel = internal_relocs;
056bafd4 10722 relend = rel + o->reloc_count;
0f02bbd9 10723 for ( ; rel < relend; rel++)
c152c796 10724 {
0f02bbd9
AM
10725 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10726 unsigned int s_type;
10727 asection **ps, *sec;
10728 struct elf_link_hash_entry *h = NULL;
10729 const char *sym_name;
c152c796 10730
0f02bbd9
AM
10731 if (r_symndx == STN_UNDEF)
10732 continue;
c152c796 10733
0f02bbd9
AM
10734 if (r_symndx >= locsymcount
10735 || (elf_bad_symtab (input_bfd)
8b127cbc 10736 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10737 {
10738 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10739
0f02bbd9
AM
10740 /* Badly formatted input files can contain relocs that
10741 reference non-existant symbols. Check here so that
10742 we do not seg fault. */
10743 if (h == NULL)
c152c796 10744 {
4eca0228 10745 _bfd_error_handler
695344c0 10746 /* xgettext:c-format */
2dcf00ce 10747 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 10748 "that references a non-existent global symbol"),
2dcf00ce 10749 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
10750 bfd_set_error (bfd_error_bad_value);
10751 return FALSE;
10752 }
3b36f7e6 10753
0f02bbd9
AM
10754 while (h->root.type == bfd_link_hash_indirect
10755 || h->root.type == bfd_link_hash_warning)
10756 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10757
0f02bbd9 10758 s_type = h->type;
cdd3575c 10759
9e2dec47 10760 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10761 mark the symbol as undefined. Note that the
10762 linker may attach linker created dynamic sections
10763 to the plugin bfd. Symbols defined in linker
10764 created sections are not plugin symbols. */
bc4e12de 10765 if ((h->root.non_ir_ref_regular
4070765b 10766 || h->root.non_ir_ref_dynamic)
9e2dec47
L
10767 && (h->root.type == bfd_link_hash_defined
10768 || h->root.type == bfd_link_hash_defweak)
10769 && (h->root.u.def.section->flags
10770 & SEC_LINKER_CREATED) == 0
10771 && h->root.u.def.section->owner != NULL
10772 && (h->root.u.def.section->owner->flags
10773 & BFD_PLUGIN) != 0)
10774 {
10775 h->root.type = bfd_link_hash_undefined;
10776 h->root.u.undef.abfd = h->root.u.def.section->owner;
10777 }
10778
0f02bbd9
AM
10779 ps = NULL;
10780 if (h->root.type == bfd_link_hash_defined
10781 || h->root.type == bfd_link_hash_defweak)
10782 ps = &h->root.u.def.section;
10783
10784 sym_name = h->root.root.string;
10785 }
10786 else
10787 {
10788 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10789
10790 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10791 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10792 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10793 sym, *ps);
10794 }
c152c796 10795
c301e700 10796 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10797 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10798 {
10799 bfd_vma val;
10800 bfd_vma dot = (rel->r_offset
10801 + o->output_offset + o->output_section->vma);
10802#ifdef DEBUG
10803 printf ("Encountered a complex symbol!");
10804 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10805 input_bfd->filename, o->name,
10806 (long) (rel - internal_relocs));
0f02bbd9
AM
10807 printf (" symbol: idx %8.8lx, name %s\n",
10808 r_symndx, sym_name);
10809 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10810 (unsigned long) rel->r_info,
10811 (unsigned long) rel->r_offset);
10812#endif
8b127cbc 10813 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10814 isymbuf, locsymcount, s_type == STT_SRELC))
10815 return FALSE;
10816
10817 /* Symbol evaluated OK. Update to absolute value. */
10818 set_symbol_value (input_bfd, isymbuf, locsymcount,
10819 r_symndx, val);
10820 continue;
10821 }
10822
10823 if (action_discarded != -1 && ps != NULL)
10824 {
cdd3575c
AM
10825 /* Complain if the definition comes from a
10826 discarded section. */
dbaa2011 10827 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10828 {
cf35638d 10829 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10830 if (action_discarded & COMPLAIN)
8b127cbc 10831 (*flinfo->info->callbacks->einfo)
695344c0 10832 /* xgettext:c-format */
871b3ab2
AM
10833 (_("%X`%s' referenced in section `%pA' of %pB: "
10834 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 10835 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10836
87e5235d 10837 /* Try to do the best we can to support buggy old
e0ae6d6f 10838 versions of gcc. Pretend that the symbol is
87e5235d
AM
10839 really defined in the kept linkonce section.
10840 FIXME: This is quite broken. Modifying the
10841 symbol here means we will be changing all later
e0ae6d6f 10842 uses of the symbol, not just in this section. */
0f02bbd9 10843 if (action_discarded & PRETEND)
87e5235d 10844 {
01b3c8ab
L
10845 asection *kept;
10846
c0f00686 10847 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10848 flinfo->info);
01b3c8ab 10849 if (kept != NULL)
87e5235d
AM
10850 {
10851 *ps = kept;
10852 continue;
10853 }
10854 }
c152c796
AM
10855 }
10856 }
10857 }
10858
10859 /* Relocate the section by invoking a back end routine.
10860
10861 The back end routine is responsible for adjusting the
10862 section contents as necessary, and (if using Rela relocs
10863 and generating a relocatable output file) adjusting the
10864 reloc addend as necessary.
10865
10866 The back end routine does not have to worry about setting
10867 the reloc address or the reloc symbol index.
10868
10869 The back end routine is given a pointer to the swapped in
10870 internal symbols, and can access the hash table entries
10871 for the external symbols via elf_sym_hashes (input_bfd).
10872
10873 When generating relocatable output, the back end routine
10874 must handle STB_LOCAL/STT_SECTION symbols specially. The
10875 output symbol is going to be a section symbol
10876 corresponding to the output section, which will require
10877 the addend to be adjusted. */
10878
8b127cbc 10879 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10880 input_bfd, o, contents,
10881 internal_relocs,
10882 isymbuf,
8b127cbc 10883 flinfo->sections);
ece5ef60 10884 if (!ret)
c152c796
AM
10885 return FALSE;
10886
ece5ef60 10887 if (ret == 2
0e1862bb 10888 || bfd_link_relocatable (flinfo->info)
8b127cbc 10889 || flinfo->info->emitrelocations)
c152c796
AM
10890 {
10891 Elf_Internal_Rela *irela;
d4730f92 10892 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10893 bfd_vma last_offset;
10894 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10895 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10896 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10897 unsigned int next_erel;
c152c796 10898 bfd_boolean rela_normal;
d4730f92 10899 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10900
d4730f92
BS
10901 esdi = elf_section_data (o);
10902 esdo = elf_section_data (o->output_section);
10903 rela_normal = FALSE;
c152c796
AM
10904
10905 /* Adjust the reloc addresses and symbol indices. */
10906
10907 irela = internal_relocs;
056bafd4 10908 irelaend = irela + o->reloc_count;
d4730f92
BS
10909 rel_hash = esdo->rel.hashes + esdo->rel.count;
10910 /* We start processing the REL relocs, if any. When we reach
10911 IRELAMID in the loop, we switch to the RELA relocs. */
10912 irelamid = irela;
10913 if (esdi->rel.hdr != NULL)
10914 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10915 * bed->s->int_rels_per_ext_rel);
eac338cf 10916 rel_hash_list = rel_hash;
d4730f92 10917 rela_hash_list = NULL;
c152c796 10918 last_offset = o->output_offset;
0e1862bb 10919 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10920 last_offset += o->output_section->vma;
10921 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10922 {
10923 unsigned long r_symndx;
10924 asection *sec;
10925 Elf_Internal_Sym sym;
10926
10927 if (next_erel == bed->s->int_rels_per_ext_rel)
10928 {
10929 rel_hash++;
10930 next_erel = 0;
10931 }
10932
d4730f92
BS
10933 if (irela == irelamid)
10934 {
10935 rel_hash = esdo->rela.hashes + esdo->rela.count;
10936 rela_hash_list = rel_hash;
10937 rela_normal = bed->rela_normal;
10938 }
10939
c152c796 10940 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10941 flinfo->info, o,
c152c796
AM
10942 irela->r_offset);
10943 if (irela->r_offset >= (bfd_vma) -2)
10944 {
10945 /* This is a reloc for a deleted entry or somesuch.
10946 Turn it into an R_*_NONE reloc, at the same
10947 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10948 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10949 being ordered. */
10950 irela->r_offset = last_offset;
10951 irela->r_info = 0;
10952 irela->r_addend = 0;
10953 continue;
10954 }
10955
10956 irela->r_offset += o->output_offset;
10957
10958 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10959 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10960 irela->r_offset += o->output_section->vma;
10961
10962 last_offset = irela->r_offset;
10963
10964 r_symndx = irela->r_info >> r_sym_shift;
10965 if (r_symndx == STN_UNDEF)
10966 continue;
10967
10968 if (r_symndx >= locsymcount
10969 || (elf_bad_symtab (input_bfd)
8b127cbc 10970 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10971 {
10972 struct elf_link_hash_entry *rh;
10973 unsigned long indx;
10974
10975 /* This is a reloc against a global symbol. We
10976 have not yet output all the local symbols, so
10977 we do not know the symbol index of any global
10978 symbol. We set the rel_hash entry for this
10979 reloc to point to the global hash table entry
10980 for this symbol. The symbol index is then
ee75fd95 10981 set at the end of bfd_elf_final_link. */
c152c796
AM
10982 indx = r_symndx - extsymoff;
10983 rh = elf_sym_hashes (input_bfd)[indx];
10984 while (rh->root.type == bfd_link_hash_indirect
10985 || rh->root.type == bfd_link_hash_warning)
10986 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10987
10988 /* Setting the index to -2 tells
10989 elf_link_output_extsym that this symbol is
10990 used by a reloc. */
10991 BFD_ASSERT (rh->indx < 0);
10992 rh->indx = -2;
c152c796
AM
10993 *rel_hash = rh;
10994
10995 continue;
10996 }
10997
10998 /* This is a reloc against a local symbol. */
10999
11000 *rel_hash = NULL;
11001 sym = isymbuf[r_symndx];
8b127cbc 11002 sec = flinfo->sections[r_symndx];
c152c796
AM
11003 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11004 {
11005 /* I suppose the backend ought to fill in the
11006 section of any STT_SECTION symbol against a
6a8d1586 11007 processor specific section. */
cf35638d 11008 r_symndx = STN_UNDEF;
6a8d1586
AM
11009 if (bfd_is_abs_section (sec))
11010 ;
c152c796
AM
11011 else if (sec == NULL || sec->owner == NULL)
11012 {
11013 bfd_set_error (bfd_error_bad_value);
11014 return FALSE;
11015 }
11016 else
11017 {
6a8d1586
AM
11018 asection *osec = sec->output_section;
11019
11020 /* If we have discarded a section, the output
11021 section will be the absolute section. In
ab96bf03
AM
11022 case of discarded SEC_MERGE sections, use
11023 the kept section. relocate_section should
11024 have already handled discarded linkonce
11025 sections. */
6a8d1586
AM
11026 if (bfd_is_abs_section (osec)
11027 && sec->kept_section != NULL
11028 && sec->kept_section->output_section != NULL)
11029 {
11030 osec = sec->kept_section->output_section;
11031 irela->r_addend -= osec->vma;
11032 }
11033
11034 if (!bfd_is_abs_section (osec))
11035 {
11036 r_symndx = osec->target_index;
cf35638d 11037 if (r_symndx == STN_UNDEF)
74541ad4 11038 {
051d833a
AM
11039 irela->r_addend += osec->vma;
11040 osec = _bfd_nearby_section (output_bfd, osec,
11041 osec->vma);
11042 irela->r_addend -= osec->vma;
11043 r_symndx = osec->target_index;
74541ad4 11044 }
6a8d1586 11045 }
c152c796
AM
11046 }
11047
11048 /* Adjust the addend according to where the
11049 section winds up in the output section. */
11050 if (rela_normal)
11051 irela->r_addend += sec->output_offset;
11052 }
11053 else
11054 {
8b127cbc 11055 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11056 {
11057 unsigned long shlink;
11058 const char *name;
11059 asection *osec;
6e0b88f1 11060 long indx;
c152c796 11061
8b127cbc 11062 if (flinfo->info->strip == strip_all)
c152c796
AM
11063 {
11064 /* You can't do ld -r -s. */
11065 bfd_set_error (bfd_error_invalid_operation);
11066 return FALSE;
11067 }
11068
11069 /* This symbol was skipped earlier, but
11070 since it is needed by a reloc, we
11071 must output it now. */
11072 shlink = symtab_hdr->sh_link;
11073 name = (bfd_elf_string_from_elf_section
11074 (input_bfd, shlink, sym.st_name));
11075 if (name == NULL)
11076 return FALSE;
11077
11078 osec = sec->output_section;
11079 sym.st_shndx =
11080 _bfd_elf_section_from_bfd_section (output_bfd,
11081 osec);
11082 if (sym.st_shndx == SHN_BAD)
11083 return FALSE;
11084
11085 sym.st_value += sec->output_offset;
0e1862bb 11086 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11087 {
11088 sym.st_value += osec->vma;
11089 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11090 {
102def4d
AM
11091 struct elf_link_hash_table *htab
11092 = elf_hash_table (flinfo->info);
11093
c152c796
AM
11094 /* STT_TLS symbols are relative to PT_TLS
11095 segment base. */
102def4d
AM
11096 if (htab->tls_sec != NULL)
11097 sym.st_value -= htab->tls_sec->vma;
11098 else
11099 sym.st_info
11100 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11101 STT_NOTYPE);
c152c796
AM
11102 }
11103 }
11104
6e0b88f1 11105 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11106 ret = elf_link_output_symstrtab (flinfo, name,
11107 &sym, sec,
11108 NULL);
6e0b88f1 11109 if (ret == 0)
c152c796 11110 return FALSE;
6e0b88f1 11111 else if (ret == 1)
8b127cbc 11112 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11113 else
11114 abort ();
c152c796
AM
11115 }
11116
8b127cbc 11117 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11118 }
11119
11120 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11121 | (irela->r_info & r_type_mask));
11122 }
11123
11124 /* Swap out the relocs. */
d4730f92
BS
11125 input_rel_hdr = esdi->rel.hdr;
11126 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11127 {
d4730f92
BS
11128 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11129 input_rel_hdr,
11130 internal_relocs,
11131 rel_hash_list))
11132 return FALSE;
c152c796
AM
11133 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11134 * bed->s->int_rels_per_ext_rel);
eac338cf 11135 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11136 }
11137
11138 input_rela_hdr = esdi->rela.hdr;
11139 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11140 {
eac338cf 11141 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11142 input_rela_hdr,
eac338cf 11143 internal_relocs,
d4730f92 11144 rela_hash_list))
c152c796
AM
11145 return FALSE;
11146 }
11147 }
11148 }
11149
11150 /* Write out the modified section contents. */
11151 if (bed->elf_backend_write_section
8b127cbc 11152 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11153 contents))
c152c796
AM
11154 {
11155 /* Section written out. */
11156 }
11157 else switch (o->sec_info_type)
11158 {
dbaa2011 11159 case SEC_INFO_TYPE_STABS:
c152c796
AM
11160 if (! (_bfd_write_section_stabs
11161 (output_bfd,
8b127cbc 11162 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11163 o, &elf_section_data (o)->sec_info, contents)))
11164 return FALSE;
11165 break;
dbaa2011 11166 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11167 if (! _bfd_write_merged_section (output_bfd, o,
11168 elf_section_data (o)->sec_info))
11169 return FALSE;
11170 break;
dbaa2011 11171 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11172 {
8b127cbc 11173 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11174 o, contents))
11175 return FALSE;
11176 }
11177 break;
2f0c68f2
CM
11178 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11179 {
11180 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11181 flinfo->info,
11182 o, contents))
11183 return FALSE;
11184 }
11185 break;
c152c796
AM
11186 default:
11187 {
310fd250
L
11188 if (! (o->flags & SEC_EXCLUDE))
11189 {
11190 file_ptr offset = (file_ptr) o->output_offset;
11191 bfd_size_type todo = o->size;
37b01f6a
DG
11192
11193 offset *= bfd_octets_per_byte (output_bfd);
11194
310fd250
L
11195 if ((o->flags & SEC_ELF_REVERSE_COPY))
11196 {
11197 /* Reverse-copy input section to output. */
11198 do
11199 {
11200 todo -= address_size;
11201 if (! bfd_set_section_contents (output_bfd,
11202 o->output_section,
11203 contents + todo,
11204 offset,
11205 address_size))
11206 return FALSE;
11207 if (todo == 0)
11208 break;
11209 offset += address_size;
11210 }
11211 while (1);
11212 }
11213 else if (! bfd_set_section_contents (output_bfd,
11214 o->output_section,
11215 contents,
11216 offset, todo))
11217 return FALSE;
11218 }
c152c796
AM
11219 }
11220 break;
11221 }
11222 }
11223
11224 return TRUE;
11225}
11226
11227/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11228 requested by the linker, and does not come from any input file. This
c152c796
AM
11229 is used to build constructor and destructor tables when linking
11230 with -Ur. */
11231
11232static bfd_boolean
11233elf_reloc_link_order (bfd *output_bfd,
11234 struct bfd_link_info *info,
11235 asection *output_section,
11236 struct bfd_link_order *link_order)
11237{
11238 reloc_howto_type *howto;
11239 long indx;
11240 bfd_vma offset;
11241 bfd_vma addend;
d4730f92 11242 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11243 struct elf_link_hash_entry **rel_hash_ptr;
11244 Elf_Internal_Shdr *rel_hdr;
11245 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11246 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11247 bfd_byte *erel;
11248 unsigned int i;
d4730f92 11249 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11250
11251 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11252 if (howto == NULL)
11253 {
11254 bfd_set_error (bfd_error_bad_value);
11255 return FALSE;
11256 }
11257
11258 addend = link_order->u.reloc.p->addend;
11259
d4730f92
BS
11260 if (esdo->rel.hdr)
11261 reldata = &esdo->rel;
11262 else if (esdo->rela.hdr)
11263 reldata = &esdo->rela;
11264 else
11265 {
11266 reldata = NULL;
11267 BFD_ASSERT (0);
11268 }
11269
c152c796 11270 /* Figure out the symbol index. */
d4730f92 11271 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11272 if (link_order->type == bfd_section_reloc_link_order)
11273 {
11274 indx = link_order->u.reloc.p->u.section->target_index;
11275 BFD_ASSERT (indx != 0);
11276 *rel_hash_ptr = NULL;
11277 }
11278 else
11279 {
11280 struct elf_link_hash_entry *h;
11281
11282 /* Treat a reloc against a defined symbol as though it were
11283 actually against the section. */
11284 h = ((struct elf_link_hash_entry *)
11285 bfd_wrapped_link_hash_lookup (output_bfd, info,
11286 link_order->u.reloc.p->u.name,
11287 FALSE, FALSE, TRUE));
11288 if (h != NULL
11289 && (h->root.type == bfd_link_hash_defined
11290 || h->root.type == bfd_link_hash_defweak))
11291 {
11292 asection *section;
11293
11294 section = h->root.u.def.section;
11295 indx = section->output_section->target_index;
11296 *rel_hash_ptr = NULL;
11297 /* It seems that we ought to add the symbol value to the
11298 addend here, but in practice it has already been added
11299 because it was passed to constructor_callback. */
11300 addend += section->output_section->vma + section->output_offset;
11301 }
11302 else if (h != NULL)
11303 {
11304 /* Setting the index to -2 tells elf_link_output_extsym that
11305 this symbol is used by a reloc. */
11306 h->indx = -2;
11307 *rel_hash_ptr = h;
11308 indx = 0;
11309 }
11310 else
11311 {
1a72702b
AM
11312 (*info->callbacks->unattached_reloc)
11313 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11314 indx = 0;
11315 }
11316 }
11317
11318 /* If this is an inplace reloc, we must write the addend into the
11319 object file. */
11320 if (howto->partial_inplace && addend != 0)
11321 {
11322 bfd_size_type size;
11323 bfd_reloc_status_type rstat;
11324 bfd_byte *buf;
11325 bfd_boolean ok;
11326 const char *sym_name;
11327
a50b1753
NC
11328 size = (bfd_size_type) bfd_get_reloc_size (howto);
11329 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11330 if (buf == NULL && size != 0)
c152c796
AM
11331 return FALSE;
11332 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11333 switch (rstat)
11334 {
11335 case bfd_reloc_ok:
11336 break;
11337
11338 default:
11339 case bfd_reloc_outofrange:
11340 abort ();
11341
11342 case bfd_reloc_overflow:
11343 if (link_order->type == bfd_section_reloc_link_order)
11344 sym_name = bfd_section_name (output_bfd,
11345 link_order->u.reloc.p->u.section);
11346 else
11347 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11348 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11349 howto->name, addend, NULL, NULL,
11350 (bfd_vma) 0);
c152c796
AM
11351 break;
11352 }
37b01f6a 11353
c152c796 11354 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
11355 link_order->offset
11356 * bfd_octets_per_byte (output_bfd),
11357 size);
c152c796
AM
11358 free (buf);
11359 if (! ok)
11360 return FALSE;
11361 }
11362
11363 /* The address of a reloc is relative to the section in a
11364 relocatable file, and is a virtual address in an executable
11365 file. */
11366 offset = link_order->offset;
0e1862bb 11367 if (! bfd_link_relocatable (info))
c152c796
AM
11368 offset += output_section->vma;
11369
11370 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11371 {
11372 irel[i].r_offset = offset;
11373 irel[i].r_info = 0;
11374 irel[i].r_addend = 0;
11375 }
11376 if (bed->s->arch_size == 32)
11377 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11378 else
11379 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11380
d4730f92 11381 rel_hdr = reldata->hdr;
c152c796
AM
11382 erel = rel_hdr->contents;
11383 if (rel_hdr->sh_type == SHT_REL)
11384 {
d4730f92 11385 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11386 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11387 }
11388 else
11389 {
11390 irel[0].r_addend = addend;
d4730f92 11391 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11392 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11393 }
11394
d4730f92 11395 ++reldata->count;
c152c796
AM
11396
11397 return TRUE;
11398}
11399
0b52efa6
PB
11400
11401/* Get the output vma of the section pointed to by the sh_link field. */
11402
11403static bfd_vma
11404elf_get_linked_section_vma (struct bfd_link_order *p)
11405{
11406 Elf_Internal_Shdr **elf_shdrp;
11407 asection *s;
11408 int elfsec;
11409
11410 s = p->u.indirect.section;
11411 elf_shdrp = elf_elfsections (s->owner);
11412 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11413 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11414 /* PR 290:
11415 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11416 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11417 sh_info fields. Hence we could get the situation
11418 where elfsec is 0. */
11419 if (elfsec == 0)
11420 {
11421 const struct elf_backend_data *bed
11422 = get_elf_backend_data (s->owner);
11423 if (bed->link_order_error_handler)
d003868e 11424 bed->link_order_error_handler
695344c0 11425 /* xgettext:c-format */
871b3ab2 11426 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
185d09ad
L
11427 return 0;
11428 }
11429 else
11430 {
11431 s = elf_shdrp[elfsec]->bfd_section;
11432 return s->output_section->vma + s->output_offset;
11433 }
0b52efa6
PB
11434}
11435
11436
11437/* Compare two sections based on the locations of the sections they are
11438 linked to. Used by elf_fixup_link_order. */
11439
11440static int
11441compare_link_order (const void * a, const void * b)
11442{
11443 bfd_vma apos;
11444 bfd_vma bpos;
11445
11446 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11447 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11448 if (apos < bpos)
11449 return -1;
11450 return apos > bpos;
11451}
11452
11453
11454/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11455 order as their linked sections. Returns false if this could not be done
11456 because an output section includes both ordered and unordered
11457 sections. Ideally we'd do this in the linker proper. */
11458
11459static bfd_boolean
11460elf_fixup_link_order (bfd *abfd, asection *o)
11461{
11462 int seen_linkorder;
11463 int seen_other;
11464 int n;
11465 struct bfd_link_order *p;
11466 bfd *sub;
11467 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11468 unsigned elfsec;
0b52efa6 11469 struct bfd_link_order **sections;
d33cdfe3 11470 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11471 bfd_vma offset;
3b36f7e6 11472
d33cdfe3
L
11473 other_sec = NULL;
11474 linkorder_sec = NULL;
0b52efa6
PB
11475 seen_other = 0;
11476 seen_linkorder = 0;
8423293d 11477 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11478 {
d33cdfe3 11479 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11480 {
11481 s = p->u.indirect.section;
d33cdfe3
L
11482 sub = s->owner;
11483 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11484 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11485 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11486 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11487 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11488 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11489 {
11490 seen_linkorder++;
11491 linkorder_sec = s;
11492 }
0b52efa6 11493 else
d33cdfe3
L
11494 {
11495 seen_other++;
11496 other_sec = s;
11497 }
0b52efa6
PB
11498 }
11499 else
11500 seen_other++;
d33cdfe3
L
11501
11502 if (seen_other && seen_linkorder)
11503 {
11504 if (other_sec && linkorder_sec)
4eca0228 11505 _bfd_error_handler
695344c0 11506 /* xgettext:c-format */
871b3ab2
AM
11507 (_("%pA has both ordered [`%pA' in %pB] "
11508 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11509 o, linkorder_sec, linkorder_sec->owner,
11510 other_sec, other_sec->owner);
d33cdfe3 11511 else
4eca0228 11512 _bfd_error_handler
871b3ab2 11513 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11514 bfd_set_error (bfd_error_bad_value);
11515 return FALSE;
11516 }
0b52efa6
PB
11517 }
11518
11519 if (!seen_linkorder)
11520 return TRUE;
11521
0b52efa6 11522 sections = (struct bfd_link_order **)
14b1c01e
AM
11523 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11524 if (sections == NULL)
11525 return FALSE;
0b52efa6 11526 seen_linkorder = 0;
3b36f7e6 11527
8423293d 11528 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11529 {
11530 sections[seen_linkorder++] = p;
11531 }
11532 /* Sort the input sections in the order of their linked section. */
11533 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11534 compare_link_order);
11535
11536 /* Change the offsets of the sections. */
11537 offset = 0;
11538 for (n = 0; n < seen_linkorder; n++)
11539 {
11540 s = sections[n]->u.indirect.section;
461686a3 11541 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11542 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11543 sections[n]->offset = offset;
11544 offset += sections[n]->size;
11545 }
11546
4dd07732 11547 free (sections);
0b52efa6
PB
11548 return TRUE;
11549}
11550
76359541
TP
11551/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11552 Returns TRUE upon success, FALSE otherwise. */
11553
11554static bfd_boolean
11555elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11556{
11557 bfd_boolean ret = FALSE;
11558 bfd *implib_bfd;
11559 const struct elf_backend_data *bed;
11560 flagword flags;
11561 enum bfd_architecture arch;
11562 unsigned int mach;
11563 asymbol **sympp = NULL;
11564 long symsize;
11565 long symcount;
11566 long src_count;
11567 elf_symbol_type *osymbuf;
11568
11569 implib_bfd = info->out_implib_bfd;
11570 bed = get_elf_backend_data (abfd);
11571
11572 if (!bfd_set_format (implib_bfd, bfd_object))
11573 return FALSE;
11574
046734ff 11575 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11576 flags = bfd_get_file_flags (abfd);
11577 flags &= ~HAS_RELOC;
11578 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11579 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11580 return FALSE;
11581
11582 /* Copy architecture of output file to import library file. */
11583 arch = bfd_get_arch (abfd);
11584 mach = bfd_get_mach (abfd);
11585 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11586 && (abfd->target_defaulted
11587 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11588 return FALSE;
11589
11590 /* Get symbol table size. */
11591 symsize = bfd_get_symtab_upper_bound (abfd);
11592 if (symsize < 0)
11593 return FALSE;
11594
11595 /* Read in the symbol table. */
11596 sympp = (asymbol **) xmalloc (symsize);
11597 symcount = bfd_canonicalize_symtab (abfd, sympp);
11598 if (symcount < 0)
11599 goto free_sym_buf;
11600
11601 /* Allow the BFD backend to copy any private header data it
11602 understands from the output BFD to the import library BFD. */
11603 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11604 goto free_sym_buf;
11605
11606 /* Filter symbols to appear in the import library. */
11607 if (bed->elf_backend_filter_implib_symbols)
11608 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11609 symcount);
11610 else
11611 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11612 if (symcount == 0)
11613 {
5df1bc57 11614 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11615 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11616 implib_bfd);
76359541
TP
11617 goto free_sym_buf;
11618 }
11619
11620
11621 /* Make symbols absolute. */
11622 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11623 sizeof (*osymbuf));
11624 for (src_count = 0; src_count < symcount; src_count++)
11625 {
11626 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11627 sizeof (*osymbuf));
11628 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11629 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11630 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11631 osymbuf[src_count].internal_elf_sym.st_value =
11632 osymbuf[src_count].symbol.value;
11633 sympp[src_count] = &osymbuf[src_count].symbol;
11634 }
11635
11636 bfd_set_symtab (implib_bfd, sympp, symcount);
11637
11638 /* Allow the BFD backend to copy any private data it understands
11639 from the output BFD to the import library BFD. This is done last
11640 to permit the routine to look at the filtered symbol table. */
11641 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11642 goto free_sym_buf;
11643
11644 if (!bfd_close (implib_bfd))
11645 goto free_sym_buf;
11646
11647 ret = TRUE;
11648
11649free_sym_buf:
11650 free (sympp);
11651 return ret;
11652}
11653
9f7c3e5e
AM
11654static void
11655elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11656{
11657 asection *o;
11658
11659 if (flinfo->symstrtab != NULL)
ef10c3ac 11660 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11661 if (flinfo->contents != NULL)
11662 free (flinfo->contents);
11663 if (flinfo->external_relocs != NULL)
11664 free (flinfo->external_relocs);
11665 if (flinfo->internal_relocs != NULL)
11666 free (flinfo->internal_relocs);
11667 if (flinfo->external_syms != NULL)
11668 free (flinfo->external_syms);
11669 if (flinfo->locsym_shndx != NULL)
11670 free (flinfo->locsym_shndx);
11671 if (flinfo->internal_syms != NULL)
11672 free (flinfo->internal_syms);
11673 if (flinfo->indices != NULL)
11674 free (flinfo->indices);
11675 if (flinfo->sections != NULL)
11676 free (flinfo->sections);
9f7c3e5e
AM
11677 if (flinfo->symshndxbuf != NULL)
11678 free (flinfo->symshndxbuf);
11679 for (o = obfd->sections; o != NULL; o = o->next)
11680 {
11681 struct bfd_elf_section_data *esdo = elf_section_data (o);
11682 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11683 free (esdo->rel.hashes);
11684 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11685 free (esdo->rela.hashes);
11686 }
11687}
0b52efa6 11688
c152c796
AM
11689/* Do the final step of an ELF link. */
11690
11691bfd_boolean
11692bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11693{
11694 bfd_boolean dynamic;
11695 bfd_boolean emit_relocs;
11696 bfd *dynobj;
8b127cbc 11697 struct elf_final_link_info flinfo;
91d6fa6a
NC
11698 asection *o;
11699 struct bfd_link_order *p;
11700 bfd *sub;
c152c796
AM
11701 bfd_size_type max_contents_size;
11702 bfd_size_type max_external_reloc_size;
11703 bfd_size_type max_internal_reloc_count;
11704 bfd_size_type max_sym_count;
11705 bfd_size_type max_sym_shndx_count;
c152c796
AM
11706 Elf_Internal_Sym elfsym;
11707 unsigned int i;
11708 Elf_Internal_Shdr *symtab_hdr;
11709 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11710 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11711 struct elf_outext_info eoinfo;
11712 bfd_boolean merged;
11713 size_t relativecount = 0;
11714 asection *reldyn = 0;
11715 bfd_size_type amt;
104d59d1
JM
11716 asection *attr_section = NULL;
11717 bfd_vma attr_size = 0;
11718 const char *std_attrs_section;
64f52338 11719 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11720
64f52338 11721 if (!is_elf_hash_table (htab))
c152c796
AM
11722 return FALSE;
11723
0e1862bb 11724 if (bfd_link_pic (info))
c152c796
AM
11725 abfd->flags |= DYNAMIC;
11726
64f52338
AM
11727 dynamic = htab->dynamic_sections_created;
11728 dynobj = htab->dynobj;
c152c796 11729
0e1862bb 11730 emit_relocs = (bfd_link_relocatable (info)
a4676736 11731 || info->emitrelocations);
c152c796 11732
8b127cbc
AM
11733 flinfo.info = info;
11734 flinfo.output_bfd = abfd;
ef10c3ac 11735 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11736 if (flinfo.symstrtab == NULL)
c152c796
AM
11737 return FALSE;
11738
11739 if (! dynamic)
11740 {
8b127cbc
AM
11741 flinfo.hash_sec = NULL;
11742 flinfo.symver_sec = NULL;
c152c796
AM
11743 }
11744 else
11745 {
3d4d4302 11746 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11747 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11748 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11749 /* Note that it is OK if symver_sec is NULL. */
11750 }
11751
8b127cbc
AM
11752 flinfo.contents = NULL;
11753 flinfo.external_relocs = NULL;
11754 flinfo.internal_relocs = NULL;
11755 flinfo.external_syms = NULL;
11756 flinfo.locsym_shndx = NULL;
11757 flinfo.internal_syms = NULL;
11758 flinfo.indices = NULL;
11759 flinfo.sections = NULL;
8b127cbc 11760 flinfo.symshndxbuf = NULL;
ffbc01cc 11761 flinfo.filesym_count = 0;
c152c796 11762
104d59d1
JM
11763 /* The object attributes have been merged. Remove the input
11764 sections from the link, and set the contents of the output
11765 secton. */
11766 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11767 for (o = abfd->sections; o != NULL; o = o->next)
11768 {
5270eddc 11769 bfd_boolean remove_section = FALSE;
b8a6ced7 11770
104d59d1
JM
11771 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11772 || strcmp (o->name, ".gnu.attributes") == 0)
11773 {
11774 for (p = o->map_head.link_order; p != NULL; p = p->next)
11775 {
11776 asection *input_section;
11777
11778 if (p->type != bfd_indirect_link_order)
11779 continue;
11780 input_section = p->u.indirect.section;
11781 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11782 elf_link_input_bfd ignores this section. */
11783 input_section->flags &= ~SEC_HAS_CONTENTS;
11784 }
a0c8462f 11785
104d59d1 11786 attr_size = bfd_elf_obj_attr_size (abfd);
b8a6ced7
AM
11787 bfd_set_section_size (abfd, o, attr_size);
11788 /* Skip this section later on. */
11789 o->map_head.link_order = NULL;
104d59d1 11790 if (attr_size)
b8a6ced7 11791 attr_section = o;
104d59d1 11792 else
5270eddc 11793 remove_section = TRUE;
104d59d1 11794 }
6e5e9d58
AM
11795 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11796 {
11797 /* Remove empty group section from linker output. */
5270eddc 11798 remove_section = TRUE;
b8a6ced7 11799 }
5270eddc 11800 if (remove_section)
b8a6ced7 11801 {
6e5e9d58
AM
11802 o->flags |= SEC_EXCLUDE;
11803 bfd_section_list_remove (abfd, o);
11804 abfd->section_count--;
11805 }
104d59d1
JM
11806 }
11807
c152c796
AM
11808 /* Count up the number of relocations we will output for each output
11809 section, so that we know the sizes of the reloc sections. We
11810 also figure out some maximum sizes. */
11811 max_contents_size = 0;
11812 max_external_reloc_size = 0;
11813 max_internal_reloc_count = 0;
11814 max_sym_count = 0;
11815 max_sym_shndx_count = 0;
11816 merged = FALSE;
11817 for (o = abfd->sections; o != NULL; o = o->next)
11818 {
11819 struct bfd_elf_section_data *esdo = elf_section_data (o);
11820 o->reloc_count = 0;
11821
8423293d 11822 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11823 {
11824 unsigned int reloc_count = 0;
9eaff861 11825 unsigned int additional_reloc_count = 0;
c152c796 11826 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11827
11828 if (p->type == bfd_section_reloc_link_order
11829 || p->type == bfd_symbol_reloc_link_order)
11830 reloc_count = 1;
11831 else if (p->type == bfd_indirect_link_order)
11832 {
11833 asection *sec;
11834
11835 sec = p->u.indirect.section;
c152c796
AM
11836
11837 /* Mark all sections which are to be included in the
11838 link. This will normally be every section. We need
11839 to do this so that we can identify any sections which
11840 the linker has decided to not include. */
11841 sec->linker_mark = TRUE;
11842
11843 if (sec->flags & SEC_MERGE)
11844 merged = TRUE;
11845
eea6121a
AM
11846 if (sec->rawsize > max_contents_size)
11847 max_contents_size = sec->rawsize;
11848 if (sec->size > max_contents_size)
11849 max_contents_size = sec->size;
c152c796 11850
c152c796
AM
11851 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11852 && (sec->owner->flags & DYNAMIC) == 0)
11853 {
11854 size_t sym_count;
11855
a961cdd5
AM
11856 /* We are interested in just local symbols, not all
11857 symbols. */
c152c796
AM
11858 if (elf_bad_symtab (sec->owner))
11859 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11860 / bed->s->sizeof_sym);
11861 else
11862 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11863
11864 if (sym_count > max_sym_count)
11865 max_sym_count = sym_count;
11866
11867 if (sym_count > max_sym_shndx_count
6a40cf0c 11868 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11869 max_sym_shndx_count = sym_count;
11870
a961cdd5
AM
11871 if (esdo->this_hdr.sh_type == SHT_REL
11872 || esdo->this_hdr.sh_type == SHT_RELA)
11873 /* Some backends use reloc_count in relocation sections
11874 to count particular types of relocs. Of course,
11875 reloc sections themselves can't have relocations. */
11876 ;
11877 else if (emit_relocs)
11878 {
11879 reloc_count = sec->reloc_count;
11880 if (bed->elf_backend_count_additional_relocs)
11881 {
11882 int c;
11883 c = (*bed->elf_backend_count_additional_relocs) (sec);
11884 additional_reloc_count += c;
11885 }
11886 }
11887 else if (bed->elf_backend_count_relocs)
11888 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11889
11890 esdi = elf_section_data (sec);
11891
c152c796
AM
11892 if ((sec->flags & SEC_RELOC) != 0)
11893 {
d4730f92 11894 size_t ext_size = 0;
c152c796 11895
d4730f92
BS
11896 if (esdi->rel.hdr != NULL)
11897 ext_size = esdi->rel.hdr->sh_size;
11898 if (esdi->rela.hdr != NULL)
11899 ext_size += esdi->rela.hdr->sh_size;
7326c758 11900
c152c796
AM
11901 if (ext_size > max_external_reloc_size)
11902 max_external_reloc_size = ext_size;
11903 if (sec->reloc_count > max_internal_reloc_count)
11904 max_internal_reloc_count = sec->reloc_count;
11905 }
11906 }
11907 }
11908
11909 if (reloc_count == 0)
11910 continue;
11911
9eaff861 11912 reloc_count += additional_reloc_count;
c152c796
AM
11913 o->reloc_count += reloc_count;
11914
0e1862bb 11915 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11916 {
d4730f92 11917 if (esdi->rel.hdr)
9eaff861 11918 {
491d01d3 11919 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11920 esdo->rel.count += additional_reloc_count;
11921 }
d4730f92 11922 if (esdi->rela.hdr)
9eaff861 11923 {
491d01d3 11924 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11925 esdo->rela.count += additional_reloc_count;
11926 }
d4730f92
BS
11927 }
11928 else
11929 {
11930 if (o->use_rela_p)
11931 esdo->rela.count += reloc_count;
2c2b4ed4 11932 else
d4730f92 11933 esdo->rel.count += reloc_count;
c152c796 11934 }
c152c796
AM
11935 }
11936
9eaff861 11937 if (o->reloc_count > 0)
c152c796
AM
11938 o->flags |= SEC_RELOC;
11939 else
11940 {
11941 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11942 set it (this is probably a bug) and if it is set
11943 assign_section_numbers will create a reloc section. */
11944 o->flags &=~ SEC_RELOC;
11945 }
11946
11947 /* If the SEC_ALLOC flag is not set, force the section VMA to
11948 zero. This is done in elf_fake_sections as well, but forcing
11949 the VMA to 0 here will ensure that relocs against these
11950 sections are handled correctly. */
11951 if ((o->flags & SEC_ALLOC) == 0
11952 && ! o->user_set_vma)
11953 o->vma = 0;
11954 }
11955
0e1862bb 11956 if (! bfd_link_relocatable (info) && merged)
64f52338 11957 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11958
11959 /* Figure out the file positions for everything but the symbol table
11960 and the relocs. We set symcount to force assign_section_numbers
11961 to create a symbol table. */
8539e4e8 11962 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11963 BFD_ASSERT (! abfd->output_has_begun);
11964 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11965 goto error_return;
11966
ee75fd95 11967 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11968 for (o = abfd->sections; o != NULL; o = o->next)
11969 {
d4730f92 11970 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11971 if ((o->flags & SEC_RELOC) != 0)
11972 {
d4730f92 11973 if (esdo->rel.hdr
9eaff861 11974 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11975 goto error_return;
11976
d4730f92 11977 if (esdo->rela.hdr
9eaff861 11978 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11979 goto error_return;
11980 }
11981
11982 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11983 to count upwards while actually outputting the relocations. */
d4730f92
BS
11984 esdo->rel.count = 0;
11985 esdo->rela.count = 0;
0ce398f1
L
11986
11987 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11988 {
11989 /* Cache the section contents so that they can be compressed
11990 later. Use bfd_malloc since it will be freed by
11991 bfd_compress_section_contents. */
11992 unsigned char *contents = esdo->this_hdr.contents;
11993 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11994 abort ();
11995 contents
11996 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11997 if (contents == NULL)
11998 goto error_return;
11999 esdo->this_hdr.contents = contents;
12000 }
c152c796
AM
12001 }
12002
c152c796 12003 /* We have now assigned file positions for all the sections except
a485e98e
AM
12004 .symtab, .strtab, and non-loaded reloc sections. We start the
12005 .symtab section at the current file position, and write directly
12006 to it. We build the .strtab section in memory. */
c152c796
AM
12007 bfd_get_symcount (abfd) = 0;
12008 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12009 /* sh_name is set in prep_headers. */
12010 symtab_hdr->sh_type = SHT_SYMTAB;
12011 /* sh_flags, sh_addr and sh_size all start off zero. */
12012 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12013 /* sh_link is set in assign_section_numbers. */
12014 /* sh_info is set below. */
12015 /* sh_offset is set just below. */
72de5009 12016 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12017
ef10c3ac
L
12018 if (max_sym_count < 20)
12019 max_sym_count = 20;
64f52338 12020 htab->strtabsize = max_sym_count;
ef10c3ac 12021 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12022 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12023 if (htab->strtab == NULL)
c152c796 12024 goto error_return;
ef10c3ac
L
12025 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12026 flinfo.symshndxbuf
12027 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12028 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12029
8539e4e8 12030 if (info->strip != strip_all || emit_relocs)
c152c796 12031 {
8539e4e8
AM
12032 file_ptr off = elf_next_file_pos (abfd);
12033
12034 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12035
12036 /* Note that at this point elf_next_file_pos (abfd) is
12037 incorrect. We do not yet know the size of the .symtab section.
12038 We correct next_file_pos below, after we do know the size. */
12039
12040 /* Start writing out the symbol table. The first symbol is always a
12041 dummy symbol. */
c152c796
AM
12042 elfsym.st_value = 0;
12043 elfsym.st_size = 0;
12044 elfsym.st_info = 0;
12045 elfsym.st_other = 0;
12046 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12047 elfsym.st_target_internal = 0;
ef10c3ac
L
12048 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12049 bfd_und_section_ptr, NULL) != 1)
c152c796 12050 goto error_return;
c152c796 12051
8539e4e8
AM
12052 /* Output a symbol for each section. We output these even if we are
12053 discarding local symbols, since they are used for relocs. These
12054 symbols have no names. We store the index of each one in the
12055 index field of the section, so that we can find it again when
12056 outputting relocs. */
12057
c152c796
AM
12058 elfsym.st_size = 0;
12059 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12060 elfsym.st_other = 0;
f0b5bb34 12061 elfsym.st_value = 0;
35fc36a8 12062 elfsym.st_target_internal = 0;
c152c796
AM
12063 for (i = 1; i < elf_numsections (abfd); i++)
12064 {
12065 o = bfd_section_from_elf_index (abfd, i);
12066 if (o != NULL)
f0b5bb34
AM
12067 {
12068 o->target_index = bfd_get_symcount (abfd);
12069 elfsym.st_shndx = i;
0e1862bb 12070 if (!bfd_link_relocatable (info))
f0b5bb34 12071 elfsym.st_value = o->vma;
ef10c3ac
L
12072 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12073 NULL) != 1)
f0b5bb34
AM
12074 goto error_return;
12075 }
c152c796
AM
12076 }
12077 }
12078
12079 /* Allocate some memory to hold information read in from the input
12080 files. */
12081 if (max_contents_size != 0)
12082 {
8b127cbc
AM
12083 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12084 if (flinfo.contents == NULL)
c152c796
AM
12085 goto error_return;
12086 }
12087
12088 if (max_external_reloc_size != 0)
12089 {
8b127cbc
AM
12090 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12091 if (flinfo.external_relocs == NULL)
c152c796
AM
12092 goto error_return;
12093 }
12094
12095 if (max_internal_reloc_count != 0)
12096 {
056bafd4 12097 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12098 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12099 if (flinfo.internal_relocs == NULL)
c152c796
AM
12100 goto error_return;
12101 }
12102
12103 if (max_sym_count != 0)
12104 {
12105 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12106 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12107 if (flinfo.external_syms == NULL)
c152c796
AM
12108 goto error_return;
12109
12110 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12111 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12112 if (flinfo.internal_syms == NULL)
c152c796
AM
12113 goto error_return;
12114
12115 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12116 flinfo.indices = (long int *) bfd_malloc (amt);
12117 if (flinfo.indices == NULL)
c152c796
AM
12118 goto error_return;
12119
12120 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12121 flinfo.sections = (asection **) bfd_malloc (amt);
12122 if (flinfo.sections == NULL)
c152c796
AM
12123 goto error_return;
12124 }
12125
12126 if (max_sym_shndx_count != 0)
12127 {
12128 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12129 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12130 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12131 goto error_return;
12132 }
12133
64f52338 12134 if (htab->tls_sec)
c152c796
AM
12135 {
12136 bfd_vma base, end = 0;
12137 asection *sec;
12138
64f52338 12139 for (sec = htab->tls_sec;
c152c796
AM
12140 sec && (sec->flags & SEC_THREAD_LOCAL);
12141 sec = sec->next)
12142 {
3a800eb9 12143 bfd_size_type size = sec->size;
c152c796 12144
3a800eb9
AM
12145 if (size == 0
12146 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12147 {
91d6fa6a
NC
12148 struct bfd_link_order *ord = sec->map_tail.link_order;
12149
12150 if (ord != NULL)
12151 size = ord->offset + ord->size;
c152c796
AM
12152 }
12153 end = sec->vma + size;
12154 }
64f52338 12155 base = htab->tls_sec->vma;
7dc98aea
RO
12156 /* Only align end of TLS section if static TLS doesn't have special
12157 alignment requirements. */
12158 if (bed->static_tls_alignment == 1)
64f52338
AM
12159 end = align_power (end, htab->tls_sec->alignment_power);
12160 htab->tls_size = end - base;
c152c796
AM
12161 }
12162
0b52efa6
PB
12163 /* Reorder SHF_LINK_ORDER sections. */
12164 for (o = abfd->sections; o != NULL; o = o->next)
12165 {
12166 if (!elf_fixup_link_order (abfd, o))
12167 return FALSE;
12168 }
12169
2f0c68f2
CM
12170 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12171 return FALSE;
12172
c152c796
AM
12173 /* Since ELF permits relocations to be against local symbols, we
12174 must have the local symbols available when we do the relocations.
12175 Since we would rather only read the local symbols once, and we
12176 would rather not keep them in memory, we handle all the
12177 relocations for a single input file at the same time.
12178
12179 Unfortunately, there is no way to know the total number of local
12180 symbols until we have seen all of them, and the local symbol
12181 indices precede the global symbol indices. This means that when
12182 we are generating relocatable output, and we see a reloc against
12183 a global symbol, we can not know the symbol index until we have
12184 finished examining all the local symbols to see which ones we are
12185 going to output. To deal with this, we keep the relocations in
12186 memory, and don't output them until the end of the link. This is
12187 an unfortunate waste of memory, but I don't see a good way around
12188 it. Fortunately, it only happens when performing a relocatable
12189 link, which is not the common case. FIXME: If keep_memory is set
12190 we could write the relocs out and then read them again; I don't
12191 know how bad the memory loss will be. */
12192
c72f2fb2 12193 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12194 sub->output_has_begun = FALSE;
12195 for (o = abfd->sections; o != NULL; o = o->next)
12196 {
8423293d 12197 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12198 {
12199 if (p->type == bfd_indirect_link_order
12200 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12201 == bfd_target_elf_flavour)
12202 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12203 {
12204 if (! sub->output_has_begun)
12205 {
8b127cbc 12206 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12207 goto error_return;
12208 sub->output_has_begun = TRUE;
12209 }
12210 }
12211 else if (p->type == bfd_section_reloc_link_order
12212 || p->type == bfd_symbol_reloc_link_order)
12213 {
12214 if (! elf_reloc_link_order (abfd, info, o, p))
12215 goto error_return;
12216 }
12217 else
12218 {
12219 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12220 {
12221 if (p->type == bfd_indirect_link_order
12222 && (bfd_get_flavour (sub)
12223 == bfd_target_elf_flavour)
12224 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12225 != bed->s->elfclass))
12226 {
12227 const char *iclass, *oclass;
12228
aebf9be7 12229 switch (bed->s->elfclass)
351f65ca 12230 {
aebf9be7
NC
12231 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12232 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12233 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12234 default: abort ();
351f65ca 12235 }
aebf9be7
NC
12236
12237 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12238 {
aebf9be7
NC
12239 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12240 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12241 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12242 default: abort ();
351f65ca
L
12243 }
12244
12245 bfd_set_error (bfd_error_wrong_format);
4eca0228 12246 _bfd_error_handler
695344c0 12247 /* xgettext:c-format */
871b3ab2 12248 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12249 sub, iclass, oclass);
12250 }
12251
12252 goto error_return;
12253 }
c152c796
AM
12254 }
12255 }
12256 }
12257
c0f00686
L
12258 /* Free symbol buffer if needed. */
12259 if (!info->reduce_memory_overheads)
12260 {
c72f2fb2 12261 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
12262 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12263 && elf_tdata (sub)->symbuf)
c0f00686
L
12264 {
12265 free (elf_tdata (sub)->symbuf);
12266 elf_tdata (sub)->symbuf = NULL;
12267 }
12268 }
12269
c152c796
AM
12270 /* Output any global symbols that got converted to local in a
12271 version script or due to symbol visibility. We do this in a
12272 separate step since ELF requires all local symbols to appear
12273 prior to any global symbols. FIXME: We should only do this if
12274 some global symbols were, in fact, converted to become local.
12275 FIXME: Will this work correctly with the Irix 5 linker? */
12276 eoinfo.failed = FALSE;
8b127cbc 12277 eoinfo.flinfo = &flinfo;
c152c796 12278 eoinfo.localsyms = TRUE;
34a79995 12279 eoinfo.file_sym_done = FALSE;
7686d77d 12280 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12281 if (eoinfo.failed)
12282 return FALSE;
12283
4e617b1e
PB
12284 /* If backend needs to output some local symbols not present in the hash
12285 table, do it now. */
8539e4e8
AM
12286 if (bed->elf_backend_output_arch_local_syms
12287 && (info->strip != strip_all || emit_relocs))
4e617b1e 12288 {
6e0b88f1 12289 typedef int (*out_sym_func)
4e617b1e
PB
12290 (void *, const char *, Elf_Internal_Sym *, asection *,
12291 struct elf_link_hash_entry *);
12292
12293 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12294 (abfd, info, &flinfo,
12295 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
12296 return FALSE;
12297 }
12298
c152c796
AM
12299 /* That wrote out all the local symbols. Finish up the symbol table
12300 with the global symbols. Even if we want to strip everything we
12301 can, we still need to deal with those global symbols that got
12302 converted to local in a version script. */
12303
12304 /* The sh_info field records the index of the first non local symbol. */
12305 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12306
12307 if (dynamic
64f52338
AM
12308 && htab->dynsym != NULL
12309 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12310 {
12311 Elf_Internal_Sym sym;
64f52338 12312 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12313
64f52338
AM
12314 o = htab->dynsym->output_section;
12315 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12316
12317 /* Write out the section symbols for the output sections. */
0e1862bb 12318 if (bfd_link_pic (info)
64f52338 12319 || htab->is_relocatable_executable)
c152c796
AM
12320 {
12321 asection *s;
12322
12323 sym.st_size = 0;
12324 sym.st_name = 0;
12325 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12326 sym.st_other = 0;
35fc36a8 12327 sym.st_target_internal = 0;
c152c796
AM
12328
12329 for (s = abfd->sections; s != NULL; s = s->next)
12330 {
12331 int indx;
12332 bfd_byte *dest;
12333 long dynindx;
12334
c152c796 12335 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12336 if (dynindx <= 0)
12337 continue;
12338 indx = elf_section_data (s)->this_idx;
c152c796
AM
12339 BFD_ASSERT (indx > 0);
12340 sym.st_shndx = indx;
c0d5a53d
L
12341 if (! check_dynsym (abfd, &sym))
12342 return FALSE;
c152c796
AM
12343 sym.st_value = s->vma;
12344 dest = dynsym + dynindx * bed->s->sizeof_sym;
12345 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12346 }
c152c796
AM
12347 }
12348
12349 /* Write out the local dynsyms. */
64f52338 12350 if (htab->dynlocal)
c152c796
AM
12351 {
12352 struct elf_link_local_dynamic_entry *e;
64f52338 12353 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12354 {
12355 asection *s;
12356 bfd_byte *dest;
12357
935bd1e0 12358 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12359 Note that we saved a word of storage and overwrote
12360 the original st_name with the dynstr_index. */
12361 sym = e->isym;
935bd1e0 12362 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 12363
cb33740c
AM
12364 s = bfd_section_from_elf_index (e->input_bfd,
12365 e->isym.st_shndx);
12366 if (s != NULL)
c152c796 12367 {
c152c796
AM
12368 sym.st_shndx =
12369 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
12370 if (! check_dynsym (abfd, &sym))
12371 return FALSE;
c152c796
AM
12372 sym.st_value = (s->output_section->vma
12373 + s->output_offset
12374 + e->isym.st_value);
12375 }
12376
c152c796
AM
12377 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12378 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12379 }
12380 }
c152c796
AM
12381 }
12382
12383 /* We get the global symbols from the hash table. */
12384 eoinfo.failed = FALSE;
12385 eoinfo.localsyms = FALSE;
8b127cbc 12386 eoinfo.flinfo = &flinfo;
7686d77d 12387 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
12388 if (eoinfo.failed)
12389 return FALSE;
12390
12391 /* If backend needs to output some symbols not present in the hash
12392 table, do it now. */
8539e4e8
AM
12393 if (bed->elf_backend_output_arch_syms
12394 && (info->strip != strip_all || emit_relocs))
c152c796 12395 {
6e0b88f1 12396 typedef int (*out_sym_func)
c152c796
AM
12397 (void *, const char *, Elf_Internal_Sym *, asection *,
12398 struct elf_link_hash_entry *);
12399
12400 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12401 (abfd, info, &flinfo,
12402 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12403 return FALSE;
12404 }
12405
ef10c3ac
L
12406 /* Finalize the .strtab section. */
12407 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12408
12409 /* Swap out the .strtab section. */
12410 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12411 return FALSE;
12412
12413 /* Now we know the size of the symtab section. */
c152c796
AM
12414 if (bfd_get_symcount (abfd) > 0)
12415 {
ee3b52e9
L
12416 /* Finish up and write out the symbol string table (.strtab)
12417 section. */
ad32986f 12418 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12419 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12420
ad32986f 12421 if (elf_symtab_shndx_list (abfd))
8539e4e8 12422 {
ad32986f 12423 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12424
ad32986f
NC
12425 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12426 {
12427 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12428 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12429 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12430 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12431 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12432
ad32986f
NC
12433 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12434 off, TRUE);
12435
12436 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12437 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12438 return FALSE;
12439 }
8539e4e8 12440 }
ee3b52e9
L
12441
12442 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12443 /* sh_name was set in prep_headers. */
12444 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12445 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12446 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12447 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12448 symstrtab_hdr->sh_entsize = 0;
12449 symstrtab_hdr->sh_link = 0;
12450 symstrtab_hdr->sh_info = 0;
12451 /* sh_offset is set just below. */
12452 symstrtab_hdr->sh_addralign = 1;
12453
12454 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12455 off, TRUE);
12456 elf_next_file_pos (abfd) = off;
12457
c152c796 12458 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12459 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12460 return FALSE;
12461 }
12462
76359541
TP
12463 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12464 {
871b3ab2 12465 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12466 info->out_implib_bfd);
76359541
TP
12467 return FALSE;
12468 }
12469
c152c796
AM
12470 /* Adjust the relocs to have the correct symbol indices. */
12471 for (o = abfd->sections; o != NULL; o = o->next)
12472 {
d4730f92 12473 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12474 bfd_boolean sort;
10bbbc1d 12475
c152c796
AM
12476 if ((o->flags & SEC_RELOC) == 0)
12477 continue;
12478
28dbcedc 12479 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12480 if (esdo->rel.hdr != NULL
10bbbc1d 12481 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
bca6d0e3
AM
12482 return FALSE;
12483 if (esdo->rela.hdr != NULL
10bbbc1d 12484 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
bca6d0e3 12485 return FALSE;
c152c796
AM
12486
12487 /* Set the reloc_count field to 0 to prevent write_relocs from
12488 trying to swap the relocs out itself. */
12489 o->reloc_count = 0;
12490 }
12491
12492 if (dynamic && info->combreloc && dynobj != NULL)
12493 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12494
12495 /* If we are linking against a dynamic object, or generating a
12496 shared library, finish up the dynamic linking information. */
12497 if (dynamic)
12498 {
12499 bfd_byte *dyncon, *dynconend;
12500
12501 /* Fix up .dynamic entries. */
3d4d4302 12502 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12503 BFD_ASSERT (o != NULL);
12504
12505 dyncon = o->contents;
eea6121a 12506 dynconend = o->contents + o->size;
c152c796
AM
12507 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12508 {
12509 Elf_Internal_Dyn dyn;
12510 const char *name;
12511 unsigned int type;
64487780
AM
12512 bfd_size_type sh_size;
12513 bfd_vma sh_addr;
c152c796
AM
12514
12515 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12516
12517 switch (dyn.d_tag)
12518 {
12519 default:
12520 continue;
12521 case DT_NULL:
12522 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12523 {
12524 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12525 {
12526 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12527 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12528 default: continue;
12529 }
12530 dyn.d_un.d_val = relativecount;
12531 relativecount = 0;
12532 break;
12533 }
12534 continue;
12535
12536 case DT_INIT:
12537 name = info->init_function;
12538 goto get_sym;
12539 case DT_FINI:
12540 name = info->fini_function;
12541 get_sym:
12542 {
12543 struct elf_link_hash_entry *h;
12544
64f52338 12545 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12546 if (h != NULL
12547 && (h->root.type == bfd_link_hash_defined
12548 || h->root.type == bfd_link_hash_defweak))
12549 {
bef26483 12550 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12551 o = h->root.u.def.section;
12552 if (o->output_section != NULL)
bef26483 12553 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12554 + o->output_offset);
12555 else
12556 {
12557 /* The symbol is imported from another shared
12558 library and does not apply to this one. */
bef26483 12559 dyn.d_un.d_ptr = 0;
c152c796
AM
12560 }
12561 break;
12562 }
12563 }
12564 continue;
12565
12566 case DT_PREINIT_ARRAYSZ:
12567 name = ".preinit_array";
4ade44b7 12568 goto get_out_size;
c152c796
AM
12569 case DT_INIT_ARRAYSZ:
12570 name = ".init_array";
4ade44b7 12571 goto get_out_size;
c152c796
AM
12572 case DT_FINI_ARRAYSZ:
12573 name = ".fini_array";
4ade44b7 12574 get_out_size:
c152c796
AM
12575 o = bfd_get_section_by_name (abfd, name);
12576 if (o == NULL)
12577 {
4eca0228 12578 _bfd_error_handler
4ade44b7 12579 (_("could not find section %s"), name);
c152c796
AM
12580 goto error_return;
12581 }
eea6121a 12582 if (o->size == 0)
4eca0228 12583 _bfd_error_handler
c152c796 12584 (_("warning: %s section has zero size"), name);
eea6121a 12585 dyn.d_un.d_val = o->size;
c152c796
AM
12586 break;
12587
12588 case DT_PREINIT_ARRAY:
12589 name = ".preinit_array";
4ade44b7 12590 goto get_out_vma;
c152c796
AM
12591 case DT_INIT_ARRAY:
12592 name = ".init_array";
4ade44b7 12593 goto get_out_vma;
c152c796
AM
12594 case DT_FINI_ARRAY:
12595 name = ".fini_array";
4ade44b7
AM
12596 get_out_vma:
12597 o = bfd_get_section_by_name (abfd, name);
12598 goto do_vma;
c152c796
AM
12599
12600 case DT_HASH:
12601 name = ".hash";
12602 goto get_vma;
fdc90cb4
JJ
12603 case DT_GNU_HASH:
12604 name = ".gnu.hash";
12605 goto get_vma;
c152c796
AM
12606 case DT_STRTAB:
12607 name = ".dynstr";
12608 goto get_vma;
12609 case DT_SYMTAB:
12610 name = ".dynsym";
12611 goto get_vma;
12612 case DT_VERDEF:
12613 name = ".gnu.version_d";
12614 goto get_vma;
12615 case DT_VERNEED:
12616 name = ".gnu.version_r";
12617 goto get_vma;
12618 case DT_VERSYM:
12619 name = ".gnu.version";
12620 get_vma:
4ade44b7
AM
12621 o = bfd_get_linker_section (dynobj, name);
12622 do_vma:
b3293efa 12623 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 12624 {
4eca0228 12625 _bfd_error_handler
4ade44b7 12626 (_("could not find section %s"), name);
c152c796
AM
12627 goto error_return;
12628 }
894891db
NC
12629 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12630 {
4eca0228 12631 _bfd_error_handler
894891db
NC
12632 (_("warning: section '%s' is being made into a note"), name);
12633 bfd_set_error (bfd_error_nonrepresentable_section);
12634 goto error_return;
12635 }
4ade44b7 12636 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12637 break;
12638
12639 case DT_REL:
12640 case DT_RELA:
12641 case DT_RELSZ:
12642 case DT_RELASZ:
12643 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12644 type = SHT_REL;
12645 else
12646 type = SHT_RELA;
64487780
AM
12647 sh_size = 0;
12648 sh_addr = 0;
c152c796
AM
12649 for (i = 1; i < elf_numsections (abfd); i++)
12650 {
12651 Elf_Internal_Shdr *hdr;
12652
12653 hdr = elf_elfsections (abfd)[i];
12654 if (hdr->sh_type == type
12655 && (hdr->sh_flags & SHF_ALLOC) != 0)
12656 {
64487780
AM
12657 sh_size += hdr->sh_size;
12658 if (sh_addr == 0
12659 || sh_addr > hdr->sh_addr)
12660 sh_addr = hdr->sh_addr;
c152c796
AM
12661 }
12662 }
64487780 12663
64f52338
AM
12664 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12665 {
12666 /* Don't count procedure linkage table relocs in the
12667 overall reloc count. */
64487780
AM
12668 sh_size -= htab->srelplt->size;
12669 if (sh_size == 0)
12670 /* If the size is zero, make the address zero too.
12671 This is to avoid a glibc bug. If the backend
12672 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12673 zero, then we'll put DT_RELA at the end of
12674 DT_JMPREL. glibc will interpret the end of
12675 DT_RELA matching the end of DT_JMPREL as the
12676 case where DT_RELA includes DT_JMPREL, and for
12677 LD_BIND_NOW will decide that processing DT_RELA
12678 will process the PLT relocs too. Net result:
12679 No PLT relocs applied. */
12680 sh_addr = 0;
12681
64f52338
AM
12682 /* If .rela.plt is the first .rela section, exclude
12683 it from DT_RELA. */
64487780
AM
12684 else if (sh_addr == (htab->srelplt->output_section->vma
12685 + htab->srelplt->output_offset))
12686 sh_addr += htab->srelplt->size;
64f52338 12687 }
64487780
AM
12688
12689 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12690 dyn.d_un.d_val = sh_size;
12691 else
12692 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
12693 break;
12694 }
12695 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12696 }
12697 }
12698
12699 /* If we have created any dynamic sections, then output them. */
12700 if (dynobj != NULL)
12701 {
12702 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12703 goto error_return;
12704
943284cc 12705 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12706 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12707 || info->error_textrel)
3d4d4302 12708 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12709 {
12710 bfd_byte *dyncon, *dynconend;
12711
943284cc
DJ
12712 dyncon = o->contents;
12713 dynconend = o->contents + o->size;
12714 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12715 {
12716 Elf_Internal_Dyn dyn;
12717
12718 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12719
12720 if (dyn.d_tag == DT_TEXTREL)
12721 {
c192a133
AM
12722 if (info->error_textrel)
12723 info->callbacks->einfo
9793eb77 12724 (_("%P%X: read-only segment has dynamic relocations\n"));
c192a133
AM
12725 else
12726 info->callbacks->einfo
9793eb77 12727 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
943284cc
DJ
12728 break;
12729 }
12730 }
12731 }
12732
c152c796
AM
12733 for (o = dynobj->sections; o != NULL; o = o->next)
12734 {
12735 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12736 || o->size == 0
c152c796
AM
12737 || o->output_section == bfd_abs_section_ptr)
12738 continue;
12739 if ((o->flags & SEC_LINKER_CREATED) == 0)
12740 {
12741 /* At this point, we are only interested in sections
12742 created by _bfd_elf_link_create_dynamic_sections. */
12743 continue;
12744 }
64f52338 12745 if (htab->stab_info.stabstr == o)
3722b82f 12746 continue;
64f52338 12747 if (htab->eh_info.hdr_sec == o)
eea6121a 12748 continue;
3d4d4302 12749 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12750 {
12751 if (! bfd_set_section_contents (abfd, o->output_section,
12752 o->contents,
37b01f6a
DG
12753 (file_ptr) o->output_offset
12754 * bfd_octets_per_byte (abfd),
eea6121a 12755 o->size))
c152c796
AM
12756 goto error_return;
12757 }
12758 else
12759 {
12760 /* The contents of the .dynstr section are actually in a
12761 stringtab. */
8539e4e8
AM
12762 file_ptr off;
12763
c152c796
AM
12764 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12765 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12766 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12767 goto error_return;
12768 }
12769 }
12770 }
12771
7bdf4127 12772 if (!info->resolve_section_groups)
c152c796
AM
12773 {
12774 bfd_boolean failed = FALSE;
12775
7bdf4127 12776 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
12777 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12778 if (failed)
12779 goto error_return;
12780 }
12781
12782 /* If we have optimized stabs strings, output them. */
64f52338 12783 if (htab->stab_info.stabstr != NULL)
c152c796 12784 {
64f52338 12785 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12786 goto error_return;
12787 }
12788
9f7c3e5e
AM
12789 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12790 goto error_return;
c152c796 12791
9f7c3e5e 12792 elf_final_link_free (abfd, &flinfo);
c152c796 12793
12bd6957 12794 elf_linker (abfd) = TRUE;
c152c796 12795
104d59d1
JM
12796 if (attr_section)
12797 {
a50b1753 12798 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12799 if (contents == NULL)
d0f16d5e 12800 return FALSE; /* Bail out and fail. */
104d59d1
JM
12801 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12802 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12803 free (contents);
12804 }
12805
c152c796
AM
12806 return TRUE;
12807
12808 error_return:
9f7c3e5e 12809 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12810 return FALSE;
12811}
12812\f
5241d853
RS
12813/* Initialize COOKIE for input bfd ABFD. */
12814
12815static bfd_boolean
12816init_reloc_cookie (struct elf_reloc_cookie *cookie,
12817 struct bfd_link_info *info, bfd *abfd)
12818{
12819 Elf_Internal_Shdr *symtab_hdr;
12820 const struct elf_backend_data *bed;
12821
12822 bed = get_elf_backend_data (abfd);
12823 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12824
12825 cookie->abfd = abfd;
12826 cookie->sym_hashes = elf_sym_hashes (abfd);
12827 cookie->bad_symtab = elf_bad_symtab (abfd);
12828 if (cookie->bad_symtab)
12829 {
12830 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12831 cookie->extsymoff = 0;
12832 }
12833 else
12834 {
12835 cookie->locsymcount = symtab_hdr->sh_info;
12836 cookie->extsymoff = symtab_hdr->sh_info;
12837 }
12838
12839 if (bed->s->arch_size == 32)
12840 cookie->r_sym_shift = 8;
12841 else
12842 cookie->r_sym_shift = 32;
12843
12844 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12845 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12846 {
12847 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12848 cookie->locsymcount, 0,
12849 NULL, NULL, NULL);
12850 if (cookie->locsyms == NULL)
12851 {
12852 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12853 return FALSE;
12854 }
12855 if (info->keep_memory)
12856 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12857 }
12858 return TRUE;
12859}
12860
12861/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12862
12863static void
12864fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12865{
12866 Elf_Internal_Shdr *symtab_hdr;
12867
12868 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12869 if (cookie->locsyms != NULL
12870 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12871 free (cookie->locsyms);
12872}
12873
12874/* Initialize the relocation information in COOKIE for input section SEC
12875 of input bfd ABFD. */
12876
12877static bfd_boolean
12878init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12879 struct bfd_link_info *info, bfd *abfd,
12880 asection *sec)
12881{
5241d853
RS
12882 if (sec->reloc_count == 0)
12883 {
12884 cookie->rels = NULL;
12885 cookie->relend = NULL;
12886 }
12887 else
12888 {
5241d853
RS
12889 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12890 info->keep_memory);
12891 if (cookie->rels == NULL)
12892 return FALSE;
12893 cookie->rel = cookie->rels;
056bafd4 12894 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
12895 }
12896 cookie->rel = cookie->rels;
12897 return TRUE;
12898}
12899
12900/* Free the memory allocated by init_reloc_cookie_rels,
12901 if appropriate. */
12902
12903static void
12904fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12905 asection *sec)
12906{
12907 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12908 free (cookie->rels);
12909}
12910
12911/* Initialize the whole of COOKIE for input section SEC. */
12912
12913static bfd_boolean
12914init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12915 struct bfd_link_info *info,
12916 asection *sec)
12917{
12918 if (!init_reloc_cookie (cookie, info, sec->owner))
12919 goto error1;
12920 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12921 goto error2;
12922 return TRUE;
12923
12924 error2:
12925 fini_reloc_cookie (cookie, sec->owner);
12926 error1:
12927 return FALSE;
12928}
12929
12930/* Free the memory allocated by init_reloc_cookie_for_section,
12931 if appropriate. */
12932
12933static void
12934fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12935 asection *sec)
12936{
12937 fini_reloc_cookie_rels (cookie, sec);
12938 fini_reloc_cookie (cookie, sec->owner);
12939}
12940\f
c152c796
AM
12941/* Garbage collect unused sections. */
12942
07adf181
AM
12943/* Default gc_mark_hook. */
12944
12945asection *
12946_bfd_elf_gc_mark_hook (asection *sec,
12947 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12948 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12949 struct elf_link_hash_entry *h,
12950 Elf_Internal_Sym *sym)
12951{
12952 if (h != NULL)
12953 {
12954 switch (h->root.type)
12955 {
12956 case bfd_link_hash_defined:
12957 case bfd_link_hash_defweak:
12958 return h->root.u.def.section;
12959
12960 case bfd_link_hash_common:
12961 return h->root.u.c.p->section;
12962
12963 default:
12964 break;
12965 }
12966 }
12967 else
12968 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12969
12970 return NULL;
12971}
12972
9e223787 12973/* Return the debug definition section. */
b7c871ed
L
12974
12975static asection *
12976elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12977 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12978 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12979 struct elf_link_hash_entry *h,
9e223787 12980 Elf_Internal_Sym *sym)
b7c871ed 12981{
9e223787
L
12982 if (h != NULL)
12983 {
12984 /* Return the global debug definition section. */
12985 if ((h->root.type == bfd_link_hash_defined
12986 || h->root.type == bfd_link_hash_defweak)
12987 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12988 return h->root.u.def.section;
12989 }
12990 else
12991 {
12992 /* Return the local debug definition section. */
12993 asection *isec = bfd_section_from_elf_index (sec->owner,
12994 sym->st_shndx);
12995 if ((isec->flags & SEC_DEBUGGING) != 0)
12996 return isec;
12997 }
b7c871ed
L
12998
12999 return NULL;
13000}
13001
5241d853
RS
13002/* COOKIE->rel describes a relocation against section SEC, which is
13003 a section we've decided to keep. Return the section that contains
13004 the relocation symbol, or NULL if no section contains it. */
13005
13006asection *
13007_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13008 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13009 struct elf_reloc_cookie *cookie,
13010 bfd_boolean *start_stop)
5241d853
RS
13011{
13012 unsigned long r_symndx;
13013 struct elf_link_hash_entry *h;
13014
13015 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13016 if (r_symndx == STN_UNDEF)
5241d853
RS
13017 return NULL;
13018
13019 if (r_symndx >= cookie->locsymcount
13020 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13021 {
13022 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13023 if (h == NULL)
13024 {
871b3ab2 13025 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13026 sec->owner);
13027 return NULL;
13028 }
5241d853
RS
13029 while (h->root.type == bfd_link_hash_indirect
13030 || h->root.type == bfd_link_hash_warning)
13031 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13032 h->mark = 1;
4e6b54a6
AM
13033 /* If this symbol is weak and there is a non-weak definition, we
13034 keep the non-weak definition because many backends put
13035 dynamic reloc info on the non-weak definition for code
13036 handling copy relocs. */
60d67dc8
AM
13037 if (h->is_weakalias)
13038 weakdef (h)->mark = 1;
1cce69b9 13039
a6a4679f 13040 if (start_stop != NULL)
1cce69b9 13041 {
7dba9362
AM
13042 /* To work around a glibc bug, mark XXX input sections
13043 when there is a reference to __start_XXX or __stop_XXX
13044 symbols. */
cbd0eecf 13045 if (h->start_stop)
1cce69b9 13046 {
cbd0eecf 13047 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13048 *start_stop = !s->gc_mark;
13049 return s;
1cce69b9
AM
13050 }
13051 }
13052
5241d853
RS
13053 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13054 }
13055
13056 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13057 &cookie->locsyms[r_symndx]);
13058}
13059
13060/* COOKIE->rel describes a relocation against section SEC, which is
13061 a section we've decided to keep. Mark the section that contains
9d0a14d3 13062 the relocation symbol. */
5241d853
RS
13063
13064bfd_boolean
13065_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13066 asection *sec,
13067 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13068 struct elf_reloc_cookie *cookie)
5241d853
RS
13069{
13070 asection *rsec;
1cce69b9 13071 bfd_boolean start_stop = FALSE;
5241d853 13072
1cce69b9
AM
13073 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13074 while (rsec != NULL)
5241d853 13075 {
1cce69b9
AM
13076 if (!rsec->gc_mark)
13077 {
13078 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13079 || (rsec->owner->flags & DYNAMIC) != 0)
13080 rsec->gc_mark = 1;
13081 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13082 return FALSE;
13083 }
13084 if (!start_stop)
13085 break;
199af150 13086 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13087 }
13088 return TRUE;
13089}
13090
07adf181
AM
13091/* The mark phase of garbage collection. For a given section, mark
13092 it and any sections in this section's group, and all the sections
13093 which define symbols to which it refers. */
13094
ccfa59ea
AM
13095bfd_boolean
13096_bfd_elf_gc_mark (struct bfd_link_info *info,
13097 asection *sec,
6a5bb875 13098 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13099{
13100 bfd_boolean ret;
9d0a14d3 13101 asection *group_sec, *eh_frame;
c152c796
AM
13102
13103 sec->gc_mark = 1;
13104
13105 /* Mark all the sections in the group. */
13106 group_sec = elf_section_data (sec)->next_in_group;
13107 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13108 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13109 return FALSE;
13110
13111 /* Look through the section relocs. */
13112 ret = TRUE;
9d0a14d3
RS
13113 eh_frame = elf_eh_frame_section (sec->owner);
13114 if ((sec->flags & SEC_RELOC) != 0
13115 && sec->reloc_count > 0
13116 && sec != eh_frame)
c152c796 13117 {
5241d853 13118 struct elf_reloc_cookie cookie;
c152c796 13119
5241d853
RS
13120 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13121 ret = FALSE;
c152c796 13122 else
c152c796 13123 {
5241d853 13124 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13125 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13126 {
13127 ret = FALSE;
13128 break;
13129 }
13130 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13131 }
13132 }
9d0a14d3
RS
13133
13134 if (ret && eh_frame && elf_fde_list (sec))
13135 {
13136 struct elf_reloc_cookie cookie;
13137
13138 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13139 ret = FALSE;
13140 else
13141 {
13142 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13143 gc_mark_hook, &cookie))
13144 ret = FALSE;
13145 fini_reloc_cookie_for_section (&cookie, eh_frame);
13146 }
13147 }
13148
2f0c68f2
CM
13149 eh_frame = elf_section_eh_frame_entry (sec);
13150 if (ret && eh_frame && !eh_frame->gc_mark)
13151 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13152 ret = FALSE;
13153
c152c796
AM
13154 return ret;
13155}
13156
3c758495
TG
13157/* Scan and mark sections in a special or debug section group. */
13158
13159static void
13160_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13161{
13162 /* Point to first section of section group. */
13163 asection *ssec;
13164 /* Used to iterate the section group. */
13165 asection *msec;
13166
13167 bfd_boolean is_special_grp = TRUE;
13168 bfd_boolean is_debug_grp = TRUE;
13169
13170 /* First scan to see if group contains any section other than debug
13171 and special section. */
13172 ssec = msec = elf_next_in_group (grp);
13173 do
13174 {
13175 if ((msec->flags & SEC_DEBUGGING) == 0)
13176 is_debug_grp = FALSE;
13177
13178 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13179 is_special_grp = FALSE;
13180
13181 msec = elf_next_in_group (msec);
13182 }
13183 while (msec != ssec);
13184
13185 /* If this is a pure debug section group or pure special section group,
13186 keep all sections in this group. */
13187 if (is_debug_grp || is_special_grp)
13188 {
13189 do
13190 {
13191 msec->gc_mark = 1;
13192 msec = elf_next_in_group (msec);
13193 }
13194 while (msec != ssec);
13195 }
13196}
13197
7f6ab9f8
AM
13198/* Keep debug and special sections. */
13199
13200bfd_boolean
13201_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13202 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13203{
13204 bfd *ibfd;
13205
c72f2fb2 13206 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13207 {
13208 asection *isec;
13209 bfd_boolean some_kept;
b40bf0a2 13210 bfd_boolean debug_frag_seen;
b7c871ed 13211 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13212
13213 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13214 continue;
57963c05
AM
13215 isec = ibfd->sections;
13216 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13217 continue;
7f6ab9f8 13218
b40bf0a2
NC
13219 /* Ensure all linker created sections are kept,
13220 see if any other section is already marked,
13221 and note if we have any fragmented debug sections. */
b7c871ed 13222 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13223 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13224 {
13225 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13226 isec->gc_mark = 1;
eb026f09
AM
13227 else if (isec->gc_mark
13228 && (isec->flags & SEC_ALLOC) != 0
13229 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13230 some_kept = TRUE;
b40bf0a2 13231
535b785f 13232 if (!debug_frag_seen
b40bf0a2
NC
13233 && (isec->flags & SEC_DEBUGGING)
13234 && CONST_STRNEQ (isec->name, ".debug_line."))
13235 debug_frag_seen = TRUE;
7f6ab9f8
AM
13236 }
13237
eb026f09
AM
13238 /* If no non-note alloc section in this file will be kept, then
13239 we can toss out the debug and special sections. */
7f6ab9f8
AM
13240 if (!some_kept)
13241 continue;
13242
13243 /* Keep debug and special sections like .comment when they are
3c758495
TG
13244 not part of a group. Also keep section groups that contain
13245 just debug sections or special sections. */
7f6ab9f8 13246 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13247 {
13248 if ((isec->flags & SEC_GROUP) != 0)
13249 _bfd_elf_gc_mark_debug_special_section_group (isec);
13250 else if (((isec->flags & SEC_DEBUGGING) != 0
13251 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13252 && elf_next_in_group (isec) == NULL)
13253 isec->gc_mark = 1;
b7c871ed
L
13254 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13255 has_kept_debug_info = TRUE;
3c758495 13256 }
b40bf0a2 13257
b40bf0a2
NC
13258 /* Look for CODE sections which are going to be discarded,
13259 and find and discard any fragmented debug sections which
13260 are associated with that code section. */
b7c871ed
L
13261 if (debug_frag_seen)
13262 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13263 if ((isec->flags & SEC_CODE) != 0
13264 && isec->gc_mark == 0)
13265 {
13266 unsigned int ilen;
13267 asection *dsec;
b40bf0a2 13268
b7c871ed 13269 ilen = strlen (isec->name);
b40bf0a2 13270
b7c871ed 13271 /* Association is determined by the name of the debug
07d6d2b8 13272 section containing the name of the code section as
b7c871ed
L
13273 a suffix. For example .debug_line.text.foo is a
13274 debug section associated with .text.foo. */
13275 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13276 {
13277 unsigned int dlen;
b40bf0a2 13278
b7c871ed
L
13279 if (dsec->gc_mark == 0
13280 || (dsec->flags & SEC_DEBUGGING) == 0)
13281 continue;
b40bf0a2 13282
b7c871ed 13283 dlen = strlen (dsec->name);
b40bf0a2 13284
b7c871ed
L
13285 if (dlen > ilen
13286 && strncmp (dsec->name + (dlen - ilen),
13287 isec->name, ilen) == 0)
b40bf0a2 13288 dsec->gc_mark = 0;
b7c871ed 13289 }
b40bf0a2 13290 }
b7c871ed
L
13291
13292 /* Mark debug sections referenced by kept debug sections. */
13293 if (has_kept_debug_info)
13294 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13295 if (isec->gc_mark
13296 && (isec->flags & SEC_DEBUGGING) != 0)
13297 if (!_bfd_elf_gc_mark (info, isec,
13298 elf_gc_mark_debug_section))
13299 return FALSE;
7f6ab9f8
AM
13300 }
13301 return TRUE;
13302}
13303
c152c796 13304static bfd_boolean
ccabcbe5 13305elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13306{
13307 bfd *sub;
ccabcbe5 13308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13309
c72f2fb2 13310 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13311 {
13312 asection *o;
13313
b19a8f85 13314 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13315 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13316 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13317 continue;
57963c05
AM
13318 o = sub->sections;
13319 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13320 continue;
c152c796
AM
13321
13322 for (o = sub->sections; o != NULL; o = o->next)
13323 {
a33dafc3
L
13324 /* When any section in a section group is kept, we keep all
13325 sections in the section group. If the first member of
13326 the section group is excluded, we will also exclude the
13327 group section. */
13328 if (o->flags & SEC_GROUP)
13329 {
13330 asection *first = elf_next_in_group (o);
13331 o->gc_mark = first->gc_mark;
13332 }
c152c796 13333
1e7eae0d 13334 if (o->gc_mark)
c152c796
AM
13335 continue;
13336
13337 /* Skip sweeping sections already excluded. */
13338 if (o->flags & SEC_EXCLUDE)
13339 continue;
13340
13341 /* Since this is early in the link process, it is simple
13342 to remove a section from the output. */
13343 o->flags |= SEC_EXCLUDE;
13344
c55fe096 13345 if (info->print_gc_sections && o->size != 0)
695344c0 13346 /* xgettext:c-format */
9793eb77 13347 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13348 o, sub);
c152c796
AM
13349 }
13350 }
13351
c152c796
AM
13352 return TRUE;
13353}
13354
13355/* Propagate collected vtable information. This is called through
13356 elf_link_hash_traverse. */
13357
13358static bfd_boolean
13359elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13360{
c152c796 13361 /* Those that are not vtables. */
cbd0eecf
L
13362 if (h->start_stop
13363 || h->u2.vtable == NULL
13364 || h->u2.vtable->parent == NULL)
c152c796
AM
13365 return TRUE;
13366
13367 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13368 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13369 return TRUE;
13370
13371 /* If we've already been done, exit. */
cbd0eecf 13372 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13373 return TRUE;
13374
13375 /* Make sure the parent's table is up to date. */
cbd0eecf 13376 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13377
cbd0eecf 13378 if (h->u2.vtable->used == NULL)
c152c796
AM
13379 {
13380 /* None of this table's entries were referenced. Re-use the
13381 parent's table. */
cbd0eecf
L
13382 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13383 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13384 }
13385 else
13386 {
13387 size_t n;
13388 bfd_boolean *cu, *pu;
13389
13390 /* Or the parent's entries into ours. */
cbd0eecf 13391 cu = h->u2.vtable->used;
c152c796 13392 cu[-1] = TRUE;
cbd0eecf 13393 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13394 if (pu != NULL)
13395 {
13396 const struct elf_backend_data *bed;
13397 unsigned int log_file_align;
13398
13399 bed = get_elf_backend_data (h->root.u.def.section->owner);
13400 log_file_align = bed->s->log_file_align;
cbd0eecf 13401 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13402 while (n--)
13403 {
13404 if (*pu)
13405 *cu = TRUE;
13406 pu++;
13407 cu++;
13408 }
13409 }
13410 }
13411
13412 return TRUE;
13413}
13414
13415static bfd_boolean
13416elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13417{
13418 asection *sec;
13419 bfd_vma hstart, hend;
13420 Elf_Internal_Rela *relstart, *relend, *rel;
13421 const struct elf_backend_data *bed;
13422 unsigned int log_file_align;
13423
c152c796
AM
13424 /* Take care of both those symbols that do not describe vtables as
13425 well as those that are not loaded. */
cbd0eecf
L
13426 if (h->start_stop
13427 || h->u2.vtable == NULL
13428 || h->u2.vtable->parent == NULL)
c152c796
AM
13429 return TRUE;
13430
13431 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13432 || h->root.type == bfd_link_hash_defweak);
13433
13434 sec = h->root.u.def.section;
13435 hstart = h->root.u.def.value;
13436 hend = hstart + h->size;
13437
13438 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13439 if (!relstart)
13440 return *(bfd_boolean *) okp = FALSE;
13441 bed = get_elf_backend_data (sec->owner);
13442 log_file_align = bed->s->log_file_align;
13443
056bafd4 13444 relend = relstart + sec->reloc_count;
c152c796
AM
13445
13446 for (rel = relstart; rel < relend; ++rel)
13447 if (rel->r_offset >= hstart && rel->r_offset < hend)
13448 {
13449 /* If the entry is in use, do nothing. */
cbd0eecf
L
13450 if (h->u2.vtable->used
13451 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13452 {
13453 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13454 if (h->u2.vtable->used[entry])
c152c796
AM
13455 continue;
13456 }
13457 /* Otherwise, kill it. */
13458 rel->r_offset = rel->r_info = rel->r_addend = 0;
13459 }
13460
13461 return TRUE;
13462}
13463
87538722
AM
13464/* Mark sections containing dynamically referenced symbols. When
13465 building shared libraries, we must assume that any visible symbol is
13466 referenced. */
715df9b8 13467
64d03ab5
AM
13468bfd_boolean
13469bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13470{
87538722 13471 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13472 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13473
715df9b8
EB
13474 if ((h->root.type == bfd_link_hash_defined
13475 || h->root.type == bfd_link_hash_defweak)
d664fd41 13476 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13477 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13478 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13479 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13480 && (!bfd_link_executable (info)
22185505 13481 || info->gc_keep_exported
b407645f
AM
13482 || info->export_dynamic
13483 || (h->dynamic
13484 && d != NULL
13485 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13486 && (h->versioned >= versioned
54e8959c
L
13487 || !bfd_hide_sym_by_version (info->version_info,
13488 h->root.root.string)))))
715df9b8
EB
13489 h->root.u.def.section->flags |= SEC_KEEP;
13490
13491 return TRUE;
13492}
3b36f7e6 13493
74f0fb50
AM
13494/* Keep all sections containing symbols undefined on the command-line,
13495 and the section containing the entry symbol. */
13496
13497void
13498_bfd_elf_gc_keep (struct bfd_link_info *info)
13499{
13500 struct bfd_sym_chain *sym;
13501
13502 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13503 {
13504 struct elf_link_hash_entry *h;
13505
13506 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13507 FALSE, FALSE, FALSE);
13508
13509 if (h != NULL
13510 && (h->root.type == bfd_link_hash_defined
13511 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13512 && !bfd_is_abs_section (h->root.u.def.section)
13513 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13514 h->root.u.def.section->flags |= SEC_KEEP;
13515 }
13516}
13517
2f0c68f2
CM
13518bfd_boolean
13519bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13520 struct bfd_link_info *info)
13521{
13522 bfd *ibfd = info->input_bfds;
13523
13524 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13525 {
13526 asection *sec;
13527 struct elf_reloc_cookie cookie;
13528
13529 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13530 continue;
57963c05
AM
13531 sec = ibfd->sections;
13532 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13533 continue;
2f0c68f2
CM
13534
13535 if (!init_reloc_cookie (&cookie, info, ibfd))
13536 return FALSE;
13537
13538 for (sec = ibfd->sections; sec; sec = sec->next)
13539 {
13540 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13541 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13542 {
13543 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13544 fini_reloc_cookie_rels (&cookie, sec);
13545 }
13546 }
13547 }
13548 return TRUE;
13549}
13550
c152c796
AM
13551/* Do mark and sweep of unused sections. */
13552
13553bfd_boolean
13554bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13555{
13556 bfd_boolean ok = TRUE;
13557 bfd *sub;
6a5bb875 13558 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13559 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13560 struct elf_link_hash_table *htab;
c152c796 13561
64d03ab5 13562 if (!bed->can_gc_sections
715df9b8 13563 || !is_elf_hash_table (info->hash))
c152c796 13564 {
9793eb77 13565 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13566 return TRUE;
13567 }
13568
74f0fb50 13569 bed->gc_keep (info);
da44f4e5 13570 htab = elf_hash_table (info);
74f0fb50 13571
9d0a14d3
RS
13572 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13573 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13574 for (sub = info->input_bfds;
13575 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13576 sub = sub->link.next)
9d0a14d3
RS
13577 {
13578 asection *sec;
13579 struct elf_reloc_cookie cookie;
13580
57963c05
AM
13581 sec = sub->sections;
13582 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13583 continue;
9d0a14d3 13584 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13585 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13586 {
13587 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13588 if (elf_section_data (sec)->sec_info
13589 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13590 elf_eh_frame_section (sub) = sec;
13591 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13592 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13593 }
13594 }
9d0a14d3 13595
c152c796 13596 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13597 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13598 if (!ok)
13599 return FALSE;
13600
13601 /* Kill the vtable relocations that were not used. */
da44f4e5 13602 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13603 if (!ok)
13604 return FALSE;
13605
715df9b8 13606 /* Mark dynamically referenced symbols. */
22185505 13607 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13608 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13609
715df9b8 13610 /* Grovel through relocs to find out who stays ... */
64d03ab5 13611 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13612 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13613 {
13614 asection *o;
13615
b19a8f85 13616 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13617 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 13618 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13619 continue;
13620
57963c05
AM
13621 o = sub->sections;
13622 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13623 continue;
13624
7f6ab9f8
AM
13625 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13626 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
13627 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13628 well as FINI_ARRAY sections for ld -r. */
c152c796 13629 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13630 if (!o->gc_mark
13631 && (o->flags & SEC_EXCLUDE) == 0
24007750 13632 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
13633 || (bfd_link_relocatable (info)
13634 && ((elf_section_data (o)->this_hdr.sh_type
13635 == SHT_PREINIT_ARRAY)
13636 || (elf_section_data (o)->this_hdr.sh_type
13637 == SHT_INIT_ARRAY)
13638 || (elf_section_data (o)->this_hdr.sh_type
13639 == SHT_FINI_ARRAY)))
7f6ab9f8
AM
13640 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13641 && elf_next_in_group (o) == NULL )))
13642 {
13643 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13644 return FALSE;
13645 }
c152c796
AM
13646 }
13647
6a5bb875 13648 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13649 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13650
c152c796 13651 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13652 return elf_gc_sweep (abfd, info);
c152c796
AM
13653}
13654\f
13655/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13656
13657bfd_boolean
13658bfd_elf_gc_record_vtinherit (bfd *abfd,
13659 asection *sec,
13660 struct elf_link_hash_entry *h,
13661 bfd_vma offset)
13662{
13663 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13664 struct elf_link_hash_entry **search, *child;
ef53be89 13665 size_t extsymcount;
c152c796
AM
13666 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13667
13668 /* The sh_info field of the symtab header tells us where the
13669 external symbols start. We don't care about the local symbols at
13670 this point. */
13671 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13672 if (!elf_bad_symtab (abfd))
13673 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13674
13675 sym_hashes = elf_sym_hashes (abfd);
13676 sym_hashes_end = sym_hashes + extsymcount;
13677
13678 /* Hunt down the child symbol, which is in this section at the same
13679 offset as the relocation. */
13680 for (search = sym_hashes; search != sym_hashes_end; ++search)
13681 {
13682 if ((child = *search) != NULL
13683 && (child->root.type == bfd_link_hash_defined
13684 || child->root.type == bfd_link_hash_defweak)
13685 && child->root.u.def.section == sec
13686 && child->root.u.def.value == offset)
13687 goto win;
13688 }
13689
695344c0 13690 /* xgettext:c-format */
9793eb77 13691 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 13692 abfd, sec, (uint64_t) offset);
c152c796
AM
13693 bfd_set_error (bfd_error_invalid_operation);
13694 return FALSE;
13695
13696 win:
cbd0eecf 13697 if (!child->u2.vtable)
f6e332e6 13698 {
cbd0eecf
L
13699 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13700 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13701 if (!child->u2.vtable)
f6e332e6
AM
13702 return FALSE;
13703 }
c152c796
AM
13704 if (!h)
13705 {
13706 /* This *should* only be the absolute section. It could potentially
13707 be that someone has defined a non-global vtable though, which
13708 would be bad. It isn't worth paging in the local symbols to be
13709 sure though; that case should simply be handled by the assembler. */
13710
cbd0eecf 13711 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13712 }
13713 else
cbd0eecf 13714 child->u2.vtable->parent = h;
c152c796
AM
13715
13716 return TRUE;
13717}
13718
13719/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13720
13721bfd_boolean
13722bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13723 asection *sec ATTRIBUTE_UNUSED,
13724 struct elf_link_hash_entry *h,
13725 bfd_vma addend)
13726{
13727 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13728 unsigned int log_file_align = bed->s->log_file_align;
13729
cbd0eecf 13730 if (!h->u2.vtable)
f6e332e6 13731 {
cbd0eecf
L
13732 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13733 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13734 if (!h->u2.vtable)
f6e332e6
AM
13735 return FALSE;
13736 }
13737
cbd0eecf 13738 if (addend >= h->u2.vtable->size)
c152c796
AM
13739 {
13740 size_t size, bytes, file_align;
cbd0eecf 13741 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
13742
13743 /* While the symbol is undefined, we have to be prepared to handle
13744 a zero size. */
13745 file_align = 1 << log_file_align;
13746 if (h->root.type == bfd_link_hash_undefined)
13747 size = addend + file_align;
13748 else
13749 {
13750 size = h->size;
13751 if (addend >= size)
13752 {
13753 /* Oops! We've got a reference past the defined end of
13754 the table. This is probably a bug -- shall we warn? */
13755 size = addend + file_align;
13756 }
13757 }
13758 size = (size + file_align - 1) & -file_align;
13759
13760 /* Allocate one extra entry for use as a "done" flag for the
13761 consolidation pass. */
13762 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13763
13764 if (ptr)
13765 {
a50b1753 13766 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13767
13768 if (ptr != NULL)
13769 {
13770 size_t oldbytes;
13771
cbd0eecf 13772 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
13773 * sizeof (bfd_boolean));
13774 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13775 }
13776 }
13777 else
a50b1753 13778 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13779
13780 if (ptr == NULL)
13781 return FALSE;
13782
13783 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
13784 h->u2.vtable->used = ptr + 1;
13785 h->u2.vtable->size = size;
c152c796
AM
13786 }
13787
cbd0eecf 13788 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13789
13790 return TRUE;
13791}
13792
ae17ab41
CM
13793/* Map an ELF section header flag to its corresponding string. */
13794typedef struct
13795{
13796 char *flag_name;
13797 flagword flag_value;
13798} elf_flags_to_name_table;
13799
13800static elf_flags_to_name_table elf_flags_to_names [] =
13801{
13802 { "SHF_WRITE", SHF_WRITE },
13803 { "SHF_ALLOC", SHF_ALLOC },
13804 { "SHF_EXECINSTR", SHF_EXECINSTR },
13805 { "SHF_MERGE", SHF_MERGE },
13806 { "SHF_STRINGS", SHF_STRINGS },
13807 { "SHF_INFO_LINK", SHF_INFO_LINK},
13808 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13809 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13810 { "SHF_GROUP", SHF_GROUP },
13811 { "SHF_TLS", SHF_TLS },
13812 { "SHF_MASKOS", SHF_MASKOS },
13813 { "SHF_EXCLUDE", SHF_EXCLUDE },
13814};
13815
b9c361e0
JL
13816/* Returns TRUE if the section is to be included, otherwise FALSE. */
13817bfd_boolean
ae17ab41 13818bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13819 struct flag_info *flaginfo,
b9c361e0 13820 asection *section)
ae17ab41 13821{
8b127cbc 13822 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13823
8b127cbc 13824 if (!flaginfo->flags_initialized)
ae17ab41 13825 {
8b127cbc
AM
13826 bfd *obfd = info->output_bfd;
13827 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13828 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13829 int with_hex = 0;
13830 int without_hex = 0;
13831
8b127cbc 13832 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13833 {
b9c361e0 13834 unsigned i;
8b127cbc 13835 flagword (*lookup) (char *);
ae17ab41 13836
8b127cbc
AM
13837 lookup = bed->elf_backend_lookup_section_flags_hook;
13838 if (lookup != NULL)
ae17ab41 13839 {
8b127cbc 13840 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13841
13842 if (hexval != 0)
13843 {
13844 if (tf->with == with_flags)
13845 with_hex |= hexval;
13846 else if (tf->with == without_flags)
13847 without_hex |= hexval;
13848 tf->valid = TRUE;
13849 continue;
13850 }
ae17ab41 13851 }
8b127cbc 13852 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13853 {
8b127cbc 13854 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13855 {
13856 if (tf->with == with_flags)
13857 with_hex |= elf_flags_to_names[i].flag_value;
13858 else if (tf->with == without_flags)
13859 without_hex |= elf_flags_to_names[i].flag_value;
13860 tf->valid = TRUE;
13861 break;
13862 }
13863 }
8b127cbc 13864 if (!tf->valid)
b9c361e0 13865 {
68ffbac6 13866 info->callbacks->einfo
9793eb77 13867 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13868 return FALSE;
ae17ab41
CM
13869 }
13870 }
8b127cbc
AM
13871 flaginfo->flags_initialized = TRUE;
13872 flaginfo->only_with_flags |= with_hex;
13873 flaginfo->not_with_flags |= without_hex;
ae17ab41 13874 }
ae17ab41 13875
8b127cbc 13876 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13877 return FALSE;
13878
8b127cbc 13879 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13880 return FALSE;
13881
13882 return TRUE;
ae17ab41
CM
13883}
13884
c152c796
AM
13885struct alloc_got_off_arg {
13886 bfd_vma gotoff;
10455f89 13887 struct bfd_link_info *info;
c152c796
AM
13888};
13889
13890/* We need a special top-level link routine to convert got reference counts
13891 to real got offsets. */
13892
13893static bfd_boolean
13894elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13895{
a50b1753 13896 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13897 bfd *obfd = gofarg->info->output_bfd;
13898 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13899
c152c796
AM
13900 if (h->got.refcount > 0)
13901 {
13902 h->got.offset = gofarg->gotoff;
10455f89 13903 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13904 }
13905 else
13906 h->got.offset = (bfd_vma) -1;
13907
13908 return TRUE;
13909}
13910
13911/* And an accompanying bit to work out final got entry offsets once
13912 we're done. Should be called from final_link. */
13913
13914bfd_boolean
13915bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13916 struct bfd_link_info *info)
13917{
13918 bfd *i;
13919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13920 bfd_vma gotoff;
c152c796
AM
13921 struct alloc_got_off_arg gofarg;
13922
10455f89
HPN
13923 BFD_ASSERT (abfd == info->output_bfd);
13924
c152c796
AM
13925 if (! is_elf_hash_table (info->hash))
13926 return FALSE;
13927
13928 /* The GOT offset is relative to the .got section, but the GOT header is
13929 put into the .got.plt section, if the backend uses it. */
13930 if (bed->want_got_plt)
13931 gotoff = 0;
13932 else
13933 gotoff = bed->got_header_size;
13934
13935 /* Do the local .got entries first. */
c72f2fb2 13936 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13937 {
13938 bfd_signed_vma *local_got;
ef53be89 13939 size_t j, locsymcount;
c152c796
AM
13940 Elf_Internal_Shdr *symtab_hdr;
13941
13942 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13943 continue;
13944
13945 local_got = elf_local_got_refcounts (i);
13946 if (!local_got)
13947 continue;
13948
13949 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13950 if (elf_bad_symtab (i))
13951 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13952 else
13953 locsymcount = symtab_hdr->sh_info;
13954
13955 for (j = 0; j < locsymcount; ++j)
13956 {
13957 if (local_got[j] > 0)
13958 {
13959 local_got[j] = gotoff;
10455f89 13960 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13961 }
13962 else
13963 local_got[j] = (bfd_vma) -1;
13964 }
13965 }
13966
13967 /* Then the global .got entries. .plt refcounts are handled by
13968 adjust_dynamic_symbol */
13969 gofarg.gotoff = gotoff;
10455f89 13970 gofarg.info = info;
c152c796
AM
13971 elf_link_hash_traverse (elf_hash_table (info),
13972 elf_gc_allocate_got_offsets,
13973 &gofarg);
13974 return TRUE;
13975}
13976
13977/* Many folk need no more in the way of final link than this, once
13978 got entry reference counting is enabled. */
13979
13980bfd_boolean
13981bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13982{
13983 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13984 return FALSE;
13985
13986 /* Invoke the regular ELF backend linker to do all the work. */
13987 return bfd_elf_final_link (abfd, info);
13988}
13989
13990bfd_boolean
13991bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13992{
a50b1753 13993 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13994
13995 if (rcookie->bad_symtab)
13996 rcookie->rel = rcookie->rels;
13997
13998 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13999 {
14000 unsigned long r_symndx;
14001
14002 if (! rcookie->bad_symtab)
14003 if (rcookie->rel->r_offset > offset)
14004 return FALSE;
14005 if (rcookie->rel->r_offset != offset)
14006 continue;
14007
14008 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14009 if (r_symndx == STN_UNDEF)
c152c796
AM
14010 return TRUE;
14011
14012 if (r_symndx >= rcookie->locsymcount
14013 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14014 {
14015 struct elf_link_hash_entry *h;
14016
14017 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14018
14019 while (h->root.type == bfd_link_hash_indirect
14020 || h->root.type == bfd_link_hash_warning)
14021 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14022
14023 if ((h->root.type == bfd_link_hash_defined
14024 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14025 && (h->root.u.def.section->owner != rcookie->abfd
14026 || h->root.u.def.section->kept_section != NULL
14027 || discarded_section (h->root.u.def.section)))
c152c796 14028 return TRUE;
c152c796
AM
14029 }
14030 else
14031 {
14032 /* It's not a relocation against a global symbol,
14033 but it could be a relocation against a local
14034 symbol for a discarded section. */
14035 asection *isec;
14036 Elf_Internal_Sym *isym;
14037
14038 /* Need to: get the symbol; get the section. */
14039 isym = &rcookie->locsyms[r_symndx];
cb33740c 14040 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14041 if (isec != NULL
14042 && (isec->kept_section != NULL
14043 || discarded_section (isec)))
cb33740c 14044 return TRUE;
c152c796
AM
14045 }
14046 return FALSE;
14047 }
14048 return FALSE;
14049}
14050
14051/* Discard unneeded references to discarded sections.
75938853
AM
14052 Returns -1 on error, 1 if any section's size was changed, 0 if
14053 nothing changed. This function assumes that the relocations are in
14054 sorted order, which is true for all known assemblers. */
c152c796 14055
75938853 14056int
c152c796
AM
14057bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14058{
14059 struct elf_reloc_cookie cookie;
18cd5bce 14060 asection *o;
c152c796 14061 bfd *abfd;
75938853 14062 int changed = 0;
c152c796
AM
14063
14064 if (info->traditional_format
14065 || !is_elf_hash_table (info->hash))
75938853 14066 return 0;
c152c796 14067
18cd5bce
AM
14068 o = bfd_get_section_by_name (output_bfd, ".stab");
14069 if (o != NULL)
c152c796 14070 {
18cd5bce 14071 asection *i;
c152c796 14072
18cd5bce 14073 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14074 {
18cd5bce
AM
14075 if (i->size == 0
14076 || i->reloc_count == 0
14077 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14078 continue;
c152c796 14079
18cd5bce
AM
14080 abfd = i->owner;
14081 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14082 continue;
c152c796 14083
18cd5bce 14084 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14085 return -1;
c152c796 14086
18cd5bce
AM
14087 if (_bfd_discard_section_stabs (abfd, i,
14088 elf_section_data (i)->sec_info,
5241d853
RS
14089 bfd_elf_reloc_symbol_deleted_p,
14090 &cookie))
75938853 14091 changed = 1;
18cd5bce
AM
14092
14093 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14094 }
18cd5bce
AM
14095 }
14096
2f0c68f2
CM
14097 o = NULL;
14098 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14099 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14100 if (o != NULL)
14101 {
14102 asection *i;
d7153c4a 14103 int eh_changed = 0;
79a94a2a 14104 unsigned int eh_alignment;
c152c796 14105
18cd5bce 14106 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14107 {
18cd5bce
AM
14108 if (i->size == 0)
14109 continue;
14110
14111 abfd = i->owner;
14112 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14113 continue;
14114
14115 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14116 return -1;
18cd5bce
AM
14117
14118 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14119 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14120 bfd_elf_reloc_symbol_deleted_p,
14121 &cookie))
d7153c4a
AM
14122 {
14123 eh_changed = 1;
14124 if (i->size != i->rawsize)
14125 changed = 1;
14126 }
18cd5bce
AM
14127
14128 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14129 }
9866ffe2 14130
79a94a2a 14131 eh_alignment = 1 << o->alignment_power;
9866ffe2
AM
14132 /* Skip over zero terminator, and prevent empty sections from
14133 adding alignment padding at the end. */
14134 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14135 if (i->size == 0)
14136 i->flags |= SEC_EXCLUDE;
14137 else if (i->size > 4)
14138 break;
14139 /* The last non-empty eh_frame section doesn't need padding. */
14140 if (i != NULL)
14141 i = i->map_tail.s;
14142 /* Any prior sections must pad the last FDE out to the output
14143 section alignment. Otherwise we might have zero padding
14144 between sections, which would be seen as a terminator. */
14145 for (; i != NULL; i = i->map_tail.s)
14146 if (i->size == 4)
14147 /* All but the last zero terminator should have been removed. */
14148 BFD_FAIL ();
14149 else
14150 {
14151 bfd_size_type size
14152 = (i->size + eh_alignment - 1) & -eh_alignment;
14153 if (i->size != size)
af471f82 14154 {
9866ffe2
AM
14155 i->size = size;
14156 changed = 1;
14157 eh_changed = 1;
af471f82 14158 }
9866ffe2 14159 }
d7153c4a
AM
14160 if (eh_changed)
14161 elf_link_hash_traverse (elf_hash_table (info),
14162 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14163 }
c152c796 14164
18cd5bce
AM
14165 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14166 {
14167 const struct elf_backend_data *bed;
57963c05 14168 asection *s;
c152c796 14169
18cd5bce
AM
14170 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14171 continue;
57963c05
AM
14172 s = abfd->sections;
14173 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14174 continue;
18cd5bce
AM
14175
14176 bed = get_elf_backend_data (abfd);
14177
14178 if (bed->elf_backend_discard_info != NULL)
14179 {
14180 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14181 return -1;
18cd5bce
AM
14182
14183 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14184 changed = 1;
18cd5bce
AM
14185
14186 fini_reloc_cookie (&cookie, abfd);
14187 }
c152c796
AM
14188 }
14189
2f0c68f2
CM
14190 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14191 _bfd_elf_end_eh_frame_parsing (info);
14192
14193 if (info->eh_frame_hdr_type
0e1862bb 14194 && !bfd_link_relocatable (info)
c152c796 14195 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14196 changed = 1;
c152c796 14197
75938853 14198 return changed;
c152c796 14199}
082b7297 14200
43e1669b 14201bfd_boolean
0c511000 14202_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14203 asection *sec,
c0f00686 14204 struct bfd_link_info *info)
082b7297
L
14205{
14206 flagword flags;
c77ec726 14207 const char *name, *key;
082b7297
L
14208 struct bfd_section_already_linked *l;
14209 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14210
c77ec726
AM
14211 if (sec->output_section == bfd_abs_section_ptr)
14212 return FALSE;
0c511000 14213
c77ec726 14214 flags = sec->flags;
0c511000 14215
c77ec726
AM
14216 /* Return if it isn't a linkonce section. A comdat group section
14217 also has SEC_LINK_ONCE set. */
14218 if ((flags & SEC_LINK_ONCE) == 0)
14219 return FALSE;
0c511000 14220
c77ec726
AM
14221 /* Don't put group member sections on our list of already linked
14222 sections. They are handled as a group via their group section. */
14223 if (elf_sec_group (sec) != NULL)
14224 return FALSE;
0c511000 14225
c77ec726
AM
14226 /* For a SHT_GROUP section, use the group signature as the key. */
14227 name = sec->name;
14228 if ((flags & SEC_GROUP) != 0
14229 && elf_next_in_group (sec) != NULL
14230 && elf_group_name (elf_next_in_group (sec)) != NULL)
14231 key = elf_group_name (elf_next_in_group (sec));
14232 else
14233 {
14234 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14235 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14236 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14237 key++;
0c511000 14238 else
c77ec726
AM
14239 /* Must be a user linkonce section that doesn't follow gcc's
14240 naming convention. In this case we won't be matching
14241 single member groups. */
14242 key = name;
0c511000 14243 }
6d2cd210 14244
c77ec726 14245 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14246
14247 for (l = already_linked_list->entry; l != NULL; l = l->next)
14248 {
c2370991 14249 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14250 sections with a signature of <key> (<key> is some string),
14251 and linkonce sections named .gnu.linkonce.<type>.<key>.
14252 Match like sections. LTO plugin sections are an exception.
14253 They are always named .gnu.linkonce.t.<key> and match either
14254 type of section. */
14255 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14256 && ((flags & SEC_GROUP) != 0
14257 || strcmp (name, l->sec->name) == 0))
14258 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14259 {
14260 /* The section has already been linked. See if we should
6d2cd210 14261 issue a warning. */
c77ec726
AM
14262 if (!_bfd_handle_already_linked (sec, l, info))
14263 return FALSE;
082b7297 14264
c77ec726 14265 if (flags & SEC_GROUP)
3d7f7666 14266 {
c77ec726
AM
14267 asection *first = elf_next_in_group (sec);
14268 asection *s = first;
3d7f7666 14269
c77ec726 14270 while (s != NULL)
3d7f7666 14271 {
c77ec726
AM
14272 s->output_section = bfd_abs_section_ptr;
14273 /* Record which group discards it. */
14274 s->kept_section = l->sec;
14275 s = elf_next_in_group (s);
14276 /* These lists are circular. */
14277 if (s == first)
14278 break;
3d7f7666
L
14279 }
14280 }
082b7297 14281
43e1669b 14282 return TRUE;
082b7297
L
14283 }
14284 }
14285
c77ec726
AM
14286 /* A single member comdat group section may be discarded by a
14287 linkonce section and vice versa. */
14288 if ((flags & SEC_GROUP) != 0)
3d7f7666 14289 {
c77ec726 14290 asection *first = elf_next_in_group (sec);
c2370991 14291
c77ec726
AM
14292 if (first != NULL && elf_next_in_group (first) == first)
14293 /* Check this single member group against linkonce sections. */
14294 for (l = already_linked_list->entry; l != NULL; l = l->next)
14295 if ((l->sec->flags & SEC_GROUP) == 0
14296 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14297 {
14298 first->output_section = bfd_abs_section_ptr;
14299 first->kept_section = l->sec;
14300 sec->output_section = bfd_abs_section_ptr;
14301 break;
14302 }
14303 }
14304 else
14305 /* Check this linkonce section against single member groups. */
14306 for (l = already_linked_list->entry; l != NULL; l = l->next)
14307 if (l->sec->flags & SEC_GROUP)
6d2cd210 14308 {
c77ec726 14309 asection *first = elf_next_in_group (l->sec);
6d2cd210 14310
c77ec726
AM
14311 if (first != NULL
14312 && elf_next_in_group (first) == first
14313 && bfd_elf_match_symbols_in_sections (first, sec, info))
14314 {
14315 sec->output_section = bfd_abs_section_ptr;
14316 sec->kept_section = first;
14317 break;
14318 }
6d2cd210 14319 }
0c511000 14320
c77ec726
AM
14321 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14322 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14323 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14324 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14325 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14326 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14327 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14328 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14329 The reverse order cannot happen as there is never a bfd with only the
14330 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14331 matter as here were are looking only for cross-bfd sections. */
14332
14333 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14334 for (l = already_linked_list->entry; l != NULL; l = l->next)
14335 if ((l->sec->flags & SEC_GROUP) == 0
14336 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14337 {
14338 if (abfd != l->sec->owner)
14339 sec->output_section = bfd_abs_section_ptr;
14340 break;
14341 }
80c29487 14342
082b7297 14343 /* This is the first section with this name. Record it. */
c77ec726 14344 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14345 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14346 return sec->output_section == bfd_abs_section_ptr;
082b7297 14347}
81e1b023 14348
a4d8e49b
L
14349bfd_boolean
14350_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14351{
14352 return sym->st_shndx == SHN_COMMON;
14353}
14354
14355unsigned int
14356_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14357{
14358 return SHN_COMMON;
14359}
14360
14361asection *
14362_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14363{
14364 return bfd_com_section_ptr;
14365}
10455f89
HPN
14366
14367bfd_vma
14368_bfd_elf_default_got_elt_size (bfd *abfd,
14369 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14370 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14371 bfd *ibfd ATTRIBUTE_UNUSED,
14372 unsigned long symndx ATTRIBUTE_UNUSED)
14373{
14374 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14375 return bed->s->arch_size / 8;
14376}
83bac4b0
NC
14377
14378/* Routines to support the creation of dynamic relocs. */
14379
83bac4b0
NC
14380/* Returns the name of the dynamic reloc section associated with SEC. */
14381
14382static const char *
14383get_dynamic_reloc_section_name (bfd * abfd,
14384 asection * sec,
14385 bfd_boolean is_rela)
14386{
ddcf1fcf
BS
14387 char *name;
14388 const char *old_name = bfd_get_section_name (NULL, sec);
14389 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14390
ddcf1fcf 14391 if (old_name == NULL)
83bac4b0
NC
14392 return NULL;
14393
ddcf1fcf 14394 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14395 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14396
14397 return name;
14398}
14399
14400/* Returns the dynamic reloc section associated with SEC.
14401 If necessary compute the name of the dynamic reloc section based
14402 on SEC's name (looked up in ABFD's string table) and the setting
14403 of IS_RELA. */
14404
14405asection *
14406_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14407 asection * sec,
14408 bfd_boolean is_rela)
14409{
14410 asection * reloc_sec = elf_section_data (sec)->sreloc;
14411
14412 if (reloc_sec == NULL)
14413 {
14414 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14415
14416 if (name != NULL)
14417 {
3d4d4302 14418 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14419
14420 if (reloc_sec != NULL)
14421 elf_section_data (sec)->sreloc = reloc_sec;
14422 }
14423 }
14424
14425 return reloc_sec;
14426}
14427
14428/* Returns the dynamic reloc section associated with SEC. If the
14429 section does not exist it is created and attached to the DYNOBJ
14430 bfd and stored in the SRELOC field of SEC's elf_section_data
14431 structure.
f8076f98 14432
83bac4b0
NC
14433 ALIGNMENT is the alignment for the newly created section and
14434 IS_RELA defines whether the name should be .rela.<SEC's name>
14435 or .rel.<SEC's name>. The section name is looked up in the
14436 string table associated with ABFD. */
14437
14438asection *
ca4be51c
AM
14439_bfd_elf_make_dynamic_reloc_section (asection *sec,
14440 bfd *dynobj,
14441 unsigned int alignment,
14442 bfd *abfd,
14443 bfd_boolean is_rela)
83bac4b0
NC
14444{
14445 asection * reloc_sec = elf_section_data (sec)->sreloc;
14446
14447 if (reloc_sec == NULL)
14448 {
14449 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14450
14451 if (name == NULL)
14452 return NULL;
14453
3d4d4302 14454 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14455
14456 if (reloc_sec == NULL)
14457 {
3d4d4302
AM
14458 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14459 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14460 if ((sec->flags & SEC_ALLOC) != 0)
14461 flags |= SEC_ALLOC | SEC_LOAD;
14462
3d4d4302 14463 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14464 if (reloc_sec != NULL)
14465 {
8877b5e5
AM
14466 /* _bfd_elf_get_sec_type_attr chooses a section type by
14467 name. Override as it may be wrong, eg. for a user
14468 section named "auto" we'll get ".relauto" which is
14469 seen to be a .rela section. */
14470 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14471 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14472 reloc_sec = NULL;
14473 }
14474 }
14475
14476 elf_section_data (sec)->sreloc = reloc_sec;
14477 }
14478
14479 return reloc_sec;
14480}
1338dd10 14481
bffebb6b
AM
14482/* Copy the ELF symbol type and other attributes for a linker script
14483 assignment from HSRC to HDEST. Generally this should be treated as
14484 if we found a strong non-dynamic definition for HDEST (except that
14485 ld ignores multiple definition errors). */
1338dd10 14486void
bffebb6b
AM
14487_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14488 struct bfd_link_hash_entry *hdest,
14489 struct bfd_link_hash_entry *hsrc)
1338dd10 14490{
bffebb6b
AM
14491 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14492 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14493 Elf_Internal_Sym isym;
1338dd10
PB
14494
14495 ehdest->type = ehsrc->type;
35fc36a8 14496 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14497
14498 isym.st_other = ehsrc->other;
b8417128 14499 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14500}
351f65ca
L
14501
14502/* Append a RELA relocation REL to section S in BFD. */
14503
14504void
14505elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14506{
14507 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14508 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14509 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14510 bed->s->swap_reloca_out (abfd, rel, loc);
14511}
14512
14513/* Append a REL relocation REL to section S in BFD. */
14514
14515void
14516elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14517{
14518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14519 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14520 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14521 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14522}
7dba9362
AM
14523
14524/* Define __start, __stop, .startof. or .sizeof. symbol. */
14525
14526struct bfd_link_hash_entry *
14527bfd_elf_define_start_stop (struct bfd_link_info *info,
14528 const char *symbol, asection *sec)
14529{
487b6440 14530 struct elf_link_hash_entry *h;
7dba9362 14531
487b6440
AM
14532 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14533 FALSE, FALSE, TRUE);
14534 if (h != NULL
14535 && (h->root.type == bfd_link_hash_undefined
14536 || h->root.type == bfd_link_hash_undefweak
bf3077a6 14537 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
7dba9362 14538 {
bf3077a6 14539 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
487b6440
AM
14540 h->root.type = bfd_link_hash_defined;
14541 h->root.u.def.section = sec;
14542 h->root.u.def.value = 0;
14543 h->def_regular = 1;
14544 h->def_dynamic = 0;
14545 h->start_stop = 1;
14546 h->u2.start_stop_section = sec;
14547 if (symbol[0] == '.')
14548 {
14549 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
14550 const struct elf_backend_data *bed;
14551 bed = get_elf_backend_data (info->output_bfd);
14552 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 14553 }
36b8fda5
AM
14554 else
14555 {
14556 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14557 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
bf3077a6 14558 if (was_dynamic)
36b8fda5
AM
14559 bfd_elf_link_record_dynamic_symbol (info, h);
14560 }
487b6440 14561 return &h->root;
7dba9362 14562 }
487b6440 14563 return NULL;
7dba9362 14564}
This page took 3.533613 seconds and 4 git commands to generate.