]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Solaris2/LD: Fix anonymous version script acceptance bug
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 }
123
124 bh = &h->root;
cf18fda4 125 bed = get_elf_backend_data (abfd);
d98685ac 126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 127 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
e28df02b 132 h->non_elf = 0;
12b2843a 133 h->root.linker_def = 1;
d98685ac 134 h->type = STT_OBJECT;
00b7642b
AM
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 137
ccabcbe5 138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
139 return h;
140}
141
b34976b6 142bfd_boolean
268b6b39 143_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
144{
145 flagword flags;
aad5d350 146 asection *s;
252b5132 147 struct elf_link_hash_entry *h;
9c5bfbb7 148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 149 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
150
151 /* This function may be called more than once. */
ce558b89 152 if (htab->sgot != NULL)
b34976b6 153 return TRUE;
252b5132 154
e5a52504 155 flags = bed->dynamic_sec_flags;
252b5132 156
14b2f831
AM
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
6de2ae4a
L
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
252b5132 166
14b2f831 167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
252b5132
RH
173 if (bed->want_got_plt)
174 {
14b2f831 175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 176 if (s == NULL
6de2ae4a
L
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
b34976b6 179 return FALSE;
6de2ae4a 180 htab->sgotplt = s;
252b5132
RH
181 }
182
64e77c6d
L
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
2517a57f
AM
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
6de2ae4a
L
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
2517a57f 194 elf_hash_table (info)->hgot = h;
d98685ac
AM
195 if (h == NULL)
196 return FALSE;
2517a57f 197 }
252b5132 198
b34976b6 199 return TRUE;
252b5132
RH
200}
201\f
7e9f0867
AM
202/* Create a strtab to hold the dynamic symbol names. */
203static bfd_boolean
204_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205{
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
6cd255ca
L
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e
L
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6cd255ca
L
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
7e9f0867
AM
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236}
237
45d6a902
AM
238/* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
252b5132 244
b34976b6 245bfd_boolean
268b6b39 246_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 247{
45d6a902 248 flagword flags;
91d6fa6a 249 asection *s;
9c5bfbb7 250 const struct elf_backend_data *bed;
9637f6ef 251 struct elf_link_hash_entry *h;
252b5132 252
0eddce27 253 if (! is_elf_hash_table (info->hash))
45d6a902
AM
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
7e9f0867
AM
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
45d6a902 261
7e9f0867 262 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
45d6a902
AM
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
9b8b325a 269 if (bfd_link_executable (info) && !info->nointerp)
252b5132 270 {
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
3496cb2a 273 if (s == NULL)
45d6a902
AM
274 return FALSE;
275 }
bb0deeff 276
45d6a902
AM
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
14b2f831
AM
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
45d6a902 281 if (s == NULL
45d6a902
AM
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
14b2f831
AM
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
45d6a902 287 if (s == NULL
45d6a902
AM
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
14b2f831
AM
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
45d6a902 293 if (s == NULL
45d6a902
AM
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
14b2f831
AM
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
45d6a902 299 if (s == NULL
45d6a902
AM
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
cae1fbbb 302 elf_hash_table (info)->dynsym = s;
45d6a902 303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
3496cb2a 306 if (s == NULL)
45d6a902
AM
307 return FALSE;
308
14b2f831 309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 310 if (s == NULL
45d6a902
AM
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
9637f6ef
L
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
45d6a902
AM
323 return FALSE;
324
fdc90cb4
JJ
325 if (info->emit_hash)
326 {
14b2f831
AM
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
fdc90cb4
JJ
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
14b2f831
AM
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
fdc90cb4
JJ
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
45d6a902
AM
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
894891db
NC
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361}
362
363/* Create dynamic sections when linking against a dynamic object. */
364
365bfd_boolean
268b6b39 366_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
367{
368 flagword flags, pltflags;
7325306f 369 struct elf_link_hash_entry *h;
45d6a902 370 asection *s;
9c5bfbb7 371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 372 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 373
252b5132
RH
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
e5a52504 376 flags = bed->dynamic_sec_flags;
252b5132
RH
377
378 pltflags = flags;
252b5132 379 if (bed->plt_not_loaded)
6df4d94c
MM
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
5d1634d7 383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
14b2f831 389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 390 if (s == NULL
252b5132 391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->splt = s;
252b5132 394
d98685ac
AM
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
7325306f
RS
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
252b5132 405
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
252b5132 410 if (s == NULL
45d6a902 411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 412 return FALSE;
6de2ae4a 413 htab->srelplt = s;
252b5132
RH
414
415 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 416 return FALSE;
252b5132 417
3018b441
RH
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
14b2f831
AM
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 428 if (s == NULL)
b34976b6 429 return FALSE;
9d19e4fd 430 htab->sdynbss = s;
252b5132 431
5474d94f
AM
432 if (bed->want_dynrelro)
433 {
434 /* Similarly, but for symbols that were originally in read-only
435 sections. */
436 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
437 (SEC_ALLOC | SEC_READONLY
438 | SEC_HAS_CONTENTS
439 | SEC_LINKER_CREATED));
440 if (s == NULL)
441 return FALSE;
442 htab->sdynrelro = s;
443 }
444
3018b441 445 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
446 normally needed. We need to create it here, though, so that the
447 linker will map it to an output section. We can't just create it
448 only if we need it, because we will not know whether we need it
449 until we have seen all the input files, and the first time the
450 main linker code calls BFD after examining all the input files
451 (size_dynamic_sections) the input sections have already been
452 mapped to the output sections. If the section turns out not to
453 be needed, we can discard it later. We will never need this
454 section when generating a shared object, since they do not use
455 copy relocs. */
9d19e4fd 456 if (bfd_link_executable (info))
3018b441 457 {
14b2f831
AM
458 s = bfd_make_section_anyway_with_flags (abfd,
459 (bed->rela_plts_and_copies_p
460 ? ".rela.bss" : ".rel.bss"),
461 flags | SEC_READONLY);
3018b441 462 if (s == NULL
45d6a902 463 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 464 return FALSE;
9d19e4fd 465 htab->srelbss = s;
5474d94f
AM
466
467 if (bed->want_dynrelro)
468 {
469 s = (bfd_make_section_anyway_with_flags
470 (abfd, (bed->rela_plts_and_copies_p
471 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
472 flags | SEC_READONLY));
473 if (s == NULL
474 || ! bfd_set_section_alignment (abfd, s,
475 bed->s->log_file_align))
476 return FALSE;
477 htab->sreldynrelro = s;
478 }
3018b441 479 }
252b5132
RH
480 }
481
b34976b6 482 return TRUE;
252b5132
RH
483}
484\f
252b5132
RH
485/* Record a new dynamic symbol. We record the dynamic symbols as we
486 read the input files, since we need to have a list of all of them
487 before we can determine the final sizes of the output sections.
488 Note that we may actually call this function even though we are not
489 going to output any dynamic symbols; in some cases we know that a
490 symbol should be in the dynamic symbol table, but only if there is
491 one. */
492
b34976b6 493bfd_boolean
c152c796
AM
494bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
495 struct elf_link_hash_entry *h)
252b5132
RH
496{
497 if (h->dynindx == -1)
498 {
2b0f7ef9 499 struct elf_strtab_hash *dynstr;
68b6ddd0 500 char *p;
252b5132 501 const char *name;
ef53be89 502 size_t indx;
252b5132 503
7a13edea
NC
504 /* XXX: The ABI draft says the linker must turn hidden and
505 internal symbols into STB_LOCAL symbols when producing the
506 DSO. However, if ld.so honors st_other in the dynamic table,
507 this would not be necessary. */
508 switch (ELF_ST_VISIBILITY (h->other))
509 {
510 case STV_INTERNAL:
511 case STV_HIDDEN:
9d6eee78
L
512 if (h->root.type != bfd_link_hash_undefined
513 && h->root.type != bfd_link_hash_undefweak)
38048eb9 514 {
f5385ebf 515 h->forced_local = 1;
67687978
PB
516 if (!elf_hash_table (info)->is_relocatable_executable)
517 return TRUE;
7a13edea 518 }
0444bdd4 519
7a13edea
NC
520 default:
521 break;
522 }
523
252b5132
RH
524 h->dynindx = elf_hash_table (info)->dynsymcount;
525 ++elf_hash_table (info)->dynsymcount;
526
527 dynstr = elf_hash_table (info)->dynstr;
528 if (dynstr == NULL)
529 {
530 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 531 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 532 if (dynstr == NULL)
b34976b6 533 return FALSE;
252b5132
RH
534 }
535
536 /* We don't put any version information in the dynamic string
aad5d350 537 table. */
252b5132
RH
538 name = h->root.root.string;
539 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
540 if (p != NULL)
541 /* We know that the p points into writable memory. In fact,
542 there are only a few symbols that have read-only names, being
543 those like _GLOBAL_OFFSET_TABLE_ that are created specially
544 by the backends. Most symbols will have names pointing into
545 an ELF string table read from a file, or to objalloc memory. */
546 *p = 0;
547
548 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
549
550 if (p != NULL)
551 *p = ELF_VER_CHR;
252b5132 552
ef53be89 553 if (indx == (size_t) -1)
b34976b6 554 return FALSE;
252b5132
RH
555 h->dynstr_index = indx;
556 }
557
b34976b6 558 return TRUE;
252b5132 559}
45d6a902 560\f
55255dae
L
561/* Mark a symbol dynamic. */
562
28caa186 563static void
55255dae 564bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
565 struct elf_link_hash_entry *h,
566 Elf_Internal_Sym *sym)
55255dae 567{
40b36307 568 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 569
40b36307 570 /* It may be called more than once on the same H. */
0e1862bb 571 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
572 return;
573
40b36307
L
574 if ((info->dynamic_data
575 && (h->type == STT_OBJECT
b8871f35 576 || h->type == STT_COMMON
40b36307 577 || (sym != NULL
b8871f35
L
578 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
579 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 580 || (d != NULL
40b36307
L
581 && h->root.type == bfd_link_hash_new
582 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
583 h->dynamic = 1;
584}
585
45d6a902
AM
586/* Record an assignment to a symbol made by a linker script. We need
587 this in case some dynamic object refers to this symbol. */
588
589bfd_boolean
fe21a8fc
L
590bfd_elf_record_link_assignment (bfd *output_bfd,
591 struct bfd_link_info *info,
268b6b39 592 const char *name,
fe21a8fc
L
593 bfd_boolean provide,
594 bfd_boolean hidden)
45d6a902 595{
00cbee0a 596 struct elf_link_hash_entry *h, *hv;
4ea42fb7 597 struct elf_link_hash_table *htab;
00cbee0a 598 const struct elf_backend_data *bed;
45d6a902 599
0eddce27 600 if (!is_elf_hash_table (info->hash))
45d6a902
AM
601 return TRUE;
602
4ea42fb7
AM
603 htab = elf_hash_table (info);
604 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 605 if (h == NULL)
4ea42fb7 606 return provide;
45d6a902 607
8e2a4f11
AM
608 if (h->root.type == bfd_link_hash_warning)
609 h = (struct elf_link_hash_entry *) h->root.u.i.link;
610
0f550b3d
L
611 if (h->versioned == unknown)
612 {
613 /* Set versioned if symbol version is unknown. */
614 char *version = strrchr (name, ELF_VER_CHR);
615 if (version)
616 {
617 if (version > name && version[-1] != ELF_VER_CHR)
618 h->versioned = versioned_hidden;
619 else
620 h->versioned = versioned;
621 }
622 }
623
00cbee0a 624 switch (h->root.type)
77cfaee6 625 {
00cbee0a
L
626 case bfd_link_hash_defined:
627 case bfd_link_hash_defweak:
628 case bfd_link_hash_common:
629 break;
630 case bfd_link_hash_undefweak:
631 case bfd_link_hash_undefined:
632 /* Since we're defining the symbol, don't let it seem to have not
633 been defined. record_dynamic_symbol and size_dynamic_sections
634 may depend on this. */
4ea42fb7 635 h->root.type = bfd_link_hash_new;
77cfaee6
AM
636 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
637 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
638 break;
639 case bfd_link_hash_new:
40b36307 640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 641 h->non_elf = 0;
00cbee0a
L
642 break;
643 case bfd_link_hash_indirect:
644 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 645 the versioned symbol point to this one. */
00cbee0a
L
646 bed = get_elf_backend_data (output_bfd);
647 hv = h;
648 while (hv->root.type == bfd_link_hash_indirect
649 || hv->root.type == bfd_link_hash_warning)
650 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
651 /* We don't need to update h->root.u since linker will set them
652 later. */
653 h->root.type = bfd_link_hash_undefined;
654 hv->root.type = bfd_link_hash_indirect;
655 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
656 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
657 break;
8e2a4f11
AM
658 default:
659 BFD_FAIL ();
c2596ca5 660 return FALSE;
55255dae 661 }
45d6a902
AM
662
663 /* If this symbol is being provided by the linker script, and it is
664 currently defined by a dynamic object, but not by a regular
665 object, then mark it as undefined so that the generic linker will
666 force the correct value. */
667 if (provide
f5385ebf
AM
668 && h->def_dynamic
669 && !h->def_regular)
45d6a902
AM
670 h->root.type = bfd_link_hash_undefined;
671
672 /* If this symbol is not being provided by the linker script, and it is
673 currently defined by a dynamic object, but not by a regular object,
81ff47b3
MR
674 then undo any forced local marking that may have been set by input
675 section garbage collection and clear out any version information
676 because the symbol will not be associated with the dynamic object
677 any more. */
45d6a902 678 if (!provide
f5385ebf
AM
679 && h->def_dynamic
680 && !h->def_regular)
81ff47b3
MR
681 {
682 h->forced_local = 0;
683 h->verinfo.verdef = NULL;
684 }
45d6a902 685
f5385ebf 686 h->def_regular = 1;
45d6a902 687
eb8476a6 688 if (hidden)
fe21a8fc 689 {
91d6fa6a 690 bed = get_elf_backend_data (output_bfd);
b8297068
AM
691 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
692 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
694 }
695
6fa3860b
PB
696 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
697 and executables. */
0e1862bb 698 if (!bfd_link_relocatable (info)
6fa3860b
PB
699 && h->dynindx != -1
700 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
701 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
702 h->forced_local = 1;
703
f5385ebf
AM
704 if ((h->def_dynamic
705 || h->ref_dynamic
6b3b0ab8
L
706 || bfd_link_dll (info)
707 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
708 && h->dynindx == -1)
709 {
c152c796 710 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
711 return FALSE;
712
713 /* If this is a weak defined symbol, and we know a corresponding
714 real symbol from the same dynamic object, make sure the real
715 symbol is also made into a dynamic symbol. */
f6e332e6
AM
716 if (h->u.weakdef != NULL
717 && h->u.weakdef->dynindx == -1)
45d6a902 718 {
f6e332e6 719 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
720 return FALSE;
721 }
722 }
723
724 return TRUE;
725}
42751cf3 726
8c58d23b
AM
727/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
728 success, and 2 on a failure caused by attempting to record a symbol
729 in a discarded section, eg. a discarded link-once section symbol. */
730
731int
c152c796
AM
732bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
733 bfd *input_bfd,
734 long input_indx)
8c58d23b
AM
735{
736 bfd_size_type amt;
737 struct elf_link_local_dynamic_entry *entry;
738 struct elf_link_hash_table *eht;
739 struct elf_strtab_hash *dynstr;
ef53be89 740 size_t dynstr_index;
8c58d23b
AM
741 char *name;
742 Elf_External_Sym_Shndx eshndx;
743 char esym[sizeof (Elf64_External_Sym)];
744
0eddce27 745 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
746 return 0;
747
748 /* See if the entry exists already. */
749 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
750 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
751 return 1;
752
753 amt = sizeof (*entry);
a50b1753 754 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
755 if (entry == NULL)
756 return 0;
757
758 /* Go find the symbol, so that we can find it's name. */
759 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 760 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
761 {
762 bfd_release (input_bfd, entry);
763 return 0;
764 }
765
766 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 767 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
768 {
769 asection *s;
770
771 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
772 if (s == NULL || bfd_is_abs_section (s->output_section))
773 {
774 /* We can still bfd_release here as nothing has done another
775 bfd_alloc. We can't do this later in this function. */
776 bfd_release (input_bfd, entry);
777 return 2;
778 }
779 }
780
781 name = (bfd_elf_string_from_elf_section
782 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
783 entry->isym.st_name));
784
785 dynstr = elf_hash_table (info)->dynstr;
786 if (dynstr == NULL)
787 {
788 /* Create a strtab to hold the dynamic symbol names. */
789 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
790 if (dynstr == NULL)
791 return 0;
792 }
793
b34976b6 794 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 795 if (dynstr_index == (size_t) -1)
8c58d23b
AM
796 return 0;
797 entry->isym.st_name = dynstr_index;
798
799 eht = elf_hash_table (info);
800
801 entry->next = eht->dynlocal;
802 eht->dynlocal = entry;
803 entry->input_bfd = input_bfd;
804 entry->input_indx = input_indx;
805 eht->dynsymcount++;
806
807 /* Whatever binding the symbol had before, it's now local. */
808 entry->isym.st_info
809 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
810
811 /* The dynindx will be set at the end of size_dynamic_sections. */
812
813 return 1;
814}
815
30b30c21 816/* Return the dynindex of a local dynamic symbol. */
42751cf3 817
30b30c21 818long
268b6b39
AM
819_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 bfd *input_bfd,
821 long input_indx)
30b30c21
RH
822{
823 struct elf_link_local_dynamic_entry *e;
824
825 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
826 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
827 return e->dynindx;
828 return -1;
829}
830
831/* This function is used to renumber the dynamic symbols, if some of
832 them are removed because they are marked as local. This is called
833 via elf_link_hash_traverse. */
834
b34976b6 835static bfd_boolean
268b6b39
AM
836elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
837 void *data)
42751cf3 838{
a50b1753 839 size_t *count = (size_t *) data;
30b30c21 840
6fa3860b
PB
841 if (h->forced_local)
842 return TRUE;
843
844 if (h->dynindx != -1)
845 h->dynindx = ++(*count);
846
847 return TRUE;
848}
849
850
851/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
852 STB_LOCAL binding. */
853
854static bfd_boolean
855elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 void *data)
857{
a50b1753 858 size_t *count = (size_t *) data;
6fa3860b 859
6fa3860b
PB
860 if (!h->forced_local)
861 return TRUE;
862
42751cf3 863 if (h->dynindx != -1)
30b30c21
RH
864 h->dynindx = ++(*count);
865
b34976b6 866 return TRUE;
42751cf3 867}
30b30c21 868
aee6f5b4
AO
869/* Return true if the dynamic symbol for a given section should be
870 omitted when creating a shared library. */
871bfd_boolean
872_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
873 struct bfd_link_info *info,
874 asection *p)
875{
74541ad4 876 struct elf_link_hash_table *htab;
ca55926c 877 asection *ip;
74541ad4 878
aee6f5b4
AO
879 switch (elf_section_data (p)->this_hdr.sh_type)
880 {
881 case SHT_PROGBITS:
882 case SHT_NOBITS:
883 /* If sh_type is yet undecided, assume it could be
884 SHT_PROGBITS/SHT_NOBITS. */
885 case SHT_NULL:
74541ad4
AM
886 htab = elf_hash_table (info);
887 if (p == htab->tls_sec)
888 return FALSE;
889
890 if (htab->text_index_section != NULL)
891 return p != htab->text_index_section && p != htab->data_index_section;
892
ca55926c 893 return (htab->dynobj != NULL
3d4d4302 894 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 895 && ip->output_section == p);
aee6f5b4
AO
896
897 /* There shouldn't be section relative relocations
898 against any other section. */
899 default:
900 return TRUE;
901 }
902}
903
062e2358 904/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
905 symbol for each output section, which come first. Next come symbols
906 which have been forced to local binding. Then all of the back-end
907 allocated local dynamic syms, followed by the rest of the global
908 symbols. */
30b30c21 909
554220db
AM
910static unsigned long
911_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
912 struct bfd_link_info *info,
913 unsigned long *section_sym_count)
30b30c21
RH
914{
915 unsigned long dynsymcount = 0;
916
0e1862bb
L
917 if (bfd_link_pic (info)
918 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 919 {
aee6f5b4 920 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
921 asection *p;
922 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 923 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
924 && (p->flags & SEC_ALLOC) != 0
925 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
926 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
927 else
928 elf_section_data (p)->dynindx = 0;
30b30c21 929 }
554220db 930 *section_sym_count = dynsymcount;
30b30c21 931
6fa3860b
PB
932 elf_link_hash_traverse (elf_hash_table (info),
933 elf_link_renumber_local_hash_table_dynsyms,
934 &dynsymcount);
935
30b30c21
RH
936 if (elf_hash_table (info)->dynlocal)
937 {
938 struct elf_link_local_dynamic_entry *p;
939 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
940 p->dynindx = ++dynsymcount;
941 }
90ac2420 942 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
943
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_hash_table_dynsyms,
946 &dynsymcount);
947
d5486c43
L
948 /* There is an unused NULL entry at the head of the table which we
949 must account for in our count even if the table is empty since it
950 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 .dynamic section. */
952 dynsymcount++;
30b30c21 953
ccabcbe5
AM
954 elf_hash_table (info)->dynsymcount = dynsymcount;
955 return dynsymcount;
30b30c21 956}
252b5132 957
54ac0771
L
958/* Merge st_other field. */
959
960static void
961elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 962 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 963 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
964{
965 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
966
967 /* If st_other has a processor-specific meaning, specific
cd3416da 968 code might be needed here. */
54ac0771
L
969 if (bed->elf_backend_merge_symbol_attribute)
970 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
971 dynamic);
972
cd3416da 973 if (!dynamic)
54ac0771 974 {
cd3416da
AM
975 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
976 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 977
cd3416da
AM
978 /* Keep the most constraining visibility. Leave the remainder
979 of the st_other field to elf_backend_merge_symbol_attribute. */
980 if (symvis - 1 < hvis - 1)
981 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 982 }
b8417128
AM
983 else if (definition
984 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
985 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 986 h->protected_def = 1;
54ac0771
L
987}
988
4f3fedcf
AM
989/* This function is called when we want to merge a new symbol with an
990 existing symbol. It handles the various cases which arise when we
991 find a definition in a dynamic object, or when there is already a
992 definition in a dynamic object. The new symbol is described by
993 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
994 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
995 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
996 of an old common symbol. We set OVERRIDE if the old symbol is
997 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
998 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
999 to change. By OK to change, we mean that we shouldn't warn if the
1000 type or size does change. */
45d6a902 1001
8a56bd02 1002static bfd_boolean
268b6b39
AM
1003_bfd_elf_merge_symbol (bfd *abfd,
1004 struct bfd_link_info *info,
1005 const char *name,
1006 Elf_Internal_Sym *sym,
1007 asection **psec,
1008 bfd_vma *pvalue,
4f3fedcf
AM
1009 struct elf_link_hash_entry **sym_hash,
1010 bfd **poldbfd,
37a9e49a 1011 bfd_boolean *pold_weak,
af44c138 1012 unsigned int *pold_alignment,
268b6b39
AM
1013 bfd_boolean *skip,
1014 bfd_boolean *override,
1015 bfd_boolean *type_change_ok,
6e33951e
L
1016 bfd_boolean *size_change_ok,
1017 bfd_boolean *matched)
252b5132 1018{
7479dfd4 1019 asection *sec, *oldsec;
45d6a902 1020 struct elf_link_hash_entry *h;
90c984fc 1021 struct elf_link_hash_entry *hi;
45d6a902
AM
1022 struct elf_link_hash_entry *flip;
1023 int bind;
1024 bfd *oldbfd;
1025 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1026 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1027 const struct elf_backend_data *bed;
6e33951e 1028 char *new_version;
45d6a902
AM
1029
1030 *skip = FALSE;
1031 *override = FALSE;
1032
1033 sec = *psec;
1034 bind = ELF_ST_BIND (sym->st_info);
1035
1036 if (! bfd_is_und_section (sec))
1037 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1038 else
1039 h = ((struct elf_link_hash_entry *)
1040 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1041 if (h == NULL)
1042 return FALSE;
1043 *sym_hash = h;
252b5132 1044
88ba32a0
L
1045 bed = get_elf_backend_data (abfd);
1046
6e33951e 1047 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1048 if (h->versioned != unversioned)
6e33951e 1049 {
422f1182
L
1050 /* Symbol version is unknown or versioned. */
1051 new_version = strrchr (name, ELF_VER_CHR);
1052 if (new_version)
1053 {
1054 if (h->versioned == unknown)
1055 {
1056 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1057 h->versioned = versioned_hidden;
1058 else
1059 h->versioned = versioned;
1060 }
1061 new_version += 1;
1062 if (new_version[0] == '\0')
1063 new_version = NULL;
1064 }
1065 else
1066 h->versioned = unversioned;
6e33951e 1067 }
422f1182
L
1068 else
1069 new_version = NULL;
6e33951e 1070
90c984fc
L
1071 /* For merging, we only care about real symbols. But we need to make
1072 sure that indirect symbol dynamic flags are updated. */
1073 hi = h;
45d6a902
AM
1074 while (h->root.type == bfd_link_hash_indirect
1075 || h->root.type == bfd_link_hash_warning)
1076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077
6e33951e
L
1078 if (!*matched)
1079 {
1080 if (hi == h || h->root.type == bfd_link_hash_new)
1081 *matched = TRUE;
1082 else
1083 {
ae7683d2 1084 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1085 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1086 true if the new symbol is only visible to the symbol with
6e33951e 1087 the same symbol version. */
422f1182
L
1088 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1089 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1090 if (!old_hidden && !new_hidden)
1091 /* The new symbol matches the existing symbol if both
1092 aren't hidden. */
1093 *matched = TRUE;
1094 else
1095 {
1096 /* OLD_VERSION is the symbol version of the existing
1097 symbol. */
422f1182
L
1098 char *old_version;
1099
1100 if (h->versioned >= versioned)
1101 old_version = strrchr (h->root.root.string,
1102 ELF_VER_CHR) + 1;
1103 else
1104 old_version = NULL;
6e33951e
L
1105
1106 /* The new symbol matches the existing symbol if they
1107 have the same symbol version. */
1108 *matched = (old_version == new_version
1109 || (old_version != NULL
1110 && new_version != NULL
1111 && strcmp (old_version, new_version) == 0));
1112 }
1113 }
1114 }
1115
934bce08
AM
1116 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1117 existing symbol. */
1118
1119 oldbfd = NULL;
1120 oldsec = NULL;
1121 switch (h->root.type)
1122 {
1123 default:
1124 break;
1125
1126 case bfd_link_hash_undefined:
1127 case bfd_link_hash_undefweak:
1128 oldbfd = h->root.u.undef.abfd;
1129 break;
1130
1131 case bfd_link_hash_defined:
1132 case bfd_link_hash_defweak:
1133 oldbfd = h->root.u.def.section->owner;
1134 oldsec = h->root.u.def.section;
1135 break;
1136
1137 case bfd_link_hash_common:
1138 oldbfd = h->root.u.c.p->section->owner;
1139 oldsec = h->root.u.c.p->section;
1140 if (pold_alignment)
1141 *pold_alignment = h->root.u.c.p->alignment_power;
1142 break;
1143 }
1144 if (poldbfd && *poldbfd == NULL)
1145 *poldbfd = oldbfd;
1146
1147 /* Differentiate strong and weak symbols. */
1148 newweak = bind == STB_WEAK;
1149 oldweak = (h->root.type == bfd_link_hash_defweak
1150 || h->root.type == bfd_link_hash_undefweak);
1151 if (pold_weak)
1152 *pold_weak = oldweak;
1153
1154 /* This code is for coping with dynamic objects, and is only useful
1155 if we are doing an ELF link. */
1156 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1157 return TRUE;
1158
40b36307 1159 /* We have to check it for every instance since the first few may be
ee659f1f 1160 references and not all compilers emit symbol type for undefined
40b36307
L
1161 symbols. */
1162 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1163
ee659f1f
AM
1164 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1165 respectively, is from a dynamic object. */
1166
1167 newdyn = (abfd->flags & DYNAMIC) != 0;
1168
1169 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1170 syms and defined syms in dynamic libraries respectively.
1171 ref_dynamic on the other hand can be set for a symbol defined in
1172 a dynamic library, and def_dynamic may not be set; When the
1173 definition in a dynamic lib is overridden by a definition in the
1174 executable use of the symbol in the dynamic lib becomes a
1175 reference to the executable symbol. */
1176 if (newdyn)
1177 {
1178 if (bfd_is_und_section (sec))
1179 {
1180 if (bind != STB_WEAK)
1181 {
1182 h->ref_dynamic_nonweak = 1;
1183 hi->ref_dynamic_nonweak = 1;
1184 }
1185 }
1186 else
1187 {
6e33951e
L
1188 /* Update the existing symbol only if they match. */
1189 if (*matched)
1190 h->dynamic_def = 1;
ee659f1f
AM
1191 hi->dynamic_def = 1;
1192 }
1193 }
1194
45d6a902
AM
1195 /* If we just created the symbol, mark it as being an ELF symbol.
1196 Other than that, there is nothing to do--there is no merge issue
1197 with a newly defined symbol--so we just return. */
1198
1199 if (h->root.type == bfd_link_hash_new)
252b5132 1200 {
f5385ebf 1201 h->non_elf = 0;
45d6a902
AM
1202 return TRUE;
1203 }
252b5132 1204
45d6a902
AM
1205 /* In cases involving weak versioned symbols, we may wind up trying
1206 to merge a symbol with itself. Catch that here, to avoid the
1207 confusion that results if we try to override a symbol with
1208 itself. The additional tests catch cases like
1209 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1210 dynamic object, which we do want to handle here. */
1211 if (abfd == oldbfd
895fa45f 1212 && (newweak || oldweak)
45d6a902 1213 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1214 || !h->def_regular))
45d6a902
AM
1215 return TRUE;
1216
707bba77 1217 olddyn = FALSE;
45d6a902
AM
1218 if (oldbfd != NULL)
1219 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1220 else if (oldsec != NULL)
45d6a902 1221 {
707bba77 1222 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1223 indices used by MIPS ELF. */
707bba77 1224 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1225 }
252b5132 1226
45d6a902
AM
1227 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1228 respectively, appear to be a definition rather than reference. */
1229
707bba77 1230 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1231
707bba77
AM
1232 olddef = (h->root.type != bfd_link_hash_undefined
1233 && h->root.type != bfd_link_hash_undefweak
202ac193 1234 && h->root.type != bfd_link_hash_common);
45d6a902 1235
0a36a439
L
1236 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1237 respectively, appear to be a function. */
1238
1239 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1240 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1241
1242 oldfunc = (h->type != STT_NOTYPE
1243 && bed->is_function_type (h->type));
1244
5b677558
AM
1245 /* If creating a default indirect symbol ("foo" or "foo@") from a
1246 dynamic versioned definition ("foo@@") skip doing so if there is
1247 an existing regular definition with a different type. We don't
1248 want, for example, a "time" variable in the executable overriding
1249 a "time" function in a shared library. */
580a2b6e 1250 if (pold_alignment == NULL
580a2b6e
L
1251 && newdyn
1252 && newdef
1253 && !olddyn
5b677558
AM
1254 && (olddef || h->root.type == bfd_link_hash_common)
1255 && ELF_ST_TYPE (sym->st_info) != h->type
1256 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1257 && h->type != STT_NOTYPE
1258 && !(newfunc && oldfunc))
580a2b6e
L
1259 {
1260 *skip = TRUE;
1261 return TRUE;
1262 }
1263
4c34aff8
AM
1264 /* Check TLS symbols. We don't check undefined symbols introduced
1265 by "ld -u" which have no type (and oldbfd NULL), and we don't
1266 check symbols from plugins because they also have no type. */
1267 if (oldbfd != NULL
1268 && (oldbfd->flags & BFD_PLUGIN) == 0
1269 && (abfd->flags & BFD_PLUGIN) == 0
1270 && ELF_ST_TYPE (sym->st_info) != h->type
1271 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1272 {
1273 bfd *ntbfd, *tbfd;
1274 bfd_boolean ntdef, tdef;
1275 asection *ntsec, *tsec;
1276
1277 if (h->type == STT_TLS)
1278 {
3b36f7e6 1279 ntbfd = abfd;
7479dfd4
L
1280 ntsec = sec;
1281 ntdef = newdef;
1282 tbfd = oldbfd;
1283 tsec = oldsec;
1284 tdef = olddef;
1285 }
1286 else
1287 {
1288 ntbfd = oldbfd;
1289 ntsec = oldsec;
1290 ntdef = olddef;
1291 tbfd = abfd;
1292 tsec = sec;
1293 tdef = newdef;
1294 }
1295
1296 if (tdef && ntdef)
4eca0228 1297 _bfd_error_handler
695344c0 1298 /* xgettext:c-format */
191c0c42
AM
1299 (_("%s: TLS definition in %B section %A "
1300 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1301 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1302 else if (!tdef && !ntdef)
4eca0228 1303 _bfd_error_handler
695344c0 1304 /* xgettext:c-format */
191c0c42
AM
1305 (_("%s: TLS reference in %B "
1306 "mismatches non-TLS reference in %B"),
7479dfd4
L
1307 tbfd, ntbfd, h->root.root.string);
1308 else if (tdef)
4eca0228 1309 _bfd_error_handler
695344c0 1310 /* xgettext:c-format */
191c0c42
AM
1311 (_("%s: TLS definition in %B section %A "
1312 "mismatches non-TLS reference in %B"),
7479dfd4
L
1313 tbfd, tsec, ntbfd, h->root.root.string);
1314 else
4eca0228 1315 _bfd_error_handler
695344c0 1316 /* xgettext:c-format */
191c0c42
AM
1317 (_("%s: TLS reference in %B "
1318 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1319 tbfd, ntbfd, ntsec, h->root.root.string);
1320
1321 bfd_set_error (bfd_error_bad_value);
1322 return FALSE;
1323 }
1324
45d6a902
AM
1325 /* If the old symbol has non-default visibility, we ignore the new
1326 definition from a dynamic object. */
1327 if (newdyn
9c7a29a3 1328 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1329 && !bfd_is_und_section (sec))
1330 {
1331 *skip = TRUE;
1332 /* Make sure this symbol is dynamic. */
f5385ebf 1333 h->ref_dynamic = 1;
90c984fc 1334 hi->ref_dynamic = 1;
45d6a902
AM
1335 /* A protected symbol has external availability. Make sure it is
1336 recorded as dynamic.
1337
1338 FIXME: Should we check type and size for protected symbol? */
1339 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1340 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1341 else
1342 return TRUE;
1343 }
1344 else if (!newdyn
9c7a29a3 1345 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1346 && h->def_dynamic)
45d6a902
AM
1347 {
1348 /* If the new symbol with non-default visibility comes from a
1349 relocatable file and the old definition comes from a dynamic
1350 object, we remove the old definition. */
6c9b78e6 1351 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1352 {
1353 /* Handle the case where the old dynamic definition is
1354 default versioned. We need to copy the symbol info from
1355 the symbol with default version to the normal one if it
1356 was referenced before. */
1357 if (h->ref_regular)
1358 {
6c9b78e6 1359 hi->root.type = h->root.type;
d2dee3b2 1360 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1361 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1362
6c9b78e6 1363 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1364 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1365 {
aed81c4e
MR
1366 /* If the new symbol is hidden or internal, completely undo
1367 any dynamic link state. */
1368 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1369 h->forced_local = 0;
1370 h->ref_dynamic = 0;
d2dee3b2
L
1371 }
1372 else
aed81c4e
MR
1373 h->ref_dynamic = 1;
1374
1375 h->def_dynamic = 0;
aed81c4e
MR
1376 /* FIXME: Should we check type and size for protected symbol? */
1377 h->size = 0;
1378 h->type = 0;
1379
6c9b78e6 1380 h = hi;
d2dee3b2
L
1381 }
1382 else
6c9b78e6 1383 h = hi;
d2dee3b2 1384 }
1de1a317 1385
f5eda473
AM
1386 /* If the old symbol was undefined before, then it will still be
1387 on the undefs list. If the new symbol is undefined or
1388 common, we can't make it bfd_link_hash_new here, because new
1389 undefined or common symbols will be added to the undefs list
1390 by _bfd_generic_link_add_one_symbol. Symbols may not be
1391 added twice to the undefs list. Also, if the new symbol is
1392 undefweak then we don't want to lose the strong undef. */
1393 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1394 {
1de1a317 1395 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1396 h->root.u.undef.abfd = abfd;
1397 }
1398 else
1399 {
1400 h->root.type = bfd_link_hash_new;
1401 h->root.u.undef.abfd = NULL;
1402 }
1403
f5eda473 1404 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1405 {
f5eda473
AM
1406 /* If the new symbol is hidden or internal, completely undo
1407 any dynamic link state. */
1408 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1409 h->forced_local = 0;
1410 h->ref_dynamic = 0;
45d6a902 1411 }
f5eda473
AM
1412 else
1413 h->ref_dynamic = 1;
1414 h->def_dynamic = 0;
45d6a902
AM
1415 /* FIXME: Should we check type and size for protected symbol? */
1416 h->size = 0;
1417 h->type = 0;
1418 return TRUE;
1419 }
14a793b2 1420
15b43f48
AM
1421 /* If a new weak symbol definition comes from a regular file and the
1422 old symbol comes from a dynamic library, we treat the new one as
1423 strong. Similarly, an old weak symbol definition from a regular
1424 file is treated as strong when the new symbol comes from a dynamic
1425 library. Further, an old weak symbol from a dynamic library is
1426 treated as strong if the new symbol is from a dynamic library.
1427 This reflects the way glibc's ld.so works.
1428
1429 Do this before setting *type_change_ok or *size_change_ok so that
1430 we warn properly when dynamic library symbols are overridden. */
1431
1432 if (newdef && !newdyn && olddyn)
0f8a2703 1433 newweak = FALSE;
15b43f48 1434 if (olddef && newdyn)
0f8a2703
AM
1435 oldweak = FALSE;
1436
d334575b 1437 /* Allow changes between different types of function symbol. */
0a36a439 1438 if (newfunc && oldfunc)
fcb93ecf
PB
1439 *type_change_ok = TRUE;
1440
79349b09
AM
1441 /* It's OK to change the type if either the existing symbol or the
1442 new symbol is weak. A type change is also OK if the old symbol
1443 is undefined and the new symbol is defined. */
252b5132 1444
79349b09
AM
1445 if (oldweak
1446 || newweak
1447 || (newdef
1448 && h->root.type == bfd_link_hash_undefined))
1449 *type_change_ok = TRUE;
1450
1451 /* It's OK to change the size if either the existing symbol or the
1452 new symbol is weak, or if the old symbol is undefined. */
1453
1454 if (*type_change_ok
1455 || h->root.type == bfd_link_hash_undefined)
1456 *size_change_ok = TRUE;
45d6a902 1457
45d6a902
AM
1458 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1459 symbol, respectively, appears to be a common symbol in a dynamic
1460 object. If a symbol appears in an uninitialized section, and is
1461 not weak, and is not a function, then it may be a common symbol
1462 which was resolved when the dynamic object was created. We want
1463 to treat such symbols specially, because they raise special
1464 considerations when setting the symbol size: if the symbol
1465 appears as a common symbol in a regular object, and the size in
1466 the regular object is larger, we must make sure that we use the
1467 larger size. This problematic case can always be avoided in C,
1468 but it must be handled correctly when using Fortran shared
1469 libraries.
1470
1471 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1472 likewise for OLDDYNCOMMON and OLDDEF.
1473
1474 Note that this test is just a heuristic, and that it is quite
1475 possible to have an uninitialized symbol in a shared object which
1476 is really a definition, rather than a common symbol. This could
1477 lead to some minor confusion when the symbol really is a common
1478 symbol in some regular object. However, I think it will be
1479 harmless. */
1480
1481 if (newdyn
1482 && newdef
79349b09 1483 && !newweak
45d6a902
AM
1484 && (sec->flags & SEC_ALLOC) != 0
1485 && (sec->flags & SEC_LOAD) == 0
1486 && sym->st_size > 0
0a36a439 1487 && !newfunc)
45d6a902
AM
1488 newdyncommon = TRUE;
1489 else
1490 newdyncommon = FALSE;
1491
1492 if (olddyn
1493 && olddef
1494 && h->root.type == bfd_link_hash_defined
f5385ebf 1495 && h->def_dynamic
45d6a902
AM
1496 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1497 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1498 && h->size > 0
0a36a439 1499 && !oldfunc)
45d6a902
AM
1500 olddyncommon = TRUE;
1501 else
1502 olddyncommon = FALSE;
1503
a4d8e49b
L
1504 /* We now know everything about the old and new symbols. We ask the
1505 backend to check if we can merge them. */
5d13b3b3
AM
1506 if (bed->merge_symbol != NULL)
1507 {
1508 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1509 return FALSE;
1510 sec = *psec;
1511 }
a4d8e49b 1512
45d6a902
AM
1513 /* If both the old and the new symbols look like common symbols in a
1514 dynamic object, set the size of the symbol to the larger of the
1515 two. */
1516
1517 if (olddyncommon
1518 && newdyncommon
1519 && sym->st_size != h->size)
1520 {
1521 /* Since we think we have two common symbols, issue a multiple
1522 common warning if desired. Note that we only warn if the
1523 size is different. If the size is the same, we simply let
1524 the old symbol override the new one as normally happens with
1525 symbols defined in dynamic objects. */
1526
1a72702b
AM
1527 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1528 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1529 if (sym->st_size > h->size)
1530 h->size = sym->st_size;
252b5132 1531
45d6a902 1532 *size_change_ok = TRUE;
252b5132
RH
1533 }
1534
45d6a902
AM
1535 /* If we are looking at a dynamic object, and we have found a
1536 definition, we need to see if the symbol was already defined by
1537 some other object. If so, we want to use the existing
1538 definition, and we do not want to report a multiple symbol
1539 definition error; we do this by clobbering *PSEC to be
1540 bfd_und_section_ptr.
1541
1542 We treat a common symbol as a definition if the symbol in the
1543 shared library is a function, since common symbols always
1544 represent variables; this can cause confusion in principle, but
1545 any such confusion would seem to indicate an erroneous program or
1546 shared library. We also permit a common symbol in a regular
202ac193
L
1547 object to override a weak symbol in a shared object. A common
1548 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1549
1550 if (newdyn
1551 && newdef
77cfaee6 1552 && (olddef
45d6a902 1553 || (h->root.type == bfd_link_hash_common
202ac193
L
1554 && (newweak
1555 || newfunc
1556 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1557 {
1558 *override = TRUE;
1559 newdef = FALSE;
1560 newdyncommon = FALSE;
252b5132 1561
45d6a902
AM
1562 *psec = sec = bfd_und_section_ptr;
1563 *size_change_ok = TRUE;
252b5132 1564
45d6a902
AM
1565 /* If we get here when the old symbol is a common symbol, then
1566 we are explicitly letting it override a weak symbol or
1567 function in a dynamic object, and we don't want to warn about
1568 a type change. If the old symbol is a defined symbol, a type
1569 change warning may still be appropriate. */
252b5132 1570
45d6a902
AM
1571 if (h->root.type == bfd_link_hash_common)
1572 *type_change_ok = TRUE;
1573 }
1574
1575 /* Handle the special case of an old common symbol merging with a
1576 new symbol which looks like a common symbol in a shared object.
1577 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1578 common symbol, and let _bfd_generic_link_add_one_symbol do the
1579 right thing. */
45d6a902
AM
1580
1581 if (newdyncommon
1582 && h->root.type == bfd_link_hash_common)
1583 {
1584 *override = TRUE;
1585 newdef = FALSE;
1586 newdyncommon = FALSE;
1587 *pvalue = sym->st_size;
a4d8e49b 1588 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1589 *size_change_ok = TRUE;
1590 }
1591
c5e2cead 1592 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1593 if (newdef && olddef && newweak)
54ac0771 1594 {
35ed3f94 1595 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1596 if (!(oldbfd != NULL
1597 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1598 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1599 {
1600 newdef = FALSE;
1601 *skip = TRUE;
1602 }
54ac0771
L
1603
1604 /* Merge st_other. If the symbol already has a dynamic index,
1605 but visibility says it should not be visible, turn it into a
1606 local symbol. */
b8417128 1607 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1608 if (h->dynindx != -1)
1609 switch (ELF_ST_VISIBILITY (h->other))
1610 {
1611 case STV_INTERNAL:
1612 case STV_HIDDEN:
1613 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1614 break;
1615 }
1616 }
c5e2cead 1617
45d6a902
AM
1618 /* If the old symbol is from a dynamic object, and the new symbol is
1619 a definition which is not from a dynamic object, then the new
1620 symbol overrides the old symbol. Symbols from regular files
1621 always take precedence over symbols from dynamic objects, even if
1622 they are defined after the dynamic object in the link.
1623
1624 As above, we again permit a common symbol in a regular object to
1625 override a definition in a shared object if the shared object
0f8a2703 1626 symbol is a function or is weak. */
45d6a902
AM
1627
1628 flip = NULL;
77cfaee6 1629 if (!newdyn
45d6a902
AM
1630 && (newdef
1631 || (bfd_is_com_section (sec)
0a36a439 1632 && (oldweak || oldfunc)))
45d6a902
AM
1633 && olddyn
1634 && olddef
f5385ebf 1635 && h->def_dynamic)
45d6a902
AM
1636 {
1637 /* Change the hash table entry to undefined, and let
1638 _bfd_generic_link_add_one_symbol do the right thing with the
1639 new definition. */
1640
1641 h->root.type = bfd_link_hash_undefined;
1642 h->root.u.undef.abfd = h->root.u.def.section->owner;
1643 *size_change_ok = TRUE;
1644
1645 olddef = FALSE;
1646 olddyncommon = FALSE;
1647
1648 /* We again permit a type change when a common symbol may be
1649 overriding a function. */
1650
1651 if (bfd_is_com_section (sec))
0a36a439
L
1652 {
1653 if (oldfunc)
1654 {
1655 /* If a common symbol overrides a function, make sure
1656 that it isn't defined dynamically nor has type
1657 function. */
1658 h->def_dynamic = 0;
1659 h->type = STT_NOTYPE;
1660 }
1661 *type_change_ok = TRUE;
1662 }
45d6a902 1663
6c9b78e6
AM
1664 if (hi->root.type == bfd_link_hash_indirect)
1665 flip = hi;
45d6a902
AM
1666 else
1667 /* This union may have been set to be non-NULL when this symbol
1668 was seen in a dynamic object. We must force the union to be
1669 NULL, so that it is correct for a regular symbol. */
1670 h->verinfo.vertree = NULL;
1671 }
1672
1673 /* Handle the special case of a new common symbol merging with an
1674 old symbol that looks like it might be a common symbol defined in
1675 a shared object. Note that we have already handled the case in
1676 which a new common symbol should simply override the definition
1677 in the shared library. */
1678
1679 if (! newdyn
1680 && bfd_is_com_section (sec)
1681 && olddyncommon)
1682 {
1683 /* It would be best if we could set the hash table entry to a
1684 common symbol, but we don't know what to use for the section
1685 or the alignment. */
1a72702b
AM
1686 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1687 bfd_link_hash_common, sym->st_size);
45d6a902 1688
4cc11e76 1689 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1690 larger, pretend that the new symbol has its size. */
1691
1692 if (h->size > *pvalue)
1693 *pvalue = h->size;
1694
af44c138
L
1695 /* We need to remember the alignment required by the symbol
1696 in the dynamic object. */
1697 BFD_ASSERT (pold_alignment);
1698 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1699
1700 olddef = FALSE;
1701 olddyncommon = FALSE;
1702
1703 h->root.type = bfd_link_hash_undefined;
1704 h->root.u.undef.abfd = h->root.u.def.section->owner;
1705
1706 *size_change_ok = TRUE;
1707 *type_change_ok = TRUE;
1708
6c9b78e6
AM
1709 if (hi->root.type == bfd_link_hash_indirect)
1710 flip = hi;
45d6a902
AM
1711 else
1712 h->verinfo.vertree = NULL;
1713 }
1714
1715 if (flip != NULL)
1716 {
1717 /* Handle the case where we had a versioned symbol in a dynamic
1718 library and now find a definition in a normal object. In this
1719 case, we make the versioned symbol point to the normal one. */
45d6a902 1720 flip->root.type = h->root.type;
00cbee0a 1721 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1722 h->root.type = bfd_link_hash_indirect;
1723 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1724 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1725 if (h->def_dynamic)
45d6a902 1726 {
f5385ebf
AM
1727 h->def_dynamic = 0;
1728 flip->ref_dynamic = 1;
45d6a902
AM
1729 }
1730 }
1731
45d6a902
AM
1732 return TRUE;
1733}
1734
1735/* This function is called to create an indirect symbol from the
1736 default for the symbol with the default version if needed. The
4f3fedcf 1737 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1738 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1739
28caa186 1740static bfd_boolean
268b6b39
AM
1741_bfd_elf_add_default_symbol (bfd *abfd,
1742 struct bfd_link_info *info,
1743 struct elf_link_hash_entry *h,
1744 const char *name,
1745 Elf_Internal_Sym *sym,
4f3fedcf
AM
1746 asection *sec,
1747 bfd_vma value,
1748 bfd **poldbfd,
e3c9d234 1749 bfd_boolean *dynsym)
45d6a902
AM
1750{
1751 bfd_boolean type_change_ok;
1752 bfd_boolean size_change_ok;
1753 bfd_boolean skip;
1754 char *shortname;
1755 struct elf_link_hash_entry *hi;
1756 struct bfd_link_hash_entry *bh;
9c5bfbb7 1757 const struct elf_backend_data *bed;
45d6a902
AM
1758 bfd_boolean collect;
1759 bfd_boolean dynamic;
e3c9d234 1760 bfd_boolean override;
45d6a902
AM
1761 char *p;
1762 size_t len, shortlen;
ffd65175 1763 asection *tmp_sec;
6e33951e 1764 bfd_boolean matched;
45d6a902 1765
422f1182
L
1766 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1767 return TRUE;
1768
45d6a902
AM
1769 /* If this symbol has a version, and it is the default version, we
1770 create an indirect symbol from the default name to the fully
1771 decorated name. This will cause external references which do not
1772 specify a version to be bound to this version of the symbol. */
1773 p = strchr (name, ELF_VER_CHR);
422f1182
L
1774 if (h->versioned == unknown)
1775 {
1776 if (p == NULL)
1777 {
1778 h->versioned = unversioned;
1779 return TRUE;
1780 }
1781 else
1782 {
1783 if (p[1] != ELF_VER_CHR)
1784 {
1785 h->versioned = versioned_hidden;
1786 return TRUE;
1787 }
1788 else
1789 h->versioned = versioned;
1790 }
1791 }
4373f8af
L
1792 else
1793 {
1794 /* PR ld/19073: We may see an unversioned definition after the
1795 default version. */
1796 if (p == NULL)
1797 return TRUE;
1798 }
45d6a902 1799
45d6a902
AM
1800 bed = get_elf_backend_data (abfd);
1801 collect = bed->collect;
1802 dynamic = (abfd->flags & DYNAMIC) != 0;
1803
1804 shortlen = p - name;
a50b1753 1805 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1806 if (shortname == NULL)
1807 return FALSE;
1808 memcpy (shortname, name, shortlen);
1809 shortname[shortlen] = '\0';
1810
1811 /* We are going to create a new symbol. Merge it with any existing
1812 symbol with this name. For the purposes of the merge, act as
1813 though we were defining the symbol we just defined, although we
1814 actually going to define an indirect symbol. */
1815 type_change_ok = FALSE;
1816 size_change_ok = FALSE;
6e33951e 1817 matched = TRUE;
ffd65175
AM
1818 tmp_sec = sec;
1819 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1820 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1821 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1822 return FALSE;
1823
1824 if (skip)
1825 goto nondefault;
1826
5b677558
AM
1827 if (hi->def_regular)
1828 {
1829 /* If the undecorated symbol will have a version added by a
1830 script different to H, then don't indirect to/from the
1831 undecorated symbol. This isn't ideal because we may not yet
1832 have seen symbol versions, if given by a script on the
1833 command line rather than via --version-script. */
1834 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1835 {
1836 bfd_boolean hide;
1837
1838 hi->verinfo.vertree
1839 = bfd_find_version_for_sym (info->version_info,
1840 hi->root.root.string, &hide);
1841 if (hi->verinfo.vertree != NULL && hide)
1842 {
1843 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1844 goto nondefault;
1845 }
1846 }
1847 if (hi->verinfo.vertree != NULL
1848 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1849 goto nondefault;
1850 }
1851
45d6a902
AM
1852 if (! override)
1853 {
c6e8a9a8 1854 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1855 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1856 {
1857 bh = &hi->root;
1858 if (! (_bfd_generic_link_add_one_symbol
1859 (info, abfd, shortname, BSF_INDIRECT,
1860 bfd_ind_section_ptr,
1861 0, name, FALSE, collect, &bh)))
1862 return FALSE;
1863 hi = (struct elf_link_hash_entry *) bh;
1864 }
45d6a902
AM
1865 }
1866 else
1867 {
1868 /* In this case the symbol named SHORTNAME is overriding the
1869 indirect symbol we want to add. We were planning on making
1870 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1871 is the name without a version. NAME is the fully versioned
1872 name, and it is the default version.
1873
1874 Overriding means that we already saw a definition for the
1875 symbol SHORTNAME in a regular object, and it is overriding
1876 the symbol defined in the dynamic object.
1877
1878 When this happens, we actually want to change NAME, the
1879 symbol we just added, to refer to SHORTNAME. This will cause
1880 references to NAME in the shared object to become references
1881 to SHORTNAME in the regular object. This is what we expect
1882 when we override a function in a shared object: that the
1883 references in the shared object will be mapped to the
1884 definition in the regular object. */
1885
1886 while (hi->root.type == bfd_link_hash_indirect
1887 || hi->root.type == bfd_link_hash_warning)
1888 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1889
1890 h->root.type = bfd_link_hash_indirect;
1891 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1892 if (h->def_dynamic)
45d6a902 1893 {
f5385ebf
AM
1894 h->def_dynamic = 0;
1895 hi->ref_dynamic = 1;
1896 if (hi->ref_regular
1897 || hi->def_regular)
45d6a902 1898 {
c152c796 1899 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1900 return FALSE;
1901 }
1902 }
1903
1904 /* Now set HI to H, so that the following code will set the
1905 other fields correctly. */
1906 hi = h;
1907 }
1908
fab4a87f
L
1909 /* Check if HI is a warning symbol. */
1910 if (hi->root.type == bfd_link_hash_warning)
1911 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1912
45d6a902
AM
1913 /* If there is a duplicate definition somewhere, then HI may not
1914 point to an indirect symbol. We will have reported an error to
1915 the user in that case. */
1916
1917 if (hi->root.type == bfd_link_hash_indirect)
1918 {
1919 struct elf_link_hash_entry *ht;
1920
45d6a902 1921 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1922 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1923
68c88cd4
AM
1924 /* A reference to the SHORTNAME symbol from a dynamic library
1925 will be satisfied by the versioned symbol at runtime. In
1926 effect, we have a reference to the versioned symbol. */
1927 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1928 hi->dynamic_def |= ht->dynamic_def;
1929
45d6a902
AM
1930 /* See if the new flags lead us to realize that the symbol must
1931 be dynamic. */
1932 if (! *dynsym)
1933 {
1934 if (! dynamic)
1935 {
0e1862bb 1936 if (! bfd_link_executable (info)
90c984fc 1937 || hi->def_dynamic
f5385ebf 1938 || hi->ref_dynamic)
45d6a902
AM
1939 *dynsym = TRUE;
1940 }
1941 else
1942 {
f5385ebf 1943 if (hi->ref_regular)
45d6a902
AM
1944 *dynsym = TRUE;
1945 }
1946 }
1947 }
1948
1949 /* We also need to define an indirection from the nondefault version
1950 of the symbol. */
1951
1952nondefault:
1953 len = strlen (name);
a50b1753 1954 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1955 if (shortname == NULL)
1956 return FALSE;
1957 memcpy (shortname, name, shortlen);
1958 memcpy (shortname + shortlen, p + 1, len - shortlen);
1959
1960 /* Once again, merge with any existing symbol. */
1961 type_change_ok = FALSE;
1962 size_change_ok = FALSE;
ffd65175
AM
1963 tmp_sec = sec;
1964 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1965 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1966 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1967 return FALSE;
1968
1969 if (skip)
1970 return TRUE;
1971
1972 if (override)
1973 {
1974 /* Here SHORTNAME is a versioned name, so we don't expect to see
1975 the type of override we do in the case above unless it is
4cc11e76 1976 overridden by a versioned definition. */
45d6a902
AM
1977 if (hi->root.type != bfd_link_hash_defined
1978 && hi->root.type != bfd_link_hash_defweak)
4eca0228 1979 _bfd_error_handler
695344c0 1980 /* xgettext:c-format */
d003868e
AM
1981 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1982 abfd, shortname);
45d6a902
AM
1983 }
1984 else
1985 {
1986 bh = &hi->root;
1987 if (! (_bfd_generic_link_add_one_symbol
1988 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1989 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1990 return FALSE;
1991 hi = (struct elf_link_hash_entry *) bh;
1992
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error
1995 to the user in that case. */
1996
1997 if (hi->root.type == bfd_link_hash_indirect)
1998 {
fcfa13d2 1999 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2000 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2001 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2002
2003 /* See if the new flags lead us to realize that the symbol
2004 must be dynamic. */
2005 if (! *dynsym)
2006 {
2007 if (! dynamic)
2008 {
0e1862bb 2009 if (! bfd_link_executable (info)
f5385ebf 2010 || hi->ref_dynamic)
45d6a902
AM
2011 *dynsym = TRUE;
2012 }
2013 else
2014 {
f5385ebf 2015 if (hi->ref_regular)
45d6a902
AM
2016 *dynsym = TRUE;
2017 }
2018 }
2019 }
2020 }
2021
2022 return TRUE;
2023}
2024\f
2025/* This routine is used to export all defined symbols into the dynamic
2026 symbol table. It is called via elf_link_hash_traverse. */
2027
28caa186 2028static bfd_boolean
268b6b39 2029_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2030{
a50b1753 2031 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2032
2033 /* Ignore indirect symbols. These are added by the versioning code. */
2034 if (h->root.type == bfd_link_hash_indirect)
2035 return TRUE;
2036
7686d77d
AM
2037 /* Ignore this if we won't export it. */
2038 if (!eif->info->export_dynamic && !h->dynamic)
2039 return TRUE;
45d6a902
AM
2040
2041 if (h->dynindx == -1
fd91d419
L
2042 && (h->def_regular || h->ref_regular)
2043 && ! bfd_hide_sym_by_version (eif->info->version_info,
2044 h->root.root.string))
45d6a902 2045 {
fd91d419 2046 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2047 {
fd91d419
L
2048 eif->failed = TRUE;
2049 return FALSE;
45d6a902
AM
2050 }
2051 }
2052
2053 return TRUE;
2054}
2055\f
2056/* Look through the symbols which are defined in other shared
2057 libraries and referenced here. Update the list of version
2058 dependencies. This will be put into the .gnu.version_r section.
2059 This function is called via elf_link_hash_traverse. */
2060
28caa186 2061static bfd_boolean
268b6b39
AM
2062_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2063 void *data)
45d6a902 2064{
a50b1753 2065 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2066 Elf_Internal_Verneed *t;
2067 Elf_Internal_Vernaux *a;
2068 bfd_size_type amt;
2069
45d6a902
AM
2070 /* We only care about symbols defined in shared objects with version
2071 information. */
f5385ebf
AM
2072 if (!h->def_dynamic
2073 || h->def_regular
45d6a902 2074 || h->dynindx == -1
7b20f099
AM
2075 || h->verinfo.verdef == NULL
2076 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2077 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2078 return TRUE;
2079
2080 /* See if we already know about this version. */
28caa186
AM
2081 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2082 t != NULL;
2083 t = t->vn_nextref)
45d6a902
AM
2084 {
2085 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2086 continue;
2087
2088 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2089 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2090 return TRUE;
2091
2092 break;
2093 }
2094
2095 /* This is a new version. Add it to tree we are building. */
2096
2097 if (t == NULL)
2098 {
2099 amt = sizeof *t;
a50b1753 2100 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2101 if (t == NULL)
2102 {
2103 rinfo->failed = TRUE;
2104 return FALSE;
2105 }
2106
2107 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2108 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2109 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2110 }
2111
2112 amt = sizeof *a;
a50b1753 2113 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2114 if (a == NULL)
2115 {
2116 rinfo->failed = TRUE;
2117 return FALSE;
2118 }
45d6a902
AM
2119
2120 /* Note that we are copying a string pointer here, and testing it
2121 above. If bfd_elf_string_from_elf_section is ever changed to
2122 discard the string data when low in memory, this will have to be
2123 fixed. */
2124 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2125
2126 a->vna_flags = h->verinfo.verdef->vd_flags;
2127 a->vna_nextptr = t->vn_auxptr;
2128
2129 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2130 ++rinfo->vers;
2131
2132 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2133
2134 t->vn_auxptr = a;
2135
2136 return TRUE;
2137}
2138
2139/* Figure out appropriate versions for all the symbols. We may not
2140 have the version number script until we have read all of the input
2141 files, so until that point we don't know which symbols should be
2142 local. This function is called via elf_link_hash_traverse. */
2143
28caa186 2144static bfd_boolean
268b6b39 2145_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2146{
28caa186 2147 struct elf_info_failed *sinfo;
45d6a902 2148 struct bfd_link_info *info;
9c5bfbb7 2149 const struct elf_backend_data *bed;
45d6a902
AM
2150 struct elf_info_failed eif;
2151 char *p;
45d6a902 2152
a50b1753 2153 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2154 info = sinfo->info;
2155
45d6a902
AM
2156 /* Fix the symbol flags. */
2157 eif.failed = FALSE;
2158 eif.info = info;
2159 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2160 {
2161 if (eif.failed)
2162 sinfo->failed = TRUE;
2163 return FALSE;
2164 }
2165
2166 /* We only need version numbers for symbols defined in regular
2167 objects. */
f5385ebf 2168 if (!h->def_regular)
45d6a902
AM
2169 return TRUE;
2170
28caa186 2171 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2172 p = strchr (h->root.root.string, ELF_VER_CHR);
2173 if (p != NULL && h->verinfo.vertree == NULL)
2174 {
2175 struct bfd_elf_version_tree *t;
45d6a902 2176
45d6a902
AM
2177 ++p;
2178 if (*p == ELF_VER_CHR)
6e33951e 2179 ++p;
45d6a902
AM
2180
2181 /* If there is no version string, we can just return out. */
2182 if (*p == '\0')
6e33951e 2183 return TRUE;
45d6a902
AM
2184
2185 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2186 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2187 {
2188 if (strcmp (t->name, p) == 0)
2189 {
2190 size_t len;
2191 char *alc;
2192 struct bfd_elf_version_expr *d;
2193
2194 len = p - h->root.root.string;
a50b1753 2195 alc = (char *) bfd_malloc (len);
45d6a902 2196 if (alc == NULL)
14b1c01e
AM
2197 {
2198 sinfo->failed = TRUE;
2199 return FALSE;
2200 }
45d6a902
AM
2201 memcpy (alc, h->root.root.string, len - 1);
2202 alc[len - 1] = '\0';
2203 if (alc[len - 2] == ELF_VER_CHR)
2204 alc[len - 2] = '\0';
2205
2206 h->verinfo.vertree = t;
2207 t->used = TRUE;
2208 d = NULL;
2209
108ba305
JJ
2210 if (t->globals.list != NULL)
2211 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2212
2213 /* See if there is anything to force this symbol to
2214 local scope. */
108ba305 2215 if (d == NULL && t->locals.list != NULL)
45d6a902 2216 {
108ba305
JJ
2217 d = (*t->match) (&t->locals, NULL, alc);
2218 if (d != NULL
2219 && h->dynindx != -1
108ba305
JJ
2220 && ! info->export_dynamic)
2221 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2222 }
2223
2224 free (alc);
2225 break;
2226 }
2227 }
2228
2229 /* If we are building an application, we need to create a
2230 version node for this version. */
0e1862bb 2231 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2232 {
2233 struct bfd_elf_version_tree **pp;
2234 int version_index;
2235
2236 /* If we aren't going to export this symbol, we don't need
2237 to worry about it. */
2238 if (h->dynindx == -1)
2239 return TRUE;
2240
ef53be89
AM
2241 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2242 sizeof *t);
45d6a902
AM
2243 if (t == NULL)
2244 {
2245 sinfo->failed = TRUE;
2246 return FALSE;
2247 }
2248
45d6a902 2249 t->name = p;
45d6a902
AM
2250 t->name_indx = (unsigned int) -1;
2251 t->used = TRUE;
2252
2253 version_index = 1;
2254 /* Don't count anonymous version tag. */
fd91d419
L
2255 if (sinfo->info->version_info != NULL
2256 && sinfo->info->version_info->vernum == 0)
45d6a902 2257 version_index = 0;
fd91d419
L
2258 for (pp = &sinfo->info->version_info;
2259 *pp != NULL;
2260 pp = &(*pp)->next)
45d6a902
AM
2261 ++version_index;
2262 t->vernum = version_index;
2263
2264 *pp = t;
2265
2266 h->verinfo.vertree = t;
2267 }
2268 else if (t == NULL)
2269 {
2270 /* We could not find the version for a symbol when
2271 generating a shared archive. Return an error. */
4eca0228 2272 _bfd_error_handler
695344c0 2273 /* xgettext:c-format */
c55fe096 2274 (_("%B: version node not found for symbol %s"),
28caa186 2275 info->output_bfd, h->root.root.string);
45d6a902
AM
2276 bfd_set_error (bfd_error_bad_value);
2277 sinfo->failed = TRUE;
2278 return FALSE;
2279 }
45d6a902
AM
2280 }
2281
2282 /* If we don't have a version for this symbol, see if we can find
2283 something. */
fd91d419 2284 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2285 {
1e8fa21e 2286 bfd_boolean hide;
ae5a3597 2287
fd91d419
L
2288 h->verinfo.vertree
2289 = bfd_find_version_for_sym (sinfo->info->version_info,
2290 h->root.root.string, &hide);
1e8fa21e
AM
2291 if (h->verinfo.vertree != NULL && hide)
2292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2293 }
2294
2295 return TRUE;
2296}
2297\f
45d6a902
AM
2298/* Read and swap the relocs from the section indicated by SHDR. This
2299 may be either a REL or a RELA section. The relocations are
2300 translated into RELA relocations and stored in INTERNAL_RELOCS,
2301 which should have already been allocated to contain enough space.
2302 The EXTERNAL_RELOCS are a buffer where the external form of the
2303 relocations should be stored.
2304
2305 Returns FALSE if something goes wrong. */
2306
2307static bfd_boolean
268b6b39 2308elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2309 asection *sec,
268b6b39
AM
2310 Elf_Internal_Shdr *shdr,
2311 void *external_relocs,
2312 Elf_Internal_Rela *internal_relocs)
45d6a902 2313{
9c5bfbb7 2314 const struct elf_backend_data *bed;
268b6b39 2315 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2316 const bfd_byte *erela;
2317 const bfd_byte *erelaend;
2318 Elf_Internal_Rela *irela;
243ef1e0
L
2319 Elf_Internal_Shdr *symtab_hdr;
2320 size_t nsyms;
45d6a902 2321
45d6a902
AM
2322 /* Position ourselves at the start of the section. */
2323 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2324 return FALSE;
2325
2326 /* Read the relocations. */
2327 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2328 return FALSE;
2329
243ef1e0 2330 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2331 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2332
45d6a902
AM
2333 bed = get_elf_backend_data (abfd);
2334
2335 /* Convert the external relocations to the internal format. */
2336 if (shdr->sh_entsize == bed->s->sizeof_rel)
2337 swap_in = bed->s->swap_reloc_in;
2338 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2339 swap_in = bed->s->swap_reloca_in;
2340 else
2341 {
2342 bfd_set_error (bfd_error_wrong_format);
2343 return FALSE;
2344 }
2345
a50b1753 2346 erela = (const bfd_byte *) external_relocs;
51992aec 2347 erelaend = erela + shdr->sh_size;
45d6a902
AM
2348 irela = internal_relocs;
2349 while (erela < erelaend)
2350 {
243ef1e0
L
2351 bfd_vma r_symndx;
2352
45d6a902 2353 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2354 r_symndx = ELF32_R_SYM (irela->r_info);
2355 if (bed->s->arch_size == 64)
2356 r_symndx >>= 24;
ce98a316
NC
2357 if (nsyms > 0)
2358 {
2359 if ((size_t) r_symndx >= nsyms)
2360 {
4eca0228 2361 _bfd_error_handler
695344c0 2362 /* xgettext:c-format */
ce98a316
NC
2363 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2364 " for offset 0x%lx in section `%A'"),
2365 abfd, sec,
2366 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2367 bfd_set_error (bfd_error_bad_value);
2368 return FALSE;
2369 }
2370 }
cf35638d 2371 else if (r_symndx != STN_UNDEF)
243ef1e0 2372 {
4eca0228 2373 _bfd_error_handler
695344c0 2374 /* xgettext:c-format */
ce98a316
NC
2375 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2376 " when the object file has no symbol table"),
d003868e
AM
2377 abfd, sec,
2378 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2379 bfd_set_error (bfd_error_bad_value);
2380 return FALSE;
2381 }
45d6a902
AM
2382 irela += bed->s->int_rels_per_ext_rel;
2383 erela += shdr->sh_entsize;
2384 }
2385
2386 return TRUE;
2387}
2388
2389/* Read and swap the relocs for a section O. They may have been
2390 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2391 not NULL, they are used as buffers to read into. They are known to
2392 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2393 the return value is allocated using either malloc or bfd_alloc,
2394 according to the KEEP_MEMORY argument. If O has two relocation
2395 sections (both REL and RELA relocations), then the REL_HDR
2396 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2397 RELA_HDR relocations. */
45d6a902
AM
2398
2399Elf_Internal_Rela *
268b6b39
AM
2400_bfd_elf_link_read_relocs (bfd *abfd,
2401 asection *o,
2402 void *external_relocs,
2403 Elf_Internal_Rela *internal_relocs,
2404 bfd_boolean keep_memory)
45d6a902 2405{
268b6b39 2406 void *alloc1 = NULL;
45d6a902 2407 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2409 struct bfd_elf_section_data *esdo = elf_section_data (o);
2410 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2411
d4730f92
BS
2412 if (esdo->relocs != NULL)
2413 return esdo->relocs;
45d6a902
AM
2414
2415 if (o->reloc_count == 0)
2416 return NULL;
2417
45d6a902
AM
2418 if (internal_relocs == NULL)
2419 {
2420 bfd_size_type size;
2421
2422 size = o->reloc_count;
2423 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2424 if (keep_memory)
a50b1753 2425 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2426 else
a50b1753 2427 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2428 if (internal_relocs == NULL)
2429 goto error_return;
2430 }
2431
2432 if (external_relocs == NULL)
2433 {
d4730f92
BS
2434 bfd_size_type size = 0;
2435
2436 if (esdo->rel.hdr)
2437 size += esdo->rel.hdr->sh_size;
2438 if (esdo->rela.hdr)
2439 size += esdo->rela.hdr->sh_size;
45d6a902 2440
268b6b39 2441 alloc1 = bfd_malloc (size);
45d6a902
AM
2442 if (alloc1 == NULL)
2443 goto error_return;
2444 external_relocs = alloc1;
2445 }
2446
d4730f92
BS
2447 internal_rela_relocs = internal_relocs;
2448 if (esdo->rel.hdr)
2449 {
2450 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2451 external_relocs,
2452 internal_relocs))
2453 goto error_return;
2454 external_relocs = (((bfd_byte *) external_relocs)
2455 + esdo->rel.hdr->sh_size);
2456 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2457 * bed->s->int_rels_per_ext_rel);
2458 }
2459
2460 if (esdo->rela.hdr
2461 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2462 external_relocs,
2463 internal_rela_relocs)))
45d6a902
AM
2464 goto error_return;
2465
2466 /* Cache the results for next time, if we can. */
2467 if (keep_memory)
d4730f92 2468 esdo->relocs = internal_relocs;
45d6a902
AM
2469
2470 if (alloc1 != NULL)
2471 free (alloc1);
2472
2473 /* Don't free alloc2, since if it was allocated we are passing it
2474 back (under the name of internal_relocs). */
2475
2476 return internal_relocs;
2477
2478 error_return:
2479 if (alloc1 != NULL)
2480 free (alloc1);
2481 if (alloc2 != NULL)
4dd07732
AM
2482 {
2483 if (keep_memory)
2484 bfd_release (abfd, alloc2);
2485 else
2486 free (alloc2);
2487 }
45d6a902
AM
2488 return NULL;
2489}
2490
2491/* Compute the size of, and allocate space for, REL_HDR which is the
2492 section header for a section containing relocations for O. */
2493
28caa186 2494static bfd_boolean
9eaff861
AO
2495_bfd_elf_link_size_reloc_section (bfd *abfd,
2496 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2497{
9eaff861 2498 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2499
2500 /* That allows us to calculate the size of the section. */
9eaff861 2501 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2502
2503 /* The contents field must last into write_object_contents, so we
2504 allocate it with bfd_alloc rather than malloc. Also since we
2505 cannot be sure that the contents will actually be filled in,
2506 we zero the allocated space. */
a50b1753 2507 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2508 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2509 return FALSE;
2510
d4730f92 2511 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2512 {
2513 struct elf_link_hash_entry **p;
2514
ca4be51c
AM
2515 p = ((struct elf_link_hash_entry **)
2516 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2517 if (p == NULL)
2518 return FALSE;
2519
d4730f92 2520 reldata->hashes = p;
45d6a902
AM
2521 }
2522
2523 return TRUE;
2524}
2525
2526/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2527 originated from the section given by INPUT_REL_HDR) to the
2528 OUTPUT_BFD. */
2529
2530bfd_boolean
268b6b39
AM
2531_bfd_elf_link_output_relocs (bfd *output_bfd,
2532 asection *input_section,
2533 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2534 Elf_Internal_Rela *internal_relocs,
2535 struct elf_link_hash_entry **rel_hash
2536 ATTRIBUTE_UNUSED)
45d6a902
AM
2537{
2538 Elf_Internal_Rela *irela;
2539 Elf_Internal_Rela *irelaend;
2540 bfd_byte *erel;
d4730f92 2541 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2542 asection *output_section;
9c5bfbb7 2543 const struct elf_backend_data *bed;
268b6b39 2544 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2545 struct bfd_elf_section_data *esdo;
45d6a902
AM
2546
2547 output_section = input_section->output_section;
45d6a902 2548
d4730f92
BS
2549 bed = get_elf_backend_data (output_bfd);
2550 esdo = elf_section_data (output_section);
2551 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2552 {
d4730f92
BS
2553 output_reldata = &esdo->rel;
2554 swap_out = bed->s->swap_reloc_out;
45d6a902 2555 }
d4730f92
BS
2556 else if (esdo->rela.hdr
2557 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2558 {
d4730f92
BS
2559 output_reldata = &esdo->rela;
2560 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2561 }
2562 else
2563 {
4eca0228 2564 _bfd_error_handler
695344c0 2565 /* xgettext:c-format */
d003868e
AM
2566 (_("%B: relocation size mismatch in %B section %A"),
2567 output_bfd, input_section->owner, input_section);
297d8443 2568 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2569 return FALSE;
2570 }
2571
d4730f92
BS
2572 erel = output_reldata->hdr->contents;
2573 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2574 irela = internal_relocs;
2575 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2576 * bed->s->int_rels_per_ext_rel);
2577 while (irela < irelaend)
2578 {
2579 (*swap_out) (output_bfd, irela, erel);
2580 irela += bed->s->int_rels_per_ext_rel;
2581 erel += input_rel_hdr->sh_entsize;
2582 }
2583
2584 /* Bump the counter, so that we know where to add the next set of
2585 relocations. */
d4730f92 2586 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2587
2588 return TRUE;
2589}
2590\f
508c3946
L
2591/* Make weak undefined symbols in PIE dynamic. */
2592
2593bfd_boolean
2594_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2595 struct elf_link_hash_entry *h)
2596{
0e1862bb 2597 if (bfd_link_pie (info)
508c3946
L
2598 && h->dynindx == -1
2599 && h->root.type == bfd_link_hash_undefweak)
2600 return bfd_elf_link_record_dynamic_symbol (info, h);
2601
2602 return TRUE;
2603}
2604
45d6a902
AM
2605/* Fix up the flags for a symbol. This handles various cases which
2606 can only be fixed after all the input files are seen. This is
2607 currently called by both adjust_dynamic_symbol and
2608 assign_sym_version, which is unnecessary but perhaps more robust in
2609 the face of future changes. */
2610
28caa186 2611static bfd_boolean
268b6b39
AM
2612_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2613 struct elf_info_failed *eif)
45d6a902 2614{
33774f08 2615 const struct elf_backend_data *bed;
508c3946 2616
45d6a902
AM
2617 /* If this symbol was mentioned in a non-ELF file, try to set
2618 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2619 permit a non-ELF file to correctly refer to a symbol defined in
2620 an ELF dynamic object. */
f5385ebf 2621 if (h->non_elf)
45d6a902
AM
2622 {
2623 while (h->root.type == bfd_link_hash_indirect)
2624 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2625
2626 if (h->root.type != bfd_link_hash_defined
2627 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2628 {
2629 h->ref_regular = 1;
2630 h->ref_regular_nonweak = 1;
2631 }
45d6a902
AM
2632 else
2633 {
2634 if (h->root.u.def.section->owner != NULL
2635 && (bfd_get_flavour (h->root.u.def.section->owner)
2636 == bfd_target_elf_flavour))
f5385ebf
AM
2637 {
2638 h->ref_regular = 1;
2639 h->ref_regular_nonweak = 1;
2640 }
45d6a902 2641 else
f5385ebf 2642 h->def_regular = 1;
45d6a902
AM
2643 }
2644
2645 if (h->dynindx == -1
f5385ebf
AM
2646 && (h->def_dynamic
2647 || h->ref_dynamic))
45d6a902 2648 {
c152c796 2649 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2650 {
2651 eif->failed = TRUE;
2652 return FALSE;
2653 }
2654 }
2655 }
2656 else
2657 {
f5385ebf 2658 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2659 was first seen in a non-ELF file. Fortunately, if the symbol
2660 was first seen in an ELF file, we're probably OK unless the
2661 symbol was defined in a non-ELF file. Catch that case here.
2662 FIXME: We're still in trouble if the symbol was first seen in
2663 a dynamic object, and then later in a non-ELF regular object. */
2664 if ((h->root.type == bfd_link_hash_defined
2665 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2666 && !h->def_regular
45d6a902
AM
2667 && (h->root.u.def.section->owner != NULL
2668 ? (bfd_get_flavour (h->root.u.def.section->owner)
2669 != bfd_target_elf_flavour)
2670 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2671 && !h->def_dynamic)))
2672 h->def_regular = 1;
45d6a902
AM
2673 }
2674
508c3946 2675 /* Backend specific symbol fixup. */
33774f08
AM
2676 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2677 if (bed->elf_backend_fixup_symbol
2678 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2679 return FALSE;
508c3946 2680
45d6a902
AM
2681 /* If this is a final link, and the symbol was defined as a common
2682 symbol in a regular object file, and there was no definition in
2683 any dynamic object, then the linker will have allocated space for
f5385ebf 2684 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2685 flag will not have been set. */
2686 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2687 && !h->def_regular
2688 && h->ref_regular
2689 && !h->def_dynamic
96f29d96 2690 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2691 h->def_regular = 1;
45d6a902 2692
4deb8f71
L
2693 /* If a weak undefined symbol has non-default visibility, we also
2694 hide it from the dynamic linker. */
2695 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2696 && h->root.type == bfd_link_hash_undefweak)
2697 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2698
2699 /* A hidden versioned symbol in executable should be forced local if
2700 it is is locally defined, not referenced by shared library and not
2701 exported. */
2702 else if (bfd_link_executable (eif->info)
2703 && h->versioned == versioned_hidden
2704 && !eif->info->export_dynamic
2705 && !h->dynamic
2706 && !h->ref_dynamic
2707 && h->def_regular)
2708 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2709
45d6a902
AM
2710 /* If -Bsymbolic was used (which means to bind references to global
2711 symbols to the definition within the shared object), and this
2712 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2713 need a PLT entry. Likewise, if the symbol has non-default
2714 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2715 will force it local. */
4deb8f71
L
2716 else if (h->needs_plt
2717 && bfd_link_pic (eif->info)
2718 && is_elf_hash_table (eif->info->hash)
2719 && (SYMBOLIC_BIND (eif->info, h)
2720 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2721 && h->def_regular)
45d6a902 2722 {
45d6a902
AM
2723 bfd_boolean force_local;
2724
45d6a902
AM
2725 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2726 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2727 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2728 }
2729
45d6a902
AM
2730 /* If this is a weak defined symbol in a dynamic object, and we know
2731 the real definition in the dynamic object, copy interesting flags
2732 over to the real definition. */
f6e332e6 2733 if (h->u.weakdef != NULL)
45d6a902 2734 {
45d6a902
AM
2735 /* If the real definition is defined by a regular object file,
2736 don't do anything special. See the longer description in
2737 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2738 if (h->u.weakdef->def_regular)
f6e332e6 2739 h->u.weakdef = NULL;
45d6a902 2740 else
a26587ba 2741 {
4e6b54a6
AM
2742 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2743
2744 while (h->root.type == bfd_link_hash_indirect)
2745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2746
2747 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2748 || h->root.type == bfd_link_hash_defweak);
2749 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2750 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2751 || weakdef->root.type == bfd_link_hash_defweak);
2752 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2753 }
45d6a902
AM
2754 }
2755
2756 return TRUE;
2757}
2758
2759/* Make the backend pick a good value for a dynamic symbol. This is
2760 called via elf_link_hash_traverse, and also calls itself
2761 recursively. */
2762
28caa186 2763static bfd_boolean
268b6b39 2764_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2765{
a50b1753 2766 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2767 bfd *dynobj;
9c5bfbb7 2768 const struct elf_backend_data *bed;
45d6a902 2769
0eddce27 2770 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2771 return FALSE;
2772
45d6a902
AM
2773 /* Ignore indirect symbols. These are added by the versioning code. */
2774 if (h->root.type == bfd_link_hash_indirect)
2775 return TRUE;
2776
2777 /* Fix the symbol flags. */
2778 if (! _bfd_elf_fix_symbol_flags (h, eif))
2779 return FALSE;
2780
2781 /* If this symbol does not require a PLT entry, and it is not
2782 defined by a dynamic object, or is not referenced by a regular
2783 object, ignore it. We do have to handle a weak defined symbol,
2784 even if no regular object refers to it, if we decided to add it
2785 to the dynamic symbol table. FIXME: Do we normally need to worry
2786 about symbols which are defined by one dynamic object and
2787 referenced by another one? */
f5385ebf 2788 if (!h->needs_plt
91e21fb7 2789 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2790 && (h->def_regular
2791 || !h->def_dynamic
2792 || (!h->ref_regular
f6e332e6 2793 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2794 {
a6aa5195 2795 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2796 return TRUE;
2797 }
2798
2799 /* If we've already adjusted this symbol, don't do it again. This
2800 can happen via a recursive call. */
f5385ebf 2801 if (h->dynamic_adjusted)
45d6a902
AM
2802 return TRUE;
2803
2804 /* Don't look at this symbol again. Note that we must set this
2805 after checking the above conditions, because we may look at a
2806 symbol once, decide not to do anything, and then get called
2807 recursively later after REF_REGULAR is set below. */
f5385ebf 2808 h->dynamic_adjusted = 1;
45d6a902
AM
2809
2810 /* If this is a weak definition, and we know a real definition, and
2811 the real symbol is not itself defined by a regular object file,
2812 then get a good value for the real definition. We handle the
2813 real symbol first, for the convenience of the backend routine.
2814
2815 Note that there is a confusing case here. If the real definition
2816 is defined by a regular object file, we don't get the real symbol
2817 from the dynamic object, but we do get the weak symbol. If the
2818 processor backend uses a COPY reloc, then if some routine in the
2819 dynamic object changes the real symbol, we will not see that
2820 change in the corresponding weak symbol. This is the way other
2821 ELF linkers work as well, and seems to be a result of the shared
2822 library model.
2823
2824 I will clarify this issue. Most SVR4 shared libraries define the
2825 variable _timezone and define timezone as a weak synonym. The
2826 tzset call changes _timezone. If you write
2827 extern int timezone;
2828 int _timezone = 5;
2829 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2830 you might expect that, since timezone is a synonym for _timezone,
2831 the same number will print both times. However, if the processor
2832 backend uses a COPY reloc, then actually timezone will be copied
2833 into your process image, and, since you define _timezone
2834 yourself, _timezone will not. Thus timezone and _timezone will
2835 wind up at different memory locations. The tzset call will set
2836 _timezone, leaving timezone unchanged. */
2837
f6e332e6 2838 if (h->u.weakdef != NULL)
45d6a902 2839 {
ec24dc88
AM
2840 /* If we get to this point, there is an implicit reference to
2841 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2842 h->u.weakdef->ref_regular = 1;
45d6a902 2843
ec24dc88
AM
2844 /* Ensure that the backend adjust_dynamic_symbol function sees
2845 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2846 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2847 return FALSE;
2848 }
2849
2850 /* If a symbol has no type and no size and does not require a PLT
2851 entry, then we are probably about to do the wrong thing here: we
2852 are probably going to create a COPY reloc for an empty object.
2853 This case can arise when a shared object is built with assembly
2854 code, and the assembly code fails to set the symbol type. */
2855 if (h->size == 0
2856 && h->type == STT_NOTYPE
f5385ebf 2857 && !h->needs_plt)
4eca0228 2858 _bfd_error_handler
45d6a902
AM
2859 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2860 h->root.root.string);
2861
2862 dynobj = elf_hash_table (eif->info)->dynobj;
2863 bed = get_elf_backend_data (dynobj);
e7c33416 2864
45d6a902
AM
2865 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2866 {
2867 eif->failed = TRUE;
2868 return FALSE;
2869 }
2870
2871 return TRUE;
2872}
2873
027297b7
L
2874/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2875 DYNBSS. */
2876
2877bfd_boolean
6cabe1ea
AM
2878_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2879 struct elf_link_hash_entry *h,
027297b7
L
2880 asection *dynbss)
2881{
91ac5911 2882 unsigned int power_of_two;
027297b7
L
2883 bfd_vma mask;
2884 asection *sec = h->root.u.def.section;
2885
2886 /* The section aligment of definition is the maximum alignment
91ac5911
L
2887 requirement of symbols defined in the section. Since we don't
2888 know the symbol alignment requirement, we start with the
2889 maximum alignment and check low bits of the symbol address
2890 for the minimum alignment. */
2891 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2892 mask = ((bfd_vma) 1 << power_of_two) - 1;
2893 while ((h->root.u.def.value & mask) != 0)
2894 {
2895 mask >>= 1;
2896 --power_of_two;
2897 }
027297b7 2898
91ac5911
L
2899 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2900 dynbss))
027297b7
L
2901 {
2902 /* Adjust the section alignment if needed. */
2903 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2904 power_of_two))
027297b7
L
2905 return FALSE;
2906 }
2907
91ac5911 2908 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2909 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2910
2911 /* Define the symbol as being at this point in DYNBSS. */
2912 h->root.u.def.section = dynbss;
2913 h->root.u.def.value = dynbss->size;
2914
2915 /* Increment the size of DYNBSS to make room for the symbol. */
2916 dynbss->size += h->size;
2917
f7483970
L
2918 /* No error if extern_protected_data is true. */
2919 if (h->protected_def
889c2a67
L
2920 && (!info->extern_protected_data
2921 || (info->extern_protected_data < 0
2922 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2923 info->callbacks->einfo
2924 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2925 h->root.root.string);
6cabe1ea 2926
027297b7
L
2927 return TRUE;
2928}
2929
45d6a902
AM
2930/* Adjust all external symbols pointing into SEC_MERGE sections
2931 to reflect the object merging within the sections. */
2932
28caa186 2933static bfd_boolean
268b6b39 2934_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2935{
2936 asection *sec;
2937
45d6a902
AM
2938 if ((h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak)
2940 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2941 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2942 {
a50b1753 2943 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2944
2945 h->root.u.def.value =
2946 _bfd_merged_section_offset (output_bfd,
2947 &h->root.u.def.section,
2948 elf_section_data (sec)->sec_info,
753731ee 2949 h->root.u.def.value);
45d6a902
AM
2950 }
2951
2952 return TRUE;
2953}
986a241f
RH
2954
2955/* Returns false if the symbol referred to by H should be considered
2956 to resolve local to the current module, and true if it should be
2957 considered to bind dynamically. */
2958
2959bfd_boolean
268b6b39
AM
2960_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2961 struct bfd_link_info *info,
89a2ee5a 2962 bfd_boolean not_local_protected)
986a241f
RH
2963{
2964 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2965 const struct elf_backend_data *bed;
2966 struct elf_link_hash_table *hash_table;
986a241f
RH
2967
2968 if (h == NULL)
2969 return FALSE;
2970
2971 while (h->root.type == bfd_link_hash_indirect
2972 || h->root.type == bfd_link_hash_warning)
2973 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2974
2975 /* If it was forced local, then clearly it's not dynamic. */
2976 if (h->dynindx == -1)
2977 return FALSE;
f5385ebf 2978 if (h->forced_local)
986a241f
RH
2979 return FALSE;
2980
2981 /* Identify the cases where name binding rules say that a
2982 visible symbol resolves locally. */
0e1862bb
L
2983 binding_stays_local_p = (bfd_link_executable (info)
2984 || SYMBOLIC_BIND (info, h));
986a241f
RH
2985
2986 switch (ELF_ST_VISIBILITY (h->other))
2987 {
2988 case STV_INTERNAL:
2989 case STV_HIDDEN:
2990 return FALSE;
2991
2992 case STV_PROTECTED:
fcb93ecf
PB
2993 hash_table = elf_hash_table (info);
2994 if (!is_elf_hash_table (hash_table))
2995 return FALSE;
2996
2997 bed = get_elf_backend_data (hash_table->dynobj);
2998
986a241f
RH
2999 /* Proper resolution for function pointer equality may require
3000 that these symbols perhaps be resolved dynamically, even though
3001 we should be resolving them to the current module. */
89a2ee5a 3002 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3003 binding_stays_local_p = TRUE;
3004 break;
3005
3006 default:
986a241f
RH
3007 break;
3008 }
3009
aa37626c 3010 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3011 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3012 return TRUE;
3013
986a241f
RH
3014 /* Otherwise, the symbol is dynamic if binding rules don't tell
3015 us that it remains local. */
3016 return !binding_stays_local_p;
3017}
f6c52c13
AM
3018
3019/* Return true if the symbol referred to by H should be considered
3020 to resolve local to the current module, and false otherwise. Differs
3021 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3022 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
3023 for the place where forced_local and dynindx == -1 are tested. If
3024 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3025 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3026 the symbol is local only for defined symbols.
3027 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3028 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3029 treatment of undefined weak symbols. For those that do not make
3030 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3031
3032bfd_boolean
268b6b39
AM
3033_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3034 struct bfd_link_info *info,
3035 bfd_boolean local_protected)
f6c52c13 3036{
fcb93ecf
PB
3037 const struct elf_backend_data *bed;
3038 struct elf_link_hash_table *hash_table;
3039
f6c52c13
AM
3040 /* If it's a local sym, of course we resolve locally. */
3041 if (h == NULL)
3042 return TRUE;
3043
d95edcac
L
3044 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3045 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3046 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3047 return TRUE;
3048
7e2294f9
AO
3049 /* Common symbols that become definitions don't get the DEF_REGULAR
3050 flag set, so test it first, and don't bail out. */
3051 if (ELF_COMMON_DEF_P (h))
3052 /* Do nothing. */;
f6c52c13 3053 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3054 resolve locally. The sym is either undefined or dynamic. */
3055 else if (!h->def_regular)
f6c52c13
AM
3056 return FALSE;
3057
3058 /* Forced local symbols resolve locally. */
f5385ebf 3059 if (h->forced_local)
f6c52c13
AM
3060 return TRUE;
3061
3062 /* As do non-dynamic symbols. */
3063 if (h->dynindx == -1)
3064 return TRUE;
3065
3066 /* At this point, we know the symbol is defined and dynamic. In an
3067 executable it must resolve locally, likewise when building symbolic
3068 shared libraries. */
0e1862bb 3069 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3070 return TRUE;
3071
3072 /* Now deal with defined dynamic symbols in shared libraries. Ones
3073 with default visibility might not resolve locally. */
3074 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3075 return FALSE;
3076
fcb93ecf
PB
3077 hash_table = elf_hash_table (info);
3078 if (!is_elf_hash_table (hash_table))
3079 return TRUE;
3080
3081 bed = get_elf_backend_data (hash_table->dynobj);
3082
f7483970
L
3083 /* If extern_protected_data is false, STV_PROTECTED non-function
3084 symbols are local. */
889c2a67
L
3085 if ((!info->extern_protected_data
3086 || (info->extern_protected_data < 0
3087 && !bed->extern_protected_data))
3088 && !bed->is_function_type (h->type))
1c16dfa5
L
3089 return TRUE;
3090
f6c52c13 3091 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3092 symbols be treated as dynamic symbols. If the address of a
3093 function not defined in an executable is set to that function's
3094 plt entry in the executable, then the address of the function in
3095 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3096 return local_protected;
3097}
e1918d23
AM
3098
3099/* Caches some TLS segment info, and ensures that the TLS segment vma is
3100 aligned. Returns the first TLS output section. */
3101
3102struct bfd_section *
3103_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3104{
3105 struct bfd_section *sec, *tls;
3106 unsigned int align = 0;
3107
3108 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3109 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3110 break;
3111 tls = sec;
3112
3113 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3114 if (sec->alignment_power > align)
3115 align = sec->alignment_power;
3116
3117 elf_hash_table (info)->tls_sec = tls;
3118
3119 /* Ensure the alignment of the first section is the largest alignment,
3120 so that the tls segment starts aligned. */
3121 if (tls != NULL)
3122 tls->alignment_power = align;
3123
3124 return tls;
3125}
0ad989f9
L
3126
3127/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3128static bfd_boolean
3129is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3130 Elf_Internal_Sym *sym)
3131{
a4d8e49b
L
3132 const struct elf_backend_data *bed;
3133
0ad989f9
L
3134 /* Local symbols do not count, but target specific ones might. */
3135 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3136 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3137 return FALSE;
3138
fcb93ecf 3139 bed = get_elf_backend_data (abfd);
0ad989f9 3140 /* Function symbols do not count. */
fcb93ecf 3141 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3142 return FALSE;
3143
3144 /* If the section is undefined, then so is the symbol. */
3145 if (sym->st_shndx == SHN_UNDEF)
3146 return FALSE;
3147
3148 /* If the symbol is defined in the common section, then
3149 it is a common definition and so does not count. */
a4d8e49b 3150 if (bed->common_definition (sym))
0ad989f9
L
3151 return FALSE;
3152
3153 /* If the symbol is in a target specific section then we
3154 must rely upon the backend to tell us what it is. */
3155 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3156 /* FIXME - this function is not coded yet:
3157
3158 return _bfd_is_global_symbol_definition (abfd, sym);
3159
3160 Instead for now assume that the definition is not global,
3161 Even if this is wrong, at least the linker will behave
3162 in the same way that it used to do. */
3163 return FALSE;
3164
3165 return TRUE;
3166}
3167
3168/* Search the symbol table of the archive element of the archive ABFD
3169 whose archive map contains a mention of SYMDEF, and determine if
3170 the symbol is defined in this element. */
3171static bfd_boolean
3172elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3173{
3174 Elf_Internal_Shdr * hdr;
ef53be89
AM
3175 size_t symcount;
3176 size_t extsymcount;
3177 size_t extsymoff;
0ad989f9
L
3178 Elf_Internal_Sym *isymbuf;
3179 Elf_Internal_Sym *isym;
3180 Elf_Internal_Sym *isymend;
3181 bfd_boolean result;
3182
3183 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3184 if (abfd == NULL)
3185 return FALSE;
3186
3187 if (! bfd_check_format (abfd, bfd_object))
3188 return FALSE;
3189
7dc3990e
L
3190 /* Select the appropriate symbol table. If we don't know if the
3191 object file is an IR object, give linker LTO plugin a chance to
3192 get the correct symbol table. */
3193 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3194#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3195 || (abfd->plugin_format == bfd_plugin_unknown
3196 && bfd_link_plugin_object_p (abfd))
3197#endif
3198 )
3199 {
3200 /* Use the IR symbol table if the object has been claimed by
3201 plugin. */
3202 abfd = abfd->plugin_dummy_bfd;
3203 hdr = &elf_tdata (abfd)->symtab_hdr;
3204 }
3205 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3206 hdr = &elf_tdata (abfd)->symtab_hdr;
3207 else
3208 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3209
3210 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3211
3212 /* The sh_info field of the symtab header tells us where the
3213 external symbols start. We don't care about the local symbols. */
3214 if (elf_bad_symtab (abfd))
3215 {
3216 extsymcount = symcount;
3217 extsymoff = 0;
3218 }
3219 else
3220 {
3221 extsymcount = symcount - hdr->sh_info;
3222 extsymoff = hdr->sh_info;
3223 }
3224
3225 if (extsymcount == 0)
3226 return FALSE;
3227
3228 /* Read in the symbol table. */
3229 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3230 NULL, NULL, NULL);
3231 if (isymbuf == NULL)
3232 return FALSE;
3233
3234 /* Scan the symbol table looking for SYMDEF. */
3235 result = FALSE;
3236 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3237 {
3238 const char *name;
3239
3240 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3241 isym->st_name);
3242 if (name == NULL)
3243 break;
3244
3245 if (strcmp (name, symdef->name) == 0)
3246 {
3247 result = is_global_data_symbol_definition (abfd, isym);
3248 break;
3249 }
3250 }
3251
3252 free (isymbuf);
3253
3254 return result;
3255}
3256\f
5a580b3a
AM
3257/* Add an entry to the .dynamic table. */
3258
3259bfd_boolean
3260_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3261 bfd_vma tag,
3262 bfd_vma val)
3263{
3264 struct elf_link_hash_table *hash_table;
3265 const struct elf_backend_data *bed;
3266 asection *s;
3267 bfd_size_type newsize;
3268 bfd_byte *newcontents;
3269 Elf_Internal_Dyn dyn;
3270
3271 hash_table = elf_hash_table (info);
3272 if (! is_elf_hash_table (hash_table))
3273 return FALSE;
3274
3275 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3276 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3277 BFD_ASSERT (s != NULL);
3278
eea6121a 3279 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3280 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3281 if (newcontents == NULL)
3282 return FALSE;
3283
3284 dyn.d_tag = tag;
3285 dyn.d_un.d_val = val;
eea6121a 3286 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3287
eea6121a 3288 s->size = newsize;
5a580b3a
AM
3289 s->contents = newcontents;
3290
3291 return TRUE;
3292}
3293
3294/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3295 otherwise just check whether one already exists. Returns -1 on error,
3296 1 if a DT_NEEDED tag already exists, and 0 on success. */
3297
4ad4eba5 3298static int
7e9f0867
AM
3299elf_add_dt_needed_tag (bfd *abfd,
3300 struct bfd_link_info *info,
4ad4eba5
AM
3301 const char *soname,
3302 bfd_boolean do_it)
5a580b3a
AM
3303{
3304 struct elf_link_hash_table *hash_table;
ef53be89 3305 size_t strindex;
5a580b3a 3306
7e9f0867
AM
3307 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3308 return -1;
3309
5a580b3a 3310 hash_table = elf_hash_table (info);
5a580b3a 3311 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3312 if (strindex == (size_t) -1)
5a580b3a
AM
3313 return -1;
3314
02be4619 3315 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3316 {
3317 asection *sdyn;
3318 const struct elf_backend_data *bed;
3319 bfd_byte *extdyn;
3320
3321 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3322 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3323 if (sdyn != NULL)
3324 for (extdyn = sdyn->contents;
3325 extdyn < sdyn->contents + sdyn->size;
3326 extdyn += bed->s->sizeof_dyn)
3327 {
3328 Elf_Internal_Dyn dyn;
5a580b3a 3329
7e9f0867
AM
3330 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3331 if (dyn.d_tag == DT_NEEDED
3332 && dyn.d_un.d_val == strindex)
3333 {
3334 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3335 return 1;
3336 }
3337 }
5a580b3a
AM
3338 }
3339
3340 if (do_it)
3341 {
7e9f0867
AM
3342 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3343 return -1;
3344
5a580b3a
AM
3345 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3346 return -1;
3347 }
3348 else
3349 /* We were just checking for existence of the tag. */
3350 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3351
3352 return 0;
3353}
3354
7b15fa7a
AM
3355/* Return true if SONAME is on the needed list between NEEDED and STOP
3356 (or the end of list if STOP is NULL), and needed by a library that
3357 will be loaded. */
3358
010e5ae2 3359static bfd_boolean
7b15fa7a
AM
3360on_needed_list (const char *soname,
3361 struct bfd_link_needed_list *needed,
3362 struct bfd_link_needed_list *stop)
010e5ae2 3363{
7b15fa7a
AM
3364 struct bfd_link_needed_list *look;
3365 for (look = needed; look != stop; look = look->next)
3366 if (strcmp (soname, look->name) == 0
3367 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3368 /* If needed by a library that itself is not directly
3369 needed, recursively check whether that library is
3370 indirectly needed. Since we add DT_NEEDED entries to
3371 the end of the list, library dependencies appear after
3372 the library. Therefore search prior to the current
3373 LOOK, preventing possible infinite recursion. */
3374 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3375 return TRUE;
3376
3377 return FALSE;
3378}
3379
14160578 3380/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3381static int
3382elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3383{
3384 const struct elf_link_hash_entry *h1;
3385 const struct elf_link_hash_entry *h2;
10b7e05b 3386 bfd_signed_vma vdiff;
5a580b3a
AM
3387
3388 h1 = *(const struct elf_link_hash_entry **) arg1;
3389 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3390 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3391 if (vdiff != 0)
3392 return vdiff > 0 ? 1 : -1;
3393 else
3394 {
d3435ae8 3395 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3396 if (sdiff != 0)
3397 return sdiff > 0 ? 1 : -1;
3398 }
14160578
AM
3399 vdiff = h1->size - h2->size;
3400 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3401}
4ad4eba5 3402
5a580b3a
AM
3403/* This function is used to adjust offsets into .dynstr for
3404 dynamic symbols. This is called via elf_link_hash_traverse. */
3405
3406static bfd_boolean
3407elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3408{
a50b1753 3409 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3410
5a580b3a
AM
3411 if (h->dynindx != -1)
3412 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3413 return TRUE;
3414}
3415
3416/* Assign string offsets in .dynstr, update all structures referencing
3417 them. */
3418
4ad4eba5
AM
3419static bfd_boolean
3420elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3421{
3422 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3423 struct elf_link_local_dynamic_entry *entry;
3424 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3425 bfd *dynobj = hash_table->dynobj;
3426 asection *sdyn;
3427 bfd_size_type size;
3428 const struct elf_backend_data *bed;
3429 bfd_byte *extdyn;
3430
3431 _bfd_elf_strtab_finalize (dynstr);
3432 size = _bfd_elf_strtab_size (dynstr);
3433
3434 bed = get_elf_backend_data (dynobj);
3d4d4302 3435 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3436 BFD_ASSERT (sdyn != NULL);
3437
3438 /* Update all .dynamic entries referencing .dynstr strings. */
3439 for (extdyn = sdyn->contents;
eea6121a 3440 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3441 extdyn += bed->s->sizeof_dyn)
3442 {
3443 Elf_Internal_Dyn dyn;
3444
3445 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3446 switch (dyn.d_tag)
3447 {
3448 case DT_STRSZ:
3449 dyn.d_un.d_val = size;
3450 break;
3451 case DT_NEEDED:
3452 case DT_SONAME:
3453 case DT_RPATH:
3454 case DT_RUNPATH:
3455 case DT_FILTER:
3456 case DT_AUXILIARY:
7ee314fa
AM
3457 case DT_AUDIT:
3458 case DT_DEPAUDIT:
5a580b3a
AM
3459 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3460 break;
3461 default:
3462 continue;
3463 }
3464 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3465 }
3466
3467 /* Now update local dynamic symbols. */
3468 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3469 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3470 entry->isym.st_name);
3471
3472 /* And the rest of dynamic symbols. */
3473 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3474
3475 /* Adjust version definitions. */
3476 if (elf_tdata (output_bfd)->cverdefs)
3477 {
3478 asection *s;
3479 bfd_byte *p;
ef53be89 3480 size_t i;
5a580b3a
AM
3481 Elf_Internal_Verdef def;
3482 Elf_Internal_Verdaux defaux;
3483
3d4d4302 3484 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3485 p = s->contents;
3486 do
3487 {
3488 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3489 &def);
3490 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3491 if (def.vd_aux != sizeof (Elf_External_Verdef))
3492 continue;
5a580b3a
AM
3493 for (i = 0; i < def.vd_cnt; ++i)
3494 {
3495 _bfd_elf_swap_verdaux_in (output_bfd,
3496 (Elf_External_Verdaux *) p, &defaux);
3497 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3498 defaux.vda_name);
3499 _bfd_elf_swap_verdaux_out (output_bfd,
3500 &defaux, (Elf_External_Verdaux *) p);
3501 p += sizeof (Elf_External_Verdaux);
3502 }
3503 }
3504 while (def.vd_next);
3505 }
3506
3507 /* Adjust version references. */
3508 if (elf_tdata (output_bfd)->verref)
3509 {
3510 asection *s;
3511 bfd_byte *p;
ef53be89 3512 size_t i;
5a580b3a
AM
3513 Elf_Internal_Verneed need;
3514 Elf_Internal_Vernaux needaux;
3515
3d4d4302 3516 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3517 p = s->contents;
3518 do
3519 {
3520 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3521 &need);
3522 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3523 _bfd_elf_swap_verneed_out (output_bfd, &need,
3524 (Elf_External_Verneed *) p);
3525 p += sizeof (Elf_External_Verneed);
3526 for (i = 0; i < need.vn_cnt; ++i)
3527 {
3528 _bfd_elf_swap_vernaux_in (output_bfd,
3529 (Elf_External_Vernaux *) p, &needaux);
3530 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3531 needaux.vna_name);
3532 _bfd_elf_swap_vernaux_out (output_bfd,
3533 &needaux,
3534 (Elf_External_Vernaux *) p);
3535 p += sizeof (Elf_External_Vernaux);
3536 }
3537 }
3538 while (need.vn_next);
3539 }
3540
3541 return TRUE;
3542}
3543\f
13285a1b
AM
3544/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3545 The default is to only match when the INPUT and OUTPUT are exactly
3546 the same target. */
3547
3548bfd_boolean
3549_bfd_elf_default_relocs_compatible (const bfd_target *input,
3550 const bfd_target *output)
3551{
3552 return input == output;
3553}
3554
3555/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3556 This version is used when different targets for the same architecture
3557 are virtually identical. */
3558
3559bfd_boolean
3560_bfd_elf_relocs_compatible (const bfd_target *input,
3561 const bfd_target *output)
3562{
3563 const struct elf_backend_data *obed, *ibed;
3564
3565 if (input == output)
3566 return TRUE;
3567
3568 ibed = xvec_get_elf_backend_data (input);
3569 obed = xvec_get_elf_backend_data (output);
3570
3571 if (ibed->arch != obed->arch)
3572 return FALSE;
3573
3574 /* If both backends are using this function, deem them compatible. */
3575 return ibed->relocs_compatible == obed->relocs_compatible;
3576}
3577
e5034e59
AM
3578/* Make a special call to the linker "notice" function to tell it that
3579 we are about to handle an as-needed lib, or have finished
1b786873 3580 processing the lib. */
e5034e59
AM
3581
3582bfd_boolean
3583_bfd_elf_notice_as_needed (bfd *ibfd,
3584 struct bfd_link_info *info,
3585 enum notice_asneeded_action act)
3586{
46135103 3587 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3588}
3589
d9689752
L
3590/* Check relocations an ELF object file. */
3591
3592bfd_boolean
3593_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3594{
3595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3596 struct elf_link_hash_table *htab = elf_hash_table (info);
3597
3598 /* If this object is the same format as the output object, and it is
3599 not a shared library, then let the backend look through the
3600 relocs.
3601
3602 This is required to build global offset table entries and to
3603 arrange for dynamic relocs. It is not required for the
3604 particular common case of linking non PIC code, even when linking
3605 against shared libraries, but unfortunately there is no way of
3606 knowing whether an object file has been compiled PIC or not.
3607 Looking through the relocs is not particularly time consuming.
3608 The problem is that we must either (1) keep the relocs in memory,
3609 which causes the linker to require additional runtime memory or
3610 (2) read the relocs twice from the input file, which wastes time.
3611 This would be a good case for using mmap.
3612
3613 I have no idea how to handle linking PIC code into a file of a
3614 different format. It probably can't be done. */
3615 if ((abfd->flags & DYNAMIC) == 0
3616 && is_elf_hash_table (htab)
3617 && bed->check_relocs != NULL
3618 && elf_object_id (abfd) == elf_hash_table_id (htab)
3619 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3620 {
3621 asection *o;
3622
3623 for (o = abfd->sections; o != NULL; o = o->next)
3624 {
3625 Elf_Internal_Rela *internal_relocs;
3626 bfd_boolean ok;
3627
5ce03cea 3628 /* Don't check relocations in excluded sections. */
d9689752 3629 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3630 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3631 || o->reloc_count == 0
3632 || ((info->strip == strip_all || info->strip == strip_debugger)
3633 && (o->flags & SEC_DEBUGGING) != 0)
3634 || bfd_is_abs_section (o->output_section))
3635 continue;
3636
3637 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3638 info->keep_memory);
3639 if (internal_relocs == NULL)
3640 return FALSE;
3641
3642 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3643
3644 if (elf_section_data (o)->relocs != internal_relocs)
3645 free (internal_relocs);
3646
3647 if (! ok)
3648 return FALSE;
3649 }
3650 }
3651
3652 return TRUE;
3653}
3654
4ad4eba5
AM
3655/* Add symbols from an ELF object file to the linker hash table. */
3656
3657static bfd_boolean
3658elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3659{
a0c402a5 3660 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3661 Elf_Internal_Shdr *hdr;
ef53be89
AM
3662 size_t symcount;
3663 size_t extsymcount;
3664 size_t extsymoff;
4ad4eba5
AM
3665 struct elf_link_hash_entry **sym_hash;
3666 bfd_boolean dynamic;
3667 Elf_External_Versym *extversym = NULL;
3668 Elf_External_Versym *ever;
3669 struct elf_link_hash_entry *weaks;
3670 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3671 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3672 Elf_Internal_Sym *isymbuf = NULL;
3673 Elf_Internal_Sym *isym;
3674 Elf_Internal_Sym *isymend;
3675 const struct elf_backend_data *bed;
3676 bfd_boolean add_needed;
66eb6687 3677 struct elf_link_hash_table *htab;
4ad4eba5 3678 bfd_size_type amt;
66eb6687 3679 void *alloc_mark = NULL;
4f87808c
AM
3680 struct bfd_hash_entry **old_table = NULL;
3681 unsigned int old_size = 0;
3682 unsigned int old_count = 0;
66eb6687 3683 void *old_tab = NULL;
66eb6687
AM
3684 void *old_ent;
3685 struct bfd_link_hash_entry *old_undefs = NULL;
3686 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3687 void *old_strtab = NULL;
66eb6687 3688 size_t tabsize = 0;
db6a5d5f 3689 asection *s;
29a9f53e 3690 bfd_boolean just_syms;
4ad4eba5 3691
66eb6687 3692 htab = elf_hash_table (info);
4ad4eba5 3693 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3694
3695 if ((abfd->flags & DYNAMIC) == 0)
3696 dynamic = FALSE;
3697 else
3698 {
3699 dynamic = TRUE;
3700
3701 /* You can't use -r against a dynamic object. Also, there's no
3702 hope of using a dynamic object which does not exactly match
3703 the format of the output file. */
0e1862bb 3704 if (bfd_link_relocatable (info)
66eb6687 3705 || !is_elf_hash_table (htab)
f13a99db 3706 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3707 {
0e1862bb 3708 if (bfd_link_relocatable (info))
9a0789ec
NC
3709 bfd_set_error (bfd_error_invalid_operation);
3710 else
3711 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3712 goto error_return;
3713 }
3714 }
3715
a0c402a5
L
3716 ehdr = elf_elfheader (abfd);
3717 if (info->warn_alternate_em
3718 && bed->elf_machine_code != ehdr->e_machine
3719 && ((bed->elf_machine_alt1 != 0
3720 && ehdr->e_machine == bed->elf_machine_alt1)
3721 || (bed->elf_machine_alt2 != 0
3722 && ehdr->e_machine == bed->elf_machine_alt2)))
3723 info->callbacks->einfo
695344c0 3724 /* xgettext:c-format */
a0c402a5
L
3725 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3726 ehdr->e_machine, abfd, bed->elf_machine_code);
3727
4ad4eba5
AM
3728 /* As a GNU extension, any input sections which are named
3729 .gnu.warning.SYMBOL are treated as warning symbols for the given
3730 symbol. This differs from .gnu.warning sections, which generate
3731 warnings when they are included in an output file. */
dd98f8d2 3732 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3733 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3734 {
db6a5d5f 3735 const char *name;
4ad4eba5 3736
db6a5d5f
AM
3737 name = bfd_get_section_name (abfd, s);
3738 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3739 {
db6a5d5f
AM
3740 char *msg;
3741 bfd_size_type sz;
3742
3743 name += sizeof ".gnu.warning." - 1;
3744
3745 /* If this is a shared object, then look up the symbol
3746 in the hash table. If it is there, and it is already
3747 been defined, then we will not be using the entry
3748 from this shared object, so we don't need to warn.
3749 FIXME: If we see the definition in a regular object
3750 later on, we will warn, but we shouldn't. The only
3751 fix is to keep track of what warnings we are supposed
3752 to emit, and then handle them all at the end of the
3753 link. */
3754 if (dynamic)
4ad4eba5 3755 {
db6a5d5f
AM
3756 struct elf_link_hash_entry *h;
3757
3758 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3759
3760 /* FIXME: What about bfd_link_hash_common? */
3761 if (h != NULL
3762 && (h->root.type == bfd_link_hash_defined
3763 || h->root.type == bfd_link_hash_defweak))
3764 continue;
3765 }
4ad4eba5 3766
db6a5d5f
AM
3767 sz = s->size;
3768 msg = (char *) bfd_alloc (abfd, sz + 1);
3769 if (msg == NULL)
3770 goto error_return;
4ad4eba5 3771
db6a5d5f
AM
3772 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3773 goto error_return;
4ad4eba5 3774
db6a5d5f 3775 msg[sz] = '\0';
4ad4eba5 3776
db6a5d5f
AM
3777 if (! (_bfd_generic_link_add_one_symbol
3778 (info, abfd, name, BSF_WARNING, s, 0, msg,
3779 FALSE, bed->collect, NULL)))
3780 goto error_return;
4ad4eba5 3781
0e1862bb 3782 if (bfd_link_executable (info))
db6a5d5f
AM
3783 {
3784 /* Clobber the section size so that the warning does
3785 not get copied into the output file. */
3786 s->size = 0;
11d2f718 3787
db6a5d5f
AM
3788 /* Also set SEC_EXCLUDE, so that symbols defined in
3789 the warning section don't get copied to the output. */
3790 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3791 }
3792 }
3793 }
3794
29a9f53e
L
3795 just_syms = ((s = abfd->sections) != NULL
3796 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3797
4ad4eba5
AM
3798 add_needed = TRUE;
3799 if (! dynamic)
3800 {
3801 /* If we are creating a shared library, create all the dynamic
3802 sections immediately. We need to attach them to something,
3803 so we attach them to this BFD, provided it is the right
bf89386a
L
3804 format and is not from ld --just-symbols. Always create the
3805 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3806 are no input BFD's of the same format as the output, we can't
3807 make a shared library. */
3808 if (!just_syms
bf89386a 3809 && (bfd_link_pic (info)
9c1d7a08
L
3810 || (!bfd_link_relocatable (info)
3811 && (info->export_dynamic || info->dynamic)))
66eb6687 3812 && is_elf_hash_table (htab)
f13a99db 3813 && info->output_bfd->xvec == abfd->xvec
66eb6687 3814 && !htab->dynamic_sections_created)
4ad4eba5
AM
3815 {
3816 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3817 goto error_return;
3818 }
3819 }
66eb6687 3820 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3821 goto error_return;
3822 else
3823 {
4ad4eba5 3824 const char *soname = NULL;
7ee314fa 3825 char *audit = NULL;
4ad4eba5 3826 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3827 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3828 int ret;
3829
3830 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3831 ld shouldn't allow it. */
29a9f53e 3832 if (just_syms)
92fd189d 3833 abort ();
4ad4eba5
AM
3834
3835 /* If this dynamic lib was specified on the command line with
3836 --as-needed in effect, then we don't want to add a DT_NEEDED
3837 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3838 in by another lib's DT_NEEDED. When --no-add-needed is used
3839 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3840 any dynamic library in DT_NEEDED tags in the dynamic lib at
3841 all. */
3842 add_needed = (elf_dyn_lib_class (abfd)
3843 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3844 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3845
3846 s = bfd_get_section_by_name (abfd, ".dynamic");
3847 if (s != NULL)
3848 {
3849 bfd_byte *dynbuf;
3850 bfd_byte *extdyn;
cb33740c 3851 unsigned int elfsec;
4ad4eba5
AM
3852 unsigned long shlink;
3853
eea6121a 3854 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3855 {
3856error_free_dyn:
3857 free (dynbuf);
3858 goto error_return;
3859 }
4ad4eba5
AM
3860
3861 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3862 if (elfsec == SHN_BAD)
4ad4eba5
AM
3863 goto error_free_dyn;
3864 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3865
3866 for (extdyn = dynbuf;
eea6121a 3867 extdyn < dynbuf + s->size;
4ad4eba5
AM
3868 extdyn += bed->s->sizeof_dyn)
3869 {
3870 Elf_Internal_Dyn dyn;
3871
3872 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3873 if (dyn.d_tag == DT_SONAME)
3874 {
3875 unsigned int tagv = dyn.d_un.d_val;
3876 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3877 if (soname == NULL)
3878 goto error_free_dyn;
3879 }
3880 if (dyn.d_tag == DT_NEEDED)
3881 {
3882 struct bfd_link_needed_list *n, **pn;
3883 char *fnm, *anm;
3884 unsigned int tagv = dyn.d_un.d_val;
3885
3886 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3887 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3888 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3889 if (n == NULL || fnm == NULL)
3890 goto error_free_dyn;
3891 amt = strlen (fnm) + 1;
a50b1753 3892 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3893 if (anm == NULL)
3894 goto error_free_dyn;
3895 memcpy (anm, fnm, amt);
3896 n->name = anm;
3897 n->by = abfd;
3898 n->next = NULL;
66eb6687 3899 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3900 ;
3901 *pn = n;
3902 }
3903 if (dyn.d_tag == DT_RUNPATH)
3904 {
3905 struct bfd_link_needed_list *n, **pn;
3906 char *fnm, *anm;
3907 unsigned int tagv = dyn.d_un.d_val;
3908
3909 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3910 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3911 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3912 if (n == NULL || fnm == NULL)
3913 goto error_free_dyn;
3914 amt = strlen (fnm) + 1;
a50b1753 3915 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3916 if (anm == NULL)
3917 goto error_free_dyn;
3918 memcpy (anm, fnm, amt);
3919 n->name = anm;
3920 n->by = abfd;
3921 n->next = NULL;
3922 for (pn = & runpath;
3923 *pn != NULL;
3924 pn = &(*pn)->next)
3925 ;
3926 *pn = n;
3927 }
3928 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3929 if (!runpath && dyn.d_tag == DT_RPATH)
3930 {
3931 struct bfd_link_needed_list *n, **pn;
3932 char *fnm, *anm;
3933 unsigned int tagv = dyn.d_un.d_val;
3934
3935 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3936 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3937 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3938 if (n == NULL || fnm == NULL)
3939 goto error_free_dyn;
3940 amt = strlen (fnm) + 1;
a50b1753 3941 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3942 if (anm == NULL)
f8703194 3943 goto error_free_dyn;
4ad4eba5
AM
3944 memcpy (anm, fnm, amt);
3945 n->name = anm;
3946 n->by = abfd;
3947 n->next = NULL;
3948 for (pn = & rpath;
3949 *pn != NULL;
3950 pn = &(*pn)->next)
3951 ;
3952 *pn = n;
3953 }
7ee314fa
AM
3954 if (dyn.d_tag == DT_AUDIT)
3955 {
3956 unsigned int tagv = dyn.d_un.d_val;
3957 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3958 }
4ad4eba5
AM
3959 }
3960
3961 free (dynbuf);
3962 }
3963
3964 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3965 frees all more recently bfd_alloc'd blocks as well. */
3966 if (runpath)
3967 rpath = runpath;
3968
3969 if (rpath)
3970 {
3971 struct bfd_link_needed_list **pn;
66eb6687 3972 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3973 ;
3974 *pn = rpath;
3975 }
3976
9acc85a6
AM
3977 /* If we have a PT_GNU_RELRO program header, mark as read-only
3978 all sections contained fully therein. This makes relro
3979 shared library sections appear as they will at run-time. */
3980 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3981 while (--phdr >= elf_tdata (abfd)->phdr)
3982 if (phdr->p_type == PT_GNU_RELRO)
3983 {
3984 for (s = abfd->sections; s != NULL; s = s->next)
3985 if ((s->flags & SEC_ALLOC) != 0
3986 && s->vma >= phdr->p_vaddr
3987 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3988 s->flags |= SEC_READONLY;
3989 break;
3990 }
3991
4ad4eba5
AM
3992 /* We do not want to include any of the sections in a dynamic
3993 object in the output file. We hack by simply clobbering the
3994 list of sections in the BFD. This could be handled more
3995 cleanly by, say, a new section flag; the existing
3996 SEC_NEVER_LOAD flag is not the one we want, because that one
3997 still implies that the section takes up space in the output
3998 file. */
3999 bfd_section_list_clear (abfd);
4000
4ad4eba5
AM
4001 /* Find the name to use in a DT_NEEDED entry that refers to this
4002 object. If the object has a DT_SONAME entry, we use it.
4003 Otherwise, if the generic linker stuck something in
4004 elf_dt_name, we use that. Otherwise, we just use the file
4005 name. */
4006 if (soname == NULL || *soname == '\0')
4007 {
4008 soname = elf_dt_name (abfd);
4009 if (soname == NULL || *soname == '\0')
4010 soname = bfd_get_filename (abfd);
4011 }
4012
4013 /* Save the SONAME because sometimes the linker emulation code
4014 will need to know it. */
4015 elf_dt_name (abfd) = soname;
4016
7e9f0867 4017 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4018 if (ret < 0)
4019 goto error_return;
4020
4021 /* If we have already included this dynamic object in the
4022 link, just ignore it. There is no reason to include a
4023 particular dynamic object more than once. */
4024 if (ret > 0)
4025 return TRUE;
7ee314fa
AM
4026
4027 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4028 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4029 }
4030
4031 /* If this is a dynamic object, we always link against the .dynsym
4032 symbol table, not the .symtab symbol table. The dynamic linker
4033 will only see the .dynsym symbol table, so there is no reason to
4034 look at .symtab for a dynamic object. */
4035
4036 if (! dynamic || elf_dynsymtab (abfd) == 0)
4037 hdr = &elf_tdata (abfd)->symtab_hdr;
4038 else
4039 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4040
4041 symcount = hdr->sh_size / bed->s->sizeof_sym;
4042
4043 /* The sh_info field of the symtab header tells us where the
4044 external symbols start. We don't care about the local symbols at
4045 this point. */
4046 if (elf_bad_symtab (abfd))
4047 {
4048 extsymcount = symcount;
4049 extsymoff = 0;
4050 }
4051 else
4052 {
4053 extsymcount = symcount - hdr->sh_info;
4054 extsymoff = hdr->sh_info;
4055 }
4056
f45794cb 4057 sym_hash = elf_sym_hashes (abfd);
012b2306 4058 if (extsymcount != 0)
4ad4eba5
AM
4059 {
4060 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4061 NULL, NULL, NULL);
4062 if (isymbuf == NULL)
4063 goto error_return;
4064
4ad4eba5 4065 if (sym_hash == NULL)
012b2306
AM
4066 {
4067 /* We store a pointer to the hash table entry for each
4068 external symbol. */
ef53be89
AM
4069 amt = extsymcount;
4070 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4071 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4072 if (sym_hash == NULL)
4073 goto error_free_sym;
4074 elf_sym_hashes (abfd) = sym_hash;
4075 }
4ad4eba5
AM
4076 }
4077
4078 if (dynamic)
4079 {
4080 /* Read in any version definitions. */
fc0e6df6
PB
4081 if (!_bfd_elf_slurp_version_tables (abfd,
4082 info->default_imported_symver))
4ad4eba5
AM
4083 goto error_free_sym;
4084
4085 /* Read in the symbol versions, but don't bother to convert them
4086 to internal format. */
4087 if (elf_dynversym (abfd) != 0)
4088 {
4089 Elf_Internal_Shdr *versymhdr;
4090
4091 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4092 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4093 if (extversym == NULL)
4094 goto error_free_sym;
4095 amt = versymhdr->sh_size;
4096 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4097 || bfd_bread (extversym, amt, abfd) != amt)
4098 goto error_free_vers;
4099 }
4100 }
4101
66eb6687
AM
4102 /* If we are loading an as-needed shared lib, save the symbol table
4103 state before we start adding symbols. If the lib turns out
4104 to be unneeded, restore the state. */
4105 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4106 {
4107 unsigned int i;
4108 size_t entsize;
4109
4110 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4111 {
4112 struct bfd_hash_entry *p;
2de92251 4113 struct elf_link_hash_entry *h;
66eb6687
AM
4114
4115 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4116 {
4117 h = (struct elf_link_hash_entry *) p;
4118 entsize += htab->root.table.entsize;
4119 if (h->root.type == bfd_link_hash_warning)
4120 entsize += htab->root.table.entsize;
4121 }
66eb6687
AM
4122 }
4123
4124 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4125 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4126 if (old_tab == NULL)
4127 goto error_free_vers;
4128
4129 /* Remember the current objalloc pointer, so that all mem for
4130 symbols added can later be reclaimed. */
4131 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4132 if (alloc_mark == NULL)
4133 goto error_free_vers;
4134
5061a885
AM
4135 /* Make a special call to the linker "notice" function to
4136 tell it that we are about to handle an as-needed lib. */
e5034e59 4137 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4138 goto error_free_vers;
5061a885 4139
f45794cb
AM
4140 /* Clone the symbol table. Remember some pointers into the
4141 symbol table, and dynamic symbol count. */
4142 old_ent = (char *) old_tab + tabsize;
66eb6687 4143 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4144 old_undefs = htab->root.undefs;
4145 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4146 old_table = htab->root.table.table;
4147 old_size = htab->root.table.size;
4148 old_count = htab->root.table.count;
5b677558
AM
4149 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4150 if (old_strtab == NULL)
4151 goto error_free_vers;
66eb6687
AM
4152
4153 for (i = 0; i < htab->root.table.size; i++)
4154 {
4155 struct bfd_hash_entry *p;
2de92251 4156 struct elf_link_hash_entry *h;
66eb6687
AM
4157
4158 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4159 {
4160 memcpy (old_ent, p, htab->root.table.entsize);
4161 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4162 h = (struct elf_link_hash_entry *) p;
4163 if (h->root.type == bfd_link_hash_warning)
4164 {
4165 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4166 old_ent = (char *) old_ent + htab->root.table.entsize;
4167 }
66eb6687
AM
4168 }
4169 }
4170 }
4ad4eba5 4171
66eb6687 4172 weaks = NULL;
4ad4eba5
AM
4173 ever = extversym != NULL ? extversym + extsymoff : NULL;
4174 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4175 isym < isymend;
4176 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4177 {
4178 int bind;
4179 bfd_vma value;
af44c138 4180 asection *sec, *new_sec;
4ad4eba5
AM
4181 flagword flags;
4182 const char *name;
4183 struct elf_link_hash_entry *h;
90c984fc 4184 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4185 bfd_boolean definition;
4186 bfd_boolean size_change_ok;
4187 bfd_boolean type_change_ok;
4188 bfd_boolean new_weakdef;
37a9e49a
L
4189 bfd_boolean new_weak;
4190 bfd_boolean old_weak;
4ad4eba5 4191 bfd_boolean override;
a4d8e49b 4192 bfd_boolean common;
97196564 4193 bfd_boolean discarded;
4ad4eba5
AM
4194 unsigned int old_alignment;
4195 bfd *old_bfd;
6e33951e 4196 bfd_boolean matched;
4ad4eba5
AM
4197
4198 override = FALSE;
4199
4200 flags = BSF_NO_FLAGS;
4201 sec = NULL;
4202 value = isym->st_value;
a4d8e49b 4203 common = bed->common_definition (isym);
97196564 4204 discarded = FALSE;
4ad4eba5
AM
4205
4206 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4207 switch (bind)
4ad4eba5 4208 {
3e7a7d11 4209 case STB_LOCAL:
4ad4eba5
AM
4210 /* This should be impossible, since ELF requires that all
4211 global symbols follow all local symbols, and that sh_info
4212 point to the first global symbol. Unfortunately, Irix 5
4213 screws this up. */
4214 continue;
3e7a7d11
NC
4215
4216 case STB_GLOBAL:
a4d8e49b 4217 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4218 flags = BSF_GLOBAL;
3e7a7d11
NC
4219 break;
4220
4221 case STB_WEAK:
4222 flags = BSF_WEAK;
4223 break;
4224
4225 case STB_GNU_UNIQUE:
4226 flags = BSF_GNU_UNIQUE;
4227 break;
4228
4229 default:
4ad4eba5 4230 /* Leave it up to the processor backend. */
3e7a7d11 4231 break;
4ad4eba5
AM
4232 }
4233
4234 if (isym->st_shndx == SHN_UNDEF)
4235 sec = bfd_und_section_ptr;
cb33740c
AM
4236 else if (isym->st_shndx == SHN_ABS)
4237 sec = bfd_abs_section_ptr;
4238 else if (isym->st_shndx == SHN_COMMON)
4239 {
4240 sec = bfd_com_section_ptr;
4241 /* What ELF calls the size we call the value. What ELF
4242 calls the value we call the alignment. */
4243 value = isym->st_size;
4244 }
4245 else
4ad4eba5
AM
4246 {
4247 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4248 if (sec == NULL)
4249 sec = bfd_abs_section_ptr;
dbaa2011 4250 else if (discarded_section (sec))
529fcb95 4251 {
e5d08002
L
4252 /* Symbols from discarded section are undefined. We keep
4253 its visibility. */
529fcb95 4254 sec = bfd_und_section_ptr;
97196564 4255 discarded = TRUE;
529fcb95
PB
4256 isym->st_shndx = SHN_UNDEF;
4257 }
4ad4eba5
AM
4258 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4259 value -= sec->vma;
4260 }
4ad4eba5
AM
4261
4262 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4263 isym->st_name);
4264 if (name == NULL)
4265 goto error_free_vers;
4266
4267 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4268 && (abfd->flags & BFD_PLUGIN) != 0)
4269 {
4270 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4271
4272 if (xc == NULL)
4273 {
4274 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4275 | SEC_EXCLUDE);
4276 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4277 if (xc == NULL)
4278 goto error_free_vers;
4279 }
4280 sec = xc;
4281 }
4282 else if (isym->st_shndx == SHN_COMMON
4283 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4284 && !bfd_link_relocatable (info))
4ad4eba5
AM
4285 {
4286 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4287
4288 if (tcomm == NULL)
4289 {
02d00247
AM
4290 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4291 | SEC_LINKER_CREATED);
4292 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4293 if (tcomm == NULL)
4ad4eba5
AM
4294 goto error_free_vers;
4295 }
4296 sec = tcomm;
4297 }
66eb6687 4298 else if (bed->elf_add_symbol_hook)
4ad4eba5 4299 {
66eb6687
AM
4300 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4301 &sec, &value))
4ad4eba5
AM
4302 goto error_free_vers;
4303
4304 /* The hook function sets the name to NULL if this symbol
4305 should be skipped for some reason. */
4306 if (name == NULL)
4307 continue;
4308 }
4309
4310 /* Sanity check that all possibilities were handled. */
4311 if (sec == NULL)
4312 {
4313 bfd_set_error (bfd_error_bad_value);
4314 goto error_free_vers;
4315 }
4316
191c0c42
AM
4317 /* Silently discard TLS symbols from --just-syms. There's
4318 no way to combine a static TLS block with a new TLS block
4319 for this executable. */
4320 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4321 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4322 continue;
4323
4ad4eba5
AM
4324 if (bfd_is_und_section (sec)
4325 || bfd_is_com_section (sec))
4326 definition = FALSE;
4327 else
4328 definition = TRUE;
4329
4330 size_change_ok = FALSE;
66eb6687 4331 type_change_ok = bed->type_change_ok;
37a9e49a 4332 old_weak = FALSE;
6e33951e 4333 matched = FALSE;
4ad4eba5
AM
4334 old_alignment = 0;
4335 old_bfd = NULL;
af44c138 4336 new_sec = sec;
4ad4eba5 4337
66eb6687 4338 if (is_elf_hash_table (htab))
4ad4eba5
AM
4339 {
4340 Elf_Internal_Versym iver;
4341 unsigned int vernum = 0;
4342 bfd_boolean skip;
4343
fc0e6df6 4344 if (ever == NULL)
4ad4eba5 4345 {
fc0e6df6
PB
4346 if (info->default_imported_symver)
4347 /* Use the default symbol version created earlier. */
4348 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4349 else
4350 iver.vs_vers = 0;
4351 }
4352 else
4353 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4354
4355 vernum = iver.vs_vers & VERSYM_VERSION;
4356
4357 /* If this is a hidden symbol, or if it is not version
4358 1, we append the version name to the symbol name.
cc86ff91
EB
4359 However, we do not modify a non-hidden absolute symbol
4360 if it is not a function, because it might be the version
4361 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4362 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4363 || (vernum > 1
4364 && (!bfd_is_abs_section (sec)
4365 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4366 {
4367 const char *verstr;
4368 size_t namelen, verlen, newlen;
4369 char *newname, *p;
4370
4371 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4372 {
fc0e6df6
PB
4373 if (vernum > elf_tdata (abfd)->cverdefs)
4374 verstr = NULL;
4375 else if (vernum > 1)
4376 verstr =
4377 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4378 else
4379 verstr = "";
4ad4eba5 4380
fc0e6df6 4381 if (verstr == NULL)
4ad4eba5 4382 {
4eca0228 4383 _bfd_error_handler
695344c0 4384 /* xgettext:c-format */
fc0e6df6
PB
4385 (_("%B: %s: invalid version %u (max %d)"),
4386 abfd, name, vernum,
4387 elf_tdata (abfd)->cverdefs);
4388 bfd_set_error (bfd_error_bad_value);
4389 goto error_free_vers;
4ad4eba5 4390 }
fc0e6df6
PB
4391 }
4392 else
4393 {
4394 /* We cannot simply test for the number of
4395 entries in the VERNEED section since the
4396 numbers for the needed versions do not start
4397 at 0. */
4398 Elf_Internal_Verneed *t;
4399
4400 verstr = NULL;
4401 for (t = elf_tdata (abfd)->verref;
4402 t != NULL;
4403 t = t->vn_nextref)
4ad4eba5 4404 {
fc0e6df6 4405 Elf_Internal_Vernaux *a;
4ad4eba5 4406
fc0e6df6
PB
4407 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4408 {
4409 if (a->vna_other == vernum)
4ad4eba5 4410 {
fc0e6df6
PB
4411 verstr = a->vna_nodename;
4412 break;
4ad4eba5 4413 }
4ad4eba5 4414 }
fc0e6df6
PB
4415 if (a != NULL)
4416 break;
4417 }
4418 if (verstr == NULL)
4419 {
4eca0228 4420 _bfd_error_handler
695344c0 4421 /* xgettext:c-format */
fc0e6df6
PB
4422 (_("%B: %s: invalid needed version %d"),
4423 abfd, name, vernum);
4424 bfd_set_error (bfd_error_bad_value);
4425 goto error_free_vers;
4ad4eba5 4426 }
4ad4eba5 4427 }
fc0e6df6
PB
4428
4429 namelen = strlen (name);
4430 verlen = strlen (verstr);
4431 newlen = namelen + verlen + 2;
4432 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4433 && isym->st_shndx != SHN_UNDEF)
4434 ++newlen;
4435
a50b1753 4436 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4437 if (newname == NULL)
4438 goto error_free_vers;
4439 memcpy (newname, name, namelen);
4440 p = newname + namelen;
4441 *p++ = ELF_VER_CHR;
4442 /* If this is a defined non-hidden version symbol,
4443 we add another @ to the name. This indicates the
4444 default version of the symbol. */
4445 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4446 && isym->st_shndx != SHN_UNDEF)
4447 *p++ = ELF_VER_CHR;
4448 memcpy (p, verstr, verlen + 1);
4449
4450 name = newname;
4ad4eba5
AM
4451 }
4452
cd3416da
AM
4453 /* If this symbol has default visibility and the user has
4454 requested we not re-export it, then mark it as hidden. */
a0d49154 4455 if (!bfd_is_und_section (sec)
cd3416da 4456 && !dynamic
ce875075 4457 && abfd->no_export
cd3416da
AM
4458 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4459 isym->st_other = (STV_HIDDEN
4460 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4461
4f3fedcf
AM
4462 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4463 sym_hash, &old_bfd, &old_weak,
4464 &old_alignment, &skip, &override,
6e33951e
L
4465 &type_change_ok, &size_change_ok,
4466 &matched))
4ad4eba5
AM
4467 goto error_free_vers;
4468
4469 if (skip)
4470 continue;
4471
6e33951e
L
4472 /* Override a definition only if the new symbol matches the
4473 existing one. */
4474 if (override && matched)
4ad4eba5
AM
4475 definition = FALSE;
4476
4477 h = *sym_hash;
4478 while (h->root.type == bfd_link_hash_indirect
4479 || h->root.type == bfd_link_hash_warning)
4480 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4481
4ad4eba5 4482 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4483 && vernum > 1
4484 && definition)
4485 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4486 }
4487
4488 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4489 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4490 (struct bfd_link_hash_entry **) sym_hash)))
4491 goto error_free_vers;
4492
a43942db
MR
4493 if ((flags & BSF_GNU_UNIQUE)
4494 && (abfd->flags & DYNAMIC) == 0
4495 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4496 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4497
4ad4eba5 4498 h = *sym_hash;
90c984fc
L
4499 /* We need to make sure that indirect symbol dynamic flags are
4500 updated. */
4501 hi = h;
4ad4eba5
AM
4502 while (h->root.type == bfd_link_hash_indirect
4503 || h->root.type == bfd_link_hash_warning)
4504 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4505
97196564
L
4506 /* Setting the index to -3 tells elf_link_output_extsym that
4507 this symbol is defined in a discarded section. */
4508 if (discarded)
4509 h->indx = -3;
4510
4ad4eba5
AM
4511 *sym_hash = h;
4512
37a9e49a 4513 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4514 new_weakdef = FALSE;
4515 if (dynamic
4516 && definition
37a9e49a 4517 && new_weak
fcb93ecf 4518 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4519 && is_elf_hash_table (htab)
f6e332e6 4520 && h->u.weakdef == NULL)
4ad4eba5
AM
4521 {
4522 /* Keep a list of all weak defined non function symbols from
4523 a dynamic object, using the weakdef field. Later in this
4524 function we will set the weakdef field to the correct
4525 value. We only put non-function symbols from dynamic
4526 objects on this list, because that happens to be the only
4527 time we need to know the normal symbol corresponding to a
4528 weak symbol, and the information is time consuming to
4529 figure out. If the weakdef field is not already NULL,
4530 then this symbol was already defined by some previous
4531 dynamic object, and we will be using that previous
4532 definition anyhow. */
4533
f6e332e6 4534 h->u.weakdef = weaks;
4ad4eba5
AM
4535 weaks = h;
4536 new_weakdef = TRUE;
4537 }
4538
4539 /* Set the alignment of a common symbol. */
a4d8e49b 4540 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4541 && h->root.type == bfd_link_hash_common)
4542 {
4543 unsigned int align;
4544
a4d8e49b 4545 if (common)
af44c138
L
4546 align = bfd_log2 (isym->st_value);
4547 else
4548 {
4549 /* The new symbol is a common symbol in a shared object.
4550 We need to get the alignment from the section. */
4551 align = new_sec->alignment_power;
4552 }
595213d4 4553 if (align > old_alignment)
4ad4eba5
AM
4554 h->root.u.c.p->alignment_power = align;
4555 else
4556 h->root.u.c.p->alignment_power = old_alignment;
4557 }
4558
66eb6687 4559 if (is_elf_hash_table (htab))
4ad4eba5 4560 {
4f3fedcf
AM
4561 /* Set a flag in the hash table entry indicating the type of
4562 reference or definition we just found. A dynamic symbol
4563 is one which is referenced or defined by both a regular
4564 object and a shared object. */
4565 bfd_boolean dynsym = FALSE;
4566
4567 /* Plugin symbols aren't normal. Don't set def_regular or
4568 ref_regular for them, or make them dynamic. */
4569 if ((abfd->flags & BFD_PLUGIN) != 0)
4570 ;
4571 else if (! dynamic)
4572 {
4573 if (! definition)
4574 {
4575 h->ref_regular = 1;
4576 if (bind != STB_WEAK)
4577 h->ref_regular_nonweak = 1;
4578 }
4579 else
4580 {
4581 h->def_regular = 1;
4582 if (h->def_dynamic)
4583 {
4584 h->def_dynamic = 0;
4585 h->ref_dynamic = 1;
4586 }
4587 }
4588
4589 /* If the indirect symbol has been forced local, don't
4590 make the real symbol dynamic. */
4591 if ((h == hi || !hi->forced_local)
0e1862bb 4592 && (bfd_link_dll (info)
4f3fedcf
AM
4593 || h->def_dynamic
4594 || h->ref_dynamic))
4595 dynsym = TRUE;
4596 }
4597 else
4598 {
4599 if (! definition)
4600 {
4601 h->ref_dynamic = 1;
4602 hi->ref_dynamic = 1;
4603 }
4604 else
4605 {
4606 h->def_dynamic = 1;
4607 hi->def_dynamic = 1;
4608 }
4609
4610 /* If the indirect symbol has been forced local, don't
4611 make the real symbol dynamic. */
4612 if ((h == hi || !hi->forced_local)
4613 && (h->def_regular
4614 || h->ref_regular
4615 || (h->u.weakdef != NULL
4616 && ! new_weakdef
4617 && h->u.weakdef->dynindx != -1)))
4618 dynsym = TRUE;
4619 }
4620
4621 /* Check to see if we need to add an indirect symbol for
4622 the default name. */
4623 if (definition
4624 || (!override && h->root.type == bfd_link_hash_common))
4625 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4626 sec, value, &old_bfd, &dynsym))
4627 goto error_free_vers;
4ad4eba5
AM
4628
4629 /* Check the alignment when a common symbol is involved. This
4630 can change when a common symbol is overridden by a normal
4631 definition or a common symbol is ignored due to the old
4632 normal definition. We need to make sure the maximum
4633 alignment is maintained. */
a4d8e49b 4634 if ((old_alignment || common)
4ad4eba5
AM
4635 && h->root.type != bfd_link_hash_common)
4636 {
4637 unsigned int common_align;
4638 unsigned int normal_align;
4639 unsigned int symbol_align;
4640 bfd *normal_bfd;
4641 bfd *common_bfd;
4642
3a81e825
AM
4643 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4644 || h->root.type == bfd_link_hash_defweak);
4645
4ad4eba5
AM
4646 symbol_align = ffs (h->root.u.def.value) - 1;
4647 if (h->root.u.def.section->owner != NULL
0616a280
AM
4648 && (h->root.u.def.section->owner->flags
4649 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4650 {
4651 normal_align = h->root.u.def.section->alignment_power;
4652 if (normal_align > symbol_align)
4653 normal_align = symbol_align;
4654 }
4655 else
4656 normal_align = symbol_align;
4657
4658 if (old_alignment)
4659 {
4660 common_align = old_alignment;
4661 common_bfd = old_bfd;
4662 normal_bfd = abfd;
4663 }
4664 else
4665 {
4666 common_align = bfd_log2 (isym->st_value);
4667 common_bfd = abfd;
4668 normal_bfd = old_bfd;
4669 }
4670
4671 if (normal_align < common_align)
d07676f8
NC
4672 {
4673 /* PR binutils/2735 */
4674 if (normal_bfd == NULL)
4eca0228 4675 _bfd_error_handler
695344c0 4676 /* xgettext:c-format */
4f3fedcf
AM
4677 (_("Warning: alignment %u of common symbol `%s' in %B is"
4678 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4679 common_bfd, h->root.u.def.section,
4680 1 << common_align, name, 1 << normal_align);
4681 else
4eca0228 4682 _bfd_error_handler
695344c0 4683 /* xgettext:c-format */
d07676f8
NC
4684 (_("Warning: alignment %u of symbol `%s' in %B"
4685 " is smaller than %u in %B"),
4686 normal_bfd, common_bfd,
4687 1 << normal_align, name, 1 << common_align);
4688 }
4ad4eba5
AM
4689 }
4690
83ad0046 4691 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4692 if (isym->st_size != 0
4693 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4694 && (definition || h->size == 0))
4695 {
83ad0046
L
4696 if (h->size != 0
4697 && h->size != isym->st_size
4698 && ! size_change_ok)
4eca0228 4699 _bfd_error_handler
695344c0 4700 /* xgettext:c-format */
d003868e
AM
4701 (_("Warning: size of symbol `%s' changed"
4702 " from %lu in %B to %lu in %B"),
4703 old_bfd, abfd,
4ad4eba5 4704 name, (unsigned long) h->size,
d003868e 4705 (unsigned long) isym->st_size);
4ad4eba5
AM
4706
4707 h->size = isym->st_size;
4708 }
4709
4710 /* If this is a common symbol, then we always want H->SIZE
4711 to be the size of the common symbol. The code just above
4712 won't fix the size if a common symbol becomes larger. We
4713 don't warn about a size change here, because that is
4f3fedcf 4714 covered by --warn-common. Allow changes between different
fcb93ecf 4715 function types. */
4ad4eba5
AM
4716 if (h->root.type == bfd_link_hash_common)
4717 h->size = h->root.u.c.size;
4718
4719 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4720 && ((definition && !new_weak)
4721 || (old_weak && h->root.type == bfd_link_hash_common)
4722 || h->type == STT_NOTYPE))
4ad4eba5 4723 {
2955ec4c
L
4724 unsigned int type = ELF_ST_TYPE (isym->st_info);
4725
4726 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4727 symbol. */
4728 if (type == STT_GNU_IFUNC
4729 && (abfd->flags & DYNAMIC) != 0)
4730 type = STT_FUNC;
4ad4eba5 4731
2955ec4c
L
4732 if (h->type != type)
4733 {
4734 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4735 /* xgettext:c-format */
4eca0228 4736 _bfd_error_handler
2955ec4c
L
4737 (_("Warning: type of symbol `%s' changed"
4738 " from %d to %d in %B"),
4739 abfd, name, h->type, type);
4740
4741 h->type = type;
4742 }
4ad4eba5
AM
4743 }
4744
54ac0771 4745 /* Merge st_other field. */
b8417128 4746 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4747
c3df8c14 4748 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4749 if (definition
4750 && (sec->flags & SEC_DEBUGGING)
4751 && !bfd_link_relocatable (info))
c3df8c14
AM
4752 dynsym = FALSE;
4753
4f3fedcf
AM
4754 /* Nor should we make plugin symbols dynamic. */
4755 if ((abfd->flags & BFD_PLUGIN) != 0)
4756 dynsym = FALSE;
4757
35fc36a8 4758 if (definition)
35399224
L
4759 {
4760 h->target_internal = isym->st_target_internal;
4761 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4762 }
35fc36a8 4763
4ad4eba5
AM
4764 if (definition && !dynamic)
4765 {
4766 char *p = strchr (name, ELF_VER_CHR);
4767 if (p != NULL && p[1] != ELF_VER_CHR)
4768 {
4769 /* Queue non-default versions so that .symver x, x@FOO
4770 aliases can be checked. */
66eb6687 4771 if (!nondeflt_vers)
4ad4eba5 4772 {
66eb6687
AM
4773 amt = ((isymend - isym + 1)
4774 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4775 nondeflt_vers
4776 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4777 if (!nondeflt_vers)
4778 goto error_free_vers;
4ad4eba5 4779 }
66eb6687 4780 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4781 }
4782 }
4783
4784 if (dynsym && h->dynindx == -1)
4785 {
c152c796 4786 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4787 goto error_free_vers;
f6e332e6 4788 if (h->u.weakdef != NULL
4ad4eba5 4789 && ! new_weakdef
f6e332e6 4790 && h->u.weakdef->dynindx == -1)
4ad4eba5 4791 {
66eb6687 4792 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4793 goto error_free_vers;
4794 }
4795 }
1f599d0e 4796 else if (h->dynindx != -1)
4ad4eba5
AM
4797 /* If the symbol already has a dynamic index, but
4798 visibility says it should not be visible, turn it into
4799 a local symbol. */
4800 switch (ELF_ST_VISIBILITY (h->other))
4801 {
4802 case STV_INTERNAL:
4803 case STV_HIDDEN:
4804 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4805 dynsym = FALSE;
4806 break;
4807 }
4808
aef28989
L
4809 /* Don't add DT_NEEDED for references from the dummy bfd nor
4810 for unmatched symbol. */
4ad4eba5 4811 if (!add_needed
aef28989 4812 && matched
4ad4eba5 4813 && definition
010e5ae2 4814 && ((dynsym
ffa9430d 4815 && h->ref_regular_nonweak
4f3fedcf
AM
4816 && (old_bfd == NULL
4817 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4818 || (h->ref_dynamic_nonweak
010e5ae2 4819 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4820 && !on_needed_list (elf_dt_name (abfd),
4821 htab->needed, NULL))))
4ad4eba5
AM
4822 {
4823 int ret;
4824 const char *soname = elf_dt_name (abfd);
4825
16e4ecc0
AM
4826 info->callbacks->minfo ("%!", soname, old_bfd,
4827 h->root.root.string);
4828
4ad4eba5
AM
4829 /* A symbol from a library loaded via DT_NEEDED of some
4830 other library is referenced by a regular object.
e56f61be 4831 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4832 --no-add-needed is used and the reference was not
4833 a weak one. */
4f3fedcf 4834 if (old_bfd != NULL
b918acf9 4835 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4836 {
4eca0228 4837 _bfd_error_handler
695344c0 4838 /* xgettext:c-format */
3cbc5de0 4839 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4840 old_bfd, name);
ff5ac77b 4841 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4842 goto error_free_vers;
4843 }
4844
a50b1753 4845 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4846 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4847
4ad4eba5 4848 add_needed = TRUE;
7e9f0867 4849 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4850 if (ret < 0)
4851 goto error_free_vers;
4852
4853 BFD_ASSERT (ret == 0);
4854 }
4855 }
4856 }
4857
66eb6687
AM
4858 if (extversym != NULL)
4859 {
4860 free (extversym);
4861 extversym = NULL;
4862 }
4863
4864 if (isymbuf != NULL)
4865 {
4866 free (isymbuf);
4867 isymbuf = NULL;
4868 }
4869
4870 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4871 {
4872 unsigned int i;
4873
4874 /* Restore the symbol table. */
f45794cb
AM
4875 old_ent = (char *) old_tab + tabsize;
4876 memset (elf_sym_hashes (abfd), 0,
4877 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4878 htab->root.table.table = old_table;
4879 htab->root.table.size = old_size;
4880 htab->root.table.count = old_count;
66eb6687 4881 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4882 htab->root.undefs = old_undefs;
4883 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4884 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4885 free (old_strtab);
4886 old_strtab = NULL;
66eb6687
AM
4887 for (i = 0; i < htab->root.table.size; i++)
4888 {
4889 struct bfd_hash_entry *p;
4890 struct elf_link_hash_entry *h;
3e0882af
L
4891 bfd_size_type size;
4892 unsigned int alignment_power;
66eb6687
AM
4893
4894 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4895 {
4896 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4897 if (h->root.type == bfd_link_hash_warning)
4898 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4899
3e0882af
L
4900 /* Preserve the maximum alignment and size for common
4901 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4902 since it can still be loaded at run time by another
3e0882af
L
4903 dynamic lib. */
4904 if (h->root.type == bfd_link_hash_common)
4905 {
4906 size = h->root.u.c.size;
4907 alignment_power = h->root.u.c.p->alignment_power;
4908 }
4909 else
4910 {
4911 size = 0;
4912 alignment_power = 0;
4913 }
66eb6687
AM
4914 memcpy (p, old_ent, htab->root.table.entsize);
4915 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4916 h = (struct elf_link_hash_entry *) p;
4917 if (h->root.type == bfd_link_hash_warning)
4918 {
4919 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4920 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4921 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4922 }
a4542f1b 4923 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4924 {
4925 if (size > h->root.u.c.size)
4926 h->root.u.c.size = size;
4927 if (alignment_power > h->root.u.c.p->alignment_power)
4928 h->root.u.c.p->alignment_power = alignment_power;
4929 }
66eb6687
AM
4930 }
4931 }
4932
5061a885
AM
4933 /* Make a special call to the linker "notice" function to
4934 tell it that symbols added for crefs may need to be removed. */
e5034e59 4935 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4936 goto error_free_vers;
5061a885 4937
66eb6687
AM
4938 free (old_tab);
4939 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4940 alloc_mark);
4941 if (nondeflt_vers != NULL)
4942 free (nondeflt_vers);
4943 return TRUE;
4944 }
2de92251 4945
66eb6687
AM
4946 if (old_tab != NULL)
4947 {
e5034e59 4948 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4949 goto error_free_vers;
66eb6687
AM
4950 free (old_tab);
4951 old_tab = NULL;
4952 }
4953
c6e8a9a8
L
4954 /* Now that all the symbols from this input file are created, if
4955 not performing a relocatable link, handle .symver foo, foo@BAR
4956 such that any relocs against foo become foo@BAR. */
0e1862bb 4957 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4958 {
ef53be89 4959 size_t cnt, symidx;
4ad4eba5
AM
4960
4961 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4962 {
4963 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4964 char *shortname, *p;
4965
4966 p = strchr (h->root.root.string, ELF_VER_CHR);
4967 if (p == NULL
4968 || (h->root.type != bfd_link_hash_defined
4969 && h->root.type != bfd_link_hash_defweak))
4970 continue;
4971
4972 amt = p - h->root.root.string;
a50b1753 4973 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4974 if (!shortname)
4975 goto error_free_vers;
4ad4eba5
AM
4976 memcpy (shortname, h->root.root.string, amt);
4977 shortname[amt] = '\0';
4978
4979 hi = (struct elf_link_hash_entry *)
66eb6687 4980 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4981 FALSE, FALSE, FALSE);
4982 if (hi != NULL
4983 && hi->root.type == h->root.type
4984 && hi->root.u.def.value == h->root.u.def.value
4985 && hi->root.u.def.section == h->root.u.def.section)
4986 {
4987 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4988 hi->root.type = bfd_link_hash_indirect;
4989 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4990 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4991 sym_hash = elf_sym_hashes (abfd);
4992 if (sym_hash)
4993 for (symidx = 0; symidx < extsymcount; ++symidx)
4994 if (sym_hash[symidx] == hi)
4995 {
4996 sym_hash[symidx] = h;
4997 break;
4998 }
4999 }
5000 free (shortname);
5001 }
5002 free (nondeflt_vers);
5003 nondeflt_vers = NULL;
5004 }
5005
4ad4eba5
AM
5006 /* Now set the weakdefs field correctly for all the weak defined
5007 symbols we found. The only way to do this is to search all the
5008 symbols. Since we only need the information for non functions in
5009 dynamic objects, that's the only time we actually put anything on
5010 the list WEAKS. We need this information so that if a regular
5011 object refers to a symbol defined weakly in a dynamic object, the
5012 real symbol in the dynamic object is also put in the dynamic
5013 symbols; we also must arrange for both symbols to point to the
5014 same memory location. We could handle the general case of symbol
5015 aliasing, but a general symbol alias can only be generated in
5016 assembler code, handling it correctly would be very time
5017 consuming, and other ELF linkers don't handle general aliasing
5018 either. */
5019 if (weaks != NULL)
5020 {
5021 struct elf_link_hash_entry **hpp;
5022 struct elf_link_hash_entry **hppend;
5023 struct elf_link_hash_entry **sorted_sym_hash;
5024 struct elf_link_hash_entry *h;
5025 size_t sym_count;
5026
5027 /* Since we have to search the whole symbol list for each weak
5028 defined symbol, search time for N weak defined symbols will be
5029 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5030 amt = extsymcount;
5031 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5032 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5033 if (sorted_sym_hash == NULL)
5034 goto error_return;
5035 sym_hash = sorted_sym_hash;
5036 hpp = elf_sym_hashes (abfd);
5037 hppend = hpp + extsymcount;
5038 sym_count = 0;
5039 for (; hpp < hppend; hpp++)
5040 {
5041 h = *hpp;
5042 if (h != NULL
5043 && h->root.type == bfd_link_hash_defined
fcb93ecf 5044 && !bed->is_function_type (h->type))
4ad4eba5
AM
5045 {
5046 *sym_hash = h;
5047 sym_hash++;
5048 sym_count++;
5049 }
5050 }
5051
5052 qsort (sorted_sym_hash, sym_count,
5053 sizeof (struct elf_link_hash_entry *),
5054 elf_sort_symbol);
5055
5056 while (weaks != NULL)
5057 {
5058 struct elf_link_hash_entry *hlook;
5059 asection *slook;
5060 bfd_vma vlook;
ed54588d 5061 size_t i, j, idx = 0;
4ad4eba5
AM
5062
5063 hlook = weaks;
f6e332e6
AM
5064 weaks = hlook->u.weakdef;
5065 hlook->u.weakdef = NULL;
4ad4eba5
AM
5066
5067 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5068 || hlook->root.type == bfd_link_hash_defweak
5069 || hlook->root.type == bfd_link_hash_common
5070 || hlook->root.type == bfd_link_hash_indirect);
5071 slook = hlook->root.u.def.section;
5072 vlook = hlook->root.u.def.value;
5073
4ad4eba5
AM
5074 i = 0;
5075 j = sym_count;
14160578 5076 while (i != j)
4ad4eba5
AM
5077 {
5078 bfd_signed_vma vdiff;
5079 idx = (i + j) / 2;
14160578 5080 h = sorted_sym_hash[idx];
4ad4eba5
AM
5081 vdiff = vlook - h->root.u.def.value;
5082 if (vdiff < 0)
5083 j = idx;
5084 else if (vdiff > 0)
5085 i = idx + 1;
5086 else
5087 {
d3435ae8 5088 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5089 if (sdiff < 0)
5090 j = idx;
5091 else if (sdiff > 0)
5092 i = idx + 1;
5093 else
14160578 5094 break;
4ad4eba5
AM
5095 }
5096 }
5097
5098 /* We didn't find a value/section match. */
14160578 5099 if (i == j)
4ad4eba5
AM
5100 continue;
5101
14160578
AM
5102 /* With multiple aliases, or when the weak symbol is already
5103 strongly defined, we have multiple matching symbols and
5104 the binary search above may land on any of them. Step
5105 one past the matching symbol(s). */
5106 while (++idx != j)
5107 {
5108 h = sorted_sym_hash[idx];
5109 if (h->root.u.def.section != slook
5110 || h->root.u.def.value != vlook)
5111 break;
5112 }
5113
5114 /* Now look back over the aliases. Since we sorted by size
5115 as well as value and section, we'll choose the one with
5116 the largest size. */
5117 while (idx-- != i)
4ad4eba5 5118 {
14160578 5119 h = sorted_sym_hash[idx];
4ad4eba5
AM
5120
5121 /* Stop if value or section doesn't match. */
14160578
AM
5122 if (h->root.u.def.section != slook
5123 || h->root.u.def.value != vlook)
4ad4eba5
AM
5124 break;
5125 else if (h != hlook)
5126 {
f6e332e6 5127 hlook->u.weakdef = h;
4ad4eba5
AM
5128
5129 /* If the weak definition is in the list of dynamic
5130 symbols, make sure the real definition is put
5131 there as well. */
5132 if (hlook->dynindx != -1 && h->dynindx == -1)
5133 {
c152c796 5134 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5135 {
5136 err_free_sym_hash:
5137 free (sorted_sym_hash);
5138 goto error_return;
5139 }
4ad4eba5
AM
5140 }
5141
5142 /* If the real definition is in the list of dynamic
5143 symbols, make sure the weak definition is put
5144 there as well. If we don't do this, then the
5145 dynamic loader might not merge the entries for the
5146 real definition and the weak definition. */
5147 if (h->dynindx != -1 && hlook->dynindx == -1)
5148 {
c152c796 5149 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5150 goto err_free_sym_hash;
4ad4eba5
AM
5151 }
5152 break;
5153 }
5154 }
5155 }
5156
5157 free (sorted_sym_hash);
5158 }
5159
33177bb1
AM
5160 if (bed->check_directives
5161 && !(*bed->check_directives) (abfd, info))
5162 return FALSE;
85fbca6a 5163
d9689752
L
5164 if (!info->check_relocs_after_open_input
5165 && !_bfd_elf_link_check_relocs (abfd, info))
5166 return FALSE;
4ad4eba5
AM
5167
5168 /* If this is a non-traditional link, try to optimize the handling
5169 of the .stab/.stabstr sections. */
5170 if (! dynamic
5171 && ! info->traditional_format
66eb6687 5172 && is_elf_hash_table (htab)
4ad4eba5
AM
5173 && (info->strip != strip_all && info->strip != strip_debugger))
5174 {
5175 asection *stabstr;
5176
5177 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5178 if (stabstr != NULL)
5179 {
5180 bfd_size_type string_offset = 0;
5181 asection *stab;
5182
5183 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5184 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5185 && (!stab->name[5] ||
5186 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5187 && (stab->flags & SEC_MERGE) == 0
5188 && !bfd_is_abs_section (stab->output_section))
5189 {
5190 struct bfd_elf_section_data *secdata;
5191
5192 secdata = elf_section_data (stab);
66eb6687
AM
5193 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5194 stabstr, &secdata->sec_info,
4ad4eba5
AM
5195 &string_offset))
5196 goto error_return;
5197 if (secdata->sec_info)
dbaa2011 5198 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5199 }
5200 }
5201 }
5202
66eb6687 5203 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5204 {
5205 /* Add this bfd to the loaded list. */
5206 struct elf_link_loaded_list *n;
5207
ca4be51c 5208 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5209 if (n == NULL)
5210 goto error_return;
5211 n->abfd = abfd;
66eb6687
AM
5212 n->next = htab->loaded;
5213 htab->loaded = n;
4ad4eba5
AM
5214 }
5215
5216 return TRUE;
5217
5218 error_free_vers:
66eb6687
AM
5219 if (old_tab != NULL)
5220 free (old_tab);
5b677558
AM
5221 if (old_strtab != NULL)
5222 free (old_strtab);
4ad4eba5
AM
5223 if (nondeflt_vers != NULL)
5224 free (nondeflt_vers);
5225 if (extversym != NULL)
5226 free (extversym);
5227 error_free_sym:
5228 if (isymbuf != NULL)
5229 free (isymbuf);
5230 error_return:
5231 return FALSE;
5232}
5233
8387904d
AM
5234/* Return the linker hash table entry of a symbol that might be
5235 satisfied by an archive symbol. Return -1 on error. */
5236
5237struct elf_link_hash_entry *
5238_bfd_elf_archive_symbol_lookup (bfd *abfd,
5239 struct bfd_link_info *info,
5240 const char *name)
5241{
5242 struct elf_link_hash_entry *h;
5243 char *p, *copy;
5244 size_t len, first;
5245
2a41f396 5246 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5247 if (h != NULL)
5248 return h;
5249
5250 /* If this is a default version (the name contains @@), look up the
5251 symbol again with only one `@' as well as without the version.
5252 The effect is that references to the symbol with and without the
5253 version will be matched by the default symbol in the archive. */
5254
5255 p = strchr (name, ELF_VER_CHR);
5256 if (p == NULL || p[1] != ELF_VER_CHR)
5257 return h;
5258
5259 /* First check with only one `@'. */
5260 len = strlen (name);
a50b1753 5261 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5262 if (copy == NULL)
5263 return (struct elf_link_hash_entry *) 0 - 1;
5264
5265 first = p - name + 1;
5266 memcpy (copy, name, first);
5267 memcpy (copy + first, name + first + 1, len - first);
5268
2a41f396 5269 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5270 if (h == NULL)
5271 {
5272 /* We also need to check references to the symbol without the
5273 version. */
5274 copy[first - 1] = '\0';
5275 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5276 FALSE, FALSE, TRUE);
8387904d
AM
5277 }
5278
5279 bfd_release (abfd, copy);
5280 return h;
5281}
5282
0ad989f9 5283/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5284 don't use _bfd_generic_link_add_archive_symbols because we need to
5285 handle versioned symbols.
0ad989f9
L
5286
5287 Fortunately, ELF archive handling is simpler than that done by
5288 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5289 oddities. In ELF, if we find a symbol in the archive map, and the
5290 symbol is currently undefined, we know that we must pull in that
5291 object file.
5292
5293 Unfortunately, we do have to make multiple passes over the symbol
5294 table until nothing further is resolved. */
5295
4ad4eba5
AM
5296static bfd_boolean
5297elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5298{
5299 symindex c;
13e570f8 5300 unsigned char *included = NULL;
0ad989f9
L
5301 carsym *symdefs;
5302 bfd_boolean loop;
5303 bfd_size_type amt;
8387904d
AM
5304 const struct elf_backend_data *bed;
5305 struct elf_link_hash_entry * (*archive_symbol_lookup)
5306 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5307
5308 if (! bfd_has_map (abfd))
5309 {
5310 /* An empty archive is a special case. */
5311 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5312 return TRUE;
5313 bfd_set_error (bfd_error_no_armap);
5314 return FALSE;
5315 }
5316
5317 /* Keep track of all symbols we know to be already defined, and all
5318 files we know to be already included. This is to speed up the
5319 second and subsequent passes. */
5320 c = bfd_ardata (abfd)->symdef_count;
5321 if (c == 0)
5322 return TRUE;
5323 amt = c;
13e570f8
AM
5324 amt *= sizeof (*included);
5325 included = (unsigned char *) bfd_zmalloc (amt);
5326 if (included == NULL)
5327 return FALSE;
0ad989f9
L
5328
5329 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5330 bed = get_elf_backend_data (abfd);
5331 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5332
5333 do
5334 {
5335 file_ptr last;
5336 symindex i;
5337 carsym *symdef;
5338 carsym *symdefend;
5339
5340 loop = FALSE;
5341 last = -1;
5342
5343 symdef = symdefs;
5344 symdefend = symdef + c;
5345 for (i = 0; symdef < symdefend; symdef++, i++)
5346 {
5347 struct elf_link_hash_entry *h;
5348 bfd *element;
5349 struct bfd_link_hash_entry *undefs_tail;
5350 symindex mark;
5351
13e570f8 5352 if (included[i])
0ad989f9
L
5353 continue;
5354 if (symdef->file_offset == last)
5355 {
5356 included[i] = TRUE;
5357 continue;
5358 }
5359
8387904d
AM
5360 h = archive_symbol_lookup (abfd, info, symdef->name);
5361 if (h == (struct elf_link_hash_entry *) 0 - 1)
5362 goto error_return;
0ad989f9
L
5363
5364 if (h == NULL)
5365 continue;
5366
5367 if (h->root.type == bfd_link_hash_common)
5368 {
5369 /* We currently have a common symbol. The archive map contains
5370 a reference to this symbol, so we may want to include it. We
5371 only want to include it however, if this archive element
5372 contains a definition of the symbol, not just another common
5373 declaration of it.
5374
5375 Unfortunately some archivers (including GNU ar) will put
5376 declarations of common symbols into their archive maps, as
5377 well as real definitions, so we cannot just go by the archive
5378 map alone. Instead we must read in the element's symbol
5379 table and check that to see what kind of symbol definition
5380 this is. */
5381 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5382 continue;
5383 }
5384 else if (h->root.type != bfd_link_hash_undefined)
5385 {
5386 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5387 /* Symbol must be defined. Don't check it again. */
5388 included[i] = TRUE;
0ad989f9
L
5389 continue;
5390 }
5391
5392 /* We need to include this archive member. */
5393 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5394 if (element == NULL)
5395 goto error_return;
5396
5397 if (! bfd_check_format (element, bfd_object))
5398 goto error_return;
5399
0ad989f9
L
5400 undefs_tail = info->hash->undefs_tail;
5401
0e144ba7
AM
5402 if (!(*info->callbacks
5403 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5404 continue;
0e144ba7 5405 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5406 goto error_return;
5407
5408 /* If there are any new undefined symbols, we need to make
5409 another pass through the archive in order to see whether
5410 they can be defined. FIXME: This isn't perfect, because
5411 common symbols wind up on undefs_tail and because an
5412 undefined symbol which is defined later on in this pass
5413 does not require another pass. This isn't a bug, but it
5414 does make the code less efficient than it could be. */
5415 if (undefs_tail != info->hash->undefs_tail)
5416 loop = TRUE;
5417
5418 /* Look backward to mark all symbols from this object file
5419 which we have already seen in this pass. */
5420 mark = i;
5421 do
5422 {
5423 included[mark] = TRUE;
5424 if (mark == 0)
5425 break;
5426 --mark;
5427 }
5428 while (symdefs[mark].file_offset == symdef->file_offset);
5429
5430 /* We mark subsequent symbols from this object file as we go
5431 on through the loop. */
5432 last = symdef->file_offset;
5433 }
5434 }
5435 while (loop);
5436
0ad989f9
L
5437 free (included);
5438
5439 return TRUE;
5440
5441 error_return:
0ad989f9
L
5442 if (included != NULL)
5443 free (included);
5444 return FALSE;
5445}
4ad4eba5
AM
5446
5447/* Given an ELF BFD, add symbols to the global hash table as
5448 appropriate. */
5449
5450bfd_boolean
5451bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5452{
5453 switch (bfd_get_format (abfd))
5454 {
5455 case bfd_object:
5456 return elf_link_add_object_symbols (abfd, info);
5457 case bfd_archive:
5458 return elf_link_add_archive_symbols (abfd, info);
5459 default:
5460 bfd_set_error (bfd_error_wrong_format);
5461 return FALSE;
5462 }
5463}
5a580b3a 5464\f
14b1c01e
AM
5465struct hash_codes_info
5466{
5467 unsigned long *hashcodes;
5468 bfd_boolean error;
5469};
a0c8462f 5470
5a580b3a
AM
5471/* This function will be called though elf_link_hash_traverse to store
5472 all hash value of the exported symbols in an array. */
5473
5474static bfd_boolean
5475elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5476{
a50b1753 5477 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5478 const char *name;
5a580b3a
AM
5479 unsigned long ha;
5480 char *alc = NULL;
5481
5a580b3a
AM
5482 /* Ignore indirect symbols. These are added by the versioning code. */
5483 if (h->dynindx == -1)
5484 return TRUE;
5485
5486 name = h->root.root.string;
422f1182 5487 if (h->versioned >= versioned)
5a580b3a 5488 {
422f1182
L
5489 char *p = strchr (name, ELF_VER_CHR);
5490 if (p != NULL)
14b1c01e 5491 {
422f1182
L
5492 alc = (char *) bfd_malloc (p - name + 1);
5493 if (alc == NULL)
5494 {
5495 inf->error = TRUE;
5496 return FALSE;
5497 }
5498 memcpy (alc, name, p - name);
5499 alc[p - name] = '\0';
5500 name = alc;
14b1c01e 5501 }
5a580b3a
AM
5502 }
5503
5504 /* Compute the hash value. */
5505 ha = bfd_elf_hash (name);
5506
5507 /* Store the found hash value in the array given as the argument. */
14b1c01e 5508 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5509
5510 /* And store it in the struct so that we can put it in the hash table
5511 later. */
f6e332e6 5512 h->u.elf_hash_value = ha;
5a580b3a
AM
5513
5514 if (alc != NULL)
5515 free (alc);
5516
5517 return TRUE;
5518}
5519
fdc90cb4
JJ
5520struct collect_gnu_hash_codes
5521{
5522 bfd *output_bfd;
5523 const struct elf_backend_data *bed;
5524 unsigned long int nsyms;
5525 unsigned long int maskbits;
5526 unsigned long int *hashcodes;
5527 unsigned long int *hashval;
5528 unsigned long int *indx;
5529 unsigned long int *counts;
5530 bfd_vma *bitmask;
5531 bfd_byte *contents;
5532 long int min_dynindx;
5533 unsigned long int bucketcount;
5534 unsigned long int symindx;
5535 long int local_indx;
5536 long int shift1, shift2;
5537 unsigned long int mask;
14b1c01e 5538 bfd_boolean error;
fdc90cb4
JJ
5539};
5540
5541/* This function will be called though elf_link_hash_traverse to store
5542 all hash value of the exported symbols in an array. */
5543
5544static bfd_boolean
5545elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5546{
a50b1753 5547 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5548 const char *name;
fdc90cb4
JJ
5549 unsigned long ha;
5550 char *alc = NULL;
5551
fdc90cb4
JJ
5552 /* Ignore indirect symbols. These are added by the versioning code. */
5553 if (h->dynindx == -1)
5554 return TRUE;
5555
5556 /* Ignore also local symbols and undefined symbols. */
5557 if (! (*s->bed->elf_hash_symbol) (h))
5558 return TRUE;
5559
5560 name = h->root.root.string;
422f1182 5561 if (h->versioned >= versioned)
fdc90cb4 5562 {
422f1182
L
5563 char *p = strchr (name, ELF_VER_CHR);
5564 if (p != NULL)
14b1c01e 5565 {
422f1182
L
5566 alc = (char *) bfd_malloc (p - name + 1);
5567 if (alc == NULL)
5568 {
5569 s->error = TRUE;
5570 return FALSE;
5571 }
5572 memcpy (alc, name, p - name);
5573 alc[p - name] = '\0';
5574 name = alc;
14b1c01e 5575 }
fdc90cb4
JJ
5576 }
5577
5578 /* Compute the hash value. */
5579 ha = bfd_elf_gnu_hash (name);
5580
5581 /* Store the found hash value in the array for compute_bucket_count,
5582 and also for .dynsym reordering purposes. */
5583 s->hashcodes[s->nsyms] = ha;
5584 s->hashval[h->dynindx] = ha;
5585 ++s->nsyms;
5586 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5587 s->min_dynindx = h->dynindx;
5588
5589 if (alc != NULL)
5590 free (alc);
5591
5592 return TRUE;
5593}
5594
5595/* This function will be called though elf_link_hash_traverse to do
5596 final dynaminc symbol renumbering. */
5597
5598static bfd_boolean
5599elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5600{
a50b1753 5601 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5602 unsigned long int bucket;
5603 unsigned long int val;
5604
fdc90cb4
JJ
5605 /* Ignore indirect symbols. */
5606 if (h->dynindx == -1)
5607 return TRUE;
5608
5609 /* Ignore also local symbols and undefined symbols. */
5610 if (! (*s->bed->elf_hash_symbol) (h))
5611 {
5612 if (h->dynindx >= s->min_dynindx)
5613 h->dynindx = s->local_indx++;
5614 return TRUE;
5615 }
5616
5617 bucket = s->hashval[h->dynindx] % s->bucketcount;
5618 val = (s->hashval[h->dynindx] >> s->shift1)
5619 & ((s->maskbits >> s->shift1) - 1);
5620 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5621 s->bitmask[val]
5622 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5623 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5624 if (s->counts[bucket] == 1)
5625 /* Last element terminates the chain. */
5626 val |= 1;
5627 bfd_put_32 (s->output_bfd, val,
5628 s->contents + (s->indx[bucket] - s->symindx) * 4);
5629 --s->counts[bucket];
5630 h->dynindx = s->indx[bucket]++;
5631 return TRUE;
5632}
5633
5634/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5635
5636bfd_boolean
5637_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5638{
5639 return !(h->forced_local
5640 || h->root.type == bfd_link_hash_undefined
5641 || h->root.type == bfd_link_hash_undefweak
5642 || ((h->root.type == bfd_link_hash_defined
5643 || h->root.type == bfd_link_hash_defweak)
5644 && h->root.u.def.section->output_section == NULL));
5645}
5646
5a580b3a
AM
5647/* Array used to determine the number of hash table buckets to use
5648 based on the number of symbols there are. If there are fewer than
5649 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5650 fewer than 37 we use 17 buckets, and so forth. We never use more
5651 than 32771 buckets. */
5652
5653static const size_t elf_buckets[] =
5654{
5655 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5656 16411, 32771, 0
5657};
5658
5659/* Compute bucket count for hashing table. We do not use a static set
5660 of possible tables sizes anymore. Instead we determine for all
5661 possible reasonable sizes of the table the outcome (i.e., the
5662 number of collisions etc) and choose the best solution. The
5663 weighting functions are not too simple to allow the table to grow
5664 without bounds. Instead one of the weighting factors is the size.
5665 Therefore the result is always a good payoff between few collisions
5666 (= short chain lengths) and table size. */
5667static size_t
b20dd2ce 5668compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5669 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5670 unsigned long int nsyms,
5671 int gnu_hash)
5a580b3a 5672{
5a580b3a 5673 size_t best_size = 0;
5a580b3a 5674 unsigned long int i;
5a580b3a 5675
5a580b3a
AM
5676 /* We have a problem here. The following code to optimize the table
5677 size requires an integer type with more the 32 bits. If
5678 BFD_HOST_U_64_BIT is set we know about such a type. */
5679#ifdef BFD_HOST_U_64_BIT
5680 if (info->optimize)
5681 {
5a580b3a
AM
5682 size_t minsize;
5683 size_t maxsize;
5684 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5685 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5686 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5687 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5688 unsigned long int *counts;
d40f3da9 5689 bfd_size_type amt;
0883b6e0 5690 unsigned int no_improvement_count = 0;
5a580b3a
AM
5691
5692 /* Possible optimization parameters: if we have NSYMS symbols we say
5693 that the hashing table must at least have NSYMS/4 and at most
5694 2*NSYMS buckets. */
5695 minsize = nsyms / 4;
5696 if (minsize == 0)
5697 minsize = 1;
5698 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5699 if (gnu_hash)
5700 {
5701 if (minsize < 2)
5702 minsize = 2;
5703 if ((best_size & 31) == 0)
5704 ++best_size;
5705 }
5a580b3a
AM
5706
5707 /* Create array where we count the collisions in. We must use bfd_malloc
5708 since the size could be large. */
5709 amt = maxsize;
5710 amt *= sizeof (unsigned long int);
a50b1753 5711 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5712 if (counts == NULL)
fdc90cb4 5713 return 0;
5a580b3a
AM
5714
5715 /* Compute the "optimal" size for the hash table. The criteria is a
5716 minimal chain length. The minor criteria is (of course) the size
5717 of the table. */
5718 for (i = minsize; i < maxsize; ++i)
5719 {
5720 /* Walk through the array of hashcodes and count the collisions. */
5721 BFD_HOST_U_64_BIT max;
5722 unsigned long int j;
5723 unsigned long int fact;
5724
fdc90cb4
JJ
5725 if (gnu_hash && (i & 31) == 0)
5726 continue;
5727
5a580b3a
AM
5728 memset (counts, '\0', i * sizeof (unsigned long int));
5729
5730 /* Determine how often each hash bucket is used. */
5731 for (j = 0; j < nsyms; ++j)
5732 ++counts[hashcodes[j] % i];
5733
5734 /* For the weight function we need some information about the
5735 pagesize on the target. This is information need not be 100%
5736 accurate. Since this information is not available (so far) we
5737 define it here to a reasonable default value. If it is crucial
5738 to have a better value some day simply define this value. */
5739# ifndef BFD_TARGET_PAGESIZE
5740# define BFD_TARGET_PAGESIZE (4096)
5741# endif
5742
fdc90cb4
JJ
5743 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5744 and the chains. */
5745 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5746
5747# if 1
5748 /* Variant 1: optimize for short chains. We add the squares
5749 of all the chain lengths (which favors many small chain
5750 over a few long chains). */
5751 for (j = 0; j < i; ++j)
5752 max += counts[j] * counts[j];
5753
5754 /* This adds penalties for the overall size of the table. */
fdc90cb4 5755 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5756 max *= fact * fact;
5757# else
5758 /* Variant 2: Optimize a lot more for small table. Here we
5759 also add squares of the size but we also add penalties for
5760 empty slots (the +1 term). */
5761 for (j = 0; j < i; ++j)
5762 max += (1 + counts[j]) * (1 + counts[j]);
5763
5764 /* The overall size of the table is considered, but not as
5765 strong as in variant 1, where it is squared. */
fdc90cb4 5766 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5767 max *= fact;
5768# endif
5769
5770 /* Compare with current best results. */
5771 if (max < best_chlen)
5772 {
5773 best_chlen = max;
5774 best_size = i;
ca4be51c 5775 no_improvement_count = 0;
5a580b3a 5776 }
0883b6e0
NC
5777 /* PR 11843: Avoid futile long searches for the best bucket size
5778 when there are a large number of symbols. */
5779 else if (++no_improvement_count == 100)
5780 break;
5a580b3a
AM
5781 }
5782
5783 free (counts);
5784 }
5785 else
5786#endif /* defined (BFD_HOST_U_64_BIT) */
5787 {
5788 /* This is the fallback solution if no 64bit type is available or if we
5789 are not supposed to spend much time on optimizations. We select the
5790 bucket count using a fixed set of numbers. */
5791 for (i = 0; elf_buckets[i] != 0; i++)
5792 {
5793 best_size = elf_buckets[i];
fdc90cb4 5794 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5795 break;
5796 }
fdc90cb4
JJ
5797 if (gnu_hash && best_size < 2)
5798 best_size = 2;
5a580b3a
AM
5799 }
5800
5a580b3a
AM
5801 return best_size;
5802}
5803
d0bf826b
AM
5804/* Size any SHT_GROUP section for ld -r. */
5805
5806bfd_boolean
5807_bfd_elf_size_group_sections (struct bfd_link_info *info)
5808{
5809 bfd *ibfd;
5810
c72f2fb2 5811 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5812 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5813 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5814 return FALSE;
5815 return TRUE;
5816}
5817
04c3a755
NS
5818/* Set a default stack segment size. The value in INFO wins. If it
5819 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5820 undefined it is initialized. */
5821
5822bfd_boolean
5823bfd_elf_stack_segment_size (bfd *output_bfd,
5824 struct bfd_link_info *info,
5825 const char *legacy_symbol,
5826 bfd_vma default_size)
5827{
5828 struct elf_link_hash_entry *h = NULL;
5829
5830 /* Look for legacy symbol. */
5831 if (legacy_symbol)
5832 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5833 FALSE, FALSE, FALSE);
5834 if (h && (h->root.type == bfd_link_hash_defined
5835 || h->root.type == bfd_link_hash_defweak)
5836 && h->def_regular
5837 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5838 {
5839 /* The symbol has no type if specified on the command line. */
5840 h->type = STT_OBJECT;
5841 if (info->stacksize)
695344c0 5842 /* xgettext:c-format */
4eca0228
AM
5843 _bfd_error_handler (_("%B: stack size specified and %s set"),
5844 output_bfd, legacy_symbol);
04c3a755 5845 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5846 /* xgettext:c-format */
4eca0228
AM
5847 _bfd_error_handler (_("%B: %s not absolute"),
5848 output_bfd, legacy_symbol);
04c3a755
NS
5849 else
5850 info->stacksize = h->root.u.def.value;
5851 }
5852
5853 if (!info->stacksize)
5854 /* If the user didn't set a size, or explicitly inhibit the
5855 size, set it now. */
5856 info->stacksize = default_size;
5857
5858 /* Provide the legacy symbol, if it is referenced. */
5859 if (h && (h->root.type == bfd_link_hash_undefined
5860 || h->root.type == bfd_link_hash_undefweak))
5861 {
5862 struct bfd_link_hash_entry *bh = NULL;
5863
5864 if (!(_bfd_generic_link_add_one_symbol
5865 (info, output_bfd, legacy_symbol,
5866 BSF_GLOBAL, bfd_abs_section_ptr,
5867 info->stacksize >= 0 ? info->stacksize : 0,
5868 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5869 return FALSE;
5870
5871 h = (struct elf_link_hash_entry *) bh;
5872 h->def_regular = 1;
5873 h->type = STT_OBJECT;
5874 }
5875
5876 return TRUE;
5877}
5878
5a580b3a
AM
5879/* Set up the sizes and contents of the ELF dynamic sections. This is
5880 called by the ELF linker emulation before_allocation routine. We
5881 must set the sizes of the sections before the linker sets the
5882 addresses of the various sections. */
5883
5884bfd_boolean
5885bfd_elf_size_dynamic_sections (bfd *output_bfd,
5886 const char *soname,
5887 const char *rpath,
5888 const char *filter_shlib,
7ee314fa
AM
5889 const char *audit,
5890 const char *depaudit,
5a580b3a
AM
5891 const char * const *auxiliary_filters,
5892 struct bfd_link_info *info,
fd91d419 5893 asection **sinterpptr)
5a580b3a 5894{
ef53be89 5895 size_t soname_indx;
5a580b3a
AM
5896 bfd *dynobj;
5897 const struct elf_backend_data *bed;
28caa186 5898 struct elf_info_failed asvinfo;
5a580b3a
AM
5899
5900 *sinterpptr = NULL;
5901
ef53be89 5902 soname_indx = (size_t) -1;
5a580b3a
AM
5903
5904 if (!is_elf_hash_table (info->hash))
5905 return TRUE;
5906
6bfdb61b 5907 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5908
5909 /* Any syms created from now on start with -1 in
5910 got.refcount/offset and plt.refcount/offset. */
5911 elf_hash_table (info)->init_got_refcount
5912 = elf_hash_table (info)->init_got_offset;
5913 elf_hash_table (info)->init_plt_refcount
5914 = elf_hash_table (info)->init_plt_offset;
5915
0e1862bb 5916 if (bfd_link_relocatable (info)
04c3a755
NS
5917 && !_bfd_elf_size_group_sections (info))
5918 return FALSE;
5919
5920 /* The backend may have to create some sections regardless of whether
5921 we're dynamic or not. */
5922 if (bed->elf_backend_always_size_sections
5923 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5924 return FALSE;
5925
5926 /* Determine any GNU_STACK segment requirements, after the backend
5927 has had a chance to set a default segment size. */
5a580b3a 5928 if (info->execstack)
12bd6957 5929 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5930 else if (info->noexecstack)
12bd6957 5931 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5932 else
5933 {
5934 bfd *inputobj;
5935 asection *notesec = NULL;
5936 int exec = 0;
5937
5938 for (inputobj = info->input_bfds;
5939 inputobj;
c72f2fb2 5940 inputobj = inputobj->link.next)
5a580b3a
AM
5941 {
5942 asection *s;
5943
a92c088a
L
5944 if (inputobj->flags
5945 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5946 continue;
5947 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5948 if (s)
5949 {
5950 if (s->flags & SEC_CODE)
5951 exec = PF_X;
5952 notesec = s;
5953 }
6bfdb61b 5954 else if (bed->default_execstack)
5a580b3a
AM
5955 exec = PF_X;
5956 }
04c3a755 5957 if (notesec || info->stacksize > 0)
12bd6957 5958 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5959 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5960 && notesec->output_section != bfd_abs_section_ptr)
5961 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5962 }
5963
5a580b3a
AM
5964 dynobj = elf_hash_table (info)->dynobj;
5965
9a2a56cc 5966 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5967 {
5968 struct elf_info_failed eif;
5969 struct elf_link_hash_entry *h;
5970 asection *dynstr;
5971 struct bfd_elf_version_tree *t;
5972 struct bfd_elf_version_expr *d;
046183de 5973 asection *s;
5a580b3a
AM
5974 bfd_boolean all_defined;
5975
3d4d4302 5976 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5977 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5978
5979 if (soname != NULL)
5980 {
5981 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5982 soname, TRUE);
ef53be89 5983 if (soname_indx == (size_t) -1
5a580b3a
AM
5984 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5985 return FALSE;
5986 }
5987
5988 if (info->symbolic)
5989 {
5990 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5991 return FALSE;
5992 info->flags |= DF_SYMBOLIC;
5993 }
5994
5995 if (rpath != NULL)
5996 {
ef53be89 5997 size_t indx;
b1b00fcc 5998 bfd_vma tag;
5a580b3a
AM
5999
6000 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6001 TRUE);
ef53be89 6002 if (indx == (size_t) -1)
5a580b3a
AM
6003 return FALSE;
6004
b1b00fcc
MF
6005 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6006 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6007 return FALSE;
5a580b3a
AM
6008 }
6009
6010 if (filter_shlib != NULL)
6011 {
ef53be89 6012 size_t indx;
5a580b3a
AM
6013
6014 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6015 filter_shlib, TRUE);
ef53be89 6016 if (indx == (size_t) -1
5a580b3a
AM
6017 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6018 return FALSE;
6019 }
6020
6021 if (auxiliary_filters != NULL)
6022 {
6023 const char * const *p;
6024
6025 for (p = auxiliary_filters; *p != NULL; p++)
6026 {
ef53be89 6027 size_t indx;
5a580b3a
AM
6028
6029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6030 *p, TRUE);
ef53be89 6031 if (indx == (size_t) -1
5a580b3a
AM
6032 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6033 return FALSE;
6034 }
6035 }
6036
7ee314fa
AM
6037 if (audit != NULL)
6038 {
ef53be89 6039 size_t indx;
7ee314fa
AM
6040
6041 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6042 TRUE);
ef53be89 6043 if (indx == (size_t) -1
7ee314fa
AM
6044 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6045 return FALSE;
6046 }
6047
6048 if (depaudit != NULL)
6049 {
ef53be89 6050 size_t indx;
7ee314fa
AM
6051
6052 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6053 TRUE);
ef53be89 6054 if (indx == (size_t) -1
7ee314fa
AM
6055 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6056 return FALSE;
6057 }
6058
5a580b3a 6059 eif.info = info;
5a580b3a
AM
6060 eif.failed = FALSE;
6061
6062 /* If we are supposed to export all symbols into the dynamic symbol
6063 table (this is not the normal case), then do so. */
55255dae 6064 if (info->export_dynamic
0e1862bb 6065 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6066 {
6067 elf_link_hash_traverse (elf_hash_table (info),
6068 _bfd_elf_export_symbol,
6069 &eif);
6070 if (eif.failed)
6071 return FALSE;
6072 }
6073
6074 /* Make all global versions with definition. */
fd91d419 6075 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6076 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6077 if (!d->symver && d->literal)
5a580b3a
AM
6078 {
6079 const char *verstr, *name;
6080 size_t namelen, verlen, newlen;
93252b1c 6081 char *newname, *p, leading_char;
5a580b3a
AM
6082 struct elf_link_hash_entry *newh;
6083
93252b1c 6084 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6085 name = d->pattern;
93252b1c 6086 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6087 verstr = t->name;
6088 verlen = strlen (verstr);
6089 newlen = namelen + verlen + 3;
6090
a50b1753 6091 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6092 if (newname == NULL)
6093 return FALSE;
93252b1c
MF
6094 newname[0] = leading_char;
6095 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6096
6097 /* Check the hidden versioned definition. */
6098 p = newname + namelen;
6099 *p++ = ELF_VER_CHR;
6100 memcpy (p, verstr, verlen + 1);
6101 newh = elf_link_hash_lookup (elf_hash_table (info),
6102 newname, FALSE, FALSE,
6103 FALSE);
6104 if (newh == NULL
6105 || (newh->root.type != bfd_link_hash_defined
6106 && newh->root.type != bfd_link_hash_defweak))
6107 {
6108 /* Check the default versioned definition. */
6109 *p++ = ELF_VER_CHR;
6110 memcpy (p, verstr, verlen + 1);
6111 newh = elf_link_hash_lookup (elf_hash_table (info),
6112 newname, FALSE, FALSE,
6113 FALSE);
6114 }
6115 free (newname);
6116
6117 /* Mark this version if there is a definition and it is
6118 not defined in a shared object. */
6119 if (newh != NULL
f5385ebf 6120 && !newh->def_dynamic
5a580b3a
AM
6121 && (newh->root.type == bfd_link_hash_defined
6122 || newh->root.type == bfd_link_hash_defweak))
6123 d->symver = 1;
6124 }
6125
6126 /* Attach all the symbols to their version information. */
5a580b3a 6127 asvinfo.info = info;
5a580b3a
AM
6128 asvinfo.failed = FALSE;
6129
6130 elf_link_hash_traverse (elf_hash_table (info),
6131 _bfd_elf_link_assign_sym_version,
6132 &asvinfo);
6133 if (asvinfo.failed)
6134 return FALSE;
6135
6136 if (!info->allow_undefined_version)
6137 {
6138 /* Check if all global versions have a definition. */
6139 all_defined = TRUE;
fd91d419 6140 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6141 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6142 if (d->literal && !d->symver && !d->script)
5a580b3a 6143 {
4eca0228 6144 _bfd_error_handler
5a580b3a
AM
6145 (_("%s: undefined version: %s"),
6146 d->pattern, t->name);
6147 all_defined = FALSE;
6148 }
6149
6150 if (!all_defined)
6151 {
6152 bfd_set_error (bfd_error_bad_value);
6153 return FALSE;
6154 }
6155 }
6156
6157 /* Find all symbols which were defined in a dynamic object and make
6158 the backend pick a reasonable value for them. */
6159 elf_link_hash_traverse (elf_hash_table (info),
6160 _bfd_elf_adjust_dynamic_symbol,
6161 &eif);
6162 if (eif.failed)
6163 return FALSE;
6164
6165 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6166 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6167 now so that we know the final size of the .dynamic section. */
6168
6169 /* If there are initialization and/or finalization functions to
6170 call then add the corresponding DT_INIT/DT_FINI entries. */
6171 h = (info->init_function
6172 ? elf_link_hash_lookup (elf_hash_table (info),
6173 info->init_function, FALSE,
6174 FALSE, FALSE)
6175 : NULL);
6176 if (h != NULL
f5385ebf
AM
6177 && (h->ref_regular
6178 || h->def_regular))
5a580b3a
AM
6179 {
6180 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6181 return FALSE;
6182 }
6183 h = (info->fini_function
6184 ? elf_link_hash_lookup (elf_hash_table (info),
6185 info->fini_function, FALSE,
6186 FALSE, FALSE)
6187 : NULL);
6188 if (h != NULL
f5385ebf
AM
6189 && (h->ref_regular
6190 || h->def_regular))
5a580b3a
AM
6191 {
6192 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6193 return FALSE;
6194 }
6195
046183de
AM
6196 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6197 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6198 {
6199 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6200 if (! bfd_link_executable (info))
5a580b3a
AM
6201 {
6202 bfd *sub;
6203 asection *o;
6204
6205 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6206 sub = sub->link.next)
3fcd97f1
JJ
6207 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6208 for (o = sub->sections; o != NULL; o = o->next)
6209 if (elf_section_data (o)->this_hdr.sh_type
6210 == SHT_PREINIT_ARRAY)
6211 {
4eca0228 6212 _bfd_error_handler
3fcd97f1
JJ
6213 (_("%B: .preinit_array section is not allowed in DSO"),
6214 sub);
6215 break;
6216 }
5a580b3a
AM
6217
6218 bfd_set_error (bfd_error_nonrepresentable_section);
6219 return FALSE;
6220 }
6221
6222 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6223 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6224 return FALSE;
6225 }
046183de
AM
6226 s = bfd_get_section_by_name (output_bfd, ".init_array");
6227 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6228 {
6229 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6230 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6231 return FALSE;
6232 }
046183de
AM
6233 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6234 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6235 {
6236 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6237 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6238 return FALSE;
6239 }
6240
3d4d4302 6241 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6242 /* If .dynstr is excluded from the link, we don't want any of
6243 these tags. Strictly, we should be checking each section
6244 individually; This quick check covers for the case where
6245 someone does a /DISCARD/ : { *(*) }. */
6246 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6247 {
6248 bfd_size_type strsize;
6249
6250 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6251 if ((info->emit_hash
6252 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6253 || (info->emit_gnu_hash
6254 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6255 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6256 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6257 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6258 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6259 bed->s->sizeof_sym))
6260 return FALSE;
6261 }
6262 }
6263
de231f20
CM
6264 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6265 return FALSE;
6266
5a580b3a
AM
6267 /* The backend must work out the sizes of all the other dynamic
6268 sections. */
9a2a56cc
AM
6269 if (dynobj != NULL
6270 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6271 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6272 return FALSE;
6273
9a2a56cc 6274 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6275 {
554220db 6276 unsigned long section_sym_count;
fd91d419 6277 struct bfd_elf_version_tree *verdefs;
5a580b3a 6278 asection *s;
5a580b3a
AM
6279
6280 /* Set up the version definition section. */
3d4d4302 6281 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6282 BFD_ASSERT (s != NULL);
6283
6284 /* We may have created additional version definitions if we are
6285 just linking a regular application. */
fd91d419 6286 verdefs = info->version_info;
5a580b3a
AM
6287
6288 /* Skip anonymous version tag. */
6289 if (verdefs != NULL && verdefs->vernum == 0)
6290 verdefs = verdefs->next;
6291
3e3b46e5 6292 if (verdefs == NULL && !info->create_default_symver)
8423293d 6293 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6294 else
6295 {
6296 unsigned int cdefs;
6297 bfd_size_type size;
6298 struct bfd_elf_version_tree *t;
6299 bfd_byte *p;
6300 Elf_Internal_Verdef def;
6301 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6302 struct bfd_link_hash_entry *bh;
6303 struct elf_link_hash_entry *h;
6304 const char *name;
5a580b3a
AM
6305
6306 cdefs = 0;
6307 size = 0;
6308
6309 /* Make space for the base version. */
6310 size += sizeof (Elf_External_Verdef);
6311 size += sizeof (Elf_External_Verdaux);
6312 ++cdefs;
6313
3e3b46e5
PB
6314 /* Make space for the default version. */
6315 if (info->create_default_symver)
6316 {
6317 size += sizeof (Elf_External_Verdef);
6318 ++cdefs;
6319 }
6320
5a580b3a
AM
6321 for (t = verdefs; t != NULL; t = t->next)
6322 {
6323 struct bfd_elf_version_deps *n;
6324
a6cc6b3b
RO
6325 /* Don't emit base version twice. */
6326 if (t->vernum == 0)
6327 continue;
6328
5a580b3a
AM
6329 size += sizeof (Elf_External_Verdef);
6330 size += sizeof (Elf_External_Verdaux);
6331 ++cdefs;
6332
6333 for (n = t->deps; n != NULL; n = n->next)
6334 size += sizeof (Elf_External_Verdaux);
6335 }
6336
eea6121a 6337 s->size = size;
a50b1753 6338 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6339 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6340 return FALSE;
6341
6342 /* Fill in the version definition section. */
6343
6344 p = s->contents;
6345
6346 def.vd_version = VER_DEF_CURRENT;
6347 def.vd_flags = VER_FLG_BASE;
6348 def.vd_ndx = 1;
6349 def.vd_cnt = 1;
3e3b46e5
PB
6350 if (info->create_default_symver)
6351 {
6352 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6353 def.vd_next = sizeof (Elf_External_Verdef);
6354 }
6355 else
6356 {
6357 def.vd_aux = sizeof (Elf_External_Verdef);
6358 def.vd_next = (sizeof (Elf_External_Verdef)
6359 + sizeof (Elf_External_Verdaux));
6360 }
5a580b3a 6361
ef53be89 6362 if (soname_indx != (size_t) -1)
5a580b3a
AM
6363 {
6364 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6365 soname_indx);
6366 def.vd_hash = bfd_elf_hash (soname);
6367 defaux.vda_name = soname_indx;
3e3b46e5 6368 name = soname;
5a580b3a
AM
6369 }
6370 else
6371 {
ef53be89 6372 size_t indx;
5a580b3a 6373
06084812 6374 name = lbasename (output_bfd->filename);
5a580b3a
AM
6375 def.vd_hash = bfd_elf_hash (name);
6376 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6377 name, FALSE);
ef53be89 6378 if (indx == (size_t) -1)
5a580b3a
AM
6379 return FALSE;
6380 defaux.vda_name = indx;
6381 }
6382 defaux.vda_next = 0;
6383
6384 _bfd_elf_swap_verdef_out (output_bfd, &def,
6385 (Elf_External_Verdef *) p);
6386 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6387 if (info->create_default_symver)
6388 {
6389 /* Add a symbol representing this version. */
6390 bh = NULL;
6391 if (! (_bfd_generic_link_add_one_symbol
6392 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6393 0, NULL, FALSE,
6394 get_elf_backend_data (dynobj)->collect, &bh)))
6395 return FALSE;
6396 h = (struct elf_link_hash_entry *) bh;
6397 h->non_elf = 0;
6398 h->def_regular = 1;
6399 h->type = STT_OBJECT;
6400 h->verinfo.vertree = NULL;
6401
6402 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6403 return FALSE;
6404
6405 /* Create a duplicate of the base version with the same
6406 aux block, but different flags. */
6407 def.vd_flags = 0;
6408 def.vd_ndx = 2;
6409 def.vd_aux = sizeof (Elf_External_Verdef);
6410 if (verdefs)
6411 def.vd_next = (sizeof (Elf_External_Verdef)
6412 + sizeof (Elf_External_Verdaux));
6413 else
6414 def.vd_next = 0;
6415 _bfd_elf_swap_verdef_out (output_bfd, &def,
6416 (Elf_External_Verdef *) p);
6417 p += sizeof (Elf_External_Verdef);
6418 }
5a580b3a
AM
6419 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6420 (Elf_External_Verdaux *) p);
6421 p += sizeof (Elf_External_Verdaux);
6422
6423 for (t = verdefs; t != NULL; t = t->next)
6424 {
6425 unsigned int cdeps;
6426 struct bfd_elf_version_deps *n;
5a580b3a 6427
a6cc6b3b
RO
6428 /* Don't emit the base version twice. */
6429 if (t->vernum == 0)
6430 continue;
6431
5a580b3a
AM
6432 cdeps = 0;
6433 for (n = t->deps; n != NULL; n = n->next)
6434 ++cdeps;
6435
6436 /* Add a symbol representing this version. */
6437 bh = NULL;
6438 if (! (_bfd_generic_link_add_one_symbol
6439 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6440 0, NULL, FALSE,
6441 get_elf_backend_data (dynobj)->collect, &bh)))
6442 return FALSE;
6443 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6444 h->non_elf = 0;
6445 h->def_regular = 1;
5a580b3a
AM
6446 h->type = STT_OBJECT;
6447 h->verinfo.vertree = t;
6448
c152c796 6449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6450 return FALSE;
6451
6452 def.vd_version = VER_DEF_CURRENT;
6453 def.vd_flags = 0;
6454 if (t->globals.list == NULL
6455 && t->locals.list == NULL
6456 && ! t->used)
6457 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6458 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6459 def.vd_cnt = cdeps + 1;
6460 def.vd_hash = bfd_elf_hash (t->name);
6461 def.vd_aux = sizeof (Elf_External_Verdef);
6462 def.vd_next = 0;
a6cc6b3b
RO
6463
6464 /* If a basever node is next, it *must* be the last node in
6465 the chain, otherwise Verdef construction breaks. */
6466 if (t->next != NULL && t->next->vernum == 0)
6467 BFD_ASSERT (t->next->next == NULL);
6468
6469 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6470 def.vd_next = (sizeof (Elf_External_Verdef)
6471 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6472
6473 _bfd_elf_swap_verdef_out (output_bfd, &def,
6474 (Elf_External_Verdef *) p);
6475 p += sizeof (Elf_External_Verdef);
6476
6477 defaux.vda_name = h->dynstr_index;
6478 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6479 h->dynstr_index);
6480 defaux.vda_next = 0;
6481 if (t->deps != NULL)
6482 defaux.vda_next = sizeof (Elf_External_Verdaux);
6483 t->name_indx = defaux.vda_name;
6484
6485 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6486 (Elf_External_Verdaux *) p);
6487 p += sizeof (Elf_External_Verdaux);
6488
6489 for (n = t->deps; n != NULL; n = n->next)
6490 {
6491 if (n->version_needed == NULL)
6492 {
6493 /* This can happen if there was an error in the
6494 version script. */
6495 defaux.vda_name = 0;
6496 }
6497 else
6498 {
6499 defaux.vda_name = n->version_needed->name_indx;
6500 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6501 defaux.vda_name);
6502 }
6503 if (n->next == NULL)
6504 defaux.vda_next = 0;
6505 else
6506 defaux.vda_next = sizeof (Elf_External_Verdaux);
6507
6508 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6509 (Elf_External_Verdaux *) p);
6510 p += sizeof (Elf_External_Verdaux);
6511 }
6512 }
6513
6514 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6515 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6516 return FALSE;
6517
6518 elf_tdata (output_bfd)->cverdefs = cdefs;
6519 }
6520
6521 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6522 {
6523 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6524 return FALSE;
6525 }
6526 else if (info->flags & DF_BIND_NOW)
6527 {
6528 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6529 return FALSE;
6530 }
6531
6532 if (info->flags_1)
6533 {
0e1862bb 6534 if (bfd_link_executable (info))
5a580b3a
AM
6535 info->flags_1 &= ~ (DF_1_INITFIRST
6536 | DF_1_NODELETE
6537 | DF_1_NOOPEN);
6538 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6539 return FALSE;
6540 }
6541
6542 /* Work out the size of the version reference section. */
6543
3d4d4302 6544 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6545 BFD_ASSERT (s != NULL);
6546 {
6547 struct elf_find_verdep_info sinfo;
6548
5a580b3a
AM
6549 sinfo.info = info;
6550 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6551 if (sinfo.vers == 0)
6552 sinfo.vers = 1;
6553 sinfo.failed = FALSE;
6554
6555 elf_link_hash_traverse (elf_hash_table (info),
6556 _bfd_elf_link_find_version_dependencies,
6557 &sinfo);
14b1c01e
AM
6558 if (sinfo.failed)
6559 return FALSE;
5a580b3a
AM
6560
6561 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6562 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6563 else
6564 {
6565 Elf_Internal_Verneed *t;
6566 unsigned int size;
6567 unsigned int crefs;
6568 bfd_byte *p;
6569
a6cc6b3b 6570 /* Build the version dependency section. */
5a580b3a
AM
6571 size = 0;
6572 crefs = 0;
6573 for (t = elf_tdata (output_bfd)->verref;
6574 t != NULL;
6575 t = t->vn_nextref)
6576 {
6577 Elf_Internal_Vernaux *a;
6578
6579 size += sizeof (Elf_External_Verneed);
6580 ++crefs;
6581 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6582 size += sizeof (Elf_External_Vernaux);
6583 }
6584
eea6121a 6585 s->size = size;
a50b1753 6586 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6587 if (s->contents == NULL)
6588 return FALSE;
6589
6590 p = s->contents;
6591 for (t = elf_tdata (output_bfd)->verref;
6592 t != NULL;
6593 t = t->vn_nextref)
6594 {
6595 unsigned int caux;
6596 Elf_Internal_Vernaux *a;
ef53be89 6597 size_t indx;
5a580b3a
AM
6598
6599 caux = 0;
6600 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6601 ++caux;
6602
6603 t->vn_version = VER_NEED_CURRENT;
6604 t->vn_cnt = caux;
6605 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6606 elf_dt_name (t->vn_bfd) != NULL
6607 ? elf_dt_name (t->vn_bfd)
06084812 6608 : lbasename (t->vn_bfd->filename),
5a580b3a 6609 FALSE);
ef53be89 6610 if (indx == (size_t) -1)
5a580b3a
AM
6611 return FALSE;
6612 t->vn_file = indx;
6613 t->vn_aux = sizeof (Elf_External_Verneed);
6614 if (t->vn_nextref == NULL)
6615 t->vn_next = 0;
6616 else
6617 t->vn_next = (sizeof (Elf_External_Verneed)
6618 + caux * sizeof (Elf_External_Vernaux));
6619
6620 _bfd_elf_swap_verneed_out (output_bfd, t,
6621 (Elf_External_Verneed *) p);
6622 p += sizeof (Elf_External_Verneed);
6623
6624 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6625 {
6626 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6627 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6628 a->vna_nodename, FALSE);
ef53be89 6629 if (indx == (size_t) -1)
5a580b3a
AM
6630 return FALSE;
6631 a->vna_name = indx;
6632 if (a->vna_nextptr == NULL)
6633 a->vna_next = 0;
6634 else
6635 a->vna_next = sizeof (Elf_External_Vernaux);
6636
6637 _bfd_elf_swap_vernaux_out (output_bfd, a,
6638 (Elf_External_Vernaux *) p);
6639 p += sizeof (Elf_External_Vernaux);
6640 }
6641 }
6642
6643 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6644 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6645 return FALSE;
6646
6647 elf_tdata (output_bfd)->cverrefs = crefs;
6648 }
6649 }
6650
8423293d
AM
6651 if ((elf_tdata (output_bfd)->cverrefs == 0
6652 && elf_tdata (output_bfd)->cverdefs == 0)
6653 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 &section_sym_count) == 0)
6655 {
3d4d4302 6656 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6657 s->flags |= SEC_EXCLUDE;
6658 }
6659 }
6660 return TRUE;
6661}
6662
74541ad4
AM
6663/* Find the first non-excluded output section. We'll use its
6664 section symbol for some emitted relocs. */
6665void
6666_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6667{
6668 asection *s;
6669
6670 for (s = output_bfd->sections; s != NULL; s = s->next)
6671 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6672 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6673 {
6674 elf_hash_table (info)->text_index_section = s;
6675 break;
6676 }
6677}
6678
6679/* Find two non-excluded output sections, one for code, one for data.
6680 We'll use their section symbols for some emitted relocs. */
6681void
6682_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6683{
6684 asection *s;
6685
266b05cf
DJ
6686 /* Data first, since setting text_index_section changes
6687 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6688 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6689 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6690 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6691 {
266b05cf 6692 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6693 break;
6694 }
6695
6696 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6697 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6698 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6699 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6700 {
266b05cf 6701 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6702 break;
6703 }
6704
6705 if (elf_hash_table (info)->text_index_section == NULL)
6706 elf_hash_table (info)->text_index_section
6707 = elf_hash_table (info)->data_index_section;
6708}
6709
8423293d
AM
6710bfd_boolean
6711bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6712{
74541ad4
AM
6713 const struct elf_backend_data *bed;
6714
8423293d
AM
6715 if (!is_elf_hash_table (info->hash))
6716 return TRUE;
6717
74541ad4
AM
6718 bed = get_elf_backend_data (output_bfd);
6719 (*bed->elf_backend_init_index_section) (output_bfd, info);
6720
8423293d
AM
6721 if (elf_hash_table (info)->dynamic_sections_created)
6722 {
6723 bfd *dynobj;
8423293d
AM
6724 asection *s;
6725 bfd_size_type dynsymcount;
6726 unsigned long section_sym_count;
8423293d
AM
6727 unsigned int dtagcount;
6728
6729 dynobj = elf_hash_table (info)->dynobj;
6730
5a580b3a
AM
6731 /* Assign dynsym indicies. In a shared library we generate a
6732 section symbol for each output section, which come first.
6733 Next come all of the back-end allocated local dynamic syms,
6734 followed by the rest of the global symbols. */
6735
554220db
AM
6736 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6737 &section_sym_count);
5a580b3a
AM
6738
6739 /* Work out the size of the symbol version section. */
3d4d4302 6740 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6741 BFD_ASSERT (s != NULL);
d5486c43 6742 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6743 {
eea6121a 6744 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6745 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6746 if (s->contents == NULL)
6747 return FALSE;
6748
6749 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6750 return FALSE;
6751 }
6752
6753 /* Set the size of the .dynsym and .hash sections. We counted
6754 the number of dynamic symbols in elf_link_add_object_symbols.
6755 We will build the contents of .dynsym and .hash when we build
6756 the final symbol table, because until then we do not know the
6757 correct value to give the symbols. We built the .dynstr
6758 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6759 s = elf_hash_table (info)->dynsym;
5a580b3a 6760 BFD_ASSERT (s != NULL);
eea6121a 6761 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6762
d5486c43
L
6763 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6764 if (s->contents == NULL)
6765 return FALSE;
5a580b3a 6766
d5486c43
L
6767 /* The first entry in .dynsym is a dummy symbol. Clear all the
6768 section syms, in case we don't output them all. */
6769 ++section_sym_count;
6770 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6771
fdc90cb4
JJ
6772 elf_hash_table (info)->bucketcount = 0;
6773
5a580b3a
AM
6774 /* Compute the size of the hashing table. As a side effect this
6775 computes the hash values for all the names we export. */
fdc90cb4
JJ
6776 if (info->emit_hash)
6777 {
6778 unsigned long int *hashcodes;
14b1c01e 6779 struct hash_codes_info hashinf;
fdc90cb4
JJ
6780 bfd_size_type amt;
6781 unsigned long int nsyms;
6782 size_t bucketcount;
6783 size_t hash_entry_size;
6784
6785 /* Compute the hash values for all exported symbols. At the same
6786 time store the values in an array so that we could use them for
6787 optimizations. */
6788 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6789 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6790 if (hashcodes == NULL)
6791 return FALSE;
14b1c01e
AM
6792 hashinf.hashcodes = hashcodes;
6793 hashinf.error = FALSE;
5a580b3a 6794
fdc90cb4
JJ
6795 /* Put all hash values in HASHCODES. */
6796 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6797 elf_collect_hash_codes, &hashinf);
6798 if (hashinf.error)
4dd07732
AM
6799 {
6800 free (hashcodes);
6801 return FALSE;
6802 }
5a580b3a 6803
14b1c01e 6804 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6805 bucketcount
6806 = compute_bucket_count (info, hashcodes, nsyms, 0);
6807 free (hashcodes);
6808
6809 if (bucketcount == 0)
6810 return FALSE;
5a580b3a 6811
fdc90cb4
JJ
6812 elf_hash_table (info)->bucketcount = bucketcount;
6813
3d4d4302 6814 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6815 BFD_ASSERT (s != NULL);
6816 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6817 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6818 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6819 if (s->contents == NULL)
6820 return FALSE;
6821
6822 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6823 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6824 s->contents + hash_entry_size);
6825 }
6826
6827 if (info->emit_gnu_hash)
6828 {
6829 size_t i, cnt;
6830 unsigned char *contents;
6831 struct collect_gnu_hash_codes cinfo;
6832 bfd_size_type amt;
6833 size_t bucketcount;
6834
6835 memset (&cinfo, 0, sizeof (cinfo));
6836
6837 /* Compute the hash values for all exported symbols. At the same
6838 time store the values in an array so that we could use them for
6839 optimizations. */
6840 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6841 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6842 if (cinfo.hashcodes == NULL)
6843 return FALSE;
6844
6845 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6846 cinfo.min_dynindx = -1;
6847 cinfo.output_bfd = output_bfd;
6848 cinfo.bed = bed;
6849
6850 /* Put all hash values in HASHCODES. */
6851 elf_link_hash_traverse (elf_hash_table (info),
6852 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6853 if (cinfo.error)
4dd07732
AM
6854 {
6855 free (cinfo.hashcodes);
6856 return FALSE;
6857 }
fdc90cb4
JJ
6858
6859 bucketcount
6860 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6861
6862 if (bucketcount == 0)
6863 {
6864 free (cinfo.hashcodes);
6865 return FALSE;
6866 }
6867
3d4d4302 6868 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6869 BFD_ASSERT (s != NULL);
6870
6871 if (cinfo.nsyms == 0)
6872 {
6873 /* Empty .gnu.hash section is special. */
6874 BFD_ASSERT (cinfo.min_dynindx == -1);
6875 free (cinfo.hashcodes);
6876 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6877 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6878 if (contents == NULL)
6879 return FALSE;
6880 s->contents = contents;
6881 /* 1 empty bucket. */
6882 bfd_put_32 (output_bfd, 1, contents);
6883 /* SYMIDX above the special symbol 0. */
6884 bfd_put_32 (output_bfd, 1, contents + 4);
6885 /* Just one word for bitmask. */
6886 bfd_put_32 (output_bfd, 1, contents + 8);
6887 /* Only hash fn bloom filter. */
6888 bfd_put_32 (output_bfd, 0, contents + 12);
6889 /* No hashes are valid - empty bitmask. */
6890 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6891 /* No hashes in the only bucket. */
6892 bfd_put_32 (output_bfd, 0,
6893 contents + 16 + bed->s->arch_size / 8);
6894 }
6895 else
6896 {
9e6619e2 6897 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6898 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6899
9e6619e2
AM
6900 x = cinfo.nsyms;
6901 maskbitslog2 = 1;
6902 while ((x >>= 1) != 0)
6903 ++maskbitslog2;
fdc90cb4
JJ
6904 if (maskbitslog2 < 3)
6905 maskbitslog2 = 5;
6906 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6907 maskbitslog2 = maskbitslog2 + 3;
6908 else
6909 maskbitslog2 = maskbitslog2 + 2;
6910 if (bed->s->arch_size == 64)
6911 {
6912 if (maskbitslog2 == 5)
6913 maskbitslog2 = 6;
6914 cinfo.shift1 = 6;
6915 }
6916 else
6917 cinfo.shift1 = 5;
6918 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6919 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6920 cinfo.maskbits = 1 << maskbitslog2;
6921 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6922 amt = bucketcount * sizeof (unsigned long int) * 2;
6923 amt += maskwords * sizeof (bfd_vma);
a50b1753 6924 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6925 if (cinfo.bitmask == NULL)
6926 {
6927 free (cinfo.hashcodes);
6928 return FALSE;
6929 }
6930
a50b1753 6931 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6932 cinfo.indx = cinfo.counts + bucketcount;
6933 cinfo.symindx = dynsymcount - cinfo.nsyms;
6934 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6935
6936 /* Determine how often each hash bucket is used. */
6937 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6938 for (i = 0; i < cinfo.nsyms; ++i)
6939 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6940
6941 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6942 if (cinfo.counts[i] != 0)
6943 {
6944 cinfo.indx[i] = cnt;
6945 cnt += cinfo.counts[i];
6946 }
6947 BFD_ASSERT (cnt == dynsymcount);
6948 cinfo.bucketcount = bucketcount;
6949 cinfo.local_indx = cinfo.min_dynindx;
6950
6951 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6952 s->size += cinfo.maskbits / 8;
a50b1753 6953 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6954 if (contents == NULL)
6955 {
6956 free (cinfo.bitmask);
6957 free (cinfo.hashcodes);
6958 return FALSE;
6959 }
6960
6961 s->contents = contents;
6962 bfd_put_32 (output_bfd, bucketcount, contents);
6963 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6964 bfd_put_32 (output_bfd, maskwords, contents + 8);
6965 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6966 contents += 16 + cinfo.maskbits / 8;
6967
6968 for (i = 0; i < bucketcount; ++i)
6969 {
6970 if (cinfo.counts[i] == 0)
6971 bfd_put_32 (output_bfd, 0, contents);
6972 else
6973 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6974 contents += 4;
6975 }
6976
6977 cinfo.contents = contents;
6978
6979 /* Renumber dynamic symbols, populate .gnu.hash section. */
6980 elf_link_hash_traverse (elf_hash_table (info),
6981 elf_renumber_gnu_hash_syms, &cinfo);
6982
6983 contents = s->contents + 16;
6984 for (i = 0; i < maskwords; ++i)
6985 {
6986 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6987 contents);
6988 contents += bed->s->arch_size / 8;
6989 }
6990
6991 free (cinfo.bitmask);
6992 free (cinfo.hashcodes);
6993 }
6994 }
5a580b3a 6995
3d4d4302 6996 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6997 BFD_ASSERT (s != NULL);
6998
4ad4eba5 6999 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7000
eea6121a 7001 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7002
7003 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7004 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7005 return FALSE;
7006 }
7007
7008 return TRUE;
7009}
4d269e42 7010\f
4d269e42
AM
7011/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7012
7013static void
7014merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7015 asection *sec)
7016{
dbaa2011
AM
7017 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7018 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7019}
7020
7021/* Finish SHF_MERGE section merging. */
7022
7023bfd_boolean
630993ec 7024_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7025{
7026 bfd *ibfd;
7027 asection *sec;
7028
7029 if (!is_elf_hash_table (info->hash))
7030 return FALSE;
7031
c72f2fb2 7032 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7033 if ((ibfd->flags & DYNAMIC) == 0
7034 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7035 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7036 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7037 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7038 if ((sec->flags & SEC_MERGE) != 0
7039 && !bfd_is_abs_section (sec->output_section))
7040 {
7041 struct bfd_elf_section_data *secdata;
7042
7043 secdata = elf_section_data (sec);
630993ec 7044 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7045 &elf_hash_table (info)->merge_info,
7046 sec, &secdata->sec_info))
7047 return FALSE;
7048 else if (secdata->sec_info)
dbaa2011 7049 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7050 }
7051
7052 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7053 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7054 merge_sections_remove_hook);
7055 return TRUE;
7056}
7057
7058/* Create an entry in an ELF linker hash table. */
7059
7060struct bfd_hash_entry *
7061_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7062 struct bfd_hash_table *table,
7063 const char *string)
7064{
7065 /* Allocate the structure if it has not already been allocated by a
7066 subclass. */
7067 if (entry == NULL)
7068 {
a50b1753 7069 entry = (struct bfd_hash_entry *)
ca4be51c 7070 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7071 if (entry == NULL)
7072 return entry;
7073 }
7074
7075 /* Call the allocation method of the superclass. */
7076 entry = _bfd_link_hash_newfunc (entry, table, string);
7077 if (entry != NULL)
7078 {
7079 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7080 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7081
7082 /* Set local fields. */
7083 ret->indx = -1;
7084 ret->dynindx = -1;
7085 ret->got = htab->init_got_refcount;
7086 ret->plt = htab->init_plt_refcount;
7087 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7088 - offsetof (struct elf_link_hash_entry, size)));
7089 /* Assume that we have been called by a non-ELF symbol reader.
7090 This flag is then reset by the code which reads an ELF input
7091 file. This ensures that a symbol created by a non-ELF symbol
7092 reader will have the flag set correctly. */
7093 ret->non_elf = 1;
7094 }
7095
7096 return entry;
7097}
7098
7099/* Copy data from an indirect symbol to its direct symbol, hiding the
7100 old indirect symbol. Also used for copying flags to a weakdef. */
7101
7102void
7103_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7104 struct elf_link_hash_entry *dir,
7105 struct elf_link_hash_entry *ind)
7106{
7107 struct elf_link_hash_table *htab;
7108
7109 /* Copy down any references that we may have already seen to the
e81830c5 7110 symbol which just became indirect. */
4d269e42 7111
422f1182 7112 if (dir->versioned != versioned_hidden)
e81830c5
AM
7113 dir->ref_dynamic |= ind->ref_dynamic;
7114 dir->ref_regular |= ind->ref_regular;
7115 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7116 dir->non_got_ref |= ind->non_got_ref;
7117 dir->needs_plt |= ind->needs_plt;
7118 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7119
7120 if (ind->root.type != bfd_link_hash_indirect)
7121 return;
7122
7123 /* Copy over the global and procedure linkage table refcount entries.
7124 These may have been already set up by a check_relocs routine. */
7125 htab = elf_hash_table (info);
7126 if (ind->got.refcount > htab->init_got_refcount.refcount)
7127 {
7128 if (dir->got.refcount < 0)
7129 dir->got.refcount = 0;
7130 dir->got.refcount += ind->got.refcount;
7131 ind->got.refcount = htab->init_got_refcount.refcount;
7132 }
7133
7134 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7135 {
7136 if (dir->plt.refcount < 0)
7137 dir->plt.refcount = 0;
7138 dir->plt.refcount += ind->plt.refcount;
7139 ind->plt.refcount = htab->init_plt_refcount.refcount;
7140 }
7141
7142 if (ind->dynindx != -1)
7143 {
7144 if (dir->dynindx != -1)
7145 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7146 dir->dynindx = ind->dynindx;
7147 dir->dynstr_index = ind->dynstr_index;
7148 ind->dynindx = -1;
7149 ind->dynstr_index = 0;
7150 }
7151}
7152
7153void
7154_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7155 struct elf_link_hash_entry *h,
7156 bfd_boolean force_local)
7157{
3aa14d16
L
7158 /* STT_GNU_IFUNC symbol must go through PLT. */
7159 if (h->type != STT_GNU_IFUNC)
7160 {
7161 h->plt = elf_hash_table (info)->init_plt_offset;
7162 h->needs_plt = 0;
7163 }
4d269e42
AM
7164 if (force_local)
7165 {
7166 h->forced_local = 1;
7167 if (h->dynindx != -1)
7168 {
7169 h->dynindx = -1;
7170 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7171 h->dynstr_index);
7172 }
7173 }
7174}
7175
7bf52ea2
AM
7176/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7177 caller. */
4d269e42
AM
7178
7179bfd_boolean
7180_bfd_elf_link_hash_table_init
7181 (struct elf_link_hash_table *table,
7182 bfd *abfd,
7183 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7184 struct bfd_hash_table *,
7185 const char *),
4dfe6ac6
NC
7186 unsigned int entsize,
7187 enum elf_target_id target_id)
4d269e42
AM
7188{
7189 bfd_boolean ret;
7190 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7191
4d269e42
AM
7192 table->init_got_refcount.refcount = can_refcount - 1;
7193 table->init_plt_refcount.refcount = can_refcount - 1;
7194 table->init_got_offset.offset = -(bfd_vma) 1;
7195 table->init_plt_offset.offset = -(bfd_vma) 1;
7196 /* The first dynamic symbol is a dummy. */
7197 table->dynsymcount = 1;
7198
7199 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7200
4d269e42 7201 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7202 table->hash_table_id = target_id;
4d269e42
AM
7203
7204 return ret;
7205}
7206
7207/* Create an ELF linker hash table. */
7208
7209struct bfd_link_hash_table *
7210_bfd_elf_link_hash_table_create (bfd *abfd)
7211{
7212 struct elf_link_hash_table *ret;
7213 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7214
7bf52ea2 7215 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7216 if (ret == NULL)
7217 return NULL;
7218
7219 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7220 sizeof (struct elf_link_hash_entry),
7221 GENERIC_ELF_DATA))
4d269e42
AM
7222 {
7223 free (ret);
7224 return NULL;
7225 }
d495ab0d 7226 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7227
7228 return &ret->root;
7229}
7230
9f7c3e5e
AM
7231/* Destroy an ELF linker hash table. */
7232
7233void
d495ab0d 7234_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7235{
d495ab0d
AM
7236 struct elf_link_hash_table *htab;
7237
7238 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7239 if (htab->dynstr != NULL)
7240 _bfd_elf_strtab_free (htab->dynstr);
7241 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7242 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7243}
7244
4d269e42
AM
7245/* This is a hook for the ELF emulation code in the generic linker to
7246 tell the backend linker what file name to use for the DT_NEEDED
7247 entry for a dynamic object. */
7248
7249void
7250bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7251{
7252 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7253 && bfd_get_format (abfd) == bfd_object)
7254 elf_dt_name (abfd) = name;
7255}
7256
7257int
7258bfd_elf_get_dyn_lib_class (bfd *abfd)
7259{
7260 int lib_class;
7261 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7262 && bfd_get_format (abfd) == bfd_object)
7263 lib_class = elf_dyn_lib_class (abfd);
7264 else
7265 lib_class = 0;
7266 return lib_class;
7267}
7268
7269void
7270bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7271{
7272 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7273 && bfd_get_format (abfd) == bfd_object)
7274 elf_dyn_lib_class (abfd) = lib_class;
7275}
7276
7277/* Get the list of DT_NEEDED entries for a link. This is a hook for
7278 the linker ELF emulation code. */
7279
7280struct bfd_link_needed_list *
7281bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7282 struct bfd_link_info *info)
7283{
7284 if (! is_elf_hash_table (info->hash))
7285 return NULL;
7286 return elf_hash_table (info)->needed;
7287}
7288
7289/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7290 hook for the linker ELF emulation code. */
7291
7292struct bfd_link_needed_list *
7293bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7294 struct bfd_link_info *info)
7295{
7296 if (! is_elf_hash_table (info->hash))
7297 return NULL;
7298 return elf_hash_table (info)->runpath;
7299}
7300
7301/* Get the name actually used for a dynamic object for a link. This
7302 is the SONAME entry if there is one. Otherwise, it is the string
7303 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7304
7305const char *
7306bfd_elf_get_dt_soname (bfd *abfd)
7307{
7308 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7309 && bfd_get_format (abfd) == bfd_object)
7310 return elf_dt_name (abfd);
7311 return NULL;
7312}
7313
7314/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7315 the ELF linker emulation code. */
7316
7317bfd_boolean
7318bfd_elf_get_bfd_needed_list (bfd *abfd,
7319 struct bfd_link_needed_list **pneeded)
7320{
7321 asection *s;
7322 bfd_byte *dynbuf = NULL;
cb33740c 7323 unsigned int elfsec;
4d269e42
AM
7324 unsigned long shlink;
7325 bfd_byte *extdyn, *extdynend;
7326 size_t extdynsize;
7327 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7328
7329 *pneeded = NULL;
7330
7331 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7332 || bfd_get_format (abfd) != bfd_object)
7333 return TRUE;
7334
7335 s = bfd_get_section_by_name (abfd, ".dynamic");
7336 if (s == NULL || s->size == 0)
7337 return TRUE;
7338
7339 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7340 goto error_return;
7341
7342 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7343 if (elfsec == SHN_BAD)
4d269e42
AM
7344 goto error_return;
7345
7346 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7347
4d269e42
AM
7348 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7349 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7350
7351 extdyn = dynbuf;
7352 extdynend = extdyn + s->size;
7353 for (; extdyn < extdynend; extdyn += extdynsize)
7354 {
7355 Elf_Internal_Dyn dyn;
7356
7357 (*swap_dyn_in) (abfd, extdyn, &dyn);
7358
7359 if (dyn.d_tag == DT_NULL)
7360 break;
7361
7362 if (dyn.d_tag == DT_NEEDED)
7363 {
7364 const char *string;
7365 struct bfd_link_needed_list *l;
7366 unsigned int tagv = dyn.d_un.d_val;
7367 bfd_size_type amt;
7368
7369 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7370 if (string == NULL)
7371 goto error_return;
7372
7373 amt = sizeof *l;
a50b1753 7374 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7375 if (l == NULL)
7376 goto error_return;
7377
7378 l->by = abfd;
7379 l->name = string;
7380 l->next = *pneeded;
7381 *pneeded = l;
7382 }
7383 }
7384
7385 free (dynbuf);
7386
7387 return TRUE;
7388
7389 error_return:
7390 if (dynbuf != NULL)
7391 free (dynbuf);
7392 return FALSE;
7393}
7394
7395struct elf_symbuf_symbol
7396{
7397 unsigned long st_name; /* Symbol name, index in string tbl */
7398 unsigned char st_info; /* Type and binding attributes */
7399 unsigned char st_other; /* Visibilty, and target specific */
7400};
7401
7402struct elf_symbuf_head
7403{
7404 struct elf_symbuf_symbol *ssym;
ef53be89 7405 size_t count;
4d269e42
AM
7406 unsigned int st_shndx;
7407};
7408
7409struct elf_symbol
7410{
7411 union
7412 {
7413 Elf_Internal_Sym *isym;
7414 struct elf_symbuf_symbol *ssym;
7415 } u;
7416 const char *name;
7417};
7418
7419/* Sort references to symbols by ascending section number. */
7420
7421static int
7422elf_sort_elf_symbol (const void *arg1, const void *arg2)
7423{
7424 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7425 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7426
7427 return s1->st_shndx - s2->st_shndx;
7428}
7429
7430static int
7431elf_sym_name_compare (const void *arg1, const void *arg2)
7432{
7433 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7434 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7435 return strcmp (s1->name, s2->name);
7436}
7437
7438static struct elf_symbuf_head *
ef53be89 7439elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7440{
14b1c01e 7441 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7442 struct elf_symbuf_symbol *ssym;
7443 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7444 size_t i, shndx_count, total_size;
4d269e42 7445
a50b1753 7446 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7447 if (indbuf == NULL)
7448 return NULL;
7449
7450 for (ind = indbuf, i = 0; i < symcount; i++)
7451 if (isymbuf[i].st_shndx != SHN_UNDEF)
7452 *ind++ = &isymbuf[i];
7453 indbufend = ind;
7454
7455 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7456 elf_sort_elf_symbol);
7457
7458 shndx_count = 0;
7459 if (indbufend > indbuf)
7460 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7461 if (ind[0]->st_shndx != ind[1]->st_shndx)
7462 shndx_count++;
7463
3ae181ee
L
7464 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7465 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7466 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7467 if (ssymbuf == NULL)
7468 {
7469 free (indbuf);
7470 return NULL;
7471 }
7472
3ae181ee 7473 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7474 ssymbuf->ssym = NULL;
7475 ssymbuf->count = shndx_count;
7476 ssymbuf->st_shndx = 0;
7477 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7478 {
7479 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7480 {
7481 ssymhead++;
7482 ssymhead->ssym = ssym;
7483 ssymhead->count = 0;
7484 ssymhead->st_shndx = (*ind)->st_shndx;
7485 }
7486 ssym->st_name = (*ind)->st_name;
7487 ssym->st_info = (*ind)->st_info;
7488 ssym->st_other = (*ind)->st_other;
7489 ssymhead->count++;
7490 }
ef53be89 7491 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7492 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7493 == total_size));
4d269e42
AM
7494
7495 free (indbuf);
7496 return ssymbuf;
7497}
7498
7499/* Check if 2 sections define the same set of local and global
7500 symbols. */
7501
8f317e31 7502static bfd_boolean
4d269e42
AM
7503bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7504 struct bfd_link_info *info)
7505{
7506 bfd *bfd1, *bfd2;
7507 const struct elf_backend_data *bed1, *bed2;
7508 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7509 size_t symcount1, symcount2;
4d269e42
AM
7510 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7511 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7512 Elf_Internal_Sym *isym, *isymend;
7513 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7514 size_t count1, count2, i;
cb33740c 7515 unsigned int shndx1, shndx2;
4d269e42
AM
7516 bfd_boolean result;
7517
7518 bfd1 = sec1->owner;
7519 bfd2 = sec2->owner;
7520
4d269e42
AM
7521 /* Both sections have to be in ELF. */
7522 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7523 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7524 return FALSE;
7525
7526 if (elf_section_type (sec1) != elf_section_type (sec2))
7527 return FALSE;
7528
4d269e42
AM
7529 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7530 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7531 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7532 return FALSE;
7533
7534 bed1 = get_elf_backend_data (bfd1);
7535 bed2 = get_elf_backend_data (bfd2);
7536 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7537 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7538 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7539 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7540
7541 if (symcount1 == 0 || symcount2 == 0)
7542 return FALSE;
7543
7544 result = FALSE;
7545 isymbuf1 = NULL;
7546 isymbuf2 = NULL;
a50b1753
NC
7547 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7548 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7549
7550 if (ssymbuf1 == NULL)
7551 {
7552 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7553 NULL, NULL, NULL);
7554 if (isymbuf1 == NULL)
7555 goto done;
7556
7557 if (!info->reduce_memory_overheads)
7558 elf_tdata (bfd1)->symbuf = ssymbuf1
7559 = elf_create_symbuf (symcount1, isymbuf1);
7560 }
7561
7562 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7563 {
7564 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7565 NULL, NULL, NULL);
7566 if (isymbuf2 == NULL)
7567 goto done;
7568
7569 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7570 elf_tdata (bfd2)->symbuf = ssymbuf2
7571 = elf_create_symbuf (symcount2, isymbuf2);
7572 }
7573
7574 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7575 {
7576 /* Optimized faster version. */
ef53be89 7577 size_t lo, hi, mid;
4d269e42
AM
7578 struct elf_symbol *symp;
7579 struct elf_symbuf_symbol *ssym, *ssymend;
7580
7581 lo = 0;
7582 hi = ssymbuf1->count;
7583 ssymbuf1++;
7584 count1 = 0;
7585 while (lo < hi)
7586 {
7587 mid = (lo + hi) / 2;
cb33740c 7588 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7589 hi = mid;
cb33740c 7590 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7591 lo = mid + 1;
7592 else
7593 {
7594 count1 = ssymbuf1[mid].count;
7595 ssymbuf1 += mid;
7596 break;
7597 }
7598 }
7599
7600 lo = 0;
7601 hi = ssymbuf2->count;
7602 ssymbuf2++;
7603 count2 = 0;
7604 while (lo < hi)
7605 {
7606 mid = (lo + hi) / 2;
cb33740c 7607 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7608 hi = mid;
cb33740c 7609 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7610 lo = mid + 1;
7611 else
7612 {
7613 count2 = ssymbuf2[mid].count;
7614 ssymbuf2 += mid;
7615 break;
7616 }
7617 }
7618
7619 if (count1 == 0 || count2 == 0 || count1 != count2)
7620 goto done;
7621
ca4be51c
AM
7622 symtable1
7623 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7624 symtable2
7625 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7626 if (symtable1 == NULL || symtable2 == NULL)
7627 goto done;
7628
7629 symp = symtable1;
7630 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7631 ssym < ssymend; ssym++, symp++)
7632 {
7633 symp->u.ssym = ssym;
7634 symp->name = bfd_elf_string_from_elf_section (bfd1,
7635 hdr1->sh_link,
7636 ssym->st_name);
7637 }
7638
7639 symp = symtable2;
7640 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7641 ssym < ssymend; ssym++, symp++)
7642 {
7643 symp->u.ssym = ssym;
7644 symp->name = bfd_elf_string_from_elf_section (bfd2,
7645 hdr2->sh_link,
7646 ssym->st_name);
7647 }
7648
7649 /* Sort symbol by name. */
7650 qsort (symtable1, count1, sizeof (struct elf_symbol),
7651 elf_sym_name_compare);
7652 qsort (symtable2, count1, sizeof (struct elf_symbol),
7653 elf_sym_name_compare);
7654
7655 for (i = 0; i < count1; i++)
7656 /* Two symbols must have the same binding, type and name. */
7657 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7658 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7659 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7660 goto done;
7661
7662 result = TRUE;
7663 goto done;
7664 }
7665
a50b1753
NC
7666 symtable1 = (struct elf_symbol *)
7667 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7668 symtable2 = (struct elf_symbol *)
7669 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7670 if (symtable1 == NULL || symtable2 == NULL)
7671 goto done;
7672
7673 /* Count definitions in the section. */
7674 count1 = 0;
7675 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7676 if (isym->st_shndx == shndx1)
4d269e42
AM
7677 symtable1[count1++].u.isym = isym;
7678
7679 count2 = 0;
7680 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7681 if (isym->st_shndx == shndx2)
4d269e42
AM
7682 symtable2[count2++].u.isym = isym;
7683
7684 if (count1 == 0 || count2 == 0 || count1 != count2)
7685 goto done;
7686
7687 for (i = 0; i < count1; i++)
7688 symtable1[i].name
7689 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7690 symtable1[i].u.isym->st_name);
7691
7692 for (i = 0; i < count2; i++)
7693 symtable2[i].name
7694 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7695 symtable2[i].u.isym->st_name);
7696
7697 /* Sort symbol by name. */
7698 qsort (symtable1, count1, sizeof (struct elf_symbol),
7699 elf_sym_name_compare);
7700 qsort (symtable2, count1, sizeof (struct elf_symbol),
7701 elf_sym_name_compare);
7702
7703 for (i = 0; i < count1; i++)
7704 /* Two symbols must have the same binding, type and name. */
7705 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7706 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7707 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7708 goto done;
7709
7710 result = TRUE;
7711
7712done:
7713 if (symtable1)
7714 free (symtable1);
7715 if (symtable2)
7716 free (symtable2);
7717 if (isymbuf1)
7718 free (isymbuf1);
7719 if (isymbuf2)
7720 free (isymbuf2);
7721
7722 return result;
7723}
7724
7725/* Return TRUE if 2 section types are compatible. */
7726
7727bfd_boolean
7728_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7729 bfd *bbfd, const asection *bsec)
7730{
7731 if (asec == NULL
7732 || bsec == NULL
7733 || abfd->xvec->flavour != bfd_target_elf_flavour
7734 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7735 return TRUE;
7736
7737 return elf_section_type (asec) == elf_section_type (bsec);
7738}
7739\f
c152c796
AM
7740/* Final phase of ELF linker. */
7741
7742/* A structure we use to avoid passing large numbers of arguments. */
7743
7744struct elf_final_link_info
7745{
7746 /* General link information. */
7747 struct bfd_link_info *info;
7748 /* Output BFD. */
7749 bfd *output_bfd;
7750 /* Symbol string table. */
ef10c3ac 7751 struct elf_strtab_hash *symstrtab;
c152c796
AM
7752 /* .hash section. */
7753 asection *hash_sec;
7754 /* symbol version section (.gnu.version). */
7755 asection *symver_sec;
7756 /* Buffer large enough to hold contents of any section. */
7757 bfd_byte *contents;
7758 /* Buffer large enough to hold external relocs of any section. */
7759 void *external_relocs;
7760 /* Buffer large enough to hold internal relocs of any section. */
7761 Elf_Internal_Rela *internal_relocs;
7762 /* Buffer large enough to hold external local symbols of any input
7763 BFD. */
7764 bfd_byte *external_syms;
7765 /* And a buffer for symbol section indices. */
7766 Elf_External_Sym_Shndx *locsym_shndx;
7767 /* Buffer large enough to hold internal local symbols of any input
7768 BFD. */
7769 Elf_Internal_Sym *internal_syms;
7770 /* Array large enough to hold a symbol index for each local symbol
7771 of any input BFD. */
7772 long *indices;
7773 /* Array large enough to hold a section pointer for each local
7774 symbol of any input BFD. */
7775 asection **sections;
ef10c3ac 7776 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7777 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7778 /* Number of STT_FILE syms seen. */
7779 size_t filesym_count;
c152c796
AM
7780};
7781
7782/* This struct is used to pass information to elf_link_output_extsym. */
7783
7784struct elf_outext_info
7785{
7786 bfd_boolean failed;
7787 bfd_boolean localsyms;
34a79995 7788 bfd_boolean file_sym_done;
8b127cbc 7789 struct elf_final_link_info *flinfo;
c152c796
AM
7790};
7791
d9352518
DB
7792
7793/* Support for evaluating a complex relocation.
7794
7795 Complex relocations are generalized, self-describing relocations. The
7796 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7797 relocations themselves.
d9352518
DB
7798
7799 The relocations are use a reserved elf-wide relocation type code (R_RELC
7800 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7801 information (start bit, end bit, word width, etc) into the addend. This
7802 information is extracted from CGEN-generated operand tables within gas.
7803
7804 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7805 internal) representing prefix-notation expressions, including but not
7806 limited to those sorts of expressions normally encoded as addends in the
7807 addend field. The symbol mangling format is:
7808
7809 <node> := <literal>
7810 | <unary-operator> ':' <node>
7811 | <binary-operator> ':' <node> ':' <node>
7812 ;
7813
7814 <literal> := 's' <digits=N> ':' <N character symbol name>
7815 | 'S' <digits=N> ':' <N character section name>
7816 | '#' <hexdigits>
7817 ;
7818
7819 <binary-operator> := as in C
7820 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7821
7822static void
a0c8462f
AM
7823set_symbol_value (bfd *bfd_with_globals,
7824 Elf_Internal_Sym *isymbuf,
7825 size_t locsymcount,
7826 size_t symidx,
7827 bfd_vma val)
d9352518 7828{
8977835c
AM
7829 struct elf_link_hash_entry **sym_hashes;
7830 struct elf_link_hash_entry *h;
7831 size_t extsymoff = locsymcount;
d9352518 7832
8977835c 7833 if (symidx < locsymcount)
d9352518 7834 {
8977835c
AM
7835 Elf_Internal_Sym *sym;
7836
7837 sym = isymbuf + symidx;
7838 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7839 {
7840 /* It is a local symbol: move it to the
7841 "absolute" section and give it a value. */
7842 sym->st_shndx = SHN_ABS;
7843 sym->st_value = val;
7844 return;
7845 }
7846 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7847 extsymoff = 0;
d9352518 7848 }
8977835c
AM
7849
7850 /* It is a global symbol: set its link type
7851 to "defined" and give it a value. */
7852
7853 sym_hashes = elf_sym_hashes (bfd_with_globals);
7854 h = sym_hashes [symidx - extsymoff];
7855 while (h->root.type == bfd_link_hash_indirect
7856 || h->root.type == bfd_link_hash_warning)
7857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7858 h->root.type = bfd_link_hash_defined;
7859 h->root.u.def.value = val;
7860 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7861}
7862
a0c8462f
AM
7863static bfd_boolean
7864resolve_symbol (const char *name,
7865 bfd *input_bfd,
8b127cbc 7866 struct elf_final_link_info *flinfo,
a0c8462f
AM
7867 bfd_vma *result,
7868 Elf_Internal_Sym *isymbuf,
7869 size_t locsymcount)
d9352518 7870{
a0c8462f
AM
7871 Elf_Internal_Sym *sym;
7872 struct bfd_link_hash_entry *global_entry;
7873 const char *candidate = NULL;
7874 Elf_Internal_Shdr *symtab_hdr;
7875 size_t i;
7876
d9352518
DB
7877 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7878
7879 for (i = 0; i < locsymcount; ++ i)
7880 {
8977835c 7881 sym = isymbuf + i;
d9352518
DB
7882
7883 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7884 continue;
7885
7886 candidate = bfd_elf_string_from_elf_section (input_bfd,
7887 symtab_hdr->sh_link,
7888 sym->st_name);
7889#ifdef DEBUG
0f02bbd9
AM
7890 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7891 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7892#endif
7893 if (candidate && strcmp (candidate, name) == 0)
7894 {
8b127cbc 7895 asection *sec = flinfo->sections [i];
d9352518 7896
0f02bbd9
AM
7897 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7898 *result += sec->output_offset + sec->output_section->vma;
d9352518 7899#ifdef DEBUG
0f02bbd9
AM
7900 printf ("Found symbol with value %8.8lx\n",
7901 (unsigned long) *result);
d9352518
DB
7902#endif
7903 return TRUE;
7904 }
7905 }
7906
7907 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7908 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7909 FALSE, FALSE, TRUE);
d9352518
DB
7910 if (!global_entry)
7911 return FALSE;
a0c8462f 7912
d9352518
DB
7913 if (global_entry->type == bfd_link_hash_defined
7914 || global_entry->type == bfd_link_hash_defweak)
7915 {
a0c8462f
AM
7916 *result = (global_entry->u.def.value
7917 + global_entry->u.def.section->output_section->vma
7918 + global_entry->u.def.section->output_offset);
d9352518 7919#ifdef DEBUG
0f02bbd9
AM
7920 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7921 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7922#endif
7923 return TRUE;
a0c8462f 7924 }
d9352518 7925
d9352518
DB
7926 return FALSE;
7927}
7928
37b01f6a
DG
7929/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7930 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7931 names like "foo.end" which is the end address of section "foo". */
7932
d9352518 7933static bfd_boolean
a0c8462f
AM
7934resolve_section (const char *name,
7935 asection *sections,
37b01f6a
DG
7936 bfd_vma *result,
7937 bfd * abfd)
d9352518 7938{
a0c8462f
AM
7939 asection *curr;
7940 unsigned int len;
d9352518 7941
a0c8462f 7942 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7943 if (strcmp (curr->name, name) == 0)
7944 {
7945 *result = curr->vma;
7946 return TRUE;
7947 }
7948
7949 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7950 /* FIXME: This could be coded more efficiently... */
a0c8462f 7951 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7952 {
7953 len = strlen (curr->name);
a0c8462f 7954 if (len > strlen (name))
d9352518
DB
7955 continue;
7956
7957 if (strncmp (curr->name, name, len) == 0)
7958 {
7959 if (strncmp (".end", name + len, 4) == 0)
7960 {
37b01f6a 7961 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7962 return TRUE;
7963 }
7964
7965 /* Insert more pseudo-section names here, if you like. */
7966 }
7967 }
a0c8462f 7968
d9352518
DB
7969 return FALSE;
7970}
7971
7972static void
a0c8462f 7973undefined_reference (const char *reftype, const char *name)
d9352518 7974{
695344c0 7975 /* xgettext:c-format */
a0c8462f
AM
7976 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7977 reftype, name);
d9352518
DB
7978}
7979
7980static bfd_boolean
a0c8462f
AM
7981eval_symbol (bfd_vma *result,
7982 const char **symp,
7983 bfd *input_bfd,
8b127cbc 7984 struct elf_final_link_info *flinfo,
a0c8462f
AM
7985 bfd_vma dot,
7986 Elf_Internal_Sym *isymbuf,
7987 size_t locsymcount,
7988 int signed_p)
d9352518 7989{
4b93929b
NC
7990 size_t len;
7991 size_t symlen;
a0c8462f
AM
7992 bfd_vma a;
7993 bfd_vma b;
4b93929b 7994 char symbuf[4096];
0f02bbd9 7995 const char *sym = *symp;
a0c8462f
AM
7996 const char *symend;
7997 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7998
7999 len = strlen (sym);
8000 symend = sym + len;
8001
4b93929b 8002 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8003 {
8004 bfd_set_error (bfd_error_invalid_operation);
8005 return FALSE;
8006 }
a0c8462f 8007
d9352518
DB
8008 switch (* sym)
8009 {
8010 case '.':
0f02bbd9
AM
8011 *result = dot;
8012 *symp = sym + 1;
d9352518
DB
8013 return TRUE;
8014
8015 case '#':
0f02bbd9
AM
8016 ++sym;
8017 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8018 return TRUE;
8019
8020 case 'S':
8021 symbol_is_section = TRUE;
1a0670f3 8022 /* Fall through. */
a0c8462f 8023 case 's':
0f02bbd9
AM
8024 ++sym;
8025 symlen = strtol (sym, (char **) symp, 10);
8026 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8027
4b93929b 8028 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8029 {
8030 bfd_set_error (bfd_error_invalid_operation);
8031 return FALSE;
8032 }
8033
8034 memcpy (symbuf, sym, symlen);
a0c8462f 8035 symbuf[symlen] = '\0';
0f02bbd9 8036 *symp = sym + symlen;
a0c8462f
AM
8037
8038 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8039 the symbol as a section, or vice-versa. so we're pretty liberal in our
8040 interpretation here; section means "try section first", not "must be a
8041 section", and likewise with symbol. */
8042
a0c8462f 8043 if (symbol_is_section)
d9352518 8044 {
37b01f6a 8045 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8046 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8047 isymbuf, locsymcount))
d9352518
DB
8048 {
8049 undefined_reference ("section", symbuf);
8050 return FALSE;
8051 }
a0c8462f
AM
8052 }
8053 else
d9352518 8054 {
8b127cbc 8055 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8056 isymbuf, locsymcount)
8b127cbc 8057 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8058 result, input_bfd))
d9352518
DB
8059 {
8060 undefined_reference ("symbol", symbuf);
8061 return FALSE;
8062 }
8063 }
8064
8065 return TRUE;
a0c8462f 8066
d9352518
DB
8067 /* All that remains are operators. */
8068
8069#define UNARY_OP(op) \
8070 if (strncmp (sym, #op, strlen (#op)) == 0) \
8071 { \
8072 sym += strlen (#op); \
a0c8462f
AM
8073 if (*sym == ':') \
8074 ++sym; \
0f02bbd9 8075 *symp = sym; \
8b127cbc 8076 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8077 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8078 return FALSE; \
8079 if (signed_p) \
0f02bbd9 8080 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8081 else \
8082 *result = op a; \
d9352518
DB
8083 return TRUE; \
8084 }
8085
8086#define BINARY_OP(op) \
8087 if (strncmp (sym, #op, strlen (#op)) == 0) \
8088 { \
8089 sym += strlen (#op); \
a0c8462f
AM
8090 if (*sym == ':') \
8091 ++sym; \
0f02bbd9 8092 *symp = sym; \
8b127cbc 8093 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8094 isymbuf, locsymcount, signed_p)) \
a0c8462f 8095 return FALSE; \
0f02bbd9 8096 ++*symp; \
8b127cbc 8097 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8098 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8099 return FALSE; \
8100 if (signed_p) \
0f02bbd9 8101 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8102 else \
8103 *result = a op b; \
d9352518
DB
8104 return TRUE; \
8105 }
8106
8107 default:
8108 UNARY_OP (0-);
8109 BINARY_OP (<<);
8110 BINARY_OP (>>);
8111 BINARY_OP (==);
8112 BINARY_OP (!=);
8113 BINARY_OP (<=);
8114 BINARY_OP (>=);
8115 BINARY_OP (&&);
8116 BINARY_OP (||);
8117 UNARY_OP (~);
8118 UNARY_OP (!);
8119 BINARY_OP (*);
8120 BINARY_OP (/);
8121 BINARY_OP (%);
8122 BINARY_OP (^);
8123 BINARY_OP (|);
8124 BINARY_OP (&);
8125 BINARY_OP (+);
8126 BINARY_OP (-);
8127 BINARY_OP (<);
8128 BINARY_OP (>);
8129#undef UNARY_OP
8130#undef BINARY_OP
8131 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8132 bfd_set_error (bfd_error_invalid_operation);
8133 return FALSE;
8134 }
8135}
8136
d9352518 8137static void
a0c8462f
AM
8138put_value (bfd_vma size,
8139 unsigned long chunksz,
8140 bfd *input_bfd,
8141 bfd_vma x,
8142 bfd_byte *location)
d9352518
DB
8143{
8144 location += (size - chunksz);
8145
41cd1ad1 8146 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8147 {
8148 switch (chunksz)
8149 {
d9352518
DB
8150 case 1:
8151 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8152 x >>= 8;
d9352518
DB
8153 break;
8154 case 2:
8155 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8156 x >>= 16;
d9352518
DB
8157 break;
8158 case 4:
8159 bfd_put_32 (input_bfd, x, location);
65164438
NC
8160 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8161 x >>= 16;
8162 x >>= 16;
d9352518 8163 break;
d9352518 8164#ifdef BFD64
41cd1ad1 8165 case 8:
d9352518 8166 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8167 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8168 x >>= 32;
8169 x >>= 32;
8170 break;
d9352518 8171#endif
41cd1ad1
NC
8172 default:
8173 abort ();
d9352518
DB
8174 break;
8175 }
8176 }
8177}
8178
a0c8462f
AM
8179static bfd_vma
8180get_value (bfd_vma size,
8181 unsigned long chunksz,
8182 bfd *input_bfd,
8183 bfd_byte *location)
d9352518 8184{
9b239e0e 8185 int shift;
d9352518
DB
8186 bfd_vma x = 0;
8187
9b239e0e
NC
8188 /* Sanity checks. */
8189 BFD_ASSERT (chunksz <= sizeof (x)
8190 && size >= chunksz
8191 && chunksz != 0
8192 && (size % chunksz) == 0
8193 && input_bfd != NULL
8194 && location != NULL);
8195
8196 if (chunksz == sizeof (x))
8197 {
8198 BFD_ASSERT (size == chunksz);
8199
8200 /* Make sure that we do not perform an undefined shift operation.
8201 We know that size == chunksz so there will only be one iteration
8202 of the loop below. */
8203 shift = 0;
8204 }
8205 else
8206 shift = 8 * chunksz;
8207
a0c8462f 8208 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8209 {
8210 switch (chunksz)
8211 {
d9352518 8212 case 1:
9b239e0e 8213 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8214 break;
8215 case 2:
9b239e0e 8216 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8217 break;
8218 case 4:
9b239e0e 8219 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8220 break;
d9352518 8221#ifdef BFD64
9b239e0e
NC
8222 case 8:
8223 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8224 break;
9b239e0e
NC
8225#endif
8226 default:
8227 abort ();
d9352518
DB
8228 }
8229 }
8230 return x;
8231}
8232
a0c8462f
AM
8233static void
8234decode_complex_addend (unsigned long *start, /* in bits */
8235 unsigned long *oplen, /* in bits */
8236 unsigned long *len, /* in bits */
8237 unsigned long *wordsz, /* in bytes */
8238 unsigned long *chunksz, /* in bytes */
8239 unsigned long *lsb0_p,
8240 unsigned long *signed_p,
8241 unsigned long *trunc_p,
8242 unsigned long encoded)
d9352518
DB
8243{
8244 * start = encoded & 0x3F;
8245 * len = (encoded >> 6) & 0x3F;
8246 * oplen = (encoded >> 12) & 0x3F;
8247 * wordsz = (encoded >> 18) & 0xF;
8248 * chunksz = (encoded >> 22) & 0xF;
8249 * lsb0_p = (encoded >> 27) & 1;
8250 * signed_p = (encoded >> 28) & 1;
8251 * trunc_p = (encoded >> 29) & 1;
8252}
8253
cdfeee4f 8254bfd_reloc_status_type
0f02bbd9 8255bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8256 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8257 bfd_byte *contents,
8258 Elf_Internal_Rela *rel,
8259 bfd_vma relocation)
d9352518 8260{
0f02bbd9
AM
8261 bfd_vma shift, x, mask;
8262 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8263 bfd_reloc_status_type r;
d9352518
DB
8264
8265 /* Perform this reloc, since it is complex.
8266 (this is not to say that it necessarily refers to a complex
8267 symbol; merely that it is a self-describing CGEN based reloc.
8268 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8269 word size, etc) encoded within it.). */
d9352518 8270
a0c8462f
AM
8271 decode_complex_addend (&start, &oplen, &len, &wordsz,
8272 &chunksz, &lsb0_p, &signed_p,
8273 &trunc_p, rel->r_addend);
d9352518
DB
8274
8275 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8276
8277 if (lsb0_p)
8278 shift = (start + 1) - len;
8279 else
8280 shift = (8 * wordsz) - (start + len);
8281
37b01f6a
DG
8282 x = get_value (wordsz, chunksz, input_bfd,
8283 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8284
8285#ifdef DEBUG
8286 printf ("Doing complex reloc: "
8287 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8288 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8289 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8290 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8291 oplen, (unsigned long) x, (unsigned long) mask,
8292 (unsigned long) relocation);
d9352518
DB
8293#endif
8294
cdfeee4f 8295 r = bfd_reloc_ok;
d9352518 8296 if (! trunc_p)
cdfeee4f
AM
8297 /* Now do an overflow check. */
8298 r = bfd_check_overflow ((signed_p
8299 ? complain_overflow_signed
8300 : complain_overflow_unsigned),
8301 len, 0, (8 * wordsz),
8302 relocation);
a0c8462f 8303
d9352518
DB
8304 /* Do the deed. */
8305 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8306
8307#ifdef DEBUG
8308 printf (" relocation: %8.8lx\n"
8309 " shifted mask: %8.8lx\n"
8310 " shifted/masked reloc: %8.8lx\n"
8311 " result: %8.8lx\n",
9ccb8af9
AM
8312 (unsigned long) relocation, (unsigned long) (mask << shift),
8313 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8314#endif
37b01f6a
DG
8315 put_value (wordsz, chunksz, input_bfd, x,
8316 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8317 return r;
d9352518
DB
8318}
8319
0e287786
AM
8320/* Functions to read r_offset from external (target order) reloc
8321 entry. Faster than bfd_getl32 et al, because we let the compiler
8322 know the value is aligned. */
53df40a4 8323
0e287786
AM
8324static bfd_vma
8325ext32l_r_offset (const void *p)
53df40a4
AM
8326{
8327 union aligned32
8328 {
8329 uint32_t v;
8330 unsigned char c[4];
8331 };
8332 const union aligned32 *a
0e287786 8333 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8334
8335 uint32_t aval = ( (uint32_t) a->c[0]
8336 | (uint32_t) a->c[1] << 8
8337 | (uint32_t) a->c[2] << 16
8338 | (uint32_t) a->c[3] << 24);
0e287786 8339 return aval;
53df40a4
AM
8340}
8341
0e287786
AM
8342static bfd_vma
8343ext32b_r_offset (const void *p)
53df40a4
AM
8344{
8345 union aligned32
8346 {
8347 uint32_t v;
8348 unsigned char c[4];
8349 };
8350 const union aligned32 *a
0e287786 8351 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8352
8353 uint32_t aval = ( (uint32_t) a->c[0] << 24
8354 | (uint32_t) a->c[1] << 16
8355 | (uint32_t) a->c[2] << 8
8356 | (uint32_t) a->c[3]);
0e287786 8357 return aval;
53df40a4
AM
8358}
8359
8360#ifdef BFD_HOST_64_BIT
0e287786
AM
8361static bfd_vma
8362ext64l_r_offset (const void *p)
53df40a4
AM
8363{
8364 union aligned64
8365 {
8366 uint64_t v;
8367 unsigned char c[8];
8368 };
8369 const union aligned64 *a
0e287786 8370 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8371
8372 uint64_t aval = ( (uint64_t) a->c[0]
8373 | (uint64_t) a->c[1] << 8
8374 | (uint64_t) a->c[2] << 16
8375 | (uint64_t) a->c[3] << 24
8376 | (uint64_t) a->c[4] << 32
8377 | (uint64_t) a->c[5] << 40
8378 | (uint64_t) a->c[6] << 48
8379 | (uint64_t) a->c[7] << 56);
0e287786 8380 return aval;
53df40a4
AM
8381}
8382
0e287786
AM
8383static bfd_vma
8384ext64b_r_offset (const void *p)
53df40a4
AM
8385{
8386 union aligned64
8387 {
8388 uint64_t v;
8389 unsigned char c[8];
8390 };
8391 const union aligned64 *a
0e287786 8392 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8393
8394 uint64_t aval = ( (uint64_t) a->c[0] << 56
8395 | (uint64_t) a->c[1] << 48
8396 | (uint64_t) a->c[2] << 40
8397 | (uint64_t) a->c[3] << 32
8398 | (uint64_t) a->c[4] << 24
8399 | (uint64_t) a->c[5] << 16
8400 | (uint64_t) a->c[6] << 8
8401 | (uint64_t) a->c[7]);
0e287786 8402 return aval;
53df40a4
AM
8403}
8404#endif
8405
c152c796
AM
8406/* When performing a relocatable link, the input relocations are
8407 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8408 referenced must be updated. Update all the relocations found in
8409 RELDATA. */
c152c796 8410
bca6d0e3 8411static bfd_boolean
c152c796 8412elf_link_adjust_relocs (bfd *abfd,
9eaff861 8413 asection *sec,
28dbcedc
AM
8414 struct bfd_elf_section_reloc_data *reldata,
8415 bfd_boolean sort)
c152c796
AM
8416{
8417 unsigned int i;
8418 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8419 bfd_byte *erela;
8420 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8421 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8422 bfd_vma r_type_mask;
8423 int r_sym_shift;
d4730f92
BS
8424 unsigned int count = reldata->count;
8425 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8426
d4730f92 8427 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8428 {
8429 swap_in = bed->s->swap_reloc_in;
8430 swap_out = bed->s->swap_reloc_out;
8431 }
d4730f92 8432 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8433 {
8434 swap_in = bed->s->swap_reloca_in;
8435 swap_out = bed->s->swap_reloca_out;
8436 }
8437 else
8438 abort ();
8439
8440 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8441 abort ();
8442
8443 if (bed->s->arch_size == 32)
8444 {
8445 r_type_mask = 0xff;
8446 r_sym_shift = 8;
8447 }
8448 else
8449 {
8450 r_type_mask = 0xffffffff;
8451 r_sym_shift = 32;
8452 }
8453
d4730f92
BS
8454 erela = reldata->hdr->contents;
8455 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8456 {
8457 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8458 unsigned int j;
8459
8460 if (*rel_hash == NULL)
8461 continue;
8462
8463 BFD_ASSERT ((*rel_hash)->indx >= 0);
8464
8465 (*swap_in) (abfd, erela, irela);
8466 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8467 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8468 | (irela[j].r_info & r_type_mask));
8469 (*swap_out) (abfd, irela, erela);
8470 }
53df40a4 8471
9eaff861
AO
8472 if (bed->elf_backend_update_relocs)
8473 (*bed->elf_backend_update_relocs) (sec, reldata);
8474
0e287786 8475 if (sort && count != 0)
53df40a4 8476 {
0e287786
AM
8477 bfd_vma (*ext_r_off) (const void *);
8478 bfd_vma r_off;
8479 size_t elt_size;
8480 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8481 bfd_byte *buf = NULL;
28dbcedc
AM
8482
8483 if (bed->s->arch_size == 32)
8484 {
8485 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8486 ext_r_off = ext32l_r_offset;
28dbcedc 8487 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8488 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8489 else
8490 abort ();
8491 }
53df40a4 8492 else
28dbcedc 8493 {
53df40a4 8494#ifdef BFD_HOST_64_BIT
28dbcedc 8495 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8496 ext_r_off = ext64l_r_offset;
28dbcedc 8497 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8498 ext_r_off = ext64b_r_offset;
28dbcedc 8499 else
53df40a4 8500#endif
28dbcedc
AM
8501 abort ();
8502 }
0e287786 8503
bca6d0e3
AM
8504 /* Must use a stable sort here. A modified insertion sort,
8505 since the relocs are mostly sorted already. */
0e287786
AM
8506 elt_size = reldata->hdr->sh_entsize;
8507 base = reldata->hdr->contents;
8508 end = base + count * elt_size;
bca6d0e3 8509 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8510 abort ();
8511
8512 /* Ensure the first element is lowest. This acts as a sentinel,
8513 speeding the main loop below. */
8514 r_off = (*ext_r_off) (base);
8515 for (p = loc = base; (p += elt_size) < end; )
8516 {
8517 bfd_vma r_off2 = (*ext_r_off) (p);
8518 if (r_off > r_off2)
8519 {
8520 r_off = r_off2;
8521 loc = p;
8522 }
8523 }
8524 if (loc != base)
8525 {
8526 /* Don't just swap *base and *loc as that changes the order
8527 of the original base[0] and base[1] if they happen to
8528 have the same r_offset. */
bca6d0e3
AM
8529 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8530 memcpy (onebuf, loc, elt_size);
0e287786 8531 memmove (base + elt_size, base, loc - base);
bca6d0e3 8532 memcpy (base, onebuf, elt_size);
0e287786
AM
8533 }
8534
b29b8669 8535 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8536 {
8537 /* base to p is sorted, *p is next to insert. */
8538 r_off = (*ext_r_off) (p);
8539 /* Search the sorted region for location to insert. */
8540 loc = p - elt_size;
8541 while (r_off < (*ext_r_off) (loc))
8542 loc -= elt_size;
8543 loc += elt_size;
8544 if (loc != p)
8545 {
bca6d0e3
AM
8546 /* Chances are there is a run of relocs to insert here,
8547 from one of more input files. Files are not always
8548 linked in order due to the way elf_link_input_bfd is
8549 called. See pr17666. */
8550 size_t sortlen = p - loc;
8551 bfd_vma r_off2 = (*ext_r_off) (loc);
8552 size_t runlen = elt_size;
8553 size_t buf_size = 96 * 1024;
8554 while (p + runlen < end
8555 && (sortlen <= buf_size
8556 || runlen + elt_size <= buf_size)
8557 && r_off2 > (*ext_r_off) (p + runlen))
8558 runlen += elt_size;
8559 if (buf == NULL)
8560 {
8561 buf = bfd_malloc (buf_size);
8562 if (buf == NULL)
8563 return FALSE;
8564 }
8565 if (runlen < sortlen)
8566 {
8567 memcpy (buf, p, runlen);
8568 memmove (loc + runlen, loc, sortlen);
8569 memcpy (loc, buf, runlen);
8570 }
8571 else
8572 {
8573 memcpy (buf, loc, sortlen);
8574 memmove (loc, p, runlen);
8575 memcpy (loc + runlen, buf, sortlen);
8576 }
b29b8669 8577 p += runlen - elt_size;
0e287786
AM
8578 }
8579 }
8580 /* Hashes are no longer valid. */
28dbcedc
AM
8581 free (reldata->hashes);
8582 reldata->hashes = NULL;
bca6d0e3 8583 free (buf);
53df40a4 8584 }
bca6d0e3 8585 return TRUE;
c152c796
AM
8586}
8587
8588struct elf_link_sort_rela
8589{
8590 union {
8591 bfd_vma offset;
8592 bfd_vma sym_mask;
8593 } u;
8594 enum elf_reloc_type_class type;
8595 /* We use this as an array of size int_rels_per_ext_rel. */
8596 Elf_Internal_Rela rela[1];
8597};
8598
8599static int
8600elf_link_sort_cmp1 (const void *A, const void *B)
8601{
a50b1753
NC
8602 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8603 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8604 int relativea, relativeb;
8605
8606 relativea = a->type == reloc_class_relative;
8607 relativeb = b->type == reloc_class_relative;
8608
8609 if (relativea < relativeb)
8610 return 1;
8611 if (relativea > relativeb)
8612 return -1;
8613 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8614 return -1;
8615 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8616 return 1;
8617 if (a->rela->r_offset < b->rela->r_offset)
8618 return -1;
8619 if (a->rela->r_offset > b->rela->r_offset)
8620 return 1;
8621 return 0;
8622}
8623
8624static int
8625elf_link_sort_cmp2 (const void *A, const void *B)
8626{
a50b1753
NC
8627 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8628 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8629
7e612e98 8630 if (a->type < b->type)
c152c796 8631 return -1;
7e612e98 8632 if (a->type > b->type)
c152c796 8633 return 1;
7e612e98 8634 if (a->u.offset < b->u.offset)
c152c796 8635 return -1;
7e612e98 8636 if (a->u.offset > b->u.offset)
c152c796
AM
8637 return 1;
8638 if (a->rela->r_offset < b->rela->r_offset)
8639 return -1;
8640 if (a->rela->r_offset > b->rela->r_offset)
8641 return 1;
8642 return 0;
8643}
8644
8645static size_t
8646elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8647{
3410fea8 8648 asection *dynamic_relocs;
fc66a176
L
8649 asection *rela_dyn;
8650 asection *rel_dyn;
c152c796
AM
8651 bfd_size_type count, size;
8652 size_t i, ret, sort_elt, ext_size;
8653 bfd_byte *sort, *s_non_relative, *p;
8654 struct elf_link_sort_rela *sq;
8655 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8656 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8657 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8658 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8659 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8660 struct bfd_link_order *lo;
8661 bfd_vma r_sym_mask;
3410fea8 8662 bfd_boolean use_rela;
c152c796 8663
3410fea8
NC
8664 /* Find a dynamic reloc section. */
8665 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8666 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8667 if (rela_dyn != NULL && rela_dyn->size > 0
8668 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8669 {
3410fea8
NC
8670 bfd_boolean use_rela_initialised = FALSE;
8671
8672 /* This is just here to stop gcc from complaining.
c8e44c6d 8673 Its initialization checking code is not perfect. */
3410fea8
NC
8674 use_rela = TRUE;
8675
8676 /* Both sections are present. Examine the sizes
8677 of the indirect sections to help us choose. */
8678 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8679 if (lo->type == bfd_indirect_link_order)
8680 {
8681 asection *o = lo->u.indirect.section;
8682
8683 if ((o->size % bed->s->sizeof_rela) == 0)
8684 {
8685 if ((o->size % bed->s->sizeof_rel) == 0)
8686 /* Section size is divisible by both rel and rela sizes.
8687 It is of no help to us. */
8688 ;
8689 else
8690 {
8691 /* Section size is only divisible by rela. */
8692 if (use_rela_initialised && (use_rela == FALSE))
8693 {
c8e44c6d
AM
8694 _bfd_error_handler (_("%B: Unable to sort relocs - "
8695 "they are in more than one size"),
8696 abfd);
3410fea8
NC
8697 bfd_set_error (bfd_error_invalid_operation);
8698 return 0;
8699 }
8700 else
8701 {
8702 use_rela = TRUE;
8703 use_rela_initialised = TRUE;
8704 }
8705 }
8706 }
8707 else if ((o->size % bed->s->sizeof_rel) == 0)
8708 {
8709 /* Section size is only divisible by rel. */
8710 if (use_rela_initialised && (use_rela == TRUE))
8711 {
c8e44c6d
AM
8712 _bfd_error_handler (_("%B: Unable to sort relocs - "
8713 "they are in more than one size"),
8714 abfd);
3410fea8
NC
8715 bfd_set_error (bfd_error_invalid_operation);
8716 return 0;
8717 }
8718 else
8719 {
8720 use_rela = FALSE;
8721 use_rela_initialised = TRUE;
8722 }
8723 }
8724 else
8725 {
c8e44c6d
AM
8726 /* The section size is not divisible by either -
8727 something is wrong. */
8728 _bfd_error_handler (_("%B: Unable to sort relocs - "
8729 "they are of an unknown size"), abfd);
3410fea8
NC
8730 bfd_set_error (bfd_error_invalid_operation);
8731 return 0;
8732 }
8733 }
8734
8735 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8736 if (lo->type == bfd_indirect_link_order)
8737 {
8738 asection *o = lo->u.indirect.section;
8739
8740 if ((o->size % bed->s->sizeof_rela) == 0)
8741 {
8742 if ((o->size % bed->s->sizeof_rel) == 0)
8743 /* Section size is divisible by both rel and rela sizes.
8744 It is of no help to us. */
8745 ;
8746 else
8747 {
8748 /* Section size is only divisible by rela. */
8749 if (use_rela_initialised && (use_rela == FALSE))
8750 {
c8e44c6d
AM
8751 _bfd_error_handler (_("%B: Unable to sort relocs - "
8752 "they are in more than one size"),
8753 abfd);
3410fea8
NC
8754 bfd_set_error (bfd_error_invalid_operation);
8755 return 0;
8756 }
8757 else
8758 {
8759 use_rela = TRUE;
8760 use_rela_initialised = TRUE;
8761 }
8762 }
8763 }
8764 else if ((o->size % bed->s->sizeof_rel) == 0)
8765 {
8766 /* Section size is only divisible by rel. */
8767 if (use_rela_initialised && (use_rela == TRUE))
8768 {
c8e44c6d
AM
8769 _bfd_error_handler (_("%B: Unable to sort relocs - "
8770 "they are in more than one size"),
8771 abfd);
3410fea8
NC
8772 bfd_set_error (bfd_error_invalid_operation);
8773 return 0;
8774 }
8775 else
8776 {
8777 use_rela = FALSE;
8778 use_rela_initialised = TRUE;
8779 }
8780 }
8781 else
8782 {
c8e44c6d
AM
8783 /* The section size is not divisible by either -
8784 something is wrong. */
8785 _bfd_error_handler (_("%B: Unable to sort relocs - "
8786 "they are of an unknown size"), abfd);
3410fea8
NC
8787 bfd_set_error (bfd_error_invalid_operation);
8788 return 0;
8789 }
8790 }
8791
8792 if (! use_rela_initialised)
8793 /* Make a guess. */
8794 use_rela = TRUE;
c152c796 8795 }
fc66a176
L
8796 else if (rela_dyn != NULL && rela_dyn->size > 0)
8797 use_rela = TRUE;
8798 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8799 use_rela = FALSE;
c152c796 8800 else
fc66a176 8801 return 0;
3410fea8
NC
8802
8803 if (use_rela)
c152c796 8804 {
3410fea8 8805 dynamic_relocs = rela_dyn;
c152c796
AM
8806 ext_size = bed->s->sizeof_rela;
8807 swap_in = bed->s->swap_reloca_in;
8808 swap_out = bed->s->swap_reloca_out;
8809 }
3410fea8
NC
8810 else
8811 {
8812 dynamic_relocs = rel_dyn;
8813 ext_size = bed->s->sizeof_rel;
8814 swap_in = bed->s->swap_reloc_in;
8815 swap_out = bed->s->swap_reloc_out;
8816 }
c152c796
AM
8817
8818 size = 0;
3410fea8 8819 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8820 if (lo->type == bfd_indirect_link_order)
3410fea8 8821 size += lo->u.indirect.section->size;
c152c796 8822
3410fea8 8823 if (size != dynamic_relocs->size)
c152c796
AM
8824 return 0;
8825
8826 sort_elt = (sizeof (struct elf_link_sort_rela)
8827 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8828
8829 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8830 if (count == 0)
8831 return 0;
a50b1753 8832 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8833
c152c796
AM
8834 if (sort == NULL)
8835 {
8836 (*info->callbacks->warning)
8837 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8838 return 0;
8839 }
8840
8841 if (bed->s->arch_size == 32)
8842 r_sym_mask = ~(bfd_vma) 0xff;
8843 else
8844 r_sym_mask = ~(bfd_vma) 0xffffffff;
8845
3410fea8 8846 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8847 if (lo->type == bfd_indirect_link_order)
8848 {
8849 bfd_byte *erel, *erelend;
8850 asection *o = lo->u.indirect.section;
8851
1da212d6
AM
8852 if (o->contents == NULL && o->size != 0)
8853 {
8854 /* This is a reloc section that is being handled as a normal
8855 section. See bfd_section_from_shdr. We can't combine
8856 relocs in this case. */
8857 free (sort);
8858 return 0;
8859 }
c152c796 8860 erel = o->contents;
eea6121a 8861 erelend = o->contents + o->size;
c8e44c6d 8862 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8863
c152c796
AM
8864 while (erel < erelend)
8865 {
8866 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8867
c152c796 8868 (*swap_in) (abfd, erel, s->rela);
7e612e98 8869 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8870 s->u.sym_mask = r_sym_mask;
8871 p += sort_elt;
8872 erel += ext_size;
8873 }
8874 }
8875
8876 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8877
8878 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8879 {
8880 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8881 if (s->type != reloc_class_relative)
8882 break;
8883 }
8884 ret = i;
8885 s_non_relative = p;
8886
8887 sq = (struct elf_link_sort_rela *) s_non_relative;
8888 for (; i < count; i++, p += sort_elt)
8889 {
8890 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8891 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8892 sq = sp;
8893 sp->u.offset = sq->rela->r_offset;
8894 }
8895
8896 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8897
c8e44c6d
AM
8898 struct elf_link_hash_table *htab = elf_hash_table (info);
8899 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8900 {
8901 /* We have plt relocs in .rela.dyn. */
8902 sq = (struct elf_link_sort_rela *) sort;
8903 for (i = 0; i < count; i++)
8904 if (sq[count - i - 1].type != reloc_class_plt)
8905 break;
8906 if (i != 0 && htab->srelplt->size == i * ext_size)
8907 {
8908 struct bfd_link_order **plo;
8909 /* Put srelplt link_order last. This is so the output_offset
8910 set in the next loop is correct for DT_JMPREL. */
8911 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8912 if ((*plo)->type == bfd_indirect_link_order
8913 && (*plo)->u.indirect.section == htab->srelplt)
8914 {
8915 lo = *plo;
8916 *plo = lo->next;
8917 }
8918 else
8919 plo = &(*plo)->next;
8920 *plo = lo;
8921 lo->next = NULL;
8922 dynamic_relocs->map_tail.link_order = lo;
8923 }
8924 }
8925
8926 p = sort;
3410fea8 8927 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8928 if (lo->type == bfd_indirect_link_order)
8929 {
8930 bfd_byte *erel, *erelend;
8931 asection *o = lo->u.indirect.section;
8932
8933 erel = o->contents;
eea6121a 8934 erelend = o->contents + o->size;
c8e44c6d 8935 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8936 while (erel < erelend)
8937 {
8938 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8939 (*swap_out) (abfd, s->rela, erel);
8940 p += sort_elt;
8941 erel += ext_size;
8942 }
8943 }
8944
8945 free (sort);
3410fea8 8946 *psec = dynamic_relocs;
c152c796
AM
8947 return ret;
8948}
8949
ef10c3ac 8950/* Add a symbol to the output symbol string table. */
c152c796 8951
6e0b88f1 8952static int
ef10c3ac
L
8953elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8954 const char *name,
8955 Elf_Internal_Sym *elfsym,
8956 asection *input_sec,
8957 struct elf_link_hash_entry *h)
c152c796 8958{
6e0b88f1 8959 int (*output_symbol_hook)
c152c796
AM
8960 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8961 struct elf_link_hash_entry *);
ef10c3ac 8962 struct elf_link_hash_table *hash_table;
c152c796 8963 const struct elf_backend_data *bed;
ef10c3ac 8964 bfd_size_type strtabsize;
c152c796 8965
8539e4e8
AM
8966 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8967
8b127cbc 8968 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8969 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8970 if (output_symbol_hook != NULL)
8971 {
8b127cbc 8972 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8973 if (ret != 1)
8974 return ret;
c152c796
AM
8975 }
8976
ef10c3ac
L
8977 if (name == NULL
8978 || *name == '\0'
8979 || (input_sec->flags & SEC_EXCLUDE))
8980 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8981 else
8982 {
ef10c3ac
L
8983 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8984 to get the final offset for st_name. */
8985 elfsym->st_name
8986 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8987 name, FALSE);
c152c796 8988 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8989 return 0;
c152c796
AM
8990 }
8991
ef10c3ac
L
8992 hash_table = elf_hash_table (flinfo->info);
8993 strtabsize = hash_table->strtabsize;
8994 if (strtabsize <= hash_table->strtabcount)
c152c796 8995 {
ef10c3ac
L
8996 strtabsize += strtabsize;
8997 hash_table->strtabsize = strtabsize;
8998 strtabsize *= sizeof (*hash_table->strtab);
8999 hash_table->strtab
9000 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9001 strtabsize);
9002 if (hash_table->strtab == NULL)
6e0b88f1 9003 return 0;
c152c796 9004 }
ef10c3ac
L
9005 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9006 hash_table->strtab[hash_table->strtabcount].dest_index
9007 = hash_table->strtabcount;
9008 hash_table->strtab[hash_table->strtabcount].destshndx_index
9009 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9010
9011 bfd_get_symcount (flinfo->output_bfd) += 1;
9012 hash_table->strtabcount += 1;
9013
9014 return 1;
9015}
9016
9017/* Swap symbols out to the symbol table and flush the output symbols to
9018 the file. */
9019
9020static bfd_boolean
9021elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9022{
9023 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9024 bfd_size_type amt;
9025 size_t i;
ef10c3ac
L
9026 const struct elf_backend_data *bed;
9027 bfd_byte *symbuf;
9028 Elf_Internal_Shdr *hdr;
9029 file_ptr pos;
9030 bfd_boolean ret;
9031
9032 if (!hash_table->strtabcount)
9033 return TRUE;
9034
9035 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9036
9037 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9038
ef10c3ac
L
9039 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9040 symbuf = (bfd_byte *) bfd_malloc (amt);
9041 if (symbuf == NULL)
9042 return FALSE;
1b786873 9043
ef10c3ac 9044 if (flinfo->symshndxbuf)
c152c796 9045 {
ef53be89
AM
9046 amt = sizeof (Elf_External_Sym_Shndx);
9047 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9048 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9049 if (flinfo->symshndxbuf == NULL)
c152c796 9050 {
ef10c3ac
L
9051 free (symbuf);
9052 return FALSE;
c152c796 9053 }
c152c796
AM
9054 }
9055
ef10c3ac
L
9056 for (i = 0; i < hash_table->strtabcount; i++)
9057 {
9058 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9059 if (elfsym->sym.st_name == (unsigned long) -1)
9060 elfsym->sym.st_name = 0;
9061 else
9062 elfsym->sym.st_name
9063 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9064 elfsym->sym.st_name);
9065 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9066 ((bfd_byte *) symbuf
9067 + (elfsym->dest_index
9068 * bed->s->sizeof_sym)),
9069 (flinfo->symshndxbuf
9070 + elfsym->destshndx_index));
9071 }
9072
9073 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9074 pos = hdr->sh_offset + hdr->sh_size;
9075 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9076 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9077 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9078 {
9079 hdr->sh_size += amt;
9080 ret = TRUE;
9081 }
9082 else
9083 ret = FALSE;
c152c796 9084
ef10c3ac
L
9085 free (symbuf);
9086
9087 free (hash_table->strtab);
9088 hash_table->strtab = NULL;
9089
9090 return ret;
c152c796
AM
9091}
9092
c0d5a53d
L
9093/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9094
9095static bfd_boolean
9096check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9097{
4fbb74a6
AM
9098 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9099 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9100 {
9101 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9102 beyond 64k. */
4eca0228 9103 _bfd_error_handler
695344c0 9104 /* xgettext:c-format */
c0d5a53d 9105 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9106 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9107 bfd_set_error (bfd_error_nonrepresentable_section);
9108 return FALSE;
9109 }
9110 return TRUE;
9111}
9112
c152c796
AM
9113/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9114 allowing an unsatisfied unversioned symbol in the DSO to match a
9115 versioned symbol that would normally require an explicit version.
9116 We also handle the case that a DSO references a hidden symbol
9117 which may be satisfied by a versioned symbol in another DSO. */
9118
9119static bfd_boolean
9120elf_link_check_versioned_symbol (struct bfd_link_info *info,
9121 const struct elf_backend_data *bed,
9122 struct elf_link_hash_entry *h)
9123{
9124 bfd *abfd;
9125 struct elf_link_loaded_list *loaded;
9126
9127 if (!is_elf_hash_table (info->hash))
9128 return FALSE;
9129
90c984fc
L
9130 /* Check indirect symbol. */
9131 while (h->root.type == bfd_link_hash_indirect)
9132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9133
c152c796
AM
9134 switch (h->root.type)
9135 {
9136 default:
9137 abfd = NULL;
9138 break;
9139
9140 case bfd_link_hash_undefined:
9141 case bfd_link_hash_undefweak:
9142 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9143 if (abfd == NULL
9144 || (abfd->flags & DYNAMIC) == 0
e56f61be 9145 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9146 return FALSE;
9147 break;
9148
9149 case bfd_link_hash_defined:
9150 case bfd_link_hash_defweak:
9151 abfd = h->root.u.def.section->owner;
9152 break;
9153
9154 case bfd_link_hash_common:
9155 abfd = h->root.u.c.p->section->owner;
9156 break;
9157 }
9158 BFD_ASSERT (abfd != NULL);
9159
9160 for (loaded = elf_hash_table (info)->loaded;
9161 loaded != NULL;
9162 loaded = loaded->next)
9163 {
9164 bfd *input;
9165 Elf_Internal_Shdr *hdr;
ef53be89
AM
9166 size_t symcount;
9167 size_t extsymcount;
9168 size_t extsymoff;
c152c796
AM
9169 Elf_Internal_Shdr *versymhdr;
9170 Elf_Internal_Sym *isym;
9171 Elf_Internal_Sym *isymend;
9172 Elf_Internal_Sym *isymbuf;
9173 Elf_External_Versym *ever;
9174 Elf_External_Versym *extversym;
9175
9176 input = loaded->abfd;
9177
9178 /* We check each DSO for a possible hidden versioned definition. */
9179 if (input == abfd
9180 || (input->flags & DYNAMIC) == 0
9181 || elf_dynversym (input) == 0)
9182 continue;
9183
9184 hdr = &elf_tdata (input)->dynsymtab_hdr;
9185
9186 symcount = hdr->sh_size / bed->s->sizeof_sym;
9187 if (elf_bad_symtab (input))
9188 {
9189 extsymcount = symcount;
9190 extsymoff = 0;
9191 }
9192 else
9193 {
9194 extsymcount = symcount - hdr->sh_info;
9195 extsymoff = hdr->sh_info;
9196 }
9197
9198 if (extsymcount == 0)
9199 continue;
9200
9201 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9202 NULL, NULL, NULL);
9203 if (isymbuf == NULL)
9204 return FALSE;
9205
9206 /* Read in any version definitions. */
9207 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9208 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9209 if (extversym == NULL)
9210 goto error_ret;
9211
9212 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9213 || (bfd_bread (extversym, versymhdr->sh_size, input)
9214 != versymhdr->sh_size))
9215 {
9216 free (extversym);
9217 error_ret:
9218 free (isymbuf);
9219 return FALSE;
9220 }
9221
9222 ever = extversym + extsymoff;
9223 isymend = isymbuf + extsymcount;
9224 for (isym = isymbuf; isym < isymend; isym++, ever++)
9225 {
9226 const char *name;
9227 Elf_Internal_Versym iver;
9228 unsigned short version_index;
9229
9230 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9231 || isym->st_shndx == SHN_UNDEF)
9232 continue;
9233
9234 name = bfd_elf_string_from_elf_section (input,
9235 hdr->sh_link,
9236 isym->st_name);
9237 if (strcmp (name, h->root.root.string) != 0)
9238 continue;
9239
9240 _bfd_elf_swap_versym_in (input, ever, &iver);
9241
d023c380
L
9242 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9243 && !(h->def_regular
9244 && h->forced_local))
c152c796
AM
9245 {
9246 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9247 have provided a definition for the undefined sym unless
9248 it is defined in a non-shared object and forced local.
9249 */
c152c796
AM
9250 abort ();
9251 }
9252
9253 version_index = iver.vs_vers & VERSYM_VERSION;
9254 if (version_index == 1 || version_index == 2)
9255 {
9256 /* This is the base or first version. We can use it. */
9257 free (extversym);
9258 free (isymbuf);
9259 return TRUE;
9260 }
9261 }
9262
9263 free (extversym);
9264 free (isymbuf);
9265 }
9266
9267 return FALSE;
9268}
9269
b8871f35
L
9270/* Convert ELF common symbol TYPE. */
9271
9272static int
9273elf_link_convert_common_type (struct bfd_link_info *info, int type)
9274{
9275 /* Commom symbol can only appear in relocatable link. */
9276 if (!bfd_link_relocatable (info))
9277 abort ();
9278 switch (info->elf_stt_common)
9279 {
9280 case unchanged:
9281 break;
9282 case elf_stt_common:
9283 type = STT_COMMON;
9284 break;
9285 case no_elf_stt_common:
9286 type = STT_OBJECT;
9287 break;
9288 }
9289 return type;
9290}
9291
c152c796
AM
9292/* Add an external symbol to the symbol table. This is called from
9293 the hash table traversal routine. When generating a shared object,
9294 we go through the symbol table twice. The first time we output
9295 anything that might have been forced to local scope in a version
9296 script. The second time we output the symbols that are still
9297 global symbols. */
9298
9299static bfd_boolean
7686d77d 9300elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9301{
7686d77d 9302 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9303 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9304 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9305 bfd_boolean strip;
9306 Elf_Internal_Sym sym;
9307 asection *input_sec;
9308 const struct elf_backend_data *bed;
6e0b88f1
AM
9309 long indx;
9310 int ret;
b8871f35 9311 unsigned int type;
c152c796
AM
9312
9313 if (h->root.type == bfd_link_hash_warning)
9314 {
9315 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9316 if (h->root.type == bfd_link_hash_new)
9317 return TRUE;
9318 }
9319
9320 /* Decide whether to output this symbol in this pass. */
9321 if (eoinfo->localsyms)
9322 {
4deb8f71 9323 if (!h->forced_local)
c152c796
AM
9324 return TRUE;
9325 }
9326 else
9327 {
4deb8f71 9328 if (h->forced_local)
c152c796
AM
9329 return TRUE;
9330 }
9331
8b127cbc 9332 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9333
12ac1cf5 9334 if (h->root.type == bfd_link_hash_undefined)
c152c796 9335 {
12ac1cf5
NC
9336 /* If we have an undefined symbol reference here then it must have
9337 come from a shared library that is being linked in. (Undefined
98da7939
L
9338 references in regular files have already been handled unless
9339 they are in unreferenced sections which are removed by garbage
9340 collection). */
12ac1cf5
NC
9341 bfd_boolean ignore_undef = FALSE;
9342
9343 /* Some symbols may be special in that the fact that they're
9344 undefined can be safely ignored - let backend determine that. */
9345 if (bed->elf_backend_ignore_undef_symbol)
9346 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9347
9348 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9349 if (!ignore_undef
12ac1cf5 9350 && h->ref_dynamic
8b127cbc
AM
9351 && (!h->ref_regular || flinfo->info->gc_sections)
9352 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9353 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9354 (*flinfo->info->callbacks->undefined_symbol)
9355 (flinfo->info, h->root.root.string,
9356 h->ref_regular ? NULL : h->root.u.undef.abfd,
9357 NULL, 0,
9358 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9359
9360 /* Strip a global symbol defined in a discarded section. */
9361 if (h->indx == -3)
9362 return TRUE;
c152c796
AM
9363 }
9364
9365 /* We should also warn if a forced local symbol is referenced from
9366 shared libraries. */
0e1862bb 9367 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9368 && h->forced_local
9369 && h->ref_dynamic
371a5866 9370 && h->def_regular
f5385ebf 9371 && !h->dynamic_def
ee659f1f 9372 && h->ref_dynamic_nonweak
8b127cbc 9373 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9374 {
17d078c5
AM
9375 bfd *def_bfd;
9376 const char *msg;
90c984fc
L
9377 struct elf_link_hash_entry *hi = h;
9378
9379 /* Check indirect symbol. */
9380 while (hi->root.type == bfd_link_hash_indirect)
9381 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9382
9383 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9384 /* xgettext:c-format */
17d078c5
AM
9385 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9386 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9387 /* xgettext:c-format */
17d078c5
AM
9388 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9389 else
695344c0 9390 /* xgettext:c-format */
17d078c5 9391 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9392 def_bfd = flinfo->output_bfd;
90c984fc
L
9393 if (hi->root.u.def.section != bfd_abs_section_ptr)
9394 def_bfd = hi->root.u.def.section->owner;
4eca0228
AM
9395 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9396 h->root.root.string);
17d078c5 9397 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9398 eoinfo->failed = TRUE;
9399 return FALSE;
9400 }
9401
9402 /* We don't want to output symbols that have never been mentioned by
9403 a regular file, or that we have been told to strip. However, if
9404 h->indx is set to -2, the symbol is used by a reloc and we must
9405 output it. */
d983c8c5 9406 strip = FALSE;
c152c796 9407 if (h->indx == -2)
d983c8c5 9408 ;
f5385ebf 9409 else if ((h->def_dynamic
77cfaee6
AM
9410 || h->ref_dynamic
9411 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9412 && !h->def_regular
9413 && !h->ref_regular)
c152c796 9414 strip = TRUE;
8b127cbc 9415 else if (flinfo->info->strip == strip_all)
c152c796 9416 strip = TRUE;
8b127cbc
AM
9417 else if (flinfo->info->strip == strip_some
9418 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9419 h->root.root.string, FALSE, FALSE) == NULL)
9420 strip = TRUE;
d56d55e7
AM
9421 else if ((h->root.type == bfd_link_hash_defined
9422 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9423 && ((flinfo->info->strip_discarded
dbaa2011 9424 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9425 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9426 && h->root.u.def.section->owner != NULL
d56d55e7 9427 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9428 strip = TRUE;
9e2278f5
AM
9429 else if ((h->root.type == bfd_link_hash_undefined
9430 || h->root.type == bfd_link_hash_undefweak)
9431 && h->root.u.undef.abfd != NULL
9432 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9433 strip = TRUE;
c152c796 9434
b8871f35
L
9435 type = h->type;
9436
c152c796 9437 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9438 nothing else to do. However, if it is a forced local symbol or
9439 an ifunc symbol we need to give the backend finish_dynamic_symbol
9440 function a chance to make it dynamic. */
c152c796
AM
9441 if (strip
9442 && h->dynindx == -1
b8871f35 9443 && type != STT_GNU_IFUNC
f5385ebf 9444 && !h->forced_local)
c152c796
AM
9445 return TRUE;
9446
9447 sym.st_value = 0;
9448 sym.st_size = h->size;
9449 sym.st_other = h->other;
c152c796
AM
9450 switch (h->root.type)
9451 {
9452 default:
9453 case bfd_link_hash_new:
9454 case bfd_link_hash_warning:
9455 abort ();
9456 return FALSE;
9457
9458 case bfd_link_hash_undefined:
9459 case bfd_link_hash_undefweak:
9460 input_sec = bfd_und_section_ptr;
9461 sym.st_shndx = SHN_UNDEF;
9462 break;
9463
9464 case bfd_link_hash_defined:
9465 case bfd_link_hash_defweak:
9466 {
9467 input_sec = h->root.u.def.section;
9468 if (input_sec->output_section != NULL)
9469 {
9470 sym.st_shndx =
8b127cbc 9471 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9472 input_sec->output_section);
9473 if (sym.st_shndx == SHN_BAD)
9474 {
4eca0228 9475 _bfd_error_handler
695344c0 9476 /* xgettext:c-format */
d003868e 9477 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9478 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9479 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9480 eoinfo->failed = TRUE;
9481 return FALSE;
9482 }
9483
9484 /* ELF symbols in relocatable files are section relative,
9485 but in nonrelocatable files they are virtual
9486 addresses. */
9487 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9488 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9489 {
9490 sym.st_value += input_sec->output_section->vma;
9491 if (h->type == STT_TLS)
9492 {
8b127cbc 9493 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9494 if (tls_sec != NULL)
9495 sym.st_value -= tls_sec->vma;
c152c796
AM
9496 }
9497 }
9498 }
9499 else
9500 {
9501 BFD_ASSERT (input_sec->owner == NULL
9502 || (input_sec->owner->flags & DYNAMIC) != 0);
9503 sym.st_shndx = SHN_UNDEF;
9504 input_sec = bfd_und_section_ptr;
9505 }
9506 }
9507 break;
9508
9509 case bfd_link_hash_common:
9510 input_sec = h->root.u.c.p->section;
a4d8e49b 9511 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9512 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9513 break;
9514
9515 case bfd_link_hash_indirect:
9516 /* These symbols are created by symbol versioning. They point
9517 to the decorated version of the name. For example, if the
9518 symbol foo@@GNU_1.2 is the default, which should be used when
9519 foo is used with no version, then we add an indirect symbol
9520 foo which points to foo@@GNU_1.2. We ignore these symbols,
9521 since the indirected symbol is already in the hash table. */
9522 return TRUE;
9523 }
9524
b8871f35
L
9525 if (type == STT_COMMON || type == STT_OBJECT)
9526 switch (h->root.type)
9527 {
9528 case bfd_link_hash_common:
9529 type = elf_link_convert_common_type (flinfo->info, type);
9530 break;
9531 case bfd_link_hash_defined:
9532 case bfd_link_hash_defweak:
9533 if (bed->common_definition (&sym))
9534 type = elf_link_convert_common_type (flinfo->info, type);
9535 else
9536 type = STT_OBJECT;
9537 break;
9538 case bfd_link_hash_undefined:
9539 case bfd_link_hash_undefweak:
9540 break;
9541 default:
9542 abort ();
9543 }
9544
4deb8f71 9545 if (h->forced_local)
b8871f35
L
9546 {
9547 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9548 /* Turn off visibility on local symbol. */
9549 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9550 }
9551 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9552 else if (h->unique_global && h->def_regular)
9553 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9554 else if (h->root.type == bfd_link_hash_undefweak
9555 || h->root.type == bfd_link_hash_defweak)
9556 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9557 else
9558 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9559 sym.st_target_internal = h->target_internal;
9560
c152c796
AM
9561 /* Give the processor backend a chance to tweak the symbol value,
9562 and also to finish up anything that needs to be done for this
9563 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9564 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9565 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9566 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9567 && h->def_regular
0e1862bb 9568 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9569 || ((h->dynindx != -1
9570 || h->forced_local)
0e1862bb 9571 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9572 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9573 || h->root.type != bfd_link_hash_undefweak))
9574 || !h->forced_local)
8b127cbc 9575 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9576 {
9577 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9578 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9579 {
9580 eoinfo->failed = TRUE;
9581 return FALSE;
9582 }
9583 }
9584
9585 /* If we are marking the symbol as undefined, and there are no
9586 non-weak references to this symbol from a regular object, then
9587 mark the symbol as weak undefined; if there are non-weak
9588 references, mark the symbol as strong. We can't do this earlier,
9589 because it might not be marked as undefined until the
9590 finish_dynamic_symbol routine gets through with it. */
9591 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9592 && h->ref_regular
c152c796
AM
9593 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9594 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9595 {
9596 int bindtype;
b8871f35 9597 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9598
9599 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9600 if (type == STT_GNU_IFUNC)
9601 type = STT_FUNC;
c152c796 9602
f5385ebf 9603 if (h->ref_regular_nonweak)
c152c796
AM
9604 bindtype = STB_GLOBAL;
9605 else
9606 bindtype = STB_WEAK;
2955ec4c 9607 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9608 }
9609
bda987c2
CD
9610 /* If this is a symbol defined in a dynamic library, don't use the
9611 symbol size from the dynamic library. Relinking an executable
9612 against a new library may introduce gratuitous changes in the
9613 executable's symbols if we keep the size. */
9614 if (sym.st_shndx == SHN_UNDEF
9615 && !h->def_regular
9616 && h->def_dynamic)
9617 sym.st_size = 0;
9618
c152c796
AM
9619 /* If a non-weak symbol with non-default visibility is not defined
9620 locally, it is a fatal error. */
0e1862bb 9621 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9622 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9623 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9624 && h->root.type == bfd_link_hash_undefined
f5385ebf 9625 && !h->def_regular)
c152c796 9626 {
17d078c5
AM
9627 const char *msg;
9628
9629 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9630 /* xgettext:c-format */
17d078c5
AM
9631 msg = _("%B: protected symbol `%s' isn't defined");
9632 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9633 /* xgettext:c-format */
17d078c5
AM
9634 msg = _("%B: internal symbol `%s' isn't defined");
9635 else
695344c0 9636 /* xgettext:c-format */
17d078c5 9637 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9638 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9639 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9640 eoinfo->failed = TRUE;
9641 return FALSE;
9642 }
9643
9644 /* If this symbol should be put in the .dynsym section, then put it
9645 there now. We already know the symbol index. We also fill in
9646 the entry in the .hash section. */
cae1fbbb 9647 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9648 && h->dynindx != -1
8b127cbc 9649 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9650 {
c152c796
AM
9651 bfd_byte *esym;
9652
90c984fc
L
9653 /* Since there is no version information in the dynamic string,
9654 if there is no version info in symbol version section, we will
1659f720 9655 have a run-time problem if not linking executable, referenced
4deb8f71 9656 by shared library, or not bound locally. */
1659f720 9657 if (h->verinfo.verdef == NULL
0e1862bb 9658 && (!bfd_link_executable (flinfo->info)
1659f720
L
9659 || h->ref_dynamic
9660 || !h->def_regular))
90c984fc
L
9661 {
9662 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9663
9664 if (p && p [1] != '\0')
9665 {
4eca0228 9666 _bfd_error_handler
695344c0 9667 /* xgettext:c-format */
90c984fc
L
9668 (_("%B: No symbol version section for versioned symbol `%s'"),
9669 flinfo->output_bfd, h->root.root.string);
9670 eoinfo->failed = TRUE;
9671 return FALSE;
9672 }
9673 }
9674
c152c796 9675 sym.st_name = h->dynstr_index;
cae1fbbb
L
9676 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9677 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9678 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9679 {
9680 eoinfo->failed = TRUE;
9681 return FALSE;
9682 }
8b127cbc 9683 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9684
8b127cbc 9685 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9686 {
9687 size_t hash_entry_size;
9688 bfd_byte *bucketpos;
9689 bfd_vma chain;
41198d0c
L
9690 size_t bucketcount;
9691 size_t bucket;
9692
8b127cbc 9693 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9694 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9695
9696 hash_entry_size
8b127cbc
AM
9697 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9698 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9699 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9700 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9701 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9702 bucketpos);
9703 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9704 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9705 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9706 }
c152c796 9707
8b127cbc 9708 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9709 {
9710 Elf_Internal_Versym iversym;
9711 Elf_External_Versym *eversym;
9712
f5385ebf 9713 if (!h->def_regular)
c152c796 9714 {
7b20f099
AM
9715 if (h->verinfo.verdef == NULL
9716 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9717 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9718 iversym.vs_vers = 0;
9719 else
9720 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9721 }
9722 else
9723 {
9724 if (h->verinfo.vertree == NULL)
9725 iversym.vs_vers = 1;
9726 else
9727 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9728 if (flinfo->info->create_default_symver)
3e3b46e5 9729 iversym.vs_vers++;
c152c796
AM
9730 }
9731
422f1182 9732 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9733 defined locally. */
422f1182 9734 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9735 iversym.vs_vers |= VERSYM_HIDDEN;
9736
8b127cbc 9737 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9738 eversym += h->dynindx;
8b127cbc 9739 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9740 }
9741 }
9742
d983c8c5
AM
9743 /* If the symbol is undefined, and we didn't output it to .dynsym,
9744 strip it from .symtab too. Obviously we can't do this for
9745 relocatable output or when needed for --emit-relocs. */
9746 else if (input_sec == bfd_und_section_ptr
9747 && h->indx != -2
0e1862bb 9748 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9749 return TRUE;
9750 /* Also strip others that we couldn't earlier due to dynamic symbol
9751 processing. */
9752 if (strip)
9753 return TRUE;
9754 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9755 return TRUE;
9756
2ec55de3
AM
9757 /* Output a FILE symbol so that following locals are not associated
9758 with the wrong input file. We need one for forced local symbols
9759 if we've seen more than one FILE symbol or when we have exactly
9760 one FILE symbol but global symbols are present in a file other
9761 than the one with the FILE symbol. We also need one if linker
9762 defined symbols are present. In practice these conditions are
9763 always met, so just emit the FILE symbol unconditionally. */
9764 if (eoinfo->localsyms
9765 && !eoinfo->file_sym_done
9766 && eoinfo->flinfo->filesym_count != 0)
9767 {
9768 Elf_Internal_Sym fsym;
9769
9770 memset (&fsym, 0, sizeof (fsym));
9771 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9772 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9773 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9774 bfd_und_section_ptr, NULL))
2ec55de3
AM
9775 return FALSE;
9776
9777 eoinfo->file_sym_done = TRUE;
9778 }
9779
8b127cbc 9780 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9781 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9782 input_sec, h);
6e0b88f1 9783 if (ret == 0)
c152c796
AM
9784 {
9785 eoinfo->failed = TRUE;
9786 return FALSE;
9787 }
6e0b88f1
AM
9788 else if (ret == 1)
9789 h->indx = indx;
9790 else if (h->indx == -2)
9791 abort();
c152c796
AM
9792
9793 return TRUE;
9794}
9795
cdd3575c
AM
9796/* Return TRUE if special handling is done for relocs in SEC against
9797 symbols defined in discarded sections. */
9798
c152c796
AM
9799static bfd_boolean
9800elf_section_ignore_discarded_relocs (asection *sec)
9801{
9802 const struct elf_backend_data *bed;
9803
cdd3575c
AM
9804 switch (sec->sec_info_type)
9805 {
dbaa2011
AM
9806 case SEC_INFO_TYPE_STABS:
9807 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9808 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9809 return TRUE;
9810 default:
9811 break;
9812 }
c152c796
AM
9813
9814 bed = get_elf_backend_data (sec->owner);
9815 if (bed->elf_backend_ignore_discarded_relocs != NULL
9816 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9817 return TRUE;
9818
9819 return FALSE;
9820}
9821
9e66c942
AM
9822/* Return a mask saying how ld should treat relocations in SEC against
9823 symbols defined in discarded sections. If this function returns
9824 COMPLAIN set, ld will issue a warning message. If this function
9825 returns PRETEND set, and the discarded section was link-once and the
9826 same size as the kept link-once section, ld will pretend that the
9827 symbol was actually defined in the kept section. Otherwise ld will
9828 zero the reloc (at least that is the intent, but some cooperation by
9829 the target dependent code is needed, particularly for REL targets). */
9830
8a696751
AM
9831unsigned int
9832_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9833{
9e66c942 9834 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9835 return PRETEND;
cdd3575c
AM
9836
9837 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9838 return 0;
cdd3575c
AM
9839
9840 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9841 return 0;
cdd3575c 9842
9e66c942 9843 return COMPLAIN | PRETEND;
cdd3575c
AM
9844}
9845
3d7f7666
L
9846/* Find a match between a section and a member of a section group. */
9847
9848static asection *
c0f00686
L
9849match_group_member (asection *sec, asection *group,
9850 struct bfd_link_info *info)
3d7f7666
L
9851{
9852 asection *first = elf_next_in_group (group);
9853 asection *s = first;
9854
9855 while (s != NULL)
9856 {
c0f00686 9857 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9858 return s;
9859
83180ade 9860 s = elf_next_in_group (s);
3d7f7666
L
9861 if (s == first)
9862 break;
9863 }
9864
9865 return NULL;
9866}
9867
01b3c8ab 9868/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9869 to replace it. Return the replacement if it is OK. Otherwise return
9870 NULL. */
01b3c8ab
L
9871
9872asection *
c0f00686 9873_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9874{
9875 asection *kept;
9876
9877 kept = sec->kept_section;
9878 if (kept != NULL)
9879 {
c2370991 9880 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9881 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9882 if (kept != NULL
9883 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9884 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9885 kept = NULL;
c2370991 9886 sec->kept_section = kept;
01b3c8ab
L
9887 }
9888 return kept;
9889}
9890
c152c796
AM
9891/* Link an input file into the linker output file. This function
9892 handles all the sections and relocations of the input file at once.
9893 This is so that we only have to read the local symbols once, and
9894 don't have to keep them in memory. */
9895
9896static bfd_boolean
8b127cbc 9897elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9898{
ece5ef60 9899 int (*relocate_section)
c152c796
AM
9900 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9901 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9902 bfd *output_bfd;
9903 Elf_Internal_Shdr *symtab_hdr;
9904 size_t locsymcount;
9905 size_t extsymoff;
9906 Elf_Internal_Sym *isymbuf;
9907 Elf_Internal_Sym *isym;
9908 Elf_Internal_Sym *isymend;
9909 long *pindex;
9910 asection **ppsection;
9911 asection *o;
9912 const struct elf_backend_data *bed;
c152c796 9913 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9914 bfd_size_type address_size;
9915 bfd_vma r_type_mask;
9916 int r_sym_shift;
ffbc01cc 9917 bfd_boolean have_file_sym = FALSE;
c152c796 9918
8b127cbc 9919 output_bfd = flinfo->output_bfd;
c152c796
AM
9920 bed = get_elf_backend_data (output_bfd);
9921 relocate_section = bed->elf_backend_relocate_section;
9922
9923 /* If this is a dynamic object, we don't want to do anything here:
9924 we don't want the local symbols, and we don't want the section
9925 contents. */
9926 if ((input_bfd->flags & DYNAMIC) != 0)
9927 return TRUE;
9928
c152c796
AM
9929 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9930 if (elf_bad_symtab (input_bfd))
9931 {
9932 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9933 extsymoff = 0;
9934 }
9935 else
9936 {
9937 locsymcount = symtab_hdr->sh_info;
9938 extsymoff = symtab_hdr->sh_info;
9939 }
9940
9941 /* Read the local symbols. */
9942 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9943 if (isymbuf == NULL && locsymcount != 0)
9944 {
9945 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9946 flinfo->internal_syms,
9947 flinfo->external_syms,
9948 flinfo->locsym_shndx);
c152c796
AM
9949 if (isymbuf == NULL)
9950 return FALSE;
9951 }
9952
9953 /* Find local symbol sections and adjust values of symbols in
9954 SEC_MERGE sections. Write out those local symbols we know are
9955 going into the output file. */
9956 isymend = isymbuf + locsymcount;
8b127cbc 9957 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9958 isym < isymend;
9959 isym++, pindex++, ppsection++)
9960 {
9961 asection *isec;
9962 const char *name;
9963 Elf_Internal_Sym osym;
6e0b88f1
AM
9964 long indx;
9965 int ret;
c152c796
AM
9966
9967 *pindex = -1;
9968
9969 if (elf_bad_symtab (input_bfd))
9970 {
9971 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9972 {
9973 *ppsection = NULL;
9974 continue;
9975 }
9976 }
9977
9978 if (isym->st_shndx == SHN_UNDEF)
9979 isec = bfd_und_section_ptr;
c152c796
AM
9980 else if (isym->st_shndx == SHN_ABS)
9981 isec = bfd_abs_section_ptr;
9982 else if (isym->st_shndx == SHN_COMMON)
9983 isec = bfd_com_section_ptr;
9984 else
9985 {
cb33740c
AM
9986 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9987 if (isec == NULL)
9988 {
9989 /* Don't attempt to output symbols with st_shnx in the
9990 reserved range other than SHN_ABS and SHN_COMMON. */
9991 *ppsection = NULL;
9992 continue;
9993 }
dbaa2011 9994 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9995 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9996 isym->st_value =
9997 _bfd_merged_section_offset (output_bfd, &isec,
9998 elf_section_data (isec)->sec_info,
9999 isym->st_value);
c152c796
AM
10000 }
10001
10002 *ppsection = isec;
10003
d983c8c5
AM
10004 /* Don't output the first, undefined, symbol. In fact, don't
10005 output any undefined local symbol. */
10006 if (isec == bfd_und_section_ptr)
c152c796
AM
10007 continue;
10008
10009 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10010 {
10011 /* We never output section symbols. Instead, we use the
10012 section symbol of the corresponding section in the output
10013 file. */
10014 continue;
10015 }
10016
10017 /* If we are stripping all symbols, we don't want to output this
10018 one. */
8b127cbc 10019 if (flinfo->info->strip == strip_all)
c152c796
AM
10020 continue;
10021
10022 /* If we are discarding all local symbols, we don't want to
10023 output this one. If we are generating a relocatable output
10024 file, then some of the local symbols may be required by
10025 relocs; we output them below as we discover that they are
10026 needed. */
8b127cbc 10027 if (flinfo->info->discard == discard_all)
c152c796
AM
10028 continue;
10029
10030 /* If this symbol is defined in a section which we are
f02571c5
AM
10031 discarding, we don't need to keep it. */
10032 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10033 && isym->st_shndx < SHN_LORESERVE
10034 && bfd_section_removed_from_list (output_bfd,
10035 isec->output_section))
e75a280b
L
10036 continue;
10037
c152c796
AM
10038 /* Get the name of the symbol. */
10039 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10040 isym->st_name);
10041 if (name == NULL)
10042 return FALSE;
10043
10044 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10045 if ((flinfo->info->strip == strip_some
10046 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10047 == NULL))
8b127cbc 10048 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10049 && (isec->flags & SEC_MERGE)
10050 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10051 || flinfo->info->discard == discard_l)
c152c796
AM
10052 && bfd_is_local_label_name (input_bfd, name)))
10053 continue;
10054
ffbc01cc
AM
10055 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10056 {
ce875075
AM
10057 if (input_bfd->lto_output)
10058 /* -flto puts a temp file name here. This means builds
10059 are not reproducible. Discard the symbol. */
10060 continue;
ffbc01cc
AM
10061 have_file_sym = TRUE;
10062 flinfo->filesym_count += 1;
10063 }
10064 if (!have_file_sym)
10065 {
10066 /* In the absence of debug info, bfd_find_nearest_line uses
10067 FILE symbols to determine the source file for local
10068 function symbols. Provide a FILE symbol here if input
10069 files lack such, so that their symbols won't be
10070 associated with a previous input file. It's not the
10071 source file, but the best we can do. */
10072 have_file_sym = TRUE;
10073 flinfo->filesym_count += 1;
10074 memset (&osym, 0, sizeof (osym));
10075 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10076 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10077 if (!elf_link_output_symstrtab (flinfo,
10078 (input_bfd->lto_output ? NULL
10079 : input_bfd->filename),
10080 &osym, bfd_abs_section_ptr,
10081 NULL))
ffbc01cc
AM
10082 return FALSE;
10083 }
10084
c152c796
AM
10085 osym = *isym;
10086
10087 /* Adjust the section index for the output file. */
10088 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10089 isec->output_section);
10090 if (osym.st_shndx == SHN_BAD)
10091 return FALSE;
10092
c152c796
AM
10093 /* ELF symbols in relocatable files are section relative, but
10094 in executable files they are virtual addresses. Note that
10095 this code assumes that all ELF sections have an associated
10096 BFD section with a reasonable value for output_offset; below
10097 we assume that they also have a reasonable value for
10098 output_section. Any special sections must be set up to meet
10099 these requirements. */
10100 osym.st_value += isec->output_offset;
0e1862bb 10101 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10102 {
10103 osym.st_value += isec->output_section->vma;
10104 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10105 {
10106 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10107 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10108 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10109 }
10110 }
10111
6e0b88f1 10112 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10113 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10114 if (ret == 0)
c152c796 10115 return FALSE;
6e0b88f1
AM
10116 else if (ret == 1)
10117 *pindex = indx;
c152c796
AM
10118 }
10119
310fd250
L
10120 if (bed->s->arch_size == 32)
10121 {
10122 r_type_mask = 0xff;
10123 r_sym_shift = 8;
10124 address_size = 4;
10125 }
10126 else
10127 {
10128 r_type_mask = 0xffffffff;
10129 r_sym_shift = 32;
10130 address_size = 8;
10131 }
10132
c152c796
AM
10133 /* Relocate the contents of each section. */
10134 sym_hashes = elf_sym_hashes (input_bfd);
10135 for (o = input_bfd->sections; o != NULL; o = o->next)
10136 {
10137 bfd_byte *contents;
10138
10139 if (! o->linker_mark)
10140 {
10141 /* This section was omitted from the link. */
10142 continue;
10143 }
10144
0e1862bb 10145 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10146 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10147 {
10148 /* Deal with the group signature symbol. */
10149 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10150 unsigned long symndx = sec_data->this_hdr.sh_info;
10151 asection *osec = o->output_section;
10152
10153 if (symndx >= locsymcount
10154 || (elf_bad_symtab (input_bfd)
8b127cbc 10155 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10156 {
10157 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10158 while (h->root.type == bfd_link_hash_indirect
10159 || h->root.type == bfd_link_hash_warning)
10160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10161 /* Arrange for symbol to be output. */
10162 h->indx = -2;
10163 elf_section_data (osec)->this_hdr.sh_info = -2;
10164 }
10165 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10166 {
10167 /* We'll use the output section target_index. */
8b127cbc 10168 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10169 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10170 }
10171 else
10172 {
8b127cbc 10173 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10174 {
10175 /* Otherwise output the local symbol now. */
10176 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10177 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10178 const char *name;
6e0b88f1
AM
10179 long indx;
10180 int ret;
bcacc0f5
AM
10181
10182 name = bfd_elf_string_from_elf_section (input_bfd,
10183 symtab_hdr->sh_link,
10184 sym.st_name);
10185 if (name == NULL)
10186 return FALSE;
10187
10188 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10189 sec);
10190 if (sym.st_shndx == SHN_BAD)
10191 return FALSE;
10192
10193 sym.st_value += o->output_offset;
10194
6e0b88f1 10195 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10196 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10197 NULL);
6e0b88f1 10198 if (ret == 0)
bcacc0f5 10199 return FALSE;
6e0b88f1 10200 else if (ret == 1)
8b127cbc 10201 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10202 else
10203 abort ();
bcacc0f5
AM
10204 }
10205 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10206 = flinfo->indices[symndx];
bcacc0f5
AM
10207 }
10208 }
10209
c152c796 10210 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10211 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10212 continue;
10213
10214 if ((o->flags & SEC_LINKER_CREATED) != 0)
10215 {
10216 /* Section was created by _bfd_elf_link_create_dynamic_sections
10217 or somesuch. */
10218 continue;
10219 }
10220
10221 /* Get the contents of the section. They have been cached by a
10222 relaxation routine. Note that o is a section in an input
10223 file, so the contents field will not have been set by any of
10224 the routines which work on output files. */
10225 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10226 {
10227 contents = elf_section_data (o)->this_hdr.contents;
10228 if (bed->caches_rawsize
10229 && o->rawsize != 0
10230 && o->rawsize < o->size)
10231 {
10232 memcpy (flinfo->contents, contents, o->rawsize);
10233 contents = flinfo->contents;
10234 }
10235 }
c152c796
AM
10236 else
10237 {
8b127cbc 10238 contents = flinfo->contents;
4a114e3e 10239 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10240 return FALSE;
10241 }
10242
10243 if ((o->flags & SEC_RELOC) != 0)
10244 {
10245 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10246 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10247 int action_discarded;
ece5ef60 10248 int ret;
c152c796
AM
10249
10250 /* Get the swapped relocs. */
10251 internal_relocs
8b127cbc
AM
10252 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10253 flinfo->internal_relocs, FALSE);
c152c796
AM
10254 if (internal_relocs == NULL
10255 && o->reloc_count > 0)
10256 return FALSE;
10257
310fd250
L
10258 /* We need to reverse-copy input .ctors/.dtors sections if
10259 they are placed in .init_array/.finit_array for output. */
10260 if (o->size > address_size
10261 && ((strncmp (o->name, ".ctors", 6) == 0
10262 && strcmp (o->output_section->name,
10263 ".init_array") == 0)
10264 || (strncmp (o->name, ".dtors", 6) == 0
10265 && strcmp (o->output_section->name,
10266 ".fini_array") == 0))
10267 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10268 {
310fd250
L
10269 if (o->size != o->reloc_count * address_size)
10270 {
4eca0228 10271 _bfd_error_handler
695344c0 10272 /* xgettext:c-format */
310fd250
L
10273 (_("error: %B: size of section %A is not "
10274 "multiple of address size"),
10275 input_bfd, o);
10276 bfd_set_error (bfd_error_on_input);
10277 return FALSE;
10278 }
10279 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10280 }
10281
0f02bbd9 10282 action_discarded = -1;
c152c796 10283 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10284 action_discarded = (*bed->action_discarded) (o);
10285
10286 /* Run through the relocs evaluating complex reloc symbols and
10287 looking for relocs against symbols from discarded sections
10288 or section symbols from removed link-once sections.
10289 Complain about relocs against discarded sections. Zero
10290 relocs against removed link-once sections. */
10291
10292 rel = internal_relocs;
10293 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10294 for ( ; rel < relend; rel++)
c152c796 10295 {
0f02bbd9
AM
10296 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10297 unsigned int s_type;
10298 asection **ps, *sec;
10299 struct elf_link_hash_entry *h = NULL;
10300 const char *sym_name;
c152c796 10301
0f02bbd9
AM
10302 if (r_symndx == STN_UNDEF)
10303 continue;
c152c796 10304
0f02bbd9
AM
10305 if (r_symndx >= locsymcount
10306 || (elf_bad_symtab (input_bfd)
8b127cbc 10307 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10308 {
10309 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10310
0f02bbd9
AM
10311 /* Badly formatted input files can contain relocs that
10312 reference non-existant symbols. Check here so that
10313 we do not seg fault. */
10314 if (h == NULL)
c152c796 10315 {
0f02bbd9 10316 char buffer [32];
dce669a1 10317
0f02bbd9 10318 sprintf_vma (buffer, rel->r_info);
4eca0228 10319 _bfd_error_handler
695344c0 10320 /* xgettext:c-format */
0f02bbd9
AM
10321 (_("error: %B contains a reloc (0x%s) for section %A "
10322 "that references a non-existent global symbol"),
10323 input_bfd, o, buffer);
10324 bfd_set_error (bfd_error_bad_value);
10325 return FALSE;
10326 }
3b36f7e6 10327
0f02bbd9
AM
10328 while (h->root.type == bfd_link_hash_indirect
10329 || h->root.type == bfd_link_hash_warning)
10330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10331
0f02bbd9 10332 s_type = h->type;
cdd3575c 10333
9e2dec47 10334 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10335 mark the symbol as undefined. Note that the
10336 linker may attach linker created dynamic sections
10337 to the plugin bfd. Symbols defined in linker
10338 created sections are not plugin symbols. */
9e2dec47
L
10339 if (h->root.non_ir_ref
10340 && (h->root.type == bfd_link_hash_defined
10341 || h->root.type == bfd_link_hash_defweak)
10342 && (h->root.u.def.section->flags
10343 & SEC_LINKER_CREATED) == 0
10344 && h->root.u.def.section->owner != NULL
10345 && (h->root.u.def.section->owner->flags
10346 & BFD_PLUGIN) != 0)
10347 {
10348 h->root.type = bfd_link_hash_undefined;
10349 h->root.u.undef.abfd = h->root.u.def.section->owner;
10350 }
10351
0f02bbd9
AM
10352 ps = NULL;
10353 if (h->root.type == bfd_link_hash_defined
10354 || h->root.type == bfd_link_hash_defweak)
10355 ps = &h->root.u.def.section;
10356
10357 sym_name = h->root.root.string;
10358 }
10359 else
10360 {
10361 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10362
10363 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10364 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10365 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10366 sym, *ps);
10367 }
c152c796 10368
c301e700 10369 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10370 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10371 {
10372 bfd_vma val;
10373 bfd_vma dot = (rel->r_offset
10374 + o->output_offset + o->output_section->vma);
10375#ifdef DEBUG
10376 printf ("Encountered a complex symbol!");
10377 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10378 input_bfd->filename, o->name,
10379 (long) (rel - internal_relocs));
0f02bbd9
AM
10380 printf (" symbol: idx %8.8lx, name %s\n",
10381 r_symndx, sym_name);
10382 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10383 (unsigned long) rel->r_info,
10384 (unsigned long) rel->r_offset);
10385#endif
8b127cbc 10386 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10387 isymbuf, locsymcount, s_type == STT_SRELC))
10388 return FALSE;
10389
10390 /* Symbol evaluated OK. Update to absolute value. */
10391 set_symbol_value (input_bfd, isymbuf, locsymcount,
10392 r_symndx, val);
10393 continue;
10394 }
10395
10396 if (action_discarded != -1 && ps != NULL)
10397 {
cdd3575c
AM
10398 /* Complain if the definition comes from a
10399 discarded section. */
dbaa2011 10400 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10401 {
cf35638d 10402 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10403 if (action_discarded & COMPLAIN)
8b127cbc 10404 (*flinfo->info->callbacks->einfo)
695344c0 10405 /* xgettext:c-format */
e1fffbe6 10406 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10407 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10408 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10409
87e5235d 10410 /* Try to do the best we can to support buggy old
e0ae6d6f 10411 versions of gcc. Pretend that the symbol is
87e5235d
AM
10412 really defined in the kept linkonce section.
10413 FIXME: This is quite broken. Modifying the
10414 symbol here means we will be changing all later
e0ae6d6f 10415 uses of the symbol, not just in this section. */
0f02bbd9 10416 if (action_discarded & PRETEND)
87e5235d 10417 {
01b3c8ab
L
10418 asection *kept;
10419
c0f00686 10420 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10421 flinfo->info);
01b3c8ab 10422 if (kept != NULL)
87e5235d
AM
10423 {
10424 *ps = kept;
10425 continue;
10426 }
10427 }
c152c796
AM
10428 }
10429 }
10430 }
10431
10432 /* Relocate the section by invoking a back end routine.
10433
10434 The back end routine is responsible for adjusting the
10435 section contents as necessary, and (if using Rela relocs
10436 and generating a relocatable output file) adjusting the
10437 reloc addend as necessary.
10438
10439 The back end routine does not have to worry about setting
10440 the reloc address or the reloc symbol index.
10441
10442 The back end routine is given a pointer to the swapped in
10443 internal symbols, and can access the hash table entries
10444 for the external symbols via elf_sym_hashes (input_bfd).
10445
10446 When generating relocatable output, the back end routine
10447 must handle STB_LOCAL/STT_SECTION symbols specially. The
10448 output symbol is going to be a section symbol
10449 corresponding to the output section, which will require
10450 the addend to be adjusted. */
10451
8b127cbc 10452 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10453 input_bfd, o, contents,
10454 internal_relocs,
10455 isymbuf,
8b127cbc 10456 flinfo->sections);
ece5ef60 10457 if (!ret)
c152c796
AM
10458 return FALSE;
10459
ece5ef60 10460 if (ret == 2
0e1862bb 10461 || bfd_link_relocatable (flinfo->info)
8b127cbc 10462 || flinfo->info->emitrelocations)
c152c796
AM
10463 {
10464 Elf_Internal_Rela *irela;
d4730f92 10465 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10466 bfd_vma last_offset;
10467 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10468 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10469 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10470 unsigned int next_erel;
c152c796 10471 bfd_boolean rela_normal;
d4730f92 10472 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10473
d4730f92
BS
10474 esdi = elf_section_data (o);
10475 esdo = elf_section_data (o->output_section);
10476 rela_normal = FALSE;
c152c796
AM
10477
10478 /* Adjust the reloc addresses and symbol indices. */
10479
10480 irela = internal_relocs;
10481 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10482 rel_hash = esdo->rel.hashes + esdo->rel.count;
10483 /* We start processing the REL relocs, if any. When we reach
10484 IRELAMID in the loop, we switch to the RELA relocs. */
10485 irelamid = irela;
10486 if (esdi->rel.hdr != NULL)
10487 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10488 * bed->s->int_rels_per_ext_rel);
eac338cf 10489 rel_hash_list = rel_hash;
d4730f92 10490 rela_hash_list = NULL;
c152c796 10491 last_offset = o->output_offset;
0e1862bb 10492 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10493 last_offset += o->output_section->vma;
10494 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10495 {
10496 unsigned long r_symndx;
10497 asection *sec;
10498 Elf_Internal_Sym sym;
10499
10500 if (next_erel == bed->s->int_rels_per_ext_rel)
10501 {
10502 rel_hash++;
10503 next_erel = 0;
10504 }
10505
d4730f92
BS
10506 if (irela == irelamid)
10507 {
10508 rel_hash = esdo->rela.hashes + esdo->rela.count;
10509 rela_hash_list = rel_hash;
10510 rela_normal = bed->rela_normal;
10511 }
10512
c152c796 10513 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10514 flinfo->info, o,
c152c796
AM
10515 irela->r_offset);
10516 if (irela->r_offset >= (bfd_vma) -2)
10517 {
10518 /* This is a reloc for a deleted entry or somesuch.
10519 Turn it into an R_*_NONE reloc, at the same
10520 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10521 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10522 being ordered. */
10523 irela->r_offset = last_offset;
10524 irela->r_info = 0;
10525 irela->r_addend = 0;
10526 continue;
10527 }
10528
10529 irela->r_offset += o->output_offset;
10530
10531 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10532 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10533 irela->r_offset += o->output_section->vma;
10534
10535 last_offset = irela->r_offset;
10536
10537 r_symndx = irela->r_info >> r_sym_shift;
10538 if (r_symndx == STN_UNDEF)
10539 continue;
10540
10541 if (r_symndx >= locsymcount
10542 || (elf_bad_symtab (input_bfd)
8b127cbc 10543 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10544 {
10545 struct elf_link_hash_entry *rh;
10546 unsigned long indx;
10547
10548 /* This is a reloc against a global symbol. We
10549 have not yet output all the local symbols, so
10550 we do not know the symbol index of any global
10551 symbol. We set the rel_hash entry for this
10552 reloc to point to the global hash table entry
10553 for this symbol. The symbol index is then
ee75fd95 10554 set at the end of bfd_elf_final_link. */
c152c796
AM
10555 indx = r_symndx - extsymoff;
10556 rh = elf_sym_hashes (input_bfd)[indx];
10557 while (rh->root.type == bfd_link_hash_indirect
10558 || rh->root.type == bfd_link_hash_warning)
10559 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10560
10561 /* Setting the index to -2 tells
10562 elf_link_output_extsym that this symbol is
10563 used by a reloc. */
10564 BFD_ASSERT (rh->indx < 0);
10565 rh->indx = -2;
10566
10567 *rel_hash = rh;
10568
10569 continue;
10570 }
10571
10572 /* This is a reloc against a local symbol. */
10573
10574 *rel_hash = NULL;
10575 sym = isymbuf[r_symndx];
8b127cbc 10576 sec = flinfo->sections[r_symndx];
c152c796
AM
10577 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10578 {
10579 /* I suppose the backend ought to fill in the
10580 section of any STT_SECTION symbol against a
6a8d1586 10581 processor specific section. */
cf35638d 10582 r_symndx = STN_UNDEF;
6a8d1586
AM
10583 if (bfd_is_abs_section (sec))
10584 ;
c152c796
AM
10585 else if (sec == NULL || sec->owner == NULL)
10586 {
10587 bfd_set_error (bfd_error_bad_value);
10588 return FALSE;
10589 }
10590 else
10591 {
6a8d1586
AM
10592 asection *osec = sec->output_section;
10593
10594 /* If we have discarded a section, the output
10595 section will be the absolute section. In
ab96bf03
AM
10596 case of discarded SEC_MERGE sections, use
10597 the kept section. relocate_section should
10598 have already handled discarded linkonce
10599 sections. */
6a8d1586
AM
10600 if (bfd_is_abs_section (osec)
10601 && sec->kept_section != NULL
10602 && sec->kept_section->output_section != NULL)
10603 {
10604 osec = sec->kept_section->output_section;
10605 irela->r_addend -= osec->vma;
10606 }
10607
10608 if (!bfd_is_abs_section (osec))
10609 {
10610 r_symndx = osec->target_index;
cf35638d 10611 if (r_symndx == STN_UNDEF)
74541ad4 10612 {
051d833a
AM
10613 irela->r_addend += osec->vma;
10614 osec = _bfd_nearby_section (output_bfd, osec,
10615 osec->vma);
10616 irela->r_addend -= osec->vma;
10617 r_symndx = osec->target_index;
74541ad4 10618 }
6a8d1586 10619 }
c152c796
AM
10620 }
10621
10622 /* Adjust the addend according to where the
10623 section winds up in the output section. */
10624 if (rela_normal)
10625 irela->r_addend += sec->output_offset;
10626 }
10627 else
10628 {
8b127cbc 10629 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10630 {
10631 unsigned long shlink;
10632 const char *name;
10633 asection *osec;
6e0b88f1 10634 long indx;
c152c796 10635
8b127cbc 10636 if (flinfo->info->strip == strip_all)
c152c796
AM
10637 {
10638 /* You can't do ld -r -s. */
10639 bfd_set_error (bfd_error_invalid_operation);
10640 return FALSE;
10641 }
10642
10643 /* This symbol was skipped earlier, but
10644 since it is needed by a reloc, we
10645 must output it now. */
10646 shlink = symtab_hdr->sh_link;
10647 name = (bfd_elf_string_from_elf_section
10648 (input_bfd, shlink, sym.st_name));
10649 if (name == NULL)
10650 return FALSE;
10651
10652 osec = sec->output_section;
10653 sym.st_shndx =
10654 _bfd_elf_section_from_bfd_section (output_bfd,
10655 osec);
10656 if (sym.st_shndx == SHN_BAD)
10657 return FALSE;
10658
10659 sym.st_value += sec->output_offset;
0e1862bb 10660 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10661 {
10662 sym.st_value += osec->vma;
10663 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10664 {
10665 /* STT_TLS symbols are relative to PT_TLS
10666 segment base. */
8b127cbc 10667 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10668 ->tls_sec != NULL);
8b127cbc 10669 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10670 ->tls_sec->vma);
10671 }
10672 }
10673
6e0b88f1 10674 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10675 ret = elf_link_output_symstrtab (flinfo, name,
10676 &sym, sec,
10677 NULL);
6e0b88f1 10678 if (ret == 0)
c152c796 10679 return FALSE;
6e0b88f1 10680 else if (ret == 1)
8b127cbc 10681 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10682 else
10683 abort ();
c152c796
AM
10684 }
10685
8b127cbc 10686 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10687 }
10688
10689 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10690 | (irela->r_info & r_type_mask));
10691 }
10692
10693 /* Swap out the relocs. */
d4730f92
BS
10694 input_rel_hdr = esdi->rel.hdr;
10695 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10696 {
d4730f92
BS
10697 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10698 input_rel_hdr,
10699 internal_relocs,
10700 rel_hash_list))
10701 return FALSE;
c152c796
AM
10702 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10703 * bed->s->int_rels_per_ext_rel);
eac338cf 10704 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10705 }
10706
10707 input_rela_hdr = esdi->rela.hdr;
10708 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10709 {
eac338cf 10710 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10711 input_rela_hdr,
eac338cf 10712 internal_relocs,
d4730f92 10713 rela_hash_list))
c152c796
AM
10714 return FALSE;
10715 }
10716 }
10717 }
10718
10719 /* Write out the modified section contents. */
10720 if (bed->elf_backend_write_section
8b127cbc 10721 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10722 contents))
c152c796
AM
10723 {
10724 /* Section written out. */
10725 }
10726 else switch (o->sec_info_type)
10727 {
dbaa2011 10728 case SEC_INFO_TYPE_STABS:
c152c796
AM
10729 if (! (_bfd_write_section_stabs
10730 (output_bfd,
8b127cbc 10731 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10732 o, &elf_section_data (o)->sec_info, contents)))
10733 return FALSE;
10734 break;
dbaa2011 10735 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10736 if (! _bfd_write_merged_section (output_bfd, o,
10737 elf_section_data (o)->sec_info))
10738 return FALSE;
10739 break;
dbaa2011 10740 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10741 {
8b127cbc 10742 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10743 o, contents))
10744 return FALSE;
10745 }
10746 break;
2f0c68f2
CM
10747 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10748 {
10749 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10750 flinfo->info,
10751 o, contents))
10752 return FALSE;
10753 }
10754 break;
c152c796
AM
10755 default:
10756 {
310fd250
L
10757 if (! (o->flags & SEC_EXCLUDE))
10758 {
10759 file_ptr offset = (file_ptr) o->output_offset;
10760 bfd_size_type todo = o->size;
37b01f6a
DG
10761
10762 offset *= bfd_octets_per_byte (output_bfd);
10763
310fd250
L
10764 if ((o->flags & SEC_ELF_REVERSE_COPY))
10765 {
10766 /* Reverse-copy input section to output. */
10767 do
10768 {
10769 todo -= address_size;
10770 if (! bfd_set_section_contents (output_bfd,
10771 o->output_section,
10772 contents + todo,
10773 offset,
10774 address_size))
10775 return FALSE;
10776 if (todo == 0)
10777 break;
10778 offset += address_size;
10779 }
10780 while (1);
10781 }
10782 else if (! bfd_set_section_contents (output_bfd,
10783 o->output_section,
10784 contents,
10785 offset, todo))
10786 return FALSE;
10787 }
c152c796
AM
10788 }
10789 break;
10790 }
10791 }
10792
10793 return TRUE;
10794}
10795
10796/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10797 requested by the linker, and does not come from any input file. This
c152c796
AM
10798 is used to build constructor and destructor tables when linking
10799 with -Ur. */
10800
10801static bfd_boolean
10802elf_reloc_link_order (bfd *output_bfd,
10803 struct bfd_link_info *info,
10804 asection *output_section,
10805 struct bfd_link_order *link_order)
10806{
10807 reloc_howto_type *howto;
10808 long indx;
10809 bfd_vma offset;
10810 bfd_vma addend;
d4730f92 10811 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10812 struct elf_link_hash_entry **rel_hash_ptr;
10813 Elf_Internal_Shdr *rel_hdr;
10814 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10815 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10816 bfd_byte *erel;
10817 unsigned int i;
d4730f92 10818 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10819
10820 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10821 if (howto == NULL)
10822 {
10823 bfd_set_error (bfd_error_bad_value);
10824 return FALSE;
10825 }
10826
10827 addend = link_order->u.reloc.p->addend;
10828
d4730f92
BS
10829 if (esdo->rel.hdr)
10830 reldata = &esdo->rel;
10831 else if (esdo->rela.hdr)
10832 reldata = &esdo->rela;
10833 else
10834 {
10835 reldata = NULL;
10836 BFD_ASSERT (0);
10837 }
10838
c152c796 10839 /* Figure out the symbol index. */
d4730f92 10840 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10841 if (link_order->type == bfd_section_reloc_link_order)
10842 {
10843 indx = link_order->u.reloc.p->u.section->target_index;
10844 BFD_ASSERT (indx != 0);
10845 *rel_hash_ptr = NULL;
10846 }
10847 else
10848 {
10849 struct elf_link_hash_entry *h;
10850
10851 /* Treat a reloc against a defined symbol as though it were
10852 actually against the section. */
10853 h = ((struct elf_link_hash_entry *)
10854 bfd_wrapped_link_hash_lookup (output_bfd, info,
10855 link_order->u.reloc.p->u.name,
10856 FALSE, FALSE, TRUE));
10857 if (h != NULL
10858 && (h->root.type == bfd_link_hash_defined
10859 || h->root.type == bfd_link_hash_defweak))
10860 {
10861 asection *section;
10862
10863 section = h->root.u.def.section;
10864 indx = section->output_section->target_index;
10865 *rel_hash_ptr = NULL;
10866 /* It seems that we ought to add the symbol value to the
10867 addend here, but in practice it has already been added
10868 because it was passed to constructor_callback. */
10869 addend += section->output_section->vma + section->output_offset;
10870 }
10871 else if (h != NULL)
10872 {
10873 /* Setting the index to -2 tells elf_link_output_extsym that
10874 this symbol is used by a reloc. */
10875 h->indx = -2;
10876 *rel_hash_ptr = h;
10877 indx = 0;
10878 }
10879 else
10880 {
1a72702b
AM
10881 (*info->callbacks->unattached_reloc)
10882 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10883 indx = 0;
10884 }
10885 }
10886
10887 /* If this is an inplace reloc, we must write the addend into the
10888 object file. */
10889 if (howto->partial_inplace && addend != 0)
10890 {
10891 bfd_size_type size;
10892 bfd_reloc_status_type rstat;
10893 bfd_byte *buf;
10894 bfd_boolean ok;
10895 const char *sym_name;
10896
a50b1753
NC
10897 size = (bfd_size_type) bfd_get_reloc_size (howto);
10898 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10899 if (buf == NULL && size != 0)
c152c796
AM
10900 return FALSE;
10901 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10902 switch (rstat)
10903 {
10904 case bfd_reloc_ok:
10905 break;
10906
10907 default:
10908 case bfd_reloc_outofrange:
10909 abort ();
10910
10911 case bfd_reloc_overflow:
10912 if (link_order->type == bfd_section_reloc_link_order)
10913 sym_name = bfd_section_name (output_bfd,
10914 link_order->u.reloc.p->u.section);
10915 else
10916 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10917 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10918 howto->name, addend, NULL, NULL,
10919 (bfd_vma) 0);
c152c796
AM
10920 break;
10921 }
37b01f6a 10922
c152c796 10923 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10924 link_order->offset
10925 * bfd_octets_per_byte (output_bfd),
10926 size);
c152c796
AM
10927 free (buf);
10928 if (! ok)
10929 return FALSE;
10930 }
10931
10932 /* The address of a reloc is relative to the section in a
10933 relocatable file, and is a virtual address in an executable
10934 file. */
10935 offset = link_order->offset;
0e1862bb 10936 if (! bfd_link_relocatable (info))
c152c796
AM
10937 offset += output_section->vma;
10938
10939 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10940 {
10941 irel[i].r_offset = offset;
10942 irel[i].r_info = 0;
10943 irel[i].r_addend = 0;
10944 }
10945 if (bed->s->arch_size == 32)
10946 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10947 else
10948 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10949
d4730f92 10950 rel_hdr = reldata->hdr;
c152c796
AM
10951 erel = rel_hdr->contents;
10952 if (rel_hdr->sh_type == SHT_REL)
10953 {
d4730f92 10954 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10955 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10956 }
10957 else
10958 {
10959 irel[0].r_addend = addend;
d4730f92 10960 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10961 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10962 }
10963
d4730f92 10964 ++reldata->count;
c152c796
AM
10965
10966 return TRUE;
10967}
10968
0b52efa6
PB
10969
10970/* Get the output vma of the section pointed to by the sh_link field. */
10971
10972static bfd_vma
10973elf_get_linked_section_vma (struct bfd_link_order *p)
10974{
10975 Elf_Internal_Shdr **elf_shdrp;
10976 asection *s;
10977 int elfsec;
10978
10979 s = p->u.indirect.section;
10980 elf_shdrp = elf_elfsections (s->owner);
10981 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10982 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10983 /* PR 290:
10984 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10985 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10986 sh_info fields. Hence we could get the situation
10987 where elfsec is 0. */
10988 if (elfsec == 0)
10989 {
10990 const struct elf_backend_data *bed
10991 = get_elf_backend_data (s->owner);
10992 if (bed->link_order_error_handler)
d003868e 10993 bed->link_order_error_handler
695344c0 10994 /* xgettext:c-format */
d003868e 10995 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10996 return 0;
10997 }
10998 else
10999 {
11000 s = elf_shdrp[elfsec]->bfd_section;
11001 return s->output_section->vma + s->output_offset;
11002 }
0b52efa6
PB
11003}
11004
11005
11006/* Compare two sections based on the locations of the sections they are
11007 linked to. Used by elf_fixup_link_order. */
11008
11009static int
11010compare_link_order (const void * a, const void * b)
11011{
11012 bfd_vma apos;
11013 bfd_vma bpos;
11014
11015 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11016 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11017 if (apos < bpos)
11018 return -1;
11019 return apos > bpos;
11020}
11021
11022
11023/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11024 order as their linked sections. Returns false if this could not be done
11025 because an output section includes both ordered and unordered
11026 sections. Ideally we'd do this in the linker proper. */
11027
11028static bfd_boolean
11029elf_fixup_link_order (bfd *abfd, asection *o)
11030{
11031 int seen_linkorder;
11032 int seen_other;
11033 int n;
11034 struct bfd_link_order *p;
11035 bfd *sub;
11036 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11037 unsigned elfsec;
0b52efa6 11038 struct bfd_link_order **sections;
d33cdfe3 11039 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11040 bfd_vma offset;
3b36f7e6 11041
d33cdfe3
L
11042 other_sec = NULL;
11043 linkorder_sec = NULL;
0b52efa6
PB
11044 seen_other = 0;
11045 seen_linkorder = 0;
8423293d 11046 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11047 {
d33cdfe3 11048 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11049 {
11050 s = p->u.indirect.section;
d33cdfe3
L
11051 sub = s->owner;
11052 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11053 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11054 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11055 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11056 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11057 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11058 {
11059 seen_linkorder++;
11060 linkorder_sec = s;
11061 }
0b52efa6 11062 else
d33cdfe3
L
11063 {
11064 seen_other++;
11065 other_sec = s;
11066 }
0b52efa6
PB
11067 }
11068 else
11069 seen_other++;
d33cdfe3
L
11070
11071 if (seen_other && seen_linkorder)
11072 {
11073 if (other_sec && linkorder_sec)
4eca0228 11074 _bfd_error_handler
695344c0 11075 /* xgettext:c-format */
4eca0228
AM
11076 (_("%A has both ordered [`%A' in %B] "
11077 "and unordered [`%A' in %B] sections"),
11078 o, linkorder_sec,
11079 linkorder_sec->owner, other_sec,
11080 other_sec->owner);
d33cdfe3 11081 else
4eca0228
AM
11082 _bfd_error_handler
11083 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11084 bfd_set_error (bfd_error_bad_value);
11085 return FALSE;
11086 }
0b52efa6
PB
11087 }
11088
11089 if (!seen_linkorder)
11090 return TRUE;
11091
0b52efa6 11092 sections = (struct bfd_link_order **)
14b1c01e
AM
11093 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11094 if (sections == NULL)
11095 return FALSE;
0b52efa6 11096 seen_linkorder = 0;
3b36f7e6 11097
8423293d 11098 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11099 {
11100 sections[seen_linkorder++] = p;
11101 }
11102 /* Sort the input sections in the order of their linked section. */
11103 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11104 compare_link_order);
11105
11106 /* Change the offsets of the sections. */
11107 offset = 0;
11108 for (n = 0; n < seen_linkorder; n++)
11109 {
11110 s = sections[n]->u.indirect.section;
461686a3 11111 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11112 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11113 sections[n]->offset = offset;
11114 offset += sections[n]->size;
11115 }
11116
4dd07732 11117 free (sections);
0b52efa6
PB
11118 return TRUE;
11119}
11120
76359541
TP
11121/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11122 Returns TRUE upon success, FALSE otherwise. */
11123
11124static bfd_boolean
11125elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11126{
11127 bfd_boolean ret = FALSE;
11128 bfd *implib_bfd;
11129 const struct elf_backend_data *bed;
11130 flagword flags;
11131 enum bfd_architecture arch;
11132 unsigned int mach;
11133 asymbol **sympp = NULL;
11134 long symsize;
11135 long symcount;
11136 long src_count;
11137 elf_symbol_type *osymbuf;
11138
11139 implib_bfd = info->out_implib_bfd;
11140 bed = get_elf_backend_data (abfd);
11141
11142 if (!bfd_set_format (implib_bfd, bfd_object))
11143 return FALSE;
11144
11145 flags = bfd_get_file_flags (abfd);
11146 flags &= ~HAS_RELOC;
11147 if (!bfd_set_start_address (implib_bfd, 0)
11148 || !bfd_set_file_flags (implib_bfd, flags))
11149 return FALSE;
11150
11151 /* Copy architecture of output file to import library file. */
11152 arch = bfd_get_arch (abfd);
11153 mach = bfd_get_mach (abfd);
11154 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11155 && (abfd->target_defaulted
11156 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11157 return FALSE;
11158
11159 /* Get symbol table size. */
11160 symsize = bfd_get_symtab_upper_bound (abfd);
11161 if (symsize < 0)
11162 return FALSE;
11163
11164 /* Read in the symbol table. */
11165 sympp = (asymbol **) xmalloc (symsize);
11166 symcount = bfd_canonicalize_symtab (abfd, sympp);
11167 if (symcount < 0)
11168 goto free_sym_buf;
11169
11170 /* Allow the BFD backend to copy any private header data it
11171 understands from the output BFD to the import library BFD. */
11172 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11173 goto free_sym_buf;
11174
11175 /* Filter symbols to appear in the import library. */
11176 if (bed->elf_backend_filter_implib_symbols)
11177 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11178 symcount);
11179 else
11180 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11181 if (symcount == 0)
11182 {
5df1bc57 11183 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11184 _bfd_error_handler (_("%B: no symbol found for import library"),
11185 implib_bfd);
76359541
TP
11186 goto free_sym_buf;
11187 }
11188
11189
11190 /* Make symbols absolute. */
11191 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11192 sizeof (*osymbuf));
11193 for (src_count = 0; src_count < symcount; src_count++)
11194 {
11195 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11196 sizeof (*osymbuf));
11197 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11198 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11199 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11200 osymbuf[src_count].internal_elf_sym.st_value =
11201 osymbuf[src_count].symbol.value;
11202 sympp[src_count] = &osymbuf[src_count].symbol;
11203 }
11204
11205 bfd_set_symtab (implib_bfd, sympp, symcount);
11206
11207 /* Allow the BFD backend to copy any private data it understands
11208 from the output BFD to the import library BFD. This is done last
11209 to permit the routine to look at the filtered symbol table. */
11210 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11211 goto free_sym_buf;
11212
11213 if (!bfd_close (implib_bfd))
11214 goto free_sym_buf;
11215
11216 ret = TRUE;
11217
11218free_sym_buf:
11219 free (sympp);
11220 return ret;
11221}
11222
9f7c3e5e
AM
11223static void
11224elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11225{
11226 asection *o;
11227
11228 if (flinfo->symstrtab != NULL)
ef10c3ac 11229 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11230 if (flinfo->contents != NULL)
11231 free (flinfo->contents);
11232 if (flinfo->external_relocs != NULL)
11233 free (flinfo->external_relocs);
11234 if (flinfo->internal_relocs != NULL)
11235 free (flinfo->internal_relocs);
11236 if (flinfo->external_syms != NULL)
11237 free (flinfo->external_syms);
11238 if (flinfo->locsym_shndx != NULL)
11239 free (flinfo->locsym_shndx);
11240 if (flinfo->internal_syms != NULL)
11241 free (flinfo->internal_syms);
11242 if (flinfo->indices != NULL)
11243 free (flinfo->indices);
11244 if (flinfo->sections != NULL)
11245 free (flinfo->sections);
9f7c3e5e
AM
11246 if (flinfo->symshndxbuf != NULL)
11247 free (flinfo->symshndxbuf);
11248 for (o = obfd->sections; o != NULL; o = o->next)
11249 {
11250 struct bfd_elf_section_data *esdo = elf_section_data (o);
11251 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11252 free (esdo->rel.hashes);
11253 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11254 free (esdo->rela.hashes);
11255 }
11256}
0b52efa6 11257
c152c796
AM
11258/* Do the final step of an ELF link. */
11259
11260bfd_boolean
11261bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11262{
11263 bfd_boolean dynamic;
11264 bfd_boolean emit_relocs;
11265 bfd *dynobj;
8b127cbc 11266 struct elf_final_link_info flinfo;
91d6fa6a
NC
11267 asection *o;
11268 struct bfd_link_order *p;
11269 bfd *sub;
c152c796
AM
11270 bfd_size_type max_contents_size;
11271 bfd_size_type max_external_reloc_size;
11272 bfd_size_type max_internal_reloc_count;
11273 bfd_size_type max_sym_count;
11274 bfd_size_type max_sym_shndx_count;
c152c796
AM
11275 Elf_Internal_Sym elfsym;
11276 unsigned int i;
11277 Elf_Internal_Shdr *symtab_hdr;
11278 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11280 struct elf_outext_info eoinfo;
11281 bfd_boolean merged;
11282 size_t relativecount = 0;
11283 asection *reldyn = 0;
11284 bfd_size_type amt;
104d59d1
JM
11285 asection *attr_section = NULL;
11286 bfd_vma attr_size = 0;
11287 const char *std_attrs_section;
64f52338 11288 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11289
64f52338 11290 if (!is_elf_hash_table (htab))
c152c796
AM
11291 return FALSE;
11292
0e1862bb 11293 if (bfd_link_pic (info))
c152c796
AM
11294 abfd->flags |= DYNAMIC;
11295
64f52338
AM
11296 dynamic = htab->dynamic_sections_created;
11297 dynobj = htab->dynobj;
c152c796 11298
0e1862bb 11299 emit_relocs = (bfd_link_relocatable (info)
a4676736 11300 || info->emitrelocations);
c152c796 11301
8b127cbc
AM
11302 flinfo.info = info;
11303 flinfo.output_bfd = abfd;
ef10c3ac 11304 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11305 if (flinfo.symstrtab == NULL)
c152c796
AM
11306 return FALSE;
11307
11308 if (! dynamic)
11309 {
8b127cbc
AM
11310 flinfo.hash_sec = NULL;
11311 flinfo.symver_sec = NULL;
c152c796
AM
11312 }
11313 else
11314 {
3d4d4302 11315 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11316 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11317 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11318 /* Note that it is OK if symver_sec is NULL. */
11319 }
11320
8b127cbc
AM
11321 flinfo.contents = NULL;
11322 flinfo.external_relocs = NULL;
11323 flinfo.internal_relocs = NULL;
11324 flinfo.external_syms = NULL;
11325 flinfo.locsym_shndx = NULL;
11326 flinfo.internal_syms = NULL;
11327 flinfo.indices = NULL;
11328 flinfo.sections = NULL;
8b127cbc 11329 flinfo.symshndxbuf = NULL;
ffbc01cc 11330 flinfo.filesym_count = 0;
c152c796 11331
104d59d1
JM
11332 /* The object attributes have been merged. Remove the input
11333 sections from the link, and set the contents of the output
11334 secton. */
11335 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11336 for (o = abfd->sections; o != NULL; o = o->next)
11337 {
11338 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11339 || strcmp (o->name, ".gnu.attributes") == 0)
11340 {
11341 for (p = o->map_head.link_order; p != NULL; p = p->next)
11342 {
11343 asection *input_section;
11344
11345 if (p->type != bfd_indirect_link_order)
11346 continue;
11347 input_section = p->u.indirect.section;
11348 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11349 elf_link_input_bfd ignores this section. */
11350 input_section->flags &= ~SEC_HAS_CONTENTS;
11351 }
a0c8462f 11352
104d59d1
JM
11353 attr_size = bfd_elf_obj_attr_size (abfd);
11354 if (attr_size)
11355 {
11356 bfd_set_section_size (abfd, o, attr_size);
11357 attr_section = o;
11358 /* Skip this section later on. */
11359 o->map_head.link_order = NULL;
11360 }
11361 else
11362 o->flags |= SEC_EXCLUDE;
11363 }
11364 }
11365
c152c796
AM
11366 /* Count up the number of relocations we will output for each output
11367 section, so that we know the sizes of the reloc sections. We
11368 also figure out some maximum sizes. */
11369 max_contents_size = 0;
11370 max_external_reloc_size = 0;
11371 max_internal_reloc_count = 0;
11372 max_sym_count = 0;
11373 max_sym_shndx_count = 0;
11374 merged = FALSE;
11375 for (o = abfd->sections; o != NULL; o = o->next)
11376 {
11377 struct bfd_elf_section_data *esdo = elf_section_data (o);
11378 o->reloc_count = 0;
11379
8423293d 11380 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11381 {
11382 unsigned int reloc_count = 0;
9eaff861 11383 unsigned int additional_reloc_count = 0;
c152c796 11384 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11385
11386 if (p->type == bfd_section_reloc_link_order
11387 || p->type == bfd_symbol_reloc_link_order)
11388 reloc_count = 1;
11389 else if (p->type == bfd_indirect_link_order)
11390 {
11391 asection *sec;
11392
11393 sec = p->u.indirect.section;
c152c796
AM
11394
11395 /* Mark all sections which are to be included in the
11396 link. This will normally be every section. We need
11397 to do this so that we can identify any sections which
11398 the linker has decided to not include. */
11399 sec->linker_mark = TRUE;
11400
11401 if (sec->flags & SEC_MERGE)
11402 merged = TRUE;
11403
eea6121a
AM
11404 if (sec->rawsize > max_contents_size)
11405 max_contents_size = sec->rawsize;
11406 if (sec->size > max_contents_size)
11407 max_contents_size = sec->size;
c152c796 11408
c152c796
AM
11409 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11410 && (sec->owner->flags & DYNAMIC) == 0)
11411 {
11412 size_t sym_count;
11413
a961cdd5
AM
11414 /* We are interested in just local symbols, not all
11415 symbols. */
c152c796
AM
11416 if (elf_bad_symtab (sec->owner))
11417 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11418 / bed->s->sizeof_sym);
11419 else
11420 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11421
11422 if (sym_count > max_sym_count)
11423 max_sym_count = sym_count;
11424
11425 if (sym_count > max_sym_shndx_count
6a40cf0c 11426 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11427 max_sym_shndx_count = sym_count;
11428
a961cdd5
AM
11429 if (esdo->this_hdr.sh_type == SHT_REL
11430 || esdo->this_hdr.sh_type == SHT_RELA)
11431 /* Some backends use reloc_count in relocation sections
11432 to count particular types of relocs. Of course,
11433 reloc sections themselves can't have relocations. */
11434 ;
11435 else if (emit_relocs)
11436 {
11437 reloc_count = sec->reloc_count;
11438 if (bed->elf_backend_count_additional_relocs)
11439 {
11440 int c;
11441 c = (*bed->elf_backend_count_additional_relocs) (sec);
11442 additional_reloc_count += c;
11443 }
11444 }
11445 else if (bed->elf_backend_count_relocs)
11446 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11447
11448 esdi = elf_section_data (sec);
11449
c152c796
AM
11450 if ((sec->flags & SEC_RELOC) != 0)
11451 {
d4730f92 11452 size_t ext_size = 0;
c152c796 11453
d4730f92
BS
11454 if (esdi->rel.hdr != NULL)
11455 ext_size = esdi->rel.hdr->sh_size;
11456 if (esdi->rela.hdr != NULL)
11457 ext_size += esdi->rela.hdr->sh_size;
7326c758 11458
c152c796
AM
11459 if (ext_size > max_external_reloc_size)
11460 max_external_reloc_size = ext_size;
11461 if (sec->reloc_count > max_internal_reloc_count)
11462 max_internal_reloc_count = sec->reloc_count;
11463 }
11464 }
11465 }
11466
11467 if (reloc_count == 0)
11468 continue;
11469
9eaff861 11470 reloc_count += additional_reloc_count;
c152c796
AM
11471 o->reloc_count += reloc_count;
11472
0e1862bb 11473 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11474 {
d4730f92 11475 if (esdi->rel.hdr)
9eaff861 11476 {
491d01d3 11477 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11478 esdo->rel.count += additional_reloc_count;
11479 }
d4730f92 11480 if (esdi->rela.hdr)
9eaff861 11481 {
491d01d3 11482 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11483 esdo->rela.count += additional_reloc_count;
11484 }
d4730f92
BS
11485 }
11486 else
11487 {
11488 if (o->use_rela_p)
11489 esdo->rela.count += reloc_count;
2c2b4ed4 11490 else
d4730f92 11491 esdo->rel.count += reloc_count;
c152c796 11492 }
c152c796
AM
11493 }
11494
9eaff861 11495 if (o->reloc_count > 0)
c152c796
AM
11496 o->flags |= SEC_RELOC;
11497 else
11498 {
11499 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11500 set it (this is probably a bug) and if it is set
11501 assign_section_numbers will create a reloc section. */
11502 o->flags &=~ SEC_RELOC;
11503 }
11504
11505 /* If the SEC_ALLOC flag is not set, force the section VMA to
11506 zero. This is done in elf_fake_sections as well, but forcing
11507 the VMA to 0 here will ensure that relocs against these
11508 sections are handled correctly. */
11509 if ((o->flags & SEC_ALLOC) == 0
11510 && ! o->user_set_vma)
11511 o->vma = 0;
11512 }
11513
0e1862bb 11514 if (! bfd_link_relocatable (info) && merged)
64f52338 11515 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11516
11517 /* Figure out the file positions for everything but the symbol table
11518 and the relocs. We set symcount to force assign_section_numbers
11519 to create a symbol table. */
8539e4e8 11520 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11521 BFD_ASSERT (! abfd->output_has_begun);
11522 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11523 goto error_return;
11524
ee75fd95 11525 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11526 for (o = abfd->sections; o != NULL; o = o->next)
11527 {
d4730f92 11528 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11529 if ((o->flags & SEC_RELOC) != 0)
11530 {
d4730f92 11531 if (esdo->rel.hdr
9eaff861 11532 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11533 goto error_return;
11534
d4730f92 11535 if (esdo->rela.hdr
9eaff861 11536 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11537 goto error_return;
11538 }
11539
11540 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11541 to count upwards while actually outputting the relocations. */
d4730f92
BS
11542 esdo->rel.count = 0;
11543 esdo->rela.count = 0;
0ce398f1
L
11544
11545 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11546 {
11547 /* Cache the section contents so that they can be compressed
11548 later. Use bfd_malloc since it will be freed by
11549 bfd_compress_section_contents. */
11550 unsigned char *contents = esdo->this_hdr.contents;
11551 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11552 abort ();
11553 contents
11554 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11555 if (contents == NULL)
11556 goto error_return;
11557 esdo->this_hdr.contents = contents;
11558 }
c152c796
AM
11559 }
11560
c152c796 11561 /* We have now assigned file positions for all the sections except
a485e98e
AM
11562 .symtab, .strtab, and non-loaded reloc sections. We start the
11563 .symtab section at the current file position, and write directly
11564 to it. We build the .strtab section in memory. */
c152c796
AM
11565 bfd_get_symcount (abfd) = 0;
11566 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11567 /* sh_name is set in prep_headers. */
11568 symtab_hdr->sh_type = SHT_SYMTAB;
11569 /* sh_flags, sh_addr and sh_size all start off zero. */
11570 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11571 /* sh_link is set in assign_section_numbers. */
11572 /* sh_info is set below. */
11573 /* sh_offset is set just below. */
72de5009 11574 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11575
ef10c3ac
L
11576 if (max_sym_count < 20)
11577 max_sym_count = 20;
64f52338 11578 htab->strtabsize = max_sym_count;
ef10c3ac 11579 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11580 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11581 if (htab->strtab == NULL)
c152c796 11582 goto error_return;
ef10c3ac
L
11583 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11584 flinfo.symshndxbuf
11585 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11586 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11587
8539e4e8 11588 if (info->strip != strip_all || emit_relocs)
c152c796 11589 {
8539e4e8
AM
11590 file_ptr off = elf_next_file_pos (abfd);
11591
11592 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11593
11594 /* Note that at this point elf_next_file_pos (abfd) is
11595 incorrect. We do not yet know the size of the .symtab section.
11596 We correct next_file_pos below, after we do know the size. */
11597
11598 /* Start writing out the symbol table. The first symbol is always a
11599 dummy symbol. */
c152c796
AM
11600 elfsym.st_value = 0;
11601 elfsym.st_size = 0;
11602 elfsym.st_info = 0;
11603 elfsym.st_other = 0;
11604 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11605 elfsym.st_target_internal = 0;
ef10c3ac
L
11606 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11607 bfd_und_section_ptr, NULL) != 1)
c152c796 11608 goto error_return;
c152c796 11609
8539e4e8
AM
11610 /* Output a symbol for each section. We output these even if we are
11611 discarding local symbols, since they are used for relocs. These
11612 symbols have no names. We store the index of each one in the
11613 index field of the section, so that we can find it again when
11614 outputting relocs. */
11615
c152c796
AM
11616 elfsym.st_size = 0;
11617 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11618 elfsym.st_other = 0;
f0b5bb34 11619 elfsym.st_value = 0;
35fc36a8 11620 elfsym.st_target_internal = 0;
c152c796
AM
11621 for (i = 1; i < elf_numsections (abfd); i++)
11622 {
11623 o = bfd_section_from_elf_index (abfd, i);
11624 if (o != NULL)
f0b5bb34
AM
11625 {
11626 o->target_index = bfd_get_symcount (abfd);
11627 elfsym.st_shndx = i;
0e1862bb 11628 if (!bfd_link_relocatable (info))
f0b5bb34 11629 elfsym.st_value = o->vma;
ef10c3ac
L
11630 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11631 NULL) != 1)
f0b5bb34
AM
11632 goto error_return;
11633 }
c152c796
AM
11634 }
11635 }
11636
11637 /* Allocate some memory to hold information read in from the input
11638 files. */
11639 if (max_contents_size != 0)
11640 {
8b127cbc
AM
11641 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11642 if (flinfo.contents == NULL)
c152c796
AM
11643 goto error_return;
11644 }
11645
11646 if (max_external_reloc_size != 0)
11647 {
8b127cbc
AM
11648 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11649 if (flinfo.external_relocs == NULL)
c152c796
AM
11650 goto error_return;
11651 }
11652
11653 if (max_internal_reloc_count != 0)
11654 {
11655 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11656 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11657 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11658 if (flinfo.internal_relocs == NULL)
c152c796
AM
11659 goto error_return;
11660 }
11661
11662 if (max_sym_count != 0)
11663 {
11664 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11665 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11666 if (flinfo.external_syms == NULL)
c152c796
AM
11667 goto error_return;
11668
11669 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11670 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11671 if (flinfo.internal_syms == NULL)
c152c796
AM
11672 goto error_return;
11673
11674 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11675 flinfo.indices = (long int *) bfd_malloc (amt);
11676 if (flinfo.indices == NULL)
c152c796
AM
11677 goto error_return;
11678
11679 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11680 flinfo.sections = (asection **) bfd_malloc (amt);
11681 if (flinfo.sections == NULL)
c152c796
AM
11682 goto error_return;
11683 }
11684
11685 if (max_sym_shndx_count != 0)
11686 {
11687 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11688 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11689 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11690 goto error_return;
11691 }
11692
64f52338 11693 if (htab->tls_sec)
c152c796
AM
11694 {
11695 bfd_vma base, end = 0;
11696 asection *sec;
11697
64f52338 11698 for (sec = htab->tls_sec;
c152c796
AM
11699 sec && (sec->flags & SEC_THREAD_LOCAL);
11700 sec = sec->next)
11701 {
3a800eb9 11702 bfd_size_type size = sec->size;
c152c796 11703
3a800eb9
AM
11704 if (size == 0
11705 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11706 {
91d6fa6a
NC
11707 struct bfd_link_order *ord = sec->map_tail.link_order;
11708
11709 if (ord != NULL)
11710 size = ord->offset + ord->size;
c152c796
AM
11711 }
11712 end = sec->vma + size;
11713 }
64f52338 11714 base = htab->tls_sec->vma;
7dc98aea
RO
11715 /* Only align end of TLS section if static TLS doesn't have special
11716 alignment requirements. */
11717 if (bed->static_tls_alignment == 1)
64f52338
AM
11718 end = align_power (end, htab->tls_sec->alignment_power);
11719 htab->tls_size = end - base;
c152c796
AM
11720 }
11721
0b52efa6
PB
11722 /* Reorder SHF_LINK_ORDER sections. */
11723 for (o = abfd->sections; o != NULL; o = o->next)
11724 {
11725 if (!elf_fixup_link_order (abfd, o))
11726 return FALSE;
11727 }
11728
2f0c68f2
CM
11729 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11730 return FALSE;
11731
c152c796
AM
11732 /* Since ELF permits relocations to be against local symbols, we
11733 must have the local symbols available when we do the relocations.
11734 Since we would rather only read the local symbols once, and we
11735 would rather not keep them in memory, we handle all the
11736 relocations for a single input file at the same time.
11737
11738 Unfortunately, there is no way to know the total number of local
11739 symbols until we have seen all of them, and the local symbol
11740 indices precede the global symbol indices. This means that when
11741 we are generating relocatable output, and we see a reloc against
11742 a global symbol, we can not know the symbol index until we have
11743 finished examining all the local symbols to see which ones we are
11744 going to output. To deal with this, we keep the relocations in
11745 memory, and don't output them until the end of the link. This is
11746 an unfortunate waste of memory, but I don't see a good way around
11747 it. Fortunately, it only happens when performing a relocatable
11748 link, which is not the common case. FIXME: If keep_memory is set
11749 we could write the relocs out and then read them again; I don't
11750 know how bad the memory loss will be. */
11751
c72f2fb2 11752 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11753 sub->output_has_begun = FALSE;
11754 for (o = abfd->sections; o != NULL; o = o->next)
11755 {
8423293d 11756 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11757 {
11758 if (p->type == bfd_indirect_link_order
11759 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11760 == bfd_target_elf_flavour)
11761 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11762 {
11763 if (! sub->output_has_begun)
11764 {
8b127cbc 11765 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11766 goto error_return;
11767 sub->output_has_begun = TRUE;
11768 }
11769 }
11770 else if (p->type == bfd_section_reloc_link_order
11771 || p->type == bfd_symbol_reloc_link_order)
11772 {
11773 if (! elf_reloc_link_order (abfd, info, o, p))
11774 goto error_return;
11775 }
11776 else
11777 {
11778 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11779 {
11780 if (p->type == bfd_indirect_link_order
11781 && (bfd_get_flavour (sub)
11782 == bfd_target_elf_flavour)
11783 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11784 != bed->s->elfclass))
11785 {
11786 const char *iclass, *oclass;
11787
aebf9be7 11788 switch (bed->s->elfclass)
351f65ca 11789 {
aebf9be7
NC
11790 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11791 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11792 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11793 default: abort ();
351f65ca 11794 }
aebf9be7
NC
11795
11796 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11797 {
aebf9be7
NC
11798 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11799 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11800 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11801 default: abort ();
351f65ca
L
11802 }
11803
11804 bfd_set_error (bfd_error_wrong_format);
4eca0228 11805 _bfd_error_handler
695344c0 11806 /* xgettext:c-format */
351f65ca
L
11807 (_("%B: file class %s incompatible with %s"),
11808 sub, iclass, oclass);
11809 }
11810
11811 goto error_return;
11812 }
c152c796
AM
11813 }
11814 }
11815 }
11816
c0f00686
L
11817 /* Free symbol buffer if needed. */
11818 if (!info->reduce_memory_overheads)
11819 {
c72f2fb2 11820 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11821 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11822 && elf_tdata (sub)->symbuf)
c0f00686
L
11823 {
11824 free (elf_tdata (sub)->symbuf);
11825 elf_tdata (sub)->symbuf = NULL;
11826 }
11827 }
11828
c152c796
AM
11829 /* Output any global symbols that got converted to local in a
11830 version script or due to symbol visibility. We do this in a
11831 separate step since ELF requires all local symbols to appear
11832 prior to any global symbols. FIXME: We should only do this if
11833 some global symbols were, in fact, converted to become local.
11834 FIXME: Will this work correctly with the Irix 5 linker? */
11835 eoinfo.failed = FALSE;
8b127cbc 11836 eoinfo.flinfo = &flinfo;
c152c796 11837 eoinfo.localsyms = TRUE;
34a79995 11838 eoinfo.file_sym_done = FALSE;
7686d77d 11839 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11840 if (eoinfo.failed)
11841 return FALSE;
11842
4e617b1e
PB
11843 /* If backend needs to output some local symbols not present in the hash
11844 table, do it now. */
8539e4e8
AM
11845 if (bed->elf_backend_output_arch_local_syms
11846 && (info->strip != strip_all || emit_relocs))
4e617b1e 11847 {
6e0b88f1 11848 typedef int (*out_sym_func)
4e617b1e
PB
11849 (void *, const char *, Elf_Internal_Sym *, asection *,
11850 struct elf_link_hash_entry *);
11851
11852 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11853 (abfd, info, &flinfo,
11854 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11855 return FALSE;
11856 }
11857
c152c796
AM
11858 /* That wrote out all the local symbols. Finish up the symbol table
11859 with the global symbols. Even if we want to strip everything we
11860 can, we still need to deal with those global symbols that got
11861 converted to local in a version script. */
11862
11863 /* The sh_info field records the index of the first non local symbol. */
11864 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11865
11866 if (dynamic
64f52338
AM
11867 && htab->dynsym != NULL
11868 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11869 {
11870 Elf_Internal_Sym sym;
64f52338 11871 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11872
64f52338
AM
11873 o = htab->dynsym->output_section;
11874 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11875
11876 /* Write out the section symbols for the output sections. */
0e1862bb 11877 if (bfd_link_pic (info)
64f52338 11878 || htab->is_relocatable_executable)
c152c796
AM
11879 {
11880 asection *s;
11881
11882 sym.st_size = 0;
11883 sym.st_name = 0;
11884 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11885 sym.st_other = 0;
35fc36a8 11886 sym.st_target_internal = 0;
c152c796
AM
11887
11888 for (s = abfd->sections; s != NULL; s = s->next)
11889 {
11890 int indx;
11891 bfd_byte *dest;
11892 long dynindx;
11893
c152c796 11894 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11895 if (dynindx <= 0)
11896 continue;
11897 indx = elf_section_data (s)->this_idx;
c152c796
AM
11898 BFD_ASSERT (indx > 0);
11899 sym.st_shndx = indx;
c0d5a53d
L
11900 if (! check_dynsym (abfd, &sym))
11901 return FALSE;
c152c796
AM
11902 sym.st_value = s->vma;
11903 dest = dynsym + dynindx * bed->s->sizeof_sym;
11904 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11905 }
c152c796
AM
11906 }
11907
11908 /* Write out the local dynsyms. */
64f52338 11909 if (htab->dynlocal)
c152c796
AM
11910 {
11911 struct elf_link_local_dynamic_entry *e;
64f52338 11912 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
11913 {
11914 asection *s;
11915 bfd_byte *dest;
11916
935bd1e0 11917 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11918 Note that we saved a word of storage and overwrote
11919 the original st_name with the dynstr_index. */
11920 sym = e->isym;
935bd1e0 11921 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11922
cb33740c
AM
11923 s = bfd_section_from_elf_index (e->input_bfd,
11924 e->isym.st_shndx);
11925 if (s != NULL)
c152c796 11926 {
c152c796
AM
11927 sym.st_shndx =
11928 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11929 if (! check_dynsym (abfd, &sym))
11930 return FALSE;
c152c796
AM
11931 sym.st_value = (s->output_section->vma
11932 + s->output_offset
11933 + e->isym.st_value);
11934 }
11935
c152c796
AM
11936 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11937 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11938 }
11939 }
c152c796
AM
11940 }
11941
11942 /* We get the global symbols from the hash table. */
11943 eoinfo.failed = FALSE;
11944 eoinfo.localsyms = FALSE;
8b127cbc 11945 eoinfo.flinfo = &flinfo;
7686d77d 11946 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11947 if (eoinfo.failed)
11948 return FALSE;
11949
11950 /* If backend needs to output some symbols not present in the hash
11951 table, do it now. */
8539e4e8
AM
11952 if (bed->elf_backend_output_arch_syms
11953 && (info->strip != strip_all || emit_relocs))
c152c796 11954 {
6e0b88f1 11955 typedef int (*out_sym_func)
c152c796
AM
11956 (void *, const char *, Elf_Internal_Sym *, asection *,
11957 struct elf_link_hash_entry *);
11958
11959 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11960 (abfd, info, &flinfo,
11961 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11962 return FALSE;
11963 }
11964
ef10c3ac
L
11965 /* Finalize the .strtab section. */
11966 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11967
11968 /* Swap out the .strtab section. */
11969 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11970 return FALSE;
11971
11972 /* Now we know the size of the symtab section. */
c152c796
AM
11973 if (bfd_get_symcount (abfd) > 0)
11974 {
ee3b52e9
L
11975 /* Finish up and write out the symbol string table (.strtab)
11976 section. */
11977 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11978 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11979
6a40cf0c
NC
11980 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11981 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11982 {
11983 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11984 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11985 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11986 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11987 symtab_shndx_hdr->sh_size = amt;
11988
11989 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11990 off, TRUE);
11991
11992 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11993 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11994 return FALSE;
11995 }
ee3b52e9
L
11996
11997 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11998 /* sh_name was set in prep_headers. */
11999 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12000 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12001 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12002 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12003 symstrtab_hdr->sh_entsize = 0;
12004 symstrtab_hdr->sh_link = 0;
12005 symstrtab_hdr->sh_info = 0;
12006 /* sh_offset is set just below. */
12007 symstrtab_hdr->sh_addralign = 1;
12008
12009 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12010 off, TRUE);
12011 elf_next_file_pos (abfd) = off;
12012
c152c796 12013 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12014 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12015 return FALSE;
12016 }
12017
76359541
TP
12018 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12019 {
4eca0228
AM
12020 _bfd_error_handler (_("%B: failed to generate import library"),
12021 info->out_implib_bfd);
76359541
TP
12022 return FALSE;
12023 }
12024
c152c796
AM
12025 /* Adjust the relocs to have the correct symbol indices. */
12026 for (o = abfd->sections; o != NULL; o = o->next)
12027 {
d4730f92 12028 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12029 bfd_boolean sort;
c152c796
AM
12030 if ((o->flags & SEC_RELOC) == 0)
12031 continue;
12032
28dbcedc 12033 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12034 if (esdo->rel.hdr != NULL
9eaff861 12035 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12036 return FALSE;
12037 if (esdo->rela.hdr != NULL
9eaff861 12038 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12039 return FALSE;
c152c796
AM
12040
12041 /* Set the reloc_count field to 0 to prevent write_relocs from
12042 trying to swap the relocs out itself. */
12043 o->reloc_count = 0;
12044 }
12045
12046 if (dynamic && info->combreloc && dynobj != NULL)
12047 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12048
12049 /* If we are linking against a dynamic object, or generating a
12050 shared library, finish up the dynamic linking information. */
12051 if (dynamic)
12052 {
12053 bfd_byte *dyncon, *dynconend;
12054
12055 /* Fix up .dynamic entries. */
3d4d4302 12056 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12057 BFD_ASSERT (o != NULL);
12058
12059 dyncon = o->contents;
eea6121a 12060 dynconend = o->contents + o->size;
c152c796
AM
12061 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12062 {
12063 Elf_Internal_Dyn dyn;
12064 const char *name;
12065 unsigned int type;
12066
12067 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12068
12069 switch (dyn.d_tag)
12070 {
12071 default:
12072 continue;
12073 case DT_NULL:
12074 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12075 {
12076 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12077 {
12078 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12079 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12080 default: continue;
12081 }
12082 dyn.d_un.d_val = relativecount;
12083 relativecount = 0;
12084 break;
12085 }
12086 continue;
12087
12088 case DT_INIT:
12089 name = info->init_function;
12090 goto get_sym;
12091 case DT_FINI:
12092 name = info->fini_function;
12093 get_sym:
12094 {
12095 struct elf_link_hash_entry *h;
12096
64f52338 12097 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12098 if (h != NULL
12099 && (h->root.type == bfd_link_hash_defined
12100 || h->root.type == bfd_link_hash_defweak))
12101 {
bef26483 12102 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12103 o = h->root.u.def.section;
12104 if (o->output_section != NULL)
bef26483 12105 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12106 + o->output_offset);
12107 else
12108 {
12109 /* The symbol is imported from another shared
12110 library and does not apply to this one. */
bef26483 12111 dyn.d_un.d_ptr = 0;
c152c796
AM
12112 }
12113 break;
12114 }
12115 }
12116 continue;
12117
12118 case DT_PREINIT_ARRAYSZ:
12119 name = ".preinit_array";
4ade44b7 12120 goto get_out_size;
c152c796
AM
12121 case DT_INIT_ARRAYSZ:
12122 name = ".init_array";
4ade44b7 12123 goto get_out_size;
c152c796
AM
12124 case DT_FINI_ARRAYSZ:
12125 name = ".fini_array";
4ade44b7 12126 get_out_size:
c152c796
AM
12127 o = bfd_get_section_by_name (abfd, name);
12128 if (o == NULL)
12129 {
4eca0228 12130 _bfd_error_handler
4ade44b7 12131 (_("could not find section %s"), name);
c152c796
AM
12132 goto error_return;
12133 }
eea6121a 12134 if (o->size == 0)
4eca0228 12135 _bfd_error_handler
c152c796 12136 (_("warning: %s section has zero size"), name);
eea6121a 12137 dyn.d_un.d_val = o->size;
c152c796
AM
12138 break;
12139
12140 case DT_PREINIT_ARRAY:
12141 name = ".preinit_array";
4ade44b7 12142 goto get_out_vma;
c152c796
AM
12143 case DT_INIT_ARRAY:
12144 name = ".init_array";
4ade44b7 12145 goto get_out_vma;
c152c796
AM
12146 case DT_FINI_ARRAY:
12147 name = ".fini_array";
4ade44b7
AM
12148 get_out_vma:
12149 o = bfd_get_section_by_name (abfd, name);
12150 goto do_vma;
c152c796
AM
12151
12152 case DT_HASH:
12153 name = ".hash";
12154 goto get_vma;
fdc90cb4
JJ
12155 case DT_GNU_HASH:
12156 name = ".gnu.hash";
12157 goto get_vma;
c152c796
AM
12158 case DT_STRTAB:
12159 name = ".dynstr";
12160 goto get_vma;
12161 case DT_SYMTAB:
12162 name = ".dynsym";
12163 goto get_vma;
12164 case DT_VERDEF:
12165 name = ".gnu.version_d";
12166 goto get_vma;
12167 case DT_VERNEED:
12168 name = ".gnu.version_r";
12169 goto get_vma;
12170 case DT_VERSYM:
12171 name = ".gnu.version";
12172 get_vma:
4ade44b7
AM
12173 o = bfd_get_linker_section (dynobj, name);
12174 do_vma:
c152c796
AM
12175 if (o == NULL)
12176 {
4eca0228 12177 _bfd_error_handler
4ade44b7 12178 (_("could not find section %s"), name);
c152c796
AM
12179 goto error_return;
12180 }
894891db
NC
12181 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12182 {
4eca0228 12183 _bfd_error_handler
894891db
NC
12184 (_("warning: section '%s' is being made into a note"), name);
12185 bfd_set_error (bfd_error_nonrepresentable_section);
12186 goto error_return;
12187 }
4ade44b7 12188 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12189 break;
12190
12191 case DT_REL:
12192 case DT_RELA:
12193 case DT_RELSZ:
12194 case DT_RELASZ:
12195 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12196 type = SHT_REL;
12197 else
12198 type = SHT_RELA;
12199 dyn.d_un.d_val = 0;
bef26483 12200 dyn.d_un.d_ptr = 0;
c152c796
AM
12201 for (i = 1; i < elf_numsections (abfd); i++)
12202 {
12203 Elf_Internal_Shdr *hdr;
12204
12205 hdr = elf_elfsections (abfd)[i];
12206 if (hdr->sh_type == type
12207 && (hdr->sh_flags & SHF_ALLOC) != 0)
12208 {
12209 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12210 dyn.d_un.d_val += hdr->sh_size;
12211 else
12212 {
bef26483
AM
12213 if (dyn.d_un.d_ptr == 0
12214 || hdr->sh_addr < dyn.d_un.d_ptr)
12215 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12216 }
12217 }
12218 }
64f52338
AM
12219 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12220 {
12221 /* Don't count procedure linkage table relocs in the
12222 overall reloc count. */
12223 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12224 dyn.d_un.d_val -= htab->srelplt->size;
12225 /* If .rela.plt is the first .rela section, exclude
12226 it from DT_RELA. */
12227 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12228 + htab->srelplt->output_offset))
12229 dyn.d_un.d_ptr += htab->srelplt->size;
12230 }
c152c796
AM
12231 break;
12232 }
12233 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12234 }
12235 }
12236
12237 /* If we have created any dynamic sections, then output them. */
12238 if (dynobj != NULL)
12239 {
12240 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12241 goto error_return;
12242
943284cc 12243 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12244 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12245 || info->error_textrel)
3d4d4302 12246 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12247 {
12248 bfd_byte *dyncon, *dynconend;
12249
943284cc
DJ
12250 dyncon = o->contents;
12251 dynconend = o->contents + o->size;
12252 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12253 {
12254 Elf_Internal_Dyn dyn;
12255
12256 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12257
12258 if (dyn.d_tag == DT_TEXTREL)
12259 {
c192a133
AM
12260 if (info->error_textrel)
12261 info->callbacks->einfo
12262 (_("%P%X: read-only segment has dynamic relocations.\n"));
12263 else
12264 info->callbacks->einfo
12265 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12266 break;
12267 }
12268 }
12269 }
12270
c152c796
AM
12271 for (o = dynobj->sections; o != NULL; o = o->next)
12272 {
12273 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12274 || o->size == 0
c152c796
AM
12275 || o->output_section == bfd_abs_section_ptr)
12276 continue;
12277 if ((o->flags & SEC_LINKER_CREATED) == 0)
12278 {
12279 /* At this point, we are only interested in sections
12280 created by _bfd_elf_link_create_dynamic_sections. */
12281 continue;
12282 }
64f52338 12283 if (htab->stab_info.stabstr == o)
3722b82f 12284 continue;
64f52338 12285 if (htab->eh_info.hdr_sec == o)
eea6121a 12286 continue;
3d4d4302 12287 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12288 {
12289 if (! bfd_set_section_contents (abfd, o->output_section,
12290 o->contents,
37b01f6a
DG
12291 (file_ptr) o->output_offset
12292 * bfd_octets_per_byte (abfd),
eea6121a 12293 o->size))
c152c796
AM
12294 goto error_return;
12295 }
12296 else
12297 {
12298 /* The contents of the .dynstr section are actually in a
12299 stringtab. */
8539e4e8
AM
12300 file_ptr off;
12301
c152c796
AM
12302 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12303 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12304 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12305 goto error_return;
12306 }
12307 }
12308 }
12309
0e1862bb 12310 if (bfd_link_relocatable (info))
c152c796
AM
12311 {
12312 bfd_boolean failed = FALSE;
12313
12314 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12315 if (failed)
12316 goto error_return;
12317 }
12318
12319 /* If we have optimized stabs strings, output them. */
64f52338 12320 if (htab->stab_info.stabstr != NULL)
c152c796 12321 {
64f52338 12322 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12323 goto error_return;
12324 }
12325
9f7c3e5e
AM
12326 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12327 goto error_return;
c152c796 12328
9f7c3e5e 12329 elf_final_link_free (abfd, &flinfo);
c152c796 12330
12bd6957 12331 elf_linker (abfd) = TRUE;
c152c796 12332
104d59d1
JM
12333 if (attr_section)
12334 {
a50b1753 12335 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12336 if (contents == NULL)
d0f16d5e 12337 return FALSE; /* Bail out and fail. */
104d59d1
JM
12338 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12339 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12340 free (contents);
12341 }
12342
c152c796
AM
12343 return TRUE;
12344
12345 error_return:
9f7c3e5e 12346 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12347 return FALSE;
12348}
12349\f
5241d853
RS
12350/* Initialize COOKIE for input bfd ABFD. */
12351
12352static bfd_boolean
12353init_reloc_cookie (struct elf_reloc_cookie *cookie,
12354 struct bfd_link_info *info, bfd *abfd)
12355{
12356 Elf_Internal_Shdr *symtab_hdr;
12357 const struct elf_backend_data *bed;
12358
12359 bed = get_elf_backend_data (abfd);
12360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12361
12362 cookie->abfd = abfd;
12363 cookie->sym_hashes = elf_sym_hashes (abfd);
12364 cookie->bad_symtab = elf_bad_symtab (abfd);
12365 if (cookie->bad_symtab)
12366 {
12367 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12368 cookie->extsymoff = 0;
12369 }
12370 else
12371 {
12372 cookie->locsymcount = symtab_hdr->sh_info;
12373 cookie->extsymoff = symtab_hdr->sh_info;
12374 }
12375
12376 if (bed->s->arch_size == 32)
12377 cookie->r_sym_shift = 8;
12378 else
12379 cookie->r_sym_shift = 32;
12380
12381 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12382 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12383 {
12384 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12385 cookie->locsymcount, 0,
12386 NULL, NULL, NULL);
12387 if (cookie->locsyms == NULL)
12388 {
12389 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12390 return FALSE;
12391 }
12392 if (info->keep_memory)
12393 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12394 }
12395 return TRUE;
12396}
12397
12398/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12399
12400static void
12401fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12402{
12403 Elf_Internal_Shdr *symtab_hdr;
12404
12405 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12406 if (cookie->locsyms != NULL
12407 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12408 free (cookie->locsyms);
12409}
12410
12411/* Initialize the relocation information in COOKIE for input section SEC
12412 of input bfd ABFD. */
12413
12414static bfd_boolean
12415init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12416 struct bfd_link_info *info, bfd *abfd,
12417 asection *sec)
12418{
12419 const struct elf_backend_data *bed;
12420
12421 if (sec->reloc_count == 0)
12422 {
12423 cookie->rels = NULL;
12424 cookie->relend = NULL;
12425 }
12426 else
12427 {
12428 bed = get_elf_backend_data (abfd);
12429
12430 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12431 info->keep_memory);
12432 if (cookie->rels == NULL)
12433 return FALSE;
12434 cookie->rel = cookie->rels;
12435 cookie->relend = (cookie->rels
12436 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12437 }
12438 cookie->rel = cookie->rels;
12439 return TRUE;
12440}
12441
12442/* Free the memory allocated by init_reloc_cookie_rels,
12443 if appropriate. */
12444
12445static void
12446fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12447 asection *sec)
12448{
12449 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12450 free (cookie->rels);
12451}
12452
12453/* Initialize the whole of COOKIE for input section SEC. */
12454
12455static bfd_boolean
12456init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12457 struct bfd_link_info *info,
12458 asection *sec)
12459{
12460 if (!init_reloc_cookie (cookie, info, sec->owner))
12461 goto error1;
12462 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12463 goto error2;
12464 return TRUE;
12465
12466 error2:
12467 fini_reloc_cookie (cookie, sec->owner);
12468 error1:
12469 return FALSE;
12470}
12471
12472/* Free the memory allocated by init_reloc_cookie_for_section,
12473 if appropriate. */
12474
12475static void
12476fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12477 asection *sec)
12478{
12479 fini_reloc_cookie_rels (cookie, sec);
12480 fini_reloc_cookie (cookie, sec->owner);
12481}
12482\f
c152c796
AM
12483/* Garbage collect unused sections. */
12484
07adf181
AM
12485/* Default gc_mark_hook. */
12486
12487asection *
12488_bfd_elf_gc_mark_hook (asection *sec,
12489 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12490 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12491 struct elf_link_hash_entry *h,
12492 Elf_Internal_Sym *sym)
12493{
12494 if (h != NULL)
12495 {
12496 switch (h->root.type)
12497 {
12498 case bfd_link_hash_defined:
12499 case bfd_link_hash_defweak:
12500 return h->root.u.def.section;
12501
12502 case bfd_link_hash_common:
12503 return h->root.u.c.p->section;
12504
12505 default:
12506 break;
12507 }
12508 }
12509 else
12510 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12511
12512 return NULL;
12513}
12514
a6a4679f
AM
12515/* For undefined __start_<name> and __stop_<name> symbols, return the
12516 first input section matching <name>. Return NULL otherwise. */
12517
12518asection *
12519_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12520 struct elf_link_hash_entry *h)
12521{
12522 asection *s;
12523 const char *sec_name;
12524
12525 if (h->root.type != bfd_link_hash_undefined
12526 && h->root.type != bfd_link_hash_undefweak)
12527 return NULL;
12528
12529 s = h->root.u.undef.section;
12530 if (s != NULL)
12531 {
12532 if (s == (asection *) 0 - 1)
12533 return NULL;
12534 return s;
12535 }
12536
12537 sec_name = NULL;
12538 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12539 sec_name = h->root.root.string + 8;
12540 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12541 sec_name = h->root.root.string + 7;
12542
12543 if (sec_name != NULL && *sec_name != '\0')
12544 {
12545 bfd *i;
12546
12547 for (i = info->input_bfds; i != NULL; i = i->link.next)
12548 {
12549 s = bfd_get_section_by_name (i, sec_name);
12550 if (s != NULL)
12551 {
12552 h->root.u.undef.section = s;
12553 break;
12554 }
12555 }
12556 }
12557
12558 if (s == NULL)
12559 h->root.u.undef.section = (asection *) 0 - 1;
12560
12561 return s;
12562}
12563
5241d853
RS
12564/* COOKIE->rel describes a relocation against section SEC, which is
12565 a section we've decided to keep. Return the section that contains
12566 the relocation symbol, or NULL if no section contains it. */
12567
12568asection *
12569_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12570 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12571 struct elf_reloc_cookie *cookie,
12572 bfd_boolean *start_stop)
5241d853
RS
12573{
12574 unsigned long r_symndx;
12575 struct elf_link_hash_entry *h;
12576
12577 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12578 if (r_symndx == STN_UNDEF)
5241d853
RS
12579 return NULL;
12580
12581 if (r_symndx >= cookie->locsymcount
12582 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12583 {
12584 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12585 if (h == NULL)
12586 {
12587 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12588 sec->owner);
12589 return NULL;
12590 }
5241d853
RS
12591 while (h->root.type == bfd_link_hash_indirect
12592 || h->root.type == bfd_link_hash_warning)
12593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12594 h->mark = 1;
4e6b54a6
AM
12595 /* If this symbol is weak and there is a non-weak definition, we
12596 keep the non-weak definition because many backends put
12597 dynamic reloc info on the non-weak definition for code
12598 handling copy relocs. */
12599 if (h->u.weakdef != NULL)
12600 h->u.weakdef->mark = 1;
1cce69b9 12601
a6a4679f 12602 if (start_stop != NULL)
1cce69b9
AM
12603 {
12604 /* To work around a glibc bug, mark all XXX input sections
12605 when there is an as yet undefined reference to __start_XXX
12606 or __stop_XXX symbols. The linker will later define such
12607 symbols for orphan input sections that have a name
12608 representable as a C identifier. */
a6a4679f 12609 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12610
a6a4679f 12611 if (s != NULL)
1cce69b9 12612 {
a6a4679f
AM
12613 *start_stop = !s->gc_mark;
12614 return s;
1cce69b9
AM
12615 }
12616 }
12617
5241d853
RS
12618 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12619 }
12620
12621 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12622 &cookie->locsyms[r_symndx]);
12623}
12624
12625/* COOKIE->rel describes a relocation against section SEC, which is
12626 a section we've decided to keep. Mark the section that contains
9d0a14d3 12627 the relocation symbol. */
5241d853
RS
12628
12629bfd_boolean
12630_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12631 asection *sec,
12632 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12633 struct elf_reloc_cookie *cookie)
5241d853
RS
12634{
12635 asection *rsec;
1cce69b9 12636 bfd_boolean start_stop = FALSE;
5241d853 12637
1cce69b9
AM
12638 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12639 while (rsec != NULL)
5241d853 12640 {
1cce69b9
AM
12641 if (!rsec->gc_mark)
12642 {
12643 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12644 || (rsec->owner->flags & DYNAMIC) != 0)
12645 rsec->gc_mark = 1;
12646 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12647 return FALSE;
12648 }
12649 if (!start_stop)
12650 break;
199af150 12651 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12652 }
12653 return TRUE;
12654}
12655
07adf181
AM
12656/* The mark phase of garbage collection. For a given section, mark
12657 it and any sections in this section's group, and all the sections
12658 which define symbols to which it refers. */
12659
ccfa59ea
AM
12660bfd_boolean
12661_bfd_elf_gc_mark (struct bfd_link_info *info,
12662 asection *sec,
6a5bb875 12663 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12664{
12665 bfd_boolean ret;
9d0a14d3 12666 asection *group_sec, *eh_frame;
c152c796
AM
12667
12668 sec->gc_mark = 1;
12669
12670 /* Mark all the sections in the group. */
12671 group_sec = elf_section_data (sec)->next_in_group;
12672 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12673 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12674 return FALSE;
12675
12676 /* Look through the section relocs. */
12677 ret = TRUE;
9d0a14d3
RS
12678 eh_frame = elf_eh_frame_section (sec->owner);
12679 if ((sec->flags & SEC_RELOC) != 0
12680 && sec->reloc_count > 0
12681 && sec != eh_frame)
c152c796 12682 {
5241d853 12683 struct elf_reloc_cookie cookie;
c152c796 12684
5241d853
RS
12685 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12686 ret = FALSE;
c152c796 12687 else
c152c796 12688 {
5241d853 12689 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12690 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12691 {
12692 ret = FALSE;
12693 break;
12694 }
12695 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12696 }
12697 }
9d0a14d3
RS
12698
12699 if (ret && eh_frame && elf_fde_list (sec))
12700 {
12701 struct elf_reloc_cookie cookie;
12702
12703 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12704 ret = FALSE;
12705 else
12706 {
12707 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12708 gc_mark_hook, &cookie))
12709 ret = FALSE;
12710 fini_reloc_cookie_for_section (&cookie, eh_frame);
12711 }
12712 }
12713
2f0c68f2
CM
12714 eh_frame = elf_section_eh_frame_entry (sec);
12715 if (ret && eh_frame && !eh_frame->gc_mark)
12716 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12717 ret = FALSE;
12718
c152c796
AM
12719 return ret;
12720}
12721
3c758495
TG
12722/* Scan and mark sections in a special or debug section group. */
12723
12724static void
12725_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12726{
12727 /* Point to first section of section group. */
12728 asection *ssec;
12729 /* Used to iterate the section group. */
12730 asection *msec;
12731
12732 bfd_boolean is_special_grp = TRUE;
12733 bfd_boolean is_debug_grp = TRUE;
12734
12735 /* First scan to see if group contains any section other than debug
12736 and special section. */
12737 ssec = msec = elf_next_in_group (grp);
12738 do
12739 {
12740 if ((msec->flags & SEC_DEBUGGING) == 0)
12741 is_debug_grp = FALSE;
12742
12743 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12744 is_special_grp = FALSE;
12745
12746 msec = elf_next_in_group (msec);
12747 }
12748 while (msec != ssec);
12749
12750 /* If this is a pure debug section group or pure special section group,
12751 keep all sections in this group. */
12752 if (is_debug_grp || is_special_grp)
12753 {
12754 do
12755 {
12756 msec->gc_mark = 1;
12757 msec = elf_next_in_group (msec);
12758 }
12759 while (msec != ssec);
12760 }
12761}
12762
7f6ab9f8
AM
12763/* Keep debug and special sections. */
12764
12765bfd_boolean
12766_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12767 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12768{
12769 bfd *ibfd;
12770
c72f2fb2 12771 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12772 {
12773 asection *isec;
12774 bfd_boolean some_kept;
b40bf0a2 12775 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12776
12777 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12778 continue;
12779
b40bf0a2
NC
12780 /* Ensure all linker created sections are kept,
12781 see if any other section is already marked,
12782 and note if we have any fragmented debug sections. */
12783 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12784 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12785 {
12786 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12787 isec->gc_mark = 1;
12788 else if (isec->gc_mark)
12789 some_kept = TRUE;
b40bf0a2
NC
12790
12791 if (debug_frag_seen == FALSE
12792 && (isec->flags & SEC_DEBUGGING)
12793 && CONST_STRNEQ (isec->name, ".debug_line."))
12794 debug_frag_seen = TRUE;
7f6ab9f8
AM
12795 }
12796
12797 /* If no section in this file will be kept, then we can
b40bf0a2 12798 toss out the debug and special sections. */
7f6ab9f8
AM
12799 if (!some_kept)
12800 continue;
12801
12802 /* Keep debug and special sections like .comment when they are
3c758495
TG
12803 not part of a group. Also keep section groups that contain
12804 just debug sections or special sections. */
7f6ab9f8 12805 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12806 {
12807 if ((isec->flags & SEC_GROUP) != 0)
12808 _bfd_elf_gc_mark_debug_special_section_group (isec);
12809 else if (((isec->flags & SEC_DEBUGGING) != 0
12810 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12811 && elf_next_in_group (isec) == NULL)
12812 isec->gc_mark = 1;
12813 }
b40bf0a2
NC
12814
12815 if (! debug_frag_seen)
12816 continue;
12817
12818 /* Look for CODE sections which are going to be discarded,
12819 and find and discard any fragmented debug sections which
12820 are associated with that code section. */
12821 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12822 if ((isec->flags & SEC_CODE) != 0
12823 && isec->gc_mark == 0)
12824 {
12825 unsigned int ilen;
12826 asection *dsec;
12827
12828 ilen = strlen (isec->name);
12829
12830 /* Association is determined by the name of the debug section
12831 containing the name of the code section as a suffix. For
12832 example .debug_line.text.foo is a debug section associated
12833 with .text.foo. */
12834 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12835 {
12836 unsigned int dlen;
12837
12838 if (dsec->gc_mark == 0
12839 || (dsec->flags & SEC_DEBUGGING) == 0)
12840 continue;
12841
12842 dlen = strlen (dsec->name);
12843
12844 if (dlen > ilen
12845 && strncmp (dsec->name + (dlen - ilen),
12846 isec->name, ilen) == 0)
12847 {
12848 dsec->gc_mark = 0;
b40bf0a2
NC
12849 }
12850 }
12851 }
7f6ab9f8
AM
12852 }
12853 return TRUE;
12854}
12855
c152c796
AM
12856/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12857
c17d87de
NC
12858struct elf_gc_sweep_symbol_info
12859{
ccabcbe5
AM
12860 struct bfd_link_info *info;
12861 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12862 bfd_boolean);
12863};
12864
c152c796 12865static bfd_boolean
ccabcbe5 12866elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12867{
1d5316ab
AM
12868 if (!h->mark
12869 && (((h->root.type == bfd_link_hash_defined
12870 || h->root.type == bfd_link_hash_defweak)
c4621b33 12871 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12872 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12873 || h->root.type == bfd_link_hash_undefined
12874 || h->root.type == bfd_link_hash_undefweak))
12875 {
12876 struct elf_gc_sweep_symbol_info *inf;
12877
12878 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12879 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12880 h->def_regular = 0;
12881 h->ref_regular = 0;
12882 h->ref_regular_nonweak = 0;
ccabcbe5 12883 }
c152c796
AM
12884
12885 return TRUE;
12886}
12887
12888/* The sweep phase of garbage collection. Remove all garbage sections. */
12889
12890typedef bfd_boolean (*gc_sweep_hook_fn)
12891 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12892
12893static bfd_boolean
ccabcbe5 12894elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12895{
12896 bfd *sub;
ccabcbe5
AM
12897 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12898 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12899 unsigned long section_sym_count;
12900 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12901
c72f2fb2 12902 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12903 {
12904 asection *o;
12905
b19a8f85
L
12906 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12907 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12908 continue;
12909
12910 for (o = sub->sections; o != NULL; o = o->next)
12911 {
a33dafc3
L
12912 /* When any section in a section group is kept, we keep all
12913 sections in the section group. If the first member of
12914 the section group is excluded, we will also exclude the
12915 group section. */
12916 if (o->flags & SEC_GROUP)
12917 {
12918 asection *first = elf_next_in_group (o);
12919 o->gc_mark = first->gc_mark;
12920 }
c152c796 12921
1e7eae0d 12922 if (o->gc_mark)
c152c796
AM
12923 continue;
12924
12925 /* Skip sweeping sections already excluded. */
12926 if (o->flags & SEC_EXCLUDE)
12927 continue;
12928
12929 /* Since this is early in the link process, it is simple
12930 to remove a section from the output. */
12931 o->flags |= SEC_EXCLUDE;
12932
c55fe096 12933 if (info->print_gc_sections && o->size != 0)
695344c0 12934 /* xgettext:c-format */
c17d87de
NC
12935 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12936
c152c796
AM
12937 /* But we also have to update some of the relocation
12938 info we collected before. */
12939 if (gc_sweep_hook
e8aaee2a 12940 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12941 && o->reloc_count != 0
12942 && !((info->strip == strip_all || info->strip == strip_debugger)
12943 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12944 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12945 {
12946 Elf_Internal_Rela *internal_relocs;
12947 bfd_boolean r;
12948
12949 internal_relocs
12950 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12951 info->keep_memory);
12952 if (internal_relocs == NULL)
12953 return FALSE;
12954
12955 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12956
12957 if (elf_section_data (o)->relocs != internal_relocs)
12958 free (internal_relocs);
12959
12960 if (!r)
12961 return FALSE;
12962 }
12963 }
12964 }
12965
12966 /* Remove the symbols that were in the swept sections from the dynamic
12967 symbol table. GCFIXME: Anyone know how to get them out of the
12968 static symbol table as well? */
ccabcbe5
AM
12969 sweep_info.info = info;
12970 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12971 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12972 &sweep_info);
c152c796 12973
ccabcbe5 12974 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12975 return TRUE;
12976}
12977
12978/* Propagate collected vtable information. This is called through
12979 elf_link_hash_traverse. */
12980
12981static bfd_boolean
12982elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12983{
c152c796 12984 /* Those that are not vtables. */
f6e332e6 12985 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12986 return TRUE;
12987
12988 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12989 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12990 return TRUE;
12991
12992 /* If we've already been done, exit. */
f6e332e6 12993 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12994 return TRUE;
12995
12996 /* Make sure the parent's table is up to date. */
f6e332e6 12997 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12998
f6e332e6 12999 if (h->vtable->used == NULL)
c152c796
AM
13000 {
13001 /* None of this table's entries were referenced. Re-use the
13002 parent's table. */
f6e332e6
AM
13003 h->vtable->used = h->vtable->parent->vtable->used;
13004 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13005 }
13006 else
13007 {
13008 size_t n;
13009 bfd_boolean *cu, *pu;
13010
13011 /* Or the parent's entries into ours. */
f6e332e6 13012 cu = h->vtable->used;
c152c796 13013 cu[-1] = TRUE;
f6e332e6 13014 pu = h->vtable->parent->vtable->used;
c152c796
AM
13015 if (pu != NULL)
13016 {
13017 const struct elf_backend_data *bed;
13018 unsigned int log_file_align;
13019
13020 bed = get_elf_backend_data (h->root.u.def.section->owner);
13021 log_file_align = bed->s->log_file_align;
f6e332e6 13022 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13023 while (n--)
13024 {
13025 if (*pu)
13026 *cu = TRUE;
13027 pu++;
13028 cu++;
13029 }
13030 }
13031 }
13032
13033 return TRUE;
13034}
13035
13036static bfd_boolean
13037elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13038{
13039 asection *sec;
13040 bfd_vma hstart, hend;
13041 Elf_Internal_Rela *relstart, *relend, *rel;
13042 const struct elf_backend_data *bed;
13043 unsigned int log_file_align;
13044
c152c796
AM
13045 /* Take care of both those symbols that do not describe vtables as
13046 well as those that are not loaded. */
f6e332e6 13047 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13048 return TRUE;
13049
13050 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13051 || h->root.type == bfd_link_hash_defweak);
13052
13053 sec = h->root.u.def.section;
13054 hstart = h->root.u.def.value;
13055 hend = hstart + h->size;
13056
13057 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13058 if (!relstart)
13059 return *(bfd_boolean *) okp = FALSE;
13060 bed = get_elf_backend_data (sec->owner);
13061 log_file_align = bed->s->log_file_align;
13062
13063 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13064
13065 for (rel = relstart; rel < relend; ++rel)
13066 if (rel->r_offset >= hstart && rel->r_offset < hend)
13067 {
13068 /* If the entry is in use, do nothing. */
f6e332e6
AM
13069 if (h->vtable->used
13070 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13071 {
13072 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13073 if (h->vtable->used[entry])
c152c796
AM
13074 continue;
13075 }
13076 /* Otherwise, kill it. */
13077 rel->r_offset = rel->r_info = rel->r_addend = 0;
13078 }
13079
13080 return TRUE;
13081}
13082
87538722
AM
13083/* Mark sections containing dynamically referenced symbols. When
13084 building shared libraries, we must assume that any visible symbol is
13085 referenced. */
715df9b8 13086
64d03ab5
AM
13087bfd_boolean
13088bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13089{
87538722 13090 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13091 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13092
715df9b8
EB
13093 if ((h->root.type == bfd_link_hash_defined
13094 || h->root.type == bfd_link_hash_defweak)
87538722 13095 && (h->ref_dynamic
c4621b33 13096 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13097 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13098 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13099 && (!bfd_link_executable (info)
22185505 13100 || info->gc_keep_exported
b407645f
AM
13101 || info->export_dynamic
13102 || (h->dynamic
13103 && d != NULL
13104 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13105 && (h->versioned >= versioned
54e8959c
L
13106 || !bfd_hide_sym_by_version (info->version_info,
13107 h->root.root.string)))))
715df9b8
EB
13108 h->root.u.def.section->flags |= SEC_KEEP;
13109
13110 return TRUE;
13111}
3b36f7e6 13112
74f0fb50
AM
13113/* Keep all sections containing symbols undefined on the command-line,
13114 and the section containing the entry symbol. */
13115
13116void
13117_bfd_elf_gc_keep (struct bfd_link_info *info)
13118{
13119 struct bfd_sym_chain *sym;
13120
13121 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13122 {
13123 struct elf_link_hash_entry *h;
13124
13125 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13126 FALSE, FALSE, FALSE);
13127
13128 if (h != NULL
13129 && (h->root.type == bfd_link_hash_defined
13130 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13131 && !bfd_is_abs_section (h->root.u.def.section)
13132 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13133 h->root.u.def.section->flags |= SEC_KEEP;
13134 }
13135}
13136
2f0c68f2
CM
13137bfd_boolean
13138bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13139 struct bfd_link_info *info)
13140{
13141 bfd *ibfd = info->input_bfds;
13142
13143 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13144 {
13145 asection *sec;
13146 struct elf_reloc_cookie cookie;
13147
13148 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13149 continue;
13150
13151 if (!init_reloc_cookie (&cookie, info, ibfd))
13152 return FALSE;
13153
13154 for (sec = ibfd->sections; sec; sec = sec->next)
13155 {
13156 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13157 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13158 {
13159 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13160 fini_reloc_cookie_rels (&cookie, sec);
13161 }
13162 }
13163 }
13164 return TRUE;
13165}
13166
c152c796
AM
13167/* Do mark and sweep of unused sections. */
13168
13169bfd_boolean
13170bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13171{
13172 bfd_boolean ok = TRUE;
13173 bfd *sub;
6a5bb875 13174 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13175 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13176 struct elf_link_hash_table *htab;
c152c796 13177
64d03ab5 13178 if (!bed->can_gc_sections
715df9b8 13179 || !is_elf_hash_table (info->hash))
c152c796 13180 {
4eca0228 13181 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13182 return TRUE;
13183 }
13184
74f0fb50 13185 bed->gc_keep (info);
da44f4e5 13186 htab = elf_hash_table (info);
74f0fb50 13187
9d0a14d3
RS
13188 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13189 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13190 for (sub = info->input_bfds;
13191 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13192 sub = sub->link.next)
9d0a14d3
RS
13193 {
13194 asection *sec;
13195 struct elf_reloc_cookie cookie;
13196
13197 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13198 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13199 {
13200 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13201 if (elf_section_data (sec)->sec_info
13202 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13203 elf_eh_frame_section (sub) = sec;
13204 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13205 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13206 }
13207 }
9d0a14d3 13208
c152c796 13209 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13210 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13211 if (!ok)
13212 return FALSE;
13213
13214 /* Kill the vtable relocations that were not used. */
da44f4e5 13215 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13216 if (!ok)
13217 return FALSE;
13218
715df9b8 13219 /* Mark dynamically referenced symbols. */
22185505 13220 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13221 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13222
715df9b8 13223 /* Grovel through relocs to find out who stays ... */
64d03ab5 13224 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13225 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13226 {
13227 asection *o;
13228
b19a8f85
L
13229 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13230 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13231 continue;
13232
7f6ab9f8
AM
13233 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13234 Also treat note sections as a root, if the section is not part
13235 of a group. */
c152c796 13236 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13237 if (!o->gc_mark
13238 && (o->flags & SEC_EXCLUDE) == 0
24007750 13239 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13240 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13241 && elf_next_in_group (o) == NULL )))
13242 {
13243 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13244 return FALSE;
13245 }
c152c796
AM
13246 }
13247
6a5bb875 13248 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13249 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13250
c152c796 13251 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13252 return elf_gc_sweep (abfd, info);
c152c796
AM
13253}
13254\f
13255/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13256
13257bfd_boolean
13258bfd_elf_gc_record_vtinherit (bfd *abfd,
13259 asection *sec,
13260 struct elf_link_hash_entry *h,
13261 bfd_vma offset)
13262{
13263 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13264 struct elf_link_hash_entry **search, *child;
ef53be89 13265 size_t extsymcount;
c152c796
AM
13266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13267
13268 /* The sh_info field of the symtab header tells us where the
13269 external symbols start. We don't care about the local symbols at
13270 this point. */
13271 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13272 if (!elf_bad_symtab (abfd))
13273 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13274
13275 sym_hashes = elf_sym_hashes (abfd);
13276 sym_hashes_end = sym_hashes + extsymcount;
13277
13278 /* Hunt down the child symbol, which is in this section at the same
13279 offset as the relocation. */
13280 for (search = sym_hashes; search != sym_hashes_end; ++search)
13281 {
13282 if ((child = *search) != NULL
13283 && (child->root.type == bfd_link_hash_defined
13284 || child->root.type == bfd_link_hash_defweak)
13285 && child->root.u.def.section == sec
13286 && child->root.u.def.value == offset)
13287 goto win;
13288 }
13289
695344c0
NC
13290 /* xgettext:c-format */
13291 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13292 abfd, sec, (unsigned long) offset);
c152c796
AM
13293 bfd_set_error (bfd_error_invalid_operation);
13294 return FALSE;
13295
13296 win:
f6e332e6
AM
13297 if (!child->vtable)
13298 {
ca4be51c
AM
13299 child->vtable = ((struct elf_link_virtual_table_entry *)
13300 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13301 if (!child->vtable)
13302 return FALSE;
13303 }
c152c796
AM
13304 if (!h)
13305 {
13306 /* This *should* only be the absolute section. It could potentially
13307 be that someone has defined a non-global vtable though, which
13308 would be bad. It isn't worth paging in the local symbols to be
13309 sure though; that case should simply be handled by the assembler. */
13310
f6e332e6 13311 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13312 }
13313 else
f6e332e6 13314 child->vtable->parent = h;
c152c796
AM
13315
13316 return TRUE;
13317}
13318
13319/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13320
13321bfd_boolean
13322bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13323 asection *sec ATTRIBUTE_UNUSED,
13324 struct elf_link_hash_entry *h,
13325 bfd_vma addend)
13326{
13327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13328 unsigned int log_file_align = bed->s->log_file_align;
13329
f6e332e6
AM
13330 if (!h->vtable)
13331 {
ca4be51c
AM
13332 h->vtable = ((struct elf_link_virtual_table_entry *)
13333 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13334 if (!h->vtable)
13335 return FALSE;
13336 }
13337
13338 if (addend >= h->vtable->size)
c152c796
AM
13339 {
13340 size_t size, bytes, file_align;
f6e332e6 13341 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13342
13343 /* While the symbol is undefined, we have to be prepared to handle
13344 a zero size. */
13345 file_align = 1 << log_file_align;
13346 if (h->root.type == bfd_link_hash_undefined)
13347 size = addend + file_align;
13348 else
13349 {
13350 size = h->size;
13351 if (addend >= size)
13352 {
13353 /* Oops! We've got a reference past the defined end of
13354 the table. This is probably a bug -- shall we warn? */
13355 size = addend + file_align;
13356 }
13357 }
13358 size = (size + file_align - 1) & -file_align;
13359
13360 /* Allocate one extra entry for use as a "done" flag for the
13361 consolidation pass. */
13362 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13363
13364 if (ptr)
13365 {
a50b1753 13366 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13367
13368 if (ptr != NULL)
13369 {
13370 size_t oldbytes;
13371
f6e332e6 13372 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13373 * sizeof (bfd_boolean));
13374 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13375 }
13376 }
13377 else
a50b1753 13378 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13379
13380 if (ptr == NULL)
13381 return FALSE;
13382
13383 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13384 h->vtable->used = ptr + 1;
13385 h->vtable->size = size;
c152c796
AM
13386 }
13387
f6e332e6 13388 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13389
13390 return TRUE;
13391}
13392
ae17ab41
CM
13393/* Map an ELF section header flag to its corresponding string. */
13394typedef struct
13395{
13396 char *flag_name;
13397 flagword flag_value;
13398} elf_flags_to_name_table;
13399
13400static elf_flags_to_name_table elf_flags_to_names [] =
13401{
13402 { "SHF_WRITE", SHF_WRITE },
13403 { "SHF_ALLOC", SHF_ALLOC },
13404 { "SHF_EXECINSTR", SHF_EXECINSTR },
13405 { "SHF_MERGE", SHF_MERGE },
13406 { "SHF_STRINGS", SHF_STRINGS },
13407 { "SHF_INFO_LINK", SHF_INFO_LINK},
13408 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13409 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13410 { "SHF_GROUP", SHF_GROUP },
13411 { "SHF_TLS", SHF_TLS },
13412 { "SHF_MASKOS", SHF_MASKOS },
13413 { "SHF_EXCLUDE", SHF_EXCLUDE },
13414};
13415
b9c361e0
JL
13416/* Returns TRUE if the section is to be included, otherwise FALSE. */
13417bfd_boolean
ae17ab41 13418bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13419 struct flag_info *flaginfo,
b9c361e0 13420 asection *section)
ae17ab41 13421{
8b127cbc 13422 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13423
8b127cbc 13424 if (!flaginfo->flags_initialized)
ae17ab41 13425 {
8b127cbc
AM
13426 bfd *obfd = info->output_bfd;
13427 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13428 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13429 int with_hex = 0;
13430 int without_hex = 0;
13431
8b127cbc 13432 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13433 {
b9c361e0 13434 unsigned i;
8b127cbc 13435 flagword (*lookup) (char *);
ae17ab41 13436
8b127cbc
AM
13437 lookup = bed->elf_backend_lookup_section_flags_hook;
13438 if (lookup != NULL)
ae17ab41 13439 {
8b127cbc 13440 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13441
13442 if (hexval != 0)
13443 {
13444 if (tf->with == with_flags)
13445 with_hex |= hexval;
13446 else if (tf->with == without_flags)
13447 without_hex |= hexval;
13448 tf->valid = TRUE;
13449 continue;
13450 }
ae17ab41 13451 }
8b127cbc 13452 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13453 {
8b127cbc 13454 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13455 {
13456 if (tf->with == with_flags)
13457 with_hex |= elf_flags_to_names[i].flag_value;
13458 else if (tf->with == without_flags)
13459 without_hex |= elf_flags_to_names[i].flag_value;
13460 tf->valid = TRUE;
13461 break;
13462 }
13463 }
8b127cbc 13464 if (!tf->valid)
b9c361e0 13465 {
68ffbac6 13466 info->callbacks->einfo
8b127cbc 13467 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13468 return FALSE;
ae17ab41
CM
13469 }
13470 }
8b127cbc
AM
13471 flaginfo->flags_initialized = TRUE;
13472 flaginfo->only_with_flags |= with_hex;
13473 flaginfo->not_with_flags |= without_hex;
ae17ab41 13474 }
ae17ab41 13475
8b127cbc 13476 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13477 return FALSE;
13478
8b127cbc 13479 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13480 return FALSE;
13481
13482 return TRUE;
ae17ab41
CM
13483}
13484
c152c796
AM
13485struct alloc_got_off_arg {
13486 bfd_vma gotoff;
10455f89 13487 struct bfd_link_info *info;
c152c796
AM
13488};
13489
13490/* We need a special top-level link routine to convert got reference counts
13491 to real got offsets. */
13492
13493static bfd_boolean
13494elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13495{
a50b1753 13496 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13497 bfd *obfd = gofarg->info->output_bfd;
13498 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13499
c152c796
AM
13500 if (h->got.refcount > 0)
13501 {
13502 h->got.offset = gofarg->gotoff;
10455f89 13503 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13504 }
13505 else
13506 h->got.offset = (bfd_vma) -1;
13507
13508 return TRUE;
13509}
13510
13511/* And an accompanying bit to work out final got entry offsets once
13512 we're done. Should be called from final_link. */
13513
13514bfd_boolean
13515bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13516 struct bfd_link_info *info)
13517{
13518 bfd *i;
13519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13520 bfd_vma gotoff;
c152c796
AM
13521 struct alloc_got_off_arg gofarg;
13522
10455f89
HPN
13523 BFD_ASSERT (abfd == info->output_bfd);
13524
c152c796
AM
13525 if (! is_elf_hash_table (info->hash))
13526 return FALSE;
13527
13528 /* The GOT offset is relative to the .got section, but the GOT header is
13529 put into the .got.plt section, if the backend uses it. */
13530 if (bed->want_got_plt)
13531 gotoff = 0;
13532 else
13533 gotoff = bed->got_header_size;
13534
13535 /* Do the local .got entries first. */
c72f2fb2 13536 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13537 {
13538 bfd_signed_vma *local_got;
ef53be89 13539 size_t j, locsymcount;
c152c796
AM
13540 Elf_Internal_Shdr *symtab_hdr;
13541
13542 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13543 continue;
13544
13545 local_got = elf_local_got_refcounts (i);
13546 if (!local_got)
13547 continue;
13548
13549 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13550 if (elf_bad_symtab (i))
13551 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13552 else
13553 locsymcount = symtab_hdr->sh_info;
13554
13555 for (j = 0; j < locsymcount; ++j)
13556 {
13557 if (local_got[j] > 0)
13558 {
13559 local_got[j] = gotoff;
10455f89 13560 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13561 }
13562 else
13563 local_got[j] = (bfd_vma) -1;
13564 }
13565 }
13566
13567 /* Then the global .got entries. .plt refcounts are handled by
13568 adjust_dynamic_symbol */
13569 gofarg.gotoff = gotoff;
10455f89 13570 gofarg.info = info;
c152c796
AM
13571 elf_link_hash_traverse (elf_hash_table (info),
13572 elf_gc_allocate_got_offsets,
13573 &gofarg);
13574 return TRUE;
13575}
13576
13577/* Many folk need no more in the way of final link than this, once
13578 got entry reference counting is enabled. */
13579
13580bfd_boolean
13581bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13582{
13583 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13584 return FALSE;
13585
13586 /* Invoke the regular ELF backend linker to do all the work. */
13587 return bfd_elf_final_link (abfd, info);
13588}
13589
13590bfd_boolean
13591bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13592{
a50b1753 13593 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13594
13595 if (rcookie->bad_symtab)
13596 rcookie->rel = rcookie->rels;
13597
13598 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13599 {
13600 unsigned long r_symndx;
13601
13602 if (! rcookie->bad_symtab)
13603 if (rcookie->rel->r_offset > offset)
13604 return FALSE;
13605 if (rcookie->rel->r_offset != offset)
13606 continue;
13607
13608 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13609 if (r_symndx == STN_UNDEF)
c152c796
AM
13610 return TRUE;
13611
13612 if (r_symndx >= rcookie->locsymcount
13613 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13614 {
13615 struct elf_link_hash_entry *h;
13616
13617 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13618
13619 while (h->root.type == bfd_link_hash_indirect
13620 || h->root.type == bfd_link_hash_warning)
13621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13622
13623 if ((h->root.type == bfd_link_hash_defined
13624 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13625 && (h->root.u.def.section->owner != rcookie->abfd
13626 || h->root.u.def.section->kept_section != NULL
13627 || discarded_section (h->root.u.def.section)))
c152c796 13628 return TRUE;
c152c796
AM
13629 }
13630 else
13631 {
13632 /* It's not a relocation against a global symbol,
13633 but it could be a relocation against a local
13634 symbol for a discarded section. */
13635 asection *isec;
13636 Elf_Internal_Sym *isym;
13637
13638 /* Need to: get the symbol; get the section. */
13639 isym = &rcookie->locsyms[r_symndx];
cb33740c 13640 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13641 if (isec != NULL
13642 && (isec->kept_section != NULL
13643 || discarded_section (isec)))
cb33740c 13644 return TRUE;
c152c796
AM
13645 }
13646 return FALSE;
13647 }
13648 return FALSE;
13649}
13650
13651/* Discard unneeded references to discarded sections.
75938853
AM
13652 Returns -1 on error, 1 if any section's size was changed, 0 if
13653 nothing changed. This function assumes that the relocations are in
13654 sorted order, which is true for all known assemblers. */
c152c796 13655
75938853 13656int
c152c796
AM
13657bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13658{
13659 struct elf_reloc_cookie cookie;
18cd5bce 13660 asection *o;
c152c796 13661 bfd *abfd;
75938853 13662 int changed = 0;
c152c796
AM
13663
13664 if (info->traditional_format
13665 || !is_elf_hash_table (info->hash))
75938853 13666 return 0;
c152c796 13667
18cd5bce
AM
13668 o = bfd_get_section_by_name (output_bfd, ".stab");
13669 if (o != NULL)
c152c796 13670 {
18cd5bce 13671 asection *i;
c152c796 13672
18cd5bce 13673 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13674 {
18cd5bce
AM
13675 if (i->size == 0
13676 || i->reloc_count == 0
13677 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13678 continue;
c152c796 13679
18cd5bce
AM
13680 abfd = i->owner;
13681 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13682 continue;
c152c796 13683
18cd5bce 13684 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13685 return -1;
c152c796 13686
18cd5bce
AM
13687 if (_bfd_discard_section_stabs (abfd, i,
13688 elf_section_data (i)->sec_info,
5241d853
RS
13689 bfd_elf_reloc_symbol_deleted_p,
13690 &cookie))
75938853 13691 changed = 1;
18cd5bce
AM
13692
13693 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13694 }
18cd5bce
AM
13695 }
13696
2f0c68f2
CM
13697 o = NULL;
13698 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13699 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13700 if (o != NULL)
13701 {
13702 asection *i;
c152c796 13703
18cd5bce 13704 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13705 {
18cd5bce
AM
13706 if (i->size == 0)
13707 continue;
13708
13709 abfd = i->owner;
13710 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13711 continue;
13712
13713 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13714 return -1;
18cd5bce
AM
13715
13716 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13717 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13718 bfd_elf_reloc_symbol_deleted_p,
13719 &cookie))
75938853 13720 changed = 1;
18cd5bce
AM
13721
13722 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13723 }
18cd5bce 13724 }
c152c796 13725
18cd5bce
AM
13726 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13727 {
13728 const struct elf_backend_data *bed;
c152c796 13729
18cd5bce
AM
13730 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13731 continue;
13732
13733 bed = get_elf_backend_data (abfd);
13734
13735 if (bed->elf_backend_discard_info != NULL)
13736 {
13737 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13738 return -1;
18cd5bce
AM
13739
13740 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13741 changed = 1;
18cd5bce
AM
13742
13743 fini_reloc_cookie (&cookie, abfd);
13744 }
c152c796
AM
13745 }
13746
2f0c68f2
CM
13747 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13748 _bfd_elf_end_eh_frame_parsing (info);
13749
13750 if (info->eh_frame_hdr_type
0e1862bb 13751 && !bfd_link_relocatable (info)
c152c796 13752 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13753 changed = 1;
c152c796 13754
75938853 13755 return changed;
c152c796 13756}
082b7297 13757
43e1669b 13758bfd_boolean
0c511000 13759_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13760 asection *sec,
c0f00686 13761 struct bfd_link_info *info)
082b7297
L
13762{
13763 flagword flags;
c77ec726 13764 const char *name, *key;
082b7297
L
13765 struct bfd_section_already_linked *l;
13766 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13767
c77ec726
AM
13768 if (sec->output_section == bfd_abs_section_ptr)
13769 return FALSE;
0c511000 13770
c77ec726 13771 flags = sec->flags;
0c511000 13772
c77ec726
AM
13773 /* Return if it isn't a linkonce section. A comdat group section
13774 also has SEC_LINK_ONCE set. */
13775 if ((flags & SEC_LINK_ONCE) == 0)
13776 return FALSE;
0c511000 13777
c77ec726
AM
13778 /* Don't put group member sections on our list of already linked
13779 sections. They are handled as a group via their group section. */
13780 if (elf_sec_group (sec) != NULL)
13781 return FALSE;
0c511000 13782
c77ec726
AM
13783 /* For a SHT_GROUP section, use the group signature as the key. */
13784 name = sec->name;
13785 if ((flags & SEC_GROUP) != 0
13786 && elf_next_in_group (sec) != NULL
13787 && elf_group_name (elf_next_in_group (sec)) != NULL)
13788 key = elf_group_name (elf_next_in_group (sec));
13789 else
13790 {
13791 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13792 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13793 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13794 key++;
0c511000 13795 else
c77ec726
AM
13796 /* Must be a user linkonce section that doesn't follow gcc's
13797 naming convention. In this case we won't be matching
13798 single member groups. */
13799 key = name;
0c511000 13800 }
6d2cd210 13801
c77ec726 13802 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13803
13804 for (l = already_linked_list->entry; l != NULL; l = l->next)
13805 {
c2370991 13806 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13807 sections with a signature of <key> (<key> is some string),
13808 and linkonce sections named .gnu.linkonce.<type>.<key>.
13809 Match like sections. LTO plugin sections are an exception.
13810 They are always named .gnu.linkonce.t.<key> and match either
13811 type of section. */
13812 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13813 && ((flags & SEC_GROUP) != 0
13814 || strcmp (name, l->sec->name) == 0))
13815 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13816 {
13817 /* The section has already been linked. See if we should
6d2cd210 13818 issue a warning. */
c77ec726
AM
13819 if (!_bfd_handle_already_linked (sec, l, info))
13820 return FALSE;
082b7297 13821
c77ec726 13822 if (flags & SEC_GROUP)
3d7f7666 13823 {
c77ec726
AM
13824 asection *first = elf_next_in_group (sec);
13825 asection *s = first;
3d7f7666 13826
c77ec726 13827 while (s != NULL)
3d7f7666 13828 {
c77ec726
AM
13829 s->output_section = bfd_abs_section_ptr;
13830 /* Record which group discards it. */
13831 s->kept_section = l->sec;
13832 s = elf_next_in_group (s);
13833 /* These lists are circular. */
13834 if (s == first)
13835 break;
3d7f7666
L
13836 }
13837 }
082b7297 13838
43e1669b 13839 return TRUE;
082b7297
L
13840 }
13841 }
13842
c77ec726
AM
13843 /* A single member comdat group section may be discarded by a
13844 linkonce section and vice versa. */
13845 if ((flags & SEC_GROUP) != 0)
3d7f7666 13846 {
c77ec726 13847 asection *first = elf_next_in_group (sec);
c2370991 13848
c77ec726
AM
13849 if (first != NULL && elf_next_in_group (first) == first)
13850 /* Check this single member group against linkonce sections. */
13851 for (l = already_linked_list->entry; l != NULL; l = l->next)
13852 if ((l->sec->flags & SEC_GROUP) == 0
13853 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13854 {
13855 first->output_section = bfd_abs_section_ptr;
13856 first->kept_section = l->sec;
13857 sec->output_section = bfd_abs_section_ptr;
13858 break;
13859 }
13860 }
13861 else
13862 /* Check this linkonce section against single member groups. */
13863 for (l = already_linked_list->entry; l != NULL; l = l->next)
13864 if (l->sec->flags & SEC_GROUP)
6d2cd210 13865 {
c77ec726 13866 asection *first = elf_next_in_group (l->sec);
6d2cd210 13867
c77ec726
AM
13868 if (first != NULL
13869 && elf_next_in_group (first) == first
13870 && bfd_elf_match_symbols_in_sections (first, sec, info))
13871 {
13872 sec->output_section = bfd_abs_section_ptr;
13873 sec->kept_section = first;
13874 break;
13875 }
6d2cd210 13876 }
0c511000 13877
c77ec726
AM
13878 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13879 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13880 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13881 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13882 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13883 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13884 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13885 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13886 The reverse order cannot happen as there is never a bfd with only the
13887 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13888 matter as here were are looking only for cross-bfd sections. */
13889
13890 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13891 for (l = already_linked_list->entry; l != NULL; l = l->next)
13892 if ((l->sec->flags & SEC_GROUP) == 0
13893 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13894 {
13895 if (abfd != l->sec->owner)
13896 sec->output_section = bfd_abs_section_ptr;
13897 break;
13898 }
80c29487 13899
082b7297 13900 /* This is the first section with this name. Record it. */
c77ec726 13901 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13902 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13903 return sec->output_section == bfd_abs_section_ptr;
082b7297 13904}
81e1b023 13905
a4d8e49b
L
13906bfd_boolean
13907_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13908{
13909 return sym->st_shndx == SHN_COMMON;
13910}
13911
13912unsigned int
13913_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13914{
13915 return SHN_COMMON;
13916}
13917
13918asection *
13919_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13920{
13921 return bfd_com_section_ptr;
13922}
10455f89
HPN
13923
13924bfd_vma
13925_bfd_elf_default_got_elt_size (bfd *abfd,
13926 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13927 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13928 bfd *ibfd ATTRIBUTE_UNUSED,
13929 unsigned long symndx ATTRIBUTE_UNUSED)
13930{
13931 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13932 return bed->s->arch_size / 8;
13933}
83bac4b0
NC
13934
13935/* Routines to support the creation of dynamic relocs. */
13936
83bac4b0
NC
13937/* Returns the name of the dynamic reloc section associated with SEC. */
13938
13939static const char *
13940get_dynamic_reloc_section_name (bfd * abfd,
13941 asection * sec,
13942 bfd_boolean is_rela)
13943{
ddcf1fcf
BS
13944 char *name;
13945 const char *old_name = bfd_get_section_name (NULL, sec);
13946 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13947
ddcf1fcf 13948 if (old_name == NULL)
83bac4b0
NC
13949 return NULL;
13950
ddcf1fcf 13951 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13952 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13953
13954 return name;
13955}
13956
13957/* Returns the dynamic reloc section associated with SEC.
13958 If necessary compute the name of the dynamic reloc section based
13959 on SEC's name (looked up in ABFD's string table) and the setting
13960 of IS_RELA. */
13961
13962asection *
13963_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13964 asection * sec,
13965 bfd_boolean is_rela)
13966{
13967 asection * reloc_sec = elf_section_data (sec)->sreloc;
13968
13969 if (reloc_sec == NULL)
13970 {
13971 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13972
13973 if (name != NULL)
13974 {
3d4d4302 13975 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13976
13977 if (reloc_sec != NULL)
13978 elf_section_data (sec)->sreloc = reloc_sec;
13979 }
13980 }
13981
13982 return reloc_sec;
13983}
13984
13985/* Returns the dynamic reloc section associated with SEC. If the
13986 section does not exist it is created and attached to the DYNOBJ
13987 bfd and stored in the SRELOC field of SEC's elf_section_data
13988 structure.
f8076f98 13989
83bac4b0
NC
13990 ALIGNMENT is the alignment for the newly created section and
13991 IS_RELA defines whether the name should be .rela.<SEC's name>
13992 or .rel.<SEC's name>. The section name is looked up in the
13993 string table associated with ABFD. */
13994
13995asection *
ca4be51c
AM
13996_bfd_elf_make_dynamic_reloc_section (asection *sec,
13997 bfd *dynobj,
13998 unsigned int alignment,
13999 bfd *abfd,
14000 bfd_boolean is_rela)
83bac4b0
NC
14001{
14002 asection * reloc_sec = elf_section_data (sec)->sreloc;
14003
14004 if (reloc_sec == NULL)
14005 {
14006 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14007
14008 if (name == NULL)
14009 return NULL;
14010
3d4d4302 14011 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14012
14013 if (reloc_sec == NULL)
14014 {
3d4d4302
AM
14015 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14016 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14017 if ((sec->flags & SEC_ALLOC) != 0)
14018 flags |= SEC_ALLOC | SEC_LOAD;
14019
3d4d4302 14020 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14021 if (reloc_sec != NULL)
14022 {
8877b5e5
AM
14023 /* _bfd_elf_get_sec_type_attr chooses a section type by
14024 name. Override as it may be wrong, eg. for a user
14025 section named "auto" we'll get ".relauto" which is
14026 seen to be a .rela section. */
14027 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14028 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14029 reloc_sec = NULL;
14030 }
14031 }
14032
14033 elf_section_data (sec)->sreloc = reloc_sec;
14034 }
14035
14036 return reloc_sec;
14037}
1338dd10 14038
bffebb6b
AM
14039/* Copy the ELF symbol type and other attributes for a linker script
14040 assignment from HSRC to HDEST. Generally this should be treated as
14041 if we found a strong non-dynamic definition for HDEST (except that
14042 ld ignores multiple definition errors). */
1338dd10 14043void
bffebb6b
AM
14044_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14045 struct bfd_link_hash_entry *hdest,
14046 struct bfd_link_hash_entry *hsrc)
1338dd10 14047{
bffebb6b
AM
14048 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14049 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14050 Elf_Internal_Sym isym;
1338dd10
PB
14051
14052 ehdest->type = ehsrc->type;
35fc36a8 14053 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14054
14055 isym.st_other = ehsrc->other;
b8417128 14056 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14057}
351f65ca
L
14058
14059/* Append a RELA relocation REL to section S in BFD. */
14060
14061void
14062elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14063{
14064 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14065 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14066 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14067 bed->s->swap_reloca_out (abfd, rel, loc);
14068}
14069
14070/* Append a REL relocation REL to section S in BFD. */
14071
14072void
14073elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14074{
14075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14076 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14077 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14078 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14079}