Handle MIPS Linux SIGTRAP siginfo.si_code values
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
6f2750fe 2 Copyright (C) 1995-2016 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"
252b5132 31
28caa186
AM
32/* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35struct elf_info_failed
36{
37 struct bfd_link_info *info;
28caa186
AM
38 bfd_boolean failed;
39};
40
41/* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44struct elf_find_verdep_info
45{
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52};
53
54static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
2f0c68f2
CM
57asection *
58_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61{
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96}
97
d98685ac
AM
98/* Define a symbol in a dynamic linkage section. */
99
100struct elf_link_hash_entry *
101_bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105{
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
ccabcbe5 108 const struct elf_backend_data *bed;
d98685ac
AM
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
cf18fda4 121 bed = get_elf_backend_data (abfd);
d98685ac 122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 123 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
e28df02b 128 h->non_elf = 0;
12b2843a 129 h->root.linker_def = 1;
d98685ac 130 h->type = STT_OBJECT;
00b7642b
AM
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 133
ccabcbe5 134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
135 return h;
136}
137
b34976b6 138bfd_boolean
268b6b39 139_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
140{
141 flagword flags;
aad5d350 142 asection *s;
252b5132 143 struct elf_link_hash_entry *h;
9c5bfbb7 144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 145 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
146
147 /* This function may be called more than once. */
3d4d4302
AM
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
b34976b6 150 return TRUE;
252b5132 151
e5a52504 152 flags = bed->dynamic_sec_flags;
252b5132 153
14b2f831
AM
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
6de2ae4a
L
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
252b5132 163
14b2f831 164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d
L
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
252b5132
RH
170 if (bed->want_got_plt)
171 {
14b2f831 172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 173 if (s == NULL
6de2ae4a
L
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
b34976b6 176 return FALSE;
6de2ae4a 177 htab->sgotplt = s;
252b5132
RH
178 }
179
64e77c6d
L
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
2517a57f
AM
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
6de2ae4a
L
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
2517a57f 191 elf_hash_table (info)->hgot = h;
d98685ac
AM
192 if (h == NULL)
193 return FALSE;
2517a57f 194 }
252b5132 195
b34976b6 196 return TRUE;
252b5132
RH
197}
198\f
7e9f0867
AM
199/* Create a strtab to hold the dynamic symbol names. */
200static bfd_boolean
201_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202{
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216}
217
45d6a902
AM
218/* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
252b5132 224
b34976b6 225bfd_boolean
268b6b39 226_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 227{
45d6a902 228 flagword flags;
91d6fa6a 229 asection *s;
9c5bfbb7 230 const struct elf_backend_data *bed;
9637f6ef 231 struct elf_link_hash_entry *h;
252b5132 232
0eddce27 233 if (! is_elf_hash_table (info->hash))
45d6a902
AM
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
7e9f0867
AM
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
45d6a902 241
7e9f0867 242 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
45d6a902
AM
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
9b8b325a 249 if (bfd_link_executable (info) && !info->nointerp)
252b5132 250 {
14b2f831
AM
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
3496cb2a 253 if (s == NULL)
45d6a902
AM
254 return FALSE;
255 }
bb0deeff 256
45d6a902
AM
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
14b2f831
AM
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
45d6a902 261 if (s == NULL
45d6a902
AM
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
14b2f831
AM
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
45d6a902 267 if (s == NULL
45d6a902
AM
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
14b2f831
AM
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
45d6a902 273 if (s == NULL
45d6a902
AM
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
14b2f831
AM
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
45d6a902 279 if (s == NULL
45d6a902
AM
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
cae1fbbb 282 elf_hash_table (info)->dynsym = s;
45d6a902 283
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288
14b2f831 289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 290 if (s == NULL
45d6a902
AM
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
9637f6ef
L
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
45d6a902
AM
303 return FALSE;
304
fdc90cb4
JJ
305 if (info->emit_hash)
306 {
14b2f831
AM
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
fdc90cb4
JJ
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
fdc90cb4
JJ
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
45d6a902
AM
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
894891db
NC
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341}
342
343/* Create dynamic sections when linking against a dynamic object. */
344
345bfd_boolean
268b6b39 346_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
347{
348 flagword flags, pltflags;
7325306f 349 struct elf_link_hash_entry *h;
45d6a902 350 asection *s;
9c5bfbb7 351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 352 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 353
252b5132
RH
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
e5a52504 356 flags = bed->dynamic_sec_flags;
252b5132
RH
357
358 pltflags = flags;
252b5132 359 if (bed->plt_not_loaded)
6df4d94c
MM
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
5d1634d7 363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
14b2f831 369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 370 if (s == NULL
252b5132 371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 372 return FALSE;
6de2ae4a 373 htab->splt = s;
252b5132 374
d98685ac
AM
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
7325306f
RS
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
252b5132 385
14b2f831
AM
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
252b5132 390 if (s == NULL
45d6a902 391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 392 return FALSE;
6de2ae4a 393 htab->srelplt = s;
252b5132
RH
394
395 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 396 return FALSE;
252b5132 397
3018b441
RH
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
14b2f831
AM
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
3496cb2a 408 if (s == NULL)
b34976b6 409 return FALSE;
252b5132 410
3018b441 411 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
0e1862bb 422 if (! bfd_link_pic (info))
3018b441 423 {
14b2f831
AM
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
3018b441 428 if (s == NULL
45d6a902 429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 430 return FALSE;
3018b441 431 }
252b5132
RH
432 }
433
b34976b6 434 return TRUE;
252b5132
RH
435}
436\f
252b5132
RH
437/* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
b34976b6 445bfd_boolean
c152c796
AM
446bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
252b5132
RH
448{
449 if (h->dynindx == -1)
450 {
2b0f7ef9 451 struct elf_strtab_hash *dynstr;
68b6ddd0 452 char *p;
252b5132 453 const char *name;
252b5132
RH
454 bfd_size_type indx;
455
7a13edea
NC
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
9d6eee78
L
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
38048eb9 466 {
f5385ebf 467 h->forced_local = 1;
67687978
PB
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
7a13edea 470 }
0444bdd4 471
7a13edea
NC
472 default:
473 break;
474 }
475
252b5132
RH
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 484 if (dynstr == NULL)
b34976b6 485 return FALSE;
252b5132
RH
486 }
487
488 /* We don't put any version information in the dynamic string
aad5d350 489 table. */
252b5132
RH
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
252b5132
RH
504
505 if (indx == (bfd_size_type) -1)
b34976b6 506 return FALSE;
252b5132
RH
507 h->dynstr_index = indx;
508 }
509
b34976b6 510 return TRUE;
252b5132 511}
45d6a902 512\f
55255dae
L
513/* Mark a symbol dynamic. */
514
28caa186 515static void
55255dae 516bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
55255dae 519{
40b36307 520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 521
40b36307 522 /* It may be called more than once on the same H. */
0e1862bb 523 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
524 return;
525
40b36307
L
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || (sym != NULL
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
a0c8462f 530 || (d != NULL
40b36307
L
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
533 h->dynamic = 1;
534}
535
45d6a902
AM
536/* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
538
539bfd_boolean
fe21a8fc
L
540bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
268b6b39 542 const char *name,
fe21a8fc
L
543 bfd_boolean provide,
544 bfd_boolean hidden)
45d6a902 545{
00cbee0a 546 struct elf_link_hash_entry *h, *hv;
4ea42fb7 547 struct elf_link_hash_table *htab;
00cbee0a 548 const struct elf_backend_data *bed;
45d6a902 549
0eddce27 550 if (!is_elf_hash_table (info->hash))
45d6a902
AM
551 return TRUE;
552
4ea42fb7
AM
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 555 if (h == NULL)
4ea42fb7 556 return provide;
45d6a902 557
00cbee0a 558 switch (h->root.type)
77cfaee6 559 {
00cbee0a
L
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
563 break;
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
4ea42fb7 569 h->root.type = bfd_link_hash_new;
77cfaee6
AM
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
572 break;
573 case bfd_link_hash_new:
40b36307 574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 575 h->non_elf = 0;
00cbee0a
L
576 break;
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 579 the versioned symbol point to this one. */
00cbee0a
L
580 bed = get_elf_backend_data (output_bfd);
581 hv = h;
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
586 later. */
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591 break;
592 case bfd_link_hash_warning:
593 abort ();
594 break;
55255dae 595 }
45d6a902
AM
596
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
601 if (provide
f5385ebf
AM
602 && h->def_dynamic
603 && !h->def_regular)
45d6a902
AM
604 h->root.type = bfd_link_hash_undefined;
605
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
610 if (!provide
f5385ebf
AM
611 && h->def_dynamic
612 && !h->def_regular)
45d6a902
AM
613 h->verinfo.verdef = NULL;
614
f5385ebf 615 h->def_regular = 1;
45d6a902 616
eb8476a6 617 if (hidden)
fe21a8fc 618 {
91d6fa6a 619 bed = get_elf_backend_data (output_bfd);
b8297068
AM
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
623 }
624
6fa3860b
PB
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626 and executables. */
0e1862bb 627 if (!bfd_link_relocatable (info)
6fa3860b
PB
628 && h->dynindx != -1
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
631 h->forced_local = 1;
632
f5385ebf
AM
633 if ((h->def_dynamic
634 || h->ref_dynamic
6b3b0ab8
L
635 || bfd_link_dll (info)
636 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
637 && h->dynindx == -1)
638 {
c152c796 639 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
640 return FALSE;
641
642 /* If this is a weak defined symbol, and we know a corresponding
643 real symbol from the same dynamic object, make sure the real
644 symbol is also made into a dynamic symbol. */
f6e332e6
AM
645 if (h->u.weakdef != NULL
646 && h->u.weakdef->dynindx == -1)
45d6a902 647 {
f6e332e6 648 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
649 return FALSE;
650 }
651 }
652
653 return TRUE;
654}
42751cf3 655
8c58d23b
AM
656/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
657 success, and 2 on a failure caused by attempting to record a symbol
658 in a discarded section, eg. a discarded link-once section symbol. */
659
660int
c152c796
AM
661bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
662 bfd *input_bfd,
663 long input_indx)
8c58d23b
AM
664{
665 bfd_size_type amt;
666 struct elf_link_local_dynamic_entry *entry;
667 struct elf_link_hash_table *eht;
668 struct elf_strtab_hash *dynstr;
669 unsigned long dynstr_index;
670 char *name;
671 Elf_External_Sym_Shndx eshndx;
672 char esym[sizeof (Elf64_External_Sym)];
673
0eddce27 674 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
675 return 0;
676
677 /* See if the entry exists already. */
678 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
679 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
680 return 1;
681
682 amt = sizeof (*entry);
a50b1753 683 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
684 if (entry == NULL)
685 return 0;
686
687 /* Go find the symbol, so that we can find it's name. */
688 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 689 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
690 {
691 bfd_release (input_bfd, entry);
692 return 0;
693 }
694
695 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 696 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
697 {
698 asection *s;
699
700 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
701 if (s == NULL || bfd_is_abs_section (s->output_section))
702 {
703 /* We can still bfd_release here as nothing has done another
704 bfd_alloc. We can't do this later in this function. */
705 bfd_release (input_bfd, entry);
706 return 2;
707 }
708 }
709
710 name = (bfd_elf_string_from_elf_section
711 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
712 entry->isym.st_name));
713
714 dynstr = elf_hash_table (info)->dynstr;
715 if (dynstr == NULL)
716 {
717 /* Create a strtab to hold the dynamic symbol names. */
718 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
719 if (dynstr == NULL)
720 return 0;
721 }
722
b34976b6 723 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
724 if (dynstr_index == (unsigned long) -1)
725 return 0;
726 entry->isym.st_name = dynstr_index;
727
728 eht = elf_hash_table (info);
729
730 entry->next = eht->dynlocal;
731 eht->dynlocal = entry;
732 entry->input_bfd = input_bfd;
733 entry->input_indx = input_indx;
734 eht->dynsymcount++;
735
736 /* Whatever binding the symbol had before, it's now local. */
737 entry->isym.st_info
738 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
739
740 /* The dynindx will be set at the end of size_dynamic_sections. */
741
742 return 1;
743}
744
30b30c21 745/* Return the dynindex of a local dynamic symbol. */
42751cf3 746
30b30c21 747long
268b6b39
AM
748_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
749 bfd *input_bfd,
750 long input_indx)
30b30c21
RH
751{
752 struct elf_link_local_dynamic_entry *e;
753
754 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
755 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
756 return e->dynindx;
757 return -1;
758}
759
760/* This function is used to renumber the dynamic symbols, if some of
761 them are removed because they are marked as local. This is called
762 via elf_link_hash_traverse. */
763
b34976b6 764static bfd_boolean
268b6b39
AM
765elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
766 void *data)
42751cf3 767{
a50b1753 768 size_t *count = (size_t *) data;
30b30c21 769
6fa3860b
PB
770 if (h->forced_local)
771 return TRUE;
772
773 if (h->dynindx != -1)
774 h->dynindx = ++(*count);
775
776 return TRUE;
777}
778
779
780/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
781 STB_LOCAL binding. */
782
783static bfd_boolean
784elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
785 void *data)
786{
a50b1753 787 size_t *count = (size_t *) data;
6fa3860b 788
6fa3860b
PB
789 if (!h->forced_local)
790 return TRUE;
791
42751cf3 792 if (h->dynindx != -1)
30b30c21
RH
793 h->dynindx = ++(*count);
794
b34976b6 795 return TRUE;
42751cf3 796}
30b30c21 797
aee6f5b4
AO
798/* Return true if the dynamic symbol for a given section should be
799 omitted when creating a shared library. */
800bfd_boolean
801_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
802 struct bfd_link_info *info,
803 asection *p)
804{
74541ad4 805 struct elf_link_hash_table *htab;
ca55926c 806 asection *ip;
74541ad4 807
aee6f5b4
AO
808 switch (elf_section_data (p)->this_hdr.sh_type)
809 {
810 case SHT_PROGBITS:
811 case SHT_NOBITS:
812 /* If sh_type is yet undecided, assume it could be
813 SHT_PROGBITS/SHT_NOBITS. */
814 case SHT_NULL:
74541ad4
AM
815 htab = elf_hash_table (info);
816 if (p == htab->tls_sec)
817 return FALSE;
818
819 if (htab->text_index_section != NULL)
820 return p != htab->text_index_section && p != htab->data_index_section;
821
ca55926c 822 return (htab->dynobj != NULL
3d4d4302 823 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 824 && ip->output_section == p);
aee6f5b4
AO
825
826 /* There shouldn't be section relative relocations
827 against any other section. */
828 default:
829 return TRUE;
830 }
831}
832
062e2358 833/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
834 symbol for each output section, which come first. Next come symbols
835 which have been forced to local binding. Then all of the back-end
836 allocated local dynamic syms, followed by the rest of the global
837 symbols. */
30b30c21 838
554220db
AM
839static unsigned long
840_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
841 struct bfd_link_info *info,
842 unsigned long *section_sym_count)
30b30c21
RH
843{
844 unsigned long dynsymcount = 0;
845
0e1862bb
L
846 if (bfd_link_pic (info)
847 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 848 {
aee6f5b4 849 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
850 asection *p;
851 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 852 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
853 && (p->flags & SEC_ALLOC) != 0
854 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
855 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
856 else
857 elf_section_data (p)->dynindx = 0;
30b30c21 858 }
554220db 859 *section_sym_count = dynsymcount;
30b30c21 860
6fa3860b
PB
861 elf_link_hash_traverse (elf_hash_table (info),
862 elf_link_renumber_local_hash_table_dynsyms,
863 &dynsymcount);
864
30b30c21
RH
865 if (elf_hash_table (info)->dynlocal)
866 {
867 struct elf_link_local_dynamic_entry *p;
868 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
869 p->dynindx = ++dynsymcount;
870 }
871
872 elf_link_hash_traverse (elf_hash_table (info),
873 elf_link_renumber_hash_table_dynsyms,
874 &dynsymcount);
875
876 /* There is an unused NULL entry at the head of the table which
1a6e6083
L
877 we must account for in our count. We always create the dynsym
878 section, even if it is empty, with dynamic sections. */
879 if (elf_hash_table (info)->dynamic_sections_created)
30b30c21
RH
880 ++dynsymcount;
881
ccabcbe5
AM
882 elf_hash_table (info)->dynsymcount = dynsymcount;
883 return dynsymcount;
30b30c21 884}
252b5132 885
54ac0771
L
886/* Merge st_other field. */
887
888static void
889elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 890 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 891 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
892{
893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
894
895 /* If st_other has a processor-specific meaning, specific
cd3416da 896 code might be needed here. */
54ac0771
L
897 if (bed->elf_backend_merge_symbol_attribute)
898 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
899 dynamic);
900
cd3416da 901 if (!dynamic)
54ac0771 902 {
cd3416da
AM
903 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
904 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 905
cd3416da
AM
906 /* Keep the most constraining visibility. Leave the remainder
907 of the st_other field to elf_backend_merge_symbol_attribute. */
908 if (symvis - 1 < hvis - 1)
909 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 910 }
b8417128
AM
911 else if (definition
912 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
913 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 914 h->protected_def = 1;
54ac0771
L
915}
916
4f3fedcf
AM
917/* This function is called when we want to merge a new symbol with an
918 existing symbol. It handles the various cases which arise when we
919 find a definition in a dynamic object, or when there is already a
920 definition in a dynamic object. The new symbol is described by
921 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
922 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
923 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
924 of an old common symbol. We set OVERRIDE if the old symbol is
925 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
926 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
927 to change. By OK to change, we mean that we shouldn't warn if the
928 type or size does change. */
45d6a902 929
8a56bd02 930static bfd_boolean
268b6b39
AM
931_bfd_elf_merge_symbol (bfd *abfd,
932 struct bfd_link_info *info,
933 const char *name,
934 Elf_Internal_Sym *sym,
935 asection **psec,
936 bfd_vma *pvalue,
4f3fedcf
AM
937 struct elf_link_hash_entry **sym_hash,
938 bfd **poldbfd,
37a9e49a 939 bfd_boolean *pold_weak,
af44c138 940 unsigned int *pold_alignment,
268b6b39
AM
941 bfd_boolean *skip,
942 bfd_boolean *override,
943 bfd_boolean *type_change_ok,
6e33951e
L
944 bfd_boolean *size_change_ok,
945 bfd_boolean *matched)
252b5132 946{
7479dfd4 947 asection *sec, *oldsec;
45d6a902 948 struct elf_link_hash_entry *h;
90c984fc 949 struct elf_link_hash_entry *hi;
45d6a902
AM
950 struct elf_link_hash_entry *flip;
951 int bind;
952 bfd *oldbfd;
953 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 954 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 955 const struct elf_backend_data *bed;
6e33951e 956 char *new_version;
45d6a902
AM
957
958 *skip = FALSE;
959 *override = FALSE;
960
961 sec = *psec;
962 bind = ELF_ST_BIND (sym->st_info);
963
964 if (! bfd_is_und_section (sec))
965 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
966 else
967 h = ((struct elf_link_hash_entry *)
968 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
969 if (h == NULL)
970 return FALSE;
971 *sym_hash = h;
252b5132 972
88ba32a0
L
973 bed = get_elf_backend_data (abfd);
974
6e33951e 975 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 976 if (h->versioned != unversioned)
6e33951e 977 {
422f1182
L
978 /* Symbol version is unknown or versioned. */
979 new_version = strrchr (name, ELF_VER_CHR);
980 if (new_version)
981 {
982 if (h->versioned == unknown)
983 {
984 if (new_version > name && new_version[-1] != ELF_VER_CHR)
985 h->versioned = versioned_hidden;
986 else
987 h->versioned = versioned;
988 }
989 new_version += 1;
990 if (new_version[0] == '\0')
991 new_version = NULL;
992 }
993 else
994 h->versioned = unversioned;
6e33951e 995 }
422f1182
L
996 else
997 new_version = NULL;
6e33951e 998
90c984fc
L
999 /* For merging, we only care about real symbols. But we need to make
1000 sure that indirect symbol dynamic flags are updated. */
1001 hi = h;
45d6a902
AM
1002 while (h->root.type == bfd_link_hash_indirect
1003 || h->root.type == bfd_link_hash_warning)
1004 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1005
6e33951e
L
1006 if (!*matched)
1007 {
1008 if (hi == h || h->root.type == bfd_link_hash_new)
1009 *matched = TRUE;
1010 else
1011 {
ae7683d2 1012 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1013 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1014 true if the new symbol is only visible to the symbol with
6e33951e 1015 the same symbol version. */
422f1182
L
1016 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1017 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1018 if (!old_hidden && !new_hidden)
1019 /* The new symbol matches the existing symbol if both
1020 aren't hidden. */
1021 *matched = TRUE;
1022 else
1023 {
1024 /* OLD_VERSION is the symbol version of the existing
1025 symbol. */
422f1182
L
1026 char *old_version;
1027
1028 if (h->versioned >= versioned)
1029 old_version = strrchr (h->root.root.string,
1030 ELF_VER_CHR) + 1;
1031 else
1032 old_version = NULL;
6e33951e
L
1033
1034 /* The new symbol matches the existing symbol if they
1035 have the same symbol version. */
1036 *matched = (old_version == new_version
1037 || (old_version != NULL
1038 && new_version != NULL
1039 && strcmp (old_version, new_version) == 0));
1040 }
1041 }
1042 }
1043
934bce08
AM
1044 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1045 existing symbol. */
1046
1047 oldbfd = NULL;
1048 oldsec = NULL;
1049 switch (h->root.type)
1050 {
1051 default:
1052 break;
1053
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 oldbfd = h->root.u.undef.abfd;
1057 break;
1058
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1061 oldbfd = h->root.u.def.section->owner;
1062 oldsec = h->root.u.def.section;
1063 break;
1064
1065 case bfd_link_hash_common:
1066 oldbfd = h->root.u.c.p->section->owner;
1067 oldsec = h->root.u.c.p->section;
1068 if (pold_alignment)
1069 *pold_alignment = h->root.u.c.p->alignment_power;
1070 break;
1071 }
1072 if (poldbfd && *poldbfd == NULL)
1073 *poldbfd = oldbfd;
1074
1075 /* Differentiate strong and weak symbols. */
1076 newweak = bind == STB_WEAK;
1077 oldweak = (h->root.type == bfd_link_hash_defweak
1078 || h->root.type == bfd_link_hash_undefweak);
1079 if (pold_weak)
1080 *pold_weak = oldweak;
1081
1082 /* This code is for coping with dynamic objects, and is only useful
1083 if we are doing an ELF link. */
1084 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1085 return TRUE;
1086
40b36307 1087 /* We have to check it for every instance since the first few may be
ee659f1f 1088 references and not all compilers emit symbol type for undefined
40b36307
L
1089 symbols. */
1090 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1091
ee659f1f
AM
1092 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1093 respectively, is from a dynamic object. */
1094
1095 newdyn = (abfd->flags & DYNAMIC) != 0;
1096
1097 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1098 syms and defined syms in dynamic libraries respectively.
1099 ref_dynamic on the other hand can be set for a symbol defined in
1100 a dynamic library, and def_dynamic may not be set; When the
1101 definition in a dynamic lib is overridden by a definition in the
1102 executable use of the symbol in the dynamic lib becomes a
1103 reference to the executable symbol. */
1104 if (newdyn)
1105 {
1106 if (bfd_is_und_section (sec))
1107 {
1108 if (bind != STB_WEAK)
1109 {
1110 h->ref_dynamic_nonweak = 1;
1111 hi->ref_dynamic_nonweak = 1;
1112 }
1113 }
1114 else
1115 {
6e33951e
L
1116 /* Update the existing symbol only if they match. */
1117 if (*matched)
1118 h->dynamic_def = 1;
ee659f1f
AM
1119 hi->dynamic_def = 1;
1120 }
1121 }
1122
45d6a902
AM
1123 /* If we just created the symbol, mark it as being an ELF symbol.
1124 Other than that, there is nothing to do--there is no merge issue
1125 with a newly defined symbol--so we just return. */
1126
1127 if (h->root.type == bfd_link_hash_new)
252b5132 1128 {
f5385ebf 1129 h->non_elf = 0;
45d6a902
AM
1130 return TRUE;
1131 }
252b5132 1132
45d6a902
AM
1133 /* In cases involving weak versioned symbols, we may wind up trying
1134 to merge a symbol with itself. Catch that here, to avoid the
1135 confusion that results if we try to override a symbol with
1136 itself. The additional tests catch cases like
1137 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1138 dynamic object, which we do want to handle here. */
1139 if (abfd == oldbfd
895fa45f 1140 && (newweak || oldweak)
45d6a902 1141 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1142 || !h->def_regular))
45d6a902
AM
1143 return TRUE;
1144
707bba77 1145 olddyn = FALSE;
45d6a902
AM
1146 if (oldbfd != NULL)
1147 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1148 else if (oldsec != NULL)
45d6a902 1149 {
707bba77 1150 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1151 indices used by MIPS ELF. */
707bba77 1152 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1153 }
252b5132 1154
45d6a902
AM
1155 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1156 respectively, appear to be a definition rather than reference. */
1157
707bba77 1158 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1159
707bba77
AM
1160 olddef = (h->root.type != bfd_link_hash_undefined
1161 && h->root.type != bfd_link_hash_undefweak
1162 && h->root.type != bfd_link_hash_common);
45d6a902 1163
0a36a439
L
1164 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1165 respectively, appear to be a function. */
1166
1167 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1168 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1169
1170 oldfunc = (h->type != STT_NOTYPE
1171 && bed->is_function_type (h->type));
1172
580a2b6e
L
1173 /* When we try to create a default indirect symbol from the dynamic
1174 definition with the default version, we skip it if its type and
40101021 1175 the type of existing regular definition mismatch. */
580a2b6e 1176 if (pold_alignment == NULL
580a2b6e
L
1177 && newdyn
1178 && newdef
1179 && !olddyn
4584ec12
L
1180 && (((olddef || h->root.type == bfd_link_hash_common)
1181 && ELF_ST_TYPE (sym->st_info) != h->type
1182 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && h->type != STT_NOTYPE
1184 && !(newfunc && oldfunc))
1185 || (olddef
1186 && ((h->type == STT_GNU_IFUNC)
1187 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1188 {
1189 *skip = TRUE;
1190 return TRUE;
1191 }
1192
4c34aff8
AM
1193 /* Check TLS symbols. We don't check undefined symbols introduced
1194 by "ld -u" which have no type (and oldbfd NULL), and we don't
1195 check symbols from plugins because they also have no type. */
1196 if (oldbfd != NULL
1197 && (oldbfd->flags & BFD_PLUGIN) == 0
1198 && (abfd->flags & BFD_PLUGIN) == 0
1199 && ELF_ST_TYPE (sym->st_info) != h->type
1200 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1201 {
1202 bfd *ntbfd, *tbfd;
1203 bfd_boolean ntdef, tdef;
1204 asection *ntsec, *tsec;
1205
1206 if (h->type == STT_TLS)
1207 {
3b36f7e6 1208 ntbfd = abfd;
7479dfd4
L
1209 ntsec = sec;
1210 ntdef = newdef;
1211 tbfd = oldbfd;
1212 tsec = oldsec;
1213 tdef = olddef;
1214 }
1215 else
1216 {
1217 ntbfd = oldbfd;
1218 ntsec = oldsec;
1219 ntdef = olddef;
1220 tbfd = abfd;
1221 tsec = sec;
1222 tdef = newdef;
1223 }
1224
1225 if (tdef && ntdef)
1226 (*_bfd_error_handler)
191c0c42
AM
1227 (_("%s: TLS definition in %B section %A "
1228 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1229 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1230 else if (!tdef && !ntdef)
1231 (*_bfd_error_handler)
191c0c42
AM
1232 (_("%s: TLS reference in %B "
1233 "mismatches non-TLS reference in %B"),
7479dfd4
L
1234 tbfd, ntbfd, h->root.root.string);
1235 else if (tdef)
1236 (*_bfd_error_handler)
191c0c42
AM
1237 (_("%s: TLS definition in %B section %A "
1238 "mismatches non-TLS reference in %B"),
7479dfd4
L
1239 tbfd, tsec, ntbfd, h->root.root.string);
1240 else
1241 (*_bfd_error_handler)
191c0c42
AM
1242 (_("%s: TLS reference in %B "
1243 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1244 tbfd, ntbfd, ntsec, h->root.root.string);
1245
1246 bfd_set_error (bfd_error_bad_value);
1247 return FALSE;
1248 }
1249
45d6a902
AM
1250 /* If the old symbol has non-default visibility, we ignore the new
1251 definition from a dynamic object. */
1252 if (newdyn
9c7a29a3 1253 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1254 && !bfd_is_und_section (sec))
1255 {
1256 *skip = TRUE;
1257 /* Make sure this symbol is dynamic. */
f5385ebf 1258 h->ref_dynamic = 1;
90c984fc 1259 hi->ref_dynamic = 1;
45d6a902
AM
1260 /* A protected symbol has external availability. Make sure it is
1261 recorded as dynamic.
1262
1263 FIXME: Should we check type and size for protected symbol? */
1264 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1265 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1266 else
1267 return TRUE;
1268 }
1269 else if (!newdyn
9c7a29a3 1270 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1271 && h->def_dynamic)
45d6a902
AM
1272 {
1273 /* If the new symbol with non-default visibility comes from a
1274 relocatable file and the old definition comes from a dynamic
1275 object, we remove the old definition. */
6c9b78e6 1276 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1277 {
1278 /* Handle the case where the old dynamic definition is
1279 default versioned. We need to copy the symbol info from
1280 the symbol with default version to the normal one if it
1281 was referenced before. */
1282 if (h->ref_regular)
1283 {
6c9b78e6 1284 hi->root.type = h->root.type;
d2dee3b2 1285 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1286 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1287
6c9b78e6 1288 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1289 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1290 {
aed81c4e
MR
1291 /* If the new symbol is hidden or internal, completely undo
1292 any dynamic link state. */
1293 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1294 h->forced_local = 0;
1295 h->ref_dynamic = 0;
d2dee3b2
L
1296 }
1297 else
aed81c4e
MR
1298 h->ref_dynamic = 1;
1299
1300 h->def_dynamic = 0;
aed81c4e
MR
1301 /* FIXME: Should we check type and size for protected symbol? */
1302 h->size = 0;
1303 h->type = 0;
1304
6c9b78e6 1305 h = hi;
d2dee3b2
L
1306 }
1307 else
6c9b78e6 1308 h = hi;
d2dee3b2 1309 }
1de1a317 1310
f5eda473
AM
1311 /* If the old symbol was undefined before, then it will still be
1312 on the undefs list. If the new symbol is undefined or
1313 common, we can't make it bfd_link_hash_new here, because new
1314 undefined or common symbols will be added to the undefs list
1315 by _bfd_generic_link_add_one_symbol. Symbols may not be
1316 added twice to the undefs list. Also, if the new symbol is
1317 undefweak then we don't want to lose the strong undef. */
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1319 {
1de1a317 1320 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1321 h->root.u.undef.abfd = abfd;
1322 }
1323 else
1324 {
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1327 }
1328
f5eda473 1329 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1330 {
f5eda473
AM
1331 /* If the new symbol is hidden or internal, completely undo
1332 any dynamic link state. */
1333 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1334 h->forced_local = 0;
1335 h->ref_dynamic = 0;
45d6a902 1336 }
f5eda473
AM
1337 else
1338 h->ref_dynamic = 1;
1339 h->def_dynamic = 0;
45d6a902
AM
1340 /* FIXME: Should we check type and size for protected symbol? */
1341 h->size = 0;
1342 h->type = 0;
1343 return TRUE;
1344 }
14a793b2 1345
15b43f48
AM
1346 /* If a new weak symbol definition comes from a regular file and the
1347 old symbol comes from a dynamic library, we treat the new one as
1348 strong. Similarly, an old weak symbol definition from a regular
1349 file is treated as strong when the new symbol comes from a dynamic
1350 library. Further, an old weak symbol from a dynamic library is
1351 treated as strong if the new symbol is from a dynamic library.
1352 This reflects the way glibc's ld.so works.
1353
1354 Do this before setting *type_change_ok or *size_change_ok so that
1355 we warn properly when dynamic library symbols are overridden. */
1356
1357 if (newdef && !newdyn && olddyn)
0f8a2703 1358 newweak = FALSE;
15b43f48 1359 if (olddef && newdyn)
0f8a2703
AM
1360 oldweak = FALSE;
1361
d334575b 1362 /* Allow changes between different types of function symbol. */
0a36a439 1363 if (newfunc && oldfunc)
fcb93ecf
PB
1364 *type_change_ok = TRUE;
1365
79349b09
AM
1366 /* It's OK to change the type if either the existing symbol or the
1367 new symbol is weak. A type change is also OK if the old symbol
1368 is undefined and the new symbol is defined. */
252b5132 1369
79349b09
AM
1370 if (oldweak
1371 || newweak
1372 || (newdef
1373 && h->root.type == bfd_link_hash_undefined))
1374 *type_change_ok = TRUE;
1375
1376 /* It's OK to change the size if either the existing symbol or the
1377 new symbol is weak, or if the old symbol is undefined. */
1378
1379 if (*type_change_ok
1380 || h->root.type == bfd_link_hash_undefined)
1381 *size_change_ok = TRUE;
45d6a902 1382
45d6a902
AM
1383 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1384 symbol, respectively, appears to be a common symbol in a dynamic
1385 object. If a symbol appears in an uninitialized section, and is
1386 not weak, and is not a function, then it may be a common symbol
1387 which was resolved when the dynamic object was created. We want
1388 to treat such symbols specially, because they raise special
1389 considerations when setting the symbol size: if the symbol
1390 appears as a common symbol in a regular object, and the size in
1391 the regular object is larger, we must make sure that we use the
1392 larger size. This problematic case can always be avoided in C,
1393 but it must be handled correctly when using Fortran shared
1394 libraries.
1395
1396 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1397 likewise for OLDDYNCOMMON and OLDDEF.
1398
1399 Note that this test is just a heuristic, and that it is quite
1400 possible to have an uninitialized symbol in a shared object which
1401 is really a definition, rather than a common symbol. This could
1402 lead to some minor confusion when the symbol really is a common
1403 symbol in some regular object. However, I think it will be
1404 harmless. */
1405
1406 if (newdyn
1407 && newdef
79349b09 1408 && !newweak
45d6a902
AM
1409 && (sec->flags & SEC_ALLOC) != 0
1410 && (sec->flags & SEC_LOAD) == 0
1411 && sym->st_size > 0
0a36a439 1412 && !newfunc)
45d6a902
AM
1413 newdyncommon = TRUE;
1414 else
1415 newdyncommon = FALSE;
1416
1417 if (olddyn
1418 && olddef
1419 && h->root.type == bfd_link_hash_defined
f5385ebf 1420 && h->def_dynamic
45d6a902
AM
1421 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1422 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1423 && h->size > 0
0a36a439 1424 && !oldfunc)
45d6a902
AM
1425 olddyncommon = TRUE;
1426 else
1427 olddyncommon = FALSE;
1428
a4d8e49b
L
1429 /* We now know everything about the old and new symbols. We ask the
1430 backend to check if we can merge them. */
5d13b3b3
AM
1431 if (bed->merge_symbol != NULL)
1432 {
1433 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1434 return FALSE;
1435 sec = *psec;
1436 }
a4d8e49b 1437
45d6a902
AM
1438 /* If both the old and the new symbols look like common symbols in a
1439 dynamic object, set the size of the symbol to the larger of the
1440 two. */
1441
1442 if (olddyncommon
1443 && newdyncommon
1444 && sym->st_size != h->size)
1445 {
1446 /* Since we think we have two common symbols, issue a multiple
1447 common warning if desired. Note that we only warn if the
1448 size is different. If the size is the same, we simply let
1449 the old symbol override the new one as normally happens with
1450 symbols defined in dynamic objects. */
1451
1452 if (! ((*info->callbacks->multiple_common)
24f58f47 1453 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1454 return FALSE;
252b5132 1455
45d6a902
AM
1456 if (sym->st_size > h->size)
1457 h->size = sym->st_size;
252b5132 1458
45d6a902 1459 *size_change_ok = TRUE;
252b5132
RH
1460 }
1461
45d6a902
AM
1462 /* If we are looking at a dynamic object, and we have found a
1463 definition, we need to see if the symbol was already defined by
1464 some other object. If so, we want to use the existing
1465 definition, and we do not want to report a multiple symbol
1466 definition error; we do this by clobbering *PSEC to be
1467 bfd_und_section_ptr.
1468
1469 We treat a common symbol as a definition if the symbol in the
1470 shared library is a function, since common symbols always
1471 represent variables; this can cause confusion in principle, but
1472 any such confusion would seem to indicate an erroneous program or
1473 shared library. We also permit a common symbol in a regular
79349b09 1474 object to override a weak symbol in a shared object. */
45d6a902
AM
1475
1476 if (newdyn
1477 && newdef
77cfaee6 1478 && (olddef
45d6a902 1479 || (h->root.type == bfd_link_hash_common
0a36a439 1480 && (newweak || newfunc))))
45d6a902
AM
1481 {
1482 *override = TRUE;
1483 newdef = FALSE;
1484 newdyncommon = FALSE;
252b5132 1485
45d6a902
AM
1486 *psec = sec = bfd_und_section_ptr;
1487 *size_change_ok = TRUE;
252b5132 1488
45d6a902
AM
1489 /* If we get here when the old symbol is a common symbol, then
1490 we are explicitly letting it override a weak symbol or
1491 function in a dynamic object, and we don't want to warn about
1492 a type change. If the old symbol is a defined symbol, a type
1493 change warning may still be appropriate. */
252b5132 1494
45d6a902
AM
1495 if (h->root.type == bfd_link_hash_common)
1496 *type_change_ok = TRUE;
1497 }
1498
1499 /* Handle the special case of an old common symbol merging with a
1500 new symbol which looks like a common symbol in a shared object.
1501 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1502 common symbol, and let _bfd_generic_link_add_one_symbol do the
1503 right thing. */
45d6a902
AM
1504
1505 if (newdyncommon
1506 && h->root.type == bfd_link_hash_common)
1507 {
1508 *override = TRUE;
1509 newdef = FALSE;
1510 newdyncommon = FALSE;
1511 *pvalue = sym->st_size;
a4d8e49b 1512 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1513 *size_change_ok = TRUE;
1514 }
1515
c5e2cead 1516 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1517 if (newdef && olddef && newweak)
54ac0771 1518 {
35ed3f94 1519 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1520 if (!(oldbfd != NULL
1521 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1522 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1523 {
1524 newdef = FALSE;
1525 *skip = TRUE;
1526 }
54ac0771
L
1527
1528 /* Merge st_other. If the symbol already has a dynamic index,
1529 but visibility says it should not be visible, turn it into a
1530 local symbol. */
b8417128 1531 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1532 if (h->dynindx != -1)
1533 switch (ELF_ST_VISIBILITY (h->other))
1534 {
1535 case STV_INTERNAL:
1536 case STV_HIDDEN:
1537 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1538 break;
1539 }
1540 }
c5e2cead 1541
45d6a902
AM
1542 /* If the old symbol is from a dynamic object, and the new symbol is
1543 a definition which is not from a dynamic object, then the new
1544 symbol overrides the old symbol. Symbols from regular files
1545 always take precedence over symbols from dynamic objects, even if
1546 they are defined after the dynamic object in the link.
1547
1548 As above, we again permit a common symbol in a regular object to
1549 override a definition in a shared object if the shared object
0f8a2703 1550 symbol is a function or is weak. */
45d6a902
AM
1551
1552 flip = NULL;
77cfaee6 1553 if (!newdyn
45d6a902
AM
1554 && (newdef
1555 || (bfd_is_com_section (sec)
0a36a439 1556 && (oldweak || oldfunc)))
45d6a902
AM
1557 && olddyn
1558 && olddef
f5385ebf 1559 && h->def_dynamic)
45d6a902
AM
1560 {
1561 /* Change the hash table entry to undefined, and let
1562 _bfd_generic_link_add_one_symbol do the right thing with the
1563 new definition. */
1564
1565 h->root.type = bfd_link_hash_undefined;
1566 h->root.u.undef.abfd = h->root.u.def.section->owner;
1567 *size_change_ok = TRUE;
1568
1569 olddef = FALSE;
1570 olddyncommon = FALSE;
1571
1572 /* We again permit a type change when a common symbol may be
1573 overriding a function. */
1574
1575 if (bfd_is_com_section (sec))
0a36a439
L
1576 {
1577 if (oldfunc)
1578 {
1579 /* If a common symbol overrides a function, make sure
1580 that it isn't defined dynamically nor has type
1581 function. */
1582 h->def_dynamic = 0;
1583 h->type = STT_NOTYPE;
1584 }
1585 *type_change_ok = TRUE;
1586 }
45d6a902 1587
6c9b78e6
AM
1588 if (hi->root.type == bfd_link_hash_indirect)
1589 flip = hi;
45d6a902
AM
1590 else
1591 /* This union may have been set to be non-NULL when this symbol
1592 was seen in a dynamic object. We must force the union to be
1593 NULL, so that it is correct for a regular symbol. */
1594 h->verinfo.vertree = NULL;
1595 }
1596
1597 /* Handle the special case of a new common symbol merging with an
1598 old symbol that looks like it might be a common symbol defined in
1599 a shared object. Note that we have already handled the case in
1600 which a new common symbol should simply override the definition
1601 in the shared library. */
1602
1603 if (! newdyn
1604 && bfd_is_com_section (sec)
1605 && olddyncommon)
1606 {
1607 /* It would be best if we could set the hash table entry to a
1608 common symbol, but we don't know what to use for the section
1609 or the alignment. */
1610 if (! ((*info->callbacks->multiple_common)
24f58f47 1611 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1612 return FALSE;
1613
4cc11e76 1614 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1615 larger, pretend that the new symbol has its size. */
1616
1617 if (h->size > *pvalue)
1618 *pvalue = h->size;
1619
af44c138
L
1620 /* We need to remember the alignment required by the symbol
1621 in the dynamic object. */
1622 BFD_ASSERT (pold_alignment);
1623 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1624
1625 olddef = FALSE;
1626 olddyncommon = FALSE;
1627
1628 h->root.type = bfd_link_hash_undefined;
1629 h->root.u.undef.abfd = h->root.u.def.section->owner;
1630
1631 *size_change_ok = TRUE;
1632 *type_change_ok = TRUE;
1633
6c9b78e6
AM
1634 if (hi->root.type == bfd_link_hash_indirect)
1635 flip = hi;
45d6a902
AM
1636 else
1637 h->verinfo.vertree = NULL;
1638 }
1639
1640 if (flip != NULL)
1641 {
1642 /* Handle the case where we had a versioned symbol in a dynamic
1643 library and now find a definition in a normal object. In this
1644 case, we make the versioned symbol point to the normal one. */
45d6a902 1645 flip->root.type = h->root.type;
00cbee0a 1646 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1647 h->root.type = bfd_link_hash_indirect;
1648 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1649 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1650 if (h->def_dynamic)
45d6a902 1651 {
f5385ebf
AM
1652 h->def_dynamic = 0;
1653 flip->ref_dynamic = 1;
45d6a902
AM
1654 }
1655 }
1656
45d6a902
AM
1657 return TRUE;
1658}
1659
1660/* This function is called to create an indirect symbol from the
1661 default for the symbol with the default version if needed. The
4f3fedcf 1662 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1663 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1664
28caa186 1665static bfd_boolean
268b6b39
AM
1666_bfd_elf_add_default_symbol (bfd *abfd,
1667 struct bfd_link_info *info,
1668 struct elf_link_hash_entry *h,
1669 const char *name,
1670 Elf_Internal_Sym *sym,
4f3fedcf
AM
1671 asection *sec,
1672 bfd_vma value,
1673 bfd **poldbfd,
e3c9d234 1674 bfd_boolean *dynsym)
45d6a902
AM
1675{
1676 bfd_boolean type_change_ok;
1677 bfd_boolean size_change_ok;
1678 bfd_boolean skip;
1679 char *shortname;
1680 struct elf_link_hash_entry *hi;
1681 struct bfd_link_hash_entry *bh;
9c5bfbb7 1682 const struct elf_backend_data *bed;
45d6a902
AM
1683 bfd_boolean collect;
1684 bfd_boolean dynamic;
e3c9d234 1685 bfd_boolean override;
45d6a902
AM
1686 char *p;
1687 size_t len, shortlen;
ffd65175 1688 asection *tmp_sec;
6e33951e 1689 bfd_boolean matched;
45d6a902 1690
422f1182
L
1691 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1692 return TRUE;
1693
45d6a902
AM
1694 /* If this symbol has a version, and it is the default version, we
1695 create an indirect symbol from the default name to the fully
1696 decorated name. This will cause external references which do not
1697 specify a version to be bound to this version of the symbol. */
1698 p = strchr (name, ELF_VER_CHR);
422f1182
L
1699 if (h->versioned == unknown)
1700 {
1701 if (p == NULL)
1702 {
1703 h->versioned = unversioned;
1704 return TRUE;
1705 }
1706 else
1707 {
1708 if (p[1] != ELF_VER_CHR)
1709 {
1710 h->versioned = versioned_hidden;
1711 return TRUE;
1712 }
1713 else
1714 h->versioned = versioned;
1715 }
1716 }
4373f8af
L
1717 else
1718 {
1719 /* PR ld/19073: We may see an unversioned definition after the
1720 default version. */
1721 if (p == NULL)
1722 return TRUE;
1723 }
45d6a902 1724
45d6a902
AM
1725 bed = get_elf_backend_data (abfd);
1726 collect = bed->collect;
1727 dynamic = (abfd->flags & DYNAMIC) != 0;
1728
1729 shortlen = p - name;
a50b1753 1730 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1731 if (shortname == NULL)
1732 return FALSE;
1733 memcpy (shortname, name, shortlen);
1734 shortname[shortlen] = '\0';
1735
1736 /* We are going to create a new symbol. Merge it with any existing
1737 symbol with this name. For the purposes of the merge, act as
1738 though we were defining the symbol we just defined, although we
1739 actually going to define an indirect symbol. */
1740 type_change_ok = FALSE;
1741 size_change_ok = FALSE;
6e33951e 1742 matched = TRUE;
ffd65175
AM
1743 tmp_sec = sec;
1744 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1745 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1746 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1747 return FALSE;
1748
1749 if (skip)
1750 goto nondefault;
1751
1752 if (! override)
1753 {
c6e8a9a8 1754 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1755 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1756 {
1757 bh = &hi->root;
1758 if (! (_bfd_generic_link_add_one_symbol
1759 (info, abfd, shortname, BSF_INDIRECT,
1760 bfd_ind_section_ptr,
1761 0, name, FALSE, collect, &bh)))
1762 return FALSE;
1763 hi = (struct elf_link_hash_entry *) bh;
1764 }
45d6a902
AM
1765 }
1766 else
1767 {
1768 /* In this case the symbol named SHORTNAME is overriding the
1769 indirect symbol we want to add. We were planning on making
1770 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1771 is the name without a version. NAME is the fully versioned
1772 name, and it is the default version.
1773
1774 Overriding means that we already saw a definition for the
1775 symbol SHORTNAME in a regular object, and it is overriding
1776 the symbol defined in the dynamic object.
1777
1778 When this happens, we actually want to change NAME, the
1779 symbol we just added, to refer to SHORTNAME. This will cause
1780 references to NAME in the shared object to become references
1781 to SHORTNAME in the regular object. This is what we expect
1782 when we override a function in a shared object: that the
1783 references in the shared object will be mapped to the
1784 definition in the regular object. */
1785
1786 while (hi->root.type == bfd_link_hash_indirect
1787 || hi->root.type == bfd_link_hash_warning)
1788 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1789
1790 h->root.type = bfd_link_hash_indirect;
1791 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1792 if (h->def_dynamic)
45d6a902 1793 {
f5385ebf
AM
1794 h->def_dynamic = 0;
1795 hi->ref_dynamic = 1;
1796 if (hi->ref_regular
1797 || hi->def_regular)
45d6a902 1798 {
c152c796 1799 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1800 return FALSE;
1801 }
1802 }
1803
1804 /* Now set HI to H, so that the following code will set the
1805 other fields correctly. */
1806 hi = h;
1807 }
1808
fab4a87f
L
1809 /* Check if HI is a warning symbol. */
1810 if (hi->root.type == bfd_link_hash_warning)
1811 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1812
45d6a902
AM
1813 /* If there is a duplicate definition somewhere, then HI may not
1814 point to an indirect symbol. We will have reported an error to
1815 the user in that case. */
1816
1817 if (hi->root.type == bfd_link_hash_indirect)
1818 {
1819 struct elf_link_hash_entry *ht;
1820
45d6a902 1821 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1822 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1823
68c88cd4
AM
1824 /* A reference to the SHORTNAME symbol from a dynamic library
1825 will be satisfied by the versioned symbol at runtime. In
1826 effect, we have a reference to the versioned symbol. */
1827 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1828 hi->dynamic_def |= ht->dynamic_def;
1829
45d6a902
AM
1830 /* See if the new flags lead us to realize that the symbol must
1831 be dynamic. */
1832 if (! *dynsym)
1833 {
1834 if (! dynamic)
1835 {
0e1862bb 1836 if (! bfd_link_executable (info)
90c984fc 1837 || hi->def_dynamic
f5385ebf 1838 || hi->ref_dynamic)
45d6a902
AM
1839 *dynsym = TRUE;
1840 }
1841 else
1842 {
f5385ebf 1843 if (hi->ref_regular)
45d6a902
AM
1844 *dynsym = TRUE;
1845 }
1846 }
1847 }
1848
1849 /* We also need to define an indirection from the nondefault version
1850 of the symbol. */
1851
1852nondefault:
1853 len = strlen (name);
a50b1753 1854 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1855 if (shortname == NULL)
1856 return FALSE;
1857 memcpy (shortname, name, shortlen);
1858 memcpy (shortname + shortlen, p + 1, len - shortlen);
1859
1860 /* Once again, merge with any existing symbol. */
1861 type_change_ok = FALSE;
1862 size_change_ok = FALSE;
ffd65175
AM
1863 tmp_sec = sec;
1864 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1865 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1866 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1867 return FALSE;
1868
1869 if (skip)
1870 return TRUE;
1871
1872 if (override)
1873 {
1874 /* Here SHORTNAME is a versioned name, so we don't expect to see
1875 the type of override we do in the case above unless it is
4cc11e76 1876 overridden by a versioned definition. */
45d6a902
AM
1877 if (hi->root.type != bfd_link_hash_defined
1878 && hi->root.type != bfd_link_hash_defweak)
1879 (*_bfd_error_handler)
d003868e
AM
1880 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1881 abfd, shortname);
45d6a902
AM
1882 }
1883 else
1884 {
1885 bh = &hi->root;
1886 if (! (_bfd_generic_link_add_one_symbol
1887 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1888 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1889 return FALSE;
1890 hi = (struct elf_link_hash_entry *) bh;
1891
1892 /* If there is a duplicate definition somewhere, then HI may not
1893 point to an indirect symbol. We will have reported an error
1894 to the user in that case. */
1895
1896 if (hi->root.type == bfd_link_hash_indirect)
1897 {
fcfa13d2 1898 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1899 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1900 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1901
1902 /* See if the new flags lead us to realize that the symbol
1903 must be dynamic. */
1904 if (! *dynsym)
1905 {
1906 if (! dynamic)
1907 {
0e1862bb 1908 if (! bfd_link_executable (info)
f5385ebf 1909 || hi->ref_dynamic)
45d6a902
AM
1910 *dynsym = TRUE;
1911 }
1912 else
1913 {
f5385ebf 1914 if (hi->ref_regular)
45d6a902
AM
1915 *dynsym = TRUE;
1916 }
1917 }
1918 }
1919 }
1920
1921 return TRUE;
1922}
1923\f
1924/* This routine is used to export all defined symbols into the dynamic
1925 symbol table. It is called via elf_link_hash_traverse. */
1926
28caa186 1927static bfd_boolean
268b6b39 1928_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1929{
a50b1753 1930 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1931
1932 /* Ignore indirect symbols. These are added by the versioning code. */
1933 if (h->root.type == bfd_link_hash_indirect)
1934 return TRUE;
1935
7686d77d
AM
1936 /* Ignore this if we won't export it. */
1937 if (!eif->info->export_dynamic && !h->dynamic)
1938 return TRUE;
45d6a902
AM
1939
1940 if (h->dynindx == -1
fd91d419
L
1941 && (h->def_regular || h->ref_regular)
1942 && ! bfd_hide_sym_by_version (eif->info->version_info,
1943 h->root.root.string))
45d6a902 1944 {
fd91d419 1945 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1946 {
fd91d419
L
1947 eif->failed = TRUE;
1948 return FALSE;
45d6a902
AM
1949 }
1950 }
1951
1952 return TRUE;
1953}
1954\f
1955/* Look through the symbols which are defined in other shared
1956 libraries and referenced here. Update the list of version
1957 dependencies. This will be put into the .gnu.version_r section.
1958 This function is called via elf_link_hash_traverse. */
1959
28caa186 1960static bfd_boolean
268b6b39
AM
1961_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1962 void *data)
45d6a902 1963{
a50b1753 1964 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1965 Elf_Internal_Verneed *t;
1966 Elf_Internal_Vernaux *a;
1967 bfd_size_type amt;
1968
45d6a902
AM
1969 /* We only care about symbols defined in shared objects with version
1970 information. */
f5385ebf
AM
1971 if (!h->def_dynamic
1972 || h->def_regular
45d6a902 1973 || h->dynindx == -1
7b20f099
AM
1974 || h->verinfo.verdef == NULL
1975 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1976 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1977 return TRUE;
1978
1979 /* See if we already know about this version. */
28caa186
AM
1980 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1981 t != NULL;
1982 t = t->vn_nextref)
45d6a902
AM
1983 {
1984 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1985 continue;
1986
1987 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1988 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1989 return TRUE;
1990
1991 break;
1992 }
1993
1994 /* This is a new version. Add it to tree we are building. */
1995
1996 if (t == NULL)
1997 {
1998 amt = sizeof *t;
a50b1753 1999 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2000 if (t == NULL)
2001 {
2002 rinfo->failed = TRUE;
2003 return FALSE;
2004 }
2005
2006 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2007 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2008 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2009 }
2010
2011 amt = sizeof *a;
a50b1753 2012 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2013 if (a == NULL)
2014 {
2015 rinfo->failed = TRUE;
2016 return FALSE;
2017 }
45d6a902
AM
2018
2019 /* Note that we are copying a string pointer here, and testing it
2020 above. If bfd_elf_string_from_elf_section is ever changed to
2021 discard the string data when low in memory, this will have to be
2022 fixed. */
2023 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2024
2025 a->vna_flags = h->verinfo.verdef->vd_flags;
2026 a->vna_nextptr = t->vn_auxptr;
2027
2028 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2029 ++rinfo->vers;
2030
2031 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2032
2033 t->vn_auxptr = a;
2034
2035 return TRUE;
2036}
2037
2038/* Figure out appropriate versions for all the symbols. We may not
2039 have the version number script until we have read all of the input
2040 files, so until that point we don't know which symbols should be
2041 local. This function is called via elf_link_hash_traverse. */
2042
28caa186 2043static bfd_boolean
268b6b39 2044_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2045{
28caa186 2046 struct elf_info_failed *sinfo;
45d6a902 2047 struct bfd_link_info *info;
9c5bfbb7 2048 const struct elf_backend_data *bed;
45d6a902
AM
2049 struct elf_info_failed eif;
2050 char *p;
2051 bfd_size_type amt;
2052
a50b1753 2053 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2054 info = sinfo->info;
2055
45d6a902
AM
2056 /* Fix the symbol flags. */
2057 eif.failed = FALSE;
2058 eif.info = info;
2059 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2060 {
2061 if (eif.failed)
2062 sinfo->failed = TRUE;
2063 return FALSE;
2064 }
2065
2066 /* We only need version numbers for symbols defined in regular
2067 objects. */
f5385ebf 2068 if (!h->def_regular)
45d6a902
AM
2069 return TRUE;
2070
28caa186 2071 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2072 p = strchr (h->root.root.string, ELF_VER_CHR);
2073 if (p != NULL && h->verinfo.vertree == NULL)
2074 {
2075 struct bfd_elf_version_tree *t;
45d6a902 2076
45d6a902
AM
2077 ++p;
2078 if (*p == ELF_VER_CHR)
6e33951e 2079 ++p;
45d6a902
AM
2080
2081 /* If there is no version string, we can just return out. */
2082 if (*p == '\0')
6e33951e 2083 return TRUE;
45d6a902
AM
2084
2085 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2086 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2087 {
2088 if (strcmp (t->name, p) == 0)
2089 {
2090 size_t len;
2091 char *alc;
2092 struct bfd_elf_version_expr *d;
2093
2094 len = p - h->root.root.string;
a50b1753 2095 alc = (char *) bfd_malloc (len);
45d6a902 2096 if (alc == NULL)
14b1c01e
AM
2097 {
2098 sinfo->failed = TRUE;
2099 return FALSE;
2100 }
45d6a902
AM
2101 memcpy (alc, h->root.root.string, len - 1);
2102 alc[len - 1] = '\0';
2103 if (alc[len - 2] == ELF_VER_CHR)
2104 alc[len - 2] = '\0';
2105
2106 h->verinfo.vertree = t;
2107 t->used = TRUE;
2108 d = NULL;
2109
108ba305
JJ
2110 if (t->globals.list != NULL)
2111 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2112
2113 /* See if there is anything to force this symbol to
2114 local scope. */
108ba305 2115 if (d == NULL && t->locals.list != NULL)
45d6a902 2116 {
108ba305
JJ
2117 d = (*t->match) (&t->locals, NULL, alc);
2118 if (d != NULL
2119 && h->dynindx != -1
108ba305
JJ
2120 && ! info->export_dynamic)
2121 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2122 }
2123
2124 free (alc);
2125 break;
2126 }
2127 }
2128
2129 /* If we are building an application, we need to create a
2130 version node for this version. */
0e1862bb 2131 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2132 {
2133 struct bfd_elf_version_tree **pp;
2134 int version_index;
2135
2136 /* If we aren't going to export this symbol, we don't need
2137 to worry about it. */
2138 if (h->dynindx == -1)
2139 return TRUE;
2140
2141 amt = sizeof *t;
a50b1753 2142 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2143 if (t == NULL)
2144 {
2145 sinfo->failed = TRUE;
2146 return FALSE;
2147 }
2148
45d6a902 2149 t->name = p;
45d6a902
AM
2150 t->name_indx = (unsigned int) -1;
2151 t->used = TRUE;
2152
2153 version_index = 1;
2154 /* Don't count anonymous version tag. */
fd91d419
L
2155 if (sinfo->info->version_info != NULL
2156 && sinfo->info->version_info->vernum == 0)
45d6a902 2157 version_index = 0;
fd91d419
L
2158 for (pp = &sinfo->info->version_info;
2159 *pp != NULL;
2160 pp = &(*pp)->next)
45d6a902
AM
2161 ++version_index;
2162 t->vernum = version_index;
2163
2164 *pp = t;
2165
2166 h->verinfo.vertree = t;
2167 }
2168 else if (t == NULL)
2169 {
2170 /* We could not find the version for a symbol when
2171 generating a shared archive. Return an error. */
2172 (*_bfd_error_handler)
c55fe096 2173 (_("%B: version node not found for symbol %s"),
28caa186 2174 info->output_bfd, h->root.root.string);
45d6a902
AM
2175 bfd_set_error (bfd_error_bad_value);
2176 sinfo->failed = TRUE;
2177 return FALSE;
2178 }
45d6a902
AM
2179 }
2180
2181 /* If we don't have a version for this symbol, see if we can find
2182 something. */
fd91d419 2183 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2184 {
1e8fa21e 2185 bfd_boolean hide;
ae5a3597 2186
fd91d419
L
2187 h->verinfo.vertree
2188 = bfd_find_version_for_sym (sinfo->info->version_info,
2189 h->root.root.string, &hide);
1e8fa21e
AM
2190 if (h->verinfo.vertree != NULL && hide)
2191 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2192 }
2193
2194 return TRUE;
2195}
2196\f
45d6a902
AM
2197/* Read and swap the relocs from the section indicated by SHDR. This
2198 may be either a REL or a RELA section. The relocations are
2199 translated into RELA relocations and stored in INTERNAL_RELOCS,
2200 which should have already been allocated to contain enough space.
2201 The EXTERNAL_RELOCS are a buffer where the external form of the
2202 relocations should be stored.
2203
2204 Returns FALSE if something goes wrong. */
2205
2206static bfd_boolean
268b6b39 2207elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2208 asection *sec,
268b6b39
AM
2209 Elf_Internal_Shdr *shdr,
2210 void *external_relocs,
2211 Elf_Internal_Rela *internal_relocs)
45d6a902 2212{
9c5bfbb7 2213 const struct elf_backend_data *bed;
268b6b39 2214 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2215 const bfd_byte *erela;
2216 const bfd_byte *erelaend;
2217 Elf_Internal_Rela *irela;
243ef1e0
L
2218 Elf_Internal_Shdr *symtab_hdr;
2219 size_t nsyms;
45d6a902 2220
45d6a902
AM
2221 /* Position ourselves at the start of the section. */
2222 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2223 return FALSE;
2224
2225 /* Read the relocations. */
2226 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2227 return FALSE;
2228
243ef1e0 2229 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2230 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2231
45d6a902
AM
2232 bed = get_elf_backend_data (abfd);
2233
2234 /* Convert the external relocations to the internal format. */
2235 if (shdr->sh_entsize == bed->s->sizeof_rel)
2236 swap_in = bed->s->swap_reloc_in;
2237 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2238 swap_in = bed->s->swap_reloca_in;
2239 else
2240 {
2241 bfd_set_error (bfd_error_wrong_format);
2242 return FALSE;
2243 }
2244
a50b1753 2245 erela = (const bfd_byte *) external_relocs;
51992aec 2246 erelaend = erela + shdr->sh_size;
45d6a902
AM
2247 irela = internal_relocs;
2248 while (erela < erelaend)
2249 {
243ef1e0
L
2250 bfd_vma r_symndx;
2251
45d6a902 2252 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2253 r_symndx = ELF32_R_SYM (irela->r_info);
2254 if (bed->s->arch_size == 64)
2255 r_symndx >>= 24;
ce98a316
NC
2256 if (nsyms > 0)
2257 {
2258 if ((size_t) r_symndx >= nsyms)
2259 {
2260 (*_bfd_error_handler)
2261 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2262 " for offset 0x%lx in section `%A'"),
2263 abfd, sec,
2264 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2265 bfd_set_error (bfd_error_bad_value);
2266 return FALSE;
2267 }
2268 }
cf35638d 2269 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2270 {
2271 (*_bfd_error_handler)
ce98a316
NC
2272 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2273 " when the object file has no symbol table"),
d003868e
AM
2274 abfd, sec,
2275 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2276 bfd_set_error (bfd_error_bad_value);
2277 return FALSE;
2278 }
45d6a902
AM
2279 irela += bed->s->int_rels_per_ext_rel;
2280 erela += shdr->sh_entsize;
2281 }
2282
2283 return TRUE;
2284}
2285
2286/* Read and swap the relocs for a section O. They may have been
2287 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2288 not NULL, they are used as buffers to read into. They are known to
2289 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2290 the return value is allocated using either malloc or bfd_alloc,
2291 according to the KEEP_MEMORY argument. If O has two relocation
2292 sections (both REL and RELA relocations), then the REL_HDR
2293 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2294 RELA_HDR relocations. */
45d6a902
AM
2295
2296Elf_Internal_Rela *
268b6b39
AM
2297_bfd_elf_link_read_relocs (bfd *abfd,
2298 asection *o,
2299 void *external_relocs,
2300 Elf_Internal_Rela *internal_relocs,
2301 bfd_boolean keep_memory)
45d6a902 2302{
268b6b39 2303 void *alloc1 = NULL;
45d6a902 2304 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2306 struct bfd_elf_section_data *esdo = elf_section_data (o);
2307 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2308
d4730f92
BS
2309 if (esdo->relocs != NULL)
2310 return esdo->relocs;
45d6a902
AM
2311
2312 if (o->reloc_count == 0)
2313 return NULL;
2314
45d6a902
AM
2315 if (internal_relocs == NULL)
2316 {
2317 bfd_size_type size;
2318
2319 size = o->reloc_count;
2320 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2321 if (keep_memory)
a50b1753 2322 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2323 else
a50b1753 2324 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2325 if (internal_relocs == NULL)
2326 goto error_return;
2327 }
2328
2329 if (external_relocs == NULL)
2330 {
d4730f92
BS
2331 bfd_size_type size = 0;
2332
2333 if (esdo->rel.hdr)
2334 size += esdo->rel.hdr->sh_size;
2335 if (esdo->rela.hdr)
2336 size += esdo->rela.hdr->sh_size;
45d6a902 2337
268b6b39 2338 alloc1 = bfd_malloc (size);
45d6a902
AM
2339 if (alloc1 == NULL)
2340 goto error_return;
2341 external_relocs = alloc1;
2342 }
2343
d4730f92
BS
2344 internal_rela_relocs = internal_relocs;
2345 if (esdo->rel.hdr)
2346 {
2347 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2348 external_relocs,
2349 internal_relocs))
2350 goto error_return;
2351 external_relocs = (((bfd_byte *) external_relocs)
2352 + esdo->rel.hdr->sh_size);
2353 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2354 * bed->s->int_rels_per_ext_rel);
2355 }
2356
2357 if (esdo->rela.hdr
2358 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2359 external_relocs,
2360 internal_rela_relocs)))
45d6a902
AM
2361 goto error_return;
2362
2363 /* Cache the results for next time, if we can. */
2364 if (keep_memory)
d4730f92 2365 esdo->relocs = internal_relocs;
45d6a902
AM
2366
2367 if (alloc1 != NULL)
2368 free (alloc1);
2369
2370 /* Don't free alloc2, since if it was allocated we are passing it
2371 back (under the name of internal_relocs). */
2372
2373 return internal_relocs;
2374
2375 error_return:
2376 if (alloc1 != NULL)
2377 free (alloc1);
2378 if (alloc2 != NULL)
4dd07732
AM
2379 {
2380 if (keep_memory)
2381 bfd_release (abfd, alloc2);
2382 else
2383 free (alloc2);
2384 }
45d6a902
AM
2385 return NULL;
2386}
2387
2388/* Compute the size of, and allocate space for, REL_HDR which is the
2389 section header for a section containing relocations for O. */
2390
28caa186 2391static bfd_boolean
268b6b39 2392_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2393 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2394{
d4730f92 2395 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2396
2397 /* That allows us to calculate the size of the section. */
d4730f92 2398 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2399
2400 /* The contents field must last into write_object_contents, so we
2401 allocate it with bfd_alloc rather than malloc. Also since we
2402 cannot be sure that the contents will actually be filled in,
2403 we zero the allocated space. */
a50b1753 2404 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2405 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2406 return FALSE;
2407
d4730f92 2408 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2409 {
2410 struct elf_link_hash_entry **p;
2411
ca4be51c
AM
2412 p = ((struct elf_link_hash_entry **)
2413 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2414 if (p == NULL)
2415 return FALSE;
2416
d4730f92 2417 reldata->hashes = p;
45d6a902
AM
2418 }
2419
2420 return TRUE;
2421}
2422
2423/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2424 originated from the section given by INPUT_REL_HDR) to the
2425 OUTPUT_BFD. */
2426
2427bfd_boolean
268b6b39
AM
2428_bfd_elf_link_output_relocs (bfd *output_bfd,
2429 asection *input_section,
2430 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2431 Elf_Internal_Rela *internal_relocs,
2432 struct elf_link_hash_entry **rel_hash
2433 ATTRIBUTE_UNUSED)
45d6a902
AM
2434{
2435 Elf_Internal_Rela *irela;
2436 Elf_Internal_Rela *irelaend;
2437 bfd_byte *erel;
d4730f92 2438 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2439 asection *output_section;
9c5bfbb7 2440 const struct elf_backend_data *bed;
268b6b39 2441 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2442 struct bfd_elf_section_data *esdo;
45d6a902
AM
2443
2444 output_section = input_section->output_section;
45d6a902 2445
d4730f92
BS
2446 bed = get_elf_backend_data (output_bfd);
2447 esdo = elf_section_data (output_section);
2448 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2449 {
d4730f92
BS
2450 output_reldata = &esdo->rel;
2451 swap_out = bed->s->swap_reloc_out;
45d6a902 2452 }
d4730f92
BS
2453 else if (esdo->rela.hdr
2454 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2455 {
d4730f92
BS
2456 output_reldata = &esdo->rela;
2457 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2458 }
2459 else
2460 {
2461 (*_bfd_error_handler)
d003868e
AM
2462 (_("%B: relocation size mismatch in %B section %A"),
2463 output_bfd, input_section->owner, input_section);
297d8443 2464 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2465 return FALSE;
2466 }
2467
d4730f92
BS
2468 erel = output_reldata->hdr->contents;
2469 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2470 irela = internal_relocs;
2471 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2472 * bed->s->int_rels_per_ext_rel);
2473 while (irela < irelaend)
2474 {
2475 (*swap_out) (output_bfd, irela, erel);
2476 irela += bed->s->int_rels_per_ext_rel;
2477 erel += input_rel_hdr->sh_entsize;
2478 }
2479
2480 /* Bump the counter, so that we know where to add the next set of
2481 relocations. */
d4730f92 2482 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2483
2484 return TRUE;
2485}
2486\f
508c3946
L
2487/* Make weak undefined symbols in PIE dynamic. */
2488
2489bfd_boolean
2490_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2491 struct elf_link_hash_entry *h)
2492{
0e1862bb 2493 if (bfd_link_pie (info)
508c3946
L
2494 && h->dynindx == -1
2495 && h->root.type == bfd_link_hash_undefweak)
2496 return bfd_elf_link_record_dynamic_symbol (info, h);
2497
2498 return TRUE;
2499}
2500
45d6a902
AM
2501/* Fix up the flags for a symbol. This handles various cases which
2502 can only be fixed after all the input files are seen. This is
2503 currently called by both adjust_dynamic_symbol and
2504 assign_sym_version, which is unnecessary but perhaps more robust in
2505 the face of future changes. */
2506
28caa186 2507static bfd_boolean
268b6b39
AM
2508_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2509 struct elf_info_failed *eif)
45d6a902 2510{
33774f08 2511 const struct elf_backend_data *bed;
508c3946 2512
45d6a902
AM
2513 /* If this symbol was mentioned in a non-ELF file, try to set
2514 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2515 permit a non-ELF file to correctly refer to a symbol defined in
2516 an ELF dynamic object. */
f5385ebf 2517 if (h->non_elf)
45d6a902
AM
2518 {
2519 while (h->root.type == bfd_link_hash_indirect)
2520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2521
2522 if (h->root.type != bfd_link_hash_defined
2523 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2524 {
2525 h->ref_regular = 1;
2526 h->ref_regular_nonweak = 1;
2527 }
45d6a902
AM
2528 else
2529 {
2530 if (h->root.u.def.section->owner != NULL
2531 && (bfd_get_flavour (h->root.u.def.section->owner)
2532 == bfd_target_elf_flavour))
f5385ebf
AM
2533 {
2534 h->ref_regular = 1;
2535 h->ref_regular_nonweak = 1;
2536 }
45d6a902 2537 else
f5385ebf 2538 h->def_regular = 1;
45d6a902
AM
2539 }
2540
2541 if (h->dynindx == -1
f5385ebf
AM
2542 && (h->def_dynamic
2543 || h->ref_dynamic))
45d6a902 2544 {
c152c796 2545 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2546 {
2547 eif->failed = TRUE;
2548 return FALSE;
2549 }
2550 }
2551 }
2552 else
2553 {
f5385ebf 2554 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2555 was first seen in a non-ELF file. Fortunately, if the symbol
2556 was first seen in an ELF file, we're probably OK unless the
2557 symbol was defined in a non-ELF file. Catch that case here.
2558 FIXME: We're still in trouble if the symbol was first seen in
2559 a dynamic object, and then later in a non-ELF regular object. */
2560 if ((h->root.type == bfd_link_hash_defined
2561 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2562 && !h->def_regular
45d6a902
AM
2563 && (h->root.u.def.section->owner != NULL
2564 ? (bfd_get_flavour (h->root.u.def.section->owner)
2565 != bfd_target_elf_flavour)
2566 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2567 && !h->def_dynamic)))
2568 h->def_regular = 1;
45d6a902
AM
2569 }
2570
508c3946 2571 /* Backend specific symbol fixup. */
33774f08
AM
2572 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2573 if (bed->elf_backend_fixup_symbol
2574 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2575 return FALSE;
508c3946 2576
45d6a902
AM
2577 /* If this is a final link, and the symbol was defined as a common
2578 symbol in a regular object file, and there was no definition in
2579 any dynamic object, then the linker will have allocated space for
f5385ebf 2580 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2581 flag will not have been set. */
2582 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2583 && !h->def_regular
2584 && h->ref_regular
2585 && !h->def_dynamic
96f29d96 2586 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2587 h->def_regular = 1;
45d6a902
AM
2588
2589 /* If -Bsymbolic was used (which means to bind references to global
2590 symbols to the definition within the shared object), and this
2591 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2592 need a PLT entry. Likewise, if the symbol has non-default
2593 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2594 will force it local. */
f5385ebf 2595 if (h->needs_plt
0e1862bb 2596 && bfd_link_pic (eif->info)
0eddce27 2597 && is_elf_hash_table (eif->info->hash)
55255dae 2598 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2599 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2600 && h->def_regular)
45d6a902 2601 {
45d6a902
AM
2602 bfd_boolean force_local;
2603
45d6a902
AM
2604 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2605 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2606 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2607 }
2608
2609 /* If a weak undefined symbol has non-default visibility, we also
2610 hide it from the dynamic linker. */
9c7a29a3 2611 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2612 && h->root.type == bfd_link_hash_undefweak)
33774f08 2613 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2614
2615 /* If this is a weak defined symbol in a dynamic object, and we know
2616 the real definition in the dynamic object, copy interesting flags
2617 over to the real definition. */
f6e332e6 2618 if (h->u.weakdef != NULL)
45d6a902 2619 {
45d6a902
AM
2620 /* If the real definition is defined by a regular object file,
2621 don't do anything special. See the longer description in
2622 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2623 if (h->u.weakdef->def_regular)
f6e332e6 2624 h->u.weakdef = NULL;
45d6a902 2625 else
a26587ba 2626 {
4e6b54a6
AM
2627 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2628
2629 while (h->root.type == bfd_link_hash_indirect)
2630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2631
2632 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2633 || h->root.type == bfd_link_hash_defweak);
2634 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2635 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2636 || weakdef->root.type == bfd_link_hash_defweak);
2637 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2638 }
45d6a902
AM
2639 }
2640
2641 return TRUE;
2642}
2643
2644/* Make the backend pick a good value for a dynamic symbol. This is
2645 called via elf_link_hash_traverse, and also calls itself
2646 recursively. */
2647
28caa186 2648static bfd_boolean
268b6b39 2649_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2650{
a50b1753 2651 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2652 bfd *dynobj;
9c5bfbb7 2653 const struct elf_backend_data *bed;
45d6a902 2654
0eddce27 2655 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2656 return FALSE;
2657
45d6a902
AM
2658 /* Ignore indirect symbols. These are added by the versioning code. */
2659 if (h->root.type == bfd_link_hash_indirect)
2660 return TRUE;
2661
2662 /* Fix the symbol flags. */
2663 if (! _bfd_elf_fix_symbol_flags (h, eif))
2664 return FALSE;
2665
2666 /* If this symbol does not require a PLT entry, and it is not
2667 defined by a dynamic object, or is not referenced by a regular
2668 object, ignore it. We do have to handle a weak defined symbol,
2669 even if no regular object refers to it, if we decided to add it
2670 to the dynamic symbol table. FIXME: Do we normally need to worry
2671 about symbols which are defined by one dynamic object and
2672 referenced by another one? */
f5385ebf 2673 if (!h->needs_plt
91e21fb7 2674 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2675 && (h->def_regular
2676 || !h->def_dynamic
2677 || (!h->ref_regular
f6e332e6 2678 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2679 {
a6aa5195 2680 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2681 return TRUE;
2682 }
2683
2684 /* If we've already adjusted this symbol, don't do it again. This
2685 can happen via a recursive call. */
f5385ebf 2686 if (h->dynamic_adjusted)
45d6a902
AM
2687 return TRUE;
2688
2689 /* Don't look at this symbol again. Note that we must set this
2690 after checking the above conditions, because we may look at a
2691 symbol once, decide not to do anything, and then get called
2692 recursively later after REF_REGULAR is set below. */
f5385ebf 2693 h->dynamic_adjusted = 1;
45d6a902
AM
2694
2695 /* If this is a weak definition, and we know a real definition, and
2696 the real symbol is not itself defined by a regular object file,
2697 then get a good value for the real definition. We handle the
2698 real symbol first, for the convenience of the backend routine.
2699
2700 Note that there is a confusing case here. If the real definition
2701 is defined by a regular object file, we don't get the real symbol
2702 from the dynamic object, but we do get the weak symbol. If the
2703 processor backend uses a COPY reloc, then if some routine in the
2704 dynamic object changes the real symbol, we will not see that
2705 change in the corresponding weak symbol. This is the way other
2706 ELF linkers work as well, and seems to be a result of the shared
2707 library model.
2708
2709 I will clarify this issue. Most SVR4 shared libraries define the
2710 variable _timezone and define timezone as a weak synonym. The
2711 tzset call changes _timezone. If you write
2712 extern int timezone;
2713 int _timezone = 5;
2714 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2715 you might expect that, since timezone is a synonym for _timezone,
2716 the same number will print both times. However, if the processor
2717 backend uses a COPY reloc, then actually timezone will be copied
2718 into your process image, and, since you define _timezone
2719 yourself, _timezone will not. Thus timezone and _timezone will
2720 wind up at different memory locations. The tzset call will set
2721 _timezone, leaving timezone unchanged. */
2722
f6e332e6 2723 if (h->u.weakdef != NULL)
45d6a902 2724 {
ec24dc88
AM
2725 /* If we get to this point, there is an implicit reference to
2726 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2727 h->u.weakdef->ref_regular = 1;
45d6a902 2728
ec24dc88
AM
2729 /* Ensure that the backend adjust_dynamic_symbol function sees
2730 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2731 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2732 return FALSE;
2733 }
2734
2735 /* If a symbol has no type and no size and does not require a PLT
2736 entry, then we are probably about to do the wrong thing here: we
2737 are probably going to create a COPY reloc for an empty object.
2738 This case can arise when a shared object is built with assembly
2739 code, and the assembly code fails to set the symbol type. */
2740 if (h->size == 0
2741 && h->type == STT_NOTYPE
f5385ebf 2742 && !h->needs_plt)
45d6a902
AM
2743 (*_bfd_error_handler)
2744 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2745 h->root.root.string);
2746
2747 dynobj = elf_hash_table (eif->info)->dynobj;
2748 bed = get_elf_backend_data (dynobj);
e7c33416 2749
45d6a902
AM
2750 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2751 {
2752 eif->failed = TRUE;
2753 return FALSE;
2754 }
2755
2756 return TRUE;
2757}
2758
027297b7
L
2759/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2760 DYNBSS. */
2761
2762bfd_boolean
6cabe1ea
AM
2763_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2764 struct elf_link_hash_entry *h,
027297b7
L
2765 asection *dynbss)
2766{
91ac5911 2767 unsigned int power_of_two;
027297b7
L
2768 bfd_vma mask;
2769 asection *sec = h->root.u.def.section;
2770
2771 /* The section aligment of definition is the maximum alignment
91ac5911
L
2772 requirement of symbols defined in the section. Since we don't
2773 know the symbol alignment requirement, we start with the
2774 maximum alignment and check low bits of the symbol address
2775 for the minimum alignment. */
2776 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2777 mask = ((bfd_vma) 1 << power_of_two) - 1;
2778 while ((h->root.u.def.value & mask) != 0)
2779 {
2780 mask >>= 1;
2781 --power_of_two;
2782 }
027297b7 2783
91ac5911
L
2784 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2785 dynbss))
027297b7
L
2786 {
2787 /* Adjust the section alignment if needed. */
2788 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2789 power_of_two))
027297b7
L
2790 return FALSE;
2791 }
2792
91ac5911 2793 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2794 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2795
2796 /* Define the symbol as being at this point in DYNBSS. */
2797 h->root.u.def.section = dynbss;
2798 h->root.u.def.value = dynbss->size;
2799
2800 /* Increment the size of DYNBSS to make room for the symbol. */
2801 dynbss->size += h->size;
2802
f7483970
L
2803 /* No error if extern_protected_data is true. */
2804 if (h->protected_def
889c2a67
L
2805 && (!info->extern_protected_data
2806 || (info->extern_protected_data < 0
2807 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2808 info->callbacks->einfo
2809 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2810 h->root.root.string);
6cabe1ea 2811
027297b7
L
2812 return TRUE;
2813}
2814
45d6a902
AM
2815/* Adjust all external symbols pointing into SEC_MERGE sections
2816 to reflect the object merging within the sections. */
2817
28caa186 2818static bfd_boolean
268b6b39 2819_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2820{
2821 asection *sec;
2822
45d6a902
AM
2823 if ((h->root.type == bfd_link_hash_defined
2824 || h->root.type == bfd_link_hash_defweak)
2825 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2826 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2827 {
a50b1753 2828 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2829
2830 h->root.u.def.value =
2831 _bfd_merged_section_offset (output_bfd,
2832 &h->root.u.def.section,
2833 elf_section_data (sec)->sec_info,
753731ee 2834 h->root.u.def.value);
45d6a902
AM
2835 }
2836
2837 return TRUE;
2838}
986a241f
RH
2839
2840/* Returns false if the symbol referred to by H should be considered
2841 to resolve local to the current module, and true if it should be
2842 considered to bind dynamically. */
2843
2844bfd_boolean
268b6b39
AM
2845_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2846 struct bfd_link_info *info,
89a2ee5a 2847 bfd_boolean not_local_protected)
986a241f
RH
2848{
2849 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2850 const struct elf_backend_data *bed;
2851 struct elf_link_hash_table *hash_table;
986a241f
RH
2852
2853 if (h == NULL)
2854 return FALSE;
2855
2856 while (h->root.type == bfd_link_hash_indirect
2857 || h->root.type == bfd_link_hash_warning)
2858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2859
2860 /* If it was forced local, then clearly it's not dynamic. */
2861 if (h->dynindx == -1)
2862 return FALSE;
f5385ebf 2863 if (h->forced_local)
986a241f
RH
2864 return FALSE;
2865
2866 /* Identify the cases where name binding rules say that a
2867 visible symbol resolves locally. */
0e1862bb
L
2868 binding_stays_local_p = (bfd_link_executable (info)
2869 || SYMBOLIC_BIND (info, h));
986a241f
RH
2870
2871 switch (ELF_ST_VISIBILITY (h->other))
2872 {
2873 case STV_INTERNAL:
2874 case STV_HIDDEN:
2875 return FALSE;
2876
2877 case STV_PROTECTED:
fcb93ecf
PB
2878 hash_table = elf_hash_table (info);
2879 if (!is_elf_hash_table (hash_table))
2880 return FALSE;
2881
2882 bed = get_elf_backend_data (hash_table->dynobj);
2883
986a241f
RH
2884 /* Proper resolution for function pointer equality may require
2885 that these symbols perhaps be resolved dynamically, even though
2886 we should be resolving them to the current module. */
89a2ee5a 2887 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2888 binding_stays_local_p = TRUE;
2889 break;
2890
2891 default:
986a241f
RH
2892 break;
2893 }
2894
aa37626c 2895 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2896 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2897 return TRUE;
2898
986a241f
RH
2899 /* Otherwise, the symbol is dynamic if binding rules don't tell
2900 us that it remains local. */
2901 return !binding_stays_local_p;
2902}
f6c52c13
AM
2903
2904/* Return true if the symbol referred to by H should be considered
2905 to resolve local to the current module, and false otherwise. Differs
2906 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2907 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2908 for the place where forced_local and dynindx == -1 are tested. If
2909 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2910 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2911 the symbol is local only for defined symbols.
2912 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2913 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2914 treatment of undefined weak symbols. For those that do not make
2915 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2916
2917bfd_boolean
268b6b39
AM
2918_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2919 struct bfd_link_info *info,
2920 bfd_boolean local_protected)
f6c52c13 2921{
fcb93ecf
PB
2922 const struct elf_backend_data *bed;
2923 struct elf_link_hash_table *hash_table;
2924
f6c52c13
AM
2925 /* If it's a local sym, of course we resolve locally. */
2926 if (h == NULL)
2927 return TRUE;
2928
d95edcac
L
2929 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2930 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2931 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2932 return TRUE;
2933
7e2294f9
AO
2934 /* Common symbols that become definitions don't get the DEF_REGULAR
2935 flag set, so test it first, and don't bail out. */
2936 if (ELF_COMMON_DEF_P (h))
2937 /* Do nothing. */;
f6c52c13 2938 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2939 resolve locally. The sym is either undefined or dynamic. */
2940 else if (!h->def_regular)
f6c52c13
AM
2941 return FALSE;
2942
2943 /* Forced local symbols resolve locally. */
f5385ebf 2944 if (h->forced_local)
f6c52c13
AM
2945 return TRUE;
2946
2947 /* As do non-dynamic symbols. */
2948 if (h->dynindx == -1)
2949 return TRUE;
2950
2951 /* At this point, we know the symbol is defined and dynamic. In an
2952 executable it must resolve locally, likewise when building symbolic
2953 shared libraries. */
0e1862bb 2954 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2955 return TRUE;
2956
2957 /* Now deal with defined dynamic symbols in shared libraries. Ones
2958 with default visibility might not resolve locally. */
2959 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2960 return FALSE;
2961
fcb93ecf
PB
2962 hash_table = elf_hash_table (info);
2963 if (!is_elf_hash_table (hash_table))
2964 return TRUE;
2965
2966 bed = get_elf_backend_data (hash_table->dynobj);
2967
f7483970
L
2968 /* If extern_protected_data is false, STV_PROTECTED non-function
2969 symbols are local. */
889c2a67
L
2970 if ((!info->extern_protected_data
2971 || (info->extern_protected_data < 0
2972 && !bed->extern_protected_data))
2973 && !bed->is_function_type (h->type))
1c16dfa5
L
2974 return TRUE;
2975
f6c52c13 2976 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2977 symbols be treated as dynamic symbols. If the address of a
2978 function not defined in an executable is set to that function's
2979 plt entry in the executable, then the address of the function in
2980 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2981 return local_protected;
2982}
e1918d23
AM
2983
2984/* Caches some TLS segment info, and ensures that the TLS segment vma is
2985 aligned. Returns the first TLS output section. */
2986
2987struct bfd_section *
2988_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2989{
2990 struct bfd_section *sec, *tls;
2991 unsigned int align = 0;
2992
2993 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2994 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2995 break;
2996 tls = sec;
2997
2998 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2999 if (sec->alignment_power > align)
3000 align = sec->alignment_power;
3001
3002 elf_hash_table (info)->tls_sec = tls;
3003
3004 /* Ensure the alignment of the first section is the largest alignment,
3005 so that the tls segment starts aligned. */
3006 if (tls != NULL)
3007 tls->alignment_power = align;
3008
3009 return tls;
3010}
0ad989f9
L
3011
3012/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3013static bfd_boolean
3014is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3015 Elf_Internal_Sym *sym)
3016{
a4d8e49b
L
3017 const struct elf_backend_data *bed;
3018
0ad989f9
L
3019 /* Local symbols do not count, but target specific ones might. */
3020 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3021 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3022 return FALSE;
3023
fcb93ecf 3024 bed = get_elf_backend_data (abfd);
0ad989f9 3025 /* Function symbols do not count. */
fcb93ecf 3026 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3027 return FALSE;
3028
3029 /* If the section is undefined, then so is the symbol. */
3030 if (sym->st_shndx == SHN_UNDEF)
3031 return FALSE;
3032
3033 /* If the symbol is defined in the common section, then
3034 it is a common definition and so does not count. */
a4d8e49b 3035 if (bed->common_definition (sym))
0ad989f9
L
3036 return FALSE;
3037
3038 /* If the symbol is in a target specific section then we
3039 must rely upon the backend to tell us what it is. */
3040 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3041 /* FIXME - this function is not coded yet:
3042
3043 return _bfd_is_global_symbol_definition (abfd, sym);
3044
3045 Instead for now assume that the definition is not global,
3046 Even if this is wrong, at least the linker will behave
3047 in the same way that it used to do. */
3048 return FALSE;
3049
3050 return TRUE;
3051}
3052
3053/* Search the symbol table of the archive element of the archive ABFD
3054 whose archive map contains a mention of SYMDEF, and determine if
3055 the symbol is defined in this element. */
3056static bfd_boolean
3057elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3058{
3059 Elf_Internal_Shdr * hdr;
3060 bfd_size_type symcount;
3061 bfd_size_type extsymcount;
3062 bfd_size_type extsymoff;
3063 Elf_Internal_Sym *isymbuf;
3064 Elf_Internal_Sym *isym;
3065 Elf_Internal_Sym *isymend;
3066 bfd_boolean result;
3067
3068 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3069 if (abfd == NULL)
3070 return FALSE;
3071
f0bf6bfd
L
3072 /* Return FALSE if the object has been claimed by plugin. */
3073 if (abfd->plugin_format == bfd_plugin_yes)
3074 return FALSE;
3075
0ad989f9
L
3076 if (! bfd_check_format (abfd, bfd_object))
3077 return FALSE;
3078
0ad989f9
L
3079 /* Select the appropriate symbol table. */
3080 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3081 hdr = &elf_tdata (abfd)->symtab_hdr;
3082 else
3083 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3084
3085 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3086
3087 /* The sh_info field of the symtab header tells us where the
3088 external symbols start. We don't care about the local symbols. */
3089 if (elf_bad_symtab (abfd))
3090 {
3091 extsymcount = symcount;
3092 extsymoff = 0;
3093 }
3094 else
3095 {
3096 extsymcount = symcount - hdr->sh_info;
3097 extsymoff = hdr->sh_info;
3098 }
3099
3100 if (extsymcount == 0)
3101 return FALSE;
3102
3103 /* Read in the symbol table. */
3104 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3105 NULL, NULL, NULL);
3106 if (isymbuf == NULL)
3107 return FALSE;
3108
3109 /* Scan the symbol table looking for SYMDEF. */
3110 result = FALSE;
3111 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3112 {
3113 const char *name;
3114
3115 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3116 isym->st_name);
3117 if (name == NULL)
3118 break;
3119
3120 if (strcmp (name, symdef->name) == 0)
3121 {
3122 result = is_global_data_symbol_definition (abfd, isym);
3123 break;
3124 }
3125 }
3126
3127 free (isymbuf);
3128
3129 return result;
3130}
3131\f
5a580b3a
AM
3132/* Add an entry to the .dynamic table. */
3133
3134bfd_boolean
3135_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3136 bfd_vma tag,
3137 bfd_vma val)
3138{
3139 struct elf_link_hash_table *hash_table;
3140 const struct elf_backend_data *bed;
3141 asection *s;
3142 bfd_size_type newsize;
3143 bfd_byte *newcontents;
3144 Elf_Internal_Dyn dyn;
3145
3146 hash_table = elf_hash_table (info);
3147 if (! is_elf_hash_table (hash_table))
3148 return FALSE;
3149
3150 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3151 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3152 BFD_ASSERT (s != NULL);
3153
eea6121a 3154 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3155 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3156 if (newcontents == NULL)
3157 return FALSE;
3158
3159 dyn.d_tag = tag;
3160 dyn.d_un.d_val = val;
eea6121a 3161 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3162
eea6121a 3163 s->size = newsize;
5a580b3a
AM
3164 s->contents = newcontents;
3165
3166 return TRUE;
3167}
3168
3169/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3170 otherwise just check whether one already exists. Returns -1 on error,
3171 1 if a DT_NEEDED tag already exists, and 0 on success. */
3172
4ad4eba5 3173static int
7e9f0867
AM
3174elf_add_dt_needed_tag (bfd *abfd,
3175 struct bfd_link_info *info,
4ad4eba5
AM
3176 const char *soname,
3177 bfd_boolean do_it)
5a580b3a
AM
3178{
3179 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3180 bfd_size_type strindex;
3181
7e9f0867
AM
3182 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3183 return -1;
3184
5a580b3a 3185 hash_table = elf_hash_table (info);
5a580b3a
AM
3186 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3187 if (strindex == (bfd_size_type) -1)
3188 return -1;
3189
02be4619 3190 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3191 {
3192 asection *sdyn;
3193 const struct elf_backend_data *bed;
3194 bfd_byte *extdyn;
3195
3196 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3197 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3198 if (sdyn != NULL)
3199 for (extdyn = sdyn->contents;
3200 extdyn < sdyn->contents + sdyn->size;
3201 extdyn += bed->s->sizeof_dyn)
3202 {
3203 Elf_Internal_Dyn dyn;
5a580b3a 3204
7e9f0867
AM
3205 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3206 if (dyn.d_tag == DT_NEEDED
3207 && dyn.d_un.d_val == strindex)
3208 {
3209 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3210 return 1;
3211 }
3212 }
5a580b3a
AM
3213 }
3214
3215 if (do_it)
3216 {
7e9f0867
AM
3217 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3218 return -1;
3219
5a580b3a
AM
3220 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3221 return -1;
3222 }
3223 else
3224 /* We were just checking for existence of the tag. */
3225 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3226
3227 return 0;
3228}
3229
010e5ae2
AM
3230static bfd_boolean
3231on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3232{
3233 for (; needed != NULL; needed = needed->next)
1240be6b
AM
3234 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3235 && strcmp (soname, needed->name) == 0)
010e5ae2
AM
3236 return TRUE;
3237
3238 return FALSE;
3239}
3240
14160578 3241/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3242static int
3243elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3244{
3245 const struct elf_link_hash_entry *h1;
3246 const struct elf_link_hash_entry *h2;
10b7e05b 3247 bfd_signed_vma vdiff;
5a580b3a
AM
3248
3249 h1 = *(const struct elf_link_hash_entry **) arg1;
3250 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3251 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3252 if (vdiff != 0)
3253 return vdiff > 0 ? 1 : -1;
3254 else
3255 {
d3435ae8 3256 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3257 if (sdiff != 0)
3258 return sdiff > 0 ? 1 : -1;
3259 }
14160578
AM
3260 vdiff = h1->size - h2->size;
3261 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3262}
4ad4eba5 3263
5a580b3a
AM
3264/* This function is used to adjust offsets into .dynstr for
3265 dynamic symbols. This is called via elf_link_hash_traverse. */
3266
3267static bfd_boolean
3268elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3269{
a50b1753 3270 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3271
5a580b3a
AM
3272 if (h->dynindx != -1)
3273 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3274 return TRUE;
3275}
3276
3277/* Assign string offsets in .dynstr, update all structures referencing
3278 them. */
3279
4ad4eba5
AM
3280static bfd_boolean
3281elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3282{
3283 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3284 struct elf_link_local_dynamic_entry *entry;
3285 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3286 bfd *dynobj = hash_table->dynobj;
3287 asection *sdyn;
3288 bfd_size_type size;
3289 const struct elf_backend_data *bed;
3290 bfd_byte *extdyn;
3291
3292 _bfd_elf_strtab_finalize (dynstr);
3293 size = _bfd_elf_strtab_size (dynstr);
3294
3295 bed = get_elf_backend_data (dynobj);
3d4d4302 3296 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3297 BFD_ASSERT (sdyn != NULL);
3298
3299 /* Update all .dynamic entries referencing .dynstr strings. */
3300 for (extdyn = sdyn->contents;
eea6121a 3301 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3302 extdyn += bed->s->sizeof_dyn)
3303 {
3304 Elf_Internal_Dyn dyn;
3305
3306 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3307 switch (dyn.d_tag)
3308 {
3309 case DT_STRSZ:
3310 dyn.d_un.d_val = size;
3311 break;
3312 case DT_NEEDED:
3313 case DT_SONAME:
3314 case DT_RPATH:
3315 case DT_RUNPATH:
3316 case DT_FILTER:
3317 case DT_AUXILIARY:
7ee314fa
AM
3318 case DT_AUDIT:
3319 case DT_DEPAUDIT:
5a580b3a
AM
3320 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3321 break;
3322 default:
3323 continue;
3324 }
3325 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3326 }
3327
3328 /* Now update local dynamic symbols. */
3329 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3330 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3331 entry->isym.st_name);
3332
3333 /* And the rest of dynamic symbols. */
3334 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3335
3336 /* Adjust version definitions. */
3337 if (elf_tdata (output_bfd)->cverdefs)
3338 {
3339 asection *s;
3340 bfd_byte *p;
3341 bfd_size_type i;
3342 Elf_Internal_Verdef def;
3343 Elf_Internal_Verdaux defaux;
3344
3d4d4302 3345 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3346 p = s->contents;
3347 do
3348 {
3349 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3350 &def);
3351 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3352 if (def.vd_aux != sizeof (Elf_External_Verdef))
3353 continue;
5a580b3a
AM
3354 for (i = 0; i < def.vd_cnt; ++i)
3355 {
3356 _bfd_elf_swap_verdaux_in (output_bfd,
3357 (Elf_External_Verdaux *) p, &defaux);
3358 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3359 defaux.vda_name);
3360 _bfd_elf_swap_verdaux_out (output_bfd,
3361 &defaux, (Elf_External_Verdaux *) p);
3362 p += sizeof (Elf_External_Verdaux);
3363 }
3364 }
3365 while (def.vd_next);
3366 }
3367
3368 /* Adjust version references. */
3369 if (elf_tdata (output_bfd)->verref)
3370 {
3371 asection *s;
3372 bfd_byte *p;
3373 bfd_size_type i;
3374 Elf_Internal_Verneed need;
3375 Elf_Internal_Vernaux needaux;
3376
3d4d4302 3377 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3378 p = s->contents;
3379 do
3380 {
3381 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3382 &need);
3383 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3384 _bfd_elf_swap_verneed_out (output_bfd, &need,
3385 (Elf_External_Verneed *) p);
3386 p += sizeof (Elf_External_Verneed);
3387 for (i = 0; i < need.vn_cnt; ++i)
3388 {
3389 _bfd_elf_swap_vernaux_in (output_bfd,
3390 (Elf_External_Vernaux *) p, &needaux);
3391 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3392 needaux.vna_name);
3393 _bfd_elf_swap_vernaux_out (output_bfd,
3394 &needaux,
3395 (Elf_External_Vernaux *) p);
3396 p += sizeof (Elf_External_Vernaux);
3397 }
3398 }
3399 while (need.vn_next);
3400 }
3401
3402 return TRUE;
3403}
3404\f
13285a1b
AM
3405/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3406 The default is to only match when the INPUT and OUTPUT are exactly
3407 the same target. */
3408
3409bfd_boolean
3410_bfd_elf_default_relocs_compatible (const bfd_target *input,
3411 const bfd_target *output)
3412{
3413 return input == output;
3414}
3415
3416/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3417 This version is used when different targets for the same architecture
3418 are virtually identical. */
3419
3420bfd_boolean
3421_bfd_elf_relocs_compatible (const bfd_target *input,
3422 const bfd_target *output)
3423{
3424 const struct elf_backend_data *obed, *ibed;
3425
3426 if (input == output)
3427 return TRUE;
3428
3429 ibed = xvec_get_elf_backend_data (input);
3430 obed = xvec_get_elf_backend_data (output);
3431
3432 if (ibed->arch != obed->arch)
3433 return FALSE;
3434
3435 /* If both backends are using this function, deem them compatible. */
3436 return ibed->relocs_compatible == obed->relocs_compatible;
3437}
3438
e5034e59
AM
3439/* Make a special call to the linker "notice" function to tell it that
3440 we are about to handle an as-needed lib, or have finished
1b786873 3441 processing the lib. */
e5034e59
AM
3442
3443bfd_boolean
3444_bfd_elf_notice_as_needed (bfd *ibfd,
3445 struct bfd_link_info *info,
3446 enum notice_asneeded_action act)
3447{
46135103 3448 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3449}
3450
4ad4eba5
AM
3451/* Add symbols from an ELF object file to the linker hash table. */
3452
3453static bfd_boolean
3454elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3455{
a0c402a5 3456 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3457 Elf_Internal_Shdr *hdr;
3458 bfd_size_type symcount;
3459 bfd_size_type extsymcount;
3460 bfd_size_type extsymoff;
3461 struct elf_link_hash_entry **sym_hash;
3462 bfd_boolean dynamic;
3463 Elf_External_Versym *extversym = NULL;
3464 Elf_External_Versym *ever;
3465 struct elf_link_hash_entry *weaks;
3466 struct elf_link_hash_entry **nondeflt_vers = NULL;
3467 bfd_size_type nondeflt_vers_cnt = 0;
3468 Elf_Internal_Sym *isymbuf = NULL;
3469 Elf_Internal_Sym *isym;
3470 Elf_Internal_Sym *isymend;
3471 const struct elf_backend_data *bed;
3472 bfd_boolean add_needed;
66eb6687 3473 struct elf_link_hash_table *htab;
4ad4eba5 3474 bfd_size_type amt;
66eb6687 3475 void *alloc_mark = NULL;
4f87808c
AM
3476 struct bfd_hash_entry **old_table = NULL;
3477 unsigned int old_size = 0;
3478 unsigned int old_count = 0;
66eb6687 3479 void *old_tab = NULL;
66eb6687
AM
3480 void *old_ent;
3481 struct bfd_link_hash_entry *old_undefs = NULL;
3482 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3483 long old_dynsymcount = 0;
a4542f1b 3484 bfd_size_type old_dynstr_size = 0;
66eb6687 3485 size_t tabsize = 0;
db6a5d5f 3486 asection *s;
29a9f53e 3487 bfd_boolean just_syms;
4ad4eba5 3488
66eb6687 3489 htab = elf_hash_table (info);
4ad4eba5 3490 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3491
3492 if ((abfd->flags & DYNAMIC) == 0)
3493 dynamic = FALSE;
3494 else
3495 {
3496 dynamic = TRUE;
3497
3498 /* You can't use -r against a dynamic object. Also, there's no
3499 hope of using a dynamic object which does not exactly match
3500 the format of the output file. */
0e1862bb 3501 if (bfd_link_relocatable (info)
66eb6687 3502 || !is_elf_hash_table (htab)
f13a99db 3503 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3504 {
0e1862bb 3505 if (bfd_link_relocatable (info))
9a0789ec
NC
3506 bfd_set_error (bfd_error_invalid_operation);
3507 else
3508 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3509 goto error_return;
3510 }
3511 }
3512
a0c402a5
L
3513 ehdr = elf_elfheader (abfd);
3514 if (info->warn_alternate_em
3515 && bed->elf_machine_code != ehdr->e_machine
3516 && ((bed->elf_machine_alt1 != 0
3517 && ehdr->e_machine == bed->elf_machine_alt1)
3518 || (bed->elf_machine_alt2 != 0
3519 && ehdr->e_machine == bed->elf_machine_alt2)))
3520 info->callbacks->einfo
3521 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3522 ehdr->e_machine, abfd, bed->elf_machine_code);
3523
4ad4eba5
AM
3524 /* As a GNU extension, any input sections which are named
3525 .gnu.warning.SYMBOL are treated as warning symbols for the given
3526 symbol. This differs from .gnu.warning sections, which generate
3527 warnings when they are included in an output file. */
dd98f8d2 3528 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3529 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3530 {
db6a5d5f 3531 const char *name;
4ad4eba5 3532
db6a5d5f
AM
3533 name = bfd_get_section_name (abfd, s);
3534 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3535 {
db6a5d5f
AM
3536 char *msg;
3537 bfd_size_type sz;
3538
3539 name += sizeof ".gnu.warning." - 1;
3540
3541 /* If this is a shared object, then look up the symbol
3542 in the hash table. If it is there, and it is already
3543 been defined, then we will not be using the entry
3544 from this shared object, so we don't need to warn.
3545 FIXME: If we see the definition in a regular object
3546 later on, we will warn, but we shouldn't. The only
3547 fix is to keep track of what warnings we are supposed
3548 to emit, and then handle them all at the end of the
3549 link. */
3550 if (dynamic)
4ad4eba5 3551 {
db6a5d5f
AM
3552 struct elf_link_hash_entry *h;
3553
3554 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3555
3556 /* FIXME: What about bfd_link_hash_common? */
3557 if (h != NULL
3558 && (h->root.type == bfd_link_hash_defined
3559 || h->root.type == bfd_link_hash_defweak))
3560 continue;
3561 }
4ad4eba5 3562
db6a5d5f
AM
3563 sz = s->size;
3564 msg = (char *) bfd_alloc (abfd, sz + 1);
3565 if (msg == NULL)
3566 goto error_return;
4ad4eba5 3567
db6a5d5f
AM
3568 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3569 goto error_return;
4ad4eba5 3570
db6a5d5f 3571 msg[sz] = '\0';
4ad4eba5 3572
db6a5d5f
AM
3573 if (! (_bfd_generic_link_add_one_symbol
3574 (info, abfd, name, BSF_WARNING, s, 0, msg,
3575 FALSE, bed->collect, NULL)))
3576 goto error_return;
4ad4eba5 3577
0e1862bb 3578 if (bfd_link_executable (info))
db6a5d5f
AM
3579 {
3580 /* Clobber the section size so that the warning does
3581 not get copied into the output file. */
3582 s->size = 0;
11d2f718 3583
db6a5d5f
AM
3584 /* Also set SEC_EXCLUDE, so that symbols defined in
3585 the warning section don't get copied to the output. */
3586 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3587 }
3588 }
3589 }
3590
29a9f53e
L
3591 just_syms = ((s = abfd->sections) != NULL
3592 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3593
4ad4eba5
AM
3594 add_needed = TRUE;
3595 if (! dynamic)
3596 {
3597 /* If we are creating a shared library, create all the dynamic
3598 sections immediately. We need to attach them to something,
3599 so we attach them to this BFD, provided it is the right
bf89386a
L
3600 format and is not from ld --just-symbols. Always create the
3601 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3602 are no input BFD's of the same format as the output, we can't
3603 make a shared library. */
3604 if (!just_syms
bf89386a
L
3605 && (bfd_link_pic (info)
3606 || info->export_dynamic
3607 || info->dynamic)
66eb6687 3608 && is_elf_hash_table (htab)
f13a99db 3609 && info->output_bfd->xvec == abfd->xvec
66eb6687 3610 && !htab->dynamic_sections_created)
4ad4eba5
AM
3611 {
3612 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3613 goto error_return;
3614 }
3615 }
66eb6687 3616 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3617 goto error_return;
3618 else
3619 {
4ad4eba5 3620 const char *soname = NULL;
7ee314fa 3621 char *audit = NULL;
4ad4eba5
AM
3622 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3623 int ret;
3624
3625 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3626 ld shouldn't allow it. */
29a9f53e 3627 if (just_syms)
92fd189d 3628 abort ();
4ad4eba5
AM
3629
3630 /* If this dynamic lib was specified on the command line with
3631 --as-needed in effect, then we don't want to add a DT_NEEDED
3632 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3633 in by another lib's DT_NEEDED. When --no-add-needed is used
3634 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3635 any dynamic library in DT_NEEDED tags in the dynamic lib at
3636 all. */
3637 add_needed = (elf_dyn_lib_class (abfd)
3638 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3639 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3640
3641 s = bfd_get_section_by_name (abfd, ".dynamic");
3642 if (s != NULL)
3643 {
3644 bfd_byte *dynbuf;
3645 bfd_byte *extdyn;
cb33740c 3646 unsigned int elfsec;
4ad4eba5
AM
3647 unsigned long shlink;
3648
eea6121a 3649 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3650 {
3651error_free_dyn:
3652 free (dynbuf);
3653 goto error_return;
3654 }
4ad4eba5
AM
3655
3656 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3657 if (elfsec == SHN_BAD)
4ad4eba5
AM
3658 goto error_free_dyn;
3659 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3660
3661 for (extdyn = dynbuf;
eea6121a 3662 extdyn < dynbuf + s->size;
4ad4eba5
AM
3663 extdyn += bed->s->sizeof_dyn)
3664 {
3665 Elf_Internal_Dyn dyn;
3666
3667 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3668 if (dyn.d_tag == DT_SONAME)
3669 {
3670 unsigned int tagv = dyn.d_un.d_val;
3671 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3672 if (soname == NULL)
3673 goto error_free_dyn;
3674 }
3675 if (dyn.d_tag == DT_NEEDED)
3676 {
3677 struct bfd_link_needed_list *n, **pn;
3678 char *fnm, *anm;
3679 unsigned int tagv = dyn.d_un.d_val;
3680
3681 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3682 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3683 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3684 if (n == NULL || fnm == NULL)
3685 goto error_free_dyn;
3686 amt = strlen (fnm) + 1;
a50b1753 3687 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3688 if (anm == NULL)
3689 goto error_free_dyn;
3690 memcpy (anm, fnm, amt);
3691 n->name = anm;
3692 n->by = abfd;
3693 n->next = NULL;
66eb6687 3694 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3695 ;
3696 *pn = n;
3697 }
3698 if (dyn.d_tag == DT_RUNPATH)
3699 {
3700 struct bfd_link_needed_list *n, **pn;
3701 char *fnm, *anm;
3702 unsigned int tagv = dyn.d_un.d_val;
3703
3704 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3705 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3706 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3707 if (n == NULL || fnm == NULL)
3708 goto error_free_dyn;
3709 amt = strlen (fnm) + 1;
a50b1753 3710 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3711 if (anm == NULL)
3712 goto error_free_dyn;
3713 memcpy (anm, fnm, amt);
3714 n->name = anm;
3715 n->by = abfd;
3716 n->next = NULL;
3717 for (pn = & runpath;
3718 *pn != NULL;
3719 pn = &(*pn)->next)
3720 ;
3721 *pn = n;
3722 }
3723 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3724 if (!runpath && dyn.d_tag == DT_RPATH)
3725 {
3726 struct bfd_link_needed_list *n, **pn;
3727 char *fnm, *anm;
3728 unsigned int tagv = dyn.d_un.d_val;
3729
3730 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3731 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3732 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3733 if (n == NULL || fnm == NULL)
3734 goto error_free_dyn;
3735 amt = strlen (fnm) + 1;
a50b1753 3736 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3737 if (anm == NULL)
f8703194 3738 goto error_free_dyn;
4ad4eba5
AM
3739 memcpy (anm, fnm, amt);
3740 n->name = anm;
3741 n->by = abfd;
3742 n->next = NULL;
3743 for (pn = & rpath;
3744 *pn != NULL;
3745 pn = &(*pn)->next)
3746 ;
3747 *pn = n;
3748 }
7ee314fa
AM
3749 if (dyn.d_tag == DT_AUDIT)
3750 {
3751 unsigned int tagv = dyn.d_un.d_val;
3752 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3753 }
4ad4eba5
AM
3754 }
3755
3756 free (dynbuf);
3757 }
3758
3759 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3760 frees all more recently bfd_alloc'd blocks as well. */
3761 if (runpath)
3762 rpath = runpath;
3763
3764 if (rpath)
3765 {
3766 struct bfd_link_needed_list **pn;
66eb6687 3767 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3768 ;
3769 *pn = rpath;
3770 }
3771
3772 /* We do not want to include any of the sections in a dynamic
3773 object in the output file. We hack by simply clobbering the
3774 list of sections in the BFD. This could be handled more
3775 cleanly by, say, a new section flag; the existing
3776 SEC_NEVER_LOAD flag is not the one we want, because that one
3777 still implies that the section takes up space in the output
3778 file. */
3779 bfd_section_list_clear (abfd);
3780
4ad4eba5
AM
3781 /* Find the name to use in a DT_NEEDED entry that refers to this
3782 object. If the object has a DT_SONAME entry, we use it.
3783 Otherwise, if the generic linker stuck something in
3784 elf_dt_name, we use that. Otherwise, we just use the file
3785 name. */
3786 if (soname == NULL || *soname == '\0')
3787 {
3788 soname = elf_dt_name (abfd);
3789 if (soname == NULL || *soname == '\0')
3790 soname = bfd_get_filename (abfd);
3791 }
3792
3793 /* Save the SONAME because sometimes the linker emulation code
3794 will need to know it. */
3795 elf_dt_name (abfd) = soname;
3796
7e9f0867 3797 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3798 if (ret < 0)
3799 goto error_return;
3800
3801 /* If we have already included this dynamic object in the
3802 link, just ignore it. There is no reason to include a
3803 particular dynamic object more than once. */
3804 if (ret > 0)
3805 return TRUE;
7ee314fa
AM
3806
3807 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3808 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3809 }
3810
3811 /* If this is a dynamic object, we always link against the .dynsym
3812 symbol table, not the .symtab symbol table. The dynamic linker
3813 will only see the .dynsym symbol table, so there is no reason to
3814 look at .symtab for a dynamic object. */
3815
3816 if (! dynamic || elf_dynsymtab (abfd) == 0)
3817 hdr = &elf_tdata (abfd)->symtab_hdr;
3818 else
3819 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3820
3821 symcount = hdr->sh_size / bed->s->sizeof_sym;
3822
3823 /* The sh_info field of the symtab header tells us where the
3824 external symbols start. We don't care about the local symbols at
3825 this point. */
3826 if (elf_bad_symtab (abfd))
3827 {
3828 extsymcount = symcount;
3829 extsymoff = 0;
3830 }
3831 else
3832 {
3833 extsymcount = symcount - hdr->sh_info;
3834 extsymoff = hdr->sh_info;
3835 }
3836
f45794cb 3837 sym_hash = elf_sym_hashes (abfd);
012b2306 3838 if (extsymcount != 0)
4ad4eba5
AM
3839 {
3840 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3841 NULL, NULL, NULL);
3842 if (isymbuf == NULL)
3843 goto error_return;
3844
4ad4eba5 3845 if (sym_hash == NULL)
012b2306
AM
3846 {
3847 /* We store a pointer to the hash table entry for each
3848 external symbol. */
3849 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3850 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3851 if (sym_hash == NULL)
3852 goto error_free_sym;
3853 elf_sym_hashes (abfd) = sym_hash;
3854 }
4ad4eba5
AM
3855 }
3856
3857 if (dynamic)
3858 {
3859 /* Read in any version definitions. */
fc0e6df6
PB
3860 if (!_bfd_elf_slurp_version_tables (abfd,
3861 info->default_imported_symver))
4ad4eba5
AM
3862 goto error_free_sym;
3863
3864 /* Read in the symbol versions, but don't bother to convert them
3865 to internal format. */
3866 if (elf_dynversym (abfd) != 0)
3867 {
3868 Elf_Internal_Shdr *versymhdr;
3869
3870 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3871 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3872 if (extversym == NULL)
3873 goto error_free_sym;
3874 amt = versymhdr->sh_size;
3875 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3876 || bfd_bread (extversym, amt, abfd) != amt)
3877 goto error_free_vers;
3878 }
3879 }
3880
66eb6687
AM
3881 /* If we are loading an as-needed shared lib, save the symbol table
3882 state before we start adding symbols. If the lib turns out
3883 to be unneeded, restore the state. */
3884 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3885 {
3886 unsigned int i;
3887 size_t entsize;
3888
3889 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3890 {
3891 struct bfd_hash_entry *p;
2de92251 3892 struct elf_link_hash_entry *h;
66eb6687
AM
3893
3894 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3895 {
3896 h = (struct elf_link_hash_entry *) p;
3897 entsize += htab->root.table.entsize;
3898 if (h->root.type == bfd_link_hash_warning)
3899 entsize += htab->root.table.entsize;
3900 }
66eb6687
AM
3901 }
3902
3903 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 3904 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
3905 if (old_tab == NULL)
3906 goto error_free_vers;
3907
3908 /* Remember the current objalloc pointer, so that all mem for
3909 symbols added can later be reclaimed. */
3910 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3911 if (alloc_mark == NULL)
3912 goto error_free_vers;
3913
5061a885
AM
3914 /* Make a special call to the linker "notice" function to
3915 tell it that we are about to handle an as-needed lib. */
e5034e59 3916 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 3917 goto error_free_vers;
5061a885 3918
f45794cb
AM
3919 /* Clone the symbol table. Remember some pointers into the
3920 symbol table, and dynamic symbol count. */
3921 old_ent = (char *) old_tab + tabsize;
66eb6687 3922 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
3923 old_undefs = htab->root.undefs;
3924 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3925 old_table = htab->root.table.table;
3926 old_size = htab->root.table.size;
3927 old_count = htab->root.table.count;
66eb6687 3928 old_dynsymcount = htab->dynsymcount;
a4542f1b 3929 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
3930
3931 for (i = 0; i < htab->root.table.size; i++)
3932 {
3933 struct bfd_hash_entry *p;
2de92251 3934 struct elf_link_hash_entry *h;
66eb6687
AM
3935
3936 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3937 {
3938 memcpy (old_ent, p, htab->root.table.entsize);
3939 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3940 h = (struct elf_link_hash_entry *) p;
3941 if (h->root.type == bfd_link_hash_warning)
3942 {
3943 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3944 old_ent = (char *) old_ent + htab->root.table.entsize;
3945 }
66eb6687
AM
3946 }
3947 }
3948 }
4ad4eba5 3949
66eb6687 3950 weaks = NULL;
4ad4eba5
AM
3951 ever = extversym != NULL ? extversym + extsymoff : NULL;
3952 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3953 isym < isymend;
3954 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3955 {
3956 int bind;
3957 bfd_vma value;
af44c138 3958 asection *sec, *new_sec;
4ad4eba5
AM
3959 flagword flags;
3960 const char *name;
3961 struct elf_link_hash_entry *h;
90c984fc 3962 struct elf_link_hash_entry *hi;
4ad4eba5
AM
3963 bfd_boolean definition;
3964 bfd_boolean size_change_ok;
3965 bfd_boolean type_change_ok;
3966 bfd_boolean new_weakdef;
37a9e49a
L
3967 bfd_boolean new_weak;
3968 bfd_boolean old_weak;
4ad4eba5 3969 bfd_boolean override;
a4d8e49b 3970 bfd_boolean common;
4ad4eba5
AM
3971 unsigned int old_alignment;
3972 bfd *old_bfd;
6e33951e 3973 bfd_boolean matched;
4ad4eba5
AM
3974
3975 override = FALSE;
3976
3977 flags = BSF_NO_FLAGS;
3978 sec = NULL;
3979 value = isym->st_value;
a4d8e49b 3980 common = bed->common_definition (isym);
4ad4eba5
AM
3981
3982 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 3983 switch (bind)
4ad4eba5 3984 {
3e7a7d11 3985 case STB_LOCAL:
4ad4eba5
AM
3986 /* This should be impossible, since ELF requires that all
3987 global symbols follow all local symbols, and that sh_info
3988 point to the first global symbol. Unfortunately, Irix 5
3989 screws this up. */
3990 continue;
3e7a7d11
NC
3991
3992 case STB_GLOBAL:
a4d8e49b 3993 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 3994 flags = BSF_GLOBAL;
3e7a7d11
NC
3995 break;
3996
3997 case STB_WEAK:
3998 flags = BSF_WEAK;
3999 break;
4000
4001 case STB_GNU_UNIQUE:
4002 flags = BSF_GNU_UNIQUE;
4003 break;
4004
4005 default:
4ad4eba5 4006 /* Leave it up to the processor backend. */
3e7a7d11 4007 break;
4ad4eba5
AM
4008 }
4009
4010 if (isym->st_shndx == SHN_UNDEF)
4011 sec = bfd_und_section_ptr;
cb33740c
AM
4012 else if (isym->st_shndx == SHN_ABS)
4013 sec = bfd_abs_section_ptr;
4014 else if (isym->st_shndx == SHN_COMMON)
4015 {
4016 sec = bfd_com_section_ptr;
4017 /* What ELF calls the size we call the value. What ELF
4018 calls the value we call the alignment. */
4019 value = isym->st_size;
4020 }
4021 else
4ad4eba5
AM
4022 {
4023 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4024 if (sec == NULL)
4025 sec = bfd_abs_section_ptr;
dbaa2011 4026 else if (discarded_section (sec))
529fcb95 4027 {
e5d08002
L
4028 /* Symbols from discarded section are undefined. We keep
4029 its visibility. */
529fcb95
PB
4030 sec = bfd_und_section_ptr;
4031 isym->st_shndx = SHN_UNDEF;
4032 }
4ad4eba5
AM
4033 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4034 value -= sec->vma;
4035 }
4ad4eba5
AM
4036
4037 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4038 isym->st_name);
4039 if (name == NULL)
4040 goto error_free_vers;
4041
4042 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4043 && (abfd->flags & BFD_PLUGIN) != 0)
4044 {
4045 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4046
4047 if (xc == NULL)
4048 {
4049 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4050 | SEC_EXCLUDE);
4051 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4052 if (xc == NULL)
4053 goto error_free_vers;
4054 }
4055 sec = xc;
4056 }
4057 else if (isym->st_shndx == SHN_COMMON
4058 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4059 && !bfd_link_relocatable (info))
4ad4eba5
AM
4060 {
4061 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4062
4063 if (tcomm == NULL)
4064 {
02d00247
AM
4065 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4066 | SEC_LINKER_CREATED);
4067 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4068 if (tcomm == NULL)
4ad4eba5
AM
4069 goto error_free_vers;
4070 }
4071 sec = tcomm;
4072 }
66eb6687 4073 else if (bed->elf_add_symbol_hook)
4ad4eba5 4074 {
66eb6687
AM
4075 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4076 &sec, &value))
4ad4eba5
AM
4077 goto error_free_vers;
4078
4079 /* The hook function sets the name to NULL if this symbol
4080 should be skipped for some reason. */
4081 if (name == NULL)
4082 continue;
4083 }
4084
4085 /* Sanity check that all possibilities were handled. */
4086 if (sec == NULL)
4087 {
4088 bfd_set_error (bfd_error_bad_value);
4089 goto error_free_vers;
4090 }
4091
191c0c42
AM
4092 /* Silently discard TLS symbols from --just-syms. There's
4093 no way to combine a static TLS block with a new TLS block
4094 for this executable. */
4095 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4096 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4097 continue;
4098
4ad4eba5
AM
4099 if (bfd_is_und_section (sec)
4100 || bfd_is_com_section (sec))
4101 definition = FALSE;
4102 else
4103 definition = TRUE;
4104
4105 size_change_ok = FALSE;
66eb6687 4106 type_change_ok = bed->type_change_ok;
37a9e49a 4107 old_weak = FALSE;
6e33951e 4108 matched = FALSE;
4ad4eba5
AM
4109 old_alignment = 0;
4110 old_bfd = NULL;
af44c138 4111 new_sec = sec;
4ad4eba5 4112
66eb6687 4113 if (is_elf_hash_table (htab))
4ad4eba5
AM
4114 {
4115 Elf_Internal_Versym iver;
4116 unsigned int vernum = 0;
4117 bfd_boolean skip;
4118
fc0e6df6 4119 if (ever == NULL)
4ad4eba5 4120 {
fc0e6df6
PB
4121 if (info->default_imported_symver)
4122 /* Use the default symbol version created earlier. */
4123 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4124 else
4125 iver.vs_vers = 0;
4126 }
4127 else
4128 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4129
4130 vernum = iver.vs_vers & VERSYM_VERSION;
4131
4132 /* If this is a hidden symbol, or if it is not version
4133 1, we append the version name to the symbol name.
cc86ff91
EB
4134 However, we do not modify a non-hidden absolute symbol
4135 if it is not a function, because it might be the version
4136 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4137 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4138 || (vernum > 1
4139 && (!bfd_is_abs_section (sec)
4140 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4141 {
4142 const char *verstr;
4143 size_t namelen, verlen, newlen;
4144 char *newname, *p;
4145
4146 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4147 {
fc0e6df6
PB
4148 if (vernum > elf_tdata (abfd)->cverdefs)
4149 verstr = NULL;
4150 else if (vernum > 1)
4151 verstr =
4152 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4153 else
4154 verstr = "";
4ad4eba5 4155
fc0e6df6 4156 if (verstr == NULL)
4ad4eba5 4157 {
fc0e6df6
PB
4158 (*_bfd_error_handler)
4159 (_("%B: %s: invalid version %u (max %d)"),
4160 abfd, name, vernum,
4161 elf_tdata (abfd)->cverdefs);
4162 bfd_set_error (bfd_error_bad_value);
4163 goto error_free_vers;
4ad4eba5 4164 }
fc0e6df6
PB
4165 }
4166 else
4167 {
4168 /* We cannot simply test for the number of
4169 entries in the VERNEED section since the
4170 numbers for the needed versions do not start
4171 at 0. */
4172 Elf_Internal_Verneed *t;
4173
4174 verstr = NULL;
4175 for (t = elf_tdata (abfd)->verref;
4176 t != NULL;
4177 t = t->vn_nextref)
4ad4eba5 4178 {
fc0e6df6 4179 Elf_Internal_Vernaux *a;
4ad4eba5 4180
fc0e6df6
PB
4181 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4182 {
4183 if (a->vna_other == vernum)
4ad4eba5 4184 {
fc0e6df6
PB
4185 verstr = a->vna_nodename;
4186 break;
4ad4eba5 4187 }
4ad4eba5 4188 }
fc0e6df6
PB
4189 if (a != NULL)
4190 break;
4191 }
4192 if (verstr == NULL)
4193 {
4194 (*_bfd_error_handler)
4195 (_("%B: %s: invalid needed version %d"),
4196 abfd, name, vernum);
4197 bfd_set_error (bfd_error_bad_value);
4198 goto error_free_vers;
4ad4eba5 4199 }
4ad4eba5 4200 }
fc0e6df6
PB
4201
4202 namelen = strlen (name);
4203 verlen = strlen (verstr);
4204 newlen = namelen + verlen + 2;
4205 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4206 && isym->st_shndx != SHN_UNDEF)
4207 ++newlen;
4208
a50b1753 4209 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4210 if (newname == NULL)
4211 goto error_free_vers;
4212 memcpy (newname, name, namelen);
4213 p = newname + namelen;
4214 *p++ = ELF_VER_CHR;
4215 /* If this is a defined non-hidden version symbol,
4216 we add another @ to the name. This indicates the
4217 default version of the symbol. */
4218 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4219 && isym->st_shndx != SHN_UNDEF)
4220 *p++ = ELF_VER_CHR;
4221 memcpy (p, verstr, verlen + 1);
4222
4223 name = newname;
4ad4eba5
AM
4224 }
4225
cd3416da
AM
4226 /* If this symbol has default visibility and the user has
4227 requested we not re-export it, then mark it as hidden. */
a0d49154 4228 if (!bfd_is_und_section (sec)
cd3416da 4229 && !dynamic
ce875075 4230 && abfd->no_export
cd3416da
AM
4231 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4232 isym->st_other = (STV_HIDDEN
4233 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4234
4f3fedcf
AM
4235 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4236 sym_hash, &old_bfd, &old_weak,
4237 &old_alignment, &skip, &override,
6e33951e
L
4238 &type_change_ok, &size_change_ok,
4239 &matched))
4ad4eba5
AM
4240 goto error_free_vers;
4241
4242 if (skip)
4243 continue;
4244
6e33951e
L
4245 /* Override a definition only if the new symbol matches the
4246 existing one. */
4247 if (override && matched)
4ad4eba5
AM
4248 definition = FALSE;
4249
4250 h = *sym_hash;
4251 while (h->root.type == bfd_link_hash_indirect
4252 || h->root.type == bfd_link_hash_warning)
4253 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4254
4ad4eba5 4255 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4256 && vernum > 1
4257 && definition)
4258 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4259 }
4260
4261 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4262 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4263 (struct bfd_link_hash_entry **) sym_hash)))
4264 goto error_free_vers;
4265
4266 h = *sym_hash;
90c984fc
L
4267 /* We need to make sure that indirect symbol dynamic flags are
4268 updated. */
4269 hi = h;
4ad4eba5
AM
4270 while (h->root.type == bfd_link_hash_indirect
4271 || h->root.type == bfd_link_hash_warning)
4272 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4273
4ad4eba5
AM
4274 *sym_hash = h;
4275
37a9e49a 4276 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4277 new_weakdef = FALSE;
4278 if (dynamic
4279 && definition
37a9e49a 4280 && new_weak
fcb93ecf 4281 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4282 && is_elf_hash_table (htab)
f6e332e6 4283 && h->u.weakdef == NULL)
4ad4eba5
AM
4284 {
4285 /* Keep a list of all weak defined non function symbols from
4286 a dynamic object, using the weakdef field. Later in this
4287 function we will set the weakdef field to the correct
4288 value. We only put non-function symbols from dynamic
4289 objects on this list, because that happens to be the only
4290 time we need to know the normal symbol corresponding to a
4291 weak symbol, and the information is time consuming to
4292 figure out. If the weakdef field is not already NULL,
4293 then this symbol was already defined by some previous
4294 dynamic object, and we will be using that previous
4295 definition anyhow. */
4296
f6e332e6 4297 h->u.weakdef = weaks;
4ad4eba5
AM
4298 weaks = h;
4299 new_weakdef = TRUE;
4300 }
4301
4302 /* Set the alignment of a common symbol. */
a4d8e49b 4303 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4304 && h->root.type == bfd_link_hash_common)
4305 {
4306 unsigned int align;
4307
a4d8e49b 4308 if (common)
af44c138
L
4309 align = bfd_log2 (isym->st_value);
4310 else
4311 {
4312 /* The new symbol is a common symbol in a shared object.
4313 We need to get the alignment from the section. */
4314 align = new_sec->alignment_power;
4315 }
595213d4 4316 if (align > old_alignment)
4ad4eba5
AM
4317 h->root.u.c.p->alignment_power = align;
4318 else
4319 h->root.u.c.p->alignment_power = old_alignment;
4320 }
4321
66eb6687 4322 if (is_elf_hash_table (htab))
4ad4eba5 4323 {
4f3fedcf
AM
4324 /* Set a flag in the hash table entry indicating the type of
4325 reference or definition we just found. A dynamic symbol
4326 is one which is referenced or defined by both a regular
4327 object and a shared object. */
4328 bfd_boolean dynsym = FALSE;
4329
4330 /* Plugin symbols aren't normal. Don't set def_regular or
4331 ref_regular for them, or make them dynamic. */
4332 if ((abfd->flags & BFD_PLUGIN) != 0)
4333 ;
4334 else if (! dynamic)
4335 {
4336 if (! definition)
4337 {
4338 h->ref_regular = 1;
4339 if (bind != STB_WEAK)
4340 h->ref_regular_nonweak = 1;
4341 }
4342 else
4343 {
4344 h->def_regular = 1;
4345 if (h->def_dynamic)
4346 {
4347 h->def_dynamic = 0;
4348 h->ref_dynamic = 1;
4349 }
4350 }
4351
4352 /* If the indirect symbol has been forced local, don't
4353 make the real symbol dynamic. */
4354 if ((h == hi || !hi->forced_local)
0e1862bb 4355 && (bfd_link_dll (info)
4f3fedcf
AM
4356 || h->def_dynamic
4357 || h->ref_dynamic))
4358 dynsym = TRUE;
4359 }
4360 else
4361 {
4362 if (! definition)
4363 {
4364 h->ref_dynamic = 1;
4365 hi->ref_dynamic = 1;
4366 }
4367 else
4368 {
4369 h->def_dynamic = 1;
4370 hi->def_dynamic = 1;
4371 }
4372
4373 /* If the indirect symbol has been forced local, don't
4374 make the real symbol dynamic. */
4375 if ((h == hi || !hi->forced_local)
4376 && (h->def_regular
4377 || h->ref_regular
4378 || (h->u.weakdef != NULL
4379 && ! new_weakdef
4380 && h->u.weakdef->dynindx != -1)))
4381 dynsym = TRUE;
4382 }
4383
4384 /* Check to see if we need to add an indirect symbol for
4385 the default name. */
4386 if (definition
4387 || (!override && h->root.type == bfd_link_hash_common))
4388 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4389 sec, value, &old_bfd, &dynsym))
4390 goto error_free_vers;
4ad4eba5
AM
4391
4392 /* Check the alignment when a common symbol is involved. This
4393 can change when a common symbol is overridden by a normal
4394 definition or a common symbol is ignored due to the old
4395 normal definition. We need to make sure the maximum
4396 alignment is maintained. */
a4d8e49b 4397 if ((old_alignment || common)
4ad4eba5
AM
4398 && h->root.type != bfd_link_hash_common)
4399 {
4400 unsigned int common_align;
4401 unsigned int normal_align;
4402 unsigned int symbol_align;
4403 bfd *normal_bfd;
4404 bfd *common_bfd;
4405
3a81e825
AM
4406 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4407 || h->root.type == bfd_link_hash_defweak);
4408
4ad4eba5
AM
4409 symbol_align = ffs (h->root.u.def.value) - 1;
4410 if (h->root.u.def.section->owner != NULL
4411 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4412 {
4413 normal_align = h->root.u.def.section->alignment_power;
4414 if (normal_align > symbol_align)
4415 normal_align = symbol_align;
4416 }
4417 else
4418 normal_align = symbol_align;
4419
4420 if (old_alignment)
4421 {
4422 common_align = old_alignment;
4423 common_bfd = old_bfd;
4424 normal_bfd = abfd;
4425 }
4426 else
4427 {
4428 common_align = bfd_log2 (isym->st_value);
4429 common_bfd = abfd;
4430 normal_bfd = old_bfd;
4431 }
4432
4433 if (normal_align < common_align)
d07676f8
NC
4434 {
4435 /* PR binutils/2735 */
4436 if (normal_bfd == NULL)
4437 (*_bfd_error_handler)
4f3fedcf
AM
4438 (_("Warning: alignment %u of common symbol `%s' in %B is"
4439 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4440 common_bfd, h->root.u.def.section,
4441 1 << common_align, name, 1 << normal_align);
4442 else
4443 (*_bfd_error_handler)
4444 (_("Warning: alignment %u of symbol `%s' in %B"
4445 " is smaller than %u in %B"),
4446 normal_bfd, common_bfd,
4447 1 << normal_align, name, 1 << common_align);
4448 }
4ad4eba5
AM
4449 }
4450
83ad0046 4451 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4452 if (isym->st_size != 0
4453 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4454 && (definition || h->size == 0))
4455 {
83ad0046
L
4456 if (h->size != 0
4457 && h->size != isym->st_size
4458 && ! size_change_ok)
4ad4eba5 4459 (*_bfd_error_handler)
d003868e
AM
4460 (_("Warning: size of symbol `%s' changed"
4461 " from %lu in %B to %lu in %B"),
4462 old_bfd, abfd,
4ad4eba5 4463 name, (unsigned long) h->size,
d003868e 4464 (unsigned long) isym->st_size);
4ad4eba5
AM
4465
4466 h->size = isym->st_size;
4467 }
4468
4469 /* If this is a common symbol, then we always want H->SIZE
4470 to be the size of the common symbol. The code just above
4471 won't fix the size if a common symbol becomes larger. We
4472 don't warn about a size change here, because that is
4f3fedcf 4473 covered by --warn-common. Allow changes between different
fcb93ecf 4474 function types. */
4ad4eba5
AM
4475 if (h->root.type == bfd_link_hash_common)
4476 h->size = h->root.u.c.size;
4477
4478 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4479 && ((definition && !new_weak)
4480 || (old_weak && h->root.type == bfd_link_hash_common)
4481 || h->type == STT_NOTYPE))
4ad4eba5 4482 {
2955ec4c
L
4483 unsigned int type = ELF_ST_TYPE (isym->st_info);
4484
4485 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4486 symbol. */
4487 if (type == STT_GNU_IFUNC
4488 && (abfd->flags & DYNAMIC) != 0)
4489 type = STT_FUNC;
4ad4eba5 4490
2955ec4c
L
4491 if (h->type != type)
4492 {
4493 if (h->type != STT_NOTYPE && ! type_change_ok)
4494 (*_bfd_error_handler)
4495 (_("Warning: type of symbol `%s' changed"
4496 " from %d to %d in %B"),
4497 abfd, name, h->type, type);
4498
4499 h->type = type;
4500 }
4ad4eba5
AM
4501 }
4502
54ac0771 4503 /* Merge st_other field. */
b8417128 4504 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4505
c3df8c14 4506 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4507 if (definition
4508 && (sec->flags & SEC_DEBUGGING)
4509 && !bfd_link_relocatable (info))
c3df8c14
AM
4510 dynsym = FALSE;
4511
4f3fedcf
AM
4512 /* Nor should we make plugin symbols dynamic. */
4513 if ((abfd->flags & BFD_PLUGIN) != 0)
4514 dynsym = FALSE;
4515
35fc36a8 4516 if (definition)
35399224
L
4517 {
4518 h->target_internal = isym->st_target_internal;
4519 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4520 }
35fc36a8 4521
4ad4eba5
AM
4522 if (definition && !dynamic)
4523 {
4524 char *p = strchr (name, ELF_VER_CHR);
4525 if (p != NULL && p[1] != ELF_VER_CHR)
4526 {
4527 /* Queue non-default versions so that .symver x, x@FOO
4528 aliases can be checked. */
66eb6687 4529 if (!nondeflt_vers)
4ad4eba5 4530 {
66eb6687
AM
4531 amt = ((isymend - isym + 1)
4532 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4533 nondeflt_vers
4534 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4535 if (!nondeflt_vers)
4536 goto error_free_vers;
4ad4eba5 4537 }
66eb6687 4538 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4539 }
4540 }
4541
4542 if (dynsym && h->dynindx == -1)
4543 {
c152c796 4544 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4545 goto error_free_vers;
f6e332e6 4546 if (h->u.weakdef != NULL
4ad4eba5 4547 && ! new_weakdef
f6e332e6 4548 && h->u.weakdef->dynindx == -1)
4ad4eba5 4549 {
66eb6687 4550 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4551 goto error_free_vers;
4552 }
4553 }
4554 else if (dynsym && h->dynindx != -1)
4555 /* If the symbol already has a dynamic index, but
4556 visibility says it should not be visible, turn it into
4557 a local symbol. */
4558 switch (ELF_ST_VISIBILITY (h->other))
4559 {
4560 case STV_INTERNAL:
4561 case STV_HIDDEN:
4562 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4563 dynsym = FALSE;
4564 break;
4565 }
4566
aef28989
L
4567 /* Don't add DT_NEEDED for references from the dummy bfd nor
4568 for unmatched symbol. */
4ad4eba5 4569 if (!add_needed
aef28989 4570 && matched
4ad4eba5 4571 && definition
010e5ae2 4572 && ((dynsym
ffa9430d 4573 && h->ref_regular_nonweak
4f3fedcf
AM
4574 && (old_bfd == NULL
4575 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4576 || (h->ref_dynamic_nonweak
010e5ae2
AM
4577 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4578 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4ad4eba5
AM
4579 {
4580 int ret;
4581 const char *soname = elf_dt_name (abfd);
4582
16e4ecc0
AM
4583 info->callbacks->minfo ("%!", soname, old_bfd,
4584 h->root.root.string);
4585
4ad4eba5
AM
4586 /* A symbol from a library loaded via DT_NEEDED of some
4587 other library is referenced by a regular object.
e56f61be 4588 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4589 --no-add-needed is used and the reference was not
4590 a weak one. */
4f3fedcf 4591 if (old_bfd != NULL
b918acf9 4592 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4593 {
4594 (*_bfd_error_handler)
3cbc5de0 4595 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4596 old_bfd, name);
ff5ac77b 4597 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4598 goto error_free_vers;
4599 }
4600
a50b1753 4601 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4602 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4603
4ad4eba5 4604 add_needed = TRUE;
7e9f0867 4605 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4606 if (ret < 0)
4607 goto error_free_vers;
4608
4609 BFD_ASSERT (ret == 0);
4610 }
4611 }
4612 }
4613
66eb6687
AM
4614 if (extversym != NULL)
4615 {
4616 free (extversym);
4617 extversym = NULL;
4618 }
4619
4620 if (isymbuf != NULL)
4621 {
4622 free (isymbuf);
4623 isymbuf = NULL;
4624 }
4625
4626 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4627 {
4628 unsigned int i;
4629
4630 /* Restore the symbol table. */
f45794cb
AM
4631 old_ent = (char *) old_tab + tabsize;
4632 memset (elf_sym_hashes (abfd), 0,
4633 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4634 htab->root.table.table = old_table;
4635 htab->root.table.size = old_size;
4636 htab->root.table.count = old_count;
66eb6687 4637 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4638 htab->root.undefs = old_undefs;
4639 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4640 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4641 for (i = 0; i < htab->root.table.size; i++)
4642 {
4643 struct bfd_hash_entry *p;
4644 struct elf_link_hash_entry *h;
3e0882af
L
4645 bfd_size_type size;
4646 unsigned int alignment_power;
66eb6687
AM
4647
4648 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4649 {
4650 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4651 if (h->root.type == bfd_link_hash_warning)
4652 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4653 if (h->dynindx >= old_dynsymcount
4654 && h->dynstr_index < old_dynstr_size)
66eb6687 4655 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4656
3e0882af
L
4657 /* Preserve the maximum alignment and size for common
4658 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4659 since it can still be loaded at run time by another
3e0882af
L
4660 dynamic lib. */
4661 if (h->root.type == bfd_link_hash_common)
4662 {
4663 size = h->root.u.c.size;
4664 alignment_power = h->root.u.c.p->alignment_power;
4665 }
4666 else
4667 {
4668 size = 0;
4669 alignment_power = 0;
4670 }
66eb6687
AM
4671 memcpy (p, old_ent, htab->root.table.entsize);
4672 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4673 h = (struct elf_link_hash_entry *) p;
4674 if (h->root.type == bfd_link_hash_warning)
4675 {
4676 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4677 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4679 }
a4542f1b 4680 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4681 {
4682 if (size > h->root.u.c.size)
4683 h->root.u.c.size = size;
4684 if (alignment_power > h->root.u.c.p->alignment_power)
4685 h->root.u.c.p->alignment_power = alignment_power;
4686 }
66eb6687
AM
4687 }
4688 }
4689
5061a885
AM
4690 /* Make a special call to the linker "notice" function to
4691 tell it that symbols added for crefs may need to be removed. */
e5034e59 4692 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4693 goto error_free_vers;
5061a885 4694
66eb6687
AM
4695 free (old_tab);
4696 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4697 alloc_mark);
4698 if (nondeflt_vers != NULL)
4699 free (nondeflt_vers);
4700 return TRUE;
4701 }
2de92251 4702
66eb6687
AM
4703 if (old_tab != NULL)
4704 {
e5034e59 4705 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4706 goto error_free_vers;
66eb6687
AM
4707 free (old_tab);
4708 old_tab = NULL;
4709 }
4710
c6e8a9a8
L
4711 /* Now that all the symbols from this input file are created, if
4712 not performing a relocatable link, handle .symver foo, foo@BAR
4713 such that any relocs against foo become foo@BAR. */
0e1862bb 4714 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4715 {
4716 bfd_size_type cnt, symidx;
4717
4718 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4719 {
4720 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4721 char *shortname, *p;
4722
4723 p = strchr (h->root.root.string, ELF_VER_CHR);
4724 if (p == NULL
4725 || (h->root.type != bfd_link_hash_defined
4726 && h->root.type != bfd_link_hash_defweak))
4727 continue;
4728
4729 amt = p - h->root.root.string;
a50b1753 4730 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4731 if (!shortname)
4732 goto error_free_vers;
4ad4eba5
AM
4733 memcpy (shortname, h->root.root.string, amt);
4734 shortname[amt] = '\0';
4735
4736 hi = (struct elf_link_hash_entry *)
66eb6687 4737 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4738 FALSE, FALSE, FALSE);
4739 if (hi != NULL
4740 && hi->root.type == h->root.type
4741 && hi->root.u.def.value == h->root.u.def.value
4742 && hi->root.u.def.section == h->root.u.def.section)
4743 {
4744 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4745 hi->root.type = bfd_link_hash_indirect;
4746 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4747 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4748 sym_hash = elf_sym_hashes (abfd);
4749 if (sym_hash)
4750 for (symidx = 0; symidx < extsymcount; ++symidx)
4751 if (sym_hash[symidx] == hi)
4752 {
4753 sym_hash[symidx] = h;
4754 break;
4755 }
4756 }
4757 free (shortname);
4758 }
4759 free (nondeflt_vers);
4760 nondeflt_vers = NULL;
4761 }
4762
4ad4eba5
AM
4763 /* Now set the weakdefs field correctly for all the weak defined
4764 symbols we found. The only way to do this is to search all the
4765 symbols. Since we only need the information for non functions in
4766 dynamic objects, that's the only time we actually put anything on
4767 the list WEAKS. We need this information so that if a regular
4768 object refers to a symbol defined weakly in a dynamic object, the
4769 real symbol in the dynamic object is also put in the dynamic
4770 symbols; we also must arrange for both symbols to point to the
4771 same memory location. We could handle the general case of symbol
4772 aliasing, but a general symbol alias can only be generated in
4773 assembler code, handling it correctly would be very time
4774 consuming, and other ELF linkers don't handle general aliasing
4775 either. */
4776 if (weaks != NULL)
4777 {
4778 struct elf_link_hash_entry **hpp;
4779 struct elf_link_hash_entry **hppend;
4780 struct elf_link_hash_entry **sorted_sym_hash;
4781 struct elf_link_hash_entry *h;
4782 size_t sym_count;
4783
4784 /* Since we have to search the whole symbol list for each weak
4785 defined symbol, search time for N weak defined symbols will be
4786 O(N^2). Binary search will cut it down to O(NlogN). */
4787 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4788 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4789 if (sorted_sym_hash == NULL)
4790 goto error_return;
4791 sym_hash = sorted_sym_hash;
4792 hpp = elf_sym_hashes (abfd);
4793 hppend = hpp + extsymcount;
4794 sym_count = 0;
4795 for (; hpp < hppend; hpp++)
4796 {
4797 h = *hpp;
4798 if (h != NULL
4799 && h->root.type == bfd_link_hash_defined
fcb93ecf 4800 && !bed->is_function_type (h->type))
4ad4eba5
AM
4801 {
4802 *sym_hash = h;
4803 sym_hash++;
4804 sym_count++;
4805 }
4806 }
4807
4808 qsort (sorted_sym_hash, sym_count,
4809 sizeof (struct elf_link_hash_entry *),
4810 elf_sort_symbol);
4811
4812 while (weaks != NULL)
4813 {
4814 struct elf_link_hash_entry *hlook;
4815 asection *slook;
4816 bfd_vma vlook;
ed54588d 4817 size_t i, j, idx = 0;
4ad4eba5
AM
4818
4819 hlook = weaks;
f6e332e6
AM
4820 weaks = hlook->u.weakdef;
4821 hlook->u.weakdef = NULL;
4ad4eba5
AM
4822
4823 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4824 || hlook->root.type == bfd_link_hash_defweak
4825 || hlook->root.type == bfd_link_hash_common
4826 || hlook->root.type == bfd_link_hash_indirect);
4827 slook = hlook->root.u.def.section;
4828 vlook = hlook->root.u.def.value;
4829
4ad4eba5
AM
4830 i = 0;
4831 j = sym_count;
14160578 4832 while (i != j)
4ad4eba5
AM
4833 {
4834 bfd_signed_vma vdiff;
4835 idx = (i + j) / 2;
14160578 4836 h = sorted_sym_hash[idx];
4ad4eba5
AM
4837 vdiff = vlook - h->root.u.def.value;
4838 if (vdiff < 0)
4839 j = idx;
4840 else if (vdiff > 0)
4841 i = idx + 1;
4842 else
4843 {
d3435ae8 4844 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4845 if (sdiff < 0)
4846 j = idx;
4847 else if (sdiff > 0)
4848 i = idx + 1;
4849 else
14160578 4850 break;
4ad4eba5
AM
4851 }
4852 }
4853
4854 /* We didn't find a value/section match. */
14160578 4855 if (i == j)
4ad4eba5
AM
4856 continue;
4857
14160578
AM
4858 /* With multiple aliases, or when the weak symbol is already
4859 strongly defined, we have multiple matching symbols and
4860 the binary search above may land on any of them. Step
4861 one past the matching symbol(s). */
4862 while (++idx != j)
4863 {
4864 h = sorted_sym_hash[idx];
4865 if (h->root.u.def.section != slook
4866 || h->root.u.def.value != vlook)
4867 break;
4868 }
4869
4870 /* Now look back over the aliases. Since we sorted by size
4871 as well as value and section, we'll choose the one with
4872 the largest size. */
4873 while (idx-- != i)
4ad4eba5 4874 {
14160578 4875 h = sorted_sym_hash[idx];
4ad4eba5
AM
4876
4877 /* Stop if value or section doesn't match. */
14160578
AM
4878 if (h->root.u.def.section != slook
4879 || h->root.u.def.value != vlook)
4ad4eba5
AM
4880 break;
4881 else if (h != hlook)
4882 {
f6e332e6 4883 hlook->u.weakdef = h;
4ad4eba5
AM
4884
4885 /* If the weak definition is in the list of dynamic
4886 symbols, make sure the real definition is put
4887 there as well. */
4888 if (hlook->dynindx != -1 && h->dynindx == -1)
4889 {
c152c796 4890 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4891 {
4892 err_free_sym_hash:
4893 free (sorted_sym_hash);
4894 goto error_return;
4895 }
4ad4eba5
AM
4896 }
4897
4898 /* If the real definition is in the list of dynamic
4899 symbols, make sure the weak definition is put
4900 there as well. If we don't do this, then the
4901 dynamic loader might not merge the entries for the
4902 real definition and the weak definition. */
4903 if (h->dynindx != -1 && hlook->dynindx == -1)
4904 {
c152c796 4905 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 4906 goto err_free_sym_hash;
4ad4eba5
AM
4907 }
4908 break;
4909 }
4910 }
4911 }
4912
4913 free (sorted_sym_hash);
4914 }
4915
33177bb1
AM
4916 if (bed->check_directives
4917 && !(*bed->check_directives) (abfd, info))
4918 return FALSE;
85fbca6a 4919
4ad4eba5
AM
4920 /* If this object is the same format as the output object, and it is
4921 not a shared library, then let the backend look through the
4922 relocs.
4923
4924 This is required to build global offset table entries and to
4925 arrange for dynamic relocs. It is not required for the
4926 particular common case of linking non PIC code, even when linking
4927 against shared libraries, but unfortunately there is no way of
4928 knowing whether an object file has been compiled PIC or not.
4929 Looking through the relocs is not particularly time consuming.
4930 The problem is that we must either (1) keep the relocs in memory,
4931 which causes the linker to require additional runtime memory or
4932 (2) read the relocs twice from the input file, which wastes time.
4933 This would be a good case for using mmap.
4934
4935 I have no idea how to handle linking PIC code into a file of a
4936 different format. It probably can't be done. */
4ad4eba5 4937 if (! dynamic
66eb6687 4938 && is_elf_hash_table (htab)
13285a1b 4939 && bed->check_relocs != NULL
39334f3a 4940 && elf_object_id (abfd) == elf_hash_table_id (htab)
f13a99db 4941 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4ad4eba5
AM
4942 {
4943 asection *o;
4944
4945 for (o = abfd->sections; o != NULL; o = o->next)
4946 {
4947 Elf_Internal_Rela *internal_relocs;
4948 bfd_boolean ok;
4949
4950 if ((o->flags & SEC_RELOC) == 0
4951 || o->reloc_count == 0
4952 || ((info->strip == strip_all || info->strip == strip_debugger)
4953 && (o->flags & SEC_DEBUGGING) != 0)
4954 || bfd_is_abs_section (o->output_section))
4955 continue;
4956
4957 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4958 info->keep_memory);
4959 if (internal_relocs == NULL)
4960 goto error_return;
4961
66eb6687 4962 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4963
4964 if (elf_section_data (o)->relocs != internal_relocs)
4965 free (internal_relocs);
4966
4967 if (! ok)
4968 goto error_return;
4969 }
4970 }
4971
4972 /* If this is a non-traditional link, try to optimize the handling
4973 of the .stab/.stabstr sections. */
4974 if (! dynamic
4975 && ! info->traditional_format
66eb6687 4976 && is_elf_hash_table (htab)
4ad4eba5
AM
4977 && (info->strip != strip_all && info->strip != strip_debugger))
4978 {
4979 asection *stabstr;
4980
4981 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4982 if (stabstr != NULL)
4983 {
4984 bfd_size_type string_offset = 0;
4985 asection *stab;
4986
4987 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4988 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4989 && (!stab->name[5] ||
4990 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4991 && (stab->flags & SEC_MERGE) == 0
4992 && !bfd_is_abs_section (stab->output_section))
4993 {
4994 struct bfd_elf_section_data *secdata;
4995
4996 secdata = elf_section_data (stab);
66eb6687
AM
4997 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4998 stabstr, &secdata->sec_info,
4ad4eba5
AM
4999 &string_offset))
5000 goto error_return;
5001 if (secdata->sec_info)
dbaa2011 5002 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5003 }
5004 }
5005 }
5006
66eb6687 5007 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5008 {
5009 /* Add this bfd to the loaded list. */
5010 struct elf_link_loaded_list *n;
5011
ca4be51c 5012 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5013 if (n == NULL)
5014 goto error_return;
5015 n->abfd = abfd;
66eb6687
AM
5016 n->next = htab->loaded;
5017 htab->loaded = n;
4ad4eba5
AM
5018 }
5019
5020 return TRUE;
5021
5022 error_free_vers:
66eb6687
AM
5023 if (old_tab != NULL)
5024 free (old_tab);
4ad4eba5
AM
5025 if (nondeflt_vers != NULL)
5026 free (nondeflt_vers);
5027 if (extversym != NULL)
5028 free (extversym);
5029 error_free_sym:
5030 if (isymbuf != NULL)
5031 free (isymbuf);
5032 error_return:
5033 return FALSE;
5034}
5035
8387904d
AM
5036/* Return the linker hash table entry of a symbol that might be
5037 satisfied by an archive symbol. Return -1 on error. */
5038
5039struct elf_link_hash_entry *
5040_bfd_elf_archive_symbol_lookup (bfd *abfd,
5041 struct bfd_link_info *info,
5042 const char *name)
5043{
5044 struct elf_link_hash_entry *h;
5045 char *p, *copy;
5046 size_t len, first;
5047
2a41f396 5048 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5049 if (h != NULL)
5050 return h;
5051
5052 /* If this is a default version (the name contains @@), look up the
5053 symbol again with only one `@' as well as without the version.
5054 The effect is that references to the symbol with and without the
5055 version will be matched by the default symbol in the archive. */
5056
5057 p = strchr (name, ELF_VER_CHR);
5058 if (p == NULL || p[1] != ELF_VER_CHR)
5059 return h;
5060
5061 /* First check with only one `@'. */
5062 len = strlen (name);
a50b1753 5063 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5064 if (copy == NULL)
5065 return (struct elf_link_hash_entry *) 0 - 1;
5066
5067 first = p - name + 1;
5068 memcpy (copy, name, first);
5069 memcpy (copy + first, name + first + 1, len - first);
5070
2a41f396 5071 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5072 if (h == NULL)
5073 {
5074 /* We also need to check references to the symbol without the
5075 version. */
5076 copy[first - 1] = '\0';
5077 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5078 FALSE, FALSE, TRUE);
8387904d
AM
5079 }
5080
5081 bfd_release (abfd, copy);
5082 return h;
5083}
5084
0ad989f9 5085/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5086 don't use _bfd_generic_link_add_archive_symbols because we need to
5087 handle versioned symbols.
0ad989f9
L
5088
5089 Fortunately, ELF archive handling is simpler than that done by
5090 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5091 oddities. In ELF, if we find a symbol in the archive map, and the
5092 symbol is currently undefined, we know that we must pull in that
5093 object file.
5094
5095 Unfortunately, we do have to make multiple passes over the symbol
5096 table until nothing further is resolved. */
5097
4ad4eba5
AM
5098static bfd_boolean
5099elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5100{
5101 symindex c;
13e570f8 5102 unsigned char *included = NULL;
0ad989f9
L
5103 carsym *symdefs;
5104 bfd_boolean loop;
5105 bfd_size_type amt;
8387904d
AM
5106 const struct elf_backend_data *bed;
5107 struct elf_link_hash_entry * (*archive_symbol_lookup)
5108 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5109
5110 if (! bfd_has_map (abfd))
5111 {
5112 /* An empty archive is a special case. */
5113 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5114 return TRUE;
5115 bfd_set_error (bfd_error_no_armap);
5116 return FALSE;
5117 }
5118
5119 /* Keep track of all symbols we know to be already defined, and all
5120 files we know to be already included. This is to speed up the
5121 second and subsequent passes. */
5122 c = bfd_ardata (abfd)->symdef_count;
5123 if (c == 0)
5124 return TRUE;
5125 amt = c;
13e570f8
AM
5126 amt *= sizeof (*included);
5127 included = (unsigned char *) bfd_zmalloc (amt);
5128 if (included == NULL)
5129 return FALSE;
0ad989f9
L
5130
5131 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5132 bed = get_elf_backend_data (abfd);
5133 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5134
5135 do
5136 {
5137 file_ptr last;
5138 symindex i;
5139 carsym *symdef;
5140 carsym *symdefend;
5141
5142 loop = FALSE;
5143 last = -1;
5144
5145 symdef = symdefs;
5146 symdefend = symdef + c;
5147 for (i = 0; symdef < symdefend; symdef++, i++)
5148 {
5149 struct elf_link_hash_entry *h;
5150 bfd *element;
5151 struct bfd_link_hash_entry *undefs_tail;
5152 symindex mark;
5153
13e570f8 5154 if (included[i])
0ad989f9
L
5155 continue;
5156 if (symdef->file_offset == last)
5157 {
5158 included[i] = TRUE;
5159 continue;
5160 }
5161
8387904d
AM
5162 h = archive_symbol_lookup (abfd, info, symdef->name);
5163 if (h == (struct elf_link_hash_entry *) 0 - 1)
5164 goto error_return;
0ad989f9
L
5165
5166 if (h == NULL)
5167 continue;
5168
5169 if (h->root.type == bfd_link_hash_common)
5170 {
5171 /* We currently have a common symbol. The archive map contains
5172 a reference to this symbol, so we may want to include it. We
5173 only want to include it however, if this archive element
5174 contains a definition of the symbol, not just another common
5175 declaration of it.
5176
5177 Unfortunately some archivers (including GNU ar) will put
5178 declarations of common symbols into their archive maps, as
5179 well as real definitions, so we cannot just go by the archive
5180 map alone. Instead we must read in the element's symbol
5181 table and check that to see what kind of symbol definition
5182 this is. */
5183 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5184 continue;
5185 }
5186 else if (h->root.type != bfd_link_hash_undefined)
5187 {
5188 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5189 /* Symbol must be defined. Don't check it again. */
5190 included[i] = TRUE;
0ad989f9
L
5191 continue;
5192 }
5193
5194 /* We need to include this archive member. */
5195 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5196 if (element == NULL)
5197 goto error_return;
5198
5199 if (! bfd_check_format (element, bfd_object))
5200 goto error_return;
5201
0ad989f9
L
5202 undefs_tail = info->hash->undefs_tail;
5203
0e144ba7
AM
5204 if (!(*info->callbacks
5205 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5206 goto error_return;
0e144ba7 5207 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5208 goto error_return;
5209
5210 /* If there are any new undefined symbols, we need to make
5211 another pass through the archive in order to see whether
5212 they can be defined. FIXME: This isn't perfect, because
5213 common symbols wind up on undefs_tail and because an
5214 undefined symbol which is defined later on in this pass
5215 does not require another pass. This isn't a bug, but it
5216 does make the code less efficient than it could be. */
5217 if (undefs_tail != info->hash->undefs_tail)
5218 loop = TRUE;
5219
5220 /* Look backward to mark all symbols from this object file
5221 which we have already seen in this pass. */
5222 mark = i;
5223 do
5224 {
5225 included[mark] = TRUE;
5226 if (mark == 0)
5227 break;
5228 --mark;
5229 }
5230 while (symdefs[mark].file_offset == symdef->file_offset);
5231
5232 /* We mark subsequent symbols from this object file as we go
5233 on through the loop. */
5234 last = symdef->file_offset;
5235 }
5236 }
5237 while (loop);
5238
0ad989f9
L
5239 free (included);
5240
5241 return TRUE;
5242
5243 error_return:
0ad989f9
L
5244 if (included != NULL)
5245 free (included);
5246 return FALSE;
5247}
4ad4eba5
AM
5248
5249/* Given an ELF BFD, add symbols to the global hash table as
5250 appropriate. */
5251
5252bfd_boolean
5253bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5254{
5255 switch (bfd_get_format (abfd))
5256 {
5257 case bfd_object:
5258 return elf_link_add_object_symbols (abfd, info);
5259 case bfd_archive:
5260 return elf_link_add_archive_symbols (abfd, info);
5261 default:
5262 bfd_set_error (bfd_error_wrong_format);
5263 return FALSE;
5264 }
5265}
5a580b3a 5266\f
14b1c01e
AM
5267struct hash_codes_info
5268{
5269 unsigned long *hashcodes;
5270 bfd_boolean error;
5271};
a0c8462f 5272
5a580b3a
AM
5273/* This function will be called though elf_link_hash_traverse to store
5274 all hash value of the exported symbols in an array. */
5275
5276static bfd_boolean
5277elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5278{
a50b1753 5279 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5280 const char *name;
5a580b3a
AM
5281 unsigned long ha;
5282 char *alc = NULL;
5283
5a580b3a
AM
5284 /* Ignore indirect symbols. These are added by the versioning code. */
5285 if (h->dynindx == -1)
5286 return TRUE;
5287
5288 name = h->root.root.string;
422f1182 5289 if (h->versioned >= versioned)
5a580b3a 5290 {
422f1182
L
5291 char *p = strchr (name, ELF_VER_CHR);
5292 if (p != NULL)
14b1c01e 5293 {
422f1182
L
5294 alc = (char *) bfd_malloc (p - name + 1);
5295 if (alc == NULL)
5296 {
5297 inf->error = TRUE;
5298 return FALSE;
5299 }
5300 memcpy (alc, name, p - name);
5301 alc[p - name] = '\0';
5302 name = alc;
14b1c01e 5303 }
5a580b3a
AM
5304 }
5305
5306 /* Compute the hash value. */
5307 ha = bfd_elf_hash (name);
5308
5309 /* Store the found hash value in the array given as the argument. */
14b1c01e 5310 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5311
5312 /* And store it in the struct so that we can put it in the hash table
5313 later. */
f6e332e6 5314 h->u.elf_hash_value = ha;
5a580b3a
AM
5315
5316 if (alc != NULL)
5317 free (alc);
5318
5319 return TRUE;
5320}
5321
fdc90cb4
JJ
5322struct collect_gnu_hash_codes
5323{
5324 bfd *output_bfd;
5325 const struct elf_backend_data *bed;
5326 unsigned long int nsyms;
5327 unsigned long int maskbits;
5328 unsigned long int *hashcodes;
5329 unsigned long int *hashval;
5330 unsigned long int *indx;
5331 unsigned long int *counts;
5332 bfd_vma *bitmask;
5333 bfd_byte *contents;
5334 long int min_dynindx;
5335 unsigned long int bucketcount;
5336 unsigned long int symindx;
5337 long int local_indx;
5338 long int shift1, shift2;
5339 unsigned long int mask;
14b1c01e 5340 bfd_boolean error;
fdc90cb4
JJ
5341};
5342
5343/* This function will be called though elf_link_hash_traverse to store
5344 all hash value of the exported symbols in an array. */
5345
5346static bfd_boolean
5347elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5348{
a50b1753 5349 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5350 const char *name;
fdc90cb4
JJ
5351 unsigned long ha;
5352 char *alc = NULL;
5353
fdc90cb4
JJ
5354 /* Ignore indirect symbols. These are added by the versioning code. */
5355 if (h->dynindx == -1)
5356 return TRUE;
5357
5358 /* Ignore also local symbols and undefined symbols. */
5359 if (! (*s->bed->elf_hash_symbol) (h))
5360 return TRUE;
5361
5362 name = h->root.root.string;
422f1182 5363 if (h->versioned >= versioned)
fdc90cb4 5364 {
422f1182
L
5365 char *p = strchr (name, ELF_VER_CHR);
5366 if (p != NULL)
14b1c01e 5367 {
422f1182
L
5368 alc = (char *) bfd_malloc (p - name + 1);
5369 if (alc == NULL)
5370 {
5371 s->error = TRUE;
5372 return FALSE;
5373 }
5374 memcpy (alc, name, p - name);
5375 alc[p - name] = '\0';
5376 name = alc;
14b1c01e 5377 }
fdc90cb4
JJ
5378 }
5379
5380 /* Compute the hash value. */
5381 ha = bfd_elf_gnu_hash (name);
5382
5383 /* Store the found hash value in the array for compute_bucket_count,
5384 and also for .dynsym reordering purposes. */
5385 s->hashcodes[s->nsyms] = ha;
5386 s->hashval[h->dynindx] = ha;
5387 ++s->nsyms;
5388 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5389 s->min_dynindx = h->dynindx;
5390
5391 if (alc != NULL)
5392 free (alc);
5393
5394 return TRUE;
5395}
5396
5397/* This function will be called though elf_link_hash_traverse to do
5398 final dynaminc symbol renumbering. */
5399
5400static bfd_boolean
5401elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5402{
a50b1753 5403 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5404 unsigned long int bucket;
5405 unsigned long int val;
5406
fdc90cb4
JJ
5407 /* Ignore indirect symbols. */
5408 if (h->dynindx == -1)
5409 return TRUE;
5410
5411 /* Ignore also local symbols and undefined symbols. */
5412 if (! (*s->bed->elf_hash_symbol) (h))
5413 {
5414 if (h->dynindx >= s->min_dynindx)
5415 h->dynindx = s->local_indx++;
5416 return TRUE;
5417 }
5418
5419 bucket = s->hashval[h->dynindx] % s->bucketcount;
5420 val = (s->hashval[h->dynindx] >> s->shift1)
5421 & ((s->maskbits >> s->shift1) - 1);
5422 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5423 s->bitmask[val]
5424 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5425 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5426 if (s->counts[bucket] == 1)
5427 /* Last element terminates the chain. */
5428 val |= 1;
5429 bfd_put_32 (s->output_bfd, val,
5430 s->contents + (s->indx[bucket] - s->symindx) * 4);
5431 --s->counts[bucket];
5432 h->dynindx = s->indx[bucket]++;
5433 return TRUE;
5434}
5435
5436/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5437
5438bfd_boolean
5439_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5440{
5441 return !(h->forced_local
5442 || h->root.type == bfd_link_hash_undefined
5443 || h->root.type == bfd_link_hash_undefweak
5444 || ((h->root.type == bfd_link_hash_defined
5445 || h->root.type == bfd_link_hash_defweak)
5446 && h->root.u.def.section->output_section == NULL));
5447}
5448
5a580b3a
AM
5449/* Array used to determine the number of hash table buckets to use
5450 based on the number of symbols there are. If there are fewer than
5451 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5452 fewer than 37 we use 17 buckets, and so forth. We never use more
5453 than 32771 buckets. */
5454
5455static const size_t elf_buckets[] =
5456{
5457 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5458 16411, 32771, 0
5459};
5460
5461/* Compute bucket count for hashing table. We do not use a static set
5462 of possible tables sizes anymore. Instead we determine for all
5463 possible reasonable sizes of the table the outcome (i.e., the
5464 number of collisions etc) and choose the best solution. The
5465 weighting functions are not too simple to allow the table to grow
5466 without bounds. Instead one of the weighting factors is the size.
5467 Therefore the result is always a good payoff between few collisions
5468 (= short chain lengths) and table size. */
5469static size_t
b20dd2ce 5470compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5471 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5472 unsigned long int nsyms,
5473 int gnu_hash)
5a580b3a 5474{
5a580b3a 5475 size_t best_size = 0;
5a580b3a 5476 unsigned long int i;
5a580b3a 5477
5a580b3a
AM
5478 /* We have a problem here. The following code to optimize the table
5479 size requires an integer type with more the 32 bits. If
5480 BFD_HOST_U_64_BIT is set we know about such a type. */
5481#ifdef BFD_HOST_U_64_BIT
5482 if (info->optimize)
5483 {
5a580b3a
AM
5484 size_t minsize;
5485 size_t maxsize;
5486 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5487 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5488 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5489 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5490 unsigned long int *counts;
d40f3da9 5491 bfd_size_type amt;
0883b6e0 5492 unsigned int no_improvement_count = 0;
5a580b3a
AM
5493
5494 /* Possible optimization parameters: if we have NSYMS symbols we say
5495 that the hashing table must at least have NSYMS/4 and at most
5496 2*NSYMS buckets. */
5497 minsize = nsyms / 4;
5498 if (minsize == 0)
5499 minsize = 1;
5500 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5501 if (gnu_hash)
5502 {
5503 if (minsize < 2)
5504 minsize = 2;
5505 if ((best_size & 31) == 0)
5506 ++best_size;
5507 }
5a580b3a
AM
5508
5509 /* Create array where we count the collisions in. We must use bfd_malloc
5510 since the size could be large. */
5511 amt = maxsize;
5512 amt *= sizeof (unsigned long int);
a50b1753 5513 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5514 if (counts == NULL)
fdc90cb4 5515 return 0;
5a580b3a
AM
5516
5517 /* Compute the "optimal" size for the hash table. The criteria is a
5518 minimal chain length. The minor criteria is (of course) the size
5519 of the table. */
5520 for (i = minsize; i < maxsize; ++i)
5521 {
5522 /* Walk through the array of hashcodes and count the collisions. */
5523 BFD_HOST_U_64_BIT max;
5524 unsigned long int j;
5525 unsigned long int fact;
5526
fdc90cb4
JJ
5527 if (gnu_hash && (i & 31) == 0)
5528 continue;
5529
5a580b3a
AM
5530 memset (counts, '\0', i * sizeof (unsigned long int));
5531
5532 /* Determine how often each hash bucket is used. */
5533 for (j = 0; j < nsyms; ++j)
5534 ++counts[hashcodes[j] % i];
5535
5536 /* For the weight function we need some information about the
5537 pagesize on the target. This is information need not be 100%
5538 accurate. Since this information is not available (so far) we
5539 define it here to a reasonable default value. If it is crucial
5540 to have a better value some day simply define this value. */
5541# ifndef BFD_TARGET_PAGESIZE
5542# define BFD_TARGET_PAGESIZE (4096)
5543# endif
5544
fdc90cb4
JJ
5545 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5546 and the chains. */
5547 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5548
5549# if 1
5550 /* Variant 1: optimize for short chains. We add the squares
5551 of all the chain lengths (which favors many small chain
5552 over a few long chains). */
5553 for (j = 0; j < i; ++j)
5554 max += counts[j] * counts[j];
5555
5556 /* This adds penalties for the overall size of the table. */
fdc90cb4 5557 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5558 max *= fact * fact;
5559# else
5560 /* Variant 2: Optimize a lot more for small table. Here we
5561 also add squares of the size but we also add penalties for
5562 empty slots (the +1 term). */
5563 for (j = 0; j < i; ++j)
5564 max += (1 + counts[j]) * (1 + counts[j]);
5565
5566 /* The overall size of the table is considered, but not as
5567 strong as in variant 1, where it is squared. */
fdc90cb4 5568 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5569 max *= fact;
5570# endif
5571
5572 /* Compare with current best results. */
5573 if (max < best_chlen)
5574 {
5575 best_chlen = max;
5576 best_size = i;
ca4be51c 5577 no_improvement_count = 0;
5a580b3a 5578 }
0883b6e0
NC
5579 /* PR 11843: Avoid futile long searches for the best bucket size
5580 when there are a large number of symbols. */
5581 else if (++no_improvement_count == 100)
5582 break;
5a580b3a
AM
5583 }
5584
5585 free (counts);
5586 }
5587 else
5588#endif /* defined (BFD_HOST_U_64_BIT) */
5589 {
5590 /* This is the fallback solution if no 64bit type is available or if we
5591 are not supposed to spend much time on optimizations. We select the
5592 bucket count using a fixed set of numbers. */
5593 for (i = 0; elf_buckets[i] != 0; i++)
5594 {
5595 best_size = elf_buckets[i];
fdc90cb4 5596 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5597 break;
5598 }
fdc90cb4
JJ
5599 if (gnu_hash && best_size < 2)
5600 best_size = 2;
5a580b3a
AM
5601 }
5602
5a580b3a
AM
5603 return best_size;
5604}
5605
d0bf826b
AM
5606/* Size any SHT_GROUP section for ld -r. */
5607
5608bfd_boolean
5609_bfd_elf_size_group_sections (struct bfd_link_info *info)
5610{
5611 bfd *ibfd;
5612
c72f2fb2 5613 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5614 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5615 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5616 return FALSE;
5617 return TRUE;
5618}
5619
04c3a755
NS
5620/* Set a default stack segment size. The value in INFO wins. If it
5621 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5622 undefined it is initialized. */
5623
5624bfd_boolean
5625bfd_elf_stack_segment_size (bfd *output_bfd,
5626 struct bfd_link_info *info,
5627 const char *legacy_symbol,
5628 bfd_vma default_size)
5629{
5630 struct elf_link_hash_entry *h = NULL;
5631
5632 /* Look for legacy symbol. */
5633 if (legacy_symbol)
5634 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5635 FALSE, FALSE, FALSE);
5636 if (h && (h->root.type == bfd_link_hash_defined
5637 || h->root.type == bfd_link_hash_defweak)
5638 && h->def_regular
5639 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5640 {
5641 /* The symbol has no type if specified on the command line. */
5642 h->type = STT_OBJECT;
5643 if (info->stacksize)
5644 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5645 output_bfd, legacy_symbol);
5646 else if (h->root.u.def.section != bfd_abs_section_ptr)
5647 (*_bfd_error_handler) (_("%B: %s not absolute"),
5648 output_bfd, legacy_symbol);
5649 else
5650 info->stacksize = h->root.u.def.value;
5651 }
5652
5653 if (!info->stacksize)
5654 /* If the user didn't set a size, or explicitly inhibit the
5655 size, set it now. */
5656 info->stacksize = default_size;
5657
5658 /* Provide the legacy symbol, if it is referenced. */
5659 if (h && (h->root.type == bfd_link_hash_undefined
5660 || h->root.type == bfd_link_hash_undefweak))
5661 {
5662 struct bfd_link_hash_entry *bh = NULL;
5663
5664 if (!(_bfd_generic_link_add_one_symbol
5665 (info, output_bfd, legacy_symbol,
5666 BSF_GLOBAL, bfd_abs_section_ptr,
5667 info->stacksize >= 0 ? info->stacksize : 0,
5668 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5669 return FALSE;
5670
5671 h = (struct elf_link_hash_entry *) bh;
5672 h->def_regular = 1;
5673 h->type = STT_OBJECT;
5674 }
5675
5676 return TRUE;
5677}
5678
5a580b3a
AM
5679/* Set up the sizes and contents of the ELF dynamic sections. This is
5680 called by the ELF linker emulation before_allocation routine. We
5681 must set the sizes of the sections before the linker sets the
5682 addresses of the various sections. */
5683
5684bfd_boolean
5685bfd_elf_size_dynamic_sections (bfd *output_bfd,
5686 const char *soname,
5687 const char *rpath,
5688 const char *filter_shlib,
7ee314fa
AM
5689 const char *audit,
5690 const char *depaudit,
5a580b3a
AM
5691 const char * const *auxiliary_filters,
5692 struct bfd_link_info *info,
fd91d419 5693 asection **sinterpptr)
5a580b3a
AM
5694{
5695 bfd_size_type soname_indx;
5696 bfd *dynobj;
5697 const struct elf_backend_data *bed;
28caa186 5698 struct elf_info_failed asvinfo;
5a580b3a
AM
5699
5700 *sinterpptr = NULL;
5701
5702 soname_indx = (bfd_size_type) -1;
5703
5704 if (!is_elf_hash_table (info->hash))
5705 return TRUE;
5706
6bfdb61b 5707 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5708
5709 /* Any syms created from now on start with -1 in
5710 got.refcount/offset and plt.refcount/offset. */
5711 elf_hash_table (info)->init_got_refcount
5712 = elf_hash_table (info)->init_got_offset;
5713 elf_hash_table (info)->init_plt_refcount
5714 = elf_hash_table (info)->init_plt_offset;
5715
0e1862bb 5716 if (bfd_link_relocatable (info)
04c3a755
NS
5717 && !_bfd_elf_size_group_sections (info))
5718 return FALSE;
5719
5720 /* The backend may have to create some sections regardless of whether
5721 we're dynamic or not. */
5722 if (bed->elf_backend_always_size_sections
5723 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5724 return FALSE;
5725
5726 /* Determine any GNU_STACK segment requirements, after the backend
5727 has had a chance to set a default segment size. */
5a580b3a 5728 if (info->execstack)
12bd6957 5729 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5730 else if (info->noexecstack)
12bd6957 5731 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5732 else
5733 {
5734 bfd *inputobj;
5735 asection *notesec = NULL;
5736 int exec = 0;
5737
5738 for (inputobj = info->input_bfds;
5739 inputobj;
c72f2fb2 5740 inputobj = inputobj->link.next)
5a580b3a
AM
5741 {
5742 asection *s;
5743
a92c088a
L
5744 if (inputobj->flags
5745 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5746 continue;
5747 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5748 if (s)
5749 {
5750 if (s->flags & SEC_CODE)
5751 exec = PF_X;
5752 notesec = s;
5753 }
6bfdb61b 5754 else if (bed->default_execstack)
5a580b3a
AM
5755 exec = PF_X;
5756 }
04c3a755 5757 if (notesec || info->stacksize > 0)
12bd6957 5758 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5759 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5760 && notesec->output_section != bfd_abs_section_ptr)
5761 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5762 }
5763
5a580b3a
AM
5764 dynobj = elf_hash_table (info)->dynobj;
5765
9a2a56cc 5766 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5767 {
5768 struct elf_info_failed eif;
5769 struct elf_link_hash_entry *h;
5770 asection *dynstr;
5771 struct bfd_elf_version_tree *t;
5772 struct bfd_elf_version_expr *d;
046183de 5773 asection *s;
5a580b3a
AM
5774 bfd_boolean all_defined;
5775
3d4d4302 5776 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5777 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5778
5779 if (soname != NULL)
5780 {
5781 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5782 soname, TRUE);
5783 if (soname_indx == (bfd_size_type) -1
5784 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5785 return FALSE;
5786 }
5787
5788 if (info->symbolic)
5789 {
5790 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5791 return FALSE;
5792 info->flags |= DF_SYMBOLIC;
5793 }
5794
5795 if (rpath != NULL)
5796 {
5797 bfd_size_type indx;
b1b00fcc 5798 bfd_vma tag;
5a580b3a
AM
5799
5800 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5801 TRUE);
b1b00fcc 5802 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5803 return FALSE;
5804
b1b00fcc
MF
5805 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5806 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5807 return FALSE;
5a580b3a
AM
5808 }
5809
5810 if (filter_shlib != NULL)
5811 {
5812 bfd_size_type indx;
5813
5814 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5815 filter_shlib, TRUE);
5816 if (indx == (bfd_size_type) -1
5817 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5818 return FALSE;
5819 }
5820
5821 if (auxiliary_filters != NULL)
5822 {
5823 const char * const *p;
5824
5825 for (p = auxiliary_filters; *p != NULL; p++)
5826 {
5827 bfd_size_type indx;
5828
5829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5830 *p, TRUE);
5831 if (indx == (bfd_size_type) -1
5832 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5833 return FALSE;
5834 }
5835 }
5836
7ee314fa
AM
5837 if (audit != NULL)
5838 {
5839 bfd_size_type indx;
5840
5841 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5842 TRUE);
5843 if (indx == (bfd_size_type) -1
5844 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5845 return FALSE;
5846 }
5847
5848 if (depaudit != NULL)
5849 {
5850 bfd_size_type indx;
5851
5852 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5853 TRUE);
5854 if (indx == (bfd_size_type) -1
5855 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5856 return FALSE;
5857 }
5858
5a580b3a 5859 eif.info = info;
5a580b3a
AM
5860 eif.failed = FALSE;
5861
5862 /* If we are supposed to export all symbols into the dynamic symbol
5863 table (this is not the normal case), then do so. */
55255dae 5864 if (info->export_dynamic
0e1862bb 5865 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5866 {
5867 elf_link_hash_traverse (elf_hash_table (info),
5868 _bfd_elf_export_symbol,
5869 &eif);
5870 if (eif.failed)
5871 return FALSE;
5872 }
5873
5874 /* Make all global versions with definition. */
fd91d419 5875 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5876 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5877 if (!d->symver && d->literal)
5a580b3a
AM
5878 {
5879 const char *verstr, *name;
5880 size_t namelen, verlen, newlen;
93252b1c 5881 char *newname, *p, leading_char;
5a580b3a
AM
5882 struct elf_link_hash_entry *newh;
5883
93252b1c 5884 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5885 name = d->pattern;
93252b1c 5886 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5887 verstr = t->name;
5888 verlen = strlen (verstr);
5889 newlen = namelen + verlen + 3;
5890
a50b1753 5891 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5892 if (newname == NULL)
5893 return FALSE;
93252b1c
MF
5894 newname[0] = leading_char;
5895 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5896
5897 /* Check the hidden versioned definition. */
5898 p = newname + namelen;
5899 *p++ = ELF_VER_CHR;
5900 memcpy (p, verstr, verlen + 1);
5901 newh = elf_link_hash_lookup (elf_hash_table (info),
5902 newname, FALSE, FALSE,
5903 FALSE);
5904 if (newh == NULL
5905 || (newh->root.type != bfd_link_hash_defined
5906 && newh->root.type != bfd_link_hash_defweak))
5907 {
5908 /* Check the default versioned definition. */
5909 *p++ = ELF_VER_CHR;
5910 memcpy (p, verstr, verlen + 1);
5911 newh = elf_link_hash_lookup (elf_hash_table (info),
5912 newname, FALSE, FALSE,
5913 FALSE);
5914 }
5915 free (newname);
5916
5917 /* Mark this version if there is a definition and it is
5918 not defined in a shared object. */
5919 if (newh != NULL
f5385ebf 5920 && !newh->def_dynamic
5a580b3a
AM
5921 && (newh->root.type == bfd_link_hash_defined
5922 || newh->root.type == bfd_link_hash_defweak))
5923 d->symver = 1;
5924 }
5925
5926 /* Attach all the symbols to their version information. */
5a580b3a 5927 asvinfo.info = info;
5a580b3a
AM
5928 asvinfo.failed = FALSE;
5929
5930 elf_link_hash_traverse (elf_hash_table (info),
5931 _bfd_elf_link_assign_sym_version,
5932 &asvinfo);
5933 if (asvinfo.failed)
5934 return FALSE;
5935
5936 if (!info->allow_undefined_version)
5937 {
5938 /* Check if all global versions have a definition. */
5939 all_defined = TRUE;
fd91d419 5940 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5941 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5942 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5943 {
5944 (*_bfd_error_handler)
5945 (_("%s: undefined version: %s"),
5946 d->pattern, t->name);
5947 all_defined = FALSE;
5948 }
5949
5950 if (!all_defined)
5951 {
5952 bfd_set_error (bfd_error_bad_value);
5953 return FALSE;
5954 }
5955 }
5956
5957 /* Find all symbols which were defined in a dynamic object and make
5958 the backend pick a reasonable value for them. */
5959 elf_link_hash_traverse (elf_hash_table (info),
5960 _bfd_elf_adjust_dynamic_symbol,
5961 &eif);
5962 if (eif.failed)
5963 return FALSE;
5964
5965 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5966 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5967 now so that we know the final size of the .dynamic section. */
5968
5969 /* If there are initialization and/or finalization functions to
5970 call then add the corresponding DT_INIT/DT_FINI entries. */
5971 h = (info->init_function
5972 ? elf_link_hash_lookup (elf_hash_table (info),
5973 info->init_function, FALSE,
5974 FALSE, FALSE)
5975 : NULL);
5976 if (h != NULL
f5385ebf
AM
5977 && (h->ref_regular
5978 || h->def_regular))
5a580b3a
AM
5979 {
5980 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5981 return FALSE;
5982 }
5983 h = (info->fini_function
5984 ? elf_link_hash_lookup (elf_hash_table (info),
5985 info->fini_function, FALSE,
5986 FALSE, FALSE)
5987 : NULL);
5988 if (h != NULL
f5385ebf
AM
5989 && (h->ref_regular
5990 || h->def_regular))
5a580b3a
AM
5991 {
5992 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5993 return FALSE;
5994 }
5995
046183de
AM
5996 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5997 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5998 {
5999 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6000 if (! bfd_link_executable (info))
5a580b3a
AM
6001 {
6002 bfd *sub;
6003 asection *o;
6004
6005 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6006 sub = sub->link.next)
3fcd97f1
JJ
6007 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6008 for (o = sub->sections; o != NULL; o = o->next)
6009 if (elf_section_data (o)->this_hdr.sh_type
6010 == SHT_PREINIT_ARRAY)
6011 {
6012 (*_bfd_error_handler)
6013 (_("%B: .preinit_array section is not allowed in DSO"),
6014 sub);
6015 break;
6016 }
5a580b3a
AM
6017
6018 bfd_set_error (bfd_error_nonrepresentable_section);
6019 return FALSE;
6020 }
6021
6022 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6023 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6024 return FALSE;
6025 }
046183de
AM
6026 s = bfd_get_section_by_name (output_bfd, ".init_array");
6027 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6028 {
6029 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6030 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6031 return FALSE;
6032 }
046183de
AM
6033 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6034 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6035 {
6036 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6037 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6038 return FALSE;
6039 }
6040
3d4d4302 6041 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6042 /* If .dynstr is excluded from the link, we don't want any of
6043 these tags. Strictly, we should be checking each section
6044 individually; This quick check covers for the case where
6045 someone does a /DISCARD/ : { *(*) }. */
6046 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6047 {
6048 bfd_size_type strsize;
6049
6050 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6051 if ((info->emit_hash
6052 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6053 || (info->emit_gnu_hash
6054 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6055 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6056 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6057 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6058 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6059 bed->s->sizeof_sym))
6060 return FALSE;
6061 }
6062 }
6063
de231f20
CM
6064 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6065 return FALSE;
6066
5a580b3a
AM
6067 /* The backend must work out the sizes of all the other dynamic
6068 sections. */
9a2a56cc
AM
6069 if (dynobj != NULL
6070 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6071 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6072 return FALSE;
6073
9a2a56cc 6074 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6075 {
554220db 6076 unsigned long section_sym_count;
fd91d419 6077 struct bfd_elf_version_tree *verdefs;
5a580b3a 6078 asection *s;
5a580b3a
AM
6079
6080 /* Set up the version definition section. */
3d4d4302 6081 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6082 BFD_ASSERT (s != NULL);
6083
6084 /* We may have created additional version definitions if we are
6085 just linking a regular application. */
fd91d419 6086 verdefs = info->version_info;
5a580b3a
AM
6087
6088 /* Skip anonymous version tag. */
6089 if (verdefs != NULL && verdefs->vernum == 0)
6090 verdefs = verdefs->next;
6091
3e3b46e5 6092 if (verdefs == NULL && !info->create_default_symver)
8423293d 6093 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6094 else
6095 {
6096 unsigned int cdefs;
6097 bfd_size_type size;
6098 struct bfd_elf_version_tree *t;
6099 bfd_byte *p;
6100 Elf_Internal_Verdef def;
6101 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6102 struct bfd_link_hash_entry *bh;
6103 struct elf_link_hash_entry *h;
6104 const char *name;
5a580b3a
AM
6105
6106 cdefs = 0;
6107 size = 0;
6108
6109 /* Make space for the base version. */
6110 size += sizeof (Elf_External_Verdef);
6111 size += sizeof (Elf_External_Verdaux);
6112 ++cdefs;
6113
3e3b46e5
PB
6114 /* Make space for the default version. */
6115 if (info->create_default_symver)
6116 {
6117 size += sizeof (Elf_External_Verdef);
6118 ++cdefs;
6119 }
6120
5a580b3a
AM
6121 for (t = verdefs; t != NULL; t = t->next)
6122 {
6123 struct bfd_elf_version_deps *n;
6124
a6cc6b3b
RO
6125 /* Don't emit base version twice. */
6126 if (t->vernum == 0)
6127 continue;
6128
5a580b3a
AM
6129 size += sizeof (Elf_External_Verdef);
6130 size += sizeof (Elf_External_Verdaux);
6131 ++cdefs;
6132
6133 for (n = t->deps; n != NULL; n = n->next)
6134 size += sizeof (Elf_External_Verdaux);
6135 }
6136
eea6121a 6137 s->size = size;
a50b1753 6138 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6139 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6140 return FALSE;
6141
6142 /* Fill in the version definition section. */
6143
6144 p = s->contents;
6145
6146 def.vd_version = VER_DEF_CURRENT;
6147 def.vd_flags = VER_FLG_BASE;
6148 def.vd_ndx = 1;
6149 def.vd_cnt = 1;
3e3b46e5
PB
6150 if (info->create_default_symver)
6151 {
6152 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6153 def.vd_next = sizeof (Elf_External_Verdef);
6154 }
6155 else
6156 {
6157 def.vd_aux = sizeof (Elf_External_Verdef);
6158 def.vd_next = (sizeof (Elf_External_Verdef)
6159 + sizeof (Elf_External_Verdaux));
6160 }
5a580b3a
AM
6161
6162 if (soname_indx != (bfd_size_type) -1)
6163 {
6164 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6165 soname_indx);
6166 def.vd_hash = bfd_elf_hash (soname);
6167 defaux.vda_name = soname_indx;
3e3b46e5 6168 name = soname;
5a580b3a
AM
6169 }
6170 else
6171 {
5a580b3a
AM
6172 bfd_size_type indx;
6173
06084812 6174 name = lbasename (output_bfd->filename);
5a580b3a
AM
6175 def.vd_hash = bfd_elf_hash (name);
6176 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6177 name, FALSE);
6178 if (indx == (bfd_size_type) -1)
6179 return FALSE;
6180 defaux.vda_name = indx;
6181 }
6182 defaux.vda_next = 0;
6183
6184 _bfd_elf_swap_verdef_out (output_bfd, &def,
6185 (Elf_External_Verdef *) p);
6186 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6187 if (info->create_default_symver)
6188 {
6189 /* Add a symbol representing this version. */
6190 bh = NULL;
6191 if (! (_bfd_generic_link_add_one_symbol
6192 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6193 0, NULL, FALSE,
6194 get_elf_backend_data (dynobj)->collect, &bh)))
6195 return FALSE;
6196 h = (struct elf_link_hash_entry *) bh;
6197 h->non_elf = 0;
6198 h->def_regular = 1;
6199 h->type = STT_OBJECT;
6200 h->verinfo.vertree = NULL;
6201
6202 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6203 return FALSE;
6204
6205 /* Create a duplicate of the base version with the same
6206 aux block, but different flags. */
6207 def.vd_flags = 0;
6208 def.vd_ndx = 2;
6209 def.vd_aux = sizeof (Elf_External_Verdef);
6210 if (verdefs)
6211 def.vd_next = (sizeof (Elf_External_Verdef)
6212 + sizeof (Elf_External_Verdaux));
6213 else
6214 def.vd_next = 0;
6215 _bfd_elf_swap_verdef_out (output_bfd, &def,
6216 (Elf_External_Verdef *) p);
6217 p += sizeof (Elf_External_Verdef);
6218 }
5a580b3a
AM
6219 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6220 (Elf_External_Verdaux *) p);
6221 p += sizeof (Elf_External_Verdaux);
6222
6223 for (t = verdefs; t != NULL; t = t->next)
6224 {
6225 unsigned int cdeps;
6226 struct bfd_elf_version_deps *n;
5a580b3a 6227
a6cc6b3b
RO
6228 /* Don't emit the base version twice. */
6229 if (t->vernum == 0)
6230 continue;
6231
5a580b3a
AM
6232 cdeps = 0;
6233 for (n = t->deps; n != NULL; n = n->next)
6234 ++cdeps;
6235
6236 /* Add a symbol representing this version. */
6237 bh = NULL;
6238 if (! (_bfd_generic_link_add_one_symbol
6239 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6240 0, NULL, FALSE,
6241 get_elf_backend_data (dynobj)->collect, &bh)))
6242 return FALSE;
6243 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6244 h->non_elf = 0;
6245 h->def_regular = 1;
5a580b3a
AM
6246 h->type = STT_OBJECT;
6247 h->verinfo.vertree = t;
6248
c152c796 6249 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6250 return FALSE;
6251
6252 def.vd_version = VER_DEF_CURRENT;
6253 def.vd_flags = 0;
6254 if (t->globals.list == NULL
6255 && t->locals.list == NULL
6256 && ! t->used)
6257 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6258 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6259 def.vd_cnt = cdeps + 1;
6260 def.vd_hash = bfd_elf_hash (t->name);
6261 def.vd_aux = sizeof (Elf_External_Verdef);
6262 def.vd_next = 0;
a6cc6b3b
RO
6263
6264 /* If a basever node is next, it *must* be the last node in
6265 the chain, otherwise Verdef construction breaks. */
6266 if (t->next != NULL && t->next->vernum == 0)
6267 BFD_ASSERT (t->next->next == NULL);
6268
6269 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6270 def.vd_next = (sizeof (Elf_External_Verdef)
6271 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6272
6273 _bfd_elf_swap_verdef_out (output_bfd, &def,
6274 (Elf_External_Verdef *) p);
6275 p += sizeof (Elf_External_Verdef);
6276
6277 defaux.vda_name = h->dynstr_index;
6278 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6279 h->dynstr_index);
6280 defaux.vda_next = 0;
6281 if (t->deps != NULL)
6282 defaux.vda_next = sizeof (Elf_External_Verdaux);
6283 t->name_indx = defaux.vda_name;
6284
6285 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6286 (Elf_External_Verdaux *) p);
6287 p += sizeof (Elf_External_Verdaux);
6288
6289 for (n = t->deps; n != NULL; n = n->next)
6290 {
6291 if (n->version_needed == NULL)
6292 {
6293 /* This can happen if there was an error in the
6294 version script. */
6295 defaux.vda_name = 0;
6296 }
6297 else
6298 {
6299 defaux.vda_name = n->version_needed->name_indx;
6300 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6301 defaux.vda_name);
6302 }
6303 if (n->next == NULL)
6304 defaux.vda_next = 0;
6305 else
6306 defaux.vda_next = sizeof (Elf_External_Verdaux);
6307
6308 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6309 (Elf_External_Verdaux *) p);
6310 p += sizeof (Elf_External_Verdaux);
6311 }
6312 }
6313
6314 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6315 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6316 return FALSE;
6317
6318 elf_tdata (output_bfd)->cverdefs = cdefs;
6319 }
6320
6321 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6322 {
6323 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6324 return FALSE;
6325 }
6326 else if (info->flags & DF_BIND_NOW)
6327 {
6328 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6329 return FALSE;
6330 }
6331
6332 if (info->flags_1)
6333 {
0e1862bb 6334 if (bfd_link_executable (info))
5a580b3a
AM
6335 info->flags_1 &= ~ (DF_1_INITFIRST
6336 | DF_1_NODELETE
6337 | DF_1_NOOPEN);
6338 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6339 return FALSE;
6340 }
6341
6342 /* Work out the size of the version reference section. */
6343
3d4d4302 6344 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6345 BFD_ASSERT (s != NULL);
6346 {
6347 struct elf_find_verdep_info sinfo;
6348
5a580b3a
AM
6349 sinfo.info = info;
6350 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6351 if (sinfo.vers == 0)
6352 sinfo.vers = 1;
6353 sinfo.failed = FALSE;
6354
6355 elf_link_hash_traverse (elf_hash_table (info),
6356 _bfd_elf_link_find_version_dependencies,
6357 &sinfo);
14b1c01e
AM
6358 if (sinfo.failed)
6359 return FALSE;
5a580b3a
AM
6360
6361 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6362 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6363 else
6364 {
6365 Elf_Internal_Verneed *t;
6366 unsigned int size;
6367 unsigned int crefs;
6368 bfd_byte *p;
6369
a6cc6b3b 6370 /* Build the version dependency section. */
5a580b3a
AM
6371 size = 0;
6372 crefs = 0;
6373 for (t = elf_tdata (output_bfd)->verref;
6374 t != NULL;
6375 t = t->vn_nextref)
6376 {
6377 Elf_Internal_Vernaux *a;
6378
6379 size += sizeof (Elf_External_Verneed);
6380 ++crefs;
6381 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6382 size += sizeof (Elf_External_Vernaux);
6383 }
6384
eea6121a 6385 s->size = size;
a50b1753 6386 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6387 if (s->contents == NULL)
6388 return FALSE;
6389
6390 p = s->contents;
6391 for (t = elf_tdata (output_bfd)->verref;
6392 t != NULL;
6393 t = t->vn_nextref)
6394 {
6395 unsigned int caux;
6396 Elf_Internal_Vernaux *a;
6397 bfd_size_type indx;
6398
6399 caux = 0;
6400 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6401 ++caux;
6402
6403 t->vn_version = VER_NEED_CURRENT;
6404 t->vn_cnt = caux;
6405 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6406 elf_dt_name (t->vn_bfd) != NULL
6407 ? elf_dt_name (t->vn_bfd)
06084812 6408 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6409 FALSE);
6410 if (indx == (bfd_size_type) -1)
6411 return FALSE;
6412 t->vn_file = indx;
6413 t->vn_aux = sizeof (Elf_External_Verneed);
6414 if (t->vn_nextref == NULL)
6415 t->vn_next = 0;
6416 else
6417 t->vn_next = (sizeof (Elf_External_Verneed)
6418 + caux * sizeof (Elf_External_Vernaux));
6419
6420 _bfd_elf_swap_verneed_out (output_bfd, t,
6421 (Elf_External_Verneed *) p);
6422 p += sizeof (Elf_External_Verneed);
6423
6424 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6425 {
6426 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6427 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6428 a->vna_nodename, FALSE);
6429 if (indx == (bfd_size_type) -1)
6430 return FALSE;
6431 a->vna_name = indx;
6432 if (a->vna_nextptr == NULL)
6433 a->vna_next = 0;
6434 else
6435 a->vna_next = sizeof (Elf_External_Vernaux);
6436
6437 _bfd_elf_swap_vernaux_out (output_bfd, a,
6438 (Elf_External_Vernaux *) p);
6439 p += sizeof (Elf_External_Vernaux);
6440 }
6441 }
6442
6443 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6444 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6445 return FALSE;
6446
6447 elf_tdata (output_bfd)->cverrefs = crefs;
6448 }
6449 }
6450
8423293d
AM
6451 if ((elf_tdata (output_bfd)->cverrefs == 0
6452 && elf_tdata (output_bfd)->cverdefs == 0)
6453 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6454 &section_sym_count) == 0)
6455 {
3d4d4302 6456 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6457 s->flags |= SEC_EXCLUDE;
6458 }
6459 }
6460 return TRUE;
6461}
6462
74541ad4
AM
6463/* Find the first non-excluded output section. We'll use its
6464 section symbol for some emitted relocs. */
6465void
6466_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6467{
6468 asection *s;
6469
6470 for (s = output_bfd->sections; s != NULL; s = s->next)
6471 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6472 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6473 {
6474 elf_hash_table (info)->text_index_section = s;
6475 break;
6476 }
6477}
6478
6479/* Find two non-excluded output sections, one for code, one for data.
6480 We'll use their section symbols for some emitted relocs. */
6481void
6482_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6483{
6484 asection *s;
6485
266b05cf
DJ
6486 /* Data first, since setting text_index_section changes
6487 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6488 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6489 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6490 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6491 {
266b05cf 6492 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6493 break;
6494 }
6495
6496 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6497 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6498 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6499 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6500 {
266b05cf 6501 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6502 break;
6503 }
6504
6505 if (elf_hash_table (info)->text_index_section == NULL)
6506 elf_hash_table (info)->text_index_section
6507 = elf_hash_table (info)->data_index_section;
6508}
6509
8423293d
AM
6510bfd_boolean
6511bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6512{
74541ad4
AM
6513 const struct elf_backend_data *bed;
6514
8423293d
AM
6515 if (!is_elf_hash_table (info->hash))
6516 return TRUE;
6517
74541ad4
AM
6518 bed = get_elf_backend_data (output_bfd);
6519 (*bed->elf_backend_init_index_section) (output_bfd, info);
6520
8423293d
AM
6521 if (elf_hash_table (info)->dynamic_sections_created)
6522 {
6523 bfd *dynobj;
8423293d
AM
6524 asection *s;
6525 bfd_size_type dynsymcount;
6526 unsigned long section_sym_count;
8423293d
AM
6527 unsigned int dtagcount;
6528
6529 dynobj = elf_hash_table (info)->dynobj;
6530
5a580b3a
AM
6531 /* Assign dynsym indicies. In a shared library we generate a
6532 section symbol for each output section, which come first.
6533 Next come all of the back-end allocated local dynamic syms,
6534 followed by the rest of the global symbols. */
6535
554220db
AM
6536 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6537 &section_sym_count);
5a580b3a
AM
6538
6539 /* Work out the size of the symbol version section. */
3d4d4302 6540 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6541 BFD_ASSERT (s != NULL);
8423293d
AM
6542 if (dynsymcount != 0
6543 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6544 {
eea6121a 6545 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6546 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6547 if (s->contents == NULL)
6548 return FALSE;
6549
6550 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6551 return FALSE;
6552 }
6553
6554 /* Set the size of the .dynsym and .hash sections. We counted
6555 the number of dynamic symbols in elf_link_add_object_symbols.
6556 We will build the contents of .dynsym and .hash when we build
6557 the final symbol table, because until then we do not know the
6558 correct value to give the symbols. We built the .dynstr
6559 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6560 s = elf_hash_table (info)->dynsym;
5a580b3a 6561 BFD_ASSERT (s != NULL);
eea6121a 6562 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6563
6564 if (dynsymcount != 0)
6565 {
a50b1753 6566 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6567 if (s->contents == NULL)
6568 return FALSE;
5a580b3a 6569
554220db
AM
6570 /* The first entry in .dynsym is a dummy symbol.
6571 Clear all the section syms, in case we don't output them all. */
6572 ++section_sym_count;
6573 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6574 }
6575
fdc90cb4
JJ
6576 elf_hash_table (info)->bucketcount = 0;
6577
5a580b3a
AM
6578 /* Compute the size of the hashing table. As a side effect this
6579 computes the hash values for all the names we export. */
fdc90cb4
JJ
6580 if (info->emit_hash)
6581 {
6582 unsigned long int *hashcodes;
14b1c01e 6583 struct hash_codes_info hashinf;
fdc90cb4
JJ
6584 bfd_size_type amt;
6585 unsigned long int nsyms;
6586 size_t bucketcount;
6587 size_t hash_entry_size;
6588
6589 /* Compute the hash values for all exported symbols. At the same
6590 time store the values in an array so that we could use them for
6591 optimizations. */
6592 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6593 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6594 if (hashcodes == NULL)
6595 return FALSE;
14b1c01e
AM
6596 hashinf.hashcodes = hashcodes;
6597 hashinf.error = FALSE;
5a580b3a 6598
fdc90cb4
JJ
6599 /* Put all hash values in HASHCODES. */
6600 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6601 elf_collect_hash_codes, &hashinf);
6602 if (hashinf.error)
4dd07732
AM
6603 {
6604 free (hashcodes);
6605 return FALSE;
6606 }
5a580b3a 6607
14b1c01e 6608 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6609 bucketcount
6610 = compute_bucket_count (info, hashcodes, nsyms, 0);
6611 free (hashcodes);
6612
6613 if (bucketcount == 0)
6614 return FALSE;
5a580b3a 6615
fdc90cb4
JJ
6616 elf_hash_table (info)->bucketcount = bucketcount;
6617
3d4d4302 6618 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6619 BFD_ASSERT (s != NULL);
6620 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6621 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6622 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6623 if (s->contents == NULL)
6624 return FALSE;
6625
6626 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6627 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6628 s->contents + hash_entry_size);
6629 }
6630
6631 if (info->emit_gnu_hash)
6632 {
6633 size_t i, cnt;
6634 unsigned char *contents;
6635 struct collect_gnu_hash_codes cinfo;
6636 bfd_size_type amt;
6637 size_t bucketcount;
6638
6639 memset (&cinfo, 0, sizeof (cinfo));
6640
6641 /* Compute the hash values for all exported symbols. At the same
6642 time store the values in an array so that we could use them for
6643 optimizations. */
6644 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6645 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6646 if (cinfo.hashcodes == NULL)
6647 return FALSE;
6648
6649 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6650 cinfo.min_dynindx = -1;
6651 cinfo.output_bfd = output_bfd;
6652 cinfo.bed = bed;
6653
6654 /* Put all hash values in HASHCODES. */
6655 elf_link_hash_traverse (elf_hash_table (info),
6656 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6657 if (cinfo.error)
4dd07732
AM
6658 {
6659 free (cinfo.hashcodes);
6660 return FALSE;
6661 }
fdc90cb4
JJ
6662
6663 bucketcount
6664 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6665
6666 if (bucketcount == 0)
6667 {
6668 free (cinfo.hashcodes);
6669 return FALSE;
6670 }
6671
3d4d4302 6672 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6673 BFD_ASSERT (s != NULL);
6674
6675 if (cinfo.nsyms == 0)
6676 {
6677 /* Empty .gnu.hash section is special. */
6678 BFD_ASSERT (cinfo.min_dynindx == -1);
6679 free (cinfo.hashcodes);
6680 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6681 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6682 if (contents == NULL)
6683 return FALSE;
6684 s->contents = contents;
6685 /* 1 empty bucket. */
6686 bfd_put_32 (output_bfd, 1, contents);
6687 /* SYMIDX above the special symbol 0. */
6688 bfd_put_32 (output_bfd, 1, contents + 4);
6689 /* Just one word for bitmask. */
6690 bfd_put_32 (output_bfd, 1, contents + 8);
6691 /* Only hash fn bloom filter. */
6692 bfd_put_32 (output_bfd, 0, contents + 12);
6693 /* No hashes are valid - empty bitmask. */
6694 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6695 /* No hashes in the only bucket. */
6696 bfd_put_32 (output_bfd, 0,
6697 contents + 16 + bed->s->arch_size / 8);
6698 }
6699 else
6700 {
9e6619e2 6701 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6702 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6703
9e6619e2
AM
6704 x = cinfo.nsyms;
6705 maskbitslog2 = 1;
6706 while ((x >>= 1) != 0)
6707 ++maskbitslog2;
fdc90cb4
JJ
6708 if (maskbitslog2 < 3)
6709 maskbitslog2 = 5;
6710 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6711 maskbitslog2 = maskbitslog2 + 3;
6712 else
6713 maskbitslog2 = maskbitslog2 + 2;
6714 if (bed->s->arch_size == 64)
6715 {
6716 if (maskbitslog2 == 5)
6717 maskbitslog2 = 6;
6718 cinfo.shift1 = 6;
6719 }
6720 else
6721 cinfo.shift1 = 5;
6722 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6723 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6724 cinfo.maskbits = 1 << maskbitslog2;
6725 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6726 amt = bucketcount * sizeof (unsigned long int) * 2;
6727 amt += maskwords * sizeof (bfd_vma);
a50b1753 6728 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6729 if (cinfo.bitmask == NULL)
6730 {
6731 free (cinfo.hashcodes);
6732 return FALSE;
6733 }
6734
a50b1753 6735 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6736 cinfo.indx = cinfo.counts + bucketcount;
6737 cinfo.symindx = dynsymcount - cinfo.nsyms;
6738 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6739
6740 /* Determine how often each hash bucket is used. */
6741 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6742 for (i = 0; i < cinfo.nsyms; ++i)
6743 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6744
6745 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6746 if (cinfo.counts[i] != 0)
6747 {
6748 cinfo.indx[i] = cnt;
6749 cnt += cinfo.counts[i];
6750 }
6751 BFD_ASSERT (cnt == dynsymcount);
6752 cinfo.bucketcount = bucketcount;
6753 cinfo.local_indx = cinfo.min_dynindx;
6754
6755 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6756 s->size += cinfo.maskbits / 8;
a50b1753 6757 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6758 if (contents == NULL)
6759 {
6760 free (cinfo.bitmask);
6761 free (cinfo.hashcodes);
6762 return FALSE;
6763 }
6764
6765 s->contents = contents;
6766 bfd_put_32 (output_bfd, bucketcount, contents);
6767 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6768 bfd_put_32 (output_bfd, maskwords, contents + 8);
6769 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6770 contents += 16 + cinfo.maskbits / 8;
6771
6772 for (i = 0; i < bucketcount; ++i)
6773 {
6774 if (cinfo.counts[i] == 0)
6775 bfd_put_32 (output_bfd, 0, contents);
6776 else
6777 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6778 contents += 4;
6779 }
6780
6781 cinfo.contents = contents;
6782
6783 /* Renumber dynamic symbols, populate .gnu.hash section. */
6784 elf_link_hash_traverse (elf_hash_table (info),
6785 elf_renumber_gnu_hash_syms, &cinfo);
6786
6787 contents = s->contents + 16;
6788 for (i = 0; i < maskwords; ++i)
6789 {
6790 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6791 contents);
6792 contents += bed->s->arch_size / 8;
6793 }
6794
6795 free (cinfo.bitmask);
6796 free (cinfo.hashcodes);
6797 }
6798 }
5a580b3a 6799
3d4d4302 6800 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6801 BFD_ASSERT (s != NULL);
6802
4ad4eba5 6803 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6804
eea6121a 6805 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6806
6807 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6808 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6809 return FALSE;
6810 }
6811
6812 return TRUE;
6813}
4d269e42 6814\f
4d269e42
AM
6815/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6816
6817static void
6818merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6819 asection *sec)
6820{
dbaa2011
AM
6821 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6822 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6823}
6824
6825/* Finish SHF_MERGE section merging. */
6826
6827bfd_boolean
630993ec 6828_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6829{
6830 bfd *ibfd;
6831 asection *sec;
6832
6833 if (!is_elf_hash_table (info->hash))
6834 return FALSE;
6835
c72f2fb2 6836 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6837 if ((ibfd->flags & DYNAMIC) == 0
6838 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6839 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6840 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6841 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6842 if ((sec->flags & SEC_MERGE) != 0
6843 && !bfd_is_abs_section (sec->output_section))
6844 {
6845 struct bfd_elf_section_data *secdata;
6846
6847 secdata = elf_section_data (sec);
630993ec 6848 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6849 &elf_hash_table (info)->merge_info,
6850 sec, &secdata->sec_info))
6851 return FALSE;
6852 else if (secdata->sec_info)
dbaa2011 6853 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6854 }
6855
6856 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6857 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6858 merge_sections_remove_hook);
6859 return TRUE;
6860}
6861
6862/* Create an entry in an ELF linker hash table. */
6863
6864struct bfd_hash_entry *
6865_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6866 struct bfd_hash_table *table,
6867 const char *string)
6868{
6869 /* Allocate the structure if it has not already been allocated by a
6870 subclass. */
6871 if (entry == NULL)
6872 {
a50b1753 6873 entry = (struct bfd_hash_entry *)
ca4be51c 6874 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6875 if (entry == NULL)
6876 return entry;
6877 }
6878
6879 /* Call the allocation method of the superclass. */
6880 entry = _bfd_link_hash_newfunc (entry, table, string);
6881 if (entry != NULL)
6882 {
6883 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6884 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6885
6886 /* Set local fields. */
6887 ret->indx = -1;
6888 ret->dynindx = -1;
6889 ret->got = htab->init_got_refcount;
6890 ret->plt = htab->init_plt_refcount;
6891 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6892 - offsetof (struct elf_link_hash_entry, size)));
6893 /* Assume that we have been called by a non-ELF symbol reader.
6894 This flag is then reset by the code which reads an ELF input
6895 file. This ensures that a symbol created by a non-ELF symbol
6896 reader will have the flag set correctly. */
6897 ret->non_elf = 1;
6898 }
6899
6900 return entry;
6901}
6902
6903/* Copy data from an indirect symbol to its direct symbol, hiding the
6904 old indirect symbol. Also used for copying flags to a weakdef. */
6905
6906void
6907_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6908 struct elf_link_hash_entry *dir,
6909 struct elf_link_hash_entry *ind)
6910{
6911 struct elf_link_hash_table *htab;
6912
6913 /* Copy down any references that we may have already seen to the
6e33951e
L
6914 symbol which just became indirect if DIR isn't a hidden versioned
6915 symbol. */
4d269e42 6916
422f1182 6917 if (dir->versioned != versioned_hidden)
6e33951e
L
6918 {
6919 dir->ref_dynamic |= ind->ref_dynamic;
6920 dir->ref_regular |= ind->ref_regular;
6921 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6922 dir->non_got_ref |= ind->non_got_ref;
6923 dir->needs_plt |= ind->needs_plt;
6924 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6925 }
4d269e42
AM
6926
6927 if (ind->root.type != bfd_link_hash_indirect)
6928 return;
6929
6930 /* Copy over the global and procedure linkage table refcount entries.
6931 These may have been already set up by a check_relocs routine. */
6932 htab = elf_hash_table (info);
6933 if (ind->got.refcount > htab->init_got_refcount.refcount)
6934 {
6935 if (dir->got.refcount < 0)
6936 dir->got.refcount = 0;
6937 dir->got.refcount += ind->got.refcount;
6938 ind->got.refcount = htab->init_got_refcount.refcount;
6939 }
6940
6941 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6942 {
6943 if (dir->plt.refcount < 0)
6944 dir->plt.refcount = 0;
6945 dir->plt.refcount += ind->plt.refcount;
6946 ind->plt.refcount = htab->init_plt_refcount.refcount;
6947 }
6948
6949 if (ind->dynindx != -1)
6950 {
6951 if (dir->dynindx != -1)
6952 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6953 dir->dynindx = ind->dynindx;
6954 dir->dynstr_index = ind->dynstr_index;
6955 ind->dynindx = -1;
6956 ind->dynstr_index = 0;
6957 }
6958}
6959
6960void
6961_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6962 struct elf_link_hash_entry *h,
6963 bfd_boolean force_local)
6964{
3aa14d16
L
6965 /* STT_GNU_IFUNC symbol must go through PLT. */
6966 if (h->type != STT_GNU_IFUNC)
6967 {
6968 h->plt = elf_hash_table (info)->init_plt_offset;
6969 h->needs_plt = 0;
6970 }
4d269e42
AM
6971 if (force_local)
6972 {
6973 h->forced_local = 1;
6974 if (h->dynindx != -1)
6975 {
6976 h->dynindx = -1;
6977 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6978 h->dynstr_index);
6979 }
6980 }
6981}
6982
7bf52ea2
AM
6983/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6984 caller. */
4d269e42
AM
6985
6986bfd_boolean
6987_bfd_elf_link_hash_table_init
6988 (struct elf_link_hash_table *table,
6989 bfd *abfd,
6990 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6991 struct bfd_hash_table *,
6992 const char *),
4dfe6ac6
NC
6993 unsigned int entsize,
6994 enum elf_target_id target_id)
4d269e42
AM
6995{
6996 bfd_boolean ret;
6997 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6998
4d269e42
AM
6999 table->init_got_refcount.refcount = can_refcount - 1;
7000 table->init_plt_refcount.refcount = can_refcount - 1;
7001 table->init_got_offset.offset = -(bfd_vma) 1;
7002 table->init_plt_offset.offset = -(bfd_vma) 1;
7003 /* The first dynamic symbol is a dummy. */
7004 table->dynsymcount = 1;
7005
7006 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7007
4d269e42 7008 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7009 table->hash_table_id = target_id;
4d269e42
AM
7010
7011 return ret;
7012}
7013
7014/* Create an ELF linker hash table. */
7015
7016struct bfd_link_hash_table *
7017_bfd_elf_link_hash_table_create (bfd *abfd)
7018{
7019 struct elf_link_hash_table *ret;
7020 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7021
7bf52ea2 7022 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7023 if (ret == NULL)
7024 return NULL;
7025
7026 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7027 sizeof (struct elf_link_hash_entry),
7028 GENERIC_ELF_DATA))
4d269e42
AM
7029 {
7030 free (ret);
7031 return NULL;
7032 }
d495ab0d 7033 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7034
7035 return &ret->root;
7036}
7037
9f7c3e5e
AM
7038/* Destroy an ELF linker hash table. */
7039
7040void
d495ab0d 7041_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7042{
d495ab0d
AM
7043 struct elf_link_hash_table *htab;
7044
7045 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7046 if (htab->dynstr != NULL)
7047 _bfd_elf_strtab_free (htab->dynstr);
7048 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7049 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7050}
7051
4d269e42
AM
7052/* This is a hook for the ELF emulation code in the generic linker to
7053 tell the backend linker what file name to use for the DT_NEEDED
7054 entry for a dynamic object. */
7055
7056void
7057bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7058{
7059 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7060 && bfd_get_format (abfd) == bfd_object)
7061 elf_dt_name (abfd) = name;
7062}
7063
7064int
7065bfd_elf_get_dyn_lib_class (bfd *abfd)
7066{
7067 int lib_class;
7068 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7069 && bfd_get_format (abfd) == bfd_object)
7070 lib_class = elf_dyn_lib_class (abfd);
7071 else
7072 lib_class = 0;
7073 return lib_class;
7074}
7075
7076void
7077bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7078{
7079 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7080 && bfd_get_format (abfd) == bfd_object)
7081 elf_dyn_lib_class (abfd) = lib_class;
7082}
7083
7084/* Get the list of DT_NEEDED entries for a link. This is a hook for
7085 the linker ELF emulation code. */
7086
7087struct bfd_link_needed_list *
7088bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7089 struct bfd_link_info *info)
7090{
7091 if (! is_elf_hash_table (info->hash))
7092 return NULL;
7093 return elf_hash_table (info)->needed;
7094}
7095
7096/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7097 hook for the linker ELF emulation code. */
7098
7099struct bfd_link_needed_list *
7100bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7101 struct bfd_link_info *info)
7102{
7103 if (! is_elf_hash_table (info->hash))
7104 return NULL;
7105 return elf_hash_table (info)->runpath;
7106}
7107
7108/* Get the name actually used for a dynamic object for a link. This
7109 is the SONAME entry if there is one. Otherwise, it is the string
7110 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7111
7112const char *
7113bfd_elf_get_dt_soname (bfd *abfd)
7114{
7115 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7116 && bfd_get_format (abfd) == bfd_object)
7117 return elf_dt_name (abfd);
7118 return NULL;
7119}
7120
7121/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7122 the ELF linker emulation code. */
7123
7124bfd_boolean
7125bfd_elf_get_bfd_needed_list (bfd *abfd,
7126 struct bfd_link_needed_list **pneeded)
7127{
7128 asection *s;
7129 bfd_byte *dynbuf = NULL;
cb33740c 7130 unsigned int elfsec;
4d269e42
AM
7131 unsigned long shlink;
7132 bfd_byte *extdyn, *extdynend;
7133 size_t extdynsize;
7134 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7135
7136 *pneeded = NULL;
7137
7138 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7139 || bfd_get_format (abfd) != bfd_object)
7140 return TRUE;
7141
7142 s = bfd_get_section_by_name (abfd, ".dynamic");
7143 if (s == NULL || s->size == 0)
7144 return TRUE;
7145
7146 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7147 goto error_return;
7148
7149 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7150 if (elfsec == SHN_BAD)
4d269e42
AM
7151 goto error_return;
7152
7153 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7154
4d269e42
AM
7155 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7156 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7157
7158 extdyn = dynbuf;
7159 extdynend = extdyn + s->size;
7160 for (; extdyn < extdynend; extdyn += extdynsize)
7161 {
7162 Elf_Internal_Dyn dyn;
7163
7164 (*swap_dyn_in) (abfd, extdyn, &dyn);
7165
7166 if (dyn.d_tag == DT_NULL)
7167 break;
7168
7169 if (dyn.d_tag == DT_NEEDED)
7170 {
7171 const char *string;
7172 struct bfd_link_needed_list *l;
7173 unsigned int tagv = dyn.d_un.d_val;
7174 bfd_size_type amt;
7175
7176 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7177 if (string == NULL)
7178 goto error_return;
7179
7180 amt = sizeof *l;
a50b1753 7181 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7182 if (l == NULL)
7183 goto error_return;
7184
7185 l->by = abfd;
7186 l->name = string;
7187 l->next = *pneeded;
7188 *pneeded = l;
7189 }
7190 }
7191
7192 free (dynbuf);
7193
7194 return TRUE;
7195
7196 error_return:
7197 if (dynbuf != NULL)
7198 free (dynbuf);
7199 return FALSE;
7200}
7201
7202struct elf_symbuf_symbol
7203{
7204 unsigned long st_name; /* Symbol name, index in string tbl */
7205 unsigned char st_info; /* Type and binding attributes */
7206 unsigned char st_other; /* Visibilty, and target specific */
7207};
7208
7209struct elf_symbuf_head
7210{
7211 struct elf_symbuf_symbol *ssym;
7212 bfd_size_type count;
7213 unsigned int st_shndx;
7214};
7215
7216struct elf_symbol
7217{
7218 union
7219 {
7220 Elf_Internal_Sym *isym;
7221 struct elf_symbuf_symbol *ssym;
7222 } u;
7223 const char *name;
7224};
7225
7226/* Sort references to symbols by ascending section number. */
7227
7228static int
7229elf_sort_elf_symbol (const void *arg1, const void *arg2)
7230{
7231 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7232 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7233
7234 return s1->st_shndx - s2->st_shndx;
7235}
7236
7237static int
7238elf_sym_name_compare (const void *arg1, const void *arg2)
7239{
7240 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7241 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7242 return strcmp (s1->name, s2->name);
7243}
7244
7245static struct elf_symbuf_head *
7246elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7247{
14b1c01e 7248 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7249 struct elf_symbuf_symbol *ssym;
7250 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7251 bfd_size_type i, shndx_count, total_size;
4d269e42 7252
a50b1753 7253 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7254 if (indbuf == NULL)
7255 return NULL;
7256
7257 for (ind = indbuf, i = 0; i < symcount; i++)
7258 if (isymbuf[i].st_shndx != SHN_UNDEF)
7259 *ind++ = &isymbuf[i];
7260 indbufend = ind;
7261
7262 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7263 elf_sort_elf_symbol);
7264
7265 shndx_count = 0;
7266 if (indbufend > indbuf)
7267 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7268 if (ind[0]->st_shndx != ind[1]->st_shndx)
7269 shndx_count++;
7270
3ae181ee
L
7271 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7272 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7273 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7274 if (ssymbuf == NULL)
7275 {
7276 free (indbuf);
7277 return NULL;
7278 }
7279
3ae181ee 7280 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7281 ssymbuf->ssym = NULL;
7282 ssymbuf->count = shndx_count;
7283 ssymbuf->st_shndx = 0;
7284 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7285 {
7286 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7287 {
7288 ssymhead++;
7289 ssymhead->ssym = ssym;
7290 ssymhead->count = 0;
7291 ssymhead->st_shndx = (*ind)->st_shndx;
7292 }
7293 ssym->st_name = (*ind)->st_name;
7294 ssym->st_info = (*ind)->st_info;
7295 ssym->st_other = (*ind)->st_other;
7296 ssymhead->count++;
7297 }
3ae181ee
L
7298 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7299 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7300 == total_size));
4d269e42
AM
7301
7302 free (indbuf);
7303 return ssymbuf;
7304}
7305
7306/* Check if 2 sections define the same set of local and global
7307 symbols. */
7308
8f317e31 7309static bfd_boolean
4d269e42
AM
7310bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7311 struct bfd_link_info *info)
7312{
7313 bfd *bfd1, *bfd2;
7314 const struct elf_backend_data *bed1, *bed2;
7315 Elf_Internal_Shdr *hdr1, *hdr2;
7316 bfd_size_type symcount1, symcount2;
7317 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7318 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7319 Elf_Internal_Sym *isym, *isymend;
7320 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7321 bfd_size_type count1, count2, i;
cb33740c 7322 unsigned int shndx1, shndx2;
4d269e42
AM
7323 bfd_boolean result;
7324
7325 bfd1 = sec1->owner;
7326 bfd2 = sec2->owner;
7327
4d269e42
AM
7328 /* Both sections have to be in ELF. */
7329 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7330 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7331 return FALSE;
7332
7333 if (elf_section_type (sec1) != elf_section_type (sec2))
7334 return FALSE;
7335
4d269e42
AM
7336 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7337 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7338 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7339 return FALSE;
7340
7341 bed1 = get_elf_backend_data (bfd1);
7342 bed2 = get_elf_backend_data (bfd2);
7343 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7344 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7345 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7346 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7347
7348 if (symcount1 == 0 || symcount2 == 0)
7349 return FALSE;
7350
7351 result = FALSE;
7352 isymbuf1 = NULL;
7353 isymbuf2 = NULL;
a50b1753
NC
7354 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7355 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7356
7357 if (ssymbuf1 == NULL)
7358 {
7359 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7360 NULL, NULL, NULL);
7361 if (isymbuf1 == NULL)
7362 goto done;
7363
7364 if (!info->reduce_memory_overheads)
7365 elf_tdata (bfd1)->symbuf = ssymbuf1
7366 = elf_create_symbuf (symcount1, isymbuf1);
7367 }
7368
7369 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7370 {
7371 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7372 NULL, NULL, NULL);
7373 if (isymbuf2 == NULL)
7374 goto done;
7375
7376 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7377 elf_tdata (bfd2)->symbuf = ssymbuf2
7378 = elf_create_symbuf (symcount2, isymbuf2);
7379 }
7380
7381 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7382 {
7383 /* Optimized faster version. */
7384 bfd_size_type lo, hi, mid;
7385 struct elf_symbol *symp;
7386 struct elf_symbuf_symbol *ssym, *ssymend;
7387
7388 lo = 0;
7389 hi = ssymbuf1->count;
7390 ssymbuf1++;
7391 count1 = 0;
7392 while (lo < hi)
7393 {
7394 mid = (lo + hi) / 2;
cb33740c 7395 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7396 hi = mid;
cb33740c 7397 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7398 lo = mid + 1;
7399 else
7400 {
7401 count1 = ssymbuf1[mid].count;
7402 ssymbuf1 += mid;
7403 break;
7404 }
7405 }
7406
7407 lo = 0;
7408 hi = ssymbuf2->count;
7409 ssymbuf2++;
7410 count2 = 0;
7411 while (lo < hi)
7412 {
7413 mid = (lo + hi) / 2;
cb33740c 7414 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7415 hi = mid;
cb33740c 7416 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7417 lo = mid + 1;
7418 else
7419 {
7420 count2 = ssymbuf2[mid].count;
7421 ssymbuf2 += mid;
7422 break;
7423 }
7424 }
7425
7426 if (count1 == 0 || count2 == 0 || count1 != count2)
7427 goto done;
7428
ca4be51c
AM
7429 symtable1
7430 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7431 symtable2
7432 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7433 if (symtable1 == NULL || symtable2 == NULL)
7434 goto done;
7435
7436 symp = symtable1;
7437 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7438 ssym < ssymend; ssym++, symp++)
7439 {
7440 symp->u.ssym = ssym;
7441 symp->name = bfd_elf_string_from_elf_section (bfd1,
7442 hdr1->sh_link,
7443 ssym->st_name);
7444 }
7445
7446 symp = symtable2;
7447 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7448 ssym < ssymend; ssym++, symp++)
7449 {
7450 symp->u.ssym = ssym;
7451 symp->name = bfd_elf_string_from_elf_section (bfd2,
7452 hdr2->sh_link,
7453 ssym->st_name);
7454 }
7455
7456 /* Sort symbol by name. */
7457 qsort (symtable1, count1, sizeof (struct elf_symbol),
7458 elf_sym_name_compare);
7459 qsort (symtable2, count1, sizeof (struct elf_symbol),
7460 elf_sym_name_compare);
7461
7462 for (i = 0; i < count1; i++)
7463 /* Two symbols must have the same binding, type and name. */
7464 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7465 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7466 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7467 goto done;
7468
7469 result = TRUE;
7470 goto done;
7471 }
7472
a50b1753
NC
7473 symtable1 = (struct elf_symbol *)
7474 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7475 symtable2 = (struct elf_symbol *)
7476 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7477 if (symtable1 == NULL || symtable2 == NULL)
7478 goto done;
7479
7480 /* Count definitions in the section. */
7481 count1 = 0;
7482 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7483 if (isym->st_shndx == shndx1)
4d269e42
AM
7484 symtable1[count1++].u.isym = isym;
7485
7486 count2 = 0;
7487 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7488 if (isym->st_shndx == shndx2)
4d269e42
AM
7489 symtable2[count2++].u.isym = isym;
7490
7491 if (count1 == 0 || count2 == 0 || count1 != count2)
7492 goto done;
7493
7494 for (i = 0; i < count1; i++)
7495 symtable1[i].name
7496 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7497 symtable1[i].u.isym->st_name);
7498
7499 for (i = 0; i < count2; i++)
7500 symtable2[i].name
7501 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7502 symtable2[i].u.isym->st_name);
7503
7504 /* Sort symbol by name. */
7505 qsort (symtable1, count1, sizeof (struct elf_symbol),
7506 elf_sym_name_compare);
7507 qsort (symtable2, count1, sizeof (struct elf_symbol),
7508 elf_sym_name_compare);
7509
7510 for (i = 0; i < count1; i++)
7511 /* Two symbols must have the same binding, type and name. */
7512 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7513 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7514 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7515 goto done;
7516
7517 result = TRUE;
7518
7519done:
7520 if (symtable1)
7521 free (symtable1);
7522 if (symtable2)
7523 free (symtable2);
7524 if (isymbuf1)
7525 free (isymbuf1);
7526 if (isymbuf2)
7527 free (isymbuf2);
7528
7529 return result;
7530}
7531
7532/* Return TRUE if 2 section types are compatible. */
7533
7534bfd_boolean
7535_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7536 bfd *bbfd, const asection *bsec)
7537{
7538 if (asec == NULL
7539 || bsec == NULL
7540 || abfd->xvec->flavour != bfd_target_elf_flavour
7541 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7542 return TRUE;
7543
7544 return elf_section_type (asec) == elf_section_type (bsec);
7545}
7546\f
c152c796
AM
7547/* Final phase of ELF linker. */
7548
7549/* A structure we use to avoid passing large numbers of arguments. */
7550
7551struct elf_final_link_info
7552{
7553 /* General link information. */
7554 struct bfd_link_info *info;
7555 /* Output BFD. */
7556 bfd *output_bfd;
7557 /* Symbol string table. */
ef10c3ac 7558 struct elf_strtab_hash *symstrtab;
c152c796
AM
7559 /* .hash section. */
7560 asection *hash_sec;
7561 /* symbol version section (.gnu.version). */
7562 asection *symver_sec;
7563 /* Buffer large enough to hold contents of any section. */
7564 bfd_byte *contents;
7565 /* Buffer large enough to hold external relocs of any section. */
7566 void *external_relocs;
7567 /* Buffer large enough to hold internal relocs of any section. */
7568 Elf_Internal_Rela *internal_relocs;
7569 /* Buffer large enough to hold external local symbols of any input
7570 BFD. */
7571 bfd_byte *external_syms;
7572 /* And a buffer for symbol section indices. */
7573 Elf_External_Sym_Shndx *locsym_shndx;
7574 /* Buffer large enough to hold internal local symbols of any input
7575 BFD. */
7576 Elf_Internal_Sym *internal_syms;
7577 /* Array large enough to hold a symbol index for each local symbol
7578 of any input BFD. */
7579 long *indices;
7580 /* Array large enough to hold a section pointer for each local
7581 symbol of any input BFD. */
7582 asection **sections;
ef10c3ac 7583 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7584 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7585 /* Number of STT_FILE syms seen. */
7586 size_t filesym_count;
c152c796
AM
7587};
7588
7589/* This struct is used to pass information to elf_link_output_extsym. */
7590
7591struct elf_outext_info
7592{
7593 bfd_boolean failed;
7594 bfd_boolean localsyms;
34a79995 7595 bfd_boolean file_sym_done;
8b127cbc 7596 struct elf_final_link_info *flinfo;
c152c796
AM
7597};
7598
d9352518
DB
7599
7600/* Support for evaluating a complex relocation.
7601
7602 Complex relocations are generalized, self-describing relocations. The
7603 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7604 relocations themselves.
d9352518
DB
7605
7606 The relocations are use a reserved elf-wide relocation type code (R_RELC
7607 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7608 information (start bit, end bit, word width, etc) into the addend. This
7609 information is extracted from CGEN-generated operand tables within gas.
7610
7611 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7612 internal) representing prefix-notation expressions, including but not
7613 limited to those sorts of expressions normally encoded as addends in the
7614 addend field. The symbol mangling format is:
7615
7616 <node> := <literal>
7617 | <unary-operator> ':' <node>
7618 | <binary-operator> ':' <node> ':' <node>
7619 ;
7620
7621 <literal> := 's' <digits=N> ':' <N character symbol name>
7622 | 'S' <digits=N> ':' <N character section name>
7623 | '#' <hexdigits>
7624 ;
7625
7626 <binary-operator> := as in C
7627 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7628
7629static void
a0c8462f
AM
7630set_symbol_value (bfd *bfd_with_globals,
7631 Elf_Internal_Sym *isymbuf,
7632 size_t locsymcount,
7633 size_t symidx,
7634 bfd_vma val)
d9352518 7635{
8977835c
AM
7636 struct elf_link_hash_entry **sym_hashes;
7637 struct elf_link_hash_entry *h;
7638 size_t extsymoff = locsymcount;
d9352518 7639
8977835c 7640 if (symidx < locsymcount)
d9352518 7641 {
8977835c
AM
7642 Elf_Internal_Sym *sym;
7643
7644 sym = isymbuf + symidx;
7645 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7646 {
7647 /* It is a local symbol: move it to the
7648 "absolute" section and give it a value. */
7649 sym->st_shndx = SHN_ABS;
7650 sym->st_value = val;
7651 return;
7652 }
7653 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7654 extsymoff = 0;
d9352518 7655 }
8977835c
AM
7656
7657 /* It is a global symbol: set its link type
7658 to "defined" and give it a value. */
7659
7660 sym_hashes = elf_sym_hashes (bfd_with_globals);
7661 h = sym_hashes [symidx - extsymoff];
7662 while (h->root.type == bfd_link_hash_indirect
7663 || h->root.type == bfd_link_hash_warning)
7664 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7665 h->root.type = bfd_link_hash_defined;
7666 h->root.u.def.value = val;
7667 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7668}
7669
a0c8462f
AM
7670static bfd_boolean
7671resolve_symbol (const char *name,
7672 bfd *input_bfd,
8b127cbc 7673 struct elf_final_link_info *flinfo,
a0c8462f
AM
7674 bfd_vma *result,
7675 Elf_Internal_Sym *isymbuf,
7676 size_t locsymcount)
d9352518 7677{
a0c8462f
AM
7678 Elf_Internal_Sym *sym;
7679 struct bfd_link_hash_entry *global_entry;
7680 const char *candidate = NULL;
7681 Elf_Internal_Shdr *symtab_hdr;
7682 size_t i;
7683
d9352518
DB
7684 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7685
7686 for (i = 0; i < locsymcount; ++ i)
7687 {
8977835c 7688 sym = isymbuf + i;
d9352518
DB
7689
7690 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7691 continue;
7692
7693 candidate = bfd_elf_string_from_elf_section (input_bfd,
7694 symtab_hdr->sh_link,
7695 sym->st_name);
7696#ifdef DEBUG
0f02bbd9
AM
7697 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7698 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7699#endif
7700 if (candidate && strcmp (candidate, name) == 0)
7701 {
8b127cbc 7702 asection *sec = flinfo->sections [i];
d9352518 7703
0f02bbd9
AM
7704 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7705 *result += sec->output_offset + sec->output_section->vma;
d9352518 7706#ifdef DEBUG
0f02bbd9
AM
7707 printf ("Found symbol with value %8.8lx\n",
7708 (unsigned long) *result);
d9352518
DB
7709#endif
7710 return TRUE;
7711 }
7712 }
7713
7714 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7715 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7716 FALSE, FALSE, TRUE);
d9352518
DB
7717 if (!global_entry)
7718 return FALSE;
a0c8462f 7719
d9352518
DB
7720 if (global_entry->type == bfd_link_hash_defined
7721 || global_entry->type == bfd_link_hash_defweak)
7722 {
a0c8462f
AM
7723 *result = (global_entry->u.def.value
7724 + global_entry->u.def.section->output_section->vma
7725 + global_entry->u.def.section->output_offset);
d9352518 7726#ifdef DEBUG
0f02bbd9
AM
7727 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7728 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7729#endif
7730 return TRUE;
a0c8462f 7731 }
d9352518 7732
d9352518
DB
7733 return FALSE;
7734}
7735
7736static bfd_boolean
a0c8462f
AM
7737resolve_section (const char *name,
7738 asection *sections,
7739 bfd_vma *result)
d9352518 7740{
a0c8462f
AM
7741 asection *curr;
7742 unsigned int len;
d9352518 7743
a0c8462f 7744 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7745 if (strcmp (curr->name, name) == 0)
7746 {
7747 *result = curr->vma;
7748 return TRUE;
7749 }
7750
7751 /* Hmm. still haven't found it. try pseudo-section names. */
a0c8462f 7752 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7753 {
7754 len = strlen (curr->name);
a0c8462f 7755 if (len > strlen (name))
d9352518
DB
7756 continue;
7757
7758 if (strncmp (curr->name, name, len) == 0)
7759 {
7760 if (strncmp (".end", name + len, 4) == 0)
7761 {
7762 *result = curr->vma + curr->size;
7763 return TRUE;
7764 }
7765
7766 /* Insert more pseudo-section names here, if you like. */
7767 }
7768 }
a0c8462f 7769
d9352518
DB
7770 return FALSE;
7771}
7772
7773static void
a0c8462f 7774undefined_reference (const char *reftype, const char *name)
d9352518 7775{
a0c8462f
AM
7776 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7777 reftype, name);
d9352518
DB
7778}
7779
7780static bfd_boolean
a0c8462f
AM
7781eval_symbol (bfd_vma *result,
7782 const char **symp,
7783 bfd *input_bfd,
8b127cbc 7784 struct elf_final_link_info *flinfo,
a0c8462f
AM
7785 bfd_vma dot,
7786 Elf_Internal_Sym *isymbuf,
7787 size_t locsymcount,
7788 int signed_p)
d9352518 7789{
4b93929b
NC
7790 size_t len;
7791 size_t symlen;
a0c8462f
AM
7792 bfd_vma a;
7793 bfd_vma b;
4b93929b 7794 char symbuf[4096];
0f02bbd9 7795 const char *sym = *symp;
a0c8462f
AM
7796 const char *symend;
7797 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7798
7799 len = strlen (sym);
7800 symend = sym + len;
7801
4b93929b 7802 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7803 {
7804 bfd_set_error (bfd_error_invalid_operation);
7805 return FALSE;
7806 }
a0c8462f 7807
d9352518
DB
7808 switch (* sym)
7809 {
7810 case '.':
0f02bbd9
AM
7811 *result = dot;
7812 *symp = sym + 1;
d9352518
DB
7813 return TRUE;
7814
7815 case '#':
0f02bbd9
AM
7816 ++sym;
7817 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7818 return TRUE;
7819
7820 case 'S':
7821 symbol_is_section = TRUE;
a0c8462f 7822 case 's':
0f02bbd9
AM
7823 ++sym;
7824 symlen = strtol (sym, (char **) symp, 10);
7825 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7826
4b93929b 7827 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7828 {
7829 bfd_set_error (bfd_error_invalid_operation);
7830 return FALSE;
7831 }
7832
7833 memcpy (symbuf, sym, symlen);
a0c8462f 7834 symbuf[symlen] = '\0';
0f02bbd9 7835 *symp = sym + symlen;
a0c8462f
AM
7836
7837 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7838 the symbol as a section, or vice-versa. so we're pretty liberal in our
7839 interpretation here; section means "try section first", not "must be a
7840 section", and likewise with symbol. */
7841
a0c8462f 7842 if (symbol_is_section)
d9352518 7843 {
8b127cbc
AM
7844 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7845 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7846 isymbuf, locsymcount))
d9352518
DB
7847 {
7848 undefined_reference ("section", symbuf);
7849 return FALSE;
7850 }
a0c8462f
AM
7851 }
7852 else
d9352518 7853 {
8b127cbc 7854 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7855 isymbuf, locsymcount)
8b127cbc 7856 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8977835c 7857 result))
d9352518
DB
7858 {
7859 undefined_reference ("symbol", symbuf);
7860 return FALSE;
7861 }
7862 }
7863
7864 return TRUE;
a0c8462f 7865
d9352518
DB
7866 /* All that remains are operators. */
7867
7868#define UNARY_OP(op) \
7869 if (strncmp (sym, #op, strlen (#op)) == 0) \
7870 { \
7871 sym += strlen (#op); \
a0c8462f
AM
7872 if (*sym == ':') \
7873 ++sym; \
0f02bbd9 7874 *symp = sym; \
8b127cbc 7875 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7876 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7877 return FALSE; \
7878 if (signed_p) \
0f02bbd9 7879 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7880 else \
7881 *result = op a; \
d9352518
DB
7882 return TRUE; \
7883 }
7884
7885#define BINARY_OP(op) \
7886 if (strncmp (sym, #op, strlen (#op)) == 0) \
7887 { \
7888 sym += strlen (#op); \
a0c8462f
AM
7889 if (*sym == ':') \
7890 ++sym; \
0f02bbd9 7891 *symp = sym; \
8b127cbc 7892 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7893 isymbuf, locsymcount, signed_p)) \
a0c8462f 7894 return FALSE; \
0f02bbd9 7895 ++*symp; \
8b127cbc 7896 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7897 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7898 return FALSE; \
7899 if (signed_p) \
0f02bbd9 7900 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7901 else \
7902 *result = a op b; \
d9352518
DB
7903 return TRUE; \
7904 }
7905
7906 default:
7907 UNARY_OP (0-);
7908 BINARY_OP (<<);
7909 BINARY_OP (>>);
7910 BINARY_OP (==);
7911 BINARY_OP (!=);
7912 BINARY_OP (<=);
7913 BINARY_OP (>=);
7914 BINARY_OP (&&);
7915 BINARY_OP (||);
7916 UNARY_OP (~);
7917 UNARY_OP (!);
7918 BINARY_OP (*);
7919 BINARY_OP (/);
7920 BINARY_OP (%);
7921 BINARY_OP (^);
7922 BINARY_OP (|);
7923 BINARY_OP (&);
7924 BINARY_OP (+);
7925 BINARY_OP (-);
7926 BINARY_OP (<);
7927 BINARY_OP (>);
7928#undef UNARY_OP
7929#undef BINARY_OP
7930 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7931 bfd_set_error (bfd_error_invalid_operation);
7932 return FALSE;
7933 }
7934}
7935
d9352518 7936static void
a0c8462f
AM
7937put_value (bfd_vma size,
7938 unsigned long chunksz,
7939 bfd *input_bfd,
7940 bfd_vma x,
7941 bfd_byte *location)
d9352518
DB
7942{
7943 location += (size - chunksz);
7944
41cd1ad1 7945 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
7946 {
7947 switch (chunksz)
7948 {
d9352518
DB
7949 case 1:
7950 bfd_put_8 (input_bfd, x, location);
41cd1ad1 7951 x >>= 8;
d9352518
DB
7952 break;
7953 case 2:
7954 bfd_put_16 (input_bfd, x, location);
41cd1ad1 7955 x >>= 16;
d9352518
DB
7956 break;
7957 case 4:
7958 bfd_put_32 (input_bfd, x, location);
65164438
NC
7959 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7960 x >>= 16;
7961 x >>= 16;
d9352518 7962 break;
d9352518 7963#ifdef BFD64
41cd1ad1 7964 case 8:
d9352518 7965 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
7966 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7967 x >>= 32;
7968 x >>= 32;
7969 break;
d9352518 7970#endif
41cd1ad1
NC
7971 default:
7972 abort ();
d9352518
DB
7973 break;
7974 }
7975 }
7976}
7977
a0c8462f
AM
7978static bfd_vma
7979get_value (bfd_vma size,
7980 unsigned long chunksz,
7981 bfd *input_bfd,
7982 bfd_byte *location)
d9352518 7983{
9b239e0e 7984 int shift;
d9352518
DB
7985 bfd_vma x = 0;
7986
9b239e0e
NC
7987 /* Sanity checks. */
7988 BFD_ASSERT (chunksz <= sizeof (x)
7989 && size >= chunksz
7990 && chunksz != 0
7991 && (size % chunksz) == 0
7992 && input_bfd != NULL
7993 && location != NULL);
7994
7995 if (chunksz == sizeof (x))
7996 {
7997 BFD_ASSERT (size == chunksz);
7998
7999 /* Make sure that we do not perform an undefined shift operation.
8000 We know that size == chunksz so there will only be one iteration
8001 of the loop below. */
8002 shift = 0;
8003 }
8004 else
8005 shift = 8 * chunksz;
8006
a0c8462f 8007 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8008 {
8009 switch (chunksz)
8010 {
d9352518 8011 case 1:
9b239e0e 8012 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8013 break;
8014 case 2:
9b239e0e 8015 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8016 break;
8017 case 4:
9b239e0e 8018 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8019 break;
d9352518 8020#ifdef BFD64
9b239e0e
NC
8021 case 8:
8022 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8023 break;
9b239e0e
NC
8024#endif
8025 default:
8026 abort ();
d9352518
DB
8027 }
8028 }
8029 return x;
8030}
8031
a0c8462f
AM
8032static void
8033decode_complex_addend (unsigned long *start, /* in bits */
8034 unsigned long *oplen, /* in bits */
8035 unsigned long *len, /* in bits */
8036 unsigned long *wordsz, /* in bytes */
8037 unsigned long *chunksz, /* in bytes */
8038 unsigned long *lsb0_p,
8039 unsigned long *signed_p,
8040 unsigned long *trunc_p,
8041 unsigned long encoded)
d9352518
DB
8042{
8043 * start = encoded & 0x3F;
8044 * len = (encoded >> 6) & 0x3F;
8045 * oplen = (encoded >> 12) & 0x3F;
8046 * wordsz = (encoded >> 18) & 0xF;
8047 * chunksz = (encoded >> 22) & 0xF;
8048 * lsb0_p = (encoded >> 27) & 1;
8049 * signed_p = (encoded >> 28) & 1;
8050 * trunc_p = (encoded >> 29) & 1;
8051}
8052
cdfeee4f 8053bfd_reloc_status_type
0f02bbd9 8054bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8055 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8056 bfd_byte *contents,
8057 Elf_Internal_Rela *rel,
8058 bfd_vma relocation)
d9352518 8059{
0f02bbd9
AM
8060 bfd_vma shift, x, mask;
8061 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8062 bfd_reloc_status_type r;
d9352518
DB
8063
8064 /* Perform this reloc, since it is complex.
8065 (this is not to say that it necessarily refers to a complex
8066 symbol; merely that it is a self-describing CGEN based reloc.
8067 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8068 word size, etc) encoded within it.). */
d9352518 8069
a0c8462f
AM
8070 decode_complex_addend (&start, &oplen, &len, &wordsz,
8071 &chunksz, &lsb0_p, &signed_p,
8072 &trunc_p, rel->r_addend);
d9352518
DB
8073
8074 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8075
8076 if (lsb0_p)
8077 shift = (start + 1) - len;
8078 else
8079 shift = (8 * wordsz) - (start + len);
8080
5dabe785 8081 /* FIXME: octets_per_byte. */
a0c8462f 8082 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
d9352518
DB
8083
8084#ifdef DEBUG
8085 printf ("Doing complex reloc: "
8086 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8087 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8088 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8089 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8090 oplen, (unsigned long) x, (unsigned long) mask,
8091 (unsigned long) relocation);
d9352518
DB
8092#endif
8093
cdfeee4f 8094 r = bfd_reloc_ok;
d9352518 8095 if (! trunc_p)
cdfeee4f
AM
8096 /* Now do an overflow check. */
8097 r = bfd_check_overflow ((signed_p
8098 ? complain_overflow_signed
8099 : complain_overflow_unsigned),
8100 len, 0, (8 * wordsz),
8101 relocation);
a0c8462f 8102
d9352518
DB
8103 /* Do the deed. */
8104 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8105
8106#ifdef DEBUG
8107 printf (" relocation: %8.8lx\n"
8108 " shifted mask: %8.8lx\n"
8109 " shifted/masked reloc: %8.8lx\n"
8110 " result: %8.8lx\n",
9ccb8af9
AM
8111 (unsigned long) relocation, (unsigned long) (mask << shift),
8112 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8113#endif
5dabe785 8114 /* FIXME: octets_per_byte. */
d9352518 8115 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
cdfeee4f 8116 return r;
d9352518
DB
8117}
8118
0e287786
AM
8119/* Functions to read r_offset from external (target order) reloc
8120 entry. Faster than bfd_getl32 et al, because we let the compiler
8121 know the value is aligned. */
53df40a4 8122
0e287786
AM
8123static bfd_vma
8124ext32l_r_offset (const void *p)
53df40a4
AM
8125{
8126 union aligned32
8127 {
8128 uint32_t v;
8129 unsigned char c[4];
8130 };
8131 const union aligned32 *a
0e287786 8132 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8133
8134 uint32_t aval = ( (uint32_t) a->c[0]
8135 | (uint32_t) a->c[1] << 8
8136 | (uint32_t) a->c[2] << 16
8137 | (uint32_t) a->c[3] << 24);
0e287786 8138 return aval;
53df40a4
AM
8139}
8140
0e287786
AM
8141static bfd_vma
8142ext32b_r_offset (const void *p)
53df40a4
AM
8143{
8144 union aligned32
8145 {
8146 uint32_t v;
8147 unsigned char c[4];
8148 };
8149 const union aligned32 *a
0e287786 8150 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8151
8152 uint32_t aval = ( (uint32_t) a->c[0] << 24
8153 | (uint32_t) a->c[1] << 16
8154 | (uint32_t) a->c[2] << 8
8155 | (uint32_t) a->c[3]);
0e287786 8156 return aval;
53df40a4
AM
8157}
8158
8159#ifdef BFD_HOST_64_BIT
0e287786
AM
8160static bfd_vma
8161ext64l_r_offset (const void *p)
53df40a4
AM
8162{
8163 union aligned64
8164 {
8165 uint64_t v;
8166 unsigned char c[8];
8167 };
8168 const union aligned64 *a
0e287786 8169 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8170
8171 uint64_t aval = ( (uint64_t) a->c[0]
8172 | (uint64_t) a->c[1] << 8
8173 | (uint64_t) a->c[2] << 16
8174 | (uint64_t) a->c[3] << 24
8175 | (uint64_t) a->c[4] << 32
8176 | (uint64_t) a->c[5] << 40
8177 | (uint64_t) a->c[6] << 48
8178 | (uint64_t) a->c[7] << 56);
0e287786 8179 return aval;
53df40a4
AM
8180}
8181
0e287786
AM
8182static bfd_vma
8183ext64b_r_offset (const void *p)
53df40a4
AM
8184{
8185 union aligned64
8186 {
8187 uint64_t v;
8188 unsigned char c[8];
8189 };
8190 const union aligned64 *a
0e287786 8191 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8192
8193 uint64_t aval = ( (uint64_t) a->c[0] << 56
8194 | (uint64_t) a->c[1] << 48
8195 | (uint64_t) a->c[2] << 40
8196 | (uint64_t) a->c[3] << 32
8197 | (uint64_t) a->c[4] << 24
8198 | (uint64_t) a->c[5] << 16
8199 | (uint64_t) a->c[6] << 8
8200 | (uint64_t) a->c[7]);
0e287786 8201 return aval;
53df40a4
AM
8202}
8203#endif
8204
c152c796
AM
8205/* When performing a relocatable link, the input relocations are
8206 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8207 referenced must be updated. Update all the relocations found in
8208 RELDATA. */
c152c796 8209
bca6d0e3 8210static bfd_boolean
c152c796 8211elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8212 struct bfd_elf_section_reloc_data *reldata,
8213 bfd_boolean sort)
c152c796
AM
8214{
8215 unsigned int i;
8216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8217 bfd_byte *erela;
8218 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8219 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8220 bfd_vma r_type_mask;
8221 int r_sym_shift;
d4730f92
BS
8222 unsigned int count = reldata->count;
8223 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8224
d4730f92 8225 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8226 {
8227 swap_in = bed->s->swap_reloc_in;
8228 swap_out = bed->s->swap_reloc_out;
8229 }
d4730f92 8230 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8231 {
8232 swap_in = bed->s->swap_reloca_in;
8233 swap_out = bed->s->swap_reloca_out;
8234 }
8235 else
8236 abort ();
8237
8238 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8239 abort ();
8240
8241 if (bed->s->arch_size == 32)
8242 {
8243 r_type_mask = 0xff;
8244 r_sym_shift = 8;
8245 }
8246 else
8247 {
8248 r_type_mask = 0xffffffff;
8249 r_sym_shift = 32;
8250 }
8251
d4730f92
BS
8252 erela = reldata->hdr->contents;
8253 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8254 {
8255 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8256 unsigned int j;
8257
8258 if (*rel_hash == NULL)
8259 continue;
8260
8261 BFD_ASSERT ((*rel_hash)->indx >= 0);
8262
8263 (*swap_in) (abfd, erela, irela);
8264 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8265 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8266 | (irela[j].r_info & r_type_mask));
8267 (*swap_out) (abfd, irela, erela);
8268 }
53df40a4 8269
0e287786 8270 if (sort && count != 0)
53df40a4 8271 {
0e287786
AM
8272 bfd_vma (*ext_r_off) (const void *);
8273 bfd_vma r_off;
8274 size_t elt_size;
8275 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8276 bfd_byte *buf = NULL;
28dbcedc
AM
8277
8278 if (bed->s->arch_size == 32)
8279 {
8280 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8281 ext_r_off = ext32l_r_offset;
28dbcedc 8282 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8283 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8284 else
8285 abort ();
8286 }
53df40a4 8287 else
28dbcedc 8288 {
53df40a4 8289#ifdef BFD_HOST_64_BIT
28dbcedc 8290 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8291 ext_r_off = ext64l_r_offset;
28dbcedc 8292 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8293 ext_r_off = ext64b_r_offset;
28dbcedc 8294 else
53df40a4 8295#endif
28dbcedc
AM
8296 abort ();
8297 }
0e287786 8298
bca6d0e3
AM
8299 /* Must use a stable sort here. A modified insertion sort,
8300 since the relocs are mostly sorted already. */
0e287786
AM
8301 elt_size = reldata->hdr->sh_entsize;
8302 base = reldata->hdr->contents;
8303 end = base + count * elt_size;
bca6d0e3 8304 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8305 abort ();
8306
8307 /* Ensure the first element is lowest. This acts as a sentinel,
8308 speeding the main loop below. */
8309 r_off = (*ext_r_off) (base);
8310 for (p = loc = base; (p += elt_size) < end; )
8311 {
8312 bfd_vma r_off2 = (*ext_r_off) (p);
8313 if (r_off > r_off2)
8314 {
8315 r_off = r_off2;
8316 loc = p;
8317 }
8318 }
8319 if (loc != base)
8320 {
8321 /* Don't just swap *base and *loc as that changes the order
8322 of the original base[0] and base[1] if they happen to
8323 have the same r_offset. */
bca6d0e3
AM
8324 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8325 memcpy (onebuf, loc, elt_size);
0e287786 8326 memmove (base + elt_size, base, loc - base);
bca6d0e3 8327 memcpy (base, onebuf, elt_size);
0e287786
AM
8328 }
8329
b29b8669 8330 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8331 {
8332 /* base to p is sorted, *p is next to insert. */
8333 r_off = (*ext_r_off) (p);
8334 /* Search the sorted region for location to insert. */
8335 loc = p - elt_size;
8336 while (r_off < (*ext_r_off) (loc))
8337 loc -= elt_size;
8338 loc += elt_size;
8339 if (loc != p)
8340 {
bca6d0e3
AM
8341 /* Chances are there is a run of relocs to insert here,
8342 from one of more input files. Files are not always
8343 linked in order due to the way elf_link_input_bfd is
8344 called. See pr17666. */
8345 size_t sortlen = p - loc;
8346 bfd_vma r_off2 = (*ext_r_off) (loc);
8347 size_t runlen = elt_size;
8348 size_t buf_size = 96 * 1024;
8349 while (p + runlen < end
8350 && (sortlen <= buf_size
8351 || runlen + elt_size <= buf_size)
8352 && r_off2 > (*ext_r_off) (p + runlen))
8353 runlen += elt_size;
8354 if (buf == NULL)
8355 {
8356 buf = bfd_malloc (buf_size);
8357 if (buf == NULL)
8358 return FALSE;
8359 }
8360 if (runlen < sortlen)
8361 {
8362 memcpy (buf, p, runlen);
8363 memmove (loc + runlen, loc, sortlen);
8364 memcpy (loc, buf, runlen);
8365 }
8366 else
8367 {
8368 memcpy (buf, loc, sortlen);
8369 memmove (loc, p, runlen);
8370 memcpy (loc + runlen, buf, sortlen);
8371 }
b29b8669 8372 p += runlen - elt_size;
0e287786
AM
8373 }
8374 }
8375 /* Hashes are no longer valid. */
28dbcedc
AM
8376 free (reldata->hashes);
8377 reldata->hashes = NULL;
bca6d0e3 8378 free (buf);
53df40a4 8379 }
bca6d0e3 8380 return TRUE;
c152c796
AM
8381}
8382
8383struct elf_link_sort_rela
8384{
8385 union {
8386 bfd_vma offset;
8387 bfd_vma sym_mask;
8388 } u;
8389 enum elf_reloc_type_class type;
8390 /* We use this as an array of size int_rels_per_ext_rel. */
8391 Elf_Internal_Rela rela[1];
8392};
8393
8394static int
8395elf_link_sort_cmp1 (const void *A, const void *B)
8396{
a50b1753
NC
8397 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8398 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8399 int relativea, relativeb;
8400
8401 relativea = a->type == reloc_class_relative;
8402 relativeb = b->type == reloc_class_relative;
8403
8404 if (relativea < relativeb)
8405 return 1;
8406 if (relativea > relativeb)
8407 return -1;
8408 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8409 return -1;
8410 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8411 return 1;
8412 if (a->rela->r_offset < b->rela->r_offset)
8413 return -1;
8414 if (a->rela->r_offset > b->rela->r_offset)
8415 return 1;
8416 return 0;
8417}
8418
8419static int
8420elf_link_sort_cmp2 (const void *A, const void *B)
8421{
a50b1753
NC
8422 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8423 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8424
7e612e98 8425 if (a->type < b->type)
c152c796 8426 return -1;
7e612e98 8427 if (a->type > b->type)
c152c796 8428 return 1;
7e612e98 8429 if (a->u.offset < b->u.offset)
c152c796 8430 return -1;
7e612e98 8431 if (a->u.offset > b->u.offset)
c152c796
AM
8432 return 1;
8433 if (a->rela->r_offset < b->rela->r_offset)
8434 return -1;
8435 if (a->rela->r_offset > b->rela->r_offset)
8436 return 1;
8437 return 0;
8438}
8439
8440static size_t
8441elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8442{
3410fea8 8443 asection *dynamic_relocs;
fc66a176
L
8444 asection *rela_dyn;
8445 asection *rel_dyn;
c152c796
AM
8446 bfd_size_type count, size;
8447 size_t i, ret, sort_elt, ext_size;
8448 bfd_byte *sort, *s_non_relative, *p;
8449 struct elf_link_sort_rela *sq;
8450 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8451 int i2e = bed->s->int_rels_per_ext_rel;
8452 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8453 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8454 struct bfd_link_order *lo;
8455 bfd_vma r_sym_mask;
3410fea8 8456 bfd_boolean use_rela;
c152c796 8457
3410fea8
NC
8458 /* Find a dynamic reloc section. */
8459 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8460 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8461 if (rela_dyn != NULL && rela_dyn->size > 0
8462 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8463 {
3410fea8
NC
8464 bfd_boolean use_rela_initialised = FALSE;
8465
8466 /* This is just here to stop gcc from complaining.
8467 It's initialization checking code is not perfect. */
8468 use_rela = TRUE;
8469
8470 /* Both sections are present. Examine the sizes
8471 of the indirect sections to help us choose. */
8472 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8473 if (lo->type == bfd_indirect_link_order)
8474 {
8475 asection *o = lo->u.indirect.section;
8476
8477 if ((o->size % bed->s->sizeof_rela) == 0)
8478 {
8479 if ((o->size % bed->s->sizeof_rel) == 0)
8480 /* Section size is divisible by both rel and rela sizes.
8481 It is of no help to us. */
8482 ;
8483 else
8484 {
8485 /* Section size is only divisible by rela. */
8486 if (use_rela_initialised && (use_rela == FALSE))
8487 {
8488 _bfd_error_handler
8489 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8490 bfd_set_error (bfd_error_invalid_operation);
8491 return 0;
8492 }
8493 else
8494 {
8495 use_rela = TRUE;
8496 use_rela_initialised = TRUE;
8497 }
8498 }
8499 }
8500 else if ((o->size % bed->s->sizeof_rel) == 0)
8501 {
8502 /* Section size is only divisible by rel. */
8503 if (use_rela_initialised && (use_rela == TRUE))
8504 {
8505 _bfd_error_handler
8506 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8507 bfd_set_error (bfd_error_invalid_operation);
8508 return 0;
8509 }
8510 else
8511 {
8512 use_rela = FALSE;
8513 use_rela_initialised = TRUE;
8514 }
8515 }
8516 else
8517 {
8518 /* The section size is not divisible by either - something is wrong. */
8519 _bfd_error_handler
8520 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8521 bfd_set_error (bfd_error_invalid_operation);
8522 return 0;
8523 }
8524 }
8525
8526 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8527 if (lo->type == bfd_indirect_link_order)
8528 {
8529 asection *o = lo->u.indirect.section;
8530
8531 if ((o->size % bed->s->sizeof_rela) == 0)
8532 {
8533 if ((o->size % bed->s->sizeof_rel) == 0)
8534 /* Section size is divisible by both rel and rela sizes.
8535 It is of no help to us. */
8536 ;
8537 else
8538 {
8539 /* Section size is only divisible by rela. */
8540 if (use_rela_initialised && (use_rela == FALSE))
8541 {
8542 _bfd_error_handler
8543 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8544 bfd_set_error (bfd_error_invalid_operation);
8545 return 0;
8546 }
8547 else
8548 {
8549 use_rela = TRUE;
8550 use_rela_initialised = TRUE;
8551 }
8552 }
8553 }
8554 else if ((o->size % bed->s->sizeof_rel) == 0)
8555 {
8556 /* Section size is only divisible by rel. */
8557 if (use_rela_initialised && (use_rela == TRUE))
8558 {
8559 _bfd_error_handler
8560 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8561 bfd_set_error (bfd_error_invalid_operation);
8562 return 0;
8563 }
8564 else
8565 {
8566 use_rela = FALSE;
8567 use_rela_initialised = TRUE;
8568 }
8569 }
8570 else
8571 {
8572 /* The section size is not divisible by either - something is wrong. */
8573 _bfd_error_handler
8574 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8575 bfd_set_error (bfd_error_invalid_operation);
8576 return 0;
8577 }
8578 }
8579
8580 if (! use_rela_initialised)
8581 /* Make a guess. */
8582 use_rela = TRUE;
c152c796 8583 }
fc66a176
L
8584 else if (rela_dyn != NULL && rela_dyn->size > 0)
8585 use_rela = TRUE;
8586 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8587 use_rela = FALSE;
c152c796 8588 else
fc66a176 8589 return 0;
3410fea8
NC
8590
8591 if (use_rela)
c152c796 8592 {
3410fea8 8593 dynamic_relocs = rela_dyn;
c152c796
AM
8594 ext_size = bed->s->sizeof_rela;
8595 swap_in = bed->s->swap_reloca_in;
8596 swap_out = bed->s->swap_reloca_out;
8597 }
3410fea8
NC
8598 else
8599 {
8600 dynamic_relocs = rel_dyn;
8601 ext_size = bed->s->sizeof_rel;
8602 swap_in = bed->s->swap_reloc_in;
8603 swap_out = bed->s->swap_reloc_out;
8604 }
c152c796
AM
8605
8606 size = 0;
3410fea8 8607 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8608 if (lo->type == bfd_indirect_link_order)
3410fea8 8609 size += lo->u.indirect.section->size;
c152c796 8610
3410fea8 8611 if (size != dynamic_relocs->size)
c152c796
AM
8612 return 0;
8613
8614 sort_elt = (sizeof (struct elf_link_sort_rela)
8615 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8616
8617 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8618 if (count == 0)
8619 return 0;
a50b1753 8620 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8621
c152c796
AM
8622 if (sort == NULL)
8623 {
8624 (*info->callbacks->warning)
8625 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8626 return 0;
8627 }
8628
8629 if (bed->s->arch_size == 32)
8630 r_sym_mask = ~(bfd_vma) 0xff;
8631 else
8632 r_sym_mask = ~(bfd_vma) 0xffffffff;
8633
3410fea8 8634 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8635 if (lo->type == bfd_indirect_link_order)
8636 {
8637 bfd_byte *erel, *erelend;
8638 asection *o = lo->u.indirect.section;
8639
1da212d6
AM
8640 if (o->contents == NULL && o->size != 0)
8641 {
8642 /* This is a reloc section that is being handled as a normal
8643 section. See bfd_section_from_shdr. We can't combine
8644 relocs in this case. */
8645 free (sort);
8646 return 0;
8647 }
c152c796 8648 erel = o->contents;
eea6121a 8649 erelend = o->contents + o->size;
5dabe785 8650 /* FIXME: octets_per_byte. */
c152c796 8651 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8652
c152c796
AM
8653 while (erel < erelend)
8654 {
8655 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8656
c152c796 8657 (*swap_in) (abfd, erel, s->rela);
7e612e98 8658 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8659 s->u.sym_mask = r_sym_mask;
8660 p += sort_elt;
8661 erel += ext_size;
8662 }
8663 }
8664
8665 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8666
8667 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8668 {
8669 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8670 if (s->type != reloc_class_relative)
8671 break;
8672 }
8673 ret = i;
8674 s_non_relative = p;
8675
8676 sq = (struct elf_link_sort_rela *) s_non_relative;
8677 for (; i < count; i++, p += sort_elt)
8678 {
8679 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8680 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8681 sq = sp;
8682 sp->u.offset = sq->rela->r_offset;
8683 }
8684
8685 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8686
3410fea8 8687 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8688 if (lo->type == bfd_indirect_link_order)
8689 {
8690 bfd_byte *erel, *erelend;
8691 asection *o = lo->u.indirect.section;
8692
8693 erel = o->contents;
eea6121a 8694 erelend = o->contents + o->size;
5dabe785 8695 /* FIXME: octets_per_byte. */
c152c796
AM
8696 p = sort + o->output_offset / ext_size * sort_elt;
8697 while (erel < erelend)
8698 {
8699 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8700 (*swap_out) (abfd, s->rela, erel);
8701 p += sort_elt;
8702 erel += ext_size;
8703 }
8704 }
8705
8706 free (sort);
3410fea8 8707 *psec = dynamic_relocs;
c152c796
AM
8708 return ret;
8709}
8710
ef10c3ac 8711/* Add a symbol to the output symbol string table. */
c152c796 8712
6e0b88f1 8713static int
ef10c3ac
L
8714elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8715 const char *name,
8716 Elf_Internal_Sym *elfsym,
8717 asection *input_sec,
8718 struct elf_link_hash_entry *h)
c152c796 8719{
6e0b88f1 8720 int (*output_symbol_hook)
c152c796
AM
8721 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8722 struct elf_link_hash_entry *);
ef10c3ac 8723 struct elf_link_hash_table *hash_table;
c152c796 8724 const struct elf_backend_data *bed;
ef10c3ac 8725 bfd_size_type strtabsize;
c152c796 8726
8539e4e8
AM
8727 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8728
8b127cbc 8729 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8730 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8731 if (output_symbol_hook != NULL)
8732 {
8b127cbc 8733 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8734 if (ret != 1)
8735 return ret;
c152c796
AM
8736 }
8737
ef10c3ac
L
8738 if (name == NULL
8739 || *name == '\0'
8740 || (input_sec->flags & SEC_EXCLUDE))
8741 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8742 else
8743 {
ef10c3ac
L
8744 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8745 to get the final offset for st_name. */
8746 elfsym->st_name
8747 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8748 name, FALSE);
c152c796 8749 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8750 return 0;
c152c796
AM
8751 }
8752
ef10c3ac
L
8753 hash_table = elf_hash_table (flinfo->info);
8754 strtabsize = hash_table->strtabsize;
8755 if (strtabsize <= hash_table->strtabcount)
c152c796 8756 {
ef10c3ac
L
8757 strtabsize += strtabsize;
8758 hash_table->strtabsize = strtabsize;
8759 strtabsize *= sizeof (*hash_table->strtab);
8760 hash_table->strtab
8761 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8762 strtabsize);
8763 if (hash_table->strtab == NULL)
6e0b88f1 8764 return 0;
c152c796 8765 }
ef10c3ac
L
8766 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8767 hash_table->strtab[hash_table->strtabcount].dest_index
8768 = hash_table->strtabcount;
8769 hash_table->strtab[hash_table->strtabcount].destshndx_index
8770 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8771
8772 bfd_get_symcount (flinfo->output_bfd) += 1;
8773 hash_table->strtabcount += 1;
8774
8775 return 1;
8776}
8777
8778/* Swap symbols out to the symbol table and flush the output symbols to
8779 the file. */
8780
8781static bfd_boolean
8782elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8783{
8784 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8785 bfd_size_type amt, i;
8786 const struct elf_backend_data *bed;
8787 bfd_byte *symbuf;
8788 Elf_Internal_Shdr *hdr;
8789 file_ptr pos;
8790 bfd_boolean ret;
8791
8792 if (!hash_table->strtabcount)
8793 return TRUE;
8794
8795 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8796
8797 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8798
ef10c3ac
L
8799 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8800 symbuf = (bfd_byte *) bfd_malloc (amt);
8801 if (symbuf == NULL)
8802 return FALSE;
1b786873 8803
ef10c3ac 8804 if (flinfo->symshndxbuf)
c152c796 8805 {
ef10c3ac
L
8806 amt = (sizeof (Elf_External_Sym_Shndx)
8807 * (bfd_get_symcount (flinfo->output_bfd)));
8808 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8809 if (flinfo->symshndxbuf == NULL)
c152c796 8810 {
ef10c3ac
L
8811 free (symbuf);
8812 return FALSE;
c152c796 8813 }
c152c796
AM
8814 }
8815
ef10c3ac
L
8816 for (i = 0; i < hash_table->strtabcount; i++)
8817 {
8818 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8819 if (elfsym->sym.st_name == (unsigned long) -1)
8820 elfsym->sym.st_name = 0;
8821 else
8822 elfsym->sym.st_name
8823 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8824 elfsym->sym.st_name);
8825 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8826 ((bfd_byte *) symbuf
8827 + (elfsym->dest_index
8828 * bed->s->sizeof_sym)),
8829 (flinfo->symshndxbuf
8830 + elfsym->destshndx_index));
8831 }
8832
8833 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8834 pos = hdr->sh_offset + hdr->sh_size;
8835 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8836 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8837 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8838 {
8839 hdr->sh_size += amt;
8840 ret = TRUE;
8841 }
8842 else
8843 ret = FALSE;
c152c796 8844
ef10c3ac
L
8845 free (symbuf);
8846
8847 free (hash_table->strtab);
8848 hash_table->strtab = NULL;
8849
8850 return ret;
c152c796
AM
8851}
8852
c0d5a53d
L
8853/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8854
8855static bfd_boolean
8856check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8857{
4fbb74a6
AM
8858 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8859 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8860 {
8861 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8862 beyond 64k. */
c0d5a53d
L
8863 (*_bfd_error_handler)
8864 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8865 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8866 bfd_set_error (bfd_error_nonrepresentable_section);
8867 return FALSE;
8868 }
8869 return TRUE;
8870}
8871
c152c796
AM
8872/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8873 allowing an unsatisfied unversioned symbol in the DSO to match a
8874 versioned symbol that would normally require an explicit version.
8875 We also handle the case that a DSO references a hidden symbol
8876 which may be satisfied by a versioned symbol in another DSO. */
8877
8878static bfd_boolean
8879elf_link_check_versioned_symbol (struct bfd_link_info *info,
8880 const struct elf_backend_data *bed,
8881 struct elf_link_hash_entry *h)
8882{
8883 bfd *abfd;
8884 struct elf_link_loaded_list *loaded;
8885
8886 if (!is_elf_hash_table (info->hash))
8887 return FALSE;
8888
90c984fc
L
8889 /* Check indirect symbol. */
8890 while (h->root.type == bfd_link_hash_indirect)
8891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8892
c152c796
AM
8893 switch (h->root.type)
8894 {
8895 default:
8896 abfd = NULL;
8897 break;
8898
8899 case bfd_link_hash_undefined:
8900 case bfd_link_hash_undefweak:
8901 abfd = h->root.u.undef.abfd;
8902 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8903 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8904 return FALSE;
8905 break;
8906
8907 case bfd_link_hash_defined:
8908 case bfd_link_hash_defweak:
8909 abfd = h->root.u.def.section->owner;
8910 break;
8911
8912 case bfd_link_hash_common:
8913 abfd = h->root.u.c.p->section->owner;
8914 break;
8915 }
8916 BFD_ASSERT (abfd != NULL);
8917
8918 for (loaded = elf_hash_table (info)->loaded;
8919 loaded != NULL;
8920 loaded = loaded->next)
8921 {
8922 bfd *input;
8923 Elf_Internal_Shdr *hdr;
8924 bfd_size_type symcount;
8925 bfd_size_type extsymcount;
8926 bfd_size_type extsymoff;
8927 Elf_Internal_Shdr *versymhdr;
8928 Elf_Internal_Sym *isym;
8929 Elf_Internal_Sym *isymend;
8930 Elf_Internal_Sym *isymbuf;
8931 Elf_External_Versym *ever;
8932 Elf_External_Versym *extversym;
8933
8934 input = loaded->abfd;
8935
8936 /* We check each DSO for a possible hidden versioned definition. */
8937 if (input == abfd
8938 || (input->flags & DYNAMIC) == 0
8939 || elf_dynversym (input) == 0)
8940 continue;
8941
8942 hdr = &elf_tdata (input)->dynsymtab_hdr;
8943
8944 symcount = hdr->sh_size / bed->s->sizeof_sym;
8945 if (elf_bad_symtab (input))
8946 {
8947 extsymcount = symcount;
8948 extsymoff = 0;
8949 }
8950 else
8951 {
8952 extsymcount = symcount - hdr->sh_info;
8953 extsymoff = hdr->sh_info;
8954 }
8955
8956 if (extsymcount == 0)
8957 continue;
8958
8959 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8960 NULL, NULL, NULL);
8961 if (isymbuf == NULL)
8962 return FALSE;
8963
8964 /* Read in any version definitions. */
8965 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 8966 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
8967 if (extversym == NULL)
8968 goto error_ret;
8969
8970 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8971 || (bfd_bread (extversym, versymhdr->sh_size, input)
8972 != versymhdr->sh_size))
8973 {
8974 free (extversym);
8975 error_ret:
8976 free (isymbuf);
8977 return FALSE;
8978 }
8979
8980 ever = extversym + extsymoff;
8981 isymend = isymbuf + extsymcount;
8982 for (isym = isymbuf; isym < isymend; isym++, ever++)
8983 {
8984 const char *name;
8985 Elf_Internal_Versym iver;
8986 unsigned short version_index;
8987
8988 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8989 || isym->st_shndx == SHN_UNDEF)
8990 continue;
8991
8992 name = bfd_elf_string_from_elf_section (input,
8993 hdr->sh_link,
8994 isym->st_name);
8995 if (strcmp (name, h->root.root.string) != 0)
8996 continue;
8997
8998 _bfd_elf_swap_versym_in (input, ever, &iver);
8999
d023c380
L
9000 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9001 && !(h->def_regular
9002 && h->forced_local))
c152c796
AM
9003 {
9004 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9005 have provided a definition for the undefined sym unless
9006 it is defined in a non-shared object and forced local.
9007 */
c152c796
AM
9008 abort ();
9009 }
9010
9011 version_index = iver.vs_vers & VERSYM_VERSION;
9012 if (version_index == 1 || version_index == 2)
9013 {
9014 /* This is the base or first version. We can use it. */
9015 free (extversym);
9016 free (isymbuf);
9017 return TRUE;
9018 }
9019 }
9020
9021 free (extversym);
9022 free (isymbuf);
9023 }
9024
9025 return FALSE;
9026}
9027
9028/* Add an external symbol to the symbol table. This is called from
9029 the hash table traversal routine. When generating a shared object,
9030 we go through the symbol table twice. The first time we output
9031 anything that might have been forced to local scope in a version
9032 script. The second time we output the symbols that are still
9033 global symbols. */
9034
9035static bfd_boolean
7686d77d 9036elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9037{
7686d77d 9038 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9039 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9040 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9041 bfd_boolean strip;
9042 Elf_Internal_Sym sym;
9043 asection *input_sec;
9044 const struct elf_backend_data *bed;
6e0b88f1
AM
9045 long indx;
9046 int ret;
6e33951e
L
9047 /* A symbol is bound locally if it is forced local or it is locally
9048 defined, hidden versioned, not referenced by shared library and
9049 not exported when linking executable. */
9050 bfd_boolean local_bind = (h->forced_local
0e1862bb 9051 || (bfd_link_executable (flinfo->info)
6e33951e
L
9052 && !flinfo->info->export_dynamic
9053 && !h->dynamic
9054 && !h->ref_dynamic
9055 && h->def_regular
422f1182 9056 && h->versioned == versioned_hidden));
c152c796
AM
9057
9058 if (h->root.type == bfd_link_hash_warning)
9059 {
9060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9061 if (h->root.type == bfd_link_hash_new)
9062 return TRUE;
9063 }
9064
9065 /* Decide whether to output this symbol in this pass. */
9066 if (eoinfo->localsyms)
9067 {
6e33951e 9068 if (!local_bind)
c152c796
AM
9069 return TRUE;
9070 }
9071 else
9072 {
6e33951e 9073 if (local_bind)
c152c796
AM
9074 return TRUE;
9075 }
9076
8b127cbc 9077 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9078
12ac1cf5 9079 if (h->root.type == bfd_link_hash_undefined)
c152c796 9080 {
12ac1cf5
NC
9081 /* If we have an undefined symbol reference here then it must have
9082 come from a shared library that is being linked in. (Undefined
98da7939
L
9083 references in regular files have already been handled unless
9084 they are in unreferenced sections which are removed by garbage
9085 collection). */
12ac1cf5
NC
9086 bfd_boolean ignore_undef = FALSE;
9087
9088 /* Some symbols may be special in that the fact that they're
9089 undefined can be safely ignored - let backend determine that. */
9090 if (bed->elf_backend_ignore_undef_symbol)
9091 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9092
9093 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9094 if (!ignore_undef
12ac1cf5 9095 && h->ref_dynamic
8b127cbc
AM
9096 && (!h->ref_regular || flinfo->info->gc_sections)
9097 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9098 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9099 {
9100 if (!(flinfo->info->callbacks->undefined_symbol
9101 (flinfo->info, h->root.root.string,
9102 h->ref_regular ? NULL : h->root.u.undef.abfd,
9103 NULL, 0,
9104 (flinfo->info->unresolved_syms_in_shared_libs
9105 == RM_GENERATE_ERROR))))
12ac1cf5 9106 {
17d078c5 9107 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9108 eoinfo->failed = TRUE;
9109 return FALSE;
9110 }
c152c796
AM
9111 }
9112 }
9113
9114 /* We should also warn if a forced local symbol is referenced from
9115 shared libraries. */
0e1862bb 9116 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9117 && h->forced_local
9118 && h->ref_dynamic
371a5866 9119 && h->def_regular
f5385ebf 9120 && !h->dynamic_def
ee659f1f 9121 && h->ref_dynamic_nonweak
8b127cbc 9122 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9123 {
17d078c5
AM
9124 bfd *def_bfd;
9125 const char *msg;
90c984fc
L
9126 struct elf_link_hash_entry *hi = h;
9127
9128 /* Check indirect symbol. */
9129 while (hi->root.type == bfd_link_hash_indirect)
9130 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9131
9132 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9133 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9134 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9135 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9136 else
9137 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9138 def_bfd = flinfo->output_bfd;
90c984fc
L
9139 if (hi->root.u.def.section != bfd_abs_section_ptr)
9140 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9141 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9142 h->root.root.string);
9143 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9144 eoinfo->failed = TRUE;
9145 return FALSE;
9146 }
9147
9148 /* We don't want to output symbols that have never been mentioned by
9149 a regular file, or that we have been told to strip. However, if
9150 h->indx is set to -2, the symbol is used by a reloc and we must
9151 output it. */
d983c8c5 9152 strip = FALSE;
c152c796 9153 if (h->indx == -2)
d983c8c5 9154 ;
f5385ebf 9155 else if ((h->def_dynamic
77cfaee6
AM
9156 || h->ref_dynamic
9157 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9158 && !h->def_regular
9159 && !h->ref_regular)
c152c796 9160 strip = TRUE;
8b127cbc 9161 else if (flinfo->info->strip == strip_all)
c152c796 9162 strip = TRUE;
8b127cbc
AM
9163 else if (flinfo->info->strip == strip_some
9164 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9165 h->root.root.string, FALSE, FALSE) == NULL)
9166 strip = TRUE;
d56d55e7
AM
9167 else if ((h->root.type == bfd_link_hash_defined
9168 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9169 && ((flinfo->info->strip_discarded
dbaa2011 9170 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9171 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9172 && h->root.u.def.section->owner != NULL
d56d55e7 9173 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9174 strip = TRUE;
9e2278f5
AM
9175 else if ((h->root.type == bfd_link_hash_undefined
9176 || h->root.type == bfd_link_hash_undefweak)
9177 && h->root.u.undef.abfd != NULL
9178 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9179 strip = TRUE;
c152c796
AM
9180
9181 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9182 nothing else to do. However, if it is a forced local symbol or
9183 an ifunc symbol we need to give the backend finish_dynamic_symbol
9184 function a chance to make it dynamic. */
c152c796
AM
9185 if (strip
9186 && h->dynindx == -1
57ca8ac7 9187 && h->type != STT_GNU_IFUNC
f5385ebf 9188 && !h->forced_local)
c152c796
AM
9189 return TRUE;
9190
9191 sym.st_value = 0;
9192 sym.st_size = h->size;
9193 sym.st_other = h->other;
6e33951e 9194 if (local_bind)
935bd1e0
L
9195 {
9196 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9197 /* Turn off visibility on local symbol. */
9198 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9199 }
02acbe22
L
9200 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9201 else if (h->unique_global && h->def_regular)
3e7a7d11 9202 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
c152c796
AM
9203 else if (h->root.type == bfd_link_hash_undefweak
9204 || h->root.type == bfd_link_hash_defweak)
9205 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9206 else
9207 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
35fc36a8 9208 sym.st_target_internal = h->target_internal;
c152c796
AM
9209
9210 switch (h->root.type)
9211 {
9212 default:
9213 case bfd_link_hash_new:
9214 case bfd_link_hash_warning:
9215 abort ();
9216 return FALSE;
9217
9218 case bfd_link_hash_undefined:
9219 case bfd_link_hash_undefweak:
9220 input_sec = bfd_und_section_ptr;
9221 sym.st_shndx = SHN_UNDEF;
9222 break;
9223
9224 case bfd_link_hash_defined:
9225 case bfd_link_hash_defweak:
9226 {
9227 input_sec = h->root.u.def.section;
9228 if (input_sec->output_section != NULL)
9229 {
9230 sym.st_shndx =
8b127cbc 9231 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9232 input_sec->output_section);
9233 if (sym.st_shndx == SHN_BAD)
9234 {
9235 (*_bfd_error_handler)
d003868e 9236 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9237 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9238 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9239 eoinfo->failed = TRUE;
9240 return FALSE;
9241 }
9242
9243 /* ELF symbols in relocatable files are section relative,
9244 but in nonrelocatable files they are virtual
9245 addresses. */
9246 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9247 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9248 {
9249 sym.st_value += input_sec->output_section->vma;
9250 if (h->type == STT_TLS)
9251 {
8b127cbc 9252 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9253 if (tls_sec != NULL)
9254 sym.st_value -= tls_sec->vma;
c152c796
AM
9255 }
9256 }
9257 }
9258 else
9259 {
9260 BFD_ASSERT (input_sec->owner == NULL
9261 || (input_sec->owner->flags & DYNAMIC) != 0);
9262 sym.st_shndx = SHN_UNDEF;
9263 input_sec = bfd_und_section_ptr;
9264 }
9265 }
9266 break;
9267
9268 case bfd_link_hash_common:
9269 input_sec = h->root.u.c.p->section;
a4d8e49b 9270 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9271 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9272 break;
9273
9274 case bfd_link_hash_indirect:
9275 /* These symbols are created by symbol versioning. They point
9276 to the decorated version of the name. For example, if the
9277 symbol foo@@GNU_1.2 is the default, which should be used when
9278 foo is used with no version, then we add an indirect symbol
9279 foo which points to foo@@GNU_1.2. We ignore these symbols,
9280 since the indirected symbol is already in the hash table. */
9281 return TRUE;
9282 }
9283
9284 /* Give the processor backend a chance to tweak the symbol value,
9285 and also to finish up anything that needs to be done for this
9286 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9287 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9288 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9289 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9290 && h->def_regular
0e1862bb 9291 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9292 || ((h->dynindx != -1
9293 || h->forced_local)
0e1862bb 9294 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9295 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9296 || h->root.type != bfd_link_hash_undefweak))
9297 || !h->forced_local)
8b127cbc 9298 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9299 {
9300 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9301 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9302 {
9303 eoinfo->failed = TRUE;
9304 return FALSE;
9305 }
9306 }
9307
9308 /* If we are marking the symbol as undefined, and there are no
9309 non-weak references to this symbol from a regular object, then
9310 mark the symbol as weak undefined; if there are non-weak
9311 references, mark the symbol as strong. We can't do this earlier,
9312 because it might not be marked as undefined until the
9313 finish_dynamic_symbol routine gets through with it. */
9314 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9315 && h->ref_regular
c152c796
AM
9316 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9317 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9318 {
9319 int bindtype;
2955ec4c
L
9320 unsigned int type = ELF_ST_TYPE (sym.st_info);
9321
9322 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9323 if (type == STT_GNU_IFUNC)
9324 type = STT_FUNC;
c152c796 9325
f5385ebf 9326 if (h->ref_regular_nonweak)
c152c796
AM
9327 bindtype = STB_GLOBAL;
9328 else
9329 bindtype = STB_WEAK;
2955ec4c 9330 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9331 }
9332
bda987c2
CD
9333 /* If this is a symbol defined in a dynamic library, don't use the
9334 symbol size from the dynamic library. Relinking an executable
9335 against a new library may introduce gratuitous changes in the
9336 executable's symbols if we keep the size. */
9337 if (sym.st_shndx == SHN_UNDEF
9338 && !h->def_regular
9339 && h->def_dynamic)
9340 sym.st_size = 0;
9341
c152c796
AM
9342 /* If a non-weak symbol with non-default visibility is not defined
9343 locally, it is a fatal error. */
0e1862bb 9344 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9345 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9346 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9347 && h->root.type == bfd_link_hash_undefined
f5385ebf 9348 && !h->def_regular)
c152c796 9349 {
17d078c5
AM
9350 const char *msg;
9351
9352 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9353 msg = _("%B: protected symbol `%s' isn't defined");
9354 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9355 msg = _("%B: internal symbol `%s' isn't defined");
9356 else
9357 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9358 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9359 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9360 eoinfo->failed = TRUE;
9361 return FALSE;
9362 }
9363
9364 /* If this symbol should be put in the .dynsym section, then put it
9365 there now. We already know the symbol index. We also fill in
9366 the entry in the .hash section. */
cae1fbbb 9367 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9368 && h->dynindx != -1
8b127cbc 9369 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9370 {
c152c796
AM
9371 bfd_byte *esym;
9372
90c984fc
L
9373 /* Since there is no version information in the dynamic string,
9374 if there is no version info in symbol version section, we will
1659f720 9375 have a run-time problem if not linking executable, referenced
6e33951e
L
9376 by shared library, not locally defined, or not bound locally.
9377 */
1659f720 9378 if (h->verinfo.verdef == NULL
6e33951e 9379 && !local_bind
0e1862bb 9380 && (!bfd_link_executable (flinfo->info)
1659f720
L
9381 || h->ref_dynamic
9382 || !h->def_regular))
90c984fc
L
9383 {
9384 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9385
9386 if (p && p [1] != '\0')
9387 {
9388 (*_bfd_error_handler)
9389 (_("%B: No symbol version section for versioned symbol `%s'"),
9390 flinfo->output_bfd, h->root.root.string);
9391 eoinfo->failed = TRUE;
9392 return FALSE;
9393 }
9394 }
9395
c152c796 9396 sym.st_name = h->dynstr_index;
cae1fbbb
L
9397 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9398 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9399 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9400 {
9401 eoinfo->failed = TRUE;
9402 return FALSE;
9403 }
8b127cbc 9404 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9405
8b127cbc 9406 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9407 {
9408 size_t hash_entry_size;
9409 bfd_byte *bucketpos;
9410 bfd_vma chain;
41198d0c
L
9411 size_t bucketcount;
9412 size_t bucket;
9413
8b127cbc 9414 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9415 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9416
9417 hash_entry_size
8b127cbc
AM
9418 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9419 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9420 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9421 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9422 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9423 bucketpos);
9424 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9425 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9426 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9427 }
c152c796 9428
8b127cbc 9429 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9430 {
9431 Elf_Internal_Versym iversym;
9432 Elf_External_Versym *eversym;
9433
f5385ebf 9434 if (!h->def_regular)
c152c796 9435 {
7b20f099
AM
9436 if (h->verinfo.verdef == NULL
9437 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9438 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9439 iversym.vs_vers = 0;
9440 else
9441 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9442 }
9443 else
9444 {
9445 if (h->verinfo.vertree == NULL)
9446 iversym.vs_vers = 1;
9447 else
9448 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9449 if (flinfo->info->create_default_symver)
3e3b46e5 9450 iversym.vs_vers++;
c152c796
AM
9451 }
9452
422f1182 9453 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9454 defined locally. */
422f1182 9455 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9456 iversym.vs_vers |= VERSYM_HIDDEN;
9457
8b127cbc 9458 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9459 eversym += h->dynindx;
8b127cbc 9460 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9461 }
9462 }
9463
d983c8c5
AM
9464 /* If the symbol is undefined, and we didn't output it to .dynsym,
9465 strip it from .symtab too. Obviously we can't do this for
9466 relocatable output or when needed for --emit-relocs. */
9467 else if (input_sec == bfd_und_section_ptr
9468 && h->indx != -2
0e1862bb 9469 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9470 return TRUE;
9471 /* Also strip others that we couldn't earlier due to dynamic symbol
9472 processing. */
9473 if (strip)
9474 return TRUE;
9475 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9476 return TRUE;
9477
2ec55de3
AM
9478 /* Output a FILE symbol so that following locals are not associated
9479 with the wrong input file. We need one for forced local symbols
9480 if we've seen more than one FILE symbol or when we have exactly
9481 one FILE symbol but global symbols are present in a file other
9482 than the one with the FILE symbol. We also need one if linker
9483 defined symbols are present. In practice these conditions are
9484 always met, so just emit the FILE symbol unconditionally. */
9485 if (eoinfo->localsyms
9486 && !eoinfo->file_sym_done
9487 && eoinfo->flinfo->filesym_count != 0)
9488 {
9489 Elf_Internal_Sym fsym;
9490
9491 memset (&fsym, 0, sizeof (fsym));
9492 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9493 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9494 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9495 bfd_und_section_ptr, NULL))
2ec55de3
AM
9496 return FALSE;
9497
9498 eoinfo->file_sym_done = TRUE;
9499 }
9500
8b127cbc 9501 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9502 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9503 input_sec, h);
6e0b88f1 9504 if (ret == 0)
c152c796
AM
9505 {
9506 eoinfo->failed = TRUE;
9507 return FALSE;
9508 }
6e0b88f1
AM
9509 else if (ret == 1)
9510 h->indx = indx;
9511 else if (h->indx == -2)
9512 abort();
c152c796
AM
9513
9514 return TRUE;
9515}
9516
cdd3575c
AM
9517/* Return TRUE if special handling is done for relocs in SEC against
9518 symbols defined in discarded sections. */
9519
c152c796
AM
9520static bfd_boolean
9521elf_section_ignore_discarded_relocs (asection *sec)
9522{
9523 const struct elf_backend_data *bed;
9524
cdd3575c
AM
9525 switch (sec->sec_info_type)
9526 {
dbaa2011
AM
9527 case SEC_INFO_TYPE_STABS:
9528 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9529 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9530 return TRUE;
9531 default:
9532 break;
9533 }
c152c796
AM
9534
9535 bed = get_elf_backend_data (sec->owner);
9536 if (bed->elf_backend_ignore_discarded_relocs != NULL
9537 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9538 return TRUE;
9539
9540 return FALSE;
9541}
9542
9e66c942
AM
9543/* Return a mask saying how ld should treat relocations in SEC against
9544 symbols defined in discarded sections. If this function returns
9545 COMPLAIN set, ld will issue a warning message. If this function
9546 returns PRETEND set, and the discarded section was link-once and the
9547 same size as the kept link-once section, ld will pretend that the
9548 symbol was actually defined in the kept section. Otherwise ld will
9549 zero the reloc (at least that is the intent, but some cooperation by
9550 the target dependent code is needed, particularly for REL targets). */
9551
8a696751
AM
9552unsigned int
9553_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9554{
9e66c942 9555 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9556 return PRETEND;
cdd3575c
AM
9557
9558 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9559 return 0;
cdd3575c
AM
9560
9561 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9562 return 0;
cdd3575c 9563
9e66c942 9564 return COMPLAIN | PRETEND;
cdd3575c
AM
9565}
9566
3d7f7666
L
9567/* Find a match between a section and a member of a section group. */
9568
9569static asection *
c0f00686
L
9570match_group_member (asection *sec, asection *group,
9571 struct bfd_link_info *info)
3d7f7666
L
9572{
9573 asection *first = elf_next_in_group (group);
9574 asection *s = first;
9575
9576 while (s != NULL)
9577 {
c0f00686 9578 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9579 return s;
9580
83180ade 9581 s = elf_next_in_group (s);
3d7f7666
L
9582 if (s == first)
9583 break;
9584 }
9585
9586 return NULL;
9587}
9588
01b3c8ab 9589/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9590 to replace it. Return the replacement if it is OK. Otherwise return
9591 NULL. */
01b3c8ab
L
9592
9593asection *
c0f00686 9594_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9595{
9596 asection *kept;
9597
9598 kept = sec->kept_section;
9599 if (kept != NULL)
9600 {
c2370991 9601 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9602 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9603 if (kept != NULL
9604 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9605 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9606 kept = NULL;
c2370991 9607 sec->kept_section = kept;
01b3c8ab
L
9608 }
9609 return kept;
9610}
9611
c152c796
AM
9612/* Link an input file into the linker output file. This function
9613 handles all the sections and relocations of the input file at once.
9614 This is so that we only have to read the local symbols once, and
9615 don't have to keep them in memory. */
9616
9617static bfd_boolean
8b127cbc 9618elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9619{
ece5ef60 9620 int (*relocate_section)
c152c796
AM
9621 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9622 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9623 bfd *output_bfd;
9624 Elf_Internal_Shdr *symtab_hdr;
9625 size_t locsymcount;
9626 size_t extsymoff;
9627 Elf_Internal_Sym *isymbuf;
9628 Elf_Internal_Sym *isym;
9629 Elf_Internal_Sym *isymend;
9630 long *pindex;
9631 asection **ppsection;
9632 asection *o;
9633 const struct elf_backend_data *bed;
c152c796 9634 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9635 bfd_size_type address_size;
9636 bfd_vma r_type_mask;
9637 int r_sym_shift;
ffbc01cc 9638 bfd_boolean have_file_sym = FALSE;
c152c796 9639
8b127cbc 9640 output_bfd = flinfo->output_bfd;
c152c796
AM
9641 bed = get_elf_backend_data (output_bfd);
9642 relocate_section = bed->elf_backend_relocate_section;
9643
9644 /* If this is a dynamic object, we don't want to do anything here:
9645 we don't want the local symbols, and we don't want the section
9646 contents. */
9647 if ((input_bfd->flags & DYNAMIC) != 0)
9648 return TRUE;
9649
c152c796
AM
9650 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9651 if (elf_bad_symtab (input_bfd))
9652 {
9653 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9654 extsymoff = 0;
9655 }
9656 else
9657 {
9658 locsymcount = symtab_hdr->sh_info;
9659 extsymoff = symtab_hdr->sh_info;
9660 }
9661
9662 /* Read the local symbols. */
9663 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9664 if (isymbuf == NULL && locsymcount != 0)
9665 {
9666 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9667 flinfo->internal_syms,
9668 flinfo->external_syms,
9669 flinfo->locsym_shndx);
c152c796
AM
9670 if (isymbuf == NULL)
9671 return FALSE;
9672 }
9673
9674 /* Find local symbol sections and adjust values of symbols in
9675 SEC_MERGE sections. Write out those local symbols we know are
9676 going into the output file. */
9677 isymend = isymbuf + locsymcount;
8b127cbc 9678 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9679 isym < isymend;
9680 isym++, pindex++, ppsection++)
9681 {
9682 asection *isec;
9683 const char *name;
9684 Elf_Internal_Sym osym;
6e0b88f1
AM
9685 long indx;
9686 int ret;
c152c796
AM
9687
9688 *pindex = -1;
9689
9690 if (elf_bad_symtab (input_bfd))
9691 {
9692 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9693 {
9694 *ppsection = NULL;
9695 continue;
9696 }
9697 }
9698
9699 if (isym->st_shndx == SHN_UNDEF)
9700 isec = bfd_und_section_ptr;
c152c796
AM
9701 else if (isym->st_shndx == SHN_ABS)
9702 isec = bfd_abs_section_ptr;
9703 else if (isym->st_shndx == SHN_COMMON)
9704 isec = bfd_com_section_ptr;
9705 else
9706 {
cb33740c
AM
9707 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9708 if (isec == NULL)
9709 {
9710 /* Don't attempt to output symbols with st_shnx in the
9711 reserved range other than SHN_ABS and SHN_COMMON. */
9712 *ppsection = NULL;
9713 continue;
9714 }
dbaa2011 9715 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9716 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9717 isym->st_value =
9718 _bfd_merged_section_offset (output_bfd, &isec,
9719 elf_section_data (isec)->sec_info,
9720 isym->st_value);
c152c796
AM
9721 }
9722
9723 *ppsection = isec;
9724
d983c8c5
AM
9725 /* Don't output the first, undefined, symbol. In fact, don't
9726 output any undefined local symbol. */
9727 if (isec == bfd_und_section_ptr)
c152c796
AM
9728 continue;
9729
9730 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9731 {
9732 /* We never output section symbols. Instead, we use the
9733 section symbol of the corresponding section in the output
9734 file. */
9735 continue;
9736 }
9737
9738 /* If we are stripping all symbols, we don't want to output this
9739 one. */
8b127cbc 9740 if (flinfo->info->strip == strip_all)
c152c796
AM
9741 continue;
9742
9743 /* If we are discarding all local symbols, we don't want to
9744 output this one. If we are generating a relocatable output
9745 file, then some of the local symbols may be required by
9746 relocs; we output them below as we discover that they are
9747 needed. */
8b127cbc 9748 if (flinfo->info->discard == discard_all)
c152c796
AM
9749 continue;
9750
9751 /* If this symbol is defined in a section which we are
f02571c5
AM
9752 discarding, we don't need to keep it. */
9753 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9754 && isym->st_shndx < SHN_LORESERVE
9755 && bfd_section_removed_from_list (output_bfd,
9756 isec->output_section))
e75a280b
L
9757 continue;
9758
c152c796
AM
9759 /* Get the name of the symbol. */
9760 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9761 isym->st_name);
9762 if (name == NULL)
9763 return FALSE;
9764
9765 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9766 if ((flinfo->info->strip == strip_some
9767 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9768 == NULL))
8b127cbc 9769 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9770 && (isec->flags & SEC_MERGE)
9771 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9772 || flinfo->info->discard == discard_l)
c152c796
AM
9773 && bfd_is_local_label_name (input_bfd, name)))
9774 continue;
9775
ffbc01cc
AM
9776 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9777 {
ce875075
AM
9778 if (input_bfd->lto_output)
9779 /* -flto puts a temp file name here. This means builds
9780 are not reproducible. Discard the symbol. */
9781 continue;
ffbc01cc
AM
9782 have_file_sym = TRUE;
9783 flinfo->filesym_count += 1;
9784 }
9785 if (!have_file_sym)
9786 {
9787 /* In the absence of debug info, bfd_find_nearest_line uses
9788 FILE symbols to determine the source file for local
9789 function symbols. Provide a FILE symbol here if input
9790 files lack such, so that their symbols won't be
9791 associated with a previous input file. It's not the
9792 source file, but the best we can do. */
9793 have_file_sym = TRUE;
9794 flinfo->filesym_count += 1;
9795 memset (&osym, 0, sizeof (osym));
9796 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9797 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9798 if (!elf_link_output_symstrtab (flinfo,
9799 (input_bfd->lto_output ? NULL
9800 : input_bfd->filename),
9801 &osym, bfd_abs_section_ptr,
9802 NULL))
ffbc01cc
AM
9803 return FALSE;
9804 }
9805
c152c796
AM
9806 osym = *isym;
9807
9808 /* Adjust the section index for the output file. */
9809 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9810 isec->output_section);
9811 if (osym.st_shndx == SHN_BAD)
9812 return FALSE;
9813
c152c796
AM
9814 /* ELF symbols in relocatable files are section relative, but
9815 in executable files they are virtual addresses. Note that
9816 this code assumes that all ELF sections have an associated
9817 BFD section with a reasonable value for output_offset; below
9818 we assume that they also have a reasonable value for
9819 output_section. Any special sections must be set up to meet
9820 these requirements. */
9821 osym.st_value += isec->output_offset;
0e1862bb 9822 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9823 {
9824 osym.st_value += isec->output_section->vma;
9825 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9826 {
9827 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9828 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9829 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9830 }
9831 }
9832
6e0b88f1 9833 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9834 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9835 if (ret == 0)
c152c796 9836 return FALSE;
6e0b88f1
AM
9837 else if (ret == 1)
9838 *pindex = indx;
c152c796
AM
9839 }
9840
310fd250
L
9841 if (bed->s->arch_size == 32)
9842 {
9843 r_type_mask = 0xff;
9844 r_sym_shift = 8;
9845 address_size = 4;
9846 }
9847 else
9848 {
9849 r_type_mask = 0xffffffff;
9850 r_sym_shift = 32;
9851 address_size = 8;
9852 }
9853
c152c796
AM
9854 /* Relocate the contents of each section. */
9855 sym_hashes = elf_sym_hashes (input_bfd);
9856 for (o = input_bfd->sections; o != NULL; o = o->next)
9857 {
9858 bfd_byte *contents;
9859
9860 if (! o->linker_mark)
9861 {
9862 /* This section was omitted from the link. */
9863 continue;
9864 }
9865
0e1862bb 9866 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9867 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9868 {
9869 /* Deal with the group signature symbol. */
9870 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9871 unsigned long symndx = sec_data->this_hdr.sh_info;
9872 asection *osec = o->output_section;
9873
9874 if (symndx >= locsymcount
9875 || (elf_bad_symtab (input_bfd)
8b127cbc 9876 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9877 {
9878 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9879 while (h->root.type == bfd_link_hash_indirect
9880 || h->root.type == bfd_link_hash_warning)
9881 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9882 /* Arrange for symbol to be output. */
9883 h->indx = -2;
9884 elf_section_data (osec)->this_hdr.sh_info = -2;
9885 }
9886 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9887 {
9888 /* We'll use the output section target_index. */
8b127cbc 9889 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9890 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9891 }
9892 else
9893 {
8b127cbc 9894 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9895 {
9896 /* Otherwise output the local symbol now. */
9897 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9898 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 9899 const char *name;
6e0b88f1
AM
9900 long indx;
9901 int ret;
bcacc0f5
AM
9902
9903 name = bfd_elf_string_from_elf_section (input_bfd,
9904 symtab_hdr->sh_link,
9905 sym.st_name);
9906 if (name == NULL)
9907 return FALSE;
9908
9909 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9910 sec);
9911 if (sym.st_shndx == SHN_BAD)
9912 return FALSE;
9913
9914 sym.st_value += o->output_offset;
9915
6e0b88f1 9916 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
9917 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9918 NULL);
6e0b88f1 9919 if (ret == 0)
bcacc0f5 9920 return FALSE;
6e0b88f1 9921 else if (ret == 1)
8b127cbc 9922 flinfo->indices[symndx] = indx;
6e0b88f1
AM
9923 else
9924 abort ();
bcacc0f5
AM
9925 }
9926 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 9927 = flinfo->indices[symndx];
bcacc0f5
AM
9928 }
9929 }
9930
c152c796 9931 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9932 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9933 continue;
9934
9935 if ((o->flags & SEC_LINKER_CREATED) != 0)
9936 {
9937 /* Section was created by _bfd_elf_link_create_dynamic_sections
9938 or somesuch. */
9939 continue;
9940 }
9941
9942 /* Get the contents of the section. They have been cached by a
9943 relaxation routine. Note that o is a section in an input
9944 file, so the contents field will not have been set by any of
9945 the routines which work on output files. */
9946 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
9947 {
9948 contents = elf_section_data (o)->this_hdr.contents;
9949 if (bed->caches_rawsize
9950 && o->rawsize != 0
9951 && o->rawsize < o->size)
9952 {
9953 memcpy (flinfo->contents, contents, o->rawsize);
9954 contents = flinfo->contents;
9955 }
9956 }
c152c796
AM
9957 else
9958 {
8b127cbc 9959 contents = flinfo->contents;
4a114e3e 9960 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
9961 return FALSE;
9962 }
9963
9964 if ((o->flags & SEC_RELOC) != 0)
9965 {
9966 Elf_Internal_Rela *internal_relocs;
0f02bbd9 9967 Elf_Internal_Rela *rel, *relend;
0f02bbd9 9968 int action_discarded;
ece5ef60 9969 int ret;
c152c796
AM
9970
9971 /* Get the swapped relocs. */
9972 internal_relocs
8b127cbc
AM
9973 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9974 flinfo->internal_relocs, FALSE);
c152c796
AM
9975 if (internal_relocs == NULL
9976 && o->reloc_count > 0)
9977 return FALSE;
9978
310fd250
L
9979 /* We need to reverse-copy input .ctors/.dtors sections if
9980 they are placed in .init_array/.finit_array for output. */
9981 if (o->size > address_size
9982 && ((strncmp (o->name, ".ctors", 6) == 0
9983 && strcmp (o->output_section->name,
9984 ".init_array") == 0)
9985 || (strncmp (o->name, ".dtors", 6) == 0
9986 && strcmp (o->output_section->name,
9987 ".fini_array") == 0))
9988 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 9989 {
310fd250
L
9990 if (o->size != o->reloc_count * address_size)
9991 {
9992 (*_bfd_error_handler)
9993 (_("error: %B: size of section %A is not "
9994 "multiple of address size"),
9995 input_bfd, o);
9996 bfd_set_error (bfd_error_on_input);
9997 return FALSE;
9998 }
9999 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10000 }
10001
0f02bbd9 10002 action_discarded = -1;
c152c796 10003 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10004 action_discarded = (*bed->action_discarded) (o);
10005
10006 /* Run through the relocs evaluating complex reloc symbols and
10007 looking for relocs against symbols from discarded sections
10008 or section symbols from removed link-once sections.
10009 Complain about relocs against discarded sections. Zero
10010 relocs against removed link-once sections. */
10011
10012 rel = internal_relocs;
10013 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10014 for ( ; rel < relend; rel++)
c152c796 10015 {
0f02bbd9
AM
10016 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10017 unsigned int s_type;
10018 asection **ps, *sec;
10019 struct elf_link_hash_entry *h = NULL;
10020 const char *sym_name;
c152c796 10021
0f02bbd9
AM
10022 if (r_symndx == STN_UNDEF)
10023 continue;
c152c796 10024
0f02bbd9
AM
10025 if (r_symndx >= locsymcount
10026 || (elf_bad_symtab (input_bfd)
8b127cbc 10027 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10028 {
10029 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10030
0f02bbd9
AM
10031 /* Badly formatted input files can contain relocs that
10032 reference non-existant symbols. Check here so that
10033 we do not seg fault. */
10034 if (h == NULL)
c152c796 10035 {
0f02bbd9 10036 char buffer [32];
dce669a1 10037
0f02bbd9
AM
10038 sprintf_vma (buffer, rel->r_info);
10039 (*_bfd_error_handler)
10040 (_("error: %B contains a reloc (0x%s) for section %A "
10041 "that references a non-existent global symbol"),
10042 input_bfd, o, buffer);
10043 bfd_set_error (bfd_error_bad_value);
10044 return FALSE;
10045 }
3b36f7e6 10046
0f02bbd9
AM
10047 while (h->root.type == bfd_link_hash_indirect
10048 || h->root.type == bfd_link_hash_warning)
10049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10050
0f02bbd9 10051 s_type = h->type;
cdd3575c 10052
9e2dec47 10053 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10054 mark the symbol as undefined. Note that the
10055 linker may attach linker created dynamic sections
10056 to the plugin bfd. Symbols defined in linker
10057 created sections are not plugin symbols. */
9e2dec47
L
10058 if (h->root.non_ir_ref
10059 && (h->root.type == bfd_link_hash_defined
10060 || h->root.type == bfd_link_hash_defweak)
10061 && (h->root.u.def.section->flags
10062 & SEC_LINKER_CREATED) == 0
10063 && h->root.u.def.section->owner != NULL
10064 && (h->root.u.def.section->owner->flags
10065 & BFD_PLUGIN) != 0)
10066 {
10067 h->root.type = bfd_link_hash_undefined;
10068 h->root.u.undef.abfd = h->root.u.def.section->owner;
10069 }
10070
0f02bbd9
AM
10071 ps = NULL;
10072 if (h->root.type == bfd_link_hash_defined
10073 || h->root.type == bfd_link_hash_defweak)
10074 ps = &h->root.u.def.section;
10075
10076 sym_name = h->root.root.string;
10077 }
10078 else
10079 {
10080 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10081
10082 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10083 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10084 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10085 sym, *ps);
10086 }
c152c796 10087
c301e700 10088 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10089 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10090 {
10091 bfd_vma val;
10092 bfd_vma dot = (rel->r_offset
10093 + o->output_offset + o->output_section->vma);
10094#ifdef DEBUG
10095 printf ("Encountered a complex symbol!");
10096 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10097 input_bfd->filename, o->name,
10098 (long) (rel - internal_relocs));
0f02bbd9
AM
10099 printf (" symbol: idx %8.8lx, name %s\n",
10100 r_symndx, sym_name);
10101 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10102 (unsigned long) rel->r_info,
10103 (unsigned long) rel->r_offset);
10104#endif
8b127cbc 10105 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10106 isymbuf, locsymcount, s_type == STT_SRELC))
10107 return FALSE;
10108
10109 /* Symbol evaluated OK. Update to absolute value. */
10110 set_symbol_value (input_bfd, isymbuf, locsymcount,
10111 r_symndx, val);
10112 continue;
10113 }
10114
10115 if (action_discarded != -1 && ps != NULL)
10116 {
cdd3575c
AM
10117 /* Complain if the definition comes from a
10118 discarded section. */
dbaa2011 10119 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10120 {
cf35638d 10121 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10122 if (action_discarded & COMPLAIN)
8b127cbc 10123 (*flinfo->info->callbacks->einfo)
e1fffbe6 10124 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10125 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10126 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10127
87e5235d 10128 /* Try to do the best we can to support buggy old
e0ae6d6f 10129 versions of gcc. Pretend that the symbol is
87e5235d
AM
10130 really defined in the kept linkonce section.
10131 FIXME: This is quite broken. Modifying the
10132 symbol here means we will be changing all later
e0ae6d6f 10133 uses of the symbol, not just in this section. */
0f02bbd9 10134 if (action_discarded & PRETEND)
87e5235d 10135 {
01b3c8ab
L
10136 asection *kept;
10137
c0f00686 10138 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10139 flinfo->info);
01b3c8ab 10140 if (kept != NULL)
87e5235d
AM
10141 {
10142 *ps = kept;
10143 continue;
10144 }
10145 }
c152c796
AM
10146 }
10147 }
10148 }
10149
10150 /* Relocate the section by invoking a back end routine.
10151
10152 The back end routine is responsible for adjusting the
10153 section contents as necessary, and (if using Rela relocs
10154 and generating a relocatable output file) adjusting the
10155 reloc addend as necessary.
10156
10157 The back end routine does not have to worry about setting
10158 the reloc address or the reloc symbol index.
10159
10160 The back end routine is given a pointer to the swapped in
10161 internal symbols, and can access the hash table entries
10162 for the external symbols via elf_sym_hashes (input_bfd).
10163
10164 When generating relocatable output, the back end routine
10165 must handle STB_LOCAL/STT_SECTION symbols specially. The
10166 output symbol is going to be a section symbol
10167 corresponding to the output section, which will require
10168 the addend to be adjusted. */
10169
8b127cbc 10170 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10171 input_bfd, o, contents,
10172 internal_relocs,
10173 isymbuf,
8b127cbc 10174 flinfo->sections);
ece5ef60 10175 if (!ret)
c152c796
AM
10176 return FALSE;
10177
ece5ef60 10178 if (ret == 2
0e1862bb 10179 || bfd_link_relocatable (flinfo->info)
8b127cbc 10180 || flinfo->info->emitrelocations)
c152c796
AM
10181 {
10182 Elf_Internal_Rela *irela;
d4730f92 10183 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10184 bfd_vma last_offset;
10185 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10186 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10187 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10188 unsigned int next_erel;
c152c796 10189 bfd_boolean rela_normal;
d4730f92 10190 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10191
d4730f92
BS
10192 esdi = elf_section_data (o);
10193 esdo = elf_section_data (o->output_section);
10194 rela_normal = FALSE;
c152c796
AM
10195
10196 /* Adjust the reloc addresses and symbol indices. */
10197
10198 irela = internal_relocs;
10199 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10200 rel_hash = esdo->rel.hashes + esdo->rel.count;
10201 /* We start processing the REL relocs, if any. When we reach
10202 IRELAMID in the loop, we switch to the RELA relocs. */
10203 irelamid = irela;
10204 if (esdi->rel.hdr != NULL)
10205 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10206 * bed->s->int_rels_per_ext_rel);
eac338cf 10207 rel_hash_list = rel_hash;
d4730f92 10208 rela_hash_list = NULL;
c152c796 10209 last_offset = o->output_offset;
0e1862bb 10210 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10211 last_offset += o->output_section->vma;
10212 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10213 {
10214 unsigned long r_symndx;
10215 asection *sec;
10216 Elf_Internal_Sym sym;
10217
10218 if (next_erel == bed->s->int_rels_per_ext_rel)
10219 {
10220 rel_hash++;
10221 next_erel = 0;
10222 }
10223
d4730f92
BS
10224 if (irela == irelamid)
10225 {
10226 rel_hash = esdo->rela.hashes + esdo->rela.count;
10227 rela_hash_list = rel_hash;
10228 rela_normal = bed->rela_normal;
10229 }
10230
c152c796 10231 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10232 flinfo->info, o,
c152c796
AM
10233 irela->r_offset);
10234 if (irela->r_offset >= (bfd_vma) -2)
10235 {
10236 /* This is a reloc for a deleted entry or somesuch.
10237 Turn it into an R_*_NONE reloc, at the same
10238 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10239 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10240 being ordered. */
10241 irela->r_offset = last_offset;
10242 irela->r_info = 0;
10243 irela->r_addend = 0;
10244 continue;
10245 }
10246
10247 irela->r_offset += o->output_offset;
10248
10249 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10250 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10251 irela->r_offset += o->output_section->vma;
10252
10253 last_offset = irela->r_offset;
10254
10255 r_symndx = irela->r_info >> r_sym_shift;
10256 if (r_symndx == STN_UNDEF)
10257 continue;
10258
10259 if (r_symndx >= locsymcount
10260 || (elf_bad_symtab (input_bfd)
8b127cbc 10261 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10262 {
10263 struct elf_link_hash_entry *rh;
10264 unsigned long indx;
10265
10266 /* This is a reloc against a global symbol. We
10267 have not yet output all the local symbols, so
10268 we do not know the symbol index of any global
10269 symbol. We set the rel_hash entry for this
10270 reloc to point to the global hash table entry
10271 for this symbol. The symbol index is then
ee75fd95 10272 set at the end of bfd_elf_final_link. */
c152c796
AM
10273 indx = r_symndx - extsymoff;
10274 rh = elf_sym_hashes (input_bfd)[indx];
10275 while (rh->root.type == bfd_link_hash_indirect
10276 || rh->root.type == bfd_link_hash_warning)
10277 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10278
10279 /* Setting the index to -2 tells
10280 elf_link_output_extsym that this symbol is
10281 used by a reloc. */
10282 BFD_ASSERT (rh->indx < 0);
10283 rh->indx = -2;
10284
10285 *rel_hash = rh;
10286
10287 continue;
10288 }
10289
10290 /* This is a reloc against a local symbol. */
10291
10292 *rel_hash = NULL;
10293 sym = isymbuf[r_symndx];
8b127cbc 10294 sec = flinfo->sections[r_symndx];
c152c796
AM
10295 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10296 {
10297 /* I suppose the backend ought to fill in the
10298 section of any STT_SECTION symbol against a
6a8d1586 10299 processor specific section. */
cf35638d 10300 r_symndx = STN_UNDEF;
6a8d1586
AM
10301 if (bfd_is_abs_section (sec))
10302 ;
c152c796
AM
10303 else if (sec == NULL || sec->owner == NULL)
10304 {
10305 bfd_set_error (bfd_error_bad_value);
10306 return FALSE;
10307 }
10308 else
10309 {
6a8d1586
AM
10310 asection *osec = sec->output_section;
10311
10312 /* If we have discarded a section, the output
10313 section will be the absolute section. In
ab96bf03
AM
10314 case of discarded SEC_MERGE sections, use
10315 the kept section. relocate_section should
10316 have already handled discarded linkonce
10317 sections. */
6a8d1586
AM
10318 if (bfd_is_abs_section (osec)
10319 && sec->kept_section != NULL
10320 && sec->kept_section->output_section != NULL)
10321 {
10322 osec = sec->kept_section->output_section;
10323 irela->r_addend -= osec->vma;
10324 }
10325
10326 if (!bfd_is_abs_section (osec))
10327 {
10328 r_symndx = osec->target_index;
cf35638d 10329 if (r_symndx == STN_UNDEF)
74541ad4 10330 {
051d833a
AM
10331 irela->r_addend += osec->vma;
10332 osec = _bfd_nearby_section (output_bfd, osec,
10333 osec->vma);
10334 irela->r_addend -= osec->vma;
10335 r_symndx = osec->target_index;
74541ad4 10336 }
6a8d1586 10337 }
c152c796
AM
10338 }
10339
10340 /* Adjust the addend according to where the
10341 section winds up in the output section. */
10342 if (rela_normal)
10343 irela->r_addend += sec->output_offset;
10344 }
10345 else
10346 {
8b127cbc 10347 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10348 {
10349 unsigned long shlink;
10350 const char *name;
10351 asection *osec;
6e0b88f1 10352 long indx;
c152c796 10353
8b127cbc 10354 if (flinfo->info->strip == strip_all)
c152c796
AM
10355 {
10356 /* You can't do ld -r -s. */
10357 bfd_set_error (bfd_error_invalid_operation);
10358 return FALSE;
10359 }
10360
10361 /* This symbol was skipped earlier, but
10362 since it is needed by a reloc, we
10363 must output it now. */
10364 shlink = symtab_hdr->sh_link;
10365 name = (bfd_elf_string_from_elf_section
10366 (input_bfd, shlink, sym.st_name));
10367 if (name == NULL)
10368 return FALSE;
10369
10370 osec = sec->output_section;
10371 sym.st_shndx =
10372 _bfd_elf_section_from_bfd_section (output_bfd,
10373 osec);
10374 if (sym.st_shndx == SHN_BAD)
10375 return FALSE;
10376
10377 sym.st_value += sec->output_offset;
0e1862bb 10378 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10379 {
10380 sym.st_value += osec->vma;
10381 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10382 {
10383 /* STT_TLS symbols are relative to PT_TLS
10384 segment base. */
8b127cbc 10385 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10386 ->tls_sec != NULL);
8b127cbc 10387 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10388 ->tls_sec->vma);
10389 }
10390 }
10391
6e0b88f1 10392 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10393 ret = elf_link_output_symstrtab (flinfo, name,
10394 &sym, sec,
10395 NULL);
6e0b88f1 10396 if (ret == 0)
c152c796 10397 return FALSE;
6e0b88f1 10398 else if (ret == 1)
8b127cbc 10399 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10400 else
10401 abort ();
c152c796
AM
10402 }
10403
8b127cbc 10404 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10405 }
10406
10407 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10408 | (irela->r_info & r_type_mask));
10409 }
10410
10411 /* Swap out the relocs. */
d4730f92
BS
10412 input_rel_hdr = esdi->rel.hdr;
10413 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10414 {
d4730f92
BS
10415 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10416 input_rel_hdr,
10417 internal_relocs,
10418 rel_hash_list))
10419 return FALSE;
c152c796
AM
10420 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10421 * bed->s->int_rels_per_ext_rel);
eac338cf 10422 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10423 }
10424
10425 input_rela_hdr = esdi->rela.hdr;
10426 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10427 {
eac338cf 10428 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10429 input_rela_hdr,
eac338cf 10430 internal_relocs,
d4730f92 10431 rela_hash_list))
c152c796
AM
10432 return FALSE;
10433 }
10434 }
10435 }
10436
10437 /* Write out the modified section contents. */
10438 if (bed->elf_backend_write_section
8b127cbc 10439 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10440 contents))
c152c796
AM
10441 {
10442 /* Section written out. */
10443 }
10444 else switch (o->sec_info_type)
10445 {
dbaa2011 10446 case SEC_INFO_TYPE_STABS:
c152c796
AM
10447 if (! (_bfd_write_section_stabs
10448 (output_bfd,
8b127cbc 10449 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10450 o, &elf_section_data (o)->sec_info, contents)))
10451 return FALSE;
10452 break;
dbaa2011 10453 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10454 if (! _bfd_write_merged_section (output_bfd, o,
10455 elf_section_data (o)->sec_info))
10456 return FALSE;
10457 break;
dbaa2011 10458 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10459 {
8b127cbc 10460 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10461 o, contents))
10462 return FALSE;
10463 }
10464 break;
2f0c68f2
CM
10465 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10466 {
10467 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10468 flinfo->info,
10469 o, contents))
10470 return FALSE;
10471 }
10472 break;
c152c796
AM
10473 default:
10474 {
5dabe785 10475 /* FIXME: octets_per_byte. */
310fd250
L
10476 if (! (o->flags & SEC_EXCLUDE))
10477 {
10478 file_ptr offset = (file_ptr) o->output_offset;
10479 bfd_size_type todo = o->size;
10480 if ((o->flags & SEC_ELF_REVERSE_COPY))
10481 {
10482 /* Reverse-copy input section to output. */
10483 do
10484 {
10485 todo -= address_size;
10486 if (! bfd_set_section_contents (output_bfd,
10487 o->output_section,
10488 contents + todo,
10489 offset,
10490 address_size))
10491 return FALSE;
10492 if (todo == 0)
10493 break;
10494 offset += address_size;
10495 }
10496 while (1);
10497 }
10498 else if (! bfd_set_section_contents (output_bfd,
10499 o->output_section,
10500 contents,
10501 offset, todo))
10502 return FALSE;
10503 }
c152c796
AM
10504 }
10505 break;
10506 }
10507 }
10508
10509 return TRUE;
10510}
10511
10512/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10513 requested by the linker, and does not come from any input file. This
c152c796
AM
10514 is used to build constructor and destructor tables when linking
10515 with -Ur. */
10516
10517static bfd_boolean
10518elf_reloc_link_order (bfd *output_bfd,
10519 struct bfd_link_info *info,
10520 asection *output_section,
10521 struct bfd_link_order *link_order)
10522{
10523 reloc_howto_type *howto;
10524 long indx;
10525 bfd_vma offset;
10526 bfd_vma addend;
d4730f92 10527 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10528 struct elf_link_hash_entry **rel_hash_ptr;
10529 Elf_Internal_Shdr *rel_hdr;
10530 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10531 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10532 bfd_byte *erel;
10533 unsigned int i;
d4730f92 10534 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10535
10536 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10537 if (howto == NULL)
10538 {
10539 bfd_set_error (bfd_error_bad_value);
10540 return FALSE;
10541 }
10542
10543 addend = link_order->u.reloc.p->addend;
10544
d4730f92
BS
10545 if (esdo->rel.hdr)
10546 reldata = &esdo->rel;
10547 else if (esdo->rela.hdr)
10548 reldata = &esdo->rela;
10549 else
10550 {
10551 reldata = NULL;
10552 BFD_ASSERT (0);
10553 }
10554
c152c796 10555 /* Figure out the symbol index. */
d4730f92 10556 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10557 if (link_order->type == bfd_section_reloc_link_order)
10558 {
10559 indx = link_order->u.reloc.p->u.section->target_index;
10560 BFD_ASSERT (indx != 0);
10561 *rel_hash_ptr = NULL;
10562 }
10563 else
10564 {
10565 struct elf_link_hash_entry *h;
10566
10567 /* Treat a reloc against a defined symbol as though it were
10568 actually against the section. */
10569 h = ((struct elf_link_hash_entry *)
10570 bfd_wrapped_link_hash_lookup (output_bfd, info,
10571 link_order->u.reloc.p->u.name,
10572 FALSE, FALSE, TRUE));
10573 if (h != NULL
10574 && (h->root.type == bfd_link_hash_defined
10575 || h->root.type == bfd_link_hash_defweak))
10576 {
10577 asection *section;
10578
10579 section = h->root.u.def.section;
10580 indx = section->output_section->target_index;
10581 *rel_hash_ptr = NULL;
10582 /* It seems that we ought to add the symbol value to the
10583 addend here, but in practice it has already been added
10584 because it was passed to constructor_callback. */
10585 addend += section->output_section->vma + section->output_offset;
10586 }
10587 else if (h != NULL)
10588 {
10589 /* Setting the index to -2 tells elf_link_output_extsym that
10590 this symbol is used by a reloc. */
10591 h->indx = -2;
10592 *rel_hash_ptr = h;
10593 indx = 0;
10594 }
10595 else
10596 {
10597 if (! ((*info->callbacks->unattached_reloc)
10598 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10599 return FALSE;
10600 indx = 0;
10601 }
10602 }
10603
10604 /* If this is an inplace reloc, we must write the addend into the
10605 object file. */
10606 if (howto->partial_inplace && addend != 0)
10607 {
10608 bfd_size_type size;
10609 bfd_reloc_status_type rstat;
10610 bfd_byte *buf;
10611 bfd_boolean ok;
10612 const char *sym_name;
10613
a50b1753
NC
10614 size = (bfd_size_type) bfd_get_reloc_size (howto);
10615 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10616 if (buf == NULL && size != 0)
c152c796
AM
10617 return FALSE;
10618 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10619 switch (rstat)
10620 {
10621 case bfd_reloc_ok:
10622 break;
10623
10624 default:
10625 case bfd_reloc_outofrange:
10626 abort ();
10627
10628 case bfd_reloc_overflow:
10629 if (link_order->type == bfd_section_reloc_link_order)
10630 sym_name = bfd_section_name (output_bfd,
10631 link_order->u.reloc.p->u.section);
10632 else
10633 sym_name = link_order->u.reloc.p->u.name;
10634 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10635 (info, NULL, sym_name, howto->name, addend, NULL,
10636 NULL, (bfd_vma) 0)))
c152c796
AM
10637 {
10638 free (buf);
10639 return FALSE;
10640 }
10641 break;
10642 }
10643 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10644 link_order->offset, size);
10645 free (buf);
10646 if (! ok)
10647 return FALSE;
10648 }
10649
10650 /* The address of a reloc is relative to the section in a
10651 relocatable file, and is a virtual address in an executable
10652 file. */
10653 offset = link_order->offset;
0e1862bb 10654 if (! bfd_link_relocatable (info))
c152c796
AM
10655 offset += output_section->vma;
10656
10657 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10658 {
10659 irel[i].r_offset = offset;
10660 irel[i].r_info = 0;
10661 irel[i].r_addend = 0;
10662 }
10663 if (bed->s->arch_size == 32)
10664 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10665 else
10666 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10667
d4730f92 10668 rel_hdr = reldata->hdr;
c152c796
AM
10669 erel = rel_hdr->contents;
10670 if (rel_hdr->sh_type == SHT_REL)
10671 {
d4730f92 10672 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10673 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10674 }
10675 else
10676 {
10677 irel[0].r_addend = addend;
d4730f92 10678 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10679 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10680 }
10681
d4730f92 10682 ++reldata->count;
c152c796
AM
10683
10684 return TRUE;
10685}
10686
0b52efa6
PB
10687
10688/* Get the output vma of the section pointed to by the sh_link field. */
10689
10690static bfd_vma
10691elf_get_linked_section_vma (struct bfd_link_order *p)
10692{
10693 Elf_Internal_Shdr **elf_shdrp;
10694 asection *s;
10695 int elfsec;
10696
10697 s = p->u.indirect.section;
10698 elf_shdrp = elf_elfsections (s->owner);
10699 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10700 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10701 /* PR 290:
10702 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10703 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10704 sh_info fields. Hence we could get the situation
10705 where elfsec is 0. */
10706 if (elfsec == 0)
10707 {
10708 const struct elf_backend_data *bed
10709 = get_elf_backend_data (s->owner);
10710 if (bed->link_order_error_handler)
d003868e
AM
10711 bed->link_order_error_handler
10712 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10713 return 0;
10714 }
10715 else
10716 {
10717 s = elf_shdrp[elfsec]->bfd_section;
10718 return s->output_section->vma + s->output_offset;
10719 }
0b52efa6
PB
10720}
10721
10722
10723/* Compare two sections based on the locations of the sections they are
10724 linked to. Used by elf_fixup_link_order. */
10725
10726static int
10727compare_link_order (const void * a, const void * b)
10728{
10729 bfd_vma apos;
10730 bfd_vma bpos;
10731
10732 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10733 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10734 if (apos < bpos)
10735 return -1;
10736 return apos > bpos;
10737}
10738
10739
10740/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10741 order as their linked sections. Returns false if this could not be done
10742 because an output section includes both ordered and unordered
10743 sections. Ideally we'd do this in the linker proper. */
10744
10745static bfd_boolean
10746elf_fixup_link_order (bfd *abfd, asection *o)
10747{
10748 int seen_linkorder;
10749 int seen_other;
10750 int n;
10751 struct bfd_link_order *p;
10752 bfd *sub;
10753 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10754 unsigned elfsec;
0b52efa6 10755 struct bfd_link_order **sections;
d33cdfe3 10756 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10757 bfd_vma offset;
3b36f7e6 10758
d33cdfe3
L
10759 other_sec = NULL;
10760 linkorder_sec = NULL;
0b52efa6
PB
10761 seen_other = 0;
10762 seen_linkorder = 0;
8423293d 10763 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10764 {
d33cdfe3 10765 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10766 {
10767 s = p->u.indirect.section;
d33cdfe3
L
10768 sub = s->owner;
10769 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10770 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10771 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10772 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10773 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10774 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10775 {
10776 seen_linkorder++;
10777 linkorder_sec = s;
10778 }
0b52efa6 10779 else
d33cdfe3
L
10780 {
10781 seen_other++;
10782 other_sec = s;
10783 }
0b52efa6
PB
10784 }
10785 else
10786 seen_other++;
d33cdfe3
L
10787
10788 if (seen_other && seen_linkorder)
10789 {
10790 if (other_sec && linkorder_sec)
10791 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10792 o, linkorder_sec,
10793 linkorder_sec->owner, other_sec,
10794 other_sec->owner);
10795 else
10796 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10797 o);
10798 bfd_set_error (bfd_error_bad_value);
10799 return FALSE;
10800 }
0b52efa6
PB
10801 }
10802
10803 if (!seen_linkorder)
10804 return TRUE;
10805
0b52efa6 10806 sections = (struct bfd_link_order **)
14b1c01e
AM
10807 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10808 if (sections == NULL)
10809 return FALSE;
0b52efa6 10810 seen_linkorder = 0;
3b36f7e6 10811
8423293d 10812 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10813 {
10814 sections[seen_linkorder++] = p;
10815 }
10816 /* Sort the input sections in the order of their linked section. */
10817 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10818 compare_link_order);
10819
10820 /* Change the offsets of the sections. */
10821 offset = 0;
10822 for (n = 0; n < seen_linkorder; n++)
10823 {
10824 s = sections[n]->u.indirect.section;
461686a3 10825 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10826 s->output_offset = offset;
10827 sections[n]->offset = offset;
5dabe785 10828 /* FIXME: octets_per_byte. */
0b52efa6
PB
10829 offset += sections[n]->size;
10830 }
10831
4dd07732 10832 free (sections);
0b52efa6
PB
10833 return TRUE;
10834}
10835
9f7c3e5e
AM
10836static void
10837elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10838{
10839 asection *o;
10840
10841 if (flinfo->symstrtab != NULL)
ef10c3ac 10842 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10843 if (flinfo->contents != NULL)
10844 free (flinfo->contents);
10845 if (flinfo->external_relocs != NULL)
10846 free (flinfo->external_relocs);
10847 if (flinfo->internal_relocs != NULL)
10848 free (flinfo->internal_relocs);
10849 if (flinfo->external_syms != NULL)
10850 free (flinfo->external_syms);
10851 if (flinfo->locsym_shndx != NULL)
10852 free (flinfo->locsym_shndx);
10853 if (flinfo->internal_syms != NULL)
10854 free (flinfo->internal_syms);
10855 if (flinfo->indices != NULL)
10856 free (flinfo->indices);
10857 if (flinfo->sections != NULL)
10858 free (flinfo->sections);
9f7c3e5e
AM
10859 if (flinfo->symshndxbuf != NULL)
10860 free (flinfo->symshndxbuf);
10861 for (o = obfd->sections; o != NULL; o = o->next)
10862 {
10863 struct bfd_elf_section_data *esdo = elf_section_data (o);
10864 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10865 free (esdo->rel.hashes);
10866 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10867 free (esdo->rela.hashes);
10868 }
10869}
0b52efa6 10870
c152c796
AM
10871/* Do the final step of an ELF link. */
10872
10873bfd_boolean
10874bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10875{
10876 bfd_boolean dynamic;
10877 bfd_boolean emit_relocs;
10878 bfd *dynobj;
8b127cbc 10879 struct elf_final_link_info flinfo;
91d6fa6a
NC
10880 asection *o;
10881 struct bfd_link_order *p;
10882 bfd *sub;
c152c796
AM
10883 bfd_size_type max_contents_size;
10884 bfd_size_type max_external_reloc_size;
10885 bfd_size_type max_internal_reloc_count;
10886 bfd_size_type max_sym_count;
10887 bfd_size_type max_sym_shndx_count;
c152c796
AM
10888 Elf_Internal_Sym elfsym;
10889 unsigned int i;
10890 Elf_Internal_Shdr *symtab_hdr;
10891 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10893 struct elf_outext_info eoinfo;
10894 bfd_boolean merged;
10895 size_t relativecount = 0;
10896 asection *reldyn = 0;
10897 bfd_size_type amt;
104d59d1
JM
10898 asection *attr_section = NULL;
10899 bfd_vma attr_size = 0;
10900 const char *std_attrs_section;
c152c796
AM
10901
10902 if (! is_elf_hash_table (info->hash))
10903 return FALSE;
10904
0e1862bb 10905 if (bfd_link_pic (info))
c152c796
AM
10906 abfd->flags |= DYNAMIC;
10907
10908 dynamic = elf_hash_table (info)->dynamic_sections_created;
10909 dynobj = elf_hash_table (info)->dynobj;
10910
0e1862bb 10911 emit_relocs = (bfd_link_relocatable (info)
a4676736 10912 || info->emitrelocations);
c152c796 10913
8b127cbc
AM
10914 flinfo.info = info;
10915 flinfo.output_bfd = abfd;
ef10c3ac 10916 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 10917 if (flinfo.symstrtab == NULL)
c152c796
AM
10918 return FALSE;
10919
10920 if (! dynamic)
10921 {
8b127cbc
AM
10922 flinfo.hash_sec = NULL;
10923 flinfo.symver_sec = NULL;
c152c796
AM
10924 }
10925 else
10926 {
3d4d4302 10927 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 10928 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 10929 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
10930 /* Note that it is OK if symver_sec is NULL. */
10931 }
10932
8b127cbc
AM
10933 flinfo.contents = NULL;
10934 flinfo.external_relocs = NULL;
10935 flinfo.internal_relocs = NULL;
10936 flinfo.external_syms = NULL;
10937 flinfo.locsym_shndx = NULL;
10938 flinfo.internal_syms = NULL;
10939 flinfo.indices = NULL;
10940 flinfo.sections = NULL;
8b127cbc 10941 flinfo.symshndxbuf = NULL;
ffbc01cc 10942 flinfo.filesym_count = 0;
c152c796 10943
104d59d1
JM
10944 /* The object attributes have been merged. Remove the input
10945 sections from the link, and set the contents of the output
10946 secton. */
10947 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10948 for (o = abfd->sections; o != NULL; o = o->next)
10949 {
10950 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10951 || strcmp (o->name, ".gnu.attributes") == 0)
10952 {
10953 for (p = o->map_head.link_order; p != NULL; p = p->next)
10954 {
10955 asection *input_section;
10956
10957 if (p->type != bfd_indirect_link_order)
10958 continue;
10959 input_section = p->u.indirect.section;
10960 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10961 elf_link_input_bfd ignores this section. */
10962 input_section->flags &= ~SEC_HAS_CONTENTS;
10963 }
a0c8462f 10964
104d59d1
JM
10965 attr_size = bfd_elf_obj_attr_size (abfd);
10966 if (attr_size)
10967 {
10968 bfd_set_section_size (abfd, o, attr_size);
10969 attr_section = o;
10970 /* Skip this section later on. */
10971 o->map_head.link_order = NULL;
10972 }
10973 else
10974 o->flags |= SEC_EXCLUDE;
10975 }
10976 }
10977
c152c796
AM
10978 /* Count up the number of relocations we will output for each output
10979 section, so that we know the sizes of the reloc sections. We
10980 also figure out some maximum sizes. */
10981 max_contents_size = 0;
10982 max_external_reloc_size = 0;
10983 max_internal_reloc_count = 0;
10984 max_sym_count = 0;
10985 max_sym_shndx_count = 0;
10986 merged = FALSE;
10987 for (o = abfd->sections; o != NULL; o = o->next)
10988 {
10989 struct bfd_elf_section_data *esdo = elf_section_data (o);
10990 o->reloc_count = 0;
10991
8423293d 10992 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10993 {
10994 unsigned int reloc_count = 0;
491d01d3 10995 unsigned int additional_reloc_count = 0;
c152c796 10996 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
10997
10998 if (p->type == bfd_section_reloc_link_order
10999 || p->type == bfd_symbol_reloc_link_order)
11000 reloc_count = 1;
11001 else if (p->type == bfd_indirect_link_order)
11002 {
11003 asection *sec;
11004
11005 sec = p->u.indirect.section;
11006 esdi = elf_section_data (sec);
11007
11008 /* Mark all sections which are to be included in the
11009 link. This will normally be every section. We need
11010 to do this so that we can identify any sections which
11011 the linker has decided to not include. */
11012 sec->linker_mark = TRUE;
11013
11014 if (sec->flags & SEC_MERGE)
11015 merged = TRUE;
11016
aed64b35
L
11017 if (esdo->this_hdr.sh_type == SHT_REL
11018 || esdo->this_hdr.sh_type == SHT_RELA)
11019 /* Some backends use reloc_count in relocation sections
11020 to count particular types of relocs. Of course,
11021 reloc sections themselves can't have relocations. */
11022 reloc_count = 0;
0e1862bb 11023 else if (emit_relocs)
491d01d3
YU
11024 {
11025 reloc_count = sec->reloc_count;
11026 if (bed->elf_backend_count_additional_relocs)
11027 {
11028 int c;
11029 c = (*bed->elf_backend_count_additional_relocs) (sec);
11030 additional_reloc_count += c;
11031 }
11032 }
c152c796 11033 else if (bed->elf_backend_count_relocs)
58217f29 11034 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11035
eea6121a
AM
11036 if (sec->rawsize > max_contents_size)
11037 max_contents_size = sec->rawsize;
11038 if (sec->size > max_contents_size)
11039 max_contents_size = sec->size;
c152c796
AM
11040
11041 /* We are interested in just local symbols, not all
11042 symbols. */
11043 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11044 && (sec->owner->flags & DYNAMIC) == 0)
11045 {
11046 size_t sym_count;
11047
11048 if (elf_bad_symtab (sec->owner))
11049 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11050 / bed->s->sizeof_sym);
11051 else
11052 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11053
11054 if (sym_count > max_sym_count)
11055 max_sym_count = sym_count;
11056
11057 if (sym_count > max_sym_shndx_count
6a40cf0c 11058 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11059 max_sym_shndx_count = sym_count;
11060
11061 if ((sec->flags & SEC_RELOC) != 0)
11062 {
d4730f92 11063 size_t ext_size = 0;
c152c796 11064
d4730f92
BS
11065 if (esdi->rel.hdr != NULL)
11066 ext_size = esdi->rel.hdr->sh_size;
11067 if (esdi->rela.hdr != NULL)
11068 ext_size += esdi->rela.hdr->sh_size;
7326c758 11069
c152c796
AM
11070 if (ext_size > max_external_reloc_size)
11071 max_external_reloc_size = ext_size;
11072 if (sec->reloc_count > max_internal_reloc_count)
11073 max_internal_reloc_count = sec->reloc_count;
11074 }
11075 }
11076 }
11077
11078 if (reloc_count == 0)
11079 continue;
11080
491d01d3 11081 reloc_count += additional_reloc_count;
c152c796
AM
11082 o->reloc_count += reloc_count;
11083
0e1862bb 11084 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11085 {
d4730f92 11086 if (esdi->rel.hdr)
491d01d3
YU
11087 {
11088 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11089 esdo->rel.count += additional_reloc_count;
11090 }
d4730f92 11091 if (esdi->rela.hdr)
491d01d3
YU
11092 {
11093 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11094 esdo->rela.count += additional_reloc_count;
11095 }
d4730f92
BS
11096 }
11097 else
11098 {
11099 if (o->use_rela_p)
11100 esdo->rela.count += reloc_count;
2c2b4ed4 11101 else
d4730f92 11102 esdo->rel.count += reloc_count;
c152c796 11103 }
c152c796
AM
11104 }
11105
11106 if (o->reloc_count > 0)
11107 o->flags |= SEC_RELOC;
11108 else
11109 {
11110 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11111 set it (this is probably a bug) and if it is set
11112 assign_section_numbers will create a reloc section. */
11113 o->flags &=~ SEC_RELOC;
11114 }
11115
11116 /* If the SEC_ALLOC flag is not set, force the section VMA to
11117 zero. This is done in elf_fake_sections as well, but forcing
11118 the VMA to 0 here will ensure that relocs against these
11119 sections are handled correctly. */
11120 if ((o->flags & SEC_ALLOC) == 0
11121 && ! o->user_set_vma)
11122 o->vma = 0;
11123 }
11124
0e1862bb 11125 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11126 elf_link_hash_traverse (elf_hash_table (info),
11127 _bfd_elf_link_sec_merge_syms, abfd);
11128
11129 /* Figure out the file positions for everything but the symbol table
11130 and the relocs. We set symcount to force assign_section_numbers
11131 to create a symbol table. */
8539e4e8 11132 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11133 BFD_ASSERT (! abfd->output_has_begun);
11134 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11135 goto error_return;
11136
ee75fd95 11137 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11138 for (o = abfd->sections; o != NULL; o = o->next)
11139 {
d4730f92 11140 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11141 if ((o->flags & SEC_RELOC) != 0)
11142 {
d4730f92
BS
11143 if (esdo->rel.hdr
11144 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11145 goto error_return;
11146
d4730f92
BS
11147 if (esdo->rela.hdr
11148 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11149 goto error_return;
11150 }
11151
11152 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11153 to count upwards while actually outputting the relocations. */
d4730f92
BS
11154 esdo->rel.count = 0;
11155 esdo->rela.count = 0;
0ce398f1
L
11156
11157 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11158 {
11159 /* Cache the section contents so that they can be compressed
11160 later. Use bfd_malloc since it will be freed by
11161 bfd_compress_section_contents. */
11162 unsigned char *contents = esdo->this_hdr.contents;
11163 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11164 abort ();
11165 contents
11166 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11167 if (contents == NULL)
11168 goto error_return;
11169 esdo->this_hdr.contents = contents;
11170 }
c152c796
AM
11171 }
11172
c152c796 11173 /* We have now assigned file positions for all the sections except
a485e98e
AM
11174 .symtab, .strtab, and non-loaded reloc sections. We start the
11175 .symtab section at the current file position, and write directly
11176 to it. We build the .strtab section in memory. */
c152c796
AM
11177 bfd_get_symcount (abfd) = 0;
11178 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11179 /* sh_name is set in prep_headers. */
11180 symtab_hdr->sh_type = SHT_SYMTAB;
11181 /* sh_flags, sh_addr and sh_size all start off zero. */
11182 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11183 /* sh_link is set in assign_section_numbers. */
11184 /* sh_info is set below. */
11185 /* sh_offset is set just below. */
72de5009 11186 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11187
ef10c3ac
L
11188 if (max_sym_count < 20)
11189 max_sym_count = 20;
11190 elf_hash_table (info)->strtabsize = max_sym_count;
11191 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11192 elf_hash_table (info)->strtab
11193 = (struct elf_sym_strtab *) bfd_malloc (amt);
11194 if (elf_hash_table (info)->strtab == NULL)
c152c796 11195 goto error_return;
ef10c3ac
L
11196 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11197 flinfo.symshndxbuf
11198 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11199 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11200
8539e4e8 11201 if (info->strip != strip_all || emit_relocs)
c152c796 11202 {
8539e4e8
AM
11203 file_ptr off = elf_next_file_pos (abfd);
11204
11205 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11206
11207 /* Note that at this point elf_next_file_pos (abfd) is
11208 incorrect. We do not yet know the size of the .symtab section.
11209 We correct next_file_pos below, after we do know the size. */
11210
11211 /* Start writing out the symbol table. The first symbol is always a
11212 dummy symbol. */
c152c796
AM
11213 elfsym.st_value = 0;
11214 elfsym.st_size = 0;
11215 elfsym.st_info = 0;
11216 elfsym.st_other = 0;
11217 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11218 elfsym.st_target_internal = 0;
ef10c3ac
L
11219 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11220 bfd_und_section_ptr, NULL) != 1)
c152c796 11221 goto error_return;
c152c796 11222
8539e4e8
AM
11223 /* Output a symbol for each section. We output these even if we are
11224 discarding local symbols, since they are used for relocs. These
11225 symbols have no names. We store the index of each one in the
11226 index field of the section, so that we can find it again when
11227 outputting relocs. */
11228
c152c796
AM
11229 elfsym.st_size = 0;
11230 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11231 elfsym.st_other = 0;
f0b5bb34 11232 elfsym.st_value = 0;
35fc36a8 11233 elfsym.st_target_internal = 0;
c152c796
AM
11234 for (i = 1; i < elf_numsections (abfd); i++)
11235 {
11236 o = bfd_section_from_elf_index (abfd, i);
11237 if (o != NULL)
f0b5bb34
AM
11238 {
11239 o->target_index = bfd_get_symcount (abfd);
11240 elfsym.st_shndx = i;
0e1862bb 11241 if (!bfd_link_relocatable (info))
f0b5bb34 11242 elfsym.st_value = o->vma;
ef10c3ac
L
11243 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11244 NULL) != 1)
f0b5bb34
AM
11245 goto error_return;
11246 }
c152c796
AM
11247 }
11248 }
11249
11250 /* Allocate some memory to hold information read in from the input
11251 files. */
11252 if (max_contents_size != 0)
11253 {
8b127cbc
AM
11254 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11255 if (flinfo.contents == NULL)
c152c796
AM
11256 goto error_return;
11257 }
11258
11259 if (max_external_reloc_size != 0)
11260 {
8b127cbc
AM
11261 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11262 if (flinfo.external_relocs == NULL)
c152c796
AM
11263 goto error_return;
11264 }
11265
11266 if (max_internal_reloc_count != 0)
11267 {
11268 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11269 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11270 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11271 if (flinfo.internal_relocs == NULL)
c152c796
AM
11272 goto error_return;
11273 }
11274
11275 if (max_sym_count != 0)
11276 {
11277 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11278 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11279 if (flinfo.external_syms == NULL)
c152c796
AM
11280 goto error_return;
11281
11282 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11283 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11284 if (flinfo.internal_syms == NULL)
c152c796
AM
11285 goto error_return;
11286
11287 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11288 flinfo.indices = (long int *) bfd_malloc (amt);
11289 if (flinfo.indices == NULL)
c152c796
AM
11290 goto error_return;
11291
11292 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11293 flinfo.sections = (asection **) bfd_malloc (amt);
11294 if (flinfo.sections == NULL)
c152c796
AM
11295 goto error_return;
11296 }
11297
11298 if (max_sym_shndx_count != 0)
11299 {
11300 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11301 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11302 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11303 goto error_return;
11304 }
11305
11306 if (elf_hash_table (info)->tls_sec)
11307 {
11308 bfd_vma base, end = 0;
11309 asection *sec;
11310
11311 for (sec = elf_hash_table (info)->tls_sec;
11312 sec && (sec->flags & SEC_THREAD_LOCAL);
11313 sec = sec->next)
11314 {
3a800eb9 11315 bfd_size_type size = sec->size;
c152c796 11316
3a800eb9
AM
11317 if (size == 0
11318 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11319 {
91d6fa6a
NC
11320 struct bfd_link_order *ord = sec->map_tail.link_order;
11321
11322 if (ord != NULL)
11323 size = ord->offset + ord->size;
c152c796
AM
11324 }
11325 end = sec->vma + size;
11326 }
11327 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11328 /* Only align end of TLS section if static TLS doesn't have special
11329 alignment requirements. */
11330 if (bed->static_tls_alignment == 1)
11331 end = align_power (end,
11332 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11333 elf_hash_table (info)->tls_size = end - base;
11334 }
11335
0b52efa6
PB
11336 /* Reorder SHF_LINK_ORDER sections. */
11337 for (o = abfd->sections; o != NULL; o = o->next)
11338 {
11339 if (!elf_fixup_link_order (abfd, o))
11340 return FALSE;
11341 }
11342
2f0c68f2
CM
11343 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11344 return FALSE;
11345
c152c796
AM
11346 /* Since ELF permits relocations to be against local symbols, we
11347 must have the local symbols available when we do the relocations.
11348 Since we would rather only read the local symbols once, and we
11349 would rather not keep them in memory, we handle all the
11350 relocations for a single input file at the same time.
11351
11352 Unfortunately, there is no way to know the total number of local
11353 symbols until we have seen all of them, and the local symbol
11354 indices precede the global symbol indices. This means that when
11355 we are generating relocatable output, and we see a reloc against
11356 a global symbol, we can not know the symbol index until we have
11357 finished examining all the local symbols to see which ones we are
11358 going to output. To deal with this, we keep the relocations in
11359 memory, and don't output them until the end of the link. This is
11360 an unfortunate waste of memory, but I don't see a good way around
11361 it. Fortunately, it only happens when performing a relocatable
11362 link, which is not the common case. FIXME: If keep_memory is set
11363 we could write the relocs out and then read them again; I don't
11364 know how bad the memory loss will be. */
11365
c72f2fb2 11366 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11367 sub->output_has_begun = FALSE;
11368 for (o = abfd->sections; o != NULL; o = o->next)
11369 {
8423293d 11370 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11371 {
11372 if (p->type == bfd_indirect_link_order
11373 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11374 == bfd_target_elf_flavour)
11375 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11376 {
11377 if (! sub->output_has_begun)
11378 {
8b127cbc 11379 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11380 goto error_return;
11381 sub->output_has_begun = TRUE;
11382 }
11383 }
11384 else if (p->type == bfd_section_reloc_link_order
11385 || p->type == bfd_symbol_reloc_link_order)
11386 {
11387 if (! elf_reloc_link_order (abfd, info, o, p))
11388 goto error_return;
11389 }
11390 else
11391 {
11392 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11393 {
11394 if (p->type == bfd_indirect_link_order
11395 && (bfd_get_flavour (sub)
11396 == bfd_target_elf_flavour)
11397 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11398 != bed->s->elfclass))
11399 {
11400 const char *iclass, *oclass;
11401
aebf9be7 11402 switch (bed->s->elfclass)
351f65ca 11403 {
aebf9be7
NC
11404 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11405 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11406 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11407 default: abort ();
351f65ca 11408 }
aebf9be7
NC
11409
11410 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11411 {
aebf9be7
NC
11412 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11413 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11414 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11415 default: abort ();
351f65ca
L
11416 }
11417
11418 bfd_set_error (bfd_error_wrong_format);
11419 (*_bfd_error_handler)
11420 (_("%B: file class %s incompatible with %s"),
11421 sub, iclass, oclass);
11422 }
11423
11424 goto error_return;
11425 }
c152c796
AM
11426 }
11427 }
11428 }
11429
c0f00686
L
11430 /* Free symbol buffer if needed. */
11431 if (!info->reduce_memory_overheads)
11432 {
c72f2fb2 11433 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11434 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11435 && elf_tdata (sub)->symbuf)
c0f00686
L
11436 {
11437 free (elf_tdata (sub)->symbuf);
11438 elf_tdata (sub)->symbuf = NULL;
11439 }
11440 }
11441
c152c796
AM
11442 /* Output any global symbols that got converted to local in a
11443 version script or due to symbol visibility. We do this in a
11444 separate step since ELF requires all local symbols to appear
11445 prior to any global symbols. FIXME: We should only do this if
11446 some global symbols were, in fact, converted to become local.
11447 FIXME: Will this work correctly with the Irix 5 linker? */
11448 eoinfo.failed = FALSE;
8b127cbc 11449 eoinfo.flinfo = &flinfo;
c152c796 11450 eoinfo.localsyms = TRUE;
34a79995 11451 eoinfo.file_sym_done = FALSE;
7686d77d 11452 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11453 if (eoinfo.failed)
11454 return FALSE;
11455
4e617b1e
PB
11456 /* If backend needs to output some local symbols not present in the hash
11457 table, do it now. */
8539e4e8
AM
11458 if (bed->elf_backend_output_arch_local_syms
11459 && (info->strip != strip_all || emit_relocs))
4e617b1e 11460 {
6e0b88f1 11461 typedef int (*out_sym_func)
4e617b1e
PB
11462 (void *, const char *, Elf_Internal_Sym *, asection *,
11463 struct elf_link_hash_entry *);
11464
11465 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11466 (abfd, info, &flinfo,
11467 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11468 return FALSE;
11469 }
11470
c152c796
AM
11471 /* That wrote out all the local symbols. Finish up the symbol table
11472 with the global symbols. Even if we want to strip everything we
11473 can, we still need to deal with those global symbols that got
11474 converted to local in a version script. */
11475
11476 /* The sh_info field records the index of the first non local symbol. */
11477 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11478
11479 if (dynamic
cae1fbbb
L
11480 && elf_hash_table (info)->dynsym != NULL
11481 && (elf_hash_table (info)->dynsym->output_section
11482 != bfd_abs_section_ptr))
c152c796
AM
11483 {
11484 Elf_Internal_Sym sym;
cae1fbbb 11485 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11486 long last_local = 0;
11487
11488 /* Write out the section symbols for the output sections. */
0e1862bb
L
11489 if (bfd_link_pic (info)
11490 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11491 {
11492 asection *s;
11493
11494 sym.st_size = 0;
11495 sym.st_name = 0;
11496 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11497 sym.st_other = 0;
35fc36a8 11498 sym.st_target_internal = 0;
c152c796
AM
11499
11500 for (s = abfd->sections; s != NULL; s = s->next)
11501 {
11502 int indx;
11503 bfd_byte *dest;
11504 long dynindx;
11505
c152c796 11506 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11507 if (dynindx <= 0)
11508 continue;
11509 indx = elf_section_data (s)->this_idx;
c152c796
AM
11510 BFD_ASSERT (indx > 0);
11511 sym.st_shndx = indx;
c0d5a53d
L
11512 if (! check_dynsym (abfd, &sym))
11513 return FALSE;
c152c796
AM
11514 sym.st_value = s->vma;
11515 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11516 if (last_local < dynindx)
11517 last_local = dynindx;
c152c796
AM
11518 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11519 }
c152c796
AM
11520 }
11521
11522 /* Write out the local dynsyms. */
11523 if (elf_hash_table (info)->dynlocal)
11524 {
11525 struct elf_link_local_dynamic_entry *e;
11526 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11527 {
11528 asection *s;
11529 bfd_byte *dest;
11530
935bd1e0 11531 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11532 Note that we saved a word of storage and overwrote
11533 the original st_name with the dynstr_index. */
11534 sym = e->isym;
935bd1e0 11535 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11536
cb33740c
AM
11537 s = bfd_section_from_elf_index (e->input_bfd,
11538 e->isym.st_shndx);
11539 if (s != NULL)
c152c796 11540 {
c152c796
AM
11541 sym.st_shndx =
11542 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11543 if (! check_dynsym (abfd, &sym))
11544 return FALSE;
c152c796
AM
11545 sym.st_value = (s->output_section->vma
11546 + s->output_offset
11547 + e->isym.st_value);
11548 }
11549
11550 if (last_local < e->dynindx)
11551 last_local = e->dynindx;
11552
11553 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11554 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11555 }
11556 }
11557
cae1fbbb 11558 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11559 last_local + 1;
11560 }
11561
11562 /* We get the global symbols from the hash table. */
11563 eoinfo.failed = FALSE;
11564 eoinfo.localsyms = FALSE;
8b127cbc 11565 eoinfo.flinfo = &flinfo;
7686d77d 11566 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11567 if (eoinfo.failed)
11568 return FALSE;
11569
11570 /* If backend needs to output some symbols not present in the hash
11571 table, do it now. */
8539e4e8
AM
11572 if (bed->elf_backend_output_arch_syms
11573 && (info->strip != strip_all || emit_relocs))
c152c796 11574 {
6e0b88f1 11575 typedef int (*out_sym_func)
c152c796
AM
11576 (void *, const char *, Elf_Internal_Sym *, asection *,
11577 struct elf_link_hash_entry *);
11578
11579 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11580 (abfd, info, &flinfo,
11581 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11582 return FALSE;
11583 }
11584
ef10c3ac
L
11585 /* Finalize the .strtab section. */
11586 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11587
11588 /* Swap out the .strtab section. */
11589 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11590 return FALSE;
11591
11592 /* Now we know the size of the symtab section. */
c152c796
AM
11593 if (bfd_get_symcount (abfd) > 0)
11594 {
ee3b52e9
L
11595 /* Finish up and write out the symbol string table (.strtab)
11596 section. */
11597 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11598 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11599
6a40cf0c
NC
11600 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11601 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11602 {
11603 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11604 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11605 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11606 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11607 symtab_shndx_hdr->sh_size = amt;
11608
11609 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11610 off, TRUE);
11611
11612 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11613 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11614 return FALSE;
11615 }
ee3b52e9
L
11616
11617 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11618 /* sh_name was set in prep_headers. */
11619 symstrtab_hdr->sh_type = SHT_STRTAB;
11620 symstrtab_hdr->sh_flags = 0;
11621 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11622 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11623 symstrtab_hdr->sh_entsize = 0;
11624 symstrtab_hdr->sh_link = 0;
11625 symstrtab_hdr->sh_info = 0;
11626 /* sh_offset is set just below. */
11627 symstrtab_hdr->sh_addralign = 1;
11628
11629 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11630 off, TRUE);
11631 elf_next_file_pos (abfd) = off;
11632
c152c796 11633 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11634 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11635 return FALSE;
11636 }
11637
11638 /* Adjust the relocs to have the correct symbol indices. */
11639 for (o = abfd->sections; o != NULL; o = o->next)
11640 {
d4730f92 11641 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11642 bfd_boolean sort;
c152c796
AM
11643 if ((o->flags & SEC_RELOC) == 0)
11644 continue;
11645
28dbcedc 11646 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11647 if (esdo->rel.hdr != NULL
11648 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11649 return FALSE;
11650 if (esdo->rela.hdr != NULL
11651 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11652 return FALSE;
c152c796
AM
11653
11654 /* Set the reloc_count field to 0 to prevent write_relocs from
11655 trying to swap the relocs out itself. */
11656 o->reloc_count = 0;
11657 }
11658
11659 if (dynamic && info->combreloc && dynobj != NULL)
11660 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11661
11662 /* If we are linking against a dynamic object, or generating a
11663 shared library, finish up the dynamic linking information. */
11664 if (dynamic)
11665 {
11666 bfd_byte *dyncon, *dynconend;
11667
11668 /* Fix up .dynamic entries. */
3d4d4302 11669 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11670 BFD_ASSERT (o != NULL);
11671
11672 dyncon = o->contents;
eea6121a 11673 dynconend = o->contents + o->size;
c152c796
AM
11674 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11675 {
11676 Elf_Internal_Dyn dyn;
11677 const char *name;
11678 unsigned int type;
11679
11680 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11681
11682 switch (dyn.d_tag)
11683 {
11684 default:
11685 continue;
11686 case DT_NULL:
11687 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11688 {
11689 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11690 {
11691 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11692 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11693 default: continue;
11694 }
11695 dyn.d_un.d_val = relativecount;
11696 relativecount = 0;
11697 break;
11698 }
11699 continue;
11700
11701 case DT_INIT:
11702 name = info->init_function;
11703 goto get_sym;
11704 case DT_FINI:
11705 name = info->fini_function;
11706 get_sym:
11707 {
11708 struct elf_link_hash_entry *h;
11709
11710 h = elf_link_hash_lookup (elf_hash_table (info), name,
11711 FALSE, FALSE, TRUE);
11712 if (h != NULL
11713 && (h->root.type == bfd_link_hash_defined
11714 || h->root.type == bfd_link_hash_defweak))
11715 {
bef26483 11716 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11717 o = h->root.u.def.section;
11718 if (o->output_section != NULL)
bef26483 11719 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11720 + o->output_offset);
11721 else
11722 {
11723 /* The symbol is imported from another shared
11724 library and does not apply to this one. */
bef26483 11725 dyn.d_un.d_ptr = 0;
c152c796
AM
11726 }
11727 break;
11728 }
11729 }
11730 continue;
11731
11732 case DT_PREINIT_ARRAYSZ:
11733 name = ".preinit_array";
11734 goto get_size;
11735 case DT_INIT_ARRAYSZ:
11736 name = ".init_array";
11737 goto get_size;
11738 case DT_FINI_ARRAYSZ:
11739 name = ".fini_array";
11740 get_size:
11741 o = bfd_get_section_by_name (abfd, name);
11742 if (o == NULL)
11743 {
11744 (*_bfd_error_handler)
d003868e 11745 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11746 goto error_return;
11747 }
eea6121a 11748 if (o->size == 0)
c152c796
AM
11749 (*_bfd_error_handler)
11750 (_("warning: %s section has zero size"), name);
eea6121a 11751 dyn.d_un.d_val = o->size;
c152c796
AM
11752 break;
11753
11754 case DT_PREINIT_ARRAY:
11755 name = ".preinit_array";
11756 goto get_vma;
11757 case DT_INIT_ARRAY:
11758 name = ".init_array";
11759 goto get_vma;
11760 case DT_FINI_ARRAY:
11761 name = ".fini_array";
11762 goto get_vma;
11763
11764 case DT_HASH:
11765 name = ".hash";
11766 goto get_vma;
fdc90cb4
JJ
11767 case DT_GNU_HASH:
11768 name = ".gnu.hash";
11769 goto get_vma;
c152c796
AM
11770 case DT_STRTAB:
11771 name = ".dynstr";
11772 goto get_vma;
11773 case DT_SYMTAB:
11774 name = ".dynsym";
11775 goto get_vma;
11776 case DT_VERDEF:
11777 name = ".gnu.version_d";
11778 goto get_vma;
11779 case DT_VERNEED:
11780 name = ".gnu.version_r";
11781 goto get_vma;
11782 case DT_VERSYM:
11783 name = ".gnu.version";
11784 get_vma:
11785 o = bfd_get_section_by_name (abfd, name);
11786 if (o == NULL)
11787 {
11788 (*_bfd_error_handler)
d003868e 11789 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11790 goto error_return;
11791 }
894891db
NC
11792 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11793 {
11794 (*_bfd_error_handler)
11795 (_("warning: section '%s' is being made into a note"), name);
11796 bfd_set_error (bfd_error_nonrepresentable_section);
11797 goto error_return;
11798 }
c152c796
AM
11799 dyn.d_un.d_ptr = o->vma;
11800 break;
11801
11802 case DT_REL:
11803 case DT_RELA:
11804 case DT_RELSZ:
11805 case DT_RELASZ:
11806 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11807 type = SHT_REL;
11808 else
11809 type = SHT_RELA;
11810 dyn.d_un.d_val = 0;
bef26483 11811 dyn.d_un.d_ptr = 0;
c152c796
AM
11812 for (i = 1; i < elf_numsections (abfd); i++)
11813 {
11814 Elf_Internal_Shdr *hdr;
11815
11816 hdr = elf_elfsections (abfd)[i];
11817 if (hdr->sh_type == type
11818 && (hdr->sh_flags & SHF_ALLOC) != 0)
11819 {
11820 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11821 dyn.d_un.d_val += hdr->sh_size;
11822 else
11823 {
bef26483
AM
11824 if (dyn.d_un.d_ptr == 0
11825 || hdr->sh_addr < dyn.d_un.d_ptr)
11826 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11827 }
11828 }
11829 }
11830 break;
11831 }
11832 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11833 }
11834 }
11835
11836 /* If we have created any dynamic sections, then output them. */
11837 if (dynobj != NULL)
11838 {
11839 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11840 goto error_return;
11841
943284cc 11842 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11843 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11844 || info->error_textrel)
3d4d4302 11845 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11846 {
11847 bfd_byte *dyncon, *dynconend;
11848
943284cc
DJ
11849 dyncon = o->contents;
11850 dynconend = o->contents + o->size;
11851 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11852 {
11853 Elf_Internal_Dyn dyn;
11854
11855 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11856
11857 if (dyn.d_tag == DT_TEXTREL)
11858 {
c192a133
AM
11859 if (info->error_textrel)
11860 info->callbacks->einfo
11861 (_("%P%X: read-only segment has dynamic relocations.\n"));
11862 else
11863 info->callbacks->einfo
11864 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11865 break;
11866 }
11867 }
11868 }
11869
c152c796
AM
11870 for (o = dynobj->sections; o != NULL; o = o->next)
11871 {
11872 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11873 || o->size == 0
c152c796
AM
11874 || o->output_section == bfd_abs_section_ptr)
11875 continue;
11876 if ((o->flags & SEC_LINKER_CREATED) == 0)
11877 {
11878 /* At this point, we are only interested in sections
11879 created by _bfd_elf_link_create_dynamic_sections. */
11880 continue;
11881 }
3722b82f
AM
11882 if (elf_hash_table (info)->stab_info.stabstr == o)
11883 continue;
eea6121a
AM
11884 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11885 continue;
3d4d4302 11886 if (strcmp (o->name, ".dynstr") != 0)
c152c796 11887 {
5dabe785 11888 /* FIXME: octets_per_byte. */
c152c796
AM
11889 if (! bfd_set_section_contents (abfd, o->output_section,
11890 o->contents,
11891 (file_ptr) o->output_offset,
eea6121a 11892 o->size))
c152c796
AM
11893 goto error_return;
11894 }
11895 else
11896 {
11897 /* The contents of the .dynstr section are actually in a
11898 stringtab. */
8539e4e8
AM
11899 file_ptr off;
11900
c152c796
AM
11901 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11902 if (bfd_seek (abfd, off, SEEK_SET) != 0
11903 || ! _bfd_elf_strtab_emit (abfd,
11904 elf_hash_table (info)->dynstr))
11905 goto error_return;
11906 }
11907 }
11908 }
11909
0e1862bb 11910 if (bfd_link_relocatable (info))
c152c796
AM
11911 {
11912 bfd_boolean failed = FALSE;
11913
11914 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11915 if (failed)
11916 goto error_return;
11917 }
11918
11919 /* If we have optimized stabs strings, output them. */
3722b82f 11920 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11921 {
11922 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11923 goto error_return;
11924 }
11925
9f7c3e5e
AM
11926 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11927 goto error_return;
c152c796 11928
9f7c3e5e 11929 elf_final_link_free (abfd, &flinfo);
c152c796 11930
12bd6957 11931 elf_linker (abfd) = TRUE;
c152c796 11932
104d59d1
JM
11933 if (attr_section)
11934 {
a50b1753 11935 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 11936 if (contents == NULL)
d0f16d5e 11937 return FALSE; /* Bail out and fail. */
104d59d1
JM
11938 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11939 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11940 free (contents);
11941 }
11942
c152c796
AM
11943 return TRUE;
11944
11945 error_return:
9f7c3e5e 11946 elf_final_link_free (abfd, &flinfo);
c152c796
AM
11947 return FALSE;
11948}
11949\f
5241d853
RS
11950/* Initialize COOKIE for input bfd ABFD. */
11951
11952static bfd_boolean
11953init_reloc_cookie (struct elf_reloc_cookie *cookie,
11954 struct bfd_link_info *info, bfd *abfd)
11955{
11956 Elf_Internal_Shdr *symtab_hdr;
11957 const struct elf_backend_data *bed;
11958
11959 bed = get_elf_backend_data (abfd);
11960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11961
11962 cookie->abfd = abfd;
11963 cookie->sym_hashes = elf_sym_hashes (abfd);
11964 cookie->bad_symtab = elf_bad_symtab (abfd);
11965 if (cookie->bad_symtab)
11966 {
11967 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11968 cookie->extsymoff = 0;
11969 }
11970 else
11971 {
11972 cookie->locsymcount = symtab_hdr->sh_info;
11973 cookie->extsymoff = symtab_hdr->sh_info;
11974 }
11975
11976 if (bed->s->arch_size == 32)
11977 cookie->r_sym_shift = 8;
11978 else
11979 cookie->r_sym_shift = 32;
11980
11981 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11982 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11983 {
11984 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11985 cookie->locsymcount, 0,
11986 NULL, NULL, NULL);
11987 if (cookie->locsyms == NULL)
11988 {
11989 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11990 return FALSE;
11991 }
11992 if (info->keep_memory)
11993 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11994 }
11995 return TRUE;
11996}
11997
11998/* Free the memory allocated by init_reloc_cookie, if appropriate. */
11999
12000static void
12001fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12002{
12003 Elf_Internal_Shdr *symtab_hdr;
12004
12005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12006 if (cookie->locsyms != NULL
12007 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12008 free (cookie->locsyms);
12009}
12010
12011/* Initialize the relocation information in COOKIE for input section SEC
12012 of input bfd ABFD. */
12013
12014static bfd_boolean
12015init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12016 struct bfd_link_info *info, bfd *abfd,
12017 asection *sec)
12018{
12019 const struct elf_backend_data *bed;
12020
12021 if (sec->reloc_count == 0)
12022 {
12023 cookie->rels = NULL;
12024 cookie->relend = NULL;
12025 }
12026 else
12027 {
12028 bed = get_elf_backend_data (abfd);
12029
12030 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12031 info->keep_memory);
12032 if (cookie->rels == NULL)
12033 return FALSE;
12034 cookie->rel = cookie->rels;
12035 cookie->relend = (cookie->rels
12036 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12037 }
12038 cookie->rel = cookie->rels;
12039 return TRUE;
12040}
12041
12042/* Free the memory allocated by init_reloc_cookie_rels,
12043 if appropriate. */
12044
12045static void
12046fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12047 asection *sec)
12048{
12049 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12050 free (cookie->rels);
12051}
12052
12053/* Initialize the whole of COOKIE for input section SEC. */
12054
12055static bfd_boolean
12056init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12057 struct bfd_link_info *info,
12058 asection *sec)
12059{
12060 if (!init_reloc_cookie (cookie, info, sec->owner))
12061 goto error1;
12062 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12063 goto error2;
12064 return TRUE;
12065
12066 error2:
12067 fini_reloc_cookie (cookie, sec->owner);
12068 error1:
12069 return FALSE;
12070}
12071
12072/* Free the memory allocated by init_reloc_cookie_for_section,
12073 if appropriate. */
12074
12075static void
12076fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12077 asection *sec)
12078{
12079 fini_reloc_cookie_rels (cookie, sec);
12080 fini_reloc_cookie (cookie, sec->owner);
12081}
12082\f
c152c796
AM
12083/* Garbage collect unused sections. */
12084
07adf181
AM
12085/* Default gc_mark_hook. */
12086
12087asection *
12088_bfd_elf_gc_mark_hook (asection *sec,
12089 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12090 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12091 struct elf_link_hash_entry *h,
12092 Elf_Internal_Sym *sym)
12093{
12094 if (h != NULL)
12095 {
12096 switch (h->root.type)
12097 {
12098 case bfd_link_hash_defined:
12099 case bfd_link_hash_defweak:
12100 return h->root.u.def.section;
12101
12102 case bfd_link_hash_common:
12103 return h->root.u.c.p->section;
12104
12105 default:
12106 break;
12107 }
12108 }
12109 else
12110 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12111
12112 return NULL;
12113}
12114
5241d853
RS
12115/* COOKIE->rel describes a relocation against section SEC, which is
12116 a section we've decided to keep. Return the section that contains
12117 the relocation symbol, or NULL if no section contains it. */
12118
12119asection *
12120_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12121 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12122 struct elf_reloc_cookie *cookie,
12123 bfd_boolean *start_stop)
5241d853
RS
12124{
12125 unsigned long r_symndx;
12126 struct elf_link_hash_entry *h;
12127
12128 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12129 if (r_symndx == STN_UNDEF)
5241d853
RS
12130 return NULL;
12131
12132 if (r_symndx >= cookie->locsymcount
12133 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12134 {
12135 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12136 if (h == NULL)
12137 {
12138 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12139 sec->owner);
12140 return NULL;
12141 }
5241d853
RS
12142 while (h->root.type == bfd_link_hash_indirect
12143 || h->root.type == bfd_link_hash_warning)
12144 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12145 h->mark = 1;
4e6b54a6
AM
12146 /* If this symbol is weak and there is a non-weak definition, we
12147 keep the non-weak definition because many backends put
12148 dynamic reloc info on the non-weak definition for code
12149 handling copy relocs. */
12150 if (h->u.weakdef != NULL)
12151 h->u.weakdef->mark = 1;
1cce69b9
AM
12152
12153 if (start_stop != NULL
12154 && (h->root.type == bfd_link_hash_undefined
12155 || h->root.type == bfd_link_hash_undefweak))
12156 {
12157 /* To work around a glibc bug, mark all XXX input sections
12158 when there is an as yet undefined reference to __start_XXX
12159 or __stop_XXX symbols. The linker will later define such
12160 symbols for orphan input sections that have a name
12161 representable as a C identifier. */
12162 const char *sec_name = NULL;
12163 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12164 sec_name = h->root.root.string + 8;
12165 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12166 sec_name = h->root.root.string + 7;
12167
12168 if (sec_name != NULL && *sec_name != '\0')
12169 {
12170 bfd *i;
12171
12172 for (i = info->input_bfds; i != NULL; i = i->link.next)
12173 {
12174 asection *s = bfd_get_section_by_name (i, sec_name);
12175 if (s != NULL && !s->gc_mark)
12176 {
12177 *start_stop = TRUE;
12178 return s;
12179 }
12180 }
12181 }
12182 }
12183
5241d853
RS
12184 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12185 }
12186
12187 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12188 &cookie->locsyms[r_symndx]);
12189}
12190
12191/* COOKIE->rel describes a relocation against section SEC, which is
12192 a section we've decided to keep. Mark the section that contains
9d0a14d3 12193 the relocation symbol. */
5241d853
RS
12194
12195bfd_boolean
12196_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12197 asection *sec,
12198 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12199 struct elf_reloc_cookie *cookie)
5241d853
RS
12200{
12201 asection *rsec;
1cce69b9 12202 bfd_boolean start_stop = FALSE;
5241d853 12203
1cce69b9
AM
12204 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12205 while (rsec != NULL)
5241d853 12206 {
1cce69b9
AM
12207 if (!rsec->gc_mark)
12208 {
12209 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12210 || (rsec->owner->flags & DYNAMIC) != 0)
12211 rsec->gc_mark = 1;
12212 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12213 return FALSE;
12214 }
12215 if (!start_stop)
12216 break;
199af150 12217 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12218 }
12219 return TRUE;
12220}
12221
07adf181
AM
12222/* The mark phase of garbage collection. For a given section, mark
12223 it and any sections in this section's group, and all the sections
12224 which define symbols to which it refers. */
12225
ccfa59ea
AM
12226bfd_boolean
12227_bfd_elf_gc_mark (struct bfd_link_info *info,
12228 asection *sec,
6a5bb875 12229 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12230{
12231 bfd_boolean ret;
9d0a14d3 12232 asection *group_sec, *eh_frame;
c152c796
AM
12233
12234 sec->gc_mark = 1;
12235
12236 /* Mark all the sections in the group. */
12237 group_sec = elf_section_data (sec)->next_in_group;
12238 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12239 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12240 return FALSE;
12241
12242 /* Look through the section relocs. */
12243 ret = TRUE;
9d0a14d3
RS
12244 eh_frame = elf_eh_frame_section (sec->owner);
12245 if ((sec->flags & SEC_RELOC) != 0
12246 && sec->reloc_count > 0
12247 && sec != eh_frame)
c152c796 12248 {
5241d853 12249 struct elf_reloc_cookie cookie;
c152c796 12250
5241d853
RS
12251 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12252 ret = FALSE;
c152c796 12253 else
c152c796 12254 {
5241d853 12255 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12256 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12257 {
12258 ret = FALSE;
12259 break;
12260 }
12261 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12262 }
12263 }
9d0a14d3
RS
12264
12265 if (ret && eh_frame && elf_fde_list (sec))
12266 {
12267 struct elf_reloc_cookie cookie;
12268
12269 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12270 ret = FALSE;
12271 else
12272 {
12273 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12274 gc_mark_hook, &cookie))
12275 ret = FALSE;
12276 fini_reloc_cookie_for_section (&cookie, eh_frame);
12277 }
12278 }
12279
2f0c68f2
CM
12280 eh_frame = elf_section_eh_frame_entry (sec);
12281 if (ret && eh_frame && !eh_frame->gc_mark)
12282 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12283 ret = FALSE;
12284
c152c796
AM
12285 return ret;
12286}
12287
3c758495
TG
12288/* Scan and mark sections in a special or debug section group. */
12289
12290static void
12291_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12292{
12293 /* Point to first section of section group. */
12294 asection *ssec;
12295 /* Used to iterate the section group. */
12296 asection *msec;
12297
12298 bfd_boolean is_special_grp = TRUE;
12299 bfd_boolean is_debug_grp = TRUE;
12300
12301 /* First scan to see if group contains any section other than debug
12302 and special section. */
12303 ssec = msec = elf_next_in_group (grp);
12304 do
12305 {
12306 if ((msec->flags & SEC_DEBUGGING) == 0)
12307 is_debug_grp = FALSE;
12308
12309 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12310 is_special_grp = FALSE;
12311
12312 msec = elf_next_in_group (msec);
12313 }
12314 while (msec != ssec);
12315
12316 /* If this is a pure debug section group or pure special section group,
12317 keep all sections in this group. */
12318 if (is_debug_grp || is_special_grp)
12319 {
12320 do
12321 {
12322 msec->gc_mark = 1;
12323 msec = elf_next_in_group (msec);
12324 }
12325 while (msec != ssec);
12326 }
12327}
12328
7f6ab9f8
AM
12329/* Keep debug and special sections. */
12330
12331bfd_boolean
12332_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12333 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12334{
12335 bfd *ibfd;
12336
c72f2fb2 12337 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12338 {
12339 asection *isec;
12340 bfd_boolean some_kept;
b40bf0a2 12341 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12342
12343 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12344 continue;
12345
b40bf0a2
NC
12346 /* Ensure all linker created sections are kept,
12347 see if any other section is already marked,
12348 and note if we have any fragmented debug sections. */
12349 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12350 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12351 {
12352 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12353 isec->gc_mark = 1;
12354 else if (isec->gc_mark)
12355 some_kept = TRUE;
b40bf0a2
NC
12356
12357 if (debug_frag_seen == FALSE
12358 && (isec->flags & SEC_DEBUGGING)
12359 && CONST_STRNEQ (isec->name, ".debug_line."))
12360 debug_frag_seen = TRUE;
7f6ab9f8
AM
12361 }
12362
12363 /* If no section in this file will be kept, then we can
b40bf0a2 12364 toss out the debug and special sections. */
7f6ab9f8
AM
12365 if (!some_kept)
12366 continue;
12367
12368 /* Keep debug and special sections like .comment when they are
3c758495
TG
12369 not part of a group. Also keep section groups that contain
12370 just debug sections or special sections. */
7f6ab9f8 12371 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12372 {
12373 if ((isec->flags & SEC_GROUP) != 0)
12374 _bfd_elf_gc_mark_debug_special_section_group (isec);
12375 else if (((isec->flags & SEC_DEBUGGING) != 0
12376 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12377 && elf_next_in_group (isec) == NULL)
12378 isec->gc_mark = 1;
12379 }
b40bf0a2
NC
12380
12381 if (! debug_frag_seen)
12382 continue;
12383
12384 /* Look for CODE sections which are going to be discarded,
12385 and find and discard any fragmented debug sections which
12386 are associated with that code section. */
12387 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12388 if ((isec->flags & SEC_CODE) != 0
12389 && isec->gc_mark == 0)
12390 {
12391 unsigned int ilen;
12392 asection *dsec;
12393
12394 ilen = strlen (isec->name);
12395
12396 /* Association is determined by the name of the debug section
12397 containing the name of the code section as a suffix. For
12398 example .debug_line.text.foo is a debug section associated
12399 with .text.foo. */
12400 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12401 {
12402 unsigned int dlen;
12403
12404 if (dsec->gc_mark == 0
12405 || (dsec->flags & SEC_DEBUGGING) == 0)
12406 continue;
12407
12408 dlen = strlen (dsec->name);
12409
12410 if (dlen > ilen
12411 && strncmp (dsec->name + (dlen - ilen),
12412 isec->name, ilen) == 0)
12413 {
12414 dsec->gc_mark = 0;
b40bf0a2
NC
12415 }
12416 }
12417 }
7f6ab9f8
AM
12418 }
12419 return TRUE;
12420}
12421
c152c796
AM
12422/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12423
c17d87de
NC
12424struct elf_gc_sweep_symbol_info
12425{
ccabcbe5
AM
12426 struct bfd_link_info *info;
12427 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12428 bfd_boolean);
12429};
12430
c152c796 12431static bfd_boolean
ccabcbe5 12432elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12433{
1d5316ab
AM
12434 if (!h->mark
12435 && (((h->root.type == bfd_link_hash_defined
12436 || h->root.type == bfd_link_hash_defweak)
c4621b33 12437 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12438 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12439 || h->root.type == bfd_link_hash_undefined
12440 || h->root.type == bfd_link_hash_undefweak))
12441 {
12442 struct elf_gc_sweep_symbol_info *inf;
12443
12444 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12445 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12446 h->def_regular = 0;
12447 h->ref_regular = 0;
12448 h->ref_regular_nonweak = 0;
ccabcbe5 12449 }
c152c796
AM
12450
12451 return TRUE;
12452}
12453
12454/* The sweep phase of garbage collection. Remove all garbage sections. */
12455
12456typedef bfd_boolean (*gc_sweep_hook_fn)
12457 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12458
12459static bfd_boolean
ccabcbe5 12460elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12461{
12462 bfd *sub;
ccabcbe5
AM
12463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12464 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12465 unsigned long section_sym_count;
12466 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12467
c72f2fb2 12468 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12469 {
12470 asection *o;
12471
b19a8f85
L
12472 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12473 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12474 continue;
12475
12476 for (o = sub->sections; o != NULL; o = o->next)
12477 {
a33dafc3
L
12478 /* When any section in a section group is kept, we keep all
12479 sections in the section group. If the first member of
12480 the section group is excluded, we will also exclude the
12481 group section. */
12482 if (o->flags & SEC_GROUP)
12483 {
12484 asection *first = elf_next_in_group (o);
12485 o->gc_mark = first->gc_mark;
12486 }
c152c796 12487
1e7eae0d 12488 if (o->gc_mark)
c152c796
AM
12489 continue;
12490
12491 /* Skip sweeping sections already excluded. */
12492 if (o->flags & SEC_EXCLUDE)
12493 continue;
12494
12495 /* Since this is early in the link process, it is simple
12496 to remove a section from the output. */
12497 o->flags |= SEC_EXCLUDE;
12498
c55fe096 12499 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12500 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12501
c152c796
AM
12502 /* But we also have to update some of the relocation
12503 info we collected before. */
12504 if (gc_sweep_hook
e8aaee2a 12505 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12506 && o->reloc_count != 0
12507 && !((info->strip == strip_all || info->strip == strip_debugger)
12508 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12509 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12510 {
12511 Elf_Internal_Rela *internal_relocs;
12512 bfd_boolean r;
12513
12514 internal_relocs
12515 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12516 info->keep_memory);
12517 if (internal_relocs == NULL)
12518 return FALSE;
12519
12520 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12521
12522 if (elf_section_data (o)->relocs != internal_relocs)
12523 free (internal_relocs);
12524
12525 if (!r)
12526 return FALSE;
12527 }
12528 }
12529 }
12530
12531 /* Remove the symbols that were in the swept sections from the dynamic
12532 symbol table. GCFIXME: Anyone know how to get them out of the
12533 static symbol table as well? */
ccabcbe5
AM
12534 sweep_info.info = info;
12535 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12536 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12537 &sweep_info);
c152c796 12538
ccabcbe5 12539 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12540 return TRUE;
12541}
12542
12543/* Propagate collected vtable information. This is called through
12544 elf_link_hash_traverse. */
12545
12546static bfd_boolean
12547elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12548{
c152c796 12549 /* Those that are not vtables. */
f6e332e6 12550 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12551 return TRUE;
12552
12553 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12554 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12555 return TRUE;
12556
12557 /* If we've already been done, exit. */
f6e332e6 12558 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12559 return TRUE;
12560
12561 /* Make sure the parent's table is up to date. */
f6e332e6 12562 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12563
f6e332e6 12564 if (h->vtable->used == NULL)
c152c796
AM
12565 {
12566 /* None of this table's entries were referenced. Re-use the
12567 parent's table. */
f6e332e6
AM
12568 h->vtable->used = h->vtable->parent->vtable->used;
12569 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12570 }
12571 else
12572 {
12573 size_t n;
12574 bfd_boolean *cu, *pu;
12575
12576 /* Or the parent's entries into ours. */
f6e332e6 12577 cu = h->vtable->used;
c152c796 12578 cu[-1] = TRUE;
f6e332e6 12579 pu = h->vtable->parent->vtable->used;
c152c796
AM
12580 if (pu != NULL)
12581 {
12582 const struct elf_backend_data *bed;
12583 unsigned int log_file_align;
12584
12585 bed = get_elf_backend_data (h->root.u.def.section->owner);
12586 log_file_align = bed->s->log_file_align;
f6e332e6 12587 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12588 while (n--)
12589 {
12590 if (*pu)
12591 *cu = TRUE;
12592 pu++;
12593 cu++;
12594 }
12595 }
12596 }
12597
12598 return TRUE;
12599}
12600
12601static bfd_boolean
12602elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12603{
12604 asection *sec;
12605 bfd_vma hstart, hend;
12606 Elf_Internal_Rela *relstart, *relend, *rel;
12607 const struct elf_backend_data *bed;
12608 unsigned int log_file_align;
12609
c152c796
AM
12610 /* Take care of both those symbols that do not describe vtables as
12611 well as those that are not loaded. */
f6e332e6 12612 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12613 return TRUE;
12614
12615 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12616 || h->root.type == bfd_link_hash_defweak);
12617
12618 sec = h->root.u.def.section;
12619 hstart = h->root.u.def.value;
12620 hend = hstart + h->size;
12621
12622 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12623 if (!relstart)
12624 return *(bfd_boolean *) okp = FALSE;
12625 bed = get_elf_backend_data (sec->owner);
12626 log_file_align = bed->s->log_file_align;
12627
12628 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12629
12630 for (rel = relstart; rel < relend; ++rel)
12631 if (rel->r_offset >= hstart && rel->r_offset < hend)
12632 {
12633 /* If the entry is in use, do nothing. */
f6e332e6
AM
12634 if (h->vtable->used
12635 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12636 {
12637 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12638 if (h->vtable->used[entry])
c152c796
AM
12639 continue;
12640 }
12641 /* Otherwise, kill it. */
12642 rel->r_offset = rel->r_info = rel->r_addend = 0;
12643 }
12644
12645 return TRUE;
12646}
12647
87538722
AM
12648/* Mark sections containing dynamically referenced symbols. When
12649 building shared libraries, we must assume that any visible symbol is
12650 referenced. */
715df9b8 12651
64d03ab5
AM
12652bfd_boolean
12653bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12654{
87538722 12655 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12656 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12657
715df9b8
EB
12658 if ((h->root.type == bfd_link_hash_defined
12659 || h->root.type == bfd_link_hash_defweak)
87538722 12660 && (h->ref_dynamic
c4621b33 12661 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12662 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12663 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12664 && (!bfd_link_executable (info)
b407645f
AM
12665 || info->export_dynamic
12666 || (h->dynamic
12667 && d != NULL
12668 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12669 && (h->versioned >= versioned
54e8959c
L
12670 || !bfd_hide_sym_by_version (info->version_info,
12671 h->root.root.string)))))
715df9b8
EB
12672 h->root.u.def.section->flags |= SEC_KEEP;
12673
12674 return TRUE;
12675}
3b36f7e6 12676
74f0fb50
AM
12677/* Keep all sections containing symbols undefined on the command-line,
12678 and the section containing the entry symbol. */
12679
12680void
12681_bfd_elf_gc_keep (struct bfd_link_info *info)
12682{
12683 struct bfd_sym_chain *sym;
12684
12685 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12686 {
12687 struct elf_link_hash_entry *h;
12688
12689 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12690 FALSE, FALSE, FALSE);
12691
12692 if (h != NULL
12693 && (h->root.type == bfd_link_hash_defined
12694 || h->root.type == bfd_link_hash_defweak)
12695 && !bfd_is_abs_section (h->root.u.def.section))
12696 h->root.u.def.section->flags |= SEC_KEEP;
12697 }
12698}
12699
2f0c68f2
CM
12700bfd_boolean
12701bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12702 struct bfd_link_info *info)
12703{
12704 bfd *ibfd = info->input_bfds;
12705
12706 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12707 {
12708 asection *sec;
12709 struct elf_reloc_cookie cookie;
12710
12711 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12712 continue;
12713
12714 if (!init_reloc_cookie (&cookie, info, ibfd))
12715 return FALSE;
12716
12717 for (sec = ibfd->sections; sec; sec = sec->next)
12718 {
12719 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12720 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12721 {
12722 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12723 fini_reloc_cookie_rels (&cookie, sec);
12724 }
12725 }
12726 }
12727 return TRUE;
12728}
12729
c152c796
AM
12730/* Do mark and sweep of unused sections. */
12731
12732bfd_boolean
12733bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12734{
12735 bfd_boolean ok = TRUE;
12736 bfd *sub;
6a5bb875 12737 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12738 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12739 struct elf_link_hash_table *htab;
c152c796 12740
64d03ab5 12741 if (!bed->can_gc_sections
715df9b8 12742 || !is_elf_hash_table (info->hash))
c152c796
AM
12743 {
12744 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12745 return TRUE;
12746 }
12747
74f0fb50 12748 bed->gc_keep (info);
da44f4e5 12749 htab = elf_hash_table (info);
74f0fb50 12750
9d0a14d3
RS
12751 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12752 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12753 for (sub = info->input_bfds;
12754 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12755 sub = sub->link.next)
9d0a14d3
RS
12756 {
12757 asection *sec;
12758 struct elf_reloc_cookie cookie;
12759
12760 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12761 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12762 {
12763 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12764 if (elf_section_data (sec)->sec_info
12765 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12766 elf_eh_frame_section (sub) = sec;
12767 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12768 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12769 }
12770 }
9d0a14d3 12771
c152c796 12772 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12773 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12774 if (!ok)
12775 return FALSE;
12776
12777 /* Kill the vtable relocations that were not used. */
da44f4e5 12778 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12779 if (!ok)
12780 return FALSE;
12781
715df9b8 12782 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12783 if (htab->dynamic_sections_created)
12784 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12785
715df9b8 12786 /* Grovel through relocs to find out who stays ... */
64d03ab5 12787 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12788 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12789 {
12790 asection *o;
12791
b19a8f85
L
12792 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12793 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12794 continue;
12795
7f6ab9f8
AM
12796 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12797 Also treat note sections as a root, if the section is not part
12798 of a group. */
c152c796 12799 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12800 if (!o->gc_mark
12801 && (o->flags & SEC_EXCLUDE) == 0
24007750 12802 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12803 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12804 && elf_next_in_group (o) == NULL )))
12805 {
12806 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12807 return FALSE;
12808 }
c152c796
AM
12809 }
12810
6a5bb875 12811 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12812 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12813
c152c796 12814 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12815 return elf_gc_sweep (abfd, info);
c152c796
AM
12816}
12817\f
12818/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12819
12820bfd_boolean
12821bfd_elf_gc_record_vtinherit (bfd *abfd,
12822 asection *sec,
12823 struct elf_link_hash_entry *h,
12824 bfd_vma offset)
12825{
12826 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12827 struct elf_link_hash_entry **search, *child;
12828 bfd_size_type extsymcount;
12829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12830
12831 /* The sh_info field of the symtab header tells us where the
12832 external symbols start. We don't care about the local symbols at
12833 this point. */
12834 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12835 if (!elf_bad_symtab (abfd))
12836 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12837
12838 sym_hashes = elf_sym_hashes (abfd);
12839 sym_hashes_end = sym_hashes + extsymcount;
12840
12841 /* Hunt down the child symbol, which is in this section at the same
12842 offset as the relocation. */
12843 for (search = sym_hashes; search != sym_hashes_end; ++search)
12844 {
12845 if ((child = *search) != NULL
12846 && (child->root.type == bfd_link_hash_defined
12847 || child->root.type == bfd_link_hash_defweak)
12848 && child->root.u.def.section == sec
12849 && child->root.u.def.value == offset)
12850 goto win;
12851 }
12852
d003868e
AM
12853 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12854 abfd, sec, (unsigned long) offset);
c152c796
AM
12855 bfd_set_error (bfd_error_invalid_operation);
12856 return FALSE;
12857
12858 win:
f6e332e6
AM
12859 if (!child->vtable)
12860 {
ca4be51c
AM
12861 child->vtable = ((struct elf_link_virtual_table_entry *)
12862 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12863 if (!child->vtable)
12864 return FALSE;
12865 }
c152c796
AM
12866 if (!h)
12867 {
12868 /* This *should* only be the absolute section. It could potentially
12869 be that someone has defined a non-global vtable though, which
12870 would be bad. It isn't worth paging in the local symbols to be
12871 sure though; that case should simply be handled by the assembler. */
12872
f6e332e6 12873 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12874 }
12875 else
f6e332e6 12876 child->vtable->parent = h;
c152c796
AM
12877
12878 return TRUE;
12879}
12880
12881/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12882
12883bfd_boolean
12884bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12885 asection *sec ATTRIBUTE_UNUSED,
12886 struct elf_link_hash_entry *h,
12887 bfd_vma addend)
12888{
12889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12890 unsigned int log_file_align = bed->s->log_file_align;
12891
f6e332e6
AM
12892 if (!h->vtable)
12893 {
ca4be51c
AM
12894 h->vtable = ((struct elf_link_virtual_table_entry *)
12895 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
12896 if (!h->vtable)
12897 return FALSE;
12898 }
12899
12900 if (addend >= h->vtable->size)
c152c796
AM
12901 {
12902 size_t size, bytes, file_align;
f6e332e6 12903 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
12904
12905 /* While the symbol is undefined, we have to be prepared to handle
12906 a zero size. */
12907 file_align = 1 << log_file_align;
12908 if (h->root.type == bfd_link_hash_undefined)
12909 size = addend + file_align;
12910 else
12911 {
12912 size = h->size;
12913 if (addend >= size)
12914 {
12915 /* Oops! We've got a reference past the defined end of
12916 the table. This is probably a bug -- shall we warn? */
12917 size = addend + file_align;
12918 }
12919 }
12920 size = (size + file_align - 1) & -file_align;
12921
12922 /* Allocate one extra entry for use as a "done" flag for the
12923 consolidation pass. */
12924 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12925
12926 if (ptr)
12927 {
a50b1753 12928 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
12929
12930 if (ptr != NULL)
12931 {
12932 size_t oldbytes;
12933
f6e332e6 12934 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
12935 * sizeof (bfd_boolean));
12936 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12937 }
12938 }
12939 else
a50b1753 12940 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
12941
12942 if (ptr == NULL)
12943 return FALSE;
12944
12945 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
12946 h->vtable->used = ptr + 1;
12947 h->vtable->size = size;
c152c796
AM
12948 }
12949
f6e332e6 12950 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
12951
12952 return TRUE;
12953}
12954
ae17ab41
CM
12955/* Map an ELF section header flag to its corresponding string. */
12956typedef struct
12957{
12958 char *flag_name;
12959 flagword flag_value;
12960} elf_flags_to_name_table;
12961
12962static elf_flags_to_name_table elf_flags_to_names [] =
12963{
12964 { "SHF_WRITE", SHF_WRITE },
12965 { "SHF_ALLOC", SHF_ALLOC },
12966 { "SHF_EXECINSTR", SHF_EXECINSTR },
12967 { "SHF_MERGE", SHF_MERGE },
12968 { "SHF_STRINGS", SHF_STRINGS },
12969 { "SHF_INFO_LINK", SHF_INFO_LINK},
12970 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12971 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12972 { "SHF_GROUP", SHF_GROUP },
12973 { "SHF_TLS", SHF_TLS },
12974 { "SHF_MASKOS", SHF_MASKOS },
12975 { "SHF_EXCLUDE", SHF_EXCLUDE },
12976};
12977
b9c361e0
JL
12978/* Returns TRUE if the section is to be included, otherwise FALSE. */
12979bfd_boolean
ae17ab41 12980bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 12981 struct flag_info *flaginfo,
b9c361e0 12982 asection *section)
ae17ab41 12983{
8b127cbc 12984 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 12985
8b127cbc 12986 if (!flaginfo->flags_initialized)
ae17ab41 12987 {
8b127cbc
AM
12988 bfd *obfd = info->output_bfd;
12989 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12990 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
12991 int with_hex = 0;
12992 int without_hex = 0;
12993
8b127cbc 12994 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 12995 {
b9c361e0 12996 unsigned i;
8b127cbc 12997 flagword (*lookup) (char *);
ae17ab41 12998
8b127cbc
AM
12999 lookup = bed->elf_backend_lookup_section_flags_hook;
13000 if (lookup != NULL)
ae17ab41 13001 {
8b127cbc 13002 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13003
13004 if (hexval != 0)
13005 {
13006 if (tf->with == with_flags)
13007 with_hex |= hexval;
13008 else if (tf->with == without_flags)
13009 without_hex |= hexval;
13010 tf->valid = TRUE;
13011 continue;
13012 }
ae17ab41 13013 }
8b127cbc 13014 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13015 {
8b127cbc 13016 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13017 {
13018 if (tf->with == with_flags)
13019 with_hex |= elf_flags_to_names[i].flag_value;
13020 else if (tf->with == without_flags)
13021 without_hex |= elf_flags_to_names[i].flag_value;
13022 tf->valid = TRUE;
13023 break;
13024 }
13025 }
8b127cbc 13026 if (!tf->valid)
b9c361e0 13027 {
68ffbac6 13028 info->callbacks->einfo
8b127cbc 13029 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13030 return FALSE;
ae17ab41
CM
13031 }
13032 }
8b127cbc
AM
13033 flaginfo->flags_initialized = TRUE;
13034 flaginfo->only_with_flags |= with_hex;
13035 flaginfo->not_with_flags |= without_hex;
ae17ab41 13036 }
ae17ab41 13037
8b127cbc 13038 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13039 return FALSE;
13040
8b127cbc 13041 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13042 return FALSE;
13043
13044 return TRUE;
ae17ab41
CM
13045}
13046
c152c796
AM
13047struct alloc_got_off_arg {
13048 bfd_vma gotoff;
10455f89 13049 struct bfd_link_info *info;
c152c796
AM
13050};
13051
13052/* We need a special top-level link routine to convert got reference counts
13053 to real got offsets. */
13054
13055static bfd_boolean
13056elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13057{
a50b1753 13058 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13059 bfd *obfd = gofarg->info->output_bfd;
13060 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13061
c152c796
AM
13062 if (h->got.refcount > 0)
13063 {
13064 h->got.offset = gofarg->gotoff;
10455f89 13065 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13066 }
13067 else
13068 h->got.offset = (bfd_vma) -1;
13069
13070 return TRUE;
13071}
13072
13073/* And an accompanying bit to work out final got entry offsets once
13074 we're done. Should be called from final_link. */
13075
13076bfd_boolean
13077bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13078 struct bfd_link_info *info)
13079{
13080 bfd *i;
13081 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13082 bfd_vma gotoff;
c152c796
AM
13083 struct alloc_got_off_arg gofarg;
13084
10455f89
HPN
13085 BFD_ASSERT (abfd == info->output_bfd);
13086
c152c796
AM
13087 if (! is_elf_hash_table (info->hash))
13088 return FALSE;
13089
13090 /* The GOT offset is relative to the .got section, but the GOT header is
13091 put into the .got.plt section, if the backend uses it. */
13092 if (bed->want_got_plt)
13093 gotoff = 0;
13094 else
13095 gotoff = bed->got_header_size;
13096
13097 /* Do the local .got entries first. */
c72f2fb2 13098 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13099 {
13100 bfd_signed_vma *local_got;
13101 bfd_size_type j, locsymcount;
13102 Elf_Internal_Shdr *symtab_hdr;
13103
13104 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13105 continue;
13106
13107 local_got = elf_local_got_refcounts (i);
13108 if (!local_got)
13109 continue;
13110
13111 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13112 if (elf_bad_symtab (i))
13113 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13114 else
13115 locsymcount = symtab_hdr->sh_info;
13116
13117 for (j = 0; j < locsymcount; ++j)
13118 {
13119 if (local_got[j] > 0)
13120 {
13121 local_got[j] = gotoff;
10455f89 13122 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13123 }
13124 else
13125 local_got[j] = (bfd_vma) -1;
13126 }
13127 }
13128
13129 /* Then the global .got entries. .plt refcounts are handled by
13130 adjust_dynamic_symbol */
13131 gofarg.gotoff = gotoff;
10455f89 13132 gofarg.info = info;
c152c796
AM
13133 elf_link_hash_traverse (elf_hash_table (info),
13134 elf_gc_allocate_got_offsets,
13135 &gofarg);
13136 return TRUE;
13137}
13138
13139/* Many folk need no more in the way of final link than this, once
13140 got entry reference counting is enabled. */
13141
13142bfd_boolean
13143bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13144{
13145 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13146 return FALSE;
13147
13148 /* Invoke the regular ELF backend linker to do all the work. */
13149 return bfd_elf_final_link (abfd, info);
13150}
13151
13152bfd_boolean
13153bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13154{
a50b1753 13155 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13156
13157 if (rcookie->bad_symtab)
13158 rcookie->rel = rcookie->rels;
13159
13160 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13161 {
13162 unsigned long r_symndx;
13163
13164 if (! rcookie->bad_symtab)
13165 if (rcookie->rel->r_offset > offset)
13166 return FALSE;
13167 if (rcookie->rel->r_offset != offset)
13168 continue;
13169
13170 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13171 if (r_symndx == STN_UNDEF)
c152c796
AM
13172 return TRUE;
13173
13174 if (r_symndx >= rcookie->locsymcount
13175 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13176 {
13177 struct elf_link_hash_entry *h;
13178
13179 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13180
13181 while (h->root.type == bfd_link_hash_indirect
13182 || h->root.type == bfd_link_hash_warning)
13183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13184
13185 if ((h->root.type == bfd_link_hash_defined
13186 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13187 && (h->root.u.def.section->owner != rcookie->abfd
13188 || h->root.u.def.section->kept_section != NULL
13189 || discarded_section (h->root.u.def.section)))
c152c796 13190 return TRUE;
c152c796
AM
13191 }
13192 else
13193 {
13194 /* It's not a relocation against a global symbol,
13195 but it could be a relocation against a local
13196 symbol for a discarded section. */
13197 asection *isec;
13198 Elf_Internal_Sym *isym;
13199
13200 /* Need to: get the symbol; get the section. */
13201 isym = &rcookie->locsyms[r_symndx];
cb33740c 13202 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13203 if (isec != NULL
13204 && (isec->kept_section != NULL
13205 || discarded_section (isec)))
cb33740c 13206 return TRUE;
c152c796
AM
13207 }
13208 return FALSE;
13209 }
13210 return FALSE;
13211}
13212
13213/* Discard unneeded references to discarded sections.
75938853
AM
13214 Returns -1 on error, 1 if any section's size was changed, 0 if
13215 nothing changed. This function assumes that the relocations are in
13216 sorted order, which is true for all known assemblers. */
c152c796 13217
75938853 13218int
c152c796
AM
13219bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13220{
13221 struct elf_reloc_cookie cookie;
18cd5bce 13222 asection *o;
c152c796 13223 bfd *abfd;
75938853 13224 int changed = 0;
c152c796
AM
13225
13226 if (info->traditional_format
13227 || !is_elf_hash_table (info->hash))
75938853 13228 return 0;
c152c796 13229
18cd5bce
AM
13230 o = bfd_get_section_by_name (output_bfd, ".stab");
13231 if (o != NULL)
c152c796 13232 {
18cd5bce 13233 asection *i;
c152c796 13234
18cd5bce 13235 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13236 {
18cd5bce
AM
13237 if (i->size == 0
13238 || i->reloc_count == 0
13239 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13240 continue;
c152c796 13241
18cd5bce
AM
13242 abfd = i->owner;
13243 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13244 continue;
c152c796 13245
18cd5bce 13246 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13247 return -1;
c152c796 13248
18cd5bce
AM
13249 if (_bfd_discard_section_stabs (abfd, i,
13250 elf_section_data (i)->sec_info,
5241d853
RS
13251 bfd_elf_reloc_symbol_deleted_p,
13252 &cookie))
75938853 13253 changed = 1;
18cd5bce
AM
13254
13255 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13256 }
18cd5bce
AM
13257 }
13258
2f0c68f2
CM
13259 o = NULL;
13260 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13261 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13262 if (o != NULL)
13263 {
13264 asection *i;
c152c796 13265
18cd5bce 13266 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13267 {
18cd5bce
AM
13268 if (i->size == 0)
13269 continue;
13270
13271 abfd = i->owner;
13272 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13273 continue;
13274
13275 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13276 return -1;
18cd5bce
AM
13277
13278 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13279 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13280 bfd_elf_reloc_symbol_deleted_p,
13281 &cookie))
75938853 13282 changed = 1;
18cd5bce
AM
13283
13284 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13285 }
18cd5bce 13286 }
c152c796 13287
18cd5bce
AM
13288 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13289 {
13290 const struct elf_backend_data *bed;
c152c796 13291
18cd5bce
AM
13292 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13293 continue;
13294
13295 bed = get_elf_backend_data (abfd);
13296
13297 if (bed->elf_backend_discard_info != NULL)
13298 {
13299 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13300 return -1;
18cd5bce
AM
13301
13302 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13303 changed = 1;
18cd5bce
AM
13304
13305 fini_reloc_cookie (&cookie, abfd);
13306 }
c152c796
AM
13307 }
13308
2f0c68f2
CM
13309 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13310 _bfd_elf_end_eh_frame_parsing (info);
13311
13312 if (info->eh_frame_hdr_type
0e1862bb 13313 && !bfd_link_relocatable (info)
c152c796 13314 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13315 changed = 1;
c152c796 13316
75938853 13317 return changed;
c152c796 13318}
082b7297 13319
43e1669b 13320bfd_boolean
0c511000 13321_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13322 asection *sec,
c0f00686 13323 struct bfd_link_info *info)
082b7297
L
13324{
13325 flagword flags;
c77ec726 13326 const char *name, *key;
082b7297
L
13327 struct bfd_section_already_linked *l;
13328 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13329
c77ec726
AM
13330 if (sec->output_section == bfd_abs_section_ptr)
13331 return FALSE;
0c511000 13332
c77ec726 13333 flags = sec->flags;
0c511000 13334
c77ec726
AM
13335 /* Return if it isn't a linkonce section. A comdat group section
13336 also has SEC_LINK_ONCE set. */
13337 if ((flags & SEC_LINK_ONCE) == 0)
13338 return FALSE;
0c511000 13339
c77ec726
AM
13340 /* Don't put group member sections on our list of already linked
13341 sections. They are handled as a group via their group section. */
13342 if (elf_sec_group (sec) != NULL)
13343 return FALSE;
0c511000 13344
c77ec726
AM
13345 /* For a SHT_GROUP section, use the group signature as the key. */
13346 name = sec->name;
13347 if ((flags & SEC_GROUP) != 0
13348 && elf_next_in_group (sec) != NULL
13349 && elf_group_name (elf_next_in_group (sec)) != NULL)
13350 key = elf_group_name (elf_next_in_group (sec));
13351 else
13352 {
13353 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13354 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13355 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13356 key++;
0c511000 13357 else
c77ec726
AM
13358 /* Must be a user linkonce section that doesn't follow gcc's
13359 naming convention. In this case we won't be matching
13360 single member groups. */
13361 key = name;
0c511000 13362 }
6d2cd210 13363
c77ec726 13364 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13365
13366 for (l = already_linked_list->entry; l != NULL; l = l->next)
13367 {
c2370991 13368 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13369 sections with a signature of <key> (<key> is some string),
13370 and linkonce sections named .gnu.linkonce.<type>.<key>.
13371 Match like sections. LTO plugin sections are an exception.
13372 They are always named .gnu.linkonce.t.<key> and match either
13373 type of section. */
13374 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13375 && ((flags & SEC_GROUP) != 0
13376 || strcmp (name, l->sec->name) == 0))
13377 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13378 {
13379 /* The section has already been linked. See if we should
6d2cd210 13380 issue a warning. */
c77ec726
AM
13381 if (!_bfd_handle_already_linked (sec, l, info))
13382 return FALSE;
082b7297 13383
c77ec726 13384 if (flags & SEC_GROUP)
3d7f7666 13385 {
c77ec726
AM
13386 asection *first = elf_next_in_group (sec);
13387 asection *s = first;
3d7f7666 13388
c77ec726 13389 while (s != NULL)
3d7f7666 13390 {
c77ec726
AM
13391 s->output_section = bfd_abs_section_ptr;
13392 /* Record which group discards it. */
13393 s->kept_section = l->sec;
13394 s = elf_next_in_group (s);
13395 /* These lists are circular. */
13396 if (s == first)
13397 break;
3d7f7666
L
13398 }
13399 }
082b7297 13400
43e1669b 13401 return TRUE;
082b7297
L
13402 }
13403 }
13404
c77ec726
AM
13405 /* A single member comdat group section may be discarded by a
13406 linkonce section and vice versa. */
13407 if ((flags & SEC_GROUP) != 0)
3d7f7666 13408 {
c77ec726 13409 asection *first = elf_next_in_group (sec);
c2370991 13410
c77ec726
AM
13411 if (first != NULL && elf_next_in_group (first) == first)
13412 /* Check this single member group against linkonce sections. */
13413 for (l = already_linked_list->entry; l != NULL; l = l->next)
13414 if ((l->sec->flags & SEC_GROUP) == 0
13415 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13416 {
13417 first->output_section = bfd_abs_section_ptr;
13418 first->kept_section = l->sec;
13419 sec->output_section = bfd_abs_section_ptr;
13420 break;
13421 }
13422 }
13423 else
13424 /* Check this linkonce section against single member groups. */
13425 for (l = already_linked_list->entry; l != NULL; l = l->next)
13426 if (l->sec->flags & SEC_GROUP)
6d2cd210 13427 {
c77ec726 13428 asection *first = elf_next_in_group (l->sec);
6d2cd210 13429
c77ec726
AM
13430 if (first != NULL
13431 && elf_next_in_group (first) == first
13432 && bfd_elf_match_symbols_in_sections (first, sec, info))
13433 {
13434 sec->output_section = bfd_abs_section_ptr;
13435 sec->kept_section = first;
13436 break;
13437 }
6d2cd210 13438 }
0c511000 13439
c77ec726
AM
13440 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13441 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13442 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13443 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13444 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13445 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13446 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13447 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13448 The reverse order cannot happen as there is never a bfd with only the
13449 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13450 matter as here were are looking only for cross-bfd sections. */
13451
13452 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13453 for (l = already_linked_list->entry; l != NULL; l = l->next)
13454 if ((l->sec->flags & SEC_GROUP) == 0
13455 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13456 {
13457 if (abfd != l->sec->owner)
13458 sec->output_section = bfd_abs_section_ptr;
13459 break;
13460 }
80c29487 13461
082b7297 13462 /* This is the first section with this name. Record it. */
c77ec726 13463 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13464 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13465 return sec->output_section == bfd_abs_section_ptr;
082b7297 13466}
81e1b023 13467
a4d8e49b
L
13468bfd_boolean
13469_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13470{
13471 return sym->st_shndx == SHN_COMMON;
13472}
13473
13474unsigned int
13475_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13476{
13477 return SHN_COMMON;
13478}
13479
13480asection *
13481_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13482{
13483 return bfd_com_section_ptr;
13484}
10455f89
HPN
13485
13486bfd_vma
13487_bfd_elf_default_got_elt_size (bfd *abfd,
13488 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13489 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13490 bfd *ibfd ATTRIBUTE_UNUSED,
13491 unsigned long symndx ATTRIBUTE_UNUSED)
13492{
13493 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13494 return bed->s->arch_size / 8;
13495}
83bac4b0
NC
13496
13497/* Routines to support the creation of dynamic relocs. */
13498
83bac4b0
NC
13499/* Returns the name of the dynamic reloc section associated with SEC. */
13500
13501static const char *
13502get_dynamic_reloc_section_name (bfd * abfd,
13503 asection * sec,
13504 bfd_boolean is_rela)
13505{
ddcf1fcf
BS
13506 char *name;
13507 const char *old_name = bfd_get_section_name (NULL, sec);
13508 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13509
ddcf1fcf 13510 if (old_name == NULL)
83bac4b0
NC
13511 return NULL;
13512
ddcf1fcf 13513 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13514 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13515
13516 return name;
13517}
13518
13519/* Returns the dynamic reloc section associated with SEC.
13520 If necessary compute the name of the dynamic reloc section based
13521 on SEC's name (looked up in ABFD's string table) and the setting
13522 of IS_RELA. */
13523
13524asection *
13525_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13526 asection * sec,
13527 bfd_boolean is_rela)
13528{
13529 asection * reloc_sec = elf_section_data (sec)->sreloc;
13530
13531 if (reloc_sec == NULL)
13532 {
13533 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13534
13535 if (name != NULL)
13536 {
3d4d4302 13537 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13538
13539 if (reloc_sec != NULL)
13540 elf_section_data (sec)->sreloc = reloc_sec;
13541 }
13542 }
13543
13544 return reloc_sec;
13545}
13546
13547/* Returns the dynamic reloc section associated with SEC. If the
13548 section does not exist it is created and attached to the DYNOBJ
13549 bfd and stored in the SRELOC field of SEC's elf_section_data
13550 structure.
f8076f98 13551
83bac4b0
NC
13552 ALIGNMENT is the alignment for the newly created section and
13553 IS_RELA defines whether the name should be .rela.<SEC's name>
13554 or .rel.<SEC's name>. The section name is looked up in the
13555 string table associated with ABFD. */
13556
13557asection *
ca4be51c
AM
13558_bfd_elf_make_dynamic_reloc_section (asection *sec,
13559 bfd *dynobj,
13560 unsigned int alignment,
13561 bfd *abfd,
13562 bfd_boolean is_rela)
83bac4b0
NC
13563{
13564 asection * reloc_sec = elf_section_data (sec)->sreloc;
13565
13566 if (reloc_sec == NULL)
13567 {
13568 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13569
13570 if (name == NULL)
13571 return NULL;
13572
3d4d4302 13573 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13574
13575 if (reloc_sec == NULL)
13576 {
3d4d4302
AM
13577 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13578 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13579 if ((sec->flags & SEC_ALLOC) != 0)
13580 flags |= SEC_ALLOC | SEC_LOAD;
13581
3d4d4302 13582 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13583 if (reloc_sec != NULL)
13584 {
8877b5e5
AM
13585 /* _bfd_elf_get_sec_type_attr chooses a section type by
13586 name. Override as it may be wrong, eg. for a user
13587 section named "auto" we'll get ".relauto" which is
13588 seen to be a .rela section. */
13589 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13590 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13591 reloc_sec = NULL;
13592 }
13593 }
13594
13595 elf_section_data (sec)->sreloc = reloc_sec;
13596 }
13597
13598 return reloc_sec;
13599}
1338dd10 13600
bffebb6b
AM
13601/* Copy the ELF symbol type and other attributes for a linker script
13602 assignment from HSRC to HDEST. Generally this should be treated as
13603 if we found a strong non-dynamic definition for HDEST (except that
13604 ld ignores multiple definition errors). */
1338dd10 13605void
bffebb6b
AM
13606_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13607 struct bfd_link_hash_entry *hdest,
13608 struct bfd_link_hash_entry *hsrc)
1338dd10 13609{
bffebb6b
AM
13610 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13611 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13612 Elf_Internal_Sym isym;
1338dd10
PB
13613
13614 ehdest->type = ehsrc->type;
35fc36a8 13615 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13616
13617 isym.st_other = ehsrc->other;
b8417128 13618 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13619}
351f65ca
L
13620
13621/* Append a RELA relocation REL to section S in BFD. */
13622
13623void
13624elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13625{
13626 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13627 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13628 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13629 bed->s->swap_reloca_out (abfd, rel, loc);
13630}
13631
13632/* Append a REL relocation REL to section S in BFD. */
13633
13634void
13635elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13636{
13637 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13638 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13639 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13640 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13641}
This page took 2.170607 seconds and 4 git commands to generate.