Move .stack before debug sections
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
2571583a 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
53df40a4 23#include "bfd_stdint.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
08ce1d72 31#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 32#include "plugin-api.h"
7dc3990e
L
33#include "plugin.h"
34#endif
252b5132 35
28caa186
AM
36/* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39struct elf_info_failed
40{
41 struct bfd_link_info *info;
28caa186
AM
42 bfd_boolean failed;
43};
44
45/* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48struct elf_find_verdep_info
49{
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56};
57
58static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
2f0c68f2
CM
61asection *
62_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65{
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100}
101
d98685ac
AM
102/* Define a symbol in a dynamic linkage section. */
103
104struct elf_link_hash_entry *
105_bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109{
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
ccabcbe5 112 const struct elf_backend_data *bed;
d98685ac
AM
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
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,
b531344c
MR
674 then clear out any version information because the symbol will not be
675 associated with the dynamic object any more. */
45d6a902 676 if (!provide
f5385ebf
AM
677 && h->def_dynamic
678 && !h->def_regular)
b531344c
MR
679 h->verinfo.verdef = NULL;
680
681 /* Make sure this symbol is not garbage collected. */
682 h->mark = 1;
45d6a902 683
f5385ebf 684 h->def_regular = 1;
45d6a902 685
eb8476a6 686 if (hidden)
fe21a8fc 687 {
91d6fa6a 688 bed = get_elf_backend_data (output_bfd);
b8297068
AM
689 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
690 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
691 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
692 }
693
6fa3860b
PB
694 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
695 and executables. */
0e1862bb 696 if (!bfd_link_relocatable (info)
6fa3860b
PB
697 && h->dynindx != -1
698 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
699 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
700 h->forced_local = 1;
701
f5385ebf
AM
702 if ((h->def_dynamic
703 || h->ref_dynamic
6b3b0ab8
L
704 || bfd_link_dll (info)
705 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
706 && h->dynindx == -1)
707 {
c152c796 708 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
709 return FALSE;
710
711 /* If this is a weak defined symbol, and we know a corresponding
712 real symbol from the same dynamic object, make sure the real
713 symbol is also made into a dynamic symbol. */
f6e332e6
AM
714 if (h->u.weakdef != NULL
715 && h->u.weakdef->dynindx == -1)
45d6a902 716 {
f6e332e6 717 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
718 return FALSE;
719 }
720 }
721
722 return TRUE;
723}
42751cf3 724
8c58d23b
AM
725/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
726 success, and 2 on a failure caused by attempting to record a symbol
727 in a discarded section, eg. a discarded link-once section symbol. */
728
729int
c152c796
AM
730bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
731 bfd *input_bfd,
732 long input_indx)
8c58d23b
AM
733{
734 bfd_size_type amt;
735 struct elf_link_local_dynamic_entry *entry;
736 struct elf_link_hash_table *eht;
737 struct elf_strtab_hash *dynstr;
ef53be89 738 size_t dynstr_index;
8c58d23b
AM
739 char *name;
740 Elf_External_Sym_Shndx eshndx;
741 char esym[sizeof (Elf64_External_Sym)];
742
0eddce27 743 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
744 return 0;
745
746 /* See if the entry exists already. */
747 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
748 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
749 return 1;
750
751 amt = sizeof (*entry);
a50b1753 752 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
753 if (entry == NULL)
754 return 0;
755
756 /* Go find the symbol, so that we can find it's name. */
757 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 758 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
759 {
760 bfd_release (input_bfd, entry);
761 return 0;
762 }
763
764 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 765 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
766 {
767 asection *s;
768
769 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
770 if (s == NULL || bfd_is_abs_section (s->output_section))
771 {
772 /* We can still bfd_release here as nothing has done another
773 bfd_alloc. We can't do this later in this function. */
774 bfd_release (input_bfd, entry);
775 return 2;
776 }
777 }
778
779 name = (bfd_elf_string_from_elf_section
780 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
781 entry->isym.st_name));
782
783 dynstr = elf_hash_table (info)->dynstr;
784 if (dynstr == NULL)
785 {
786 /* Create a strtab to hold the dynamic symbol names. */
787 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
788 if (dynstr == NULL)
789 return 0;
790 }
791
b34976b6 792 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 793 if (dynstr_index == (size_t) -1)
8c58d23b
AM
794 return 0;
795 entry->isym.st_name = dynstr_index;
796
797 eht = elf_hash_table (info);
798
799 entry->next = eht->dynlocal;
800 eht->dynlocal = entry;
801 entry->input_bfd = input_bfd;
802 entry->input_indx = input_indx;
803 eht->dynsymcount++;
804
805 /* Whatever binding the symbol had before, it's now local. */
806 entry->isym.st_info
807 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
808
809 /* The dynindx will be set at the end of size_dynamic_sections. */
810
811 return 1;
812}
813
30b30c21 814/* Return the dynindex of a local dynamic symbol. */
42751cf3 815
30b30c21 816long
268b6b39
AM
817_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
818 bfd *input_bfd,
819 long input_indx)
30b30c21
RH
820{
821 struct elf_link_local_dynamic_entry *e;
822
823 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
824 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
825 return e->dynindx;
826 return -1;
827}
828
829/* This function is used to renumber the dynamic symbols, if some of
830 them are removed because they are marked as local. This is called
831 via elf_link_hash_traverse. */
832
b34976b6 833static bfd_boolean
268b6b39
AM
834elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
835 void *data)
42751cf3 836{
a50b1753 837 size_t *count = (size_t *) data;
30b30c21 838
6fa3860b
PB
839 if (h->forced_local)
840 return TRUE;
841
842 if (h->dynindx != -1)
843 h->dynindx = ++(*count);
844
845 return TRUE;
846}
847
848
849/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
850 STB_LOCAL binding. */
851
852static bfd_boolean
853elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
854 void *data)
855{
a50b1753 856 size_t *count = (size_t *) data;
6fa3860b 857
6fa3860b
PB
858 if (!h->forced_local)
859 return TRUE;
860
42751cf3 861 if (h->dynindx != -1)
30b30c21
RH
862 h->dynindx = ++(*count);
863
b34976b6 864 return TRUE;
42751cf3 865}
30b30c21 866
aee6f5b4
AO
867/* Return true if the dynamic symbol for a given section should be
868 omitted when creating a shared library. */
869bfd_boolean
870_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
871 struct bfd_link_info *info,
872 asection *p)
873{
74541ad4 874 struct elf_link_hash_table *htab;
ca55926c 875 asection *ip;
74541ad4 876
aee6f5b4
AO
877 switch (elf_section_data (p)->this_hdr.sh_type)
878 {
879 case SHT_PROGBITS:
880 case SHT_NOBITS:
881 /* If sh_type is yet undecided, assume it could be
882 SHT_PROGBITS/SHT_NOBITS. */
883 case SHT_NULL:
74541ad4
AM
884 htab = elf_hash_table (info);
885 if (p == htab->tls_sec)
886 return FALSE;
887
888 if (htab->text_index_section != NULL)
889 return p != htab->text_index_section && p != htab->data_index_section;
890
ca55926c 891 return (htab->dynobj != NULL
3d4d4302 892 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 893 && ip->output_section == p);
aee6f5b4
AO
894
895 /* There shouldn't be section relative relocations
896 against any other section. */
897 default:
898 return TRUE;
899 }
900}
901
062e2358 902/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
903 symbol for each output section, which come first. Next come symbols
904 which have been forced to local binding. Then all of the back-end
905 allocated local dynamic syms, followed by the rest of the global
906 symbols. */
30b30c21 907
554220db
AM
908static unsigned long
909_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
910 struct bfd_link_info *info,
911 unsigned long *section_sym_count)
30b30c21
RH
912{
913 unsigned long dynsymcount = 0;
914
0e1862bb
L
915 if (bfd_link_pic (info)
916 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 917 {
aee6f5b4 918 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
919 asection *p;
920 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 921 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
922 && (p->flags & SEC_ALLOC) != 0
923 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
924 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
925 else
926 elf_section_data (p)->dynindx = 0;
30b30c21 927 }
554220db 928 *section_sym_count = dynsymcount;
30b30c21 929
6fa3860b
PB
930 elf_link_hash_traverse (elf_hash_table (info),
931 elf_link_renumber_local_hash_table_dynsyms,
932 &dynsymcount);
933
30b30c21
RH
934 if (elf_hash_table (info)->dynlocal)
935 {
936 struct elf_link_local_dynamic_entry *p;
937 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
938 p->dynindx = ++dynsymcount;
939 }
90ac2420 940 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
941
942 elf_link_hash_traverse (elf_hash_table (info),
943 elf_link_renumber_hash_table_dynsyms,
944 &dynsymcount);
945
d5486c43
L
946 /* There is an unused NULL entry at the head of the table which we
947 must account for in our count even if the table is empty since it
948 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
949 .dynamic section. */
950 dynsymcount++;
30b30c21 951
ccabcbe5
AM
952 elf_hash_table (info)->dynsymcount = dynsymcount;
953 return dynsymcount;
30b30c21 954}
252b5132 955
54ac0771
L
956/* Merge st_other field. */
957
958static void
959elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 960 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 961 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
962{
963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
964
965 /* If st_other has a processor-specific meaning, specific
cd3416da 966 code might be needed here. */
54ac0771
L
967 if (bed->elf_backend_merge_symbol_attribute)
968 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
969 dynamic);
970
cd3416da 971 if (!dynamic)
54ac0771 972 {
cd3416da
AM
973 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
974 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 975
cd3416da
AM
976 /* Keep the most constraining visibility. Leave the remainder
977 of the st_other field to elf_backend_merge_symbol_attribute. */
978 if (symvis - 1 < hvis - 1)
979 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 980 }
b8417128
AM
981 else if (definition
982 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
983 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 984 h->protected_def = 1;
54ac0771
L
985}
986
4f3fedcf
AM
987/* This function is called when we want to merge a new symbol with an
988 existing symbol. It handles the various cases which arise when we
989 find a definition in a dynamic object, or when there is already a
990 definition in a dynamic object. The new symbol is described by
991 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
992 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
993 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
994 of an old common symbol. We set OVERRIDE if the old symbol is
995 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
996 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
997 to change. By OK to change, we mean that we shouldn't warn if the
998 type or size does change. */
45d6a902 999
8a56bd02 1000static bfd_boolean
268b6b39
AM
1001_bfd_elf_merge_symbol (bfd *abfd,
1002 struct bfd_link_info *info,
1003 const char *name,
1004 Elf_Internal_Sym *sym,
1005 asection **psec,
1006 bfd_vma *pvalue,
4f3fedcf
AM
1007 struct elf_link_hash_entry **sym_hash,
1008 bfd **poldbfd,
37a9e49a 1009 bfd_boolean *pold_weak,
af44c138 1010 unsigned int *pold_alignment,
268b6b39
AM
1011 bfd_boolean *skip,
1012 bfd_boolean *override,
1013 bfd_boolean *type_change_ok,
6e33951e
L
1014 bfd_boolean *size_change_ok,
1015 bfd_boolean *matched)
252b5132 1016{
7479dfd4 1017 asection *sec, *oldsec;
45d6a902 1018 struct elf_link_hash_entry *h;
90c984fc 1019 struct elf_link_hash_entry *hi;
45d6a902
AM
1020 struct elf_link_hash_entry *flip;
1021 int bind;
1022 bfd *oldbfd;
1023 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1024 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1025 const struct elf_backend_data *bed;
6e33951e 1026 char *new_version;
45d6a902
AM
1027
1028 *skip = FALSE;
1029 *override = FALSE;
1030
1031 sec = *psec;
1032 bind = ELF_ST_BIND (sym->st_info);
1033
1034 if (! bfd_is_und_section (sec))
1035 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1036 else
1037 h = ((struct elf_link_hash_entry *)
1038 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1039 if (h == NULL)
1040 return FALSE;
1041 *sym_hash = h;
252b5132 1042
88ba32a0
L
1043 bed = get_elf_backend_data (abfd);
1044
6e33951e 1045 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1046 if (h->versioned != unversioned)
6e33951e 1047 {
422f1182
L
1048 /* Symbol version is unknown or versioned. */
1049 new_version = strrchr (name, ELF_VER_CHR);
1050 if (new_version)
1051 {
1052 if (h->versioned == unknown)
1053 {
1054 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1055 h->versioned = versioned_hidden;
1056 else
1057 h->versioned = versioned;
1058 }
1059 new_version += 1;
1060 if (new_version[0] == '\0')
1061 new_version = NULL;
1062 }
1063 else
1064 h->versioned = unversioned;
6e33951e 1065 }
422f1182
L
1066 else
1067 new_version = NULL;
6e33951e 1068
90c984fc
L
1069 /* For merging, we only care about real symbols. But we need to make
1070 sure that indirect symbol dynamic flags are updated. */
1071 hi = h;
45d6a902
AM
1072 while (h->root.type == bfd_link_hash_indirect
1073 || h->root.type == bfd_link_hash_warning)
1074 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1075
6e33951e
L
1076 if (!*matched)
1077 {
1078 if (hi == h || h->root.type == bfd_link_hash_new)
1079 *matched = TRUE;
1080 else
1081 {
ae7683d2 1082 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1083 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1084 true if the new symbol is only visible to the symbol with
6e33951e 1085 the same symbol version. */
422f1182
L
1086 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1087 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1088 if (!old_hidden && !new_hidden)
1089 /* The new symbol matches the existing symbol if both
1090 aren't hidden. */
1091 *matched = TRUE;
1092 else
1093 {
1094 /* OLD_VERSION is the symbol version of the existing
1095 symbol. */
422f1182
L
1096 char *old_version;
1097
1098 if (h->versioned >= versioned)
1099 old_version = strrchr (h->root.root.string,
1100 ELF_VER_CHR) + 1;
1101 else
1102 old_version = NULL;
6e33951e
L
1103
1104 /* The new symbol matches the existing symbol if they
1105 have the same symbol version. */
1106 *matched = (old_version == new_version
1107 || (old_version != NULL
1108 && new_version != NULL
1109 && strcmp (old_version, new_version) == 0));
1110 }
1111 }
1112 }
1113
934bce08
AM
1114 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1115 existing symbol. */
1116
1117 oldbfd = NULL;
1118 oldsec = NULL;
1119 switch (h->root.type)
1120 {
1121 default:
1122 break;
1123
1124 case bfd_link_hash_undefined:
1125 case bfd_link_hash_undefweak:
1126 oldbfd = h->root.u.undef.abfd;
1127 break;
1128
1129 case bfd_link_hash_defined:
1130 case bfd_link_hash_defweak:
1131 oldbfd = h->root.u.def.section->owner;
1132 oldsec = h->root.u.def.section;
1133 break;
1134
1135 case bfd_link_hash_common:
1136 oldbfd = h->root.u.c.p->section->owner;
1137 oldsec = h->root.u.c.p->section;
1138 if (pold_alignment)
1139 *pold_alignment = h->root.u.c.p->alignment_power;
1140 break;
1141 }
1142 if (poldbfd && *poldbfd == NULL)
1143 *poldbfd = oldbfd;
1144
1145 /* Differentiate strong and weak symbols. */
1146 newweak = bind == STB_WEAK;
1147 oldweak = (h->root.type == bfd_link_hash_defweak
1148 || h->root.type == bfd_link_hash_undefweak);
1149 if (pold_weak)
1150 *pold_weak = oldweak;
1151
1152 /* This code is for coping with dynamic objects, and is only useful
1153 if we are doing an ELF link. */
1154 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1155 return TRUE;
1156
40b36307 1157 /* We have to check it for every instance since the first few may be
ee659f1f 1158 references and not all compilers emit symbol type for undefined
40b36307
L
1159 symbols. */
1160 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1161
ee659f1f
AM
1162 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1163 respectively, is from a dynamic object. */
1164
1165 newdyn = (abfd->flags & DYNAMIC) != 0;
1166
1167 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1168 syms and defined syms in dynamic libraries respectively.
1169 ref_dynamic on the other hand can be set for a symbol defined in
1170 a dynamic library, and def_dynamic may not be set; When the
1171 definition in a dynamic lib is overridden by a definition in the
1172 executable use of the symbol in the dynamic lib becomes a
1173 reference to the executable symbol. */
1174 if (newdyn)
1175 {
1176 if (bfd_is_und_section (sec))
1177 {
1178 if (bind != STB_WEAK)
1179 {
1180 h->ref_dynamic_nonweak = 1;
1181 hi->ref_dynamic_nonweak = 1;
1182 }
1183 }
1184 else
1185 {
6e33951e
L
1186 /* Update the existing symbol only if they match. */
1187 if (*matched)
1188 h->dynamic_def = 1;
ee659f1f
AM
1189 hi->dynamic_def = 1;
1190 }
1191 }
1192
45d6a902
AM
1193 /* If we just created the symbol, mark it as being an ELF symbol.
1194 Other than that, there is nothing to do--there is no merge issue
1195 with a newly defined symbol--so we just return. */
1196
1197 if (h->root.type == bfd_link_hash_new)
252b5132 1198 {
f5385ebf 1199 h->non_elf = 0;
45d6a902
AM
1200 return TRUE;
1201 }
252b5132 1202
45d6a902
AM
1203 /* In cases involving weak versioned symbols, we may wind up trying
1204 to merge a symbol with itself. Catch that here, to avoid the
1205 confusion that results if we try to override a symbol with
1206 itself. The additional tests catch cases like
1207 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1208 dynamic object, which we do want to handle here. */
1209 if (abfd == oldbfd
895fa45f 1210 && (newweak || oldweak)
45d6a902 1211 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1212 || !h->def_regular))
45d6a902
AM
1213 return TRUE;
1214
707bba77 1215 olddyn = FALSE;
45d6a902
AM
1216 if (oldbfd != NULL)
1217 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1218 else if (oldsec != NULL)
45d6a902 1219 {
707bba77 1220 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1221 indices used by MIPS ELF. */
707bba77 1222 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1223 }
252b5132 1224
45d6a902
AM
1225 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1226 respectively, appear to be a definition rather than reference. */
1227
707bba77 1228 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1229
707bba77
AM
1230 olddef = (h->root.type != bfd_link_hash_undefined
1231 && h->root.type != bfd_link_hash_undefweak
202ac193 1232 && h->root.type != bfd_link_hash_common);
45d6a902 1233
0a36a439
L
1234 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1235 respectively, appear to be a function. */
1236
1237 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1238 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1239
1240 oldfunc = (h->type != STT_NOTYPE
1241 && bed->is_function_type (h->type));
1242
5b677558
AM
1243 /* If creating a default indirect symbol ("foo" or "foo@") from a
1244 dynamic versioned definition ("foo@@") skip doing so if there is
1245 an existing regular definition with a different type. We don't
1246 want, for example, a "time" variable in the executable overriding
1247 a "time" function in a shared library. */
580a2b6e 1248 if (pold_alignment == NULL
580a2b6e
L
1249 && newdyn
1250 && newdef
1251 && !olddyn
5b677558
AM
1252 && (olddef || h->root.type == bfd_link_hash_common)
1253 && ELF_ST_TYPE (sym->st_info) != h->type
1254 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1255 && h->type != STT_NOTYPE
1256 && !(newfunc && oldfunc))
580a2b6e
L
1257 {
1258 *skip = TRUE;
1259 return TRUE;
1260 }
1261
4c34aff8
AM
1262 /* Check TLS symbols. We don't check undefined symbols introduced
1263 by "ld -u" which have no type (and oldbfd NULL), and we don't
1264 check symbols from plugins because they also have no type. */
1265 if (oldbfd != NULL
1266 && (oldbfd->flags & BFD_PLUGIN) == 0
1267 && (abfd->flags & BFD_PLUGIN) == 0
1268 && ELF_ST_TYPE (sym->st_info) != h->type
1269 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1270 {
1271 bfd *ntbfd, *tbfd;
1272 bfd_boolean ntdef, tdef;
1273 asection *ntsec, *tsec;
1274
1275 if (h->type == STT_TLS)
1276 {
3b36f7e6 1277 ntbfd = abfd;
7479dfd4
L
1278 ntsec = sec;
1279 ntdef = newdef;
1280 tbfd = oldbfd;
1281 tsec = oldsec;
1282 tdef = olddef;
1283 }
1284 else
1285 {
1286 ntbfd = oldbfd;
1287 ntsec = oldsec;
1288 ntdef = olddef;
1289 tbfd = abfd;
1290 tsec = sec;
1291 tdef = newdef;
1292 }
1293
1294 if (tdef && ntdef)
4eca0228 1295 _bfd_error_handler
695344c0 1296 /* xgettext:c-format */
191c0c42
AM
1297 (_("%s: TLS definition in %B section %A "
1298 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1299 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1300 else if (!tdef && !ntdef)
4eca0228 1301 _bfd_error_handler
695344c0 1302 /* xgettext:c-format */
191c0c42
AM
1303 (_("%s: TLS reference in %B "
1304 "mismatches non-TLS reference in %B"),
7479dfd4
L
1305 tbfd, ntbfd, h->root.root.string);
1306 else if (tdef)
4eca0228 1307 _bfd_error_handler
695344c0 1308 /* xgettext:c-format */
191c0c42
AM
1309 (_("%s: TLS definition in %B section %A "
1310 "mismatches non-TLS reference in %B"),
7479dfd4
L
1311 tbfd, tsec, ntbfd, h->root.root.string);
1312 else
4eca0228 1313 _bfd_error_handler
695344c0 1314 /* xgettext:c-format */
191c0c42
AM
1315 (_("%s: TLS reference in %B "
1316 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1317 tbfd, ntbfd, ntsec, h->root.root.string);
1318
1319 bfd_set_error (bfd_error_bad_value);
1320 return FALSE;
1321 }
1322
45d6a902
AM
1323 /* If the old symbol has non-default visibility, we ignore the new
1324 definition from a dynamic object. */
1325 if (newdyn
9c7a29a3 1326 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1327 && !bfd_is_und_section (sec))
1328 {
1329 *skip = TRUE;
1330 /* Make sure this symbol is dynamic. */
f5385ebf 1331 h->ref_dynamic = 1;
90c984fc 1332 hi->ref_dynamic = 1;
45d6a902
AM
1333 /* A protected symbol has external availability. Make sure it is
1334 recorded as dynamic.
1335
1336 FIXME: Should we check type and size for protected symbol? */
1337 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1338 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1339 else
1340 return TRUE;
1341 }
1342 else if (!newdyn
9c7a29a3 1343 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1344 && h->def_dynamic)
45d6a902
AM
1345 {
1346 /* If the new symbol with non-default visibility comes from a
1347 relocatable file and the old definition comes from a dynamic
1348 object, we remove the old definition. */
6c9b78e6 1349 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1350 {
1351 /* Handle the case where the old dynamic definition is
1352 default versioned. We need to copy the symbol info from
1353 the symbol with default version to the normal one if it
1354 was referenced before. */
1355 if (h->ref_regular)
1356 {
6c9b78e6 1357 hi->root.type = h->root.type;
d2dee3b2 1358 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1359 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1360
6c9b78e6 1361 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1362 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1363 {
aed81c4e
MR
1364 /* If the new symbol is hidden or internal, completely undo
1365 any dynamic link state. */
1366 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1367 h->forced_local = 0;
1368 h->ref_dynamic = 0;
d2dee3b2
L
1369 }
1370 else
aed81c4e
MR
1371 h->ref_dynamic = 1;
1372
1373 h->def_dynamic = 0;
aed81c4e
MR
1374 /* FIXME: Should we check type and size for protected symbol? */
1375 h->size = 0;
1376 h->type = 0;
1377
6c9b78e6 1378 h = hi;
d2dee3b2
L
1379 }
1380 else
6c9b78e6 1381 h = hi;
d2dee3b2 1382 }
1de1a317 1383
f5eda473
AM
1384 /* If the old symbol was undefined before, then it will still be
1385 on the undefs list. If the new symbol is undefined or
1386 common, we can't make it bfd_link_hash_new here, because new
1387 undefined or common symbols will be added to the undefs list
1388 by _bfd_generic_link_add_one_symbol. Symbols may not be
1389 added twice to the undefs list. Also, if the new symbol is
1390 undefweak then we don't want to lose the strong undef. */
1391 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1392 {
1de1a317 1393 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1394 h->root.u.undef.abfd = abfd;
1395 }
1396 else
1397 {
1398 h->root.type = bfd_link_hash_new;
1399 h->root.u.undef.abfd = NULL;
1400 }
1401
f5eda473 1402 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1403 {
f5eda473
AM
1404 /* If the new symbol is hidden or internal, completely undo
1405 any dynamic link state. */
1406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1407 h->forced_local = 0;
1408 h->ref_dynamic = 0;
45d6a902 1409 }
f5eda473
AM
1410 else
1411 h->ref_dynamic = 1;
1412 h->def_dynamic = 0;
45d6a902
AM
1413 /* FIXME: Should we check type and size for protected symbol? */
1414 h->size = 0;
1415 h->type = 0;
1416 return TRUE;
1417 }
14a793b2 1418
15b43f48
AM
1419 /* If a new weak symbol definition comes from a regular file and the
1420 old symbol comes from a dynamic library, we treat the new one as
1421 strong. Similarly, an old weak symbol definition from a regular
1422 file is treated as strong when the new symbol comes from a dynamic
1423 library. Further, an old weak symbol from a dynamic library is
1424 treated as strong if the new symbol is from a dynamic library.
1425 This reflects the way glibc's ld.so works.
1426
1427 Do this before setting *type_change_ok or *size_change_ok so that
1428 we warn properly when dynamic library symbols are overridden. */
1429
1430 if (newdef && !newdyn && olddyn)
0f8a2703 1431 newweak = FALSE;
15b43f48 1432 if (olddef && newdyn)
0f8a2703
AM
1433 oldweak = FALSE;
1434
d334575b 1435 /* Allow changes between different types of function symbol. */
0a36a439 1436 if (newfunc && oldfunc)
fcb93ecf
PB
1437 *type_change_ok = TRUE;
1438
79349b09
AM
1439 /* It's OK to change the type if either the existing symbol or the
1440 new symbol is weak. A type change is also OK if the old symbol
1441 is undefined and the new symbol is defined. */
252b5132 1442
79349b09
AM
1443 if (oldweak
1444 || newweak
1445 || (newdef
1446 && h->root.type == bfd_link_hash_undefined))
1447 *type_change_ok = TRUE;
1448
1449 /* It's OK to change the size if either the existing symbol or the
1450 new symbol is weak, or if the old symbol is undefined. */
1451
1452 if (*type_change_ok
1453 || h->root.type == bfd_link_hash_undefined)
1454 *size_change_ok = TRUE;
45d6a902 1455
45d6a902
AM
1456 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1457 symbol, respectively, appears to be a common symbol in a dynamic
1458 object. If a symbol appears in an uninitialized section, and is
1459 not weak, and is not a function, then it may be a common symbol
1460 which was resolved when the dynamic object was created. We want
1461 to treat such symbols specially, because they raise special
1462 considerations when setting the symbol size: if the symbol
1463 appears as a common symbol in a regular object, and the size in
1464 the regular object is larger, we must make sure that we use the
1465 larger size. This problematic case can always be avoided in C,
1466 but it must be handled correctly when using Fortran shared
1467 libraries.
1468
1469 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1470 likewise for OLDDYNCOMMON and OLDDEF.
1471
1472 Note that this test is just a heuristic, and that it is quite
1473 possible to have an uninitialized symbol in a shared object which
1474 is really a definition, rather than a common symbol. This could
1475 lead to some minor confusion when the symbol really is a common
1476 symbol in some regular object. However, I think it will be
1477 harmless. */
1478
1479 if (newdyn
1480 && newdef
79349b09 1481 && !newweak
45d6a902
AM
1482 && (sec->flags & SEC_ALLOC) != 0
1483 && (sec->flags & SEC_LOAD) == 0
1484 && sym->st_size > 0
0a36a439 1485 && !newfunc)
45d6a902
AM
1486 newdyncommon = TRUE;
1487 else
1488 newdyncommon = FALSE;
1489
1490 if (olddyn
1491 && olddef
1492 && h->root.type == bfd_link_hash_defined
f5385ebf 1493 && h->def_dynamic
45d6a902
AM
1494 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1495 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1496 && h->size > 0
0a36a439 1497 && !oldfunc)
45d6a902
AM
1498 olddyncommon = TRUE;
1499 else
1500 olddyncommon = FALSE;
1501
a4d8e49b
L
1502 /* We now know everything about the old and new symbols. We ask the
1503 backend to check if we can merge them. */
5d13b3b3
AM
1504 if (bed->merge_symbol != NULL)
1505 {
1506 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1507 return FALSE;
1508 sec = *psec;
1509 }
a4d8e49b 1510
45d6a902
AM
1511 /* If both the old and the new symbols look like common symbols in a
1512 dynamic object, set the size of the symbol to the larger of the
1513 two. */
1514
1515 if (olddyncommon
1516 && newdyncommon
1517 && sym->st_size != h->size)
1518 {
1519 /* Since we think we have two common symbols, issue a multiple
1520 common warning if desired. Note that we only warn if the
1521 size is different. If the size is the same, we simply let
1522 the old symbol override the new one as normally happens with
1523 symbols defined in dynamic objects. */
1524
1a72702b
AM
1525 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1526 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1527 if (sym->st_size > h->size)
1528 h->size = sym->st_size;
252b5132 1529
45d6a902 1530 *size_change_ok = TRUE;
252b5132
RH
1531 }
1532
45d6a902
AM
1533 /* If we are looking at a dynamic object, and we have found a
1534 definition, we need to see if the symbol was already defined by
1535 some other object. If so, we want to use the existing
1536 definition, and we do not want to report a multiple symbol
1537 definition error; we do this by clobbering *PSEC to be
1538 bfd_und_section_ptr.
1539
1540 We treat a common symbol as a definition if the symbol in the
1541 shared library is a function, since common symbols always
1542 represent variables; this can cause confusion in principle, but
1543 any such confusion would seem to indicate an erroneous program or
1544 shared library. We also permit a common symbol in a regular
202ac193
L
1545 object to override a weak symbol in a shared object. A common
1546 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1547
1548 if (newdyn
1549 && newdef
77cfaee6 1550 && (olddef
45d6a902 1551 || (h->root.type == bfd_link_hash_common
202ac193
L
1552 && (newweak
1553 || newfunc
1554 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1555 {
1556 *override = TRUE;
1557 newdef = FALSE;
1558 newdyncommon = FALSE;
252b5132 1559
45d6a902
AM
1560 *psec = sec = bfd_und_section_ptr;
1561 *size_change_ok = TRUE;
252b5132 1562
45d6a902
AM
1563 /* If we get here when the old symbol is a common symbol, then
1564 we are explicitly letting it override a weak symbol or
1565 function in a dynamic object, and we don't want to warn about
1566 a type change. If the old symbol is a defined symbol, a type
1567 change warning may still be appropriate. */
252b5132 1568
45d6a902
AM
1569 if (h->root.type == bfd_link_hash_common)
1570 *type_change_ok = TRUE;
1571 }
1572
1573 /* Handle the special case of an old common symbol merging with a
1574 new symbol which looks like a common symbol in a shared object.
1575 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1576 common symbol, and let _bfd_generic_link_add_one_symbol do the
1577 right thing. */
45d6a902
AM
1578
1579 if (newdyncommon
1580 && h->root.type == bfd_link_hash_common)
1581 {
1582 *override = TRUE;
1583 newdef = FALSE;
1584 newdyncommon = FALSE;
1585 *pvalue = sym->st_size;
a4d8e49b 1586 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1587 *size_change_ok = TRUE;
1588 }
1589
c5e2cead 1590 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1591 if (newdef && olddef && newweak)
54ac0771 1592 {
35ed3f94 1593 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1594 if (!(oldbfd != NULL
1595 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1596 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1597 {
1598 newdef = FALSE;
1599 *skip = TRUE;
1600 }
54ac0771
L
1601
1602 /* Merge st_other. If the symbol already has a dynamic index,
1603 but visibility says it should not be visible, turn it into a
1604 local symbol. */
b8417128 1605 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1606 if (h->dynindx != -1)
1607 switch (ELF_ST_VISIBILITY (h->other))
1608 {
1609 case STV_INTERNAL:
1610 case STV_HIDDEN:
1611 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1612 break;
1613 }
1614 }
c5e2cead 1615
45d6a902
AM
1616 /* If the old symbol is from a dynamic object, and the new symbol is
1617 a definition which is not from a dynamic object, then the new
1618 symbol overrides the old symbol. Symbols from regular files
1619 always take precedence over symbols from dynamic objects, even if
1620 they are defined after the dynamic object in the link.
1621
1622 As above, we again permit a common symbol in a regular object to
1623 override a definition in a shared object if the shared object
0f8a2703 1624 symbol is a function or is weak. */
45d6a902
AM
1625
1626 flip = NULL;
77cfaee6 1627 if (!newdyn
45d6a902
AM
1628 && (newdef
1629 || (bfd_is_com_section (sec)
0a36a439 1630 && (oldweak || oldfunc)))
45d6a902
AM
1631 && olddyn
1632 && olddef
f5385ebf 1633 && h->def_dynamic)
45d6a902
AM
1634 {
1635 /* Change the hash table entry to undefined, and let
1636 _bfd_generic_link_add_one_symbol do the right thing with the
1637 new definition. */
1638
1639 h->root.type = bfd_link_hash_undefined;
1640 h->root.u.undef.abfd = h->root.u.def.section->owner;
1641 *size_change_ok = TRUE;
1642
1643 olddef = FALSE;
1644 olddyncommon = FALSE;
1645
1646 /* We again permit a type change when a common symbol may be
1647 overriding a function. */
1648
1649 if (bfd_is_com_section (sec))
0a36a439
L
1650 {
1651 if (oldfunc)
1652 {
1653 /* If a common symbol overrides a function, make sure
1654 that it isn't defined dynamically nor has type
1655 function. */
1656 h->def_dynamic = 0;
1657 h->type = STT_NOTYPE;
1658 }
1659 *type_change_ok = TRUE;
1660 }
45d6a902 1661
6c9b78e6
AM
1662 if (hi->root.type == bfd_link_hash_indirect)
1663 flip = hi;
45d6a902
AM
1664 else
1665 /* This union may have been set to be non-NULL when this symbol
1666 was seen in a dynamic object. We must force the union to be
1667 NULL, so that it is correct for a regular symbol. */
1668 h->verinfo.vertree = NULL;
1669 }
1670
1671 /* Handle the special case of a new common symbol merging with an
1672 old symbol that looks like it might be a common symbol defined in
1673 a shared object. Note that we have already handled the case in
1674 which a new common symbol should simply override the definition
1675 in the shared library. */
1676
1677 if (! newdyn
1678 && bfd_is_com_section (sec)
1679 && olddyncommon)
1680 {
1681 /* It would be best if we could set the hash table entry to a
1682 common symbol, but we don't know what to use for the section
1683 or the alignment. */
1a72702b
AM
1684 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1685 bfd_link_hash_common, sym->st_size);
45d6a902 1686
4cc11e76 1687 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1688 larger, pretend that the new symbol has its size. */
1689
1690 if (h->size > *pvalue)
1691 *pvalue = h->size;
1692
af44c138
L
1693 /* We need to remember the alignment required by the symbol
1694 in the dynamic object. */
1695 BFD_ASSERT (pold_alignment);
1696 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1697
1698 olddef = FALSE;
1699 olddyncommon = FALSE;
1700
1701 h->root.type = bfd_link_hash_undefined;
1702 h->root.u.undef.abfd = h->root.u.def.section->owner;
1703
1704 *size_change_ok = TRUE;
1705 *type_change_ok = TRUE;
1706
6c9b78e6
AM
1707 if (hi->root.type == bfd_link_hash_indirect)
1708 flip = hi;
45d6a902
AM
1709 else
1710 h->verinfo.vertree = NULL;
1711 }
1712
1713 if (flip != NULL)
1714 {
1715 /* Handle the case where we had a versioned symbol in a dynamic
1716 library and now find a definition in a normal object. In this
1717 case, we make the versioned symbol point to the normal one. */
45d6a902 1718 flip->root.type = h->root.type;
00cbee0a 1719 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1720 h->root.type = bfd_link_hash_indirect;
1721 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1722 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1723 if (h->def_dynamic)
45d6a902 1724 {
f5385ebf
AM
1725 h->def_dynamic = 0;
1726 flip->ref_dynamic = 1;
45d6a902
AM
1727 }
1728 }
1729
45d6a902
AM
1730 return TRUE;
1731}
1732
1733/* This function is called to create an indirect symbol from the
1734 default for the symbol with the default version if needed. The
4f3fedcf 1735 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1736 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1737
28caa186 1738static bfd_boolean
268b6b39
AM
1739_bfd_elf_add_default_symbol (bfd *abfd,
1740 struct bfd_link_info *info,
1741 struct elf_link_hash_entry *h,
1742 const char *name,
1743 Elf_Internal_Sym *sym,
4f3fedcf
AM
1744 asection *sec,
1745 bfd_vma value,
1746 bfd **poldbfd,
e3c9d234 1747 bfd_boolean *dynsym)
45d6a902
AM
1748{
1749 bfd_boolean type_change_ok;
1750 bfd_boolean size_change_ok;
1751 bfd_boolean skip;
1752 char *shortname;
1753 struct elf_link_hash_entry *hi;
1754 struct bfd_link_hash_entry *bh;
9c5bfbb7 1755 const struct elf_backend_data *bed;
45d6a902
AM
1756 bfd_boolean collect;
1757 bfd_boolean dynamic;
e3c9d234 1758 bfd_boolean override;
45d6a902
AM
1759 char *p;
1760 size_t len, shortlen;
ffd65175 1761 asection *tmp_sec;
6e33951e 1762 bfd_boolean matched;
45d6a902 1763
422f1182
L
1764 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1765 return TRUE;
1766
45d6a902
AM
1767 /* If this symbol has a version, and it is the default version, we
1768 create an indirect symbol from the default name to the fully
1769 decorated name. This will cause external references which do not
1770 specify a version to be bound to this version of the symbol. */
1771 p = strchr (name, ELF_VER_CHR);
422f1182
L
1772 if (h->versioned == unknown)
1773 {
1774 if (p == NULL)
1775 {
1776 h->versioned = unversioned;
1777 return TRUE;
1778 }
1779 else
1780 {
1781 if (p[1] != ELF_VER_CHR)
1782 {
1783 h->versioned = versioned_hidden;
1784 return TRUE;
1785 }
1786 else
1787 h->versioned = versioned;
1788 }
1789 }
4373f8af
L
1790 else
1791 {
1792 /* PR ld/19073: We may see an unversioned definition after the
1793 default version. */
1794 if (p == NULL)
1795 return TRUE;
1796 }
45d6a902 1797
45d6a902
AM
1798 bed = get_elf_backend_data (abfd);
1799 collect = bed->collect;
1800 dynamic = (abfd->flags & DYNAMIC) != 0;
1801
1802 shortlen = p - name;
a50b1753 1803 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1804 if (shortname == NULL)
1805 return FALSE;
1806 memcpy (shortname, name, shortlen);
1807 shortname[shortlen] = '\0';
1808
1809 /* We are going to create a new symbol. Merge it with any existing
1810 symbol with this name. For the purposes of the merge, act as
1811 though we were defining the symbol we just defined, although we
1812 actually going to define an indirect symbol. */
1813 type_change_ok = FALSE;
1814 size_change_ok = FALSE;
6e33951e 1815 matched = TRUE;
ffd65175
AM
1816 tmp_sec = sec;
1817 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1818 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1819 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1820 return FALSE;
1821
1822 if (skip)
1823 goto nondefault;
1824
5b677558
AM
1825 if (hi->def_regular)
1826 {
1827 /* If the undecorated symbol will have a version added by a
1828 script different to H, then don't indirect to/from the
1829 undecorated symbol. This isn't ideal because we may not yet
1830 have seen symbol versions, if given by a script on the
1831 command line rather than via --version-script. */
1832 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1833 {
1834 bfd_boolean hide;
1835
1836 hi->verinfo.vertree
1837 = bfd_find_version_for_sym (info->version_info,
1838 hi->root.root.string, &hide);
1839 if (hi->verinfo.vertree != NULL && hide)
1840 {
1841 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1842 goto nondefault;
1843 }
1844 }
1845 if (hi->verinfo.vertree != NULL
1846 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1847 goto nondefault;
1848 }
1849
45d6a902
AM
1850 if (! override)
1851 {
c6e8a9a8 1852 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1853 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1854 {
1855 bh = &hi->root;
1856 if (! (_bfd_generic_link_add_one_symbol
1857 (info, abfd, shortname, BSF_INDIRECT,
1858 bfd_ind_section_ptr,
1859 0, name, FALSE, collect, &bh)))
1860 return FALSE;
1861 hi = (struct elf_link_hash_entry *) bh;
1862 }
45d6a902
AM
1863 }
1864 else
1865 {
1866 /* In this case the symbol named SHORTNAME is overriding the
1867 indirect symbol we want to add. We were planning on making
1868 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1869 is the name without a version. NAME is the fully versioned
1870 name, and it is the default version.
1871
1872 Overriding means that we already saw a definition for the
1873 symbol SHORTNAME in a regular object, and it is overriding
1874 the symbol defined in the dynamic object.
1875
1876 When this happens, we actually want to change NAME, the
1877 symbol we just added, to refer to SHORTNAME. This will cause
1878 references to NAME in the shared object to become references
1879 to SHORTNAME in the regular object. This is what we expect
1880 when we override a function in a shared object: that the
1881 references in the shared object will be mapped to the
1882 definition in the regular object. */
1883
1884 while (hi->root.type == bfd_link_hash_indirect
1885 || hi->root.type == bfd_link_hash_warning)
1886 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1887
1888 h->root.type = bfd_link_hash_indirect;
1889 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1890 if (h->def_dynamic)
45d6a902 1891 {
f5385ebf
AM
1892 h->def_dynamic = 0;
1893 hi->ref_dynamic = 1;
1894 if (hi->ref_regular
1895 || hi->def_regular)
45d6a902 1896 {
c152c796 1897 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1898 return FALSE;
1899 }
1900 }
1901
1902 /* Now set HI to H, so that the following code will set the
1903 other fields correctly. */
1904 hi = h;
1905 }
1906
fab4a87f
L
1907 /* Check if HI is a warning symbol. */
1908 if (hi->root.type == bfd_link_hash_warning)
1909 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1910
45d6a902
AM
1911 /* If there is a duplicate definition somewhere, then HI may not
1912 point to an indirect symbol. We will have reported an error to
1913 the user in that case. */
1914
1915 if (hi->root.type == bfd_link_hash_indirect)
1916 {
1917 struct elf_link_hash_entry *ht;
1918
45d6a902 1919 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1920 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1921
68c88cd4
AM
1922 /* A reference to the SHORTNAME symbol from a dynamic library
1923 will be satisfied by the versioned symbol at runtime. In
1924 effect, we have a reference to the versioned symbol. */
1925 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1926 hi->dynamic_def |= ht->dynamic_def;
1927
45d6a902
AM
1928 /* See if the new flags lead us to realize that the symbol must
1929 be dynamic. */
1930 if (! *dynsym)
1931 {
1932 if (! dynamic)
1933 {
0e1862bb 1934 if (! bfd_link_executable (info)
90c984fc 1935 || hi->def_dynamic
f5385ebf 1936 || hi->ref_dynamic)
45d6a902
AM
1937 *dynsym = TRUE;
1938 }
1939 else
1940 {
f5385ebf 1941 if (hi->ref_regular)
45d6a902
AM
1942 *dynsym = TRUE;
1943 }
1944 }
1945 }
1946
1947 /* We also need to define an indirection from the nondefault version
1948 of the symbol. */
1949
1950nondefault:
1951 len = strlen (name);
a50b1753 1952 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1953 if (shortname == NULL)
1954 return FALSE;
1955 memcpy (shortname, name, shortlen);
1956 memcpy (shortname + shortlen, p + 1, len - shortlen);
1957
1958 /* Once again, merge with any existing symbol. */
1959 type_change_ok = FALSE;
1960 size_change_ok = FALSE;
ffd65175
AM
1961 tmp_sec = sec;
1962 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1963 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1964 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1965 return FALSE;
1966
1967 if (skip)
1968 return TRUE;
1969
1970 if (override)
1971 {
1972 /* Here SHORTNAME is a versioned name, so we don't expect to see
1973 the type of override we do in the case above unless it is
4cc11e76 1974 overridden by a versioned definition. */
45d6a902
AM
1975 if (hi->root.type != bfd_link_hash_defined
1976 && hi->root.type != bfd_link_hash_defweak)
4eca0228 1977 _bfd_error_handler
695344c0 1978 /* xgettext:c-format */
d003868e
AM
1979 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1980 abfd, shortname);
45d6a902
AM
1981 }
1982 else
1983 {
1984 bh = &hi->root;
1985 if (! (_bfd_generic_link_add_one_symbol
1986 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1987 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1988 return FALSE;
1989 hi = (struct elf_link_hash_entry *) bh;
1990
1991 /* If there is a duplicate definition somewhere, then HI may not
1992 point to an indirect symbol. We will have reported an error
1993 to the user in that case. */
1994
1995 if (hi->root.type == bfd_link_hash_indirect)
1996 {
fcfa13d2 1997 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1998 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1999 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2000
2001 /* See if the new flags lead us to realize that the symbol
2002 must be dynamic. */
2003 if (! *dynsym)
2004 {
2005 if (! dynamic)
2006 {
0e1862bb 2007 if (! bfd_link_executable (info)
f5385ebf 2008 || hi->ref_dynamic)
45d6a902
AM
2009 *dynsym = TRUE;
2010 }
2011 else
2012 {
f5385ebf 2013 if (hi->ref_regular)
45d6a902
AM
2014 *dynsym = TRUE;
2015 }
2016 }
2017 }
2018 }
2019
2020 return TRUE;
2021}
2022\f
2023/* This routine is used to export all defined symbols into the dynamic
2024 symbol table. It is called via elf_link_hash_traverse. */
2025
28caa186 2026static bfd_boolean
268b6b39 2027_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2028{
a50b1753 2029 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2030
2031 /* Ignore indirect symbols. These are added by the versioning code. */
2032 if (h->root.type == bfd_link_hash_indirect)
2033 return TRUE;
2034
7686d77d
AM
2035 /* Ignore this if we won't export it. */
2036 if (!eif->info->export_dynamic && !h->dynamic)
2037 return TRUE;
45d6a902
AM
2038
2039 if (h->dynindx == -1
fd91d419
L
2040 && (h->def_regular || h->ref_regular)
2041 && ! bfd_hide_sym_by_version (eif->info->version_info,
2042 h->root.root.string))
45d6a902 2043 {
fd91d419 2044 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2045 {
fd91d419
L
2046 eif->failed = TRUE;
2047 return FALSE;
45d6a902
AM
2048 }
2049 }
2050
2051 return TRUE;
2052}
2053\f
2054/* Look through the symbols which are defined in other shared
2055 libraries and referenced here. Update the list of version
2056 dependencies. This will be put into the .gnu.version_r section.
2057 This function is called via elf_link_hash_traverse. */
2058
28caa186 2059static bfd_boolean
268b6b39
AM
2060_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2061 void *data)
45d6a902 2062{
a50b1753 2063 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2064 Elf_Internal_Verneed *t;
2065 Elf_Internal_Vernaux *a;
2066 bfd_size_type amt;
2067
45d6a902
AM
2068 /* We only care about symbols defined in shared objects with version
2069 information. */
f5385ebf
AM
2070 if (!h->def_dynamic
2071 || h->def_regular
45d6a902 2072 || h->dynindx == -1
7b20f099
AM
2073 || h->verinfo.verdef == NULL
2074 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2075 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2076 return TRUE;
2077
2078 /* See if we already know about this version. */
28caa186
AM
2079 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2080 t != NULL;
2081 t = t->vn_nextref)
45d6a902
AM
2082 {
2083 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2084 continue;
2085
2086 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2087 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2088 return TRUE;
2089
2090 break;
2091 }
2092
2093 /* This is a new version. Add it to tree we are building. */
2094
2095 if (t == NULL)
2096 {
2097 amt = sizeof *t;
a50b1753 2098 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2099 if (t == NULL)
2100 {
2101 rinfo->failed = TRUE;
2102 return FALSE;
2103 }
2104
2105 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2106 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2107 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2108 }
2109
2110 amt = sizeof *a;
a50b1753 2111 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2112 if (a == NULL)
2113 {
2114 rinfo->failed = TRUE;
2115 return FALSE;
2116 }
45d6a902
AM
2117
2118 /* Note that we are copying a string pointer here, and testing it
2119 above. If bfd_elf_string_from_elf_section is ever changed to
2120 discard the string data when low in memory, this will have to be
2121 fixed. */
2122 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2123
2124 a->vna_flags = h->verinfo.verdef->vd_flags;
2125 a->vna_nextptr = t->vn_auxptr;
2126
2127 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2128 ++rinfo->vers;
2129
2130 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2131
2132 t->vn_auxptr = a;
2133
2134 return TRUE;
2135}
2136
2137/* Figure out appropriate versions for all the symbols. We may not
2138 have the version number script until we have read all of the input
2139 files, so until that point we don't know which symbols should be
2140 local. This function is called via elf_link_hash_traverse. */
2141
28caa186 2142static bfd_boolean
268b6b39 2143_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2144{
28caa186 2145 struct elf_info_failed *sinfo;
45d6a902 2146 struct bfd_link_info *info;
9c5bfbb7 2147 const struct elf_backend_data *bed;
45d6a902
AM
2148 struct elf_info_failed eif;
2149 char *p;
45d6a902 2150
a50b1753 2151 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2152 info = sinfo->info;
2153
45d6a902
AM
2154 /* Fix the symbol flags. */
2155 eif.failed = FALSE;
2156 eif.info = info;
2157 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2158 {
2159 if (eif.failed)
2160 sinfo->failed = TRUE;
2161 return FALSE;
2162 }
2163
2164 /* We only need version numbers for symbols defined in regular
2165 objects. */
f5385ebf 2166 if (!h->def_regular)
45d6a902
AM
2167 return TRUE;
2168
28caa186 2169 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2170 p = strchr (h->root.root.string, ELF_VER_CHR);
2171 if (p != NULL && h->verinfo.vertree == NULL)
2172 {
2173 struct bfd_elf_version_tree *t;
45d6a902 2174
45d6a902
AM
2175 ++p;
2176 if (*p == ELF_VER_CHR)
6e33951e 2177 ++p;
45d6a902
AM
2178
2179 /* If there is no version string, we can just return out. */
2180 if (*p == '\0')
6e33951e 2181 return TRUE;
45d6a902
AM
2182
2183 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2184 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2185 {
2186 if (strcmp (t->name, p) == 0)
2187 {
2188 size_t len;
2189 char *alc;
2190 struct bfd_elf_version_expr *d;
2191
2192 len = p - h->root.root.string;
a50b1753 2193 alc = (char *) bfd_malloc (len);
45d6a902 2194 if (alc == NULL)
14b1c01e
AM
2195 {
2196 sinfo->failed = TRUE;
2197 return FALSE;
2198 }
45d6a902
AM
2199 memcpy (alc, h->root.root.string, len - 1);
2200 alc[len - 1] = '\0';
2201 if (alc[len - 2] == ELF_VER_CHR)
2202 alc[len - 2] = '\0';
2203
2204 h->verinfo.vertree = t;
2205 t->used = TRUE;
2206 d = NULL;
2207
108ba305
JJ
2208 if (t->globals.list != NULL)
2209 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2210
2211 /* See if there is anything to force this symbol to
2212 local scope. */
108ba305 2213 if (d == NULL && t->locals.list != NULL)
45d6a902 2214 {
108ba305
JJ
2215 d = (*t->match) (&t->locals, NULL, alc);
2216 if (d != NULL
2217 && h->dynindx != -1
108ba305
JJ
2218 && ! info->export_dynamic)
2219 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2220 }
2221
2222 free (alc);
2223 break;
2224 }
2225 }
2226
2227 /* If we are building an application, we need to create a
2228 version node for this version. */
0e1862bb 2229 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2230 {
2231 struct bfd_elf_version_tree **pp;
2232 int version_index;
2233
2234 /* If we aren't going to export this symbol, we don't need
2235 to worry about it. */
2236 if (h->dynindx == -1)
2237 return TRUE;
2238
ef53be89
AM
2239 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2240 sizeof *t);
45d6a902
AM
2241 if (t == NULL)
2242 {
2243 sinfo->failed = TRUE;
2244 return FALSE;
2245 }
2246
45d6a902 2247 t->name = p;
45d6a902
AM
2248 t->name_indx = (unsigned int) -1;
2249 t->used = TRUE;
2250
2251 version_index = 1;
2252 /* Don't count anonymous version tag. */
fd91d419
L
2253 if (sinfo->info->version_info != NULL
2254 && sinfo->info->version_info->vernum == 0)
45d6a902 2255 version_index = 0;
fd91d419
L
2256 for (pp = &sinfo->info->version_info;
2257 *pp != NULL;
2258 pp = &(*pp)->next)
45d6a902
AM
2259 ++version_index;
2260 t->vernum = version_index;
2261
2262 *pp = t;
2263
2264 h->verinfo.vertree = t;
2265 }
2266 else if (t == NULL)
2267 {
2268 /* We could not find the version for a symbol when
2269 generating a shared archive. Return an error. */
4eca0228 2270 _bfd_error_handler
695344c0 2271 /* xgettext:c-format */
c55fe096 2272 (_("%B: version node not found for symbol %s"),
28caa186 2273 info->output_bfd, h->root.root.string);
45d6a902
AM
2274 bfd_set_error (bfd_error_bad_value);
2275 sinfo->failed = TRUE;
2276 return FALSE;
2277 }
45d6a902
AM
2278 }
2279
2280 /* If we don't have a version for this symbol, see if we can find
2281 something. */
fd91d419 2282 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2283 {
1e8fa21e 2284 bfd_boolean hide;
ae5a3597 2285
fd91d419
L
2286 h->verinfo.vertree
2287 = bfd_find_version_for_sym (sinfo->info->version_info,
2288 h->root.root.string, &hide);
1e8fa21e
AM
2289 if (h->verinfo.vertree != NULL && hide)
2290 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2291 }
2292
2293 return TRUE;
2294}
2295\f
45d6a902
AM
2296/* Read and swap the relocs from the section indicated by SHDR. This
2297 may be either a REL or a RELA section. The relocations are
2298 translated into RELA relocations and stored in INTERNAL_RELOCS,
2299 which should have already been allocated to contain enough space.
2300 The EXTERNAL_RELOCS are a buffer where the external form of the
2301 relocations should be stored.
2302
2303 Returns FALSE if something goes wrong. */
2304
2305static bfd_boolean
268b6b39 2306elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2307 asection *sec,
268b6b39
AM
2308 Elf_Internal_Shdr *shdr,
2309 void *external_relocs,
2310 Elf_Internal_Rela *internal_relocs)
45d6a902 2311{
9c5bfbb7 2312 const struct elf_backend_data *bed;
268b6b39 2313 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2314 const bfd_byte *erela;
2315 const bfd_byte *erelaend;
2316 Elf_Internal_Rela *irela;
243ef1e0
L
2317 Elf_Internal_Shdr *symtab_hdr;
2318 size_t nsyms;
45d6a902 2319
45d6a902
AM
2320 /* Position ourselves at the start of the section. */
2321 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2322 return FALSE;
2323
2324 /* Read the relocations. */
2325 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2326 return FALSE;
2327
243ef1e0 2328 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2329 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2330
45d6a902
AM
2331 bed = get_elf_backend_data (abfd);
2332
2333 /* Convert the external relocations to the internal format. */
2334 if (shdr->sh_entsize == bed->s->sizeof_rel)
2335 swap_in = bed->s->swap_reloc_in;
2336 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2337 swap_in = bed->s->swap_reloca_in;
2338 else
2339 {
2340 bfd_set_error (bfd_error_wrong_format);
2341 return FALSE;
2342 }
2343
a50b1753 2344 erela = (const bfd_byte *) external_relocs;
51992aec 2345 erelaend = erela + shdr->sh_size;
45d6a902
AM
2346 irela = internal_relocs;
2347 while (erela < erelaend)
2348 {
243ef1e0
L
2349 bfd_vma r_symndx;
2350
45d6a902 2351 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2352 r_symndx = ELF32_R_SYM (irela->r_info);
2353 if (bed->s->arch_size == 64)
2354 r_symndx >>= 24;
ce98a316
NC
2355 if (nsyms > 0)
2356 {
2357 if ((size_t) r_symndx >= nsyms)
2358 {
4eca0228 2359 _bfd_error_handler
695344c0 2360 /* xgettext:c-format */
ce98a316
NC
2361 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2362 " for offset 0x%lx in section `%A'"),
2363 abfd, sec,
2364 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2365 bfd_set_error (bfd_error_bad_value);
2366 return FALSE;
2367 }
2368 }
cf35638d 2369 else if (r_symndx != STN_UNDEF)
243ef1e0 2370 {
4eca0228 2371 _bfd_error_handler
695344c0 2372 /* xgettext:c-format */
ce98a316
NC
2373 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2374 " when the object file has no symbol table"),
d003868e
AM
2375 abfd, sec,
2376 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2377 bfd_set_error (bfd_error_bad_value);
2378 return FALSE;
2379 }
45d6a902
AM
2380 irela += bed->s->int_rels_per_ext_rel;
2381 erela += shdr->sh_entsize;
2382 }
2383
2384 return TRUE;
2385}
2386
2387/* Read and swap the relocs for a section O. They may have been
2388 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2389 not NULL, they are used as buffers to read into. They are known to
2390 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2391 the return value is allocated using either malloc or bfd_alloc,
2392 according to the KEEP_MEMORY argument. If O has two relocation
2393 sections (both REL and RELA relocations), then the REL_HDR
2394 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2395 RELA_HDR relocations. */
45d6a902
AM
2396
2397Elf_Internal_Rela *
268b6b39
AM
2398_bfd_elf_link_read_relocs (bfd *abfd,
2399 asection *o,
2400 void *external_relocs,
2401 Elf_Internal_Rela *internal_relocs,
2402 bfd_boolean keep_memory)
45d6a902 2403{
268b6b39 2404 void *alloc1 = NULL;
45d6a902 2405 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2406 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2407 struct bfd_elf_section_data *esdo = elf_section_data (o);
2408 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2409
d4730f92
BS
2410 if (esdo->relocs != NULL)
2411 return esdo->relocs;
45d6a902
AM
2412
2413 if (o->reloc_count == 0)
2414 return NULL;
2415
45d6a902
AM
2416 if (internal_relocs == NULL)
2417 {
2418 bfd_size_type size;
2419
2420 size = o->reloc_count;
2421 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2422 if (keep_memory)
a50b1753 2423 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2424 else
a50b1753 2425 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2426 if (internal_relocs == NULL)
2427 goto error_return;
2428 }
2429
2430 if (external_relocs == NULL)
2431 {
d4730f92
BS
2432 bfd_size_type size = 0;
2433
2434 if (esdo->rel.hdr)
2435 size += esdo->rel.hdr->sh_size;
2436 if (esdo->rela.hdr)
2437 size += esdo->rela.hdr->sh_size;
45d6a902 2438
268b6b39 2439 alloc1 = bfd_malloc (size);
45d6a902
AM
2440 if (alloc1 == NULL)
2441 goto error_return;
2442 external_relocs = alloc1;
2443 }
2444
d4730f92
BS
2445 internal_rela_relocs = internal_relocs;
2446 if (esdo->rel.hdr)
2447 {
2448 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2449 external_relocs,
2450 internal_relocs))
2451 goto error_return;
2452 external_relocs = (((bfd_byte *) external_relocs)
2453 + esdo->rel.hdr->sh_size);
2454 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2455 * bed->s->int_rels_per_ext_rel);
2456 }
2457
2458 if (esdo->rela.hdr
2459 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2460 external_relocs,
2461 internal_rela_relocs)))
45d6a902
AM
2462 goto error_return;
2463
2464 /* Cache the results for next time, if we can. */
2465 if (keep_memory)
d4730f92 2466 esdo->relocs = internal_relocs;
45d6a902
AM
2467
2468 if (alloc1 != NULL)
2469 free (alloc1);
2470
2471 /* Don't free alloc2, since if it was allocated we are passing it
2472 back (under the name of internal_relocs). */
2473
2474 return internal_relocs;
2475
2476 error_return:
2477 if (alloc1 != NULL)
2478 free (alloc1);
2479 if (alloc2 != NULL)
4dd07732
AM
2480 {
2481 if (keep_memory)
2482 bfd_release (abfd, alloc2);
2483 else
2484 free (alloc2);
2485 }
45d6a902
AM
2486 return NULL;
2487}
2488
2489/* Compute the size of, and allocate space for, REL_HDR which is the
2490 section header for a section containing relocations for O. */
2491
28caa186 2492static bfd_boolean
9eaff861
AO
2493_bfd_elf_link_size_reloc_section (bfd *abfd,
2494 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2495{
9eaff861 2496 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2497
2498 /* That allows us to calculate the size of the section. */
9eaff861 2499 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2500
2501 /* The contents field must last into write_object_contents, so we
2502 allocate it with bfd_alloc rather than malloc. Also since we
2503 cannot be sure that the contents will actually be filled in,
2504 we zero the allocated space. */
a50b1753 2505 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2506 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2507 return FALSE;
2508
d4730f92 2509 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2510 {
2511 struct elf_link_hash_entry **p;
2512
ca4be51c
AM
2513 p = ((struct elf_link_hash_entry **)
2514 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2515 if (p == NULL)
2516 return FALSE;
2517
d4730f92 2518 reldata->hashes = p;
45d6a902
AM
2519 }
2520
2521 return TRUE;
2522}
2523
2524/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2525 originated from the section given by INPUT_REL_HDR) to the
2526 OUTPUT_BFD. */
2527
2528bfd_boolean
268b6b39
AM
2529_bfd_elf_link_output_relocs (bfd *output_bfd,
2530 asection *input_section,
2531 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2532 Elf_Internal_Rela *internal_relocs,
2533 struct elf_link_hash_entry **rel_hash
2534 ATTRIBUTE_UNUSED)
45d6a902
AM
2535{
2536 Elf_Internal_Rela *irela;
2537 Elf_Internal_Rela *irelaend;
2538 bfd_byte *erel;
d4730f92 2539 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2540 asection *output_section;
9c5bfbb7 2541 const struct elf_backend_data *bed;
268b6b39 2542 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2543 struct bfd_elf_section_data *esdo;
45d6a902
AM
2544
2545 output_section = input_section->output_section;
45d6a902 2546
d4730f92
BS
2547 bed = get_elf_backend_data (output_bfd);
2548 esdo = elf_section_data (output_section);
2549 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2550 {
d4730f92
BS
2551 output_reldata = &esdo->rel;
2552 swap_out = bed->s->swap_reloc_out;
45d6a902 2553 }
d4730f92
BS
2554 else if (esdo->rela.hdr
2555 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2556 {
d4730f92
BS
2557 output_reldata = &esdo->rela;
2558 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2559 }
2560 else
2561 {
4eca0228 2562 _bfd_error_handler
695344c0 2563 /* xgettext:c-format */
d003868e
AM
2564 (_("%B: relocation size mismatch in %B section %A"),
2565 output_bfd, input_section->owner, input_section);
297d8443 2566 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2567 return FALSE;
2568 }
2569
d4730f92
BS
2570 erel = output_reldata->hdr->contents;
2571 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2572 irela = internal_relocs;
2573 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2574 * bed->s->int_rels_per_ext_rel);
2575 while (irela < irelaend)
2576 {
2577 (*swap_out) (output_bfd, irela, erel);
2578 irela += bed->s->int_rels_per_ext_rel;
2579 erel += input_rel_hdr->sh_entsize;
2580 }
2581
2582 /* Bump the counter, so that we know where to add the next set of
2583 relocations. */
d4730f92 2584 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2585
2586 return TRUE;
2587}
2588\f
508c3946
L
2589/* Make weak undefined symbols in PIE dynamic. */
2590
2591bfd_boolean
2592_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2593 struct elf_link_hash_entry *h)
2594{
0e1862bb 2595 if (bfd_link_pie (info)
508c3946
L
2596 && h->dynindx == -1
2597 && h->root.type == bfd_link_hash_undefweak)
2598 return bfd_elf_link_record_dynamic_symbol (info, h);
2599
2600 return TRUE;
2601}
2602
45d6a902
AM
2603/* Fix up the flags for a symbol. This handles various cases which
2604 can only be fixed after all the input files are seen. This is
2605 currently called by both adjust_dynamic_symbol and
2606 assign_sym_version, which is unnecessary but perhaps more robust in
2607 the face of future changes. */
2608
28caa186 2609static bfd_boolean
268b6b39
AM
2610_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2611 struct elf_info_failed *eif)
45d6a902 2612{
33774f08 2613 const struct elf_backend_data *bed;
508c3946 2614
45d6a902
AM
2615 /* If this symbol was mentioned in a non-ELF file, try to set
2616 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2617 permit a non-ELF file to correctly refer to a symbol defined in
2618 an ELF dynamic object. */
f5385ebf 2619 if (h->non_elf)
45d6a902
AM
2620 {
2621 while (h->root.type == bfd_link_hash_indirect)
2622 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2623
2624 if (h->root.type != bfd_link_hash_defined
2625 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2626 {
2627 h->ref_regular = 1;
2628 h->ref_regular_nonweak = 1;
2629 }
45d6a902
AM
2630 else
2631 {
2632 if (h->root.u.def.section->owner != NULL
2633 && (bfd_get_flavour (h->root.u.def.section->owner)
2634 == bfd_target_elf_flavour))
f5385ebf
AM
2635 {
2636 h->ref_regular = 1;
2637 h->ref_regular_nonweak = 1;
2638 }
45d6a902 2639 else
f5385ebf 2640 h->def_regular = 1;
45d6a902
AM
2641 }
2642
2643 if (h->dynindx == -1
f5385ebf
AM
2644 && (h->def_dynamic
2645 || h->ref_dynamic))
45d6a902 2646 {
c152c796 2647 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2648 {
2649 eif->failed = TRUE;
2650 return FALSE;
2651 }
2652 }
2653 }
2654 else
2655 {
f5385ebf 2656 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2657 was first seen in a non-ELF file. Fortunately, if the symbol
2658 was first seen in an ELF file, we're probably OK unless the
2659 symbol was defined in a non-ELF file. Catch that case here.
2660 FIXME: We're still in trouble if the symbol was first seen in
2661 a dynamic object, and then later in a non-ELF regular object. */
2662 if ((h->root.type == bfd_link_hash_defined
2663 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2664 && !h->def_regular
45d6a902
AM
2665 && (h->root.u.def.section->owner != NULL
2666 ? (bfd_get_flavour (h->root.u.def.section->owner)
2667 != bfd_target_elf_flavour)
2668 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2669 && !h->def_dynamic)))
2670 h->def_regular = 1;
45d6a902
AM
2671 }
2672
508c3946 2673 /* Backend specific symbol fixup. */
33774f08
AM
2674 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2675 if (bed->elf_backend_fixup_symbol
2676 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2677 return FALSE;
508c3946 2678
45d6a902
AM
2679 /* If this is a final link, and the symbol was defined as a common
2680 symbol in a regular object file, and there was no definition in
2681 any dynamic object, then the linker will have allocated space for
f5385ebf 2682 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2683 flag will not have been set. */
2684 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2685 && !h->def_regular
2686 && h->ref_regular
2687 && !h->def_dynamic
96f29d96 2688 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2689 h->def_regular = 1;
45d6a902 2690
4deb8f71
L
2691 /* If a weak undefined symbol has non-default visibility, we also
2692 hide it from the dynamic linker. */
2693 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2694 && h->root.type == bfd_link_hash_undefweak)
2695 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2696
2697 /* A hidden versioned symbol in executable should be forced local if
2698 it is is locally defined, not referenced by shared library and not
2699 exported. */
2700 else if (bfd_link_executable (eif->info)
2701 && h->versioned == versioned_hidden
2702 && !eif->info->export_dynamic
2703 && !h->dynamic
2704 && !h->ref_dynamic
2705 && h->def_regular)
2706 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2707
45d6a902
AM
2708 /* If -Bsymbolic was used (which means to bind references to global
2709 symbols to the definition within the shared object), and this
2710 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2711 need a PLT entry. Likewise, if the symbol has non-default
2712 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2713 will force it local. */
4deb8f71
L
2714 else if (h->needs_plt
2715 && bfd_link_pic (eif->info)
2716 && is_elf_hash_table (eif->info->hash)
2717 && (SYMBOLIC_BIND (eif->info, h)
2718 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2719 && h->def_regular)
45d6a902 2720 {
45d6a902
AM
2721 bfd_boolean force_local;
2722
45d6a902
AM
2723 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2724 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2725 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2726 }
2727
45d6a902
AM
2728 /* If this is a weak defined symbol in a dynamic object, and we know
2729 the real definition in the dynamic object, copy interesting flags
2730 over to the real definition. */
f6e332e6 2731 if (h->u.weakdef != NULL)
45d6a902 2732 {
45d6a902
AM
2733 /* If the real definition is defined by a regular object file,
2734 don't do anything special. See the longer description in
2735 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2736 if (h->u.weakdef->def_regular)
f6e332e6 2737 h->u.weakdef = NULL;
45d6a902 2738 else
a26587ba 2739 {
4e6b54a6
AM
2740 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2741
2742 while (h->root.type == bfd_link_hash_indirect)
2743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2744
2745 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2746 || h->root.type == bfd_link_hash_defweak);
2747 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2748 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2749 || weakdef->root.type == bfd_link_hash_defweak);
2750 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2751 }
45d6a902
AM
2752 }
2753
2754 return TRUE;
2755}
2756
2757/* Make the backend pick a good value for a dynamic symbol. This is
2758 called via elf_link_hash_traverse, and also calls itself
2759 recursively. */
2760
28caa186 2761static bfd_boolean
268b6b39 2762_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2763{
a50b1753 2764 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2765 bfd *dynobj;
9c5bfbb7 2766 const struct elf_backend_data *bed;
45d6a902 2767
0eddce27 2768 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2769 return FALSE;
2770
45d6a902
AM
2771 /* Ignore indirect symbols. These are added by the versioning code. */
2772 if (h->root.type == bfd_link_hash_indirect)
2773 return TRUE;
2774
2775 /* Fix the symbol flags. */
2776 if (! _bfd_elf_fix_symbol_flags (h, eif))
2777 return FALSE;
2778
2779 /* If this symbol does not require a PLT entry, and it is not
2780 defined by a dynamic object, or is not referenced by a regular
2781 object, ignore it. We do have to handle a weak defined symbol,
2782 even if no regular object refers to it, if we decided to add it
2783 to the dynamic symbol table. FIXME: Do we normally need to worry
2784 about symbols which are defined by one dynamic object and
2785 referenced by another one? */
f5385ebf 2786 if (!h->needs_plt
91e21fb7 2787 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2788 && (h->def_regular
2789 || !h->def_dynamic
2790 || (!h->ref_regular
f6e332e6 2791 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2792 {
a6aa5195 2793 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2794 return TRUE;
2795 }
2796
2797 /* If we've already adjusted this symbol, don't do it again. This
2798 can happen via a recursive call. */
f5385ebf 2799 if (h->dynamic_adjusted)
45d6a902
AM
2800 return TRUE;
2801
2802 /* Don't look at this symbol again. Note that we must set this
2803 after checking the above conditions, because we may look at a
2804 symbol once, decide not to do anything, and then get called
2805 recursively later after REF_REGULAR is set below. */
f5385ebf 2806 h->dynamic_adjusted = 1;
45d6a902
AM
2807
2808 /* If this is a weak definition, and we know a real definition, and
2809 the real symbol is not itself defined by a regular object file,
2810 then get a good value for the real definition. We handle the
2811 real symbol first, for the convenience of the backend routine.
2812
2813 Note that there is a confusing case here. If the real definition
2814 is defined by a regular object file, we don't get the real symbol
2815 from the dynamic object, but we do get the weak symbol. If the
2816 processor backend uses a COPY reloc, then if some routine in the
2817 dynamic object changes the real symbol, we will not see that
2818 change in the corresponding weak symbol. This is the way other
2819 ELF linkers work as well, and seems to be a result of the shared
2820 library model.
2821
2822 I will clarify this issue. Most SVR4 shared libraries define the
2823 variable _timezone and define timezone as a weak synonym. The
2824 tzset call changes _timezone. If you write
2825 extern int timezone;
2826 int _timezone = 5;
2827 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2828 you might expect that, since timezone is a synonym for _timezone,
2829 the same number will print both times. However, if the processor
2830 backend uses a COPY reloc, then actually timezone will be copied
2831 into your process image, and, since you define _timezone
2832 yourself, _timezone will not. Thus timezone and _timezone will
2833 wind up at different memory locations. The tzset call will set
2834 _timezone, leaving timezone unchanged. */
2835
f6e332e6 2836 if (h->u.weakdef != NULL)
45d6a902 2837 {
ec24dc88
AM
2838 /* If we get to this point, there is an implicit reference to
2839 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2840 h->u.weakdef->ref_regular = 1;
45d6a902 2841
ec24dc88
AM
2842 /* Ensure that the backend adjust_dynamic_symbol function sees
2843 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2844 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2845 return FALSE;
2846 }
2847
2848 /* If a symbol has no type and no size and does not require a PLT
2849 entry, then we are probably about to do the wrong thing here: we
2850 are probably going to create a COPY reloc for an empty object.
2851 This case can arise when a shared object is built with assembly
2852 code, and the assembly code fails to set the symbol type. */
2853 if (h->size == 0
2854 && h->type == STT_NOTYPE
f5385ebf 2855 && !h->needs_plt)
4eca0228 2856 _bfd_error_handler
45d6a902
AM
2857 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2858 h->root.root.string);
2859
2860 dynobj = elf_hash_table (eif->info)->dynobj;
2861 bed = get_elf_backend_data (dynobj);
e7c33416 2862
45d6a902
AM
2863 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2864 {
2865 eif->failed = TRUE;
2866 return FALSE;
2867 }
2868
2869 return TRUE;
2870}
2871
027297b7
L
2872/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2873 DYNBSS. */
2874
2875bfd_boolean
6cabe1ea
AM
2876_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2877 struct elf_link_hash_entry *h,
027297b7
L
2878 asection *dynbss)
2879{
91ac5911 2880 unsigned int power_of_two;
027297b7
L
2881 bfd_vma mask;
2882 asection *sec = h->root.u.def.section;
2883
2884 /* The section aligment of definition is the maximum alignment
91ac5911
L
2885 requirement of symbols defined in the section. Since we don't
2886 know the symbol alignment requirement, we start with the
2887 maximum alignment and check low bits of the symbol address
2888 for the minimum alignment. */
2889 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2890 mask = ((bfd_vma) 1 << power_of_two) - 1;
2891 while ((h->root.u.def.value & mask) != 0)
2892 {
2893 mask >>= 1;
2894 --power_of_two;
2895 }
027297b7 2896
91ac5911
L
2897 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2898 dynbss))
027297b7
L
2899 {
2900 /* Adjust the section alignment if needed. */
2901 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2902 power_of_two))
027297b7
L
2903 return FALSE;
2904 }
2905
91ac5911 2906 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2907 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2908
2909 /* Define the symbol as being at this point in DYNBSS. */
2910 h->root.u.def.section = dynbss;
2911 h->root.u.def.value = dynbss->size;
2912
2913 /* Increment the size of DYNBSS to make room for the symbol. */
2914 dynbss->size += h->size;
2915
f7483970
L
2916 /* No error if extern_protected_data is true. */
2917 if (h->protected_def
889c2a67
L
2918 && (!info->extern_protected_data
2919 || (info->extern_protected_data < 0
2920 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2921 info->callbacks->einfo
2922 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2923 h->root.root.string);
6cabe1ea 2924
027297b7
L
2925 return TRUE;
2926}
2927
45d6a902
AM
2928/* Adjust all external symbols pointing into SEC_MERGE sections
2929 to reflect the object merging within the sections. */
2930
28caa186 2931static bfd_boolean
268b6b39 2932_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2933{
2934 asection *sec;
2935
45d6a902
AM
2936 if ((h->root.type == bfd_link_hash_defined
2937 || h->root.type == bfd_link_hash_defweak)
2938 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2939 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2940 {
a50b1753 2941 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2942
2943 h->root.u.def.value =
2944 _bfd_merged_section_offset (output_bfd,
2945 &h->root.u.def.section,
2946 elf_section_data (sec)->sec_info,
753731ee 2947 h->root.u.def.value);
45d6a902
AM
2948 }
2949
2950 return TRUE;
2951}
986a241f
RH
2952
2953/* Returns false if the symbol referred to by H should be considered
2954 to resolve local to the current module, and true if it should be
2955 considered to bind dynamically. */
2956
2957bfd_boolean
268b6b39
AM
2958_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2959 struct bfd_link_info *info,
89a2ee5a 2960 bfd_boolean not_local_protected)
986a241f
RH
2961{
2962 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2963 const struct elf_backend_data *bed;
2964 struct elf_link_hash_table *hash_table;
986a241f
RH
2965
2966 if (h == NULL)
2967 return FALSE;
2968
2969 while (h->root.type == bfd_link_hash_indirect
2970 || h->root.type == bfd_link_hash_warning)
2971 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2972
2973 /* If it was forced local, then clearly it's not dynamic. */
2974 if (h->dynindx == -1)
2975 return FALSE;
f5385ebf 2976 if (h->forced_local)
986a241f
RH
2977 return FALSE;
2978
2979 /* Identify the cases where name binding rules say that a
2980 visible symbol resolves locally. */
0e1862bb
L
2981 binding_stays_local_p = (bfd_link_executable (info)
2982 || SYMBOLIC_BIND (info, h));
986a241f
RH
2983
2984 switch (ELF_ST_VISIBILITY (h->other))
2985 {
2986 case STV_INTERNAL:
2987 case STV_HIDDEN:
2988 return FALSE;
2989
2990 case STV_PROTECTED:
fcb93ecf
PB
2991 hash_table = elf_hash_table (info);
2992 if (!is_elf_hash_table (hash_table))
2993 return FALSE;
2994
2995 bed = get_elf_backend_data (hash_table->dynobj);
2996
986a241f
RH
2997 /* Proper resolution for function pointer equality may require
2998 that these symbols perhaps be resolved dynamically, even though
2999 we should be resolving them to the current module. */
89a2ee5a 3000 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3001 binding_stays_local_p = TRUE;
3002 break;
3003
3004 default:
986a241f
RH
3005 break;
3006 }
3007
aa37626c 3008 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3009 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3010 return TRUE;
3011
986a241f
RH
3012 /* Otherwise, the symbol is dynamic if binding rules don't tell
3013 us that it remains local. */
3014 return !binding_stays_local_p;
3015}
f6c52c13
AM
3016
3017/* Return true if the symbol referred to by H should be considered
3018 to resolve local to the current module, and false otherwise. Differs
3019 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3020 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
3021 for the place where forced_local and dynindx == -1 are tested. If
3022 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3023 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3024 the symbol is local only for defined symbols.
3025 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3026 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3027 treatment of undefined weak symbols. For those that do not make
3028 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3029
3030bfd_boolean
268b6b39
AM
3031_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3032 struct bfd_link_info *info,
3033 bfd_boolean local_protected)
f6c52c13 3034{
fcb93ecf
PB
3035 const struct elf_backend_data *bed;
3036 struct elf_link_hash_table *hash_table;
3037
f6c52c13
AM
3038 /* If it's a local sym, of course we resolve locally. */
3039 if (h == NULL)
3040 return TRUE;
3041
d95edcac
L
3042 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3043 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3044 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3045 return TRUE;
3046
7e2294f9
AO
3047 /* Common symbols that become definitions don't get the DEF_REGULAR
3048 flag set, so test it first, and don't bail out. */
3049 if (ELF_COMMON_DEF_P (h))
3050 /* Do nothing. */;
f6c52c13 3051 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3052 resolve locally. The sym is either undefined or dynamic. */
3053 else if (!h->def_regular)
f6c52c13
AM
3054 return FALSE;
3055
3056 /* Forced local symbols resolve locally. */
f5385ebf 3057 if (h->forced_local)
f6c52c13
AM
3058 return TRUE;
3059
3060 /* As do non-dynamic symbols. */
3061 if (h->dynindx == -1)
3062 return TRUE;
3063
3064 /* At this point, we know the symbol is defined and dynamic. In an
3065 executable it must resolve locally, likewise when building symbolic
3066 shared libraries. */
0e1862bb 3067 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3068 return TRUE;
3069
3070 /* Now deal with defined dynamic symbols in shared libraries. Ones
3071 with default visibility might not resolve locally. */
3072 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3073 return FALSE;
3074
fcb93ecf
PB
3075 hash_table = elf_hash_table (info);
3076 if (!is_elf_hash_table (hash_table))
3077 return TRUE;
3078
3079 bed = get_elf_backend_data (hash_table->dynobj);
3080
f7483970
L
3081 /* If extern_protected_data is false, STV_PROTECTED non-function
3082 symbols are local. */
889c2a67
L
3083 if ((!info->extern_protected_data
3084 || (info->extern_protected_data < 0
3085 && !bed->extern_protected_data))
3086 && !bed->is_function_type (h->type))
1c16dfa5
L
3087 return TRUE;
3088
f6c52c13 3089 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3090 symbols be treated as dynamic symbols. If the address of a
3091 function not defined in an executable is set to that function's
3092 plt entry in the executable, then the address of the function in
3093 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3094 return local_protected;
3095}
e1918d23
AM
3096
3097/* Caches some TLS segment info, and ensures that the TLS segment vma is
3098 aligned. Returns the first TLS output section. */
3099
3100struct bfd_section *
3101_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3102{
3103 struct bfd_section *sec, *tls;
3104 unsigned int align = 0;
3105
3106 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3107 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3108 break;
3109 tls = sec;
3110
3111 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3112 if (sec->alignment_power > align)
3113 align = sec->alignment_power;
3114
3115 elf_hash_table (info)->tls_sec = tls;
3116
3117 /* Ensure the alignment of the first section is the largest alignment,
3118 so that the tls segment starts aligned. */
3119 if (tls != NULL)
3120 tls->alignment_power = align;
3121
3122 return tls;
3123}
0ad989f9
L
3124
3125/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3126static bfd_boolean
3127is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3128 Elf_Internal_Sym *sym)
3129{
a4d8e49b
L
3130 const struct elf_backend_data *bed;
3131
0ad989f9
L
3132 /* Local symbols do not count, but target specific ones might. */
3133 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3134 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3135 return FALSE;
3136
fcb93ecf 3137 bed = get_elf_backend_data (abfd);
0ad989f9 3138 /* Function symbols do not count. */
fcb93ecf 3139 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3140 return FALSE;
3141
3142 /* If the section is undefined, then so is the symbol. */
3143 if (sym->st_shndx == SHN_UNDEF)
3144 return FALSE;
3145
3146 /* If the symbol is defined in the common section, then
3147 it is a common definition and so does not count. */
a4d8e49b 3148 if (bed->common_definition (sym))
0ad989f9
L
3149 return FALSE;
3150
3151 /* If the symbol is in a target specific section then we
3152 must rely upon the backend to tell us what it is. */
3153 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3154 /* FIXME - this function is not coded yet:
3155
3156 return _bfd_is_global_symbol_definition (abfd, sym);
3157
3158 Instead for now assume that the definition is not global,
3159 Even if this is wrong, at least the linker will behave
3160 in the same way that it used to do. */
3161 return FALSE;
3162
3163 return TRUE;
3164}
3165
3166/* Search the symbol table of the archive element of the archive ABFD
3167 whose archive map contains a mention of SYMDEF, and determine if
3168 the symbol is defined in this element. */
3169static bfd_boolean
3170elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3171{
3172 Elf_Internal_Shdr * hdr;
ef53be89
AM
3173 size_t symcount;
3174 size_t extsymcount;
3175 size_t extsymoff;
0ad989f9
L
3176 Elf_Internal_Sym *isymbuf;
3177 Elf_Internal_Sym *isym;
3178 Elf_Internal_Sym *isymend;
3179 bfd_boolean result;
3180
3181 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3182 if (abfd == NULL)
3183 return FALSE;
3184
3185 if (! bfd_check_format (abfd, bfd_object))
3186 return FALSE;
3187
7dc3990e
L
3188 /* Select the appropriate symbol table. If we don't know if the
3189 object file is an IR object, give linker LTO plugin a chance to
3190 get the correct symbol table. */
3191 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3192#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3193 || (abfd->plugin_format == bfd_plugin_unknown
3194 && bfd_link_plugin_object_p (abfd))
3195#endif
3196 )
3197 {
3198 /* Use the IR symbol table if the object has been claimed by
3199 plugin. */
3200 abfd = abfd->plugin_dummy_bfd;
3201 hdr = &elf_tdata (abfd)->symtab_hdr;
3202 }
3203 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3204 hdr = &elf_tdata (abfd)->symtab_hdr;
3205 else
3206 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3207
3208 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3209
3210 /* The sh_info field of the symtab header tells us where the
3211 external symbols start. We don't care about the local symbols. */
3212 if (elf_bad_symtab (abfd))
3213 {
3214 extsymcount = symcount;
3215 extsymoff = 0;
3216 }
3217 else
3218 {
3219 extsymcount = symcount - hdr->sh_info;
3220 extsymoff = hdr->sh_info;
3221 }
3222
3223 if (extsymcount == 0)
3224 return FALSE;
3225
3226 /* Read in the symbol table. */
3227 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3228 NULL, NULL, NULL);
3229 if (isymbuf == NULL)
3230 return FALSE;
3231
3232 /* Scan the symbol table looking for SYMDEF. */
3233 result = FALSE;
3234 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3235 {
3236 const char *name;
3237
3238 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3239 isym->st_name);
3240 if (name == NULL)
3241 break;
3242
3243 if (strcmp (name, symdef->name) == 0)
3244 {
3245 result = is_global_data_symbol_definition (abfd, isym);
3246 break;
3247 }
3248 }
3249
3250 free (isymbuf);
3251
3252 return result;
3253}
3254\f
5a580b3a
AM
3255/* Add an entry to the .dynamic table. */
3256
3257bfd_boolean
3258_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3259 bfd_vma tag,
3260 bfd_vma val)
3261{
3262 struct elf_link_hash_table *hash_table;
3263 const struct elf_backend_data *bed;
3264 asection *s;
3265 bfd_size_type newsize;
3266 bfd_byte *newcontents;
3267 Elf_Internal_Dyn dyn;
3268
3269 hash_table = elf_hash_table (info);
3270 if (! is_elf_hash_table (hash_table))
3271 return FALSE;
3272
3273 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3274 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3275 BFD_ASSERT (s != NULL);
3276
eea6121a 3277 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3278 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3279 if (newcontents == NULL)
3280 return FALSE;
3281
3282 dyn.d_tag = tag;
3283 dyn.d_un.d_val = val;
eea6121a 3284 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3285
eea6121a 3286 s->size = newsize;
5a580b3a
AM
3287 s->contents = newcontents;
3288
3289 return TRUE;
3290}
3291
3292/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3293 otherwise just check whether one already exists. Returns -1 on error,
3294 1 if a DT_NEEDED tag already exists, and 0 on success. */
3295
4ad4eba5 3296static int
7e9f0867
AM
3297elf_add_dt_needed_tag (bfd *abfd,
3298 struct bfd_link_info *info,
4ad4eba5
AM
3299 const char *soname,
3300 bfd_boolean do_it)
5a580b3a
AM
3301{
3302 struct elf_link_hash_table *hash_table;
ef53be89 3303 size_t strindex;
5a580b3a 3304
7e9f0867
AM
3305 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3306 return -1;
3307
5a580b3a 3308 hash_table = elf_hash_table (info);
5a580b3a 3309 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3310 if (strindex == (size_t) -1)
5a580b3a
AM
3311 return -1;
3312
02be4619 3313 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3314 {
3315 asection *sdyn;
3316 const struct elf_backend_data *bed;
3317 bfd_byte *extdyn;
3318
3319 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3320 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3321 if (sdyn != NULL)
3322 for (extdyn = sdyn->contents;
3323 extdyn < sdyn->contents + sdyn->size;
3324 extdyn += bed->s->sizeof_dyn)
3325 {
3326 Elf_Internal_Dyn dyn;
5a580b3a 3327
7e9f0867
AM
3328 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3329 if (dyn.d_tag == DT_NEEDED
3330 && dyn.d_un.d_val == strindex)
3331 {
3332 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3333 return 1;
3334 }
3335 }
5a580b3a
AM
3336 }
3337
3338 if (do_it)
3339 {
7e9f0867
AM
3340 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3341 return -1;
3342
5a580b3a
AM
3343 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3344 return -1;
3345 }
3346 else
3347 /* We were just checking for existence of the tag. */
3348 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3349
3350 return 0;
3351}
3352
7b15fa7a
AM
3353/* Return true if SONAME is on the needed list between NEEDED and STOP
3354 (or the end of list if STOP is NULL), and needed by a library that
3355 will be loaded. */
3356
010e5ae2 3357static bfd_boolean
7b15fa7a
AM
3358on_needed_list (const char *soname,
3359 struct bfd_link_needed_list *needed,
3360 struct bfd_link_needed_list *stop)
010e5ae2 3361{
7b15fa7a
AM
3362 struct bfd_link_needed_list *look;
3363 for (look = needed; look != stop; look = look->next)
3364 if (strcmp (soname, look->name) == 0
3365 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3366 /* If needed by a library that itself is not directly
3367 needed, recursively check whether that library is
3368 indirectly needed. Since we add DT_NEEDED entries to
3369 the end of the list, library dependencies appear after
3370 the library. Therefore search prior to the current
3371 LOOK, preventing possible infinite recursion. */
3372 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3373 return TRUE;
3374
3375 return FALSE;
3376}
3377
14160578 3378/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3379static int
3380elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3381{
3382 const struct elf_link_hash_entry *h1;
3383 const struct elf_link_hash_entry *h2;
10b7e05b 3384 bfd_signed_vma vdiff;
5a580b3a
AM
3385
3386 h1 = *(const struct elf_link_hash_entry **) arg1;
3387 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3388 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3389 if (vdiff != 0)
3390 return vdiff > 0 ? 1 : -1;
3391 else
3392 {
d3435ae8 3393 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3394 if (sdiff != 0)
3395 return sdiff > 0 ? 1 : -1;
3396 }
14160578
AM
3397 vdiff = h1->size - h2->size;
3398 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3399}
4ad4eba5 3400
5a580b3a
AM
3401/* This function is used to adjust offsets into .dynstr for
3402 dynamic symbols. This is called via elf_link_hash_traverse. */
3403
3404static bfd_boolean
3405elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3406{
a50b1753 3407 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3408
5a580b3a
AM
3409 if (h->dynindx != -1)
3410 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3411 return TRUE;
3412}
3413
3414/* Assign string offsets in .dynstr, update all structures referencing
3415 them. */
3416
4ad4eba5
AM
3417static bfd_boolean
3418elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3419{
3420 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3421 struct elf_link_local_dynamic_entry *entry;
3422 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3423 bfd *dynobj = hash_table->dynobj;
3424 asection *sdyn;
3425 bfd_size_type size;
3426 const struct elf_backend_data *bed;
3427 bfd_byte *extdyn;
3428
3429 _bfd_elf_strtab_finalize (dynstr);
3430 size = _bfd_elf_strtab_size (dynstr);
3431
3432 bed = get_elf_backend_data (dynobj);
3d4d4302 3433 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3434 BFD_ASSERT (sdyn != NULL);
3435
3436 /* Update all .dynamic entries referencing .dynstr strings. */
3437 for (extdyn = sdyn->contents;
eea6121a 3438 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3439 extdyn += bed->s->sizeof_dyn)
3440 {
3441 Elf_Internal_Dyn dyn;
3442
3443 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3444 switch (dyn.d_tag)
3445 {
3446 case DT_STRSZ:
3447 dyn.d_un.d_val = size;
3448 break;
3449 case DT_NEEDED:
3450 case DT_SONAME:
3451 case DT_RPATH:
3452 case DT_RUNPATH:
3453 case DT_FILTER:
3454 case DT_AUXILIARY:
7ee314fa
AM
3455 case DT_AUDIT:
3456 case DT_DEPAUDIT:
5a580b3a
AM
3457 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3458 break;
3459 default:
3460 continue;
3461 }
3462 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3463 }
3464
3465 /* Now update local dynamic symbols. */
3466 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3467 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3468 entry->isym.st_name);
3469
3470 /* And the rest of dynamic symbols. */
3471 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3472
3473 /* Adjust version definitions. */
3474 if (elf_tdata (output_bfd)->cverdefs)
3475 {
3476 asection *s;
3477 bfd_byte *p;
ef53be89 3478 size_t i;
5a580b3a
AM
3479 Elf_Internal_Verdef def;
3480 Elf_Internal_Verdaux defaux;
3481
3d4d4302 3482 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3483 p = s->contents;
3484 do
3485 {
3486 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3487 &def);
3488 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3489 if (def.vd_aux != sizeof (Elf_External_Verdef))
3490 continue;
5a580b3a
AM
3491 for (i = 0; i < def.vd_cnt; ++i)
3492 {
3493 _bfd_elf_swap_verdaux_in (output_bfd,
3494 (Elf_External_Verdaux *) p, &defaux);
3495 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3496 defaux.vda_name);
3497 _bfd_elf_swap_verdaux_out (output_bfd,
3498 &defaux, (Elf_External_Verdaux *) p);
3499 p += sizeof (Elf_External_Verdaux);
3500 }
3501 }
3502 while (def.vd_next);
3503 }
3504
3505 /* Adjust version references. */
3506 if (elf_tdata (output_bfd)->verref)
3507 {
3508 asection *s;
3509 bfd_byte *p;
ef53be89 3510 size_t i;
5a580b3a
AM
3511 Elf_Internal_Verneed need;
3512 Elf_Internal_Vernaux needaux;
3513
3d4d4302 3514 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3515 p = s->contents;
3516 do
3517 {
3518 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3519 &need);
3520 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3521 _bfd_elf_swap_verneed_out (output_bfd, &need,
3522 (Elf_External_Verneed *) p);
3523 p += sizeof (Elf_External_Verneed);
3524 for (i = 0; i < need.vn_cnt; ++i)
3525 {
3526 _bfd_elf_swap_vernaux_in (output_bfd,
3527 (Elf_External_Vernaux *) p, &needaux);
3528 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3529 needaux.vna_name);
3530 _bfd_elf_swap_vernaux_out (output_bfd,
3531 &needaux,
3532 (Elf_External_Vernaux *) p);
3533 p += sizeof (Elf_External_Vernaux);
3534 }
3535 }
3536 while (need.vn_next);
3537 }
3538
3539 return TRUE;
3540}
3541\f
13285a1b
AM
3542/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3543 The default is to only match when the INPUT and OUTPUT are exactly
3544 the same target. */
3545
3546bfd_boolean
3547_bfd_elf_default_relocs_compatible (const bfd_target *input,
3548 const bfd_target *output)
3549{
3550 return input == output;
3551}
3552
3553/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3554 This version is used when different targets for the same architecture
3555 are virtually identical. */
3556
3557bfd_boolean
3558_bfd_elf_relocs_compatible (const bfd_target *input,
3559 const bfd_target *output)
3560{
3561 const struct elf_backend_data *obed, *ibed;
3562
3563 if (input == output)
3564 return TRUE;
3565
3566 ibed = xvec_get_elf_backend_data (input);
3567 obed = xvec_get_elf_backend_data (output);
3568
3569 if (ibed->arch != obed->arch)
3570 return FALSE;
3571
3572 /* If both backends are using this function, deem them compatible. */
3573 return ibed->relocs_compatible == obed->relocs_compatible;
3574}
3575
e5034e59
AM
3576/* Make a special call to the linker "notice" function to tell it that
3577 we are about to handle an as-needed lib, or have finished
1b786873 3578 processing the lib. */
e5034e59
AM
3579
3580bfd_boolean
3581_bfd_elf_notice_as_needed (bfd *ibfd,
3582 struct bfd_link_info *info,
3583 enum notice_asneeded_action act)
3584{
46135103 3585 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3586}
3587
d9689752
L
3588/* Check relocations an ELF object file. */
3589
3590bfd_boolean
3591_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3592{
3593 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3594 struct elf_link_hash_table *htab = elf_hash_table (info);
3595
3596 /* If this object is the same format as the output object, and it is
3597 not a shared library, then let the backend look through the
3598 relocs.
3599
3600 This is required to build global offset table entries and to
3601 arrange for dynamic relocs. It is not required for the
3602 particular common case of linking non PIC code, even when linking
3603 against shared libraries, but unfortunately there is no way of
3604 knowing whether an object file has been compiled PIC or not.
3605 Looking through the relocs is not particularly time consuming.
3606 The problem is that we must either (1) keep the relocs in memory,
3607 which causes the linker to require additional runtime memory or
3608 (2) read the relocs twice from the input file, which wastes time.
3609 This would be a good case for using mmap.
3610
3611 I have no idea how to handle linking PIC code into a file of a
3612 different format. It probably can't be done. */
3613 if ((abfd->flags & DYNAMIC) == 0
3614 && is_elf_hash_table (htab)
3615 && bed->check_relocs != NULL
3616 && elf_object_id (abfd) == elf_hash_table_id (htab)
3617 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3618 {
3619 asection *o;
3620
3621 for (o = abfd->sections; o != NULL; o = o->next)
3622 {
3623 Elf_Internal_Rela *internal_relocs;
3624 bfd_boolean ok;
3625
5ce03cea 3626 /* Don't check relocations in excluded sections. */
d9689752 3627 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3628 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3629 || o->reloc_count == 0
3630 || ((info->strip == strip_all || info->strip == strip_debugger)
3631 && (o->flags & SEC_DEBUGGING) != 0)
3632 || bfd_is_abs_section (o->output_section))
3633 continue;
3634
3635 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3636 info->keep_memory);
3637 if (internal_relocs == NULL)
3638 return FALSE;
3639
3640 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3641
3642 if (elf_section_data (o)->relocs != internal_relocs)
3643 free (internal_relocs);
3644
3645 if (! ok)
3646 return FALSE;
3647 }
3648 }
3649
3650 return TRUE;
3651}
3652
4ad4eba5
AM
3653/* Add symbols from an ELF object file to the linker hash table. */
3654
3655static bfd_boolean
3656elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3657{
a0c402a5 3658 Elf_Internal_Ehdr *ehdr;
4ad4eba5 3659 Elf_Internal_Shdr *hdr;
ef53be89
AM
3660 size_t symcount;
3661 size_t extsymcount;
3662 size_t extsymoff;
4ad4eba5
AM
3663 struct elf_link_hash_entry **sym_hash;
3664 bfd_boolean dynamic;
3665 Elf_External_Versym *extversym = NULL;
3666 Elf_External_Versym *ever;
3667 struct elf_link_hash_entry *weaks;
3668 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 3669 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
3670 Elf_Internal_Sym *isymbuf = NULL;
3671 Elf_Internal_Sym *isym;
3672 Elf_Internal_Sym *isymend;
3673 const struct elf_backend_data *bed;
3674 bfd_boolean add_needed;
66eb6687 3675 struct elf_link_hash_table *htab;
4ad4eba5 3676 bfd_size_type amt;
66eb6687 3677 void *alloc_mark = NULL;
4f87808c
AM
3678 struct bfd_hash_entry **old_table = NULL;
3679 unsigned int old_size = 0;
3680 unsigned int old_count = 0;
66eb6687 3681 void *old_tab = NULL;
66eb6687
AM
3682 void *old_ent;
3683 struct bfd_link_hash_entry *old_undefs = NULL;
3684 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 3685 void *old_strtab = NULL;
66eb6687 3686 size_t tabsize = 0;
db6a5d5f 3687 asection *s;
29a9f53e 3688 bfd_boolean just_syms;
4ad4eba5 3689
66eb6687 3690 htab = elf_hash_table (info);
4ad4eba5 3691 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3692
3693 if ((abfd->flags & DYNAMIC) == 0)
3694 dynamic = FALSE;
3695 else
3696 {
3697 dynamic = TRUE;
3698
3699 /* You can't use -r against a dynamic object. Also, there's no
3700 hope of using a dynamic object which does not exactly match
3701 the format of the output file. */
0e1862bb 3702 if (bfd_link_relocatable (info)
66eb6687 3703 || !is_elf_hash_table (htab)
f13a99db 3704 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3705 {
0e1862bb 3706 if (bfd_link_relocatable (info))
9a0789ec
NC
3707 bfd_set_error (bfd_error_invalid_operation);
3708 else
3709 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3710 goto error_return;
3711 }
3712 }
3713
a0c402a5
L
3714 ehdr = elf_elfheader (abfd);
3715 if (info->warn_alternate_em
3716 && bed->elf_machine_code != ehdr->e_machine
3717 && ((bed->elf_machine_alt1 != 0
3718 && ehdr->e_machine == bed->elf_machine_alt1)
3719 || (bed->elf_machine_alt2 != 0
3720 && ehdr->e_machine == bed->elf_machine_alt2)))
3721 info->callbacks->einfo
695344c0 3722 /* xgettext:c-format */
a0c402a5
L
3723 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3724 ehdr->e_machine, abfd, bed->elf_machine_code);
3725
4ad4eba5
AM
3726 /* As a GNU extension, any input sections which are named
3727 .gnu.warning.SYMBOL are treated as warning symbols for the given
3728 symbol. This differs from .gnu.warning sections, which generate
3729 warnings when they are included in an output file. */
dd98f8d2 3730 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3731 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3732 {
db6a5d5f 3733 const char *name;
4ad4eba5 3734
db6a5d5f
AM
3735 name = bfd_get_section_name (abfd, s);
3736 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3737 {
db6a5d5f
AM
3738 char *msg;
3739 bfd_size_type sz;
3740
3741 name += sizeof ".gnu.warning." - 1;
3742
3743 /* If this is a shared object, then look up the symbol
3744 in the hash table. If it is there, and it is already
3745 been defined, then we will not be using the entry
3746 from this shared object, so we don't need to warn.
3747 FIXME: If we see the definition in a regular object
3748 later on, we will warn, but we shouldn't. The only
3749 fix is to keep track of what warnings we are supposed
3750 to emit, and then handle them all at the end of the
3751 link. */
3752 if (dynamic)
4ad4eba5 3753 {
db6a5d5f
AM
3754 struct elf_link_hash_entry *h;
3755
3756 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3757
3758 /* FIXME: What about bfd_link_hash_common? */
3759 if (h != NULL
3760 && (h->root.type == bfd_link_hash_defined
3761 || h->root.type == bfd_link_hash_defweak))
3762 continue;
3763 }
4ad4eba5 3764
db6a5d5f
AM
3765 sz = s->size;
3766 msg = (char *) bfd_alloc (abfd, sz + 1);
3767 if (msg == NULL)
3768 goto error_return;
4ad4eba5 3769
db6a5d5f
AM
3770 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3771 goto error_return;
4ad4eba5 3772
db6a5d5f 3773 msg[sz] = '\0';
4ad4eba5 3774
db6a5d5f
AM
3775 if (! (_bfd_generic_link_add_one_symbol
3776 (info, abfd, name, BSF_WARNING, s, 0, msg,
3777 FALSE, bed->collect, NULL)))
3778 goto error_return;
4ad4eba5 3779
0e1862bb 3780 if (bfd_link_executable (info))
db6a5d5f
AM
3781 {
3782 /* Clobber the section size so that the warning does
3783 not get copied into the output file. */
3784 s->size = 0;
11d2f718 3785
db6a5d5f
AM
3786 /* Also set SEC_EXCLUDE, so that symbols defined in
3787 the warning section don't get copied to the output. */
3788 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3789 }
3790 }
3791 }
3792
29a9f53e
L
3793 just_syms = ((s = abfd->sections) != NULL
3794 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3795
4ad4eba5
AM
3796 add_needed = TRUE;
3797 if (! dynamic)
3798 {
3799 /* If we are creating a shared library, create all the dynamic
3800 sections immediately. We need to attach them to something,
3801 so we attach them to this BFD, provided it is the right
bf89386a
L
3802 format and is not from ld --just-symbols. Always create the
3803 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3804 are no input BFD's of the same format as the output, we can't
3805 make a shared library. */
3806 if (!just_syms
bf89386a 3807 && (bfd_link_pic (info)
9c1d7a08
L
3808 || (!bfd_link_relocatable (info)
3809 && (info->export_dynamic || info->dynamic)))
66eb6687 3810 && is_elf_hash_table (htab)
f13a99db 3811 && info->output_bfd->xvec == abfd->xvec
66eb6687 3812 && !htab->dynamic_sections_created)
4ad4eba5
AM
3813 {
3814 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3815 goto error_return;
3816 }
3817 }
66eb6687 3818 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3819 goto error_return;
3820 else
3821 {
4ad4eba5 3822 const char *soname = NULL;
7ee314fa 3823 char *audit = NULL;
4ad4eba5 3824 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 3825 const Elf_Internal_Phdr *phdr;
4ad4eba5
AM
3826 int ret;
3827
3828 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3829 ld shouldn't allow it. */
29a9f53e 3830 if (just_syms)
92fd189d 3831 abort ();
4ad4eba5
AM
3832
3833 /* If this dynamic lib was specified on the command line with
3834 --as-needed in effect, then we don't want to add a DT_NEEDED
3835 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3836 in by another lib's DT_NEEDED. When --no-add-needed is used
3837 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3838 any dynamic library in DT_NEEDED tags in the dynamic lib at
3839 all. */
3840 add_needed = (elf_dyn_lib_class (abfd)
3841 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3842 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3843
3844 s = bfd_get_section_by_name (abfd, ".dynamic");
3845 if (s != NULL)
3846 {
3847 bfd_byte *dynbuf;
3848 bfd_byte *extdyn;
cb33740c 3849 unsigned int elfsec;
4ad4eba5
AM
3850 unsigned long shlink;
3851
eea6121a 3852 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3853 {
3854error_free_dyn:
3855 free (dynbuf);
3856 goto error_return;
3857 }
4ad4eba5
AM
3858
3859 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3860 if (elfsec == SHN_BAD)
4ad4eba5
AM
3861 goto error_free_dyn;
3862 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3863
3864 for (extdyn = dynbuf;
eea6121a 3865 extdyn < dynbuf + s->size;
4ad4eba5
AM
3866 extdyn += bed->s->sizeof_dyn)
3867 {
3868 Elf_Internal_Dyn dyn;
3869
3870 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3871 if (dyn.d_tag == DT_SONAME)
3872 {
3873 unsigned int tagv = dyn.d_un.d_val;
3874 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3875 if (soname == NULL)
3876 goto error_free_dyn;
3877 }
3878 if (dyn.d_tag == DT_NEEDED)
3879 {
3880 struct bfd_link_needed_list *n, **pn;
3881 char *fnm, *anm;
3882 unsigned int tagv = dyn.d_un.d_val;
3883
3884 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3885 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3886 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3887 if (n == NULL || fnm == NULL)
3888 goto error_free_dyn;
3889 amt = strlen (fnm) + 1;
a50b1753 3890 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3891 if (anm == NULL)
3892 goto error_free_dyn;
3893 memcpy (anm, fnm, amt);
3894 n->name = anm;
3895 n->by = abfd;
3896 n->next = NULL;
66eb6687 3897 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3898 ;
3899 *pn = n;
3900 }
3901 if (dyn.d_tag == DT_RUNPATH)
3902 {
3903 struct bfd_link_needed_list *n, **pn;
3904 char *fnm, *anm;
3905 unsigned int tagv = dyn.d_un.d_val;
3906
3907 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3908 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3909 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3910 if (n == NULL || fnm == NULL)
3911 goto error_free_dyn;
3912 amt = strlen (fnm) + 1;
a50b1753 3913 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3914 if (anm == NULL)
3915 goto error_free_dyn;
3916 memcpy (anm, fnm, amt);
3917 n->name = anm;
3918 n->by = abfd;
3919 n->next = NULL;
3920 for (pn = & runpath;
3921 *pn != NULL;
3922 pn = &(*pn)->next)
3923 ;
3924 *pn = n;
3925 }
3926 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3927 if (!runpath && dyn.d_tag == DT_RPATH)
3928 {
3929 struct bfd_link_needed_list *n, **pn;
3930 char *fnm, *anm;
3931 unsigned int tagv = dyn.d_un.d_val;
3932
3933 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3934 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3935 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3936 if (n == NULL || fnm == NULL)
3937 goto error_free_dyn;
3938 amt = strlen (fnm) + 1;
a50b1753 3939 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3940 if (anm == NULL)
f8703194 3941 goto error_free_dyn;
4ad4eba5
AM
3942 memcpy (anm, fnm, amt);
3943 n->name = anm;
3944 n->by = abfd;
3945 n->next = NULL;
3946 for (pn = & rpath;
3947 *pn != NULL;
3948 pn = &(*pn)->next)
3949 ;
3950 *pn = n;
3951 }
7ee314fa
AM
3952 if (dyn.d_tag == DT_AUDIT)
3953 {
3954 unsigned int tagv = dyn.d_un.d_val;
3955 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3956 }
4ad4eba5
AM
3957 }
3958
3959 free (dynbuf);
3960 }
3961
3962 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3963 frees all more recently bfd_alloc'd blocks as well. */
3964 if (runpath)
3965 rpath = runpath;
3966
3967 if (rpath)
3968 {
3969 struct bfd_link_needed_list **pn;
66eb6687 3970 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3971 ;
3972 *pn = rpath;
3973 }
3974
9acc85a6
AM
3975 /* If we have a PT_GNU_RELRO program header, mark as read-only
3976 all sections contained fully therein. This makes relro
3977 shared library sections appear as they will at run-time. */
3978 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3979 while (--phdr >= elf_tdata (abfd)->phdr)
3980 if (phdr->p_type == PT_GNU_RELRO)
3981 {
3982 for (s = abfd->sections; s != NULL; s = s->next)
3983 if ((s->flags & SEC_ALLOC) != 0
3984 && s->vma >= phdr->p_vaddr
3985 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3986 s->flags |= SEC_READONLY;
3987 break;
3988 }
3989
4ad4eba5
AM
3990 /* We do not want to include any of the sections in a dynamic
3991 object in the output file. We hack by simply clobbering the
3992 list of sections in the BFD. This could be handled more
3993 cleanly by, say, a new section flag; the existing
3994 SEC_NEVER_LOAD flag is not the one we want, because that one
3995 still implies that the section takes up space in the output
3996 file. */
3997 bfd_section_list_clear (abfd);
3998
4ad4eba5
AM
3999 /* Find the name to use in a DT_NEEDED entry that refers to this
4000 object. If the object has a DT_SONAME entry, we use it.
4001 Otherwise, if the generic linker stuck something in
4002 elf_dt_name, we use that. Otherwise, we just use the file
4003 name. */
4004 if (soname == NULL || *soname == '\0')
4005 {
4006 soname = elf_dt_name (abfd);
4007 if (soname == NULL || *soname == '\0')
4008 soname = bfd_get_filename (abfd);
4009 }
4010
4011 /* Save the SONAME because sometimes the linker emulation code
4012 will need to know it. */
4013 elf_dt_name (abfd) = soname;
4014
7e9f0867 4015 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4016 if (ret < 0)
4017 goto error_return;
4018
4019 /* If we have already included this dynamic object in the
4020 link, just ignore it. There is no reason to include a
4021 particular dynamic object more than once. */
4022 if (ret > 0)
4023 return TRUE;
7ee314fa
AM
4024
4025 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4026 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4027 }
4028
4029 /* If this is a dynamic object, we always link against the .dynsym
4030 symbol table, not the .symtab symbol table. The dynamic linker
4031 will only see the .dynsym symbol table, so there is no reason to
4032 look at .symtab for a dynamic object. */
4033
4034 if (! dynamic || elf_dynsymtab (abfd) == 0)
4035 hdr = &elf_tdata (abfd)->symtab_hdr;
4036 else
4037 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4038
4039 symcount = hdr->sh_size / bed->s->sizeof_sym;
4040
4041 /* The sh_info field of the symtab header tells us where the
4042 external symbols start. We don't care about the local symbols at
4043 this point. */
4044 if (elf_bad_symtab (abfd))
4045 {
4046 extsymcount = symcount;
4047 extsymoff = 0;
4048 }
4049 else
4050 {
4051 extsymcount = symcount - hdr->sh_info;
4052 extsymoff = hdr->sh_info;
4053 }
4054
f45794cb 4055 sym_hash = elf_sym_hashes (abfd);
012b2306 4056 if (extsymcount != 0)
4ad4eba5
AM
4057 {
4058 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4059 NULL, NULL, NULL);
4060 if (isymbuf == NULL)
4061 goto error_return;
4062
4ad4eba5 4063 if (sym_hash == NULL)
012b2306
AM
4064 {
4065 /* We store a pointer to the hash table entry for each
4066 external symbol. */
ef53be89
AM
4067 amt = extsymcount;
4068 amt *= sizeof (struct elf_link_hash_entry *);
012b2306
AM
4069 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4070 if (sym_hash == NULL)
4071 goto error_free_sym;
4072 elf_sym_hashes (abfd) = sym_hash;
4073 }
4ad4eba5
AM
4074 }
4075
4076 if (dynamic)
4077 {
4078 /* Read in any version definitions. */
fc0e6df6
PB
4079 if (!_bfd_elf_slurp_version_tables (abfd,
4080 info->default_imported_symver))
4ad4eba5
AM
4081 goto error_free_sym;
4082
4083 /* Read in the symbol versions, but don't bother to convert them
4084 to internal format. */
4085 if (elf_dynversym (abfd) != 0)
4086 {
4087 Elf_Internal_Shdr *versymhdr;
4088
4089 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 4090 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
4091 if (extversym == NULL)
4092 goto error_free_sym;
4093 amt = versymhdr->sh_size;
4094 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4095 || bfd_bread (extversym, amt, abfd) != amt)
4096 goto error_free_vers;
4097 }
4098 }
4099
66eb6687
AM
4100 /* If we are loading an as-needed shared lib, save the symbol table
4101 state before we start adding symbols. If the lib turns out
4102 to be unneeded, restore the state. */
4103 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4104 {
4105 unsigned int i;
4106 size_t entsize;
4107
4108 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4109 {
4110 struct bfd_hash_entry *p;
2de92251 4111 struct elf_link_hash_entry *h;
66eb6687
AM
4112
4113 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4114 {
4115 h = (struct elf_link_hash_entry *) p;
4116 entsize += htab->root.table.entsize;
4117 if (h->root.type == bfd_link_hash_warning)
4118 entsize += htab->root.table.entsize;
4119 }
66eb6687
AM
4120 }
4121
4122 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4123 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4124 if (old_tab == NULL)
4125 goto error_free_vers;
4126
4127 /* Remember the current objalloc pointer, so that all mem for
4128 symbols added can later be reclaimed. */
4129 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4130 if (alloc_mark == NULL)
4131 goto error_free_vers;
4132
5061a885
AM
4133 /* Make a special call to the linker "notice" function to
4134 tell it that we are about to handle an as-needed lib. */
e5034e59 4135 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4136 goto error_free_vers;
5061a885 4137
f45794cb
AM
4138 /* Clone the symbol table. Remember some pointers into the
4139 symbol table, and dynamic symbol count. */
4140 old_ent = (char *) old_tab + tabsize;
66eb6687 4141 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4142 old_undefs = htab->root.undefs;
4143 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4144 old_table = htab->root.table.table;
4145 old_size = htab->root.table.size;
4146 old_count = htab->root.table.count;
5b677558
AM
4147 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4148 if (old_strtab == NULL)
4149 goto error_free_vers;
66eb6687
AM
4150
4151 for (i = 0; i < htab->root.table.size; i++)
4152 {
4153 struct bfd_hash_entry *p;
2de92251 4154 struct elf_link_hash_entry *h;
66eb6687
AM
4155
4156 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4157 {
4158 memcpy (old_ent, p, htab->root.table.entsize);
4159 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4160 h = (struct elf_link_hash_entry *) p;
4161 if (h->root.type == bfd_link_hash_warning)
4162 {
4163 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4164 old_ent = (char *) old_ent + htab->root.table.entsize;
4165 }
66eb6687
AM
4166 }
4167 }
4168 }
4ad4eba5 4169
66eb6687 4170 weaks = NULL;
4ad4eba5
AM
4171 ever = extversym != NULL ? extversym + extsymoff : NULL;
4172 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4173 isym < isymend;
4174 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4175 {
4176 int bind;
4177 bfd_vma value;
af44c138 4178 asection *sec, *new_sec;
4ad4eba5
AM
4179 flagword flags;
4180 const char *name;
4181 struct elf_link_hash_entry *h;
90c984fc 4182 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4183 bfd_boolean definition;
4184 bfd_boolean size_change_ok;
4185 bfd_boolean type_change_ok;
4186 bfd_boolean new_weakdef;
37a9e49a
L
4187 bfd_boolean new_weak;
4188 bfd_boolean old_weak;
4ad4eba5 4189 bfd_boolean override;
a4d8e49b 4190 bfd_boolean common;
97196564 4191 bfd_boolean discarded;
4ad4eba5
AM
4192 unsigned int old_alignment;
4193 bfd *old_bfd;
6e33951e 4194 bfd_boolean matched;
4ad4eba5
AM
4195
4196 override = FALSE;
4197
4198 flags = BSF_NO_FLAGS;
4199 sec = NULL;
4200 value = isym->st_value;
a4d8e49b 4201 common = bed->common_definition (isym);
97196564 4202 discarded = FALSE;
4ad4eba5
AM
4203
4204 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4205 switch (bind)
4ad4eba5 4206 {
3e7a7d11 4207 case STB_LOCAL:
4ad4eba5
AM
4208 /* This should be impossible, since ELF requires that all
4209 global symbols follow all local symbols, and that sh_info
4210 point to the first global symbol. Unfortunately, Irix 5
4211 screws this up. */
4212 continue;
3e7a7d11
NC
4213
4214 case STB_GLOBAL:
a4d8e49b 4215 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4216 flags = BSF_GLOBAL;
3e7a7d11
NC
4217 break;
4218
4219 case STB_WEAK:
4220 flags = BSF_WEAK;
4221 break;
4222
4223 case STB_GNU_UNIQUE:
4224 flags = BSF_GNU_UNIQUE;
4225 break;
4226
4227 default:
4ad4eba5 4228 /* Leave it up to the processor backend. */
3e7a7d11 4229 break;
4ad4eba5
AM
4230 }
4231
4232 if (isym->st_shndx == SHN_UNDEF)
4233 sec = bfd_und_section_ptr;
cb33740c
AM
4234 else if (isym->st_shndx == SHN_ABS)
4235 sec = bfd_abs_section_ptr;
4236 else if (isym->st_shndx == SHN_COMMON)
4237 {
4238 sec = bfd_com_section_ptr;
4239 /* What ELF calls the size we call the value. What ELF
4240 calls the value we call the alignment. */
4241 value = isym->st_size;
4242 }
4243 else
4ad4eba5
AM
4244 {
4245 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4246 if (sec == NULL)
4247 sec = bfd_abs_section_ptr;
dbaa2011 4248 else if (discarded_section (sec))
529fcb95 4249 {
e5d08002
L
4250 /* Symbols from discarded section are undefined. We keep
4251 its visibility. */
529fcb95 4252 sec = bfd_und_section_ptr;
97196564 4253 discarded = TRUE;
529fcb95
PB
4254 isym->st_shndx = SHN_UNDEF;
4255 }
4ad4eba5
AM
4256 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4257 value -= sec->vma;
4258 }
4ad4eba5
AM
4259
4260 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4261 isym->st_name);
4262 if (name == NULL)
4263 goto error_free_vers;
4264
4265 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4266 && (abfd->flags & BFD_PLUGIN) != 0)
4267 {
4268 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4269
4270 if (xc == NULL)
4271 {
4272 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4273 | SEC_EXCLUDE);
4274 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4275 if (xc == NULL)
4276 goto error_free_vers;
4277 }
4278 sec = xc;
4279 }
4280 else if (isym->st_shndx == SHN_COMMON
4281 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4282 && !bfd_link_relocatable (info))
4ad4eba5
AM
4283 {
4284 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4285
4286 if (tcomm == NULL)
4287 {
02d00247
AM
4288 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4289 | SEC_LINKER_CREATED);
4290 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4291 if (tcomm == NULL)
4ad4eba5
AM
4292 goto error_free_vers;
4293 }
4294 sec = tcomm;
4295 }
66eb6687 4296 else if (bed->elf_add_symbol_hook)
4ad4eba5 4297 {
66eb6687
AM
4298 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4299 &sec, &value))
4ad4eba5
AM
4300 goto error_free_vers;
4301
4302 /* The hook function sets the name to NULL if this symbol
4303 should be skipped for some reason. */
4304 if (name == NULL)
4305 continue;
4306 }
4307
4308 /* Sanity check that all possibilities were handled. */
4309 if (sec == NULL)
4310 {
4311 bfd_set_error (bfd_error_bad_value);
4312 goto error_free_vers;
4313 }
4314
191c0c42
AM
4315 /* Silently discard TLS symbols from --just-syms. There's
4316 no way to combine a static TLS block with a new TLS block
4317 for this executable. */
4318 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4319 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4320 continue;
4321
4ad4eba5
AM
4322 if (bfd_is_und_section (sec)
4323 || bfd_is_com_section (sec))
4324 definition = FALSE;
4325 else
4326 definition = TRUE;
4327
4328 size_change_ok = FALSE;
66eb6687 4329 type_change_ok = bed->type_change_ok;
37a9e49a 4330 old_weak = FALSE;
6e33951e 4331 matched = FALSE;
4ad4eba5
AM
4332 old_alignment = 0;
4333 old_bfd = NULL;
af44c138 4334 new_sec = sec;
4ad4eba5 4335
66eb6687 4336 if (is_elf_hash_table (htab))
4ad4eba5
AM
4337 {
4338 Elf_Internal_Versym iver;
4339 unsigned int vernum = 0;
4340 bfd_boolean skip;
4341
fc0e6df6 4342 if (ever == NULL)
4ad4eba5 4343 {
fc0e6df6
PB
4344 if (info->default_imported_symver)
4345 /* Use the default symbol version created earlier. */
4346 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4347 else
4348 iver.vs_vers = 0;
4349 }
4350 else
4351 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4352
4353 vernum = iver.vs_vers & VERSYM_VERSION;
4354
4355 /* If this is a hidden symbol, or if it is not version
4356 1, we append the version name to the symbol name.
cc86ff91
EB
4357 However, we do not modify a non-hidden absolute symbol
4358 if it is not a function, because it might be the version
4359 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4360 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4361 || (vernum > 1
4362 && (!bfd_is_abs_section (sec)
4363 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4364 {
4365 const char *verstr;
4366 size_t namelen, verlen, newlen;
4367 char *newname, *p;
4368
4369 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4370 {
fc0e6df6
PB
4371 if (vernum > elf_tdata (abfd)->cverdefs)
4372 verstr = NULL;
4373 else if (vernum > 1)
4374 verstr =
4375 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4376 else
4377 verstr = "";
4ad4eba5 4378
fc0e6df6 4379 if (verstr == NULL)
4ad4eba5 4380 {
4eca0228 4381 _bfd_error_handler
695344c0 4382 /* xgettext:c-format */
fc0e6df6
PB
4383 (_("%B: %s: invalid version %u (max %d)"),
4384 abfd, name, vernum,
4385 elf_tdata (abfd)->cverdefs);
4386 bfd_set_error (bfd_error_bad_value);
4387 goto error_free_vers;
4ad4eba5 4388 }
fc0e6df6
PB
4389 }
4390 else
4391 {
4392 /* We cannot simply test for the number of
4393 entries in the VERNEED section since the
4394 numbers for the needed versions do not start
4395 at 0. */
4396 Elf_Internal_Verneed *t;
4397
4398 verstr = NULL;
4399 for (t = elf_tdata (abfd)->verref;
4400 t != NULL;
4401 t = t->vn_nextref)
4ad4eba5 4402 {
fc0e6df6 4403 Elf_Internal_Vernaux *a;
4ad4eba5 4404
fc0e6df6
PB
4405 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4406 {
4407 if (a->vna_other == vernum)
4ad4eba5 4408 {
fc0e6df6
PB
4409 verstr = a->vna_nodename;
4410 break;
4ad4eba5 4411 }
4ad4eba5 4412 }
fc0e6df6
PB
4413 if (a != NULL)
4414 break;
4415 }
4416 if (verstr == NULL)
4417 {
4eca0228 4418 _bfd_error_handler
695344c0 4419 /* xgettext:c-format */
fc0e6df6
PB
4420 (_("%B: %s: invalid needed version %d"),
4421 abfd, name, vernum);
4422 bfd_set_error (bfd_error_bad_value);
4423 goto error_free_vers;
4ad4eba5 4424 }
4ad4eba5 4425 }
fc0e6df6
PB
4426
4427 namelen = strlen (name);
4428 verlen = strlen (verstr);
4429 newlen = namelen + verlen + 2;
4430 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4431 && isym->st_shndx != SHN_UNDEF)
4432 ++newlen;
4433
a50b1753 4434 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4435 if (newname == NULL)
4436 goto error_free_vers;
4437 memcpy (newname, name, namelen);
4438 p = newname + namelen;
4439 *p++ = ELF_VER_CHR;
4440 /* If this is a defined non-hidden version symbol,
4441 we add another @ to the name. This indicates the
4442 default version of the symbol. */
4443 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4444 && isym->st_shndx != SHN_UNDEF)
4445 *p++ = ELF_VER_CHR;
4446 memcpy (p, verstr, verlen + 1);
4447
4448 name = newname;
4ad4eba5
AM
4449 }
4450
cd3416da
AM
4451 /* If this symbol has default visibility and the user has
4452 requested we not re-export it, then mark it as hidden. */
a0d49154 4453 if (!bfd_is_und_section (sec)
cd3416da 4454 && !dynamic
ce875075 4455 && abfd->no_export
cd3416da
AM
4456 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4457 isym->st_other = (STV_HIDDEN
4458 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4459
4f3fedcf
AM
4460 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4461 sym_hash, &old_bfd, &old_weak,
4462 &old_alignment, &skip, &override,
6e33951e
L
4463 &type_change_ok, &size_change_ok,
4464 &matched))
4ad4eba5
AM
4465 goto error_free_vers;
4466
4467 if (skip)
4468 continue;
4469
6e33951e
L
4470 /* Override a definition only if the new symbol matches the
4471 existing one. */
4472 if (override && matched)
4ad4eba5
AM
4473 definition = FALSE;
4474
4475 h = *sym_hash;
4476 while (h->root.type == bfd_link_hash_indirect
4477 || h->root.type == bfd_link_hash_warning)
4478 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4479
4ad4eba5 4480 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4481 && vernum > 1
4482 && definition)
4483 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4484 }
4485
4486 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4487 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4488 (struct bfd_link_hash_entry **) sym_hash)))
4489 goto error_free_vers;
4490
a43942db
MR
4491 if ((flags & BSF_GNU_UNIQUE)
4492 && (abfd->flags & DYNAMIC) == 0
4493 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4494 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4495
4ad4eba5 4496 h = *sym_hash;
90c984fc
L
4497 /* We need to make sure that indirect symbol dynamic flags are
4498 updated. */
4499 hi = h;
4ad4eba5
AM
4500 while (h->root.type == bfd_link_hash_indirect
4501 || h->root.type == bfd_link_hash_warning)
4502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4503
97196564
L
4504 /* Setting the index to -3 tells elf_link_output_extsym that
4505 this symbol is defined in a discarded section. */
4506 if (discarded)
4507 h->indx = -3;
4508
4ad4eba5
AM
4509 *sym_hash = h;
4510
37a9e49a 4511 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4512 new_weakdef = FALSE;
4513 if (dynamic
4514 && definition
37a9e49a 4515 && new_weak
fcb93ecf 4516 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4517 && is_elf_hash_table (htab)
f6e332e6 4518 && h->u.weakdef == NULL)
4ad4eba5
AM
4519 {
4520 /* Keep a list of all weak defined non function symbols from
4521 a dynamic object, using the weakdef field. Later in this
4522 function we will set the weakdef field to the correct
4523 value. We only put non-function symbols from dynamic
4524 objects on this list, because that happens to be the only
4525 time we need to know the normal symbol corresponding to a
4526 weak symbol, and the information is time consuming to
4527 figure out. If the weakdef field is not already NULL,
4528 then this symbol was already defined by some previous
4529 dynamic object, and we will be using that previous
4530 definition anyhow. */
4531
f6e332e6 4532 h->u.weakdef = weaks;
4ad4eba5
AM
4533 weaks = h;
4534 new_weakdef = TRUE;
4535 }
4536
4537 /* Set the alignment of a common symbol. */
a4d8e49b 4538 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4539 && h->root.type == bfd_link_hash_common)
4540 {
4541 unsigned int align;
4542
a4d8e49b 4543 if (common)
af44c138
L
4544 align = bfd_log2 (isym->st_value);
4545 else
4546 {
4547 /* The new symbol is a common symbol in a shared object.
4548 We need to get the alignment from the section. */
4549 align = new_sec->alignment_power;
4550 }
595213d4 4551 if (align > old_alignment)
4ad4eba5
AM
4552 h->root.u.c.p->alignment_power = align;
4553 else
4554 h->root.u.c.p->alignment_power = old_alignment;
4555 }
4556
66eb6687 4557 if (is_elf_hash_table (htab))
4ad4eba5 4558 {
4f3fedcf
AM
4559 /* Set a flag in the hash table entry indicating the type of
4560 reference or definition we just found. A dynamic symbol
4561 is one which is referenced or defined by both a regular
4562 object and a shared object. */
4563 bfd_boolean dynsym = FALSE;
4564
4565 /* Plugin symbols aren't normal. Don't set def_regular or
4566 ref_regular for them, or make them dynamic. */
4567 if ((abfd->flags & BFD_PLUGIN) != 0)
4568 ;
4569 else if (! dynamic)
4570 {
4571 if (! definition)
4572 {
4573 h->ref_regular = 1;
4574 if (bind != STB_WEAK)
4575 h->ref_regular_nonweak = 1;
4576 }
4577 else
4578 {
4579 h->def_regular = 1;
4580 if (h->def_dynamic)
4581 {
4582 h->def_dynamic = 0;
4583 h->ref_dynamic = 1;
4584 }
4585 }
4586
4587 /* If the indirect symbol has been forced local, don't
4588 make the real symbol dynamic. */
4589 if ((h == hi || !hi->forced_local)
0e1862bb 4590 && (bfd_link_dll (info)
4f3fedcf
AM
4591 || h->def_dynamic
4592 || h->ref_dynamic))
4593 dynsym = TRUE;
4594 }
4595 else
4596 {
4597 if (! definition)
4598 {
4599 h->ref_dynamic = 1;
4600 hi->ref_dynamic = 1;
4601 }
4602 else
4603 {
4604 h->def_dynamic = 1;
4605 hi->def_dynamic = 1;
4606 }
4607
4608 /* If the indirect symbol has been forced local, don't
4609 make the real symbol dynamic. */
4610 if ((h == hi || !hi->forced_local)
4611 && (h->def_regular
4612 || h->ref_regular
4613 || (h->u.weakdef != NULL
4614 && ! new_weakdef
4615 && h->u.weakdef->dynindx != -1)))
4616 dynsym = TRUE;
4617 }
4618
4619 /* Check to see if we need to add an indirect symbol for
4620 the default name. */
4621 if (definition
4622 || (!override && h->root.type == bfd_link_hash_common))
4623 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4624 sec, value, &old_bfd, &dynsym))
4625 goto error_free_vers;
4ad4eba5
AM
4626
4627 /* Check the alignment when a common symbol is involved. This
4628 can change when a common symbol is overridden by a normal
4629 definition or a common symbol is ignored due to the old
4630 normal definition. We need to make sure the maximum
4631 alignment is maintained. */
a4d8e49b 4632 if ((old_alignment || common)
4ad4eba5
AM
4633 && h->root.type != bfd_link_hash_common)
4634 {
4635 unsigned int common_align;
4636 unsigned int normal_align;
4637 unsigned int symbol_align;
4638 bfd *normal_bfd;
4639 bfd *common_bfd;
4640
3a81e825
AM
4641 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4642 || h->root.type == bfd_link_hash_defweak);
4643
4ad4eba5
AM
4644 symbol_align = ffs (h->root.u.def.value) - 1;
4645 if (h->root.u.def.section->owner != NULL
0616a280
AM
4646 && (h->root.u.def.section->owner->flags
4647 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
4648 {
4649 normal_align = h->root.u.def.section->alignment_power;
4650 if (normal_align > symbol_align)
4651 normal_align = symbol_align;
4652 }
4653 else
4654 normal_align = symbol_align;
4655
4656 if (old_alignment)
4657 {
4658 common_align = old_alignment;
4659 common_bfd = old_bfd;
4660 normal_bfd = abfd;
4661 }
4662 else
4663 {
4664 common_align = bfd_log2 (isym->st_value);
4665 common_bfd = abfd;
4666 normal_bfd = old_bfd;
4667 }
4668
4669 if (normal_align < common_align)
d07676f8
NC
4670 {
4671 /* PR binutils/2735 */
4672 if (normal_bfd == NULL)
4eca0228 4673 _bfd_error_handler
695344c0 4674 /* xgettext:c-format */
4f3fedcf
AM
4675 (_("Warning: alignment %u of common symbol `%s' in %B is"
4676 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4677 common_bfd, h->root.u.def.section,
4678 1 << common_align, name, 1 << normal_align);
4679 else
4eca0228 4680 _bfd_error_handler
695344c0 4681 /* xgettext:c-format */
d07676f8
NC
4682 (_("Warning: alignment %u of symbol `%s' in %B"
4683 " is smaller than %u in %B"),
4684 normal_bfd, common_bfd,
4685 1 << normal_align, name, 1 << common_align);
4686 }
4ad4eba5
AM
4687 }
4688
83ad0046 4689 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4690 if (isym->st_size != 0
4691 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4692 && (definition || h->size == 0))
4693 {
83ad0046
L
4694 if (h->size != 0
4695 && h->size != isym->st_size
4696 && ! size_change_ok)
4eca0228 4697 _bfd_error_handler
695344c0 4698 /* xgettext:c-format */
d003868e
AM
4699 (_("Warning: size of symbol `%s' changed"
4700 " from %lu in %B to %lu in %B"),
4701 old_bfd, abfd,
4ad4eba5 4702 name, (unsigned long) h->size,
d003868e 4703 (unsigned long) isym->st_size);
4ad4eba5
AM
4704
4705 h->size = isym->st_size;
4706 }
4707
4708 /* If this is a common symbol, then we always want H->SIZE
4709 to be the size of the common symbol. The code just above
4710 won't fix the size if a common symbol becomes larger. We
4711 don't warn about a size change here, because that is
4f3fedcf 4712 covered by --warn-common. Allow changes between different
fcb93ecf 4713 function types. */
4ad4eba5
AM
4714 if (h->root.type == bfd_link_hash_common)
4715 h->size = h->root.u.c.size;
4716
4717 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4718 && ((definition && !new_weak)
4719 || (old_weak && h->root.type == bfd_link_hash_common)
4720 || h->type == STT_NOTYPE))
4ad4eba5 4721 {
2955ec4c
L
4722 unsigned int type = ELF_ST_TYPE (isym->st_info);
4723
4724 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4725 symbol. */
4726 if (type == STT_GNU_IFUNC
4727 && (abfd->flags & DYNAMIC) != 0)
4728 type = STT_FUNC;
4ad4eba5 4729
2955ec4c
L
4730 if (h->type != type)
4731 {
4732 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 4733 /* xgettext:c-format */
4eca0228 4734 _bfd_error_handler
2955ec4c
L
4735 (_("Warning: type of symbol `%s' changed"
4736 " from %d to %d in %B"),
4737 abfd, name, h->type, type);
4738
4739 h->type = type;
4740 }
4ad4eba5
AM
4741 }
4742
54ac0771 4743 /* Merge st_other field. */
b8417128 4744 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4745
c3df8c14 4746 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4747 if (definition
4748 && (sec->flags & SEC_DEBUGGING)
4749 && !bfd_link_relocatable (info))
c3df8c14
AM
4750 dynsym = FALSE;
4751
4f3fedcf
AM
4752 /* Nor should we make plugin symbols dynamic. */
4753 if ((abfd->flags & BFD_PLUGIN) != 0)
4754 dynsym = FALSE;
4755
35fc36a8 4756 if (definition)
35399224
L
4757 {
4758 h->target_internal = isym->st_target_internal;
4759 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4760 }
35fc36a8 4761
4ad4eba5
AM
4762 if (definition && !dynamic)
4763 {
4764 char *p = strchr (name, ELF_VER_CHR);
4765 if (p != NULL && p[1] != ELF_VER_CHR)
4766 {
4767 /* Queue non-default versions so that .symver x, x@FOO
4768 aliases can be checked. */
66eb6687 4769 if (!nondeflt_vers)
4ad4eba5 4770 {
66eb6687
AM
4771 amt = ((isymend - isym + 1)
4772 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4773 nondeflt_vers
4774 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4775 if (!nondeflt_vers)
4776 goto error_free_vers;
4ad4eba5 4777 }
66eb6687 4778 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4779 }
4780 }
4781
4782 if (dynsym && h->dynindx == -1)
4783 {
c152c796 4784 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4785 goto error_free_vers;
f6e332e6 4786 if (h->u.weakdef != NULL
4ad4eba5 4787 && ! new_weakdef
f6e332e6 4788 && h->u.weakdef->dynindx == -1)
4ad4eba5 4789 {
66eb6687 4790 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4791 goto error_free_vers;
4792 }
4793 }
1f599d0e 4794 else if (h->dynindx != -1)
4ad4eba5
AM
4795 /* If the symbol already has a dynamic index, but
4796 visibility says it should not be visible, turn it into
4797 a local symbol. */
4798 switch (ELF_ST_VISIBILITY (h->other))
4799 {
4800 case STV_INTERNAL:
4801 case STV_HIDDEN:
4802 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4803 dynsym = FALSE;
4804 break;
4805 }
4806
aef28989
L
4807 /* Don't add DT_NEEDED for references from the dummy bfd nor
4808 for unmatched symbol. */
4ad4eba5 4809 if (!add_needed
aef28989 4810 && matched
4ad4eba5 4811 && definition
010e5ae2 4812 && ((dynsym
ffa9430d 4813 && h->ref_regular_nonweak
4f3fedcf
AM
4814 && (old_bfd == NULL
4815 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4816 || (h->ref_dynamic_nonweak
010e5ae2 4817 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4818 && !on_needed_list (elf_dt_name (abfd),
4819 htab->needed, NULL))))
4ad4eba5
AM
4820 {
4821 int ret;
4822 const char *soname = elf_dt_name (abfd);
4823
16e4ecc0
AM
4824 info->callbacks->minfo ("%!", soname, old_bfd,
4825 h->root.root.string);
4826
4ad4eba5
AM
4827 /* A symbol from a library loaded via DT_NEEDED of some
4828 other library is referenced by a regular object.
e56f61be 4829 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4830 --no-add-needed is used and the reference was not
4831 a weak one. */
4f3fedcf 4832 if (old_bfd != NULL
b918acf9 4833 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 4834 {
4eca0228 4835 _bfd_error_handler
695344c0 4836 /* xgettext:c-format */
3cbc5de0 4837 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4838 old_bfd, name);
ff5ac77b 4839 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4840 goto error_free_vers;
4841 }
4842
a50b1753 4843 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4844 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4845
4ad4eba5 4846 add_needed = TRUE;
7e9f0867 4847 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4848 if (ret < 0)
4849 goto error_free_vers;
4850
4851 BFD_ASSERT (ret == 0);
4852 }
4853 }
4854 }
4855
66eb6687
AM
4856 if (extversym != NULL)
4857 {
4858 free (extversym);
4859 extversym = NULL;
4860 }
4861
4862 if (isymbuf != NULL)
4863 {
4864 free (isymbuf);
4865 isymbuf = NULL;
4866 }
4867
4868 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4869 {
4870 unsigned int i;
4871
4872 /* Restore the symbol table. */
f45794cb
AM
4873 old_ent = (char *) old_tab + tabsize;
4874 memset (elf_sym_hashes (abfd), 0,
4875 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4876 htab->root.table.table = old_table;
4877 htab->root.table.size = old_size;
4878 htab->root.table.count = old_count;
66eb6687 4879 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4880 htab->root.undefs = old_undefs;
4881 htab->root.undefs_tail = old_undefs_tail;
5b677558
AM
4882 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4883 free (old_strtab);
4884 old_strtab = NULL;
66eb6687
AM
4885 for (i = 0; i < htab->root.table.size; i++)
4886 {
4887 struct bfd_hash_entry *p;
4888 struct elf_link_hash_entry *h;
3e0882af
L
4889 bfd_size_type size;
4890 unsigned int alignment_power;
66eb6687
AM
4891
4892 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4893 {
4894 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4895 if (h->root.type == bfd_link_hash_warning)
4896 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4897
3e0882af
L
4898 /* Preserve the maximum alignment and size for common
4899 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4900 since it can still be loaded at run time by another
3e0882af
L
4901 dynamic lib. */
4902 if (h->root.type == bfd_link_hash_common)
4903 {
4904 size = h->root.u.c.size;
4905 alignment_power = h->root.u.c.p->alignment_power;
4906 }
4907 else
4908 {
4909 size = 0;
4910 alignment_power = 0;
4911 }
66eb6687
AM
4912 memcpy (p, old_ent, htab->root.table.entsize);
4913 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4914 h = (struct elf_link_hash_entry *) p;
4915 if (h->root.type == bfd_link_hash_warning)
4916 {
4917 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4918 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4919 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4920 }
a4542f1b 4921 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4922 {
4923 if (size > h->root.u.c.size)
4924 h->root.u.c.size = size;
4925 if (alignment_power > h->root.u.c.p->alignment_power)
4926 h->root.u.c.p->alignment_power = alignment_power;
4927 }
66eb6687
AM
4928 }
4929 }
4930
5061a885
AM
4931 /* Make a special call to the linker "notice" function to
4932 tell it that symbols added for crefs may need to be removed. */
e5034e59 4933 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4934 goto error_free_vers;
5061a885 4935
66eb6687
AM
4936 free (old_tab);
4937 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4938 alloc_mark);
4939 if (nondeflt_vers != NULL)
4940 free (nondeflt_vers);
4941 return TRUE;
4942 }
2de92251 4943
66eb6687
AM
4944 if (old_tab != NULL)
4945 {
e5034e59 4946 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4947 goto error_free_vers;
66eb6687
AM
4948 free (old_tab);
4949 old_tab = NULL;
4950 }
4951
c6e8a9a8
L
4952 /* Now that all the symbols from this input file are created, if
4953 not performing a relocatable link, handle .symver foo, foo@BAR
4954 such that any relocs against foo become foo@BAR. */
0e1862bb 4955 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 4956 {
ef53be89 4957 size_t cnt, symidx;
4ad4eba5
AM
4958
4959 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4960 {
4961 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4962 char *shortname, *p;
4963
4964 p = strchr (h->root.root.string, ELF_VER_CHR);
4965 if (p == NULL
4966 || (h->root.type != bfd_link_hash_defined
4967 && h->root.type != bfd_link_hash_defweak))
4968 continue;
4969
4970 amt = p - h->root.root.string;
a50b1753 4971 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4972 if (!shortname)
4973 goto error_free_vers;
4ad4eba5
AM
4974 memcpy (shortname, h->root.root.string, amt);
4975 shortname[amt] = '\0';
4976
4977 hi = (struct elf_link_hash_entry *)
66eb6687 4978 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4979 FALSE, FALSE, FALSE);
4980 if (hi != NULL
4981 && hi->root.type == h->root.type
4982 && hi->root.u.def.value == h->root.u.def.value
4983 && hi->root.u.def.section == h->root.u.def.section)
4984 {
4985 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4986 hi->root.type = bfd_link_hash_indirect;
4987 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4988 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4989 sym_hash = elf_sym_hashes (abfd);
4990 if (sym_hash)
4991 for (symidx = 0; symidx < extsymcount; ++symidx)
4992 if (sym_hash[symidx] == hi)
4993 {
4994 sym_hash[symidx] = h;
4995 break;
4996 }
4997 }
4998 free (shortname);
4999 }
5000 free (nondeflt_vers);
5001 nondeflt_vers = NULL;
5002 }
5003
4ad4eba5
AM
5004 /* Now set the weakdefs field correctly for all the weak defined
5005 symbols we found. The only way to do this is to search all the
5006 symbols. Since we only need the information for non functions in
5007 dynamic objects, that's the only time we actually put anything on
5008 the list WEAKS. We need this information so that if a regular
5009 object refers to a symbol defined weakly in a dynamic object, the
5010 real symbol in the dynamic object is also put in the dynamic
5011 symbols; we also must arrange for both symbols to point to the
5012 same memory location. We could handle the general case of symbol
5013 aliasing, but a general symbol alias can only be generated in
5014 assembler code, handling it correctly would be very time
5015 consuming, and other ELF linkers don't handle general aliasing
5016 either. */
5017 if (weaks != NULL)
5018 {
5019 struct elf_link_hash_entry **hpp;
5020 struct elf_link_hash_entry **hppend;
5021 struct elf_link_hash_entry **sorted_sym_hash;
5022 struct elf_link_hash_entry *h;
5023 size_t sym_count;
5024
5025 /* Since we have to search the whole symbol list for each weak
5026 defined symbol, search time for N weak defined symbols will be
5027 O(N^2). Binary search will cut it down to O(NlogN). */
ef53be89
AM
5028 amt = extsymcount;
5029 amt *= sizeof (struct elf_link_hash_entry *);
a50b1753 5030 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
5031 if (sorted_sym_hash == NULL)
5032 goto error_return;
5033 sym_hash = sorted_sym_hash;
5034 hpp = elf_sym_hashes (abfd);
5035 hppend = hpp + extsymcount;
5036 sym_count = 0;
5037 for (; hpp < hppend; hpp++)
5038 {
5039 h = *hpp;
5040 if (h != NULL
5041 && h->root.type == bfd_link_hash_defined
fcb93ecf 5042 && !bed->is_function_type (h->type))
4ad4eba5
AM
5043 {
5044 *sym_hash = h;
5045 sym_hash++;
5046 sym_count++;
5047 }
5048 }
5049
5050 qsort (sorted_sym_hash, sym_count,
5051 sizeof (struct elf_link_hash_entry *),
5052 elf_sort_symbol);
5053
5054 while (weaks != NULL)
5055 {
5056 struct elf_link_hash_entry *hlook;
5057 asection *slook;
5058 bfd_vma vlook;
ed54588d 5059 size_t i, j, idx = 0;
4ad4eba5
AM
5060
5061 hlook = weaks;
f6e332e6
AM
5062 weaks = hlook->u.weakdef;
5063 hlook->u.weakdef = NULL;
4ad4eba5
AM
5064
5065 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5066 || hlook->root.type == bfd_link_hash_defweak
5067 || hlook->root.type == bfd_link_hash_common
5068 || hlook->root.type == bfd_link_hash_indirect);
5069 slook = hlook->root.u.def.section;
5070 vlook = hlook->root.u.def.value;
5071
4ad4eba5
AM
5072 i = 0;
5073 j = sym_count;
14160578 5074 while (i != j)
4ad4eba5
AM
5075 {
5076 bfd_signed_vma vdiff;
5077 idx = (i + j) / 2;
14160578 5078 h = sorted_sym_hash[idx];
4ad4eba5
AM
5079 vdiff = vlook - h->root.u.def.value;
5080 if (vdiff < 0)
5081 j = idx;
5082 else if (vdiff > 0)
5083 i = idx + 1;
5084 else
5085 {
d3435ae8 5086 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5087 if (sdiff < 0)
5088 j = idx;
5089 else if (sdiff > 0)
5090 i = idx + 1;
5091 else
14160578 5092 break;
4ad4eba5
AM
5093 }
5094 }
5095
5096 /* We didn't find a value/section match. */
14160578 5097 if (i == j)
4ad4eba5
AM
5098 continue;
5099
14160578
AM
5100 /* With multiple aliases, or when the weak symbol is already
5101 strongly defined, we have multiple matching symbols and
5102 the binary search above may land on any of them. Step
5103 one past the matching symbol(s). */
5104 while (++idx != j)
5105 {
5106 h = sorted_sym_hash[idx];
5107 if (h->root.u.def.section != slook
5108 || h->root.u.def.value != vlook)
5109 break;
5110 }
5111
5112 /* Now look back over the aliases. Since we sorted by size
5113 as well as value and section, we'll choose the one with
5114 the largest size. */
5115 while (idx-- != i)
4ad4eba5 5116 {
14160578 5117 h = sorted_sym_hash[idx];
4ad4eba5
AM
5118
5119 /* Stop if value or section doesn't match. */
14160578
AM
5120 if (h->root.u.def.section != slook
5121 || h->root.u.def.value != vlook)
4ad4eba5
AM
5122 break;
5123 else if (h != hlook)
5124 {
f6e332e6 5125 hlook->u.weakdef = h;
4ad4eba5
AM
5126
5127 /* If the weak definition is in the list of dynamic
5128 symbols, make sure the real definition is put
5129 there as well. */
5130 if (hlook->dynindx != -1 && h->dynindx == -1)
5131 {
c152c796 5132 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5133 {
5134 err_free_sym_hash:
5135 free (sorted_sym_hash);
5136 goto error_return;
5137 }
4ad4eba5
AM
5138 }
5139
5140 /* If the real definition is in the list of dynamic
5141 symbols, make sure the weak definition is put
5142 there as well. If we don't do this, then the
5143 dynamic loader might not merge the entries for the
5144 real definition and the weak definition. */
5145 if (h->dynindx != -1 && hlook->dynindx == -1)
5146 {
c152c796 5147 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5148 goto err_free_sym_hash;
4ad4eba5
AM
5149 }
5150 break;
5151 }
5152 }
5153 }
5154
5155 free (sorted_sym_hash);
5156 }
5157
33177bb1
AM
5158 if (bed->check_directives
5159 && !(*bed->check_directives) (abfd, info))
5160 return FALSE;
85fbca6a 5161
d9689752
L
5162 if (!info->check_relocs_after_open_input
5163 && !_bfd_elf_link_check_relocs (abfd, info))
5164 return FALSE;
4ad4eba5
AM
5165
5166 /* If this is a non-traditional link, try to optimize the handling
5167 of the .stab/.stabstr sections. */
5168 if (! dynamic
5169 && ! info->traditional_format
66eb6687 5170 && is_elf_hash_table (htab)
4ad4eba5
AM
5171 && (info->strip != strip_all && info->strip != strip_debugger))
5172 {
5173 asection *stabstr;
5174
5175 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5176 if (stabstr != NULL)
5177 {
5178 bfd_size_type string_offset = 0;
5179 asection *stab;
5180
5181 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5182 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5183 && (!stab->name[5] ||
5184 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5185 && (stab->flags & SEC_MERGE) == 0
5186 && !bfd_is_abs_section (stab->output_section))
5187 {
5188 struct bfd_elf_section_data *secdata;
5189
5190 secdata = elf_section_data (stab);
66eb6687
AM
5191 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5192 stabstr, &secdata->sec_info,
4ad4eba5
AM
5193 &string_offset))
5194 goto error_return;
5195 if (secdata->sec_info)
dbaa2011 5196 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5197 }
5198 }
5199 }
5200
66eb6687 5201 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5202 {
5203 /* Add this bfd to the loaded list. */
5204 struct elf_link_loaded_list *n;
5205
ca4be51c 5206 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5207 if (n == NULL)
5208 goto error_return;
5209 n->abfd = abfd;
66eb6687
AM
5210 n->next = htab->loaded;
5211 htab->loaded = n;
4ad4eba5
AM
5212 }
5213
5214 return TRUE;
5215
5216 error_free_vers:
66eb6687
AM
5217 if (old_tab != NULL)
5218 free (old_tab);
5b677558
AM
5219 if (old_strtab != NULL)
5220 free (old_strtab);
4ad4eba5
AM
5221 if (nondeflt_vers != NULL)
5222 free (nondeflt_vers);
5223 if (extversym != NULL)
5224 free (extversym);
5225 error_free_sym:
5226 if (isymbuf != NULL)
5227 free (isymbuf);
5228 error_return:
5229 return FALSE;
5230}
5231
8387904d
AM
5232/* Return the linker hash table entry of a symbol that might be
5233 satisfied by an archive symbol. Return -1 on error. */
5234
5235struct elf_link_hash_entry *
5236_bfd_elf_archive_symbol_lookup (bfd *abfd,
5237 struct bfd_link_info *info,
5238 const char *name)
5239{
5240 struct elf_link_hash_entry *h;
5241 char *p, *copy;
5242 size_t len, first;
5243
2a41f396 5244 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5245 if (h != NULL)
5246 return h;
5247
5248 /* If this is a default version (the name contains @@), look up the
5249 symbol again with only one `@' as well as without the version.
5250 The effect is that references to the symbol with and without the
5251 version will be matched by the default symbol in the archive. */
5252
5253 p = strchr (name, ELF_VER_CHR);
5254 if (p == NULL || p[1] != ELF_VER_CHR)
5255 return h;
5256
5257 /* First check with only one `@'. */
5258 len = strlen (name);
a50b1753 5259 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5260 if (copy == NULL)
5261 return (struct elf_link_hash_entry *) 0 - 1;
5262
5263 first = p - name + 1;
5264 memcpy (copy, name, first);
5265 memcpy (copy + first, name + first + 1, len - first);
5266
2a41f396 5267 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5268 if (h == NULL)
5269 {
5270 /* We also need to check references to the symbol without the
5271 version. */
5272 copy[first - 1] = '\0';
5273 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5274 FALSE, FALSE, TRUE);
8387904d
AM
5275 }
5276
5277 bfd_release (abfd, copy);
5278 return h;
5279}
5280
0ad989f9 5281/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5282 don't use _bfd_generic_link_add_archive_symbols because we need to
5283 handle versioned symbols.
0ad989f9
L
5284
5285 Fortunately, ELF archive handling is simpler than that done by
5286 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5287 oddities. In ELF, if we find a symbol in the archive map, and the
5288 symbol is currently undefined, we know that we must pull in that
5289 object file.
5290
5291 Unfortunately, we do have to make multiple passes over the symbol
5292 table until nothing further is resolved. */
5293
4ad4eba5
AM
5294static bfd_boolean
5295elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5296{
5297 symindex c;
13e570f8 5298 unsigned char *included = NULL;
0ad989f9
L
5299 carsym *symdefs;
5300 bfd_boolean loop;
5301 bfd_size_type amt;
8387904d
AM
5302 const struct elf_backend_data *bed;
5303 struct elf_link_hash_entry * (*archive_symbol_lookup)
5304 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5305
5306 if (! bfd_has_map (abfd))
5307 {
5308 /* An empty archive is a special case. */
5309 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5310 return TRUE;
5311 bfd_set_error (bfd_error_no_armap);
5312 return FALSE;
5313 }
5314
5315 /* Keep track of all symbols we know to be already defined, and all
5316 files we know to be already included. This is to speed up the
5317 second and subsequent passes. */
5318 c = bfd_ardata (abfd)->symdef_count;
5319 if (c == 0)
5320 return TRUE;
5321 amt = c;
13e570f8
AM
5322 amt *= sizeof (*included);
5323 included = (unsigned char *) bfd_zmalloc (amt);
5324 if (included == NULL)
5325 return FALSE;
0ad989f9
L
5326
5327 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5328 bed = get_elf_backend_data (abfd);
5329 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5330
5331 do
5332 {
5333 file_ptr last;
5334 symindex i;
5335 carsym *symdef;
5336 carsym *symdefend;
5337
5338 loop = FALSE;
5339 last = -1;
5340
5341 symdef = symdefs;
5342 symdefend = symdef + c;
5343 for (i = 0; symdef < symdefend; symdef++, i++)
5344 {
5345 struct elf_link_hash_entry *h;
5346 bfd *element;
5347 struct bfd_link_hash_entry *undefs_tail;
5348 symindex mark;
5349
13e570f8 5350 if (included[i])
0ad989f9
L
5351 continue;
5352 if (symdef->file_offset == last)
5353 {
5354 included[i] = TRUE;
5355 continue;
5356 }
5357
8387904d
AM
5358 h = archive_symbol_lookup (abfd, info, symdef->name);
5359 if (h == (struct elf_link_hash_entry *) 0 - 1)
5360 goto error_return;
0ad989f9
L
5361
5362 if (h == NULL)
5363 continue;
5364
5365 if (h->root.type == bfd_link_hash_common)
5366 {
5367 /* We currently have a common symbol. The archive map contains
5368 a reference to this symbol, so we may want to include it. We
5369 only want to include it however, if this archive element
5370 contains a definition of the symbol, not just another common
5371 declaration of it.
5372
5373 Unfortunately some archivers (including GNU ar) will put
5374 declarations of common symbols into their archive maps, as
5375 well as real definitions, so we cannot just go by the archive
5376 map alone. Instead we must read in the element's symbol
5377 table and check that to see what kind of symbol definition
5378 this is. */
5379 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5380 continue;
5381 }
5382 else if (h->root.type != bfd_link_hash_undefined)
5383 {
5384 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5385 /* Symbol must be defined. Don't check it again. */
5386 included[i] = TRUE;
0ad989f9
L
5387 continue;
5388 }
5389
5390 /* We need to include this archive member. */
5391 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5392 if (element == NULL)
5393 goto error_return;
5394
5395 if (! bfd_check_format (element, bfd_object))
5396 goto error_return;
5397
0ad989f9
L
5398 undefs_tail = info->hash->undefs_tail;
5399
0e144ba7
AM
5400 if (!(*info->callbacks
5401 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5402 continue;
0e144ba7 5403 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5404 goto error_return;
5405
5406 /* If there are any new undefined symbols, we need to make
5407 another pass through the archive in order to see whether
5408 they can be defined. FIXME: This isn't perfect, because
5409 common symbols wind up on undefs_tail and because an
5410 undefined symbol which is defined later on in this pass
5411 does not require another pass. This isn't a bug, but it
5412 does make the code less efficient than it could be. */
5413 if (undefs_tail != info->hash->undefs_tail)
5414 loop = TRUE;
5415
5416 /* Look backward to mark all symbols from this object file
5417 which we have already seen in this pass. */
5418 mark = i;
5419 do
5420 {
5421 included[mark] = TRUE;
5422 if (mark == 0)
5423 break;
5424 --mark;
5425 }
5426 while (symdefs[mark].file_offset == symdef->file_offset);
5427
5428 /* We mark subsequent symbols from this object file as we go
5429 on through the loop. */
5430 last = symdef->file_offset;
5431 }
5432 }
5433 while (loop);
5434
0ad989f9
L
5435 free (included);
5436
5437 return TRUE;
5438
5439 error_return:
0ad989f9
L
5440 if (included != NULL)
5441 free (included);
5442 return FALSE;
5443}
4ad4eba5
AM
5444
5445/* Given an ELF BFD, add symbols to the global hash table as
5446 appropriate. */
5447
5448bfd_boolean
5449bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5450{
5451 switch (bfd_get_format (abfd))
5452 {
5453 case bfd_object:
5454 return elf_link_add_object_symbols (abfd, info);
5455 case bfd_archive:
5456 return elf_link_add_archive_symbols (abfd, info);
5457 default:
5458 bfd_set_error (bfd_error_wrong_format);
5459 return FALSE;
5460 }
5461}
5a580b3a 5462\f
14b1c01e
AM
5463struct hash_codes_info
5464{
5465 unsigned long *hashcodes;
5466 bfd_boolean error;
5467};
a0c8462f 5468
5a580b3a
AM
5469/* This function will be called though elf_link_hash_traverse to store
5470 all hash value of the exported symbols in an array. */
5471
5472static bfd_boolean
5473elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5474{
a50b1753 5475 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5476 const char *name;
5a580b3a
AM
5477 unsigned long ha;
5478 char *alc = NULL;
5479
5a580b3a
AM
5480 /* Ignore indirect symbols. These are added by the versioning code. */
5481 if (h->dynindx == -1)
5482 return TRUE;
5483
5484 name = h->root.root.string;
422f1182 5485 if (h->versioned >= versioned)
5a580b3a 5486 {
422f1182
L
5487 char *p = strchr (name, ELF_VER_CHR);
5488 if (p != NULL)
14b1c01e 5489 {
422f1182
L
5490 alc = (char *) bfd_malloc (p - name + 1);
5491 if (alc == NULL)
5492 {
5493 inf->error = TRUE;
5494 return FALSE;
5495 }
5496 memcpy (alc, name, p - name);
5497 alc[p - name] = '\0';
5498 name = alc;
14b1c01e 5499 }
5a580b3a
AM
5500 }
5501
5502 /* Compute the hash value. */
5503 ha = bfd_elf_hash (name);
5504
5505 /* Store the found hash value in the array given as the argument. */
14b1c01e 5506 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5507
5508 /* And store it in the struct so that we can put it in the hash table
5509 later. */
f6e332e6 5510 h->u.elf_hash_value = ha;
5a580b3a
AM
5511
5512 if (alc != NULL)
5513 free (alc);
5514
5515 return TRUE;
5516}
5517
fdc90cb4
JJ
5518struct collect_gnu_hash_codes
5519{
5520 bfd *output_bfd;
5521 const struct elf_backend_data *bed;
5522 unsigned long int nsyms;
5523 unsigned long int maskbits;
5524 unsigned long int *hashcodes;
5525 unsigned long int *hashval;
5526 unsigned long int *indx;
5527 unsigned long int *counts;
5528 bfd_vma *bitmask;
5529 bfd_byte *contents;
5530 long int min_dynindx;
5531 unsigned long int bucketcount;
5532 unsigned long int symindx;
5533 long int local_indx;
5534 long int shift1, shift2;
5535 unsigned long int mask;
14b1c01e 5536 bfd_boolean error;
fdc90cb4
JJ
5537};
5538
5539/* This function will be called though elf_link_hash_traverse to store
5540 all hash value of the exported symbols in an array. */
5541
5542static bfd_boolean
5543elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5544{
a50b1753 5545 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5546 const char *name;
fdc90cb4
JJ
5547 unsigned long ha;
5548 char *alc = NULL;
5549
fdc90cb4
JJ
5550 /* Ignore indirect symbols. These are added by the versioning code. */
5551 if (h->dynindx == -1)
5552 return TRUE;
5553
5554 /* Ignore also local symbols and undefined symbols. */
5555 if (! (*s->bed->elf_hash_symbol) (h))
5556 return TRUE;
5557
5558 name = h->root.root.string;
422f1182 5559 if (h->versioned >= versioned)
fdc90cb4 5560 {
422f1182
L
5561 char *p = strchr (name, ELF_VER_CHR);
5562 if (p != NULL)
14b1c01e 5563 {
422f1182
L
5564 alc = (char *) bfd_malloc (p - name + 1);
5565 if (alc == NULL)
5566 {
5567 s->error = TRUE;
5568 return FALSE;
5569 }
5570 memcpy (alc, name, p - name);
5571 alc[p - name] = '\0';
5572 name = alc;
14b1c01e 5573 }
fdc90cb4
JJ
5574 }
5575
5576 /* Compute the hash value. */
5577 ha = bfd_elf_gnu_hash (name);
5578
5579 /* Store the found hash value in the array for compute_bucket_count,
5580 and also for .dynsym reordering purposes. */
5581 s->hashcodes[s->nsyms] = ha;
5582 s->hashval[h->dynindx] = ha;
5583 ++s->nsyms;
5584 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5585 s->min_dynindx = h->dynindx;
5586
5587 if (alc != NULL)
5588 free (alc);
5589
5590 return TRUE;
5591}
5592
5593/* This function will be called though elf_link_hash_traverse to do
5594 final dynaminc symbol renumbering. */
5595
5596static bfd_boolean
5597elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5598{
a50b1753 5599 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5600 unsigned long int bucket;
5601 unsigned long int val;
5602
fdc90cb4
JJ
5603 /* Ignore indirect symbols. */
5604 if (h->dynindx == -1)
5605 return TRUE;
5606
5607 /* Ignore also local symbols and undefined symbols. */
5608 if (! (*s->bed->elf_hash_symbol) (h))
5609 {
5610 if (h->dynindx >= s->min_dynindx)
5611 h->dynindx = s->local_indx++;
5612 return TRUE;
5613 }
5614
5615 bucket = s->hashval[h->dynindx] % s->bucketcount;
5616 val = (s->hashval[h->dynindx] >> s->shift1)
5617 & ((s->maskbits >> s->shift1) - 1);
5618 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5619 s->bitmask[val]
5620 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5621 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5622 if (s->counts[bucket] == 1)
5623 /* Last element terminates the chain. */
5624 val |= 1;
5625 bfd_put_32 (s->output_bfd, val,
5626 s->contents + (s->indx[bucket] - s->symindx) * 4);
5627 --s->counts[bucket];
5628 h->dynindx = s->indx[bucket]++;
5629 return TRUE;
5630}
5631
5632/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5633
5634bfd_boolean
5635_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5636{
5637 return !(h->forced_local
5638 || h->root.type == bfd_link_hash_undefined
5639 || h->root.type == bfd_link_hash_undefweak
5640 || ((h->root.type == bfd_link_hash_defined
5641 || h->root.type == bfd_link_hash_defweak)
5642 && h->root.u.def.section->output_section == NULL));
5643}
5644
5a580b3a
AM
5645/* Array used to determine the number of hash table buckets to use
5646 based on the number of symbols there are. If there are fewer than
5647 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5648 fewer than 37 we use 17 buckets, and so forth. We never use more
5649 than 32771 buckets. */
5650
5651static const size_t elf_buckets[] =
5652{
5653 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5654 16411, 32771, 0
5655};
5656
5657/* Compute bucket count for hashing table. We do not use a static set
5658 of possible tables sizes anymore. Instead we determine for all
5659 possible reasonable sizes of the table the outcome (i.e., the
5660 number of collisions etc) and choose the best solution. The
5661 weighting functions are not too simple to allow the table to grow
5662 without bounds. Instead one of the weighting factors is the size.
5663 Therefore the result is always a good payoff between few collisions
5664 (= short chain lengths) and table size. */
5665static size_t
b20dd2ce 5666compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5667 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5668 unsigned long int nsyms,
5669 int gnu_hash)
5a580b3a 5670{
5a580b3a 5671 size_t best_size = 0;
5a580b3a 5672 unsigned long int i;
5a580b3a 5673
5a580b3a
AM
5674 /* We have a problem here. The following code to optimize the table
5675 size requires an integer type with more the 32 bits. If
5676 BFD_HOST_U_64_BIT is set we know about such a type. */
5677#ifdef BFD_HOST_U_64_BIT
5678 if (info->optimize)
5679 {
5a580b3a
AM
5680 size_t minsize;
5681 size_t maxsize;
5682 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5683 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5684 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5685 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5686 unsigned long int *counts;
d40f3da9 5687 bfd_size_type amt;
0883b6e0 5688 unsigned int no_improvement_count = 0;
5a580b3a
AM
5689
5690 /* Possible optimization parameters: if we have NSYMS symbols we say
5691 that the hashing table must at least have NSYMS/4 and at most
5692 2*NSYMS buckets. */
5693 minsize = nsyms / 4;
5694 if (minsize == 0)
5695 minsize = 1;
5696 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5697 if (gnu_hash)
5698 {
5699 if (minsize < 2)
5700 minsize = 2;
5701 if ((best_size & 31) == 0)
5702 ++best_size;
5703 }
5a580b3a
AM
5704
5705 /* Create array where we count the collisions in. We must use bfd_malloc
5706 since the size could be large. */
5707 amt = maxsize;
5708 amt *= sizeof (unsigned long int);
a50b1753 5709 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5710 if (counts == NULL)
fdc90cb4 5711 return 0;
5a580b3a
AM
5712
5713 /* Compute the "optimal" size for the hash table. The criteria is a
5714 minimal chain length. The minor criteria is (of course) the size
5715 of the table. */
5716 for (i = minsize; i < maxsize; ++i)
5717 {
5718 /* Walk through the array of hashcodes and count the collisions. */
5719 BFD_HOST_U_64_BIT max;
5720 unsigned long int j;
5721 unsigned long int fact;
5722
fdc90cb4
JJ
5723 if (gnu_hash && (i & 31) == 0)
5724 continue;
5725
5a580b3a
AM
5726 memset (counts, '\0', i * sizeof (unsigned long int));
5727
5728 /* Determine how often each hash bucket is used. */
5729 for (j = 0; j < nsyms; ++j)
5730 ++counts[hashcodes[j] % i];
5731
5732 /* For the weight function we need some information about the
5733 pagesize on the target. This is information need not be 100%
5734 accurate. Since this information is not available (so far) we
5735 define it here to a reasonable default value. If it is crucial
5736 to have a better value some day simply define this value. */
5737# ifndef BFD_TARGET_PAGESIZE
5738# define BFD_TARGET_PAGESIZE (4096)
5739# endif
5740
fdc90cb4
JJ
5741 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5742 and the chains. */
5743 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5744
5745# if 1
5746 /* Variant 1: optimize for short chains. We add the squares
5747 of all the chain lengths (which favors many small chain
5748 over a few long chains). */
5749 for (j = 0; j < i; ++j)
5750 max += counts[j] * counts[j];
5751
5752 /* This adds penalties for the overall size of the table. */
fdc90cb4 5753 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5754 max *= fact * fact;
5755# else
5756 /* Variant 2: Optimize a lot more for small table. Here we
5757 also add squares of the size but we also add penalties for
5758 empty slots (the +1 term). */
5759 for (j = 0; j < i; ++j)
5760 max += (1 + counts[j]) * (1 + counts[j]);
5761
5762 /* The overall size of the table is considered, but not as
5763 strong as in variant 1, where it is squared. */
fdc90cb4 5764 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5765 max *= fact;
5766# endif
5767
5768 /* Compare with current best results. */
5769 if (max < best_chlen)
5770 {
5771 best_chlen = max;
5772 best_size = i;
ca4be51c 5773 no_improvement_count = 0;
5a580b3a 5774 }
0883b6e0
NC
5775 /* PR 11843: Avoid futile long searches for the best bucket size
5776 when there are a large number of symbols. */
5777 else if (++no_improvement_count == 100)
5778 break;
5a580b3a
AM
5779 }
5780
5781 free (counts);
5782 }
5783 else
5784#endif /* defined (BFD_HOST_U_64_BIT) */
5785 {
5786 /* This is the fallback solution if no 64bit type is available or if we
5787 are not supposed to spend much time on optimizations. We select the
5788 bucket count using a fixed set of numbers. */
5789 for (i = 0; elf_buckets[i] != 0; i++)
5790 {
5791 best_size = elf_buckets[i];
fdc90cb4 5792 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5793 break;
5794 }
fdc90cb4
JJ
5795 if (gnu_hash && best_size < 2)
5796 best_size = 2;
5a580b3a
AM
5797 }
5798
5a580b3a
AM
5799 return best_size;
5800}
5801
d0bf826b
AM
5802/* Size any SHT_GROUP section for ld -r. */
5803
5804bfd_boolean
5805_bfd_elf_size_group_sections (struct bfd_link_info *info)
5806{
5807 bfd *ibfd;
5808
c72f2fb2 5809 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5810 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5811 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5812 return FALSE;
5813 return TRUE;
5814}
5815
04c3a755
NS
5816/* Set a default stack segment size. The value in INFO wins. If it
5817 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5818 undefined it is initialized. */
5819
5820bfd_boolean
5821bfd_elf_stack_segment_size (bfd *output_bfd,
5822 struct bfd_link_info *info,
5823 const char *legacy_symbol,
5824 bfd_vma default_size)
5825{
5826 struct elf_link_hash_entry *h = NULL;
5827
5828 /* Look for legacy symbol. */
5829 if (legacy_symbol)
5830 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5831 FALSE, FALSE, FALSE);
5832 if (h && (h->root.type == bfd_link_hash_defined
5833 || h->root.type == bfd_link_hash_defweak)
5834 && h->def_regular
5835 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5836 {
5837 /* The symbol has no type if specified on the command line. */
5838 h->type = STT_OBJECT;
5839 if (info->stacksize)
695344c0 5840 /* xgettext:c-format */
4eca0228
AM
5841 _bfd_error_handler (_("%B: stack size specified and %s set"),
5842 output_bfd, legacy_symbol);
04c3a755 5843 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 5844 /* xgettext:c-format */
4eca0228
AM
5845 _bfd_error_handler (_("%B: %s not absolute"),
5846 output_bfd, legacy_symbol);
04c3a755
NS
5847 else
5848 info->stacksize = h->root.u.def.value;
5849 }
5850
5851 if (!info->stacksize)
5852 /* If the user didn't set a size, or explicitly inhibit the
5853 size, set it now. */
5854 info->stacksize = default_size;
5855
5856 /* Provide the legacy symbol, if it is referenced. */
5857 if (h && (h->root.type == bfd_link_hash_undefined
5858 || h->root.type == bfd_link_hash_undefweak))
5859 {
5860 struct bfd_link_hash_entry *bh = NULL;
5861
5862 if (!(_bfd_generic_link_add_one_symbol
5863 (info, output_bfd, legacy_symbol,
5864 BSF_GLOBAL, bfd_abs_section_ptr,
5865 info->stacksize >= 0 ? info->stacksize : 0,
5866 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5867 return FALSE;
5868
5869 h = (struct elf_link_hash_entry *) bh;
5870 h->def_regular = 1;
5871 h->type = STT_OBJECT;
5872 }
5873
5874 return TRUE;
5875}
5876
b531344c
MR
5877/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5878
5879struct elf_gc_sweep_symbol_info
5880{
5881 struct bfd_link_info *info;
5882 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5883 bfd_boolean);
5884};
5885
5886static bfd_boolean
5887elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5888{
5889 if (!h->mark
5890 && (((h->root.type == bfd_link_hash_defined
5891 || h->root.type == bfd_link_hash_defweak)
5892 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5893 && h->root.u.def.section->gc_mark))
5894 || h->root.type == bfd_link_hash_undefined
5895 || h->root.type == bfd_link_hash_undefweak))
5896 {
5897 struct elf_gc_sweep_symbol_info *inf;
5898
5899 inf = (struct elf_gc_sweep_symbol_info *) data;
5900 (*inf->hide_symbol) (inf->info, h, TRUE);
5901 h->def_regular = 0;
5902 h->ref_regular = 0;
5903 h->ref_regular_nonweak = 0;
5904 }
5905
5906 return TRUE;
5907}
5908
5a580b3a
AM
5909/* Set up the sizes and contents of the ELF dynamic sections. This is
5910 called by the ELF linker emulation before_allocation routine. We
5911 must set the sizes of the sections before the linker sets the
5912 addresses of the various sections. */
5913
5914bfd_boolean
5915bfd_elf_size_dynamic_sections (bfd *output_bfd,
5916 const char *soname,
5917 const char *rpath,
5918 const char *filter_shlib,
7ee314fa
AM
5919 const char *audit,
5920 const char *depaudit,
5a580b3a
AM
5921 const char * const *auxiliary_filters,
5922 struct bfd_link_info *info,
fd91d419 5923 asection **sinterpptr)
5a580b3a 5924{
ef53be89 5925 size_t soname_indx;
5a580b3a
AM
5926 bfd *dynobj;
5927 const struct elf_backend_data *bed;
28caa186 5928 struct elf_info_failed asvinfo;
5a580b3a
AM
5929
5930 *sinterpptr = NULL;
5931
ef53be89 5932 soname_indx = (size_t) -1;
5a580b3a
AM
5933
5934 if (!is_elf_hash_table (info->hash))
5935 return TRUE;
5936
6bfdb61b 5937 bed = get_elf_backend_data (output_bfd);
04c3a755 5938
b531344c
MR
5939 if (info->gc_sections && bed->can_gc_sections)
5940 {
5941 struct elf_gc_sweep_symbol_info sweep_info;
5942 unsigned long section_sym_count;
5943
5944 /* Remove the symbols that were in the swept sections from the
5945 dynamic symbol table. GCFIXME: Anyone know how to get them
5946 out of the static symbol table as well? */
5947 sweep_info.info = info;
5948 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
5949 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
5950 &sweep_info);
5951
5952 _bfd_elf_link_renumber_dynsyms (output_bfd, info, &section_sym_count);
5953 }
5954
04c3a755
NS
5955 /* Any syms created from now on start with -1 in
5956 got.refcount/offset and plt.refcount/offset. */
5957 elf_hash_table (info)->init_got_refcount
5958 = elf_hash_table (info)->init_got_offset;
5959 elf_hash_table (info)->init_plt_refcount
5960 = elf_hash_table (info)->init_plt_offset;
5961
0e1862bb 5962 if (bfd_link_relocatable (info)
04c3a755
NS
5963 && !_bfd_elf_size_group_sections (info))
5964 return FALSE;
5965
5966 /* The backend may have to create some sections regardless of whether
5967 we're dynamic or not. */
5968 if (bed->elf_backend_always_size_sections
5969 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5970 return FALSE;
5971
5972 /* Determine any GNU_STACK segment requirements, after the backend
5973 has had a chance to set a default segment size. */
5a580b3a 5974 if (info->execstack)
12bd6957 5975 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5976 else if (info->noexecstack)
12bd6957 5977 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5978 else
5979 {
5980 bfd *inputobj;
5981 asection *notesec = NULL;
5982 int exec = 0;
5983
5984 for (inputobj = info->input_bfds;
5985 inputobj;
c72f2fb2 5986 inputobj = inputobj->link.next)
5a580b3a
AM
5987 {
5988 asection *s;
5989
a92c088a
L
5990 if (inputobj->flags
5991 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5992 continue;
5993 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5994 if (s)
5995 {
5996 if (s->flags & SEC_CODE)
5997 exec = PF_X;
5998 notesec = s;
5999 }
6bfdb61b 6000 else if (bed->default_execstack)
5a580b3a
AM
6001 exec = PF_X;
6002 }
04c3a755 6003 if (notesec || info->stacksize > 0)
12bd6957 6004 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 6005 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
6006 && notesec->output_section != bfd_abs_section_ptr)
6007 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
6008 }
6009
5a580b3a
AM
6010 dynobj = elf_hash_table (info)->dynobj;
6011
9a2a56cc 6012 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
6013 {
6014 struct elf_info_failed eif;
6015 struct elf_link_hash_entry *h;
6016 asection *dynstr;
6017 struct bfd_elf_version_tree *t;
6018 struct bfd_elf_version_expr *d;
046183de 6019 asection *s;
5a580b3a
AM
6020 bfd_boolean all_defined;
6021
3d4d4302 6022 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 6023 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
6024
6025 if (soname != NULL)
6026 {
6027 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6028 soname, TRUE);
ef53be89 6029 if (soname_indx == (size_t) -1
5a580b3a
AM
6030 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6031 return FALSE;
6032 }
6033
6034 if (info->symbolic)
6035 {
6036 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6037 return FALSE;
6038 info->flags |= DF_SYMBOLIC;
6039 }
6040
6041 if (rpath != NULL)
6042 {
ef53be89 6043 size_t indx;
b1b00fcc 6044 bfd_vma tag;
5a580b3a
AM
6045
6046 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6047 TRUE);
ef53be89 6048 if (indx == (size_t) -1)
5a580b3a
AM
6049 return FALSE;
6050
b1b00fcc
MF
6051 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6052 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6053 return FALSE;
5a580b3a
AM
6054 }
6055
6056 if (filter_shlib != NULL)
6057 {
ef53be89 6058 size_t indx;
5a580b3a
AM
6059
6060 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6061 filter_shlib, TRUE);
ef53be89 6062 if (indx == (size_t) -1
5a580b3a
AM
6063 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6064 return FALSE;
6065 }
6066
6067 if (auxiliary_filters != NULL)
6068 {
6069 const char * const *p;
6070
6071 for (p = auxiliary_filters; *p != NULL; p++)
6072 {
ef53be89 6073 size_t indx;
5a580b3a
AM
6074
6075 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6076 *p, TRUE);
ef53be89 6077 if (indx == (size_t) -1
5a580b3a
AM
6078 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6079 return FALSE;
6080 }
6081 }
6082
7ee314fa
AM
6083 if (audit != NULL)
6084 {
ef53be89 6085 size_t indx;
7ee314fa
AM
6086
6087 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6088 TRUE);
ef53be89 6089 if (indx == (size_t) -1
7ee314fa
AM
6090 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6091 return FALSE;
6092 }
6093
6094 if (depaudit != NULL)
6095 {
ef53be89 6096 size_t indx;
7ee314fa
AM
6097
6098 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6099 TRUE);
ef53be89 6100 if (indx == (size_t) -1
7ee314fa
AM
6101 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6102 return FALSE;
6103 }
6104
5a580b3a 6105 eif.info = info;
5a580b3a
AM
6106 eif.failed = FALSE;
6107
6108 /* If we are supposed to export all symbols into the dynamic symbol
6109 table (this is not the normal case), then do so. */
55255dae 6110 if (info->export_dynamic
0e1862bb 6111 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
6112 {
6113 elf_link_hash_traverse (elf_hash_table (info),
6114 _bfd_elf_export_symbol,
6115 &eif);
6116 if (eif.failed)
6117 return FALSE;
6118 }
6119
6120 /* Make all global versions with definition. */
fd91d419 6121 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6122 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6123 if (!d->symver && d->literal)
5a580b3a
AM
6124 {
6125 const char *verstr, *name;
6126 size_t namelen, verlen, newlen;
93252b1c 6127 char *newname, *p, leading_char;
5a580b3a
AM
6128 struct elf_link_hash_entry *newh;
6129
93252b1c 6130 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6131 name = d->pattern;
93252b1c 6132 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6133 verstr = t->name;
6134 verlen = strlen (verstr);
6135 newlen = namelen + verlen + 3;
6136
a50b1753 6137 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6138 if (newname == NULL)
6139 return FALSE;
93252b1c
MF
6140 newname[0] = leading_char;
6141 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6142
6143 /* Check the hidden versioned definition. */
6144 p = newname + namelen;
6145 *p++ = ELF_VER_CHR;
6146 memcpy (p, verstr, verlen + 1);
6147 newh = elf_link_hash_lookup (elf_hash_table (info),
6148 newname, FALSE, FALSE,
6149 FALSE);
6150 if (newh == NULL
6151 || (newh->root.type != bfd_link_hash_defined
6152 && newh->root.type != bfd_link_hash_defweak))
6153 {
6154 /* Check the default versioned definition. */
6155 *p++ = ELF_VER_CHR;
6156 memcpy (p, verstr, verlen + 1);
6157 newh = elf_link_hash_lookup (elf_hash_table (info),
6158 newname, FALSE, FALSE,
6159 FALSE);
6160 }
6161 free (newname);
6162
6163 /* Mark this version if there is a definition and it is
6164 not defined in a shared object. */
6165 if (newh != NULL
f5385ebf 6166 && !newh->def_dynamic
5a580b3a
AM
6167 && (newh->root.type == bfd_link_hash_defined
6168 || newh->root.type == bfd_link_hash_defweak))
6169 d->symver = 1;
6170 }
6171
6172 /* Attach all the symbols to their version information. */
5a580b3a 6173 asvinfo.info = info;
5a580b3a
AM
6174 asvinfo.failed = FALSE;
6175
6176 elf_link_hash_traverse (elf_hash_table (info),
6177 _bfd_elf_link_assign_sym_version,
6178 &asvinfo);
6179 if (asvinfo.failed)
6180 return FALSE;
6181
6182 if (!info->allow_undefined_version)
6183 {
6184 /* Check if all global versions have a definition. */
6185 all_defined = TRUE;
fd91d419 6186 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6187 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6188 if (d->literal && !d->symver && !d->script)
5a580b3a 6189 {
4eca0228 6190 _bfd_error_handler
5a580b3a
AM
6191 (_("%s: undefined version: %s"),
6192 d->pattern, t->name);
6193 all_defined = FALSE;
6194 }
6195
6196 if (!all_defined)
6197 {
6198 bfd_set_error (bfd_error_bad_value);
6199 return FALSE;
6200 }
6201 }
6202
6203 /* Find all symbols which were defined in a dynamic object and make
6204 the backend pick a reasonable value for them. */
6205 elf_link_hash_traverse (elf_hash_table (info),
6206 _bfd_elf_adjust_dynamic_symbol,
6207 &eif);
6208 if (eif.failed)
6209 return FALSE;
6210
6211 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6212 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6213 now so that we know the final size of the .dynamic section. */
6214
6215 /* If there are initialization and/or finalization functions to
6216 call then add the corresponding DT_INIT/DT_FINI entries. */
6217 h = (info->init_function
6218 ? elf_link_hash_lookup (elf_hash_table (info),
6219 info->init_function, FALSE,
6220 FALSE, FALSE)
6221 : NULL);
6222 if (h != NULL
f5385ebf
AM
6223 && (h->ref_regular
6224 || h->def_regular))
5a580b3a
AM
6225 {
6226 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6227 return FALSE;
6228 }
6229 h = (info->fini_function
6230 ? elf_link_hash_lookup (elf_hash_table (info),
6231 info->fini_function, FALSE,
6232 FALSE, FALSE)
6233 : NULL);
6234 if (h != NULL
f5385ebf
AM
6235 && (h->ref_regular
6236 || h->def_regular))
5a580b3a
AM
6237 {
6238 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6239 return FALSE;
6240 }
6241
046183de
AM
6242 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6243 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6244 {
6245 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6246 if (! bfd_link_executable (info))
5a580b3a
AM
6247 {
6248 bfd *sub;
6249 asection *o;
6250
6251 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6252 sub = sub->link.next)
3fcd97f1
JJ
6253 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6254 for (o = sub->sections; o != NULL; o = o->next)
6255 if (elf_section_data (o)->this_hdr.sh_type
6256 == SHT_PREINIT_ARRAY)
6257 {
4eca0228 6258 _bfd_error_handler
3fcd97f1
JJ
6259 (_("%B: .preinit_array section is not allowed in DSO"),
6260 sub);
6261 break;
6262 }
5a580b3a
AM
6263
6264 bfd_set_error (bfd_error_nonrepresentable_section);
6265 return FALSE;
6266 }
6267
6268 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6269 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6270 return FALSE;
6271 }
046183de
AM
6272 s = bfd_get_section_by_name (output_bfd, ".init_array");
6273 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6274 {
6275 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6276 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6277 return FALSE;
6278 }
046183de
AM
6279 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6280 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6281 {
6282 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6283 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6284 return FALSE;
6285 }
6286
3d4d4302 6287 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6288 /* If .dynstr is excluded from the link, we don't want any of
6289 these tags. Strictly, we should be checking each section
6290 individually; This quick check covers for the case where
6291 someone does a /DISCARD/ : { *(*) }. */
6292 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6293 {
6294 bfd_size_type strsize;
6295
6296 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6297 if ((info->emit_hash
6298 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6299 || (info->emit_gnu_hash
6300 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6301 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6302 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6303 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6304 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6305 bed->s->sizeof_sym))
6306 return FALSE;
6307 }
6308 }
6309
de231f20
CM
6310 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6311 return FALSE;
6312
5a580b3a
AM
6313 /* The backend must work out the sizes of all the other dynamic
6314 sections. */
9a2a56cc
AM
6315 if (dynobj != NULL
6316 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6317 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6318 return FALSE;
6319
9a2a56cc 6320 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6321 {
554220db 6322 unsigned long section_sym_count;
fd91d419 6323 struct bfd_elf_version_tree *verdefs;
5a580b3a 6324 asection *s;
5a580b3a
AM
6325
6326 /* Set up the version definition section. */
3d4d4302 6327 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6328 BFD_ASSERT (s != NULL);
6329
6330 /* We may have created additional version definitions if we are
6331 just linking a regular application. */
fd91d419 6332 verdefs = info->version_info;
5a580b3a
AM
6333
6334 /* Skip anonymous version tag. */
6335 if (verdefs != NULL && verdefs->vernum == 0)
6336 verdefs = verdefs->next;
6337
3e3b46e5 6338 if (verdefs == NULL && !info->create_default_symver)
8423293d 6339 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6340 else
6341 {
6342 unsigned int cdefs;
6343 bfd_size_type size;
6344 struct bfd_elf_version_tree *t;
6345 bfd_byte *p;
6346 Elf_Internal_Verdef def;
6347 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6348 struct bfd_link_hash_entry *bh;
6349 struct elf_link_hash_entry *h;
6350 const char *name;
5a580b3a
AM
6351
6352 cdefs = 0;
6353 size = 0;
6354
6355 /* Make space for the base version. */
6356 size += sizeof (Elf_External_Verdef);
6357 size += sizeof (Elf_External_Verdaux);
6358 ++cdefs;
6359
3e3b46e5
PB
6360 /* Make space for the default version. */
6361 if (info->create_default_symver)
6362 {
6363 size += sizeof (Elf_External_Verdef);
6364 ++cdefs;
6365 }
6366
5a580b3a
AM
6367 for (t = verdefs; t != NULL; t = t->next)
6368 {
6369 struct bfd_elf_version_deps *n;
6370
a6cc6b3b
RO
6371 /* Don't emit base version twice. */
6372 if (t->vernum == 0)
6373 continue;
6374
5a580b3a
AM
6375 size += sizeof (Elf_External_Verdef);
6376 size += sizeof (Elf_External_Verdaux);
6377 ++cdefs;
6378
6379 for (n = t->deps; n != NULL; n = n->next)
6380 size += sizeof (Elf_External_Verdaux);
6381 }
6382
eea6121a 6383 s->size = size;
a50b1753 6384 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6385 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6386 return FALSE;
6387
6388 /* Fill in the version definition section. */
6389
6390 p = s->contents;
6391
6392 def.vd_version = VER_DEF_CURRENT;
6393 def.vd_flags = VER_FLG_BASE;
6394 def.vd_ndx = 1;
6395 def.vd_cnt = 1;
3e3b46e5
PB
6396 if (info->create_default_symver)
6397 {
6398 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6399 def.vd_next = sizeof (Elf_External_Verdef);
6400 }
6401 else
6402 {
6403 def.vd_aux = sizeof (Elf_External_Verdef);
6404 def.vd_next = (sizeof (Elf_External_Verdef)
6405 + sizeof (Elf_External_Verdaux));
6406 }
5a580b3a 6407
ef53be89 6408 if (soname_indx != (size_t) -1)
5a580b3a
AM
6409 {
6410 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6411 soname_indx);
6412 def.vd_hash = bfd_elf_hash (soname);
6413 defaux.vda_name = soname_indx;
3e3b46e5 6414 name = soname;
5a580b3a
AM
6415 }
6416 else
6417 {
ef53be89 6418 size_t indx;
5a580b3a 6419
06084812 6420 name = lbasename (output_bfd->filename);
5a580b3a
AM
6421 def.vd_hash = bfd_elf_hash (name);
6422 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6423 name, FALSE);
ef53be89 6424 if (indx == (size_t) -1)
5a580b3a
AM
6425 return FALSE;
6426 defaux.vda_name = indx;
6427 }
6428 defaux.vda_next = 0;
6429
6430 _bfd_elf_swap_verdef_out (output_bfd, &def,
6431 (Elf_External_Verdef *) p);
6432 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6433 if (info->create_default_symver)
6434 {
6435 /* Add a symbol representing this version. */
6436 bh = NULL;
6437 if (! (_bfd_generic_link_add_one_symbol
6438 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6439 0, NULL, FALSE,
6440 get_elf_backend_data (dynobj)->collect, &bh)))
6441 return FALSE;
6442 h = (struct elf_link_hash_entry *) bh;
6443 h->non_elf = 0;
6444 h->def_regular = 1;
6445 h->type = STT_OBJECT;
6446 h->verinfo.vertree = NULL;
6447
6448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6449 return FALSE;
6450
6451 /* Create a duplicate of the base version with the same
6452 aux block, but different flags. */
6453 def.vd_flags = 0;
6454 def.vd_ndx = 2;
6455 def.vd_aux = sizeof (Elf_External_Verdef);
6456 if (verdefs)
6457 def.vd_next = (sizeof (Elf_External_Verdef)
6458 + sizeof (Elf_External_Verdaux));
6459 else
6460 def.vd_next = 0;
6461 _bfd_elf_swap_verdef_out (output_bfd, &def,
6462 (Elf_External_Verdef *) p);
6463 p += sizeof (Elf_External_Verdef);
6464 }
5a580b3a
AM
6465 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6466 (Elf_External_Verdaux *) p);
6467 p += sizeof (Elf_External_Verdaux);
6468
6469 for (t = verdefs; t != NULL; t = t->next)
6470 {
6471 unsigned int cdeps;
6472 struct bfd_elf_version_deps *n;
5a580b3a 6473
a6cc6b3b
RO
6474 /* Don't emit the base version twice. */
6475 if (t->vernum == 0)
6476 continue;
6477
5a580b3a
AM
6478 cdeps = 0;
6479 for (n = t->deps; n != NULL; n = n->next)
6480 ++cdeps;
6481
6482 /* Add a symbol representing this version. */
6483 bh = NULL;
6484 if (! (_bfd_generic_link_add_one_symbol
6485 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6486 0, NULL, FALSE,
6487 get_elf_backend_data (dynobj)->collect, &bh)))
6488 return FALSE;
6489 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6490 h->non_elf = 0;
6491 h->def_regular = 1;
5a580b3a
AM
6492 h->type = STT_OBJECT;
6493 h->verinfo.vertree = t;
6494
c152c796 6495 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6496 return FALSE;
6497
6498 def.vd_version = VER_DEF_CURRENT;
6499 def.vd_flags = 0;
6500 if (t->globals.list == NULL
6501 && t->locals.list == NULL
6502 && ! t->used)
6503 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6504 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6505 def.vd_cnt = cdeps + 1;
6506 def.vd_hash = bfd_elf_hash (t->name);
6507 def.vd_aux = sizeof (Elf_External_Verdef);
6508 def.vd_next = 0;
a6cc6b3b
RO
6509
6510 /* If a basever node is next, it *must* be the last node in
6511 the chain, otherwise Verdef construction breaks. */
6512 if (t->next != NULL && t->next->vernum == 0)
6513 BFD_ASSERT (t->next->next == NULL);
6514
6515 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6516 def.vd_next = (sizeof (Elf_External_Verdef)
6517 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6518
6519 _bfd_elf_swap_verdef_out (output_bfd, &def,
6520 (Elf_External_Verdef *) p);
6521 p += sizeof (Elf_External_Verdef);
6522
6523 defaux.vda_name = h->dynstr_index;
6524 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6525 h->dynstr_index);
6526 defaux.vda_next = 0;
6527 if (t->deps != NULL)
6528 defaux.vda_next = sizeof (Elf_External_Verdaux);
6529 t->name_indx = defaux.vda_name;
6530
6531 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6532 (Elf_External_Verdaux *) p);
6533 p += sizeof (Elf_External_Verdaux);
6534
6535 for (n = t->deps; n != NULL; n = n->next)
6536 {
6537 if (n->version_needed == NULL)
6538 {
6539 /* This can happen if there was an error in the
6540 version script. */
6541 defaux.vda_name = 0;
6542 }
6543 else
6544 {
6545 defaux.vda_name = n->version_needed->name_indx;
6546 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6547 defaux.vda_name);
6548 }
6549 if (n->next == NULL)
6550 defaux.vda_next = 0;
6551 else
6552 defaux.vda_next = sizeof (Elf_External_Verdaux);
6553
6554 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6555 (Elf_External_Verdaux *) p);
6556 p += sizeof (Elf_External_Verdaux);
6557 }
6558 }
6559
6560 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6561 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6562 return FALSE;
6563
6564 elf_tdata (output_bfd)->cverdefs = cdefs;
6565 }
6566
6567 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6568 {
6569 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6570 return FALSE;
6571 }
6572 else if (info->flags & DF_BIND_NOW)
6573 {
6574 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6575 return FALSE;
6576 }
6577
6578 if (info->flags_1)
6579 {
0e1862bb 6580 if (bfd_link_executable (info))
5a580b3a
AM
6581 info->flags_1 &= ~ (DF_1_INITFIRST
6582 | DF_1_NODELETE
6583 | DF_1_NOOPEN);
6584 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6585 return FALSE;
6586 }
6587
6588 /* Work out the size of the version reference section. */
6589
3d4d4302 6590 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6591 BFD_ASSERT (s != NULL);
6592 {
6593 struct elf_find_verdep_info sinfo;
6594
5a580b3a
AM
6595 sinfo.info = info;
6596 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6597 if (sinfo.vers == 0)
6598 sinfo.vers = 1;
6599 sinfo.failed = FALSE;
6600
6601 elf_link_hash_traverse (elf_hash_table (info),
6602 _bfd_elf_link_find_version_dependencies,
6603 &sinfo);
14b1c01e
AM
6604 if (sinfo.failed)
6605 return FALSE;
5a580b3a
AM
6606
6607 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6608 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6609 else
6610 {
6611 Elf_Internal_Verneed *t;
6612 unsigned int size;
6613 unsigned int crefs;
6614 bfd_byte *p;
6615
a6cc6b3b 6616 /* Build the version dependency section. */
5a580b3a
AM
6617 size = 0;
6618 crefs = 0;
6619 for (t = elf_tdata (output_bfd)->verref;
6620 t != NULL;
6621 t = t->vn_nextref)
6622 {
6623 Elf_Internal_Vernaux *a;
6624
6625 size += sizeof (Elf_External_Verneed);
6626 ++crefs;
6627 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6628 size += sizeof (Elf_External_Vernaux);
6629 }
6630
eea6121a 6631 s->size = size;
a50b1753 6632 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6633 if (s->contents == NULL)
6634 return FALSE;
6635
6636 p = s->contents;
6637 for (t = elf_tdata (output_bfd)->verref;
6638 t != NULL;
6639 t = t->vn_nextref)
6640 {
6641 unsigned int caux;
6642 Elf_Internal_Vernaux *a;
ef53be89 6643 size_t indx;
5a580b3a
AM
6644
6645 caux = 0;
6646 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6647 ++caux;
6648
6649 t->vn_version = VER_NEED_CURRENT;
6650 t->vn_cnt = caux;
6651 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6652 elf_dt_name (t->vn_bfd) != NULL
6653 ? elf_dt_name (t->vn_bfd)
06084812 6654 : lbasename (t->vn_bfd->filename),
5a580b3a 6655 FALSE);
ef53be89 6656 if (indx == (size_t) -1)
5a580b3a
AM
6657 return FALSE;
6658 t->vn_file = indx;
6659 t->vn_aux = sizeof (Elf_External_Verneed);
6660 if (t->vn_nextref == NULL)
6661 t->vn_next = 0;
6662 else
6663 t->vn_next = (sizeof (Elf_External_Verneed)
6664 + caux * sizeof (Elf_External_Vernaux));
6665
6666 _bfd_elf_swap_verneed_out (output_bfd, t,
6667 (Elf_External_Verneed *) p);
6668 p += sizeof (Elf_External_Verneed);
6669
6670 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6671 {
6672 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6673 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6674 a->vna_nodename, FALSE);
ef53be89 6675 if (indx == (size_t) -1)
5a580b3a
AM
6676 return FALSE;
6677 a->vna_name = indx;
6678 if (a->vna_nextptr == NULL)
6679 a->vna_next = 0;
6680 else
6681 a->vna_next = sizeof (Elf_External_Vernaux);
6682
6683 _bfd_elf_swap_vernaux_out (output_bfd, a,
6684 (Elf_External_Vernaux *) p);
6685 p += sizeof (Elf_External_Vernaux);
6686 }
6687 }
6688
6689 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6690 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6691 return FALSE;
6692
6693 elf_tdata (output_bfd)->cverrefs = crefs;
6694 }
6695 }
6696
8423293d
AM
6697 if ((elf_tdata (output_bfd)->cverrefs == 0
6698 && elf_tdata (output_bfd)->cverdefs == 0)
6699 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6700 &section_sym_count) == 0)
6701 {
3d4d4302 6702 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6703 s->flags |= SEC_EXCLUDE;
6704 }
6705 }
6706 return TRUE;
6707}
6708
74541ad4
AM
6709/* Find the first non-excluded output section. We'll use its
6710 section symbol for some emitted relocs. */
6711void
6712_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6713{
6714 asection *s;
6715
6716 for (s = output_bfd->sections; s != NULL; s = s->next)
6717 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6718 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6719 {
6720 elf_hash_table (info)->text_index_section = s;
6721 break;
6722 }
6723}
6724
6725/* Find two non-excluded output sections, one for code, one for data.
6726 We'll use their section symbols for some emitted relocs. */
6727void
6728_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6729{
6730 asection *s;
6731
266b05cf
DJ
6732 /* Data first, since setting text_index_section changes
6733 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6734 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6735 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6736 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6737 {
266b05cf 6738 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6739 break;
6740 }
6741
6742 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6743 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6744 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6745 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6746 {
266b05cf 6747 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6748 break;
6749 }
6750
6751 if (elf_hash_table (info)->text_index_section == NULL)
6752 elf_hash_table (info)->text_index_section
6753 = elf_hash_table (info)->data_index_section;
6754}
6755
8423293d
AM
6756bfd_boolean
6757bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6758{
74541ad4
AM
6759 const struct elf_backend_data *bed;
6760
8423293d
AM
6761 if (!is_elf_hash_table (info->hash))
6762 return TRUE;
6763
74541ad4
AM
6764 bed = get_elf_backend_data (output_bfd);
6765 (*bed->elf_backend_init_index_section) (output_bfd, info);
6766
8423293d
AM
6767 if (elf_hash_table (info)->dynamic_sections_created)
6768 {
6769 bfd *dynobj;
8423293d
AM
6770 asection *s;
6771 bfd_size_type dynsymcount;
6772 unsigned long section_sym_count;
8423293d
AM
6773 unsigned int dtagcount;
6774
6775 dynobj = elf_hash_table (info)->dynobj;
6776
5a580b3a
AM
6777 /* Assign dynsym indicies. In a shared library we generate a
6778 section symbol for each output section, which come first.
6779 Next come all of the back-end allocated local dynamic syms,
6780 followed by the rest of the global symbols. */
6781
554220db
AM
6782 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6783 &section_sym_count);
5a580b3a
AM
6784
6785 /* Work out the size of the symbol version section. */
3d4d4302 6786 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6787 BFD_ASSERT (s != NULL);
d5486c43 6788 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6789 {
eea6121a 6790 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6791 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6792 if (s->contents == NULL)
6793 return FALSE;
6794
6795 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6796 return FALSE;
6797 }
6798
6799 /* Set the size of the .dynsym and .hash sections. We counted
6800 the number of dynamic symbols in elf_link_add_object_symbols.
6801 We will build the contents of .dynsym and .hash when we build
6802 the final symbol table, because until then we do not know the
6803 correct value to give the symbols. We built the .dynstr
6804 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6805 s = elf_hash_table (info)->dynsym;
5a580b3a 6806 BFD_ASSERT (s != NULL);
eea6121a 6807 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 6808
d5486c43
L
6809 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6810 if (s->contents == NULL)
6811 return FALSE;
5a580b3a 6812
d5486c43
L
6813 /* The first entry in .dynsym is a dummy symbol. Clear all the
6814 section syms, in case we don't output them all. */
6815 ++section_sym_count;
6816 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 6817
fdc90cb4
JJ
6818 elf_hash_table (info)->bucketcount = 0;
6819
5a580b3a
AM
6820 /* Compute the size of the hashing table. As a side effect this
6821 computes the hash values for all the names we export. */
fdc90cb4
JJ
6822 if (info->emit_hash)
6823 {
6824 unsigned long int *hashcodes;
14b1c01e 6825 struct hash_codes_info hashinf;
fdc90cb4
JJ
6826 bfd_size_type amt;
6827 unsigned long int nsyms;
6828 size_t bucketcount;
6829 size_t hash_entry_size;
6830
6831 /* Compute the hash values for all exported symbols. At the same
6832 time store the values in an array so that we could use them for
6833 optimizations. */
6834 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6835 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6836 if (hashcodes == NULL)
6837 return FALSE;
14b1c01e
AM
6838 hashinf.hashcodes = hashcodes;
6839 hashinf.error = FALSE;
5a580b3a 6840
fdc90cb4
JJ
6841 /* Put all hash values in HASHCODES. */
6842 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6843 elf_collect_hash_codes, &hashinf);
6844 if (hashinf.error)
4dd07732
AM
6845 {
6846 free (hashcodes);
6847 return FALSE;
6848 }
5a580b3a 6849
14b1c01e 6850 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6851 bucketcount
6852 = compute_bucket_count (info, hashcodes, nsyms, 0);
6853 free (hashcodes);
6854
6855 if (bucketcount == 0)
6856 return FALSE;
5a580b3a 6857
fdc90cb4
JJ
6858 elf_hash_table (info)->bucketcount = bucketcount;
6859
3d4d4302 6860 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6861 BFD_ASSERT (s != NULL);
6862 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6863 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6864 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6865 if (s->contents == NULL)
6866 return FALSE;
6867
6868 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6869 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6870 s->contents + hash_entry_size);
6871 }
6872
6873 if (info->emit_gnu_hash)
6874 {
6875 size_t i, cnt;
6876 unsigned char *contents;
6877 struct collect_gnu_hash_codes cinfo;
6878 bfd_size_type amt;
6879 size_t bucketcount;
6880
6881 memset (&cinfo, 0, sizeof (cinfo));
6882
6883 /* Compute the hash values for all exported symbols. At the same
6884 time store the values in an array so that we could use them for
6885 optimizations. */
6886 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6887 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6888 if (cinfo.hashcodes == NULL)
6889 return FALSE;
6890
6891 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6892 cinfo.min_dynindx = -1;
6893 cinfo.output_bfd = output_bfd;
6894 cinfo.bed = bed;
6895
6896 /* Put all hash values in HASHCODES. */
6897 elf_link_hash_traverse (elf_hash_table (info),
6898 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6899 if (cinfo.error)
4dd07732
AM
6900 {
6901 free (cinfo.hashcodes);
6902 return FALSE;
6903 }
fdc90cb4
JJ
6904
6905 bucketcount
6906 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6907
6908 if (bucketcount == 0)
6909 {
6910 free (cinfo.hashcodes);
6911 return FALSE;
6912 }
6913
3d4d4302 6914 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6915 BFD_ASSERT (s != NULL);
6916
6917 if (cinfo.nsyms == 0)
6918 {
6919 /* Empty .gnu.hash section is special. */
6920 BFD_ASSERT (cinfo.min_dynindx == -1);
6921 free (cinfo.hashcodes);
6922 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6923 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6924 if (contents == NULL)
6925 return FALSE;
6926 s->contents = contents;
6927 /* 1 empty bucket. */
6928 bfd_put_32 (output_bfd, 1, contents);
6929 /* SYMIDX above the special symbol 0. */
6930 bfd_put_32 (output_bfd, 1, contents + 4);
6931 /* Just one word for bitmask. */
6932 bfd_put_32 (output_bfd, 1, contents + 8);
6933 /* Only hash fn bloom filter. */
6934 bfd_put_32 (output_bfd, 0, contents + 12);
6935 /* No hashes are valid - empty bitmask. */
6936 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6937 /* No hashes in the only bucket. */
6938 bfd_put_32 (output_bfd, 0,
6939 contents + 16 + bed->s->arch_size / 8);
6940 }
6941 else
6942 {
9e6619e2 6943 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6944 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6945
9e6619e2
AM
6946 x = cinfo.nsyms;
6947 maskbitslog2 = 1;
6948 while ((x >>= 1) != 0)
6949 ++maskbitslog2;
fdc90cb4
JJ
6950 if (maskbitslog2 < 3)
6951 maskbitslog2 = 5;
6952 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6953 maskbitslog2 = maskbitslog2 + 3;
6954 else
6955 maskbitslog2 = maskbitslog2 + 2;
6956 if (bed->s->arch_size == 64)
6957 {
6958 if (maskbitslog2 == 5)
6959 maskbitslog2 = 6;
6960 cinfo.shift1 = 6;
6961 }
6962 else
6963 cinfo.shift1 = 5;
6964 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6965 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6966 cinfo.maskbits = 1 << maskbitslog2;
6967 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6968 amt = bucketcount * sizeof (unsigned long int) * 2;
6969 amt += maskwords * sizeof (bfd_vma);
a50b1753 6970 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6971 if (cinfo.bitmask == NULL)
6972 {
6973 free (cinfo.hashcodes);
6974 return FALSE;
6975 }
6976
a50b1753 6977 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6978 cinfo.indx = cinfo.counts + bucketcount;
6979 cinfo.symindx = dynsymcount - cinfo.nsyms;
6980 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6981
6982 /* Determine how often each hash bucket is used. */
6983 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6984 for (i = 0; i < cinfo.nsyms; ++i)
6985 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6986
6987 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6988 if (cinfo.counts[i] != 0)
6989 {
6990 cinfo.indx[i] = cnt;
6991 cnt += cinfo.counts[i];
6992 }
6993 BFD_ASSERT (cnt == dynsymcount);
6994 cinfo.bucketcount = bucketcount;
6995 cinfo.local_indx = cinfo.min_dynindx;
6996
6997 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6998 s->size += cinfo.maskbits / 8;
a50b1753 6999 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7000 if (contents == NULL)
7001 {
7002 free (cinfo.bitmask);
7003 free (cinfo.hashcodes);
7004 return FALSE;
7005 }
7006
7007 s->contents = contents;
7008 bfd_put_32 (output_bfd, bucketcount, contents);
7009 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7010 bfd_put_32 (output_bfd, maskwords, contents + 8);
7011 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7012 contents += 16 + cinfo.maskbits / 8;
7013
7014 for (i = 0; i < bucketcount; ++i)
7015 {
7016 if (cinfo.counts[i] == 0)
7017 bfd_put_32 (output_bfd, 0, contents);
7018 else
7019 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7020 contents += 4;
7021 }
7022
7023 cinfo.contents = contents;
7024
7025 /* Renumber dynamic symbols, populate .gnu.hash section. */
7026 elf_link_hash_traverse (elf_hash_table (info),
7027 elf_renumber_gnu_hash_syms, &cinfo);
7028
7029 contents = s->contents + 16;
7030 for (i = 0; i < maskwords; ++i)
7031 {
7032 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7033 contents);
7034 contents += bed->s->arch_size / 8;
7035 }
7036
7037 free (cinfo.bitmask);
7038 free (cinfo.hashcodes);
7039 }
7040 }
5a580b3a 7041
3d4d4302 7042 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7043 BFD_ASSERT (s != NULL);
7044
4ad4eba5 7045 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7046
eea6121a 7047 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7048
7049 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7050 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7051 return FALSE;
7052 }
7053
7054 return TRUE;
7055}
4d269e42 7056\f
4d269e42
AM
7057/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7058
7059static void
7060merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7061 asection *sec)
7062{
dbaa2011
AM
7063 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7064 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7065}
7066
7067/* Finish SHF_MERGE section merging. */
7068
7069bfd_boolean
630993ec 7070_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7071{
7072 bfd *ibfd;
7073 asection *sec;
7074
7075 if (!is_elf_hash_table (info->hash))
7076 return FALSE;
7077
c72f2fb2 7078 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7079 if ((ibfd->flags & DYNAMIC) == 0
7080 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7081 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7082 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7083 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7084 if ((sec->flags & SEC_MERGE) != 0
7085 && !bfd_is_abs_section (sec->output_section))
7086 {
7087 struct bfd_elf_section_data *secdata;
7088
7089 secdata = elf_section_data (sec);
630993ec 7090 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7091 &elf_hash_table (info)->merge_info,
7092 sec, &secdata->sec_info))
7093 return FALSE;
7094 else if (secdata->sec_info)
dbaa2011 7095 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7096 }
7097
7098 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7099 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7100 merge_sections_remove_hook);
7101 return TRUE;
7102}
7103
7104/* Create an entry in an ELF linker hash table. */
7105
7106struct bfd_hash_entry *
7107_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7108 struct bfd_hash_table *table,
7109 const char *string)
7110{
7111 /* Allocate the structure if it has not already been allocated by a
7112 subclass. */
7113 if (entry == NULL)
7114 {
a50b1753 7115 entry = (struct bfd_hash_entry *)
ca4be51c 7116 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7117 if (entry == NULL)
7118 return entry;
7119 }
7120
7121 /* Call the allocation method of the superclass. */
7122 entry = _bfd_link_hash_newfunc (entry, table, string);
7123 if (entry != NULL)
7124 {
7125 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7126 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7127
7128 /* Set local fields. */
7129 ret->indx = -1;
7130 ret->dynindx = -1;
7131 ret->got = htab->init_got_refcount;
7132 ret->plt = htab->init_plt_refcount;
7133 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7134 - offsetof (struct elf_link_hash_entry, size)));
7135 /* Assume that we have been called by a non-ELF symbol reader.
7136 This flag is then reset by the code which reads an ELF input
7137 file. This ensures that a symbol created by a non-ELF symbol
7138 reader will have the flag set correctly. */
7139 ret->non_elf = 1;
7140 }
7141
7142 return entry;
7143}
7144
7145/* Copy data from an indirect symbol to its direct symbol, hiding the
7146 old indirect symbol. Also used for copying flags to a weakdef. */
7147
7148void
7149_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7150 struct elf_link_hash_entry *dir,
7151 struct elf_link_hash_entry *ind)
7152{
7153 struct elf_link_hash_table *htab;
7154
7155 /* Copy down any references that we may have already seen to the
e81830c5 7156 symbol which just became indirect. */
4d269e42 7157
422f1182 7158 if (dir->versioned != versioned_hidden)
e81830c5
AM
7159 dir->ref_dynamic |= ind->ref_dynamic;
7160 dir->ref_regular |= ind->ref_regular;
7161 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7162 dir->non_got_ref |= ind->non_got_ref;
7163 dir->needs_plt |= ind->needs_plt;
7164 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7165
7166 if (ind->root.type != bfd_link_hash_indirect)
7167 return;
7168
7169 /* Copy over the global and procedure linkage table refcount entries.
7170 These may have been already set up by a check_relocs routine. */
7171 htab = elf_hash_table (info);
7172 if (ind->got.refcount > htab->init_got_refcount.refcount)
7173 {
7174 if (dir->got.refcount < 0)
7175 dir->got.refcount = 0;
7176 dir->got.refcount += ind->got.refcount;
7177 ind->got.refcount = htab->init_got_refcount.refcount;
7178 }
7179
7180 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7181 {
7182 if (dir->plt.refcount < 0)
7183 dir->plt.refcount = 0;
7184 dir->plt.refcount += ind->plt.refcount;
7185 ind->plt.refcount = htab->init_plt_refcount.refcount;
7186 }
7187
7188 if (ind->dynindx != -1)
7189 {
7190 if (dir->dynindx != -1)
7191 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7192 dir->dynindx = ind->dynindx;
7193 dir->dynstr_index = ind->dynstr_index;
7194 ind->dynindx = -1;
7195 ind->dynstr_index = 0;
7196 }
7197}
7198
7199void
7200_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7201 struct elf_link_hash_entry *h,
7202 bfd_boolean force_local)
7203{
3aa14d16
L
7204 /* STT_GNU_IFUNC symbol must go through PLT. */
7205 if (h->type != STT_GNU_IFUNC)
7206 {
7207 h->plt = elf_hash_table (info)->init_plt_offset;
7208 h->needs_plt = 0;
7209 }
4d269e42
AM
7210 if (force_local)
7211 {
7212 h->forced_local = 1;
7213 if (h->dynindx != -1)
7214 {
7215 h->dynindx = -1;
7216 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7217 h->dynstr_index);
7218 }
7219 }
7220}
7221
7bf52ea2
AM
7222/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7223 caller. */
4d269e42
AM
7224
7225bfd_boolean
7226_bfd_elf_link_hash_table_init
7227 (struct elf_link_hash_table *table,
7228 bfd *abfd,
7229 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7230 struct bfd_hash_table *,
7231 const char *),
4dfe6ac6
NC
7232 unsigned int entsize,
7233 enum elf_target_id target_id)
4d269e42
AM
7234{
7235 bfd_boolean ret;
7236 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7237
4d269e42
AM
7238 table->init_got_refcount.refcount = can_refcount - 1;
7239 table->init_plt_refcount.refcount = can_refcount - 1;
7240 table->init_got_offset.offset = -(bfd_vma) 1;
7241 table->init_plt_offset.offset = -(bfd_vma) 1;
7242 /* The first dynamic symbol is a dummy. */
7243 table->dynsymcount = 1;
7244
7245 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7246
4d269e42 7247 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7248 table->hash_table_id = target_id;
4d269e42
AM
7249
7250 return ret;
7251}
7252
7253/* Create an ELF linker hash table. */
7254
7255struct bfd_link_hash_table *
7256_bfd_elf_link_hash_table_create (bfd *abfd)
7257{
7258 struct elf_link_hash_table *ret;
7259 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7260
7bf52ea2 7261 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7262 if (ret == NULL)
7263 return NULL;
7264
7265 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7266 sizeof (struct elf_link_hash_entry),
7267 GENERIC_ELF_DATA))
4d269e42
AM
7268 {
7269 free (ret);
7270 return NULL;
7271 }
d495ab0d 7272 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7273
7274 return &ret->root;
7275}
7276
9f7c3e5e
AM
7277/* Destroy an ELF linker hash table. */
7278
7279void
d495ab0d 7280_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7281{
d495ab0d
AM
7282 struct elf_link_hash_table *htab;
7283
7284 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7285 if (htab->dynstr != NULL)
7286 _bfd_elf_strtab_free (htab->dynstr);
7287 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7288 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7289}
7290
4d269e42
AM
7291/* This is a hook for the ELF emulation code in the generic linker to
7292 tell the backend linker what file name to use for the DT_NEEDED
7293 entry for a dynamic object. */
7294
7295void
7296bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7297{
7298 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7299 && bfd_get_format (abfd) == bfd_object)
7300 elf_dt_name (abfd) = name;
7301}
7302
7303int
7304bfd_elf_get_dyn_lib_class (bfd *abfd)
7305{
7306 int lib_class;
7307 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7308 && bfd_get_format (abfd) == bfd_object)
7309 lib_class = elf_dyn_lib_class (abfd);
7310 else
7311 lib_class = 0;
7312 return lib_class;
7313}
7314
7315void
7316bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7317{
7318 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7319 && bfd_get_format (abfd) == bfd_object)
7320 elf_dyn_lib_class (abfd) = lib_class;
7321}
7322
7323/* Get the list of DT_NEEDED entries for a link. This is a hook for
7324 the linker ELF emulation code. */
7325
7326struct bfd_link_needed_list *
7327bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7328 struct bfd_link_info *info)
7329{
7330 if (! is_elf_hash_table (info->hash))
7331 return NULL;
7332 return elf_hash_table (info)->needed;
7333}
7334
7335/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7336 hook for the linker ELF emulation code. */
7337
7338struct bfd_link_needed_list *
7339bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7340 struct bfd_link_info *info)
7341{
7342 if (! is_elf_hash_table (info->hash))
7343 return NULL;
7344 return elf_hash_table (info)->runpath;
7345}
7346
7347/* Get the name actually used for a dynamic object for a link. This
7348 is the SONAME entry if there is one. Otherwise, it is the string
7349 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7350
7351const char *
7352bfd_elf_get_dt_soname (bfd *abfd)
7353{
7354 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7355 && bfd_get_format (abfd) == bfd_object)
7356 return elf_dt_name (abfd);
7357 return NULL;
7358}
7359
7360/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7361 the ELF linker emulation code. */
7362
7363bfd_boolean
7364bfd_elf_get_bfd_needed_list (bfd *abfd,
7365 struct bfd_link_needed_list **pneeded)
7366{
7367 asection *s;
7368 bfd_byte *dynbuf = NULL;
cb33740c 7369 unsigned int elfsec;
4d269e42
AM
7370 unsigned long shlink;
7371 bfd_byte *extdyn, *extdynend;
7372 size_t extdynsize;
7373 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7374
7375 *pneeded = NULL;
7376
7377 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7378 || bfd_get_format (abfd) != bfd_object)
7379 return TRUE;
7380
7381 s = bfd_get_section_by_name (abfd, ".dynamic");
7382 if (s == NULL || s->size == 0)
7383 return TRUE;
7384
7385 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7386 goto error_return;
7387
7388 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7389 if (elfsec == SHN_BAD)
4d269e42
AM
7390 goto error_return;
7391
7392 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7393
4d269e42
AM
7394 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7395 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7396
7397 extdyn = dynbuf;
7398 extdynend = extdyn + s->size;
7399 for (; extdyn < extdynend; extdyn += extdynsize)
7400 {
7401 Elf_Internal_Dyn dyn;
7402
7403 (*swap_dyn_in) (abfd, extdyn, &dyn);
7404
7405 if (dyn.d_tag == DT_NULL)
7406 break;
7407
7408 if (dyn.d_tag == DT_NEEDED)
7409 {
7410 const char *string;
7411 struct bfd_link_needed_list *l;
7412 unsigned int tagv = dyn.d_un.d_val;
7413 bfd_size_type amt;
7414
7415 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7416 if (string == NULL)
7417 goto error_return;
7418
7419 amt = sizeof *l;
a50b1753 7420 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7421 if (l == NULL)
7422 goto error_return;
7423
7424 l->by = abfd;
7425 l->name = string;
7426 l->next = *pneeded;
7427 *pneeded = l;
7428 }
7429 }
7430
7431 free (dynbuf);
7432
7433 return TRUE;
7434
7435 error_return:
7436 if (dynbuf != NULL)
7437 free (dynbuf);
7438 return FALSE;
7439}
7440
7441struct elf_symbuf_symbol
7442{
7443 unsigned long st_name; /* Symbol name, index in string tbl */
7444 unsigned char st_info; /* Type and binding attributes */
7445 unsigned char st_other; /* Visibilty, and target specific */
7446};
7447
7448struct elf_symbuf_head
7449{
7450 struct elf_symbuf_symbol *ssym;
ef53be89 7451 size_t count;
4d269e42
AM
7452 unsigned int st_shndx;
7453};
7454
7455struct elf_symbol
7456{
7457 union
7458 {
7459 Elf_Internal_Sym *isym;
7460 struct elf_symbuf_symbol *ssym;
7461 } u;
7462 const char *name;
7463};
7464
7465/* Sort references to symbols by ascending section number. */
7466
7467static int
7468elf_sort_elf_symbol (const void *arg1, const void *arg2)
7469{
7470 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7471 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7472
7473 return s1->st_shndx - s2->st_shndx;
7474}
7475
7476static int
7477elf_sym_name_compare (const void *arg1, const void *arg2)
7478{
7479 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7480 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7481 return strcmp (s1->name, s2->name);
7482}
7483
7484static struct elf_symbuf_head *
ef53be89 7485elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 7486{
14b1c01e 7487 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7488 struct elf_symbuf_symbol *ssym;
7489 struct elf_symbuf_head *ssymbuf, *ssymhead;
ef53be89 7490 size_t i, shndx_count, total_size;
4d269e42 7491
a50b1753 7492 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7493 if (indbuf == NULL)
7494 return NULL;
7495
7496 for (ind = indbuf, i = 0; i < symcount; i++)
7497 if (isymbuf[i].st_shndx != SHN_UNDEF)
7498 *ind++ = &isymbuf[i];
7499 indbufend = ind;
7500
7501 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7502 elf_sort_elf_symbol);
7503
7504 shndx_count = 0;
7505 if (indbufend > indbuf)
7506 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7507 if (ind[0]->st_shndx != ind[1]->st_shndx)
7508 shndx_count++;
7509
3ae181ee
L
7510 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7511 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7512 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7513 if (ssymbuf == NULL)
7514 {
7515 free (indbuf);
7516 return NULL;
7517 }
7518
3ae181ee 7519 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7520 ssymbuf->ssym = NULL;
7521 ssymbuf->count = shndx_count;
7522 ssymbuf->st_shndx = 0;
7523 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7524 {
7525 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7526 {
7527 ssymhead++;
7528 ssymhead->ssym = ssym;
7529 ssymhead->count = 0;
7530 ssymhead->st_shndx = (*ind)->st_shndx;
7531 }
7532 ssym->st_name = (*ind)->st_name;
7533 ssym->st_info = (*ind)->st_info;
7534 ssym->st_other = (*ind)->st_other;
7535 ssymhead->count++;
7536 }
ef53be89 7537 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
7538 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7539 == total_size));
4d269e42
AM
7540
7541 free (indbuf);
7542 return ssymbuf;
7543}
7544
7545/* Check if 2 sections define the same set of local and global
7546 symbols. */
7547
8f317e31 7548static bfd_boolean
4d269e42
AM
7549bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7550 struct bfd_link_info *info)
7551{
7552 bfd *bfd1, *bfd2;
7553 const struct elf_backend_data *bed1, *bed2;
7554 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 7555 size_t symcount1, symcount2;
4d269e42
AM
7556 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7557 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7558 Elf_Internal_Sym *isym, *isymend;
7559 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 7560 size_t count1, count2, i;
cb33740c 7561 unsigned int shndx1, shndx2;
4d269e42
AM
7562 bfd_boolean result;
7563
7564 bfd1 = sec1->owner;
7565 bfd2 = sec2->owner;
7566
4d269e42
AM
7567 /* Both sections have to be in ELF. */
7568 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7569 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7570 return FALSE;
7571
7572 if (elf_section_type (sec1) != elf_section_type (sec2))
7573 return FALSE;
7574
4d269e42
AM
7575 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7576 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7577 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7578 return FALSE;
7579
7580 bed1 = get_elf_backend_data (bfd1);
7581 bed2 = get_elf_backend_data (bfd2);
7582 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7583 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7584 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7585 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7586
7587 if (symcount1 == 0 || symcount2 == 0)
7588 return FALSE;
7589
7590 result = FALSE;
7591 isymbuf1 = NULL;
7592 isymbuf2 = NULL;
a50b1753
NC
7593 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7594 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7595
7596 if (ssymbuf1 == NULL)
7597 {
7598 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7599 NULL, NULL, NULL);
7600 if (isymbuf1 == NULL)
7601 goto done;
7602
7603 if (!info->reduce_memory_overheads)
7604 elf_tdata (bfd1)->symbuf = ssymbuf1
7605 = elf_create_symbuf (symcount1, isymbuf1);
7606 }
7607
7608 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7609 {
7610 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7611 NULL, NULL, NULL);
7612 if (isymbuf2 == NULL)
7613 goto done;
7614
7615 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7616 elf_tdata (bfd2)->symbuf = ssymbuf2
7617 = elf_create_symbuf (symcount2, isymbuf2);
7618 }
7619
7620 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7621 {
7622 /* Optimized faster version. */
ef53be89 7623 size_t lo, hi, mid;
4d269e42
AM
7624 struct elf_symbol *symp;
7625 struct elf_symbuf_symbol *ssym, *ssymend;
7626
7627 lo = 0;
7628 hi = ssymbuf1->count;
7629 ssymbuf1++;
7630 count1 = 0;
7631 while (lo < hi)
7632 {
7633 mid = (lo + hi) / 2;
cb33740c 7634 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7635 hi = mid;
cb33740c 7636 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7637 lo = mid + 1;
7638 else
7639 {
7640 count1 = ssymbuf1[mid].count;
7641 ssymbuf1 += mid;
7642 break;
7643 }
7644 }
7645
7646 lo = 0;
7647 hi = ssymbuf2->count;
7648 ssymbuf2++;
7649 count2 = 0;
7650 while (lo < hi)
7651 {
7652 mid = (lo + hi) / 2;
cb33740c 7653 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7654 hi = mid;
cb33740c 7655 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7656 lo = mid + 1;
7657 else
7658 {
7659 count2 = ssymbuf2[mid].count;
7660 ssymbuf2 += mid;
7661 break;
7662 }
7663 }
7664
7665 if (count1 == 0 || count2 == 0 || count1 != count2)
7666 goto done;
7667
ca4be51c
AM
7668 symtable1
7669 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7670 symtable2
7671 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7672 if (symtable1 == NULL || symtable2 == NULL)
7673 goto done;
7674
7675 symp = symtable1;
7676 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7677 ssym < ssymend; ssym++, symp++)
7678 {
7679 symp->u.ssym = ssym;
7680 symp->name = bfd_elf_string_from_elf_section (bfd1,
7681 hdr1->sh_link,
7682 ssym->st_name);
7683 }
7684
7685 symp = symtable2;
7686 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7687 ssym < ssymend; ssym++, symp++)
7688 {
7689 symp->u.ssym = ssym;
7690 symp->name = bfd_elf_string_from_elf_section (bfd2,
7691 hdr2->sh_link,
7692 ssym->st_name);
7693 }
7694
7695 /* Sort symbol by name. */
7696 qsort (symtable1, count1, sizeof (struct elf_symbol),
7697 elf_sym_name_compare);
7698 qsort (symtable2, count1, sizeof (struct elf_symbol),
7699 elf_sym_name_compare);
7700
7701 for (i = 0; i < count1; i++)
7702 /* Two symbols must have the same binding, type and name. */
7703 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7704 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7705 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7706 goto done;
7707
7708 result = TRUE;
7709 goto done;
7710 }
7711
a50b1753
NC
7712 symtable1 = (struct elf_symbol *)
7713 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7714 symtable2 = (struct elf_symbol *)
7715 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7716 if (symtable1 == NULL || symtable2 == NULL)
7717 goto done;
7718
7719 /* Count definitions in the section. */
7720 count1 = 0;
7721 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7722 if (isym->st_shndx == shndx1)
4d269e42
AM
7723 symtable1[count1++].u.isym = isym;
7724
7725 count2 = 0;
7726 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7727 if (isym->st_shndx == shndx2)
4d269e42
AM
7728 symtable2[count2++].u.isym = isym;
7729
7730 if (count1 == 0 || count2 == 0 || count1 != count2)
7731 goto done;
7732
7733 for (i = 0; i < count1; i++)
7734 symtable1[i].name
7735 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7736 symtable1[i].u.isym->st_name);
7737
7738 for (i = 0; i < count2; i++)
7739 symtable2[i].name
7740 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7741 symtable2[i].u.isym->st_name);
7742
7743 /* Sort symbol by name. */
7744 qsort (symtable1, count1, sizeof (struct elf_symbol),
7745 elf_sym_name_compare);
7746 qsort (symtable2, count1, sizeof (struct elf_symbol),
7747 elf_sym_name_compare);
7748
7749 for (i = 0; i < count1; i++)
7750 /* Two symbols must have the same binding, type and name. */
7751 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7752 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7753 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7754 goto done;
7755
7756 result = TRUE;
7757
7758done:
7759 if (symtable1)
7760 free (symtable1);
7761 if (symtable2)
7762 free (symtable2);
7763 if (isymbuf1)
7764 free (isymbuf1);
7765 if (isymbuf2)
7766 free (isymbuf2);
7767
7768 return result;
7769}
7770
7771/* Return TRUE if 2 section types are compatible. */
7772
7773bfd_boolean
7774_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7775 bfd *bbfd, const asection *bsec)
7776{
7777 if (asec == NULL
7778 || bsec == NULL
7779 || abfd->xvec->flavour != bfd_target_elf_flavour
7780 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7781 return TRUE;
7782
7783 return elf_section_type (asec) == elf_section_type (bsec);
7784}
7785\f
c152c796
AM
7786/* Final phase of ELF linker. */
7787
7788/* A structure we use to avoid passing large numbers of arguments. */
7789
7790struct elf_final_link_info
7791{
7792 /* General link information. */
7793 struct bfd_link_info *info;
7794 /* Output BFD. */
7795 bfd *output_bfd;
7796 /* Symbol string table. */
ef10c3ac 7797 struct elf_strtab_hash *symstrtab;
c152c796
AM
7798 /* .hash section. */
7799 asection *hash_sec;
7800 /* symbol version section (.gnu.version). */
7801 asection *symver_sec;
7802 /* Buffer large enough to hold contents of any section. */
7803 bfd_byte *contents;
7804 /* Buffer large enough to hold external relocs of any section. */
7805 void *external_relocs;
7806 /* Buffer large enough to hold internal relocs of any section. */
7807 Elf_Internal_Rela *internal_relocs;
7808 /* Buffer large enough to hold external local symbols of any input
7809 BFD. */
7810 bfd_byte *external_syms;
7811 /* And a buffer for symbol section indices. */
7812 Elf_External_Sym_Shndx *locsym_shndx;
7813 /* Buffer large enough to hold internal local symbols of any input
7814 BFD. */
7815 Elf_Internal_Sym *internal_syms;
7816 /* Array large enough to hold a symbol index for each local symbol
7817 of any input BFD. */
7818 long *indices;
7819 /* Array large enough to hold a section pointer for each local
7820 symbol of any input BFD. */
7821 asection **sections;
ef10c3ac 7822 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7823 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7824 /* Number of STT_FILE syms seen. */
7825 size_t filesym_count;
c152c796
AM
7826};
7827
7828/* This struct is used to pass information to elf_link_output_extsym. */
7829
7830struct elf_outext_info
7831{
7832 bfd_boolean failed;
7833 bfd_boolean localsyms;
34a79995 7834 bfd_boolean file_sym_done;
8b127cbc 7835 struct elf_final_link_info *flinfo;
c152c796
AM
7836};
7837
d9352518
DB
7838
7839/* Support for evaluating a complex relocation.
7840
7841 Complex relocations are generalized, self-describing relocations. The
7842 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7843 relocations themselves.
d9352518
DB
7844
7845 The relocations are use a reserved elf-wide relocation type code (R_RELC
7846 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7847 information (start bit, end bit, word width, etc) into the addend. This
7848 information is extracted from CGEN-generated operand tables within gas.
7849
7850 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7851 internal) representing prefix-notation expressions, including but not
7852 limited to those sorts of expressions normally encoded as addends in the
7853 addend field. The symbol mangling format is:
7854
7855 <node> := <literal>
7856 | <unary-operator> ':' <node>
7857 | <binary-operator> ':' <node> ':' <node>
7858 ;
7859
7860 <literal> := 's' <digits=N> ':' <N character symbol name>
7861 | 'S' <digits=N> ':' <N character section name>
7862 | '#' <hexdigits>
7863 ;
7864
7865 <binary-operator> := as in C
7866 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7867
7868static void
a0c8462f
AM
7869set_symbol_value (bfd *bfd_with_globals,
7870 Elf_Internal_Sym *isymbuf,
7871 size_t locsymcount,
7872 size_t symidx,
7873 bfd_vma val)
d9352518 7874{
8977835c
AM
7875 struct elf_link_hash_entry **sym_hashes;
7876 struct elf_link_hash_entry *h;
7877 size_t extsymoff = locsymcount;
d9352518 7878
8977835c 7879 if (symidx < locsymcount)
d9352518 7880 {
8977835c
AM
7881 Elf_Internal_Sym *sym;
7882
7883 sym = isymbuf + symidx;
7884 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7885 {
7886 /* It is a local symbol: move it to the
7887 "absolute" section and give it a value. */
7888 sym->st_shndx = SHN_ABS;
7889 sym->st_value = val;
7890 return;
7891 }
7892 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7893 extsymoff = 0;
d9352518 7894 }
8977835c
AM
7895
7896 /* It is a global symbol: set its link type
7897 to "defined" and give it a value. */
7898
7899 sym_hashes = elf_sym_hashes (bfd_with_globals);
7900 h = sym_hashes [symidx - extsymoff];
7901 while (h->root.type == bfd_link_hash_indirect
7902 || h->root.type == bfd_link_hash_warning)
7903 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7904 h->root.type = bfd_link_hash_defined;
7905 h->root.u.def.value = val;
7906 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7907}
7908
a0c8462f
AM
7909static bfd_boolean
7910resolve_symbol (const char *name,
7911 bfd *input_bfd,
8b127cbc 7912 struct elf_final_link_info *flinfo,
a0c8462f
AM
7913 bfd_vma *result,
7914 Elf_Internal_Sym *isymbuf,
7915 size_t locsymcount)
d9352518 7916{
a0c8462f
AM
7917 Elf_Internal_Sym *sym;
7918 struct bfd_link_hash_entry *global_entry;
7919 const char *candidate = NULL;
7920 Elf_Internal_Shdr *symtab_hdr;
7921 size_t i;
7922
d9352518
DB
7923 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7924
7925 for (i = 0; i < locsymcount; ++ i)
7926 {
8977835c 7927 sym = isymbuf + i;
d9352518
DB
7928
7929 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7930 continue;
7931
7932 candidate = bfd_elf_string_from_elf_section (input_bfd,
7933 symtab_hdr->sh_link,
7934 sym->st_name);
7935#ifdef DEBUG
0f02bbd9
AM
7936 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7937 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7938#endif
7939 if (candidate && strcmp (candidate, name) == 0)
7940 {
8b127cbc 7941 asection *sec = flinfo->sections [i];
d9352518 7942
0f02bbd9
AM
7943 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7944 *result += sec->output_offset + sec->output_section->vma;
d9352518 7945#ifdef DEBUG
0f02bbd9
AM
7946 printf ("Found symbol with value %8.8lx\n",
7947 (unsigned long) *result);
d9352518
DB
7948#endif
7949 return TRUE;
7950 }
7951 }
7952
7953 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7954 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7955 FALSE, FALSE, TRUE);
d9352518
DB
7956 if (!global_entry)
7957 return FALSE;
a0c8462f 7958
d9352518
DB
7959 if (global_entry->type == bfd_link_hash_defined
7960 || global_entry->type == bfd_link_hash_defweak)
7961 {
a0c8462f
AM
7962 *result = (global_entry->u.def.value
7963 + global_entry->u.def.section->output_section->vma
7964 + global_entry->u.def.section->output_offset);
d9352518 7965#ifdef DEBUG
0f02bbd9
AM
7966 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7967 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7968#endif
7969 return TRUE;
a0c8462f 7970 }
d9352518 7971
d9352518
DB
7972 return FALSE;
7973}
7974
37b01f6a
DG
7975/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7976 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7977 names like "foo.end" which is the end address of section "foo". */
7978
d9352518 7979static bfd_boolean
a0c8462f
AM
7980resolve_section (const char *name,
7981 asection *sections,
37b01f6a
DG
7982 bfd_vma *result,
7983 bfd * abfd)
d9352518 7984{
a0c8462f
AM
7985 asection *curr;
7986 unsigned int len;
d9352518 7987
a0c8462f 7988 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7989 if (strcmp (curr->name, name) == 0)
7990 {
7991 *result = curr->vma;
7992 return TRUE;
7993 }
7994
7995 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7996 /* FIXME: This could be coded more efficiently... */
a0c8462f 7997 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7998 {
7999 len = strlen (curr->name);
a0c8462f 8000 if (len > strlen (name))
d9352518
DB
8001 continue;
8002
8003 if (strncmp (curr->name, name, len) == 0)
8004 {
8005 if (strncmp (".end", name + len, 4) == 0)
8006 {
37b01f6a 8007 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
8008 return TRUE;
8009 }
8010
8011 /* Insert more pseudo-section names here, if you like. */
8012 }
8013 }
a0c8462f 8014
d9352518
DB
8015 return FALSE;
8016}
8017
8018static void
a0c8462f 8019undefined_reference (const char *reftype, const char *name)
d9352518 8020{
695344c0 8021 /* xgettext:c-format */
a0c8462f
AM
8022 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8023 reftype, name);
d9352518
DB
8024}
8025
8026static bfd_boolean
a0c8462f
AM
8027eval_symbol (bfd_vma *result,
8028 const char **symp,
8029 bfd *input_bfd,
8b127cbc 8030 struct elf_final_link_info *flinfo,
a0c8462f
AM
8031 bfd_vma dot,
8032 Elf_Internal_Sym *isymbuf,
8033 size_t locsymcount,
8034 int signed_p)
d9352518 8035{
4b93929b
NC
8036 size_t len;
8037 size_t symlen;
a0c8462f
AM
8038 bfd_vma a;
8039 bfd_vma b;
4b93929b 8040 char symbuf[4096];
0f02bbd9 8041 const char *sym = *symp;
a0c8462f
AM
8042 const char *symend;
8043 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8044
8045 len = strlen (sym);
8046 symend = sym + len;
8047
4b93929b 8048 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8049 {
8050 bfd_set_error (bfd_error_invalid_operation);
8051 return FALSE;
8052 }
a0c8462f 8053
d9352518
DB
8054 switch (* sym)
8055 {
8056 case '.':
0f02bbd9
AM
8057 *result = dot;
8058 *symp = sym + 1;
d9352518
DB
8059 return TRUE;
8060
8061 case '#':
0f02bbd9
AM
8062 ++sym;
8063 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8064 return TRUE;
8065
8066 case 'S':
8067 symbol_is_section = TRUE;
1a0670f3 8068 /* Fall through. */
a0c8462f 8069 case 's':
0f02bbd9
AM
8070 ++sym;
8071 symlen = strtol (sym, (char **) symp, 10);
8072 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8073
4b93929b 8074 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8075 {
8076 bfd_set_error (bfd_error_invalid_operation);
8077 return FALSE;
8078 }
8079
8080 memcpy (symbuf, sym, symlen);
a0c8462f 8081 symbuf[symlen] = '\0';
0f02bbd9 8082 *symp = sym + symlen;
a0c8462f
AM
8083
8084 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8085 the symbol as a section, or vice-versa. so we're pretty liberal in our
8086 interpretation here; section means "try section first", not "must be a
8087 section", and likewise with symbol. */
8088
a0c8462f 8089 if (symbol_is_section)
d9352518 8090 {
37b01f6a 8091 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8092 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8093 isymbuf, locsymcount))
d9352518
DB
8094 {
8095 undefined_reference ("section", symbuf);
8096 return FALSE;
8097 }
a0c8462f
AM
8098 }
8099 else
d9352518 8100 {
8b127cbc 8101 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8102 isymbuf, locsymcount)
8b127cbc 8103 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8104 result, input_bfd))
d9352518
DB
8105 {
8106 undefined_reference ("symbol", symbuf);
8107 return FALSE;
8108 }
8109 }
8110
8111 return TRUE;
a0c8462f 8112
d9352518
DB
8113 /* All that remains are operators. */
8114
8115#define UNARY_OP(op) \
8116 if (strncmp (sym, #op, strlen (#op)) == 0) \
8117 { \
8118 sym += strlen (#op); \
a0c8462f
AM
8119 if (*sym == ':') \
8120 ++sym; \
0f02bbd9 8121 *symp = sym; \
8b127cbc 8122 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8123 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8124 return FALSE; \
8125 if (signed_p) \
0f02bbd9 8126 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8127 else \
8128 *result = op a; \
d9352518
DB
8129 return TRUE; \
8130 }
8131
8132#define BINARY_OP(op) \
8133 if (strncmp (sym, #op, strlen (#op)) == 0) \
8134 { \
8135 sym += strlen (#op); \
a0c8462f
AM
8136 if (*sym == ':') \
8137 ++sym; \
0f02bbd9 8138 *symp = sym; \
8b127cbc 8139 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8140 isymbuf, locsymcount, signed_p)) \
a0c8462f 8141 return FALSE; \
0f02bbd9 8142 ++*symp; \
8b127cbc 8143 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8144 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8145 return FALSE; \
8146 if (signed_p) \
0f02bbd9 8147 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8148 else \
8149 *result = a op b; \
d9352518
DB
8150 return TRUE; \
8151 }
8152
8153 default:
8154 UNARY_OP (0-);
8155 BINARY_OP (<<);
8156 BINARY_OP (>>);
8157 BINARY_OP (==);
8158 BINARY_OP (!=);
8159 BINARY_OP (<=);
8160 BINARY_OP (>=);
8161 BINARY_OP (&&);
8162 BINARY_OP (||);
8163 UNARY_OP (~);
8164 UNARY_OP (!);
8165 BINARY_OP (*);
8166 BINARY_OP (/);
8167 BINARY_OP (%);
8168 BINARY_OP (^);
8169 BINARY_OP (|);
8170 BINARY_OP (&);
8171 BINARY_OP (+);
8172 BINARY_OP (-);
8173 BINARY_OP (<);
8174 BINARY_OP (>);
8175#undef UNARY_OP
8176#undef BINARY_OP
8177 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8178 bfd_set_error (bfd_error_invalid_operation);
8179 return FALSE;
8180 }
8181}
8182
d9352518 8183static void
a0c8462f
AM
8184put_value (bfd_vma size,
8185 unsigned long chunksz,
8186 bfd *input_bfd,
8187 bfd_vma x,
8188 bfd_byte *location)
d9352518
DB
8189{
8190 location += (size - chunksz);
8191
41cd1ad1 8192 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8193 {
8194 switch (chunksz)
8195 {
d9352518
DB
8196 case 1:
8197 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8198 x >>= 8;
d9352518
DB
8199 break;
8200 case 2:
8201 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8202 x >>= 16;
d9352518
DB
8203 break;
8204 case 4:
8205 bfd_put_32 (input_bfd, x, location);
65164438
NC
8206 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8207 x >>= 16;
8208 x >>= 16;
d9352518 8209 break;
d9352518 8210#ifdef BFD64
41cd1ad1 8211 case 8:
d9352518 8212 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8213 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8214 x >>= 32;
8215 x >>= 32;
8216 break;
d9352518 8217#endif
41cd1ad1
NC
8218 default:
8219 abort ();
d9352518
DB
8220 break;
8221 }
8222 }
8223}
8224
a0c8462f
AM
8225static bfd_vma
8226get_value (bfd_vma size,
8227 unsigned long chunksz,
8228 bfd *input_bfd,
8229 bfd_byte *location)
d9352518 8230{
9b239e0e 8231 int shift;
d9352518
DB
8232 bfd_vma x = 0;
8233
9b239e0e
NC
8234 /* Sanity checks. */
8235 BFD_ASSERT (chunksz <= sizeof (x)
8236 && size >= chunksz
8237 && chunksz != 0
8238 && (size % chunksz) == 0
8239 && input_bfd != NULL
8240 && location != NULL);
8241
8242 if (chunksz == sizeof (x))
8243 {
8244 BFD_ASSERT (size == chunksz);
8245
8246 /* Make sure that we do not perform an undefined shift operation.
8247 We know that size == chunksz so there will only be one iteration
8248 of the loop below. */
8249 shift = 0;
8250 }
8251 else
8252 shift = 8 * chunksz;
8253
a0c8462f 8254 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8255 {
8256 switch (chunksz)
8257 {
d9352518 8258 case 1:
9b239e0e 8259 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8260 break;
8261 case 2:
9b239e0e 8262 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8263 break;
8264 case 4:
9b239e0e 8265 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8266 break;
d9352518 8267#ifdef BFD64
9b239e0e
NC
8268 case 8:
8269 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8270 break;
9b239e0e
NC
8271#endif
8272 default:
8273 abort ();
d9352518
DB
8274 }
8275 }
8276 return x;
8277}
8278
a0c8462f
AM
8279static void
8280decode_complex_addend (unsigned long *start, /* in bits */
8281 unsigned long *oplen, /* in bits */
8282 unsigned long *len, /* in bits */
8283 unsigned long *wordsz, /* in bytes */
8284 unsigned long *chunksz, /* in bytes */
8285 unsigned long *lsb0_p,
8286 unsigned long *signed_p,
8287 unsigned long *trunc_p,
8288 unsigned long encoded)
d9352518
DB
8289{
8290 * start = encoded & 0x3F;
8291 * len = (encoded >> 6) & 0x3F;
8292 * oplen = (encoded >> 12) & 0x3F;
8293 * wordsz = (encoded >> 18) & 0xF;
8294 * chunksz = (encoded >> 22) & 0xF;
8295 * lsb0_p = (encoded >> 27) & 1;
8296 * signed_p = (encoded >> 28) & 1;
8297 * trunc_p = (encoded >> 29) & 1;
8298}
8299
cdfeee4f 8300bfd_reloc_status_type
0f02bbd9 8301bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8302 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8303 bfd_byte *contents,
8304 Elf_Internal_Rela *rel,
8305 bfd_vma relocation)
d9352518 8306{
0f02bbd9
AM
8307 bfd_vma shift, x, mask;
8308 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8309 bfd_reloc_status_type r;
d9352518
DB
8310
8311 /* Perform this reloc, since it is complex.
8312 (this is not to say that it necessarily refers to a complex
8313 symbol; merely that it is a self-describing CGEN based reloc.
8314 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8315 word size, etc) encoded within it.). */
d9352518 8316
a0c8462f
AM
8317 decode_complex_addend (&start, &oplen, &len, &wordsz,
8318 &chunksz, &lsb0_p, &signed_p,
8319 &trunc_p, rel->r_addend);
d9352518
DB
8320
8321 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8322
8323 if (lsb0_p)
8324 shift = (start + 1) - len;
8325 else
8326 shift = (8 * wordsz) - (start + len);
8327
37b01f6a
DG
8328 x = get_value (wordsz, chunksz, input_bfd,
8329 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8330
8331#ifdef DEBUG
8332 printf ("Doing complex reloc: "
8333 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8334 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8335 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8336 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8337 oplen, (unsigned long) x, (unsigned long) mask,
8338 (unsigned long) relocation);
d9352518
DB
8339#endif
8340
cdfeee4f 8341 r = bfd_reloc_ok;
d9352518 8342 if (! trunc_p)
cdfeee4f
AM
8343 /* Now do an overflow check. */
8344 r = bfd_check_overflow ((signed_p
8345 ? complain_overflow_signed
8346 : complain_overflow_unsigned),
8347 len, 0, (8 * wordsz),
8348 relocation);
a0c8462f 8349
d9352518
DB
8350 /* Do the deed. */
8351 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8352
8353#ifdef DEBUG
8354 printf (" relocation: %8.8lx\n"
8355 " shifted mask: %8.8lx\n"
8356 " shifted/masked reloc: %8.8lx\n"
8357 " result: %8.8lx\n",
9ccb8af9
AM
8358 (unsigned long) relocation, (unsigned long) (mask << shift),
8359 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8360#endif
37b01f6a
DG
8361 put_value (wordsz, chunksz, input_bfd, x,
8362 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8363 return r;
d9352518
DB
8364}
8365
0e287786
AM
8366/* Functions to read r_offset from external (target order) reloc
8367 entry. Faster than bfd_getl32 et al, because we let the compiler
8368 know the value is aligned. */
53df40a4 8369
0e287786
AM
8370static bfd_vma
8371ext32l_r_offset (const void *p)
53df40a4
AM
8372{
8373 union aligned32
8374 {
8375 uint32_t v;
8376 unsigned char c[4];
8377 };
8378 const union aligned32 *a
0e287786 8379 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8380
8381 uint32_t aval = ( (uint32_t) a->c[0]
8382 | (uint32_t) a->c[1] << 8
8383 | (uint32_t) a->c[2] << 16
8384 | (uint32_t) a->c[3] << 24);
0e287786 8385 return aval;
53df40a4
AM
8386}
8387
0e287786
AM
8388static bfd_vma
8389ext32b_r_offset (const void *p)
53df40a4
AM
8390{
8391 union aligned32
8392 {
8393 uint32_t v;
8394 unsigned char c[4];
8395 };
8396 const union aligned32 *a
0e287786 8397 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8398
8399 uint32_t aval = ( (uint32_t) a->c[0] << 24
8400 | (uint32_t) a->c[1] << 16
8401 | (uint32_t) a->c[2] << 8
8402 | (uint32_t) a->c[3]);
0e287786 8403 return aval;
53df40a4
AM
8404}
8405
8406#ifdef BFD_HOST_64_BIT
0e287786
AM
8407static bfd_vma
8408ext64l_r_offset (const void *p)
53df40a4
AM
8409{
8410 union aligned64
8411 {
8412 uint64_t v;
8413 unsigned char c[8];
8414 };
8415 const union aligned64 *a
0e287786 8416 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8417
8418 uint64_t aval = ( (uint64_t) a->c[0]
8419 | (uint64_t) a->c[1] << 8
8420 | (uint64_t) a->c[2] << 16
8421 | (uint64_t) a->c[3] << 24
8422 | (uint64_t) a->c[4] << 32
8423 | (uint64_t) a->c[5] << 40
8424 | (uint64_t) a->c[6] << 48
8425 | (uint64_t) a->c[7] << 56);
0e287786 8426 return aval;
53df40a4
AM
8427}
8428
0e287786
AM
8429static bfd_vma
8430ext64b_r_offset (const void *p)
53df40a4
AM
8431{
8432 union aligned64
8433 {
8434 uint64_t v;
8435 unsigned char c[8];
8436 };
8437 const union aligned64 *a
0e287786 8438 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8439
8440 uint64_t aval = ( (uint64_t) a->c[0] << 56
8441 | (uint64_t) a->c[1] << 48
8442 | (uint64_t) a->c[2] << 40
8443 | (uint64_t) a->c[3] << 32
8444 | (uint64_t) a->c[4] << 24
8445 | (uint64_t) a->c[5] << 16
8446 | (uint64_t) a->c[6] << 8
8447 | (uint64_t) a->c[7]);
0e287786 8448 return aval;
53df40a4
AM
8449}
8450#endif
8451
c152c796
AM
8452/* When performing a relocatable link, the input relocations are
8453 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8454 referenced must be updated. Update all the relocations found in
8455 RELDATA. */
c152c796 8456
bca6d0e3 8457static bfd_boolean
c152c796 8458elf_link_adjust_relocs (bfd *abfd,
9eaff861 8459 asection *sec,
28dbcedc
AM
8460 struct bfd_elf_section_reloc_data *reldata,
8461 bfd_boolean sort)
c152c796
AM
8462{
8463 unsigned int i;
8464 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8465 bfd_byte *erela;
8466 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8467 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8468 bfd_vma r_type_mask;
8469 int r_sym_shift;
d4730f92
BS
8470 unsigned int count = reldata->count;
8471 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8472
d4730f92 8473 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8474 {
8475 swap_in = bed->s->swap_reloc_in;
8476 swap_out = bed->s->swap_reloc_out;
8477 }
d4730f92 8478 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8479 {
8480 swap_in = bed->s->swap_reloca_in;
8481 swap_out = bed->s->swap_reloca_out;
8482 }
8483 else
8484 abort ();
8485
8486 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8487 abort ();
8488
8489 if (bed->s->arch_size == 32)
8490 {
8491 r_type_mask = 0xff;
8492 r_sym_shift = 8;
8493 }
8494 else
8495 {
8496 r_type_mask = 0xffffffff;
8497 r_sym_shift = 32;
8498 }
8499
d4730f92
BS
8500 erela = reldata->hdr->contents;
8501 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8502 {
8503 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8504 unsigned int j;
8505
8506 if (*rel_hash == NULL)
8507 continue;
8508
8509 BFD_ASSERT ((*rel_hash)->indx >= 0);
8510
8511 (*swap_in) (abfd, erela, irela);
8512 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8513 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8514 | (irela[j].r_info & r_type_mask));
8515 (*swap_out) (abfd, irela, erela);
8516 }
53df40a4 8517
9eaff861
AO
8518 if (bed->elf_backend_update_relocs)
8519 (*bed->elf_backend_update_relocs) (sec, reldata);
8520
0e287786 8521 if (sort && count != 0)
53df40a4 8522 {
0e287786
AM
8523 bfd_vma (*ext_r_off) (const void *);
8524 bfd_vma r_off;
8525 size_t elt_size;
8526 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8527 bfd_byte *buf = NULL;
28dbcedc
AM
8528
8529 if (bed->s->arch_size == 32)
8530 {
8531 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8532 ext_r_off = ext32l_r_offset;
28dbcedc 8533 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8534 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8535 else
8536 abort ();
8537 }
53df40a4 8538 else
28dbcedc 8539 {
53df40a4 8540#ifdef BFD_HOST_64_BIT
28dbcedc 8541 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8542 ext_r_off = ext64l_r_offset;
28dbcedc 8543 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8544 ext_r_off = ext64b_r_offset;
28dbcedc 8545 else
53df40a4 8546#endif
28dbcedc
AM
8547 abort ();
8548 }
0e287786 8549
bca6d0e3
AM
8550 /* Must use a stable sort here. A modified insertion sort,
8551 since the relocs are mostly sorted already. */
0e287786
AM
8552 elt_size = reldata->hdr->sh_entsize;
8553 base = reldata->hdr->contents;
8554 end = base + count * elt_size;
bca6d0e3 8555 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8556 abort ();
8557
8558 /* Ensure the first element is lowest. This acts as a sentinel,
8559 speeding the main loop below. */
8560 r_off = (*ext_r_off) (base);
8561 for (p = loc = base; (p += elt_size) < end; )
8562 {
8563 bfd_vma r_off2 = (*ext_r_off) (p);
8564 if (r_off > r_off2)
8565 {
8566 r_off = r_off2;
8567 loc = p;
8568 }
8569 }
8570 if (loc != base)
8571 {
8572 /* Don't just swap *base and *loc as that changes the order
8573 of the original base[0] and base[1] if they happen to
8574 have the same r_offset. */
bca6d0e3
AM
8575 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8576 memcpy (onebuf, loc, elt_size);
0e287786 8577 memmove (base + elt_size, base, loc - base);
bca6d0e3 8578 memcpy (base, onebuf, elt_size);
0e287786
AM
8579 }
8580
b29b8669 8581 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8582 {
8583 /* base to p is sorted, *p is next to insert. */
8584 r_off = (*ext_r_off) (p);
8585 /* Search the sorted region for location to insert. */
8586 loc = p - elt_size;
8587 while (r_off < (*ext_r_off) (loc))
8588 loc -= elt_size;
8589 loc += elt_size;
8590 if (loc != p)
8591 {
bca6d0e3
AM
8592 /* Chances are there is a run of relocs to insert here,
8593 from one of more input files. Files are not always
8594 linked in order due to the way elf_link_input_bfd is
8595 called. See pr17666. */
8596 size_t sortlen = p - loc;
8597 bfd_vma r_off2 = (*ext_r_off) (loc);
8598 size_t runlen = elt_size;
8599 size_t buf_size = 96 * 1024;
8600 while (p + runlen < end
8601 && (sortlen <= buf_size
8602 || runlen + elt_size <= buf_size)
8603 && r_off2 > (*ext_r_off) (p + runlen))
8604 runlen += elt_size;
8605 if (buf == NULL)
8606 {
8607 buf = bfd_malloc (buf_size);
8608 if (buf == NULL)
8609 return FALSE;
8610 }
8611 if (runlen < sortlen)
8612 {
8613 memcpy (buf, p, runlen);
8614 memmove (loc + runlen, loc, sortlen);
8615 memcpy (loc, buf, runlen);
8616 }
8617 else
8618 {
8619 memcpy (buf, loc, sortlen);
8620 memmove (loc, p, runlen);
8621 memcpy (loc + runlen, buf, sortlen);
8622 }
b29b8669 8623 p += runlen - elt_size;
0e287786
AM
8624 }
8625 }
8626 /* Hashes are no longer valid. */
28dbcedc
AM
8627 free (reldata->hashes);
8628 reldata->hashes = NULL;
bca6d0e3 8629 free (buf);
53df40a4 8630 }
bca6d0e3 8631 return TRUE;
c152c796
AM
8632}
8633
8634struct elf_link_sort_rela
8635{
8636 union {
8637 bfd_vma offset;
8638 bfd_vma sym_mask;
8639 } u;
8640 enum elf_reloc_type_class type;
8641 /* We use this as an array of size int_rels_per_ext_rel. */
8642 Elf_Internal_Rela rela[1];
8643};
8644
8645static int
8646elf_link_sort_cmp1 (const void *A, const void *B)
8647{
a50b1753
NC
8648 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8649 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8650 int relativea, relativeb;
8651
8652 relativea = a->type == reloc_class_relative;
8653 relativeb = b->type == reloc_class_relative;
8654
8655 if (relativea < relativeb)
8656 return 1;
8657 if (relativea > relativeb)
8658 return -1;
8659 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8660 return -1;
8661 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8662 return 1;
8663 if (a->rela->r_offset < b->rela->r_offset)
8664 return -1;
8665 if (a->rela->r_offset > b->rela->r_offset)
8666 return 1;
8667 return 0;
8668}
8669
8670static int
8671elf_link_sort_cmp2 (const void *A, const void *B)
8672{
a50b1753
NC
8673 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8674 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8675
7e612e98 8676 if (a->type < b->type)
c152c796 8677 return -1;
7e612e98 8678 if (a->type > b->type)
c152c796 8679 return 1;
7e612e98 8680 if (a->u.offset < b->u.offset)
c152c796 8681 return -1;
7e612e98 8682 if (a->u.offset > b->u.offset)
c152c796
AM
8683 return 1;
8684 if (a->rela->r_offset < b->rela->r_offset)
8685 return -1;
8686 if (a->rela->r_offset > b->rela->r_offset)
8687 return 1;
8688 return 0;
8689}
8690
8691static size_t
8692elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8693{
3410fea8 8694 asection *dynamic_relocs;
fc66a176
L
8695 asection *rela_dyn;
8696 asection *rel_dyn;
c152c796
AM
8697 bfd_size_type count, size;
8698 size_t i, ret, sort_elt, ext_size;
8699 bfd_byte *sort, *s_non_relative, *p;
8700 struct elf_link_sort_rela *sq;
8701 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8702 int i2e = bed->s->int_rels_per_ext_rel;
c8e44c6d 8703 unsigned int opb = bfd_octets_per_byte (abfd);
c152c796
AM
8704 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8705 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8706 struct bfd_link_order *lo;
8707 bfd_vma r_sym_mask;
3410fea8 8708 bfd_boolean use_rela;
c152c796 8709
3410fea8
NC
8710 /* Find a dynamic reloc section. */
8711 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8712 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8713 if (rela_dyn != NULL && rela_dyn->size > 0
8714 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8715 {
3410fea8
NC
8716 bfd_boolean use_rela_initialised = FALSE;
8717
8718 /* This is just here to stop gcc from complaining.
c8e44c6d 8719 Its initialization checking code is not perfect. */
3410fea8
NC
8720 use_rela = TRUE;
8721
8722 /* Both sections are present. Examine the sizes
8723 of the indirect sections to help us choose. */
8724 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8725 if (lo->type == bfd_indirect_link_order)
8726 {
8727 asection *o = lo->u.indirect.section;
8728
8729 if ((o->size % bed->s->sizeof_rela) == 0)
8730 {
8731 if ((o->size % bed->s->sizeof_rel) == 0)
8732 /* Section size is divisible by both rel and rela sizes.
8733 It is of no help to us. */
8734 ;
8735 else
8736 {
8737 /* Section size is only divisible by rela. */
8738 if (use_rela_initialised && (use_rela == FALSE))
8739 {
c8e44c6d
AM
8740 _bfd_error_handler (_("%B: Unable to sort relocs - "
8741 "they are in more than one size"),
8742 abfd);
3410fea8
NC
8743 bfd_set_error (bfd_error_invalid_operation);
8744 return 0;
8745 }
8746 else
8747 {
8748 use_rela = TRUE;
8749 use_rela_initialised = TRUE;
8750 }
8751 }
8752 }
8753 else if ((o->size % bed->s->sizeof_rel) == 0)
8754 {
8755 /* Section size is only divisible by rel. */
8756 if (use_rela_initialised && (use_rela == TRUE))
8757 {
c8e44c6d
AM
8758 _bfd_error_handler (_("%B: Unable to sort relocs - "
8759 "they are in more than one size"),
8760 abfd);
3410fea8
NC
8761 bfd_set_error (bfd_error_invalid_operation);
8762 return 0;
8763 }
8764 else
8765 {
8766 use_rela = FALSE;
8767 use_rela_initialised = TRUE;
8768 }
8769 }
8770 else
8771 {
c8e44c6d
AM
8772 /* The section size is not divisible by either -
8773 something is wrong. */
8774 _bfd_error_handler (_("%B: Unable to sort relocs - "
8775 "they are of an unknown size"), abfd);
3410fea8
NC
8776 bfd_set_error (bfd_error_invalid_operation);
8777 return 0;
8778 }
8779 }
8780
8781 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8782 if (lo->type == bfd_indirect_link_order)
8783 {
8784 asection *o = lo->u.indirect.section;
8785
8786 if ((o->size % bed->s->sizeof_rela) == 0)
8787 {
8788 if ((o->size % bed->s->sizeof_rel) == 0)
8789 /* Section size is divisible by both rel and rela sizes.
8790 It is of no help to us. */
8791 ;
8792 else
8793 {
8794 /* Section size is only divisible by rela. */
8795 if (use_rela_initialised && (use_rela == FALSE))
8796 {
c8e44c6d
AM
8797 _bfd_error_handler (_("%B: Unable to sort relocs - "
8798 "they are in more than one size"),
8799 abfd);
3410fea8
NC
8800 bfd_set_error (bfd_error_invalid_operation);
8801 return 0;
8802 }
8803 else
8804 {
8805 use_rela = TRUE;
8806 use_rela_initialised = TRUE;
8807 }
8808 }
8809 }
8810 else if ((o->size % bed->s->sizeof_rel) == 0)
8811 {
8812 /* Section size is only divisible by rel. */
8813 if (use_rela_initialised && (use_rela == TRUE))
8814 {
c8e44c6d
AM
8815 _bfd_error_handler (_("%B: Unable to sort relocs - "
8816 "they are in more than one size"),
8817 abfd);
3410fea8
NC
8818 bfd_set_error (bfd_error_invalid_operation);
8819 return 0;
8820 }
8821 else
8822 {
8823 use_rela = FALSE;
8824 use_rela_initialised = TRUE;
8825 }
8826 }
8827 else
8828 {
c8e44c6d
AM
8829 /* The section size is not divisible by either -
8830 something is wrong. */
8831 _bfd_error_handler (_("%B: Unable to sort relocs - "
8832 "they are of an unknown size"), abfd);
3410fea8
NC
8833 bfd_set_error (bfd_error_invalid_operation);
8834 return 0;
8835 }
8836 }
8837
8838 if (! use_rela_initialised)
8839 /* Make a guess. */
8840 use_rela = TRUE;
c152c796 8841 }
fc66a176
L
8842 else if (rela_dyn != NULL && rela_dyn->size > 0)
8843 use_rela = TRUE;
8844 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8845 use_rela = FALSE;
c152c796 8846 else
fc66a176 8847 return 0;
3410fea8
NC
8848
8849 if (use_rela)
c152c796 8850 {
3410fea8 8851 dynamic_relocs = rela_dyn;
c152c796
AM
8852 ext_size = bed->s->sizeof_rela;
8853 swap_in = bed->s->swap_reloca_in;
8854 swap_out = bed->s->swap_reloca_out;
8855 }
3410fea8
NC
8856 else
8857 {
8858 dynamic_relocs = rel_dyn;
8859 ext_size = bed->s->sizeof_rel;
8860 swap_in = bed->s->swap_reloc_in;
8861 swap_out = bed->s->swap_reloc_out;
8862 }
c152c796
AM
8863
8864 size = 0;
3410fea8 8865 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8866 if (lo->type == bfd_indirect_link_order)
3410fea8 8867 size += lo->u.indirect.section->size;
c152c796 8868
3410fea8 8869 if (size != dynamic_relocs->size)
c152c796
AM
8870 return 0;
8871
8872 sort_elt = (sizeof (struct elf_link_sort_rela)
8873 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8874
8875 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8876 if (count == 0)
8877 return 0;
a50b1753 8878 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8879
c152c796
AM
8880 if (sort == NULL)
8881 {
8882 (*info->callbacks->warning)
8883 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8884 return 0;
8885 }
8886
8887 if (bed->s->arch_size == 32)
8888 r_sym_mask = ~(bfd_vma) 0xff;
8889 else
8890 r_sym_mask = ~(bfd_vma) 0xffffffff;
8891
3410fea8 8892 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8893 if (lo->type == bfd_indirect_link_order)
8894 {
8895 bfd_byte *erel, *erelend;
8896 asection *o = lo->u.indirect.section;
8897
1da212d6
AM
8898 if (o->contents == NULL && o->size != 0)
8899 {
8900 /* This is a reloc section that is being handled as a normal
8901 section. See bfd_section_from_shdr. We can't combine
8902 relocs in this case. */
8903 free (sort);
8904 return 0;
8905 }
c152c796 8906 erel = o->contents;
eea6121a 8907 erelend = o->contents + o->size;
c8e44c6d 8908 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 8909
c152c796
AM
8910 while (erel < erelend)
8911 {
8912 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8913
c152c796 8914 (*swap_in) (abfd, erel, s->rela);
7e612e98 8915 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8916 s->u.sym_mask = r_sym_mask;
8917 p += sort_elt;
8918 erel += ext_size;
8919 }
8920 }
8921
8922 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8923
8924 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8925 {
8926 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8927 if (s->type != reloc_class_relative)
8928 break;
8929 }
8930 ret = i;
8931 s_non_relative = p;
8932
8933 sq = (struct elf_link_sort_rela *) s_non_relative;
8934 for (; i < count; i++, p += sort_elt)
8935 {
8936 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8937 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8938 sq = sp;
8939 sp->u.offset = sq->rela->r_offset;
8940 }
8941
8942 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8943
c8e44c6d
AM
8944 struct elf_link_hash_table *htab = elf_hash_table (info);
8945 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8946 {
8947 /* We have plt relocs in .rela.dyn. */
8948 sq = (struct elf_link_sort_rela *) sort;
8949 for (i = 0; i < count; i++)
8950 if (sq[count - i - 1].type != reloc_class_plt)
8951 break;
8952 if (i != 0 && htab->srelplt->size == i * ext_size)
8953 {
8954 struct bfd_link_order **plo;
8955 /* Put srelplt link_order last. This is so the output_offset
8956 set in the next loop is correct for DT_JMPREL. */
8957 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8958 if ((*plo)->type == bfd_indirect_link_order
8959 && (*plo)->u.indirect.section == htab->srelplt)
8960 {
8961 lo = *plo;
8962 *plo = lo->next;
8963 }
8964 else
8965 plo = &(*plo)->next;
8966 *plo = lo;
8967 lo->next = NULL;
8968 dynamic_relocs->map_tail.link_order = lo;
8969 }
8970 }
8971
8972 p = sort;
3410fea8 8973 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8974 if (lo->type == bfd_indirect_link_order)
8975 {
8976 bfd_byte *erel, *erelend;
8977 asection *o = lo->u.indirect.section;
8978
8979 erel = o->contents;
eea6121a 8980 erelend = o->contents + o->size;
c8e44c6d 8981 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
8982 while (erel < erelend)
8983 {
8984 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8985 (*swap_out) (abfd, s->rela, erel);
8986 p += sort_elt;
8987 erel += ext_size;
8988 }
8989 }
8990
8991 free (sort);
3410fea8 8992 *psec = dynamic_relocs;
c152c796
AM
8993 return ret;
8994}
8995
ef10c3ac 8996/* Add a symbol to the output symbol string table. */
c152c796 8997
6e0b88f1 8998static int
ef10c3ac
L
8999elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9000 const char *name,
9001 Elf_Internal_Sym *elfsym,
9002 asection *input_sec,
9003 struct elf_link_hash_entry *h)
c152c796 9004{
6e0b88f1 9005 int (*output_symbol_hook)
c152c796
AM
9006 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9007 struct elf_link_hash_entry *);
ef10c3ac 9008 struct elf_link_hash_table *hash_table;
c152c796 9009 const struct elf_backend_data *bed;
ef10c3ac 9010 bfd_size_type strtabsize;
c152c796 9011
8539e4e8
AM
9012 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9013
8b127cbc 9014 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9015 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9016 if (output_symbol_hook != NULL)
9017 {
8b127cbc 9018 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9019 if (ret != 1)
9020 return ret;
c152c796
AM
9021 }
9022
ef10c3ac
L
9023 if (name == NULL
9024 || *name == '\0'
9025 || (input_sec->flags & SEC_EXCLUDE))
9026 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9027 else
9028 {
ef10c3ac
L
9029 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9030 to get the final offset for st_name. */
9031 elfsym->st_name
9032 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9033 name, FALSE);
c152c796 9034 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9035 return 0;
c152c796
AM
9036 }
9037
ef10c3ac
L
9038 hash_table = elf_hash_table (flinfo->info);
9039 strtabsize = hash_table->strtabsize;
9040 if (strtabsize <= hash_table->strtabcount)
c152c796 9041 {
ef10c3ac
L
9042 strtabsize += strtabsize;
9043 hash_table->strtabsize = strtabsize;
9044 strtabsize *= sizeof (*hash_table->strtab);
9045 hash_table->strtab
9046 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9047 strtabsize);
9048 if (hash_table->strtab == NULL)
6e0b88f1 9049 return 0;
c152c796 9050 }
ef10c3ac
L
9051 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9052 hash_table->strtab[hash_table->strtabcount].dest_index
9053 = hash_table->strtabcount;
9054 hash_table->strtab[hash_table->strtabcount].destshndx_index
9055 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9056
9057 bfd_get_symcount (flinfo->output_bfd) += 1;
9058 hash_table->strtabcount += 1;
9059
9060 return 1;
9061}
9062
9063/* Swap symbols out to the symbol table and flush the output symbols to
9064 the file. */
9065
9066static bfd_boolean
9067elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9068{
9069 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
ef53be89
AM
9070 bfd_size_type amt;
9071 size_t i;
ef10c3ac
L
9072 const struct elf_backend_data *bed;
9073 bfd_byte *symbuf;
9074 Elf_Internal_Shdr *hdr;
9075 file_ptr pos;
9076 bfd_boolean ret;
9077
9078 if (!hash_table->strtabcount)
9079 return TRUE;
9080
9081 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9082
9083 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9084
ef10c3ac
L
9085 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9086 symbuf = (bfd_byte *) bfd_malloc (amt);
9087 if (symbuf == NULL)
9088 return FALSE;
1b786873 9089
ef10c3ac 9090 if (flinfo->symshndxbuf)
c152c796 9091 {
ef53be89
AM
9092 amt = sizeof (Elf_External_Sym_Shndx);
9093 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9094 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9095 if (flinfo->symshndxbuf == NULL)
c152c796 9096 {
ef10c3ac
L
9097 free (symbuf);
9098 return FALSE;
c152c796 9099 }
c152c796
AM
9100 }
9101
ef10c3ac
L
9102 for (i = 0; i < hash_table->strtabcount; i++)
9103 {
9104 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9105 if (elfsym->sym.st_name == (unsigned long) -1)
9106 elfsym->sym.st_name = 0;
9107 else
9108 elfsym->sym.st_name
9109 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9110 elfsym->sym.st_name);
9111 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9112 ((bfd_byte *) symbuf
9113 + (elfsym->dest_index
9114 * bed->s->sizeof_sym)),
9115 (flinfo->symshndxbuf
9116 + elfsym->destshndx_index));
9117 }
9118
9119 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9120 pos = hdr->sh_offset + hdr->sh_size;
9121 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9122 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9123 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9124 {
9125 hdr->sh_size += amt;
9126 ret = TRUE;
9127 }
9128 else
9129 ret = FALSE;
c152c796 9130
ef10c3ac
L
9131 free (symbuf);
9132
9133 free (hash_table->strtab);
9134 hash_table->strtab = NULL;
9135
9136 return ret;
c152c796
AM
9137}
9138
c0d5a53d
L
9139/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9140
9141static bfd_boolean
9142check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9143{
4fbb74a6
AM
9144 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9145 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9146 {
9147 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9148 beyond 64k. */
4eca0228 9149 _bfd_error_handler
695344c0 9150 /* xgettext:c-format */
c0d5a53d 9151 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 9152 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9153 bfd_set_error (bfd_error_nonrepresentable_section);
9154 return FALSE;
9155 }
9156 return TRUE;
9157}
9158
c152c796
AM
9159/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9160 allowing an unsatisfied unversioned symbol in the DSO to match a
9161 versioned symbol that would normally require an explicit version.
9162 We also handle the case that a DSO references a hidden symbol
9163 which may be satisfied by a versioned symbol in another DSO. */
9164
9165static bfd_boolean
9166elf_link_check_versioned_symbol (struct bfd_link_info *info,
9167 const struct elf_backend_data *bed,
9168 struct elf_link_hash_entry *h)
9169{
9170 bfd *abfd;
9171 struct elf_link_loaded_list *loaded;
9172
9173 if (!is_elf_hash_table (info->hash))
9174 return FALSE;
9175
90c984fc
L
9176 /* Check indirect symbol. */
9177 while (h->root.type == bfd_link_hash_indirect)
9178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9179
c152c796
AM
9180 switch (h->root.type)
9181 {
9182 default:
9183 abfd = NULL;
9184 break;
9185
9186 case bfd_link_hash_undefined:
9187 case bfd_link_hash_undefweak:
9188 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9189 if (abfd == NULL
9190 || (abfd->flags & DYNAMIC) == 0
e56f61be 9191 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9192 return FALSE;
9193 break;
9194
9195 case bfd_link_hash_defined:
9196 case bfd_link_hash_defweak:
9197 abfd = h->root.u.def.section->owner;
9198 break;
9199
9200 case bfd_link_hash_common:
9201 abfd = h->root.u.c.p->section->owner;
9202 break;
9203 }
9204 BFD_ASSERT (abfd != NULL);
9205
9206 for (loaded = elf_hash_table (info)->loaded;
9207 loaded != NULL;
9208 loaded = loaded->next)
9209 {
9210 bfd *input;
9211 Elf_Internal_Shdr *hdr;
ef53be89
AM
9212 size_t symcount;
9213 size_t extsymcount;
9214 size_t extsymoff;
c152c796
AM
9215 Elf_Internal_Shdr *versymhdr;
9216 Elf_Internal_Sym *isym;
9217 Elf_Internal_Sym *isymend;
9218 Elf_Internal_Sym *isymbuf;
9219 Elf_External_Versym *ever;
9220 Elf_External_Versym *extversym;
9221
9222 input = loaded->abfd;
9223
9224 /* We check each DSO for a possible hidden versioned definition. */
9225 if (input == abfd
9226 || (input->flags & DYNAMIC) == 0
9227 || elf_dynversym (input) == 0)
9228 continue;
9229
9230 hdr = &elf_tdata (input)->dynsymtab_hdr;
9231
9232 symcount = hdr->sh_size / bed->s->sizeof_sym;
9233 if (elf_bad_symtab (input))
9234 {
9235 extsymcount = symcount;
9236 extsymoff = 0;
9237 }
9238 else
9239 {
9240 extsymcount = symcount - hdr->sh_info;
9241 extsymoff = hdr->sh_info;
9242 }
9243
9244 if (extsymcount == 0)
9245 continue;
9246
9247 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9248 NULL, NULL, NULL);
9249 if (isymbuf == NULL)
9250 return FALSE;
9251
9252 /* Read in any version definitions. */
9253 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9254 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9255 if (extversym == NULL)
9256 goto error_ret;
9257
9258 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9259 || (bfd_bread (extversym, versymhdr->sh_size, input)
9260 != versymhdr->sh_size))
9261 {
9262 free (extversym);
9263 error_ret:
9264 free (isymbuf);
9265 return FALSE;
9266 }
9267
9268 ever = extversym + extsymoff;
9269 isymend = isymbuf + extsymcount;
9270 for (isym = isymbuf; isym < isymend; isym++, ever++)
9271 {
9272 const char *name;
9273 Elf_Internal_Versym iver;
9274 unsigned short version_index;
9275
9276 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9277 || isym->st_shndx == SHN_UNDEF)
9278 continue;
9279
9280 name = bfd_elf_string_from_elf_section (input,
9281 hdr->sh_link,
9282 isym->st_name);
9283 if (strcmp (name, h->root.root.string) != 0)
9284 continue;
9285
9286 _bfd_elf_swap_versym_in (input, ever, &iver);
9287
d023c380
L
9288 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9289 && !(h->def_regular
9290 && h->forced_local))
c152c796
AM
9291 {
9292 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9293 have provided a definition for the undefined sym unless
9294 it is defined in a non-shared object and forced local.
9295 */
c152c796
AM
9296 abort ();
9297 }
9298
9299 version_index = iver.vs_vers & VERSYM_VERSION;
9300 if (version_index == 1 || version_index == 2)
9301 {
9302 /* This is the base or first version. We can use it. */
9303 free (extversym);
9304 free (isymbuf);
9305 return TRUE;
9306 }
9307 }
9308
9309 free (extversym);
9310 free (isymbuf);
9311 }
9312
9313 return FALSE;
9314}
9315
b8871f35
L
9316/* Convert ELF common symbol TYPE. */
9317
9318static int
9319elf_link_convert_common_type (struct bfd_link_info *info, int type)
9320{
9321 /* Commom symbol can only appear in relocatable link. */
9322 if (!bfd_link_relocatable (info))
9323 abort ();
9324 switch (info->elf_stt_common)
9325 {
9326 case unchanged:
9327 break;
9328 case elf_stt_common:
9329 type = STT_COMMON;
9330 break;
9331 case no_elf_stt_common:
9332 type = STT_OBJECT;
9333 break;
9334 }
9335 return type;
9336}
9337
c152c796
AM
9338/* Add an external symbol to the symbol table. This is called from
9339 the hash table traversal routine. When generating a shared object,
9340 we go through the symbol table twice. The first time we output
9341 anything that might have been forced to local scope in a version
9342 script. The second time we output the symbols that are still
9343 global symbols. */
9344
9345static bfd_boolean
7686d77d 9346elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9347{
7686d77d 9348 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9349 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9350 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9351 bfd_boolean strip;
9352 Elf_Internal_Sym sym;
9353 asection *input_sec;
9354 const struct elf_backend_data *bed;
6e0b88f1
AM
9355 long indx;
9356 int ret;
b8871f35 9357 unsigned int type;
c152c796
AM
9358
9359 if (h->root.type == bfd_link_hash_warning)
9360 {
9361 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9362 if (h->root.type == bfd_link_hash_new)
9363 return TRUE;
9364 }
9365
9366 /* Decide whether to output this symbol in this pass. */
9367 if (eoinfo->localsyms)
9368 {
4deb8f71 9369 if (!h->forced_local)
c152c796
AM
9370 return TRUE;
9371 }
9372 else
9373 {
4deb8f71 9374 if (h->forced_local)
c152c796
AM
9375 return TRUE;
9376 }
9377
8b127cbc 9378 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9379
12ac1cf5 9380 if (h->root.type == bfd_link_hash_undefined)
c152c796 9381 {
12ac1cf5
NC
9382 /* If we have an undefined symbol reference here then it must have
9383 come from a shared library that is being linked in. (Undefined
98da7939
L
9384 references in regular files have already been handled unless
9385 they are in unreferenced sections which are removed by garbage
9386 collection). */
12ac1cf5
NC
9387 bfd_boolean ignore_undef = FALSE;
9388
9389 /* Some symbols may be special in that the fact that they're
9390 undefined can be safely ignored - let backend determine that. */
9391 if (bed->elf_backend_ignore_undef_symbol)
9392 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9393
9394 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9395 if (!ignore_undef
12ac1cf5 9396 && h->ref_dynamic
8b127cbc
AM
9397 && (!h->ref_regular || flinfo->info->gc_sections)
9398 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9399 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
1a72702b
AM
9400 (*flinfo->info->callbacks->undefined_symbol)
9401 (flinfo->info, h->root.root.string,
9402 h->ref_regular ? NULL : h->root.u.undef.abfd,
9403 NULL, 0,
9404 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
97196564
L
9405
9406 /* Strip a global symbol defined in a discarded section. */
9407 if (h->indx == -3)
9408 return TRUE;
c152c796
AM
9409 }
9410
9411 /* We should also warn if a forced local symbol is referenced from
9412 shared libraries. */
0e1862bb 9413 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9414 && h->forced_local
9415 && h->ref_dynamic
371a5866 9416 && h->def_regular
f5385ebf 9417 && !h->dynamic_def
ee659f1f 9418 && h->ref_dynamic_nonweak
8b127cbc 9419 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9420 {
17d078c5
AM
9421 bfd *def_bfd;
9422 const char *msg;
90c984fc
L
9423 struct elf_link_hash_entry *hi = h;
9424
9425 /* Check indirect symbol. */
9426 while (hi->root.type == bfd_link_hash_indirect)
9427 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9428
9429 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 9430 /* xgettext:c-format */
17d078c5
AM
9431 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9432 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 9433 /* xgettext:c-format */
17d078c5
AM
9434 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9435 else
695344c0 9436 /* xgettext:c-format */
17d078c5 9437 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9438 def_bfd = flinfo->output_bfd;
90c984fc
L
9439 if (hi->root.u.def.section != bfd_abs_section_ptr)
9440 def_bfd = hi->root.u.def.section->owner;
4eca0228
AM
9441 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9442 h->root.root.string);
17d078c5 9443 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9444 eoinfo->failed = TRUE;
9445 return FALSE;
9446 }
9447
9448 /* We don't want to output symbols that have never been mentioned by
9449 a regular file, or that we have been told to strip. However, if
9450 h->indx is set to -2, the symbol is used by a reloc and we must
9451 output it. */
d983c8c5 9452 strip = FALSE;
c152c796 9453 if (h->indx == -2)
d983c8c5 9454 ;
f5385ebf 9455 else if ((h->def_dynamic
77cfaee6
AM
9456 || h->ref_dynamic
9457 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9458 && !h->def_regular
9459 && !h->ref_regular)
c152c796 9460 strip = TRUE;
8b127cbc 9461 else if (flinfo->info->strip == strip_all)
c152c796 9462 strip = TRUE;
8b127cbc
AM
9463 else if (flinfo->info->strip == strip_some
9464 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9465 h->root.root.string, FALSE, FALSE) == NULL)
9466 strip = TRUE;
d56d55e7
AM
9467 else if ((h->root.type == bfd_link_hash_defined
9468 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9469 && ((flinfo->info->strip_discarded
dbaa2011 9470 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9471 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9472 && h->root.u.def.section->owner != NULL
d56d55e7 9473 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9474 strip = TRUE;
9e2278f5
AM
9475 else if ((h->root.type == bfd_link_hash_undefined
9476 || h->root.type == bfd_link_hash_undefweak)
9477 && h->root.u.undef.abfd != NULL
9478 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9479 strip = TRUE;
c152c796 9480
b8871f35
L
9481 type = h->type;
9482
c152c796 9483 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9484 nothing else to do. However, if it is a forced local symbol or
9485 an ifunc symbol we need to give the backend finish_dynamic_symbol
9486 function a chance to make it dynamic. */
c152c796
AM
9487 if (strip
9488 && h->dynindx == -1
b8871f35 9489 && type != STT_GNU_IFUNC
f5385ebf 9490 && !h->forced_local)
c152c796
AM
9491 return TRUE;
9492
9493 sym.st_value = 0;
9494 sym.st_size = h->size;
9495 sym.st_other = h->other;
c152c796
AM
9496 switch (h->root.type)
9497 {
9498 default:
9499 case bfd_link_hash_new:
9500 case bfd_link_hash_warning:
9501 abort ();
9502 return FALSE;
9503
9504 case bfd_link_hash_undefined:
9505 case bfd_link_hash_undefweak:
9506 input_sec = bfd_und_section_ptr;
9507 sym.st_shndx = SHN_UNDEF;
9508 break;
9509
9510 case bfd_link_hash_defined:
9511 case bfd_link_hash_defweak:
9512 {
9513 input_sec = h->root.u.def.section;
9514 if (input_sec->output_section != NULL)
9515 {
9516 sym.st_shndx =
8b127cbc 9517 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9518 input_sec->output_section);
9519 if (sym.st_shndx == SHN_BAD)
9520 {
4eca0228 9521 _bfd_error_handler
695344c0 9522 /* xgettext:c-format */
d003868e 9523 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9524 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9525 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9526 eoinfo->failed = TRUE;
9527 return FALSE;
9528 }
9529
9530 /* ELF symbols in relocatable files are section relative,
9531 but in nonrelocatable files they are virtual
9532 addresses. */
9533 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9534 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9535 {
9536 sym.st_value += input_sec->output_section->vma;
9537 if (h->type == STT_TLS)
9538 {
8b127cbc 9539 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9540 if (tls_sec != NULL)
9541 sym.st_value -= tls_sec->vma;
c152c796
AM
9542 }
9543 }
9544 }
9545 else
9546 {
9547 BFD_ASSERT (input_sec->owner == NULL
9548 || (input_sec->owner->flags & DYNAMIC) != 0);
9549 sym.st_shndx = SHN_UNDEF;
9550 input_sec = bfd_und_section_ptr;
9551 }
9552 }
9553 break;
9554
9555 case bfd_link_hash_common:
9556 input_sec = h->root.u.c.p->section;
a4d8e49b 9557 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9558 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9559 break;
9560
9561 case bfd_link_hash_indirect:
9562 /* These symbols are created by symbol versioning. They point
9563 to the decorated version of the name. For example, if the
9564 symbol foo@@GNU_1.2 is the default, which should be used when
9565 foo is used with no version, then we add an indirect symbol
9566 foo which points to foo@@GNU_1.2. We ignore these symbols,
9567 since the indirected symbol is already in the hash table. */
9568 return TRUE;
9569 }
9570
b8871f35
L
9571 if (type == STT_COMMON || type == STT_OBJECT)
9572 switch (h->root.type)
9573 {
9574 case bfd_link_hash_common:
9575 type = elf_link_convert_common_type (flinfo->info, type);
9576 break;
9577 case bfd_link_hash_defined:
9578 case bfd_link_hash_defweak:
9579 if (bed->common_definition (&sym))
9580 type = elf_link_convert_common_type (flinfo->info, type);
9581 else
9582 type = STT_OBJECT;
9583 break;
9584 case bfd_link_hash_undefined:
9585 case bfd_link_hash_undefweak:
9586 break;
9587 default:
9588 abort ();
9589 }
9590
4deb8f71 9591 if (h->forced_local)
b8871f35
L
9592 {
9593 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9594 /* Turn off visibility on local symbol. */
9595 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9596 }
9597 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9598 else if (h->unique_global && h->def_regular)
9599 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9600 else if (h->root.type == bfd_link_hash_undefweak
9601 || h->root.type == bfd_link_hash_defweak)
9602 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9603 else
9604 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9605 sym.st_target_internal = h->target_internal;
9606
c152c796
AM
9607 /* Give the processor backend a chance to tweak the symbol value,
9608 and also to finish up anything that needs to be done for this
9609 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9610 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9611 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9612 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9613 && h->def_regular
0e1862bb 9614 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9615 || ((h->dynindx != -1
9616 || h->forced_local)
0e1862bb 9617 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9618 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9619 || h->root.type != bfd_link_hash_undefweak))
9620 || !h->forced_local)
8b127cbc 9621 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9622 {
9623 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9624 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9625 {
9626 eoinfo->failed = TRUE;
9627 return FALSE;
9628 }
9629 }
9630
9631 /* If we are marking the symbol as undefined, and there are no
9632 non-weak references to this symbol from a regular object, then
9633 mark the symbol as weak undefined; if there are non-weak
9634 references, mark the symbol as strong. We can't do this earlier,
9635 because it might not be marked as undefined until the
9636 finish_dynamic_symbol routine gets through with it. */
9637 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9638 && h->ref_regular
c152c796
AM
9639 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9640 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9641 {
9642 int bindtype;
b8871f35 9643 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9644
9645 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9646 if (type == STT_GNU_IFUNC)
9647 type = STT_FUNC;
c152c796 9648
f5385ebf 9649 if (h->ref_regular_nonweak)
c152c796
AM
9650 bindtype = STB_GLOBAL;
9651 else
9652 bindtype = STB_WEAK;
2955ec4c 9653 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9654 }
9655
bda987c2
CD
9656 /* If this is a symbol defined in a dynamic library, don't use the
9657 symbol size from the dynamic library. Relinking an executable
9658 against a new library may introduce gratuitous changes in the
9659 executable's symbols if we keep the size. */
9660 if (sym.st_shndx == SHN_UNDEF
9661 && !h->def_regular
9662 && h->def_dynamic)
9663 sym.st_size = 0;
9664
c152c796
AM
9665 /* If a non-weak symbol with non-default visibility is not defined
9666 locally, it is a fatal error. */
0e1862bb 9667 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9668 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9669 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9670 && h->root.type == bfd_link_hash_undefined
f5385ebf 9671 && !h->def_regular)
c152c796 9672 {
17d078c5
AM
9673 const char *msg;
9674
9675 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 9676 /* xgettext:c-format */
17d078c5
AM
9677 msg = _("%B: protected symbol `%s' isn't defined");
9678 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 9679 /* xgettext:c-format */
17d078c5
AM
9680 msg = _("%B: internal symbol `%s' isn't defined");
9681 else
695344c0 9682 /* xgettext:c-format */
17d078c5 9683 msg = _("%B: hidden symbol `%s' isn't defined");
4eca0228 9684 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9685 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9686 eoinfo->failed = TRUE;
9687 return FALSE;
9688 }
9689
9690 /* If this symbol should be put in the .dynsym section, then put it
9691 there now. We already know the symbol index. We also fill in
9692 the entry in the .hash section. */
cae1fbbb 9693 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9694 && h->dynindx != -1
8b127cbc 9695 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9696 {
c152c796
AM
9697 bfd_byte *esym;
9698
90c984fc
L
9699 /* Since there is no version information in the dynamic string,
9700 if there is no version info in symbol version section, we will
1659f720 9701 have a run-time problem if not linking executable, referenced
4deb8f71 9702 by shared library, or not bound locally. */
1659f720 9703 if (h->verinfo.verdef == NULL
0e1862bb 9704 && (!bfd_link_executable (flinfo->info)
1659f720
L
9705 || h->ref_dynamic
9706 || !h->def_regular))
90c984fc
L
9707 {
9708 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9709
9710 if (p && p [1] != '\0')
9711 {
4eca0228 9712 _bfd_error_handler
695344c0 9713 /* xgettext:c-format */
90c984fc
L
9714 (_("%B: No symbol version section for versioned symbol `%s'"),
9715 flinfo->output_bfd, h->root.root.string);
9716 eoinfo->failed = TRUE;
9717 return FALSE;
9718 }
9719 }
9720
c152c796 9721 sym.st_name = h->dynstr_index;
cae1fbbb
L
9722 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9723 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9724 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9725 {
9726 eoinfo->failed = TRUE;
9727 return FALSE;
9728 }
8b127cbc 9729 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9730
8b127cbc 9731 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9732 {
9733 size_t hash_entry_size;
9734 bfd_byte *bucketpos;
9735 bfd_vma chain;
41198d0c
L
9736 size_t bucketcount;
9737 size_t bucket;
9738
8b127cbc 9739 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9740 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9741
9742 hash_entry_size
8b127cbc
AM
9743 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9744 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9745 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9746 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9747 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9748 bucketpos);
9749 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9750 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9751 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9752 }
c152c796 9753
8b127cbc 9754 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9755 {
9756 Elf_Internal_Versym iversym;
9757 Elf_External_Versym *eversym;
9758
f5385ebf 9759 if (!h->def_regular)
c152c796 9760 {
7b20f099
AM
9761 if (h->verinfo.verdef == NULL
9762 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9763 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9764 iversym.vs_vers = 0;
9765 else
9766 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9767 }
9768 else
9769 {
9770 if (h->verinfo.vertree == NULL)
9771 iversym.vs_vers = 1;
9772 else
9773 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9774 if (flinfo->info->create_default_symver)
3e3b46e5 9775 iversym.vs_vers++;
c152c796
AM
9776 }
9777
422f1182 9778 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9779 defined locally. */
422f1182 9780 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9781 iversym.vs_vers |= VERSYM_HIDDEN;
9782
8b127cbc 9783 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9784 eversym += h->dynindx;
8b127cbc 9785 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9786 }
9787 }
9788
d983c8c5
AM
9789 /* If the symbol is undefined, and we didn't output it to .dynsym,
9790 strip it from .symtab too. Obviously we can't do this for
9791 relocatable output or when needed for --emit-relocs. */
9792 else if (input_sec == bfd_und_section_ptr
9793 && h->indx != -2
0e1862bb 9794 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9795 return TRUE;
9796 /* Also strip others that we couldn't earlier due to dynamic symbol
9797 processing. */
9798 if (strip)
9799 return TRUE;
9800 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9801 return TRUE;
9802
2ec55de3
AM
9803 /* Output a FILE symbol so that following locals are not associated
9804 with the wrong input file. We need one for forced local symbols
9805 if we've seen more than one FILE symbol or when we have exactly
9806 one FILE symbol but global symbols are present in a file other
9807 than the one with the FILE symbol. We also need one if linker
9808 defined symbols are present. In practice these conditions are
9809 always met, so just emit the FILE symbol unconditionally. */
9810 if (eoinfo->localsyms
9811 && !eoinfo->file_sym_done
9812 && eoinfo->flinfo->filesym_count != 0)
9813 {
9814 Elf_Internal_Sym fsym;
9815
9816 memset (&fsym, 0, sizeof (fsym));
9817 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9818 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9819 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9820 bfd_und_section_ptr, NULL))
2ec55de3
AM
9821 return FALSE;
9822
9823 eoinfo->file_sym_done = TRUE;
9824 }
9825
8b127cbc 9826 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9827 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9828 input_sec, h);
6e0b88f1 9829 if (ret == 0)
c152c796
AM
9830 {
9831 eoinfo->failed = TRUE;
9832 return FALSE;
9833 }
6e0b88f1
AM
9834 else if (ret == 1)
9835 h->indx = indx;
9836 else if (h->indx == -2)
9837 abort();
c152c796
AM
9838
9839 return TRUE;
9840}
9841
cdd3575c
AM
9842/* Return TRUE if special handling is done for relocs in SEC against
9843 symbols defined in discarded sections. */
9844
c152c796
AM
9845static bfd_boolean
9846elf_section_ignore_discarded_relocs (asection *sec)
9847{
9848 const struct elf_backend_data *bed;
9849
cdd3575c
AM
9850 switch (sec->sec_info_type)
9851 {
dbaa2011
AM
9852 case SEC_INFO_TYPE_STABS:
9853 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9854 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9855 return TRUE;
9856 default:
9857 break;
9858 }
c152c796
AM
9859
9860 bed = get_elf_backend_data (sec->owner);
9861 if (bed->elf_backend_ignore_discarded_relocs != NULL
9862 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9863 return TRUE;
9864
9865 return FALSE;
9866}
9867
9e66c942
AM
9868/* Return a mask saying how ld should treat relocations in SEC against
9869 symbols defined in discarded sections. If this function returns
9870 COMPLAIN set, ld will issue a warning message. If this function
9871 returns PRETEND set, and the discarded section was link-once and the
9872 same size as the kept link-once section, ld will pretend that the
9873 symbol was actually defined in the kept section. Otherwise ld will
9874 zero the reloc (at least that is the intent, but some cooperation by
9875 the target dependent code is needed, particularly for REL targets). */
9876
8a696751
AM
9877unsigned int
9878_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9879{
9e66c942 9880 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9881 return PRETEND;
cdd3575c
AM
9882
9883 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9884 return 0;
cdd3575c
AM
9885
9886 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9887 return 0;
cdd3575c 9888
9e66c942 9889 return COMPLAIN | PRETEND;
cdd3575c
AM
9890}
9891
3d7f7666
L
9892/* Find a match between a section and a member of a section group. */
9893
9894static asection *
c0f00686
L
9895match_group_member (asection *sec, asection *group,
9896 struct bfd_link_info *info)
3d7f7666
L
9897{
9898 asection *first = elf_next_in_group (group);
9899 asection *s = first;
9900
9901 while (s != NULL)
9902 {
c0f00686 9903 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9904 return s;
9905
83180ade 9906 s = elf_next_in_group (s);
3d7f7666
L
9907 if (s == first)
9908 break;
9909 }
9910
9911 return NULL;
9912}
9913
01b3c8ab 9914/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9915 to replace it. Return the replacement if it is OK. Otherwise return
9916 NULL. */
01b3c8ab
L
9917
9918asection *
c0f00686 9919_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9920{
9921 asection *kept;
9922
9923 kept = sec->kept_section;
9924 if (kept != NULL)
9925 {
c2370991 9926 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9927 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9928 if (kept != NULL
9929 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9930 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9931 kept = NULL;
c2370991 9932 sec->kept_section = kept;
01b3c8ab
L
9933 }
9934 return kept;
9935}
9936
c152c796
AM
9937/* Link an input file into the linker output file. This function
9938 handles all the sections and relocations of the input file at once.
9939 This is so that we only have to read the local symbols once, and
9940 don't have to keep them in memory. */
9941
9942static bfd_boolean
8b127cbc 9943elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9944{
ece5ef60 9945 int (*relocate_section)
c152c796
AM
9946 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9947 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9948 bfd *output_bfd;
9949 Elf_Internal_Shdr *symtab_hdr;
9950 size_t locsymcount;
9951 size_t extsymoff;
9952 Elf_Internal_Sym *isymbuf;
9953 Elf_Internal_Sym *isym;
9954 Elf_Internal_Sym *isymend;
9955 long *pindex;
9956 asection **ppsection;
9957 asection *o;
9958 const struct elf_backend_data *bed;
c152c796 9959 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9960 bfd_size_type address_size;
9961 bfd_vma r_type_mask;
9962 int r_sym_shift;
ffbc01cc 9963 bfd_boolean have_file_sym = FALSE;
c152c796 9964
8b127cbc 9965 output_bfd = flinfo->output_bfd;
c152c796
AM
9966 bed = get_elf_backend_data (output_bfd);
9967 relocate_section = bed->elf_backend_relocate_section;
9968
9969 /* If this is a dynamic object, we don't want to do anything here:
9970 we don't want the local symbols, and we don't want the section
9971 contents. */
9972 if ((input_bfd->flags & DYNAMIC) != 0)
9973 return TRUE;
9974
c152c796
AM
9975 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9976 if (elf_bad_symtab (input_bfd))
9977 {
9978 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9979 extsymoff = 0;
9980 }
9981 else
9982 {
9983 locsymcount = symtab_hdr->sh_info;
9984 extsymoff = symtab_hdr->sh_info;
9985 }
9986
9987 /* Read the local symbols. */
9988 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9989 if (isymbuf == NULL && locsymcount != 0)
9990 {
9991 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9992 flinfo->internal_syms,
9993 flinfo->external_syms,
9994 flinfo->locsym_shndx);
c152c796
AM
9995 if (isymbuf == NULL)
9996 return FALSE;
9997 }
9998
9999 /* Find local symbol sections and adjust values of symbols in
10000 SEC_MERGE sections. Write out those local symbols we know are
10001 going into the output file. */
10002 isymend = isymbuf + locsymcount;
8b127cbc 10003 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10004 isym < isymend;
10005 isym++, pindex++, ppsection++)
10006 {
10007 asection *isec;
10008 const char *name;
10009 Elf_Internal_Sym osym;
6e0b88f1
AM
10010 long indx;
10011 int ret;
c152c796
AM
10012
10013 *pindex = -1;
10014
10015 if (elf_bad_symtab (input_bfd))
10016 {
10017 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10018 {
10019 *ppsection = NULL;
10020 continue;
10021 }
10022 }
10023
10024 if (isym->st_shndx == SHN_UNDEF)
10025 isec = bfd_und_section_ptr;
c152c796
AM
10026 else if (isym->st_shndx == SHN_ABS)
10027 isec = bfd_abs_section_ptr;
10028 else if (isym->st_shndx == SHN_COMMON)
10029 isec = bfd_com_section_ptr;
10030 else
10031 {
cb33740c
AM
10032 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10033 if (isec == NULL)
10034 {
10035 /* Don't attempt to output symbols with st_shnx in the
10036 reserved range other than SHN_ABS and SHN_COMMON. */
10037 *ppsection = NULL;
10038 continue;
10039 }
dbaa2011 10040 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10041 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10042 isym->st_value =
10043 _bfd_merged_section_offset (output_bfd, &isec,
10044 elf_section_data (isec)->sec_info,
10045 isym->st_value);
c152c796
AM
10046 }
10047
10048 *ppsection = isec;
10049
d983c8c5
AM
10050 /* Don't output the first, undefined, symbol. In fact, don't
10051 output any undefined local symbol. */
10052 if (isec == bfd_und_section_ptr)
c152c796
AM
10053 continue;
10054
10055 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10056 {
10057 /* We never output section symbols. Instead, we use the
10058 section symbol of the corresponding section in the output
10059 file. */
10060 continue;
10061 }
10062
10063 /* If we are stripping all symbols, we don't want to output this
10064 one. */
8b127cbc 10065 if (flinfo->info->strip == strip_all)
c152c796
AM
10066 continue;
10067
10068 /* If we are discarding all local symbols, we don't want to
10069 output this one. If we are generating a relocatable output
10070 file, then some of the local symbols may be required by
10071 relocs; we output them below as we discover that they are
10072 needed. */
8b127cbc 10073 if (flinfo->info->discard == discard_all)
c152c796
AM
10074 continue;
10075
10076 /* If this symbol is defined in a section which we are
f02571c5
AM
10077 discarding, we don't need to keep it. */
10078 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10079 && isym->st_shndx < SHN_LORESERVE
10080 && bfd_section_removed_from_list (output_bfd,
10081 isec->output_section))
e75a280b
L
10082 continue;
10083
c152c796
AM
10084 /* Get the name of the symbol. */
10085 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10086 isym->st_name);
10087 if (name == NULL)
10088 return FALSE;
10089
10090 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10091 if ((flinfo->info->strip == strip_some
10092 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10093 == NULL))
8b127cbc 10094 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10095 && (isec->flags & SEC_MERGE)
10096 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10097 || flinfo->info->discard == discard_l)
c152c796
AM
10098 && bfd_is_local_label_name (input_bfd, name)))
10099 continue;
10100
ffbc01cc
AM
10101 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10102 {
ce875075
AM
10103 if (input_bfd->lto_output)
10104 /* -flto puts a temp file name here. This means builds
10105 are not reproducible. Discard the symbol. */
10106 continue;
ffbc01cc
AM
10107 have_file_sym = TRUE;
10108 flinfo->filesym_count += 1;
10109 }
10110 if (!have_file_sym)
10111 {
10112 /* In the absence of debug info, bfd_find_nearest_line uses
10113 FILE symbols to determine the source file for local
10114 function symbols. Provide a FILE symbol here if input
10115 files lack such, so that their symbols won't be
10116 associated with a previous input file. It's not the
10117 source file, but the best we can do. */
10118 have_file_sym = TRUE;
10119 flinfo->filesym_count += 1;
10120 memset (&osym, 0, sizeof (osym));
10121 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10122 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10123 if (!elf_link_output_symstrtab (flinfo,
10124 (input_bfd->lto_output ? NULL
10125 : input_bfd->filename),
10126 &osym, bfd_abs_section_ptr,
10127 NULL))
ffbc01cc
AM
10128 return FALSE;
10129 }
10130
c152c796
AM
10131 osym = *isym;
10132
10133 /* Adjust the section index for the output file. */
10134 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10135 isec->output_section);
10136 if (osym.st_shndx == SHN_BAD)
10137 return FALSE;
10138
c152c796
AM
10139 /* ELF symbols in relocatable files are section relative, but
10140 in executable files they are virtual addresses. Note that
10141 this code assumes that all ELF sections have an associated
10142 BFD section with a reasonable value for output_offset; below
10143 we assume that they also have a reasonable value for
10144 output_section. Any special sections must be set up to meet
10145 these requirements. */
10146 osym.st_value += isec->output_offset;
0e1862bb 10147 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10148 {
10149 osym.st_value += isec->output_section->vma;
10150 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10151 {
10152 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
10153 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10154 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
10155 }
10156 }
10157
6e0b88f1 10158 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10159 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10160 if (ret == 0)
c152c796 10161 return FALSE;
6e0b88f1
AM
10162 else if (ret == 1)
10163 *pindex = indx;
c152c796
AM
10164 }
10165
310fd250
L
10166 if (bed->s->arch_size == 32)
10167 {
10168 r_type_mask = 0xff;
10169 r_sym_shift = 8;
10170 address_size = 4;
10171 }
10172 else
10173 {
10174 r_type_mask = 0xffffffff;
10175 r_sym_shift = 32;
10176 address_size = 8;
10177 }
10178
c152c796
AM
10179 /* Relocate the contents of each section. */
10180 sym_hashes = elf_sym_hashes (input_bfd);
10181 for (o = input_bfd->sections; o != NULL; o = o->next)
10182 {
10183 bfd_byte *contents;
10184
10185 if (! o->linker_mark)
10186 {
10187 /* This section was omitted from the link. */
10188 continue;
10189 }
10190
0e1862bb 10191 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
10192 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10193 {
10194 /* Deal with the group signature symbol. */
10195 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10196 unsigned long symndx = sec_data->this_hdr.sh_info;
10197 asection *osec = o->output_section;
10198
10199 if (symndx >= locsymcount
10200 || (elf_bad_symtab (input_bfd)
8b127cbc 10201 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
10202 {
10203 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10204 while (h->root.type == bfd_link_hash_indirect
10205 || h->root.type == bfd_link_hash_warning)
10206 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10207 /* Arrange for symbol to be output. */
10208 h->indx = -2;
10209 elf_section_data (osec)->this_hdr.sh_info = -2;
10210 }
10211 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10212 {
10213 /* We'll use the output section target_index. */
8b127cbc 10214 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
10215 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10216 }
10217 else
10218 {
8b127cbc 10219 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
10220 {
10221 /* Otherwise output the local symbol now. */
10222 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 10223 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10224 const char *name;
6e0b88f1
AM
10225 long indx;
10226 int ret;
bcacc0f5
AM
10227
10228 name = bfd_elf_string_from_elf_section (input_bfd,
10229 symtab_hdr->sh_link,
10230 sym.st_name);
10231 if (name == NULL)
10232 return FALSE;
10233
10234 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10235 sec);
10236 if (sym.st_shndx == SHN_BAD)
10237 return FALSE;
10238
10239 sym.st_value += o->output_offset;
10240
6e0b88f1 10241 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10242 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10243 NULL);
6e0b88f1 10244 if (ret == 0)
bcacc0f5 10245 return FALSE;
6e0b88f1 10246 else if (ret == 1)
8b127cbc 10247 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10248 else
10249 abort ();
bcacc0f5
AM
10250 }
10251 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10252 = flinfo->indices[symndx];
bcacc0f5
AM
10253 }
10254 }
10255
c152c796 10256 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10257 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10258 continue;
10259
10260 if ((o->flags & SEC_LINKER_CREATED) != 0)
10261 {
10262 /* Section was created by _bfd_elf_link_create_dynamic_sections
10263 or somesuch. */
10264 continue;
10265 }
10266
10267 /* Get the contents of the section. They have been cached by a
10268 relaxation routine. Note that o is a section in an input
10269 file, so the contents field will not have been set by any of
10270 the routines which work on output files. */
10271 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10272 {
10273 contents = elf_section_data (o)->this_hdr.contents;
10274 if (bed->caches_rawsize
10275 && o->rawsize != 0
10276 && o->rawsize < o->size)
10277 {
10278 memcpy (flinfo->contents, contents, o->rawsize);
10279 contents = flinfo->contents;
10280 }
10281 }
c152c796
AM
10282 else
10283 {
8b127cbc 10284 contents = flinfo->contents;
4a114e3e 10285 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10286 return FALSE;
10287 }
10288
10289 if ((o->flags & SEC_RELOC) != 0)
10290 {
10291 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10292 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10293 int action_discarded;
ece5ef60 10294 int ret;
c152c796
AM
10295
10296 /* Get the swapped relocs. */
10297 internal_relocs
8b127cbc
AM
10298 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10299 flinfo->internal_relocs, FALSE);
c152c796
AM
10300 if (internal_relocs == NULL
10301 && o->reloc_count > 0)
10302 return FALSE;
10303
310fd250
L
10304 /* We need to reverse-copy input .ctors/.dtors sections if
10305 they are placed in .init_array/.finit_array for output. */
10306 if (o->size > address_size
10307 && ((strncmp (o->name, ".ctors", 6) == 0
10308 && strcmp (o->output_section->name,
10309 ".init_array") == 0)
10310 || (strncmp (o->name, ".dtors", 6) == 0
10311 && strcmp (o->output_section->name,
10312 ".fini_array") == 0))
10313 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10314 {
310fd250
L
10315 if (o->size != o->reloc_count * address_size)
10316 {
4eca0228 10317 _bfd_error_handler
695344c0 10318 /* xgettext:c-format */
310fd250
L
10319 (_("error: %B: size of section %A is not "
10320 "multiple of address size"),
10321 input_bfd, o);
10322 bfd_set_error (bfd_error_on_input);
10323 return FALSE;
10324 }
10325 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10326 }
10327
0f02bbd9 10328 action_discarded = -1;
c152c796 10329 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10330 action_discarded = (*bed->action_discarded) (o);
10331
10332 /* Run through the relocs evaluating complex reloc symbols and
10333 looking for relocs against symbols from discarded sections
10334 or section symbols from removed link-once sections.
10335 Complain about relocs against discarded sections. Zero
10336 relocs against removed link-once sections. */
10337
10338 rel = internal_relocs;
10339 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10340 for ( ; rel < relend; rel++)
c152c796 10341 {
0f02bbd9
AM
10342 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10343 unsigned int s_type;
10344 asection **ps, *sec;
10345 struct elf_link_hash_entry *h = NULL;
10346 const char *sym_name;
c152c796 10347
0f02bbd9
AM
10348 if (r_symndx == STN_UNDEF)
10349 continue;
c152c796 10350
0f02bbd9
AM
10351 if (r_symndx >= locsymcount
10352 || (elf_bad_symtab (input_bfd)
8b127cbc 10353 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10354 {
10355 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10356
0f02bbd9
AM
10357 /* Badly formatted input files can contain relocs that
10358 reference non-existant symbols. Check here so that
10359 we do not seg fault. */
10360 if (h == NULL)
c152c796 10361 {
0f02bbd9 10362 char buffer [32];
dce669a1 10363
0f02bbd9 10364 sprintf_vma (buffer, rel->r_info);
4eca0228 10365 _bfd_error_handler
695344c0 10366 /* xgettext:c-format */
0f02bbd9
AM
10367 (_("error: %B contains a reloc (0x%s) for section %A "
10368 "that references a non-existent global symbol"),
10369 input_bfd, o, buffer);
10370 bfd_set_error (bfd_error_bad_value);
10371 return FALSE;
10372 }
3b36f7e6 10373
0f02bbd9
AM
10374 while (h->root.type == bfd_link_hash_indirect
10375 || h->root.type == bfd_link_hash_warning)
10376 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10377
0f02bbd9 10378 s_type = h->type;
cdd3575c 10379
9e2dec47 10380 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10381 mark the symbol as undefined. Note that the
10382 linker may attach linker created dynamic sections
10383 to the plugin bfd. Symbols defined in linker
10384 created sections are not plugin symbols. */
9e2dec47
L
10385 if (h->root.non_ir_ref
10386 && (h->root.type == bfd_link_hash_defined
10387 || h->root.type == bfd_link_hash_defweak)
10388 && (h->root.u.def.section->flags
10389 & SEC_LINKER_CREATED) == 0
10390 && h->root.u.def.section->owner != NULL
10391 && (h->root.u.def.section->owner->flags
10392 & BFD_PLUGIN) != 0)
10393 {
10394 h->root.type = bfd_link_hash_undefined;
10395 h->root.u.undef.abfd = h->root.u.def.section->owner;
10396 }
10397
0f02bbd9
AM
10398 ps = NULL;
10399 if (h->root.type == bfd_link_hash_defined
10400 || h->root.type == bfd_link_hash_defweak)
10401 ps = &h->root.u.def.section;
10402
10403 sym_name = h->root.root.string;
10404 }
10405 else
10406 {
10407 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10408
10409 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10410 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10411 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10412 sym, *ps);
10413 }
c152c796 10414
c301e700 10415 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10416 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10417 {
10418 bfd_vma val;
10419 bfd_vma dot = (rel->r_offset
10420 + o->output_offset + o->output_section->vma);
10421#ifdef DEBUG
10422 printf ("Encountered a complex symbol!");
10423 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10424 input_bfd->filename, o->name,
10425 (long) (rel - internal_relocs));
0f02bbd9
AM
10426 printf (" symbol: idx %8.8lx, name %s\n",
10427 r_symndx, sym_name);
10428 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10429 (unsigned long) rel->r_info,
10430 (unsigned long) rel->r_offset);
10431#endif
8b127cbc 10432 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10433 isymbuf, locsymcount, s_type == STT_SRELC))
10434 return FALSE;
10435
10436 /* Symbol evaluated OK. Update to absolute value. */
10437 set_symbol_value (input_bfd, isymbuf, locsymcount,
10438 r_symndx, val);
10439 continue;
10440 }
10441
10442 if (action_discarded != -1 && ps != NULL)
10443 {
cdd3575c
AM
10444 /* Complain if the definition comes from a
10445 discarded section. */
dbaa2011 10446 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10447 {
cf35638d 10448 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10449 if (action_discarded & COMPLAIN)
8b127cbc 10450 (*flinfo->info->callbacks->einfo)
695344c0 10451 /* xgettext:c-format */
e1fffbe6 10452 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10453 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10454 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10455
87e5235d 10456 /* Try to do the best we can to support buggy old
e0ae6d6f 10457 versions of gcc. Pretend that the symbol is
87e5235d
AM
10458 really defined in the kept linkonce section.
10459 FIXME: This is quite broken. Modifying the
10460 symbol here means we will be changing all later
e0ae6d6f 10461 uses of the symbol, not just in this section. */
0f02bbd9 10462 if (action_discarded & PRETEND)
87e5235d 10463 {
01b3c8ab
L
10464 asection *kept;
10465
c0f00686 10466 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10467 flinfo->info);
01b3c8ab 10468 if (kept != NULL)
87e5235d
AM
10469 {
10470 *ps = kept;
10471 continue;
10472 }
10473 }
c152c796
AM
10474 }
10475 }
10476 }
10477
10478 /* Relocate the section by invoking a back end routine.
10479
10480 The back end routine is responsible for adjusting the
10481 section contents as necessary, and (if using Rela relocs
10482 and generating a relocatable output file) adjusting the
10483 reloc addend as necessary.
10484
10485 The back end routine does not have to worry about setting
10486 the reloc address or the reloc symbol index.
10487
10488 The back end routine is given a pointer to the swapped in
10489 internal symbols, and can access the hash table entries
10490 for the external symbols via elf_sym_hashes (input_bfd).
10491
10492 When generating relocatable output, the back end routine
10493 must handle STB_LOCAL/STT_SECTION symbols specially. The
10494 output symbol is going to be a section symbol
10495 corresponding to the output section, which will require
10496 the addend to be adjusted. */
10497
8b127cbc 10498 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10499 input_bfd, o, contents,
10500 internal_relocs,
10501 isymbuf,
8b127cbc 10502 flinfo->sections);
ece5ef60 10503 if (!ret)
c152c796
AM
10504 return FALSE;
10505
ece5ef60 10506 if (ret == 2
0e1862bb 10507 || bfd_link_relocatable (flinfo->info)
8b127cbc 10508 || flinfo->info->emitrelocations)
c152c796
AM
10509 {
10510 Elf_Internal_Rela *irela;
d4730f92 10511 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10512 bfd_vma last_offset;
10513 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10514 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10515 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10516 unsigned int next_erel;
c152c796 10517 bfd_boolean rela_normal;
d4730f92 10518 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10519
d4730f92
BS
10520 esdi = elf_section_data (o);
10521 esdo = elf_section_data (o->output_section);
10522 rela_normal = FALSE;
c152c796
AM
10523
10524 /* Adjust the reloc addresses and symbol indices. */
10525
10526 irela = internal_relocs;
10527 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10528 rel_hash = esdo->rel.hashes + esdo->rel.count;
10529 /* We start processing the REL relocs, if any. When we reach
10530 IRELAMID in the loop, we switch to the RELA relocs. */
10531 irelamid = irela;
10532 if (esdi->rel.hdr != NULL)
10533 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10534 * bed->s->int_rels_per_ext_rel);
eac338cf 10535 rel_hash_list = rel_hash;
d4730f92 10536 rela_hash_list = NULL;
c152c796 10537 last_offset = o->output_offset;
0e1862bb 10538 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10539 last_offset += o->output_section->vma;
10540 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10541 {
10542 unsigned long r_symndx;
10543 asection *sec;
10544 Elf_Internal_Sym sym;
10545
10546 if (next_erel == bed->s->int_rels_per_ext_rel)
10547 {
10548 rel_hash++;
10549 next_erel = 0;
10550 }
10551
d4730f92
BS
10552 if (irela == irelamid)
10553 {
10554 rel_hash = esdo->rela.hashes + esdo->rela.count;
10555 rela_hash_list = rel_hash;
10556 rela_normal = bed->rela_normal;
10557 }
10558
c152c796 10559 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10560 flinfo->info, o,
c152c796
AM
10561 irela->r_offset);
10562 if (irela->r_offset >= (bfd_vma) -2)
10563 {
10564 /* This is a reloc for a deleted entry or somesuch.
10565 Turn it into an R_*_NONE reloc, at the same
10566 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10567 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10568 being ordered. */
10569 irela->r_offset = last_offset;
10570 irela->r_info = 0;
10571 irela->r_addend = 0;
10572 continue;
10573 }
10574
10575 irela->r_offset += o->output_offset;
10576
10577 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10578 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10579 irela->r_offset += o->output_section->vma;
10580
10581 last_offset = irela->r_offset;
10582
10583 r_symndx = irela->r_info >> r_sym_shift;
10584 if (r_symndx == STN_UNDEF)
10585 continue;
10586
10587 if (r_symndx >= locsymcount
10588 || (elf_bad_symtab (input_bfd)
8b127cbc 10589 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10590 {
10591 struct elf_link_hash_entry *rh;
10592 unsigned long indx;
10593
10594 /* This is a reloc against a global symbol. We
10595 have not yet output all the local symbols, so
10596 we do not know the symbol index of any global
10597 symbol. We set the rel_hash entry for this
10598 reloc to point to the global hash table entry
10599 for this symbol. The symbol index is then
ee75fd95 10600 set at the end of bfd_elf_final_link. */
c152c796
AM
10601 indx = r_symndx - extsymoff;
10602 rh = elf_sym_hashes (input_bfd)[indx];
10603 while (rh->root.type == bfd_link_hash_indirect
10604 || rh->root.type == bfd_link_hash_warning)
10605 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10606
10607 /* Setting the index to -2 tells
10608 elf_link_output_extsym that this symbol is
10609 used by a reloc. */
10610 BFD_ASSERT (rh->indx < 0);
10611 rh->indx = -2;
10612
10613 *rel_hash = rh;
10614
10615 continue;
10616 }
10617
10618 /* This is a reloc against a local symbol. */
10619
10620 *rel_hash = NULL;
10621 sym = isymbuf[r_symndx];
8b127cbc 10622 sec = flinfo->sections[r_symndx];
c152c796
AM
10623 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10624 {
10625 /* I suppose the backend ought to fill in the
10626 section of any STT_SECTION symbol against a
6a8d1586 10627 processor specific section. */
cf35638d 10628 r_symndx = STN_UNDEF;
6a8d1586
AM
10629 if (bfd_is_abs_section (sec))
10630 ;
c152c796
AM
10631 else if (sec == NULL || sec->owner == NULL)
10632 {
10633 bfd_set_error (bfd_error_bad_value);
10634 return FALSE;
10635 }
10636 else
10637 {
6a8d1586
AM
10638 asection *osec = sec->output_section;
10639
10640 /* If we have discarded a section, the output
10641 section will be the absolute section. In
ab96bf03
AM
10642 case of discarded SEC_MERGE sections, use
10643 the kept section. relocate_section should
10644 have already handled discarded linkonce
10645 sections. */
6a8d1586
AM
10646 if (bfd_is_abs_section (osec)
10647 && sec->kept_section != NULL
10648 && sec->kept_section->output_section != NULL)
10649 {
10650 osec = sec->kept_section->output_section;
10651 irela->r_addend -= osec->vma;
10652 }
10653
10654 if (!bfd_is_abs_section (osec))
10655 {
10656 r_symndx = osec->target_index;
cf35638d 10657 if (r_symndx == STN_UNDEF)
74541ad4 10658 {
051d833a
AM
10659 irela->r_addend += osec->vma;
10660 osec = _bfd_nearby_section (output_bfd, osec,
10661 osec->vma);
10662 irela->r_addend -= osec->vma;
10663 r_symndx = osec->target_index;
74541ad4 10664 }
6a8d1586 10665 }
c152c796
AM
10666 }
10667
10668 /* Adjust the addend according to where the
10669 section winds up in the output section. */
10670 if (rela_normal)
10671 irela->r_addend += sec->output_offset;
10672 }
10673 else
10674 {
8b127cbc 10675 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10676 {
10677 unsigned long shlink;
10678 const char *name;
10679 asection *osec;
6e0b88f1 10680 long indx;
c152c796 10681
8b127cbc 10682 if (flinfo->info->strip == strip_all)
c152c796
AM
10683 {
10684 /* You can't do ld -r -s. */
10685 bfd_set_error (bfd_error_invalid_operation);
10686 return FALSE;
10687 }
10688
10689 /* This symbol was skipped earlier, but
10690 since it is needed by a reloc, we
10691 must output it now. */
10692 shlink = symtab_hdr->sh_link;
10693 name = (bfd_elf_string_from_elf_section
10694 (input_bfd, shlink, sym.st_name));
10695 if (name == NULL)
10696 return FALSE;
10697
10698 osec = sec->output_section;
10699 sym.st_shndx =
10700 _bfd_elf_section_from_bfd_section (output_bfd,
10701 osec);
10702 if (sym.st_shndx == SHN_BAD)
10703 return FALSE;
10704
10705 sym.st_value += sec->output_offset;
0e1862bb 10706 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10707 {
10708 sym.st_value += osec->vma;
10709 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10710 {
10711 /* STT_TLS symbols are relative to PT_TLS
10712 segment base. */
8b127cbc 10713 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10714 ->tls_sec != NULL);
8b127cbc 10715 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10716 ->tls_sec->vma);
10717 }
10718 }
10719
6e0b88f1 10720 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10721 ret = elf_link_output_symstrtab (flinfo, name,
10722 &sym, sec,
10723 NULL);
6e0b88f1 10724 if (ret == 0)
c152c796 10725 return FALSE;
6e0b88f1 10726 else if (ret == 1)
8b127cbc 10727 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10728 else
10729 abort ();
c152c796
AM
10730 }
10731
8b127cbc 10732 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10733 }
10734
10735 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10736 | (irela->r_info & r_type_mask));
10737 }
10738
10739 /* Swap out the relocs. */
d4730f92
BS
10740 input_rel_hdr = esdi->rel.hdr;
10741 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10742 {
d4730f92
BS
10743 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10744 input_rel_hdr,
10745 internal_relocs,
10746 rel_hash_list))
10747 return FALSE;
c152c796
AM
10748 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10749 * bed->s->int_rels_per_ext_rel);
eac338cf 10750 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10751 }
10752
10753 input_rela_hdr = esdi->rela.hdr;
10754 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10755 {
eac338cf 10756 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10757 input_rela_hdr,
eac338cf 10758 internal_relocs,
d4730f92 10759 rela_hash_list))
c152c796
AM
10760 return FALSE;
10761 }
10762 }
10763 }
10764
10765 /* Write out the modified section contents. */
10766 if (bed->elf_backend_write_section
8b127cbc 10767 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10768 contents))
c152c796
AM
10769 {
10770 /* Section written out. */
10771 }
10772 else switch (o->sec_info_type)
10773 {
dbaa2011 10774 case SEC_INFO_TYPE_STABS:
c152c796
AM
10775 if (! (_bfd_write_section_stabs
10776 (output_bfd,
8b127cbc 10777 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10778 o, &elf_section_data (o)->sec_info, contents)))
10779 return FALSE;
10780 break;
dbaa2011 10781 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10782 if (! _bfd_write_merged_section (output_bfd, o,
10783 elf_section_data (o)->sec_info))
10784 return FALSE;
10785 break;
dbaa2011 10786 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10787 {
8b127cbc 10788 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10789 o, contents))
10790 return FALSE;
10791 }
10792 break;
2f0c68f2
CM
10793 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10794 {
10795 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10796 flinfo->info,
10797 o, contents))
10798 return FALSE;
10799 }
10800 break;
c152c796
AM
10801 default:
10802 {
310fd250
L
10803 if (! (o->flags & SEC_EXCLUDE))
10804 {
10805 file_ptr offset = (file_ptr) o->output_offset;
10806 bfd_size_type todo = o->size;
37b01f6a
DG
10807
10808 offset *= bfd_octets_per_byte (output_bfd);
10809
310fd250
L
10810 if ((o->flags & SEC_ELF_REVERSE_COPY))
10811 {
10812 /* Reverse-copy input section to output. */
10813 do
10814 {
10815 todo -= address_size;
10816 if (! bfd_set_section_contents (output_bfd,
10817 o->output_section,
10818 contents + todo,
10819 offset,
10820 address_size))
10821 return FALSE;
10822 if (todo == 0)
10823 break;
10824 offset += address_size;
10825 }
10826 while (1);
10827 }
10828 else if (! bfd_set_section_contents (output_bfd,
10829 o->output_section,
10830 contents,
10831 offset, todo))
10832 return FALSE;
10833 }
c152c796
AM
10834 }
10835 break;
10836 }
10837 }
10838
10839 return TRUE;
10840}
10841
10842/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10843 requested by the linker, and does not come from any input file. This
c152c796
AM
10844 is used to build constructor and destructor tables when linking
10845 with -Ur. */
10846
10847static bfd_boolean
10848elf_reloc_link_order (bfd *output_bfd,
10849 struct bfd_link_info *info,
10850 asection *output_section,
10851 struct bfd_link_order *link_order)
10852{
10853 reloc_howto_type *howto;
10854 long indx;
10855 bfd_vma offset;
10856 bfd_vma addend;
d4730f92 10857 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10858 struct elf_link_hash_entry **rel_hash_ptr;
10859 Elf_Internal_Shdr *rel_hdr;
10860 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10861 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10862 bfd_byte *erel;
10863 unsigned int i;
d4730f92 10864 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10865
10866 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10867 if (howto == NULL)
10868 {
10869 bfd_set_error (bfd_error_bad_value);
10870 return FALSE;
10871 }
10872
10873 addend = link_order->u.reloc.p->addend;
10874
d4730f92
BS
10875 if (esdo->rel.hdr)
10876 reldata = &esdo->rel;
10877 else if (esdo->rela.hdr)
10878 reldata = &esdo->rela;
10879 else
10880 {
10881 reldata = NULL;
10882 BFD_ASSERT (0);
10883 }
10884
c152c796 10885 /* Figure out the symbol index. */
d4730f92 10886 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10887 if (link_order->type == bfd_section_reloc_link_order)
10888 {
10889 indx = link_order->u.reloc.p->u.section->target_index;
10890 BFD_ASSERT (indx != 0);
10891 *rel_hash_ptr = NULL;
10892 }
10893 else
10894 {
10895 struct elf_link_hash_entry *h;
10896
10897 /* Treat a reloc against a defined symbol as though it were
10898 actually against the section. */
10899 h = ((struct elf_link_hash_entry *)
10900 bfd_wrapped_link_hash_lookup (output_bfd, info,
10901 link_order->u.reloc.p->u.name,
10902 FALSE, FALSE, TRUE));
10903 if (h != NULL
10904 && (h->root.type == bfd_link_hash_defined
10905 || h->root.type == bfd_link_hash_defweak))
10906 {
10907 asection *section;
10908
10909 section = h->root.u.def.section;
10910 indx = section->output_section->target_index;
10911 *rel_hash_ptr = NULL;
10912 /* It seems that we ought to add the symbol value to the
10913 addend here, but in practice it has already been added
10914 because it was passed to constructor_callback. */
10915 addend += section->output_section->vma + section->output_offset;
10916 }
10917 else if (h != NULL)
10918 {
10919 /* Setting the index to -2 tells elf_link_output_extsym that
10920 this symbol is used by a reloc. */
10921 h->indx = -2;
10922 *rel_hash_ptr = h;
10923 indx = 0;
10924 }
10925 else
10926 {
1a72702b
AM
10927 (*info->callbacks->unattached_reloc)
10928 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
10929 indx = 0;
10930 }
10931 }
10932
10933 /* If this is an inplace reloc, we must write the addend into the
10934 object file. */
10935 if (howto->partial_inplace && addend != 0)
10936 {
10937 bfd_size_type size;
10938 bfd_reloc_status_type rstat;
10939 bfd_byte *buf;
10940 bfd_boolean ok;
10941 const char *sym_name;
10942
a50b1753
NC
10943 size = (bfd_size_type) bfd_get_reloc_size (howto);
10944 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10945 if (buf == NULL && size != 0)
c152c796
AM
10946 return FALSE;
10947 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10948 switch (rstat)
10949 {
10950 case bfd_reloc_ok:
10951 break;
10952
10953 default:
10954 case bfd_reloc_outofrange:
10955 abort ();
10956
10957 case bfd_reloc_overflow:
10958 if (link_order->type == bfd_section_reloc_link_order)
10959 sym_name = bfd_section_name (output_bfd,
10960 link_order->u.reloc.p->u.section);
10961 else
10962 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
10963 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10964 howto->name, addend, NULL, NULL,
10965 (bfd_vma) 0);
c152c796
AM
10966 break;
10967 }
37b01f6a 10968
c152c796 10969 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10970 link_order->offset
10971 * bfd_octets_per_byte (output_bfd),
10972 size);
c152c796
AM
10973 free (buf);
10974 if (! ok)
10975 return FALSE;
10976 }
10977
10978 /* The address of a reloc is relative to the section in a
10979 relocatable file, and is a virtual address in an executable
10980 file. */
10981 offset = link_order->offset;
0e1862bb 10982 if (! bfd_link_relocatable (info))
c152c796
AM
10983 offset += output_section->vma;
10984
10985 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10986 {
10987 irel[i].r_offset = offset;
10988 irel[i].r_info = 0;
10989 irel[i].r_addend = 0;
10990 }
10991 if (bed->s->arch_size == 32)
10992 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10993 else
10994 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10995
d4730f92 10996 rel_hdr = reldata->hdr;
c152c796
AM
10997 erel = rel_hdr->contents;
10998 if (rel_hdr->sh_type == SHT_REL)
10999 {
d4730f92 11000 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11001 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11002 }
11003 else
11004 {
11005 irel[0].r_addend = addend;
d4730f92 11006 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11007 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11008 }
11009
d4730f92 11010 ++reldata->count;
c152c796
AM
11011
11012 return TRUE;
11013}
11014
0b52efa6
PB
11015
11016/* Get the output vma of the section pointed to by the sh_link field. */
11017
11018static bfd_vma
11019elf_get_linked_section_vma (struct bfd_link_order *p)
11020{
11021 Elf_Internal_Shdr **elf_shdrp;
11022 asection *s;
11023 int elfsec;
11024
11025 s = p->u.indirect.section;
11026 elf_shdrp = elf_elfsections (s->owner);
11027 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11028 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
11029 /* PR 290:
11030 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 11031 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
11032 sh_info fields. Hence we could get the situation
11033 where elfsec is 0. */
11034 if (elfsec == 0)
11035 {
11036 const struct elf_backend_data *bed
11037 = get_elf_backend_data (s->owner);
11038 if (bed->link_order_error_handler)
d003868e 11039 bed->link_order_error_handler
695344c0 11040 /* xgettext:c-format */
d003868e 11041 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
11042 return 0;
11043 }
11044 else
11045 {
11046 s = elf_shdrp[elfsec]->bfd_section;
11047 return s->output_section->vma + s->output_offset;
11048 }
0b52efa6
PB
11049}
11050
11051
11052/* Compare two sections based on the locations of the sections they are
11053 linked to. Used by elf_fixup_link_order. */
11054
11055static int
11056compare_link_order (const void * a, const void * b)
11057{
11058 bfd_vma apos;
11059 bfd_vma bpos;
11060
11061 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11062 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11063 if (apos < bpos)
11064 return -1;
11065 return apos > bpos;
11066}
11067
11068
11069/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11070 order as their linked sections. Returns false if this could not be done
11071 because an output section includes both ordered and unordered
11072 sections. Ideally we'd do this in the linker proper. */
11073
11074static bfd_boolean
11075elf_fixup_link_order (bfd *abfd, asection *o)
11076{
11077 int seen_linkorder;
11078 int seen_other;
11079 int n;
11080 struct bfd_link_order *p;
11081 bfd *sub;
11082 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 11083 unsigned elfsec;
0b52efa6 11084 struct bfd_link_order **sections;
d33cdfe3 11085 asection *s, *other_sec, *linkorder_sec;
0b52efa6 11086 bfd_vma offset;
3b36f7e6 11087
d33cdfe3
L
11088 other_sec = NULL;
11089 linkorder_sec = NULL;
0b52efa6
PB
11090 seen_other = 0;
11091 seen_linkorder = 0;
8423293d 11092 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11093 {
d33cdfe3 11094 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
11095 {
11096 s = p->u.indirect.section;
d33cdfe3
L
11097 sub = s->owner;
11098 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11099 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
11100 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11101 && elfsec < elf_numsections (sub)
4fbb74a6
AM
11102 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11103 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
11104 {
11105 seen_linkorder++;
11106 linkorder_sec = s;
11107 }
0b52efa6 11108 else
d33cdfe3
L
11109 {
11110 seen_other++;
11111 other_sec = s;
11112 }
0b52efa6
PB
11113 }
11114 else
11115 seen_other++;
d33cdfe3
L
11116
11117 if (seen_other && seen_linkorder)
11118 {
11119 if (other_sec && linkorder_sec)
4eca0228 11120 _bfd_error_handler
695344c0 11121 /* xgettext:c-format */
4eca0228
AM
11122 (_("%A has both ordered [`%A' in %B] "
11123 "and unordered [`%A' in %B] sections"),
11124 o, linkorder_sec,
11125 linkorder_sec->owner, other_sec,
11126 other_sec->owner);
d33cdfe3 11127 else
4eca0228
AM
11128 _bfd_error_handler
11129 (_("%A has both ordered and unordered sections"), o);
d33cdfe3
L
11130 bfd_set_error (bfd_error_bad_value);
11131 return FALSE;
11132 }
0b52efa6
PB
11133 }
11134
11135 if (!seen_linkorder)
11136 return TRUE;
11137
0b52efa6 11138 sections = (struct bfd_link_order **)
14b1c01e
AM
11139 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11140 if (sections == NULL)
11141 return FALSE;
0b52efa6 11142 seen_linkorder = 0;
3b36f7e6 11143
8423293d 11144 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
11145 {
11146 sections[seen_linkorder++] = p;
11147 }
11148 /* Sort the input sections in the order of their linked section. */
11149 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11150 compare_link_order);
11151
11152 /* Change the offsets of the sections. */
11153 offset = 0;
11154 for (n = 0; n < seen_linkorder; n++)
11155 {
11156 s = sections[n]->u.indirect.section;
461686a3 11157 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 11158 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
11159 sections[n]->offset = offset;
11160 offset += sections[n]->size;
11161 }
11162
4dd07732 11163 free (sections);
0b52efa6
PB
11164 return TRUE;
11165}
11166
76359541
TP
11167/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11168 Returns TRUE upon success, FALSE otherwise. */
11169
11170static bfd_boolean
11171elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11172{
11173 bfd_boolean ret = FALSE;
11174 bfd *implib_bfd;
11175 const struct elf_backend_data *bed;
11176 flagword flags;
11177 enum bfd_architecture arch;
11178 unsigned int mach;
11179 asymbol **sympp = NULL;
11180 long symsize;
11181 long symcount;
11182 long src_count;
11183 elf_symbol_type *osymbuf;
11184
11185 implib_bfd = info->out_implib_bfd;
11186 bed = get_elf_backend_data (abfd);
11187
11188 if (!bfd_set_format (implib_bfd, bfd_object))
11189 return FALSE;
11190
11191 flags = bfd_get_file_flags (abfd);
11192 flags &= ~HAS_RELOC;
11193 if (!bfd_set_start_address (implib_bfd, 0)
11194 || !bfd_set_file_flags (implib_bfd, flags))
11195 return FALSE;
11196
11197 /* Copy architecture of output file to import library file. */
11198 arch = bfd_get_arch (abfd);
11199 mach = bfd_get_mach (abfd);
11200 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11201 && (abfd->target_defaulted
11202 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11203 return FALSE;
11204
11205 /* Get symbol table size. */
11206 symsize = bfd_get_symtab_upper_bound (abfd);
11207 if (symsize < 0)
11208 return FALSE;
11209
11210 /* Read in the symbol table. */
11211 sympp = (asymbol **) xmalloc (symsize);
11212 symcount = bfd_canonicalize_symtab (abfd, sympp);
11213 if (symcount < 0)
11214 goto free_sym_buf;
11215
11216 /* Allow the BFD backend to copy any private header data it
11217 understands from the output BFD to the import library BFD. */
11218 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11219 goto free_sym_buf;
11220
11221 /* Filter symbols to appear in the import library. */
11222 if (bed->elf_backend_filter_implib_symbols)
11223 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11224 symcount);
11225 else
11226 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11227 if (symcount == 0)
11228 {
5df1bc57 11229 bfd_set_error (bfd_error_no_symbols);
4eca0228
AM
11230 _bfd_error_handler (_("%B: no symbol found for import library"),
11231 implib_bfd);
76359541
TP
11232 goto free_sym_buf;
11233 }
11234
11235
11236 /* Make symbols absolute. */
11237 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11238 sizeof (*osymbuf));
11239 for (src_count = 0; src_count < symcount; src_count++)
11240 {
11241 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11242 sizeof (*osymbuf));
11243 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11244 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11245 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11246 osymbuf[src_count].internal_elf_sym.st_value =
11247 osymbuf[src_count].symbol.value;
11248 sympp[src_count] = &osymbuf[src_count].symbol;
11249 }
11250
11251 bfd_set_symtab (implib_bfd, sympp, symcount);
11252
11253 /* Allow the BFD backend to copy any private data it understands
11254 from the output BFD to the import library BFD. This is done last
11255 to permit the routine to look at the filtered symbol table. */
11256 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11257 goto free_sym_buf;
11258
11259 if (!bfd_close (implib_bfd))
11260 goto free_sym_buf;
11261
11262 ret = TRUE;
11263
11264free_sym_buf:
11265 free (sympp);
11266 return ret;
11267}
11268
9f7c3e5e
AM
11269static void
11270elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11271{
11272 asection *o;
11273
11274 if (flinfo->symstrtab != NULL)
ef10c3ac 11275 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
11276 if (flinfo->contents != NULL)
11277 free (flinfo->contents);
11278 if (flinfo->external_relocs != NULL)
11279 free (flinfo->external_relocs);
11280 if (flinfo->internal_relocs != NULL)
11281 free (flinfo->internal_relocs);
11282 if (flinfo->external_syms != NULL)
11283 free (flinfo->external_syms);
11284 if (flinfo->locsym_shndx != NULL)
11285 free (flinfo->locsym_shndx);
11286 if (flinfo->internal_syms != NULL)
11287 free (flinfo->internal_syms);
11288 if (flinfo->indices != NULL)
11289 free (flinfo->indices);
11290 if (flinfo->sections != NULL)
11291 free (flinfo->sections);
9f7c3e5e
AM
11292 if (flinfo->symshndxbuf != NULL)
11293 free (flinfo->symshndxbuf);
11294 for (o = obfd->sections; o != NULL; o = o->next)
11295 {
11296 struct bfd_elf_section_data *esdo = elf_section_data (o);
11297 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11298 free (esdo->rel.hashes);
11299 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11300 free (esdo->rela.hashes);
11301 }
11302}
0b52efa6 11303
c152c796
AM
11304/* Do the final step of an ELF link. */
11305
11306bfd_boolean
11307bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11308{
11309 bfd_boolean dynamic;
11310 bfd_boolean emit_relocs;
11311 bfd *dynobj;
8b127cbc 11312 struct elf_final_link_info flinfo;
91d6fa6a
NC
11313 asection *o;
11314 struct bfd_link_order *p;
11315 bfd *sub;
c152c796
AM
11316 bfd_size_type max_contents_size;
11317 bfd_size_type max_external_reloc_size;
11318 bfd_size_type max_internal_reloc_count;
11319 bfd_size_type max_sym_count;
11320 bfd_size_type max_sym_shndx_count;
c152c796
AM
11321 Elf_Internal_Sym elfsym;
11322 unsigned int i;
11323 Elf_Internal_Shdr *symtab_hdr;
11324 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
11325 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11326 struct elf_outext_info eoinfo;
11327 bfd_boolean merged;
11328 size_t relativecount = 0;
11329 asection *reldyn = 0;
11330 bfd_size_type amt;
104d59d1
JM
11331 asection *attr_section = NULL;
11332 bfd_vma attr_size = 0;
11333 const char *std_attrs_section;
64f52338 11334 struct elf_link_hash_table *htab = elf_hash_table (info);
c152c796 11335
64f52338 11336 if (!is_elf_hash_table (htab))
c152c796
AM
11337 return FALSE;
11338
0e1862bb 11339 if (bfd_link_pic (info))
c152c796
AM
11340 abfd->flags |= DYNAMIC;
11341
64f52338
AM
11342 dynamic = htab->dynamic_sections_created;
11343 dynobj = htab->dynobj;
c152c796 11344
0e1862bb 11345 emit_relocs = (bfd_link_relocatable (info)
a4676736 11346 || info->emitrelocations);
c152c796 11347
8b127cbc
AM
11348 flinfo.info = info;
11349 flinfo.output_bfd = abfd;
ef10c3ac 11350 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11351 if (flinfo.symstrtab == NULL)
c152c796
AM
11352 return FALSE;
11353
11354 if (! dynamic)
11355 {
8b127cbc
AM
11356 flinfo.hash_sec = NULL;
11357 flinfo.symver_sec = NULL;
c152c796
AM
11358 }
11359 else
11360 {
3d4d4302 11361 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11362 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11363 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11364 /* Note that it is OK if symver_sec is NULL. */
11365 }
11366
8b127cbc
AM
11367 flinfo.contents = NULL;
11368 flinfo.external_relocs = NULL;
11369 flinfo.internal_relocs = NULL;
11370 flinfo.external_syms = NULL;
11371 flinfo.locsym_shndx = NULL;
11372 flinfo.internal_syms = NULL;
11373 flinfo.indices = NULL;
11374 flinfo.sections = NULL;
8b127cbc 11375 flinfo.symshndxbuf = NULL;
ffbc01cc 11376 flinfo.filesym_count = 0;
c152c796 11377
104d59d1
JM
11378 /* The object attributes have been merged. Remove the input
11379 sections from the link, and set the contents of the output
11380 secton. */
11381 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11382 for (o = abfd->sections; o != NULL; o = o->next)
11383 {
11384 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11385 || strcmp (o->name, ".gnu.attributes") == 0)
11386 {
11387 for (p = o->map_head.link_order; p != NULL; p = p->next)
11388 {
11389 asection *input_section;
11390
11391 if (p->type != bfd_indirect_link_order)
11392 continue;
11393 input_section = p->u.indirect.section;
11394 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11395 elf_link_input_bfd ignores this section. */
11396 input_section->flags &= ~SEC_HAS_CONTENTS;
11397 }
a0c8462f 11398
104d59d1
JM
11399 attr_size = bfd_elf_obj_attr_size (abfd);
11400 if (attr_size)
11401 {
11402 bfd_set_section_size (abfd, o, attr_size);
11403 attr_section = o;
11404 /* Skip this section later on. */
11405 o->map_head.link_order = NULL;
11406 }
11407 else
11408 o->flags |= SEC_EXCLUDE;
11409 }
11410 }
11411
c152c796
AM
11412 /* Count up the number of relocations we will output for each output
11413 section, so that we know the sizes of the reloc sections. We
11414 also figure out some maximum sizes. */
11415 max_contents_size = 0;
11416 max_external_reloc_size = 0;
11417 max_internal_reloc_count = 0;
11418 max_sym_count = 0;
11419 max_sym_shndx_count = 0;
11420 merged = FALSE;
11421 for (o = abfd->sections; o != NULL; o = o->next)
11422 {
11423 struct bfd_elf_section_data *esdo = elf_section_data (o);
11424 o->reloc_count = 0;
11425
8423293d 11426 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11427 {
11428 unsigned int reloc_count = 0;
9eaff861 11429 unsigned int additional_reloc_count = 0;
c152c796 11430 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11431
11432 if (p->type == bfd_section_reloc_link_order
11433 || p->type == bfd_symbol_reloc_link_order)
11434 reloc_count = 1;
11435 else if (p->type == bfd_indirect_link_order)
11436 {
11437 asection *sec;
11438
11439 sec = p->u.indirect.section;
c152c796
AM
11440
11441 /* Mark all sections which are to be included in the
11442 link. This will normally be every section. We need
11443 to do this so that we can identify any sections which
11444 the linker has decided to not include. */
11445 sec->linker_mark = TRUE;
11446
11447 if (sec->flags & SEC_MERGE)
11448 merged = TRUE;
11449
eea6121a
AM
11450 if (sec->rawsize > max_contents_size)
11451 max_contents_size = sec->rawsize;
11452 if (sec->size > max_contents_size)
11453 max_contents_size = sec->size;
c152c796 11454
c152c796
AM
11455 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11456 && (sec->owner->flags & DYNAMIC) == 0)
11457 {
11458 size_t sym_count;
11459
a961cdd5
AM
11460 /* We are interested in just local symbols, not all
11461 symbols. */
c152c796
AM
11462 if (elf_bad_symtab (sec->owner))
11463 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11464 / bed->s->sizeof_sym);
11465 else
11466 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11467
11468 if (sym_count > max_sym_count)
11469 max_sym_count = sym_count;
11470
11471 if (sym_count > max_sym_shndx_count
6a40cf0c 11472 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11473 max_sym_shndx_count = sym_count;
11474
a961cdd5
AM
11475 if (esdo->this_hdr.sh_type == SHT_REL
11476 || esdo->this_hdr.sh_type == SHT_RELA)
11477 /* Some backends use reloc_count in relocation sections
11478 to count particular types of relocs. Of course,
11479 reloc sections themselves can't have relocations. */
11480 ;
11481 else if (emit_relocs)
11482 {
11483 reloc_count = sec->reloc_count;
11484 if (bed->elf_backend_count_additional_relocs)
11485 {
11486 int c;
11487 c = (*bed->elf_backend_count_additional_relocs) (sec);
11488 additional_reloc_count += c;
11489 }
11490 }
11491 else if (bed->elf_backend_count_relocs)
11492 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11493
11494 esdi = elf_section_data (sec);
11495
c152c796
AM
11496 if ((sec->flags & SEC_RELOC) != 0)
11497 {
d4730f92 11498 size_t ext_size = 0;
c152c796 11499
d4730f92
BS
11500 if (esdi->rel.hdr != NULL)
11501 ext_size = esdi->rel.hdr->sh_size;
11502 if (esdi->rela.hdr != NULL)
11503 ext_size += esdi->rela.hdr->sh_size;
7326c758 11504
c152c796
AM
11505 if (ext_size > max_external_reloc_size)
11506 max_external_reloc_size = ext_size;
11507 if (sec->reloc_count > max_internal_reloc_count)
11508 max_internal_reloc_count = sec->reloc_count;
11509 }
11510 }
11511 }
11512
11513 if (reloc_count == 0)
11514 continue;
11515
9eaff861 11516 reloc_count += additional_reloc_count;
c152c796
AM
11517 o->reloc_count += reloc_count;
11518
0e1862bb 11519 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11520 {
d4730f92 11521 if (esdi->rel.hdr)
9eaff861 11522 {
491d01d3 11523 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
11524 esdo->rel.count += additional_reloc_count;
11525 }
d4730f92 11526 if (esdi->rela.hdr)
9eaff861 11527 {
491d01d3 11528 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
11529 esdo->rela.count += additional_reloc_count;
11530 }
d4730f92
BS
11531 }
11532 else
11533 {
11534 if (o->use_rela_p)
11535 esdo->rela.count += reloc_count;
2c2b4ed4 11536 else
d4730f92 11537 esdo->rel.count += reloc_count;
c152c796 11538 }
c152c796
AM
11539 }
11540
9eaff861 11541 if (o->reloc_count > 0)
c152c796
AM
11542 o->flags |= SEC_RELOC;
11543 else
11544 {
11545 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11546 set it (this is probably a bug) and if it is set
11547 assign_section_numbers will create a reloc section. */
11548 o->flags &=~ SEC_RELOC;
11549 }
11550
11551 /* If the SEC_ALLOC flag is not set, force the section VMA to
11552 zero. This is done in elf_fake_sections as well, but forcing
11553 the VMA to 0 here will ensure that relocs against these
11554 sections are handled correctly. */
11555 if ((o->flags & SEC_ALLOC) == 0
11556 && ! o->user_set_vma)
11557 o->vma = 0;
11558 }
11559
0e1862bb 11560 if (! bfd_link_relocatable (info) && merged)
64f52338 11561 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
11562
11563 /* Figure out the file positions for everything but the symbol table
11564 and the relocs. We set symcount to force assign_section_numbers
11565 to create a symbol table. */
8539e4e8 11566 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11567 BFD_ASSERT (! abfd->output_has_begun);
11568 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11569 goto error_return;
11570
ee75fd95 11571 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11572 for (o = abfd->sections; o != NULL; o = o->next)
11573 {
d4730f92 11574 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11575 if ((o->flags & SEC_RELOC) != 0)
11576 {
d4730f92 11577 if (esdo->rel.hdr
9eaff861 11578 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11579 goto error_return;
11580
d4730f92 11581 if (esdo->rela.hdr
9eaff861 11582 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11583 goto error_return;
11584 }
11585
11586 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11587 to count upwards while actually outputting the relocations. */
d4730f92
BS
11588 esdo->rel.count = 0;
11589 esdo->rela.count = 0;
0ce398f1
L
11590
11591 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11592 {
11593 /* Cache the section contents so that they can be compressed
11594 later. Use bfd_malloc since it will be freed by
11595 bfd_compress_section_contents. */
11596 unsigned char *contents = esdo->this_hdr.contents;
11597 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11598 abort ();
11599 contents
11600 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11601 if (contents == NULL)
11602 goto error_return;
11603 esdo->this_hdr.contents = contents;
11604 }
c152c796
AM
11605 }
11606
c152c796 11607 /* We have now assigned file positions for all the sections except
a485e98e
AM
11608 .symtab, .strtab, and non-loaded reloc sections. We start the
11609 .symtab section at the current file position, and write directly
11610 to it. We build the .strtab section in memory. */
c152c796
AM
11611 bfd_get_symcount (abfd) = 0;
11612 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11613 /* sh_name is set in prep_headers. */
11614 symtab_hdr->sh_type = SHT_SYMTAB;
11615 /* sh_flags, sh_addr and sh_size all start off zero. */
11616 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11617 /* sh_link is set in assign_section_numbers. */
11618 /* sh_info is set below. */
11619 /* sh_offset is set just below. */
72de5009 11620 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11621
ef10c3ac
L
11622 if (max_sym_count < 20)
11623 max_sym_count = 20;
64f52338 11624 htab->strtabsize = max_sym_count;
ef10c3ac 11625 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
11626 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11627 if (htab->strtab == NULL)
c152c796 11628 goto error_return;
ef10c3ac
L
11629 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11630 flinfo.symshndxbuf
11631 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11632 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11633
8539e4e8 11634 if (info->strip != strip_all || emit_relocs)
c152c796 11635 {
8539e4e8
AM
11636 file_ptr off = elf_next_file_pos (abfd);
11637
11638 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11639
11640 /* Note that at this point elf_next_file_pos (abfd) is
11641 incorrect. We do not yet know the size of the .symtab section.
11642 We correct next_file_pos below, after we do know the size. */
11643
11644 /* Start writing out the symbol table. The first symbol is always a
11645 dummy symbol. */
c152c796
AM
11646 elfsym.st_value = 0;
11647 elfsym.st_size = 0;
11648 elfsym.st_info = 0;
11649 elfsym.st_other = 0;
11650 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11651 elfsym.st_target_internal = 0;
ef10c3ac
L
11652 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11653 bfd_und_section_ptr, NULL) != 1)
c152c796 11654 goto error_return;
c152c796 11655
8539e4e8
AM
11656 /* Output a symbol for each section. We output these even if we are
11657 discarding local symbols, since they are used for relocs. These
11658 symbols have no names. We store the index of each one in the
11659 index field of the section, so that we can find it again when
11660 outputting relocs. */
11661
c152c796
AM
11662 elfsym.st_size = 0;
11663 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11664 elfsym.st_other = 0;
f0b5bb34 11665 elfsym.st_value = 0;
35fc36a8 11666 elfsym.st_target_internal = 0;
c152c796
AM
11667 for (i = 1; i < elf_numsections (abfd); i++)
11668 {
11669 o = bfd_section_from_elf_index (abfd, i);
11670 if (o != NULL)
f0b5bb34
AM
11671 {
11672 o->target_index = bfd_get_symcount (abfd);
11673 elfsym.st_shndx = i;
0e1862bb 11674 if (!bfd_link_relocatable (info))
f0b5bb34 11675 elfsym.st_value = o->vma;
ef10c3ac
L
11676 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11677 NULL) != 1)
f0b5bb34
AM
11678 goto error_return;
11679 }
c152c796
AM
11680 }
11681 }
11682
11683 /* Allocate some memory to hold information read in from the input
11684 files. */
11685 if (max_contents_size != 0)
11686 {
8b127cbc
AM
11687 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11688 if (flinfo.contents == NULL)
c152c796
AM
11689 goto error_return;
11690 }
11691
11692 if (max_external_reloc_size != 0)
11693 {
8b127cbc
AM
11694 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11695 if (flinfo.external_relocs == NULL)
c152c796
AM
11696 goto error_return;
11697 }
11698
11699 if (max_internal_reloc_count != 0)
11700 {
11701 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11702 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11703 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11704 if (flinfo.internal_relocs == NULL)
c152c796
AM
11705 goto error_return;
11706 }
11707
11708 if (max_sym_count != 0)
11709 {
11710 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11711 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11712 if (flinfo.external_syms == NULL)
c152c796
AM
11713 goto error_return;
11714
11715 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11716 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11717 if (flinfo.internal_syms == NULL)
c152c796
AM
11718 goto error_return;
11719
11720 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11721 flinfo.indices = (long int *) bfd_malloc (amt);
11722 if (flinfo.indices == NULL)
c152c796
AM
11723 goto error_return;
11724
11725 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11726 flinfo.sections = (asection **) bfd_malloc (amt);
11727 if (flinfo.sections == NULL)
c152c796
AM
11728 goto error_return;
11729 }
11730
11731 if (max_sym_shndx_count != 0)
11732 {
11733 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11734 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11735 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11736 goto error_return;
11737 }
11738
64f52338 11739 if (htab->tls_sec)
c152c796
AM
11740 {
11741 bfd_vma base, end = 0;
11742 asection *sec;
11743
64f52338 11744 for (sec = htab->tls_sec;
c152c796
AM
11745 sec && (sec->flags & SEC_THREAD_LOCAL);
11746 sec = sec->next)
11747 {
3a800eb9 11748 bfd_size_type size = sec->size;
c152c796 11749
3a800eb9
AM
11750 if (size == 0
11751 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11752 {
91d6fa6a
NC
11753 struct bfd_link_order *ord = sec->map_tail.link_order;
11754
11755 if (ord != NULL)
11756 size = ord->offset + ord->size;
c152c796
AM
11757 }
11758 end = sec->vma + size;
11759 }
64f52338 11760 base = htab->tls_sec->vma;
7dc98aea
RO
11761 /* Only align end of TLS section if static TLS doesn't have special
11762 alignment requirements. */
11763 if (bed->static_tls_alignment == 1)
64f52338
AM
11764 end = align_power (end, htab->tls_sec->alignment_power);
11765 htab->tls_size = end - base;
c152c796
AM
11766 }
11767
0b52efa6
PB
11768 /* Reorder SHF_LINK_ORDER sections. */
11769 for (o = abfd->sections; o != NULL; o = o->next)
11770 {
11771 if (!elf_fixup_link_order (abfd, o))
11772 return FALSE;
11773 }
11774
2f0c68f2
CM
11775 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11776 return FALSE;
11777
c152c796
AM
11778 /* Since ELF permits relocations to be against local symbols, we
11779 must have the local symbols available when we do the relocations.
11780 Since we would rather only read the local symbols once, and we
11781 would rather not keep them in memory, we handle all the
11782 relocations for a single input file at the same time.
11783
11784 Unfortunately, there is no way to know the total number of local
11785 symbols until we have seen all of them, and the local symbol
11786 indices precede the global symbol indices. This means that when
11787 we are generating relocatable output, and we see a reloc against
11788 a global symbol, we can not know the symbol index until we have
11789 finished examining all the local symbols to see which ones we are
11790 going to output. To deal with this, we keep the relocations in
11791 memory, and don't output them until the end of the link. This is
11792 an unfortunate waste of memory, but I don't see a good way around
11793 it. Fortunately, it only happens when performing a relocatable
11794 link, which is not the common case. FIXME: If keep_memory is set
11795 we could write the relocs out and then read them again; I don't
11796 know how bad the memory loss will be. */
11797
c72f2fb2 11798 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11799 sub->output_has_begun = FALSE;
11800 for (o = abfd->sections; o != NULL; o = o->next)
11801 {
8423293d 11802 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11803 {
11804 if (p->type == bfd_indirect_link_order
11805 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11806 == bfd_target_elf_flavour)
11807 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11808 {
11809 if (! sub->output_has_begun)
11810 {
8b127cbc 11811 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11812 goto error_return;
11813 sub->output_has_begun = TRUE;
11814 }
11815 }
11816 else if (p->type == bfd_section_reloc_link_order
11817 || p->type == bfd_symbol_reloc_link_order)
11818 {
11819 if (! elf_reloc_link_order (abfd, info, o, p))
11820 goto error_return;
11821 }
11822 else
11823 {
11824 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11825 {
11826 if (p->type == bfd_indirect_link_order
11827 && (bfd_get_flavour (sub)
11828 == bfd_target_elf_flavour)
11829 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11830 != bed->s->elfclass))
11831 {
11832 const char *iclass, *oclass;
11833
aebf9be7 11834 switch (bed->s->elfclass)
351f65ca 11835 {
aebf9be7
NC
11836 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11837 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11838 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11839 default: abort ();
351f65ca 11840 }
aebf9be7
NC
11841
11842 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11843 {
aebf9be7
NC
11844 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11845 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11846 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11847 default: abort ();
351f65ca
L
11848 }
11849
11850 bfd_set_error (bfd_error_wrong_format);
4eca0228 11851 _bfd_error_handler
695344c0 11852 /* xgettext:c-format */
351f65ca
L
11853 (_("%B: file class %s incompatible with %s"),
11854 sub, iclass, oclass);
11855 }
11856
11857 goto error_return;
11858 }
c152c796
AM
11859 }
11860 }
11861 }
11862
c0f00686
L
11863 /* Free symbol buffer if needed. */
11864 if (!info->reduce_memory_overheads)
11865 {
c72f2fb2 11866 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11867 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11868 && elf_tdata (sub)->symbuf)
c0f00686
L
11869 {
11870 free (elf_tdata (sub)->symbuf);
11871 elf_tdata (sub)->symbuf = NULL;
11872 }
11873 }
11874
c152c796
AM
11875 /* Output any global symbols that got converted to local in a
11876 version script or due to symbol visibility. We do this in a
11877 separate step since ELF requires all local symbols to appear
11878 prior to any global symbols. FIXME: We should only do this if
11879 some global symbols were, in fact, converted to become local.
11880 FIXME: Will this work correctly with the Irix 5 linker? */
11881 eoinfo.failed = FALSE;
8b127cbc 11882 eoinfo.flinfo = &flinfo;
c152c796 11883 eoinfo.localsyms = TRUE;
34a79995 11884 eoinfo.file_sym_done = FALSE;
7686d77d 11885 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11886 if (eoinfo.failed)
11887 return FALSE;
11888
4e617b1e
PB
11889 /* If backend needs to output some local symbols not present in the hash
11890 table, do it now. */
8539e4e8
AM
11891 if (bed->elf_backend_output_arch_local_syms
11892 && (info->strip != strip_all || emit_relocs))
4e617b1e 11893 {
6e0b88f1 11894 typedef int (*out_sym_func)
4e617b1e
PB
11895 (void *, const char *, Elf_Internal_Sym *, asection *,
11896 struct elf_link_hash_entry *);
11897
11898 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11899 (abfd, info, &flinfo,
11900 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11901 return FALSE;
11902 }
11903
c152c796
AM
11904 /* That wrote out all the local symbols. Finish up the symbol table
11905 with the global symbols. Even if we want to strip everything we
11906 can, we still need to deal with those global symbols that got
11907 converted to local in a version script. */
11908
11909 /* The sh_info field records the index of the first non local symbol. */
11910 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11911
11912 if (dynamic
64f52338
AM
11913 && htab->dynsym != NULL
11914 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
11915 {
11916 Elf_Internal_Sym sym;
64f52338 11917 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 11918
64f52338
AM
11919 o = htab->dynsym->output_section;
11920 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
11921
11922 /* Write out the section symbols for the output sections. */
0e1862bb 11923 if (bfd_link_pic (info)
64f52338 11924 || htab->is_relocatable_executable)
c152c796
AM
11925 {
11926 asection *s;
11927
11928 sym.st_size = 0;
11929 sym.st_name = 0;
11930 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11931 sym.st_other = 0;
35fc36a8 11932 sym.st_target_internal = 0;
c152c796
AM
11933
11934 for (s = abfd->sections; s != NULL; s = s->next)
11935 {
11936 int indx;
11937 bfd_byte *dest;
11938 long dynindx;
11939
c152c796 11940 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11941 if (dynindx <= 0)
11942 continue;
11943 indx = elf_section_data (s)->this_idx;
c152c796
AM
11944 BFD_ASSERT (indx > 0);
11945 sym.st_shndx = indx;
c0d5a53d
L
11946 if (! check_dynsym (abfd, &sym))
11947 return FALSE;
c152c796
AM
11948 sym.st_value = s->vma;
11949 dest = dynsym + dynindx * bed->s->sizeof_sym;
11950 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11951 }
c152c796
AM
11952 }
11953
11954 /* Write out the local dynsyms. */
64f52338 11955 if (htab->dynlocal)
c152c796
AM
11956 {
11957 struct elf_link_local_dynamic_entry *e;
64f52338 11958 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
11959 {
11960 asection *s;
11961 bfd_byte *dest;
11962
935bd1e0 11963 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11964 Note that we saved a word of storage and overwrote
11965 the original st_name with the dynstr_index. */
11966 sym = e->isym;
935bd1e0 11967 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11968
cb33740c
AM
11969 s = bfd_section_from_elf_index (e->input_bfd,
11970 e->isym.st_shndx);
11971 if (s != NULL)
c152c796 11972 {
c152c796
AM
11973 sym.st_shndx =
11974 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11975 if (! check_dynsym (abfd, &sym))
11976 return FALSE;
c152c796
AM
11977 sym.st_value = (s->output_section->vma
11978 + s->output_offset
11979 + e->isym.st_value);
11980 }
11981
c152c796
AM
11982 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11983 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11984 }
11985 }
c152c796
AM
11986 }
11987
11988 /* We get the global symbols from the hash table. */
11989 eoinfo.failed = FALSE;
11990 eoinfo.localsyms = FALSE;
8b127cbc 11991 eoinfo.flinfo = &flinfo;
7686d77d 11992 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11993 if (eoinfo.failed)
11994 return FALSE;
11995
11996 /* If backend needs to output some symbols not present in the hash
11997 table, do it now. */
8539e4e8
AM
11998 if (bed->elf_backend_output_arch_syms
11999 && (info->strip != strip_all || emit_relocs))
c152c796 12000 {
6e0b88f1 12001 typedef int (*out_sym_func)
c152c796
AM
12002 (void *, const char *, Elf_Internal_Sym *, asection *,
12003 struct elf_link_hash_entry *);
12004
12005 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12006 (abfd, info, &flinfo,
12007 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
12008 return FALSE;
12009 }
12010
ef10c3ac
L
12011 /* Finalize the .strtab section. */
12012 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12013
12014 /* Swap out the .strtab section. */
12015 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
12016 return FALSE;
12017
12018 /* Now we know the size of the symtab section. */
c152c796
AM
12019 if (bfd_get_symcount (abfd) > 0)
12020 {
ee3b52e9
L
12021 /* Finish up and write out the symbol string table (.strtab)
12022 section. */
12023 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
12024 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12025
6a40cf0c
NC
12026 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12027 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
12028 {
12029 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12030 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12031 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12032 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12033 symtab_shndx_hdr->sh_size = amt;
12034
12035 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12036 off, TRUE);
12037
12038 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12039 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12040 return FALSE;
12041 }
ee3b52e9
L
12042
12043 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12044 /* sh_name was set in prep_headers. */
12045 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12046 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12047 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12048 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12049 symstrtab_hdr->sh_entsize = 0;
12050 symstrtab_hdr->sh_link = 0;
12051 symstrtab_hdr->sh_info = 0;
12052 /* sh_offset is set just below. */
12053 symstrtab_hdr->sh_addralign = 1;
12054
12055 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12056 off, TRUE);
12057 elf_next_file_pos (abfd) = off;
12058
c152c796 12059 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12060 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
12061 return FALSE;
12062 }
12063
76359541
TP
12064 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12065 {
4eca0228
AM
12066 _bfd_error_handler (_("%B: failed to generate import library"),
12067 info->out_implib_bfd);
76359541
TP
12068 return FALSE;
12069 }
12070
c152c796
AM
12071 /* Adjust the relocs to have the correct symbol indices. */
12072 for (o = abfd->sections; o != NULL; o = o->next)
12073 {
d4730f92 12074 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12075 bfd_boolean sort;
c152c796
AM
12076 if ((o->flags & SEC_RELOC) == 0)
12077 continue;
12078
28dbcedc 12079 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12080 if (esdo->rel.hdr != NULL
9eaff861 12081 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
bca6d0e3
AM
12082 return FALSE;
12083 if (esdo->rela.hdr != NULL
9eaff861 12084 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
bca6d0e3 12085 return FALSE;
c152c796
AM
12086
12087 /* Set the reloc_count field to 0 to prevent write_relocs from
12088 trying to swap the relocs out itself. */
12089 o->reloc_count = 0;
12090 }
12091
12092 if (dynamic && info->combreloc && dynobj != NULL)
12093 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12094
12095 /* If we are linking against a dynamic object, or generating a
12096 shared library, finish up the dynamic linking information. */
12097 if (dynamic)
12098 {
12099 bfd_byte *dyncon, *dynconend;
12100
12101 /* Fix up .dynamic entries. */
3d4d4302 12102 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12103 BFD_ASSERT (o != NULL);
12104
12105 dyncon = o->contents;
eea6121a 12106 dynconend = o->contents + o->size;
c152c796
AM
12107 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12108 {
12109 Elf_Internal_Dyn dyn;
12110 const char *name;
12111 unsigned int type;
12112
12113 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12114
12115 switch (dyn.d_tag)
12116 {
12117 default:
12118 continue;
12119 case DT_NULL:
12120 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12121 {
12122 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12123 {
12124 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12125 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12126 default: continue;
12127 }
12128 dyn.d_un.d_val = relativecount;
12129 relativecount = 0;
12130 break;
12131 }
12132 continue;
12133
12134 case DT_INIT:
12135 name = info->init_function;
12136 goto get_sym;
12137 case DT_FINI:
12138 name = info->fini_function;
12139 get_sym:
12140 {
12141 struct elf_link_hash_entry *h;
12142
64f52338 12143 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12144 if (h != NULL
12145 && (h->root.type == bfd_link_hash_defined
12146 || h->root.type == bfd_link_hash_defweak))
12147 {
bef26483 12148 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12149 o = h->root.u.def.section;
12150 if (o->output_section != NULL)
bef26483 12151 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12152 + o->output_offset);
12153 else
12154 {
12155 /* The symbol is imported from another shared
12156 library and does not apply to this one. */
bef26483 12157 dyn.d_un.d_ptr = 0;
c152c796
AM
12158 }
12159 break;
12160 }
12161 }
12162 continue;
12163
12164 case DT_PREINIT_ARRAYSZ:
12165 name = ".preinit_array";
4ade44b7 12166 goto get_out_size;
c152c796
AM
12167 case DT_INIT_ARRAYSZ:
12168 name = ".init_array";
4ade44b7 12169 goto get_out_size;
c152c796
AM
12170 case DT_FINI_ARRAYSZ:
12171 name = ".fini_array";
4ade44b7 12172 get_out_size:
c152c796
AM
12173 o = bfd_get_section_by_name (abfd, name);
12174 if (o == NULL)
12175 {
4eca0228 12176 _bfd_error_handler
4ade44b7 12177 (_("could not find section %s"), name);
c152c796
AM
12178 goto error_return;
12179 }
eea6121a 12180 if (o->size == 0)
4eca0228 12181 _bfd_error_handler
c152c796 12182 (_("warning: %s section has zero size"), name);
eea6121a 12183 dyn.d_un.d_val = o->size;
c152c796
AM
12184 break;
12185
12186 case DT_PREINIT_ARRAY:
12187 name = ".preinit_array";
4ade44b7 12188 goto get_out_vma;
c152c796
AM
12189 case DT_INIT_ARRAY:
12190 name = ".init_array";
4ade44b7 12191 goto get_out_vma;
c152c796
AM
12192 case DT_FINI_ARRAY:
12193 name = ".fini_array";
4ade44b7
AM
12194 get_out_vma:
12195 o = bfd_get_section_by_name (abfd, name);
12196 goto do_vma;
c152c796
AM
12197
12198 case DT_HASH:
12199 name = ".hash";
12200 goto get_vma;
fdc90cb4
JJ
12201 case DT_GNU_HASH:
12202 name = ".gnu.hash";
12203 goto get_vma;
c152c796
AM
12204 case DT_STRTAB:
12205 name = ".dynstr";
12206 goto get_vma;
12207 case DT_SYMTAB:
12208 name = ".dynsym";
12209 goto get_vma;
12210 case DT_VERDEF:
12211 name = ".gnu.version_d";
12212 goto get_vma;
12213 case DT_VERNEED:
12214 name = ".gnu.version_r";
12215 goto get_vma;
12216 case DT_VERSYM:
12217 name = ".gnu.version";
12218 get_vma:
4ade44b7
AM
12219 o = bfd_get_linker_section (dynobj, name);
12220 do_vma:
c152c796
AM
12221 if (o == NULL)
12222 {
4eca0228 12223 _bfd_error_handler
4ade44b7 12224 (_("could not find section %s"), name);
c152c796
AM
12225 goto error_return;
12226 }
894891db
NC
12227 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12228 {
4eca0228 12229 _bfd_error_handler
894891db
NC
12230 (_("warning: section '%s' is being made into a note"), name);
12231 bfd_set_error (bfd_error_nonrepresentable_section);
12232 goto error_return;
12233 }
4ade44b7 12234 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
12235 break;
12236
12237 case DT_REL:
12238 case DT_RELA:
12239 case DT_RELSZ:
12240 case DT_RELASZ:
12241 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12242 type = SHT_REL;
12243 else
12244 type = SHT_RELA;
12245 dyn.d_un.d_val = 0;
bef26483 12246 dyn.d_un.d_ptr = 0;
c152c796
AM
12247 for (i = 1; i < elf_numsections (abfd); i++)
12248 {
12249 Elf_Internal_Shdr *hdr;
12250
12251 hdr = elf_elfsections (abfd)[i];
12252 if (hdr->sh_type == type
12253 && (hdr->sh_flags & SHF_ALLOC) != 0)
12254 {
12255 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12256 dyn.d_un.d_val += hdr->sh_size;
12257 else
12258 {
bef26483
AM
12259 if (dyn.d_un.d_ptr == 0
12260 || hdr->sh_addr < dyn.d_un.d_ptr)
12261 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
12262 }
12263 }
12264 }
64f52338
AM
12265 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12266 {
12267 /* Don't count procedure linkage table relocs in the
12268 overall reloc count. */
12269 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12270 dyn.d_un.d_val -= htab->srelplt->size;
12271 /* If .rela.plt is the first .rela section, exclude
12272 it from DT_RELA. */
12273 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12274 + htab->srelplt->output_offset))
12275 dyn.d_un.d_ptr += htab->srelplt->size;
12276 }
c152c796
AM
12277 break;
12278 }
12279 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12280 }
12281 }
12282
12283 /* If we have created any dynamic sections, then output them. */
12284 if (dynobj != NULL)
12285 {
12286 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12287 goto error_return;
12288
943284cc 12289 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 12290 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 12291 || info->error_textrel)
3d4d4302 12292 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
12293 {
12294 bfd_byte *dyncon, *dynconend;
12295
943284cc
DJ
12296 dyncon = o->contents;
12297 dynconend = o->contents + o->size;
12298 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12299 {
12300 Elf_Internal_Dyn dyn;
12301
12302 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12303
12304 if (dyn.d_tag == DT_TEXTREL)
12305 {
c192a133
AM
12306 if (info->error_textrel)
12307 info->callbacks->einfo
12308 (_("%P%X: read-only segment has dynamic relocations.\n"));
12309 else
12310 info->callbacks->einfo
12311 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
12312 break;
12313 }
12314 }
12315 }
12316
c152c796
AM
12317 for (o = dynobj->sections; o != NULL; o = o->next)
12318 {
12319 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 12320 || o->size == 0
c152c796
AM
12321 || o->output_section == bfd_abs_section_ptr)
12322 continue;
12323 if ((o->flags & SEC_LINKER_CREATED) == 0)
12324 {
12325 /* At this point, we are only interested in sections
12326 created by _bfd_elf_link_create_dynamic_sections. */
12327 continue;
12328 }
64f52338 12329 if (htab->stab_info.stabstr == o)
3722b82f 12330 continue;
64f52338 12331 if (htab->eh_info.hdr_sec == o)
eea6121a 12332 continue;
3d4d4302 12333 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
12334 {
12335 if (! bfd_set_section_contents (abfd, o->output_section,
12336 o->contents,
37b01f6a
DG
12337 (file_ptr) o->output_offset
12338 * bfd_octets_per_byte (abfd),
eea6121a 12339 o->size))
c152c796
AM
12340 goto error_return;
12341 }
12342 else
12343 {
12344 /* The contents of the .dynstr section are actually in a
12345 stringtab. */
8539e4e8
AM
12346 file_ptr off;
12347
c152c796
AM
12348 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12349 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 12350 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
12351 goto error_return;
12352 }
12353 }
12354 }
12355
0e1862bb 12356 if (bfd_link_relocatable (info))
c152c796
AM
12357 {
12358 bfd_boolean failed = FALSE;
12359
12360 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12361 if (failed)
12362 goto error_return;
12363 }
12364
12365 /* If we have optimized stabs strings, output them. */
64f52338 12366 if (htab->stab_info.stabstr != NULL)
c152c796 12367 {
64f52338 12368 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
12369 goto error_return;
12370 }
12371
9f7c3e5e
AM
12372 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12373 goto error_return;
c152c796 12374
9f7c3e5e 12375 elf_final_link_free (abfd, &flinfo);
c152c796 12376
12bd6957 12377 elf_linker (abfd) = TRUE;
c152c796 12378
104d59d1
JM
12379 if (attr_section)
12380 {
a50b1753 12381 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12382 if (contents == NULL)
d0f16d5e 12383 return FALSE; /* Bail out and fail. */
104d59d1
JM
12384 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12385 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12386 free (contents);
12387 }
12388
c152c796
AM
12389 return TRUE;
12390
12391 error_return:
9f7c3e5e 12392 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12393 return FALSE;
12394}
12395\f
5241d853
RS
12396/* Initialize COOKIE for input bfd ABFD. */
12397
12398static bfd_boolean
12399init_reloc_cookie (struct elf_reloc_cookie *cookie,
12400 struct bfd_link_info *info, bfd *abfd)
12401{
12402 Elf_Internal_Shdr *symtab_hdr;
12403 const struct elf_backend_data *bed;
12404
12405 bed = get_elf_backend_data (abfd);
12406 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12407
12408 cookie->abfd = abfd;
12409 cookie->sym_hashes = elf_sym_hashes (abfd);
12410 cookie->bad_symtab = elf_bad_symtab (abfd);
12411 if (cookie->bad_symtab)
12412 {
12413 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12414 cookie->extsymoff = 0;
12415 }
12416 else
12417 {
12418 cookie->locsymcount = symtab_hdr->sh_info;
12419 cookie->extsymoff = symtab_hdr->sh_info;
12420 }
12421
12422 if (bed->s->arch_size == 32)
12423 cookie->r_sym_shift = 8;
12424 else
12425 cookie->r_sym_shift = 32;
12426
12427 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12428 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12429 {
12430 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12431 cookie->locsymcount, 0,
12432 NULL, NULL, NULL);
12433 if (cookie->locsyms == NULL)
12434 {
12435 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12436 return FALSE;
12437 }
12438 if (info->keep_memory)
12439 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12440 }
12441 return TRUE;
12442}
12443
12444/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12445
12446static void
12447fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12448{
12449 Elf_Internal_Shdr *symtab_hdr;
12450
12451 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12452 if (cookie->locsyms != NULL
12453 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12454 free (cookie->locsyms);
12455}
12456
12457/* Initialize the relocation information in COOKIE for input section SEC
12458 of input bfd ABFD. */
12459
12460static bfd_boolean
12461init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12462 struct bfd_link_info *info, bfd *abfd,
12463 asection *sec)
12464{
12465 const struct elf_backend_data *bed;
12466
12467 if (sec->reloc_count == 0)
12468 {
12469 cookie->rels = NULL;
12470 cookie->relend = NULL;
12471 }
12472 else
12473 {
12474 bed = get_elf_backend_data (abfd);
12475
12476 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12477 info->keep_memory);
12478 if (cookie->rels == NULL)
12479 return FALSE;
12480 cookie->rel = cookie->rels;
12481 cookie->relend = (cookie->rels
12482 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12483 }
12484 cookie->rel = cookie->rels;
12485 return TRUE;
12486}
12487
12488/* Free the memory allocated by init_reloc_cookie_rels,
12489 if appropriate. */
12490
12491static void
12492fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12493 asection *sec)
12494{
12495 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12496 free (cookie->rels);
12497}
12498
12499/* Initialize the whole of COOKIE for input section SEC. */
12500
12501static bfd_boolean
12502init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12503 struct bfd_link_info *info,
12504 asection *sec)
12505{
12506 if (!init_reloc_cookie (cookie, info, sec->owner))
12507 goto error1;
12508 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12509 goto error2;
12510 return TRUE;
12511
12512 error2:
12513 fini_reloc_cookie (cookie, sec->owner);
12514 error1:
12515 return FALSE;
12516}
12517
12518/* Free the memory allocated by init_reloc_cookie_for_section,
12519 if appropriate. */
12520
12521static void
12522fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12523 asection *sec)
12524{
12525 fini_reloc_cookie_rels (cookie, sec);
12526 fini_reloc_cookie (cookie, sec->owner);
12527}
12528\f
c152c796
AM
12529/* Garbage collect unused sections. */
12530
07adf181
AM
12531/* Default gc_mark_hook. */
12532
12533asection *
12534_bfd_elf_gc_mark_hook (asection *sec,
12535 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12536 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12537 struct elf_link_hash_entry *h,
12538 Elf_Internal_Sym *sym)
12539{
12540 if (h != NULL)
12541 {
12542 switch (h->root.type)
12543 {
12544 case bfd_link_hash_defined:
12545 case bfd_link_hash_defweak:
12546 return h->root.u.def.section;
12547
12548 case bfd_link_hash_common:
12549 return h->root.u.c.p->section;
12550
12551 default:
12552 break;
12553 }
12554 }
12555 else
12556 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12557
12558 return NULL;
12559}
12560
a6a4679f
AM
12561/* For undefined __start_<name> and __stop_<name> symbols, return the
12562 first input section matching <name>. Return NULL otherwise. */
12563
12564asection *
12565_bfd_elf_is_start_stop (const struct bfd_link_info *info,
12566 struct elf_link_hash_entry *h)
12567{
12568 asection *s;
12569 const char *sec_name;
12570
12571 if (h->root.type != bfd_link_hash_undefined
12572 && h->root.type != bfd_link_hash_undefweak)
12573 return NULL;
12574
12575 s = h->root.u.undef.section;
12576 if (s != NULL)
12577 {
12578 if (s == (asection *) 0 - 1)
12579 return NULL;
12580 return s;
12581 }
12582
12583 sec_name = NULL;
12584 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12585 sec_name = h->root.root.string + 8;
12586 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12587 sec_name = h->root.root.string + 7;
12588
12589 if (sec_name != NULL && *sec_name != '\0')
12590 {
12591 bfd *i;
12592
12593 for (i = info->input_bfds; i != NULL; i = i->link.next)
12594 {
12595 s = bfd_get_section_by_name (i, sec_name);
12596 if (s != NULL)
12597 {
12598 h->root.u.undef.section = s;
12599 break;
12600 }
12601 }
12602 }
12603
12604 if (s == NULL)
12605 h->root.u.undef.section = (asection *) 0 - 1;
12606
12607 return s;
12608}
12609
5241d853
RS
12610/* COOKIE->rel describes a relocation against section SEC, which is
12611 a section we've decided to keep. Return the section that contains
12612 the relocation symbol, or NULL if no section contains it. */
12613
12614asection *
12615_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12616 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12617 struct elf_reloc_cookie *cookie,
12618 bfd_boolean *start_stop)
5241d853
RS
12619{
12620 unsigned long r_symndx;
12621 struct elf_link_hash_entry *h;
12622
12623 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12624 if (r_symndx == STN_UNDEF)
5241d853
RS
12625 return NULL;
12626
12627 if (r_symndx >= cookie->locsymcount
12628 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12629 {
12630 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12631 if (h == NULL)
12632 {
12633 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12634 sec->owner);
12635 return NULL;
12636 }
5241d853
RS
12637 while (h->root.type == bfd_link_hash_indirect
12638 || h->root.type == bfd_link_hash_warning)
12639 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12640 h->mark = 1;
4e6b54a6
AM
12641 /* If this symbol is weak and there is a non-weak definition, we
12642 keep the non-weak definition because many backends put
12643 dynamic reloc info on the non-weak definition for code
12644 handling copy relocs. */
12645 if (h->u.weakdef != NULL)
12646 h->u.weakdef->mark = 1;
1cce69b9 12647
a6a4679f 12648 if (start_stop != NULL)
1cce69b9
AM
12649 {
12650 /* To work around a glibc bug, mark all XXX input sections
12651 when there is an as yet undefined reference to __start_XXX
12652 or __stop_XXX symbols. The linker will later define such
12653 symbols for orphan input sections that have a name
12654 representable as a C identifier. */
a6a4679f 12655 asection *s = _bfd_elf_is_start_stop (info, h);
1cce69b9 12656
a6a4679f 12657 if (s != NULL)
1cce69b9 12658 {
a6a4679f
AM
12659 *start_stop = !s->gc_mark;
12660 return s;
1cce69b9
AM
12661 }
12662 }
12663
5241d853
RS
12664 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12665 }
12666
12667 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12668 &cookie->locsyms[r_symndx]);
12669}
12670
12671/* COOKIE->rel describes a relocation against section SEC, which is
12672 a section we've decided to keep. Mark the section that contains
9d0a14d3 12673 the relocation symbol. */
5241d853
RS
12674
12675bfd_boolean
12676_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12677 asection *sec,
12678 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12679 struct elf_reloc_cookie *cookie)
5241d853
RS
12680{
12681 asection *rsec;
1cce69b9 12682 bfd_boolean start_stop = FALSE;
5241d853 12683
1cce69b9
AM
12684 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12685 while (rsec != NULL)
5241d853 12686 {
1cce69b9
AM
12687 if (!rsec->gc_mark)
12688 {
12689 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12690 || (rsec->owner->flags & DYNAMIC) != 0)
12691 rsec->gc_mark = 1;
12692 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12693 return FALSE;
12694 }
12695 if (!start_stop)
12696 break;
199af150 12697 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12698 }
12699 return TRUE;
12700}
12701
07adf181
AM
12702/* The mark phase of garbage collection. For a given section, mark
12703 it and any sections in this section's group, and all the sections
12704 which define symbols to which it refers. */
12705
ccfa59ea
AM
12706bfd_boolean
12707_bfd_elf_gc_mark (struct bfd_link_info *info,
12708 asection *sec,
6a5bb875 12709 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12710{
12711 bfd_boolean ret;
9d0a14d3 12712 asection *group_sec, *eh_frame;
c152c796
AM
12713
12714 sec->gc_mark = 1;
12715
12716 /* Mark all the sections in the group. */
12717 group_sec = elf_section_data (sec)->next_in_group;
12718 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12719 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12720 return FALSE;
12721
12722 /* Look through the section relocs. */
12723 ret = TRUE;
9d0a14d3
RS
12724 eh_frame = elf_eh_frame_section (sec->owner);
12725 if ((sec->flags & SEC_RELOC) != 0
12726 && sec->reloc_count > 0
12727 && sec != eh_frame)
c152c796 12728 {
5241d853 12729 struct elf_reloc_cookie cookie;
c152c796 12730
5241d853
RS
12731 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12732 ret = FALSE;
c152c796 12733 else
c152c796 12734 {
5241d853 12735 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12736 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12737 {
12738 ret = FALSE;
12739 break;
12740 }
12741 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12742 }
12743 }
9d0a14d3
RS
12744
12745 if (ret && eh_frame && elf_fde_list (sec))
12746 {
12747 struct elf_reloc_cookie cookie;
12748
12749 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12750 ret = FALSE;
12751 else
12752 {
12753 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12754 gc_mark_hook, &cookie))
12755 ret = FALSE;
12756 fini_reloc_cookie_for_section (&cookie, eh_frame);
12757 }
12758 }
12759
2f0c68f2
CM
12760 eh_frame = elf_section_eh_frame_entry (sec);
12761 if (ret && eh_frame && !eh_frame->gc_mark)
12762 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12763 ret = FALSE;
12764
c152c796
AM
12765 return ret;
12766}
12767
3c758495
TG
12768/* Scan and mark sections in a special or debug section group. */
12769
12770static void
12771_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12772{
12773 /* Point to first section of section group. */
12774 asection *ssec;
12775 /* Used to iterate the section group. */
12776 asection *msec;
12777
12778 bfd_boolean is_special_grp = TRUE;
12779 bfd_boolean is_debug_grp = TRUE;
12780
12781 /* First scan to see if group contains any section other than debug
12782 and special section. */
12783 ssec = msec = elf_next_in_group (grp);
12784 do
12785 {
12786 if ((msec->flags & SEC_DEBUGGING) == 0)
12787 is_debug_grp = FALSE;
12788
12789 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12790 is_special_grp = FALSE;
12791
12792 msec = elf_next_in_group (msec);
12793 }
12794 while (msec != ssec);
12795
12796 /* If this is a pure debug section group or pure special section group,
12797 keep all sections in this group. */
12798 if (is_debug_grp || is_special_grp)
12799 {
12800 do
12801 {
12802 msec->gc_mark = 1;
12803 msec = elf_next_in_group (msec);
12804 }
12805 while (msec != ssec);
12806 }
12807}
12808
7f6ab9f8
AM
12809/* Keep debug and special sections. */
12810
12811bfd_boolean
12812_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12813 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12814{
12815 bfd *ibfd;
12816
c72f2fb2 12817 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12818 {
12819 asection *isec;
12820 bfd_boolean some_kept;
b40bf0a2 12821 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12822
12823 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12824 continue;
12825
b40bf0a2
NC
12826 /* Ensure all linker created sections are kept,
12827 see if any other section is already marked,
12828 and note if we have any fragmented debug sections. */
12829 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12830 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12831 {
12832 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12833 isec->gc_mark = 1;
12834 else if (isec->gc_mark)
12835 some_kept = TRUE;
b40bf0a2
NC
12836
12837 if (debug_frag_seen == FALSE
12838 && (isec->flags & SEC_DEBUGGING)
12839 && CONST_STRNEQ (isec->name, ".debug_line."))
12840 debug_frag_seen = TRUE;
7f6ab9f8
AM
12841 }
12842
12843 /* If no section in this file will be kept, then we can
b40bf0a2 12844 toss out the debug and special sections. */
7f6ab9f8
AM
12845 if (!some_kept)
12846 continue;
12847
12848 /* Keep debug and special sections like .comment when they are
3c758495
TG
12849 not part of a group. Also keep section groups that contain
12850 just debug sections or special sections. */
7f6ab9f8 12851 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12852 {
12853 if ((isec->flags & SEC_GROUP) != 0)
12854 _bfd_elf_gc_mark_debug_special_section_group (isec);
12855 else if (((isec->flags & SEC_DEBUGGING) != 0
12856 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12857 && elf_next_in_group (isec) == NULL)
12858 isec->gc_mark = 1;
12859 }
b40bf0a2
NC
12860
12861 if (! debug_frag_seen)
12862 continue;
12863
12864 /* Look for CODE sections which are going to be discarded,
12865 and find and discard any fragmented debug sections which
12866 are associated with that code section. */
12867 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12868 if ((isec->flags & SEC_CODE) != 0
12869 && isec->gc_mark == 0)
12870 {
12871 unsigned int ilen;
12872 asection *dsec;
12873
12874 ilen = strlen (isec->name);
12875
12876 /* Association is determined by the name of the debug section
12877 containing the name of the code section as a suffix. For
12878 example .debug_line.text.foo is a debug section associated
12879 with .text.foo. */
12880 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12881 {
12882 unsigned int dlen;
12883
12884 if (dsec->gc_mark == 0
12885 || (dsec->flags & SEC_DEBUGGING) == 0)
12886 continue;
12887
12888 dlen = strlen (dsec->name);
12889
12890 if (dlen > ilen
12891 && strncmp (dsec->name + (dlen - ilen),
12892 isec->name, ilen) == 0)
12893 {
12894 dsec->gc_mark = 0;
b40bf0a2
NC
12895 }
12896 }
12897 }
7f6ab9f8
AM
12898 }
12899 return TRUE;
12900}
12901
c152c796
AM
12902/* The sweep phase of garbage collection. Remove all garbage sections. */
12903
12904typedef bfd_boolean (*gc_sweep_hook_fn)
12905 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12906
12907static bfd_boolean
ccabcbe5 12908elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12909{
12910 bfd *sub;
ccabcbe5
AM
12911 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12912 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
c152c796 12913
c72f2fb2 12914 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12915 {
12916 asection *o;
12917
b19a8f85
L
12918 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12919 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12920 continue;
12921
12922 for (o = sub->sections; o != NULL; o = o->next)
12923 {
a33dafc3
L
12924 /* When any section in a section group is kept, we keep all
12925 sections in the section group. If the first member of
12926 the section group is excluded, we will also exclude the
12927 group section. */
12928 if (o->flags & SEC_GROUP)
12929 {
12930 asection *first = elf_next_in_group (o);
12931 o->gc_mark = first->gc_mark;
12932 }
c152c796 12933
1e7eae0d 12934 if (o->gc_mark)
c152c796
AM
12935 continue;
12936
12937 /* Skip sweeping sections already excluded. */
12938 if (o->flags & SEC_EXCLUDE)
12939 continue;
12940
12941 /* Since this is early in the link process, it is simple
12942 to remove a section from the output. */
12943 o->flags |= SEC_EXCLUDE;
12944
c55fe096 12945 if (info->print_gc_sections && o->size != 0)
695344c0 12946 /* xgettext:c-format */
8f56d4fd
MR
12947 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"),
12948 sub, o->name);
c17d87de 12949
c152c796
AM
12950 /* But we also have to update some of the relocation
12951 info we collected before. */
12952 if (gc_sweep_hook
e8aaee2a 12953 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12954 && o->reloc_count != 0
12955 && !((info->strip == strip_all || info->strip == strip_debugger)
12956 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12957 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12958 {
12959 Elf_Internal_Rela *internal_relocs;
12960 bfd_boolean r;
12961
12962 internal_relocs
12963 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12964 info->keep_memory);
12965 if (internal_relocs == NULL)
12966 return FALSE;
12967
12968 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12969
12970 if (elf_section_data (o)->relocs != internal_relocs)
12971 free (internal_relocs);
12972
12973 if (!r)
12974 return FALSE;
12975 }
12976 }
12977 }
12978
c152c796
AM
12979 return TRUE;
12980}
12981
12982/* Propagate collected vtable information. This is called through
12983 elf_link_hash_traverse. */
12984
12985static bfd_boolean
12986elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12987{
c152c796 12988 /* Those that are not vtables. */
f6e332e6 12989 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12990 return TRUE;
12991
12992 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12993 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12994 return TRUE;
12995
12996 /* If we've already been done, exit. */
f6e332e6 12997 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12998 return TRUE;
12999
13000 /* Make sure the parent's table is up to date. */
f6e332e6 13001 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 13002
f6e332e6 13003 if (h->vtable->used == NULL)
c152c796
AM
13004 {
13005 /* None of this table's entries were referenced. Re-use the
13006 parent's table. */
f6e332e6
AM
13007 h->vtable->used = h->vtable->parent->vtable->used;
13008 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
13009 }
13010 else
13011 {
13012 size_t n;
13013 bfd_boolean *cu, *pu;
13014
13015 /* Or the parent's entries into ours. */
f6e332e6 13016 cu = h->vtable->used;
c152c796 13017 cu[-1] = TRUE;
f6e332e6 13018 pu = h->vtable->parent->vtable->used;
c152c796
AM
13019 if (pu != NULL)
13020 {
13021 const struct elf_backend_data *bed;
13022 unsigned int log_file_align;
13023
13024 bed = get_elf_backend_data (h->root.u.def.section->owner);
13025 log_file_align = bed->s->log_file_align;
f6e332e6 13026 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
13027 while (n--)
13028 {
13029 if (*pu)
13030 *cu = TRUE;
13031 pu++;
13032 cu++;
13033 }
13034 }
13035 }
13036
13037 return TRUE;
13038}
13039
13040static bfd_boolean
13041elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13042{
13043 asection *sec;
13044 bfd_vma hstart, hend;
13045 Elf_Internal_Rela *relstart, *relend, *rel;
13046 const struct elf_backend_data *bed;
13047 unsigned int log_file_align;
13048
c152c796
AM
13049 /* Take care of both those symbols that do not describe vtables as
13050 well as those that are not loaded. */
f6e332e6 13051 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
13052 return TRUE;
13053
13054 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13055 || h->root.type == bfd_link_hash_defweak);
13056
13057 sec = h->root.u.def.section;
13058 hstart = h->root.u.def.value;
13059 hend = hstart + h->size;
13060
13061 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13062 if (!relstart)
13063 return *(bfd_boolean *) okp = FALSE;
13064 bed = get_elf_backend_data (sec->owner);
13065 log_file_align = bed->s->log_file_align;
13066
13067 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13068
13069 for (rel = relstart; rel < relend; ++rel)
13070 if (rel->r_offset >= hstart && rel->r_offset < hend)
13071 {
13072 /* If the entry is in use, do nothing. */
f6e332e6
AM
13073 if (h->vtable->used
13074 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
13075 {
13076 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 13077 if (h->vtable->used[entry])
c152c796
AM
13078 continue;
13079 }
13080 /* Otherwise, kill it. */
13081 rel->r_offset = rel->r_info = rel->r_addend = 0;
13082 }
13083
13084 return TRUE;
13085}
13086
87538722
AM
13087/* Mark sections containing dynamically referenced symbols. When
13088 building shared libraries, we must assume that any visible symbol is
13089 referenced. */
715df9b8 13090
64d03ab5
AM
13091bfd_boolean
13092bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13093{
87538722 13094 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13095 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13096
715df9b8
EB
13097 if ((h->root.type == bfd_link_hash_defined
13098 || h->root.type == bfd_link_hash_defweak)
87538722 13099 && (h->ref_dynamic
c4621b33 13100 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13101 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13102 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13103 && (!bfd_link_executable (info)
22185505 13104 || info->gc_keep_exported
b407645f
AM
13105 || info->export_dynamic
13106 || (h->dynamic
13107 && d != NULL
13108 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13109 && (h->versioned >= versioned
54e8959c
L
13110 || !bfd_hide_sym_by_version (info->version_info,
13111 h->root.root.string)))))
715df9b8
EB
13112 h->root.u.def.section->flags |= SEC_KEEP;
13113
13114 return TRUE;
13115}
3b36f7e6 13116
74f0fb50
AM
13117/* Keep all sections containing symbols undefined on the command-line,
13118 and the section containing the entry symbol. */
13119
13120void
13121_bfd_elf_gc_keep (struct bfd_link_info *info)
13122{
13123 struct bfd_sym_chain *sym;
13124
13125 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13126 {
13127 struct elf_link_hash_entry *h;
13128
13129 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13130 FALSE, FALSE, FALSE);
13131
13132 if (h != NULL
13133 && (h->root.type == bfd_link_hash_defined
13134 || h->root.type == bfd_link_hash_defweak)
f02cb058
AM
13135 && !bfd_is_abs_section (h->root.u.def.section)
13136 && !bfd_is_und_section (h->root.u.def.section))
74f0fb50
AM
13137 h->root.u.def.section->flags |= SEC_KEEP;
13138 }
13139}
13140
2f0c68f2
CM
13141bfd_boolean
13142bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13143 struct bfd_link_info *info)
13144{
13145 bfd *ibfd = info->input_bfds;
13146
13147 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13148 {
13149 asection *sec;
13150 struct elf_reloc_cookie cookie;
13151
13152 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13153 continue;
13154
13155 if (!init_reloc_cookie (&cookie, info, ibfd))
13156 return FALSE;
13157
13158 for (sec = ibfd->sections; sec; sec = sec->next)
13159 {
13160 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13161 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13162 {
13163 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13164 fini_reloc_cookie_rels (&cookie, sec);
13165 }
13166 }
13167 }
13168 return TRUE;
13169}
13170
c152c796
AM
13171/* Do mark and sweep of unused sections. */
13172
13173bfd_boolean
13174bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13175{
13176 bfd_boolean ok = TRUE;
13177 bfd *sub;
6a5bb875 13178 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13179 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13180 struct elf_link_hash_table *htab;
c152c796 13181
64d03ab5 13182 if (!bed->can_gc_sections
715df9b8 13183 || !is_elf_hash_table (info->hash))
c152c796 13184 {
4eca0228 13185 _bfd_error_handler(_("Warning: gc-sections option ignored"));
c152c796
AM
13186 return TRUE;
13187 }
13188
74f0fb50 13189 bed->gc_keep (info);
da44f4e5 13190 htab = elf_hash_table (info);
74f0fb50 13191
9d0a14d3
RS
13192 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13193 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
13194 for (sub = info->input_bfds;
13195 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13196 sub = sub->link.next)
9d0a14d3
RS
13197 {
13198 asection *sec;
13199 struct elf_reloc_cookie cookie;
13200
13201 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 13202 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
13203 {
13204 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
13205 if (elf_section_data (sec)->sec_info
13206 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
13207 elf_eh_frame_section (sub) = sec;
13208 fini_reloc_cookie_for_section (&cookie, sec);
199af150 13209 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
13210 }
13211 }
9d0a14d3 13212
c152c796 13213 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 13214 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
13215 if (!ok)
13216 return FALSE;
13217
13218 /* Kill the vtable relocations that were not used. */
da44f4e5 13219 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
13220 if (!ok)
13221 return FALSE;
13222
715df9b8 13223 /* Mark dynamically referenced symbols. */
22185505 13224 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 13225 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 13226
715df9b8 13227 /* Grovel through relocs to find out who stays ... */
64d03ab5 13228 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 13229 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13230 {
13231 asection *o;
13232
b19a8f85
L
13233 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13234 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
13235 continue;
13236
7f6ab9f8
AM
13237 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13238 Also treat note sections as a root, if the section is not part
13239 of a group. */
c152c796 13240 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
13241 if (!o->gc_mark
13242 && (o->flags & SEC_EXCLUDE) == 0
24007750 13243 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
13244 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13245 && elf_next_in_group (o) == NULL )))
13246 {
13247 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13248 return FALSE;
13249 }
c152c796
AM
13250 }
13251
6a5bb875 13252 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 13253 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 13254
c152c796 13255 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 13256 return elf_gc_sweep (abfd, info);
c152c796
AM
13257}
13258\f
13259/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13260
13261bfd_boolean
13262bfd_elf_gc_record_vtinherit (bfd *abfd,
13263 asection *sec,
13264 struct elf_link_hash_entry *h,
13265 bfd_vma offset)
13266{
13267 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13268 struct elf_link_hash_entry **search, *child;
ef53be89 13269 size_t extsymcount;
c152c796
AM
13270 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13271
13272 /* The sh_info field of the symtab header tells us where the
13273 external symbols start. We don't care about the local symbols at
13274 this point. */
13275 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13276 if (!elf_bad_symtab (abfd))
13277 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13278
13279 sym_hashes = elf_sym_hashes (abfd);
13280 sym_hashes_end = sym_hashes + extsymcount;
13281
13282 /* Hunt down the child symbol, which is in this section at the same
13283 offset as the relocation. */
13284 for (search = sym_hashes; search != sym_hashes_end; ++search)
13285 {
13286 if ((child = *search) != NULL
13287 && (child->root.type == bfd_link_hash_defined
13288 || child->root.type == bfd_link_hash_defweak)
13289 && child->root.u.def.section == sec
13290 && child->root.u.def.value == offset)
13291 goto win;
13292 }
13293
695344c0
NC
13294 /* xgettext:c-format */
13295 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
4eca0228 13296 abfd, sec, (unsigned long) offset);
c152c796
AM
13297 bfd_set_error (bfd_error_invalid_operation);
13298 return FALSE;
13299
13300 win:
f6e332e6
AM
13301 if (!child->vtable)
13302 {
ca4be51c
AM
13303 child->vtable = ((struct elf_link_virtual_table_entry *)
13304 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
13305 if (!child->vtable)
13306 return FALSE;
13307 }
c152c796
AM
13308 if (!h)
13309 {
13310 /* This *should* only be the absolute section. It could potentially
13311 be that someone has defined a non-global vtable though, which
13312 would be bad. It isn't worth paging in the local symbols to be
13313 sure though; that case should simply be handled by the assembler. */
13314
f6e332e6 13315 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
13316 }
13317 else
f6e332e6 13318 child->vtable->parent = h;
c152c796
AM
13319
13320 return TRUE;
13321}
13322
13323/* Called from check_relocs to record the existence of a VTENTRY reloc. */
13324
13325bfd_boolean
13326bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13327 asection *sec ATTRIBUTE_UNUSED,
13328 struct elf_link_hash_entry *h,
13329 bfd_vma addend)
13330{
13331 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13332 unsigned int log_file_align = bed->s->log_file_align;
13333
f6e332e6
AM
13334 if (!h->vtable)
13335 {
ca4be51c
AM
13336 h->vtable = ((struct elf_link_virtual_table_entry *)
13337 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13338 if (!h->vtable)
13339 return FALSE;
13340 }
13341
13342 if (addend >= h->vtable->size)
c152c796
AM
13343 {
13344 size_t size, bytes, file_align;
f6e332e6 13345 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13346
13347 /* While the symbol is undefined, we have to be prepared to handle
13348 a zero size. */
13349 file_align = 1 << log_file_align;
13350 if (h->root.type == bfd_link_hash_undefined)
13351 size = addend + file_align;
13352 else
13353 {
13354 size = h->size;
13355 if (addend >= size)
13356 {
13357 /* Oops! We've got a reference past the defined end of
13358 the table. This is probably a bug -- shall we warn? */
13359 size = addend + file_align;
13360 }
13361 }
13362 size = (size + file_align - 1) & -file_align;
13363
13364 /* Allocate one extra entry for use as a "done" flag for the
13365 consolidation pass. */
13366 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13367
13368 if (ptr)
13369 {
a50b1753 13370 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13371
13372 if (ptr != NULL)
13373 {
13374 size_t oldbytes;
13375
f6e332e6 13376 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13377 * sizeof (bfd_boolean));
13378 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13379 }
13380 }
13381 else
a50b1753 13382 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13383
13384 if (ptr == NULL)
13385 return FALSE;
13386
13387 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13388 h->vtable->used = ptr + 1;
13389 h->vtable->size = size;
c152c796
AM
13390 }
13391
f6e332e6 13392 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13393
13394 return TRUE;
13395}
13396
ae17ab41
CM
13397/* Map an ELF section header flag to its corresponding string. */
13398typedef struct
13399{
13400 char *flag_name;
13401 flagword flag_value;
13402} elf_flags_to_name_table;
13403
13404static elf_flags_to_name_table elf_flags_to_names [] =
13405{
13406 { "SHF_WRITE", SHF_WRITE },
13407 { "SHF_ALLOC", SHF_ALLOC },
13408 { "SHF_EXECINSTR", SHF_EXECINSTR },
13409 { "SHF_MERGE", SHF_MERGE },
13410 { "SHF_STRINGS", SHF_STRINGS },
13411 { "SHF_INFO_LINK", SHF_INFO_LINK},
13412 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13413 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13414 { "SHF_GROUP", SHF_GROUP },
13415 { "SHF_TLS", SHF_TLS },
13416 { "SHF_MASKOS", SHF_MASKOS },
13417 { "SHF_EXCLUDE", SHF_EXCLUDE },
13418};
13419
b9c361e0
JL
13420/* Returns TRUE if the section is to be included, otherwise FALSE. */
13421bfd_boolean
ae17ab41 13422bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13423 struct flag_info *flaginfo,
b9c361e0 13424 asection *section)
ae17ab41 13425{
8b127cbc 13426 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13427
8b127cbc 13428 if (!flaginfo->flags_initialized)
ae17ab41 13429 {
8b127cbc
AM
13430 bfd *obfd = info->output_bfd;
13431 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13432 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13433 int with_hex = 0;
13434 int without_hex = 0;
13435
8b127cbc 13436 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13437 {
b9c361e0 13438 unsigned i;
8b127cbc 13439 flagword (*lookup) (char *);
ae17ab41 13440
8b127cbc
AM
13441 lookup = bed->elf_backend_lookup_section_flags_hook;
13442 if (lookup != NULL)
ae17ab41 13443 {
8b127cbc 13444 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13445
13446 if (hexval != 0)
13447 {
13448 if (tf->with == with_flags)
13449 with_hex |= hexval;
13450 else if (tf->with == without_flags)
13451 without_hex |= hexval;
13452 tf->valid = TRUE;
13453 continue;
13454 }
ae17ab41 13455 }
8b127cbc 13456 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13457 {
8b127cbc 13458 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13459 {
13460 if (tf->with == with_flags)
13461 with_hex |= elf_flags_to_names[i].flag_value;
13462 else if (tf->with == without_flags)
13463 without_hex |= elf_flags_to_names[i].flag_value;
13464 tf->valid = TRUE;
13465 break;
13466 }
13467 }
8b127cbc 13468 if (!tf->valid)
b9c361e0 13469 {
68ffbac6 13470 info->callbacks->einfo
8b127cbc 13471 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13472 return FALSE;
ae17ab41
CM
13473 }
13474 }
8b127cbc
AM
13475 flaginfo->flags_initialized = TRUE;
13476 flaginfo->only_with_flags |= with_hex;
13477 flaginfo->not_with_flags |= without_hex;
ae17ab41 13478 }
ae17ab41 13479
8b127cbc 13480 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13481 return FALSE;
13482
8b127cbc 13483 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13484 return FALSE;
13485
13486 return TRUE;
ae17ab41
CM
13487}
13488
c152c796
AM
13489struct alloc_got_off_arg {
13490 bfd_vma gotoff;
10455f89 13491 struct bfd_link_info *info;
c152c796
AM
13492};
13493
13494/* We need a special top-level link routine to convert got reference counts
13495 to real got offsets. */
13496
13497static bfd_boolean
13498elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13499{
a50b1753 13500 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13501 bfd *obfd = gofarg->info->output_bfd;
13502 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13503
c152c796
AM
13504 if (h->got.refcount > 0)
13505 {
13506 h->got.offset = gofarg->gotoff;
10455f89 13507 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13508 }
13509 else
13510 h->got.offset = (bfd_vma) -1;
13511
13512 return TRUE;
13513}
13514
13515/* And an accompanying bit to work out final got entry offsets once
13516 we're done. Should be called from final_link. */
13517
13518bfd_boolean
13519bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13520 struct bfd_link_info *info)
13521{
13522 bfd *i;
13523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13524 bfd_vma gotoff;
c152c796
AM
13525 struct alloc_got_off_arg gofarg;
13526
10455f89
HPN
13527 BFD_ASSERT (abfd == info->output_bfd);
13528
c152c796
AM
13529 if (! is_elf_hash_table (info->hash))
13530 return FALSE;
13531
13532 /* The GOT offset is relative to the .got section, but the GOT header is
13533 put into the .got.plt section, if the backend uses it. */
13534 if (bed->want_got_plt)
13535 gotoff = 0;
13536 else
13537 gotoff = bed->got_header_size;
13538
13539 /* Do the local .got entries first. */
c72f2fb2 13540 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13541 {
13542 bfd_signed_vma *local_got;
ef53be89 13543 size_t j, locsymcount;
c152c796
AM
13544 Elf_Internal_Shdr *symtab_hdr;
13545
13546 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13547 continue;
13548
13549 local_got = elf_local_got_refcounts (i);
13550 if (!local_got)
13551 continue;
13552
13553 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13554 if (elf_bad_symtab (i))
13555 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13556 else
13557 locsymcount = symtab_hdr->sh_info;
13558
13559 for (j = 0; j < locsymcount; ++j)
13560 {
13561 if (local_got[j] > 0)
13562 {
13563 local_got[j] = gotoff;
10455f89 13564 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13565 }
13566 else
13567 local_got[j] = (bfd_vma) -1;
13568 }
13569 }
13570
13571 /* Then the global .got entries. .plt refcounts are handled by
13572 adjust_dynamic_symbol */
13573 gofarg.gotoff = gotoff;
10455f89 13574 gofarg.info = info;
c152c796
AM
13575 elf_link_hash_traverse (elf_hash_table (info),
13576 elf_gc_allocate_got_offsets,
13577 &gofarg);
13578 return TRUE;
13579}
13580
13581/* Many folk need no more in the way of final link than this, once
13582 got entry reference counting is enabled. */
13583
13584bfd_boolean
13585bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13586{
13587 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13588 return FALSE;
13589
13590 /* Invoke the regular ELF backend linker to do all the work. */
13591 return bfd_elf_final_link (abfd, info);
13592}
13593
13594bfd_boolean
13595bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13596{
a50b1753 13597 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13598
13599 if (rcookie->bad_symtab)
13600 rcookie->rel = rcookie->rels;
13601
13602 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13603 {
13604 unsigned long r_symndx;
13605
13606 if (! rcookie->bad_symtab)
13607 if (rcookie->rel->r_offset > offset)
13608 return FALSE;
13609 if (rcookie->rel->r_offset != offset)
13610 continue;
13611
13612 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13613 if (r_symndx == STN_UNDEF)
c152c796
AM
13614 return TRUE;
13615
13616 if (r_symndx >= rcookie->locsymcount
13617 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13618 {
13619 struct elf_link_hash_entry *h;
13620
13621 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13622
13623 while (h->root.type == bfd_link_hash_indirect
13624 || h->root.type == bfd_link_hash_warning)
13625 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13626
13627 if ((h->root.type == bfd_link_hash_defined
13628 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13629 && (h->root.u.def.section->owner != rcookie->abfd
13630 || h->root.u.def.section->kept_section != NULL
13631 || discarded_section (h->root.u.def.section)))
c152c796 13632 return TRUE;
c152c796
AM
13633 }
13634 else
13635 {
13636 /* It's not a relocation against a global symbol,
13637 but it could be a relocation against a local
13638 symbol for a discarded section. */
13639 asection *isec;
13640 Elf_Internal_Sym *isym;
13641
13642 /* Need to: get the symbol; get the section. */
13643 isym = &rcookie->locsyms[r_symndx];
cb33740c 13644 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13645 if (isec != NULL
13646 && (isec->kept_section != NULL
13647 || discarded_section (isec)))
cb33740c 13648 return TRUE;
c152c796
AM
13649 }
13650 return FALSE;
13651 }
13652 return FALSE;
13653}
13654
13655/* Discard unneeded references to discarded sections.
75938853
AM
13656 Returns -1 on error, 1 if any section's size was changed, 0 if
13657 nothing changed. This function assumes that the relocations are in
13658 sorted order, which is true for all known assemblers. */
c152c796 13659
75938853 13660int
c152c796
AM
13661bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13662{
13663 struct elf_reloc_cookie cookie;
18cd5bce 13664 asection *o;
c152c796 13665 bfd *abfd;
75938853 13666 int changed = 0;
c152c796
AM
13667
13668 if (info->traditional_format
13669 || !is_elf_hash_table (info->hash))
75938853 13670 return 0;
c152c796 13671
18cd5bce
AM
13672 o = bfd_get_section_by_name (output_bfd, ".stab");
13673 if (o != NULL)
c152c796 13674 {
18cd5bce 13675 asection *i;
c152c796 13676
18cd5bce 13677 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13678 {
18cd5bce
AM
13679 if (i->size == 0
13680 || i->reloc_count == 0
13681 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13682 continue;
c152c796 13683
18cd5bce
AM
13684 abfd = i->owner;
13685 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13686 continue;
c152c796 13687
18cd5bce 13688 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13689 return -1;
c152c796 13690
18cd5bce
AM
13691 if (_bfd_discard_section_stabs (abfd, i,
13692 elf_section_data (i)->sec_info,
5241d853
RS
13693 bfd_elf_reloc_symbol_deleted_p,
13694 &cookie))
75938853 13695 changed = 1;
18cd5bce
AM
13696
13697 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13698 }
18cd5bce
AM
13699 }
13700
2f0c68f2
CM
13701 o = NULL;
13702 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13703 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13704 if (o != NULL)
13705 {
13706 asection *i;
c152c796 13707
18cd5bce 13708 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13709 {
18cd5bce
AM
13710 if (i->size == 0)
13711 continue;
13712
13713 abfd = i->owner;
13714 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13715 continue;
13716
13717 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13718 return -1;
18cd5bce
AM
13719
13720 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13721 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13722 bfd_elf_reloc_symbol_deleted_p,
13723 &cookie))
75938853 13724 changed = 1;
18cd5bce
AM
13725
13726 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13727 }
18cd5bce 13728 }
c152c796 13729
18cd5bce
AM
13730 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13731 {
13732 const struct elf_backend_data *bed;
c152c796 13733
18cd5bce
AM
13734 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13735 continue;
13736
13737 bed = get_elf_backend_data (abfd);
13738
13739 if (bed->elf_backend_discard_info != NULL)
13740 {
13741 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13742 return -1;
18cd5bce
AM
13743
13744 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13745 changed = 1;
18cd5bce
AM
13746
13747 fini_reloc_cookie (&cookie, abfd);
13748 }
c152c796
AM
13749 }
13750
2f0c68f2
CM
13751 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13752 _bfd_elf_end_eh_frame_parsing (info);
13753
13754 if (info->eh_frame_hdr_type
0e1862bb 13755 && !bfd_link_relocatable (info)
c152c796 13756 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13757 changed = 1;
c152c796 13758
75938853 13759 return changed;
c152c796 13760}
082b7297 13761
43e1669b 13762bfd_boolean
0c511000 13763_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13764 asection *sec,
c0f00686 13765 struct bfd_link_info *info)
082b7297
L
13766{
13767 flagword flags;
c77ec726 13768 const char *name, *key;
082b7297
L
13769 struct bfd_section_already_linked *l;
13770 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13771
c77ec726
AM
13772 if (sec->output_section == bfd_abs_section_ptr)
13773 return FALSE;
0c511000 13774
c77ec726 13775 flags = sec->flags;
0c511000 13776
c77ec726
AM
13777 /* Return if it isn't a linkonce section. A comdat group section
13778 also has SEC_LINK_ONCE set. */
13779 if ((flags & SEC_LINK_ONCE) == 0)
13780 return FALSE;
0c511000 13781
c77ec726
AM
13782 /* Don't put group member sections on our list of already linked
13783 sections. They are handled as a group via their group section. */
13784 if (elf_sec_group (sec) != NULL)
13785 return FALSE;
0c511000 13786
c77ec726
AM
13787 /* For a SHT_GROUP section, use the group signature as the key. */
13788 name = sec->name;
13789 if ((flags & SEC_GROUP) != 0
13790 && elf_next_in_group (sec) != NULL
13791 && elf_group_name (elf_next_in_group (sec)) != NULL)
13792 key = elf_group_name (elf_next_in_group (sec));
13793 else
13794 {
13795 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13796 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13797 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13798 key++;
0c511000 13799 else
c77ec726
AM
13800 /* Must be a user linkonce section that doesn't follow gcc's
13801 naming convention. In this case we won't be matching
13802 single member groups. */
13803 key = name;
0c511000 13804 }
6d2cd210 13805
c77ec726 13806 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13807
13808 for (l = already_linked_list->entry; l != NULL; l = l->next)
13809 {
c2370991 13810 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13811 sections with a signature of <key> (<key> is some string),
13812 and linkonce sections named .gnu.linkonce.<type>.<key>.
13813 Match like sections. LTO plugin sections are an exception.
13814 They are always named .gnu.linkonce.t.<key> and match either
13815 type of section. */
13816 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13817 && ((flags & SEC_GROUP) != 0
13818 || strcmp (name, l->sec->name) == 0))
13819 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13820 {
13821 /* The section has already been linked. See if we should
6d2cd210 13822 issue a warning. */
c77ec726
AM
13823 if (!_bfd_handle_already_linked (sec, l, info))
13824 return FALSE;
082b7297 13825
c77ec726 13826 if (flags & SEC_GROUP)
3d7f7666 13827 {
c77ec726
AM
13828 asection *first = elf_next_in_group (sec);
13829 asection *s = first;
3d7f7666 13830
c77ec726 13831 while (s != NULL)
3d7f7666 13832 {
c77ec726
AM
13833 s->output_section = bfd_abs_section_ptr;
13834 /* Record which group discards it. */
13835 s->kept_section = l->sec;
13836 s = elf_next_in_group (s);
13837 /* These lists are circular. */
13838 if (s == first)
13839 break;
3d7f7666
L
13840 }
13841 }
082b7297 13842
43e1669b 13843 return TRUE;
082b7297
L
13844 }
13845 }
13846
c77ec726
AM
13847 /* A single member comdat group section may be discarded by a
13848 linkonce section and vice versa. */
13849 if ((flags & SEC_GROUP) != 0)
3d7f7666 13850 {
c77ec726 13851 asection *first = elf_next_in_group (sec);
c2370991 13852
c77ec726
AM
13853 if (first != NULL && elf_next_in_group (first) == first)
13854 /* Check this single member group against linkonce sections. */
13855 for (l = already_linked_list->entry; l != NULL; l = l->next)
13856 if ((l->sec->flags & SEC_GROUP) == 0
13857 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13858 {
13859 first->output_section = bfd_abs_section_ptr;
13860 first->kept_section = l->sec;
13861 sec->output_section = bfd_abs_section_ptr;
13862 break;
13863 }
13864 }
13865 else
13866 /* Check this linkonce section against single member groups. */
13867 for (l = already_linked_list->entry; l != NULL; l = l->next)
13868 if (l->sec->flags & SEC_GROUP)
6d2cd210 13869 {
c77ec726 13870 asection *first = elf_next_in_group (l->sec);
6d2cd210 13871
c77ec726
AM
13872 if (first != NULL
13873 && elf_next_in_group (first) == first
13874 && bfd_elf_match_symbols_in_sections (first, sec, info))
13875 {
13876 sec->output_section = bfd_abs_section_ptr;
13877 sec->kept_section = first;
13878 break;
13879 }
6d2cd210 13880 }
0c511000 13881
c77ec726
AM
13882 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13883 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13884 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13885 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13886 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13887 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13888 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13889 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13890 The reverse order cannot happen as there is never a bfd with only the
13891 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13892 matter as here were are looking only for cross-bfd sections. */
13893
13894 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13895 for (l = already_linked_list->entry; l != NULL; l = l->next)
13896 if ((l->sec->flags & SEC_GROUP) == 0
13897 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13898 {
13899 if (abfd != l->sec->owner)
13900 sec->output_section = bfd_abs_section_ptr;
13901 break;
13902 }
80c29487 13903
082b7297 13904 /* This is the first section with this name. Record it. */
c77ec726 13905 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13906 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13907 return sec->output_section == bfd_abs_section_ptr;
082b7297 13908}
81e1b023 13909
a4d8e49b
L
13910bfd_boolean
13911_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13912{
13913 return sym->st_shndx == SHN_COMMON;
13914}
13915
13916unsigned int
13917_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13918{
13919 return SHN_COMMON;
13920}
13921
13922asection *
13923_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13924{
13925 return bfd_com_section_ptr;
13926}
10455f89
HPN
13927
13928bfd_vma
13929_bfd_elf_default_got_elt_size (bfd *abfd,
13930 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13931 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13932 bfd *ibfd ATTRIBUTE_UNUSED,
13933 unsigned long symndx ATTRIBUTE_UNUSED)
13934{
13935 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13936 return bed->s->arch_size / 8;
13937}
83bac4b0
NC
13938
13939/* Routines to support the creation of dynamic relocs. */
13940
83bac4b0
NC
13941/* Returns the name of the dynamic reloc section associated with SEC. */
13942
13943static const char *
13944get_dynamic_reloc_section_name (bfd * abfd,
13945 asection * sec,
13946 bfd_boolean is_rela)
13947{
ddcf1fcf
BS
13948 char *name;
13949 const char *old_name = bfd_get_section_name (NULL, sec);
13950 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13951
ddcf1fcf 13952 if (old_name == NULL)
83bac4b0
NC
13953 return NULL;
13954
ddcf1fcf 13955 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13956 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13957
13958 return name;
13959}
13960
13961/* Returns the dynamic reloc section associated with SEC.
13962 If necessary compute the name of the dynamic reloc section based
13963 on SEC's name (looked up in ABFD's string table) and the setting
13964 of IS_RELA. */
13965
13966asection *
13967_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13968 asection * sec,
13969 bfd_boolean is_rela)
13970{
13971 asection * reloc_sec = elf_section_data (sec)->sreloc;
13972
13973 if (reloc_sec == NULL)
13974 {
13975 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13976
13977 if (name != NULL)
13978 {
3d4d4302 13979 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13980
13981 if (reloc_sec != NULL)
13982 elf_section_data (sec)->sreloc = reloc_sec;
13983 }
13984 }
13985
13986 return reloc_sec;
13987}
13988
13989/* Returns the dynamic reloc section associated with SEC. If the
13990 section does not exist it is created and attached to the DYNOBJ
13991 bfd and stored in the SRELOC field of SEC's elf_section_data
13992 structure.
f8076f98 13993
83bac4b0
NC
13994 ALIGNMENT is the alignment for the newly created section and
13995 IS_RELA defines whether the name should be .rela.<SEC's name>
13996 or .rel.<SEC's name>. The section name is looked up in the
13997 string table associated with ABFD. */
13998
13999asection *
ca4be51c
AM
14000_bfd_elf_make_dynamic_reloc_section (asection *sec,
14001 bfd *dynobj,
14002 unsigned int alignment,
14003 bfd *abfd,
14004 bfd_boolean is_rela)
83bac4b0
NC
14005{
14006 asection * reloc_sec = elf_section_data (sec)->sreloc;
14007
14008 if (reloc_sec == NULL)
14009 {
14010 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14011
14012 if (name == NULL)
14013 return NULL;
14014
3d4d4302 14015 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14016
14017 if (reloc_sec == NULL)
14018 {
3d4d4302
AM
14019 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14020 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14021 if ((sec->flags & SEC_ALLOC) != 0)
14022 flags |= SEC_ALLOC | SEC_LOAD;
14023
3d4d4302 14024 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14025 if (reloc_sec != NULL)
14026 {
8877b5e5
AM
14027 /* _bfd_elf_get_sec_type_attr chooses a section type by
14028 name. Override as it may be wrong, eg. for a user
14029 section named "auto" we'll get ".relauto" which is
14030 seen to be a .rela section. */
14031 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
14032 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14033 reloc_sec = NULL;
14034 }
14035 }
14036
14037 elf_section_data (sec)->sreloc = reloc_sec;
14038 }
14039
14040 return reloc_sec;
14041}
1338dd10 14042
bffebb6b
AM
14043/* Copy the ELF symbol type and other attributes for a linker script
14044 assignment from HSRC to HDEST. Generally this should be treated as
14045 if we found a strong non-dynamic definition for HDEST (except that
14046 ld ignores multiple definition errors). */
1338dd10 14047void
bffebb6b
AM
14048_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14049 struct bfd_link_hash_entry *hdest,
14050 struct bfd_link_hash_entry *hsrc)
1338dd10 14051{
bffebb6b
AM
14052 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14053 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14054 Elf_Internal_Sym isym;
1338dd10
PB
14055
14056 ehdest->type = ehsrc->type;
35fc36a8 14057 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14058
14059 isym.st_other = ehsrc->other;
b8417128 14060 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 14061}
351f65ca
L
14062
14063/* Append a RELA relocation REL to section S in BFD. */
14064
14065void
14066elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14067{
14068 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14069 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14070 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14071 bed->s->swap_reloca_out (abfd, rel, loc);
14072}
14073
14074/* Append a REL relocation REL to section S in BFD. */
14075
14076void
14077elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14078{
14079 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14080 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14081 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14082 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14083}
This page took 2.530729 seconds and 4 git commands to generate.