]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
MIPS/GAS: Fix an ISA override not lifting ABI restrictions
[thirdparty/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
b8871f35 528 || h->type == STT_COMMON
40b36307 529 || (sym != NULL
b8871f35
L
530 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
531 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 532 || (d != NULL
40b36307
L
533 && h->root.type == bfd_link_hash_new
534 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
535 h->dynamic = 1;
536}
537
45d6a902
AM
538/* Record an assignment to a symbol made by a linker script. We need
539 this in case some dynamic object refers to this symbol. */
540
541bfd_boolean
fe21a8fc
L
542bfd_elf_record_link_assignment (bfd *output_bfd,
543 struct bfd_link_info *info,
268b6b39 544 const char *name,
fe21a8fc
L
545 bfd_boolean provide,
546 bfd_boolean hidden)
45d6a902 547{
00cbee0a 548 struct elf_link_hash_entry *h, *hv;
4ea42fb7 549 struct elf_link_hash_table *htab;
00cbee0a 550 const struct elf_backend_data *bed;
45d6a902 551
0eddce27 552 if (!is_elf_hash_table (info->hash))
45d6a902
AM
553 return TRUE;
554
4ea42fb7
AM
555 htab = elf_hash_table (info);
556 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 557 if (h == NULL)
4ea42fb7 558 return provide;
45d6a902 559
0f550b3d
L
560 if (h->versioned == unknown)
561 {
562 /* Set versioned if symbol version is unknown. */
563 char *version = strrchr (name, ELF_VER_CHR);
564 if (version)
565 {
566 if (version > name && version[-1] != ELF_VER_CHR)
567 h->versioned = versioned_hidden;
568 else
569 h->versioned = versioned;
570 }
571 }
572
00cbee0a 573 switch (h->root.type)
77cfaee6 574 {
00cbee0a
L
575 case bfd_link_hash_defined:
576 case bfd_link_hash_defweak:
577 case bfd_link_hash_common:
578 break;
579 case bfd_link_hash_undefweak:
580 case bfd_link_hash_undefined:
581 /* Since we're defining the symbol, don't let it seem to have not
582 been defined. record_dynamic_symbol and size_dynamic_sections
583 may depend on this. */
4ea42fb7 584 h->root.type = bfd_link_hash_new;
77cfaee6
AM
585 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
586 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
587 break;
588 case bfd_link_hash_new:
40b36307 589 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 590 h->non_elf = 0;
00cbee0a
L
591 break;
592 case bfd_link_hash_indirect:
593 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 594 the versioned symbol point to this one. */
00cbee0a
L
595 bed = get_elf_backend_data (output_bfd);
596 hv = h;
597 while (hv->root.type == bfd_link_hash_indirect
598 || hv->root.type == bfd_link_hash_warning)
599 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
600 /* We don't need to update h->root.u since linker will set them
601 later. */
602 h->root.type = bfd_link_hash_undefined;
603 hv->root.type = bfd_link_hash_indirect;
604 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
605 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
606 break;
607 case bfd_link_hash_warning:
608 abort ();
609 break;
55255dae 610 }
45d6a902
AM
611
612 /* If this symbol is being provided by the linker script, and it is
613 currently defined by a dynamic object, but not by a regular
614 object, then mark it as undefined so that the generic linker will
615 force the correct value. */
616 if (provide
f5385ebf
AM
617 && h->def_dynamic
618 && !h->def_regular)
45d6a902
AM
619 h->root.type = bfd_link_hash_undefined;
620
621 /* If this symbol is not being provided by the linker script, and it is
622 currently defined by a dynamic object, but not by a regular object,
623 then clear out any version information because the symbol will not be
624 associated with the dynamic object any more. */
625 if (!provide
f5385ebf
AM
626 && h->def_dynamic
627 && !h->def_regular)
45d6a902
AM
628 h->verinfo.verdef = NULL;
629
f5385ebf 630 h->def_regular = 1;
45d6a902 631
eb8476a6 632 if (hidden)
fe21a8fc 633 {
91d6fa6a 634 bed = get_elf_backend_data (output_bfd);
b8297068
AM
635 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
636 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
637 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
638 }
639
6fa3860b
PB
640 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
641 and executables. */
0e1862bb 642 if (!bfd_link_relocatable (info)
6fa3860b
PB
643 && h->dynindx != -1
644 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
645 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
646 h->forced_local = 1;
647
f5385ebf
AM
648 if ((h->def_dynamic
649 || h->ref_dynamic
6b3b0ab8
L
650 || bfd_link_dll (info)
651 || elf_hash_table (info)->is_relocatable_executable)
45d6a902
AM
652 && h->dynindx == -1)
653 {
c152c796 654 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
655 return FALSE;
656
657 /* If this is a weak defined symbol, and we know a corresponding
658 real symbol from the same dynamic object, make sure the real
659 symbol is also made into a dynamic symbol. */
f6e332e6
AM
660 if (h->u.weakdef != NULL
661 && h->u.weakdef->dynindx == -1)
45d6a902 662 {
f6e332e6 663 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
664 return FALSE;
665 }
666 }
667
668 return TRUE;
669}
42751cf3 670
8c58d23b
AM
671/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
672 success, and 2 on a failure caused by attempting to record a symbol
673 in a discarded section, eg. a discarded link-once section symbol. */
674
675int
c152c796
AM
676bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
677 bfd *input_bfd,
678 long input_indx)
8c58d23b
AM
679{
680 bfd_size_type amt;
681 struct elf_link_local_dynamic_entry *entry;
682 struct elf_link_hash_table *eht;
683 struct elf_strtab_hash *dynstr;
684 unsigned long dynstr_index;
685 char *name;
686 Elf_External_Sym_Shndx eshndx;
687 char esym[sizeof (Elf64_External_Sym)];
688
0eddce27 689 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
690 return 0;
691
692 /* See if the entry exists already. */
693 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
694 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
695 return 1;
696
697 amt = sizeof (*entry);
a50b1753 698 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
699 if (entry == NULL)
700 return 0;
701
702 /* Go find the symbol, so that we can find it's name. */
703 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 704 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
705 {
706 bfd_release (input_bfd, entry);
707 return 0;
708 }
709
710 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 711 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
712 {
713 asection *s;
714
715 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
716 if (s == NULL || bfd_is_abs_section (s->output_section))
717 {
718 /* We can still bfd_release here as nothing has done another
719 bfd_alloc. We can't do this later in this function. */
720 bfd_release (input_bfd, entry);
721 return 2;
722 }
723 }
724
725 name = (bfd_elf_string_from_elf_section
726 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
727 entry->isym.st_name));
728
729 dynstr = elf_hash_table (info)->dynstr;
730 if (dynstr == NULL)
731 {
732 /* Create a strtab to hold the dynamic symbol names. */
733 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
734 if (dynstr == NULL)
735 return 0;
736 }
737
b34976b6 738 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
739 if (dynstr_index == (unsigned long) -1)
740 return 0;
741 entry->isym.st_name = dynstr_index;
742
743 eht = elf_hash_table (info);
744
745 entry->next = eht->dynlocal;
746 eht->dynlocal = entry;
747 entry->input_bfd = input_bfd;
748 entry->input_indx = input_indx;
749 eht->dynsymcount++;
750
751 /* Whatever binding the symbol had before, it's now local. */
752 entry->isym.st_info
753 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
754
755 /* The dynindx will be set at the end of size_dynamic_sections. */
756
757 return 1;
758}
759
30b30c21 760/* Return the dynindex of a local dynamic symbol. */
42751cf3 761
30b30c21 762long
268b6b39
AM
763_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
30b30c21
RH
766{
767 struct elf_link_local_dynamic_entry *e;
768
769 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
770 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
771 return e->dynindx;
772 return -1;
773}
774
775/* This function is used to renumber the dynamic symbols, if some of
776 them are removed because they are marked as local. This is called
777 via elf_link_hash_traverse. */
778
b34976b6 779static bfd_boolean
268b6b39
AM
780elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
781 void *data)
42751cf3 782{
a50b1753 783 size_t *count = (size_t *) data;
30b30c21 784
6fa3860b
PB
785 if (h->forced_local)
786 return TRUE;
787
788 if (h->dynindx != -1)
789 h->dynindx = ++(*count);
790
791 return TRUE;
792}
793
794
795/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
796 STB_LOCAL binding. */
797
798static bfd_boolean
799elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
800 void *data)
801{
a50b1753 802 size_t *count = (size_t *) data;
6fa3860b 803
6fa3860b
PB
804 if (!h->forced_local)
805 return TRUE;
806
42751cf3 807 if (h->dynindx != -1)
30b30c21
RH
808 h->dynindx = ++(*count);
809
b34976b6 810 return TRUE;
42751cf3 811}
30b30c21 812
aee6f5b4
AO
813/* Return true if the dynamic symbol for a given section should be
814 omitted when creating a shared library. */
815bfd_boolean
816_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
817 struct bfd_link_info *info,
818 asection *p)
819{
74541ad4 820 struct elf_link_hash_table *htab;
ca55926c 821 asection *ip;
74541ad4 822
aee6f5b4
AO
823 switch (elf_section_data (p)->this_hdr.sh_type)
824 {
825 case SHT_PROGBITS:
826 case SHT_NOBITS:
827 /* If sh_type is yet undecided, assume it could be
828 SHT_PROGBITS/SHT_NOBITS. */
829 case SHT_NULL:
74541ad4
AM
830 htab = elf_hash_table (info);
831 if (p == htab->tls_sec)
832 return FALSE;
833
834 if (htab->text_index_section != NULL)
835 return p != htab->text_index_section && p != htab->data_index_section;
836
ca55926c 837 return (htab->dynobj != NULL
3d4d4302 838 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 839 && ip->output_section == p);
aee6f5b4
AO
840
841 /* There shouldn't be section relative relocations
842 against any other section. */
843 default:
844 return TRUE;
845 }
846}
847
062e2358 848/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
849 symbol for each output section, which come first. Next come symbols
850 which have been forced to local binding. Then all of the back-end
851 allocated local dynamic syms, followed by the rest of the global
852 symbols. */
30b30c21 853
554220db
AM
854static unsigned long
855_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
856 struct bfd_link_info *info,
857 unsigned long *section_sym_count)
30b30c21
RH
858{
859 unsigned long dynsymcount = 0;
860
0e1862bb
L
861 if (bfd_link_pic (info)
862 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 863 {
aee6f5b4 864 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
865 asection *p;
866 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 867 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
868 && (p->flags & SEC_ALLOC) != 0
869 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
870 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
871 else
872 elf_section_data (p)->dynindx = 0;
30b30c21 873 }
554220db 874 *section_sym_count = dynsymcount;
30b30c21 875
6fa3860b
PB
876 elf_link_hash_traverse (elf_hash_table (info),
877 elf_link_renumber_local_hash_table_dynsyms,
878 &dynsymcount);
879
30b30c21
RH
880 if (elf_hash_table (info)->dynlocal)
881 {
882 struct elf_link_local_dynamic_entry *p;
883 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
884 p->dynindx = ++dynsymcount;
885 }
886
887 elf_link_hash_traverse (elf_hash_table (info),
888 elf_link_renumber_hash_table_dynsyms,
889 &dynsymcount);
890
891 /* There is an unused NULL entry at the head of the table which
1a6e6083
L
892 we must account for in our count. We always create the dynsym
893 section, even if it is empty, with dynamic sections. */
894 if (elf_hash_table (info)->dynamic_sections_created)
30b30c21
RH
895 ++dynsymcount;
896
ccabcbe5
AM
897 elf_hash_table (info)->dynsymcount = dynsymcount;
898 return dynsymcount;
30b30c21 899}
252b5132 900
54ac0771
L
901/* Merge st_other field. */
902
903static void
904elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 905 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 906 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
907{
908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
909
910 /* If st_other has a processor-specific meaning, specific
cd3416da 911 code might be needed here. */
54ac0771
L
912 if (bed->elf_backend_merge_symbol_attribute)
913 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
914 dynamic);
915
cd3416da 916 if (!dynamic)
54ac0771 917 {
cd3416da
AM
918 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
919 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 920
cd3416da
AM
921 /* Keep the most constraining visibility. Leave the remainder
922 of the st_other field to elf_backend_merge_symbol_attribute. */
923 if (symvis - 1 < hvis - 1)
924 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 925 }
b8417128
AM
926 else if (definition
927 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
928 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 929 h->protected_def = 1;
54ac0771
L
930}
931
4f3fedcf
AM
932/* This function is called when we want to merge a new symbol with an
933 existing symbol. It handles the various cases which arise when we
934 find a definition in a dynamic object, or when there is already a
935 definition in a dynamic object. The new symbol is described by
936 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
937 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
938 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
939 of an old common symbol. We set OVERRIDE if the old symbol is
940 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
941 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
942 to change. By OK to change, we mean that we shouldn't warn if the
943 type or size does change. */
45d6a902 944
8a56bd02 945static bfd_boolean
268b6b39
AM
946_bfd_elf_merge_symbol (bfd *abfd,
947 struct bfd_link_info *info,
948 const char *name,
949 Elf_Internal_Sym *sym,
950 asection **psec,
951 bfd_vma *pvalue,
4f3fedcf
AM
952 struct elf_link_hash_entry **sym_hash,
953 bfd **poldbfd,
37a9e49a 954 bfd_boolean *pold_weak,
af44c138 955 unsigned int *pold_alignment,
268b6b39
AM
956 bfd_boolean *skip,
957 bfd_boolean *override,
958 bfd_boolean *type_change_ok,
6e33951e
L
959 bfd_boolean *size_change_ok,
960 bfd_boolean *matched)
252b5132 961{
7479dfd4 962 asection *sec, *oldsec;
45d6a902 963 struct elf_link_hash_entry *h;
90c984fc 964 struct elf_link_hash_entry *hi;
45d6a902
AM
965 struct elf_link_hash_entry *flip;
966 int bind;
967 bfd *oldbfd;
968 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 969 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 970 const struct elf_backend_data *bed;
6e33951e 971 char *new_version;
45d6a902
AM
972
973 *skip = FALSE;
974 *override = FALSE;
975
976 sec = *psec;
977 bind = ELF_ST_BIND (sym->st_info);
978
979 if (! bfd_is_und_section (sec))
980 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
981 else
982 h = ((struct elf_link_hash_entry *)
983 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
984 if (h == NULL)
985 return FALSE;
986 *sym_hash = h;
252b5132 987
88ba32a0
L
988 bed = get_elf_backend_data (abfd);
989
6e33951e 990 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 991 if (h->versioned != unversioned)
6e33951e 992 {
422f1182
L
993 /* Symbol version is unknown or versioned. */
994 new_version = strrchr (name, ELF_VER_CHR);
995 if (new_version)
996 {
997 if (h->versioned == unknown)
998 {
999 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1000 h->versioned = versioned_hidden;
1001 else
1002 h->versioned = versioned;
1003 }
1004 new_version += 1;
1005 if (new_version[0] == '\0')
1006 new_version = NULL;
1007 }
1008 else
1009 h->versioned = unversioned;
6e33951e 1010 }
422f1182
L
1011 else
1012 new_version = NULL;
6e33951e 1013
90c984fc
L
1014 /* For merging, we only care about real symbols. But we need to make
1015 sure that indirect symbol dynamic flags are updated. */
1016 hi = h;
45d6a902
AM
1017 while (h->root.type == bfd_link_hash_indirect
1018 || h->root.type == bfd_link_hash_warning)
1019 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1020
6e33951e
L
1021 if (!*matched)
1022 {
1023 if (hi == h || h->root.type == bfd_link_hash_new)
1024 *matched = TRUE;
1025 else
1026 {
ae7683d2 1027 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1028 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1029 true if the new symbol is only visible to the symbol with
6e33951e 1030 the same symbol version. */
422f1182
L
1031 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1032 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1033 if (!old_hidden && !new_hidden)
1034 /* The new symbol matches the existing symbol if both
1035 aren't hidden. */
1036 *matched = TRUE;
1037 else
1038 {
1039 /* OLD_VERSION is the symbol version of the existing
1040 symbol. */
422f1182
L
1041 char *old_version;
1042
1043 if (h->versioned >= versioned)
1044 old_version = strrchr (h->root.root.string,
1045 ELF_VER_CHR) + 1;
1046 else
1047 old_version = NULL;
6e33951e
L
1048
1049 /* The new symbol matches the existing symbol if they
1050 have the same symbol version. */
1051 *matched = (old_version == new_version
1052 || (old_version != NULL
1053 && new_version != NULL
1054 && strcmp (old_version, new_version) == 0));
1055 }
1056 }
1057 }
1058
934bce08
AM
1059 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1060 existing symbol. */
1061
1062 oldbfd = NULL;
1063 oldsec = NULL;
1064 switch (h->root.type)
1065 {
1066 default:
1067 break;
1068
1069 case bfd_link_hash_undefined:
1070 case bfd_link_hash_undefweak:
1071 oldbfd = h->root.u.undef.abfd;
1072 break;
1073
1074 case bfd_link_hash_defined:
1075 case bfd_link_hash_defweak:
1076 oldbfd = h->root.u.def.section->owner;
1077 oldsec = h->root.u.def.section;
1078 break;
1079
1080 case bfd_link_hash_common:
1081 oldbfd = h->root.u.c.p->section->owner;
1082 oldsec = h->root.u.c.p->section;
1083 if (pold_alignment)
1084 *pold_alignment = h->root.u.c.p->alignment_power;
1085 break;
1086 }
1087 if (poldbfd && *poldbfd == NULL)
1088 *poldbfd = oldbfd;
1089
1090 /* Differentiate strong and weak symbols. */
1091 newweak = bind == STB_WEAK;
1092 oldweak = (h->root.type == bfd_link_hash_defweak
1093 || h->root.type == bfd_link_hash_undefweak);
1094 if (pold_weak)
1095 *pold_weak = oldweak;
1096
1097 /* This code is for coping with dynamic objects, and is only useful
1098 if we are doing an ELF link. */
1099 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1100 return TRUE;
1101
40b36307 1102 /* We have to check it for every instance since the first few may be
ee659f1f 1103 references and not all compilers emit symbol type for undefined
40b36307
L
1104 symbols. */
1105 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1106
ee659f1f
AM
1107 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1108 respectively, is from a dynamic object. */
1109
1110 newdyn = (abfd->flags & DYNAMIC) != 0;
1111
1112 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1113 syms and defined syms in dynamic libraries respectively.
1114 ref_dynamic on the other hand can be set for a symbol defined in
1115 a dynamic library, and def_dynamic may not be set; When the
1116 definition in a dynamic lib is overridden by a definition in the
1117 executable use of the symbol in the dynamic lib becomes a
1118 reference to the executable symbol. */
1119 if (newdyn)
1120 {
1121 if (bfd_is_und_section (sec))
1122 {
1123 if (bind != STB_WEAK)
1124 {
1125 h->ref_dynamic_nonweak = 1;
1126 hi->ref_dynamic_nonweak = 1;
1127 }
1128 }
1129 else
1130 {
6e33951e
L
1131 /* Update the existing symbol only if they match. */
1132 if (*matched)
1133 h->dynamic_def = 1;
ee659f1f
AM
1134 hi->dynamic_def = 1;
1135 }
1136 }
1137
45d6a902
AM
1138 /* If we just created the symbol, mark it as being an ELF symbol.
1139 Other than that, there is nothing to do--there is no merge issue
1140 with a newly defined symbol--so we just return. */
1141
1142 if (h->root.type == bfd_link_hash_new)
252b5132 1143 {
f5385ebf 1144 h->non_elf = 0;
45d6a902
AM
1145 return TRUE;
1146 }
252b5132 1147
45d6a902
AM
1148 /* In cases involving weak versioned symbols, we may wind up trying
1149 to merge a symbol with itself. Catch that here, to avoid the
1150 confusion that results if we try to override a symbol with
1151 itself. The additional tests catch cases like
1152 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1153 dynamic object, which we do want to handle here. */
1154 if (abfd == oldbfd
895fa45f 1155 && (newweak || oldweak)
45d6a902 1156 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1157 || !h->def_regular))
45d6a902
AM
1158 return TRUE;
1159
707bba77 1160 olddyn = FALSE;
45d6a902
AM
1161 if (oldbfd != NULL)
1162 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1163 else if (oldsec != NULL)
45d6a902 1164 {
707bba77 1165 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1166 indices used by MIPS ELF. */
707bba77 1167 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1168 }
252b5132 1169
45d6a902
AM
1170 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1171 respectively, appear to be a definition rather than reference. */
1172
707bba77 1173 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1174
707bba77
AM
1175 olddef = (h->root.type != bfd_link_hash_undefined
1176 && h->root.type != bfd_link_hash_undefweak
202ac193 1177 && h->root.type != bfd_link_hash_common);
45d6a902 1178
0a36a439
L
1179 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1180 respectively, appear to be a function. */
1181
1182 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1184
1185 oldfunc = (h->type != STT_NOTYPE
1186 && bed->is_function_type (h->type));
1187
580a2b6e
L
1188 /* When we try to create a default indirect symbol from the dynamic
1189 definition with the default version, we skip it if its type and
40101021 1190 the type of existing regular definition mismatch. */
580a2b6e 1191 if (pold_alignment == NULL
580a2b6e
L
1192 && newdyn
1193 && newdef
1194 && !olddyn
4584ec12
L
1195 && (((olddef || h->root.type == bfd_link_hash_common)
1196 && ELF_ST_TYPE (sym->st_info) != h->type
1197 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1198 && h->type != STT_NOTYPE
1199 && !(newfunc && oldfunc))
1200 || (olddef
1201 && ((h->type == STT_GNU_IFUNC)
1202 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
580a2b6e
L
1203 {
1204 *skip = TRUE;
1205 return TRUE;
1206 }
1207
4c34aff8
AM
1208 /* Check TLS symbols. We don't check undefined symbols introduced
1209 by "ld -u" which have no type (and oldbfd NULL), and we don't
1210 check symbols from plugins because they also have no type. */
1211 if (oldbfd != NULL
1212 && (oldbfd->flags & BFD_PLUGIN) == 0
1213 && (abfd->flags & BFD_PLUGIN) == 0
1214 && ELF_ST_TYPE (sym->st_info) != h->type
1215 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1216 {
1217 bfd *ntbfd, *tbfd;
1218 bfd_boolean ntdef, tdef;
1219 asection *ntsec, *tsec;
1220
1221 if (h->type == STT_TLS)
1222 {
3b36f7e6 1223 ntbfd = abfd;
7479dfd4
L
1224 ntsec = sec;
1225 ntdef = newdef;
1226 tbfd = oldbfd;
1227 tsec = oldsec;
1228 tdef = olddef;
1229 }
1230 else
1231 {
1232 ntbfd = oldbfd;
1233 ntsec = oldsec;
1234 ntdef = olddef;
1235 tbfd = abfd;
1236 tsec = sec;
1237 tdef = newdef;
1238 }
1239
1240 if (tdef && ntdef)
1241 (*_bfd_error_handler)
191c0c42
AM
1242 (_("%s: TLS definition in %B section %A "
1243 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1244 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1245 else if (!tdef && !ntdef)
1246 (*_bfd_error_handler)
191c0c42
AM
1247 (_("%s: TLS reference in %B "
1248 "mismatches non-TLS reference in %B"),
7479dfd4
L
1249 tbfd, ntbfd, h->root.root.string);
1250 else if (tdef)
1251 (*_bfd_error_handler)
191c0c42
AM
1252 (_("%s: TLS definition in %B section %A "
1253 "mismatches non-TLS reference in %B"),
7479dfd4
L
1254 tbfd, tsec, ntbfd, h->root.root.string);
1255 else
1256 (*_bfd_error_handler)
191c0c42
AM
1257 (_("%s: TLS reference in %B "
1258 "mismatches non-TLS definition in %B section %A"),
7479dfd4
L
1259 tbfd, ntbfd, ntsec, h->root.root.string);
1260
1261 bfd_set_error (bfd_error_bad_value);
1262 return FALSE;
1263 }
1264
45d6a902
AM
1265 /* If the old symbol has non-default visibility, we ignore the new
1266 definition from a dynamic object. */
1267 if (newdyn
9c7a29a3 1268 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1269 && !bfd_is_und_section (sec))
1270 {
1271 *skip = TRUE;
1272 /* Make sure this symbol is dynamic. */
f5385ebf 1273 h->ref_dynamic = 1;
90c984fc 1274 hi->ref_dynamic = 1;
45d6a902
AM
1275 /* A protected symbol has external availability. Make sure it is
1276 recorded as dynamic.
1277
1278 FIXME: Should we check type and size for protected symbol? */
1279 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1280 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1281 else
1282 return TRUE;
1283 }
1284 else if (!newdyn
9c7a29a3 1285 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1286 && h->def_dynamic)
45d6a902
AM
1287 {
1288 /* If the new symbol with non-default visibility comes from a
1289 relocatable file and the old definition comes from a dynamic
1290 object, we remove the old definition. */
6c9b78e6 1291 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1292 {
1293 /* Handle the case where the old dynamic definition is
1294 default versioned. We need to copy the symbol info from
1295 the symbol with default version to the normal one if it
1296 was referenced before. */
1297 if (h->ref_regular)
1298 {
6c9b78e6 1299 hi->root.type = h->root.type;
d2dee3b2 1300 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1301 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1302
6c9b78e6 1303 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1304 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1305 {
aed81c4e
MR
1306 /* If the new symbol is hidden or internal, completely undo
1307 any dynamic link state. */
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
d2dee3b2
L
1311 }
1312 else
aed81c4e
MR
1313 h->ref_dynamic = 1;
1314
1315 h->def_dynamic = 0;
aed81c4e
MR
1316 /* FIXME: Should we check type and size for protected symbol? */
1317 h->size = 0;
1318 h->type = 0;
1319
6c9b78e6 1320 h = hi;
d2dee3b2
L
1321 }
1322 else
6c9b78e6 1323 h = hi;
d2dee3b2 1324 }
1de1a317 1325
f5eda473
AM
1326 /* If the old symbol was undefined before, then it will still be
1327 on the undefs list. If the new symbol is undefined or
1328 common, we can't make it bfd_link_hash_new here, because new
1329 undefined or common symbols will be added to the undefs list
1330 by _bfd_generic_link_add_one_symbol. Symbols may not be
1331 added twice to the undefs list. Also, if the new symbol is
1332 undefweak then we don't want to lose the strong undef. */
1333 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1334 {
1de1a317 1335 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1336 h->root.u.undef.abfd = abfd;
1337 }
1338 else
1339 {
1340 h->root.type = bfd_link_hash_new;
1341 h->root.u.undef.abfd = NULL;
1342 }
1343
f5eda473 1344 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1345 {
f5eda473
AM
1346 /* If the new symbol is hidden or internal, completely undo
1347 any dynamic link state. */
1348 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1349 h->forced_local = 0;
1350 h->ref_dynamic = 0;
45d6a902 1351 }
f5eda473
AM
1352 else
1353 h->ref_dynamic = 1;
1354 h->def_dynamic = 0;
45d6a902
AM
1355 /* FIXME: Should we check type and size for protected symbol? */
1356 h->size = 0;
1357 h->type = 0;
1358 return TRUE;
1359 }
14a793b2 1360
15b43f48
AM
1361 /* If a new weak symbol definition comes from a regular file and the
1362 old symbol comes from a dynamic library, we treat the new one as
1363 strong. Similarly, an old weak symbol definition from a regular
1364 file is treated as strong when the new symbol comes from a dynamic
1365 library. Further, an old weak symbol from a dynamic library is
1366 treated as strong if the new symbol is from a dynamic library.
1367 This reflects the way glibc's ld.so works.
1368
1369 Do this before setting *type_change_ok or *size_change_ok so that
1370 we warn properly when dynamic library symbols are overridden. */
1371
1372 if (newdef && !newdyn && olddyn)
0f8a2703 1373 newweak = FALSE;
15b43f48 1374 if (olddef && newdyn)
0f8a2703
AM
1375 oldweak = FALSE;
1376
d334575b 1377 /* Allow changes between different types of function symbol. */
0a36a439 1378 if (newfunc && oldfunc)
fcb93ecf
PB
1379 *type_change_ok = TRUE;
1380
79349b09
AM
1381 /* It's OK to change the type if either the existing symbol or the
1382 new symbol is weak. A type change is also OK if the old symbol
1383 is undefined and the new symbol is defined. */
252b5132 1384
79349b09
AM
1385 if (oldweak
1386 || newweak
1387 || (newdef
1388 && h->root.type == bfd_link_hash_undefined))
1389 *type_change_ok = TRUE;
1390
1391 /* It's OK to change the size if either the existing symbol or the
1392 new symbol is weak, or if the old symbol is undefined. */
1393
1394 if (*type_change_ok
1395 || h->root.type == bfd_link_hash_undefined)
1396 *size_change_ok = TRUE;
45d6a902 1397
45d6a902
AM
1398 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1399 symbol, respectively, appears to be a common symbol in a dynamic
1400 object. If a symbol appears in an uninitialized section, and is
1401 not weak, and is not a function, then it may be a common symbol
1402 which was resolved when the dynamic object was created. We want
1403 to treat such symbols specially, because they raise special
1404 considerations when setting the symbol size: if the symbol
1405 appears as a common symbol in a regular object, and the size in
1406 the regular object is larger, we must make sure that we use the
1407 larger size. This problematic case can always be avoided in C,
1408 but it must be handled correctly when using Fortran shared
1409 libraries.
1410
1411 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1412 likewise for OLDDYNCOMMON and OLDDEF.
1413
1414 Note that this test is just a heuristic, and that it is quite
1415 possible to have an uninitialized symbol in a shared object which
1416 is really a definition, rather than a common symbol. This could
1417 lead to some minor confusion when the symbol really is a common
1418 symbol in some regular object. However, I think it will be
1419 harmless. */
1420
1421 if (newdyn
1422 && newdef
79349b09 1423 && !newweak
45d6a902
AM
1424 && (sec->flags & SEC_ALLOC) != 0
1425 && (sec->flags & SEC_LOAD) == 0
1426 && sym->st_size > 0
0a36a439 1427 && !newfunc)
45d6a902
AM
1428 newdyncommon = TRUE;
1429 else
1430 newdyncommon = FALSE;
1431
1432 if (olddyn
1433 && olddef
1434 && h->root.type == bfd_link_hash_defined
f5385ebf 1435 && h->def_dynamic
45d6a902
AM
1436 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1437 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1438 && h->size > 0
0a36a439 1439 && !oldfunc)
45d6a902
AM
1440 olddyncommon = TRUE;
1441 else
1442 olddyncommon = FALSE;
1443
a4d8e49b
L
1444 /* We now know everything about the old and new symbols. We ask the
1445 backend to check if we can merge them. */
5d13b3b3
AM
1446 if (bed->merge_symbol != NULL)
1447 {
1448 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1449 return FALSE;
1450 sec = *psec;
1451 }
a4d8e49b 1452
45d6a902
AM
1453 /* If both the old and the new symbols look like common symbols in a
1454 dynamic object, set the size of the symbol to the larger of the
1455 two. */
1456
1457 if (olddyncommon
1458 && newdyncommon
1459 && sym->st_size != h->size)
1460 {
1461 /* Since we think we have two common symbols, issue a multiple
1462 common warning if desired. Note that we only warn if the
1463 size is different. If the size is the same, we simply let
1464 the old symbol override the new one as normally happens with
1465 symbols defined in dynamic objects. */
1466
1467 if (! ((*info->callbacks->multiple_common)
24f58f47 1468 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902 1469 return FALSE;
252b5132 1470
45d6a902
AM
1471 if (sym->st_size > h->size)
1472 h->size = sym->st_size;
252b5132 1473
45d6a902 1474 *size_change_ok = TRUE;
252b5132
RH
1475 }
1476
45d6a902
AM
1477 /* If we are looking at a dynamic object, and we have found a
1478 definition, we need to see if the symbol was already defined by
1479 some other object. If so, we want to use the existing
1480 definition, and we do not want to report a multiple symbol
1481 definition error; we do this by clobbering *PSEC to be
1482 bfd_und_section_ptr.
1483
1484 We treat a common symbol as a definition if the symbol in the
1485 shared library is a function, since common symbols always
1486 represent variables; this can cause confusion in principle, but
1487 any such confusion would seem to indicate an erroneous program or
1488 shared library. We also permit a common symbol in a regular
202ac193
L
1489 object to override a weak symbol in a shared object. A common
1490 symbol in executable also overrides a symbol in a shared object. */
45d6a902
AM
1491
1492 if (newdyn
1493 && newdef
77cfaee6 1494 && (olddef
45d6a902 1495 || (h->root.type == bfd_link_hash_common
202ac193
L
1496 && (newweak
1497 || newfunc
1498 || (!olddyn && bfd_link_executable (info))))))
45d6a902
AM
1499 {
1500 *override = TRUE;
1501 newdef = FALSE;
1502 newdyncommon = FALSE;
252b5132 1503
45d6a902
AM
1504 *psec = sec = bfd_und_section_ptr;
1505 *size_change_ok = TRUE;
252b5132 1506
45d6a902
AM
1507 /* If we get here when the old symbol is a common symbol, then
1508 we are explicitly letting it override a weak symbol or
1509 function in a dynamic object, and we don't want to warn about
1510 a type change. If the old symbol is a defined symbol, a type
1511 change warning may still be appropriate. */
252b5132 1512
45d6a902
AM
1513 if (h->root.type == bfd_link_hash_common)
1514 *type_change_ok = TRUE;
1515 }
1516
1517 /* Handle the special case of an old common symbol merging with a
1518 new symbol which looks like a common symbol in a shared object.
1519 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1520 common symbol, and let _bfd_generic_link_add_one_symbol do the
1521 right thing. */
45d6a902
AM
1522
1523 if (newdyncommon
1524 && h->root.type == bfd_link_hash_common)
1525 {
1526 *override = TRUE;
1527 newdef = FALSE;
1528 newdyncommon = FALSE;
1529 *pvalue = sym->st_size;
a4d8e49b 1530 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1531 *size_change_ok = TRUE;
1532 }
1533
c5e2cead 1534 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1535 if (newdef && olddef && newweak)
54ac0771 1536 {
35ed3f94 1537 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1538 if (!(oldbfd != NULL
1539 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1540 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1541 {
1542 newdef = FALSE;
1543 *skip = TRUE;
1544 }
54ac0771
L
1545
1546 /* Merge st_other. If the symbol already has a dynamic index,
1547 but visibility says it should not be visible, turn it into a
1548 local symbol. */
b8417128 1549 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1550 if (h->dynindx != -1)
1551 switch (ELF_ST_VISIBILITY (h->other))
1552 {
1553 case STV_INTERNAL:
1554 case STV_HIDDEN:
1555 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1556 break;
1557 }
1558 }
c5e2cead 1559
45d6a902
AM
1560 /* If the old symbol is from a dynamic object, and the new symbol is
1561 a definition which is not from a dynamic object, then the new
1562 symbol overrides the old symbol. Symbols from regular files
1563 always take precedence over symbols from dynamic objects, even if
1564 they are defined after the dynamic object in the link.
1565
1566 As above, we again permit a common symbol in a regular object to
1567 override a definition in a shared object if the shared object
0f8a2703 1568 symbol is a function or is weak. */
45d6a902
AM
1569
1570 flip = NULL;
77cfaee6 1571 if (!newdyn
45d6a902
AM
1572 && (newdef
1573 || (bfd_is_com_section (sec)
0a36a439 1574 && (oldweak || oldfunc)))
45d6a902
AM
1575 && olddyn
1576 && olddef
f5385ebf 1577 && h->def_dynamic)
45d6a902
AM
1578 {
1579 /* Change the hash table entry to undefined, and let
1580 _bfd_generic_link_add_one_symbol do the right thing with the
1581 new definition. */
1582
1583 h->root.type = bfd_link_hash_undefined;
1584 h->root.u.undef.abfd = h->root.u.def.section->owner;
1585 *size_change_ok = TRUE;
1586
1587 olddef = FALSE;
1588 olddyncommon = FALSE;
1589
1590 /* We again permit a type change when a common symbol may be
1591 overriding a function. */
1592
1593 if (bfd_is_com_section (sec))
0a36a439
L
1594 {
1595 if (oldfunc)
1596 {
1597 /* If a common symbol overrides a function, make sure
1598 that it isn't defined dynamically nor has type
1599 function. */
1600 h->def_dynamic = 0;
1601 h->type = STT_NOTYPE;
1602 }
1603 *type_change_ok = TRUE;
1604 }
45d6a902 1605
6c9b78e6
AM
1606 if (hi->root.type == bfd_link_hash_indirect)
1607 flip = hi;
45d6a902
AM
1608 else
1609 /* This union may have been set to be non-NULL when this symbol
1610 was seen in a dynamic object. We must force the union to be
1611 NULL, so that it is correct for a regular symbol. */
1612 h->verinfo.vertree = NULL;
1613 }
1614
1615 /* Handle the special case of a new common symbol merging with an
1616 old symbol that looks like it might be a common symbol defined in
1617 a shared object. Note that we have already handled the case in
1618 which a new common symbol should simply override the definition
1619 in the shared library. */
1620
1621 if (! newdyn
1622 && bfd_is_com_section (sec)
1623 && olddyncommon)
1624 {
1625 /* It would be best if we could set the hash table entry to a
1626 common symbol, but we don't know what to use for the section
1627 or the alignment. */
1628 if (! ((*info->callbacks->multiple_common)
24f58f47 1629 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
45d6a902
AM
1630 return FALSE;
1631
4cc11e76 1632 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1633 larger, pretend that the new symbol has its size. */
1634
1635 if (h->size > *pvalue)
1636 *pvalue = h->size;
1637
af44c138
L
1638 /* We need to remember the alignment required by the symbol
1639 in the dynamic object. */
1640 BFD_ASSERT (pold_alignment);
1641 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1642
1643 olddef = FALSE;
1644 olddyncommon = FALSE;
1645
1646 h->root.type = bfd_link_hash_undefined;
1647 h->root.u.undef.abfd = h->root.u.def.section->owner;
1648
1649 *size_change_ok = TRUE;
1650 *type_change_ok = TRUE;
1651
6c9b78e6
AM
1652 if (hi->root.type == bfd_link_hash_indirect)
1653 flip = hi;
45d6a902
AM
1654 else
1655 h->verinfo.vertree = NULL;
1656 }
1657
1658 if (flip != NULL)
1659 {
1660 /* Handle the case where we had a versioned symbol in a dynamic
1661 library and now find a definition in a normal object. In this
1662 case, we make the versioned symbol point to the normal one. */
45d6a902 1663 flip->root.type = h->root.type;
00cbee0a 1664 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1665 h->root.type = bfd_link_hash_indirect;
1666 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1667 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1668 if (h->def_dynamic)
45d6a902 1669 {
f5385ebf
AM
1670 h->def_dynamic = 0;
1671 flip->ref_dynamic = 1;
45d6a902
AM
1672 }
1673 }
1674
45d6a902
AM
1675 return TRUE;
1676}
1677
1678/* This function is called to create an indirect symbol from the
1679 default for the symbol with the default version if needed. The
4f3fedcf 1680 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1681 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1682
28caa186 1683static bfd_boolean
268b6b39
AM
1684_bfd_elf_add_default_symbol (bfd *abfd,
1685 struct bfd_link_info *info,
1686 struct elf_link_hash_entry *h,
1687 const char *name,
1688 Elf_Internal_Sym *sym,
4f3fedcf
AM
1689 asection *sec,
1690 bfd_vma value,
1691 bfd **poldbfd,
e3c9d234 1692 bfd_boolean *dynsym)
45d6a902
AM
1693{
1694 bfd_boolean type_change_ok;
1695 bfd_boolean size_change_ok;
1696 bfd_boolean skip;
1697 char *shortname;
1698 struct elf_link_hash_entry *hi;
1699 struct bfd_link_hash_entry *bh;
9c5bfbb7 1700 const struct elf_backend_data *bed;
45d6a902
AM
1701 bfd_boolean collect;
1702 bfd_boolean dynamic;
e3c9d234 1703 bfd_boolean override;
45d6a902
AM
1704 char *p;
1705 size_t len, shortlen;
ffd65175 1706 asection *tmp_sec;
6e33951e 1707 bfd_boolean matched;
45d6a902 1708
422f1182
L
1709 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1710 return TRUE;
1711
45d6a902
AM
1712 /* If this symbol has a version, and it is the default version, we
1713 create an indirect symbol from the default name to the fully
1714 decorated name. This will cause external references which do not
1715 specify a version to be bound to this version of the symbol. */
1716 p = strchr (name, ELF_VER_CHR);
422f1182
L
1717 if (h->versioned == unknown)
1718 {
1719 if (p == NULL)
1720 {
1721 h->versioned = unversioned;
1722 return TRUE;
1723 }
1724 else
1725 {
1726 if (p[1] != ELF_VER_CHR)
1727 {
1728 h->versioned = versioned_hidden;
1729 return TRUE;
1730 }
1731 else
1732 h->versioned = versioned;
1733 }
1734 }
4373f8af
L
1735 else
1736 {
1737 /* PR ld/19073: We may see an unversioned definition after the
1738 default version. */
1739 if (p == NULL)
1740 return TRUE;
1741 }
45d6a902 1742
45d6a902
AM
1743 bed = get_elf_backend_data (abfd);
1744 collect = bed->collect;
1745 dynamic = (abfd->flags & DYNAMIC) != 0;
1746
1747 shortlen = p - name;
a50b1753 1748 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1749 if (shortname == NULL)
1750 return FALSE;
1751 memcpy (shortname, name, shortlen);
1752 shortname[shortlen] = '\0';
1753
1754 /* We are going to create a new symbol. Merge it with any existing
1755 symbol with this name. For the purposes of the merge, act as
1756 though we were defining the symbol we just defined, although we
1757 actually going to define an indirect symbol. */
1758 type_change_ok = FALSE;
1759 size_change_ok = FALSE;
6e33951e 1760 matched = TRUE;
ffd65175
AM
1761 tmp_sec = sec;
1762 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1763 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1764 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1765 return FALSE;
1766
1767 if (skip)
1768 goto nondefault;
1769
1770 if (! override)
1771 {
c6e8a9a8 1772 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1773 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1774 {
1775 bh = &hi->root;
1776 if (! (_bfd_generic_link_add_one_symbol
1777 (info, abfd, shortname, BSF_INDIRECT,
1778 bfd_ind_section_ptr,
1779 0, name, FALSE, collect, &bh)))
1780 return FALSE;
1781 hi = (struct elf_link_hash_entry *) bh;
1782 }
45d6a902
AM
1783 }
1784 else
1785 {
1786 /* In this case the symbol named SHORTNAME is overriding the
1787 indirect symbol we want to add. We were planning on making
1788 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1789 is the name without a version. NAME is the fully versioned
1790 name, and it is the default version.
1791
1792 Overriding means that we already saw a definition for the
1793 symbol SHORTNAME in a regular object, and it is overriding
1794 the symbol defined in the dynamic object.
1795
1796 When this happens, we actually want to change NAME, the
1797 symbol we just added, to refer to SHORTNAME. This will cause
1798 references to NAME in the shared object to become references
1799 to SHORTNAME in the regular object. This is what we expect
1800 when we override a function in a shared object: that the
1801 references in the shared object will be mapped to the
1802 definition in the regular object. */
1803
1804 while (hi->root.type == bfd_link_hash_indirect
1805 || hi->root.type == bfd_link_hash_warning)
1806 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1807
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1810 if (h->def_dynamic)
45d6a902 1811 {
f5385ebf
AM
1812 h->def_dynamic = 0;
1813 hi->ref_dynamic = 1;
1814 if (hi->ref_regular
1815 || hi->def_regular)
45d6a902 1816 {
c152c796 1817 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1818 return FALSE;
1819 }
1820 }
1821
1822 /* Now set HI to H, so that the following code will set the
1823 other fields correctly. */
1824 hi = h;
1825 }
1826
fab4a87f
L
1827 /* Check if HI is a warning symbol. */
1828 if (hi->root.type == bfd_link_hash_warning)
1829 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1830
45d6a902
AM
1831 /* If there is a duplicate definition somewhere, then HI may not
1832 point to an indirect symbol. We will have reported an error to
1833 the user in that case. */
1834
1835 if (hi->root.type == bfd_link_hash_indirect)
1836 {
1837 struct elf_link_hash_entry *ht;
1838
45d6a902 1839 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1840 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 1841
68c88cd4
AM
1842 /* A reference to the SHORTNAME symbol from a dynamic library
1843 will be satisfied by the versioned symbol at runtime. In
1844 effect, we have a reference to the versioned symbol. */
1845 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1846 hi->dynamic_def |= ht->dynamic_def;
1847
45d6a902
AM
1848 /* See if the new flags lead us to realize that the symbol must
1849 be dynamic. */
1850 if (! *dynsym)
1851 {
1852 if (! dynamic)
1853 {
0e1862bb 1854 if (! bfd_link_executable (info)
90c984fc 1855 || hi->def_dynamic
f5385ebf 1856 || hi->ref_dynamic)
45d6a902
AM
1857 *dynsym = TRUE;
1858 }
1859 else
1860 {
f5385ebf 1861 if (hi->ref_regular)
45d6a902
AM
1862 *dynsym = TRUE;
1863 }
1864 }
1865 }
1866
1867 /* We also need to define an indirection from the nondefault version
1868 of the symbol. */
1869
1870nondefault:
1871 len = strlen (name);
a50b1753 1872 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
1873 if (shortname == NULL)
1874 return FALSE;
1875 memcpy (shortname, name, shortlen);
1876 memcpy (shortname + shortlen, p + 1, len - shortlen);
1877
1878 /* Once again, merge with any existing symbol. */
1879 type_change_ok = FALSE;
1880 size_change_ok = FALSE;
ffd65175
AM
1881 tmp_sec = sec;
1882 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 1883 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1884 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1885 return FALSE;
1886
1887 if (skip)
1888 return TRUE;
1889
1890 if (override)
1891 {
1892 /* Here SHORTNAME is a versioned name, so we don't expect to see
1893 the type of override we do in the case above unless it is
4cc11e76 1894 overridden by a versioned definition. */
45d6a902
AM
1895 if (hi->root.type != bfd_link_hash_defined
1896 && hi->root.type != bfd_link_hash_defweak)
1897 (*_bfd_error_handler)
d003868e
AM
1898 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1899 abfd, shortname);
45d6a902
AM
1900 }
1901 else
1902 {
1903 bh = &hi->root;
1904 if (! (_bfd_generic_link_add_one_symbol
1905 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1906 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1907 return FALSE;
1908 hi = (struct elf_link_hash_entry *) bh;
1909
1910 /* If there is a duplicate definition somewhere, then HI may not
1911 point to an indirect symbol. We will have reported an error
1912 to the user in that case. */
1913
1914 if (hi->root.type == bfd_link_hash_indirect)
1915 {
fcfa13d2 1916 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
1917 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1918 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
1919
1920 /* See if the new flags lead us to realize that the symbol
1921 must be dynamic. */
1922 if (! *dynsym)
1923 {
1924 if (! dynamic)
1925 {
0e1862bb 1926 if (! bfd_link_executable (info)
f5385ebf 1927 || hi->ref_dynamic)
45d6a902
AM
1928 *dynsym = TRUE;
1929 }
1930 else
1931 {
f5385ebf 1932 if (hi->ref_regular)
45d6a902
AM
1933 *dynsym = TRUE;
1934 }
1935 }
1936 }
1937 }
1938
1939 return TRUE;
1940}
1941\f
1942/* This routine is used to export all defined symbols into the dynamic
1943 symbol table. It is called via elf_link_hash_traverse. */
1944
28caa186 1945static bfd_boolean
268b6b39 1946_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1947{
a50b1753 1948 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
1949
1950 /* Ignore indirect symbols. These are added by the versioning code. */
1951 if (h->root.type == bfd_link_hash_indirect)
1952 return TRUE;
1953
7686d77d
AM
1954 /* Ignore this if we won't export it. */
1955 if (!eif->info->export_dynamic && !h->dynamic)
1956 return TRUE;
45d6a902
AM
1957
1958 if (h->dynindx == -1
fd91d419
L
1959 && (h->def_regular || h->ref_regular)
1960 && ! bfd_hide_sym_by_version (eif->info->version_info,
1961 h->root.root.string))
45d6a902 1962 {
fd91d419 1963 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 1964 {
fd91d419
L
1965 eif->failed = TRUE;
1966 return FALSE;
45d6a902
AM
1967 }
1968 }
1969
1970 return TRUE;
1971}
1972\f
1973/* Look through the symbols which are defined in other shared
1974 libraries and referenced here. Update the list of version
1975 dependencies. This will be put into the .gnu.version_r section.
1976 This function is called via elf_link_hash_traverse. */
1977
28caa186 1978static bfd_boolean
268b6b39
AM
1979_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1980 void *data)
45d6a902 1981{
a50b1753 1982 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
1983 Elf_Internal_Verneed *t;
1984 Elf_Internal_Vernaux *a;
1985 bfd_size_type amt;
1986
45d6a902
AM
1987 /* We only care about symbols defined in shared objects with version
1988 information. */
f5385ebf
AM
1989 if (!h->def_dynamic
1990 || h->def_regular
45d6a902 1991 || h->dynindx == -1
7b20f099
AM
1992 || h->verinfo.verdef == NULL
1993 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1994 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
1995 return TRUE;
1996
1997 /* See if we already know about this version. */
28caa186
AM
1998 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1999 t != NULL;
2000 t = t->vn_nextref)
45d6a902
AM
2001 {
2002 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2003 continue;
2004
2005 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2006 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2007 return TRUE;
2008
2009 break;
2010 }
2011
2012 /* This is a new version. Add it to tree we are building. */
2013
2014 if (t == NULL)
2015 {
2016 amt = sizeof *t;
a50b1753 2017 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2018 if (t == NULL)
2019 {
2020 rinfo->failed = TRUE;
2021 return FALSE;
2022 }
2023
2024 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2025 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2026 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2027 }
2028
2029 amt = sizeof *a;
a50b1753 2030 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2031 if (a == NULL)
2032 {
2033 rinfo->failed = TRUE;
2034 return FALSE;
2035 }
45d6a902
AM
2036
2037 /* Note that we are copying a string pointer here, and testing it
2038 above. If bfd_elf_string_from_elf_section is ever changed to
2039 discard the string data when low in memory, this will have to be
2040 fixed. */
2041 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2042
2043 a->vna_flags = h->verinfo.verdef->vd_flags;
2044 a->vna_nextptr = t->vn_auxptr;
2045
2046 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2047 ++rinfo->vers;
2048
2049 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2050
2051 t->vn_auxptr = a;
2052
2053 return TRUE;
2054}
2055
2056/* Figure out appropriate versions for all the symbols. We may not
2057 have the version number script until we have read all of the input
2058 files, so until that point we don't know which symbols should be
2059 local. This function is called via elf_link_hash_traverse. */
2060
28caa186 2061static bfd_boolean
268b6b39 2062_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2063{
28caa186 2064 struct elf_info_failed *sinfo;
45d6a902 2065 struct bfd_link_info *info;
9c5bfbb7 2066 const struct elf_backend_data *bed;
45d6a902
AM
2067 struct elf_info_failed eif;
2068 char *p;
2069 bfd_size_type amt;
2070
a50b1753 2071 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2072 info = sinfo->info;
2073
45d6a902
AM
2074 /* Fix the symbol flags. */
2075 eif.failed = FALSE;
2076 eif.info = info;
2077 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2078 {
2079 if (eif.failed)
2080 sinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 /* We only need version numbers for symbols defined in regular
2085 objects. */
f5385ebf 2086 if (!h->def_regular)
45d6a902
AM
2087 return TRUE;
2088
28caa186 2089 bed = get_elf_backend_data (info->output_bfd);
45d6a902
AM
2090 p = strchr (h->root.root.string, ELF_VER_CHR);
2091 if (p != NULL && h->verinfo.vertree == NULL)
2092 {
2093 struct bfd_elf_version_tree *t;
45d6a902 2094
45d6a902
AM
2095 ++p;
2096 if (*p == ELF_VER_CHR)
6e33951e 2097 ++p;
45d6a902
AM
2098
2099 /* If there is no version string, we can just return out. */
2100 if (*p == '\0')
6e33951e 2101 return TRUE;
45d6a902
AM
2102
2103 /* Look for the version. If we find it, it is no longer weak. */
fd91d419 2104 for (t = sinfo->info->version_info; t != NULL; t = t->next)
45d6a902
AM
2105 {
2106 if (strcmp (t->name, p) == 0)
2107 {
2108 size_t len;
2109 char *alc;
2110 struct bfd_elf_version_expr *d;
2111
2112 len = p - h->root.root.string;
a50b1753 2113 alc = (char *) bfd_malloc (len);
45d6a902 2114 if (alc == NULL)
14b1c01e
AM
2115 {
2116 sinfo->failed = TRUE;
2117 return FALSE;
2118 }
45d6a902
AM
2119 memcpy (alc, h->root.root.string, len - 1);
2120 alc[len - 1] = '\0';
2121 if (alc[len - 2] == ELF_VER_CHR)
2122 alc[len - 2] = '\0';
2123
2124 h->verinfo.vertree = t;
2125 t->used = TRUE;
2126 d = NULL;
2127
108ba305
JJ
2128 if (t->globals.list != NULL)
2129 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
2130
2131 /* See if there is anything to force this symbol to
2132 local scope. */
108ba305 2133 if (d == NULL && t->locals.list != NULL)
45d6a902 2134 {
108ba305
JJ
2135 d = (*t->match) (&t->locals, NULL, alc);
2136 if (d != NULL
2137 && h->dynindx != -1
108ba305
JJ
2138 && ! info->export_dynamic)
2139 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2140 }
2141
2142 free (alc);
2143 break;
2144 }
2145 }
2146
2147 /* If we are building an application, we need to create a
2148 version node for this version. */
0e1862bb 2149 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2150 {
2151 struct bfd_elf_version_tree **pp;
2152 int version_index;
2153
2154 /* If we aren't going to export this symbol, we don't need
2155 to worry about it. */
2156 if (h->dynindx == -1)
2157 return TRUE;
2158
2159 amt = sizeof *t;
a50b1753 2160 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
45d6a902
AM
2161 if (t == NULL)
2162 {
2163 sinfo->failed = TRUE;
2164 return FALSE;
2165 }
2166
45d6a902 2167 t->name = p;
45d6a902
AM
2168 t->name_indx = (unsigned int) -1;
2169 t->used = TRUE;
2170
2171 version_index = 1;
2172 /* Don't count anonymous version tag. */
fd91d419
L
2173 if (sinfo->info->version_info != NULL
2174 && sinfo->info->version_info->vernum == 0)
45d6a902 2175 version_index = 0;
fd91d419
L
2176 for (pp = &sinfo->info->version_info;
2177 *pp != NULL;
2178 pp = &(*pp)->next)
45d6a902
AM
2179 ++version_index;
2180 t->vernum = version_index;
2181
2182 *pp = t;
2183
2184 h->verinfo.vertree = t;
2185 }
2186 else if (t == NULL)
2187 {
2188 /* We could not find the version for a symbol when
2189 generating a shared archive. Return an error. */
2190 (*_bfd_error_handler)
c55fe096 2191 (_("%B: version node not found for symbol %s"),
28caa186 2192 info->output_bfd, h->root.root.string);
45d6a902
AM
2193 bfd_set_error (bfd_error_bad_value);
2194 sinfo->failed = TRUE;
2195 return FALSE;
2196 }
45d6a902
AM
2197 }
2198
2199 /* If we don't have a version for this symbol, see if we can find
2200 something. */
fd91d419 2201 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
45d6a902 2202 {
1e8fa21e 2203 bfd_boolean hide;
ae5a3597 2204
fd91d419
L
2205 h->verinfo.vertree
2206 = bfd_find_version_for_sym (sinfo->info->version_info,
2207 h->root.root.string, &hide);
1e8fa21e
AM
2208 if (h->verinfo.vertree != NULL && hide)
2209 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2210 }
2211
2212 return TRUE;
2213}
2214\f
45d6a902
AM
2215/* Read and swap the relocs from the section indicated by SHDR. This
2216 may be either a REL or a RELA section. The relocations are
2217 translated into RELA relocations and stored in INTERNAL_RELOCS,
2218 which should have already been allocated to contain enough space.
2219 The EXTERNAL_RELOCS are a buffer where the external form of the
2220 relocations should be stored.
2221
2222 Returns FALSE if something goes wrong. */
2223
2224static bfd_boolean
268b6b39 2225elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2226 asection *sec,
268b6b39
AM
2227 Elf_Internal_Shdr *shdr,
2228 void *external_relocs,
2229 Elf_Internal_Rela *internal_relocs)
45d6a902 2230{
9c5bfbb7 2231 const struct elf_backend_data *bed;
268b6b39 2232 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2233 const bfd_byte *erela;
2234 const bfd_byte *erelaend;
2235 Elf_Internal_Rela *irela;
243ef1e0
L
2236 Elf_Internal_Shdr *symtab_hdr;
2237 size_t nsyms;
45d6a902 2238
45d6a902
AM
2239 /* Position ourselves at the start of the section. */
2240 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2241 return FALSE;
2242
2243 /* Read the relocations. */
2244 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2245 return FALSE;
2246
243ef1e0 2247 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2248 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2249
45d6a902
AM
2250 bed = get_elf_backend_data (abfd);
2251
2252 /* Convert the external relocations to the internal format. */
2253 if (shdr->sh_entsize == bed->s->sizeof_rel)
2254 swap_in = bed->s->swap_reloc_in;
2255 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2256 swap_in = bed->s->swap_reloca_in;
2257 else
2258 {
2259 bfd_set_error (bfd_error_wrong_format);
2260 return FALSE;
2261 }
2262
a50b1753 2263 erela = (const bfd_byte *) external_relocs;
51992aec 2264 erelaend = erela + shdr->sh_size;
45d6a902
AM
2265 irela = internal_relocs;
2266 while (erela < erelaend)
2267 {
243ef1e0
L
2268 bfd_vma r_symndx;
2269
45d6a902 2270 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2271 r_symndx = ELF32_R_SYM (irela->r_info);
2272 if (bed->s->arch_size == 64)
2273 r_symndx >>= 24;
ce98a316
NC
2274 if (nsyms > 0)
2275 {
2276 if ((size_t) r_symndx >= nsyms)
2277 {
2278 (*_bfd_error_handler)
2279 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2280 " for offset 0x%lx in section `%A'"),
2281 abfd, sec,
2282 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2283 bfd_set_error (bfd_error_bad_value);
2284 return FALSE;
2285 }
2286 }
cf35638d 2287 else if (r_symndx != STN_UNDEF)
243ef1e0
L
2288 {
2289 (*_bfd_error_handler)
ce98a316
NC
2290 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2291 " when the object file has no symbol table"),
d003868e
AM
2292 abfd, sec,
2293 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2294 bfd_set_error (bfd_error_bad_value);
2295 return FALSE;
2296 }
45d6a902
AM
2297 irela += bed->s->int_rels_per_ext_rel;
2298 erela += shdr->sh_entsize;
2299 }
2300
2301 return TRUE;
2302}
2303
2304/* Read and swap the relocs for a section O. They may have been
2305 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2306 not NULL, they are used as buffers to read into. They are known to
2307 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2308 the return value is allocated using either malloc or bfd_alloc,
2309 according to the KEEP_MEMORY argument. If O has two relocation
2310 sections (both REL and RELA relocations), then the REL_HDR
2311 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2312 RELA_HDR relocations. */
45d6a902
AM
2313
2314Elf_Internal_Rela *
268b6b39
AM
2315_bfd_elf_link_read_relocs (bfd *abfd,
2316 asection *o,
2317 void *external_relocs,
2318 Elf_Internal_Rela *internal_relocs,
2319 bfd_boolean keep_memory)
45d6a902 2320{
268b6b39 2321 void *alloc1 = NULL;
45d6a902 2322 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2323 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2324 struct bfd_elf_section_data *esdo = elf_section_data (o);
2325 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2326
d4730f92
BS
2327 if (esdo->relocs != NULL)
2328 return esdo->relocs;
45d6a902
AM
2329
2330 if (o->reloc_count == 0)
2331 return NULL;
2332
45d6a902
AM
2333 if (internal_relocs == NULL)
2334 {
2335 bfd_size_type size;
2336
2337 size = o->reloc_count;
2338 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2339 if (keep_memory)
a50b1753 2340 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2341 else
a50b1753 2342 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2343 if (internal_relocs == NULL)
2344 goto error_return;
2345 }
2346
2347 if (external_relocs == NULL)
2348 {
d4730f92
BS
2349 bfd_size_type size = 0;
2350
2351 if (esdo->rel.hdr)
2352 size += esdo->rel.hdr->sh_size;
2353 if (esdo->rela.hdr)
2354 size += esdo->rela.hdr->sh_size;
45d6a902 2355
268b6b39 2356 alloc1 = bfd_malloc (size);
45d6a902
AM
2357 if (alloc1 == NULL)
2358 goto error_return;
2359 external_relocs = alloc1;
2360 }
2361
d4730f92
BS
2362 internal_rela_relocs = internal_relocs;
2363 if (esdo->rel.hdr)
2364 {
2365 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2366 external_relocs,
2367 internal_relocs))
2368 goto error_return;
2369 external_relocs = (((bfd_byte *) external_relocs)
2370 + esdo->rel.hdr->sh_size);
2371 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2372 * bed->s->int_rels_per_ext_rel);
2373 }
2374
2375 if (esdo->rela.hdr
2376 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2377 external_relocs,
2378 internal_rela_relocs)))
45d6a902
AM
2379 goto error_return;
2380
2381 /* Cache the results for next time, if we can. */
2382 if (keep_memory)
d4730f92 2383 esdo->relocs = internal_relocs;
45d6a902
AM
2384
2385 if (alloc1 != NULL)
2386 free (alloc1);
2387
2388 /* Don't free alloc2, since if it was allocated we are passing it
2389 back (under the name of internal_relocs). */
2390
2391 return internal_relocs;
2392
2393 error_return:
2394 if (alloc1 != NULL)
2395 free (alloc1);
2396 if (alloc2 != NULL)
4dd07732
AM
2397 {
2398 if (keep_memory)
2399 bfd_release (abfd, alloc2);
2400 else
2401 free (alloc2);
2402 }
45d6a902
AM
2403 return NULL;
2404}
2405
2406/* Compute the size of, and allocate space for, REL_HDR which is the
2407 section header for a section containing relocations for O. */
2408
28caa186 2409static bfd_boolean
268b6b39 2410_bfd_elf_link_size_reloc_section (bfd *abfd,
d4730f92 2411 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2412{
d4730f92 2413 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2414
2415 /* That allows us to calculate the size of the section. */
d4730f92 2416 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2417
2418 /* The contents field must last into write_object_contents, so we
2419 allocate it with bfd_alloc rather than malloc. Also since we
2420 cannot be sure that the contents will actually be filled in,
2421 we zero the allocated space. */
a50b1753 2422 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2423 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2424 return FALSE;
2425
d4730f92 2426 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2427 {
2428 struct elf_link_hash_entry **p;
2429
ca4be51c
AM
2430 p = ((struct elf_link_hash_entry **)
2431 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2432 if (p == NULL)
2433 return FALSE;
2434
d4730f92 2435 reldata->hashes = p;
45d6a902
AM
2436 }
2437
2438 return TRUE;
2439}
2440
2441/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2442 originated from the section given by INPUT_REL_HDR) to the
2443 OUTPUT_BFD. */
2444
2445bfd_boolean
268b6b39
AM
2446_bfd_elf_link_output_relocs (bfd *output_bfd,
2447 asection *input_section,
2448 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2449 Elf_Internal_Rela *internal_relocs,
2450 struct elf_link_hash_entry **rel_hash
2451 ATTRIBUTE_UNUSED)
45d6a902
AM
2452{
2453 Elf_Internal_Rela *irela;
2454 Elf_Internal_Rela *irelaend;
2455 bfd_byte *erel;
d4730f92 2456 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2457 asection *output_section;
9c5bfbb7 2458 const struct elf_backend_data *bed;
268b6b39 2459 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2460 struct bfd_elf_section_data *esdo;
45d6a902
AM
2461
2462 output_section = input_section->output_section;
45d6a902 2463
d4730f92
BS
2464 bed = get_elf_backend_data (output_bfd);
2465 esdo = elf_section_data (output_section);
2466 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2467 {
d4730f92
BS
2468 output_reldata = &esdo->rel;
2469 swap_out = bed->s->swap_reloc_out;
45d6a902 2470 }
d4730f92
BS
2471 else if (esdo->rela.hdr
2472 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2473 {
d4730f92
BS
2474 output_reldata = &esdo->rela;
2475 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2476 }
2477 else
2478 {
2479 (*_bfd_error_handler)
d003868e
AM
2480 (_("%B: relocation size mismatch in %B section %A"),
2481 output_bfd, input_section->owner, input_section);
297d8443 2482 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2483 return FALSE;
2484 }
2485
d4730f92
BS
2486 erel = output_reldata->hdr->contents;
2487 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2488 irela = internal_relocs;
2489 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2490 * bed->s->int_rels_per_ext_rel);
2491 while (irela < irelaend)
2492 {
2493 (*swap_out) (output_bfd, irela, erel);
2494 irela += bed->s->int_rels_per_ext_rel;
2495 erel += input_rel_hdr->sh_entsize;
2496 }
2497
2498 /* Bump the counter, so that we know where to add the next set of
2499 relocations. */
d4730f92 2500 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2501
2502 return TRUE;
2503}
2504\f
508c3946
L
2505/* Make weak undefined symbols in PIE dynamic. */
2506
2507bfd_boolean
2508_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2509 struct elf_link_hash_entry *h)
2510{
0e1862bb 2511 if (bfd_link_pie (info)
508c3946
L
2512 && h->dynindx == -1
2513 && h->root.type == bfd_link_hash_undefweak)
2514 return bfd_elf_link_record_dynamic_symbol (info, h);
2515
2516 return TRUE;
2517}
2518
45d6a902
AM
2519/* Fix up the flags for a symbol. This handles various cases which
2520 can only be fixed after all the input files are seen. This is
2521 currently called by both adjust_dynamic_symbol and
2522 assign_sym_version, which is unnecessary but perhaps more robust in
2523 the face of future changes. */
2524
28caa186 2525static bfd_boolean
268b6b39
AM
2526_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2527 struct elf_info_failed *eif)
45d6a902 2528{
33774f08 2529 const struct elf_backend_data *bed;
508c3946 2530
45d6a902
AM
2531 /* If this symbol was mentioned in a non-ELF file, try to set
2532 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2533 permit a non-ELF file to correctly refer to a symbol defined in
2534 an ELF dynamic object. */
f5385ebf 2535 if (h->non_elf)
45d6a902
AM
2536 {
2537 while (h->root.type == bfd_link_hash_indirect)
2538 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2539
2540 if (h->root.type != bfd_link_hash_defined
2541 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2542 {
2543 h->ref_regular = 1;
2544 h->ref_regular_nonweak = 1;
2545 }
45d6a902
AM
2546 else
2547 {
2548 if (h->root.u.def.section->owner != NULL
2549 && (bfd_get_flavour (h->root.u.def.section->owner)
2550 == bfd_target_elf_flavour))
f5385ebf
AM
2551 {
2552 h->ref_regular = 1;
2553 h->ref_regular_nonweak = 1;
2554 }
45d6a902 2555 else
f5385ebf 2556 h->def_regular = 1;
45d6a902
AM
2557 }
2558
2559 if (h->dynindx == -1
f5385ebf
AM
2560 && (h->def_dynamic
2561 || h->ref_dynamic))
45d6a902 2562 {
c152c796 2563 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2564 {
2565 eif->failed = TRUE;
2566 return FALSE;
2567 }
2568 }
2569 }
2570 else
2571 {
f5385ebf 2572 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2573 was first seen in a non-ELF file. Fortunately, if the symbol
2574 was first seen in an ELF file, we're probably OK unless the
2575 symbol was defined in a non-ELF file. Catch that case here.
2576 FIXME: We're still in trouble if the symbol was first seen in
2577 a dynamic object, and then later in a non-ELF regular object. */
2578 if ((h->root.type == bfd_link_hash_defined
2579 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2580 && !h->def_regular
45d6a902
AM
2581 && (h->root.u.def.section->owner != NULL
2582 ? (bfd_get_flavour (h->root.u.def.section->owner)
2583 != bfd_target_elf_flavour)
2584 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2585 && !h->def_dynamic)))
2586 h->def_regular = 1;
45d6a902
AM
2587 }
2588
508c3946 2589 /* Backend specific symbol fixup. */
33774f08
AM
2590 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2591 if (bed->elf_backend_fixup_symbol
2592 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2593 return FALSE;
508c3946 2594
45d6a902
AM
2595 /* If this is a final link, and the symbol was defined as a common
2596 symbol in a regular object file, and there was no definition in
2597 any dynamic object, then the linker will have allocated space for
f5385ebf 2598 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2599 flag will not have been set. */
2600 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2601 && !h->def_regular
2602 && h->ref_regular
2603 && !h->def_dynamic
96f29d96 2604 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2605 h->def_regular = 1;
45d6a902
AM
2606
2607 /* If -Bsymbolic was used (which means to bind references to global
2608 symbols to the definition within the shared object), and this
2609 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2610 need a PLT entry. Likewise, if the symbol has non-default
2611 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2612 will force it local. */
f5385ebf 2613 if (h->needs_plt
0e1862bb 2614 && bfd_link_pic (eif->info)
0eddce27 2615 && is_elf_hash_table (eif->info->hash)
55255dae 2616 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2617 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2618 && h->def_regular)
45d6a902 2619 {
45d6a902
AM
2620 bfd_boolean force_local;
2621
45d6a902
AM
2622 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2623 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2624 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2625 }
2626
2627 /* If a weak undefined symbol has non-default visibility, we also
2628 hide it from the dynamic linker. */
9c7a29a3 2629 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2630 && h->root.type == bfd_link_hash_undefweak)
33774f08 2631 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2632
2633 /* If this is a weak defined symbol in a dynamic object, and we know
2634 the real definition in the dynamic object, copy interesting flags
2635 over to the real definition. */
f6e332e6 2636 if (h->u.weakdef != NULL)
45d6a902 2637 {
45d6a902
AM
2638 /* If the real definition is defined by a regular object file,
2639 don't do anything special. See the longer description in
2640 _bfd_elf_adjust_dynamic_symbol, below. */
4e6b54a6 2641 if (h->u.weakdef->def_regular)
f6e332e6 2642 h->u.weakdef = NULL;
45d6a902 2643 else
a26587ba 2644 {
4e6b54a6
AM
2645 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2646
2647 while (h->root.type == bfd_link_hash_indirect)
2648 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2649
2650 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2651 || h->root.type == bfd_link_hash_defweak);
2652 BFD_ASSERT (weakdef->def_dynamic);
a26587ba
RS
2653 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2654 || weakdef->root.type == bfd_link_hash_defweak);
2655 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2656 }
45d6a902
AM
2657 }
2658
2659 return TRUE;
2660}
2661
2662/* Make the backend pick a good value for a dynamic symbol. This is
2663 called via elf_link_hash_traverse, and also calls itself
2664 recursively. */
2665
28caa186 2666static bfd_boolean
268b6b39 2667_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2668{
a50b1753 2669 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902 2670 bfd *dynobj;
9c5bfbb7 2671 const struct elf_backend_data *bed;
45d6a902 2672
0eddce27 2673 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2674 return FALSE;
2675
45d6a902
AM
2676 /* Ignore indirect symbols. These are added by the versioning code. */
2677 if (h->root.type == bfd_link_hash_indirect)
2678 return TRUE;
2679
2680 /* Fix the symbol flags. */
2681 if (! _bfd_elf_fix_symbol_flags (h, eif))
2682 return FALSE;
2683
2684 /* If this symbol does not require a PLT entry, and it is not
2685 defined by a dynamic object, or is not referenced by a regular
2686 object, ignore it. We do have to handle a weak defined symbol,
2687 even if no regular object refers to it, if we decided to add it
2688 to the dynamic symbol table. FIXME: Do we normally need to worry
2689 about symbols which are defined by one dynamic object and
2690 referenced by another one? */
f5385ebf 2691 if (!h->needs_plt
91e21fb7 2692 && h->type != STT_GNU_IFUNC
f5385ebf
AM
2693 && (h->def_regular
2694 || !h->def_dynamic
2695 || (!h->ref_regular
f6e332e6 2696 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2697 {
a6aa5195 2698 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2699 return TRUE;
2700 }
2701
2702 /* If we've already adjusted this symbol, don't do it again. This
2703 can happen via a recursive call. */
f5385ebf 2704 if (h->dynamic_adjusted)
45d6a902
AM
2705 return TRUE;
2706
2707 /* Don't look at this symbol again. Note that we must set this
2708 after checking the above conditions, because we may look at a
2709 symbol once, decide not to do anything, and then get called
2710 recursively later after REF_REGULAR is set below. */
f5385ebf 2711 h->dynamic_adjusted = 1;
45d6a902
AM
2712
2713 /* If this is a weak definition, and we know a real definition, and
2714 the real symbol is not itself defined by a regular object file,
2715 then get a good value for the real definition. We handle the
2716 real symbol first, for the convenience of the backend routine.
2717
2718 Note that there is a confusing case here. If the real definition
2719 is defined by a regular object file, we don't get the real symbol
2720 from the dynamic object, but we do get the weak symbol. If the
2721 processor backend uses a COPY reloc, then if some routine in the
2722 dynamic object changes the real symbol, we will not see that
2723 change in the corresponding weak symbol. This is the way other
2724 ELF linkers work as well, and seems to be a result of the shared
2725 library model.
2726
2727 I will clarify this issue. Most SVR4 shared libraries define the
2728 variable _timezone and define timezone as a weak synonym. The
2729 tzset call changes _timezone. If you write
2730 extern int timezone;
2731 int _timezone = 5;
2732 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2733 you might expect that, since timezone is a synonym for _timezone,
2734 the same number will print both times. However, if the processor
2735 backend uses a COPY reloc, then actually timezone will be copied
2736 into your process image, and, since you define _timezone
2737 yourself, _timezone will not. Thus timezone and _timezone will
2738 wind up at different memory locations. The tzset call will set
2739 _timezone, leaving timezone unchanged. */
2740
f6e332e6 2741 if (h->u.weakdef != NULL)
45d6a902 2742 {
ec24dc88
AM
2743 /* If we get to this point, there is an implicit reference to
2744 H->U.WEAKDEF by a regular object file via the weak symbol H. */
f6e332e6 2745 h->u.weakdef->ref_regular = 1;
45d6a902 2746
ec24dc88
AM
2747 /* Ensure that the backend adjust_dynamic_symbol function sees
2748 H->U.WEAKDEF before H by recursively calling ourselves. */
f6e332e6 2749 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2750 return FALSE;
2751 }
2752
2753 /* If a symbol has no type and no size and does not require a PLT
2754 entry, then we are probably about to do the wrong thing here: we
2755 are probably going to create a COPY reloc for an empty object.
2756 This case can arise when a shared object is built with assembly
2757 code, and the assembly code fails to set the symbol type. */
2758 if (h->size == 0
2759 && h->type == STT_NOTYPE
f5385ebf 2760 && !h->needs_plt)
45d6a902
AM
2761 (*_bfd_error_handler)
2762 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2763 h->root.root.string);
2764
2765 dynobj = elf_hash_table (eif->info)->dynobj;
2766 bed = get_elf_backend_data (dynobj);
e7c33416 2767
45d6a902
AM
2768 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2769 {
2770 eif->failed = TRUE;
2771 return FALSE;
2772 }
2773
2774 return TRUE;
2775}
2776
027297b7
L
2777/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2778 DYNBSS. */
2779
2780bfd_boolean
6cabe1ea
AM
2781_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2782 struct elf_link_hash_entry *h,
027297b7
L
2783 asection *dynbss)
2784{
91ac5911 2785 unsigned int power_of_two;
027297b7
L
2786 bfd_vma mask;
2787 asection *sec = h->root.u.def.section;
2788
2789 /* The section aligment of definition is the maximum alignment
91ac5911
L
2790 requirement of symbols defined in the section. Since we don't
2791 know the symbol alignment requirement, we start with the
2792 maximum alignment and check low bits of the symbol address
2793 for the minimum alignment. */
2794 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2795 mask = ((bfd_vma) 1 << power_of_two) - 1;
2796 while ((h->root.u.def.value & mask) != 0)
2797 {
2798 mask >>= 1;
2799 --power_of_two;
2800 }
027297b7 2801
91ac5911
L
2802 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2803 dynbss))
027297b7
L
2804 {
2805 /* Adjust the section alignment if needed. */
2806 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2807 power_of_two))
027297b7
L
2808 return FALSE;
2809 }
2810
91ac5911 2811 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2812 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2813
2814 /* Define the symbol as being at this point in DYNBSS. */
2815 h->root.u.def.section = dynbss;
2816 h->root.u.def.value = dynbss->size;
2817
2818 /* Increment the size of DYNBSS to make room for the symbol. */
2819 dynbss->size += h->size;
2820
f7483970
L
2821 /* No error if extern_protected_data is true. */
2822 if (h->protected_def
889c2a67
L
2823 && (!info->extern_protected_data
2824 || (info->extern_protected_data < 0
2825 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05
AM
2826 info->callbacks->einfo
2827 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2828 h->root.root.string);
6cabe1ea 2829
027297b7
L
2830 return TRUE;
2831}
2832
45d6a902
AM
2833/* Adjust all external symbols pointing into SEC_MERGE sections
2834 to reflect the object merging within the sections. */
2835
28caa186 2836static bfd_boolean
268b6b39 2837_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2838{
2839 asection *sec;
2840
45d6a902
AM
2841 if ((h->root.type == bfd_link_hash_defined
2842 || h->root.type == bfd_link_hash_defweak)
2843 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 2844 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 2845 {
a50b1753 2846 bfd *output_bfd = (bfd *) data;
45d6a902
AM
2847
2848 h->root.u.def.value =
2849 _bfd_merged_section_offset (output_bfd,
2850 &h->root.u.def.section,
2851 elf_section_data (sec)->sec_info,
753731ee 2852 h->root.u.def.value);
45d6a902
AM
2853 }
2854
2855 return TRUE;
2856}
986a241f
RH
2857
2858/* Returns false if the symbol referred to by H should be considered
2859 to resolve local to the current module, and true if it should be
2860 considered to bind dynamically. */
2861
2862bfd_boolean
268b6b39
AM
2863_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2864 struct bfd_link_info *info,
89a2ee5a 2865 bfd_boolean not_local_protected)
986a241f
RH
2866{
2867 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2868 const struct elf_backend_data *bed;
2869 struct elf_link_hash_table *hash_table;
986a241f
RH
2870
2871 if (h == NULL)
2872 return FALSE;
2873
2874 while (h->root.type == bfd_link_hash_indirect
2875 || h->root.type == bfd_link_hash_warning)
2876 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2877
2878 /* If it was forced local, then clearly it's not dynamic. */
2879 if (h->dynindx == -1)
2880 return FALSE;
f5385ebf 2881 if (h->forced_local)
986a241f
RH
2882 return FALSE;
2883
2884 /* Identify the cases where name binding rules say that a
2885 visible symbol resolves locally. */
0e1862bb
L
2886 binding_stays_local_p = (bfd_link_executable (info)
2887 || SYMBOLIC_BIND (info, h));
986a241f
RH
2888
2889 switch (ELF_ST_VISIBILITY (h->other))
2890 {
2891 case STV_INTERNAL:
2892 case STV_HIDDEN:
2893 return FALSE;
2894
2895 case STV_PROTECTED:
fcb93ecf
PB
2896 hash_table = elf_hash_table (info);
2897 if (!is_elf_hash_table (hash_table))
2898 return FALSE;
2899
2900 bed = get_elf_backend_data (hash_table->dynobj);
2901
986a241f
RH
2902 /* Proper resolution for function pointer equality may require
2903 that these symbols perhaps be resolved dynamically, even though
2904 we should be resolving them to the current module. */
89a2ee5a 2905 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
2906 binding_stays_local_p = TRUE;
2907 break;
2908
2909 default:
986a241f
RH
2910 break;
2911 }
2912
aa37626c 2913 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 2914 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
2915 return TRUE;
2916
986a241f
RH
2917 /* Otherwise, the symbol is dynamic if binding rules don't tell
2918 us that it remains local. */
2919 return !binding_stays_local_p;
2920}
f6c52c13
AM
2921
2922/* Return true if the symbol referred to by H should be considered
2923 to resolve local to the current module, and false otherwise. Differs
2924 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 2925 undefined symbols. The two functions are virtually identical except
89a2ee5a
AM
2926 for the place where forced_local and dynindx == -1 are tested. If
2927 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2928 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2929 the symbol is local only for defined symbols.
2930 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2931 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2932 treatment of undefined weak symbols. For those that do not make
2933 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
2934
2935bfd_boolean
268b6b39
AM
2936_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2937 struct bfd_link_info *info,
2938 bfd_boolean local_protected)
f6c52c13 2939{
fcb93ecf
PB
2940 const struct elf_backend_data *bed;
2941 struct elf_link_hash_table *hash_table;
2942
f6c52c13
AM
2943 /* If it's a local sym, of course we resolve locally. */
2944 if (h == NULL)
2945 return TRUE;
2946
d95edcac
L
2947 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2948 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2949 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2950 return TRUE;
2951
7e2294f9
AO
2952 /* Common symbols that become definitions don't get the DEF_REGULAR
2953 flag set, so test it first, and don't bail out. */
2954 if (ELF_COMMON_DEF_P (h))
2955 /* Do nothing. */;
f6c52c13 2956 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2957 resolve locally. The sym is either undefined or dynamic. */
2958 else if (!h->def_regular)
f6c52c13
AM
2959 return FALSE;
2960
2961 /* Forced local symbols resolve locally. */
f5385ebf 2962 if (h->forced_local)
f6c52c13
AM
2963 return TRUE;
2964
2965 /* As do non-dynamic symbols. */
2966 if (h->dynindx == -1)
2967 return TRUE;
2968
2969 /* At this point, we know the symbol is defined and dynamic. In an
2970 executable it must resolve locally, likewise when building symbolic
2971 shared libraries. */
0e1862bb 2972 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2973 return TRUE;
2974
2975 /* Now deal with defined dynamic symbols in shared libraries. Ones
2976 with default visibility might not resolve locally. */
2977 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2978 return FALSE;
2979
fcb93ecf
PB
2980 hash_table = elf_hash_table (info);
2981 if (!is_elf_hash_table (hash_table))
2982 return TRUE;
2983
2984 bed = get_elf_backend_data (hash_table->dynobj);
2985
f7483970
L
2986 /* If extern_protected_data is false, STV_PROTECTED non-function
2987 symbols are local. */
889c2a67
L
2988 if ((!info->extern_protected_data
2989 || (info->extern_protected_data < 0
2990 && !bed->extern_protected_data))
2991 && !bed->is_function_type (h->type))
1c16dfa5
L
2992 return TRUE;
2993
f6c52c13 2994 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
2995 symbols be treated as dynamic symbols. If the address of a
2996 function not defined in an executable is set to that function's
2997 plt entry in the executable, then the address of the function in
2998 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
2999 return local_protected;
3000}
e1918d23
AM
3001
3002/* Caches some TLS segment info, and ensures that the TLS segment vma is
3003 aligned. Returns the first TLS output section. */
3004
3005struct bfd_section *
3006_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3007{
3008 struct bfd_section *sec, *tls;
3009 unsigned int align = 0;
3010
3011 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3012 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3013 break;
3014 tls = sec;
3015
3016 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3017 if (sec->alignment_power > align)
3018 align = sec->alignment_power;
3019
3020 elf_hash_table (info)->tls_sec = tls;
3021
3022 /* Ensure the alignment of the first section is the largest alignment,
3023 so that the tls segment starts aligned. */
3024 if (tls != NULL)
3025 tls->alignment_power = align;
3026
3027 return tls;
3028}
0ad989f9
L
3029
3030/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3031static bfd_boolean
3032is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3033 Elf_Internal_Sym *sym)
3034{
a4d8e49b
L
3035 const struct elf_backend_data *bed;
3036
0ad989f9
L
3037 /* Local symbols do not count, but target specific ones might. */
3038 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3039 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3040 return FALSE;
3041
fcb93ecf 3042 bed = get_elf_backend_data (abfd);
0ad989f9 3043 /* Function symbols do not count. */
fcb93ecf 3044 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3045 return FALSE;
3046
3047 /* If the section is undefined, then so is the symbol. */
3048 if (sym->st_shndx == SHN_UNDEF)
3049 return FALSE;
3050
3051 /* If the symbol is defined in the common section, then
3052 it is a common definition and so does not count. */
a4d8e49b 3053 if (bed->common_definition (sym))
0ad989f9
L
3054 return FALSE;
3055
3056 /* If the symbol is in a target specific section then we
3057 must rely upon the backend to tell us what it is. */
3058 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3059 /* FIXME - this function is not coded yet:
3060
3061 return _bfd_is_global_symbol_definition (abfd, sym);
3062
3063 Instead for now assume that the definition is not global,
3064 Even if this is wrong, at least the linker will behave
3065 in the same way that it used to do. */
3066 return FALSE;
3067
3068 return TRUE;
3069}
3070
3071/* Search the symbol table of the archive element of the archive ABFD
3072 whose archive map contains a mention of SYMDEF, and determine if
3073 the symbol is defined in this element. */
3074static bfd_boolean
3075elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3076{
3077 Elf_Internal_Shdr * hdr;
3078 bfd_size_type symcount;
3079 bfd_size_type extsymcount;
3080 bfd_size_type extsymoff;
3081 Elf_Internal_Sym *isymbuf;
3082 Elf_Internal_Sym *isym;
3083 Elf_Internal_Sym *isymend;
3084 bfd_boolean result;
3085
3086 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3087 if (abfd == NULL)
3088 return FALSE;
3089
f0bf6bfd
L
3090 /* Return FALSE if the object has been claimed by plugin. */
3091 if (abfd->plugin_format == bfd_plugin_yes)
3092 return FALSE;
3093
0ad989f9
L
3094 if (! bfd_check_format (abfd, bfd_object))
3095 return FALSE;
3096
0ad989f9
L
3097 /* Select the appropriate symbol table. */
3098 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3099 hdr = &elf_tdata (abfd)->symtab_hdr;
3100 else
3101 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3102
3103 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3104
3105 /* The sh_info field of the symtab header tells us where the
3106 external symbols start. We don't care about the local symbols. */
3107 if (elf_bad_symtab (abfd))
3108 {
3109 extsymcount = symcount;
3110 extsymoff = 0;
3111 }
3112 else
3113 {
3114 extsymcount = symcount - hdr->sh_info;
3115 extsymoff = hdr->sh_info;
3116 }
3117
3118 if (extsymcount == 0)
3119 return FALSE;
3120
3121 /* Read in the symbol table. */
3122 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3123 NULL, NULL, NULL);
3124 if (isymbuf == NULL)
3125 return FALSE;
3126
3127 /* Scan the symbol table looking for SYMDEF. */
3128 result = FALSE;
3129 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3130 {
3131 const char *name;
3132
3133 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3134 isym->st_name);
3135 if (name == NULL)
3136 break;
3137
3138 if (strcmp (name, symdef->name) == 0)
3139 {
3140 result = is_global_data_symbol_definition (abfd, isym);
3141 break;
3142 }
3143 }
3144
3145 free (isymbuf);
3146
3147 return result;
3148}
3149\f
5a580b3a
AM
3150/* Add an entry to the .dynamic table. */
3151
3152bfd_boolean
3153_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3154 bfd_vma tag,
3155 bfd_vma val)
3156{
3157 struct elf_link_hash_table *hash_table;
3158 const struct elf_backend_data *bed;
3159 asection *s;
3160 bfd_size_type newsize;
3161 bfd_byte *newcontents;
3162 Elf_Internal_Dyn dyn;
3163
3164 hash_table = elf_hash_table (info);
3165 if (! is_elf_hash_table (hash_table))
3166 return FALSE;
3167
3168 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3169 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3170 BFD_ASSERT (s != NULL);
3171
eea6121a 3172 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3173 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3174 if (newcontents == NULL)
3175 return FALSE;
3176
3177 dyn.d_tag = tag;
3178 dyn.d_un.d_val = val;
eea6121a 3179 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3180
eea6121a 3181 s->size = newsize;
5a580b3a
AM
3182 s->contents = newcontents;
3183
3184 return TRUE;
3185}
3186
3187/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3188 otherwise just check whether one already exists. Returns -1 on error,
3189 1 if a DT_NEEDED tag already exists, and 0 on success. */
3190
4ad4eba5 3191static int
7e9f0867
AM
3192elf_add_dt_needed_tag (bfd *abfd,
3193 struct bfd_link_info *info,
4ad4eba5
AM
3194 const char *soname,
3195 bfd_boolean do_it)
5a580b3a
AM
3196{
3197 struct elf_link_hash_table *hash_table;
5a580b3a
AM
3198 bfd_size_type strindex;
3199
7e9f0867
AM
3200 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3201 return -1;
3202
5a580b3a 3203 hash_table = elf_hash_table (info);
5a580b3a
AM
3204 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3205 if (strindex == (bfd_size_type) -1)
3206 return -1;
3207
02be4619 3208 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3209 {
3210 asection *sdyn;
3211 const struct elf_backend_data *bed;
3212 bfd_byte *extdyn;
3213
3214 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3215 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3216 if (sdyn != NULL)
3217 for (extdyn = sdyn->contents;
3218 extdyn < sdyn->contents + sdyn->size;
3219 extdyn += bed->s->sizeof_dyn)
3220 {
3221 Elf_Internal_Dyn dyn;
5a580b3a 3222
7e9f0867
AM
3223 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3224 if (dyn.d_tag == DT_NEEDED
3225 && dyn.d_un.d_val == strindex)
3226 {
3227 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3228 return 1;
3229 }
3230 }
5a580b3a
AM
3231 }
3232
3233 if (do_it)
3234 {
7e9f0867
AM
3235 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3236 return -1;
3237
5a580b3a
AM
3238 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3239 return -1;
3240 }
3241 else
3242 /* We were just checking for existence of the tag. */
3243 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3244
3245 return 0;
3246}
3247
7b15fa7a
AM
3248/* Return true if SONAME is on the needed list between NEEDED and STOP
3249 (or the end of list if STOP is NULL), and needed by a library that
3250 will be loaded. */
3251
010e5ae2 3252static bfd_boolean
7b15fa7a
AM
3253on_needed_list (const char *soname,
3254 struct bfd_link_needed_list *needed,
3255 struct bfd_link_needed_list *stop)
010e5ae2 3256{
7b15fa7a
AM
3257 struct bfd_link_needed_list *look;
3258 for (look = needed; look != stop; look = look->next)
3259 if (strcmp (soname, look->name) == 0
3260 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3261 /* If needed by a library that itself is not directly
3262 needed, recursively check whether that library is
3263 indirectly needed. Since we add DT_NEEDED entries to
3264 the end of the list, library dependencies appear after
3265 the library. Therefore search prior to the current
3266 LOOK, preventing possible infinite recursion. */
3267 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3268 return TRUE;
3269
3270 return FALSE;
3271}
3272
14160578 3273/* Sort symbol by value, section, and size. */
4ad4eba5
AM
3274static int
3275elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3276{
3277 const struct elf_link_hash_entry *h1;
3278 const struct elf_link_hash_entry *h2;
10b7e05b 3279 bfd_signed_vma vdiff;
5a580b3a
AM
3280
3281 h1 = *(const struct elf_link_hash_entry **) arg1;
3282 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3283 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3284 if (vdiff != 0)
3285 return vdiff > 0 ? 1 : -1;
3286 else
3287 {
d3435ae8 3288 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
10b7e05b
NC
3289 if (sdiff != 0)
3290 return sdiff > 0 ? 1 : -1;
3291 }
14160578
AM
3292 vdiff = h1->size - h2->size;
3293 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
5a580b3a 3294}
4ad4eba5 3295
5a580b3a
AM
3296/* This function is used to adjust offsets into .dynstr for
3297 dynamic symbols. This is called via elf_link_hash_traverse. */
3298
3299static bfd_boolean
3300elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3301{
a50b1753 3302 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3303
5a580b3a
AM
3304 if (h->dynindx != -1)
3305 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3306 return TRUE;
3307}
3308
3309/* Assign string offsets in .dynstr, update all structures referencing
3310 them. */
3311
4ad4eba5
AM
3312static bfd_boolean
3313elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3314{
3315 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3316 struct elf_link_local_dynamic_entry *entry;
3317 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3318 bfd *dynobj = hash_table->dynobj;
3319 asection *sdyn;
3320 bfd_size_type size;
3321 const struct elf_backend_data *bed;
3322 bfd_byte *extdyn;
3323
3324 _bfd_elf_strtab_finalize (dynstr);
3325 size = _bfd_elf_strtab_size (dynstr);
3326
3327 bed = get_elf_backend_data (dynobj);
3d4d4302 3328 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3329 BFD_ASSERT (sdyn != NULL);
3330
3331 /* Update all .dynamic entries referencing .dynstr strings. */
3332 for (extdyn = sdyn->contents;
eea6121a 3333 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3334 extdyn += bed->s->sizeof_dyn)
3335 {
3336 Elf_Internal_Dyn dyn;
3337
3338 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3339 switch (dyn.d_tag)
3340 {
3341 case DT_STRSZ:
3342 dyn.d_un.d_val = size;
3343 break;
3344 case DT_NEEDED:
3345 case DT_SONAME:
3346 case DT_RPATH:
3347 case DT_RUNPATH:
3348 case DT_FILTER:
3349 case DT_AUXILIARY:
7ee314fa
AM
3350 case DT_AUDIT:
3351 case DT_DEPAUDIT:
5a580b3a
AM
3352 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3353 break;
3354 default:
3355 continue;
3356 }
3357 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3358 }
3359
3360 /* Now update local dynamic symbols. */
3361 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3362 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3363 entry->isym.st_name);
3364
3365 /* And the rest of dynamic symbols. */
3366 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3367
3368 /* Adjust version definitions. */
3369 if (elf_tdata (output_bfd)->cverdefs)
3370 {
3371 asection *s;
3372 bfd_byte *p;
3373 bfd_size_type i;
3374 Elf_Internal_Verdef def;
3375 Elf_Internal_Verdaux defaux;
3376
3d4d4302 3377 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3378 p = s->contents;
3379 do
3380 {
3381 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3382 &def);
3383 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3384 if (def.vd_aux != sizeof (Elf_External_Verdef))
3385 continue;
5a580b3a
AM
3386 for (i = 0; i < def.vd_cnt; ++i)
3387 {
3388 _bfd_elf_swap_verdaux_in (output_bfd,
3389 (Elf_External_Verdaux *) p, &defaux);
3390 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3391 defaux.vda_name);
3392 _bfd_elf_swap_verdaux_out (output_bfd,
3393 &defaux, (Elf_External_Verdaux *) p);
3394 p += sizeof (Elf_External_Verdaux);
3395 }
3396 }
3397 while (def.vd_next);
3398 }
3399
3400 /* Adjust version references. */
3401 if (elf_tdata (output_bfd)->verref)
3402 {
3403 asection *s;
3404 bfd_byte *p;
3405 bfd_size_type i;
3406 Elf_Internal_Verneed need;
3407 Elf_Internal_Vernaux needaux;
3408
3d4d4302 3409 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3410 p = s->contents;
3411 do
3412 {
3413 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3414 &need);
3415 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3416 _bfd_elf_swap_verneed_out (output_bfd, &need,
3417 (Elf_External_Verneed *) p);
3418 p += sizeof (Elf_External_Verneed);
3419 for (i = 0; i < need.vn_cnt; ++i)
3420 {
3421 _bfd_elf_swap_vernaux_in (output_bfd,
3422 (Elf_External_Vernaux *) p, &needaux);
3423 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3424 needaux.vna_name);
3425 _bfd_elf_swap_vernaux_out (output_bfd,
3426 &needaux,
3427 (Elf_External_Vernaux *) p);
3428 p += sizeof (Elf_External_Vernaux);
3429 }
3430 }
3431 while (need.vn_next);
3432 }
3433
3434 return TRUE;
3435}
3436\f
13285a1b
AM
3437/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3438 The default is to only match when the INPUT and OUTPUT are exactly
3439 the same target. */
3440
3441bfd_boolean
3442_bfd_elf_default_relocs_compatible (const bfd_target *input,
3443 const bfd_target *output)
3444{
3445 return input == output;
3446}
3447
3448/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3449 This version is used when different targets for the same architecture
3450 are virtually identical. */
3451
3452bfd_boolean
3453_bfd_elf_relocs_compatible (const bfd_target *input,
3454 const bfd_target *output)
3455{
3456 const struct elf_backend_data *obed, *ibed;
3457
3458 if (input == output)
3459 return TRUE;
3460
3461 ibed = xvec_get_elf_backend_data (input);
3462 obed = xvec_get_elf_backend_data (output);
3463
3464 if (ibed->arch != obed->arch)
3465 return FALSE;
3466
3467 /* If both backends are using this function, deem them compatible. */
3468 return ibed->relocs_compatible == obed->relocs_compatible;
3469}
3470
e5034e59
AM
3471/* Make a special call to the linker "notice" function to tell it that
3472 we are about to handle an as-needed lib, or have finished
1b786873 3473 processing the lib. */
e5034e59
AM
3474
3475bfd_boolean
3476_bfd_elf_notice_as_needed (bfd *ibfd,
3477 struct bfd_link_info *info,
3478 enum notice_asneeded_action act)
3479{
46135103 3480 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3481}
3482
d9689752
L
3483/* Check relocations an ELF object file. */
3484
3485bfd_boolean
3486_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3487{
3488 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3489 struct elf_link_hash_table *htab = elf_hash_table (info);
3490
3491 /* If this object is the same format as the output object, and it is
3492 not a shared library, then let the backend look through the
3493 relocs.
3494
3495 This is required to build global offset table entries and to
3496 arrange for dynamic relocs. It is not required for the
3497 particular common case of linking non PIC code, even when linking
3498 against shared libraries, but unfortunately there is no way of
3499 knowing whether an object file has been compiled PIC or not.
3500 Looking through the relocs is not particularly time consuming.
3501 The problem is that we must either (1) keep the relocs in memory,
3502 which causes the linker to require additional runtime memory or
3503 (2) read the relocs twice from the input file, which wastes time.
3504 This would be a good case for using mmap.
3505
3506 I have no idea how to handle linking PIC code into a file of a
3507 different format. It probably can't be done. */
3508 if ((abfd->flags & DYNAMIC) == 0
3509 && is_elf_hash_table (htab)
3510 && bed->check_relocs != NULL
3511 && elf_object_id (abfd) == elf_hash_table_id (htab)
3512 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3513 {
3514 asection *o;
3515
3516 for (o = abfd->sections; o != NULL; o = o->next)
3517 {
3518 Elf_Internal_Rela *internal_relocs;
3519 bfd_boolean ok;
3520
5ce03cea 3521 /* Don't check relocations in excluded sections. */
d9689752 3522 if ((o->flags & SEC_RELOC) == 0
5ce03cea 3523 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3524 || o->reloc_count == 0
3525 || ((info->strip == strip_all || info->strip == strip_debugger)
3526 && (o->flags & SEC_DEBUGGING) != 0)
3527 || bfd_is_abs_section (o->output_section))
3528 continue;
3529
3530 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3531 info->keep_memory);
3532 if (internal_relocs == NULL)
3533 return FALSE;
3534
3535 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3536
3537 if (elf_section_data (o)->relocs != internal_relocs)
3538 free (internal_relocs);
3539
3540 if (! ok)
3541 return FALSE;
3542 }
3543 }
3544
3545 return TRUE;
3546}
3547
4ad4eba5
AM
3548/* Add symbols from an ELF object file to the linker hash table. */
3549
3550static bfd_boolean
3551elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3552{
a0c402a5 3553 Elf_Internal_Ehdr *ehdr;
4ad4eba5
AM
3554 Elf_Internal_Shdr *hdr;
3555 bfd_size_type symcount;
3556 bfd_size_type extsymcount;
3557 bfd_size_type extsymoff;
3558 struct elf_link_hash_entry **sym_hash;
3559 bfd_boolean dynamic;
3560 Elf_External_Versym *extversym = NULL;
3561 Elf_External_Versym *ever;
3562 struct elf_link_hash_entry *weaks;
3563 struct elf_link_hash_entry **nondeflt_vers = NULL;
3564 bfd_size_type nondeflt_vers_cnt = 0;
3565 Elf_Internal_Sym *isymbuf = NULL;
3566 Elf_Internal_Sym *isym;
3567 Elf_Internal_Sym *isymend;
3568 const struct elf_backend_data *bed;
3569 bfd_boolean add_needed;
66eb6687 3570 struct elf_link_hash_table *htab;
4ad4eba5 3571 bfd_size_type amt;
66eb6687 3572 void *alloc_mark = NULL;
4f87808c
AM
3573 struct bfd_hash_entry **old_table = NULL;
3574 unsigned int old_size = 0;
3575 unsigned int old_count = 0;
66eb6687 3576 void *old_tab = NULL;
66eb6687
AM
3577 void *old_ent;
3578 struct bfd_link_hash_entry *old_undefs = NULL;
3579 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3580 long old_dynsymcount = 0;
a4542f1b 3581 bfd_size_type old_dynstr_size = 0;
66eb6687 3582 size_t tabsize = 0;
db6a5d5f 3583 asection *s;
29a9f53e 3584 bfd_boolean just_syms;
4ad4eba5 3585
66eb6687 3586 htab = elf_hash_table (info);
4ad4eba5 3587 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3588
3589 if ((abfd->flags & DYNAMIC) == 0)
3590 dynamic = FALSE;
3591 else
3592 {
3593 dynamic = TRUE;
3594
3595 /* You can't use -r against a dynamic object. Also, there's no
3596 hope of using a dynamic object which does not exactly match
3597 the format of the output file. */
0e1862bb 3598 if (bfd_link_relocatable (info)
66eb6687 3599 || !is_elf_hash_table (htab)
f13a99db 3600 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 3601 {
0e1862bb 3602 if (bfd_link_relocatable (info))
9a0789ec
NC
3603 bfd_set_error (bfd_error_invalid_operation);
3604 else
3605 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3606 goto error_return;
3607 }
3608 }
3609
a0c402a5
L
3610 ehdr = elf_elfheader (abfd);
3611 if (info->warn_alternate_em
3612 && bed->elf_machine_code != ehdr->e_machine
3613 && ((bed->elf_machine_alt1 != 0
3614 && ehdr->e_machine == bed->elf_machine_alt1)
3615 || (bed->elf_machine_alt2 != 0
3616 && ehdr->e_machine == bed->elf_machine_alt2)))
3617 info->callbacks->einfo
3618 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3619 ehdr->e_machine, abfd, bed->elf_machine_code);
3620
4ad4eba5
AM
3621 /* As a GNU extension, any input sections which are named
3622 .gnu.warning.SYMBOL are treated as warning symbols for the given
3623 symbol. This differs from .gnu.warning sections, which generate
3624 warnings when they are included in an output file. */
dd98f8d2 3625 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 3626 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 3627 {
db6a5d5f 3628 const char *name;
4ad4eba5 3629
db6a5d5f
AM
3630 name = bfd_get_section_name (abfd, s);
3631 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 3632 {
db6a5d5f
AM
3633 char *msg;
3634 bfd_size_type sz;
3635
3636 name += sizeof ".gnu.warning." - 1;
3637
3638 /* If this is a shared object, then look up the symbol
3639 in the hash table. If it is there, and it is already
3640 been defined, then we will not be using the entry
3641 from this shared object, so we don't need to warn.
3642 FIXME: If we see the definition in a regular object
3643 later on, we will warn, but we shouldn't. The only
3644 fix is to keep track of what warnings we are supposed
3645 to emit, and then handle them all at the end of the
3646 link. */
3647 if (dynamic)
4ad4eba5 3648 {
db6a5d5f
AM
3649 struct elf_link_hash_entry *h;
3650
3651 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3652
3653 /* FIXME: What about bfd_link_hash_common? */
3654 if (h != NULL
3655 && (h->root.type == bfd_link_hash_defined
3656 || h->root.type == bfd_link_hash_defweak))
3657 continue;
3658 }
4ad4eba5 3659
db6a5d5f
AM
3660 sz = s->size;
3661 msg = (char *) bfd_alloc (abfd, sz + 1);
3662 if (msg == NULL)
3663 goto error_return;
4ad4eba5 3664
db6a5d5f
AM
3665 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3666 goto error_return;
4ad4eba5 3667
db6a5d5f 3668 msg[sz] = '\0';
4ad4eba5 3669
db6a5d5f
AM
3670 if (! (_bfd_generic_link_add_one_symbol
3671 (info, abfd, name, BSF_WARNING, s, 0, msg,
3672 FALSE, bed->collect, NULL)))
3673 goto error_return;
4ad4eba5 3674
0e1862bb 3675 if (bfd_link_executable (info))
db6a5d5f
AM
3676 {
3677 /* Clobber the section size so that the warning does
3678 not get copied into the output file. */
3679 s->size = 0;
11d2f718 3680
db6a5d5f
AM
3681 /* Also set SEC_EXCLUDE, so that symbols defined in
3682 the warning section don't get copied to the output. */
3683 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3684 }
3685 }
3686 }
3687
29a9f53e
L
3688 just_syms = ((s = abfd->sections) != NULL
3689 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3690
4ad4eba5
AM
3691 add_needed = TRUE;
3692 if (! dynamic)
3693 {
3694 /* If we are creating a shared library, create all the dynamic
3695 sections immediately. We need to attach them to something,
3696 so we attach them to this BFD, provided it is the right
bf89386a
L
3697 format and is not from ld --just-symbols. Always create the
3698 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
3699 are no input BFD's of the same format as the output, we can't
3700 make a shared library. */
3701 if (!just_syms
bf89386a 3702 && (bfd_link_pic (info)
9c1d7a08
L
3703 || (!bfd_link_relocatable (info)
3704 && (info->export_dynamic || info->dynamic)))
66eb6687 3705 && is_elf_hash_table (htab)
f13a99db 3706 && info->output_bfd->xvec == abfd->xvec
66eb6687 3707 && !htab->dynamic_sections_created)
4ad4eba5
AM
3708 {
3709 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3710 goto error_return;
3711 }
3712 }
66eb6687 3713 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3714 goto error_return;
3715 else
3716 {
4ad4eba5 3717 const char *soname = NULL;
7ee314fa 3718 char *audit = NULL;
4ad4eba5
AM
3719 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3720 int ret;
3721
3722 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3723 ld shouldn't allow it. */
29a9f53e 3724 if (just_syms)
92fd189d 3725 abort ();
4ad4eba5
AM
3726
3727 /* If this dynamic lib was specified on the command line with
3728 --as-needed in effect, then we don't want to add a DT_NEEDED
3729 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3730 in by another lib's DT_NEEDED. When --no-add-needed is used
3731 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3732 any dynamic library in DT_NEEDED tags in the dynamic lib at
3733 all. */
3734 add_needed = (elf_dyn_lib_class (abfd)
3735 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3736 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3737
3738 s = bfd_get_section_by_name (abfd, ".dynamic");
3739 if (s != NULL)
3740 {
3741 bfd_byte *dynbuf;
3742 bfd_byte *extdyn;
cb33740c 3743 unsigned int elfsec;
4ad4eba5
AM
3744 unsigned long shlink;
3745
eea6121a 3746 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194
L
3747 {
3748error_free_dyn:
3749 free (dynbuf);
3750 goto error_return;
3751 }
4ad4eba5
AM
3752
3753 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 3754 if (elfsec == SHN_BAD)
4ad4eba5
AM
3755 goto error_free_dyn;
3756 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3757
3758 for (extdyn = dynbuf;
eea6121a 3759 extdyn < dynbuf + s->size;
4ad4eba5
AM
3760 extdyn += bed->s->sizeof_dyn)
3761 {
3762 Elf_Internal_Dyn dyn;
3763
3764 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3765 if (dyn.d_tag == DT_SONAME)
3766 {
3767 unsigned int tagv = dyn.d_un.d_val;
3768 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3769 if (soname == NULL)
3770 goto error_free_dyn;
3771 }
3772 if (dyn.d_tag == DT_NEEDED)
3773 {
3774 struct bfd_link_needed_list *n, **pn;
3775 char *fnm, *anm;
3776 unsigned int tagv = dyn.d_un.d_val;
3777
3778 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3779 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3780 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3781 if (n == NULL || fnm == NULL)
3782 goto error_free_dyn;
3783 amt = strlen (fnm) + 1;
a50b1753 3784 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3785 if (anm == NULL)
3786 goto error_free_dyn;
3787 memcpy (anm, fnm, amt);
3788 n->name = anm;
3789 n->by = abfd;
3790 n->next = NULL;
66eb6687 3791 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3792 ;
3793 *pn = n;
3794 }
3795 if (dyn.d_tag == DT_RUNPATH)
3796 {
3797 struct bfd_link_needed_list *n, **pn;
3798 char *fnm, *anm;
3799 unsigned int tagv = dyn.d_un.d_val;
3800
3801 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3802 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3803 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3804 if (n == NULL || fnm == NULL)
3805 goto error_free_dyn;
3806 amt = strlen (fnm) + 1;
a50b1753 3807 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3808 if (anm == NULL)
3809 goto error_free_dyn;
3810 memcpy (anm, fnm, amt);
3811 n->name = anm;
3812 n->by = abfd;
3813 n->next = NULL;
3814 for (pn = & runpath;
3815 *pn != NULL;
3816 pn = &(*pn)->next)
3817 ;
3818 *pn = n;
3819 }
3820 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3821 if (!runpath && dyn.d_tag == DT_RPATH)
3822 {
3823 struct bfd_link_needed_list *n, **pn;
3824 char *fnm, *anm;
3825 unsigned int tagv = dyn.d_un.d_val;
3826
3827 amt = sizeof (struct bfd_link_needed_list);
a50b1753 3828 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
3829 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3830 if (n == NULL || fnm == NULL)
3831 goto error_free_dyn;
3832 amt = strlen (fnm) + 1;
a50b1753 3833 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 3834 if (anm == NULL)
f8703194 3835 goto error_free_dyn;
4ad4eba5
AM
3836 memcpy (anm, fnm, amt);
3837 n->name = anm;
3838 n->by = abfd;
3839 n->next = NULL;
3840 for (pn = & rpath;
3841 *pn != NULL;
3842 pn = &(*pn)->next)
3843 ;
3844 *pn = n;
3845 }
7ee314fa
AM
3846 if (dyn.d_tag == DT_AUDIT)
3847 {
3848 unsigned int tagv = dyn.d_un.d_val;
3849 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3850 }
4ad4eba5
AM
3851 }
3852
3853 free (dynbuf);
3854 }
3855
3856 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3857 frees all more recently bfd_alloc'd blocks as well. */
3858 if (runpath)
3859 rpath = runpath;
3860
3861 if (rpath)
3862 {
3863 struct bfd_link_needed_list **pn;
66eb6687 3864 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3865 ;
3866 *pn = rpath;
3867 }
3868
3869 /* We do not want to include any of the sections in a dynamic
3870 object in the output file. We hack by simply clobbering the
3871 list of sections in the BFD. This could be handled more
3872 cleanly by, say, a new section flag; the existing
3873 SEC_NEVER_LOAD flag is not the one we want, because that one
3874 still implies that the section takes up space in the output
3875 file. */
3876 bfd_section_list_clear (abfd);
3877
4ad4eba5
AM
3878 /* Find the name to use in a DT_NEEDED entry that refers to this
3879 object. If the object has a DT_SONAME entry, we use it.
3880 Otherwise, if the generic linker stuck something in
3881 elf_dt_name, we use that. Otherwise, we just use the file
3882 name. */
3883 if (soname == NULL || *soname == '\0')
3884 {
3885 soname = elf_dt_name (abfd);
3886 if (soname == NULL || *soname == '\0')
3887 soname = bfd_get_filename (abfd);
3888 }
3889
3890 /* Save the SONAME because sometimes the linker emulation code
3891 will need to know it. */
3892 elf_dt_name (abfd) = soname;
3893
7e9f0867 3894 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3895 if (ret < 0)
3896 goto error_return;
3897
3898 /* If we have already included this dynamic object in the
3899 link, just ignore it. There is no reason to include a
3900 particular dynamic object more than once. */
3901 if (ret > 0)
3902 return TRUE;
7ee314fa
AM
3903
3904 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 3905 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
3906 }
3907
3908 /* If this is a dynamic object, we always link against the .dynsym
3909 symbol table, not the .symtab symbol table. The dynamic linker
3910 will only see the .dynsym symbol table, so there is no reason to
3911 look at .symtab for a dynamic object. */
3912
3913 if (! dynamic || elf_dynsymtab (abfd) == 0)
3914 hdr = &elf_tdata (abfd)->symtab_hdr;
3915 else
3916 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3917
3918 symcount = hdr->sh_size / bed->s->sizeof_sym;
3919
3920 /* The sh_info field of the symtab header tells us where the
3921 external symbols start. We don't care about the local symbols at
3922 this point. */
3923 if (elf_bad_symtab (abfd))
3924 {
3925 extsymcount = symcount;
3926 extsymoff = 0;
3927 }
3928 else
3929 {
3930 extsymcount = symcount - hdr->sh_info;
3931 extsymoff = hdr->sh_info;
3932 }
3933
f45794cb 3934 sym_hash = elf_sym_hashes (abfd);
012b2306 3935 if (extsymcount != 0)
4ad4eba5
AM
3936 {
3937 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3938 NULL, NULL, NULL);
3939 if (isymbuf == NULL)
3940 goto error_return;
3941
4ad4eba5 3942 if (sym_hash == NULL)
012b2306
AM
3943 {
3944 /* We store a pointer to the hash table entry for each
3945 external symbol. */
3946 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3947 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3948 if (sym_hash == NULL)
3949 goto error_free_sym;
3950 elf_sym_hashes (abfd) = sym_hash;
3951 }
4ad4eba5
AM
3952 }
3953
3954 if (dynamic)
3955 {
3956 /* Read in any version definitions. */
fc0e6df6
PB
3957 if (!_bfd_elf_slurp_version_tables (abfd,
3958 info->default_imported_symver))
4ad4eba5
AM
3959 goto error_free_sym;
3960
3961 /* Read in the symbol versions, but don't bother to convert them
3962 to internal format. */
3963 if (elf_dynversym (abfd) != 0)
3964 {
3965 Elf_Internal_Shdr *versymhdr;
3966
3967 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
a50b1753 3968 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4ad4eba5
AM
3969 if (extversym == NULL)
3970 goto error_free_sym;
3971 amt = versymhdr->sh_size;
3972 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3973 || bfd_bread (extversym, amt, abfd) != amt)
3974 goto error_free_vers;
3975 }
3976 }
3977
66eb6687
AM
3978 /* If we are loading an as-needed shared lib, save the symbol table
3979 state before we start adding symbols. If the lib turns out
3980 to be unneeded, restore the state. */
3981 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3982 {
3983 unsigned int i;
3984 size_t entsize;
3985
3986 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3987 {
3988 struct bfd_hash_entry *p;
2de92251 3989 struct elf_link_hash_entry *h;
66eb6687
AM
3990
3991 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3992 {
3993 h = (struct elf_link_hash_entry *) p;
3994 entsize += htab->root.table.entsize;
3995 if (h->root.type == bfd_link_hash_warning)
3996 entsize += htab->root.table.entsize;
3997 }
66eb6687
AM
3998 }
3999
4000 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4001 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4002 if (old_tab == NULL)
4003 goto error_free_vers;
4004
4005 /* Remember the current objalloc pointer, so that all mem for
4006 symbols added can later be reclaimed. */
4007 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4008 if (alloc_mark == NULL)
4009 goto error_free_vers;
4010
5061a885
AM
4011 /* Make a special call to the linker "notice" function to
4012 tell it that we are about to handle an as-needed lib. */
e5034e59 4013 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4014 goto error_free_vers;
5061a885 4015
f45794cb
AM
4016 /* Clone the symbol table. Remember some pointers into the
4017 symbol table, and dynamic symbol count. */
4018 old_ent = (char *) old_tab + tabsize;
66eb6687 4019 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4020 old_undefs = htab->root.undefs;
4021 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4022 old_table = htab->root.table.table;
4023 old_size = htab->root.table.size;
4024 old_count = htab->root.table.count;
66eb6687 4025 old_dynsymcount = htab->dynsymcount;
a4542f1b 4026 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
66eb6687
AM
4027
4028 for (i = 0; i < htab->root.table.size; i++)
4029 {
4030 struct bfd_hash_entry *p;
2de92251 4031 struct elf_link_hash_entry *h;
66eb6687
AM
4032
4033 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4034 {
4035 memcpy (old_ent, p, htab->root.table.entsize);
4036 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4037 h = (struct elf_link_hash_entry *) p;
4038 if (h->root.type == bfd_link_hash_warning)
4039 {
4040 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4041 old_ent = (char *) old_ent + htab->root.table.entsize;
4042 }
66eb6687
AM
4043 }
4044 }
4045 }
4ad4eba5 4046
66eb6687 4047 weaks = NULL;
4ad4eba5
AM
4048 ever = extversym != NULL ? extversym + extsymoff : NULL;
4049 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4050 isym < isymend;
4051 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4052 {
4053 int bind;
4054 bfd_vma value;
af44c138 4055 asection *sec, *new_sec;
4ad4eba5
AM
4056 flagword flags;
4057 const char *name;
4058 struct elf_link_hash_entry *h;
90c984fc 4059 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4060 bfd_boolean definition;
4061 bfd_boolean size_change_ok;
4062 bfd_boolean type_change_ok;
4063 bfd_boolean new_weakdef;
37a9e49a
L
4064 bfd_boolean new_weak;
4065 bfd_boolean old_weak;
4ad4eba5 4066 bfd_boolean override;
a4d8e49b 4067 bfd_boolean common;
4ad4eba5
AM
4068 unsigned int old_alignment;
4069 bfd *old_bfd;
6e33951e 4070 bfd_boolean matched;
4ad4eba5
AM
4071
4072 override = FALSE;
4073
4074 flags = BSF_NO_FLAGS;
4075 sec = NULL;
4076 value = isym->st_value;
a4d8e49b 4077 common = bed->common_definition (isym);
4ad4eba5
AM
4078
4079 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4080 switch (bind)
4ad4eba5 4081 {
3e7a7d11 4082 case STB_LOCAL:
4ad4eba5
AM
4083 /* This should be impossible, since ELF requires that all
4084 global symbols follow all local symbols, and that sh_info
4085 point to the first global symbol. Unfortunately, Irix 5
4086 screws this up. */
4087 continue;
3e7a7d11
NC
4088
4089 case STB_GLOBAL:
a4d8e49b 4090 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4091 flags = BSF_GLOBAL;
3e7a7d11
NC
4092 break;
4093
4094 case STB_WEAK:
4095 flags = BSF_WEAK;
4096 break;
4097
4098 case STB_GNU_UNIQUE:
4099 flags = BSF_GNU_UNIQUE;
4100 break;
4101
4102 default:
4ad4eba5 4103 /* Leave it up to the processor backend. */
3e7a7d11 4104 break;
4ad4eba5
AM
4105 }
4106
4107 if (isym->st_shndx == SHN_UNDEF)
4108 sec = bfd_und_section_ptr;
cb33740c
AM
4109 else if (isym->st_shndx == SHN_ABS)
4110 sec = bfd_abs_section_ptr;
4111 else if (isym->st_shndx == SHN_COMMON)
4112 {
4113 sec = bfd_com_section_ptr;
4114 /* What ELF calls the size we call the value. What ELF
4115 calls the value we call the alignment. */
4116 value = isym->st_size;
4117 }
4118 else
4ad4eba5
AM
4119 {
4120 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4121 if (sec == NULL)
4122 sec = bfd_abs_section_ptr;
dbaa2011 4123 else if (discarded_section (sec))
529fcb95 4124 {
e5d08002
L
4125 /* Symbols from discarded section are undefined. We keep
4126 its visibility. */
529fcb95
PB
4127 sec = bfd_und_section_ptr;
4128 isym->st_shndx = SHN_UNDEF;
4129 }
4ad4eba5
AM
4130 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4131 value -= sec->vma;
4132 }
4ad4eba5
AM
4133
4134 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4135 isym->st_name);
4136 if (name == NULL)
4137 goto error_free_vers;
4138
4139 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4140 && (abfd->flags & BFD_PLUGIN) != 0)
4141 {
4142 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4143
4144 if (xc == NULL)
4145 {
4146 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4147 | SEC_EXCLUDE);
4148 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4149 if (xc == NULL)
4150 goto error_free_vers;
4151 }
4152 sec = xc;
4153 }
4154 else if (isym->st_shndx == SHN_COMMON
4155 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4156 && !bfd_link_relocatable (info))
4ad4eba5
AM
4157 {
4158 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4159
4160 if (tcomm == NULL)
4161 {
02d00247
AM
4162 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4163 | SEC_LINKER_CREATED);
4164 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4165 if (tcomm == NULL)
4ad4eba5
AM
4166 goto error_free_vers;
4167 }
4168 sec = tcomm;
4169 }
66eb6687 4170 else if (bed->elf_add_symbol_hook)
4ad4eba5 4171 {
66eb6687
AM
4172 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4173 &sec, &value))
4ad4eba5
AM
4174 goto error_free_vers;
4175
4176 /* The hook function sets the name to NULL if this symbol
4177 should be skipped for some reason. */
4178 if (name == NULL)
4179 continue;
4180 }
4181
4182 /* Sanity check that all possibilities were handled. */
4183 if (sec == NULL)
4184 {
4185 bfd_set_error (bfd_error_bad_value);
4186 goto error_free_vers;
4187 }
4188
191c0c42
AM
4189 /* Silently discard TLS symbols from --just-syms. There's
4190 no way to combine a static TLS block with a new TLS block
4191 for this executable. */
4192 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4193 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4194 continue;
4195
4ad4eba5
AM
4196 if (bfd_is_und_section (sec)
4197 || bfd_is_com_section (sec))
4198 definition = FALSE;
4199 else
4200 definition = TRUE;
4201
4202 size_change_ok = FALSE;
66eb6687 4203 type_change_ok = bed->type_change_ok;
37a9e49a 4204 old_weak = FALSE;
6e33951e 4205 matched = FALSE;
4ad4eba5
AM
4206 old_alignment = 0;
4207 old_bfd = NULL;
af44c138 4208 new_sec = sec;
4ad4eba5 4209
66eb6687 4210 if (is_elf_hash_table (htab))
4ad4eba5
AM
4211 {
4212 Elf_Internal_Versym iver;
4213 unsigned int vernum = 0;
4214 bfd_boolean skip;
4215
fc0e6df6 4216 if (ever == NULL)
4ad4eba5 4217 {
fc0e6df6
PB
4218 if (info->default_imported_symver)
4219 /* Use the default symbol version created earlier. */
4220 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4221 else
4222 iver.vs_vers = 0;
4223 }
4224 else
4225 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4226
4227 vernum = iver.vs_vers & VERSYM_VERSION;
4228
4229 /* If this is a hidden symbol, or if it is not version
4230 1, we append the version name to the symbol name.
cc86ff91
EB
4231 However, we do not modify a non-hidden absolute symbol
4232 if it is not a function, because it might be the version
4233 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4234 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4235 || (vernum > 1
4236 && (!bfd_is_abs_section (sec)
4237 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4238 {
4239 const char *verstr;
4240 size_t namelen, verlen, newlen;
4241 char *newname, *p;
4242
4243 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4244 {
fc0e6df6
PB
4245 if (vernum > elf_tdata (abfd)->cverdefs)
4246 verstr = NULL;
4247 else if (vernum > 1)
4248 verstr =
4249 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4250 else
4251 verstr = "";
4ad4eba5 4252
fc0e6df6 4253 if (verstr == NULL)
4ad4eba5 4254 {
fc0e6df6
PB
4255 (*_bfd_error_handler)
4256 (_("%B: %s: invalid version %u (max %d)"),
4257 abfd, name, vernum,
4258 elf_tdata (abfd)->cverdefs);
4259 bfd_set_error (bfd_error_bad_value);
4260 goto error_free_vers;
4ad4eba5 4261 }
fc0e6df6
PB
4262 }
4263 else
4264 {
4265 /* We cannot simply test for the number of
4266 entries in the VERNEED section since the
4267 numbers for the needed versions do not start
4268 at 0. */
4269 Elf_Internal_Verneed *t;
4270
4271 verstr = NULL;
4272 for (t = elf_tdata (abfd)->verref;
4273 t != NULL;
4274 t = t->vn_nextref)
4ad4eba5 4275 {
fc0e6df6 4276 Elf_Internal_Vernaux *a;
4ad4eba5 4277
fc0e6df6
PB
4278 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4279 {
4280 if (a->vna_other == vernum)
4ad4eba5 4281 {
fc0e6df6
PB
4282 verstr = a->vna_nodename;
4283 break;
4ad4eba5 4284 }
4ad4eba5 4285 }
fc0e6df6
PB
4286 if (a != NULL)
4287 break;
4288 }
4289 if (verstr == NULL)
4290 {
4291 (*_bfd_error_handler)
4292 (_("%B: %s: invalid needed version %d"),
4293 abfd, name, vernum);
4294 bfd_set_error (bfd_error_bad_value);
4295 goto error_free_vers;
4ad4eba5 4296 }
4ad4eba5 4297 }
fc0e6df6
PB
4298
4299 namelen = strlen (name);
4300 verlen = strlen (verstr);
4301 newlen = namelen + verlen + 2;
4302 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4303 && isym->st_shndx != SHN_UNDEF)
4304 ++newlen;
4305
a50b1753 4306 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4307 if (newname == NULL)
4308 goto error_free_vers;
4309 memcpy (newname, name, namelen);
4310 p = newname + namelen;
4311 *p++ = ELF_VER_CHR;
4312 /* If this is a defined non-hidden version symbol,
4313 we add another @ to the name. This indicates the
4314 default version of the symbol. */
4315 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4316 && isym->st_shndx != SHN_UNDEF)
4317 *p++ = ELF_VER_CHR;
4318 memcpy (p, verstr, verlen + 1);
4319
4320 name = newname;
4ad4eba5
AM
4321 }
4322
cd3416da
AM
4323 /* If this symbol has default visibility and the user has
4324 requested we not re-export it, then mark it as hidden. */
a0d49154 4325 if (!bfd_is_und_section (sec)
cd3416da 4326 && !dynamic
ce875075 4327 && abfd->no_export
cd3416da
AM
4328 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4329 isym->st_other = (STV_HIDDEN
4330 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4331
4f3fedcf
AM
4332 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4333 sym_hash, &old_bfd, &old_weak,
4334 &old_alignment, &skip, &override,
6e33951e
L
4335 &type_change_ok, &size_change_ok,
4336 &matched))
4ad4eba5
AM
4337 goto error_free_vers;
4338
4339 if (skip)
4340 continue;
4341
6e33951e
L
4342 /* Override a definition only if the new symbol matches the
4343 existing one. */
4344 if (override && matched)
4ad4eba5
AM
4345 definition = FALSE;
4346
4347 h = *sym_hash;
4348 while (h->root.type == bfd_link_hash_indirect
4349 || h->root.type == bfd_link_hash_warning)
4350 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4351
4ad4eba5 4352 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4353 && vernum > 1
4354 && definition)
4355 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4356 }
4357
4358 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4359 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4360 (struct bfd_link_hash_entry **) sym_hash)))
4361 goto error_free_vers;
4362
4363 h = *sym_hash;
90c984fc
L
4364 /* We need to make sure that indirect symbol dynamic flags are
4365 updated. */
4366 hi = h;
4ad4eba5
AM
4367 while (h->root.type == bfd_link_hash_indirect
4368 || h->root.type == bfd_link_hash_warning)
4369 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4370
4ad4eba5
AM
4371 *sym_hash = h;
4372
37a9e49a 4373 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4374 new_weakdef = FALSE;
4375 if (dynamic
4376 && definition
37a9e49a 4377 && new_weak
fcb93ecf 4378 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4379 && is_elf_hash_table (htab)
f6e332e6 4380 && h->u.weakdef == NULL)
4ad4eba5
AM
4381 {
4382 /* Keep a list of all weak defined non function symbols from
4383 a dynamic object, using the weakdef field. Later in this
4384 function we will set the weakdef field to the correct
4385 value. We only put non-function symbols from dynamic
4386 objects on this list, because that happens to be the only
4387 time we need to know the normal symbol corresponding to a
4388 weak symbol, and the information is time consuming to
4389 figure out. If the weakdef field is not already NULL,
4390 then this symbol was already defined by some previous
4391 dynamic object, and we will be using that previous
4392 definition anyhow. */
4393
f6e332e6 4394 h->u.weakdef = weaks;
4ad4eba5
AM
4395 weaks = h;
4396 new_weakdef = TRUE;
4397 }
4398
4399 /* Set the alignment of a common symbol. */
a4d8e49b 4400 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4401 && h->root.type == bfd_link_hash_common)
4402 {
4403 unsigned int align;
4404
a4d8e49b 4405 if (common)
af44c138
L
4406 align = bfd_log2 (isym->st_value);
4407 else
4408 {
4409 /* The new symbol is a common symbol in a shared object.
4410 We need to get the alignment from the section. */
4411 align = new_sec->alignment_power;
4412 }
595213d4 4413 if (align > old_alignment)
4ad4eba5
AM
4414 h->root.u.c.p->alignment_power = align;
4415 else
4416 h->root.u.c.p->alignment_power = old_alignment;
4417 }
4418
66eb6687 4419 if (is_elf_hash_table (htab))
4ad4eba5 4420 {
4f3fedcf
AM
4421 /* Set a flag in the hash table entry indicating the type of
4422 reference or definition we just found. A dynamic symbol
4423 is one which is referenced or defined by both a regular
4424 object and a shared object. */
4425 bfd_boolean dynsym = FALSE;
4426
4427 /* Plugin symbols aren't normal. Don't set def_regular or
4428 ref_regular for them, or make them dynamic. */
4429 if ((abfd->flags & BFD_PLUGIN) != 0)
4430 ;
4431 else if (! dynamic)
4432 {
4433 if (! definition)
4434 {
4435 h->ref_regular = 1;
4436 if (bind != STB_WEAK)
4437 h->ref_regular_nonweak = 1;
4438 }
4439 else
4440 {
4441 h->def_regular = 1;
4442 if (h->def_dynamic)
4443 {
4444 h->def_dynamic = 0;
4445 h->ref_dynamic = 1;
4446 }
4447 }
4448
4449 /* If the indirect symbol has been forced local, don't
4450 make the real symbol dynamic. */
4451 if ((h == hi || !hi->forced_local)
0e1862bb 4452 && (bfd_link_dll (info)
4f3fedcf
AM
4453 || h->def_dynamic
4454 || h->ref_dynamic))
4455 dynsym = TRUE;
4456 }
4457 else
4458 {
4459 if (! definition)
4460 {
4461 h->ref_dynamic = 1;
4462 hi->ref_dynamic = 1;
4463 }
4464 else
4465 {
4466 h->def_dynamic = 1;
4467 hi->def_dynamic = 1;
4468 }
4469
4470 /* If the indirect symbol has been forced local, don't
4471 make the real symbol dynamic. */
4472 if ((h == hi || !hi->forced_local)
4473 && (h->def_regular
4474 || h->ref_regular
4475 || (h->u.weakdef != NULL
4476 && ! new_weakdef
4477 && h->u.weakdef->dynindx != -1)))
4478 dynsym = TRUE;
4479 }
4480
4481 /* Check to see if we need to add an indirect symbol for
4482 the default name. */
4483 if (definition
4484 || (!override && h->root.type == bfd_link_hash_common))
4485 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4486 sec, value, &old_bfd, &dynsym))
4487 goto error_free_vers;
4ad4eba5
AM
4488
4489 /* Check the alignment when a common symbol is involved. This
4490 can change when a common symbol is overridden by a normal
4491 definition or a common symbol is ignored due to the old
4492 normal definition. We need to make sure the maximum
4493 alignment is maintained. */
a4d8e49b 4494 if ((old_alignment || common)
4ad4eba5
AM
4495 && h->root.type != bfd_link_hash_common)
4496 {
4497 unsigned int common_align;
4498 unsigned int normal_align;
4499 unsigned int symbol_align;
4500 bfd *normal_bfd;
4501 bfd *common_bfd;
4502
3a81e825
AM
4503 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4504 || h->root.type == bfd_link_hash_defweak);
4505
4ad4eba5
AM
4506 symbol_align = ffs (h->root.u.def.value) - 1;
4507 if (h->root.u.def.section->owner != NULL
4508 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4509 {
4510 normal_align = h->root.u.def.section->alignment_power;
4511 if (normal_align > symbol_align)
4512 normal_align = symbol_align;
4513 }
4514 else
4515 normal_align = symbol_align;
4516
4517 if (old_alignment)
4518 {
4519 common_align = old_alignment;
4520 common_bfd = old_bfd;
4521 normal_bfd = abfd;
4522 }
4523 else
4524 {
4525 common_align = bfd_log2 (isym->st_value);
4526 common_bfd = abfd;
4527 normal_bfd = old_bfd;
4528 }
4529
4530 if (normal_align < common_align)
d07676f8
NC
4531 {
4532 /* PR binutils/2735 */
4533 if (normal_bfd == NULL)
4534 (*_bfd_error_handler)
4f3fedcf
AM
4535 (_("Warning: alignment %u of common symbol `%s' in %B is"
4536 " greater than the alignment (%u) of its section %A"),
d07676f8
NC
4537 common_bfd, h->root.u.def.section,
4538 1 << common_align, name, 1 << normal_align);
4539 else
4540 (*_bfd_error_handler)
4541 (_("Warning: alignment %u of symbol `%s' in %B"
4542 " is smaller than %u in %B"),
4543 normal_bfd, common_bfd,
4544 1 << normal_align, name, 1 << common_align);
4545 }
4ad4eba5
AM
4546 }
4547
83ad0046 4548 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
4549 if (isym->st_size != 0
4550 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
4551 && (definition || h->size == 0))
4552 {
83ad0046
L
4553 if (h->size != 0
4554 && h->size != isym->st_size
4555 && ! size_change_ok)
4ad4eba5 4556 (*_bfd_error_handler)
d003868e
AM
4557 (_("Warning: size of symbol `%s' changed"
4558 " from %lu in %B to %lu in %B"),
4559 old_bfd, abfd,
4ad4eba5 4560 name, (unsigned long) h->size,
d003868e 4561 (unsigned long) isym->st_size);
4ad4eba5
AM
4562
4563 h->size = isym->st_size;
4564 }
4565
4566 /* If this is a common symbol, then we always want H->SIZE
4567 to be the size of the common symbol. The code just above
4568 won't fix the size if a common symbol becomes larger. We
4569 don't warn about a size change here, because that is
4f3fedcf 4570 covered by --warn-common. Allow changes between different
fcb93ecf 4571 function types. */
4ad4eba5
AM
4572 if (h->root.type == bfd_link_hash_common)
4573 h->size = h->root.u.c.size;
4574
4575 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
4576 && ((definition && !new_weak)
4577 || (old_weak && h->root.type == bfd_link_hash_common)
4578 || h->type == STT_NOTYPE))
4ad4eba5 4579 {
2955ec4c
L
4580 unsigned int type = ELF_ST_TYPE (isym->st_info);
4581
4582 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4583 symbol. */
4584 if (type == STT_GNU_IFUNC
4585 && (abfd->flags & DYNAMIC) != 0)
4586 type = STT_FUNC;
4ad4eba5 4587
2955ec4c
L
4588 if (h->type != type)
4589 {
4590 if (h->type != STT_NOTYPE && ! type_change_ok)
4591 (*_bfd_error_handler)
4592 (_("Warning: type of symbol `%s' changed"
4593 " from %d to %d in %B"),
4594 abfd, name, h->type, type);
4595
4596 h->type = type;
4597 }
4ad4eba5
AM
4598 }
4599
54ac0771 4600 /* Merge st_other field. */
b8417128 4601 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 4602
c3df8c14 4603 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
4604 if (definition
4605 && (sec->flags & SEC_DEBUGGING)
4606 && !bfd_link_relocatable (info))
c3df8c14
AM
4607 dynsym = FALSE;
4608
4f3fedcf
AM
4609 /* Nor should we make plugin symbols dynamic. */
4610 if ((abfd->flags & BFD_PLUGIN) != 0)
4611 dynsym = FALSE;
4612
35fc36a8 4613 if (definition)
35399224
L
4614 {
4615 h->target_internal = isym->st_target_internal;
4616 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4617 }
35fc36a8 4618
4ad4eba5
AM
4619 if (definition && !dynamic)
4620 {
4621 char *p = strchr (name, ELF_VER_CHR);
4622 if (p != NULL && p[1] != ELF_VER_CHR)
4623 {
4624 /* Queue non-default versions so that .symver x, x@FOO
4625 aliases can be checked. */
66eb6687 4626 if (!nondeflt_vers)
4ad4eba5 4627 {
66eb6687
AM
4628 amt = ((isymend - isym + 1)
4629 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
4630 nondeflt_vers
4631 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
4632 if (!nondeflt_vers)
4633 goto error_free_vers;
4ad4eba5 4634 }
66eb6687 4635 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4636 }
4637 }
4638
4639 if (dynsym && h->dynindx == -1)
4640 {
c152c796 4641 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4642 goto error_free_vers;
f6e332e6 4643 if (h->u.weakdef != NULL
4ad4eba5 4644 && ! new_weakdef
f6e332e6 4645 && h->u.weakdef->dynindx == -1)
4ad4eba5 4646 {
66eb6687 4647 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4648 goto error_free_vers;
4649 }
4650 }
1f599d0e 4651 else if (h->dynindx != -1)
4ad4eba5
AM
4652 /* If the symbol already has a dynamic index, but
4653 visibility says it should not be visible, turn it into
4654 a local symbol. */
4655 switch (ELF_ST_VISIBILITY (h->other))
4656 {
4657 case STV_INTERNAL:
4658 case STV_HIDDEN:
4659 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4660 dynsym = FALSE;
4661 break;
4662 }
4663
aef28989
L
4664 /* Don't add DT_NEEDED for references from the dummy bfd nor
4665 for unmatched symbol. */
4ad4eba5 4666 if (!add_needed
aef28989 4667 && matched
4ad4eba5 4668 && definition
010e5ae2 4669 && ((dynsym
ffa9430d 4670 && h->ref_regular_nonweak
4f3fedcf
AM
4671 && (old_bfd == NULL
4672 || (old_bfd->flags & BFD_PLUGIN) == 0))
ffa9430d 4673 || (h->ref_dynamic_nonweak
010e5ae2 4674 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
4675 && !on_needed_list (elf_dt_name (abfd),
4676 htab->needed, NULL))))
4ad4eba5
AM
4677 {
4678 int ret;
4679 const char *soname = elf_dt_name (abfd);
4680
16e4ecc0
AM
4681 info->callbacks->minfo ("%!", soname, old_bfd,
4682 h->root.root.string);
4683
4ad4eba5
AM
4684 /* A symbol from a library loaded via DT_NEEDED of some
4685 other library is referenced by a regular object.
e56f61be 4686 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
4687 --no-add-needed is used and the reference was not
4688 a weak one. */
4f3fedcf 4689 if (old_bfd != NULL
b918acf9 4690 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be
L
4691 {
4692 (*_bfd_error_handler)
3cbc5de0 4693 (_("%B: undefined reference to symbol '%s'"),
4f3fedcf 4694 old_bfd, name);
ff5ac77b 4695 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
4696 goto error_free_vers;
4697 }
4698
a50b1753 4699 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 4700 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 4701
4ad4eba5 4702 add_needed = TRUE;
7e9f0867 4703 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4704 if (ret < 0)
4705 goto error_free_vers;
4706
4707 BFD_ASSERT (ret == 0);
4708 }
4709 }
4710 }
4711
66eb6687
AM
4712 if (extversym != NULL)
4713 {
4714 free (extversym);
4715 extversym = NULL;
4716 }
4717
4718 if (isymbuf != NULL)
4719 {
4720 free (isymbuf);
4721 isymbuf = NULL;
4722 }
4723
4724 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4725 {
4726 unsigned int i;
4727
4728 /* Restore the symbol table. */
f45794cb
AM
4729 old_ent = (char *) old_tab + tabsize;
4730 memset (elf_sym_hashes (abfd), 0,
4731 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
4732 htab->root.table.table = old_table;
4733 htab->root.table.size = old_size;
4734 htab->root.table.count = old_count;
66eb6687 4735 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
4736 htab->root.undefs = old_undefs;
4737 htab->root.undefs_tail = old_undefs_tail;
d45f8bda 4738 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
66eb6687
AM
4739 for (i = 0; i < htab->root.table.size; i++)
4740 {
4741 struct bfd_hash_entry *p;
4742 struct elf_link_hash_entry *h;
3e0882af
L
4743 bfd_size_type size;
4744 unsigned int alignment_power;
66eb6687
AM
4745
4746 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4747 {
4748 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4749 if (h->root.type == bfd_link_hash_warning)
4750 h = (struct elf_link_hash_entry *) h->root.u.i.link;
a4542f1b
AM
4751 if (h->dynindx >= old_dynsymcount
4752 && h->dynstr_index < old_dynstr_size)
66eb6687 4753 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4754
3e0882af
L
4755 /* Preserve the maximum alignment and size for common
4756 symbols even if this dynamic lib isn't on DT_NEEDED
a4542f1b 4757 since it can still be loaded at run time by another
3e0882af
L
4758 dynamic lib. */
4759 if (h->root.type == bfd_link_hash_common)
4760 {
4761 size = h->root.u.c.size;
4762 alignment_power = h->root.u.c.p->alignment_power;
4763 }
4764 else
4765 {
4766 size = 0;
4767 alignment_power = 0;
4768 }
66eb6687
AM
4769 memcpy (p, old_ent, htab->root.table.entsize);
4770 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4771 h = (struct elf_link_hash_entry *) p;
4772 if (h->root.type == bfd_link_hash_warning)
4773 {
4774 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4775 old_ent = (char *) old_ent + htab->root.table.entsize;
a4542f1b 4776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2de92251 4777 }
a4542f1b 4778 if (h->root.type == bfd_link_hash_common)
3e0882af
L
4779 {
4780 if (size > h->root.u.c.size)
4781 h->root.u.c.size = size;
4782 if (alignment_power > h->root.u.c.p->alignment_power)
4783 h->root.u.c.p->alignment_power = alignment_power;
4784 }
66eb6687
AM
4785 }
4786 }
4787
5061a885
AM
4788 /* Make a special call to the linker "notice" function to
4789 tell it that symbols added for crefs may need to be removed. */
e5034e59 4790 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 4791 goto error_free_vers;
5061a885 4792
66eb6687
AM
4793 free (old_tab);
4794 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4795 alloc_mark);
4796 if (nondeflt_vers != NULL)
4797 free (nondeflt_vers);
4798 return TRUE;
4799 }
2de92251 4800
66eb6687
AM
4801 if (old_tab != NULL)
4802 {
e5034e59 4803 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 4804 goto error_free_vers;
66eb6687
AM
4805 free (old_tab);
4806 old_tab = NULL;
4807 }
4808
c6e8a9a8
L
4809 /* Now that all the symbols from this input file are created, if
4810 not performing a relocatable link, handle .symver foo, foo@BAR
4811 such that any relocs against foo become foo@BAR. */
0e1862bb 4812 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5
AM
4813 {
4814 bfd_size_type cnt, symidx;
4815
4816 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4817 {
4818 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4819 char *shortname, *p;
4820
4821 p = strchr (h->root.root.string, ELF_VER_CHR);
4822 if (p == NULL
4823 || (h->root.type != bfd_link_hash_defined
4824 && h->root.type != bfd_link_hash_defweak))
4825 continue;
4826
4827 amt = p - h->root.root.string;
a50b1753 4828 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
4829 if (!shortname)
4830 goto error_free_vers;
4ad4eba5
AM
4831 memcpy (shortname, h->root.root.string, amt);
4832 shortname[amt] = '\0';
4833
4834 hi = (struct elf_link_hash_entry *)
66eb6687 4835 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4836 FALSE, FALSE, FALSE);
4837 if (hi != NULL
4838 && hi->root.type == h->root.type
4839 && hi->root.u.def.value == h->root.u.def.value
4840 && hi->root.u.def.section == h->root.u.def.section)
4841 {
4842 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4843 hi->root.type = bfd_link_hash_indirect;
4844 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4845 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4846 sym_hash = elf_sym_hashes (abfd);
4847 if (sym_hash)
4848 for (symidx = 0; symidx < extsymcount; ++symidx)
4849 if (sym_hash[symidx] == hi)
4850 {
4851 sym_hash[symidx] = h;
4852 break;
4853 }
4854 }
4855 free (shortname);
4856 }
4857 free (nondeflt_vers);
4858 nondeflt_vers = NULL;
4859 }
4860
4ad4eba5
AM
4861 /* Now set the weakdefs field correctly for all the weak defined
4862 symbols we found. The only way to do this is to search all the
4863 symbols. Since we only need the information for non functions in
4864 dynamic objects, that's the only time we actually put anything on
4865 the list WEAKS. We need this information so that if a regular
4866 object refers to a symbol defined weakly in a dynamic object, the
4867 real symbol in the dynamic object is also put in the dynamic
4868 symbols; we also must arrange for both symbols to point to the
4869 same memory location. We could handle the general case of symbol
4870 aliasing, but a general symbol alias can only be generated in
4871 assembler code, handling it correctly would be very time
4872 consuming, and other ELF linkers don't handle general aliasing
4873 either. */
4874 if (weaks != NULL)
4875 {
4876 struct elf_link_hash_entry **hpp;
4877 struct elf_link_hash_entry **hppend;
4878 struct elf_link_hash_entry **sorted_sym_hash;
4879 struct elf_link_hash_entry *h;
4880 size_t sym_count;
4881
4882 /* Since we have to search the whole symbol list for each weak
4883 defined symbol, search time for N weak defined symbols will be
4884 O(N^2). Binary search will cut it down to O(NlogN). */
4885 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
a50b1753 4886 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4ad4eba5
AM
4887 if (sorted_sym_hash == NULL)
4888 goto error_return;
4889 sym_hash = sorted_sym_hash;
4890 hpp = elf_sym_hashes (abfd);
4891 hppend = hpp + extsymcount;
4892 sym_count = 0;
4893 for (; hpp < hppend; hpp++)
4894 {
4895 h = *hpp;
4896 if (h != NULL
4897 && h->root.type == bfd_link_hash_defined
fcb93ecf 4898 && !bed->is_function_type (h->type))
4ad4eba5
AM
4899 {
4900 *sym_hash = h;
4901 sym_hash++;
4902 sym_count++;
4903 }
4904 }
4905
4906 qsort (sorted_sym_hash, sym_count,
4907 sizeof (struct elf_link_hash_entry *),
4908 elf_sort_symbol);
4909
4910 while (weaks != NULL)
4911 {
4912 struct elf_link_hash_entry *hlook;
4913 asection *slook;
4914 bfd_vma vlook;
ed54588d 4915 size_t i, j, idx = 0;
4ad4eba5
AM
4916
4917 hlook = weaks;
f6e332e6
AM
4918 weaks = hlook->u.weakdef;
4919 hlook->u.weakdef = NULL;
4ad4eba5
AM
4920
4921 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4922 || hlook->root.type == bfd_link_hash_defweak
4923 || hlook->root.type == bfd_link_hash_common
4924 || hlook->root.type == bfd_link_hash_indirect);
4925 slook = hlook->root.u.def.section;
4926 vlook = hlook->root.u.def.value;
4927
4ad4eba5
AM
4928 i = 0;
4929 j = sym_count;
14160578 4930 while (i != j)
4ad4eba5
AM
4931 {
4932 bfd_signed_vma vdiff;
4933 idx = (i + j) / 2;
14160578 4934 h = sorted_sym_hash[idx];
4ad4eba5
AM
4935 vdiff = vlook - h->root.u.def.value;
4936 if (vdiff < 0)
4937 j = idx;
4938 else if (vdiff > 0)
4939 i = idx + 1;
4940 else
4941 {
d3435ae8 4942 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4943 if (sdiff < 0)
4944 j = idx;
4945 else if (sdiff > 0)
4946 i = idx + 1;
4947 else
14160578 4948 break;
4ad4eba5
AM
4949 }
4950 }
4951
4952 /* We didn't find a value/section match. */
14160578 4953 if (i == j)
4ad4eba5
AM
4954 continue;
4955
14160578
AM
4956 /* With multiple aliases, or when the weak symbol is already
4957 strongly defined, we have multiple matching symbols and
4958 the binary search above may land on any of them. Step
4959 one past the matching symbol(s). */
4960 while (++idx != j)
4961 {
4962 h = sorted_sym_hash[idx];
4963 if (h->root.u.def.section != slook
4964 || h->root.u.def.value != vlook)
4965 break;
4966 }
4967
4968 /* Now look back over the aliases. Since we sorted by size
4969 as well as value and section, we'll choose the one with
4970 the largest size. */
4971 while (idx-- != i)
4ad4eba5 4972 {
14160578 4973 h = sorted_sym_hash[idx];
4ad4eba5
AM
4974
4975 /* Stop if value or section doesn't match. */
14160578
AM
4976 if (h->root.u.def.section != slook
4977 || h->root.u.def.value != vlook)
4ad4eba5
AM
4978 break;
4979 else if (h != hlook)
4980 {
f6e332e6 4981 hlook->u.weakdef = h;
4ad4eba5
AM
4982
4983 /* If the weak definition is in the list of dynamic
4984 symbols, make sure the real definition is put
4985 there as well. */
4986 if (hlook->dynindx != -1 && h->dynindx == -1)
4987 {
c152c796 4988 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
4989 {
4990 err_free_sym_hash:
4991 free (sorted_sym_hash);
4992 goto error_return;
4993 }
4ad4eba5
AM
4994 }
4995
4996 /* If the real definition is in the list of dynamic
4997 symbols, make sure the weak definition is put
4998 there as well. If we don't do this, then the
4999 dynamic loader might not merge the entries for the
5000 real definition and the weak definition. */
5001 if (h->dynindx != -1 && hlook->dynindx == -1)
5002 {
c152c796 5003 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5004 goto err_free_sym_hash;
4ad4eba5
AM
5005 }
5006 break;
5007 }
5008 }
5009 }
5010
5011 free (sorted_sym_hash);
5012 }
5013
33177bb1
AM
5014 if (bed->check_directives
5015 && !(*bed->check_directives) (abfd, info))
5016 return FALSE;
85fbca6a 5017
d9689752
L
5018 if (!info->check_relocs_after_open_input
5019 && !_bfd_elf_link_check_relocs (abfd, info))
5020 return FALSE;
4ad4eba5
AM
5021
5022 /* If this is a non-traditional link, try to optimize the handling
5023 of the .stab/.stabstr sections. */
5024 if (! dynamic
5025 && ! info->traditional_format
66eb6687 5026 && is_elf_hash_table (htab)
4ad4eba5
AM
5027 && (info->strip != strip_all && info->strip != strip_debugger))
5028 {
5029 asection *stabstr;
5030
5031 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5032 if (stabstr != NULL)
5033 {
5034 bfd_size_type string_offset = 0;
5035 asection *stab;
5036
5037 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5038 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5039 && (!stab->name[5] ||
5040 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5041 && (stab->flags & SEC_MERGE) == 0
5042 && !bfd_is_abs_section (stab->output_section))
5043 {
5044 struct bfd_elf_section_data *secdata;
5045
5046 secdata = elf_section_data (stab);
66eb6687
AM
5047 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5048 stabstr, &secdata->sec_info,
4ad4eba5
AM
5049 &string_offset))
5050 goto error_return;
5051 if (secdata->sec_info)
dbaa2011 5052 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5053 }
5054 }
5055 }
5056
66eb6687 5057 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
5058 {
5059 /* Add this bfd to the loaded list. */
5060 struct elf_link_loaded_list *n;
5061
ca4be51c 5062 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5063 if (n == NULL)
5064 goto error_return;
5065 n->abfd = abfd;
66eb6687
AM
5066 n->next = htab->loaded;
5067 htab->loaded = n;
4ad4eba5
AM
5068 }
5069
5070 return TRUE;
5071
5072 error_free_vers:
66eb6687
AM
5073 if (old_tab != NULL)
5074 free (old_tab);
4ad4eba5
AM
5075 if (nondeflt_vers != NULL)
5076 free (nondeflt_vers);
5077 if (extversym != NULL)
5078 free (extversym);
5079 error_free_sym:
5080 if (isymbuf != NULL)
5081 free (isymbuf);
5082 error_return:
5083 return FALSE;
5084}
5085
8387904d
AM
5086/* Return the linker hash table entry of a symbol that might be
5087 satisfied by an archive symbol. Return -1 on error. */
5088
5089struct elf_link_hash_entry *
5090_bfd_elf_archive_symbol_lookup (bfd *abfd,
5091 struct bfd_link_info *info,
5092 const char *name)
5093{
5094 struct elf_link_hash_entry *h;
5095 char *p, *copy;
5096 size_t len, first;
5097
2a41f396 5098 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5099 if (h != NULL)
5100 return h;
5101
5102 /* If this is a default version (the name contains @@), look up the
5103 symbol again with only one `@' as well as without the version.
5104 The effect is that references to the symbol with and without the
5105 version will be matched by the default symbol in the archive. */
5106
5107 p = strchr (name, ELF_VER_CHR);
5108 if (p == NULL || p[1] != ELF_VER_CHR)
5109 return h;
5110
5111 /* First check with only one `@'. */
5112 len = strlen (name);
a50b1753 5113 copy = (char *) bfd_alloc (abfd, len);
8387904d
AM
5114 if (copy == NULL)
5115 return (struct elf_link_hash_entry *) 0 - 1;
5116
5117 first = p - name + 1;
5118 memcpy (copy, name, first);
5119 memcpy (copy + first, name + first + 1, len - first);
5120
2a41f396 5121 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5122 if (h == NULL)
5123 {
5124 /* We also need to check references to the symbol without the
5125 version. */
5126 copy[first - 1] = '\0';
5127 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5128 FALSE, FALSE, TRUE);
8387904d
AM
5129 }
5130
5131 bfd_release (abfd, copy);
5132 return h;
5133}
5134
0ad989f9 5135/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5136 don't use _bfd_generic_link_add_archive_symbols because we need to
5137 handle versioned symbols.
0ad989f9
L
5138
5139 Fortunately, ELF archive handling is simpler than that done by
5140 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5141 oddities. In ELF, if we find a symbol in the archive map, and the
5142 symbol is currently undefined, we know that we must pull in that
5143 object file.
5144
5145 Unfortunately, we do have to make multiple passes over the symbol
5146 table until nothing further is resolved. */
5147
4ad4eba5
AM
5148static bfd_boolean
5149elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5150{
5151 symindex c;
13e570f8 5152 unsigned char *included = NULL;
0ad989f9
L
5153 carsym *symdefs;
5154 bfd_boolean loop;
5155 bfd_size_type amt;
8387904d
AM
5156 const struct elf_backend_data *bed;
5157 struct elf_link_hash_entry * (*archive_symbol_lookup)
5158 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5159
5160 if (! bfd_has_map (abfd))
5161 {
5162 /* An empty archive is a special case. */
5163 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5164 return TRUE;
5165 bfd_set_error (bfd_error_no_armap);
5166 return FALSE;
5167 }
5168
5169 /* Keep track of all symbols we know to be already defined, and all
5170 files we know to be already included. This is to speed up the
5171 second and subsequent passes. */
5172 c = bfd_ardata (abfd)->symdef_count;
5173 if (c == 0)
5174 return TRUE;
5175 amt = c;
13e570f8
AM
5176 amt *= sizeof (*included);
5177 included = (unsigned char *) bfd_zmalloc (amt);
5178 if (included == NULL)
5179 return FALSE;
0ad989f9
L
5180
5181 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5182 bed = get_elf_backend_data (abfd);
5183 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5184
5185 do
5186 {
5187 file_ptr last;
5188 symindex i;
5189 carsym *symdef;
5190 carsym *symdefend;
5191
5192 loop = FALSE;
5193 last = -1;
5194
5195 symdef = symdefs;
5196 symdefend = symdef + c;
5197 for (i = 0; symdef < symdefend; symdef++, i++)
5198 {
5199 struct elf_link_hash_entry *h;
5200 bfd *element;
5201 struct bfd_link_hash_entry *undefs_tail;
5202 symindex mark;
5203
13e570f8 5204 if (included[i])
0ad989f9
L
5205 continue;
5206 if (symdef->file_offset == last)
5207 {
5208 included[i] = TRUE;
5209 continue;
5210 }
5211
8387904d
AM
5212 h = archive_symbol_lookup (abfd, info, symdef->name);
5213 if (h == (struct elf_link_hash_entry *) 0 - 1)
5214 goto error_return;
0ad989f9
L
5215
5216 if (h == NULL)
5217 continue;
5218
5219 if (h->root.type == bfd_link_hash_common)
5220 {
5221 /* We currently have a common symbol. The archive map contains
5222 a reference to this symbol, so we may want to include it. We
5223 only want to include it however, if this archive element
5224 contains a definition of the symbol, not just another common
5225 declaration of it.
5226
5227 Unfortunately some archivers (including GNU ar) will put
5228 declarations of common symbols into their archive maps, as
5229 well as real definitions, so we cannot just go by the archive
5230 map alone. Instead we must read in the element's symbol
5231 table and check that to see what kind of symbol definition
5232 this is. */
5233 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5234 continue;
5235 }
5236 else if (h->root.type != bfd_link_hash_undefined)
5237 {
5238 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5239 /* Symbol must be defined. Don't check it again. */
5240 included[i] = TRUE;
0ad989f9
L
5241 continue;
5242 }
5243
5244 /* We need to include this archive member. */
5245 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5246 if (element == NULL)
5247 goto error_return;
5248
5249 if (! bfd_check_format (element, bfd_object))
5250 goto error_return;
5251
0ad989f9
L
5252 undefs_tail = info->hash->undefs_tail;
5253
0e144ba7
AM
5254 if (!(*info->callbacks
5255 ->add_archive_element) (info, element, symdef->name, &element))
0ad989f9 5256 goto error_return;
0e144ba7 5257 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5258 goto error_return;
5259
5260 /* If there are any new undefined symbols, we need to make
5261 another pass through the archive in order to see whether
5262 they can be defined. FIXME: This isn't perfect, because
5263 common symbols wind up on undefs_tail and because an
5264 undefined symbol which is defined later on in this pass
5265 does not require another pass. This isn't a bug, but it
5266 does make the code less efficient than it could be. */
5267 if (undefs_tail != info->hash->undefs_tail)
5268 loop = TRUE;
5269
5270 /* Look backward to mark all symbols from this object file
5271 which we have already seen in this pass. */
5272 mark = i;
5273 do
5274 {
5275 included[mark] = TRUE;
5276 if (mark == 0)
5277 break;
5278 --mark;
5279 }
5280 while (symdefs[mark].file_offset == symdef->file_offset);
5281
5282 /* We mark subsequent symbols from this object file as we go
5283 on through the loop. */
5284 last = symdef->file_offset;
5285 }
5286 }
5287 while (loop);
5288
0ad989f9
L
5289 free (included);
5290
5291 return TRUE;
5292
5293 error_return:
0ad989f9
L
5294 if (included != NULL)
5295 free (included);
5296 return FALSE;
5297}
4ad4eba5
AM
5298
5299/* Given an ELF BFD, add symbols to the global hash table as
5300 appropriate. */
5301
5302bfd_boolean
5303bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5304{
5305 switch (bfd_get_format (abfd))
5306 {
5307 case bfd_object:
5308 return elf_link_add_object_symbols (abfd, info);
5309 case bfd_archive:
5310 return elf_link_add_archive_symbols (abfd, info);
5311 default:
5312 bfd_set_error (bfd_error_wrong_format);
5313 return FALSE;
5314 }
5315}
5a580b3a 5316\f
14b1c01e
AM
5317struct hash_codes_info
5318{
5319 unsigned long *hashcodes;
5320 bfd_boolean error;
5321};
a0c8462f 5322
5a580b3a
AM
5323/* This function will be called though elf_link_hash_traverse to store
5324 all hash value of the exported symbols in an array. */
5325
5326static bfd_boolean
5327elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5328{
a50b1753 5329 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5330 const char *name;
5a580b3a
AM
5331 unsigned long ha;
5332 char *alc = NULL;
5333
5a580b3a
AM
5334 /* Ignore indirect symbols. These are added by the versioning code. */
5335 if (h->dynindx == -1)
5336 return TRUE;
5337
5338 name = h->root.root.string;
422f1182 5339 if (h->versioned >= versioned)
5a580b3a 5340 {
422f1182
L
5341 char *p = strchr (name, ELF_VER_CHR);
5342 if (p != NULL)
14b1c01e 5343 {
422f1182
L
5344 alc = (char *) bfd_malloc (p - name + 1);
5345 if (alc == NULL)
5346 {
5347 inf->error = TRUE;
5348 return FALSE;
5349 }
5350 memcpy (alc, name, p - name);
5351 alc[p - name] = '\0';
5352 name = alc;
14b1c01e 5353 }
5a580b3a
AM
5354 }
5355
5356 /* Compute the hash value. */
5357 ha = bfd_elf_hash (name);
5358
5359 /* Store the found hash value in the array given as the argument. */
14b1c01e 5360 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5361
5362 /* And store it in the struct so that we can put it in the hash table
5363 later. */
f6e332e6 5364 h->u.elf_hash_value = ha;
5a580b3a
AM
5365
5366 if (alc != NULL)
5367 free (alc);
5368
5369 return TRUE;
5370}
5371
fdc90cb4
JJ
5372struct collect_gnu_hash_codes
5373{
5374 bfd *output_bfd;
5375 const struct elf_backend_data *bed;
5376 unsigned long int nsyms;
5377 unsigned long int maskbits;
5378 unsigned long int *hashcodes;
5379 unsigned long int *hashval;
5380 unsigned long int *indx;
5381 unsigned long int *counts;
5382 bfd_vma *bitmask;
5383 bfd_byte *contents;
5384 long int min_dynindx;
5385 unsigned long int bucketcount;
5386 unsigned long int symindx;
5387 long int local_indx;
5388 long int shift1, shift2;
5389 unsigned long int mask;
14b1c01e 5390 bfd_boolean error;
fdc90cb4
JJ
5391};
5392
5393/* This function will be called though elf_link_hash_traverse to store
5394 all hash value of the exported symbols in an array. */
5395
5396static bfd_boolean
5397elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5398{
a50b1753 5399 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 5400 const char *name;
fdc90cb4
JJ
5401 unsigned long ha;
5402 char *alc = NULL;
5403
fdc90cb4
JJ
5404 /* Ignore indirect symbols. These are added by the versioning code. */
5405 if (h->dynindx == -1)
5406 return TRUE;
5407
5408 /* Ignore also local symbols and undefined symbols. */
5409 if (! (*s->bed->elf_hash_symbol) (h))
5410 return TRUE;
5411
5412 name = h->root.root.string;
422f1182 5413 if (h->versioned >= versioned)
fdc90cb4 5414 {
422f1182
L
5415 char *p = strchr (name, ELF_VER_CHR);
5416 if (p != NULL)
14b1c01e 5417 {
422f1182
L
5418 alc = (char *) bfd_malloc (p - name + 1);
5419 if (alc == NULL)
5420 {
5421 s->error = TRUE;
5422 return FALSE;
5423 }
5424 memcpy (alc, name, p - name);
5425 alc[p - name] = '\0';
5426 name = alc;
14b1c01e 5427 }
fdc90cb4
JJ
5428 }
5429
5430 /* Compute the hash value. */
5431 ha = bfd_elf_gnu_hash (name);
5432
5433 /* Store the found hash value in the array for compute_bucket_count,
5434 and also for .dynsym reordering purposes. */
5435 s->hashcodes[s->nsyms] = ha;
5436 s->hashval[h->dynindx] = ha;
5437 ++s->nsyms;
5438 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5439 s->min_dynindx = h->dynindx;
5440
5441 if (alc != NULL)
5442 free (alc);
5443
5444 return TRUE;
5445}
5446
5447/* This function will be called though elf_link_hash_traverse to do
5448 final dynaminc symbol renumbering. */
5449
5450static bfd_boolean
5451elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5452{
a50b1753 5453 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
5454 unsigned long int bucket;
5455 unsigned long int val;
5456
fdc90cb4
JJ
5457 /* Ignore indirect symbols. */
5458 if (h->dynindx == -1)
5459 return TRUE;
5460
5461 /* Ignore also local symbols and undefined symbols. */
5462 if (! (*s->bed->elf_hash_symbol) (h))
5463 {
5464 if (h->dynindx >= s->min_dynindx)
5465 h->dynindx = s->local_indx++;
5466 return TRUE;
5467 }
5468
5469 bucket = s->hashval[h->dynindx] % s->bucketcount;
5470 val = (s->hashval[h->dynindx] >> s->shift1)
5471 & ((s->maskbits >> s->shift1) - 1);
5472 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5473 s->bitmask[val]
5474 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5475 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5476 if (s->counts[bucket] == 1)
5477 /* Last element terminates the chain. */
5478 val |= 1;
5479 bfd_put_32 (s->output_bfd, val,
5480 s->contents + (s->indx[bucket] - s->symindx) * 4);
5481 --s->counts[bucket];
5482 h->dynindx = s->indx[bucket]++;
5483 return TRUE;
5484}
5485
5486/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5487
5488bfd_boolean
5489_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5490{
5491 return !(h->forced_local
5492 || h->root.type == bfd_link_hash_undefined
5493 || h->root.type == bfd_link_hash_undefweak
5494 || ((h->root.type == bfd_link_hash_defined
5495 || h->root.type == bfd_link_hash_defweak)
5496 && h->root.u.def.section->output_section == NULL));
5497}
5498
5a580b3a
AM
5499/* Array used to determine the number of hash table buckets to use
5500 based on the number of symbols there are. If there are fewer than
5501 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5502 fewer than 37 we use 17 buckets, and so forth. We never use more
5503 than 32771 buckets. */
5504
5505static const size_t elf_buckets[] =
5506{
5507 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5508 16411, 32771, 0
5509};
5510
5511/* Compute bucket count for hashing table. We do not use a static set
5512 of possible tables sizes anymore. Instead we determine for all
5513 possible reasonable sizes of the table the outcome (i.e., the
5514 number of collisions etc) and choose the best solution. The
5515 weighting functions are not too simple to allow the table to grow
5516 without bounds. Instead one of the weighting factors is the size.
5517 Therefore the result is always a good payoff between few collisions
5518 (= short chain lengths) and table size. */
5519static size_t
b20dd2ce 5520compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
5521 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5522 unsigned long int nsyms,
5523 int gnu_hash)
5a580b3a 5524{
5a580b3a 5525 size_t best_size = 0;
5a580b3a 5526 unsigned long int i;
5a580b3a 5527
5a580b3a
AM
5528 /* We have a problem here. The following code to optimize the table
5529 size requires an integer type with more the 32 bits. If
5530 BFD_HOST_U_64_BIT is set we know about such a type. */
5531#ifdef BFD_HOST_U_64_BIT
5532 if (info->optimize)
5533 {
5a580b3a
AM
5534 size_t minsize;
5535 size_t maxsize;
5536 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 5537 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 5538 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 5539 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5540 unsigned long int *counts;
d40f3da9 5541 bfd_size_type amt;
0883b6e0 5542 unsigned int no_improvement_count = 0;
5a580b3a
AM
5543
5544 /* Possible optimization parameters: if we have NSYMS symbols we say
5545 that the hashing table must at least have NSYMS/4 and at most
5546 2*NSYMS buckets. */
5547 minsize = nsyms / 4;
5548 if (minsize == 0)
5549 minsize = 1;
5550 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5551 if (gnu_hash)
5552 {
5553 if (minsize < 2)
5554 minsize = 2;
5555 if ((best_size & 31) == 0)
5556 ++best_size;
5557 }
5a580b3a
AM
5558
5559 /* Create array where we count the collisions in. We must use bfd_malloc
5560 since the size could be large. */
5561 amt = maxsize;
5562 amt *= sizeof (unsigned long int);
a50b1753 5563 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 5564 if (counts == NULL)
fdc90cb4 5565 return 0;
5a580b3a
AM
5566
5567 /* Compute the "optimal" size for the hash table. The criteria is a
5568 minimal chain length. The minor criteria is (of course) the size
5569 of the table. */
5570 for (i = minsize; i < maxsize; ++i)
5571 {
5572 /* Walk through the array of hashcodes and count the collisions. */
5573 BFD_HOST_U_64_BIT max;
5574 unsigned long int j;
5575 unsigned long int fact;
5576
fdc90cb4
JJ
5577 if (gnu_hash && (i & 31) == 0)
5578 continue;
5579
5a580b3a
AM
5580 memset (counts, '\0', i * sizeof (unsigned long int));
5581
5582 /* Determine how often each hash bucket is used. */
5583 for (j = 0; j < nsyms; ++j)
5584 ++counts[hashcodes[j] % i];
5585
5586 /* For the weight function we need some information about the
5587 pagesize on the target. This is information need not be 100%
5588 accurate. Since this information is not available (so far) we
5589 define it here to a reasonable default value. If it is crucial
5590 to have a better value some day simply define this value. */
5591# ifndef BFD_TARGET_PAGESIZE
5592# define BFD_TARGET_PAGESIZE (4096)
5593# endif
5594
fdc90cb4
JJ
5595 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5596 and the chains. */
5597 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5598
5599# if 1
5600 /* Variant 1: optimize for short chains. We add the squares
5601 of all the chain lengths (which favors many small chain
5602 over a few long chains). */
5603 for (j = 0; j < i; ++j)
5604 max += counts[j] * counts[j];
5605
5606 /* This adds penalties for the overall size of the table. */
fdc90cb4 5607 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5608 max *= fact * fact;
5609# else
5610 /* Variant 2: Optimize a lot more for small table. Here we
5611 also add squares of the size but we also add penalties for
5612 empty slots (the +1 term). */
5613 for (j = 0; j < i; ++j)
5614 max += (1 + counts[j]) * (1 + counts[j]);
5615
5616 /* The overall size of the table is considered, but not as
5617 strong as in variant 1, where it is squared. */
fdc90cb4 5618 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5619 max *= fact;
5620# endif
5621
5622 /* Compare with current best results. */
5623 if (max < best_chlen)
5624 {
5625 best_chlen = max;
5626 best_size = i;
ca4be51c 5627 no_improvement_count = 0;
5a580b3a 5628 }
0883b6e0
NC
5629 /* PR 11843: Avoid futile long searches for the best bucket size
5630 when there are a large number of symbols. */
5631 else if (++no_improvement_count == 100)
5632 break;
5a580b3a
AM
5633 }
5634
5635 free (counts);
5636 }
5637 else
5638#endif /* defined (BFD_HOST_U_64_BIT) */
5639 {
5640 /* This is the fallback solution if no 64bit type is available or if we
5641 are not supposed to spend much time on optimizations. We select the
5642 bucket count using a fixed set of numbers. */
5643 for (i = 0; elf_buckets[i] != 0; i++)
5644 {
5645 best_size = elf_buckets[i];
fdc90cb4 5646 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5647 break;
5648 }
fdc90cb4
JJ
5649 if (gnu_hash && best_size < 2)
5650 best_size = 2;
5a580b3a
AM
5651 }
5652
5a580b3a
AM
5653 return best_size;
5654}
5655
d0bf826b
AM
5656/* Size any SHT_GROUP section for ld -r. */
5657
5658bfd_boolean
5659_bfd_elf_size_group_sections (struct bfd_link_info *info)
5660{
5661 bfd *ibfd;
5662
c72f2fb2 5663 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b
AM
5664 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5665 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5666 return FALSE;
5667 return TRUE;
5668}
5669
04c3a755
NS
5670/* Set a default stack segment size. The value in INFO wins. If it
5671 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5672 undefined it is initialized. */
5673
5674bfd_boolean
5675bfd_elf_stack_segment_size (bfd *output_bfd,
5676 struct bfd_link_info *info,
5677 const char *legacy_symbol,
5678 bfd_vma default_size)
5679{
5680 struct elf_link_hash_entry *h = NULL;
5681
5682 /* Look for legacy symbol. */
5683 if (legacy_symbol)
5684 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5685 FALSE, FALSE, FALSE);
5686 if (h && (h->root.type == bfd_link_hash_defined
5687 || h->root.type == bfd_link_hash_defweak)
5688 && h->def_regular
5689 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5690 {
5691 /* The symbol has no type if specified on the command line. */
5692 h->type = STT_OBJECT;
5693 if (info->stacksize)
5694 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5695 output_bfd, legacy_symbol);
5696 else if (h->root.u.def.section != bfd_abs_section_ptr)
5697 (*_bfd_error_handler) (_("%B: %s not absolute"),
5698 output_bfd, legacy_symbol);
5699 else
5700 info->stacksize = h->root.u.def.value;
5701 }
5702
5703 if (!info->stacksize)
5704 /* If the user didn't set a size, or explicitly inhibit the
5705 size, set it now. */
5706 info->stacksize = default_size;
5707
5708 /* Provide the legacy symbol, if it is referenced. */
5709 if (h && (h->root.type == bfd_link_hash_undefined
5710 || h->root.type == bfd_link_hash_undefweak))
5711 {
5712 struct bfd_link_hash_entry *bh = NULL;
5713
5714 if (!(_bfd_generic_link_add_one_symbol
5715 (info, output_bfd, legacy_symbol,
5716 BSF_GLOBAL, bfd_abs_section_ptr,
5717 info->stacksize >= 0 ? info->stacksize : 0,
5718 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5719 return FALSE;
5720
5721 h = (struct elf_link_hash_entry *) bh;
5722 h->def_regular = 1;
5723 h->type = STT_OBJECT;
5724 }
5725
5726 return TRUE;
5727}
5728
5a580b3a
AM
5729/* Set up the sizes and contents of the ELF dynamic sections. This is
5730 called by the ELF linker emulation before_allocation routine. We
5731 must set the sizes of the sections before the linker sets the
5732 addresses of the various sections. */
5733
5734bfd_boolean
5735bfd_elf_size_dynamic_sections (bfd *output_bfd,
5736 const char *soname,
5737 const char *rpath,
5738 const char *filter_shlib,
7ee314fa
AM
5739 const char *audit,
5740 const char *depaudit,
5a580b3a
AM
5741 const char * const *auxiliary_filters,
5742 struct bfd_link_info *info,
fd91d419 5743 asection **sinterpptr)
5a580b3a
AM
5744{
5745 bfd_size_type soname_indx;
5746 bfd *dynobj;
5747 const struct elf_backend_data *bed;
28caa186 5748 struct elf_info_failed asvinfo;
5a580b3a
AM
5749
5750 *sinterpptr = NULL;
5751
5752 soname_indx = (bfd_size_type) -1;
5753
5754 if (!is_elf_hash_table (info->hash))
5755 return TRUE;
5756
6bfdb61b 5757 bed = get_elf_backend_data (output_bfd);
04c3a755
NS
5758
5759 /* Any syms created from now on start with -1 in
5760 got.refcount/offset and plt.refcount/offset. */
5761 elf_hash_table (info)->init_got_refcount
5762 = elf_hash_table (info)->init_got_offset;
5763 elf_hash_table (info)->init_plt_refcount
5764 = elf_hash_table (info)->init_plt_offset;
5765
0e1862bb 5766 if (bfd_link_relocatable (info)
04c3a755
NS
5767 && !_bfd_elf_size_group_sections (info))
5768 return FALSE;
5769
5770 /* The backend may have to create some sections regardless of whether
5771 we're dynamic or not. */
5772 if (bed->elf_backend_always_size_sections
5773 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5774 return FALSE;
5775
5776 /* Determine any GNU_STACK segment requirements, after the backend
5777 has had a chance to set a default segment size. */
5a580b3a 5778 if (info->execstack)
12bd6957 5779 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5a580b3a 5780 else if (info->noexecstack)
12bd6957 5781 elf_stack_flags (output_bfd) = PF_R | PF_W;
5a580b3a
AM
5782 else
5783 {
5784 bfd *inputobj;
5785 asection *notesec = NULL;
5786 int exec = 0;
5787
5788 for (inputobj = info->input_bfds;
5789 inputobj;
c72f2fb2 5790 inputobj = inputobj->link.next)
5a580b3a
AM
5791 {
5792 asection *s;
5793
a92c088a
L
5794 if (inputobj->flags
5795 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5a580b3a
AM
5796 continue;
5797 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5798 if (s)
5799 {
5800 if (s->flags & SEC_CODE)
5801 exec = PF_X;
5802 notesec = s;
5803 }
6bfdb61b 5804 else if (bed->default_execstack)
5a580b3a
AM
5805 exec = PF_X;
5806 }
04c3a755 5807 if (notesec || info->stacksize > 0)
12bd6957 5808 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
0e1862bb 5809 if (notesec && exec && bfd_link_relocatable (info)
04c3a755
NS
5810 && notesec->output_section != bfd_abs_section_ptr)
5811 notesec->output_section->flags |= SEC_CODE;
5a580b3a
AM
5812 }
5813
5a580b3a
AM
5814 dynobj = elf_hash_table (info)->dynobj;
5815
9a2a56cc 5816 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a
AM
5817 {
5818 struct elf_info_failed eif;
5819 struct elf_link_hash_entry *h;
5820 asection *dynstr;
5821 struct bfd_elf_version_tree *t;
5822 struct bfd_elf_version_expr *d;
046183de 5823 asection *s;
5a580b3a
AM
5824 bfd_boolean all_defined;
5825
3d4d4302 5826 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
9b8b325a 5827 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5a580b3a
AM
5828
5829 if (soname != NULL)
5830 {
5831 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5832 soname, TRUE);
5833 if (soname_indx == (bfd_size_type) -1
5834 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5835 return FALSE;
5836 }
5837
5838 if (info->symbolic)
5839 {
5840 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5841 return FALSE;
5842 info->flags |= DF_SYMBOLIC;
5843 }
5844
5845 if (rpath != NULL)
5846 {
5847 bfd_size_type indx;
b1b00fcc 5848 bfd_vma tag;
5a580b3a
AM
5849
5850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5851 TRUE);
b1b00fcc 5852 if (indx == (bfd_size_type) -1)
5a580b3a
AM
5853 return FALSE;
5854
b1b00fcc
MF
5855 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5856 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5857 return FALSE;
5a580b3a
AM
5858 }
5859
5860 if (filter_shlib != NULL)
5861 {
5862 bfd_size_type indx;
5863
5864 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5865 filter_shlib, TRUE);
5866 if (indx == (bfd_size_type) -1
5867 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5868 return FALSE;
5869 }
5870
5871 if (auxiliary_filters != NULL)
5872 {
5873 const char * const *p;
5874
5875 for (p = auxiliary_filters; *p != NULL; p++)
5876 {
5877 bfd_size_type indx;
5878
5879 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5880 *p, TRUE);
5881 if (indx == (bfd_size_type) -1
5882 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5883 return FALSE;
5884 }
5885 }
5886
7ee314fa
AM
5887 if (audit != NULL)
5888 {
5889 bfd_size_type indx;
5890
5891 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5892 TRUE);
5893 if (indx == (bfd_size_type) -1
5894 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5895 return FALSE;
5896 }
5897
5898 if (depaudit != NULL)
5899 {
5900 bfd_size_type indx;
5901
5902 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5903 TRUE);
5904 if (indx == (bfd_size_type) -1
5905 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5906 return FALSE;
5907 }
5908
5a580b3a 5909 eif.info = info;
5a580b3a
AM
5910 eif.failed = FALSE;
5911
5912 /* If we are supposed to export all symbols into the dynamic symbol
5913 table (this is not the normal case), then do so. */
55255dae 5914 if (info->export_dynamic
0e1862bb 5915 || (bfd_link_executable (info) && info->dynamic))
5a580b3a
AM
5916 {
5917 elf_link_hash_traverse (elf_hash_table (info),
5918 _bfd_elf_export_symbol,
5919 &eif);
5920 if (eif.failed)
5921 return FALSE;
5922 }
5923
5924 /* Make all global versions with definition. */
fd91d419 5925 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5926 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5927 if (!d->symver && d->literal)
5a580b3a
AM
5928 {
5929 const char *verstr, *name;
5930 size_t namelen, verlen, newlen;
93252b1c 5931 char *newname, *p, leading_char;
5a580b3a
AM
5932 struct elf_link_hash_entry *newh;
5933
93252b1c 5934 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 5935 name = d->pattern;
93252b1c 5936 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
5937 verstr = t->name;
5938 verlen = strlen (verstr);
5939 newlen = namelen + verlen + 3;
5940
a50b1753 5941 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
5942 if (newname == NULL)
5943 return FALSE;
93252b1c
MF
5944 newname[0] = leading_char;
5945 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
5946
5947 /* Check the hidden versioned definition. */
5948 p = newname + namelen;
5949 *p++ = ELF_VER_CHR;
5950 memcpy (p, verstr, verlen + 1);
5951 newh = elf_link_hash_lookup (elf_hash_table (info),
5952 newname, FALSE, FALSE,
5953 FALSE);
5954 if (newh == NULL
5955 || (newh->root.type != bfd_link_hash_defined
5956 && newh->root.type != bfd_link_hash_defweak))
5957 {
5958 /* Check the default versioned definition. */
5959 *p++ = ELF_VER_CHR;
5960 memcpy (p, verstr, verlen + 1);
5961 newh = elf_link_hash_lookup (elf_hash_table (info),
5962 newname, FALSE, FALSE,
5963 FALSE);
5964 }
5965 free (newname);
5966
5967 /* Mark this version if there is a definition and it is
5968 not defined in a shared object. */
5969 if (newh != NULL
f5385ebf 5970 && !newh->def_dynamic
5a580b3a
AM
5971 && (newh->root.type == bfd_link_hash_defined
5972 || newh->root.type == bfd_link_hash_defweak))
5973 d->symver = 1;
5974 }
5975
5976 /* Attach all the symbols to their version information. */
5a580b3a 5977 asvinfo.info = info;
5a580b3a
AM
5978 asvinfo.failed = FALSE;
5979
5980 elf_link_hash_traverse (elf_hash_table (info),
5981 _bfd_elf_link_assign_sym_version,
5982 &asvinfo);
5983 if (asvinfo.failed)
5984 return FALSE;
5985
5986 if (!info->allow_undefined_version)
5987 {
5988 /* Check if all global versions have a definition. */
5989 all_defined = TRUE;
fd91d419 5990 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 5991 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 5992 if (d->literal && !d->symver && !d->script)
5a580b3a
AM
5993 {
5994 (*_bfd_error_handler)
5995 (_("%s: undefined version: %s"),
5996 d->pattern, t->name);
5997 all_defined = FALSE;
5998 }
5999
6000 if (!all_defined)
6001 {
6002 bfd_set_error (bfd_error_bad_value);
6003 return FALSE;
6004 }
6005 }
6006
6007 /* Find all symbols which were defined in a dynamic object and make
6008 the backend pick a reasonable value for them. */
6009 elf_link_hash_traverse (elf_hash_table (info),
6010 _bfd_elf_adjust_dynamic_symbol,
6011 &eif);
6012 if (eif.failed)
6013 return FALSE;
6014
6015 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 6016 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
6017 now so that we know the final size of the .dynamic section. */
6018
6019 /* If there are initialization and/or finalization functions to
6020 call then add the corresponding DT_INIT/DT_FINI entries. */
6021 h = (info->init_function
6022 ? elf_link_hash_lookup (elf_hash_table (info),
6023 info->init_function, FALSE,
6024 FALSE, FALSE)
6025 : NULL);
6026 if (h != NULL
f5385ebf
AM
6027 && (h->ref_regular
6028 || h->def_regular))
5a580b3a
AM
6029 {
6030 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6031 return FALSE;
6032 }
6033 h = (info->fini_function
6034 ? elf_link_hash_lookup (elf_hash_table (info),
6035 info->fini_function, FALSE,
6036 FALSE, FALSE)
6037 : NULL);
6038 if (h != NULL
f5385ebf
AM
6039 && (h->ref_regular
6040 || h->def_regular))
5a580b3a
AM
6041 {
6042 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6043 return FALSE;
6044 }
6045
046183de
AM
6046 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6047 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6048 {
6049 /* DT_PREINIT_ARRAY is not allowed in shared library. */
0e1862bb 6050 if (! bfd_link_executable (info))
5a580b3a
AM
6051 {
6052 bfd *sub;
6053 asection *o;
6054
6055 for (sub = info->input_bfds; sub != NULL;
c72f2fb2 6056 sub = sub->link.next)
3fcd97f1
JJ
6057 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6058 for (o = sub->sections; o != NULL; o = o->next)
6059 if (elf_section_data (o)->this_hdr.sh_type
6060 == SHT_PREINIT_ARRAY)
6061 {
6062 (*_bfd_error_handler)
6063 (_("%B: .preinit_array section is not allowed in DSO"),
6064 sub);
6065 break;
6066 }
5a580b3a
AM
6067
6068 bfd_set_error (bfd_error_nonrepresentable_section);
6069 return FALSE;
6070 }
6071
6072 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6073 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6074 return FALSE;
6075 }
046183de
AM
6076 s = bfd_get_section_by_name (output_bfd, ".init_array");
6077 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6078 {
6079 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6080 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6081 return FALSE;
6082 }
046183de
AM
6083 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6084 if (s != NULL && s->linker_has_input)
5a580b3a
AM
6085 {
6086 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6087 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6088 return FALSE;
6089 }
6090
3d4d4302 6091 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6092 /* If .dynstr is excluded from the link, we don't want any of
6093 these tags. Strictly, we should be checking each section
6094 individually; This quick check covers for the case where
6095 someone does a /DISCARD/ : { *(*) }. */
6096 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6097 {
6098 bfd_size_type strsize;
6099
6100 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
6101 if ((info->emit_hash
6102 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6103 || (info->emit_gnu_hash
6104 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
6105 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6106 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6107 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6108 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6109 bed->s->sizeof_sym))
6110 return FALSE;
6111 }
6112 }
6113
de231f20
CM
6114 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6115 return FALSE;
6116
5a580b3a
AM
6117 /* The backend must work out the sizes of all the other dynamic
6118 sections. */
9a2a56cc
AM
6119 if (dynobj != NULL
6120 && bed->elf_backend_size_dynamic_sections != NULL
5a580b3a
AM
6121 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6122 return FALSE;
6123
9a2a56cc 6124 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6125 {
554220db 6126 unsigned long section_sym_count;
fd91d419 6127 struct bfd_elf_version_tree *verdefs;
5a580b3a 6128 asection *s;
5a580b3a
AM
6129
6130 /* Set up the version definition section. */
3d4d4302 6131 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
6132 BFD_ASSERT (s != NULL);
6133
6134 /* We may have created additional version definitions if we are
6135 just linking a regular application. */
fd91d419 6136 verdefs = info->version_info;
5a580b3a
AM
6137
6138 /* Skip anonymous version tag. */
6139 if (verdefs != NULL && verdefs->vernum == 0)
6140 verdefs = verdefs->next;
6141
3e3b46e5 6142 if (verdefs == NULL && !info->create_default_symver)
8423293d 6143 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6144 else
6145 {
6146 unsigned int cdefs;
6147 bfd_size_type size;
6148 struct bfd_elf_version_tree *t;
6149 bfd_byte *p;
6150 Elf_Internal_Verdef def;
6151 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
6152 struct bfd_link_hash_entry *bh;
6153 struct elf_link_hash_entry *h;
6154 const char *name;
5a580b3a
AM
6155
6156 cdefs = 0;
6157 size = 0;
6158
6159 /* Make space for the base version. */
6160 size += sizeof (Elf_External_Verdef);
6161 size += sizeof (Elf_External_Verdaux);
6162 ++cdefs;
6163
3e3b46e5
PB
6164 /* Make space for the default version. */
6165 if (info->create_default_symver)
6166 {
6167 size += sizeof (Elf_External_Verdef);
6168 ++cdefs;
6169 }
6170
5a580b3a
AM
6171 for (t = verdefs; t != NULL; t = t->next)
6172 {
6173 struct bfd_elf_version_deps *n;
6174
a6cc6b3b
RO
6175 /* Don't emit base version twice. */
6176 if (t->vernum == 0)
6177 continue;
6178
5a580b3a
AM
6179 size += sizeof (Elf_External_Verdef);
6180 size += sizeof (Elf_External_Verdaux);
6181 ++cdefs;
6182
6183 for (n = t->deps; n != NULL; n = n->next)
6184 size += sizeof (Elf_External_Verdaux);
6185 }
6186
eea6121a 6187 s->size = size;
a50b1753 6188 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6189 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6190 return FALSE;
6191
6192 /* Fill in the version definition section. */
6193
6194 p = s->contents;
6195
6196 def.vd_version = VER_DEF_CURRENT;
6197 def.vd_flags = VER_FLG_BASE;
6198 def.vd_ndx = 1;
6199 def.vd_cnt = 1;
3e3b46e5
PB
6200 if (info->create_default_symver)
6201 {
6202 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6203 def.vd_next = sizeof (Elf_External_Verdef);
6204 }
6205 else
6206 {
6207 def.vd_aux = sizeof (Elf_External_Verdef);
6208 def.vd_next = (sizeof (Elf_External_Verdef)
6209 + sizeof (Elf_External_Verdaux));
6210 }
5a580b3a
AM
6211
6212 if (soname_indx != (bfd_size_type) -1)
6213 {
6214 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6215 soname_indx);
6216 def.vd_hash = bfd_elf_hash (soname);
6217 defaux.vda_name = soname_indx;
3e3b46e5 6218 name = soname;
5a580b3a
AM
6219 }
6220 else
6221 {
5a580b3a
AM
6222 bfd_size_type indx;
6223
06084812 6224 name = lbasename (output_bfd->filename);
5a580b3a
AM
6225 def.vd_hash = bfd_elf_hash (name);
6226 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6227 name, FALSE);
6228 if (indx == (bfd_size_type) -1)
6229 return FALSE;
6230 defaux.vda_name = indx;
6231 }
6232 defaux.vda_next = 0;
6233
6234 _bfd_elf_swap_verdef_out (output_bfd, &def,
6235 (Elf_External_Verdef *) p);
6236 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6237 if (info->create_default_symver)
6238 {
6239 /* Add a symbol representing this version. */
6240 bh = NULL;
6241 if (! (_bfd_generic_link_add_one_symbol
6242 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6243 0, NULL, FALSE,
6244 get_elf_backend_data (dynobj)->collect, &bh)))
6245 return FALSE;
6246 h = (struct elf_link_hash_entry *) bh;
6247 h->non_elf = 0;
6248 h->def_regular = 1;
6249 h->type = STT_OBJECT;
6250 h->verinfo.vertree = NULL;
6251
6252 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6253 return FALSE;
6254
6255 /* Create a duplicate of the base version with the same
6256 aux block, but different flags. */
6257 def.vd_flags = 0;
6258 def.vd_ndx = 2;
6259 def.vd_aux = sizeof (Elf_External_Verdef);
6260 if (verdefs)
6261 def.vd_next = (sizeof (Elf_External_Verdef)
6262 + sizeof (Elf_External_Verdaux));
6263 else
6264 def.vd_next = 0;
6265 _bfd_elf_swap_verdef_out (output_bfd, &def,
6266 (Elf_External_Verdef *) p);
6267 p += sizeof (Elf_External_Verdef);
6268 }
5a580b3a
AM
6269 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6270 (Elf_External_Verdaux *) p);
6271 p += sizeof (Elf_External_Verdaux);
6272
6273 for (t = verdefs; t != NULL; t = t->next)
6274 {
6275 unsigned int cdeps;
6276 struct bfd_elf_version_deps *n;
5a580b3a 6277
a6cc6b3b
RO
6278 /* Don't emit the base version twice. */
6279 if (t->vernum == 0)
6280 continue;
6281
5a580b3a
AM
6282 cdeps = 0;
6283 for (n = t->deps; n != NULL; n = n->next)
6284 ++cdeps;
6285
6286 /* Add a symbol representing this version. */
6287 bh = NULL;
6288 if (! (_bfd_generic_link_add_one_symbol
6289 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6290 0, NULL, FALSE,
6291 get_elf_backend_data (dynobj)->collect, &bh)))
6292 return FALSE;
6293 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6294 h->non_elf = 0;
6295 h->def_regular = 1;
5a580b3a
AM
6296 h->type = STT_OBJECT;
6297 h->verinfo.vertree = t;
6298
c152c796 6299 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6300 return FALSE;
6301
6302 def.vd_version = VER_DEF_CURRENT;
6303 def.vd_flags = 0;
6304 if (t->globals.list == NULL
6305 && t->locals.list == NULL
6306 && ! t->used)
6307 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6308 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6309 def.vd_cnt = cdeps + 1;
6310 def.vd_hash = bfd_elf_hash (t->name);
6311 def.vd_aux = sizeof (Elf_External_Verdef);
6312 def.vd_next = 0;
a6cc6b3b
RO
6313
6314 /* If a basever node is next, it *must* be the last node in
6315 the chain, otherwise Verdef construction breaks. */
6316 if (t->next != NULL && t->next->vernum == 0)
6317 BFD_ASSERT (t->next->next == NULL);
6318
6319 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6320 def.vd_next = (sizeof (Elf_External_Verdef)
6321 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6322
6323 _bfd_elf_swap_verdef_out (output_bfd, &def,
6324 (Elf_External_Verdef *) p);
6325 p += sizeof (Elf_External_Verdef);
6326
6327 defaux.vda_name = h->dynstr_index;
6328 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6329 h->dynstr_index);
6330 defaux.vda_next = 0;
6331 if (t->deps != NULL)
6332 defaux.vda_next = sizeof (Elf_External_Verdaux);
6333 t->name_indx = defaux.vda_name;
6334
6335 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6336 (Elf_External_Verdaux *) p);
6337 p += sizeof (Elf_External_Verdaux);
6338
6339 for (n = t->deps; n != NULL; n = n->next)
6340 {
6341 if (n->version_needed == NULL)
6342 {
6343 /* This can happen if there was an error in the
6344 version script. */
6345 defaux.vda_name = 0;
6346 }
6347 else
6348 {
6349 defaux.vda_name = n->version_needed->name_indx;
6350 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6351 defaux.vda_name);
6352 }
6353 if (n->next == NULL)
6354 defaux.vda_next = 0;
6355 else
6356 defaux.vda_next = sizeof (Elf_External_Verdaux);
6357
6358 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6359 (Elf_External_Verdaux *) p);
6360 p += sizeof (Elf_External_Verdaux);
6361 }
6362 }
6363
6364 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6365 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6366 return FALSE;
6367
6368 elf_tdata (output_bfd)->cverdefs = cdefs;
6369 }
6370
6371 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6372 {
6373 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6374 return FALSE;
6375 }
6376 else if (info->flags & DF_BIND_NOW)
6377 {
6378 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6379 return FALSE;
6380 }
6381
6382 if (info->flags_1)
6383 {
0e1862bb 6384 if (bfd_link_executable (info))
5a580b3a
AM
6385 info->flags_1 &= ~ (DF_1_INITFIRST
6386 | DF_1_NODELETE
6387 | DF_1_NOOPEN);
6388 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6389 return FALSE;
6390 }
6391
6392 /* Work out the size of the version reference section. */
6393
3d4d4302 6394 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
6395 BFD_ASSERT (s != NULL);
6396 {
6397 struct elf_find_verdep_info sinfo;
6398
5a580b3a
AM
6399 sinfo.info = info;
6400 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6401 if (sinfo.vers == 0)
6402 sinfo.vers = 1;
6403 sinfo.failed = FALSE;
6404
6405 elf_link_hash_traverse (elf_hash_table (info),
6406 _bfd_elf_link_find_version_dependencies,
6407 &sinfo);
14b1c01e
AM
6408 if (sinfo.failed)
6409 return FALSE;
5a580b3a
AM
6410
6411 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 6412 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
6413 else
6414 {
6415 Elf_Internal_Verneed *t;
6416 unsigned int size;
6417 unsigned int crefs;
6418 bfd_byte *p;
6419
a6cc6b3b 6420 /* Build the version dependency section. */
5a580b3a
AM
6421 size = 0;
6422 crefs = 0;
6423 for (t = elf_tdata (output_bfd)->verref;
6424 t != NULL;
6425 t = t->vn_nextref)
6426 {
6427 Elf_Internal_Vernaux *a;
6428
6429 size += sizeof (Elf_External_Verneed);
6430 ++crefs;
6431 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6432 size += sizeof (Elf_External_Vernaux);
6433 }
6434
eea6121a 6435 s->size = size;
a50b1753 6436 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6437 if (s->contents == NULL)
6438 return FALSE;
6439
6440 p = s->contents;
6441 for (t = elf_tdata (output_bfd)->verref;
6442 t != NULL;
6443 t = t->vn_nextref)
6444 {
6445 unsigned int caux;
6446 Elf_Internal_Vernaux *a;
6447 bfd_size_type indx;
6448
6449 caux = 0;
6450 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6451 ++caux;
6452
6453 t->vn_version = VER_NEED_CURRENT;
6454 t->vn_cnt = caux;
6455 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6456 elf_dt_name (t->vn_bfd) != NULL
6457 ? elf_dt_name (t->vn_bfd)
06084812 6458 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6459 FALSE);
6460 if (indx == (bfd_size_type) -1)
6461 return FALSE;
6462 t->vn_file = indx;
6463 t->vn_aux = sizeof (Elf_External_Verneed);
6464 if (t->vn_nextref == NULL)
6465 t->vn_next = 0;
6466 else
6467 t->vn_next = (sizeof (Elf_External_Verneed)
6468 + caux * sizeof (Elf_External_Vernaux));
6469
6470 _bfd_elf_swap_verneed_out (output_bfd, t,
6471 (Elf_External_Verneed *) p);
6472 p += sizeof (Elf_External_Verneed);
6473
6474 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6475 {
6476 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6477 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6478 a->vna_nodename, FALSE);
6479 if (indx == (bfd_size_type) -1)
6480 return FALSE;
6481 a->vna_name = indx;
6482 if (a->vna_nextptr == NULL)
6483 a->vna_next = 0;
6484 else
6485 a->vna_next = sizeof (Elf_External_Vernaux);
6486
6487 _bfd_elf_swap_vernaux_out (output_bfd, a,
6488 (Elf_External_Vernaux *) p);
6489 p += sizeof (Elf_External_Vernaux);
6490 }
6491 }
6492
6493 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6494 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6495 return FALSE;
6496
6497 elf_tdata (output_bfd)->cverrefs = crefs;
6498 }
6499 }
6500
8423293d
AM
6501 if ((elf_tdata (output_bfd)->cverrefs == 0
6502 && elf_tdata (output_bfd)->cverdefs == 0)
6503 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6504 &section_sym_count) == 0)
6505 {
3d4d4302 6506 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
6507 s->flags |= SEC_EXCLUDE;
6508 }
6509 }
6510 return TRUE;
6511}
6512
74541ad4
AM
6513/* Find the first non-excluded output section. We'll use its
6514 section symbol for some emitted relocs. */
6515void
6516_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6517{
6518 asection *s;
6519
6520 for (s = output_bfd->sections; s != NULL; s = s->next)
6521 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6522 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6523 {
6524 elf_hash_table (info)->text_index_section = s;
6525 break;
6526 }
6527}
6528
6529/* Find two non-excluded output sections, one for code, one for data.
6530 We'll use their section symbols for some emitted relocs. */
6531void
6532_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6533{
6534 asection *s;
6535
266b05cf
DJ
6536 /* Data first, since setting text_index_section changes
6537 _bfd_elf_link_omit_section_dynsym. */
74541ad4 6538 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf 6539 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
74541ad4
AM
6540 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6541 {
266b05cf 6542 elf_hash_table (info)->data_index_section = s;
74541ad4
AM
6543 break;
6544 }
6545
6546 for (s = output_bfd->sections; s != NULL; s = s->next)
266b05cf
DJ
6547 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6548 == (SEC_ALLOC | SEC_READONLY))
74541ad4
AM
6549 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6550 {
266b05cf 6551 elf_hash_table (info)->text_index_section = s;
74541ad4
AM
6552 break;
6553 }
6554
6555 if (elf_hash_table (info)->text_index_section == NULL)
6556 elf_hash_table (info)->text_index_section
6557 = elf_hash_table (info)->data_index_section;
6558}
6559
8423293d
AM
6560bfd_boolean
6561bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6562{
74541ad4
AM
6563 const struct elf_backend_data *bed;
6564
8423293d
AM
6565 if (!is_elf_hash_table (info->hash))
6566 return TRUE;
6567
74541ad4
AM
6568 bed = get_elf_backend_data (output_bfd);
6569 (*bed->elf_backend_init_index_section) (output_bfd, info);
6570
8423293d
AM
6571 if (elf_hash_table (info)->dynamic_sections_created)
6572 {
6573 bfd *dynobj;
8423293d
AM
6574 asection *s;
6575 bfd_size_type dynsymcount;
6576 unsigned long section_sym_count;
8423293d
AM
6577 unsigned int dtagcount;
6578
6579 dynobj = elf_hash_table (info)->dynobj;
6580
5a580b3a
AM
6581 /* Assign dynsym indicies. In a shared library we generate a
6582 section symbol for each output section, which come first.
6583 Next come all of the back-end allocated local dynamic syms,
6584 followed by the rest of the global symbols. */
6585
554220db
AM
6586 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6587 &section_sym_count);
5a580b3a
AM
6588
6589 /* Work out the size of the symbol version section. */
3d4d4302 6590 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 6591 BFD_ASSERT (s != NULL);
8423293d
AM
6592 if (dynsymcount != 0
6593 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6594 {
eea6121a 6595 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 6596 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6597 if (s->contents == NULL)
6598 return FALSE;
6599
6600 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6601 return FALSE;
6602 }
6603
6604 /* Set the size of the .dynsym and .hash sections. We counted
6605 the number of dynamic symbols in elf_link_add_object_symbols.
6606 We will build the contents of .dynsym and .hash when we build
6607 the final symbol table, because until then we do not know the
6608 correct value to give the symbols. We built the .dynstr
6609 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 6610 s = elf_hash_table (info)->dynsym;
5a580b3a 6611 BFD_ASSERT (s != NULL);
eea6121a 6612 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6613
6614 if (dynsymcount != 0)
6615 {
a50b1753 6616 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
554220db
AM
6617 if (s->contents == NULL)
6618 return FALSE;
5a580b3a 6619
554220db
AM
6620 /* The first entry in .dynsym is a dummy symbol.
6621 Clear all the section syms, in case we don't output them all. */
6622 ++section_sym_count;
6623 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6624 }
6625
fdc90cb4
JJ
6626 elf_hash_table (info)->bucketcount = 0;
6627
5a580b3a
AM
6628 /* Compute the size of the hashing table. As a side effect this
6629 computes the hash values for all the names we export. */
fdc90cb4
JJ
6630 if (info->emit_hash)
6631 {
6632 unsigned long int *hashcodes;
14b1c01e 6633 struct hash_codes_info hashinf;
fdc90cb4
JJ
6634 bfd_size_type amt;
6635 unsigned long int nsyms;
6636 size_t bucketcount;
6637 size_t hash_entry_size;
6638
6639 /* Compute the hash values for all exported symbols. At the same
6640 time store the values in an array so that we could use them for
6641 optimizations. */
6642 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 6643 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
6644 if (hashcodes == NULL)
6645 return FALSE;
14b1c01e
AM
6646 hashinf.hashcodes = hashcodes;
6647 hashinf.error = FALSE;
5a580b3a 6648
fdc90cb4
JJ
6649 /* Put all hash values in HASHCODES. */
6650 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6651 elf_collect_hash_codes, &hashinf);
6652 if (hashinf.error)
4dd07732
AM
6653 {
6654 free (hashcodes);
6655 return FALSE;
6656 }
5a580b3a 6657
14b1c01e 6658 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6659 bucketcount
6660 = compute_bucket_count (info, hashcodes, nsyms, 0);
6661 free (hashcodes);
6662
6663 if (bucketcount == 0)
6664 return FALSE;
5a580b3a 6665
fdc90cb4
JJ
6666 elf_hash_table (info)->bucketcount = bucketcount;
6667
3d4d4302 6668 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
6669 BFD_ASSERT (s != NULL);
6670 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6671 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 6672 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6673 if (s->contents == NULL)
6674 return FALSE;
6675
6676 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6677 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6678 s->contents + hash_entry_size);
6679 }
6680
6681 if (info->emit_gnu_hash)
6682 {
6683 size_t i, cnt;
6684 unsigned char *contents;
6685 struct collect_gnu_hash_codes cinfo;
6686 bfd_size_type amt;
6687 size_t bucketcount;
6688
6689 memset (&cinfo, 0, sizeof (cinfo));
6690
6691 /* Compute the hash values for all exported symbols. At the same
6692 time store the values in an array so that we could use them for
6693 optimizations. */
6694 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 6695 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
6696 if (cinfo.hashcodes == NULL)
6697 return FALSE;
6698
6699 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6700 cinfo.min_dynindx = -1;
6701 cinfo.output_bfd = output_bfd;
6702 cinfo.bed = bed;
6703
6704 /* Put all hash values in HASHCODES. */
6705 elf_link_hash_traverse (elf_hash_table (info),
6706 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 6707 if (cinfo.error)
4dd07732
AM
6708 {
6709 free (cinfo.hashcodes);
6710 return FALSE;
6711 }
fdc90cb4
JJ
6712
6713 bucketcount
6714 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6715
6716 if (bucketcount == 0)
6717 {
6718 free (cinfo.hashcodes);
6719 return FALSE;
6720 }
6721
3d4d4302 6722 s = bfd_get_linker_section (dynobj, ".gnu.hash");
fdc90cb4
JJ
6723 BFD_ASSERT (s != NULL);
6724
6725 if (cinfo.nsyms == 0)
6726 {
6727 /* Empty .gnu.hash section is special. */
6728 BFD_ASSERT (cinfo.min_dynindx == -1);
6729 free (cinfo.hashcodes);
6730 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 6731 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6732 if (contents == NULL)
6733 return FALSE;
6734 s->contents = contents;
6735 /* 1 empty bucket. */
6736 bfd_put_32 (output_bfd, 1, contents);
6737 /* SYMIDX above the special symbol 0. */
6738 bfd_put_32 (output_bfd, 1, contents + 4);
6739 /* Just one word for bitmask. */
6740 bfd_put_32 (output_bfd, 1, contents + 8);
6741 /* Only hash fn bloom filter. */
6742 bfd_put_32 (output_bfd, 0, contents + 12);
6743 /* No hashes are valid - empty bitmask. */
6744 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6745 /* No hashes in the only bucket. */
6746 bfd_put_32 (output_bfd, 0,
6747 contents + 16 + bed->s->arch_size / 8);
6748 }
6749 else
6750 {
9e6619e2 6751 unsigned long int maskwords, maskbitslog2, x;
0b33793d 6752 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 6753
9e6619e2
AM
6754 x = cinfo.nsyms;
6755 maskbitslog2 = 1;
6756 while ((x >>= 1) != 0)
6757 ++maskbitslog2;
fdc90cb4
JJ
6758 if (maskbitslog2 < 3)
6759 maskbitslog2 = 5;
6760 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6761 maskbitslog2 = maskbitslog2 + 3;
6762 else
6763 maskbitslog2 = maskbitslog2 + 2;
6764 if (bed->s->arch_size == 64)
6765 {
6766 if (maskbitslog2 == 5)
6767 maskbitslog2 = 6;
6768 cinfo.shift1 = 6;
6769 }
6770 else
6771 cinfo.shift1 = 5;
6772 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6773 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6774 cinfo.maskbits = 1 << maskbitslog2;
6775 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6776 amt = bucketcount * sizeof (unsigned long int) * 2;
6777 amt += maskwords * sizeof (bfd_vma);
a50b1753 6778 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
6779 if (cinfo.bitmask == NULL)
6780 {
6781 free (cinfo.hashcodes);
6782 return FALSE;
6783 }
6784
a50b1753 6785 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
6786 cinfo.indx = cinfo.counts + bucketcount;
6787 cinfo.symindx = dynsymcount - cinfo.nsyms;
6788 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6789
6790 /* Determine how often each hash bucket is used. */
6791 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6792 for (i = 0; i < cinfo.nsyms; ++i)
6793 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6794
6795 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6796 if (cinfo.counts[i] != 0)
6797 {
6798 cinfo.indx[i] = cnt;
6799 cnt += cinfo.counts[i];
6800 }
6801 BFD_ASSERT (cnt == dynsymcount);
6802 cinfo.bucketcount = bucketcount;
6803 cinfo.local_indx = cinfo.min_dynindx;
6804
6805 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6806 s->size += cinfo.maskbits / 8;
a50b1753 6807 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
6808 if (contents == NULL)
6809 {
6810 free (cinfo.bitmask);
6811 free (cinfo.hashcodes);
6812 return FALSE;
6813 }
6814
6815 s->contents = contents;
6816 bfd_put_32 (output_bfd, bucketcount, contents);
6817 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6818 bfd_put_32 (output_bfd, maskwords, contents + 8);
6819 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6820 contents += 16 + cinfo.maskbits / 8;
6821
6822 for (i = 0; i < bucketcount; ++i)
6823 {
6824 if (cinfo.counts[i] == 0)
6825 bfd_put_32 (output_bfd, 0, contents);
6826 else
6827 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6828 contents += 4;
6829 }
6830
6831 cinfo.contents = contents;
6832
6833 /* Renumber dynamic symbols, populate .gnu.hash section. */
6834 elf_link_hash_traverse (elf_hash_table (info),
6835 elf_renumber_gnu_hash_syms, &cinfo);
6836
6837 contents = s->contents + 16;
6838 for (i = 0; i < maskwords; ++i)
6839 {
6840 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6841 contents);
6842 contents += bed->s->arch_size / 8;
6843 }
6844
6845 free (cinfo.bitmask);
6846 free (cinfo.hashcodes);
6847 }
6848 }
5a580b3a 6849
3d4d4302 6850 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
6851 BFD_ASSERT (s != NULL);
6852
4ad4eba5 6853 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6854
eea6121a 6855 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6856
6857 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6858 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6859 return FALSE;
6860 }
6861
6862 return TRUE;
6863}
4d269e42 6864\f
4d269e42
AM
6865/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6866
6867static void
6868merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6869 asection *sec)
6870{
dbaa2011
AM
6871 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6872 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
6873}
6874
6875/* Finish SHF_MERGE section merging. */
6876
6877bfd_boolean
630993ec 6878_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
6879{
6880 bfd *ibfd;
6881 asection *sec;
6882
6883 if (!is_elf_hash_table (info->hash))
6884 return FALSE;
6885
c72f2fb2 6886 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
6887 if ((ibfd->flags & DYNAMIC) == 0
6888 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
6889 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6890 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
6891 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6892 if ((sec->flags & SEC_MERGE) != 0
6893 && !bfd_is_abs_section (sec->output_section))
6894 {
6895 struct bfd_elf_section_data *secdata;
6896
6897 secdata = elf_section_data (sec);
630993ec 6898 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
6899 &elf_hash_table (info)->merge_info,
6900 sec, &secdata->sec_info))
6901 return FALSE;
6902 else if (secdata->sec_info)
dbaa2011 6903 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
6904 }
6905
6906 if (elf_hash_table (info)->merge_info != NULL)
630993ec 6907 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
6908 merge_sections_remove_hook);
6909 return TRUE;
6910}
6911
6912/* Create an entry in an ELF linker hash table. */
6913
6914struct bfd_hash_entry *
6915_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6916 struct bfd_hash_table *table,
6917 const char *string)
6918{
6919 /* Allocate the structure if it has not already been allocated by a
6920 subclass. */
6921 if (entry == NULL)
6922 {
a50b1753 6923 entry = (struct bfd_hash_entry *)
ca4be51c 6924 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
6925 if (entry == NULL)
6926 return entry;
6927 }
6928
6929 /* Call the allocation method of the superclass. */
6930 entry = _bfd_link_hash_newfunc (entry, table, string);
6931 if (entry != NULL)
6932 {
6933 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6934 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6935
6936 /* Set local fields. */
6937 ret->indx = -1;
6938 ret->dynindx = -1;
6939 ret->got = htab->init_got_refcount;
6940 ret->plt = htab->init_plt_refcount;
6941 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6942 - offsetof (struct elf_link_hash_entry, size)));
6943 /* Assume that we have been called by a non-ELF symbol reader.
6944 This flag is then reset by the code which reads an ELF input
6945 file. This ensures that a symbol created by a non-ELF symbol
6946 reader will have the flag set correctly. */
6947 ret->non_elf = 1;
6948 }
6949
6950 return entry;
6951}
6952
6953/* Copy data from an indirect symbol to its direct symbol, hiding the
6954 old indirect symbol. Also used for copying flags to a weakdef. */
6955
6956void
6957_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6958 struct elf_link_hash_entry *dir,
6959 struct elf_link_hash_entry *ind)
6960{
6961 struct elf_link_hash_table *htab;
6962
6963 /* Copy down any references that we may have already seen to the
6e33951e
L
6964 symbol which just became indirect if DIR isn't a hidden versioned
6965 symbol. */
4d269e42 6966
422f1182 6967 if (dir->versioned != versioned_hidden)
6e33951e
L
6968 {
6969 dir->ref_dynamic |= ind->ref_dynamic;
6970 dir->ref_regular |= ind->ref_regular;
6971 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6972 dir->non_got_ref |= ind->non_got_ref;
6973 dir->needs_plt |= ind->needs_plt;
6974 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6975 }
4d269e42
AM
6976
6977 if (ind->root.type != bfd_link_hash_indirect)
6978 return;
6979
6980 /* Copy over the global and procedure linkage table refcount entries.
6981 These may have been already set up by a check_relocs routine. */
6982 htab = elf_hash_table (info);
6983 if (ind->got.refcount > htab->init_got_refcount.refcount)
6984 {
6985 if (dir->got.refcount < 0)
6986 dir->got.refcount = 0;
6987 dir->got.refcount += ind->got.refcount;
6988 ind->got.refcount = htab->init_got_refcount.refcount;
6989 }
6990
6991 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6992 {
6993 if (dir->plt.refcount < 0)
6994 dir->plt.refcount = 0;
6995 dir->plt.refcount += ind->plt.refcount;
6996 ind->plt.refcount = htab->init_plt_refcount.refcount;
6997 }
6998
6999 if (ind->dynindx != -1)
7000 {
7001 if (dir->dynindx != -1)
7002 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7003 dir->dynindx = ind->dynindx;
7004 dir->dynstr_index = ind->dynstr_index;
7005 ind->dynindx = -1;
7006 ind->dynstr_index = 0;
7007 }
7008}
7009
7010void
7011_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7012 struct elf_link_hash_entry *h,
7013 bfd_boolean force_local)
7014{
3aa14d16
L
7015 /* STT_GNU_IFUNC symbol must go through PLT. */
7016 if (h->type != STT_GNU_IFUNC)
7017 {
7018 h->plt = elf_hash_table (info)->init_plt_offset;
7019 h->needs_plt = 0;
7020 }
4d269e42
AM
7021 if (force_local)
7022 {
7023 h->forced_local = 1;
7024 if (h->dynindx != -1)
7025 {
7026 h->dynindx = -1;
7027 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7028 h->dynstr_index);
7029 }
7030 }
7031}
7032
7bf52ea2
AM
7033/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7034 caller. */
4d269e42
AM
7035
7036bfd_boolean
7037_bfd_elf_link_hash_table_init
7038 (struct elf_link_hash_table *table,
7039 bfd *abfd,
7040 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7041 struct bfd_hash_table *,
7042 const char *),
4dfe6ac6
NC
7043 unsigned int entsize,
7044 enum elf_target_id target_id)
4d269e42
AM
7045{
7046 bfd_boolean ret;
7047 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7048
4d269e42
AM
7049 table->init_got_refcount.refcount = can_refcount - 1;
7050 table->init_plt_refcount.refcount = can_refcount - 1;
7051 table->init_got_offset.offset = -(bfd_vma) 1;
7052 table->init_plt_offset.offset = -(bfd_vma) 1;
7053 /* The first dynamic symbol is a dummy. */
7054 table->dynsymcount = 1;
7055
7056 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7057
4d269e42 7058 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7059 table->hash_table_id = target_id;
4d269e42
AM
7060
7061 return ret;
7062}
7063
7064/* Create an ELF linker hash table. */
7065
7066struct bfd_link_hash_table *
7067_bfd_elf_link_hash_table_create (bfd *abfd)
7068{
7069 struct elf_link_hash_table *ret;
7070 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7071
7bf52ea2 7072 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7073 if (ret == NULL)
7074 return NULL;
7075
7076 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7077 sizeof (struct elf_link_hash_entry),
7078 GENERIC_ELF_DATA))
4d269e42
AM
7079 {
7080 free (ret);
7081 return NULL;
7082 }
d495ab0d 7083 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7084
7085 return &ret->root;
7086}
7087
9f7c3e5e
AM
7088/* Destroy an ELF linker hash table. */
7089
7090void
d495ab0d 7091_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7092{
d495ab0d
AM
7093 struct elf_link_hash_table *htab;
7094
7095 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7096 if (htab->dynstr != NULL)
7097 _bfd_elf_strtab_free (htab->dynstr);
7098 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7099 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7100}
7101
4d269e42
AM
7102/* This is a hook for the ELF emulation code in the generic linker to
7103 tell the backend linker what file name to use for the DT_NEEDED
7104 entry for a dynamic object. */
7105
7106void
7107bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7108{
7109 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7110 && bfd_get_format (abfd) == bfd_object)
7111 elf_dt_name (abfd) = name;
7112}
7113
7114int
7115bfd_elf_get_dyn_lib_class (bfd *abfd)
7116{
7117 int lib_class;
7118 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7119 && bfd_get_format (abfd) == bfd_object)
7120 lib_class = elf_dyn_lib_class (abfd);
7121 else
7122 lib_class = 0;
7123 return lib_class;
7124}
7125
7126void
7127bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7128{
7129 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7130 && bfd_get_format (abfd) == bfd_object)
7131 elf_dyn_lib_class (abfd) = lib_class;
7132}
7133
7134/* Get the list of DT_NEEDED entries for a link. This is a hook for
7135 the linker ELF emulation code. */
7136
7137struct bfd_link_needed_list *
7138bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7139 struct bfd_link_info *info)
7140{
7141 if (! is_elf_hash_table (info->hash))
7142 return NULL;
7143 return elf_hash_table (info)->needed;
7144}
7145
7146/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7147 hook for the linker ELF emulation code. */
7148
7149struct bfd_link_needed_list *
7150bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7151 struct bfd_link_info *info)
7152{
7153 if (! is_elf_hash_table (info->hash))
7154 return NULL;
7155 return elf_hash_table (info)->runpath;
7156}
7157
7158/* Get the name actually used for a dynamic object for a link. This
7159 is the SONAME entry if there is one. Otherwise, it is the string
7160 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7161
7162const char *
7163bfd_elf_get_dt_soname (bfd *abfd)
7164{
7165 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7166 && bfd_get_format (abfd) == bfd_object)
7167 return elf_dt_name (abfd);
7168 return NULL;
7169}
7170
7171/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7172 the ELF linker emulation code. */
7173
7174bfd_boolean
7175bfd_elf_get_bfd_needed_list (bfd *abfd,
7176 struct bfd_link_needed_list **pneeded)
7177{
7178 asection *s;
7179 bfd_byte *dynbuf = NULL;
cb33740c 7180 unsigned int elfsec;
4d269e42
AM
7181 unsigned long shlink;
7182 bfd_byte *extdyn, *extdynend;
7183 size_t extdynsize;
7184 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7185
7186 *pneeded = NULL;
7187
7188 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7189 || bfd_get_format (abfd) != bfd_object)
7190 return TRUE;
7191
7192 s = bfd_get_section_by_name (abfd, ".dynamic");
7193 if (s == NULL || s->size == 0)
7194 return TRUE;
7195
7196 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7197 goto error_return;
7198
7199 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7200 if (elfsec == SHN_BAD)
4d269e42
AM
7201 goto error_return;
7202
7203 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7204
4d269e42
AM
7205 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7206 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7207
7208 extdyn = dynbuf;
7209 extdynend = extdyn + s->size;
7210 for (; extdyn < extdynend; extdyn += extdynsize)
7211 {
7212 Elf_Internal_Dyn dyn;
7213
7214 (*swap_dyn_in) (abfd, extdyn, &dyn);
7215
7216 if (dyn.d_tag == DT_NULL)
7217 break;
7218
7219 if (dyn.d_tag == DT_NEEDED)
7220 {
7221 const char *string;
7222 struct bfd_link_needed_list *l;
7223 unsigned int tagv = dyn.d_un.d_val;
7224 bfd_size_type amt;
7225
7226 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7227 if (string == NULL)
7228 goto error_return;
7229
7230 amt = sizeof *l;
a50b1753 7231 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
7232 if (l == NULL)
7233 goto error_return;
7234
7235 l->by = abfd;
7236 l->name = string;
7237 l->next = *pneeded;
7238 *pneeded = l;
7239 }
7240 }
7241
7242 free (dynbuf);
7243
7244 return TRUE;
7245
7246 error_return:
7247 if (dynbuf != NULL)
7248 free (dynbuf);
7249 return FALSE;
7250}
7251
7252struct elf_symbuf_symbol
7253{
7254 unsigned long st_name; /* Symbol name, index in string tbl */
7255 unsigned char st_info; /* Type and binding attributes */
7256 unsigned char st_other; /* Visibilty, and target specific */
7257};
7258
7259struct elf_symbuf_head
7260{
7261 struct elf_symbuf_symbol *ssym;
7262 bfd_size_type count;
7263 unsigned int st_shndx;
7264};
7265
7266struct elf_symbol
7267{
7268 union
7269 {
7270 Elf_Internal_Sym *isym;
7271 struct elf_symbuf_symbol *ssym;
7272 } u;
7273 const char *name;
7274};
7275
7276/* Sort references to symbols by ascending section number. */
7277
7278static int
7279elf_sort_elf_symbol (const void *arg1, const void *arg2)
7280{
7281 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7282 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7283
7284 return s1->st_shndx - s2->st_shndx;
7285}
7286
7287static int
7288elf_sym_name_compare (const void *arg1, const void *arg2)
7289{
7290 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7291 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7292 return strcmp (s1->name, s2->name);
7293}
7294
7295static struct elf_symbuf_head *
7296elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7297{
14b1c01e 7298 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
7299 struct elf_symbuf_symbol *ssym;
7300 struct elf_symbuf_head *ssymbuf, *ssymhead;
3ae181ee 7301 bfd_size_type i, shndx_count, total_size;
4d269e42 7302
a50b1753 7303 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
7304 if (indbuf == NULL)
7305 return NULL;
7306
7307 for (ind = indbuf, i = 0; i < symcount; i++)
7308 if (isymbuf[i].st_shndx != SHN_UNDEF)
7309 *ind++ = &isymbuf[i];
7310 indbufend = ind;
7311
7312 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7313 elf_sort_elf_symbol);
7314
7315 shndx_count = 0;
7316 if (indbufend > indbuf)
7317 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7318 if (ind[0]->st_shndx != ind[1]->st_shndx)
7319 shndx_count++;
7320
3ae181ee
L
7321 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7322 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 7323 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
7324 if (ssymbuf == NULL)
7325 {
7326 free (indbuf);
7327 return NULL;
7328 }
7329
3ae181ee 7330 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
7331 ssymbuf->ssym = NULL;
7332 ssymbuf->count = shndx_count;
7333 ssymbuf->st_shndx = 0;
7334 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7335 {
7336 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7337 {
7338 ssymhead++;
7339 ssymhead->ssym = ssym;
7340 ssymhead->count = 0;
7341 ssymhead->st_shndx = (*ind)->st_shndx;
7342 }
7343 ssym->st_name = (*ind)->st_name;
7344 ssym->st_info = (*ind)->st_info;
7345 ssym->st_other = (*ind)->st_other;
7346 ssymhead->count++;
7347 }
3ae181ee
L
7348 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7349 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7350 == total_size));
4d269e42
AM
7351
7352 free (indbuf);
7353 return ssymbuf;
7354}
7355
7356/* Check if 2 sections define the same set of local and global
7357 symbols. */
7358
8f317e31 7359static bfd_boolean
4d269e42
AM
7360bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7361 struct bfd_link_info *info)
7362{
7363 bfd *bfd1, *bfd2;
7364 const struct elf_backend_data *bed1, *bed2;
7365 Elf_Internal_Shdr *hdr1, *hdr2;
7366 bfd_size_type symcount1, symcount2;
7367 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7368 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7369 Elf_Internal_Sym *isym, *isymend;
7370 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7371 bfd_size_type count1, count2, i;
cb33740c 7372 unsigned int shndx1, shndx2;
4d269e42
AM
7373 bfd_boolean result;
7374
7375 bfd1 = sec1->owner;
7376 bfd2 = sec2->owner;
7377
4d269e42
AM
7378 /* Both sections have to be in ELF. */
7379 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7380 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7381 return FALSE;
7382
7383 if (elf_section_type (sec1) != elf_section_type (sec2))
7384 return FALSE;
7385
4d269e42
AM
7386 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7387 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 7388 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
7389 return FALSE;
7390
7391 bed1 = get_elf_backend_data (bfd1);
7392 bed2 = get_elf_backend_data (bfd2);
7393 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7394 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7395 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7396 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7397
7398 if (symcount1 == 0 || symcount2 == 0)
7399 return FALSE;
7400
7401 result = FALSE;
7402 isymbuf1 = NULL;
7403 isymbuf2 = NULL;
a50b1753
NC
7404 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7405 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
7406
7407 if (ssymbuf1 == NULL)
7408 {
7409 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7410 NULL, NULL, NULL);
7411 if (isymbuf1 == NULL)
7412 goto done;
7413
7414 if (!info->reduce_memory_overheads)
7415 elf_tdata (bfd1)->symbuf = ssymbuf1
7416 = elf_create_symbuf (symcount1, isymbuf1);
7417 }
7418
7419 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7420 {
7421 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7422 NULL, NULL, NULL);
7423 if (isymbuf2 == NULL)
7424 goto done;
7425
7426 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7427 elf_tdata (bfd2)->symbuf = ssymbuf2
7428 = elf_create_symbuf (symcount2, isymbuf2);
7429 }
7430
7431 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7432 {
7433 /* Optimized faster version. */
7434 bfd_size_type lo, hi, mid;
7435 struct elf_symbol *symp;
7436 struct elf_symbuf_symbol *ssym, *ssymend;
7437
7438 lo = 0;
7439 hi = ssymbuf1->count;
7440 ssymbuf1++;
7441 count1 = 0;
7442 while (lo < hi)
7443 {
7444 mid = (lo + hi) / 2;
cb33740c 7445 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 7446 hi = mid;
cb33740c 7447 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
7448 lo = mid + 1;
7449 else
7450 {
7451 count1 = ssymbuf1[mid].count;
7452 ssymbuf1 += mid;
7453 break;
7454 }
7455 }
7456
7457 lo = 0;
7458 hi = ssymbuf2->count;
7459 ssymbuf2++;
7460 count2 = 0;
7461 while (lo < hi)
7462 {
7463 mid = (lo + hi) / 2;
cb33740c 7464 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 7465 hi = mid;
cb33740c 7466 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
7467 lo = mid + 1;
7468 else
7469 {
7470 count2 = ssymbuf2[mid].count;
7471 ssymbuf2 += mid;
7472 break;
7473 }
7474 }
7475
7476 if (count1 == 0 || count2 == 0 || count1 != count2)
7477 goto done;
7478
ca4be51c
AM
7479 symtable1
7480 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7481 symtable2
7482 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
7483 if (symtable1 == NULL || symtable2 == NULL)
7484 goto done;
7485
7486 symp = symtable1;
7487 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7488 ssym < ssymend; ssym++, symp++)
7489 {
7490 symp->u.ssym = ssym;
7491 symp->name = bfd_elf_string_from_elf_section (bfd1,
7492 hdr1->sh_link,
7493 ssym->st_name);
7494 }
7495
7496 symp = symtable2;
7497 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7498 ssym < ssymend; ssym++, symp++)
7499 {
7500 symp->u.ssym = ssym;
7501 symp->name = bfd_elf_string_from_elf_section (bfd2,
7502 hdr2->sh_link,
7503 ssym->st_name);
7504 }
7505
7506 /* Sort symbol by name. */
7507 qsort (symtable1, count1, sizeof (struct elf_symbol),
7508 elf_sym_name_compare);
7509 qsort (symtable2, count1, sizeof (struct elf_symbol),
7510 elf_sym_name_compare);
7511
7512 for (i = 0; i < count1; i++)
7513 /* Two symbols must have the same binding, type and name. */
7514 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7515 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7516 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7517 goto done;
7518
7519 result = TRUE;
7520 goto done;
7521 }
7522
a50b1753
NC
7523 symtable1 = (struct elf_symbol *)
7524 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7525 symtable2 = (struct elf_symbol *)
7526 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
7527 if (symtable1 == NULL || symtable2 == NULL)
7528 goto done;
7529
7530 /* Count definitions in the section. */
7531 count1 = 0;
7532 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 7533 if (isym->st_shndx == shndx1)
4d269e42
AM
7534 symtable1[count1++].u.isym = isym;
7535
7536 count2 = 0;
7537 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 7538 if (isym->st_shndx == shndx2)
4d269e42
AM
7539 symtable2[count2++].u.isym = isym;
7540
7541 if (count1 == 0 || count2 == 0 || count1 != count2)
7542 goto done;
7543
7544 for (i = 0; i < count1; i++)
7545 symtable1[i].name
7546 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7547 symtable1[i].u.isym->st_name);
7548
7549 for (i = 0; i < count2; i++)
7550 symtable2[i].name
7551 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7552 symtable2[i].u.isym->st_name);
7553
7554 /* Sort symbol by name. */
7555 qsort (symtable1, count1, sizeof (struct elf_symbol),
7556 elf_sym_name_compare);
7557 qsort (symtable2, count1, sizeof (struct elf_symbol),
7558 elf_sym_name_compare);
7559
7560 for (i = 0; i < count1; i++)
7561 /* Two symbols must have the same binding, type and name. */
7562 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7563 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7564 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7565 goto done;
7566
7567 result = TRUE;
7568
7569done:
7570 if (symtable1)
7571 free (symtable1);
7572 if (symtable2)
7573 free (symtable2);
7574 if (isymbuf1)
7575 free (isymbuf1);
7576 if (isymbuf2)
7577 free (isymbuf2);
7578
7579 return result;
7580}
7581
7582/* Return TRUE if 2 section types are compatible. */
7583
7584bfd_boolean
7585_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7586 bfd *bbfd, const asection *bsec)
7587{
7588 if (asec == NULL
7589 || bsec == NULL
7590 || abfd->xvec->flavour != bfd_target_elf_flavour
7591 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7592 return TRUE;
7593
7594 return elf_section_type (asec) == elf_section_type (bsec);
7595}
7596\f
c152c796
AM
7597/* Final phase of ELF linker. */
7598
7599/* A structure we use to avoid passing large numbers of arguments. */
7600
7601struct elf_final_link_info
7602{
7603 /* General link information. */
7604 struct bfd_link_info *info;
7605 /* Output BFD. */
7606 bfd *output_bfd;
7607 /* Symbol string table. */
ef10c3ac 7608 struct elf_strtab_hash *symstrtab;
c152c796
AM
7609 /* .hash section. */
7610 asection *hash_sec;
7611 /* symbol version section (.gnu.version). */
7612 asection *symver_sec;
7613 /* Buffer large enough to hold contents of any section. */
7614 bfd_byte *contents;
7615 /* Buffer large enough to hold external relocs of any section. */
7616 void *external_relocs;
7617 /* Buffer large enough to hold internal relocs of any section. */
7618 Elf_Internal_Rela *internal_relocs;
7619 /* Buffer large enough to hold external local symbols of any input
7620 BFD. */
7621 bfd_byte *external_syms;
7622 /* And a buffer for symbol section indices. */
7623 Elf_External_Sym_Shndx *locsym_shndx;
7624 /* Buffer large enough to hold internal local symbols of any input
7625 BFD. */
7626 Elf_Internal_Sym *internal_syms;
7627 /* Array large enough to hold a symbol index for each local symbol
7628 of any input BFD. */
7629 long *indices;
7630 /* Array large enough to hold a section pointer for each local
7631 symbol of any input BFD. */
7632 asection **sections;
ef10c3ac 7633 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 7634 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
7635 /* Number of STT_FILE syms seen. */
7636 size_t filesym_count;
c152c796
AM
7637};
7638
7639/* This struct is used to pass information to elf_link_output_extsym. */
7640
7641struct elf_outext_info
7642{
7643 bfd_boolean failed;
7644 bfd_boolean localsyms;
34a79995 7645 bfd_boolean file_sym_done;
8b127cbc 7646 struct elf_final_link_info *flinfo;
c152c796
AM
7647};
7648
d9352518
DB
7649
7650/* Support for evaluating a complex relocation.
7651
7652 Complex relocations are generalized, self-describing relocations. The
7653 implementation of them consists of two parts: complex symbols, and the
a0c8462f 7654 relocations themselves.
d9352518
DB
7655
7656 The relocations are use a reserved elf-wide relocation type code (R_RELC
7657 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7658 information (start bit, end bit, word width, etc) into the addend. This
7659 information is extracted from CGEN-generated operand tables within gas.
7660
7661 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7662 internal) representing prefix-notation expressions, including but not
7663 limited to those sorts of expressions normally encoded as addends in the
7664 addend field. The symbol mangling format is:
7665
7666 <node> := <literal>
7667 | <unary-operator> ':' <node>
7668 | <binary-operator> ':' <node> ':' <node>
7669 ;
7670
7671 <literal> := 's' <digits=N> ':' <N character symbol name>
7672 | 'S' <digits=N> ':' <N character section name>
7673 | '#' <hexdigits>
7674 ;
7675
7676 <binary-operator> := as in C
7677 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7678
7679static void
a0c8462f
AM
7680set_symbol_value (bfd *bfd_with_globals,
7681 Elf_Internal_Sym *isymbuf,
7682 size_t locsymcount,
7683 size_t symidx,
7684 bfd_vma val)
d9352518 7685{
8977835c
AM
7686 struct elf_link_hash_entry **sym_hashes;
7687 struct elf_link_hash_entry *h;
7688 size_t extsymoff = locsymcount;
d9352518 7689
8977835c 7690 if (symidx < locsymcount)
d9352518 7691 {
8977835c
AM
7692 Elf_Internal_Sym *sym;
7693
7694 sym = isymbuf + symidx;
7695 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7696 {
7697 /* It is a local symbol: move it to the
7698 "absolute" section and give it a value. */
7699 sym->st_shndx = SHN_ABS;
7700 sym->st_value = val;
7701 return;
7702 }
7703 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7704 extsymoff = 0;
d9352518 7705 }
8977835c
AM
7706
7707 /* It is a global symbol: set its link type
7708 to "defined" and give it a value. */
7709
7710 sym_hashes = elf_sym_hashes (bfd_with_globals);
7711 h = sym_hashes [symidx - extsymoff];
7712 while (h->root.type == bfd_link_hash_indirect
7713 || h->root.type == bfd_link_hash_warning)
7714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7715 h->root.type = bfd_link_hash_defined;
7716 h->root.u.def.value = val;
7717 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
7718}
7719
a0c8462f
AM
7720static bfd_boolean
7721resolve_symbol (const char *name,
7722 bfd *input_bfd,
8b127cbc 7723 struct elf_final_link_info *flinfo,
a0c8462f
AM
7724 bfd_vma *result,
7725 Elf_Internal_Sym *isymbuf,
7726 size_t locsymcount)
d9352518 7727{
a0c8462f
AM
7728 Elf_Internal_Sym *sym;
7729 struct bfd_link_hash_entry *global_entry;
7730 const char *candidate = NULL;
7731 Elf_Internal_Shdr *symtab_hdr;
7732 size_t i;
7733
d9352518
DB
7734 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7735
7736 for (i = 0; i < locsymcount; ++ i)
7737 {
8977835c 7738 sym = isymbuf + i;
d9352518
DB
7739
7740 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7741 continue;
7742
7743 candidate = bfd_elf_string_from_elf_section (input_bfd,
7744 symtab_hdr->sh_link,
7745 sym->st_name);
7746#ifdef DEBUG
0f02bbd9
AM
7747 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7748 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
7749#endif
7750 if (candidate && strcmp (candidate, name) == 0)
7751 {
8b127cbc 7752 asection *sec = flinfo->sections [i];
d9352518 7753
0f02bbd9
AM
7754 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7755 *result += sec->output_offset + sec->output_section->vma;
d9352518 7756#ifdef DEBUG
0f02bbd9
AM
7757 printf ("Found symbol with value %8.8lx\n",
7758 (unsigned long) *result);
d9352518
DB
7759#endif
7760 return TRUE;
7761 }
7762 }
7763
7764 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 7765 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 7766 FALSE, FALSE, TRUE);
d9352518
DB
7767 if (!global_entry)
7768 return FALSE;
a0c8462f 7769
d9352518
DB
7770 if (global_entry->type == bfd_link_hash_defined
7771 || global_entry->type == bfd_link_hash_defweak)
7772 {
a0c8462f
AM
7773 *result = (global_entry->u.def.value
7774 + global_entry->u.def.section->output_section->vma
7775 + global_entry->u.def.section->output_offset);
d9352518 7776#ifdef DEBUG
0f02bbd9
AM
7777 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7778 global_entry->root.string, (unsigned long) *result);
d9352518
DB
7779#endif
7780 return TRUE;
a0c8462f 7781 }
d9352518 7782
d9352518
DB
7783 return FALSE;
7784}
7785
37b01f6a
DG
7786/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7787 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7788 names like "foo.end" which is the end address of section "foo". */
7789
d9352518 7790static bfd_boolean
a0c8462f
AM
7791resolve_section (const char *name,
7792 asection *sections,
37b01f6a
DG
7793 bfd_vma *result,
7794 bfd * abfd)
d9352518 7795{
a0c8462f
AM
7796 asection *curr;
7797 unsigned int len;
d9352518 7798
a0c8462f 7799 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7800 if (strcmp (curr->name, name) == 0)
7801 {
7802 *result = curr->vma;
7803 return TRUE;
7804 }
7805
7806 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 7807 /* FIXME: This could be coded more efficiently... */
a0c8462f 7808 for (curr = sections; curr; curr = curr->next)
d9352518
DB
7809 {
7810 len = strlen (curr->name);
a0c8462f 7811 if (len > strlen (name))
d9352518
DB
7812 continue;
7813
7814 if (strncmp (curr->name, name, len) == 0)
7815 {
7816 if (strncmp (".end", name + len, 4) == 0)
7817 {
37b01f6a 7818 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
d9352518
DB
7819 return TRUE;
7820 }
7821
7822 /* Insert more pseudo-section names here, if you like. */
7823 }
7824 }
a0c8462f 7825
d9352518
DB
7826 return FALSE;
7827}
7828
7829static void
a0c8462f 7830undefined_reference (const char *reftype, const char *name)
d9352518 7831{
a0c8462f
AM
7832 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7833 reftype, name);
d9352518
DB
7834}
7835
7836static bfd_boolean
a0c8462f
AM
7837eval_symbol (bfd_vma *result,
7838 const char **symp,
7839 bfd *input_bfd,
8b127cbc 7840 struct elf_final_link_info *flinfo,
a0c8462f
AM
7841 bfd_vma dot,
7842 Elf_Internal_Sym *isymbuf,
7843 size_t locsymcount,
7844 int signed_p)
d9352518 7845{
4b93929b
NC
7846 size_t len;
7847 size_t symlen;
a0c8462f
AM
7848 bfd_vma a;
7849 bfd_vma b;
4b93929b 7850 char symbuf[4096];
0f02bbd9 7851 const char *sym = *symp;
a0c8462f
AM
7852 const char *symend;
7853 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
7854
7855 len = strlen (sym);
7856 symend = sym + len;
7857
4b93929b 7858 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
7859 {
7860 bfd_set_error (bfd_error_invalid_operation);
7861 return FALSE;
7862 }
a0c8462f 7863
d9352518
DB
7864 switch (* sym)
7865 {
7866 case '.':
0f02bbd9
AM
7867 *result = dot;
7868 *symp = sym + 1;
d9352518
DB
7869 return TRUE;
7870
7871 case '#':
0f02bbd9
AM
7872 ++sym;
7873 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
7874 return TRUE;
7875
7876 case 'S':
7877 symbol_is_section = TRUE;
a0c8462f 7878 case 's':
0f02bbd9
AM
7879 ++sym;
7880 symlen = strtol (sym, (char **) symp, 10);
7881 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 7882
4b93929b 7883 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
7884 {
7885 bfd_set_error (bfd_error_invalid_operation);
7886 return FALSE;
7887 }
7888
7889 memcpy (symbuf, sym, symlen);
a0c8462f 7890 symbuf[symlen] = '\0';
0f02bbd9 7891 *symp = sym + symlen;
a0c8462f
AM
7892
7893 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
7894 the symbol as a section, or vice-versa. so we're pretty liberal in our
7895 interpretation here; section means "try section first", not "must be a
7896 section", and likewise with symbol. */
7897
a0c8462f 7898 if (symbol_is_section)
d9352518 7899 {
37b01f6a 7900 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 7901 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7902 isymbuf, locsymcount))
d9352518
DB
7903 {
7904 undefined_reference ("section", symbuf);
7905 return FALSE;
7906 }
a0c8462f
AM
7907 }
7908 else
d9352518 7909 {
8b127cbc 7910 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 7911 isymbuf, locsymcount)
8b127cbc 7912 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 7913 result, input_bfd))
d9352518
DB
7914 {
7915 undefined_reference ("symbol", symbuf);
7916 return FALSE;
7917 }
7918 }
7919
7920 return TRUE;
a0c8462f 7921
d9352518
DB
7922 /* All that remains are operators. */
7923
7924#define UNARY_OP(op) \
7925 if (strncmp (sym, #op, strlen (#op)) == 0) \
7926 { \
7927 sym += strlen (#op); \
a0c8462f
AM
7928 if (*sym == ':') \
7929 ++sym; \
0f02bbd9 7930 *symp = sym; \
8b127cbc 7931 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7932 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7933 return FALSE; \
7934 if (signed_p) \
0f02bbd9 7935 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
7936 else \
7937 *result = op a; \
d9352518
DB
7938 return TRUE; \
7939 }
7940
7941#define BINARY_OP(op) \
7942 if (strncmp (sym, #op, strlen (#op)) == 0) \
7943 { \
7944 sym += strlen (#op); \
a0c8462f
AM
7945 if (*sym == ':') \
7946 ++sym; \
0f02bbd9 7947 *symp = sym; \
8b127cbc 7948 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 7949 isymbuf, locsymcount, signed_p)) \
a0c8462f 7950 return FALSE; \
0f02bbd9 7951 ++*symp; \
8b127cbc 7952 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 7953 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
7954 return FALSE; \
7955 if (signed_p) \
0f02bbd9 7956 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
7957 else \
7958 *result = a op b; \
d9352518
DB
7959 return TRUE; \
7960 }
7961
7962 default:
7963 UNARY_OP (0-);
7964 BINARY_OP (<<);
7965 BINARY_OP (>>);
7966 BINARY_OP (==);
7967 BINARY_OP (!=);
7968 BINARY_OP (<=);
7969 BINARY_OP (>=);
7970 BINARY_OP (&&);
7971 BINARY_OP (||);
7972 UNARY_OP (~);
7973 UNARY_OP (!);
7974 BINARY_OP (*);
7975 BINARY_OP (/);
7976 BINARY_OP (%);
7977 BINARY_OP (^);
7978 BINARY_OP (|);
7979 BINARY_OP (&);
7980 BINARY_OP (+);
7981 BINARY_OP (-);
7982 BINARY_OP (<);
7983 BINARY_OP (>);
7984#undef UNARY_OP
7985#undef BINARY_OP
7986 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7987 bfd_set_error (bfd_error_invalid_operation);
7988 return FALSE;
7989 }
7990}
7991
d9352518 7992static void
a0c8462f
AM
7993put_value (bfd_vma size,
7994 unsigned long chunksz,
7995 bfd *input_bfd,
7996 bfd_vma x,
7997 bfd_byte *location)
d9352518
DB
7998{
7999 location += (size - chunksz);
8000
41cd1ad1 8001 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8002 {
8003 switch (chunksz)
8004 {
d9352518
DB
8005 case 1:
8006 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8007 x >>= 8;
d9352518
DB
8008 break;
8009 case 2:
8010 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8011 x >>= 16;
d9352518
DB
8012 break;
8013 case 4:
8014 bfd_put_32 (input_bfd, x, location);
65164438
NC
8015 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8016 x >>= 16;
8017 x >>= 16;
d9352518 8018 break;
d9352518 8019#ifdef BFD64
41cd1ad1 8020 case 8:
d9352518 8021 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8022 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8023 x >>= 32;
8024 x >>= 32;
8025 break;
d9352518 8026#endif
41cd1ad1
NC
8027 default:
8028 abort ();
d9352518
DB
8029 break;
8030 }
8031 }
8032}
8033
a0c8462f
AM
8034static bfd_vma
8035get_value (bfd_vma size,
8036 unsigned long chunksz,
8037 bfd *input_bfd,
8038 bfd_byte *location)
d9352518 8039{
9b239e0e 8040 int shift;
d9352518
DB
8041 bfd_vma x = 0;
8042
9b239e0e
NC
8043 /* Sanity checks. */
8044 BFD_ASSERT (chunksz <= sizeof (x)
8045 && size >= chunksz
8046 && chunksz != 0
8047 && (size % chunksz) == 0
8048 && input_bfd != NULL
8049 && location != NULL);
8050
8051 if (chunksz == sizeof (x))
8052 {
8053 BFD_ASSERT (size == chunksz);
8054
8055 /* Make sure that we do not perform an undefined shift operation.
8056 We know that size == chunksz so there will only be one iteration
8057 of the loop below. */
8058 shift = 0;
8059 }
8060 else
8061 shift = 8 * chunksz;
8062
a0c8462f 8063 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8064 {
8065 switch (chunksz)
8066 {
d9352518 8067 case 1:
9b239e0e 8068 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8069 break;
8070 case 2:
9b239e0e 8071 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8072 break;
8073 case 4:
9b239e0e 8074 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8075 break;
d9352518 8076#ifdef BFD64
9b239e0e
NC
8077 case 8:
8078 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8079 break;
9b239e0e
NC
8080#endif
8081 default:
8082 abort ();
d9352518
DB
8083 }
8084 }
8085 return x;
8086}
8087
a0c8462f
AM
8088static void
8089decode_complex_addend (unsigned long *start, /* in bits */
8090 unsigned long *oplen, /* in bits */
8091 unsigned long *len, /* in bits */
8092 unsigned long *wordsz, /* in bytes */
8093 unsigned long *chunksz, /* in bytes */
8094 unsigned long *lsb0_p,
8095 unsigned long *signed_p,
8096 unsigned long *trunc_p,
8097 unsigned long encoded)
d9352518
DB
8098{
8099 * start = encoded & 0x3F;
8100 * len = (encoded >> 6) & 0x3F;
8101 * oplen = (encoded >> 12) & 0x3F;
8102 * wordsz = (encoded >> 18) & 0xF;
8103 * chunksz = (encoded >> 22) & 0xF;
8104 * lsb0_p = (encoded >> 27) & 1;
8105 * signed_p = (encoded >> 28) & 1;
8106 * trunc_p = (encoded >> 29) & 1;
8107}
8108
cdfeee4f 8109bfd_reloc_status_type
0f02bbd9 8110bfd_elf_perform_complex_relocation (bfd *input_bfd,
cdfeee4f 8111 asection *input_section ATTRIBUTE_UNUSED,
0f02bbd9
AM
8112 bfd_byte *contents,
8113 Elf_Internal_Rela *rel,
8114 bfd_vma relocation)
d9352518 8115{
0f02bbd9
AM
8116 bfd_vma shift, x, mask;
8117 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8118 bfd_reloc_status_type r;
d9352518
DB
8119
8120 /* Perform this reloc, since it is complex.
8121 (this is not to say that it necessarily refers to a complex
8122 symbol; merely that it is a self-describing CGEN based reloc.
8123 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8124 word size, etc) encoded within it.). */
d9352518 8125
a0c8462f
AM
8126 decode_complex_addend (&start, &oplen, &len, &wordsz,
8127 &chunksz, &lsb0_p, &signed_p,
8128 &trunc_p, rel->r_addend);
d9352518
DB
8129
8130 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8131
8132 if (lsb0_p)
8133 shift = (start + 1) - len;
8134 else
8135 shift = (8 * wordsz) - (start + len);
8136
37b01f6a
DG
8137 x = get_value (wordsz, chunksz, input_bfd,
8138 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
d9352518
DB
8139
8140#ifdef DEBUG
8141 printf ("Doing complex reloc: "
8142 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8143 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8144 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8145 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
8146 oplen, (unsigned long) x, (unsigned long) mask,
8147 (unsigned long) relocation);
d9352518
DB
8148#endif
8149
cdfeee4f 8150 r = bfd_reloc_ok;
d9352518 8151 if (! trunc_p)
cdfeee4f
AM
8152 /* Now do an overflow check. */
8153 r = bfd_check_overflow ((signed_p
8154 ? complain_overflow_signed
8155 : complain_overflow_unsigned),
8156 len, 0, (8 * wordsz),
8157 relocation);
a0c8462f 8158
d9352518
DB
8159 /* Do the deed. */
8160 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8161
8162#ifdef DEBUG
8163 printf (" relocation: %8.8lx\n"
8164 " shifted mask: %8.8lx\n"
8165 " shifted/masked reloc: %8.8lx\n"
8166 " result: %8.8lx\n",
9ccb8af9
AM
8167 (unsigned long) relocation, (unsigned long) (mask << shift),
8168 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 8169#endif
37b01f6a
DG
8170 put_value (wordsz, chunksz, input_bfd, x,
8171 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
cdfeee4f 8172 return r;
d9352518
DB
8173}
8174
0e287786
AM
8175/* Functions to read r_offset from external (target order) reloc
8176 entry. Faster than bfd_getl32 et al, because we let the compiler
8177 know the value is aligned. */
53df40a4 8178
0e287786
AM
8179static bfd_vma
8180ext32l_r_offset (const void *p)
53df40a4
AM
8181{
8182 union aligned32
8183 {
8184 uint32_t v;
8185 unsigned char c[4];
8186 };
8187 const union aligned32 *a
0e287786 8188 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8189
8190 uint32_t aval = ( (uint32_t) a->c[0]
8191 | (uint32_t) a->c[1] << 8
8192 | (uint32_t) a->c[2] << 16
8193 | (uint32_t) a->c[3] << 24);
0e287786 8194 return aval;
53df40a4
AM
8195}
8196
0e287786
AM
8197static bfd_vma
8198ext32b_r_offset (const void *p)
53df40a4
AM
8199{
8200 union aligned32
8201 {
8202 uint32_t v;
8203 unsigned char c[4];
8204 };
8205 const union aligned32 *a
0e287786 8206 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
8207
8208 uint32_t aval = ( (uint32_t) a->c[0] << 24
8209 | (uint32_t) a->c[1] << 16
8210 | (uint32_t) a->c[2] << 8
8211 | (uint32_t) a->c[3]);
0e287786 8212 return aval;
53df40a4
AM
8213}
8214
8215#ifdef BFD_HOST_64_BIT
0e287786
AM
8216static bfd_vma
8217ext64l_r_offset (const void *p)
53df40a4
AM
8218{
8219 union aligned64
8220 {
8221 uint64_t v;
8222 unsigned char c[8];
8223 };
8224 const union aligned64 *a
0e287786 8225 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8226
8227 uint64_t aval = ( (uint64_t) a->c[0]
8228 | (uint64_t) a->c[1] << 8
8229 | (uint64_t) a->c[2] << 16
8230 | (uint64_t) a->c[3] << 24
8231 | (uint64_t) a->c[4] << 32
8232 | (uint64_t) a->c[5] << 40
8233 | (uint64_t) a->c[6] << 48
8234 | (uint64_t) a->c[7] << 56);
0e287786 8235 return aval;
53df40a4
AM
8236}
8237
0e287786
AM
8238static bfd_vma
8239ext64b_r_offset (const void *p)
53df40a4
AM
8240{
8241 union aligned64
8242 {
8243 uint64_t v;
8244 unsigned char c[8];
8245 };
8246 const union aligned64 *a
0e287786 8247 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
8248
8249 uint64_t aval = ( (uint64_t) a->c[0] << 56
8250 | (uint64_t) a->c[1] << 48
8251 | (uint64_t) a->c[2] << 40
8252 | (uint64_t) a->c[3] << 32
8253 | (uint64_t) a->c[4] << 24
8254 | (uint64_t) a->c[5] << 16
8255 | (uint64_t) a->c[6] << 8
8256 | (uint64_t) a->c[7]);
0e287786 8257 return aval;
53df40a4
AM
8258}
8259#endif
8260
c152c796
AM
8261/* When performing a relocatable link, the input relocations are
8262 preserved. But, if they reference global symbols, the indices
d4730f92
BS
8263 referenced must be updated. Update all the relocations found in
8264 RELDATA. */
c152c796 8265
bca6d0e3 8266static bfd_boolean
c152c796 8267elf_link_adjust_relocs (bfd *abfd,
28dbcedc
AM
8268 struct bfd_elf_section_reloc_data *reldata,
8269 bfd_boolean sort)
c152c796
AM
8270{
8271 unsigned int i;
8272 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8273 bfd_byte *erela;
8274 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8275 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8276 bfd_vma r_type_mask;
8277 int r_sym_shift;
d4730f92
BS
8278 unsigned int count = reldata->count;
8279 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 8280
d4730f92 8281 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
8282 {
8283 swap_in = bed->s->swap_reloc_in;
8284 swap_out = bed->s->swap_reloc_out;
8285 }
d4730f92 8286 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
8287 {
8288 swap_in = bed->s->swap_reloca_in;
8289 swap_out = bed->s->swap_reloca_out;
8290 }
8291 else
8292 abort ();
8293
8294 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8295 abort ();
8296
8297 if (bed->s->arch_size == 32)
8298 {
8299 r_type_mask = 0xff;
8300 r_sym_shift = 8;
8301 }
8302 else
8303 {
8304 r_type_mask = 0xffffffff;
8305 r_sym_shift = 32;
8306 }
8307
d4730f92
BS
8308 erela = reldata->hdr->contents;
8309 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
8310 {
8311 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8312 unsigned int j;
8313
8314 if (*rel_hash == NULL)
8315 continue;
8316
8317 BFD_ASSERT ((*rel_hash)->indx >= 0);
8318
8319 (*swap_in) (abfd, erela, irela);
8320 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8321 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8322 | (irela[j].r_info & r_type_mask));
8323 (*swap_out) (abfd, irela, erela);
8324 }
53df40a4 8325
0e287786 8326 if (sort && count != 0)
53df40a4 8327 {
0e287786
AM
8328 bfd_vma (*ext_r_off) (const void *);
8329 bfd_vma r_off;
8330 size_t elt_size;
8331 bfd_byte *base, *end, *p, *loc;
bca6d0e3 8332 bfd_byte *buf = NULL;
28dbcedc
AM
8333
8334 if (bed->s->arch_size == 32)
8335 {
8336 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8337 ext_r_off = ext32l_r_offset;
28dbcedc 8338 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8339 ext_r_off = ext32b_r_offset;
28dbcedc
AM
8340 else
8341 abort ();
8342 }
53df40a4 8343 else
28dbcedc 8344 {
53df40a4 8345#ifdef BFD_HOST_64_BIT
28dbcedc 8346 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 8347 ext_r_off = ext64l_r_offset;
28dbcedc 8348 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 8349 ext_r_off = ext64b_r_offset;
28dbcedc 8350 else
53df40a4 8351#endif
28dbcedc
AM
8352 abort ();
8353 }
0e287786 8354
bca6d0e3
AM
8355 /* Must use a stable sort here. A modified insertion sort,
8356 since the relocs are mostly sorted already. */
0e287786
AM
8357 elt_size = reldata->hdr->sh_entsize;
8358 base = reldata->hdr->contents;
8359 end = base + count * elt_size;
bca6d0e3 8360 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
8361 abort ();
8362
8363 /* Ensure the first element is lowest. This acts as a sentinel,
8364 speeding the main loop below. */
8365 r_off = (*ext_r_off) (base);
8366 for (p = loc = base; (p += elt_size) < end; )
8367 {
8368 bfd_vma r_off2 = (*ext_r_off) (p);
8369 if (r_off > r_off2)
8370 {
8371 r_off = r_off2;
8372 loc = p;
8373 }
8374 }
8375 if (loc != base)
8376 {
8377 /* Don't just swap *base and *loc as that changes the order
8378 of the original base[0] and base[1] if they happen to
8379 have the same r_offset. */
bca6d0e3
AM
8380 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8381 memcpy (onebuf, loc, elt_size);
0e287786 8382 memmove (base + elt_size, base, loc - base);
bca6d0e3 8383 memcpy (base, onebuf, elt_size);
0e287786
AM
8384 }
8385
b29b8669 8386 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
8387 {
8388 /* base to p is sorted, *p is next to insert. */
8389 r_off = (*ext_r_off) (p);
8390 /* Search the sorted region for location to insert. */
8391 loc = p - elt_size;
8392 while (r_off < (*ext_r_off) (loc))
8393 loc -= elt_size;
8394 loc += elt_size;
8395 if (loc != p)
8396 {
bca6d0e3
AM
8397 /* Chances are there is a run of relocs to insert here,
8398 from one of more input files. Files are not always
8399 linked in order due to the way elf_link_input_bfd is
8400 called. See pr17666. */
8401 size_t sortlen = p - loc;
8402 bfd_vma r_off2 = (*ext_r_off) (loc);
8403 size_t runlen = elt_size;
8404 size_t buf_size = 96 * 1024;
8405 while (p + runlen < end
8406 && (sortlen <= buf_size
8407 || runlen + elt_size <= buf_size)
8408 && r_off2 > (*ext_r_off) (p + runlen))
8409 runlen += elt_size;
8410 if (buf == NULL)
8411 {
8412 buf = bfd_malloc (buf_size);
8413 if (buf == NULL)
8414 return FALSE;
8415 }
8416 if (runlen < sortlen)
8417 {
8418 memcpy (buf, p, runlen);
8419 memmove (loc + runlen, loc, sortlen);
8420 memcpy (loc, buf, runlen);
8421 }
8422 else
8423 {
8424 memcpy (buf, loc, sortlen);
8425 memmove (loc, p, runlen);
8426 memcpy (loc + runlen, buf, sortlen);
8427 }
b29b8669 8428 p += runlen - elt_size;
0e287786
AM
8429 }
8430 }
8431 /* Hashes are no longer valid. */
28dbcedc
AM
8432 free (reldata->hashes);
8433 reldata->hashes = NULL;
bca6d0e3 8434 free (buf);
53df40a4 8435 }
bca6d0e3 8436 return TRUE;
c152c796
AM
8437}
8438
8439struct elf_link_sort_rela
8440{
8441 union {
8442 bfd_vma offset;
8443 bfd_vma sym_mask;
8444 } u;
8445 enum elf_reloc_type_class type;
8446 /* We use this as an array of size int_rels_per_ext_rel. */
8447 Elf_Internal_Rela rela[1];
8448};
8449
8450static int
8451elf_link_sort_cmp1 (const void *A, const void *B)
8452{
a50b1753
NC
8453 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8454 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
8455 int relativea, relativeb;
8456
8457 relativea = a->type == reloc_class_relative;
8458 relativeb = b->type == reloc_class_relative;
8459
8460 if (relativea < relativeb)
8461 return 1;
8462 if (relativea > relativeb)
8463 return -1;
8464 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8465 return -1;
8466 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8467 return 1;
8468 if (a->rela->r_offset < b->rela->r_offset)
8469 return -1;
8470 if (a->rela->r_offset > b->rela->r_offset)
8471 return 1;
8472 return 0;
8473}
8474
8475static int
8476elf_link_sort_cmp2 (const void *A, const void *B)
8477{
a50b1753
NC
8478 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8479 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 8480
7e612e98 8481 if (a->type < b->type)
c152c796 8482 return -1;
7e612e98 8483 if (a->type > b->type)
c152c796 8484 return 1;
7e612e98 8485 if (a->u.offset < b->u.offset)
c152c796 8486 return -1;
7e612e98 8487 if (a->u.offset > b->u.offset)
c152c796
AM
8488 return 1;
8489 if (a->rela->r_offset < b->rela->r_offset)
8490 return -1;
8491 if (a->rela->r_offset > b->rela->r_offset)
8492 return 1;
8493 return 0;
8494}
8495
8496static size_t
8497elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8498{
3410fea8 8499 asection *dynamic_relocs;
fc66a176
L
8500 asection *rela_dyn;
8501 asection *rel_dyn;
c152c796
AM
8502 bfd_size_type count, size;
8503 size_t i, ret, sort_elt, ext_size;
8504 bfd_byte *sort, *s_non_relative, *p;
8505 struct elf_link_sort_rela *sq;
8506 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8507 int i2e = bed->s->int_rels_per_ext_rel;
8508 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8509 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8510 struct bfd_link_order *lo;
8511 bfd_vma r_sym_mask;
3410fea8 8512 bfd_boolean use_rela;
c152c796 8513
3410fea8
NC
8514 /* Find a dynamic reloc section. */
8515 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8516 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8517 if (rela_dyn != NULL && rela_dyn->size > 0
8518 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8519 {
3410fea8
NC
8520 bfd_boolean use_rela_initialised = FALSE;
8521
8522 /* This is just here to stop gcc from complaining.
8523 It's initialization checking code is not perfect. */
8524 use_rela = TRUE;
8525
8526 /* Both sections are present. Examine the sizes
8527 of the indirect sections to help us choose. */
8528 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8529 if (lo->type == bfd_indirect_link_order)
8530 {
8531 asection *o = lo->u.indirect.section;
8532
8533 if ((o->size % bed->s->sizeof_rela) == 0)
8534 {
8535 if ((o->size % bed->s->sizeof_rel) == 0)
8536 /* Section size is divisible by both rel and rela sizes.
8537 It is of no help to us. */
8538 ;
8539 else
8540 {
8541 /* Section size is only divisible by rela. */
8542 if (use_rela_initialised && (use_rela == FALSE))
8543 {
8544 _bfd_error_handler
8545 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8546 bfd_set_error (bfd_error_invalid_operation);
8547 return 0;
8548 }
8549 else
8550 {
8551 use_rela = TRUE;
8552 use_rela_initialised = TRUE;
8553 }
8554 }
8555 }
8556 else if ((o->size % bed->s->sizeof_rel) == 0)
8557 {
8558 /* Section size is only divisible by rel. */
8559 if (use_rela_initialised && (use_rela == TRUE))
8560 {
8561 _bfd_error_handler
8562 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8563 bfd_set_error (bfd_error_invalid_operation);
8564 return 0;
8565 }
8566 else
8567 {
8568 use_rela = FALSE;
8569 use_rela_initialised = TRUE;
8570 }
8571 }
8572 else
8573 {
8574 /* The section size is not divisible by either - something is wrong. */
8575 _bfd_error_handler
8576 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8577 bfd_set_error (bfd_error_invalid_operation);
8578 return 0;
8579 }
8580 }
8581
8582 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8583 if (lo->type == bfd_indirect_link_order)
8584 {
8585 asection *o = lo->u.indirect.section;
8586
8587 if ((o->size % bed->s->sizeof_rela) == 0)
8588 {
8589 if ((o->size % bed->s->sizeof_rel) == 0)
8590 /* Section size is divisible by both rel and rela sizes.
8591 It is of no help to us. */
8592 ;
8593 else
8594 {
8595 /* Section size is only divisible by rela. */
8596 if (use_rela_initialised && (use_rela == FALSE))
8597 {
8598 _bfd_error_handler
8599 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8600 bfd_set_error (bfd_error_invalid_operation);
8601 return 0;
8602 }
8603 else
8604 {
8605 use_rela = TRUE;
8606 use_rela_initialised = TRUE;
8607 }
8608 }
8609 }
8610 else if ((o->size % bed->s->sizeof_rel) == 0)
8611 {
8612 /* Section size is only divisible by rel. */
8613 if (use_rela_initialised && (use_rela == TRUE))
8614 {
8615 _bfd_error_handler
8616 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8617 bfd_set_error (bfd_error_invalid_operation);
8618 return 0;
8619 }
8620 else
8621 {
8622 use_rela = FALSE;
8623 use_rela_initialised = TRUE;
8624 }
8625 }
8626 else
8627 {
8628 /* The section size is not divisible by either - something is wrong. */
8629 _bfd_error_handler
8630 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8631 bfd_set_error (bfd_error_invalid_operation);
8632 return 0;
8633 }
8634 }
8635
8636 if (! use_rela_initialised)
8637 /* Make a guess. */
8638 use_rela = TRUE;
c152c796 8639 }
fc66a176
L
8640 else if (rela_dyn != NULL && rela_dyn->size > 0)
8641 use_rela = TRUE;
8642 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8643 use_rela = FALSE;
c152c796 8644 else
fc66a176 8645 return 0;
3410fea8
NC
8646
8647 if (use_rela)
c152c796 8648 {
3410fea8 8649 dynamic_relocs = rela_dyn;
c152c796
AM
8650 ext_size = bed->s->sizeof_rela;
8651 swap_in = bed->s->swap_reloca_in;
8652 swap_out = bed->s->swap_reloca_out;
8653 }
3410fea8
NC
8654 else
8655 {
8656 dynamic_relocs = rel_dyn;
8657 ext_size = bed->s->sizeof_rel;
8658 swap_in = bed->s->swap_reloc_in;
8659 swap_out = bed->s->swap_reloc_out;
8660 }
c152c796
AM
8661
8662 size = 0;
3410fea8 8663 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8664 if (lo->type == bfd_indirect_link_order)
3410fea8 8665 size += lo->u.indirect.section->size;
c152c796 8666
3410fea8 8667 if (size != dynamic_relocs->size)
c152c796
AM
8668 return 0;
8669
8670 sort_elt = (sizeof (struct elf_link_sort_rela)
8671 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8672
8673 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
8674 if (count == 0)
8675 return 0;
a50b1753 8676 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 8677
c152c796
AM
8678 if (sort == NULL)
8679 {
8680 (*info->callbacks->warning)
8681 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8682 return 0;
8683 }
8684
8685 if (bed->s->arch_size == 32)
8686 r_sym_mask = ~(bfd_vma) 0xff;
8687 else
8688 r_sym_mask = ~(bfd_vma) 0xffffffff;
8689
3410fea8 8690 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8691 if (lo->type == bfd_indirect_link_order)
8692 {
8693 bfd_byte *erel, *erelend;
8694 asection *o = lo->u.indirect.section;
8695
1da212d6
AM
8696 if (o->contents == NULL && o->size != 0)
8697 {
8698 /* This is a reloc section that is being handled as a normal
8699 section. See bfd_section_from_shdr. We can't combine
8700 relocs in this case. */
8701 free (sort);
8702 return 0;
8703 }
c152c796 8704 erel = o->contents;
eea6121a 8705 erelend = o->contents + o->size;
5dabe785 8706 /* FIXME: octets_per_byte. */
c152c796 8707 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8708
c152c796
AM
8709 while (erel < erelend)
8710 {
8711 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8712
c152c796 8713 (*swap_in) (abfd, erel, s->rela);
7e612e98 8714 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
8715 s->u.sym_mask = r_sym_mask;
8716 p += sort_elt;
8717 erel += ext_size;
8718 }
8719 }
8720
8721 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8722
8723 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8724 {
8725 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8726 if (s->type != reloc_class_relative)
8727 break;
8728 }
8729 ret = i;
8730 s_non_relative = p;
8731
8732 sq = (struct elf_link_sort_rela *) s_non_relative;
8733 for (; i < count; i++, p += sort_elt)
8734 {
8735 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8736 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8737 sq = sp;
8738 sp->u.offset = sq->rela->r_offset;
8739 }
8740
8741 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8742
3410fea8 8743 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8744 if (lo->type == bfd_indirect_link_order)
8745 {
8746 bfd_byte *erel, *erelend;
8747 asection *o = lo->u.indirect.section;
8748
8749 erel = o->contents;
eea6121a 8750 erelend = o->contents + o->size;
5dabe785 8751 /* FIXME: octets_per_byte. */
c152c796
AM
8752 p = sort + o->output_offset / ext_size * sort_elt;
8753 while (erel < erelend)
8754 {
8755 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8756 (*swap_out) (abfd, s->rela, erel);
8757 p += sort_elt;
8758 erel += ext_size;
8759 }
8760 }
8761
8762 free (sort);
3410fea8 8763 *psec = dynamic_relocs;
c152c796
AM
8764 return ret;
8765}
8766
ef10c3ac 8767/* Add a symbol to the output symbol string table. */
c152c796 8768
6e0b88f1 8769static int
ef10c3ac
L
8770elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8771 const char *name,
8772 Elf_Internal_Sym *elfsym,
8773 asection *input_sec,
8774 struct elf_link_hash_entry *h)
c152c796 8775{
6e0b88f1 8776 int (*output_symbol_hook)
c152c796
AM
8777 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8778 struct elf_link_hash_entry *);
ef10c3ac 8779 struct elf_link_hash_table *hash_table;
c152c796 8780 const struct elf_backend_data *bed;
ef10c3ac 8781 bfd_size_type strtabsize;
c152c796 8782
8539e4e8
AM
8783 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8784
8b127cbc 8785 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
8786 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8787 if (output_symbol_hook != NULL)
8788 {
8b127cbc 8789 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
8790 if (ret != 1)
8791 return ret;
c152c796
AM
8792 }
8793
ef10c3ac
L
8794 if (name == NULL
8795 || *name == '\0'
8796 || (input_sec->flags & SEC_EXCLUDE))
8797 elfsym->st_name = (unsigned long) -1;
c152c796
AM
8798 else
8799 {
ef10c3ac
L
8800 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8801 to get the final offset for st_name. */
8802 elfsym->st_name
8803 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8804 name, FALSE);
c152c796 8805 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 8806 return 0;
c152c796
AM
8807 }
8808
ef10c3ac
L
8809 hash_table = elf_hash_table (flinfo->info);
8810 strtabsize = hash_table->strtabsize;
8811 if (strtabsize <= hash_table->strtabcount)
c152c796 8812 {
ef10c3ac
L
8813 strtabsize += strtabsize;
8814 hash_table->strtabsize = strtabsize;
8815 strtabsize *= sizeof (*hash_table->strtab);
8816 hash_table->strtab
8817 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8818 strtabsize);
8819 if (hash_table->strtab == NULL)
6e0b88f1 8820 return 0;
c152c796 8821 }
ef10c3ac
L
8822 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8823 hash_table->strtab[hash_table->strtabcount].dest_index
8824 = hash_table->strtabcount;
8825 hash_table->strtab[hash_table->strtabcount].destshndx_index
8826 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8827
8828 bfd_get_symcount (flinfo->output_bfd) += 1;
8829 hash_table->strtabcount += 1;
8830
8831 return 1;
8832}
8833
8834/* Swap symbols out to the symbol table and flush the output symbols to
8835 the file. */
8836
8837static bfd_boolean
8838elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8839{
8840 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8841 bfd_size_type amt, i;
8842 const struct elf_backend_data *bed;
8843 bfd_byte *symbuf;
8844 Elf_Internal_Shdr *hdr;
8845 file_ptr pos;
8846 bfd_boolean ret;
8847
8848 if (!hash_table->strtabcount)
8849 return TRUE;
8850
8851 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8852
8853 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 8854
ef10c3ac
L
8855 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8856 symbuf = (bfd_byte *) bfd_malloc (amt);
8857 if (symbuf == NULL)
8858 return FALSE;
1b786873 8859
ef10c3ac 8860 if (flinfo->symshndxbuf)
c152c796 8861 {
ef10c3ac
L
8862 amt = (sizeof (Elf_External_Sym_Shndx)
8863 * (bfd_get_symcount (flinfo->output_bfd)));
8864 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8865 if (flinfo->symshndxbuf == NULL)
c152c796 8866 {
ef10c3ac
L
8867 free (symbuf);
8868 return FALSE;
c152c796 8869 }
c152c796
AM
8870 }
8871
ef10c3ac
L
8872 for (i = 0; i < hash_table->strtabcount; i++)
8873 {
8874 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8875 if (elfsym->sym.st_name == (unsigned long) -1)
8876 elfsym->sym.st_name = 0;
8877 else
8878 elfsym->sym.st_name
8879 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8880 elfsym->sym.st_name);
8881 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8882 ((bfd_byte *) symbuf
8883 + (elfsym->dest_index
8884 * bed->s->sizeof_sym)),
8885 (flinfo->symshndxbuf
8886 + elfsym->destshndx_index));
8887 }
8888
8889 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8890 pos = hdr->sh_offset + hdr->sh_size;
8891 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8892 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8893 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8894 {
8895 hdr->sh_size += amt;
8896 ret = TRUE;
8897 }
8898 else
8899 ret = FALSE;
c152c796 8900
ef10c3ac
L
8901 free (symbuf);
8902
8903 free (hash_table->strtab);
8904 hash_table->strtab = NULL;
8905
8906 return ret;
c152c796
AM
8907}
8908
c0d5a53d
L
8909/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8910
8911static bfd_boolean
8912check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8913{
4fbb74a6
AM
8914 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8915 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
8916 {
8917 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 8918 beyond 64k. */
c0d5a53d
L
8919 (*_bfd_error_handler)
8920 (_("%B: Too many sections: %d (>= %d)"),
4fbb74a6 8921 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
8922 bfd_set_error (bfd_error_nonrepresentable_section);
8923 return FALSE;
8924 }
8925 return TRUE;
8926}
8927
c152c796
AM
8928/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8929 allowing an unsatisfied unversioned symbol in the DSO to match a
8930 versioned symbol that would normally require an explicit version.
8931 We also handle the case that a DSO references a hidden symbol
8932 which may be satisfied by a versioned symbol in another DSO. */
8933
8934static bfd_boolean
8935elf_link_check_versioned_symbol (struct bfd_link_info *info,
8936 const struct elf_backend_data *bed,
8937 struct elf_link_hash_entry *h)
8938{
8939 bfd *abfd;
8940 struct elf_link_loaded_list *loaded;
8941
8942 if (!is_elf_hash_table (info->hash))
8943 return FALSE;
8944
90c984fc
L
8945 /* Check indirect symbol. */
8946 while (h->root.type == bfd_link_hash_indirect)
8947 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8948
c152c796
AM
8949 switch (h->root.type)
8950 {
8951 default:
8952 abfd = NULL;
8953 break;
8954
8955 case bfd_link_hash_undefined:
8956 case bfd_link_hash_undefweak:
8957 abfd = h->root.u.undef.abfd;
8958 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8959 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8960 return FALSE;
8961 break;
8962
8963 case bfd_link_hash_defined:
8964 case bfd_link_hash_defweak:
8965 abfd = h->root.u.def.section->owner;
8966 break;
8967
8968 case bfd_link_hash_common:
8969 abfd = h->root.u.c.p->section->owner;
8970 break;
8971 }
8972 BFD_ASSERT (abfd != NULL);
8973
8974 for (loaded = elf_hash_table (info)->loaded;
8975 loaded != NULL;
8976 loaded = loaded->next)
8977 {
8978 bfd *input;
8979 Elf_Internal_Shdr *hdr;
8980 bfd_size_type symcount;
8981 bfd_size_type extsymcount;
8982 bfd_size_type extsymoff;
8983 Elf_Internal_Shdr *versymhdr;
8984 Elf_Internal_Sym *isym;
8985 Elf_Internal_Sym *isymend;
8986 Elf_Internal_Sym *isymbuf;
8987 Elf_External_Versym *ever;
8988 Elf_External_Versym *extversym;
8989
8990 input = loaded->abfd;
8991
8992 /* We check each DSO for a possible hidden versioned definition. */
8993 if (input == abfd
8994 || (input->flags & DYNAMIC) == 0
8995 || elf_dynversym (input) == 0)
8996 continue;
8997
8998 hdr = &elf_tdata (input)->dynsymtab_hdr;
8999
9000 symcount = hdr->sh_size / bed->s->sizeof_sym;
9001 if (elf_bad_symtab (input))
9002 {
9003 extsymcount = symcount;
9004 extsymoff = 0;
9005 }
9006 else
9007 {
9008 extsymcount = symcount - hdr->sh_info;
9009 extsymoff = hdr->sh_info;
9010 }
9011
9012 if (extsymcount == 0)
9013 continue;
9014
9015 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9016 NULL, NULL, NULL);
9017 if (isymbuf == NULL)
9018 return FALSE;
9019
9020 /* Read in any version definitions. */
9021 versymhdr = &elf_tdata (input)->dynversym_hdr;
a50b1753 9022 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
c152c796
AM
9023 if (extversym == NULL)
9024 goto error_ret;
9025
9026 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9027 || (bfd_bread (extversym, versymhdr->sh_size, input)
9028 != versymhdr->sh_size))
9029 {
9030 free (extversym);
9031 error_ret:
9032 free (isymbuf);
9033 return FALSE;
9034 }
9035
9036 ever = extversym + extsymoff;
9037 isymend = isymbuf + extsymcount;
9038 for (isym = isymbuf; isym < isymend; isym++, ever++)
9039 {
9040 const char *name;
9041 Elf_Internal_Versym iver;
9042 unsigned short version_index;
9043
9044 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9045 || isym->st_shndx == SHN_UNDEF)
9046 continue;
9047
9048 name = bfd_elf_string_from_elf_section (input,
9049 hdr->sh_link,
9050 isym->st_name);
9051 if (strcmp (name, h->root.root.string) != 0)
9052 continue;
9053
9054 _bfd_elf_swap_versym_in (input, ever, &iver);
9055
d023c380
L
9056 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9057 && !(h->def_regular
9058 && h->forced_local))
c152c796
AM
9059 {
9060 /* If we have a non-hidden versioned sym, then it should
d023c380
L
9061 have provided a definition for the undefined sym unless
9062 it is defined in a non-shared object and forced local.
9063 */
c152c796
AM
9064 abort ();
9065 }
9066
9067 version_index = iver.vs_vers & VERSYM_VERSION;
9068 if (version_index == 1 || version_index == 2)
9069 {
9070 /* This is the base or first version. We can use it. */
9071 free (extversym);
9072 free (isymbuf);
9073 return TRUE;
9074 }
9075 }
9076
9077 free (extversym);
9078 free (isymbuf);
9079 }
9080
9081 return FALSE;
9082}
9083
b8871f35
L
9084/* Convert ELF common symbol TYPE. */
9085
9086static int
9087elf_link_convert_common_type (struct bfd_link_info *info, int type)
9088{
9089 /* Commom symbol can only appear in relocatable link. */
9090 if (!bfd_link_relocatable (info))
9091 abort ();
9092 switch (info->elf_stt_common)
9093 {
9094 case unchanged:
9095 break;
9096 case elf_stt_common:
9097 type = STT_COMMON;
9098 break;
9099 case no_elf_stt_common:
9100 type = STT_OBJECT;
9101 break;
9102 }
9103 return type;
9104}
9105
c152c796
AM
9106/* Add an external symbol to the symbol table. This is called from
9107 the hash table traversal routine. When generating a shared object,
9108 we go through the symbol table twice. The first time we output
9109 anything that might have been forced to local scope in a version
9110 script. The second time we output the symbols that are still
9111 global symbols. */
9112
9113static bfd_boolean
7686d77d 9114elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 9115{
7686d77d 9116 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 9117 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 9118 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
9119 bfd_boolean strip;
9120 Elf_Internal_Sym sym;
9121 asection *input_sec;
9122 const struct elf_backend_data *bed;
6e0b88f1
AM
9123 long indx;
9124 int ret;
b8871f35 9125 unsigned int type;
6e33951e
L
9126 /* A symbol is bound locally if it is forced local or it is locally
9127 defined, hidden versioned, not referenced by shared library and
9128 not exported when linking executable. */
9129 bfd_boolean local_bind = (h->forced_local
0e1862bb 9130 || (bfd_link_executable (flinfo->info)
6e33951e
L
9131 && !flinfo->info->export_dynamic
9132 && !h->dynamic
9133 && !h->ref_dynamic
9134 && h->def_regular
422f1182 9135 && h->versioned == versioned_hidden));
c152c796
AM
9136
9137 if (h->root.type == bfd_link_hash_warning)
9138 {
9139 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9140 if (h->root.type == bfd_link_hash_new)
9141 return TRUE;
9142 }
9143
9144 /* Decide whether to output this symbol in this pass. */
9145 if (eoinfo->localsyms)
9146 {
6e33951e 9147 if (!local_bind)
c152c796
AM
9148 return TRUE;
9149 }
9150 else
9151 {
6e33951e 9152 if (local_bind)
c152c796
AM
9153 return TRUE;
9154 }
9155
8b127cbc 9156 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9157
12ac1cf5 9158 if (h->root.type == bfd_link_hash_undefined)
c152c796 9159 {
12ac1cf5
NC
9160 /* If we have an undefined symbol reference here then it must have
9161 come from a shared library that is being linked in. (Undefined
98da7939
L
9162 references in regular files have already been handled unless
9163 they are in unreferenced sections which are removed by garbage
9164 collection). */
12ac1cf5
NC
9165 bfd_boolean ignore_undef = FALSE;
9166
9167 /* Some symbols may be special in that the fact that they're
9168 undefined can be safely ignored - let backend determine that. */
9169 if (bed->elf_backend_ignore_undef_symbol)
9170 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9171
9172 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 9173 if (!ignore_undef
12ac1cf5 9174 && h->ref_dynamic
8b127cbc
AM
9175 && (!h->ref_regular || flinfo->info->gc_sections)
9176 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9177 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9178 {
9179 if (!(flinfo->info->callbacks->undefined_symbol
9180 (flinfo->info, h->root.root.string,
9181 h->ref_regular ? NULL : h->root.u.undef.abfd,
9182 NULL, 0,
9183 (flinfo->info->unresolved_syms_in_shared_libs
9184 == RM_GENERATE_ERROR))))
12ac1cf5 9185 {
17d078c5 9186 bfd_set_error (bfd_error_bad_value);
12ac1cf5
NC
9187 eoinfo->failed = TRUE;
9188 return FALSE;
9189 }
c152c796
AM
9190 }
9191 }
9192
9193 /* We should also warn if a forced local symbol is referenced from
9194 shared libraries. */
0e1862bb 9195 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
9196 && h->forced_local
9197 && h->ref_dynamic
371a5866 9198 && h->def_regular
f5385ebf 9199 && !h->dynamic_def
ee659f1f 9200 && h->ref_dynamic_nonweak
8b127cbc 9201 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 9202 {
17d078c5
AM
9203 bfd *def_bfd;
9204 const char *msg;
90c984fc
L
9205 struct elf_link_hash_entry *hi = h;
9206
9207 /* Check indirect symbol. */
9208 while (hi->root.type == bfd_link_hash_indirect)
9209 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
9210
9211 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9212 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9213 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9214 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9215 else
9216 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8b127cbc 9217 def_bfd = flinfo->output_bfd;
90c984fc
L
9218 if (hi->root.u.def.section != bfd_abs_section_ptr)
9219 def_bfd = hi->root.u.def.section->owner;
8b127cbc 9220 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
17d078c5
AM
9221 h->root.root.string);
9222 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9223 eoinfo->failed = TRUE;
9224 return FALSE;
9225 }
9226
9227 /* We don't want to output symbols that have never been mentioned by
9228 a regular file, or that we have been told to strip. However, if
9229 h->indx is set to -2, the symbol is used by a reloc and we must
9230 output it. */
d983c8c5 9231 strip = FALSE;
c152c796 9232 if (h->indx == -2)
d983c8c5 9233 ;
f5385ebf 9234 else if ((h->def_dynamic
77cfaee6
AM
9235 || h->ref_dynamic
9236 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
9237 && !h->def_regular
9238 && !h->ref_regular)
c152c796 9239 strip = TRUE;
8b127cbc 9240 else if (flinfo->info->strip == strip_all)
c152c796 9241 strip = TRUE;
8b127cbc
AM
9242 else if (flinfo->info->strip == strip_some
9243 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
9244 h->root.root.string, FALSE, FALSE) == NULL)
9245 strip = TRUE;
d56d55e7
AM
9246 else if ((h->root.type == bfd_link_hash_defined
9247 || h->root.type == bfd_link_hash_defweak)
8b127cbc 9248 && ((flinfo->info->strip_discarded
dbaa2011 9249 && discarded_section (h->root.u.def.section))
ca4be51c
AM
9250 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9251 && h->root.u.def.section->owner != NULL
d56d55e7 9252 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 9253 strip = TRUE;
9e2278f5
AM
9254 else if ((h->root.type == bfd_link_hash_undefined
9255 || h->root.type == bfd_link_hash_undefweak)
9256 && h->root.u.undef.abfd != NULL
9257 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9258 strip = TRUE;
c152c796 9259
b8871f35
L
9260 type = h->type;
9261
c152c796 9262 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
9263 nothing else to do. However, if it is a forced local symbol or
9264 an ifunc symbol we need to give the backend finish_dynamic_symbol
9265 function a chance to make it dynamic. */
c152c796
AM
9266 if (strip
9267 && h->dynindx == -1
b8871f35 9268 && type != STT_GNU_IFUNC
f5385ebf 9269 && !h->forced_local)
c152c796
AM
9270 return TRUE;
9271
9272 sym.st_value = 0;
9273 sym.st_size = h->size;
9274 sym.st_other = h->other;
c152c796
AM
9275 switch (h->root.type)
9276 {
9277 default:
9278 case bfd_link_hash_new:
9279 case bfd_link_hash_warning:
9280 abort ();
9281 return FALSE;
9282
9283 case bfd_link_hash_undefined:
9284 case bfd_link_hash_undefweak:
9285 input_sec = bfd_und_section_ptr;
9286 sym.st_shndx = SHN_UNDEF;
9287 break;
9288
9289 case bfd_link_hash_defined:
9290 case bfd_link_hash_defweak:
9291 {
9292 input_sec = h->root.u.def.section;
9293 if (input_sec->output_section != NULL)
9294 {
9295 sym.st_shndx =
8b127cbc 9296 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
9297 input_sec->output_section);
9298 if (sym.st_shndx == SHN_BAD)
9299 {
9300 (*_bfd_error_handler)
d003868e 9301 (_("%B: could not find output section %A for input section %A"),
8b127cbc 9302 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 9303 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
9304 eoinfo->failed = TRUE;
9305 return FALSE;
9306 }
9307
9308 /* ELF symbols in relocatable files are section relative,
9309 but in nonrelocatable files they are virtual
9310 addresses. */
9311 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 9312 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9313 {
9314 sym.st_value += input_sec->output_section->vma;
9315 if (h->type == STT_TLS)
9316 {
8b127cbc 9317 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
9318 if (tls_sec != NULL)
9319 sym.st_value -= tls_sec->vma;
c152c796
AM
9320 }
9321 }
9322 }
9323 else
9324 {
9325 BFD_ASSERT (input_sec->owner == NULL
9326 || (input_sec->owner->flags & DYNAMIC) != 0);
9327 sym.st_shndx = SHN_UNDEF;
9328 input_sec = bfd_und_section_ptr;
9329 }
9330 }
9331 break;
9332
9333 case bfd_link_hash_common:
9334 input_sec = h->root.u.c.p->section;
a4d8e49b 9335 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
9336 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9337 break;
9338
9339 case bfd_link_hash_indirect:
9340 /* These symbols are created by symbol versioning. They point
9341 to the decorated version of the name. For example, if the
9342 symbol foo@@GNU_1.2 is the default, which should be used when
9343 foo is used with no version, then we add an indirect symbol
9344 foo which points to foo@@GNU_1.2. We ignore these symbols,
9345 since the indirected symbol is already in the hash table. */
9346 return TRUE;
9347 }
9348
b8871f35
L
9349 if (type == STT_COMMON || type == STT_OBJECT)
9350 switch (h->root.type)
9351 {
9352 case bfd_link_hash_common:
9353 type = elf_link_convert_common_type (flinfo->info, type);
9354 break;
9355 case bfd_link_hash_defined:
9356 case bfd_link_hash_defweak:
9357 if (bed->common_definition (&sym))
9358 type = elf_link_convert_common_type (flinfo->info, type);
9359 else
9360 type = STT_OBJECT;
9361 break;
9362 case bfd_link_hash_undefined:
9363 case bfd_link_hash_undefweak:
9364 break;
9365 default:
9366 abort ();
9367 }
9368
9369 if (local_bind)
9370 {
9371 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9372 /* Turn off visibility on local symbol. */
9373 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9374 }
9375 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9376 else if (h->unique_global && h->def_regular)
9377 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9378 else if (h->root.type == bfd_link_hash_undefweak
9379 || h->root.type == bfd_link_hash_defweak)
9380 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9381 else
9382 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9383 sym.st_target_internal = h->target_internal;
9384
c152c796
AM
9385 /* Give the processor backend a chance to tweak the symbol value,
9386 and also to finish up anything that needs to be done for this
9387 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 9388 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 9389 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 9390 if ((h->type == STT_GNU_IFUNC
5f35ea9c 9391 && h->def_regular
0e1862bb 9392 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
9393 || ((h->dynindx != -1
9394 || h->forced_local)
0e1862bb 9395 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
9396 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9397 || h->root.type != bfd_link_hash_undefweak))
9398 || !h->forced_local)
8b127cbc 9399 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
9400 {
9401 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 9402 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
9403 {
9404 eoinfo->failed = TRUE;
9405 return FALSE;
9406 }
9407 }
9408
9409 /* If we are marking the symbol as undefined, and there are no
9410 non-weak references to this symbol from a regular object, then
9411 mark the symbol as weak undefined; if there are non-weak
9412 references, mark the symbol as strong. We can't do this earlier,
9413 because it might not be marked as undefined until the
9414 finish_dynamic_symbol routine gets through with it. */
9415 if (sym.st_shndx == SHN_UNDEF
f5385ebf 9416 && h->ref_regular
c152c796
AM
9417 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9418 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9419 {
9420 int bindtype;
b8871f35 9421 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
9422
9423 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9424 if (type == STT_GNU_IFUNC)
9425 type = STT_FUNC;
c152c796 9426
f5385ebf 9427 if (h->ref_regular_nonweak)
c152c796
AM
9428 bindtype = STB_GLOBAL;
9429 else
9430 bindtype = STB_WEAK;
2955ec4c 9431 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
9432 }
9433
bda987c2
CD
9434 /* If this is a symbol defined in a dynamic library, don't use the
9435 symbol size from the dynamic library. Relinking an executable
9436 against a new library may introduce gratuitous changes in the
9437 executable's symbols if we keep the size. */
9438 if (sym.st_shndx == SHN_UNDEF
9439 && !h->def_regular
9440 && h->def_dynamic)
9441 sym.st_size = 0;
9442
c152c796
AM
9443 /* If a non-weak symbol with non-default visibility is not defined
9444 locally, it is a fatal error. */
0e1862bb 9445 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
9446 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9447 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9448 && h->root.type == bfd_link_hash_undefined
f5385ebf 9449 && !h->def_regular)
c152c796 9450 {
17d078c5
AM
9451 const char *msg;
9452
9453 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9454 msg = _("%B: protected symbol `%s' isn't defined");
9455 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9456 msg = _("%B: internal symbol `%s' isn't defined");
9457 else
9458 msg = _("%B: hidden symbol `%s' isn't defined");
8b127cbc 9459 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 9460 bfd_set_error (bfd_error_bad_value);
c152c796
AM
9461 eoinfo->failed = TRUE;
9462 return FALSE;
9463 }
9464
9465 /* If this symbol should be put in the .dynsym section, then put it
9466 there now. We already know the symbol index. We also fill in
9467 the entry in the .hash section. */
cae1fbbb 9468 if (elf_hash_table (flinfo->info)->dynsym != NULL
202e2356 9469 && h->dynindx != -1
8b127cbc 9470 && elf_hash_table (flinfo->info)->dynamic_sections_created)
c152c796 9471 {
c152c796
AM
9472 bfd_byte *esym;
9473
90c984fc
L
9474 /* Since there is no version information in the dynamic string,
9475 if there is no version info in symbol version section, we will
1659f720 9476 have a run-time problem if not linking executable, referenced
6e33951e
L
9477 by shared library, not locally defined, or not bound locally.
9478 */
1659f720 9479 if (h->verinfo.verdef == NULL
6e33951e 9480 && !local_bind
0e1862bb 9481 && (!bfd_link_executable (flinfo->info)
1659f720
L
9482 || h->ref_dynamic
9483 || !h->def_regular))
90c984fc
L
9484 {
9485 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9486
9487 if (p && p [1] != '\0')
9488 {
9489 (*_bfd_error_handler)
9490 (_("%B: No symbol version section for versioned symbol `%s'"),
9491 flinfo->output_bfd, h->root.root.string);
9492 eoinfo->failed = TRUE;
9493 return FALSE;
9494 }
9495 }
9496
c152c796 9497 sym.st_name = h->dynstr_index;
cae1fbbb
L
9498 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9499 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 9500 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
9501 {
9502 eoinfo->failed = TRUE;
9503 return FALSE;
9504 }
8b127cbc 9505 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 9506
8b127cbc 9507 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
9508 {
9509 size_t hash_entry_size;
9510 bfd_byte *bucketpos;
9511 bfd_vma chain;
41198d0c
L
9512 size_t bucketcount;
9513 size_t bucket;
9514
8b127cbc 9515 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 9516 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
9517
9518 hash_entry_size
8b127cbc
AM
9519 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9520 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 9521 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
9522 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9523 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9524 bucketpos);
9525 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9526 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
9527 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9528 }
c152c796 9529
8b127cbc 9530 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
9531 {
9532 Elf_Internal_Versym iversym;
9533 Elf_External_Versym *eversym;
9534
f5385ebf 9535 if (!h->def_regular)
c152c796 9536 {
7b20f099
AM
9537 if (h->verinfo.verdef == NULL
9538 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9539 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
9540 iversym.vs_vers = 0;
9541 else
9542 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9543 }
9544 else
9545 {
9546 if (h->verinfo.vertree == NULL)
9547 iversym.vs_vers = 1;
9548 else
9549 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 9550 if (flinfo->info->create_default_symver)
3e3b46e5 9551 iversym.vs_vers++;
c152c796
AM
9552 }
9553
422f1182 9554 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 9555 defined locally. */
422f1182 9556 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
9557 iversym.vs_vers |= VERSYM_HIDDEN;
9558
8b127cbc 9559 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 9560 eversym += h->dynindx;
8b127cbc 9561 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
9562 }
9563 }
9564
d983c8c5
AM
9565 /* If the symbol is undefined, and we didn't output it to .dynsym,
9566 strip it from .symtab too. Obviously we can't do this for
9567 relocatable output or when needed for --emit-relocs. */
9568 else if (input_sec == bfd_und_section_ptr
9569 && h->indx != -2
0e1862bb 9570 && !bfd_link_relocatable (flinfo->info))
d983c8c5
AM
9571 return TRUE;
9572 /* Also strip others that we couldn't earlier due to dynamic symbol
9573 processing. */
9574 if (strip)
9575 return TRUE;
9576 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
9577 return TRUE;
9578
2ec55de3
AM
9579 /* Output a FILE symbol so that following locals are not associated
9580 with the wrong input file. We need one for forced local symbols
9581 if we've seen more than one FILE symbol or when we have exactly
9582 one FILE symbol but global symbols are present in a file other
9583 than the one with the FILE symbol. We also need one if linker
9584 defined symbols are present. In practice these conditions are
9585 always met, so just emit the FILE symbol unconditionally. */
9586 if (eoinfo->localsyms
9587 && !eoinfo->file_sym_done
9588 && eoinfo->flinfo->filesym_count != 0)
9589 {
9590 Elf_Internal_Sym fsym;
9591
9592 memset (&fsym, 0, sizeof (fsym));
9593 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9594 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
9595 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9596 bfd_und_section_ptr, NULL))
2ec55de3
AM
9597 return FALSE;
9598
9599 eoinfo->file_sym_done = TRUE;
9600 }
9601
8b127cbc 9602 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9603 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9604 input_sec, h);
6e0b88f1 9605 if (ret == 0)
c152c796
AM
9606 {
9607 eoinfo->failed = TRUE;
9608 return FALSE;
9609 }
6e0b88f1
AM
9610 else if (ret == 1)
9611 h->indx = indx;
9612 else if (h->indx == -2)
9613 abort();
c152c796
AM
9614
9615 return TRUE;
9616}
9617
cdd3575c
AM
9618/* Return TRUE if special handling is done for relocs in SEC against
9619 symbols defined in discarded sections. */
9620
c152c796
AM
9621static bfd_boolean
9622elf_section_ignore_discarded_relocs (asection *sec)
9623{
9624 const struct elf_backend_data *bed;
9625
cdd3575c
AM
9626 switch (sec->sec_info_type)
9627 {
dbaa2011
AM
9628 case SEC_INFO_TYPE_STABS:
9629 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 9630 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
9631 return TRUE;
9632 default:
9633 break;
9634 }
c152c796
AM
9635
9636 bed = get_elf_backend_data (sec->owner);
9637 if (bed->elf_backend_ignore_discarded_relocs != NULL
9638 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9639 return TRUE;
9640
9641 return FALSE;
9642}
9643
9e66c942
AM
9644/* Return a mask saying how ld should treat relocations in SEC against
9645 symbols defined in discarded sections. If this function returns
9646 COMPLAIN set, ld will issue a warning message. If this function
9647 returns PRETEND set, and the discarded section was link-once and the
9648 same size as the kept link-once section, ld will pretend that the
9649 symbol was actually defined in the kept section. Otherwise ld will
9650 zero the reloc (at least that is the intent, but some cooperation by
9651 the target dependent code is needed, particularly for REL targets). */
9652
8a696751
AM
9653unsigned int
9654_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 9655{
9e66c942 9656 if (sec->flags & SEC_DEBUGGING)
69d54b1b 9657 return PRETEND;
cdd3575c
AM
9658
9659 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 9660 return 0;
cdd3575c
AM
9661
9662 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 9663 return 0;
cdd3575c 9664
9e66c942 9665 return COMPLAIN | PRETEND;
cdd3575c
AM
9666}
9667
3d7f7666
L
9668/* Find a match between a section and a member of a section group. */
9669
9670static asection *
c0f00686
L
9671match_group_member (asection *sec, asection *group,
9672 struct bfd_link_info *info)
3d7f7666
L
9673{
9674 asection *first = elf_next_in_group (group);
9675 asection *s = first;
9676
9677 while (s != NULL)
9678 {
c0f00686 9679 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
9680 return s;
9681
83180ade 9682 s = elf_next_in_group (s);
3d7f7666
L
9683 if (s == first)
9684 break;
9685 }
9686
9687 return NULL;
9688}
9689
01b3c8ab 9690/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
9691 to replace it. Return the replacement if it is OK. Otherwise return
9692 NULL. */
01b3c8ab
L
9693
9694asection *
c0f00686 9695_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
9696{
9697 asection *kept;
9698
9699 kept = sec->kept_section;
9700 if (kept != NULL)
9701 {
c2370991 9702 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9703 kept = match_group_member (sec, kept, info);
1dd2625f
BW
9704 if (kept != NULL
9705 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9706 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
01b3c8ab 9707 kept = NULL;
c2370991 9708 sec->kept_section = kept;
01b3c8ab
L
9709 }
9710 return kept;
9711}
9712
c152c796
AM
9713/* Link an input file into the linker output file. This function
9714 handles all the sections and relocations of the input file at once.
9715 This is so that we only have to read the local symbols once, and
9716 don't have to keep them in memory. */
9717
9718static bfd_boolean
8b127cbc 9719elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 9720{
ece5ef60 9721 int (*relocate_section)
c152c796
AM
9722 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9723 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9724 bfd *output_bfd;
9725 Elf_Internal_Shdr *symtab_hdr;
9726 size_t locsymcount;
9727 size_t extsymoff;
9728 Elf_Internal_Sym *isymbuf;
9729 Elf_Internal_Sym *isym;
9730 Elf_Internal_Sym *isymend;
9731 long *pindex;
9732 asection **ppsection;
9733 asection *o;
9734 const struct elf_backend_data *bed;
c152c796 9735 struct elf_link_hash_entry **sym_hashes;
310fd250
L
9736 bfd_size_type address_size;
9737 bfd_vma r_type_mask;
9738 int r_sym_shift;
ffbc01cc 9739 bfd_boolean have_file_sym = FALSE;
c152c796 9740
8b127cbc 9741 output_bfd = flinfo->output_bfd;
c152c796
AM
9742 bed = get_elf_backend_data (output_bfd);
9743 relocate_section = bed->elf_backend_relocate_section;
9744
9745 /* If this is a dynamic object, we don't want to do anything here:
9746 we don't want the local symbols, and we don't want the section
9747 contents. */
9748 if ((input_bfd->flags & DYNAMIC) != 0)
9749 return TRUE;
9750
c152c796
AM
9751 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9752 if (elf_bad_symtab (input_bfd))
9753 {
9754 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9755 extsymoff = 0;
9756 }
9757 else
9758 {
9759 locsymcount = symtab_hdr->sh_info;
9760 extsymoff = symtab_hdr->sh_info;
9761 }
9762
9763 /* Read the local symbols. */
9764 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9765 if (isymbuf == NULL && locsymcount != 0)
9766 {
9767 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
9768 flinfo->internal_syms,
9769 flinfo->external_syms,
9770 flinfo->locsym_shndx);
c152c796
AM
9771 if (isymbuf == NULL)
9772 return FALSE;
9773 }
9774
9775 /* Find local symbol sections and adjust values of symbols in
9776 SEC_MERGE sections. Write out those local symbols we know are
9777 going into the output file. */
9778 isymend = isymbuf + locsymcount;
8b127cbc 9779 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
9780 isym < isymend;
9781 isym++, pindex++, ppsection++)
9782 {
9783 asection *isec;
9784 const char *name;
9785 Elf_Internal_Sym osym;
6e0b88f1
AM
9786 long indx;
9787 int ret;
c152c796
AM
9788
9789 *pindex = -1;
9790
9791 if (elf_bad_symtab (input_bfd))
9792 {
9793 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9794 {
9795 *ppsection = NULL;
9796 continue;
9797 }
9798 }
9799
9800 if (isym->st_shndx == SHN_UNDEF)
9801 isec = bfd_und_section_ptr;
c152c796
AM
9802 else if (isym->st_shndx == SHN_ABS)
9803 isec = bfd_abs_section_ptr;
9804 else if (isym->st_shndx == SHN_COMMON)
9805 isec = bfd_com_section_ptr;
9806 else
9807 {
cb33740c
AM
9808 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9809 if (isec == NULL)
9810 {
9811 /* Don't attempt to output symbols with st_shnx in the
9812 reserved range other than SHN_ABS and SHN_COMMON. */
9813 *ppsection = NULL;
9814 continue;
9815 }
dbaa2011 9816 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
9817 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9818 isym->st_value =
9819 _bfd_merged_section_offset (output_bfd, &isec,
9820 elf_section_data (isec)->sec_info,
9821 isym->st_value);
c152c796
AM
9822 }
9823
9824 *ppsection = isec;
9825
d983c8c5
AM
9826 /* Don't output the first, undefined, symbol. In fact, don't
9827 output any undefined local symbol. */
9828 if (isec == bfd_und_section_ptr)
c152c796
AM
9829 continue;
9830
9831 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9832 {
9833 /* We never output section symbols. Instead, we use the
9834 section symbol of the corresponding section in the output
9835 file. */
9836 continue;
9837 }
9838
9839 /* If we are stripping all symbols, we don't want to output this
9840 one. */
8b127cbc 9841 if (flinfo->info->strip == strip_all)
c152c796
AM
9842 continue;
9843
9844 /* If we are discarding all local symbols, we don't want to
9845 output this one. If we are generating a relocatable output
9846 file, then some of the local symbols may be required by
9847 relocs; we output them below as we discover that they are
9848 needed. */
8b127cbc 9849 if (flinfo->info->discard == discard_all)
c152c796
AM
9850 continue;
9851
9852 /* If this symbol is defined in a section which we are
f02571c5
AM
9853 discarding, we don't need to keep it. */
9854 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
9855 && isym->st_shndx < SHN_LORESERVE
9856 && bfd_section_removed_from_list (output_bfd,
9857 isec->output_section))
e75a280b
L
9858 continue;
9859
c152c796
AM
9860 /* Get the name of the symbol. */
9861 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9862 isym->st_name);
9863 if (name == NULL)
9864 return FALSE;
9865
9866 /* See if we are discarding symbols with this name. */
8b127cbc
AM
9867 if ((flinfo->info->strip == strip_some
9868 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 9869 == NULL))
8b127cbc 9870 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
9871 && (isec->flags & SEC_MERGE)
9872 && !bfd_link_relocatable (flinfo->info))
8b127cbc 9873 || flinfo->info->discard == discard_l)
c152c796
AM
9874 && bfd_is_local_label_name (input_bfd, name)))
9875 continue;
9876
ffbc01cc
AM
9877 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9878 {
ce875075
AM
9879 if (input_bfd->lto_output)
9880 /* -flto puts a temp file name here. This means builds
9881 are not reproducible. Discard the symbol. */
9882 continue;
ffbc01cc
AM
9883 have_file_sym = TRUE;
9884 flinfo->filesym_count += 1;
9885 }
9886 if (!have_file_sym)
9887 {
9888 /* In the absence of debug info, bfd_find_nearest_line uses
9889 FILE symbols to determine the source file for local
9890 function symbols. Provide a FILE symbol here if input
9891 files lack such, so that their symbols won't be
9892 associated with a previous input file. It's not the
9893 source file, but the best we can do. */
9894 have_file_sym = TRUE;
9895 flinfo->filesym_count += 1;
9896 memset (&osym, 0, sizeof (osym));
9897 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9898 osym.st_shndx = SHN_ABS;
ef10c3ac
L
9899 if (!elf_link_output_symstrtab (flinfo,
9900 (input_bfd->lto_output ? NULL
9901 : input_bfd->filename),
9902 &osym, bfd_abs_section_ptr,
9903 NULL))
ffbc01cc
AM
9904 return FALSE;
9905 }
9906
c152c796
AM
9907 osym = *isym;
9908
9909 /* Adjust the section index for the output file. */
9910 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9911 isec->output_section);
9912 if (osym.st_shndx == SHN_BAD)
9913 return FALSE;
9914
c152c796
AM
9915 /* ELF symbols in relocatable files are section relative, but
9916 in executable files they are virtual addresses. Note that
9917 this code assumes that all ELF sections have an associated
9918 BFD section with a reasonable value for output_offset; below
9919 we assume that they also have a reasonable value for
9920 output_section. Any special sections must be set up to meet
9921 these requirements. */
9922 osym.st_value += isec->output_offset;
0e1862bb 9923 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
9924 {
9925 osym.st_value += isec->output_section->vma;
9926 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9927 {
9928 /* STT_TLS symbols are relative to PT_TLS segment base. */
8b127cbc
AM
9929 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9930 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
c152c796
AM
9931 }
9932 }
9933
6e0b88f1 9934 indx = bfd_get_symcount (output_bfd);
ef10c3ac 9935 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 9936 if (ret == 0)
c152c796 9937 return FALSE;
6e0b88f1
AM
9938 else if (ret == 1)
9939 *pindex = indx;
c152c796
AM
9940 }
9941
310fd250
L
9942 if (bed->s->arch_size == 32)
9943 {
9944 r_type_mask = 0xff;
9945 r_sym_shift = 8;
9946 address_size = 4;
9947 }
9948 else
9949 {
9950 r_type_mask = 0xffffffff;
9951 r_sym_shift = 32;
9952 address_size = 8;
9953 }
9954
c152c796
AM
9955 /* Relocate the contents of each section. */
9956 sym_hashes = elf_sym_hashes (input_bfd);
9957 for (o = input_bfd->sections; o != NULL; o = o->next)
9958 {
9959 bfd_byte *contents;
9960
9961 if (! o->linker_mark)
9962 {
9963 /* This section was omitted from the link. */
9964 continue;
9965 }
9966
0e1862bb 9967 if (bfd_link_relocatable (flinfo->info)
bcacc0f5
AM
9968 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9969 {
9970 /* Deal with the group signature symbol. */
9971 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9972 unsigned long symndx = sec_data->this_hdr.sh_info;
9973 asection *osec = o->output_section;
9974
9975 if (symndx >= locsymcount
9976 || (elf_bad_symtab (input_bfd)
8b127cbc 9977 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
9978 {
9979 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9980 while (h->root.type == bfd_link_hash_indirect
9981 || h->root.type == bfd_link_hash_warning)
9982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9983 /* Arrange for symbol to be output. */
9984 h->indx = -2;
9985 elf_section_data (osec)->this_hdr.sh_info = -2;
9986 }
9987 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9988 {
9989 /* We'll use the output section target_index. */
8b127cbc 9990 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
9991 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9992 }
9993 else
9994 {
8b127cbc 9995 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
9996 {
9997 /* Otherwise output the local symbol now. */
9998 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 9999 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 10000 const char *name;
6e0b88f1
AM
10001 long indx;
10002 int ret;
bcacc0f5
AM
10003
10004 name = bfd_elf_string_from_elf_section (input_bfd,
10005 symtab_hdr->sh_link,
10006 sym.st_name);
10007 if (name == NULL)
10008 return FALSE;
10009
10010 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10011 sec);
10012 if (sym.st_shndx == SHN_BAD)
10013 return FALSE;
10014
10015 sym.st_value += o->output_offset;
10016
6e0b88f1 10017 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10018 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10019 NULL);
6e0b88f1 10020 if (ret == 0)
bcacc0f5 10021 return FALSE;
6e0b88f1 10022 else if (ret == 1)
8b127cbc 10023 flinfo->indices[symndx] = indx;
6e0b88f1
AM
10024 else
10025 abort ();
bcacc0f5
AM
10026 }
10027 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 10028 = flinfo->indices[symndx];
bcacc0f5
AM
10029 }
10030 }
10031
c152c796 10032 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10033 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
10034 continue;
10035
10036 if ((o->flags & SEC_LINKER_CREATED) != 0)
10037 {
10038 /* Section was created by _bfd_elf_link_create_dynamic_sections
10039 or somesuch. */
10040 continue;
10041 }
10042
10043 /* Get the contents of the section. They have been cached by a
10044 relaxation routine. Note that o is a section in an input
10045 file, so the contents field will not have been set by any of
10046 the routines which work on output files. */
10047 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
10048 {
10049 contents = elf_section_data (o)->this_hdr.contents;
10050 if (bed->caches_rawsize
10051 && o->rawsize != 0
10052 && o->rawsize < o->size)
10053 {
10054 memcpy (flinfo->contents, contents, o->rawsize);
10055 contents = flinfo->contents;
10056 }
10057 }
c152c796
AM
10058 else
10059 {
8b127cbc 10060 contents = flinfo->contents;
4a114e3e 10061 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
10062 return FALSE;
10063 }
10064
10065 if ((o->flags & SEC_RELOC) != 0)
10066 {
10067 Elf_Internal_Rela *internal_relocs;
0f02bbd9 10068 Elf_Internal_Rela *rel, *relend;
0f02bbd9 10069 int action_discarded;
ece5ef60 10070 int ret;
c152c796
AM
10071
10072 /* Get the swapped relocs. */
10073 internal_relocs
8b127cbc
AM
10074 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10075 flinfo->internal_relocs, FALSE);
c152c796
AM
10076 if (internal_relocs == NULL
10077 && o->reloc_count > 0)
10078 return FALSE;
10079
310fd250
L
10080 /* We need to reverse-copy input .ctors/.dtors sections if
10081 they are placed in .init_array/.finit_array for output. */
10082 if (o->size > address_size
10083 && ((strncmp (o->name, ".ctors", 6) == 0
10084 && strcmp (o->output_section->name,
10085 ".init_array") == 0)
10086 || (strncmp (o->name, ".dtors", 6) == 0
10087 && strcmp (o->output_section->name,
10088 ".fini_array") == 0))
10089 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 10090 {
310fd250
L
10091 if (o->size != o->reloc_count * address_size)
10092 {
10093 (*_bfd_error_handler)
10094 (_("error: %B: size of section %A is not "
10095 "multiple of address size"),
10096 input_bfd, o);
10097 bfd_set_error (bfd_error_on_input);
10098 return FALSE;
10099 }
10100 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
10101 }
10102
0f02bbd9 10103 action_discarded = -1;
c152c796 10104 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
10105 action_discarded = (*bed->action_discarded) (o);
10106
10107 /* Run through the relocs evaluating complex reloc symbols and
10108 looking for relocs against symbols from discarded sections
10109 or section symbols from removed link-once sections.
10110 Complain about relocs against discarded sections. Zero
10111 relocs against removed link-once sections. */
10112
10113 rel = internal_relocs;
10114 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10115 for ( ; rel < relend; rel++)
c152c796 10116 {
0f02bbd9
AM
10117 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10118 unsigned int s_type;
10119 asection **ps, *sec;
10120 struct elf_link_hash_entry *h = NULL;
10121 const char *sym_name;
c152c796 10122
0f02bbd9
AM
10123 if (r_symndx == STN_UNDEF)
10124 continue;
c152c796 10125
0f02bbd9
AM
10126 if (r_symndx >= locsymcount
10127 || (elf_bad_symtab (input_bfd)
8b127cbc 10128 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
10129 {
10130 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 10131
0f02bbd9
AM
10132 /* Badly formatted input files can contain relocs that
10133 reference non-existant symbols. Check here so that
10134 we do not seg fault. */
10135 if (h == NULL)
c152c796 10136 {
0f02bbd9 10137 char buffer [32];
dce669a1 10138
0f02bbd9
AM
10139 sprintf_vma (buffer, rel->r_info);
10140 (*_bfd_error_handler)
10141 (_("error: %B contains a reloc (0x%s) for section %A "
10142 "that references a non-existent global symbol"),
10143 input_bfd, o, buffer);
10144 bfd_set_error (bfd_error_bad_value);
10145 return FALSE;
10146 }
3b36f7e6 10147
0f02bbd9
AM
10148 while (h->root.type == bfd_link_hash_indirect
10149 || h->root.type == bfd_link_hash_warning)
10150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 10151
0f02bbd9 10152 s_type = h->type;
cdd3575c 10153
9e2dec47 10154 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
10155 mark the symbol as undefined. Note that the
10156 linker may attach linker created dynamic sections
10157 to the plugin bfd. Symbols defined in linker
10158 created sections are not plugin symbols. */
9e2dec47
L
10159 if (h->root.non_ir_ref
10160 && (h->root.type == bfd_link_hash_defined
10161 || h->root.type == bfd_link_hash_defweak)
10162 && (h->root.u.def.section->flags
10163 & SEC_LINKER_CREATED) == 0
10164 && h->root.u.def.section->owner != NULL
10165 && (h->root.u.def.section->owner->flags
10166 & BFD_PLUGIN) != 0)
10167 {
10168 h->root.type = bfd_link_hash_undefined;
10169 h->root.u.undef.abfd = h->root.u.def.section->owner;
10170 }
10171
0f02bbd9
AM
10172 ps = NULL;
10173 if (h->root.type == bfd_link_hash_defined
10174 || h->root.type == bfd_link_hash_defweak)
10175 ps = &h->root.u.def.section;
10176
10177 sym_name = h->root.root.string;
10178 }
10179 else
10180 {
10181 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10182
10183 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 10184 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
10185 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10186 sym, *ps);
10187 }
c152c796 10188
c301e700 10189 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 10190 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
10191 {
10192 bfd_vma val;
10193 bfd_vma dot = (rel->r_offset
10194 + o->output_offset + o->output_section->vma);
10195#ifdef DEBUG
10196 printf ("Encountered a complex symbol!");
10197 printf (" (input_bfd %s, section %s, reloc %ld\n",
9ccb8af9
AM
10198 input_bfd->filename, o->name,
10199 (long) (rel - internal_relocs));
0f02bbd9
AM
10200 printf (" symbol: idx %8.8lx, name %s\n",
10201 r_symndx, sym_name);
10202 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10203 (unsigned long) rel->r_info,
10204 (unsigned long) rel->r_offset);
10205#endif
8b127cbc 10206 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
10207 isymbuf, locsymcount, s_type == STT_SRELC))
10208 return FALSE;
10209
10210 /* Symbol evaluated OK. Update to absolute value. */
10211 set_symbol_value (input_bfd, isymbuf, locsymcount,
10212 r_symndx, val);
10213 continue;
10214 }
10215
10216 if (action_discarded != -1 && ps != NULL)
10217 {
cdd3575c
AM
10218 /* Complain if the definition comes from a
10219 discarded section. */
dbaa2011 10220 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 10221 {
cf35638d 10222 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 10223 if (action_discarded & COMPLAIN)
8b127cbc 10224 (*flinfo->info->callbacks->einfo)
e1fffbe6 10225 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 10226 "defined in discarded section `%A' of %B\n"),
e1fffbe6 10227 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 10228
87e5235d 10229 /* Try to do the best we can to support buggy old
e0ae6d6f 10230 versions of gcc. Pretend that the symbol is
87e5235d
AM
10231 really defined in the kept linkonce section.
10232 FIXME: This is quite broken. Modifying the
10233 symbol here means we will be changing all later
e0ae6d6f 10234 uses of the symbol, not just in this section. */
0f02bbd9 10235 if (action_discarded & PRETEND)
87e5235d 10236 {
01b3c8ab
L
10237 asection *kept;
10238
c0f00686 10239 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 10240 flinfo->info);
01b3c8ab 10241 if (kept != NULL)
87e5235d
AM
10242 {
10243 *ps = kept;
10244 continue;
10245 }
10246 }
c152c796
AM
10247 }
10248 }
10249 }
10250
10251 /* Relocate the section by invoking a back end routine.
10252
10253 The back end routine is responsible for adjusting the
10254 section contents as necessary, and (if using Rela relocs
10255 and generating a relocatable output file) adjusting the
10256 reloc addend as necessary.
10257
10258 The back end routine does not have to worry about setting
10259 the reloc address or the reloc symbol index.
10260
10261 The back end routine is given a pointer to the swapped in
10262 internal symbols, and can access the hash table entries
10263 for the external symbols via elf_sym_hashes (input_bfd).
10264
10265 When generating relocatable output, the back end routine
10266 must handle STB_LOCAL/STT_SECTION symbols specially. The
10267 output symbol is going to be a section symbol
10268 corresponding to the output section, which will require
10269 the addend to be adjusted. */
10270
8b127cbc 10271 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
10272 input_bfd, o, contents,
10273 internal_relocs,
10274 isymbuf,
8b127cbc 10275 flinfo->sections);
ece5ef60 10276 if (!ret)
c152c796
AM
10277 return FALSE;
10278
ece5ef60 10279 if (ret == 2
0e1862bb 10280 || bfd_link_relocatable (flinfo->info)
8b127cbc 10281 || flinfo->info->emitrelocations)
c152c796
AM
10282 {
10283 Elf_Internal_Rela *irela;
d4730f92 10284 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
10285 bfd_vma last_offset;
10286 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
10287 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10288 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 10289 unsigned int next_erel;
c152c796 10290 bfd_boolean rela_normal;
d4730f92 10291 struct bfd_elf_section_data *esdi, *esdo;
c152c796 10292
d4730f92
BS
10293 esdi = elf_section_data (o);
10294 esdo = elf_section_data (o->output_section);
10295 rela_normal = FALSE;
c152c796
AM
10296
10297 /* Adjust the reloc addresses and symbol indices. */
10298
10299 irela = internal_relocs;
10300 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
d4730f92
BS
10301 rel_hash = esdo->rel.hashes + esdo->rel.count;
10302 /* We start processing the REL relocs, if any. When we reach
10303 IRELAMID in the loop, we switch to the RELA relocs. */
10304 irelamid = irela;
10305 if (esdi->rel.hdr != NULL)
10306 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10307 * bed->s->int_rels_per_ext_rel);
eac338cf 10308 rel_hash_list = rel_hash;
d4730f92 10309 rela_hash_list = NULL;
c152c796 10310 last_offset = o->output_offset;
0e1862bb 10311 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10312 last_offset += o->output_section->vma;
10313 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10314 {
10315 unsigned long r_symndx;
10316 asection *sec;
10317 Elf_Internal_Sym sym;
10318
10319 if (next_erel == bed->s->int_rels_per_ext_rel)
10320 {
10321 rel_hash++;
10322 next_erel = 0;
10323 }
10324
d4730f92
BS
10325 if (irela == irelamid)
10326 {
10327 rel_hash = esdo->rela.hashes + esdo->rela.count;
10328 rela_hash_list = rel_hash;
10329 rela_normal = bed->rela_normal;
10330 }
10331
c152c796 10332 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 10333 flinfo->info, o,
c152c796
AM
10334 irela->r_offset);
10335 if (irela->r_offset >= (bfd_vma) -2)
10336 {
10337 /* This is a reloc for a deleted entry or somesuch.
10338 Turn it into an R_*_NONE reloc, at the same
10339 offset as the last reloc. elf_eh_frame.c and
e460dd0d 10340 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
10341 being ordered. */
10342 irela->r_offset = last_offset;
10343 irela->r_info = 0;
10344 irela->r_addend = 0;
10345 continue;
10346 }
10347
10348 irela->r_offset += o->output_offset;
10349
10350 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 10351 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10352 irela->r_offset += o->output_section->vma;
10353
10354 last_offset = irela->r_offset;
10355
10356 r_symndx = irela->r_info >> r_sym_shift;
10357 if (r_symndx == STN_UNDEF)
10358 continue;
10359
10360 if (r_symndx >= locsymcount
10361 || (elf_bad_symtab (input_bfd)
8b127cbc 10362 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
10363 {
10364 struct elf_link_hash_entry *rh;
10365 unsigned long indx;
10366
10367 /* This is a reloc against a global symbol. We
10368 have not yet output all the local symbols, so
10369 we do not know the symbol index of any global
10370 symbol. We set the rel_hash entry for this
10371 reloc to point to the global hash table entry
10372 for this symbol. The symbol index is then
ee75fd95 10373 set at the end of bfd_elf_final_link. */
c152c796
AM
10374 indx = r_symndx - extsymoff;
10375 rh = elf_sym_hashes (input_bfd)[indx];
10376 while (rh->root.type == bfd_link_hash_indirect
10377 || rh->root.type == bfd_link_hash_warning)
10378 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10379
10380 /* Setting the index to -2 tells
10381 elf_link_output_extsym that this symbol is
10382 used by a reloc. */
10383 BFD_ASSERT (rh->indx < 0);
10384 rh->indx = -2;
10385
10386 *rel_hash = rh;
10387
10388 continue;
10389 }
10390
10391 /* This is a reloc against a local symbol. */
10392
10393 *rel_hash = NULL;
10394 sym = isymbuf[r_symndx];
8b127cbc 10395 sec = flinfo->sections[r_symndx];
c152c796
AM
10396 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10397 {
10398 /* I suppose the backend ought to fill in the
10399 section of any STT_SECTION symbol against a
6a8d1586 10400 processor specific section. */
cf35638d 10401 r_symndx = STN_UNDEF;
6a8d1586
AM
10402 if (bfd_is_abs_section (sec))
10403 ;
c152c796
AM
10404 else if (sec == NULL || sec->owner == NULL)
10405 {
10406 bfd_set_error (bfd_error_bad_value);
10407 return FALSE;
10408 }
10409 else
10410 {
6a8d1586
AM
10411 asection *osec = sec->output_section;
10412
10413 /* If we have discarded a section, the output
10414 section will be the absolute section. In
ab96bf03
AM
10415 case of discarded SEC_MERGE sections, use
10416 the kept section. relocate_section should
10417 have already handled discarded linkonce
10418 sections. */
6a8d1586
AM
10419 if (bfd_is_abs_section (osec)
10420 && sec->kept_section != NULL
10421 && sec->kept_section->output_section != NULL)
10422 {
10423 osec = sec->kept_section->output_section;
10424 irela->r_addend -= osec->vma;
10425 }
10426
10427 if (!bfd_is_abs_section (osec))
10428 {
10429 r_symndx = osec->target_index;
cf35638d 10430 if (r_symndx == STN_UNDEF)
74541ad4 10431 {
051d833a
AM
10432 irela->r_addend += osec->vma;
10433 osec = _bfd_nearby_section (output_bfd, osec,
10434 osec->vma);
10435 irela->r_addend -= osec->vma;
10436 r_symndx = osec->target_index;
74541ad4 10437 }
6a8d1586 10438 }
c152c796
AM
10439 }
10440
10441 /* Adjust the addend according to where the
10442 section winds up in the output section. */
10443 if (rela_normal)
10444 irela->r_addend += sec->output_offset;
10445 }
10446 else
10447 {
8b127cbc 10448 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
10449 {
10450 unsigned long shlink;
10451 const char *name;
10452 asection *osec;
6e0b88f1 10453 long indx;
c152c796 10454
8b127cbc 10455 if (flinfo->info->strip == strip_all)
c152c796
AM
10456 {
10457 /* You can't do ld -r -s. */
10458 bfd_set_error (bfd_error_invalid_operation);
10459 return FALSE;
10460 }
10461
10462 /* This symbol was skipped earlier, but
10463 since it is needed by a reloc, we
10464 must output it now. */
10465 shlink = symtab_hdr->sh_link;
10466 name = (bfd_elf_string_from_elf_section
10467 (input_bfd, shlink, sym.st_name));
10468 if (name == NULL)
10469 return FALSE;
10470
10471 osec = sec->output_section;
10472 sym.st_shndx =
10473 _bfd_elf_section_from_bfd_section (output_bfd,
10474 osec);
10475 if (sym.st_shndx == SHN_BAD)
10476 return FALSE;
10477
10478 sym.st_value += sec->output_offset;
0e1862bb 10479 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10480 {
10481 sym.st_value += osec->vma;
10482 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10483 {
10484 /* STT_TLS symbols are relative to PT_TLS
10485 segment base. */
8b127cbc 10486 BFD_ASSERT (elf_hash_table (flinfo->info)
c152c796 10487 ->tls_sec != NULL);
8b127cbc 10488 sym.st_value -= (elf_hash_table (flinfo->info)
c152c796
AM
10489 ->tls_sec->vma);
10490 }
10491 }
10492
6e0b88f1 10493 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
10494 ret = elf_link_output_symstrtab (flinfo, name,
10495 &sym, sec,
10496 NULL);
6e0b88f1 10497 if (ret == 0)
c152c796 10498 return FALSE;
6e0b88f1 10499 else if (ret == 1)
8b127cbc 10500 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
10501 else
10502 abort ();
c152c796
AM
10503 }
10504
8b127cbc 10505 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
10506 }
10507
10508 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10509 | (irela->r_info & r_type_mask));
10510 }
10511
10512 /* Swap out the relocs. */
d4730f92
BS
10513 input_rel_hdr = esdi->rel.hdr;
10514 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 10515 {
d4730f92
BS
10516 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10517 input_rel_hdr,
10518 internal_relocs,
10519 rel_hash_list))
10520 return FALSE;
c152c796
AM
10521 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10522 * bed->s->int_rels_per_ext_rel);
eac338cf 10523 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
10524 }
10525
10526 input_rela_hdr = esdi->rela.hdr;
10527 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10528 {
eac338cf 10529 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 10530 input_rela_hdr,
eac338cf 10531 internal_relocs,
d4730f92 10532 rela_hash_list))
c152c796
AM
10533 return FALSE;
10534 }
10535 }
10536 }
10537
10538 /* Write out the modified section contents. */
10539 if (bed->elf_backend_write_section
8b127cbc 10540 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 10541 contents))
c152c796
AM
10542 {
10543 /* Section written out. */
10544 }
10545 else switch (o->sec_info_type)
10546 {
dbaa2011 10547 case SEC_INFO_TYPE_STABS:
c152c796
AM
10548 if (! (_bfd_write_section_stabs
10549 (output_bfd,
8b127cbc 10550 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
10551 o, &elf_section_data (o)->sec_info, contents)))
10552 return FALSE;
10553 break;
dbaa2011 10554 case SEC_INFO_TYPE_MERGE:
c152c796
AM
10555 if (! _bfd_write_merged_section (output_bfd, o,
10556 elf_section_data (o)->sec_info))
10557 return FALSE;
10558 break;
dbaa2011 10559 case SEC_INFO_TYPE_EH_FRAME:
c152c796 10560 {
8b127cbc 10561 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
10562 o, contents))
10563 return FALSE;
10564 }
10565 break;
2f0c68f2
CM
10566 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10567 {
10568 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10569 flinfo->info,
10570 o, contents))
10571 return FALSE;
10572 }
10573 break;
c152c796
AM
10574 default:
10575 {
310fd250
L
10576 if (! (o->flags & SEC_EXCLUDE))
10577 {
10578 file_ptr offset = (file_ptr) o->output_offset;
10579 bfd_size_type todo = o->size;
37b01f6a
DG
10580
10581 offset *= bfd_octets_per_byte (output_bfd);
10582
310fd250
L
10583 if ((o->flags & SEC_ELF_REVERSE_COPY))
10584 {
10585 /* Reverse-copy input section to output. */
10586 do
10587 {
10588 todo -= address_size;
10589 if (! bfd_set_section_contents (output_bfd,
10590 o->output_section,
10591 contents + todo,
10592 offset,
10593 address_size))
10594 return FALSE;
10595 if (todo == 0)
10596 break;
10597 offset += address_size;
10598 }
10599 while (1);
10600 }
10601 else if (! bfd_set_section_contents (output_bfd,
10602 o->output_section,
10603 contents,
10604 offset, todo))
10605 return FALSE;
10606 }
c152c796
AM
10607 }
10608 break;
10609 }
10610 }
10611
10612 return TRUE;
10613}
10614
10615/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 10616 requested by the linker, and does not come from any input file. This
c152c796
AM
10617 is used to build constructor and destructor tables when linking
10618 with -Ur. */
10619
10620static bfd_boolean
10621elf_reloc_link_order (bfd *output_bfd,
10622 struct bfd_link_info *info,
10623 asection *output_section,
10624 struct bfd_link_order *link_order)
10625{
10626 reloc_howto_type *howto;
10627 long indx;
10628 bfd_vma offset;
10629 bfd_vma addend;
d4730f92 10630 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
10631 struct elf_link_hash_entry **rel_hash_ptr;
10632 Elf_Internal_Shdr *rel_hdr;
10633 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10634 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10635 bfd_byte *erel;
10636 unsigned int i;
d4730f92 10637 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
10638
10639 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10640 if (howto == NULL)
10641 {
10642 bfd_set_error (bfd_error_bad_value);
10643 return FALSE;
10644 }
10645
10646 addend = link_order->u.reloc.p->addend;
10647
d4730f92
BS
10648 if (esdo->rel.hdr)
10649 reldata = &esdo->rel;
10650 else if (esdo->rela.hdr)
10651 reldata = &esdo->rela;
10652 else
10653 {
10654 reldata = NULL;
10655 BFD_ASSERT (0);
10656 }
10657
c152c796 10658 /* Figure out the symbol index. */
d4730f92 10659 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
10660 if (link_order->type == bfd_section_reloc_link_order)
10661 {
10662 indx = link_order->u.reloc.p->u.section->target_index;
10663 BFD_ASSERT (indx != 0);
10664 *rel_hash_ptr = NULL;
10665 }
10666 else
10667 {
10668 struct elf_link_hash_entry *h;
10669
10670 /* Treat a reloc against a defined symbol as though it were
10671 actually against the section. */
10672 h = ((struct elf_link_hash_entry *)
10673 bfd_wrapped_link_hash_lookup (output_bfd, info,
10674 link_order->u.reloc.p->u.name,
10675 FALSE, FALSE, TRUE));
10676 if (h != NULL
10677 && (h->root.type == bfd_link_hash_defined
10678 || h->root.type == bfd_link_hash_defweak))
10679 {
10680 asection *section;
10681
10682 section = h->root.u.def.section;
10683 indx = section->output_section->target_index;
10684 *rel_hash_ptr = NULL;
10685 /* It seems that we ought to add the symbol value to the
10686 addend here, but in practice it has already been added
10687 because it was passed to constructor_callback. */
10688 addend += section->output_section->vma + section->output_offset;
10689 }
10690 else if (h != NULL)
10691 {
10692 /* Setting the index to -2 tells elf_link_output_extsym that
10693 this symbol is used by a reloc. */
10694 h->indx = -2;
10695 *rel_hash_ptr = h;
10696 indx = 0;
10697 }
10698 else
10699 {
10700 if (! ((*info->callbacks->unattached_reloc)
10701 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10702 return FALSE;
10703 indx = 0;
10704 }
10705 }
10706
10707 /* If this is an inplace reloc, we must write the addend into the
10708 object file. */
10709 if (howto->partial_inplace && addend != 0)
10710 {
10711 bfd_size_type size;
10712 bfd_reloc_status_type rstat;
10713 bfd_byte *buf;
10714 bfd_boolean ok;
10715 const char *sym_name;
10716
a50b1753
NC
10717 size = (bfd_size_type) bfd_get_reloc_size (howto);
10718 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 10719 if (buf == NULL && size != 0)
c152c796
AM
10720 return FALSE;
10721 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10722 switch (rstat)
10723 {
10724 case bfd_reloc_ok:
10725 break;
10726
10727 default:
10728 case bfd_reloc_outofrange:
10729 abort ();
10730
10731 case bfd_reloc_overflow:
10732 if (link_order->type == bfd_section_reloc_link_order)
10733 sym_name = bfd_section_name (output_bfd,
10734 link_order->u.reloc.p->u.section);
10735 else
10736 sym_name = link_order->u.reloc.p->u.name;
10737 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
10738 (info, NULL, sym_name, howto->name, addend, NULL,
10739 NULL, (bfd_vma) 0)))
c152c796
AM
10740 {
10741 free (buf);
10742 return FALSE;
10743 }
10744 break;
10745 }
37b01f6a 10746
c152c796 10747 ok = bfd_set_section_contents (output_bfd, output_section, buf,
37b01f6a
DG
10748 link_order->offset
10749 * bfd_octets_per_byte (output_bfd),
10750 size);
c152c796
AM
10751 free (buf);
10752 if (! ok)
10753 return FALSE;
10754 }
10755
10756 /* The address of a reloc is relative to the section in a
10757 relocatable file, and is a virtual address in an executable
10758 file. */
10759 offset = link_order->offset;
0e1862bb 10760 if (! bfd_link_relocatable (info))
c152c796
AM
10761 offset += output_section->vma;
10762
10763 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10764 {
10765 irel[i].r_offset = offset;
10766 irel[i].r_info = 0;
10767 irel[i].r_addend = 0;
10768 }
10769 if (bed->s->arch_size == 32)
10770 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10771 else
10772 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10773
d4730f92 10774 rel_hdr = reldata->hdr;
c152c796
AM
10775 erel = rel_hdr->contents;
10776 if (rel_hdr->sh_type == SHT_REL)
10777 {
d4730f92 10778 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
10779 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10780 }
10781 else
10782 {
10783 irel[0].r_addend = addend;
d4730f92 10784 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
10785 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10786 }
10787
d4730f92 10788 ++reldata->count;
c152c796
AM
10789
10790 return TRUE;
10791}
10792
0b52efa6
PB
10793
10794/* Get the output vma of the section pointed to by the sh_link field. */
10795
10796static bfd_vma
10797elf_get_linked_section_vma (struct bfd_link_order *p)
10798{
10799 Elf_Internal_Shdr **elf_shdrp;
10800 asection *s;
10801 int elfsec;
10802
10803 s = p->u.indirect.section;
10804 elf_shdrp = elf_elfsections (s->owner);
10805 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10806 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
10807 /* PR 290:
10808 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 10809 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
10810 sh_info fields. Hence we could get the situation
10811 where elfsec is 0. */
10812 if (elfsec == 0)
10813 {
10814 const struct elf_backend_data *bed
10815 = get_elf_backend_data (s->owner);
10816 if (bed->link_order_error_handler)
d003868e
AM
10817 bed->link_order_error_handler
10818 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
10819 return 0;
10820 }
10821 else
10822 {
10823 s = elf_shdrp[elfsec]->bfd_section;
10824 return s->output_section->vma + s->output_offset;
10825 }
0b52efa6
PB
10826}
10827
10828
10829/* Compare two sections based on the locations of the sections they are
10830 linked to. Used by elf_fixup_link_order. */
10831
10832static int
10833compare_link_order (const void * a, const void * b)
10834{
10835 bfd_vma apos;
10836 bfd_vma bpos;
10837
10838 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10839 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10840 if (apos < bpos)
10841 return -1;
10842 return apos > bpos;
10843}
10844
10845
10846/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10847 order as their linked sections. Returns false if this could not be done
10848 because an output section includes both ordered and unordered
10849 sections. Ideally we'd do this in the linker proper. */
10850
10851static bfd_boolean
10852elf_fixup_link_order (bfd *abfd, asection *o)
10853{
10854 int seen_linkorder;
10855 int seen_other;
10856 int n;
10857 struct bfd_link_order *p;
10858 bfd *sub;
10859 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 10860 unsigned elfsec;
0b52efa6 10861 struct bfd_link_order **sections;
d33cdfe3 10862 asection *s, *other_sec, *linkorder_sec;
0b52efa6 10863 bfd_vma offset;
3b36f7e6 10864
d33cdfe3
L
10865 other_sec = NULL;
10866 linkorder_sec = NULL;
0b52efa6
PB
10867 seen_other = 0;
10868 seen_linkorder = 0;
8423293d 10869 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 10870 {
d33cdfe3 10871 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
10872 {
10873 s = p->u.indirect.section;
d33cdfe3
L
10874 sub = s->owner;
10875 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10876 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
10877 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10878 && elfsec < elf_numsections (sub)
4fbb74a6
AM
10879 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10880 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
d33cdfe3
L
10881 {
10882 seen_linkorder++;
10883 linkorder_sec = s;
10884 }
0b52efa6 10885 else
d33cdfe3
L
10886 {
10887 seen_other++;
10888 other_sec = s;
10889 }
0b52efa6
PB
10890 }
10891 else
10892 seen_other++;
d33cdfe3
L
10893
10894 if (seen_other && seen_linkorder)
10895 {
10896 if (other_sec && linkorder_sec)
10897 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10898 o, linkorder_sec,
10899 linkorder_sec->owner, other_sec,
10900 other_sec->owner);
10901 else
10902 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10903 o);
10904 bfd_set_error (bfd_error_bad_value);
10905 return FALSE;
10906 }
0b52efa6
PB
10907 }
10908
10909 if (!seen_linkorder)
10910 return TRUE;
10911
0b52efa6 10912 sections = (struct bfd_link_order **)
14b1c01e
AM
10913 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10914 if (sections == NULL)
10915 return FALSE;
0b52efa6 10916 seen_linkorder = 0;
3b36f7e6 10917
8423293d 10918 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
10919 {
10920 sections[seen_linkorder++] = p;
10921 }
10922 /* Sort the input sections in the order of their linked section. */
10923 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10924 compare_link_order);
10925
10926 /* Change the offsets of the sections. */
10927 offset = 0;
10928 for (n = 0; n < seen_linkorder; n++)
10929 {
10930 s = sections[n]->u.indirect.section;
461686a3 10931 offset &= ~(bfd_vma) 0 << s->alignment_power;
37b01f6a 10932 s->output_offset = offset / bfd_octets_per_byte (abfd);
0b52efa6
PB
10933 sections[n]->offset = offset;
10934 offset += sections[n]->size;
10935 }
10936
4dd07732 10937 free (sections);
0b52efa6
PB
10938 return TRUE;
10939}
10940
9f7c3e5e
AM
10941static void
10942elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10943{
10944 asection *o;
10945
10946 if (flinfo->symstrtab != NULL)
ef10c3ac 10947 _bfd_elf_strtab_free (flinfo->symstrtab);
9f7c3e5e
AM
10948 if (flinfo->contents != NULL)
10949 free (flinfo->contents);
10950 if (flinfo->external_relocs != NULL)
10951 free (flinfo->external_relocs);
10952 if (flinfo->internal_relocs != NULL)
10953 free (flinfo->internal_relocs);
10954 if (flinfo->external_syms != NULL)
10955 free (flinfo->external_syms);
10956 if (flinfo->locsym_shndx != NULL)
10957 free (flinfo->locsym_shndx);
10958 if (flinfo->internal_syms != NULL)
10959 free (flinfo->internal_syms);
10960 if (flinfo->indices != NULL)
10961 free (flinfo->indices);
10962 if (flinfo->sections != NULL)
10963 free (flinfo->sections);
9f7c3e5e
AM
10964 if (flinfo->symshndxbuf != NULL)
10965 free (flinfo->symshndxbuf);
10966 for (o = obfd->sections; o != NULL; o = o->next)
10967 {
10968 struct bfd_elf_section_data *esdo = elf_section_data (o);
10969 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10970 free (esdo->rel.hashes);
10971 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10972 free (esdo->rela.hashes);
10973 }
10974}
0b52efa6 10975
c152c796
AM
10976/* Do the final step of an ELF link. */
10977
10978bfd_boolean
10979bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10980{
10981 bfd_boolean dynamic;
10982 bfd_boolean emit_relocs;
10983 bfd *dynobj;
8b127cbc 10984 struct elf_final_link_info flinfo;
91d6fa6a
NC
10985 asection *o;
10986 struct bfd_link_order *p;
10987 bfd *sub;
c152c796
AM
10988 bfd_size_type max_contents_size;
10989 bfd_size_type max_external_reloc_size;
10990 bfd_size_type max_internal_reloc_count;
10991 bfd_size_type max_sym_count;
10992 bfd_size_type max_sym_shndx_count;
c152c796
AM
10993 Elf_Internal_Sym elfsym;
10994 unsigned int i;
10995 Elf_Internal_Shdr *symtab_hdr;
10996 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
10997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10998 struct elf_outext_info eoinfo;
10999 bfd_boolean merged;
11000 size_t relativecount = 0;
11001 asection *reldyn = 0;
11002 bfd_size_type amt;
104d59d1
JM
11003 asection *attr_section = NULL;
11004 bfd_vma attr_size = 0;
11005 const char *std_attrs_section;
c152c796
AM
11006
11007 if (! is_elf_hash_table (info->hash))
11008 return FALSE;
11009
0e1862bb 11010 if (bfd_link_pic (info))
c152c796
AM
11011 abfd->flags |= DYNAMIC;
11012
11013 dynamic = elf_hash_table (info)->dynamic_sections_created;
11014 dynobj = elf_hash_table (info)->dynobj;
11015
0e1862bb 11016 emit_relocs = (bfd_link_relocatable (info)
a4676736 11017 || info->emitrelocations);
c152c796 11018
8b127cbc
AM
11019 flinfo.info = info;
11020 flinfo.output_bfd = abfd;
ef10c3ac 11021 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 11022 if (flinfo.symstrtab == NULL)
c152c796
AM
11023 return FALSE;
11024
11025 if (! dynamic)
11026 {
8b127cbc
AM
11027 flinfo.hash_sec = NULL;
11028 flinfo.symver_sec = NULL;
c152c796
AM
11029 }
11030 else
11031 {
3d4d4302 11032 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 11033 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 11034 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
11035 /* Note that it is OK if symver_sec is NULL. */
11036 }
11037
8b127cbc
AM
11038 flinfo.contents = NULL;
11039 flinfo.external_relocs = NULL;
11040 flinfo.internal_relocs = NULL;
11041 flinfo.external_syms = NULL;
11042 flinfo.locsym_shndx = NULL;
11043 flinfo.internal_syms = NULL;
11044 flinfo.indices = NULL;
11045 flinfo.sections = NULL;
8b127cbc 11046 flinfo.symshndxbuf = NULL;
ffbc01cc 11047 flinfo.filesym_count = 0;
c152c796 11048
104d59d1
JM
11049 /* The object attributes have been merged. Remove the input
11050 sections from the link, and set the contents of the output
11051 secton. */
11052 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11053 for (o = abfd->sections; o != NULL; o = o->next)
11054 {
11055 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11056 || strcmp (o->name, ".gnu.attributes") == 0)
11057 {
11058 for (p = o->map_head.link_order; p != NULL; p = p->next)
11059 {
11060 asection *input_section;
11061
11062 if (p->type != bfd_indirect_link_order)
11063 continue;
11064 input_section = p->u.indirect.section;
11065 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11066 elf_link_input_bfd ignores this section. */
11067 input_section->flags &= ~SEC_HAS_CONTENTS;
11068 }
a0c8462f 11069
104d59d1
JM
11070 attr_size = bfd_elf_obj_attr_size (abfd);
11071 if (attr_size)
11072 {
11073 bfd_set_section_size (abfd, o, attr_size);
11074 attr_section = o;
11075 /* Skip this section later on. */
11076 o->map_head.link_order = NULL;
11077 }
11078 else
11079 o->flags |= SEC_EXCLUDE;
11080 }
11081 }
11082
c152c796
AM
11083 /* Count up the number of relocations we will output for each output
11084 section, so that we know the sizes of the reloc sections. We
11085 also figure out some maximum sizes. */
11086 max_contents_size = 0;
11087 max_external_reloc_size = 0;
11088 max_internal_reloc_count = 0;
11089 max_sym_count = 0;
11090 max_sym_shndx_count = 0;
11091 merged = FALSE;
11092 for (o = abfd->sections; o != NULL; o = o->next)
11093 {
11094 struct bfd_elf_section_data *esdo = elf_section_data (o);
11095 o->reloc_count = 0;
11096
8423293d 11097 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11098 {
11099 unsigned int reloc_count = 0;
491d01d3 11100 unsigned int additional_reloc_count = 0;
c152c796 11101 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
11102
11103 if (p->type == bfd_section_reloc_link_order
11104 || p->type == bfd_symbol_reloc_link_order)
11105 reloc_count = 1;
11106 else if (p->type == bfd_indirect_link_order)
11107 {
11108 asection *sec;
11109
11110 sec = p->u.indirect.section;
11111 esdi = elf_section_data (sec);
11112
11113 /* Mark all sections which are to be included in the
11114 link. This will normally be every section. We need
11115 to do this so that we can identify any sections which
11116 the linker has decided to not include. */
11117 sec->linker_mark = TRUE;
11118
11119 if (sec->flags & SEC_MERGE)
11120 merged = TRUE;
11121
aed64b35
L
11122 if (esdo->this_hdr.sh_type == SHT_REL
11123 || esdo->this_hdr.sh_type == SHT_RELA)
11124 /* Some backends use reloc_count in relocation sections
11125 to count particular types of relocs. Of course,
11126 reloc sections themselves can't have relocations. */
11127 reloc_count = 0;
0e1862bb 11128 else if (emit_relocs)
491d01d3
YU
11129 {
11130 reloc_count = sec->reloc_count;
11131 if (bed->elf_backend_count_additional_relocs)
11132 {
11133 int c;
11134 c = (*bed->elf_backend_count_additional_relocs) (sec);
11135 additional_reloc_count += c;
11136 }
11137 }
c152c796 11138 else if (bed->elf_backend_count_relocs)
58217f29 11139 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
c152c796 11140
eea6121a
AM
11141 if (sec->rawsize > max_contents_size)
11142 max_contents_size = sec->rawsize;
11143 if (sec->size > max_contents_size)
11144 max_contents_size = sec->size;
c152c796
AM
11145
11146 /* We are interested in just local symbols, not all
11147 symbols. */
11148 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11149 && (sec->owner->flags & DYNAMIC) == 0)
11150 {
11151 size_t sym_count;
11152
11153 if (elf_bad_symtab (sec->owner))
11154 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11155 / bed->s->sizeof_sym);
11156 else
11157 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11158
11159 if (sym_count > max_sym_count)
11160 max_sym_count = sym_count;
11161
11162 if (sym_count > max_sym_shndx_count
6a40cf0c 11163 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
11164 max_sym_shndx_count = sym_count;
11165
11166 if ((sec->flags & SEC_RELOC) != 0)
11167 {
d4730f92 11168 size_t ext_size = 0;
c152c796 11169
d4730f92
BS
11170 if (esdi->rel.hdr != NULL)
11171 ext_size = esdi->rel.hdr->sh_size;
11172 if (esdi->rela.hdr != NULL)
11173 ext_size += esdi->rela.hdr->sh_size;
7326c758 11174
c152c796
AM
11175 if (ext_size > max_external_reloc_size)
11176 max_external_reloc_size = ext_size;
11177 if (sec->reloc_count > max_internal_reloc_count)
11178 max_internal_reloc_count = sec->reloc_count;
11179 }
11180 }
11181 }
11182
11183 if (reloc_count == 0)
11184 continue;
11185
491d01d3 11186 reloc_count += additional_reloc_count;
c152c796
AM
11187 o->reloc_count += reloc_count;
11188
0e1862bb 11189 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 11190 {
d4730f92 11191 if (esdi->rel.hdr)
491d01d3
YU
11192 {
11193 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11194 esdo->rel.count += additional_reloc_count;
11195 }
d4730f92 11196 if (esdi->rela.hdr)
491d01d3
YU
11197 {
11198 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11199 esdo->rela.count += additional_reloc_count;
11200 }
d4730f92
BS
11201 }
11202 else
11203 {
11204 if (o->use_rela_p)
11205 esdo->rela.count += reloc_count;
2c2b4ed4 11206 else
d4730f92 11207 esdo->rel.count += reloc_count;
c152c796 11208 }
c152c796
AM
11209 }
11210
11211 if (o->reloc_count > 0)
11212 o->flags |= SEC_RELOC;
11213 else
11214 {
11215 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11216 set it (this is probably a bug) and if it is set
11217 assign_section_numbers will create a reloc section. */
11218 o->flags &=~ SEC_RELOC;
11219 }
11220
11221 /* If the SEC_ALLOC flag is not set, force the section VMA to
11222 zero. This is done in elf_fake_sections as well, but forcing
11223 the VMA to 0 here will ensure that relocs against these
11224 sections are handled correctly. */
11225 if ((o->flags & SEC_ALLOC) == 0
11226 && ! o->user_set_vma)
11227 o->vma = 0;
11228 }
11229
0e1862bb 11230 if (! bfd_link_relocatable (info) && merged)
c152c796
AM
11231 elf_link_hash_traverse (elf_hash_table (info),
11232 _bfd_elf_link_sec_merge_syms, abfd);
11233
11234 /* Figure out the file positions for everything but the symbol table
11235 and the relocs. We set symcount to force assign_section_numbers
11236 to create a symbol table. */
8539e4e8 11237 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
c152c796
AM
11238 BFD_ASSERT (! abfd->output_has_begun);
11239 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11240 goto error_return;
11241
ee75fd95 11242 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
11243 for (o = abfd->sections; o != NULL; o = o->next)
11244 {
d4730f92 11245 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
11246 if ((o->flags & SEC_RELOC) != 0)
11247 {
d4730f92
BS
11248 if (esdo->rel.hdr
11249 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
11250 goto error_return;
11251
d4730f92
BS
11252 if (esdo->rela.hdr
11253 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
11254 goto error_return;
11255 }
11256
11257 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11258 to count upwards while actually outputting the relocations. */
d4730f92
BS
11259 esdo->rel.count = 0;
11260 esdo->rela.count = 0;
0ce398f1
L
11261
11262 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11263 {
11264 /* Cache the section contents so that they can be compressed
11265 later. Use bfd_malloc since it will be freed by
11266 bfd_compress_section_contents. */
11267 unsigned char *contents = esdo->this_hdr.contents;
11268 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11269 abort ();
11270 contents
11271 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11272 if (contents == NULL)
11273 goto error_return;
11274 esdo->this_hdr.contents = contents;
11275 }
c152c796
AM
11276 }
11277
c152c796 11278 /* We have now assigned file positions for all the sections except
a485e98e
AM
11279 .symtab, .strtab, and non-loaded reloc sections. We start the
11280 .symtab section at the current file position, and write directly
11281 to it. We build the .strtab section in memory. */
c152c796
AM
11282 bfd_get_symcount (abfd) = 0;
11283 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11284 /* sh_name is set in prep_headers. */
11285 symtab_hdr->sh_type = SHT_SYMTAB;
11286 /* sh_flags, sh_addr and sh_size all start off zero. */
11287 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11288 /* sh_link is set in assign_section_numbers. */
11289 /* sh_info is set below. */
11290 /* sh_offset is set just below. */
72de5009 11291 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 11292
ef10c3ac
L
11293 if (max_sym_count < 20)
11294 max_sym_count = 20;
11295 elf_hash_table (info)->strtabsize = max_sym_count;
11296 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11297 elf_hash_table (info)->strtab
11298 = (struct elf_sym_strtab *) bfd_malloc (amt);
11299 if (elf_hash_table (info)->strtab == NULL)
c152c796 11300 goto error_return;
ef10c3ac
L
11301 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11302 flinfo.symshndxbuf
11303 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11304 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 11305
8539e4e8 11306 if (info->strip != strip_all || emit_relocs)
c152c796 11307 {
8539e4e8
AM
11308 file_ptr off = elf_next_file_pos (abfd);
11309
11310 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11311
11312 /* Note that at this point elf_next_file_pos (abfd) is
11313 incorrect. We do not yet know the size of the .symtab section.
11314 We correct next_file_pos below, after we do know the size. */
11315
11316 /* Start writing out the symbol table. The first symbol is always a
11317 dummy symbol. */
c152c796
AM
11318 elfsym.st_value = 0;
11319 elfsym.st_size = 0;
11320 elfsym.st_info = 0;
11321 elfsym.st_other = 0;
11322 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 11323 elfsym.st_target_internal = 0;
ef10c3ac
L
11324 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11325 bfd_und_section_ptr, NULL) != 1)
c152c796 11326 goto error_return;
c152c796 11327
8539e4e8
AM
11328 /* Output a symbol for each section. We output these even if we are
11329 discarding local symbols, since they are used for relocs. These
11330 symbols have no names. We store the index of each one in the
11331 index field of the section, so that we can find it again when
11332 outputting relocs. */
11333
c152c796
AM
11334 elfsym.st_size = 0;
11335 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11336 elfsym.st_other = 0;
f0b5bb34 11337 elfsym.st_value = 0;
35fc36a8 11338 elfsym.st_target_internal = 0;
c152c796
AM
11339 for (i = 1; i < elf_numsections (abfd); i++)
11340 {
11341 o = bfd_section_from_elf_index (abfd, i);
11342 if (o != NULL)
f0b5bb34
AM
11343 {
11344 o->target_index = bfd_get_symcount (abfd);
11345 elfsym.st_shndx = i;
0e1862bb 11346 if (!bfd_link_relocatable (info))
f0b5bb34 11347 elfsym.st_value = o->vma;
ef10c3ac
L
11348 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11349 NULL) != 1)
f0b5bb34
AM
11350 goto error_return;
11351 }
c152c796
AM
11352 }
11353 }
11354
11355 /* Allocate some memory to hold information read in from the input
11356 files. */
11357 if (max_contents_size != 0)
11358 {
8b127cbc
AM
11359 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11360 if (flinfo.contents == NULL)
c152c796
AM
11361 goto error_return;
11362 }
11363
11364 if (max_external_reloc_size != 0)
11365 {
8b127cbc
AM
11366 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11367 if (flinfo.external_relocs == NULL)
c152c796
AM
11368 goto error_return;
11369 }
11370
11371 if (max_internal_reloc_count != 0)
11372 {
11373 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11374 amt *= sizeof (Elf_Internal_Rela);
8b127cbc
AM
11375 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11376 if (flinfo.internal_relocs == NULL)
c152c796
AM
11377 goto error_return;
11378 }
11379
11380 if (max_sym_count != 0)
11381 {
11382 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
11383 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11384 if (flinfo.external_syms == NULL)
c152c796
AM
11385 goto error_return;
11386
11387 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
11388 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11389 if (flinfo.internal_syms == NULL)
c152c796
AM
11390 goto error_return;
11391
11392 amt = max_sym_count * sizeof (long);
8b127cbc
AM
11393 flinfo.indices = (long int *) bfd_malloc (amt);
11394 if (flinfo.indices == NULL)
c152c796
AM
11395 goto error_return;
11396
11397 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
11398 flinfo.sections = (asection **) bfd_malloc (amt);
11399 if (flinfo.sections == NULL)
c152c796
AM
11400 goto error_return;
11401 }
11402
11403 if (max_sym_shndx_count != 0)
11404 {
11405 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
11406 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11407 if (flinfo.locsym_shndx == NULL)
c152c796
AM
11408 goto error_return;
11409 }
11410
11411 if (elf_hash_table (info)->tls_sec)
11412 {
11413 bfd_vma base, end = 0;
11414 asection *sec;
11415
11416 for (sec = elf_hash_table (info)->tls_sec;
11417 sec && (sec->flags & SEC_THREAD_LOCAL);
11418 sec = sec->next)
11419 {
3a800eb9 11420 bfd_size_type size = sec->size;
c152c796 11421
3a800eb9
AM
11422 if (size == 0
11423 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 11424 {
91d6fa6a
NC
11425 struct bfd_link_order *ord = sec->map_tail.link_order;
11426
11427 if (ord != NULL)
11428 size = ord->offset + ord->size;
c152c796
AM
11429 }
11430 end = sec->vma + size;
11431 }
11432 base = elf_hash_table (info)->tls_sec->vma;
7dc98aea
RO
11433 /* Only align end of TLS section if static TLS doesn't have special
11434 alignment requirements. */
11435 if (bed->static_tls_alignment == 1)
11436 end = align_power (end,
11437 elf_hash_table (info)->tls_sec->alignment_power);
c152c796
AM
11438 elf_hash_table (info)->tls_size = end - base;
11439 }
11440
0b52efa6
PB
11441 /* Reorder SHF_LINK_ORDER sections. */
11442 for (o = abfd->sections; o != NULL; o = o->next)
11443 {
11444 if (!elf_fixup_link_order (abfd, o))
11445 return FALSE;
11446 }
11447
2f0c68f2
CM
11448 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11449 return FALSE;
11450
c152c796
AM
11451 /* Since ELF permits relocations to be against local symbols, we
11452 must have the local symbols available when we do the relocations.
11453 Since we would rather only read the local symbols once, and we
11454 would rather not keep them in memory, we handle all the
11455 relocations for a single input file at the same time.
11456
11457 Unfortunately, there is no way to know the total number of local
11458 symbols until we have seen all of them, and the local symbol
11459 indices precede the global symbol indices. This means that when
11460 we are generating relocatable output, and we see a reloc against
11461 a global symbol, we can not know the symbol index until we have
11462 finished examining all the local symbols to see which ones we are
11463 going to output. To deal with this, we keep the relocations in
11464 memory, and don't output them until the end of the link. This is
11465 an unfortunate waste of memory, but I don't see a good way around
11466 it. Fortunately, it only happens when performing a relocatable
11467 link, which is not the common case. FIXME: If keep_memory is set
11468 we could write the relocs out and then read them again; I don't
11469 know how bad the memory loss will be. */
11470
c72f2fb2 11471 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
11472 sub->output_has_begun = FALSE;
11473 for (o = abfd->sections; o != NULL; o = o->next)
11474 {
8423293d 11475 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
11476 {
11477 if (p->type == bfd_indirect_link_order
11478 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11479 == bfd_target_elf_flavour)
11480 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11481 {
11482 if (! sub->output_has_begun)
11483 {
8b127cbc 11484 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
11485 goto error_return;
11486 sub->output_has_begun = TRUE;
11487 }
11488 }
11489 else if (p->type == bfd_section_reloc_link_order
11490 || p->type == bfd_symbol_reloc_link_order)
11491 {
11492 if (! elf_reloc_link_order (abfd, info, o, p))
11493 goto error_return;
11494 }
11495 else
11496 {
11497 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
11498 {
11499 if (p->type == bfd_indirect_link_order
11500 && (bfd_get_flavour (sub)
11501 == bfd_target_elf_flavour)
11502 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11503 != bed->s->elfclass))
11504 {
11505 const char *iclass, *oclass;
11506
aebf9be7 11507 switch (bed->s->elfclass)
351f65ca 11508 {
aebf9be7
NC
11509 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11510 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11511 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11512 default: abort ();
351f65ca 11513 }
aebf9be7
NC
11514
11515 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 11516 {
aebf9be7
NC
11517 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11518 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11519 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11520 default: abort ();
351f65ca
L
11521 }
11522
11523 bfd_set_error (bfd_error_wrong_format);
11524 (*_bfd_error_handler)
11525 (_("%B: file class %s incompatible with %s"),
11526 sub, iclass, oclass);
11527 }
11528
11529 goto error_return;
11530 }
c152c796
AM
11531 }
11532 }
11533 }
11534
c0f00686
L
11535 /* Free symbol buffer if needed. */
11536 if (!info->reduce_memory_overheads)
11537 {
c72f2fb2 11538 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3fcd97f1
JJ
11539 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11540 && elf_tdata (sub)->symbuf)
c0f00686
L
11541 {
11542 free (elf_tdata (sub)->symbuf);
11543 elf_tdata (sub)->symbuf = NULL;
11544 }
11545 }
11546
c152c796
AM
11547 /* Output any global symbols that got converted to local in a
11548 version script or due to symbol visibility. We do this in a
11549 separate step since ELF requires all local symbols to appear
11550 prior to any global symbols. FIXME: We should only do this if
11551 some global symbols were, in fact, converted to become local.
11552 FIXME: Will this work correctly with the Irix 5 linker? */
11553 eoinfo.failed = FALSE;
8b127cbc 11554 eoinfo.flinfo = &flinfo;
c152c796 11555 eoinfo.localsyms = TRUE;
34a79995 11556 eoinfo.file_sym_done = FALSE;
7686d77d 11557 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11558 if (eoinfo.failed)
11559 return FALSE;
11560
4e617b1e
PB
11561 /* If backend needs to output some local symbols not present in the hash
11562 table, do it now. */
8539e4e8
AM
11563 if (bed->elf_backend_output_arch_local_syms
11564 && (info->strip != strip_all || emit_relocs))
4e617b1e 11565 {
6e0b88f1 11566 typedef int (*out_sym_func)
4e617b1e
PB
11567 (void *, const char *, Elf_Internal_Sym *, asection *,
11568 struct elf_link_hash_entry *);
11569
11570 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
11571 (abfd, info, &flinfo,
11572 (out_sym_func) elf_link_output_symstrtab)))
4e617b1e
PB
11573 return FALSE;
11574 }
11575
c152c796
AM
11576 /* That wrote out all the local symbols. Finish up the symbol table
11577 with the global symbols. Even if we want to strip everything we
11578 can, we still need to deal with those global symbols that got
11579 converted to local in a version script. */
11580
11581 /* The sh_info field records the index of the first non local symbol. */
11582 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11583
11584 if (dynamic
cae1fbbb
L
11585 && elf_hash_table (info)->dynsym != NULL
11586 && (elf_hash_table (info)->dynsym->output_section
11587 != bfd_abs_section_ptr))
c152c796
AM
11588 {
11589 Elf_Internal_Sym sym;
cae1fbbb 11590 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
c152c796
AM
11591 long last_local = 0;
11592
11593 /* Write out the section symbols for the output sections. */
0e1862bb
L
11594 if (bfd_link_pic (info)
11595 || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
11596 {
11597 asection *s;
11598
11599 sym.st_size = 0;
11600 sym.st_name = 0;
11601 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11602 sym.st_other = 0;
35fc36a8 11603 sym.st_target_internal = 0;
c152c796
AM
11604
11605 for (s = abfd->sections; s != NULL; s = s->next)
11606 {
11607 int indx;
11608 bfd_byte *dest;
11609 long dynindx;
11610
c152c796 11611 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
11612 if (dynindx <= 0)
11613 continue;
11614 indx = elf_section_data (s)->this_idx;
c152c796
AM
11615 BFD_ASSERT (indx > 0);
11616 sym.st_shndx = indx;
c0d5a53d
L
11617 if (! check_dynsym (abfd, &sym))
11618 return FALSE;
c152c796
AM
11619 sym.st_value = s->vma;
11620 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
11621 if (last_local < dynindx)
11622 last_local = dynindx;
c152c796
AM
11623 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11624 }
c152c796
AM
11625 }
11626
11627 /* Write out the local dynsyms. */
11628 if (elf_hash_table (info)->dynlocal)
11629 {
11630 struct elf_link_local_dynamic_entry *e;
11631 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11632 {
11633 asection *s;
11634 bfd_byte *dest;
11635
935bd1e0 11636 /* Copy the internal symbol and turn off visibility.
c152c796
AM
11637 Note that we saved a word of storage and overwrote
11638 the original st_name with the dynstr_index. */
11639 sym = e->isym;
935bd1e0 11640 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
c152c796 11641
cb33740c
AM
11642 s = bfd_section_from_elf_index (e->input_bfd,
11643 e->isym.st_shndx);
11644 if (s != NULL)
c152c796 11645 {
c152c796
AM
11646 sym.st_shndx =
11647 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
11648 if (! check_dynsym (abfd, &sym))
11649 return FALSE;
c152c796
AM
11650 sym.st_value = (s->output_section->vma
11651 + s->output_offset
11652 + e->isym.st_value);
11653 }
11654
11655 if (last_local < e->dynindx)
11656 last_local = e->dynindx;
11657
11658 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11659 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11660 }
11661 }
11662
cae1fbbb 11663 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
c152c796
AM
11664 last_local + 1;
11665 }
11666
11667 /* We get the global symbols from the hash table. */
11668 eoinfo.failed = FALSE;
11669 eoinfo.localsyms = FALSE;
8b127cbc 11670 eoinfo.flinfo = &flinfo;
7686d77d 11671 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796
AM
11672 if (eoinfo.failed)
11673 return FALSE;
11674
11675 /* If backend needs to output some symbols not present in the hash
11676 table, do it now. */
8539e4e8
AM
11677 if (bed->elf_backend_output_arch_syms
11678 && (info->strip != strip_all || emit_relocs))
c152c796 11679 {
6e0b88f1 11680 typedef int (*out_sym_func)
c152c796
AM
11681 (void *, const char *, Elf_Internal_Sym *, asection *,
11682 struct elf_link_hash_entry *);
11683
11684 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
11685 (abfd, info, &flinfo,
11686 (out_sym_func) elf_link_output_symstrtab)))
c152c796
AM
11687 return FALSE;
11688 }
11689
ef10c3ac
L
11690 /* Finalize the .strtab section. */
11691 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11692
11693 /* Swap out the .strtab section. */
11694 if (!elf_link_swap_symbols_out (&flinfo))
c152c796
AM
11695 return FALSE;
11696
11697 /* Now we know the size of the symtab section. */
c152c796
AM
11698 if (bfd_get_symcount (abfd) > 0)
11699 {
ee3b52e9
L
11700 /* Finish up and write out the symbol string table (.strtab)
11701 section. */
11702 Elf_Internal_Shdr *symstrtab_hdr;
8539e4e8
AM
11703 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11704
6a40cf0c
NC
11705 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11706 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
8539e4e8
AM
11707 {
11708 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11709 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11710 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11711 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11712 symtab_shndx_hdr->sh_size = amt;
11713
11714 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11715 off, TRUE);
11716
11717 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11718 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11719 return FALSE;
11720 }
ee3b52e9
L
11721
11722 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11723 /* sh_name was set in prep_headers. */
11724 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 11725 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 11726 symstrtab_hdr->sh_addr = 0;
ef10c3ac 11727 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
11728 symstrtab_hdr->sh_entsize = 0;
11729 symstrtab_hdr->sh_link = 0;
11730 symstrtab_hdr->sh_info = 0;
11731 /* sh_offset is set just below. */
11732 symstrtab_hdr->sh_addralign = 1;
11733
11734 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11735 off, TRUE);
11736 elf_next_file_pos (abfd) = off;
11737
c152c796 11738 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 11739 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
c152c796
AM
11740 return FALSE;
11741 }
11742
11743 /* Adjust the relocs to have the correct symbol indices. */
11744 for (o = abfd->sections; o != NULL; o = o->next)
11745 {
d4730f92 11746 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 11747 bfd_boolean sort;
c152c796
AM
11748 if ((o->flags & SEC_RELOC) == 0)
11749 continue;
11750
28dbcedc 11751 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3
AM
11752 if (esdo->rel.hdr != NULL
11753 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11754 return FALSE;
11755 if (esdo->rela.hdr != NULL
11756 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11757 return FALSE;
c152c796
AM
11758
11759 /* Set the reloc_count field to 0 to prevent write_relocs from
11760 trying to swap the relocs out itself. */
11761 o->reloc_count = 0;
11762 }
11763
11764 if (dynamic && info->combreloc && dynobj != NULL)
11765 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11766
11767 /* If we are linking against a dynamic object, or generating a
11768 shared library, finish up the dynamic linking information. */
11769 if (dynamic)
11770 {
11771 bfd_byte *dyncon, *dynconend;
11772
11773 /* Fix up .dynamic entries. */
3d4d4302 11774 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
11775 BFD_ASSERT (o != NULL);
11776
11777 dyncon = o->contents;
eea6121a 11778 dynconend = o->contents + o->size;
c152c796
AM
11779 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11780 {
11781 Elf_Internal_Dyn dyn;
11782 const char *name;
11783 unsigned int type;
11784
11785 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11786
11787 switch (dyn.d_tag)
11788 {
11789 default:
11790 continue;
11791 case DT_NULL:
11792 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11793 {
11794 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11795 {
11796 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11797 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11798 default: continue;
11799 }
11800 dyn.d_un.d_val = relativecount;
11801 relativecount = 0;
11802 break;
11803 }
11804 continue;
11805
11806 case DT_INIT:
11807 name = info->init_function;
11808 goto get_sym;
11809 case DT_FINI:
11810 name = info->fini_function;
11811 get_sym:
11812 {
11813 struct elf_link_hash_entry *h;
11814
11815 h = elf_link_hash_lookup (elf_hash_table (info), name,
11816 FALSE, FALSE, TRUE);
11817 if (h != NULL
11818 && (h->root.type == bfd_link_hash_defined
11819 || h->root.type == bfd_link_hash_defweak))
11820 {
bef26483 11821 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
11822 o = h->root.u.def.section;
11823 if (o->output_section != NULL)
bef26483 11824 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
11825 + o->output_offset);
11826 else
11827 {
11828 /* The symbol is imported from another shared
11829 library and does not apply to this one. */
bef26483 11830 dyn.d_un.d_ptr = 0;
c152c796
AM
11831 }
11832 break;
11833 }
11834 }
11835 continue;
11836
11837 case DT_PREINIT_ARRAYSZ:
11838 name = ".preinit_array";
11839 goto get_size;
11840 case DT_INIT_ARRAYSZ:
11841 name = ".init_array";
11842 goto get_size;
11843 case DT_FINI_ARRAYSZ:
11844 name = ".fini_array";
11845 get_size:
11846 o = bfd_get_section_by_name (abfd, name);
11847 if (o == NULL)
11848 {
11849 (*_bfd_error_handler)
d003868e 11850 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11851 goto error_return;
11852 }
eea6121a 11853 if (o->size == 0)
c152c796
AM
11854 (*_bfd_error_handler)
11855 (_("warning: %s section has zero size"), name);
eea6121a 11856 dyn.d_un.d_val = o->size;
c152c796
AM
11857 break;
11858
11859 case DT_PREINIT_ARRAY:
11860 name = ".preinit_array";
11861 goto get_vma;
11862 case DT_INIT_ARRAY:
11863 name = ".init_array";
11864 goto get_vma;
11865 case DT_FINI_ARRAY:
11866 name = ".fini_array";
11867 goto get_vma;
11868
11869 case DT_HASH:
11870 name = ".hash";
11871 goto get_vma;
fdc90cb4
JJ
11872 case DT_GNU_HASH:
11873 name = ".gnu.hash";
11874 goto get_vma;
c152c796
AM
11875 case DT_STRTAB:
11876 name = ".dynstr";
11877 goto get_vma;
11878 case DT_SYMTAB:
11879 name = ".dynsym";
11880 goto get_vma;
11881 case DT_VERDEF:
11882 name = ".gnu.version_d";
11883 goto get_vma;
11884 case DT_VERNEED:
11885 name = ".gnu.version_r";
11886 goto get_vma;
11887 case DT_VERSYM:
11888 name = ".gnu.version";
11889 get_vma:
11890 o = bfd_get_section_by_name (abfd, name);
11891 if (o == NULL)
11892 {
11893 (*_bfd_error_handler)
d003868e 11894 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
11895 goto error_return;
11896 }
894891db
NC
11897 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11898 {
11899 (*_bfd_error_handler)
11900 (_("warning: section '%s' is being made into a note"), name);
11901 bfd_set_error (bfd_error_nonrepresentable_section);
11902 goto error_return;
11903 }
c152c796
AM
11904 dyn.d_un.d_ptr = o->vma;
11905 break;
11906
11907 case DT_REL:
11908 case DT_RELA:
11909 case DT_RELSZ:
11910 case DT_RELASZ:
11911 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11912 type = SHT_REL;
11913 else
11914 type = SHT_RELA;
11915 dyn.d_un.d_val = 0;
bef26483 11916 dyn.d_un.d_ptr = 0;
c152c796
AM
11917 for (i = 1; i < elf_numsections (abfd); i++)
11918 {
11919 Elf_Internal_Shdr *hdr;
11920
11921 hdr = elf_elfsections (abfd)[i];
11922 if (hdr->sh_type == type
11923 && (hdr->sh_flags & SHF_ALLOC) != 0)
11924 {
11925 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11926 dyn.d_un.d_val += hdr->sh_size;
11927 else
11928 {
bef26483
AM
11929 if (dyn.d_un.d_ptr == 0
11930 || hdr->sh_addr < dyn.d_un.d_ptr)
11931 dyn.d_un.d_ptr = hdr->sh_addr;
c152c796
AM
11932 }
11933 }
11934 }
11935 break;
11936 }
11937 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11938 }
11939 }
11940
11941 /* If we have created any dynamic sections, then output them. */
11942 if (dynobj != NULL)
11943 {
11944 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11945 goto error_return;
11946
943284cc 11947 /* Check for DT_TEXTREL (late, in case the backend removes it). */
0e1862bb 11948 if (((info->warn_shared_textrel && bfd_link_pic (info))
be7b303d 11949 || info->error_textrel)
3d4d4302 11950 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
11951 {
11952 bfd_byte *dyncon, *dynconend;
11953
943284cc
DJ
11954 dyncon = o->contents;
11955 dynconend = o->contents + o->size;
11956 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11957 {
11958 Elf_Internal_Dyn dyn;
11959
11960 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11961
11962 if (dyn.d_tag == DT_TEXTREL)
11963 {
c192a133
AM
11964 if (info->error_textrel)
11965 info->callbacks->einfo
11966 (_("%P%X: read-only segment has dynamic relocations.\n"));
11967 else
11968 info->callbacks->einfo
11969 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
11970 break;
11971 }
11972 }
11973 }
11974
c152c796
AM
11975 for (o = dynobj->sections; o != NULL; o = o->next)
11976 {
11977 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11978 || o->size == 0
c152c796
AM
11979 || o->output_section == bfd_abs_section_ptr)
11980 continue;
11981 if ((o->flags & SEC_LINKER_CREATED) == 0)
11982 {
11983 /* At this point, we are only interested in sections
11984 created by _bfd_elf_link_create_dynamic_sections. */
11985 continue;
11986 }
3722b82f
AM
11987 if (elf_hash_table (info)->stab_info.stabstr == o)
11988 continue;
eea6121a
AM
11989 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11990 continue;
3d4d4302 11991 if (strcmp (o->name, ".dynstr") != 0)
c152c796
AM
11992 {
11993 if (! bfd_set_section_contents (abfd, o->output_section,
11994 o->contents,
37b01f6a
DG
11995 (file_ptr) o->output_offset
11996 * bfd_octets_per_byte (abfd),
eea6121a 11997 o->size))
c152c796
AM
11998 goto error_return;
11999 }
12000 else
12001 {
12002 /* The contents of the .dynstr section are actually in a
12003 stringtab. */
8539e4e8
AM
12004 file_ptr off;
12005
c152c796
AM
12006 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12007 if (bfd_seek (abfd, off, SEEK_SET) != 0
12008 || ! _bfd_elf_strtab_emit (abfd,
12009 elf_hash_table (info)->dynstr))
12010 goto error_return;
12011 }
12012 }
12013 }
12014
0e1862bb 12015 if (bfd_link_relocatable (info))
c152c796
AM
12016 {
12017 bfd_boolean failed = FALSE;
12018
12019 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12020 if (failed)
12021 goto error_return;
12022 }
12023
12024 /* If we have optimized stabs strings, output them. */
3722b82f 12025 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
12026 {
12027 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12028 goto error_return;
12029 }
12030
9f7c3e5e
AM
12031 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12032 goto error_return;
c152c796 12033
9f7c3e5e 12034 elf_final_link_free (abfd, &flinfo);
c152c796 12035
12bd6957 12036 elf_linker (abfd) = TRUE;
c152c796 12037
104d59d1
JM
12038 if (attr_section)
12039 {
a50b1753 12040 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 12041 if (contents == NULL)
d0f16d5e 12042 return FALSE; /* Bail out and fail. */
104d59d1
JM
12043 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12044 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12045 free (contents);
12046 }
12047
c152c796
AM
12048 return TRUE;
12049
12050 error_return:
9f7c3e5e 12051 elf_final_link_free (abfd, &flinfo);
c152c796
AM
12052 return FALSE;
12053}
12054\f
5241d853
RS
12055/* Initialize COOKIE for input bfd ABFD. */
12056
12057static bfd_boolean
12058init_reloc_cookie (struct elf_reloc_cookie *cookie,
12059 struct bfd_link_info *info, bfd *abfd)
12060{
12061 Elf_Internal_Shdr *symtab_hdr;
12062 const struct elf_backend_data *bed;
12063
12064 bed = get_elf_backend_data (abfd);
12065 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12066
12067 cookie->abfd = abfd;
12068 cookie->sym_hashes = elf_sym_hashes (abfd);
12069 cookie->bad_symtab = elf_bad_symtab (abfd);
12070 if (cookie->bad_symtab)
12071 {
12072 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12073 cookie->extsymoff = 0;
12074 }
12075 else
12076 {
12077 cookie->locsymcount = symtab_hdr->sh_info;
12078 cookie->extsymoff = symtab_hdr->sh_info;
12079 }
12080
12081 if (bed->s->arch_size == 32)
12082 cookie->r_sym_shift = 8;
12083 else
12084 cookie->r_sym_shift = 32;
12085
12086 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12087 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12088 {
12089 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12090 cookie->locsymcount, 0,
12091 NULL, NULL, NULL);
12092 if (cookie->locsyms == NULL)
12093 {
12094 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12095 return FALSE;
12096 }
12097 if (info->keep_memory)
12098 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12099 }
12100 return TRUE;
12101}
12102
12103/* Free the memory allocated by init_reloc_cookie, if appropriate. */
12104
12105static void
12106fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12107{
12108 Elf_Internal_Shdr *symtab_hdr;
12109
12110 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12111 if (cookie->locsyms != NULL
12112 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12113 free (cookie->locsyms);
12114}
12115
12116/* Initialize the relocation information in COOKIE for input section SEC
12117 of input bfd ABFD. */
12118
12119static bfd_boolean
12120init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12121 struct bfd_link_info *info, bfd *abfd,
12122 asection *sec)
12123{
12124 const struct elf_backend_data *bed;
12125
12126 if (sec->reloc_count == 0)
12127 {
12128 cookie->rels = NULL;
12129 cookie->relend = NULL;
12130 }
12131 else
12132 {
12133 bed = get_elf_backend_data (abfd);
12134
12135 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12136 info->keep_memory);
12137 if (cookie->rels == NULL)
12138 return FALSE;
12139 cookie->rel = cookie->rels;
12140 cookie->relend = (cookie->rels
12141 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12142 }
12143 cookie->rel = cookie->rels;
12144 return TRUE;
12145}
12146
12147/* Free the memory allocated by init_reloc_cookie_rels,
12148 if appropriate. */
12149
12150static void
12151fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12152 asection *sec)
12153{
12154 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12155 free (cookie->rels);
12156}
12157
12158/* Initialize the whole of COOKIE for input section SEC. */
12159
12160static bfd_boolean
12161init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12162 struct bfd_link_info *info,
12163 asection *sec)
12164{
12165 if (!init_reloc_cookie (cookie, info, sec->owner))
12166 goto error1;
12167 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12168 goto error2;
12169 return TRUE;
12170
12171 error2:
12172 fini_reloc_cookie (cookie, sec->owner);
12173 error1:
12174 return FALSE;
12175}
12176
12177/* Free the memory allocated by init_reloc_cookie_for_section,
12178 if appropriate. */
12179
12180static void
12181fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12182 asection *sec)
12183{
12184 fini_reloc_cookie_rels (cookie, sec);
12185 fini_reloc_cookie (cookie, sec->owner);
12186}
12187\f
c152c796
AM
12188/* Garbage collect unused sections. */
12189
07adf181
AM
12190/* Default gc_mark_hook. */
12191
12192asection *
12193_bfd_elf_gc_mark_hook (asection *sec,
12194 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12195 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12196 struct elf_link_hash_entry *h,
12197 Elf_Internal_Sym *sym)
12198{
12199 if (h != NULL)
12200 {
12201 switch (h->root.type)
12202 {
12203 case bfd_link_hash_defined:
12204 case bfd_link_hash_defweak:
12205 return h->root.u.def.section;
12206
12207 case bfd_link_hash_common:
12208 return h->root.u.c.p->section;
12209
12210 default:
12211 break;
12212 }
12213 }
12214 else
12215 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12216
12217 return NULL;
12218}
12219
5241d853
RS
12220/* COOKIE->rel describes a relocation against section SEC, which is
12221 a section we've decided to keep. Return the section that contains
12222 the relocation symbol, or NULL if no section contains it. */
12223
12224asection *
12225_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12226 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
12227 struct elf_reloc_cookie *cookie,
12228 bfd_boolean *start_stop)
5241d853
RS
12229{
12230 unsigned long r_symndx;
12231 struct elf_link_hash_entry *h;
12232
12233 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 12234 if (r_symndx == STN_UNDEF)
5241d853
RS
12235 return NULL;
12236
12237 if (r_symndx >= cookie->locsymcount
12238 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12239 {
12240 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
12241 if (h == NULL)
12242 {
12243 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12244 sec->owner);
12245 return NULL;
12246 }
5241d853
RS
12247 while (h->root.type == bfd_link_hash_indirect
12248 || h->root.type == bfd_link_hash_warning)
12249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 12250 h->mark = 1;
4e6b54a6
AM
12251 /* If this symbol is weak and there is a non-weak definition, we
12252 keep the non-weak definition because many backends put
12253 dynamic reloc info on the non-weak definition for code
12254 handling copy relocs. */
12255 if (h->u.weakdef != NULL)
12256 h->u.weakdef->mark = 1;
1cce69b9
AM
12257
12258 if (start_stop != NULL
12259 && (h->root.type == bfd_link_hash_undefined
12260 || h->root.type == bfd_link_hash_undefweak))
12261 {
12262 /* To work around a glibc bug, mark all XXX input sections
12263 when there is an as yet undefined reference to __start_XXX
12264 or __stop_XXX symbols. The linker will later define such
12265 symbols for orphan input sections that have a name
12266 representable as a C identifier. */
12267 const char *sec_name = NULL;
12268 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12269 sec_name = h->root.root.string + 8;
12270 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12271 sec_name = h->root.root.string + 7;
12272
12273 if (sec_name != NULL && *sec_name != '\0')
12274 {
12275 bfd *i;
12276
12277 for (i = info->input_bfds; i != NULL; i = i->link.next)
12278 {
12279 asection *s = bfd_get_section_by_name (i, sec_name);
12280 if (s != NULL && !s->gc_mark)
12281 {
12282 *start_stop = TRUE;
12283 return s;
12284 }
12285 }
12286 }
12287 }
12288
5241d853
RS
12289 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12290 }
12291
12292 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12293 &cookie->locsyms[r_symndx]);
12294}
12295
12296/* COOKIE->rel describes a relocation against section SEC, which is
12297 a section we've decided to keep. Mark the section that contains
9d0a14d3 12298 the relocation symbol. */
5241d853
RS
12299
12300bfd_boolean
12301_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12302 asection *sec,
12303 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 12304 struct elf_reloc_cookie *cookie)
5241d853
RS
12305{
12306 asection *rsec;
1cce69b9 12307 bfd_boolean start_stop = FALSE;
5241d853 12308
1cce69b9
AM
12309 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12310 while (rsec != NULL)
5241d853 12311 {
1cce69b9
AM
12312 if (!rsec->gc_mark)
12313 {
12314 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12315 || (rsec->owner->flags & DYNAMIC) != 0)
12316 rsec->gc_mark = 1;
12317 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12318 return FALSE;
12319 }
12320 if (!start_stop)
12321 break;
199af150 12322 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
12323 }
12324 return TRUE;
12325}
12326
07adf181
AM
12327/* The mark phase of garbage collection. For a given section, mark
12328 it and any sections in this section's group, and all the sections
12329 which define symbols to which it refers. */
12330
ccfa59ea
AM
12331bfd_boolean
12332_bfd_elf_gc_mark (struct bfd_link_info *info,
12333 asection *sec,
6a5bb875 12334 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
12335{
12336 bfd_boolean ret;
9d0a14d3 12337 asection *group_sec, *eh_frame;
c152c796
AM
12338
12339 sec->gc_mark = 1;
12340
12341 /* Mark all the sections in the group. */
12342 group_sec = elf_section_data (sec)->next_in_group;
12343 if (group_sec && !group_sec->gc_mark)
ccfa59ea 12344 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
12345 return FALSE;
12346
12347 /* Look through the section relocs. */
12348 ret = TRUE;
9d0a14d3
RS
12349 eh_frame = elf_eh_frame_section (sec->owner);
12350 if ((sec->flags & SEC_RELOC) != 0
12351 && sec->reloc_count > 0
12352 && sec != eh_frame)
c152c796 12353 {
5241d853 12354 struct elf_reloc_cookie cookie;
c152c796 12355
5241d853
RS
12356 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12357 ret = FALSE;
c152c796 12358 else
c152c796 12359 {
5241d853 12360 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 12361 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
12362 {
12363 ret = FALSE;
12364 break;
12365 }
12366 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
12367 }
12368 }
9d0a14d3
RS
12369
12370 if (ret && eh_frame && elf_fde_list (sec))
12371 {
12372 struct elf_reloc_cookie cookie;
12373
12374 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12375 ret = FALSE;
12376 else
12377 {
12378 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12379 gc_mark_hook, &cookie))
12380 ret = FALSE;
12381 fini_reloc_cookie_for_section (&cookie, eh_frame);
12382 }
12383 }
12384
2f0c68f2
CM
12385 eh_frame = elf_section_eh_frame_entry (sec);
12386 if (ret && eh_frame && !eh_frame->gc_mark)
12387 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12388 ret = FALSE;
12389
c152c796
AM
12390 return ret;
12391}
12392
3c758495
TG
12393/* Scan and mark sections in a special or debug section group. */
12394
12395static void
12396_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12397{
12398 /* Point to first section of section group. */
12399 asection *ssec;
12400 /* Used to iterate the section group. */
12401 asection *msec;
12402
12403 bfd_boolean is_special_grp = TRUE;
12404 bfd_boolean is_debug_grp = TRUE;
12405
12406 /* First scan to see if group contains any section other than debug
12407 and special section. */
12408 ssec = msec = elf_next_in_group (grp);
12409 do
12410 {
12411 if ((msec->flags & SEC_DEBUGGING) == 0)
12412 is_debug_grp = FALSE;
12413
12414 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12415 is_special_grp = FALSE;
12416
12417 msec = elf_next_in_group (msec);
12418 }
12419 while (msec != ssec);
12420
12421 /* If this is a pure debug section group or pure special section group,
12422 keep all sections in this group. */
12423 if (is_debug_grp || is_special_grp)
12424 {
12425 do
12426 {
12427 msec->gc_mark = 1;
12428 msec = elf_next_in_group (msec);
12429 }
12430 while (msec != ssec);
12431 }
12432}
12433
7f6ab9f8
AM
12434/* Keep debug and special sections. */
12435
12436bfd_boolean
12437_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12438 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12439{
12440 bfd *ibfd;
12441
c72f2fb2 12442 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
12443 {
12444 asection *isec;
12445 bfd_boolean some_kept;
b40bf0a2 12446 bfd_boolean debug_frag_seen;
7f6ab9f8
AM
12447
12448 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12449 continue;
12450
b40bf0a2
NC
12451 /* Ensure all linker created sections are kept,
12452 see if any other section is already marked,
12453 and note if we have any fragmented debug sections. */
12454 debug_frag_seen = some_kept = FALSE;
7f6ab9f8
AM
12455 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12456 {
12457 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12458 isec->gc_mark = 1;
12459 else if (isec->gc_mark)
12460 some_kept = TRUE;
b40bf0a2
NC
12461
12462 if (debug_frag_seen == FALSE
12463 && (isec->flags & SEC_DEBUGGING)
12464 && CONST_STRNEQ (isec->name, ".debug_line."))
12465 debug_frag_seen = TRUE;
7f6ab9f8
AM
12466 }
12467
12468 /* If no section in this file will be kept, then we can
b40bf0a2 12469 toss out the debug and special sections. */
7f6ab9f8
AM
12470 if (!some_kept)
12471 continue;
12472
12473 /* Keep debug and special sections like .comment when they are
3c758495
TG
12474 not part of a group. Also keep section groups that contain
12475 just debug sections or special sections. */
7f6ab9f8 12476 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
12477 {
12478 if ((isec->flags & SEC_GROUP) != 0)
12479 _bfd_elf_gc_mark_debug_special_section_group (isec);
12480 else if (((isec->flags & SEC_DEBUGGING) != 0
12481 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12482 && elf_next_in_group (isec) == NULL)
12483 isec->gc_mark = 1;
12484 }
b40bf0a2
NC
12485
12486 if (! debug_frag_seen)
12487 continue;
12488
12489 /* Look for CODE sections which are going to be discarded,
12490 and find and discard any fragmented debug sections which
12491 are associated with that code section. */
12492 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12493 if ((isec->flags & SEC_CODE) != 0
12494 && isec->gc_mark == 0)
12495 {
12496 unsigned int ilen;
12497 asection *dsec;
12498
12499 ilen = strlen (isec->name);
12500
12501 /* Association is determined by the name of the debug section
12502 containing the name of the code section as a suffix. For
12503 example .debug_line.text.foo is a debug section associated
12504 with .text.foo. */
12505 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12506 {
12507 unsigned int dlen;
12508
12509 if (dsec->gc_mark == 0
12510 || (dsec->flags & SEC_DEBUGGING) == 0)
12511 continue;
12512
12513 dlen = strlen (dsec->name);
12514
12515 if (dlen > ilen
12516 && strncmp (dsec->name + (dlen - ilen),
12517 isec->name, ilen) == 0)
12518 {
12519 dsec->gc_mark = 0;
b40bf0a2
NC
12520 }
12521 }
12522 }
7f6ab9f8
AM
12523 }
12524 return TRUE;
12525}
12526
c152c796
AM
12527/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12528
c17d87de
NC
12529struct elf_gc_sweep_symbol_info
12530{
ccabcbe5
AM
12531 struct bfd_link_info *info;
12532 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12533 bfd_boolean);
12534};
12535
c152c796 12536static bfd_boolean
ccabcbe5 12537elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 12538{
1d5316ab
AM
12539 if (!h->mark
12540 && (((h->root.type == bfd_link_hash_defined
12541 || h->root.type == bfd_link_hash_defweak)
c4621b33 12542 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6673f753 12543 && h->root.u.def.section->gc_mark))
1d5316ab
AM
12544 || h->root.type == bfd_link_hash_undefined
12545 || h->root.type == bfd_link_hash_undefweak))
12546 {
12547 struct elf_gc_sweep_symbol_info *inf;
12548
12549 inf = (struct elf_gc_sweep_symbol_info *) data;
ccabcbe5 12550 (*inf->hide_symbol) (inf->info, h, TRUE);
1d5316ab
AM
12551 h->def_regular = 0;
12552 h->ref_regular = 0;
12553 h->ref_regular_nonweak = 0;
ccabcbe5 12554 }
c152c796
AM
12555
12556 return TRUE;
12557}
12558
12559/* The sweep phase of garbage collection. Remove all garbage sections. */
12560
12561typedef bfd_boolean (*gc_sweep_hook_fn)
12562 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12563
12564static bfd_boolean
ccabcbe5 12565elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
12566{
12567 bfd *sub;
ccabcbe5
AM
12568 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12569 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12570 unsigned long section_sym_count;
12571 struct elf_gc_sweep_symbol_info sweep_info;
c152c796 12572
c72f2fb2 12573 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12574 {
12575 asection *o;
12576
b19a8f85
L
12577 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12578 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12579 continue;
12580
12581 for (o = sub->sections; o != NULL; o = o->next)
12582 {
a33dafc3
L
12583 /* When any section in a section group is kept, we keep all
12584 sections in the section group. If the first member of
12585 the section group is excluded, we will also exclude the
12586 group section. */
12587 if (o->flags & SEC_GROUP)
12588 {
12589 asection *first = elf_next_in_group (o);
12590 o->gc_mark = first->gc_mark;
12591 }
c152c796 12592
1e7eae0d 12593 if (o->gc_mark)
c152c796
AM
12594 continue;
12595
12596 /* Skip sweeping sections already excluded. */
12597 if (o->flags & SEC_EXCLUDE)
12598 continue;
12599
12600 /* Since this is early in the link process, it is simple
12601 to remove a section from the output. */
12602 o->flags |= SEC_EXCLUDE;
12603
c55fe096 12604 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
12605 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12606
c152c796
AM
12607 /* But we also have to update some of the relocation
12608 info we collected before. */
12609 if (gc_sweep_hook
e8aaee2a 12610 && (o->flags & SEC_RELOC) != 0
9850436d
AM
12611 && o->reloc_count != 0
12612 && !((info->strip == strip_all || info->strip == strip_debugger)
12613 && (o->flags & SEC_DEBUGGING) != 0)
e8aaee2a 12614 && !bfd_is_abs_section (o->output_section))
c152c796
AM
12615 {
12616 Elf_Internal_Rela *internal_relocs;
12617 bfd_boolean r;
12618
12619 internal_relocs
12620 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12621 info->keep_memory);
12622 if (internal_relocs == NULL)
12623 return FALSE;
12624
12625 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12626
12627 if (elf_section_data (o)->relocs != internal_relocs)
12628 free (internal_relocs);
12629
12630 if (!r)
12631 return FALSE;
12632 }
12633 }
12634 }
12635
12636 /* Remove the symbols that were in the swept sections from the dynamic
12637 symbol table. GCFIXME: Anyone know how to get them out of the
12638 static symbol table as well? */
ccabcbe5
AM
12639 sweep_info.info = info;
12640 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12641 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12642 &sweep_info);
c152c796 12643
ccabcbe5 12644 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
12645 return TRUE;
12646}
12647
12648/* Propagate collected vtable information. This is called through
12649 elf_link_hash_traverse. */
12650
12651static bfd_boolean
12652elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12653{
c152c796 12654 /* Those that are not vtables. */
f6e332e6 12655 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12656 return TRUE;
12657
12658 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 12659 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
12660 return TRUE;
12661
12662 /* If we've already been done, exit. */
f6e332e6 12663 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
12664 return TRUE;
12665
12666 /* Make sure the parent's table is up to date. */
f6e332e6 12667 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 12668
f6e332e6 12669 if (h->vtable->used == NULL)
c152c796
AM
12670 {
12671 /* None of this table's entries were referenced. Re-use the
12672 parent's table. */
f6e332e6
AM
12673 h->vtable->used = h->vtable->parent->vtable->used;
12674 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
12675 }
12676 else
12677 {
12678 size_t n;
12679 bfd_boolean *cu, *pu;
12680
12681 /* Or the parent's entries into ours. */
f6e332e6 12682 cu = h->vtable->used;
c152c796 12683 cu[-1] = TRUE;
f6e332e6 12684 pu = h->vtable->parent->vtable->used;
c152c796
AM
12685 if (pu != NULL)
12686 {
12687 const struct elf_backend_data *bed;
12688 unsigned int log_file_align;
12689
12690 bed = get_elf_backend_data (h->root.u.def.section->owner);
12691 log_file_align = bed->s->log_file_align;
f6e332e6 12692 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
12693 while (n--)
12694 {
12695 if (*pu)
12696 *cu = TRUE;
12697 pu++;
12698 cu++;
12699 }
12700 }
12701 }
12702
12703 return TRUE;
12704}
12705
12706static bfd_boolean
12707elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12708{
12709 asection *sec;
12710 bfd_vma hstart, hend;
12711 Elf_Internal_Rela *relstart, *relend, *rel;
12712 const struct elf_backend_data *bed;
12713 unsigned int log_file_align;
12714
c152c796
AM
12715 /* Take care of both those symbols that do not describe vtables as
12716 well as those that are not loaded. */
f6e332e6 12717 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
12718 return TRUE;
12719
12720 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12721 || h->root.type == bfd_link_hash_defweak);
12722
12723 sec = h->root.u.def.section;
12724 hstart = h->root.u.def.value;
12725 hend = hstart + h->size;
12726
12727 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12728 if (!relstart)
12729 return *(bfd_boolean *) okp = FALSE;
12730 bed = get_elf_backend_data (sec->owner);
12731 log_file_align = bed->s->log_file_align;
12732
12733 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12734
12735 for (rel = relstart; rel < relend; ++rel)
12736 if (rel->r_offset >= hstart && rel->r_offset < hend)
12737 {
12738 /* If the entry is in use, do nothing. */
f6e332e6
AM
12739 if (h->vtable->used
12740 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
12741 {
12742 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 12743 if (h->vtable->used[entry])
c152c796
AM
12744 continue;
12745 }
12746 /* Otherwise, kill it. */
12747 rel->r_offset = rel->r_info = rel->r_addend = 0;
12748 }
12749
12750 return TRUE;
12751}
12752
87538722
AM
12753/* Mark sections containing dynamically referenced symbols. When
12754 building shared libraries, we must assume that any visible symbol is
12755 referenced. */
715df9b8 12756
64d03ab5
AM
12757bfd_boolean
12758bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 12759{
87538722 12760 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 12761 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 12762
715df9b8
EB
12763 if ((h->root.type == bfd_link_hash_defined
12764 || h->root.type == bfd_link_hash_defweak)
87538722 12765 && (h->ref_dynamic
c4621b33 12766 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 12767 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 12768 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 12769 && (!bfd_link_executable (info)
b407645f
AM
12770 || info->export_dynamic
12771 || (h->dynamic
12772 && d != NULL
12773 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 12774 && (h->versioned >= versioned
54e8959c
L
12775 || !bfd_hide_sym_by_version (info->version_info,
12776 h->root.root.string)))))
715df9b8
EB
12777 h->root.u.def.section->flags |= SEC_KEEP;
12778
12779 return TRUE;
12780}
3b36f7e6 12781
74f0fb50
AM
12782/* Keep all sections containing symbols undefined on the command-line,
12783 and the section containing the entry symbol. */
12784
12785void
12786_bfd_elf_gc_keep (struct bfd_link_info *info)
12787{
12788 struct bfd_sym_chain *sym;
12789
12790 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12791 {
12792 struct elf_link_hash_entry *h;
12793
12794 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12795 FALSE, FALSE, FALSE);
12796
12797 if (h != NULL
12798 && (h->root.type == bfd_link_hash_defined
12799 || h->root.type == bfd_link_hash_defweak)
12800 && !bfd_is_abs_section (h->root.u.def.section))
12801 h->root.u.def.section->flags |= SEC_KEEP;
12802 }
12803}
12804
2f0c68f2
CM
12805bfd_boolean
12806bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12807 struct bfd_link_info *info)
12808{
12809 bfd *ibfd = info->input_bfds;
12810
12811 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12812 {
12813 asection *sec;
12814 struct elf_reloc_cookie cookie;
12815
12816 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12817 continue;
12818
12819 if (!init_reloc_cookie (&cookie, info, ibfd))
12820 return FALSE;
12821
12822 for (sec = ibfd->sections; sec; sec = sec->next)
12823 {
12824 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12825 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12826 {
12827 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12828 fini_reloc_cookie_rels (&cookie, sec);
12829 }
12830 }
12831 }
12832 return TRUE;
12833}
12834
c152c796
AM
12835/* Do mark and sweep of unused sections. */
12836
12837bfd_boolean
12838bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12839{
12840 bfd_boolean ok = TRUE;
12841 bfd *sub;
6a5bb875 12842 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 12843 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 12844 struct elf_link_hash_table *htab;
c152c796 12845
64d03ab5 12846 if (!bed->can_gc_sections
715df9b8 12847 || !is_elf_hash_table (info->hash))
c152c796
AM
12848 {
12849 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12850 return TRUE;
12851 }
12852
74f0fb50 12853 bed->gc_keep (info);
da44f4e5 12854 htab = elf_hash_table (info);
74f0fb50 12855
9d0a14d3
RS
12856 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12857 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
12858 for (sub = info->input_bfds;
12859 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12860 sub = sub->link.next)
9d0a14d3
RS
12861 {
12862 asection *sec;
12863 struct elf_reloc_cookie cookie;
12864
12865 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 12866 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
12867 {
12868 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
12869 if (elf_section_data (sec)->sec_info
12870 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
12871 elf_eh_frame_section (sub) = sec;
12872 fini_reloc_cookie_for_section (&cookie, sec);
199af150 12873 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
12874 }
12875 }
9d0a14d3 12876
c152c796 12877 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 12878 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
12879 if (!ok)
12880 return FALSE;
12881
12882 /* Kill the vtable relocations that were not used. */
da44f4e5 12883 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
12884 if (!ok)
12885 return FALSE;
12886
715df9b8 12887 /* Mark dynamically referenced symbols. */
da44f4e5
AM
12888 if (htab->dynamic_sections_created)
12889 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 12890
715df9b8 12891 /* Grovel through relocs to find out who stays ... */
64d03ab5 12892 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 12893 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12894 {
12895 asection *o;
12896
b19a8f85
L
12897 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12898 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
12899 continue;
12900
7f6ab9f8
AM
12901 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12902 Also treat note sections as a root, if the section is not part
12903 of a group. */
c152c796 12904 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
12905 if (!o->gc_mark
12906 && (o->flags & SEC_EXCLUDE) == 0
24007750 12907 && ((o->flags & SEC_KEEP) != 0
7f6ab9f8
AM
12908 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12909 && elf_next_in_group (o) == NULL )))
12910 {
12911 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12912 return FALSE;
12913 }
c152c796
AM
12914 }
12915
6a5bb875 12916 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 12917 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 12918
c152c796 12919 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 12920 return elf_gc_sweep (abfd, info);
c152c796
AM
12921}
12922\f
12923/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12924
12925bfd_boolean
12926bfd_elf_gc_record_vtinherit (bfd *abfd,
12927 asection *sec,
12928 struct elf_link_hash_entry *h,
12929 bfd_vma offset)
12930{
12931 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12932 struct elf_link_hash_entry **search, *child;
12933 bfd_size_type extsymcount;
12934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12935
12936 /* The sh_info field of the symtab header tells us where the
12937 external symbols start. We don't care about the local symbols at
12938 this point. */
12939 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12940 if (!elf_bad_symtab (abfd))
12941 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12942
12943 sym_hashes = elf_sym_hashes (abfd);
12944 sym_hashes_end = sym_hashes + extsymcount;
12945
12946 /* Hunt down the child symbol, which is in this section at the same
12947 offset as the relocation. */
12948 for (search = sym_hashes; search != sym_hashes_end; ++search)
12949 {
12950 if ((child = *search) != NULL
12951 && (child->root.type == bfd_link_hash_defined
12952 || child->root.type == bfd_link_hash_defweak)
12953 && child->root.u.def.section == sec
12954 && child->root.u.def.value == offset)
12955 goto win;
12956 }
12957
d003868e
AM
12958 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12959 abfd, sec, (unsigned long) offset);
c152c796
AM
12960 bfd_set_error (bfd_error_invalid_operation);
12961 return FALSE;
12962
12963 win:
f6e332e6
AM
12964 if (!child->vtable)
12965 {
ca4be51c
AM
12966 child->vtable = ((struct elf_link_virtual_table_entry *)
12967 bfd_zalloc (abfd, sizeof (*child->vtable)));
f6e332e6
AM
12968 if (!child->vtable)
12969 return FALSE;
12970 }
c152c796
AM
12971 if (!h)
12972 {
12973 /* This *should* only be the absolute section. It could potentially
12974 be that someone has defined a non-global vtable though, which
12975 would be bad. It isn't worth paging in the local symbols to be
12976 sure though; that case should simply be handled by the assembler. */
12977
f6e332e6 12978 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
12979 }
12980 else
f6e332e6 12981 child->vtable->parent = h;
c152c796
AM
12982
12983 return TRUE;
12984}
12985
12986/* Called from check_relocs to record the existence of a VTENTRY reloc. */
12987
12988bfd_boolean
12989bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12990 asection *sec ATTRIBUTE_UNUSED,
12991 struct elf_link_hash_entry *h,
12992 bfd_vma addend)
12993{
12994 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12995 unsigned int log_file_align = bed->s->log_file_align;
12996
f6e332e6
AM
12997 if (!h->vtable)
12998 {
ca4be51c
AM
12999 h->vtable = ((struct elf_link_virtual_table_entry *)
13000 bfd_zalloc (abfd, sizeof (*h->vtable)));
f6e332e6
AM
13001 if (!h->vtable)
13002 return FALSE;
13003 }
13004
13005 if (addend >= h->vtable->size)
c152c796
AM
13006 {
13007 size_t size, bytes, file_align;
f6e332e6 13008 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
13009
13010 /* While the symbol is undefined, we have to be prepared to handle
13011 a zero size. */
13012 file_align = 1 << log_file_align;
13013 if (h->root.type == bfd_link_hash_undefined)
13014 size = addend + file_align;
13015 else
13016 {
13017 size = h->size;
13018 if (addend >= size)
13019 {
13020 /* Oops! We've got a reference past the defined end of
13021 the table. This is probably a bug -- shall we warn? */
13022 size = addend + file_align;
13023 }
13024 }
13025 size = (size + file_align - 1) & -file_align;
13026
13027 /* Allocate one extra entry for use as a "done" flag for the
13028 consolidation pass. */
13029 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13030
13031 if (ptr)
13032 {
a50b1753 13033 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
13034
13035 if (ptr != NULL)
13036 {
13037 size_t oldbytes;
13038
f6e332e6 13039 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
13040 * sizeof (bfd_boolean));
13041 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13042 }
13043 }
13044 else
a50b1753 13045 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
13046
13047 if (ptr == NULL)
13048 return FALSE;
13049
13050 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
13051 h->vtable->used = ptr + 1;
13052 h->vtable->size = size;
c152c796
AM
13053 }
13054
f6e332e6 13055 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
13056
13057 return TRUE;
13058}
13059
ae17ab41
CM
13060/* Map an ELF section header flag to its corresponding string. */
13061typedef struct
13062{
13063 char *flag_name;
13064 flagword flag_value;
13065} elf_flags_to_name_table;
13066
13067static elf_flags_to_name_table elf_flags_to_names [] =
13068{
13069 { "SHF_WRITE", SHF_WRITE },
13070 { "SHF_ALLOC", SHF_ALLOC },
13071 { "SHF_EXECINSTR", SHF_EXECINSTR },
13072 { "SHF_MERGE", SHF_MERGE },
13073 { "SHF_STRINGS", SHF_STRINGS },
13074 { "SHF_INFO_LINK", SHF_INFO_LINK},
13075 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13076 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13077 { "SHF_GROUP", SHF_GROUP },
13078 { "SHF_TLS", SHF_TLS },
13079 { "SHF_MASKOS", SHF_MASKOS },
13080 { "SHF_EXCLUDE", SHF_EXCLUDE },
13081};
13082
b9c361e0
JL
13083/* Returns TRUE if the section is to be included, otherwise FALSE. */
13084bfd_boolean
ae17ab41 13085bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 13086 struct flag_info *flaginfo,
b9c361e0 13087 asection *section)
ae17ab41 13088{
8b127cbc 13089 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 13090
8b127cbc 13091 if (!flaginfo->flags_initialized)
ae17ab41 13092 {
8b127cbc
AM
13093 bfd *obfd = info->output_bfd;
13094 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13095 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
13096 int with_hex = 0;
13097 int without_hex = 0;
13098
8b127cbc 13099 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 13100 {
b9c361e0 13101 unsigned i;
8b127cbc 13102 flagword (*lookup) (char *);
ae17ab41 13103
8b127cbc
AM
13104 lookup = bed->elf_backend_lookup_section_flags_hook;
13105 if (lookup != NULL)
ae17ab41 13106 {
8b127cbc 13107 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
13108
13109 if (hexval != 0)
13110 {
13111 if (tf->with == with_flags)
13112 with_hex |= hexval;
13113 else if (tf->with == without_flags)
13114 without_hex |= hexval;
13115 tf->valid = TRUE;
13116 continue;
13117 }
ae17ab41 13118 }
8b127cbc 13119 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 13120 {
8b127cbc 13121 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
13122 {
13123 if (tf->with == with_flags)
13124 with_hex |= elf_flags_to_names[i].flag_value;
13125 else if (tf->with == without_flags)
13126 without_hex |= elf_flags_to_names[i].flag_value;
13127 tf->valid = TRUE;
13128 break;
13129 }
13130 }
8b127cbc 13131 if (!tf->valid)
b9c361e0 13132 {
68ffbac6 13133 info->callbacks->einfo
8b127cbc 13134 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 13135 return FALSE;
ae17ab41
CM
13136 }
13137 }
8b127cbc
AM
13138 flaginfo->flags_initialized = TRUE;
13139 flaginfo->only_with_flags |= with_hex;
13140 flaginfo->not_with_flags |= without_hex;
ae17ab41 13141 }
ae17ab41 13142
8b127cbc 13143 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
13144 return FALSE;
13145
8b127cbc 13146 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
13147 return FALSE;
13148
13149 return TRUE;
ae17ab41
CM
13150}
13151
c152c796
AM
13152struct alloc_got_off_arg {
13153 bfd_vma gotoff;
10455f89 13154 struct bfd_link_info *info;
c152c796
AM
13155};
13156
13157/* We need a special top-level link routine to convert got reference counts
13158 to real got offsets. */
13159
13160static bfd_boolean
13161elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13162{
a50b1753 13163 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
13164 bfd *obfd = gofarg->info->output_bfd;
13165 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 13166
c152c796
AM
13167 if (h->got.refcount > 0)
13168 {
13169 h->got.offset = gofarg->gotoff;
10455f89 13170 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
13171 }
13172 else
13173 h->got.offset = (bfd_vma) -1;
13174
13175 return TRUE;
13176}
13177
13178/* And an accompanying bit to work out final got entry offsets once
13179 we're done. Should be called from final_link. */
13180
13181bfd_boolean
13182bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13183 struct bfd_link_info *info)
13184{
13185 bfd *i;
13186 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13187 bfd_vma gotoff;
c152c796
AM
13188 struct alloc_got_off_arg gofarg;
13189
10455f89
HPN
13190 BFD_ASSERT (abfd == info->output_bfd);
13191
c152c796
AM
13192 if (! is_elf_hash_table (info->hash))
13193 return FALSE;
13194
13195 /* The GOT offset is relative to the .got section, but the GOT header is
13196 put into the .got.plt section, if the backend uses it. */
13197 if (bed->want_got_plt)
13198 gotoff = 0;
13199 else
13200 gotoff = bed->got_header_size;
13201
13202 /* Do the local .got entries first. */
c72f2fb2 13203 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
13204 {
13205 bfd_signed_vma *local_got;
13206 bfd_size_type j, locsymcount;
13207 Elf_Internal_Shdr *symtab_hdr;
13208
13209 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13210 continue;
13211
13212 local_got = elf_local_got_refcounts (i);
13213 if (!local_got)
13214 continue;
13215
13216 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13217 if (elf_bad_symtab (i))
13218 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13219 else
13220 locsymcount = symtab_hdr->sh_info;
13221
13222 for (j = 0; j < locsymcount; ++j)
13223 {
13224 if (local_got[j] > 0)
13225 {
13226 local_got[j] = gotoff;
10455f89 13227 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
13228 }
13229 else
13230 local_got[j] = (bfd_vma) -1;
13231 }
13232 }
13233
13234 /* Then the global .got entries. .plt refcounts are handled by
13235 adjust_dynamic_symbol */
13236 gofarg.gotoff = gotoff;
10455f89 13237 gofarg.info = info;
c152c796
AM
13238 elf_link_hash_traverse (elf_hash_table (info),
13239 elf_gc_allocate_got_offsets,
13240 &gofarg);
13241 return TRUE;
13242}
13243
13244/* Many folk need no more in the way of final link than this, once
13245 got entry reference counting is enabled. */
13246
13247bfd_boolean
13248bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13249{
13250 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13251 return FALSE;
13252
13253 /* Invoke the regular ELF backend linker to do all the work. */
13254 return bfd_elf_final_link (abfd, info);
13255}
13256
13257bfd_boolean
13258bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13259{
a50b1753 13260 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
13261
13262 if (rcookie->bad_symtab)
13263 rcookie->rel = rcookie->rels;
13264
13265 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13266 {
13267 unsigned long r_symndx;
13268
13269 if (! rcookie->bad_symtab)
13270 if (rcookie->rel->r_offset > offset)
13271 return FALSE;
13272 if (rcookie->rel->r_offset != offset)
13273 continue;
13274
13275 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 13276 if (r_symndx == STN_UNDEF)
c152c796
AM
13277 return TRUE;
13278
13279 if (r_symndx >= rcookie->locsymcount
13280 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13281 {
13282 struct elf_link_hash_entry *h;
13283
13284 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13285
13286 while (h->root.type == bfd_link_hash_indirect
13287 || h->root.type == bfd_link_hash_warning)
13288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13289
13290 if ((h->root.type == bfd_link_hash_defined
13291 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
13292 && (h->root.u.def.section->owner != rcookie->abfd
13293 || h->root.u.def.section->kept_section != NULL
13294 || discarded_section (h->root.u.def.section)))
c152c796 13295 return TRUE;
c152c796
AM
13296 }
13297 else
13298 {
13299 /* It's not a relocation against a global symbol,
13300 but it could be a relocation against a local
13301 symbol for a discarded section. */
13302 asection *isec;
13303 Elf_Internal_Sym *isym;
13304
13305 /* Need to: get the symbol; get the section. */
13306 isym = &rcookie->locsyms[r_symndx];
cb33740c 13307 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
13308 if (isec != NULL
13309 && (isec->kept_section != NULL
13310 || discarded_section (isec)))
cb33740c 13311 return TRUE;
c152c796
AM
13312 }
13313 return FALSE;
13314 }
13315 return FALSE;
13316}
13317
13318/* Discard unneeded references to discarded sections.
75938853
AM
13319 Returns -1 on error, 1 if any section's size was changed, 0 if
13320 nothing changed. This function assumes that the relocations are in
13321 sorted order, which is true for all known assemblers. */
c152c796 13322
75938853 13323int
c152c796
AM
13324bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13325{
13326 struct elf_reloc_cookie cookie;
18cd5bce 13327 asection *o;
c152c796 13328 bfd *abfd;
75938853 13329 int changed = 0;
c152c796
AM
13330
13331 if (info->traditional_format
13332 || !is_elf_hash_table (info->hash))
75938853 13333 return 0;
c152c796 13334
18cd5bce
AM
13335 o = bfd_get_section_by_name (output_bfd, ".stab");
13336 if (o != NULL)
c152c796 13337 {
18cd5bce 13338 asection *i;
c152c796 13339
18cd5bce 13340 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 13341 {
18cd5bce
AM
13342 if (i->size == 0
13343 || i->reloc_count == 0
13344 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13345 continue;
c152c796 13346
18cd5bce
AM
13347 abfd = i->owner;
13348 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13349 continue;
c152c796 13350
18cd5bce 13351 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13352 return -1;
c152c796 13353
18cd5bce
AM
13354 if (_bfd_discard_section_stabs (abfd, i,
13355 elf_section_data (i)->sec_info,
5241d853
RS
13356 bfd_elf_reloc_symbol_deleted_p,
13357 &cookie))
75938853 13358 changed = 1;
18cd5bce
AM
13359
13360 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13361 }
18cd5bce
AM
13362 }
13363
2f0c68f2
CM
13364 o = NULL;
13365 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13366 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
13367 if (o != NULL)
13368 {
13369 asection *i;
c152c796 13370
18cd5bce 13371 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 13372 {
18cd5bce
AM
13373 if (i->size == 0)
13374 continue;
13375
13376 abfd = i->owner;
13377 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13378 continue;
13379
13380 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 13381 return -1;
18cd5bce
AM
13382
13383 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13384 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
13385 bfd_elf_reloc_symbol_deleted_p,
13386 &cookie))
75938853 13387 changed = 1;
18cd5bce
AM
13388
13389 fini_reloc_cookie_for_section (&cookie, i);
c152c796 13390 }
18cd5bce 13391 }
c152c796 13392
18cd5bce
AM
13393 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13394 {
13395 const struct elf_backend_data *bed;
c152c796 13396
18cd5bce
AM
13397 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13398 continue;
13399
13400 bed = get_elf_backend_data (abfd);
13401
13402 if (bed->elf_backend_discard_info != NULL)
13403 {
13404 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 13405 return -1;
18cd5bce
AM
13406
13407 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 13408 changed = 1;
18cd5bce
AM
13409
13410 fini_reloc_cookie (&cookie, abfd);
13411 }
c152c796
AM
13412 }
13413
2f0c68f2
CM
13414 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13415 _bfd_elf_end_eh_frame_parsing (info);
13416
13417 if (info->eh_frame_hdr_type
0e1862bb 13418 && !bfd_link_relocatable (info)
c152c796 13419 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 13420 changed = 1;
c152c796 13421
75938853 13422 return changed;
c152c796 13423}
082b7297 13424
43e1669b 13425bfd_boolean
0c511000 13426_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 13427 asection *sec,
c0f00686 13428 struct bfd_link_info *info)
082b7297
L
13429{
13430 flagword flags;
c77ec726 13431 const char *name, *key;
082b7297
L
13432 struct bfd_section_already_linked *l;
13433 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 13434
c77ec726
AM
13435 if (sec->output_section == bfd_abs_section_ptr)
13436 return FALSE;
0c511000 13437
c77ec726 13438 flags = sec->flags;
0c511000 13439
c77ec726
AM
13440 /* Return if it isn't a linkonce section. A comdat group section
13441 also has SEC_LINK_ONCE set. */
13442 if ((flags & SEC_LINK_ONCE) == 0)
13443 return FALSE;
0c511000 13444
c77ec726
AM
13445 /* Don't put group member sections on our list of already linked
13446 sections. They are handled as a group via their group section. */
13447 if (elf_sec_group (sec) != NULL)
13448 return FALSE;
0c511000 13449
c77ec726
AM
13450 /* For a SHT_GROUP section, use the group signature as the key. */
13451 name = sec->name;
13452 if ((flags & SEC_GROUP) != 0
13453 && elf_next_in_group (sec) != NULL
13454 && elf_group_name (elf_next_in_group (sec)) != NULL)
13455 key = elf_group_name (elf_next_in_group (sec));
13456 else
13457 {
13458 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 13459 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
13460 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13461 key++;
0c511000 13462 else
c77ec726
AM
13463 /* Must be a user linkonce section that doesn't follow gcc's
13464 naming convention. In this case we won't be matching
13465 single member groups. */
13466 key = name;
0c511000 13467 }
6d2cd210 13468
c77ec726 13469 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
13470
13471 for (l = already_linked_list->entry; l != NULL; l = l->next)
13472 {
c2370991 13473 /* We may have 2 different types of sections on the list: group
c77ec726
AM
13474 sections with a signature of <key> (<key> is some string),
13475 and linkonce sections named .gnu.linkonce.<type>.<key>.
13476 Match like sections. LTO plugin sections are an exception.
13477 They are always named .gnu.linkonce.t.<key> and match either
13478 type of section. */
13479 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13480 && ((flags & SEC_GROUP) != 0
13481 || strcmp (name, l->sec->name) == 0))
13482 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
13483 {
13484 /* The section has already been linked. See if we should
6d2cd210 13485 issue a warning. */
c77ec726
AM
13486 if (!_bfd_handle_already_linked (sec, l, info))
13487 return FALSE;
082b7297 13488
c77ec726 13489 if (flags & SEC_GROUP)
3d7f7666 13490 {
c77ec726
AM
13491 asection *first = elf_next_in_group (sec);
13492 asection *s = first;
3d7f7666 13493
c77ec726 13494 while (s != NULL)
3d7f7666 13495 {
c77ec726
AM
13496 s->output_section = bfd_abs_section_ptr;
13497 /* Record which group discards it. */
13498 s->kept_section = l->sec;
13499 s = elf_next_in_group (s);
13500 /* These lists are circular. */
13501 if (s == first)
13502 break;
3d7f7666
L
13503 }
13504 }
082b7297 13505
43e1669b 13506 return TRUE;
082b7297
L
13507 }
13508 }
13509
c77ec726
AM
13510 /* A single member comdat group section may be discarded by a
13511 linkonce section and vice versa. */
13512 if ((flags & SEC_GROUP) != 0)
3d7f7666 13513 {
c77ec726 13514 asection *first = elf_next_in_group (sec);
c2370991 13515
c77ec726
AM
13516 if (first != NULL && elf_next_in_group (first) == first)
13517 /* Check this single member group against linkonce sections. */
13518 for (l = already_linked_list->entry; l != NULL; l = l->next)
13519 if ((l->sec->flags & SEC_GROUP) == 0
13520 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13521 {
13522 first->output_section = bfd_abs_section_ptr;
13523 first->kept_section = l->sec;
13524 sec->output_section = bfd_abs_section_ptr;
13525 break;
13526 }
13527 }
13528 else
13529 /* Check this linkonce section against single member groups. */
13530 for (l = already_linked_list->entry; l != NULL; l = l->next)
13531 if (l->sec->flags & SEC_GROUP)
6d2cd210 13532 {
c77ec726 13533 asection *first = elf_next_in_group (l->sec);
6d2cd210 13534
c77ec726
AM
13535 if (first != NULL
13536 && elf_next_in_group (first) == first
13537 && bfd_elf_match_symbols_in_sections (first, sec, info))
13538 {
13539 sec->output_section = bfd_abs_section_ptr;
13540 sec->kept_section = first;
13541 break;
13542 }
6d2cd210 13543 }
0c511000 13544
c77ec726
AM
13545 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13546 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13547 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13548 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13549 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13550 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13551 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13552 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13553 The reverse order cannot happen as there is never a bfd with only the
13554 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13555 matter as here were are looking only for cross-bfd sections. */
13556
13557 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13558 for (l = already_linked_list->entry; l != NULL; l = l->next)
13559 if ((l->sec->flags & SEC_GROUP) == 0
13560 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13561 {
13562 if (abfd != l->sec->owner)
13563 sec->output_section = bfd_abs_section_ptr;
13564 break;
13565 }
80c29487 13566
082b7297 13567 /* This is the first section with this name. Record it. */
c77ec726 13568 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 13569 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 13570 return sec->output_section == bfd_abs_section_ptr;
082b7297 13571}
81e1b023 13572
a4d8e49b
L
13573bfd_boolean
13574_bfd_elf_common_definition (Elf_Internal_Sym *sym)
13575{
13576 return sym->st_shndx == SHN_COMMON;
13577}
13578
13579unsigned int
13580_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13581{
13582 return SHN_COMMON;
13583}
13584
13585asection *
13586_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13587{
13588 return bfd_com_section_ptr;
13589}
10455f89
HPN
13590
13591bfd_vma
13592_bfd_elf_default_got_elt_size (bfd *abfd,
13593 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13594 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13595 bfd *ibfd ATTRIBUTE_UNUSED,
13596 unsigned long symndx ATTRIBUTE_UNUSED)
13597{
13598 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13599 return bed->s->arch_size / 8;
13600}
83bac4b0
NC
13601
13602/* Routines to support the creation of dynamic relocs. */
13603
83bac4b0
NC
13604/* Returns the name of the dynamic reloc section associated with SEC. */
13605
13606static const char *
13607get_dynamic_reloc_section_name (bfd * abfd,
13608 asection * sec,
13609 bfd_boolean is_rela)
13610{
ddcf1fcf
BS
13611 char *name;
13612 const char *old_name = bfd_get_section_name (NULL, sec);
13613 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 13614
ddcf1fcf 13615 if (old_name == NULL)
83bac4b0
NC
13616 return NULL;
13617
ddcf1fcf 13618 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 13619 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
13620
13621 return name;
13622}
13623
13624/* Returns the dynamic reloc section associated with SEC.
13625 If necessary compute the name of the dynamic reloc section based
13626 on SEC's name (looked up in ABFD's string table) and the setting
13627 of IS_RELA. */
13628
13629asection *
13630_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13631 asection * sec,
13632 bfd_boolean is_rela)
13633{
13634 asection * reloc_sec = elf_section_data (sec)->sreloc;
13635
13636 if (reloc_sec == NULL)
13637 {
13638 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13639
13640 if (name != NULL)
13641 {
3d4d4302 13642 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
13643
13644 if (reloc_sec != NULL)
13645 elf_section_data (sec)->sreloc = reloc_sec;
13646 }
13647 }
13648
13649 return reloc_sec;
13650}
13651
13652/* Returns the dynamic reloc section associated with SEC. If the
13653 section does not exist it is created and attached to the DYNOBJ
13654 bfd and stored in the SRELOC field of SEC's elf_section_data
13655 structure.
f8076f98 13656
83bac4b0
NC
13657 ALIGNMENT is the alignment for the newly created section and
13658 IS_RELA defines whether the name should be .rela.<SEC's name>
13659 or .rel.<SEC's name>. The section name is looked up in the
13660 string table associated with ABFD. */
13661
13662asection *
ca4be51c
AM
13663_bfd_elf_make_dynamic_reloc_section (asection *sec,
13664 bfd *dynobj,
13665 unsigned int alignment,
13666 bfd *abfd,
13667 bfd_boolean is_rela)
83bac4b0
NC
13668{
13669 asection * reloc_sec = elf_section_data (sec)->sreloc;
13670
13671 if (reloc_sec == NULL)
13672 {
13673 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13674
13675 if (name == NULL)
13676 return NULL;
13677
3d4d4302 13678 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
13679
13680 if (reloc_sec == NULL)
13681 {
3d4d4302
AM
13682 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13683 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
13684 if ((sec->flags & SEC_ALLOC) != 0)
13685 flags |= SEC_ALLOC | SEC_LOAD;
13686
3d4d4302 13687 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
13688 if (reloc_sec != NULL)
13689 {
8877b5e5
AM
13690 /* _bfd_elf_get_sec_type_attr chooses a section type by
13691 name. Override as it may be wrong, eg. for a user
13692 section named "auto" we'll get ".relauto" which is
13693 seen to be a .rela section. */
13694 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
83bac4b0
NC
13695 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13696 reloc_sec = NULL;
13697 }
13698 }
13699
13700 elf_section_data (sec)->sreloc = reloc_sec;
13701 }
13702
13703 return reloc_sec;
13704}
1338dd10 13705
bffebb6b
AM
13706/* Copy the ELF symbol type and other attributes for a linker script
13707 assignment from HSRC to HDEST. Generally this should be treated as
13708 if we found a strong non-dynamic definition for HDEST (except that
13709 ld ignores multiple definition errors). */
1338dd10 13710void
bffebb6b
AM
13711_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13712 struct bfd_link_hash_entry *hdest,
13713 struct bfd_link_hash_entry *hsrc)
1338dd10 13714{
bffebb6b
AM
13715 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13716 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13717 Elf_Internal_Sym isym;
1338dd10
PB
13718
13719 ehdest->type = ehsrc->type;
35fc36a8 13720 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
13721
13722 isym.st_other = ehsrc->other;
b8417128 13723 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 13724}
351f65ca
L
13725
13726/* Append a RELA relocation REL to section S in BFD. */
13727
13728void
13729elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13730{
13731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13732 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13733 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13734 bed->s->swap_reloca_out (abfd, rel, loc);
13735}
13736
13737/* Append a REL relocation REL to section S in BFD. */
13738
13739void
13740elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13741{
13742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13743 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13744 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 13745 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 13746}